Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

sync: the lease-holder-only claim reads the drain lease holder in its own claim statement (the separate lease read cost the #6007 budget a prepare statement)

garrytancb1e05c68309
diff --git a/docs/guides/live-sync.md b/docs/guides/live-sync.md
index b6d6321bac..2a161191e7 100644
--- a/docs/guides/live-sync.md
+++ b/docs/guides/live-sync.md
@@ -115,7 +115,7 @@ Timing knobs the drain uses:
| `preparation_deadlines` write switch (`persistence.preparation_deadlines`, `GBRAIN_PREPARATION_DEADLINES`) | Kill switch for the four keys above and the attempt counter. `remember`, `put_page` and `edit_page` keep their own 30 s budget either way. | on | `gbrain config set persistence.preparation_deadlines false` (or `GBRAIN_PREPARATION_DEADLINES=0`) restores the previous behaviour exactly: no deadline on sync or maintenance writes, no counter, no `preparation_stalled` holds. A running `serve` picks the change up within 5 seconds. |
| 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` job deadline | A managed `sync` Minions job's drain: its `timeout_ms` (autopilot stamps `max(2 × --interval, 300 s)`), or the queue's wall-clock limit for a job without one, capped at an hour, less a 60 s reserve. `sync.job_drain=false` (or job data `drain: false`) keeps the single pass. | per job | Stops as `resumable` / `deadline`; on a worker-backed brain a stop that is safe to repeat queues one follow-up job (`continuation_job_id`). |
-| `GBRAIN_SYNC_BULK_MAX_TXN_MS` (was `sync.bulk_max_txn_ms`, retired in 0.60.265.0) | Target time per bulk group; sizes the next group from the last one's time per page. | 15000 | A smaller next group. |
+| `GBRAIN_SYNC_BULK_MAX_TXN_MS` (was `sync.bulk_max_txn_ms`, retired in 0.60.266.0) | 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 | — |
### Sync config keys
@@ -128,7 +128,7 @@ Timing knobs the drain uses:
`sync.federated_v2`, `sync.waive_batch`, `sync.bulk`. Each one set to `false` restores the earlier behavior, and
stays honored until at least 2027-01-14 (`CONFIG_ROLLBACK_WINDOWS` in `src/core/wire-skew.ts`). They are not
tunables: `config set` says so, and no help text or printed fix recommends them.
-- **Retired keys**, no longer read since 0.60.265.0, each with an environment variable for the process that syncs
+- **Retired keys**, no longer read since 0.60.266.0, each with an environment variable for the process that syncs
(the serve when it runs the catch-up, the cron line or the shell):
| Retired key | Replacement | Default |
diff --git a/docs/guides/repair.md b/docs/guides/repair.md
index 4a1f54fad9..31004ce4d8 100644
--- a/docs/guides/repair.md
+++ b/docs/guides/repair.md
@@ -1466,7 +1466,7 @@ reference: [`preparation_stalled`](write-refusals.md#preparation_stalled).
Settings, all read by every sync (`sync.holds` is a brain config key; the `GBRAIN_SYNC_*` variables are read from the
environment of the process that syncs and replace the `sync.hold_*` and `sync.parser_regression` keys retired in
-0.60.265.0, see [sync config keys](live-sync.md#sync-config-keys)):
+0.60.266.0, see [sync config keys](live-sync.md#sync-config-keys)):
| Setting | Default | Effect |
| --- | --- | --- |
diff --git a/llms-full.txt b/llms-full.txt
index ba793bad8b..94b039eb17 100644
--- a/llms-full.txt
+++ b/llms-full.txt
@@ -6719,7 +6719,7 @@ Timing knobs the drain uses:
| `preparation_deadlines` write switch (`persistence.preparation_deadlines`, `GBRAIN_PREPARATION_DEADLINES`) | Kill switch for the four keys above and the attempt counter. `remember`, `put_page` and `edit_page` keep their own 30 s budget either way. | on | `gbrain config set persistence.preparation_deadlines false` (or `GBRAIN_PREPARATION_DEADLINES=0`) restores the previous behaviour exactly: no deadline on sync or maintenance writes, no counter, no `preparation_stalled` holds. A running `serve` picks the change up within 5 seconds. |
| 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` job deadline | A managed `sync` Minions job's drain: its `timeout_ms` (autopilot stamps `max(2 × --interval, 300 s)`), or the queue's wall-clock limit for a job without one, capped at an hour, less a 60 s reserve. `sync.job_drain=false` (or job data `drain: false`) keeps the single pass. | per job | Stops as `resumable` / `deadline`; on a worker-backed brain a stop that is safe to repeat queues one follow-up job (`continuation_job_id`). |
-| `GBRAIN_SYNC_BULK_MAX_TXN_MS` (was `sync.bulk_max_txn_ms`, retired in 0.60.265.0) | Target time per bulk group; sizes the next group from the last one's time per page. | 15000 | A smaller next group. |
+| `GBRAIN_SYNC_BULK_MAX_TXN_MS` (was `sync.bulk_max_txn_ms`, retired in 0.60.266.0) | 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 | — |
### Sync config keys
@@ -6732,7 +6732,7 @@ Timing knobs the drain uses:
`sync.federated_v2`, `sync.waive_batch`, `sync.bulk`. Each one set to `false` restores the earlier behavior, and
stays honored until at least 2027-01-14 (`CONFIG_ROLLBACK_WINDOWS` in `src/core/wire-skew.ts`). They are not
tunables: `config set` says so, and no help text or printed fix recommends them.
-- **Retired keys**, no longer read since 0.60.265.0, each with an environment variable for the process that syncs
+- **Retired keys**, no longer read since 0.60.266.0, each with an environment variable for the process that syncs
(the serve when it runs the catch-up, the cron line or the shell):
| Retired key | Replacement | Default |
diff --git a/skills/migrations/v0.60.265.0.md b/skills/migrations/v0.60.266.0.md
similarity index 97%
rename from skills/migrations/v0.60.265.0.md
rename to skills/migrations/v0.60.266.0.md
index dd829d03cd..af539901c5 100644
--- a/skills/migrations/v0.60.265.0.md
+++ b/skills/migrations/v0.60.266.0.md
@@ -1,4 +1,4 @@
-# Upgrade to v0.60.265.0: retired sync config keys move to environment variables
+# Upgrade to v0.60.266.0: retired sync config keys move to environment variables
Five `sync.*` config keys are no longer read: `sync.bulk_max_txn_ms`, `sync.hold_cap`,
`sync.hold_escalate_count`, `sync.hold_escalate_pct` and `sync.parser_regression`. Each one is now an environment
diff --git a/skills/skills.lock.json b/skills/skills.lock.json
index 30db1a5d20..9462b04f67 100644
--- a/skills/skills.lock.json
+++ b/skills/skills.lock.json
@@ -158,7 +158,7 @@
"migrations/v0.60.11.0.md": "1e503113f591c5cd142b78ab4472bbdbceb890c6cbac2fbdf8aa61cd087200b8",
"migrations/v0.60.20.0.md": "9266863a9f76164c64a72b1be17b8ddb3bb97e1c3c5db9f666b69ebc7a228fdd",
"migrations/v0.60.26.0.md": "30d761ef5a2361bad66053f97b7b659304a591274be33e281f4f6df0d7d5566e",
- "migrations/v0.60.265.0.md": "688c2f451d21442046829a0a0ccd33426af6dea8e4e05871372c57bbd5a33582",
+ "migrations/v0.60.266.0.md": "570f58cd3ff0ba759fc034362d3eadcbeaaeb909e34c881145a8fb1dbf881b53",
"migrations/v0.60.27.0.md": "1c3ac6a23ccfce8ad299457799b02958f7d21fcd967c471634f73bdb66f2b603",
"migrations/v0.60.28.0.md": "f7fa936066fb188f6f3f54bb6c720f57743792e4371d0b98dd3cb5a57d1bd9c9",
"migrations/v0.60.30.0.md": "2dd24bb319cad80ae15c52fb163c704fc3adfc1182d44ab563f636f07434a9e0",
diff --git a/src/core/error-registry.ts b/src/core/error-registry.ts
index c9e0f39a3a..a255cda7dd 100644
--- a/src/core/error-registry.ts
+++ b/src/core/error-registry.ts
@@ -201,7 +201,7 @@ export const CODES = {
why: "A renewed lease is not progress: the detector keys on the head claim's phase and step. With a live owner on this host the drain prints `stalled <N>s on <step>` from the allowance (budget plus 30 s) and keeps going; it stops only when `persistence.preparation_ceiling_ms` passes without the root being released, or when that owner's heartbeat row reads wedged (`cause: owner_wedged_here`, with the owner's kind, pid and nonce and `retry_after_ms` to the ceiling, after which the owner frees the root and the next pass holds the entry). A lapsed claim (`owner_missing`) stops at once unless this host owns the checkout and a live full consumer can reclaim it, which gets one more window. `drain.stall.cause` names which.",
reasons: ['owner_wedged_here', 'owner_missing', 'preparation_overdue', 'publication_overdue', 'no_progress'],
// #6712 PR 2: a pass that admits nothing with no unfinished request stops `no_progress`; an older build wrote `no_admission` for it.
- retired_reasons: { no_admission: '0.60.265.0' },
+ retired_reasons: { no_admission: '0.60.266.0' },
docs: 'docs/guides/write-refusals.md#drain-stalled',
suggestion: 'Inspect the writer with gbrain sources writer status --source <id> --json (read-only: the owner process, its step and what it waits on, and the next action). Before the ceiling (`next.safe_to_loop` true) rerun next.command after retry_after_ms; past it, or with a wedged owner, restart the named owner process on the brain host and rerun the same sync.',
fix: { argv: ['gbrain', 'sources', 'writer', 'status', '--source', '{source_id}', '--json'], consent: [], actor: 'agent', why: "Read-only: names the owner process holding the head write (kind, pid), its step and wait cause, and ends in a next_action.", requires_exclusive: false,
diff --git a/src/core/persistence/journal.ts b/src/core/persistence/journal.ts
index 44f9a1cfd9..bf1fb26a1f 100644
--- a/src/core/persistence/journal.ts
+++ b/src/core/persistence/journal.ts
@@ -629,34 +629,34 @@ export async function hasClaimableWrite(engine: SqlEngine, hostId: string, exclu
/**
* #6712 PR 2 (Garry, 2026-10-16, via GBRA-40): while a source's drain lease is live, only the lease holder publishes that
* source's managed-sync requests. A non-resident consumer (a short-lived command such as `put` or `remember`, waiting on its
- * own write) skips the sync rows of every source whose lease another live process holds, so it never publishes a drainer's
- * pages or breaks up its lane groups. A lease whose same-host holder is provably dead (a killed sync whose TTL has not
- * lapsed yet) does not count, and with no live lease nothing changes: a crashed drainer's rows still drain through any consumer.
+ * own write) ranks a sync row whose source's lease another process holds after every other row and claims it only when that
+ * holder is provably dead (a killed same-host sync whose TTL has not lapsed yet), so it never publishes a live drainer's pages
+ * or breaks up its lane groups. With no live lease nothing changes: a crashed drainer's rows still drain through any consumer.
+ * The holder rides on the claim's own read, so the claim costs no extra statement. $5 the sync lock id prefix, $6 pid, $7 host.
*/
-async function leasedSourcesHeldElsewhere(engine: BrainEngine): Promise<string[]> {
- const prefix = syncLockId('');
- const rows = await engine.executeRaw<{ id: string; holder_pid: number; holder_host: string | null }>(`SELECT id, holder_pid, holder_host FROM gbrain_cycle_locks
- WHERE starts_with(id, $1::text) AND ttl_expires_at>now() AND NOT (holder_pid=$2::integer AND holder_host IS NOT DISTINCT FROM $3::text)`, [prefix, process.pid, hostname()]);
- return rows.filter(row => classifyHolderLiveness(row.holder_pid, row.holder_host ?? '', 0, { graceMs: 0 }) !== 'dead_eligible').map(row => row.id.slice(prefix.length));
-}
+const LEASE_HOLDER = (column: string) => `(SELECT l.${column} FROM gbrain_cycle_locks l WHERE ${SYNC_KIND('r')} AND l.id=$5::text||r.source_id
+ AND l.ttl_expires_at>now() AND NOT (l.holder_pid=$6::integer AND l.holder_host IS NOT DISTINCT FROM $7::text))`;
+type ClaimCandidate = WriteRequest & { lease_holder_pid?: number | null; lease_holder_host?: string | null };
export async function claimNextWrite(engine: BrainEngine, hostId: string, leaseMs = 30_000, excludeRoots: string[] = [], fifoRoots: string[] = [],
laneRoots: string[] = []): Promise<(WriteRequest & { passed_sync?: boolean }) | null> {
const first = `(${foregroundPrioritySql()}) AND NOT (COALESCE(r.worktree_id::text,'db:'||r.source_incarnation::text)=ANY($3::text[]))`;
- const leased = RESIDENT_CONSUMER_KINDS.includes(claimOwnerKind()) ? [] : await leasedSourcesHeldElsewhere(engine);
+ const holderOnly = !RESIDENT_CONSUMER_KINDS.includes(claimOwnerKind());
return engine.transactionDirect(async tx => {
// The protocol declaration and the FIFO head read are sent together.
const [, [row]] = await pipelined(tx, [
() => declarePersistenceProtocol(tx),
- () => tx.executeRaw<WriteRequest>(`SELECT r.* FROM persistence_requests r
+ () => tx.executeRaw<ClaimCandidate>(`SELECT r.*${holderOnly
+ ? `, ${LEASE_HOLDER('holder_pid')} AS lease_holder_pid, ${LEASE_HOLDER('holder_host')} AS lease_holder_host` : ''} FROM persistence_requests r
LEFT JOIN persistence_worktrees w ON w.id=r.worktree_id
WHERE ${claimableWriteSql(`(${first})`, '$4::text[]')}
-- On a root whose lane groups run in this process only a foreground write is claimed (beside them).
- AND NOT (r.worktree_id::text=ANY($4::text[]) AND (${SYNC_KIND('r')} OR NOT (${first})))${leased.length ? `
- AND NOT (${SYNC_KIND('r')} AND r.source_id=ANY($5::text[]))` : ''}
- ORDER BY ${CLAIM_ORDER(`(${first})`)} LIMIT 1 FOR UPDATE OF r SKIP LOCKED`, [hostId, excludeRoots, fifoRoots, laneRoots, ...(leased.length ? [leased] : [])]),
- ]) as [unknown, WriteRequest[]];
+ AND NOT (r.worktree_id::text=ANY($4::text[]) AND (${SYNC_KIND('r')} OR NOT (${first})))
+ ORDER BY ${holderOnly ? `${LEASE_HOLDER('holder_pid')} IS NOT NULL, ` : ''}${CLAIM_ORDER(`(${first})`)} LIMIT 1 FOR UPDATE OF r SKIP LOCKED`,
+ [hostId, excludeRoots, fifoRoots, laneRoots, ...(holderOnly ? [syncLockId(''), process.pid, hostname()] : [])]),
+ ]) as [unknown, ClaimCandidate[]];
if (!row) return null;
+ if (row.lease_holder_pid != null && classifyHolderLiveness(row.lease_holder_pid, row.lease_holder_host ?? '', 0, { graceMs: 0 }) !== 'dead_eligible') return null;
// `passed_sync`: the claimed row went ahead of a queued sync row of its root.
// #6278: the claim stamps its own `preparing` phase, so a kill before the first renewal is charged like any other.
const token = randomUUID();
diff --git a/src/core/sync-config-keys.ts b/src/core/sync-config-keys.ts
index 1ff66397c9..4c8526da63 100644
--- a/src/core/sync-config-keys.ts
+++ b/src/core/sync-config-keys.ts
@@ -25,15 +25,15 @@ export interface RetiredSyncKey {
}
export const RETIRED_SYNC_KEYS = {
- 'sync.bulk_max_txn_ms': { key: 'sync.bulk_max_txn_ms', retired_in: '0.60.265.0', tuned: 'the target time of one bulk group without lanes, in ms',
+ 'sync.bulk_max_txn_ms': { key: 'sync.bulk_max_txn_ms', retired_in: '0.60.266.0', tuned: 'the target time of one bulk group without lanes, in ms',
replacement: { env: 'GBRAIN_SYNC_BULK_MAX_TXN_MS', default: '15000' } },
- 'sync.hold_cap': { key: 'sync.hold_cap', retired_in: '0.60.265.0', tuned: 'how many held files a sync result lists in detail',
+ 'sync.hold_cap': { key: 'sync.hold_cap', retired_in: '0.60.266.0', tuned: 'how many held files a sync result lists in detail',
replacement: { env: 'GBRAIN_SYNC_HOLD_CAP', default: '500' } },
- 'sync.hold_escalate_count': { key: 'sync.hold_escalate_count', retired_in: '0.60.265.0', tuned: 'the held-file count above which a source escalates',
+ 'sync.hold_escalate_count': { key: 'sync.hold_escalate_count', retired_in: '0.60.266.0', tuned: 'the held-file count above which a source escalates',
replacement: { env: 'GBRAIN_SYNC_HOLD_ESCALATE_COUNT', default: '50' } },
- 'sync.hold_escalate_pct': { key: 'sync.hold_escalate_pct', retired_in: '0.60.265.0', tuned: 'the share of a run\'s screened imports above which it escalates, in percent',
+ 'sync.hold_escalate_pct': { key: 'sync.hold_escalate_pct', retired_in: '0.60.266.0', tuned: 'the share of a run\'s screened imports above which it escalates, in percent',
replacement: { env: 'GBRAIN_SYNC_HOLD_ESCALATE_PCT', default: '5' } },
- 'sync.parser_regression': { key: 'sync.parser_regression', retired_in: '0.60.265.0', tuned: 'whether sync stops (stop) or holds the file (hold) when bytes that imported before are refused',
+ 'sync.parser_regression': { key: 'sync.parser_regression', retired_in: '0.60.266.0', tuned: 'whether sync stops (stop) or holds the file (hold) when bytes that imported before are refused',
replacement: { env: 'GBRAIN_SYNC_PARSER_REGRESSION', default: 'stop' } },
'sync.knock_on_refreeze_limit': { key: 'sync.knock_on_refreeze_limit', retired_in: '0.60.255.0', tuned: 'the knock-on re-freeze budget', replacement: null },
'sync.knock_on_run_limit': { key: 'sync.knock_on_run_limit', retired_in: '0.60.255.0', tuned: 'the knock-on re-freeze budget per run', replacement: null },
diff --git a/src/core/wire-skew.ts b/src/core/wire-skew.ts
index 010da5e934..3faaafd1af 100644
--- a/src/core/wire-skew.ts
+++ b/src/core/wire-skew.ts
@@ -47,14 +47,14 @@ export function retiredWireNotice(fields: readonly string[]): string {
export interface ConfigRollbackWindow { key: string; floor: string; honored_until: string; named_in: string; restores: string }
export const CONFIG_ROLLBACK_WINDOWS = {
- 'sync.job_drain': { key: 'sync.job_drain', floor: '0.60.265.0', honored_until: '2027-01-14', named_in: '0.60.233.0 behavior notice (#6706)',
+ 'sync.job_drain': { key: 'sync.job_drain', floor: '0.60.266.0', honored_until: '2027-01-14', named_in: '0.60.233.0 behavior notice (#6706)',
restores: 'single-pass managed `sync` jobs instead of draining to the job deadline' },
- 'sync.foreground_priority': { key: 'sync.foreground_priority', floor: '0.60.265.0', honored_until: '2027-01-14', named_in: 'docs/guides/live-sync.md (#5984)',
+ 'sync.foreground_priority': { key: 'sync.foreground_priority', floor: '0.60.266.0', honored_until: '2027-01-14', named_in: 'docs/guides/live-sync.md (#5984)',
restores: 'first-in-first-out bulk groups instead of letting foreground writes go first' },
- 'sync.federated_v2': { key: 'sync.federated_v2', floor: '0.60.265.0', honored_until: '2027-01-14', named_in: 'skills/migrations/v0.40.5.md',
+ 'sync.federated_v2': { key: 'sync.federated_v2', floor: '0.60.266.0', honored_until: '2027-01-14', named_in: 'skills/migrations/v0.40.5.md',
restores: 'the v0.39 sequential sync --all instead of the federated v2 path' },
- 'sync.waive_batch': { key: 'sync.waive_batch', floor: '0.60.265.0', honored_until: '2027-01-14', named_in: '#5984 behavior notice',
+ 'sync.waive_batch': { key: 'sync.waive_batch', floor: '0.60.266.0', honored_until: '2027-01-14', named_in: '#5984 behavior notice',
restores: 'one waived entry per transaction instead of batched waivers' },
- 'sync.bulk': { key: 'sync.bulk', floor: '0.60.265.0', honored_until: '2027-01-14', named_in: '#5984 behavior notice and docs/guides/live-sync.md',
+ 'sync.bulk': { key: 'sync.bulk', floor: '0.60.266.0', honored_until: '2027-01-14', named_in: '#5984 behavior notice and docs/guides/live-sync.md',
restores: 'one page per transaction on managed Postgres sync instead of bulk groups' },
} as const satisfies Record<string, ConfigRollbackWindow>;
diff --git a/test/error-registry-6317-contract.test.ts b/test/error-registry-6317-contract.test.ts
index c5609d96e9..9f63a33f35 100644
--- a/test/error-registry-6317-contract.test.ts
+++ b/test/error-registry-6317-contract.test.ts
@@ -97,7 +97,7 @@ describe('drain_stalled: the same-host stop has a cause vocabulary and a filled
const entry = CODES.drain_stalled as CodeEntry;
expect(entry.reasons).toEqual(['owner_wedged_here', 'owner_missing', 'preparation_overdue', 'publication_overdue', 'no_progress']);
// #6712 PR 2: the in-pass governor's no-admission stop (#6423) is `no_progress` now; the retired reason keeps its tombstone.
- expect(entry.retired_reasons).toEqual({ no_admission: '0.60.265.0' });
+ expect(entry.retired_reasons).toEqual({ no_admission: '0.60.266.0' });
expect(entry.why).toContain('owner_wedged_here');
expect(entry.why).toContain('retry_after_ms');
expect(entry.why).not.toContain('nothing here can claim it');
diff --git a/test/sync-config-keys-6712.test.ts b/test/sync-config-keys-6712.test.ts
index 8c208a08a4..2a0ac6e707 100644
--- a/test/sync-config-keys-6712.test.ts
+++ b/test/sync-config-keys-6712.test.ts
@@ -62,7 +62,7 @@ test('the three kinds are disjoint; tunables and rollback keys are registered, r
test('config set of a retired key warns, names its replacement and writes nothing', async () => withEnv(CLEAR, async () => {
const set = await config(['set', 'sync.hold_cap', '200']);
expect(set.exit).toBeNull();
- expect(set.err).toContain('sync.hold_cap was retired in 0.60.265.0');
+ expect(set.err).toContain('sync.hold_cap was retired in 0.60.266.0');
expect(set.err).toContain('GBRAIN_SYNC_HOLD_CAP=<value>');
expect(set.err).toContain('nothing was written');
expect(await engine.getConfig('sync.hold_cap')).toBeNull();