Repository navigation
Give the CLI a runtime instead of the process, and cut npm test from 2m49s to about 77s - #151
Merged
Merged
Conversation
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B8Ng6auNzySjPMPZLrnpsh
runCli(argv) returns the exit code, loads one command module per call, and backs execCli in tests, so parsing and API tests no longer start node. npm test goes through tools/scripts/test.mjs with NODE_COMPILE_CACHE. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B8Ng6auNzySjPMPZLrnpsh
…orter The layout guard lands with the *.process.test.ts renames. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B8Ng6auNzySjPMPZLrnpsh
Files that start real tug processes are *.process.test.ts and import their entry points from tests/cli-process.ts. src/test-layout.test.ts keeps the tier honest, and npm run test:fast / test:process run one tier. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B8Ng6auNzySjPMPZLrnpsh
npm test is plain node --test again. The layout guard carries its own tier helper, and test:process runs the *.process.test.ts files. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B8Ng6auNzySjPMPZLrnpsh
…ements Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B8Ng6auNzySjPMPZLrnpsh
Everything in src/cli that read process.env, cwd, stdio, signals, console or the exit code now takes a CliRuntime (src/cli/runtime.ts). Per-run state that lived in module singletons (verbose, usage, resolved config, version checks, progress) lives on the runtime, so nothing resets between runs. runCli(argv, rt) builds the command tree per run and loads only the named command. execCli builds a test runtime from plain objects, so no global is patched and calls overlap safely; each call has a 60 s deadline. ESLint rejects process, console and os.homedir in src/cli outside runtime.ts. Shared image, compose and transport modules take an explicit env or version from the CLI. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B8Ng6auNzySjPMPZLrnpsh
…ng paths, doc fixes - the exit-code test lints a failing file, so it proves rt.exitCode reaches runCli's result - ESLint also rejects node:process and process imports, globalThis.process/console and a named homedir import; src/cli/runtime-boundary.test.ts runs the repository config over snippets - --credential-out and a relative TUG_CREDENTIALS_DIR / XDG_CONFIG_HOME resolve against rt.cwd - lint names an unreadable file as typed instead of as an absolute path - the layout guard also catches a fast file that spawns a process and names the CLI or runtime entry in any spelling - docs: one headline time, correct file counts, wait-progress is in-process, no first person Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B8Ng6auNzySjPMPZLrnpsh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
npm testtook 2m49s (8m35s of CPU) because tests started the CLI as a child process 467 times, at about half a second each. A first version of this PR made the CLI callable in-process by patchingprocess.env, cwd, stdout, stderr, stdin andprocess.exitfrom a test helper. Review found that this automated around the real problem: the CLI read ambient process state in about 25 files and kept per-run state in module singletons. This version removes the cause.The CLI runtime
src/cli/runtime.tsdefinesCliRuntime:env,cwd,stdin,stdout,stderr,signals,version,exitCode, andstate. Commands, helpers, prompts, pickers, progress and child-process runners takertexplicitly; output goes throughout(rt, …)anderr(rt, …), command-line paths throughresolvePath(rt, …), the home directory throughhomeDir(rt). Child processes start withrt.envandrt.cwd, and their output is forwarded to the runtime's streams unless those are the real terminal.--verbose, work started, resolved config, version checks, the running progress display) isrt.state, created with the runtime. The four module singletons are gone, along withresetUsage,setVerbose,setResolvedConfig,resetVersionChecksand thepinPackageVersionseam, so nothing needs resetting between runs.src/cli/program.tsexportsrunCli(argv, rt), which builds the command tree per run, loads only the module of the command the arguments name (root options come from the program itself, oneREGISTRARStable), and returns the exit code.src/cli.tsis a three-line entry.process,consoleandos.homedirinsrc/cli/outsideruntime.ts, includingnode:processandprocessimports,globalThis.processandglobalThis.console, and a namedhomedirimport.src/cli/runtime-boundary.test.tsruns the repository's ESLint config over snippets so the rule cannot be loosened or bypassed silently. Shared domain modules (images/,compose/,transport.ts) take an explicitenvor version from the CLI and default to the process for the control plane and agent. Time and the temp directory are not part of the boundary. Seedocs/architecture.md#the-cli-runtime.Tests
tests/cli-runtime.tsbuilds a runtime from plain objects;execCli(tests/cli-inprocess.ts) runs the realrunCliwith one. Nothing global is patched, calls overlap safely, each has a 60 s deadline, and theTUG_LOG_LEVELworkaround and the globalTUG_DATA_DIRleak instartMtlsControlare gone.console,process.env.PATH, cwd, stdin orprocess.exitCode(wait-progress,ssh-hostkey,host,init.import,context.skew,progress,process) now use a runtime.*.process.test.ts, importcliEntry/mainEntryfromtests/cli-process.ts, and are guarded bysrc/test-layout.test.ts, which also rejects a fast file that spawns a process and names the CLI or runtime entry in any spelling.npm run test:processruns them.NODE_COMPILE_CACHEon the test step. No runner script:npm testis plainnode --test.npm test(4 cores)--test-concurrency=4)docs/test-performance.mdhas the measurements (serial per-file timings, concurrency sweep) anddocs/testing.mdthe conventions.Review
Two independent read-only reviews of this branch found no regression in the real CLI: help, usage, errors and exit codes matched
mainbyte for byte for 32 invocations, including multi-command modules, root options before the command, and a 60-column terminal. Their findings were fixed: a vacuous exit-code test now lints a failing file, the ESLint gaps above,--credential-outand relativeTUG_CREDENTIALS_DIR/XDG_CONFIG_HOMEnow resolve againstrt.cwd,tug lintstill names an unreadable file as typed, the layout guard hole, and doc inconsistencies.Test plan
npm test: 1,192 pass, 0 fail (withNODE_COMPILE_CACHEset)npm run lint(3 pre-existing warnings),npm run typecheck,npm run build,npm run test:smoke:distdist/cli.jsrun by hand: exit codes and output forlint(ok, findings, missing file), an unknown command,--version, a missing option and a missing credentialsrc/cli.tsunder type stripping on 22.18.0 is untested hereNotes
tug credential liststarts in about 0.34 s instead of 0.45 s because only that command's module loads.patch. The CLI contract,x-tug, the wire protocol and persisted formats are unchanged.🤖 Generated with Claude Code
https://claude.ai/code/session_01B8Ng6auNzySjPMPZLrnpsh