Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/acp-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, updateDecision } from "./update-notes.js";
import { VERSION, BUILD_COMMIT } from "./version.js";
import { isExternalSummaryBlock } from "./external-summary-marker.js";
import { storageStatusLabel } from "./persist.js";
Expand Down Expand Up @@ -423,10 +423,14 @@ export function handleAcpStatus(args: Record<string, unknown>, 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}).`);
// #4295: branch on the SAME shared decision the banner and panel use —
// raw pendingRestart alone would say "ready" when the disk holds a
// newer-but-still-unfixed version. visible ⇒ the decision is present.
const dec = updateDecision(upd);
if (dec !== null && dec.kind === "ready") {
lines.push(`CRITICAL UPDATE READY (instance-level): ${dec.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}.`);
lines.push(`CRITICAL UPDATE AVAILABLE (instance-level): critical fix ships in ${dec !== null ? dec.version : "a newer release"} or newer — running ${upd.runningVersion}.`);
}
for (const e of upd.span) {
lines.push(` · ${e.version} [${e.tier}] ${e.summary}`);
Expand Down
68 changes: 46 additions & 22 deletions src/update-notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,36 @@ export function getUpdateVisibility(runningVersion: string): UpdateVisibility &
};
}

/** The ONE actionable update decision (#4295). Every surface — web banner,
* /acp panel line, acp_status section — renders THIS instead of branching
* on raw pendingRestart itself, so they can never disagree about what a
* restart actually delivers:
* ready → the on-disk install already carries EVERY critical fix in the
* span (disk ≥ the highest critical version), so restarting NOW
* delivers them all; version = the ACTUAL disk version.
* available → at least one critical fix is still uninstalled; version = the
* MINIMUM version carrying the FIRST STILL-UNINSTALLED critical
* fix (a partially installed span skips the criticals already on
* disk — naming one of those would claim the disk holds a fix it
* does not contain, the #4295 mismatch).
* null when the span is invisible (#1977: routine/recommended-only stay
* silent). The physical pendingRestart fact stays on UpdateVisibility
* untouched — this only decides how the notice reads. */
export function updateDecision(v: UpdateVisibility): { kind: "available" | "ready"; version: string; summary: string } | null {
if (!v.critical) return null;
const criticals = v.span.filter((e) => e.tier === "critical"); // ascending
if (criticals.length === 0) return null; // defensive: critical implies one, stay total
const top = criticals[criticals.length - 1];
const disk = v.diskVersion;
const diskUsable = disk !== undefined && semver.valid(disk) === disk;
if (v.pendingRestart && diskUsable && semver.gte(disk, top.version)) {
return { kind: "ready", version: disk, summary: top.summary };
}
const next = diskUsable ? criticals.find((e) => semver.gt(e.version, disk)) : undefined;
const target = next ?? criticals[0];
return { kind: "available", version: target.version, summary: target.summary };
}

/** Shared wording for the /acp panel line, acp_status and the web update
* banner — one line, stripper-safe slot is the caller's job. Kept compact:
* the panel is small, and acp_status carries the full span. Only ever
Expand All @@ -190,40 +220,34 @@ 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 d = updateDecision(v);
if (d !== null && d.kind === "ready") {
const topCritical = v.span.find((e) => e.tier === "critical");
const notable = topCritical ? ` Critical fix since ${v.runningVersion}: ${topCritical.summary}` : "";
return `Critical update ready: ${latest} — restart to finish.${notable}`;
return `Critical update ready: ${d.version} — restart to finish.${notable}`;
}
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 (d !== null && d.kind === "available") {
// ≥ the MINIMUM version carrying the next still-uninstalled fix: 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: ≥ ${d.version} — ${d.summary}`;
}
const latest = v.span.length > 0 ? v.span[v.span.length - 1].version : v.diskVersion;
return `Update available: ${latest ?? "newer version"}.`;
}

