diff --git a/.github/workflows/announcements-gate.yml b/.github/workflows/announcements-gate.yml new file mode 100644 index 0000000000..c6e8774e0f --- /dev/null +++ b/.github/workflows/announcements-gate.yml @@ -0,0 +1,36 @@ +name: announcements-gate + +on: + # #5548 §五.4: the ONLY new CI touchpoint for the announcement surface — + # validate announcements/remote.json with the client's own validator module + # so a bad push is blocked before merge (PR) or right after land (push). + pull_request: + types: [opened, synchronize, reopened] + branches: [master, agent-relay] # #3468: agent PRs target the relay branch + paths: + - "announcements/remote.json" + - ".github/workflows/announcements-gate.yml" + push: + branches: [master] + paths: + - "announcements/remote.json" + workflow_dispatch: + +concurrency: + group: announcements-gate-${{ github.ref }} + cancel-in-progress: ${{ github.event_name != 'schedule' }} + +jobs: + validate: + runs-on: ubuntu-latest + strategy: + matrix: + node-version: [22] + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: ${{ matrix.node-version }} + cache: npm + - run: npm ci --no-audit --no-fund + - run: node --import tsx scripts/validate-remote-announcements.ts diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 17bcb8ea11..51e6f4cb54 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -371,6 +371,7 @@ This index is generated from `website/config-reference/*.yaml` — edit the seed | `BILI_SNAPSHOT_SHADOW` | boolean | true (on) | — | Snapshot-render shadow observability (#3756 follow-up): compute the outbound snapshot view on every armed turn WITHOUT using it, classify the diff against the legacy outbound, and surface per-session counters in the web panel + the snapshot-shadow diagnostics topic. 0/false/off disables; 1/true/on enables; otherwise the config-file snapshot.shadow boolean applies (default on). The render lane itself stays rollout-gated (BILI_ROLLOUT_SNAPSHOT_RENDER), so the shadow lane never changes outbound bytes - it is the dry-run evidence that flips the rollout later. | | `BILI_CONFIG_FILE` | string | unset (XDG config path) | — | Override the config file path (path relocation). | | `BILI_SESSIONS_DIR` | string | unset (XDG state path) | — | Where session records are stored (path relocation). | +| `BILI_ANNOUNCEMENTS_URL` | string | official raw URL of announcements/remote.json | — | Override the remote announcements document URL (#5548 lane C): installs behind firewalls can point bili at their own relay of announcements/remote.json; unset = the official raw address. Path relocation; fail-open either way (unreachable or malformed document = no remote lane, never blocks startup or traffic). | | `BILI_ENCRYPTION_KEY` | string | unset | — | Key material for encrypted persisted payloads (secret). | | `BILI_TUNNEL_ALLOWED_HOSTS` | string[] (csv) | unset | — | Host allowlist for the MITM tunnel lane. | | `BILI_RECLAIM_FETCH_PATCH` | boolean-ish | on | — | Toggle for reclaiming the global fetch patch on shutdown. | diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index 600daf8c27..d34b7cdc73 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -371,6 +371,7 @@ | `BILI_SNAPSHOT_SHADOW` | boolean | true (on) | — | 快照渲染影子对账(#3756 后续):每轮已折叠会话都用引擎现算一次出站快照视图但不使用,与 legacy 实际出站比对分类,逐会话计数进 web 面板与 snapshot-shadow 诊断主题。0/false/off 关闭;1/true/on 开启;否则读配置文件 snapshot.shadow 布尔(默认开)。渲染车道本身仍受灰度开关控制(BILI_ROLLOUT_SNAPSHOT_RENDER),影子车道绝不改变出站字节 —— 它是后续翻转灰度的干跑证据。 | | `BILI_CONFIG_FILE` | string | unset (XDG config path) | — | 覆盖配置文件路径(路径重定位)。 | | `BILI_SESSIONS_DIR` | string | unset (XDG state path) | — | 会话记录的存放目录(路径重定位)。 | +| `BILI_ANNOUNCEMENTS_URL` | string | official raw URL of announcements/remote.json | — | 覆盖远程公告文档地址(#5548 C 车道):防火墙后的装机可指向自己中继的 announcements/remote.json;缺省 = 官方 raw 地址。路径重定位类;任何情况下 fail-open(拉不到/文档坏 = 无远程车道,永不阻断启动或流量)。 | | `BILI_ENCRYPTION_KEY` | string | unset | — | 加密持久化载荷的密钥材料(机密)。 | | `BILI_TUNNEL_ALLOWED_HOSTS` | string[] (csv) | unset | — | MITM 隧道车道的主机白名单。 | | `BILI_RECLAIM_FETCH_PATCH` | boolean-ish | on | — | 进程退出时回收全局 fetch 补丁的开关。 | diff --git a/LIFECYCLE.md b/LIFECYCLE.md index 073fd4ce9e..6f74de4733 100644 --- a/LIFECYCLE.md +++ b/LIFECYCLE.md @@ -183,6 +183,7 @@ | `BILI_UPDATE_CHECK_INTERVAL_MS` | update.checkIntervalMs 覆盖(#2030) | | `BILI_ADVISORY_CHECK` | 公告检查开关(默认 ON;文件 advisoryCheck:false 退出) | | `BILI_ADVISORY_URL` | 公告文档 URL 覆盖 | +| `BILI_ANNOUNCEMENTS_URL` | 统一公告面远程文档 URL 覆盖(#5548 C 车道,防火墙中继逃生门;缺省官方 raw 地址,fail-open) | | `BILI_RELEASE_NOTES_CHECK` | 发布说明检查(#1870,默认 ON) | | `BILI_RELEASE_NOTES_URL` | 发布说明 URL 覆盖 | | `BILI_UPDATE_POLICY_URL` | update.policyUrl——更新策略 URL 覆盖(**测试专用**:生产固定官方 repo,#3619 安全边界) | diff --git a/announcements/remote.json b/announcements/remote.json new file mode 100644 index 0000000000..a533cf87f5 --- /dev/null +++ b/announcements/remote.json @@ -0,0 +1,4 @@ +{ + "v": 1, + "entries": [] +} diff --git a/scripts/validate-remote-announcements.ts b/scripts/validate-remote-announcements.ts new file mode 100644 index 0000000000..6f1370a161 --- /dev/null +++ b/scripts/validate-remote-announcements.ts @@ -0,0 +1,32 @@ +// #5548 §五.4: CI gate for announcements/remote.json — runs the SAME validator +// module the client uses, so a bad push is blocked before merge instead of +// silently degrading on every install after push. +import fs from "node:fs"; +import { applyRemoteDoc, REMOTE_DOC_CAP_BYTES } from "../src/notices.js"; + +const file = new URL("../announcements/remote.json", import.meta.url); +let raw: string; +try { + raw = fs.readFileSync(file, "utf8"); +} catch { + console.log("announcements/remote.json absent — nothing to validate"); + process.exit(0); +} +if (Buffer.byteLength(raw) > REMOTE_DOC_CAP_BYTES) { + console.error(`FAIL: announcements/remote.json exceeds ${REMOTE_DOC_CAP_BYTES} bytes`); + process.exit(1); +} +let doc: unknown; +try { + doc = JSON.parse(raw); +} catch (e) { + console.error(`FAIL: announcements/remote.json is not valid JSON: ${String(e)}`); + process.exit(1); +} +const res = applyRemoteDoc(doc); +for (const w of res.warnings ?? []) console.warn(`WARN: ${w}`); +if (!res.ok) { + console.error(`FAIL: ${res.reason}`); + process.exit(1); +} +console.log(`OK: ${res.entries.length} entr${res.entries.length === 1 ? "y" : "ies"} validated (v${(doc as { v: number }).v})`); diff --git a/src/cli.ts b/src/cli.ts index daaea99423..73e138b122 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -37,6 +37,7 @@ import { checkForUpdate, startAutoUpdate } from "./update.js"; import { registerUpdateActions } from "./server/update-actions.js"; import { startAdvisoryWatcher, getAdvisoryState, advisoryDeferring, advisoryBlocksVersion, advisoryAwaitingFirstCheck } from "./advisory.js"; import { startReleaseNotesWatcher } from "./update-notes.js"; +import { startRemoteAnnouncementWatcher } from "./remote-announcements.js"; import { resolveProxy } from "./upstream-proxy.js"; import { runMcpStdio } from "./mcp.js"; import { PLUGIN_AGENTS, isPluginAgent, pluginInstall, pluginRemove, pluginStatusAll, pluginUpdate, renderPluginList, warnActivePluginSessions, type PluginAgent } from "./plugin-install.js"; @@ -900,4 +901,10 @@ export async function main(): Promise { resolveProxy: (url) => resolveProxy(opts.routes, opts.proxy, url, opts.proxyFallback), }); } + // #5548 lane C: the remote announcement fetch rides the update-check + // cadence (§五.2). Unconditional by design: a one-way broadcast with zero + // side effects — offline it simply never renders lane C. + startRemoteAnnouncementWatcher({ + resolveProxy: (url) => resolveProxy(opts.routes, opts.proxy, url, opts.proxyFallback), + }); } diff --git a/src/install-id.ts b/src/install-id.ts new file mode 100644 index 0000000000..2aaa3f2f6c --- /dev/null +++ b/src/install-id.ts @@ -0,0 +1,38 @@ +// #5548 P2: stable per-install identity for percent bucketing (§4.1) — same +// persistence discipline as the dismissal store (state dir, best-effort fs). +import fs from "node:fs"; +import path from "node:path"; +import { randomUUID } from "node:crypto"; +import { stateDir } from "./paths.js"; + +const INSTALL_ID_FILE = "install-id"; + +function stateDirFor(env: NodeJS.ProcessEnv): string { + const v = env.XDG_STATE_HOME; + return v && v.length > 0 ? path.join(path.resolve(v), "billion-context") : stateDir(); +} + +let cached: { dir: string; id: string } | undefined; + +/** Stable uuid persisted under the state dir. Returns "" when it cannot be + * persisted (read-only state) — percent-gated notices then hide rather than + * flicker buckets across restarts. */ +export function installIdOf(env: NodeJS.ProcessEnv = process.env): string { + const dir = stateDirFor(env); + if (cached && cached.dir === dir) return cached.id; + try { + const file = path.join(dir, INSTALL_ID_FILE); + try { + const raw = fs.readFileSync(file, "utf8").trim(); + if (/^[0-9a-f-]{36}$/i.test(raw)) return (cached = { dir, id: raw }).id; + } catch { + // first run — no file yet + } + const id = randomUUID(); + fs.mkdirSync(dir, { recursive: true }); + fs.writeFileSync(file, id + "\n"); + return (cached = { dir, id }).id; + } catch { + return (cached = { dir, id: "" }).id; + } +} diff --git a/src/local-announcements.ts b/src/local-announcements.ts new file mode 100644 index 0000000000..a3555db1ee --- /dev/null +++ b/src/local-announcements.ts @@ -0,0 +1,75 @@ +// #5548 P2 lane B: local announcements — one validated record per *.json file +// under /announcements/. The directory is re-scanned on every load; +// a result is memoized by a name:size:mtime signature so bad files log once +// per change instead of once per overview poll. A broken file never takes +// down the other files or the page (§六: any source unavailable → that source absent). +import fs from "node:fs"; +import path from "node:path"; +import { configDir } from "./paths.js"; +import { log } from "./logger.js"; +import { validateNoticeRecord, type NoticeRecord } from "./notices.js"; + +const ANNOUNCEMENTS_DIRNAME = "announcements"; +export const LOCAL_FILE_CAP_BYTES = 64 * 1024; // §六.3 size cap + +interface LocalAnnouncementLoad { + records: NoticeRecord[]; + errors: Array<{ file: string; reason: string }>; +} + +function announcementsDirFor(env: NodeJS.ProcessEnv): string { + const v = env.XDG_CONFIG_HOME; + const base = v && v.length > 0 ? path.join(path.resolve(v), "billion-context") : configDir(); + return path.join(base, ANNOUNCEMENTS_DIRNAME); +} + +let cache: { dir: string; sig: string; result: LocalAnnouncementLoad } | undefined; + +export function loadLocalAnnouncements(env: NodeJS.ProcessEnv = process.env): LocalAnnouncementLoad { + const dir = announcementsDirFor(env); + let entries: fs.Dirent[]; + try { + entries = fs.readdirSync(dir, { withFileTypes: true }); + } catch { + // no directory = zero local notices, not an error + return { records: [], errors: [] }; + } + const names = entries.filter((e) => e.isFile() && e.name.endsWith(".json")).map((e) => e.name).sort(); + const parts: string[] = []; + const records: NoticeRecord[] = []; + const errors: LocalAnnouncementLoad["errors"] = []; + for (const name of names) { + const file = path.join(dir, name); + let st: fs.Stats; + try { + st = fs.statSync(file); + } catch { + continue; // vanished between readdir and stat + } + parts.push(`${name}:${st.size}:${st.mtimeMs}`); + if (st.size > LOCAL_FILE_CAP_BYTES) { + errors.push({ file: name, reason: `exceeds ${LOCAL_FILE_CAP_BYTES} bytes` }); + continue; + } + let doc: unknown; + try { + doc = JSON.parse(fs.readFileSync(file, "utf8")); + } catch (e) { + errors.push({ file: name, reason: `bad JSON: ${String(e)}` }); + continue; + } + const res = validateNoticeRecord(doc); + if (!res.ok) { + errors.push({ file: name, reason: res.reason }); + continue; + } + if (res.droppedFields.length > 0) log("debug", `[notices] ${name}: dropped unknown field(s) ${res.droppedFields.join(", ")}`); + records.push(res.rec); + } + const sig = parts.join("|"); + if (cache && cache.dir === dir && cache.sig === sig) return cache.result; + if (errors.length > 0) log("warn", `[notices] local lane: ${errors.map((e) => `${e.file} (${e.reason})`).join("; ")}`); + const result: LocalAnnouncementLoad = { records, errors }; + cache = { dir, sig, result }; + return result; +} diff --git a/src/notices.ts b/src/notices.ts new file mode 100644 index 0000000000..0ceed294bb --- /dev/null +++ b/src/notices.ts @@ -0,0 +1,484 @@ +// #5548 unified announcement surface (design v2, owner-approved): three sources +// (A built-in declarations / B local JSON / C remote GitHub raw) feed ONE +// renderer through shared pure filter/sort functions and one dismissal store. +// P1 landed lane A; P2 adds lane B's machinery here: the whitelist validator, +// the targeting gates (versions matcher / percent bucketing / since-until +// window), the id@hash16 dismissal keys, and layer merging (B > C > A). +// Pure module: no fs, no io — the dismissal store stays in pck-notice.ts, the +// local-file loader in local-announcements.ts. + +import { createHash } from "node:crypto"; + +export type NoticeKind = "new-feature" | "upgrade-advisory" | "warning" | "suggestion" | "incident" | "deprecation" | "other"; +type NoticeTone = "ok" | "warn" | "info"; +type NoticeDismiss = "persistent" | "instance" | "always"; +/** Multilang copy: locale object (keys like "zh-CN"/"en") or a plain string. */ +type NoticeText = string | Record; + +export interface NoticeRecord { + id: string; + kind: NoticeKind; + title: NoticeText; + desc?: NoticeText; + hint?: NoticeText; + ctaLabel?: NoticeText; + ctaUrl?: string; + /** Explicit tone overrides the kind-derived one (toneOf). */ + tone?: NoticeTone; + /** persistent (default) | instance (process-only) | always (never hidden, no close button). */ + dismiss?: NoticeDismiss; + /** 0–100; absent/null falls back to the kind default in KIND_PRIORITY. */ + priority?: number | null; + /** ISO8601 UTC; sort tiebreak after priority (newer first). */ + publishedAt?: string; + // Targeting dimensions (§4.1) — AND-combined, applied to EVERY source: + versions?: string[]; + percent?: number | null; + since?: string; + until?: string; +} + +/** Server-side state a built-in declaration's active predicate may read. */ +interface BuiltInContext { + /** effectivePromptCacheRouting() result ("disabled"/"auto"/"enabled"). */ + promptCacheRouting: string; + /** True when the user wrote an explicit pluginFullResendWatermark rollout override. */ + wmOverridePresent: boolean; +} + +interface BuiltinNotice extends NoticeRecord { + active: (ctx: BuiltInContext) => boolean; +} + +const KIND_PRIORITY: Record = { + incident: 90, + "upgrade-advisory": 80, + warning: 70, + deprecation: 60, + suggestion: 50, + "new-feature": 40, + other: 30, +}; + +export function priorityOf(rec: Pick): number { + return typeof rec.priority === "number" ? rec.priority : KIND_PRIORITY[rec.kind]; +} + +/** Kind-derived tone: new-feature→ok, warning/incident/deprecation→warn, rest→info. */ +export function toneOf(rec: Pick): NoticeTone { + if (rec.tone) return rec.tone; + if (rec.kind === "new-feature") return "ok"; + if (rec.kind === "warning" || rec.kind === "incident" || rec.kind === "deprecation") return "warn"; + return "info"; +} + +export function dismissModeOf(rec: Pick): NoticeDismiss { + return rec.dismiss ?? "persistent"; +} + +/** Visible under the record's dismiss mode. Persistent keys are + * `id@hash16(record)`; a legacy bare-id entry (pre-P2 stores) still counts, + * so an upgrade never re-shows a banner the user already closed. */ +export function noticeVisible( + rec: NoticeRecord, + dismissedPersistent: ReadonlySet, + dismissedInstance: ReadonlySet = new Set(), +): boolean { + const mode = dismissModeOf(rec); + if (mode === "always") return true; + if (mode === "instance") return !dismissedInstance.has(rec.id); + return !(dismissedPersistent.has(dismissKeyOf(rec)) || dismissedPersistent.has(rec.id)); +} + +/** Stable sort: priority desc, then publishedAt desc (missing timestamps last). */ +export function sortNotices(list: readonly T[]): T[] { + const ts = (r: NoticeRecord): number => { + const t = r.publishedAt ? Date.parse(r.publishedAt) : NaN; + return Number.isNaN(t) ? -Infinity : t; + }; + return [...list].sort((a, b) => { + const pa = priorityOf(a), pb = priorityOf(b); + if (pa !== pb) return pb - pa; + const ta = ts(a), tb = ts(b); + if (ta !== tb) return tb - ta; + return 0; + }); +} + +/** Merge notice layers by id — earlier layers win (B local > C remote > A + * built-in, §三). Input order within a layer is preserved. */ +export function mergeNoticeLayers(layers: readonly (readonly NoticeRecord[])[]): NoticeRecord[] { + const seen = new Set(); + const out: NoticeRecord[] = []; + for (const layer of layers) { + for (const rec of layer) { + if (seen.has(rec.id)) continue; + seen.add(rec.id); + out.push(rec); + } + } + return out; +} + +// --------------------------------------------------------------------------- +// P2 lane B: validation, targeting gates, dismissal hashing (§4) +// --------------------------------------------------------------------------- + +export const NOTICE_KINDS: readonly NoticeKind[] = ["new-feature", "upgrade-advisory", "warning", "suggestion", "incident", "deprecation", "other"]; +const NOTICE_TONES: readonly NoticeTone[] = ["ok", "warn", "info"]; +const DISMISS_MODES: readonly NoticeDismiss[] = ["persistent", "instance", "always"]; +const NOTICE_ID_RE = /^[a-z0-9][a-z0-9-]{0,63}$/; +const TEXT_LIMITS = { title: 120, desc: 600, hint: 300, ctaLabel: 120 } as const; +const CTA_URL_MAX = 200; +const NOTICE_FIELDS: readonly string[] = [ + "id", "kind", "title", "desc", "hint", "ctaLabel", "ctaUrl", + "tone", "dismiss", "priority", "publishedAt", "versions", "percent", "since", "until", +]; + +type TextCheck = { ok: true; value: NoticeText } | { ok: false; reason: string }; + +function checkText(v: unknown, field: string, limit: number): TextCheck { + if (typeof v === "string") { + return v.length <= limit ? { ok: true, value: v } : { ok: false, reason: `${field} exceeds ${limit} chars` }; + } + if (v && typeof v === "object" && !Array.isArray(v)) { + const entries = Object.entries(v as Record); + if (entries.length === 0) return { ok: false, reason: `${field}: empty locale object` }; + for (const [k, val] of entries) { + if (typeof val !== "string" || val.length > limit) { + return { ok: false, reason: `${field}[${k}] must be a string ≤${limit} chars` }; + } + } + return { ok: true, value: v as Record }; + } + return { ok: false, reason: `${field} must be a string or locale object` }; +} + +function isKind(v: unknown): v is NoticeKind { + return typeof v === "string" && (NOTICE_KINDS as readonly string[]).includes(v); +} + +function isIso8601(v: unknown): v is string { + return typeof v === "string" && Number.isFinite(Date.parse(v)); +} + +function isIntInRange(v: unknown, lo: number, hi: number): v is number { + return typeof v === "number" && Number.isInteger(v) && v >= lo && v <= hi; +} + +/** Whitelist validator (§4): unknown fields are dropped (reported, not fatal); + * a bad value invalidates ONLY this record. Returns the cleaned record. */ +export function validateNoticeRecord(raw: unknown): { ok: true; rec: NoticeRecord; droppedFields: string[] } | { ok: false; reason: string } { + if (!raw || typeof raw !== "object" || Array.isArray(raw)) return { ok: false, reason: "record is not an object" }; + const src = raw as Record; + const droppedFields = Object.keys(src).filter((k) => !NOTICE_FIELDS.includes(k)); + const id = src["id"]; + if (typeof id !== "string" || !NOTICE_ID_RE.test(id)) return { ok: false, reason: "id must match ^[a-z0-9][a-z0-9-]{0,63}$" }; + const kind = src["kind"]; + if (!isKind(kind)) return { ok: false, reason: `kind must be one of: ${NOTICE_KINDS.join(" / ")}` }; + const title = checkText(src["title"], "title", TEXT_LIMITS.title); + if (!title.ok) return { ok: false, reason: title.reason }; + const rec: NoticeRecord = { id, kind, title: title.value }; + if ("desc" in src) { + const d = checkText(src["desc"], "desc", TEXT_LIMITS.desc); + if (!d.ok) return { ok: false, reason: d.reason }; + rec.desc = d.value; + } + if ("hint" in src) { + const h = checkText(src["hint"], "hint", TEXT_LIMITS.hint); + if (!h.ok) return { ok: false, reason: h.reason }; + rec.hint = h.value; + } + if ("ctaLabel" in src) { + const c = checkText(src["ctaLabel"], "ctaLabel", TEXT_LIMITS.ctaLabel); + if (!c.ok) return { ok: false, reason: c.reason }; + rec.ctaLabel = c.value; + } + if ("ctaUrl" in src) { + const u = src["ctaUrl"]; + if (typeof u !== "string" || u.length > CTA_URL_MAX || !/^https?:\/\//.test(u)) { + return { ok: false, reason: `ctaUrl must be http(s) ≤${CTA_URL_MAX} chars` }; + } + rec.ctaUrl = u; + } + if ("tone" in src && !NOTICE_TONES.includes(src["tone"] as NoticeTone)) return { ok: false, reason: "tone must be ok/warn/info" }; + else if ("tone" in src) rec.tone = src["tone"] as NoticeTone; + if ("dismiss" in src && !DISMISS_MODES.includes(src["dismiss"] as NoticeDismiss)) return { ok: false, reason: "dismiss must be persistent/instance/always" }; + else if ("dismiss" in src) rec.dismiss = src["dismiss"] as NoticeDismiss; + if ("priority" in src) { + if (src["priority"] !== null && !isIntInRange(src["priority"], 0, 100)) return { ok: false, reason: "priority must be an integer 0–100 or null" }; + rec.priority = src["priority"] as number | null; + } + for (const f of ["publishedAt", "since", "until"] as const) { + if (!(f in src)) continue; + if (!isIso8601(src[f])) return { ok: false, reason: `${f} must be ISO8601` }; + rec[f] = src[f]; + } + if ("versions" in src) { + const vs = src["versions"]; + if (!Array.isArray(vs) || vs.some((el) => typeof el !== "string")) return { ok: false, reason: "versions must be an array of matcher strings" }; + for (const el of vs as string[]) { + if (parseVersionMatcher(el) === null) return { ok: false, reason: `versions element not parseable: ${el}` }; + } + rec.versions = vs as string[]; + } + if ("percent" in src) { + if (src["percent"] !== null && !isIntInRange(src["percent"], 0, 100)) return { ok: false, reason: "percent must be an integer 0–100 or null" }; + rec.percent = src["percent"] as number | null; + } + return { ok: true, rec, droppedFields }; +} + +// ---- versions matcher (§4.1) --------------------------------------------- + +type VerT = [number, number, number]; +interface Constraint { op: ">" | ">=" | "<" | "<=" | "="; v: VerT } + +/** Numeric body of a version string — dirty/pre-release builds compare by + * their major.minor.patch body only (§4.1). */ +export function parseVersion(s: string): VerT | null { + const m = /^(\d+)\.(\d+)\.(\d+)/.exec(s.trim()); + return m ? [Number(m[1]), Number(m[2]), Number(m[3])] : null; +} + +function cmpVer(a: VerT, b: VerT): number { + for (let i = 0; i < 3; i++) if (a[i] !== b[i]) return a[i] < b[i] ? -1 : 1; + return 0; +} + +function holds(c: Constraint, v: VerT): boolean { + const d = cmpVer(v, c.v); + switch (c.op) { + case ">": return d > 0; + case ">=": return d >= 0; + case "<": return d < 0; + case "<=": return d <= 0; + default: return d === 0; + } +} + +function caretRange(tok: string): Constraint[] | null { + const v = parseVersion(tok.slice(1)); + if (!v) return null; + const upper: VerT = v[0] > 0 ? [v[0] + 1, 0, 0] : v[1] > 0 ? [0, v[1] + 1, 0] : [0, 0, v[2] + 1]; + return [{ op: ">=", v }, { op: "<", v: upper }]; +} + +function parseTokenList(tokens: string[]): Constraint[] | null { + const bare: VerT[] = []; + const cons: Constraint[] = []; + for (const tok of tokens) { + if (tok.startsWith("^")) { + const cr = caretRange(tok); + if (!cr) return null; + cons.push(...cr); + continue; + } + const m = /^(>=|<=|>|<|=)?(.*)$/.exec(tok)!; + const v = parseVersion(m[2]); + if (!v) return null; + if (m[1]) cons.push({ op: m[1] as Constraint["op"], v }); + else bare.push(v); + } + // One bare version is an exact pin; two bare versions inside brackets mean + // the inclusive range "[c d]" (§4.1 example); mixed lists are ambiguous → invalid. + if (bare.length === 1 && cons.length === 0) cons.push({ op: "=", v: bare[0] }); + else if (bare.length === 2 && cons.length === 0) cons.push({ op: ">=", v: bare[0] }, { op: "<=", v: bare[1] }); + else if (bare.length > 0) return null; + return cons; +} + +/** One matcher element → alternatives joined by OR (`||`); each alternative is + * an AND of constraints. Element forms: exact `0.1.212`, comparator list + * `>=a =a 0 ? alts : null; + } + const cons = parseTokenList(s.split(/\s+/).filter(Boolean)); + return cons ? [cons] : null; +} + +/** Elements AND together; empty/absent list matches every version. An + * unparseable LOCAL version hides any gated record (宁缺毋滥, §4.1). */ +export function versionMatches(elements: readonly string[], version: string | null): boolean { + if (elements.length === 0) return true; + const ver = version === null ? null : parseVersion(version); + if (!ver) return false; + return elements.every((el) => { + const alts = parseVersionMatcher(el); + return alts !== null && alts.some((alt) => alt.every((c) => holds(c, ver))); + }); +} + +// ---- percent bucketing (§4.1) --------------------------------------------- + +/** Deterministic per-install, per-notice bucket in [0,100): same install + id + * always lands in the same bucket; different ids use different salt so + * buckets don't correlate with business rollouts. */ +export function percentBucket(installId: string, id: string): number { + const h = createHash("sha256").update(`${installId}|${id}`).digest("hex"); + return parseInt(h.slice(0, 8), 16) % 100; +} + +export interface TargetingContext { + /** Running bili version string (null-safe: unparseable → gated records hide). */ + version: string | null; + /** Stable install identity; "" when it cannot be persisted (read-only state). */ + installId: string; + now: number; +} + +/** The three common targeting gates, AND-combined (§4.1). since is inclusive, + * until exclusive. A missing installId hides percent-gated records rather + * than flicker buckets across restarts. */ +export function passesTargeting(rec: Pick, ctx: TargetingContext): boolean { + if (rec.versions && rec.versions.length > 0 && !versionMatches(rec.versions, ctx.version)) return false; + if (typeof rec.percent === "number") { + if (!ctx.installId) return false; + if (percentBucket(ctx.installId, rec.id) >= rec.percent) return false; + } + if (rec.since && Number.isFinite(Date.parse(rec.since)) && Date.parse(rec.since) > ctx.now) return false; + if (rec.until && Number.isFinite(Date.parse(rec.until)) && ctx.now >= Date.parse(rec.until)) return false; + return true; +} + +// ---- dismissal hashing (§4.4) ---------------------------------------------- + +function canonValue(v: unknown): unknown { + if (Array.isArray(v)) return v.map(canonValue); + if (v && typeof v === "object") { + const out: Record = {}; + for (const k of Object.keys(v as Record).sort()) out[k] = canonValue((v as Record)[k]); + return out; + } + return v; +} + +/** Canonical form of a validated record — key-sorted JSON, stable across + * processes and independent of source-file key order. */ +export function normalizeRecord(rec: NoticeRecord): string { + return JSON.stringify(canonValue(rec)); +} + +export function noticeHash16(rec: NoticeRecord): string { + return createHash("sha1").update(normalizeRecord(rec)).digest("hex").slice(0, 16); +} + +/** Store key: `id@hash16`. Content edits change the hash → the banner re-shows + * once (persistent mode); always-mode records never reach the store. */ +export function dismissKeyOf(rec: NoticeRecord): string { + return `${rec.id}@${noticeHash16(rec)}`; +} + +// ---- P3 lane C: remote document (§五) -------------------------------------- + +/** Remote document format version; this build reads v1 only. Higher v → the + * WHOLE document is skipped (older builds must never partially parse a newer + * shape, §五.5). */ +export const REMOTE_DOC_VERSION = 1; +/** Size cap for the remote document body (§六.3): over cap = invalid doc. */ +export const REMOTE_DOC_CAP_BYTES = 512 * 1024; + +/** Pure gate for a fetched/published remote document: v header check + each + * entry through the shared whitelist validator. Invalid entries are skipped + * with a warning (reported by the CI gate), never fatal to the rest. */ +export function applyRemoteDoc(raw: unknown): { ok: true; entries: NoticeRecord[]; warnings: string[] } | { ok: false; reason: string } { + if (!raw || typeof raw !== "object" || Array.isArray(raw)) return { ok: false, reason: "document is not an object" }; + const doc = raw as Record; + if (doc["v"] !== REMOTE_DOC_VERSION) return { ok: false, reason: `unsupported document version ${JSON.stringify(doc["v"])} (this build reads v${REMOTE_DOC_VERSION})` }; + const list = doc["entries"]; + if (!Array.isArray(list)) return { ok: false, reason: "entries missing or not an array" }; + const entries: NoticeRecord[] = []; + const warnings: string[] = []; + list.forEach((item, i) => { + const r = validateNoticeRecord(item); + if (!r.ok) { + warnings.push(`entry[${i}]: ${r.reason}`); + return; + } + if (r.droppedFields.length > 0) warnings.push(`entry[${i}]: dropped unknown field(s) ${r.droppedFields.join(", ")}`); + entries.push(r.rec); + }); + return { ok: true, entries, warnings }; +} + +export const PCK_NOTICE_ID = "pck-default-off"; +export const WM_FASTPATH_INTRO_ID = "wm-fastpath-intro"; + +/** Lane A: built-in declarations (bound to code behavior, review-gated here). */ +export const BUILTIN_NOTICES: readonly BuiltinNotice[] = [ + { + // #5079: prompt_cache.routing default flip (auto → disabled) upgrade notice. + // Explicit tone "warn": the derived tone for upgrade-advisory is info, but + // this banner has rendered as .banner.warn since #5079 (byte-stable). + id: PCK_NOTICE_ID, + kind: "upgrade-advisory", + tone: "warn", + title: { "zh-CN": "prompt_cache_key 自注入已默认关闭(升级提示)", en: "prompt_cache_key self-stamping is now off by default (upgrade notice)" }, + desc: { + "zh-CN": "本版本起 bili 不再自行向请求添加 OpenAI 的 prompt_cache_key 字段(此前默认开启)。如果你的上游是按该字段路由前缀缓存的中转站或号池(如 workbuddy panel/hub 等),请打开开关恢复注入,否则流量可能被随机路由、频繁未命中缓存。", + en: "Since this version bili no longer adds OpenAI's prompt_cache_key field to requests on its own (previously on by default). If your upstream routes prefix cache by this key (relays such as workbuddy panel/hub, account pools), turn the switch back on — otherwise traffic may route randomly and miss the prefix cache.", + }, + hint: { + "zh-CN": "开关在「配置」页快速配置里(promptCache.routing:disabled/auto/enabled),也可直接改 billion-context.json;直连官方 API 的用户可忽略此提示。", + en: "Switch: Configuration page → quick config (promptCache.routing: disabled/auto/enabled), or edit billion-context.json directly; direct official-API users can ignore this notice.", + }, + active: (ctx) => ctx.promptCacheRouting === "disabled", + }, + { + // #5403/#5469: watermark fastpath experimental-grey intro — every install + // sees it on first upgrade until closed or an explicit rollout override. + id: WM_FASTPATH_INTRO_ID, + kind: "new-feature", + tone: "ok", + title: { "zh-CN": "新功能提示:大上下文增量解析(增量压缩过渡功能 · 实验性)", en: "New: incremental parsing for large contexts (incremental-compression bridge feature, experimental)" }, + desc: { + "zh-CN": "长会话每轮都要把整个历史重新发给代理解析一遍,现在改为增量解析:只解析本轮新增的那部分,优化长会话的解析性能(发给模型的内容不变;有任何疑点,当轮自动回退老路径并重新对齐)。当前版本对约 5% 的安装灰度开启,属实验性功能。", + en: "Long conversations used to re-send their entire history for parsing on every turn. Now only the new part of each turn is parsed — faster parsing on long sessions (the request sent to the model is unchanged; any doubt falls back to the old path for that turn and re-aligns). This release rolls it out to roughly 5% of installs; it is marked experimental.", + }, + hint: { + "zh-CN": "开关在「配置」页快速配置(大上下文增量解析),也可在 billion-context.json 写 rollout.features.pluginFullResendWatermark: true/false,或设环境变量 BILI_ROLLOUT_PLUGIN_FULL_RESEND_WATERMARK=1/0。", + en: "Switch: Configuration page → quick config (incremental parsing for large contexts), or write rollout.features.pluginFullResendWatermark: true/false in billion-context.json, or set environment variable BILI_ROLLOUT_PLUGIN_FULL_RESEND_WATERMARK=1/0.", + }, + active: (ctx) => !ctx.wmOverridePresent, + }, +]; + +const dynamicIds = new Set(); + +/** Lanes B/C register their loaded ids so dismissal stays validated (#5548 §4.5). */ +export function registerDynamicNoticeIds(ids: Iterable): void { + for (const id of ids) dynamicIds.add(id); +} + +/** Every notice id a dismissal POST may name: built-ins + registered lanes B/C. */ +export function knownNoticeIds(): ReadonlySet { + const all = new Set(BUILTIN_NOTICES.map((b) => b.id)); + for (const id of dynamicIds) all.add(id); + return all; +} + +interface VisibleNoticesOptions { + dismissed: ReadonlySet; + instanceDismissed?: ReadonlySet; +} + +/** Lane-A visible set: active predicates AND dismiss mode, priority-sorted. + * Wire shape strips the active function — plain records only. */ +export function visibleBuiltInNotices(ctx: BuiltInContext, opts: VisibleNoticesOptions): NoticeRecord[] { + const live = BUILTIN_NOTICES.filter((b) => b.active(ctx) && noticeVisible(b, opts.dismissed, opts.instanceDismissed)); + return sortNotices(live).map(({ active: _active, ...wire }) => wire); +} diff --git a/src/pck-notice.ts b/src/pck-notice.ts index 55b5041e59..e1e0424871 100644 --- a/src/pck-notice.ts +++ b/src/pck-notice.ts @@ -1,63 +1,106 @@ -// #5079: one-time upgrade notice for the prompt_cache.routing default flip -// (auto → disabled). bili no longer writes its own prompt_cache_key by -// default; upstreams that ROUTE prefix cache by that field (body-keyed relays -// like the workbuddy panel, header-keyed ones like the workbuddy hub, and -// account-pool aggregators) lose their affinity signal unless the user turns -// the switch back on. The notice is shown until dismissed — once per proxy -// instance, persisted beside instances.json (same discipline as -// host-plugin-notice.ts: server-side, not per-browser, best-effort fs that -// never fails a request or a page load). It is only active while the EFFECTIVE -// routing is "disabled": users who already chose auto/enabled made the call -// themselves and are not nagged. - +// #5079/#5469 → #5548: shared dismissal store for upgrade notices. P1 moved the +// notice declarations into src/notices.ts (the registry); this module keeps only +// the persistence layer — the store file, the allowlist-checked writes, and the +// pck predicate (still consumed by acp-status.ts / server.ts startup log). +// #5548 P2: store keys upgrade from bare ids to `id@hash16(record)` (content +// edits re-show once), the array keeps insertion order under a 200-key FIFO cap, +// and instance-mode dismissals live in memory only. import fs from "node:fs"; import path from "node:path"; import { stateDir } from "./paths.js"; +import { BUILTIN_NOTICES, PCK_NOTICE_ID, WM_FASTPATH_INTRO_ID, dismissKeyOf, knownNoticeIds, type NoticeRecord } from "./notices.js"; -export const PCK_NOTICE_ID = "pck-default-off"; -// #5403 watermark fastpath experimental-grey intro (owner ruling 2026-11-02): -// same one-time-per-instance store, its own id so dismissal stays independent. -export const WM_FASTPATH_INTRO_ID = "wm-fastpath-intro"; - -const UPGRADE_NOTICE_IDS: ReadonlySet = new Set([PCK_NOTICE_ID, WM_FASTPATH_INTRO_ID]); +export { PCK_NOTICE_ID, WM_FASTPATH_INTRO_ID }; const DISMISS_FILE = "upgrade-notices.json"; +const MAX_STORED_KEYS = 200; // #5548 §4.4 FIFO cap function stateDirFor(env: NodeJS.ProcessEnv): string { const v = env.XDG_STATE_HOME; return v && v.length > 0 ? path.join(path.resolve(v), "billion-context") : stateDir(); } -export function dismissedUpgradeNotices(env: NodeJS.ProcessEnv = process.env): ReadonlySet { +export function dismissedUpgradeNotices(env: NodeJS.ProcessEnv = process.env): Set { try { const raw = fs.readFileSync(path.join(stateDirFor(env), DISMISS_FILE), "utf8"); const doc: unknown = JSON.parse(raw); - const arr = doc && typeof doc === "object" && Array.isArray((doc as { dismissed?: unknown })["dismissed"]) - ? ((doc as { dismissed: unknown[] })["dismissed"]) - : []; - return new Set(arr.filter((x): x is string => typeof x === "string")); + if (doc && typeof doc === "object" && Array.isArray((doc as { dismissed?: unknown }).dismissed)) { + return new Set((doc as { dismissed: unknown[] }).dismissed.filter((x): x is string => typeof x === "string")); + } } catch { - return new Set(); + // absent/unreadable/corrupt → empty set (first run or fresh profile) } + return new Set(); +} + +/** Ordered key list as persisted — insertion order IS the FIFO order. */ +function readStoreKeys(env: NodeJS.ProcessEnv): string[] { + return [...dismissedUpgradeNotices(env)]; +} + +function appendKey(key: string, env: NodeJS.ProcessEnv): void { + const keys = readStoreKeys(env); + if (!keys.includes(key)) keys.push(key); + if (keys.length > MAX_STORED_KEYS) keys.splice(0, keys.length - MAX_STORED_KEYS); + const dir = stateDirFor(env); + fs.mkdirSync(dir, { recursive: true }); + fs.writeFileSync(path.join(dir, DISMISS_FILE), JSON.stringify({ dismissed: keys }, null, 2) + "\n"); } -/** Idempotent. Returns false only for unknown notice ids. */ +/** Legacy entry point (#5079/#5469 suites + pre-P2 stores): writes the bare id + * key, which noticeVisible() still honors so an upgrade never re-shows an + * already-closed banner. New callers write id@hash16 keys via + * dismissPersistentNotice(). */ export function dismissUpgradeNotice(id: string, env: NodeJS.ProcessEnv = process.env): boolean { - if (!UPGRADE_NOTICE_IDS.has(id)) return false; - const set = new Set(dismissedUpgradeNotices(env)); - set.add(id); + if (!knownNoticeIds().has(id)) return false; + try { + appendKey(id, env); + } catch { + // best-effort persistence: nothing to report if the state dir is unwritable + } + return true; +} + +/** Persistent dismissal under the #5548 hash key (`id@hash16`); a content edit + * changes the hash and re-shows the banner once. */ +export function dismissPersistentNotice(key: string, env: NodeJS.ProcessEnv = process.env): boolean { + const at = key.indexOf("@"); + if (at <= 0) return false; + if (!knownNoticeIds().has(key.slice(0, at))) return false; try { - const dir = stateDirFor(env); - fs.mkdirSync(dir, { recursive: true }); - fs.writeFileSync(path.join(dir, DISMISS_FILE), JSON.stringify({ dismissed: [...set].sort() }, null, 2) + "\n"); + appendKey(key, env); } catch { - // best effort: dismissal intent still holds for this process's answers + // best-effort persistence: nothing to report if the state dir is unwritable } return true; } -/** The notice is visible while self-stamping is off and the user has not closed it. */ +// Instance-mode dismissals (§4.4): process memory only — they re-show on restart. +const instanceDismissed = new Set(); + +export function instanceDismissedNotices(): ReadonlySet { + return instanceDismissed; +} + +export function recordInstanceNoticeDismissal(id: string): void { + instanceDismissed.add(id); +} + +function builtinWireRecord(id: string): NoticeRecord | undefined { + const b = BUILTIN_NOTICES.find((x) => x.id === id); + if (!b) return undefined; + const { active: _active, ...wire } = b; + return wire; +} + +/** True when the prompt-cache routing default-flip notice should surface: + * routing is disabled and the user has not dismissed it (legacy bare-id key + * or the #5548 hash key). */ export function pckNoticeActive(routing: string, env: NodeJS.ProcessEnv = process.env): boolean { if (routing !== "disabled") return false; - return !dismissedUpgradeNotices(env).has(PCK_NOTICE_ID); + const store = dismissedUpgradeNotices(env); + if (store.has(PCK_NOTICE_ID)) return false; + const wire = builtinWireRecord(PCK_NOTICE_ID); + if (wire && store.has(dismissKeyOf(wire))) return false; + return true; } diff --git a/src/remote-announcements.ts b/src/remote-announcements.ts new file mode 100644 index 0000000000..17134eb5eb --- /dev/null +++ b/src/remote-announcements.ts @@ -0,0 +1,168 @@ +// #5548 P3 lane C: remote announcements from a single GitHub raw file on +// master (publish = git push, retract = delete entry + push). Mirrors the +// advisory watcher's shape — same cadence, fail-open, warnOnce logging — but +// is a pure read-only display feed: never blocks, installs, or restarts. +// A/B render first; lane C merges in whenever its entries arrive. +import fs from "node:fs"; +import path from "node:path"; +import { log as loggerLog, type Logger } from "./logger.js"; +import { cacheDir } from "./paths.js"; +import { CHECK_INTERVAL_MS, egressDispatcher } from "./update.js"; +import type { FetchOptions } from "./fetch-util.js"; +import { applyRemoteDoc, REMOTE_DOC_CAP_BYTES, REMOTE_DOC_VERSION, registerDynamicNoticeIds, type NoticeRecord } from "./notices.js"; + +export const DEFAULT_REMOTE_ANNOUNCEMENTS_URL = "https://raw.githubusercontent.com/ranxianglei/billion-context/master/announcements/remote.json"; +const FETCH_TIMEOUT_MS = 8000; +const SNAPSHOT_FILE = "announcements-remote-snapshot.json"; + +interface RemoteAnnouncementOptions { + /** Override the feed URL (env BILI_ANNOUNCEMENTS_URL wins over this). */ + url?: string; + resolveProxy?: (url: string) => string | undefined; + log?: Logger; + /** Test seam: shorten the fetch timeout for slow-response injection. */ + timeoutMs?: number; +} + +interface RemoteState { + entries: NoticeRecord[]; + lastError?: string; + lastCheckAt?: number; +} + +let state: RemoteState = { entries: [] }; +let timer: ReturnType | undefined; +let inFlight = false; + +export function remoteAnnouncementEntries(): readonly NoticeRecord[] { + return state.entries; +} + +/** Test seam: drop in-memory state (and warn-once keys) so each test starts + * from a clean slate — otherwise an earlier test's identical failure key + * swallows a later test's expected warning. */ +export function _resetRemoteAnnouncementsForTest(): void { + state = { entries: [] }; + warned.clear(); +} + +export function _stopRemoteAnnouncementWatcherForTest(): void { + if (timer) { + clearInterval(timer); + timer = undefined; + } +} + +/** BILI_ANNOUNCEMENTS_URL is the firewall-relay escape hatch (§五.3): + * path-relocation class env, owner-approved design point. */ +export function resolveRemoteUrl(envUrl: string | undefined, optsUrl: string | undefined): string { + const pick = (v: string | undefined): string | undefined => { + const t = v?.trim(); + return t && t.length > 0 ? t : undefined; + }; + return pick(envUrl) ?? pick(optsUrl) ?? DEFAULT_REMOTE_ANNOUNCEMENTS_URL; +} + +function snapshotFile(): string { + return path.join(cacheDir(), SNAPSHOT_FILE); +} + +/** Last-known-good snapshot from a previous session (§五.2): a failed fetch + * keeps showing this instead of nothing. Absent/corrupt → no lane C. */ +export function loadRemoteSnapshot(): NoticeRecord[] { + try { + const doc: unknown = JSON.parse(fs.readFileSync(snapshotFile(), "utf8")); + const res = applyRemoteDoc(doc); + if (!res.ok) return []; + registerDynamicNoticeIds(res.entries.map((r) => r.id)); + return res.entries; + } catch { + return []; + } +} + +function saveRemoteSnapshot(entries: readonly NoticeRecord[]): void { + try { + fs.mkdirSync(path.dirname(snapshotFile()), { recursive: true }); + fs.writeFileSync(snapshotFile(), JSON.stringify({ v: REMOTE_DOC_VERSION, entries }, null, 2) + "\n"); + } catch { + // cache dir unwritable: in-memory copy still serves this session + } +} + +const warned = new Set(); +function warnOnce(log: Logger, key: string, message: string): void { + if (warned.has(key)) return; + warned.add(key); + log("warn", message); +} + +/** One fetch cycle. Fail-open by design: any error keeps the previous + * entries (snapshot-seeded at startup), logs warnOnce, never throws. */ +export async function runRemoteCheck(opts: RemoteAnnouncementOptions = {}): Promise { + if (inFlight) return; + inFlight = true; + const log = opts.log ?? loggerLog; + try { + const url = resolveRemoteUrl(process.env.BILI_ANNOUNCEMENTS_URL, opts.url); + let body: string; + try { + const dispatcher = egressDispatcher({ resolveProxy: opts.resolveProxy }, url); + const init: FetchOptions = { + method: "GET", + headers: { Accept: "application/json" }, + signal: AbortSignal.timeout(opts.timeoutMs ?? FETCH_TIMEOUT_MS), + ...(dispatcher ? { dispatcher } : {}), + }; + const res = await fetch(url, init as RequestInit); + if (!res.ok) throw new Error(`remote announcements returned HTTP ${res.status}`); + body = await res.text(); + } catch (e) { + state.lastError = String(e); + warnOnce(log, `fetch:${String(e)}`, `[notices] remote check failed (${String(e)}) — continuing with last-known-good`); + return; + } + if (Buffer.byteLength(body, "utf8") > REMOTE_DOC_CAP_BYTES) { + state.lastError = `document exceeds ${REMOTE_DOC_CAP_BYTES} bytes`; + warnOnce(log, "size", `[notices] ignoring oversized remote document (>${REMOTE_DOC_CAP_BYTES} bytes)`); + return; + } + let doc: unknown; + try { + doc = JSON.parse(body); + } catch (e) { + state.lastError = String(e); + warnOnce(log, "parse", `[notices] ignoring malformed remote document (${String(e)})`); + return; + } + const res = applyRemoteDoc(doc); + if (!res.ok) { + state.lastError = res.reason; + warnOnce(log, `doc:${res.reason}`, `[notices] ignoring remote document: ${res.reason}`); + return; + } + for (const w of res.warnings) warnOnce(log, `entry:${w}`, `[notices] remote document: ${w}`); + state = { entries: res.entries, lastCheckAt: Date.now() }; + registerDynamicNoticeIds(res.entries.map((r) => r.id)); + saveRemoteSnapshot(res.entries); + } finally { + inFlight = false; + } +} + +/** Start the periodic feed: same cadence as the update check (§五.2), first + * fetch after a short startup delay like the advisory watcher, unref'd so it + * never holds the process open. */ +export function startRemoteAnnouncementWatcher(opts: RemoteAnnouncementOptions = {}): void { + if (timer) return; + const log = opts.log ?? loggerLog; + state.entries = loadRemoteSnapshot(); + log("info", `[notices] remote feed enabled (checking every ${CHECK_INTERVAL_MS / 1000 | 0}s, independent of auto-update)`); + setTimeout(() => { + void runRemoteCheck(opts); + }, 10_000); + timer = setInterval(() => { + void runRemoteCheck(opts); + }, CHECK_INTERVAL_MS); + timer.unref?.(); +} diff --git a/src/server/admin.ts b/src/server/admin.ts index da947bc792..07c530ccdb 100644 --- a/src/server/admin.ts +++ b/src/server/admin.ts @@ -23,7 +23,11 @@ import { getUnrecognizedPathStats } from "./observability.js"; import { getStore } from "../persist.js"; import { BodyTooLargeError, headerValue, readBody } from "../server.js"; import { dismissHostPlugin, visibleHostPluginNotices } from "../host-plugin-notice.js"; -import { dismissedUpgradeNotices, dismissUpgradeNotice, PCK_NOTICE_ID, pckNoticeActive, WM_FASTPATH_INTRO_ID } from "../pck-notice.js"; +import { dismissedUpgradeNotices, dismissPersistentNotice, instanceDismissedNotices, recordInstanceNoticeDismissal } from "../pck-notice.js"; +import { BUILTIN_NOTICES, dismissKeyOf, dismissModeOf, mergeNoticeLayers, noticeVisible, passesTargeting, registerDynamicNoticeIds, sortNotices, visibleBuiltInNotices, type NoticeRecord, type TargetingContext } from "../notices.js"; +import { installIdOf } from "../install-id.js"; +import { loadLocalAnnouncements } from "../local-announcements.js"; +import { remoteAnnouncementEntries } from "../remote-announcements.js"; import { featureOverrideAbsent } from "../rollout.js"; import { listSessions, memoryReport, peekSession, splitSessionWarnings, totalInFlight } from "../session.js"; import { BILI_TUNNEL_HEADER } from "../tunnel-guard.js"; @@ -438,8 +442,14 @@ export async function handleAdminRoute(req: http.IncomingMessage, res: http.Serv if (req.method === "POST" && req.url?.startsWith("/__bili/conflicts/clear")) return handleConflictsClear(req.url, res, log); // #2861: silence a known host-side plugin notice for this environment. if (req.method === "POST" && req.url === "/__bili/host-plugins/dismiss") return handleHostPluginDismiss(req, res, log); - // #5079: silence the prompt_cache.routing default-off upgrade notice. + // #5079: silence a known notice of the unified announcement surface. if (req.method === "POST" && req.url === "/__bili/notices/dismiss") return handleNoticeDismiss(req, res, log); + // #5548: unified announcement payload for non-overview consumers. + if (req.method === "GET" && req.url === "/__bili/notices") { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ notices: collectVisibleNotices(opts) }, null, 2)); + return; + } // #3923/#3959: dismiss the surfaced split-brain notice (body {"sessionId": string}). if (req.method === "POST" && req.url === "/__bili/storage/split-brain/ack") return handleSplitBrainAck(req, res, log); if (req.method === "GET" && req.url === "/__bili/upstream") { @@ -1040,12 +1050,25 @@ async function handleNoticeDismiss(req: http.IncomingMessage, res: http.ServerRe res.end(JSON.stringify({ error: 'expected JSON body {"id": ""}' }, null, 2)); return; } - if (!dismissUpgradeNotice(id)) { + const rec = findNoticeRecord(id); + if (!rec) { res.writeHead(400, { "content-type": "application/json" }); res.end(JSON.stringify({ error: `unknown notice: ${id}` }, null, 2)); return; } - log("info", `[notices] notice ${id} dismissed via web UI`); + // #5548 §4.4 dismiss modes: always → success without writing; instance → + // process memory only; persistent → the id@hash16 store key. + const mode = dismissModeOf(rec); + if (mode === "always") { + log("info", `[notices] dismiss ignored for ${id} (always-shown)`); + } else if (mode === "instance") { + recordInstanceNoticeDismissal(id); + } else if (!dismissPersistentNotice(dismissKeyOf(rec))) { + res.writeHead(400, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: `unknown notice: ${id}` }, null, 2)); + return; + } + log("info", `[notices] notice ${id} dismissed via web UI (${mode})`); res.writeHead(200, { "content-type": "application/json" }); res.end(JSON.stringify({ ok: true }, null, 2)); } @@ -1302,6 +1325,45 @@ function sendResignStatus(res: http.ServerResponse): void { }, null, 2)); } +// #5548 P2: unified announcement payload — lane A built-ins + lane B local JSON +// (B overrides A on id collision, §三), targeting gates (versions/percent/ +// since/until) applied to every source, priority-sorted server-side. The wm +// intro stays visible to EVERY install on its first upgrade until closed unless +// an explicit rollout override exists. +function collectVisibleNotices(opts: ProxyOptions): NoticeRecord[] { + const dismissed = dismissedUpgradeNotices(process.env); + const inst = instanceDismissedNotices(); + const tctx: TargetingContext = { version: VERSION, installId: installIdOf(process.env), now: Date.now() }; + const local = loadLocalAnnouncements(process.env).records; + const remote = [...remoteAnnouncementEntries()]; + registerDynamicNoticeIds(local.map((r) => r.id)); + registerDynamicNoticeIds(remote.map((r) => r.id)); + const merged = mergeNoticeLayers([ + local.filter((r) => passesTargeting(r, tctx) && noticeVisible(r, dismissed, inst)), + remote.filter((r) => passesTargeting(r, tctx) && noticeVisible(r, dismissed, inst)), + visibleBuiltInNotices( + { + promptCacheRouting: effectivePromptCacheRouting(opts.promptCache), + wmOverridePresent: !featureOverrideAbsent("pluginFullResendWatermark", loadConfigFile().rollout), + }, + { dismissed, instanceDismissed: inst }, + ), + ]); + return sortNotices(merged).filter((r) => passesTargeting(r, tctx)); +} + +/** Dismissal lookup by id — local lane first (B > C > A), then built-ins. */ +function findNoticeRecord(id: string): NoticeRecord | undefined { + const local = loadLocalAnnouncements(process.env).records.find((r) => r.id === id); + if (local) return local; + const remote = remoteAnnouncementEntries().find((r) => r.id === id); + if (remote) return remote; + const b = BUILTIN_NOTICES.find((x) => x.id === id); + if (!b) return undefined; + const { active: _active, ...wire } = b; + return wire; +} + async function sendOverview(res: http.ServerResponse, opts: ProxyOptions, laneHost?: string): Promise { let diskVersion: string | undefined; let stale = false; @@ -1338,16 +1400,7 @@ async function sendOverview(res: http.ServerResponse, opts: ProxyOptions, laneHo updatePolicy: updatePolicyPayload(), pluginAdvisories: detectCostAdvisories(process.env), hostPluginNotices: visibleHostPluginNotices(process.env), - pckNotice: { id: PCK_NOTICE_ID, active: pckNoticeActive(effectivePromptCacheRouting(opts.promptCache)) }, - wmNotice: { - id: WM_FASTPATH_INTRO_ID, - // #5403 experimental-grey intro: visible to EVERY install on its - // first upgrade to this build until closed — unless the user - // already wrote an explicit rollout override (they made the call). - active: - !dismissedUpgradeNotices(process.env).has(WM_FASTPATH_INTRO_ID) && - featureOverrideAbsent("pluginFullResendWatermark", loadConfigFile().rollout), - }, + notices: collectVisibleNotices(opts), inFlight: totalInFlight(), blindTunnels: getBlindTunnelStats(), conflicts: summarizeConflicts(listSessions()), diff --git a/src/web/client.ts b/src/web/client.ts index e3bc1d4f70..1868c497b2 100644 --- a/src/web/client.ts +++ b/src/web/client.ts @@ -419,28 +419,87 @@ export const WEB_CLIENT = `(function () { return html; } window.bili_hostPluginBanner = bili_hostPluginBanner; - // #5079: one-time-per-instance upgrade notice — prompt_cache_key - // self-stamping is off by default now. Server decides active (effective - // routing "disabled" && not dismissed); pure builder, same test-seam - // convention as the banners above. - function bili_pckNoticeBanner(s) { - let html = '"; - html += '
' + escapeHtml(s.desc) + '
"; - html += '
' + escapeHtml(s.hint) + "
"; - return html; + // #5548: unified announcement surface (design v2 lane A). The server ships + // priority-sorted visible records whose copy rides the record (string or + // locale object, fallback locale → zh-CN → en → any); elements are built + // with textContent only — zero innerHTML with external data (design §4.2). + function bili_noticeText(v, lang) { + if (typeof v === "string") return v; + if (v && typeof v === "object") { + for (const k of [lang, "zh-CN", "en"]) if (typeof v[k] === "string" && v[k]) return v[k]; + for (const k of Object.keys(v)) if (typeof v[k] === "string" && v[k]) return v[k]; + } + return ""; } - window.bili_pckNoticeBanner = bili_pckNoticeBanner; - // #5403: one-time-per-instance intro for the watermark fastpath - // experimental grey rollout — same server-side dismissal convention as - // above (own notice id, visible on every install until closed or an - // explicit rollout override exists); pure builder + same test seam. - function bili_wmNoticeBanner(s) { - let html = '"; - html += '
' + escapeHtml(s.desc) + '
"; - html += '
' + escapeHtml(s.hint) + "
"; - return html; + window.bili_noticeText = bili_noticeText; + const NOTICE_MAX_HEADER_BANNERS = 5; + function bili_buildNoticeEl(n) { + const el = document.createElement("div"); + el.className = "banner show " + String(n.tone || "info"); + const title = document.createElement("div"); + title.className = "banner-title"; + title.textContent = bili_noticeText(n.title, locale); + el.appendChild(title); + const row = document.createElement("div"); + row.className = "alert-row"; + const span = document.createElement("span"); + span.textContent = bili_noticeText(n.desc, locale); + row.appendChild(span); + if (n.ctaUrl && n.ctaLabel) { + const link = document.createElement("a"); + link.className = "mono"; + link.href = String(n.ctaUrl); + link.target = "_blank"; + link.rel = "noopener"; + link.textContent = bili_noticeText(n.ctaLabel, locale); + row.appendChild(link); + } + if (n.dismiss !== "always") { + const btn = document.createElement("button"); + btn.className = "btn sm notice-dismiss"; + btn.textContent = t("notices.dismiss_btn"); + btn.addEventListener("click", async () => { + busy(btn, true); + try { + await json(API_ROOT + "/notices/dismiss", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ id: typeof n.id === "string" ? n.id : "" }) }); + toast(t("notices.dismissed"), "ok"); + await loadOverview(true); + } catch (e) { + toast(e.message, "err"); + } finally { + busy(btn, false); + } + }); + row.appendChild(btn); + } + el.appendChild(row); + if (n.hint !== undefined) { + const hint = document.createElement("div"); + hint.className = "dim small"; + hint.style.marginTop = "6px"; + hint.textContent = bili_noticeText(n.hint, locale); + el.appendChild(hint); + } + return el; + } + window.bili_buildNoticeEl = bili_buildNoticeEl; + function renderAnnouncementBanners(list) { + const host = $("notices-banners"); + if (!host) return; + while (host.firstChild) host.removeChild(host.firstChild); + const card = $("notice-list-card"); + const ovf = $("notice-list"); + if (ovf) while (ovf.firstChild) ovf.removeChild(ovf.firstChild); + if (card) card.hidden = true; + if (!Array.isArray(list) || !list.length) return; + const head = list.slice(0, NOTICE_MAX_HEADER_BANNERS); + for (const n of head) host.appendChild(bili_buildNoticeEl(n)); + const rest = list.slice(NOTICE_MAX_HEADER_BANNERS); + if (rest.length && card && ovf) { + card.hidden = false; + for (const n of rest) ovf.appendChild(bili_buildNoticeEl(n)); + } } - window.bili_wmNoticeBanner = bili_wmNoticeBanner; function $(id) { return document.getElementById(id); } function toast(message, kind) { const host = $("toast-host"); @@ -1583,65 +1642,13 @@ function changelogCatOf(rawLine) { hb.innerHTML = ""; } } - // #5079: prompt_cache_key default-off upgrade notice — dismissal is - // server-side (POST /__bili/notices/dismiss) so one close silences - // every browser; the banner re-renders from the refetched overview. - const nb = $("pck-notice-banner"); - if (nb) { - const pn = d.pckNotice && typeof d.pckNotice === "object" ? d.pckNotice : null; - if (pn && pn.active === true) { - nb.hidden = false; - nb.classList.add("show"); - nb.innerHTML = bili_pckNoticeBanner({ on: t("pckoff.on"), desc: t("pckoff.desc"), hint: t("pckoff.hint"), dismiss: t("pckoff.dismiss_btn") }); - for (const btn of Array.from(nb.querySelectorAll(".pck-notice-dismiss"))) { - btn.addEventListener("click", async () => { - busy(btn, true); - try { - await json(API_ROOT + "/notices/dismiss", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ id: typeof pn.id === "string" ? pn.id : "" }) }); - toast(t("pckoff.dismissed"), "ok"); - await loadOverview(true); - } catch (e) { - toast(e.message, "err"); - } finally { - busy(btn, false); - } - }); - } - } else { - nb.hidden = true; - nb.classList.remove("show"); - nb.innerHTML = ""; - } - } - // #5403: watermark fastpath experimental-grey intro — all installs see - // it on first upgrade until closed (server-side, like #5079 above). - const wnb = $("wm-notice-banner"); - if (wnb) { - const wn = d.wmNotice && typeof d.wmNotice === "object" ? d.wmNotice : null; - if (wn && wn.active === true) { - wnb.hidden = false; - wnb.classList.add("show"); - wnb.innerHTML = bili_wmNoticeBanner({ on: t("wmfast.on"), desc: t("wmfast.desc"), hint: t("wmfast.hint"), dismiss: t("wmfast.dismiss_btn") }); - for (const btn of Array.from(wnb.querySelectorAll(".wm-notice-dismiss"))) { - btn.addEventListener("click", async () => { - busy(btn, true); - try { - await json(API_ROOT + "/notices/dismiss", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ id: typeof wn.id === "string" ? wn.id : "" }) }); - toast(t("wmfast.dismissed"), "ok"); - await loadOverview(true); - } catch (e) { - toast(e.message, "err"); - } finally { - busy(btn, false); - } - }); - } - } else { - wnb.hidden = true; - wnb.classList.remove("show"); - wnb.innerHTML = ""; - } - } + // #5548: unified announcement surface — one loop over the server's + // visible records (lane A built-ins today; B/C merge in later phases). + // Dismissal stays server-side (POST /__bili/notices/dismiss) so one + // close silences every browser; banners re-render from the refetched + // overview. More than NOTICE_MAX_HEADER_BANNERS overflow into the + // overview's 公告列表 card. + renderAnnouncementBanners(Array.isArray(d.notices) ? d.notices : []); // #1682: global upstream-connection alert banner — visible on every view, // one row per active alert (no stacking), dismiss per alert instance. latestAlerts = Array.isArray(d.alerts) ? d.alerts : []; diff --git a/src/web/i18n.ts b/src/web/i18n.ts index 36cc98dfc2..7e1807eda1 100644 --- a/src/web/i18n.ts +++ b/src/web/i18n.ts @@ -284,16 +284,10 @@ export const MESSAGES: Record> = { "pck.on": "缓存路由键(prompt_cache_key)已停用", "pck.sessions": "受影响会话:{ids}", "pck.hint": "该上游拒绝携带 prompt_cache_key 的请求(带键 400、去键成功,A/B 实证),bili 重启前不再向其发送该键。若这是你的中转/号池,相关会话的前缀缓存亲和可能下降。", - "pckoff.on": "prompt_cache_key 自注入已默认关闭(升级提示)", - "pckoff.desc": "本版本起 bili 不再自行向请求添加 OpenAI 的 prompt_cache_key 字段(此前默认开启)。如果你的上游是按该字段路由前缀缓存的中转站或号池(如 workbuddy panel/hub 等),请打开开关恢复注入,否则流量可能被随机路由、频繁未命中缓存。", - "pckoff.hint": "开关在「配置」页快速配置里(promptCache.routing:disabled/auto/enabled),也可直接改 billion-context.json;直连官方 API 的用户可忽略此提示。", - "pckoff.dismiss_btn": "我知道了", - "pckoff.dismissed": "已关闭该提示", - "wmfast.on": "新功能提示:大上下文增量解析(增量压缩过渡功能 · 实验性)", - "wmfast.desc": "长会话每轮都要把整个历史重新发给代理解析一遍,现在改为增量解析:只解析本轮新增的那部分,优化长会话的解析性能(发给模型的内容不变;有任何疑点,当轮自动回退老路径并重新对齐)。当前版本对约 5% 的安装灰度开启,属实验性功能。", - "wmfast.hint": "开关在「配置」页快速配置(大上下文增量解析),也可在 billion-context.json 写 rollout.features.pluginFullResendWatermark: true/false,或设环境变量 BILI_ROLLOUT_PLUGIN_FULL_RESEND_WATERMARK=1/0。", - "wmfast.dismiss_btn": "我知道了", - "wmfast.dismissed": "已关闭该提示", + // #5548: banner copy now rides the notice records (src/notices.ts); only action words stay here. + "notices.dismiss_btn": "我知道了", + "notices.dismissed": "已关闭该提示", + "notices.list_title": "公告列表", "ses.title": "会话", "ses.sub": "覆盖内存与磁盘的全部会话 · 点击行查看详情、轨迹与交接文档", "ses.search_ph": "搜索标题、标签或 id…", @@ -1018,16 +1012,10 @@ export const MESSAGES: Record> = { "pck.on": "Cache routing key (prompt_cache_key) withheld", "pck.sessions": "Affected session(s): {ids}", "pck.hint": "This upstream rejected requests carrying prompt_cache_key (400 with the key, accepted without it), so bili withholds the key there until restart. If this is your relay/account pool, prefix-cache affinity may degrade for those sessions.", - "pckoff.on": "prompt_cache_key self-stamping is now off by default (upgrade notice)", - "pckoff.desc": "Since this version bili no longer adds OpenAI's prompt_cache_key field to requests on its own (previously on by default). If your upstream routes prefix cache by this key (relays such as workbuddy panel/hub, account pools), turn the switch back on — otherwise traffic may route randomly and miss the prefix cache.", - "pckoff.hint": "Switch: Configuration page → quick config (promptCache.routing: disabled/auto/enabled), or edit billion-context.json directly; direct official-API users can ignore this notice.", - "pckoff.dismiss_btn": "Got it", - "pckoff.dismissed": "Notice dismissed", - "wmfast.on": "New: incremental parsing for large contexts (incremental-compression bridge feature, experimental)", - "wmfast.desc": "Long conversations used to re-send their entire history for parsing on every turn. Now only the new part of each turn is parsed — faster parsing on long sessions (the request sent to the model is unchanged; any doubt falls back to the old path for that turn and re-aligns). This release rolls it out to roughly 5% of installs; it is marked experimental.", - "wmfast.hint": "Switch: Configuration page → quick config (incremental parsing for large contexts), or write rollout.features.pluginFullResendWatermark: true/false in billion-context.json, or set BILI_ROLLOUT_PLUGIN_FULL_RESEND_WATERMARK=1/0.", - "wmfast.dismiss_btn": "Got it", - "wmfast.dismissed": "Notice dismissed", + // #5548: banner copy now rides the notice records (src/notices.ts); only action words stay here. + "notices.dismiss_btn": "Got it", + "notices.dismissed": "Notice dismissed", + "notices.list_title": "Announcements", "ses.title": "Sessions", "ses.sub": "Every session across memory and disk — click a row for details, trajectory & handoff document", "ses.search_ph": "Search title, label or id…", diff --git a/src/web/page.ts b/src/web/page.ts index 52439b4fb6..9f53d671ea 100644 --- a/src/web/page.ts +++ b/src/web/page.ts @@ -40,8 +40,7 @@ export function renderPage(origin: string, version: string, embed = false, theme - - +
@@ -72,6 +71,7 @@ export function renderPage(origin: string, version: string, embed = false, theme
${zh("ov.recent")}${zh("ov.view_all")}
${zh("ses.th_title")}${zh("ses.th_client")}${zh("ses.th_proto")}${zh("ses.th_upstream")}${zh("ses.th_reqs")}${zh("ses.th_ctx")}${zh("ses.th_input")}${zh("ses.th_saved")}${zh("ses.th_hit")}${zh("ses.th_switches")}${zh("ses.th_folds")}${zh("ses.th_blocks")}${zh("ses.th_seen")}
+