9a6363b80e
The server now names a user turn's origin (followup, auto_continue, auto_switch, scheduled) on chat messages, user frames and queued prompts, from the prompt's operation id while it holds the prompt and from its own text prefixes in history read later. Origin is a new field, so older clients keep treating these as ordinary user turns. The app draws an agent follow-up, auto-continue and account-switch prompt as a quiet outlined card on the agent's side with an icon, label and the schedule facts from the preamble; the operator's own scheduled message keeps its bubble with a small Scheduled mark. Find searches the drawn text. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
74 lines
3.0 KiB
Go
74 lines
3.0 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// schedule_followup queues a durable, removable prompt under the agent's
|
|
// follow-up label, and a pending follow-up never holds the operator's own
|
|
// prompt back.
|
|
func TestScheduleFollowupQueuesWithoutBlockingTheOperator(t *testing.T) {
|
|
store := openQueueStore(t, filepath.Join(t.TempDir(), "queue.db"))
|
|
broker := newChannelChat()
|
|
server := startQueueServer(t, broker, store)
|
|
token, err := store.ChatToolToken(context.Background(), chatToolSessionScope+"ses_1")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
call := func(arguments string) map[string]any {
|
|
body := `{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"schedule_followup","arguments":` + arguments + `}}`
|
|
request, _ := http.NewRequestWithContext(server.ctx, http.MethodPost, server.srv.URL+chatToolPath+token, strings.NewReader(body))
|
|
request.Header.Set("Content-Type", "application/json")
|
|
response, err := http.DefaultClient.Do(request)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer response.Body.Close()
|
|
var out struct {
|
|
Result map[string]any `json:"result"`
|
|
}
|
|
_ = json.NewDecoder(response.Body).Decode(&out)
|
|
return out.Result
|
|
}
|
|
if result := call(`{"message":"check the run","after_minutes":30}`); result["isError"] != false {
|
|
t.Fatalf("schedule_followup = %v", result)
|
|
}
|
|
rows, err := store.ChatQueuedPrompts(context.Background(), "ses_1")
|
|
if err != nil || len(rows) != 1 || !strings.HasPrefix(rows[0].OperationID, chatFollowupPrefix) || rows[0].Client != chatFollowupClient || rows[0].SendAt.IsZero() || !strings.Contains(rows[0].Text, "check the run") {
|
|
t.Fatalf("stored follow-up = %+v %v", rows, err)
|
|
}
|
|
|
|
conn := dialChatConn(t, server.ctx, server.srv)
|
|
if ack := sendPrompt(t, server.ctx, conn, "p1", "hello", "op-1"); ack.ChatQueued {
|
|
t.Fatalf("the operator's prompt waited behind a pending follow-up: %+v", ack)
|
|
}
|
|
waitFor(t, "operator prompt delivered", func() bool { return broker.promptCount() == 1 })
|
|
endTurn(broker)
|
|
|
|
call(`{"message":"two","after_minutes":60}`)
|
|
call(`{"message":"three","after_minutes":90}`)
|
|
if result := call(`{"message":"four","after_minutes":120}`); result["isError"] != true {
|
|
t.Fatalf("a fourth pending follow-up must be refused: %v", result)
|
|
}
|
|
writeChat(t, server.ctx, conn, protocol.Message{Type: protocol.ChatUnqueue, ID: "rm", ChatUnqueue: &protocol.ChatUnqueueRequest{SessionID: "ses_1", OperationID: rows[0].OperationID}})
|
|
removed := readUntil(t, server.ctx, conn, protocol.ChatUnqueue)
|
|
if removed.ChatQueue == nil || removed.ChatQueue.Removed != rows[0].OperationID || len(removed.ChatQueue.Prompts) != 2 {
|
|
t.Fatalf("the operator removes a follow-up: %+v", removed.ChatQueue)
|
|
}
|
|
for _, prompt := range removed.ChatQueue.Prompts {
|
|
if prompt.Origin != protocol.ChatOriginFollowup {
|
|
t.Fatalf("a queued follow-up is marked as the agent's: %+v", prompt)
|
|
}
|
|
}
|
|
if n := broker.promptCount(); n != 1 {
|
|
t.Fatalf("no follow-up is delivered before its time: %v", broker.promptTexts())
|
|
}
|
|
}
|