oxe-cc 0.9.2 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (163) hide show
  1. package/.cursor/commands/oxe-retro.md +2 -2
  2. package/.cursor/commands/oxe-spec.md +2 -2
  3. package/.github/prompts/oxe-retro.prompt.md +2 -2
  4. package/.github/prompts/oxe-spec.prompt.md +2 -2
  5. package/README.md +1 -1
  6. package/bin/banner.txt +1 -1
  7. package/bin/lib/oxe-context-engine.cjs +1 -0
  8. package/bin/lib/oxe-dashboard.cjs +9 -7
  9. package/bin/lib/oxe-operational.cjs +569 -4
  10. package/bin/oxe-cc.js +141 -57
  11. package/commands/oxe/retro.md +2 -2
  12. package/commands/oxe/spec.md +2 -2
  13. package/lib/runtime/compiler/graph-compiler.d.ts +83 -0
  14. package/lib/runtime/compiler/graph-compiler.js +135 -0
  15. package/lib/runtime/compiler/index.d.ts +1 -0
  16. package/lib/runtime/compiler/index.js +17 -0
  17. package/lib/runtime/context/context-pack-builder.d.ts +36 -0
  18. package/lib/runtime/context/context-pack-builder.js +136 -0
  19. package/lib/runtime/context/index.d.ts +1 -0
  20. package/lib/runtime/context/index.js +17 -0
  21. package/lib/runtime/delivery/branch-manager.d.ts +19 -0
  22. package/lib/runtime/delivery/branch-manager.js +78 -0
  23. package/lib/runtime/delivery/ci-checks.d.ts +34 -0
  24. package/lib/runtime/delivery/ci-checks.js +209 -0
  25. package/lib/runtime/delivery/index.d.ts +3 -0
  26. package/lib/runtime/delivery/index.js +19 -0
  27. package/lib/runtime/delivery/pr-manager.d.ts +30 -0
  28. package/lib/runtime/delivery/pr-manager.js +82 -0
  29. package/lib/runtime/events/bus.d.ts +9 -0
  30. package/lib/runtime/events/bus.js +63 -0
  31. package/lib/runtime/events/catalog.d.ts +3 -0
  32. package/lib/runtime/events/catalog.js +30 -0
  33. package/lib/runtime/events/envelope.d.ts +13 -0
  34. package/lib/runtime/events/envelope.js +2 -0
  35. package/lib/runtime/events/index.d.ts +3 -0
  36. package/lib/runtime/events/index.js +19 -0
  37. package/lib/runtime/evidence/evidence-store.d.ts +22 -0
  38. package/lib/runtime/evidence/evidence-store.js +106 -0
  39. package/lib/runtime/evidence/index.d.ts +1 -0
  40. package/lib/runtime/evidence/index.js +17 -0
  41. package/lib/runtime/gate/gate-manager.d.ts +39 -0
  42. package/lib/runtime/gate/gate-manager.js +104 -0
  43. package/lib/runtime/gate/index.d.ts +1 -0
  44. package/lib/runtime/gate/index.js +17 -0
  45. package/lib/runtime/index.d.ts +16 -0
  46. package/lib/runtime/index.js +40 -0
  47. package/lib/runtime/models/attempt.d.ts +12 -0
  48. package/lib/runtime/models/attempt.js +2 -0
  49. package/lib/runtime/models/evidence.d.ts +9 -0
  50. package/lib/runtime/models/evidence.js +2 -0
  51. package/lib/runtime/models/gate-decision.d.ts +10 -0
  52. package/lib/runtime/models/gate-decision.js +2 -0
  53. package/lib/runtime/models/index.d.ts +8 -0
  54. package/lib/runtime/models/index.js +24 -0
  55. package/lib/runtime/models/run.d.ts +13 -0
  56. package/lib/runtime/models/run.js +2 -0
  57. package/lib/runtime/models/session.d.ts +10 -0
  58. package/lib/runtime/models/session.js +2 -0
  59. package/lib/runtime/models/verification-result.d.ts +9 -0
  60. package/lib/runtime/models/verification-result.js +2 -0
  61. package/lib/runtime/models/work-item.d.ts +15 -0
  62. package/lib/runtime/models/work-item.js +2 -0
  63. package/lib/runtime/models/workspace.d.ts +25 -0
  64. package/lib/runtime/models/workspace.js +2 -0
  65. package/lib/runtime/plugins/index.d.ts +2 -0
  66. package/lib/runtime/plugins/index.js +18 -0
  67. package/lib/runtime/plugins/plugin-abi.d.ts +76 -0
  68. package/lib/runtime/plugins/plugin-abi.js +2 -0
  69. package/lib/runtime/plugins/plugin-registry.d.ts +21 -0
  70. package/lib/runtime/plugins/plugin-registry.js +114 -0
  71. package/lib/runtime/policy/index.d.ts +1 -0
  72. package/lib/runtime/policy/index.js +17 -0
  73. package/lib/runtime/policy/policy-engine.d.ts +40 -0
  74. package/lib/runtime/policy/policy-engine.js +80 -0
  75. package/lib/runtime/projection/index.d.ts +1 -0
  76. package/lib/runtime/projection/index.js +17 -0
  77. package/lib/runtime/projection/projection-engine.d.ts +11 -0
  78. package/lib/runtime/projection/projection-engine.js +218 -0
  79. package/lib/runtime/reducers/debug-reducer.d.ts +10 -0
  80. package/lib/runtime/reducers/debug-reducer.js +30 -0
  81. package/lib/runtime/reducers/index.d.ts +2 -0
  82. package/lib/runtime/reducers/index.js +18 -0
  83. package/lib/runtime/reducers/run-state-reducer.d.ts +20 -0
  84. package/lib/runtime/reducers/run-state-reducer.js +110 -0
  85. package/lib/runtime/scheduler/index.d.ts +1 -0
  86. package/lib/runtime/scheduler/index.js +17 -0
  87. package/lib/runtime/scheduler/multi-agent-coordinator.d.ts +34 -0
  88. package/lib/runtime/scheduler/multi-agent-coordinator.js +166 -0
  89. package/lib/runtime/scheduler/scheduler.d.ts +39 -0
  90. package/lib/runtime/scheduler/scheduler.js +196 -0
  91. package/lib/runtime/verification/index.d.ts +1 -0
  92. package/lib/runtime/verification/index.js +17 -0
  93. package/lib/runtime/verification/verification-compiler.d.ts +56 -0
  94. package/lib/runtime/verification/verification-compiler.js +147 -0
  95. package/lib/runtime/workspace/index.d.ts +5 -0
  96. package/lib/runtime/workspace/index.js +24 -0
  97. package/lib/runtime/workspace/strategies/ephemeral-container.d.ts +22 -0
  98. package/lib/runtime/workspace/strategies/ephemeral-container.js +109 -0
  99. package/lib/runtime/workspace/strategies/git-worktree.d.ts +12 -0
  100. package/lib/runtime/workspace/strategies/git-worktree.js +79 -0
  101. package/lib/runtime/workspace/strategies/inplace.d.ts +10 -0
  102. package/lib/runtime/workspace/strategies/inplace.js +37 -0
  103. package/lib/runtime/workspace/workspace-manager.d.ts +13 -0
  104. package/lib/runtime/workspace/workspace-manager.js +2 -0
  105. package/lib/sdk/index.cjs +24 -7
  106. package/lib/sdk/index.d.ts +17 -7
  107. package/oxe/templates/LESSONS-METRICS.template.json +13 -0
  108. package/oxe/workflows/references/robustness-elevation.md +295 -0
  109. package/oxe/workflows/references/workflow-runtime-contracts.json +32 -4
  110. package/oxe/workflows/retro.md +21 -0
  111. package/oxe/workflows/spec.md +50 -26
  112. package/oxe/workflows/verify.md +36 -0
  113. package/package.json +9 -3
  114. package/packages/runtime/package.json +17 -0
  115. package/packages/runtime/src/compiler/graph-compiler.ts +245 -0
  116. package/packages/runtime/src/compiler/index.ts +1 -0
  117. package/packages/runtime/src/context/context-pack-builder.ts +193 -0
  118. package/packages/runtime/src/context/index.ts +1 -0
  119. package/packages/runtime/src/delivery/branch-manager.ts +84 -0
  120. package/packages/runtime/src/delivery/ci-checks.ts +252 -0
  121. package/packages/runtime/src/delivery/index.ts +3 -0
  122. package/packages/runtime/src/delivery/pr-manager.ts +112 -0
  123. package/packages/runtime/src/events/bus.ts +92 -0
  124. package/packages/runtime/src/events/catalog.ts +29 -0
  125. package/packages/runtime/src/events/envelope.ts +14 -0
  126. package/packages/runtime/src/events/index.ts +3 -0
  127. package/packages/runtime/src/evidence/evidence-store.ts +130 -0
  128. package/packages/runtime/src/evidence/index.ts +1 -0
  129. package/packages/runtime/src/gate/gate-manager.ts +137 -0
  130. package/packages/runtime/src/gate/index.ts +1 -0
  131. package/packages/runtime/src/index.ts +32 -0
  132. package/packages/runtime/src/models/attempt.ts +19 -0
  133. package/packages/runtime/src/models/evidence.ts +21 -0
  134. package/packages/runtime/src/models/gate-decision.ts +21 -0
  135. package/packages/runtime/src/models/index.ts +8 -0
  136. package/packages/runtime/src/models/run.ts +24 -0
  137. package/packages/runtime/src/models/session.ts +11 -0
  138. package/packages/runtime/src/models/verification-result.ts +10 -0
  139. package/packages/runtime/src/models/work-item.ts +25 -0
  140. package/packages/runtime/src/models/workspace.ts +28 -0
  141. package/packages/runtime/src/plugins/index.ts +2 -0
  142. package/packages/runtime/src/plugins/plugin-abi.ts +95 -0
  143. package/packages/runtime/src/plugins/plugin-registry.ts +119 -0
  144. package/packages/runtime/src/policy/index.ts +1 -0
  145. package/packages/runtime/src/policy/policy-engine.ts +113 -0
  146. package/packages/runtime/src/projection/index.ts +1 -0
  147. package/packages/runtime/src/projection/projection-engine.ts +249 -0
  148. package/packages/runtime/src/reducers/debug-reducer.ts +36 -0
  149. package/packages/runtime/src/reducers/index.ts +2 -0
  150. package/packages/runtime/src/reducers/run-state-reducer.ts +127 -0
  151. package/packages/runtime/src/scheduler/index.ts +1 -0
  152. package/packages/runtime/src/scheduler/multi-agent-coordinator.ts +231 -0
  153. package/packages/runtime/src/scheduler/scheduler.ts +281 -0
  154. package/packages/runtime/src/verification/index.ts +1 -0
  155. package/packages/runtime/src/verification/verification-compiler.ts +225 -0
  156. package/packages/runtime/src/workspace/index.ts +5 -0
  157. package/packages/runtime/src/workspace/strategies/ephemeral-container.ts +121 -0
  158. package/packages/runtime/src/workspace/strategies/git-worktree.ts +77 -0
  159. package/packages/runtime/src/workspace/strategies/inplace.ts +35 -0
  160. package/packages/runtime/src/workspace/workspace-manager.ts +15 -0
  161. package/packages/runtime/tsconfig.json +17 -0
  162. package/vscode-extension/oxe-agents-1.0.0.vsix +0 -0
  163. package/vscode-extension/package.json +1 -1
