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
7 changes: 2 additions & 5 deletions src/codex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,15 @@ async function runCodex(
config: ResolvedProjectConfig,
args: string[],
): Promise<CodexResult> {
const isDevMode =
process.env.PIV_DEV_MODE === "1" ||
process.env.PIV_PRINT_CODEX_LOGS === "1";
const outputFile = args[args.indexOf("--output-last-message") + 1] ?? "";
const envOverrides = config.codex.codexHome
? { CODEX_HOME: config.codex.codexHome }
: {};
const result = await runCommand(config.codex.binary, args, {
cwd: config.executionPath,
env: envOverrides,
streamStdout: isDevMode,
streamStderr: isDevMode,
streamStdout: config.codex.streamLogs,
streamStderr: config.codex.streamLogs,
stdinMode: "ignore",
});

Expand Down
87 changes: 87 additions & 0 deletions src/comments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import type { BugRecord } from "./types";

export interface TokenUsage {
inputTokens?: number;
outputTokens?: number;
totalTokens?: number;
}

export function formatCodexUsageLine(usage?: TokenUsage): string {
if (!usage) {
return "Token usage: unknown";
}
const input = usage.inputTokens ?? "unknown";
const output = usage.outputTokens ?? "unknown";
const total =
usage.totalTokens ??
(typeof usage.inputTokens === "number" &&
typeof usage.outputTokens === "number"
? usage.inputTokens + usage.outputTokens
: "unknown");
return `Token usage: input ${input}, output ${output}, total ${total}`;
}

export function buildPlanComment(
issueKey: string,
planSummary: string,
usage?: TokenUsage,
): string {
const maxSummaryLength = 6000;
const normalized = planSummary.trim();
const truncated =
normalized.length > maxSummaryLength
? `${normalized.slice(0, maxSummaryLength)}\n\n[truncated]`
: normalized;

return [
`PIV loop plan for ${issueKey}`,
"",
"Planning completed; implementation started.",
"",
formatCodexUsageLine(usage),
"",
"Plan:",
truncated || "(No plan summary returned by planning agent.)",
].join("\n");
}

export function buildImplementationComment(
draftPrUrl: string | undefined,
usage?: TokenUsage,
): string {
return [
`Implementation completed. Draft PR: ${draftPrUrl ?? "(created)"}`,
formatCodexUsageLine(usage),
].join("\n");
}

export function buildReviewComment(input: {
issueKey: string;
passed: boolean;
summary: string;
usage?: TokenUsage;
bugs: BugRecord[];
}): string {
return [
`PIV loop review for ${input.issueKey}`,
"",
`Result: ${input.passed ? "PASS" : "FAIL"}`,
"",
formatCodexUsageLine(input.usage),
"",
input.summary,
"",
input.bugs.length > 0
? "Bugs were detected and converted to GitHub issues."
: "No bugs found.",
].join("\n");
}

export function buildBugsCanceledComment(bugs: BugRecord[]): string {
return [
"Review/testing found bugs. Moved issue to Canceled.",
...bugs.map(
(bug) => `- ${bug.title}${bug.issueUrl ? ` (${bug.issueUrl})` : ""}`,
),
].join("\n");
}
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ function buildEnvBase(cwd: string): ProjectRuntimeConfig {
},
codex: {
binary: env.CODEX_BINARY ?? "codex",
streamLogs: env.PIV_DEV_MODE === "1" || env.PIV_PRINT_CODEX_LOGS === "1",
model: env.CODEX_MODEL,
models: {
plan: env.CODEX_MODEL_PLAN,
Expand Down
74 changes: 74 additions & 0 deletions src/review.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import type { BugRecord } from "./types";

export interface ReviewOutcome {
passed: boolean;
summary: string;
bugs: BugRecord[];
}

export function parseReviewOutcome(text: string): ReviewOutcome {
const upper = text.toUpperCase();
const passed =
upper.includes("RESULT: PASS") && !upper.includes("RESULT: FAIL");
const summary = extractSummary(text);
const bugs = extractBugs(text);
return {
passed: passed && bugs.length === 0,
summary,
bugs,
};
}

function extractSummary(text: string): string {
const match = text.match(/SUMMARY:\s*([\s\S]*?)(?:\nBUGS_JSON:|$)/i);
if (match?.[1]) {
return match[1].trim();
}
return text.trim().slice(0, 1200);
}

function extractBugs(text: string): BugRecord[] {
const jsonFromLabel = text.match(/BUGS_JSON:\s*([\s\S]*)$/i)?.[1]?.trim();
if (jsonFromLabel) {
const parsed = parseBugJson(jsonFromLabel);
if (parsed.length > 0) {
return parsed;
}
}

const fenced = text.match(/```json\s*([\s\S]*?)```/i)?.[1]?.trim();
if (fenced) {
const parsed = parseBugJson(fenced);
if (parsed.length > 0) {
return parsed;
}
}

return [];
}

function parseBugJson(input: string): BugRecord[] {
try {
const parsed = JSON.parse(input) as unknown;
if (Array.isArray(parsed)) {
return parsed
.map((item) => {
if (!item || typeof item !== "object") {
return null;
}
const bug = item as Record<string, unknown>;
if (typeof bug.title !== "string" || typeof bug.body !== "string") {
return null;
}
return {
title: bug.title,
body: bug.body,
} satisfies BugRecord;
})
.filter((item): item is BugRecord => item !== null);
}
} catch {
return [];
}
return [];
}
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export interface ProjectRuntimeConfig {
};
codex: {
binary: string;
streamLogs: boolean;
model?: string;
models?: {
plan?: string;
Expand Down
Loading
Loading