mirror of
https://github.com/AgentSeal/codeburn.git
synced 2026-11-29 10:43:47 +00:00
* 0.9.25: correctness + provider fixes, energy guard, logo, default DMG CLI/engine fixes: local-model savings count reasoning; ZCode reasoning billed; codex-credits version-boundary tiering; GLM alias repointed to z.ai discounted rate; openclaw/pi/qwen timestamp mtime fallback; hermes delta timestamp; sqlite/warp/hermes busy+permission reclassified as failed (no sealed empty); codex-cache dev+ino; cursor-cache WAL; antigravity server TTL; quota timeout+ grokbot footer+claude near-expiry; serve RSS guard env+hysteresis; CSV export subfolder; providers pane fixed window; Warp credit-based cost. Desktop/menubar: unified flame logo; default DMG; docs signed+notarized. * menubar: one running instance, self-relaunch on language change The single-instance guard only retired strictly older copies, so two login items starting in the same instant both survived, and a copy whose bundle identity did not survive install was never seen at all. The decision is now total (newest launch wins, ties break on pid) and peers are matched on bundle id or executable name. --keep-both opts one launch out. Language changes no longer have the desktop app quit and reopen the menu bar, which made macOS attribute the new process to the desktop app and re-ask for the data-access grant. The menu bar relaunches itself via the existing remote command key; older menu bars that do not watch the key fall back to the old path after 5s. * desktop: export toast shows the saved path; resource buckets on app_close The export toast showed the folder that was picked, not the dated subfolder the files land in. The handler already parsed the CLI's saved line; it now returns that path and the toast uses it. app_close carries cpu/mem (and serveCpu/serveMem when serve reported) as bucket labels, sampled on the existing flush interval. serve adds cpuSec and peak rssMb to its answer frames. Unmeasured figures are omitted, never sent as zero. * menubar guard never leaves zero instances; installer replaces in place; JSON export into folder Guard: an unreadable launch date on either side could make two copies retire each other. Unreadable peers now count only when their pid is lower, and a copy that cannot read its own date retires lower pids only, so a disagreement keeps at least one instance. Installer: an existing bundle in /Applications or ~/Applications is replaced where it lives instead of a second copy being added. The old bundle is moved aside and restored on any failure; a cross-volume move falls back to a copy that is re-verified with codesign. An unwritable location is never escalated: the install goes to ~/Applications and the leftover is named. Export: -o pointing at an existing folder writes the JSON inside it. Short sessions omit the cpu buckets. setLanguage reopens the menu bar if its own relaunch never comes back. AppRelaunch passes the bundle path as an argument. * menubar guard orders by kernel start time; installer opens the copy that exists and recovers a killed install Guard: launch dates could be unreadable and pid order can disagree with start order after pid reuse, so two copies could still retire each other. Ordering now uses the kernel-reported process start time, which both sides read identically. A copy whose start time cannot be read is left out, so the worst case is two menu bars, never none. Installer: with the only copy in an unwritable /Applications, a plain run tried to open a path that did not exist. It now opens the installed copy. A placement killed mid-way is recovered on the next run, a concurrent install is refused, and a failed restore names where the previous bundle is. Process matching uses the exact executable name. Export: a trailing separator names a folder. Telemetry cpu is measured from telemetry start. * installer: a recycled pid can no longer block installs; language change waits for the replacement process An aside left by an earlier install counted as an active install whenever its pid was live, so once that pid was reused every install was refused for good. It now also has to be recent (ctime within 10 minutes), a pid we may not signal is not ours, the refusal names the file, and an orphan that cannot be cleared is reported instead of aborting the install. setLanguage confirmed the relaunch with a matcher the dying process still satisfied, so the reopen fallback never fired. It now waits for a pid that was not running before. * desktop: only the headline refreshes live; act, yield and the optimize scan move to slower tiers An open window ran act report (a fresh CLI process each time), yield and an optimize scan on every refresh. The headline poll now passes --no-optimize and takes the snapshot path; act report refreshes on mount, on manual refresh and every 10 minutes, yield every 5. The optimize scan runs at most once a day per query scope, is stored in userData with its time, and is forced fresh by the Optimize page or a manual refresh. Figures that are not live show their age, and no stored scan means no savings clause rather than a zero. On battery the live interval doubles; the chosen cadence stays the base. * desktop: a stored optimize scan is only valid on the day it was computed; cadence changes no longer refetch The scan cache is keyed on the argv, which says 'today' or '30days', not a date, so yesterday's scan was served as today's. It is now reused only on the same local day. Stored findings in Signals carry their age, a failed scan shows the error instead of scanning forever, and the cold-boot label uses the time the app received the payload. usePolled fetched immediately whenever its interval changed, so every power transition (and launching on battery) refetched every live hook and could spawn an extra act report. Fetching and timer arming are now separate effects. Optimize's yield is on the same 5 minute tier as Overview. * desktop: re-ask for the optimize scan on the first render of a new local day An app left open across midnight kept yesterday's stored savings under Today, because the scan was only requested on mount, scope change or manual refresh. The local day is now one of the hook's triggers; the previous day's figure is dropped while the new scan runs. * serve: a menubar poll is never answered from the status snapshot Inside its settle window the status snapshot returns the pre-change payload on purpose. The resident serve process then memoized that answer as clean, so after a single write followed by quiet the headline stayed on the old figure until the next write or the five minute memo cap. The snapshot is for one-shot runs; the resident process already holds the parsed state, so it now skips it. * tests stop writing the real serve pid file; superseded caches are swept; installer notes reach the desktop card ServeConnection takes its pid file as a parameter, so the Swift tests no longer share ~/.cache/codeburn/menubar-serve.pid with a running menu bar, and the reaper only signals a record that is a serve --stdio command. Superseded session-cache, codex-results and cursor-results files (100MB+ each) are removed once untouched for 14 days, at most once a day. daily-cache and the hermes ledger are never swept: they carry sealed days a version bump adopts. The menu bar installer's leftover-copy advice is emitted as a marked line under CODEBURN_PROGRESS and shown on the desktop card. The skipped-refresh NSLog is debug only. The app's vitest workers turn off Node's built-in web storage. * menubar: anonymous usage statistics, under the same consent as the desktop and the Windows tray The Swift menu bar sent nothing, so every codeburn-menubar row was a Windows tray. It now follows the desktop's telemetry.v1.json when that exists (read only: same install id, same on/off, onboarded required) and otherwise keeps its own state with the same region default (off in EU/EEA/UK/CH and when the region is unknown). Settings gains one Privacy toggle, disabled when the desktop owns the choice. Events use the tray's names; the daily usage snapshot is the CLI's block passed through a sanitizer and is left to the desktop when it is the consent source. Debug builds never send. A resident CLI that gets disabled for the run now says so in the log. * menubar statistics: private network session, abandoned batch on opt-out, a desktop No survives the desktop going away The sender used URLSession.shared, so a server or CDN cookie and the default User-Agent rode along and would have outlived an opt-out. It now owns an ephemeral session with no cookies, no cache, a fixed agent string and no redirects. A batch that is out when the toggle goes off is dropped rather than re-queued under the fresh id. The desktop's answer is recorded locally, so a missing or damaged state file never falls back to the region default, and the standalone id is persisted when minted. The toggle stays usable as a local off switch under desktop consent. Copy states what this app actually sends. * vercel gateway spend is shown on its own row and kept out of totals by default The gateway reports daily aggregates per model, with nothing to match against the local tools that were pointed at it, so every headline counted that traffic twice. Totals now leave the gateway slice out when all providers are shown; the row keeps its full amount and is marked not in total, --provider vercel-gateway is unchanged, and codeburn gateway-totals include folds it back in. The daily cache still stores the slice, so the choice applies retroactively. Reports with no gateway credential are byte-identical. The gateway's request_count now feeds the call count instead of each daily row counting as one call. Hermes: a mid-session price change lands on the day it is observed and sealed days are never rewritten; stated in the code and changelog. * gateway exclusion applies to every report through the parse, and the detail panels follow the headline The first pass narrowed only the day totals, so By tool, top models, top projects, activities, status json projects and any project-filtered table still carried gateway spend next to a headline that did not, and By tool shares could pass 100%. The rule now lives in parseAllSessions: with all providers in scope and the opt-in off, aggregate-only sessions are dropped after the memo, and the callers that derive cached days opt out so stored slices are unchanged. day.projects is reduced like models. The gateway stays visible once, as a labelled not-in-total row with no share, and standalone reports note the excluded amount on stderr so machine output is unchanged. * dashboard: the background history index follows the gateway exclusion too The index keeps the whole corpus so it can fill the daily cache, but its per-period projection handed that unfiltered list to the project, model and activity panels, so they regained gateway spend once the index landed. * ci: a hung lock suite fails in 90s and retries once with a warning; slow Windows tests get measured limits The cache-lock step failed 6 of the last 60 runs with no output for five minutes after the vitest banner; passing runs take 12-18s on every Node version. Each attempt is now capped at 90s and retried once, with a warning whenever that happens. On windows-latest a single CLI spawn takes 1.4-12s, so the four-spawn status snapshot test and the c6 parser suite get limits taken from those runs, each with its reason. Both test jobs print their ten slowest cases. * desktop: Privacy & data uses the same rows as the rest of Settings The pane mixed two static claims with two controls in its own layout. It now uses the about-row pattern every other pane uses: title and hint on the left, one control on the right. The claims became the subtitle. Rows: anonymous usage statistics (toggle, link to what is sent), device sharing state with Manage, local report snapshots with Clear, export with Open export. * desktop: Privacy & data keeps one bounded column and rows that never squeeze The card stretched with the window, stranding each control hundreds of pixels from its text. This pane's heading, subtitle and card now share a 760px column and one text measure. Rows stack on their own width, because the window can never be narrow enough for the old viewport rule to fire; row buttons do not wrap. The what-is-sent link sits on its own line, and the snapshot and export descriptions fit their measure and say this machine. * menubar tests clean up their scratch defaults and leave the real Keychain alone Four test files never removed their UserDefaults suites and the rest removed the domain but left the plist, so thousands of files piled up in ~/Library/Preferences. A shared helper forgets a suite and deletes its file, and every test that makes a suite now uses it. cfprefsd still rewrites an empty plist when the test process exits, so a run leaves about a hundred 42-byte files with no test data in them. The real-Keychain round trip only runs with CODEBURN_TEST_REAL_KEYCHAIN=1. * desktop: the resident parser retires after 15 idle minutes and restarts on demand The renderer stops polling while the window is hidden, but the serve child stayed resident with its parsed cache, one to two GB on a large corpus. After 15 minutes without a request it is stopped the way a settings restart stops it, without spending the unexpected-death budget, and the next request starts a new resident through the existing lazy path, never a one-shot. CODEBURN_SERVE_IDLE_MS tunes or disables it. * desktop: every settings pane keeps a bounded column, and dark buttons have an edge Cards stretched with the window, so on a wide display controls sat up to 1300px from their labels and intro lines ran past 140 characters. All panes now share one left-aligned column (760px, 980px for projects, aliases and pricing, which hold wide rows) and one text measure. Rows stack on their own width instead of a viewport rule the 900px minimum window could never reach, and a long path in Projects no longer overflows the pane. In dark mode the secondary button matched or sat below its surface, and was invisible on the plugin install dialog. It gets a face and a border from two new tokens; light is unchanged. The onboarding consent label matches Settings. * desktop: this device shows its fingerprint on its own line The local name and the fingerprint are two adjacent inline spans, so they read as one run of text. * menubar: the resident parser retires after 15 idle minutes and restarts on demand The menu bar kept its serve child, and the parsed cache with it, for the whole run, even with the popover closed and nothing changing. After 15 minutes without a request the child is retired without counting as a death, and the next request starts a new resident through ensureStarted, never the one-shot fallback, whose snapshot settle window would make the figure lag a whole tick. The window sits above the longest gap between real requests, so a child stays warm while work is happening. CodeBurnServeIdleSeconds tunes or disables it. * serve: a poll re-checks only what the watcher saw change, and shard writes are coalesced With a session being written, every poll re-listed about 2,400 directories and re-fingerprinted about 6,000 transcripts, then rewrote the whole current-month shard (49MB on a large corpus) and read it back on the next poll. The resident process now reuses the previous listing and fingerprints for paths its root watcher proves untouched, and falls back to the full sweep whenever the watcher is missing, unhealthy, overflowed, armed after the sweep, does not cover the path, the memo is older than 4.5 minutes, or the process was suspended. Shard publication is held for up to 90s and flushed when stdin closes; a held write is dropped rather than written over something another process published. One-shot runs are unchanged. Active poll cost 0.71 to 0.33 CPU-s at a 30s cadence; time-to-reflect unchanged at about 1.2s across append, new dir, new file, delete, rename-over, in-flight, watcher overflow and unhealthy watcher. Changelog: state the telemetry default as it is in this version. * a retired parser gets to publish its held shards, and durable providers are never held Both apps retired the serve child with SIGTERM before its stdin-close flush could run, so every retire discarded the coalesced window. They now close stdin, give the child a bounded grace to publish and exit, and signal it only if it is still running; a settings restart does the same. Measured: flushed 0/5 before, 5/5 after, for both clients. copilot's cache entries are the only record once Copilot prunes its own files, so a held window that was dropped (retire, crash, kill -9) before the source was pruned lost that spend for good. A window that a durable provider dirtied is published on the poll that parsed it, as before coalescing existed. The RSS guard publishes a held window before it clears the caches instead of discarding it. * serve: release the lifetime parse after the fill, and make the collection real The RSS guard dropped its memos and then called globalThis.gc, which exists only under --expose-gc; no launcher passes it, so the collection had never run and the freed pages never went back. On a cold start the resident idled at 2.2GB with 364MB of live heap: clearing every memo moved RSS by 0MB, a collection alone by 700MB. After the background fill has sealed its summaries the process now publishes any held shard window, drops the memos it can re-read from disk (the cold start had loaded the cache with scope 'all', which then served every later poll), and collects; it collects again a minute later for as long as a pass returns 64MB, at most four times. Cold idle 2198MB to 700MB, warm 900MB to 781MB. Reports are leaf-identical cold and warm, time-to-reflect unchanged, a released month scope reloads in 0.3-0.5s. * warp: the billed cost reaches reports and survives the cache The provider derived a dollar cost from Warp's own billing record (provider cents, charged usage, or credits), but warp was missing from the reported-cost pass-through, so the cached call kept no cost and every report, cold or warm, priced the conversation from its tokens. A fixture billed at $1.22 reported $0.15. A call whose cost came from a billing record now keeps it; a call that only has the token floor stores none and is still re-priced when prices change. Warp entries re-parse once (provider parse version), and the daily cache moves to v34 so days whose Warp source still exists re-derive; call counts do not move, so nothing is double counted or dropped. * openclaw and pi: a reported cost survives the cache like every other reported cost Both providers read the cost their tool recorded for a message, and both were missing from the reported-cost pass-through, so reports priced every call from tokens instead. Fixtures reported $0.011 against $0.056 (openclaw) and $0.013 against $0.410 (pi). A call with a reported cost now keeps it; a call with none, or an explicit zero, stores none and is still re-priced when prices change. omp had been added to the list in #1150 and pi, the same parser, was never revisited. The provider list is one named set, the unreachable providerCallToTurn is gone, and both providers re-parse once; the v34 daily cache re-derives their days with call counts unchanged. * test: pin parse count per report and per resident poll
1777 lines
80 KiB
TypeScript
1777 lines
80 KiB
TypeScript
// @vitest-environment node
|
|
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
|
import { spawn } from 'node:child_process'
|
|
import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, rmSync, chmodSync } from 'node:fs'
|
|
import { tmpdir } from 'node:os'
|
|
import { delimiter, dirname, join, isAbsolute, relative, win32, posix } from 'node:path'
|
|
|
|
import { spawnCli, spawnCliAction, spawnEnvFor, spawnSpecFor, startServe, killAll, serveUsage, shutdownAll, CliError, cmdShimArgs, escapeForCmd, nodeManagerDirs, notFoundStage, reapOrphanServe, resolveCodeburnPath, resolveTarget } from './cli'
|
|
|
|
let dir: string
|
|
const originalBin = process.env.CODEBURN_BIN
|
|
const originalPathDirs = process.env.CODEBURN_PATH_DIRS
|
|
const originalPathFile = process.env.CODEBURN_CLI_PATH_FILE
|
|
const originalViteUrl = process.env.VITE_DEV_SERVER_URL
|
|
const originalBundled = process.env.CODEBURN_BUNDLED_CLI
|
|
const originalDevRepoRoot = process.env.CODEBURN_DEV_REPO_ROOT
|
|
const originalServeIdle = process.env.CODEBURN_SERVE_IDLE_MS
|
|
|
|
/**
|
|
* Windows has no catchable SIGTERM: child.kill() is TerminateProcess, which ends the child
|
|
* outright, so a grace window, a cleanup handler and a child that ignores the first signal
|
|
* are all things the platform cannot do rather than things this code gets wrong. The same
|
|
* goes for a resident that closes its own stdin. These stay exactly as they are and simply
|
|
* do not run there.
|
|
*/
|
|
const posixOnly = it.skipIf(process.platform === 'win32')
|
|
|
|
/** Writes an executable node script and points CODEBURN_BIN at it. */
|
|
function fakeBin(name: string, body: string): string {
|
|
const p = join(dir, name)
|
|
writeFileSync(p, `#!/usr/bin/env node\n${body}\n`, { mode: 0o755 })
|
|
chmodSync(p, 0o755)
|
|
process.env.CODEBURN_BIN = p
|
|
return p
|
|
}
|
|
|
|
function readMaybe(path: string): string {
|
|
try { return readFileSync(path, 'utf8') } catch { return '' }
|
|
}
|
|
|
|
async function waitFor(condition: () => boolean, timeoutMs = 3_000): Promise<void> {
|
|
const deadline = Date.now() + timeoutMs
|
|
while (!condition()) {
|
|
if (Date.now() > deadline) throw new Error('waitFor timed out')
|
|
await new Promise(resolve => setTimeout(resolve, 10))
|
|
}
|
|
}
|
|
|
|
/** A protocol-faithful fake CLI whose serve child accepts requests before its
|
|
* delayed ready frame. Files expose process starts and heavy request executions
|
|
* without relying on timing or private ServeClient internals. */
|
|
function fakeResidentBin(): {
|
|
startsFile: string
|
|
heavyFile: string
|
|
oneShotsFile: string
|
|
actionsFile: string
|
|
serveEnvFile: string
|
|
} {
|
|
const startsFile = join(dir, 'serve-starts')
|
|
const heavyFile = join(dir, 'heavy-requests')
|
|
const oneShotsFile = join(dir, 'one-shot-reads')
|
|
const actionsFile = join(dir, 'actions')
|
|
const serveEnvFile = join(dir, 'serve-progress-env')
|
|
fakeBin(
|
|
'resident.js',
|
|
`const fs = require('node:fs'); const readline = require('node:readline');
|
|
const command = process.argv[2];
|
|
if (command === 'serve') {
|
|
fs.appendFileSync(${JSON.stringify(startsFile)}, 's');
|
|
const generation = fs.readFileSync(${JSON.stringify(startsFile)}, 'utf8').length;
|
|
fs.writeFileSync(${JSON.stringify(serveEnvFile)}, process.env.CODEBURN_PROGRESS || '');
|
|
const rl = readline.createInterface({ input: process.stdin });
|
|
rl.on('line', line => {
|
|
const request = JSON.parse(line);
|
|
fs.appendFileSync(${JSON.stringify(heavyFile)}, 'h');
|
|
const progress = 'CODEBURN_PROGRESS ' + JSON.stringify({ kind: 'provider', provider: 'claude', state: 'start', generation }) + '\\n';
|
|
process.stdout.write(JSON.stringify({ id: request.id, progress }) + '\\n');
|
|
process.stdout.write(JSON.stringify({ id: request.id, ok: true, output: JSON.stringify({ via: 'serve', generation, args: request.args }) }) + '\\n');
|
|
});
|
|
setTimeout(() => process.stdout.write(JSON.stringify({ ready: true, pid: process.pid }) + '\\n'), 100);
|
|
} else if (command === 'currency') {
|
|
fs.appendFileSync(${JSON.stringify(actionsFile)}, 'a');
|
|
process.stdout.write('currency updated');
|
|
} else {
|
|
fs.appendFileSync(${JSON.stringify(oneShotsFile)}, 'o');
|
|
process.stdout.write(JSON.stringify({ via: 'spawn', command }));
|
|
}`,
|
|
)
|
|
return { startsFile, heavyFile, oneShotsFile, actionsFile, serveEnvFile }
|
|
}
|
|
|
|
/** Writes the repo CLI under this test's isolated dev-root override. */
|
|
function fakeDevRepoCli(): string {
|
|
const repoRoot = join(dir, 'dev-repo')
|
|
const p = join(repoRoot, 'dist', 'cli.js')
|
|
mkdirSync(dirname(p), { recursive: true })
|
|
writeFileSync(p, '#!/usr/bin/env node\n', { mode: 0o755 })
|
|
chmodSync(p, 0o755)
|
|
process.env.CODEBURN_DEV_REPO_ROOT = repoRoot
|
|
return p
|
|
}
|
|
|
|
beforeEach(() => {
|
|
dir = mkdtempSync(join(tmpdir(), 'codeburn-cli-'))
|
|
})
|
|
|
|
afterEach(() => {
|
|
killAll()
|
|
if (originalBin === undefined) delete process.env.CODEBURN_BIN
|
|
else process.env.CODEBURN_BIN = originalBin
|
|
if (originalPathDirs === undefined) delete process.env.CODEBURN_PATH_DIRS
|
|
else process.env.CODEBURN_PATH_DIRS = originalPathDirs
|
|
if (originalPathFile === undefined) delete process.env.CODEBURN_CLI_PATH_FILE
|
|
else process.env.CODEBURN_CLI_PATH_FILE = originalPathFile
|
|
if (originalViteUrl === undefined) delete process.env.VITE_DEV_SERVER_URL
|
|
else process.env.VITE_DEV_SERVER_URL = originalViteUrl
|
|
if (originalBundled === undefined) delete process.env.CODEBURN_BUNDLED_CLI
|
|
else process.env.CODEBURN_BUNDLED_CLI = originalBundled
|
|
if (originalDevRepoRoot === undefined) delete process.env.CODEBURN_DEV_REPO_ROOT
|
|
else process.env.CODEBURN_DEV_REPO_ROOT = originalDevRepoRoot
|
|
if (originalServeIdle === undefined) delete process.env.CODEBURN_SERVE_IDLE_MS
|
|
else process.env.CODEBURN_SERVE_IDLE_MS = originalServeIdle
|
|
rmSync(dir, { recursive: true, force: true })
|
|
})
|
|
|
|
describe('resolveCodeburnPath (Vite development)', () => {
|
|
it('prefers the executable repo dist/cli.js when the Vite dev server is set', () => {
|
|
delete process.env.CODEBURN_BIN
|
|
process.env.CODEBURN_PATH_DIRS = ''
|
|
process.env.CODEBURN_CLI_PATH_FILE = join(dir, 'no-persisted-path')
|
|
process.env.VITE_DEV_SERVER_URL = 'http://localhost:5173'
|
|
const devBin = fakeDevRepoCli()
|
|
|
|
expect(resolveCodeburnPath()).toBe(devBin)
|
|
})
|
|
|
|
it('prefers the repo dev CLI over a persisted-path file (stale global) in dev', () => {
|
|
// A persisted global (e.g. an older Homebrew codeburn) must NOT shadow the
|
|
// repo build in dev, or newly-added commands break. Regression: 0.9.15
|
|
// lacked `sessions`, so the persisted path produced a CLI error.
|
|
delete process.env.CODEBURN_BIN
|
|
process.env.CODEBURN_PATH_DIRS = ''
|
|
const persistedTarget = join(dir, 'stale-codeburn')
|
|
writeFileSync(persistedTarget, '#!/usr/bin/env node\n', { mode: 0o755 })
|
|
chmodSync(persistedTarget, 0o755)
|
|
const persistedFile = join(dir, 'cli-path.v1')
|
|
writeFileSync(persistedFile, persistedTarget)
|
|
process.env.CODEBURN_CLI_PATH_FILE = persistedFile
|
|
process.env.VITE_DEV_SERVER_URL = 'http://localhost:5173'
|
|
const devBin = fakeDevRepoCli()
|
|
|
|
const resolved = resolveCodeburnPath()
|
|
expect(resolved).toBe(devBin)
|
|
expect(resolved).not.toBe(persistedTarget)
|
|
})
|
|
|
|
it('does not return the repo dev CLI outside the Vite dev server', () => {
|
|
delete process.env.CODEBURN_BIN
|
|
process.env.CODEBURN_PATH_DIRS = ''
|
|
process.env.CODEBURN_CLI_PATH_FILE = join(dir, 'no-persisted-path')
|
|
delete process.env.VITE_DEV_SERVER_URL
|
|
|
|
expect(resolveCodeburnPath()).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('resolveTarget (bundled CLI in the packaged app)', () => {
|
|
/** An existing (not necessarily executable) file to stand in for the bundle. */
|
|
function bundledEntry(name = 'cli.js'): string {
|
|
const p = join(dir, name)
|
|
writeFileSync(p, '// bundled cli\n')
|
|
return p
|
|
}
|
|
|
|
it('resolves the bundled CLI, beating a persisted path and PATH', () => {
|
|
delete process.env.CODEBURN_BIN
|
|
delete process.env.VITE_DEV_SERVER_URL
|
|
process.env.CODEBURN_PATH_DIRS = ''
|
|
// A persisted global that should NOT win once a bundled CLI is present.
|
|
const persistedTarget = join(dir, 'stale-global')
|
|
writeFileSync(persistedTarget, '#!/usr/bin/env node\n', { mode: 0o755 })
|
|
chmodSync(persistedTarget, 0o755)
|
|
const persistedFile = join(dir, 'cli-path.v1')
|
|
writeFileSync(persistedFile, persistedTarget)
|
|
process.env.CODEBURN_CLI_PATH_FILE = persistedFile
|
|
|
|
const entry = bundledEntry()
|
|
process.env.CODEBURN_BUNDLED_CLI = entry
|
|
|
|
expect(resolveTarget()).toEqual({ kind: 'bundled', entry })
|
|
expect(resolveCodeburnPath()).toBe(entry)
|
|
})
|
|
|
|
it('CODEBURN_BIN still overrides the bundled CLI', () => {
|
|
const override = fakeBin('override.js', 'process.stdout.write("{}")') // sets CODEBURN_BIN
|
|
process.env.CODEBURN_BUNDLED_CLI = bundledEntry('bundled.js')
|
|
delete process.env.VITE_DEV_SERVER_URL
|
|
|
|
expect(resolveTarget()).toEqual({ kind: 'external', bin: override })
|
|
})
|
|
|
|
it('the dev repo CLI beats the bundled CLI in Vite development', () => {
|
|
delete process.env.CODEBURN_BIN
|
|
process.env.CODEBURN_PATH_DIRS = ''
|
|
process.env.CODEBURN_CLI_PATH_FILE = join(dir, 'no-persisted-path')
|
|
process.env.CODEBURN_BUNDLED_CLI = bundledEntry('bundled.js')
|
|
process.env.VITE_DEV_SERVER_URL = 'http://localhost:5173'
|
|
const devBin = fakeDevRepoCli()
|
|
|
|
const target = resolveTarget()
|
|
expect(target).toEqual({ kind: 'external', bin: devBin })
|
|
})
|
|
|
|
it('falls through when CODEBURN_BUNDLED_CLI points at a missing file', () => {
|
|
delete process.env.CODEBURN_BIN
|
|
delete process.env.VITE_DEV_SERVER_URL
|
|
process.env.CODEBURN_PATH_DIRS = '' // force an empty PATH search space
|
|
process.env.CODEBURN_CLI_PATH_FILE = join(dir, 'no-persisted-path')
|
|
process.env.CODEBURN_BUNDLED_CLI = join(dir, 'does-not-exist', 'cli.js')
|
|
|
|
expect(resolveTarget()).toBeNull()
|
|
expect(resolveCodeburnPath()).toBeNull()
|
|
})
|
|
|
|
// The Windows P0: the packaged app set CODEBURN_BUNDLED_CLI to a C:\ path, but
|
|
// the guard was `startsWith('/')` (POSIX-only), so the bundled CLI was skipped
|
|
// and resolution fell through to a PATH search that finds nothing → not-found
|
|
// on 100% of Windows installs. The guard is now `path.isAbsolute`, which is
|
|
// the platform variant (win32 on Windows). These tests pin both the intent
|
|
// (relative paths are still rejected as a safety guard) and the Windows fix.
|
|
it('resolves an absolute CODEBURN_BUNDLED_CLI (as the packaged app sets it)', () => {
|
|
delete process.env.CODEBURN_BIN
|
|
delete process.env.VITE_DEV_SERVER_URL
|
|
process.env.CODEBURN_PATH_DIRS = ''
|
|
process.env.CODEBURN_CLI_PATH_FILE = join(dir, 'no-persisted-path')
|
|
const entry = bundledEntry('launch.js')
|
|
expect(isAbsolute(entry)).toBe(true)
|
|
process.env.CODEBURN_BUNDLED_CLI = entry
|
|
expect(resolveTarget()).toEqual({ kind: 'bundled', entry })
|
|
})
|
|
|
|
it('rejects a relative CODEBURN_BUNDLED_CLI even when the file exists (guards relative injection)', () => {
|
|
delete process.env.CODEBURN_BIN
|
|
delete process.env.VITE_DEV_SERVER_URL
|
|
process.env.CODEBURN_PATH_DIRS = ''
|
|
process.env.CODEBURN_CLI_PATH_FILE = join(dir, 'no-persisted-path')
|
|
const entry = bundledEntry('rel-launch.js')
|
|
const rel = relative(process.cwd(), entry) // resolvable via cwd, but NOT absolute
|
|
expect(isAbsolute(rel)).toBe(false)
|
|
process.env.CODEBURN_BUNDLED_CLI = rel
|
|
// File exists (isFile true), so only the isAbsolute guard can reject it.
|
|
expect(resolveTarget()).toBeNull()
|
|
})
|
|
|
|
it('rejects a relative CODEBURN_BIN override even when the file exists', () => {
|
|
delete process.env.VITE_DEV_SERVER_URL
|
|
process.env.CODEBURN_PATH_DIRS = ''
|
|
process.env.CODEBURN_CLI_PATH_FILE = join(dir, 'no-persisted-path')
|
|
delete process.env.CODEBURN_BUNDLED_CLI
|
|
const bin = join(dir, 'rel-codeburn.js')
|
|
writeFileSync(bin, '#!/usr/bin/env node\n', { mode: 0o755 })
|
|
chmodSync(bin, 0o755)
|
|
const rel = relative(process.cwd(), bin)
|
|
expect(isAbsolute(rel)).toBe(false)
|
|
process.env.CODEBURN_BIN = rel
|
|
expect(resolveTarget()).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('absolute-path guard is cross-platform (path.isAbsolute, not startsWith("/"))', () => {
|
|
// On the POSIX CI host `isAbsolute` is path.posix.isAbsolute, so these assert
|
|
// the per-platform variants directly to encode the Windows intent regardless
|
|
// of where the suite runs.
|
|
const winPath = 'C:\\Users\\x\\resources\\cli\\dist\\launch.js'
|
|
|
|
it('accepts a Windows absolute bundled path where the old startsWith("/") guard rejected it', () => {
|
|
expect(winPath.startsWith('/')).toBe(false) // old guard: dropped it → the P0
|
|
expect(win32.isAbsolute(winPath)).toBe(true) // new guard on Windows: accepted
|
|
expect(win32.isAbsolute('cli\\dist\\launch.js')).toBe(false) // relative still rejected
|
|
})
|
|
|
|
it('accepts a POSIX absolute path and rejects a relative one (macOS/Linux unchanged)', () => {
|
|
expect(posix.isAbsolute('/res/cli/dist/launch.js')).toBe(true)
|
|
expect(posix.isAbsolute('cli/dist/launch.js')).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('notFoundStage (non-sensitive telemetry enum for a not-found)', () => {
|
|
it('reports bundled-not-absolute for a relative CODEBURN_BUNDLED_CLI', () => {
|
|
delete process.env.CODEBURN_BIN
|
|
process.env.CODEBURN_BUNDLED_CLI = 'cli/dist/launch.js'
|
|
expect(notFoundStage()).toBe('bundled-not-absolute')
|
|
})
|
|
|
|
it('reports bundled-missing for an absolute CODEBURN_BUNDLED_CLI whose file is absent', () => {
|
|
delete process.env.CODEBURN_BIN
|
|
process.env.CODEBURN_BUNDLED_CLI = join(dir, 'nope', 'cli.js')
|
|
expect(notFoundStage()).toBe('bundled-missing')
|
|
})
|
|
|
|
it('reports bin-not-absolute for a relative CODEBURN_BIN override', () => {
|
|
process.env.CODEBURN_BIN = 'relative/codeburn'
|
|
delete process.env.CODEBURN_BUNDLED_CLI
|
|
expect(notFoundStage()).toBe('bin-not-absolute')
|
|
})
|
|
|
|
it('reports no-path-match when nothing is configured', () => {
|
|
delete process.env.CODEBURN_BIN
|
|
delete process.env.CODEBURN_BUNDLED_CLI
|
|
expect(notFoundStage()).toBe('no-path-match')
|
|
})
|
|
|
|
it('spawnCli rejects not-found carrying the resolution stage as detail', async () => {
|
|
delete process.env.CODEBURN_BIN
|
|
delete process.env.VITE_DEV_SERVER_URL
|
|
process.env.CODEBURN_PATH_DIRS = ''
|
|
process.env.CODEBURN_CLI_PATH_FILE = join(dir, 'no-persisted-path')
|
|
process.env.CODEBURN_BUNDLED_CLI = 'cli/dist/launch.js' // relative → bundled-not-absolute
|
|
await expect(spawnCli(['status'])).rejects.toMatchObject({ kind: 'not-found', detail: 'bundled-not-absolute' })
|
|
})
|
|
})
|
|
|
|
describe('spawnSpecFor (bundled CLI runs via Electron-as-node)', () => {
|
|
it('spawns process.execPath with the bundle as argv[0] and ELECTRON_RUN_AS_NODE set', () => {
|
|
const spec = spawnSpecFor({ kind: 'bundled', entry: '/res/cli/dist/cli.js' }, ['status', '--period', 'today'])
|
|
expect(spec.bin).toBe(process.execPath)
|
|
expect(spec.args).toEqual(['/res/cli/dist/cli.js', 'status', '--period', 'today'])
|
|
expect(spec.env.ELECTRON_RUN_AS_NODE).toBe('1')
|
|
// PATH is still augmented (the bundle's own dir leads), harmless for a CLI
|
|
// that itself shells out during pairing/sync.
|
|
expect((spec.env.PATH ?? '').split(delimiter)[0]).toBe('/res/cli/dist')
|
|
})
|
|
|
|
it('spawns an external CLI directly, with no run-as-node flag', () => {
|
|
const spec = spawnSpecFor({ kind: 'external', bin: '/some/bin/codeburn' }, ['status'])
|
|
expect(spec.bin).toBe('/some/bin/codeburn')
|
|
expect(spec.args).toEqual(['status'])
|
|
expect(spec.env.ELECTRON_RUN_AS_NODE).toBeUndefined()
|
|
expect((spec.env.PATH ?? '').split(delimiter)[0]).toBe('/some/bin')
|
|
})
|
|
|
|
// Windows cannot start a script. Both shapes a `codeburn` install takes there get a runner
|
|
// that can; a POSIX `codeburn` is a shebang script with an exec bit and still runs directly.
|
|
const windowsOnly = it.skipIf(process.platform !== 'win32')
|
|
|
|
windowsOnly('runs a .js CLI through Node rather than failing with EFTYPE', () => {
|
|
const spec = spawnSpecFor({ kind: 'external', bin: 'C:\\repo\\dist\\cli.js' }, ['status'])
|
|
expect(spec.bin).toBe(process.execPath)
|
|
expect(spec.args).toEqual(['C:\\repo\\dist\\cli.js', 'status'])
|
|
expect(spec.env.ELECTRON_RUN_AS_NODE).toBe('1')
|
|
})
|
|
|
|
windowsOnly('runs a .cmd shim through cmd.exe, as one verbatim command line', () => {
|
|
const spec = spawnSpecFor({ kind: 'external', bin: 'C:\\npm\\codeburn.cmd' }, ['status', '--period', 'today'])
|
|
expect(spec.bin.toLowerCase()).toMatch(/\\system32\\cmd\.exe$/)
|
|
expect(spec.args.slice(0, 3)).toEqual(['/d', '/s', '/c'])
|
|
expect(spec.verbatim).toBe(true)
|
|
expect(spec.env.ELECTRON_RUN_AS_NODE).toBeUndefined()
|
|
})
|
|
|
|
windowsOnly('leaves an .exe alone', () => {
|
|
const spec = spawnSpecFor({ kind: 'external', bin: 'C:\\tools\\codeburn.exe' }, ['status'])
|
|
expect(spec.bin).toBe('C:\\tools\\codeburn.exe')
|
|
expect(spec.args).toEqual(['status'])
|
|
expect(spec.verbatim).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
// Two parsers read this string: cmd itself, then the runtime that splits a command line back
|
|
// into argv. Nothing a renderer can put in an argument may reach either as syntax.
|
|
describe('cmd.exe command line', () => {
|
|
it('quotes every argument and neutralises the shell metacharacters', () => {
|
|
// The quotes are for the runtime that splits the line back into argv; the `^` are for
|
|
// cmd, which reads the line first and would otherwise act on what is inside them.
|
|
expect(escapeForCmd('status', false)).toBe('^"status^"')
|
|
expect(escapeForCmd('a&b', false)).toBe('^"a^&b^"')
|
|
expect(escapeForCmd('a|b', false)).toBe('^"a^|b^"')
|
|
expect(escapeForCmd('a>b<c', false)).toBe('^"a^>b^<c^"')
|
|
// A `.cmd` re-expands its own arguments, so those get a second round of escaping.
|
|
expect(escapeForCmd('a&b', true)).toBe('^^^"a^^^&b^^^"')
|
|
})
|
|
|
|
it('keeps an embedded quote from ending the argument', () => {
|
|
expect(escapeForCmd('say "hi"', false)).toBe('^"say^ \\^"hi\\^"^"')
|
|
// A trailing backslash run would otherwise escape the closing quote and swallow the
|
|
// argument after it.
|
|
expect(escapeForCmd('C:\\dir\\', false)).toBe('^"C:\\dir\\\\^"')
|
|
})
|
|
|
|
it('builds one /d /s /c line with the shim first', () => {
|
|
const args = cmdShimArgs('C:\\npm\\codeburn.cmd', ['export', '-o', 'C:\\out dir'])
|
|
expect(args.slice(0, 3)).toEqual(['/d', '/s', '/c'])
|
|
expect(args[3]).toBe('"^"C:\\npm\\codeburn.cmd^" ^^^"export^^^" ^^^"-o^^^" ^^^"C:\\out^^^ dir^^^""')
|
|
})
|
|
|
|
it.skipIf(process.platform !== 'win32')('runs a real .cmd shim end to end with its arguments intact', async () => {
|
|
// The shape a global npm install leaves on PATH: a batch file that re-expands %* into a
|
|
// node invocation, which is the second parse the double escaping above exists for.
|
|
const target = join(dir, 'echo-args.js')
|
|
writeFileSync(target, 'process.stdout.write(JSON.stringify({ args: process.argv.slice(2) }))\n')
|
|
const shim = join(dir, 'codeburn.cmd')
|
|
writeFileSync(shim, `@echo off\r\n"${process.execPath}" "${target}" %*\r\n`)
|
|
process.env.CODEBURN_BIN = shim
|
|
|
|
await expect(spawnCli(['status', '--model', 'a & b'])).resolves.toEqual({
|
|
args: ['status', '--model', 'a & b'],
|
|
})
|
|
})
|
|
|
|
it('never lets an argument close the line and start a command', () => {
|
|
const line = cmdShimArgs('C:\\npm\\codeburn.cmd', ['status" & calc.exe & "'])[3]!
|
|
// Every quote the argument carried is escaped and so is every &, so neither parser can
|
|
// read the tail as a second command.
|
|
expect(line).not.toMatch(/(^|[^^])& calc/)
|
|
expect(line).not.toMatch(/(^|[^^\\])" /)
|
|
expect(line).toContain('^^^&')
|
|
})
|
|
|
|
it('spawnCli runs the bundled entry end-to-end as Node', async () => {
|
|
delete process.env.CODEBURN_BIN
|
|
delete process.env.VITE_DEV_SERVER_URL
|
|
process.env.CODEBURN_PATH_DIRS = ''
|
|
process.env.CODEBURN_CLI_PATH_FILE = join(dir, 'no-persisted-path')
|
|
// process.execPath is the Node running vitest here; a real packaged app uses
|
|
// Electron's binary, but the spawn shape (execPath + entry + args + env) is
|
|
// identical, so this exercises the whole bundled path.
|
|
const entry = join(dir, 'bundled-cli.js')
|
|
writeFileSync(
|
|
entry,
|
|
'process.stdout.write(JSON.stringify({ ranAsNode: process.env.ELECTRON_RUN_AS_NODE === "1", firstArg: process.argv[2] }))\n',
|
|
)
|
|
process.env.CODEBURN_BUNDLED_CLI = entry
|
|
|
|
const result = (await spawnCli(['status'])) as { ranAsNode: boolean; firstArg: string }
|
|
expect(result).toEqual({ ranAsNode: true, firstArg: 'status' })
|
|
})
|
|
})
|
|
|
|
describe('spawnCli', () => {
|
|
it('resolves parsed JSON on success', async () => {
|
|
fakeBin('ok.js', 'process.stdout.write(JSON.stringify({ ok: 1 }))')
|
|
await expect(spawnCli(['status'])).resolves.toEqual({ ok: 1 })
|
|
})
|
|
|
|
it('rejects with kind "nonzero" on a non-zero exit', async () => {
|
|
fakeBin('fail.js', 'process.stderr.write("boom"); process.exit(2)')
|
|
await expect(spawnCli(['status'])).rejects.toMatchObject({ kind: 'nonzero' } satisfies Partial<CliError>)
|
|
})
|
|
|
|
it('rejects with kind "bad-json" on non-JSON stdout', async () => {
|
|
fakeBin('garbage.js', 'process.stdout.write("not json at all")')
|
|
await expect(spawnCli(['status'])).rejects.toMatchObject({ kind: 'bad-json' })
|
|
})
|
|
|
|
it('rejects with kind "timeout" when the binary hangs', async () => {
|
|
fakeBin('hang.js', 'setInterval(() => {}, 1000)')
|
|
await expect(spawnCli(['status'], { timeoutMs: 300 })).rejects.toMatchObject({ kind: 'timeout' })
|
|
})
|
|
|
|
it('rejects with kind "not-found" when no binary resolves', async () => {
|
|
delete process.env.CODEBURN_BIN
|
|
process.env.CODEBURN_PATH_DIRS = '' // force an empty search space
|
|
process.env.CODEBURN_CLI_PATH_FILE = join(dir, 'no-such-persisted-path')
|
|
try {
|
|
await expect(spawnCli(['status'])).rejects.toMatchObject({ kind: 'not-found' })
|
|
} finally {
|
|
delete process.env.CODEBURN_PATH_DIRS
|
|
delete process.env.CODEBURN_CLI_PATH_FILE
|
|
}
|
|
})
|
|
|
|
it('rejects with kind "too-large" and kills a binary that floods stdout', async () => {
|
|
fakeBin('flood.js', "const s='x'.repeat(1024*1024); for(let i=0;i<20;i++) process.stdout.write(s); setInterval(()=>{},1000)")
|
|
await expect(spawnCli(['status'])).rejects.toMatchObject({ kind: 'too-large' } satisfies Partial<CliError>)
|
|
})
|
|
})
|
|
|
|
describe('no-output watchdog (timeoutMs bounds SILENCE, not total runtime)', () => {
|
|
/** First stderr write is immediate after Node boot: that only removes the extra
|
|
* setInterval delay. The spawn-time silence timer still includes Node boot;
|
|
* the larger smoke window absorbs startup. Further ticks keep the process
|
|
* alive past `timeoutMs`. Not a production timeout change. */
|
|
function chattyBin(everyMs: number, extraTicks: number): void {
|
|
fakeBin(
|
|
'chatty.js',
|
|
`let n = 1;
|
|
process.stderr.write('CODEBURN_PROGRESS {"kind":"tick","provider":"claude","done":0,"total":${extraTicks + 1}}\\n');
|
|
const t = setInterval(() => {
|
|
process.stderr.write('CODEBURN_PROGRESS {"kind":"tick","provider":"claude","done":' + n + ',"total":${extraTicks + 1}}\\n');
|
|
if (++n > ${extraTicks}) { clearInterval(t); process.stdout.write(JSON.stringify({ ok: 1, ticks: n })); }
|
|
}, ${everyMs});`,
|
|
)
|
|
}
|
|
|
|
// Production already restarts the idle window on every stdout/stderr byte;
|
|
// this is not a product fix. The fixture used to wait one setInterval before
|
|
// the first byte, so a 600ms smoke window raced Node boot (independent 614ms
|
|
// fail). Immediate first stderr write removes only that delay — boot still
|
|
// counts. Extra ticks run ~4s under a 2s silence window, so a fixed
|
|
// total-runtime cap still fails this test. Not a production timeout change.
|
|
it('never kills a child that keeps producing output past the window', async () => {
|
|
chattyBin(400, 10)
|
|
await expect(spawnCli(['optimize'], { timeoutMs: 2_000 })).resolves.toEqual({ ok: 1, ticks: 11 })
|
|
})
|
|
|
|
it('kills a child that goes silent, measured from its LAST byte', async () => {
|
|
// One byte lands ~300ms in (plus node boot), well inside the 1s window, then
|
|
// silence. A fixed cap kills at 1s; only a window measured from the LAST byte
|
|
// waits past 1.3s. The gap is wide enough that node's boot cost cannot blur it.
|
|
fakeBin(
|
|
'talks-then-hangs.js',
|
|
`setTimeout(() => process.stderr.write('CODEBURN_PROGRESS {"kind":"keepalive"}\\n'), 300);
|
|
setInterval(() => {}, 1000);`,
|
|
)
|
|
const began = Date.now()
|
|
await expect(spawnCli(['optimize'], { timeoutMs: 1_000 })).rejects.toMatchObject({ kind: 'timeout' })
|
|
expect(Date.now() - began).toBeGreaterThanOrEqual(1_250)
|
|
})
|
|
|
|
it('keeps progress heartbeats out of the surfaced error message', async () => {
|
|
fakeBin(
|
|
'progress-then-fails.js',
|
|
`process.stderr.write('CODEBURN_PROGRESS {"kind":"tick","provider":"claude","done":1,"total":2}\\n');
|
|
process.stderr.write('permission denied\\n');
|
|
process.exit(2);`,
|
|
)
|
|
await expect(spawnCli(['status'])).rejects.toMatchObject({ kind: 'nonzero', message: 'permission denied' })
|
|
})
|
|
|
|
it('enables CODEBURN_PROGRESS on every read spawn so long parses heartbeat', async () => {
|
|
fakeBin('env-echo.js', 'process.stdout.write(JSON.stringify({ progress: process.env.CODEBURN_PROGRESS }))')
|
|
await expect(spawnCli(['act', 'report', '--json'])).resolves.toEqual({ progress: '1' })
|
|
})
|
|
})
|
|
|
|
describe('graceful kill (SIGTERM, then SIGKILL after the grace)', () => {
|
|
posixOnly('sends SIGTERM first and SIGKILLs a child that ignores it', async () => {
|
|
const signalFile = join(dir, 'signals')
|
|
const pidFile = join(dir, 'stubborn-pid')
|
|
fakeBin(
|
|
'ignores-sigterm.js',
|
|
`const fs = require('node:fs');
|
|
fs.writeFileSync(${JSON.stringify(pidFile)}, String(process.pid));
|
|
process.on('SIGTERM', () => fs.appendFileSync(${JSON.stringify(signalFile)}, 'TERM'));
|
|
process.stdout.write('ready');
|
|
setInterval(() => {}, 1000);`,
|
|
)
|
|
|
|
await expect(spawnCli(['status'], { timeoutMs: 1_500 })).rejects.toMatchObject({ kind: 'timeout' })
|
|
// SIGTERM arrives with the rejection; the child survives it and is SIGKILLed
|
|
// only after KILL_GRACE_MS (5s).
|
|
await waitFor(() => readMaybe(signalFile) === 'TERM')
|
|
const pid = Number(readMaybe(pidFile))
|
|
expect(Number.isInteger(pid)).toBe(true)
|
|
expect(() => process.kill(pid, 0)).not.toThrow() // still alive inside the grace
|
|
await waitFor(() => {
|
|
try { process.kill(pid, 0); return false } catch { return true }
|
|
}, 12_000)
|
|
}, 20_000)
|
|
|
|
posixOnly('keeps a child inside the SIGTERM grace reapable, so quit cannot orphan it', async () => {
|
|
// The grace timer dies with the app. A child that ignores SIGTERM must still
|
|
// be in the reap set when quit sweeps, or it survives the app that spawned it.
|
|
//
|
|
// The child emits one byte the instant it is ready (pidfile written, handler
|
|
// installed). Because the watchdog restarts on output, the window is measured
|
|
// from READINESS rather than from spawn — so node's cold boot, however slow
|
|
// the machine is, can never eat into it and make this flake.
|
|
const pidFile = join(dir, 'grace-pid')
|
|
fakeBin(
|
|
'ignores-sigterm-quit.js',
|
|
`require('node:fs').writeFileSync(${JSON.stringify(pidFile)}, String(process.pid));
|
|
process.on('SIGTERM', () => {});
|
|
process.stderr.write('CODEBURN_PROGRESS {"kind":"keepalive"}\\n');
|
|
setInterval(() => {}, 1000);`,
|
|
)
|
|
|
|
await expect(spawnCli(['status'], { timeoutMs: 1_500 })).rejects.toMatchObject({ kind: 'timeout' })
|
|
await waitFor(() => readMaybe(pidFile).length > 0)
|
|
const pid = Number(readMaybe(pidFile))
|
|
expect(() => process.kill(pid, 0)).not.toThrow() // alive, mid-grace
|
|
|
|
shutdownAll() // the quit sweep, landing inside the 5s grace
|
|
await waitFor(() => {
|
|
try { process.kill(pid, 0); return false } catch { return true }
|
|
}, 3_000)
|
|
})
|
|
|
|
posixOnly('lets a SIGTERM-handling child exit on its own without waiting for SIGKILL', async () => {
|
|
const cleanupFile = join(dir, 'cleanup')
|
|
fakeBin(
|
|
'handles-sigterm.js',
|
|
`const fs = require('node:fs');
|
|
process.on('SIGTERM', () => { fs.writeFileSync(${JSON.stringify(cleanupFile)}, 'released'); process.exit(0); });
|
|
process.stdout.write('ready');
|
|
setInterval(() => {}, 1000);`,
|
|
)
|
|
const began = Date.now()
|
|
await expect(spawnCli(['status'], { timeoutMs: 1_500 })).rejects.toMatchObject({ kind: 'timeout' })
|
|
await waitFor(() => readMaybe(cleanupFile) === 'released')
|
|
expect(Date.now() - began).toBeLessThan(5_000) // never waited out the grace
|
|
})
|
|
})
|
|
|
|
// These start real children and then wait for the OS to report them gone, which is the same
|
|
// reason the resident-serve block gets room: the assertions are about which process survives,
|
|
// never about how quickly a busy machine gets round to ending it.
|
|
describe('orphan serve reaping', { timeout: 30_000 }, () => {
|
|
/** A long-lived stand-in for an orphaned `codeburn serve --stdio` child. */
|
|
function orphanServe(): { pid: number; pidFile: string } {
|
|
const bin = join(dir, 'codeburn')
|
|
writeFileSync(bin, '#!/usr/bin/env node\nsetInterval(() => {}, 1000);\n', { mode: 0o755 })
|
|
chmodSync(bin, 0o755)
|
|
const child = spawn(process.execPath, [bin, 'serve', '--stdio'], { stdio: 'ignore' })
|
|
child.unref()
|
|
const pidFile = join(dir, 'serve.pid')
|
|
writeFileSync(pidFile, JSON.stringify({ pid: child.pid, cmd: [process.execPath, bin, 'serve', '--stdio'].join(' ') }))
|
|
return { pid: child.pid!, pidFile }
|
|
}
|
|
|
|
it('startServe records the resident pid so a later launch can find it', async () => {
|
|
fakeResidentBin()
|
|
const pidFile = join(dir, 'recorded.pid')
|
|
startServe(pidFile)
|
|
await waitFor(() => readMaybe(pidFile).length > 0)
|
|
const record = JSON.parse(readMaybe(pidFile)) as { pid: number; cmd: string }
|
|
expect(record.pid).toBeGreaterThan(1)
|
|
expect(record.cmd).toContain('serve --stdio')
|
|
})
|
|
|
|
it('kills a serve child orphaned by a previous run and clears the pidfile', async () => {
|
|
const { pid, pidFile } = orphanServe()
|
|
await waitFor(() => { try { process.kill(pid, 0); return true } catch { return false } })
|
|
|
|
reapOrphanServe(pidFile)
|
|
|
|
await waitFor(() => {
|
|
try { process.kill(pid, 0); return false } catch { return true }
|
|
})
|
|
expect(readMaybe(pidFile)).toBe('')
|
|
})
|
|
|
|
// A keyword sniff ("looks like a cli.js running serve") matches plenty of
|
|
// unrelated tools. Identity is the exact argv we recorded, nothing looser.
|
|
it.each([
|
|
['an unrelated tool', 'unrelated-tool', ['work']],
|
|
['a lookalike cli.js running serve', 'cli.js', ['serve', '--stdio']],
|
|
['a lookalike named codeburn', 'codeburn', ['serve', '--stdio']],
|
|
])('never signals a recycled pid belonging to %s', async (_label, name, args) => {
|
|
const bin = join(dir, name)
|
|
writeFileSync(bin, '#!/usr/bin/env node\nsetInterval(() => {}, 1000);\n', { mode: 0o755 })
|
|
chmodSync(bin, 0o755)
|
|
const bystander = spawn(process.execPath, [bin, ...args], { stdio: 'ignore' })
|
|
const pidFile = join(dir, `recycled-${name}.pid`)
|
|
// The pid is right; the recorded command belongs to the serve child that
|
|
// used to own it. Only an exact match may fire.
|
|
writeFileSync(pidFile, JSON.stringify({ pid: bystander.pid, cmd: '/opt/codeburn/dist/cli.js serve --stdio' }))
|
|
try {
|
|
reapOrphanServe(pidFile)
|
|
await new Promise(resolve => setTimeout(resolve, 200))
|
|
expect(() => process.kill(bystander.pid!, 0)).not.toThrow()
|
|
} finally {
|
|
bystander.kill('SIGKILL')
|
|
}
|
|
})
|
|
|
|
it('ignores a missing, unparseable, or incomplete pidfile', () => {
|
|
expect(() => reapOrphanServe(join(dir, 'absent.pid'))).not.toThrow()
|
|
const junk = join(dir, 'junk.pid')
|
|
writeFileSync(junk, 'not-a-pid')
|
|
expect(() => reapOrphanServe(junk)).not.toThrow()
|
|
const noCmd = join(dir, 'no-cmd.pid')
|
|
writeFileSync(noCmd, JSON.stringify({ pid: 999999 }))
|
|
expect(() => reapOrphanServe(noCmd)).not.toThrow()
|
|
})
|
|
})
|
|
|
|
describe('spawn PATH augmentation (GUI-launched apps have a minimal PATH)', () => {
|
|
it("prepends the resolved binary's own directory so its env-shebang finds node", async () => {
|
|
const bin = fakeBin('path-echo.js', 'process.stdout.write(JSON.stringify({ path: process.env.PATH }))')
|
|
const result = await spawnCli(['status']) as { path: string }
|
|
expect(result.path.split(delimiter)[0]).toBe(dirname(bin))
|
|
})
|
|
|
|
it('spawnEnvFor dedupes and keeps the original PATH entries', () => {
|
|
const env = spawnEnvFor('/some/tool/bin/codeburn')
|
|
const parts = (env.PATH ?? '').split(delimiter)
|
|
expect(parts[0]).toBe('/some/tool/bin')
|
|
expect(new Set(parts).size).toBe(parts.length)
|
|
for (const original of (process.env.PATH ?? '').split(delimiter).filter(Boolean)) {
|
|
expect(parts).toContain(original)
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('spawnCli coalescing (read-only)', () => {
|
|
it('shares one child between two concurrent identical calls', async () => {
|
|
const countFile = join(dir, 'spawns')
|
|
fakeBin('counter.js', `require('fs').appendFileSync(${JSON.stringify(countFile)},'x'); process.stdout.write(JSON.stringify({ok:1}))`)
|
|
const [a, b] = await Promise.all([spawnCli(['status']), spawnCli(['status'])])
|
|
expect(a).toEqual({ ok: 1 })
|
|
expect(b).toEqual({ ok: 1 })
|
|
expect(readFileSync(countFile, 'utf8')).toBe('x') // exactly one spawn
|
|
})
|
|
|
|
it('reflects an external config change on the next same-argv read', async () => {
|
|
const configFile = join(dir, 'external-config')
|
|
const countFile = join(dir, 'spawns')
|
|
writeFileSync(configFile, 'before')
|
|
fakeBin(
|
|
'external-config.js',
|
|
`const fs = require('node:fs'); fs.appendFileSync(${JSON.stringify(countFile)}, 'x'); process.stdout.write(JSON.stringify({ value: fs.readFileSync(${JSON.stringify(configFile)}, 'utf8') }))`,
|
|
)
|
|
|
|
await expect(spawnCli(['model-alias', '--list'])).resolves.toEqual({ value: 'before' })
|
|
writeFileSync(configFile, 'after')
|
|
await expect(spawnCli(['model-alias', '--list'])).resolves.toEqual({ value: 'after' })
|
|
expect(readFileSync(countFile, 'utf8')).toBe('xx')
|
|
})
|
|
|
|
it('never coalesces config-mutating action calls', async () => {
|
|
const countFile = join(dir, 'spawns')
|
|
fakeBin('action-counter.js', `require('fs').appendFileSync(${JSON.stringify(countFile)},'x'); process.stdout.write('done')`)
|
|
await Promise.all([spawnCliAction(['currency', 'EUR']), spawnCliAction(['currency', 'EUR'])])
|
|
expect(readFileSync(countFile, 'utf8')).toBe('xx') // two independent spawns
|
|
})
|
|
|
|
it('runs a fresh read after a config-mutating action', async () => {
|
|
const countFile = join(dir, 'spawns')
|
|
fakeBin('mixed.js', `require('fs').appendFileSync(${JSON.stringify(countFile)},'x'); process.stdout.write(JSON.stringify({ok:1}))`)
|
|
await spawnCli(['model-alias', '--list'])
|
|
await spawnCliAction(['model-alias', 'a', 'b'])
|
|
await spawnCli(['model-alias', '--list'])
|
|
expect(readFileSync(countFile, 'utf8')).toBe('xxx')
|
|
})
|
|
|
|
it('fences old in-flight reads across a mutation without deleting the new flight', async () => {
|
|
const configFile = join(dir, 'generation-config')
|
|
const startsFile = join(dir, 'generation-read-starts')
|
|
const releaseDir = join(dir, 'generation-release')
|
|
mkdirSync(releaseDir)
|
|
writeFileSync(configFile, 'old')
|
|
fakeBin(
|
|
'generation-fence.js',
|
|
`const fs = require('node:fs'); const path = require('node:path');
|
|
if (process.argv[3] === '--list') {
|
|
const value = fs.readFileSync(${JSON.stringify(configFile)}, 'utf8');
|
|
fs.appendFileSync(${JSON.stringify(startsFile)}, 'r');
|
|
const generation = fs.readFileSync(${JSON.stringify(startsFile)}, 'utf8').length;
|
|
const release = path.join(${JSON.stringify(releaseDir)}, String(generation));
|
|
const timer = setInterval(() => {
|
|
if (!fs.existsSync(release)) return;
|
|
clearInterval(timer);
|
|
process.stdout.write(JSON.stringify({ value, generation }));
|
|
}, 5);
|
|
} else {
|
|
fs.writeFileSync(${JSON.stringify(configFile)}, 'new');
|
|
process.stdout.write('updated');
|
|
}`,
|
|
)
|
|
|
|
const oldRead = spawnCli(['model-alias', '--list'])
|
|
await waitFor(() => readMaybe(startsFile) === 'r')
|
|
await expect(spawnCliAction(['model-alias', 'alias', 'model']))
|
|
.resolves.toMatchObject({ ok: true })
|
|
|
|
const newRead = spawnCli(['model-alias', '--list'])
|
|
await waitFor(() => readMaybe(startsFile) === 'rr')
|
|
writeFileSync(join(releaseDir, '1'), '')
|
|
await expect(oldRead).resolves.toEqual({ value: 'old', generation: 1 })
|
|
|
|
// Settling the superseded flight must not remove the current generation's
|
|
// entry: this identical call still shares read #2 instead of spawning #3.
|
|
const coalescedNewRead = spawnCli(['model-alias', '--list'])
|
|
await new Promise(resolve => setTimeout(resolve, 100))
|
|
expect(readMaybe(startsFile)).toBe('rr')
|
|
|
|
writeFileSync(join(releaseDir, '2'), '')
|
|
await expect(Promise.all([newRead, coalescedNewRead])).resolves.toEqual([
|
|
{ value: 'new', generation: 2 },
|
|
{ value: 'new', generation: 2 },
|
|
])
|
|
})
|
|
})
|
|
|
|
// Every test here starts several real node children, and the suite runs fifty files at once.
|
|
// The assertions are about which child answers, never about how quickly, so the wall clock
|
|
// gets room rather than the machine having to be fast enough for the default.
|
|
describe('resident serve single-flight', { timeout: 30_000 }, () => {
|
|
it('startServe is idempotent and creates only one resident child', async () => {
|
|
const files = fakeResidentBin()
|
|
startServe()
|
|
startServe()
|
|
|
|
const result = await spawnCli(['status', '--double-start'], { timeoutMs: 5_000 }) as { generation: number }
|
|
|
|
expect(result.generation).toBe(1)
|
|
expect(readMaybe(files.startsFile)).toBe('s')
|
|
expect(readMaybe(files.heavyFile)).toBe('h')
|
|
})
|
|
|
|
it('lazily starts a new resident after an unexpected death and one-shot fallback', async () => {
|
|
const startsFile = join(dir, 'serve-starts')
|
|
const oneShotsFile = join(dir, 'one-shot-reads')
|
|
fakeBin(
|
|
'dies-once-resident.js',
|
|
`const fs = require('node:fs'); const readline = require('node:readline');
|
|
const command = process.argv[2];
|
|
if (command === 'serve') {
|
|
fs.appendFileSync(${JSON.stringify(startsFile)}, 's');
|
|
const generation = fs.readFileSync(${JSON.stringify(startsFile)}, 'utf8').length;
|
|
const rl = readline.createInterface({ input: process.stdin });
|
|
rl.once('line', line => {
|
|
const request = JSON.parse(line);
|
|
if (generation === 1) process.exit(1);
|
|
process.stdout.write(JSON.stringify({ id: request.id, ok: true, output: JSON.stringify({ via: 'serve', generation }) }) + '\\n');
|
|
});
|
|
} else {
|
|
fs.appendFileSync(${JSON.stringify(oneShotsFile)}, 'o');
|
|
process.stdout.write(JSON.stringify({ via: 'spawn' }));
|
|
}`,
|
|
)
|
|
startServe()
|
|
|
|
await expect(spawnCli(['status', '--first'], { timeoutMs: 5_000 }))
|
|
.resolves.toEqual({ via: 'spawn' })
|
|
await expect(spawnCli(['models', '--second'], { timeoutMs: 5_000 }))
|
|
.resolves.toEqual({ via: 'serve', generation: 2 })
|
|
|
|
expect(readMaybe(startsFile)).toBe('ss')
|
|
expect(readMaybe(oneShotsFile)).toBe('o')
|
|
})
|
|
|
|
posixOnly('falls back instead of crashing when a live resident closes its stdin', async () => {
|
|
const stdinClosedFile = join(dir, 'resident-stdin-closed')
|
|
fakeBin(
|
|
'closes-stdin-resident.js',
|
|
`const fs = require('node:fs'); const readline = require('node:readline');
|
|
if (process.argv[2] === 'serve') {
|
|
const rl = readline.createInterface({ input: process.stdin });
|
|
rl.once('line', line => {
|
|
const request = JSON.parse(line);
|
|
process.stdout.write(JSON.stringify({ id: request.id, ok: true, output: JSON.stringify({ via: 'serve' }) }) + '\\n', () => {
|
|
rl.close();
|
|
fs.closeSync(0);
|
|
fs.writeFileSync(${JSON.stringify(stdinClosedFile)}, 'closed');
|
|
});
|
|
});
|
|
setInterval(() => {}, 1000);
|
|
} else {
|
|
process.stdout.write(JSON.stringify({ via: 'spawn' }));
|
|
}`,
|
|
)
|
|
startServe()
|
|
|
|
await expect(spawnCli(['status', '--warm'], { timeoutMs: 5_000 }))
|
|
.resolves.toEqual({ via: 'serve' })
|
|
await waitFor(() => readMaybe(stdinClosedFile) === 'closed')
|
|
await expect(spawnCli(['models', '--after-stdin-close'], { timeoutMs: 5_000 }))
|
|
.resolves.toEqual({ via: 'spawn' })
|
|
})
|
|
|
|
it('gives the first resident status request the power-user cold timeout floor', async () => {
|
|
fakeBin(
|
|
'slow-cold-resident.js',
|
|
`const readline = require('node:readline');
|
|
if (process.argv[2] === 'serve') {
|
|
const rl = readline.createInterface({ input: process.stdin });
|
|
rl.on('line', line => {
|
|
const request = JSON.parse(line);
|
|
setTimeout(() => process.stdout.write(JSON.stringify({ id: request.id, ok: true, output: JSON.stringify({ via: 'serve' }) }) + '\\n'), 80);
|
|
});
|
|
} else {
|
|
process.stdout.write(JSON.stringify({ via: 'spawn' }));
|
|
}`,
|
|
)
|
|
startServe()
|
|
|
|
await expect(spawnCli(['status', '--cold-floor'], { timeoutMs: 20 }))
|
|
.resolves.toEqual({ via: 'serve' })
|
|
})
|
|
|
|
it('starts a queued resident timeout only after the request ahead settles', async () => {
|
|
fakeBin(
|
|
'serial-resident.js',
|
|
`const readline = require('node:readline');
|
|
if (process.argv[2] === 'serve') {
|
|
const rl = readline.createInterface({ input: process.stdin });
|
|
(async () => {
|
|
for await (const line of rl) {
|
|
const request = JSON.parse(line);
|
|
if (request.args.includes('--slow')) await new Promise(resolve => setTimeout(resolve, 400));
|
|
process.stdout.write(JSON.stringify({ id: request.id, ok: true, output: JSON.stringify({ via: 'serve', args: request.args }) }) + '\\n');
|
|
}
|
|
})();
|
|
} else {
|
|
process.stdout.write(JSON.stringify({ via: 'spawn' }));
|
|
}`,
|
|
)
|
|
startServe()
|
|
await expect(spawnCli(['status', '--warm'], { timeoutMs: 5_000 }))
|
|
.resolves.toMatchObject({ via: 'serve' })
|
|
|
|
const slow = spawnCli(['sessions', '--slow'], { timeoutMs: 1_000 })
|
|
const queued = spawnCli(['models', '--queued'], { timeoutMs: 200 })
|
|
const [slowResult, queuedResult] = await Promise.all([slow, queued])
|
|
|
|
expect(slowResult).toMatchObject({ via: 'serve' })
|
|
expect(queuedResult).toMatchObject({ via: 'serve' })
|
|
})
|
|
|
|
it('uses the first real request as the only heavy execution, even before ready', async () => {
|
|
const files = fakeResidentBin()
|
|
startServe()
|
|
|
|
const result = await spawnCli(['status', '--format', 'menubar-json'], {
|
|
timeoutMs: 5_000,
|
|
extraEnv: { CODEBURN_PROGRESS: '1' },
|
|
}) as { via: string; generation: number }
|
|
|
|
expect(result).toMatchObject({ via: 'serve', generation: 1 })
|
|
expect(readMaybe(files.startsFile)).toBe('s')
|
|
expect(readMaybe(files.heavyFile)).toBe('h')
|
|
expect(readMaybe(files.oneShotsFile)).toBe('')
|
|
expect(readMaybe(files.serveEnvFile)).toBe('1')
|
|
})
|
|
|
|
// The Projects pane's unfiltered list is the heaviest read in the app, and it
|
|
// was the one panel query that could not ride the resident child: `report` was
|
|
// in neither routing table, so every open paid a node boot plus a full
|
|
// session-cache parse and held one of the two run slots while it did.
|
|
it('routes the JSON report through the resident child instead of a cold spawn', async () => {
|
|
const files = fakeResidentBin()
|
|
startServe()
|
|
|
|
const result = await spawnCli(['report', '--format', 'json', '--period', 'lifetime'], { timeoutMs: 5_000 }) as { via: string }
|
|
|
|
expect(result).toMatchObject({ via: 'serve' })
|
|
expect(readMaybe(files.oneShotsFile)).toBe('')
|
|
})
|
|
|
|
it('forwards serve progress frames through the read onStderr callback', async () => {
|
|
fakeResidentBin()
|
|
startServe()
|
|
const chunks: string[] = []
|
|
|
|
await spawnCli(['status'], {
|
|
timeoutMs: 5_000,
|
|
extraEnv: { CODEBURN_PROGRESS: '1' },
|
|
onStderr: chunk => { chunks.push(chunk) },
|
|
})
|
|
|
|
expect(chunks.join('')).toBe('CODEBURN_PROGRESS {"kind":"provider","provider":"claude","state":"start","generation":1}\n')
|
|
})
|
|
|
|
it('resets the resident watchdog on every progress frame of a long request', async () => {
|
|
// Same contract as a one-shot spawn: a warm serve request that keeps
|
|
// streaming progress is never killed, however long the parse takes. The
|
|
// first status request warms serve, so the second runs under the plain
|
|
// (non-cold-floor) window and can only survive by resetting it.
|
|
fakeBin(
|
|
'streaming-resident.js',
|
|
`const readline = require('node:readline');
|
|
if (process.argv[2] === 'serve') {
|
|
const rl = readline.createInterface({ input: process.stdin });
|
|
rl.on('line', line => {
|
|
const request = JSON.parse(line);
|
|
if (!request.args.includes('--stream')) {
|
|
process.stdout.write(JSON.stringify({ id: request.id, ok: true, output: JSON.stringify({ via: 'serve' }) }) + '\\n');
|
|
return;
|
|
}
|
|
let n = 0;
|
|
const t = setInterval(() => {
|
|
process.stdout.write(JSON.stringify({ id: request.id, progress: 'CODEBURN_PROGRESS {"kind":"tick","provider":"claude","done":' + n + ',"total":15}\\n' }) + '\\n');
|
|
if (++n >= 15) {
|
|
clearInterval(t);
|
|
process.stdout.write(JSON.stringify({ id: request.id, ok: true, output: JSON.stringify({ via: 'serve', ticks: n }) }) + '\\n');
|
|
}
|
|
}, 100);
|
|
});
|
|
} else {
|
|
process.stdout.write(JSON.stringify({ via: 'spawn' }));
|
|
}`,
|
|
)
|
|
startServe()
|
|
|
|
await expect(spawnCli(['status', '--warm'], { timeoutMs: 5_000 })).resolves.toEqual({ via: 'serve' })
|
|
// ~1.5s of streamed work under a 600ms silence window.
|
|
await expect(spawnCli(['optimize', '--stream'], { timeoutMs: 600 }))
|
|
.resolves.toEqual({ via: 'serve', ticks: 15 })
|
|
})
|
|
|
|
it('rejects and terminates a resident that emits an oversized valid JSON frame', async () => {
|
|
const startsFile = join(dir, 'oversized-frame-starts')
|
|
const oneShotsFile = join(dir, 'oversized-frame-one-shots')
|
|
fakeBin(
|
|
'oversized-frame-resident.js',
|
|
`const fs = require('node:fs'); const readline = require('node:readline');
|
|
if (process.argv[2] === 'serve') {
|
|
fs.appendFileSync(${JSON.stringify(startsFile)}, 's');
|
|
const generation = fs.readFileSync(${JSON.stringify(startsFile)}, 'utf8').length;
|
|
const rl = readline.createInterface({ input: process.stdin });
|
|
rl.once('line', line => {
|
|
const request = JSON.parse(line);
|
|
const output = generation === 1
|
|
? JSON.stringify({ value: 'x'.repeat(16 * 1024 * 1024 + 1024) })
|
|
: JSON.stringify({ generation });
|
|
process.stdout.write(JSON.stringify({ id: request.id, ok: true, output }) + '\\n');
|
|
});
|
|
setInterval(() => {}, 1000);
|
|
} else {
|
|
fs.appendFileSync(${JSON.stringify(oneShotsFile)}, 'o');
|
|
process.stdout.write('{}');
|
|
}`,
|
|
)
|
|
startServe()
|
|
|
|
await expect(spawnCli(['status', '--oversized-frame'], { timeoutMs: 5_000 }))
|
|
.rejects.toMatchObject({ kind: 'too-large' } satisfies Partial<CliError>)
|
|
await expect(spawnCli(['status', '--after-oversized-frame'], { timeoutMs: 5_000 }))
|
|
.resolves.toEqual({ generation: 2 })
|
|
expect(readMaybe(startsFile)).toBe('ss')
|
|
expect(readMaybe(oneShotsFile)).toBe('')
|
|
})
|
|
|
|
it('keeps serve enabled after more overflows than the resident death budget', async () => {
|
|
const startsFile = join(dir, 'overflow-budget-starts')
|
|
const oneShotsFile = join(dir, 'overflow-budget-one-shots')
|
|
fakeBin(
|
|
'always-oversized-resident.js',
|
|
`const fs = require('node:fs'); const readline = require('node:readline');
|
|
if (process.argv[2] === 'serve') {
|
|
fs.appendFileSync(${JSON.stringify(startsFile)}, 's');
|
|
const rl = readline.createInterface({ input: process.stdin });
|
|
rl.once('line', line => {
|
|
const request = JSON.parse(line);
|
|
const output = JSON.stringify({ value: 'x'.repeat(16 * 1024 * 1024 + 1024) });
|
|
process.stdout.write(JSON.stringify({ id: request.id, ok: true, output }) + '\\n');
|
|
});
|
|
setInterval(() => {}, 1000);
|
|
} else {
|
|
fs.appendFileSync(${JSON.stringify(oneShotsFile)}, 'o');
|
|
process.stdout.write('{}');
|
|
}`,
|
|
)
|
|
startServe()
|
|
|
|
for (let attempt = 0; attempt < 4; attempt += 1) {
|
|
await expect(spawnCli(['status', '--overflow', String(attempt)], { timeoutMs: 5_000 }))
|
|
.rejects.toMatchObject({ kind: 'too-large' } satisfies Partial<CliError>)
|
|
}
|
|
|
|
// An overflow kill is deliberate, so it never spends the unexpected-death
|
|
// budget: the fourth request still reaches a resident, not a one-shot.
|
|
expect(readMaybe(startsFile)).toBe('ssss')
|
|
expect(readMaybe(oneShotsFile)).toBe('')
|
|
})
|
|
|
|
it('rejects and terminates a resident whose protocol line never terminates', async () => {
|
|
const startsFile = join(dir, 'unterminated-line-starts')
|
|
const oneShotsFile = join(dir, 'unterminated-line-one-shots')
|
|
fakeBin(
|
|
'unterminated-line-resident.js',
|
|
`const fs = require('node:fs'); const readline = require('node:readline');
|
|
if (process.argv[2] === 'serve') {
|
|
fs.appendFileSync(${JSON.stringify(startsFile)}, 's');
|
|
const rl = readline.createInterface({ input: process.stdin });
|
|
rl.once('line', () => process.stdout.write('x'.repeat(16 * 1024 * 1024 + 1024)));
|
|
setInterval(() => {}, 1000);
|
|
} else {
|
|
fs.appendFileSync(${JSON.stringify(oneShotsFile)}, 'o');
|
|
process.stdout.write('{}');
|
|
}`,
|
|
)
|
|
startServe()
|
|
|
|
await expect(spawnCli(['status', '--unterminated-line'], { timeoutMs: 5_000 }))
|
|
.rejects.toMatchObject({ kind: 'too-large' } satisfies Partial<CliError>)
|
|
expect(readMaybe(startsFile)).toBe('s')
|
|
expect(readMaybe(oneShotsFile)).toBe('')
|
|
})
|
|
|
|
it('keeps requests with any non-progress env override on the one-shot path', async () => {
|
|
const files = fakeResidentBin()
|
|
startServe()
|
|
|
|
const result = await spawnCli(['status'], {
|
|
timeoutMs: 5_000,
|
|
extraEnv: { CODEBURN_PROGRESS: '1', CODEBURN_TEST_MODE: 'isolated' },
|
|
}) as { via: string }
|
|
|
|
expect(result.via).toBe('spawn')
|
|
expect(readMaybe(files.heavyFile)).toBe('')
|
|
expect(readMaybe(files.oneShotsFile)).toBe('o')
|
|
})
|
|
|
|
it('treats empty and undefined-only env overrides as serve-compatible', async () => {
|
|
const files = fakeResidentBin()
|
|
startServe()
|
|
|
|
const empty = await spawnCli(['status', '--empty-env'], {
|
|
timeoutMs: 5_000,
|
|
extraEnv: {},
|
|
}) as { via: string }
|
|
const undefinedOnly = await spawnCli(['models', '--undefined-env'], {
|
|
timeoutMs: 5_000,
|
|
extraEnv: { CODEBURN_PROGRESS: undefined },
|
|
}) as { via: string }
|
|
|
|
expect(empty.via).toBe('serve')
|
|
expect(undefinedOnly.via).toBe('serve')
|
|
expect(readMaybe(files.heavyFile)).toBe('hh')
|
|
expect(readMaybe(files.oneShotsFile)).toBe('')
|
|
})
|
|
|
|
it('restarts the resident child after a successful config mutation', async () => {
|
|
const files = fakeResidentBin()
|
|
startServe()
|
|
|
|
const before = await spawnCli(['status'], { timeoutMs: 5_000 }) as { generation: number }
|
|
const action = await spawnCliAction(['currency', 'EUR'], { timeoutMs: 5_000 })
|
|
const after = await spawnCli(['status'], { timeoutMs: 5_000 }) as { generation: number }
|
|
|
|
expect(action).toMatchObject({ ok: true, stdout: 'currency updated', code: 0 })
|
|
expect(before.generation).toBe(1)
|
|
expect(after.generation).toBe(2)
|
|
expect(readMaybe(files.startsFile)).toBe('ss')
|
|
expect(readMaybe(files.heavyFile)).toBe('hh')
|
|
expect(readMaybe(files.actionsFile)).toBe('a')
|
|
})
|
|
|
|
posixOnly('retires the outgoing resident on EOF, and signals it only if it ignores EOF', async () => {
|
|
// A settings mutation replaces a child that may hold the cache refresh lock
|
|
// and a held shard window. Closing stdin lets it publish that window and
|
|
// unlink its own lock on the way out; SIGTERM is the fallback for a child
|
|
// that does not end on EOF, not the first move.
|
|
const eofFile = join(dir, 'restart-eof')
|
|
const signalFile = join(dir, 'restart-signals')
|
|
const residentBin = (ignoreEof: boolean): string =>
|
|
`const fs = require('node:fs'); const readline = require('node:readline');
|
|
const command = process.argv[2];
|
|
if (command === 'serve') {
|
|
process.on('SIGTERM', () => { fs.appendFileSync(${JSON.stringify(signalFile)}, 'TERM'); process.exit(0); });
|
|
const rl = readline.createInterface({ input: process.stdin });
|
|
if (${ignoreEof}) setInterval(() => {}, 1000);
|
|
else rl.on('close', () => { fs.appendFileSync(${JSON.stringify(eofFile)}, 'EOF'); process.exit(0); });
|
|
rl.on('line', line => {
|
|
const request = JSON.parse(line);
|
|
process.stdout.write(JSON.stringify({ id: request.id, ok: true, output: JSON.stringify({ via: 'serve' }) }) + '\\n');
|
|
});
|
|
} else {
|
|
process.stdout.write('currency updated');
|
|
}`
|
|
|
|
fakeBin('eof-aware-resident.js', residentBin(false))
|
|
startServe()
|
|
await expect(spawnCli(['status'], { timeoutMs: 5_000 })).resolves.toEqual({ via: 'serve' })
|
|
await expect(spawnCliAction(['currency', 'EUR'], { timeoutMs: 5_000 })).resolves.toMatchObject({ ok: true })
|
|
await waitFor(() => readMaybe(eofFile) === 'EOF')
|
|
expect(readMaybe(signalFile)).toBe('')
|
|
|
|
killAll()
|
|
fakeBin('eof-aware-resident.js', residentBin(true))
|
|
startServe()
|
|
await expect(spawnCli(['status'], { timeoutMs: 5_000 })).resolves.toEqual({ via: 'serve' })
|
|
await expect(spawnCliAction(['currency', 'EUR'], { timeoutMs: 5_000 })).resolves.toMatchObject({ ok: true })
|
|
await waitFor(() => readMaybe(signalFile) === 'TERM', 10_000)
|
|
})
|
|
|
|
it('preserves the unexpected-death budget across mutation restarts', async () => {
|
|
const startsFile = join(dir, 'serve-starts')
|
|
const oneShotsFile = join(dir, 'one-shot-reads')
|
|
fakeBin(
|
|
'crashing-resident.js',
|
|
`const fs = require('node:fs'); const readline = require('node:readline');
|
|
const command = process.argv[2];
|
|
if (command === 'serve') {
|
|
fs.appendFileSync(${JSON.stringify(startsFile)}, 's');
|
|
const rl = readline.createInterface({ input: process.stdin });
|
|
rl.once('line', () => process.exit(1));
|
|
} else if (command === 'currency') {
|
|
process.stdout.write('currency updated');
|
|
} else {
|
|
fs.appendFileSync(${JSON.stringify(oneShotsFile)}, 'o');
|
|
process.stdout.write(JSON.stringify({ via: 'spawn' }));
|
|
}`,
|
|
)
|
|
startServe()
|
|
|
|
for (let attempt = 0; attempt < 3; attempt += 1) {
|
|
await expect(spawnCli(['status', '--attempt', String(attempt)], { timeoutMs: 5_000 }))
|
|
.resolves.toEqual({ via: 'spawn' })
|
|
await expect(spawnCliAction(['currency', attempt % 2 === 0 ? 'EUR' : 'USD'], { timeoutMs: 5_000 }))
|
|
.resolves.toMatchObject({ ok: true })
|
|
}
|
|
|
|
// A mutation may replace a healthy child, but it must not erase real crash
|
|
// history and resurrect serve after the third unexpected death.
|
|
expect(readMaybe(startsFile)).toBe('sss')
|
|
await expect(spawnCli(['status', '--after-budget'], { timeoutMs: 5_000 }))
|
|
.resolves.toEqual({ via: 'spawn' })
|
|
expect(readMaybe(startsFile)).toBe('sss')
|
|
expect(readMaybe(oneShotsFile)).toBe('oooo')
|
|
})
|
|
|
|
it('stops lazy crash recovery after three consecutive resident deaths', async () => {
|
|
const startsFile = join(dir, 'serve-starts')
|
|
const oneShotsFile = join(dir, 'one-shot-reads')
|
|
fakeBin(
|
|
'always-crashing-resident.js',
|
|
`const fs = require('node:fs'); const readline = require('node:readline');
|
|
if (process.argv[2] === 'serve') {
|
|
fs.appendFileSync(${JSON.stringify(startsFile)}, 's');
|
|
const rl = readline.createInterface({ input: process.stdin });
|
|
rl.once('line', () => process.exit(1));
|
|
} else {
|
|
fs.appendFileSync(${JSON.stringify(oneShotsFile)}, 'o');
|
|
process.stdout.write(JSON.stringify({ via: 'spawn' }));
|
|
}`,
|
|
)
|
|
startServe()
|
|
|
|
for (let attempt = 0; attempt < 4; attempt += 1) {
|
|
await expect(spawnCli(['status', '--lazy-crash', String(attempt)], { timeoutMs: 5_000 }))
|
|
.resolves.toEqual({ via: 'spawn' })
|
|
}
|
|
|
|
expect(readMaybe(startsFile)).toBe('sss')
|
|
expect(readMaybe(oneShotsFile)).toBe('oooo')
|
|
})
|
|
|
|
it('does not spawn a one-shot fallback after killAll destroys serve', async () => {
|
|
const requestSeenFile = join(dir, 'request-seen')
|
|
const oneShotsFile = join(dir, 'one-shot-reads')
|
|
fakeBin(
|
|
'shutdown-resident.js',
|
|
`const fs = require('node:fs'); const readline = require('node:readline');
|
|
if (process.argv[2] === 'serve') {
|
|
const rl = readline.createInterface({ input: process.stdin });
|
|
rl.once('line', () => { fs.writeFileSync(${JSON.stringify(requestSeenFile)}, '1'); });
|
|
} else {
|
|
fs.appendFileSync(${JSON.stringify(oneShotsFile)}, 'o');
|
|
process.stdout.write('{}');
|
|
}`,
|
|
)
|
|
startServe()
|
|
const pending = spawnCli(['status', '--shutdown'], { timeoutMs: 60_000 })
|
|
for (let attempt = 0; attempt < 400 && !readMaybe(requestSeenFile); attempt += 1) {
|
|
await new Promise(resolve => setTimeout(resolve, 5))
|
|
}
|
|
const requestSeen = readMaybe(requestSeenFile)
|
|
killAll()
|
|
|
|
expect(requestSeen).toBe('1')
|
|
await expect(pending).rejects.toMatchObject({ kind: 'nonzero' })
|
|
await new Promise(resolve => setTimeout(resolve, 25))
|
|
expect(readMaybe(oneShotsFile)).toBe('')
|
|
})
|
|
|
|
it('gracefully releases the resident lock, then force-reaps its process tree', async () => {
|
|
if (process.platform === 'win32') return
|
|
const pidFile = join(dir, 'resident-shutdown-pid')
|
|
const grandchildPidFile = join(dir, 'provider-child-pid')
|
|
const termFile = join(dir, 'resident-shutdown-term')
|
|
const grandchildTermFile = join(dir, 'provider-child-term')
|
|
const hydrationLock = join(dir, 'hydrating.lock')
|
|
const grandchildBody = `
|
|
const fs = require('node:fs');
|
|
fs.writeFileSync(${JSON.stringify(grandchildPidFile)}, String(process.pid));
|
|
process.on('SIGTERM', () => fs.appendFileSync(${JSON.stringify(grandchildTermFile)}, 'TERM'));
|
|
setInterval(() => {}, 1000);
|
|
`
|
|
fakeBin(
|
|
'stubborn-resident-shutdown.js',
|
|
`const fs = require('node:fs'); const readline = require('node:readline'); const { spawn } = require('node:child_process');
|
|
if (process.argv[2] === 'serve') {
|
|
fs.writeFileSync(${JSON.stringify(pidFile)}, String(process.pid));
|
|
fs.writeFileSync(${JSON.stringify(hydrationLock)}, 'owned');
|
|
spawn(process.execPath, ['-e', ${JSON.stringify(grandchildBody)}], { stdio: 'ignore' });
|
|
process.on('SIGTERM', () => {
|
|
fs.writeFileSync(${JSON.stringify(termFile)}, 'TERM');
|
|
try { fs.unlinkSync(${JSON.stringify(hydrationLock)}); } catch {}
|
|
setTimeout(() => process.exit(0), 50);
|
|
});
|
|
readline.createInterface({ input: process.stdin });
|
|
setInterval(() => {}, 1000);
|
|
} else { process.stdout.write('{}'); }`,
|
|
)
|
|
startServe()
|
|
await waitFor(() => readMaybe(pidFile).length > 0 && readMaybe(grandchildPidFile).length > 0)
|
|
const pids = [Number(readMaybe(pidFile)), Number(readMaybe(grandchildPidFile))]
|
|
|
|
try {
|
|
const startedAt = performance.now()
|
|
await shutdownAll()
|
|
const elapsedMs = performance.now() - startedAt
|
|
|
|
expect(elapsedMs).toBeLessThan(1_500)
|
|
expect(readMaybe(termFile)).toBe('TERM')
|
|
expect(readMaybe(grandchildTermFile)).toBe('TERM')
|
|
expect(readMaybe(hydrationLock)).toBe('')
|
|
for (const pid of pids) expect(() => process.kill(pid, 0)).toThrow()
|
|
} finally {
|
|
for (const pid of pids) {
|
|
try { process.kill(pid, 'SIGKILL') } catch { /* already gone */ }
|
|
}
|
|
}
|
|
})
|
|
|
|
it('keeps the warm resident child after a successful export', async () => {
|
|
const files = fakeResidentBin()
|
|
startServe()
|
|
|
|
const before = await spawnCli(['status'], { timeoutMs: 5_000 }) as { generation: number }
|
|
const action = await spawnCliAction(['export', '-f', 'json', '-o', join(dir, 'usage.json')], { timeoutMs: 5_000 })
|
|
// A different panel query proves which resident generation handled the
|
|
// next served read without relying on same-request coalescing.
|
|
const after = await spawnCli(['models', '--format', 'json'], { timeoutMs: 5_000 }) as { generation: number }
|
|
|
|
expect(action.ok).toBe(true)
|
|
expect(before.generation).toBe(1)
|
|
expect(after.generation).toBe(1)
|
|
expect(readMaybe(files.startsFile)).toBe('s')
|
|
expect(readMaybe(files.heavyFile)).toBe('hh')
|
|
})
|
|
})
|
|
|
|
/** A resident that reports how it was retired, so a retire is observable without
|
|
* probing pids (a signalled child is briefly a zombie, which answers
|
|
* `kill(pid, 0)`). It goes away on stdin EOF — like `serve --stdio`, which
|
|
* publishes its coalesced shard window there — and records any SIGTERM it
|
|
* received separately, so a test can pin that none was sent. `--slow` delays one
|
|
* answer; `lateExitMs` holds the exit open past the replacement's start;
|
|
* `ignoreEof` models a child that does not end on EOF and must still be reaped. */
|
|
function fakeRetiringBin(lateExitMs = 0, ignoreEof = false): {
|
|
startsFile: string
|
|
deathsFile: string
|
|
signalsFile: string
|
|
oneShotsFile: string
|
|
} {
|
|
const startsFile = join(dir, 'retire-starts')
|
|
const deathsFile = join(dir, 'retire-deaths')
|
|
const signalsFile = join(dir, 'retire-signals')
|
|
const oneShotsFile = join(dir, 'retire-one-shots')
|
|
fakeBin(
|
|
'retiring-resident.js',
|
|
`const fs = require('node:fs'); const readline = require('node:readline');
|
|
const command = process.argv[2];
|
|
if (command === 'serve') {
|
|
fs.appendFileSync(${JSON.stringify(startsFile)}, 's');
|
|
const generation = fs.readFileSync(${JSON.stringify(startsFile)}, 'utf8').length;
|
|
const retire = () => {
|
|
fs.appendFileSync(${JSON.stringify(deathsFile)}, 'x');
|
|
setTimeout(() => process.exit(0), ${lateExitMs});
|
|
};
|
|
process.on('SIGTERM', () => { fs.appendFileSync(${JSON.stringify(signalsFile)}, 'T'); retire(); });
|
|
const rl = readline.createInterface({ input: process.stdin });
|
|
if (${ignoreEof}) setInterval(() => {}, 1000); else rl.on('close', retire);
|
|
rl.on('line', line => {
|
|
const request = JSON.parse(line);
|
|
const answer = () => process.stdout.write(JSON.stringify({
|
|
id: request.id,
|
|
ok: true,
|
|
output: JSON.stringify({ via: 'serve', generation }),
|
|
usage: generation === 1 ? { cpuSec: 12, rssMb: 1234 } : { cpuSec: 1, rssMb: 10 },
|
|
}) + '\\n');
|
|
if (request.args.includes('--slow')) setTimeout(answer, 300); else answer();
|
|
});
|
|
} else if (command === 'currency') {
|
|
process.stdout.write('currency updated');
|
|
} else {
|
|
fs.appendFileSync(${JSON.stringify(oneShotsFile)}, 'o');
|
|
process.stdout.write(JSON.stringify({ via: 'spawn' }));
|
|
}`,
|
|
)
|
|
return { startsFile, deathsFile, signalsFile, oneShotsFile }
|
|
}
|
|
|
|
describe('resident serve idle retire', { timeout: 30_000 }, () => {
|
|
beforeEach(() => { process.env.CODEBURN_SERVE_IDLE_MS = '50' })
|
|
|
|
posixOnly('retires an idle child and serves the next request from a new resident', async () => {
|
|
const files = fakeRetiringBin()
|
|
startServe()
|
|
|
|
await expect(spawnCli(['status', '--warm'], { timeoutMs: 5_000 })).resolves.toEqual({ via: 'serve', generation: 1 })
|
|
await waitFor(() => readMaybe(files.deathsFile) === 'x')
|
|
|
|
await expect(spawnCli(['status', '--after-retire'], { timeoutMs: 5_000 })).resolves.toEqual({ via: 'serve', generation: 2 })
|
|
expect(readMaybe(files.startsFile)).toBe('ss')
|
|
expect(readMaybe(files.oneShotsFile)).toBe('')
|
|
})
|
|
|
|
posixOnly('never spends the unexpected-death budget, however often it retires', async () => {
|
|
const files = fakeRetiringBin()
|
|
startServe()
|
|
|
|
for (let round = 1; round <= 5; round += 1) {
|
|
await expect(spawnCli(['status', '--round', String(round)], { timeoutMs: 5_000 }))
|
|
.resolves.toEqual({ via: 'serve', generation: round })
|
|
await waitFor(() => readMaybe(files.deathsFile).length === round)
|
|
}
|
|
|
|
// Three unexpected deaths disable serve for the app run; retires are
|
|
// deliberate, so the sixth request still reaches a resident.
|
|
await expect(spawnCli(['status', '--sixth'], { timeoutMs: 5_000 })).resolves.toEqual({ via: 'serve', generation: 6 })
|
|
expect(readMaybe(files.oneShotsFile)).toBe('')
|
|
})
|
|
|
|
posixOnly('never retires a child with a request in flight or queued behind one', async () => {
|
|
const files = fakeRetiringBin()
|
|
startServe()
|
|
|
|
// The slow answer takes 6 idle windows; the queued request takes its turn in
|
|
// the microtask right after it, which is the one moment a timer could slip in.
|
|
const slow = spawnCli(['status', '--slow'], { timeoutMs: 5_000 })
|
|
const queued = spawnCli(['models', '--queued'], { timeoutMs: 5_000 })
|
|
|
|
await expect(slow).resolves.toEqual({ via: 'serve', generation: 1 })
|
|
await expect(queued).resolves.toEqual({ via: 'serve', generation: 1 })
|
|
expect(readMaybe(files.deathsFile)).toBe('')
|
|
expect(readMaybe(files.oneShotsFile)).toBe('')
|
|
|
|
await waitFor(() => readMaybe(files.deathsFile) === 'x')
|
|
expect(readMaybe(files.startsFile)).toBe('s')
|
|
})
|
|
|
|
posixOnly('keeps the serve usage peak across a retire', async () => {
|
|
const files = fakeRetiringBin()
|
|
startServe()
|
|
|
|
await spawnCli(['status', '--peak'], { timeoutMs: 5_000 })
|
|
await waitFor(() => readMaybe(files.deathsFile) === 'x')
|
|
await spawnCli(['status', '--after-peak'], { timeoutMs: 5_000 })
|
|
|
|
// app_close reports what the resident cost, so a replacement's counters
|
|
// restarting at zero must not erase the peak.
|
|
expect(serveUsage()).toEqual({ cpuSec: 12, rssMb: 1234 })
|
|
})
|
|
|
|
posixOnly('keeps the child resident for good when the idle window is 0', async () => {
|
|
process.env.CODEBURN_SERVE_IDLE_MS = '0'
|
|
const files = fakeRetiringBin()
|
|
startServe()
|
|
|
|
await spawnCli(['status', '--no-retire'], { timeoutMs: 5_000 })
|
|
await new Promise(resolve => setTimeout(resolve, 250))
|
|
|
|
expect(readMaybe(files.deathsFile)).toBe('')
|
|
await expect(spawnCli(['status', '--still-warm'], { timeoutMs: 5_000 })).resolves.toEqual({ via: 'serve', generation: 1 })
|
|
expect(readMaybe(files.startsFile)).toBe('s')
|
|
})
|
|
|
|
posixOnly('ignores a retired child that exits only after its replacement is serving', async () => {
|
|
const files = fakeRetiringBin(200)
|
|
startServe()
|
|
|
|
await spawnCli(['status', '--warm'], { timeoutMs: 5_000 })
|
|
await waitFor(() => readMaybe(files.deathsFile) === 'x')
|
|
|
|
// The retired child's exit and stdout EOF land while the replacement is
|
|
// mid-request: they must reject nothing and requeue nothing.
|
|
await expect(spawnCli(['status', '--slow'], { timeoutMs: 5_000 })).resolves.toEqual({ via: 'serve', generation: 2 })
|
|
expect(readMaybe(files.startsFile)).toBe('ss')
|
|
expect(readMaybe(files.oneShotsFile)).toBe('')
|
|
})
|
|
|
|
posixOnly('closes stdin first and never signals a child that exits on EOF', async () => {
|
|
// EOF is what makes `serve --stdio` publish its coalesced shard window on
|
|
// the way out. SIGTERM is caught only to unlink the cache lock and is then
|
|
// re-raised, so signalling here would kill the flush.
|
|
const files = fakeRetiringBin()
|
|
startServe()
|
|
|
|
await expect(spawnCli(['status', '--warm'], { timeoutMs: 5_000 })).resolves.toEqual({ via: 'serve', generation: 1 })
|
|
await waitFor(() => readMaybe(files.deathsFile) === 'x')
|
|
expect(readMaybe(files.signalsFile)).toBe('')
|
|
|
|
// Still exactly one resident per generation, and no one-shot fallback.
|
|
await expect(spawnCli(['status', '--after-retire'], { timeoutMs: 5_000 })).resolves.toEqual({ via: 'serve', generation: 2 })
|
|
expect(readMaybe(files.startsFile)).toBe('ss')
|
|
expect(readMaybe(files.oneShotsFile)).toBe('')
|
|
expect(readMaybe(files.signalsFile)).toBe('')
|
|
})
|
|
|
|
posixOnly('signals a retired child that ignores EOF, but only after the grace', async () => {
|
|
const files = fakeRetiringBin(0, true)
|
|
startServe()
|
|
|
|
await expect(spawnCli(['status', '--warm'], { timeoutMs: 5_000 })).resolves.toEqual({ via: 'serve', generation: 1 })
|
|
// Inside the grace: stdin is closed, nothing has been signalled yet.
|
|
await new Promise(resolve => setTimeout(resolve, 400))
|
|
expect(readMaybe(files.signalsFile)).toBe('')
|
|
|
|
await waitFor(() => readMaybe(files.signalsFile) === 'T', 10_000)
|
|
expect(readMaybe(files.deathsFile)).toBe('x')
|
|
})
|
|
|
|
posixOnly('leaves one child behind when a mutation restart lands in the idle window', async () => {
|
|
const files = fakeRetiringBin()
|
|
startServe()
|
|
|
|
await expect(spawnCli(['status', '--before'], { timeoutMs: 5_000 })).resolves.toEqual({ via: 'serve', generation: 1 })
|
|
await expect(spawnCliAction(['currency', 'EUR'], { timeoutMs: 5_000 })).resolves.toMatchObject({ ok: true })
|
|
// Issued without yielding to the timer queue: whichever of the retire and
|
|
// the restart ran first, exactly one child is alive to answer this.
|
|
await expect(spawnCli(['status', '--after'], { timeoutMs: 5_000 })).resolves.toEqual({ via: 'serve', generation: 2 })
|
|
|
|
expect(readMaybe(files.startsFile)).toBe('ss')
|
|
expect(readMaybe(files.oneShotsFile)).toBe('')
|
|
})
|
|
})
|
|
|
|
describe('killAll', () => {
|
|
it('reaps an in-flight child so its promise settles', async () => {
|
|
fakeBin('hang-kill.js', 'setInterval(() => {}, 1000)')
|
|
const pending = spawnCli(['status'], { timeoutMs: 60_000 })
|
|
// Let the child spawn before reaping.
|
|
await new Promise(resolve => setTimeout(resolve, 50))
|
|
killAll()
|
|
await expect(pending).rejects.toMatchObject({ kind: 'nonzero' })
|
|
})
|
|
|
|
it('terminal shutdown rejects new read and action races without spawning', async () => {
|
|
const startsFile = join(dir, 'starts')
|
|
fakeBin(
|
|
'shutdown-guard.js',
|
|
`require('node:fs').appendFileSync(${JSON.stringify(startsFile)}, 'x'); process.stdout.write('{}')`,
|
|
)
|
|
|
|
shutdownAll()
|
|
startServe()
|
|
|
|
await expect(spawnCli(['status', '--after-shutdown']))
|
|
.rejects.toMatchObject({ kind: 'nonzero' })
|
|
await expect(spawnCliAction(['currency', 'EUR']))
|
|
.resolves.toMatchObject({ ok: false, code: null })
|
|
expect(readMaybe(startsFile)).toBe('')
|
|
})
|
|
|
|
it('terminal shutdown cancels read and action slots admitted before their spawn microtask', async () => {
|
|
const startsFile = join(dir, 'starts-after-admission')
|
|
fakeBin(
|
|
'shutdown-after-admission.js',
|
|
`require('node:fs').appendFileSync(${JSON.stringify(startsFile)}, process.argv[2] + '\\n'); if (process.argv[2] === 'status') process.stdout.write('{}'); else process.stdout.write('updated')`,
|
|
)
|
|
|
|
// Both calls synchronously acquire the two free scheduler slots. Their
|
|
// actual spawn resumes in a microtask, which is exactly the before-quit race.
|
|
const read = spawnCli(['status', '--admitted'])
|
|
const action = spawnCliAction(['currency', 'EUR'])
|
|
shutdownAll()
|
|
|
|
await Promise.all([
|
|
expect(read).rejects.toMatchObject({ kind: 'nonzero' }),
|
|
expect(action).resolves.toMatchObject({ ok: false, code: null }),
|
|
])
|
|
expect(readMaybe(startsFile)).toBe('')
|
|
})
|
|
})
|
|
|
|
describe('spawnCli concurrency scheduler', () => {
|
|
// A fake CLI that records each spawn (by subcommand) and then blocks until a
|
|
// release file named after that subcommand appears, so the test controls
|
|
// exactly when each child exits and can observe how many run at once.
|
|
function schedulerBin(startedFile: string, releaseDir: string): void {
|
|
fakeBin(
|
|
'sched.js',
|
|
`const fs = require('fs'); const path = require('path');
|
|
const cmd = process.argv[2];
|
|
fs.appendFileSync(${JSON.stringify(startedFile)}, cmd + '\\n');
|
|
const rel = path.join(${JSON.stringify(releaseDir)}, cmd);
|
|
const t = setInterval(() => {
|
|
if (fs.existsSync(rel)) { clearInterval(t); process.stdout.write('{}'); process.exit(0); }
|
|
}, 5);`,
|
|
)
|
|
}
|
|
function startedList(startedFile: string): string[] {
|
|
try { return readFileSync(startedFile, 'utf8').split('\n').filter(Boolean) } catch { return [] }
|
|
}
|
|
function release(releaseDir: string, cmd: string): void { writeFileSync(join(releaseDir, cmd), '') }
|
|
async function waitUntil(cond: () => boolean, timeoutMs = 3000): Promise<void> {
|
|
const deadline = Date.now() + timeoutMs
|
|
while (!cond()) {
|
|
if (Date.now() > deadline) throw new Error('waitUntil timed out')
|
|
await new Promise(r => setTimeout(r, 10))
|
|
}
|
|
}
|
|
const delay = (ms: number) => new Promise(r => setTimeout(r, ms))
|
|
|
|
// Reset scheduler state so a leaked running slot can never starve the next test.
|
|
afterEach(() => { killAll() })
|
|
|
|
it('runs at most two children at once; a third waits for a freed slot', async () => {
|
|
const startedFile = join(dir, 'started')
|
|
const releaseDir = join(dir, 'release'); mkdirSync(releaseDir)
|
|
schedulerBin(startedFile, releaseDir)
|
|
|
|
const p1 = spawnCli(['status'])
|
|
const p2 = spawnCli(['models'])
|
|
const p3 = spawnCli(['sessions'])
|
|
await waitUntil(() => startedList(startedFile).length === 2)
|
|
await delay(100) // the cap must keep the third from sneaking in
|
|
expect(startedList(startedFile).sort()).toEqual(['models', 'status'])
|
|
|
|
release(releaseDir, 'status') // free one slot
|
|
await waitUntil(() => startedList(startedFile).includes('sessions'))
|
|
|
|
release(releaseDir, 'models'); release(releaseDir, 'sessions')
|
|
await Promise.all([p1, p2, p3])
|
|
})
|
|
|
|
it('lets a later interactive spawn preempt an earlier queued background one', async () => {
|
|
const startedFile = join(dir, 'started')
|
|
const releaseDir = join(dir, 'release'); mkdirSync(releaseDir)
|
|
schedulerBin(startedFile, releaseDir)
|
|
|
|
// Fill both slots so the next two calls must queue.
|
|
const p1 = spawnCli(['fill1'], { priority: 'background' })
|
|
const p2 = spawnCli(['fill2'], { priority: 'background' })
|
|
await waitUntil(() => startedList(startedFile).length === 2)
|
|
|
|
// Queue a background first, then an interactive.
|
|
const pbg = spawnCli(['bg'], { priority: 'background' })
|
|
const pint = spawnCli(['inter'], { priority: 'interactive' })
|
|
await delay(50)
|
|
expect(startedList(startedFile)).not.toContain('bg')
|
|
expect(startedList(startedFile)).not.toContain('inter')
|
|
|
|
// Free exactly one slot: the interactive must take it despite queueing later.
|
|
release(releaseDir, 'fill1')
|
|
await waitUntil(() => startedList(startedFile).length === 3)
|
|
expect(startedList(startedFile)).toContain('inter')
|
|
expect(startedList(startedFile)).not.toContain('bg')
|
|
|
|
release(releaseDir, 'fill2'); release(releaseDir, 'inter'); release(releaseDir, 'bg')
|
|
await Promise.all([p1, p2, pbg, pint])
|
|
})
|
|
|
|
it('does not spend a slot on a coalesced (same-argv) call', async () => {
|
|
const startedFile = join(dir, 'started')
|
|
const releaseDir = join(dir, 'release'); mkdirSync(releaseDir)
|
|
schedulerBin(startedFile, releaseDir)
|
|
|
|
const p1 = spawnCli(['status'])
|
|
const p2 = spawnCli(['models'])
|
|
await waitUntil(() => startedList(startedFile).length === 2)
|
|
|
|
const p1b = spawnCli(['status']) // coalesces onto p1's child — no new slot
|
|
const p3 = spawnCli(['sessions']) // genuinely new — queued behind the cap
|
|
await delay(100)
|
|
expect(startedList(startedFile).length).toBe(2) // still just the two originals
|
|
|
|
release(releaseDir, 'status') // frees the slot the coalesced pair shared
|
|
await waitUntil(() => startedList(startedFile).includes('sessions'))
|
|
|
|
release(releaseDir, 'models'); release(releaseDir, 'sessions')
|
|
const [a, b] = await Promise.all([p1, p1b])
|
|
expect(a).toEqual(b) // one child served both callers
|
|
await Promise.all([p2, p3])
|
|
})
|
|
|
|
it('cancels a queued spawn on killAll instead of letting it spawn later', async () => {
|
|
const startedFile = join(dir, 'started')
|
|
const releaseDir = join(dir, 'release'); mkdirSync(releaseDir)
|
|
schedulerBin(startedFile, releaseDir)
|
|
|
|
const p1 = spawnCli(['status'])
|
|
const p2 = spawnCli(['models'])
|
|
await waitUntil(() => startedList(startedFile).length === 2)
|
|
const p3 = spawnCli(['sessions']) // queued behind the cap, no child yet
|
|
await delay(50)
|
|
expect(startedList(startedFile)).not.toContain('sessions')
|
|
|
|
killAll() // reaps the two running AND cancels the queued third
|
|
// Attach all three rejection handlers synchronously: the queued spawn rejects
|
|
// at once, so it must not sit unhandled while the killed children settle.
|
|
await Promise.all([
|
|
expect(p1).rejects.toMatchObject({ kind: 'nonzero' }),
|
|
expect(p2).rejects.toMatchObject({ kind: 'nonzero' }),
|
|
expect(p3).rejects.toMatchObject({ kind: 'nonzero' }),
|
|
])
|
|
|
|
await delay(50)
|
|
expect(startedList(startedFile)).not.toContain('sessions') // never spawned
|
|
})
|
|
|
|
it('limits six simultaneous resident-failure fallbacks to two one-shot children', async () => {
|
|
const startedFile = join(dir, 'fallback-started')
|
|
const activeDir = join(dir, 'fallback-active'); mkdirSync(activeDir)
|
|
const activeCountsFile = join(dir, 'fallback-active-counts')
|
|
const releaseDir = join(dir, 'fallback-release'); mkdirSync(releaseDir)
|
|
fakeBin(
|
|
'failing-resident-with-blocked-fallbacks.js',
|
|
`const fs = require('node:fs'); const path = require('node:path'); const readline = require('node:readline');
|
|
if (process.argv[2] === 'serve') {
|
|
const rl = readline.createInterface({ input: process.stdin });
|
|
rl.on('line', line => {
|
|
const request = JSON.parse(line);
|
|
process.stdout.write(JSON.stringify({ id: request.id, ok: false, error: 'resident failed' }) + '\\n');
|
|
});
|
|
} else {
|
|
const id = process.argv[3];
|
|
fs.appendFileSync(${JSON.stringify(startedFile)}, id + '\\n');
|
|
const activeFile = path.join(${JSON.stringify(activeDir)}, String(process.pid));
|
|
fs.writeFileSync(activeFile, '');
|
|
fs.appendFileSync(${JSON.stringify(activeCountsFile)}, fs.readdirSync(${JSON.stringify(activeDir)}).length + '\\n');
|
|
const releaseFile = path.join(${JSON.stringify(releaseDir)}, id);
|
|
const timer = setInterval(() => {
|
|
if (!fs.existsSync(releaseFile)) return;
|
|
clearInterval(timer);
|
|
fs.unlinkSync(activeFile);
|
|
process.stdout.write(JSON.stringify({ via: 'spawn', id }));
|
|
}, 5);
|
|
}`,
|
|
)
|
|
startServe()
|
|
|
|
const requests = Array.from({ length: 6 }, (_, index) =>
|
|
spawnCli(['status', `fallback-${index}`], { timeoutMs: 5_000 }),
|
|
)
|
|
await waitUntil(() => startedList(startedFile).length >= 2)
|
|
await delay(150)
|
|
const admittedBeforeRelease = startedList(startedFile)
|
|
|
|
for (let index = 0; index < 6; index += 1) release(releaseDir, `fallback-${index}`)
|
|
await Promise.all(requests)
|
|
|
|
const activeCounts = startedList(activeCountsFile).map(Number)
|
|
expect(admittedBeforeRelease).toHaveLength(2)
|
|
expect(Math.max(...activeCounts)).toBeLessThanOrEqual(2)
|
|
})
|
|
})
|
|
|
|
describe('spawnCliAction', () => {
|
|
it('returns stdout and ok:true on success', async () => {
|
|
fakeBin('action-ok.js', 'process.stdout.write("currency updated")')
|
|
await expect(spawnCliAction(['currency', 'EUR'])).resolves.toEqual({ ok: true, stdout: 'currency updated', stderr: '', code: 0 })
|
|
})
|
|
|
|
it('returns stderr and ok:false on a non-zero exit', async () => {
|
|
fakeBin('action-fail.js', 'process.stderr.write("invalid alias"); process.exit(3)')
|
|
await expect(spawnCliAction(['model-alias', 'a', 'b'])).resolves.toEqual({ ok: false, stdout: '', stderr: 'invalid alias', code: 3 })
|
|
})
|
|
})
|
|
|
|
describe('nodeManagerDirs (nvm resolution)', () => {
|
|
const savedNvm = process.env.NVM_DIR
|
|
afterEach(() => {
|
|
if (savedNvm === undefined) delete process.env.NVM_DIR
|
|
else process.env.NVM_DIR = savedNvm
|
|
})
|
|
|
|
it('scans nvm version dirs newest-first and takes the first that holds codeburn', () => {
|
|
// Two versions; the lexicographically-"newest" (v9.0.0 > v22.0.0 as strings)
|
|
// has NO codeburn, while the real newer v22.0.0 does. The old `sort().reverse()[0]`
|
|
// would pick v9.0.0's bin and miss the CLI entirely.
|
|
const nvm = mkdtempSync(join(tmpdir(), 'codeburn-nvm-'))
|
|
try {
|
|
const versions = join(nvm, 'versions', 'node')
|
|
const v9bin = join(versions, 'v9.0.0', 'bin')
|
|
const v22bin = join(versions, 'v22.0.0', 'bin')
|
|
mkdirSync(v9bin, { recursive: true })
|
|
mkdirSync(v22bin, { recursive: true })
|
|
const codeburn = join(v22bin, 'codeburn')
|
|
writeFileSync(codeburn, '#!/bin/sh\n', { mode: 0o755 })
|
|
chmodSync(codeburn, 0o755)
|
|
|
|
process.env.NVM_DIR = nvm
|
|
const dirs = nodeManagerDirs()
|
|
expect(dirs).toContain(v22bin)
|
|
expect(dirs).not.toContain(v9bin)
|
|
} finally {
|
|
rmSync(nvm, { recursive: true, force: true })
|
|
}
|
|
})
|
|
})
|