Skip to content

refactor(cli): run one-shot commands through one shared owner - #10405

Merged
morluto merged 4 commits into
mainfrom
refactor/cli-one-shot-owner
Nov 14, 2026
Merged

morluto merged 4 commits into
mainfrom
refactor/cli-one-shot-owner

Conversation

@morluto

@morluto morluto commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

Summary

The one-shot CLI command sequence existed twice: in scripts/rea.mjs and in the test session worker tests/support/cli/cliCommandSessionWorker.mjs (its comment said it "matches scripts/rea.mjs"). The copy had drifted, so tests on the shared session exercised different behavior from the shipped executable. This moves the sequence into one owner that both call.

Problem and expected behavior

Before this change, through the file-scoped session (runFileCli), compared with a fresh rea process (executed, observed with the new parity test against the old worker):

  • --version --json: the fresh process returns a version document; the session passed it to Incur instead. Failed.
  • mcp add --no-global --agent amp --json: the fresh process refuses with AMP_PROJECT_SCOPE_UNSUPPORTED; the session did not. Failed.
  • capabilities --json --token-limit 5: both reject the output combination. Passed.

Expected: the session and a fresh process produce identical exit code, stdout and stderr for the same command.

This duplication is also what blocks moving more tests onto shared sessions, which is the main lever for CLI spawn cost in CI.

Change and scope

  • New src/cliOneShot.ts exports runOneShotCli(arguments, host): argument normalization, output admission, configuration errors, version documents, the Amp scope refusal, and dispatch with result delivery. It imports ./cli.js lazily, so the early answers still avoid loading the command catalog.
  • scripts/rea.mjs keeps admission, MCP/doctor routing, closed-pipe status, worker shutdown and process exit. It calls runOneShotCli with a host bound to its streams.
  • The session worker calls the same function with a host that captures output per command and records exit status instead of exiting.
  • renderCliOutputArgumentError takes a CliPreExecutionError, so the Amp refusal code is typed instead of passed as an out-of-union literal.
  • The cliOutput runtime bundle entry is superseded and removed (bundle-runtime.mjs, prepack.mjs, launcher runtime file list). cliOneShot is added to the bundle, prepack and both Knip configs.
  • tests/boundary/cli/dispatcher.test.ts previously stubbed cliOutput.js. It now replaces only the command runtime and result output, so output admission runs production code. The copied runtime links the installation's node_modules because production chunks import zod and semver.

Not included: moving more tests onto shared sessions, and the 113 test files that spawn scripts/rea.mjs directly.

Contribution scope: not applicable (refactor within existing boundaries).

Contract and boundary impact

  • Semantic owner and earliest changed stage: src/cliOneShot.ts now owns the one-shot sequence. Consumers: scripts/rea.mjs and the test session worker. Superseded: the worker's copy and the cliOutput bundle entry.

  • Contribution scope fit and linked maintainer decision: not applicable.

  • CLI and MCP/tool-catalog contract: no intended change. MCP and doctor routing are untouched.

  • Provider, bridge, target-format, or platform compatibility: none.

  • Evidence, artifact, provenance, or reconstruction contract: none.

  • Target selection, authority, upstream behavior, and cleanup details (if applicable): worker shutdown and closed-pipe handling stay in the launcher; ordering is unchanged (resources close before SDK output finishes on an explicit exit).

  • Generated metadata, package, or installation impact: packaged files gain dist/cli-runtime/cliOneShot.js and lose dist/cli-runtime/cliOutput.js; prepack.mjs checks the new set.

  • Read existing files or captured data.

  • Launch or attach to a process.

  • Inject instrumentation or run scripts/code.

  • Change process memory, registers, files, or configuration.

  • Contact network services.

  • Recover protected code or work around protections.

  • Documentation or usability only; no target interaction.

  • Not sure yet; investigation or maintainer guidance needed.

