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
6 changes: 6 additions & 0 deletions docs/schemas/plans/pipeline.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
"additionalProperties": false
}
},
"name": {
"type": "string"
},
"setup": {
"anyOf": [
{
Expand Down Expand Up @@ -121,6 +124,9 @@
}
]
},
"state": {
"type": "string"
},
"steps": {
"type": "object",
"additionalProperties": {
Expand Down
181 changes: 133 additions & 48 deletions packages/agent-code-review/src/mcp.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { describe, expect, it, vi } from "vitest";
import { createCodeReviewState } from "./review-store.js";
import { spawn } from "@poe-code/agent-spawn";
import { createCodeReviewAgentMcpConfig, createCodeReviewAgentMcpGroup } from "./mcp.js";

vi.mock("@poe-code/agent-spawn", async (importOriginal) => ({
...(await importOriginal<typeof import("@poe-code/agent-spawn")>()),
spawn: vi.fn(async () => ({ stdout: "", stderr: "", exitCode: 0 }))
}));

describe("createCodeReviewAgentMcpConfig", () => {
it("launches the MCP server through the shipped root executable", () => {
expect(
Expand Down Expand Up @@ -35,59 +41,138 @@ describe("createCodeReviewAgentMcpConfig", () => {
it.each([
["audit logging", { appendFailure: new Error("Store unavailable") }, "Store unavailable"],
["PR loading", { fetchFailure: new Error("GitHub unavailable") }, "GitHub unavailable"]
])("marks %s failures as failed instead of leaving pending state", async (_label, failure, message) => {
const states: Array<{ status: string; error?: string }> = [];
const state = createCodeReviewState({
sessionId: "session-1",
prUrl: "https://github.com/acme/repo/pull/1",
selectedAgent: "codex",
selectedProfiles: ["generic"]
});
const group = createCodeReviewAgentMcpGroup(
{
role: "orchestrator",
session: "session-1",
actor: "orchestrator",
cwd: "/repo",
agent: "codex"
},
{
store: {
read: vi.fn(async () => state),
addSubagent: vi.fn(async (_pr, _actor, status) => {
states.push(status);
return state;
}),
updateSubagent: vi.fn(async (_pr, _actor, status) => {
states.push(status);
return state;
}),
appendOrchestratorAction: vi.fn(async () => {
if ("appendFailure" in failure) throw failure.appendFailure;
return state;
])(
"marks %s failures as failed instead of leaving pending state",
async (_label, failure, message) => {
const states: Array<{ status: string; error?: string }> = [];
const state = createCodeReviewState({
sessionId: "session-1",
prUrl: "https://github.com/acme/repo/pull/1",
selectedAgent: "codex",
selectedProfiles: ["generic"]
});
const group = createCodeReviewAgentMcpGroup(
{
role: "orchestrator",
session: "session-1",
actor: "orchestrator",
cwd: "/repo",
agent: "codex"
},
{
store: {
read: vi.fn(async () => state),
addSubagent: vi.fn(async (_pr, _actor, status) => {
states.push(status);
return state;
}),
updateSubagent: vi.fn(async (_pr, _actor, status) => {
states.push(status);
return state;
}),
appendOrchestratorAction: vi.fn(async () => {
if ("appendFailure" in failure) throw failure.appendFailure;
return state;
})
} as never,
fetchPr: vi.fn(async () => {
if ("fetchFailure" in failure) throw failure.fetchFailure;
return {};
})
} as never,
fetchPr: vi.fn(async () => {
if ("fetchFailure" in failure) throw failure.fetchFailure;
return {};
})
}
);
const spawnCommand = group.children.find(({ name }) => name === "code_review_agent_spawn");
}
);
const spawnCommand = group.children.find(({ name }) => name === "code_review_agent_spawn");

expect(spawnCommand).toBeDefined();
await expect(
spawnCommand?.handler({
params: { pr: "https://github.com/acme/repo/pull/1", profile: "generic" }
} as never)
).resolves.toEqual({ actor: "generic", agent: "codex", status: "pending" });
await vi.waitFor(() => {
expect(states.at(-1)).toMatchObject({ status: "failed", error: message });
});
});
expect(spawnCommand).toBeDefined();
await expect(
spawnCommand?.handler({
params: { pr: "https://github.com/acme/repo/pull/1", profile: "generic" }
} as never)
).resolves.toEqual({ actor: "generic", agent: "codex", status: "pending" });
await vi.waitFor(() => {
expect(states.at(-1)).toMatchObject({ status: "failed", error: message });
});
}
);
});

describe("createCodeReviewAgentMcpGroup orchestrator tools", () => {
it.each(["codex", "claude-code", "claude", "CLAUDE"])(
"spawns %s nested reviewers using stdin-safe prompt transport",
async (agent) => {
const state = createCodeReviewState({
sessionId: "session-1",
prUrl: "https://github.com/acme/repo/pull/1",
selectedAgent: agent,
selectedProfiles: ["generic"]
});
const group = createCodeReviewAgentMcpGroup(
{
role: "orchestrator",
session: "session-1",
actor: "orchestrator",
cwd: "/repo",
agent
},
{
store: {
read: vi.fn(async () => state),
addSubagent: vi.fn(async () => state),
updateSubagent: vi.fn(async () => state),
appendOrchestratorAction: vi.fn(async () => state)
} as never,
fetchPr: vi.fn(async () => ({}))
}
);
const command = group.children.find(({ name }) => name === "code_review_agent_spawn");

vi.mocked(spawn).mockClear();
await command?.handler({
params: { pr: "https://github.com/acme/repo/pull/1", profile: "generic" }
} as never);
await vi.waitFor(() => expect(spawn).toHaveBeenCalled());
expect(spawn).toHaveBeenCalledWith(agent, expect.objectContaining({ useStdin: true }));
}
);

it.each(["kimi", "goose"])(
"does not pass raw text stdin to the %s structured-input protocol",
async (agent) => {
const state = createCodeReviewState({
sessionId: "session-1",
prUrl: "https://github.com/acme/repo/pull/1",
selectedAgent: agent,
selectedProfiles: ["generic"]
});
const group = createCodeReviewAgentMcpGroup(
{
role: "orchestrator",
session: "session-1",
actor: "orchestrator",
cwd: "/repo",
agent
},
{
store: {
read: vi.fn(async () => state),
addSubagent: vi.fn(async () => state),
updateSubagent: vi.fn(async () => state),
appendOrchestratorAction: vi.fn(async () => state)
} as never,
fetchPr: vi.fn(async () => ({}))
}
);
const command = group.children.find(({ name }) => name === "code_review_agent_spawn");

vi.mocked(spawn).mockClear();
await command?.handler({
params: { pr: "https://github.com/acme/repo/pull/1", profile: "generic" }
} as never);
await vi.waitFor(() => expect(spawn).toHaveBeenCalled());
expect(spawn).toHaveBeenCalledWith(agent, expect.not.objectContaining({ useStdin: true }));
}
);

it("exposes local merged-draft edit, delete, and discard commands", async () => {
const state = createCodeReviewState({
sessionId: "session-1",
Expand Down
7 changes: 6 additions & 1 deletion packages/agent-code-review/src/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
CodeReviewDraft,
CodeReviewInlineComment
} from "./review-state.js";
import { shouldUseTextStdinForCodeReview } from "./prompt-transport.js";
import { CodeReviewYamlStore, resolveCodeReviewStoreDirectory } from "./review-store.js";

export const CODE_REVIEW_AGENT_MCP_ROLES = ["agent", "orchestrator", "subagent"] as const;
Expand Down Expand Up @@ -491,7 +492,11 @@ async function spawnWithPoeCode(
mcpServers: Record<string, { command: string; args: string[] }>;
}
): Promise<SpawnResult> {
return spawn(agent, { prompt, ...options });
return spawn(agent, {
prompt,
...options,
...(shouldUseTextStdinForCodeReview(agent) ? { useStdin: true } : {})
});
}

function renderSubagentPrompt(input: {
Expand Down
6 changes: 6 additions & 0 deletions packages/agent-code-review/src/prompt-transport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { getSpawnConfig } from "@poe-code/agent-spawn";

export function shouldUseTextStdinForCodeReview(agent: string): boolean {
const config = getSpawnConfig(agent);
return config?.kind === "cli" && (config.agentId === "codex" || config.agentId === "claude-code");
}
Comment on lines +3 to +6

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agent is still the raw configured value here. The repo accepts aliases like claude and resolves them to claude-code in @poe-code/agent-defs; spawn() also resolves aliases later, but this check runs before that. With --agent claude/POE_DEFAULT_AGENT=claude, the large code-review prompt still goes through argv and can hit the same E2BIG failure. Please normalize before deciding stdin eligibility (and cover the alias case in tests).

66 changes: 61 additions & 5 deletions packages/agent-code-review/src/review.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { describe, expect, it, vi } from "vitest";
import type { CodeReviewYamlStore } from "./review-store.js";
import { createCodeReviewState } from "./review-store.js";
import { runCodeReview } from "./review.js";
import { spawn } from "@poe-code/agent-spawn";

vi.mock("@poe-code/agent-spawn", async (importOriginal) => ({
...(await importOriginal<typeof import("@poe-code/agent-spawn")>()),
spawn: vi.fn(async () => ({ stdout: "", stderr: "", exitCode: 0 }))
}));

const assetMocks = vi.hoisted(() => ({
loadProfile: vi.fn(async () => "custom profile"),
Expand All @@ -13,11 +19,13 @@ vi.mock("./assets.js", async (importOriginal) => {
const actual = await importOriginal<typeof import("./assets.js")>();
return {
...actual,
discoverCodeReviewProfiles: vi.fn(async () => [{
name: "generic",
content: "generic profile",
source: "built-in" as const
}]),
discoverCodeReviewProfiles: vi.fn(async () => [
{
name: "generic",
content: "generic profile",
source: "built-in" as const
}
]),
loadCodeReviewProfile: assetMocks.loadProfile,
loadCodeReviewPrompt: assetMocks.loadPrompt
};
Expand Down Expand Up @@ -66,4 +74,52 @@ describe("runCodeReview asset paths", () => {
expect(assetMocks.loadProfile).toHaveBeenCalledWith(resolve(cwd, "profiles/security.md"));
expect(assetMocks.loadPrompt).toHaveBeenCalledWith(resolve(cwd, "prompts/review.md"));
});

it.each(["codex", "claude-code", "claude", "CLAUDE"])(
"pipes %s text-safe orchestrator prompts through stdin",
async (agent) => {
vi.mocked(spawn).mockClear();
await runCodeReview(
{ prUrl, cwd: "/repo/worktree" },
{
resolveOptions: async (input) => ({
...input,
agent,
draftStore: ".poe-code/code-review/reviews",
humanGate: { provider: "none" }
}),
fetchPr: async () => ({}),
fetchDiff: async () => "",
fetchComments: async () => ({}),
store: createStore()
}
);

expect(spawn).toHaveBeenCalledWith(agent, expect.objectContaining({ useStdin: true }));
}
);

it.each(["kimi", "goose"])(
"does not pipe raw text into the %s structured-input orchestrator",
async (agent) => {
vi.mocked(spawn).mockClear();
await runCodeReview(
{ prUrl, cwd: "/repo/worktree" },
{
resolveOptions: async (input) => ({
...input,
agent,
draftStore: ".poe-code/code-review/reviews",
humanGate: { provider: "none" }
}),
fetchPr: async () => ({}),
fetchDiff: async () => "",
fetchComments: async () => ({}),
store: createStore()
}
);

expect(spawn).toHaveBeenCalledWith(agent, expect.not.objectContaining({ useStdin: true }));
}
);
});
7 changes: 6 additions & 1 deletion packages/agent-code-review/src/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
resolveCodeReviewRuntimeOptions
} from "./config.js";
import { createCodeReviewAgentMcpConfig } from "./mcp.js";
import { shouldUseTextStdinForCodeReview } from "./prompt-transport.js";
import type { CodeReviewState } from "./review-state.js";
import { CodeReviewYamlStore, resolveCodeReviewStoreDirectory } from "./review-store.js";

Expand Down Expand Up @@ -236,5 +237,9 @@ async function spawnWithPoeCode(
prompt: string,
options: Omit<SpawnOptions, "prompt">
): Promise<SpawnResult> {
return spawn(agent, { prompt, ...options });
return spawn(agent, {
prompt,
...options,
...(shouldUseTextStdinForCodeReview(agent) ? { useStdin: true } : {})
});
}
13 changes: 13 additions & 0 deletions packages/agent-spawn/src/agent-spawn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,19 @@ describe("buildSpawnArgs", () => {
expect(result.args).not.toContain(prompt);
});

it("does not silently switch buildSpawnArgs callers to stdin for large prompts", () => {
const prompt = "x".repeat(128 * 1024);
const result = buildSpawnArgs("codex", { prompt });

expect(result.args).toEqual([
codexSpawnConfig.promptFlag,
prompt,
...codexSpawnConfig.defaultArgs,
...codexSpawnConfig.modes.yolo
]);
expect(result.args).toContain(prompt);
});

it("ignores useStdin for agents without stdinMode", () => {
const result = buildSpawnArgs("opencode", { prompt: "hello", useStdin: true });

Expand Down
Loading