Skip to content

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 - #2218

Merged
Fmarzochi merged 7 commits into
mainfrom
fix/memory-notify-sync-cost
Dec 5, 2026
Merged

Fmarzochi merged 7 commits into
mainfrom
fix/memory-notify-sync-cost

Conversation

@Fmarzochi

@Fmarzochi Fmarzochi commented Dec 5, 2026 •

Copy link
Copy Markdown
Owner

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.

…ty-one, and two syncs on one machine take turns at the checkout

memory-notify.test.js timed out on the Windows Node 22 lanes from time to time: the sync a write schedules read the remotes twice, asked the remote twice whether it has the branch and pulled it twice, wrote the identity of the checkout twice per push and read the status twice, twenty-one git commands where a loaded Windows runner takes a second or more each. The remotes are read once, the pull of the init is the pull of the sync, the identity is written once per checkout, the status is read once. A sync that found another in flight failed on the index lock of the first and reported a push that did not happen: the syncs of a process queue behind each other and the processes take turns through a lock file beside the checkout. The server logs how long each sync after a write took. The test stops the server of each case before the next, says what the server logged when a sync does not land in time, and proves two servers writing at once both reach the remote.

Closes #2214

Signed-off-by: Felipe Marzochi <fmarzochi@gmail.com>

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 8 files

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread tests/team-sync-lock.test.js Resolved
Comment thread tests/memory-notify.test.js Resolved
…les a pull brought from the commits, and reads no status

The lock of the team sync is the one of the state store (exclusive create, release by the owner alone, a file still empty held), with a new option that reclaims the lock of a process that is gone only while the file still carries that holder token, so a lock another process took in the meantime is never removed and a waiter never spins. 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. A push clears the pull the init kept, a checkout made anew at the same path gets its identity again, and the sync reads no status, which could only answer zero after a pull and a push that went through. The tests prove the pull into an empty checkout and into one that is behind, the identity of a recreated checkout, the empty lock held, and both documents of two servers writing at once on the remote by their hashes.

Signed-off-by: Felipe Marzochi <fmarzochi@gmail.com>

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6 issues found across 8 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="scripts/lib/state-store/sqlite-compat-lock.js">

<violation number="1" location="scripts/lib/state-store/sqlite-compat-lock.js:253">
P1: Stale-lock recovery can delete a new owner's lock: `unlinkLockIfOwned` checks the token and unlinks the path in separate operations, so concurrent stale-lock waiters can remove a lock acquired between those operations. Make stale-lock takeover atomic (for example, atomically claim the observed lock and verify the claimed token) before allowing another sync to proceed.</violation>
</file>

<file name="CHANGELOG.md">

<violation number="1" location="CHANGELOG.md:41">
P3: This headline implies every sync runs twelve Git commands, but an empty remote branch takes ten. Change it to “up to twelve” and include the no-branch exception.</violation>

<violation number="2" location="CHANGELOG.md:41">
P3: The lock description omits the 90-second wait limit. A sync that waits longer fails with 'Another team sync has held ... nothing was synced this time.' Add that limit to this bullet so the error is documented.</violation>
</file>

<file name="tests/team-sync-git-backend.test.js">

<violation number="1" location="tests/team-sync-git-backend.test.js:464">
P2: This push does not test the cache invalidation described by the test: the preceding `fresh.pull()` already consumed the init result. Add a push-before-first-pull case, or a second checkout, and assert that its following pull reads the remote rather than returning the cached init result.</violation>
</file>

<file name="mcp/servers/egc-memory/src/sync/GitBackend.ts">

<violation number="1" location="mcp/servers/egc-memory/src/sync/GitBackend.ts:200">
P3: `headCommit` treats every git failure as "no commit", so a failed `rev-parse` after a successful pull makes `pullBranch` return `[]`. The pulled count then reads zero for a pull that brought files. Catch only the unborn-HEAD case, or let other errors propagate.</violation>

