Repository navigation
Description
Machine-Readable Contract
contract_schema_version: v1
issue_kind: implementation
parent_issue: "#2190"
goal_ref: "#2190 AC7: remote identity observation前に取得する、canonical git-common-dir + fixed dedicated identity スコープの process-scoped lifecycle mutex primitiveを提供する"
change_kind: codeParent Issue
Parent Goal Ref
- Goal: 実装: fix: bind issue-refinement control-plane preflight to a dedicated linked worktree #2190(親)は production preflight profile 群を remote default-ref OID に fail-closed で束縛した detached dedicated linked worktree lifecycle へ移行する。本Issueはそのための process-scoped lifecycle mutex primitive だけを担う。
- Desired Destination: 実装: fix: bind issue-refinement control-plane preflight to a dedicated linked worktree #2190 の Allowed Paths のうち
scripts/agent-ops/worktree_bootstrap_exec.py/scripts/agent-ops/worktree_catalog.pyと concurrency テストのみを扱う。
Current Validated Scope
- Child 1(実装: control-plane preflight 用 sanitized Git subprocess 実行基盤を新設する #2196)および 実装: #2197向け remote protocol Git builderを閉鎖型に hardenする #2378 で提供された trusted sanitized Git executable/environment と closed operation を前提とする。
scripts/agent-ops/worktree_bootstrap_exec.pyにだけ、canonical existinggit-common-dirと fixed dedicated identity をキーとする process-scoped lifecycle mutex primitive を実装する。acquire は 実装: control-plane preflight の remote default-ref OID observe/fetch/pin/verify プロトコルを実装する #2197 consumer が remote identity observation より前に呼ぶ prerequisite であるが、本Issueは consumer protocol ordering を実装・実証しない。- canonical common-dir は
worktree_bootstrap_exec.pyが 実装: control-plane preflight 用 sanitized Git subprocess 実行基盤を新設する #2196/実装: #2197向け remote protocol Git builderを閉鎖型に hardenする #2378 の trusted sanitized Git executable/environment を通じて対象 repository から取得する absolute realpath の existinggit-common-dirのみを用いる。caller は argv、path、key を供給できない。ここで独自の stdout validation を追加せず、未sanitize helper を使わず、worktree_catalog.pyに Git 実行を複製しない。 - acquire は非推測可能かつ process-local・非serializable な ownership token を内包する guard だけを返す。
assert_held()とrelease()は同一 token と acquire 時 owner PID を必須とし、foreign process、released guard、key mismatch を拒否する。guard は reentrant ではない。 - kernel advisory mutex と lease を混同しない。TTL、heartbeat、stale scanner、lease takeover、PID file、daemon は導入しない。fixed lock-file anchor の存在・内容は authority ではなく、release 時に unlink しない。
worktree_catalog.pyは catalog/deadline の既存責務を維持し、mutex state manager や Git execution authority にしない。
Remaining Parent Gaps
- remote OID observe/fetch/pin/verify/worktree protocolの本体実装(実装: fix: bind issue-refinement control-plane preflight to a dedicated linked worktree #2190 AC4)は 実装: control-plane preflight の remote default-ref OID observe/fetch/pin/verify プロトコルを実装する #2197 が唯一所有する。実装: control-plane preflight の remote default-ref OID observe/fetch/pin/verify プロトコルを実装する #2197 は本Issueの guard を prerequisite として consume し、remote observation → child execution → artifact validation attempt → finally release の統合 ordering を実装・検証する。実装: control-plane preflight lifecycle 用 git-common-dir スコープ排他ロックを実装する #2198 は未実装 consumer の ordering をテストで実証すると主張しない。
- outer lifecycle controller の ownership は 実装: control-plane preflight の remote default-ref OID observe/fetch/pin/verify プロトコルを実装する #2197/実装: control-plane preflight を dedicated linked worktree の identity probe と command class 分離で 4 production profile へ移行する #2199/実装: control-plane preflight の artifact confinement と #2190 全AC最終検証を実装する #2200 の統合で確定する。owner は guard を acquire した outer controller であり、child/descendant が terminal state となり artifact validation の試行が成功・失敗を問わず完了した後に、その同じ owner が
finallyで explicit release を呼ぶ。実装: control-plane preflight lifecycle 用 git-common-dir スコープ排他ロックを実装する #2198 はその consumer flow を実装しない。 - identity probe・command class分離・primary invariant・4 production profile移行(実装: fix: bind issue-refinement control-plane preflight to a dedicated linked worktree #2190 AC1/AC3/AC5/AC9/AC10)は Child 4(後続 Issue)で扱う。
- artifact confinement・実装: fix: bind issue-refinement control-plane preflight to a dedicated linked worktree #2190 全AC最終検証(AC6/AC11/AC12)は Child 5(後続 Issue)で扱う。
Outcome
scripts/agent-ops/worktree_bootstrap_exec.py に、trusted sanitized Git 経由で内部導出した canonical existing git-common-dir と fixed identity をキーとする lifecycle mutex primitiveだけが実装される。primitive は acquire、owner-process assert_held、same-token explicit release、context-manager の例外時 release、bounded contention、foreign/released/key-mismatch rejection、holder death 後の reacquire を提供する。kernel lock の release と owner が呼ぶ explicit release は別の状態遷移として扱い、TTL/stale/takeover/PID-file/daemon を持たない。tests/agent_ops/test_control_plane_worktree_concurrency.py の focused pytest が primitive 単体のこの契約と primary snapshot 不変を実証する。remote observe/fetch/pin/verify/worktree、child/artifact processing、consumer ordering は所有しない。
Runtime Verification Applicability
decision: immediate
reason: "isolated tmp git repo fixtureに対し2並行subprocessを起動し、canonical common-dir mutex・bounded timeout・owner/release境界・holder death後reacquire・primary snapshot不変性を実証する。production primary checkoutを操作しない。"
applicable_acs:
- AC1
- AC2
- AC3
- AC4
- AC5
- AC6
execution_environment:
cli_tools:
- python3
- uv
- git
auth_required: false
network_required: false
skip_conditions:
- "isolated tmp git repo fixtureを構築できない実行環境ではSKIPではなくenvironment blockedとして扱う"
fallback_policy:
fallback_success_is_pass: false
notes: "unbounded wait、caller supplied key、TTL/stale takeover、foreign/released guardの受理、又はholder death後のreacquire不能をPASSに変換しない。timeoutはcontrol_plane_unavailable/lifecycle_lock_busyである。"
artifact_requirements:
- "pytest output(canonical common-dir acquire、owner validity/release、2並行process、holder death後reacquire)"In Scope
git-common-dir + fixed dedicated identityスコープの process-scoped lifecycle kernel mutex primitiveworktree_bootstrap_exec.py内で 実装: control-plane preflight 用 sanitized Git subprocess 実行基盤を新設する #2196/実装: #2197向け remote protocol Git builderを閉鎖型に hardenする #2378 の trusted sanitized Git executable/environment を通じ対象 repository から内部導出する、absolute realpath の existinggit-common-dirのみを鍵にすること- caller 入力を受けない fixed lock path、opaque ownership token、owner-process
assert_held、same-token explicit release、context-manager 例外時 release の契約と実装 - bounded monotonic wait、timeout時の
control_plane_unavailable/lifecycle_lock_busy - foreign process、double/released guard、key mismatch の拒否、holder process death 後の kernel lock release と後続 process の reacquire
- 2並行process fixture による primitive 単体テストと primary 不変性
Out of Scope
- sanitized Git subprocess実行基盤そのもの(実装: control-plane preflight 用 sanitized Git subprocess 実行基盤を新設する #2196/実装: #2197向け remote protocol Git builderを閉鎖型に hardenする #2378 の既存責務)および、このmutex取得のための独自 stdout validation・未sanitize helper 利用・
worktree_catalog.pyへの Git 実行複製 - TTL、heartbeat、stale scanner、lease takeover、PID file、daemon、serializable token、generic lock API
- remote OID observe/fetch/pin/verify/worktree protocolの本体実装(実装: fix: bind issue-refinement control-plane preflight to a dedicated linked worktree #2190 AC4、実装: control-plane preflight の remote default-ref OID observe/fetch/pin/verify プロトコルを実装する #2197 が唯一所有)
- detached worktree作成、child/artifact processing、artifact validation、consumer protocol ordering の実装または 実装: control-plane preflight lifecycle 用 git-common-dir スコープ排他ロックを実装する #2198 テストでの統合実証。これらは 実装: control-plane preflight の remote default-ref OID observe/fetch/pin/verify プロトコルを実装する #2197/実装: control-plane preflight を dedicated linked worktree の identity probe と command class 分離で 4 production profile へ移行する #2199/実装: control-plane preflight の artifact confinement と #2190 全AC最終検証を実装する #2200 が guard を consume して所有する
- identity probe・command class分離・primary invariant・4 production profile移行(実装: fix: bind issue-refinement control-plane preflight to a dedicated linked worktree #2190 AC1/AC3/AC5/AC9/AC10、Child 4 で扱う)
- artifact confinement(実装: fix: bind issue-refinement control-plane preflight to a dedicated linked worktree #2190 AC6/AC11、Child 5 で扱う)
WORKTREE_CATALOG_ENTRY_V1スキーマ変更
Acceptance Criteria
- AC1:
worktree_bootstrap_exec.pyが 実装: control-plane preflight 用 sanitized Git subprocess 実行基盤を新設する #2196/実装: #2197向け remote protocol Git builderを閉鎖型に hardenする #2378 の trusted sanitized Git executable/environment を通じて、caller supplied argv/path/key を受けず、対象 repository の absolute realpath で existing なgit-common-dirのみから fixed lock key を導出することを primitive 単体で実証する。独自 stdout validation、未sanitize helper、worktree_catalog.pyの Git 実行複製を行わない。 - AC2: 同一 repository の primary worktree と linked worktree から起動した competing process が同じ mutex を競合し、異なる repository 同士は競合しないことを primitive 単体で実証する。
- AC3:
LOCK_EX | LOCK_NBの monotonic bounded retry が、deadline 内の owner explicit release 後に取得でき、deadline 超過時にcontrol_plane_unavailable/lifecycle_lock_busyを返すことを実証する。 - AC4: acquire が返す非推測可能な process-local ownership token を持つ同一 owner PID だけが
assert_held()と explicitrelease()を実行できること、foreign process、double/released guard、異なる canonical common-dir に束縛された guard の使用を拒否し、再検証時に暗黙 reacquire しないことを実証する。 - AC5: holder process を SIGKILL した後、lock file を削除せず後続 process が reacquire できることを実証する。これは holder death による kernel FD/lock release であり、owner が呼ぶ explicit release とは異なる。primary branch/HEAD/status は実行前後で不変である。
- AC6: lock file を release 時に unlink せず、success、context-manager body の例外、timeout の全経路で FD が最終的に close されることを primitive 単体で実証する。
Verification Commands
# AC1
$ uv run --locked pytest tests/agent_ops/test_control_plane_worktree_concurrency.py::test_given_primary_or_linked_worktree_when_acquired_then_canonical_common_dir_and_fixed_key -q# AC2
$ uv run --locked pytest tests/agent_ops/test_control_plane_worktree_concurrency.py::test_given_competing_primary_and_linked_processes_when_acquired_then_competing_processes_by_common_dir -q# AC3
$ uv run --locked pytest tests/agent_ops/test_control_plane_worktree_concurrency.py::test_given_busy_lock_when_owner_releases_after_observed_contention_then_bounded_timeout_and_explicit_release -q# AC4
$ uv run --locked pytest tests/agent_ops/test_control_plane_worktree_concurrency.py::test_given_guard_when_foreign_released_or_key_tampered_then_owner_token_foreign_released_or_key_mismatch -q# AC5
$ uv run --locked pytest tests/agent_ops/test_control_plane_worktree_concurrency.py::test_holder_death_reacquire_and_primary_invariant_with_long_lived_fork_child -q# AC6
$ uv run --locked pytest tests/agent_ops/test_control_plane_worktree_concurrency.py::test_context_manager_exception_and_fd_close_for_same_process_timeout -qRepository baseline gates (CI regression checks; they are retained but are not AC evidence for this mutex primitive):
$ pnpm typecheck$ pnpm lint$ pnpm test$ pnpm build$ uv run --locked pytest .claude/skills/gemini-cli-headless-delegation/tests/test_model_routing.py -qAllowed Paths
scripts/agent-ops/worktree_bootstrap_exec.py
scripts/agent-ops/worktree_catalog.py
tests/agent_ops/test_control_plane_worktree_concurrency.py
Stop Conditions
- Allowed Paths 外の変更が必要と判明した場合
- In Scope の固定契約(キー集合・スキーマ・型定義)の変更が必要になった場合
- 新規 Issue の起票が必要と判断した場合(スコープ分割が発生する場合)
- 後続 Phase / 別スコープへの波及が判明した場合
- nested SubAgent delegation が必要になった場合
- 外部サービス利用・権限昇格・既存テスト大規模改変が必要になった場合
- 本child issueのAllowed Paths外への波及が必要と判明した場合
Required Skills
なし
Required Design References
- docs/dev/agent-skill-boundaries.md
- docs/dev/workflow.md
- https://git-scm.com/docs/git-worktree.html(`--detach --lock --reason`、porcelain identity fields)
- https://git-scm.com/docs/git-rev-parse(`--path-format=absolute --git-common-dir`)
- https://docs.python.org/3/library/fcntl.html(advisory
flocklifecycle)
Scope Delta(任意)
N/A
Activity
squne121 commentedon Aug 28, 2026
Dependency rationale: #2198 is narrowed to the lifecycle mutex/lease primitive. Remote observe/fetch/pin/verify/worktree protocol is exclusively #2197 work and requires this lease.
squne121 commentedon Aug 29, 2026
敵対的レビュー結果
確認時点では、
Closes #2198を持つPRは検索上存在せず、今回は実装差分ではなくIssue契約のレビューです。また、#2197向け前提実装であるPR #2381は、current head6a6612c40e5a21ab99041692e1ec778b49505875でopen・未マージです。まず、現在の責務分割は以前より明確です
#2198をmutex primitiveだけに狭め、remote observe/fetch/pin/verify/worktree作成を#2197へ一意に寄せた修正は妥当です。Issueコメントと#2197のreframe rationaleも、この責務分割を明示しています。以前あった「#2197と#2198が同じworktree作成を所有する」という競合は、おおむね解消されています。
ただし、責務を分けたことで、今度はmutexの寿命を誰が所有するのかが抜け落ちています。
P0-1: mutexを保持するプロセスが決まっていない
#2198は、remote観測前にlockを取得し、child/artifact processingとvalidation完了後にreleaseするprimitiveを要求しています。一方で、それらの処理自体はOut of Scopeです。#2197も「artifact validation完了まで保持してrelease」と書いていますが、artifact validation実装は明示的に#2200の所有です。さらに#2199は、bootstrap後にdedicated worktree内executorへ「re-exec」する設計です。
この契約では、次のどちらになるのか分かりません。
worktree_bootstrap_exec.pyがlockを取得し、そのまま専用executorへ本当のexecveを行うこの差は重大です。Pythonで作成したFDは原則としてexec時に継承されないため、本当のre-execを行うと、明示的に継承可能にしない限りlock FDが閉じられます。一方、
fork()ではFDが複製されます。 ([Python documentation]1)しかも現行の
skill_runtime_exec.pyは、child終了後に unauthorized change検査とstdout/artifact projection検証を行っています。bootstrap側だけがlockを持つ設計では、child終了時にlockが解放され、実際の最終validationより前に排他区間が終わります。推奨修正
re-execではなく、一つの外側controllerがFDを保持したままdedicated executorをspawn・superviseする構造へ寄せるべきです。自然な所有者は、すでにchildの前後を囲んでいる
skill_runtime_exec.py、またはそこから呼び出される専用controllerです。この設計なら、以下は不要です。
どうしても本当の
execveを維持するなら、#2198または#2199に次を明文化する必要があります。しかし、個人趣味開発の運用性を優先するなら、これは採用しない方がよいです。
P0-2:
lease、stale、mutexの意味が混ざっているこの用途にTTL付きleaseは不要です。Linuxの
flockはopen file descriptionに結び付くadvisory lockで、明示unlockするか、そのlockを参照する全FDが閉じられれば解放されます。プロセスが異常終了した場合もFDが閉じられるため、heartbeat、期限更新、stale掃除は必要ありません。 ([Python documentation]2)現在のAC4は「stale leaseをvalidとして扱わない」としていますが、これを時間ベースのstale判定として実装すると、次の問題が生じます。
ここで必要なのはleaseではなく、process-scoped lifecycle mutex guardです。
AC4は次の意味へ変えるべきです。
また、lock fileはrelease時にunlinkしてはいけません。プロセスAが古いinodeをlock中にファイルを削除・再作成すると、プロセスBが新しいinodeをlockでき、排他が二系統に分裂します。固定ファイルを残し、kernel lockだけを取得・解放するべきです。これは「ゴミファイル」ではなくmutexの固定anchorです。 ([man7.org]3)
なお、Gitの
worktree --lockはworktree metadataをprune、move、deleteから保護する管理lockであり、control-plane process全体を直列化するmutexではありません。両者は別概念として明記すべきです。 ([Git]4)P0-3: mutex keyとなる
git-common-dirを安全に取得できない現行
worktree_catalog.pyの_git_common_dir()は、shutil.which("git")と継承environmentを使って、直接git rev-parse --git-common-dirを実行しています。これはcatalog用途には現状の前提で動いていますが、排他keyのauthorityとして使うには不十分です。Git公式仕様では、
--git-common-dirはGIT_COMMON_DIRの影響を受けます。一方、--path-format=absolute --git-common-dirを使えば絶対pathとして取得できます。つまり少なくともrepository選択系GIT_*を除外したexact commandが必要です。 ([Git]5)ところが、PR #2381のclosed operation一覧には、remote URL、fetch、ref readback、worktree add/removeなどはありますが、
rev-parse --git-common-dir用operationはありません。現在のAllowed Pathsのまま実装すると、実装者は次のどれかを選ばざるを得ません。
worktree_catalog.pyへ複製するすべて不適切です。
推奨修正
PR #2381のmerge後に、#2198でexact closed operationを一つだけ追加してください。
要件は次の程度で十分です。
そのため、#2198のAllowed Pathsには最低限、以下を追加する必要があります。
逆に、primitiveだけを扱う#2198では
worktree_bootstrap_exec.pyを変更しない方が責務が明確です。また、確認時点の#2198はnative dependency上の未解決blockerが0ですが、PR #2381はまだ未マージです。少なくとも実装開始前に、#2378/PR #2381のmergeをdependencyとして明示すべきです。過去のownerコメントでも、推奨順は「builder/hardening → #2198 mutex → #2197 protocol」とされています。
P0-4: AC1・AC2がprimitive単体では証明不能
現在のAC1は、valid leaseなしにconsumerがremote protocolを開始できないことを#2198で実証するよう要求しています。しかしremote protocolは#2197の実装であり、#2198のテストはそのconsumerをまだ持ちません。
AC2はさらに問題があります。
これをprimitive側で強制するには、例えば次のようなsemantic state machineが必要になります。
しかし、このmarkerは単なる自己申告です。安全性は上がらず、失敗経路だけ増えます。またvalidationが失敗した場合、「完了を示せないからrelease不可」とすると、長寿命controller processがmutexを保持し続けます。
正しい条件は次です。
したがって、#2198が保証するのは次だけで十分です。
acquire → remote → child → artifact validation → releaseのend-to-end orderingは#2200の統合テストへ移すべきです。#2200自身がartifact validationとlock保持延長を所有すると宣言しています。P1-1:
worktree_catalog.pyをstate managerにしないworktree_catalog.pyは、現在「worktree catalogとmonotonic deadlineの共有SSOT」であり、import-safe・side effectなしを明示しています。ここにFD所有、PID、release stateなどを持つlock managerを追加すると、catalogとlifecycle resource managementが混ざります。新規の小さなmoduleに分ける方がよいです。
APIもgeneric lock libraryにせず、用途固定にします。
callerに次を渡させないでください。
fixed identityは関数内部のconstantで十分です。
P1-2: fork childからのunlockを防ぐ
PR #2381のcurrent実装はGit process supervisor作成に
os.fork()を使用しています。close-on-exec設定はexec時の継承を防ぎますが、forkではFDが複製されます。 ([Python documentation]1)これは必ずしも問題ではありません。forkしたGit supervisorが終了するまでmutexが残るのは、むしろ安全側です。
ただし、fork childが誤って
LOCK_UNを実行すると、共有open file descriptionのlockを親ごと解除できます。guardには取得時PIDを保持し、次を必須にしてください。このcheckは
assert_held()とrelease()の両方で、fcntl.flock(..., LOCK_UN)より前に行う必要があります。また、#2381のsupervisor timeout/cleanup後に新しいprocessがmutexを再取得できる統合テストを一件追加すべきです。
P1-3: deadlineをもう一種類増やさない
現行
worktree_catalog.pyにはすでにmonotonicDeadlineがあり、PR #2381にもremote protocol全体用のGitProtocolDeadlineがあります。#2198専用のtimeout classをさらに追加すると、各phaseが独立した満額timeoutを持ち、全体時間が読めなくなります。
mutex APIはclassを増やさず、外側からabsolute monotonic deadlineを受け取れば十分です。
実装は次で足ります。
busy時の外向け結果は既存契約どおり
control_plane_unavailableとし、診断用reasonをlifecycle_lock_busyに固定すると分かりやすいです。P1-4: unrelated verificationをAC証拠から外す
#2198のVerification Commandsには、focused pytestのほかに次が含まれています。
pnpm typecheckpnpm lintpnpm testpnpm buildrepository-wide gatesをCI回帰確認として実行すること自体は構いません。しかし、これらはmutexのACを一切証明しません。親#2190への過去レビューでも、Gemini model-routing testは因果関係のないfalse-green要因として指摘されています。
ローカルの実装・レビューループでは次だけで十分です。
frontendの全体gateは通常CIへ残し、Gemini model-routing testは#2198のVerification Commandsから削除するのが妥当です。
推奨する最小実装契約
Python標準ライブラリの
fcntl.flockで足ります。fasteners.InterProcessLockもUnixではfcntlを包む非再入・advisory lockなので、Linux/WSL限定の個人開発へ新しいruntime dependencyを追加する利点はほぼありません。現在のpyproject.tomlにもlock library依存はありません。 ([GitHub]6)Acceptance Criteriaの修正案
次の条件は#2198から外し、#2200の統合ACへ移してください。
推奨実装順
結論として、#2198を閉じる必要はありません。方向性は妥当なので
reframe_in_placeが適切です。 ただし現在のまま実装すると、TTL付きleaseの過剰実装か、re-execでlockを失う実装か、artifact validation前にreleaseする実装のいずれかになりやすいため、契約修正前の実装開始は非推奨です。squne121 commentedon Aug 29, 2026
root-owned entry router audit
route: invoke_impl_review_loop
reason: fresh_review_go_live_equality
squne121 commentedon Aug 29, 2026
issue-refinement-loop: approved
approve(final artifact readback verified)clearafter one controlled rewritego(baseline expected failures: AC1–AC6; regression gates passed)squne121 commentedon Aug 29, 2026
root-owned entry router audit
route: invoke_impl_review_loop
reason: fresh_review_go_live_equality
test: bind mutex acceptance selectors (#2198)
squne121 commentedon Aug 29, 2026
root-owned entry router audit
route: invoke_impl_review_loop
reason: fresh_review_go_live_equality
fix: harden lifecycle mutex fork and deadline handling (#2198)
squne121 commentedon Aug 29, 2026
root-owned entry router audit
route: invoke_impl_review_loop
reason: fresh_review_go_live_equality
squne121 commentedon Aug 29, 2026
root-owned entry router audit
route: invoke_impl_review_loop
reason: fresh_review_go_live_equality
squne121 commentedon Aug 29, 2026
root-owned entry router audit
route: invoke_impl_review_loop
reason: fresh_review_go_live_equality
feat: add control-plane lifecycle mutex (#2395)
feat(#2197): control-plane remote default-ref observe/fetch/pin/verif…