Evidence and regression coverage

  • Tests added or updated: tests/boundary/cli/cliSessionParity.test.ts (new, three cases); tests/boundary/cli/dispatcher.test.ts and tests/boundary/process/runtimeBundle.test.ts fixtures updated.
  • Base reproduction or other evidence: with the old worker restored, the parity test fails 2 of 3 cases; with this change all 3 pass.
  • User-visible CLI/MCP output (if applicable): unchanged; checked by hand: rea --version → 6.9.0; --version --format json → version document; mcp add --no-global --json → AMP_PROJECT_SCOPE_UNSUPPORTED, exit 1; capabilities --json | head -1 → exit 141.
  • Remaining proof gaps:
    • Full acceptance, boundary, MCP and process projects were not run locally; CI covers them.
    • Windows launcher behavior is unverified locally.
    • The installed-package path is covered only by prepack.mjs file checks here, not by a local verify:package run.

Validation performed

Local macOS, Node 24.22.1:

  • npm run check:fast — typecheck and lint passed.
  • npm run build:cached — passed.
  • npx vitest run --project cli-boundary — 247 files, 1,850 passed, 5 skipped.
  • All 31 acceptance files that use runFileCli/sharedCliRun — 333 passed, 1 skipped.
  • npx vitest run tests/boundary/cli/dispatcher.test.ts — 20 passed.
  • npx vitest run tests/boundary/process/runtimeBundle.test.ts — 2 passed (fixture updated for the new bundle entry after review).
  • npm run knip and npm run knip:production — no new findings.
  • npx oxfmt --check . (the repository formatter) — passed.

Compatibility, safety, and release

  • Breaking changes or migration steps: none.
  • Real Hopper/Ghidra, browser, or OS coverage: not applicable.
  • Package or release metadata impact: one runtime entry renamed as above.
  • Security, privacy, process, or containment review: process exit and cleanup ownership unchanged.

Author readiness

Human owner:

  • I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work.
  • I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps.
  • I declare this PR ready for initial maintainer review.

Before merge

  • This PR addresses a concrete problem or an agreed enhancement.
  • Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

scripts/rea.mjs and the test CLI session worker each implemented the
one-shot command sequence: argument normalization, output admission,
configuration errors, version documents, the Amp scope refusal, and
result delivery. The worker copy had drifted: it never rendered
formatted version reports or the Amp refusal.

Move the sequence into src/cliOneShot.ts. Both callers supply a host
for their streams, exit and resource ownership. The launcher keeps
closed-pipe status, worker shutdown and process exit; the session
worker captures output per command.

The cliOutput bundle entry is superseded and removed. The dispatcher
test now replaces only the command runtime, so output admission runs
production code. A new parity test compares the file session with a
fresh process.
@greptile-apps

greptile-apps Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new blocking issue was identified.

Summary

Moves the one-shot command sequence into runOneShotCli, shared by the executable and test session.

  • Fresh CLI commands and file sessions now share one command path.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Executable launcher] --> C[runOneShotCli]
  B[Test session worker] --> C
  C --> D{Early answer?}
  D -->|Yes| E[Write version or refusal]
  D -->|No| F[Load command runtime]
  F --> G[Run command and deliver result]
  G --> H[Host owns exit and cleanup]
Loading

Reviews (4) · Last reviewed commit: "Merge branch 'main' into refactor/cli-on..." · Reviewed by Greptile

Comment thread scripts/bundle-runtime.mjs Resolved
@morluto
morluto marked this pull request as ready for review November 14, 2026 03:47
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-14T03:48:54.005131Z 8c40210 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@morluto
morluto merged commit bddbd80 into main Nov 14, 2026
50 of 51 checks passed
@morluto
morluto deleted the refactor/cli-one-shot-owner branch November 14, 2026 03:48
@morluto morluto added the area: cli CLI adapter and one-shot commands label Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: cli CLI adapter and one-shot commands

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant