CODY-021: a removed agent is reconciled the moment its turn settles

If the current agent was taken off the bridge's list while its request was
in flight, start-up rightly kept it for that request, but nothing fell back
afterwards: the screen named the default (first in the list) while state,
storage and the conversation were still the removed agent's.

The controller now reconciles inside dispatch, in the same step as any
state change and before anything is drawn: on a resting screen, if the
stored agent is not offered, the stored agent and thread are swapped to the
default's own and the state follows (agent_reconciled). While a request is
in flight nothing changes, so the turn is never moved or repeated. This
also covers an abandoned turn at start.

CODY-022: README scope list no longer says multiple agents are excluded.

Not reviewed, not deployed, not uploaded.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Dan
2026-11-21 16:24:48 -08:00
co-authored by Claude Fable 5.2
parent c8b4fb0310
commit bb3a309427
5 changed files with 114 additions and 3 deletions
+23
View File
@@ -43,6 +43,8 @@ interface AgentThread {
}
const DEFAULT_AGENT = 'default';
/** Screens on which nothing is recording or in flight. */
const RESTING: ReadonlySet<AppState['phase']> = new Set(['ready', 'answer', 'error', 'cancelled']);
function validAgents(value: unknown): Agent[] | undefined {
if (!Array.isArray(value)) return undefined;
@@ -355,6 +357,25 @@ export class Controller {
void this.save();
}
/**
* The current agent has been taken off the bridge's list. While its request is still in
* flight it stays the current agent, so that turn is followed to its end and never moved or
* repeated elsewhere. The moment nothing is in flight, the stored agent, its conversation and
* the state all fall back to the default together; otherwise the screen would name the
* default while the next question was still filed under the removed agent's conversation.
*/
private reconcileRemovedAgent(): void {
if (!this.stored) return;
const current = this.stored.agentId ?? DEFAULT_AGENT;
if (current === DEFAULT_AGENT) return;
if (!RESTING.has(this.state.phase)) return;
if (this.state.agents.some((a) => a.id === current)) return;
this.swapStoredAgent(DEFAULT_AGENT);
void this.save();
this.note('agents', `${current} is no longer offered: back to the default agent`);
this.state = reduce(this.state, { type: 'agent_reconciled', agentId: DEFAULT_AGENT, conversationId: this.stored.conversationId }).state;
}
/** A swipe at Ready: move to the next or previous agent. Does nothing unless there is a choice. */
private cycleAgent(delta: 1 | -1): void {
const agents = this.state.agents;
@@ -495,6 +516,8 @@ export class Controller {
const before = this.state;
const step = reduce(before, event);
this.state = step.state;
// In the same step, before anything is drawn or another question can start.
this.reconcileRemovedAgent();
if (before.phase !== step.state.phase) this.note('state', `${before.phase} -> ${step.state.phase}`);
for (const effect of step.effects) void this.run(effect);
this.render();