From 6228e28f8e0b188d58697ef865a6594560d58666 Mon Sep 17 00:00:00 2001 From: ci-check Date: Wed, 18 Nov 2026 17:00:01 +0800 Subject: [PATCH 1/3] fix(dsh-ledger): move the pnpm store sections with the importer on in-place update MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit but left packages:/snapshots: at the old version — an internally inconsistent lockfile that hard-fails pnpm 11 remove (and dangles a symlink on pnpm >=12, entry is left alone, a missing one is cloned from the closest existing billion-context entry (same peer suffix first, then nearest semver), with the new version's SRI integrity taken from the registry doc (the source's hash is never carried). A version move that cannot be completed safely (new hash unknown while the old entry carries one) fails soft with zero writes. Also: the dep map of a cloned entry is cross-checked against the on-disk copy's declaration (declared deps missing from the map are resolved from the store; names no longer declared are dropped); a new lockfile-internal finding kind makes the broken state visible to bili doctor; doctor --fix sources the SRI hash from the registry doc (one lightweight GET per distinct drifted pnpm profile). Closes #7125 --- src/cli.ts | 2 +- src/doctor.ts | 31 +- src/dsh-ledger.ts | 423 +++++++++++++++++- src/update.ts | 8 +- .../issue3496-dsh-ledger-convergence.test.ts | 253 +++++++++-- 5 files changed, 663 insertions(+), 54 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index ae2e533fb3..abce61718f 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -812,7 +812,7 @@ export async function main(): Promise { // #3496: explicit repair pass — the read-only audit above stays // read-only; --fix is the only thing that writes dsh-profile // ledgers, and it prints every change it makes. - const fixes = fixDshLedgers(); + const fixes = await fixDshLedgers(process.env, { resolveProxy: updaterResolveProxy }); report.ledgerFixes = fixes; process.stdout.write(doctorJson ? JSON.stringify(report, null, 2) + "\n" : renderDoctorReport(report) + "\n" + renderLedgerFixReport(fixes)); if (fixes.some((f) => f.error !== undefined || f.residualFindings.length > 0)) process.exitCode = 1; diff --git a/src/doctor.ts b/src/doctor.ts index b34d4bc7b5..30f26f3966 100644 --- a/src/doctor.ts +++ b/src/doctor.ts @@ -10,10 +10,10 @@ import fs from "node:fs"; import path from "node:path"; import { isPidAlive, listInstances, procMainScript } from "./instance.js"; import { PLUGIN_AGENTS, UPDATE_CHANNEL, claudeDesktopPresent, detectOpencodeMajor, inspectLanePresence, inspectOpencodeClaudeAgent, isBiliClaudeBaseUrl, opencodeClaudeAgentBaseUrl, type PluginAgent } from "./plugin-install.js"; -import { findInstallDir, fetchRegistryVersion, hostManagedInstall, isGitWorkingTree, isVersionNewer, lastUpdateCheckTime, normalizeUpdateTag, staleInstallStatus } from "./update.js"; +import { findInstallDir, fetchRegistryVersion, fetchVersionDoc, hostManagedInstall, isGitWorkingTree, isVersionNewer, lastUpdateCheckTime, normalizeUpdateTag, staleInstallStatus } from "./update.js"; import { describeAdvisory, evaluateAdvisories, type AdvisoryEvaluation } from "./advisory.js"; import { claudeRoutingWarningFile } from "./paths.js"; -import { dshProfileDirs } from "./dsh-channel.js"; +import { DSH_PACKAGE, dshProfileDirs } from "./dsh-channel.js"; import { convergeDshLedger, dshProfileLedgerFindings, readDshLedgerState, type LedgerChangeDetail, type LedgerFinding } from "./dsh-ledger.js"; type LaneVerdict = "ok" | "stale" | "frozen" | "broken" | "absent"; @@ -218,21 +218,40 @@ export async function runDoctor(opts: DoctorOpts): Promise { * skip-condition profiles (local pin, no dep, no copy) get ZERO writes. The * desktop profile IS included here — detection excludes it by design (#3224 * option A: bili is its in-place writer), but explicit repair must reach the - * very profile where the #3160 incident happened. Pure local file ops; never - * throws (dsh never ran → []). */ -export function fixDshLedgers(env: NodeJS.ProcessEnv = process.env): DshLedgerFixRow[] { + * very profile where the #3160 incident happened. + * + * #7125: a pnpm store entry needs the target version's SRI hash, so this pass + * does ONE lightweight registry-doc GET per distinct drifted pnpm profile + * (cached per version). When the fetch fails, convergence fails soft with a + * reason and ZERO writes — strictly better than writing an inconsistent + * lockfile. Never throws (dsh never ran → []). */ +export async function fixDshLedgers( + env: NodeJS.ProcessEnv = process.env, + opts: { resolveProxy?: (url: string) => string | undefined; fetchVersionDoc?: typeof fetchVersionDoc } = {}, +): Promise { let dirs: string[]; try { dirs = dshProfileDirs(env); } catch { return []; } + const fetchDoc = opts.fetchVersionDoc ?? fetchVersionDoc; + const docCache = new Map(); const rows: DshLedgerFixRow[] = []; for (const dir of dirs) { const profile = path.basename(dir); const findingsBefore = dshProfileLedgerFindings(dir, { includeDesktop: true }); if (findingsBefore.length === 0) continue; - const res = convergeDshLedger(dir, readDshLedgerState(dir).copyVersion!); + const state = readDshLedgerState(dir); + const targetVersion = state.copyVersion!; + let integrity: string | undefined; + if (state.lockKind === "pnpm" && (state.lockStoreBroken === true || state.lockVersion !== targetVersion)) { + if (!docCache.has(targetVersion)) { + docCache.set(targetVersion, await fetchDoc({ resolveProxy: opts.resolveProxy }, DSH_PACKAGE, targetVersion)); + } + integrity = docCache.get(targetVersion)?.integrity; + } + const res = convergeDshLedger(dir, targetVersion, { integrity }); rows.push({ profile, findingsBefore, diff --git a/src/dsh-ledger.ts b/src/dsh-ledger.ts index d3e95a3426..b79de44a4a 100644 --- a/src/dsh-ledger.ts +++ b/src/dsh-ledger.ts @@ -35,10 +35,18 @@ interface DshLedgerState { lockVersion?: string; /** The version of the on-disk bundle copy, or undefined when unreadable. */ copyVersion?: string; + /** The direct deps the on-disk copy declares (its package.json) — the + * cross-check source for a cloned store entry's dep map (#7125). */ + copyDeps?: { dependencies?: Record; optionalDependencies?: Record }; + /** The pnpm lockfile records a version whose store sections + * (packages:/snapshots:) lack that version's entries — the record was + * written without its resolution data (#7125). undefined = not judged + * (no pnpm lockfile, flat ≤5.4 shape, or no recorded version). */ + lockStoreBroken?: boolean; lockKind: LedgerLockKind; } -type LedgerFindingKind = "copy-vs-lock" | "manifest-vs-lock" | "copy-vs-manifest"; +type LedgerFindingKind = "copy-vs-lock" | "manifest-vs-lock" | "copy-vs-manifest" | "lockfile-internal"; export interface LedgerFinding { profile: string; @@ -95,24 +103,32 @@ function splitPeerSuffix(v: string): [version: string, peerSuffix: string] { } /** Plain-YAML-safe scalar: quote anything that could be misread (leading - * indicators like `>` or the alias indicator `*`, whitespace, …). Versions - * and ^/~ specs stay bare; a leading `*` is quoted because YAML would parse - * it as an alias token, not a spec (pnpm's own writer quotes it too). */ + * indicators like `>` or the alias indicator `*`, whitespace, …). Versions, + * ^/~ specs, and base64 integrity hashes stay bare — their `+`/`/`/`=` are + * legal inside a YAML plain scalar, and pnpm writes them bare; a leading `*` + * is quoted because YAML would parse it as an alias token, not a spec (pnpm's + * own writer quotes it too). */ function yamlScalar(v: string): string { - return /^[A-Za-z0-9.^~_-][\w.^~*-]*$/.test(v) ? v : JSON.stringify(v); + return /^[A-Za-z0-9.^~_-][\w.^~*+=/-]*$/.test(v) ? v : JSON.stringify(v); } /** Read all three ledgers of one profile. Never throws; every absent part is * simply undefined. */ export function readDshLedgerState(profileDir: string): DshLedgerState { const state: DshLedgerState = { spec: dshProfileDepSpec(profileDir), lockKind: "none" }; - const doc = readJsonFile(path.join(profileDir, "node_modules", DSH_PACKAGE, "package.json")) as { version?: unknown } | undefined; - if (typeof doc?.version === "string") state.copyVersion = doc.version; + const doc = readJsonFile(path.join(profileDir, "node_modules", DSH_PACKAGE, "package.json")) as + | { version?: unknown; dependencies?: unknown; optionalDependencies?: unknown } + | undefined; + if (typeof doc?.version === "string") { + state.copyVersion = doc.version; + state.copyDeps = readDepMaps(doc); + } const pnpm = readPnpmLockResolution(profileDir); if (pnpm !== undefined) { state.lockKind = "pnpm"; state.lockSpecifier = pnpm.specifier; state.lockVersion = pnpm.version; + state.lockStoreBroken = pnpm.storeBroken; return state; } const npm = readPackageLockResolution(profileDir); @@ -124,9 +140,35 @@ export function readDshLedgerState(profileDir: string): DshLedgerState { return state; } +/** The direct dep maps of a package.json doc. Only well-formed maps + * (object of name → string spec) are kept; a malformed doc yields undefined, + * which means "no cross-check" for the store sync — never a wrong one. */ +function readDepMaps(doc: { dependencies?: unknown; optionalDependencies?: unknown }): { dependencies?: Record; optionalDependencies?: Record } | undefined { + const out: { dependencies?: Record; optionalDependencies?: Record } = {}; + for (const key of ["dependencies", "optionalDependencies"] as const) { + const raw = doc[key]; + if (typeof raw !== "object" || raw === null) continue; + const map: Record = {}; + let wellFormed = true; + for (const [name, spec] of Object.entries(raw as Record)) { + if (typeof spec !== "string") { wellFormed = false; break; } + map[name] = spec; + } + if (wellFormed) out[key] = map; + } + return out.dependencies !== undefined || out.optionalDependencies !== undefined ? out : undefined; +} + interface LockResolution { specifier?: string; version?: string; + /** The recorded version value VERBATIM (peer suffix included) — the key + * the suffixed store entry (v9 snapshots:, v6–v8 packages:) carries. */ + versionRaw?: string; + /** #7125: the recorded version has no store-section entry (packages: / + * snapshots:) — the record was written without its resolution data. + * undefined = not judged (flat shape, no store sections, no version). */ + storeBroken?: boolean; } /** Read the lockfile's recorded resolution for billion-context. Handles the @@ -172,12 +214,36 @@ function readPnpmLockResolution(profileDir: string): LockResolution | undefined const spec = inner.match(/^\s*specifier:\s*(\S+)/); if (spec && out.specifier === undefined) out.specifier = unquote(spec[1]); const ver = inner.match(/^\s*version:\s*(\S+)/); - if (ver && out.version === undefined) out.version = splitPeerSuffix(unquote(ver[1]))[0]; + if (ver && out.version === undefined) { + out.version = splitPeerSuffix(unquote(ver[1]))[0]; + out.versionRaw = unquote(ver[1]); + } } } + if (out.version !== undefined) out.storeBroken = pnpmStoreBroken(lines, out.version, out.versionRaw ?? out.version); return out.specifier !== undefined || out.version !== undefined ? out : undefined; } +/** #7125: does the recorded version have its store-section entries? + * v9+ (packages: AND snapshots: present) — packages: carries the BARE key + * `billion-context@`, snapshots: the SUFFIXED key + * `billion-context@`; either missing = broken. v6–v8 (only + * packages:) — the key is `/billion-context/`; missing = broken. + * Flat ≤5.4 has no store sections → not judged (undefined). */ +function pnpmStoreBroken(lines: string[], version: string, versionRaw: string): boolean { + const sections = topLevelSections(lines); + const packages = sections.get("packages"); + const snapshots = sections.get("snapshots"); + if (snapshots !== undefined && packages !== undefined) { + return findStoreEntry(lines, packages, `billion-context@${version}`) === undefined + || findStoreEntry(lines, snapshots, `billion-context@${versionRaw}`) === undefined; + } + if (packages !== undefined) { + return findStoreEntry(lines, packages, `/billion-context/${versionRaw}`) === undefined; + } + return false; +} + /** npm-materialized profiles record the same two facts in package-lock.json. */ function readPackageLockResolution(profileDir: string): LockResolution | undefined { const doc = readJsonFile(path.join(profileDir, "package-lock.json")) as @@ -239,6 +305,16 @@ export function dshProfileLedgerFindings(profileDir: string, opts?: { includeDes detail: `${DSH_PACKAGE} copy v${copyVersion} does not match the lockfile resolution v${state.lockVersion} — the copy was written out-of-band (the host package manager never resolved it); ${remediation(profile)}`, }); } + if (state.lockStoreBroken === true) { + // #7125: the record and its resolution data disagree — invisible to + // the three-rung audit above (importer, copy and manifest all agree), + // but package-manager operations (update/remove of other plugins) + // hard-fail on it. + findings.push({ + profile, kind: "lockfile-internal", copyVersion, lockVersion: state.lockVersion, spec, + detail: `the lockfile records ${DSH_PACKAGE} v${state.lockVersion} but its store sections (packages:/snapshots:) lack that version's entries — the record was written without its resolution data; ${remediation(profile)}`, + }); + } if (state.lockSpecifier !== undefined && state.lockSpecifier !== spec) { findings.push({ profile, kind: "manifest-vs-lock", copyVersion, lockVersion: state.lockVersion, spec, @@ -360,14 +436,320 @@ function patchFlatEntries(lines: string[], spec: string, version: string): PnpmP return { kind: "ok", next: lines.join("\n"), details }; } -/** Rewrite the pnpm lockfile so it records (spec, version). Pure: returns the - * new text or a reason; never touches disk. */ -export function patchPnpmLock(text: string, spec: string, version: string): PnpmPatch { +/** Options for the store-section sync in patchPnpmLock (#7125). */ +export interface PnpmLockPatchOpts { + /** The NEW version's SRI integrity (from the registry doc). Required when + * a store entry must be created and its source carries an integrity — + * the source's hash is NEVER carried to the new key. Absent → the patch + * fails soft (zero writes) rather than writing a broken lockfile. */ + integrity?: string; + /** The on-disk copy's direct deps (its package.json) — cross-checked + * against the cloned store entry's dep map. Absent → the map is cloned + * as-is. */ + deps?: { dependencies?: Record; optionalDependencies?: Record }; +} + +type StoreSync = { kind: "ok"; details: LedgerChangeDetail[] } | { kind: "fail"; reason: string }; + +interface StoreEntry { start: number; end: number; inline: string | undefined; } + +/** Locate the store entry for `key` (a 2-space-indented `key:` line) inside + * [from, to). pnpm writes an entry either as a block (` key:` + indented + * children) or inline (` key: {}`); both are recognized — `inline` carries + * the inline value when present, else the block runs to the next 2-space key + * line or the section end. */ +function findStoreEntry(lines: string[], range: readonly [number, number], key: string): StoreEntry | undefined { + for (let i = range[0] + 1; i < range[1]; i++) { + const m = lines[i]!.match(/^ ([^\s:]+):([ \t]*.*)$/); + if (m === null || m[1] !== key) continue; + const rest = m[2].trim(); + if (rest !== "") return { start: i, end: i + 1, inline: rest }; + let end = range[1]; + for (let j = i + 1; j < range[1]; j++) { + if (/^ [^\s]/.test(lines[j]!)) { end = j; break; } + } + return { start: i, end, inline: undefined }; + } + return undefined; +} + +function versionDistance(a: string, b: string): number { + const num = (v: string): number => { + const m = v.match(/^(\d+)\.(\d+)\.(\d+)/); + if (m === null) return Number.NaN; + return Number(m[1]) * 10000 + Number(m[2]) * 100 + Number(m[3]); + }; + const na = num(a); + const nb = num(b); + if (Number.isNaN(na) || Number.isNaN(nb)) return Number.POSITIVE_INFINITY; + return Math.abs(na - nb); +} + +/** Pick the existing billion-context store entry to clone for `targetKey`: + * prefer the same peer suffix, then the closest major.minor.patch. Returns + * the entry's span plus its inline value (block entries have inline=undefined). */ +function pickSourceBlock(lines: string[], range: readonly [number, number], targetKey: string): { start: number; end: number; key: string; inline: string | undefined } | undefined { + const targetM = targetKey.match(/@?\/?(\d+\.\d+\.\d+)/); + const targetSuffix = targetKey.includes("(") ? targetKey.slice(targetKey.indexOf("(")) : ""; + let best: { start: number; end: number; key: string; dist: number; sameSuffix: boolean; inline: string | undefined } | undefined; + for (let i = range[0] + 1; i < range[1]; i++) { + const m = lines[i]!.match(/^ (billion-context@[^\s:]+|\/billion-context\/[^\s:]+):([ \t]*.*)$/); + if (m === null) continue; + const rest = m[2].trim(); + const inline = rest !== "" ? rest : undefined; + let end = range[1]; + if (inline === undefined) { + for (let j = i + 1; j < range[1]; j++) { + if (/^ [^\s]/.test(lines[j]!)) { end = j; break; } + } + } else { + end = i + 1; + } + const keyM = m[1].match(/@?\/?(\d+\.\d+\.\d+)/); + const dist = targetM !== null && keyM !== null ? versionDistance(targetM[1], keyM[1]) : Number.POSITIVE_INFINITY; + const sameSuffix = targetSuffix !== "" && m[1].endsWith(targetSuffix); + if (best === undefined || (sameSuffix && !best.sameSuffix) || (sameSuffix === best.sameSuffix && dist < best.dist)) { + best = { start: i, end, key: m[1], dist, sameSuffix, inline }; + } + } + return best; +} + +/** Rewrite the cloned store block's integrity to the NEW version's hash. The + * source's hash is NEVER carried to the new key (a wrong hash makes pnpm + * hard-fail verification). When the source carries an integrity and the new + * one is unknown, fail — a zero-write is strictly better than a broken + * lockfile. */ +function applyIntegrity(block: string[], integrity: string | undefined): { kind: "ok"; lines: string[] } | { kind: "fail"; reason: string } { + let resIdx = -1; + let resHead = ""; + let resRest = ""; + for (let i = 0; i < block.length; i++) { + const m = block[i]!.match(/^([ \t]+)resolution:[ \t]*(.*)$/); + if (m !== null) { resIdx = i; resHead = m[1]; resRest = m[2].trim(); break; } + } + const missingNewHash = (): { kind: "fail"; reason: string } => ({ kind: "fail", reason: "the source store entry carries an integrity the new version cannot reuse, and the new integrity is unknown — refusing to write a wrong hash (rerun with registry access, or let pnpm repair the lockfile)" }); + if (resIdx === -1) { + if (integrity === undefined) return { kind: "ok", lines: block }; + // No resolution line yet — add one at the entry's child indent (match + // an existing child, else the default 4 spaces under a 2-space key). + const firstChild = block.find((l) => l.trim() !== ""); + const ind = firstChild !== undefined ? firstChild.length - firstChild.trimStart().length : 4; + return { kind: "ok", lines: [`${" ".repeat(ind)}resolution: {integrity: ${yamlScalar(integrity)}}`, ...block] }; + } + if (resRest !== "") { + // Inline form: resolution: {integrity: "…"} + if (integrity === undefined) { + if (/integrity[ \t]*:/.test(resRest)) return missingNewHash(); + return { kind: "ok", lines: block }; + } + const replaced = resRest.replace(/integrity[ \t]*:[ \t]*("[^"]*"|'[^']*'|[^\s,}]+)/, `integrity: ${yamlScalar(integrity)}`); + if (replaced === resRest) return { kind: "fail", reason: "could not locate the integrity field in the cloned resolution line" }; + block[resIdx] = `${resHead}resolution: ${replaced}`; + return { kind: "ok", lines: block }; + } + // Block form: integrity: is a child of resolution: + const resInd = resHead.length; + let integIdx = -1; + for (let i = resIdx + 1; i < block.length; i++) { + const l = block[i]!; + if (l.trim() === "") continue; + const ind = l.length - l.trimStart().length; + if (ind <= resInd) break; + if (/^[ \t]*integrity:[ \t]*/.test(l)) { integIdx = i; break; } + } + if (integIdx === -1) { + if (integrity === undefined) return { kind: "ok", lines: block }; + block.splice(resIdx + 1, 0, `${resHead} integrity: ${yamlScalar(integrity)}`); + return { kind: "ok", lines: block }; + } + if (integrity === undefined) return missingNewHash(); + block[integIdx] = block[integIdx]!.replace(/integrity:[ \t]*.*$/, `integrity: ${yamlScalar(integrity)}`); + return { kind: "ok", lines: block }; +} + +/** Resolve a direct dep's version from the lockfile's store section: exactly + * one candidate → that version (peer suffix included); 0 or >1 → undefined + * (the caller fails soft). */ +function resolveDepFromStore(lines: string[], section: "packages" | "snapshots", name: string): string | undefined { + const sections = topLevelSections(lines); + const range = sections.get(section); + if (range === undefined) return undefined; + const esc = name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); + // Match BOTH store-entry forms: a block header (` name@v:`) and an inline + // value (` name@v: {}`) — a dep with no deps of its own is written inline, + // and its version still lives in the key. + const keyRe = section === "snapshots" + ? new RegExp(`^ ${esc}@([^\\s:]+):([ \\t].*)?$`) + : new RegExp(`^ /${esc}/([^\\s:]+):([ \\t].*)?$`); + const found: string[] = []; + for (let i = range[0] + 1; i < range[1]; i++) { + const m = lines[i]!.match(keyRe); + if (m !== null) found.push(m[1]); + } + return found.length === 1 ? found[0] : undefined; +} + +/** Cross-check the cloned store block's dep map against the on-disk copy's + * direct deps (the NEW version's authoritative declaration): declared deps + * missing from the map are resolved from the store (exactly one candidate — + * ambiguity fails soft); names no longer declared are dropped. */ +function crossCheckDepMaps(lines: string[], block: string[], section: "packages" | "snapshots", deps: PnpmLockPatchOpts["deps"]): { kind: "ok"; lines: string[] } | { kind: "fail"; reason: string } { + if (deps === undefined) return { kind: "ok", lines: block }; + const declared: Record = {}; + for (const name of Object.keys(deps.dependencies ?? {})) declared[name] = "dependencies"; + for (const name of Object.keys(deps.optionalDependencies ?? {})) declared[name] = "optionalDependencies"; + const headerIdx: Record = {}; + const entryIdx: Record> = {}; + let current: string | undefined; + let headerIndent = 0; + for (let i = 0; i < block.length; i++) { + const l = block[i]!; + if (l.trim() === "") continue; + const ind = l.length - l.trimStart().length; + const hm = l.match(/^([ \t]+)(dependencies|optionalDependencies):[ \t]*$/); + if (hm !== null) { + current = hm[2]; + headerIndent = hm[1].length; + headerIdx[current] = i; + entryIdx[current] = entryIdx[current] ?? {}; + continue; + } + if (current === undefined) continue; + if (ind <= headerIndent) { current = undefined; continue; } + const em = l.match(/^([ \t]+)([^\s:][^:]*):[ \t]*\S.*$/); + if (em !== null) entryIdx[current][em[2]] = i; + } + const out = block.slice(); + const surviving: Record = {}; + const drop: number[] = []; + for (const bucket of Object.keys(headerIdx)) { + for (const [name, idx] of Object.entries(entryIdx[bucket]!)) { + if (declared[name] === undefined || declared[name] !== bucket) drop.push(idx); + else surviving[name] = bucket; + } + } + for (const idx of drop.sort((a, b) => b - a)) out.splice(idx, 1); + for (const [name, bucket] of Object.entries(declared)) { + if (surviving[name] !== undefined) continue; + // Only fill a bucket the entry already carries — a store entry that + // has no dep map at all (v9 packages:) must not grow one; a dep a + // missing bucket would hold is left to pnpm's install-time repair. + const hIdx = out.findIndex((l) => new RegExp(`^[ \\t]+${bucket}:[ \\t]*$`).test(l)); + if (hIdx === -1) continue; + const value = resolveDepFromStore(lines, section, name); + if (value === undefined) { + return { kind: "fail", reason: `cannot resolve '${name}' for the cloned store entry — the lockfile's ${section} section has no unique entry for it; let pnpm repair the lockfile (install --fix-lockfile) and rerun` }; + } + const hInd = out[hIdx]!.length - out[hIdx]!.trimStart().length; + out.splice(hIdx + 1, 0, `${" ".repeat(hInd + 2)}${name}: ${value}`); + } + return { kind: "ok", lines: out }; +} + +/** Read the patched importer's version value VERBATIM (peer suffix included) + * from the first nested billion-context entry — the key the suffixed store + * entry (snapshots:) carries. */ +function readNestedVersionValue(lines: string[]): string | undefined { + const sections = topLevelSections(lines); + const candidates: string[] = []; + if (sections.has("importers")) candidates.push("importers"); + if (sections.has("dependencies")) candidates.push("dependencies"); + if (sections.has("devDependencies")) candidates.push("devDependencies"); + for (const sec of candidates) { + const range = sections.get(sec)!; + for (let i = range[0] + 1; i < range[1]; i++) { + const m = lines[i]!.match(PKG_ENTRY_RE); + if (m === null || m[3] !== undefined) continue; + const indent = m[1].length; + for (let j = i + 1; j < range[1]; j++) { + const inner = lines[j]!; + if (inner.trim() === "") continue; + const innerIndent = inner.match(/^[ \t]*/)?.[0].length ?? 0; + if (innerIndent <= indent) break; + const vm = inner.match(/^\s*version:\s*(\S+)/); + if (vm !== null) return unquote(vm[1]); + } + } + } + return undefined; +} + +/** Sync ONE store section so it holds an entry for `targetKey`: a present + * entry is left alone (zero writes); a missing one is cloned from the + * closest existing billion-context entry. `applyInt` rewrites the clone's + * integrity to the new version's hash (packages: only — snapshots: entries + * carry the dep map, not the hash); the dep map is cross-checked either way. */ +function syncStoreSection(lines: string[], section: "packages" | "snapshots", targetKey: string, opts: PnpmLockPatchOpts | undefined, applyInt: boolean): StoreSync { + const sections = topLevelSections(lines); + const range = sections.get(section); + if (range === undefined) return { kind: "ok", details: [] }; + if (findStoreEntry(lines, range, targetKey) !== undefined) return { kind: "ok", details: [] }; + const source = pickSourceBlock(lines, range, targetKey); + if (source === undefined) { + return { kind: "fail", reason: `pnpm-lock.yaml ${section}: has no billion-context entry to clone for ${targetKey} — let pnpm record the resolution (install) and rerun` }; + } + let newLines: string[]; + if (source.inline !== undefined) { + // An inline source (`key: {}`) has no children to rewrite — clone it + // verbatim under the new key. + newLines = [` ${targetKey}: ${source.inline}`]; + } else { + let block = lines.slice(source.start + 1, source.end); + if (applyInt) { + const integ = applyIntegrity(block, opts?.integrity); + if (integ.kind === "fail") return integ; + block = integ.lines; + } + const deps = crossCheckDepMaps(lines, block, section, opts?.deps); + if (deps.kind === "fail") return deps; + newLines = [` ${targetKey}:`, ...deps.lines]; + } + lines.splice(source.end, 0, ...newLines); + return { kind: "ok", details: [{ file: "pnpm-lock.yaml", field: `${section}: ${targetKey}`, before: "(missing)", after: `cloned from ${source.key}` }] }; +} + +/** #7125: after the importer record moves to `version`, the store sections + * must hold that version's entries too — an importer pointing at a version + * with no packages:/snapshots: entry is an internally inconsistent lockfile + * (pnpm 11 `remove` hard-fails, pnpm ≥12 dangles a symlink — #3570). */ +function syncPnpmStoreEntries(lines: string[], version: string, versionRaw: string | undefined, opts: PnpmLockPatchOpts | undefined): StoreSync { + const details: LedgerChangeDetail[] = []; + const raw = versionRaw ?? version; + // snapshots: carry the dep map, NOT the integrity (it lives in packages:). + const snap = syncStoreSection(lines, "snapshots", `billion-context@${raw}`, opts, false); + if (snap.kind === "fail") return snap; + details.push(...snap.details); + const pkg = syncStoreSection(lines, "packages", `billion-context@${version}`, opts, true); + if (pkg.kind === "fail") return pkg; + details.push(...pkg.details); + return { kind: "ok", details }; +} + +function syncPnpmStoreEntriesV6(lines: string[], version: string, versionRaw: string | undefined, opts: PnpmLockPatchOpts | undefined): StoreSync { + const raw = versionRaw ?? version; + return syncStoreSection(lines, "packages", `/billion-context/${raw}`, opts, true); +} + +/** Rewrite the pnpm lockfile so it records (spec, version). When `opts` is + * given, the store sections (packages:/snapshots:) are synced to hold that + * version's entries too (#7125) — the importer record and its resolution + * data move together; without them the lockfile is internally inconsistent + * and package-manager operations on it hard-fail. Pure: returns the new text + * or a reason; never touches disk. */ +export function patchPnpmLock(text: string, spec: string, version: string, opts?: PnpmLockPatchOpts): PnpmPatch { const lines = text.split("\n"); const sections = topLevelSections(lines); if (sections.has("importers")) { const range = sections.get("importers")!; - return patchNestedEntries(lines, range[0], range[1], spec, version); + const r = patchNestedEntries(lines, range[0], range[1], spec, version); + if (r.kind === "fail") return r; + if (opts !== undefined) { + const store = syncPnpmStoreEntries(lines, version, readNestedVersionValue(lines), opts); + if (store.kind === "fail") return store; + return { kind: "ok", next: lines.join("\n"), details: [...r.details, ...store.details] }; + } + return r; } if (sections.has("specifiers")) { return patchFlatEntries(lines, spec, version); @@ -391,6 +773,11 @@ export function patchPnpmLock(text: string, spec: string, version: string): Pnpm patched++; } if (patched === 0) return { kind: "fail", reason: "no billion-context entry found in the lockfile — run 'dsh plugin --profile add billion-context' once so the package manager records the resolution, then retry" }; + if (opts !== undefined) { + const store = syncPnpmStoreEntriesV6(lines, version, readNestedVersionValue(lines), opts); + if (store.kind === "fail") return store; + return { kind: "ok", next: lines.join("\n"), details: [...details, ...store.details] }; + } return { kind: "ok", next: lines.join("\n"), details }; } return { kind: "fail", reason: "lockfile shape not recognized (no importers/specifiers/dependencies sections)" }; @@ -474,8 +861,14 @@ function atomicWriteText(file: string, content: string): string | undefined { * papered over — syncing the lockfile to the copy under such a spec would * produce a record pnpm itself calls unsatisfiable, i.e. a wedge wearing a * green badge. The finding stays visible; the user aligns the declaration. + * + * `opts.integrity` (#7125): the target version's registry SRI hash. When + * given, the pnpm store sections are synced so the new importer record has + * its resolution data; a version move that CANNOT be completed safely (new + * hash unknown while the old entry carries one) fails with a reason and + * ZERO writes instead of leaving an inconsistent lockfile behind. */ -export function convergeDshLedger(profileDir: string, targetVersion: string): LedgerConvergenceResult { +export function convergeDshLedger(profileDir: string, targetVersion: string, opts?: { integrity?: string }): LedgerConvergenceResult { const notes: string[] = []; const state = readDshLedgerState(profileDir); const fail = (error: string): LedgerConvergenceResult => ({ changed: [], details: [], notes, error }); @@ -524,7 +917,9 @@ export function convergeDshLedger(profileDir: string, targetVersion: string): Le } catch { return fail(`lockfile ${lockFile} vanished between read and patch — retry`); } - const patch = state.lockKind === "pnpm" ? patchPnpmLock(raw, finalSpec, targetVersion) : patchNpmLock(raw, finalSpec, targetVersion); + const patch = state.lockKind === "pnpm" + ? patchPnpmLock(raw, finalSpec, targetVersion, { integrity: opts?.integrity, deps: state.copyDeps }) + : patchNpmLock(raw, finalSpec, targetVersion); if (patch.kind === "fail") return fail(patch.reason); // Zero-writes contract: an already-in-step record produces NO write — // not even a byte-identical re-serialization (mtime churn on a diff --git a/src/update.ts b/src/update.ts index 6393774f3c..c3f1f0332a 100644 --- a/src/update.ts +++ b/src/update.ts @@ -2196,7 +2196,7 @@ export async function installViaTarball( // the install (the copy itself is verified and standing). if (isDshDesktopBiliCopy(installDir, real, env)) { const profileDir = path.join(resolveDshHome(env), "profiles", DSH_DESKTOP_PROFILE); - const conv = convergeDshLedger(profileDir, version); + const conv = convergeDshLedger(profileDir, version, { integrity }); if (conv.error !== undefined) { loggerLog("warn", `[update] dsh ${DSH_DESKTOP_PROFILE} profile copy replaced at ${version} but its ledgers were not converged (${conv.error}) — dsh may revert the copy on its next plugin install; rerun 'bili doctor --fix'`); } else if (conv.changed.length > 0) { @@ -2233,8 +2233,10 @@ export async function detectStaleInstall( /** Fetch one published version's registry doc (#1481): tarball URL plus * integrity/shasum for verification. Returns undefined when the version does - * not exist or the fetch fails — callers treat that as "do nothing". */ -async function fetchVersionDoc( + * not exist or the fetch fails — callers treat that as "do nothing". Also + * used by `bili doctor --fix` (#7125) to source the SRI hash a pnpm store + * entry needs when converging a dsh profile lockfile. */ +export async function fetchVersionDoc( opts: Pick, packageName: string, version: string, diff --git a/tests/issue3496-dsh-ledger-convergence.test.ts b/tests/issue3496-dsh-ledger-convergence.test.ts index dd7de54a36..39a346c82d 100644 --- a/tests/issue3496-dsh-ledger-convergence.test.ts +++ b/tests/issue3496-dsh-ledger-convergence.test.ts @@ -34,6 +34,9 @@ interface ProfileOpts { spec?: string; /** On-disk bundle copy version; undefined = no readable copy. */ copyVersion?: string; + /** The on-disk copy's own direct deps (its package.json) — #7125 dep-map + * cross-check. Absent = the copy declares no deps (cross-check skipped). */ + copyDeps?: { dependencies?: Record; optionalDependencies?: Record }; pnpmLock?: string; npmLock?: unknown; /** Raw package.json content override (corrupt-file cases). */ @@ -53,7 +56,10 @@ function makeProfile(root: string, name: string, opts: ProfileOpts): string { if (opts.copyVersion !== undefined) { const copyDir = path.join(dir, "node_modules", "billion-context"); mkdirSync(copyDir, { recursive: true }); - writeFileSync(path.join(copyDir, "package.json"), JSON.stringify({ name: "billion-context", version: opts.copyVersion }) + "\n"); + const copyDoc: Record = { name: "billion-context", version: opts.copyVersion }; + if (opts.copyDeps?.dependencies !== undefined) copyDoc.dependencies = opts.copyDeps.dependencies; + if (opts.copyDeps?.optionalDependencies !== undefined) copyDoc.optionalDependencies = opts.copyDeps.optionalDependencies; + writeFileSync(path.join(copyDir, "package.json"), JSON.stringify(copyDoc, null, 2) + "\n"); } if (opts.pnpmLock !== undefined) writeFileSync(path.join(dir, "pnpm-lock.yaml"), opts.pnpmLock); if (opts.npmLock !== undefined) writeFileSync(path.join(dir, "package-lock.json"), JSON.stringify(opts.npmLock, null, 2) + "\n"); @@ -243,17 +249,22 @@ test("#3496: post-self-heal wedge (stale lock record) converges the lockfile, ma const kinds = dshProfileLedgerFindings(dir).map((f) => f.kind); assert.deepEqual(kinds, ["copy-vs-lock", "manifest-vs-lock"]); const before = profileFiles(dir); - const res = convergeDshLedger(dir, "0.1.197"); + const res = convergeDshLedger(dir, "0.1.197", { integrity: "sha512-new==" }); assert.equal(res.error, undefined); assert.deepEqual(res.changed, ["pnpm-lock.yaml"]); assert.deepEqual(res.details.map((d) => [d.field, d.before, d.after]), [ ["specifier", "^0.1.196", "^0.1.197"], ["version", "0.1.196", "0.1.197"], + ["snapshots: billion-context@0.1.197", "(missing)", "cloned from billion-context@0.1.196"], + ["packages: billion-context@0.1.197", "(missing)", "cloned from billion-context@0.1.196"], ]); const lock = readFileSync(path.join(dir, "pnpm-lock.yaml"), "utf8"); assert.match(lock, /specifier: \^0\.1\.197/); assert.match(lock, /version: 0\.1\.197/); - assert.ok(lock.includes("billion-context@0.1.196:"), "store bookkeeping sections stay untouched — pnpm reconciles them itself"); + assert.ok(lock.includes(" billion-context@0.1.196:"), "old packages entry is kept"); + assert.ok(lock.includes(" billion-context@0.1.196: {}"), "old snapshots entry is kept"); + assert.ok(lock.includes(" billion-context@0.1.197: {}"), "new snapshots entry resolves the importer"); + assert.match(lock, / billion-context@0\.1\.197:\n resolution: \{integrity: sha512-new==\}/, "new packages entry carries the new hash"); assert.equal(readFileSync(path.join(dir, "package.json"), "utf8"), before["package.json"], "in-step manifest must not be rewritten"); assert.deepEqual(dshProfileLedgerFindings(dir), [], "re-audit after convergence must be clean"); } finally { @@ -270,12 +281,13 @@ test("#3496: #3160 raw shape — phantom spec + hand-placed copy converge to the const dir = makeProfile(root, "phantom", { spec: "^1.2.9", copyVersion: "1.2.9", pnpmLock: pnpmLockV9("^0.1.196", "0.1.196") }); const kinds = dshProfileLedgerFindings(dir).map((f) => f.kind).sort(); assert.deepEqual(kinds, ["copy-vs-lock", "manifest-vs-lock"]); - const res = convergeDshLedger(dir, "1.2.9"); + const res = convergeDshLedger(dir, "1.2.9", { integrity: "sha512-new==" }); assert.equal(res.error, undefined); assert.deepEqual(res.changed, ["pnpm-lock.yaml"], "the declaration ADMITS the copy — bili keeps it, converges the record"); const lock = readFileSync(path.join(dir, "pnpm-lock.yaml"), "utf8"); assert.match(lock, /specifier: \^1\.2\.9/); assert.match(lock, /version: 1\.2\.9/); + assert.ok(lock.includes(" billion-context@1.2.9: {}"), "the phantom version's snapshots entry is still recorded"); assert.deepEqual(dshProfileLedgerFindings(dir), [], "ledgers agree — the residual risk (a version the registry lacks) is reported, see the --fix output contract"); } finally { rmrf(root); @@ -289,7 +301,7 @@ test("#3496: phantom spec + REAL newer copy — manifest AND lockfile converge ( const dir = makeProfile(root, "full", { spec: "^1.2.9", copyVersion: "0.1.197", pnpmLock: pnpmLockV9("^0.1.196", "0.1.196") }); // copy-vs-manifest is the no-lockfile rung — with a lock present, two rungs fire. assert.deepEqual(dshProfileLedgerFindings(dir).map((f) => f.kind), ["copy-vs-lock", "manifest-vs-lock"]); - const res = convergeDshLedger(dir, "0.1.197"); + const res = convergeDshLedger(dir, "0.1.197", { integrity: "sha512-new==" }); assert.equal(res.error, undefined); assert.deepEqual([...res.changed].sort(), ["package.json", "pnpm-lock.yaml"]); const manifest = JSON.parse(readFileSync(path.join(dir, "package.json"), "utf8")) as { dependencies: Record }; @@ -354,27 +366,30 @@ test("#3496: peer-suffixed lock version — real drift swaps the version part on const findings = dshProfileLedgerFindings(dir); assert.deepEqual(findings.map((f) => f.kind), ["copy-vs-lock", "manifest-vs-lock"]); assert.equal(findings[0]!.lockVersion, "0.1.196", "findings name the version, not the peer set"); - const before = readFileSync(path.join(dir, "pnpm-lock.yaml"), "utf8"); - const res = convergeDshLedger(dir, "0.1.197"); + const res = convergeDshLedger(dir, "0.1.197", { integrity: "sha512-new==" }); assert.equal(res.error, undefined); assert.deepEqual(res.changed, ["pnpm-lock.yaml"]); assert.deepEqual(res.details.map((d) => [d.field, d.before, d.after]), [ ["specifier", "^0.1.196", "^0.1.197"], ["version", "0.1.196(@types/node@16.9.1)", "0.1.197(@types/node@16.9.1)"], + ["snapshots: billion-context@0.1.197(@types/node@16.9.1)", "(missing)", "cloned from billion-context@0.1.196(@types/node@16.9.1)"], + ["packages: billion-context@0.1.197", "(missing)", "cloned from billion-context@0.1.196"], ]); const after = readFileSync(path.join(dir, "pnpm-lock.yaml"), "utf8"); - // Exactly the two billion-context importer lines move; the version line - // stays bare (pnpm's own style — no quoting churn) and keeps its suffix. - const expected = before - .replace(" specifier: ^0.1.196", " specifier: ^0.1.197") - .replace(" version: 0.1.196(@types/node@16.9.1)", " version: 0.1.197(@types/node@16.9.1)"); - assert.equal(after, expected); + // The two billion-context importer lines move (the version keeps its + // peer suffix); the store sections gain the new version's entries so + // the importer resolves — the old entries are kept. + assert.match(after, /specifier: \^0\.1\.197/); + assert.match(after, /version: 0\.1\.197\(@types\/node@16\.9\.1\)/); + assert.ok(after.includes(" billion-context@0.1.197(@types/node@16.9.1):"), "new suffixed snapshots entry"); + assert.match(after, / billion-context@0\.1\.197:\n resolution: \{integrity: sha512-new==\}/, "new bare packages entry carries the new hash"); + assert.ok(after.includes(" billion-context@0.1.196(@types/node@16.9.1):"), "old suffixed snapshots entry is kept"); + assert.ok(after.includes(" billion-context@0.1.196:"), "old bare packages entry is kept"); assert.ok(after.includes(" version: 1.2.3(@types/node@16.9.1)"), "another dependency's suffixed version is not ours to touch"); - assert.ok(after.includes(" billion-context@0.1.196(@types/node@16.9.1):"), "store bookkeeping sections stay untouched (#3570)"); assert.deepEqual(dshProfileLedgerFindings(dir), [], "re-audit after convergence must be clean"); // Second pass is a no-op: the suffixed record is already in step. const mt = profileMtimes(dir); - const again = convergeDshLedger(dir, "0.1.197"); + const again = convergeDshLedger(dir, "0.1.197", { integrity: "sha512-new==" }); assert.deepEqual(again.changed, []); assertZeroWrites(dir, profileFiles(dir), mt); } finally { @@ -433,12 +448,13 @@ test("#3496: pnpm v6–v8 top-level nested lockfile converges", () => { const root = mkdtempSync(path.join(tmpdir(), "bc-i3496-f-")); try { const dir = makeProfile(root, "v6", { spec: "^1.2.9", copyVersion: "0.1.197", pnpmLock: pnpmLockV6("^0.1.196", "0.1.196") }); - const res = convergeDshLedger(dir, "0.1.197"); + const res = convergeDshLedger(dir, "0.1.197", { integrity: "sha512-new==" }); assert.equal(res.error, undefined); const lock = readFileSync(path.join(dir, "pnpm-lock.yaml"), "utf8"); assert.match(lock, /specifier: \^0\.1\.197/); assert.match(lock, /version: 0\.1\.197/); - assert.match(lock, /\/billion-context\/0\.1\.196:/, "packages map untouched"); + assert.match(lock, /\/billion-context\/0\.1\.196:/, "old packages entry is kept"); + assert.match(lock, /\/billion-context\/0\.1\.197:\n resolution: \{integrity: sha512-new==\}/, "new packages entry carries the new hash"); assert.deepEqual(dshProfileLedgerFindings(dir), []); } finally { rmrf(root); @@ -492,7 +508,7 @@ test("#3496: dist-tag spec rides along — declaration kept, lockfile converges" try { const dir = makeProfile(root, "disttag", { spec: "latest", copyVersion: "0.1.197", pnpmLock: pnpmLockV9("^0.1.196", "0.1.196") }); const before = profileFiles(dir); - const res = convergeDshLedger(dir, "0.1.197"); + const res = convergeDshLedger(dir, "0.1.197", { integrity: "sha512-new==" }); assert.equal(res.error, undefined); assert.deepEqual(res.changed, ["pnpm-lock.yaml"]); assert.equal(readFileSync(path.join(dir, "package.json"), "utf8"), before["package.json"], "'latest' is the user's declaration — never rewritten"); @@ -510,7 +526,7 @@ test("#3496: admitting range spec — kept, lockfile converges, quoted YAML roun const root = mkdtempSync(path.join(tmpdir(), "bc-i3496-j-")); try { const dir = makeProfile(root, "range", { spec: ">=0.1.0", copyVersion: "0.1.197", pnpmLock: pnpmLockV9("^0.1.196", "0.1.196") }); - const res = convergeDshLedger(dir, "0.1.197"); + const res = convergeDshLedger(dir, "0.1.197", { integrity: "sha512-new==" }); assert.equal(res.error, undefined); const lock = readFileSync(path.join(dir, "pnpm-lock.yaml"), "utf8"); assert.match(lock, /specifier: ">=0\.1\.0"/, "leading '>' is a YAML block indicator — must be quoted"); @@ -526,7 +542,7 @@ test("#3496: bare `*` spec — quoted in the lockfile (YAML alias indicator), ro const root = mkdtempSync(path.join(tmpdir(), "bc-i3496-u-")); try { const dir = makeProfile(root, "star", { spec: "*", copyVersion: "0.1.197", pnpmLock: pnpmLockV9("^0.1.196", "0.1.196") }); - const res = convergeDshLedger(dir, "0.1.197"); + const res = convergeDshLedger(dir, "0.1.197", { integrity: "sha512-new==" }); assert.equal(res.error, undefined); const lock = readFileSync(path.join(dir, "pnpm-lock.yaml"), "utf8"); assert.match(lock, /specifier: "\*"/, "a leading '*' would parse as a YAML alias token — must be quoted"); @@ -558,7 +574,7 @@ test("#3496: exact-style spec keeps exact style when converged", () => { const root = mkdtempSync(path.join(tmpdir(), "bc-i3496-l-")); try { const dir = makeProfile(root, "exact", { spec: "0.1.190", copyVersion: "0.1.197", pnpmLock: pnpmLockV9("0.1.190", "0.1.190") }); - const res = convergeDshLedger(dir, "0.1.197"); + const res = convergeDshLedger(dir, "0.1.197", { integrity: "sha512-new==" }); assert.equal(res.error, undefined); const manifest = JSON.parse(readFileSync(path.join(dir, "package.json"), "utf8")) as { dependencies: Record }; assert.equal(manifest.dependencies["billion-context"], "0.1.197", "no caret invented where the user declared none"); @@ -629,6 +645,182 @@ test("#3496: desktop gating — detection excludes by default, includeDesktop re } }); +// — #7125: the store sections must move with the importer record —————————————————— + +test("#7125: broken lockfile on the desktop profile (importer moved, store not) — invisible to passive detection, repaired by --fix", () => { + const root = mkdtempSync(path.join(tmpdir(), "bc-i7125-a-")); + try { + // The exact #7125 shape: the importer record points at 0.1.197 but the + // store sections still only hold 0.1.196 — what the pre-fix auto-update + // left behind. + const brokenLock = [ + "lockfileVersion: '9.0'", + "", + "settings:", + " autoInstallPeers: true", + " excludeLinksFromLockfile: false", + "", + "importers:", + "", + " .:", + " dependencies:", + " billion-context:", + " specifier: ^0.1.197", + " version: 0.1.197", + " other-dep:", + " specifier: ^1.2.3", + " version: 1.2.3", + "", + "packages:", + "", + " billion-context@0.1.196:", + " resolution: {integrity: sha512-fake==}", + "", + "snapshots:", + "", + " billion-context@0.1.196: {}", + "", + ].join("\n"); + const dir = makeProfile(root, "desktop", { spec: "^0.1.197", copyVersion: "0.1.197", pnpmLock: brokenLock }); + // The record and its resolution data disagree — detected by state read… + const state = readDshLedgerState(dir); + assert.equal(state.lockVersion, "0.1.197"); + assert.equal(state.lockStoreBroken, true, "the store sections lack the recorded version's entries"); + // …and by the audit, but ONLY when the desktop profile is included + // (#3224 option A: the in-place writer's lane stays out of passive detection). + assert.deepEqual(dshProfileLedgerFindings(dir), []); + assert.deepEqual(dshProfileLedgerFindings(dir, { includeDesktop: true }).map((f) => f.kind), ["lockfile-internal"]); + // Converging repairs the store WITHOUT moving the importer (already at the target). + const res = convergeDshLedger(dir, "0.1.197", { integrity: "sha512-new==" }); + assert.equal(res.error, undefined); + assert.deepEqual(res.changed, ["pnpm-lock.yaml"], "only the lockfile changes — manifest and importer are already in step"); + assert.ok(res.details.some((d) => d.field === "snapshots: billion-context@0.1.197" && d.before === "(missing)")); + assert.ok(res.details.some((d) => d.field === "packages: billion-context@0.1.197" && d.before === "(missing)")); + assert.ok(!res.details.some((d) => d.field === "specifier" || d.field === "version"), "the importer is untouched"); + const lock = readFileSync(path.join(dir, "pnpm-lock.yaml"), "utf8"); + assert.match(lock, /billion-context@0\.1\.197:\n resolution: \{integrity: sha512-new==\}/); + assert.match(lock, /billion-context@0\.1\.197: \{\}/); + assert.match(lock, /billion-context@0\.1\.196:/, "the old store entry is kept"); + // Re-audit (includeDesktop) is clean: the record and its resolution data now agree. + assert.deepEqual(dshProfileLedgerFindings(dir, { includeDesktop: true }), []); + } finally { + rmrf(root); + rmrf(__cacheRoot); + } +}); + +test("#7125: converge without the new integrity fails soft when the source entry carries one — zero writes", () => { + const root = mkdtempSync(path.join(tmpdir(), "bc-i7125-b-")); + try { + const dir = makeProfile(root, "nohash", { spec: "^0.1.196", copyVersion: "0.1.197", pnpmLock: pnpmLockV9("^0.1.196", "0.1.196") }); + const before = profileFiles(dir); + const beforeMt = profileMtimes(dir); + const res = convergeDshLedger(dir, "0.1.197"); // no integrity + assert.ok(res.error !== undefined, "the source entry's hash cannot be reused for the new version"); + assert.ok(res.error!.includes("integrity"), "the failure names the missing integrity"); + assertZeroWrites(dir, before, beforeMt); + } finally { + rmrf(root); + rmrf(__cacheRoot); + } +}); + +test("#7125: dep-map cross-check — declared dep resolved from the store, undeclared dep dropped", () => { + const root = mkdtempSync(path.join(tmpdir(), "bc-i7125-c-")); + try { + // The 0.1.196 snapshots entry carries a dep map (other-dep); the on-disk + // copy (0.1.197) declares some-dep instead. The store holds a unique + // some-dep entry to resolve it from. + const lock = [ + "lockfileVersion: '9.0'", + "", + "settings:", + " autoInstallPeers: true", + " excludeLinksFromLockfile: false", + "", + "importers:", + "", + " .:", + " dependencies:", + " billion-context:", + " specifier: ^0.1.196", + " version: 0.1.196", + "", + "packages:", + "", + " billion-context@0.1.196:", + " resolution: {integrity: sha512-fake==}", + "", + "snapshots:", + "", + " billion-context@0.1.196:", + " dependencies:", + " other-dep: 1.2.3", + "", + " some-dep@1.0.0: {}", + "", + ].join("\n"); + const dir = makeProfile(root, "deps", { spec: "^0.1.196", copyVersion: "0.1.197", pnpmLock: lock, copyDeps: { dependencies: { "some-dep": "^1.0.0" } } }); + const res = convergeDshLedger(dir, "0.1.197", { integrity: "sha512-new==" }); + assert.equal(res.error, undefined); + const out = readFileSync(path.join(dir, "pnpm-lock.yaml"), "utf8"); + const count = (s: string, sub: string): number => s.split(sub).length - 1; + assert.equal(count(out, "other-dep: 1.2.3"), 1, "the undeclared dep is dropped from the cloned entry (kept only in the old one)"); + assert.equal(count(out, "some-dep: 1.0.0"), 1, "the declared dep is resolved from the store and added once"); + assert.match(out, /billion-context@0\.1\.197:\n dependencies:\n some-dep: 1\.0\.0/); + } finally { + rmrf(root); + rmrf(__cacheRoot); + } +}); + +test("#7125: dep-map cross-check — an ambiguous store entry fails soft, zero writes", () => { + const root = mkdtempSync(path.join(tmpdir(), "bc-i7125-d-")); + try { + const lock = [ + "lockfileVersion: '9.0'", + "", + "settings:", + " autoInstallPeers: true", + " excludeLinksFromLockfile: false", + "", + "importers:", + "", + " .:", + " dependencies:", + " billion-context:", + " specifier: ^0.1.196", + " version: 0.1.196", + "", + "packages:", + "", + " billion-context@0.1.196:", + " resolution: {integrity: sha512-fake==}", + "", + "snapshots:", + "", + " billion-context@0.1.196:", + " dependencies:", + " other-dep: 1.2.3", + "", + " some-dep@1.0.0: {}", + "", + " some-dep@2.0.0: {}", + "", + ].join("\n"); + const dir = makeProfile(root, "ambig", { spec: "^0.1.196", copyVersion: "0.1.197", pnpmLock: lock, copyDeps: { dependencies: { "some-dep": "^1.0.0" } } }); + const before = profileFiles(dir); + const beforeMt = profileMtimes(dir); + const res = convergeDshLedger(dir, "0.1.197", { integrity: "sha512-new==" }); + assert.ok(res.error !== undefined, "two store candidates for some-dep — cannot pick one"); + assert.ok(res.error!.includes("some-dep")); + assertZeroWrites(dir, before, beforeMt); + } finally { + rmrf(root); + rmrf(__cacheRoot); + } +}); + // — pure patchers ———————————————————————————————————————————————————————————————— test("#3496: patchPnpmLock structural failures — no partial result", () => { @@ -674,7 +866,7 @@ test("#3496: specAdmits — caret/tilde/exact, loose ^0.x, unjudgeable stays sil // — bili doctor --fix surface ——————————————————————————————————————————————————————— -test("#3496: fixDshLedgers walks every profile, fixes only the drifted, leaves the rest byte-identical", () => { +test("#3496: fixDshLedgers walks every profile, fixes only the drifted, leaves the rest byte-identical", async () => { const root = mkdtempSync(path.join(tmpdir(), "bc-i3496-q-")); try { const wedged = makeProfile(root, "wedged", { spec: "^1.2.9", copyVersion: "0.1.197", pnpmLock: pnpmLockV9("^0.1.196", "0.1.196") }); @@ -685,11 +877,12 @@ test("#3496: fixDshLedgers walks every profile, fixes only the drifted, leaves t const beforeHealthyMt = profileMtimes(healthy); const beforePinnedMt = profileMtimes(pinned); - const rows = fixDshLedgers({ ...process.env, DSH_HOME: root }); + const rows = await fixDshLedgers({ ...process.env, DSH_HOME: root }, { fetchVersionDoc: async () => ({ integrity: "sha512-test==" }) }); assert.deepEqual(rows.map((r) => r.profile), ["wedged"]); const row = rows[0]!; assert.deepEqual([...row.changed].sort(), ["package.json", "pnpm-lock.yaml"]); assert.ok(row.details.some((d) => d.file === "package.json" && d.before === "^1.2.9" && d.after === "^0.1.197")); + assert.ok(row.details.some((d) => d.field === "packages: billion-context@0.1.197"), "the store section is synced so the importer resolves (#7125)"); assert.deepEqual(row.residualFindings, [], "re-verification after the fix must be clean"); assert.equal(row.error, undefined); assertZeroWrites(healthy, beforeHealthy, beforeHealthyMt); @@ -701,11 +894,11 @@ test("#3496: fixDshLedgers walks every profile, fixes only the drifted, leaves t } }); -test("#3496: fixDshLedgers refuses (with a visible reason) when the spec is outside rewrite authority", () => { +test("#3496: fixDshLedgers refuses (with a visible reason) when the spec is outside rewrite authority", async () => { const root = mkdtempSync(path.join(tmpdir(), "bc-i3496-r-")); try { makeProfile(root, "tilde", { spec: "~1.2.9", copyVersion: "0.1.197", pnpmLock: pnpmLockV9("^0.1.196", "0.1.196") }); - const rows = fixDshLedgers({ ...process.env, DSH_HOME: root }); + const rows = await fixDshLedgers({ ...process.env, DSH_HOME: root }, { fetchVersionDoc: async () => ({ integrity: "sha512-test==" }) }); assert.equal(rows.length, 1); assert.ok(rows[0]!.error?.includes("rewrite authority")); assert.ok(rows[0]!.residualFindings.length > 0, "the drift stays reported until the human aligns the declaration"); @@ -715,24 +908,24 @@ test("#3496: fixDshLedgers refuses (with a visible reason) when the spec is outs } }); -test("#3496: fixDshLedgers fail-soft — dsh home absent or empty yields no rows", () => { +test("#3496: fixDshLedgers fail-soft — dsh home absent or empty yields no rows", async () => { const root = mkdtempSync(path.join(tmpdir(), "bc-i3496-s-")); try { - assert.deepEqual(fixDshLedgers({ ...process.env, DSH_HOME: path.join(root, "never-existed") }), []); + assert.deepEqual(await fixDshLedgers({ ...process.env, DSH_HOME: path.join(root, "never-existed") }), []); mkdirSync(path.join(root, "empty-home", "profiles"), { recursive: true }); - assert.deepEqual(fixDshLedgers({ ...process.env, DSH_HOME: path.join(root, "empty-home") }), []); + assert.deepEqual(await fixDshLedgers({ ...process.env, DSH_HOME: path.join(root, "empty-home") }), []); } finally { rmrf(root); rmrf(__cacheRoot); } }); -test("#3496: renderLedgerFixReport prints every change and never claims false success", () => { +test("#3496: renderLedgerFixReport prints every change and never claims false success", async () => { const root = mkdtempSync(path.join(tmpdir(), "bc-i3496-t-")); try { assert.match(renderLedgerFixReport([]), /nothing written/); makeProfile(root, "wedged", { spec: "^1.2.9", copyVersion: "0.1.197", pnpmLock: pnpmLockV9("^0.1.196", "0.1.196") }); - const rows = fixDshLedgers({ ...process.env, DSH_HOME: root }); + const rows = await fixDshLedgers({ ...process.env, DSH_HOME: root }, { fetchVersionDoc: async () => ({ integrity: "sha512-test==" }) }); const text = renderLedgerFixReport(rows); assert.match(text, /wedged/); assert.match(text, /converged/); From 74aaa0e0909ee1c0ee56b92157f6eb24fe7de59e Mon Sep 17 00:00:00 2001 From: ci-check Date: Wed, 18 Nov 2026 17:12:27 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix(dsh-ledger):=20PnpmLockPatchOpts=20is?= =?UTF-8?q?=20internal=20=E2=80=94=20drop=20the=20export=20(unused-exports?= =?UTF-8?q?=20gate)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/dsh-ledger.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dsh-ledger.ts b/src/dsh-ledger.ts index b79de44a4a..df8f1ce8b8 100644 --- a/src/dsh-ledger.ts +++ b/src/dsh-ledger.ts @@ -437,7 +437,7 @@ function patchFlatEntries(lines: string[], spec: string, version: string): PnpmP } /** Options for the store-section sync in patchPnpmLock (#7125). */ -export interface PnpmLockPatchOpts { +interface PnpmLockPatchOpts { /** The NEW version's SRI integrity (from the registry doc). Required when * a store entry must be created and its source carries an integrity — * the source's hash is NEVER carried to the new key. Absent → the patch From 7ea607712e7665640c5df8b0f0f86f0ad7a66fb5 Mon Sep 17 00:00:00 2001 From: ci-check Date: Wed, 18 Nov 2026 18:34:16 +0800 Subject: [PATCH 3/3] fix(dsh-ledger): close the two #7125 fail-soft gaps found in review The store-section sync cloned the SOURCE version's resolution data verbatim: when a dependency's declared spec changed between versions (zod 4.1.8 -> exact 4.6.7) without the name or bucket moving, the clone recorded a resolution the new manifest contradicts, and a frozen install would faithfully install the wrong dep. crossCheckDepMaps now checks every surviving entry's resolved version against the on-disk copy's declared spec (semver; unjudgeable specs stay silent, same discipline as specAdmits) and fails soft on a proven contradiction. And when that fail-soft (or any convergence failure) leaves the ledgers on the old version while the copy stands at the new one, installViaTarball had already destroyed the displaced pnpm link - the copy's only rollback source. The link is now removed only after a non-divergent convergence; a divergent one keeps it and names it in the warn log. LedgerConvergenceResult gains a 'divergent' flag (set on error paths where at least one ledger exists) so the caller can tell 'nothing to converge' (link removed as before) from 'ledgers left behind' (link kept). --- src/dsh-ledger.ts | 51 +++++- src/update.ts | 25 +-- .../issue3496-dsh-ledger-convergence.test.ts | 141 ++++++++++++++++ tests/update-desktop-inplace.test.ts | 153 ++++++++++++++++++ 4 files changed, 354 insertions(+), 16 deletions(-) diff --git a/src/dsh-ledger.ts b/src/dsh-ledger.ts index df8f1ce8b8..08eeb6e37c 100644 --- a/src/dsh-ledger.ts +++ b/src/dsh-ledger.ts @@ -22,6 +22,7 @@ import fs from "node:fs"; import path from "node:path"; +import semver from "semver"; import { DSH_DESKTOP_PROFILE, DSH_PACKAGE, dshProfileDepSpec, isRegistryDepSpec, planDshProfileManifestSpec } from "./dsh-channel.js"; type LedgerLockKind = "pnpm" | "npm" | "none"; @@ -73,6 +74,12 @@ interface LedgerConvergenceResult { notes: string[]; /** Set when the state is not safely convergable — NOTHING was written. */ error?: string; + /** Set with `error` when at least one ledger (manifest spec / lockfile + * record) exists and still records the OLD version after the copy moved + * — the caller must not destroy the displaced copy (the only rollback + * source). Absent/false = no ledgers to diverge from (nothing to + * converge) or convergence succeeded. */ + divergent?: boolean; } const PKG_ESCAPED = DSH_PACKAGE.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); @@ -590,10 +597,27 @@ function resolveDepFromStore(lines: string[], section: "packages" | "snapshots", return found.length === 1 ? found[0] : undefined; } +/** Does the store entry's resolved version satisfy the spec the on-disk copy + * declares for that dep? A cloned store block carries the SOURCE version's + * resolution data — when the dependency's DECLARED spec changed between + * versions (zod 4.1.8 → exact 4.6.7, #7125), the clone would record a + * resolution the new manifest contradicts, and a frozen install would + * faithfully install the wrong dep. undefined = unjudgeable (dist-tag / URL + * / workspace spec, non-semver value) — the caller stays silent, same + * "err on fewer false positives" discipline as specAdmits; false = a proven + * contradiction → the patch fails soft. */ +function depResolutionAdmits(spec: string, resolved: string): boolean | undefined { + if (semver.valid(resolved) === null) return undefined; + if (semver.validRange(spec) === null) return undefined; + return semver.satisfies(resolved, spec); +} + /** Cross-check the cloned store block's dep map against the on-disk copy's * direct deps (the NEW version's authoritative declaration): declared deps * missing from the map are resolved from the store (exactly one candidate — - * ambiguity fails soft); names no longer declared are dropped. */ + * ambiguity fails soft); names no longer declared are dropped; a surviving + * entry whose recorded resolution contradicts the new declaration fails soft + * (the resolution data predates a dependency change). */ function crossCheckDepMaps(lines: string[], block: string[], section: "packages" | "snapshots", deps: PnpmLockPatchOpts["deps"]): { kind: "ok"; lines: string[] } | { kind: "fail"; reason: string } { if (deps === undefined) return { kind: "ok", lines: block }; const declared: Record = {}; @@ -625,8 +649,19 @@ function crossCheckDepMaps(lines: string[], block: string[], section: "packages" const drop: number[] = []; for (const bucket of Object.keys(headerIdx)) { for (const [name, idx] of Object.entries(entryIdx[bucket]!)) { - if (declared[name] === undefined || declared[name] !== bucket) drop.push(idx); - else surviving[name] = bucket; + if (declared[name] === undefined || declared[name] !== bucket) { + drop.push(idx); + continue; + } + const spec = (bucket === "dependencies" ? deps.dependencies : deps.optionalDependencies)![name]; + const vm = block[idx]!.match(/^[ \t]+[^\s:][^:]*:[ \t]*(\S.*)$/); + if (vm !== null) { + const resolved = unquote(splitPeerSuffix(vm[1])[0]); + if (depResolutionAdmits(spec, resolved) === false) { + return { kind: "fail", reason: `the cloned ${section} entry carries '${name}: ${resolved}' but the on-disk copy declares '${name}: ${spec}' — the store's resolution data predates the dependency change; let pnpm record the new resolution (install) and rerun` }; + } + } + surviving[name] = bucket; } } for (const idx of drop.sort((a, b) => b - a)) out.splice(idx, 1); @@ -871,7 +906,11 @@ function atomicWriteText(file: string, content: string): string | undefined { export function convergeDshLedger(profileDir: string, targetVersion: string, opts?: { integrity?: string }): LedgerConvergenceResult { const notes: string[] = []; const state = readDshLedgerState(profileDir); - const fail = (error: string): LedgerConvergenceResult => ({ changed: [], details: [], notes, error }); + // A failed convergence leaves every ledger that EXISTS still recording the + // old version while the copy moved — that divergence is what keeps the + // displaced copy alive as the rollback source (the caller reads it). + const ledgersPresent = state.spec !== undefined || state.lockKind !== "none"; + const fail = (error: string): LedgerConvergenceResult => ({ changed: [], details: [], notes, error, divergent: ledgersPresent }); const spec = state.spec; if (spec === undefined) return fail("no billion-context dependency declared in the profile manifest — nothing to converge"); @@ -935,14 +974,14 @@ export function convergeDshLedger(profileDir: string, targetVersion: string, opt const details: LedgerChangeDetail[] = []; if (plan.action === "rewrite") { const err = atomicWriteText(path.join(profileDir, "package.json"), plan.nextRaw ?? ""); - if (err !== undefined) return { changed, details, notes, error: `manifest write failed: ${err}` }; + if (err !== undefined) return { changed, details, notes, error: `manifest write failed: ${err}`, divergent: ledgersPresent }; changed.push("package.json"); details.push({ file: "package.json", field: "dependencies[billion-context]", before: spec, after: finalSpec }); } if (lockNext !== undefined) { const lockFile = state.lockKind === "pnpm" ? "pnpm-lock.yaml" : "package-lock.json"; const err = atomicWriteText(path.join(profileDir, lockFile), lockNext); - if (err !== undefined) return { changed, details, notes, error: `lockfile write failed: ${err}${changed.length > 0 ? ` (package.json already updated — rerun to finish)` : ""}` }; + if (err !== undefined) return { changed, details, notes, error: `lockfile write failed: ${err}${changed.length > 0 ? ` (package.json already updated — rerun to finish)` : ""}`, divergent: ledgersPresent }; changed.push(lockFile); details.push(...lockDetails); } diff --git a/src/update.ts b/src/update.ts index c3f1f0332a..080f99e58c 100644 --- a/src/update.ts +++ b/src/update.ts @@ -2172,16 +2172,12 @@ export async function installViaTarball( } } - // Success: the backup is no longer needed, and neither is the displaced - // pnpm link (the store copy underneath it was left byte-identical). + // Success: the backup is no longer needed. The displaced pnpm link is the + // copy's only rollback source, so its removal waits for the ledger + // convergence below — a failed convergence leaves the ledgers on the old + // version while the copy stands at the new one, and destroying the old + // link would leave no way back (#7125). if (!pnpmOldLink) await rm(backupDir, { recursive: true, force: true }); - if (pnpmOldLink) { - try { - await unlink(pnpmOldLink); - } catch { - // inert once installDir holds the fresh real directory - } - } await rm(workDir, { recursive: true, force: true }); // #2894/#3496: the dsh desktop lane is the only one bili overwrites in @@ -2194,15 +2190,24 @@ export async function installViaTarball( // lockfile record — to the version just installed, as one transaction. // Local pins stay respected; a ledger-sync failure logs but never fails // the install (the copy itself is verified and standing). + let keptPnpmLink: string | null = null; if (isDshDesktopBiliCopy(installDir, real, env)) { const profileDir = path.join(resolveDshHome(env), "profiles", DSH_DESKTOP_PROFILE); const conv = convergeDshLedger(profileDir, version, { integrity }); if (conv.error !== undefined) { - loggerLog("warn", `[update] dsh ${DSH_DESKTOP_PROFILE} profile copy replaced at ${version} but its ledgers were not converged (${conv.error}) — dsh may revert the copy on its next plugin install; rerun 'bili doctor --fix'`); + if (pnpmOldLink !== null && conv.divergent === true) keptPnpmLink = pnpmOldLink; + loggerLog("warn", `[update] dsh ${DSH_DESKTOP_PROFILE} profile copy replaced at ${version} but its ledgers were not converged (${conv.error}) — dsh may revert the copy on its next plugin install; rerun 'bili doctor --fix'${keptPnpmLink !== null ? `; the previous copy is kept at ${keptPnpmLink} as the rollback source` : ""}`); } else if (conv.changed.length > 0) { loggerLog("info", `[update] dsh ${DSH_DESKTOP_PROFILE} profile ledgers converged to ${version}: ${conv.details.map((d) => `${d.file} ${d.field} ${d.before} → ${d.after}`).join("; ")}`); } } + if (pnpmOldLink !== null && keptPnpmLink === null) { + try { + await unlink(pnpmOldLink); + } catch { + // inert once installDir holds the fresh real directory + } + } return { ok: true }; } diff --git a/tests/issue3496-dsh-ledger-convergence.test.ts b/tests/issue3496-dsh-ledger-convergence.test.ts index 39a346c82d..59376206fc 100644 --- a/tests/issue3496-dsh-ledger-convergence.test.ts +++ b/tests/issue3496-dsh-ledger-convergence.test.ts @@ -821,6 +821,147 @@ test("#7125: dep-map cross-check — an ambiguous store entry fails soft, zero w } }); +test("#7125: dep-map cross-check — a surviving entry whose resolution contradicts the new declaration fails soft, zero writes", () => { + const root = mkdtempSync(path.join(tmpdir(), "bc-i7125-e-")); + try { + // The 0.1.196 snapshot resolved zod to 4.1.8; the on-disk copy + // (0.1.197) declares EXACT 4.6.7 — the dependency's spec changed + // between versions without the name or bucket moving. Cloning the old + // resolution would record a dep the new manifest contradicts (a frozen + // install would faithfully install the wrong zod), so the patch must + // refuse instead. + const lock = [ + "lockfileVersion: '9.0'", + "", + "settings:", + " autoInstallPeers: true", + " excludeLinksFromLockfile: false", + "", + "importers:", + "", + " .:", + " dependencies:", + " billion-context:", + " specifier: ^0.1.196", + " version: 0.1.196", + "", + "packages:", + "", + " billion-context@0.1.196:", + " resolution: {integrity: sha512-fake==}", + "", + "snapshots:", + "", + " billion-context@0.1.196:", + " dependencies:", + " zod: 4.1.8", + "", + " zod@4.1.8: {}", + "", + ].join("\n"); + const dir = makeProfile(root, "depchange", { spec: "^0.1.196", copyVersion: "0.1.197", pnpmLock: lock, copyDeps: { dependencies: { zod: "4.6.7" } } }); + const before = profileFiles(dir); + const beforeMt = profileMtimes(dir); + const res = convergeDshLedger(dir, "0.1.197", { integrity: "sha512-new==" }); + assert.ok(res.error !== undefined, "a contradicting cloned resolution must fail the patch"); + assert.ok(res.error!.includes("zod"), `reason must name the dep: ${res.error}`); + assert.ok(res.error!.includes("4.1.8") && res.error!.includes("4.6.7"), `reason must carry both versions: ${res.error}`); + assertZeroWrites(dir, before, beforeMt); + } finally { + rmrf(root); + rmrf(__cacheRoot); + } +}); + +test("#7125: dep-map cross-check — a surviving entry whose resolution SATISFIES the new declaration clones through", () => { + const root = mkdtempSync(path.join(tmpdir(), "bc-i7125-f-")); + try { + // Negative gate: the range spec still admits the old resolution — no + // false positive, the clone proceeds as before. + const lock = [ + "lockfileVersion: '9.0'", + "", + "settings:", + " autoInstallPeers: true", + " excludeLinksFromLockfile: false", + "", + "importers:", + "", + " .:", + " dependencies:", + " billion-context:", + " specifier: ^0.1.196", + " version: 0.1.196", + "", + "packages:", + "", + " billion-context@0.1.196:", + " resolution: {integrity: sha512-fake==}", + "", + "snapshots:", + "", + " billion-context@0.1.196:", + " dependencies:", + " mysql2: 3.15.5", + "", + " mysql2@3.15.5: {}", + "", + ].join("\n"); + const dir = makeProfile(root, "depsat", { spec: "^0.1.196", copyVersion: "0.1.197", pnpmLock: lock, copyDeps: { dependencies: { mysql2: "^3.15.3" } } }); + const res = convergeDshLedger(dir, "0.1.197", { integrity: "sha512-new==" }); + assert.equal(res.error, undefined, `satisfying resolution must not fail the patch: ${res.error}`); + const out = readFileSync(path.join(dir, "pnpm-lock.yaml"), "utf8"); + assert.match(out, /billion-context@0\.1\.197:\n dependencies:\n mysql2: 3\.15\.5/); + } finally { + rmrf(root); + rmrf(__cacheRoot); + } +}); + +test("#7125: dep-map cross-check — an unjudgeable declared spec (dist-tag) stays silent", () => { + const root = mkdtempSync(path.join(tmpdir(), "bc-i7125-g-")); + try { + // Same "err on fewer false positives" discipline as specAdmits: a + // dist-tag declaration cannot be semver-checked, so the clone + // proceeds as before. + const lock = [ + "lockfileVersion: '9.0'", + "", + "settings:", + " autoInstallPeers: true", + " excludeLinksFromLockfile: false", + "", + "importers:", + "", + " .:", + " dependencies:", + " billion-context:", + " specifier: ^0.1.196", + " version: 0.1.196", + "", + "packages:", + "", + " billion-context@0.1.196:", + " resolution: {integrity: sha512-fake==}", + "", + "snapshots:", + "", + " billion-context@0.1.196:", + " dependencies:", + " some-dep: 2.0.0", + "", + " some-dep@2.0.0: {}", + "", + ].join("\n"); + const dir = makeProfile(root, "deptest", { spec: "^0.1.196", copyVersion: "0.1.197", pnpmLock: lock, copyDeps: { dependencies: { "some-dep": "latest" } } }); + const res = convergeDshLedger(dir, "0.1.197", { integrity: "sha512-new==" }); + assert.equal(res.error, undefined, `unjudgeable spec must not fail the patch: ${res.error}`); + } finally { + rmrf(root); + rmrf(__cacheRoot); + } +}); + // — pure patchers ———————————————————————————————————————————————————————————————— test("#3496: patchPnpmLock structural failures — no partial result", () => { diff --git a/tests/update-desktop-inplace.test.ts b/tests/update-desktop-inplace.test.ts index a169dc32fa..b7b1796687 100644 --- a/tests/update-desktop-inplace.test.ts +++ b/tests/update-desktop-inplace.test.ts @@ -22,6 +22,7 @@ const __cacheRoot = mkdtempSync(path.join(tmpdir(), "bc-desktop-inplace-cache-") process.env.XDG_CACHE_HOME = path.join(__cacheRoot, "cache"); const { installViaTarball, readDiskVersion, refreshDshDesktopCopy, dshDesktopCopyPathMatches } = await import("../src/update.ts"); import { refreshDshProfileBundles, syncDshProfileManifestSpec } from "../src/dsh-channel.ts"; +import { setLogCapture } from "../src/logger.ts"; import { rmrf } from "./tmp-rm.ts"; function integrityField(buf: Buffer, alg = "sha512"): string { @@ -104,6 +105,158 @@ test("desktop profile copy: link displaced, real dir laid down, .pnpm store unto } }); +// #7125: fail-soft lock planning must not destroy the rollback source. When +// the target version's SRI is unknown (legacy SHA-1-only registry doc), the +// v9 lock patch refuses to write — the source packages: entry carries an +// integrity the clone cannot replace. The ledgers then stay on the old +// version while the copy stands at the new one, and the displaced pnpm link +// is the only way back: it must SURVIVE a divergent convergence, and still +// be unlinked when the convergence succeeds. +function buildLedgerFixture(root: string): { dshHome: string; profileDir: string; storeCopy: string; flat: string } { + const dshHome = path.join(root, "dsh-home"); + const profileDir = path.join(dshHome, "profiles", "desktop"); + const storeCopy = path.join(profileDir, ".pnpm", "billion-context@1.2.3", "node_modules", "billion-context"); + const flat = path.join(profileDir, "node_modules", "billion-context"); + writePkg(storeCopy, "1.2.3"); + mkdirSync(path.join(profileDir, "node_modules"), { recursive: true }); + makePnpmLink(storeCopy, flat); + // The two ledgers at the OLD version: manifest spec + v9 lockfile record. + writeFileSync( + path.join(profileDir, "package.json"), + JSON.stringify({ name: "@dsh/profile-desktop", dependencies: { "billion-context": "^1.2.3" } }, null, 2) + "\n", + ); + writeFileSync(path.join(profileDir, "pnpm-lock.yaml"), [ + "lockfileVersion: '9.0'", + "", + "settings:", + " autoInstallPeers: true", + " excludeLinksFromLockfile: false", + "", + "importers:", + "", + " .:", + " dependencies:", + " billion-context:", + " specifier: ^1.2.3", + " version: 1.2.3", + "", + "packages:", + "", + " billion-context@1.2.3:", + " resolution: {integrity: sha512-old==}", + "", + "snapshots:", + "", + " billion-context@1.2.3: {}", + "", + ].join("\n")); + return { dshHome, profileDir, storeCopy, flat }; +} + +function makeTgzFor(root: string, version: string): Buffer { + const src = path.join(root, "pkg", "package"); + writePkg(src, version); + const tgzPath = path.join(root, "pkg.tgz"); + tar.c({ cwd: path.join(root, "pkg"), file: tgzPath, gzip: true, sync: true }, ["package"]); + return readFileSync(tgzPath); +} + +test("desktop profile copy: divergent ledger convergence keeps the displaced pnpm link as rollback source (#7125)", { timeout: 30_000 }, async (t) => { + const root = mkdtempSync(path.join(tmpdir(), "bc-desktop-divergent-")); + const prevDshHome = process.env.DSH_HOME; + const prevXdgCacheHome = process.env.XDG_CACHE_HOME; + const originalFetch = globalThis.fetch; + t.after(() => { + globalThis.fetch = originalFetch; + setLogCapture(null); + 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 = buildLedgerFixture(root); + process.env.DSH_HOME = fx.dshHome; + process.env.XDG_CACHE_HOME = path.join(root, "cache"); + const tgz = makeTgzFor(root, "2.0.0"); + // Legacy registry doc: SHA-1 shasum only, no SRI — the lock patch + // cannot produce the new packages: integrity and must refuse. + const shasum = crypto.createHash("sha1").update(tgz).digest("hex"); + + globalThis.fetch = (() => Promise.resolve(new Response(tgz))) as unknown as typeof fetch; + const lines: string[] = []; + setLogCapture((level, msg) => { lines.push(`${level}: ${msg}`); }); + const r = await installViaTarball("2.0.0", "https://registry.test/x.tgz", fx.flat, undefined, shasum); + assert.equal(r.ok, true, r.error ?? "a ledger-sync failure must not fail the install"); + + // The copy moved to 2.0.0 and the ledgers stayed at 1.2.3 ... + assert.equal(lstatSync(fx.flat).isSymbolicLink(), false, "the pnpm link must be replaced, not followed"); + assert.equal(await readDiskVersion(fx.flat), "2.0.0"); + assert.match(readFileSync(path.join(fx.profileDir, "pnpm-lock.yaml"), "utf8"), /version: 1\.2\.3/, "the lockfile must be untouched"); + // ... so the displaced link survives as the rollback source. + const kept = readdirSync(path.dirname(fx.flat)).filter((e) => e.startsWith("billion-context.pnpm-")); + assert.equal(kept.length, 1, `exactly one displaced link kept: ${kept.join(", ")}`); + assert.ok( + lines.some((l) => l.startsWith("warn:") && l.includes(kept[0]!) && l.includes("rollback source")), + `the warn must name the kept link: ${lines.join(" | ")}`, + ); + } catch (e) { + if (String(e).includes("EPERM")) { + t.skip("creating directory symlinks requires elevated privileges on this platform"); + return; + } + throw e; + } +}); + +test("desktop profile copy: converged ledgers unlink the displaced pnpm link as before (#7125 control)", { timeout: 30_000 }, async (t) => { + const root = mkdtempSync(path.join(tmpdir(), "bc-desktop-converged-")); + const prevDshHome = process.env.DSH_HOME; + const prevXdgCacheHome = process.env.XDG_CACHE_HOME; + const originalFetch = globalThis.fetch; + t.after(() => { + globalThis.fetch = originalFetch; + setLogCapture(null); + 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 = buildLedgerFixture(root); + process.env.DSH_HOME = fx.dshHome; + process.env.XDG_CACHE_HOME = path.join(root, "cache"); + const tgz = makeTgzFor(root, "2.0.0"); + + globalThis.fetch = (() => Promise.resolve(new Response(tgz))) as unknown as typeof fetch; + const lines: string[] = []; + setLogCapture((level, msg) => { lines.push(`${level}: ${msg}`); }); + const r = await installViaTarball("2.0.0", "https://registry.test/x.tgz", fx.flat, integrityField(tgz)); + assert.equal(r.ok, true, r.error ?? "install failed without reason"); + + assert.equal(lstatSync(fx.flat).isSymbolicLink(), false, "the pnpm link must be replaced, not followed"); + assert.equal(await readDiskVersion(fx.flat), "2.0.0"); + // Both ledgers converged to 2.0.0 — the lock patch could produce the + // new integrity, so nothing must be kept back. + const lock = readFileSync(path.join(fx.profileDir, "pnpm-lock.yaml"), "utf8"); + assert.match(lock, /version: 2\.0\.0/); + assert.ok(lock.includes(`integrity: ${integrityField(tgz)}`), `the new integrity must be recorded in the cloned entry`); + assert.match(readFileSync(path.join(fx.profileDir, "package.json"), "utf8"), /\^2\.0\.0/); + for (const entry of readdirSync(path.dirname(fx.flat))) { + assert.ok(!entry.startsWith("billion-context.pnpm-"), `leftover displaced link: ${entry}`); + } + assert.ok(!lines.some((l) => l.startsWith("warn:")), `unexpected warns: ${lines.join(" | ")}`); + } catch (e) { + if (String(e).includes("EPERM")) { + t.skip("creating directory symlinks requires elevated privileges on this platform"); + return; + } + throw e; + } +}); + // —— drive-path coverage: refreshDshDesktopCopy + the channel skip (#1575) ————————— interface DesktopFixture { root: string; dshHome: string; storeCopy: string; flat: string; tgz: Buffer; }