/** Localizable wire payload for the web update banner (#1870 web surface):
* the SERVER must not bake an English sentence — the UI ships zh + en, so
* 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).
* ready → version = the on-disk version a restart delivers.
* null when the span is invisible (#1977: routine/recommended stay silent). */
* Semantics live in updateDecision (#4295): ready names the actual disk
* version once it carries ALL critical fixes; available names the minimum
* version with the first still-uninstalled critical fix. 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 } | null {
if (!v.visible) return null;
const criticals = v.span.filter((e) => e.tier === "critical");
const first = criticals[0];
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) {
return { kind: "ready", version: v.diskVersion, summary: newest.summary };
}
return { kind: "available", version: first.version, summary: first.summary };
return updateDecision(v);
}

function throttleFile(): string {
Expand Down
100 changes: 100 additions & 0 deletions tests/update-notes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
getUpdateVisibility,
describeUpdateReady,
updateBannerPayload,
updateDecision,
type ReleaseNoteEntry,
} from "../src/update-notes.ts";
import { VERSION } from "../src/version.ts";
Expand Down Expand Up @@ -180,6 +181,66 @@ test("#1977 describeUpdateReady: critical wording + byte-stability", () => {
}
});

test("#4295 updateDecision: ready iff disk carries ALL critical fixes; available names the first still-uninstalled one", () => {
_resetReleaseNotesWatcherForTest();
try {
const FIX_A: ReleaseNoteEntry = { version: "0.1.180", date: "2026-10-02", tier: "critical", summary: "fix A (#4295)" };
const FIX_B: ReleaseNoteEntry = { version: "0.1.182", tier: "critical", summary: "fix B (#4295)" };
const TAIL: ReleaseNoteEntry = { version: "0.1.183", tier: "routine", summary: "routine tail" };

// Issue scenario verbatim: running 0.1.178, disk 0.1.179 (newer but
// UNFIXED), notes 0.1.180 critical / 0.1.181 routine. A restart loads
// 0.1.179 — nothing may call that state ready.
_setReleaseNotesStateForTest({ entries: [{ version: "0.1.181", tier: "routine", summary: "tail" }, FIX_A], diskVersion: "0.1.179" });
let v = getUpdateVisibility("0.1.178");
assert.equal(v.pendingRestart, true, "physical pendingRestart fact preserved");
assert.equal(v.visible, true);
assert.deepEqual(updateDecision(v), { kind: "available", version: "0.1.180", summary: "fix A (#4295)" }, "partial install is NOT ready — names the first still-uninstalled critical");
assert.deepEqual(updateBannerPayload(v), { kind: "available", version: "0.1.180", summary: "fix A (#4295)" }, "banner shares the decision");
assert.ok(describeUpdateReady(v).startsWith("Critical update available: ≥ 0.1.180 —"), "panel wording says available");
assert.ok(!describeUpdateReady(v).includes("restart to finish"), "no restart instruction while the fix is uninstalled");

// Multiple criticals: a disk between them already holds the lower one.
_setReleaseNotesStateForTest({ entries: [TAIL, FIX_B, FIX_A], diskVersion: "0.1.181" });
v = getUpdateVisibility("0.1.178");
assert.deepEqual(updateDecision(v), { kind: "available", version: "0.1.182", summary: "fix B (#4295)" }, "disk 0.1.181 holds fix A only → next uninstalled is 0.1.182");
_setReleaseNotesStateForTest({ entries: [TAIL, FIX_B, FIX_A], diskVersion: "0.1.180" });
v = getUpdateVisibility("0.1.178");
assert.deepEqual(updateDecision(v), { kind: "available", version: "0.1.182", summary: "fix B (#4295)" }, "disk == fix A version → A IS installed, B still missing");

// Disk carries every critical (even when notes lag behind disk) →
// ready names the ACTUAL disk version, not the span tail.
_setReleaseNotesStateForTest({ entries: [TAIL, FIX_B, FIX_A], diskVersion: "0.1.183" });
v = getUpdateVisibility("0.1.178");
assert.deepEqual(updateDecision(v), { kind: "ready", version: "0.1.183", summary: "fix B (#4295)" }, "all criticals on disk → ready, actual disk version, newest critical summary");
assert.ok(describeUpdateReady(v).startsWith("Critical update ready: 0.1.183 — restart to finish."), "panel names the disk version");
_setReleaseNotesStateForTest({ entries: [FIX_B, FIX_A], diskVersion: "0.1.184" });
v = getUpdateVisibility("0.1.178");
assert.deepEqual(updateDecision(v), { kind: "ready", version: "0.1.184", summary: "fix B (#4295)" }, "notes lagging behind disk → still names the disk version");

// Equal / rollback / unknown / invalid disk → AVAILABLE at the first
// critical; none of them may read as ready.
for (const disk of ["0.1.178", "0.1.175", undefined, "not-semver"]) {
_setReleaseNotesStateForTest({ entries: [TAIL, FIX_B, FIX_A], ...(disk !== undefined ? { diskVersion: disk } : {}) });
v = getUpdateVisibility("0.1.178");
assert.equal(v.pendingRestart, false, `pendingRestart false for disk=${String(disk)}`);
assert.deepEqual(updateDecision(v), { kind: "available", version: "0.1.180", summary: "fix A (#4295)" }, `available at first critical for disk=${String(disk)}`);
}

// Quiet states stay silent through the shared decision.
_setReleaseNotesStateForTest({ entries: [TAIL], diskVersion: "0.1.183" });
v = getUpdateVisibility("0.1.182");
assert.equal(v.visible, false);
assert.equal(updateDecision(v), null, "routine-only span: no decision");
assert.equal(updateBannerPayload(v), null, "routine-only span: no banner");
_resetReleaseNotesWatcherForTest();
v = getUpdateVisibility("0.1.182");
assert.equal(updateDecision(v), null, "clean install: no decision");
} finally {
_resetReleaseNotesWatcherForTest();
}
});

