Skip to content

fix: keep self-update off npm link checkouts, and report hook injection only on change (#871) - #874

Merged
jeff-r2026 merged 5 commits into
Tencent:mainfrom
SaulMoro:fix/linked-self-update
Sep 29, 2026
Merged

jeff-r2026 merged 5 commits into
Tencent:mainfrom
SaulMoro:fix/linked-self-update

Conversation

@SaulMoro

@SaulMoro SaulMoro commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes the two bugs in #871.

1. Self-update no longer replaces an npm link checkout. When the running CLI is outside node_modules, resolveInstallPrefix() returns null. doUpdate then ran npm install -g without --prefix, which lands in npm's default global prefix, where npm link put its symlink. Now it refuses the install before prompting:

 doUpdate()                                    src/update.ts
   checkForUpdate()
   policy === 'skip' → return
+  target = resolveInstallPrefix()
+  target is null (linked checkout, running from source)
+    warn + persist to debug.log: "pull and rebuild it, or npm install -g <pkg> --registry=<registry>"
+    return
+  vendored flat layout → warn + persist, return (moved up from after the prompt)
   policy === 'prompt' → ask "Update now?"
   acquireLock()
-  target = resolveInstallPrefix()
-  vendored flat layout → warn, return
-  npm install -g <pkg> [--prefix only when target is set]
+  npm install -g <pkg> --prefix=<target.prefix>

Both refusals also go to debug.log, because the hook commands send stderr to /dev/null. Before, the vendored one left no trace when a hook hit it.

2. pull reports a hook injection only when a file changed. The adapters in src/hooks.ts already compare before writing. The file-based injectors rewrote their files and logged ✔ Injected … on every pull. They now use the existing writeIfChanged helper and log at debug level when nothing changed:

 injectOmpHooks / injectOpencodeHooks / injectPiHooks / injectOpenClawHooks
-  writeFile(file, source)
-  log.success('Injected …')
+  if (await writeIfChanged(file, source)) log.success('Injected …')
+  else log.debug('… already up-to-date')

 injectHermesHooks
+  scriptChanged    = writeIfChanged(script)
+  hookChanged      = upsertHermesHook(...)       // now returns whether it wrote
+  allowlistChanged = addHermesAllowlist(...)     // same
+  log.success only if any of the three is true

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature causing existing behavior to change)
  • Documentation only
  • Refactor / internal cleanup

Test Plan

  • npx tsc --noEmit passes
  • npm run lint passes (0 warnings)
  • npx vitest run passes: 337 files, 5252 passed, 1 skipped
  • Added/updated tests for the change

New tests, each red before its fix:

update.test.ts
  doUpdate from <home>/u/dev/teamai-cli/dist/index.js (no node_modules)
    → only the version check runs, no `npm install`; warns and persists "Self-update skipped"
  same, policy prompt + TTY
    → askConfirmation is never called
  POSIX vendored layout (existing test, new assertion)
    → log.persist gets "Self-update is not supported"
{omp,opencode,pi,openclaw,hermes}-hooks.test.ts
  inject twice with identical content
    → log.success called on the first run only

The existing doUpdate tests ran through the null-target path, because the suite runs from this checkout. They now point fileURLToPath at a real <tmp>/lib/node_modules/teamai-cli layout.

Real CLI after npm run build (macOS, Node 24.21.0; the global teamai is the published v0.25.0 copy that replaced my link):

  • Before: where npm link had put a symlink, $(npm root -g)/teamai-cli was a directory with package.json 0.25.0 and the mtime of a hook run. The Stop hook printed the old /teamai-share-learnings hint.
    After: node dist/index.js update from this worktree:

    ⚠ Self-update skipped: teamai is running from …/fix-linked-self-update, which is not an npm install it can update in place (for example an `npm link` checkout). For a checkout, pull and rebuild it; to switch to the published package, run "npm install -g teamai-cli --registry=https://registry.npmjs.org".
    

    The global install's shasum is the same before and after, and ~/.teamai/debug.log has the [WARN] Self-update skipped line.

  • Before: teamai pull with the published v0.25.0 on an up-to-date sandbox (isolated HOME, local bare git team repo, ~/.omp and ~/.config/opencode present):

    ✔ [user] Team repo: already up to date
    ✔ [user] Already synced at 2750e19, skipping
    ✔ Injected teamai OMP hook into <home>/.omp/agent/extensions/teamai-hooks.ts
    ✔ Injected teamai OpenCode hook into <home>/.config/opencode/plugin/teamai-hooks.ts
    

    After: the same sandbox, this build. The first pull prints the Claude Updated, OMP and OpenCode Injected lines. The second prints only:

    ✔ [user] Team repo: already up to date
    ✔ [user] Already synced at 2750e19, skipping
    

    pull --verbose shows [debug] teamai OMP hook already up-to-date … and the same line for OpenCode, next to the existing Claude line.

