Skip to content

実装: _resolve_origin_tx() のunbound判定をreason-code分解しpost-merge-cleanup adapterのorigin session envを明示override化する #2719

Closed

Description

@squne121

Parent Issue

なし(単独改善)

Machine-Readable Contract

contract_schema_version: v1
issue_kind: implementation
parent_issue: "none"
goal_ref: "_resolve_origin_tx() の unbound判定にreason-code分解を導入し、post-merge-cleanup adapterのorigin session transportをhook経路と同様の明示的override方式に是正する"
change_kind: code

Parent Goal Ref

Current Validated Scope

  • scripts/task-context/task_context_workflow_signals.py の _resolve_origin_tx() を reason-code 分解実装に改修
  • scripts/task-context/task_context_schema.py の events テーブルまたは event payload へ reason-code 永続化経路を追加
  • .claude/skills/post-merge-cleanup/scripts/task_context_workflow_signal.py の子プロセス起動を明示的 env override 方式に是正
  • 上記に対応する production-shaped regression test の追加

Remaining Parent Gaps

  • なし

Outcome

_resolve_origin_tx()(scripts/task-context/task_context_workflow_signals.py:191-206)が 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)のいずれかとして区別され、signal apply処理がeventsテーブルへ記録するeventの一部として永続化される。あわせて.claude/skills/post-merge-cleanup/scripts/task_context_workflow_signal.pyが.claude/hooks/task_context/ctl_client.pyと同様に、呼び出し元から渡されたorigin session_idを子プロセスのCLAUDE_CODE_SESSION_IDへsubprocess.run(..., env=child_env, ...)で明示的に上書きするように改修される。production-shaped regression testが追加され、上記すべてが実際にtask_contextctl.pyをsubprocess経由で起動する経路で検証可能な状態になる。

Runtime Verification Applicability

  • decision: immediate
  • reason: Task Context DBへの実際の読み書きを伴う挙動変更であるため、docs/dev/runtime-verification-policy.mdのSKIP規約に従う。

Background

#2701(研究Issue、CLOSE予定)の調査により、以下2点の実データ調査結果が判明した。

  1. _resolve_origin_tx()(scripts/task-context/task_context_workflow_signals.py:191-206)は origin session を unbound と判定する際、以下6つの異なる失敗理由(0件/ended済み/run_kind不一致/binding session不一致/task未attach/複数candidate)を単一SQLクエリで評価し、全て単一の unbound に握り潰している。加えて events テーブル(scripts/task-context/task_context_schema.py:298-307)にも resolution の入力/出力(比較対象のsession_id値、どのpredicateで落ちたか)を永続化する仕組みが存在しないため、post-merge-cleanup実行時にorigin unbound症状が発生した際の事後調査が著しく困難だった。
  2. 同じ調査で、.claude/skills/post-merge-cleanup/scripts/task_context_workflow_signal.py(post-merge-cleanup adapter、_run() 内 subprocess.run 呼び出し)が子プロセス task_contextctl.py 起動時に subprocess.run() へ env= を渡さず親プロセスの環境を無条件に継承しており、origin_session_id 用の明示的なパラメータも持たないことが判明した。対照的に .claude/hooks/task_context/ctl_client.py(hook経路、call() 内)はhook JSONのauthoritative session_idを明示的に child_env["CLAUDE_CODE_SESSION_ID"] へ上書きしてから子プロセスを起動する。この非対称性は、post-merge-cleanup adapter自身のプロセス環境にある CLAUDE_CODE_SESSION_ID がstale/不正確な場合にorigin provenanceの信頼性を損なう構造的リスクである。

Source/Related