<violation number="2" location="mcp/servers/egc-memory/src/sync/GitBackend.ts:209">
P3: `push()` no longer ensures the checkout's commit identity, so it depends on an earlier `init()` that did not return at the `readRemote: false` branch. Call `ensureGitIdentity()` again in `push()` before the commit, or document that `push()` requires a full `init()`.</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread tests/team-sync-git-backend.test.js Resolved
Comment thread CHANGELOG.md Resolved
Comment thread CHANGELOG.md Resolved
…reads the remotes again, and the sync log says whether a push went out and how long a failed sync took

The checkout is ready only when user.name and user.email are both set, so a merge of the pull never meets an unknown author. The retry of adding origin reads the remotes again instead of the list it already had. The log line of a sync says pushed as a yes or a no, since one commit carries every document, and a sync that failed or gave up on the lock logs how long it waited too. The lock test runs a second process that holds the lock through the built module and proves the sync of this process starts after that one ended.

Signed-off-by: Felipe Marzochi <fmarzochi@gmail.com>

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 5 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="CHANGELOG.md">

<violation number="1" location="CHANGELOG.md:41">
P3: The changelog overstates remote reuse: `ensureOriginRemote` deliberately reads remotes again after a concurrent `addRemote` failure. Qualify “read once” to the normal path or mention the retry.</violation>

<violation number="2" location="CHANGELOG.md:41">
P3: The changelog misstates what the removed status check proved: it could return zero even when the push failed or no push ran. Say that the status check was redundant for conflict reporting instead of tying zero to a successful push.</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread CHANGELOG.md Resolved
Comment thread CHANGELOG.md Resolved
…tep, the push gets the identity itself, and a rev-parse that fails with a reason is thrown

The reclaim renames the lock aside, one waiter getting it, reads it where it landed and removes it only when it is the lock that was seen; a file that is not that lock is linked back, and a newer lock already at the path stays. The push ensures the identity of the checkout, so a push after an init that did not read the remote commits too. rev-parse runs with --quiet, so a HEAD without a commit answers null and any other failure is thrown instead of reading as an empty pull. The test adds a checkout that pushed before its first pull, and the changelog says up to twelve commands, the retry of the origin, why the status check was redundant and the 90 s wait of the lock.

Signed-off-by: Felipe Marzochi <fmarzochi@gmail.com>
…his PID or an empty lock left for a minute is reclaimed too, and the identity keeps what the person set

The poll tries the exclusive create before it looks at the deadline and again without a pause after a reclaim, so a wait shorter than one poll still takes a lock that was abandoned. A lock that names this very process with a token it does not hold is a leftover of an earlier life of the PID (a restarted container), and a lock file left with no holder written for over a minute was abandoned by a process killed while writing it: both are reclaimed the same way, renamed aside and verified. The identity of the checkout is read key by key, a key without a value is written and a key the person set is kept, and the memo of a checkout seen to is the file its configuration was in, so a checkout made anew by another process is looked at again. The files a pull brought are listed with core.quotepath off, so a path with a letter outside ASCII is named as it is.

Signed-off-by: Felipe Marzochi <fmarzochi@gmail.com>
…c-cost

# Conflicts:
#	CHANGELOG.md

Signed-off-by: Felipe Marzochi <fmarzochi@gmail.com>
…its configuration, not by the number of the file

git writes a new configuration file on every change and the number of the file can be the one just freed, which is what happened on the Ubuntu lanes: the memo took a configuration rewritten by the person for the one it had seen, and a name left empty was not written back. The memo now keeps the text of .git/config, a small file read without running git, and compares it exactly.

Signed-off-by: Felipe Marzochi <fmarzochi@gmail.com>
@sonarqubecloud

sonarqubecloud Bot commented Dec 5, 2026

Copy link
Copy Markdown

@codecov

codecov Bot commented Dec 5, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.21053% with 12 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
scripts/lib/state-store/sqlite-compat-lock.js 84.21% 12 Missing ⚠️

📢 Thoughts on this report? Let us know!

@Fmarzochi
Fmarzochi merged commit 78d62a5 into main Dec 5, 2026
35 checks passed
@Fmarzochi
Fmarzochi deleted the fix/memory-notify-sync-cost branch December 5, 2026 04:39
@Fmarzochi Fmarzochi moved this to Done in EGC Roadmap Dec 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

memory-notify.test.js times out on the Windows Node 22 lanes: the scheduled sync does not reach the remote within the budget

1 participant