Repository navigation
fix(cli): install a working bgagent command and report a real version (#913) - #1059
nizar-lahlali wants to merge 6 commits into
Conversation
…#913) cli/README.md told users to `npm install -g @backgroundagent/cli`, a package that was never published (the package is @abca/cli), so there was no working `bgagent`: the guides ran `node lib/bin/bgagent.js` from cli/ about 120 times, and `bgagent setup` printed bare `bgagent` next steps that failed when copied. `bgagent --version` was a hard-coded 0.0.0. The CLI stays unpublished: it belongs to the checkout the stack is deployed from. Following the design in #913: - `mise run cli:install` compiles the CLI and writes ~/.local/bin/bgagent (BGAGENT_BIN_DIR overrides), a sh script that runs the CLI of the ABCA checkout or worktree the user is in, and of the installing checkout elsewhere, with one stderr line when it is not the installed one (BGAGENT_SHIM_QUIET silences it). It uses `mise exec` when mise is installed, and prints the PATH line to add when needed. It never replaces a bgagent it did not write or another checkout's without --force. - `mise run install` writes it too, without compiling (`mise run build` does) and with --keep-existing: it never replaces another checkout's command and never fails the install. - The script sets BGAGENT_INVOKED_AS, so setup's next steps print `bgagent`, not `node <checkout>/cli/lib/bin/bgagent.js`. - `--version` prints the package version and the commit recorded at compile in lib/build-info.json (-dirty for uncommitted changes), falling back to package.json; an unreadable file is reported in the string rather than stopping the CLI at start-up. - Docs: the README explains installation, which checkout runs and that ~/.bgagent is per user; the Quick Start, user and deployment guides and the plugin skills call `bgagent`, with `node cli/lib/bin/bgagent.js` as the fallback. Tests: the installer and the script in throwaway checkouts (install, idempotency, refusals, --force, --keep-existing, PATH hint, checkout resolution, unbuilt checkout, quiet, mise path, quoting), a check that no doc installs @backgroundagent/cli, cliVersion and cliInvocation.
…s directory (#913) From /review_pr on #1059: - The command ran `mise exec -C <checkout>`, which changes directory, so every relative path (`submit --attachment`, `trace download -o`, `deny --reason-file`) resolved in the checkout root, and an attachment outside it was refused. It now finds the checkout's Node with `mise which node` in a subshell (no directory change, no install, no auto-trust of a mise config) and falls back to plain node. - Following the checkout of the working directory ran code from any directory with the marker files: a fork's PR checked out for review, or a checkout planted in a shared parent, would run with the operator's credentials. The command now always runs the checkout that installed it; `mise run cli:install --force` from another checkout switches it (isadeks's first proposal in #913). - The installer no longer writes through a symlink: it lstats the target and writes a new file renamed over it, so --force replaces a link instead of overwriting what it points to. - It replaces the bgagent of a checkout that no longer exists, updates its own older script, restores a lost execute bit, warns when another bgagent comes first on PATH, skips `mise run install` when CI is set, runs when started through a symlinked path, and swallows only file-system errors under --keep-existing. - write-build-info records `git rev-parse --short=12` (a tag no longer replaces the commit) with -dirty for tracked changes, and says why when no commit can be read. cliVersion reports an unreadable build-info or package.json instead of hiding it, and the entry point exports `program` so a test checks that --version is cliVersion(). Tests: the shim suite is rewritten for the pinned design (27 tests, 16 of which fail against the previous installer), plus version wiring and cliInvocation reading BGAGENT_INVOKED_AS from the environment. Docs follow in the next commit.
Conflicts: - mise.toml: kept main's git-hook step of `install` (#968, #1064) and added the bgagent step after it; the cli:install comment describes the pinned command. - cli/README.md, cli/AGENTS.md: kept main's wording that @abca/cli is private (#1052), with this branch's installation section, rewritten for the pinned command. - docs/guides/USER_GUIDE.md: kept main's corrected examples (MIME type from the extension, trace download without gunzip) with `bgagent`; the Starlight mirror is regenerated.
The command always runs the checkout that installed it and keeps the caller's directory, so the docs no longer say it follows the checkout you are in. Also from /review_pr on #1059: - Quick Start: `mise run install` gives a command that works once `mise run build` has compiled the CLI; what to do when it is not found (the PATH line, then `mise run cli:install`), not built, or shadowed by an old `alias bgagent=`. - User guide: the checkout sentence, and two stale "from cli/" lines. - Plugin skills: when `bgagent` is not found, try ~/.local/bin/bgagent or the repo-root entrypoint; never pass --force without asking the user. The onboard-repo skill gets the same note; the plugin README line that was half converted is fixed. - AGENTS.md, CONTRIBUTING.md: run cli:install from the repo root (not //cli:install), and the command works after the build.
ayushtr-aws
left a comment
There was a problem hiding this comment.
Request changes. The work is solid, but mise run install now pins the global bgagent to whichever checkout runs it first, which collides with the repo's worktree-per-issue workflow and with this PR's own security argument.
Context: #913 is approved, the branch name is valid, and the PR is based on current main.
Vision alignment
Fits: operator tooling with no control-plane, IAM or blast-radius change. Keeping the CLI unpublished and tied to the checkout is consistent with the bootstrap-version and DeploymentFeatures coupling.
One governance point: this implements the first proposal on #913, not the variant marked "preferred". The security rationale is sound, but it sits only in the PR body; please get it acknowledged on the issue by a maintainer before merge.
Blocking
1. mise run install pins bgagent to throwaway or untrusted worktrees (mise.toml install task; scripts/install-cli-shim.ts:121,129-131)
--keep-existingwrites when no command exists, and also when the recorded checkout is gone.AGENTS.md:48tells contributors to runmise run installin every new worktree.- Everyday case: a contributor's first install in an issue worktree pins
bgagentthere. Deleting the worktree makesbgagentexit 127, and the next install anywhere silently re-pins it. - Security case: a reviewer who installs in a fork-PR worktree leaves every later
bgagentrunning that fork's CLI with their AWS and Cognito credentials — the scenario this PR rejected the "follow the checkout" variant to avoid. The install already runs fork code; what is new is that the pin persists after the review. - Reproduced:
mise run installin a fresh/tmpworktree of this PR, with no existing command, wrote abgagentpinned to that worktree. cli/README.md:16contradicts it: without--forcethe task "never replaces … one that another checkout installed".
Suggested fix: under --keep-existing, skip linked worktrees (git rev-parse --git-dir differs from --git-common-dir) and print a hint instead; never auto-replace a gone checkout's command without --force, say what to run. Correct the README sentence and add a worktree note to AGENTS.md.
Non-blocking
Code
- Temp file leak:
writeAtomic(install-cli-shim.ts:143-154) leaves an executable.bgagent.<pid>.<hex>behind on any failure; a directory target with--forcereproduces it (EISDIR). Unlink in acatch, and haveplanInstallrefuse directories. - Silent Node fallback: when
mise which nodefails (e.g. the pinned Node is not installed), the script runs whatevernodeis onPATH, which can be outsideengines. Print one stderr line on fallback. - Over-broad catch:
'code' in errinmain()also matches Node'sERR_INVALID_ARG_*errors, so a script bug exits 0 under--keep-existing. Require'syscall' in err. --forceunder CI:--force --keep-existingwithCIset skips the write, although the comment onplanInstallsays force wins.- Misleading PATH hint: after keeping a foreign file, the hint says to put the bin directory first, which would run a command that is not this checkout's.
- Ownership parse-back: a checkout path containing
'followed by a newline is misjudged as gone (:87-91). Reject control characters in the path. -dirtyis repo-wide: a docs-only edit marks the CLI build dirty. Scope the status call with-- .or document it.
Tests (cdk/test/scripts/install-cli-shim.test.ts)
- The dangling-symlink test (
:217-225) uses--keep-existing, so nothing is written and it cannot fail. Use--force. - "updates an older bgagent" (
:166-178) asserts only the wordupdated, not the status or the rewritten content. cli/scripts/write-build-info.mjshas no test: the commit,-dirtyand no-git paths are uncovered, as is thelib/build-info.jsonlocation shared withversion.ts.- "uses the node mise resolves" (
:338-356): the fakemiseechoes the Node already onPATH, so a script ignoringnode_binwould pass. - Environment dependence: the suite throws if
miselives in/usr/bin, shells out to the realmise(:381-387, unlike sibling suites), and sets nospawnSynctimeout across about 80 child processes. - The registry-install regex (
:407) missesnpx @abca/cliandnpm i @abca/cli -g, and would flag prose that warns against the command.
Plugin skills
deploy/SKILL.mdandsetup/SKILL.mdlack the "never pass--forcewithout asking" sentence the other four carry.- "Run
mise //cli:compilefrom the repo root" should name the path the command prints, not the working directory.
Documentation
- Guides, README,
AGENTS.md,CONTRIBUTING.mdand skills are updated and otherwise accurate; the Starlight mirror matches its sources. - Missing: the worktree behaviour and the README correction from the blocking issue.
mise.toml:60-62and the README say "only when there is none", but the installer also rewrites this checkout's own stale or non-executable command.- Overlaps #780 on
QUICK_START.mdx; whichever lands second needs a rebase.
Tests & CI
- CI green at review time except
integ-smoke(pending). - Run locally on
6040eab0(macOS arm64):install-cli-shim.test.ts27/27, thecliversion and setup suites pass;mise run cli:install --forceforwards--force(foreign file refused without it, replaced with it); the installed command printed0.0.0 (6040eab0d19f)from another directory. Fullmise run buildnot re-run. - Bootstrap synth-coverage: not applicable — no construct, stack or handler change.
Review agents run
- Security review — the PATH-installed script, file write, credentials exposure.
code-reviewer+silent-failure-hunter— one pass.pr-test-analyzer+comment-analyzer— one pass.type-design-analyzer— omitted: the only new type is a three-variantPlanunion private to the script.
Human heuristics
- Proportionality: pass — 239 lines for the installer is justified by the symlink and ownership handling.
- Coherence: concern — the only suite under
cdk/test/scripts/that shells out to the realmise(install-cli-shim.test.ts:381-387). - Clarity: concern — the silent Node fallback and the over-broad catch hide failures behind plausible defaults.
- Appropriateness: concern — some tests assert what the code does, not what it should (
:166-178,:217-225); Linux and x86_64 unverified, per the PR description.
#913) From ayushtr-aws's review of #1059: - `mise run install` runs in every new worktree (AGENTS.md), so the first install, often in a throwaway issue worktree or a fork's PR under review, pinned bgagent there, and deleting it made the next install anywhere re-pin it. With --keep-existing the installer now writes nothing from a linked worktree (its .git is a file, read from the file system so a missing git cannot hide it) and never replaces the command of a checkout that no longer exists; both say to run `mise run cli:install --force`. - writeAtomic removes its temporary file on any failure; a directory at the target is refused, even with --force. - With mise installed but no Node for the checkout, the command says so on stderr before running the node on PATH. - --keep-existing swallows only file-system errors (code and syscall); --force wins over CI; no PATH advice for a command it kept that is not this checkout's; a checkout path with a control character is refused. - write-build-info marks -dirty only for changes under cli/. Tests: the shim suite runs with only node and git on PATH and reads the task wiring from mise.toml instead of calling mise, with a timeout on every process. New cases: the linked worktree, the deleted checkout, a directory target, a control character, --force under CI, no PATH advice for a kept command; the dangling-symlink, update and mise-node tests now assert behavior. A new suite covers write-build-info.mjs (commit, tag, -dirty scope, no git, the round trip through cliVersion). The registry check reads fenced code only and catches npx and `npm i ... -g`. Docs: README and AGENTS.md describe the worktree behavior; the skills say never to pass --force without asking and to compile in the checkout the message names.
mise run cli:installnow gives you a workingbgagentcommand, andbgagent --versionnames the commit the CLI was built from.mise run installwrites the command too; it works oncemise run buildhas compiled the CLI. Before,cli/README.mdtold users tonpm install -g @backgroundagent/cli, a package that was never published. #1052 has since removed that line; it noted that@abca/cliis private. There was no workingbgagent, so the guides rannode lib/bin/bgagent.jsfromcli/about 90 times, and the plugin skills usednode cli/lib/bin/bgagent.js.--versionwas a hard-coded0.0.0.Area
cdk— infrastructure, handlers, constructsagent— Python runtime / Docker imagecli—bgagentclientdocs— guides or design sources (docs/guides/,docs/design/)tooling— rootmise.toml, scripts, CI workflowscdk: one test suite undercdk/test/scripts/(where the root scripts are tested); no CDK source change.Related
Closes #913. Unblocks #988: its installer can call
mise run cli:installinstead ofnode lib/bin/bgagent.js. Overlaps #780 (QUICK_START.mdx).Design note for #913. This implements isadeks's first proposal, a command pinned to the checkout that installed it, not the preferred variant that follows the checkout you are in. A security review of the variant showed it runs code from any directory holding the two marker files, with the operator's AWS and Cognito credentials. It reproduced three ways in: a fork's pull request checked out for review, a checkout planted in a shared parent such as
/tmp, andmise exec -Cauto-trusting that directory'smise.toml. To use another checkout or worktree, runmise run cli:install --forcethere, or callnode cli/lib/bin/bgagent.jsin it.Changes
The CLI stays unpublished. It belongs to the checkout the stack was deployed from: it checks the bootstrap version, reads the
DeploymentFeaturesoutput and shares API shapes with the handlers.mise run cli:installcompiles the CLI and writes~/.local/bin/bgagentscripts/install-cli-shim.ts,mise.tomlBGAGENT_BIN_DIRsets another directory. The installer prints thePATHline to add, and warns when anotherbgagentcomes first onPATH. It never replaces abgagentit did not write, or another checkout's, without--force. It replaces the command of a checkout that no longer exists, updates its own older script and restores a lost execute bit.scripts/install-cli-shim.tslstats the target and writes a new file (O_EXCL,O_NOFOLLOW) renamed over it. The write is atomic and leaves no temporary file on failure.--forcereplaces a symlink itself, never what the link points to, and never a directory. Under--keep-existingonly file-system errors (withcodeandsyscall) are swallowed; a bug in the script still fails. A checkout path with a control character is refused.bgagentscript is pinned and keeps your directorymise which nodein a subshell (no directory change, no install, no trust prompt) and falls back tonode; when mise is installed but has no Node for the checkout, it says so on stderr. Your directory stays, so--attachment,-oand--reason-filepaths resolve where you typed them. A missing or unbuilt checkout exits 127 and says what to run.mise run installwrites the command, from a main checkout onlymise.tomlmain's git-hook step (#968), with--keep-existing. It writes only when there is no command (or updates this checkout's own), never from a linked worktree, never in CI, and never fails the install. A linked worktree has a.gitfile, read from the file system so a missing git cannot hide it;AGENTS.mdasks formise run installin every worktree, and a throwaway issue worktree or a fork's PR under review must not become whatbgagentruns. A deleted checkout's command is not replaced without--forceeither. It does not compile;mise run builddoes.bgagentcli/src/commands/setup.tscliInvocation()returnsBGAGENT_INVOKED_AS, which the script sets. It otherwise printed the form it was launched with,node /abs/path/cli/lib/bin/bgagent.js.--versionprints the version and the commitcli/scripts/write-build-info.mjs,cli/src/version.ts,cli/package.json,cli/src/bin/bgagent.tslib/build-info.json(git-ignored, outside the agent image context). It holds the package version andgit rev-parse --short=12 HEAD, plus-dirtyfor tracked changes undercli/, so a tag never replaces the SHA; it says why when there is no commit.cliVersion()falls back topackage.jsonand never throws at startup: an unreadable file shows in the string. The entry point exportsprogram, so a test checks that--versioniscliVersion(). The version stays0.0.0; choosing one is a maintainer call.bgagentcli/README.md,QUICK_START.mdx,USER_GUIDE.md,DEPLOYMENT_GUIDE.md,docs/abca-plugin/,AGENTS.md,cli/AGENTS.md,CONTRIBUTING.md~/.bgagentis per user, the oldalias bgagent=, and the fallback. The Quick Start says what to do whenbgagentis not found or not built. The skills try~/.local/bin/bgagent, since Claude Code's shell may lack it onPATH, then the entrypoint, and never pass--forcewithout asking. The Starlight mirror is regenerated.Review shaped this.
/review_pr(six agents) foundmise exec -Crunning the CLI from the checkout root, writes through a symlink, and the security issue above. ayushtr-aws's review foundmise run installpinning the command to the first worktree it ran in.Testing
On the committed tree, merged with
main(macOS, arm64):mise run build: passes (agent 3,139 tests, cdk 8,127, cli 1,486, web 237) and rewrites no file.security:sast:masking,security:sast:range(0 findings) andsecurity:secrets: pass.cdk/test/scripts/install-cli-shim.test.ts(32 tests) runs the installer in throwaway checkouts and the written script undersh.PATHholds only links tonodeandgit, every process has a timeout, and the task wiring is read frommise.tomlwithout callingmise.--forceand--keep-existing; a linked worktree skipped bymise run install; a checkout that no longer exists (kept without--force); a directory target; a control character in the path;--forceunder CI; noPATHadvice for a kept command; an older script of its own; a lost execute bit; a symlinked and a dangling-symlink target; a failed write;CI; thePATHhint and anotherbgagentfirst onPATH; started through a symlinked path; an unknown argument.BGAGENT_INVOKED_AS; the Nodemise which nodenames is the one that runs, without a directory change; fallback tonode, with a notice; a checkout path with a space and a quote.misetask wiring, and a check that no command block in tracked Markdown installs or runs the CLI from a registry (npm i … -g,npx,yarn global add).cli/test/version.test.ts(8 tests) coverscliVersionand--versionwired to it.cli/test/write-build-info.test.ts(4 tests) covers the commit, a tag not replacing it,-dirtyscoped tocli/, no git, and the round trip throughcliVersion. TwocliInvocationcases are insetup.test.ts.mise run installnow declines to write the command and namesmise run cli:install --force./tmp,~/.local/bin/bgagent --versionprinted0.0.0 (ce3879534982-dirty).Not verified: Linux and x86_64, and the plugin skills end to end in Claude Code.
Acknowledgment
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of the project license.