diff --git a/src/acp-status.ts b/src/acp-status.ts index 784aa6aaf3..ba3b8f276d 100644 --- a/src/acp-status.ts +++ b/src/acp-status.ts @@ -22,7 +22,7 @@ import { coveredRefSpan } from "./decompress-shared.js"; import { preCompactionArchiveOf, statusInputBaseline, type Session } from "./session.js"; import { compressBreakerDetail, compressLastFailureCause } from "./stream.js"; import { describeAdvisory, getAdvisoryState } from "./advisory.js"; -import { getUpdateVisibility } from "./update-notes.js"; +import { getUpdateVisibility, updateBannerPayload } from "./update-notes.js"; import { VERSION, BUILD_COMMIT } from "./version.js"; import { isExternalSummaryBlock } from "./external-summary-marker.js"; import { storageStatusLabel } from "./persist.js"; @@ -412,7 +412,8 @@ export function handleAcpStatus(args: Record, ctx: AcpStatusCtx extra.push(`CRITICAL ADVISORY (instance-level): bili is auto-updating through the self-updater's safety chain — ${describeAdvisory(adv.active, adv.lastError)}. Live state: the advisory field of GET /__bili/status.`); } const upd = getUpdateVisibility(VERSION); - if (upd.visible) { + const updateAction = updateBannerPayload(upd); + if (updateAction) { // #1870 + #1977: the self-updater is a silent courier on native // lanes — the "Restart to finish" log line never reaches // pi/opencode/dsh users, so disk runs new while the process runs old. @@ -423,8 +424,8 @@ export function handleAcpStatus(args: Record, ctx: AcpStatusCtx // exists while auto-update is off. extra.push(""); const lines: string[] = []; - if (upd.pendingRestart) { - lines.push(`CRITICAL UPDATE READY (instance-level): ${upd.diskVersion} downloaded — restart this agent's proxy to finish (running ${upd.runningVersion}).`); + if (updateAction.kind === "ready") { + lines.push(`CRITICAL UPDATE READY (instance-level): ${updateAction.version} downloaded — restart this agent's proxy to finish (running ${upd.runningVersion}).`); } else { lines.push(`CRITICAL UPDATE AVAILABLE (instance-level): release with a critical-tier fix on the channel — running ${upd.runningVersion}.`); } diff --git a/src/update-notes.ts b/src/update-notes.ts index c74b517269..5586ffa4bb 100644 --- a/src/update-notes.ts +++ b/src/update-notes.ts @@ -20,8 +20,8 @@ * old for days. This module keeps the state the visibility surfaces * (acp_status, the /acp panel) read: the running version, the disk version * (restart pending = disk newer than running), and the release notes SPAN — - * every entry strictly newer than the running version, which is exactly - * "what you'll get when you restart" from the USER's point of view. + * every entry strictly newer than the running version. The disk version + * determines which fixes a restart can deliver. * * Fail-open everywhere: an unreachable or malformed source must never degrade * a working proxy. No install side effects here — visibility only. @@ -170,7 +170,7 @@ type UpdateVisibility = { /** Disk version if the watcher saw one (undefined = unknown). */ diskVersion?: string; /** Disk holds a strictly newer version than this process — a restart - * finishes the update. The one action that clears the notice. */ + * loads that version; newer critical fixes may still need installation. */ pendingRestart: boolean; /** Notes for everything between running and the newest known release. */ span: ReleaseNoteEntry[]; @@ -213,19 +213,14 @@ export function getUpdateVisibility(runningVersion: string): UpdateVisibility & * hosts differ (npm -g / opencode plugin / dsh / pi) and the wrong hint is * worse than none — name the version, let the client's own docs say how. */ export function describeUpdateReady(v: UpdateVisibility): string { - const latest = v.span.length > 0 ? v.span[v.span.length - 1].version : v.diskVersion; - const topCritical = v.span.find((e) => e.tier === "critical"); - if (v.pendingRestart && latest !== undefined) { - const notable = topCritical ? ` Critical fix since ${v.runningVersion}: ${topCritical.summary}` : ""; - return `Critical update ready: ${latest} — restart to finish.${notable}`; + const action = updateBannerPayload({ ...v, visible: v.critical }); + if (action?.kind === "ready") { + return `Critical update ready: ${action.version} — restart to finish. Critical fix since ${v.runningVersion}: ${action.summary}`; } - if (topCritical && latest !== undefined) { - // ≥ the MINIMUM version carrying the fix (topCritical = first critical - // in the ascending span): any version at-or-above it is safe, so the - // line must read "≥ X", not "X" — pinning exactly X would be wrong - // advice for hosts whose installer grabbed a newer release. - return `Critical update available: ≥ ${topCritical.version} — ${topCritical.summary}`; + if (action?.kind === "available") { + return `Critical update available: ≥ ${action.version} — ${action.summary}`; } + const latest = v.span.length > 0 ? v.span[v.span.length - 1].version : v.diskVersion; return `Update available: ${latest ?? "newer version"}.`; } @@ -234,7 +229,7 @@ export function describeUpdateReady(v: UpdateVisibility): string { * it sends kind/version/summary and lets i18n compose the sentence * ("upgrade to v{version} or newer", separately translated per locale). * available → version = the MINIMUM version carrying the critical fix - * (first critical in the ascending span; anything ≥ it is safe). + * (first critical not yet installed; anything ≥ it carries that fix). * ready → version = the on-disk version a restart delivers. * null when the span is invisible (#1977: routine/recommended stay silent). */ export function updateBannerPayload(v: UpdateVisibility & { visible: boolean }): { kind: "available" | "ready"; version: string; summary: string; summaryZh?: string } | null { @@ -244,6 +239,9 @@ export function updateBannerPayload(v: UpdateVisibility & { visible: boolean }): const newest = criticals[criticals.length - 1] ?? first; if (first === undefined || newest === undefined) return null; // defensive: visible implies a critical, but stay total if (v.pendingRestart && v.diskVersion !== undefined) { + const diskVersion = v.diskVersion; + const missing = criticals.find((e) => semver.gt(e.version, diskVersion)); + if (missing) return { kind: "available", version: missing.version, summary: missing.summary, ...(missing.summaryZh ? { summaryZh: missing.summaryZh } : {}) }; return { kind: "ready", version: v.diskVersion, summary: newest.summary, ...(newest.summaryZh ? { summaryZh: newest.summaryZh } : {}) }; } return { kind: "available", version: first.version, summary: first.summary, ...(first.summaryZh ? { summaryZh: first.summaryZh } : {}) }; diff --git a/tests/update-notes.test.ts b/tests/update-notes.test.ts index bd7118faf8..31cfc6193c 100644 --- a/tests/update-notes.test.ts +++ b/tests/update-notes.test.ts @@ -363,3 +363,81 @@ test("#1977 /acp panel: one critical-update line BEFORE the footer, stripper-saf _resetSessionsForTest(); } }); + +test("#4295 update action follows installed critical fixes, not pending restart alone", () => { + const entries: ReleaseNoteEntry[] = [ + { version: "0.1.179", tier: "recommended", summary: "recommended" }, + { version: "0.1.180", tier: "critical", summary: "first fix" }, + { version: "0.1.181", tier: "routine", summary: "routine" }, + { version: "0.1.182", tier: "critical", summary: "second fix" }, + { version: "0.1.183", tier: "routine", summary: "future routine" }, + ]; + const cases = [ + { disk: undefined, kind: "available", version: "0.1.180", summary: "first fix", pending: false }, + { disk: "bad", kind: "available", version: "0.1.180", summary: "first fix", pending: false }, + { disk: "0.1.177", kind: "available", version: "0.1.180", summary: "first fix", pending: false }, + { disk: "0.1.178", kind: "available", version: "0.1.180", summary: "first fix", pending: false }, + { disk: "0.1.179", kind: "available", version: "0.1.180", summary: "first fix", pending: true }, + { disk: "0.1.180", kind: "available", version: "0.1.182", summary: "second fix", pending: true }, + { disk: "0.1.181", kind: "available", version: "0.1.182", summary: "second fix", pending: true }, + { disk: "0.1.182", kind: "ready", version: "0.1.182", summary: "second fix", pending: true }, + { disk: "0.1.184", kind: "ready", version: "0.1.184", summary: "second fix", pending: true }, + ] as const; + try { + for (const c of cases) { + _resetReleaseNotesWatcherForTest(); + _setReleaseNotesStateForTest({ entries, ...(c.disk === undefined ? {} : { diskVersion: c.disk }) }); + const v = getUpdateVisibility("0.1.178"); + assert.equal(v.pendingRestart, c.pending, String(c.disk)); + assert.deepEqual(updateBannerPayload(v), { kind: c.kind, version: c.version, summary: c.summary }, String(c.disk)); + const line = describeUpdateReady(v); + assert.ok(line.startsWith(c.kind === "ready" + ? `Critical update ready: ${c.version} — restart to finish.` + : `Critical update available: ≥ ${c.version} —`), line); + assert.ok(line.includes(c.summary), line); + assert.equal(line, describeUpdateReady(v), "stable repeated rendering"); + } + _setReleaseNotesStateForTest({ entries: entries.filter((e) => e.tier !== "critical"), diskVersion: "0.1.184" }); + assert.equal(updateBannerPayload(getUpdateVisibility("0.1.178")), null, "routine/recommended updates stay silent"); + } finally { + _resetReleaseNotesWatcherForTest(); + } +}); + +test("#4295 status and panel agree when only some critical updates are installed", async () => { + const { handleAcpStatus } = await import("../src/acp-status.ts"); + const { handlePluginStatus } = await import("../src/plugin.ts"); + const { getSession, _resetSessionsForTest } = await import("../src/session.ts"); + const next3 = semver.inc(NEXT2, "patch")!; + const next4 = semver.inc(next3, "patch")!; + _resetSessionsForTest(); + getSession("upd-sess", { protocol: "anthropic", label: "UPD" }); + const entries: ReleaseNoteEntry[] = [ + { version: NEXT, tier: "recommended", summary: "recommended" }, + { version: NEXT2, tier: "critical", summary: "first fix" }, + { version: next3, tier: "critical", summary: "second fix" }, + { version: next4, tier: "routine", summary: "future routine" }, + ]; + try { + for (const disk of [NEXT, NEXT2, next3]) { + _resetReleaseNotesWatcherForTest(); + _setReleaseNotesStateForTest({ entries, diskVersion: disk }); + const ready = disk === next3; + const version = disk === NEXT ? NEXT2 : next3; + const status = handleAcpStatus({}, makeCtx12()).text; + assert.ok(status.includes(ready + ? `CRITICAL UPDATE READY (instance-level): ${disk} downloaded` + : "CRITICAL UPDATE AVAILABLE (instance-level):"), status); + assert.equal(status.includes("UPDATE READY"), ready, "restart must deliver the critical fixes"); + const res = mockRes(); + handlePluginStatus("never-seen", res.res, { core: createCore(), config: defaultConfig(200000), log: () => {} }, true); + const body = JSON.parse(res.body) as { panel: string }; + assert.ok(body.panel.includes(ready + ? `Critical update ready: ${disk} — restart to finish.` + : `Critical update available: ≥ ${version} —`), body.panel); + } + } finally { + _resetSessionsForTest(); + _resetReleaseNotesWatcherForTest(); + } +}); diff --git a/tests/web-update-banner.test.ts b/tests/web-update-banner.test.ts index 05c0f25d10..5afde9d7fe 100644 --- a/tests/web-update-banner.test.ts +++ b/tests/web-update-banner.test.ts @@ -63,6 +63,8 @@ test("#1870 web surface: /__bili/overview and /__bili/status share one updateRea // Hermetic sessions dir: /__bili/overview walks the disk store, and a missing // dir rejects loudly by design (#1937) — pin an existing temp dir. const prevSessionsDir = process.env.BILI_SESSIONS_DIR; + const prevStorageMode = process.env.BILI_STORAGE_UNIFIED; + process.env.BILI_STORAGE_UNIFIED = "legacy"; const sessionsDir = mkdtempSync(path.join(tmpdir(), "bili-update-banner-")); process.env.BILI_SESSIONS_DIR = sessionsDir; _setStoreForTest(new SessionStore({ enabled: false })); @@ -128,12 +130,27 @@ test("#1870 web surface: /__bili/overview and /__bili/status share one updateRea o = await fetchWire(port, "/__bili/overview"); assert.deepEqual(o.updateReady, { kind: "ready", version: "99.0.3", summary: "fixes cache corruption" }, "ready payload: names the disk version"); assert.deepEqual(o.updateReady, s.updateReady, "overview/status updateReady parity (pending restart)"); + + const entries = [entry("99.0.4", "routine", "future routine"), entry("99.0.3", "critical", "second fix"), entry("99.0.2", "critical", "first fix")]; + for (const diskVersion of ["99.0.1", "99.0.2", "99.0.3"]) { + _setReleaseNotesStateForTest({ entries, diskVersion }); + s = await fetchWire(port, "/__bili/status"); + o = await fetchWire(port, "/__bili/overview"); + assert.deepEqual(o.updateReady, { + kind: diskVersion === "99.0.3" ? "ready" : "available", + version: diskVersion === "99.0.1" ? "99.0.2" : "99.0.3", + summary: diskVersion === "99.0.1" ? "first fix" : "second fix", + }, diskVersion); + assert.deepEqual(o.updateReady, s.updateReady, "partial-install endpoint parity"); + } } finally { _resetReleaseNotesWatcherForTest(); await close(proxy); await rmrf(sessionsDir); if (prevSessionsDir === undefined) delete process.env.BILI_SESSIONS_DIR; else process.env.BILI_SESSIONS_DIR = prevSessionsDir; + if (prevStorageMode === undefined) delete process.env.BILI_STORAGE_UNIFIED; + else process.env.BILI_STORAGE_UNIFIED = prevStorageMode; } });