Replace NodeJS dict aliases with maps and named field reads

Co-authored-by: Danny Kim <temeddix@gmail.com>
Co-committed-by: Danny Kim <temeddix@gmail.com>
This commit was merged in pull request #24.
This commit is contained in:
Danny Kim
2026-10-20 05:01:49 +00:00
committed by temeddix
parent 32806120b3
commit 063eb09750
42 changed files with 2386 additions and 982 deletions
+73 -46
View File
@@ -45,9 +45,15 @@ async function writeArtifacts(
review?: string,
anchors?: unknown,
): Promise<void> {
if (!outcome.ok) return;
if (review !== undefined) await writePrivate(outcome.reviewPath, review);
if (anchors !== undefined) await writePrivate(outcome.anchorsPath, JSON.stringify(anchors));
if (!outcome.ok) {
return;
}
if (review !== undefined) {
await writePrivate(outcome.reviewPath, review);
}
if (anchors !== undefined) {
await writePrivate(outcome.anchorsPath, JSON.stringify(anchors));
}
}
function manifestFor(runtimeDir: string, profileHome: string): RuntimeManifest {
@@ -59,7 +65,7 @@ function manifestFor(runtimeDir: string, profileHome: string): RuntimeManifest {
profiles: [
{
index: 0,
type: "claude",
type: "CLAUDE",
model: "test",
uid: currentUid(),
gid: currentGid(),
@@ -83,7 +89,7 @@ async function fixture(
const outcome: RuntimeOutcome = source.ok
? {
...source,
profile: { type: "codex", model: "gpt-test" },
profile: { type: "CODEX", model: "gpt-test" },
reviewPath: `${profileHome}/review.md`,
anchorsPath: `${profileHome}/anchors.json`,
}
@@ -106,7 +112,9 @@ const missing = async (path: string): Promise<boolean> => {
await stat(path);
return false;
} catch (error) {
if (isNotFound(error)) return true;
if (isNotFound(error)) {
return true;
}
throw error;
}
};
@@ -118,9 +126,10 @@ function requestBody(init?: RequestInit): unknown {
// A fake Gitea that records each request body and answers with `status`.
function recordingFetch(bodies: unknown[], status = 201): Fetcher {
return (_url, init) => {
return async (_url, init) => {
bodies.push(requestBody(init));
return Promise.resolve(new Response(null, { status }));
const resolved = await Promise.resolve(new Response(null, { status }));
return resolved;
};
}
@@ -128,14 +137,17 @@ test("posts a successful issue result and removes runtime credentials", async ()
const result = await fixture({ ok: true, body: "done" });
const requests: RecordedRequest[] = [];
await postRuntime(result.manifestPath, context, (url, init) => {
await postRuntime(result.manifestPath, context, async (url, init) => {
requests.push({ url, init });
return Promise.resolve(new Response(null, { status: 201 }));
const resolved = await Promise.resolve(new Response(null, { status: 201 }));
return resolved;
});
expect(requests.length).toBe(1);
const [request] = requests;
expect(request?.url).toStrictEqual(`${context.apiUrl}/repos/owner/repo/issues/7/comments`);
expect(request?.url).toStrictEqual(
`${context.apiUrl}/repos/owner/repo/issues/7/comments`,
);
expect(requestBody(request?.init)).toStrictEqual({
body: "done\n\n> Profile: `codex` · `gpt-test`",
});
@@ -147,9 +159,11 @@ test("posts a successful issue result and removes runtime credentials", async ()
});
test("retries a rejected inline review without anchors", async () => {
const result = await fixture({ ok: true, body: "ignored final response" }, "Approved\nReady", [
{ path: "run.ts", line: 1, side: "new", body: "Finding" },
]);
const result = await fixture(
{ ok: true, body: "ignored final response" },
"Approved\nReady",
[{ path: "run.ts", line: 1, side: "NEW", body: "Finding" }],
);
const bodies: unknown[] = [];
await postRuntime(
@@ -158,9 +172,12 @@ test("retries a rejected inline review without anchors", async () => {
...context,
eventName: "pull_request",
},
(_url, init) => {
async (_url, init) => {
bodies.push(requestBody(init));
return Promise.resolve(new Response(null, { status: bodies.length === 1 ? 422 : 200 }));
const resolved = await Promise.resolve(
new Response(null, { status: bodies.length === 1 ? 422 : 200 }),
);
return resolved;
},
);
@@ -178,14 +195,14 @@ test("reports each tried profile and keeps the action failed", async () => {
const result = await fixture({
ok: false,
failures: [
{ type: "codex", model: "gpt-test", reason: "sign-in failed" },
{ type: "claude", model: "claude-test", reason: "usage exhausted" },
{ type: "opencode", model: "openrouter/test", reason: "failed" },
{ type: "CODEX", model: "gpt-test", reason: "SIGN_IN_FAILED" },
{ type: "CLAUDE", model: "claude-test", reason: "USAGE_EXHAUSTED" },
{ type: "OPENCODE", model: "openrouter/test", reason: "FAILED" },
],
});
const bodies: unknown[] = [];
await expect(() =>
await expect(
postRuntime(result.manifestPath, context, recordingFetch(bodies)),
).rejects.toThrow("agent phase failed");
@@ -208,15 +225,15 @@ test("drops failure entries that are not known labels", async () => {
JSON.stringify({
ok: false,
failures: [
{ type: "codex", model: "gpt-test", reason: "token sk-secret leaked" },
{ type: "shell", model: "x", reason: "failed" },
{ type: "claude", model: "claude-test", reason: "sign-in failed" },
{ type: "CODEX", model: "gpt-test", reason: "token sk-secret leaked" },
{ type: "shell", model: "x", reason: "FAILED" },
{ type: "CLAUDE", model: "claude-test", reason: "SIGN_IN_FAILED" },
],
}),
);
const bodies: unknown[] = [];
await expect(() =>
await expect(
postRuntime(result.manifestPath, context, recordingFetch(bodies)),
).rejects.toThrow("agent phase failed");
@@ -232,7 +249,7 @@ test("reports a generic failure when the runner wrote no outcome", async () => {
await rm(result.outcomeFile);
const bodies: unknown[] = [];
await expect(() =>
await expect(
postRuntime(result.manifestPath, context, recordingFetch(bodies)),
).rejects.toThrow("agent phase failed");
@@ -243,10 +260,13 @@ test("reports a generic failure when the runner wrote no outcome", async () => {
test("removes runtime credentials when posting fails", async () => {
const result = await fixture({ ok: true, body: "done" });
await expect(() =>
postRuntime(result.manifestPath, context, () =>
Promise.resolve(new Response("failed", { status: 500 })),
),
await expect(
postRuntime(result.manifestPath, context, async () => {
const resolved = await Promise.resolve(
new Response("failed", { status: 500 }),
);
return resolved;
}),
).rejects.toThrow("500");
assert(await missing(result.runtimeDir));
});
@@ -255,10 +275,11 @@ test("preserves a Gitea transport failure without retrying a comment", async ()
const result = await fixture({ ok: true, body: "done" });
let requests = 0;
await expect(() =>
postRuntime(result.manifestPath, context, () => {
await expect(
postRuntime(result.manifestPath, context, async () => {
requests += 1;
return Promise.reject(new Error(`network failure ${requests}`));
await Promise.resolve();
throw new Error(`network failure ${requests}`);
}),
).rejects.toThrow("network failure 1");
@@ -266,27 +287,33 @@ test("preserves a Gitea transport failure without retrying a comment", async ()
assert(await missing(result.runtimeDir));
});
test.skipIf(IS_WINDOWS)("refuses to follow an agent artifact symlink", async () => {
const result = await fixture({ ok: true, body: "done" });
await symlink(result.reviewerFile, result.reviewPath);
let requests = 0;
test.skipIf(IS_WINDOWS)(
"refuses to follow an agent artifact symlink",
async () => {
const result = await fixture({ ok: true, body: "done" });
await symlink(result.reviewerFile, result.reviewPath);
let requests = 0;
await expect(() =>
postRuntime(result.manifestPath, context, () => {
requests += 1;
return Promise.resolve(new Response(null, { status: 200 }));
}),
).rejects.toThrow("regular private file");
await expect(
postRuntime(result.manifestPath, context, async () => {
requests += 1;
const resolved = await Promise.resolve(
new Response(null, { status: 200 }),
);
return resolved;
}),
).rejects.toThrow("regular private file");
expect(requests).toBe(1);
assert(await missing(result.runtimeDir));
});
expect(requests).toBe(1);
assert(await missing(result.runtimeDir));
},
);
test("reports an attributed failure when a pull request review is missing", async () => {
const result = await fixture({ ok: true, body: "done" });
const bodies: unknown[] = [];
await expect(() =>
await expect(
postRuntime(
result.manifestPath,
{