Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: housekeeping keeps a subagent's own page writes, survives its own errors, and a code-less refusal still ages out; writer status omits unset caps
garrytan24a2ea92fe52
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 4e50fe74b0..4f4d21e9fa 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -12,17 +12,18 @@ identifiers and attribution are available in the pre-removal Git revision## [Unreleased]-**gbrain's own write bookkeeping no longer caps your writes.** Every accepted write leaves a receipt, and gbrain used to cap how many request ids and how many receipt bytes one writer and one brain could hold (2,000,000 / 8,000,000 ids, 16 GiB / 64 GiB). Near those caps, background writers paused at 80% and every write receipt carried a low-capacity warning; at the cap, `remember`, `put_page` and sync refused with `write_quota_exhausted` until someone on the brain host raised a number. Nothing about your data needed that: a cap only stopped writes. Both caps are now off unless the operator sets one. Receipt storage is kept in check by housekeeping instead: while receipts not yet compacted reserve more than `persistence.housekeeping.receipt_target_bytes` (default 8 GiB; the few KiB a compacted receipt keeps so its request id stays replayable never count, since nothing can shrink them), the compaction pass also compacts committed and cancelled receipts older than 7 days, plus refusals that gbrain's own producers will write again from current content, and doctor `persistence_capacity` tells the brain host's operator. A refused write you made that `gbrain repair failed-writes` could still replay now keeps its intent at any age, until a later write resolves it or you drop it with `gbrain repair receipt-compaction --include-failures`; the retention pass used to drop it after 30 days. An explicit `persistence.receipt_retention_days` is left as set. On a brain that several hosts write to, these defaults hold once every one of them runs this version: an older gbrain still enforces its old caps and its 30-day retention, and the `gbrain config set persistence.limits.*` that its `write_quota_exhausted` refusal suggests would turn a cap on for every host, so upgrade the other hosts instead of raising it.+**gbrain's own write bookkeeping no longer caps your writes.** Every accepted write leaves a receipt, and gbrain used to cap how many request ids and how many receipt bytes one writer and one brain could hold (2,000,000 / 8,000,000 ids, 16 GiB / 64 GiB). Near those caps, background writers paused at 80% and every write receipt carried a low-capacity warning; at the cap, `remember`, `put_page` and sync refused with `write_quota_exhausted` until someone on the brain host raised a number. Nothing about your data needed that: a cap only stopped writes. Both caps are now off unless the operator sets one. Receipt storage is kept in check by housekeeping instead: while receipts not yet compacted reserve more than `persistence.housekeeping.receipt_target_bytes` (default 8 GiB; the few KiB a compacted receipt keeps so its request id stays replayable never count, since nothing can shrink them), the compaction pass also compacts committed and cancelled receipts older than 7 days (except a subagent's own page writes, which a deferred synthesis postprocess still compares with the page), plus refusals that gbrain's own producers will write again from current content, and doctor `persistence_capacity` tells the brain host's operator. A refused write you made that `gbrain repair failed-writes` could still replay now keeps its intent at any age, until a later write resolves it or you drop it with `gbrain repair receipt-compaction --include-failures`; the retention pass used to drop it after 30 days. An explicit `persistence.receipt_retention_days` is left as set. On a brain that several hosts write to, these defaults hold once every one of them runs this version: an older gbrain still enforces its old caps and its 30-day retention, and the `gbrain config set persistence.limits.*` that its `write_quota_exhausted` refusal suggests would turn a cap on for every host, so upgrade the other hosts instead of raising it.### Itemized changes- `src/core/persistence/model.ts`, `limits.ts`: `principal_lifetime_ids`, `brain_lifetime_ids`, `principal_terminal_bytes` and `brain_terminal_bytes` default to no cap; a value set with `gbrain config set` is enforced as before (refusal at the cap, background pause at its line). New key `persistence.housekeeping.receipt_target_bytes` (0 turns housekeeping and its warning off), measured by `readCompactableReceiptBytes`: terminal receipts not yet compacted that no recovery holds.-- `src/core/persistence/compaction-pass.ts`: `housekeepReceipts` compacts up to 100 receipts older than 7 days per pass: settled page writes, and refused maintenance pages and managed sync, file import, reconcile and file repair page writes. Job receipts, connector writes and every other refusal (a caller's write, conversation facts, atom members, connector items) keep their intent until the retention window while the brain is over the target and the retention is the default, with every exclusion of the retention pass; the pass record counts them as `early`.+- `src/core/persistence/compaction-pass.ts`: `housekeepReceipts` compacts up to 100 receipts older than 7 days per pass: settled page writes, and refused maintenance pages and managed sync, file import, reconcile and file repair page writes. Job receipts, a subagent's own page writes (`restrictedNamespace` authority), connector writes and every other refusal (a caller's write, conversation facts, atom members, connector items) keep their intent until the retention window while the brain is over the target and the retention is the default, with every exclusion of the retention pass; the pass record counts them as `early`. A housekeeping error is counted as `housekeeping_error` in the pass record and never skips the reservation sweep or the record.- `src/commands/doctor/checks/persistence-capacity.ts`: with no cap set, `ok` says so and `warn` fires above the target with the read-only `gbrain repair receipt-compaction --json` preview (actor `host_admin`); `details.housekeeping` reports target, stored bytes, the not-yet-compacted bytes the target counts and an explicit retention. The forecast, capacity notices, repair capacity rows and `persistence_request_growth` cover only caps that are set, so the `/metrics` `gbrain_write_capacity_*` gauges are exported only for a cap you set; an alert on them needs a cap.-- `src/core/persistence/journal.ts`, `terminal-classify.ts`: the retention pass skips `UNRESOLVED_CALLER_REFUSAL_SQL` receipts (a writer-guard or `source_changed` refusal of a caller's `put_page`, `add_timeline_entry` or `remember` that no later request wrote, removed or repeated); `gbrain repair failed-writes` reads the same refusal predicates from there.+- `src/core/persistence/journal.ts`, `terminal-classify.ts`: the retention pass skips `UNRESOLVED_CALLER_REFUSAL_SQL` receipts (a writer-guard or `source_changed` refusal of a caller's `put_page`, `add_timeline_entry` or `remember` that no later request wrote, removed or repeated); `gbrain repair failed-writes` reads the same refusal predicates from there. A refusal with no recorded error code is not one of them and compacts at the retention as before.+- `src/core/persistence/diagnostics.ts`: `gbrain sources writer status --json` lists only the limits that are set; an unset cap is left out instead of printing as `null`.- `src/core/persistence/reconcile-backup.ts`: the `--backups` listing leaves out `max_retained_count` while the brain id cap is unset (it serialized as `null`); the retained-backup bound is `brain_recovery_bytes`.- Docs: `docs/guides/repair.md` (refused writes keep their intent until resolved; no retention raise needed for a recovery), `docs/guides/concurrent-writes.md` (limits table, receipt housekeeping, growth model as sizing for a cap you set), `docs/guides/write-refusals.md`.-- Tests: `test/persistence-opt-in-caps.test.ts` (counters at the old default caps admit background and interactive writes with no notice and no raise; a set cap still refuses; housekeeping above the target takes week-old settled page writes and refused maintenance and sync writes, and keeps a caller's refused write, a refused conversation-facts job, a committed job and young receipts; the retention pass keeps a 40-day-old replayable drift or guard refusal and compacts a resolved one and a non-replayable failure (red before: the drift refusal was compacted); a 40-day synthetic run never holds more than a week of uncompacted committed receipts; doctor's operator warning; compacted receipts alone above the target leave housekeeping idle and doctor ok, red before; the backup listing names no count bound while the id cap is unset, red before), `test/persistence-capacity.test.ts` (key count); the capacity forecast, notice and read-wave tests set the cap they exercise; the doctor goldens show the uncapped default and `details.housekeeping`.+- Tests: `test/persistence-opt-in-caps.test.ts` (counters at the old default caps admit background and interactive writes with no notice and no raise; a set cap still refuses; housekeeping above the target takes week-old settled page writes and refused maintenance and sync writes, and keeps a caller's refused write, a refused conversation-facts job, a committed job and young receipts; the retention pass keeps a 40-day-old replayable drift or guard refusal and compacts a resolved one and a non-replayable failure (red before: the drift refusal was compacted); a 40-day synthetic run never holds more than a week of uncompacted committed receipts; doctor's operator warning; compacted receipts alone above the target leave housekeeping idle and doctor ok, red before; the backup listing names no count bound while the id cap is unset, red before; a subagent's page write is kept from early compaction, a refusal with no error code still compacts at the retention, a housekeeping error still records the pass, and writer status prints no unset cap, each red before), `test/managed-synthesis-postprocess.test.ts` (an interrupted synthesis child output ten days old, with storage above the target and a maintenance drift on the page, is still verified and stamped by the next pass; red before: `revision_conflict`), `test/persistence-capacity.test.ts` (key count); the capacity forecast, notice and read-wave tests set the cap they exercise; the doctor goldens show the uncapped default and `details.housekeeping`.**A write that saved now says so when it carries a low-capacity warning, and the warning no longer asks the user to raise a write cap by tens of gigabytes.** When a cumulative write cap (receipt bytes or request ids) was at 80% or forecast to run out within 30 days, every write receipt's `write_capacity_low` notice ended with `actor: host_admin` and `next: tell_user_to_run: gbrain config set persistence.limits.principal_terminal_bytes <value>`. An agent that read only the end of a successful `remember` took that for a refusal and told its user the memory was not saved. The value was also oversized: it projected the last 24 hours for a whole year, so one bulk sync day could recommend several times the current cap. The notice now opens with "This write was saved and writes still work", its fix is the read-only `gbrain doctor --only persistence_capacity --json`, and the one-more-year value uses the 7-day mean rate. Days-to-exhaustion warnings still react to a bulk day. Doctor's finding keeps the `gbrain config set` command for the brain host's operator.diff --git a/docs/architecture/key-files/core-persistence-capacity.md b/docs/architecture/key-files/core-persistence-capacity.mdindex dd78b2055f..d39b862d6d 100644--- a/docs/architecture/key-files/core-persistence-capacity.md+++ b/docs/architecture/key-files/core-persistence-capacity.md@@ -9,7 +9,7 @@ Cumulative write caps (permanent request ids, receipt bytes; opt-in since GBRA-1- `src/core/persistence/capacity-forecast.ts` — `readCapacityForecast`: per principal, one statement walks `persistence_requests_principal` newest first until `created_at` leaves the 7-day window and buckets by day; gross and net (compacted) receipt rates, `days_to_exhaustion` (at the larger of the 7-day mean and the last 24 hours), `basis` (`:legacy_under_reserved` while receipts admitted before the intent-sized reservation remain), `recommended_limit` (one more year at the 7-day mean, GST-2107, so one bulk day is not projected for a year). `light` skips the reclaim read and table size. Thresholds `persistence.capacity.warn_days` (30) and `fail_days` (7).- `src/core/engine-key.ts` + `src/core/engine-root.ts` — the key every per-engine cache uses. `engineKey(engine)` returns the engine itself through the `ENGINE_ROOT` symbol (each engine stores itself there; a page-transaction clone inherits it, forwarding proxies and a recovered degraded engine return it), and otherwise walks a page-transaction clone, a read-through view or a statement-deadline wrapper (`engineCacheKey`) to the engine behind it. A process with mounted brains keeps one entry per brain in the search telemetry writer (`getTelemetryWriter(engine)`), `search.track_retrieval`, minion admission policy, `mcp.strict_params` last-known-good, publication ceiling, `write_capacity_low` throttle, `search.hnsw_iterative_scan` and `search.vector_legacy_guard`, and a transaction or view shares its engine's entry. A one-brain process behaves as before. Test: `test/engine-keyed-caches.test.ts`.- `src/core/persistence/capacity-snapshot.ts` — `cachedCapacitySnapshot`: one light forecast per engine per 60 s, shared by every caller; `capacityNotices` builds `write_capacity_low` (kind `info`, muteable; it says the write was saved and its fix is the read-only doctor check, GST-2107; hourly per engine and scope on write and sync receipts, `scopes` limits a remote caller to the brain and its own principal) and `write_capacity_backpressure` (once per pause start). Read by `mcp/dispatch.ts`, `cli/main.ts`, `commands/sync/{run,operator}.ts`, `cycle.ts` and the admin `/metrics` gauges in `serve-http-metrics.ts`. #6803: the cache keys on the real engine (`engineCacheKey`), the shared fetch is bounded at `CAPACITY_FETCH_BOUND_MS` (10 s) and a timed-out fetch is never cached; `capacitySnapshotWithin` (sync status, write and sync receipts: `CAPACITY_READ_WAIT_MS`, 1.5 s) returns `unknown` for that call alone, and `capacityNotices` on an unknown snapshot returns no notice and leaves throttle and pause state as it was. Test: `test/capacity-bounded-read-6803.test.ts`.-- `src/core/persistence/compaction-pass.ts` — `runCompactionPass`, the consumer's idle and volume maintenance: `compactWriteReceiptsDetailed`, `housekeepReceipts` (GBRA-108: while receipts not yet compacted reserve more than `persistence.housekeeping.receipt_target_bytes` (`limits.ts` `readCompactableReceiptBytes`; compacted receipts never count) with the default retention, up to 100 receipts older than `HOUSEKEEPING_FLOOR_DAYS` that `EARLY_SQL` admits: settled non-job, non-connector writes and refused maintenance or managed sync page writes; counted as `early`), one sweep step, and the `persistence.maintenance.last_compaction` record (`at`, `host_id`, `compacted`, `freed_bytes`, `skipped_by_reason`). Each step resumes from the timer queue (`journal.ts` `yieldToEventLoop`), as do the consumer's maintenance chain, the per-table queue `VACUUM` and the effects drain passes (`NOOP_SETTLE_BATCH` 50), so on PGLite a request waits behind one step at most. The retention pass (`compactWriteReceiptsDetailed`) skips `terminal-classify.ts` `UNRESOLVED_CALLER_REFUSAL_SQL` (a caller refusal `repair failed-writes` could still replay, GBRA-108). `journal.ts` `compactReceipt` compacts one receipt in its own transaction (counters first, `lock_timeout` 2 s, `statement_timeout` 15 s) and skips open managed-sync runs (`OPEN_SYNC_CURSOR_EXCLUSION`) and the newest sync receipt per worktree (`newestSyncReceipt`).+- `src/core/persistence/compaction-pass.ts` — `runCompactionPass`, the consumer's idle and volume maintenance: `compactWriteReceiptsDetailed`, `housekeepReceipts` (GBRA-108: while receipts not yet compacted reserve more than `persistence.housekeeping.receipt_target_bytes` (`limits.ts` `readCompactableReceiptBytes`; compacted receipts never count) with the default retention, up to 100 receipts older than `HOUSEKEEPING_FLOOR_DAYS` that `EARLY_SQL` admits: settled non-job, non-connector writes that are not a subagent's own (`restrictedNamespace`), and refused maintenance or managed sync page writes; counted as `early`; an error counts as `housekeeping_error` and the sweep still runs), one sweep step, and the `persistence.maintenance.last_compaction` record (`at`, `host_id`, `compacted`, `freed_bytes`, `skipped_by_reason`). Each step resumes from the timer queue (`journal.ts` `yieldToEventLoop`), as do the consumer's maintenance chain, the per-table queue `VACUUM` and the effects drain passes (`NOOP_SETTLE_BATCH` 50), so on PGLite a request waits behind one step at most. The retention pass (`compactWriteReceiptsDetailed`) skips `terminal-classify.ts` `UNRESOLVED_CALLER_REFUSAL_SQL` (a caller refusal `repair failed-writes` could still replay, GBRA-108). `journal.ts` `compactReceipt` compacts one receipt in its own transaction (counters first, `lock_timeout` 2 s, `statement_timeout` 15 s) and skips open managed-sync runs (`OPEN_SYNC_CURSOR_EXCLUSION`) and the newest sync receipt per worktree (`newestSyncReceipt`).- `src/core/repair/receipt-compaction.ts` — `gbrain repair receipt-compaction`: explicit-only, preview-bound, destructive consent, `publication: 'projection'`; `--older-than` (the `pages purge-deleted` grammar) floored at the configured retention unless `--force`, committed only unless `--include-failures`; the approved set binds an absolute cutoff and the newest sequence. Test: `test/repair-receipt-compaction.test.ts`.- `src/core/persistence/refusal-breaker.ts` — `assertRefusalBreakerClosed`: `submitMaintenance` refuses before admission after `persistence.maintenance.refusal_breaker_threshold` (3; 0 off) same-code terminal refusals in 24 h for the same request `digest` (source, slug, intent with its revision and planner); `listSuppressedWrites` feeds doctor `maintenance_write_drift`. The ghost case (`canonical_file_missing`, tagged by `page-prepare.ts`, kept in `error_detail.origin`, returned by `liveFileDrift`) refuses before admission in `prepared-maintenance.ts`. Test: `test/maintenance-ghost-breaker-6696.test.ts`.- Sync: `sync-run.ts` `preflightSyncCapacity` refuses `write_quota_insufficient` before a run's first admission; `sync-drain.ts` stops a later cap refusal `resumable` / `capacity_paused` (`drain.capacity_pause`). Test: `test/sync-capacity-preflight.test.ts`.diff --git a/docs/guides/concurrent-writes.md b/docs/guides/concurrent-writes.mdindex 919f46f7d9..bf2b10d71b 100644--- a/docs/guides/concurrent-writes.md+++ b/docs/guides/concurrent-writes.md@@ -984,7 +984,8 @@ off), the resident consumer's compaction pass also compacts receipts older than7 days, oldest first, up to 100 a pass: settled page writes, and the refusalsof producers that write again from current content (maintenance pages andmanaged sync, file import, reconcile and file repair page writes). Job-receipts (`submit_job`), connector writes and every other refusal keep their+receipts (`submit_job`), a subagent's own page writes (a deferred synthesis+postprocess compares them with the page), connector writes and every other refusal keep theirintent until the retention window (a caller's replayable refusal until alater write resolves it): `gbrain repair failed-writes` replays a caller'srefused write from it, conversation facts resubmit a frozendiff --git a/src/core/behavior-change-notice.ts b/src/core/behavior-change-notice.tsindex 8dfdd2358e..79bacd2dad 100644--- a/src/core/behavior-change-notice.ts+++ b/src/core/behavior-change-notice.ts@@ -1493,7 +1493,7 @@ export const BEHAVIOR_CHANGES: ReadonlyArray<{ since: string; text: ChangeText;{ since: '0.60.622.0', id: 'think_window_trajectory_gst731', text: '`think` on a question that names one month or day (or given `since`/`until`) still gets the entity\'s trajectory: each metric\'s latest reading before the window now joins the readings inside it, as the value in force when the window opens (unless its `valid_until` ended by then), and an entity page dated outside the window still names whose trajectory to read (it stays out of the evidence). Before, "MRR in April" with readings in March and June, or a company page last edited outside the month, injected no trajectory. `WINDOW_EXCLUDED_<n>_TRAJECTORY_POINTS` no longer counts the carried reading.' },{ since: '0.60.622.0', id: 'purge_cancels_deferred_write_gst1602', text: 'With `persistence.refresh_barrier` on, `gbrain forget <id> --purge` no longer refuses `purge_blocked_pending_recovery` while a write deferred behind a refresh holds the claim in its staged body: the deferred write was never claimed, so the purge cancels it with `purged_content`, as it does a queued one, and removes the content at once instead of up to 15 minutes later.' },{ since: '0.60.623.0', id: 'owner_reads_private_gst2059', text: 'A brain can now name its owner: `gbrain access owner set <member>` (config `access.owner_member`, an enabled member). Every remote credential or stdio server bound to that member reads the whole brain, `visibility: private` pages, privacy-alias pages (`access: private`, ...), derived and pinned-question pages included, through `get_page`, `search`, `query`, `recall` and the other page reads in the sources its grant reads, and `recall` returns its private facts. `readable_by` and folder rules still bind it, and it is not relaxed when it speaks for a shared-room audience. A page the owner\'s credential creates in `default` through `put_page` is not kept to the owner (no `readable_by: [member:<id>]`). Other members, unbound and public callers are unchanged. `_meta.access.relaxation` reads `owner` for such a call, and `gbrain access check --token <name>` reports it. `gbrain repair private-labels` (named runs only) previews the `access: private` labels stamped on ordinary pages with counts per category, keeps real restrictions, and clears the rest in reviewed batches (`--apply --expect <hash>`), recording each change so `--class restore` can put it back. The bulk-ingestion page template no longer writes an `access:` line; what each designation means is in docs/guides/access-designations.md.' },- { since: 'TBD', id: 'opt_in_receipt_caps_gbra108', text: 'gbrain\'s own receipt bookkeeping no longer refuses, pauses or throttles writes by default: the cumulative caps on permanent request ids and reserved receipt bytes (`persistence.limits.{principal,brain}_{lifetime_ids,terminal_bytes}`, 2,000,000 / 8,000,000 ids and 16 GiB / 64 GiB before) apply only when set, so `write_quota_exhausted`, `write_quota_insufficient`, `write_capacity_backpressure`, `write_capacity_low` and a sync\'s `capacity_paused` happen only under a cap the operator chose. Instead, while receipts not yet compacted reserve more than `persistence.housekeeping.receipt_target_bytes` (default 8 GiB, 0 off; compacted receipts never count) and the retention is the default, the compaction pass also compacts settled page writes and refused maintenance and managed sync page writes older than 7 days (job receipts, connector writes and every other refusal keep their intent until the retention window), the `/metrics` `gbrain_write_capacity_*` gauges appear only for a cap that is set, and the retention pass no longer drops a caller\'s refused write that `gbrain repair failed-writes` could still replay (it waits until a later write resolves it, or `gbrain repair receipt-compaction --include-failures` drops it), and doctor `persistence_capacity` warns the host operator. A cap already set is unchanged; `gbrain config unset` removes one. On a brain several hosts write to, this holds once every host runs this version: an older gbrain still enforces its old caps and 30-day retention, and raising a cap as its refusal suggests turns the cap on for every host.' },+ { since: 'TBD', id: 'opt_in_receipt_caps_gbra108', text: 'gbrain\'s own receipt bookkeeping no longer refuses, pauses or throttles writes by default: the cumulative caps on permanent request ids and reserved receipt bytes (`persistence.limits.{principal,brain}_{lifetime_ids,terminal_bytes}`, 2,000,000 / 8,000,000 ids and 16 GiB / 64 GiB before) apply only when set, so `write_quota_exhausted`, `write_quota_insufficient`, `write_capacity_backpressure`, `write_capacity_low` and a sync\'s `capacity_paused` happen only under a cap the operator chose. Instead, while receipts not yet compacted reserve more than `persistence.housekeeping.receipt_target_bytes` (default 8 GiB, 0 off; compacted receipts never count) and the retention is the default, the compaction pass also compacts settled page writes and refused maintenance and managed sync page writes older than 7 days (job receipts, a subagent\'s own page writes, connector writes and every other refusal keep their intent until the retention window), the `/metrics` `gbrain_write_capacity_*` gauges appear only for a cap that is set, and the retention pass no longer drops a caller\'s refused write that `gbrain repair failed-writes` could still replay (it waits until a later write resolves it, or `gbrain repair receipt-compaction --include-failures` drops it), and doctor `persistence_capacity` warns the host operator. A cap already set is unchanged; `gbrain config unset` removes one. On a brain several hosts write to, this holds once every host runs this version: an older gbrain still enforces its old caps and 30-day retention, and raising a cap as its refusal suggests turns the cap on for every host.' },{ since: 'TBD', id: 'write_capacity_low_saved_gst2107', text: 'The `write_capacity_low` notice a write receipt carries now opens with "This write was saved" and its fix is the read-only `gbrain doctor --only persistence_capacity --json` (actor agent) instead of a `gbrain config set` cap raise for the user, so an agent reading the end of a successful write no longer reports it as refused. The one-more-year value doctor, the notice snapshot and `/metrics` recommend now uses the 7-day mean rate, so one bulk day is no longer projected for a year; days-to-exhaustion still uses the larger of the mean and the last 24 hours. A `write_quota_exhausted`, `write_quota_insufficient` or background-pause refusal names the same value doctor does instead of sizing the year from the latest 1,000 admissions.' },];diff --git a/src/core/persistence/compaction-pass.ts b/src/core/persistence/compaction-pass.tsindex 64c0dc48f8..35e3987343 100644--- a/src/core/persistence/compaction-pass.ts+++ b/src/core/persistence/compaction-pass.ts@@ -29,7 +29,11 @@ const recordedAt = new WeakMap<object, number>();export async function runCompactionPass(engine: BrainEngine, opts: { hostId: string; notice?: boolean; now?: number }): Promise<CompactionRecord> {const result = await compactWriteReceiptsDetailed(engine);await yieldToEventLoop();- const early = await housekeepReceipts(engine);+ // A housekeeping error never skips the reservation sweep or the pass record: it is counted and the pass carries on.+ const early = await housekeepReceipts(engine).catch((error: unknown): ReceiptCompactionResult => {+ process.stderr.write(`[persistence] phase=housekeeping error=${getCode(error) ?? (error instanceof Error ? error.message : String(error))}\n`);+ return { compacted: 0, freed_bytes: 0, skipped_by_reason: { housekeeping_error: 1 } };+ });for (const [reason, n] of Object.entries(early.skipped_by_reason)) result.skipped_by_reason[reason] = (result.skipped_by_reason[reason] ?? 0) + n;await yieldToEventLoop();const swept = await sweepCompactedReservations(engine);@@ -50,13 +54,15 @@ export async function runCompactionPass(engine: BrainEngine, opts: { hostId: str/*** What housekeeping may compact early (GBRA-108). Settled receipts, except `submit_job` ones (atom retries, generation- * members and synthesis postprocess read their committed intents) and connector writes (a later retry's `retryOf` resolves+ * members and synthesis postprocess read their committed intents), a subagent's own writes (`restrictedNamespace`: a deferred+ * synthesis postprocess compares the child's committed content with the page) and connector writes (a later retry's `retryOf` resolves* an earlier refusal). Refused receipts only where the producer writes again from current content and nothing reads the* intent: managed sync, file import, reconcile and file repair page writes, and maintenance pages. Every other refusal keeps* its intent until the retention window: a caller's write (`gbrain repair failed-writes` replays it), conversation facts* (the frozen extraction), atom members, connector items (their held-retry pointer).*/-const EARLY_SQL = (r: string) => `((${r}.state IN ('committed','cancelled') AND ${r}.operation<>'submit_job' AND COALESCE(${r}.intent->>'kind','') NOT LIKE 'connector_%')+const EARLY_SQL = (r: string) => `((${r}.state IN ('committed','cancelled') AND ${r}.operation<>'submit_job' AND COALESCE(${r}.intent->>'kind','') NOT LIKE 'connector_%'+ AND COALESCE(${r}.authority->>'restrictedNamespace','')<>'true')OR (${r}.state IN ('failed','conflict') AND ((${r}.operation='put_page' AND (${r}.intent->>'kind' LIKE 'managed_sync_%'OR ${r}.intent->>'kind' IN ('managed_file_import','canonical_reconcile','managed_file_repair')))OR (${r}.operation='submit_job' AND ${r}.intent->>'kind'='managed_maintenance_page'))))`;diff --git a/src/core/persistence/diagnostics.ts b/src/core/persistence/diagnostics.tsindex 85e4912c47..9e90a65128 100644--- a/src/core/persistence/diagnostics.ts+++ b/src/core/persistence/diagnostics.ts@@ -198,7 +198,8 @@ export async function readWriterDiagnostics(engine: BrainEngine) {? 'The process that answered has no resident consumer (a one-shot CLI, for example); another process may own writes. See worktrees and bindings for ownership.': 'The resident consumer of the process that answered this status.' };return { ...brain, sampled_at: new Date().toISOString(), publication_concurrency: publicationConcurrency(engine),- local_process_ingress, worktrees, counters, queue, effects, limits, capacity: capacityDiagnostics(counters, limits), preparation_policy,+ local_process_ingress, worktrees, counters, queue, effects,+ limits: Object.fromEntries(Object.entries(limits).filter(([, value]) => Number.isFinite(value))), capacity: capacityDiagnostics(counters, limits), preparation_policy,recent_failures: failures.map(row => ({ ...row, next_action: recentFailureNextAction(row.error_detail?.origin, row.request_id) })),backend_visibility: backends.visibility,blockers: blockers.map(({ id: rowId, claim_phase: _phase, execution_token: _token, claim_lapsed: _lapsed, publication_started: _started, ...row }, index) => {diff --git a/src/core/persistence/journal.ts b/src/core/persistence/journal.tsindex 44079a575c..4e1cd200e4 100644--- a/src/core/persistence/journal.ts+++ b/src/core/persistence/journal.ts@@ -1247,7 +1247,7 @@ export async function compactWriteReceiptsDetailed(engine: BrainEngine, retentioWHERE state IN ('committed','conflict','failed','cancelled') AND recovery IS NULL AND NOT compactedAND completed_at < now()-($1::double precision*interval '1 day')AND NOT EXISTS (SELECT 1 FROM persistence_effects e WHERE e.request_id=r.id AND e.state<>'committed')- AND NOT ${UNRESOLVED_CALLER_REFUSAL_SQL('r')}+ AND NOT COALESCE(${UNRESOLVED_CALLER_REFUSAL_SQL('r')}, false)${OPEN_SYNC_CURSOR_EXCLUSION('r')}${NEWEST_SYNC_RECEIPT_EXCLUSION('r')}ORDER BY sequence LIMIT 100`, [retentionDays]);diff --git a/test/managed-synthesis-postprocess.test.ts b/test/managed-synthesis-postprocess.test.tsindex 270f424508..fd6f136c2d 100644--- a/test/managed-synthesis-postprocess.test.ts+++ b/test/managed-synthesis-postprocess.test.ts@@ -21,6 +21,9 @@ import { requestIndexesCheck } from '../src/commands/doctor/checks/persistence-rimport { PERSISTENCE_REQUEST_ID_INDEX } from '../src/core/persistence/schema.ts';import { buildIndexOnline } from '../src/core/schema-migrations/helpers.ts';import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';+import { runCompactionPass } from '../src/core/persistence/compaction-pass.ts';+import { RECEIPT_TARGET_KEY } from '../src/core/persistence/limits.ts';+import { declarePersistenceProtocol } from '../src/core/persistence/protocol.ts';import { isTerminal as isTerminalRequest, type RequestState as PersistenceRequestState } from '../src/core/persistence/model.ts';import { configureGateway, resetGateway, __setChatTransportForTests, __setEmbedTransportForTests, __setDecideTransportForTests } from '../src/core/ai/gateway.ts';import { flushDecideWrites } from '../src/core/ai/decide/store.ts';@@ -358,6 +361,29 @@ test('a maintenance-only frontmatter drift after the child commit is adopted (#6});}, 120_000);+test('GBRA-108: receipt housekeeping keeps an interrupted child output, so a later pass still verifies it after a maintenance drift', async () => {+ await fixture(async ({ engine, sourceId, opts, calls }) => {+ await interruptAfterChild(engine, sourceId, opts);+ const slug = await outputSlug(engine, sourceId);+ await maintenanceFrontmatterDrift(engine, sourceId, slug);+ await disposePersistenceConsumer(engine);+ // Ten days later, with receipt storage above the housekeeping target, housekeeping runs before synthesis does.+ await engine.transaction(async tx => { await declarePersistenceProtocol(tx);+ await tx.executeRaw("UPDATE persistence_requests SET created_at=created_at-interval '10 days',completed_at=completed_at-interval '10 days'"); });+ await engine.setConfig(RECEIPT_TARGET_KEY, '1');+ await runCompactionPass(engine, { hostId: 'host-a' });+ const [child] = await engine.executeRaw<{ compacted: boolean }>(+ "SELECT compacted FROM persistence_requests WHERE source_id=$1 AND slug=$2 AND operation='put_page' AND state='committed' ORDER BY sequence LIMIT 1", [sourceId, slug]);+ expect(child).toEqual({ compacted: false });+ const spent = calls();+ const recovered = await runPhaseSynthesize(engine, opts);+ expect(recovered.status).toBe('ok');+ expect(calls()).toBe(spent);+ expect(recovered.details.pages_written).toBe(1);+ expect((await engine.readPageSnapshot(slug, { sourceId }))!.page.compiled_truth).not.toContain('"an entirely invented');+ });+}, 120_000);+test('a user put_page that only changes frontmatter is still refused, not adopted (#6360)', async () => {await fixture(async ({ engine, sourceId, opts }) => {await interruptAfterChild(engine, sourceId, opts);diff --git a/test/persistence-opt-in-caps.test.ts b/test/persistence-opt-in-caps.test.tsindex 55540547e7..d6b08e1f29 100644--- a/test/persistence-opt-in-caps.test.ts+++ b/test/persistence-opt-in-caps.test.ts@@ -10,7 +10,8 @@ import { tmpdir } from 'node:os';import { join } from 'node:path';import type { BrainEngine } from '../src/core/engine.ts';import { withAdmissionOrigin } from '../src/core/persistence/admission-origin.ts';-import { runCompactionPass } from '../src/core/persistence/compaction-pass.ts';+import { LAST_COMPACTION_KEY, runCompactionPass } from '../src/core/persistence/compaction-pass.ts';+import { readWriterDiagnostics } from '../src/core/persistence/diagnostics.ts';import { __resetCapacityNoticesForTests, capacityNotices } from '../src/core/persistence/capacity-snapshot.ts';import { declarePersistenceProtocol } from '../src/core/persistence/protocol.ts';import { RECEIPT_TARGET_KEY, readJournalLimits } from '../src/core/persistence/limits.ts';@@ -84,7 +85,7 @@ for (const kind of testBackends()) {check('above the target, housekeeping compacts settled receipts and re-derivable producer refusals older than a week, and keeps every intent a replay or retry reads', async engine => {const requests = await requestFixture(engine);- const slugs = ['old-committed', 'old-failed', 'old-conflict', 'old-maintenance-failed', 'old-sync-failed', 'old-facts-failed', 'old-job-committed', 'young-committed'];+ const slugs = ['old-committed', 'old-failed', 'old-conflict', 'old-maintenance-failed', 'old-sync-failed', 'old-facts-failed', 'old-job-committed', 'old-subagent-committed', 'young-committed'];for (const slug of slugs) await requests.publish(await requests.admit(slug));await settleEffects(engine);const [failed] = await engine.executeRaw<{ id: string }>("SELECT id FROM persistence_requests WHERE slug='old-failed'");@@ -98,6 +99,8 @@ for (const kind of testBackends()) {// A producer that recovers from its refused intent (the frozen extraction), and a settled job whose intent later runs read.await editRequest(engine, await id('old-facts-failed'), `operation='submit_job',state='failed',error_code='example_failure',intent=intent||'{"kind":"managed_conversation_facts"}'::jsonb`);await editRequest(engine, await id('old-job-committed'), `operation='submit_job',intent=intent||'{"kind":"managed_atom_page"}'::jsonb`);+ // A subagent's own page write: a deferred synthesis postprocess compares its committed content with the page.+ await editRequest(engine, await id('old-subagent-committed'), `authority=authority||'{"restrictedNamespace":true}'::jsonb`);await age(engine, 10);await raw(engine, "UPDATE persistence_requests SET completed_at=now()-interval '2 days' WHERE slug='young-committed'");// Under the target (the default) nothing inside the 30-day retention is touched.@@ -119,6 +122,7 @@ for (const kind of testBackends()) {expect(rows['old-sync-failed']).toMatchObject({ compacted: true, has_intent: false });expect(rows['old-facts-failed']).toMatchObject({ compacted: false, has_intent: true });expect(rows['old-job-committed']).toMatchObject({ compacted: false, has_intent: true });+ expect(rows['old-subagent-committed']).toMatchObject({ compacted: false, has_intent: true });expect(rows['old-failed']).toMatchObject({ compacted: false, has_intent: true });expect(rows['old-conflict']).toMatchObject({ compacted: false, has_intent: true });expect(rows['young-committed']).toMatchObject({ compacted: false, has_intent: true });@@ -126,7 +130,7 @@ for (const kind of testBackends()) {check('the retention pass keeps a refused caller write that failed-writes can still replay until a later request resolves it', async engine => {const requests = await requestFixture(engine);- for (const slug of ['drifted', 'guarded', 'resolved', 'other-failure']) await requests.publish(await requests.admit(slug));+ for (const slug of ['drifted', 'guarded', 'resolved', 'other-failure', 'no-code-failure']) await requests.publish(await requests.admit(slug));await settleEffects(engine);const id = async (slug: string) => (await engine.executeRaw<{ id: string }>('SELECT id FROM persistence_requests WHERE slug=$1 ORDER BY sequence LIMIT 1', [slug]))[0]!.id;// Refusals `gbrain repair failed-writes` replays: a file/database drift and a writer guard on a caller's own page write.@@ -135,6 +139,8 @@ for (const kind of testBackends()) {await editRequest(engine, await id('resolved'), "state='conflict',error_code='source_changed'");// A refusal failed-writes does not replay.await editRequest(engine, await id('other-failure'), "state='failed',error_code='example_failure'");+ // A refusal with no recorded code is not one failed-writes replays either (the predicate is NULL, not true).+ await editRequest(engine, await id('no-code-failure'), "state='failed',error_code=NULL");await age(engine, 40);// A later committed write of the page resolves its refusal.const later = await requests.admit('resolved');@@ -148,6 +154,7 @@ for (const kind of testBackends()) {expect(first('guarded')).toMatchObject({ compacted: false, has_intent: true });expect(first('resolved')).toMatchObject({ compacted: true });expect(first('other-failure')).toMatchObject({ compacted: true });+ expect(first('no-code-failure')).toMatchObject({ compacted: true });// Explicitly dropping it stays the operator's call (gbrain repair receipt-compaction --include-failures).});@@ -214,6 +221,25 @@ for (const kind of testBackends()) {expect((await checkPersistenceCapacity(engine)).status).toBe('warn');});+ check('a housekeeping error still runs the reservation sweep and records the pass', async engine => {+ const requests = await requestFixture(engine);+ await requests.publish(await requests.admit('page'));+ await raw(engine, "INSERT INTO config (key,value) VALUES ($1,'not-a-number') ON CONFLICT (key) DO UPDATE SET value=EXCLUDED.value", [RECEIPT_TARGET_KEY]);+ // Past the hourly record interval, so the pass records even though it compacted nothing.+ const pass = await runCompactionPass(engine, { hostId: 'host-a', now: Date.now() + 2 * 60 * 60_000 });+ expect(pass.skipped_by_reason).toMatchObject({ housekeeping_error: 1 });+ expect(JSON.parse((await engine.getConfig(LAST_COMPACTION_KEY))!)).toMatchObject({ host_id: 'host-a', skipped_by_reason: { housekeeping_error: 1 } });+ });++ check('writer status lists only the limits that are set, never an unset cap as null', async engine => {+ const listed = async () => JSON.parse(JSON.stringify((await readWriterDiagnostics(engine)).limits)) as Record<string, unknown>;+ const limits = await listed();+ expect(Object.values(limits).every(value => typeof value === 'number')).toBe(true);+ expect(limits).not.toHaveProperty('brainTerminalBytes');+ await engine.setConfig('persistence.limits.brain_terminal_bytes', '5000000');+ expect(await listed()).toMatchObject({ brainTerminalBytes: 5_000_000 });+ });+check('the reconciliation backup listing names no count bound while the id cap is unset, and the cap once set', async engine => {const authority = { version: 1, principal: { kind: 'cli', id: 'opt-in-caps' }, remote: false, sourceId: 'default' } as unknown as WriteAuthority;const listing = async () => JSON.parse(JSON.stringify(await manageReconcileBackups(engine, authority, 'people/example', { action: 'list' })));