Skip to content

fix(cli): install a working bgagent command and report a real version (#913) - #1059

Open
nizar-lahlali wants to merge 6 commits into
mainfrom
fix/913-cli-install-and-version
Open

nizar-lahlali wants to merge 6 commits into
mainfrom
fix/913-cli-install-and-version

Conversation

@nizar-lahlali

@nizar-lahlali nizar-lahlali commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

mise run cli:install now gives you a working bgagent command, and bgagent --version names the commit the CLI was built from. mise run install writes the command too; it works once mise run build has compiled the CLI. Before, cli/README.md told users to npm install -g @backgroundagent/cli, a package that was never published. #1052 has since removed that line; it noted that @abca/cli is private. There was no working bgagent, so the guides ran node lib/bin/bgagent.js from cli/ about 90 times, and the plugin skills used node cli/lib/bin/bgagent.js. --version was a hard-coded 0.0.0.

Area

  • cdk — infrastructure, handlers, constructs
  • agent — Python runtime / Docker image
  • cli — bgagent client
  • docs — guides or design sources (docs/guides/, docs/design/)
  • tooling — root mise.toml, scripts, CI workflows

cdk: one test suite under cdk/test/scripts/ (where the root scripts are tested); no CDK source change.

Related

Closes #913. Unblocks #988: its installer can call mise run cli:install instead of node 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, and mise exec -C auto-trusting that directory's mise.toml. To use another checkout or worktree, run mise run cli:install --force there, or call node cli/lib/bin/bgagent.js in it.

Changes

The CLI stays unpublished. It belongs to the checkout the stack was deployed from: it checks the bootstrap version, reads the DeploymentFeatures output and shares API shapes with the handlers.

Change Where Note for the reviewer
mise run cli:install compiles the CLI and writes ~/.local/bin/bgagent scripts/install-cli-shim.ts, mise.toml BGAGENT_BIN_DIR sets another directory. The installer prints the PATH line to add, and warns when another bgagent comes first on PATH. It never replaces a bgagent it 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.
The file is written safely scripts/install-cli-shim.ts It lstats 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. --force replaces a symlink itself, never what the link points to, and never a directory. Under --keep-existing only file-system errors (with code and syscall) are swallowed; a bug in the script still fails. A checkout path with a control character is refused.
The bgagent script is pinned and keeps your directory written by the installer It always runs the installed checkout and never code from the working directory. It finds that checkout's Node with mise which node in a subshell (no directory change, no install, no trust prompt) and falls back to node; when mise is installed but has no Node for the checkout, it says so on stderr. Your directory stays, so --attachment, -o and --reason-file paths resolve where you typed them. A missing or unbuilt checkout exits 127 and says what to run.
mise run install writes the command, from a main checkout only mise.toml It runs after main'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 .git file, read from the file system so a missing git cannot hide it; AGENTS.md asks for mise run install in every worktree, and a throwaway issue worktree or a fork's PR under review must not become what bgagent runs. A deleted checkout's command is not replaced without --force either. It does not compile; mise run build does.
Setup prints bare bgagent cli/src/commands/setup.ts cliInvocation() returns BGAGENT_INVOKED_AS, which the script sets. It otherwise printed the form it was launched with, node /abs/path/cli/lib/bin/bgagent.js.
--version prints the version and the commit cli/scripts/write-build-info.mjs, cli/src/version.ts, cli/package.json, cli/src/bin/bgagent.ts The compile script writes lib/build-info.json (git-ignored, outside the agent image context). It holds the package version and git rev-parse --short=12 HEAD, plus -dirty for tracked changes under cli/, so a tag never replaces the SHA; it says why when there is no commit. cliVersion() falls back to package.json and never throws at startup: an unreadable file shows in the string. The entry point exports program, so a test checks that --version is cliVersion(). The version stays 0.0.0; choosing one is a maintainer call.
Docs and plugin skills call bgagent cli/README.md, QUICK_START.mdx, USER_GUIDE.md, DEPLOYMENT_GUIDE.md, docs/abca-plugin/, AGENTS.md, cli/AGENTS.md, CONTRIBUTING.md The README covers installation, which checkout runs and why, that ~/.bgagent is per user, the old alias bgagent=, and the fallback. The Quick Start says what to do when bgagent is not found or not built. The skills try ~/.local/bin/bgagent, since Claude Code's shell may lack it on PATH, then the entrypoint, and never pass --force without asking. The Starlight mirror is regenerated.

Review shaped this. /review_pr (six agents) found mise exec -C running the CLI from the checkout root, writes through a symlink, and the security issue above. ayushtr-aws's review found mise run install pinning 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) and security:secrets: pass.
  • cdk/test/scripts/install-cli-shim.test.ts (32 tests) runs the installer in throwaway checkouts and the written script under sh. PATH holds only links to node and git, every process has a timeout, and the task wiring is read from mise.toml without calling mise.
    • Installer: install, idempotency and the default directory; refusing another checkout's command and a foreign file; --force and --keep-existing; a linked worktree skipped by mise run install; a checkout that no longer exists (kept without --force); a directory target; a control character in the path; --force under CI; no PATH advice 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; the PATH hint and another bgagent first on PATH; started through a symlinked path; an unknown argument.
    • Command: runs the installed checkout from any directory and keeps it; never runs a planted checkout's code from its root or below; a gone checkout and an unbuilt one; BGAGENT_INVOKED_AS; the Node mise which node names is the one that runs, without a directory change; fallback to node, with a notice; a checkout path with a space and a quote.
    • mise task 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) covers cliVersion and --version wired to it. cli/test/write-build-info.test.ts (4 tests) covers the commit, a tag not replacing it, -dirty scoped to cli/, no git, and the round trip through cliVersion. Two cliInvocation cases are in setup.test.ts.
  • By hand:
    • In this linked worktree, mise run install now declines to write the command and names mise run cli:install --force.
    • From /tmp, ~/.local/bin/bgagent --version printed 0.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.

