From 1248205a0001d1f7d6c31bdda81a09ce5908d6d7 Mon Sep 17 00:00:00 2001 From: ci-check Date: Wed, 18 Nov 2026 19:39:58 +0800 Subject: [PATCH 1/3] fix(update): respect the dsh desktop profile's pnpm minimumReleaseAge in the in-place auto-refresh (#7139) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The dsh desktop profile is a pnpm-managed project whose host can pin a package-maturity policy (minimumReleaseAge + minimumReleaseAgeExclude). The desktop in-place refresh (refreshDshDesktopCopy) installs the selected release's tarball directly and converges the profile ledger, bypassing pnpm's resolver — so a fresh release could land before its maturity window elapsed, leaving the manifest requesting a version the lockfile cannot resolve and breaking every pnpm operation on the profile until it matured. Add a maturity gate (new src/release-maturity.ts) on the auto-refresh path: - resolve the profile's effective pnpm policy with pnpm's own precedence (env pnpm_config_* > env npm_config_* > project pnpm-workspace.yaml > project .npmrc > user ~/.npmrc > global config.yaml > $PREFIX/etc/npmrc, per-key first definition wins; a null file value is 'not provided' and does not shadow a lower-precedence positive value); - match minimumReleaseAgeExclude with pnpm's version-policy grammar (bare/wildcard name = whole package, name@v1 || v2 = exact union, * / ? name globs); - read the target release's publish time from the registry packument's time map (fetched at most once per version; cached under cacheDir(), in-process memo as fallback); - verdicts: no-policy / excluded / mature -> proceed; immature -> defer without a strike (it matures on its own); undatable policy or publish time -> fail closed (defer + strike, the host policy is never bypassed or silently excluded). The advisory/panel install path (forceInstallVersion) is out of scope: it targets already-mature older versions. No new bili config keys — the gate reads the host's pnpm config. Regression tests: tests/release-maturity.test.ts (19: policy precedence, source forms, malformed values fail closed, exclude grammar, gate verdicts, time cache) and 7 new cases in tests/update-desktop-inplace.test.ts (immature defers with copy/manifest/lockfile byte-identical, excluded proceeds, other-version exclude does not lift the gate, mature proceeds, no-time / network-failure / malformed-policy fail closed). --- CHANGELOG.md | 1 + docs/architecture/update.md | 4 +- docs/architecture/update.zh-CN.md | 4 +- reference/architecture.md | 1 + src/release-maturity.ts | 433 +++++++++++++++++++++++++++ src/update.ts | 28 +- tests/release-maturity.test.ts | 409 +++++++++++++++++++++++++ tests/update-desktop-inplace.test.ts | 390 +++++++++++++++++++++++- 8 files changed, 1264 insertions(+), 6 deletions(-) create mode 100644 src/release-maturity.ts create mode 100644 tests/release-maturity.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 978e98698a..87f296acac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag - **系统架构文档体系落地(owner 指令 2026-11-08):`docs/architecture/` 成为实时维护的架构枢纽**:新增 `docs/architecture.md` / `docs/architecture.zh-CN.md` 总枢纽(整体架构图中英双版转正 + 8 模块总表),首个样板模块页 `docs/architecture/storage.{md,zh-CN.md}`(统一存储:职责/原理/工作机制/硬不变量,配模块图中英双版)。文件级地图 `reference/architecture.md` 补齐 125 个缺失条目(新存储层 / marker / fastpath / lanes 等全部新模块)并升级为 CI 强制全量覆盖。新增 CI 车道 `architecture-docs`(`tools/check-architecture-docs.mjs`):地图覆盖全 `src/` 文件、总表↔页面成对、图 svg 优先 + png 备选齐备、SVG 基础良构(未转义 `&` 曾让英文图线上破图)。AGENTS.md「Documentation Layout」新增实时维护规则:改结构 ⇒ 同 PR 更新页 + 图 + 总表。其余 7 个模块页(pipeline / lanes / kernel / identity / fastpaths / webui / update)同一 PR 全部补齐——总表 8 行全 ✅(每页 = 中英双版文档 + 中英双版图)。 ### Fixed +- **dsh 桌面副本自动更新尊重宿主 pnpm minimumReleaseAge 成熟度窗口(#7139)**:DSH 桌面 profile 是 pnpm 管的(可配严格 `minimumReleaseAge`,如 1440 分钟),而 bili 桌面副本自动更新(`refreshDshDesktopCopy`)直接抓新版 tarball 安装,从不查宿主成熟度策略——新版没过窗口也照装,manifest/lockfile 随之收敛到新版,pnpm store sections 却只有旧版 resolution:profile 上任何无关的 pnpm 操作随后报 `ERR_PNPM_LOCKFILE_MISSING_DEPENDENCY` / `ERR_PNPM_RESOLUTION_POLICY_VIOLATIONS_UNHANDLED`,严格 lock-only 修复要等版本成熟才成功。现在自动更新路径在安装前做成熟度门:策略按 pnpm 自身优先级逐键解析(env `pnpm_config_*` > 项目 `pnpm-workspace.yaml` > 项目 `.npmrc` > 用户 `~/.npmrc` > 全局 `$XDG_CONFIG_HOME/pnpm/config.yaml` > `$PREFIX/etc/npmrc`,首个定义者胜),`minimumReleaseAgeExclude` 按 pnpm version-policy 语法匹配(裸名/无版本通配名 = 整包豁免,`name@v1 || v2` 精确版本并集,名字支持 `*`/`?` glob);发布时间取 registry 完整 packument 的 `time` 字段(缓存进 bili cache 目录,同版本不重复抓取,进程内 memo 兜底)。判定:无策略 / 被豁免 / 已成熟 → 照常进行;未成熟 → 本轮推迟(不记 strike,下轮重试,命中缓存时零流量);发布时间或策略无法确立(registry 失败/无 time 字段/值畸形/配置不可读)→ **fail closed**(记 strike + warn,副本/manifest/lockfile 一律不动,绝不绕过宿主策略)。**旧行为→新行为**:桌面副本自动更新路径此前无条件安装,现在未成熟版本被推迟(方向 = 尊重宿主显式策略);advisory 强制安装 / 面板安装路径不在本门范围(它们面向已成熟的旧版本)。不引入新 bili 配置键——读的是宿主 pnpm 配置。回归:tests/release-maturity.test.ts(19 例:策略优先级/各来源形态/畸形值 fail-closed/exclude 语法全集/门判定/时间缓存)、tests/update-desktop-inplace.test.ts(新增 7 例:未成熟推迟且副本+manifest+lockfile 字节级不变 / 整包豁免放行 / 他版本豁免不解除门 / 已成熟照常收敛 / 无 time fail-closed / 网络失败 fail-closed / 畸形策略在流量前 fail-closed)。 - **纯 SQLite 姿态下证实损坏不再静默降级 JSON,改为响亮拒启(owner 策略 2026-11-21,#7156 生产事故)**:quick_check 证实 index.db 损坏(含「首个 post-open 写入」撞损坏的大库/缓存判决分支,#7156 FIX4)且进程有效姿态是**纯 SQLite**(有效 mode full + sqlite 后端 + 无加密 + jsonWriter off = 2026-11-13 缺省翻转后的缺省,或显式 off)时,旧契约(响亮告警 + 不挂索引照常启动、JSON 信封服务)在这里会服务一个近乎为空的非持久视图,其后每一轮都只落在非持久副本里——静默丢持久历史,比损坏本身更糟(实测现场:0.1.216 的 3.2GB index.db 损坏后自动「降级 JSON」,面板显示 requests:2/上下文 0,而真实 1563 请求/124k 上下文/$12.41 成本全在索引里)。现在该姿态 REFUSES TO START:文件原样不动(不搬不拷不重建),日志与诊断面只给可操作的手工修复(备份 index.db/-wal/-shm → 手工移开 → 再启动,从双写期信封重建);`BILI_STORAGE_ALLOW_LEGACY_DEGRADE=1` 可有意识地恢复旧降级路径(永不缺省、不被任何配置值隐含)。其余姿态(显式双写 full、shadow、legacy、网络后端、加密)信封仍是真恢复来源,旧契约逐字节保持。姿态判定是纯函数(与 persist 的 jsonWriterOff 同一公式,两开关永不分歧);拒启被记录进诊断面(`/__bili/status` 与 diagnostics 可见「REFUSING TO START (pure-sqlite stance)」前缀)且每次启动都重新生效(latch 只短路进程内重扫)。回归:tests/issue7156-pure-sqlite-refusal.test.ts(姿态真值表 + 两种纯 sqlite 拼法与 file 层的损坏拒启/逃生阀/重复拒启 + 字节不变断言 + 双写姿态保留降级契约)、tests/issue4816-integrity-throw-quarantine.test.ts、tests/acp-status-storage-degraded.test.ts、tests/web-storage-banner.test.ts、tests/storage-durability.test.ts 与 storage 全量套件绿。 - **信封导入不再顶掉 index 已有的压缩块谱系与生命周期统计(#7156 事故第二现场,2026-11-21)**:#7156 的生产现场里,降级窗口(不挂索引、纯信封服务)内的一次会话保存把信封里的 32 个粗块结构写进了 `importRecord`——它把信封当成权威谱系:按 incoming 集合先删后插,index 里 103 个更细的压缩块(及其 summary/content 引用链)被整批删除,生命周期统计也从 requests:1563/124k 上下文/$12.41 成本回退成 requests:2/0。之后每一轮 preflight 都在 32 粗块的地基上折叠,上下文装不进模型窗口、会话不可用。现在 `ingestSession` 新增 `envelopeImport` 标记(仅 `UnifiedShadow.importRecord`/`importRecordAsync` 两个导入入口传):存储侧已有块谱系时**只采纳信封里 seq 更高的新消息**,块谱系与 stale 清理一律跳过(index 无块的真正首开导入不变,仍整体采纳信封结构);stats_json 同带单调守卫——envelopeImport 下已存 requests ≥ 传入 requests 时保留已存值(仅导入路径生效,organic 保存仍是权威且零额外开销)。回归:tests/envelope-import-block-lineage.test.ts(细块保留 + stats 只升不降 + 首开导入采纳 + organic 权威语义不变 + importRecord wiring 五用例)、tests/storage-ingest-watermark.test.ts。 - **dsh 运行时注册的 preset 继承压缩完全禁止(#7190,关 #7178 / #7062 / #7169)**:dsh 的 preset 注册表 `AgentPresetRegistry.register(definition)` 是**启动后才出生**的 preset 行唯一的出生点,它把嵌套行直接挂进内存 PresetTree(其 `commit()` 为空实现),从不经过 loader 的 `internal/config` 变换——bili 的 #4147 结构性 disable 重写(`bili-compaction-gate`,#3354/#3662)永远碰不到这些行。于是运行时注册 preset 的压缩引擎是全副武装的(每步压力触发 + 模型切换前置压缩 + 溢出恢复三条触发器都在 dsh 的 surface 计量上点火),而 bili 侧 wire guard 拒绝每一次压缩调用:#7062 的逐步 422 风暴、#7178 的模型切换死局(`MODEL_SWITCH_COMPACTION_FAILED` 永不满足前提)、#7169 的 landed-checkpoint 触发 rebase 抹掉 bili 自己的折叠块再 DIGEST DRIFT 冻结信封,全是这台引擎在开火。修法与 boot-time 行同一判决、同一机制:gate 入口(always-on,bili-native 关着也活着)现在包一层 `agentPresets.register`——定义里的 compaction-basic / command-compact 行在挂入前就地改写为 `disabled: true`。判决按每次注册调用检查(bili-native 没宣告持线 = 定义原样通过,dsh 原生压缩照常);无 compaction 行 / 畸形定义按身份原样通过(注册表自己的校验看到字节级相同的输入);Symbol-for 标记保证重 apply 不双重包裹;宿主没有该注册表 = 无运行时 preset 可注册,静默不包裹。遥测新增 `runtime` / `runtimeWrapped` 维度,#6770 覆盖一行据此报告「register() 已覆盖——尚无运行时注册」或「N 个运行时注册被改写」,wrap 未知时不报假缺口。**非模型可见文本改动**(只动 dsh 侧 web banner 的覆盖一行与日志)。dsh 源码依据(deepseek-ai/deepseek-harness @ 2e39e70,与现场 0.2.2-rc.3 逐点核验一致):`packages/preset/agent-preset-registry/src/index.ts:96`(register 立即激活、重复 id 抛错)、`mount.ts:22-23`(PresetTree 空 commit)、per-fiber `internal/config` waterfall(`vendor/cordis/src/fiber.ts:691-700`)——preset 作用域 fiber 在自己的上下文里解析配置,根上注册的 global hook 不触发。回归:tests/issue7190-dsh-runtime-preset-coverage.test.ts(新增 10 条行驱动正反用例:改写/身份保持/判决关/重包裹/`this` 转发/无注册表降级/get 回退/遥测去重 + 覆盖文案四形态)、tests/issue3354-dsh-compaction-disable.test.ts、tests/issue6770-compaction-coverage-text.test.ts、tests/issue4147-dsh-overlay-structural.test.ts 全绿。 diff --git a/docs/architecture/update.md b/docs/architecture/update.md index d72efc3528..aba1c271bb 100644 --- a/docs/architecture/update.md +++ b/docs/architecture/update.md @@ -23,6 +23,8 @@ Get every copy of bili on a machine to the newest version without bricking anyth **Ledger convergence.** The desktop dsh profile is the one copy bili owns outright; its self-heal swaps `node_modules/billion-context` directly, so `convergeDshLedger` (`src/dsh-ledger.ts`) also moves the profile manifest's declared spec AND the lockfile's recorded resolution in the same operation (#3496) — a writer that moves only the copy manufactures the drift #3224 detects. Pre-existing drift converges on explicit demand only: `bili doctor --fix`. `plugin-install.ts` only drives the host's own channel (`dsh plugin add`), never installs a second copy beside it (#966). +**Maturity gate.** Before the desktop self-heal installs a new version, `release-maturity.ts` checks the host's pnpm `minimumReleaseAge` policy — resolved per key in pnpm's own precedence (env `pnpm_config_*` > project `pnpm-workspace.yaml`/`.npmrc` > user `~/.npmrc` > global config), with `minimumReleaseAgeExclude` matched by the pnpm version-policy grammar. An immature release is deferred to the next cycle (no strike); if the publish time or the policy cannot be established, the refresh fails closed (strike, copy/manifest/lockfile untouched) — the updater never bypasses the host's maturity policy (#7139). + **Advisory & restart.** `advisory.ts` watches for critical defects and files an advisory (#1481 — a human acts, the machine never rolls back on its own); `update-notes.ts` carries tiered release notes (severe-only by default, #1870); `restart.ts` offers opt-in self-restart once newer code is on disk (#811). ## Hard rules @@ -31,7 +33,7 @@ Human-review tier (§7.4) — no auto-merge; changing `update.ts` requires a NO- ## Key entries -`src/update.ts` (updater — load-bearing) · `src/advisory.ts` · `src/restart.ts` · `src/plugin-install.ts` · `src/dsh-ledger.ts` · `src/dsh-channel.ts` · `src/pi-channel.ts` · `src/launcher.ts` +`src/update.ts` (updater — load-bearing) · `src/release-maturity.ts` (pnpm maturity gate for the desktop lane, #7139) · `src/advisory.ts` · `src/restart.ts` · `src/plugin-install.ts` · `src/dsh-ledger.ts` · `src/dsh-channel.ts` · `src/pi-channel.ts` · `src/launcher.ts` ## Related diff --git a/docs/architecture/update.zh-CN.md b/docs/architecture/update.zh-CN.md index cee0272a98..e6fb99cbbe 100644 --- a/docs/architecture/update.zh-CN.md +++ b/docs/architecture/update.zh-CN.md @@ -23,6 +23,8 @@ **三账收敛。** 桌面 dsh profile 是 bili 唯一完全自有的拷贝;它的自愈直接换 `node_modules/billion-context`,所以 `convergeDshLedger`(`src/dsh-ledger.ts`)同一操作里还要挪 profile manifest 的声明 spec 与 lockfile 的记录(#3496)——只挪拷贝的写者会亲手造出 #3224 检测的那种漂移。既有漂移只在显式要求时收敛:`bili doctor --fix`。`plugin-install.ts` 只驱动宿主自己的通道(`dsh plugin add`),绝不在旁边装第二份(#966)。 +**成熟度门。** 桌面自愈安装新版本前,`release-maturity.ts` 先查宿主的 pnpm `minimumReleaseAge` 策略——按 pnpm 自身优先级逐键解析(env `pnpm_config_*` > 项目 `pnpm-workspace.yaml`/`.npmrc` > 用户 `~/.npmrc` > 全局 config),`minimumReleaseAgeExclude` 按 pnpm version-policy 语法匹配。未成熟版本推迟到下轮(不记 strike);发布时间或策略无法确立时 fail closed(记 strike、副本/manifest/lockfile 一律不动)——更新器绝不绕过宿主的成熟度策略(#7139)。 + **通告与重启。** `advisory.ts` 观察严重缺陷并开 advisory(#1481——人来处置,机器永不自行回滚);`update-notes.ts` 承载分级 release notes(默认只 severe,#1870);`restart.ts` 在新代码落盘后提供可选自重启(#811)。 ## 硬规则 @@ -31,7 +33,7 @@ ## 关键入口 -`src/update.ts`(更新器——承重件) · `src/advisory.ts` · `src/restart.ts` · `src/plugin-install.ts` · `src/dsh-ledger.ts` · `src/dsh-channel.ts` · `src/pi-channel.ts` · `src/launcher.ts` +`src/update.ts`(更新器——承重件) · `src/release-maturity.ts`(桌面车道 pnpm 成熟度门,#7139) · `src/advisory.ts` · `src/restart.ts` · `src/plugin-install.ts` · `src/dsh-ledger.ts` · `src/dsh-channel.ts` · `src/pi-channel.ts` · `src/launcher.ts` ## 相关 diff --git a/reference/architecture.md b/reference/architecture.md index 6fb6f6adef..78decd5e9c 100644 --- a/reference/architecture.md +++ b/reference/architecture.md @@ -171,6 +171,7 @@ billion-context/ │ ├── external-summary-surface.ts # Tool/schema and client-facing summary contract │ ├── external-summary-marker.ts # Zero-import marker contract for externally summarized blocks │ ├── update.ts # Self-updater (load-bearing — no-op release protocol) + install lanes +│ ├── release-maturity.ts # pnpm minimumReleaseAge gate for the dsh desktop auto-refresh: host policy resolution (env > project > user > global), exclude matching, registry publish-time fetch + cache, fail-closed verdicts (#7139) │ ├── advisory.ts # Critical-defect advisory watcher (#1481) │ ├── plugin-advisory.ts # dsh cost-advisory detection + TTL cache (#2462) │ ├── host-plugin-notice.ts # Known host-side metering plugin notice + dismiss state (#2861) diff --git a/src/release-maturity.ts b/src/release-maturity.ts new file mode 100644 index 0000000000..fa879d3fb3 --- /dev/null +++ b/src/release-maturity.ts @@ -0,0 +1,433 @@ +/** + * #7139: pnpm minimumReleaseAge gate for the dsh desktop in-place lane. + * + * The dsh desktop profile is a pnpm-managed project. Its host can pin a + * package-maturity policy — `minimumReleaseAge` (minutes) plus + * `minimumReleaseAgeExclude` — a supply-chain rule that refuses to resolve + * versions published less than N minutes ago. bili's in-place desktop refresh + * (refreshDshDesktopCopy) installs the selected release's tarball directly and + * converges the profile ledger, bypassing pnpm's resolver — so the policy has + * to be enforced HERE, before the swap. Without it, a fresh release lands in + * the profile before its maturity window elapses: the manifest then requests + * a version the lockfile cannot resolve, and every subsequent pnpm operation + * on the profile (even for unrelated packages) fails until the release + * matures. + * + * Policy resolution mirrors pnpm's own precedence — per key, the FIRST source + * that defines it wins: + * env pnpm_config_* → env npm_config_* → project pnpm-workspace.yaml → + * project .npmrc → user ~/.npmrc → global $XDG_CONFIG_HOME/pnpm/config.yaml + * → global $PREFIX/etc/npmrc. + * Excludes travel with the source that defined them. A null/empty value in a + * file (e.g. `minimumReleaseAge:` with nothing after) is "not provided" and + * does not shadow a positive value in a lower-precedence source; only an + * explicit number (0 = policy off) wins. + * + * Publish time comes from the registry packument's `time` map — the same + * field pnpm's resolver reads (the tarball's Last-Modified is NOT reliable: + * measured 12 s off the registry time). The full packument is large (tens of + * MB for this package), so it is fetched at most once per target version: the + * extracted time map is cached under cacheDir() as .registry-time-.json, + * and a hit for the target version means zero registry traffic on later + * checks (the time map is append-only — entries never change). An in-process + * memo bounds traffic when the cache dir is unwritable (the same degradation + * the backoff state already accepts). + * + * Fail closed: whenever the policy or the publish time cannot be established + * (unreadable/malformed config, registry without a time field, fetch failure), + * the caller defers the refresh for this cycle — the host policy is never + * bypassed, and no exclusion is silently added. + */ +import { readFileSync } from "node:fs"; +import { mkdir, readFile, rename, writeFile } from "node:fs/promises"; +import os from "node:os"; +import path from "node:path"; +import { cacheDir } from "./paths.js"; +import { proxyDispatcher } from "./upstream-proxy.js"; +import type { FetchOptions } from "./fetch-util.js"; + +const PACKUMENT_FETCH_TIMEOUT_MS = 30_000; + +export type MaturityVerdict = + | { kind: "no-policy" } + | { kind: "excluded"; spec: string; source: string } + | { kind: "mature"; publishedAt: string; minAgeMinutes: number } + | { kind: "immature"; publishedAt: string; minAgeMinutes: number; source: string } + | { kind: "undatable"; reason: string }; + +interface ResolvedPolicy { + /** undefined = no minimumReleaseAge defined in any source */ + minAgeMinutes: number | undefined; + minAgeSource: string; + excludeSpecs: string[]; + excludeSource: string; +} + +type PolicyResolution = { ok: true; policy: ResolvedPolicy } | { ok: false; reason: string }; + +/** Resolve the profile's effective pnpm maturity policy (see module header + * for the source precedence). `ok: false` = the policy cannot be established + * — callers fail closed. */ +export function resolveMaturityPolicy(projectDir: string, env: NodeJS.ProcessEnv): PolicyResolution { + interface Source { label: string; minAgeRaw?: string; excludeRaw?: string } + const sources: Source[] = []; + for (const varName of ["pnpm_config_minimum_release_age", "npm_config_minimum_release_age"]) { + const v = env[varName]; + if (v !== undefined && v.trim() !== "") sources.push({ label: `env ${varName}`, minAgeRaw: v.trim() }); + } + for (const varName of ["pnpm_config_minimum_release_age_exclude", "npm_config_minimum_release_age_exclude"]) { + const v = env[varName]; + if (v !== undefined && v.trim() !== "") sources.push({ label: `env ${varName}`, excludeRaw: v.trim() }); + } + const home = env.HOME?.trim() ? env.HOME : os.homedir(); + const fileSources: Array<{ label: string; file: string; kind: "yaml" | "npmrc" }> = [ + { label: "project pnpm-workspace.yaml", file: path.join(projectDir, "pnpm-workspace.yaml"), kind: "yaml" }, + { label: "project .npmrc", file: path.join(projectDir, ".npmrc"), kind: "npmrc" }, + { label: "user ~/.npmrc", file: path.join(home, ".npmrc"), kind: "npmrc" }, + { label: "global pnpm config.yaml", file: path.join(env.XDG_CONFIG_HOME?.trim() || path.join(home, ".config"), "pnpm", "config.yaml"), kind: "yaml" }, + ]; + if (env.PREFIX?.trim()) fileSources.push({ label: "global $PREFIX/etc/npmrc", file: path.join(env.PREFIX.trim(), "etc", "npmrc"), kind: "npmrc" }); + for (const rs of fileSources) { + let text: string; + try { + text = readFileSync(rs.file, "utf-8"); + } catch (err) { + const code = (err as NodeJS.ErrnoException).code; + if (code === "ENOENT" || code === "ENOTDIR") continue; + return { ok: false, reason: `cannot read ${rs.label}: ${err instanceof Error ? err.message : String(err)}` }; + } + const parsed = rs.kind === "yaml" ? parseYamlPolicyKeys(text) : parseNpmrcPolicyKeys(text); + if (parsed.error) return { ok: false, reason: `${rs.label}: ${parsed.error}` }; + if (parsed.minAgeRaw !== undefined || parsed.excludeRaw !== undefined) { + sources.push({ label: rs.label, minAgeRaw: parsed.minAgeRaw, excludeRaw: parsed.excludeRaw }); + } + } + let minAgeMinutes: number | undefined; + let minAgeSource = ""; + let excludeSpecs: string[] | undefined; + let excludeSource = ""; + for (const s of sources) { + // a null/empty file value is "not provided" (pnpm semantics) — it does + // NOT shadow a positive value in a lower-precedence source; only an + // explicit number (including 0 = policy off) wins. + if (minAgeMinutes === undefined && s.minAgeRaw !== undefined && s.minAgeRaw !== "") { + const parsed = parseMinAgeMinutes(s.minAgeRaw); + if (!parsed.ok) return { ok: false, reason: `${s.label}: ${parsed.reason}` }; + minAgeMinutes = parsed.minutes; + minAgeSource = s.label; + } + if (excludeSpecs === undefined && s.excludeRaw !== undefined) { + excludeSpecs = parseExcludeList(s.excludeRaw); + excludeSource = s.label; + } + } + return { ok: true, policy: { minAgeMinutes, minAgeSource, excludeSpecs: excludeSpecs ?? [], excludeSource } }; +} + +/** pnpm's exclude rule grammar (version-policy crate): `[@]`. + * A bare name (or a wildcard name WITHOUT a version part) excludes every + * version of a matching package; `name@1.0.0 || 2.0.0` excludes exactly the + * listed versions. A wildcard name combined with a version part is a pnpm + * config error — surfaced here as a parse error so the caller fails closed. */ +export function matchMaturityExclude( + specs: string[], + packageName: string, + version: string, +): { matched: true; spec: string } | { matched: false } | { error: string } { + for (const spec of specs) { + const rule = parseExcludeRule(spec); + if ("error" in rule) return rule; + if (!nameMatches(rule.name, packageName)) continue; + if (rule.versions.length === 0) return { matched: true, spec }; + if (rule.versions.includes(version)) return { matched: true, spec }; + } + return { matched: false }; +} + +/** The gate. `no-policy` / `excluded` / `mature` → the refresh may proceed; + * `immature` → defer (not a failure — the version matures on its own); + * `undatable` → fail closed (defer AND report — the caller records a strike). */ +export async function checkReleaseMaturity(args: { + projectDir: string; + packageName: string; + version: string; + registryBase: string; + env: NodeJS.ProcessEnv; + resolveProxy?: (url: string) => string | undefined; +}): Promise { + const resolution = resolveMaturityPolicy(args.projectDir, args.env); + if (!resolution.ok) return { kind: "undatable", reason: resolution.reason }; + const { minAgeMinutes, minAgeSource, excludeSpecs, excludeSource } = resolution.policy; + if (minAgeMinutes === undefined || minAgeMinutes <= 0) return { kind: "no-policy" }; + const excluded = matchMaturityExclude(excludeSpecs, args.packageName, args.version); + if ("error" in excluded) return { kind: "undatable", reason: `invalid minimumReleaseAgeExclude rule: ${excluded.error}` }; + if (excluded.matched) return { kind: "excluded", spec: excluded.spec, source: excludeSource }; + const t = await fetchPublishTime({ packageName: args.packageName, version: args.version, registryBase: args.registryBase, resolveProxy: args.resolveProxy }); + if (!t.publishedAt) return { kind: "undatable", reason: t.error ?? `no publish time for ${args.packageName}@${args.version}` }; + const publishedMs = Date.parse(t.publishedAt); + if (!Number.isFinite(publishedMs)) return { kind: "undatable", reason: `unparseable publish time "${t.publishedAt}" for ${args.packageName}@${args.version}` }; + if (Date.now() - publishedMs >= minAgeMinutes * 60_000) { + return { kind: "mature", publishedAt: t.publishedAt, minAgeMinutes }; + } + return { kind: "immature", publishedAt: t.publishedAt, minAgeMinutes, source: minAgeSource }; +} + +function parseMinAgeMinutes(raw: string): { ok: true; minutes: number } | { ok: false; reason: string } { + if (!/^\d+$/.test(raw)) return { ok: false, reason: `minimumReleaseAge value "${raw}" is not a non-negative integer number of minutes` }; + const minutes = Number(raw); + if (!Number.isSafeInteger(minutes)) return { ok: false, reason: `minimumReleaseAge value "${raw}" overflows` }; + return { ok: true, minutes }; +} + +function parseExcludeList(raw: string): string[] { + return raw.split(",").map((s) => s.trim()).filter((s) => s !== ""); +} + +function parseExcludeRule(spec: string): { name: string; versions: string[] } | { error: string } { + const s = spec.trim(); + if (!s) return { error: "empty minimumReleaseAgeExclude entry" }; + const at = s.startsWith("@") ? s.indexOf("@", 1) : s.indexOf("@"); + const name = at === -1 ? s : s.slice(0, at); + const rest = at === -1 ? "" : s.slice(at + 1); + if (!name) return { error: `exclude rule "${spec}" has no package name` }; + if (rest === "") return { name, versions: [] }; + if (name.includes("*") || name.includes("?")) return { error: `wildcard name combined with a version part: "${spec}"` }; + const versions = rest.split("||").map((v) => v.trim()); + if (versions.some((v) => v === "")) return { error: `malformed version union in exclude rule "${spec}"` }; + return { name, versions }; +} + +function nameMatches(pattern: string, name: string): boolean { + if (!/[*?]/.test(pattern)) return pattern === name; + const rx = "^" + pattern.split("").map((c) => (c === "*" ? "[^]*" : c === "?" ? "." : c.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))).join("") + "$"; + return new RegExp(rx).test(name); +} + +// ——— config file readers (minimal, line-based; same hand-rolled precedent as +// ——— the pnpm-lock parsing in dsh-ledger.ts — zero runtime deps) ——— + +function parseNpmrcPolicyKeys(text: string): { minAgeRaw?: string; excludeRaw?: string; error?: string } { + const out: { minAgeRaw?: string; excludeRaw?: string; error?: string } = {}; + for (const rawLine of text.split(/\r?\n/)) { + const line = rawLine.trim(); + if (!line || line.startsWith("#") || line.startsWith(";")) continue; + const eq = line.indexOf("="); + if (eq <= 0) continue; + const key = line.slice(0, eq).trim().toLowerCase(); + const val = line.slice(eq + 1).trim(); + if (key === "minimumreleaseage") { + if (out.minAgeRaw !== undefined) { out.error = "duplicate minimumReleaseAge entry"; return out; } + out.minAgeRaw = stripQuotes(val); + } else if (key === "minimumreleaseageexclude") { + if (out.excludeRaw !== undefined) { out.error = "duplicate minimumReleaseAgeExclude entry"; return out; } + out.excludeRaw = stripQuotes(val); + } + } + return out; +} + +/** Top-level `minimumReleaseAge` / `minimumReleaseAgeExclude` only — the + * policy keys live at column 0 in pnpm-workspace.yaml / config.yaml. Other + * keys and their indented bodies are skipped; malformed content in OTHER + * keys cannot affect these two, but malformed content in OUR keys fails + * closed (returns an error). */ +function parseYamlPolicyKeys(text: string): { minAgeRaw?: string; excludeRaw?: string; error?: string } { + const out: { minAgeRaw?: string; excludeRaw?: string; error?: string } = {}; + const lines = text.split(/\r?\n/); + let i = 0; + while (i < lines.length) { + const line = lines[i]; + const trimmed = line.trim(); + i++; + if (!trimmed || trimmed.startsWith("#")) continue; + if (/^\s/.test(line)) continue; // indented content under a key we don't parse + const m = /^([A-Za-z_][A-Za-z0-9_-]*)\s*:(.*)$/.exec(line); + if (!m) continue; // not a `key: value` line (document marker, top-level list item, ...) + const key = m[1]; + const rest = m[2] ?? ""; + if (key === "minimumReleaseAge") { + if (out.minAgeRaw !== undefined) { out.error = "duplicate minimumReleaseAge key"; return out; } + const v = stripInlineComment(rest).trim(); + if (v === "" || v === "~" || v.toLowerCase() === "null") { out.minAgeRaw = ""; continue; } // null value = "not set" + out.minAgeRaw = stripQuotes(v); + } else if (key === "minimumReleaseAgeExclude") { + if (out.excludeRaw !== undefined) { out.error = "duplicate minimumReleaseAgeExclude key"; return out; } + const v = stripInlineComment(rest).trim(); + if (v === "" || v === "~" || v.toLowerCase() === "null") { + // block list follows (or the value was null = no excludes) + const items: string[] = []; + let sawItem = false; + while (i < lines.length) { + const l = lines[i]; + const lt = l.trim(); + if (!lt || lt.startsWith("#")) { i++; continue; } + if (!/^\s/.test(l)) break; // next top-level key — the value was null + const item = /^\s*-\s*(.*)$/.exec(l); + if (!item) { out.error = `expected a list of rules under minimumReleaseAgeExclude, got: ${lt}`; return out; } + i++; + sawItem = true; + items.push(stripQuotes(stripInlineComment(item[1]).trim())); + } + out.excludeRaw = sawItem ? items.join(",") : ""; + } else if (v.startsWith("[")) { + if (!v.endsWith("]")) { out.error = "multi-line flow list for minimumReleaseAgeExclude is not supported"; return out; } + const items = v.slice(1, -1).split(",").map((s) => stripQuotes(stripInlineComment(s).trim())).filter((s) => s !== ""); + out.excludeRaw = items.join(","); + } else { + out.error = `minimumReleaseAgeExclude must be a list of rules, got scalar "${v}"`; + return out; + } + } + } + return out; +} + +function stripInlineComment(s: string): string { + let inSingle = false; + let inDouble = false; + for (let i = 0; i < s.length; i++) { + const c = s[i]; + if (inSingle) { if (c === "'") inSingle = false; continue; } + if (inDouble) { if (c === '"') inDouble = false; continue; } + if (c === "'") { inSingle = true; continue; } + if (c === '"') { inDouble = true; continue; } + if (c === "#" && (i === 0 || s[i - 1] === " " || s[i - 1] === "\t")) return s.slice(0, i); + } + return s; +} + +function stripQuotes(s: string): string { + if (s.length >= 2) { + const a = s[0]; + const b = s[s.length - 1]; + if ((a === '"' && b === '"') || (a === "'" && b === "'")) return s.slice(1, -1); + } + return s; +} + +// ——— publish time: registry packument `time` map, cached ——— + +interface TimeCacheDoc { fetchedAt: string; time: Record; } + +// In-process memo: bounds registry traffic when the cache dir is unwritable. +const timeMemo = new Map(); + +function timeCachePath(packageName: string): string { + return path.join(cacheDir(), `.registry-time-${packageName.replace(/[^a-zA-Z0-9@._-]/g, "_")}.json`); +} + +async function readTimeCache(packageName: string): Promise | undefined> { + try { + const raw = await readFile(timeCachePath(packageName), "utf-8"); + const doc = JSON.parse(raw) as TimeCacheDoc; + if (!doc || typeof doc !== "object" || !doc.time || typeof doc.time !== "object" || Array.isArray(doc.time)) return undefined; + for (const v of Object.values(doc.time)) if (typeof v !== "string") return undefined; + return doc.time; + } catch { + return undefined; + } +} + +async function writeTimeCache(packageName: string, time: Record): Promise { + const p = timeCachePath(packageName); + await mkdir(path.dirname(p), { recursive: true }); + const doc: TimeCacheDoc = { fetchedAt: new Date().toISOString(), time }; + const tmp = `${p}.${process.pid}.tmp`; + await writeFile(tmp, JSON.stringify(doc)); + await rename(tmp, p); +} + +/** Locate the packument `time` map without parsing the whole (tens-of-MB) + * document: find each `"time"` key, brace-scan its object (string-aware), + * and accept the first one that carries the target version with all-string + * values. npmjs places the top-level map after `versions` (near the end); + * scanning occurrences keeps this correct for any key order. */ +function extractTimeObject(text: string, version: string): Record | undefined { + let from = 0; + while (from < text.length) { + const key = text.indexOf('"time"', from); + if (key === -1) return undefined; + let i = key + 6; // '"time"'.length + while (i < text.length && (text[i] === " " || text[i] === "\t")) i++; + if (text[i] === ":") i++; + while (i < text.length && (text[i] === " " || text[i] === "\t")) i++; + if (text[i] !== "{") { from = key + 6; continue; } + const braceStart = i; + let depth = 0; + let inStr = false; + let esc = false; + let end = -1; + for (; i < text.length; i++) { + const c = text[i]; + if (inStr) { + if (esc) esc = false; + else if (c === "\\") esc = true; + else if (c === '"') inStr = false; + continue; + } + if (c === '"') inStr = true; + else if (c === "{") depth++; + else if (c === "}") { depth--; if (depth === 0) { end = i; break; } } + } + if (end === -1) return undefined; // truncated document + try { + const obj: unknown = JSON.parse(text.slice(braceStart, end + 1)); + if (obj && typeof obj === "object" && !Array.isArray(obj)) { + const rec = obj as Record; + if (typeof rec[version] === "string" && Object.values(rec).every((v) => typeof v === "string")) { + return rec as Record; + } + } + } catch { + // not valid JSON at this occurrence — keep scanning + } + from = key + 5; + } + return undefined; +} + +async function fetchPublishTime(args: { + packageName: string; + version: string; + registryBase: string; + resolveProxy?: (url: string) => string | undefined; +}): Promise<{ publishedAt?: string; error?: string }> { + const memoKey = `${args.packageName}@${args.version}`; + const memoHit = timeMemo.get(memoKey); + if (memoHit) return { publishedAt: memoHit }; + const cached = await readTimeCache(args.packageName); + const cachedAt = cached?.[args.version]; + if (cachedAt) { + timeMemo.set(memoKey, cachedAt); + return { publishedAt: cachedAt }; + } + const url = `${args.registryBase}/${encodeURIComponent(args.packageName)}`; + const dispatcher = proxyDispatcher(args.resolveProxy?.(url), PACKUMENT_FETCH_TIMEOUT_MS); + const init: FetchOptions = { + signal: AbortSignal.timeout(PACKUMENT_FETCH_TIMEOUT_MS), + headers: { Accept: "application/json" }, + ...(dispatcher ? { dispatcher } : {}), + }; + let res: Response; + try { + res = await fetch(url, init as RequestInit); + } catch (err) { + return { error: `registry fetch failed: ${err instanceof Error ? err.message : String(err)}` }; + } + if (!res.ok) { + await res.body?.cancel().catch(() => undefined); + return { error: `registry returned HTTP ${res.status} for ${args.packageName}` }; + } + let text: string; + try { + text = await res.text(); + } catch (err) { + return { error: `registry response read failed: ${err instanceof Error ? err.message : String(err)}` }; + } + const time = extractTimeObject(text, args.version); + if (!time) return { error: `registry packument for ${args.packageName} carries no publish time for ${args.version}` }; + timeMemo.set(memoKey, time[args.version]); + // best-effort: an unwritable cache dir degrades to the per-process memo + await writeTimeCache(args.packageName, time).catch(() => undefined); + return { publishedAt: time[args.version] }; +} diff --git a/src/update.ts b/src/update.ts index 6393774f3c..b2b46e320d 100644 --- a/src/update.ts +++ b/src/update.ts @@ -33,6 +33,7 @@ import { cacheDir } from "./paths.js"; import { log as loggerLog, type Logger } from "./logger.js"; import { refreshDshProfileBundles, isDshProfileCopy, dshProfileDirs, dshProfileDependsOnBili, dshProfileDepSpec, isRegistryDepSpec, DSH_PACKAGE, DSH_DESKTOP_PROFILE } from "./dsh-channel.js"; import { convergeDshLedger } from "./dsh-ledger.js"; +import { checkReleaseMaturity } from "./release-maturity.js"; import { isPiNpmCopy, piNpmEntrySpec, runPiAsync, PI_NPM_SPEC } from "./pi-channel.js"; import { resolveDshHome, resolveKimiHome, resolveOmpHome, resolvePiHome } from "./client-config.js"; import { proxyDispatcher } from "./upstream-proxy.js"; @@ -1174,6 +1175,7 @@ export async function fetchRegistryVersion(opts: Pick = {}): NodeJS.ProcessEnv { + const home = path.join(root, "home"); + mkdirSync(path.join(home, ".config"), { recursive: true }); + return { ...process.env, HOME: home, XDG_CONFIG_HOME: path.join(home, ".config"), PREFIX: path.join(root, "prefix"), ...overrides }; +} + +function isoHoursAgo(hours: number): string { + return new Date(Date.now() - hours * 3_600_000).toISOString(); +} + +/** Stub the registry: serves ONLY the packument (the gate's only fetch). + * `time` = the packument's time map (omit via opts to test the no-time case). */ +function stubPackumentFetch(time: Record | undefined, opts: { failNetwork?: boolean; status?: number } = {}): { packumentCalls: number } { + const state = { packumentCalls: 0 }; + globalThis.fetch = ((url: unknown) => { + const u = String(url); + if (u === `${REGISTRY}/billion-context`) { + state.packumentCalls++; + if (opts.failNetwork) return Promise.reject(new Error("network down")); + if (opts.status) return Promise.resolve(new Response("nope", { status: opts.status })); + const doc: Record = { name: "billion-context", versions: {} }; + if (time !== undefined) doc.time = time; + return Promise.resolve(new Response(JSON.stringify(doc), { headers: { "content-type": "application/json" } })); + } + return Promise.reject(new Error(`unexpected fetch in test: ${u}`)); + }) as unknown as typeof fetch; + return state; +} + +function projectWith(root: string, yaml?: string, npmrc?: string, name = "project"): string { + const projectDir = path.join(root, name); + mkdirSync(projectDir, { recursive: true }); + if (yaml !== undefined) writeFileSync(path.join(projectDir, "pnpm-workspace.yaml"), yaml); + if (npmrc !== undefined) writeFileSync(path.join(projectDir, ".npmrc"), npmrc); + return projectDir; +} + +// —— resolveMaturityPolicy: precedence & parsing ——— + +test("policy: no source anywhere → no minimumReleaseAge", () => { + const root = mkdtempSync(path.join(tmpdir(), "bc-maturity-nopolicy-")); + try { + const projectDir = projectWith(root); + const r = resolveMaturityPolicy(projectDir, makeEnv(root)); + assert.equal(r.ok, true); + if (!r.ok) return; + assert.equal(r.policy.minAgeMinutes, undefined); + assert.deepEqual(r.policy.excludeSpecs, []); + } finally { + rmrf(root); + } +}); + +test("policy: env pnpm_config wins over project yaml; npm_config is the fallback", () => { + const root = mkdtempSync(path.join(tmpdir(), "bc-maturity-env-")); + try { + const projectDir = projectWith(root, "minimumReleaseAge: 60\n"); + const env = makeEnv(root, { pnpm_config_minimum_release_age: "1440", npm_config_minimum_release_age: "30" }); + const r = resolveMaturityPolicy(projectDir, env); + assert.equal(r.ok, true); + if (!r.ok) return; + assert.equal(r.policy.minAgeMinutes, 1440); + assert.equal(r.policy.minAgeSource, "env pnpm_config_minimum_release_age"); + const r2 = resolveMaturityPolicy(projectDir, makeEnv(root, { npm_config_minimum_release_age: "30" })); + assert.equal(r2.ok, true); + if (!r2.ok) return; + assert.equal(r2.policy.minAgeMinutes, 30); + assert.equal(r2.policy.minAgeSource, "env npm_config_minimum_release_age"); + } finally { + rmrf(root); + } +}); + +test("policy: project yaml beats project .npmrc beats user .npmrc beats global config.yaml beats $PREFIX/etc/npmrc", () => { + const root = mkdtempSync(path.join(tmpdir(), "bc-maturity-precedence-")); + try { + const env = makeEnv(root); + const userNpmrc = path.join(env.HOME as string, ".npmrc"); + writeFileSync(userNpmrc, "minimumReleaseAge=50\n"); + const globalYaml = path.join(env.XDG_CONFIG_HOME as string, "pnpm"); + mkdirSync(globalYaml, { recursive: true }); + writeFileSync(path.join(globalYaml, "config.yaml"), "minimumReleaseAge: 40\n"); + mkdirSync(path.join(env.PREFIX as string, "etc"), { recursive: true }); + writeFileSync(path.join(env.PREFIX as string, "etc", "npmrc"), "minimumReleaseAge=30\n"); + + let projectDir = projectWith(root, "minimumReleaseAge: 100\n", "minimumReleaseAge=90\n", "p-yaml"); + let r = resolveMaturityPolicy(projectDir, env); + assert.equal(r.ok, true); + if (r.ok) assert.equal(r.policy.minAgeSource, "project pnpm-workspace.yaml"); + + // a null project value is "not provided" — it must not shadow the user's positive value + projectDir = projectWith(root, "minimumReleaseAge:\n", undefined, "p-null"); + r = resolveMaturityPolicy(projectDir, env); + assert.equal(r.ok, true); + if (r.ok) { + assert.equal(r.policy.minAgeMinutes, 50); + assert.equal(r.policy.minAgeSource, "user ~/.npmrc"); + } + + projectDir = projectWith(root, undefined, "minimumReleaseAge=90\n", "p-npmrc"); + r = resolveMaturityPolicy(projectDir, env); + assert.equal(r.ok, true); + if (r.ok) assert.equal(r.policy.minAgeSource, "project .npmrc"); + + projectDir = projectWith(root, undefined, undefined, "p-none"); + r = resolveMaturityPolicy(projectDir, env); + assert.equal(r.ok, true); + if (r.ok) assert.equal(r.policy.minAgeSource, "user ~/.npmrc"); + + // no user npmrc → global config.yaml + writeFileSync(userNpmrc, ""); + r = resolveMaturityPolicy(projectDir, env); + assert.equal(r.ok, true); + if (r.ok) assert.equal(r.policy.minAgeSource, "global pnpm config.yaml"); + + // no global yaml → $PREFIX/etc/npmrc + writeFileSync(path.join(globalYaml, "config.yaml"), ""); + r = resolveMaturityPolicy(projectDir, env); + assert.equal(r.ok, true); + if (r.ok) assert.equal(r.policy.minAgeSource, "global $PREFIX/etc/npmrc"); + } finally { + rmrf(root); + } +}); + +test("policy: excludes travel with the source that defines them (env minAge + project excludes)", () => { + const root = mkdtempSync(path.join(tmpdir(), "bc-maturity-exclsrc-")); + try { + const projectDir = projectWith(root, "minimumReleaseAge: 60\nminimumReleaseAgeExclude:\n - billion-context\n"); + const env = makeEnv(root, { pnpm_config_minimum_release_age: "1440" }); + const r = resolveMaturityPolicy(projectDir, env); + assert.equal(r.ok, true); + if (!r.ok) return; + assert.equal(r.policy.minAgeMinutes, 1440); + assert.deepEqual(r.policy.excludeSpecs, ["billion-context"]); + assert.equal(r.policy.excludeSource, "project pnpm-workspace.yaml"); + } finally { + rmrf(root); + } +}); + +test("policy: yaml forms — flow list, null value, inline comments, quoted values", () => { + const root = mkdtempSync(path.join(tmpdir(), "bc-maturity-yaml-")); + try { + let projectDir = projectWith(root, 'minimumReleaseAge: "1440" # twenty-four hours\nminimumReleaseAgeExclude: [billion-context, other@1.0.0]\n'); + let r = resolveMaturityPolicy(projectDir, makeEnv(root)); + assert.equal(r.ok, true); + if (r.ok) { + assert.equal(r.policy.minAgeMinutes, 1440); + assert.deepEqual(r.policy.excludeSpecs, ["billion-context", "other@1.0.0"]); + } + projectDir = projectWith(root, "minimumReleaseAge:\nminimumReleaseAgeExclude:\n - 'billion-context@1.0.0'\n"); + r = resolveMaturityPolicy(projectDir, makeEnv(root)); + assert.equal(r.ok, true); + if (r.ok) { + assert.equal(r.policy.minAgeMinutes, undefined); // null = "not provided" — no source defines it + assert.deepEqual(r.policy.excludeSpecs, ["billion-context@1.0.0"]); + } + } finally { + rmrf(root); + } +}); + +test("policy: npmrc forms — comments, semicolons, quotes", () => { + const root = mkdtempSync(path.join(tmpdir(), "bc-maturity-npmrc-")); + try { + const projectDir = projectWith(root, undefined, "# comment\n; another\nregistry=https://x\nminimumReleaseAge = 1440\nminimumReleaseAgeExclude=\"a@1.0.0, b\"\n"); + const r = resolveMaturityPolicy(projectDir, makeEnv(root)); + assert.equal(r.ok, true); + if (!r.ok) return; + assert.equal(r.policy.minAgeMinutes, 1440); + assert.deepEqual(r.policy.excludeSpecs, ["a@1.0.0", "b"]); + } finally { + rmrf(root); + } +}); + +test("policy: malformed values fail closed (no policy established)", () => { + const root = mkdtempSync(path.join(tmpdir(), "bc-maturity-malformed-")); + try { + let projectDir = projectWith(root, "minimumReleaseAge: abc\n"); + let r = resolveMaturityPolicy(projectDir, makeEnv(root)); + assert.equal(r.ok, false); + if (r.ok) return; + assert.match(r.reason, /not a non-negative integer/); + + projectDir = projectWith(root, "minimumReleaseAge: -5\n"); + r = resolveMaturityPolicy(projectDir, makeEnv(root)); + assert.equal(r.ok, false); + + projectDir = projectWith(root, "minimumReleaseAge: 1440\nminimumReleaseAge: 60\n"); + r = resolveMaturityPolicy(projectDir, makeEnv(root)); + assert.equal(r.ok, false); + if (r.ok) return; + assert.match(r.reason, /duplicate/); + + projectDir = projectWith(root, "minimumReleaseAge: 1440\nminimumReleaseAgeExclude: just-a-scalar\n"); + r = resolveMaturityPolicy(projectDir, makeEnv(root)); + assert.equal(r.ok, false); + if (r.ok) return; + assert.match(r.reason, /must be a list/); + + projectDir = projectWith(root, "minimumReleaseAge: 1440\nminimumReleaseAgeExclude: [a,\n b]\n"); + r = resolveMaturityPolicy(projectDir, makeEnv(root)); + assert.equal(r.ok, false); + if (r.ok) return; + assert.match(r.reason, /multi-line flow/); + + // a directory where a config file should be: unreadable → fail closed + const weird = projectWith(root, undefined, undefined, "p-unreadable"); + mkdirSync(path.join(weird, ".npmrc")); + r = resolveMaturityPolicy(weird, makeEnv(root)); + assert.equal(r.ok, false); + if (r.ok) return; + assert.match(r.reason, /cannot read project \.npmrc/); + } finally { + rmrf(root); + } +}); + +// —— matchMaturityExclude: the pnpm version-policy rule grammar ——— + +test("exclude: bare name and scoped name exclude every version", () => { + assert.deepEqual(matchMaturityExclude(["billion-context"], "billion-context", "9.9.9"), { matched: true, spec: "billion-context" }); + assert.deepEqual(matchMaturityExclude(["@scope/tools"], "@scope/tools", "1.0.0"), { matched: true, spec: "@scope/tools" }); + assert.deepEqual(matchMaturityExclude(["other"], "billion-context", "1.0.0"), { matched: false }); +}); + +test("exclude: exact name@version and version unions", () => { + assert.deepEqual(matchMaturityExclude(["billion-context@1.0.0"], "billion-context", "1.0.0"), { matched: true, spec: "billion-context@1.0.0" }); + assert.deepEqual(matchMaturityExclude(["billion-context@1.0.0"], "billion-context", "1.0.1"), { matched: false }); + assert.deepEqual(matchMaturityExclude(["billion-context@1.0.0 || 2.0.0"], "billion-context", "2.0.0"), { matched: true, spec: "billion-context@1.0.0 || 2.0.0" }); + assert.deepEqual(matchMaturityExclude(["billion-context@1.0.0 || 2.0.0"], "billion-context", "3.0.0"), { matched: false }); +}); + +test("exclude: wildcard names (no version part) match by glob", () => { + assert.deepEqual(matchMaturityExclude(["is-*"], "is-foo", "1.0.0"), { matched: true, spec: "is-*" }); + assert.deepEqual(matchMaturityExclude(["@scope/*"], "@scope/anything", "1.0.0"), { matched: true, spec: "@scope/*" }); + assert.deepEqual(matchMaturityExclude(["is-*"], "other", "1.0.0"), { matched: false }); + assert.deepEqual(matchMaturityExclude(["b?llion-context"], "billion-context", "1.0.0"), { matched: true, spec: "b?llion-context" }); +}); + +test("exclude: malformed rules are errors, not silent misses", () => { + assert.ok("error" in matchMaturityExclude(["is-*@1.0.0"], "is-foo", "1.0.0")); + assert.ok("error" in matchMaturityExclude(["billion-context@1.0.0 ||"], "billion-context", "1.0.0")); + assert.ok("error" in matchMaturityExclude([""], "billion-context", "1.0.0")); +}); + +// —— checkReleaseMaturity: verdicts against a stubbed packument ——— + +test("gate: no policy → no-policy, zero registry traffic", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bc-maturity-gate-nopolicy-")); + const originalFetch = globalThis.fetch; + try { + const projectDir = projectWith(root); + globalThis.fetch = (() => Promise.reject(new Error("no registry traffic expected with no policy"))) as unknown as typeof fetch; + const v = await checkReleaseMaturity({ projectDir, packageName: "billion-context", version: "1.0.0", registryBase: REGISTRY, env: makeEnv(root) }); + assert.deepEqual(v, { kind: "no-policy" }); + } finally { + globalThis.fetch = originalFetch; + rmrf(root); + } +}); + +test("gate: excluded package → excluded, zero registry traffic", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bc-maturity-gate-excluded-")); + const originalFetch = globalThis.fetch; + try { + const projectDir = projectWith(root, "minimumReleaseAge: 1440\nminimumReleaseAgeExclude:\n - billion-context\n"); + globalThis.fetch = (() => Promise.reject(new Error("no registry traffic expected for an excluded package"))) as unknown as typeof fetch; + const v = await checkReleaseMaturity({ projectDir, packageName: "billion-context", version: "1.0.0", registryBase: REGISTRY, env: makeEnv(root) }); + assert.deepEqual(v, { kind: "excluded", spec: "billion-context", source: "project pnpm-workspace.yaml" }); + } finally { + globalThis.fetch = originalFetch; + rmrf(root); + } +}); + +// NOTE: the module keeps an in-process memo keyed by package@version, so every +// gate test below uses a UNIQUE version — a reused version would be served +// from the memo and the stub (and the verdict under test) never exercised. + +test("gate: mature release → mature (publish time outside the window)", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bc-maturity-gate-mature-")); + const originalFetch = globalThis.fetch; + try { + const projectDir = projectWith(root, "minimumReleaseAge: 1440\n"); + const publishedAt = isoHoursAgo(48); + const maturityCalls = stubPackumentFetch({ created: publishedAt, modified: publishedAt, "2.0.0": publishedAt }); + const v = await checkReleaseMaturity({ projectDir, packageName: "billion-context", version: "2.0.0", registryBase: REGISTRY, env: makeEnv(root) }); + assert.deepEqual(v, { kind: "mature", publishedAt, minAgeMinutes: 1440 }); + assert.equal(maturityCalls.packumentCalls, 1); + } finally { + globalThis.fetch = originalFetch; + rmrf(root); + } +}); + +test("gate: immature release → immature with the policy source named", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bc-maturity-gate-immature-")); + const originalFetch = globalThis.fetch; + try { + const projectDir = projectWith(root, "minimumReleaseAge: 1440\n"); + const publishedAt = isoHoursAgo(1); + stubPackumentFetch({ created: publishedAt, modified: publishedAt, "3.0.0": publishedAt }); + const v = await checkReleaseMaturity({ projectDir, packageName: "billion-context", version: "3.0.0", registryBase: REGISTRY, env: makeEnv(root) }); + assert.deepEqual(v, { kind: "immature", publishedAt, minAgeMinutes: 1440, source: "project pnpm-workspace.yaml" }); + } finally { + globalThis.fetch = originalFetch; + rmrf(root); + } +}); + +test("gate: packument without a usable time fails closed (undatable)", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bc-maturity-gate-notime-")); + const originalFetch = globalThis.fetch; + try { + const projectDir = projectWith(root, "minimumReleaseAge: 1440\n"); + // no time key at all + stubPackumentFetch(undefined); + let v = await checkReleaseMaturity({ projectDir, packageName: "billion-context", version: "4.0.0", registryBase: REGISTRY, env: makeEnv(root) }); + assert.equal(v.kind, "undatable"); + if (v.kind === "undatable") assert.match(v.reason, /no publish time/); + // time map present but missing the target version + stubPackumentFetch({ created: isoHoursAgo(48), modified: isoHoursAgo(48), "0.9.0": isoHoursAgo(48) }); + v = await checkReleaseMaturity({ projectDir, packageName: "billion-context", version: "4.1.0", registryBase: REGISTRY, env: makeEnv(root) }); + assert.equal(v.kind, "undatable"); + } finally { + globalThis.fetch = originalFetch; + rmrf(root); + } +}); + +test("gate: fetch failure and non-OK status fail closed (undatable)", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bc-maturity-gate-fetchfail-")); + const originalFetch = globalThis.fetch; + try { + const projectDir = projectWith(root, "minimumReleaseAge: 1440\n"); + stubPackumentFetch({ "5.0.0": isoHoursAgo(48) }, { failNetwork: true }); + let v = await checkReleaseMaturity({ projectDir, packageName: "billion-context", version: "5.0.0", registryBase: REGISTRY, env: makeEnv(root) }); + assert.equal(v.kind, "undatable"); + if (v.kind === "undatable") assert.match(v.reason, /registry fetch failed/); + stubPackumentFetch({ "6.0.0": isoHoursAgo(48) }, { status: 404 }); + v = await checkReleaseMaturity({ projectDir, packageName: "billion-context", version: "6.0.0", registryBase: REGISTRY, env: makeEnv(root) }); + assert.equal(v.kind, "undatable"); + if (v.kind === "undatable") assert.match(v.reason, /HTTP 404/); + } finally { + globalThis.fetch = originalFetch; + rmrf(root); + } +}); + +test("gate: malformed policy fails closed before any registry traffic", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bc-maturity-gate-badpolicy-")); + const originalFetch = globalThis.fetch; + try { + const projectDir = projectWith(root, "minimumReleaseAge: abc\n"); + globalThis.fetch = (() => Promise.reject(new Error("no registry traffic expected when the policy is malformed"))) as unknown as typeof fetch; + const v = await checkReleaseMaturity({ projectDir, packageName: "billion-context", version: "1.0.0", registryBase: REGISTRY, env: makeEnv(root) }); + assert.equal(v.kind, "undatable"); + if (v.kind === "undatable") assert.match(v.reason, /not a non-negative integer/); + } finally { + globalThis.fetch = originalFetch; + rmrf(root); + } +}); + +test("gate: time cache — repeat checks for the same version do not refetch", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bc-maturity-gate-cache-")); + const originalFetch = globalThis.fetch; + try { + const projectDir = projectWith(root, "minimumReleaseAge: 1440\n"); + const publishedAt = isoHoursAgo(48); + const maturityCalls = stubPackumentFetch({ created: publishedAt, modified: publishedAt, "7.7.7": publishedAt, "7.7.8": isoHoursAgo(1) }); + const env = makeEnv(root); + const v1 = await checkReleaseMaturity({ projectDir, packageName: "billion-context", version: "7.7.7", registryBase: REGISTRY, env }); + assert.equal(v1.kind, "mature"); + // same version again: memo + file cache — no traffic + const v2 = await checkReleaseMaturity({ projectDir, packageName: "billion-context", version: "7.7.7", registryBase: REGISTRY, env }); + assert.equal(v2.kind, "mature"); + // a DIFFERENT version from the same packument: still served from cache + const v3 = await checkReleaseMaturity({ projectDir, packageName: "billion-context", version: "7.7.8", registryBase: REGISTRY, env }); + assert.equal(v3.kind, "immature"); + assert.equal(maturityCalls.packumentCalls, 1, `expected exactly one packument fetch, got ${maturityCalls.packumentCalls}`); + } finally { + globalThis.fetch = originalFetch; + rmrf(root); + } +}); diff --git a/tests/update-desktop-inplace.test.ts b/tests/update-desktop-inplace.test.ts index a169dc32fa..9539ab8037 100644 --- a/tests/update-desktop-inplace.test.ts +++ b/tests/update-desktop-inplace.test.ts @@ -145,6 +145,16 @@ function captureLog(): { lines: string[]; log: CaptureLog } { return { lines, log }; } +// #7139: the maturity gate resolves the profile's pnpm policy from the env it +// is given (user ~/.npmrc, global config.yaml, $PREFIX/etc/npmrc). The +// drive-path tests must not see the machine's real config — point +// HOME/XDG_CONFIG_HOME/PREFIX at empty temp dirs. +function hermeticPolicyEnv(root: string): NodeJS.ProcessEnv { + const home = path.join(root, "home"); + mkdirSync(path.join(home, ".config"), { recursive: true }); + return { ...process.env, HOME: home, XDG_CONFIG_HOME: path.join(home, ".config"), PREFIX: path.join(root, "prefix") }; +} + test("refreshDshDesktopCopy: stale junction displaced in place through the full drive path (#1575)", { timeout: 30_000 }, async (t) => { const root = mkdtempSync(path.join(tmpdir(), "bc-desktop-drive-")); const prevDshHome = process.env.DSH_HOME; @@ -164,7 +174,7 @@ test("refreshDshDesktopCopy: stale junction displaced in place through the full process.env.XDG_CACHE_HOME = path.join(root, "cache"); stubRegistryFetch(fx.tgz, "https://registry.test/bc-2.0.0.tgz", "2.0.0", integrityField(fx.tgz)); const { lines, log } = captureLog(); - await refreshDshDesktopCopy("2.0.0", log, process.env); + await refreshDshDesktopCopy("2.0.0", log, hermeticPolicyEnv(root)); assert.equal(lstatSync(fx.flat).isSymbolicLink(), false, "the pnpm link must be replaced, not followed"); assert.equal(lstatSync(fx.flat).isDirectory(), true); @@ -277,7 +287,7 @@ test("refreshDshDesktopCopy: manifest spec converges to the installed version, r process.env.DSH_HOME = fx.dshHome; process.env.XDG_CACHE_HOME = path.join(root, "cache"); stubRegistryFetch(fx.tgz, "https://registry.test/bc-0.1.194.tgz", "0.1.194", integrityField(fx.tgz)); - await refreshDshDesktopCopy("0.1.194", captureLog().log, process.env); + await refreshDshDesktopCopy("0.1.194", captureLog().log, hermeticPolicyEnv(root)); assert.equal(await readDiskVersion(fx.flat), "0.1.194"); const manifest = JSON.parse(readFileSync(manifestPath, "utf8")) as { @@ -346,7 +356,7 @@ test("refreshDshDesktopCopy converges the lockfile record along with the manifes process.env.DSH_HOME = fx.dshHome; process.env.XDG_CACHE_HOME = path.join(root, "cache"); stubRegistryFetch(fx.tgz, "https://registry.test/bc-0.1.194.tgz", "0.1.194", integrityField(fx.tgz)); - await refreshDshDesktopCopy("0.1.194", captureLog().log, process.env); + await refreshDshDesktopCopy("0.1.194", captureLog().log, hermeticPolicyEnv(root)); assert.equal(await readDiskVersion(fx.flat), "0.1.194"); const manifest = JSON.parse(readFileSync(path.join(profileDir, "package.json"), "utf8")) as { dependencies?: Record }; @@ -441,6 +451,380 @@ test("syncDshProfileManifestSpec writes atomically — converges and leaves no o } }); +// —— #7139: the pnpm minimumReleaseAge gate on the in-place lane ——— + +function isoHoursAgo(hours: number): string { + return new Date(Date.now() - hours * 3_600_000).toISOString(); +} + +/** Registry stub serving the packument (the gate's publish-time source) + * alongside the dist doc + tarball. `time` = the packument's time map; + * undefined = no time key at all. */ +function stubMaturityRegistryFetch(tgz: Buffer, tarballUrl: string, version: string, integrity: string, time: Record | undefined, opts: { failNetwork?: boolean } = {}): { packumentCalls: number } { + const state = { packumentCalls: 0 }; + globalThis.fetch = ((url: unknown) => { + const u = String(url); + if (u.endsWith("/billion-context")) { + state.packumentCalls++; + if (opts.failNetwork) return Promise.reject(new Error("network down")); + const doc: Record = { name: "billion-context", versions: {} }; + if (time !== undefined) doc.time = time; + return Promise.resolve(new Response(JSON.stringify(doc), { headers: { "content-type": "application/json" } })); + } + if (u.endsWith(`/billion-context/${version}`)) { + return Promise.resolve(new Response(JSON.stringify({ dist: { tarball: tarballUrl, integrity } }), { headers: { "content-type": "application/json" } })); + } + if (u === tarballUrl) { + return Promise.resolve(new Response(tgz)); + } + return Promise.reject(new Error(`unexpected fetch in test: ${u}`)); + }) as unknown as typeof fetch; + return state; +} + +/** The frozen backoff state file (see the #2206 note at the top of this + * file) — read straight from disk so the assertion is about what the lane + * PERSISTED, not in-process bookkeeping. */ +function backoffStrikeCount(version: string): number { + try { + const doc = JSON.parse(readFileSync(path.join(__cacheRoot, "cache", "billion-context", ".install-backoff.json"), "utf8")) as { entries?: Record }; + return doc.entries?.[`owner:dsh-desktop:${version}`]?.count ?? 0; + } catch { + return 0; + } +} + +test("refreshDshDesktopCopy: immature release deferred — copy, manifest, lockfile untouched, no strike (#7139)", { timeout: 30_000 }, async (t) => { + const root = mkdtempSync(path.join(tmpdir(), "bc-desktop-maturity-defer-")); + const prevDshHome = process.env.DSH_HOME; + const prevXdgCacheHome = process.env.XDG_CACHE_HOME; + const originalFetch = globalThis.fetch; + t.after(() => { + globalThis.fetch = originalFetch; + if (prevDshHome === undefined) delete process.env.DSH_HOME; + else process.env.DSH_HOME = prevDshHome; + if (prevXdgCacheHome === undefined) delete process.env.XDG_CACHE_HOME; + else process.env.XDG_CACHE_HOME = prevXdgCacheHome; + rmrf(root); + }); + try { + const fx = buildDesktopFixture(root, "1.2.3", "3.0.0"); + const profileDir = path.join(fx.dshHome, "profiles", "desktop"); + const manifestBefore = JSON.stringify({ + name: "@dsh/profile-desktop", + dependencies: { "billion-context": "^1.2.3" }, + dsh: { profile: { bundles: ["billion-context"] } }, + }, null, 2) + "\n"; + writeFileSync(path.join(profileDir, "package.json"), manifestBefore); + const lockBefore = [ + "lockfileVersion: '9.0'", + "", + "importers:", + "", + " .:", + " dependencies:", + " billion-context:", + " specifier: ^1.2.3", + " version: 1.2.3", + "", + "packages:", + "", + " billion-context@1.2.3:", + " resolution: {integrity: sha512-fake==}", + "", + "snapshots:", + "", + " billion-context@1.2.3: {}", + "", + ].join("\n"); + writeFileSync(path.join(profileDir, "pnpm-lock.yaml"), lockBefore); + writeFileSync(path.join(profileDir, "pnpm-workspace.yaml"), "minimumReleaseAge: 1440\n"); + process.env.DSH_HOME = fx.dshHome; + process.env.XDG_CACHE_HOME = path.join(root, "cache"); + const env = hermeticPolicyEnv(root); + const publishedAt = isoHoursAgo(1); + const maturityCalls = stubMaturityRegistryFetch(fx.tgz, "https://registry.test/bc-3.0.0.tgz", "3.0.0", integrityField(fx.tgz), { created: publishedAt, modified: publishedAt, "3.0.0": publishedAt }); + const { lines, log } = captureLog(); + await refreshDshDesktopCopy("3.0.0", log, env); + + assert.equal(lstatSync(fx.flat).isSymbolicLink(), true, "the in-place swap must not have run"); + assert.equal(await readDiskVersion(fx.storeCopy), "1.2.3"); + assert.equal(readFileSync(path.join(profileDir, "package.json"), "utf8"), manifestBefore, "the manifest must be byte-identical"); + assert.equal(readFileSync(path.join(profileDir, "pnpm-lock.yaml"), "utf8"), lockBefore, "the lockfile must be byte-identical"); + assert.equal(maturityCalls.packumentCalls, 1, "the gate must have consulted the registry publish time"); + assert.ok(lines.some((l) => l.startsWith("info:") && l.includes("minimumReleaseAge") && l.includes("deferring")), `expected the deferral line, got: ${lines.join(" | ")}`); + assert.ok(!lines.some((l) => l.startsWith("warn:")), `unexpected warns: ${lines.join(" | ")}`); + assert.equal(backoffStrikeCount("3.0.0"), 0, "deferral is not a failure — no backoff strike"); + } catch (e) { + if (String(e).includes("EPERM")) { + t.skip("creating directory symlinks requires elevated privileges on this platform"); + return; + } + throw e; + } +}); + +test("refreshDshDesktopCopy: minimumReleaseAgeExclude lets the excluded release through (#7139)", { timeout: 30_000 }, async (t) => { + const root = mkdtempSync(path.join(tmpdir(), "bc-desktop-maturity-excluded-")); + const prevDshHome = process.env.DSH_HOME; + const prevXdgCacheHome = process.env.XDG_CACHE_HOME; + const originalFetch = globalThis.fetch; + t.after(() => { + globalThis.fetch = originalFetch; + if (prevDshHome === undefined) delete process.env.DSH_HOME; + else process.env.DSH_HOME = prevDshHome; + if (prevXdgCacheHome === undefined) delete process.env.XDG_CACHE_HOME; + else process.env.XDG_CACHE_HOME = prevXdgCacheHome; + rmrf(root); + }); + try { + const fx = buildDesktopFixture(root, "1.2.3", "3.1.0"); + const profileDir = path.join(fx.dshHome, "profiles", "desktop"); + writeFileSync(path.join(profileDir, "pnpm-workspace.yaml"), [ + "minimumReleaseAge: 1440", + "minimumReleaseAgeExclude:", + " - billion-context", + "", + ].join("\n")); + process.env.DSH_HOME = fx.dshHome; + process.env.XDG_CACHE_HOME = path.join(root, "cache"); + const publishedAt = isoHoursAgo(1); // immature — but excluded + const maturityCalls = stubMaturityRegistryFetch(fx.tgz, "https://registry.test/bc-3.1.0.tgz", "3.1.0", integrityField(fx.tgz), { created: publishedAt, modified: publishedAt, "3.1.0": publishedAt }); + const { lines, log } = captureLog(); + await refreshDshDesktopCopy("3.1.0", log, hermeticPolicyEnv(root)); + + assert.equal(lstatSync(fx.flat).isSymbolicLink(), false, "the excluded release must install"); + assert.equal(await readDiskVersion(fx.flat), "3.1.0"); + assert.equal(maturityCalls.packumentCalls, 0, "an excluded package must not need the publish time"); + assert.ok(lines.some((l) => l.startsWith("info:") && l.includes("in place")), `expected the in-place success line, got: ${lines.join(" | ")}`); + assert.ok(!lines.some((l) => l.includes("deferring")), `must not defer an excluded release: ${lines.join(" | ")}`); + } catch (e) { + if (String(e).includes("EPERM")) { + t.skip("creating directory symlinks requires elevated privileges on this platform"); + return; + } + throw e; + } +}); + +test("refreshDshDesktopCopy: an exclude for a different version does not lift the gate (#7139)", { timeout: 30_000 }, async (t) => { + const root = mkdtempSync(path.join(tmpdir(), "bc-desktop-maturity-excl-other-")); + const prevDshHome = process.env.DSH_HOME; + const prevXdgCacheHome = process.env.XDG_CACHE_HOME; + const originalFetch = globalThis.fetch; + t.after(() => { + globalThis.fetch = originalFetch; + if (prevDshHome === undefined) delete process.env.DSH_HOME; + else process.env.DSH_HOME = prevDshHome; + if (prevXdgCacheHome === undefined) delete process.env.XDG_CACHE_HOME; + else process.env.XDG_CACHE_HOME = prevXdgCacheHome; + rmrf(root); + }); + try { + const fx = buildDesktopFixture(root, "1.2.3", "3.2.0"); + const profileDir = path.join(fx.dshHome, "profiles", "desktop"); + writeFileSync(path.join(profileDir, "pnpm-workspace.yaml"), [ + "minimumReleaseAge: 1440", + "minimumReleaseAgeExclude:", + " - billion-context@9.9.9", + "", + ].join("\n")); + process.env.DSH_HOME = fx.dshHome; + process.env.XDG_CACHE_HOME = path.join(root, "cache"); + const publishedAt = isoHoursAgo(1); + stubMaturityRegistryFetch(fx.tgz, "https://registry.test/bc-3.2.0.tgz", "3.2.0", integrityField(fx.tgz), { created: publishedAt, modified: publishedAt, "3.2.0": publishedAt }); + const { lines, log } = captureLog(); + await refreshDshDesktopCopy("3.2.0", log, hermeticPolicyEnv(root)); + + assert.equal(lstatSync(fx.flat).isSymbolicLink(), true, "the unrelated exclude must not lift the gate"); + assert.ok(lines.some((l) => l.startsWith("info:") && l.includes("deferring")), `expected the deferral line, got: ${lines.join(" | ")}`); + assert.equal(backoffStrikeCount("3.2.0"), 0); + } catch (e) { + if (String(e).includes("EPERM")) { + t.skip("creating directory symlinks requires elevated privileges on this platform"); + return; + } + throw e; + } +}); + +test("refreshDshDesktopCopy: a mature release updates and converges the ledgers as usual (#7139)", { timeout: 30_000 }, async (t) => { + const root = mkdtempSync(path.join(tmpdir(), "bc-desktop-maturity-mature-")); + const prevDshHome = process.env.DSH_HOME; + const prevXdgCacheHome = process.env.XDG_CACHE_HOME; + const originalFetch = globalThis.fetch; + t.after(() => { + globalThis.fetch = originalFetch; + if (prevDshHome === undefined) delete process.env.DSH_HOME; + else process.env.DSH_HOME = prevDshHome; + if (prevXdgCacheHome === undefined) delete process.env.XDG_CACHE_HOME; + else process.env.XDG_CACHE_HOME = prevXdgCacheHome; + rmrf(root); + }); + try { + const fx = buildDesktopFixture(root, "1.2.3", "4.0.0"); + const profileDir = path.join(fx.dshHome, "profiles", "desktop"); + writeFileSync(path.join(profileDir, "package.json"), JSON.stringify({ + name: "@dsh/profile-desktop", + dependencies: { "billion-context": "^1.2.3", "other-dep": "^1.2.3" }, + dsh: { profile: { bundles: ["billion-context"] } }, + }, null, 2) + "\n"); + const lockPath = path.join(profileDir, "pnpm-lock.yaml"); + writeFileSync(lockPath, [ + "lockfileVersion: '9.0'", + "", + "importers:", + "", + " .:", + " dependencies:", + " billion-context:", + " specifier: ^1.2.3", + " version: 1.2.3", + "", + "packages:", + "", + " billion-context@1.2.3:", + " resolution: {integrity: sha512-fake==}", + "", + "snapshots:", + "", + " billion-context@1.2.3: {}", + "", + ].join("\n")); + writeFileSync(path.join(profileDir, "pnpm-workspace.yaml"), "minimumReleaseAge: 1440\n"); + process.env.DSH_HOME = fx.dshHome; + process.env.XDG_CACHE_HOME = path.join(root, "cache"); + const publishedAt = isoHoursAgo(48); // outside the 24h window + stubMaturityRegistryFetch(fx.tgz, "https://registry.test/bc-4.0.0.tgz", "4.0.0", integrityField(fx.tgz), { created: publishedAt, modified: publishedAt, "4.0.0": publishedAt }); + const { lines, log } = captureLog(); + await refreshDshDesktopCopy("4.0.0", log, hermeticPolicyEnv(root)); + + assert.equal(lstatSync(fx.flat).isSymbolicLink(), false, "the mature release must install"); + assert.equal(await readDiskVersion(fx.flat), "4.0.0"); + const manifest = JSON.parse(readFileSync(path.join(profileDir, "package.json"), "utf8")) as { dependencies?: Record }; + assert.equal(manifest.dependencies?.["billion-context"], "^4.0.0", "the manifest must converge"); + assert.equal(manifest.dependencies?.["other-dep"], "^1.2.3"); + const lockText = readFileSync(lockPath, "utf8"); + assert.match(lockText, /specifier: \^4\.0\.0/); + assert.match(lockText, /version: 4\.0\.0/); + assert.ok(lockText.includes("billion-context@1.2.3:"), "store sections stay pnpm's to reconcile"); + assert.ok(lines.some((l) => l.startsWith("info:") && l.includes("in place")), `expected the in-place success line, got: ${lines.join(" | ")}`); + } catch (e) { + if (String(e).includes("EPERM")) { + t.skip("creating directory symlinks requires elevated privileges on this platform"); + return; + } + throw e; + } +}); + +test("refreshDshDesktopCopy: publish time unavailable → fail closed, strike recorded, copy untouched (#7139)", { timeout: 30_000 }, async (t) => { + const root = mkdtempSync(path.join(tmpdir(), "bc-desktop-maturity-notime-")); + const prevDshHome = process.env.DSH_HOME; + const prevXdgCacheHome = process.env.XDG_CACHE_HOME; + const originalFetch = globalThis.fetch; + t.after(() => { + globalThis.fetch = originalFetch; + if (prevDshHome === undefined) delete process.env.DSH_HOME; + else process.env.DSH_HOME = prevDshHome; + if (prevXdgCacheHome === undefined) delete process.env.XDG_CACHE_HOME; + else process.env.XDG_CACHE_HOME = prevXdgCacheHome; + rmrf(root); + }); + try { + const fx = buildDesktopFixture(root, "1.2.3", "5.0.0"); + const profileDir = path.join(fx.dshHome, "profiles", "desktop"); + writeFileSync(path.join(profileDir, "pnpm-workspace.yaml"), "minimumReleaseAge: 1440\n"); + process.env.DSH_HOME = fx.dshHome; + process.env.XDG_CACHE_HOME = path.join(root, "cache"); + stubMaturityRegistryFetch(fx.tgz, "https://registry.test/bc-5.0.0.tgz", "5.0.0", integrityField(fx.tgz), undefined); // no time key + const { lines, log } = captureLog(); + await refreshDshDesktopCopy("5.0.0", log, hermeticPolicyEnv(root)); + + assert.equal(lstatSync(fx.flat).isSymbolicLink(), true, "fail closed: the swap must not have run"); + assert.equal(await readDiskVersion(fx.storeCopy), "1.2.3"); + assert.ok(lines.some((l) => l.startsWith("warn:") && l.includes("fail closed")), `expected the fail-closed warn, got: ${lines.join(" | ")}`); + assert.equal(backoffStrikeCount("5.0.0"), 1, "an undatable policy must record a strike (backed-off retry, not silent retry)"); + } catch (e) { + if (String(e).includes("EPERM")) { + t.skip("creating directory symlinks requires elevated privileges on this platform"); + return; + } + throw e; + } +}); + +test("refreshDshDesktopCopy: registry unreachable → fail closed, strike recorded (#7139)", { timeout: 30_000 }, async (t) => { + const root = mkdtempSync(path.join(tmpdir(), "bc-desktop-maturity-netfail-")); + const prevDshHome = process.env.DSH_HOME; + const prevXdgCacheHome = process.env.XDG_CACHE_HOME; + const originalFetch = globalThis.fetch; + t.after(() => { + globalThis.fetch = originalFetch; + if (prevDshHome === undefined) delete process.env.DSH_HOME; + else process.env.DSH_HOME = prevDshHome; + if (prevXdgCacheHome === undefined) delete process.env.XDG_CACHE_HOME; + else process.env.XDG_CACHE_HOME = prevXdgCacheHome; + rmrf(root); + }); + try { + const fx = buildDesktopFixture(root, "1.2.3", "5.1.0"); + const profileDir = path.join(fx.dshHome, "profiles", "desktop"); + writeFileSync(path.join(profileDir, "pnpm-workspace.yaml"), "minimumReleaseAge: 1440\n"); + process.env.DSH_HOME = fx.dshHome; + process.env.XDG_CACHE_HOME = path.join(root, "cache"); + stubMaturityRegistryFetch(fx.tgz, "https://registry.test/bc-5.1.0.tgz", "5.1.0", integrityField(fx.tgz), { "5.1.0": isoHoursAgo(48) }, { failNetwork: true }); + const { lines, log } = captureLog(); + await refreshDshDesktopCopy("5.1.0", log, hermeticPolicyEnv(root)); + + assert.equal(lstatSync(fx.flat).isSymbolicLink(), true, "fail closed: the swap must not have run"); + assert.ok(lines.some((l) => l.startsWith("warn:") && l.includes("fail closed")), `expected the fail-closed warn, got: ${lines.join(" | ")}`); + assert.equal(backoffStrikeCount("5.1.0"), 1); + } catch (e) { + if (String(e).includes("EPERM")) { + t.skip("creating directory symlinks requires elevated privileges on this platform"); + return; + } + throw e; + } +}); + +test("refreshDshDesktopCopy: malformed maturity policy → fail closed before any registry traffic (#7139)", { timeout: 30_000 }, async (t) => { + const root = mkdtempSync(path.join(tmpdir(), "bc-desktop-maturity-badpolicy-")); + const prevDshHome = process.env.DSH_HOME; + const prevXdgCacheHome = process.env.XDG_CACHE_HOME; + const originalFetch = globalThis.fetch; + t.after(() => { + globalThis.fetch = originalFetch; + if (prevDshHome === undefined) delete process.env.DSH_HOME; + else process.env.DSH_HOME = prevDshHome; + if (prevXdgCacheHome === undefined) delete process.env.XDG_CACHE_HOME; + else process.env.XDG_CACHE_HOME = prevXdgCacheHome; + rmrf(root); + }); + try { + const fx = buildDesktopFixture(root, "1.2.3", "5.2.0"); + const profileDir = path.join(fx.dshHome, "profiles", "desktop"); + writeFileSync(path.join(profileDir, "pnpm-workspace.yaml"), "minimumReleaseAge: abc\n"); + process.env.DSH_HOME = fx.dshHome; + process.env.XDG_CACHE_HOME = path.join(root, "cache"); + const maturityCalls = stubMaturityRegistryFetch(fx.tgz, "https://registry.test/bc-5.2.0.tgz", "5.2.0", integrityField(fx.tgz), { "5.2.0": isoHoursAgo(48) }); + const { lines, log } = captureLog(); + await refreshDshDesktopCopy("5.2.0", log, hermeticPolicyEnv(root)); + + assert.equal(lstatSync(fx.flat).isSymbolicLink(), true, "fail closed: the swap must not have run"); + assert.equal(maturityCalls.packumentCalls, 0, "a malformed policy must fail before any registry traffic"); + assert.ok(lines.some((l) => l.startsWith("warn:") && l.includes("fail closed")), `expected the fail-closed warn, got: ${lines.join(" | ")}`); + assert.equal(backoffStrikeCount("5.2.0"), 1); + } catch (e) { + if (String(e).includes("EPERM")) { + t.skip("creating directory symlinks requires elevated privileges on this platform"); + return; + } + throw e; + } +}); + test("dshDesktopCopyPathMatches honors Windows case-insensitivity (#2972-L3)", () => { const anchor = "/home/u/.dsh/profiles/desktop"; const suffix = "/node_modules/billion-context"; From 12aba96570fca496018f6b4494a98e93fbc84921 Mon Sep 17 00:00:00 2001 From: ci-check Date: Wed, 18 Nov 2026 19:53:38 +0800 Subject: [PATCH 2/3] fix(update): keep MaturityVerdict module-internal (unused-exports gate) The verdict type is only consumed through the inferred return type of checkReleaseMaturity; nothing imports it, so the export is dead surface. --- src/release-maturity.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/release-maturity.ts b/src/release-maturity.ts index fa879d3fb3..588144688d 100644 --- a/src/release-maturity.ts +++ b/src/release-maturity.ts @@ -48,7 +48,7 @@ import type { FetchOptions } from "./fetch-util.js"; const PACKUMENT_FETCH_TIMEOUT_MS = 30_000; -export type MaturityVerdict = +type MaturityVerdict = | { kind: "no-policy" } | { kind: "excluded"; spec: string; source: string } | { kind: "mature"; publishedAt: string; minAgeMinutes: number } From e82c1a7bd6748d78e7ce414001c7147b2e235feb Mon Sep 17 00:00:00 2001 From: ci-check Date: Wed, 18 Nov 2026 20:28:21 +0800 Subject: [PATCH 3/3] fix(update): a null minimumReleaseAgeExclude must not shadow a lower list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit pnpm treats a null list value as unset (serde None), so a bare 'minimumReleaseAgeExclude:' with no items must fall through to the next source — the parser modeled it as an empty string, which shadowed a lower-precedence exclude list (contradicting the module's own documented contract: null/empty = 'not provided'). An explicit empty list '[]' stays 'provided' and clears the excludes. 4 tests: 2 regression (fail on the old code), 2 pinning the '[]' semantics (the gate must still apply — the #7139 breakage direction). --- src/release-maturity.ts | 13 ++++--- tests/release-maturity.test.ts | 70 ++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 5 deletions(-) diff --git a/src/release-maturity.ts b/src/release-maturity.ts index 588144688d..bef0f42f47 100644 --- a/src/release-maturity.ts +++ b/src/release-maturity.ts @@ -19,9 +19,11 @@ * project .npmrc → user ~/.npmrc → global $XDG_CONFIG_HOME/pnpm/config.yaml * → global $PREFIX/etc/npmrc. * Excludes travel with the source that defined them. A null/empty value in a - * file (e.g. `minimumReleaseAge:` with nothing after) is "not provided" and - * does not shadow a positive value in a lower-precedence source; only an - * explicit number (0 = policy off) wins. + * file (e.g. `minimumReleaseAge:` with nothing after, or a bare + * `minimumReleaseAgeExclude:` with no list) is "not provided" and does not + * shadow a value in a lower-precedence source; only an explicit value wins — + * a number for the age (0 = policy off), or a list for the excludes (an + * explicit empty list `[]` IS provided: it clears the excludes). * * Publish time comes from the registry packument's `time` map — the same * field pnpm's resolver reads (the tarball's Last-Modified is NOT reliable: @@ -254,7 +256,8 @@ function parseYamlPolicyKeys(text: string): { minAgeRaw?: string; excludeRaw?: s if (out.excludeRaw !== undefined) { out.error = "duplicate minimumReleaseAgeExclude key"; return out; } const v = stripInlineComment(rest).trim(); if (v === "" || v === "~" || v.toLowerCase() === "null") { - // block list follows (or the value was null = no excludes) + // a block list may follow; if none does, the value is null = + // "not provided" (must not shadow a lower-precedence list) const items: string[] = []; let sawItem = false; while (i < lines.length) { @@ -268,7 +271,7 @@ function parseYamlPolicyKeys(text: string): { minAgeRaw?: string; excludeRaw?: s sawItem = true; items.push(stripQuotes(stripInlineComment(item[1]).trim())); } - out.excludeRaw = sawItem ? items.join(",") : ""; + if (sawItem) out.excludeRaw = items.join(","); } else if (v.startsWith("[")) { if (!v.endsWith("]")) { out.error = "multi-line flow list for minimumReleaseAgeExclude is not supported"; return out; } const items = v.slice(1, -1).split(",").map((s) => stripQuotes(stripInlineComment(s).trim())).filter((s) => s !== ""); diff --git a/tests/release-maturity.test.ts b/tests/release-maturity.test.ts index aac16dccf9..6715e8553d 100644 --- a/tests/release-maturity.test.ts +++ b/tests/release-maturity.test.ts @@ -159,6 +159,43 @@ test("policy: excludes travel with the source that defines them (env minAge + pr } }); +test("policy: a null exclude list does not shadow a lower-precedence list (pnpm: null = unset)", () => { + const root = mkdtempSync(path.join(tmpdir(), "bc-maturity-null-excl-")); + try { + const env = makeEnv(root); + writeFileSync(path.join(env.HOME as string, ".npmrc"), "minimumReleaseAgeExclude=billion-context\n"); + const projectDir = projectWith(root, "minimumReleaseAge: 1440\nminimumReleaseAgeExclude:\n"); + const r = resolveMaturityPolicy(projectDir, env); + assert.equal(r.ok, true); + if (r.ok) { + assert.equal(r.policy.minAgeMinutes, 1440); + assert.equal(r.policy.minAgeSource, "project pnpm-workspace.yaml"); + assert.deepEqual(r.policy.excludeSpecs, ["billion-context"]); + assert.equal(r.policy.excludeSource, "user ~/.npmrc"); + } + } finally { + rmrf(root); + } +}); + +test("policy: an explicit empty exclude list ([]) IS provided and clears lower lists", () => { + const root = mkdtempSync(path.join(tmpdir(), "bc-maturity-empty-excl-")); + try { + const env = makeEnv(root); + writeFileSync(path.join(env.HOME as string, ".npmrc"), "minimumReleaseAgeExclude=billion-context\n"); + const projectDir = projectWith(root, "minimumReleaseAge: 1440\nminimumReleaseAgeExclude: []\n"); + const r = resolveMaturityPolicy(projectDir, env); + assert.equal(r.ok, true); + if (r.ok) { + assert.equal(r.policy.minAgeMinutes, 1440); + assert.deepEqual(r.policy.excludeSpecs, []); + assert.equal(r.policy.excludeSource, "project pnpm-workspace.yaml"); + } + } finally { + rmrf(root); + } +}); + test("policy: yaml forms — flow list, null value, inline comments, quoted values", () => { const root = mkdtempSync(path.join(tmpdir(), "bc-maturity-yaml-")); try { @@ -331,6 +368,39 @@ test("gate: immature release → immature with the policy source named", async ( } }); +test("gate: null exclude in project yaml → the user-npmrc exclude still applies (excluded, no traffic)", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bc-maturity-gate-null-excl-")); + const originalFetch = globalThis.fetch; + try { + const env = makeEnv(root); + writeFileSync(path.join(env.HOME as string, ".npmrc"), "minimumReleaseAgeExclude=billion-context\n"); + const projectDir = projectWith(root, "minimumReleaseAge: 1440\nminimumReleaseAgeExclude:\n"); + globalThis.fetch = (() => Promise.reject(new Error("no registry traffic expected for an excluded package"))) as unknown as typeof fetch; + const v = await checkReleaseMaturity({ projectDir, packageName: "billion-context", version: "8.0.0", registryBase: REGISTRY, env }); + assert.deepEqual(v, { kind: "excluded", spec: "billion-context", source: "user ~/.npmrc" }); + } finally { + globalThis.fetch = originalFetch; + rmrf(root); + } +}); + +test("gate: explicit [] exclude in project yaml → the gate still applies (immature)", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bc-maturity-gate-empty-excl-")); + const originalFetch = globalThis.fetch; + try { + const env = makeEnv(root); + writeFileSync(path.join(env.HOME as string, ".npmrc"), "minimumReleaseAgeExclude=billion-context\n"); + const projectDir = projectWith(root, "minimumReleaseAge: 1440\nminimumReleaseAgeExclude: []\n"); + const publishedAt = isoHoursAgo(1); + stubPackumentFetch({ created: publishedAt, modified: publishedAt, "8.1.0": publishedAt }); + const v = await checkReleaseMaturity({ projectDir, packageName: "billion-context", version: "8.1.0", registryBase: REGISTRY, env }); + assert.deepEqual(v, { kind: "immature", publishedAt, minAgeMinutes: 1440, source: "project pnpm-workspace.yaml" }); + } finally { + globalThis.fetch = originalFetch; + rmrf(root); + } +}); + test("gate: packument without a usable time fails closed (undatable)", async () => { const root = mkdtempSync(path.join(tmpdir(), "bc-maturity-gate-notime-")); const originalFetch = globalThis.fetch;