Repository navigation
Cancel a steered message until the agent's next step; stuck steer bubbles #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve expiry during automatic step promotion. If a held message expires just before this call,
|
||
| 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) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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)); | ||
|
Comment on lines
+1276
to
+1279
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Subscribe to
|
||
| 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 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
grep -n 'stepping\|stepSteers\|AtStep' internal/node/node.go sed -n 636,730p internal/node/node.goRepository: jm2/switchworks
Length of output: 3434
Script executed:
Repository: jm2/switchworks
Length of output: 21062
Keep step promotion pending while
stepSteersis running.When another non-user item completes while
stepSteersholdss.stepping, the new invocation returns. The current pass only promotes IDs from its initial snapshot, so a message queued afterward can miss the running turn’s next step and start in the next turn instead.Record a pending step and process newly eligible
AtStepmessages before the currentstepSteersrun clearss.stepping.