mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-09-14 18:01:20 +08:00
5db554c7b6
## Summary
Today users have to run `hyperframes upgrade` (or the right install command for their package manager) to get a new release — we ship fixes but they don't reach the install until the user remembers. This PR borrows the Claude Code model: detect the update on run N, install it in a detached background child, surface one line ("hyperframes auto-updated to vX.Y.Z") on run N+1. The user's current command never blocks, never prompts, never sees an install stream.
## Flow across two runs
```
Run N → checkForUpdate() sees latest > current → spawn detached
child running `npm install -g hyperframes@X` (or bun /
pnpm / brew equivalent). Parent exits immediately.
(between) → detached child installs, writes completedUpdate into
~/.hyperframes/config.json, clears pendingUpdate.
Run N+1 → reportCompletedUpdate() prints one line and clears the
marker. User is on the new version.
```
## Installer detection
Walks `realpathSync(process.argv[1])` against each package manager's well-known global prefix. Wrong guesses are biased toward `skip` — we'd rather miss an auto-update than clobber a Homebrew install with npm.
| Resolved entry path contains | Detected as | Install command |
|---|---|---|
| `…/Cellar/hyperframes/<v>/…` | `brew` | `brew upgrade hyperframes` |
| `…/.bun/…` | `bun` | `bun add -g hyperframes@<v>` |
| `…/pnpm/global/…` or `…/.pnpm/…` | `pnpm` | `pnpm add -g hyperframes@<v>` |
| `…/lib/node_modules/hyperframes/…` | `npm` | `npm install -g hyperframes@<v>` |
| `…/packages/cli/…` (workspace link) | `skip` | (no-op) |
| `…/_npx/…`, `…/bunx-…/…` | `skip` | (no-op) |
| Anything else | `skip` | (no-op) |
## Guardrails
- **Never auto-update across a major version.** The existing banner still nudges the user to run `hyperframes upgrade` explicitly.
- **Skip on CI, non-TTY, dev mode,** npx / bunx / workspace link, or any install layout the detector doesn't recognize.
- **`HYPERFRAMES_NO_AUTO_INSTALL=1`** disables the install without silencing the notice banner.
- **`HYPERFRAMES_NO_UPDATE_CHECK=1`** silences both (existing knob).
- **Fresh pending install (<10 min old)** prevents re-launch on every invocation.
- **Installer stdout + stderr go to `~/.hyperframes/auto-update.log`** for postmortem — the terminal stays clean.
- **Failed installs are surfaced once** with a prompt to run `hyperframes upgrade` manually.
## What changed
| File | Role |
|---|---|
| `packages/cli/src/utils/installerDetection.ts` | Classifies the running install → npm \| bun \| pnpm \| brew \| skip, with the right install command. |
| `packages/cli/src/utils/autoUpdate.ts` | `scheduleBackgroundInstall` + `reportCompletedUpdate`. Spawns a detached `node -e "..."` child that runs the install and writes the outcome back to the config, then `unref()`s so the parent exits immediately. |
| `packages/cli/src/telemetry/config.ts` | `pendingUpdate` + `completedUpdate` fields on the config schema. |
| `packages/cli/src/cli.ts` | Wires `reportCompletedUpdate()` at startup and `scheduleBackgroundInstall()` after `checkForUpdate()` resolves. |
## Verification
### Unit tests — 19 / 19 pass (full CLI suite 115 / 115)
- `installerDetection.test.ts` — 9 cases, one per layout (workspace, npx, bunx, brew, bun, pnpm, npm, unknown, unresolved).
- `autoUpdate.test.ts` — 10 scheduling-policy cases:
- Minor/patch → schedules + writes pendingUpdate
- Major bump → **does not** schedule
- Dev mode → skipped
- `CI=1` → skipped
- `HYPERFRAMES_NO_AUTO_INSTALL=1` → skipped
- Unknown installer → skipped
- Already-on-latest → skipped
- Fresh pending install → de-duplicated
- Stale pending install (>10 min) → supersedes
- Previous run already completed this version → skipped
Unit tests mock `spawn` and the installer — they verify the **policy**, not the real detached-child path.
### Live end-to-end smoke test (on this Mac, real processes)
To validate the parts the unit tests can't — actual detached spawn, real config writeback, banner surfacing in a fresh subsequent process — I wired a smoke script that exercises the exact same code path `autoUpdate.ts` uses, but with `echo …` as the "install command" so nothing global gets touched.
**Steps exercised:**
1. Backed up the user's real `~/.hyperframes/config.json`.
2. Wrote a `pendingUpdate` marker for version `0.4.99` (like `scheduleBackgroundInstall` does).
3. Spawned the **exact same detached `node -e "..."` child** the real scheduler produces, with the install command replaced by `echo 'faux install for 0.4.99'`.
4. The parent `unref()`d and continued; 800 ms later the parent re-read `config.json`.
5. Ran `reportCompletedUpdate()` in a **fresh subprocess** (via `bunx tsx -e ...`) to match the real "Run N+1" conditions, capturing its stderr.
6. Asserted the marker was cleared.
7. Restored the original config on exit.
**Observed output:**
```
[setup] Backed up config to /Users/miguel/.hyperframes/config.json.smoke-backup
[setup] Wrote pendingUpdate for v0.4.99
[spawn] Detached child pid=49469
[after] completedUpdate = {"version":"0.4.99","ok":true,"finishedAt":"2026-04-17T17:26:50.115Z"}
[after] pendingUpdate = (cleared)
✓ detached spawn + writeback verified
[banner-subprocess] stderr: "hyperframes auto-updated to v0.4.99"
✓ banner fired in fresh process + marker cleared
ALL CHECKS PASSED ✓
[cleanup] Config restored
```
**What this proves:**
| Claim | Evidence |
|---|---|
| Detached spawn works (doesn't block the parent) | `[spawn] pid=49469` logged, parent continued immediately |
| Detached child is process-independent | Parent exited its own work while child ran `exec(CMD)` |
| Child writes correct config shape | `completedUpdate = { version: "0.4.99", ok: true, finishedAt: … }` |
| Child clears the pending marker | `pendingUpdate = (cleared)` |
| Banner fires only in a fresh process | Subprocess stderr = `"hyperframes auto-updated to v0.4.99"` |
| Banner message format | Matches the copy in `autoUpdate.ts:reportCompletedUpdate` exactly |
| Marker clears after banner | Second file read shows `completedUpdate` absent |
Both the original test-plan checkboxes (fresh install, `HYPERFRAMES_NO_AUTO_INSTALL=1`, `CI=1`) are covered by either the unit-test suite or this smoke test — the scheduling-policy gates are unit-tested under `CI=true`, and the real detached-spawn path is smoke-tested above.
### What's still worth doing
- **Physical installer test on a real `npm i -g` / `brew` / `bun add -g` environment** — the smoke test above replaces the install command with `echo`, so we've never actually seen npm/bun/brew run the real command. That's the one remaining unknown. Worth one manual run on the maintainer's machine before cutting v0.4.4.
## Test plan
- [x] `bunx vitest run` on `packages/cli` — 115 / 115 pass (incl. 19 new)
- [x] `tsc --noEmit` clean
- [x] `tsup` build clean
- [x] **Live e2e smoke test** exercising the real detached spawn + config writeback + fresh-process banner (output above)
- [x] CI green on this branch (Typecheck, Test, Test: runtime contract, Build, Lint, Format)
- [ ] One manual run on a physical `npm i -g hyperframes@0.4.2` install to confirm the real `npm install -g hyperframes@0.4.3` command actually runs when `autoUpdate.ts` delegates to it (the smoke test stopped short of executing `npm`)
## Notes
- Independent of any version bump — ship whenever.
- The existing `checkForUpdate` + `printUpdateNotice` still work unchanged; this PR adds a second stage that *applies* the update rather than just telling the user about it.
- `hyperframes upgrade` still exists and is still the right command for explicit upgrades (especially major-version jumps).
128 lines
4.1 KiB
TypeScript
128 lines
4.1 KiB
TypeScript
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import { homedir } from "node:os";
|
|
import { randomUUID } from "node:crypto";
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Config directory: ~/.hyperframes/
|
|
// ---------------------------------------------------------------------------
|
|
|
|
const CONFIG_DIR = join(homedir(), ".hyperframes");
|
|
const CONFIG_FILE = join(CONFIG_DIR, "config.json");
|
|
|
|
export interface HyperframesConfig {
|
|
/** Whether anonymous telemetry is enabled (default: true in production) */
|
|
telemetryEnabled: boolean;
|
|
/** Stable anonymous identifier — no PII, just a random UUID */
|
|
anonymousId: string;
|
|
/** Whether the first-run telemetry notice has been shown */
|
|
telemetryNoticeShown: boolean;
|
|
/** Total CLI command invocations (for engagement prompts) */
|
|
commandCount: number;
|
|
/** ISO timestamp of the last npm registry version check */
|
|
lastUpdateCheck?: string;
|
|
/** Latest version found on npm */
|
|
latestVersion?: string;
|
|
/**
|
|
* Auto-update marker. Set when a background install is spawned so a
|
|
* subsequent run can skip re-triggering it. Cleared once
|
|
* `completedUpdate` captures the outcome.
|
|
*/
|
|
pendingUpdate?: {
|
|
/** Version being installed. */
|
|
version: string;
|
|
/** Install command being run, for debug logging. */
|
|
command: string;
|
|
/** ISO timestamp of when the background install was launched. */
|
|
startedAt: string;
|
|
};
|
|
/**
|
|
* Outcome of the last completed auto-update, written by the detached
|
|
* installer. Surfaced once in the next invocation and then cleared.
|
|
*/
|
|
completedUpdate?: {
|
|
version: string;
|
|
/** Whether the install succeeded. */
|
|
ok: boolean;
|
|
/** ISO timestamp of when the installer finished. */
|
|
finishedAt: string;
|
|
/** Non-empty when `ok === false` — the installer's stderr tail. */
|
|
error?: string;
|
|
/** True after the result has been surfaced once to the user. */
|
|
reported?: boolean;
|
|
};
|
|
}
|
|
|
|
const DEFAULT_CONFIG: HyperframesConfig = {
|
|
telemetryEnabled: true,
|
|
anonymousId: "",
|
|
telemetryNoticeShown: false,
|
|
commandCount: 0,
|
|
};
|
|
|
|
let cachedConfig: HyperframesConfig | null = null;
|
|
|
|
/**
|
|
* Read the config file, creating it with defaults if it doesn't exist.
|
|
* Returns a mutable copy — call `writeConfig()` to persist changes.
|
|
*/
|
|
export function readConfig(): HyperframesConfig {
|
|
if (cachedConfig) return { ...cachedConfig };
|
|
|
|
if (!existsSync(CONFIG_FILE)) {
|
|
const config = { ...DEFAULT_CONFIG, anonymousId: randomUUID() };
|
|
writeConfig(config);
|
|
return config;
|
|
}
|
|
|
|
try {
|
|
const raw = readFileSync(CONFIG_FILE, "utf-8");
|
|
const parsed = JSON.parse(raw) as Partial<HyperframesConfig>;
|
|
|
|
const config: HyperframesConfig = {
|
|
telemetryEnabled: parsed.telemetryEnabled ?? DEFAULT_CONFIG.telemetryEnabled,
|
|
anonymousId: parsed.anonymousId || randomUUID(),
|
|
telemetryNoticeShown: parsed.telemetryNoticeShown ?? DEFAULT_CONFIG.telemetryNoticeShown,
|
|
commandCount: parsed.commandCount ?? DEFAULT_CONFIG.commandCount,
|
|
lastUpdateCheck: parsed.lastUpdateCheck,
|
|
latestVersion: parsed.latestVersion,
|
|
pendingUpdate: parsed.pendingUpdate,
|
|
completedUpdate: parsed.completedUpdate,
|
|
};
|
|
|
|
cachedConfig = config;
|
|
return { ...config };
|
|
} catch {
|
|
// Corrupted config — reset
|
|
const config = { ...DEFAULT_CONFIG, anonymousId: randomUUID() };
|
|
writeConfig(config);
|
|
return config;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Persist config to disk. Updates the in-memory cache.
|
|
*/
|
|
export function writeConfig(config: HyperframesConfig): void {
|
|
try {
|
|
mkdirSync(CONFIG_DIR, { recursive: true, mode: 0o700 });
|
|
writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2) + "\n", { mode: 0o600 });
|
|
cachedConfig = { ...config };
|
|
} catch {
|
|
// Non-fatal — telemetry should never break the CLI
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Increment the command counter and persist.
|
|
*/
|
|
export function incrementCommandCount(): number {
|
|
const config = readConfig();
|
|
config.commandCount++;
|
|
writeConfig(config);
|
|
return config.commandCount;
|
|
}
|
|
|
|
/** Expose the config directory path for the telemetry command output */
|
|
export const CONFIG_PATH = CONFIG_FILE;
|