← Back to Pull requests

fix(guard): judge Cursor's cwd-less shell calls at its workspace root since Cursor hooks get no cwd and every bare push passed as a warning #242

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 26, 2026 03:20 UTC
Author

概要

Orca の Cursor(ACP)のワーカーは、upstream-guard のフックに作業ディレクトリ(cwd)を渡さない。そのため、場所を指定しない git push origin … は宛先を決められず、警告で通っていた。takt の作業ツリーで打つ git push upstream …(宛先は nrslib)も、同じく警告で通っていた。

この PR では、入力に cwd が無いとき、Cursor が渡す作業ツリーの場所を推定して判定する。推定が使えるのは、「宛先を決められない」を「宛先が決まった許可」か「宛先が決まった拒否」に置き換えるときだけ。推定が無ければ拒否になる入力は、推定の値が何であっても拒否のまま残る。あわせて、cwd が無いと絶対パスの git -C まで捨てていた不具合を直した。

設計と設計レビュー

  • 設計:~/.orca/bots/sub-marshal/audit/upstream-guard-cwd/DESIGN.md(案 A。副元帥が 2026-11-26 に決めた)

  • 設計の独立レビュー:同じ場所の DESIGN-REVIEW.md。結論は「条件つきで実装に入ってよい」。試作で 48,825 通りを回し、推定が拒否を許可に変えないことを確かめている(違反 0)。

  • レビューの条件の 5 点は、設計の 7 章に書き足し、そのとおりに実装した。

  • 1. 推定で許可したものも記録に残す(allows.log)
    2. 入力の cwd と tool_input.cwd は、絶対パスのときだけ確かな cwd にする。相対パスは推定の候補から解く(これまでは、フックのプロセスの場所の ~/.claude から解かれていた)
    3. 推定なしの判定が拒否なら、推定の拒否より先にそれを返す
    4. 例外の扱い:候補の場所の判定の例外は、その候補から拒否も許可も取らない
    5. 残る穴に、Cursor のシェルが cwd に加えて環境変数・関数・alias も持ち越すことを書く
  • 調べの結果(cwd が空の記録 18 件は、どれも Cursor のワーカー)は、同じ場所の REPORT.md。
  • 変えた判定

    場所は次の順で決める(locate)。

    1. 入力の cwd、次に tool_input.cwd(Cursor の working_directory)。絶対パスのときだけ使う
    2. どちらも無ければ推定する。候補は workspace_roots の先頭と CURSOR_PROJECT_DIR。CLAUDE_PROJECT_DIR は使わない

    判定は次のとおり(decide_located)。入力に cwd があるとき(Claude Code、Codex、Pi)は、これまでの decide をそのまま呼ぶ。

    | 推定なしの判定 | 推定の場所での判定 | 結果 |
    | --- | --- | --- |
    | 拒否 | 何でも | 拒否(そのまま。推定の一文は付けない) |
    | 許可か警告 | どれかが宛先の決まった拒否 | 拒否。理由に「推定して判定した。別の場所なら working_directory に絶対パスを」の一文を足す |
    | 警告 | すべてが許可 | 許可。allows.log に残す |
    | それ以外 | — | 推定なしの判定のまま |

    推定の場所での「宛先を決められない」は使わない。takt の作業ツリーは upstream に nrslib の語を持つので、使うと cd $D; git push origin x のような正当な push まで止まるため。

    ほかの変更:

  • git -C <絶対パス> は、cwd が無くても場所にする(_parse_git_globals の 1 行)

  • 記録の 1 行に cwd_source(payload、tool_input、estimated、none)を足した

  • upstream-guard check に --no-cwd と --workspace-root <dir> を足した。Cursor の入力を、フックと同じ locate を通して再現する。Pi の拡張は --cwd を渡し続けるので影響を受けない

  • docs/upstream-guard.md に、Cursor の扱いと、残る穴を書いた
  • 試験

    tests/upstream_guard_test.py に足したもの(入力はどれも表に書き、ネットワークにも本物の nrslib にも触れない):

  • decide_located の表(45 行)。設計の 3.1 の行に、レビューの指摘 8 の行(エイリアス、git -c remote.origin.url、-C の組み合わせ)を足した

  • cwd が空だった記録 18 件の形の固定。値は偽のものに替えた。推定なしと推定ありの期待を両方持つ

  • 性質の試験:判定の表(408 件)と上の表のコマンドに、推定の候補の 1 つと 2 つの組をすべて掛けて、推定なしの拒否が変わらないこと、警告でないものは推定の拒否のほかに変わらないことを確かめる(46,095 通り)

  • locate の表(22 行)。is_dir と realpath は差し替える

  • フックの入口:使い捨てのリポジトリ(takt の形と、その worktree)に向けた Cursor の形の入力で、origin へは許可、upstream へは拒否、記録の cwd_source を確かめる。check の入口も同じく確かめる
  • 足した試験は、直す前のコードでは落ちる(古い振る舞いを新しい名前で包んだ写しで、10 件が落ちた)。

    変異の確かめ(写しに 1 つずつ入れて試験を流し、どれも落ちることを確かめた。ソースは元のまま):

    | 変異 | 落ちた試験 |
    | --- | --- |
    | 推定の順:workspace_roots を tool_input.cwd より先に | Locate |
    | 推定の順:CURSOR_PROJECT_DIR を workspace_roots より先に | Locate |
    | 推定なしの拒否を、推定の拒否の後に返す | DecideLocatedTable、性質の試験、CursorHook |
    | 相対の cwd を確かな cwd として許す | Locate、CursorHook |
    | 推定の場所の「決められない」拒否も使う | DecideLocatedTable、18 件の試験 |
    | -C の直しを戻す | DecideLocatedTable |
    | 推定の許可を記録しない | CursorHook |
    | 候補の例外を捕まえない | DecideLocatedTable |
    | 候補の「すべて許可」を「どれか許可」にする | DecideLocatedTable |

    手元では make done と make build-hm が通った(反映はしていない)。ビルドした世代の upstream-guard に、設計レビューの Cursor の形の入力 15 件を渡し、レビューの「設計」の列と同じ結果になった。違うのは、相対パスの入力の cwd(P9)だけで、許可から警告に変わった(レビューの指摘 2 のとおり)。

    残る穴(別件)

  • 推定で許可したときに updatedInput.cwd を返し、実行の場所を判定した場所に揃える案:https://github.com/j5ik2o/dotfiles/issues/240

  • cwd の無い git のエイリアスが、警告も記録も無く通る:https://github.com/j5ik2o/dotfiles/issues/241
  • ほかに、推定の候補が無い Cursor の呼び出しで、宛先が作業ディレクトリに依存する push(git push upstream など)は、これまでどおり警告で通る。

    反映

    ~/.local/bin/upstream-guard は Home Manager が置く Nix のパッケージ(modules/common.nix)を指すので、マージの後に make apply を流すまで、全ボットのフックは古い判定のままになる。

    <!-- devin-review-badge-begin -->

    ---

    <a href="https://app.devin.ai/review/j5ik2o/dotfiles/pull/242" target="_blank"><picture><source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4" alt="Devin Review"></picture></a>
    <!-- devin-review-badge-end -->

    <!-- CURSOR_SUMMARY -->
    ---

    [!NOTE]

    Medium Risk

    Changes PreToolUse judgment for Orca agent git/gh commands; design keeps pre-estimation denies, but wrong workspace hints or Cursor shell carry-over can still mis-allow pushes.

    > Overview

    Cursor’s Orca ACP worker often invokes the upstream-guard hook without a shell cwd, so pushes like git push origin … and git push upstream … used to land as unresolved warnings instead of real allow/deny decisions.

    > The hook now locates the judging directory from absolute cwd / tool_input.cwd, else workspace_roots[0] and CURSOR_PROJECT_DIR (not CLAUDE_PROJECT_DIR). decide_located re-runs judgment at those hints: hard denies stay denies; a hint that resolves a blocked destination denies (with an estimate note); a warn that becomes allow at all hints becomes allow and is logged to allows.log with cwd_source.

    > git -C /abs/path is treated as a known location even when the payload has no cwd. upstream-guard check adds --no-cwd and --workspace-root to replay Cursor payloads; docs cover Cursor gaps (shell state carry-over, no hints).

    > Large test additions cover locate, decide_located, property checks that estimation never upgrades a deny, 18 reproduced empty-cwd records, and hook_main integration.

    > <sup>Reviewed by Cursor Bugbot for commit a7fee6cd5cdf5852eef770cc8c7fde8e53d424b0. Configure here.</sup>

    <!-- /CURSOR_SUMMARY -->

    <!-- This is an auto-generated comment: release notes by coderabbit.ai -->

    <!-- coderabbit-ai-generated:v1 -->

    Summary by CodeRabbit

    新機能
    作業ディレクトリが入力に含まれない場合、Cursor の情報などから場所を推定して判定できるようになりました。
    推定した場所に基づく許可・警告・拒否の結果が記録されるようになりました。
    手動確認時に Cursor からの入力を再現できるようになりました。
    ドキュメント
    場所の推定規則、判定結果の記録内容、Git コマンドでの確認方法を追記しました。

    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: devin-ai-integration[bot] Review duration: 6m
    All Vouch review confidence checks passed
    Residual risk 0.44 = Change Risk (0.57) × (1 − Review Confidence (0.23)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.57 · 650 lines diff across 8 files
    0.57
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 40% · Reviewer familiarity: 35%
    23%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #242"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: medium confidence: 23.4% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 57% (MEDIUM) · Higher = larger blast radius
    57%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 23.4% · Lower = rushed or shallow review
    23.4%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 43.6% · Moderate risk
    43.6%