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();
+17
View File
@@ -109,6 +109,12 @@ export type AppEvent =
}
/** The user picked another agent (a swipe at Ready). `conversationId` is that agent's own, if it has one. */
| { type: 'agent_selected'; agentId: string; conversationId: string | null }
/**
* The current agent is no longer offered and nothing is in flight with it: fall back to the
* default agent and ITS conversation. Unlike a selection this applies on any resting screen,
* and leaves what is on screen (the removed agent's last answer or error) in place.
*/
| { type: 'agent_reconciled'; agentId: string; conversationId: string | null }
| { type: 'unpaired' }
/** Semantic actions. `freshId` is a new UUID supplied by the caller in case a turn starts. */
| { type: 'activate'; freshId: string }
@@ -234,6 +240,17 @@ export function reduce(state: AppState, event: AppEvent): Step {
});
}
case 'agent_reconciled': {
if (!IDLE.has(state.phase)) return same(state);
return same({
...state,
agentId: event.agentId,
conversationId: event.conversationId,
conversationEpoch: state.conversationEpoch + 1,
creatingConversation: false,
});
}
case 'unpaired': {
const effects: Effect[] = state.phase === 'listening' ? [{ type: 'discard_capture' }] : [];
return {
+14
View File
@@ -422,6 +422,20 @@ describe('choosing an agent', () => {
expect(late.creatingConversation).toBe(false);
});
it('falling back from a removed agent: applied on any resting screen without disturbing it, never while something is in flight', () => {
const base = run(readyWithAgents(), select).state;
const back: AppEvent = { type: 'agent_reconciled', agentId: 'default', conversationId: CONV };
const answered = run(base, { type: 'submit_text', freshId: ID1, text: 'q' }, { type: 'turn_update', generation: base.generation + 1, result: result(ID1), pages: ['her answer'] }).state;
const after = reduce(answered, back).state;
expect(after.phase).toBe('answer');
expect(after.pages).toEqual(['her answer']); // what she said stays on screen
expect([after.agentId, after.conversationId]).toEqual(['default', CONV]);
expect(after.conversationEpoch).toBe(answered.conversationEpoch + 1);
for (const busy of [run(base, { type: 'activate', freshId: ID1 }).state, run(base, { type: 'submit_text', freshId: ID1, text: 'q' }).state]) {
expect(reduce(busy, back).state, busy.phase).toBe(busy);
}
});
it('with one agent, or none listed, no screen mentions an agent', () => {
for (const agents of [[], [AGENTS[0]!]]) {
const state = run(initialState, { type: 'init', paired: true, conversationId: CONV, captureMaxSeconds: 30, resumeRequestId: null, agents }).state;