Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e2af609845 |
@@ -10,6 +10,39 @@ export function openCodeArgs(model: string, prompt: string): string[] {
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ChunkWriter = (chunk: Uint8Array) => void | Promise<void>;
|
||||||
|
|
||||||
|
export async function collectOpenCodeOutput(
|
||||||
|
stdout: ReadableStream<Uint8Array>,
|
||||||
|
stderr: ReadableStream<Uint8Array>,
|
||||||
|
writeStdout: ChunkWriter,
|
||||||
|
writeStderr: ChunkWriter,
|
||||||
|
): Promise<{ stdout: string; stderr: string }> {
|
||||||
|
const collect = async (
|
||||||
|
stream: ReadableStream<Uint8Array>,
|
||||||
|
write: ChunkWriter,
|
||||||
|
) => {
|
||||||
|
const chunks: Uint8Array[] = [];
|
||||||
|
for await (const chunk of stream) {
|
||||||
|
chunks.push(chunk);
|
||||||
|
await write(chunk);
|
||||||
|
}
|
||||||
|
const length = chunks.reduce((total, chunk) => total + chunk.length, 0);
|
||||||
|
const joined = new Uint8Array(length);
|
||||||
|
let offset = 0;
|
||||||
|
for (const chunk of chunks) {
|
||||||
|
joined.set(chunk, offset);
|
||||||
|
offset += chunk.length;
|
||||||
|
}
|
||||||
|
return new TextDecoder().decode(joined);
|
||||||
|
};
|
||||||
|
const [output, error] = await Promise.all([
|
||||||
|
collect(stdout, writeStdout),
|
||||||
|
collect(stderr, writeStderr),
|
||||||
|
]);
|
||||||
|
return { stdout: output, stderr: error };
|
||||||
|
}
|
||||||
|
|
||||||
export function parseOpenCodeEvents(output: string): string {
|
export function parseOpenCodeEvents(output: string): string {
|
||||||
let result: string | undefined;
|
let result: string | undefined;
|
||||||
for (const line of output.split("\n")) {
|
for (const line of output.split("\n")) {
|
||||||
|
|||||||
+37
-1
@@ -1,5 +1,9 @@
|
|||||||
import { assertEquals, assertRejects } from "jsr:@std/assert@1";
|
import { assertEquals, assertRejects } from "jsr:@std/assert@1";
|
||||||
import { openCodeArgs, parseOpenCodeEvents } from "./opencode.ts";
|
import {
|
||||||
|
collectOpenCodeOutput,
|
||||||
|
openCodeArgs,
|
||||||
|
parseOpenCodeEvents,
|
||||||
|
} from "./opencode.ts";
|
||||||
|
|
||||||
Deno.test("builds a non-interactive auto-approved OpenCode command", () => {
|
Deno.test("builds a non-interactive auto-approved OpenCode command", () => {
|
||||||
assertEquals(
|
assertEquals(
|
||||||
@@ -38,3 +42,35 @@ Deno.test("rejects a successful OpenCode run without visible text", async () =>
|
|||||||
"no text output",
|
"no text output",
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test("streams OpenCode output while collecting it", async () => {
|
||||||
|
const encode = (value: string) => new TextEncoder().encode(value);
|
||||||
|
const stdout = new ReadableStream<Uint8Array>({
|
||||||
|
start(controller) {
|
||||||
|
controller.enqueue(encode("first\n"));
|
||||||
|
controller.enqueue(encode("second\n"));
|
||||||
|
controller.close();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const stderr = new ReadableStream<Uint8Array>({
|
||||||
|
start(controller) {
|
||||||
|
controller.enqueue(encode("warning\n"));
|
||||||
|
controller.close();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const shown: string[] = [];
|
||||||
|
|
||||||
|
const result = await collectOpenCodeOutput(
|
||||||
|
stdout,
|
||||||
|
stderr,
|
||||||
|
(chunk: Uint8Array) => {
|
||||||
|
shown.push(`out:${new TextDecoder().decode(chunk)}`);
|
||||||
|
},
|
||||||
|
(chunk: Uint8Array) => {
|
||||||
|
shown.push(`err:${new TextDecoder().decode(chunk)}`);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(result, { stdout: "first\nsecond\n", stderr: "warning\n" });
|
||||||
|
assertEquals(shown, ["out:first\n", "err:warning\n", "out:second\n"]);
|
||||||
|
});
|
||||||
|
|||||||
+14
-7
@@ -26,6 +26,9 @@ const VERDICTS: Record<string, [event: string, mark: string]> = {
|
|||||||
Approved: ["APPROVED", "✅"],
|
Approved: ["APPROVED", "✅"],
|
||||||
"Changes requested": ["REQUEST_CHANGES", "🛑"],
|
"Changes requested": ["REQUEST_CHANGES", "🛑"],
|
||||||
};
|
};
|
||||||
|
const FAILURE = "AI bot failed.\n\n> Profile: none · all profiles failed";
|
||||||
|
|
||||||
|
class GiteaRequestError extends Error {}
|
||||||
|
|
||||||
function parseAnchors(text: string, sourcePath: string): Anchor[] {
|
function parseAnchors(text: string, sourcePath: string): Anchor[] {
|
||||||
const entries: unknown = JSON.parse(text);
|
const entries: unknown = JSON.parse(text);
|
||||||
@@ -128,7 +131,9 @@ export async function postRuntime(
|
|||||||
body: JSON.stringify(body),
|
body: JSON.stringify(body),
|
||||||
});
|
});
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`${path}: ${response.status} ${await response.text()}`);
|
throw new GiteaRequestError(
|
||||||
|
`${path}: ${response.status} ${await response.text()}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const postComment = (body: string) =>
|
const postComment = (body: string) =>
|
||||||
@@ -141,18 +146,15 @@ export async function postRuntime(
|
|||||||
try {
|
try {
|
||||||
outcome = await readOutcome(manifest.outcomeFile);
|
outcome = await readOutcome(manifest.outcomeFile);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
await postComment(
|
await postComment(FAILURE);
|
||||||
"AI bot failed.\n\n> Profile: none · all profiles failed",
|
|
||||||
);
|
|
||||||
throw new Error("agent phase failed", { cause: error });
|
throw new Error("agent phase failed", { cause: error });
|
||||||
}
|
}
|
||||||
if (!outcome.ok) {
|
if (!outcome.ok) {
|
||||||
await postComment(
|
await postComment(FAILURE);
|
||||||
"AI bot failed.\n\n> Profile: none · all profiles failed",
|
|
||||||
);
|
|
||||||
throw new Error("agent phase failed");
|
throw new Error("agent phase failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
const review = await readAgentArtifact(
|
const review = await readAgentArtifact(
|
||||||
manifest,
|
manifest,
|
||||||
outcome.reviewPath,
|
outcome.reviewPath,
|
||||||
@@ -198,6 +200,11 @@ export async function postRuntime(
|
|||||||
console.error(`inline comments rejected: ${error.message}`);
|
console.error(`inline comments rejected: ${error.message}`);
|
||||||
await postReview([]);
|
await postReview([]);
|
||||||
});
|
});
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof GiteaRequestError) throw error;
|
||||||
|
await postComment(FAILURE);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
await Deno.remove(manifest.runtimeDir, { recursive: true }).catch(
|
await Deno.remove(manifest.runtimeDir, { recursive: true }).catch(
|
||||||
(error) => {
|
(error) => {
|
||||||
|
|||||||
+24
-1
@@ -212,6 +212,29 @@ Deno.test("refuses to follow an agent artifact symlink", async () => {
|
|||||||
"regular private file",
|
"regular private file",
|
||||||
);
|
);
|
||||||
|
|
||||||
assertEquals(requests, 0);
|
assertEquals(requests, 1);
|
||||||
|
assert(await missing(result.runtimeDir));
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.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 assertRejects(
|
||||||
|
() =>
|
||||||
|
postRuntime(result.manifestPath, {
|
||||||
|
...context,
|
||||||
|
eventName: "pull_request",
|
||||||
|
}, (_url, init) => {
|
||||||
|
bodies.push(JSON.parse(String(init?.body)));
|
||||||
|
return Promise.resolve(new Response(null, { status: 201 }));
|
||||||
|
}),
|
||||||
|
Error,
|
||||||
|
"did not write",
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(bodies, [{
|
||||||
|
body: "AI bot failed.\n\n> Profile: none · all profiles failed",
|
||||||
|
}]);
|
||||||
assert(await missing(result.runtimeDir));
|
assert(await missing(result.runtimeDir));
|
||||||
});
|
});
|
||||||
|
|||||||
+25
-11
@@ -19,7 +19,11 @@ import {
|
|||||||
sanitizedEnvironment,
|
sanitizedEnvironment,
|
||||||
withProfile,
|
withProfile,
|
||||||
} from "./profiles.ts";
|
} from "./profiles.ts";
|
||||||
import { openCodeArgs, parseOpenCodeEvents } from "./opencode.ts";
|
import {
|
||||||
|
collectOpenCodeOutput,
|
||||||
|
openCodeArgs,
|
||||||
|
parseOpenCodeEvents,
|
||||||
|
} from "./opencode.ts";
|
||||||
import {
|
import {
|
||||||
readRuntimeManifest,
|
readRuntimeManifest,
|
||||||
type RuntimeOutcome,
|
type RuntimeOutcome,
|
||||||
@@ -112,7 +116,6 @@ async function runAgentCommand(
|
|||||||
|
|
||||||
async function prepareProfile(
|
async function prepareProfile(
|
||||||
profile: RuntimeProfile,
|
profile: RuntimeProfile,
|
||||||
token: string | undefined,
|
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
await runAgentCommand(profile, "git", [
|
await runAgentCommand(profile, "git", [
|
||||||
"config",
|
"config",
|
||||||
@@ -120,7 +123,7 @@ async function prepareProfile(
|
|||||||
"--add",
|
"--add",
|
||||||
"safe.directory",
|
"safe.directory",
|
||||||
WORKSPACE,
|
WORKSPACE,
|
||||||
], token);
|
]);
|
||||||
await runAgentCommand(profile, "npx", [
|
await runAgentCommand(profile, "npx", [
|
||||||
"-y",
|
"-y",
|
||||||
...SKILLS_INSTALL.split(" "),
|
...SKILLS_INSTALL.split(" "),
|
||||||
@@ -128,7 +131,7 @@ async function prepareProfile(
|
|||||||
"--agent",
|
"--agent",
|
||||||
profile.type === "claude" ? "claude-code" : profile.type,
|
profile.type === "claude" ? "claude-code" : profile.type,
|
||||||
"--yes",
|
"--yes",
|
||||||
], token);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function renderPrompt(
|
async function renderPrompt(
|
||||||
@@ -365,7 +368,7 @@ async function runOpenCode(
|
|||||||
token: string,
|
token: string,
|
||||||
prompt: string,
|
prompt: string,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const result = await agentCommand(
|
const process = agentCommand(
|
||||||
profile,
|
profile,
|
||||||
"opencode",
|
"opencode",
|
||||||
openCodeArgs(profile.model, prompt),
|
openCodeArgs(profile.model, prompt),
|
||||||
@@ -376,12 +379,23 @@ async function runOpenCode(
|
|||||||
stdout: "piped",
|
stdout: "piped",
|
||||||
stderr: "piped",
|
stderr: "piped",
|
||||||
},
|
},
|
||||||
).output();
|
).spawn();
|
||||||
const error = new TextDecoder().decode(result.stderr).trim();
|
const output = collectOpenCodeOutput(
|
||||||
if (!result.success) {
|
process.stdout,
|
||||||
throw new Error(error || `opencode exited with ${result.code}`);
|
process.stderr,
|
||||||
|
async (chunk) => {
|
||||||
|
await Deno.stdout.write(chunk);
|
||||||
|
},
|
||||||
|
async (chunk) => {
|
||||||
|
await Deno.stderr.write(chunk);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
const [status, streams] = await Promise.all([process.status, output]);
|
||||||
|
const error = streams.stderr.trim();
|
||||||
|
if (!status.success) {
|
||||||
|
throw new Error(error || `opencode exited with ${status.code}`);
|
||||||
}
|
}
|
||||||
return parseOpenCodeEvents(new TextDecoder().decode(result.stdout));
|
return parseOpenCodeEvents(streams.stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function runProfile(profile: RuntimeProfile): Promise<AttemptResult> {
|
async function runProfile(profile: RuntimeProfile): Promise<AttemptResult> {
|
||||||
@@ -403,7 +417,7 @@ async function runProfile(profile: RuntimeProfile): Promise<AttemptResult> {
|
|||||||
profile.gid,
|
profile.gid,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
await prepareProfile(profile, token);
|
await prepareProfile(profile);
|
||||||
const rendered = await renderPrompt(profile);
|
const rendered = await renderPrompt(profile);
|
||||||
reviewPath = rendered.reviewPath;
|
reviewPath = rendered.reviewPath;
|
||||||
anchorsPath = rendered.anchorsPath;
|
anchorsPath = rendered.anchorsPath;
|
||||||
|
|||||||
@@ -169,6 +169,12 @@ Deno.test("installs the OpenCode CLI", async () => {
|
|||||||
assert(action.includes("opencode-ai"));
|
assert(action.includes("opencode-ai"));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test("profile preparation does not receive provider credentials", async () => {
|
||||||
|
const runner = await Deno.readTextFile("src/run.ts");
|
||||||
|
assert(runner.includes("await prepareProfile(profile);"));
|
||||||
|
assertEquals(runner.includes("prepareProfile(profile, token)"), false);
|
||||||
|
});
|
||||||
|
|
||||||
Deno.test("central profiles fall back from Codex to Claude to OpenCode", async () => {
|
Deno.test("central profiles fall back from Codex to Claude to OpenCode", async () => {
|
||||||
const credentials = await yaml("profiles.yml");
|
const credentials = await yaml("profiles.yml");
|
||||||
const profiles = sequence(credentials.profiles, "profiles");
|
const profiles = sequence(credentials.profiles, "profiles");
|
||||||
|
|||||||
Reference in New Issue
Block a user