fix(contracts): preserve local inputs and comparison uncertainty

This commit is contained in:
morluto
2026-10-09 06:49:02 +08:00
parent 7a7460f04f
commit 70e8a8f860
35 changed files with 460 additions and 386 deletions
-117
View File
@@ -1,117 +0,0 @@
import { describe, expect, it } from "vitest";
import { processScenarioSchema } from "./processScenario.js";
describe("direct process scenarios", () => {
it("defaults to one command with inherited environment and no filesystem scan", () => {
expect(processScenarioSchema.parse({ executable: "node" })).toMatchObject({
executable: "node",
arguments: [],
working_directory: ".",
environment: {},
filesystem_observation_paths: [],
events: [],
});
});
it("accepts caller-selected environment and explicit filesystem observations", () => {
const environment = { "app.setting": "value", "1": "numbered" };
const scenario = processScenarioSchema.parse({
executable: "node",
arguments: ["./task.mjs"],
working_directory: "./project",
environment,
filesystem_observation_paths: ["state"],
});
expect(scenario.environment).toEqual(environment);
expect(scenario.filesystem_observation_paths).toEqual(["state"]);
});
it("rejects malformed environment names and adapter-reserved identity", () => {
for (const name of ["", "KEY=VALUE", "KEY\0VALUE", "REA_PROCESS_RUN_ID"]) {
const result = processScenarioSchema.safeParse({
executable: "node",
environment: { [name]: "value" },
});
expect(result.success).toBe(false);
if (name === "" && !result.success) {
const invalidKey = result.error.issues[0];
expect(invalidKey?.code).toBe("invalid_key");
if (invalidKey?.code === "invalid_key") {
expect(invalidKey.issues[0]?.message).toBe(
"Environment names must not be empty",
);
}
}
if (name === "REA_PROCESS_RUN_ID" && !result.success) {
const invalidKey = result.error.issues[0];
expect(invalidKey?.code).toBe("invalid_key");
if (invalidKey?.code === "invalid_key") {
expect(invalidKey.issues[0]?.message).toBe(
"REA_PROCESS_RUN_ID is reserved by the process adapter",
);
}
}
}
expect(
processScenarioSchema.safeParse({
executable: "node",
environment: { "REA_PROCESS_RUN_ID\n": "caller-value" },
}).success,
).toBe(true);
});
it("rejects NUL in strings passed to child-process APIs", () => {
const nul = "\0";
for (const input of [
{ executable: `node${nul}` },
{ executable: "node", arguments: [nul] },
{ executable: "node", working_directory: `.${nul}` },
{ executable: "node", environment: { KEY: nul } },
{ executable: "node", filesystem_observation_paths: [nul] },
])
expect(processScenarioSchema.safeParse(input).success).toBe(false);
expect(
processScenarioSchema.safeParse({
executable: "node",
events: [{ type: "input", at_ms: 0, data: nul }],
}).success,
).toBe(true);
});
it("keeps bounded capture controls and ordered scheduled interaction", () => {
const scenario = processScenarioSchema.parse({
executable: "node",
events: [
{ type: "input", at_ms: 10, data: "answer", sensitive: true },
{ type: "resize", at_ms: 20, columns: 100, rows: 30 },
{ type: "signal", at_ms: 30, signal: "SIGTERM" },
],
limits: { output_bytes: 2_000_000, filesystem_depth: 0 },
});
expect(scenario.events).toHaveLength(3);
expect(scenario.events[0]).toMatchObject({ sensitive: true });
expect(scenario.limits.output_bytes).toBe(2_000_000);
expect(scenario.limits.filesystem_depth).toBe(0);
});
it("rejects events after timeout and unknown orchestration fields", () => {
expect(
processScenarioSchema.safeParse({
executable: "node",
timeout_ms: 10,
events: [{ type: "input", at_ms: 11, data: "late" }],
}).success,
).toBe(false);
for (const [field, value] of [
["command_shims", []],
["replay", {}],
["reactive", {}],
["checkpoints", []],
] as const)
expect(
processScenarioSchema.safeParse({ executable: "node", [field]: value })
.success,
).toBe(false);
});
});
+13 -10
View File
@@ -91,7 +91,7 @@ export const processComparisonContract = (
* Defaults are part of the evidence contract and must remain deterministic.
*/
export const processScenarioSchema = z
.object({
.strictObject({
executable: childProcessString.min(1),
arguments: z.array(childProcessString).default([]),
working_directory: childProcessString.default("."),
@@ -100,7 +100,7 @@ export const processScenarioSchema = z
.array(childProcessString.min(1))
.default([]),
terminal: z
.object({
.strictObject({
columns: z.number().int().min(1).max(65_535).default(80),
rows: z.number().int().min(1).max(65_535).default(24),
scrollback: z.number().int().min(0).default(1_000),
@@ -109,19 +109,19 @@ export const processScenarioSchema = z
events: z
.array(
z.discriminatedUnion("type", [
z.object({
z.strictObject({
...timedEventBase,
type: z.literal("input"),
data: z.string(),
sensitive: z.boolean().default(false),
}),
z.object({
z.strictObject({
...timedEventBase,
type: z.literal("resize"),
columns: z.number().int().min(1).max(65_535),
rows: z.number().int().min(1).max(65_535),
}),
z.object({
z.strictObject({
...timedEventBase,
type: z.literal("signal"),
signal: z.enum(["SIGINT", "SIGTERM", "SIGKILL"]),
@@ -133,7 +133,7 @@ export const processScenarioSchema = z
idle_timeout_ms: positiveBudget.default(30_000),
settle_ms: z.number().int().safe().nonnegative().default(100),
limits: z
.object({
.strictObject({
output_bytes: positiveBudget.default(1_000_000),
files: positiveBudget.default(10_000),
file_bytes: positiveBudget.default(10_000_000),
@@ -149,15 +149,19 @@ export const processScenarioSchema = z
filesystem_depth: 16,
}),
normalization: z
.object({
.strictObject({
paths: z.boolean().default(false),
pids: z.boolean().default(true),
ports: z.boolean().default(true),
time_bucket_ms: positiveBudget.default(10),
patterns: z
.array(
z.object({
pattern: z.string(),
z.strictObject({
pattern: z
.string()
.describe(
"Literal text to replace in captured output; not a regular expression.",
),
replacement: z.string(),
}),
)
@@ -171,7 +175,6 @@ export const processScenarioSchema = z
patterns: [],
}),
})
.strict()
.superRefine((scenario, context) => {
for (let index = 1; index < scenario.events.length; index += 1) {
const event = scenario.events[index];