In Scope

  • _resolve_origin_tx()(scripts/task-context/task_context_workflow_signals.py)を改修し、既存の単一SQLクエリを分解し、フィルタなしLEFT JOINで生candidateを取得してから各predicateを順に個別評価する実装へ変更する。unboundの内部原因を以下のreason-codeで区別して返す:
    • origin_session_missing(origin_session_idがNone/空)
    • origin_run_not_found(er.claude_session_id = originに一致するrunが0件)
    • origin_run_ended(一致するrunはあるがended_at IS NOT NULL)
    • origin_run_kind_mismatch(run_kindがnative_operator/claude_gpt以外)
    • origin_task_unattached(er.task_id IS NULL)
    • origin_binding_session_mismatch(tb.current_claude_session_id != origin)
    • origin_ambiguous(上記全条件を満たす行が2件以上)
  • 上記reason-codeを、signal apply処理がeventsテーブルへ記録するeventの一部として永続化する(既存のeventsテーブルschemaに新規カラムを追加、またはevent payload内のmetadata_json列に含める。既存read/writeパスとの後方互換性を保つこと)。
  • .claude/skills/post-merge-cleanup/scripts/task_context_workflow_signal.pyを改修し、呼び出し元プロセスから受け取ったorigin session_id(既存の入力経路。無ければ親プロセスのCLAUDE_CODE_SESSION_IDをfallbackとして使うが、明示的に子プロセスのenvへ設定するロジックを追加する)を、.claude/hooks/task_context/ctl_client.pyと同様にsubprocess.run(..., env=child_env, ...)で明示的に上書きするように是正する。
  • production-shaped regression test: mock-onlyではなく、実際にtask_contextctl.pyをsubprocess経由で起動し、上記各reason-codeが正しく返る/永続化されることを検証するテストをtests/task-context/配下に追加する(PR #2697で「unit/fixture testがgreenでもproduction pathが未接続」という問題が過去に発覚した教訓を踏まえる)。

Out of Scope

Acceptance Criteria

  • AC1: _resolve_origin_tx()が、既存の単一SQLクエリではなくフィルタなしLEFT JOINによる生candidate取得+predicateの個別評価という構造に改修され、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)それぞれについて、対応する入力データを用意した場合に正しいreason-codeが返ることがテストで検証されている。
  • AC2: AC1のreason-codeが、signal apply実行後のeventsテーブル(またはevent payload)に永続化されており、実際にSQLiteへ読み書きしてreason-code値をSELECTで確認できるテストが存在する。
  • AC3: .claude/skills/post-merge-cleanup/scripts/task_context_workflow_signal.pyの_run()内subprocess.run呼び出しがenv=引数を渡すように改修されており、渡されたorigin session_idが子プロセスのCLAUDE_CODE_SESSION_IDとして明示的に設定されることがテストで検証されている。
  • AC4: AC1〜AC3を検証するproduction-shaped regression test(task_contextctl.pyを実際にsubprocess経由で起動する形式)がtests/task-context/配下に新規追加され、既存テストを破壊せず全件PASSする。

Verification Commands

# AC1
$ 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
# AC2
$ uv run --locked python3 -m pytest tests/task-context/ -q
# AC3
$ rg -n "env=child_env" .claude/skills/post-merge-cleanup/scripts/task_context_workflow_signal.py
# AC4
$ rg -l "subprocess.run" tests/task-context/

Allowed Paths

  • scripts/task-context/task_context_workflow_signals.py
  • scripts/task-context/task_context_schema.py
  • .claude/skills/post-merge-cleanup/scripts/task_context_workflow_signal.py
  • tests/task-context/

Stop Conditions

  • Allowed Paths 外の変更が必要と判明した場合
  • In Scope の固定契約(キー集合・スキーマ・型定義)の変更が必要になった場合
  • 新規 Issue の起票が必要と判断した場合(スコープ分割が発生する場合)
  • 後続 Phase / 別スコープへの波及が判明した場合
  • nested SubAgent delegation が必要になった場合
  • 外部サービス利用・権限昇格・既存テスト大規模改変が必要になった場合
  • Task Context DBへの本番data破壊的操作(migration miss等でexisting行を破損する変更)を行う必要が生じた場合

Required Skills

なし

Required Design References

  • docs/dev/runtime-verification-policy.md

Scope Delta(任意)

N/A

Notes for Reviewer

Scope Collision (C1) preflight 検出: related_issue=#2701, #2680, #2569, #1390, #420, #205; overlapping_paths=.claude/skills/post-merge-cleanup/scripts/task_context_workflow_signal.py, scripts/task-context/task_context_schema.py, scripts/task-context/task_context_workflow_signals.py, tests/task-context。

  • #2701: 本Issueの直接のsource研究Issue(CLOSE予定)。同じファイルへのAllowed Paths重複は当然の関係であり、非衝突(本Issueが#2701のfollow-upとして実装を引き継ぐため)。
  • #2569: Herdr cold restart時のTask Binding復元が主目的で、scripts/task-context配下を広く含むが、本Issueのreason-code分解やadapter env override是正とは異なる変更意図。同時オープンの場合はマージ順序に注意(scripts/task-context/task_context_workflow_signals.pyのテキスト差分が競合する可能性があるため、後発側がrebaseする)。
  • #2680 / #1390 / #420 / #205: tests/task-context/ や scripts/ の broad path 重複のみで、具体的な変更対象ファイルは異なる(false positive寄り)。

Activity

  1. added
    enhancement
    phase/implementation
    triage-required
    agent/implementer
    on Sep 21, 2026
  2. squne121 commented on Sep 21, 2026

    @squne121
    OwnerAuthor

    implement-issue: 実装完了 (2026-09-21T15:10:56Z)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent/implementerenhancementphase/implementationtriage-required

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions