Initial import of streamline

This commit is contained in:
2026-11-16 17:26:44 -08:00
commit 8e4aec1855
1221 changed files with 274256 additions and 0 deletions
+212
View File
@@ -0,0 +1,212 @@
import { afterEach, describe, expect, spyOn, test } from "bun:test";
import { existsSync, mkdtempSync, rmSync, writeFileSync, readFileSync, mkdirSync, readdirSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import type { Security } from "@bbbug/shared/types";
import { prepareSecurityEvalSource, pruneSecurityEvalSources, SecurityEvalSourceError } from "../src/security_eval_source";
const dirs: string[] = [];
afterEach(() => {
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
});
function git(cwd: string, args: string[]): string {
const proc = Bun.spawnSync({
cmd: ["git", "-c", "user.name=Streamline Test", "-c", "user.email=streamline-test@example.com", ...args],
cwd,
stdout: "pipe",
stderr: "pipe",
});
if (proc.exitCode !== 0) throw new Error(`${args.join(" ")} failed: ${proc.stderr.toString()}`);
return proc.stdout.toString().trim();
}
function fixture() {
const root = mkdtempSync(path.join(tmpdir(), "streamline-security-eval-source-"));
dirs.push(root);
const remote = path.join(root, "remote.git");
const seed = path.join(root, "seed");
const aiWorkspace = path.join(root, "ai_workspace");
const workspaceDir = path.join(root, "security-sec-1");
mkdirSync(remote);
mkdirSync(seed);
mkdirSync(aiWorkspace);
git(remote, ["init", "--bare"]);
git(seed, ["init"]);
writeFileSync(path.join(seed, "README.md"), "security source\n");
git(seed, ["add", "README.md"]);
git(seed, ["commit", "-m", "seed"]);
git(seed, ["branch", "-M", "develop"]);
git(seed, ["remote", "add", "origin", remote]);
git(seed, ["push", "origin", "develop"]);
writeFileSync(path.join(aiWorkspace, "CLAUDE.md"), "Shared evaluation guidance.\n");
return { remote: `file://${remote}`, aiWorkspace, workspaceDir,
commit: readFileSync(path.join(seed, ".git", "refs", "heads", "develop"), "utf8").trim() };
}
const security = (targetRef: string): Security => ({ id: "sec-1", targetRef, lang: "zh" }) as Security;
describe("Security Evaluate private source", () => {
test("runner clones the requested revision, copies CLAUDE.md, and removes only the clone", async () => {
const f = fixture();
mkdirSync(path.join(f.workspaceDir, "TikTok"), { recursive: true });
writeFileSync(path.join(f.workspaceDir, "TikTok", "stale"), "old checkout");
writeFileSync(path.join(f.workspaceDir, "TestSpec.md"), "keep the artifact\n");
const source = await prepareSecurityEvalSource({
security: security(f.commit), workspaceDir: f.workspaceDir, aiWorkspace: f.aiWorkspace,
baseBranch: "develop", repositoryUrl: f.remote, log: () => {},
});
expect(source.cwd).toBe(f.workspaceDir);
expect(readFileSync(path.join(source.sourceDir, ".git", "HEAD"), "utf8").trim()).toBe(f.commit);
expect(existsSync(path.join(source.sourceDir, "stale"))).toBe(false);
expect(readFileSync(path.join(source.cwd, "CLAUDE.md"), "utf8")).toBe("Shared evaluation guidance.\n");
expect(source.note).not.toContain("Shared evaluation guidance.");
expect(source.note).not.toContain("<ai_workspace_claude_md>");
expect(source.note).toContain("Do not clone");
expect(source.note).toContain("<boundry>");
expect(source.note).toContain("</boundry>");
await source.release();
await source.release();
expect(existsSync(source.sourceDir)).toBe(false);
expect(existsSync(path.join(f.workspaceDir, "TestSpec.md"))).toBe(true);
expect(existsSync(path.join(f.workspaceDir, "CLAUDE.md"))).toBe(true);
writeFileSync(path.join(f.aiWorkspace, "CLAUDE.md"), "Updated evaluation guidance.\n");
const branchSource = await prepareSecurityEvalSource({
security: security("develop"), workspaceDir: f.workspaceDir, aiWorkspace: f.aiWorkspace,
baseBranch: "develop", repositoryUrl: f.remote, log: () => {},
});
expect(readFileSync(path.join(branchSource.sourceDir, ".git", "HEAD"), "utf8").trim()).toBe(f.commit);
expect(readFileSync(path.join(branchSource.cwd, "CLAUDE.md"), "utf8")).toBe("Updated evaluation guidance.\n");
await branchSource.release();
});
test("restart cleanup removes only abandoned Security source, not reports or logs", async () => {
const f = fixture();
mkdirSync(path.join(f.workspaceDir, "logs"), { recursive: true });
writeFileSync(path.join(f.workspaceDir, "logs", "eval-r1.log"), "keep the log\n");
writeFileSync(path.join(f.workspaceDir, "TestSpec.md"), "keep the spec\n");
const source = await prepareSecurityEvalSource({
security: security(f.commit), workspaceDir: f.workspaceDir, aiWorkspace: f.aiWorkspace,
baseBranch: "develop", repositoryUrl: f.remote, log: () => {},
});
source.markAgentPid(process.pid);
await pruneSecurityEvalSources(path.dirname(f.workspaceDir), new Set(), () => {});
expect(existsSync(source.sourceDir)).toBe(true); // A live agent must not be deleted.
source.markAgentPid(2147483647); // Prior process is gone after a restart.
await pruneSecurityEvalSources(path.dirname(f.workspaceDir), new Set(), () => {});
expect(existsSync(source.sourceDir)).toBe(false);
expect(readFileSync(path.join(f.workspaceDir, "logs", "eval-r1.log"), "utf8")).toBe("keep the log\n");
expect(readFileSync(path.join(f.workspaceDir, "TestSpec.md"), "utf8")).toBe("keep the spec\n");
await source.release();
});
test("clone failure retries three times, cleans partial checkouts, then fails", async () => {
const f = fixture();
const warnings: string[] = [];
await expect(prepareSecurityEvalSource({
security: security("missing-ref"), workspaceDir: f.workspaceDir, aiWorkspace: f.aiWorkspace,
baseBranch: "develop", repositoryUrl: f.remote, cloneRetryDelayMs: 1,
log: (level, message) => { if (level === "warn") warnings.push(message); },
})).rejects.toThrow("failed after 3 attempts");
expect(warnings.filter((message) => message.includes("clone attempt")).length).toBe(2);
expect(existsSync(path.join(f.workspaceDir, "TikTok"))).toBe(false);
});
test("shared AI workspace checkout is rejected before launching the agent", async () => {
const f = fixture();
mkdirSync(path.join(f.aiWorkspace, "TikTok"));
await expect(prepareSecurityEvalSource({
security: security("develop"), workspaceDir: f.workspaceDir, aiWorkspace: f.aiWorkspace,
baseBranch: "develop", repositoryUrl: f.remote, log: () => {},
})).rejects.toBeInstanceOf(SecurityEvalSourceError);
});
test("slow background deletion cannot block a new clone or remove its source", async () => {
const f = fixture();
const completed = Promise.withResolvers<void>();
const started = Promise.withResolvers<void>();
const exited = Promise.withResolvers<number>();
const log = (_level: string, message: string) => {
if (message.includes("background source cleanup completed")) completed.resolve();
};
const prepare = () => prepareSecurityEvalSource({
security: security(f.commit), workspaceDir: f.workspaceDir, aiWorkspace: f.aiWorkspace,
baseBranch: "develop", repositoryUrl: f.remote, log,
});
const first = await prepare();
writeFileSync(path.join(f.workspaceDir, "TestSpec.md"), "keep the spec\n");
const originalSpawn = Bun.spawn.bind(Bun);
let retired = "";
const spawn = spyOn(Bun, "spawn").mockImplementation(((options: any) => {
const command = options.cmd as string[];
if (command.includes("/bin/rm") && command.at(-1)!.startsWith(path.dirname(f.workspaceDir))) {
retired = command.at(-1)!;
started.resolve();
return { exited: exited.promise, stderr: new Blob([]).stream(), kill() {} };
}
return originalSpawn(options);
}) as typeof Bun.spawn);
let next: Awaited<ReturnType<typeof prepare>> | undefined;
try {
await first.release();
await started.promise;
expect(existsSync(first.sourceDir)).toBe(false);
expect(existsSync(retired)).toBe(true);
next = await Promise.race([
prepare(),
Bun.sleep(2_000).then(() => { throw new Error("new clone blocked by old deletion"); }),
]);
rmSync(retired, { recursive: true, force: true });
exited.resolve(0);
await completed.promise;
expect(readFileSync(path.join(next.sourceDir, ".git/HEAD"), "utf8").trim()).toBe(f.commit);
expect(existsSync(path.join(f.workspaceDir, ".streamline-security-eval-source.json"))).toBe(true);
expect(readFileSync(path.join(f.workspaceDir, "TestSpec.md"), "utf8")).toBe("keep the spec\n");
} finally {
if (retired) rmSync(retired, { recursive: true, force: true });
exited.resolve(0);
spawn.mockRestore();
await next?.release();
}
});
test("failed background deletion preserves the result and is recovered on a later sweep", async () => {
const f = fixture();
const pending = Promise.withResolvers<void>();
const completed = Promise.withResolvers<void>();
const log = (_level: string, message: string) => {
if (message.includes("background source cleanup remains pending")) pending.resolve();
if (message.includes("background source cleanup completed")) completed.resolve();
};
const source = await prepareSecurityEvalSource({
security: security(f.commit), workspaceDir: f.workspaceDir, aiWorkspace: f.aiWorkspace,
baseBranch: "develop", repositoryUrl: f.remote, log,
});
writeFileSync(path.join(f.workspaceDir, "TestSpec.md"), "keep the spec\n");
const originalSpawn = Bun.spawn.bind(Bun);
const spawn = spyOn(Bun, "spawn").mockImplementation(((options: any) => {
const command = options.cmd as string[];
if (command.includes("/bin/rm") && command.at(-1)!.startsWith(path.dirname(f.workspaceDir))) {
return { exited: Promise.resolve(1), stderr: new Blob(["busy"]).stream(), kill() {} };
}
return originalSpawn(options);
}) as typeof Bun.spawn);
try {
await source.release();
await pending.promise;
expect(existsSync(source.sourceDir)).toBe(false);
expect(readFileSync(path.join(f.workspaceDir, "TestSpec.md"), "utf8")).toBe("keep the spec\n");
expect(readdirSync(path.join(path.dirname(f.workspaceDir), ".security-eval-source-gc"))).toHaveLength(1);
} finally {
spawn.mockRestore();
}
await pruneSecurityEvalSources(path.dirname(f.workspaceDir), new Set(), log);
await completed.promise;
expect(readdirSync(path.join(path.dirname(f.workspaceDir), ".security-eval-source-gc"))).toHaveLength(0);
});
});