Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions pm-stack/aggregator/scripts/record-claude-launch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,25 @@ import { appendFile, mkdir } from "node:fs/promises";
import { homedir } from "node:os";
import { dirname, join } from "node:path";

function usageError(message: string): never {
console.error(`record-claude-launch: ${message}`);
process.exit(2);
}

/** The token after `--<name>`, refused when it is missing or is itself a flag:
* a flag token accepted as a value would be consumed a second time as a flag
* in its own right, and the corrupted record appended with exit 0 (#3415). */
function valueAfter(index: number, name: string): string {
const value = process.argv[index + 1];
if (value === undefined || value.startsWith("--")) {
usageError(`--${name} requires a value that is not a flag`);
}
return value;
}

function arg(name: string): string | undefined {
const i = process.argv.indexOf(`--${name}`);
return i >= 0 ? process.argv[i + 1] : undefined;
return i >= 0 ? valueAfter(i, name) : undefined;
}

const file =
Expand All @@ -41,9 +57,7 @@ if (!label) {

const now = new Date();
const id = arg("id") ?? `claude-${now.getTime().toString(36)}-${Math.random().toString(36).slice(2, 7)}`;
const taskRefs = process.argv
.map((a, i) => (a === "--task" ? process.argv[i + 1] : undefined))
.filter((v): v is string => typeof v === "string");
const taskRefs = process.argv.flatMap((a, i) => (a === "--task" ? [valueAfter(i, "task")] : []));

const record = {
id,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Pinning test for #3415: record-claude-launch must refuse a value-taking
// option whose next token is missing or is itself a flag — with a usage
// error (exit 2) and NO record appended — instead of accepting the flag
// token as the value, consuming it a second time as a flag, and appending
// a corrupted launch record with exit 0. Control behaviour is unchanged.
import { describe, expect, test } from "bun:test";
import { mkdtemp, readFile, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";

const script = join(import.meta.dir, "../scripts/record-claude-launch.ts");

async function runScript(
args: string[],
): Promise<{ code: number; stderr: string; stdout: string }> {
const proc = Bun.spawn([process.execPath, script, ...args], {
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr] = await Promise.all([
new Response(proc.stdout).text(),
new Response(proc.stderr).text(),
]);
return { code: await proc.exited, stdout, stderr };
}

describe("record-claude-launch flag-as-value refusal (#3415)", () => {
test("flag-as-value and missing values exit 2 and append nothing", async () => {
const dir = await mkdtemp(join(tmpdir(), "record-launch-3415-"));
try {
const out = join(dir, "records.jsonl");
const badArgSets: string[][] = [
// --task takes the --model flag as its value (taskRefs ['--model'])
["--file", out, "--label", "Slice", "--task", "--model", "gpt-x"],
// --label takes the --id flag as its value (a session named '--id')
["--file", out, "--label", "--id", "claude-x"],
// --model takes the --file flag as its value
["--file", out, "--label", "Slice", "--model", "--file", join(dir, "other.jsonl")],
// a value flag in last position has no value at all
["--file", out, "--label", "Slice", "--model"],
["--file", out, "--label"],
];
for (const args of badArgSets) {
const { code, stderr } = await runScript(args);
expect(code).toBe(2);
expect(stderr).toContain("requires a value");
}
// No invocation above may have appended a record anywhere.
await expect(readFile(out, "utf8")).rejects.toThrow();
await expect(readFile(join(dir, "other.jsonl"), "utf8")).rejects.toThrow();
} finally {
await rm(dir, { recursive: true, force: true });
}
});

test("control: a normal invocation records exactly the given label and refs", async () => {
const dir = await mkdtemp(join(tmpdir(), "record-launch-3415-"));
try {
const out = join(dir, "records.jsonl");
const { code } = await runScript([
"--file", out,
"--label", "Slice 3 builder",
"--id", "claude-ctl",
"--task", "developing-today/code#123",
"--task", "developing-today/code#456",
"--model", "claude-x",
]);
expect(code).toBe(0);
const lines = (await readFile(out, "utf8")).trim().split("\n");
expect(lines).toHaveLength(1);
const record = JSON.parse(lines[0]);
expect(record.label).toBe("Slice 3 builder");
expect(record.id).toBe("claude-ctl");
expect(record.model).toBe("claude-x");
expect(record.taskRefs).toEqual(["developing-today/code#123", "developing-today/code#456"]);
} finally {
await rm(dir, { recursive: true, force: true });
}
});
});
Loading