Not run: Hermes, OpenClaw and Pi under the real CLI, because they are not installed here. Their unit tests use real files in tmp dirs.

Related Issues

Closes #871

Notes for Reviewers

Door: two-way. No data or config format changes. Reverting restores the old install and logging behavior.

Blast Radius: narrow.

  • Self-update now does nothing for any install that prefixFromEntryPath cannot attribute. Before, it installed into npm's default global prefix. Anyone who relied on that is on an unusual layout, and they now get the warning with the exact command to run instead.
  • Under updatePolicy: prompt, the vendored-layout refusal now comes before the question instead of after the user answers yes. It is also persisted to debug.log, so a vendored install on a Stop hook adds a WARN line per run, the same as a linked checkout.
  • upsertHermesHook and addHermesAllowlist now return Promise<boolean>. Their other caller (applyHermesAgentHook) ignores the value.
  • The OMP, OpenCode, Pi and OpenClaw injectors no longer call ensureDir separately, because writeIfChanged creates the parent directory itself.

Both bugs ship in one PR because #871 reports them together. The commits keep them apart: update, hooks, one review follow-up for each, and the vendored persist from an adversarial review pass (verdict approve, that one low finding).

… with the registry package

When the running CLI is not under node_modules (an npm link checkout),
resolveInstallPrefix returns null and doUpdate ran a prefix-less
npm install -g. That lands in npm's default global prefix, where npm link
put its symlink, so the next hook run swapped the checkout for the
published package. Skip the install and say how to update instead.
…file changes

These injectors rewrote their file and logged success on every pull, so
an up-to-date pull listed them as injected while Claude and Codex, which
already compare before writing, printed nothing. Use writeIfChanged and
log at debug level when the file is current.
… the skip in debug.log

Review follow-ups: resolve the install target before the prompt policy so
nobody confirms an update that is then skipped; persist the skip warning
because hooks discard stderr; word it for every null target, not only a
link; tell the user to pull before rebuilding; pass --registry in the
suggested install command.
…hing changes

Both are reached by the pull reconcile and still logged success on every
run. OpenClaw now writes its two files with writeIfChanged; Hermes also
reports whether its config.yaml entry and allowlist were written.
Adversarial review follow-up: the Stop hook discards stderr, so the
vendored-layout refusal left no trace while the linked-checkout refusal
next to it is persisted.
@jeff-r2026 jeff-r2026 self-assigned this Sep 28, 2026
@github-actions

Copy link
Copy Markdown
  • [P3 nit] src/hermes-hooks.ts:58 does not count an executable-mode repair as a change. If the script content, config, and allowlist are current but its execute bit was removed, chmod repairs the hook while the command logs “already up-to-date” and suppresses the injection success message.

The PR description includes sufficient unit, lint, build, and representative real-CLI verification.

@jeff-r2026
jeff-r2026 merged commit facc610 into Tencent:main Sep 29, 2026
14 checks passed
Walternater pushed a commit to Walternater/teamai-cli that referenced this pull request Nov 2, 2026
…on only on change (Tencent#871) (Tencent#874)

* fix(update): leave an npm link checkout alone instead of replacing it with the registry package

When the running CLI is not under node_modules (an npm link checkout),
resolveInstallPrefix returns null and doUpdate ran a prefix-less
npm install -g. That lands in npm's default global prefix, where npm link
put its symlink, so the next hook run swapped the checkout for the
published package. Skip the install and say how to update instead.

* fix(hooks): report OMP, OpenCode and Pi hook injection only when the file changes

These injectors rewrote their file and logged success on every pull, so
an up-to-date pull listed them as injected while Claude and Codex, which
already compare before writing, printed nothing. Use writeIfChanged and
log at debug level when the file is current.

* fix(update): refuse an unsupported install before prompting, and keep the skip in debug.log

Review follow-ups: resolve the install target before the prompt policy so
nobody confirms an update that is then skipped; persist the skip warning
because hooks discard stderr; word it for every null target, not only a
link; tell the user to pull before rebuilding; pass --registry in the
suggested install command.

* fix(hooks): report Hermes and OpenClaw hook injection only when something changes

Both are reached by the pull reconcile and still logged success on every
run. OpenClaw now writes its two files with writeIfChanged; Hermes also
reports whether its config.yaml entry and allowlist were written.

* fix(update): persist the vendored-install skip to debug.log too

Adversarial review follow-up: the Stop hook discards stderr, so the
vendored-layout refusal left no trace while the linked-checkout refusal
next to it is persisted.
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.

[bug] Self-update replaces an npm link checkout with the published package, and pull reports OMP/OpenCode hook injection on every run

2 participants