Repository navigation
fix(knowledge): lock the shared worktree against concurrent commands - #2068
Conversation
Every knowledge-producing command - push, remove, manifest edits and promotion - calls withKnowledgeWorktree on the SAME fixed path, and each one removes that worktree with --force both on entry to clear an interrupted run and in `finally`. Nothing serialized them. If two commands overlapped, the later one's entry cleanup deleted the checkout the earlier one was still committing in: changes made by A were pushed in B's PR, or A's own changes silently vanished when B's `finally` ran. The active tree was never touched, which made the loss look like a merge failure rather than a race. Branch worktrees already take a lock beside the checkout for this exact reason (Tencent#1443). The knowledge checkout had no lock at all. - Add KNOWLEDGE_LOCK_FILENAME and take an exclusive lock beside `wt` for the whole lifecycle: stale cleanup, fetch, worktree add, the callback, and the final removal. A contender is refused with CheckoutRefusedError before it can mutate anything, matching how a busy checkout is handled elsewhere. - Put the lock BESIDE the worktree, never inside it: `worktree remove --force` would delete a lock held inside while its owner still holds it. Use a dedicated name instead of `.sync-lock`, because push and promotion already hold `.sync-lock` when they call here and would deadlock against themselves. - Skip and recognize the new lock (and its reclaim artifacts) during migration, so a stale lock is never copied into the partition and mistaken for a live one. - Add knowledge-worktree-lock.test.ts covering refusal without mutation, lock ownership across the callback, and release when the callback throws. Signed-off-by: TINGyu123644 <[email protected]>
|
Quick review orientation (TL;DR): The key design decision is why this is a new
Verification extracted the real function body from both the pre-fix commit and this branch and replayed the same interleaving: pre-fix, B deleted A's in-use checkout; fixed, B is refused and A's checkout is untouched. CI is green. Happy to split the migration changes into a separate commit if that helps review. |
reports-branch.ts lives in src/utils/, so `./update.js` resolved to
src/utils/update.js, which does not exist - acquireLock and releaseLock are
exported from src/update.ts. A type check reports it as
error TS2307: Cannot find module './update.js'
and at runtime the module would fail to resolve. Use the same `../` level the
file already uses for `../types.js`.
Verified with `tsc --noEmit` against the upstream baseline: this file now
reports no error the pre-change tree does not also report.
Signed-off-by: TINGyu123644 <[email protected]>
|
Follow-up commit 60e0651 fixes a real mistake in the first commit: Caught by a type check, not by the harness I described earlier — the harness extracts the function body and stubs its dependencies, so it never resolves the module specifier. Worth stating plainly: the string How it was checked: Method: ran The environment still has no installed dependencies, so the same run reports ~26k |
No other confident findings in the current diff. Nothing was run, built, or installed from the PR. |
|
On the real-CLI record: I could not produce one for this change, and I would rather say so precisely than dress up a substitute.
What I do have is behavioural, not stubbed: If you have a POSIX host, the representative case is two concurrent |
|
I tried to produce the verify-world record on Linux instead of stopping at "Windows is not supported". Result: the obstacle is not the host platform, and I want to correct what I said earlier. What I got working:
Where this change still has no world record:
I added an origin and retried. Then the real reason showed up: the world's — so any So the honest state of this PR is unchanged from what I wrote before, with a sharper reason:
If the review requires that record, this PR cannot supply it here. I would rather say that plainly than leave a claim I cannot back. The representative case remains available to anyone with a POSIX host and a self-mode repo: two concurrent |
|
The real-CLI record the thread was waiting for — produced on a POSIX host (Linux), with the packed build, in a real self-mode project. Setup: a team repo (teamai.yaml + main ( r1's search is wrong: r2 entered the shared knowledge checkout concurrently and its stale-cleanup this PR (main + #2068, same setup): Also verified on latest main + this PR (stacked with #1954/#2076): typecheck, build, lint, unit 202 passed, e2e 4 passed. The PR is mergeable from my side. |
Fixes #2067
What changed
withKnowledgeWorktree()now holds an exclusive lock for the entire life of the shared knowledge checkout: stale cleanup, fetch,worktree add, the caller's callback, and the final removal. A contender is refused withCheckoutRefusedErrorbefore it makes any git call, so a running command keeps its checkout and its uncommitted work.Why a dedicated lock
wt, not inside it:worktree remove --forcewould delete a lock held inside while its owner is still holding it..sync-lock:pushandpromotion-publishalready hold.sync-lockwhen they call into this function, so reusing it would deadlock against themselves.remove,manifest-editandpromotion-publishhold no lock today, so they now contend on this one and are serialized withpushas well.This follows the existing pattern in
branch-worktree.ts(removeOldCheckouts, #1443), where a checkout is never removed while another command holds its lock.Tests
These run real
git worktreeoperations (about 3s each), not stubs: each case builds a repository, takes the lock throughwithKnowledgeWorktree, and asserts what happened to the checkout.tsc --noEmitover the repository reports 0 errors.On the real-CLI record
I could not produce an end-to-end
verify:worldrun for this one, and it is worth being precise about why rather than implying it was done:withKnowledgeWorktreeis reached fromremove.ts:60,manifest-edit.ts:51,push.ts:1365andpromotion-publish.ts:79— all of them self-mode branches. A workspace-scope project never enters this function, soteamai removethere runs a different path and exercises none of this code.verify:worldharness refuses to run on Windows:scripts/verify/world.tsthrows "A world needs a POSIX shell; Windows is not supported."teamai init .reads the business repo'sorigin, and git has already applied theurl.<file>.insteadOfrewrite that makes a fake remote reachable, so the URL comes back asfile://…andinitrejects it as an invalid repo URL.So the honest state is: the lock's behaviour is proven by the three real-git tests above, and the end-to-end CLI run needs a POSIX host. If a reviewer has one, the pair to run is two concurrent
teamai removecalls in a self-mode world — one should reportthe knowledge checkout is busyinstead of deleting the other's checkout.Two earlier assertions in this PR description are superseded and were wrong: the pre-fix harness was extracted-and-stubbed, which cannot see module specifiers (it missed
'./update.js', since corrected to'../update.js'), and it could not have observed real lock contention either.