…#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.
)

security:sast:range flagged path-join-resolve-traversal in cliVersion:
its libDir parameter went into path.join. The parameter only existed for
the tests, so the two paths are now built once from __dirname, and
cliVersion takes the file paths themselves, which the tests pass. No
behavior change.
bgagent added 3 commits November 23, 2026 10:25
…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 ayushtr-aws left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-existing writes when no command exists, and also when the recorded checkout is gone. AGENTS.md:48 tells contributors to run mise run install in every new worktree.
  • Everyday case: a contributor's first install in an issue worktree pins bgagent there. Deleting the worktree makes bgagent exit 127, and the next install anywhere silently re-pins it.
  • Security case: a reviewer who installs in a fork-PR worktree leaves every later bgagent running 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 install in a fresh /tmp worktree of this PR, with no existing command, wrote a bgagent pinned to that worktree.
  • cli/README.md:16 contradicts it: without --force the 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 --force reproduces it (EISDIR). Unlink in a catch, and have planInstall refuse directories.
  • Silent Node fallback: when mise which node fails (e.g. the pinned Node is not installed), the script runs whatever node is on PATH, which can be outside engines. Print one stderr line on fallback.
  • Over-broad catch: 'code' in err in main() also matches Node's ERR_INVALID_ARG_* errors, so a script bug exits 0 under --keep-existing. Require 'syscall' in err.
  • --force under CI: --force --keep-existing with CI set skips the write, although the comment on planInstall says 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.
  • -dirty is 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 word updated, not the status or the rewritten content.
  • cli/scripts/write-build-info.mjs has no test: the commit, -dirty and no-git paths are uncovered, as is the lib/build-info.json location shared with version.ts.
  • "uses the node mise resolves" (:338-356): the fake mise echoes the Node already on PATH, so a script ignoring node_bin would pass.
  • Environment dependence: the suite throws if mise lives in /usr/bin, shells out to the real mise (:381-387, unlike sibling suites), and sets no spawnSync timeout across about 80 child processes.
  • The registry-install regex (:407) misses npx @abca/cli and npm i @abca/cli -g, and would flag prose that warns against the command.

Plugin skills

  • deploy/SKILL.md and setup/SKILL.md lack the "never pass --force without asking" sentence the other four carry.
  • "Run mise //cli:compile from the repo root" should name the path the command prints, not the working directory.

Documentation

  • Guides, README, AGENTS.md, CONTRIBUTING.md and 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-62 and 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.ts 27/27, the cli version and setup suites pass; mise run cli:install --force forwards --force (foreign file refused without it, replaced with it); the installed command printed 0.0.0 (6040eab0d19f) from another directory. Full mise run build not 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-variant Plan union 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 real mise (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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(cli): README install command 404s — @backgroundagent/cli is unpublished and misnamed, and --version is a hardcoded 0.0.0

2 participants