Chat: draw agent follow-ups and automatic continues apart from the operator
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>
This commit is contained in:
@@ -271,6 +271,24 @@ const (
|
||||
ChatMessageKindNotice = "notice"
|
||||
)
|
||||
|
||||
// ChatMessage.Origin (and ChatEventFrame.Origin, ChatQueuedPrompt.Origin)
|
||||
// values: who put a user turn into the chat when it was not the operator
|
||||
// typing it now. Empty is the operator's own message. Origin is separate from
|
||||
// Kind, so a client that ignores it still treats the turn as an ordinary user
|
||||
// turn.
|
||||
//
|
||||
// ChatOriginFollowup is a follow-up the agent scheduled for itself
|
||||
// (schedule_followup). ChatOriginAutoContinue is the server resending a chat
|
||||
// after its usage limit reset; ChatOriginAutoSwitch is the server continuing
|
||||
// a chat on another Claude account. ChatOriginScheduled is the operator's own
|
||||
// message they scheduled to send later.
|
||||
const (
|
||||
ChatOriginFollowup = "followup"
|
||||
ChatOriginAutoContinue = "auto_continue"
|
||||
ChatOriginAutoSwitch = "auto_switch"
|
||||
ChatOriginScheduled = "scheduled"
|
||||
)
|
||||
|
||||
// ChatCommand is one slash command or skill the chat's harness session offers.
|
||||
// Name is the word after the slash; the operator sends "/<Name> <args>" as an
|
||||
// ordinary prompt and the harness expands it. ArgumentHint is the harness's
|
||||
@@ -594,6 +612,8 @@ type ChatQueuedPrompt struct {
|
||||
// SendAt (epoch milliseconds) is set on a scheduled prompt: the earliest
|
||||
// time the server will deliver it.
|
||||
SendAt int64 `json:"send_at,omitempty"`
|
||||
// Origin is the ChatMessage.Origin the prompt is delivered with.
|
||||
Origin string `json:"origin,omitempty"`
|
||||
}
|
||||
|
||||
// ChatQueueFrame is a session's current server-side prompt queue, in delivery
|
||||
@@ -659,8 +679,11 @@ type ChatMessage struct {
|
||||
OperationID string `json:"operation_id,omitempty"`
|
||||
// Kind is empty for an ordinary user or assistant turn, or one of the
|
||||
// ChatMessageKind values for content the harness injected.
|
||||
Kind string `json:"kind,omitempty"`
|
||||
Parts []ChatPart `json:"parts"`
|
||||
Kind string `json:"kind,omitempty"`
|
||||
// Origin is one of the ChatOrigin values on a user turn the operator did
|
||||
// not type just now; empty otherwise, or when the server no longer knows.
|
||||
Origin string `json:"origin,omitempty"`
|
||||
Parts []ChatPart `json:"parts"`
|
||||
}
|
||||
|
||||
// ChatDeltaPartID is the stable part id an incremental text or reasoning delta
|
||||
@@ -764,6 +787,8 @@ type ChatEventFrame struct {
|
||||
// MessageKind is the ChatMessage.Kind of the message a text frame starts,
|
||||
// so a live agent report or notice is never drawn as an answer or a prompt.
|
||||
MessageKind string `json:"message_kind,omitempty"`
|
||||
// Origin is the ChatMessage.Origin of a user frame's message.
|
||||
Origin string `json:"origin,omitempty"`
|
||||
// Seq numbers a text or reasoning delta within its session, increasing;
|
||||
// see ChatHistoryPage.LiveSeq. Zero when the harness does not number them.
|
||||
Seq int64 `json:"seq,omitempty"`
|
||||
|
||||
@@ -100,7 +100,7 @@ func chatAutoSwitchText(from, to string) string {
|
||||
if from == "" {
|
||||
from = "the previous account"
|
||||
}
|
||||
return "[Sent automatically by Pipelines] Usage ran out on " + from + ", so Pipelines switched this chat to " + to + ". " +
|
||||
return chatAutoSwitchTextPrefix + " Usage ran out on " + from + ", so Pipelines switched this chat to " + to + ". " +
|
||||
"Before continuing: you already used up a full usage window on this task. Briefly check your progress against my original request and your plan, " +
|
||||
"and make sure you are on track, not going in circles or off the rails. If something looks wrong, stop and ask me; otherwise continue where you left off."
|
||||
}
|
||||
@@ -274,6 +274,7 @@ func (h *chatHub) queueServerPrompt(ctx context.Context, broker chatBroker, sess
|
||||
Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "usr", Text: text},
|
||||
Client: chatAutoSwitchClient,
|
||||
OperationID: operationID,
|
||||
Origin: chatPromptOrigin(operationID, false),
|
||||
}, "")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ func addChatHistory(ctx context.Context, response *protocol.Message, broker chat
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
page.Messages = hub.reconcileHistory(query.SessionID, page.Messages)
|
||||
page.Messages = markChatOrigins(hub.reconcileHistory(query.SessionID, page.Messages))
|
||||
response.ChatMessages = page.Messages // legacy peers keep working.
|
||||
response.ChatHistory = &page
|
||||
// The session's recorded per-turn changes ride with its history, so the
|
||||
|
||||
@@ -124,9 +124,11 @@ type chatMessage struct {
|
||||
operationID string
|
||||
// kind is the ChatMessage.Kind of harness-injected content (an agent
|
||||
// report or a notice); empty for an ordinary turn.
|
||||
kind string
|
||||
order []string
|
||||
parts map[string]*chatPart
|
||||
kind string
|
||||
// origin is the ChatMessage.Origin of a user turn.
|
||||
origin string
|
||||
order []string
|
||||
parts map[string]*chatPart
|
||||
}
|
||||
|
||||
type chatTurn struct {
|
||||
@@ -302,6 +304,9 @@ func (h *chatHub) applyLocked(frame protocol.ChatEventFrame) {
|
||||
if message.operationID == "" && frame.Kind == protocol.ChatEventUser {
|
||||
message.operationID = frame.OperationID
|
||||
}
|
||||
if message.origin == "" && frame.Kind == protocol.ChatEventUser {
|
||||
message.origin = frame.Origin
|
||||
}
|
||||
if message.kind == "" && frame.MessageKind != "" && frame.Kind != protocol.ChatEventUser {
|
||||
message.kind = frame.MessageKind
|
||||
}
|
||||
@@ -635,6 +640,7 @@ func (h *chatHub) snapshot(sessionID string) []protocol.ChatMessage {
|
||||
out.Client = message.client
|
||||
out.OperationID = message.operationID
|
||||
out.Kind = message.kind
|
||||
out.Origin = message.origin
|
||||
for _, partID := range message.order {
|
||||
if part, ok := message.parts[partID]; ok {
|
||||
piece := part.part
|
||||
@@ -674,6 +680,9 @@ func (h *chatHub) reconcileHistory(sessionID string, messages []protocol.ChatMes
|
||||
if out[i].Client == "" {
|
||||
out[i].Client = live.client
|
||||
}
|
||||
if out[i].Origin == "" {
|
||||
out[i].Origin = live.origin
|
||||
}
|
||||
}
|
||||
}
|
||||
return out
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// A user turn the operator did not type just now carries an origin
|
||||
// (protocol.ChatOrigin*), so clients can draw an agent's follow-up or the
|
||||
// server's automatic continue apart from the operator's own messages. While
|
||||
// the server holds the prompt (queued, or retained after delivery) the origin
|
||||
// comes from its operation id; a history page read later, after the server
|
||||
// forgot the prompt, recognises the server-written text prefixes instead.
|
||||
|
||||
const (
|
||||
// chatFollowupTextPrefix starts the text of an agent's follow-up.
|
||||
chatFollowupTextPrefix = "[Follow-up you scheduled at "
|
||||
// chatAutoSwitchTextPrefix starts the text of the auto-switch continue.
|
||||
chatAutoSwitchTextPrefix = "[Sent automatically by Pipelines]"
|
||||
)
|
||||
|
||||
// chatPromptOrigin is the origin of a prompt sent under operationID;
|
||||
// scheduled says it waits for a send_at (the operator's "send later").
|
||||
func chatPromptOrigin(operationID string, scheduled bool) string {
|
||||
switch {
|
||||
case strings.HasPrefix(operationID, chatFollowupPrefix):
|
||||
return protocol.ChatOriginFollowup
|
||||
case strings.HasPrefix(operationID, chatAutoContinuePrefix):
|
||||
return protocol.ChatOriginAutoContinue
|
||||
case strings.HasPrefix(operationID, chatAutoSwitchPrefix):
|
||||
return protocol.ChatOriginAutoSwitch
|
||||
case scheduled:
|
||||
return protocol.ChatOriginScheduled
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// chatTextOrigin is the origin a user turn's text shows on its own: only the
|
||||
// server's own prefixes are recognised, so an operator's message never is.
|
||||
func chatTextOrigin(message protocol.ChatMessage) string {
|
||||
for _, part := range message.Parts {
|
||||
if part.Type != protocol.ChatPartText && part.Type != "" {
|
||||
continue
|
||||
}
|
||||
switch text := strings.TrimSpace(part.Text); {
|
||||
case strings.HasPrefix(text, chatFollowupTextPrefix):
|
||||
return protocol.ChatOriginFollowup
|
||||
case strings.HasPrefix(text, chatAutoSwitchTextPrefix):
|
||||
return protocol.ChatOriginAutoSwitch
|
||||
}
|
||||
return ""
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// markChatOrigins gives each user turn of a history page that has no origin
|
||||
// the one its text shows. The page is copied before a change, since a
|
||||
// harness may keep the slice it returned.
|
||||
func markChatOrigins(messages []protocol.ChatMessage) []protocol.ChatMessage {
|
||||
out := messages
|
||||
for i := range messages {
|
||||
if messages[i].Role != protocol.ChatRoleUser || messages[i].Origin != "" {
|
||||
continue
|
||||
}
|
||||
origin := chatTextOrigin(messages[i])
|
||||
if origin == "" {
|
||||
continue
|
||||
}
|
||||
if &out[0] == &messages[0] {
|
||||
out = append([]protocol.ChatMessage(nil), messages...)
|
||||
}
|
||||
out[i].Origin = origin
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
func TestChatPromptOriginFromOperationID(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
operationID string
|
||||
scheduled bool
|
||||
want string
|
||||
}{
|
||||
{chatFollowupPrefix + "abc", true, protocol.ChatOriginFollowup},
|
||||
{chatRunFollowupPrefix + "7-finished-abc", true, protocol.ChatOriginFollowup},
|
||||
{chatAutoContinuePrefix + "msg_1", true, protocol.ChatOriginAutoContinue},
|
||||
{chatAutoSwitchPrefix + "msg_1", false, protocol.ChatOriginAutoSwitch},
|
||||
{"op-1", true, protocol.ChatOriginScheduled},
|
||||
{"op-1", false, ""},
|
||||
} {
|
||||
if got := chatPromptOrigin(tc.operationID, tc.scheduled); got != tc.want {
|
||||
t.Errorf("chatPromptOrigin(%q, %v) = %q, want %q", tc.operationID, tc.scheduled, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A history page read after the server forgot the prompt still marks the
|
||||
// server's own prompts by their text, and never the operator's or an
|
||||
// assistant's message; the harness's slice is not changed.
|
||||
func TestMarkChatOriginsRecognisesServerText(t *testing.T) {
|
||||
text := func(role, s string) protocol.ChatMessage {
|
||||
return protocol.ChatMessage{Role: role, Parts: []protocol.ChatPart{{Type: protocol.ChatPartText, Text: s}}}
|
||||
}
|
||||
page := []protocol.ChatMessage{
|
||||
text(protocol.ChatRoleUser, "[Follow-up you scheduled at 2026-12-03T10:00:00Z with schedule_followup] check the run"),
|
||||
text(protocol.ChatRoleUser, chatAutoSwitchText("a@example.com", "b@example.com")),
|
||||
text(protocol.ChatRoleUser, "please look at [Follow-up you scheduled at noon]"),
|
||||
text(protocol.ChatRoleAssistant, "[Follow-up you scheduled at 2026-12-03T10:00:00Z with schedule_followup] echo"),
|
||||
{Role: protocol.ChatRoleUser, Origin: protocol.ChatOriginScheduled, Parts: []protocol.ChatPart{{Type: protocol.ChatPartText, Text: chatAutoSwitchTextPrefix}}},
|
||||
}
|
||||
got := markChatOrigins(page)
|
||||
want := []string{protocol.ChatOriginFollowup, protocol.ChatOriginAutoSwitch, "", "", protocol.ChatOriginScheduled}
|
||||
for i, origin := range want {
|
||||
if got[i].Origin != origin {
|
||||
t.Errorf("message %d origin = %q, want %q", i, got[i].Origin, origin)
|
||||
}
|
||||
}
|
||||
if page[0].Origin != "" {
|
||||
t.Fatalf("the harness's page was changed in place")
|
||||
}
|
||||
}
|
||||
|
||||
// The hub keeps a live user turn's origin, so a resume snapshot and a history
|
||||
// page mapped to that turn carry it.
|
||||
func TestChatHubKeepsUserOrigin(t *testing.T) {
|
||||
hub := newChatHub()
|
||||
hub.apply(protocol.ChatEventFrame{
|
||||
SessionID: "ses_1", MessageID: "msg_f", Kind: protocol.ChatEventUser,
|
||||
Client: chatFollowupClient, OperationID: chatFollowupPrefix + "abc", Origin: protocol.ChatOriginFollowup,
|
||||
Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "usr", Text: "later"},
|
||||
})
|
||||
snapshot := hub.snapshot("ses_1")
|
||||
if len(snapshot) != 1 || snapshot[0].Origin != protocol.ChatOriginFollowup {
|
||||
t.Fatalf("snapshot = %+v", snapshot)
|
||||
}
|
||||
got := hub.reconcileHistory("ses_1", []protocol.ChatMessage{{ID: "msg_f", Role: protocol.ChatRoleUser}})
|
||||
if got[0].Origin != protocol.ChatOriginFollowup {
|
||||
t.Fatalf("history origin = %+v", got[0])
|
||||
}
|
||||
}
|
||||
|
||||
// A queued prompt tells clients its origin.
|
||||
func TestQueuedPromptWireOrigin(t *testing.T) {
|
||||
later := time.Now().Add(time.Hour)
|
||||
if got := (&chatQueuedPrompt{operationID: chatFollowupPrefix + "x", sendAt: later}).wire().Origin; got != protocol.ChatOriginFollowup {
|
||||
t.Fatalf("follow-up origin = %q", got)
|
||||
}
|
||||
if got := (&chatQueuedPrompt{operationID: "op-1", sendAt: later}).wire().Origin; got != protocol.ChatOriginScheduled {
|
||||
t.Fatalf("send-later origin = %q", got)
|
||||
}
|
||||
if got := (&chatQueuedPrompt{operationID: "op-1"}).wire().Origin; got != "" {
|
||||
t.Fatalf("queued operator prompt origin = %q", got)
|
||||
}
|
||||
}
|
||||
@@ -332,6 +332,7 @@ func (p *chatQueuedPrompt) wire() protocol.ChatQueuedPrompt {
|
||||
Text: p.text,
|
||||
Client: p.client,
|
||||
Attachments: p.parts,
|
||||
Origin: chatPromptOrigin(p.operationID, !p.sendAt.IsZero()),
|
||||
}
|
||||
if !p.queuedAt.IsZero() {
|
||||
out.QueuedAt = p.queuedAt.UnixMilli()
|
||||
|
||||
@@ -236,7 +236,7 @@ func joinChatDeltas(last, next protocol.Message) (protocol.Message, bool) {
|
||||
func plainChatDelta(f *protocol.ChatEventFrame) bool {
|
||||
return (f.Kind == protocol.ChatEventText || f.Kind == protocol.ChatEventReasoning) && f.Delta != "" && f.Part != nil &&
|
||||
f.Part.Text == "" && f.Step == nil && f.Usage == nil && f.Shell == nil && f.Form == nil && f.Compaction == nil &&
|
||||
len(f.Attachments) == 0 && f.Client == "" && f.OperationID == ""
|
||||
len(f.Attachments) == 0 && f.Client == "" && f.OperationID == "" && f.Origin == ""
|
||||
}
|
||||
|
||||
// utf16Len is the length of s in UTF-16 code units, the unit a Dart client
|
||||
@@ -1227,6 +1227,7 @@ func (h *chatHub) publishDelivery(room *chatRoom, p *chatQueuedPrompt) {
|
||||
Attachments: p.parts,
|
||||
Client: p.client,
|
||||
OperationID: p.operationID,
|
||||
Origin: chatPromptOrigin(p.operationID, !p.sendAt.IsZero()),
|
||||
}
|
||||
h.mu.Lock()
|
||||
turn := h.turnLocked(room.sessionID)
|
||||
|
||||
@@ -139,6 +139,7 @@ func admitChatPrompt(ctx context.Context, request protocol.Message, broker chatB
|
||||
queuedPrompt.sendAt = at
|
||||
}
|
||||
}
|
||||
userFrame.Origin = chatPromptOrigin(operationID, !queuedPrompt.sendAt.IsZero())
|
||||
// Claim the operation before admission so a concurrent retry of the same
|
||||
// prompt is acknowledged rather than delivered twice.
|
||||
hub.rememberOp(sessionID, operationID)
|
||||
|
||||
@@ -123,10 +123,10 @@ func (s *chatToolService) runScheduleFollowup(ctx context.Context, sessionID, me
|
||||
var token [8]byte
|
||||
_, _ = rand.Read(token[:])
|
||||
operationID := chatFollowupPrefix + hex.EncodeToString(token[:])
|
||||
text := fmt.Sprintf("[Follow-up you scheduled at %s with schedule_followup] %s", now.UTC().Format(time.RFC3339), message)
|
||||
text := fmt.Sprintf(chatFollowupTextPrefix+"%s with schedule_followup] %s", now.UTC().Format(time.RFC3339), message)
|
||||
if run != nil {
|
||||
operationID = fmt.Sprintf("%s%d-%s-%s", chatRunFollowupPrefix, run.ID, on, hex.EncodeToString(token[:]))
|
||||
text = fmt.Sprintf("[Follow-up you scheduled at %s with schedule_followup, for when run %d %s; delivered at %s at the latest] %s",
|
||||
text = fmt.Sprintf(chatFollowupTextPrefix+"%s with schedule_followup, for when run %d %s; delivered at %s at the latest] %s",
|
||||
now.UTC().Format(time.RFC3339), run.ID, runFollowupCondition(on), sendAt.UTC().Format(time.RFC3339), message)
|
||||
}
|
||||
if err := s.hub.queueFollowup(ctx, s.broker, sessionID, operationID, text, sendAt); err != nil {
|
||||
@@ -417,6 +417,7 @@ func (h *chatHub) queueFollowup(ctx context.Context, broker chatBroker, sessionI
|
||||
Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "usr", Text: text},
|
||||
Client: chatFollowupClient,
|
||||
OperationID: operationID,
|
||||
Origin: protocol.ChatOriginFollowup,
|
||||
}, "")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -58,9 +58,15 @@ func TestScheduleFollowupQueuesWithoutBlockingTheOperator(t *testing.T) {
|
||||
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}})
|
||||
if removed := readUntil(t, server.ctx, conn, protocol.ChatUnqueue); removed.ChatQueue == nil || removed.ChatQueue.Removed != rows[0].OperationID || len(removed.ChatQueue.Prompts) != 2 {
|
||||
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())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user