š fix(kavach): a session whose record names a main checkout is judged in a peer's worktree, and can cd out
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-worktreenow judges such a session's writes. Before, its write in a live peer's worktree ran unchecked, even under refuse (probe case 11). - The
cdout.cd-out-of-worktreenow lets it leave that worktree. Before, the rule refused everycdout 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, whenownable_checkoutwould not take it (not the plain cloneworktree.ownCheckoutnames).- It shares
session_worktree's record checks, which now live inrecorded_folder: host id,cliSessionId, and an absolute real directory that is not a symlink. session_worktree's behaviour is unchanged. Its root check is nowsamefile, notresolve() !=: the root is the path or a folder above it, so an ordering mutant of!=could not be killed.
- It shares
worktree_guard.pyother_sessions_worktree: whensession_worktreeisNoneandsession_main_checkoutis not, it runsworktree_ownersand fires as today, with its own message.- A worktree session never asks for the main checkout.
- The
scan-failedfail-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
cdto the main checkout.
rules.pycd_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 thatworktree_ownersfinds a live owner for (where the write rule refuses).- A
cdpasses 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
cdinto another live peer's worktree, acdfrom a worktree no one else works in, and anycdafter a failed scan (the write rule then finds no owner either). - A worktree session never asks for the main checkout.
- A
- 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
cdstill pass the worktree guard; - a failed scan allows the call and logs
scan-failed; session_main_checkoutgets 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_worktreenever raises.
- in a worktree it made (and an
test_rules.py(MainCheckoutRecord):- from a live peer's worktree, a
cdto main, to a subfolder of it and to a made worktree passes; - a
cdinto 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
cdin the same command is judged from where the way out led; - one end-to-end case runs through
command_guard.pywith both rules on refuse.
- from a live peer's worktree, a
- The main-checkout cases move out of
test_a_record_naming_no_worktree_keeps_todays_behaviourandtest_a_recorded_path_that_is_no_ownable_root_names_nothing.
- The new and moved signatures return
Optional[...], notX | None,session_worktree's included. The Python mutation gate mutates the|in a return annotation, and underfrom __future__ import annotationsno 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.
| case | athya_mono hook (0.8.9) | this branch: worktree guard | this branch: command guard |
|---|---|---|---|
| 1 worktree session, touch in a live peer's worktree | exit 2, rule fired | exit 2, rule fired | exit 0 |
| 2 worktree session, Write in a live peer's worktree | exit 2, rule fired | exit 2, rule fired | (not Bash) |
| 3 / 4 / 5 / 7 / 8 / 9 read, cd back, own, no-record worktree, wrong session, CLI | exit 0 | exit 0 | exit 0 |
| 10 scheduled task, touch in a worktree it made | exit 0 | exit 0 | exit 0 |
| 11 scheduled task, touch in a live peer's worktree | exit 0 (unchecked) | exit 2, rule fired | exit 0 |
| 11w scheduled task, Write in a live peer's worktree | exit 0 (unchecked) | exit 2, rule fired | (not Bash) |
11r scheduled task, ls in the peer's | exit 0 | exit 0 | exit 0 |
11c scheduled task, cd to the worktree it made, from the peer's | exit 2 (cd-out-of-worktree) | exit 0 | exit 0 |
11m scheduled task, cd to the main checkout, from the peer's | exit 2 (cd-out-of-worktree) | exit 0 | exit 0 |
11e scheduled task, cd into the peer's, from the main checkout | exit 0 | exit 0 | exit 0 |
11p scheduled task, cd into another live peer's, from the peer's | exit 2 | exit 0 | exit 2 |
11b scheduled task, cd to the main checkout, from the worktree it made | exit 2 | exit 0 | exit 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 scanned | median | min | max | owners |
|---|---|---|---|---|
athya_mono silly-gauss-043949 (one live owner) | 52.7 ms | 45.2 ms | 358.7 ms | 1 |
athya_mono 0114-w-02 (no record) | 54.5 ms | 43.6 ms | 91.9 ms | 0 |
veda angry-shirley-92bed1 (this session's) | 56.7 ms | 49.2 ms | 84.9 ms | 1 |
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
cdthat 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