diff --git a/apps/even-client/src/core/controller.ts b/apps/even-client/src/core/controller.ts index de34924..90b66ef 100644 --- a/apps/even-client/src/core/controller.ts +++ b/apps/even-client/src/core/controller.ts @@ -158,6 +158,13 @@ export class Controller { private leaving = false; /** Unconfirmed exit attempts since the last activity. */ 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. */ private heldPress: { generation: number; at: number } | null = null; @@ -397,7 +404,10 @@ export class Controller { 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 (!retry) { + this.exitFailures = 0; + this.activityEpoch += 1; + } if (this.stopped || this.exited || !this.idleExitMs || !this.idleArmed || !this.hudReady) return; const phase = this.state.phase; const wait = @@ -425,7 +435,7 @@ export class Controller { private async leaveGlasses(reason: string): Promise { if (this.exited || this.leaving) return; this.leaving = true; - const generation = this.state.generation; + const epoch = this.activityEpoch; this.note('lifecycle', `leaving the glasses: ${reason}`); this.emit(); let confirmed = false; @@ -447,9 +457,11 @@ export class Controller { 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; + // as we know, so this must not be final. If anything happened meanwhile (a gesture, even one + // that changed no state, or a touch on the phone page), that activity already restarted the + // 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; if (this.exitFailures >= this.exitAttempts) { this.note('lifecycle', `the host did not confirm leaving after ${this.exitFailures} attempts; staying until the next activity`); diff --git a/tests/integration/idleExitHost.test.ts b/tests/integration/idleExitHost.test.ts index 1f3f6b8..7404038 100644 --- a/tests/integration/idleExitHost.test.ts +++ b/tests/integration/idleExitHost.test.ts @@ -20,6 +20,7 @@ class FakeEvenHost { /** What the next exit calls answer; 'hold' settles only on `releaseExit`. Then `true`. */ exitScript: Array = []; private heldExit: ((ok: boolean) => void) | null = null; + private heldExitReject: ((err: Error) => void) | null = null; exitCalls: number[] = []; hudCreates = 0; inFlight = 0; @@ -50,7 +51,11 @@ class FakeEvenHost { this.exitCalls.push(mode); const next = this.exitScript.shift() ?? true; if (next === 'throw') return Promise.reject(new Error('host error')); - if (next === 'hold') return new Promise((resolve) => (this.heldExit = resolve)); + if (next === 'hold') + return new Promise((resolve, reject) => { + this.heldExit = resolve; + this.heldExitReject = reject; + }); return Promise.resolve(next); }), getLocalStorage: (key: string) => @@ -97,6 +102,13 @@ class FakeEvenHost { systemExit(): void { 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 { 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); }); }); + +/** + * 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 { + 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 { + 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); + }); +});