Skip to content

調査: context-mode ctx_execute の worktree cwd 安全性を検証し quarantine 方針を固定する #826

Closed

Description

@squne121

Machine-Readable Contract

contract_schema_version: v1
issue_kind: research
parent_issue: "#813"
goal_ref: "context-mode MCP を Claude Code に段階的導入し、大出力を扱う read-only 調査補助として運用する"
change_kind: research-only

Parent Issue

#813

Current Objective

upstream context-mode #756 の cwd bug が loop-protocol の worktree 運用に影響するかを検証し、ctx_execute を許可するか quarantine 継続するかの判断材料を作る。

Background

upstream #756 では、Claude Code が worktree にいるにもかかわらず ctx_execute が main checkout 側で実行される問題が報告されている。read-only コマンドであっても、cwd が違えば rg / git log / test log 参照結果が誤る。

本 Issue の目的は「ctx_execute を許可できるか判断する」ことではなく、「許可しない理由を runtime evidence として固定する」ことである。初期判断は quarantine 継続とし、その根拠を artifact に記録する。

Outcome

current project policy で ctx_execute が明示 deny であることを構造化 parse で確認し、Bash と ctx_execute の cwd / repo root / branch / HEAD SHA の比較 probe 結果(probe 不可の場合はその記録を含む)が artifact 化される。ctx_execute / ctx_batch_execute / ctx_execute_file の execution-like tool quarantine matrix が docs に記録され、quarantine_continue 判定が固定される。

In Scope

  • current project policy で mcp__context-mode__ctx_execute が明示 deny であることの構造化 parse 確認
  • 通常 profile で ctx_execute が permission-denied になることの確認
  • 比較 probe が必要な場合は commit されない isolated local profile で固定 read-only command のみを実行
  • worktree / main checkout / nested repo での Bash vs ctx_execute の cwd 比較
  • pwd, printf '%s\n' "${CLAUDE_PROJECT_DIR:-<unset>}", git rev-parse --show-toplevel, git branch --show-current, git rev-parse HEAD, git rev-parse --is-inside-work-tree
  • upstream feat-storage-add-snapshot-schema-migration-issue-736 #756 の current state / installed context-mode version の確認
  • ctx_execute / ctx_batch_execute / ctx_execute_file の execution-like tool quarantine matrix 作成
  • artifact と docs への結果記録
  • artifact schema test の追加

Out of Scope

  • ctx_execute による build / test / git write 操作
  • ctx_execute の恒久許可
  • upstream context-mode の修正
  • ctx_batch_execute / ctx_execute_file の routine-use 許可判断(今 Issue は deny/quarantine 方針の記録のみ)

Acceptance Criteria

  • AC1: current project policy で mcp__context-mode__ctx_execute が明示 deny であることを .claude/settings.json / registered-tools.json / permission-policy.json の構造化 parse で検証している
  • AC2: 通常 profile で ctx_execute が実行されず permission-denied になることを artifact に記録している。実行できた場合は Stop Condition
  • AC3: 比較 probe を行う場合、commit されない isolated local profile で固定 read-only command のみを実行し、probe profile が commit されていないこと、deny が復元されたことを artifact に記録している
  • AC4: main checkout / linked worktree / nested repo の各 case で Bash と ctx_execute の pwd, git rev-parse --show-toplevel, git branch --show-current, git rev-parse HEAD, git rev-parse --is-inside-work-tree を比較している
  • AC5: CLAUDE_PROJECT_DIR は informational field として記録し、session cwd の正否判定には使っていない
  • AC6: upstream feat-storage-add-snapshot-schema-migration-issue-736 #756 の current state / fetched_at / installed context-mode version / local probe result を artifact に保存している
  • AC7: ctx_execute, ctx_batch_execute, ctx_execute_file の execution-like tool quarantine matrix が docs に記録されている
  • AC8: upstream feat-storage-add-snapshot-schema-migration-issue-736 #756 が open、local probe 不一致、probe 不可、または sibling execution-like tools 未評価のいずれかなら、verdict は quarantine_continue
  • AC9: docs/dev/agent-ops/context-mode-cwd-quarantine.md に build / test / git / repo grep / log summary を context-mode execution-like tools へ移さない方針が明記されている
  • AC10: artifact schema test が null / stale / unredacted path / token-like value を fail する

