Skip to content

Commit 4cb4007

Browse files
committed
refactor(workflow): strengthen contracts and zod boundaries
1 parent 3343404 commit 4cb4007

25 files changed

Lines changed: 687 additions & 321 deletions

package-lock.json

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"dev": "node scripts/dev-server.mjs",
2929
"postinstall": "node scripts/fix-node-pty-permissions.mjs",
3030
"start": "node dist/cli.js serve",
31-
"test": "tsx src/config.test.ts && tsx src/ui/card-types.test.ts && tsx src/ui/patch-display.test.ts && tsx src/ui/tool-display.test.ts && tsx src/apply-patch.test.ts && tsx src/process-platform.test.ts && tsx src/process-sessions.test.ts && tsx src/mcp-sessions.test.ts && tsx src/server-shutdown.test.ts && tsx src/local-agent-runtime.test.ts && tsx src/local-agent-adapters.test.ts && tsx src/local-agent-availability.test.ts && tsx src/local-agent-profiles.test.ts && tsx src/local-agent-targets.test.ts && tsx src/local-agent-store.test.ts && tsx src/roots.test.ts && tsx src/skills.test.ts && tsx src/workspaces.test.ts && tsx src/review-checkpoints.test.ts && tsx src/oauth-store.test.ts && tsx src/cli.test.ts && tsx src/workflow-types.test.ts && tsx src/workflow-store.test.ts && tsx src/workflow-script.test.ts && tsx src/workflow-sandbox.test.ts && tsx src/workflow-engine.test.ts && tsx src/workflow-files.test.ts && tsx src/workflow-replay.test.ts && tsx src/workflow-schema.test.ts",
31+
"test": "tsx src/config.test.ts && tsx src/ui/card-types.test.ts && tsx src/ui/patch-display.test.ts && tsx src/ui/tool-display.test.ts && tsx src/apply-patch.test.ts && tsx src/process-platform.test.ts && tsx src/process-sessions.test.ts && tsx src/mcp-sessions.test.ts && tsx src/server-shutdown.test.ts && tsx src/local-agent-runtime.test.ts && tsx src/local-agent-adapters.test.ts && tsx src/local-agent-availability.test.ts && tsx src/local-agent-profiles.test.ts && tsx src/local-agent-targets.test.ts && tsx src/local-agent-store.test.ts && tsx src/roots.test.ts && tsx src/skills.test.ts && tsx src/workspaces.test.ts && tsx src/review-checkpoints.test.ts && tsx src/oauth-store.test.ts && tsx src/cli.test.ts && tsx src/workflow-contracts.test.ts && tsx src/workflow-types.test.ts && tsx src/workflow-store.test.ts && tsx src/workflow-script.test.ts && tsx src/workflow-sandbox.test.ts && tsx src/workflow-engine.test.ts && tsx src/workflow-files.test.ts && tsx src/workflow-replay.test.ts && tsx src/workflow-schema.test.ts",
3232
"typecheck": "tsc -p tsconfig.json --noEmit"
3333
},
3434
"keywords": [],
@@ -49,6 +49,7 @@
4949
"diff": "^8.0.3",
5050
"drizzle-orm": "^0.45.2",
5151
"express": "^5.2.1",
52+
"json-schema-to-ts": "^3.1.1",
5253
"lucide": "^1.24.0",
5354
"react": "^19.2.6",
5455
"react-dom": "^19.2.6",

src/json-types.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import type { JSONSchema } from "json-schema-to-ts";
2+
import * as z from "zod/v4";
3+
4+
export type JsonPrimitive = string | number | boolean | null;
5+
6+
export type JsonValue =
7+
| JsonPrimitive
8+
| JsonValue[]
9+
| { [key: string]: JsonValue };
10+
11+
export type JsonObject = { [key: string]: JsonValue };
12+
13+
/** JSON Schema is the portable contract shared with provider SDKs and Ajv. */
14+
export type JsonSchema = JSONSchema;
15+
16+
export const jsonValueSchema: z.ZodType<JsonValue> = z.lazy(() =>
17+
z.union([
18+
z.string(),
19+
z.number().finite(),
20+
z.boolean(),
21+
z.null(),
22+
z.array(jsonValueSchema),
23+
z.record(z.string(), jsonValueSchema),
24+
]),
25+
);
26+
27+
export const jsonObjectSchema: z.ZodType<JsonObject> = z.record(
28+
z.string(),
29+
jsonValueSchema,
30+
);
31+
32+
export const jsonSchemaSchema = jsonObjectSchema.transform(
33+
(value): JsonSchema => value as JsonSchema,
34+
);
35+
36+
export function parseJsonValue(value: unknown): JsonValue {
37+
return jsonValueSchema.parse(value);
38+
}
39+
40+
export function parseJsonText(text: string): JsonValue {
41+
return parseJsonValue(JSON.parse(text) as unknown);
42+
}

src/local-agent-adapters.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ assert.equal(
397397
type: "object",
398398
properties: { n: { type: "number" } },
399399
required: ["n"],
400-
};
400+
} as const;
401401
assert.deepEqual(claudeOutputFormatOptions(schema), {
402402
outputFormat: { type: "json_schema", schema },
403403
});

