@ordewell/web 0.12.6 → 0.13.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.
- package/dist/server/main.js +531 -210
- package/dist/server/main.js.map +1 -1
- package/package.json +2 -2
package/dist/server/main.js
CHANGED
|
@@ -70,6 +70,16 @@ function classify(err) {
|
|
|
70
70
|
if (err instanceof core.PlannerTurnStoppedError) return { status: 409, code: "planner_turn_stopped" };
|
|
71
71
|
if (err instanceof core.PlannerTurnDiscardedError) return { status: 409, code: "planner_turn_discarded" };
|
|
72
72
|
if (err instanceof core.ConversationBusyError) return { status: 409, code: "conversation_busy" };
|
|
73
|
+
if (err instanceof core.WorkspaceReadError) {
|
|
74
|
+
if (err.code === "gone") return { status: 410, code: "gone" };
|
|
75
|
+
if (err.code === "path_refused") return { status: 403, code: "path_refused" };
|
|
76
|
+
if (err.code === "path_not_found") return { status: 404, code: "path_not_found" };
|
|
77
|
+
return { status: 400, code: "refused" };
|
|
78
|
+
}
|
|
79
|
+
if (err instanceof core.UserGitError) {
|
|
80
|
+
if (err.code === "landing_branch_held" || err.code === "confirm_stale" || err.code === "stale") return { status: 409, code: err.code };
|
|
81
|
+
return { status: 400, code: err.code };
|
|
82
|
+
}
|
|
73
83
|
if (err instanceof core.ConversationEditError || err instanceof core.PlanEditError || err instanceof core.TaskControlError) {
|
|
74
84
|
return { status: 400, code: "refused" };
|
|
75
85
|
}
|
|
@@ -88,7 +98,9 @@ function failure(c, err, label, { fallback = 500, message = "Internal error" } =
|
|
|
88
98
|
...classified ? { code: classified.code } : {},
|
|
89
99
|
...err instanceof core.WorkspaceNotAProjectError ? { workspace: err.workspace } : {},
|
|
90
100
|
...err instanceof core.PlannerNotReadyError ? { plannerNotReady: err.reason } : {},
|
|
91
|
-
...err instanceof core.SessionHeldError ? { sessionHeld: err.held } : {}
|
|
101
|
+
...err instanceof core.SessionHeldError ? { sessionHeld: err.held } : {},
|
|
102
|
+
...err instanceof core.UserGitError && err.detail.held ? { landingBranchHeld: err.detail.held } : {},
|
|
103
|
+
...err instanceof core.UserGitError && err.detail.difference ? { confirmStale: err.detail.difference } : {}
|
|
92
104
|
};
|
|
93
105
|
return c.json(body, status);
|
|
94
106
|
}
|
|
@@ -101,14 +113,14 @@ var CONTROL_REFUSALS = {
|
|
|
101
113
|
no_task: { status: 404, code: "child_task_not_found" },
|
|
102
114
|
not_waiting: { status: 409, code: "checkpoint_not_waiting" }
|
|
103
115
|
};
|
|
104
|
-
function childrenRoute(
|
|
116
|
+
function childrenRoute(pool) {
|
|
105
117
|
const router = new hono.Hono();
|
|
106
118
|
const childOf = async (c) => {
|
|
107
|
-
const child = await
|
|
119
|
+
const child = await pool.session(c.req.param("id")).children.find(c.req.param("childId"));
|
|
108
120
|
if (!child) throw new ChildNotFoundError();
|
|
109
121
|
return child;
|
|
110
122
|
};
|
|
111
|
-
const position = (c) =>
|
|
123
|
+
const position = (c) => pool.childPosition(c.req.param("id"), c.req.param("childId"));
|
|
112
124
|
const controlled = (c, outcome) => {
|
|
113
125
|
if (!outcome.ok) return refuse(c, CONTROL_REFUSALS[outcome.code].status, outcome.message, CONTROL_REFUSALS[outcome.code].code);
|
|
114
126
|
return c.json({ message: outcome.message, ...outcome.id ? { id: outcome.id } : {} });
|
|
@@ -116,7 +128,7 @@ function childrenRoute(pool2) {
|
|
|
116
128
|
const asksAboveItsPlan = (body) => "raiseTokens" in body || "force" in body;
|
|
117
129
|
router.get("/:id/children", async (c) => {
|
|
118
130
|
try {
|
|
119
|
-
return c.json({ children: await
|
|
131
|
+
return c.json({ children: await pool.session(c.req.param("id")).children.list() });
|
|
120
132
|
} catch (err) {
|
|
121
133
|
return failure(c, err, "list child plans");
|
|
122
134
|
}
|
|
@@ -356,24 +368,30 @@ function scanWorkspacesImpl(deps) {
|
|
|
356
368
|
} catch {
|
|
357
369
|
}
|
|
358
370
|
}
|
|
359
|
-
|
|
371
|
+
const hidden = projectDir === void 0 ? void 0 : pathModule.resolve(projectDir);
|
|
372
|
+
return [...paths].filter((p) => p !== hidden).sort();
|
|
360
373
|
}
|
|
361
|
-
function scanWorkspaces() {
|
|
374
|
+
function scanWorkspaces(sea = {}) {
|
|
362
375
|
return scanWorkspacesImpl({
|
|
363
376
|
fsModule: fs__namespace,
|
|
364
377
|
pathModule: path__namespace,
|
|
365
378
|
cwd: process.cwd(),
|
|
366
379
|
// Not HOME, which Windows does not set: os.homedir() reads USERPROFILE there.
|
|
367
380
|
home: os__namespace.homedir(),
|
|
368
|
-
|
|
381
|
+
// Four up from dist/server/ is the root of an Ordewell source checkout,
|
|
382
|
+
// whose own `.ordewell/` (its committed skills) is no workspace of the
|
|
383
|
+
// user's. The binary has no checkout: its `__dirname` is the executable's
|
|
384
|
+
// folder, and four up from there could be the home or a projects folder
|
|
385
|
+
// that does hold one, so nothing is hidden (ADR-0065 C2).
|
|
386
|
+
projectDir: core.isSea(sea) ? void 0 : path__namespace.resolve(__dirname, "..", "..", "..", "..")
|
|
369
387
|
});
|
|
370
388
|
}
|
|
371
389
|
var MAX_OUTPUT_BYTES = 4 * 1024 * 1024;
|
|
372
390
|
var SEALED_OUTPUT_GRACE_MS = 1e3;
|
|
373
|
-
function run(file,
|
|
391
|
+
function run(file, args, opts) {
|
|
374
392
|
return new Promise((resolve3) => {
|
|
375
393
|
try {
|
|
376
|
-
const child = child_process.execFile(file,
|
|
394
|
+
const child = child_process.execFile(file, args, {
|
|
377
395
|
cwd: opts.cwd,
|
|
378
396
|
timeout: opts.timeout,
|
|
379
397
|
maxBuffer: MAX_OUTPUT_BYTES,
|
|
@@ -558,8 +576,8 @@ var PoolFileSystem = class _PoolFileSystem extends core.BaseFileSystem {
|
|
|
558
576
|
}
|
|
559
577
|
}
|
|
560
578
|
async execBashImpl(command, shell, signal, cwd) {
|
|
561
|
-
const { file, args
|
|
562
|
-
const result = file === null ? await run(command, [], { cwd, timeout: BASH_TIMEOUT_MS, shell: true, env, signal }) : await run(file, [...
|
|
579
|
+
const { file, args, env, wallTimeMs } = shell;
|
|
580
|
+
const result = file === null ? await run(command, [], { cwd, timeout: BASH_TIMEOUT_MS, shell: true, env, signal }) : await run(file, [...args, command], { cwd, timeout: wallTimeMs ?? BASH_TIMEOUT_MS, env, signal, sealed: wallTimeMs !== void 0 });
|
|
563
581
|
const out = (result.stdout || "").trim();
|
|
564
582
|
const err = (result.stderr || "").trim();
|
|
565
583
|
if (signal?.aborted) return { success: false, output: core.STOPPED_TOOL_RESULT, truncated: false };
|
|
@@ -600,8 +618,8 @@ var PoolAwareRunner = class {
|
|
|
600
618
|
this.owned.add(session.id);
|
|
601
619
|
session.onExit(() => this.owned.delete(session.id));
|
|
602
620
|
this.broadcast({ type: "task_started", taskId: opts.taskId, order: opts.order ?? 0, title: opts.title ?? "", runner: opts.runner, modelId: opts.modelId });
|
|
603
|
-
session.onOutput((
|
|
604
|
-
this.broadcast({ type: "task_output", taskId: opts.taskId, text });
|
|
621
|
+
session.onOutput((text2) => {
|
|
622
|
+
this.broadcast({ type: "task_output", taskId: opts.taskId, text: text2 });
|
|
605
623
|
});
|
|
606
624
|
return session;
|
|
607
625
|
}
|
|
@@ -689,10 +707,10 @@ var OrchestratorPool = class {
|
|
|
689
707
|
this.accountLimitCollectors = deps.accountLimitCollectors ?? ((workspace) => {
|
|
690
708
|
const workspaceRoot = () => workspace;
|
|
691
709
|
const isInstalled = (runner) => async () => (await this.runnerInstallation.filterInstalled([runner])).includes(runner);
|
|
692
|
-
const
|
|
710
|
+
const server = this.registry.getManifest("codex")?.modelDiscovery.appServer;
|
|
693
711
|
return [
|
|
694
712
|
core.claudeAccountLimitCollector({ workspaceRoot, isInstalled: isInstalled("claude-code") }),
|
|
695
|
-
core.codexAccountLimitCollector({ workspaceRoot, isInstalled: isInstalled("codex"), command:
|
|
713
|
+
core.codexAccountLimitCollector({ workspaceRoot, isInstalled: isInstalled("codex"), command: server?.command, args: server?.args }),
|
|
696
714
|
core.openCodeGoAccountLimitCollector({ workspaceRoot, isInstalled: isInstalled("opencode") })
|
|
697
715
|
];
|
|
698
716
|
});
|
|
@@ -830,6 +848,33 @@ var OrchestratorPool = class {
|
|
|
830
848
|
hasSession(sessionId) {
|
|
831
849
|
return this.sessions.has(sessionId);
|
|
832
850
|
}
|
|
851
|
+
/** The session open here under this id for this workspace, if any: an id names a session only within its workspace. */
|
|
852
|
+
heldHere(sessionId, workspace) {
|
|
853
|
+
const held = this.sessionWorkspaces.get(sessionId);
|
|
854
|
+
return held !== void 0 && path.resolve(held) === path.resolve(workspace) ? this.sessions.get(sessionId) : void 0;
|
|
855
|
+
}
|
|
856
|
+
/**
|
|
857
|
+
* Whether a session's run holds its landing branch (ADR-0062 G7): this
|
|
858
|
+
* host's own word for a session open here; for one another live host holds,
|
|
859
|
+
* that host's word in its lease record, or the saved record where the holder
|
|
860
|
+
* says nothing; and for one nobody holds, nothing is at work.
|
|
861
|
+
*/
|
|
862
|
+
userRunIsLive(sessionId, workspace, plan) {
|
|
863
|
+
const session = this.heldHere(sessionId, workspace);
|
|
864
|
+
if (session) return session.runIsLive;
|
|
865
|
+
const held = (this.leaseHost ?? core.processLeaseHost()).peek(sessionId, workspace);
|
|
866
|
+
return held !== null && (held.holder.runLive === true || core.savedRunLooksLive(plan));
|
|
867
|
+
}
|
|
868
|
+
/**
|
|
869
|
+
* A user git action into the session's timeline (ADR-0062 G6): straight in
|
|
870
|
+
* for a session open here, else left in its directory for whichever host
|
|
871
|
+
* holds it, or next opens it, to fold (ADR-0049 L5).
|
|
872
|
+
*/
|
|
873
|
+
recordUserAction(sessionId, workspace, text2) {
|
|
874
|
+
const session = this.heldHere(sessionId, workspace);
|
|
875
|
+
if (session?.planState) session.recordUserAction(text2);
|
|
876
|
+
else core.appendUserAction(sessionId, workspace, { text: text2, at: (/* @__PURE__ */ new Date()).toISOString() });
|
|
877
|
+
}
|
|
833
878
|
/** Answer an approval prompt, the planner's or a task runner's. False means the id was unknown or already settled. */
|
|
834
879
|
resolveApproval(sessionId, approvalId, answer) {
|
|
835
880
|
return this.sessions.get(sessionId)?.resolveApproval(approvalId, answer) ?? false;
|
|
@@ -843,6 +888,13 @@ var OrchestratorPool = class {
|
|
|
843
888
|
isExecuting(sessionId) {
|
|
844
889
|
return this.sessions.get(sessionId)?.isExecuting ?? false;
|
|
845
890
|
}
|
|
891
|
+
/** Work under way in any session held here, which stopping this daemon would cut off: a run not settled, or a planner turn answering. */
|
|
892
|
+
workLive() {
|
|
893
|
+
for (const session of this.sessions.values()) {
|
|
894
|
+
if (session.runIsLive || session.plannerTurnId !== void 0) return true;
|
|
895
|
+
}
|
|
896
|
+
return false;
|
|
897
|
+
}
|
|
846
898
|
getPlan(sessionId) {
|
|
847
899
|
return this.sessions.get(sessionId)?.planState ?? null;
|
|
848
900
|
}
|
|
@@ -972,6 +1024,9 @@ var OrchestratorPool = class {
|
|
|
972
1024
|
// to render the current level; without it the picker shows "default"
|
|
973
1025
|
// for a planner that is running at "high".
|
|
974
1026
|
plannerThinkingEffort: config.plannerThinkingEffort ?? "",
|
|
1027
|
+
// The saved level, read through the service so an environment override
|
|
1028
|
+
// is reported the way the next plan will read it (ADR-0001, ADR-0030).
|
|
1029
|
+
autonomousMode: this.settingsService.autonomousMode(),
|
|
975
1030
|
maxParallel: config.maxParallelSessions,
|
|
976
1031
|
modelAllowlist: userSettings.modelAllowlist,
|
|
977
1032
|
// The model remembered per planner backend (this task), so a surface can
|
|
@@ -988,9 +1043,9 @@ var OrchestratorPool = class {
|
|
|
988
1043
|
* they were opened with. Throws a {@link RunSettingsError}, saving nothing,
|
|
989
1044
|
* for a value out of range.
|
|
990
1045
|
*/
|
|
991
|
-
updateRunSettings(
|
|
992
|
-
this.settingsService.updateRunSettings(
|
|
993
|
-
if (sessionId) this.sessions.get(sessionId)?.applyRunSettings(
|
|
1046
|
+
updateRunSettings(changes2, sessionId, workspace) {
|
|
1047
|
+
this.settingsService.updateRunSettings(changes2);
|
|
1048
|
+
if (sessionId) this.sessions.get(sessionId)?.applyRunSettings(changes2);
|
|
994
1049
|
return this.getSettings(sessionId, workspace);
|
|
995
1050
|
}
|
|
996
1051
|
/** The `containers` setting as a surface shows it: `prove` starts a throwaway container to measure (ADR-0059 L3). */
|
|
@@ -1052,14 +1107,14 @@ var OrchestratorPool = class {
|
|
|
1052
1107
|
* reads a catalog not yet held, waits for that one; every other write — a
|
|
1053
1108
|
* key, an allowlist, a limit — lands at once (ADR-0040).
|
|
1054
1109
|
*/
|
|
1055
|
-
async writeSettings(
|
|
1056
|
-
const change = plannerChangeOf(
|
|
1057
|
-
if (!changesPlanner(change)) return this.updateSettings(
|
|
1110
|
+
async writeSettings(changes2, sessionId, requestWorkspace) {
|
|
1111
|
+
const change = plannerChangeOf(changes2, admittedEnv(changes2)?.accepted);
|
|
1112
|
+
if (!changesPlanner(change)) return this.updateSettings(changes2, sessionId, requestWorkspace);
|
|
1058
1113
|
const turn = this.plannerWrites.then(async () => {
|
|
1059
1114
|
const workspace = this.settingsWorkspace(sessionId, requestWorkspace);
|
|
1060
1115
|
const judging = this.catalogJudging(change, workspace);
|
|
1061
1116
|
await this.discoverUnheld(workspace, judging ? [judging] : []);
|
|
1062
|
-
return this.updateSettings(
|
|
1117
|
+
return this.updateSettings(changes2, sessionId, requestWorkspace);
|
|
1063
1118
|
});
|
|
1064
1119
|
this.plannerWrites = turn.catch(() => void 0);
|
|
1065
1120
|
return turn;
|
|
@@ -1094,12 +1149,12 @@ var OrchestratorPool = class {
|
|
|
1094
1149
|
plannerProviderInEffect(workspace) {
|
|
1095
1150
|
return this.plannerConfig(void 0, workspace).aiProvider;
|
|
1096
1151
|
}
|
|
1097
|
-
updateSettings(
|
|
1152
|
+
updateSettings(changes2, sessionId, requestWorkspace) {
|
|
1098
1153
|
const workspace = this.settingsWorkspace(sessionId, requestWorkspace);
|
|
1099
1154
|
const current = this.plannerConfig(void 0, workspace).planner;
|
|
1100
|
-
const admission = admittedEnv(
|
|
1155
|
+
const admission = admittedEnv(changes2);
|
|
1101
1156
|
const envChanges = admission ? admission.accepted : void 0;
|
|
1102
|
-
const plannerChange = plannerChangeOf(
|
|
1157
|
+
const plannerChange = plannerChangeOf(changes2, envChanges);
|
|
1103
1158
|
const selection = changesPlanner(plannerChange) ? core.selectPlanner(current, plannerChange, this.plannerModelMemory, this.plannerCatalogFor(plannerChange.provider ?? current.provider, workspace)) : void 0;
|
|
1104
1159
|
if (envChanges) {
|
|
1105
1160
|
let touched = false;
|
|
@@ -1128,8 +1183,8 @@ var OrchestratorPool = class {
|
|
|
1128
1183
|
this.pickedModel = { model: selection.next.model, provider: this.plannerProviderInEffect(workspace), workspace: workspaceKey(workspace) };
|
|
1129
1184
|
}
|
|
1130
1185
|
}
|
|
1131
|
-
if (
|
|
1132
|
-
const allowlist =
|
|
1186
|
+
if (changes2.modelAllowlist && typeof changes2.modelAllowlist === "object") {
|
|
1187
|
+
const allowlist = changes2.modelAllowlist;
|
|
1133
1188
|
for (const [runner, ids] of Object.entries(allowlist)) {
|
|
1134
1189
|
if (Array.isArray(ids)) {
|
|
1135
1190
|
this.settingsService.setModelAllowlist(runner, ids.map(String));
|
|
@@ -1138,6 +1193,9 @@ var OrchestratorPool = class {
|
|
|
1138
1193
|
}
|
|
1139
1194
|
}
|
|
1140
1195
|
}
|
|
1196
|
+
if (typeof changes2.autonomousMode === "boolean") {
|
|
1197
|
+
this.settingsService.setAutonomousMode(changes2.autonomousMode);
|
|
1198
|
+
}
|
|
1141
1199
|
if (envChanges) for (const monitor of this.accountLimits.values()) monitor.refresh();
|
|
1142
1200
|
if (selection?.remember) {
|
|
1143
1201
|
const { provider, model, effort } = selection.remember;
|
|
@@ -1262,19 +1320,19 @@ var OrchestratorPool = class {
|
|
|
1262
1320
|
const installedIds = new Set(
|
|
1263
1321
|
await installation.filterInstalled(runners.map((p) => p.manifest.name))
|
|
1264
1322
|
);
|
|
1265
|
-
|
|
1323
|
+
return Promise.all(
|
|
1266
1324
|
runners.filter((p) => installedIds.has(p.manifest.name)).map(async (p) => {
|
|
1267
|
-
const { usable, auth } = await installation.runnerUsability(p.manifest.name);
|
|
1268
|
-
if (!usable) return null;
|
|
1325
|
+
const { usable, reason, auth, version } = await installation.runnerUsability(p.manifest.name);
|
|
1269
1326
|
return {
|
|
1270
1327
|
id: p.manifest.name,
|
|
1271
1328
|
name: p.manifest.displayName,
|
|
1272
1329
|
enabled: enabled.has(p.manifest.name),
|
|
1273
|
-
...
|
|
1330
|
+
...usable ? {} : { usable: false, ...reason ? { reason } : {} },
|
|
1331
|
+
...auth ? { auth } : {},
|
|
1332
|
+
...version ? { version } : {}
|
|
1274
1333
|
};
|
|
1275
1334
|
})
|
|
1276
1335
|
);
|
|
1277
|
-
return rows.filter((row) => row !== null);
|
|
1278
1336
|
}
|
|
1279
1337
|
async generatePlan(sessionId, goal, runners, workspace, modelOverride, options) {
|
|
1280
1338
|
const recheck = this.recheckSignedOutPlanner(workspace, this.plannerConfig(modelOverride).aiProvider);
|
|
@@ -1395,6 +1453,7 @@ var OrchestratorPool = class {
|
|
|
1395
1453
|
}
|
|
1396
1454
|
this.destroy(sessionId);
|
|
1397
1455
|
this.registerSession(sessionId, workspace, session);
|
|
1456
|
+
session.foldUserActions();
|
|
1398
1457
|
return session.planState ?? saved.plan;
|
|
1399
1458
|
}
|
|
1400
1459
|
/**
|
|
@@ -1475,15 +1534,15 @@ async function removeSession(sessionId, workspace, stop, close, reclaim) {
|
|
|
1475
1534
|
await reclaim();
|
|
1476
1535
|
return core.deleteSession(sessionId, workspace);
|
|
1477
1536
|
}
|
|
1478
|
-
function admittedEnv(
|
|
1479
|
-
const raw =
|
|
1537
|
+
function admittedEnv(changes2) {
|
|
1538
|
+
const raw = changes2.env;
|
|
1480
1539
|
return raw && typeof raw === "object" && !Array.isArray(raw) ? core.admitSettingsEnv(raw) : void 0;
|
|
1481
1540
|
}
|
|
1482
|
-
function plannerChangeOf(
|
|
1483
|
-
return
|
|
1541
|
+
function plannerChangeOf(changes2, envChanges) {
|
|
1542
|
+
return changes2.inherited === true ? {} : {
|
|
1484
1543
|
provider: envChanges?.AI_PROVIDER,
|
|
1485
|
-
model: envChanges?.ORCHESTRATOR_MODEL ?? stringField(
|
|
1486
|
-
effort: envChanges?.ORDEWELL_PLANNER_EFFORT ?? stringField(
|
|
1544
|
+
model: envChanges?.ORCHESTRATOR_MODEL ?? stringField(changes2.orchestratorModel),
|
|
1545
|
+
effort: envChanges?.ORDEWELL_PLANNER_EFFORT ?? stringField(changes2.plannerThinkingEffort)
|
|
1487
1546
|
};
|
|
1488
1547
|
}
|
|
1489
1548
|
function changesPlanner(change) {
|
|
@@ -1495,7 +1554,7 @@ function stringField(value) {
|
|
|
1495
1554
|
function workspaceKey(workspace) {
|
|
1496
1555
|
return path.resolve(workspace ?? process.cwd());
|
|
1497
1556
|
}
|
|
1498
|
-
function sessionsRoute(
|
|
1557
|
+
function sessionsRoute(pool) {
|
|
1499
1558
|
const router = new hono.Hono();
|
|
1500
1559
|
router.get("/", (c) => {
|
|
1501
1560
|
const ws = c.req.query("workspace") || process.cwd();
|
|
@@ -1505,14 +1564,18 @@ function sessionsRoute(pool2) {
|
|
|
1505
1564
|
router.get("/:id", (c) => {
|
|
1506
1565
|
const ws = c.req.query("workspace") || process.cwd();
|
|
1507
1566
|
const id = c.req.param("id");
|
|
1508
|
-
const saved = core.
|
|
1567
|
+
const saved = core.loadSession(id, ws);
|
|
1509
1568
|
if (!saved) return refuse(c, 404, "Session not found", "session_not_found");
|
|
1510
|
-
const run2 =
|
|
1511
|
-
const
|
|
1569
|
+
const run2 = pool.runSnapshot(id);
|
|
1570
|
+
const plannerTurnId = pool.plannerTurnId(id);
|
|
1571
|
+
const plannerCompaction = pool.plannerCompaction(id);
|
|
1572
|
+
const { conversationHistory, researchLog, plannerUsage, pendingTurns } = core.surfacePlan(pool.getPlan(id) ?? saved.plan);
|
|
1512
1573
|
return c.json({
|
|
1513
1574
|
meta: saved.meta,
|
|
1514
|
-
plan: core.surfacePlanState(
|
|
1575
|
+
plan: core.surfacePlanState(pool.getPlanState(id) ?? core.migratePlanState(saved.plan)),
|
|
1576
|
+
conversation: { conversationHistory, researchLog, plannerUsage, pendingTurns },
|
|
1515
1577
|
...run2 ? { run: run2 } : {},
|
|
1578
|
+
...plannerTurnId ? { plannerTurnId } : {},
|
|
1516
1579
|
...plannerCompaction ? { plannerCompaction } : {}
|
|
1517
1580
|
});
|
|
1518
1581
|
});
|
|
@@ -1520,14 +1583,14 @@ function sessionsRoute(pool2) {
|
|
|
1520
1583
|
const ws = c.req.query("workspace") || process.cwd();
|
|
1521
1584
|
try {
|
|
1522
1585
|
const id = c.req.param("id");
|
|
1523
|
-
const plan =
|
|
1524
|
-
const run2 =
|
|
1525
|
-
const plannerTurnId =
|
|
1526
|
-
const plannerCompaction =
|
|
1586
|
+
const plan = pool.adoptSavedSession(id, ws);
|
|
1587
|
+
const run2 = pool.runSnapshot(id);
|
|
1588
|
+
const plannerTurnId = pool.plannerTurnId(id);
|
|
1589
|
+
const plannerCompaction = pool.plannerCompaction(id);
|
|
1527
1590
|
return c.json({
|
|
1528
1591
|
ok: true,
|
|
1529
1592
|
plan: core.surfacePlan(plan),
|
|
1530
|
-
goal:
|
|
1593
|
+
goal: pool.getGoal(id),
|
|
1531
1594
|
...run2 ? { run: run2 } : {},
|
|
1532
1595
|
...plannerTurnId ? { plannerTurnId } : {},
|
|
1533
1596
|
...plannerCompaction ? { plannerCompaction } : {}
|
|
@@ -1538,21 +1601,21 @@ function sessionsRoute(pool2) {
|
|
|
1538
1601
|
});
|
|
1539
1602
|
router.get("/:id/tasks/:taskId/log", (c) => {
|
|
1540
1603
|
const ws = c.req.query("workspace") || process.cwd();
|
|
1541
|
-
return c.json({ attempts: core.listTaskLogAttempts({ baseDir: ws, sessionId: c.req.param("id") }, c.req.param("taskId")), position:
|
|
1604
|
+
return c.json({ attempts: core.listTaskLogAttempts({ baseDir: ws, sessionId: c.req.param("id") }, c.req.param("taskId")), position: pool.streamPosition(c.req.param("id")) });
|
|
1542
1605
|
});
|
|
1543
1606
|
router.get("/:id/tasks/:taskId/log/:attempt", (c) => {
|
|
1544
1607
|
const ws = c.req.query("workspace") || process.cwd();
|
|
1545
1608
|
const attempt = Number(c.req.param("attempt"));
|
|
1546
1609
|
if (!Number.isInteger(attempt) || attempt < 1) return refuse(c, 400, "attempt must be a positive integer");
|
|
1547
|
-
return c.json({ attempt, events: core.taskLogForSurface(core.readTaskLog({ baseDir: ws, sessionId: c.req.param("id") }, c.req.param("taskId"), attempt)), position:
|
|
1610
|
+
return c.json({ attempt, events: core.taskLogForSurface(core.readTaskLog({ baseDir: ws, sessionId: c.req.param("id") }, c.req.param("taskId"), attempt)), position: pool.streamPosition(c.req.param("id")) });
|
|
1548
1611
|
});
|
|
1549
1612
|
router.delete("/:id", async (c) => {
|
|
1550
1613
|
const ws = c.req.query("workspace") || process.cwd();
|
|
1551
1614
|
const id = c.req.param("id");
|
|
1552
|
-
const held =
|
|
1615
|
+
const held = pool.hasSession(id);
|
|
1553
1616
|
let removed;
|
|
1554
1617
|
try {
|
|
1555
|
-
removed = await removeSession(id, ws, () =>
|
|
1618
|
+
removed = await removeSession(id, ws, () => pool.stopTree(id), () => pool.close(id), () => pool.reclaim(id));
|
|
1556
1619
|
} catch (err) {
|
|
1557
1620
|
return c.json({ ok: false, message: err instanceof Error ? err.message : String(err) });
|
|
1558
1621
|
}
|
|
@@ -1563,7 +1626,7 @@ function sessionsRoute(pool2) {
|
|
|
1563
1626
|
});
|
|
1564
1627
|
});
|
|
1565
1628
|
router.post("/:id/close", async (c) => {
|
|
1566
|
-
await
|
|
1629
|
+
await pool.close(c.req.param("id"));
|
|
1567
1630
|
return c.json({ ok: true });
|
|
1568
1631
|
});
|
|
1569
1632
|
return router;
|
|
@@ -1577,7 +1640,7 @@ async function runUserCommand(session, command) {
|
|
|
1577
1640
|
if (!outcome.ok) throw new core.TaskControlError(outcome.message);
|
|
1578
1641
|
return outcome;
|
|
1579
1642
|
}
|
|
1580
|
-
function plansRoute(
|
|
1643
|
+
function plansRoute(pool) {
|
|
1581
1644
|
const router = new hono.Hono();
|
|
1582
1645
|
router.post("/:sessionId/generate", async (c) => {
|
|
1583
1646
|
try {
|
|
@@ -1586,9 +1649,9 @@ function plansRoute(pool2) {
|
|
|
1586
1649
|
const ws = workspace || c.req.query("workspace") || process.cwd();
|
|
1587
1650
|
const queryRunners = c.req.query("runners");
|
|
1588
1651
|
const runnerList = Array.isArray(runners) ? runners : runners ? [runners] : queryRunners ? queryRunners.split(",").map((s) => s.trim()).filter(Boolean) : void 0;
|
|
1589
|
-
const plan = await
|
|
1590
|
-
const { models, modelsByRunner } = await
|
|
1591
|
-
const notes = (
|
|
1652
|
+
const plan = await pool.generatePlan(c.req.param("sessionId"), goal, runnerList, ws, model, { allowInit });
|
|
1653
|
+
const { models, modelsByRunner } = await pool.getProviderModels(ws);
|
|
1654
|
+
const notes = (pool.session(c.req.param("sessionId")).planState?.conversationHistory ?? []).filter((e) => e.kind === "system").map((e) => e.content);
|
|
1592
1655
|
return c.json({ plan: core.surfacePlanState(plan), models, modelsByRunner, ...notes.length > 0 ? { notes } : {} });
|
|
1593
1656
|
} catch (err) {
|
|
1594
1657
|
return failure(c, err, "generate", { message: "Plan generation failed" });
|
|
@@ -1596,7 +1659,7 @@ function plansRoute(pool2) {
|
|
|
1596
1659
|
});
|
|
1597
1660
|
router.post("/:sessionId/execute", async (c) => {
|
|
1598
1661
|
try {
|
|
1599
|
-
const session =
|
|
1662
|
+
const session = pool.session(c.req.param("sessionId"));
|
|
1600
1663
|
if (!session.planState || session.planTasks.length === 0) throw new core.NoPlanError("execute");
|
|
1601
1664
|
const { message } = await runUserCommand(session, { command: "run_plan" });
|
|
1602
1665
|
return c.json({ status: "running", message });
|
|
@@ -1606,8 +1669,8 @@ function plansRoute(pool2) {
|
|
|
1606
1669
|
});
|
|
1607
1670
|
router.post("/:sessionId/stop", async (c) => {
|
|
1608
1671
|
try {
|
|
1609
|
-
if (
|
|
1610
|
-
const session =
|
|
1672
|
+
if (pool.hasSession(c.req.param("sessionId"))) {
|
|
1673
|
+
const session = pool.session(c.req.param("sessionId"));
|
|
1611
1674
|
const outcome = await session.execute({ command: "stop_run" }, { kind: "user" });
|
|
1612
1675
|
if (!outcome.ok && (session.isExecuting || session.startsPending)) throw new core.TaskControlError(outcome.message);
|
|
1613
1676
|
return c.json({ status: "stopped", message: outcome.message });
|
|
@@ -1618,14 +1681,14 @@ function plansRoute(pool2) {
|
|
|
1618
1681
|
}
|
|
1619
1682
|
});
|
|
1620
1683
|
router.post("/:sessionId/planning/stop", (c) => {
|
|
1621
|
-
const cancelled =
|
|
1684
|
+
const cancelled = pool.cancelPlanning(c.req.param("sessionId"));
|
|
1622
1685
|
return c.json({ cancelled });
|
|
1623
1686
|
});
|
|
1624
1687
|
router.post("/:sessionId/execute-command", async (c) => {
|
|
1625
1688
|
try {
|
|
1626
1689
|
const parsed = core.parseExecutionCommand(await jsonObject(c));
|
|
1627
1690
|
if (!parsed.ok) return refuse(c, 400, parsed.message, "refused");
|
|
1628
|
-
const outcome = await
|
|
1691
|
+
const outcome = await pool.session(c.req.param("sessionId")).execute(parsed.command, { kind: "user" });
|
|
1629
1692
|
if (!outcome.ok) {
|
|
1630
1693
|
return c.json({ error: outcome.message, code: "refused", ...outcome.payload ? { payload: outcome.payload } : {} }, 400);
|
|
1631
1694
|
}
|
|
@@ -1636,7 +1699,7 @@ function plansRoute(pool2) {
|
|
|
1636
1699
|
});
|
|
1637
1700
|
router.get("/:sessionId/escalations", (c) => {
|
|
1638
1701
|
try {
|
|
1639
|
-
return c.json({ escalations:
|
|
1702
|
+
return c.json({ escalations: pool.session(c.req.param("sessionId")).escalations() });
|
|
1640
1703
|
} catch (err) {
|
|
1641
1704
|
return failure(c, err, "escalations");
|
|
1642
1705
|
}
|
|
@@ -1645,7 +1708,7 @@ function plansRoute(pool2) {
|
|
|
1645
1708
|
try {
|
|
1646
1709
|
const { id, option, note, confirmed } = await jsonObject(c);
|
|
1647
1710
|
if (typeof id !== "string" || typeof option !== "string") return refuse(c, 400, "id and option are required", "refused");
|
|
1648
|
-
const outcome = await
|
|
1711
|
+
const outcome = await pool.session(c.req.param("sessionId")).answerEscalation(id, option, typeof note === "string" ? note : void 0, confirmed === true);
|
|
1649
1712
|
if (!outcome.ok) return c.json({ error: outcome.message, code: "refused", ...outcome.payload ? { payload: outcome.payload } : {} }, 400);
|
|
1650
1713
|
return c.json({ ok: true, message: outcome.message, ...outcome.payload ? { payload: outcome.payload } : {} });
|
|
1651
1714
|
} catch (err) {
|
|
@@ -1657,8 +1720,8 @@ function plansRoute(pool2) {
|
|
|
1657
1720
|
const body = await c.req.json().catch(() => ({}));
|
|
1658
1721
|
const fields = typeof body === "object" && body !== null ? body : {};
|
|
1659
1722
|
if (typeof fields.name !== "string" || !fields.name.trim()) return c.json({ error: "name is required" }, 400);
|
|
1660
|
-
const
|
|
1661
|
-
const id =
|
|
1723
|
+
const args = typeof fields.args === "string" ? fields.args : "";
|
|
1724
|
+
const id = pool.session(c.req.param("sessionId")).invokeTaskCommand(c.req.param("taskId"), fields.name.trim(), args);
|
|
1662
1725
|
return c.json({ id });
|
|
1663
1726
|
} catch (err) {
|
|
1664
1727
|
return failure(c, err, "task edit");
|
|
@@ -1672,7 +1735,7 @@ function plansRoute(pool2) {
|
|
|
1672
1735
|
const raiseTokens = typeof body === "object" && body !== null && "raiseTokens" in body ? body.raiseTokens : void 0;
|
|
1673
1736
|
const raise = typeof raiseTokens === "number" && Number.isSafeInteger(raiseTokens) && raiseTokens > 0 ? raiseTokens : void 0;
|
|
1674
1737
|
if (raiseTokens !== void 0 && raise === void 0) return refuse(c, 400, "A raise is a whole number of tokens, 1 or more.", "refused");
|
|
1675
|
-
const session =
|
|
1738
|
+
const session = pool.session(c.req.param("sessionId"));
|
|
1676
1739
|
const taskId = idOfRef(session, c.req.param("taskId"));
|
|
1677
1740
|
if (!session.awaitsCheckpoint(taskId)) return refuse(c, 409, NO_CHECKPOINT, "checkpoint_not_waiting");
|
|
1678
1741
|
const { message } = await runUserCommand(session, { command: "answer_checkpoint", task: taskId, answer, ...reason ? { reason } : {}, ...raise !== void 0 ? { raiseTokens: raise } : {} });
|
|
@@ -1684,7 +1747,7 @@ function plansRoute(pool2) {
|
|
|
1684
1747
|
}
|
|
1685
1748
|
router.get("/:sessionId/tasks/:taskId/merge-gate", (c) => {
|
|
1686
1749
|
try {
|
|
1687
|
-
const session =
|
|
1750
|
+
const session = pool.session(c.req.param("sessionId"));
|
|
1688
1751
|
const tasks = core.flattenTasks(session.planState?.tasks ?? []);
|
|
1689
1752
|
const mergeGate = session.mergeGate(idOfRef(session, c.req.param("taskId"))).map((id) => {
|
|
1690
1753
|
const dep = tasks.find((t) => t.id === id);
|
|
@@ -1697,7 +1760,7 @@ function plansRoute(pool2) {
|
|
|
1697
1760
|
});
|
|
1698
1761
|
router.get("/:sessionId/tasks/:taskId/revert-preview", async (c) => {
|
|
1699
1762
|
try {
|
|
1700
|
-
const session =
|
|
1763
|
+
const session = pool.session(c.req.param("sessionId"));
|
|
1701
1764
|
return c.json(await session.revertPreview(idOfRef(session, c.req.param("taskId"))));
|
|
1702
1765
|
} catch (err) {
|
|
1703
1766
|
return failure(c, err, "task edit");
|
|
@@ -1705,7 +1768,7 @@ function plansRoute(pool2) {
|
|
|
1705
1768
|
});
|
|
1706
1769
|
router.get("/:sessionId/isolation/diff", async (c) => {
|
|
1707
1770
|
try {
|
|
1708
|
-
return c.json({ diff: await
|
|
1771
|
+
return c.json({ diff: await pool.session(c.req.param("sessionId")).reviewRunDiff() });
|
|
1709
1772
|
} catch (err) {
|
|
1710
1773
|
return failure(c, err, "task edit");
|
|
1711
1774
|
}
|
|
@@ -1714,7 +1777,7 @@ function plansRoute(pool2) {
|
|
|
1714
1777
|
try {
|
|
1715
1778
|
const body = await c.req.json().catch(() => ({}));
|
|
1716
1779
|
const pastChecks = typeof body === "object" && body !== null && "pastChecks" in body && body.pastChecks === true;
|
|
1717
|
-
const outcome = await
|
|
1780
|
+
const outcome = await pool.session(c.req.param("sessionId")).execute(pastChecks ? { command: "merge_all", pastChecks: true } : { command: "merge_all" }, { kind: "user" });
|
|
1718
1781
|
if (outcome.payload?.kind === "merge") return c.json(outcome.payload.result);
|
|
1719
1782
|
if (outcome.payload?.kind === "merge_hold") return refuse(c, 409, outcome.message, "merge_past_check");
|
|
1720
1783
|
if (!outcome.ok) throw new core.TaskControlError(outcome.message);
|
|
@@ -1731,7 +1794,7 @@ function plansRoute(pool2) {
|
|
|
1731
1794
|
]) {
|
|
1732
1795
|
router.post(`/:sessionId/isolation/${segment}`, async (c) => {
|
|
1733
1796
|
try {
|
|
1734
|
-
await run2(
|
|
1797
|
+
await run2(pool.session(c.req.param("sessionId")));
|
|
1735
1798
|
return c.json({ ok: true });
|
|
1736
1799
|
} catch (err) {
|
|
1737
1800
|
return failure(c, err, "task edit");
|
|
@@ -1740,7 +1803,7 @@ function plansRoute(pool2) {
|
|
|
1740
1803
|
}
|
|
1741
1804
|
router.post("/:sessionId/tasks/:taskId/resolve-conflict", async (c) => {
|
|
1742
1805
|
try {
|
|
1743
|
-
const plan = await
|
|
1806
|
+
const plan = await pool.session(c.req.param("sessionId")).resolveConflictAsTask(c.req.param("taskId"));
|
|
1744
1807
|
return c.json({ plan: plan && core.surfacePlan(plan) });
|
|
1745
1808
|
} catch (err) {
|
|
1746
1809
|
return failure(c, err, "task edit");
|
|
@@ -1748,9 +1811,9 @@ function plansRoute(pool2) {
|
|
|
1748
1811
|
});
|
|
1749
1812
|
router.put("/:sessionId/tasks/:taskId", async (c) => {
|
|
1750
1813
|
try {
|
|
1751
|
-
const
|
|
1752
|
-
if (
|
|
1753
|
-
const result = await
|
|
1814
|
+
const changes2 = await jsonObject(c);
|
|
1815
|
+
if (changes2.assignedModel === null) changes2.assignedModel = void 0;
|
|
1816
|
+
const result = await pool.session(c.req.param("sessionId")).updateTask(c.req.param("taskId"), changes2);
|
|
1754
1817
|
if (!result) return refuse(c, 404, "Task not found", "task_not_found");
|
|
1755
1818
|
return c.json({ ok: true });
|
|
1756
1819
|
} catch (err) {
|
|
@@ -1759,7 +1822,7 @@ function plansRoute(pool2) {
|
|
|
1759
1822
|
});
|
|
1760
1823
|
router.delete("/:sessionId/tasks/:taskId", async (c) => {
|
|
1761
1824
|
try {
|
|
1762
|
-
const result = await
|
|
1825
|
+
const result = await pool.session(c.req.param("sessionId")).removeTask(c.req.param("taskId"));
|
|
1763
1826
|
if (!result) return refuse(c, 404, "Task not found", "task_not_found");
|
|
1764
1827
|
return c.json({ ok: true });
|
|
1765
1828
|
} catch (err) {
|
|
@@ -1769,7 +1832,7 @@ function plansRoute(pool2) {
|
|
|
1769
1832
|
router.post("/:sessionId/tasks", async (c) => {
|
|
1770
1833
|
try {
|
|
1771
1834
|
const body = await jsonObject(c);
|
|
1772
|
-
const result = await
|
|
1835
|
+
const result = await pool.session(c.req.param("sessionId")).addTask(body);
|
|
1773
1836
|
if (!result) return refuse(c, 404, "Session not found", "session_not_found");
|
|
1774
1837
|
return c.json({ ok: true });
|
|
1775
1838
|
} catch (err) {
|
|
@@ -1778,7 +1841,7 @@ function plansRoute(pool2) {
|
|
|
1778
1841
|
});
|
|
1779
1842
|
router.post("/:sessionId/review/approve", async (c) => {
|
|
1780
1843
|
try {
|
|
1781
|
-
const session =
|
|
1844
|
+
const session = pool.session(c.req.param("sessionId"));
|
|
1782
1845
|
const plan = session.planState;
|
|
1783
1846
|
if (!plan) throw new core.NoPlanError("review");
|
|
1784
1847
|
const { message } = await runUserCommand(session, { command: "approve_review" });
|
|
@@ -1793,7 +1856,7 @@ function plansRoute(pool2) {
|
|
|
1793
1856
|
if (!goal) return c.json({ error: "goal is required" }, 400);
|
|
1794
1857
|
const ws = workspace || c.req.query("workspace") || process.cwd();
|
|
1795
1858
|
const runnerList = Array.isArray(runners) ? runners : runners ? [runners] : void 0;
|
|
1796
|
-
const plan = await
|
|
1859
|
+
const plan = await pool.startPlanning(c.req.param("sessionId"), goal, runnerList, ws, model, { allowInit });
|
|
1797
1860
|
return c.json({ plan: core.surfacePlan(plan) });
|
|
1798
1861
|
} catch (err) {
|
|
1799
1862
|
return failure(c, err, "converse start", { message: "Planning failed" });
|
|
@@ -1803,7 +1866,7 @@ function plansRoute(pool2) {
|
|
|
1803
1866
|
try {
|
|
1804
1867
|
const { message } = await jsonObject(c);
|
|
1805
1868
|
if (!message) return c.json({ error: "message is required" }, 400);
|
|
1806
|
-
const plan = await
|
|
1869
|
+
const plan = await pool.continuePlanning(c.req.param("sessionId"), message);
|
|
1807
1870
|
return c.json({ plan: core.surfacePlan(plan) });
|
|
1808
1871
|
} catch (err) {
|
|
1809
1872
|
return failure(c, err, "conversation message");
|
|
@@ -1813,7 +1876,7 @@ function plansRoute(pool2) {
|
|
|
1813
1876
|
try {
|
|
1814
1877
|
const { message } = await jsonObject(c);
|
|
1815
1878
|
if (!message) return c.json({ error: "message is required" }, 400);
|
|
1816
|
-
const plan = await
|
|
1879
|
+
const plan = await pool.forceSendPrompt(c.req.param("sessionId"), message);
|
|
1817
1880
|
return c.json({ plan: core.surfacePlan(plan) });
|
|
1818
1881
|
} catch (err) {
|
|
1819
1882
|
return failure(c, err, "conversation message");
|
|
@@ -1821,7 +1884,7 @@ function plansRoute(pool2) {
|
|
|
1821
1884
|
});
|
|
1822
1885
|
router.post("/:sessionId/converse/resend/cancel", (c) => {
|
|
1823
1886
|
try {
|
|
1824
|
-
const cancelled =
|
|
1887
|
+
const cancelled = pool.session(c.req.param("sessionId")).cancelPlannerResend();
|
|
1825
1888
|
return c.json({ cancelled });
|
|
1826
1889
|
} catch (err) {
|
|
1827
1890
|
return failure(c, err, "cancel resend", { fallback: 404, message: "Not found" });
|
|
@@ -1829,7 +1892,7 @@ function plansRoute(pool2) {
|
|
|
1829
1892
|
});
|
|
1830
1893
|
router.post("/:sessionId/conversation/fork", (c) => {
|
|
1831
1894
|
try {
|
|
1832
|
-
const fork =
|
|
1895
|
+
const fork = pool.forkConversation(c.req.param("sessionId"));
|
|
1833
1896
|
return c.json({ ...fork, plan: core.surfacePlan(fork.plan) });
|
|
1834
1897
|
} catch (err) {
|
|
1835
1898
|
return failure(c, err, "conversation edit");
|
|
@@ -1837,7 +1900,7 @@ function plansRoute(pool2) {
|
|
|
1837
1900
|
});
|
|
1838
1901
|
router.get("/:sessionId/conversation/rewind-targets", (c) => {
|
|
1839
1902
|
try {
|
|
1840
|
-
return c.json({ targets:
|
|
1903
|
+
return c.json({ targets: pool.session(c.req.param("sessionId")).rewindTargets() });
|
|
1841
1904
|
} catch (err) {
|
|
1842
1905
|
return failure(c, err, "conversation edit");
|
|
1843
1906
|
}
|
|
@@ -1846,7 +1909,7 @@ function plansRoute(pool2) {
|
|
|
1846
1909
|
try {
|
|
1847
1910
|
const { index } = await jsonObject(c);
|
|
1848
1911
|
if (typeof index !== "number" || !Number.isInteger(index) || index < 0) return c.json({ error: "index must be a non-negative integer" }, 400);
|
|
1849
|
-
const rewound =
|
|
1912
|
+
const rewound = pool.rewindConversation(c.req.param("sessionId"), index);
|
|
1850
1913
|
return c.json({ ...rewound, plan: core.surfacePlan(rewound.plan) });
|
|
1851
1914
|
} catch (err) {
|
|
1852
1915
|
return failure(c, err, "conversation edit");
|
|
@@ -1854,7 +1917,7 @@ function plansRoute(pool2) {
|
|
|
1854
1917
|
});
|
|
1855
1918
|
router.post("/:sessionId/conversation/compact", async (c) => {
|
|
1856
1919
|
try {
|
|
1857
|
-
const compacted = await
|
|
1920
|
+
const compacted = await pool.compactConversation(c.req.param("sessionId"));
|
|
1858
1921
|
return c.json({ ...compacted, plan: core.surfacePlan(compacted.plan) });
|
|
1859
1922
|
} catch (err) {
|
|
1860
1923
|
return failure(c, err, "conversation edit");
|
|
@@ -1862,26 +1925,26 @@ function plansRoute(pool2) {
|
|
|
1862
1925
|
});
|
|
1863
1926
|
router.post("/:sessionId/conversation/compact/retry", async (c) => {
|
|
1864
1927
|
try {
|
|
1865
|
-
const plan = await
|
|
1928
|
+
const plan = await pool.retryPlannerCompaction(c.req.param("sessionId"));
|
|
1866
1929
|
return c.json({ plan: core.surfacePlan(plan) });
|
|
1867
1930
|
} catch (err) {
|
|
1868
1931
|
return failure(c, err, "retry compaction");
|
|
1869
1932
|
}
|
|
1870
1933
|
});
|
|
1871
1934
|
router.get("/:sessionId/prd", (c) => {
|
|
1872
|
-
const plan =
|
|
1935
|
+
const plan = pool.getPlan(c.req.param("sessionId"));
|
|
1873
1936
|
if (!plan?.prdMarkdown) return refuse(c, 404, "No PRD found");
|
|
1874
1937
|
return c.json({ prdMarkdown: plan.prdMarkdown });
|
|
1875
1938
|
});
|
|
1876
1939
|
return router;
|
|
1877
1940
|
}
|
|
1878
|
-
function runnersRoute(
|
|
1941
|
+
function runnersRoute(pool) {
|
|
1879
1942
|
const router = new hono.Hono();
|
|
1880
1943
|
router.get("/", async (c) => {
|
|
1881
1944
|
const workspace = c.req.query("workspace");
|
|
1882
|
-
const runners = await
|
|
1883
|
-
await
|
|
1884
|
-
const state =
|
|
1945
|
+
const runners = await pool.getInstalledRunners(workspace);
|
|
1946
|
+
await pool.discoverPlannerCatalogs(void 0, workspace);
|
|
1947
|
+
const state = pool.getRunnerState(workspace);
|
|
1885
1948
|
return c.json({
|
|
1886
1949
|
runners,
|
|
1887
1950
|
orchestratorModel: state.orchestratorModel
|
|
@@ -1893,7 +1956,7 @@ function runnersRoute(pool2) {
|
|
|
1893
1956
|
const id = c.req.param("id");
|
|
1894
1957
|
const { enabled } = await jsonObject(c);
|
|
1895
1958
|
if (typeof enabled !== "boolean") return refuse(c, 400, "enabled must be true or false");
|
|
1896
|
-
|
|
1959
|
+
pool.setRunnerEnabled(id, enabled);
|
|
1897
1960
|
return c.json({ ok: true });
|
|
1898
1961
|
} catch (err) {
|
|
1899
1962
|
return failure(c, err, "runner toggle", { fallback: 400 });
|
|
@@ -1901,21 +1964,21 @@ function runnersRoute(pool2) {
|
|
|
1901
1964
|
});
|
|
1902
1965
|
return router;
|
|
1903
1966
|
}
|
|
1904
|
-
function modelsRoute(
|
|
1967
|
+
function modelsRoute(pool) {
|
|
1905
1968
|
const router = new hono.Hono();
|
|
1906
1969
|
router.get("/", async (c) => {
|
|
1907
|
-
const result = await
|
|
1970
|
+
const result = await pool.getProviderModels(c.req.query("workspace"));
|
|
1908
1971
|
return c.json(result);
|
|
1909
1972
|
});
|
|
1910
1973
|
return router;
|
|
1911
1974
|
}
|
|
1912
|
-
function workspacesRoute(
|
|
1975
|
+
function workspacesRoute(pool) {
|
|
1913
1976
|
const router = new hono.Hono();
|
|
1914
1977
|
router.get("/isolation-preview", async (c) => {
|
|
1915
1978
|
try {
|
|
1916
1979
|
const workspace = c.req.query("workspace") || process.cwd();
|
|
1917
1980
|
core.assertWorkspaceExists(workspace);
|
|
1918
|
-
const preview = await
|
|
1981
|
+
const preview = await pool.previewIsolation(workspace);
|
|
1919
1982
|
return c.json({ preview });
|
|
1920
1983
|
} catch (err) {
|
|
1921
1984
|
return failure(c, err, "read isolation preview", { message: "Could not read the repositories" });
|
|
@@ -1935,30 +1998,30 @@ function answerOf(body) {
|
|
|
1935
1998
|
if (decision === "deny" && typeof note === "string" && note.trim()) return { decision, note: note.trim() };
|
|
1936
1999
|
return { decision: "deny" };
|
|
1937
2000
|
}
|
|
1938
|
-
function approvalsRoute(
|
|
2001
|
+
function approvalsRoute(pool) {
|
|
1939
2002
|
const router = new hono.Hono();
|
|
1940
2003
|
router.get("/:sessionId", (c) => {
|
|
1941
2004
|
const sessionId = c.req.param("sessionId");
|
|
1942
|
-
if (!
|
|
2005
|
+
if (!pool.hasSession(sessionId)) return refuse(c, 404, `Unknown session: ${sessionId}`, "session_not_found");
|
|
1943
2006
|
return c.json({
|
|
1944
|
-
pending:
|
|
1945
|
-
approvedScopes:
|
|
2007
|
+
pending: pool.outstandingApprovals(sessionId),
|
|
2008
|
+
approvedScopes: pool.approvedScopes(sessionId)
|
|
1946
2009
|
});
|
|
1947
2010
|
});
|
|
1948
2011
|
router.post("/:sessionId/:approvalId", async (c) => {
|
|
1949
2012
|
const sessionId = c.req.param("sessionId");
|
|
1950
2013
|
const approvalId = c.req.param("approvalId");
|
|
1951
|
-
if (!
|
|
2014
|
+
if (!pool.hasSession(sessionId)) return refuse(c, 404, `Unknown session: ${sessionId}`, "session_not_found");
|
|
1952
2015
|
const answer = answerOf(await c.req.json().catch(() => null));
|
|
1953
2016
|
const decided = core.toApprovalDecision(answer);
|
|
1954
|
-
const request =
|
|
2017
|
+
const request = pool.outstandingApprovals(sessionId).find((pending) => pending.id === approvalId)?.request;
|
|
1955
2018
|
if (request?.taskId) {
|
|
1956
|
-
const outcome = await
|
|
2019
|
+
const outcome = await pool.session(sessionId).execute(
|
|
1957
2020
|
{ command: "answer_approval", task: request.taskId, approval: approvalId, answer: decided.decision, ...decided.decision === "deny" && decided.note ? { note: decided.note } : {} },
|
|
1958
2021
|
{ kind: "user" }
|
|
1959
2022
|
);
|
|
1960
2023
|
if (!outcome.ok) return refuse(c, 400, outcome.message, "refused");
|
|
1961
|
-
} else if (!
|
|
2024
|
+
} else if (!pool.resolveApproval(sessionId, approvalId, answer)) {
|
|
1962
2025
|
return refuse(c, 409, "Approval is no longer outstanding: it timed out or was already answered.", "approval_not_outstanding");
|
|
1963
2026
|
}
|
|
1964
2027
|
return c.json({ ok: true, granted: core.isGranted(decided), decision: decided.decision });
|
|
@@ -1972,7 +2035,7 @@ function containerChange(body) {
|
|
|
1972
2035
|
if (typeof change.image === "string") return { image: change.image };
|
|
1973
2036
|
return "login" in change ? { login: change.login } : null;
|
|
1974
2037
|
}
|
|
1975
|
-
function settingsRoute(
|
|
2038
|
+
function settingsRoute(pool) {
|
|
1976
2039
|
const router = new hono.Hono();
|
|
1977
2040
|
router.use("/containers", async (c, next) => {
|
|
1978
2041
|
if (!core.containersOffered()) return refuse(c, 404, "This build does not offer containers");
|
|
@@ -1980,7 +2043,7 @@ function settingsRoute(pool2) {
|
|
|
1980
2043
|
});
|
|
1981
2044
|
router.get("/containers", async (c) => {
|
|
1982
2045
|
try {
|
|
1983
|
-
const status = await
|
|
2046
|
+
const status = await pool.containerStatus(c.req.query("prove") === "1", c.req.query("workspace") || void 0);
|
|
1984
2047
|
return c.json(status);
|
|
1985
2048
|
} catch (err) {
|
|
1986
2049
|
return failure(c, err, "containers status");
|
|
@@ -1992,7 +2055,7 @@ function settingsRoute(pool2) {
|
|
|
1992
2055
|
const change = containerChange(body);
|
|
1993
2056
|
if (!change) return refuse(c, 400, "One of on, image or login is required");
|
|
1994
2057
|
const { workspace } = body;
|
|
1995
|
-
const result = await
|
|
2058
|
+
const result = await pool.changeContainers(change, typeof workspace === "string" ? workspace || void 0 : void 0);
|
|
1996
2059
|
return c.json(result);
|
|
1997
2060
|
} catch (err) {
|
|
1998
2061
|
return failure(c, err, "containers change", { fallback: 400 });
|
|
@@ -2002,8 +2065,8 @@ function settingsRoute(pool2) {
|
|
|
2002
2065
|
await core.installedPlannerDetected();
|
|
2003
2066
|
const sessionId = c.req.query("sessionId") || void 0;
|
|
2004
2067
|
const workspace = c.req.query("workspace") || void 0;
|
|
2005
|
-
await
|
|
2006
|
-
const settings =
|
|
2068
|
+
await pool.discoverPlannerCatalogs(sessionId, workspace);
|
|
2069
|
+
const settings = pool.getSettings(sessionId, workspace);
|
|
2007
2070
|
return c.json(settings);
|
|
2008
2071
|
});
|
|
2009
2072
|
router.patch("/", async (c) => {
|
|
@@ -2021,13 +2084,13 @@ function settingsRoute(pool2) {
|
|
|
2021
2084
|
if (run2 !== void 0) {
|
|
2022
2085
|
if (!run2 || typeof run2 !== "object" || Array.isArray(run2)) return refuse(c, 400, "run must be an object of run settings");
|
|
2023
2086
|
try {
|
|
2024
|
-
|
|
2087
|
+
pool.updateRunSettings(run2, sessionId, workspace);
|
|
2025
2088
|
} catch (err) {
|
|
2026
2089
|
if (err instanceof core.RunSettingsError) return refuse(c, 400, err.message);
|
|
2027
2090
|
throw err;
|
|
2028
2091
|
}
|
|
2029
2092
|
}
|
|
2030
|
-
const result = Object.keys(rest).length > 0 ? await
|
|
2093
|
+
const result = Object.keys(rest).length > 0 ? await pool.writeSettings(rest, sessionId, workspace) : pool.getSettings(sessionId, workspace);
|
|
2031
2094
|
return c.json(result);
|
|
2032
2095
|
} catch (err) {
|
|
2033
2096
|
return failure(c, err, "settings update", { fallback: 400 });
|
|
@@ -2047,17 +2110,226 @@ function commandsRoute() {
|
|
|
2047
2110
|
});
|
|
2048
2111
|
return router;
|
|
2049
2112
|
}
|
|
2113
|
+
var SCOPES = ["checkout", "integration", "task"];
|
|
2114
|
+
function workspaceOf(c) {
|
|
2115
|
+
const workspace = c.req.query("workspace") || process.cwd();
|
|
2116
|
+
core.assertWorkspaceExists(workspace);
|
|
2117
|
+
return workspace;
|
|
2118
|
+
}
|
|
2119
|
+
function targetOf(c) {
|
|
2120
|
+
const scope = c.req.query("scope");
|
|
2121
|
+
if (scope !== void 0 && !SCOPES.includes(scope)) {
|
|
2122
|
+
throw new core.WorkspaceReadError("refused", `Unknown scope "${scope}": name ${SCOPES.join(", ")}.`);
|
|
2123
|
+
}
|
|
2124
|
+
const target = { workspace: workspaceOf(c) };
|
|
2125
|
+
const repo = c.req.query("repo");
|
|
2126
|
+
const sessionId = c.req.query("sessionId");
|
|
2127
|
+
const taskId = c.req.query("taskId");
|
|
2128
|
+
if (repo) target.repo = repo;
|
|
2129
|
+
if (scope) target.scope = scope;
|
|
2130
|
+
if (sessionId) target.sessionId = sessionId;
|
|
2131
|
+
if (taskId) target.taskId = taskId;
|
|
2132
|
+
return target;
|
|
2133
|
+
}
|
|
2134
|
+
function required(c, name) {
|
|
2135
|
+
const value = c.req.query(name);
|
|
2136
|
+
if (!value) throw new core.WorkspaceReadError("refused", `Name the ${name} to read.`);
|
|
2137
|
+
return value;
|
|
2138
|
+
}
|
|
2139
|
+
function flag(c, name) {
|
|
2140
|
+
const value = c.req.query(name);
|
|
2141
|
+
return value === "1" || value === "true";
|
|
2142
|
+
}
|
|
2143
|
+
function count(c, name) {
|
|
2144
|
+
const value = c.req.query(name);
|
|
2145
|
+
if (value === void 0) return void 0;
|
|
2146
|
+
const n = Number(value);
|
|
2147
|
+
if (!Number.isInteger(n) || n < 1) throw new core.WorkspaceReadError("refused", `"${name}" must be a positive whole number.`);
|
|
2148
|
+
return n;
|
|
2149
|
+
}
|
|
2150
|
+
function optional(c, name) {
|
|
2151
|
+
return c.req.query(name) || void 0;
|
|
2152
|
+
}
|
|
2153
|
+
function read(label, answer) {
|
|
2154
|
+
return async (c) => {
|
|
2155
|
+
try {
|
|
2156
|
+
return c.json(await answer(c));
|
|
2157
|
+
} catch (err) {
|
|
2158
|
+
return failure(c, err, label, { message: "Could not read the workspace" });
|
|
2159
|
+
}
|
|
2160
|
+
};
|
|
2161
|
+
}
|
|
2162
|
+
function filesRoute(reader) {
|
|
2163
|
+
const router = new hono.Hono();
|
|
2164
|
+
router.get("/tree", read("read tree", (c) => reader.tree(targetOf(c), c.req.query("dir") ?? "")));
|
|
2165
|
+
router.get("/content", read("read file", async (c) => {
|
|
2166
|
+
const target = targetOf(c);
|
|
2167
|
+
return reader.content(target, required(c, "path"));
|
|
2168
|
+
}));
|
|
2169
|
+
router.get("/find", read("find files", async (c) => {
|
|
2170
|
+
const target = targetOf(c);
|
|
2171
|
+
return reader.find(target, { query: required(c, "query"), text: flag(c, "text"), limit: count(c, "limit") });
|
|
2172
|
+
}));
|
|
2173
|
+
return router;
|
|
2174
|
+
}
|
|
2175
|
+
function gitRoute(reader) {
|
|
2176
|
+
const router = new hono.Hono();
|
|
2177
|
+
router.get("/repos", read("read repositories", (c) => reader.repos(workspaceOf(c))));
|
|
2178
|
+
router.get("/status", read("read status", (c) => reader.status(targetOf(c))));
|
|
2179
|
+
router.get("/diff", read("read diff", async (c) => {
|
|
2180
|
+
const target = targetOf(c);
|
|
2181
|
+
const kind = c.req.query("kind");
|
|
2182
|
+
if (kind !== void 0 && kind !== "staged" && kind !== "unstaged") throw new core.WorkspaceReadError("refused", `Unknown diff kind "${kind}": name staged or unstaged.`);
|
|
2183
|
+
return reader.diff(target, { kind, from: optional(c, "from"), to: optional(c, "to"), path: optional(c, "path") });
|
|
2184
|
+
}));
|
|
2185
|
+
router.get("/log", read("read log", async (c) => {
|
|
2186
|
+
const target = targetOf(c);
|
|
2187
|
+
return reader.log(target, { ref: optional(c, "ref"), cursor: optional(c, "cursor"), limit: count(c, "limit") });
|
|
2188
|
+
}));
|
|
2189
|
+
router.get("/commit", read("read commit", async (c) => {
|
|
2190
|
+
const target = targetOf(c);
|
|
2191
|
+
return reader.commit(target, required(c, "sha"), optional(c, "path"));
|
|
2192
|
+
}));
|
|
2193
|
+
router.get("/branches", read("read branches", (c) => reader.branches(targetOf(c))));
|
|
2194
|
+
return router;
|
|
2195
|
+
}
|
|
2196
|
+
function taskDiffRoute(reader) {
|
|
2197
|
+
const router = new hono.Hono();
|
|
2198
|
+
router.get("/:sessionId/tasks/:taskId/diff", read("read task diff", (c) => reader.taskDiff(workspaceOf(c), c.req.param("sessionId") ?? "", c.req.param("taskId") ?? "", {
|
|
2199
|
+
repo: optional(c, "repo"),
|
|
2200
|
+
path: optional(c, "path"),
|
|
2201
|
+
names: flag(c, "names")
|
|
2202
|
+
})));
|
|
2203
|
+
return router;
|
|
2204
|
+
}
|
|
2205
|
+
function bad(message) {
|
|
2206
|
+
throw new InvalidBodyError(message);
|
|
2207
|
+
}
|
|
2208
|
+
function text(body, name) {
|
|
2209
|
+
const value = body[name];
|
|
2210
|
+
if (typeof value !== "string") bad(`"${name}" must be a string.`);
|
|
2211
|
+
return value;
|
|
2212
|
+
}
|
|
2213
|
+
function optionalText(body, name) {
|
|
2214
|
+
return body[name] === void 0 ? void 0 : text(body, name);
|
|
2215
|
+
}
|
|
2216
|
+
function texts(body, name) {
|
|
2217
|
+
const value = body[name];
|
|
2218
|
+
if (value === void 0) return void 0;
|
|
2219
|
+
if (!Array.isArray(value) || !value.every((v) => typeof v === "string")) bad(`"${name}" must be a list of strings.`);
|
|
2220
|
+
return value;
|
|
2221
|
+
}
|
|
2222
|
+
function yes(body, name) {
|
|
2223
|
+
const value = body[name];
|
|
2224
|
+
if (value === void 0) return void 0;
|
|
2225
|
+
if (typeof value !== "boolean") bad(`"${name}" must be true or false.`);
|
|
2226
|
+
return value;
|
|
2227
|
+
}
|
|
2228
|
+
function confirmOf(body) {
|
|
2229
|
+
const value = body.confirm;
|
|
2230
|
+
if (value === void 0) return {};
|
|
2231
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) bad('"confirm" must be an object.');
|
|
2232
|
+
return value;
|
|
2233
|
+
}
|
|
2234
|
+
function scopeOf(body) {
|
|
2235
|
+
const workspace = optionalText(body, "workspace") || process.cwd();
|
|
2236
|
+
core.assertWorkspaceExists(workspace);
|
|
2237
|
+
const repo = optionalText(body, "repo");
|
|
2238
|
+
return repo ? { workspace, repo } : { workspace };
|
|
2239
|
+
}
|
|
2240
|
+
function changes(body) {
|
|
2241
|
+
const paths = texts(body, "paths");
|
|
2242
|
+
const hunks = optionalText(body, "hunks");
|
|
2243
|
+
if (paths === void 0 === (hunks === void 0)) bad('Name either "paths" or "hunks".');
|
|
2244
|
+
return hunks !== void 0 ? { hunks } : { paths };
|
|
2245
|
+
}
|
|
2246
|
+
function act(label, action) {
|
|
2247
|
+
return async (c) => {
|
|
2248
|
+
try {
|
|
2249
|
+
const body = await jsonObject(c);
|
|
2250
|
+
return c.json(await action(scopeOf(body), body));
|
|
2251
|
+
} catch (err) {
|
|
2252
|
+
return failure(c, err, label, { message: "Could not act on the repository" });
|
|
2253
|
+
}
|
|
2254
|
+
};
|
|
2255
|
+
}
|
|
2256
|
+
function fileWriteRoute(git) {
|
|
2257
|
+
const router = new hono.Hono();
|
|
2258
|
+
router.put("/content", act("save file", (scope, body) => {
|
|
2259
|
+
const baseHash = body.baseHash === null ? null : text(body, "baseHash");
|
|
2260
|
+
return git.save(scope, { path: text(body, "path"), text: text(body, "text"), baseHash });
|
|
2261
|
+
}));
|
|
2262
|
+
return router;
|
|
2263
|
+
}
|
|
2264
|
+
function gitWriteRoute(git) {
|
|
2265
|
+
const router = new hono.Hono();
|
|
2266
|
+
router.post("/stage", act("stage", (scope, body) => git.stage(scope, changes(body))));
|
|
2267
|
+
router.post("/unstage", act("unstage", (scope, body) => git.unstage(scope, changes(body))));
|
|
2268
|
+
router.post("/commit", act("commit", (scope, body) => {
|
|
2269
|
+
const amend = yes(body, "amend");
|
|
2270
|
+
const head = confirmOf(body).head;
|
|
2271
|
+
return git.commit(scope, {
|
|
2272
|
+
message: text(body, "message"),
|
|
2273
|
+
...amend !== void 0 ? { amend } : {},
|
|
2274
|
+
...typeof head === "string" ? { confirm: { head } } : {}
|
|
2275
|
+
});
|
|
2276
|
+
}));
|
|
2277
|
+
router.post("/discard", act("discard", (scope, body) => {
|
|
2278
|
+
const files = texts(confirmOf(body), "files") ?? [];
|
|
2279
|
+
return git.discard(scope, { ...changes(body), confirm: { files } });
|
|
2280
|
+
}));
|
|
2281
|
+
router.post("/branch", act("create branch", (scope, body) => {
|
|
2282
|
+
const from = optionalText(body, "from");
|
|
2283
|
+
const switching = yes(body, "switch");
|
|
2284
|
+
return git.createBranch(scope, { name: text(body, "name"), ...from !== void 0 ? { from } : {}, ...switching !== void 0 ? { switch: switching } : {} });
|
|
2285
|
+
}));
|
|
2286
|
+
router.post("/switch", act("switch branch", (scope, body) => git.switchBranch(scope, { branch: text(body, "branch") })));
|
|
2287
|
+
router.post("/branch-delete", act("delete branch", (scope, body) => {
|
|
2288
|
+
const confirm = confirmOf(body);
|
|
2289
|
+
const shown = typeof confirm.branch === "string" ? confirm.branch : "";
|
|
2290
|
+
if (typeof confirm.unmerged !== "boolean") bad('"confirm.unmerged" must say whether the branch was shown as unmerged.');
|
|
2291
|
+
return git.deleteBranch(scope, { branch: text(body, "branch"), confirm: { branch: shown, unmerged: confirm.unmerged } });
|
|
2292
|
+
}));
|
|
2293
|
+
router.post("/stash", act("stash", (scope, body) => {
|
|
2294
|
+
const paths = texts(body, "paths");
|
|
2295
|
+
const message = optionalText(body, "message");
|
|
2296
|
+
return git.stash(scope, { ...paths !== void 0 ? { paths } : {}, ...message !== void 0 ? { message } : {} });
|
|
2297
|
+
}));
|
|
2298
|
+
router.post("/stash-pop", act("pop stash", (scope, body) => git.stashPop(scope, { stash: text(body, "stash") })));
|
|
2299
|
+
router.post("/stash-drop", act("drop stash", (scope, body) => git.stashDrop(scope, { stash: text(body, "stash") })));
|
|
2300
|
+
router.post("/fetch", act("fetch", (scope) => git.fetch(scope)));
|
|
2301
|
+
const remote = (body) => {
|
|
2302
|
+
const confirm = confirmOf(body);
|
|
2303
|
+
return { remote: typeof confirm.remote === "string" ? confirm.remote : "", branch: typeof confirm.branch === "string" ? confirm.branch : "" };
|
|
2304
|
+
};
|
|
2305
|
+
router.post("/pull", act("pull", (scope, body) => git.pull(scope, { confirm: remote(body) })));
|
|
2306
|
+
router.post("/push", act("push", (scope, body) => git.push(scope, { confirm: remote(body) })));
|
|
2307
|
+
return router;
|
|
2308
|
+
}
|
|
2309
|
+
function mergePreflightRoute(reader) {
|
|
2310
|
+
const router = new hono.Hono();
|
|
2311
|
+
router.get("/:sessionId/isolation/merge-preflight", async (c) => {
|
|
2312
|
+
try {
|
|
2313
|
+
const workspace = c.req.query("workspace") || process.cwd();
|
|
2314
|
+
core.assertWorkspaceExists(workspace);
|
|
2315
|
+
return c.json(await reader.mergePreflight(workspace, c.req.param("sessionId") ?? ""));
|
|
2316
|
+
} catch (err) {
|
|
2317
|
+
return failure(c, err, "merge preflight", { message: "Could not read the run" });
|
|
2318
|
+
}
|
|
2319
|
+
});
|
|
2320
|
+
return router;
|
|
2321
|
+
}
|
|
2050
2322
|
var CHILD_PATH = /^\/ws\/session\/([^/]+)\/child\/([^/]+)$/;
|
|
2051
|
-
function joinChild(
|
|
2052
|
-
const leave = () =>
|
|
2323
|
+
function joinChild(pool, ws, rootId, childId) {
|
|
2324
|
+
const leave = () => pool.unwatchChild(rootId, childId, ws);
|
|
2053
2325
|
ws.on("close", leave);
|
|
2054
2326
|
ws.on("error", leave);
|
|
2055
2327
|
const refuse3 = () => ws.close(4404, "No such child plan in this tree");
|
|
2056
|
-
void
|
|
2328
|
+
void pool.watchChild(rootId, childId, ws).then((watching) => {
|
|
2057
2329
|
if (!watching) refuse3();
|
|
2058
2330
|
}, refuse3);
|
|
2059
2331
|
}
|
|
2060
|
-
function createWsHandler(
|
|
2332
|
+
function createWsHandler(pool) {
|
|
2061
2333
|
const wss = new ws.WebSocketServer({
|
|
2062
2334
|
noServer: true,
|
|
2063
2335
|
// Select the plain subprotocol explicitly. Left to itself `ws` echoes back
|
|
@@ -2069,32 +2341,32 @@ function createWsHandler(pool2) {
|
|
|
2069
2341
|
const url = new URL(request.url ?? "", `http://${request.headers.host}`);
|
|
2070
2342
|
const child = CHILD_PATH.exec(url.pathname);
|
|
2071
2343
|
if (child) {
|
|
2072
|
-
joinChild(
|
|
2344
|
+
joinChild(pool, ws, child[1], child[2]);
|
|
2073
2345
|
return;
|
|
2074
2346
|
}
|
|
2075
2347
|
const sessionId = url.pathname.split("/").pop() || "";
|
|
2076
2348
|
ws.on("close", () => {
|
|
2077
|
-
|
|
2349
|
+
pool.unsubscribe(sessionId, ws);
|
|
2078
2350
|
});
|
|
2079
2351
|
ws.on("error", () => {
|
|
2080
|
-
|
|
2352
|
+
pool.unsubscribe(sessionId, ws);
|
|
2081
2353
|
});
|
|
2082
2354
|
ws.send(JSON.stringify({ type: "connected", sessionId }));
|
|
2083
|
-
const view =
|
|
2355
|
+
const view = pool.runSnapshot(sessionId);
|
|
2084
2356
|
if (view) {
|
|
2085
2357
|
ws.send(JSON.stringify({
|
|
2086
2358
|
type: "run_snapshot",
|
|
2087
2359
|
sessionId,
|
|
2088
2360
|
view,
|
|
2089
|
-
approvals:
|
|
2090
|
-
...
|
|
2361
|
+
approvals: pool.announcedApprovals(sessionId),
|
|
2362
|
+
...pool.streamPosition(sessionId)
|
|
2091
2363
|
}));
|
|
2092
2364
|
}
|
|
2093
|
-
const compaction =
|
|
2365
|
+
const compaction = pool.plannerCompaction(sessionId);
|
|
2094
2366
|
if (compaction) ws.send(JSON.stringify(compaction));
|
|
2095
|
-
const detection =
|
|
2367
|
+
const detection = pool.plannerDetection(sessionId);
|
|
2096
2368
|
if (detection?.waiting) ws.send(JSON.stringify(detection));
|
|
2097
|
-
|
|
2369
|
+
pool.subscribe(sessionId, ws);
|
|
2098
2370
|
});
|
|
2099
2371
|
return (request, socket, head) => {
|
|
2100
2372
|
wss.handleUpgrade(request, socket, head, (ws) => {
|
|
@@ -2128,12 +2400,12 @@ function decideAdmission(headers, context) {
|
|
|
2128
2400
|
}
|
|
2129
2401
|
return ADMIT;
|
|
2130
2402
|
}
|
|
2131
|
-
function isOwnLoopbackAuthority(host,
|
|
2403
|
+
function isOwnLoopbackAuthority(host, port) {
|
|
2132
2404
|
if (!host) return false;
|
|
2133
2405
|
const authority = splitAuthority(host);
|
|
2134
2406
|
if (!authority) return false;
|
|
2135
2407
|
if (!LOOPBACK_HOSTNAMES.has(authority.hostname.toLowerCase())) return false;
|
|
2136
|
-
return authority.port ===
|
|
2408
|
+
return authority.port === port;
|
|
2137
2409
|
}
|
|
2138
2410
|
var DEFAULT_HTTP_PORT = 80;
|
|
2139
2411
|
function splitAuthority(host) {
|
|
@@ -2153,13 +2425,46 @@ function splitAuthority(host) {
|
|
|
2153
2425
|
if (value.indexOf(":", colon + 1) !== -1) return void 0;
|
|
2154
2426
|
return withPort(value.slice(0, colon), value.slice(colon + 1));
|
|
2155
2427
|
}
|
|
2156
|
-
function withPort(hostname,
|
|
2157
|
-
if (!/^\d+$/.test(
|
|
2158
|
-
return { hostname, port: Number(
|
|
2428
|
+
function withPort(hostname, port) {
|
|
2429
|
+
if (!/^\d+$/.test(port)) return void 0;
|
|
2430
|
+
return { hostname, port: Number(port) };
|
|
2431
|
+
}
|
|
2432
|
+
var cached;
|
|
2433
|
+
function serverVersion(from = __dirname, sea = {}) {
|
|
2434
|
+
const embedded = embeddedVersion(sea);
|
|
2435
|
+
if (embedded) return embedded;
|
|
2436
|
+
if (cached !== void 0) return cached;
|
|
2437
|
+
cached = findVersion(from);
|
|
2438
|
+
return cached;
|
|
2439
|
+
}
|
|
2440
|
+
function embeddedVersion(sea) {
|
|
2441
|
+
try {
|
|
2442
|
+
return core.seaVersion(sea);
|
|
2443
|
+
} catch {
|
|
2444
|
+
return "unknown";
|
|
2445
|
+
}
|
|
2446
|
+
}
|
|
2447
|
+
function findVersion(from) {
|
|
2448
|
+
for (let dir = from; ; dir = path.dirname(dir)) {
|
|
2449
|
+
try {
|
|
2450
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(dir, "package.json"), "utf8"));
|
|
2451
|
+
const { name, version } = pkg;
|
|
2452
|
+
if (name === "@ordewell/web" && typeof version === "string" && version) return version;
|
|
2453
|
+
} catch {
|
|
2454
|
+
}
|
|
2455
|
+
if (path.dirname(dir) === dir) return "unknown";
|
|
2456
|
+
}
|
|
2159
2457
|
}
|
|
2160
2458
|
|
|
2161
2459
|
// server/app.ts
|
|
2162
|
-
function
|
|
2460
|
+
function userGitFor(pool, reader) {
|
|
2461
|
+
return core.createUserGit({
|
|
2462
|
+
reader,
|
|
2463
|
+
landingRuns: (checkout) => core.landingRuns(checkout, (workspace, sessionId, plan) => pool.userRunIsLive(sessionId, workspace, plan)),
|
|
2464
|
+
record: (workspace, held, text2) => pool.recordUserAction(held.sessionId, workspace, text2)
|
|
2465
|
+
});
|
|
2466
|
+
}
|
|
2467
|
+
function createApp(pool, admission, reader = core.createWorkspaceReader({ config: new WebConfig() }), userGit = userGitFor(pool, reader)) {
|
|
2163
2468
|
const app = new hono.Hono();
|
|
2164
2469
|
app.use("*", async (c, next) => {
|
|
2165
2470
|
const decision = decideAdmission({
|
|
@@ -2173,21 +2478,33 @@ function createApp(pool2, admission) {
|
|
|
2173
2478
|
if (!decision.admit) return c.json({ error: decision.reason }, decision.status);
|
|
2174
2479
|
await next();
|
|
2175
2480
|
});
|
|
2176
|
-
app.get("/api/server", (c) => c.json({
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2481
|
+
app.get("/api/server", (c) => c.json({
|
|
2482
|
+
pid: process.pid,
|
|
2483
|
+
port: admission.port,
|
|
2484
|
+
version: serverVersion(),
|
|
2485
|
+
contract: core.DAEMON_CONTRACT,
|
|
2486
|
+
live: pool.workLive()
|
|
2487
|
+
}));
|
|
2488
|
+
app.route("/api/sessions", sessionsRoute(pool));
|
|
2489
|
+
app.route("/api/sessions", childrenRoute(pool));
|
|
2490
|
+
app.route("/api/plans", plansRoute(pool));
|
|
2491
|
+
app.route("/api/plans", taskDiffRoute(reader));
|
|
2492
|
+
app.route("/api/plans", mergePreflightRoute(reader));
|
|
2493
|
+
app.route("/api/runners", runnersRoute(pool));
|
|
2494
|
+
app.route("/api/models", modelsRoute(pool));
|
|
2495
|
+
app.route("/api/settings", settingsRoute(pool));
|
|
2183
2496
|
app.route("/api/commands", commandsRoute());
|
|
2184
|
-
app.route("/api/workspaces", workspacesRoute(
|
|
2185
|
-
app.route("/api/approvals", approvalsRoute(
|
|
2497
|
+
app.route("/api/workspaces", workspacesRoute(pool));
|
|
2498
|
+
app.route("/api/approvals", approvalsRoute(pool));
|
|
2499
|
+
app.route("/api/files", filesRoute(reader));
|
|
2500
|
+
app.route("/api/git", gitRoute(reader));
|
|
2501
|
+
app.route("/api/files", fileWriteRoute(userGit));
|
|
2502
|
+
app.route("/api/git", gitWriteRoute(userGit));
|
|
2186
2503
|
return app;
|
|
2187
2504
|
}
|
|
2188
|
-
function attachWsHandler(
|
|
2189
|
-
const handler = createWsHandler(
|
|
2190
|
-
|
|
2505
|
+
function attachWsHandler(server, pool, admission) {
|
|
2506
|
+
const handler = createWsHandler(pool);
|
|
2507
|
+
server.on("upgrade", (request, socket, head) => {
|
|
2191
2508
|
const decision = decideAdmission({
|
|
2192
2509
|
origin: request.headers.origin ?? null,
|
|
2193
2510
|
host: request.headers.host ?? null,
|
|
@@ -2326,73 +2643,77 @@ function createRequestListener(app, admission, maxBodyBytes = MAX_BODY_BYTES) {
|
|
|
2326
2643
|
}
|
|
2327
2644
|
};
|
|
2328
2645
|
}
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
if (portIdx !== -1 && args[portIdx + 1]) {
|
|
2333
|
-
|
|
2334
|
-
}
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
core.processLeaseHost().describe({ kind: "daemon", port, ...watchParentPid ? { ownerPid: watchParentPid } : {} });
|
|
2341
|
-
|
|
2342
|
-
void pool.detectInstalledPlanner();
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
server.listen(port, "127.0.0.1", () => {
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
|
|
2352
|
-
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
|
|
2360
|
-
|
|
2361
|
-
|
|
2362
|
-
});
|
|
2363
|
-
function shutdown() {
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2646
|
+
function runDaemon(args) {
|
|
2647
|
+
let port = 3742;
|
|
2648
|
+
const portIdx = args.indexOf("--port");
|
|
2649
|
+
if (portIdx !== -1 && args[portIdx + 1]) {
|
|
2650
|
+
port = parseInt(args[portIdx + 1], 10) || 3742;
|
|
2651
|
+
}
|
|
2652
|
+
const watchParentIdx = args.indexOf("--watch-parent");
|
|
2653
|
+
const watchParentPid = watchParentIdx !== -1 ? parseInt(args[watchParentIdx + 1], 10) : void 0;
|
|
2654
|
+
const pidFileIdx = args.indexOf("--pid-file");
|
|
2655
|
+
const pidFile = pidFileIdx !== -1 ? args[pidFileIdx + 1] : void 0;
|
|
2656
|
+
let ownsPidFile = false;
|
|
2657
|
+
core.processLeaseHost().describe({ kind: "daemon", port, ...watchParentPid ? { ownerPid: watchParentPid } : {} });
|
|
2658
|
+
const pool = new OrchestratorPool({ runner: new core.StructuredRunner() });
|
|
2659
|
+
void pool.detectInstalledPlanner();
|
|
2660
|
+
const server = http.createServer();
|
|
2661
|
+
let ownsToken = false;
|
|
2662
|
+
server.listen(port, "127.0.0.1", () => {
|
|
2663
|
+
const { token, file: tokenFile } = core.mintDaemonToken(port);
|
|
2664
|
+
ownsToken = true;
|
|
2665
|
+
if (pidFile) {
|
|
2666
|
+
fs.mkdirSync(path.dirname(pidFile), { recursive: true });
|
|
2667
|
+
fs.writeFileSync(pidFile, String(process.pid), { mode: 384 });
|
|
2668
|
+
ownsPidFile = true;
|
|
2669
|
+
}
|
|
2670
|
+
const admission = { port, token, tokenFile };
|
|
2671
|
+
const app = createApp(pool, admission);
|
|
2672
|
+
app.get("/", (c) => c.json({ message: "Ordewell API", docs: "/api/workspaces" }));
|
|
2673
|
+
server.on("request", createRequestListener(app, admission));
|
|
2674
|
+
attachWsHandler(server, pool, admission);
|
|
2675
|
+
pool.getProviderModels().catch((err) => {
|
|
2676
|
+
console.error(`[web] model discovery warmup failed: ${err?.message ?? err}`);
|
|
2677
|
+
});
|
|
2678
|
+
console.error(`[web] Ordewell API server: http://localhost:${port}`);
|
|
2679
|
+
});
|
|
2680
|
+
function shutdown() {
|
|
2681
|
+
if (ownsToken) core.clearDaemonToken(port);
|
|
2682
|
+
if (ownsPidFile && pidFile) {
|
|
2683
|
+
try {
|
|
2684
|
+
fs.unlinkSync(pidFile);
|
|
2685
|
+
} catch {
|
|
2686
|
+
}
|
|
2369
2687
|
}
|
|
2688
|
+
pool.destroyAll();
|
|
2689
|
+
server.close();
|
|
2690
|
+
process.exit(0);
|
|
2691
|
+
}
|
|
2692
|
+
process.on("unhandledRejection", (reason) => {
|
|
2693
|
+
console.error(`[web] unhandled rejection: ${reason instanceof Error ? reason.stack ?? reason.message : String(reason)}`);
|
|
2694
|
+
});
|
|
2695
|
+
process.on("SIGINT", () => {
|
|
2696
|
+
if (!watchParentPid) shutdown();
|
|
2697
|
+
});
|
|
2698
|
+
process.on("SIGTERM", () => {
|
|
2699
|
+
shutdown();
|
|
2700
|
+
});
|
|
2701
|
+
process.on("SIGHUP", () => {
|
|
2702
|
+
shutdown();
|
|
2703
|
+
});
|
|
2704
|
+
if (watchParentPid) {
|
|
2705
|
+
const parentCheck = setInterval(() => {
|
|
2706
|
+
try {
|
|
2707
|
+
process.kill(watchParentPid, 0);
|
|
2708
|
+
} catch {
|
|
2709
|
+
clearInterval(parentCheck);
|
|
2710
|
+
shutdown();
|
|
2711
|
+
}
|
|
2712
|
+
}, 5e3);
|
|
2370
2713
|
}
|
|
2371
|
-
pool.destroyAll();
|
|
2372
|
-
server.close();
|
|
2373
|
-
process.exit(0);
|
|
2374
|
-
}
|
|
2375
|
-
process.on("unhandledRejection", (reason) => {
|
|
2376
|
-
console.error(`[web] unhandled rejection: ${reason instanceof Error ? reason.stack ?? reason.message : String(reason)}`);
|
|
2377
|
-
});
|
|
2378
|
-
process.on("SIGINT", () => {
|
|
2379
|
-
if (!watchParentPid) shutdown();
|
|
2380
|
-
});
|
|
2381
|
-
process.on("SIGTERM", () => {
|
|
2382
|
-
shutdown();
|
|
2383
|
-
});
|
|
2384
|
-
process.on("SIGHUP", () => {
|
|
2385
|
-
shutdown();
|
|
2386
|
-
});
|
|
2387
|
-
if (watchParentPid) {
|
|
2388
|
-
const parentCheck = setInterval(() => {
|
|
2389
|
-
try {
|
|
2390
|
-
process.kill(watchParentPid, 0);
|
|
2391
|
-
} catch {
|
|
2392
|
-
clearInterval(parentCheck);
|
|
2393
|
-
shutdown();
|
|
2394
|
-
}
|
|
2395
|
-
}, 5e3);
|
|
2396
2714
|
}
|
|
2715
|
+
|
|
2716
|
+
// server/main.ts
|
|
2717
|
+
runDaemon(process.argv.slice(2));
|
|
2397
2718
|
//# sourceMappingURL=main.js.map
|
|
2398
2719
|
//# sourceMappingURL=main.js.map
|