fix(P01): add Zod BackendResult validation and fix opencode silent success
---ci---
project: ci
phase: 1
milestone: v0.8
status: in_progress
decisions:
- id: D-022
decision: Validate BackendResult at boundary with Zod schema
rationale: External backend output is untrusted; runtime validation prevents corrupt commit streams
confidence: 0.92
- id: D-023
decision: opencode parseResult returns success:false on malformed JSON
rationale: Silent success:true on parse failure masks backend errors; fail loudly instead
confidence: 0.95
requirements:
covered: [FIX-02, FIX-03]
---/ci---
FIX-02: Add Zod BackendResultSchema and validateBackendResult() in
backends/types.ts. backendResultToAgentResult() in base.ts now validates
before passing through. Invalid results produce success:false with error
detail. Path traversal protection: artifact paths with '..' or leading '/'
are rejected.
FIX-03: opencode.ts parseResult() no longer defaults to success:true when
JSON parsing fails entirely. Both the inner parse error and the no-JSON
match case now return emptyBackendResult() with descriptive error messages.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { z } from "zod";
|
||||
import { AgentName, AutonomyLevel, ModelProfile } from "../types/config.js";
|
||||
import { AgentContext } from "../agents/base.js";
|
||||
import { Decision } from "../types/decisions.js";
|
||||
@@ -5,6 +6,55 @@ import { Escalation } from "../types/escalation.js";
|
||||
|
||||
export type BackendType = "llm" | "agent";
|
||||
|
||||
export const ArtifactSchema = z.object({
|
||||
path: z.string().min(1, "Artifact path must not be empty"),
|
||||
content: z.string(),
|
||||
operation: z.enum(["create", "update", "delete"]),
|
||||
});
|
||||
|
||||
export const TokenUsageSchema = z.object({
|
||||
input_tokens: z.number().min(0),
|
||||
output_tokens: z.number().min(0),
|
||||
total_tokens: z.number().min(0),
|
||||
estimated_cost_usd: z.number().min(0),
|
||||
});
|
||||
|
||||
export const BackendResultSchema = z.object({
|
||||
success: z.boolean(),
|
||||
output: z.string(),
|
||||
artifacts: z.array(ArtifactSchema),
|
||||
decisions: z.array(z.unknown()),
|
||||
escalations: z.array(z.unknown()),
|
||||
usage: TokenUsageSchema,
|
||||
error: z.string().optional(),
|
||||
}).refine(
|
||||
(r) => !(r.success === true && r.error && r.error.length > 0),
|
||||
{ message: "Result cannot be both success and have an error message" }
|
||||
);
|
||||
|
||||
export function validateBackendResult(raw: unknown): { result: BackendResult | null; errors: string[] } {
|
||||
const parseResult = BackendResultSchema.safeParse(raw);
|
||||
if (!parseResult.success) {
|
||||
return {
|
||||
result: null,
|
||||
errors: parseResult.error.errors.map((e) => `${e.path.join(".")}: ${e.message}`),
|
||||
};
|
||||
}
|
||||
const data = parseResult.data;
|
||||
if (!Array.isArray(data.artifacts)) {
|
||||
return { result: null, errors: ["artifacts: expected array"] };
|
||||
}
|
||||
for (const a of data.artifacts) {
|
||||
if (a.path.includes("..")) {
|
||||
return { result: null, errors: [`artifacts: path "${a.path}" contains ".." (path traversal risk)`] };
|
||||
}
|
||||
if (a.path.startsWith("/")) {
|
||||
return { result: null, errors: [`artifacts: path "${a.path}" is absolute (must be relative)`] };
|
||||
}
|
||||
}
|
||||
return { result: data as BackendResult, errors: [] };
|
||||
}
|
||||
|
||||
export interface BackendRequest {
|
||||
persona: AgentName;
|
||||
workflow: string;
|
||||
|
||||
Reference in New Issue
Block a user