CODY-019: a failed exit no longer overwrites a countdown restarted by activity

An exit attempt remembered only the reducer generation. A gesture that
changes no state (a swipe at Ready) restarted the 30 s countdown, but when
the pending host call then failed, the 3 s retry replaced that timer.

The controller now counts every restart of the normal countdown (gesture,
state change, phone activity) in an activity epoch, captures it when an
attempt starts, and schedules no retry if it has moved. Bounded attempts,
late-success memory, exit-event finality and one host call at a time are
unchanged.

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:52:51 -08:00
co-authored by Claude Fable 5.2
parent 3d0edebd13
commit d8582e99a2
2 changed files with 133 additions and 6 deletions
+17 -5
View File
@@ -158,6 +158,13 @@ export class Controller {
private leaving = false; private leaving = false;
/** Unconfirmed exit attempts since the last activity. */ /** Unconfirmed exit attempts since the last activity. */
private exitFailures = 0; private exitFailures = 0;
/**
* Counts every restart of the normal countdown: a gesture, a state change, a touch on the
* phone page. Reducer generation is not enough, because a gesture can change no state (a
* swipe at Ready). An exit attempt that started before the latest activity must not schedule
* its short retry over the countdown that activity restarted.
*/
private activityEpoch = 0;
/** Set while a long press that STARTED a recording is still held down. */ /** Set while a long press that STARTED a recording is still held down. */
private heldPress: { generation: number; at: number } | null = null; private heldPress: { generation: number; at: number } | null = null;
@@ -397,7 +404,10 @@ export class Controller {
if (this.idleTimer) clearTimeout(this.idleTimer); if (this.idleTimer) clearTimeout(this.idleTimer);
this.idleTimer = null; this.idleTimer = null;
// Anything other than our own retry is activity: the full wait and the attempt count restart. // Anything other than our own retry is activity: the full wait and the attempt count restart.
if (!retry) this.exitFailures = 0; if (!retry) {
this.exitFailures = 0;
this.activityEpoch += 1;
}
if (this.stopped || this.exited || !this.idleExitMs || !this.idleArmed || !this.hudReady) return; if (this.stopped || this.exited || !this.idleExitMs || !this.idleArmed || !this.hudReady) return;
const phase = this.state.phase; const phase = this.state.phase;
const wait = const wait =
@@ -425,7 +435,7 @@ export class Controller {
private async leaveGlasses(reason: string): Promise<void> { private async leaveGlasses(reason: string): Promise<void> {
if (this.exited || this.leaving) return; if (this.exited || this.leaving) return;
this.leaving = true; this.leaving = true;
const generation = this.state.generation; const epoch = this.activityEpoch;
this.note('lifecycle', `leaving the glasses: ${reason}`); this.note('lifecycle', `leaving the glasses: ${reason}`);
this.emit(); this.emit();
let confirmed = false; let confirmed = false;
@@ -447,9 +457,11 @@ export class Controller {
return; return;
} }
// Not confirmed: refused, failed, or no answer in time. We are still on the glasses as far // 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 // as we know, so this must not be final. If anything happened meanwhile (a gesture, even one
// already restarted the full countdown; otherwise try again, a bounded number of times. // that changed no state, or a touch on the phone page), that activity already restarted the
if (this.state.generation !== generation) return; // full countdown or the phone grace period, and that timer stands: no short retry replaces
// it. Otherwise try again, a bounded number of times.
if (this.activityEpoch !== epoch) return;
this.exitFailures += 1; this.exitFailures += 1;
if (this.exitFailures >= this.exitAttempts) { if (this.exitFailures >= this.exitAttempts) {
this.note('lifecycle', `the host did not confirm leaving after ${this.exitFailures} attempts; staying until the next activity`); this.note('lifecycle', `the host did not confirm leaving after ${this.exitFailures} attempts; staying until the next activity`);
+116 -1
View File
@@ -20,6 +20,7 @@ class FakeEvenHost {
/** What the next exit calls answer; 'hold' settles only on `releaseExit`. Then `true`. */ /** What the next exit calls answer; 'hold' settles only on `releaseExit`. Then `true`. */
exitScript: Array<boolean | 'hold' | 'throw'> = []; exitScript: Array<boolean | 'hold' | 'throw'> = [];
private heldExit: ((ok: boolean) => void) | null = null; private heldExit: ((ok: boolean) => void) | null = null;
private heldExitReject: ((err: Error) => void) | null = null;
exitCalls: number[] = []; exitCalls: number[] = [];
hudCreates = 0; hudCreates = 0;
inFlight = 0; inFlight = 0;
@@ -50,7 +51,11 @@ class FakeEvenHost {
this.exitCalls.push(mode); this.exitCalls.push(mode);
const next = this.exitScript.shift() ?? true; const next = this.exitScript.shift() ?? true;
if (next === 'throw') return Promise.reject(new Error('host error')); if (next === 'throw') return Promise.reject(new Error('host error'));
if (next === 'hold') return new Promise<boolean>((resolve) => (this.heldExit = resolve)); if (next === 'hold')
return new Promise<boolean>((resolve, reject) => {
this.heldExit = resolve;
this.heldExitReject = reject;
});
return Promise.resolve(next); return Promise.resolve(next);
}), }),
getLocalStorage: (key: string) => getLocalStorage: (key: string) =>
@@ -97,6 +102,13 @@ class FakeEvenHost {
systemExit(): void { systemExit(): void {
for (const cb of this.hub) cb({ sysEvent: { eventType: 7 } }); for (const cb of this.hub) cb({ sysEvent: { eventType: 7 } });
} }
/** The held exit call fails with an error instead of answering. */
rejectExit(): void {
const reject = this.heldExitReject;
this.heldExit = null;
this.heldExitReject = null;
reject?.(new Error('host error'));
}
swipeDown(): void { swipeDown(): void {
for (const cb of this.hub) cb({ sysEvent: { eventType: 2 } }); for (const cb of this.hub) cb({ sysEvent: { eventType: 2 } });
} }
@@ -373,3 +385,106 @@ describe('default idle times with the production adapter (fake clock)', () => {
expect(host.maxInFlight).toBe(1); expect(host.maxInFlight).toBe(1);
}); });
}); });
/**
* CODY-019. Activity that arrives while an exit call is still with the host restarts the full
* countdown (or the phone grace period). When that call then fails, its short retry must not
* replace the restarted timer. Default times, fake clock, production adapter.
*/
describe('activity during a pending exit keeps its full countdown (CODY-019)', () => {
const fakeClock = () => vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval', 'Date'] });
/** Launched from the glasses, idle for 30 s: the first exit call is now held by the host. */
async function firstExitHeld(): Promise<void> {
await pairedHost();
fakeClock();
controller = makeController({ idleExit: {}, exitRetryMs: undefined as never, exitAttempts: undefined as never });
host.pushLaunchSource('glassesMenu');
host.exitScript = ['hold'];
await controller.start();
await vi.advanceTimersByTimeAsync(30_050);
expect(host.exitCalls).toEqual([0]);
expect(host.exitHeld()).toBe(true);
}
/** From the activity: nothing for the full 30 s, then one more call, which succeeds. */
async function fullCountdownThenLeaves(expectedCalls: number): Promise<void> {
const before = host.exitCalls.length;
await vi.advanceTimersByTimeAsync(29_800); // 3 s retry would have fired long ago
expect(host.exitCalls.length).toBe(before);
expect(logged('left the glasses')).toBe(0);
await vi.advanceTimersByTimeAsync(10 * 60_000);
expect(host.exitCalls.length).toBe(expectedCalls);
expect(logged('left the glasses')).toBe(1);
expect(host.maxInFlight).toBe(1);
}
it('a swipe at Ready (no state change), then the host answers false', async () => {
await firstExitHeld();
const generation = snapshot.state.generation;
host.swipeDown();
expect(snapshot.state.generation).toBe(generation); // the gesture changed no state
host.releaseExit(false);
await fullCountdownThenLeaves(2);
});
it('a swipe at Ready, then the host call fails with an error', async () => {
await firstExitHeld();
host.swipeDown();
host.rejectExit();
await fullCountdownThenLeaves(2);
});
it('a swipe at Ready, then the host never answers in time; its late success is still final', async () => {
await firstExitHeld();
host.swipeDown();
await vi.advanceTimersByTimeAsync(ADAPTER_TIMEOUT_MS + 50); // the wait for an answer runs out
expect(logged('will try again')).toBe(0); // no short retry was scheduled over the swipe's countdown
await vi.advanceTimersByTimeAsync(29_700);
expect(host.exitCalls).toEqual([0]);
// The full countdown from the swipe ends; the retry waits on the SAME held call.
await vi.advanceTimersByTimeAsync(400);
expect(host.exitCalls).toEqual([0]);
expect(host.inFlight).toBe(1);
host.releaseExit(true);
await vi.advanceTimersByTimeAsync(10 * 60_000);
expect(host.exitCalls).toEqual([0]); // one host call ever
expect(logged('left the glasses')).toBe(1);
expect(host.maxInFlight).toBe(1);
});
it('a touch on the phone page, then the host answers false: the whole grace period stands', async () => {
await firstExitHeld();
controller.notePhoneActivity();
host.releaseExit(false);
// Grace is 120 s, then the 30 s Ready wait: nothing before 150 s from the touch.
await vi.advanceTimersByTimeAsync(149_500);
expect(host.exitCalls).toEqual([0]);
expect(logged('left the glasses')).toBe(0);
await vi.advanceTimersByTimeAsync(1_000);
expect(host.exitCalls).toEqual([0, 0]);
await vi.advanceTimersByTimeAsync(10 * 60_000);
expect(host.exitCalls).toEqual([0, 0]);
expect(logged('left the glasses')).toBe(1);
expect(host.maxInFlight).toBe(1);
});
it('after such activity the attempts are still bounded: a host that keeps refusing gets four, then none', async () => {
await firstExitHeld();
host.swipeDown();
host.exitScript = [false, false, false, false, false, false];
host.releaseExit(false);
await vi.advanceTimersByTimeAsync(10 * 60_000);
expect(host.exitCalls).toEqual([0, 0, 0, 0, 0]); // the one overtaken by the swipe, then four
expect(logged('staying until the next activity')).toBe(1);
expect(host.maxInFlight).toBe(1);
});
it('control: with no activity, the same refusal IS retried after 3 s', async () => {
await firstExitHeld();
host.releaseExit(false);
await vi.advanceTimersByTimeAsync(3_100);
expect(host.exitCalls).toEqual([0, 0]);
expect(logged('left the glasses')).toBe(1);
});
});