✨ feat(kavach): a hosted session's plain clone can be its own checkout

Merged
AT
19d ago
+527-15

Slice: #98

kavach gains worktree.ownCheckout. A cloud session works in a plain clone with no linked worktree, so the worktree guard refused every write there (#98). With this setting that clone counts as the session's own. Alok chose this option (A) on 2026-11-15, and chose to keep the guard files refused inside the clone.

What the setting does

  • The gate. A plain (unlinked) checkout passes the ownership gate in place of a Claude-managed worktree only when:

    • the hook's own environment holds one of the listed values for every variable in env (required);
    • the checkout is the one CLAUDE_PROJECT_DIR names, so cd into another clone gains nothing.

    Linked worktrees are judged as before: an unmanaged one stays refused even with the setting.

  • After the gate, every rule still applies: targets confined to the clone, trusted commands, reviewed CLIs, the push lock.

  • Fences. A worktree session finds these outside its worktree, so they stay refused inside an owned clone:

    • the clone's git folder (a write there could plant a hook);
    • the guard's settings file (KAVACH_CONFIG) and kavach's own package folder;
    • each path in the new protectedPaths (for veda: .claude/hooks/, .claude/settings.json, .claude/settings.local.json, .claude/kavach.json, .claude/sarathi.json, node_modules/).

    A target that holds a fence is refused too, so rm -rf .claude and mv .claude .claude-off are refused. Without the hook script, the hook would fail open.

    Paths compare by file identity as well as by name. On this Mac a Write to .GIT/hooks/pre-commit got through a plain path comparison, so a case spelling or a symlink now reaches nothing.

  • Known gap: this stops slips, not intent. Targets are judged from the command line. A command whose writes the guard counts as the whole checkout still reaches fenced paths: git checkout HEAD -- <path>, git restore, git apply of a patch under .tmp/, pnpm install. A session set on changing its guard files in a cloud clone can still do it. The README says so, and a test records each command, so a fix flips them knowingly. Whether that is acceptable before veda turns the setting on is Alok's call (#98).

  • Off by default. ownCheckout: null is refused rather than read as absent.

Evidence

  • Local pnpm check. Every package's check passes:

    • kavach: 648 Python tests OK, 753 node tests pass, coverage 99.91% lines and 97.70% branches (kavach's check, re-run after the last change);
    • the other packages' checks, pnpm lint and pnpm guards pass.

    Four scripts/version-rule.test.mjs tests fail on this machine only, as on #95: the shell's tag.gpgSign makes a lightweight git tag ask for a message ("fatal: no tag message?"). CI runs the full check.

  • New tests:

    • test_config.py: test_own_checkout;
    • test_worktree_guard.py: OwnCheckoutTest, 10 tests, covering owned writes and commits, the trusted script and reviewed CLI after the gate, the git folder, guard files and settings file, targets that hold a fence, clone_fences and in_fence themselves, targets outside, env mismatch, the project-dir tie, and linked worktrees.
  • Break-the-check runs: each was made to fail open in turn, and the new tests failed:

    • drop the git-folder fence;
    • ignore CLAUDE_PROJECT_DIR;
    • drop the case-insensitive comparison;
    • drop the "holds a fence" check (rm -rf .claude and mv .claude … were allowed before it).
  • Not exercised in a cloud session. Two things are still unverified there:

    • that the hook sees CLAUDE_CODE_REMOTE=true;
    • that CLAUDE_PROJECT_DIR equals the clone.

    The cloud probe on #98 ran before this change, and its env output was not captured.

What remains (not here)

  1. Release kavach and sarathi, bump the pin, and install in the main checkout.

  2. Add to veda's .claude/kavach.json, after the release (an older kavach refuses every call on the unknown key):

    "ownCheckout": {
      "env": { "CLAUDE_CODE_REMOTE": ["true"] },
      "reason": "a Claude cloud session clones the repository for itself, with no worktree",
      "protectedPaths": [".claude/hooks/", ".claude/settings.json", ".claude/settings.local.json", ".claude/kavach.json", ".claude/sarathi.json", "node_modules/"]
    }
    
  3. Re-run the cloud probe, including a commit and a push.

  4. Decide whether to close the gap above.

CLAUDE_CODE_REMOTE is unset in a desktop session here, Remote Control included (checked with printenv).

No version bump; the release PR follows the merge.

🤖 Generated with Claude Code


Summary by cubic

Makes a hosted session's plain clone count as its own checkout via the new worktree.ownCheckout setting, so Claude can write there instead of every mutation being refused by the worktree guard.

  • The clone only counts as owned while the hook's environment matches every variable in the setting's env, and only for the clone named by CLAUDE_PROJECT_DIR.
  • Inside an owned clone, its git folder, kavach's settings file and code, and each configured protectedPaths entry stay refused, as well as any target that holds one of them.
  • Paths compare by file identity, so a case spelling or a symlink cannot reach a fence.
  • Off by default; an older kavach refuses every call on the unknown key.
  • Known gap: commands whose writes count as the whole checkout (git checkout HEAD -- <path>, git apply, pnpm install) can still change fenced paths; the README and tests record this.