Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(persistence): a completed effect drops its writer_busy bookkeeping, so receipt bytes do not depend on contention

garrytanada195a1de72
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 58c0f4d3ca..2e202034c6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -36,7 +36,7 @@ identifiers and attribution are available in the pre-removal Git revision
- `src/core/persistence/worktree-lease.ts`: waiter markers (`noteWaiting`, `clearWaiting`, `foreignWaiterAhead`); lane leases drain and lane takes yield to a foreign waiter that has waited 1 s.
- `src/core/persistence/ownership.ts`: a no-wait `acquireWorktree` yields to a foreign waiter; new `acquireWorktreeWaiting` for the effect worker.
-- `src/core/persistence/effects.ts`, `effect-journal.ts`, `effect-model.ts`: effects wait for a contended worktree once per root per pass; `writer_busy` backs off (`retryBusyEffect`) and records `busy_retries`, `busy_since` and `busy_at`.
+- `src/core/persistence/effects.ts`, `effect-journal.ts`, `effect-model.ts`: effects wait for a contended worktree once per root per pass; `writer_busy` backs off (`retryBusyEffect`) and records `busy_retries`, `busy_since` and `busy_at` while the effect waits; completion drops them, so a receipt's size never depends on contention.
- `src/core/persistence/effect-catchup.ts` (new): `readStalledCatchups`, used by doctor `parked_effects` and writer status.
- `src/core/hardened-git.ts`: a Git stream that has settled no longer keeps its caller's options and chunk handler reachable through its idle timer.
diff --git a/src/core/behavior-change-notice.ts b/src/core/behavior-change-notice.ts
index 3862207295..1946b434de 100644
--- a/src/core/behavior-change-notice.ts
+++ b/src/core/behavior-change-notice.ts
@@ -1213,7 +1213,7 @@ export const BEHAVIOR_CHANGES: ReadonlyArray<{ since: string; text: ChangeText;
{ since: '0.60.423.0', text: 'A managed write that meets a pooler\'s client cap (Supavisor `EMAXCONNSESSION` in session mode, "too many clients already") is now released as contention and retried, like SQLSTATE 53300, instead of failing terminally as `storage_error` and stopping the sync drain `blocked_by_failures`. The persistence log adds `cause=pool_exhausted` beside the code the pooler sent, with the pool-sizing fix in place of the generic writer-status pointer.' },
// GBRA-133 wave 7 (writer_busy catch-up fairness across processes). `since` is stamped at the merge slot.
{ since: '0.60.424.0', text: 'A Git or mirror effect that finds its canonical worktree held by another gbrain process now waits for it (up to 5 seconds per pass) instead of trying once between that process\'s writes, and once it has waited a second the other process yields: its lane leases drain and its no-wait takes report busy until the effect took its turn. Before, a long sync in one process could win every race for the worktree, so another process\'s backlog of Git effects never ran. Waiters mark themselves beside the worktree\'s coordination lock (`<lock>.waiters/<pid>`); a marker its process stopped refreshing is ignored.' },
- { since: '0.60.424.0', text: 'An effect that still finds its worktree busy retries as `writer_busy` with a backoff that grows per consecutive busy retry (250 ms doubling to 60 s) instead of every 250 ms, and it never parks on contention. Its data records `busy_retries`, `busy_since` and `busy_at`.' },
+ { since: '0.60.424.0', text: 'An effect that still finds its worktree busy retries as `writer_busy` with a backoff that grows per consecutive busy retry (250 ms doubling to 60 s) instead of every 250 ms, and it never parks on contention. While it waits its data records `busy_retries`, `busy_since` and `busy_at`; they are dropped when it completes.' },
{ since: '0.60.424.0', text: 'doctor `parked_effects` and `gbrain sources writer status` now report `stalled_catchups`: per source, effects that have waited on a busy canonical worktree for over 10 minutes, with when the wait began and the read-only writer status command that shows who holds the worktree. doctor warns "Catch-up not progressing" for them even when nothing is parked.' },
];
diff --git a/src/core/persistence/effect-journal.ts b/src/core/persistence/effect-journal.ts
index ccf4cbee35..32a7ab2ce5 100644
--- a/src/core/persistence/effect-journal.ts
+++ b/src/core/persistence/effect-journal.ts
@@ -352,7 +352,7 @@ export async function completeEffect(engine: SqlEngine, effect: PersistenceEffec
await engine.executeRaw(`UPDATE persistence_effects SET
state=CASE WHEN jsonb_array_length(COALESCE(data->'parked','[]'::jsonb))>0 THEN 'failed' ELSE 'committed' END,
error_code=CASE WHEN jsonb_array_length(COALESCE(data->'parked','[]'::jsonb))>0 THEN 'targets_parked' END,
- data=data-'retry_slugs'-'target_failures'-'failing_target',execution_token=NULL,claim_expires_at=NULL,claim_owner=NULL,
+ data=data-'retry_slugs'-'target_failures'-'failing_target'-'busy_retries'-'busy_since'-'busy_at',execution_token=NULL,claim_expires_at=NULL,claim_owner=NULL,
outcome=$3::text::jsonb,updated_at=now() WHERE id=$1 AND execution_token=$2::uuid AND recovery IS NULL AND ${PERSISTENCE_PROTOCOL_PREDICATE}`,
[effect.id, effect.execution_token, JSON.stringify(outcome)]);
}
diff --git a/src/core/persistence/effects.ts b/src/core/persistence/effects.ts
index dda24593ca..89aa0a1ba3 100644
--- a/src/core/persistence/effects.ts
+++ b/src/core/persistence/effects.ts
@@ -994,7 +994,7 @@ async function runEffectPass(engine: BrainEngine, config: GBrainConfig, opts: Ef
await engine.executeRaw(`UPDATE persistence_effects e SET
state=CASE WHEN jsonb_array_length(COALESCE(e.data->'parked','[]'::jsonb))>0 THEN 'failed' ELSE 'committed' END,
error_code=CASE WHEN jsonb_array_length(COALESCE(e.data->'parked','[]'::jsonb))>0 THEN 'targets_parked' END,
- data=e.data-'retry_slugs'-'target_failures'-'failing_target',execution_token=NULL,claim_expires_at=NULL,claim_owner=NULL,
+ data=e.data-'retry_slugs'-'target_failures'-'failing_target'-'busy_retries'-'busy_since'-'busy_at',execution_token=NULL,claim_expires_at=NULL,claim_owner=NULL,
outcome=$2::text::jsonb,updated_at=now()
FROM jsonb_to_recordset($1::text::jsonb) AS t(id bigint,token uuid)
WHERE e.id=t.id AND e.execution_token=t.token AND e.recovery IS NULL AND ${PERSISTENCE_PROTOCOL_PREDICATE}`,
diff --git a/test/effect-catchup-contention-gbra133.test.ts b/test/effect-catchup-contention-gbra133.test.ts
index ed7641a9a3..57db594a80 100644
--- a/test/effect-catchup-contention-gbra133.test.ts
+++ b/test/effect-catchup-contention-gbra133.test.ts
@@ -104,10 +104,12 @@ describe('GBRA-133 Git effect catch-up under cross-process worktree contention',
expect(doctor.details).toMatchObject({ parked_effects: 0, stalled_catchups: [{ source_id: 'default', stalled_effects: 3 }] });
} finally { await held.release(); }
- // Once the worktree is free the backlog commits and its busy streak no longer counts.
+ // Once the worktree is free the backlog commits and its busy streak no longer counts. The committed effects keep no busy
+ // bookkeeping: an effect's data is part of its request's receipt bytes, which must not depend on how often it met contention.
await release(engine);
await pass(engine);
expect(await gitStates(engine)).toEqual({ committed: 3 });
expect(await readStalledCatchups(engine)).toEqual([]);
+ expect(await engine.executeRaw<{ n: number }>("SELECT count(*)::int AS n FROM persistence_effects WHERE kind='git' AND (data ? 'busy_retries' OR data ? 'busy_since' OR data ? 'busy_at')")).toEqual([{ n: 0 }]);
}), 180_000);
});