/** * Error-code registry — PROTOCOL §8. * * Extensible registry: the closed set of codes that appear in command errors (`response` * with `success:false`, §9.3) and async `error` events (§9.4). Codes are string literals so * they serialize plainly over the wire — never `Error` objects (§4 design rule #1). */ import { Type, type Static } from '@sinclair/typebox'; /** * The v0.1 error-code registry. Keys are the stable, wire-facing identifiers; each maps to * itself so `ErrorCode` (a `keyof`) and the runtime string agree. Add new codes here only — * the type derives automatically. */ export const ERROR_CODES = { /** Malformed message, bad JSON, failed validation (§8). */ INVALID_REQUEST: 'INVALID_REQUEST', /** Referenced agent_id, request_id, or session entry doesn't exist (§8). */ NOT_FOUND: 'NOT_FOUND', /** Per-agent command sent on a daemon connection without a valid agent_id attachment (§8). */ NOT_ATTACHED: 'NOT_ATTACHED', /** The server refused the operation (rare in v0.1 — the gate runs in-agent) (§8). */ PERMISSION_DENIED: 'PERMISSION_DENIED', /** Version mismatch, message out of sequence (§8). */ PROTOCOL_ERROR: 'PROTOCOL_ERROR', /** An ask hit the headless timeout (ARCHITECTURE §6.2) (§8). */ TIMEOUT: 'TIMEOUT', /** Unexpected server-side failure (§8). */ INTERNAL_ERROR: 'INTERNAL_ERROR', /** Runtime startup failed; the durable agent can be retried (§8). */ RUNTIME_START_FAILED: 'RUNTIME_START_FAILED', /** Existing session state is present but cannot be restored safely. */ SESSION_CORRUPT: 'SESSION_CORRUPT', /** A replay entry exceeds the daemon's safe wire-size cap. */ REPLAY_ENTRY_TOO_LARGE: 'REPLAY_ENTRY_TOO_LARGE', /** The LLM provider call failed (mid-turn) (§8). */ PROVIDER_ERROR: 'PROVIDER_ERROR', /** A tool returned an error (also surfaces as tool_result with is_error) (§8). */ TOOL_ERROR: 'TOOL_ERROR', /** The operation conflicts with current runtime/session state (e.g. compact while a turn is queued/active) (§8). */ CONFLICT: 'CONFLICT', } as const; /** The union of all error-code string literals (single source of truth). */ export type ErrorCode = keyof typeof ERROR_CODES; /** Runtime-validation schema: the literal union of every registered code. */ export const ErrorCodeSchema = Type.Union( (Object.keys(ERROR_CODES) as ErrorCode[]).map((c) => Type.Literal(c)), ); /** Compile-time check that ErrorCodeSchema's Static matches ErrorCode. */ export type _ErrorCodeStatic = Static;