Skip to content

fix: keep session state per transcript so parallel sessions don't collide - #5

Open
michel-slm wants to merge 1 commit into
vincent-k2026:masterfrom
michel-slm:per-session-state
Open

michel-slm wants to merge 1 commit into
vincent-k2026:masterfrom
michel-slm:per-session-state

Conversation

@michel-slm

Copy link
Copy Markdown

Fixes #4.

Problem

With two Claude Code sessions open, every statusline render from one session overwrote the other's entry in the shared ~/.claude/plugins/codachi/state.json. Each switch re-rolled the pet's species and palette, cleared events.json, and counted a new session in memory.json, so pets changed mid-session and totalSessions (and with it the relationship tier) grew far faster than real use.

Fix

state.json keeps a sessions map keyed by transcript path:

  • Only an unseen transcript path counts a session, rolls a pet and clears events. Known sessions keep their identity, and rewrite the file at most once a minute.
  • Uptime is credited per session; sessions unseen for a day (or beyond the newest 50) are pruned and their remaining uptime credited.
  • The top-level fields still mirror the latest session and the schema version stays 1, following the additive-schema rule in state.ts, so older codachi and codachi stats read the file unchanged. A file written by 0.3.0 is adopted rather than reset: its session keeps its pet and its uptime is still credited.

Before and after

The same reproduction run against the npm 0.3.0 package and against a tarball packed from this branch (npm pack, installed with npm install --prefix): two sessions, A and B, render alternately for five rounds in an isolated HOME. Each line shows the pet each session sees.

== codachi 0.3.0 (npm) ==
round 1  A: animal=2 palette=1   B: animal=2 palette=4
round 2  A: animal=2 palette=9   B: animal=3 palette=6
round 3  A: animal=2 palette=6   B: animal=1 palette=7
round 4  A: animal=0 palette=1   B: animal=2 palette=3
round 5  A: animal=3 palette=0   B: animal=0 palette=3
totalSessions = 10 (2 sessions actually started)

== codachi 0.3.1-pr4.1 (this branch) ==
round 1  A: animal=1 palette=8   B: animal=4 palette=3
round 2  A: animal=1 palette=8   B: animal=4 palette=3
round 3  A: animal=1 palette=8   B: animal=4 palette=3
round 4  A: animal=1 palette=8   B: animal=4 palette=3
round 5  A: animal=1 palette=8   B: animal=4 palette=3
totalSessions = 2 (2 sessions actually started)
Reproduction script
#!/bin/bash
# Alternate statusline renders from two sessions against one codachi build,
# in an isolated HOME, and report each session's pet and the session count.
set -u
entry="$1"; label="$2"; rounds="${3:-5}"
home="$(mktemp -d)"
render() {  # $1 = transcript path
    printf '{"transcript_path":"%s","model":{"display_name":"Test"},"context_window":{"used_percentage":10,"context_window_size":200000}}' "$1" \
        | HOME="$home" NO_COLOR=1 node "$entry" >/dev/null 2>&1
    python3 -I -c '
import json,sys
s=json.load(open(sys.argv[1]+"/.claude/plugins/codachi/state.json"))
e=(s.get("sessions") or {}).get(sys.argv[2])
if e is None and s.get("transcriptPath")==sys.argv[2]: e=s
a,p=(e["animalIndex"],e["paletteIndex"]) if e else ("-","-")
print(f"animal={a} palette={p}",end="")' "$home" "$1"
}
echo "== $label =="
for i in $(seq 1 "$rounds"); do
    a=$(render /tmp/session-A.jsonl); b=$(render /tmp/session-B.jsonl)
    printf 'round %d  A: %s   B: %s\n' "$i" "$a" "$b"
done
python3 -I -c 'import json,sys; print("totalSessions =", json.load(open(sys.argv[1]+"/.claude/plugins/codachi/memory.json"))["totalSessions"], "(2 sessions actually started)")' "$home"
rm -rf "$home"

Usage: ./compare.sh <path to dist/index.js> <label>

Tests

  • 8 new tests in src/state.test.ts simulate separate render processes sharing one state.json. Run against the unmodified state.ts, the 5 that cover this bug fail (pets stay stable, sessions counted once, no event clearing on a switch, the top-level mirror, malformed entries); the other 3 pass on both, as they cover behaviour 0.3.0 already had.
  • Full suite: 369 passed (361 before), tsc clean, npm run bench p95 0.03ms.

Not in this PR

A genuinely new session still clears events.json, which is shared by all sessions, so starting a session can drop another's recent events. Fixing that means the hook writing events per session as well; happy to follow up separately.

…lide

With two Claude Code sessions open, each statusline render from one
session overwrote the other's entry in the shared state.json. Every
switch re-rolled the pet's species and palette, cleared events.json,
and counted a new session in memory.json, so pets changed mid-session
and totalSessions and the relationship tier grew far faster than real
use.

state.json now keeps a `sessions` map keyed by transcript path. Only
an unseen transcript path counts a session, rolls a pet and clears
events; known sessions keep their identity and rewrite the file at
most once a minute. Uptime is credited per session, and sessions
unseen for a day (or beyond the newest 50) are pruned and credited.

The top-level fields still mirror the latest session and the schema
version stays 1, so older codachi and `codachi stats` read the file
unchanged, and a 0.3.0 file is adopted rather than reset.

Fixes vincent-k2026#4

Assisted-by: Claude Code:claude-opus-5-5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Parallel Claude Code sessions re-roll each other's pet and inflate session counts

1 participant