Verification Commands

# AC1
rg -q "ctx_execute" .claude/settings.json

# AC2
uv run python3 -c "import json; d=json.load(open('.claude/artifacts/context-mode/cwd-comparison-result.json')); assert d['project_policy']['ctx_execute'] == 'deny'"

# AC3
uv run python3 -c "import json; d=json.load(open('.claude/artifacts/context-mode/cwd-comparison-result.json')); p=d.get('probe', {}); assert p.get('profile_committed') == False"

# AC4
uv run python3 -c "import json; d=json.load(open('.claude/artifacts/context-mode/cwd-comparison-result.json')); assert all(k in d['cases'] for k in ['main_checkout','linked_worktree','nested_repo'])"

# AC5
uv run python3 -c "import json; d=json.load(open('.claude/artifacts/context-mode/cwd-comparison-result.json')); assert 'informational_only' in d and 'claude_project_dir' in d['informational_only']"

# AC6
uv run python3 -c "import json; d=json.load(open('.claude/artifacts/context-mode/cwd-comparison-result.json')); assert d.get('upstream_issue_756_state') in ('open','closed') and bool(d.get('context_mode_version')) and bool(d.get('fetched_at'))"

# AC7
rg -q "ctx_batch_execute" docs/dev/agent-ops/context-mode-cwd-quarantine.md

# AC8
uv run python3 -c "import json; d=json.load(open('.claude/artifacts/context-mode/cwd-comparison-result.json')); assert d['verdict'] in ('quarantine_continue','probe_blocked_quarantine_continue')"

# AC9
rg -q "移さない" docs/dev/agent-ops/context-mode-cwd-quarantine.md

# AC10
uv run pytest tests/context_mode/test_cwd_quarantine_artifact.py -v

