Skip to content

Commit 6048b79

Browse files
author
Peter Steinberger
committed
fix: bind frozen Knip caches across pnpm versions
1 parent 5b0ff66 commit 6048b79

2 files changed

Lines changed: 68 additions & 28 deletions

File tree

‎src/repair/pinned-openclaw-validation-helper.ts‎

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { parse as parseYaml } from "yaml";
88
import { runContainedCommand } from "./command-runner.js";
99

1010
const PINNED_OPENCLAW_KNIP_VERSION = "6.8.0";
11+
const PINNED_OPENCLAW_KNIP_PUBLISHED_AT = "2026-04-29T06:27:29.928Z";
1112
const PREPARED_PNPM_HELPER_CACHE = ".__clawsweeper_pnpm_helper_cache__";
1213
const MINIMUM_OPENCLAW_RELEASE_AGE_MINUTES = 48 * 60;
1314
const PINNED_OPENCLAW_KNIP_LOCK = fileURLToPath(
@@ -40,12 +41,9 @@ export function preparePinnedOpenClawValidationHelper({
4041
const profileRoot = path.dirname(String(validationEnv.HOME));
4142
const helperCache = path.join(String(validationEnv.COREPACK_HOME), PREPARED_PNPM_HELPER_CACHE);
4243
const minimumReleaseAge = openClawMinimumReleaseAge(cwd);
43-
const cacheRoot = path.join(
44-
helperCache,
45-
"pnpm",
46-
"dlx",
47-
pinnedOpenClawDlxCacheKey(installRegistry),
48-
);
44+
const dlxRoot = path.join(helperCache, "pnpm", "dlx");
45+
const fullCacheKey = pinnedOpenClawDlxCacheKey(installRegistry);
46+
const cacheRoot = path.join(dlxRoot, fullCacheKey);
4947
const helperProject = path.join(cacheRoot, "pinned");
5048
fs.mkdirSync(helperProject, { recursive: true, mode: 0o700 });
5149
fs.copyFileSync(PINNED_OPENCLAW_KNIP_LOCK, path.join(helperProject, "pnpm-lock.yaml"));
@@ -83,8 +81,9 @@ export function preparePinnedOpenClawValidationHelper({
8381
);
8482
scopePinnedOpenClawJitiCache(helperProject);
8583
fs.symlinkSync("pinned", path.join(cacheRoot, "pkg"));
86-
seedOfflinePinnedOpenClawMetadata(helperCache, installRegistry);
84+
fs.symlinkSync(fullCacheKey, path.join(dlxRoot, fullCacheKey.slice(0, 32)));
8785
assertPinnedOpenClawValidationHelperLock(helperCache);
86+
seedOfflinePinnedOpenClawMetadata(helperCache, helperProject, installRegistry);
8887
}
8988

9089
function scopePinnedOpenClawJitiCache(helperProject: string): void {
@@ -107,32 +106,60 @@ function pinnedOpenClawDlxCacheKey(installRegistry: string): string {
107106
];
108107
return createHash("sha256")
109108
.update(JSON.stringify([resolvedPackages, registries]))
110-
.digest("hex")
111-
.slice(0, 32);
109+
.digest("hex");
112110
}
113111

114-
function seedOfflinePinnedOpenClawMetadata(helperCache: string, installRegistry: string): void {
112+
function seedOfflinePinnedOpenClawMetadata(
113+
helperCache: string,
114+
helperProject: string,
115+
installRegistry: string,
116+
): void {
115117
const cacheRoot = path.join(helperCache, "pnpm");
116118
const versions = fs.readdirSync(cacheRoot).filter((entry) => /^v\d+$/.test(entry));
117119
// pnpm's encode-registry package replaces the host/port separator with `+`.
118120
const registryHost = new URL(installRegistry).host.replace(":", "+");
119-
let seeded = false;
121+
const manifest = JSON.parse(
122+
fs.readFileSync(path.join(helperProject, "node_modules", "knip", "package.json"), "utf8"),
123+
) as Record<string, unknown>;
124+
if (manifest.name !== "knip" || manifest.version !== PINNED_OPENCLAW_KNIP_VERSION) {
125+
throw new Error("pinned OpenClaw Knip package does not match the trusted dependency graph");
126+
}
127+
const lock = parseYaml(fs.readFileSync(PINNED_OPENCLAW_KNIP_LOCK, "utf8")) as {
128+
packages?: Record<string, { resolution?: { integrity?: string } }>;
129+
};
130+
const integrity = lock.packages?.[`knip@${PINNED_OPENCLAW_KNIP_VERSION}`]?.resolution?.integrity;
131+
if (typeof integrity !== "string" || !integrity.startsWith("sha512-")) {
132+
throw new Error("pinned OpenClaw Knip integrity is missing from the trusted dependency graph");
133+
}
134+
const version = {
135+
...manifest,
136+
dist: {
137+
integrity,
138+
tarball: new URL(`knip/-/knip-${PINNED_OPENCLAW_KNIP_VERSION}.tgz`, installRegistry).href,
139+
},
140+
};
141+
const metadata = {
142+
name: "knip",
143+
"dist-tags": { latest: PINNED_OPENCLAW_KNIP_VERSION },
144+
versions: { [PINNED_OPENCLAW_KNIP_VERSION]: version },
145+
time: { [PINNED_OPENCLAW_KNIP_VERSION]: PINNED_OPENCLAW_KNIP_PUBLISHED_AT },
146+
modified: PINNED_OPENCLAW_KNIP_PUBLISHED_AT,
147+
cachedAt: Date.now(),
148+
};
120149
for (const version of versions) {
121-
const metadata = path.join(cacheRoot, version, "metadata-full", registryHost, "knip.jsonl");
122-
if (!fs.existsSync(metadata)) continue;
123-
const filtered = path.join(
124-
cacheRoot,
125-
version,
126-
"metadata-full-filtered",
127-
registryHost,
128-
"knip.jsonl",
129-
);
130-
fs.mkdirSync(path.dirname(filtered), { recursive: true });
131-
fs.copyFileSync(metadata, filtered);
132-
seeded = true;
150+
for (const layout of ["metadata", "metadata-full", "metadata-full-filtered"]) {
151+
const destination = path.join(cacheRoot, version, layout, registryHost, "knip.jsonl");
152+
fs.mkdirSync(path.dirname(destination), { recursive: true });
153+
fs.writeFileSync(
154+
destination,
155+
`${JSON.stringify({ modified: PINNED_OPENCLAW_KNIP_PUBLISHED_AT })}\n${JSON.stringify(metadata)}\n`,
156+
);
157+
}
133158
}
134-
if (!seeded) {
135-
throw new Error("pinned OpenClaw Knip registry metadata was not created by frozen install");
159+
for (const layout of ["metadata-v1.3", "metadata-full-v1.3", "metadata-ff-v1.3"]) {
160+
const destination = path.join(cacheRoot, layout, registryHost, "knip.json");
161+
fs.mkdirSync(path.dirname(destination), { recursive: true });
162+
fs.writeFileSync(destination, JSON.stringify(metadata));
136163
}
137164
}
138165

‎test/repair/target-validation.test.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3666,6 +3666,9 @@ if (args[0] === "install") {
36663666
const bin = path.join(process.cwd(), "node_modules", ".bin", "knip");
36673667
fs.mkdirSync(path.dirname(bin), { recursive: true });
36683668
fs.writeFileSync(bin, "#!/bin/sh\\nexit 0\\n", { mode: 0o755 });
3669+
const manifest = path.join(process.cwd(), "node_modules", "knip", "package.json");
3670+
fs.mkdirSync(path.dirname(manifest), { recursive: true });
3671+
fs.writeFileSync(manifest, JSON.stringify({ name: "knip", version: "6.8.0" }));
36693672
if (fs.existsSync(${JSON.stringify(path.join(hostBin, "tamper-lock"))})) {
36703673
const lock = path.join(process.cwd(), "pnpm-lock.yaml");
36713674
fs.writeFileSync(lock, fs.readFileSync(lock, "utf8").replace("specifier: 6.8.0", "specifier: 6.8.1"));
@@ -3679,13 +3682,23 @@ if (args[0] === "install") {
36793682
}
36803683
if (args.at(-1) === "first" || args.at(-1) === "second") {
36813684
const registry = process.env.PNPM_CONFIG_REGISTRY;
3682-
const cacheKey = require("node:crypto")
3685+
const fullCacheKey = require("node:crypto")
36833686
.createHash("sha256")
36843687
.update(JSON.stringify([["[email protected]"], [["@jsr", "https://npm.jsr.io/"], ["default", registry]]]))
3685-
.digest("hex")
3686-
.slice(0, 32);
3688+
.digest("hex");
3689+
const cacheKey = fullCacheKey.slice(0, 32);
36873690
const marker = path.join(process.env.XDG_CACHE_HOME, "pnpm", "dlx", cacheKey, "marker");
36883691
if (fs.readFileSync(marker, "utf8") !== "frozen helper") process.exit(42);
3692+
if (fs.readFileSync(path.join(process.env.XDG_CACHE_HOME, "pnpm", "dlx", fullCacheKey, "marker"), "utf8") !== "frozen helper") process.exit(44);
3693+
const host = new URL(registry).host.replace(":", "+");
3694+
for (const metadataPath of [
3695+
path.join(process.env.XDG_CACHE_HOME, "pnpm", "v11", "metadata-full-filtered", host, "knip.jsonl"),
3696+
path.join(process.env.XDG_CACHE_HOME, "pnpm", "metadata-ff-v1.3", host, "knip.json"),
3697+
]) {
3698+
const metadata = JSON.parse(fs.readFileSync(metadataPath, "utf8").trim().split("\\n").at(-1));
3699+
if (Object.keys(metadata.versions).join(",") !== "6.8.0") process.exit(45);
3700+
if (!metadata.versions["6.8.0"].dist.integrity.startsWith("sha512-")) process.exit(46);
3701+
}
36893702
const knip = path.join(path.dirname(marker), "pinned", "node_modules", ".bin", "knip");
36903703
if (!fs.readFileSync(knip, "utf8").includes("JITI_FS_CACHE=0")) process.exit(43);
36913704
if (args.at(-1) === "first") fs.writeFileSync(marker, "validation mutated its own cache");

0 commit comments

Comments
 (0)