-
-
Notifications
You must be signed in to change notification settings - Fork 371
feat(workflow): isolate workflow script execution #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Waishnav
merged 39 commits into
pr/dw-11-workflow-mcp-ui
from
pr/dw-12-sandbox-process-boundary
Jul 27, 2026
Merged
Changes from 4 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
f5e9252
feat(workflow): isolate script execution in child process
Waishnav 09a7fc4
test(workflow): cover sandbox termination and escapes
Waishnav 12cee07
fix(workflow): keep sandbox values in vm realm
Waishnav d4efd02
fix(workflow): harden sandbox process teardown
Waishnav a78357a
fix(workflow): journal terminal transitions atomically
Waishnav 5528337
feat(workflow): add shared lifecycle supervisor
Waishnav e0b078a
fix(workflow): supervise cancellation and stale workers
Waishnav 244d42e
fix(workflow): close stale reaper races
Waishnav 0b52e5e
fix(workflow): bound MCP event pages
Waishnav ed623da
feat(workflow): persist exact replay values
Waishnav 749ebb7
fix(workflow): replay only deterministic call prefixes
Waishnav 4b0802f
docs(workflow): explain deterministic prefix replay
Waishnav 8f30225
test(workflow): cover oversized structured results
Waishnav 287cbce
refactor(agents): share profile prompt identity helpers
Waishnav 550f776
feat(workflow): select configured agent profiles
Waishnav 229f282
fix(workflow): honor nested workflow context
Waishnav 7675c14
fix(workflow): confine project workflow scripts
Waishnav b9c78eb
fix(workflow): parse module syntax without raw scans
Waishnav 323fe50
docs(workflow): teach profile selection and project scripts
Waishnav a23be1c
fix(workflow): preserve resume names and typed reads
Waishnav 94ac40d
fix(workflow): parse metadata as pure literals
Waishnav 3f01013
test(workflow): cover path and profile identity guards
Waishnav 620b180
refactor(config): remove experimental provider persistence
Waishnav d640cf8
refactor(agents): share execution target resolution
Waishnav f2482bb
refactor(agents): describe provider model and effort capabilities
Waishnav 321b81c
refactor(features): separate subagent and workflow capabilities
Waishnav 69fee30
fix(workspace): expose only usable agent capabilities
Waishnav f25d8e9
refactor(skills): replace subagent delegation guidance
Waishnav 2f17478
docs(skills): add provider-specific override references
Waishnav f214c19
fix(skills): keep bundled agent skills package-managed
Waishnav 1ace1f0
refactor(workflow): name live provider availability accurately
Waishnav 5c1ddea
docs(agents): document experimental capability boundaries
Waishnav 691998e
test(workflow): follow provider availability rename
Waishnav 5bf5374
docs(config): describe effective agent capability gates
Waishnav 562e54a
feat(agents): discover usable delegation targets
Waishnav b347a82
feat(workflow): merge dw-16 agent/subagent overhaul (#118)
Waishnav b677c3e
feat(workflow): merge dw-15 profiles and project workflows (#117)
Waishnav 8ce8274
Merge pull request #116 from pr/dw-14-prefix-replay-integrity
Waishnav 0b0f5e2
Merge pull request #115 from pr/dw-13-workflow-lifecycle-supervisor
Waishnav File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,292 @@ | ||
| import vm from "node:vm"; | ||
| import { parseWorkflowScript } from "./workflow-script.js"; | ||
| import type { JsonValue } from "./json-types.js"; | ||
| import { WORKFLOW_MAX_ITEMS } from "./workflow-types.js"; | ||
|
|
||
| type SandboxMethod = "agent" | "workflow" | "phase" | "log"; | ||
|
|
||
| interface StartMessage { | ||
| type: "start"; | ||
| source: string; | ||
| filename: string; | ||
| args: JsonValue | undefined; | ||
| budget: { | ||
| total: number | null; | ||
| spent: number; | ||
| remaining: number; | ||
| }; | ||
| } | ||
|
|
||
| interface CallResultMessage { | ||
| type: "call_result"; | ||
| id: number; | ||
| value?: unknown; | ||
| error?: SerializedError; | ||
| } | ||
|
|
||
| interface SerializedError { | ||
| name: string; | ||
| message: string; | ||
| stack?: string; | ||
| kind?: string; | ||
| } | ||
|
|
||
| interface BridgeSuccessEnvelope { | ||
| ok: true; | ||
| value?: unknown; | ||
| } | ||
|
|
||
| interface BridgeErrorEnvelope { | ||
| ok: false; | ||
| error: SerializedError; | ||
| } | ||
|
|
||
| type BridgeEnvelope = BridgeSuccessEnvelope | BridgeErrorEnvelope; | ||
|
|
||
| let nextCallId = 1; | ||
| const pending = new Map< | ||
| number, | ||
| { resolve(value: string): void } | ||
| >(); | ||
|
|
||
| process.on("message", (message: StartMessage | CallResultMessage) => { | ||
| if (message.type === "call_result") { | ||
| const waiter = pending.get(message.id); | ||
| if (!waiter) return; | ||
| pending.delete(message.id); | ||
| const envelope: BridgeEnvelope = message.error | ||
| ? { ok: false, error: message.error } | ||
| : { ok: true, value: message.value }; | ||
| waiter.resolve(JSON.stringify(envelope)); | ||
| return; | ||
| } | ||
| void execute(message); | ||
| }); | ||
|
|
||
| async function execute(message: StartMessage): Promise<void> { | ||
| try { | ||
| const parsed = parseWorkflowScript(message.source, { filename: message.filename }); | ||
| const bridge = (method: SandboxMethod, args: unknown[]): unknown => { | ||
| if (method === "phase" || method === "log") { | ||
| process.send?.({ type: "notify", method, args }); | ||
| return undefined; | ||
| } | ||
| const id = nextCallId; | ||
| nextCallId += 1; | ||
| process.send?.({ type: "call", id, method, args }); | ||
| return new Promise<string>((resolve) => { | ||
| pending.set(id, { resolve }); | ||
| }); | ||
| }; | ||
|
|
||
| const context = vm.createContext({ __workflowBridge: bridge }); | ||
| installContextApi(context, message); | ||
| const factory = parsed.script.runInContext(context, { | ||
| timeout: 5_000, | ||
| displayErrors: true, | ||
| }) as () => Promise<unknown>; | ||
| if (typeof factory !== "function") { | ||
| throw new Error("Workflow script did not compile to a function"); | ||
| } | ||
| const value = await factory(); | ||
| process.send?.({ type: "result", value }, () => disconnect()); | ||
| } catch (error) { | ||
| process.send?.({ type: "error", error: serializeError(error) }, () => disconnect()); | ||
| } | ||
| } | ||
|
|
||
| function installContextApi(context: vm.Context, message: StartMessage): void { | ||
| const bootstrap = `(() => { | ||
| const bridge = globalThis.__workflowBridge; | ||
| delete globalThis.__workflowBridge; | ||
|
|
||
| class WorkflowDeterminismError extends Error { | ||
| constructor(message) { | ||
| super(message); | ||
| this.name = "WorkflowDeterminismError"; | ||
| } | ||
| } | ||
|
|
||
| class WorkflowEngineError extends Error { | ||
| constructor(kind, message) { | ||
| super(message); | ||
| this.name = "WorkflowEngineError"; | ||
| this.kind = kind; | ||
| } | ||
| } | ||
|
|
||
| Object.defineProperty(Math, "random", { | ||
| configurable: false, | ||
| writable: false, | ||
| value() { | ||
| throw new WorkflowDeterminismError("Math.random() is banned in workflow scripts"); | ||
| }, | ||
| }); | ||
|
|
||
| const RealDate = Date; | ||
| function DateShim(...dateArgs) { | ||
| if (!new.target) { | ||
| throw new WorkflowDeterminismError("Date() is banned in workflow scripts"); | ||
| } | ||
| if (dateArgs.length === 0) { | ||
| throw new WorkflowDeterminismError( | ||
| "new Date() without arguments is banned in workflow scripts (pass an ISO string)", | ||
| ); | ||
| } | ||
| return Reflect.construct(RealDate, dateArgs, DateShim); | ||
| } | ||
| DateShim.now = () => { | ||
| throw new WorkflowDeterminismError("Date.now() is banned in workflow scripts"); | ||
| }; | ||
| DateShim.parse = RealDate.parse.bind(RealDate); | ||
| DateShim.UTC = RealDate.UTC.bind(RealDate); | ||
| DateShim.prototype = Object.create(RealDate.prototype, { | ||
| constructor: { | ||
| value: DateShim, | ||
| writable: false, | ||
| configurable: false, | ||
| }, | ||
| }); | ||
| Object.freeze(DateShim.prototype); | ||
| Object.freeze(DateShim); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| const rehydrateError = (input) => { | ||
| const error = input?.name === "WorkflowDeterminismError" | ||
| ? new WorkflowDeterminismError(input.message) | ||
| : input?.name === "WorkflowEngineError" && typeof input.kind === "string" | ||
| ? new WorkflowEngineError(input.kind, input.message) | ||
| : new Error(input?.message ?? "Workflow bridge call failed"); | ||
| if (typeof input?.name === "string") error.name = input.name; | ||
| if (typeof input?.stack === "string") error.stack = input.stack; | ||
| return error; | ||
| }; | ||
| const call = (method, callArgs) => new Promise((resolve, reject) => { | ||
| bridge(method, callArgs).then( | ||
| (payloadJson) => { | ||
| let payload; | ||
| try { | ||
| payload = JSON.parse(payloadJson); | ||
| } catch { | ||
| reject(new WorkflowEngineError("internal", "Workflow bridge returned invalid JSON")); | ||
| return; | ||
| } | ||
| if (payload?.ok === true) resolve(payload.value); | ||
| else reject(rehydrateError(payload?.error)); | ||
| }, | ||
| () => reject(new WorkflowEngineError("internal", "Workflow bridge call failed")), | ||
| ); | ||
| }); | ||
| const agent = (...callArgs) => call("agent", callArgs); | ||
| const workflow = (...callArgs) => call("workflow", callArgs); | ||
| const phase = (title) => { | ||
| if (typeof title !== "string" || !title.trim()) { | ||
| throw new WorkflowEngineError("internal", "phase(title) requires a non-empty string"); | ||
| } | ||
| return bridge("phase", [title]); | ||
| }; | ||
| const log = (...callArgs) => bridge("log", callArgs); | ||
| const parallel = async (tasks) => { | ||
| if (!Array.isArray(tasks)) { | ||
| throw new WorkflowEngineError("internal", "parallel(thunks) requires an array of functions"); | ||
| } | ||
| if (tasks.length > ${WORKFLOW_MAX_ITEMS}) { | ||
| throw new WorkflowEngineError( | ||
| "internal", | ||
| "parallel exceeds max items ${WORKFLOW_MAX_ITEMS} (got " + tasks.length + ")", | ||
| ); | ||
| } | ||
| return Promise.all(tasks.map(async (task, index) => { | ||
| if (typeof task !== "function") { | ||
| throw new WorkflowEngineError( | ||
| "internal", | ||
| "parallel thunks[" + index + "] must be a function", | ||
| ); | ||
| } | ||
| try { return await task(); } catch { return null; } | ||
| })); | ||
| }; | ||
| const pipeline = async (items, ...stages) => { | ||
| if (!Array.isArray(items)) { | ||
| throw new WorkflowEngineError( | ||
| "internal", | ||
| "pipeline(items, ...stages) requires an items array", | ||
| ); | ||
| } | ||
| if (items.length > ${WORKFLOW_MAX_ITEMS}) { | ||
| throw new WorkflowEngineError( | ||
| "internal", | ||
| "pipeline exceeds max items ${WORKFLOW_MAX_ITEMS} (got " + items.length + ")", | ||
| ); | ||
| } | ||
| for (let index = 0; index < stages.length; index += 1) { | ||
| if (typeof stages[index] !== "function") { | ||
| throw new WorkflowEngineError( | ||
| "internal", | ||
| "pipeline stage[" + index + "] must be a function", | ||
| ); | ||
| } | ||
| } | ||
| return Promise.all(items.map(async (item, index) => { | ||
| let value = item; | ||
| for (const stage of stages) { | ||
| try { value = await stage(value, item, index); } catch { return null; } | ||
| } | ||
| return value; | ||
| })); | ||
| }; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| const args = JSON.parse(${JSON.stringify(JSON.stringify(message.args ?? null))}); | ||
| const budget = Object.freeze({ | ||
| total: ${JSON.stringify(message.budget.total)}, | ||
| spent: () => ${JSON.stringify(message.budget.spent)}, | ||
| remaining: () => ${String(message.budget.remaining)}, | ||
| }); | ||
| const stringifyConsoleArg = (value) => { | ||
| if (typeof value === "string") return value; | ||
| try { return JSON.stringify(value); } catch { return String(value); } | ||
| }; | ||
| const consoleLine = (...callArgs) => log(callArgs.map(stringifyConsoleArg).join(" ")); | ||
| const console = Object.freeze({ | ||
| log: consoleLine, | ||
| warn: consoleLine, | ||
| error: consoleLine, | ||
| info: consoleLine, | ||
| debug: consoleLine, | ||
| }); | ||
|
|
||
| Object.defineProperties(globalThis, { | ||
| agent: { value: Object.freeze(agent), writable: false, configurable: false }, | ||
| workflow: { value: Object.freeze(workflow), writable: false, configurable: false }, | ||
| phase: { value: Object.freeze(phase), writable: false, configurable: false }, | ||
| log: { value: Object.freeze(log), writable: false, configurable: false }, | ||
| parallel: { value: Object.freeze(parallel), writable: false, configurable: false }, | ||
| pipeline: { value: Object.freeze(pipeline), writable: false, configurable: false }, | ||
| args: { value: Object.freeze(args), writable: false, configurable: false }, | ||
| budget: { value: budget, writable: false, configurable: false }, | ||
| console: { value: console, writable: false, configurable: false }, | ||
| Date: { value: DateShim, writable: false, configurable: false }, | ||
| }); | ||
| })()`; | ||
| vm.runInContext(bootstrap, context, { timeout: 5_000 }); | ||
| } | ||
|
|
||
| function serializeError(error: unknown): SerializedError { | ||
| if (!error || typeof error !== "object") { | ||
| return { name: "Error", message: String(error) }; | ||
| } | ||
| const record = error as { | ||
| name?: unknown; | ||
| message?: unknown; | ||
| stack?: unknown; | ||
| kind?: unknown; | ||
| }; | ||
| return { | ||
| name: typeof record.name === "string" ? record.name : "Error", | ||
| message: typeof record.message === "string" ? record.message : String(error), | ||
| stack: typeof record.stack === "string" ? record.stack : undefined, | ||
| kind: typeof record.kind === "string" ? record.kind : undefined, | ||
| }; | ||
| } | ||
|
|
||
| function disconnect(): void { | ||
| if (process.connected) process.disconnect?.(); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.