fix(memory): the team sync runs twelve git commands where it ran twenty-one, and two syncs on one machine take turns at the checkout

Merged
FM
2d ago
+688-101

Closes #2214.

What was wrong

tests/memory-notify.test.js timed out on the Windows Node 22 lanes from time to time (runs 47226472980 and 47432254369, and on 47443608709 the file failed in the pool and passed alone): the sync a write schedules did not reach the bare remote within the budget. In both failing runs the sync of the case that timed out landed later and broke the case that followed, because the test left that server alive: on the first run the remote moved while the switch was off, on the second the push of the stop found the other sync still in the checkout and failed on its index lock.

The sync was not stuck, it was expensive. One round against a remote that already has the branch ran git twenty-one times: the remotes were read twice (the check of the checkout and the origin each asked), the remote was asked twice whether it has the branch and pulled twice (once by init, once by pull), the identity of the checkout was written twice on every push, the status was read twice. On this machine the round takes four seconds; a loaded Windows runner takes a second or more per git command.

What changes

  • GitBackend: the remotes are read once; the pull init makes is the pull of the sync, handed over by pull() once instead of asking the remote again, and a push clears it so a later pull() reads the remote; the files a pull brought are read from the commits (rev-parse before and after, then diff or ls-tree), since the summary git prints is cut to a terminal width and says nothing for a pull into an empty checkout; the identity is written once per checkout, when it is missing, and again when the checkout is made anew at the same path; the sync reads no status, which could only answer zero after a pull and a push that went through. Twelve commands for the round, ten when the remote has no branch yet. A remote that stops being readable still fails with the reason, at the init of the next sync or at a pull that has to read it.
  • sync-lock.ts: one team sync at a time on the machine, through the lock of the state store (scripts/lib/state-store/sqlite-compat-lock.js: exclusive create, release by the owner alone, a file still empty held, Windows retries), with a new reclaimDead option of acquireFileLock that reclaims the lock of a process that is gone only while the file still carries that holder's token, so a lock another process took in the meantime is never the one removed and a waiter never spins; its default stays off, so the database keeps failing closed as before. The processes (the server of another session, the hook at the end of a response, egc team sync) take turns through ~/.egc/team-sync.lock, a live holder waited for (90 s), then given up with the lock named. teamSync and teamInit run under it.
  • memory-notify.ts: the server logs how long each sync after a write took and what it moved (memory sync after a write ran, with ms, pulled, pushed, errors), so a slow checkout and a sync that never ran tell apart in ~/.egc/logs/egc-memory-orchestrator.log.
  • tests/memory-notify.test.js: each case stops the server it started in a finally, so a late sync never reaches the next case; a wait that times out prints the tail of the server log and the stderr of the servers; a new case starts two servers of the machine writing at once, for two projects with origins of their own, and proves both documents reach the remote with neither sync failing on the other.
  • tests/team-sync-git-backend.test.js: a shim on the PATH counts the git commands of a round (not on Windows, where git is not started through a shell script): twelve at most, ls-remote, pull, status and remote once each, no identity rewrite. The case of the remote that stops being readable now proves the failure at the next init and at a pull that reads the remote.
  • tests/team-sync-lock.test.js: the lock is taken and released (also when the sync throws), the syncs of one process take turns, a lock of a process that is gone or of this process is taken over, a live holder is waited for, a holder that stays is given up after the wait.
  • CHANGELOG entry under Fixed.

Proof

  • RED, on the build before the change: the counted round runs 21 git commands; with two servers writing at once the document of the second project never reaches the remote.
  • GREEN: memory-notify 7/7, team-sync-git-backend 19/19, team-sync-lock 9/9 (a second process holding the lock included), lib/state-store-lock-ttl 36/36, lib/state-store-adapter-lock 8/8 and lib/state-store-concurrency 17/17 (the lock of the database keeps failing closed by default); team-records, team-sync-global, team-cli, team-cli-server-failures, hooks/team-autosync, lib/team-mcp-client and lib/doctor-team pass as before.
  • eslint 0, local gate 0/0/0/0.