Repository navigation
fix(data-layout): exclude a contended scope from reconcile/report, not just the fetch (#374) - #414
Conversation
…t just the fetch (#374) Follow-up to #406. The sync-lock contention path skipped only pullForScope's fetch/deploy and locked the usage report, but the outer pull() STILL ran the hooks / MCP / co-author reconcile stages for that scope. Each of those calls `loadTeamConfig(localConfig.repo.localPath)` and injects from the SHARED team clone — the same clone the lock holder (a concurrent push) may have checked out on a generated `teamai/push-in-progress` branch. So a losing pull could still read unmerged, unreviewed content off a transient branch and deploy it into the user's tool dirs (hooks/MCP), even though the fetch was skipped. Fix: propagate contention to the outer stages. pullForScope now records a contended scope in a shared `Set<LocalConfig>` accumulator; pull() then drops those scopes from EVERY later stage that reads or mutates the shared clone — hooks/MCP/co-author reconcile and the usage report. Skipping is safe and idempotent: the next uncontended pull reconciles and reports normally. This completes the reviewer's original ask on #406 ("propagate skipped to the outer pull(); at minimum exclude that scope's reconcile/report") — the earlier fix had only locked the report, not the reconcile stages. Test plan (all green): - npx tsc --noEmit — clean; npx vitest run — 191 files / 2639 tests pass - real CLI repro: shared clone checked out on `teamai/push-in-progress` carrying an `unmerged` skill that exists only on that branch; hold `<dataHome>/.sync-lock` and run `pull --force` → "sync in progress elsewhere — skipped"; the unmerged skill is NOT deployed to the workspace and the clone stays on its branch. The reconcile stages emit nothing for the contended scope. Refs #374 (follow-up to P1-2B / #406).
| // transient branch held by the concurrent writer. Skipping is safe/idempotent | ||
| // — the next uncontended pull reconciles and reports normally. | ||
| const reconcileUser = activeUserConfig && !contended.has(activeUserConfig) ? activeUserConfig : null; | ||
| const reconcileProject = projectConfig && !contended.has(projectConfig) ? projectConfig : null; |
There was a problem hiding this comment.
[P1] contended scope 还需要从后面的 cross-team source 阶段排除。这里生成了 reconcileUser/reconcileProject 并用于 hooks、MCP、co-author 和 report,但文件末尾仍用原始 projectConfig ?? activeUserConfig 调用 pullSources。pullSources 会重新从共享 clone 的 teamai.yaml 读取 sources,所以持锁 push 的临时分支仍能把未合并的 source 声明和外部 skill 部署到业务仓库。
本地可稳定复现:主分支 teamai.yaml 没有 sources;让共享 clone 停在 teamai/push-in-progress,该分支仅新增一个 unmerged-source;预置 source cache 后持有 .sync-lock 并运行 pull。日志先显示 sync in progress elsewhere — skipped,随后仍显示 [source:unmerged-source] Synced 1 skills,对应 skill 实际出现在 <project>/.claude/skills。建议 source 阶段也使用过滤后的 scope(例如 reconcileProject ?? reconcileUser),或在 contended 时明确跳过它。
There was a problem hiding this comment.
确认成立,已修复(ae8bed7)。我把过滤后的 scope 应用到了 hooks/MCP/co-author reconcile 和 report,却漏了文件末尾的 pullSources —— 它仍用原始 projectConfig ?? activeUserConfig,会从共享 clone 的 teamai.yaml 重读 sources 并部署外部 skill,竞争时正好能把临时分支上未合并的 source 声明同步进业务仓库。采纳你的建议:source 阶段改用 reconcileProject ?? reconcileUser,竞争 scope 自动排除(非 inherit 的 project 模式下 project 竞争时整个 source 阶段跳过)。真实 CLI 复现:main 的 teamai.yaml 无 sources,teamai/push-in-progress 分支新增 sources: [unmerged-source](指向发布 ext-skill 的外部仓);持锁跑 pull --force → 输出 skipped、无 [source:...] Synced、ext-skill 未部署、clone 分支保留。
…e stage too Follow-up within #414. The contention-filtered scopes were applied to the hooks/MCP/co-author reconcile and usage-report stages, but the final cross-team `pullSources` stage still used the raw `projectConfig ?? activeUserConfig`. pullSources re-reads `sources` from the shared clone's teamai.yaml and deploys external skills, so on lock contention a holder's transient `teamai/push-in-progress` branch could still sync unmerged source declarations (and their external skills) into the workspace. Fix: the source stage now uses the contention-filtered `reconcileProject ?? reconcileUser`, so a contended scope is excluded here as well (in non-inherit project mode the whole source stage is skipped when the project scope is contended). Test plan (all green): - npx tsc --noEmit — clean; npx vitest run — 191 files / 2639 tests pass - real CLI repro: main teamai.yaml has NO sources; a `teamai/push-in-progress` branch adds `sources: [unmerged-source]` pointing at an external repo that publishes `ext-skill`; hold `.sync-lock` and `pull --force` → "sync in progress elsewhere — skipped", no `[source:...] Synced` line, and `ext-skill` is NOT deployed to the workspace; the clone stays on its branch. Refs #374 (P1-2B follow-up).
| // deploy from a clone we could not safely snapshot, and mark it contended | ||
| // so the caller also skips the later reconcile/report stages that touch the | ||
| // same clone. Idempotent: the next pull (once the lock frees) syncs normally. | ||
| policy.contended?.add(localConfig); |
There was a problem hiding this comment.
[P1] Keep the sync lock through all shared-clone reads
contended only covers a pull that loses the initial acquireLock. When that acquire succeeds, refreshTeamRepo releases the lock before pullForScope reloads teamai.yaml, scans/copies resources, and before the outer hook/MCP/source stages run. A concurrent teamai push can therefore acquire the lock immediately after refresh, check out its generated push branch, and those unlocked stages still consume that transient tree; this scope is never added to contended.
I reproduced this locally by making releaseLock simulate the next push checkout (adding a skill that exists only on the transient branch). The same pull({ force: true }) then logged Synced 1 skills and copied that unmerged skill into the project tool directory. Please hold the partition lock for the full clone-consuming scope (refresh + deploy/reconcile/source), or materialize an immutable snapshot while locked and make every later stage read only that snapshot.
There was a problem hiding this comment.
你是对的,这是根本性的 TOCTOU —— 前几轮我一直在扩大「排除 contended scope」的覆盖面,但那只处理「一开始就没抢到锁」的情况。抢到锁的路径里,refreshTeamRepo 在 fetch 后就释放了锁,之后 scan/deploy + 外层 reconcile/source/report 全在无锁窗口读共享 clone,并发 push 能在此刻抢锁 checkout 临时分支。已按你的建议 a 修复(93736ee):把锁上提到 pull(),在每个 git 模式 scope 消费共享 clone 的整个生命周期内持有(fetch + scan/deploy + reconcile + source + report),结束后在 finally 统一释放。因锁非重入且现由 pull() 持有,内层的锁全部移除(refreshTeamRepo 不再锁、reportUsageToTeam 不再锁)。抢不到锁的 scope 进 contended 并从所有阶段排除,同时打印可见的 "[scope] sync in progress elsewhere — skipped"。http/self 不取锁(无共享 clone / 自有 reports-lock)。
真实 CLI 复现:外部持锁(活 PID)、clone 停在 teamai/push-in-progress(带 unmerged skill),pull --force → 打印 skipped,unmerged 和正常的 demo skill 都没有被部署(整个 scope 的 fetch/scan/deploy/reconcile/source 都被锁门控),外部锁完好未被偷删。2639 单测全绿。
… pull, closing the TOCTOU window The previous contention handling only covered a pull that LOST the initial acquireLock. But when acquire succeeded, refreshTeamRepo released the lock right after the fetch — before pullForScope scans/copies resources and before the outer hooks/MCP/co-author/source/report stages, all of which read the shared clone. A concurrent `teamai push` could acquire the lock in that window, check out its generated push branch, and those now-unlocked stages would consume the transient tree — deploying unmerged, unreviewed content. The scope was never marked contended because this pull did hold the lock initially. Fix: move the lock up to pull() and hold it across each git-mode scope's ENTIRE clone-consuming lifecycle. pull() acquires `<dataHome>/.sync-lock` per git-mode scope up front (user and project clones are distinct); a scope whose lock is held by another process is added to `contended` and excluded from every stage; all held locks are released together in a finally after the source stage. Because the lock is non-reentrant and now owned by pull(), the inner locks are removed: refreshTeamRepo no longer locks, and reportUsageToTeam no longer locks (its caller holds it). http/self scopes take no lock (no shared clone / own reports-lock). Contention now shows a visible "[scope] sync in progress elsewhere — skipped" info line, since the whole scope is skipped (a plain success line would mislead). Test plan (all green): - npx tsc --noEmit — clean; npx vitest run — 191 files / 2639 tests pass - real CLI: hold `<dataHome>/.sync-lock` with a live PID, then `pull --force` on a clone parked on `teamai/push-in-progress` with an `unmerged` skill → visible "sync in progress elsewhere — skipped"; NEITHER the unmerged skill NOR the normal `demo` skill is deployed (the entire scope — fetch/scan/deploy/reconcile/ source — is gated by the lock), and the external lock is left intact. Refs #374 (P1-2B follow-up).
| // Non-fatal — pull continues even if hook migration fails | ||
| } | ||
|
|
||
| // Shared-clone concurrency (issue #374). A git-mode scope's team clone is |
There was a problem hiding this comment.
[P1] Acquire the scope lock before auto-migrating hooks
The new full-lifecycle lock still starts after autoMigrateHooksIfNeeded(). That migration calls autoDetectInit(), reads teamai.yaml from the same shared clone, trusts its toolPaths, and writes executable hook configuration. If a concurrent push already holds the lock while its generated branch is checked out, this step consumes the transient config before lockScope notices contention.
I reproduced this locally with legacy ~/.claude/settings.json, a held scope lock, and a transient-branch config whose Claude settings target was .rogue/settings.json: the same pull() first logged Updated teamai hooks in .../.rogue/settings.json, then logged [project] sync in progress elsewhere — skipped. Thus an unmerged branch still causes a control-plane write even though the scope is supposedly skipped wholesale. Please run migration only after the active scope lock is acquired (or make migration use a snapshot/config source that does not read the shared clone).
There was a problem hiding this comment.
确认成立,已修复(5b4690d)。full-lifecycle 锁虽然上提到了 pull(),但仍晚于 step-0 的 autoMigrateHooksIfNeeded() —— 它 autoDetectInit() 读共享 clone 的 teamai.yaml/toolPaths 并写 hook 配置,发生在 lockScope 之前,所以并发 push 持锁+临时分支时,这一步会消费临时 config 并按其 toolPaths 写控制面(哪怕该 scope 随后被判 skipped)。
采纳你的建议,把 migration 拆成两半:
legacyHooksNeedReinject()—— 只读~/.claude/settings.json的格式门控(不碰共享 clone),仍在前面评估;reinjectLegacyHooks(localConfig)—— 真正读teamConfig.toolPaths并写 hook 的部分,改为接收已持锁的 config,在pull()的 scope 锁内、reconcile 之前、对非-contended scope(reconcileProject ?? reconcileUser)运行。若活跃 scope 都 contended 则跳过,下次无竞争的 pull 再迁移(幂等)。
真实 CLI 复现:legacy ~/.claude/settings.json(有 teamai hooks、无 hook-dispatch)、共享 clone 停在 teamai/push-in-progress、其 teamai.yaml 把 Claude settings 指向 .rogue/settings.json;持锁跑 pull --force → 输出 skipped,且任何位置都没有写出 .rogue/settings.json(migration 不再在锁前从临时分支读/写)。2639 单测全绿。
… lock, not before it The full-lifecycle lock still started AFTER `autoMigrateHooksIfNeeded()` (pull's step 0). That migration called `autoDetectInit()`, read `teamai.yaml` + `toolPaths` from the shared clone, and wrote executable hook config — all before `lockScope` ran. So while a concurrent push held the lock with its generated branch checked out, this step consumed the transient config and could write hooks to whatever path that branch's `toolPaths` declared, even though the scope was then reported as skipped. Fix: split the migration. - `legacyHooksNeedReinject()` — the cheap gate that reads ONLY `~/.claude/settings.json` (no shared clone). Still evaluated up front. - `reinjectLegacyHooks(localConfig)` — the actual reinject, which reads `teamConfig.toolPaths` from the shared clone and writes hook config. Now takes an already-locked config and runs INSIDE pull()'s scope lock, against a non-contended scope (`reconcileProject ?? reconcileUser`), before the reconcile stages. If the only active scopes are contended it is skipped — the next uncontended pull migrates (idempotent). Test plan (all green): - npx tsc --noEmit — clean; npx vitest run — 191 files / 2639 tests pass - real CLI: legacy `~/.claude/settings.json` (teamai hooks, no hook-dispatch); shared clone parked on `teamai/push-in-progress` whose teamai.yaml points Claude `settings` at `.rogue/settings.json`; hold `.sync-lock` and `pull --force` → "sync in progress elsewhere — skipped" and NO `.rogue/settings.json` is written anywhere (migration no longer reads/writes from the transient branch before the lock). Refs #374 (P1-2B follow-up).
…e stage too Follow-up within Tencent#414. The contention-filtered scopes were applied to the hooks/MCP/co-author reconcile and usage-report stages, but the final cross-team `pullSources` stage still used the raw `projectConfig ?? activeUserConfig`. pullSources re-reads `sources` from the shared clone's teamai.yaml and deploys external skills, so on lock contention a holder's transient `teamai/push-in-progress` branch could still sync unmerged source declarations (and their external skills) into the workspace. Fix: the source stage now uses the contention-filtered `reconcileProject ?? reconcileUser`, so a contended scope is excluded here as well (in non-inherit project mode the whole source stage is skipped when the project scope is contended). Test plan (all green): - npx tsc --noEmit — clean; npx vitest run — 191 files / 2639 tests pass - real CLI repro: main teamai.yaml has NO sources; a `teamai/push-in-progress` branch adds `sources: [unmerged-source]` pointing at an external repo that publishes `ext-skill`; hold `.sync-lock` and `pull --force` → "sync in progress elsewhere — skipped", no `[source:...] Synced` line, and `ext-skill` is NOT deployed to the workspace; the clone stays on its branch. Refs Tencent#374 (P1-2B follow-up).
…reconcile-contention fix(data-layout): exclude a contended scope from reconcile/report, not just the fetch (Tencent#374)
Context
Follow-up to #406 (P1-2B, already merged). A round of review on #406 flagged
that the sync-lock contention path was incomplete, and #406 landed before the
last fix — so this ships it as its own PR.
On lock contention, #406 skipped only
pullForScope's fetch/deploy and locked theusage report. But the outer
pull()still ran the hooks / MCP / co-authorreconcile stages for that scope. Each of those calls
loadTeamConfig(localConfig.repo.localPath)and injects from the shared teamclone — the same clone the lock holder (a concurrent
push) may have checkedout on a generated
teamai/push-in-progressbranch. So a losing pull could stillread unmerged, unreviewed content off a transient branch and deploy it into the
user's tool dirs (hooks/MCP), even though the fetch itself was skipped.
What this PR does
Propagate contention to the outer stages.
pullForScoperecords a contended scopein a shared
Set<LocalConfig>accumulator;pull()then drops those scopes fromevery later stage that reads or mutates the shared clone — hooks / MCP /
co-author reconcile and the usage report. Skipping is safe and idempotent: the
next uncontended pull reconciles and reports normally.
This completes the reviewer's original ask ("propagate
skippedto the outerpull(); at minimum exclude that scope's reconcile/report") — the earlier fix hadonly locked the report, not the reconcile stages.
Test plan (all executed green)
npx tsc --noEmit— cleannpx vitest run— 191 files / 2639 tests passteamai/push-in-progresscarrying an
unmergedskill that exists only on that branch; hold<dataHome>/.sync-lockand runpull --force→ "sync in progresselsewhere — skipped"; the unmerged skill is NOT deployed to the
workspace and the clone stays on its branch. The reconcile stages emit
nothing for the contended scope.
Refs #374 (follow-up to P1-2B / #406).