170 lines
4.5 KiB
JavaScript
170 lines
4.5 KiB
JavaScript
import {
|
|
Annotation,
|
|
END,
|
|
START,
|
|
StateGraph,
|
|
} from '@langchain/langgraph';
|
|
import {
|
|
buildRunEvent,
|
|
normalizeRunSpec,
|
|
} from './contracts.mjs';
|
|
|
|
export const CODE_RUN_SHADOW_WORKFLOW = 'code-run-v1';
|
|
|
|
const ShadowState = Annotation.Root({
|
|
spec: Annotation(),
|
|
status: Annotation({
|
|
reducer: (_current, update) => update,
|
|
default: () => 'queued',
|
|
}),
|
|
phase: Annotation({
|
|
reducer: (_current, update) => update,
|
|
default: () => 'accepted',
|
|
}),
|
|
plan: Annotation({
|
|
reducer: (_current, update) => update,
|
|
default: () => null,
|
|
}),
|
|
result: Annotation({
|
|
reducer: (_current, update) => update,
|
|
default: () => null,
|
|
}),
|
|
events: Annotation({
|
|
reducer: (current, update) => [...current, ...update],
|
|
default: () => [],
|
|
}),
|
|
});
|
|
|
|
function eventFor(state, type, data = null) {
|
|
return buildRunEvent({
|
|
runId: state.spec.runId,
|
|
sequence: state.events.length + 1,
|
|
type,
|
|
data,
|
|
});
|
|
}
|
|
|
|
function validateNode(state) {
|
|
const spec = normalizeRunSpec(state.spec);
|
|
if (spec.workflow.name !== CODE_RUN_SHADOW_WORKFLOW) {
|
|
const error = new Error(`Unsupported workflow: ${spec.workflow.name}`);
|
|
error.code = 'WORKFLOW_NOT_SUPPORTED';
|
|
throw error;
|
|
}
|
|
if (spec.policy.executionMode !== 'observe-only' || spec.policy.sideEffectsAllowed !== false) {
|
|
const error = new Error('Phase 2 LangGraph service accepts observe-only runs');
|
|
error.code = 'WORKFLOW_OBSERVE_ONLY_REQUIRED';
|
|
throw error;
|
|
}
|
|
return {
|
|
spec,
|
|
status: 'running',
|
|
phase: 'validated',
|
|
events: [eventFor(state, 'workflow_validated', {
|
|
workflow: spec.workflow,
|
|
executionMode: 'observe-only',
|
|
})],
|
|
};
|
|
}
|
|
|
|
async function planNode(state, executorGateway) {
|
|
const taskType = String(state.spec.input?.taskType ?? '').trim() || 'code-change';
|
|
const instruction = String(state.spec.input?.instruction ?? '');
|
|
const executorJob = await executorGateway.createJob({
|
|
jobId: `${state.spec.runId}:executor-preview`,
|
|
idempotencyKey: `${state.spec.runId}:build_plan:v1`,
|
|
executor: 'goosed',
|
|
task: {
|
|
type: taskType,
|
|
instruction,
|
|
workspaceRef: state.spec.input?.workspaceRef ?? null,
|
|
inputRefs: state.spec.input?.inputRefs ?? [],
|
|
},
|
|
subject: state.spec.subject,
|
|
authorization: {
|
|
executionAllowed: false,
|
|
actorId: state.spec.subject?.userId ?? null,
|
|
},
|
|
policy: {
|
|
sideEffectsAllowed: false,
|
|
networkAllowed: false,
|
|
},
|
|
controls: {
|
|
timeoutMs: state.spec.limits?.timeoutMs,
|
|
cancellationAllowed: true,
|
|
fallbackExecutor: 'aider',
|
|
},
|
|
metadata: {
|
|
source: 'langgraph-shadow-plan',
|
|
workflow: state.spec.workflow,
|
|
},
|
|
});
|
|
const plan = {
|
|
taskType,
|
|
executorAdapter: 'native-agent-run',
|
|
executorJob: {
|
|
kind: 'executor-job',
|
|
id: executorJob.job.jobId,
|
|
executor: executorJob.job.executor,
|
|
status: executorJob.job.status,
|
|
reason: executorJob.job.reason,
|
|
durable: executorGateway.status().store.durable,
|
|
},
|
|
instructionCharacters: instruction.length,
|
|
steps: [
|
|
'accept_control_plane_run',
|
|
'project_executor_boundary',
|
|
'record_shadow_result',
|
|
],
|
|
};
|
|
return {
|
|
phase: 'planned',
|
|
plan,
|
|
events: [eventFor(state, 'workflow_planned', {
|
|
taskType,
|
|
executorAdapter: plan.executorAdapter,
|
|
executorJob: plan.executorJob,
|
|
stepCount: plan.steps.length,
|
|
})],
|
|
};
|
|
}
|
|
|
|
function finalizeNode(state) {
|
|
const result = {
|
|
observed: true,
|
|
executed: false,
|
|
executorAdapter: state.plan.executorAdapter,
|
|
taskType: state.plan.taskType,
|
|
};
|
|
return {
|
|
status: 'succeeded',
|
|
phase: 'completed',
|
|
result,
|
|
events: [eventFor(state, 'workflow_completed', result)],
|
|
};
|
|
}
|
|
|
|
export function createCodeRunShadowGraph({ checkpointer, executorGateway } = {}) {
|
|
if (!checkpointer) throw new Error('Code run shadow graph requires a checkpointer');
|
|
if (!executorGateway?.createJob) {
|
|
throw new Error('Code run shadow graph requires an Executor Gateway');
|
|
}
|
|
return new StateGraph(ShadowState)
|
|
.addNode('validate_run', validateNode)
|
|
.addNode('build_plan', (state) => planNode(state, executorGateway))
|
|
.addNode('finalize_run', finalizeNode)
|
|
.addEdge(START, 'validate_run')
|
|
.addEdge('validate_run', 'build_plan')
|
|
.addEdge('build_plan', 'finalize_run')
|
|
.addEdge('finalize_run', END)
|
|
.compile({ checkpointer });
|
|
}
|
|
|
|
export const codeRunShadowGraphInternals = {
|
|
ShadowState,
|
|
eventFor,
|
|
validateNode,
|
|
planNode,
|
|
finalizeNode,
|
|
};
|