src/local-agent-adapters.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { spawn, spawnSync, type ChildProcessWithoutNullStreams } from "node:child_process";
22
import { resolve } from "node:path";
33
import { Readable, Writable } from "node:stream";
4-
import type { EffortLevel } from "@anthropic-ai/claude-agent-sdk";
4+
import type {
5+
EffortLevel,
6+
OutputFormat,
7+
} from "@anthropic-ai/claude-agent-sdk";
8+
import type { JsonSchema } from "./json-types.js";
59
import type { LocalAgentProvider } from "./local-agent-profiles.js";
610
import { removeDevspaceNodeModulesBinFromPath } from "./local-agent-path.js";
711
import {
@@ -116,8 +120,8 @@ class ClaudeLocalAgentAdapter implements LocalAgentAdapter {
116120

117121
/** Build Claude SDK outputFormat when a JSON Schema is requested. */
118122
export function claudeOutputFormatOptions(
119-
schema: object | undefined,
120-
): { outputFormat: { type: "json_schema"; schema: Record<string, unknown> } } | Record<string, never> {
123+
schema: JsonSchema | undefined,
124+
): { outputFormat: OutputFormat } | Record<string, never> {
121125
if (!schema) return {};
122126
return {
123127
outputFormat: {

src/local-agent-capabilities.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import type { LocalAgentProvider } from "./local-agent-profiles.js";
2+
3+
export interface LocalAgentProviderCapabilities {
4+
structuredOutput: "native" | "prompt";
5+
resumableSessions: boolean;
6+
cancellation: "signal" | "process";
7+
supportsWorkspaceIsolation: boolean;
8+
}
9+
10+
export const LOCAL_AGENT_PROVIDER_CAPABILITIES = {
11+
codex: {
12+
structuredOutput: "native",
13+
resumableSessions: true,
14+
cancellation: "signal",
15+
supportsWorkspaceIsolation: true,
16+
},
17+
claude: {
18+
structuredOutput: "native",
19+
resumableSessions: true,
20+
cancellation: "signal",
21+
supportsWorkspaceIsolation: true,
22+
},
23+
opencode: {
24+
structuredOutput: "prompt",
25+
resumableSessions: true,
26+
cancellation: "process",
27+
supportsWorkspaceIsolation: true,
28+
},
29+
pi: {
30+
structuredOutput: "prompt",
31+
resumableSessions: true,
32+
cancellation: "process",
33+
supportsWorkspaceIsolation: true,
34+
},
35+
cursor: {
36+
structuredOutput: "prompt",
37+
resumableSessions: true,
38+
cancellation: "process",
39+
supportsWorkspaceIsolation: true,
40+
},
41+
copilot: {
42+
structuredOutput: "prompt",
43+
resumableSessions: true,
44+
cancellation: "process",
45+
supportsWorkspaceIsolation: true,
46+
},
47+
} as const satisfies Record<LocalAgentProvider, LocalAgentProviderCapabilities>;
48+
49+
export function supportsNativeStructuredOutput(provider: LocalAgentProvider): boolean {
50+
return LOCAL_AGENT_PROVIDER_CAPABILITIES[provider].structuredOutput === "native";
51+
}

src/local-agent-profiles.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ import { basename, join, resolve } from "node:path";
44
import { parse as parseYaml } from "yaml";
55
import type { ServerConfig } from "./config.js";
66

7-
export type LocalAgentProvider = "codex" | "claude" | "opencode" | "pi" | "cursor" | "copilot";
8-
9-
export const LOCAL_AGENT_PROVIDERS: readonly LocalAgentProvider[] = [
7+
export const LOCAL_AGENT_PROVIDERS = [
108
"codex",
119
"claude",
1210
"opencode",
1311
"pi",
1412
"cursor",
1513
"copilot",
16-
];
14+
] as const;
15+
16+
export type LocalAgentProvider = (typeof LOCAL_AGENT_PROVIDERS)[number];
1717

1818
export interface LocalAgentProfile {
1919
name: string;

src/local-agent-runtime.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import type {
77
ThreadOptions,
88
TurnOptions,
99
} from "@openai/codex-sdk";
10+
import type { JsonSchema } from "./json-types.js";
11+
import type { LocalAgentProvider } from "./local-agent-profiles.js";
1012

1113
export type LocalAgentWriteMode = "read_only" | "allowed" | "full_access";
1214

@@ -19,11 +21,11 @@ export interface LocalAgentRunInput {
1921
/** Provider-native effort / reasoning level (was thinking). */
2022
effort?: string;
2123
/** JSON Schema for native structured output (codex/claude). */
22-
schema?: object;
24+
schema?: JsonSchema;
2325
}
2426

2527
export interface LocalAgentRunResult {
26-
provider: string;
28+
provider: LocalAgentProvider;
2729
providerSessionId: string | null;
2830
finalResponse: string;
2931
items: unknown[];
@@ -32,7 +34,7 @@ export interface LocalAgentRunResult {
3234
}
3335

3436
export interface LocalAgentRuntime {
35-
readonly provider: string;
37+
readonly provider: LocalAgentProvider;
3638
run(input: LocalAgentRunInput): Promise<LocalAgentRunResult>;
3739
}
3840

0 commit comments

Comments
 (0)