Skip to content
Hiep Lam
Go back

Inside the Brainstorming Skill, Part 6: Under the Hood

Edit page

“Inside the Brainstorming Skill” - Part 6 of 7 (appendix). Parts 0–5 documented what the skill does; this one opens the visual companion and looks at the machinery - a zero-dependency Node server with a hand-rolled WebSocket, a filesystem seam between agent and browser, and a security model for a localhost server that might not stay on localhost. Source-grounded; every claim is cited file:line.


The first six posts (0 through 5) documented the brainstorming skill as behavior - the Socratic interview, the gates, the visual companion as seen by the user. This appendix is for the curious: it opens the hood of that visual companion and looks at the code underneath.

The question it answers is the one most people ask first: a skill is supposed to be markdown instructions - so why does this one ship a folder of scripts?

What’s actually in the folder

Under skills/brainstorming/scripts/ there are five files, ~1,400 lines total:

FileLinesRole
start-server.sh209Launcher - port, session dir, key, backgrounding, watchdog wiring
server.cjs723The server - HTTP + a hand-rolled WebSocket, file-watching, auth
helper.js167Client side, injected into every screen - clicks, reconnect, overlay
frame-template.html213Page chrome + the CSS classes the agent writes against
stop-server.sh120Teardown - prove identity, then SIGTERM → SIGKILL

None of this touches the Socratic method. It exists for one feature - the visual companion of Part 5 - and nothing else. Decline the companion and not a line of it runs.

The reason a server has to exist

An AI agent acts in discrete turns. It writes some output, ends its turn, and waits. A browser, meanwhile, is real-time - it holds an open connection and reacts to clicks the instant they happen. These two clocks don’t line up, and the agent cannot bridge them itself: it isn’t running between turns, so it can’t serve a page or receive a click.

So the skill inserts a long-lived process between them. And the elegant part is how the agent and that process communicate - they don’t, directly. They communicate through the filesystem.

Why the skill ships a server - the data flow

Trace the eight steps in the diagram:

  1. The agent writes a semantic HTML file into the session’s content/ directory (server.cjs reads this dir; the agent’s only job is to drop a file there).
  2. The server’s fs.watch on that directory fires, debounced ~100ms (server.cjs:590–613).
  3. The server pushes {type:"reload"} to every connected browser over the WebSocket (server.cjs:519–524, 611).
  4. The browser re-requests GET /?key=… (helper.js:100 reloads on that message).
  5. The server serves the newest screen by mtime, wraps a fragment in the frame template, and injects helper.js (server.cjs:406–419, 267–278).
  6. The human clicks an option; helper.js ships a WebSocket event back (helper.js:130–141).
  7. The server appends that click to state/events, one JSON object per line (server.cjs:513–516).
  8. On its next turn, the agent reads state/events and merges it with the terminal reply (this is the loop from Part 5).

The whole architecture falls out of one sentence: the filesystem is the seam between the agent’s turn-based world and the browser’s real-time world. The agent and the server never hold a connection to each other - the agent writes files and reads files; the server watches files and serves a browser. That decoupling is why a turn-based agent can drive a live UI at all.

Why zero dependencies - and a WebSocket written by hand

Open server.cjs and the first four lines are the whole dependency list (server.cjs:1–4):

const crypto = require("crypto");
const http = require("http");
const fs = require("fs");
const path = require("path");

All four are Node built-ins. There is no express, no ws, no chokidar. That is deliberate, and the design spec spells out why (2026-03-11-zero-dep-brainstorm-server-design.md:3,7):

“Replace the brainstorm companion server’s vendored node_modules (express, ws, chokidar - 714 tracked files) with a single zero-dependency server.js… Vendoring node_modules into the git repo creates a supply chain risk: frozen dependencies don’t get security patches, 714 files of third-party code are committed without audit.”

So the server was rewritten to depend on nothing - which meant implementing the WebSocket protocol by hand. server.cjs:6–81 is a compact RFC 6455 implementation: the SHA-1 + magic-GUID handshake (computeAcceptKey, server.cjs:12–14), frame encode/decode across the three length encodings, XOR-unmasking of client frames, and exactly four opcodes - TEXT, CLOSE, PING, PONG (server.cjs:8). Binary frames, fragmentation, and extensions are deliberately skipped because the only payloads are small JSON click events between localhost peers (design spec:33). A 10 MB frame cap (server.cjs:10) guards against a hostile client.

This is what lets Superpowers’ own CLAUDE.md claim the project is “a zero-dependency plugin by design.” The companion server isn’t an exception to that rule; it was rebuilt to honor it.

One honest caveat. Zero npm dependencies does not mean zero network calls. The companion’s frame optionally loads a Prime Radiant branding logo from a remote URL for rough usage telemetry (server.cjs:106, 242–252). It’s opt-out - set SUPERPOWERS_DISABLE_TELEMETRY (or DISABLE_TELEMETRY) and the logo, and its request, are dropped (server.cjs:107–112, 244–249). The server code itself pulls in no third-party packages.

Why the extension is .cjs

A small detail with a real reason. The file does double duty (server.cjs:712–723):

if (require.main === module) {
  startServer();          // run directly: `node server.cjs` starts the server
}
module.exports = {        // required from a test: export the protocol functions
  computeAcceptKey, encodeFrame, decodeFrame, browserLauncherForPlatform, OPCODES, …
};

The .cjs extension pins the file to CommonJS regardless of any surrounding ESM package config, so require() always works. That is what lets tests/brainstorm-server/ws-protocol.test.js import encodeFrame/decodeFrame and unit-test the hand-rolled protocol in isolation - while the same file, run directly, is the server. One file, two roles, exactly as the design spec intended (design spec:11–14).

The security model: a localhost server that might not stay on localhost

This is the most interesting code in the folder, because the companion can be told to bind a non-loopback host for remote/containerized setups (start-server.sh:11–12). The moment it does, “it’s just localhost” stops being a defense. The server’s answer is a per-session key.

For a feature whose pitch is “I’ll pop open a browser tab,” there is a surprising amount of adversarial thinking here - precisely because a careless version would be a local-network information leak.

Lifecycle: starting, surviving, and dying cleanly

A server spawned by a turn-based agent has a basic problem: who turns it off? The skill solves this with two independent shutdown triggers and a careful launch.

Starting (start-server.sh): it generates a unique session id ($$-timestamp, start-server.sh:114), resolves the harness PID as the server’s owner - the grandparent of the script, since the immediate parent is an ephemeral shell that dies when the script exits (start-server.sh:153–159) - and backgrounds the server with nohup … & disown (start-server.sh:180–183) so it survives the turn. It then polls the log for server-started and re-checks that the process is still alive a beat later, to catch environments that reap detached processes (start-server.sh:185–205). On Codex and Windows/Git Bash, which reap background processes, it auto-switches to foreground mode (start-server.sh:97–107).

Surviving / dying (server.cjs): a watchdog runs every 60 seconds (server.cjs:640–644) and shuts the server down if either the owner harness has exited (ownerAlive via process.kill(pid, 0), server.cjs:634–637) or it has been idle past the timeout (default 4 hours, server.cjs:552–557). If the preferred port is taken it falls back to a random one exactly once (server.cjs:691–708). And when it gets its preferred port it persists the port and token to .last-port/.last-token (server.cjs:663–680), so a restart with the same --project-dir reuses them - which is why an already-open browser tab silently reconnects instead of 403-ing (the tab’s cookie still validates).

Stopping (stop-server.sh): it refuses to kill a PID it can’t prove is this server, matching a per-start instance id baked into the process’s command line (stop-server.sh:43–71) - a stale PID file after a reboot fails closed as stale_pid rather than risking signalling an unrelated process (stop-server.sh:78–83). It tries SIGTERM, waits ~2s, then escalates to SIGKILL (stop-server.sh:85–102). Ephemeral /tmp sessions are deleted; persistent .superpowers/ sessions are kept so the mockups survive for later review (stop-server.sh:112–115).

The client half: helper.js

The browser side is small but does the unglamorous work that makes the loop feel solid (helper.js):

What the machinery teaches about the skill

Step back and the scripts are a small case study in the same values the prose half of the skill preaches:

So: the scripts are there because one feature - and only one - needs to render in a browser and hear clicks back. Everything about how they’re built (zero-dependency, hand-rolled WebSocket, keyed auth, a filesystem seam, a self-terminating watchdog) follows from a single constraint: ship a live localhost server inside a zero-dependency plugin without letting it become a liability. That constraint, met carefully, is the whole story under the hood.


Sources


← Previous: Part 5 - The Visual Companion · This appendix closes the series.

The series - Inside the Brainstorming Skill: 0 · The Map · 1 · Dialogue, Not Interrogation · 2 · The Interview Engine · 3 · From Answers to Design · 4 · The Spec Pipeline · 5 · The Visual Companion · 6 · Under the Hood (you are here)


Edit page
Share this post:

Previous Post
Inside the Brainstorming Skill, Part 5: The Visual Companion
Next Post
Pseudo-terminals (PTYs): a deep dive

Keep reading

A few more pieces in the same vein.

Rather read on your own time? Subscribe via RSS