import type { SettingDef } from "./settings.ts"; /** Numeric operational metadata only: existing schema, settings and reload retain all authority. */ export interface OperationalNumberSetting extends Pick { type: "number" | "integer"; store: "config"; risk: "raises"; min: number; max: number; defaultValue: number; unit: string; /** The independently classified H0 item, not a safety floor or wire/storage contract. */ inventory: string; } export const GROOM_FYI_WINDOW_SETTING = { key: "tasks.groom_fyi_window_min", path: ["tasks", "groom_fyi_window_min"], group: "Routing", label: "Planner state-update FYI interval (minutes)", type: "number", store: "config", risk: "raises", applies: "live", min: 1, max: 1440, defaultValue: 10, unit: "minutes", inventory: "src/hub/tasks.ts:GROOM_FYI_WINDOW_MS", } as const satisfies OperationalNumberSetting; const taskNumber = >(def: T) => ({ group: "Routing", store: "config", risk: "raises", applies: "live", ...def }) as const satisfies OperationalNumberSetting; /** #489 H2, tasks and routing. Each default equals the constant it replaces (pinned in test/operational-settings.test.ts); none is a safety floor or a contract. */ export const DEMOTE_AT_SETTING = taskNumber({ key: "tasks.demote_at", path: ["tasks", "demote_at"], label: "Peer demotion threshold (decayed recent failures)", type: "number", min: 0.5, max: 20, defaultValue: 1.5, unit: "failures", inventory: "src/hub/tasks.ts:DEMOTE_AT", }); export const DEMOTION_HALF_LIFE_SETTING = taskNumber({ key: "tasks.demotion_half_life_h", path: ["tasks", "demotion_half_life_h"], label: "Demotion half-life: a failure counts half after (hours)", type: "number", min: 1, max: 168, defaultValue: 24, unit: "hours", inventory: "src/hub/tasks.ts:DEMOTION_HALF_LIFE_MS", }); export const CONTRADICTION_WINDOW_SETTING = taskNumber({ key: "tasks.contradiction_window_days", path: ["tasks", "contradiction_window_days"], label: "Approval contradiction window (days)", type: "number", min: 1, max: 90, defaultValue: 7, unit: "days", inventory: "src/hub/tasks.ts:CONTRADICTION_WINDOW_MS", }); export const RESCREEN_MAX_SETTING = taskNumber({ key: "tasks.rescreen_max", path: ["tasks", "rescreen_max"], label: "PII re-screen attempts per task in a hub run", type: "integer", min: 1, max: 100, defaultValue: 10, unit: "attempts", inventory: "src/hub/tasks.ts:RESCREEN_MAX", }); export const SPLIT_MIN_SETTING = taskNumber({ key: "tasks.split_min_observations", path: ["tasks", "split_min_observations"], label: "Shadow split prediction: measured tasks a peer needs", type: "integer", min: 3, max: 100, defaultValue: 5, unit: "tasks", inventory: "src/hub/routing.ts:SPLIT_MIN", }); export const SPLIT_FAILURE_SHARE_SETTING = taskNumber({ key: "tasks.split_max_failure_share", path: ["tasks", "split_max_failure_share"], label: "Shadow split prediction: failed share a history may hold", type: "number", min: 0, max: 1, defaultValue: 0.3, unit: "share", inventory: "src/hub/routing.ts:SPLIT_FAILURE_SHARE", }); const busNumber = >(def: T) => ({ group: "Routing", store: "config", risk: "raises", applies: "live", type: "integer", min: 1, ...def }) as const satisfies OperationalNumberSetting; export const BUS_MAX_ATTEMPTS_SETTING = busNumber({ key: "bus.max_attempts", path: ["bus", "max_attempts"], label: "Safe-failure delivery attempts", max: 100, defaultValue: 3, unit: "attempts", inventory: "src/hub/bus.ts:MAX_ATTEMPTS", }); export const BUS_DIGEST_MAX_SETTING = busNumber({ key: "bus.digest_max", path: ["bus", "digest_max"], label: "Maximum originals in a push digest", max: 10, defaultValue: 10, unit: "envelopes", inventory: "src/hub/bus.ts:DIGEST_MAX", }); export const BUS_PULL_BUDGET_BYTES_SETTING = busNumber({ key: "bus.pull_budget_bytes", path: ["bus", "pull_budget_bytes"], label: "Pull rendered-text budget (bytes)", max: 65_536, defaultValue: 65_536, unit: "bytes", inventory: "src/hub/bus.ts:PULL_BUDGET_BYTES", }); export const BUS_NOTES_KEPT_SETTING = busNumber({ key: "bus.notes_kept", path: ["bus", "notes_kept"], label: "Shared notes retained in a merged preface", max: 10, defaultValue: 10, unit: "notes", inventory: "src/hub/bus.ts:NOTES_KEPT", }); export const BUS_TERMINAL_HISTORY_LIMIT_SETTING = busNumber({ key: "bus.terminal_history_limit", path: ["bus", "terminal_history_limit"], label: "Unprotected terminal delivery history target", max: 2048, defaultValue: 2048, unit: "rows", inventory: "src/hub/delivery-journal.ts:TERMINAL_CAP", }); /** #1172: bounded wait for a push channel's explicit native delivery confirmation. */ export const BUS_ACCEPTED_SETTLE_MS_SETTING = { ...busNumber({ key: "bus.accepted_settle_ms", path: ["bus", "accepted_settle_ms"], label: "Accepted channel delivery confirmation timeout", max: 86_400_000, defaultValue: 600_000, unit: "milliseconds", inventory: "src/hub/operational-settings.ts:BUS_ACCEPTED_SETTLE_MS_SETTING.defaultValue", }), min: 1000, } as const satisfies OperationalNumberSetting; /** * #489 H4, recovery and upgrade waits. These are read by the upgrade coordinator (a CLI process) from the project's own config at the START of each wait, so a change applies at the next wait and a wait * already running keeps its deadline. Every row's minimum is today's constant: a person-visible wait can be raised, never cut below what ships (a request that times out may already have applied, and a * shorter reconnect wait fails a recovery earlier than the person was told). The one row that may shorten, `recovery.terminal_idle_wait_s`, only ends a wait in manual-required, which retains the terminal. */ const recoveryNumber = >(def: T) => ({ group: "Routing", store: "config", risk: "raises", applies: "next use", type: "integer", ...def }) as const satisfies OperationalNumberSetting; export const RECOVERY_RECONNECT_WAIT_SETTING = recoveryNumber({ key: "recovery.reconnect_wait_s", path: ["recovery", "reconnect_wait_s"], label: "Upgrade recovery: wait for a managed terminal to attach (seconds)", min: 600, max: 3600, defaultValue: 600, unit: "seconds", inventory: "src/cli/upgrade-runtime.ts:RECONNECT_WAIT_MS", }); export const RECOVERY_UNMANAGED_RECONNECT_WAIT_SETTING = recoveryNumber({ key: "recovery.unmanaged_reconnect_wait_s", path: ["recovery", "unmanaged_reconnect_wait_s"], label: "Upgrade recovery: wait for an unmanaged Claude to reconnect (seconds)", min: 90, max: 900, defaultValue: 90, unit: "seconds", inventory: "src/cli/upgrade-runtime.ts:UNMANAGED_RECONNECT_WAIT_MS", }); export const RECOVERY_CONTROL_TIMEOUT_SETTING = recoveryNumber({ key: "recovery.control_timeout_s", path: ["recovery", "control_timeout_s"], label: "Upgrade recovery: wait for one control request to a hub (seconds)", min: 30, max: 300, defaultValue: 30, unit: "seconds", inventory: "src/cli/upgrade-runtime.ts:RECOVERY_CONTROL_TIMEOUT_MS", }); export const RECOVERY_SHUTDOWN_WAIT_SETTING = recoveryNumber({ key: "recovery.shutdown_wait_s", path: ["recovery", "shutdown_wait_s"], label: "Upgrade recovery: wait for the old hub to stop after commit (seconds)", min: 30, max: 300, defaultValue: 30, unit: "seconds", inventory: "src/cli/upgrade.ts:deadline", }); export const RECOVERY_TERMINAL_IDLE_WAIT_SETTING = recoveryNumber({ key: "recovery.terminal_idle_wait_s", path: ["recovery", "terminal_idle_wait_s"], label: "Upgrade recovery: wait for a terminal to go idle before closing it (seconds)", min: 30, max: 600, defaultValue: 120, unit: "seconds", inventory: "src/cli/upgrade-runtime.ts:waitForIdle", }); const adapterNumber = >(def: T) => ({ group: "Start", store: "config", risk: "raises", applies: "next use", type: "integer", ...def }) as const satisfies OperationalNumberSetting; export const ACP_TOOL_CEILING_SETTING = adapterNumber({ key: "adapter_timing.acp_tool_ceiling_min", path: ["adapter_timing", "acp_tool_ceiling_min"], label: "Next ACP launch: open-tool ceiling (minutes, at least the watchdog; 60 covers a documented 50-minute gate wait)", min: 1, max: 120, defaultValue: 60, unit: "minutes", inventory: "src/adapters/acp.ts:TOOL_CEILING_MS", }); export const ACP_ACTIVITY_BATCH_SETTING = adapterNumber({ key: "adapter_timing.acp_activity_batch_ms", path: ["adapter_timing", "acp_activity_batch_ms"], label: "Next ACP launch: activity display batch delay (milliseconds)", min: 10, max: 50, defaultValue: 50, unit: "milliseconds", inventory: "src/adapters/acp.ts:setTimeout", }); export const JCODE_STEER_TIMEOUT_SETTING = adapterNumber({ key: "adapter_timing.jcode_steer_timeout_s", path: ["adapter_timing", "jcode_steer_timeout_s"], label: "Next Jcode launch: total native steer deadline (seconds)", min: 5, max: 60, defaultValue: 5, unit: "seconds", inventory: "src/adapters/jcode.ts:STEER_TIMEOUT_MS", }); /** #489 H6: relay policy is captured at construction; inference consumers are daemon-start bound. */ const modelNumber = >(def: T) => ({ group: "Routing", store: "config", risk: "raises", type: "integer", ...def }) as const satisfies OperationalNumberSetting; export const MODEL_COOLDOWN_FAILURES_SETTING = modelNumber({ key: "model_relay.cooldown_failures", path: ["model_relay", "cooldown_failures"], label: "Model relay: consecutive transport failures before cooldown", min: 1, max: 100, defaultValue: 3, applies: "restart", restartReason: "Model relays and their health state persist until daemon shutdown", unit: "failures", inventory: "src/models/relay.ts:COOLDOWN_FAILURES", }); export const MODEL_COOLDOWN_SETTING = modelNumber({ key: "model_relay.cooldown_s", path: ["model_relay", "cooldown_s"], label: "Model relay: initial cooldown and recent-failure window (seconds)", min: 1, max: 300, defaultValue: 30, applies: "restart", restartReason: "Model relays and their health state persist until daemon shutdown", unit: "seconds", inventory: "src/models/relay.ts:COOLDOWN_MS", }); export const MODEL_COOLDOWN_CAP_SETTING = modelNumber({ key: "model_relay.cooldown_cap_s", path: ["model_relay", "cooldown_cap_s"], label: "Model relay: maximum cooldown (seconds)", min: 300, max: 3600, defaultValue: 300, applies: "restart", restartReason: "Model relays and their health state persist until daemon shutdown", unit: "seconds", inventory: "src/models/relay.ts:COOLDOWN_CAP_MS", }); export const MODEL_TERMINAL_HISTORY_SETTING = modelNumber({ key: "model_relay.terminal_history_limit", path: ["model_relay", "terminal_history_limit"], label: "Model relay: retained completed request records", min: 1, max: 1000, defaultValue: 1000, applies: "restart", restartReason: "Model relays and their health state persist until daemon shutdown", unit: "records", inventory: "src/models/relay.ts:JOURNAL_LIMIT", }); const inferenceRestart = "The inference consumer captures its configuration at daemon startup"; export const INFERENCE_BACKOFF_SETTING = modelNumber({ key: "inference.backoff_min", path: ["inference", "backoff_min"], label: "Inference: cooldown after a failed optional call (minutes)", min: 1, max: 60, defaultValue: 5, applies: "restart", restartReason: inferenceRestart, unit: "minutes", inventory: "src/hub/inference.ts:BACKOFF_MS", }); export const INFERENCE_DIGEST_ITEMS_SETTING = modelNumber({ key: "inference.digest_min_items", path: ["inference", "digest_min_items"], label: "Inference: condense status chatter above this item count", min: 1, max: 100_000, defaultValue: 5, applies: "restart", restartReason: inferenceRestart, unit: "items", inventory: "src/hub/inference.ts:digest_min_items", }); export const INFERENCE_DIGEST_CHARS_SETTING = modelNumber({ key: "inference.digest_min_chars", path: ["inference", "digest_min_chars"], label: "Inference: condense status chatter above this character count", min: 1, max: 10_000_000, defaultValue: 8000, applies: "restart", restartReason: inferenceRestart, unit: "characters", inventory: "src/hub/inference.ts:digest_min_chars", }); /** #489 H7: accepted presentation values are captured once by the next console launch. */ const consoleNumber = >(def: T) => ({ group: "Start", store: "config", risk: "raises", applies: "next use", type: "integer", ...def }) as const satisfies OperationalNumberSetting; export const CONSOLE_POLL_SETTING = consoleNumber({ key: "console.poll_ms", path: ["console", "poll_ms"], label: "Next console launch: refresh interval (milliseconds)", min: 2000, max: 60_000, defaultValue: 4000, unit: "milliseconds", inventory: "src/cli/console.ts:options.pollMs ?? 4000", }); export const CONSOLE_NOTICE_SETTING = consoleNumber({ key: "console.notice_ms", path: ["console", "notice_ms"], label: "Next console launch: notice visibility (milliseconds)", min: 10_000, max: 60_000, defaultValue: 10_000, unit: "milliseconds", inventory: "src/cli/console-state.ts:NOTICE_MS", }); export const CONSOLE_HISTORY_SETTING = consoleNumber({ key: "console.history_limit", path: ["console", "history_limit"], label: "Next console launch: retained command history", min: 1, max: 100, defaultValue: 100, unit: "commands", inventory: "src/cli/console-state.ts:[...s.history, input].slice", }); export const CONSOLE_PENDING_STREAM_SETTING = consoleNumber({ key: "console.pending_stream_limit", path: ["console", "pending_stream_limit"], label: "Next console launch: buffered offscreen stream events", min: 1, max: 1000, defaultValue: 1000, unit: "events", inventory: "src/cli/console.ts:pendingStream.length > 1000", }); export const CONSOLE_ATTENTION_SETTING = consoleNumber({ key: "console.attention_limit", path: ["console", "attention_limit"], label: "Next console launch: retained message attention entries", min: 1, max: 1000, defaultValue: 1000, unit: "messages", inventory: "src/cli/console.ts:[...(state.attentionMessages ?? []), { id: msg.e.env.id, peer: msg.e.env.from, important: msg.e.env.priority === important, since: msg.e.env.ts }].slice", }); /** Fixed metadata; never registration from a file, request, adapter or environment variable. */ export const OPERATIONAL_NUMBERS: readonly OperationalNumberSetting[] = [GROOM_FYI_WINDOW_SETTING, DEMOTE_AT_SETTING, DEMOTION_HALF_LIFE_SETTING, CONTRADICTION_WINDOW_SETTING, RESCREEN_MAX_SETTING, SPLIT_MIN_SETTING, SPLIT_FAILURE_SHARE_SETTING, BUS_MAX_ATTEMPTS_SETTING, BUS_DIGEST_MAX_SETTING, BUS_PULL_BUDGET_BYTES_SETTING, BUS_NOTES_KEPT_SETTING, BUS_TERMINAL_HISTORY_LIMIT_SETTING, BUS_ACCEPTED_SETTLE_MS_SETTING, RECOVERY_RECONNECT_WAIT_SETTING, RECOVERY_UNMANAGED_RECONNECT_WAIT_SETTING, RECOVERY_CONTROL_TIMEOUT_SETTING, RECOVERY_SHUTDOWN_WAIT_SETTING, RECOVERY_TERMINAL_IDLE_WAIT_SETTING, ACP_TOOL_CEILING_SETTING, ACP_ACTIVITY_BATCH_SETTING, JCODE_STEER_TIMEOUT_SETTING, MODEL_COOLDOWN_FAILURES_SETTING, MODEL_COOLDOWN_SETTING, MODEL_COOLDOWN_CAP_SETTING, MODEL_TERMINAL_HISTORY_SETTING, INFERENCE_BACKOFF_SETTING, INFERENCE_DIGEST_ITEMS_SETTING, INFERENCE_DIGEST_CHARS_SETTING, CONSOLE_POLL_SETTING, CONSOLE_NOTICE_SETTING, CONSOLE_HISTORY_SETTING, CONSOLE_PENDING_STREAM_SETTING, CONSOLE_ATTENTION_SETTING]; /** The `tasks` leaves the six #489 H2 readers need; `HubConfig.tasks` has them all. */ export interface TaskOperationalConfig { tasks: { demote_at?: number; demotion_half_life_h?: number; contradiction_window_days?: number; rescreen_max?: number; split_min_observations?: number; split_max_failure_share?: number } } /** * The one place the daemon turns the six task settings into `TasksDeps` readers. `config` is asked at every use, so a reload applies at the next demotion read, verdict, re-screen or prediction; an unset leaf is the row's default, * which is the constant it replaced. */ export const taskOperationalReaders = (config: () => TaskOperationalConfig) => ({ demoteAt: () => config().tasks.demote_at ?? DEMOTE_AT_SETTING.defaultValue, demotionHalfLifeH: () => config().tasks.demotion_half_life_h ?? DEMOTION_HALF_LIFE_SETTING.defaultValue, contradictionWindowDays: () => config().tasks.contradiction_window_days ?? CONTRADICTION_WINDOW_SETTING.defaultValue, rescreenMax: () => config().tasks.rescreen_max ?? RESCREEN_MAX_SETTING.defaultValue, splitMinObservations: () => config().tasks.split_min_observations ?? SPLIT_MIN_SETTING.defaultValue, splitMaxFailureShare: () => config().tasks.split_max_failure_share ?? SPLIT_FAILURE_SHARE_SETTING.defaultValue, }); export interface BusOperationalConfig { bus?: { max_attempts?: number; digest_max?: number; pull_budget_bytes?: number; notes_kept?: number; terminal_history_limit?: number; accepted_settle_ms?: number } } /** Read accepted configuration at the next operation; a reload itself never trims or retries work. */ export const busOperationalReaders = (config: () => BusOperationalConfig) => ({ maxAttempts: () => config().bus?.max_attempts ?? BUS_MAX_ATTEMPTS_SETTING.defaultValue, digestMax: () => config().bus?.digest_max ?? BUS_DIGEST_MAX_SETTING.defaultValue, pullBudgetBytes: () => config().bus?.pull_budget_bytes ?? BUS_PULL_BUDGET_BYTES_SETTING.defaultValue, notesKept: () => config().bus?.notes_kept ?? BUS_NOTES_KEPT_SETTING.defaultValue, terminalHistoryLimit: () => config().bus?.terminal_history_limit ?? BUS_TERMINAL_HISTORY_LIMIT_SETTING.defaultValue, acceptedSettleMs: () => config().bus?.accepted_settle_ms ?? BUS_ACCEPTED_SETTLE_MS_SETTING.defaultValue, }); export interface RecoveryOperationalConfig { recovery?: { reconnect_wait_s?: unknown; unmanaged_reconnect_wait_s?: unknown; control_timeout_s?: unknown; shutdown_wait_s?: unknown; terminal_idle_wait_s?: unknown } } export interface RecoveryWaits { reconnectWaitMs: number; unmanagedReconnectWaitMs: number; controlTimeoutMs: number; shutdownWaitMs: number; terminalIdleWaitMs: number } const recoveryMs = (def: OperationalNumberSetting, raw: unknown): number => { // A value outside the row's bounds, a non-integer or a non-number is the default, never an error inside a recovery: the file was validated when it was written, and a recovery must not fail on a later edit. return typeof raw === "number" && Number.isSafeInteger(raw) && raw >= def.min && raw <= def.max ? raw * 1000 : def.defaultValue * 1000; }; /** The one place the coordinator turns the five recovery settings into milliseconds. `config` is asked at each wait, so the next wait reads the file as it is then. */ export const recoveryWaits = (config: () => RecoveryOperationalConfig | undefined): RecoveryWaits => { const leaf = config()?.recovery; return { reconnectWaitMs: recoveryMs(RECOVERY_RECONNECT_WAIT_SETTING, leaf?.reconnect_wait_s), unmanagedReconnectWaitMs: recoveryMs(RECOVERY_UNMANAGED_RECONNECT_WAIT_SETTING, leaf?.unmanaged_reconnect_wait_s), controlTimeoutMs: recoveryMs(RECOVERY_CONTROL_TIMEOUT_SETTING, leaf?.control_timeout_s), shutdownWaitMs: recoveryMs(RECOVERY_SHUTDOWN_WAIT_SETTING, leaf?.shutdown_wait_s), terminalIdleWaitMs: recoveryMs(RECOVERY_TERMINAL_IDLE_WAIT_SETTING, leaf?.terminal_idle_wait_s), }; }; export interface AdapterTimingConfig { adapter_timing?: { acp_tool_ceiling_min?: number; acp_activity_batch_ms?: number; jcode_steer_timeout_s?: number } } /** The host captures primitive option values for a new adapter; an accepted edit never retimes an existing launch. */ export const adapterOperationalReaders = (config: () => AdapterTimingConfig) => ({ acpToolCeilingMs: () => (config().adapter_timing?.acp_tool_ceiling_min ?? ACP_TOOL_CEILING_SETTING.defaultValue) * 60_000, acpActivityBatchMs: () => config().adapter_timing?.acp_activity_batch_ms ?? ACP_ACTIVITY_BATCH_SETTING.defaultValue, jcodeSteerTimeoutMs: () => (config().adapter_timing?.jcode_steer_timeout_s ?? JCODE_STEER_TIMEOUT_SETTING.defaultValue) * 1000, }); export const operationalNumber = (key: string): OperationalNumberSetting | undefined => OPERATIONAL_NUMBERS.find(def => def.key === key); export interface ModelOperationalConfig { model_relay?: { cooldown_failures?: number; cooldown_s?: number; cooldown_cap_s?: number; terminal_history_limit?: number } } /** The host captures these primitives when it constructs a relay. No current request, cooldown or journal is rewritten by reload. */ export const modelOperationalReaders = (config: () => ModelOperationalConfig) => ({ cooldownFailures: () => config().model_relay?.cooldown_failures ?? MODEL_COOLDOWN_FAILURES_SETTING.defaultValue, cooldownMs: () => (config().model_relay?.cooldown_s ?? MODEL_COOLDOWN_SETTING.defaultValue) * 1000, cooldownCapMs: () => (config().model_relay?.cooldown_cap_s ?? MODEL_COOLDOWN_CAP_SETTING.defaultValue) * 1000, terminalHistoryLimit: () => config().model_relay?.terminal_history_limit ?? MODEL_TERMINAL_HISTORY_SETTING.defaultValue, }); export interface ConsoleOperationalConfig { console?: { poll_ms?: number; notice_ms?: number; history_limit?: number; pending_stream_limit?: number; attention_limit?: number } } /** Existing authenticated settings_get rows only: pending file values and client-supplied bounds never choose a value. */ export function consoleOperationalSnapshot(rows: unknown) { const read = (def: OperationalNumberSetting): number => { if (!Array.isArray(rows)) return def.defaultValue; const matches = rows.filter(row => row && typeof row === "object" && !Array.isArray(row) && row.key === def.key); if (matches.length !== 1) return def.defaultValue; const value: unknown = matches[0].value; return typeof value === "number" && Number.isSafeInteger(value) && value >= def.min && value <= def.max ? value : def.defaultValue; }; return Object.freeze({ pollMs: read(CONSOLE_POLL_SETTING), noticeMs: read(CONSOLE_NOTICE_SETTING), historyLimit: read(CONSOLE_HISTORY_SETTING), pendingStreamLimit: read(CONSOLE_PENDING_STREAM_SETTING), attentionLimit: read(CONSOLE_ATTENTION_SETTING) }); }