diff --git a/cli/internal/daemon/side_question_test.go b/cli/internal/daemon/side_question_test.go index 3f1c98144..d4a975a33 100644 --- a/cli/internal/daemon/side_question_test.go +++ b/cli/internal/daemon/side_question_test.go @@ -2,10 +2,6 @@ package daemon import ( "encoding/json" - "io" - "net" - "net/http" - "net/http/httptest" "testing" "albedo/cli/internal/testwire" @@ -26,60 +22,6 @@ func TestSideQuestionMalformedEventIsProtocolFailure(t *testing.T) { } } -func TestSideQuestionEventWireShape(t *testing.T) { - raw := json.RawMessage(`{"type":"side_question","sequence":3,"data":{"side_question":{"id":"01a0fcea-2b00-7000-8000-000000000001","question":"which file owns retries?","status":"answered","answer":"retry.go","error":null,"asked_at":1794268800000,"continuation_session_id":null}}}`) - got, err := decodeChatEvent(raw) - if err != nil { - t.Fatal(err) - } - if got.Type != EventSideQuestion || got.SideQuestion == nil || got.SideQuestion.Status != "answered" || got.SideQuestion.Answer == nil || *got.SideQuestion.Answer != "retry.go" { - t.Fatalf("decoded side question event: %#v", got) - } -} - -func TestSideQuestionAskAndCancelRoundTrip(t *testing.T) { - var paths []string - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.URL.Path == "/server" { - _, _ = io.WriteString(w, testwire.Server) - return - } - paths = append(paths, r.Method+" "+r.URL.Path) - status := "running" - if r.Method == http.MethodPost { - status = "cancelled" - } else { - body, _ := io.ReadAll(r.Body) - if string(body) != `{"question":"which file?"}` { - t.Errorf("ask body = %s", body) - } - } - _, _ = io.WriteString(w, `{"side_question":{"id":"q1","question":"which file?","status":"`+status+`","answer":null,"error":null,"asked_at":1,"continuation_session_id":null}}`) - })) - t.Cleanup(server.Close) - conn, err := Attach(t.Context(), ConnectionSnapshot{Port: server.Listener.Addr().(*net.TCPAddr).Port, Token: "token"}, nil) - if err != nil { - t.Fatal(err) - } - t.Cleanup(conn.HTTPClient().CloseIdleConnections) - client := NewChatClient(conn, "session-a") - if !client.SideQuestionsSupported() { - t.Fatal("attached daemon did not report side_questions") - } - asked, err := client.AskSideQuestion(t.Context(), "q1", "which file?") - if err != nil || asked.Status != "running" { - t.Fatalf("ask = %#v, err = %v", asked, err) - } - cancelled, err := client.CancelSideQuestion(t.Context(), "q1") - if err != nil || cancelled.Status != "cancelled" { - t.Fatalf("cancel = %#v, err = %v", cancelled, err) - } - want := []string{"PUT /sessions/session-a/side-questions/q1", "POST /sessions/session-a/side-questions/q1/cancel"} - if len(paths) != 2 || paths[0] != want[0] || paths[1] != want[1] { - t.Fatalf("paths = %v, want %v", paths, want) - } -} - func TestSideQuestionMalformedSnapshotIsProtocolFailure(t *testing.T) { snapshot := testwire.Session("test", testwire.GenerationA, 0) snapshot["side_question"] = map[string]any{"id": "q1", "question": "why?", "status": "answered", "answer": nil, "error": nil, "asked_at": 1, "continuation_session_id": nil} @@ -90,16 +32,4 @@ func TestSideQuestionMalformedSnapshotIsProtocolFailure(t *testing.T) { if _, err := decodeSession(data); err == nil { t.Fatal("malformed snapshot side question decoded without error") } - snapshot["side_question"] = map[string]any{"id": "q1", "question": "why?", "status": "answered", "answer": "because", "error": nil, "asked_at": 1, "continuation_session_id": nil} - data, err = json.Marshal(snapshot) - if err != nil { - t.Fatal(err) - } - session, err := decodeSession(data) - if err != nil { - t.Fatal(err) - } - if session.SideQuestion == nil || session.SideQuestion.Answer == nil || *session.SideQuestion.Answer != "because" { - t.Fatalf("snapshot side question: %#v", session.SideQuestion) - } } diff --git a/cli/internal/tui/chat_btw.go b/cli/internal/tui/chat_btw.go index 501f65236..85509f405 100644 --- a/cli/internal/tui/chat_btw.go +++ b/cli/internal/tui/chat_btw.go @@ -181,19 +181,8 @@ func (m *ChatModel) btwContinueCmd() tea.Cmd { } session, generation, client := m.SessionID, m.Generation, m.client questionID := string(exchange.ID) - if exchange.ContinuationSessionID != nil && *exchange.ContinuationSessionID != "" { - target := *exchange.ContinuationSessionID - m.btw.continuing = true - m.btw.continueErr = "" - return func() tea.Msg { - opened, err := client.OpenSideQuestionContinuation(context.Background(), target) - if err != nil { - return ChatSideQuestionContinuedMsg{Err: err, SessionID: session, QuestionID: questionID, Generation: generation} - } - return ChatSideQuestionOpenMsg{Session: opened, SessionID: session, Generation: generation} - } - } - if m.btw.continueHandle == nil { + target := exchange.ContinuationSessionID + if target == nil && m.btw.continueHandle == nil { handle, err := daemon.NewCreation(daemon.CreateSessionRequest{Kind: "side_question", SourceSessionID: session, SideQuestionID: questionID}) if err != nil { m.btw.continueErr = err.Error() @@ -205,11 +194,17 @@ func (m *ChatModel) btwContinueCmd() tea.Cmd { m.btw.continuing = true m.btw.continueErr = "" return func() tea.Msg { - created, err := client.ContinueSideQuestion(context.Background(), handle) + var opened daemon.Session + var err error + if target != nil { + opened, err = client.OpenSideQuestionContinuation(context.Background(), *target) + } else { + opened, err = client.ContinueSideQuestion(context.Background(), handle) + } if err != nil { return ChatSideQuestionContinuedMsg{Err: err, SessionID: session, QuestionID: questionID, Generation: generation} } - return ChatSideQuestionOpenMsg{Session: created, SessionID: session, Generation: generation} + return ChatSideQuestionOpenMsg{Session: opened, SessionID: session, Generation: generation} } } @@ -290,7 +285,6 @@ func (m *ChatModel) handleBtwResult(msg tea.Msg) (tea.Cmd, bool) { var apiErr *daemon.APIError if errors.As(msg.Err, &apiErr) && apiErr.Code == "side_question_continued" && m.btw.exchange != nil && string(m.btw.exchange.ID) == msg.QuestionID && m.btw.exchange.ContinuationSessionID != nil && *m.btw.exchange.ContinuationSessionID != "" { // The stream named the session the other Continue created. - m.btw.continuing = false return m.btwContinueCmd(), true } m.btw.continueErr = msg.Err.Error() diff --git a/cli/internal/tui/shot_btw_test.go b/cli/internal/tui/shot_btw_test.go index 08e51a7d0..14ec4abe7 100644 --- a/cli/internal/tui/shot_btw_test.go +++ b/cli/internal/tui/shot_btw_test.go @@ -1,16 +1,12 @@ -// The /btw side question panel in every state a person sees, for the shot -// gallery, plus the key routing an E2E run cannot force deterministically. +// Panel gallery and admission races that the real-daemon TUI test cannot time. package tui import ( - "io" - "net/http" "strings" "testing" "albedo/cli/internal/daemon" tea "charm.land/bubbletea/v2" - "github.com/charmbracelet/x/ansi" ) func init() { @@ -105,24 +101,6 @@ func TestBtwUnsupportedDaemonNeverBecomesPrompt(t *testing.T) { } } -func TestBtwReopenAndResetClearExchange(t *testing.T) { - m := newTestChatModel(t, &daemon.Session{ID: "session", Workspace: "/work"}) - var cmds []tea.Cmd - m.submitInput("/btw", &cmds) - if !m.btw.open || m.btw.exchange != nil { - t.Fatalf("/btw with no exchange: open=%v exchange=%v", m.btw.open, m.btw.exchange) - } - answer := "retry.go" - m.handleStreamEvent(daemon.StreamEvent{Type: daemon.EventSideQuestion, SideQuestion: &daemon.SideQuestion{ID: "q1", Question: "which file?", Status: "answered", Answer: &answer, AskedAt: 1}}) - if m.btw.exchange == nil || m.btw.exchange.Status != "answered" { - t.Fatalf("stream event did not set the exchange: %#v", m.btw.exchange) - } - m.handleStreamEvent(daemon.StreamEvent{Type: daemon.EventReset, Reason: "recovery"}) - if m.btw.exchange != nil { - t.Fatalf("reset to null kept the exchange: %#v", m.btw.exchange) - } -} - func TestBtwContinueHandleIsStablePerExchange(t *testing.T) { m := newTestChatModel(t, &daemon.Session{ID: "session", Workspace: "/work"}) answer := "retry.go" @@ -147,53 +125,6 @@ func TestBtwContinueHandleIsStablePerExchange(t *testing.T) { } } -func TestBtwAskShowsAnswerAndEscDoesNotInterrupt(t *testing.T) { - conn := commandTestConnection(t, func(w http.ResponseWriter, r *http.Request) { - if r.Method == http.MethodPut && strings.Contains(r.URL.Path, "/side-questions/") { - body, _ := io.ReadAll(r.Body) - if !strings.Contains(string(body), "which file") { - t.Errorf("ask body = %s", body) - } - id := r.URL.Path[strings.LastIndex(r.URL.Path, "/")+1:] - _, _ = io.WriteString(w, `{"side_question":{"id":"`+id+`","question":"which file owns retries?","status":"running","answer":null,"error":null,"asked_at":1,"continuation_session_id":null}}`) - return - } - w.WriteHeader(http.StatusNotFound) - }) - session := &daemon.Session{ID: "session", Workspace: "/work"} - m := NewChatModel(session, daemon.NewChatClient(conn, session.ID)) - t.Cleanup(m.Close) - m.Status.Running = true - var cmds []tea.Cmd - m.TextArea.SetValue("/btw which file owns retries?") - m.submitInput("/btw which file owns retries?", &cmds) - if len(m.pendingUsers) != 0 || len(cmds) != 1 { - t.Fatalf("ask state pending=%d cmds=%d", len(m.pendingUsers), len(cmds)) - } - asked, ok := cmds[0]().(ChatSideQuestionAskedMsg) - if !ok || asked.Err != nil { - t.Fatalf("ask result = %#v", asked) - } - m, _ = m.Update(asked) - if m.btw.exchange == nil || m.btw.exchange.Status != "running" || !m.btw.open { - t.Fatalf("panel after ask: %#v open=%v", m.btw.exchange, m.btw.open) - } - answer := "retry.go" - m.handleStreamEvent(daemon.StreamEvent{Type: daemon.EventSideQuestion, SideQuestion: &daemon.SideQuestion{ID: asked.QuestionID, Question: "which file owns retries?", Status: "answered", Answer: &answer, AskedAt: 1}}) - if view := ansi.Strip(m.View()); !strings.Contains(view, "retry.go") { - t.Fatalf("answer not in panel:\n%s", view) - } - m, _ = m.Update(tea.KeyPressMsg{Code: tea.KeyEscape}) - if m.btw.open || m.Stopping || !m.Status.Running { - t.Fatalf("esc state open=%v stopping=%v running=%v", m.btw.open, m.Stopping, m.Status.Running) - } - // /btw alone reopens the same exchange. - m.submitInput("/btw", &cmds) - if !m.btw.open || m.btw.exchange == nil || m.btw.exchange.Status != "answered" { - t.Fatalf("reopen state open=%v exchange=%#v", m.btw.open, m.btw.exchange) - } -} - func TestBtwStaleAskResultIsIgnored(t *testing.T) { m := newTestChatModel(t, &daemon.Session{ID: "session", Workspace: "/work"}) m.btw.pending = &btwAdmission{id: "q-new"} diff --git a/web/src/lib/chat/SideQuestion.svelte b/web/src/lib/chat/SideQuestion.svelte index 16e31a91c..ff43516c7 100644 --- a/web/src/lib/chat/SideQuestion.svelte +++ b/web/src/lib/chat/SideQuestion.svelte @@ -2,14 +2,10 @@ import type { SideQuestions } from '../side-questions.svelte'; import Markdown from './Markdown.svelte'; - // A side question's card, docked above the composer inside its chat. It - // holds one exchange; the transcript never carries it. let { questions, onopen }: { questions: SideQuestions; onopen: (sessionId: string) => void } = $props(); const exchange = $derived(questions.exchange); - const statusText = $derived( - exchange?.status === 'running' ? 'asking' : exchange?.status === 'answered' ? 'answered' : exchange?.status === 'failed' ? 'failed' : 'cancelled', - ); + const statusText = $derived(exchange?.status === 'running' ? 'asking' : exchange?.status); function keydown(event: KeyboardEvent) { if (event.key !== 'Escape') return; @@ -133,9 +129,6 @@ cursor: default; } @media (max-width: 760px) { - .side-question { - width: 100%; - } .answer { max-height: 36vh; }