Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(refresh): a refresh whose drain landed gbrain's own pushed commits takes them as its start under the fence instead of refusing refresh_source_changed (#8828)
garrytan397cee49f078
diff --git a/.github/workflows/tla-spec.yml b/.github/workflows/tla-spec.ymlindex 1c1ce5978d..6a1039ba79 100644--- a/.github/workflows/tla-spec.yml+++ b/.github/workflows/tla-spec.yml@@ -42,6 +42,7 @@ on:- 'test/refresh-write-barrier.test.ts'- 'test/import-write-barrier.test.ts'- 'test/refresh-barrier-held-after-abort.test.ts'+ - 'test/refresh-own-commits-replan-8828.test.ts'- 'src/core/persistence/coordinator.ts'- 'src/core/persistence/group-publish.ts'- 'src/core/persistence/page-prepare.ts'@@ -87,6 +88,7 @@ on:- 'test/refresh-write-barrier.test.ts'- 'test/import-write-barrier.test.ts'- 'test/refresh-barrier-held-after-abort.test.ts'+ - 'test/refresh-own-commits-replan-8828.test.ts'- 'src/core/persistence/coordinator.ts'- 'src/core/persistence/group-publish.ts'- 'src/core/persistence/page-prepare.ts'diff --git a/docs/designs/REFRESH_WRITE_BARRIER.md b/docs/designs/REFRESH_WRITE_BARRIER.mdindex 94d6440be1..620d0e8d44 100644--- a/docs/designs/REFRESH_WRITE_BARRIER.md+++ b/docs/designs/REFRESH_WRITE_BARRIER.md@@ -40,6 +40,11 @@ decision with stated preconditions, below), and the soak cell (lane L5).(`refreshFenceClear`); `draining` and `syncing` keep claiming so queued work drains.- `fenceAndMerge` refuses `refresh_source_changed` when any request, checkout effect or recoveryrecord reached the worktree after the drain check.+- #8828: HEAD that moved past `old_head` under the fence only by gbrain's own commits (every sha in+ `old_head..HEAD` is in the commit ledger) and that the upstream ref holds is the drained writes+ the writer pushed: the fenced row takes HEAD as `old_head` and the upstream ref as `target_head`+ (it must hold the precheck's target) before `merge_started`, and the merge goes on. Any other HEAD+ move refuses `refresh_source_changed`.### The barrier sequencediff --git a/docs/guides/write-refusals.md b/docs/guides/write-refusals.mdindex fa242ece29..232bfbc200 100644--- a/docs/guides/write-refusals.md+++ b/docs/guides/write-refusals.md@@ -571,7 +571,7 @@ this anchor (`--json` puts them in one object on stdout); the command exits 1.| <a id="refresh_drain_timeout"></a>`refresh_drain_timeout` | `refresh_drain_timeout` | Queued writes or Git effects on the checkout did not finish within `--wait-drain` (default 60 s; `sources.refresh_drain_wait_ms`; `GBRAIN_REFRESH_DRAIN_WAIT_MS`). The counts are in the message. The fence was lifted and HEAD did not move. | Retry, or wait longer with `--wait-drain <seconds>`. Inspect what is queued with `gbrain sources writer status <source> --json`. || <a id="refresh_lock_timeout"></a>`refresh_lock_timeout` | `refresh_lock_timeout` | The refresh fence locks the brain, worktree, source and refresh rows with a 5 s lock timeout, and another database transaction held one of them longer. `why` lists the five oldest open transactions in this database holding locks on those tables (pid, application, state, how long the transaction has been open). They are candidates: the blocker may not be among them, because Postgres does not show which row each one holds. It never repeats their SQL, and says so when that lookup failed or took longer than 3 s. HEAD did not move: no fence was set, or the draining refresh was aborted. When the same holder also blocked that abort, the refusal says the row stays `draining`, and the next `gbrain sources refresh <source>` resumes it. | Retry `gbrain sources refresh <source>`. If it repeats, a transaction holding one of those rows is stuck: `why` lists the oldest candidates, so confirm the blocker on the brain host before ending any transaction. || <a id="refresh_sync_timeout"></a>`refresh_sync_timeout` | `refresh_sync_timeout` | Reported per source in a refresh's `sync_blocked`, not as a refusal: the source's managed sync committed nothing within its share of the refresh sync budget (each member gets an equal share of what is left; `sources.refresh_sync_budget_ms`, `GBRAIN_REFRESH_SYNC_BUDGET_MS`, default 5 min), for example because no consumer claimed its write. The message names that write and its state. The checkout is at the upstream commit, the refresh completed and released its lock, and the source is behind until its next sync. A sync stopped by the drain's own rules is reported with that stop's code instead (`drain_stalled` and the others). | Run the `resume` command (`gbrain sync --source <source> --no-pull`). If it stalls the same way, check the write it names with `gbrain sources writer status <source> --json`. |-| <a id="refresh_source_changed"></a>`refresh_source_changed` | `refresh_source_changed` | HEAD moved (an outside commit or checkout), or the checkout's ownership or set of sources changed, between the precheck and the merge. Nothing was merged and the fence was lifted. When HEAD moved or work reached the checkout after the drain check, the run already retried once from a fresh precheck, and the message names why that retry was refused too. | Retry `gbrain sources refresh <source>`. |+| <a id="refresh_source_changed"></a>`refresh_source_changed` | `refresh_source_changed` | HEAD moved (an outside commit or checkout, or gbrain's own commits the upstream does not hold yet), or the checkout's ownership or set of sources changed, between the precheck and the merge. Nothing was merged and the fence was lifted. When HEAD moved or work reached the checkout after the drain check, the run already retried once from a fresh precheck, and the message names why that retry was refused too. | Retry `gbrain sources refresh <source>`. || <a id="refresh_recovery_required"></a>`refresh_recovery_required` | `refresh_recovery_required` | Publication or source recovery is pending on the checkout, or a refresh was interrupted and HEAD is neither the commit it started from nor the verified upstream commit. (A commit that lands on the upstream commit right after the refresh's own merge is not this case: the refresh records the merge and syncs it. Nor is one that lands on the start commit right before the merge: git refuses the fast-forward and the refresh ends `refresh_source_changed`.) Writes to the checkout stay refused until it is resolved; gbrain never adopts a HEAD it cannot verify. | `gbrain sources writer status <source>`, then `gbrain sources refresh <source> --resume`. When HEAD is unverified the fix names both ways out: reset to the start commit and run `--abandon`, or reset to the upstream commit and run `--resume`. Resetting is destructive, so ask the user first. Writer status reads HEAD and names the one step left when HEAD already sits at one of those commits: `--resume` at the upstream commit, `--abandon` at the start commit when no member source has synced past it. When one has (its `last_commit` is set and is neither the start commit nor an ancestor of it; a commit git cannot find counts too), `--abandon` refuses with this code and changes nothing, because the next sync would roll that member's pages back; `--resume` refuses too, and both name the one way out that keeps those pages: the fix (`next: ask_user`) previews with `git -C <checkout> status --short`, fast-forwards the checkout with `git merge --ff-only` to the commit the members synced (the upstream commit, or a later one a member already synced; it keeps uncommitted edits and refuses if one overlaps an incoming file), then runs `--resume`. When git refuses, the fix names a hard reset to that commit instead (destructive: it discards local commits and uncommitted edits not in that commit). `--resume` finishes a `recovery_required` refresh whose HEAD is the upstream commit or descends from it, unless a member synced a commit on this refresh's line (or one git cannot place) that HEAD does not contain; then it refuses with the same fast-forward fix. || <a id="worktree_refreshing"></a>`worktree_refreshing` | `worktree_refreshing` | A write, or a managed sync started by a cycle or another command, reached a checkout while a refresh is draining or merging it. It was not saved. `detail` is `retry_after_ms=<n>`, estimated from the refresh itself: the median duration of the worktree's last completed refreshes minus how long this one has run, never below 15 s while it syncs or 2 s in its other phases (so a refresh of several minutes is never answered with a 1 s retry). The message names the phase, how long the refresh has run and typically takes, and the CLI's human output ends with one line, `Nothing was saved; retry the same command in about <n> s (retry_after_ms=<n>).`, which survives `| tail -1`. Writes to database-only memory are not affected. An interactive write (CLI, MCP) waits out the fence for up to 10 s, or its explicit `wait_ms` if lower, before this refusal; a background write is refused at once. The refusal is `retryable`, its `fix.next` is `wait`, and the CLI exits 1; the memory verbs keep their frozen `unavailable` code with `canonical: worktree_refreshing`. With `persistence.refresh_barrier` on, a single page or memory write waits behind the refresh instead ([deferred writes](#deferred-writes)). | Retry the same request ID after `retry_after_ms`; nothing was recorded, so the retry cannot write twice. `gbrain sources writer status --source <source> --json` shows the refresh. A cycle's sync is retried by the next cycle. |diff --git a/spec/tla/README.md b/spec/tla/README.mdindex 8d20938e7c..f9db94ebf6 100644--- a/spec/tla/README.md+++ b/spec/tla/README.md@@ -168,7 +168,7 @@ invariant. Behind constants it also models the stacked follow-ups: writes held fEvery config kills the refreshing process once (`MaxCrashes = 1`), at any step, after which `recoverOne` resumes therefresh row it left. A refresh that finds HEAD moved at the merge aborts and the tick retries, as `fenceAndMerge` does,-so `recovery_required` is reached only through recovery or a refused merge. `RefreshBarrier.cfg` also lets+unless every commit in old_head..HEAD is gbrain's own: then HEAD becomes old_head and the merge goes on (#8828). So `recovery_required` is reached only through recovery or a refused merge. `RefreshBarrier.cfg` also letssomeone reset the checkout to old_head after the merge (`MayReset`).`test/spec-refresh-barrier.test.ts` keeps the default config on #8682 alone, every safety config on every invariant,@@ -190,6 +190,7 @@ every config killing the refresh at least once, and the workflow's path filter c| `RestoreLeavesNoOpenWrite` | a restore leaves no write admitted before it open (finding C) || `NoApplyAfterRevoke` | no write commits after its credential was revoked || `CommitsInAdmissionOrder` | writes to one page commit in admission order |+| `ReplanOnlyOwnCommits` | a refresh takes a HEAD that moved under its fence as its start only when no commit another agent made since its old_head is in it (#8828) || `DeferredLeaves`, `EveryWriteEnds` | liveness under `Fairness`: a deferred write leaves deferral; an admitted write ends |### Actions and code@@ -208,6 +209,7 @@ every config killing the refresh at least once, and the workflow's path filter c| `ExpRefBegin`, `ExpRefCommit` | `expireDeferredWrites`: the protocol, the refresh row re-read `released_at IS NULL FOR NO KEY UPDATE`, `completeWrite` || `ExpImpBegin`, `ExpImpRecheck`, `ExpImpCommit` | #8705's import expiry in `expireDeferredWrites` || `SyncBegin` … `SyncStamp` | a managed sync pass, whose rows go through the claim like writes: no write passes the queued row importing its page or a running sync row, and a sync row waits behind earlier writes and is never claimed while a write runs; the page row, then its checkpoint row's `UPDATE sources SET last_commit` (sync-prepare.ts) |+| `RefMerge` | the HEAD check under the commit lock in `fenceAndMerge`: HEAD at old_head goes on; #8828 `replanOnOwnCommits` takes a HEAD moved only by gbrain's ledgered commits as old_head (`OwnSince`, the code's `ownCommits`); any other move aborts || `OwedSyncBegin` | GST-1853 `syncAfterUnfinishedRefresh` (worktree-refresh.ts): while the worktree's newest refresh is aborted and a member's `last_commit` is not HEAD, a refresh run ends by running that member's managed `--no-pull` sync || `Foreign`, `LocalEdit`, `Age`, `Revoke`, `Restore` | the environment: another agent's plain-git commit, an uncommitted edit, the wait limit passing, a revoked token, `quarantineSharedSkillRestore` (PGLite-only, one transaction, no worker) |@@ -237,6 +239,7 @@ every config killing the refresh at least once, and the workflow's path filter c| `resume-marks-unmerged-recovery` | guard | `RecoveryOnlyByPolicy` | `movedBeforeMerge` (the finding below): the code edit aborts only a HEAD moved by gbrain's own commits; the refresh killed at fenced after a foreign commit, aborted by the owner scan || `resume-needs-exact-target` | guard | `RecoveryOnlyByPolicy` | `recoverOne`'s bookkeeping branch: with HEAD only exactly at the target, a commit between the merge and bookkeeping strands the row; the refresh killed after its merge || `abort-skips-owed-sync` | guard | `DeferredLeaves` (AbortLiveness) | `syncAfterUnfinishedRefresh`: no refresh run syncs the members an aborted refresh left behind HEAD; the three GST-1853 tests (aborted then diverged, killed at fenced then `--resume`, owner-scan abort then a diverged tick) |+| `replan-over-foreign-commit` | guard | `ReplanOnlyOwnCommits` | `replanOnOwnCommits`'s ledger check (`ownCommits`): the refresh takes a HEAD with another agent's commit in it as its start; the #8828 test with one foreign commit among gbrain's own published commits |#8708's local-edit adoption has no guard mutant of its own: `RefreshBarrierAdopt.cfg` checks it against every safety property.diff --git a/spec/tla/refresh-barrier/RefreshBarrier.cfg b/spec/tla/refresh-barrier/RefreshBarrier.cfgindex a27d939ed5..520444df67 100644--- a/spec/tla/refresh-barrier/RefreshBarrier.cfg+++ b/spec/tla/refresh-barrier/RefreshBarrier.cfg@@ -33,4 +33,5 @@ INVARIANT RecoveryOnlyByPolicyINVARIANT ExpiryOnlyWhileWaitingINVARIANT RestoreLeavesNoOpenWriteINVARIANT NoApplyAfterRevoke+INVARIANT ReplanOnlyOwnCommitsINVARIANT CommitsInAdmissionOrderdiff --git a/spec/tla/refresh-barrier/RefreshBarrier.tla b/spec/tla/refresh-barrier/RefreshBarrier.tlaindex e314905e4e..761c0fd19a 100644--- a/spec/tla/refresh-barrier/RefreshBarrier.tla+++ b/spec/tla/refresh-barrier/RefreshBarrier.tla@@ -123,7 +123,8 @@ VARIABLEShist, \* every version that was ever a revision of the page (page history)lastCommit, \* sources.last_commit: the commit managed sync last importedlocal, \* an uncommitted edit of F in the worktree (None = clean)- nver, nforeign, nlocal,+ nver, nlocal,+ fgn, \* the commits another agent made with plain git (their count is bounded by MaxForeign)\* environmentrestored, revoked, preRestore,ncrash, \* times the refreshing process was killed@@ -133,6 +134,7 @@ VARIABLESbadRelease, \* a deferred write committed before its refresh was releasedbadDefer, \* a write was deferred to a refresh that was already releasedbadRevoke, \* a write committed after its credential was revoked+ badReplan, \* a refresh took as its start a HEAD that holds a commit another agent made after its old_head\* locksheld, want,\* process locals@@ -141,9 +143,9 @@ VARIABLESreqv == <<st, code, seq, nseq, dref, dimp, applied, cpos, ncommit, expRev, overdue, adopted>>refv == <<rstate, rel, rchg, rhead, rmoved, rtgt, rmstart, lost, rwhy, cur, nref>>-gitv == <<head, verAt, fileV, dbV, hist, lastCommit, local, nver, nforeign, nlocal>>+gitv == <<head, verAt, fileV, dbV, hist, lastCommit, local, nver, fgn, nlocal>>envv == <<restored, revoked, preRestore, ncrash>>-ghostv == <<hidden, badExpiry, badRelease, badDefer, badRevoke>>+ghostv == <<hidden, badExpiry, badRelease, badDefer, badRevoke, badReplan>>lockv == <<held, want>>procv == <<pc, cw, cprep, cfin, ew, aref, aread, passHead, sseq>>vars == <<reqv, refv, gitv, envv, ghostv, lockv, procv>>@@ -267,7 +269,7 @@ Init ==/\ lastCommit = 1/\ local = None/\ nver = 1- /\ nforeign = 0+ /\ fgn = {}/\ nlocal = 0/\ restored = FALSE/\ revoked = FALSE@@ -278,6 +280,7 @@ Init ==/\ badRelease = FALSE/\ badDefer = FALSE/\ badRevoke = FALSE+ /\ badReplan = FALSE/\ held = [p \in Procs |-> NoLocks]/\ want = [p \in Procs |-> <<>>]/\ pc = [p \in Procs |-> "idle"]@@ -359,7 +362,7 @@ AdmCommit(w) ==/\ Commit(ADM(w)) /\ Go(ADM(w), "idle") /\ Need(ADM(w), <<>>)/\ aref' = [aref EXCEPT ![w] = None] /\ aread' = [aread EXCEPT ![w] = "none"]/\ UNCHANGED <<code, dimp, applied, cpos, ncommit, expRev, overdue, adopted, rchg, rhead, rmoved, rtgt, rmstart, lost, rwhy, nref, gitv, envv,- hidden, badExpiry, badRelease, badRevoke, cw, cprep, cfin, ew, passHead, sseq>>+ hidden, badExpiry, badRelease, badRevoke, badReplan, cw, cprep, cfin, ew, passHead, sseq>>\* -------------------------------- consumer --------------------------------\* claimNextWrite: the protocol declaration and the head read, SELECT ... ORDER BY sequence LIMIT 1 FOR UPDATE SKIP@@ -444,7 +447,7 @@ ConsPub ==/\ code' = [code EXCEPT ![cw] = IF mine /\ denied THEN "permission_denied" ELSE @]/\ UNCHANGED <<head, verAt, fileV, nver, dbV, hist, local, applied, ncommit, cpos, hidden, badRelease, badRevoke>>/\ Commit(CONS) /\ ConsIdle- /\ UNCHANGED <<seq, nseq, dref, dimp, expRev, overdue, adopted, refv, lastCommit, nforeign, nlocal, envv, badExpiry, badDefer,+ /\ UNCHANGED <<seq, nseq, dref, dimp, expRev, overdue, adopted, refv, lastCommit, fgn, nlocal, envv, badExpiry, badDefer, badReplan,want, ew, aref, aread, passHead, sseq>>\* holdForImport: UPDATE ... SET state='deferred', deferred_import_from=<last_commit read before the check>@@ -466,8 +469,8 @@ ConsAdopt ==/\ NewCommit(local) /\ dbV' = local /\ hist' = hist \cup {local} /\ local' = None/\ adopted' = [adopted EXCEPT ![cw] = TRUE]/\ Need(CONS, <<L(REQ(cw), "U")>>) /\ Go(CONS, "c_release")- /\ UNCHANGED <<st, code, seq, nseq, dref, dimp, applied, cpos, ncommit, expRev, overdue, refv, lastCommit, nver, nforeign,- nlocal, envv, badExpiry, badRelease, badDefer, badRevoke, held, cw, cprep, cfin, ew, aref, aread, passHead, sseq>>+ /\ UNCHANGED <<st, code, seq, nseq, dref, dimp, applied, cpos, ncommit, expRev, overdue, refv, lastCommit, nver, fgn,+ nlocal, envv, badExpiry, badRelease, badDefer, badRevoke, badReplan, held, cw, cprep, cfin, ew, aref, aread, passHead, sseq>>\* releaseUnpublishedClaim: back to queued, prepared again by the next claim.ConsRelease ==@@ -534,14 +537,25 @@ AbortCommit ==/\ Commit(RFR) /\ Go(RFR, "idle")/\ UNCHANGED <<reqv, rchg, rhead, rmoved, rtgt, rmstart, lost, rwhy, nref, gitv, envv, ghostv, want, cw, cprep, cfin, ew, aref, aread, passHead, sseq>>-\* Under the commit lock: HEAD must still be old_head. A HEAD that moved (a drained write's ledger commit, or another-\* agent's plain-git commit) aborts the refresh, refresh_source_changed, and the tick retries. Otherwise the-\* merge_started marker commits (synchronous_commit on), then the ff-only merge.+\* A commit another agent made in old_head..HEAD (HEAD only moves forward before a merge).+ForeignSince(i) == fgn \cap ((rhead[i] + 1)..head) # {}+\* #8828 replanOnOwnCommits: every commit in old_head..HEAD is in gbrain's commit ledger (ownCommits). The code also asks that+\* the upstream ref hold HEAD (the writer pushed those commits); the model has no upstream, so the refresh's target is past HEAD.+OwnSince(i) == ~ForeignSince(i)++\* Under the commit lock: HEAD must still be old_head. #8828: a HEAD that moved past old_head only by gbrain's own commits+\* (drained writes' ledger commits) becomes old_head in one UPDATE of the fenced row, before merge_started, and the merge goes+\* on. A move with another agent's plain-git commit in it aborts the refresh, refresh_source_changed, and the tick retries.+\* Otherwise the merge_started marker commits (synchronous_commit on), then the ff-only merge.RefMerge ==/\ Ready(RFR, "r_merge")/\ Need(RFR, <<L(REF(nref), "NKU")>>)- /\ Go(RFR, IF head # rhead[nref] THEN "r_abort" ELSE "r_mstart")- /\ UNCHANGED <<reqv, refv, gitv, envv, ghostv, held, cw, cprep, cfin, ew, aref, aread, passHead, sseq>>+ /\ IF head # rhead[nref] /\ ~OwnSince(nref)+ THEN Go(RFR, "r_abort") /\ UNCHANGED <<rhead, badReplan>>+ ELSE /\ Go(RFR, "r_mstart") /\ rhead' = [rhead EXCEPT ![nref] = head]+ /\ badReplan' = (badReplan \/ ForeignSince(nref))+ /\ UNCHANGED <<reqv, rstate, rel, rchg, rmoved, rtgt, rmstart, lost, rwhy, cur, nref, gitv, envv,+ hidden, badExpiry, badRelease, badDefer, badRevoke, held, cw, cprep, cfin, ew, aref, aread, passHead, sseq>>MergeStartCommit ==/\ Ready(RFR, "r_mstart")@@ -561,7 +575,7 @@ FfMerge ==/\ rtgt' = [rtgt EXCEPT ![nref] = head + 1]/\ Need(RFR, <<L(REF(nref), "NKU"), L(SRC, "NKU")>>) /\ Go(RFR, "r_bk")ELSE /\ Need(RFR, <<L(REF(nref), "NKU")>>) /\ Go(RFR, "r_mark") /\ UNCHANGED <<head, verAt, fileV, nver, rmoved, rtgt>>- /\ UNCHANGED <<reqv, rstate, rel, rchg, rhead, rmstart, lost, rwhy, cur, nref, dbV, hist, lastCommit, local, nforeign, nlocal,+ /\ UNCHANGED <<reqv, rstate, rel, rchg, rhead, rmstart, lost, rwhy, cur, nref, dbV, hist, lastCommit, local, fgn, nlocal,envv, ghostv, held, cw, cprep, cfin, ew, aref, aread, passHead, sseq>>MarkCommit ==@@ -576,7 +590,7 @@ PersonAbandon ==/\ pc[RFR] = "r_recovery"/\ head' = rhead[nref] /\ fileV' = verAt[rhead[nref]] /\ local' = None/\ Need(RFR, <<L(REF(nref), "NKU")>>) /\ Go(RFR, "r_abort")- /\ UNCHANGED <<reqv, refv, verAt, dbV, hist, lastCommit, nver, nforeign, nlocal, envv, ghostv, held,+ /\ UNCHANGED <<reqv, refv, verAt, dbV, hist, lastCommit, nver, fgn, nlocal, envv, ghostv, held,cw, cprep, cfin, ew, aref, aread, passHead, sseq>>PersonResume ==@@ -589,7 +603,7 @@ PersonResume ==/\ rtgt' = [rtgt EXCEPT ![nref] = base + 1] /\ lost' = [lost EXCEPT ![nref] = FALSE]/\ local' = None/\ Need(RFR, <<L(REF(nref), "NKU"), L(SRC, "NKU")>>) /\ Go(RFR, "r_bk")- /\ UNCHANGED <<reqv, rstate, rel, rchg, rhead, rmstart, rwhy, cur, nref, dbV, hist, lastCommit, nforeign, nlocal, envv, ghostv, held,+ /\ UNCHANGED <<reqv, rstate, rel, rchg, rhead, rmstart, rwhy, cur, nref, dbV, hist, lastCommit, fgn, nlocal, envv, ghostv, held,cw, cprep, cfin, ew, aref, aread, passHead, sseq>>\* bookkeeping: transition to merged (the refresh row), the sources' upstream observation, then syncing.@@ -669,7 +683,7 @@ ExpRefCommit ==/\ badExpiry' = (badExpiry \/ (fire /\ ~(st[ew] = "deferred" /\ ~rel[dref[ew]])))/\ Commit(EXP) /\ Go(EXP, "idle") /\ ew' = 0/\ UNCHANGED <<seq, nseq, dref, dimp, applied, cpos, ncommit, expRev, overdue, adopted, refv, gitv, envv,- hidden, badRelease, badDefer, badRevoke, want, cw, cprep, cfin, aref, aread, passHead, sseq>>+ hidden, badRelease, badDefer, badRevoke, badReplan, want, cw, cprep, cfin, aref, aread, passHead, sseq>>\* #8705: a write held for an import that has not landed past the limit ends pending_import_expired.ExpImpBegin(w) ==@@ -698,7 +712,7 @@ ExpImpCommit ==/\ badExpiry' = (badExpiry \/ (fire /\ ~(st[ew] = "deferred" /\ dimp[ew] # None /\ lastCommit = dimp[ew])))/\ Commit(EXP) /\ Go(EXP, "idle") /\ ew' = 0/\ UNCHANGED <<seq, nseq, dref, dimp, applied, cpos, ncommit, expRev, overdue, adopted, refv, gitv, envv,- hidden, badRelease, badDefer, badRevoke, want, cw, cprep, cfin, aref, aread, passHead, sseq>>+ hidden, badRelease, badDefer, badRevoke, badReplan, want, cw, cprep, cfin, aref, aread, passHead, sseq>>\* ------------------------------ managed sync ------------------------------\* A pass starts only with no refresh, or as the refresh's own member sync (assertManagedSyncAllowed). It imports F's@@ -735,7 +749,7 @@ SyncImport ==THEN dbV' = fileV /\ hist' = hist \cup {fileV}ELSE UNCHANGED <<dbV, hist>> \* nothing to import, or a dirty file sync holds/\ Commit(SYN) /\ Go(SYN, "s_ckpt")- /\ UNCHANGED <<reqv, refv, head, verAt, fileV, lastCommit, local, nver, nforeign, nlocal, envv, ghostv,+ /\ UNCHANGED <<reqv, refv, head, verAt, fileV, lastCommit, local, nver, fgn, nlocal, envv, ghostv,want, cw, cprep, cfin, ew, aref, aread, passHead, sseq>>\* The checkpoint row is claimed once no earlier write is ahead of it; the pass stamp moves last_commit (UPDATE sources).@@ -748,27 +762,27 @@ SyncStamp ==/\ Ready(SYN, "s_stamp")/\ lastCommit' = passHead/\ Commit(SYN) /\ Go(SYN, "idle") /\ passHead' = 0 /\ sseq' = 0- /\ UNCHANGED <<reqv, refv, head, verAt, fileV, dbV, hist, local, nver, nforeign, nlocal, envv, ghostv,+ /\ UNCHANGED <<reqv, refv, head, verAt, fileV, dbV, hist, local, nver, fgn, nlocal, envv, ghostv,want, cw, cprep, cfin, ew, aref, aread>>\* ------------------------------- environment -------------------------------Foreign ==- /\ nforeign < MaxForeign /\ local = None+ /\ Cardinality(fgn) < MaxForeign /\ local = None/\ IF OutsideHelp THEN TRUE ELSE RefIs({"draining"})- /\ NewCommit(nver + 1) /\ nver' = nver + 1 /\ nforeign' = nforeign + 1+ /\ NewCommit(nver + 1) /\ nver' = nver + 1 /\ fgn' = fgn \cup {head + 1}/\ UNCHANGED <<reqv, refv, dbV, hist, lastCommit, local, nlocal, envv, ghostv, lockv, procv>>LocalEdit ==/\ nlocal < MaxLocalEdits /\ local = None /\ cur = None/\ local' = nver + 1 /\ nver' = nver + 1 /\ nlocal' = nlocal + 1- /\ UNCHANGED <<reqv, refv, head, verAt, fileV, dbV, hist, lastCommit, nforeign, envv, ghostv, lockv, procv>>+ /\ UNCHANGED <<reqv, refv, head, verAt, fileV, dbV, hist, lastCommit, fgn, envv, ghostv, lockv, procv>>\* After the merge, someone resets the checkout to old_head with plain git (git reset --hard), before bookkeeping.ResetAfterMerge ==/\ MayReset /\ cur # None /\ RefIs({"fenced"}) /\ HoldsTarget(cur)/\ head' = rhead[cur] /\ fileV' = verAt[rhead[cur]] /\ local' = None /\ lost' = [lost EXCEPT ![cur] = TRUE]/\ UNCHANGED <<reqv, rstate, rel, rchg, rhead, rmoved, rtgt, rmstart, rwhy, cur, nref, verAt, dbV, hist, lastCommit, nver,- nforeign, nlocal, envv, ghostv, lockv, procv>>+ fgn, nlocal, envv, ghostv, lockv, procv>>\* created_at passes the limit; only a deferred row's age is ever read.Age(w) ==@@ -853,6 +867,8 @@ ExpiryOnlyWhileWaiting == ~badExpiryRestoreLeavesNoOpenWrite == restored => \A w \in preRestore : st[w] \in Terminal\* No write commits after its credential was revoked.NoApplyAfterRevoke == ~badRevoke+\* #8828: a refresh never takes as its start a HEAD holding another agent's commit made after its old_head.+ReplanOnlyOwnCommits == ~badReplan\* Writes to one page commit in admission order: a write never lands on top of a later one.CommitsInAdmissionOrder == \A a, b \in Writes :(st[a] = "committed" /\ st[b] = "committed" /\ seq[a] < seq[b]) => cpos[a] < cpos[b]diff --git a/spec/tla/refresh-barrier/RefreshBarrierAdopt.cfg b/spec/tla/refresh-barrier/RefreshBarrierAdopt.cfgindex 4f846719a5..baa4a5c1e5 100644--- a/spec/tla/refresh-barrier/RefreshBarrierAdopt.cfg+++ b/spec/tla/refresh-barrier/RefreshBarrierAdopt.cfg@@ -33,4 +33,5 @@ INVARIANT RecoveryOnlyByPolicyINVARIANT ExpiryOnlyWhileWaitingINVARIANT RestoreLeavesNoOpenWriteINVARIANT NoApplyAfterRevoke+INVARIANT ReplanOnlyOwnCommitsINVARIANT CommitsInAdmissionOrderdiff --git a/spec/tla/refresh-barrier/RefreshBarrierAdoptNightly.cfg b/spec/tla/refresh-barrier/RefreshBarrierAdoptNightly.cfgindex 0f7e95cd27..b47d6a78a9 100644--- a/spec/tla/refresh-barrier/RefreshBarrierAdoptNightly.cfg+++ b/spec/tla/refresh-barrier/RefreshBarrierAdoptNightly.cfg@@ -32,4 +32,5 @@ INVARIANT RecoveryOnlyByPolicyINVARIANT ExpiryOnlyWhileWaitingINVARIANT RestoreLeavesNoOpenWriteINVARIANT NoApplyAfterRevoke+INVARIANT ReplanOnlyOwnCommitsINVARIANT CommitsInAdmissionOrderdiff --git a/spec/tla/refresh-barrier/RefreshBarrierEnv.cfg b/spec/tla/refresh-barrier/RefreshBarrierEnv.cfgindex 05e253548c..e0cb1bef16 100644--- a/spec/tla/refresh-barrier/RefreshBarrierEnv.cfg+++ b/spec/tla/refresh-barrier/RefreshBarrierEnv.cfg@@ -32,4 +32,5 @@ INVARIANT RecoveryOnlyByPolicyINVARIANT ExpiryOnlyWhileWaitingINVARIANT RestoreLeavesNoOpenWriteINVARIANT NoApplyAfterRevoke+INVARIANT ReplanOnlyOwnCommitsINVARIANT CommitsInAdmissionOrderdiff --git a/spec/tla/refresh-barrier/RefreshBarrierImport.cfg b/spec/tla/refresh-barrier/RefreshBarrierImport.cfgindex 55fdd5997c..fae4e00c01 100644--- a/spec/tla/refresh-barrier/RefreshBarrierImport.cfg+++ b/spec/tla/refresh-barrier/RefreshBarrierImport.cfg@@ -34,4 +34,5 @@ INVARIANT RecoveryOnlyByPolicyINVARIANT ExpiryOnlyWhileWaitingINVARIANT RestoreLeavesNoOpenWriteINVARIANT NoApplyAfterRevoke+INVARIANT ReplanOnlyOwnCommitsINVARIANT CommitsInAdmissionOrderdiff --git a/spec/tla/refresh-barrier/mutants.json b/spec/tla/refresh-barrier/mutants.jsonindex eb4c67798a..730c06e07d 100644--- a/spec/tla/refresh-barrier/mutants.json+++ b/spec/tla/refresh-barrier/mutants.json@@ -495,6 +495,33 @@}]}+ },+ {+ "id": "replan-over-foreign-commit",+ "kind": "guard",+ "title": "a refresh takes a HEAD moved under its fence as its start even when another agent's commit is in it (#8828's ledger check removed)",+ "expect": "ReplanOnlyOwnCommits",+ "spec": {+ "file": "RefreshBarrier.tla",+ "before": "OwnSince(i) == ~ForeignSince(i)",+ "after": "OwnSince(i) == TRUE"+ },+ "code": {+ "edits": [+ {+ "file": "src/core/persistence/worktree-refresh.ts",+ "before": " if (await ancestry(git, root, row.old_head, head) !== true || !await ownCommits(git, root, row.old_head, head)) return null;\n",+ "after": " if (await ancestry(git, root, row.old_head, head) !== true) return null;\n"+ }+ ],+ "tests": [+ {+ "test": "test/refresh-own-commits-replan-8828.test.ts",+ "filter": "one foreign commit",+ "postgres": true+ }+ ]+ }}]}diff --git a/src/core/persistence/worktree-refresh.ts b/src/core/persistence/worktree-refresh.tsindex 7760d54108..2cba4978b9 100644--- a/src/core/persistence/worktree-refresh.ts+++ b/src/core/persistence/worktree-refresh.ts@@ -168,6 +168,24 @@ async function ownCommits(git: Git, root: string, from: string, to: string): Proreturn shas.every(sha => ledgered.has(sha));}const OWN_COMMITS = "gbrain's own commits: writes admitted before the fence that landed while the refresh drained";+/**+ * #8828: the drain waits for the writes admitted before the fence, and their commits move HEAD past the precheck's old_head. When+ * HEAD descends from old_head, every commit in old_head..HEAD is in gbrain's commit ledger, and the upstream ref (which the writer's+ * push moves) holds HEAD and the verified target, the fenced refresh takes HEAD as its start and that upstream as its target instead+ * of refusing: the fence defers new writes and the caller's commit lock keeps gbrain's commits out. The row changes only while it is+ * fenced with no merge started. Null otherwise, and the caller refuses refresh_source_changed as before.+ */+async function replanOnOwnCommits(engine: BrainEngine, git: Git, root: string, row: WorktreeRefreshRow, head: string): Promise<WorktreeRefreshRow | null> {+ if (await ancestry(git, root, row.old_head, head) !== true || !await ownCommits(git, root, row.old_head, head)) return null;+ const target = await revParse(git, root, '@{upstream}');+ if (!target || (target !== head && !await isAncestor(git, root, head, target)) || !await isAncestor(git, root, row.target_head, target)) return null;+ const [replanned] = await engine.transaction(async tx => {+ await tx.executeRaw("SELECT set_config('synchronous_commit','on',true)");+ return tx.executeRaw<WorktreeRefreshRow>(`UPDATE persistence_worktree_refreshes SET old_head=$2,target_head=$3,updated_at=now()+ WHERE id=$1::uuid AND state='fenced' AND (outcome->>'merge_started') IS DISTINCT FROM 'true' RETURNING *`, [row.id, head, target]);+ });+ return replanned ?? null;+}/*** GBEV-44, GST-1674: a `fenced` row whose merge never started (`outcome.merge_started` is still false; it turns true, durably, just before* `git merge --ff-only`) and whose HEAD moved past old_head on its own line, by gbrain's commits or anyone's, with no member synced off it.@@ -945,7 +963,7 @@ async function fenceAndMerge(engine: BrainEngine, git: Git, sourceId: string, pl`Retry gbrain sources refresh ${sourceId}`, row.id));if (!await transition(tx, row, ['draining'], 'fenced', { merge_started: false })) throw refusal('refresh_source_changed', 'The refresh record was moved by recovery while draining.', `gbrain sources refresh ${sourceId} --resume`, row.id);}).catch(error => { throw (error as { code?: string }).code === LOCK_NOT_AVAILABLE ? new FenceLockTimeout() : error; });- const current = await readRefresh(engine, row.id);+ let current = await readRefresh(engine, row.id);await hooks?.boundary?.('fenced', current);// #8117: `gbrain git commit-push` serializes on the checkout's commit lock, not on the refresh fence, so the HEAD check// through the merge runs under that lock too: a door commit can neither move HEAD nor hold .git/index.lock in between.@@ -954,6 +972,8 @@ async function fenceAndMerge(engine: BrainEngine, git: Git, sourceId: string, pl: opError('commit_lock_busy', `The checkout's commit lock could not be taken (${error instanceof Error ? error.message : String(error)}); nothing moved.`, `Retry gbrain sources refresh ${sourceId}.`, { retryable: true })));try {const head = await headOf(git, plan.root);+ const replanned = head !== row.old_head && head !== null ? await replanOnOwnCommits(engine, git, plan.root, current, head) : null;+ if (replanned) row = current = replanned;if (head !== row.old_head) {if (head === null) throw await markRecoveryRequired(engine, current, head, plan.root);await abort(engine, current, ['fenced'], retryInTick(refusal('refresh_source_changed',diff --git a/test/refresh-own-commits-replan-8828.test.ts b/test/refresh-own-commits-replan-8828.test.tsnew file mode 100644index 0000000000..535e260b6f--- /dev/null+++ b/test/refresh-own-commits-replan-8828.test.ts@@ -0,0 +1,107 @@+/**+ * #8828: a refresh whose drain waited for gbrain's own writes saw HEAD move past the precheck's old_head (those writes' commits,+ * pushed upstream by the writer), refused refresh_source_changed, and its same-run retry met the next writes and refused too. When+ * every commit since old_head is gbrain's own and the upstream it pushed to holds them, the refresh now takes HEAD as its start+ * under the fence and fast-forwards to that upstream; any other HEAD move still refuses.+ * Seams: the real refresh state machine, commit ledger and git; PGLite, and Postgres when DATABASE_URL is set.+ */+import { afterAll, beforeAll, beforeEach, afterEach, expect, test } from 'bun:test';+import type { BrainEngine } from '../src/core/engine.ts';+import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';+import { resetWriteSwitches } from '../src/core/persistence/switches.ts';+import { _setRefreshFenceWaitForTests } from '../src/core/persistence/admission-retry.ts';+import { _resetRefreshBarrierCacheForTests } from '../src/core/persistence/refresh-barrier.ts';+import { refreshWorktree } from '../src/core/persistence/worktree-refresh.ts';+import { recordCommit } from '../src/core/git/commit-ledger.ts';+import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';+import { testBackends } from './helpers/test-backends.ts';+import { withEnv } from './helpers/with-env.ts';+import { commitFile, git, page, withRefreshFixture, type RefreshFixture } from './helpers/worktree-refresh-fixture.ts';++const backends = testBackends();+const engines: BrainEngine[] = [];+let closePostgres: (() => Promise<void>) | undefined;++beforeAll(async () => {+ if (backends.includes('pglite')) {+ const engine = new PGLiteEngine();+ await engine.connect({}); await engine.initSchema(); engines.push(engine);+ }+ if (backends.includes('postgres')) {+ const pg = await isolatedPersistencePostgres(process.env.DATABASE_URL!);+ engines.push(pg.engine); closePostgres = pg.close;+ }+}, 120_000);+afterAll(async () => {+ for (const engine of engines) { await disposePersistenceConsumer(engine); if (engine.kind === 'pglite') await engine.disconnect(); }+ await closePostgres?.();+});+beforeEach(() => { _setRefreshFenceWaitForTests(0); _resetRefreshBarrierCacheForTests(); });+afterEach(() => { resetWriteSwitches(); _setRefreshFenceWaitForTests(null); });++const each = (fn: (f: RefreshFixture) => Promise<void>) => withEnv({ GBRAIN_REFRESH_BARRIER: '1' }, async () => {+ resetWriteSwitches();+ for (const engine of engines) await withRefreshFixture(engine, fn);+});++/**+ * What the writer does for a write admitted before the fence that lands while the refresh drains: commit on the checkout (ledgered),+ * then publish without moving the checkout. The remote moved, so the publish merges it in memory and pushes the merge (#7010).+ */+function ownCommitPublished(f: RefreshFixture, n: number, foreign = false): { own: string; published: string } {+ let own = commitFile(f.root, `alpha/drained-${n}.md`, page(`Drained ${n}`, 'A write admitted before the fence.'), 'gbrain: persist canonical memory update');+ recordCommit(f.root, { sha: own, writer: 'effect' });+ if (foreign) own = commitFile(f.root, `alpha/foreign-${n}.md`, page('Foreign', 'A commit gbrain did not make.'), 'foreign commit');+ git(f.root, 'fetch', '-q', 'origin');+ const remote = git(f.root, 'rev-parse', 'origin/main');+ const tree = git(f.root, 'merge-tree', '--write-tree', own, remote);+ const published = git(f.root, 'commit-tree', tree, '-p', own, '-p', remote, '-m', 'gbrain: integrate remote commits');+ git(f.root, 'push', '-q', 'origin', `${published}:refs/heads/main`);+ git(f.root, 'fetch', '-q', 'origin');+ return { own, published };+}++test('#8828: a refresh whose drain landed gbrain\'s own published commits fast-forwards from them instead of refusing refresh_source_changed', () => each(async f => {+ const upstream = f.push('alpha/two.md', page('Alpha two', 'An upstream observation.'));+ const landed: Array<{ own: string; published: string }> = [];+ const result = await refreshWorktree(f.engine, f.alpha, { hooks: { boundary: async point => {+ if (point === 'drained') landed.push(ownCommitPublished(f, landed.length + 1));+ } } });+ expect(landed).toHaveLength(1);+ expect(result.status).toBe('completed');+ expect(result.target_head).toBe(landed[0]!.published);+ expect(f.head()).toBe(landed[0]!.published);+ expect(git(f.root, 'merge-base', '--is-ancestor', upstream, 'HEAD')).toBe('');+ const [row] = await f.refreshRows();+ expect(row!.state).toBe('completed');+ expect(await f.lastCommit(f.alpha)).toBe(landed[0]!.published);+ expect(await f.lastCommit(f.beta)).toBe(landed[0]!.published);+}), 180_000);++test('#8828: a HEAD move under the fence that is not all gbrain\'s own published commits still refuses refresh_source_changed', () => each(async f => {+ f.push('alpha/two.md', page('Alpha two', 'An upstream observation.'));+ let n = 0;+ const unpublished = await refreshWorktree(f.engine, f.alpha, { hooks: { boundary: async point => {+ if (point !== 'drained') return;+ const sha = commitFile(f.root, `alpha/local-${++n}.md`, page('Local', 'A ledgered commit not pushed yet.'), 'gbrain: persist canonical memory update');+ recordCommit(f.root, { sha, writer: 'effect' });+ } } }).then(() => null, thrown => thrown as { code?: string; message: string });+ expect(unpublished?.code).toBe('refresh_source_changed');+ for (const row of await f.refreshRows()) expect(row.state).toBe('aborted');+}), 180_000);++test('#8828: one foreign commit among gbrain\'s own published commits still refuses refresh_source_changed and merges nothing', () => each(async f => {+ f.push('alpha/two.md', page('Alpha two', 'An upstream observation.'));+ const landed: Array<{ own: string; published: string }> = [];+ const refused = await refreshWorktree(f.engine, f.alpha, { hooks: { boundary: async point => {+ if (point === 'drained') landed.push(ownCommitPublished(f, landed.length + 1, true));+ } } }).then(() => null, thrown => thrown as { code?: string; message: string });+ expect(refused?.code).toBe('refresh_source_changed');+ expect(refused?.message).toContain('a commit or checkout gbrain did not make');+ expect(landed).toHaveLength(2);+ expect(f.head()).toBe(landed[1]!.own);+ const rows = await f.refreshRows();+ expect(rows).toHaveLength(2);+ for (const row of rows) expect(row.state).toBe('aborted');+}), 180_000);