CODY-016, 017, 018: launch source cannot be missed; a failed idle exit is retried

CODY-016. The SDK pushes the launch source once, early. The Even adapter now
subscribes in its constructor, remembers the value and replays it to a
listener that subscribes later. appMenu still does not arm idle exit.

CODY-018. exitNow() returns whether the host confirmed. The adapter keeps at
most one shutdown call in flight (through the same queue as every other host
call), lets a repeat request wait on that call, and never calls the host
again after a confirmed exit, including one confirmed late. The controller
marks itself as gone only on confirmation or on a system_exit/abnormal_exit
event; otherwise it retries after 3 s, at most 4 attempts in a row, under the
same resting-screen and phone-grace guards, and starts again after activity.

CODY-017. README: one owner-reported locked-phone success; pocketed,
five-minute and ten-cycle runs unverified.

Tests: production-adapter tests with a stand-in host, including the default
30 s under a fake clock; a simulator scenario that performs the real exit
(development-only ?idleExit=fast). Not reviewed. 0.1.1 is not uploaded.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Dan
2026-11-21 14:38:29 -08:00
co-authored by Claude Fable 5.2
parent 1ae105ed18
commit bbde3c86a8
12 changed files with 679 additions and 42 deletions
+58 -6
View File
@@ -90,6 +90,10 @@ export interface ControllerOptions {
phoneGraceMs?: number;
/** A question asked after this long a silence starts a new conversation. */
conversationGapMs?: number;
/** If the host does not confirm the exit, try again after this long... */
exitRetryMs?: number;
/** ...up to this many attempts in a row. Any activity starts the count again. */
exitAttempts?: number;
}
interface Capture {
@@ -146,7 +150,14 @@ export class Controller {
private idleArmed = false;
private phoneBusyUntil = 0;
private launchSource: 'appMenu' | 'glassesMenu' | 'unknown' = 'unknown';
private readonly exitRetryMs: number;
private readonly exitAttempts: number;
/** The host confirmed we left. Final. */
private exited = false;
/** An exit attempt is in progress: no second one starts until it has answered. */
private leaving = false;
/** Unconfirmed exit attempts since the last activity. */
private exitFailures = 0;
/** Set while a long press that STARTED a recording is still held down. */
private heldPress: { generation: number; at: number } | null = null;
@@ -162,6 +173,8 @@ export class Controller {
this.idleExitMs = options.idleExit === null ? null : { ...DEFAULT_IDLE_EXIT_MS, ...options.idleExit };
this.phoneGraceMs = options.phoneGraceMs ?? 120_000;
this.conversationGapMs = options.conversationGapMs ?? 10 * 60_000;
this.exitRetryMs = options.exitRetryMs ?? 3_000;
this.exitAttempts = Math.max(1, options.exitAttempts ?? 4);
}
// ------------------------------------------------------------------ public
@@ -380,9 +393,11 @@ export class Controller {
* recording, transcribing, waiting for the agent or for an approval, or while disconnected
* with a request outstanding.
*/
private scheduleIdleExit(): void {
private scheduleIdleExit(retry = false): void {
if (this.idleTimer) clearTimeout(this.idleTimer);
this.idleTimer = null;
// Anything other than our own retry is activity: the full wait and the attempt count restart.
if (!retry) this.exitFailures = 0;
if (this.stopped || this.exited || !this.idleExitMs || !this.idleArmed || !this.hudReady) return;
const phase = this.state.phase;
const wait =
@@ -394,24 +409,56 @@ export class Controller {
? this.idleExitMs.message
: null;
if (wait === null) return;
const delay = Math.max(wait, this.phoneBusyUntil - this.now() + wait);
const delay = retry ? this.exitRetryMs : Math.max(wait, this.phoneBusyUntil - this.now() + wait);
const generation = this.state.generation;
this.idleTimer = setTimeout(() => {
this.idleTimer = null;
// Something happened in the meantime, or the screen is no longer a resting one.
if (this.stopped || this.exited || this.state.generation !== generation || this.state.phase !== phase) return;
if (this.now() < this.phoneBusyUntil) return this.scheduleIdleExit();
// An earlier attempt has not answered yet: look again shortly, never start a second.
if (this.leaving) return this.scheduleIdleExit(true);
void this.leaveGlasses(`idle on ${phase}`);
}, delay);
}
private async leaveGlasses(reason: string): Promise<void> {
if (this.exited) return;
this.exited = true;
if (this.exited || this.leaving) return;
this.leaving = true;
const generation = this.state.generation;
this.note('lifecycle', `leaving the glasses: ${reason}`);
this.emit();
await this.closeMic();
await this.device.exitNow();
let confirmed = false;
try {
await this.closeMic();
confirmed = await this.device.exitNow();
} catch {
confirmed = false;
} finally {
this.leaving = false;
}
if (this.stopped) return;
if (confirmed) {
this.exited = true;
if (this.idleTimer) clearTimeout(this.idleTimer);
this.idleTimer = null;
this.note('lifecycle', 'left the glasses');
this.emit();
return;
}
// Not confirmed: refused, failed, or no answer in time. We are still on the glasses as far
// as we know, so this must not be final. If the user did something meanwhile, their activity
// already restarted the full countdown; otherwise try again, a bounded number of times.
if (this.state.generation !== generation) return;
this.exitFailures += 1;
if (this.exitFailures >= this.exitAttempts) {
this.note('lifecycle', `the host did not confirm leaving after ${this.exitFailures} attempts; staying until the next activity`);
this.emit();
return;
}
this.note('lifecycle', 'the host did not confirm leaving; will try again');
this.emit();
this.scheduleIdleExit(true);
}
// ----------------------------------------------------------------- effects
@@ -799,6 +846,11 @@ export class Controller {
return;
case 'system_exit':
case 'abnormal_exit':
// The glasses say we are gone (the user or the system closed the app). That is as final
// as our own confirmed exit: no idle exit, and no retry of one, is sent after it.
this.exited = true;
if (this.idleTimer) clearTimeout(this.idleTimer);
this.idleTimer = null;
this.note('lifecycle', event.type);
return this.dispatch({ type: 'app_exit' });
case 'foreground_enter':