Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
v0.60.44.0 managed Postgres sync drains a backlog in one run with bulk publication (#5984)
garrytan04d6c71f6806
diff --git a/.claude-plugin/plugin.json b/.claude-plugin/plugin.jsonindex 794cf0e51b..87bd401c6e 100644--- a/.claude-plugin/plugin.json+++ b/.claude-plugin/plugin.json@@ -1,6 +1,6 @@{"name": "gbrain",- "version": "0.60.41.0",+ "version": "0.60.44.0","description": "Personal knowledge brain for your coding agent — hybrid search, synthesis, graph traversal, and durable cross-session memory over Postgres/PGLite with pgvector, plus a curated brain-first skill set.","author": {"name": "Garry Tan",diff --git a/.codex-plugin/plugin.json b/.codex-plugin/plugin.jsonindex 36933d128d..b1da40391c 100644--- a/.codex-plugin/plugin.json+++ b/.codex-plugin/plugin.json@@ -1,6 +1,6 @@{"name": "gbrain",- "version": "0.60.41.0",+ "version": "0.60.44.0","description": "Personal knowledge brain for your coding agent — hybrid search, synthesis, graph traversal, and durable cross-session memory over Postgres/PGLite with pgvector, plus a curated brain-first skill set.","author": {"name": "Garry Tan",diff --git a/BOOTSTRAP_FOR_AGENTS.md b/BOOTSTRAP_FOR_AGENTS.mdindex 6e9f5e3a8d..6310a01c76 100644--- a/BOOTSTRAP_FOR_AGENTS.md+++ b/BOOTSTRAP_FOR_AGENTS.md@@ -1,4 +1,4 @@-<!-- gbrain-runbook-stamp: 0.60.41.0 -->+<!-- gbrain-runbook-stamp: 0.60.44.0 --><!-- This stamp must equal the VERSION file at every release; CI enforces it(scripts/check-bootstrap-tag.sh). `gbrain bootstrap status` compares it tothe installed binary and warns on skew. -->diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 763cd33a9d..eb5dd16372 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -10,6 +10,75 @@ credits are retained; no result has been reassigned to another provider. Originaidentifiers and attribution are available in the pre-removal Git revision`6040075c6cb95be5881cc2e1b76ef7d71f4e5d29` (retained on 2026-09-23).+## [0.60.44.0] - 2026-10-04++**A managed Postgres brain now catches up a big sync backlog in one `gbrain sync` run, about 4.6 times faster, and the run tells your agent exactly what happened and what to do next (#5984).**++On a brain with managed writes and a database across the internet, catching up thousands of changed files used to crawl: each `gbrain sync` saved one page, then quit, so people looped it in a shell, and a 9,400-file backlog projected to about two days. Now one run keeps going until the backlog is done. It prints progress and an ETA, skips work that changes nothing, and saves pages in groups while every page keeps its own write receipt. On a test rig with 57 ms to the database, the same 10,000-file backlog drops from about 49 hours to about 11, and the run ends with one clear verdict: done, safe to rerun, or blocked with the fix.++### How to use it++```bash+gbrain sync --source <id> --no-pull --json+```++The JSON ends with `outcome` (`synced`, `resumable` or `blocked`) and, unless it is done, `next: { command, safe_to_loop, eta_seconds, why }`. `gbrain doctor` and `gbrain sources status` show a managed backlog's remaining pages and ETA from any process. Bulk groups are on by default on Postgres; turn them off with `--no-bulk`, `GBRAIN_SYNC_BULK=0` or `gbrain config set sync.bulk false`.++### The numbers that matter++| 500-file backlog, 57 ms to the database | Before | Now |+| --- | --- | --- |+| Pages per minute | 3.4 (shell loop) | 13 to 16 (one run) |+| 10,000-file backlog | about 49 h | about 11 h |+| `sync --all`, two sources | 0 pages in 15 min | 16 pages/min |+| A foreground write during catch-up (p95) | 29.6 s, 15 failed catch-up runs | 15.7 s (15.0 s idle), no failures |+| Same backlog next to the database | 350 pages/min | 775 pages/min |++### Things to watch++- A managed sync that stops at its deadline with a write still pending now exits 0 as `resumable` instead of 1. Rerun the same command; accepted writes keep their request IDs.+- Managed sources under `sync --all` drain one at a time.+- 150 pages/min at 57 ms is not reached. Each page's database write is still a chain of about 40 dependent statements; reaching it needs pages to publish in parallel, which is a separate design.++## To take advantage of v0.60.44.0++`gbrain upgrade` should do this automatically. If it didn't, or if `gbrain doctor` warns about a partial migration:++1. **Run the orchestrator manually:**+ ```bash+ gbrain apply-migrations --yes+ ```+2. **Nothing else to migrate.** There is no schema change; the new behavior applies to the next `gbrain sync`.+3. **Verify the outcome:**+ ```bash+ gbrain sync --source <id> --no-pull --json+ gbrain sources status+ ```+4. **If any step fails or the numbers look wrong,** please file an issue:+ https://github.com/garrytan/gbrain/issues with:+ - output of `gbrain doctor`+ - contents of `~/.gbrain/upgrade-errors.jsonl` if it exists+ - which step broke++ This feedback loop is how the gbrain maintainers find fragile upgrade paths. Thank you.++### Itemized changes++- **Drain** (`src/core/persistence/sync-drain.ts`): `runDrain` re-enters the single-pass managed sync until the cursor is done, the caller's signal or `--timeout` stops it, a strict run deadline is about 15 s away (`registerRunDeadline`, armed by the sync watchdog), a writer head needs intervention, or the awaited write and its worktree head make no progress for 30 s and 3 passes. It retries only `worktree_refreshing`, admission contention, `database_contention`, connection errors and statement timeouts. Every page notes forward progress, so the progress-aware watchdog never stops a progressing run. A drain waits up to 30 s per page instead of re-entering. The CLI single-source, `--all`, `--watch` and the PGLite owner delegate use it; `SyncResult.drain` carries `outcome`, `stop_reason`, `written`, `waived`, `remaining`, `rate_pages_per_min`, `eta_seconds`, `stall` and `bulk`; `--json` adds `outcome` and `next` (`syncResumeCommand` keeps brain, source and cursor options).+- **Stop reasons** are catalogued in `docs/guides/write-refusals.md#managed-sync-drain-stops`: `deadline`, `drain_stalled`, `database_contention`, writer-blocked (`recovery_required`, `owner_unavailable`, unexpected bytes) and `blocked_by_failures`.+- **Backlog estimate**: the managed cursor stamps a drain window (`progress`); `readManagedSyncBacklog` feeds the new `managed_sync_backlog` doctor check and `sources status` (`managed_backlog` in `--json`).+- **Waivers** (`src/core/persistence/sync-waivers.ts`): a delete of a page already soft-deleted at the frozen revision, with its file gone, advances without a request, under the page guard and cursor lock and only when no unfinished request touches the page. Unchanged imports moved here. Both report as `waived: { imports, deletes }`; `GBRAIN_SYNC_WAIVE_NOOP=0` turns them off.+- **Waits** (`awaitWrite`): the publishing process hands the finished row to its waiters without a read; other waits poll a narrow select with backoff and classify the end as `terminal`, `pending`, `blocked` (with `gbrain sources writer status <source> --json`) or `read_failed`.+- **Bulk publication** (`sync-group.ts`, `group-publish.ts`, `admitWriteGroupInTransaction`, `claimGroupFollowers`): groups of up to `sync.bulk_size` (16) consecutive page imports and deletes, sized to `sync.bulk_max_txn_ms` (15 s); one admission transaction, one publication transaction with per-page authorization, validation, attribution (`setMemberAttribution`), effects and receipts; counters locked after the pages are applied; no group forms while a foreground write is queued. A failure rolls the group back, pages publish singly, and later pages are cancelled.+- **Round trips**: parameterized `executeRaw` and page snapshots use prepared statements wherever the connection allows them (PgBouncer transaction pooling unchanged; `GBRAIN_PREPARE=false` still forces unprepared). `lockCounters` and multi-key `lockPageKeys` are set-based; Postgres transactions track held page guards like PGLite; publication completes its locked request without relocking; recovery cleanup runs only after a recovery record; the protocol declaration, shared skillpack roots and source-wide sync validation are read once per transaction.+- **Connectors** wait for a pending withdrawal mirror of a page before freezing its file's before-image, so a re-render no longer conflicts with a just-committed fact withdrawal.+- `--no-pull` help now says it is required on managed brains; `sync --help` documents the drain, `--timeout` and `--no-bulk`.++### For contributors++- `scripts/bench/managed-sync-catchup.ts` (opt-in, Docker + toxiproxy) reproduces the issue at any RTT; `GBRAIN_SQL_TRACE=<file>` records every database round trip at the socket. Method, baseline and results are in `docs/eval/managed-sync-catchup.md`.+- Goldens updated for prepared raw statements and set-based guards (`test/fixtures/goldens/sql-text`, `postgres-engine-gauge`, `exports`, `doctor`).+## [0.60.41.0] - 2026-10-04**A keyword-only `query` whose answer is in the top five now grades `moderate` again when the question merely uses a word the brain never writes, while questions about a missing attribute or an unknown company still grade `weak` (#5919). Conversation pages can set their own segmentation gap (#5918).**diff --git a/VERSION b/VERSIONindex 1889bf9064..cf91026bc4 100644--- a/VERSION+++ b/VERSION@@ -1 +1 @@-0.60.41.0+0.60.44.0diff --git a/docs/eval/managed-sync-catchup.md b/docs/eval/managed-sync-catchup.mdindex f6477971d9..5b48aa001e 100644--- a/docs/eval/managed-sync-catchup.md+++ b/docs/eval/managed-sync-catchup.md@@ -11,6 +11,12 @@ takes 77 to 83. By the CEO-A1 decision rule, the measured numberscall for multi-page (bulk) publication after the round-trip diet;pipelined admission alone cannot reach the target.+With the drain loop, the diet and bulk publication (v0.60.44.0), one+`gbrain sync` catches up at 13 to 16 pages/min at 57 ms (the 10k+backlog in about 11 h instead of 49 h) and 775 pages/min near the+database, with foreground writes unharmed. See+[Results on the #5984 branch](#results-on-the-5984-branch-v060440).+This page is the research and decision record for the #5984 plan(CEO-A1, A2, A3, A4, A6, A17, A19, A25, A26, A27, A29; ENG-A5, A9,A11, A14, A15, A18; DX-A12, A13). The bench is opt-in and never runs@@ -414,26 +420,70 @@ the describe fix and the counter-lock order. Then measure, and expectthe rule to select 2.3. Skip 2.2 unless post-2.1 publication is400 ms or less per page.-## Post-Phase-1 rows (placeholder)--Root thread: rerun the two commands above on the integrated branch and-fill this table. It covers the drain loop, waivers, narrow waits and-the waived counts in `entries_waived`.--| Row | RTT | Pages/min | Per-page p50 | RT per page | Notes |-|---|---|---|---|---|---|-| `cli` | 57 | _TBD_ | | | |-| `reentry` | 57 | _TBD_ | | | |-| `foreground` | 57 | _TBD_ | | | p95 delta, `55P03` count |-| `all` | 57 | _TBD_ | | | must be > 0 |-| `cli` 10k | 57 | _TBD_ | | | ship-gate row |-| `cli` | ~0 | _TBD_ | | | |--## Post-diet (2.1) and Phase 2 rows (placeholder)--| Row | RTT | Pages/min | Per-page p50 | RT per page | Mechanism selected |-|---|---|---|---|---|---|-| `cli` | 57 | _TBD_ | | | |-| `cli` 10k | 57 | _TBD_ | | | |-| `foreground` | 57 | _TBD_ | | | |-| `effects` | 57 | _TBD_ | | | retrieval-ready time |+## Results on the #5984 branch (v0.60.44.0)++Same bench, same 500-file backlog (534 entries), 57 ms rows time-boxed+at 15 min. "This branch" is one `gbrain sync` run, which now drains the+whole backlog; master is the reporter's CLI loop.++| Row | RTT | Master | This branch | Notes |+|---|---|---|---|---|+| `cli` | 57 ms | 3.4 pages/min | 13.1 pages/min (15.7 with a 30 s group budget) | default group budget 15 s |+| `cli` 10k files, 300-word pages, receipt history | 57 ms | 3.4 pages/min, ≈ 49 h | 15.7 pages/min, ≈ 10.7 h (30 s budget) | ship-gate row; ≈ 4.6x |+| `all`, two sources | 57 ms | 0 pages in 15 min | 16 pages/min | sources drain one at a time |+| `foreground`, a `put_page` every second | 57 ms | p95 19.7 s idle, 29.6 s during; 15 of 73 runs failed | p95 15.0 s idle, 15.7 s during; 0 failures, 0 lock timeouts | catch-up yields: 4.2 pages/min while writes keep arriving |+| `cli` | ~0 ms | 350 pages/min | 775 pages/min | |+| `reentry` | ~0 ms | 345 pages/min | 673 pages/min | |++Round trips per committed page (all processes, traced) fell from 258 to+about 155 to 180, and the per-page publication cost inside a bulk group+is about 40 round trips.++### What was built, and what the rule selected++1. **Phase 1.** The drain loop (one run catches up, with an outcome,+ `next`, progress and ETA), the no-op delete and import waivers, and+ classified waits with in-process completion handoff.+2. **2.1 round-trip diet.** Parameterized `executeRaw` statements are+ prepared (the describe round trip is gone wherever the connection+ allows prepared statements; PgBouncer transaction pooling is+ unchanged). Counter and multi-key page-guard locks are set-based.+ Postgres transactions remember the page guards they hold. Publication+ completes its locked request without relocking. Recovery cleanup is+ skipped when there was no recovery record. The protocol declaration,+ shared skillpack roots and the source-wide part of sync validation+ are read once per transaction.+3. **2.2 pipelined admission: skipped.** Post-diet publication still+ costs about 2 s per page at 57 ms, far above the 400 ms the rule+ requires (CEO-A9).+4. **2.3 bulk publication: built** (CEO-A1, ENG-A3/A4/A5). A draining+ sync freezes a group of up to 16 consecutive page imports and+ deletes, admits them in one transaction, and the writer publishes the+ group in one transaction. Each page keeps its own request row,+ receipt, authorization, attribution (the database stamps each page's+ own request) and effects. A failure rolls the group back; pages then+ publish singly, so the failure names its page and later pages are+ cancelled. Counters are locked after the members are applied, and no+ group forms while a foreground write is queued, so foreground writes+ stay within about 1 s of their idle p95.++### Shortfall against 150 pages/min++The branch reaches 13 to 16 pages/min at 57 ms, not 150. Inside a group,+each page still runs a chain of about 40 dependent statements (snapshot,+validation, apply, chunks, projections, effects, receipt), one after+another, because the database stamps each page's write with its own+request through transaction-local settings. Running members concurrently+inside one transaction would mix those stamps. Two mechanisms could reach+the target, and both need their own design approval:++- **Concurrent publication of independent pages,** each in its own+ transaction on its own connection, with ordering enforced at commit+ instead of by the per-worktree FIFO claim. Eight concurrent publishers+ at about 2 s per page would give roughly 200 pages/min at 57 ms.+- **Set-based apply** for a group (one statement per table for all of a+ group's pages), which removes the per-page chain.++The worktree fence + bulk import + reconcile alternative (CEO-A27) stays+unbuilt: it measures 51 pages/min as unmanaged sync but gives up+per-page receipts and effects during the import.diff --git a/llms-full.txt b/llms-full.txtindex ba91e760c9..0ca8471df6 100644--- a/llms-full.txt+++ b/llms-full.txt@@ -4878,7 +4878,7 @@ transaction, and the writer publishes the group in one transaction. Everypage still gets its own write request, receipt, attribution and failurereport; if one page fails, the pages before it commit, that page is reported,and the pages after it are cancelled and re-frozen once it is fixed. Group size-adapts so a group takes about `sync.bulk_max_txn_ms` (default 30 s); a+adapts so a group takes about `sync.bulk_max_txn_ms` (default 15 s); aforeground write waits behind at most one group. Turn bulk off with`--no-bulk`, `GBRAIN_SYNC_BULK=0` or `gbrain config set sync.bulk false`. Thefinal JSON reports `drain.bulk` (`enabled`, `reason` when off, `groups`,@@ -4912,7 +4912,7 @@ Timing knobs the drain uses:| `GBRAIN_SYNC_STALL_ABORT_SECONDS` | Progress window for the deadline above. | 900 | The watchdog stops the run and prints the resume command. || No-progress detector | Awaited write and checkout head unchanged. | 30 s and 3 passes | Stops as `blocked` / `drain_stalled` with diagnostics. || Page write wait | One page's (or group's) publication before the drain re-checks. | 30 s inside a drain (5 s, checkpoint 8 s, for a single pass) | The drain re-enters; this is not a stop. |-| `sync.bulk_max_txn_ms` / `GBRAIN_SYNC_BULK_MAX_TXN_MS` | Target time per bulk group; sizes the next group from the last one's time per page. | 30000 | A smaller next group. |+| `sync.bulk_max_txn_ms` / `GBRAIN_SYNC_BULK_MAX_TXN_MS` | Target time per bulk group; sizes the next group from the last one's time per page. | 15000 | A smaller next group. || `sync.bulk_size` / `GBRAIN_SYNC_BULK_SIZE` | Largest bulk group. | 16 | — |Two more tips:diff --git a/openclaw.plugin.json b/openclaw.plugin.jsonindex 0cc22502e6..c6c8aa1d8c 100644--- a/openclaw.plugin.json+++ b/openclaw.plugin.json@@ -1,7 +1,7 @@{"id": "gbrain-context-engine","name": "gbrain",- "version": "0.60.41.0",+ "version": "0.60.44.0","description": "Personal knowledge brain with Postgres + pgvector hybrid search","family": "bundle-plugin","kind": "context-engine",diff --git a/package.json b/package.jsonindex 94e3d59ac8..7367b7e049 100644--- a/package.json+++ b/package.json@@ -207,7 +207,7 @@"bun": ">=1.4.0"},"license": "MIT",- "version": "0.60.41.0",+ "version": "0.60.44.0","overrides": {"@ai-sdk/provider-utils": "4.0.56","@hono/node-server": "^2.0.5",diff --git a/plugin-variants/gbrain-coding/.claude-plugin/plugin.json b/plugin-variants/gbrain-coding/.claude-plugin/plugin.jsonindex 9bbe51034a..c6c8c05086 100644--- a/plugin-variants/gbrain-coding/.claude-plugin/plugin.json+++ b/plugin-variants/gbrain-coding/.claude-plugin/plugin.json@@ -1,6 +1,6 @@{"name": "gbrain-coding",- "version": "0.60.41.0",+ "version": "0.60.44.0","description": "Brain-first coding agent working inside a repo: retrieval, routing, ingest discipline, correction hygiene. Default persona for the claude-code harness bridge; also published as the gbrain-coding marketplace variant. (persona variant of the gbrain plugin — 21 skills)","author": {"name": "Garry Tan",diff --git a/plugin-variants/gbrain-coding/.codex-plugin/plugin.json b/plugin-variants/gbrain-coding/.codex-plugin/plugin.jsonindex 13606a70e8..5b695020db 100644--- a/plugin-variants/gbrain-coding/.codex-plugin/plugin.json+++ b/plugin-variants/gbrain-coding/.codex-plugin/plugin.json@@ -1,6 +1,6 @@{"name": "gbrain-coding",- "version": "0.60.41.0",+ "version": "0.60.44.0","description": "Brain-first coding agent working inside a repo: retrieval, routing, ingest discipline, correction hygiene. Default persona for the claude-code harness bridge; also published as the gbrain-coding marketplace variant. (persona variant of the gbrain plugin — 21 skills)","author": {"name": "Garry Tan",diff --git a/plugin-variants/gbrain-coding/README.md b/plugin-variants/gbrain-coding/README.mdindex fe84a490c6..f6d809db19 100644--- a/plugin-variants/gbrain-coding/README.md+++ b/plugin-variants/gbrain-coding/README.md@@ -1,4 +1,4 @@-<!-- gbrain-plugin-tree-stamp: 0.60.41.0 -->+<!-- gbrain-plugin-tree-stamp: 0.60.44.0 --># gbrain-coding (generated persona variant — do not hand-edit)Brain-first coding agent working inside a repo: retrieval, routing, ingest discipline, correction hygiene. Default persona for the claude-code harness bridge; also published as the gbrain-coding marketplace variant.diff --git a/plugin-variants/gbrain-daily/.claude-plugin/plugin.json b/plugin-variants/gbrain-daily/.claude-plugin/plugin.jsonindex 371f5601a1..6ca75a4ee2 100644--- a/plugin-variants/gbrain-daily/.claude-plugin/plugin.json+++ b/plugin-variants/gbrain-daily/.claude-plugin/plugin.json@@ -1,6 +1,6 @@{"name": "gbrain-daily",- "version": "0.60.41.0",+ "version": "0.60.44.0","description": "Personal knowledge-brain daily use: meetings, tasks, briefings, reading, research. Published as the gbrain-daily marketplace variant. (persona variant of the gbrain plugin — 20 skills)","author": {"name": "Garry Tan",diff --git a/plugin-variants/gbrain-daily/.codex-plugin/plugin.json b/plugin-variants/gbrain-daily/.codex-plugin/plugin.jsonindex 05cee2749f..e00012b411 100644--- a/plugin-variants/gbrain-daily/.codex-plugin/plugin.json+++ b/plugin-variants/gbrain-daily/.codex-plugin/plugin.json@@ -1,6 +1,6 @@{"name": "gbrain-daily",- "version": "0.60.41.0",+ "version": "0.60.44.0","description": "Personal knowledge-brain daily use: meetings, tasks, briefings, reading, research. Published as the gbrain-daily marketplace variant. (persona variant of the gbrain plugin — 20 skills)","author": {"name": "Garry Tan",diff --git a/plugin-variants/gbrain-daily/README.md b/plugin-variants/gbrain-daily/README.mdindex 6787ab62ad..c14d960a82 100644--- a/plugin-variants/gbrain-daily/README.md+++ b/plugin-variants/gbrain-daily/README.md@@ -1,4 +1,4 @@-<!-- gbrain-plugin-tree-stamp: 0.60.41.0 -->+<!-- gbrain-plugin-tree-stamp: 0.60.44.0 --># gbrain-daily (generated persona variant — do not hand-edit)Personal knowledge-brain daily use: meetings, tasks, briefings, reading, research. Published as the gbrain-daily marketplace variant.diff --git a/plugin/README.md b/plugin/README.mdindex 3f011cce54..29d5cfa11c 100644--- a/plugin/README.md+++ b/plugin/README.md@@ -1,4 +1,4 @@-<!-- gbrain-plugin-tree-stamp: 0.60.41.0 -->+<!-- gbrain-plugin-tree-stamp: 0.60.44.0 --># gbrain plugin skill tree (generated — do not hand-edit)This tree is the curated skill set for the gbrain Codex and Claude Codediff --git a/templates/bootstrap/template-repo/README.md b/templates/bootstrap/template-repo/README.mdindex 25c0c89f98..1d1bc359b5 100644--- a/templates/bootstrap/template-repo/README.md+++ b/templates/bootstrap/template-repo/README.md@@ -1,6 +1,6 @@# gbrain agent workspace — template-<!-- gbrain-template-stamp: 0.60.41.0 -->+<!-- gbrain-template-stamp: 0.60.44.0 -->This repository is the **"Use this template"** distribution artifact for a[gbrain](https://github.com/garrytan/gbrain) personal-agent workspace — the same