Skip to content

Commit feec7ad

Browse files
committed
feat: store interrupt sends in the steer lane
An interrupt send now stores the message at the end of the steer lane. Before, it went to the head of the queue lane from R0 or R1, and to the tail of the queue lane from I1. FinishInterruption delivers the whole steer lane, so the message goes into history after the steer messages that were queued before it.
1 parent 084b81f commit feec7ad

7 files changed

Lines changed: 42 additions & 55 deletions

File tree

‎coderd/x/chatd/ARCHITECTURE.md‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ Queued messages wait in two lanes. These rules apply to every transition that qu
134134
- When `Create(initialMessages, initialStatus)` creates a child chat, it first locks the family's root chat row with `FOR SHARE` and fails if the root is archived, so a new child never joins an archived family.
135135
- `SetArchived(archived)` sets or clears the archived marker for one chat.
136136
- `SetProject(project)` sets or clears the project of one chat without changing its execution state. A move of a root chat applies it to the root and each descendant in one transaction, and fails without changing any chat if one of them isn't in `W`, `E0` or `E1`. Only the root's `project_id` changes; subagents go through `SetProject` only so that their execution state is checked too. As in a family archive, the root row is locked first, so a concurrent child creation either joins the move or waits for it. A move to the root's current project applies no transition.
137-
- `SendMessage(m, busy_behavior)` inserts a user message directly when the chat is idle, or queues it when the chat is busy. `busy_behavior` must be `queue`, `steer` or `interrupt`. `queue` and `steer` name the lane (see [Queued message lanes](#queued-message-lanes)). Outside `E1`, a busy send with one of them stores `m` in its lane and keeps the status. From `E1`, `busy_behavior=steer` inserts the queued `steer` messages and then `m` into history, and `queue` messages stay queued. From `E1`, `busy_behavior=queue` queues `m` as a `queue` message and promotes the next messages. With `busy_behavior=interrupt`, it also requests interruption, or cancels a pending dynamic-tool action and inserts the message directly. A queued interrupt message goes into the `queue` lane.
137+
- `SendMessage(m, busy_behavior)` inserts a user message directly when the chat is idle, or queues it when the chat is busy. `busy_behavior` must be `queue`, `steer` or `interrupt`. `queue` and `steer` name the lane (see [Queued message lanes](#queued-message-lanes)). Outside `E1`, a busy send with one of them stores `m` in its lane and keeps the status. From `E1`, `busy_behavior=steer` inserts the queued `steer` messages and then `m` into history, and `queue` messages stay queued. From `E1`, `busy_behavior=queue` queues `m` as a `queue` message and promotes the next messages. With `busy_behavior=interrupt`, it also requests interruption, or cancels a pending dynamic-tool action and inserts the message directly. A queued interrupt message goes to the end of the `steer` lane.
138138
- `EditMessage(k, replacement)` clears queued messages, cancels or obsoletes active work, marks the truncated active-history suffix as deleted, inserts the replacement turn followed by any caller-provided suffix messages, and lands in `running`.
139139
- `DeleteQueuedMessage(qid)` removes one queued message without changing the active history.
140140
- `PromoteQueuedMessage(qid)` makes a queued message the next message to process in its lane. It reorders the queue, interrupts active work, cancels pending dynamic-tool action, or promotes into history immediately as required by the input state.
@@ -549,9 +549,7 @@ For `busy_behavior=interrupt`, `SendMessage(m, interrupt)` supports:
549549
- `A0 -> SendMessage(m, interrupt) -> R0`
550550
- `A1 -> SendMessage(m, interrupt) -> R1`
551551

552-
When `SendMessage(m, interrupt)` lands in `I1`, `FinishInterruption(partial?)` finalizes the interrupted suffix and then promotes every `steer` message, or the queue head if there are none. If no worker owns the chat, the same transaction applies `FinishInterruption` instead, so the promotion happens immediately and the chat lands in `R0` or `R1` without an owner. `m` goes into the `queue` lane. From `R0` or `R1`, `m` goes to the head of the `queue` lane, behind the `steer` lane, so `m` is promoted if the `steer` lane is empty. If no worker owns the chat, the response then returns `m` as `message` with `queued` set to false.
553-
554-
From `I1`, `m` joins the tail of the `queue` lane. The promoted messages in `messages` carry the queue IDs of older queued messages in `queued_message_id`, and `queued_message` is `m`.
552+
When `SendMessage(m, interrupt)` lands in `I1`, `m` goes to the end of the `steer` lane. `FinishInterruption(partial?)` finalizes the interrupted suffix and then promotes every `steer` message, so `m` goes into history after the `steer` messages that were queued before it. If no worker owns the chat, the same transaction applies `FinishInterruption` instead, so the promotion happens immediately and the chat lands in `R0` or `R1` without an owner. The response then returns `m` as `message` with `queued` set to false.
555553

556554
Other input states are not supported.
557555

‎coderd/x/chatd/chatd.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,8 @@ const (
448448
// assistant model call.
449449
SendMessageBusyBehaviorSteer SendMessageBusyBehavior = "steer"
450450
// SendMessageBusyBehaviorInterrupt interrupts the active run. It persists
451-
// the partial response before this message runs ahead of queued messages.
452-
// Further messages join the queue tail while the interruption is pending.
451+
// the partial response, then delivers this message and the other steer
452+
// messages ahead of queued messages.
453453
SendMessageBusyBehaviorInterrupt SendMessageBusyBehavior = "interrupt"
454454
)
455455

‎coderd/x/chatd/chatstate/interrupt_unowned_test.go‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func TestSendMessageInterruptUnownedInterruptingChat(t *testing.T) {
301301
"the promoted turn must still go through admission")
302302
})
303303

304-
t.Run("QueuedHeadIsPromotedFirst", func(t *testing.T) {
304+
t.Run("MessageIsPromotedAheadOfQueue", func(t *testing.T) {
305305
t.Parallel()
306306
f := newTestFixture(t)
307307
ctx := testutil.Context(t, testutil.WaitShort)
@@ -314,10 +314,12 @@ func TestSendMessageInterruptUnownedInterruptingChat(t *testing.T) {
314314

315315
result := send(t, f, m, "second", chatstate.BusyBehaviorInterrupt)
316316

317-
require.NotNil(t, result.QueuedMessage, "the new message stays queued behind the promoted head")
317+
require.True(t, result.FinishedInterruption)
318+
require.Nil(t, result.QueuedMessage, "the message is promoted ahead of the queued message")
318319
require.NotEmpty(t, result.InsertedMessages)
319-
assertChatMessageText(t, result.InsertedMessages[len(result.InsertedMessages)-1], "first")
320-
requireQueuedMessageDeleted(ctx, t, f, created.Chat.ID, first.QueuedMessage.ID)
320+
assertChatMessageText(t, result.InsertedMessages[len(result.InsertedMessages)-1], "second")
321+
require.Equal(t, []int64{first.QueuedMessage.ID}, queuedIDsByPosition(ctx, t, f, created.Chat.ID),
322+
"the earlier queued message stays queued")
321323
require.Equal(t, chatstate.StateR1, f.classify(ctx, t, created.Chat.ID))
322324
require.False(t, f.readChat(ctx, t, created.Chat.ID).WorkerID.Valid)
323325
})

‎coderd/x/chatd/chatstate/transitions.go‎

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -580,9 +580,10 @@ type SendMessageResult struct {
580580
FinishedInterruption bool
581581
}
582582

583-
// SendMessage admits a new user message. Depending on input state and
584-
// BusyBehavior, the message lands directly in history, in the queue,
585-
// or replaces the queue head as part of a running-state promotion.
583+
// SendMessage inserts directly in idle states and queues by lane in busy
584+
// states. From E1, it queues the message and resumes with the steer lane,
585+
// or the queue head if no steer messages exist. An interrupt send inserts
586+
// directly from E1 and A*, and otherwise queues in the steer lane.
586587
func (tx *Tx) SendMessage(input SendMessageInput) (SendMessageResult, error) {
587588
chat, from, err := tx.requireFromAllowed(TransitionSendMessage)
588589
if err != nil {
@@ -628,7 +629,7 @@ func (tx *Tx) SendMessage(input SendMessageInput) (SendMessageResult, error) {
628629
// lands in I1; queue and steer keep the input state.
629630
case StateR0, StateR1, StateI0, StateI1:
630631
if input.BusyBehavior == BusyBehaviorInterrupt {
631-
return tx.sendMessageInterrupt(chat, from, input)
632+
return tx.sendMessageInterrupt(chat, input)
632633
}
633634
return tx.queueMessage(chat, input)
634635

@@ -740,13 +741,11 @@ func (tx *Tx) queueMessage(chat database.Chat, input SendMessageInput) (SendMess
740741
}, nil
741742
}
742743

743-
// sendMessageInterrupt queues the message in the queue lane and lands the
744-
// chat in I1. A send that starts an interruption goes to the head of the
745-
// queue lane, behind the steer lane; sends during an interruption join the
746-
// tail to keep arrival order. An unowned chat has no runner to finish the
747-
// interruption, so this transaction finishes it.
748-
func (tx *Tx) sendMessageInterrupt(chat database.Chat, from ExecutionState, input SendMessageInput) (SendMessageResult, error) {
749-
queued, err := tx.insertQueuedMessage(chat.OwnerID, input.Message, database.ChatBusyBehaviorQueue)
744+
// sendMessageInterrupt stores the message at the end of the steer lane and
745+
// lands the chat in I1. An unowned chat has no runner to finish the
746+
// interruption, so this transaction finishes it and delivers the steer lane.
747+
func (tx *Tx) sendMessageInterrupt(chat database.Chat, input SendMessageInput) (SendMessageResult, error) {
748+
queued, err := tx.insertQueuedMessage(chat.OwnerID, input.Message, database.ChatBusyBehaviorSteer)
750749
if err != nil {
751750
return SendMessageResult{}, xerrors.Errorf("insert queued: %w", err)
752751
}
@@ -761,32 +760,18 @@ func (tx *Tx) sendMessageInterrupt(chat database.Chat, from ExecutionState, inpu
761760
}); err != nil {
762761
return SendMessageResult{}, xerrors.Errorf("update status: %w", err)
763762
}
764-
result := SendMessageResult{QueuedMessage: &queued}
765-
startsInterruption := from == StateR0 || from == StateR1
766-
if startsInterruption {
767-
if _, err := tx.store.ReorderChatQueuedMessageToHead(tx.ctx, database.ReorderChatQueuedMessageToHeadParams{
768-
ID: result.QueuedMessage.ID,
769-
ChatID: tx.chatID,
770-
}); err != nil {
771-
return SendMessageResult{}, xerrors.Errorf("reorder queue: %w", err)
772-
}
773-
}
774763
if chat.WorkerID.Valid {
775-
return result, nil
764+
return SendMessageResult{QueuedMessage: &queued}, nil
776765
}
777766
finished, err := tx.finishUnownedInterruption(chat, "Tool execution interrupted by new user message")
778767
if err != nil {
779768
return SendMessageResult{}, err
780769
}
781-
result.InsertedMessages = finished.InsertedMessages
782-
result.PromotedQueuedAt = finished.PromotedQueuedAt
783-
result.FinishedInterruption = true
784-
if slices.ContainsFunc(finished.PromotedMessages, func(m database.ChatMessage) bool {
785-
return m.QueuedMessageID.Int64 == queued.ID
786-
}) {
787-
result.QueuedMessage = nil
788-
}
789-
return result, nil
770+
return SendMessageResult{
771+
InsertedMessages: finished.InsertedMessages,
772+
PromotedQueuedAt: finished.PromotedQueuedAt,
773+
FinishedInterruption: true,
774+
}, nil
790775
}
791776

792777
// EditMessageInput configures [Tx.EditMessage].

‎coderd/x/chatd/chatstate/transitions_helpers_test.go‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -435,20 +435,18 @@ func seedStateMultiQueued(t *testing.T, f *testFixture, state chatstate.Executio
435435
case chatstate.StateI1:
436436
created := createTestChat(t, f)
437437
m := chatstate.NewChatMachine(f.DB, f.Pub, created.Chat.ID)
438-
tailBody := "queued-i1-b"
439-
tail := sendQueuedMessage(t, f, m, tailBody)
440-
require.NotNil(t, tail.QueuedMessage)
441-
// R1 -> I1 via interrupt-mode SendMessage queues a second
442-
// message and flips status to interrupting.
443-
headBody := "queued-i1-a"
444-
head := sendInterruptMessage(t, f, m, created.Chat.ID, headBody)
445-
require.NotNil(t, head.QueuedMessage)
438+
firstBody := "queued-i1-a"
439+
first := sendQueuedMessage(t, f, m, firstBody)
440+
require.NotNil(t, first.QueuedMessage)
441+
secondBody := "queued-i1-b"
442+
second := sendInterruptMessage(t, f, m, created.Chat.ID, secondBody)
443+
require.NotNil(t, second.QueuedMessage)
446444
return seededChat{
447445
chatID: created.Chat.ID,
448446
exists: true,
449447
initialUserMessageID: firstUserMessageID(ctx, t, f, created.Chat.ID),
450-
queuedMessageIDs: []int64{head.QueuedMessage.ID, tail.QueuedMessage.ID},
451-
queuedMessageBodies: []string{headBody, tailBody},
448+
queuedMessageIDs: []int64{first.QueuedMessage.ID, second.QueuedMessage.ID},
449+
queuedMessageBodies: []string{firstBody, secondBody},
452450
}
453451

454452
case chatstate.StateA1:

‎coderd/x/chatd/chatstate/transitions_matrix_test.go‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,8 @@ func sendMessageInterruptCase(from, want chatstate.ExecutionState) transitionCas
13741374
"SendMessage(interrupt) from I* does not insert history")
13751375
newQueued := assertFetchedQueuedMessage(ctx, t, f, seeded.chatID, *result.sendMessage.QueuedMessage)
13761376
assertQueuedMessageText(t, newQueued, "sm-interrupt")
1377+
require.Equal(t, database.ChatBusyBehaviorSteer, newQueued.BusyBehavior,
1378+
"SendMessage(interrupt) from I* stores a steer row")
13771379
wantQueue := append(append([]int64{}, base.queueIDs...), newQueued.ID)
13781380
require.Equal(t, wantQueue, afterQueueIDs,
13791381
"SendMessage(interrupt) from I* appends to the queue tail")
@@ -1391,9 +1393,11 @@ func sendMessageInterruptCase(from, want chatstate.ExecutionState) transitionCas
13911393
"SendMessage(interrupt) from R* returns the queued message")
13921394
newQueued := assertFetchedQueuedMessage(ctx, t, f, seeded.chatID, *result.sendMessage.QueuedMessage)
13931395
assertQueuedMessageText(t, newQueued, "sm-interrupt")
1394-
wantQueue := append([]int64{newQueued.ID}, base.queueIDs...)
1396+
require.Equal(t, database.ChatBusyBehaviorSteer, newQueued.BusyBehavior,
1397+
"SendMessage(interrupt) from R* stores a steer row")
1398+
wantQueue := append(append([]int64{}, base.queueIDs...), newQueued.ID)
13951399
require.Equal(t, wantQueue, afterQueueIDs,
1396-
"SendMessage(interrupt) from R* puts the message at the queue head")
1400+
"SendMessage(interrupt) from R* appends to the queue tail")
13971401
require.Greater(t, after.QueueVersion, base.queueVersion,
13981402
"SendMessage(interrupt) from R* advances queue_version")
13991403
require.Equal(t, database.ChatStatusInterrupting, after.Status,

‎codersdk/chats.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -940,8 +940,8 @@ const (
940940
// the current run finishes.
941941
ChatBusyBehaviorQueue ChatBusyBehavior = "queue"
942942
// ChatBusyBehaviorInterrupt interrupts the active run. It persists
943-
// the partial response before this message runs ahead of queued messages.
944-
// Further messages join the queue tail while the interruption is pending.
943+
// the partial response, then delivers this message and the other
944+
// interrupt messages ahead of queued messages.
945945
ChatBusyBehaviorInterrupt ChatBusyBehavior = "interrupt"
946946
)
947947

0 commit comments

Comments
 (0)