diff --git a/CHANGELOG.md b/CHANGELOG.md index 0034b02..8bb9ede 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,9 +9,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed -- **Replies say which ask they answer** — A reply reaches the asking agent with the first 400 - bytes of its ask quoted under the header, and `fleet_inbox` lists it as `re_text`, so an agent - with several questions out can tell "1. yes" to one apart from another without looking it up. +- **Replies say which ask they answer** — A reply reaches the asking agent with the first 400 bytes + of its ask quoted under the header, and `fleet_inbox` lists it as `re_text`, so an agent with + several questions out can tell "1. yes" to one apart from another without looking it up. +- **Cancel a steered message** — A person's message sent Now (steer) into a running turn waits in + the queue until the turn's next step, when the agent reads steered input anyway, so it can be + cancelled until then; Codex cannot take input back once it is in. Its pending bubble no longer + sticks until a reload: it goes once the turn that carried it ends. ## [0.1.6] — 2026-10-25 diff --git a/docs/messaging.md b/docs/messaging.md index 01e0711..443d6b7 100644 --- a/docs/messaging.md +++ b/docs/messaging.md @@ -444,6 +444,11 @@ it runs this version. distracted. Nodes before 0.1.5 take `auto` as `queue`. - Messages that arrive while a steer is being delivered go in together, as one steer input, each with its own header. +- A person's message sent in mode `auto` (the web UI's Now) into a running turn waits in the session's + queue until the turn's next step (a tool call or message finishing), when + the agent would read it anyway, so it can be cancelled until then. If the + turn ends first, it starts the next one. Mode `steer`, Send now, and + agents' messages go in at once. - `interrupt` puts the message at the head of the queue and cancels the running turn. - When a session goes idle, the messages in its queue are delivered diff --git a/docs/web-ui.md b/docs/web-ui.md index 4d82274..18601d4 100644 --- a/docs/web-ui.md +++ b/docs/web-ui.md @@ -43,8 +43,11 @@ system; the choice is kept per browser. (steers it into the running turn, or interrupts a session that cannot take one mid-turn) and **Cancel**, and when it expires undelivered ("expires 14:30", from a node that enforces it); it becomes the usual - message when its turn starts. A message sent **now** into a running - turn shows the same way, "Steered: the agent reads it at its next + message when its turn starts. A message you send **now** into a running + turn waits in the queue for the turn's next step, when the agent reads + steered input anyway, so it shows as **Queued** ("Goes in at the + agent's next step") with **Cancel** until then; once it has gone in it + shows the same way, "Steered: the agent reads it at its next step", until the harness takes it in (Codex reads a steer at its next step, which can be a long tool call away). Under a message from you (or another person) a receipt says when it went into the session's input, diff --git a/internal/node/node.go b/internal/node/node.go index 7895fd2..0254d4f 100644 --- a/internal/node/node.go +++ b/internal/node/node.go @@ -124,6 +124,10 @@ type queuedMsg struct { ReplyTo string `json:"reply_to,omitempty"` // ReplyToText: the start of the ask a reply answers. ReplyToText string `json:"reply_to_text,omitempty"` + // AtStep: a steer into the running turn, held until that turn's next + // step (when the agent would read it anyway), so it can still be + // cancelled. If the turn ends first it starts the next one. + AtStep bool `json:"at_step,omitempty"` // Attachments: metadata only. The bytes wait in the session's // attachment directory (see images.go) until the message is delivered. Attachments []model.Attachment `json:"attachments,omitempty"` @@ -240,6 +244,7 @@ type session struct { // Steers in flight: steering is set while one is being delivered, and // steerBuf holds the messages that arrived meanwhile, to go in together. steering bool + stepping bool // stepSteers is running steerBuf []*steerReq // solo holds queued messages that failed in a batch: each is retried // on its own, so only one that fails alone is reported failed. @@ -616,6 +621,7 @@ func (s *session) sink(ev adapter.Event) { s.appendLocked(ev.Kind, ev.Payload) idle := false save := false + step := false // the running turn finished a step, with steers held for it switch ev.Kind { case model.KindTurnStarted: if p, ok := ev.Payload.(model.TurnStarted); ok { @@ -634,6 +640,9 @@ func (s *session) sink(ev adapter.Event) { idle = true // re-check delivery and settling now that the turn is done } case model.KindItemCompleted: + if p, ok := ev.Payload.(model.Item); ok && p.TurnID == s.activeTurn && p.ItemType != "userMessage" && s.hasAtStepLocked() { + step = true + } if p, ok := ev.Payload.(model.Item); ok && p.ItemType == "agentMessage" { if p.Text != "" { s.lastText = p.Text @@ -666,9 +675,51 @@ func (s *session) sink(ev adapter.Event) { if idle { go s.n.tryDeliver(s) } + if step { + go s.n.stepSteers(s) + } s.n.wakeSender() } +func (s *session) hasAtStepLocked() bool { + for _, m := range s.rec.Queue { + if m.AtStep && !m.Sending { + return true + } + } + return false +} + +// stepSteers steers the messages held for the running turn's next step +// (AtStep) into it, as promote does: the agent reads steered input at its +// next step, so holding them until then costs no time and leaves them +// cancellable meanwhile. +func (n *Node) stepSteers(s *session) { + s.mu.Lock() + if s.stepping { + s.mu.Unlock() + return + } + s.stepping = true + var ids []string + for _, m := range s.rec.Queue { + if m.AtStep && !m.Sending { + ids = append(ids, m.ID) + } + } + s.mu.Unlock() + defer func() { + s.mu.Lock() + s.stepping = false + s.mu.Unlock() + }() + for _, id := range ids { + if _, err := n.promote(context.Background(), s, &wire.QueuedCmd{MessageID: id, Mode: model.ModeSteer}); err != nil { + n.logf("session %s: steer of held message %s: %v", s.rec.Path, id, err) + } + } +} + func (s *session) flushDeltaLocked() { if s.delta == nil { return @@ -1393,6 +1444,12 @@ func (n *Node) deliver(ctx context.Context, s *session, m *wire.MessageCmd) (jso Attachments: model.MetaOnly(images), ExpiresAt: m.ExpiresAt} caps := h.Caps() steer := caps.Steer && (m.Mode == model.ModeSteer || (m.Mode == model.ModeAuto && s.turnRunning(h))) + if steer && m.Mode == model.ModeAuto && s.turnRunning(h) && strings.HasPrefix(m.From, "user:") { + // A person's message sent Now goes into the running turn at its + // next step, from the queue, where they can still cancel it until + // then (stepSteers). An explicit steer, and agents', go in at once. + steer, msg.AtStep = false, true + } s.mu.Lock() if s.acceptedLocked(m.ID, time.Now()) || s.queuedLocked(m.ID) >= 0 { s.mu.Unlock() @@ -1508,7 +1565,11 @@ func (n *Node) deliver(ctx context.Context, s *session, m *wire.MessageCmd) (jso if interrupt { mode = model.ModeInterrupt } - s.emit(model.KindMessageQueued, model.MessageEvent{MessageID: msg.ID, From: msg.From, Text: msg.Text, Mode: mode, Attachments: msg.Attachments}) + queued := mode + if msg.AtStep { + queued = model.ModeSteer + } + s.emit(model.KindMessageQueued, model.MessageEvent{MessageID: msg.ID, From: msg.From, Text: msg.Text, Mode: queued, Attachments: msg.Attachments}) if interrupt { if err := h.Interrupt(ctx); err != nil && !errors.Is(err, adapter.ErrNotActive) { n.logf("session %s: interrupt for message %s: %v", s.rec.Path, msg.ID, err) @@ -2063,7 +2124,7 @@ func (n *Node) queueList(s *session) json.RawMessage { res := wire.QueueResult{Messages: []wire.QueueEntry{}} for i, m := range s.rec.Queue { res.Messages = append(res.Messages, wire.QueueEntry{ID: m.ID, From: m.From, Kind: m.Kind, Owner: m.Owner, - Images: len(m.Attachments), Position: i, ExpiresAt: m.ExpiresAt}) + Images: len(m.Attachments), Position: i, ExpiresAt: m.ExpiresAt, AtStep: m.AtStep}) } return model.Marshal(res) } diff --git a/internal/node/receipts_test.go b/internal/node/receipts_test.go index a7b0af8..cff93ae 100644 --- a/internal/node/receipts_test.go +++ b/internal/node/receipts_test.go @@ -57,10 +57,11 @@ func TestSeenReceipts(t *testing.T) { t.Fatalf("seen %+v", got) } - // Steered into a turn that is interrupted: not seen. + // Steered into a turn that is interrupted: not seen. (An agent's: a + // person's waits for the turn's next step, TestPersonSteerAtStep.) turn("t2") c := ulid.New() - if dr := send(t, n, s, wire.MessageCmd{ID: c, From: "user:web", Owner: true, Text: "three", Mode: model.ModeAuto}); dr.Mode != model.ModeSteer { + if dr := send(t, n, s, wire.MessageCmd{ID: c, From: "devbox/b", Text: "three", Mode: model.ModeAuto}); dr.Mode != model.ModeSteer { t.Fatalf("mode %s", dr.Mode) } done("t2", "interrupted") @@ -72,7 +73,7 @@ func TestSeenReceipts(t *testing.T) { // before that. turn("t3") d := ulid.New() - send(t, n, s, wire.MessageCmd{ID: d, From: "user:web", Owner: true, Text: "four", Mode: model.ModeAuto}) + send(t, n, s, wire.MessageCmd{ID: d, From: "devbox/b", Text: "four", Mode: model.ModeAuto}) reply("t3", "i3") done("t3", "completed") if got := seen(); len(got) != 3 || got[2].MessageID != d || got[2].TurnID != "t3" || got[2].ItemID != "i3" { @@ -83,3 +84,38 @@ func TestSeenReceipts(t *testing.T) { t.Fatalf("delivered after seen: %s", kinds) } } + +// A person's steer into a running turn waits in the queue for the turn's +// next step, when the agent would read it anyway: until then it can be +// cancelled; at the step it goes into the turn. +func TestPersonSteerAtStep(t *testing.T) { + h := &queueHandle{status: model.StatusActive, steer: true} + n, s := queueNode(t, h) + h.sink(adapter.Event{Kind: model.KindTurnStarted, Payload: model.TurnStarted{TurnID: "t1"}}) + step := func() { + h.sink(adapter.Event{Kind: model.KindItemCompleted, Payload: model.Item{TurnID: "t1", ItemID: ulid.New(), ItemType: "commandExecution"}}) + } + + a := ulid.New() + if dr := send(t, n, s, wire.MessageCmd{ID: a, From: "user:web", Owner: true, Text: "stop that", Mode: model.ModeAuto}); dr.Mode != model.ModeQueue { + t.Fatalf("a person's steer: %s, want queue (held)", dr.Mode) + } + if len(h.deliveries()) != 0 || s.queueLen() != 1 { + t.Fatal("steered at once") + } + if err := n.cancelQueued(s, &wire.QueuedCmd{MessageID: a}); err != nil { + t.Fatalf("cancel before the step: %v", err) + } + step() + if len(h.deliveries()) != 0 { + t.Fatal("a cancelled steer went in") + } + + b := ulid.New() + send(t, n, s, wire.MessageCmd{ID: b, From: "user:web", Owner: true, Text: "and this", Mode: model.ModeAuto}) + step() + eventually(t, "the held steer in at the step", func() bool { + d := h.deliveries() + return len(d) == 1 && d[0].mode == adapter.DeliverSteer && s.queueLen() == 0 + }) +} diff --git a/internal/web/assets/app.js b/internal/web/assets/app.js index 45d9e36..41bb34c 100644 --- a/internal/web/assets/app.js +++ b/internal/web/assets/app.js @@ -1020,7 +1020,7 @@ function sessionView(id, full) { clock(m.expires_at) ? h('span', {title: 'Dropped undelivered if the host is still offline then', text: `expires ${clock(m.expires_at)}`}) : null, h('button', {type: 'button', class: 'link', text: 'Cancel', onclick: (e) => cancelQueued([m], e.currentTarget)})) : h('div', {class: 'queued-foot'}, - h('span', {text: m.position ? `Queued, ${m.position} ahead` : 'Queued, next'}), + h('span', {text: m.at_step ? 'Goes in at the agent\'s next step' : m.position ? `Queued, ${m.position} ahead` : 'Queued, next'}), // The node drops it undelivered at expires_at (a node from // 0.1.5); Send now still sends it. clock(m.expires_at) ? h('span', {title: 'Not delivered after this; Send now still sends it', text: `expires ${clock(m.expires_at)}`}) : null, @@ -1273,6 +1273,11 @@ function sessionView(id, full) { add(h('div', {class: 'sys', text: `A queued message from ${p.from} expired before it was delivered`})); queueSoon(); break; + case 'message.seen': + // The turn that carried it is over: it has been read, whether or + // not its input showed in the transcript. + dropSending(sending.find((x) => x.id === p.message_id)); + break; case 'message.queued': case 'message.promoted': case 'session.queue': @@ -1315,8 +1320,9 @@ function sessionView(id, full) { const known = items.get(p.item_id); switch (p.item_type) { case 'userMessage': { - if (done || known) return; const text = (raw.content || []).filter((c) => c.type === 'text').map((c) => c.text).join('\n'); + takenIn(text); // a steer may show only as completed + if (done || known) return; const parts = splitRouted(text).map((part) => { const r = parseRouted(part); return r diff --git a/internal/wire/wire.go b/internal/wire/wire.go index fbf4848..c92309d 100644 --- a/internal/wire/wire.go +++ b/internal/wire/wire.go @@ -167,6 +167,8 @@ type QueueEntry struct { // ExpiresAt is when the node drops it undelivered (nodes from 0.1.5, // for messages that came with one). ExpiresAt time.Time `json:"expires_at,omitzero"` + // AtStep: it goes into the running turn at the turn's next step. + AtStep bool `json:"at_step,omitempty"` } // SpawnCmd carries the coordinator's session record and the request.