diff --git a/README.md b/README.md index 4b3b042..823d5e9 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ the implementer read from the server's log, and what Cody reproduced independent | Path | State | |---|---| | Client, bridge and contract | Implemented. | -| Automated tests with fakes (296) | Passing. | +| Automated tests with fakes (299) | Passing. | | Client in Even's official simulator | Passing (7 scenarios). Simulator only. | | Bridge against a real Hermes gateway | Passing (9 scenarios) with a **stub model**. | | Built-in speech-to-text (Whisper, in the bridge) | Passing (11 scenarios) on a real speech recording, including with no network. Not yet on audio from the glasses. | @@ -120,5 +120,6 @@ What each suite does and does not show is in `docs/test-evidence.md`. ## Not in this slice -Wake words, always-on listening, camera features, text-to-speech, multiple agents, billing, store -launch, and running alongside Even's native captions (the public SDK does not offer it). +Wake words, always-on listening, choosing an agent by saying its name, camera features, +text-to-speech, billing, store launch, and running alongside Even's native captions (the public +SDK does not offer it). diff --git a/apps/even-client/src/core/controller.ts b/apps/even-client/src/core/controller.ts index 0ed2005..703b931 100644 --- a/apps/even-client/src/core/controller.ts +++ b/apps/even-client/src/core/controller.ts @@ -43,6 +43,8 @@ interface AgentThread { } const DEFAULT_AGENT = 'default'; +/** Screens on which nothing is recording or in flight. */ +const RESTING: ReadonlySet = 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(); diff --git a/apps/even-client/src/core/reducer.ts b/apps/even-client/src/core/reducer.ts index f6715e3..706f8f0 100644 --- a/apps/even-client/src/core/reducer.ts +++ b/apps/even-client/src/core/reducer.ts @@ -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 { diff --git a/apps/even-client/test/reducer.test.ts b/apps/even-client/test/reducer.test.ts index 1b2c4c6..bf8707d 100644 --- a/apps/even-client/test/reducer.test.ts +++ b/apps/even-client/test/reducer.test.ts @@ -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; diff --git a/tests/integration/agents.test.ts b/tests/integration/agents.test.ts index 252bb49..ccd4f3f 100644 --- a/tests/integration/agents.test.ts +++ b/tests/integration/agents.test.ts @@ -513,6 +513,62 @@ describe('glasses: which agent it reopens on', () => { expect(snapshot.state.agents.map((a) => a.id)).toEqual(['default', 'cody']); }); + it('CODY-021: removed while her question is in flight: the turn stays hers and settles honestly, then screen, state, storage and routing all move to the default', async () => { + await paired(); + await ask('pepper one'); + await backToReady(); + device.swipeDown(); // Wendy + h.hermes.next({ kind: 'hang' }); + controller.askText('wendy, do the thing'); + await until('in flight with Wendy', () => phase() === 'thinking' && h.hermes.executions === 2); + const wendyRequest = snapshot.state.requestId!; + await until('request id saved', () => JSON.parse(device.storage.get('hermeven.v1')!).activeRequestId === wendyRequest); + // The owner takes Wendy off the list and restarts the bridge; the page is reopened. + (h.config.agents as unknown[]).splice(1, 1); + await h.restartBridge(); + const before = JSON.parse(device.storage.get('hermeven.v1')!); + device.storage.set('hermeven.v1', JSON.stringify({ ...before, bridgeUrl: h.url })); + await relaunch(); + // Still her turn while it is unresolved: re-attached by id, not moved, not repeated. + expect(snapshot.state.requestId ?? wendyRequest).toBe(wendyRequest); + await until('her turn settled', () => phase() === 'error'); + expect(snapshot.state.error?.code).toBe('outcome_unknown'); + expect(h.hermes.executedBy).toEqual([null, 'wendy']); + // The moment it settled, everything agrees on the default agent. + expect(snapshot.state.agentId).toBe('default'); + await until('saved', () => JSON.parse(device.storage.get('hermeven.v1')!).agentId === 'default'); + await backToReady(); + expect(device.frame.body).toBe('Pepper\n\nReady.'); + expect(snapshot.state.agentId).toBe('default'); + // The next question goes to the default agent, in the DEFAULT agent's own conversation. + await ask('pepper two'); + expect(device.frame.body).toContain('turn=2'); + expect(device.frame.body).toContain('pepper one'); + expect(h.hermes.executedBy).toEqual([null, 'wendy', null]); + const posts = h.hermes.requests.filter((r) => r.method === 'POST' && r.path === '/v1/runs'); + expect(posts.filter((p) => p.profile === 'wendy').length).toBe(1); + expect(snapshot.state.conversationId).toBe(JSON.parse(device.storage.get('hermeven.v1')!).conversationId); + }); + + it('CODY-021: the same when the question is asked straight from the removed agent\'s result screen, without going back to Ready', async () => { + await paired(); + device.swipeDown(); // Wendy + h.hermes.next({ kind: 'hang' }); + controller.askText('wendy, do the thing'); + await until('in flight with Wendy', () => phase() === 'thinking' && h.hermes.executions === 1); + await until('request id saved', () => typeof JSON.parse(device.storage.get('hermeven.v1')!).activeRequestId === 'string'); + (h.config.agents as unknown[]).splice(1, 1); + await h.restartBridge(); + const before = JSON.parse(device.storage.get('hermeven.v1')!); + device.storage.set('hermeven.v1', JSON.stringify({ ...before, bridgeUrl: h.url })); + await relaunch(); + await until('her turn settled', () => phase() === 'error'); + await ask('straight on'); // from the error screen + expect(h.hermes.executedBy).toEqual(['wendy', null]); + expect(device.frame.body).toContain('turn=1'); + expect(sent.filter((s) => s.path === '/v1/conversations').map((c) => JSON.parse(c.body).agent_id)).toEqual(['wendy', 'default']); + }); + it('the bridge cannot be reached for the list at start: the last known list is used', async () => { await paired(); device.swipeDown();