Address Cody review round 2: all ten findings (CODY-001 to CODY-010)

CODY-001 Bridge URL must be HTTPS (HTTP only to loopback), in the client and
         in manifest generation.
CODY-002 Even host calls stay serialised until the SDK call really settles;
         a microphone start that completes after its timeout is followed by
         a stop.
CODY-003 Request id is stored before the first submit and cleared only on a
         definitive answer; lost replies are treated as ambiguous and
         reconciled under the same id; HUD no longer claims "not sent".
CODY-004 Revoking a device cancels its in-flight turns, aborts
         transcription and asks Hermes to stop, from HTTP and CLI paths.
CODY-005 Speech model pinned to a commit with per-file SHA-256; fetched and
         verified ahead of time; verified on every load; never downloaded
         at run time; no unpinned configuration.
CODY-006 State directory 0700, database/WAL/SHM 0600 regardless of umask.
CODY-007 credential_expired handled like unauthorized on every path.
CODY-008 Approval wait start and paused agent deadline persisted and
         restored across restarts (schema v2).
CODY-009 Exactly one conversation create per epoch, sent in sequence, with
         stale results dropped before persistence.
CODY-010 Wrong pairing guesses no longer consume the outstanding code;
         failures are indistinguishable.

Checks: typecheck clean; npm test 182 passed; client build ok;
test:real-stt 11 passed (including with no network); 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 19:49:14 -08:00
co-authored by Claude Fable 5.2
parent 3d5691dfa3
commit e1c02d5266
43 changed files with 2104 additions and 227 deletions
+54 -6
View File
@@ -9,6 +9,7 @@ import {
} from '@hermeven/contracts';
import type { BridgeConfig } from './config.ts';
import type { Db } from './db.ts';
import type { Devices } from './devices.ts';
import {
buildRunBody,
HermesSubmitError,
@@ -39,6 +40,8 @@ export interface TurnRow {
updated_at: number;
agent_started_at: number | null;
approval_started_at: number | null;
/** Absolute time the agent must finish by; pushed back by time spent awaiting approval. */
agent_deadline_at: number | null;
completed_at: number | null;
transcribe_ms: number | null;
agent_ms: number | null;
@@ -118,6 +121,7 @@ export class TurnEngine {
private readonly deps: {
db: Db;
config: BridgeConfig;
devices: Devices;
hermes: HermesAdapter;
stt: SttAdapter;
log: EngineLog;
@@ -220,6 +224,7 @@ export class TurnEngine {
updated_at: now,
agent_started_at: null,
approval_started_at: null,
agent_deadline_at: null,
completed_at: null,
transcribe_ms: null,
agent_ms: null,
@@ -261,6 +266,38 @@ export class TurnEngine {
return this.row(requestId);
}
/**
* Revoke a device and stop everything it has in flight. Access ends and the turns are
* flagged in one transaction (Devices.revoke); this then acts on the flags at once.
*/
revokeDevice(deviceId: string): boolean {
const revoked = this.deps.devices.revoke(deviceId);
this.deps.log.info({ device_id: deviceId, revoked }, 'device.revoke_applied');
this.applyCancelFlags();
return revoked;
}
/**
* Act on cancel flags set directly in the database: abort transcription and wake the worker
* so it asks Hermes to stop. Runs right after an in-process revocation, and on a short
* timer so a revocation made by the operator CLI (another process) is honoured promptly.
*/
applyCancelFlags(): number {
if (this.active.size === 0) return 0;
const flagged = this.deps.db
.prepare("SELECT request_id FROM turns WHERE state != 'done' AND cancel_requested = 1")
.all() as Array<{ request_id: string }>;
let nudged = 0;
for (const { request_id } of flagged) {
const active = this.active.get(request_id);
if (!active) continue;
active.sttAbort?.abort();
active.wake?.();
nudged += 1;
}
return nudged;
}
/** Resume or settle every turn a previous process left in flight. Call once at startup. */
recover(): void {
const rows = this.deps.db.prepare("SELECT * FROM turns WHERE state != 'done'").all() as TurnRow[];
@@ -427,13 +464,15 @@ export class TurnEngine {
.get(row.conversation_id) as ConversationRow;
// Persist the exact body BEFORE the first network attempt, so a crash can replay it
// under the same idempotency key instead of guessing.
const agentStartedAt = this.now();
this.setState(requestId, 'thinking', {
pending_body: buildRunBody({
input: row.input_text ?? '',
sessionId: conversation.hermes_session_id,
instructions: this.deps.config.hermes.instructions,
}),
agent_started_at: this.now(),
agent_started_at: agentStartedAt,
agent_deadline_at: agentStartedAt + this.timing.agentDeadlineMs,
});
}
@@ -482,7 +521,7 @@ export class TurnEngine {
/** Returns the run id, or null when the turn was settled here. */
private async submitToHermes(requestId: string, active: ActiveTurn, maybeAlreadySent: boolean): Promise<string | null> {
const row = this.row(requestId)!;
const deadline = (row.agent_started_at ?? this.now()) + this.timing.agentDeadlineMs;
const deadline = row.agent_deadline_at ?? (row.agent_started_at ?? this.now()) + this.timing.agentDeadlineMs;
let ambiguous = maybeAlreadySent;
let notSent = 0;
@@ -529,8 +568,11 @@ export class TurnEngine {
private async poll(requestId: string, active: ActiveTurn): Promise<void> {
const start = this.row(requestId)!;
const runId = start.hermes_run_id!;
let deadlineAt = (start.agent_started_at ?? this.now()) + this.timing.agentDeadlineMs;
let approvalSince: number | null = null;
// Both clocks come from the database, so a bridge restart resumes them where they were:
// the approval wait is measured from when the approval was first seen, and the agent
// deadline keeps whatever pause earlier approvals earned it.
let deadlineAt = start.agent_deadline_at ?? (start.agent_started_at ?? this.now()) + this.timing.agentDeadlineMs;
let approvalSince: number | null = start.approval_started_at;
let stopReason: 'user' | 'agent_timeout' | 'approval_timeout' | null = null;
let stopAccepted = false;
let graceUntil = 0;
@@ -587,14 +629,20 @@ export class TurnEngine {
this.finish(requestId, 'unknown', { code: 'outcome_unknown' });
return;
case 'waiting_for_approval':
if (approvalSince === null) approvalSince = now;
if (!stopReason) this.setState(requestId, 'approval_required', { approval_started_at: now });
if (approvalSince === null) {
// Recorded once, when the approval is first seen, and never moved afterwards.
approvalSince = now;
this.update(requestId, { approval_started_at: now });
}
if (!stopReason) this.setState(requestId, 'approval_required');
break;
default:
if (approvalSince !== null) {
// Approval was resolved on another surface; time spent waiting does not count.
// Persisted in one write, so a restart cannot apply the pause twice or not at all.
deadlineAt += now - approvalSince;
approvalSince = null;
this.update(requestId, { agent_deadline_at: deadlineAt, approval_started_at: null });
}
if (!stopReason) this.setState(requestId, 'thinking');
}