Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(agent-host): keep the next queued message cancelable during startup

wojiecihuo6691fca77edc
diff --git a/docs/spec/03-runtime/19-remote-agent-control-protocol.md b/docs/spec/03-runtime/19-remote-agent-control-protocol.md
index 8b067e895..9b7c49218 100644
--- a/docs/spec/03-runtime/19-remote-agent-control-protocol.md
+++ b/docs/spec/03-runtime/19-remote-agent-control-protocol.md
@@ -265,6 +265,9 @@ Queued turns and their idempotency keys are persisted by Rust host-core
(D375), so a Host restart restores the queue in order. A restored queue is
held; release resumes on the first controller attach, local or remote, so a
reboot never starts work unattended.
+Runtime events received before a queued start is acknowledged belong to the
+dequeued turn being started. They must not change the next waiting entry's
+status or prevent that entry from being canceled.
`canceled` is the terminal state of a queued turn that never started;
`interrupted` is the terminal state of a started turn that was stopped or
aborted. Terminal turns are immutable.
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index d686a2e46..f67b3a908 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -1643,6 +1643,19 @@ identify the platform validation still needed.
- **Milestone**: M6+
- **Status**: Draft; host-side reorder covered by `turn_queue` unit tests
+#### E2E-QUEUE-cancel-next-during-start: The next waiting message stays cancelable
+
+- **Preconditions**: An isolated desktop profile and a local provider that can
+ hold a reply open; one active turn followed by queued messages A and B.
+- **Steps**: Release the active reply. While A is executing, cancel B. Finish
+ A and send another message.
+- **Expected**: A's early runtime events retain A's identity. B remains queued
+ at position 1 until cancellation succeeds, never reaches the provider, and
+ disappears from the durable queue. A and the subsequent message complete.
+- **Specs linked**: `03-runtime/19-remote-agent-control-protocol.md` (turn states)
+- **Acceptance**: C (chat), F (persistence)
+- **Status**: Manual local-provider acceptance scenario
+
#### E2E-QUEUE-edit-restores-draft-only-when-input-empty: Edit restores the queued draft
- **Preconditions**: Provider configured; a turn is running with a queued prompt
diff --git a/packages/agent-host/src/agent-host.ts b/packages/agent-host/src/agent-host.ts
index 8de447e95..caa6cdd41 100644
--- a/packages/agent-host/src/agent-host.ts
+++ b/packages/agent-host/src/agent-host.ts
@@ -172,6 +172,8 @@ export class AgentHost {
private readonly states = new Map<string, SessionState>();
private readonly turnIndex = new Map<string, string>();
private readonly runtimeAliases = new Map<string, string>();
+ /** The dequeued turn whose runtime prompt acknowledgement is still pending. */
+ private readonly startingQueuedTurns = new Map<string, TurnRecord>();
private readonly idempotency = new Map<string, IdempotencyEntry>();
private readonly draining = new Set<string>();
/** A pass that arrived while one was running: the queue must be looked at again. */
@@ -825,6 +827,7 @@ export class AgentHost {
const record = await this.queue.shift(sessionId);
if (!record) return;
const turn = this.ensureTurn(state, record.id);
+ this.startingQueuedTurns.set(sessionId, turn);
try {
const started = await this.runtime.prompt({
sessionId,
@@ -863,6 +866,8 @@ export class AgentHost {
this.emit(state, "turn.failed", { turn: this.toRacpTurn(state, turn) }, { turnId: turn.id });
this.renumberQueue(state);
this.notifyQueue(sessionId);
+ } finally {
+ this.startingQueuedTurns.delete(sessionId);
}
}
}
@@ -1227,11 +1232,13 @@ export class AgentHost {
private resolveTurnId(state: SessionState, runtimeTurnId: string): string {
const alias = this.runtimeAliases.get(runtimeTurnId);
if (alias) return alias;
- // A queued turn that started before its prompt() call returned: adopt
- // the runtime id for the head record so the two never diverge.
- const head = this.queue.peek(state.id);
- if (head && state.activeTurnId === undefined && !state.turns.has(runtimeTurnId) && this.draining.has(state.id)) {
- return head.id;
+ // prompt() can emit events before returning its runtime id. The starting
+ // record has already left the queue; the new head belongs to another turn.
+ const starting = this.startingQueuedTurns.get(state.id);
+ if (starting && !starting.runtimeTurnId && !state.turns.has(runtimeTurnId)) {
+ starting.runtimeTurnId = runtimeTurnId;
+ this.runtimeAliases.set(runtimeTurnId, starting.id);
+ return starting.id;
}
return runtimeTurnId;
}