// ---------------------------------------------------------------------------
// Surfaces. Mirrors the #1577 advisory-surface tests.

Expand Down Expand Up @@ -305,3 +366,42 @@ test("#1977 /acp panel: one critical-update line BEFORE the footer, stripper-saf
_resetSessionsForTest();
}
});

test("#4295 surfaces: partially installed fix renders AVAILABLE (never READY) on acp_status + /acp panel", async () => {
_resetReleaseNotesWatcherForTest();
const { handleAcpStatus } = await import("../src/acp-status.ts");
const { _resetSessionsForTest } = await import("../src/session.ts");
const { handlePluginStatus } = await import("../src/plugin.ts");
try {
// Disk strictly between running and the first critical fix: the exact
// #4295 mismatch state (restart would load an unfixed version).
const PARTIAL_DISK = semver.inc(RUNNING, "patch") ?? "0.0.2";
const PARTIAL_CRIT = semver.inc(RUNNING, "minor") ?? "0.2.0";
const PARTIAL_TAIL = semver.inc(PARTIAL_CRIT, "patch") ?? "0.2.1";
const partial: ReleaseNoteEntry[] = [
{ version: PARTIAL_TAIL, tier: "routine", summary: "routine tail after the fix" },
{ version: PARTIAL_CRIT, date: "2026-10-02", tier: "critical", summary: "partial-install critical fix (#4295)" },
];
_setReleaseNotesStateForTest({ entries: partial, diskVersion: PARTIAL_DISK });

const ctx = makeCtx12();
const out = handleAcpStatus({}, ctx);
assert.ok(out.text.includes("CRITICAL UPDATE AVAILABLE (instance-level):"), "acp_status says AVAILABLE while the critical fix is still uninstalled");
assert.ok(!out.text.includes("CRITICAL UPDATE READY"), "acp_status never claims ready/restart-to-finish in this state");
assert.ok(out.text.includes(`ships in ${PARTIAL_CRIT} or newer`), "names the minimum fixed version");
assert.ok(out.text.includes(`· ${PARTIAL_CRIT} [critical] partial-install critical fix (#4295)`), "span listing intact");

_resetSessionsForTest();
const { getSession } = await import("../src/session.ts");
getSession("upd-sess-4295", { protocol: "anthropic", label: "UPD" });
const deps = { core: createCore(), config: defaultConfig(200000), log: (_l: string, _m: string) => {} };
const r = mockRes();
handlePluginStatus("never-seen", r.res, deps, true);
const p = JSON.parse(r.body).panel as string;
assert.ok(p.includes(`Critical update available: ≥ ${PARTIAL_CRIT} —`), "panel line says available with the minimum fixed version");
assert.ok(!p.includes("Critical update ready"), "panel line never says ready for a partial install");
} finally {
_resetReleaseNotesWatcherForTest();
_resetSessionsForTest();
}
});
Loading