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
18 changes: 16 additions & 2 deletions dist/index.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "shopfloor",
"version": "2.0.3",
"version": "2.0.4",
"private": true,
"type": "module",
"engines": {
Expand Down
27 changes: 26 additions & 1 deletion src/agents/codex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export class CodexAgentAdapter implements AgentAdapter {
}

try {
return args.decisionSchema.parse(parsedJson);
return args.decisionSchema.parse(stripNullProperties(parsedJson));
} catch (err) {
throw new AgentError(
"agent_invalid_output",
Expand Down Expand Up @@ -205,6 +205,31 @@ export class CodexAgentAdapter implements AgentAdapter {
}
}

// OpenAI strict structured outputs cannot express an optional property: every
// field is required and an absent value is returned as explicit `null`.
// zodToJsonSchema(target: "openAi") therefore renders our `.optional()` fields
// as nullable, and Codex emits `null` for the ones it chose to omit (e.g. a
// review comment's start_line/start_side). Zod `.optional()` accepts
// `undefined`, not `null`, so those decisions would fail schema validation.
// Recursively drop null-valued properties before parsing so they read as
// "absent". Fields that are genuinely `.nullable()` in our schemas pair it
// with `.default(null)`, so a stripped null round-trips back to null via the
// default and no information is lost.
function stripNullProperties(value: unknown): unknown {
if (Array.isArray(value)) {
return value.map(stripNullProperties);
}
if (value !== null && typeof value === "object") {
const out: Record<string, unknown> = {};
for (const [key, val] of Object.entries(value)) {
if (val === null) continue;
out[key] = stripNullProperties(val);
}
return out;
}
return value;
}

function logAgentInput<T>(args: RunStageArgs<T>): void {
core.startGroup(`Codex agent input (${args.model})`);
try {
Expand Down
54 changes: 54 additions & 0 deletions test/agents/codex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,60 @@ describe("CodexAgentAdapter", () => {
expect(err.subtype).toBe("schema_validation_error");
});

it("treats null-valued optional fields as absent (OpenAI strict output)", async () => {
// OpenAI strict structured outputs return absent optionals as explicit
// null rather than omitting them. The adapter must strip these so Zod's
// `.optional()` accepts the decision instead of failing on `null`.
const schema = z.object({
verdict: z.string(),
note: z.string().optional(),
items: z
.array(z.object({ id: z.number(), label: z.string().optional() }))
.default([]),
});
runImpl.fn = vi.fn(async () => ({
finalResponse: JSON.stringify({
verdict: "ok",
note: null,
items: [{ id: 1, label: null }],
}),
items: [],
usage: null,
}));
const agent = new CodexAgentAdapter(baseOpts());
const result = await agent.runStage({
systemPrompt: "SYS",
userPrompt: "USER",
tools: [],
decisionSchema: schema,
model: "gpt-5.5",
});
expect(result).toEqual({ verdict: "ok", items: [{ id: 1 }] });
});

it("preserves a meaningful null on a nullable+default field", async () => {
// A genuinely nullable field (paired with .default(null)) must still end
// up null after stripping, via the default.
const schema = z.object({
verdict: z.string(),
supplied: z.object({ path: z.string() }).nullable().default(null),
});
runImpl.fn = vi.fn(async () => ({
finalResponse: JSON.stringify({ verdict: "ok", supplied: null }),
items: [],
usage: null,
}));
const agent = new CodexAgentAdapter(baseOpts());
const result = await agent.runStage({
systemPrompt: "SYS",
userPrompt: "USER",
tools: [],
decisionSchema: schema,
model: "gpt-5.5",
});
expect(result).toEqual({ verdict: "ok", supplied: null });
});

it("maps a thrown turn failure to agent_execution", async () => {
runImpl.fn = vi.fn(async () => {
throw new Error("turn failed: model exploded");
Expand Down
Loading