#5762·gbrain

0.60.11.0 managed: sync checkpoint can never commit on a large persistence_requests table (validation query takes 35-113s under the coordinator's 5s statement_timeout)

Author: andreineacsuCreated Sep 30, 2026Updated Sep 30, 2026

Environment: gbrain 0.60.11.0 (f8d1e3936), macOS, bun 1.3.14, Postgres 17, managed persistence, serve --http, one default git source plus four Google connector sources.

Summary

On this brain, a managed sync of default can process every entry of its cursor but can never commit its checkpoint. The checkpoint's validation query takes 35 to 113 seconds, while the coordinator runs it under a 5-second statement_timeout. Each attempt times out, is released as database_contention, and is retried roughly every 20 seconds, holding the source-exclusive lock for up to 5 seconds each time.

Observed

gbrain sync --source default --no-pull --retry-failed --no-embed --no-extract (the documented recovery for an earlier revision_conflict cursor, same flags as the stored cursor) re-enumerated the run and processed all entries:

Sync PARTIAL: an accepted write is not committed; last_commit is unchanged.
  write_pending [write_pending]: The write is accepted but not committed; the sync checkpoint has not advanced.
  Source: "default"; slug: "__managed_sync_checkpoint__"; path: null
  Fix: Re-run the same sync options to resume this request. Do not submit a replacement request or skip the pending write.
  Committed counts are cumulative for run d74cea3a-...: added=0, modified=0, deleted=0.

The cursor shows index 960 / total 960. Re-running the same command returns the same message. The checkpoint request (operation=submit_job, slug=__managed_sync_checkpoint__) has alternated between running and queued with blocked_reason=database_contention for over 20 minutes and is still pending. sources status keeps default at the pre-run last_commit.

Cause, as far as I can measure it

The managed_sync_checkpoint apply in src/core/persistence/sync-prepare.ts (around lines 128-134) runs:

sql
SELECT r.id FROM persistence_requests r WHERE r.worktree_id=$1::uuid AND
  (r.recovery IS NOT NULL OR (r.intent->>'runId'=$2 AND r.intent->>'kind' IN ('managed_sync_import','managed_sync_delete') AND r.state<>'committed'
    AND (r.state IN ('queued','running','recovering') OR NOT EXISTS (SELECT 1 FROM persistence_requests committed
      WHERE committed.source_id=r.source_id AND committed.intent->>'runId'=$2 AND committed.intent->>'index'=r.intent->>'index' AND committed.state='committed')))) LIMIT 1
  • persistence_requests here has 512,724 rows (3065 MB); 64,341 of them belong to this worktree.
  • No index covers worktree_id alone or intent->>'runId'. EXPLAIN gives a sequential scan of the whole table with a correlated sequential-scan subplan, estimated cost about 1.7e10.
  • Run standalone and read-only with the real parameters, the query returns no row (nothing actually blocks the checkpoint). It took 1:53 cold and 0:35 warm.
  • The coordinator sets statement_timeout to 5s for this transaction (src/core/persistence/coordinator.ts:193, :299), and a timeout is classified as transient and released as database_contention (coordinator.ts:96).

In this run only one request carries the run id (the checkpoint itself), because all 960 entries were no-ops. So the query scans 3 GB to prove that zero page receipts are incomplete.

Consequence

  • The source's sync checkpoint cannot advance on a brain whose request table has grown this large, and nothing in the retry path changes the outcome.
  • Every retry takes the source-exclusive lock for up to 5 seconds, so ordinary writes to that source contend with it indefinitely.
  • The CLI's own advice is to re-run or wait, and it forbids replacing or skipping the pending write, so the operator has no supported way out.

Expected

The checkpoint validation should stay within the coordinator's statement budget as persistence_requests grows. For example, an index that serves the runId lookup, reading the run's receipts from the immutable sync manifest instead of scanning, or a budget sized for this validation. Alternatively, a documented recovery for a checkpoint that times out.

Related: #5751 (untracked files re-admitted every run burn lifetime IDs, which grows this table), #5353 / #5381 (the --retry-failed recovery that led here).