Repository navigation
fix(brainstorming): persist brainstorm.choice() events and keep companion session dirs stable across restarts - #2761
xianfengsong wants to merge 6 commits into
Conversation
…estart Co-Authored-By: Claude Code <noreply@anthropic.com>
Co-Authored-By: Claude Code <noreply@anthropic.com>
Co-Authored-By: Claude Code <noreply@anthropic.com>
danishkumar26
left a comment
There was a problem hiding this comment.
Reviewed. Fixes both issue #2725 bugs: choice-shaped events now persist to state/events with choice mirrored from value, and --project-dir restarts reuse a stable server session directory with an ownership-proven kill of the previous server plus an orphan sweep for pre-fix processes.
Verified: tests/brainstorm-server/start-server.test.sh 19/19 pass (stable directory, restart marker cleared, port reuse, no unrelated-process kills, orphan sweep); server.test.js 35/35 pass including the new choice-persistence test.
Non-blocking note: the persistence gate uses event.choice || (...), so a non-string choice of 0 would not be persisted. Choice values arrive as strings from data attributes, so this does not occur in practice.
Fixes #2725
Who is submitting this PR? (required)
glm-5.3-flashWhat your human partner asked you to do, in their words:
Human-authored summary (required, under 200 words)
Issue #2725 was not theorized — I reproduced both bugs with my own clicks in a real browser: choosing an option through
window.brainstorm.choice()never reached the agent, and after any companion restart the screens and directories diverged, leaving the agent and the browser tab in disagreement about where content lived. This PR fixes both inside the companion's own scripts. Before approving I checked the RED→GREEN evidence below, reviewed the complete diff againstupstream/dev, and re-ran the reproduction myself on the fixed code to confirm both bugs are gone.What problem are you trying to solve?
Issue #2725, both bugs manually reproduced (real browser, real clicks) and documented with a repro script in the issue comment:
brainstorm.choice()events are silently dropped. The helper API sends{type:'choice', value}, but the server's persistence gate inhandleMessage(server.cjs) requiresevent.choiceto be present. The event reaches the server's stdout log and never thestate/eventsfile the agent reads. The page tells the user the choice was registered; the agent sees nothing.start-server.shcreates a unique session dir per invocation (SESSION_ID="$$-$(date +%s)") while.last-port/.last-tokenare project-shared. After a same---project-dirrestart the URL is byte-identical (the already-open tab reconnects, asvisual-companion.mdpromises) but the reportedscreen_dirandstate_dirpoint at a fresh empty directory. Screens pushed to the previously-noted directory are never served; the user's new clicks land in a directory the agent doesn't read; the old directory's events file holds plausible-looking stale records.Session failure mode (from the issue): the agent asks the user to pick between options on a served screen, the user clicks, the companion confirms the click visually, and the agent's next turn has no record of the choice — or, after any restart, the agent writes new screens into a directory the browser tab is no longer being served from.
What does this PR change?
In
skills/brainstorming/scripts/server.cjs,handleMessagenow also persists events shaped{type:'choice', value}by normalizing them to{...event, choice: event.value}(events already carryingchoicepersist unchanged, non-choice noise is still not persisted). Inskills/brainstorming/scripts/start-server.sh,--project-dirmode now uses one stable session directory (<project>/.superpowers/brainstorm/server/) instead of a per-invocation<pid>-<epoch>dir, kills a still-live previous instance only with ownership proof (pid alive AND its command line carries--brainstorm-server-id=<id from that dir's state file>; ambiguous metadata fails closed), sweeps live pre-fix orphan servers on the first post-upgrade restart (stop only provable ones, never delete directories), and clears the previous run'sserver-stoppedmarker on every successful start. Tests intests/brainstorm-server/(server.test.js +1, start-server.test.sh +11 tests across four new sections) pin each behavior RED-first.Is this change appropriate for the core library?
Yes. The brainstorming skill and its visual companion are core general-purpose skills; both bugs reproduce on any project, any domain, with any harness that runs the companion. The change adds no dependency, integrates no third-party service, and is not project-, team-, or tool-specific — it makes the companion's own documented promises true (choices reach the agent; the tab reconnects to the same screens after a restart). The only touched files are the companion's scripts and their test suite.
What alternatives did you consider?
helper.jssendschoice:already-shaped). Rejected: tabs running a stale cached pre-fix helper, and custom pages callingbrainstorm.send({type:'choice', value})directly, would stay broken. Server-side normalization repairs every sender path at one site;helper.jsstays byte-identical.start-server.shcallstop-server.shto clean up the previous instance. Rejected:stop-server.shrm -rfs the session dir when it is under/tmp/*, so a project checked out under/tmp(or a user pointing--project-dirat a/tmppath) would lose its screens/events on every restart. The ownership proof is inlined instead, with no directory deletion anywhere in the kill path.Does this PR contain multiple unrelated changes?
No — one problem, one PR. Both bugs are the two halves of issue #2725 and they interact: making the session dir stable is what makes the previously dormant "kill any existing server" block live in project mode, which is what requires the ownership-proof hardening and the upgrade-path orphan sweep. The
server-stoppedmarker cleanup falls out of the same stable-dir change (with a stable dir, a stale marker would make the documented aliveness check permanently ambiguous). The singleserver.cjshunk (3 lines) is Bug 1; everything else instart-server.shand the tests serves Bug 2. The spec commit (f637583) is the design document for exactly this fix.Existing PRs
Searched
obra/superpowersPRs (open and closed) on 2026-12-01 for "2725", "brainstorm choice events", and "companion screen_dir restart" viagh search prs— zero hits. No prior attempt exists to differentiate from.Environments tested
Live walkthrough host: macOS 15.7.7 (Darwin 24.6.0, arm64), Node v22.23.2,
bash5.3 (homebrew;/bin/bash3.2 also exercised by the test suites' bash-3.2-safe idioms).New harness support (required if this PR adds a new harness)
N/A — this PR adds no harness support. It changes the brainstorming companion's server/start scripts and their tests only.
Clean-session transcript for "Let's make a react todo list"
Evaluation
?key=bootstrap page, cookie jar, key-authed WebSocket).tests/brainstorm-server/package.jsonruns the suites in a single&&chain, andbranding.test.js— fifth in the chain — carries 3 failures that pre-exist on the branch based4b6d3be(upstream/dev, untouched by this diff), so the chain can never complete on this branch. Every suite was therefore executed and reported individually: ws-protocol 32, helper 15, browser-launcher 3, auth 20, branding 3 failed (pre-existing), server 35, lifecycle 13, start-server 19, stop-server 7, windows-lifecycle 9 pass / 3 skip.bash scripts/lint-shell.shwas likewise partially executed, becauseshellcheckis not installed locally (caveat 2 below).Baseline before any fix (upstream/dev
d4b6d3be, every suite run individually becausenpm test&&-chains and truncates at the first failure):ws-protocol32/0,helper15/0,browser-launcher3/0,auth20/0,branding4 pass/3 fail (pre-existing on upstream/dev, untouched by this PR — see caveat below),server34/0,lifecycle13/0,start-server8/0,stop-server7/0. In this baseline, abrainstorm.choice()click produced NO line instate/events(Bug 1), and a same-project restart reported a differentscreen_dir/state_dir(Bug 2).RED (new test added, fix not yet present):
server.test.js: 34 passed, 1 failed —choice-shaped events persist with a choice field:state/eventsnever created; the{type:'choice', value:'blue'}event was dropped by the old gate.start-server.test.sh: 11 passed, 3 failed — project mode still stacked<pid>-<epoch>dirs (found 2),server-stoppedmarker survived a restart.start-server.test.sh: 15 passed, 1 failed — with a planted unrelated live pid inserver.pid, the old bare kill murdered the innocent process (unrelated alive=no).start-server.test.sh: 18 passed, 1 failed — a live pre-fix server in an orphan<pid>-<epoch>dir survived the restart and held the preferred port.GREEN (after each fix, all suites rerun individually):
server.test.js35/0.start-server.test.sh14/0.Final gate on this branch (every suite run individually, 2026-12-01):
visible logo should appear before the Superpowers version text); count did not growbash -nsyntax check of start-server.shLive walkthrough of the #2725 manual repro against the patched scripts (temp project under
$HOME, not/tmp; macOS 15.7.7 / Node v22.23.2):--project-dir, wrote a screen whose button callswindow.brainstorm.choice('top')into the stable content dir, drove the exact helper payload{"type":"choice","value":"top"}over the key-authed WebSocket.state/eventsthen contained exactly:{"type":"choice","value":"top","timestamp":1796120994192,"choice":"top"}.--project-dir. Startup JSON reported the samescreen_dir, the samestate_dir, and the byte-identicalurl(same preferred port 52073 and same persisted token). The pre-restart screen was still served through the browser flow (GET /?key=…→ bootstrap page +Set-Cookie: brainstorm-key-52073=…, thenGET /with the cookie → the pre-restart screen, HTTP 200), andstate/eventsstill held the round-1 choice (history survives restarts by design).server-started, the third bound the same preferred port (52073, matching.last-port— no EADDRINUSE fallback),lsofshowed exactly one listener (the new pid), and the phase-2 cookie jar (the still-open browser tab's cookie) still authenticated against the third instance.Human browser verification (the same repro re-run by the human partner with real clicks after the fix, macOS):
window.brainstorm.choice()screen put{"type":"choice","value":"top","screen":"nav-position","timestamp":1796125602858,"choice":"top"}intostate/events— before the fix the identical action left no events file at all.kill+ same---project-dirrestart, the startup JSON reported the byte-identicalscreen_dir/state_dir(…/brainstorm/server/…) and URL (port 62532); the tab reconnected to the pre-restart screen, a screen pushed after the restart appeared instantly, and its click ("choice":"blue") landed in the same events file. Exactly one session directory (server/) existed — before the fix the same steps produced two diverging directories and an invisible screen.Rigor
superpowers:writing-skillsandcompleted adversarial pressure testing (paste results below)
N/A — no behavior-shaping prose changed (SKILL.md, visual-companion.md, helper.js are byte-identical to upstream/dev).
Adversarial cases pinned by tests: planted pid pointing at an unrelated live process (must not be signaled); orphan whose instance-id does not match its command line (must not be signaled); malformed/missing instance-id files (fail closed); non-choice events must not persist;
/tmpmode must keep per-invocation dirs; orphan directory and its events must survive the sweep;server-stoppedmarker must not survive a restart.rationalizations, "human partner" language) without extensive evals
showing the change is an improvement
Human review
Known caveats to disclose in review (kept in the draft so nothing is hidden at submission time)
visible logo should appear before the Superpowers version text) and are untouched by this PR. They also truncate a barenpm test(the suite&&-chains), which is why every suite above was run and reported individually.scripts/lint-shell.shrequiresshellcheck, which is not installed on the authoring machine, so the shellcheck half of the lint could not run; the script's other half (thebash -nsyntax check) passes, and no CI workflow in this repo runs shellcheck. If maintainers want, the PR can be linted in CI or the human partner can run shellcheck locally before submission.