@ordewell/web 0.12.0 → 0.12.2

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.
@@ -181,7 +181,7 @@ function run(file, args2, opts) {
181
181
  // server/adapters/PoolFileSystem.ts
182
182
  var SEARCH_TIMEOUT_MS = 2e4;
183
183
  var BASH_TIMEOUT_MS = 12e4;
184
- var PoolFileSystem = class extends core.BaseFileSystem {
184
+ var PoolFileSystem = class _PoolFileSystem extends core.BaseFileSystem {
185
185
  constructor(workspaceRoot) {
186
186
  super();
187
187
  this.workspaceRoot = workspaceRoot;
@@ -190,6 +190,9 @@ var PoolFileSystem = class extends core.BaseFileSystem {
190
190
  getWorkspaceRoot() {
191
191
  return this.workspaceRoot;
192
192
  }
193
+ rootedAt(workspace) {
194
+ return new _PoolFileSystem(workspace);
195
+ }
193
196
  /**
194
197
  * PATH for the search subprocesses. `execFile` starts them with no shell, so
195
198
  * on a Windows box whose POSIX tools live in the Git tree (`grep.exe` beside
@@ -1169,6 +1172,20 @@ var OrchestratorPool = class {
1169
1172
  }
1170
1173
  this.clients.delete(sessionId);
1171
1174
  }
1175
+ /** Close a session for a surface that is done with it: it is dropped now, and this resolves once all it started has gone. */
1176
+ async close(sessionId) {
1177
+ const session = this.sessions.get(sessionId);
1178
+ this.destroy(sessionId, { notifyClients: true });
1179
+ await session?.gone;
1180
+ }
1181
+ /** Stop a session's whole tree, and wait until it has gone; the session stays open (ADR-0055 D16). */
1182
+ async stopTree(sessionId) {
1183
+ await this.sessions.get(sessionId)?.stopSubtree();
1184
+ }
1185
+ /** What a session's tree left in the engine, which only the `containers` setting says to look for (ADR-0059 L6). */
1186
+ reclaim(sessionId) {
1187
+ return core.reclaimContainers(sessionId, this.settingsService.getAll().containers);
1188
+ }
1172
1189
  destroyAll() {
1173
1190
  for (const id of this.sessions.keys()) this.destroy(id);
1174
1191
  }
@@ -1177,7 +1194,12 @@ var scanWorkspaces2 = scanWorkspaces;
1177
1194
  function getSessionList(workspace) {
1178
1195
  return core.listSessions(workspace);
1179
1196
  }
1180
- function removeSession(sessionId, workspace) {
1197
+ async function removeSession(sessionId, workspace, stop, close, reclaim) {
1198
+ await stop();
1199
+ const { refused } = await core.discardChildRuns(sessionId, workspace, core.createWorktreeIsolation({ config: new WebConfig() }));
1200
+ if (refused.length > 0) throw new Error(`The session was kept: ${refused.map(({ worktree, reason }) => `${worktree} (${reason})`).join(", ")}`);
1201
+ close();
1202
+ await reclaim();
1181
1203
  return core.deleteSession(sessionId, workspace);
1182
1204
  }
1183
1205
  function admittedEnv(changes) {
@@ -1305,20 +1327,24 @@ function sessionsRoute(pool2) {
1305
1327
  if (!Number.isInteger(attempt) || attempt < 1) return refuse(c, 400, "attempt must be a positive integer");
1306
1328
  return c.json({ attempt, events: core.taskLogForSurface(core.readTaskLog({ baseDir: ws, sessionId: c.req.param("id") }, c.req.param("taskId"), attempt)), position: pool2.streamPosition(c.req.param("id")) });
1307
1329
  });
1308
- router.delete("/:id", (c) => {
1330
+ router.delete("/:id", async (c) => {
1309
1331
  const ws = c.req.query("workspace") || process.cwd();
1310
1332
  const id = c.req.param("id");
1311
1333
  const held = pool2.hasSession(id);
1312
- pool2.destroy(id, { notifyClients: true });
1313
- const removed = removeSession(id, ws);
1334
+ let removed;
1335
+ try {
1336
+ removed = await removeSession(id, ws, () => pool2.stopTree(id), () => pool2.destroy(id, { notifyClients: true }), () => pool2.reclaim(id));
1337
+ } catch (err) {
1338
+ return c.json({ ok: false, message: err instanceof Error ? err.message : String(err) });
1339
+ }
1314
1340
  const ok = held || removed;
1315
1341
  return c.json({
1316
1342
  ok,
1317
1343
  ...ok ? {} : { message: `No session "${id}" was found in ${ws}.` }
1318
1344
  });
1319
1345
  });
1320
- router.post("/:id/close", (c) => {
1321
- pool2.destroy(c.req.param("id"), { notifyClients: true });
1346
+ router.post("/:id/close", async (c) => {
1347
+ await pool2.close(c.req.param("id"));
1322
1348
  return c.json({ ok: true });
1323
1349
  });
1324
1350
  return router;
@@ -1422,12 +1448,15 @@ function plansRoute(pool2) {
1422
1448
  for (const answer of ["approve", "reject"]) {
1423
1449
  router.post(`/:sessionId/tasks/:taskId/checkpoint/${answer}`, async (c) => {
1424
1450
  try {
1425
- const body = answer === "reject" ? await c.req.json().catch(() => ({})) : {};
1451
+ const body = await c.req.json().catch(() => ({}));
1426
1452
  const reason = typeof body === "object" && body !== null && "reason" in body && typeof body.reason === "string" ? body.reason.trim() : "";
1453
+ const raiseTokens = typeof body === "object" && body !== null && "raiseTokens" in body ? body.raiseTokens : void 0;
1454
+ const raise = typeof raiseTokens === "number" && Number.isSafeInteger(raiseTokens) && raiseTokens > 0 ? raiseTokens : void 0;
1455
+ if (raiseTokens !== void 0 && raise === void 0) return refuse(c, 400, "A raise is a whole number of tokens, 1 or more.", "refused");
1427
1456
  const session = pool2.session(c.req.param("sessionId"));
1428
1457
  const taskId = idOfRef(session, c.req.param("taskId"));
1429
1458
  if (!session.awaitsCheckpoint(taskId)) return refuse(c, 409, NO_CHECKPOINT, "checkpoint_not_waiting");
1430
- const { message } = await runUserCommand(session, { command: "answer_checkpoint", task: taskId, answer, ...reason ? { reason } : {} });
1459
+ const { message } = await runUserCommand(session, { command: "answer_checkpoint", task: taskId, answer, ...reason ? { reason } : {}, ...raise !== void 0 ? { raiseTokens: raise } : {} });
1431
1460
  return c.json({ ok: true, message });
1432
1461
  } catch (err) {
1433
1462
  return failure(c, err, "task edit");
@@ -1464,8 +1493,11 @@ function plansRoute(pool2) {
1464
1493
  });
1465
1494
  router.post("/:sessionId/isolation/merge", async (c) => {
1466
1495
  try {
1467
- const outcome = await pool2.session(c.req.param("sessionId")).execute({ command: "merge_all" }, { kind: "user" });
1496
+ const body = await c.req.json().catch(() => ({}));
1497
+ const pastChecks = typeof body === "object" && body !== null && "pastChecks" in body && body.pastChecks === true;
1498
+ const outcome = await pool2.session(c.req.param("sessionId")).execute(pastChecks ? { command: "merge_all", pastChecks: true } : { command: "merge_all" }, { kind: "user" });
1468
1499
  if (outcome.payload?.kind === "merge") return c.json(outcome.payload.result);
1500
+ if (outcome.payload?.kind === "merge_hold") return refuse(c, 409, outcome.message, "merge_past_check");
1469
1501
  if (!outcome.ok) throw new core.TaskControlError(outcome.message);
1470
1502
  throw new Error("Merge all answered without its result.");
1471
1503
  } catch (err) {