Skip to content

Commit 745a12a

Browse files
committed
fix(genbi): preserve source checkout context loader install
1 parent 65ef45b commit 745a12a

3 files changed

Lines changed: 40 additions & 42 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This tracked marker identifies the source workspace package. It is intentionally excluded from the npm package files allowlist.

‎apps/context-loader/scripts/installer.mjs‎

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
import { createHash } from "node:crypto";
2-
import { execFile as execFileCallback } from "node:child_process";
3-
import { lstatSync } from "node:fs";
42
import { chmod, lstat, mkdir, open, readFile, realpath, rename, rm, stat } from "node:fs/promises";
53
import path from "node:path";
6-
import { promisify } from "node:util";
74
import { gunzipSync } from "node:zlib";
85
import { CANONICAL_BINARY, ContextLoaderPackageError, isSha256, sha256File, targetFor } from "../lib/verified.mjs";
96

10-
const execFile = promisify(execFileCallback);
11-
const SOURCE_ORIGINS = new Set([
12-
"git@github.com:Canner/WrenAI.git",
13-
"https://github.com/Canner/WrenAI.git",
14-
"ssh://git@github.com/Canner/WrenAI.git",
15-
]);
7+
const SOURCE_MARKER = "SOURCE_WORKSPACE";
168

179
/** Downloads, verifies, and atomically installs the exact manifest row for this package. */
1810
export async function installContextLoader({ packageRoot, fetchImpl = fetch, platform = process.platform, arch = process.arch } = {}) {
@@ -67,23 +59,19 @@ export async function installContextLoader({ packageRoot, fetchImpl = fetch, pla
6759

6860
/**
6961
* The checked-out source package carries an intentionally empty, pre-release
70-
* artifact template. Only that exact repository layout skips postinstall;
71-
* packed packages always download and verify their tagged artifact.
62+
* artifact template. Only the tracked source-only marker in the canonical
63+
* workspace layout skips postinstall; the package files allowlist excludes it,
64+
* so packed packages always download and verify their tagged artifact.
7265
*/
7366
export async function isRepositorySourcePackage(packageRoot) {
7467
try {
75-
if (!lstatSync(packageRoot).isDirectory()) return false;
76-
const physicalPackage = await realpath(packageRoot);
77-
const { stdout: repositoryOutput } = await execFile("git", ["-C", physicalPackage, "rev-parse", "--show-toplevel"], { timeout: 2_000 });
78-
const repositoryRoot = await realpath(repositoryOutput.trim());
79-
if (physicalPackage !== path.join(repositoryRoot, "apps", "context-loader")) return false;
80-
const { stdout: originOutput } = await execFile("git", ["-C", repositoryRoot, "remote", "get-url", "origin"], { timeout: 2_000 });
81-
if (!SOURCE_ORIGINS.has(originOutput.trim())) return false;
82-
await execFile(
83-
"git",
84-
["-C", repositoryRoot, "ls-files", "--error-unmatch", "--", "apps/context-loader/package.json", "pnpm-workspace.yaml", "core/wren/pyproject.toml"],
85-
{ timeout: 2_000 },
86-
);
68+
const physicalPackage = await physicalPackageRoot(packageRoot);
69+
const workspaceRoot = path.resolve(physicalPackage, "..", "..");
70+
if (physicalPackage !== path.join(workspaceRoot, "apps", "context-loader")) return false;
71+
if (!(await lstat(workspaceRoot)).isDirectory() || (await realpath(workspaceRoot)) !== workspaceRoot) return false;
72+
const marker = await ownedRegularFile(physicalPackage, SOURCE_MARKER);
73+
const workspaceManifest = await ownedRegularFile(workspaceRoot, "pnpm-workspace.yaml");
74+
if (marker !== path.join(physicalPackage, SOURCE_MARKER) || workspaceManifest !== path.join(workspaceRoot, "pnpm-workspace.yaml")) return false;
8775
return true;
8876
} catch {
8977
return false;

‎apps/context-loader/test/installer.test.mjs‎

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ async function fixture({ artifact = Buffer.from("#!/bin/sh\necho loader\n"), tar
4646

4747
const fetchArchive = (archive) => async () => ({ ok: true, arrayBuffer: async () => archive });
4848

49+
async function makeSourceWorkspace(root) {
50+
const packageRoot = path.join(root, "apps", "context-loader");
51+
await mkdir(packageRoot, { recursive: true });
52+
await writeFile(path.join(root, "pnpm-workspace.yaml"), "packages: []\n");
53+
await writeFile(path.join(packageRoot, "SOURCE_WORKSPACE"), "source marker\n");
54+
return packageRoot;
55+
}
56+
4957
test("installs a digest-verified darwin-arm64 binary atomically and records canonical state", async () => {
5058
const { root, archive, artifact } = await fixture();
5159
const first = await installContextLoader({ packageRoot: root, fetchImpl: fetchArchive(archive), platform: "darwin", arch: "arm64" });
@@ -123,23 +131,24 @@ test("refuses a same-bytes external binary during reuse and a bin-directory link
123131
assert.deepEqual(await readdir(externalDir), ["wren-context-loader"]);
124132
});
125133

126-
test("skips only the tracked source checkout, never a lookalike git repository or packed copy", async () => {
127-
const sourcePackage = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
128-
assert.equal(await isRepositorySourcePackage(sourcePackage), true);
129-
130-
const fakeRoot = await mkdtemp(path.join(os.tmpdir(), "context-loader-fake-source-"));
131-
const fakePackage = path.join(fakeRoot, "apps", "context-loader");
132-
await mkdir(path.join(fakeRoot, "core", "wren"), { recursive: true });
133-
await mkdir(fakePackage, { recursive: true });
134-
await writeFile(path.join(fakePackage, "package.json"), "{}\n");
135-
await writeFile(path.join(fakeRoot, "pnpm-workspace.yaml"), "packages: []\n");
136-
await writeFile(path.join(fakeRoot, "core", "wren", "pyproject.toml"), "[project]\nname = \"wren\"\n");
137-
execFileSync("git", ["init", fakeRoot]);
138-
execFileSync("git", ["-C", fakeRoot, "add", "apps/context-loader/package.json", "pnpm-workspace.yaml", "core/wren/pyproject.toml"]);
139-
execFileSync("git", ["-C", fakeRoot, "remote", "add", "origin", "https://example.invalid/lookalike.git"]);
140-
assert.equal(await isRepositorySourcePackage(fakePackage), false);
141-
142-
const packedCopy = path.join(fakeRoot, "node_modules", "@wrenai", "context-loader");
143-
await mkdir(packedCopy, { recursive: true });
144-
assert.equal(await isRepositorySourcePackage(packedCopy), false);
134+
test("recognizes fork and source-archive workspaces, while npm-packed copies cannot bypass", async () => {
135+
const forkRoot = await mkdtemp(path.join(os.tmpdir(), "context-loader-fork-"));
136+
const forkPackage = await makeSourceWorkspace(forkRoot);
137+
execFileSync("git", ["init", forkRoot]);
138+
execFileSync("git", ["-C", forkRoot, "remote", "add", "origin", "https://example.invalid/fork.git"]);
139+
assert.equal(await isRepositorySourcePackage(forkPackage), true);
140+
141+
const sourceArchiveRoot = await mkdtemp(path.join(os.tmpdir(), "context-loader-source-archive-"));
142+
const sourceArchivePackage = await makeSourceWorkspace(sourceArchiveRoot);
143+
assert.equal(await isRepositorySourcePackage(sourceArchivePackage), true);
144+
145+
const actualPackageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
146+
const packed = JSON.parse(execFileSync("npm", ["pack", "--json", "--dry-run"], { cwd: actualPackageRoot, encoding: "utf8" }));
147+
assert.equal(packed[0].files.some((entry) => entry.path === "SOURCE_WORKSPACE"), false);
148+
149+
const unpackedRoot = await mkdtemp(path.join(os.tmpdir(), "context-loader-unpacked-"));
150+
const unpackedPackage = path.join(unpackedRoot, "apps", "context-loader");
151+
await mkdir(unpackedPackage, { recursive: true });
152+
await writeFile(path.join(unpackedRoot, "pnpm-workspace.yaml"), "packages: []\n");
153+
assert.equal(await isRepositorySourcePackage(unpackedPackage), false);
145154
});

0 commit comments

Comments
 (0)