@ordewell/web 0.14.1 → 0.14.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.
@@ -117,6 +117,39 @@ var StreamSequence = class {
117
117
  return { stream: this.stream, seq: this.counts.get(key) ?? 0 };
118
118
  }
119
119
  };
120
+
121
+ // server/pool/heldOutput.ts
122
+ var SLOW_SOCKET_BYTES = 1024 * 1024;
123
+ var WINDOW_MS = 250;
124
+ var HeldOutput = class {
125
+ constructor(tell) {
126
+ this.tell = tell;
127
+ }
128
+ tell;
129
+ runs = /* @__PURE__ */ new Map();
130
+ /** Whether `message` is held: it joins the run it continues, or starts one when `behind` says a socket needs it. */
131
+ hold(sessionId, workspace, message, behind) {
132
+ const run2 = this.runs.get(sessionId);
133
+ if (run2 && run2.taskId === message.taskId && run2.workspace === workspace) {
134
+ run2.text += message.text;
135
+ return true;
136
+ }
137
+ if (run2) this.flush(sessionId);
138
+ if (!behind()) return false;
139
+ const timer = setTimeout(() => this.flush(sessionId), WINDOW_MS);
140
+ timer.unref?.();
141
+ this.runs.set(sessionId, { workspace, taskId: message.taskId, text: message.text, timer });
142
+ return true;
143
+ }
144
+ /** Tells the run, if any. Called before anything else of the session is, so nothing passes the output. */
145
+ flush(sessionId) {
146
+ const run2 = this.runs.get(sessionId);
147
+ if (!run2) return;
148
+ clearTimeout(run2.timer);
149
+ this.runs.delete(sessionId);
150
+ this.tell(sessionId, run2.workspace, { type: "task_output", taskId: run2.taskId, text: run2.text });
151
+ }
152
+ };
120
153
  var WebConfig = class extends core.BaseConfig {
121
154
  _enabledRunnersOverride;
122
155
  _providerModelLists;
@@ -467,6 +500,7 @@ ${err}` : out;
467
500
  };
468
501
 
469
502
  // server/adapters/PoolAwareRunner.ts
503
+ var OUTPUT_WINDOW_MS = 100;
470
504
  var PoolAwareRunner = class {
471
505
  constructor(sessionId, broadcast, inner) {
472
506
  this.sessionId = sessionId;
@@ -483,25 +517,60 @@ var PoolAwareRunner = class {
483
517
  * /stop kill another plan's tasks.
484
518
  */
485
519
  owned = /* @__PURE__ */ new Set();
520
+ /** Each live session's way to tell what it printed that no frame carries yet. */
521
+ flushers = /* @__PURE__ */ new Map();
486
522
  get activeCount() {
487
523
  return this.owned.size;
488
524
  }
489
525
  async spawn(opts) {
490
526
  const session = await this.inner.spawn({ ...opts, planSessionId: this.sessionId });
491
527
  this.owned.add(session.id);
492
- session.onExit(() => this.owned.delete(session.id));
493
- this.broadcast({ type: "task_started", taskId: opts.taskId, order: opts.order ?? 0, title: opts.title ?? "", runner: opts.runner, modelId: opts.modelId });
494
- session.onOutput((text2) => {
495
- this.broadcast({ type: "task_output", taskId: opts.taskId, text: text2 });
528
+ const output = this.outputOf(opts.taskId);
529
+ this.flushers.set(session.id, output.flush);
530
+ session.onExit(() => {
531
+ output.flush();
532
+ this.owned.delete(session.id);
533
+ this.flushers.delete(session.id);
496
534
  });
535
+ this.broadcast({ type: "task_started", taskId: opts.taskId, order: opts.order ?? 0, title: opts.title ?? "", runner: opts.runner, modelId: opts.modelId });
536
+ session.onOutput(output.add);
497
537
  return session;
498
538
  }
539
+ /**
540
+ * A runner's output arrives in chunks as small as a line, and every frame is
541
+ * stamped, serialized and sent to every socket. One frame per window carries
542
+ * the same bytes in the same order, and the message itself is unchanged
543
+ * (ADR-0063): a surface appends the text it receives.
544
+ */
545
+ outputOf(taskId) {
546
+ let pending = "";
547
+ let timer;
548
+ const flush = () => {
549
+ if (timer !== void 0) clearTimeout(timer);
550
+ timer = void 0;
551
+ if (pending === "") return;
552
+ const text2 = pending;
553
+ pending = "";
554
+ this.broadcast({ type: "task_output", taskId, text: text2 });
555
+ };
556
+ return {
557
+ add: (text2) => {
558
+ pending += text2;
559
+ timer ??= setTimeout(flush, OUTPUT_WINDOW_MS);
560
+ },
561
+ flush
562
+ };
563
+ }
499
564
  stop(sessionId) {
565
+ this.flushers.get(sessionId)?.();
500
566
  this.owned.delete(sessionId);
501
567
  return this.inner.stop(sessionId);
502
568
  }
503
569
  stopAll() {
504
- for (const id of this.owned) void this.inner.stop(id);
570
+ for (const id of this.owned) {
571
+ this.flushers.get(id)?.();
572
+ void this.inner.stop(id);
573
+ }
505
574
  this.owned.clear();
506
575
  }
507
576
  };
@@ -533,6 +602,7 @@ var OrchestratorPool = class {
533
602
  */
534
603
  stream = crypto.randomUUID();
535
604
  sequence = new StreamSequence(this.stream);
605
+ heldOutput = new HeldOutput((sessionId, workspace, msg) => this.emit(sessionId, workspace, msg));
536
606
  childStreams = new ChildStreams(this.sequence, (ws, workspace) => this.hears(ws, workspace));
537
607
  registry = new core.RunnerRegistry();
538
608
  modelResolver;
@@ -738,8 +808,26 @@ var OrchestratorPool = class {
738
808
  this.live(rootId, workspace);
739
809
  return this.childStreams.position(rootId, childId);
740
810
  }
741
- /** `workspace` is the one of the Session that speaks, which need not be the one holding the id by the time it does. */
811
+ /**
812
+ * `workspace` is the one of the Session that speaks, which need not be the one
813
+ * holding the id by the time it does.
814
+ *
815
+ * A socket that is not keeping up (a client whose event loop is busy) leaves
816
+ * its frames piling in the daemon, each one stamped and serialized for it. A
817
+ * frame cannot be dropped or merged once numbered without a client reading a
818
+ * gap and reloading, and a reload returns no task output, so what shrinks is
819
+ * decided before the number: a run of one task's output is told as one frame,
820
+ * for every socket (ADR-0063). Anything else of the session ends the run
821
+ * first, so output never passes or trails what followed it. Status updates
822
+ * are not merged here: each replaces what a client holds, but a client that
823
+ * folded the one before it can differ from one that never saw it.
824
+ */
742
825
  broadcast(sessionId, workspace, msg) {
826
+ if (msg.type === "task_output" && this.heldOutput.hold(sessionId, workspace, msg, () => this.someSocketBehind(sessionId, workspace))) return;
827
+ this.heldOutput.flush(sessionId);
828
+ this.emit(sessionId, workspace, msg);
829
+ }
830
+ emit(sessionId, workspace, msg) {
743
831
  const monitor = workspace ? this.accountLimits.get(workspace) : void 0;
744
832
  if (msg.type === "planner_usage") monitor?.invalidate();
745
833
  else if (msg.type === "task_log" && msg.events.some((event) => event.type === "turn_end")) {
@@ -749,6 +837,12 @@ var OrchestratorPool = class {
749
837
  }
750
838
  this.send(sessionId, workspace, JSON.stringify({ ...msg, ...this.sequence.next(sessionId) }));
751
839
  }
840
+ someSocketBehind(sessionId, workspace) {
841
+ for (const ws of this.clients.get(sessionId) ?? []) {
842
+ if (ws.readyState === ws.OPEN && this.hears(ws, workspace) && ws.bufferedAmount > SLOW_SOCKET_BYTES) return true;
843
+ }
844
+ return false;
845
+ }
752
846
  hasSession(sessionId) {
753
847
  return this.sessions.has(sessionId);
754
848
  }
@@ -1708,6 +1802,17 @@ function childrenRoute(pool) {
1708
1802
  }
1709
1803
  return router;
1710
1804
  }
1805
+ function liveMeta(id, goal, plan) {
1806
+ return {
1807
+ id,
1808
+ goal: goal || "(untitled)",
1809
+ runners: plan.runners,
1810
+ taskCount: plan.tasks.flatMap((t) => [t, ...t.subtasks || []]).length,
1811
+ status: plan.status,
1812
+ createdAt: plan.generatedAt,
1813
+ updatedAt: plan.lastUpdated
1814
+ };
1815
+ }
1711
1816
  function sessionsRoute(pool) {
1712
1817
  const router = new hono.Hono();
1713
1818
  router.get("/", (c) => {
@@ -1726,16 +1831,18 @@ function sessionsRoute(pool) {
1726
1831
  try {
1727
1832
  const ws = c.req.query("workspace") || process.cwd();
1728
1833
  const id = c.req.param("id");
1729
- const saved = core.loadSession(id, ws);
1730
- if (!saved) return refuse(c, 404, "Session not found", "session_not_found");
1731
1834
  const live = pool.holds(id, ws);
1835
+ const livePlan = live ? pool.getPlan(id) : null;
1836
+ const livePlanState = live ? pool.getPlanState(id) : null;
1837
+ const saved = livePlan && livePlanState ? null : core.loadSession(id, ws);
1838
+ if (!saved && !(livePlan && livePlanState)) return refuse(c, 404, "Session not found", "session_not_found");
1732
1839
  const run2 = live ? pool.runSnapshot(id) : null;
1733
1840
  const plannerTurnId = live ? pool.plannerTurnId(id) : void 0;
1734
1841
  const plannerCompaction = live ? pool.plannerCompaction(id) : void 0;
1735
- const { conversationHistory, researchLog, plannerUsage, pendingTurns } = core.surfacePlan((live ? pool.getPlan(id) : null) ?? saved.plan);
1842
+ const { conversationHistory, researchLog, plannerUsage, pendingTurns } = core.surfacePlan(livePlan ?? saved.plan);
1736
1843
  return c.json({
1737
- meta: saved.meta,
1738
- plan: core.surfacePlanState((live ? pool.getPlanState(id) : null) ?? core.migratePlanState(saved.plan)),
1844
+ meta: saved?.meta ?? liveMeta(id, pool.getGoal(id), livePlan),
1845
+ plan: core.surfacePlanState(livePlanState ?? core.migratePlanState(saved.plan)),
1739
1846
  conversation: { conversationHistory, researchLog, plannerUsage, pendingTurns },
1740
1847
  ...run2 ? { run: run2 } : {},
1741
1848
  ...plannerTurnId ? { plannerTurnId } : {},