Address Cody review round 3: CODY-003 and CODY-006

CODY-003 Request identity is durable before submission. Storage results are
         explicit at the DevicePort boundary (storageSet resolves true only
         on the host's own timely confirmation; storageGet distinguishes
         unknown from empty). A request is not sent until the phone has
         confirmed storing its id; a timeout or refusal holds it back,
         unsent and retryable under the same id. Unreadable storage at
         start is not treated as unpaired; an unsaveable pairing is revoked.
CODY-006 The state directory is set to 0700 on every open, including when
         it already existed, and the bridge refuses to start with an
         operator-facing error if it cannot be. Deployment guide creates
         it with install -d -m 0700.

Checks: typecheck clean; npm test 194 passed; client build ok;
test:real-stt 11 passed; test:real-hermes 9 passed; test:simulator 6
passed; git diff --check clean.

Not reviewed. No physical-device, droplet or production evidence.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Dan
2026-11-18 20:29:05 -08:00
co-authored by Claude Fable 5.2
parent e1c02d5266
commit 1507cea077
19 changed files with 759 additions and 45 deletions
+61 -11
View File
@@ -87,6 +87,10 @@ export class Controller {
private pending: PendingInput | null = null;
private lastTurn: TurnResult | null = null;
private hudReady = false;
/** The request id last CONFIRMED stored by the host (`undefined` = not known). */
private confirmedActive: string | null | undefined = undefined;
/** App storage could not be read at start: nothing is assumed about what it holds. */
private storageFault = false;
private creating: { epoch: number; promise: Promise<string> } | null = null;
private createChain: Promise<void> = Promise.resolve();
private stopped = false;
@@ -115,7 +119,11 @@ export class Controller {
// ------------------------------------------------------------------ public
async start(): Promise<void> {
this.stored = await this.load();
const loaded = await this.load();
this.storageFault = loaded === 'unreadable';
this.stored = loaded === 'unreadable' ? null : loaded;
if (this.stored) this.confirmedActive = this.stored.activeCancelled ? undefined : this.stored.activeRequestId;
if (this.storageFault) this.note('storage', 'app storage could not be read; saved pairing state is unknown');
this.hudReady = await this.device.init(renderHud(this.state, this.now()));
this.note('hud', this.hudReady ? 'HUD page created' : 'HUD page was refused by the host');
this.unsubscribers.push(
@@ -150,6 +158,7 @@ export class Controller {
conversationId: this.stored?.conversationId ?? null,
captureMaxSeconds,
resumeRequestId: abandoned ? null : (this.stored?.activeRequestId ?? null),
storageFault: this.storageFault,
});
if (abandoned) void this.cancelTurn(abandoned);
}
@@ -179,6 +188,10 @@ export class Controller {
/** Explicit pairing from the phone. Throws with a user-presentable message on failure. */
async pair(bridgeUrl: string, code: string, deviceName: string): Promise<void> {
if (this.storageFault) {
// Pairing now would overwrite storage we could not read, which may hold a running turn.
throw new Error('Saved settings could not be read. Close Hermeven and open it again before pairing.');
}
const client = this.makeClient(bridgeUrl, '');
const credential = await client.claim(code, deviceName);
client.token = credential.token;
@@ -193,7 +206,15 @@ export class Controller {
activeRequestId: null,
activeCancelled: false,
};
await this.save();
if (!(await this.save())) {
// A pairing that exists only in memory would vanish on the next start and leave a live
// credential behind on the bridge. Undo it instead.
this.stored = null;
this.client = null;
await client.revoke().catch(() => undefined);
throw new Error('The phone could not save the pairing, so it was not kept. Get a new pairing code and try again.');
}
this.confirmedActive = null;
this.note('pairing', `paired as ${credential.device_id}`);
let captureMaxSeconds: number = DEFAULT_LIMITS.captureMaxSeconds;
try {
@@ -433,7 +454,15 @@ export class Controller {
// Remember the request id before anything is sent. From here until the bridge gives a
// definite answer, this request may or may not exist there; if the page dies in between,
// the next start re-attaches to it by id instead of forgetting a turn that is running.
await this.setActiveRequest(requestId);
//
// Nothing is sent until the phone has CONFIRMED storing the id. An unanswered or refused
// write stops here, with the request provably unsent and kept for a retry under the same id.
if (!(await this.setActiveRequest(requestId))) {
this.note('turn', `request ${requestId} held back: its id could not be stored`);
if (!this.stale(generation)) this.dispatch({ type: 'not_saved', generation });
return;
}
if (this.stale(generation)) return;
for (let attempt = 1; ; attempt++) {
if (this.stale(generation)) return;
try {
@@ -688,10 +717,19 @@ export class Controller {
}
}
private async setActiveRequest(requestId: string | null): Promise<void> {
if (!this.stored || (this.stored.activeRequestId === requestId && !this.stored.activeCancelled)) return;
this.stored = { ...this.stored, activeRequestId: requestId, activeCancelled: false };
await this.save();
/**
* Record which request may exist on the bridge. Resolves true only when the phone has
* positively confirmed storing it; a write that timed out or failed is retried on the next
* call rather than assumed.
*/
private async setActiveRequest(requestId: string | null): Promise<boolean> {
if (!this.stored) return false;
const unchanged = this.stored.activeRequestId === requestId && !this.stored.activeCancelled;
if (unchanged && this.confirmedActive === requestId) return true;
if (!unchanged) this.stored = { ...this.stored, activeRequestId: requestId, activeCancelled: false };
const ok = await this.save();
this.confirmedActive = ok ? requestId : undefined;
return ok;
}
/** Clear the stored request id only if it is still this one (a newer turn may own the slot). */
@@ -699,9 +737,18 @@ export class Controller {
if (this.stored?.activeRequestId === requestId) await this.setActiveRequest(null);
}
private async load(): Promise<Stored | null> {
/** `'unreadable'` when the host would not say what is stored; never guessed as "nothing". */
private async load(): Promise<Stored | null | 'unreadable'> {
let raw = '';
for (let attempt = 1; ; attempt++) {
const read = await this.device.storageGet(STORAGE_KEY);
if (read.ok) {
raw = read.value;
break;
}
if (attempt >= 3) return 'unreadable';
}
try {
const raw = await this.device.storageGet(STORAGE_KEY);
if (!raw) return null;
const parsed = JSON.parse(raw) as Partial<Stored>;
if (typeof parsed.bridgeUrl !== 'string' || typeof parsed.token !== 'string' || !parsed.token) return null;
@@ -720,8 +767,11 @@ export class Controller {
}
}
private async save(): Promise<void> {
await this.device.storageSet(STORAGE_KEY, this.stored ? JSON.stringify(this.stored) : '');
/** True only when the host confirmed the write. See DevicePort.storageSet. */
private async save(): Promise<boolean> {
const ok = await this.device.storageSet(STORAGE_KEY, this.stored ? JSON.stringify(this.stored) : '');
if (!ok) this.note('storage', 'a write to app storage was not confirmed');
return ok;
}
private async forget(): Promise<void> {