Files
hyperframes/packages/cli/src/utils/dailyNotice.ts
T
Wenbo ZhuandClaude Opus 5.5 7cb6018c6b feat(cli): surface outdated CLI and skills to agents; add hyperframes update --check (#7094)
* feat(cli): surface outdated CLI and skills to agents; add hyperframes update

Single commit of #7094 on top of main (no merge commits through the signed
GraphQL path). Includes: 2h update/skills check interval and stale-cache
recheck; `_meta.checkedAt`, `_meta.skillsOutdated`, `_meta.skillsMissing`;
`_meta.updateNotice`, one sentence for the agent to relay to the person, at
most once a day per project (brew/npx-aware command, omitted under CI, the
update-check opt-out and agent plugins); `hyperframes update [--check]
[--yes] [--json]` (CLI then skills at the installed CLI's tag; brew
version read back); doctor names the running binary and its channel; the
hyperframes-cli skill gets one line to pass the notice on, naming no
command. Keeps main's install-target skills check (#7120) in updateSkills.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(cli): update notice names a command that moves this install

- `_meta.updateNotice` (and `update --check`'s skills command) now name a
  command that moves the running install: a global npm/pnpm/bun/brew CLI
  runs its own `hyperframes update` (or just its manager's command when
  only the CLI is behind), since `npx hyperframes` would run a project's
  copy or a fresh npx one; a project-local CLI moves with its project's
  manager by lockfile (`npm install`/`pnpm add`/`bun add`/`yarn add
  hyperframes@<v>`), which `update --yes` now names too instead of
  `npx hyperframes@latest`.
- `update`'s confirmation hint states a fact and names no step for the
  agent: "Updating is the person's call; `update --check` only reports."
- `upgrade --json`, `upgrade --project --json` and every `skills` JSON
  output leave out `_meta.updateNotice` (NO_UPDATE_NOTICE), like `update`.
- dailyNotice: a null/non-object file reads as empty and is repaired; a
  future timestamp is stale; writes go through a 0600 tmp file + rename;
  withMeta never throws from the notice.
- Docs: --json without --yes exits 2; plugin `update` exits 1
  (plugin_managed); full `_meta` sample; per-install notice commands.
  `update --check` says "npm registry not reached" instead of "latest".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 09:31:52 +00:00

43 lines
1.7 KiB
TypeScript

import { mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { CONFIG_PATH } from "../telemetry/config.js";
const NOTICE_INTERVAL_MS = 24 * 60 * 60 * 1000;
/** The recorded showings; anything that is not an object of numbers reads as none. */
function readShown(noticeFile: string): Record<string, number> {
let parsed: unknown;
try {
parsed = JSON.parse(readFileSync(noticeFile, "utf-8"));
} catch {
return {};
}
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return {};
return Object.fromEntries(
Object.entries(parsed).filter((entry): entry is [string, number] => Number.isFinite(entry[1])),
);
}
/** Shown within the last day. A time ahead of the clock is not trusted, so it reads as stale. */
const recent = (at: number, now: number) => at <= now && now - at < NOTICE_INTERVAL_MS;
/** True at most once a day per project for this kind of notice, recorded in `<kind>-notice.json`. */
export function claimDailyNotice(kind: string, project: string, now = Date.now()): boolean {
const noticeFile = join(dirname(CONFIG_PATH), `${kind}-notice.json`);
const shown = readShown(noticeFile);
if (recent(shown[project] ?? 0, now)) return false;
const kept = Object.entries(shown).filter(([, at]) => recent(at, now));
const tmp = `${noticeFile}.${process.pid}.tmp`;
try {
mkdirSync(dirname(noticeFile), { recursive: true, mode: 0o700 });
writeFileSync(tmp, JSON.stringify(Object.fromEntries([...kept, [project, now]])), {
mode: 0o600,
});
renameSync(tmp, noticeFile);
} catch {
rmSync(tmp, { force: true });
return false;
}
return true;
}