šŸ› fix(kavach): a session whose record names a main checkout is judged in a peer's worktree, and can cd out

Merged
AT
1d ago
+449-38

Slice: #641

The slice was widened on 2026-11-27 to take in the cd out. Alok approved it with a šŸ‘ on its new Proposal: https://github.com/veda-hq/veda/issues/641#issuecomment-7202809973.

What changes

A session whose desktop-app record names a main checkout is now judged by both rules below. Every scheduled task's record does, because the task makes its own worktree later.

  • Writes. other-sessions-worktree now judges such a session's writes. Before, its write in a live peer's worktree ran unchecked, even under refuse (probe case 11).
  • The cd out. cd-out-of-worktree now lets it leave that worktree. Before, the rule refused every cd out of it (cases 11c, 11m). With both rules on refuse, the session could then only read.

The code:

  • boundary.py: session_main_checkout(session_id, own, environ) names the main (not linked) checkout the record names, when ownable_checkout would not take it (not the plain clone worktree.ownCheckout names).
    • It shares session_worktree's record checks, which now live in recorded_folder: host id, cliSessionId, and an absolute real directory that is not a symlink.
    • session_worktree's behaviour is unchanged. Its root check is now samefile, not resolve() !=: the root is the path or a folder above it, so an ordering mutant of != could not be killed.
  • worktree_guard.py other_sessions_worktree: when session_worktree is None and session_main_checkout is not, it runs worktree_owners and fires as today, with its own message.
    • A worktree session never asks for the main checkout.
    • The scan-failed fail-open (#594) is kept.
    • The message says the session started in the main checkout, so its own worktree is one it made, and it gives no cd to the main checkout.
  • rules.py cd_out_of_worktree: the new branch applies when there is no recorded worktree but a recorded main checkout, and the shell is in a managed worktree that worktree_owners finds a live owner for (where the write rule refuses).
    • A cd passes to that main checkout, or into a managed worktree of it whose scan finds no live owner (main_checkout_way_out).
    • Everything else is judged as before: a cd into another live peer's worktree, a cd from a worktree no one else works in, and any cd after a failed scan (the write rule then finds no owner either).
    • A worktree session never asks for the main checkout.
  • Tests:
    • test_worktree_guard.py:
      • in a worktree it made (and an agent-* one), the record is silent under refuse;
      • in a live peer's worktree it warns, and it refuses under REFUSE;
      • a read and a cd still pass the worktree guard;
      • a failed scan allows the call and logs scan-failed;
      • session_main_checkout gets a case per check, including a malformed id the record also carries, a symlinked app folder, a non-string path, and a relative path that is a main checkout from the hook's folder;
      • this session's worktree sorting after the shell's still fires;
      • session_worktree never raises.
    • test_rules.py (MainCheckoutRecord):
      • from a live peer's worktree, a cd to main, to a subfolder of it and to a made worktree passes;
      • a cd into another live peer's worktree, from a worktree no one else works in, after a failed scan, or to another repository's (sorting before and after) or an unmanaged worktree, still fires;
      • a later cd in the same command is judged from where the way out led;
      • one end-to-end case runs through command_guard.py with both rules on refuse.
    • The main-checkout cases move out of test_a_record_naming_no_worktree_keeps_todays_behaviour and test_a_recorded_path_that_is_no_ownable_root_names_nothing.
  • The new and moved signatures return Optional[...], not X | None, session_worktree's included. The Python mutation gate mutates the | in a return annotation, and under from __future__ import annotations no test can kill those mutants: 55 survived on five signatures.
  • Docs: kavach's README rows for both rules and the docstrings are updated.
  • No version here. Main took 0.8.14, 0.8.15 and sarathi 0.8.16 while this was built, so this pull request sets no version. That differs from the slice's wording, which puts the bump here; the outcome is the same. A release pull request (Slice: #641) sets kavach and sarathi to the next legal version once this merges, and its tag follows at once.

A known limit: a side session started inside a scheduled task's own made worktree records it as its cwd. That makes the worktree "another live session's" for the task.

Probe

Crafted PreToolUse events, judged only, with nothing run. Each event goes through three things:

  • athya_mono's live hook (kavach 0.8.9);
  • this branch's worktree guard;
  • this branch's command guard.

Both guards run with athya_mono's .claude/kavach.json, where both rules refuse. The full output is in a comment below.

caseathya_mono hook (0.8.9)this branch: worktree guardthis branch: command guard
1 worktree session, touch in a live peer's worktreeexit 2, rule firedexit 2, rule firedexit 0
2 worktree session, Write in a live peer's worktreeexit 2, rule firedexit 2, rule fired(not Bash)
3 / 4 / 5 / 7 / 8 / 9 read, cd back, own, no-record worktree, wrong session, CLIexit 0exit 0exit 0
10 scheduled task, touch in a worktree it madeexit 0exit 0exit 0
11 scheduled task, touch in a live peer's worktreeexit 0 (unchecked)exit 2, rule firedexit 0
11w scheduled task, Write in a live peer's worktreeexit 0 (unchecked)exit 2, rule fired(not Bash)
11r scheduled task, ls in the peer'sexit 0exit 0exit 0
11c scheduled task, cd to the worktree it made, from the peer'sexit 2 (cd-out-of-worktree)exit 0exit 0
11m scheduled task, cd to the main checkout, from the peer'sexit 2 (cd-out-of-worktree)exit 0exit 0
11e scheduled task, cd into the peer's, from the main checkoutexit 0exit 0exit 0
11p scheduled task, cd into another live peer's, from the peer'sexit 2exit 0exit 2
11b scheduled task, cd to the main checkout, from the worktree it madeexit 2exit 0exit 2

Cost of the scan

boundary.worktree_owners was timed in process, 21 runs each, against the real records folder: 1,209 records, measured 2026-11-27 on Alok's Mac.

worktree scannedmedianminmaxowners
athya_mono silly-gauss-043949 (one live owner)52.7 ms45.2 ms358.7 ms1
athya_mono 0114-w-02 (no record)54.5 ms43.6 ms91.9 ms0
veda angry-shirley-92bed1 (this session's)56.7 ms49.2 ms84.9 ms1

What a main-checkout session now pays:

  • Each write in a managed worktree: one scan, about 55 ms. Before, it returned before the scan.
  • Each cd that leaves a managed worktree: one scan of the shell's worktree, plus one of the target when the target is another managed worktree.

A worktree session pays nothing new. An earlier run, taken while the test suite ran beside it, gave medians of 375–526 ms; that was the load, not the scan.

šŸ¤– Generated with Claude Code