Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
feat([issue-12738]): store CoS agent records in PostgreSQL instead of state.json
Adam Eivy23125a0e65a9
diff --git a/docs/BACKUP.md b/docs/BACKUP.mdindex 06c8760dde..86a8f24018 100644--- a/docs/BACKUP.md+++ b/docs/BACKUP.md@@ -351,7 +351,7 @@ a snapshot as a complete recovery point.Now that PostgreSQL is a **required** dependency (it owns the creative catalog, memory, and a growing set of app-native records — see [Storage Classification Contract](./STORAGE.md)), **the database dump is part of required system state, not an optional extra.** A snapshot that captured `data/` but failed to capture the DB is incomplete, and PortOS surfaces that explicitly.-The dump includes the machine-local `cos_pending_agent_feedback` reference index, the `cos_tasks` CoS task queues and their completed-task history (the Markdown task files were retired into it and kept as `*.imported`), and `review_queue_triage` presentation markers. The queue's source records remain in their owning stores; restoring the dump therefore preserves snooze/dismissal decisions without exporting them through federation or duplicating source payloads.+The dump includes the machine-local `cos_pending_agent_feedback` reference index, the `cos_tasks` CoS task queues and their completed-task history (the Markdown task files were retired into it and kept as `*.imported`), the `cos_agents` CoS agent working set (moved out of `data/cos/state.json`, whose pre-migration copy is kept as `state.json.imported`), and `review_queue_triage` presentation markers. The queue's source records remain in their owning stores; restoring the dump therefore preserves snooze/dismissal decisions without exporting them through federation or duplicating source payloads.Implementation: `server/services/backup.js` (snapshot/dump/restore), `server/services/backupScheduler.js` (cron), `server/routes/backup.js` (API), and `server/routes/database.js` (DB export/sync).@@ -511,6 +511,8 @@ After the preflight, rsync copies `<snapshot>/data/` back to `./data/`. RestoreFile restore copies what the snapshot holds and leaves live files the snapshot lacks in place. One consequence is handled explicitly: a snapshot taken before the CoS config moved to its own file (#6182) carries its CoS settings inside `cos/state.json` and has no `cos/config.json`. When such a restore lands that legacy slice, the live `cos/config.json` (newer than the restore point) is moved aside to `cos/config.pre-restore-<timestamp>.json` and the settings are recovered from the restored `state.json`; otherwise the newer file would keep winning every read.+A snapshot taken before the agent records moved into PostgreSQL (#12738) also carries an `agents` map inside `cos/state.json`. A file restore does not bring those records back: agent records are `cos_agents` rows, restored with the database dump, and the CoS runtime ignores (and drops on its next save) an `agents` map it finds in `state.json`.+**Historical file-to-database metadata (#12397).** On a PostgreSQL install, full and selective file restores refuse selected legacy metadata with `409 BACKUP_LEGACY_DB_METADATA` before preview or execution starts a transfer. The diagnostic names only affected domains: universes (record `index.json` and type-level `config.runs`), pipeline issues/series, Story Builder, Writers Room (`folders.json`, `exercises.json`, work `manifest.json`), and Creative Director projects. These records now live in PostgreSQL. A live domain import marker can suppress re-import after file restore; restoring the older file-migration ledger does not reset that separate marker. Clearing markers is unsafe too: the importers preserve existing rows with `ON CONFLICT DO NOTHING`, so they cannot provide replacement-restore semantics for overlapping IDs.Keep the original backup intact. Copy it into a disposable offline install running the compatible historical version, upgrade that isolated install through the file-to-database transition, and verify the imported records and linked files there. Make a current-format file and PostgreSQL backup from the converted install, then use the normal verified restore workflow. Do not convert against the live database or delete its markers. Current file-primary siblings (such as manuscript reviews, draft Markdown and media assets) and parked `.imported` recovery evidence remain eligible for file restore. Selecting one of those siblings is allowed even if another part of the snapshot contains legacy metadata.diff --git a/docs/STORAGE.md b/docs/STORAGE.mdindex cc9afc848e..80df4ac917 100644--- a/docs/STORAGE.md+++ b/docs/STORAGE.md@@ -159,6 +159,8 @@ The queues federate exactly as before: a full-sync peer advertises its working sDirect editing of the Markdown files and the file watcher are retired (decision 2026-10-20, [plan](./plans/2026-10-20-cos-task-queue-to-postgres.md)); there is no runtime fallback to either. **Migration:** db-migration 018 imports `data/TASKS.md` and `data/COS-TASKS.md` (at the location the install's CoS config named, else the defaults) and, on installs that ran the interim completed-task archive, folds `cos_task_archive` in and drops it; it verifies that every parsed task landed as a row before committing. db-migration 019, which runs only after 018 has committed, renames each file to `<name>.imported` (the recovery copy; nothing reads it) and drops the `userTasksFile` / `cosTasksFile` config keys. Input-derived, so no `data.reference` seed ships for either file. Backed up by the Postgres dump. Repository: `server/services/cosTaskRepository.js` (in-memory backend only under the dev/test escape hatch); store: `server/services/cosTaskStore.js`.+**CoS agent records** (`cos_agents`, `db-primary`, #12738): the agent working set — running agents plus finished ones `archiveStaleAgents()` has not yet moved to `data/cos/agents/<date>/` (retention `completedAgentRetentionMs`, 24 h by default) — is one row per agent, keyed by `agent_id`. The row's `agent` column is the record's exact JSON text (`json`, not `jsonb`, so bytes and key order round-trip and the merge-admission ownership compare stays exact); `task_id`, `status`, `started_at`, `completed_at` and `pid` are projections for queries, and `seq` keeps insertion order. `cosState` keeps its cached `state.agents` map, so callers are unchanged, and `saveState()` diffs it against each record's last stored text (`server/lib/cosAgentRows.js` `planAgentSave`) and writes only the changed rows in one transaction: a status or output update for one agent no longer rewrites every other agent, and the rest of `state.json` (config is in `config.json`) is rewritten only when it changed. `cos_agent_store.version` bumps with every write and a write names the version it was planned against; a stale cache (for example after a database restore) is refused with `COS_AGENTS_STALE` and reloads, rather than deleting rows it never saw. Safety readers (`readAgentsStateForSafetyCheck`, merge admission, the restore gate, abandoned-agent reconciliation) read the rows, never the cache, and an unreadable store is `trusted: false`. The sentinel sweep reads only `{ id, status }` (`listStoredAgentStatuses`). Older finished agents stay in the date-bucketed archive, reached by id through the agent index; they are never loaded wholesale. `state.json` keeps the small runtime remainder (running/paused flags, stats, Persistent Mind runtime, merge leases) behind `withStateLock`. **Intentionally machine-local — never federated.** **Migration:** db-migration 020 imports `state.json`'s `agents` map and verifies every record byte for byte (`agent::text` against its source serialization) before committing, so a failure stops the boot; db-migration 021, which runs only after 020 committed, re-verifies, keeps the original file as `state.json.imported` (the recovery copy; nothing reads it) and rewrites `state.json` without the key. There is no runtime fallback: a `state.json` that still carries the map (a restored pre-migration file) is ignored with a warning and the key is dropped on the next save. Input-derived, so no `data.reference` seed. Stale `state.json.<pid>.<ms>.<uuid>.tmp` files left by a write that was killed are swept once per process on first load (`sweepStaleStateTempFiles`: other processes' files older than a minute). Backed up by the Postgres dump. Repository: `server/services/cosAgentRepository.js` (in-memory backend, persisted to `data/cos-agents.json`, only under the dev/test escape hatch).+**Private security assessments** reuse the existing Review Hub item store for report prose and CoS agent archives for local source/transcripts. They add no store format or database migration. The source inventory stays in run memory; interruption before report validation requires a new assessment. Assessment tasks and all their archive files are excluded from peer federation, and shared socket notifications carry no report prose. Reports follow existing local backup and Review Hub retention; temporary sandbox homes are removed after completion. See [the assessment design and research](research/2026-09-06-private-security-models.md).**Where it lives.** Filesystem under `./data/` (or an OS-managed sync container). DB may hold metadata/index rows (hashes, word counts, segment indexes) but **not** the body.diff --git a/docs/TROUBLESHOOTING.md b/docs/TROUBLESHOOTING.mdindex 8a8c8c2426..305a8547f4 100644--- a/docs/TROUBLESHOOTING.md+++ b/docs/TROUBLESHOOTING.md@@ -593,13 +593,13 @@ CoS runtime records share one serialized state queue (`withStateLock` in `server|------|------|| `🚦 Merge admission operation=… status=settled …` | a settled admission transaction that took 1 s or more || `🚦 Merge admission … status=queue-timeout\|pending\|failed …` | always |-| `🐢 CoS state write slow outcome=… totalMs=… prepareMs serializeMs writeMs replaceMs dirSyncMs loopLagMs loopLagMaxMs sizeKiB` | any `saveState` of 1 s or more |+| `🐢 CoS state write slow outcome=… totalMs=… prepareMs agentsMs serializeMs writeMs replaceMs dirSyncMs loopLagMs loopLagMaxMs sizeKiB` | any `saveState` of 1 s or more || `🐢 CoS state queue slow hold heldMs=… waitMs=… loopLagMs=… loopLagMaxMs=…` | any operation that held the queue for 1 s or more |Elapsed time inside an awaited phase is asynchronous wait plus delayed scheduling. While an instrumented operation is open, a 100 ms tick measures how long timers sat overdue (`server/lib/eventLoopLagProbe.js`) and charges that to the current phase as `…LagMs`. Read each slow phase as lag versus the rest:- **`queueMs` high, `queueLagMs` near 0** — the state queue was held by other operations while the loop stayed responsive. Look for `🐢 CoS state queue slow hold` lines in the same window.-- **`persistenceMs` (or `trustedStateMs`) high, its `…LagMs` near 0** — asynchronous I/O wait: storage or fsync latency, or libuv threadpool queueing behind other file work. `writeMs` (temp write + file flush), `replaceMs` (rename) and `dirSyncMs` (directory flush) locate it; `readMs`/`parseMs` split the ownership read.+- **`persistenceMs` (or `trustedStateMs`) high, its `…LagMs` near 0** — asynchronous I/O wait: storage or fsync latency, or libuv threadpool queueing behind other file work. `agentsMs` (the changed `cos_agents` rows, written in one transaction), `writeMs` (temp write + file flush), `replaceMs` (rename) and `dirSyncMs` (directory flush) locate it; `readMs`/`parseMs` split the ownership read.- **`…LagMs` close to the phase time, or `timerLateMs` well above 0** — the event loop could not run: blocking JavaScript, garbage collection, host CPU starvation or process suspension. These numbers do not tell those apart; capture a CPU profile ([safely](#dynamic-import-callback-missing-after-profiling)) and host load at the time.Lag resolves to about one tick: the first 100 ms of a stall and any overdue stretch of 20 ms or less are not counted. `timerLateMs` is how late the 15 s admission deadline fired; anything beyond a few milliseconds of timer jitter proves scheduling delay on its own.diff --git a/docs/features/chief-of-staff.md b/docs/features/chief-of-staff.mdindex 4cb02ee601..f90603e711 100644--- a/docs/features/chief-of-staff.md+++ b/docs/features/chief-of-staff.md@@ -5,6 +5,7 @@ Autonomous agent manager that works the task queues, spawns sub-agents, and main## Architecture- **Task Store** (`server/services/cosTaskStore.js`): The user and internal queues as `cos_tasks` rows (working-set cache, row-diff saves); `server/services/cosTaskRepository.js` is the persistence layer+- **Agent Records** (`server/services/cosAgentRepository.js`): The running and recently finished agents as `cos_agents` rows; `cosState.saveState()` writes only the rows whose record changed- **CoS Service** (`server/services/cos.js`): State management, health monitoring, task evaluation- **Sub-Agent Spawner** (`server/services/subAgentSpawner.js`): Claude CLI execution with MCP- **CoS Routes** (`server/routes/cos.js`): REST API endpoints@@ -263,7 +264,8 @@ silently released, and no initialized submodule is discarded. Older receiptswithout a checkout binding retain the existing branch-reconciliation fallback.Leases extend the existing machine-local CoS runtime ownership in-`data/cos/state.json`, under its existing write queue; there is no new store,+`data/cos/state.json` (the agent records they bind are `cos_agents` rows),+under its existing write queue; there is no new store,seed, migration, or federation payload. Admission waits at most 15 seconds tostart its state-queue callback; an expired waiter returns `admitted: false`,`reason: state-queue-timeout`, and `retryAfterMs: 5000`. Its fenced callback@@ -309,7 +311,7 @@ The claim prompt binds each branch through the host-control-gated`POST /api/cos/claim-ownership` (`action: "bind"`, registered agent ID, branch)BEFORE creating that worktree, and `release` hands a branch off mid-run. Therun's completion releases everything; quiet diffs, cwd, or a missing basename-never do. Bindings live on the run record in `data/cos/state.json` — no new+never do. Bindings live on the run record (a `cos_agents` row) — no newstore or migration; records without them keep their previous behavior.branch-reconcile applies the same owners twice: its scan holds a branch with a@@ -380,7 +382,7 @@ authorize an unreadable first result.```./data/cos/-├── state.json # Daemon state and config+├── state.json # Daemon runtime state (config is config.json; agent records are cos_agents rows)├── agents/{agentId}/ # Agent prompts and outputs├── reports/{date}.json # Daily reports└── scripts/ # Generated automation scriptsdiff --git a/scripts/lib/dbTestFiles.js b/scripts/lib/dbTestFiles.jsindex fd678d2553..5e6badc765 100644--- a/scripts/lib/dbTestFiles.js+++ b/scripts/lib/dbTestFiles.js@@ -66,6 +66,10 @@ export const DB_TEST_INCLUDE = ['scripts/migrateMemoryToPg.db.test.js','lib/db/schema/audit.db.test.js','lib/db/schema/syncFeed.db.test.js',+ 'services/cosTaskRepository.db.test.js',+ 'scripts/db-migrations/018-cos-tasks-from-files.db.test.js',+ 'services/cosAgentRepository.db.test.js',+ 'scripts/db-migrations/020-cos-agents-from-state.db.test.js',];const patterns = DB_TEST_INCLUDE.map((pattern) => posix.normalize(posix.join('server', pattern)));diff --git a/scripts/recover-initialized-checkout.js b/scripts/recover-initialized-checkout.jsindex b8c9f022a6..1dc5a7a1bf 100644--- a/scripts/recover-initialized-checkout.js+++ b/scripts/recover-initialized-checkout.js@@ -93,8 +93,9 @@ async function prepareInstall(installRoot, appId) {for (const relative of ['data', 'data/cos', 'data/cos/agents', 'data/cos/reports', 'data/cos/scripts', 'data/cos/initialized-checkout-retirements']) {await canonicalDirectory(join(installRoot, relative));}+ // Agent records live in cos_agents (#12738); state.json must still be a readable object.const state = await readJson(join(installRoot, 'data/cos/state.json'), 64 * 1024 * 1024);- if (!object(state) || !object(state.agents)) fail('recovery-ownership-unreadable');+ if (!object(state)) fail('recovery-ownership-unreadable');const context = async requestedId => {if (requestedId !== appId) fail('recovery-app-changed');const apps = await readJson(join(installRoot, 'data/apps.json'), 16 * 1024 * 1024);diff --git a/scripts/recover-initialized-checkout.test.js b/scripts/recover-initialized-checkout.test.jsindex a5c2fe6e73..90aa5312c3 100644--- a/scripts/recover-initialized-checkout.test.js+++ b/scripts/recover-initialized-checkout.test.js@@ -17,7 +17,7 @@ async function fixture(action = 'review') {// Persisted registries key apps by ID; getAppById adds the id at read time.const apps = { apps: { 'portos-default': { repoPath: root } } };await writeFile(join(root, 'data/apps.json'), JSON.stringify(apps));- await writeFile(join(root, 'data/cos/state.json'), JSON.stringify({ agents: {} }));+ await writeFile(join(root, 'data/cos/state.json'), JSON.stringify({ running: false }));const body = { ...request, ...(['restore', 'finalize', 'resume-quarantine'].includes(action)? { review: { digest: 'b'.repeat(64), ...(action === 'resume-quarantine' ? { acceptSuperseded: true } : {}) } } : {}) };const input = join(root, 'request.json');@@ -127,7 +127,7 @@ describe('initialized checkout recovery CLI', () => {const f = await fixture('restore');if (change === 'missing-install') f.args[3] = join(f.root, 'absent');if (change === 'missing-agents') await rm(join(f.root, 'data/cos/agents'), { recursive: true });- if (change === 'invalid-state') await writeFile(join(f.root, 'data/cos/state.json'), '{"agents":null}');+ if (change === 'invalid-state') await writeFile(join(f.root, 'data/cos/state.json'), '[]');if (change === 'wrong-app-root') {f.apps.apps['portos-default'].repoPath = join(f.root, 'other');await writeFile(join(f.root, 'data/apps.json'), JSON.stringify(f.apps));diff --git a/scripts/seed-readme-demo-data.mjs b/scripts/seed-readme-demo-data.mjsindex 10770650d9..1631760c40 100644--- a/scripts/seed-readme-demo-data.mjs+++ b/scripts/seed-readme-demo-data.mjs@@ -8,9 +8,10 @@* empty directory, then overlays fake records that make the public UI pages* useful to inspect without exposing a real install.*- * The demo's tasks are Markdown files that db-migration 018 imports, so the demo- * server must start against a database that has not yet run it (a fresh,- * disposable one). An already-migrated database ignores them.+ * The demo's tasks are Markdown files that db-migration 018 imports, and its+ * agents are a `cos/state.json` `agents` map that db-migration 020 imports, so+ * the demo server must start against a database that has not yet run them (a+ * fresh, disposable one). An already-migrated database ignores them.** Usage:* node scripts/seed-readme-demo-data.mjs --root /tmp/portos-readme-demo@@ -174,8 +175,8 @@ writeJson('providers.json', {},});-// Durable user config lives in its own file (#6182); state.json holds only the-// runtime records.+// Durable user config lives in its own file (#6182); state.json holds the+// runtime records in their pre-migration shape (see the header).writeJson('cos/config.json', {userTasksFile,cosTasksFile,diff --git a/server/lib/README.md b/server/lib/README.mdindex 34efdf3324..8183937410 100644--- a/server/lib/README.md+++ b/server/lib/README.md@@ -546,6 +546,7 @@ The barrel `server/lib/index.js` is a machine-checkable enumeration of every pub| `taskGenerationOverrides.js` | `applyTaskGenerationOverrides(provider, metadata)` — the per-RUN `temperature` / `thinking` / `effort` a task form or schedule entry pins, layered onto a COPY of the provider record (the saved record is shared by every other run; the child-env composer turns the copy into OpenCode’s dynamic `agent.build` config downstream). Every value arrives untrusted — a COS-TASKS.md round-trip hands back strings — so each override applies only when it validates, and an empty temperature field is absent rather than `0`, which would otherwise pin greedy decoding on a task whose author just cleared the box. || `cosTaskPrompt.js` | The CoS task prompt/note split (#4153) — `metadata.prompt` is the full agent-facing payload, `metadata.context` the one-line human note. `isPromptPayload(value)` is the single discriminator (a newline means it's a prompt, not a note), shared by the store's write path and the on-disk migration so they can't classify the same task differently. `getTaskPrompt(task)` prefers `metadata.prompt` and falls back to a legacy `metadata.context`; `getTaskContextNote(task)` returns the note only on an already-split task; `taskContextBlock(task)` renders both as one block so prompt templates (including customized ones) never see the split; `splitTaskPromptFields(metadata)` routes a multi-line `context` to `prompt` at CREATE time only. Pure, dependency-free. || `cosTaskRows.js` | Row model for the `cos_tasks` queues: the stored form of a task (the old Markdown round-trip's normalization), the recent-completed working-set rule, fractional queue `position` assignment, and `planListSave` (turn "the whole list changed" into "only these rows changed"). Shared by the Postgres and in-memory repository backends so they cannot drift |+| `cosAgentRows.js` | Row model for `cos_agents`, the CoS agent working set: `agentRowColumns(agent)` (the task/status/start/completion/pid projections the queries read) and `planAgentSave({ persisted, next })` (turn "the whole agents map" into "only these rows changed" by comparing each record's JSON text with the one last stored). Shared by `cosState.saveState` and the repository backends || `taskPauseHold.js` | The user-PAUSE hold — pure metadata helpers shared by the writer (`pauseAgent`), the reader (`resumeAgent`), and the clearer (`updateTask`). `AGENT_PAUSED_CATEGORY` is the `blockedCategory` a pause stamps; `PAUSE_METADATA_KEYS` names every key it writes; `pauseMetadata({agentId, pausedAt, workspacePath, runId})` arms it and `clearedPauseMetadata()` releases it (undefined-valued, so `updateTask` deletes rather than serializing `"null"`); `isAgentPausedTask(task)` / `isResumablePausedTask(task, agentId)` answer whether a pause is still live and whether it is THIS agent's. `updateTask` clears the whole set whenever the task leaves `blocked`, so a revive by any path (dedupe, cooldown expiry, manual unblock) can't leave a task running under a stale pause. Also carries the cycle-safe RELEASE adapter (#3730) `agentManagement.js` registers at load — `resolvePausedTaskResume(task)` hands `updateTask` the paused run's resume pointer before the write (so any un-block resumes on the preserved worktree, not just the Resume dialog) and `retirePausedAgent(agentId, taskId, branchName)` retires the paused record after it. || `taskBlockCategories.js` | The `blockedCategory` vocabulary — which blocks are the system's to clear and which are a person's, read by the pause logic, the failure reaper, and the investigation auto-retry so they can't drift into three literal sets. `PAUSED_BLOCKED_CATEGORIES` (paused until something outside the task changes — keeps the resume pointer), `USER_DECISION_BLOCKED_CATEGORIES` (user intent / open decision — the reaper's exemption), `NON_AUTO_RETRY_BLOCK_CATEGORIES` (their union — a completed investigation must never revive these), `TIMED_COOLDOWN_BLOCKED_CATEGORIES` (a timer clears these — the cooldown sweeper revives them, so block reporters stay quiet), `PROVIDER_CONFIG_BLOCKED_CATEGORY` (the shared literal the agent-spawn stamp site uses, so the stamp cannot drift from the two sets it belongs to). Pure. || `taskRequeue.js` | The REQUEUE stamp (#3376) — pure metadata helpers for the one BACKWARD lifecycle step (`in_progress → pending`, performed by the orphan sweep and the retry-hold release). `REQUEUED_AT_KEY` / `LAST_SPAWNED_AT_KEY` name the two stamps; `isPostSpawnRequeue(pendingTask, inProgressTask)` answers whether the requeue happened strictly AFTER that spawn, which is how the federated merge tells a real requeue apart from an ordinary edit landing on a peer's stale `pending` copy. Returns false when either stamp is missing, so callers fall back to the lifecycle rank. |diff --git a/server/lib/cosAgentRows.js b/server/lib/cosAgentRows.jsnew file mode 100644index 0000000000..9b2cced3f6--- /dev/null+++ b/server/lib/cosAgentRows.js@@ -0,0 +1,49 @@+/**+ * Row model for `cos_agents`: one row per CoS agent record in the runtime+ * working set (running agents plus finished ones not yet archived to+ * `data/cos/agents/<date>/`).+ *+ * The record is stored as its exact JSON text, so a read returns the same bytes+ * (key order included) that the caller wrote. `planAgentSave` turns "here is the+ * whole agents map" into "only these rows changed" by comparing each record's+ * serialization with the one last persisted, which is what keeps a status or+ * output update for one agent from rewriting every other agent.+ */++const timestamp = (value) => {+ const ms = typeof value === 'string' || typeof value === 'number' ? Date.parse(value) : NaN;+ return Number.isFinite(ms) ? new Date(ms).toISOString() : null;+};++/** The projection columns the queries read, derived from one record. */+export function agentRowColumns(agent) {+ return {+ taskId: typeof agent?.taskId === 'string' ? agent.taskId : null,+ status: typeof agent?.status === 'string' ? agent.status : 'unknown',+ startedAt: timestamp(agent?.startedAt),+ completedAt: timestamp(agent?.completedAt),+ pid: Number.isSafeInteger(agent?.pid) && agent.pid > 0 && agent.pid <= 2_147_483_647 ? agent.pid : null,+ };+}++/**+ * Diff the next agents map against the persisted serializations.+ *+ * @param {{ persisted: Map<string, string>, next: object }} input+ * `persisted` maps agent id to the JSON text last stored for it.+ * @returns {{ upserts: Array<{ id: string, body: string, agent: object }>, deletes: string[], persisted: Map<string, string> }}+ * `persisted` is what the store holds once the plan is applied.+ */+export function planAgentSave({ persisted, next }) {+ const upserts = [];+ const after = new Map();+ for (const [id, agent] of Object.entries(next ?? {})) {+ // An undefined entry is absent, exactly as JSON.stringify of the map drops it.+ if (agent === undefined) continue;+ const body = JSON.stringify(agent);+ after.set(id, body);+ if (persisted.get(id) !== body) upserts.push({ id, body, agent });+ }+ const deletes = [...persisted.keys()].filter((id) => !after.has(id));+ return { upserts, deletes, persisted: after };+}diff --git a/server/lib/db.guards.test.js b/server/lib/db.guards.test.jsindex 2824b70492..771697a06d 100644--- a/server/lib/db.guards.test.js+++ b/server/lib/db.guards.test.js@@ -473,8 +473,10 @@ describe('DB-backed test files are covered by vitest.config.db.js', () => {// detector regex) but is not a DB-backed suite — exclude it.if (base === 'db.guards.test.js') continue;const head = readHead(join(root, rel));- // A real DB-backed suite both gates on checkHealth() and exposes dbReady.- if (!(/\bcheckHealth\b/.test(head) && /\bdbReady\b/.test(head))) continue;+ // A real DB-backed suite gates on checkHealth() and either exposes dbReady or+ // gates through requireDbOrSkip (the cos_tasks suites slipped past a+ // dbReady-only detector and never ran).+ if (!(/\bcheckHealth\b/.test(head) && /\b(dbReady|requireDbOrSkip)\b/.test(head))) continue;const relativePath = join(prefix, rel);const covered = (hasDbGlob && base === 'db.test.js') || explicitIncludes.has(relativePath);if (!covered) offenders.push(prefix + rel.split(sep).join('/'));diff --git a/server/lib/db/schema/README.md b/server/lib/db/schema/README.mdindex acd332d2c3..eebe9ebc71 100644--- a/server/lib/db/schema/README.md+++ b/server/lib/db/schema/README.md@@ -31,6 +31,7 @@ init-db.sql, each with a one-line reason.| `userActions.js` | `userActionsDdl` | Operator-action ledger — what the human did in the UI (machine-local) || `reviewQueueTriage.js` | `reviewQueueTriageDdl` | Machine-local Review Hub presentation markers — snooze, optional dismissal, and delivery generation keyed by canonical action identity || `cosTasks.js` | `cosTasksDdl` | The CoS task queues, one row per task (stored form, queue order, completed-at window) plus a per-queue write version |+| `cosAgents.js` | `cosAgentsDdl` | The CoS agent working set (running + not-yet-archived), one row per agent (exact JSON text, query projections, insertion order) plus a write version || `cosAgentFeedback.js` | `cosAgentFeedbackDdl` | Durable machine-local references to completed manual CoS runs awaiting a rating (agent id + archive locator only) || `aiGraph.js` | `aiGraphDdl` | AI provider connection graph — connections, harness bindings, executable route bindings (machine-local) || `peerExecution.js` | `peerExecutionDdl` | Receiver-local permanent execution consumption and generation floors; no grants or federation |@@ -52,7 +53,7 @@ init-db.sql, each with a one-line reason.### Composer (`index.js`)-- `buildUpgradeDdl()` → phase-1 list (`core` → `tribe` → `humanActivity` → `post` → `commissions` → `userActions` → `reviewQueueTriage` → `cosAgentFeedback` → `cosTasks` → `aiGraph` → `peerExecution`).+- `buildUpgradeDdl()` → phase-1 list (`core` → `tribe` → `humanActivity` → `post` → `commissions` → `userActions` → `reviewQueueTriage` → `cosAgentFeedback` → `cosTasks` → `cosAgents` → `aiGraph` → `peerExecution`).- `buildCatalogDdl()` → phase-2 list (`catalog` → `media` → `catalogUserTypes` →`universes` → `library` → `pipeline` → `writersRoom` → `lora` → `privacy` → `stackerNews` → `x` →`beeper` → `audit` DDL → audit triggers → `syncFeed` DDL → sync-feed triggers).diff --git a/server/lib/db/schema/cosAgents.js b/server/lib/db/schema/cosAgents.jsnew file mode 100644index 0000000000..0fdc8fe2ce--- /dev/null+++ b/server/lib/db/schema/cosAgents.js@@ -0,0 +1,25 @@+// The CoS agent records in the runtime working set (running agents plus finished+// ones not yet archived to data/cos/agents/<date>/), one row per agent. `agent`+// is the record's exact JSON text (`json`, not `jsonb`, so key order and bytes+// round-trip); the other columns are projections for queries. `seq` keeps the+// map's insertion order. `cos_agent_store.version` bumps on every write and a+// write names the version it was planned against, so a stale cache is refused.+// Machine-local, never federated.+export const cosAgentsDdl = [+ `CREATE TABLE IF NOT EXISTS cos_agents (+ agent_id TEXT COLLATE "C" PRIMARY KEY,+ seq BIGINT GENERATED BY DEFAULT AS IDENTITY,+ task_id TEXT,+ status TEXT NOT NULL,+ started_at TIMESTAMPTZ,+ completed_at TIMESTAMPTZ,+ pid INTEGER,+ agent JSON NOT NULL+ )`,+ `CREATE INDEX IF NOT EXISTS idx_cos_agents_seq ON cos_agents (seq)`,+ `CREATE INDEX IF NOT EXISTS idx_cos_agents_status ON cos_agents (status)`,+ `CREATE TABLE IF NOT EXISTS cos_agent_store (+ id SMALLINT PRIMARY KEY CHECK (id = 1),+ version BIGINT NOT NULL DEFAULT 0+ )`,+];diff --git a/server/lib/db/schema/index.js b/server/lib/db/schema/index.jsindex c5d654898f..e32ad2368b 100644--- a/server/lib/db/schema/index.js+++ b/server/lib/db/schema/index.js@@ -22,6 +22,7 @@ import { userActionsDdl } from './userActions.js';import { reviewQueueTriageDdl } from './reviewQueueTriage.js';import { cosAgentFeedbackDdl } from './cosAgentFeedback.js';import { cosTasksDdl } from './cosTasks.js';+import { cosAgentsDdl } from './cosAgents.js';import { aiGraphDdl } from './aiGraph.js';import { peerExecutionDdl } from './peerExecution.js';import { catalogDdl, catalogUserTypesDdl, catalogPendingAppliesDdl } from './catalog.js';@@ -52,6 +53,7 @@ export {reviewQueueTriageDdl,cosAgentFeedbackDdl,cosTasksDdl,+ cosAgentsDdl,aiGraphDdl,peerExecutionDdl,catalogDdl,@@ -93,6 +95,7 @@ export function buildUpgradeDdl() {...reviewQueueTriageDdl,...cosAgentFeedbackDdl,...cosTasksDdl,+ ...cosAgentsDdl,...aiGraphDdl,...peerExecutionDdl,];diff --git a/server/lib/db/schema/schema.test.js b/server/lib/db/schema/schema.test.jsindex f22cd5849c..06078a4777 100644--- a/server/lib/db/schema/schema.test.js+++ b/server/lib/db/schema/schema.test.js@@ -25,7 +25,7 @@ import {} from './index.js';const DOMAIN_ARRAYS = [- 'coreDdl', 'tribeDdl', 'humanActivityDdl', 'postDdl', 'commissionsDdl', 'userActionsDdl', 'reviewQueueTriageDdl', 'cosAgentFeedbackDdl', 'cosTasksDdl', 'aiGraphDdl', 'peerExecutionDdl',+ 'coreDdl', 'tribeDdl', 'humanActivityDdl', 'postDdl', 'commissionsDdl', 'userActionsDdl', 'reviewQueueTriageDdl', 'cosAgentFeedbackDdl', 'cosTasksDdl', 'cosAgentsDdl', 'aiGraphDdl', 'peerExecutionDdl','catalogDdl', 'catalogIdentityDdl', 'catalogUserTypesDdl', 'mediaDdl', 'universesDdl','libraryDdl', 'pipelineDdl', 'writersRoomDdl', 'loraDdl', 'privacyDdl', 'stackerNewsDdl', 'xDdl','beeperDdl', 'mindToolRecipesDdl',@@ -51,6 +51,7 @@ describe('db/schema barrel + composer (#2832)', () => {...schema.reviewQueueTriageDdl,...schema.cosAgentFeedbackDdl,...schema.cosTasksDdl,+ ...schema.cosAgentsDdl,...schema.aiGraphDdl,...schema.peerExecutionDdl,];diff --git a/server/lib/index.js b/server/lib/index.jsindex 6d529bcfbd..ce5886530e 100644--- a/server/lib/index.js+++ b/server/lib/index.js@@ -494,6 +494,7 @@ export * from './taskParser.js';export * from './taskGenerationOverrides.js';export * from './cosTaskPrompt.js';export * from './cosTaskRows.js';+export * from './cosAgentRows.js';export * from './taskPauseHold.js';export * from './taskBlockCategories.js';export * from './taskRequeue.js';diff --git a/server/routes/cosAgentRoutes.admissionQueue.test.js b/server/routes/cosAgentRoutes.admissionQueue.test.jsindex 88b379b5f9..7b6704e286 100644--- a/server/routes/cosAgentRoutes.admissionQueue.test.js+++ b/server/routes/cosAgentRoutes.admissionQueue.test.js@@ -139,10 +139,28 @@ describe('merge admission state queue deadline', () => {const admission = await response;const logs = [...console.log.mock.calls, ...console.warn.mock.calls].flat();for (const line of logs) {- expect(line).toMatch(/^🚦 Merge admission operation=acquire status=(pending|settled) phase=(trustedState|origin|persistence) phaseMs=\d+ totalMs=\d+ queueMs=\d+ trustedStateMs=\d+ stateLoadMs=\d+ originMs=\d+ ownerRecoveryMs=\d+ persistenceMs=\d+ readMs=\d+ parseMs=\d+ prepareMs=\d+ serializeMs=\d+ writeMs=\d+ replaceMs=\d+ dirSyncMs=\d+ loopLagMs=\d+ loopLagMaxMs=\d+ queueLagMs=\d+ trustedStateLagMs=\d+ persistenceLagMs=\d+ timerLateMs=\d+$/);+ expect(line).toMatch(/^🚦 Merge admission operation=acquire status=(pending|settled) phase=(trustedState|origin|persistence) phaseMs=\d+ totalMs=\d+ queueMs=\d+ trustedStateMs=\d+ stateLoadMs=\d+ originMs=\d+ ownerRecoveryMs=\d+ persistenceMs=\d+ readMs=\d+ parseMs=\d+ prepareMs=\d+ agentsMs=\d+ serializeMs=\d+ writeMs=\d+ replaceMs=\d+ dirSyncMs=\d+ loopLagMs=\d+ loopLagMaxMs=\d+ queueLagMs=\d+ trustedStateLagMs=\d+ persistenceLagMs=\d+ timerLateMs=\d+$/);expect(line).not.toContain(admission.token);expect(line).not.toContain(agentId);expect(line).not.toContain('/repos');}});});++// #12738: the stored agent rows come back in insertion order, which a cached map+// that deleted and re-added an id does not keep. Ownership compares per record.+describe('merge admission ownership comparison', () => {+ const other = { id: 'synthetic-other', taskId: 'synthetic-task-2', status: 'completed' };++ it('admits when the stored records match the cache in a different order', async () => {+ fixture.state.agents = { [agentId]: fixture.state.agents[agentId], [other.id]: other };+ fixture.disk = { agents: { [other.id]: other, [agentId]: fixture.state.agents[agentId] }, mergeAdmissions: {} };+ expect(await call('acquire')).toMatchObject({ admitted: true });+ });++ it('refuses when a stored record differs from the cached one', async () => {+ fixture.state.agents = { [agentId]: fixture.state.agents[agentId], [other.id]: other };+ fixture.disk = { agents: { [agentId]: fixture.state.agents[agentId], [other.id]: { ...other, status: 'running' } }, mergeAdmissions: {} };+ expect(await call('acquire')).toMatchObject({ admitted: false, reason: 'ownership-stale' });+ });+});diff --git a/server/scripts/db-migrations/020-cos-agents-from-state.db.test.js b/server/scripts/db-migrations/020-cos-agents-from-state.db.test.jsnew file mode 100644index 0000000000..da31453fc4--- /dev/null+++ b/server/scripts/db-migrations/020-cos-agents-from-state.db.test.js@@ -0,0 +1,81 @@+/**+ * Migrations 020 + 021 against a real PostgreSQL: state.json's agents map becomes+ * cos_agents rows byte for byte and in order, the runtime reads them back, and+ * the key is retired only afterwards.+ *+ * Runs via `npm run test:db` (→ `portos_test`) ONLY; skips cleanly when no+ * database is reachable.+ */+import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';+import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';++const paths = vi.hoisted(() => ({ root: '' }));+vi.mock('../../lib/paths.js', async (importOriginal) => {+ const actual = await importOriginal();+ const override = { root: () => paths.root, data: () => `${paths.root}/data`, cos: () => `${paths.root}/data/cos` };+ return { ...actual, PATHS: new Proxy(actual.PATHS, { get: (target, key) => (key in override ? override[key]() : target[key]) }) };+});++const { checkHealth, close, ensureSchema, query, withTransaction } = await import('../../lib/db.js');+const { requireDbOrSkip } = await import('../../lib/dbTestGate.js');+const { up: importUp } = await import('./020-cos-agents-from-state.js');+const { up: retireUp } = await import('./021-retire-state-agents.js');+const { __makePgCosAgentBackend } = await import('../../services/cosAgentRepository.js');++const health = await checkHealth().catch(() => ({ connected: false }));+if (health.connected) await ensureSchema();+const runDb = requireDbOrSkip('db-migrations/020-cos-agents-from-state.db.test', health.connected, 'disposable PostgreSQL unavailable');++const clean = async () => {+ await query('DELETE FROM cos_agents');+ await query('DELETE FROM cos_agent_store');+};++beforeAll(async () => {+ if (!runDb) return;+ paths.root = mkdtempSync(join(tmpdir(), 'cos-agent-migration-db-'));+ await clean();+});+afterAll(async () => {+ if (runDb) await clean();+ if (paths.root) rmSync(paths.root, { recursive: true, force: true });+ if (health.connected) await close();+});++describe.skipIf(!runDb)('migrations 020 + 021 — CoS agent records into PostgreSQL', () => {+ it('imports every record byte for byte, keeps order, then retires the map', async () => {+ const agents = Object.fromEntries(Array.from({ length: 230 }, (_, n) => {+ const id = `agent-example-${String(229 - n).padStart(4, '0')}`;+ return [id, {+ id, taskId: `task-example-${n}`, status: n % 4 === 0 ? 'running' : 'completed',+ startedAt: '2026-10-01T00:00:00.000Z', ...(n % 4 ? { completedAt: '2026-10-01T01:00:00.000Z' } : {}),+ pid: n % 4 === 0 ? 1000 + n : undefined,+ metadata: { note: `quote "x" \\ backslash, tab\t, NUL \u0000, emoji 🚀, }{ #${n}`, zeta: 1, alpha: 2 },+ output: Array.from({ length: n % 7 }, (_, line) => ({ line: `line ${line}` })),+ }];+ }));+ const original = JSON.stringify({ running: false, agents }, null, 2);+ mkdirSync(join(paths.root, 'data/cos'), { recursive: true });+ writeFileSync(join(paths.root, 'data/cos/state.json'), original);++ await withTransaction(importUp);++ const backend = __makePgCosAgentBackend({ query, withTransaction });+ const { version, rows } = await backend.loadAgents();+ expect(version).toBe(1);+ expect(rows.map((row) => row.id)).toEqual(Object.keys(agents));+ expect(rows.map((row) => row.body)).toEqual(Object.values(agents).map((agent) => JSON.stringify(agent)));+ expect((await query(`SELECT count(*)::int AS n FROM cos_agents WHERE status = 'running' AND pid IS NOT NULL`)).rows[0].n).toBe(58);+ expect(JSON.parse(readFileSync(join(paths.root, 'data/cos/state.json'), 'utf8')).agents).toBeDefined();++ await withTransaction(retireUp);+ expect(JSON.parse(readFileSync(join(paths.root, 'data/cos/state.json'), 'utf8'))).toEqual({ running: false });+ expect(readFileSync(join(paths.root, 'data/cos/state.json.imported'), 'utf8')).toBe(original);++ // The runtime's next write is planned against the migrated version.+ await withTransaction(importUp);+ expect((await backend.loadAgents()).version).toBe(1);+ });+});diff --git a/server/scripts/db-migrations/020-cos-agents-from-state.js b/server/scripts/db-migrations/020-cos-agents-from-state.jsnew file mode 100644index 0000000000..013424129e--- /dev/null+++ b/server/scripts/db-migrations/020-cos-agents-from-state.js@@ -0,0 +1,70 @@+/**+ * Move the CoS agent records into PostgreSQL (#12738).+ *+ * Until now every agent record lived in `data/cos/state.json`'s `agents` map,+ * and every status or output update rewrote the whole file. They now live in+ * `cos_agents`, one row per agent, holding the record's exact JSON text. This+ * migration is the only code that still reads the map.+ *+ * Input-derived, so it ships no `data.reference` seed and gates on its INPUT+ * (the map), never on the absence of its output. The map is the source of truth+ * until 021 retires it, so an existing row with the same id is replaced. Every+ * record is verified byte for byte (`agent::text` against the source+ * serialization) before commit; any mismatch throws, the transaction rolls back,+ * and boot stops to retry on the next start rather than serve a half-migrated+ * agent set. The store version is bumped so a cache planned before this import+ * (a database restore replaying migrations at runtime) cannot overwrite it.+ */+import { readCosStateAgents } from './_cosStateAgents.js';++const BATCH = 100;++// Frozen copy of the projection columns lib/cosAgentRows.js derives.+function rowColumns(agent) {+ const timestamp = (value) => {+ const ms = typeof value === 'string' || typeof value === 'number' ? Date.parse(value) : NaN;+ return Number.isFinite(ms) ? new Date(ms).toISOString() : null;+ };+ return {+ taskId: typeof agent?.taskId === 'string' ? agent.taskId : null,+ status: typeof agent?.status === 'string' ? agent.status : 'unknown',+ startedAt: timestamp(agent?.startedAt),+ completedAt: timestamp(agent?.completedAt),+ pid: Number.isSafeInteger(agent?.pid) && agent.pid > 0 && agent.pid <= 2_147_483_647 ? agent.pid : null,+ };+}++export async function up(client) {+ const { agents } = await readCosStateAgents();+ if (!agents) return;+ // An undefined entry cannot occur in parsed JSON; every entry is a record to keep.+ const rows = Object.entries(agents).map(([id, agent]) => ({ id, body: JSON.stringify(agent), ...rowColumns(agent) }));+ for (let start = 0; start < rows.length; start += BATCH) {+ const batch = rows.slice(start, start + BATCH);+ await client.query(+ `INSERT INTO cos_agents (agent_id, task_id, status, started_at, completed_at, pid, agent)+ SELECT u.agent_id, u.task_id, u.status, u.started_at, u.completed_at, u.pid, u.agent::json+ FROM unnest($1::text[], $2::text[], $3::text[], $4::timestamptz[], $5::timestamptz[], $6::int[], $7::text[])+ WITH ORDINALITY AS u(agent_id, task_id, status, started_at, completed_at, pid, agent, ord)+ ORDER BY u.ord+ ON CONFLICT (agent_id) DO UPDATE SET task_id = EXCLUDED.task_id, status = EXCLUDED.status,+ started_at = EXCLUDED.started_at, completed_at = EXCLUDED.completed_at, pid = EXCLUDED.pid, agent = EXCLUDED.agent`,+ [batch.map((r) => r.id), batch.map((r) => r.taskId), batch.map((r) => r.status), batch.map((r) => r.startedAt),+ batch.map((r) => r.completedAt), batch.map((r) => r.pid), batch.map((r) => r.body)],+ );+ }+ const { rows: stored } = await client.query(+ 'SELECT agent_id, agent::text AS body FROM cos_agents WHERE agent_id = ANY($1::text[])',+ [rows.map((r) => r.id)],+ );+ const byId = new Map(stored.map((row) => [row.agent_id, row.body]));+ const mismatched = rows.filter((r) => byId.get(r.id) !== r.body).length;+ if (mismatched > 0) {+ throw new Error(`CoS agent import verification failed: ${mismatched} of ${rows.length} records are missing or differ`);+ }+ await client.query(+ `INSERT INTO cos_agent_store (id, version) VALUES (1, 1)+ ON CONFLICT (id) DO UPDATE SET version = cos_agent_store.version + 1`,+ );+ console.log(`🗄️ CoS agents: imported ${rows.length} agent record(s) from state.json`);+}diff --git a/server/scripts/db-migrations/020-cos-agents-from-state.test.js b/server/scripts/db-migrations/020-cos-agents-from-state.test.jsnew file mode 100644index 0000000000..26d4a532d5--- /dev/null+++ b/server/scripts/db-migrations/020-cos-agents-from-state.test.js@@ -0,0 +1,108 @@+import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';+import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';++const paths = vi.hoisted(() => ({ root: '' }));+vi.mock('../../lib/paths.js', () => ({+ PATHS: { get root() { return paths.root; }, get data() { return `${paths.root}/data`; }, get cos() { return `${paths.root}/data/cos`; } },+}));++const { up: importUp } = await import('./020-cos-agents-from-state.js');+const { up: retireUp } = await import('./021-retire-state-agents.js');++const statePath = () => join(paths.root, 'data/cos/state.json');+const writeState = (value) => { mkdirSync(join(paths.root, 'data/cos'), { recursive: true }); writeFileSync(statePath(), typeof value === 'string' ? value : JSON.stringify(value)); };+const AGENTS = {+ 'agent-example-b': { id: 'agent-example-b', taskId: 'task-example-1', status: 'completed', startedAt: '2026-01-01T00:00:00.000Z', completedAt: '2026-01-01T01:00:00.000Z', pid: 4242, output: [{ line: 'done' }] },+ 'agent-example-a': { id: 'agent-example-a', status: 'running', metadata: { note: 'a }{ b' } },+};++// An in-memory stand-in for the two tables, enough to observe what the SQL asks for.+let rows;+let store;+const client = ({ corrupt = null } = {}) => ({+ query: vi.fn(async (sql, params) => {+ if (/INSERT INTO cos_agents/.test(sql)) {+ params[0].forEach((id, i) => rows.set(id, { status: params[2][i], pid: params[5][i], body: corrupt === id ? '{}' : params[6][i] }));+ return { rows: [], rowCount: params[0].length };+ }+ if (/SELECT agent_id, agent::text/.test(sql)) {+ return { rows: params[0].filter((id) => rows.has(id)).map((id) => ({ agent_id: id, body: rows.get(id).body })) };+ }+ if (/INSERT INTO cos_agent_store/.test(sql)) { store += 1; return { rows: [] }; }+ throw new Error(`unexpected query: ${sql}`);+ }),+});++beforeEach(() => {+ rmSync(paths.root, { recursive: true, force: true });+ paths.root = mkdtempSync(join(tmpdir(), 'cos-agent-import-'));+ rows = new Map();+ store = 0;+});+afterAll(() => rmSync(paths.root, { recursive: true, force: true }));++describe('db-migrations 020 + 021 — CoS agent records out of state.json', () => {+ it('imports every agent byte for byte in map order, then retires the key keeping a recovery copy', async () => {+ const original = JSON.stringify({ running: false, stats: { agentsSpawned: 2 }, agents: AGENTS }, null, 2);+ writeState(original);+ const db = client();+ await importUp(db);+ expect([...rows.keys()]).toEqual(['agent-example-b', 'agent-example-a']);+ for (const [id, agent] of Object.entries(AGENTS)) expect(rows.get(id).body).toBe(JSON.stringify(agent));+ expect(rows.get('agent-example-b')).toMatchObject({ status: 'completed', pid: 4242 });+ expect(store).toBe(1);+ // 020 never touches the file; only 021 does, after 020 committed.+ expect(readFileSync(statePath(), 'utf8')).toBe(original);++ await retireUp(db);+ expect(JSON.parse(readFileSync(statePath(), 'utf8'))).toEqual({ running: false, stats: { agentsSpawned: 2 } });+ expect(readFileSync(`${statePath()}.imported`, 'utf8')).toBe(original);++ // Nothing left to move: both are no-ops on a re-run.+ db.query.mockClear();+ await importUp(db);+ await retireUp(db);+ expect(db.query).not.toHaveBeenCalled();+ });++ it('aborts (throws) when a stored record differs from its source, so the boot stops', async () => {+ writeState({ agents: AGENTS });+ await expect(importUp(client({ corrupt: 'agent-example-a' }))).rejects.toThrow('verification failed: 1 of 2');+ expect(store).toBe(0);+ });++ it('refuses to retire the map while any record is missing from cos_agents', async () => {+ writeState({ agents: AGENTS });+ rows.set('agent-example-b', { body: JSON.stringify(AGENTS['agent-example-b']) });+ await expect(retireUp(client())).rejects.toThrow('1 of 2 state.json records are not stored');+ expect(JSON.parse(readFileSync(statePath(), 'utf8')).agents).toEqual(AGENTS);+ });++ it.each([+ ['no state file', null],+ ['unreadable state file', '{broken'],+ ['no agents map', { running: false }],+ ])('does nothing for %s', async (_label, value) => {+ if (value !== null) writeState(value);+ const db = client();+ await importUp(db);+ await retireUp(db);+ expect(db.query).not.toHaveBeenCalled();+ });++ it('retires a non-object agents value without importing it', async () => {+ writeState({ running: true, agents: ['not', 'records'] });+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});+ try {+ const db = client();+ await importUp(db);+ expect(db.query).not.toHaveBeenCalled();+ await retireUp(db);+ expect(JSON.parse(readFileSync(statePath(), 'utf8'))).toEqual({ running: true });+ } finally {+ warn.mockRestore();+ }+ });+});diff --git a/server/scripts/db-migrations/021-retire-state-agents.js b/server/scripts/db-migrations/021-retire-state-agents.jsnew file mode 100644index 0000000000..8b4adfba97--- /dev/null+++ b/server/scripts/db-migrations/021-retire-state-agents.js@@ -0,0 +1,38 @@+/**+ * Retire the `agents` map from `data/cos/state.json` (#12738).+ *+ * Runs after 020 has committed (migrations apply in order, each in its own+ * transaction). Before touching the file it re-verifies that every record in the+ * map is stored in `cos_agents` with identical bytes, and throws (stopping boot)+ * if not, so the map is only ever removed once its records are rows. The+ * original file is kept beside it as `state.json.imported` — the recovery copy,+ * which nothing reads — then state.json is rewritten without the key. With no+ * map left there is nothing to do, so a re-run (or a fresh install) is a no-op.+ */+import { existsSync } from 'node:fs';+import { copyFile } from 'node:fs/promises';+import { writeJsonAtomic } from '../../../scripts/migrations/_lib.js';+import { cosStatePath, readCosStateAgents } from './_cosStateAgents.js';++export async function up(client) {+ const { state, agents } = await readCosStateAgents();+ if (!state || !Object.hasOwn(state, 'agents')) return;+ const entries = Object.entries(agents ?? {});+ if (entries.length > 0) {+ const { rows } = await client.query(+ 'SELECT agent_id, agent::text AS body FROM cos_agents WHERE agent_id = ANY($1::text[])',+ [entries.map(([id]) => id)],+ );+ const byId = new Map(rows.map((row) => [row.agent_id, row.body]));+ const missing = entries.filter(([id, agent]) => byId.get(id) !== JSON.stringify(agent)).length;+ if (missing > 0) {+ throw new Error(`CoS agents: ${missing} of ${entries.length} state.json records are not stored in cos_agents; refusing to retire the map`);+ }+ }+ const path = cosStatePath();+ const aside = existsSync(`${path}.imported`) ? `${path}.imported-${Date.now()}` : `${path}.imported`;+ await copyFile(path, aside);+ const { agents: _retired, ...rest } = state;+ await writeJsonAtomic(path, rest);+ console.log(`🗄️ CoS agents: retired the agents map from state.json (${entries.length} record(s); original kept as ${aside.split('/').pop()})`);+}diff --git a/server/scripts/db-migrations/README.md b/server/scripts/db-migrations/README.mdindex f4572dc533..e6f2fbce13 100644--- a/server/scripts/db-migrations/README.md+++ b/server/scripts/db-migrations/README.md@@ -72,3 +72,9 @@ The CoS feedback reference migration is input-gated: it derives rows from theinstall's existing `state.json` and therefore has no `data.reference` seed. Aseed would be copied before the migration and could overwrite the meaning of auser's existing state.++The CoS agent migrations follow the same input-gated shape: 020 imports the+`agents` map from `data/cos/state.json` into `cos_agents` and verifies every+record byte for byte before committing; 021, which runs only after 020 has+committed, re-verifies, keeps the original file as `state.json.imported`, and+removes the key. Neither ships a seed.diff --git a/server/scripts/db-migrations/_cosStateAgents.js b/server/scripts/db-migrations/_cosStateAgents.jsnew file mode 100644index 0000000000..75d102a426--- /dev/null+++ b/server/scripts/db-migrations/_cosStateAgents.js@@ -0,0 +1,28 @@+/**+ * The agent records an install kept in `data/cos/state.json`'s `agents` map+ * before they moved into `cos_agents` (#12738). Shared by migrations 020+ * (import) and 021 (retire the key), which must read the same input.+ *+ * Returns `{ state, agents }` — `agents` is null when there is nothing to move:+ * no file, a file that is not a JSON object (the runtime quarantines and resets+ * those already), or no `agents` map in it. An `agents` value that is not a+ * plain object is reported the same way, with a warning, since the runtime+ * never read such a value as records either.+ */+import { join } from 'node:path';+import { PATHS } from '../../lib/paths.js';+import { readJsonOrNull } from '../../../scripts/migrations/_lib.js';++const isPlainObject = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);++export const cosStatePath = () => join(PATHS.cos, 'state.json');++export async function readCosStateAgents() {+ const state = await readJsonOrNull(cosStatePath());+ if (!isPlainObject(state) || !Object.hasOwn(state, 'agents')) return { state: isPlainObject(state) ? state : null, agents: null };+ if (!isPlainObject(state.agents)) {+ console.warn('⚠️ CoS agents: state.json carries a non-object agents value; nothing to import');+ return { state, agents: null };+ }+ return { state, agents: state.agents };+}diff --git a/server/scripts/init-db.sql b/server/scripts/init-db.sqlindex 9bea6536c0..79e1c8bc1e 100644--- a/server/scripts/init-db.sql+++ b/server/scripts/init-db.sql@@ -364,6 +364,28 @@ CREATE TABLE IF NOT EXISTS cos_task_lists (version BIGINT NOT NULL DEFAULT 0);+-- The CoS agent working set (running agents plus finished ones not yet archived+-- to data/cos/agents/<date>/), one row per agent. `agent` is the record's exact+-- JSON text (json, not jsonb, so bytes and key order round-trip); the other+-- columns are query projections and `seq` keeps insertion order.+-- cos_agent_store.version bumps on every write. Machine-local, never federated.+CREATE TABLE IF NOT EXISTS cos_agents (+ agent_id TEXT COLLATE "C" PRIMARY KEY,+ seq BIGINT GENERATED BY DEFAULT AS IDENTITY,+ task_id TEXT,+ status TEXT NOT NULL,+ started_at TIMESTAMPTZ,+ completed_at TIMESTAMPTZ,+ pid INTEGER,+ agent JSON NOT NULL+);+CREATE INDEX IF NOT EXISTS idx_cos_agents_seq ON cos_agents (seq);+CREATE INDEX IF NOT EXISTS idx_cos_agents_status ON cos_agents (status);+CREATE TABLE IF NOT EXISTS cos_agent_store (+ id SMALLINT PRIMARY KEY CHECK (id = 1),+ version BIGINT NOT NULL DEFAULT 0+);+-- Auto-update updated_at and sync_sequence on content/metadata changes.-- Skips bump for access-stat-only updates (access_count, last_accessed)-- to avoid sync noise from read operations.diff --git a/server/services/_cosAgentRepositoryContract.js b/server/services/_cosAgentRepositoryContract.jsnew file mode 100644index 0000000000..5c005f7e63--- /dev/null+++ b/server/services/_cosAgentRepositoryContract.js@@ -0,0 +1,94 @@+/**+ * The behavior every cos_agents backend must have, run by the in-memory suite+ * (cosAgentRepository.test.js) and the PostgreSQL suite (cosAgentRepository.db.test.js)+ * so the two cannot drift. Not a test file itself: it only registers cases.+ */+import { beforeEach, describe, expect, it } from 'vitest';+import { planAgentSave } from '../lib/cosAgentRows.js';++const id = (n) => `agent-example-${String(n).padStart(4, '0')}`;+const agent = (n, extra = {}) => ({+ id: id(n), taskId: `task-example-${n}`, status: 'running', startedAt: '2026-01-02T03:04:05.000Z',+ metadata: { app: 'example-app', note: 'a }{ b \u0000 ü' }, output: [], ...extra,+});++/** @param {{ name: string, makeBackend: () => Promise<object>|object }} options */+export function __runCosAgentRepositoryContract({ name, makeBackend }) {+ describe(`cos_agents contract (${name})`, () => {+ let backend;+ let persisted;+ let version;+ // Saves a whole map the way cosState does: plan against what was loaded, then apply.+ const save = async (next) => {+ const plan = planAgentSave({ persisted, next });+ version = await backend.apply(plan, version);+ persisted = plan.persisted;+ return plan;+ };+ const load = async () => {+ const loaded = await backend.loadAgents();+ return { version: loaded.version, agents: Object.fromEntries(loaded.rows.map((row) => [row.id, JSON.parse(row.body)])), rows: loaded.rows };+ };++ beforeEach(async () => {+ backend = await makeBackend();+ persisted = new Map();+ version = 0;+ });++ it('starts empty at version 0', async () => {+ expect(await backend.loadAgents()).toEqual({ version: 0, rows: [] });+ });++ it('round-trips every record byte for byte, in insertion order', async () => {+ const map = { [id(3)]: agent(3), [id(1)]: agent(1, { status: 'completed', completedAt: '2026-01-02T04:00:00.000Z', pid: 4242 }), [id(2)]: agent(2) };+ await save(map);+ const { rows } = await backend.loadAgents();+ expect(rows.map((row) => row.id)).toEqual([id(3), id(1), id(2)]);+ expect(rows.map((row) => row.body)).toEqual(Object.values(map).map((value) => JSON.stringify(value)));+ });++ it('writes only the changed row and deletes ids that left the map', async () => {+ const map = { [id(1)]: agent(1), [id(2)]: agent(2), [id(3)]: agent(3) };+ await save(map);+ const changed = { ...map, [id(2)]: { ...map[id(2)], status: 'completed', output: [{ line: 'done' }] } };+ delete changed[id(3)];+ const plan = await save(changed);+ expect(plan.upserts.map((row) => row.id)).toEqual([id(2)]);+ expect(plan.deletes).toEqual([id(3)]);+ const { agents } = await load();+ expect(agents).toEqual({ [id(1)]: map[id(1)], [id(2)]: changed[id(2)] });+ });++ it('bumps the version per write and refuses a write planned against a stale version', async () => {+ await save({ [id(1)]: agent(1) });+ expect((await backend.loadAgents()).version).toBe(1);+ const stalePlan = planAgentSave({ persisted: new Map(), next: {} });+ await expect(backend.apply({ ...stalePlan, deletes: [id(1)] }, 0)).rejects.toMatchObject({ code: 'COS_AGENTS_STALE', status: 409 });+ expect((await load()).agents).toEqual({ [id(1)]: agent(1) });+ expect((await backend.loadAgents()).version).toBe(1);+ });++ it('lets exactly one of two writes planned against the same version land', async () => {+ await save({ [id(1)]: agent(1) });+ const first = planAgentSave({ persisted, next: { [id(1)]: agent(1, { status: 'completed' }) } });+ const second = planAgentSave({ persisted, next: { [id(1)]: agent(1, { status: 'failed' }) } });+ const results = await Promise.allSettled([backend.apply(first, version), backend.apply(second, version)]);+ expect(results.filter((result) => result.status === 'fulfilled')).toHaveLength(1);+ expect(results.find((result) => result.status === 'rejected').reason.code).toBe('COS_AGENTS_STALE');+ });++ it('serves single records and bodiless statuses straight from the store', async () => {+ await save({ [id(1)]: agent(1), [id(2)]: agent(2, { status: 'paused' }) });+ expect(await backend.getAgent(id(2))).toEqual(agent(2, { status: 'paused' }));+ expect(await backend.getAgent(id(9))).toBeNull();+ expect(await backend.listStatuses()).toEqual([{ id: id(1), status: 'running' }, { id: id(2), status: 'paused' }]);+ });++ it('stores a malformed record without dropping it', async () => {+ await save({ [id(1)]: { status: 42 }, [id(2)]: null });+ expect((await load()).agents).toEqual({ [id(1)]: { status: 42 }, [id(2)]: null });+ expect(await backend.listStatuses()).toEqual([{ id: id(1), status: 'unknown' }, { id: id(2), status: 'unknown' }]);+ });+ });+}diff --git a/server/services/abandonedAgentRecovery.js b/server/services/abandonedAgentRecovery.jsindex 928ade5059..fa6955bd8f 100644--- a/server/services/abandonedAgentRecovery.js+++ b/server/services/abandonedAgentRecovery.js@@ -8,6 +8,7 @@ import { PATHS } from '../lib/paths.js';import { maintenance } from '../lib/maintenanceAdmission.js';import { execGit } from '../lib/execGit.js';import { getActiveAgentsFromRunner } from './cosRunnerClient.js';+import { getStoredAgent } from './cosAgentRepository.js';const inputSchema = z.object({agentId: z.string().regex(/^agent-[a-zA-Z0-9-]+$/), runId: z.string().uuid(),@@ -45,11 +46,13 @@ export async function reconcileAbandonedAgent(input, deps = {}) {refuse('The existing abandonment receipt does not match this request.');return receipt;};+ // Read before the synchronous gate transaction; the maintenance hold keeps+ // new agents from registering meanwhile. An unreadable store is not "absent".+ const ownership = await (deps.readAgent ?? getStoredAgent)(request.agentId)+ .then(agent => (agent === null ? 'absent' : 'present'), () => 'unreadable');let evidence;const validate = operation => {- const state = JSON.parse(fs.readFileSync(join(data, 'cos', 'state.json'), 'utf8'));- if (!state.agents || Array.isArray(state.agents) || typeof state.agents !== 'object'- || Object.hasOwn(state.agents, request.agentId)) refuse('Agent ownership is present or unreadable.');+ if (ownership !== 'absent') refuse('Agent ownership is present or unreadable.');if (!absent(workspace) || !absent(join(data, 'cos', 'agents', request.agentId)))refuse('Retained agent output or workspace needs recovery.');const agentsDirectory = join(data, 'cos', 'agents');diff --git a/server/services/abandonedAgentRecovery.test.js b/server/services/abandonedAgentRecovery.test.jsindex 563ae8a6b7..b8e75d1c9f 100644--- a/server/services/abandonedAgentRecovery.test.js+++ b/server/services/abandonedAgentRecovery.test.js@@ -13,7 +13,6 @@ beforeEach(() => {request = { agentId: 'agent-example', runId: randomUUID(), reason: 'Operator confirms duplicate was abandoned', confirmAbandoned: true };fs.mkdirSync(join(data, 'cos'), { recursive: true });fs.mkdirSync(join(data, 'runs', request.runId), { recursive: true });- save(join(data, 'cos', 'state.json'), { agents: {} });save(join(data, 'runs', request.runId, 'metadata.json'), { id: request.runId, agentId: request.agentId,taskId: 'example-task', workspacePath: join(data, 'cos', 'worktrees', request.agentId), endTime: null, success: null });fs.writeFileSync(join(data, 'runs', request.runId, 'output.txt'), '');@@ -24,7 +23,7 @@ beforeEach(() => {operation = gate.admit('agent', request.agentId);gate.admit('provider', 'other-work');hold = gate.begin({ owner: 'Operator', reason: 'Recovery' }).hold;- deps = { data, gate, isDead: () => true, runner: async () => [], git: async () => ({ stdout: '' }) };+ deps = { data, gate, isDead: () => true, runner: async () => [], git: async () => ({ stdout: '' }), readAgent: async () => null };});afterEach(() => fs.rmSync(data, { recursive: true, force: true }));it('records abandonment, preserves unrelated work and hold, and replays without claiming success', async () => {@@ -35,11 +34,12 @@ it('records abandonment, preserves unrelated work and hold, and replays withoutexpect(await reconcileAbandonedAgent(request, deps)).toMatchObject({ ...receipt, replayed: true });expect(JSON.parse(fs.readFileSync(join(data, 'runs', request.runId, 'metadata.json'))).success).toBeNull();});-it.each(['live pid', 'runner owner', 'bad runner', 'agent owner', 'saved output', 'workspace', 'branch', 'bad metadata', 'bad events', 'not user-killed', 'unsettled', 'runner reservation', 'archived output', 'later live handoff'])('preserves ownership for %s and leaves the journal usable', async problem => {+it.each(['live pid', 'runner owner', 'bad runner', 'agent owner', 'unreadable agent store', 'saved output', 'workspace', 'branch', 'bad metadata', 'bad events', 'not user-killed', 'unsettled', 'runner reservation', 'archived output', 'later live handoff'])('preserves ownership for %s and leaves the journal usable', async problem => {if (problem === 'live pid') deps.isDead = () => false;if (problem === 'runner owner') deps.runner = async () => [{ id: request.agentId }];if (problem === 'bad runner') deps.runner = async () => null;- if (problem === 'agent owner') save(join(data, 'cos', 'state.json'), { agents: { [request.agentId]: { status: 'paused' } } });+ if (problem === 'agent owner') deps.readAgent = async id => (id === request.agentId ? { id, status: 'paused' } : null);+ if (problem === 'unreadable agent store') deps.readAgent = async () => { throw new Error('database unavailable'); };if (problem === 'saved output') fs.writeFileSync(join(data, 'runs', request.runId, 'output.txt'), 'unsaved work');if (problem === 'workspace') fs.mkdirSync(join(data, 'cos', 'worktrees', request.agentId), { recursive: true });if (problem === 'branch') deps.git = async () => ({ stdout: 'preserved-branch' });diff --git a/server/services/autonomousJobs.test.js b/server/services/autonomousJobs.test.jsindex 7b859c8ff2..a5b098311e 100644--- a/server/services/autonomousJobs.test.js+++ b/server/services/autonomousJobs.test.js@@ -47,6 +47,7 @@ vi.mock('./worktreeManager.js', () => ({cleanupOrphanedWorktrees: vi.fn().mockResolvedValue(0)}))+vi.mock('./cosAgentRepository.js', () => ({ listStoredAgentStatuses: vi.fn(async () => []) }))vi.mock('./agentState.js', () => ({getActiveAgentIds: vi.fn().mockReturnValue([])}))@@ -76,6 +77,7 @@ import {initJobs} from './autonomousJobs.js'import { readJSONFile, atomicWrite } from '../lib/fileUtils.js'+import { listStoredAgentStatuses } from './cosAgentRepository.js'import { cosEvents } from './cosEvents.js'import { checkAndPrompt } from './autobiography.js'import { reapMergedWorktrees, cleanupOrphanedWorktrees } from './worktreeManager.js'@@ -1194,13 +1196,8 @@ describe('autonomousJobs', () => {})it('paused agent id is in the activeAgentIds set passed to reapMergedWorktrees', async () => {- // agentDataCleanup calls readJSONFile once — for data/cos/state.json- readJSONFile.mockResolvedValueOnce({- agents: {- 'paused-agent-1': { status: 'paused' },- 'running-agent-2': { status: 'running' }- }- })+ // agentDataCleanup reads the stored agent statuses once+ listStoredAgentStatuses.mockResolvedValueOnce([{ id: 'paused-agent-1', status: 'paused' }, { id: 'running-agent-2', status: 'running' }])await agentDataCleanup()@@ -1210,12 +1207,7 @@ describe('autonomousJobs', () => {})it('paused agent id is in the set passed to cleanupOrphanedWorktrees', async () => {- readJSONFile.mockResolvedValueOnce({- agents: {- 'paused-agent-1': { status: 'paused' },- 'running-agent-2': { status: 'running' }- }- })+ listStoredAgentStatuses.mockResolvedValueOnce([{ id: 'paused-agent-1', status: 'paused' }, { id: 'running-agent-2', status: 'running' }])await agentDataCleanup()@@ -1225,7 +1217,7 @@ describe('autonomousJobs', () => {})it('both reap passes see an agent dispatched after the job took its snapshot', async () => {- readJSONFile.mockResolvedValueOnce({ agents: { 'paused-agent-1': { status: 'paused' } } })+ listStoredAgentStatuses.mockResolvedValueOnce([{ id: 'paused-agent-1', status: 'paused' }])// Empty when the job starts; agent-x registers while the earlier passes run.getActiveAgentIds.mockReturnValueOnce([]).mockReturnValue(['agent-x'])@@ -1239,13 +1231,8 @@ describe('autonomousJobs', () => {}})- it('non-paused agent is NOT added to the protect set via state.json', async () => {- readJSONFile.mockResolvedValueOnce({- agents: {- 'paused-agent-1': { status: 'paused' },- 'running-agent-2': { status: 'running' }- }- })+ it('non-paused agent is NOT added to the protect set via the stored statuses', async () => {+ listStoredAgentStatuses.mockResolvedValueOnce([{ id: 'paused-agent-1', status: 'paused' }, { id: 'running-agent-2', status: 'running' }])await agentDataCleanup()@@ -1256,9 +1243,7 @@ describe('autonomousJobs', () => {it('in-memory active agents are also protected alongside paused agents', async () => {getActiveAgentIds.mockReturnValue(['live-agent-99'])- readJSONFile.mockResolvedValueOnce({- agents: { 'paused-agent-1': { status: 'paused' } }- })+ listStoredAgentStatuses.mockResolvedValueOnce([{ id: 'paused-agent-1', status: 'paused' }])await agentDataCleanup()@@ -1267,8 +1252,8 @@ describe('autonomousJobs', () => {expect(opts.activeAgentIds().has('live-agent-99')).toBe(true)})- it('handles missing cosState gracefully (null state.json)', async () => {- readJSONFile.mockResolvedValueOnce(null)+ it('handles an unreadable agent store gracefully', async () => {+ listStoredAgentStatuses.mockRejectedValueOnce(new Error('database unavailable'))await expect(agentDataCleanup()).resolves.not.toThrow()expect(reapMergedWorktrees).toHaveBeenCalledOnce()diff --git a/server/services/autonomousJobs/scriptHandlers.js b/server/services/autonomousJobs/scriptHandlers.jsindex 0ba7cedfb2..bb157e1942 100644--- a/server/services/autonomousJobs/scriptHandlers.js+++ b/server/services/autonomousJobs/scriptHandlers.js@@ -10,7 +10,7 @@ import { readdir, stat } from 'fs/promises'import { join, dirname } from 'path'import { fileURLToPath } from 'url'import { existsSync } from 'fs'-import { spawn } from '../../lib/childProcess.js';import { DAY, PATHS, readJSONFile, rmGuarded } from '../../lib/fileUtils.js'+import { spawn } from '../../lib/childProcess.js';import { DAY, PATHS, rmGuarded } from '../../lib/fileUtils.js'import { checkAndPrompt as autobiographyCheckAndPrompt } from '../autobiography.js'import { runGoalCheckIn } from '../goalCheckIn.js'import { cleanupOrphanedWorktrees, reapMergedWorktrees } from '../worktreeManager.js'@@ -91,10 +91,17 @@ async function agentDataCleanup() {// worktree + data dir as resume context, but a paused agent is removed from// the in-memory active/runner maps (so `getActiveAgentIds()` omits it) and —// critically — those maps are empty after a server restart while the agent's- // `status: 'paused'` survives in state.json. Read the persisted status so a- // paused agent's worktree/transcript is never reaped or rm'd out from under a- // later resume. (readJSONFile keeps this off the subAgentSpawner import path.)- const cosState = await readJSONFile(join(PATHS.cos, 'state.json'), null)+ // `status: 'paused'` survives in the stored record. Read the persisted status+ // (ids and statuses only, never the record bodies) so a paused agent's+ // worktree/transcript is never reaped or rm'd out from under a later resume.+ // Lazy, and the repository rather than cosState, to stay off the+ // subAgentSpawner import path.+ const storedStatuses = await import('../cosAgentRepository.js')+ .then(m => m.listStoredAgentStatuses())+ .catch((err) => {+ console.warn(`⚠️ Sentinel sweep could not read stored agent statuses: ${err.message}`)+ return null+ })// Every agent record that has not reached an outcome yet. A superset of// `activeIds`: it re-adds the paused agents the in-memory maps drop, and —// after a restart — the interrupted runs still recorded `running`, whose@@ -104,11 +111,9 @@ async function agentDataCleanup() {// record left behind by a dead process is precisely what they exist to collect.const unfinishedIds = new Set(activeIds)const pausedIds = new Set()- if (cosState?.agents && typeof cosState.agents === 'object') {- for (const [id, agent] of Object.entries(cosState.agents)) {- if (agent?.status === 'paused') pausedIds.add(id)- if (agent?.status !== 'completed' && agent?.status !== 'failed') unfinishedIds.add(id)- }+ for (const { id, status } of storedStatuses ?? []) {+ if (status === 'paused') pausedIds.add(id)+ if (status !== 'completed' && status !== 'failed') unfinishedIds.add(id)}for (const id of pausedIds) activeIds.add(id)// The worktree reaps run tens of seconds after the snapshot above, after the@@ -127,12 +132,12 @@ async function agentDataCleanup() {console.warn(`⚠️ Sentinel sweep could not list apps: ${err.message}`)return []})- // `null` when state.json could not be read: without it `unfinishedIds` is just+ // `null` when the stored statuses could not be read: without them `unfinishedIds` is just// the in-memory map, which is EMPTY after a restart — sweeping on that would// delete the resume signal of every paused and interrupted run.const sentinelsRemoved = await sweepStaleDoneSentinels([PATHS.root, ...apps.map(app => app?.repoPath)],- cosState?.agents ? unfinishedIds : null+ storedStatuses ? unfinishedIds : null).catch((err) => {console.warn(`⚠️ Stale sentinel sweep failed: ${err.message}`)return 0diff --git a/server/services/cosAgentRepository.db.test.js b/server/services/cosAgentRepository.db.test.jsnew file mode 100644index 0000000000..51b2120549--- /dev/null+++ b/server/services/cosAgentRepository.db.test.js@@ -0,0 +1,52 @@+/**+ * PostgreSQL backend of the CoS agent records, against the shared contract in+ * _cosAgentRepositoryContract.js. Runs via `npm run test:db` (→ `portos_test`)+ * ONLY; skips cleanly when no database is reachable.+ */+import { afterAll, describe, expect, it } from 'vitest';+import { checkHealth, close, ensureSchema, query, withTransaction } from '../lib/db.js';+import { requireDbOrSkip } from '../lib/dbTestGate.js';+import { planAgentSave } from '../lib/cosAgentRows.js';+import { __makePgCosAgentBackend } from './cosAgentRepository.js';+import { __runCosAgentRepositoryContract } from './_cosAgentRepositoryContract.js';++const health = await checkHealth().catch(() => ({ connected: false }));+if (health.connected) await ensureSchema();+const runDb = requireDbOrSkip('services/cosAgentRepository.db.test', health.connected, 'disposable PostgreSQL unavailable');++const reset = async () => {+ await query('DELETE FROM cos_agents');+ await query('DELETE FROM cos_agent_store');+};++afterAll(async () => {+ if (runDb) await reset();+ if (health.connected) await close();+});++describe.skipIf(!runDb)('cos_agents repository on PostgreSQL', () => {+ __runCosAgentRepositoryContract({+ name: 'postgres',+ makeBackend: async () => {+ await reset();+ return __makePgCosAgentBackend({ query, withTransaction });+ },+ });++ it('rewrites only the changed row: untouched rows keep their tuple version', async () => {+ await reset();+ const backend = __makePgCosAgentBackend({ query, withTransaction });+ const agents = Object.fromEntries(Array.from({ length: 5 }, (_, n) => [`agent-example-${n}`, { id: `agent-example-${n}`, status: 'running' }]));+ const first = planAgentSave({ persisted: new Map(), next: agents });+ const version = await backend.apply(first, 0);+ const xmins = async () => Object.fromEntries((await query('SELECT agent_id, xmin::text AS xmin FROM cos_agents')).rows.map((row) => [row.agent_id, row.xmin]));+ const before = await xmins();+ const second = planAgentSave({ persisted: first.persisted, next: { ...agents, 'agent-example-2': { id: 'agent-example-2', status: 'completed' } } });+ await backend.apply(second, version);+ const after = await xmins();+ const changed = Object.keys(after).filter((agentId) => after[agentId] !== before[agentId]);+ expect(changed).toEqual(['agent-example-2']);+ const { rows } = await query("SELECT status, task_id FROM cos_agents WHERE agent_id = 'agent-example-2'");+ expect(rows[0]).toEqual({ status: 'completed', task_id: null });+ });+});diff --git a/server/services/cosAgentRepository.js b/server/services/cosAgentRepository.jsnew file mode 100644index 0000000000..5077b605ca--- /dev/null+++ b/server/services/cosAgentRepository.js@@ -0,0 +1,154 @@+/**+ * Persistence for the CoS agent working set (`cos_agents`): one row per agent+ * record that used to live in `data/cos/state.json`'s `agents` map.+ *+ * PostgreSQL is authoritative. The in-memory backend exists only for the+ * documented development/test escape hatch (it persists to `data/cos-agents.json`+ * outside the test runner so a database-less dev run survives a restart).+ *+ * The working set is what state.json held: running agents plus finished ones+ * `archiveStaleAgents()` has not yet moved to `data/cos/agents/<date>/`. A write+ * carries only the changed rows (lib/cosAgentRows.js `planAgentSave`) and names+ * the store version it was planned against; a write planned against an older+ * version is refused with `COS_AGENTS_STALE`, so a stale cache (for example+ * after a database restore) can never delete or overwrite rows it did not see.+ */++import { join } from 'node:path';+import { atomicWrite, PATHS, readJSONFileStrict } from '../lib/fileUtils.js';+import { createPgFileFacade, resolvePgBackend } from '../lib/pgFileFacade.js';+import { isTestRunner } from '../lib/runtimeEnv.js';+import { agentRowColumns, planAgentSave } from '../lib/cosAgentRows.js';++// Bounds one statement's parameter payload (a record with its output is up to ~100 KB).+const UPSERT_BATCH = 100;+const agentsFile = () => join(PATHS.data, 'cos-agents.json');++const staleError = (expected, actual) => Object.assign(+ new Error(`CoS agent records changed underneath this write (planned against version ${expected}, store is at ${actual}). Read the current state before retrying.`),+ { code: 'COS_AGENTS_STALE', status: 409 },+);++function makePgBackend(db) {+ const { query, withTransaction } = db;+ return {+ name: 'postgres',+ async loadAgents() {+ // One statement, so the version and the rows come from the same snapshot.+ const { rows } = await query(+ `SELECT COALESCE((SELECT version FROM cos_agent_store WHERE id = 1), 0) AS version, a.agent_id, a.agent::text AS body+ FROM (SELECT 1) AS one LEFT JOIN cos_agents a ON true+ ORDER BY a.seq`,+ );+ return {+ version: Number(rows[0]?.version ?? 0),+ rows: rows.filter((row) => row.agent_id !== null).map((row) => ({ id: row.agent_id, body: row.body })),+ };+ },+ async apply({ upserts, deletes }, expectedVersion) {+ return withTransaction(async (client) => {+ await client.query('INSERT INTO cos_agent_store (id, version) VALUES (1, 0) ON CONFLICT (id) DO NOTHING');+ const { rows: locked } = await client.query('SELECT version FROM cos_agent_store WHERE id = 1 FOR UPDATE');+ const current = Number(locked[0].version);+ if (current !== expectedVersion) throw staleError(expectedVersion, current);+ if (deletes.length > 0) await client.query('DELETE FROM cos_agents WHERE agent_id = ANY($1::text[])', [deletes]);+ for (let start = 0; start < upserts.length; start += UPSERT_BATCH) {+ const batch = upserts.slice(start, start + UPSERT_BATCH).map(({ id, body, agent }) => ({ id, body, ...agentRowColumns(agent) }));+ await client.query(+ `INSERT INTO cos_agents (agent_id, task_id, status, started_at, completed_at, pid, agent)+ SELECT u.agent_id, u.task_id, u.status, u.started_at, u.completed_at, u.pid, u.agent::json+ FROM unnest($1::text[], $2::text[], $3::text[], $4::timestamptz[], $5::timestamptz[], $6::int[], $7::text[])+ AS u(agent_id, task_id, status, started_at, completed_at, pid, agent)+ ON CONFLICT (agent_id) DO UPDATE SET task_id = EXCLUDED.task_id, status = EXCLUDED.status,+ started_at = EXCLUDED.started_at, completed_at = EXCLUDED.completed_at, pid = EXCLUDED.pid, agent = EXCLUDED.agent`,+ [batch.map((r) => r.id), batch.map((r) => r.taskId), batch.map((r) => r.status), batch.map((r) => r.startedAt),+ batch.map((r) => r.completedAt), batch.map((r) => r.pid), batch.map((r) => r.body)],+ );+ }+ const { rows } = await client.query('UPDATE cos_agent_store SET version = version + 1 WHERE id = 1 RETURNING version');+ return Number(rows[0].version);+ });+ },+ async getAgent(agentId) {+ const { rows } = await query('SELECT agent::text AS body FROM cos_agents WHERE agent_id = $1', [agentId]);+ return rows[0] ? JSON.parse(rows[0].body) : null;+ },+ async listStatuses() {+ const { rows } = await query('SELECT agent_id, status FROM cos_agents ORDER BY seq');+ return rows.map((row) => ({ id: row.agent_id, status: row.status }));+ },+ };+}++async function makeMemoryBackend({ persist }) {+ // id -> exact JSON text, in insertion order (a Map keeps it, like `seq`).+ const bodies = new Map();+ let version = 0;+ if (persist) {+ const { ok, value: saved } = await readJSONFileStrict(agentsFile(), null, { logError: false });+ // Never start empty over an unreadable file: the next write would replace it.+ if (!ok) throw new Error(`Unreadable ${agentsFile()} — repair or remove it before starting the dev file backend`);+ for (const row of saved?.rows ?? []) bodies.set(row.id, row.body);+ version = saved?.version ?? 0;+ }+ const flush = () => (persist ? atomicWrite(agentsFile(), { version, rows: [...bodies].map(([id, body]) => ({ id, body })) }) : Promise.resolve());++ return {+ name: 'memory',+ async loadAgents() { return { version, rows: [...bodies].map(([id, body]) => ({ id, body })) }; },+ async apply({ upserts, deletes }, expectedVersion) {+ if (version !== expectedVersion) throw staleError(expectedVersion, version);+ for (const id of deletes) bodies.delete(id);+ for (const { id, body } of upserts) bodies.set(id, body);+ version += 1;+ await flush();+ return version;+ },+ async getAgent(agentId) { return bodies.has(agentId) ? JSON.parse(bodies.get(agentId)) : null; },+ async listStatuses() {+ return [...bodies].map(([id, body]) => ({ id, status: agentRowColumns(JSON.parse(body)).status }));+ },+ };+}++const backendFacade = createPgFileFacade({+ makeFile: () => makeMemoryBackend({ persist: !isTestRunner() }),+ makePg: () => resolvePgBackend({+ requirement: 'The CoS agent records require PostgreSQL — run `npm run setup:db` (dev/test only: set MEMORY_BACKEND=file for the file escape hatch)',+ loadDb: () => import('../lib/db.js'),+ makePg: makePgBackend,+ }),+});+const backend = async () => backendFacade.getBackend();++/** `{ version, rows: [{ id, body }] }` — the working set in insertion order, each record as its stored JSON text. */+export async function loadAgentRows() { return (await backend()).loadAgents(); }++/**+ * Persist a save plan (`planAgentSave`'s upserts/deletes) in one transaction,+ * provided the store is still at `expectedVersion`; returns the new version.+ * Throws `COS_AGENTS_STALE` (status 409) otherwise.+ */+export async function applyAgentSave(plan, expectedVersion) { return (await backend()).apply(plan, expectedVersion); }++/** One stored agent record, or null. Reads the store, never a cache. */+export async function getStoredAgent(agentId) { return (await backend()).getAgent(agentId); }++/** `[{ id, status }]` for every stored agent, without record bodies. */+export async function listStoredAgentStatuses() { return (await backend()).listStatuses(); }++/** Test seam: replace the stored working set with `agents` (an id → record map). */+export async function __seedCosAgents(agents) {+ const selected = await backend();+ const { version, rows } = await selected.loadAgents();+ const plan = planAgentSave({ persisted: new Map(rows.map((row) => [row.id, row.body])), next: agents });+ return selected.apply(plan, version);+}++/** Test seam: drop the selected backend (and, for the in-memory one, everything in it). */+export function __resetCosAgentRepository() { backendFacade.reset(); }++/** Test seam: the PostgreSQL backend over a `{ query, withTransaction }` handle, for the DB suite. */+export const __makePgCosAgentBackend = makePgBackend;+/** Test seam: a fresh in-memory backend; it touches disk only with `persist: true`. */+export const __makeMemoryCosAgentBackend = ({ persist = false } = {}) => makeMemoryBackend({ persist });diff --git a/server/services/cosAgentRepository.test.js b/server/services/cosAgentRepository.test.jsnew file mode 100644index 0000000000..ac882be5ea--- /dev/null+++ b/server/services/cosAgentRepository.test.js@@ -0,0 +1,18 @@+import { describe, expect, it } from 'vitest';+import { __makeMemoryCosAgentBackend, __resetCosAgentRepository, applyAgentSave, getStoredAgent, listStoredAgentStatuses, loadAgentRows } from './cosAgentRepository.js';+import { __runCosAgentRepositoryContract } from './_cosAgentRepositoryContract.js';++__runCosAgentRepositoryContract({ name: 'in-memory', makeBackend: () => __makeMemoryCosAgentBackend() });++describe('cosAgentRepository facade (test runner)', () => {+ it('selects the in-memory backend and does not touch disk', async () => {+ __resetCosAgentRepository();+ const agent = { id: 'agent-example-0001', status: 'running' };+ await applyAgentSave({ upserts: [{ id: agent.id, body: JSON.stringify(agent), agent }], deletes: [] }, 0);+ expect(await getStoredAgent(agent.id)).toEqual(agent);+ expect(await listStoredAgentStatuses()).toEqual([{ id: agent.id, status: 'running' }]);+ expect((await loadAgentRows()).version).toBe(1);+ __resetCosAgentRepository();+ expect(await loadAgentRows()).toEqual({ version: 0, rows: [] });+ });+});diff --git a/server/services/cosMergeAdmission.js b/server/services/cosMergeAdmission.jsindex f6e89a6344..9e2a1e4e71 100644--- a/server/services/cosMergeAdmission.js+++ b/server/services/cosMergeAdmission.js@@ -57,9 +57,17 @@ export function __setMergeAdmissionClockForTests(next) {const REPORTED_PHASES = ['queue', 'trustedState', 'stateLoad', 'origin', 'ownerRecovery', 'persistence'];const REPORTED_STAGES = [['trustedState', 'read'], ['trustedState', 'parse'], ['persistence', 'prepare'],- ['persistence', 'serialize'], ['persistence', 'write'], ['persistence', 'replace'], ['persistence', 'dirSync']];+ ['persistence', 'agents'], ['persistence', 'serialize'], ['persistence', 'write'], ['persistence', 'replace'], ['persistence', 'dirSync']];const REPORTED_LAG_PHASES = ['queue', 'trustedState', 'persistence'];+// Per record, not per serialized map: the stored rows come back in insertion+// order, which a map that deleted and re-added an id does not keep.+function sameAgentRecords(cached, stored) {+ const ids = Object.keys(cached ?? {});+ return isPlainObject(cached) && ids.length === Object.keys(stored).length+ && ids.every((id) => Object.hasOwn(stored, id) && JSON.stringify(cached[id]) === JSON.stringify(stored[id]));+}+async function transaction(work, operation) {// Lazy: almost every task-generator suite reaches prWatcher, even when no// pending merge exists. Admission needs runtime state only on the merge path.@@ -112,7 +120,7 @@ async function transaction(work, operation) {const state = await measure('stateLoad', loadState);// A recovered/defaulted or externally replaced state is not proof of an// empty ownership set. Do not let the cache overwrite contradictory disk.- if (JSON.stringify(state.agents) !== JSON.stringify(snapshot.agents)+ if (!sameAgentRecords(state.agents, snapshot.agents)|| JSON.stringify(state.mergeAdmissions ?? {}) !== JSON.stringify(snapshot.mergeAdmissions)) {return refuse('ownership-stale');}diff --git a/server/services/cosRepositoryDevPersistence.test.js b/server/services/cosRepositoryDevPersistence.test.jsnew file mode 100644index 0000000000..1d0fd57dbd--- /dev/null+++ b/server/services/cosRepositoryDevPersistence.test.js@@ -0,0 +1,40 @@+// The dev file escape hatch persists both CoS repositories to data/; a restart+// must reload what was written, and an unreadable file must stop the backend+// rather than start empty and overwrite it.+import { afterAll, describe, expect, it, vi } from 'vitest';+import { writeFileSync } from 'node:fs';+import { join } from 'node:path';+import { mockPathsDataRoot } from '../lib/mockPathsDataRoot.js';++const { tempRoot, makeProxy, cleanup } = mockPathsDataRoot({ prefix: 'cos-repo-dev-' });+vi.mock('../lib/fileUtils.js', async (importOriginal) => makeProxy(await importOriginal()));+afterAll(cleanup);++const { __makeMemoryCosAgentBackend } = await import('./cosAgentRepository.js');+const { __makeMemoryCosTaskBackend } = await import('./cosTaskRepository.js');++describe('CoS repositories on the dev file backend', () => {+ it('reload agent rows written before a restart', async () => {+ const first = await __makeMemoryCosAgentBackend({ persist: true });+ const agent = { id: 'agent-example-1', status: 'running' };+ await first.apply({ upserts: [{ id: agent.id, body: JSON.stringify(agent), agent }], deletes: [] }, 0);+ const restarted = await __makeMemoryCosAgentBackend({ persist: true });+ expect(await restarted.loadAgents()).toEqual({ version: 1, rows: [{ id: agent.id, body: JSON.stringify(agent) }] });+ });++ it('reload task rows written before a restart', async () => {+ const first = await __makeMemoryCosTaskBackend({ persist: true });+ const task = { id: 'task-example-1', status: 'pending', priority: 'MEDIUM', priorityValue: 2, description: 'Example', metadata: {} };+ await first.apply('user', { upserts: [{ task, position: 0 }], deletes: [] });+ const restarted = await __makeMemoryCosTaskBackend({ persist: true });+ expect((await restarted.loadWorkingSet('user')).rows.map((row) => row.task.id)).toEqual([task.id]);+ expect(await restarted.readVersion('user')).toBe(1);+ });++ it('refuses to start over an unreadable file', async () => {+ writeFileSync(join(tempRoot, 'cos-agents.json'), '{broken');+ writeFileSync(join(tempRoot, 'cos-tasks.json'), '{broken');+ await expect(__makeMemoryCosAgentBackend({ persist: true })).rejects.toThrow('Unreadable');+ await expect(__makeMemoryCosTaskBackend({ persist: true })).rejects.toThrow('Unreadable');+ });+});diff --git a/server/services/cosState.failedWrite.test.js b/server/services/cosState.failedWrite.test.jsindex afa2348b76..03f6e6d2cc 100644--- a/server/services/cosState.failedWrite.test.js+++ b/server/services/cosState.failedWrite.test.js@@ -7,6 +7,11 @@ const { tempRoot, makeProxy, spies, cleanup } = mockPathsDataRoot({prefix: 'cos-failed-write-', wrapExports: ['atomicWrite', 'ensureDirs', 'readJSONFileStrict'], makeSpy: vi.fn,});vi.mock('../lib/fileUtils.js', async importOriginal => makeProxy(await importOriginal()));+// Agent records are cos_agents rows (#12738): their write is the injectable failure point.+vi.mock('./cosAgentRepository.js', async importOriginal => {+ const real = await importOriginal();+ return { ...real, applyAgentSave: vi.fn(real.applyAgentSave) };+});const cosDir = join(tempRoot, 'cos');const statePath = join(cosDir, 'state.json');@@ -16,15 +21,23 @@ const writeJson = (path, value) => writeFileSync(path, JSON.stringify(value));const readJson = path => JSON.parse(readFileSync(path, 'utf8'));const writeFailure = () => Object.assign(new Error('Synthetic persistence failure'), { code: 'EIO' });let store;+let repo;let realWrite;+let realApply;+const storedAgents = async () => Object.fromEntries((await repo.loadAgentRows()).rows.map(row => [row.id, JSON.parse(row.body)]));beforeEach(async () => {vi.resetModules();rmSync(cosDir, { force: true, recursive: true });mkdirSync(cosDir, { recursive: true });writeJson(configPath, { maxConcurrentAgents: 3 });- writeJson(statePath, { agents: { example: { id: 'example', status: 'running', metadata: { claimBranches: [] } } } });+ writeJson(statePath, {});store = await import('./cosState.js');+ repo = await import('./cosAgentRepository.js');+ repo.__resetCosAgentRepository();+ realApply ??= repo.applyAgentSave.getMockImplementation();+ repo.applyAgentSave.mockReset().mockImplementation(realApply);+ await repo.__seedCosAgents({ example: { id: 'example', status: 'running', metadata: { claimBranches: [] } } });const actual = await vi.importActual('../lib/fileUtils.js');realWrite = actual.atomicWrite;for (const [name, spy] of Object.entries(spies)) spy.mockReset().mockImplementation(actual[name]);@@ -42,9 +55,9 @@ async function saveHealthCheck() {describe('CoS failed persistence recovery', () => {it.each(['bind', 'release'])('does not carry a rejected %s into the next queued write or a stale retry', async action => {const durable = action === 'bind' ? [] : [binding];- writeJson(statePath, { agents: { example: { status: 'running', metadata: { claimBranches: durable } } } });+ await repo.__seedCosAgents({ example: { status: 'running', metadata: { claimBranches: durable } } });let rejectedState;- spies.atomicWrite.mockRejectedValueOnce(writeFailure());+ repo.applyAgentSave.mockRejectedValueOnce(writeFailure());await expect(store.withStateLock(async () => {const state = await store.loadState();rejectedState = { ...state, agents: { ...state.agents, example: {@@ -52,19 +65,19 @@ describe('CoS failed persistence recovery', () => {} } };await store.saveState(rejectedState);})).rejects.toMatchObject({ code: 'EIO' });- expect(readJson(statePath).agents.example.metadata.claimBranches).toEqual(durable);+ expect((await storedAgents()).example.metadata.claimBranches).toEqual(durable);await saveHealthCheck();- expect(readJson(statePath).agents.example.metadata.claimBranches).toEqual(durable);+ expect((await storedAgents()).example.metadata.claimBranches).toEqual(durable);expect(readJson(statePath).lastHealthCheck).toBe('2026-01-01T00:00:00.000Z');await expect(store.saveState({ ...rejectedState })).rejects.toMatchObject({ code: 'COS_PERSISTENCE_STALE_WRITE' });});it('discards an existing agent lifecycle caller\'s in-place mutation after rejection', async () => {const { updateAgent } = await import('./cosAgentLifecycle.js');- spies.atomicWrite.mockRejectedValueOnce(writeFailure());+ repo.applyAgentSave.mockRejectedValueOnce(writeFailure());await expect(updateAgent('example', { metadata: { claimBranches: [binding] } })).rejects.toMatchObject({ code: 'EIO' });await updateAgent('example', { status: 'paused' });- expect(readJson(statePath).agents.example).toMatchObject({ status: 'paused', metadata: { claimBranches: [] } });+ expect((await storedAgents()).example).toMatchObject({ status: 'paused', metadata: { claimBranches: [] } });});it('reloads rejected in-place config changes and re-anchors a live runtime read independently', async () => {@@ -131,9 +144,26 @@ describe('CoS failed persistence recovery', () => {expect(readJson(path).repaired).toBe(true);});+ it('reads the stored outcome when an agent row write commits before reporting failure', async () => {+ const value = await store.loadState();+ value.agents.example = { ...value.agents.example, status: 'completed' };+ repo.applyAgentSave.mockImplementationOnce(async (...args) => {+ await realApply(...args);+ throw writeFailure();+ });+ await expect(store.withStateLock(() => store.saveState(value))).rejects.toMatchObject({ code: 'EIO' });+ await store.withStateLock(async () => {+ const current = await store.loadState();+ expect(current.agents.example.status).toBe('completed');+ current.agents.example.metadata = { claimBranches: [binding] };+ await store.saveState(current);+ });+ expect((await storedAgents()).example).toMatchObject({ status: 'completed', metadata: { claimBranches: [binding] } });+ });+it('retries a rejected legacy config split before runtime writes can remove the legacy settings', async () => {rmSync(configPath);- writeJson(statePath, { agents: {}, config: { maxConcurrentAgents: 9 } });+ writeJson(statePath, { config: { maxConcurrentAgents: 9 } });spies.atomicWrite.mockRejectedValueOnce(writeFailure());await expect(store.loadState()).rejects.toMatchObject({ code: 'EIO' });expect(readJson(statePath).config.maxConcurrentAgents).toBe(9);@@ -144,7 +174,7 @@ describe('CoS failed persistence recovery', () => {it('shares a pending legacy split so a late loader cannot overwrite the next config update', async () => {rmSync(configPath);- writeJson(statePath, { agents: {}, config: { maxConcurrentAgents: 9 } });+ writeJson(statePath, { config: { maxConcurrentAgents: 9 } });spies.atomicWrite.mockRejectedValueOnce(writeFailure());await expect(store.saveConfig({ maxConcurrentAgents: 9 })).rejects.toMatchObject({ code: 'EIO' });// Keep the interleaving deterministic: only the first write waits; all@@ -184,7 +214,7 @@ describe('CoS failed persistence recovery', () => {let rejectWrite;let signalStarted;const started = new Promise(resolve => { signalStarted = resolve; });- spies.atomicWrite.mockImplementationOnce(() => new Promise((resolve, reject) => {+ repo.applyAgentSave.mockImplementationOnce(() => new Promise((resolve, reject) => {rejectWrite = reject;signalStarted();}));@@ -203,7 +233,7 @@ describe('CoS failed persistence recovery', () => {rejectWrite(writeFailure());await failureAssertion;await next;- expect(readJson(statePath).agents.example.metadata.claimBranches).toEqual([]);+ expect((await storedAgents()).example.metadata.claimBranches).toEqual([]);expect((await store.loadState()).config.maxConcurrentAgents).toBe(5);});});diff --git a/server/services/cosState.js b/server/services/cosState.jsindex 176894bb8e..d0a044ec5e 100644--- a/server/services/cosState.js+++ b/server/services/cosState.js@@ -5,7 +5,7 @@ import { normalizePersistentMindMaintainer } from '../lib/persistentMindMaintain* Shared state management for Chief of Staff services.*/-import { readFile, readdir, rm, rename } from 'fs/promises';+import { readFile, readdir, rm, rename, stat } from 'fs/promises';import { existsSync } from 'fs';import { join, dirname, basename } from 'path';import { createFileWriteQueue } from '../lib/fileWriteQueue.js';@@ -24,6 +24,8 @@ import {normalizePersistentMindThinkingPresets,} from '../lib/persistentMindThinkingPresets.js';import { cosEvents } from './cosEvents.js';+import { planAgentSave } from '../lib/cosAgentRows.js';+import { applyAgentSave, loadAgentRows } from './cosAgentRepository.js';import { ServerError } from '../lib/errorHandler.js';import { DEFAULT_ALWAYS_APPROVE_KINDS } from './taskLearning/safetyKind.js';@@ -449,6 +451,51 @@ export async function saveConfig(config) {// In-memory state cache — avoids re-reading state.json from disk on every call.// All mutations go through withStateLock, so the cache stays consistent.let stateCache = null;+// What `cos_agents` holds for the cached `state.agents` (#12738): the store+// version and each record's stored JSON text, so a save writes only the rows+// whose serialization changed. `lastStatePayload` is the last state.json body+// written, so an agent-only save does not rewrite the file at all.+let agentStore = null;+let lastStatePayload = null;+let warnedLegacyAgents = false;++function dropRuntimeCaches() {+ stateCache = null;+ agentStore = null;+ lastStatePayload = null;+}++async function loadAgentStore() {+ const { version, rows } = await loadAgentRows();+ const persisted = new Map(rows.map((row) => [row.id, row.body]));+ const agents = Object.fromEntries(rows.map((row) => [row.id, JSON.parse(row.body)]));+ return { store: { version, persisted }, agents };+}++// atomicWrite's temp name is `<file>.<pid>.<ms>.<uuid>.tmp`; a process killed+// mid-write leaves one behind. Sweep once per process, never this process's own+// (it may be mid-write) and never one younger than a minute.+const STATE_TEMP = /^state\.json\.(\d+)\.(\d+)\.[0-9a-f-]{36}\.tmp$/;+const STATE_TEMP_MIN_AGE_MS = 60_000;+let stateTempSweep = null;+function sweepStaleStateTempFiles() {+ stateTempSweep ??= (async () => {+ const now = Date.now();+ const names = await readdir(PATHS.cos).catch(() => []);+ let removed = 0;+ for (const name of names) {+ const match = STATE_TEMP.exec(name);+ if (!match || Number(match[1]) === process.pid) continue;+ const path = join(PATHS.cos, name);+ const info = await stat(path).catch(() => null);+ if (!info?.isFile() || now - info.mtimeMs < STATE_TEMP_MIN_AGE_MS) continue;+ if (await rm(path).then(() => true, () => false)) removed += 1;+ }+ if (removed > 0) console.log(`🧹 Removed ${removed} stale CoS state temp file(s) left by interrupted writes`);+ return removed;+ })();+ return stateTempSweep;+}// Master "Improve" flag with backward compat for the legacy split self/app flags.// Falls through only when improvementEnabled is null/undefined — explicit `false` wins.@@ -486,6 +533,7 @@ export async function loadState() {const generation = stateRecovery.generation;const restored = restoreGeneration;await ensureDirectories();+ await sweepStaleStateTempFiles();let state;if (stateRecovery.required) {state = await readRecoveryFile(STATE_FILE);@@ -493,9 +541,19 @@ export async function loadState() {const content = await readFile(STATE_FILE, 'utf-8');state = parseStateFile(content) ?? await recoverFromUnreadableState(content);}- // A concurrent read/write or restore may have superseded this disk read.+ const { store, agents } = await loadAgentStore();+ // A concurrent read/write or restore may have superseded this read.if (stateCache || generation !== stateRecovery.generation || restored !== restoreGeneration) return loadState();+ // Agent records live in cos_agents (db-migrations 020/021 moved them). A+ // state.json that still carries the map (a restored pre-migration file) is not+ // read: the database is the only source, and the next save drops the key.+ if (state && Object.hasOwn(state, 'agents') && !warnedLegacyAgents) {+ warnedLegacyAgents = true;+ console.warn(`⚠️ ${STATE_FILE} carries a legacy agents map; ignoring it — agent records are stored in cos_agents`);+ }+ agentStore = store;+ lastStatePayload = null;stateCache = state ? {...DEFAULT_STATE,...state,@@ -503,8 +561,8 @@ export async function loadState() {config,stats: { ...DEFAULT_STATE.stats, ...state.stats },persistentMind: normalizePersistentMindState(state.persistentMind),- agents: state.agents ?? {}- } : Object.assign(structuredClone(DEFAULT_STATE), { config });+ agents+ } : Object.assign(structuredClone(DEFAULT_STATE), { config, agents });stateRecovery.required = false;return stampGeneration(stateCache, stateRecovery);}@@ -526,12 +584,29 @@ async function readStateForSafetyCheck({ onStage } = {}) {return { trusted: true, state };}+// The stored agent records, read from cos_agents rather than the cache. A store+// that cannot be read is `trusted: false`, never an empty map.+async function readStoredAgentsForSafetyCheck({ onStage } = {}) {+ onStage?.('agents');+ try {+ return { trusted: true, agents: (await loadAgentStore()).agents };+ } catch (error) {+ console.warn(`⚠️ CoS agent records could not be read for a safety check: ${error.message}`);+ return { trusted: false, agents: null };+ }+}+// Merge admission shares the runtime ownership store and its write queue.// Missing leases are legacy empty state; malformed leases are never defaulted.-// `onStage('read'|'parse')` marks each stage for slow-admission diagnostics.+// `onStage('read'|'parse'|'agents')` marks each stage for slow-admission diagnostics.export async function readMergeAdmissionStateForSafetyCheck({ onStage } = {}) {const { trusted, state } = await readStateForSafetyCheck({ onStage });- return { trusted, agents: state?.agents ?? null, mergeAdmissions: state && Object.hasOwn(state, 'mergeAdmissions') ? state.mergeAdmissions : {} };+ const stored = await readStoredAgentsForSafetyCheck({ onStage });+ return {+ trusted: trusted && stored.trusted,+ agents: stored.agents,+ mergeAdmissions: state && Object.hasOwn(state, 'mergeAdmissions') ? state.mergeAdmissions : {},+ };}// The Persistent Mind slice, for the update route's image-work gate.@@ -545,12 +620,7 @@ export async function readPersistentMindStateForSafetyCheck() {// must read as "an agent may be running", not as "no agents are running" —// getting that backwards restarts PortOS out from under a live agent.export async function readAgentsStateForSafetyCheck() {- const { trusted, state } = await readStateForSafetyCheck();- const agents = state?.agents;- return {- trusted,- agents: trusted && isPlainObject(agents) ? agents : null,- };+ return readStoredAgentsForSafetyCheck();}/**@@ -578,23 +648,37 @@ export async function saveState(state, { onStage } = {}) {try {await ensureDirectories();state.config = await loadConfig();- // Config lives in its own file; never re-serialize it onto the hot path.+ agentStore ??= (await loadAgentStore()).store;+ // Agent records are rows: write only those whose serialization changed, so+ // one agent's update never rewrites another's (#12738).+ stage('agents');+ const plan = planAgentSave({ persisted: agentStore.persisted, next: isPlainObject(state.agents) ? state.agents : {} });+ if (plan.upserts.length > 0 || plan.deletes.length > 0) {+ agentStore = { version: await applyAgentSave(plan, agentStore.version), persisted: plan.persisted };+ bytes += plan.upserts.reduce((sum, row) => sum + Buffer.byteLength(row.body), 0);+ }+ // Config lives in its own file and agents in cos_agents; never re-serialize+ // either onto the hot path. An unchanged remainder is not rewritten.const persisted = { ...state };delete persisted.config;+ delete persisted.agents;stage('serialize');const payload = JSON.stringify(persisted, null, 2);- bytes = Buffer.byteLength(payload);- await atomicWrite(STATE_FILE, payload, { onStage: stage });+ if (payload !== lastStatePayload) {+ bytes += Buffer.byteLength(payload);+ await atomicWrite(STATE_FILE, payload, { onStage: stage });+ lastStatePayload = payload;+ }outcome = 'saved';} catch (error) {- stateCache = null;+ dropRuntimeCaches();invalidateFailedWrite(stateRecovery);throw error;} finally {const summary = span.end();if (summary.totalMs >= SLOW_STATE_OPERATION_MS) {const ms = (name) => summary.phases[name]?.ms ?? 0;- console.warn(`🐢 CoS state write slow outcome=${outcome} totalMs=${summary.totalMs} prepareMs=${ms('prepare')} serializeMs=${ms('serialize')} writeMs=${ms('write')} replaceMs=${ms('replace')} dirSyncMs=${ms('dirSync')} loopLagMs=${summary.lagMs} loopLagMaxMs=${summary.lagMaxMs} sizeKiB=${Math.round(bytes / 1024)}`);+ console.warn(`🐢 CoS state write slow outcome=${outcome} totalMs=${summary.totalMs} prepareMs=${ms('prepare')} agentsMs=${ms('agents')} serializeMs=${ms('serialize')} writeMs=${ms('write')} replaceMs=${ms('replace')} dirSyncMs=${ms('dirSync')} loopLagMs=${summary.lagMs} loopLagMaxMs=${summary.lagMaxMs} sizeKiB=${Math.round(bytes / 1024)}`);}}stateCache = stampGeneration(state, stateRecovery);@@ -633,11 +717,12 @@ async function supersedeConfigForRestoredLegacySlice(snapshotLegacyConfig) {export function withLiveCosRestore(transfer, { snapshotLegacyConfig = null } = {}) {return withConfigLock(() => withStateLock(async () => {const persisted = await readStateForSafetyCheck();+ const stored = await readStoredAgentsForSafetyCheck();const states = [stateCache, persisted.state].filter(Boolean);- const unsafe = !persisted.trusted || isDaemonRunning() || states.some(state =>- state.running || state.persistentMind?.started || state.persistentMind?.activeTurn- || (state.agents != null && !isPlainObject(state.agents))- || Object.values(state.agents ?? {}).some(agent =>+ const agentMaps = [stateCache?.agents, stored.agents].filter((agents) => agents != null);+ const unsafe = !persisted.trusted || !stored.trusted || isDaemonRunning() || states.some(state =>+ state.running || state.persistentMind?.started || state.persistentMind?.activeTurn)+ || agentMaps.some(agents => !isPlainObject(agents) || Object.values(agents).some(agent =>!['completed', 'failed', 'cancelled'].includes(agent?.status)));if (unsafe) {throw new ServerError('Stop the CoS daemon and Persistent Mind, and finish or stop active agents before restoring CoS files. Repair unreadable CoS state before retrying.', {@@ -648,7 +733,7 @@ export function withLiveCosRestore(transfer, { snapshotLegacyConfig = null } = {const [result] = await Promise.allSettled([Promise.resolve().then(transfer)]);restoreGeneration += 1;configCache = null;- stateCache = null;+ dropRuntimeCaches();const [reload] = await Promise.allSettled([(async () => {if (snapshotLegacyConfig && result.status === 'fulfilled') await supersedeConfigForRestoredLegacySlice(snapshotLegacyConfig);const { reloadAgentIndexAfterRestore } = await import('./cosAgentIndex.js');diff --git a/server/services/cosState.test.js b/server/services/cosState.test.jsindex 1060c2b8de..7ced40dd5b 100644--- a/server/services/cosState.test.js+++ b/server/services/cosState.test.js@@ -1,5 +1,5 @@import { describe, it, expect, beforeEach, afterAll, vi } from 'vitest';-import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'fs';+import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, utimesSync, writeFileSync } from 'fs';import { join } from 'path';import { mockPathsDataRoot } from '../lib/mockPathsDataRoot.js';import { __createManualClock } from '../lib/eventLoopLagProbe.js';@@ -7,6 +7,11 @@ import { __createManualClock } from '../lib/eventLoopLagProbe.js';const { tempRoot, makeProxy, cleanup } = mockPathsDataRoot({ prefix: 'cos-state-test-' });vi.mock('../lib/fileUtils.js', async (importOriginal) => makeProxy(await importOriginal()));+// Observe the row writes cosState sends, against the real in-memory backend.+vi.mock('./cosAgentRepository.js', async (importOriginal) => {+ const real = await importOriginal();+ return { ...real, applyAgentSave: vi.fn(real.applyAgentSave) };+});const COS_DIR = join(tempRoot, 'cos');const STATE_PATH = join(COS_DIR, 'state.json');@@ -22,13 +27,18 @@ const freshModule = async () => {};const writeState = (state) => writeFileSync(STATE_PATH, JSON.stringify(state, null, 2));+// Agent records are cos_agents rows (#12738); seed them into the in-memory store+// the freshly imported cosState module uses.+const seedAgents = async (agents) => (await import('./cosAgentRepository.js')).__seedCosAgents(agents);+const storedAgents = async () => Object.fromEntries((await (await import('./cosAgentRepository.js')).loadAgentRows()).rows.map((row) => [row.id, JSON.parse(row.body)]));const writeConfig = (config) => writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2));const readJson = (path) => JSON.parse(readFileSync(path, 'utf-8'));const quarantined = (prefix) => readdirSync(COS_DIR).filter(f => f.startsWith(`${prefix}.corrupted.`));-beforeEach(() => {+beforeEach(async () => {rmSync(COS_DIR, { recursive: true, force: true });mkdirSync(COS_DIR, { recursive: true });+ (await import('./cosAgentRepository.js')).__resetCosAgentRepository();});describe('cosState persistence', () => {@@ -57,13 +67,8 @@ describe('cosState persistence', () => {});writeState({running: false,- agents: {- 'agent-1': {- id: 'agent-1',- // Verbatim shape of the text that triggered the false positive.- prompt: 'Using saved defaults: --review-with={value}{, --review-iterations=…}{ — project|global}',- },- },+ // Verbatim shape of the text that triggered the false positive.+ note: 'Using saved defaults: --review-with={value}{, --review-iterations=…}{ — project|global}',});const { loadState } = await freshModule();@@ -71,7 +76,7 @@ describe('cosState persistence', () => {expect(state.config.maxConcurrentAgents).toBe(20);expect(state.config.maxConcurrentAgentsPerProject).toBe(12);- expect(state.agents['agent-1']).toBeDefined();+ expect(state.note).toContain('{value}{');// A valid file is never treated as corrupt, so nothing is quarantined.expect(quarantined('state.json')).toHaveLength(0);});@@ -91,9 +96,8 @@ describe('cosState persistence', () => {});it('reports a "}{"-carrying state file as trusted to the update safety gate', async () => {- writeState({ agents: { 'agent-1': { id: 'agent-1', status: 'running', prompt: 'a }{ b' } } });-const { readAgentsStateForSafetyCheck } = await freshModule();+ await seedAgents({ 'agent-1': { id: 'agent-1', status: 'running', prompt: 'a }{ b' } });// `trusted: false` here would make the update gate treat a live agent as// "records unreadable" and demand a manual state.json restore.@@ -138,7 +142,7 @@ describe('cosState persistence', () => {expect(recovered.config.maxConcurrentAgentsPerProject).toBe(12);// Settings the user never changed still come from the shipped defaults.expect(recovered.config.maxTotalProcesses).toBe(DEFAULT_CONFIG.maxTotalProcesses);- // Agent records are genuinely lost — but the bad bytes stay inspectable.+ // The bad bytes stay inspectable.expect(recovered.agents).toEqual({});expect(quarantined('state.json')).toHaveLength(1);});@@ -147,8 +151,9 @@ describe('cosState persistence', () => {describe('CoS config lives in its own file', () => {it('never writes config back into state.json', async () => {writeConfig({ maxConcurrentAgents: 9 });- writeState({ running: true, agents: { a1: { status: 'running' } } });+ writeState({ running: true });const { loadState, saveState, getConfig } = await freshModule();+ await seedAgents({ a1: { status: 'running' } });const state = await loadState();expect(state.config.maxConcurrentAgents).toBe(9);@@ -161,7 +166,8 @@ describe('CoS config lives in its own file', () => {const persisted = readJson(STATE_PATH);expect(persisted.config).toBeUndefined();- expect(Object.keys(persisted.agents)).toEqual(['a1', 'a2']);+ expect(persisted.agents).toBeUndefined();+ expect(Object.keys(await storedAgents())).toEqual(['a1', 'a2']);// config.json is untouched by a runtime-record write.expect(readJson(CONFIG_PATH)).toEqual({ maxConcurrentAgents: 9 });});@@ -448,13 +454,15 @@ describe('live CoS restore boundary', () => {{ agents: { example: { status: 'paused' } } },{ agents: {}, persistentMind: { started: true } },{ agents: {}, persistentMind: { activeTurn: { id: 'example-turn' } } },- ])('refuses unsafe ownership before transferring: %j', async state => {+ ])('refuses unsafe ownership before transferring: %j', async ({ agents, ...state }) => {writeState(state);const store = await freshModule();+ await seedAgents(agents);const transfer = vi.fn();await expect(store.withLiveCosRestore(transfer)).rejects.toMatchObject({ code: 'COS_RESTORE_BUSY' });expect(transfer).not.toHaveBeenCalled();expect(readJson(STATE_PATH)).toEqual(state);+ expect(await storedAgents()).toEqual(agents);});it('refuses unreadable runtime records without overwriting them', async () => {@@ -482,10 +490,10 @@ describe('state operation slow diagnostics', () => {if (stage === 'write') clock.advance(1500);if (stage === 'dirSync') clock.stall(600);} });- expect(stages).toEqual(['prepare', 'serialize', 'write', 'replace', 'dirSync']);- expect(readJson(STATE_PATH).agents).toEqual({});+ expect(stages).toEqual(['prepare', 'agents', 'serialize', 'write', 'replace', 'dirSync']);+ expect(readJson(STATE_PATH).agents).toBeUndefined();expect(warn).toHaveBeenCalledTimes(1);- expect(warn.mock.calls[0][0]).toMatch(/^🐢 CoS state write slow outcome=saved totalMs=2100 prepareMs=0 serializeMs=0 writeMs=1500 replaceMs=0 dirSyncMs=600 loopLagMs=500 loopLagMaxMs=500 sizeKiB=\d+$/);+ expect(warn.mock.calls[0][0]).toMatch(/^🐢 CoS state write slow outcome=saved totalMs=2100 prepareMs=0 agentsMs=0 serializeMs=0 writeMs=1500 replaceMs=0 dirSyncMs=600 loopLagMs=500 loopLagMaxMs=500 sizeKiB=\d+$/);warn.mockClear();await store.withStateLock(async () => clock.advance(200));@@ -498,3 +506,91 @@ describe('state operation slow diagnostics', () => {}});});++// #12738: agent records are cos_agents rows, written per changed row.+describe('agent records in cos_agents', () => {+ const agent = (n, extra = {}) => ({ id: `agent-example-${n}`, taskId: `task-example-${n}`, status: 'running', output: [], ...extra });++ it('loses no concurrent update and writes only the changed row per update', async () => {+ const store = await freshModule();+ const repo = await import('./cosAgentRepository.js');+ const agents = Object.fromEntries(Array.from({ length: 8 }, (_, n) => [agent(n).id, agent(n)]));+ await seedAgents(agents);+ await store.saveState(await store.loadState());+ const fileWrite = statSync(STATE_PATH).mtimeMs;+ repo.applyAgentSave.mockClear();++ await Promise.all(Object.keys(agents).map((agentId, n) => store.withStateLock(async () => {+ const state = await store.loadState();+ state.agents[agentId] = { ...state.agents[agentId], status: 'completed', output: [`line ${n}`] };+ await store.saveState(state);+ })));++ const stored = await storedAgents();+ expect(Object.values(stored).map((record) => record.status)).toEqual(Array(8).fill('completed'));+ expect(stored[agent(3).id].output).toEqual(['line 3']);+ const writes = repo.applyAgentSave.mock.calls.map(([plan]) => plan);+ expect(writes).toHaveLength(8);+ expect(writes.every((plan) => plan.upserts.length === 1 && plan.deletes.length === 0)).toBe(true);+ // Nothing outside the agent map changed, so state.json was not rewritten.+ expect(statSync(STATE_PATH).mtimeMs).toBe(fileWrite);+ expect(readJson(STATE_PATH).agents).toBeUndefined();+ });++ it('keeps agent records when state.json becomes unreadable', async () => {+ const store = await freshModule();+ await seedAgents({ [agent(1).id]: agent(1) });+ writeFileSync(STATE_PATH, '{broken');+ expect((await store.loadState()).agents).toEqual({ [agent(1).id]: agent(1) });+ });++ it('ignores a legacy agents map left in state.json and drops it on the next save', async () => {+ writeState({ running: false, agents: { [agent(1).id]: agent(1) } });+ const store = await freshModule();+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});+ try {+ const state = await store.loadState();+ expect(state.agents).toEqual({});+ expect(warn.mock.calls.some(([line]) => line.includes('legacy agents map'))).toBe(true);+ await store.saveState(state);+ expect(readJson(STATE_PATH)).not.toHaveProperty('agents');+ } finally {+ warn.mockRestore();+ }+ });++ it('refuses a save planned on a cache the store has moved past, then reloads from the store', async () => {+ const store = await freshModule();+ await seedAgents({ [agent(1).id]: agent(1) });+ const state = await store.loadState();+ // The store changes underneath the cache (a database restore, say).+ await seedAgents({ [agent(2).id]: agent(2) });+ state.agents[agent(1).id].status = 'completed';+ await expect(store.saveState(state)).rejects.toMatchObject({ code: 'COS_AGENTS_STALE' });+ expect(await storedAgents()).toEqual({ [agent(2).id]: agent(2) });+ expect(Object.keys((await store.loadState()).agents)).toEqual([agent(2).id]);+ });++ it('sweeps stale state.json temp files from other processes, never its own or a fresh one', async () => {+ const uuid = '0123abcd-0123-4567-89ab-0123456789ab';+ const names = {+ stale: `state.json.${process.pid + 1}.1000.${uuid}.tmp`,+ own: `state.json.${process.pid}.1000.${uuid}.tmp`,+ fresh: `state.json.${process.pid + 2}.1000.${uuid}.tmp`,+ unrelated: `config.json.${process.pid + 1}.1000.${uuid}.tmp`,+ };+ for (const name of Object.values(names)) writeFileSync(join(COS_DIR, name), '{}');+ const old = new Date(Date.now() - 5 * 60_000);+ for (const key of ['stale', 'own', 'unrelated']) utimesSync(join(COS_DIR, names[key]), old, old);+ const log = vi.spyOn(console, 'log').mockImplementation(() => {});+ try {+ const store = await freshModule();+ await store.loadState();+ const left = readdirSync(COS_DIR);+ expect(left).not.toContain(names.stale);+ expect(left).toEqual(expect.arrayContaining([names.own, names.fresh, names.unrelated]));+ } finally {+ log.mockRestore();+ }+ });+});diff --git a/server/services/cosTaskRepository.js b/server/services/cosTaskRepository.jsindex e0d53d5953..faab401a43 100644--- a/server/services/cosTaskRepository.js+++ b/server/services/cosTaskRepository.js@@ -134,7 +134,9 @@ async function makeMemoryBackend({ persist }) {const lists = Object.fromEntries(SOURCES.map((source) => [source, new Map()]));const versions = Object.fromEntries(SOURCES.map((source) => [source, 0]));if (persist) {- const saved = await readJSONFileStrict(tasksFile(), {}, { logError: false });+ // readJSONFileStrict returns `{ ok, value }`; never start empty over an unreadable file.+ const { ok, value: saved } = await readJSONFileStrict(tasksFile(), {}, { logError: false });+ if (!ok) throw new Error(`Unreadable ${tasksFile()} — repair or remove it before starting the dev file backend`);for (const source of SOURCES) {for (const row of saved?.[source]?.rows ?? []) lists[source].set(row.task.id, row);versions[source] = saved?.[source]?.version ?? 0;@@ -231,5 +233,5 @@ export function __resetCosTaskRepository() { backendFacade.reset(); }/** Test seam: the PostgreSQL backend over a `{ query, withTransaction }` handle, for the DB suite. */export const __makePgCosTaskBackend = makePgBackend;-/** Test seam: a fresh in-memory backend that never touches disk. */-export const __makeMemoryCosTaskBackend = () => makeMemoryBackend({ persist: false });+/** Test seam: a fresh in-memory backend; it touches disk only with `persist: true`. */+export const __makeMemoryCosTaskBackend = ({ persist = false } = {}) => makeMemoryBackend({ persist });diff --git a/server/services/worktreeExternalCleanup.test.js b/server/services/worktreeExternalCleanup.test.jsindex 584bc55915..e8922b5b88 100644--- a/server/services/worktreeExternalCleanup.test.js+++ b/server/services/worktreeExternalCleanup.test.js@@ -229,7 +229,9 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('external orphan cleanup recovery policyit('retains merged work when durable completion policy is unreadable despite an empty owner snapshot', async () => {const path = await addTree('agent-policy-unreadable');- await writeFile(join(PATHS.cos, 'state.json'), '{');+ // Agent records are cos_agents rows (#12738); the agent index is the durable policy input left on disk.+ await mkdir(join(PATHS.cos, 'agents'), { recursive: true });+ await writeFile(join(PATHS.cos, 'agents', 'index.json'), '{');expect(await cleanupOrphanedWorktrees(primary, new Set(), NO_OWNERS)).toBe(0);expect(existsSync(path)).toBe(true);expect(await branchExists('agent-policy-unreadable')).toBe(true);