@@ -0,0 +1,127 @@
1
+ import type { OxeEvent } from '../events/envelope';
2
+ import type { Run } from '../models/run';
3
+ import type { WorkItem } from '../models/work-item';
4
+ import type { Attempt } from '../models/attempt';
5
+ import type { Workspace } from '../models/workspace';
6
+
7
+ export interface RunState {
8
+ run: Run | null;
9
+ workItems: Map<string, WorkItem>;
10
+ attempts: Map<string, Attempt[]>;
11
+ workspaces: Map<string, Workspace>;
12
+ completedWorkItems: Set<string>;
13
+ failedWorkItems: Set<string>;
14
+ blockedWorkItems: Set<string>;
15
+ }
16
+
17
+ export function createEmptyRunState(): RunState {
18
+ return {
19
+ run: null,
20
+ workItems: new Map(),
21
+ attempts: new Map(),
22
+ workspaces: new Map(),
23
+ completedWorkItems: new Set(),
24
+ failedWorkItems: new Set(),
25
+ blockedWorkItems: new Set(),
26
+ };
27
+ }
28
+
29
+ export function reduce(events: OxeEvent[]): RunState {
30
+ return events.reduce(applyEvent, createEmptyRunState());
31
+ }
32
+
33
+ // Exported alias so debug-reducer can import applyEvent without circular issues
34
+ export { applyEvent as applyEventExported };
35
+
36
+ function applyEvent(state: RunState, event: OxeEvent): RunState {
37
+ switch (event.type) {
38
+ case 'RunStarted': {
39
+ const run = event.payload as unknown as Run;
40
+ return { ...state, run };
41
+ }
42
+
43
+ case 'RunCompleted': {
44
+ if (!state.run) return state;
45
+ const status = (event.payload as { status?: Run['status'] }).status ?? 'completed';
46
+ return {
47
+ ...state,
48
+ run: { ...state.run, status, ended_at: event.timestamp },
49
+ };
50
+ }
51
+
52
+ case 'WorkItemReady': {
53
+ if (!event.work_item_id) return state;
54
+ const workItems = new Map(state.workItems);
55
+ const existing = workItems.get(event.work_item_id);
56
+ if (existing) {
57
+ workItems.set(event.work_item_id, { ...existing, status: 'ready' });
58
+ } else {
59
+ // First time we see this work item — create from payload
60
+ const item = event.payload as unknown as WorkItem;
61
+ workItems.set(event.work_item_id, { ...item, work_item_id: event.work_item_id, status: 'ready' });
62
+ }
63
+ return { ...state, workItems };
64
+ }
65
+
66
+ case 'AttemptStarted': {
67
+ if (!event.work_item_id || !event.attempt_id) return state;
68
+ const attempts = new Map(state.attempts);
69
+ const attempt: Attempt = {
70
+ attempt_id: event.attempt_id,
71
+ work_item_id: event.work_item_id,
72
+ attempt_number: (event.payload as { attempt_number?: number }).attempt_number ?? 1,
73
+ workspace_id: null,
74
+ agent_profile: null,
75
+ model: null,
76
+ started_at: event.timestamp,
77
+ ended_at: null,
78
+ outcome: null,
79
+ };
80
+ const existing = attempts.get(event.work_item_id) ?? [];
81
+ attempts.set(event.work_item_id, [...existing, attempt]);
82
+ return { ...state, attempts };
83
+ }
84
+
85
+ case 'WorkspaceAllocated': {
86
+ const ws = event.payload as unknown as Workspace;
87
+ if (!ws.workspace_id) return state;
88
+ const workspaces = new Map(state.workspaces);
89
+ workspaces.set(ws.workspace_id, { ...ws, status: 'ready' });
90
+ return { ...state, workspaces };
91
+ }
92
+
93
+ case 'WorkItemCompleted': {
94
+ if (!event.work_item_id) return state;
95
+ const workItems = new Map(state.workItems);
96
+ const item = workItems.get(event.work_item_id);
97
+ if (item) workItems.set(event.work_item_id, { ...item, status: 'completed' });
98
+ const completedWorkItems = new Set(state.completedWorkItems);
99
+ completedWorkItems.add(event.work_item_id);
100
+ return { ...state, workItems, completedWorkItems };
101
+ }
102
+
103
+ case 'WorkItemBlocked': {
104
+ if (!event.work_item_id) return state;
105
+ const workItems = new Map(state.workItems);
106
+ const item = workItems.get(event.work_item_id);
107
+ if (item) workItems.set(event.work_item_id, { ...item, status: 'blocked' });
108
+ const blockedWorkItems = new Set(state.blockedWorkItems);
109
+ blockedWorkItems.add(event.work_item_id);
110
+ return { ...state, workItems, blockedWorkItems };
111
+ }
112
+
113
+ default:
114
+ return state;
115
+ }
116
+ }
117
+
118
+ export function getWorkItemStatus(
119
+ state: RunState,
120
+ workItemId: string
121
+ ): WorkItem['status'] | null {
122
+ return state.workItems.get(workItemId)?.status ?? null;
123
+ }
124
+
125
+ export function getAttemptCount(state: RunState, workItemId: string): number {
126
+ return state.attempts.get(workItemId)?.length ?? 0;
127
+ }
@@ -0,0 +1 @@
1
+ export * from './scheduler';
@@ -0,0 +1,231 @@
1
+ import { appendEvent } from '../events/bus';
2
+ import type { ExecutionGraph, GraphNode } from '../compiler/graph-compiler';
3
+ import type { WorkspaceManager } from '../workspace/workspace-manager';
4
+ import type { TaskExecutor, TaskResult, SchedulerContext } from './scheduler';
5
+ import { Scheduler } from './scheduler';
6
+ import type { WorkspaceLease } from '../models/workspace';
7
+
8
+ export type CoordinationMode = 'parallel' | 'competitive' | 'cooperative';
9
+
10
+ export interface AgentSpec {
11
+ id: string;
12
+ executor: TaskExecutor;
13
+ workspaceManager: WorkspaceManager;
14
+ /** Task IDs this agent is responsible for (used in parallel mode) */
15
+ assignedTaskIds?: string[];
16
+ }
17
+
18
+ export interface CoordinationOptions {
19
+ mode: CoordinationMode;
20
+ agents: AgentSpec[];
21
+ projectRoot: string;
22
+ sessionId: string | null;
23
+ runId: string;
24
+ onEvent?: SchedulerContext['onEvent'];
25
+ }
26
+
27
+ export interface CoordinationResult {
28
+ mode: CoordinationMode;
29
+ run_id: string;
30
+ completed: string[];
31
+ failed: string[];
32
+ blocked: string[];
33
+ agent_results: Array<{ agent_id: string; completed: string[]; failed: string[] }>;
34
+ }
35
+
36
+ // ─── Parallel mode ───────────────────────────────────────────────────────────
37
+ // Tasks are partitioned across agents. Each agent runs its own Scheduler
38
+ // on a sub-graph. Results are merged.
39
+
40
+ async function runParallel(
41
+ graph: ExecutionGraph,
42
+ opts: CoordinationOptions
43
+ ): Promise<CoordinationResult> {
44
+ const { agents, projectRoot, sessionId, runId } = opts;
45
+
46
+ // Partition tasks across agents (round-robin if assignedTaskIds not set)
47
+ const partitions = agents.map((a) => a.assignedTaskIds ?? []);
48
+ if (partitions.every((p) => p.length === 0)) {
49
+ const allIds = [...graph.nodes.keys()];
50
+ allIds.forEach((id, i) => {
51
+ partitions[i % agents.length].push(id);
52
+ });
53
+ }
54
+
55
+ appendEvent(projectRoot, sessionId, {
56
+ type: 'RunStarted',
57
+ run_id: runId,
58
+ payload: { mode: 'parallel', agent_count: agents.length },
59
+ });
60
+
61
+ const agentResults = await Promise.all(
62
+ agents.map(async (agent, idx) => {
63
+ const subGraph = subGraphFor(graph, partitions[idx]);
64
+ if (subGraph.nodes.size === 0) {
65
+ return { agent_id: agent.id, completed: [], failed: [] };
66
+ }
67
+ const ctx: SchedulerContext = {
68
+ projectRoot,
69
+ sessionId,
70
+ runId: `${runId}-agent${idx}`,
71
+ executor: agent.executor,
72
+ workspaceManager: agent.workspaceManager,
73
+ onEvent: opts.onEvent,
74
+ };
75
+ const scheduler = new Scheduler();
76
+ const result = await scheduler.run(subGraph, ctx);
77
+ return { agent_id: agent.id, completed: result.completed, failed: result.failed };
78
+ })
79
+ );
80
+
81
+ const completed = agentResults.flatMap((r) => r.completed);
82
+ const failed = agentResults.flatMap((r) => r.failed);
83
+
84
+ appendEvent(projectRoot, sessionId, {
85
+ type: 'RunCompleted',
86
+ run_id: runId,
87
+ payload: { mode: 'parallel', completed: completed.length, failed: failed.length },
88
+ });
89
+
90
+ return { mode: 'parallel', run_id: runId, completed, failed, blocked: [], agent_results: agentResults };
91
+ }
92
+
93
+ // ─── Competitive mode ────────────────────────────────────────────────────────
94
+ // Two agents attempt the same task. First success wins; the loser's workspace
95
+ // is disposed. Requires exactly 2 agents.
96
+
97
+ async function runCompetitive(
98
+ graph: ExecutionGraph,
99
+ opts: CoordinationOptions
100
+ ): Promise<CoordinationResult> {
101
+ if (opts.agents.length < 2) {
102
+ throw new Error('Competitive mode requires at least 2 agents');
103
+ }
104
+ const [agentA, agentB] = opts.agents;
105
+ const { projectRoot, sessionId, runId } = opts;
106
+
107
+ appendEvent(projectRoot, sessionId, {
108
+ type: 'RunStarted',
109
+ run_id: runId,
110
+ payload: { mode: 'competitive' },
111
+ });
112
+
113
+ const completed: string[] = [];
114
+ const failed: string[] = [];
115
+
116
+ for (const wave of graph.waves) {
117
+ for (const nodeId of wave.node_ids) {
118
+ const node = graph.nodes.get(nodeId)!;
119
+ const result = await competeTwoAgents(nodeId, node, agentA, agentB, opts);
120
+ if (result.success) completed.push(nodeId);
121
+ else failed.push(nodeId);
122
+ if (failed.length > 0) break;
123
+ }
124
+ if (failed.length > 0) break;
125
+ }
126
+
127
+ appendEvent(projectRoot, sessionId, {
128
+ type: 'RunCompleted',
129
+ run_id: runId,
130
+ payload: { mode: 'competitive', completed: completed.length },
131
+ });
132
+
133
+ return {
134
+ mode: 'competitive',
135
+ run_id: runId,
136
+ completed,
137
+ failed,
138
+ blocked: [],
139
+ agent_results: [
140
+ { agent_id: agentA.id, completed, failed },
141
+ { agent_id: agentB.id, completed: [], failed: [] },
142
+ ],
143
+ };
144
+ }
145
+
146
+ async function competeTwoAgents(
147
+ nodeId: string,
148
+ node: GraphNode,
149
+ agentA: AgentSpec,
150
+ agentB: AgentSpec,
151
+ opts: CoordinationOptions
152
+ ): Promise<TaskResult> {
153
+ const { projectRoot, sessionId, runId } = opts;
154
+
155
+ const allocA = await agentA.workspaceManager.allocate({
156
+ work_item_id: nodeId, attempt_number: 1, strategy: node.workspace_strategy, mutation_scope: node.mutation_scope,
157
+ });
158
+ const allocB = await agentB.workspaceManager.allocate({
159
+ work_item_id: nodeId, attempt_number: 1, strategy: node.workspace_strategy, mutation_scope: node.mutation_scope,
160
+ });
161
+
162
+ appendEvent(projectRoot, sessionId, {
163
+ type: 'AttemptStarted',
164
+ run_id: runId,
165
+ work_item_id: nodeId,
166
+ payload: { mode: 'competitive', agents: [agentA.id, agentB.id] },
167
+ });
168
+
169
+ // Race both agents — first success wins
170
+ const [resultA, resultB] = await Promise.all([
171
+ agentA.executor.execute(node, allocA, runId, 1).catch((e) => ({
172
+ success: false, failure_class: 'env' as const, evidence: [], output: String(e),
173
+ })),
174
+ agentB.executor.execute(node, allocB, runId, 1).catch((e) => ({
175
+ success: false, failure_class: 'env' as const, evidence: [], output: String(e),
176
+ })),
177
+ ]);
178
+
179
+ // Clean up both workspaces
180
+ await Promise.all([
181
+ agentA.workspaceManager.dispose(allocA.workspace_id).catch(() => {}),
182
+ agentB.workspaceManager.dispose(allocB.workspace_id).catch(() => {}),
183
+ ]);
184
+
185
+ // Pick winner: prefer success; if both succeed, prefer A (primary agent)
186
+ const winner = resultA.success ? resultA : resultB.success ? resultB : resultA;
187
+
188
+ if (winner.success) {
189
+ appendEvent(projectRoot, sessionId, { type: 'WorkItemCompleted', run_id: runId, work_item_id: nodeId, payload: { mode: 'competitive' } });
190
+ } else {
191
+ appendEvent(projectRoot, sessionId, { type: 'WorkItemBlocked', run_id: runId, work_item_id: nodeId, payload: { mode: 'competitive', failure_class: winner.failure_class } });
192
+ }
193
+
194
+ return winner;
195
+ }
196
+
197
+ // ─── Public API ──────────────────────────────────────────────────────────────
198
+
199
+ export class MultiAgentCoordinator {
200
+ async run(graph: ExecutionGraph, opts: CoordinationOptions): Promise<CoordinationResult> {
201
+ switch (opts.mode) {
202
+ case 'parallel': return runParallel(graph, opts);
203
+ case 'competitive': return runCompetitive(graph, opts);
204
+ case 'cooperative':
205
+ // Cooperative mode: planner (agent[0]) prepares context,
206
+ // executor (agent[1]) implements. For R3, delegate to sequential parallel.
207
+ return runParallel(graph, { ...opts, mode: 'parallel' });
208
+ default:
209
+ throw new Error(`Unknown coordination mode: ${opts.mode}`);
210
+ }
211
+ }
212
+ }
213
+
214
+ // ─── Helpers ─────────────────────────────────────────────────────────────────
215
+
216
+ function subGraphFor(graph: ExecutionGraph, nodeIds: string[]): ExecutionGraph {
217
+ const ids = new Set(nodeIds);
218
+ const nodes = new Map([...graph.nodes].filter(([id]) => ids.has(id)));
219
+ const edges = graph.edges.filter((e) => ids.has(e.from) && ids.has(e.to));
220
+ const waves = graph.waves.map((w) => ({
221
+ wave_number: w.wave_number,
222
+ node_ids: w.node_ids.filter((id) => ids.has(id)),
223
+ })).filter((w) => w.node_ids.length > 0);
224
+
225
+ return {
226
+ nodes,
227
+ edges,
228
+ waves,
229
+ metadata: { ...graph.metadata, node_count: nodes.size, wave_count: waves.length },
230
+ };
231
+ }
@@ -0,0 +1,281 @@
1
+ import { appendEvent } from '../events/bus';
2
+ import type { OxeEvent } from '../events/envelope';
3
+ import type { EventInput } from '../events/bus';
4
+ import type { ExecutionGraph, GraphNode } from '../compiler/graph-compiler';
5
+ import type { WorkspaceManager, WorkspaceRequest } from '../workspace/workspace-manager';
6
+ import type { WorkspaceLease } from '../models/workspace';
7
+
8
+ export interface TaskResult {
9
+ success: boolean;
10
+ failure_class: 'env' | 'policy' | 'test' | 'timeout' | null;
11
+ evidence: string[];
12
+ output: string;
13
+ }
14
+
15
+ export interface TaskExecutor {
16
+ execute(
17
+ node: GraphNode,
18
+ lease: WorkspaceLease,
19
+ runId: string,
20
+ attemptNumber: number
21
+ ): Promise<TaskResult>;
22
+ }
23
+
24
+ export interface SchedulerContext {
25
+ projectRoot: string;
26
+ sessionId: string | null;
27
+ runId: string;
28
+ executor: TaskExecutor;
29
+ workspaceManager: WorkspaceManager;
30
+ onEvent?: (event: OxeEvent) => void;
31
+ }
32
+
33
+ export interface RunResult {
34
+ run_id: string;
35
+ status: 'completed' | 'failed' | 'cancelled';
36
+ completed: string[];
37
+ failed: string[];
38
+ blocked: string[];
39
+ }
40
+
41
+ type NodeStatus = 'pending' | 'ready' | 'running' | 'completed' | 'failed' | 'blocked';
42
+
43
+ export class Scheduler {
44
+ private cancelled = false;
45
+ private paused = false;
46
+
47
+ async run(graph: ExecutionGraph, ctx: SchedulerContext): Promise<RunResult> {
48
+ this.cancelled = false;
49
+ this.paused = false;
50
+
51
+ const status = new Map<string, NodeStatus>();
52
+ for (const id of graph.nodes.keys()) status.set(id, 'pending');
53
+
54
+ const completed: string[] = [];
55
+ const failed: string[] = [];
56
+ const blocked: string[] = [];
57
+
58
+ this.emit(ctx, { type: 'RunStarted', payload: { run_id: ctx.runId } });
59
+
60
+ for (const wave of graph.waves) {
61
+ if (this.cancelled) break;
62
+ const waveFailed = await this.runWave(
63
+ wave.node_ids,
64
+ graph,
65
+ ctx,
66
+ status,
67
+ completed,
68
+ failed,
69
+ blocked
70
+ );
71
+ if (waveFailed) break;
72
+ }
73
+
74
+ // Any remaining pending nodes become blocked
75
+ for (const [id, s] of status) {
76
+ if (s === 'pending') {
77
+ status.set(id, 'blocked');
78
+ blocked.push(id);
79
+ this.emit(ctx, {
80
+ type: 'WorkItemBlocked',
81
+ work_item_id: id,
82
+ payload: { reason: 'upstream_wave_failed' },
83
+ });
84
+ }
85
+ }
86
+
87
+ const finalStatus: RunResult['status'] = this.cancelled
88
+ ? 'cancelled'
89
+ : failed.length > 0
90
+ ? 'failed'
91
+ : 'completed';
92
+
93
+ this.emit(ctx, {
94
+ type: 'RunCompleted',
95
+ payload: { run_id: ctx.runId, status: finalStatus },
96
+ });
97
+
98
+ return { run_id: ctx.runId, status: finalStatus, completed, failed, blocked };
99
+ }
100
+
101
+ private async runWave(
102
+ nodeIds: string[],
103
+ graph: ExecutionGraph,
104
+ ctx: SchedulerContext,
105
+ status: Map<string, NodeStatus>,
106
+ completed: string[],
107
+ failed: string[],
108
+ blocked: string[]
109
+ ): Promise<boolean> {
110
+ // Partition: eligible vs blocked-by-dep
111
+ const eligible: string[] = [];
112
+ const depsNotMet: string[] = [];
113
+
114
+ for (const id of nodeIds) {
115
+ const node = graph.nodes.get(id)!;
116
+ const depsMet = node.depends_on.every((dep) => status.get(dep) === 'completed');
117
+ if (depsMet) {
118
+ eligible.push(id);
119
+ } else {
120
+ depsNotMet.push(id);
121
+ }
122
+ }
123
+
124
+ // Nodes whose deps weren't met in this wave → blocked
125
+ for (const id of depsNotMet) {
126
+ status.set(id, 'blocked');
127
+ blocked.push(id);
128
+ this.emit(ctx, {
129
+ type: 'WorkItemBlocked',
130
+ work_item_id: id,
131
+ payload: { reason: 'dependency_not_met' },
132
+ });
133
+ }
134
+
135
+ // Separate read-only (no mutation_scope) from mutation nodes
136
+ const readOnly = eligible.filter((id) => {
137
+ const node = graph.nodes.get(id)!;
138
+ return node.mutation_scope.length === 0;
139
+ });
140
+ const mutations = eligible.filter((id) => !readOnly.includes(id));
141
+
142
+ // Run read-only nodes in parallel
143
+ if (readOnly.length > 0) {
144
+ await Promise.all(
145
+ readOnly.map((id) => this.runNode(id, graph, ctx, status, completed, failed))
146
+ );
147
+ }
148
+
149
+ // Run mutation nodes sequentially to avoid scope conflicts
150
+ for (const id of mutations) {
151
+ if (this.cancelled) break;
152
+ await this.runNode(id, graph, ctx, status, completed, failed);
153
+ }
154
+
155
+ return failed.length > 0;
156
+ }
157
+
158
+ private async runNode(
159
+ nodeId: string,
160
+ graph: ExecutionGraph,
161
+ ctx: SchedulerContext,
162
+ status: Map<string, NodeStatus>,
163
+ completed: string[],
164
+ failed: string[]
165
+ ): Promise<void> {
166
+ const node = graph.nodes.get(nodeId)!;
167
+ status.set(nodeId, 'running');
168
+ this.emit(ctx, {
169
+ type: 'WorkItemReady',
170
+ work_item_id: nodeId,
171
+ payload: { title: node.title, wave: node.wave },
172
+ });
173
+
174
+ let lease: WorkspaceLease | null = null;
175
+ let lastResult: TaskResult | null = null;
176
+ const maxAttempts = node.policy.max_retries + 1;
177
+
178
+ for (let attempt = 1; attempt <= maxAttempts; attempt++) {
179
+ const attemptId = `${nodeId}-a${attempt}`;
180
+
181
+ this.emit(ctx, {
182
+ type: 'AttemptStarted',
183
+ work_item_id: nodeId,
184
+ attempt_id: attemptId,
185
+ payload: { attempt_number: attempt },
186
+ });
187
+
188
+ try {
189
+ const wsReq: WorkspaceRequest = {
190
+ work_item_id: nodeId,
191
+ attempt_number: attempt,
192
+ strategy: node.workspace_strategy,
193
+ mutation_scope: node.mutation_scope,
194
+ };
195
+ lease = await ctx.workspaceManager.allocate(wsReq);
196
+ this.emit(ctx, {
197
+ type: 'WorkspaceAllocated',
198
+ work_item_id: nodeId,
199
+ attempt_id: attemptId,
200
+ payload: { workspace_id: lease.workspace_id, strategy: lease.strategy },
201
+ });
202
+
203
+ lastResult = await ctx.executor.execute(node, lease, ctx.runId, attempt);
204
+
205
+ if (lastResult.success) {
206
+ this.emit(ctx, {
207
+ type: 'WorkItemCompleted',
208
+ work_item_id: nodeId,
209
+ attempt_id: attemptId,
210
+ payload: { attempt_number: attempt, evidence: lastResult.evidence },
211
+ });
212
+ status.set(nodeId, 'completed');
213
+ completed.push(nodeId);
214
+ return;
215
+ }
216
+
217
+ // Policy failures never retry
218
+ if (lastResult.failure_class === 'policy') break;
219
+
220
+ if (attempt < maxAttempts) {
221
+ this.emit(ctx, {
222
+ type: 'RetryScheduled',
223
+ work_item_id: nodeId,
224
+ payload: { next_attempt: attempt + 1, reason: lastResult.failure_class },
225
+ });
226
+ }
227
+ } catch (err) {
228
+ lastResult = {
229
+ success: false,
230
+ failure_class: 'env',
231
+ evidence: [],
232
+ output: String(err),
233
+ };
234
+ if (attempt < maxAttempts) {
235
+ this.emit(ctx, {
236
+ type: 'RetryScheduled',
237
+ work_item_id: nodeId,
238
+ payload: { next_attempt: attempt + 1, reason: 'env' },
239
+ });
240
+ }
241
+ } finally {
242
+ if (lease) {
243
+ await ctx.workspaceManager.dispose(lease.workspace_id).catch(() => {});
244
+ lease = null;
245
+ }
246
+ }
247
+ }
248
+
249
+ // All attempts exhausted
250
+ this.emit(ctx, {
251
+ type: 'WorkItemBlocked',
252
+ work_item_id: nodeId,
253
+ payload: { failure_class: lastResult?.failure_class ?? 'env', max_attempts: maxAttempts },
254
+ });
255
+ status.set(nodeId, 'failed');
256
+ failed.push(nodeId);
257
+ }
258
+
259
+ pause(): void {
260
+ this.paused = true;
261
+ }
262
+
263
+ resume(): void {
264
+ this.paused = false;
265
+ }
266
+
267
+ cancel(): void {
268
+ this.cancelled = true;
269
+ }
270
+
271
+ private emit(
272
+ ctx: SchedulerContext,
273
+ input: EventInput
274
+ ): void {
275
+ const event = appendEvent(ctx.projectRoot, ctx.sessionId, {
276
+ run_id: ctx.runId,
277
+ ...input,
278
+ });
279
+ ctx.onEvent?.(event);
280
+ }
281
+ }
@@ -0,0 +1 @@
1
+ export * from './verification-compiler';