šŸ› fix(kavach): a write in another live session's worktree warns, 0.7.19

Merged
AT
7d ago
+510-5

Slice: #375
Refs #363

Under work item #43.

kavach's worktree guard admitted a write in any .claude/worktrees worktree, another session's included. A session whose shell lands in another session's worktree could edit its files, commit on its branch and push it. This adds rule other-sessions-worktree at the guard's ownership gate. It warns on trial, and refuses once rules in .claude/kavach.json says so.

When it fires: a call the gate admits in a managed worktree, when both of these hold:

  • the desktop app's record of this session (the one #366 trusts: CLAUDE_CODE_HOST_SESSION_ID names it, its cliSessionId is the payload's session_id) names a worktree that is not the shell's;
  • another record, not archived, names the shell's worktree as its cwd.

The finding names both worktrees first (the log row clips at 200 characters), then the cd back. It covers Bash, Edit, Write and NotebookEdit, and the trusted and reviewed commands, since it sits before their early returns. Reads never reach it.

When it does not:

  • this session's own worktree, a worktree shared with its starter (its record names it);
  • a worktree the session made itself, and a subagent's agent-* worktree (no other record names them);
  • no record (CLI sessions), a record that does not match, a record naming a main checkout.

In all of these the guard judges as in 0.7.18.

What changes:

  • boundary.worktree_owners(root, host) returns the other live records naming root. It reads each record's first 4 KB, where the app writes cwd third.
    • boundary.top_level_string reads only the top-level object's cwd from that head. A nested cwd (as in folderPlace), a value that is not a string, or one cut off reads the whole record instead, so it never skips one wrongly.
    • The whole record is read only when that cwd is the worktree, or when the head has none and the text holds the worktree's name. That name is matched unescaped, as the app writes non-ASCII.
    • It never raises; a failure is no owner, so it can only stay quiet.
  • worktree_guard.py: other_sessions_worktree and judge_other_sessions_worktree, which load rules.py and use its mode_of, Fire, context, answer and refusal.
    • In warn mode the guard prints the rules' PreToolUse answer, carrying additionalContext, once the call is allowed, and logs a warn row. In refuse mode it denies with the rules' BLOCKED line and a refuse row.
    • While warning, an error in the check allows and logs; once refusing, it refuses. A failed scan is not such an error: it is silent in both modes.
  • config.RULE_IDS gains the rule, so rules may name it. README rule row and docstrings.
  • kavach and sarathi move to 0.7.19 (sarathi's VERSION in cli.ts too).

Cost: the scan runs whenever the record's worktree is not the shell's. That is not rare: it covers every write in an agent-* or self-made worktree, not only another session's. Timed on this machine (1058 records, this worktree's guard, 11 runs each, medians): the own worktree adds nothing, a worktree no one owns adds about 40 ms, and one a live record owns about 35 ms. Before the head read it added 400 to 800 ms. Slice #375 and its page were written before this was measured. They call the case rare, give 0.6 s (a Node timing), and describe a byte search; the head read replaces that.

Tests (OtherSessionsWorktreeTest in test_worktree_guard.py, the app folder faked under a test HOME):

  • A Bash, Write and Edit call warns. The warning is a log row naming the rule and mode warn. refusesFrom is shown. Several owners are counted.
  • Under refuse it exits 2 with a refuse row. Reviewed (niyam pr-signals) and trusted (python3 scripts/worktree.py status) commands warn and refuse too.
  • Silent under refuse:
    • the own worktree, a self-made worktree, an agent-* worktree, and a shared worktree;
    • no host variable or a malformed one, no session_id, and another CLI session's id;
    • a read, and a record naming a main checkout, a subfolder, a relative path or a missing folder;
    • an owner that is archived, unreadable, not an object, with a relative cwd, with a malformed id, or that is a symlink, and this session's own record as the only one.
  • A cwd past the head still counts, and a head naming another folder is skipped.
  • A nested cwd before the top-level one is no skip, a non-ASCII worktree name is found, and top_level_string has cases of its own.
  • An error in the check allows and logs on trial, and raises once it refuses. worktree_owners never raises.

pnpm check passes.

Checked by hand: this worktree's built sarathi, driven with crafted PreToolUse events and real records, passes the guard's warning through as additionalContext. That was a touch from another live veda worktree with this session's session_id. The same event without session_id is silent, and so is one from this worktree. The real .claude/hooks/sarathi.sh runs the root pin, so that check is repeated on the pin bump.

Open for Alok (the šŸ‘ approved the slice and did not settle these):

  1. Does #363 close when the warning ships, or stay open until refuse?
  2. What does refuse need first? A trial whose log has no fires proves nothing by itself. Then the crafted event is the only positive fire. A failed scan is also silent and logs nothing, so a quiet log cannot tell "no fires" from "scan broken", and under refuse a failed scan lets the write through. Cubic asked for it to fail closed; I kept the slice's "never raises" to avoid #335's lockout, and it is yours to settle with the switch. I propose reading the trial's rows, plus one real subagent payload confirming it carries the parent's session_id.

The slice's backtest found 12 moves into a worktree another record names. It counted archived owners too, so the shipped rule, which skips them, fires on fewer.

Not here: CLI sessions and unmatched records keep today's behaviour. A worktree whose owner is archived or has no record is not protected. Switching to refuse is a later change.

šŸ¤– Generated with Claude Code