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,CAAK,YAAA;AAAA,QACnB,WAAA,EAAa;AAAA,OACf;AAAA,MACA;AAAA,KACF;AAAA,EACF;AAEA,EAAA,OAAO;AAAA,IACL,OAAO,GAAA,CAAI,KAAA;AAAA,IACX,QAAQ,GAAA,CAAI,MAAA;AAAA,IACZ,WAAW,GAAA,CAAI,SAAA;AAAA,IACf,cAAc,IAAA,CAAK;AAAA,GACrB;AACF","file":"release-check.mjs","sourcesContent":["/**\n * Release check for `@agentproto/cli` (and the daemon it carries) — shared\n * between the CLI (`agentproto daemon status` fold) and the VS Code extension\n * (the release status-bar indicator + update prompt). Both packages already\n * depend on `@agentproto/runtime`, so it's the natural home for the ONE copy\n * of the decision + IO, rather than a vscode-private module the CLI couldn't\n * reach.\n *\n * Two halves:\n *  - the pure decision logic (`.`-version comparison, cache freshness, the\n *    offline-safe `resolveReleaseCheck`) — no import beyond the standard\n *    library, unit-testable without network/home/extension;\n *  - the IO half (npm registry fetch + the `~/.agentproto/release-check.json`\n *    versioned cache) — plain Node, no vscode import, runnable in tests.\n *\n * Contract (from .plans/release-update-indicator-PLAN.md, WP-A):\n *  - The signal \"a new release exists\" is the npm registry for\n *    `@agentproto/cli` (the CLI carries the daemon; `health.version` IS the\n *    CLI version, so the daemon inherits it).\n *  - Two comparison modes by `build.source` read on the daemon's /health:\n *      - `tarball` / absent → compare `health.version` against npm latest.\n *      - `workspace` → signal `workspace` distinctly: the real update for a\n *        workspace install is a local REBUILD, not an npm reinstall.\n *  - Offline-safe: never claim an update we didn't verify. When the npm fetch\n *    failed and the cache is stale, the answer is `unknown` — never a false\n *    \"update dispo\" off a stale number.\n */\n\nimport { promises as fs } from \"node:fs\"\nimport { homedir } from \"node:os\"\nimport { dirname, join } from \"node:path\"\n\n/** Cache file version — bump when the on-disk shape changes. */\nexport const RELEASE_CHECK_CACHE_VERSION = 1\n\n/** Default npm poll TTL (ms). ~1 h, never below the 10 min floor. */\nexport const RELEASE_CHECK_DEFAULT_TTL_MS = 60 * 60 * 1000\nexport const RELEASE_CHECK_MIN_TTL_MS = 10 * 60 * 1000\n\n/** Build provenance, from the daemon's health.build.source. */\nexport type ReleaseBuildSource = \"workspace\" | \"tarball\" | null\n\n/**\n * What the release indicator reports. `workspace` is deliberately distinct\n * from `current`: both mean \"nothing to install via npm\", but `workspace`\n * additionally tells the display to frame the update as a rebuild.\n */\nexport type ReleaseState = \"current\" | \"behind\" | \"unknown\" | \"workspace\"\n\n/** Persistent cache — the offline fallback. Versioned like the other\n *  `~/.agentproto/*.json` stores (`{ version: N, ... }`). */\nexport interface ReleaseCheckCache {\n  /** Cache-file shape version. Must equal RELEASE_CHECK_CACHE_VERSION to be\n   *  trusted; a mismatch (older/newer on-disk shape) is treated as no cache. */\n  version: number\n  /** Latest `@agentproto/cli` observed on npm. Null while unknown. */\n  latest: string | null\n  /** Local CLI version at the time of the check that produced `latest`. */\n  localVersion: string | null\n  /** Epoch ms of the last successful npm fetch. */\n  checkedAtMs: number\n}\n\n/**\n * Compare two dotted versions numerically. Returns a positive integer when\n * `a > b`, negative when `a < b`, 0 when equal or unparseable. Accepts\n * optional `v` prefix and trailing prerelease/build suffix.\n */\nexport function compareVersions(a: string, b: string): number {\n  const parse = (v: string): { parts: number[]; ok: boolean } => {\n    const parts =\n      v\n        .replace(/^v/i, \"\")\n        .split(\"-\")[0]\n        ?.split(\".\")\n        .map(Number)\n        .filter(n => Number.isFinite(n)) ?? []\n    // A version that yields no numeric component (e.g. \"abc\") can't be\n    // compared — treat the whole comparison as inconclusive rather than guess.\n    return { parts, ok: parts.length > 0 }\n  }\n  const pa = parse(a)\n  const pb = parse(b)\n  if (!pa.ok || !pb.ok) return 0\n  const len = Math.max(pa.parts.length, pb.parts.length)\n  for (let i = 0; i < len; i++) {\n    const da = pa.parts[i] ?? 0\n    const db = pb.parts[i] ?? 0\n    if (da !== db) return da - db\n  }\n  return 0\n}\n\n/**\n * The WP-A decision. Returns:\n *  - `unknown`   — no usable `latestVersion` (network error / no cache → the\n *                  offline-safe answer; never a fabricated \"behind\").\n *  - `workspace` — the daemon is served from a workspace build: the real\n *                  update is a rebuild, so this is its own state, not a\n *                  plain `behind`.\n *  - `behind`    — `buildSource` is a published tarball and npm is ahead.\n *  - `current`   — tarball and npm is not ahead.\n */\nexport function compareRelease(\n  localVersion: string | null | undefined,\n  latestVersion: string | null | undefined,\n  buildSource: ReleaseBuildSource,\n): ReleaseState {\n  if (!localVersion || !latestVersion) return \"unknown\"\n  if (buildSource === \"workspace\") return \"workspace\"\n  if (compareVersions(latestVersion, localVersion) > 0) return \"behind\"\n  return \"current\"\n}\n\n/** TTL, clamped to the 10 min floor. */\nexport function releaseTtlMs(intervalMin: number | undefined): number {\n  const min = intervalMin && intervalMin > 0 ? intervalMin : RELEASE_CHECK_DEFAULT_TTL_MS / 60_000\n  return Math.max(min * 60_000, RELEASE_CHECK_MIN_TTL_MS)\n}\n\n/** True when the cache is young enough to trust without a fresh fetch. */\nexport function isCacheFresh(\n  cache: ReleaseCheckCache | null,\n  nowMs: number,\n  ttlMs: number,\n): boolean {\n  if (!cache || cache.version !== RELEASE_CHECK_CACHE_VERSION) return false\n  if (typeof cache.checkedAtMs !== \"number\" || Number.isNaN(cache.checkedAtMs)) return false\n  return nowMs - cache.checkedAtMs <= ttlMs\n}\n\nexport interface ResolveCheckInput {\n  localVersion: string | null\n  buildSource: ReleaseBuildSource\n  /** Npm result from a fresh fetch. Null when the network fetch failed or was\n   *  skipped because the cache was already fresh. */\n  latestFromNpm: string | null\n  cache: ReleaseCheckCache | null\n  nowMs: number\n  ttlMs: number\n}\n\nexport interface ResolveCheckResult {\n  state: ReleaseState\n  /** The `@agentproto/cli` latest we trust, if any (fresh npm or fresh cache). */\n  latest: string | null\n  /** Whether `latest` came from the cache rather than a live fetch. */\n  fromCache: boolean\n  /** Cached latest (may be stale) — informational only, never drives state. */\n  cachedLatest: string | null\n}\n\n/**\n * Resolve the full check from its inputs. This is the pure, offline-safe\n * core: a fresh npm result wins; otherwise a fresh cache is trusted (this is\n * the \"return the cache if the TTL hasn't expired\" fallback); otherwise the\n * answer is `unknown` — a stale cache never fabricates a \"behind\".\n */\nexport function resolveReleaseCheck(input: ResolveCheckInput): ResolveCheckResult {\n  const freshCache = isCacheFresh(input.cache, input.nowMs, input.ttlMs)\n  if (input.latestFromNpm) {\n    return {\n      state: compareRelease(input.localVersion, input.latestFromNpm, input.buildSource),\n      latest: input.latestFromNpm,\n      fromCache: false,\n      cachedLatest: input.cache?.latest ?? null,\n    }\n  }\n  if (freshCache && input.cache?.latest) {\n    return {\n      state: compareRelease(input.localVersion, input.cache.latest, input.buildSource),\n      latest: input.cache.latest,\n      fromCache: true,\n      cachedLatest: input.cache.latest,\n    }\n  }\n  // Network failed AND the cache is stale or absent → unknown, no false claim.\n  return {\n    state: \"unknown\",\n    latest: null,\n    fromCache: false,\n    cachedLatest: input.cache?.latest ?? null,\n  }\n}\n\n// ── IO half ───────────────────────────────────────────────────────────────\n\n/** `@agentproto/cli` npm package (scoped → `%2F` in the registry URL). */\nconst CLI_PACKAGE = \"@agentproto/cli\"\nconst NPM_LATEST_URL = `https://registry.npmjs.org/${CLI_PACKAGE.replace(\"/\", \"%2F\")}/latest`\n\n/** `~/.agentproto/release-check.json`. */\nexport function releaseCheckCachePath(home: string = homedir()): string {\n  return join(home, \".agentproto\", \"release-check.json\")\n}\n\nexport interface FetchLatestCli {\n  (opts?: { timeoutMs?: number }): Promise<string | null>\n}\n\n/** Fetch the latest published `@agentproto/cli` version from the npm\n *  registry. Returns null on any network error (so the caller can fall back to\n *  the cache / report `unknown`), never throws. */\nexport async function fetchLatestCliVersion(opts: { timeoutMs?: number } = {}): Promise<string | null> {\n  const timeoutMs = opts.timeoutMs ?? 5_000\n  const controller = new AbortController()\n  const timer = setTimeout(() => controller.abort(), timeoutMs)\n  try {\n    const res = await fetch(NPM_LATEST_URL, { signal: controller.signal })\n    if (!res.ok) return null\n    const body = (await res.json()) as { version?: unknown }\n    if (typeof body.version === \"string\" && body.version.length > 0) return body.version\n    return null\n  } catch {\n    return null\n  } finally {\n    clearTimeout(timer)\n  }\n}\n\n/** Read + parse the cache. Returns null when missing, malformed, or of a\n *  version we don't understand. */\nexport async function readReleaseCache(\n  path: string = releaseCheckCachePath(),\n): Promise<ReleaseCheckCache | null> {\n  try {\n    const raw = await fs.readFile(path, \"utf8\")\n    const parsed = JSON.parse(raw) as unknown\n    if (!parsed || typeof parsed !== \"object\" || Array.isArray(parsed)) return null\n    const c = parsed as Partial<ReleaseCheckCache>\n    if (c.version !== RELEASE_CHECK_CACHE_VERSION) return null\n    return {\n      version: RELEASE_CHECK_CACHE_VERSION,\n      latest: typeof c.latest === \"string\" ? c.latest : null,\n      localVersion: typeof c.localVersion === \"string\" ? c.localVersion : null,\n      checkedAtMs: typeof c.checkedAtMs === \"number\" ? c.checkedAtMs : Number.NaN,\n    }\n  } catch {\n    return null\n  }\n}\n\n/** Atomically write the cache (tmp + rename), like the other versioned\n *  `~/.agentproto` stores. */\nexport async function writeReleaseCache(\n  next: ReleaseCheckCache,\n  path: string = releaseCheckCachePath(),\n): Promise<void> {\n  await fs.mkdir(dirname(path), { recursive: true })\n  const tmp = `${path}.tmp`\n  await fs.writeFile(tmp, JSON.stringify(next, null, 2), \"utf8\")\n  await fs.rename(tmp, path)\n}\n\nexport interface ReleaseCheckView {\n  /** Decided state. */\n  state: \"current\" | \"behind\" | \"unknown\" | \"workspace\"\n  /** Latest `@agentproto/cli` version to trust (fresh npm or fresh cache). */\n  latest: string | null\n  /** Whether this run used the cache instead of npm. */\n  fromCache: boolean\n  /** Local CLI version that was compared. */\n  localVersion: string | null\n}\n\nexport interface RunReleaseCheckOptions {\n  localVersion: string | null\n  buildSource: ReleaseBuildSource\n  /** Poll TTL (ms). Pass releaseTtlMs(config) typically. */\n  ttlMs: number\n  nowMs?: number\n  /** Injectable fetcher for tests; defaults to the real npm fetch. */\n  fetchLatest?: FetchLatestCli\n  cachePath?: string\n}\n\n/** Run one release check: consult the cache, fetch npm only when stale, and\n *  persist a successful live result. Offline-safe by construction. */\nexport async function runReleaseCheck(opts: RunReleaseCheckOptions): Promise<ReleaseCheckView> {\n  const nowMs = opts.nowMs ?? Date.now()\n  const path = opts.cachePath ?? releaseCheckCachePath()\n  const fetchLatest = opts.fetchLatest ?? fetchLatestCliVersion\n  const cache = await readReleaseCache(path)\n\n  // Fetch only when the cache can't answer fresh (no cache, wrong version, or\n  // TTL expired). A fresh cache is the offline fallback — do not hit npm.\n  const cacheFresh = cache && typeof cache.checkedAtMs === \"number\" && nowMs - cache.checkedAtMs <= opts.ttlMs\n  const latestFromNpm = cacheFresh ? null : await fetchLatest()\n\n  const res = resolveReleaseCheck({\n    localVersion: opts.localVersion,\n    buildSource: opts.buildSource,\n    latestFromNpm,\n    cache,\n    nowMs,\n    ttlMs: opts.ttlMs,\n  })\n\n  // Persist a live successful result for the next offline window.\n  if (latestFromNpm) {\n    await writeReleaseCache(\n      {\n        version: RELEASE_CHECK_CACHE_VERSION,\n        latest: latestFromNpm,\n        localVersion: opts.localVersion,\n        checkedAtMs: nowMs,\n      },\n      path,\n    )\n  }\n\n  return {\n    state: res.state,\n    latest: res.latest,\n    fromCache: res.fromCache,\n    localVersion: opts.localVersion,\n  }\n}"]}