Repository navigation
feat: _resolve_origin_tx() をreason-code分解し post-merge-cleanup adapterのenvを明示override化する - #2720
Merged
Merged
Conversation
…のenvを明示override化する (#2719) _resolve_origin_tx() が unbound を返す原因を7種類のreason-code (origin_session_missing / origin_run_not_found / origin_run_ended / origin_run_kind_mismatch / origin_task_unattached / origin_binding_session_mismatch / origin_ambiguous) に分解し、 apply_workflow_signal 実行時に events テーブルへ診断用イベントとして 永続化する(公開contractの unbound disposition 自体は変更せず、既存の non-mutating regression契約は保持)。 post-merge-cleanup adapter (task_context_workflow_signal.py) の _run() を ctl_client.py と同様に env=child_env で CLAUDE_CODE_SESSION_ID を明示的に 上書きするよう改修し、--origin-session-id フラグを追加。 production-shaped regression test を追加し、task_contextctl.py を実際に subprocess経由で起動する経路で reason-code永続化とenv override伝播を検証する。 Co-Authored-By: Claude Sonnet 5 <[email protected]>
4 tasks
Contributor
Preview URLPreview removed (PR #2720 closed). |
squne121
marked this pull request as ready for review
September 21, 2026 15:24
This was referenced Sep 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Issue #2719 の実装です。
_resolve_origin_tx()(scripts/task-context/task_context_workflow_signals.py)がunboundを返す際の内部原因を7種類の reason-code に分解し、post-merge-cleanupadapter(
.claude/skills/post-merge-cleanup/scripts/task_context_workflow_signal.py)の origin sessionenv 伝播を hook 経路(
ctl_client.py)と同様の明示 override 方式へ是正しました。_resolve_origin_tx()を、単一 6-predicate ANDed SQL クエリから「フィルタなし LEFT JOIN で生 candidate を取得 → predicate を個別評価」する構造へ改修しました(
_classify_origin_candidates()として分離)。
origin_session_missing/origin_run_not_found/origin_run_ended/origin_run_kind_mismatch/origin_task_unattached/origin_binding_session_mismatch/origin_ambiguousの7種類を区別します(AC1)。apply_workflow_signal()/begin_cleanup_lifecycle()はどちらも従来通り
{"disposition": "deferred", "reason_code": "unbound"}を返します(Issue 実装: workflow trusted signalsとIssue/PR Task equivalenceをTask Contextへ統合する #2565 契約、および 実装: apply_workflow_signal()のimplementation_pr_observed分岐でcross-Task accepted-fact identity判定をlocal Activity state判定より先に評価する #2690/fix: apply_workflow_signal() の cross-Task accepted-fact identity 判定を local Activity state 判定より先に評価する #2692 fix_delta の non-mutating regression テストとの後方互換性を維持)。
具体的な reason-code は診断専用の内部値として扱い、
apply_workflow_signal()実行時にeventsテーブルへworkflow:origin_resolution_failedイベントとして永続化しました(AC2)。ExecutionRun candidate が一件も見つからない
origin_session_missing/origin_run_not_foundは診断対象となる Task/ExecutionRun 自体が存在しないため、意図的に永続化対象から除外しています
(既存の non-mutating 契約テストがまさにこの2ケースを検証しているため)。
origin_ambiguous(>1 candidate が全 predicate を満たす)はux_execution_runs_open_managed_sessionpartial unique index により現行スキーマの実 DB 書き込み経路からは到達不能です(分解前のコードも同じ
len(rows) != 1相当のガードを持っていました)。将来のスキーマ変更や raw SQL bypass に対するdefense-in-depth として、predicate 評価ロジックを
_classify_origin_candidates()という純粋関数に分離し、synthetic candidate リストを直接渡すテストで区別可能性を検証しました。
.claude/skills/post-merge-cleanup/scripts/task_context_workflow_signal.pyの_run()を、.claude/hooks/task_context/ctl_client.pyのcall()と同様にchild_env = dict(os.environ)→child_env["CLAUDE_CODE_SESSION_ID"] = origin_session_id→subprocess.run(..., env=child_env)という明示 override 方式に改修しました(AC3)。従来は
env=を渡さず親プロセスの環境を無条件に継承していたため、
--origin-session-idフラグを新規追加し、指定時はそれを優先、未指定時はこの adapter 自身の
CLAUDE_CODE_SESSION_IDにフォールバックします。task_contextctl.pyを実際に subprocess 経由で起動する production-shaped regression test をtests/task-context/へ新規追加しました(AC4)。mock-only ではなく、(1) reason-code のeventsテーブル永続化を実 SQLite ファイルに対する SELECT で確認するテスト、(2) 2段subprocess チェーン(テスト → adapter subprocess → adapter 自身の
task_contextctl.pysubprocess)を通じて、ambient env と異なる
--origin-session-idoverride が実際にgrandchild プロセスへ到達することを確認するテスト、の2本を実装しました
(PR feat: owner_reaction_decision.py の selected 出力を not_planned heavy mutation gate へ限定接続する #2697 で "unit/fixture test は green だが production path は実際には未接続" だった
教訓を踏まえています)。
Checks
rg -c -e origin_session_missing -e origin_run_not_found -e origin_run_ended -e origin_run_kind_mismatch -e origin_task_unattached -e origin_binding_session_mismatch -e origin_ambiguous scripts/task-context/task_context_workflow_signals.py(AC1 VC。11)uv run --locked python3 -m pytest tests/task-context/ -q(AC2 VC。474 passed, 1 warning)rg -n "env=child_env" .claude/skills/post-merge-cleanup/scripts/task_context_workflow_signal.py(AC3 VC。該当行あり)rg -l "subprocess.run" tests/task-context/(AC4 VC。新規test_origin_reason_code_subprocess.pyを含む8ファイルにヒット)pnpm typecheck/pnpm lint/pnpm test(108 files / 1939 tests passed)/pnpm builduv run pytest .claude/skills/gemini-cli-headless-delegation/tests/test_model_routing.py -q(24 passed)uv run ruff check scripts/task-context/task_context_workflow_signals.py .claude/skills/post-merge-cleanup/scripts/task_context_workflow_signal.py(All checks passed。tests/task-context/はリポジトリの ruff CI 対象パス外のため対象外)Schema Change Applicability
task_context_schema.pyの DDL は変更していません(新規カラム・新規テーブルなし)。events.metadata_jsonは既存の汎用 JSON カラムであり、その中に格納するreason_code/signal_kind/source/countキーはいずれも既存のALLOWED_EVENT_METADATA_KEYSallowlist に既に含まれているキーです(新規キー追加なし)。task-contextctlの wire contract(stdin/stdout envelope の形)も変更していません。post-merge-cleanup adapter に追加した
--origin-session-idは既存呼び出しに対して完全に後方互換な optional flag です。
Schema Consumer Inventory
N/A
reason: schema_change ではないため、既存 consumer の before/after 差分は発生しません。
Safety Claim Matrix
apply_workflow_signal()/begin_cleanup_lifecycle()の公開 disposition はunbound分解後も変化しない(Issue #2565 契約維持)tests/task-context/test_signal_workflow_contract.py::test_given_unbound_origin_when_fact_applied_then_it_is_deferred_before_dedupe_or_claim_mutationほか既存4テストが分解後も無修正で PASSorigin_session_missing/origin_run_not_foundの unbound resolution は事前の non-mutating 契約(events/task_ref_claims/projection_outbox行数不変)を維持するtests/task-context/test_signal_origin_first_precedence.py、新規test_origin_resolution_reason_codes.py::test_given_origin_run_not_found_when_signal_applied_then_no_event_is_persistedorigin_run_ended/origin_run_kind_mismatch/origin_task_unattached/origin_binding_session_mismatch/origin_ambiguous)はeventsテーブルへ append-only で永続化され、SELECT で読み取れるtest_origin_resolution_reason_codes.py::test_given_origin_run_ended_when_signal_applied_then_specific_reason_code_is_persisted_and_readable_via_select、実 subprocess 版test_origin_reason_code_subprocess.py::test_given_ended_run_origin_when_signal_apply_runs_via_subprocess_then_reason_code_is_persistedCLAUDE_CODE_SESSION_IDoverride は ambient env の値をリークさせず、grandchildtask_contextctl.pyプロセスへ確実に到達するtest_post_merge_workflow_signal_adapter.py::test_given_explicit_origin_session_id_when_run_invokes_ctl_then_child_env_overrides_claude_code_session_id、実2段 subprocess 版test_origin_reason_code_subprocess.py::test_given_mismatched_ambient_env_when_adapter_subprocess_runs_then_it_forwards_the_explicit_overrideorigin_ambiguousは現行スキーマの unique index により実 DB 書き込み経路から到達不能である(意図的な defense-in-depth)test_origin_resolution_reason_codes.py::test_given_two_fully_qualifying_candidates_when_classified_then_reason_is_origin_ambiguous(コメントで到達不能性の根拠を明記)Notes
decision: immediate(reason: Task Context DB への実際の読み書きを伴う挙動変更であるため)ですが、
## Acceptance Criteria内に<!-- runtime-verification: true -->メタタグは付与されていません(
docs/dev/runtime-verification-policy.mdの整合ルール上は本来 blocker ですが、導入: AIエージェント運用をminimal harnessへ縮退し、過剰なcontrol-planeを廃止する #1860 Owner Decision により issue-contract-review の
status は着手可否の gate にせず、live Issue 本文と実テストを正本として扱っています)。
本 Issue の動作検証対象(SQLite への実読み書き、subprocess 経由の実プロセス起動)は
外部サービス依存を持たないため SKIP 経路は不要であり、
test_origin_reason_code_subprocess.pyが実ファイルベース SQLite DB + 実
subprocess.runチェーンによる直接証跡です(Allowed Paths に
artifacts/が含まれないため、証跡は本 PR 本文の Checksセクションへ pytest 実行結果として記載しています)。
scripts/task-context/task_context_workflow_signals.py、.claude/skills/post-merge-cleanup/scripts/task_context_workflow_signal.py、tests/task-context/の3パスのみを変更しています(task_context_schema.pyはIssue が許可する「新規カラム追加または既存
metadata_json列を使う」の後者を選んだため未変更です)。
実際には未接続だった)を踏まえ、
test_origin_reason_code_subprocess.pyはmock を一切使わず実
task_contextctl.pysubprocess・実 SQLite ファイルに対して検証しています。