šŸ› fix(kavach): the cd back to the session's recorded worktree passes, 0.7.18

Merged
AT
8d ago
+241-41

Slice: #364
Fixes #362

Under work item #43.

From another session's managed worktree, kavach's cd-out-of-worktree took that worktree as the session's, since it is ownable. It flagged the cd back to the session's own worktree and named the wrong one. Once a consumer sets the rule to refuse, that strands the session.

What changes:

  • boundary.py gets the desktop app's session-record reader, moved from worktree_guard.py, which keeps using it under the same names: session_record, claude_app_root, SESSION_ID, HOST_SESSION and RECORD_LIMIT.

  • A new boundary.session_worktree(session_id, own, environ) returns the record's worktreePath, else its cwd, only when all of these hold:

    • the hook's CLAUDE_CODE_HOST_SESSION_ID names the record;
    • the record's cliSessionId is the payload's session_id;
    • the path is absolute, a real directory, and the root of a checkout a session can own (ownable_checkout).

    It never raises. Anything else is None.

  • rules.py:

    • Call carries the payload's session_id.
    • cd_out_of_worktree reads the record at most once per call, and only for a cd that leaves the shell's checkout and that #350's pass did not already let through.
    • A cd into the recorded worktree passes, and a later cd in the same command is judged from it.
    • When the shell is elsewhere, a finding reads "(the shell is in X; Y is this session's)" and says cd Y brings it back.
  • With no record that fits, the rule judges exactly as in 0.7.15. Nothing fires that did not fire before.

  • One message changes with a matching record. From a checkout no session owns, a cd that still fires now reads "(the shell is in X; Y is this session's)". It replaces #350's "takes as no session's own" wording. The tests check this wording only from another session's worktree.

  • kavach and sarathi move to 0.7.18. v0.7.17 released #365's sarathi while this was open, and main is merged in for that version clash. sarathi's VERSION in cli.ts changes too. Slice #364 and its page say 0.7.17, written before that tag.

Why the record: the hook payload does not say which worktree is the session's. The app's record does. kavach already trusts it, under the same two checks, for the side-session notes folders a session was granted. I surveyed the records on this machine:

  • Records for worktree sessions have worktreePath.
  • The rest have cwd set to the folder the session started in.
  • cwd does not follow the shell; shellFolders does. In 36 of the 110 records with shellFolders, a shell folder lies outside cwd, while cwd stayed at the worktree the session started in.
  • No record's worktreePath differs from its cwd.

Tests, a new RecordedWorktree class in test_rules.py with the app folder faked under a test HOME:

  • From another session's worktree, cd <recorded>, cd <recorded> && pwd and pushd <recorded>/sub pass. judge() under refuse refuses nothing.
  • The same cd still fires with no host variable, a malformed host id, no session_id, or a wrong cliSessionId.
  • A recorded path that is no ownable root names nothing: /, a worktree's subfolder, the main checkout, a relative path, or a missing folder.
  • A record with only cwd counts.
  • A cd elsewhere names the recorded worktree, including the refusal for a folder in no repository. From the recorded worktree itself, the finding reads as before.
  • A later cd is judged from the recorded worktree.
  • A cd inside the checkout reads no record.
  • Through command_guard.py, under refuse, the cd back exits 0 with session_id and 2 without it.

pnpm check passes.

Not checked yet: one event through veda's real sarathi.sh. The hook runs the root pin, 0.7.15, so that check waits for the pin bump after v0.7.18. On it, this session's session_id and a cwd in another veda worktree should let cd <this worktree> exit 0, and the same event without session_id should exit 2.

Not here: #363, where the worktree guard admits writes in any managed worktree. CLI sessions with no desktop app keep today's behaviour.

šŸ¤– Generated with Claude Code