@femtomc/mu-server 26.2.68 → 26.2.70
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.
- package/README.md +15 -10
- package/dist/api/events.js +77 -19
- package/dist/api/forum.js +52 -18
- package/dist/api/issues.js +120 -33
- package/dist/cli.js +8 -6
- package/dist/control_plane.d.ts +6 -115
- package/dist/control_plane.js +16 -654
- package/dist/control_plane_bootstrap_helpers.d.ts +15 -0
- package/dist/control_plane_bootstrap_helpers.js +75 -0
- package/dist/control_plane_contract.d.ts +119 -0
- package/dist/control_plane_contract.js +1 -0
- package/dist/control_plane_run_outbox.d.ts +7 -0
- package/dist/control_plane_run_outbox.js +52 -0
- package/dist/control_plane_telegram_generation.d.ts +27 -0
- package/dist/control_plane_telegram_generation.js +521 -0
- package/dist/cron_request.d.ts +8 -0
- package/dist/cron_request.js +65 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.js +2 -1
- package/dist/server.d.ts +11 -13
- package/dist/server.js +46 -1598
- package/dist/server_program_orchestration.d.ts +38 -0
- package/dist/server_program_orchestration.js +252 -0
- package/dist/server_routing.d.ts +30 -0
- package/dist/server_routing.js +1102 -0
- package/dist/server_runtime.d.ts +30 -0
- package/dist/server_runtime.js +43 -0
- package/dist/server_types.d.ts +3 -0
- package/dist/server_types.js +16 -0
- package/dist/session_lifecycle.d.ts +11 -0
- package/dist/session_lifecycle.js +149 -0
- package/package.json +6 -6
package/dist/server.js
CHANGED
|
@@ -1,57 +1,19 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { GenerationTelemetryRecorder, getControlPlanePaths, IdentityStore, ROLE_SCOPES, } from "@femtomc/mu-control-plane";
|
|
1
|
+
import { GenerationTelemetryRecorder, } from "@femtomc/mu-control-plane";
|
|
3
2
|
import { currentRunId, EventLog, FsJsonlStore, getStorePaths, JsonlEventSink } from "@femtomc/mu-core/node";
|
|
4
3
|
import { ForumStore } from "@femtomc/mu-forum";
|
|
5
4
|
import { IssueStore } from "@femtomc/mu-issue";
|
|
6
5
|
import { ControlPlaneActivitySupervisor } from "./activity_supervisor.js";
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { issueRoutes } from "./api/issues.js";
|
|
10
|
-
import { applyMuConfigPatch, DEFAULT_MU_CONFIG, getMuConfigPath, muConfigPresence, readMuConfigFile, redactMuConfigSecrets, writeMuConfigFile, } from "./config.js";
|
|
11
|
-
import { bootstrapControlPlane, } from "./control_plane.js";
|
|
12
|
-
import { CronProgramRegistry } from "./cron_programs.js";
|
|
6
|
+
import { DEFAULT_MU_CONFIG, readMuConfigFile, writeMuConfigFile, } from "./config.js";
|
|
7
|
+
import { bootstrapControlPlane } from "./control_plane.js";
|
|
13
8
|
import { ControlPlaneGenerationSupervisor } from "./generation_supervisor.js";
|
|
14
|
-
import { HeartbeatProgramRegistry, } from "./heartbeat_programs.js";
|
|
15
9
|
import { ActivityHeartbeatScheduler } from "./heartbeat_scheduler.js";
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
".json": "application/json",
|
|
21
|
-
".png": "image/png",
|
|
22
|
-
".jpg": "image/jpeg",
|
|
23
|
-
".svg": "image/svg+xml",
|
|
24
|
-
".ico": "image/x-icon",
|
|
25
|
-
".woff": "font/woff",
|
|
26
|
-
".woff2": "font/woff2",
|
|
27
|
-
};
|
|
28
|
-
// Resolve public/ dir relative to this file (works in npm global installs)
|
|
29
|
-
const PUBLIC_DIR = join(new URL(".", import.meta.url).pathname, "..", "public");
|
|
10
|
+
import { createProcessSessionLifecycle } from "./session_lifecycle.js";
|
|
11
|
+
import { createServerProgramOrchestration } from "./server_program_orchestration.js";
|
|
12
|
+
import { createServerRequestHandler } from "./server_routing.js";
|
|
13
|
+
import { toNonNegativeInt } from "./server_types.js";
|
|
30
14
|
const DEFAULT_OPERATOR_WAKE_COALESCE_MS = 2_000;
|
|
31
15
|
const DEFAULT_AUTO_RUN_HEARTBEAT_EVERY_MS = 15_000;
|
|
32
|
-
|
|
33
|
-
function normalizeWakeMode(value) {
|
|
34
|
-
if (typeof value !== "string") {
|
|
35
|
-
return "immediate";
|
|
36
|
-
}
|
|
37
|
-
const normalized = value.trim().toLowerCase().replaceAll("-", "_");
|
|
38
|
-
return normalized === "next_heartbeat" ? "next_heartbeat" : "immediate";
|
|
39
|
-
}
|
|
40
|
-
function toNonNegativeInt(value, fallback) {
|
|
41
|
-
if (typeof value === "number" && Number.isFinite(value)) {
|
|
42
|
-
return Math.max(0, Math.trunc(value));
|
|
43
|
-
}
|
|
44
|
-
if (typeof value === "string" && /^\d+$/.test(value.trim())) {
|
|
45
|
-
return Math.max(0, Number.parseInt(value, 10));
|
|
46
|
-
}
|
|
47
|
-
return Math.max(0, Math.trunc(fallback));
|
|
48
|
-
}
|
|
49
|
-
function shellQuoteArg(value) {
|
|
50
|
-
return `'${value.replaceAll("'", `'"'"'`)}'`;
|
|
51
|
-
}
|
|
52
|
-
function shellJoin(args) {
|
|
53
|
-
return args.map(shellQuoteArg).join(" ");
|
|
54
|
-
}
|
|
16
|
+
export { createProcessSessionLifecycle };
|
|
55
17
|
function describeError(err) {
|
|
56
18
|
if (err instanceof Error)
|
|
57
19
|
return err.message;
|
|
@@ -67,71 +29,6 @@ function summarizeControlPlane(handle) {
|
|
|
67
29
|
routes: handle.activeAdapters.map((adapter) => ({ name: adapter.name, route: adapter.route })),
|
|
68
30
|
};
|
|
69
31
|
}
|
|
70
|
-
function parseCronTarget(body) {
|
|
71
|
-
const targetKind = typeof body.target_kind === "string" ? body.target_kind.trim().toLowerCase() : "";
|
|
72
|
-
if (targetKind === "run") {
|
|
73
|
-
const jobId = typeof body.run_job_id === "string" ? body.run_job_id.trim() : "";
|
|
74
|
-
const rootIssueId = typeof body.run_root_issue_id === "string" ? body.run_root_issue_id.trim() : "";
|
|
75
|
-
if (!jobId && !rootIssueId) {
|
|
76
|
-
return {
|
|
77
|
-
target: null,
|
|
78
|
-
error: "run target requires run_job_id or run_root_issue_id",
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
return {
|
|
82
|
-
target: {
|
|
83
|
-
kind: "run",
|
|
84
|
-
job_id: jobId || null,
|
|
85
|
-
root_issue_id: rootIssueId || null,
|
|
86
|
-
},
|
|
87
|
-
error: null,
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
if (targetKind === "activity") {
|
|
91
|
-
const activityId = typeof body.activity_id === "string" ? body.activity_id.trim() : "";
|
|
92
|
-
if (!activityId) {
|
|
93
|
-
return {
|
|
94
|
-
target: null,
|
|
95
|
-
error: "activity target requires activity_id",
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
return {
|
|
99
|
-
target: {
|
|
100
|
-
kind: "activity",
|
|
101
|
-
activity_id: activityId,
|
|
102
|
-
},
|
|
103
|
-
error: null,
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
return {
|
|
107
|
-
target: null,
|
|
108
|
-
error: "target_kind must be run or activity",
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
function hasCronScheduleInput(body) {
|
|
112
|
-
return (body.schedule != null ||
|
|
113
|
-
body.schedule_kind != null ||
|
|
114
|
-
body.at_ms != null ||
|
|
115
|
-
body.at != null ||
|
|
116
|
-
body.every_ms != null ||
|
|
117
|
-
body.anchor_ms != null ||
|
|
118
|
-
body.expr != null ||
|
|
119
|
-
body.tz != null);
|
|
120
|
-
}
|
|
121
|
-
function cronScheduleInputFromBody(body) {
|
|
122
|
-
if (body.schedule && typeof body.schedule === "object" && !Array.isArray(body.schedule)) {
|
|
123
|
-
return { ...body.schedule };
|
|
124
|
-
}
|
|
125
|
-
return {
|
|
126
|
-
kind: typeof body.schedule_kind === "string" ? body.schedule_kind : undefined,
|
|
127
|
-
at_ms: body.at_ms,
|
|
128
|
-
at: body.at,
|
|
129
|
-
every_ms: body.every_ms,
|
|
130
|
-
anchor_ms: body.anchor_ms,
|
|
131
|
-
expr: body.expr,
|
|
132
|
-
tz: body.tz,
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
32
|
export function createContext(repoRoot) {
|
|
136
33
|
const paths = getStorePaths(repoRoot);
|
|
137
34
|
const eventsStore = new FsJsonlStore(paths.eventsPath);
|
|
@@ -142,7 +39,7 @@ export function createContext(repoRoot) {
|
|
|
142
39
|
const forumStore = new ForumStore(new FsJsonlStore(paths.forumPath), { events: eventLog });
|
|
143
40
|
return { repoRoot, issueStore, forumStore, eventLog, eventsStore };
|
|
144
41
|
}
|
|
145
|
-
|
|
42
|
+
function createServer(options = {}) {
|
|
146
43
|
const repoRoot = options.repoRoot || process.cwd();
|
|
147
44
|
const context = createContext(repoRoot);
|
|
148
45
|
const readConfig = options.configReader ?? readMuConfigFile;
|
|
@@ -170,145 +67,7 @@ export function createServer(options = {}) {
|
|
|
170
67
|
const operatorWakeCoalesceMs = toNonNegativeInt(options.operatorWakeCoalesceMs, DEFAULT_OPERATOR_WAKE_COALESCE_MS);
|
|
171
68
|
const autoRunHeartbeatEveryMs = Math.max(1_000, toNonNegativeInt(options.autoRunHeartbeatEveryMs, DEFAULT_AUTO_RUN_HEARTBEAT_EVERY_MS));
|
|
172
69
|
const operatorWakeLastByKey = new Map();
|
|
173
|
-
const
|
|
174
|
-
let sessionMutationScheduled = null;
|
|
175
|
-
const runShellCommand = async (command) => {
|
|
176
|
-
const proc = Bun.spawn({
|
|
177
|
-
cmd: ["bash", "-lc", command],
|
|
178
|
-
cwd: repoRoot,
|
|
179
|
-
env: Bun.env,
|
|
180
|
-
stdin: "ignore",
|
|
181
|
-
stdout: "pipe",
|
|
182
|
-
stderr: "pipe",
|
|
183
|
-
});
|
|
184
|
-
const [exitCode, stdout, stderr] = await Promise.all([
|
|
185
|
-
proc.exited,
|
|
186
|
-
proc.stdout ? new Response(proc.stdout).text() : Promise.resolve(""),
|
|
187
|
-
proc.stderr ? new Response(proc.stderr).text() : Promise.resolve(""),
|
|
188
|
-
]);
|
|
189
|
-
return {
|
|
190
|
-
exitCode: Number.isFinite(exitCode) ? Number(exitCode) : 1,
|
|
191
|
-
stdout,
|
|
192
|
-
stderr,
|
|
193
|
-
};
|
|
194
|
-
};
|
|
195
|
-
const defaultSessionMutationHooks = {
|
|
196
|
-
reload: async () => {
|
|
197
|
-
if (sessionMutationScheduled) {
|
|
198
|
-
return {
|
|
199
|
-
ok: true,
|
|
200
|
-
action: sessionMutationScheduled.action,
|
|
201
|
-
message: `session ${sessionMutationScheduled.action} already scheduled`,
|
|
202
|
-
details: { scheduled_at_ms: sessionMutationScheduled.at_ms },
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
const nowMs = Date.now();
|
|
206
|
-
const restartCommand = Bun.env.MU_RESTART_COMMAND?.trim();
|
|
207
|
-
const inferredArgs = process.argv[0] === process.execPath
|
|
208
|
-
? [process.execPath, ...process.argv.slice(1)]
|
|
209
|
-
: [process.execPath, ...process.argv];
|
|
210
|
-
const restartShellCommand = restartCommand && restartCommand.length > 0 ? restartCommand : shellJoin(inferredArgs);
|
|
211
|
-
if (!restartShellCommand.trim()) {
|
|
212
|
-
return {
|
|
213
|
-
ok: false,
|
|
214
|
-
action: "reload",
|
|
215
|
-
message: "unable to determine restart command",
|
|
216
|
-
};
|
|
217
|
-
}
|
|
218
|
-
const exitDelayMs = 1_000;
|
|
219
|
-
const launchDelayMs = exitDelayMs + 300;
|
|
220
|
-
const delayedShellCommand = `sleep ${(launchDelayMs / 1_000).toFixed(2)}; ${restartShellCommand}`;
|
|
221
|
-
let spawnedPid = null;
|
|
222
|
-
try {
|
|
223
|
-
const proc = Bun.spawn({
|
|
224
|
-
cmd: ["bash", "-lc", delayedShellCommand],
|
|
225
|
-
cwd: repoRoot,
|
|
226
|
-
env: Bun.env,
|
|
227
|
-
stdin: "ignore",
|
|
228
|
-
stdout: "inherit",
|
|
229
|
-
stderr: "inherit",
|
|
230
|
-
});
|
|
231
|
-
spawnedPid = proc.pid ?? null;
|
|
232
|
-
}
|
|
233
|
-
catch (err) {
|
|
234
|
-
return {
|
|
235
|
-
ok: false,
|
|
236
|
-
action: "reload",
|
|
237
|
-
message: `failed to spawn replacement process: ${describeError(err)}`,
|
|
238
|
-
};
|
|
239
|
-
}
|
|
240
|
-
sessionMutationScheduled = { action: "reload", at_ms: nowMs };
|
|
241
|
-
setTimeout(() => {
|
|
242
|
-
process.exit(0);
|
|
243
|
-
}, exitDelayMs);
|
|
244
|
-
return {
|
|
245
|
-
ok: true,
|
|
246
|
-
action: "reload",
|
|
247
|
-
message: "reload scheduled; restarting process",
|
|
248
|
-
details: {
|
|
249
|
-
restart_command: restartShellCommand,
|
|
250
|
-
restart_launch_command: delayedShellCommand,
|
|
251
|
-
spawned_pid: spawnedPid,
|
|
252
|
-
exit_delay_ms: exitDelayMs,
|
|
253
|
-
launch_delay_ms: launchDelayMs,
|
|
254
|
-
},
|
|
255
|
-
};
|
|
256
|
-
},
|
|
257
|
-
update: async () => {
|
|
258
|
-
if (sessionMutationScheduled) {
|
|
259
|
-
return {
|
|
260
|
-
ok: true,
|
|
261
|
-
action: sessionMutationScheduled.action,
|
|
262
|
-
message: `session ${sessionMutationScheduled.action} already scheduled`,
|
|
263
|
-
details: { scheduled_at_ms: sessionMutationScheduled.at_ms },
|
|
264
|
-
};
|
|
265
|
-
}
|
|
266
|
-
const updateCommand = Bun.env.MU_UPDATE_COMMAND?.trim() || "npm install -g @femtomc/mu@latest";
|
|
267
|
-
const result = await runShellCommand(updateCommand);
|
|
268
|
-
if (result.exitCode !== 0) {
|
|
269
|
-
return {
|
|
270
|
-
ok: false,
|
|
271
|
-
action: "update",
|
|
272
|
-
message: `update command failed (exit ${result.exitCode})`,
|
|
273
|
-
details: {
|
|
274
|
-
update_command: updateCommand,
|
|
275
|
-
stdout: result.stdout.slice(-4_000),
|
|
276
|
-
stderr: result.stderr.slice(-4_000),
|
|
277
|
-
},
|
|
278
|
-
};
|
|
279
|
-
}
|
|
280
|
-
const reloadResult = await defaultSessionMutationHooks.reload?.();
|
|
281
|
-
if (!reloadResult) {
|
|
282
|
-
return {
|
|
283
|
-
ok: false,
|
|
284
|
-
action: "update",
|
|
285
|
-
message: "reload hook unavailable after update",
|
|
286
|
-
};
|
|
287
|
-
}
|
|
288
|
-
if (!reloadResult.ok) {
|
|
289
|
-
return {
|
|
290
|
-
ok: false,
|
|
291
|
-
action: "update",
|
|
292
|
-
message: reloadResult.message,
|
|
293
|
-
details: {
|
|
294
|
-
update_command: updateCommand,
|
|
295
|
-
reload: reloadResult.details ?? null,
|
|
296
|
-
},
|
|
297
|
-
};
|
|
298
|
-
}
|
|
299
|
-
return {
|
|
300
|
-
ok: true,
|
|
301
|
-
action: "update",
|
|
302
|
-
message: "update applied; reload scheduled",
|
|
303
|
-
details: {
|
|
304
|
-
update_command: updateCommand,
|
|
305
|
-
reload: reloadResult.details ?? null,
|
|
306
|
-
update_stdout_tail: result.stdout.slice(-1_000),
|
|
307
|
-
},
|
|
308
|
-
};
|
|
309
|
-
},
|
|
310
|
-
};
|
|
311
|
-
const sessionMutationHooks = options.sessionMutationHooks ?? defaultSessionMutationHooks;
|
|
70
|
+
const sessionLifecycle = options.sessionLifecycle ?? createProcessSessionLifecycle({ repoRoot });
|
|
312
71
|
const emitOperatorWake = async (opts) => {
|
|
313
72
|
const dedupeKey = opts.dedupeKey.trim();
|
|
314
73
|
if (!dedupeKey) {
|
|
@@ -358,7 +117,7 @@ export function createServer(options = {}) {
|
|
|
358
117
|
heartbeatScheduler,
|
|
359
118
|
generation,
|
|
360
119
|
telemetry: generationTelemetry,
|
|
361
|
-
|
|
120
|
+
sessionLifecycle,
|
|
362
121
|
terminalEnabled: true,
|
|
363
122
|
});
|
|
364
123
|
});
|
|
@@ -431,246 +190,15 @@ export function createServer(options = {}) {
|
|
|
431
190
|
await handle?.stop();
|
|
432
191
|
},
|
|
433
192
|
};
|
|
434
|
-
const heartbeatPrograms =
|
|
193
|
+
const { heartbeatPrograms, cronPrograms, registerAutoRunHeartbeatProgram, disableAutoRunHeartbeatProgram, } = createServerProgramOrchestration({
|
|
435
194
|
repoRoot,
|
|
436
195
|
heartbeatScheduler,
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
wakeMode: opts.wakeMode,
|
|
443
|
-
});
|
|
444
|
-
return result ?? { ok: false, reason: "not_found" };
|
|
445
|
-
},
|
|
446
|
-
activityHeartbeat: async (opts) => {
|
|
447
|
-
return activitySupervisor.heartbeat({
|
|
448
|
-
activityId: opts.activityId ?? null,
|
|
449
|
-
reason: opts.reason ?? null,
|
|
450
|
-
});
|
|
451
|
-
},
|
|
452
|
-
onTickEvent: async (event) => {
|
|
453
|
-
await context.eventLog.emit("heartbeat_program.tick", {
|
|
454
|
-
source: "mu-server.heartbeat-programs",
|
|
455
|
-
payload: {
|
|
456
|
-
program_id: event.program_id,
|
|
457
|
-
status: event.status,
|
|
458
|
-
reason: event.reason,
|
|
459
|
-
message: event.message,
|
|
460
|
-
program: event.program,
|
|
461
|
-
},
|
|
462
|
-
});
|
|
463
|
-
await emitOperatorWake({
|
|
464
|
-
dedupeKey: `heartbeat-program:${event.program_id}`,
|
|
465
|
-
message: event.message,
|
|
466
|
-
payload: {
|
|
467
|
-
wake_source: "heartbeat_program",
|
|
468
|
-
program_id: event.program_id,
|
|
469
|
-
status: event.status,
|
|
470
|
-
reason: event.reason,
|
|
471
|
-
wake_mode: event.program.wake_mode,
|
|
472
|
-
target_kind: event.program.target.kind,
|
|
473
|
-
target: event.program.target.kind === "run"
|
|
474
|
-
? {
|
|
475
|
-
job_id: event.program.target.job_id,
|
|
476
|
-
root_issue_id: event.program.target.root_issue_id,
|
|
477
|
-
}
|
|
478
|
-
: { activity_id: event.program.target.activity_id },
|
|
479
|
-
},
|
|
480
|
-
});
|
|
481
|
-
},
|
|
482
|
-
});
|
|
483
|
-
const cronPrograms = new CronProgramRegistry({
|
|
484
|
-
repoRoot,
|
|
485
|
-
heartbeatScheduler,
|
|
486
|
-
runHeartbeat: async (opts) => {
|
|
487
|
-
const result = await controlPlaneProxy.heartbeatRun?.({
|
|
488
|
-
jobId: opts.jobId ?? null,
|
|
489
|
-
rootIssueId: opts.rootIssueId ?? null,
|
|
490
|
-
reason: opts.reason ?? null,
|
|
491
|
-
wakeMode: opts.wakeMode,
|
|
492
|
-
});
|
|
493
|
-
return result ?? { ok: false, reason: "not_found" };
|
|
494
|
-
},
|
|
495
|
-
activityHeartbeat: async (opts) => {
|
|
496
|
-
return activitySupervisor.heartbeat({
|
|
497
|
-
activityId: opts.activityId ?? null,
|
|
498
|
-
reason: opts.reason ?? null,
|
|
499
|
-
});
|
|
500
|
-
},
|
|
501
|
-
onLifecycleEvent: async (event) => {
|
|
502
|
-
await context.eventLog.emit("cron_program.lifecycle", {
|
|
503
|
-
source: "mu-server.cron-programs",
|
|
504
|
-
payload: {
|
|
505
|
-
action: event.action,
|
|
506
|
-
program_id: event.program_id,
|
|
507
|
-
message: event.message,
|
|
508
|
-
program: event.program,
|
|
509
|
-
},
|
|
510
|
-
});
|
|
511
|
-
},
|
|
512
|
-
onTickEvent: async (event) => {
|
|
513
|
-
await context.eventLog.emit("cron_program.tick", {
|
|
514
|
-
source: "mu-server.cron-programs",
|
|
515
|
-
payload: {
|
|
516
|
-
program_id: event.program_id,
|
|
517
|
-
status: event.status,
|
|
518
|
-
reason: event.reason,
|
|
519
|
-
message: event.message,
|
|
520
|
-
program: event.program,
|
|
521
|
-
},
|
|
522
|
-
});
|
|
523
|
-
await emitOperatorWake({
|
|
524
|
-
dedupeKey: `cron-program:${event.program_id}`,
|
|
525
|
-
message: event.message,
|
|
526
|
-
payload: {
|
|
527
|
-
wake_source: "cron_program",
|
|
528
|
-
program_id: event.program_id,
|
|
529
|
-
status: event.status,
|
|
530
|
-
reason: event.reason,
|
|
531
|
-
wake_mode: event.program.wake_mode,
|
|
532
|
-
target_kind: event.program.target.kind,
|
|
533
|
-
target: event.program.target.kind === "run"
|
|
534
|
-
? {
|
|
535
|
-
job_id: event.program.target.job_id,
|
|
536
|
-
root_issue_id: event.program.target.root_issue_id,
|
|
537
|
-
}
|
|
538
|
-
: { activity_id: event.program.target.activity_id },
|
|
539
|
-
},
|
|
540
|
-
});
|
|
541
|
-
},
|
|
196
|
+
controlPlaneProxy,
|
|
197
|
+
activitySupervisor,
|
|
198
|
+
eventLog: context.eventLog,
|
|
199
|
+
autoRunHeartbeatEveryMs,
|
|
200
|
+
emitOperatorWake,
|
|
542
201
|
});
|
|
543
|
-
const findAutoRunHeartbeatProgram = async (jobId) => {
|
|
544
|
-
const normalizedJobId = jobId.trim();
|
|
545
|
-
if (!normalizedJobId) {
|
|
546
|
-
return null;
|
|
547
|
-
}
|
|
548
|
-
const knownProgramId = autoRunHeartbeatProgramByJobId.get(normalizedJobId);
|
|
549
|
-
if (knownProgramId) {
|
|
550
|
-
const knownProgram = await heartbeatPrograms.get(knownProgramId);
|
|
551
|
-
if (knownProgram) {
|
|
552
|
-
return knownProgram;
|
|
553
|
-
}
|
|
554
|
-
autoRunHeartbeatProgramByJobId.delete(normalizedJobId);
|
|
555
|
-
}
|
|
556
|
-
const programs = await heartbeatPrograms.list({ targetKind: "run", limit: 500 });
|
|
557
|
-
for (const program of programs) {
|
|
558
|
-
if (program.metadata.auto_run_job_id !== normalizedJobId) {
|
|
559
|
-
continue;
|
|
560
|
-
}
|
|
561
|
-
autoRunHeartbeatProgramByJobId.set(normalizedJobId, program.program_id);
|
|
562
|
-
return program;
|
|
563
|
-
}
|
|
564
|
-
return null;
|
|
565
|
-
};
|
|
566
|
-
const registerAutoRunHeartbeatProgram = async (run) => {
|
|
567
|
-
if (run.source === "command") {
|
|
568
|
-
return;
|
|
569
|
-
}
|
|
570
|
-
const jobId = run.job_id.trim();
|
|
571
|
-
if (!jobId || run.status !== "running") {
|
|
572
|
-
return;
|
|
573
|
-
}
|
|
574
|
-
const rootIssueId = typeof run.root_issue_id === "string" ? run.root_issue_id.trim() : "";
|
|
575
|
-
const metadata = {
|
|
576
|
-
auto_run_heartbeat: true,
|
|
577
|
-
auto_run_job_id: jobId,
|
|
578
|
-
auto_run_root_issue_id: rootIssueId || null,
|
|
579
|
-
auto_disable_on_terminal: true,
|
|
580
|
-
run_mode: run.mode,
|
|
581
|
-
run_source: run.source,
|
|
582
|
-
};
|
|
583
|
-
const existing = await findAutoRunHeartbeatProgram(jobId);
|
|
584
|
-
if (existing) {
|
|
585
|
-
const result = await heartbeatPrograms.update({
|
|
586
|
-
programId: existing.program_id,
|
|
587
|
-
title: `Run heartbeat: ${rootIssueId || jobId}`,
|
|
588
|
-
target: {
|
|
589
|
-
kind: "run",
|
|
590
|
-
job_id: jobId,
|
|
591
|
-
root_issue_id: rootIssueId || null,
|
|
592
|
-
},
|
|
593
|
-
enabled: true,
|
|
594
|
-
everyMs: autoRunHeartbeatEveryMs,
|
|
595
|
-
reason: AUTO_RUN_HEARTBEAT_REASON,
|
|
596
|
-
wakeMode: "next_heartbeat",
|
|
597
|
-
metadata,
|
|
598
|
-
});
|
|
599
|
-
if (result.ok && result.program) {
|
|
600
|
-
autoRunHeartbeatProgramByJobId.set(jobId, result.program.program_id);
|
|
601
|
-
await context.eventLog.emit("run.auto_heartbeat.lifecycle", {
|
|
602
|
-
source: "mu-server.runs",
|
|
603
|
-
payload: {
|
|
604
|
-
action: "updated",
|
|
605
|
-
run_job_id: jobId,
|
|
606
|
-
run_root_issue_id: rootIssueId || null,
|
|
607
|
-
program_id: result.program.program_id,
|
|
608
|
-
program: result.program,
|
|
609
|
-
},
|
|
610
|
-
});
|
|
611
|
-
}
|
|
612
|
-
return;
|
|
613
|
-
}
|
|
614
|
-
const created = await heartbeatPrograms.create({
|
|
615
|
-
title: `Run heartbeat: ${rootIssueId || jobId}`,
|
|
616
|
-
target: {
|
|
617
|
-
kind: "run",
|
|
618
|
-
job_id: jobId,
|
|
619
|
-
root_issue_id: rootIssueId || null,
|
|
620
|
-
},
|
|
621
|
-
everyMs: autoRunHeartbeatEveryMs,
|
|
622
|
-
reason: AUTO_RUN_HEARTBEAT_REASON,
|
|
623
|
-
wakeMode: "next_heartbeat",
|
|
624
|
-
metadata,
|
|
625
|
-
enabled: true,
|
|
626
|
-
});
|
|
627
|
-
autoRunHeartbeatProgramByJobId.set(jobId, created.program_id);
|
|
628
|
-
await context.eventLog.emit("run.auto_heartbeat.lifecycle", {
|
|
629
|
-
source: "mu-server.runs",
|
|
630
|
-
payload: {
|
|
631
|
-
action: "registered",
|
|
632
|
-
run_job_id: jobId,
|
|
633
|
-
run_root_issue_id: rootIssueId || null,
|
|
634
|
-
program_id: created.program_id,
|
|
635
|
-
program: created,
|
|
636
|
-
},
|
|
637
|
-
});
|
|
638
|
-
};
|
|
639
|
-
const disableAutoRunHeartbeatProgram = async (opts) => {
|
|
640
|
-
const program = await findAutoRunHeartbeatProgram(opts.jobId);
|
|
641
|
-
if (!program) {
|
|
642
|
-
return;
|
|
643
|
-
}
|
|
644
|
-
const metadata = {
|
|
645
|
-
...program.metadata,
|
|
646
|
-
auto_disabled_from_status: opts.status,
|
|
647
|
-
auto_disabled_reason: opts.reason,
|
|
648
|
-
auto_disabled_at_ms: Date.now(),
|
|
649
|
-
};
|
|
650
|
-
const result = await heartbeatPrograms.update({
|
|
651
|
-
programId: program.program_id,
|
|
652
|
-
enabled: false,
|
|
653
|
-
everyMs: 0,
|
|
654
|
-
reason: AUTO_RUN_HEARTBEAT_REASON,
|
|
655
|
-
wakeMode: program.wake_mode,
|
|
656
|
-
metadata,
|
|
657
|
-
});
|
|
658
|
-
autoRunHeartbeatProgramByJobId.delete(opts.jobId.trim());
|
|
659
|
-
if (!result.ok || !result.program) {
|
|
660
|
-
return;
|
|
661
|
-
}
|
|
662
|
-
await context.eventLog.emit("run.auto_heartbeat.lifecycle", {
|
|
663
|
-
source: "mu-server.runs",
|
|
664
|
-
payload: {
|
|
665
|
-
action: "disabled",
|
|
666
|
-
run_job_id: opts.jobId,
|
|
667
|
-
status: opts.status,
|
|
668
|
-
reason: opts.reason,
|
|
669
|
-
program_id: result.program.program_id,
|
|
670
|
-
program: result.program,
|
|
671
|
-
},
|
|
672
|
-
});
|
|
673
|
-
};
|
|
674
202
|
const loadConfigFromDisk = async () => {
|
|
675
203
|
try {
|
|
676
204
|
return await readConfig(context.repoRoot);
|
|
@@ -1155,1089 +683,26 @@ export function createServer(options = {}) {
|
|
|
1155
683
|
});
|
|
1156
684
|
return await reloadInFlight;
|
|
1157
685
|
};
|
|
1158
|
-
const handleRequest =
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
config_path: getMuConfigPath(context.repoRoot),
|
|
1179
|
-
config: redactMuConfigSecrets(config),
|
|
1180
|
-
presence: muConfigPresence(config),
|
|
1181
|
-
}, { headers });
|
|
1182
|
-
}
|
|
1183
|
-
catch (err) {
|
|
1184
|
-
return Response.json({ error: `failed to read config: ${describeError(err)}` }, { status: 500, headers });
|
|
1185
|
-
}
|
|
1186
|
-
}
|
|
1187
|
-
if (request.method === "POST") {
|
|
1188
|
-
let body;
|
|
1189
|
-
try {
|
|
1190
|
-
body = (await request.json());
|
|
1191
|
-
}
|
|
1192
|
-
catch {
|
|
1193
|
-
return Response.json({ error: "invalid json body" }, { status: 400, headers });
|
|
1194
|
-
}
|
|
1195
|
-
if (!body || !("patch" in body)) {
|
|
1196
|
-
return Response.json({ error: "missing patch payload" }, { status: 400, headers });
|
|
1197
|
-
}
|
|
1198
|
-
try {
|
|
1199
|
-
const base = await loadConfigFromDisk();
|
|
1200
|
-
const next = applyMuConfigPatch(base, body.patch);
|
|
1201
|
-
const configPath = await writeConfig(context.repoRoot, next);
|
|
1202
|
-
return Response.json({
|
|
1203
|
-
ok: true,
|
|
1204
|
-
repo_root: context.repoRoot,
|
|
1205
|
-
config_path: configPath,
|
|
1206
|
-
config: redactMuConfigSecrets(next),
|
|
1207
|
-
presence: muConfigPresence(next),
|
|
1208
|
-
}, { headers });
|
|
1209
|
-
}
|
|
1210
|
-
catch (err) {
|
|
1211
|
-
return Response.json({ error: `failed to write config: ${describeError(err)}` }, { status: 500, headers });
|
|
1212
|
-
}
|
|
1213
|
-
}
|
|
1214
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
1215
|
-
}
|
|
1216
|
-
if (path === "/api/control-plane/reload") {
|
|
1217
|
-
if (request.method !== "POST") {
|
|
1218
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
1219
|
-
}
|
|
1220
|
-
let reason = "api_control_plane_reload";
|
|
1221
|
-
try {
|
|
1222
|
-
const body = (await request.json());
|
|
1223
|
-
if (typeof body.reason === "string" && body.reason.trim().length > 0) {
|
|
1224
|
-
reason = body.reason.trim();
|
|
1225
|
-
}
|
|
1226
|
-
}
|
|
1227
|
-
catch {
|
|
1228
|
-
// ignore invalid body for reason
|
|
1229
|
-
}
|
|
1230
|
-
const result = await reloadControlPlane(reason);
|
|
1231
|
-
return Response.json(result, { status: result.ok ? 200 : 500, headers });
|
|
1232
|
-
}
|
|
1233
|
-
if (path === "/api/control-plane/rollback") {
|
|
1234
|
-
if (request.method !== "POST") {
|
|
1235
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
1236
|
-
}
|
|
1237
|
-
const result = await reloadControlPlane("rollback");
|
|
1238
|
-
return Response.json(result, { status: result.ok ? 200 : 500, headers });
|
|
1239
|
-
}
|
|
1240
|
-
if (path === "/api/status") {
|
|
1241
|
-
const issues = await context.issueStore.list();
|
|
1242
|
-
const openIssues = issues.filter((i) => i.status === "open");
|
|
1243
|
-
const readyIssues = await context.issueStore.ready();
|
|
1244
|
-
const controlPlane = {
|
|
1245
|
-
...summarizeControlPlane(controlPlaneCurrent),
|
|
1246
|
-
generation: generationSupervisor.snapshot(),
|
|
1247
|
-
observability: {
|
|
1248
|
-
counters: generationTelemetry.counters(),
|
|
1249
|
-
},
|
|
1250
|
-
};
|
|
1251
|
-
return Response.json({
|
|
1252
|
-
repo_root: context.repoRoot,
|
|
1253
|
-
open_count: openIssues.length,
|
|
1254
|
-
ready_count: readyIssues.length,
|
|
1255
|
-
control_plane: controlPlane,
|
|
1256
|
-
}, { headers });
|
|
1257
|
-
}
|
|
1258
|
-
if (path === "/api/commands/submit") {
|
|
1259
|
-
if (request.method !== "POST") {
|
|
1260
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
1261
|
-
}
|
|
1262
|
-
let body;
|
|
1263
|
-
try {
|
|
1264
|
-
body = (await request.json());
|
|
1265
|
-
}
|
|
1266
|
-
catch {
|
|
1267
|
-
return Response.json({ error: "invalid json body" }, { status: 400, headers });
|
|
1268
|
-
}
|
|
1269
|
-
const kind = typeof body.kind === "string" ? body.kind.trim() : "";
|
|
1270
|
-
if (!kind) {
|
|
1271
|
-
return Response.json({ error: "kind is required" }, { status: 400, headers });
|
|
1272
|
-
}
|
|
1273
|
-
let commandText;
|
|
1274
|
-
switch (kind) {
|
|
1275
|
-
case "run_start": {
|
|
1276
|
-
const prompt = typeof body.prompt === "string" ? body.prompt.trim() : "";
|
|
1277
|
-
if (!prompt) {
|
|
1278
|
-
return Response.json({ error: "prompt is required for run_start" }, { status: 400, headers });
|
|
1279
|
-
}
|
|
1280
|
-
const maxStepsSuffix = typeof body.max_steps === "number" && Number.isFinite(body.max_steps)
|
|
1281
|
-
? ` --max-steps ${Math.max(1, Math.trunc(body.max_steps))}`
|
|
1282
|
-
: "";
|
|
1283
|
-
commandText = `mu! run start ${prompt}${maxStepsSuffix}`;
|
|
1284
|
-
break;
|
|
1285
|
-
}
|
|
1286
|
-
case "run_resume": {
|
|
1287
|
-
const rootId = typeof body.root_issue_id === "string" ? body.root_issue_id.trim() : "";
|
|
1288
|
-
const maxSteps = typeof body.max_steps === "number" && Number.isFinite(body.max_steps)
|
|
1289
|
-
? ` ${Math.max(1, Math.trunc(body.max_steps))}`
|
|
1290
|
-
: "";
|
|
1291
|
-
commandText = `mu! run resume${rootId ? ` ${rootId}` : ""}${maxSteps}`;
|
|
1292
|
-
break;
|
|
1293
|
-
}
|
|
1294
|
-
case "run_interrupt": {
|
|
1295
|
-
const rootId = typeof body.root_issue_id === "string" ? body.root_issue_id.trim() : "";
|
|
1296
|
-
commandText = `mu! run interrupt${rootId ? ` ${rootId}` : ""}`;
|
|
1297
|
-
break;
|
|
1298
|
-
}
|
|
1299
|
-
case "reload":
|
|
1300
|
-
commandText = "/mu reload";
|
|
1301
|
-
break;
|
|
1302
|
-
case "update":
|
|
1303
|
-
commandText = "/mu update";
|
|
1304
|
-
break;
|
|
1305
|
-
case "status":
|
|
1306
|
-
commandText = "/mu status";
|
|
1307
|
-
break;
|
|
1308
|
-
case "issue_list":
|
|
1309
|
-
commandText = "/mu issue list";
|
|
1310
|
-
break;
|
|
1311
|
-
case "issue_get": {
|
|
1312
|
-
const issueId = typeof body.issue_id === "string" ? body.issue_id.trim() : "";
|
|
1313
|
-
commandText = `/mu issue get${issueId ? ` ${issueId}` : ""}`;
|
|
1314
|
-
break;
|
|
1315
|
-
}
|
|
1316
|
-
case "forum_read": {
|
|
1317
|
-
const topic = typeof body.topic === "string" ? body.topic.trim() : "";
|
|
1318
|
-
const limit = typeof body.limit === "number" && Number.isFinite(body.limit)
|
|
1319
|
-
? ` ${Math.max(1, Math.trunc(body.limit))}`
|
|
1320
|
-
: "";
|
|
1321
|
-
commandText = `/mu forum read${topic ? ` ${topic}` : ""}${limit}`;
|
|
1322
|
-
break;
|
|
1323
|
-
}
|
|
1324
|
-
case "run_list":
|
|
1325
|
-
commandText = "/mu run list";
|
|
1326
|
-
break;
|
|
1327
|
-
case "run_status": {
|
|
1328
|
-
const rootId = typeof body.root_issue_id === "string" ? body.root_issue_id.trim() : "";
|
|
1329
|
-
commandText = `/mu run status${rootId ? ` ${rootId}` : ""}`;
|
|
1330
|
-
break;
|
|
1331
|
-
}
|
|
1332
|
-
case "ready":
|
|
1333
|
-
commandText = "/mu ready";
|
|
1334
|
-
break;
|
|
1335
|
-
default:
|
|
1336
|
-
return Response.json({ error: `unknown command kind: ${kind}` }, { status: 400, headers });
|
|
1337
|
-
}
|
|
1338
|
-
try {
|
|
1339
|
-
if (!controlPlaneProxy.submitTerminalCommand) {
|
|
1340
|
-
return Response.json({ error: "control plane not available" }, { status: 503, headers });
|
|
1341
|
-
}
|
|
1342
|
-
const result = await controlPlaneProxy.submitTerminalCommand({
|
|
1343
|
-
commandText,
|
|
1344
|
-
repoRoot: context.repoRoot,
|
|
1345
|
-
});
|
|
1346
|
-
return Response.json({ ok: true, result }, { headers });
|
|
1347
|
-
}
|
|
1348
|
-
catch (err) {
|
|
1349
|
-
return Response.json({ error: `command failed: ${describeError(err)}` }, { status: 500, headers });
|
|
1350
|
-
}
|
|
1351
|
-
}
|
|
1352
|
-
if (path === "/api/runs") {
|
|
1353
|
-
if (request.method !== "GET") {
|
|
1354
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
1355
|
-
}
|
|
1356
|
-
const status = url.searchParams.get("status")?.trim() || undefined;
|
|
1357
|
-
const limitRaw = url.searchParams.get("limit");
|
|
1358
|
-
const limit = limitRaw && /^\d+$/.test(limitRaw) ? Math.max(1, Math.min(500, Number.parseInt(limitRaw, 10))) : undefined;
|
|
1359
|
-
const runs = await controlPlaneProxy.listRuns?.({ status, limit });
|
|
1360
|
-
return Response.json({ count: runs?.length ?? 0, runs: runs ?? [] }, { headers });
|
|
1361
|
-
}
|
|
1362
|
-
if (path === "/api/runs/start") {
|
|
1363
|
-
if (request.method !== "POST") {
|
|
1364
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
1365
|
-
}
|
|
1366
|
-
let body;
|
|
1367
|
-
try {
|
|
1368
|
-
body = (await request.json());
|
|
1369
|
-
}
|
|
1370
|
-
catch {
|
|
1371
|
-
return Response.json({ error: "invalid json body" }, { status: 400, headers });
|
|
1372
|
-
}
|
|
1373
|
-
const prompt = typeof body.prompt === "string" ? body.prompt.trim() : "";
|
|
1374
|
-
if (prompt.length === 0) {
|
|
1375
|
-
return Response.json({ error: "prompt is required" }, { status: 400, headers });
|
|
1376
|
-
}
|
|
1377
|
-
const maxSteps = typeof body.max_steps === "number" && Number.isFinite(body.max_steps)
|
|
1378
|
-
? Math.max(1, Math.trunc(body.max_steps))
|
|
1379
|
-
: undefined;
|
|
1380
|
-
try {
|
|
1381
|
-
const run = await controlPlaneProxy.startRun?.({ prompt, maxSteps });
|
|
1382
|
-
if (!run) {
|
|
1383
|
-
return Response.json({ error: "run supervisor unavailable" }, { status: 503, headers });
|
|
1384
|
-
}
|
|
1385
|
-
await registerAutoRunHeartbeatProgram(run).catch(async (error) => {
|
|
1386
|
-
await context.eventLog.emit("run.auto_heartbeat.lifecycle", {
|
|
1387
|
-
source: "mu-server.runs",
|
|
1388
|
-
payload: {
|
|
1389
|
-
action: "register_failed",
|
|
1390
|
-
run_job_id: run.job_id,
|
|
1391
|
-
error: describeError(error),
|
|
1392
|
-
},
|
|
1393
|
-
});
|
|
1394
|
-
});
|
|
1395
|
-
return Response.json({ ok: true, run }, { status: 201, headers });
|
|
1396
|
-
}
|
|
1397
|
-
catch (err) {
|
|
1398
|
-
return Response.json({ error: describeError(err) }, { status: 500, headers });
|
|
1399
|
-
}
|
|
1400
|
-
}
|
|
1401
|
-
if (path === "/api/runs/resume") {
|
|
1402
|
-
if (request.method !== "POST") {
|
|
1403
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
1404
|
-
}
|
|
1405
|
-
let body;
|
|
1406
|
-
try {
|
|
1407
|
-
body = (await request.json());
|
|
1408
|
-
}
|
|
1409
|
-
catch {
|
|
1410
|
-
return Response.json({ error: "invalid json body" }, { status: 400, headers });
|
|
1411
|
-
}
|
|
1412
|
-
const rootIssueId = typeof body.root_issue_id === "string" ? body.root_issue_id.trim() : "";
|
|
1413
|
-
if (rootIssueId.length === 0) {
|
|
1414
|
-
return Response.json({ error: "root_issue_id is required" }, { status: 400, headers });
|
|
1415
|
-
}
|
|
1416
|
-
const maxSteps = typeof body.max_steps === "number" && Number.isFinite(body.max_steps)
|
|
1417
|
-
? Math.max(1, Math.trunc(body.max_steps))
|
|
1418
|
-
: undefined;
|
|
1419
|
-
try {
|
|
1420
|
-
const run = await controlPlaneProxy.resumeRun?.({ rootIssueId, maxSteps });
|
|
1421
|
-
if (!run) {
|
|
1422
|
-
return Response.json({ error: "run supervisor unavailable" }, { status: 503, headers });
|
|
1423
|
-
}
|
|
1424
|
-
await registerAutoRunHeartbeatProgram(run).catch(async (error) => {
|
|
1425
|
-
await context.eventLog.emit("run.auto_heartbeat.lifecycle", {
|
|
1426
|
-
source: "mu-server.runs",
|
|
1427
|
-
payload: {
|
|
1428
|
-
action: "register_failed",
|
|
1429
|
-
run_job_id: run.job_id,
|
|
1430
|
-
error: describeError(error),
|
|
1431
|
-
},
|
|
1432
|
-
});
|
|
1433
|
-
});
|
|
1434
|
-
return Response.json({ ok: true, run }, { status: 201, headers });
|
|
1435
|
-
}
|
|
1436
|
-
catch (err) {
|
|
1437
|
-
return Response.json({ error: describeError(err) }, { status: 500, headers });
|
|
1438
|
-
}
|
|
1439
|
-
}
|
|
1440
|
-
if (path === "/api/runs/interrupt") {
|
|
1441
|
-
if (request.method !== "POST") {
|
|
1442
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
1443
|
-
}
|
|
1444
|
-
let body;
|
|
1445
|
-
try {
|
|
1446
|
-
body = (await request.json());
|
|
1447
|
-
}
|
|
1448
|
-
catch {
|
|
1449
|
-
return Response.json({ error: "invalid json body" }, { status: 400, headers });
|
|
1450
|
-
}
|
|
1451
|
-
const rootIssueId = typeof body.root_issue_id === "string" ? body.root_issue_id.trim() : null;
|
|
1452
|
-
const jobId = typeof body.job_id === "string" ? body.job_id.trim() : null;
|
|
1453
|
-
const result = await controlPlaneProxy.interruptRun?.({
|
|
1454
|
-
rootIssueId,
|
|
1455
|
-
jobId,
|
|
1456
|
-
});
|
|
1457
|
-
if (!result) {
|
|
1458
|
-
return Response.json({ error: "run supervisor unavailable" }, { status: 503, headers });
|
|
1459
|
-
}
|
|
1460
|
-
if (!result.ok && result.reason === "not_running" && result.run) {
|
|
1461
|
-
await disableAutoRunHeartbeatProgram({
|
|
1462
|
-
jobId: result.run.job_id,
|
|
1463
|
-
status: result.run.status,
|
|
1464
|
-
reason: "interrupt_not_running",
|
|
1465
|
-
}).catch(() => {
|
|
1466
|
-
// best effort cleanup only
|
|
1467
|
-
});
|
|
1468
|
-
}
|
|
1469
|
-
return Response.json(result, { status: result.ok ? 200 : 404, headers });
|
|
1470
|
-
}
|
|
1471
|
-
if (path === "/api/runs/heartbeat") {
|
|
1472
|
-
if (request.method !== "POST") {
|
|
1473
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
1474
|
-
}
|
|
1475
|
-
let body;
|
|
1476
|
-
try {
|
|
1477
|
-
body = (await request.json());
|
|
1478
|
-
}
|
|
1479
|
-
catch {
|
|
1480
|
-
return Response.json({ error: "invalid json body" }, { status: 400, headers });
|
|
1481
|
-
}
|
|
1482
|
-
const rootIssueId = typeof body.root_issue_id === "string" ? body.root_issue_id.trim() : null;
|
|
1483
|
-
const jobId = typeof body.job_id === "string" ? body.job_id.trim() : null;
|
|
1484
|
-
const reason = typeof body.reason === "string" ? body.reason.trim() : null;
|
|
1485
|
-
const wakeMode = normalizeWakeMode(body.wake_mode);
|
|
1486
|
-
const result = await controlPlaneProxy.heartbeatRun?.({
|
|
1487
|
-
rootIssueId,
|
|
1488
|
-
jobId,
|
|
1489
|
-
reason,
|
|
1490
|
-
wakeMode,
|
|
1491
|
-
});
|
|
1492
|
-
if (!result) {
|
|
1493
|
-
return Response.json({ error: "run supervisor unavailable" }, { status: 503, headers });
|
|
1494
|
-
}
|
|
1495
|
-
if (!result.ok && result.reason === "not_running" && result.run) {
|
|
1496
|
-
await disableAutoRunHeartbeatProgram({
|
|
1497
|
-
jobId: result.run.job_id,
|
|
1498
|
-
status: result.run.status,
|
|
1499
|
-
reason: "run_not_running",
|
|
1500
|
-
}).catch(() => {
|
|
1501
|
-
// best effort cleanup only
|
|
1502
|
-
});
|
|
1503
|
-
}
|
|
1504
|
-
if (result.ok) {
|
|
1505
|
-
return Response.json(result, { status: 200, headers });
|
|
1506
|
-
}
|
|
1507
|
-
if (result.reason === "missing_target") {
|
|
1508
|
-
return Response.json(result, { status: 400, headers });
|
|
1509
|
-
}
|
|
1510
|
-
if (result.reason === "not_running") {
|
|
1511
|
-
return Response.json(result, { status: 409, headers });
|
|
1512
|
-
}
|
|
1513
|
-
return Response.json(result, { status: 404, headers });
|
|
1514
|
-
}
|
|
1515
|
-
if (path.startsWith("/api/runs/")) {
|
|
1516
|
-
const rest = path.slice("/api/runs/".length);
|
|
1517
|
-
const [rawId, maybeSub] = rest.split("/");
|
|
1518
|
-
const idOrRoot = decodeURIComponent(rawId ?? "").trim();
|
|
1519
|
-
if (idOrRoot.length === 0) {
|
|
1520
|
-
return Response.json({ error: "missing run id" }, { status: 400, headers });
|
|
1521
|
-
}
|
|
1522
|
-
if (maybeSub === "trace") {
|
|
1523
|
-
if (request.method !== "GET") {
|
|
1524
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
1525
|
-
}
|
|
1526
|
-
const limitRaw = url.searchParams.get("limit");
|
|
1527
|
-
const limit = limitRaw && /^\d+$/.test(limitRaw)
|
|
1528
|
-
? Math.max(1, Math.min(2_000, Number.parseInt(limitRaw, 10)))
|
|
1529
|
-
: undefined;
|
|
1530
|
-
const trace = await controlPlaneProxy.traceRun?.({ idOrRoot, limit });
|
|
1531
|
-
if (!trace) {
|
|
1532
|
-
return Response.json({ error: "run trace not found" }, { status: 404, headers });
|
|
1533
|
-
}
|
|
1534
|
-
return Response.json(trace, { headers });
|
|
1535
|
-
}
|
|
1536
|
-
if (request.method !== "GET") {
|
|
1537
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
1538
|
-
}
|
|
1539
|
-
const run = await controlPlaneProxy.getRun?.(idOrRoot);
|
|
1540
|
-
if (!run) {
|
|
1541
|
-
return Response.json({ error: "run not found" }, { status: 404, headers });
|
|
1542
|
-
}
|
|
1543
|
-
if (run.status !== "running") {
|
|
1544
|
-
await disableAutoRunHeartbeatProgram({
|
|
1545
|
-
jobId: run.job_id,
|
|
1546
|
-
status: run.status,
|
|
1547
|
-
reason: "run_terminal_snapshot",
|
|
1548
|
-
}).catch(() => {
|
|
1549
|
-
// best effort cleanup only
|
|
1550
|
-
});
|
|
1551
|
-
}
|
|
1552
|
-
return Response.json(run, { headers });
|
|
1553
|
-
}
|
|
1554
|
-
if (path === "/api/cron/status") {
|
|
1555
|
-
if (request.method !== "GET") {
|
|
1556
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
1557
|
-
}
|
|
1558
|
-
const status = await cronPrograms.status();
|
|
1559
|
-
return Response.json(status, { headers });
|
|
1560
|
-
}
|
|
1561
|
-
if (path === "/api/cron") {
|
|
1562
|
-
if (request.method !== "GET") {
|
|
1563
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
1564
|
-
}
|
|
1565
|
-
const enabledRaw = url.searchParams.get("enabled")?.trim().toLowerCase();
|
|
1566
|
-
const enabled = enabledRaw === "true" ? true : enabledRaw === "false" ? false : undefined;
|
|
1567
|
-
const targetKindRaw = url.searchParams.get("target_kind")?.trim().toLowerCase();
|
|
1568
|
-
const targetKind = targetKindRaw === "run" || targetKindRaw === "activity" ? targetKindRaw : undefined;
|
|
1569
|
-
const scheduleKindRaw = url.searchParams.get("schedule_kind")?.trim().toLowerCase();
|
|
1570
|
-
const scheduleKind = scheduleKindRaw === "at" || scheduleKindRaw === "every" || scheduleKindRaw === "cron"
|
|
1571
|
-
? scheduleKindRaw
|
|
1572
|
-
: undefined;
|
|
1573
|
-
const limitRaw = url.searchParams.get("limit");
|
|
1574
|
-
const limit = limitRaw && /^\d+$/.test(limitRaw) ? Math.max(1, Math.min(500, Number.parseInt(limitRaw, 10))) : undefined;
|
|
1575
|
-
const programs = await cronPrograms.list({ enabled, targetKind, scheduleKind, limit });
|
|
1576
|
-
return Response.json({ count: programs.length, programs }, { headers });
|
|
1577
|
-
}
|
|
1578
|
-
if (path === "/api/cron/create") {
|
|
1579
|
-
if (request.method !== "POST") {
|
|
1580
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
1581
|
-
}
|
|
1582
|
-
let body;
|
|
1583
|
-
try {
|
|
1584
|
-
body = (await request.json());
|
|
1585
|
-
}
|
|
1586
|
-
catch {
|
|
1587
|
-
return Response.json({ error: "invalid json body" }, { status: 400, headers });
|
|
1588
|
-
}
|
|
1589
|
-
const title = typeof body.title === "string" ? body.title.trim() : "";
|
|
1590
|
-
if (!title) {
|
|
1591
|
-
return Response.json({ error: "title is required" }, { status: 400, headers });
|
|
1592
|
-
}
|
|
1593
|
-
const parsedTarget = parseCronTarget(body);
|
|
1594
|
-
if (!parsedTarget.target) {
|
|
1595
|
-
return Response.json({ error: parsedTarget.error ?? "invalid target" }, { status: 400, headers });
|
|
1596
|
-
}
|
|
1597
|
-
if (!hasCronScheduleInput(body)) {
|
|
1598
|
-
return Response.json({ error: "schedule is required" }, { status: 400, headers });
|
|
1599
|
-
}
|
|
1600
|
-
const schedule = cronScheduleInputFromBody(body);
|
|
1601
|
-
const reason = typeof body.reason === "string" ? body.reason.trim() : undefined;
|
|
1602
|
-
const wakeMode = normalizeWakeMode(body.wake_mode);
|
|
1603
|
-
const enabled = typeof body.enabled === "boolean" ? body.enabled : undefined;
|
|
1604
|
-
try {
|
|
1605
|
-
const program = await cronPrograms.create({
|
|
1606
|
-
title,
|
|
1607
|
-
target: parsedTarget.target,
|
|
1608
|
-
schedule,
|
|
1609
|
-
reason,
|
|
1610
|
-
wakeMode,
|
|
1611
|
-
enabled,
|
|
1612
|
-
metadata: body.metadata && typeof body.metadata === "object" && !Array.isArray(body.metadata)
|
|
1613
|
-
? body.metadata
|
|
1614
|
-
: undefined,
|
|
1615
|
-
});
|
|
1616
|
-
return Response.json({ ok: true, program }, { status: 201, headers });
|
|
1617
|
-
}
|
|
1618
|
-
catch (err) {
|
|
1619
|
-
return Response.json({ error: describeError(err) }, { status: 400, headers });
|
|
1620
|
-
}
|
|
1621
|
-
}
|
|
1622
|
-
if (path === "/api/cron/update") {
|
|
1623
|
-
if (request.method !== "POST") {
|
|
1624
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
1625
|
-
}
|
|
1626
|
-
let body;
|
|
1627
|
-
try {
|
|
1628
|
-
body = (await request.json());
|
|
1629
|
-
}
|
|
1630
|
-
catch {
|
|
1631
|
-
return Response.json({ error: "invalid json body" }, { status: 400, headers });
|
|
1632
|
-
}
|
|
1633
|
-
const programId = typeof body.program_id === "string" ? body.program_id.trim() : "";
|
|
1634
|
-
if (!programId) {
|
|
1635
|
-
return Response.json({ error: "program_id is required" }, { status: 400, headers });
|
|
1636
|
-
}
|
|
1637
|
-
let target;
|
|
1638
|
-
if (typeof body.target_kind === "string") {
|
|
1639
|
-
const parsedTarget = parseCronTarget(body);
|
|
1640
|
-
if (!parsedTarget.target) {
|
|
1641
|
-
return Response.json({ error: parsedTarget.error ?? "invalid target" }, { status: 400, headers });
|
|
1642
|
-
}
|
|
1643
|
-
target = parsedTarget.target;
|
|
1644
|
-
}
|
|
1645
|
-
const schedule = hasCronScheduleInput(body) ? cronScheduleInputFromBody(body) : undefined;
|
|
1646
|
-
const wakeMode = Object.hasOwn(body, "wake_mode") ? normalizeWakeMode(body.wake_mode) : undefined;
|
|
1647
|
-
try {
|
|
1648
|
-
const result = await cronPrograms.update({
|
|
1649
|
-
programId,
|
|
1650
|
-
title: typeof body.title === "string" ? body.title : undefined,
|
|
1651
|
-
reason: typeof body.reason === "string" ? body.reason : undefined,
|
|
1652
|
-
wakeMode,
|
|
1653
|
-
enabled: typeof body.enabled === "boolean" ? body.enabled : undefined,
|
|
1654
|
-
target,
|
|
1655
|
-
schedule,
|
|
1656
|
-
metadata: body.metadata && typeof body.metadata === "object" && !Array.isArray(body.metadata)
|
|
1657
|
-
? body.metadata
|
|
1658
|
-
: undefined,
|
|
1659
|
-
});
|
|
1660
|
-
if (result.ok) {
|
|
1661
|
-
return Response.json(result, { headers });
|
|
1662
|
-
}
|
|
1663
|
-
if (result.reason === "not_found") {
|
|
1664
|
-
return Response.json(result, { status: 404, headers });
|
|
1665
|
-
}
|
|
1666
|
-
return Response.json(result, { status: 400, headers });
|
|
1667
|
-
}
|
|
1668
|
-
catch (err) {
|
|
1669
|
-
return Response.json({ error: describeError(err) }, { status: 400, headers });
|
|
1670
|
-
}
|
|
1671
|
-
}
|
|
1672
|
-
if (path === "/api/cron/delete") {
|
|
1673
|
-
if (request.method !== "POST") {
|
|
1674
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
1675
|
-
}
|
|
1676
|
-
let body;
|
|
1677
|
-
try {
|
|
1678
|
-
body = (await request.json());
|
|
1679
|
-
}
|
|
1680
|
-
catch {
|
|
1681
|
-
return Response.json({ error: "invalid json body" }, { status: 400, headers });
|
|
1682
|
-
}
|
|
1683
|
-
const programId = typeof body.program_id === "string" ? body.program_id.trim() : "";
|
|
1684
|
-
if (!programId) {
|
|
1685
|
-
return Response.json({ error: "program_id is required" }, { status: 400, headers });
|
|
1686
|
-
}
|
|
1687
|
-
const result = await cronPrograms.remove(programId);
|
|
1688
|
-
return Response.json(result, { status: result.ok ? 200 : result.reason === "not_found" ? 404 : 400, headers });
|
|
1689
|
-
}
|
|
1690
|
-
if (path === "/api/cron/trigger") {
|
|
1691
|
-
if (request.method !== "POST") {
|
|
1692
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
1693
|
-
}
|
|
1694
|
-
let body;
|
|
1695
|
-
try {
|
|
1696
|
-
body = (await request.json());
|
|
1697
|
-
}
|
|
1698
|
-
catch {
|
|
1699
|
-
return Response.json({ error: "invalid json body" }, { status: 400, headers });
|
|
1700
|
-
}
|
|
1701
|
-
const result = await cronPrograms.trigger({
|
|
1702
|
-
programId: typeof body.program_id === "string" ? body.program_id : null,
|
|
1703
|
-
reason: typeof body.reason === "string" ? body.reason : null,
|
|
1704
|
-
});
|
|
1705
|
-
if (result.ok) {
|
|
1706
|
-
return Response.json(result, { headers });
|
|
1707
|
-
}
|
|
1708
|
-
if (result.reason === "missing_target") {
|
|
1709
|
-
return Response.json(result, { status: 400, headers });
|
|
1710
|
-
}
|
|
1711
|
-
if (result.reason === "not_found") {
|
|
1712
|
-
return Response.json(result, { status: 404, headers });
|
|
1713
|
-
}
|
|
1714
|
-
return Response.json(result, { status: 409, headers });
|
|
1715
|
-
}
|
|
1716
|
-
if (path.startsWith("/api/cron/")) {
|
|
1717
|
-
if (request.method !== "GET") {
|
|
1718
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
1719
|
-
}
|
|
1720
|
-
const id = decodeURIComponent(path.slice("/api/cron/".length)).trim();
|
|
1721
|
-
if (!id) {
|
|
1722
|
-
return Response.json({ error: "missing program id" }, { status: 400, headers });
|
|
1723
|
-
}
|
|
1724
|
-
const program = await cronPrograms.get(id);
|
|
1725
|
-
if (!program) {
|
|
1726
|
-
return Response.json({ error: "program not found" }, { status: 404, headers });
|
|
1727
|
-
}
|
|
1728
|
-
return Response.json(program, { headers });
|
|
1729
|
-
}
|
|
1730
|
-
if (path === "/api/heartbeats") {
|
|
1731
|
-
if (request.method !== "GET") {
|
|
1732
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
1733
|
-
}
|
|
1734
|
-
const enabledRaw = url.searchParams.get("enabled")?.trim().toLowerCase();
|
|
1735
|
-
const enabled = enabledRaw === "true" ? true : enabledRaw === "false" ? false : undefined;
|
|
1736
|
-
const targetKindRaw = url.searchParams.get("target_kind")?.trim().toLowerCase();
|
|
1737
|
-
const targetKind = targetKindRaw === "run" || targetKindRaw === "activity" ? targetKindRaw : undefined;
|
|
1738
|
-
const limitRaw = url.searchParams.get("limit");
|
|
1739
|
-
const limit = limitRaw && /^\d+$/.test(limitRaw) ? Math.max(1, Math.min(500, Number.parseInt(limitRaw, 10))) : undefined;
|
|
1740
|
-
const programs = await heartbeatPrograms.list({ enabled, targetKind, limit });
|
|
1741
|
-
return Response.json({ count: programs.length, programs }, { headers });
|
|
1742
|
-
}
|
|
1743
|
-
if (path === "/api/heartbeats/create") {
|
|
1744
|
-
if (request.method !== "POST") {
|
|
1745
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
1746
|
-
}
|
|
1747
|
-
let body;
|
|
1748
|
-
try {
|
|
1749
|
-
body = (await request.json());
|
|
1750
|
-
}
|
|
1751
|
-
catch {
|
|
1752
|
-
return Response.json({ error: "invalid json body" }, { status: 400, headers });
|
|
1753
|
-
}
|
|
1754
|
-
const title = typeof body.title === "string" ? body.title.trim() : "";
|
|
1755
|
-
if (!title) {
|
|
1756
|
-
return Response.json({ error: "title is required" }, { status: 400, headers });
|
|
1757
|
-
}
|
|
1758
|
-
const targetKind = typeof body.target_kind === "string" ? body.target_kind.trim().toLowerCase() : "";
|
|
1759
|
-
let target = null;
|
|
1760
|
-
if (targetKind === "run") {
|
|
1761
|
-
const jobId = typeof body.run_job_id === "string" ? body.run_job_id.trim() : "";
|
|
1762
|
-
const rootIssueId = typeof body.run_root_issue_id === "string" ? body.run_root_issue_id.trim() : "";
|
|
1763
|
-
if (!jobId && !rootIssueId) {
|
|
1764
|
-
return Response.json({ error: "run target requires run_job_id or run_root_issue_id" }, { status: 400, headers });
|
|
1765
|
-
}
|
|
1766
|
-
target = {
|
|
1767
|
-
kind: "run",
|
|
1768
|
-
job_id: jobId || null,
|
|
1769
|
-
root_issue_id: rootIssueId || null,
|
|
1770
|
-
};
|
|
1771
|
-
}
|
|
1772
|
-
else if (targetKind === "activity") {
|
|
1773
|
-
const activityId = typeof body.activity_id === "string" ? body.activity_id.trim() : "";
|
|
1774
|
-
if (!activityId) {
|
|
1775
|
-
return Response.json({ error: "activity target requires activity_id" }, { status: 400, headers });
|
|
1776
|
-
}
|
|
1777
|
-
target = {
|
|
1778
|
-
kind: "activity",
|
|
1779
|
-
activity_id: activityId,
|
|
1780
|
-
};
|
|
1781
|
-
}
|
|
1782
|
-
else {
|
|
1783
|
-
return Response.json({ error: "target_kind must be run or activity" }, { status: 400, headers });
|
|
1784
|
-
}
|
|
1785
|
-
const everyMs = typeof body.every_ms === "number" && Number.isFinite(body.every_ms)
|
|
1786
|
-
? Math.max(0, Math.trunc(body.every_ms))
|
|
1787
|
-
: undefined;
|
|
1788
|
-
const reason = typeof body.reason === "string" ? body.reason.trim() : undefined;
|
|
1789
|
-
const wakeMode = normalizeWakeMode(body.wake_mode);
|
|
1790
|
-
const enabled = typeof body.enabled === "boolean" ? body.enabled : undefined;
|
|
1791
|
-
try {
|
|
1792
|
-
const program = await heartbeatPrograms.create({
|
|
1793
|
-
title,
|
|
1794
|
-
target,
|
|
1795
|
-
everyMs,
|
|
1796
|
-
reason,
|
|
1797
|
-
wakeMode,
|
|
1798
|
-
enabled,
|
|
1799
|
-
metadata: body.metadata && typeof body.metadata === "object" && !Array.isArray(body.metadata)
|
|
1800
|
-
? body.metadata
|
|
1801
|
-
: undefined,
|
|
1802
|
-
});
|
|
1803
|
-
return Response.json({ ok: true, program }, { status: 201, headers });
|
|
1804
|
-
}
|
|
1805
|
-
catch (err) {
|
|
1806
|
-
return Response.json({ error: describeError(err) }, { status: 400, headers });
|
|
1807
|
-
}
|
|
1808
|
-
}
|
|
1809
|
-
if (path === "/api/heartbeats/update") {
|
|
1810
|
-
if (request.method !== "POST") {
|
|
1811
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
1812
|
-
}
|
|
1813
|
-
let body;
|
|
1814
|
-
try {
|
|
1815
|
-
body = (await request.json());
|
|
1816
|
-
}
|
|
1817
|
-
catch {
|
|
1818
|
-
return Response.json({ error: "invalid json body" }, { status: 400, headers });
|
|
1819
|
-
}
|
|
1820
|
-
const programId = typeof body.program_id === "string" ? body.program_id.trim() : "";
|
|
1821
|
-
if (!programId) {
|
|
1822
|
-
return Response.json({ error: "program_id is required" }, { status: 400, headers });
|
|
1823
|
-
}
|
|
1824
|
-
let target;
|
|
1825
|
-
if (typeof body.target_kind === "string") {
|
|
1826
|
-
const targetKind = body.target_kind.trim().toLowerCase();
|
|
1827
|
-
if (targetKind === "run") {
|
|
1828
|
-
const jobId = typeof body.run_job_id === "string" ? body.run_job_id.trim() : "";
|
|
1829
|
-
const rootIssueId = typeof body.run_root_issue_id === "string" ? body.run_root_issue_id.trim() : "";
|
|
1830
|
-
if (!jobId && !rootIssueId) {
|
|
1831
|
-
return Response.json({ error: "run target requires run_job_id or run_root_issue_id" }, { status: 400, headers });
|
|
1832
|
-
}
|
|
1833
|
-
target = {
|
|
1834
|
-
kind: "run",
|
|
1835
|
-
job_id: jobId || null,
|
|
1836
|
-
root_issue_id: rootIssueId || null,
|
|
1837
|
-
};
|
|
1838
|
-
}
|
|
1839
|
-
else if (targetKind === "activity") {
|
|
1840
|
-
const activityId = typeof body.activity_id === "string" ? body.activity_id.trim() : "";
|
|
1841
|
-
if (!activityId) {
|
|
1842
|
-
return Response.json({ error: "activity target requires activity_id" }, { status: 400, headers });
|
|
1843
|
-
}
|
|
1844
|
-
target = {
|
|
1845
|
-
kind: "activity",
|
|
1846
|
-
activity_id: activityId,
|
|
1847
|
-
};
|
|
1848
|
-
}
|
|
1849
|
-
else {
|
|
1850
|
-
return Response.json({ error: "target_kind must be run or activity" }, { status: 400, headers });
|
|
1851
|
-
}
|
|
1852
|
-
}
|
|
1853
|
-
const wakeMode = Object.hasOwn(body, "wake_mode") ? normalizeWakeMode(body.wake_mode) : undefined;
|
|
1854
|
-
try {
|
|
1855
|
-
const result = await heartbeatPrograms.update({
|
|
1856
|
-
programId,
|
|
1857
|
-
title: typeof body.title === "string" ? body.title : undefined,
|
|
1858
|
-
target,
|
|
1859
|
-
everyMs: typeof body.every_ms === "number" && Number.isFinite(body.every_ms)
|
|
1860
|
-
? Math.max(0, Math.trunc(body.every_ms))
|
|
1861
|
-
: undefined,
|
|
1862
|
-
reason: typeof body.reason === "string" ? body.reason : undefined,
|
|
1863
|
-
wakeMode,
|
|
1864
|
-
enabled: typeof body.enabled === "boolean" ? body.enabled : undefined,
|
|
1865
|
-
metadata: body.metadata && typeof body.metadata === "object" && !Array.isArray(body.metadata)
|
|
1866
|
-
? body.metadata
|
|
1867
|
-
: undefined,
|
|
1868
|
-
});
|
|
1869
|
-
if (result.ok) {
|
|
1870
|
-
return Response.json(result, { headers });
|
|
1871
|
-
}
|
|
1872
|
-
return Response.json(result, { status: result.reason === "not_found" ? 404 : 400, headers });
|
|
1873
|
-
}
|
|
1874
|
-
catch (err) {
|
|
1875
|
-
return Response.json({ error: describeError(err) }, { status: 400, headers });
|
|
1876
|
-
}
|
|
1877
|
-
}
|
|
1878
|
-
if (path === "/api/heartbeats/delete") {
|
|
1879
|
-
if (request.method !== "POST") {
|
|
1880
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
1881
|
-
}
|
|
1882
|
-
let body;
|
|
1883
|
-
try {
|
|
1884
|
-
body = (await request.json());
|
|
1885
|
-
}
|
|
1886
|
-
catch {
|
|
1887
|
-
return Response.json({ error: "invalid json body" }, { status: 400, headers });
|
|
1888
|
-
}
|
|
1889
|
-
const programId = typeof body.program_id === "string" ? body.program_id.trim() : "";
|
|
1890
|
-
if (!programId) {
|
|
1891
|
-
return Response.json({ error: "program_id is required" }, { status: 400, headers });
|
|
1892
|
-
}
|
|
1893
|
-
const result = await heartbeatPrograms.remove(programId);
|
|
1894
|
-
return Response.json(result, { status: result.ok ? 200 : result.reason === "not_found" ? 404 : 400, headers });
|
|
1895
|
-
}
|
|
1896
|
-
if (path === "/api/heartbeats/trigger") {
|
|
1897
|
-
if (request.method !== "POST") {
|
|
1898
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
1899
|
-
}
|
|
1900
|
-
let body;
|
|
1901
|
-
try {
|
|
1902
|
-
body = (await request.json());
|
|
1903
|
-
}
|
|
1904
|
-
catch {
|
|
1905
|
-
return Response.json({ error: "invalid json body" }, { status: 400, headers });
|
|
1906
|
-
}
|
|
1907
|
-
const result = await heartbeatPrograms.trigger({
|
|
1908
|
-
programId: typeof body.program_id === "string" ? body.program_id : null,
|
|
1909
|
-
reason: typeof body.reason === "string" ? body.reason : null,
|
|
1910
|
-
});
|
|
1911
|
-
if (result.ok) {
|
|
1912
|
-
return Response.json(result, { headers });
|
|
1913
|
-
}
|
|
1914
|
-
if (result.reason === "missing_target") {
|
|
1915
|
-
return Response.json(result, { status: 400, headers });
|
|
1916
|
-
}
|
|
1917
|
-
if (result.reason === "not_found") {
|
|
1918
|
-
return Response.json(result, { status: 404, headers });
|
|
1919
|
-
}
|
|
1920
|
-
return Response.json(result, { status: 409, headers });
|
|
1921
|
-
}
|
|
1922
|
-
if (path.startsWith("/api/heartbeats/")) {
|
|
1923
|
-
if (request.method !== "GET") {
|
|
1924
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
1925
|
-
}
|
|
1926
|
-
const id = decodeURIComponent(path.slice("/api/heartbeats/".length)).trim();
|
|
1927
|
-
if (!id) {
|
|
1928
|
-
return Response.json({ error: "missing program id" }, { status: 400, headers });
|
|
1929
|
-
}
|
|
1930
|
-
const program = await heartbeatPrograms.get(id);
|
|
1931
|
-
if (!program) {
|
|
1932
|
-
return Response.json({ error: "program not found" }, { status: 404, headers });
|
|
1933
|
-
}
|
|
1934
|
-
return Response.json(program, { headers });
|
|
1935
|
-
}
|
|
1936
|
-
if (path === "/api/activities") {
|
|
1937
|
-
if (request.method !== "GET") {
|
|
1938
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
1939
|
-
}
|
|
1940
|
-
const statusRaw = url.searchParams.get("status")?.trim().toLowerCase();
|
|
1941
|
-
const status = statusRaw === "running" || statusRaw === "completed" || statusRaw === "failed" || statusRaw === "cancelled"
|
|
1942
|
-
? statusRaw
|
|
1943
|
-
: undefined;
|
|
1944
|
-
const kind = url.searchParams.get("kind")?.trim() || undefined;
|
|
1945
|
-
const limitRaw = url.searchParams.get("limit");
|
|
1946
|
-
const limit = limitRaw && /^\d+$/.test(limitRaw) ? Math.max(1, Math.min(500, Number.parseInt(limitRaw, 10))) : undefined;
|
|
1947
|
-
const activities = activitySupervisor.list({ status, kind, limit });
|
|
1948
|
-
return Response.json({ count: activities.length, activities }, { headers });
|
|
1949
|
-
}
|
|
1950
|
-
if (path === "/api/activities/start") {
|
|
1951
|
-
if (request.method !== "POST") {
|
|
1952
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
1953
|
-
}
|
|
1954
|
-
let body;
|
|
1955
|
-
try {
|
|
1956
|
-
body = (await request.json());
|
|
1957
|
-
}
|
|
1958
|
-
catch {
|
|
1959
|
-
return Response.json({ error: "invalid json body" }, { status: 400, headers });
|
|
1960
|
-
}
|
|
1961
|
-
const title = typeof body.title === "string" ? body.title.trim() : "";
|
|
1962
|
-
if (!title) {
|
|
1963
|
-
return Response.json({ error: "title is required" }, { status: 400, headers });
|
|
1964
|
-
}
|
|
1965
|
-
const kind = typeof body.kind === "string" ? body.kind.trim() : undefined;
|
|
1966
|
-
const heartbeatEveryMs = typeof body.heartbeat_every_ms === "number" && Number.isFinite(body.heartbeat_every_ms)
|
|
1967
|
-
? Math.max(0, Math.trunc(body.heartbeat_every_ms))
|
|
1968
|
-
: undefined;
|
|
1969
|
-
const source = body.source === "api" || body.source === "command" || body.source === "system" ? body.source : "api";
|
|
1970
|
-
try {
|
|
1971
|
-
const activity = activitySupervisor.start({
|
|
1972
|
-
title,
|
|
1973
|
-
kind,
|
|
1974
|
-
heartbeatEveryMs,
|
|
1975
|
-
metadata: body.metadata ?? undefined,
|
|
1976
|
-
source,
|
|
1977
|
-
});
|
|
1978
|
-
return Response.json({ ok: true, activity }, { status: 201, headers });
|
|
1979
|
-
}
|
|
1980
|
-
catch (err) {
|
|
1981
|
-
return Response.json({ error: describeError(err) }, { status: 400, headers });
|
|
1982
|
-
}
|
|
1983
|
-
}
|
|
1984
|
-
if (path === "/api/activities/progress") {
|
|
1985
|
-
if (request.method !== "POST") {
|
|
1986
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
1987
|
-
}
|
|
1988
|
-
let body;
|
|
1989
|
-
try {
|
|
1990
|
-
body = (await request.json());
|
|
1991
|
-
}
|
|
1992
|
-
catch {
|
|
1993
|
-
return Response.json({ error: "invalid json body" }, { status: 400, headers });
|
|
1994
|
-
}
|
|
1995
|
-
const result = activitySupervisor.progress({
|
|
1996
|
-
activityId: typeof body.activity_id === "string" ? body.activity_id : null,
|
|
1997
|
-
message: typeof body.message === "string" ? body.message : null,
|
|
1998
|
-
});
|
|
1999
|
-
if (result.ok) {
|
|
2000
|
-
return Response.json(result, { headers });
|
|
2001
|
-
}
|
|
2002
|
-
if (result.reason === "missing_target") {
|
|
2003
|
-
return Response.json(result, { status: 400, headers });
|
|
2004
|
-
}
|
|
2005
|
-
if (result.reason === "not_running") {
|
|
2006
|
-
return Response.json(result, { status: 409, headers });
|
|
2007
|
-
}
|
|
2008
|
-
return Response.json(result, { status: 404, headers });
|
|
2009
|
-
}
|
|
2010
|
-
if (path === "/api/activities/heartbeat") {
|
|
2011
|
-
if (request.method !== "POST") {
|
|
2012
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
2013
|
-
}
|
|
2014
|
-
let body;
|
|
2015
|
-
try {
|
|
2016
|
-
body = (await request.json());
|
|
2017
|
-
}
|
|
2018
|
-
catch {
|
|
2019
|
-
return Response.json({ error: "invalid json body" }, { status: 400, headers });
|
|
2020
|
-
}
|
|
2021
|
-
const result = activitySupervisor.heartbeat({
|
|
2022
|
-
activityId: typeof body.activity_id === "string" ? body.activity_id : null,
|
|
2023
|
-
reason: typeof body.reason === "string" ? body.reason : null,
|
|
2024
|
-
});
|
|
2025
|
-
if (result.ok) {
|
|
2026
|
-
return Response.json(result, { headers });
|
|
2027
|
-
}
|
|
2028
|
-
if (result.reason === "missing_target") {
|
|
2029
|
-
return Response.json(result, { status: 400, headers });
|
|
2030
|
-
}
|
|
2031
|
-
if (result.reason === "not_running") {
|
|
2032
|
-
return Response.json(result, { status: 409, headers });
|
|
2033
|
-
}
|
|
2034
|
-
return Response.json(result, { status: 404, headers });
|
|
2035
|
-
}
|
|
2036
|
-
if (path === "/api/activities/complete" || path === "/api/activities/fail" || path === "/api/activities/cancel") {
|
|
2037
|
-
if (request.method !== "POST") {
|
|
2038
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
2039
|
-
}
|
|
2040
|
-
let body;
|
|
2041
|
-
try {
|
|
2042
|
-
body = (await request.json());
|
|
2043
|
-
}
|
|
2044
|
-
catch {
|
|
2045
|
-
return Response.json({ error: "invalid json body" }, { status: 400, headers });
|
|
2046
|
-
}
|
|
2047
|
-
const activityId = typeof body.activity_id === "string" ? body.activity_id : null;
|
|
2048
|
-
const message = typeof body.message === "string" ? body.message : null;
|
|
2049
|
-
const result = path === "/api/activities/complete"
|
|
2050
|
-
? activitySupervisor.complete({ activityId, message })
|
|
2051
|
-
: path === "/api/activities/fail"
|
|
2052
|
-
? activitySupervisor.fail({ activityId, message })
|
|
2053
|
-
: activitySupervisor.cancel({ activityId, message });
|
|
2054
|
-
if (result.ok) {
|
|
2055
|
-
return Response.json(result, { headers });
|
|
2056
|
-
}
|
|
2057
|
-
if (result.reason === "missing_target") {
|
|
2058
|
-
return Response.json(result, { status: 400, headers });
|
|
2059
|
-
}
|
|
2060
|
-
if (result.reason === "not_running") {
|
|
2061
|
-
return Response.json(result, { status: 409, headers });
|
|
2062
|
-
}
|
|
2063
|
-
return Response.json(result, { status: 404, headers });
|
|
2064
|
-
}
|
|
2065
|
-
if (path.startsWith("/api/activities/")) {
|
|
2066
|
-
if (request.method !== "GET") {
|
|
2067
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
2068
|
-
}
|
|
2069
|
-
const rest = path.slice("/api/activities/".length);
|
|
2070
|
-
const [rawId, maybeSub] = rest.split("/");
|
|
2071
|
-
const activityId = decodeURIComponent(rawId ?? "").trim();
|
|
2072
|
-
if (activityId.length === 0) {
|
|
2073
|
-
return Response.json({ error: "missing activity id" }, { status: 400, headers });
|
|
2074
|
-
}
|
|
2075
|
-
if (maybeSub === "events") {
|
|
2076
|
-
const limitRaw = url.searchParams.get("limit");
|
|
2077
|
-
const limit = limitRaw && /^\d+$/.test(limitRaw)
|
|
2078
|
-
? Math.max(1, Math.min(2_000, Number.parseInt(limitRaw, 10)))
|
|
2079
|
-
: undefined;
|
|
2080
|
-
const events = activitySupervisor.events(activityId, { limit });
|
|
2081
|
-
if (!events) {
|
|
2082
|
-
return Response.json({ error: "activity not found" }, { status: 404, headers });
|
|
2083
|
-
}
|
|
2084
|
-
return Response.json({ count: events.length, events }, { headers });
|
|
2085
|
-
}
|
|
2086
|
-
const activity = activitySupervisor.get(activityId);
|
|
2087
|
-
if (!activity) {
|
|
2088
|
-
return Response.json({ error: "activity not found" }, { status: 404, headers });
|
|
2089
|
-
}
|
|
2090
|
-
return Response.json(activity, { headers });
|
|
2091
|
-
}
|
|
2092
|
-
if (path === "/api/identities" || path === "/api/identities/link" || path === "/api/identities/unlink") {
|
|
2093
|
-
const cpPaths = getControlPlanePaths(context.repoRoot);
|
|
2094
|
-
const identityStore = new IdentityStore(cpPaths.identitiesPath);
|
|
2095
|
-
await identityStore.load();
|
|
2096
|
-
if (path === "/api/identities") {
|
|
2097
|
-
if (request.method !== "GET") {
|
|
2098
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
2099
|
-
}
|
|
2100
|
-
const includeInactive = url.searchParams.get("include_inactive")?.trim().toLowerCase() === "true";
|
|
2101
|
-
const bindings = identityStore.listBindings({ includeInactive });
|
|
2102
|
-
return Response.json({ count: bindings.length, bindings }, { headers });
|
|
2103
|
-
}
|
|
2104
|
-
if (path === "/api/identities/link") {
|
|
2105
|
-
if (request.method !== "POST") {
|
|
2106
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
2107
|
-
}
|
|
2108
|
-
let body;
|
|
2109
|
-
try {
|
|
2110
|
-
body = (await request.json());
|
|
2111
|
-
}
|
|
2112
|
-
catch {
|
|
2113
|
-
return Response.json({ error: "invalid json body" }, { status: 400, headers });
|
|
2114
|
-
}
|
|
2115
|
-
const channel = typeof body.channel === "string" ? body.channel.trim() : "";
|
|
2116
|
-
if (!channel || (channel !== "slack" && channel !== "discord" && channel !== "telegram")) {
|
|
2117
|
-
return Response.json({ error: "channel is required (slack, discord, telegram)" }, { status: 400, headers });
|
|
2118
|
-
}
|
|
2119
|
-
const actorId = typeof body.actor_id === "string" ? body.actor_id.trim() : "";
|
|
2120
|
-
if (!actorId) {
|
|
2121
|
-
return Response.json({ error: "actor_id is required" }, { status: 400, headers });
|
|
2122
|
-
}
|
|
2123
|
-
const tenantId = typeof body.tenant_id === "string" ? body.tenant_id.trim() : "";
|
|
2124
|
-
if (!tenantId) {
|
|
2125
|
-
return Response.json({ error: "tenant_id is required" }, { status: 400, headers });
|
|
2126
|
-
}
|
|
2127
|
-
const roleKey = typeof body.role === "string" ? body.role.trim() : "operator";
|
|
2128
|
-
const roleScopes = ROLE_SCOPES[roleKey];
|
|
2129
|
-
if (!roleScopes) {
|
|
2130
|
-
return Response.json({ error: `invalid role: ${roleKey} (operator, contributor, viewer)` }, { status: 400, headers });
|
|
2131
|
-
}
|
|
2132
|
-
const bindingId = typeof body.binding_id === "string" && body.binding_id.trim().length > 0
|
|
2133
|
-
? body.binding_id.trim()
|
|
2134
|
-
: `bind-${crypto.randomUUID()}`;
|
|
2135
|
-
const operatorId = typeof body.operator_id === "string" && body.operator_id.trim().length > 0
|
|
2136
|
-
? body.operator_id.trim()
|
|
2137
|
-
: "default";
|
|
2138
|
-
const decision = await identityStore.link({
|
|
2139
|
-
bindingId,
|
|
2140
|
-
operatorId,
|
|
2141
|
-
channel: channel,
|
|
2142
|
-
channelTenantId: tenantId,
|
|
2143
|
-
channelActorId: actorId,
|
|
2144
|
-
scopes: [...roleScopes],
|
|
2145
|
-
});
|
|
2146
|
-
switch (decision.kind) {
|
|
2147
|
-
case "linked":
|
|
2148
|
-
return Response.json({ ok: true, kind: "linked", binding: decision.binding }, { status: 201, headers });
|
|
2149
|
-
case "binding_exists":
|
|
2150
|
-
return Response.json({ ok: false, kind: "binding_exists", binding: decision.binding }, { status: 409, headers });
|
|
2151
|
-
case "principal_already_linked":
|
|
2152
|
-
return Response.json({ ok: false, kind: "principal_already_linked", binding: decision.binding }, { status: 409, headers });
|
|
2153
|
-
}
|
|
2154
|
-
}
|
|
2155
|
-
if (path === "/api/identities/unlink") {
|
|
2156
|
-
if (request.method !== "POST") {
|
|
2157
|
-
return Response.json({ error: "Method Not Allowed" }, { status: 405, headers });
|
|
2158
|
-
}
|
|
2159
|
-
let body;
|
|
2160
|
-
try {
|
|
2161
|
-
body = (await request.json());
|
|
2162
|
-
}
|
|
2163
|
-
catch {
|
|
2164
|
-
return Response.json({ error: "invalid json body" }, { status: 400, headers });
|
|
2165
|
-
}
|
|
2166
|
-
const bindingId = typeof body.binding_id === "string" ? body.binding_id.trim() : "";
|
|
2167
|
-
if (!bindingId) {
|
|
2168
|
-
return Response.json({ error: "binding_id is required" }, { status: 400, headers });
|
|
2169
|
-
}
|
|
2170
|
-
const actorBindingId = typeof body.actor_binding_id === "string" ? body.actor_binding_id.trim() : "";
|
|
2171
|
-
if (!actorBindingId) {
|
|
2172
|
-
return Response.json({ error: "actor_binding_id is required" }, { status: 400, headers });
|
|
2173
|
-
}
|
|
2174
|
-
const reason = typeof body.reason === "string" ? body.reason.trim() : null;
|
|
2175
|
-
const decision = await identityStore.unlinkSelf({
|
|
2176
|
-
bindingId,
|
|
2177
|
-
actorBindingId,
|
|
2178
|
-
reason: reason || null,
|
|
2179
|
-
});
|
|
2180
|
-
switch (decision.kind) {
|
|
2181
|
-
case "unlinked":
|
|
2182
|
-
return Response.json({ ok: true, kind: "unlinked", binding: decision.binding }, { headers });
|
|
2183
|
-
case "not_found":
|
|
2184
|
-
return Response.json({ ok: false, kind: "not_found" }, { status: 404, headers });
|
|
2185
|
-
case "invalid_actor":
|
|
2186
|
-
return Response.json({ ok: false, kind: "invalid_actor" }, { status: 403, headers });
|
|
2187
|
-
case "already_inactive":
|
|
2188
|
-
return Response.json({ ok: false, kind: "already_inactive", binding: decision.binding }, { status: 409, headers });
|
|
2189
|
-
}
|
|
2190
|
-
}
|
|
2191
|
-
}
|
|
2192
|
-
if (path.startsWith("/api/issues")) {
|
|
2193
|
-
const response = await issueRoutes(request, context);
|
|
2194
|
-
headers.forEach((value, key) => {
|
|
2195
|
-
response.headers.set(key, value);
|
|
2196
|
-
});
|
|
2197
|
-
return response;
|
|
2198
|
-
}
|
|
2199
|
-
if (path.startsWith("/api/forum")) {
|
|
2200
|
-
const response = await forumRoutes(request, context);
|
|
2201
|
-
headers.forEach((value, key) => {
|
|
2202
|
-
response.headers.set(key, value);
|
|
2203
|
-
});
|
|
2204
|
-
return response;
|
|
2205
|
-
}
|
|
2206
|
-
if (path.startsWith("/api/events")) {
|
|
2207
|
-
const response = await eventRoutes(request, context);
|
|
2208
|
-
headers.forEach((value, key) => {
|
|
2209
|
-
response.headers.set(key, value);
|
|
2210
|
-
});
|
|
2211
|
-
return response;
|
|
2212
|
-
}
|
|
2213
|
-
if (path.startsWith("/webhooks/")) {
|
|
2214
|
-
const response = await controlPlaneProxy.handleWebhook(path, request);
|
|
2215
|
-
if (response) {
|
|
2216
|
-
headers.forEach((value, key) => {
|
|
2217
|
-
response.headers.set(key, value);
|
|
2218
|
-
});
|
|
2219
|
-
return response;
|
|
2220
|
-
}
|
|
2221
|
-
}
|
|
2222
|
-
const filePath = resolve(PUBLIC_DIR, `.${path === "/" ? "/index.html" : path}`);
|
|
2223
|
-
if (!filePath.startsWith(PUBLIC_DIR)) {
|
|
2224
|
-
return new Response("Forbidden", { status: 403, headers });
|
|
2225
|
-
}
|
|
2226
|
-
const file = Bun.file(filePath);
|
|
2227
|
-
if (await file.exists()) {
|
|
2228
|
-
const ext = extname(filePath);
|
|
2229
|
-
const mime = MIME_TYPES[ext] ?? "application/octet-stream";
|
|
2230
|
-
headers.set("Content-Type", mime);
|
|
2231
|
-
return new Response(await file.arrayBuffer(), { status: 200, headers });
|
|
2232
|
-
}
|
|
2233
|
-
const indexPath = join(PUBLIC_DIR, "index.html");
|
|
2234
|
-
const indexFile = Bun.file(indexPath);
|
|
2235
|
-
if (await indexFile.exists()) {
|
|
2236
|
-
headers.set("Content-Type", "text/html; charset=utf-8");
|
|
2237
|
-
return new Response(await indexFile.arrayBuffer(), { status: 200, headers });
|
|
2238
|
-
}
|
|
2239
|
-
return new Response("Not Found", { status: 404, headers });
|
|
2240
|
-
};
|
|
686
|
+
const handleRequest = createServerRequestHandler({
|
|
687
|
+
context,
|
|
688
|
+
controlPlaneProxy,
|
|
689
|
+
activitySupervisor,
|
|
690
|
+
heartbeatPrograms,
|
|
691
|
+
cronPrograms,
|
|
692
|
+
loadConfigFromDisk,
|
|
693
|
+
writeConfig,
|
|
694
|
+
reloadControlPlane,
|
|
695
|
+
getControlPlaneStatus: () => ({
|
|
696
|
+
...summarizeControlPlane(controlPlaneCurrent),
|
|
697
|
+
generation: generationSupervisor.snapshot(),
|
|
698
|
+
observability: {
|
|
699
|
+
counters: generationTelemetry.counters(),
|
|
700
|
+
},
|
|
701
|
+
}),
|
|
702
|
+
registerAutoRunHeartbeatProgram,
|
|
703
|
+
disableAutoRunHeartbeatProgram,
|
|
704
|
+
describeError,
|
|
705
|
+
});
|
|
2241
706
|
const server = {
|
|
2242
707
|
port: options.port || 3000,
|
|
2243
708
|
fetch: handleRequest,
|
|
@@ -2249,32 +714,15 @@ export function createServer(options = {}) {
|
|
|
2249
714
|
};
|
|
2250
715
|
return server;
|
|
2251
716
|
}
|
|
2252
|
-
export
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
const heartbeatScheduler = options.heartbeatScheduler ?? new ActivityHeartbeatScheduler();
|
|
2256
|
-
const generationTelemetry = options.generationTelemetry ?? new GenerationTelemetryRecorder();
|
|
2257
|
-
const controlPlane = await bootstrapControlPlane({
|
|
2258
|
-
repoRoot,
|
|
2259
|
-
config: config.control_plane,
|
|
2260
|
-
heartbeatScheduler,
|
|
2261
|
-
generation: {
|
|
2262
|
-
generation_id: "control-plane-gen-0",
|
|
2263
|
-
generation_seq: 0,
|
|
2264
|
-
},
|
|
2265
|
-
telemetry: generationTelemetry,
|
|
2266
|
-
sessionMutationHooks: options.sessionMutationHooks,
|
|
2267
|
-
terminalEnabled: true,
|
|
2268
|
-
});
|
|
2269
|
-
const serverConfig = createServer({
|
|
717
|
+
export { composeServerRuntime } from "./server_runtime.js";
|
|
718
|
+
export function createServerFromRuntime(runtime, options = {}) {
|
|
719
|
+
return createServer({
|
|
2270
720
|
...options,
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
generationTelemetry,
|
|
721
|
+
repoRoot: runtime.repoRoot,
|
|
722
|
+
config: runtime.config,
|
|
723
|
+
heartbeatScheduler: runtime.heartbeatScheduler,
|
|
724
|
+
generationTelemetry: runtime.generationTelemetry,
|
|
725
|
+
sessionLifecycle: runtime.sessionLifecycle,
|
|
726
|
+
controlPlane: runtime.controlPlane,
|
|
2275
727
|
});
|
|
2276
|
-
return {
|
|
2277
|
-
serverConfig,
|
|
2278
|
-
controlPlane: serverConfig.controlPlane,
|
|
2279
|
-
};
|
|
2280
728
|
}
|