e42f9cc031
Claude Code (stream-json stdin) and Codex (turn/steer pinned to the active turn) receive a prompt sent while a turn runs immediately. Other harnesses (OpenCode) get a durable chat_prompt_queue row owned by the session: it survives the last client detaching and a server restart, is delivered in order when the turn ends with or without a client, and is deduplicated by operation id. chat_subscribe carries the queue, chat_queue frames report delivered/removed/failed changes, and chat_unqueue removes a waiting prompt.
90 lines
3.4 KiB
Go
90 lines
3.4 KiB
Go
package claude
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// A message sent while a turn runs is written to the CLI at once: the CLI
|
|
// reads it at the running turn's next step, and the transcript records it as
|
|
// the operator's turn at that point, carrying the caller's message id.
|
|
func TestSteerPromptReachesTheRunningTurn(t *testing.T) {
|
|
chat := testChat(t)
|
|
session := newCreatedChat(t, chat)
|
|
frames := subscribe(t, chat, session.ID)
|
|
if delivered, err := chat.SteerPrompt(context.Background(), session.ID, "too early", "msg_early", nil); err != nil || delivered {
|
|
t.Fatalf("idle session took a mid-turn message: delivered=%v err=%v", delivered, err)
|
|
}
|
|
if err := chat.Prompt(context.Background(), session.ID, "long task"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
started := 0
|
|
waitFrame(t, frames, "turn started", func(frame protocol.ChatEventFrame) bool {
|
|
if frame.Kind == protocol.ChatEventStep && frame.Step != nil && frame.Step.Status == protocol.ChatStepStarted {
|
|
started++
|
|
return true
|
|
}
|
|
return false
|
|
})
|
|
delivered, err := chat.SteerPrompt(context.Background(), session.ID, "also check the docs", "msg_steer", nil)
|
|
if err != nil || !delivered {
|
|
t.Fatalf("running turn refused a mid-turn message: delivered=%v err=%v", delivered, err)
|
|
}
|
|
// The fake CLI only finishes the long turn once it has read the message.
|
|
waitFrame(t, frames, "steered reply", func(frame protocol.ChatEventFrame) bool {
|
|
return frame.Kind == protocol.ChatEventTool || (frame.Kind == protocol.ChatEventText && strings.Contains(frame.Delta, "Noted")) ||
|
|
(frame.Part != nil && strings.Contains(frame.Part.Text, "Noted"))
|
|
})
|
|
waitStepEnded(t, frames)
|
|
steered, err := os.ReadFile(os.Getenv("FAKE_CLAUDE_LOG") + ".steer")
|
|
if err != nil || strings.TrimSpace(string(steered)) != "also check the docs" {
|
|
t.Fatalf("CLI did not receive the message mid-turn: %q %v", steered, err)
|
|
}
|
|
if chat.TurnRunning(session.ID) {
|
|
t.Fatal("turn still running after its result")
|
|
}
|
|
page, err := chat.Messages(context.Background(), session.ID, 50, "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var users []string
|
|
for _, message := range page.Messages {
|
|
if message.Role == protocol.ChatRoleUser {
|
|
users = append(users, message.ID+"="+message.Parts[0].Text)
|
|
}
|
|
}
|
|
if len(users) != 2 || users[1] != "msg_steer=also check the docs" {
|
|
t.Fatalf("transcript users = %v, want the mid-turn message recorded once with its id", users)
|
|
}
|
|
}
|
|
|
|
// When the CLI ends the turn before reading a mid-turn message it runs that
|
|
// message as a turn of its own; its output must be reported as a running turn
|
|
// with a start and an end rather than activity outside any turn.
|
|
func TestSteerPromptFollowUpTurnIsAdopted(t *testing.T) {
|
|
chat := testChat(t)
|
|
session := newCreatedChat(t, chat)
|
|
frames := subscribe(t, chat, session.ID)
|
|
s := chat.session(session.ID)
|
|
s.mu.Lock()
|
|
s.awaitFollowUp = true
|
|
s.mu.Unlock()
|
|
s.adoptFollowUpTurn()
|
|
frame := waitFrame(t, frames, "follow-up start", func(frame protocol.ChatEventFrame) bool {
|
|
return frame.Kind == protocol.ChatEventStep && frame.Step != nil && frame.Step.Status == protocol.ChatStepStarted
|
|
})
|
|
if frame.Step.TurnID == "" || !chat.TurnRunning(session.ID) {
|
|
t.Fatalf("follow-up turn not adopted: %+v", frame.Step)
|
|
}
|
|
s.handleResult(lineEnvelope{Type: "result", Subtype: "success"})
|
|
waitStepEnded(t, frames)
|
|
s.adoptFollowUpTurn()
|
|
if chat.TurnRunning(session.ID) {
|
|
t.Fatal("a turn without outstanding messages was adopted")
|
|
}
|
|
}
|