Allowed Paths

  • .claude/artifacts/context-mode/**
  • docs/dev/agent-ops/context-mode-cwd-quarantine.md
  • tests/context_mode/test_cwd_quarantine_artifact.py

Stop Conditions

  • .claude/settings.json を commit 上で ctx_execute allow に変更する必要が出た場合
  • standalone MCP mode / SQLite surrogate だけで Claude Code worktree cwd 安全性を結論づけようとした場合
  • ctx_batch_execute / ctx_execute_file の実行相当性を評価せずに ctx_execute quarantine を解除しようとした場合
  • CLAUDE_PROJECT_DIR を session cwd と同一視する設計になった場合
  • artifact に null, pending, unknown を含んだまま status: pass になる場合
  • upstream feat-storage-add-snapshot-schema-migration-issue-736 #756 が open のまま build/test/git/read-only repo command を context-mode execution-like tools に解放しようとした場合

Delivery Rule

  • research-only
  • PR は docs / artifact のみ
  • child 1 / child 2 完了後に着手する

Handoff Contract

  • Current Objective
  • Bounded Current Context
  • Open Questions
  • Next Action
  • Artifact Refs

Activity

  1. added
    state/queued
    on Jun 12, 2026
  2. squne121 commented on Jun 13, 2026

    @squne121
    OwnerAuthor

    判定: REQUEST_CHANGES

    #826 の方向性は妥当ですが、現契約のまま実装させると 「ctx_execute は危ないので隔離する」という結論を、危ない ctx_execute をどう安全に検証したか不明なまま artifact 化する 形になります。現時点では merge-ready な Issue 契約ではありません。

    #826 は upstream #756 の cwd bug を検証し、Bash と ctx_execute の cwd / repo root / branch / HEAD SHA を比較する research-only Issueです。初期判断は quarantine 継続です。 親 #813 でも ctx_execute は child 3 完了まで明示 deny、rg / git log / test log 要約も例外扱いしない方針になっています。

    重大ブロッカー

    1. ctx_execute を deny しているのに、#826 は ctx_execute 実行比較を要求している

    現在の .claude/settings.json は mcp__context-mode__ctx_execute と mcp__context-mode__ctx_fetch_and_index を明示 deny しています。 それ自体は正しいですが、#826 の AC1 は Bash と ctx_execute の比較を要求しています。

    この矛盾を解消するには、Issue に次の二段階を明記してください。

    1. 通常 profile では ctx_execute が permission deny されることを artifact 化する。
    2. 比較が必要な場合だけ、commit されない一時 local profile / isolated throwaway profile で、固定済み read-only probe だけを実行し、最後に deny が復元されたことを artifact 化する。

    .claude/settings.json を PR 上で allow に変更して検証する実装は不可です。

    2. ctx_batch_execute / ctx_execute_file が抜けている

    context-mode は 11 MCP tools を登録し、その中に ctx_batch_execute, ctx_execute, ctx_execute_file, ctx_index, ctx_search, ctx_fetch_and_index があります。([GitHub]1) current artifact でも 11 tool 登録が確認されています。

    問題は、明示 deny されているのが ctx_execute と ctx_fetch_and_index だけで、ctx_batch_execute / ctx_execute_file は settings 上の明示 deny に入っていないことです。 context-mode README 上、ctx_batch_execute は複数コマンド実行、ctx_execute_file は sandbox 内でファイル処理を行う tool と説明されています。([GitHub]1)

    #826 は ctx_execute 単体ではなく、少なくとも次の quarantine matrix を要求すべきです。

    execution_like_tools:
      ctx_execute:
        policy: deny
        cwd_probe_required: yes
      ctx_batch_execute:
        policy: deny_or_not_routine_use
        cwd_probe_required: yes_if_shell_execution_supported
      ctx_execute_file:
        policy: deny_or_not_routine_use
        cwd_probe_required: yes_if_repo_relative_file_resolution_supported

    ctx_batch_execute / ctx_execute_file を別 Issue に逃がすなら、#826 の Stop Conditions に「execution-like sibling tools の扱いが未定義なら quarantine 継続」と書くべきです。

    3. 既存 artifact に「実験用 profile / project scope / main 非影響」の矛盾がある

    PR #856 は context-mode を project-scoped / team-shared / commit-tracked setting として導入した、と説明しています。 実際に .claude/settings.json では enabledPlugins.context-mode@context-mode: true が入っています。

    一方、profile-isolation.json は「local scope のみ」「main branch の .claude/settings.json と hooks は変更なし」と記録しています。 これは current repo の状態と整合しません。#826 はこの artifact を前提にしてはいけません。

    修正: #826 の artifact schema に effective_scope を入れ、少なくとも次を明示してください。

    effective_scope:
      settings_file: ".claude/settings.json"
      plugin_enabled_project_shared: true
      deny_entries_committed: true
      probe_profile_committed: false

    4. Claude Code runtime の証跡が弱い

    #826 が扱う upstream #756 は Claude Code + worktree の ctx_execute cwd bug です。upstream #756 は open で、ctx_execute が worktree ではなく main repo / MCP server process cwd で動く観測が報告されています。([GitHub]2)

    ところが既存 ctx-doctor-result.json には Claude Code: not installed — using standalone MCP mode とあります。 これは #824 の導入確認には一定の意味がありますが、#826 の Claude Code worktree cwd 検証の証跡にはなりません。

    #826 は Claude Code session evidence を必須化してください。例:

    runtime:
      client: "Claude Code"
      evidence:
        claude_session_id_present: true
        claude_project_dir_present: true
        mcp_tool_call_source: "Claude Code MCP tool"
      invalid_if:
        - "standalone MCP mode"
        - "SQLite surrogate only"
        - "context-mode CLI direct invocation only"

    5. CLAUDE_PROJECT_DIR の意味を誤解しやすい

    Claude Code docs は、stdio MCP server に CLAUDE_PROJECT_DIR を渡すが、これは「working directory に依存せず project-relative path を解決するための project root」だと説明しています。([Claude API Docs]3) したがって、CLAUDE_PROJECT_DIR == worktree cwd を無条件に期待値にすると誤判定になります。

    #826 の比較対象はこう分けるべきです。

    expected:
      bash_pwd: session cwd
      bash_git_toplevel: active checkout/worktree root
      ctx_execute_pwd: should match session cwd, or quarantine
      ctx_execute_git_toplevel: should match active checkout/worktree root, or quarantine
    informational_only:
      claude_project_dir: stable project root / MCP env value

    env | rg '^CLAUDE_PROJECT_DIR=' も避けた方がよいです。settings では Bash(env) / Bash(printenv) が deny されており、環境変数 dump は secret surface でもあります。 固定値だけを出すなら printf '%s\n' "${CLAUDE_PROJECT_DIR:-<unset>}" にしてください。

    6. artifact の検証が test -f だけで弱い

    #826 の Verification Commands は artifact と doc の存在確認だけです。 これでは、cwd が一致したのか、不一致だったのか、ctx_execute が deny されただけなのか、artifact が stale なのかを判定できません。

    Allowed Paths に以下を追加すべきです。

    - `tests/context_mode/test_cwd_quarantine_artifact.py`

    最低限の検証はこうです。

    uv run pytest tests/context_mode/test_cwd_quarantine_artifact.py -v

    検証内容:

    required_artifact_fields:
      schema: context_mode_cwd_quarantine_v1
      issue: "#826"
      generated_at: non_empty
      context_mode_version: non_null
      upstream_issue_756_state: "open|closed"
      project_policy.ctx_execute: "deny"
      cases:
        - main_checkout
        - linked_worktree
        - nested_repo
      verdict: "quarantine_continue|probe_blocked_quarantine_continue"
    forbidden:
      - raw home path
      - token-like value
      - env dump
      - status pass with null comparison values

    7. #825 完了を過信しない方がよい

    #825 / PR #866 は closed ですが、current deny-negative-test.json には context_mode_version: null, head_sha: pending, direct_read coverage の null, mutation detail の null, FTS cleanup の null が残っています。 さらに conftest.py は artifact 生成失敗を握り潰します。

    これは #826 の直接ブロッカーではありませんが、親 #813 の「child 2 完了済みだから safety gate 済み」とは扱わない方がよいです。#826 の docs には「#825 artifact に未確定/null フィールドが残る限り、ctx_index/search routine-use 判断にも依存しない」と明記するのが安全です。

    #826 に入れるべき修正案

    Acceptance Criteria 差し替え案

    ## Acceptance Criteria
    
    - [ ] AC1: current project policy で `mcp__context-mode__ctx_execute` が明示 deny であることを `.claude/settings.json` / `registered-tools.json` / `permission-policy.json` の構造化 parse で検証している
    - [ ] AC2: 通常 profile で `ctx_execute` が実行されず permission-denied になることを artifact に記録している。実行できた場合は Stop Condition
    - [ ] AC3: 比較 probe を行う場合、commit されない isolated local profile で固定 read-only command のみを実行し、probe profile が commit されていないこと、deny が復元されたことを artifact に記録している
    - [ ] AC4: main checkout / linked worktree / nested repo の各 case で Bash と `ctx_execute` の `pwd`, `git rev-parse --show-toplevel`, `git branch --show-current`, `git rev-parse HEAD`, `git rev-parse --is-inside-work-tree` を比較している
    - [ ] AC5: `CLAUDE_PROJECT_DIR` は informational field として記録し、session cwd の正否判定には使っていない
    - [ ] AC6: upstream #756 の current state / fetched_at / installed context-mode version / local probe result を artifact に保存している
    - [ ] AC7: `ctx_execute`, `ctx_batch_execute`, `ctx_execute_file` の execution-like tool quarantine matrix が docs に記録されている
    - [ ] AC8: upstream #756 が open、local probe 不一致、probe 不可、または sibling execution-like tools 未評価のいずれかなら、verdict は `quarantine_continue`
    - [ ] AC9: `docs/dev/agent-ops/context-mode-cwd-quarantine.md` に build / test / git / repo grep / log summary を context-mode execution-like tools へ移さない方針が明記されている
    - [ ] AC10: artifact schema test が null / stale / unredacted path / token-like value を fail する

    Allowed Paths 修正案

    ## Allowed Paths
    
    - `.claude/artifacts/context-mode/**`
    - `docs/dev/agent-ops/context-mode-cwd-quarantine.md`
    - `tests/context_mode/test_cwd_quarantine_artifact.py`

    Stop Conditions 追加案

    ## Stop Conditions
    
    - `.claude/settings.json` を commit 上で `ctx_execute` allow に変更する必要が出た場合
    - standalone MCP mode / SQLite surrogate だけで Claude Code worktree cwd 安全性を結論づけようとした場合
    - `ctx_batch_execute` / `ctx_execute_file` の実行相当性を評価せずに `ctx_execute` quarantine を解除しようとした場合
    - `CLAUDE_PROJECT_DIR` を session cwd と同一視する設計になった場合
    - artifact に `null`, `pending`, `unknown` を含んだまま `status: pass` になる場合
    - upstream #756 が open のまま build/test/git/read-only repo command を context-mode execution-like tools に解放しようとした場合

    実装時の推奨 probe command

    env dump は使わず、固定 read-only だけにする。

    pwd
    printf 'CLAUDE_PROJECT_DIR=%s\n' "${CLAUDE_PROJECT_DIR:-<unset>}"
    git rev-parse --show-toplevel
    git rev-parse --is-inside-work-tree
    git branch --show-current
    git rev-parse HEAD
    git status --short --branch

    Git worktree は main worktree と linked worktree を分けて保持する仕組みなので、branch / HEAD / toplevel の比較は妥当です。([Git]4) git rev-parse --show-toplevel は working tree の top-level directory を返すため、cwd drift 検出に適しています。(Git)

    最終方針

    #826 は 「ctx_execute を許可できるか判断する Issue」ではなく、「許可しない理由を runtime evidence として固定する Issue」 に寄せるべきです。

    現時点の推奨結論はこれです。

    CONTEXT_MODE_CWD_VERDICT:
      issue: "#826"
      verdict: "quarantine_continue"
      reason:
        - "upstream #756 is open"
        - "ctx_execute is explicitly denied in project settings"
        - "previous artifacts do not prove Claude Code worktree runtime behavior"
        - "execution-like sibling tools are not fully covered"
      allowed_routine_use:
        - "none for ctx_execute / ctx_batch_execute / ctx_execute_file"
      build_test_git_policy:
        - "continue using Bash only"
  3. squne121 commented on Jun 13, 2026

    @squne121
    OwnerAuthor

    Refinement Loop: Human Escalation Required

    The loop has terminated and requires human review. Cause: human judgment required

    Blockers

    "PLANNER_FAIL_CLOSED: missing_required_section — research テンプレートの必須セクション `## Handoff Contract` が #826 本文に存在しない。preflight が fail-closed で停止。"
    
    "anchor comment (issuecomment-4698582760) は REQUEST_CHANGES 判定で AC1-AC10 差し替え / Allowed Paths 修正 / Stop Conditions 追加を要求しているが、preflight blocked のため refinement loop が未開始。"
    
  4. squne121 commented on Jun 13, 2026

    @squne121
    OwnerAuthor

    Refinement Loop: Approved

    The issue has been approved and is ready for implementation.

  5. added 4 commits that reference this issue on Jun 13, 2026
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

    state/queued

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions