Skip to content

REMEMBER_BRANCH_CMD: per-session identity for concurrent sessions of one project - #496

Merged
fdaviddpt merged 2 commits into
mainfrom
fix/481
Sep 1, 2026
Merged

fdaviddpt merged 2 commits into
mainfrom
fix/481

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Closes #481.

What

Adds REMEMBER_BRANCH_CMD, an env var naming an executable invoked as $REMEMBER_BRANCH_CMD "$SESSION_ID", whose stdout resolves the | <branch> identity slot of each ## HH:MM | <branch> memory-log header in scripts/save-session.sh. It is checked after REMEMBER_BRANCH and before the existing git branch --show-current fallback; a non-zero exit or empty stdout falls through to that fallback (now with a WARNING logged naming the command and the session, see below).

Why

REMEMBER_BRANCH is process environment, inherited once at launch, and the git-branch fallback it wraps resolves off $PROJECT_DIR -- both are therefore identical for every session of one project. With several Claude Code sessions running concurrently in one project (a real, previously-established case: #212, and post-tool-hook.sh's own header), every entry collapses to the same identity slot, which is exactly the failure REMEMBER_BRANCH was added to prevent, reached through the branch of the original fallback expression that was supposed to be safe (a real git repo, resolving successfully). $SESSION_ID is already computed earlier in save-session.sh and differs per session, so it is the value handed to the new command.

Self-review follow-up (second commit)

An oss:auditor pass on the first commit found the new fall-through path completely silent: a configured-but-broken REMEMBER_BRANCH_CMD (typo'd path, permission denied, a resolver that starts failing) was indistinguishable from REMEMBER_BRANCH_CMD never having been set, with stderr discarded and no log call -- inconsistent with three other fallback paths in this same file, which each log an ERROR/WARNING specifically so a fault doesn't silently read as the ordinary case. Below-bar item, tooling and platform notes, and the raw finding text are in the sections below.

Tests

tests/test_save_session_branch_override.py -- rewritten, now 12 cases: the original REMEMBER_BRANCH truth table (env wins, git fallback, unknown fallback, empty-string-treated-as-unset, the :- operator guard) plus the new REMEMBER_BRANCH_CMD cases (env wins over the command, command wins over git, command receives $SESSION_ID as $1, non-zero exit falls through and logs, empty stdout falls through and logs, success does not log, unset leaves old behavior unchanged and logs nothing -- both a negative assertion and its positive control for each fall-through case).

Red confirmed both times by reverting just scripts/save-session.sh via git stash and re-running: 11 failed before the first commit's block existed, 2 failed before the second commit's logging existed (both fixed once the code landed).

Full suite (pytest) run twice, once per commit: 1923 passed / 43 skipped (566s), then 1924 passed / 43 skipped (483s) -- run because scripts/save-session.sh is referenced by roughly 50 other test files by grep count, not because anything narrower looked risky.

Docs

README.md "Environment variables" table: new REMEMBER_BRANCH_CMD row, including a note that the command is exec'd directly (no shell reparsing) so a Windows Git-Bash path needs to be forward-slash/PATH-resolvable, not a native backslash path -- added in response to the auditor's platform finding below.

changelog.d/481.added.md added.

Below-bar and reported-for-filing items

See the accompanying agent report's adjacent/review.findings for the full text; anchors quoted here so both sides of the receipt exist in one place:

  • The auditor's Windows/Git-Bash test-coverage gap (finding 2) is only partially addressed: the documentation half is fixed in this PR (README path-form note), but building Windows-specific test fixtures to actually exercise REMEMBER_BRANCH_CMD through Git Bash was judged a design decision (conditional test vs. separate fixture file vs. docs-only) rather than something to decide solo mid-lane -- reported-for-filing, not fixed, and it is a real gap: pytestmark = pytest.mark.skipif(sys.platform == "win32", ...) covers the whole test module (pre-existing, unchanged by this PR) so none of the 12 REMEMBER_BRANCH_CMD tests run on the Windows CI leg.

…sessions in one project (#481)

REMEMBER_BRANCH is process environment, inherited once at launch: every
session of one $PROJECT_DIR shares the same value, and so does the git
branch fallback it wraps. Several Claude Code sessions running at once
in one project therefore write the same "| <branch>" identity slot into
every entry, which is the exact failure the slot exists to prevent,
reached through the branch that is supposed to be safe.

Add REMEMBER_BRANCH_CMD, an executable invoked as
`$REMEMBER_BRANCH_CMD "$SESSION_ID"` and checked after REMEMBER_BRANCH,
before the git fallback. $SESSION_ID is already in scope at save time
and differs per session, so a site can map it to a name of its own
choosing. A non-zero exit or empty stdout falls through to the existing
git/unknown chain rather than writing a broken slot.

Co-Authored-By: Max <noreply>
…not configured"

Self-review (oss:auditor) on 9be452e found the new fall-through path
(a configured REMEMBER_BRANCH_CMD that exits non-zero or prints
nothing) indistinguishable from REMEMBER_BRANCH_CMD never having been
set at all -- no log line, stderr discarded. Every comparable
fallback in this script logs on failure (torn now.md read, unreadable
entry, unrecognised envelope); this one didn't.

Restructure the BRANCH-resolution block so a configured-but-failing
resolver logs a WARNING naming the command and the session before
falling through to the git/unknown chain, while an unconfigured one
stays silent as before. Also note in the README that the command is
exec'd directly (no shell reparsing), so a Windows Git Bash user needs
a forward-slash/PATH-resolvable path, not a native backslash one.

Co-Authored-By: Max <noreply>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant