Fix duplicate gateway spawning and add extension unit tests in pi-coding-agent (0xKobold)
Session from 0xKobolds on Hugging Face by moikapy · MIT · condensed by Coders Talk
Diagnose why multiple WebSocket gateway servers were coming up on session start for the 0xKobold pi-coding-agent framework, then write unit tests covering the extensions directory including plan mode and subagent spawning.
- +0mPrompt · +0mInvestigate why multiple gateways are up when only one should run by default.
- +0mAgent did · +0mTraced ports 18789-18792 via lsof/ps/pstree, read gateway-extension.ts, pi-config, update/self-update extensions and the subagent spawner to find why several bun processes each opened their own gateway.
- +1mAgent did · +1mIdentified that gateway-extension, update-extension and self-update-extension all fired session_start handlers on every subagent spawn; added hasAttemptedStart flags and KOBOLD_SUBAGENT/PI_SESSION_PARENT env checks to gate startup to the main session only.
- +10mPrompt · +10mWrite unit tests for the src/extensions directory.
- +10mAgent did · +10mExplored extension files and existing test setup, then wrote a mock ExtensionAPI helper plus new test suites for persona-loader, fileops, gateway, mode-manager and agent/subagent extensions.
- +15mFail · +15mgateway.test.ts failed on the gateway:status command test because the handler used console.log instead of sendMessage, so the test's assertions didn't match reality.
- +15mAgent did · +15mRead the gateway-extension source around the status/broadcast commands, rewrote the test expectations to match the real command output, and got all gateway tests passing.
- +15mFail · +15mpersona-loader.test.ts failed because PERSONA_DIR is resolved from the real homedir() at module load time, so tests picked up the developer's actual ~/.0xkobold files instead of the isolated test fixtures.
- +15mAgent did · +15mReworked persona-loader tests to isolate test directories, renamed TEST_DIR references, and marked several file-content tests as skipped since PERSONA_DIR is cached at module import and can't be mocked without restructuring the extension.
- +18mOutcome · +18mExtension test suite (mocks, gateway, fileops, persona-loader, mode-manager, agent/subagent) running under bun test with gateway tests fully passing; a handful of persona-loader tests left skipped due to module-level homedir() caching.
I'd start by naming the exact env vars and lifecycle hook I suspect (session_start firing per-subagent) so the agent goes straight to gateway/update/self-update extensions instead of spending a dozen turns on lsof and pstree. The fix that actually did the work was gating those session_start handlers behind a hasAttemptedStart flag and the KOBOLD_SUBAGENT/PI_SESSION_PARENT env check. The test-writing pass was solid but ran into a real limitation I hadn't flagged up front: PERSONA_DIR is resolved from homedir() at module load, so several persona-loader tests had to be skipped rather than truly fixed.