litepaseo 0.13.1 → 0.13.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.
Files changed (127) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/dist/server/cli.mjs +2 -2
  3. package/dist/server/{worker-DfNLXgRM.mjs → worker-C-KgOZ4j.mjs} +329 -416
  4. package/dist/web/assets/{AddProjectDialog-BH4t1mH0.js → AddProjectDialog-DqnbtCX_.js} +1 -1
  5. package/dist/web/assets/AddProjectDialog-DqnbtCX_.js.br +0 -0
  6. package/dist/web/assets/AddProjectDialog-DqnbtCX_.js.gz +0 -0
  7. package/dist/web/assets/{ArchivedWorkspaces-c0EOLkEH.js → ArchivedWorkspaces-BVNADrnb.js} +1 -1
  8. package/dist/web/assets/ArchivedWorkspaces-BVNADrnb.js.br +0 -0
  9. package/dist/web/assets/ArchivedWorkspaces-BVNADrnb.js.gz +0 -0
  10. package/dist/web/assets/{ChangesPanel-DjkKjhfi.js → ChangesPanel-B_aMHOy0.js} +1 -1
  11. package/dist/web/assets/ChangesPanel-B_aMHOy0.js.br +0 -0
  12. package/dist/web/assets/ChangesPanel-B_aMHOy0.js.gz +0 -0
  13. package/dist/web/assets/{CodeBlockNode-bijGGeoN.js → CodeBlockNode-BHpMMKG-.js} +1 -1
  14. package/dist/web/assets/CodeBlockNode-BHpMMKG-.js.br +0 -0
  15. package/dist/web/assets/CodeBlockNode-BHpMMKG-.js.gz +0 -0
  16. package/dist/web/assets/{CommandCenter-CCTGjCPX.js → CommandCenter-CLxqHc3O.js} +1 -1
  17. package/dist/web/assets/CommandCenter-CLxqHc3O.js.br +0 -0
  18. package/dist/web/assets/CommandCenter-CLxqHc3O.js.gz +0 -0
  19. package/dist/web/assets/{ConversationSearch-1MY_nmz4.js → ConversationSearch-FTWEWSUu.js} +1 -1
  20. package/dist/web/assets/ConversationSearch-FTWEWSUu.js.br +0 -0
  21. package/dist/web/assets/ConversationSearch-FTWEWSUu.js.gz +0 -0
  22. package/dist/web/assets/{FileViewer-B0bQlA3z.js → FileViewer-C5O-KMUx.js} +1 -1
  23. package/dist/web/assets/FileViewer-C5O-KMUx.js.br +0 -0
  24. package/dist/web/assets/FileViewer-C5O-KMUx.js.gz +0 -0
  25. package/dist/web/assets/{FilesPanel-D_-v_ZcF.js → FilesPanel-Dg9lqGBo.js} +1 -1
  26. package/dist/web/assets/FilesPanel-Dg9lqGBo.js.br +0 -0
  27. package/dist/web/assets/FilesPanel-Dg9lqGBo.js.gz +0 -0
  28. package/dist/web/assets/{HostSettings-DSgKGO4q.js → HostSettings-DsUCWEUL.js} +1 -1
  29. package/dist/web/assets/HostSettings-DsUCWEUL.js.br +0 -0
  30. package/dist/web/assets/HostSettings-DsUCWEUL.js.gz +0 -0
  31. package/dist/web/assets/{KanbanPage-CdxFwCay.js → KanbanPage-CSWy9O3h.js} +1 -1
  32. package/dist/web/assets/KanbanPage-CSWy9O3h.js.br +0 -0
  33. package/dist/web/assets/KanbanPage-CSWy9O3h.js.gz +0 -0
  34. package/dist/web/assets/{LabelsDialog-BLDf7cQV.js → LabelsDialog-CITuVVJW.js} +1 -1
  35. package/dist/web/assets/LabelsDialog-CITuVVJW.js.br +0 -0
  36. package/dist/web/assets/LabelsDialog-CITuVVJW.js.gz +0 -0
  37. package/dist/web/assets/{MarkdownMessage-C7r5tc7R.js → MarkdownMessage-BSPsJz2u.js} +5 -5
  38. package/dist/web/assets/MarkdownMessage-BSPsJz2u.js.br +0 -0
  39. package/dist/web/assets/MarkdownMessage-BSPsJz2u.js.gz +0 -0
  40. package/dist/web/assets/{PreCodeBlock-Df4DvpxT.js → PreCodeBlock-CUsnJ_0J.js} +1 -1
  41. package/dist/web/assets/{PreCodeBlock.vue_vue_type_style_index_0_lang-bn_V109a.js → PreCodeBlock.vue_vue_type_style_index_0_lang-B_ZZH8sz.js} +1 -1
  42. package/dist/web/assets/PreCodeBlock.vue_vue_type_style_index_0_lang-B_ZZH8sz.js.br +0 -0
  43. package/dist/web/assets/PreCodeBlock.vue_vue_type_style_index_0_lang-B_ZZH8sz.js.gz +0 -0
  44. package/dist/web/assets/{SubagentsPanel-CU1DOqGi.js → SubagentsPanel-Brk1hh5C.js} +1 -1
  45. package/dist/web/assets/SubagentsPanel-Brk1hh5C.js.br +0 -0
  46. package/dist/web/assets/SubagentsPanel-Brk1hh5C.js.gz +0 -0
  47. package/dist/web/assets/{TerminalPanel-CKE10Knv.js → TerminalPanel-tG9J8yCX.js} +1 -1
  48. package/dist/web/assets/TerminalPanel-tG9J8yCX.js.br +0 -0
  49. package/dist/web/assets/TerminalPanel-tG9J8yCX.js.gz +0 -0
  50. package/dist/web/assets/{Tooltip-CKIQz11V.js → Tooltip-MKPYhhV1.js} +1 -1
  51. package/dist/web/assets/Tooltip-MKPYhhV1.js.br +0 -0
  52. package/dist/web/assets/Tooltip-MKPYhhV1.js.gz +0 -0
  53. package/dist/web/assets/{index--Rpf_YJs.js → index-DAwT50Ss.js} +29 -29
  54. package/dist/web/assets/index-DAwT50Ss.js.br +0 -0
  55. package/dist/web/assets/index-DAwT50Ss.js.gz +0 -0
  56. package/dist/web/assets/{index10-BH5cR790.js → index10-DahGcoad.js} +1 -1
  57. package/dist/web/assets/index10-DahGcoad.js.br +0 -0
  58. package/dist/web/assets/index10-DahGcoad.js.gz +0 -0
  59. package/dist/web/assets/index3-C_-f4bxf.js +1 -0
  60. package/dist/web/assets/index4-Bv2X2ReP.js +1 -0
  61. package/dist/web/assets/{index5-COjA17v2.js → index5-CgcNfgxb.js} +1 -1
  62. package/dist/web/assets/index5-CgcNfgxb.js.br +0 -0
  63. package/dist/web/assets/index5-CgcNfgxb.js.gz +0 -0
  64. package/dist/web/assets/{index6-C7WwIv8h.js → index6-CrSTK4tr.js} +1 -1
  65. package/dist/web/assets/index6-CrSTK4tr.js.br +0 -0
  66. package/dist/web/assets/index6-CrSTK4tr.js.gz +0 -0
  67. package/dist/web/assets/{index7-MofimpJO.js → index7-DWSIBAjx.js} +1 -1
  68. package/dist/web/assets/index7-DWSIBAjx.js.br +0 -0
  69. package/dist/web/assets/index7-DWSIBAjx.js.gz +0 -0
  70. package/dist/web/assets/{index8-BN62x6v7.js → index8-C-KUFSiY.js} +1 -1
  71. package/dist/web/assets/index8-C-KUFSiY.js.br +0 -0
  72. package/dist/web/assets/index8-C-KUFSiY.js.gz +0 -0
  73. package/dist/web/assets/{index9-DSF8wXFh.js → index9-CHVI36Nl.js} +1 -1
  74. package/dist/web/assets/index9-CHVI36Nl.js.br +0 -0
  75. package/dist/web/assets/index9-CHVI36Nl.js.gz +0 -0
  76. package/dist/web/index.html +1 -1
  77. package/dist/web/index.html.br +0 -0
  78. package/dist/web/index.html.gz +0 -0
  79. package/package.json +1 -1
  80. package/dist/web/assets/AddProjectDialog-BH4t1mH0.js.br +0 -0
  81. package/dist/web/assets/AddProjectDialog-BH4t1mH0.js.gz +0 -0
  82. package/dist/web/assets/ArchivedWorkspaces-c0EOLkEH.js.br +0 -0
  83. package/dist/web/assets/ArchivedWorkspaces-c0EOLkEH.js.gz +0 -0
  84. package/dist/web/assets/ChangesPanel-DjkKjhfi.js.br +0 -0
  85. package/dist/web/assets/ChangesPanel-DjkKjhfi.js.gz +0 -0
  86. package/dist/web/assets/CodeBlockNode-bijGGeoN.js.br +0 -0
  87. package/dist/web/assets/CodeBlockNode-bijGGeoN.js.gz +0 -0
  88. package/dist/web/assets/CommandCenter-CCTGjCPX.js.br +0 -0
  89. package/dist/web/assets/CommandCenter-CCTGjCPX.js.gz +0 -0
  90. package/dist/web/assets/ConversationSearch-1MY_nmz4.js.br +0 -0
  91. package/dist/web/assets/ConversationSearch-1MY_nmz4.js.gz +0 -0
  92. package/dist/web/assets/FileViewer-B0bQlA3z.js.br +0 -0
  93. package/dist/web/assets/FileViewer-B0bQlA3z.js.gz +0 -0
  94. package/dist/web/assets/FilesPanel-D_-v_ZcF.js.br +0 -0
  95. package/dist/web/assets/FilesPanel-D_-v_ZcF.js.gz +0 -0
  96. package/dist/web/assets/HostSettings-DSgKGO4q.js.br +0 -0
  97. package/dist/web/assets/HostSettings-DSgKGO4q.js.gz +0 -0
  98. package/dist/web/assets/KanbanPage-CdxFwCay.js.br +0 -0
  99. package/dist/web/assets/KanbanPage-CdxFwCay.js.gz +0 -0
  100. package/dist/web/assets/LabelsDialog-BLDf7cQV.js.br +0 -0
  101. package/dist/web/assets/LabelsDialog-BLDf7cQV.js.gz +0 -0
  102. package/dist/web/assets/MarkdownMessage-C7r5tc7R.js.br +0 -0
  103. package/dist/web/assets/MarkdownMessage-C7r5tc7R.js.gz +0 -0
  104. package/dist/web/assets/PreCodeBlock.vue_vue_type_style_index_0_lang-bn_V109a.js.br +0 -0
  105. package/dist/web/assets/PreCodeBlock.vue_vue_type_style_index_0_lang-bn_V109a.js.gz +0 -0
  106. package/dist/web/assets/SubagentsPanel-CU1DOqGi.js.br +0 -0
  107. package/dist/web/assets/SubagentsPanel-CU1DOqGi.js.gz +0 -0
  108. package/dist/web/assets/TerminalPanel-CKE10Knv.js.br +0 -0
  109. package/dist/web/assets/TerminalPanel-CKE10Knv.js.gz +0 -0
  110. package/dist/web/assets/Tooltip-CKIQz11V.js.br +0 -0
  111. package/dist/web/assets/Tooltip-CKIQz11V.js.gz +0 -0
  112. package/dist/web/assets/index--Rpf_YJs.js.br +0 -0
  113. package/dist/web/assets/index--Rpf_YJs.js.gz +0 -0
  114. package/dist/web/assets/index10-BH5cR790.js.br +0 -0
  115. package/dist/web/assets/index10-BH5cR790.js.gz +0 -0
  116. package/dist/web/assets/index3-OdSvV8Az.js +0 -1
  117. package/dist/web/assets/index4-p9EqAM3V.js +0 -1
  118. package/dist/web/assets/index5-COjA17v2.js.br +0 -0
  119. package/dist/web/assets/index5-COjA17v2.js.gz +0 -0
  120. package/dist/web/assets/index6-C7WwIv8h.js.br +0 -0
  121. package/dist/web/assets/index6-C7WwIv8h.js.gz +0 -0
  122. package/dist/web/assets/index7-MofimpJO.js.br +0 -0
  123. package/dist/web/assets/index7-MofimpJO.js.gz +0 -0
  124. package/dist/web/assets/index8-BN62x6v7.js.br +0 -0
  125. package/dist/web/assets/index8-BN62x6v7.js.gz +0 -0
  126. package/dist/web/assets/index9-DSF8wXFh.js.br +0 -0
  127. package/dist/web/assets/index9-DSF8wXFh.js.gz +0 -0
@@ -9,7 +9,6 @@ import { createHostClient, parseHostTarget } from "@getpaseo/client/node";
9
9
  import { createPaseoApi } from "@getpaseo/client";
10
10
  import { deriveAgentStateBucket } from "@getpaseo/protocol/agent-state-bucket";
11
11
  import { getParentAgentIdFromLabels } from "@getpaseo/protocol/agent-labels";
12
- import { timelineItemIdentity } from "@getpaseo/protocol/timeline-identity";
13
12
  import { lstat, readFile, realpath } from "node:fs/promises";
14
13
  import { Hono } from "hono";
15
14
  import { bodyLimit } from "hono/body-limit";
@@ -733,465 +732,388 @@ var HostWorkspaces = class extends HostMirror {
733
732
  };
734
733
  // src/server/timeline-view.ts
735
734
  const TAIL_LIMIT = 60;
736
- const CATCH_UP_PAGE = 200;
737
- /** Catch-up pages read in one go; further behind than this, reloading the tail is cheaper. */
738
- const MAX_CATCH_UP_PAGES = 25;
739
- /** Viewers that joined a loading view tracked for its load report. */
740
- const MAX_JOINERS = 64;
741
- /** Rows kept per view; older rows are dropped and stay reachable through history pages. */
735
+ /** Rows kept per view; older rows stay reachable through on-demand history pages. */
742
736
  const MAX_ROWS = 600;
743
737
  const KEEP_ROWS = 500;
744
738
  /** Rows sent to a client that (re)joins; older ones load on demand. */
745
739
  const RESET_ROWS = 200;
746
- const MAX_BUFFER = 5e3;
747
- const FLUSH_MS$2 = 500;
740
+ const MAX_JOINERS = 64;
741
+ /** Low-frequency pull interval while an agent is actively running and watched. */
742
+ const DEFAULT_POLL_MS = 1500;
743
+ /** Retry delay when the initial load fails. */
748
744
  const RETRY_MS = 2e3;
745
+ /** One-shot follow-up delay after a turn ends if turnMetrics have not landed yet. */
746
+ const METRICS_SETTLE_MS = 700;
747
+ /** Per-pull RPC timeout so a wedged remote host can never lock `inFlight` forever. */
748
+ const PULL_TIMEOUT_MS = 2e4;
749
+ /** If a browser reconnects/resumes after this much silence, verify once against the daemon. */
750
+ const RESUME_VERIFY_MS = 5e3;
749
751
  const now = () => performance.now();
750
- /** Same fold the daemon's TimelineProjection applies when a tool call reports again. */
751
- function mergeToolCallItems(existing, incoming) {
752
- const a = existing.detail, b = incoming.detail;
753
- const detail = a?.type === "unknown" && b?.type !== "unknown" ? b : b?.type === "unknown" && a?.type !== "unknown" ? a : b;
754
- const metadata = existing.metadata || incoming.metadata ? {
755
- ...existing.metadata,
756
- ...incoming.metadata
757
- } : void 0;
758
- const merged = {
759
- ...existing,
760
- ...incoming,
761
- detail,
762
- metadata
752
+ function toRow(entry) {
753
+ return {
754
+ seqStart: entry.seqStart,
755
+ seqEnd: entry.seqEnd,
756
+ timestamp: entry.timestamp,
757
+ provider: entry.provider,
758
+ ...entry.turnId ? { turnId: entry.turnId } : {},
759
+ item: entry.item,
760
+ ...entry.turnMetrics?.length ? { turnMetrics: entry.turnMetrics } : {}
763
761
  };
764
- if (incoming.status === "failed") merged.error = incoming.error;
765
- else if (incoming.status === "completed" || incoming.status === "canceled") merged.error = null;
766
- else if (incoming.error !== void 0) merged.error = incoming.error;
767
- return merged;
768
762
  }
769
- function textMergeable(previous, entry) {
770
- const a = previous.item, b = entry.item;
771
- if (a.type !== b.type || previous.seqEnd + 1 !== entry.seqStart || previous.turnId !== entry.turnId) return false;
772
- if (a.type === "reasoning") return true;
773
- return a.type === "assistant_message" && (b.messageId === void 0 || a.messageId === b.messageId);
763
+ function rowFingerprint(row) {
764
+ return JSON.stringify([
765
+ row.seqEnd,
766
+ row.timestamp,
767
+ row.provider,
768
+ row.turnId ?? null,
769
+ row.item,
770
+ row.turnMetrics ?? null
771
+ ]);
774
772
  }
775
773
  /**
776
- * Live projection of one agent's timeline, shared by every browser viewing it. Daemon stream
777
- * events are folded with the daemon's own projection rules, so each event costs O(1) work and
778
- * reaches browsers as a small upsert/append op. Gaps, unknown identities and reconnects fall
779
- * back to an incremental `after` fetch, which returns only rows touched since the cursor.
774
+ * Single-source-of-truth projected timeline view (Wake + Pull).
775
+ *
776
+ * Every row comes directly from the daemon's `fetchAgentTimeline(..., projection: "projected")`:
777
+ * - Idle conversations do zero polling and use zero timers.
778
+ * - Active (running) conversations pull at a low cadence (1.5 s) while watched and broadcast only
779
+ * rows whose fingerprint actually changed.
780
+ * - Turning idle triggers a final pull (with one short follow-up if turnMetrics haven't landed),
781
+ * and every changed row advances a monotonic `revision` so returning tabs resume cleanly with
782
+ * all updates including `turnMetrics`.
780
783
  */
781
784
  var TimelineView = class {
782
785
  rows = [];
783
786
  bySeqStart = /* @__PURE__ */ new Map();
784
- identities = /* @__PURE__ */ new Map();
785
- /** seqStarts whose turnMetrics were attached after streaming; included in resumeOps so returning tabs pick them up. */
786
- metricsTouched = /* @__PURE__ */ new Set();
787
+ fingerprints = /* @__PURE__ */ new Map();
788
+ rowRev = /* @__PURE__ */ new Map();
787
789
  epoch = "";
788
- lastSeq = -1;
790
+ revision = 0;
791
+ resumeFloor = 0;
789
792
  hasOlder = false;
790
793
  startCursor = null;
791
- /**
792
- * The view knows every row changed after this seq, so a browser holding everything up to it
793
- * can resume with just those rows. Set by each load; raised when rows are trimmed.
794
- */
795
- resumeFloor = Number.POSITIVE_INFINITY;
796
- /** True while rows reach back to the first projected row, so unseen identities are new. */
797
- complete = false;
798
- state = "loading";
799
- buffer = [];
800
- overflowed = false;
794
+ state = "idle";
795
+ active = false;
801
796
  watchers = /* @__PURE__ */ new Set();
802
- /** seqStart → pending change since the last flush; full means send the whole row. */
803
- pending = /* @__PURE__ */ new Map();
804
- pendingReset = false;
805
- flushTimer;
797
+ pollTimer;
806
798
  retryTimer;
807
- subscription = null;
799
+ settleTimer;
800
+ followUpTimer;
801
+ inFlight = false;
802
+ pendingPull = false;
803
+ pendingSettle = false;
804
+ consecutiveFailures = 0;
805
+ lastPullAt = 0;
808
806
  token = 0;
809
- loads = 0;
810
- /** Timings of the load in progress, until the first rows go out. */
811
807
  timing = null;
812
- /** Viewers that joined while the view was loading, waiting for its first rows. */
813
808
  joiners = [];
814
809
  driver;
815
810
  agentId;
816
811
  report;
817
- constructor(driver, agentId, report) {
812
+ pollMs;
813
+ constructor(driver, agentId, report, pollMs = DEFAULT_POLL_MS) {
818
814
  this.driver = driver;
819
815
  this.agentId = agentId;
820
816
  this.report = report;
817
+ this.pollMs = pollMs;
821
818
  }
822
819
  get watcherCount() {
823
820
  return this.watchers.size;
824
821
  }
825
- /** Where the view stands, for diagnosing a viewer that waits. */
826
822
  get status() {
827
823
  return this.state;
828
824
  }
829
825
  start() {
830
- const token = ++this.token;
826
+ if (this.state === "disposed" || this.state === "loading" || this.state === "live") return;
831
827
  this.state = "loading";
832
- this.buffer = [];
833
- this.overflowed = false;
834
- const attemptAt = now();
835
- if (this.timing) {
836
- this.timing.attempts++;
837
- this.timing.attemptAt = attemptAt;
838
- } else this.timing = {
839
- startedAt: attemptAt,
840
- attemptAt,
841
- subscribeMs: null,
828
+ this.token++;
829
+ this.timing ??= {
830
+ startedAt: now(),
842
831
  fetchMs: null,
843
- attempts: 1,
832
+ attempts: 0,
844
833
  failures: 0
845
834
  };
846
- const subscription = this.driver.subscribeAgentTimeline(this.agentId, (message) => {
847
- if (token === this.token) this.onMessage(message);
848
- });
849
- this.subscription = subscription;
850
- subscription.ready.then(() => {
851
- if (token !== this.token) return;
852
- if (this.timing) this.timing.subscribeMs = Math.round(now() - this.timing.attemptAt);
853
- return this.reload();
854
- }, () => this.fail(token));
855
- }
856
- dispose() {
857
- this.token++;
858
- this.state = "disposed";
859
- clearTimeout(this.flushTimer);
860
- clearTimeout(this.retryTimer);
861
- this.watchers.clear();
862
- const released = this.subscription?.release().catch(() => {}) ?? Promise.resolve();
863
- this.subscription = null;
864
- return released;
835
+ this.timing.attempts++;
836
+ this.pull(true);
837
+ }
838
+ /** Updates whether this conversation is actively running; drives low-frequency pulling. */
839
+ setActive(running) {
840
+ if (this.state === "disposed") return;
841
+ const wasActive = this.active;
842
+ this.active = running;
843
+ clearTimeout(this.settleTimer);
844
+ this.settleTimer = void 0;
845
+ if (running) {
846
+ if (this.state === "live") {
847
+ this.pull(false);
848
+ this.schedulePoll();
849
+ }
850
+ } else {
851
+ clearTimeout(this.pollTimer);
852
+ this.pollTimer = void 0;
853
+ if (wasActive && this.state === "live") this.pull(false, true);
854
+ }
865
855
  }
866
856
  /**
867
- * Adds a browser. A live view answers at once: with the rows changed since `since` when the
868
- * browser holds this conversation up to a point the view can resume from (nothing when none
869
- * changed), else with a reset. A loading view sends its reset once loaded.
857
+ * Requests an immediate pull (e.g. after a message send, rewind, reconnect, or state update).
858
+ * With `followUp = true`, schedules one short 500ms follow-up pull if idle so instant commands
859
+ * or newly written prompts are never missed without starting an unbounded polling loop.
870
860
  */
861
+ wake(followUp = false) {
862
+ if (this.state === "disposed") return;
863
+ if (this.state === "idle" || this.state === "failed") return this.start();
864
+ if (this.state === "live") this.pull(false);
865
+ else this.pendingPull = true;
866
+ if (followUp) {
867
+ clearTimeout(this.followUpTimer);
868
+ this.followUpTimer = setTimeout(() => {
869
+ this.followUpTimer = void 0;
870
+ if (this.state === "live" && !this.active) this.pull(false);
871
+ }, 500);
872
+ this.followUpTimer.unref?.();
873
+ }
874
+ }
875
+ /** Backwards-compatible alias for one-shot tail refresh. */
876
+ refreshTailMetrics() {
877
+ this.wake();
878
+ }
871
879
  watch(watcher, since) {
872
- if (this.state === "live" || this.state === "catching-up") {
873
- this.flush();
874
- const resumed = since ? this.resumeOps(since) : null;
875
- if (!resumed) watcher([this.resetOp()]);
876
- else if (resumed.length) watcher(resumed);
877
- } else if (this.watchers.size && this.joiners.length < MAX_JOINERS) this.joiners.push(now());
878
880
  this.watchers.add(watcher);
879
- return () => this.watchers.delete(watcher);
880
- }
881
- onMessage(message) {
882
- switch (message.type) {
883
- case "agent_stream": {
884
- const payload = message.payload;
885
- if (payload.event.type !== "timeline" || payload.seq === void 0) return;
886
- if (this.state !== "live") return this.bufferEvent(payload);
887
- return this.process(payload);
881
+ if (this.state === "live") {
882
+ const ops = since ? this.resumeOps(since) : null;
883
+ if (ops) {
884
+ if (ops.length) watcher(ops);
885
+ } else watcher([this.resetOp()]);
886
+ if (this.active) this.schedulePoll();
887
+ else if (since && now() - this.lastPullAt >= RESUME_VERIFY_MS) this.wake();
888
+ } else if (this.state === "loading" && this.joiners.length < MAX_JOINERS) this.joiners.push(now());
889
+ return () => {
890
+ this.watchers.delete(watcher);
891
+ if (!this.watchers.size) {
892
+ clearTimeout(this.pollTimer);
893
+ this.pollTimer = void 0;
888
894
  }
889
- case "agent.timeline.replacement":
890
- this.reload();
891
- return;
892
- case "agent.timeline.subscription_restored":
893
- this.state === "live" && this.catchUp();
894
- return;
895
- case "agent.timeline.error": return this.fail(this.token);
896
- }
897
- }
898
- bufferEvent(payload) {
899
- if (this.overflowed) return;
900
- if (this.buffer.length >= MAX_BUFFER) {
901
- this.buffer = [];
902
- this.overflowed = true;
903
- return;
904
- }
905
- this.buffer.push(payload);
895
+ };
906
896
  }
907
- process(payload) {
908
- const seq = payload.seq;
909
- if (payload.epoch !== void 0 && payload.epoch !== this.epoch) return void this.reload();
910
- if (seq <= this.lastSeq) return;
911
- if (seq > this.lastSeq + 1) {
912
- this.bufferEvent(payload);
913
- this.catchUp();
897
+ dispose() {
898
+ this.state = "disposed";
899
+ this.token++;
900
+ clearTimeout(this.pollTimer);
901
+ clearTimeout(this.retryTimer);
902
+ clearTimeout(this.settleTimer);
903
+ clearTimeout(this.followUpTimer);
904
+ this.pollTimer = this.retryTimer = this.settleTimer = this.followUpTimer = void 0;
905
+ this.watchers.clear();
906
+ return Promise.resolve();
907
+ }
908
+ schedulePoll() {
909
+ if (this.state !== "live" || !this.active || !this.watchers.size || this.pollTimer) return;
910
+ this.pollTimer = setTimeout(() => {
911
+ this.pollTimer = void 0;
912
+ if (this.state !== "live" || !this.active || !this.watchers.size) return;
913
+ this.pull(false);
914
+ }, this.pollMs);
915
+ this.pollTimer.unref?.();
916
+ }
917
+ async pull(initial, checkMetricsSettle = false) {
918
+ if (this.state === "disposed") return;
919
+ if (this.inFlight) {
920
+ this.pendingPull = true;
921
+ if (checkMetricsSettle) this.pendingSettle = true;
914
922
  return;
915
923
  }
916
- if (!this.applyLive(payload)) return void this.catchUp();
917
- this.lastSeq = seq;
918
- }
919
- /** Folds one source event; false when the view cannot decide locally. */
920
- applyLive(payload) {
921
- const entry = {
922
- seqStart: payload.seq,
923
- seqEnd: payload.seq,
924
- timestamp: payload.timestamp,
925
- provider: payload.event.provider ?? "",
926
- ...payload.event.turnId ? { turnId: payload.event.turnId } : {},
927
- item: payload.event.item
928
- };
929
- const identity = timelineItemIdentity(entry.item);
930
- if (identity !== null) {
931
- const existing = this.identities.get(identity);
932
- if (existing) {
933
- if (entry.item.type === "tool_call") {
934
- if (existing.item.type === "tool_call" && existing.turnId === entry.turnId) {
935
- existing.item = mergeToolCallItems(existing.item, entry.item);
936
- existing.timestamp = entry.timestamp;
937
- existing.seqEnd = Math.max(existing.seqEnd, entry.seqEnd);
938
- this.mark(existing.seqStart, true);
939
- return true;
940
- }
941
- } else if (entry.item.type === "plugin" && existing.item.type === "plugin") {
942
- Object.assign(existing, {
943
- ...entry,
944
- seqStart: existing.seqStart
945
- });
946
- this.mark(existing.seqStart, true);
947
- return true;
948
- }
949
- } else if (!this.complete) return false;
950
- }
951
- const previous = this.rows.at(-1);
952
- if (previous && textMergeable(previous, entry)) {
953
- const text = entry.item.text ?? "";
954
- previous.item = {
955
- type: previous.item.type,
956
- text: (previous.item.text ?? "") + text,
957
- ...previous.item.type === "assistant_message" && previous.item.messageId ? { messageId: previous.item.messageId } : {}
958
- };
959
- previous.timestamp = entry.timestamp;
960
- previous.seqEnd = entry.seqEnd;
961
- this.mark(previous.seqStart, false, text);
962
- return true;
963
- }
964
- this.insert(entry);
965
- return true;
966
- }
967
- async reload() {
924
+ this.inFlight = true;
968
925
  const token = this.token;
969
- const load = ++this.loads;
970
- this.state = "loading";
971
926
  const fetchAt = now();
927
+ let timeoutHandle;
972
928
  try {
973
- const page = await this.driver.fetchAgentTimeline(this.agentId, {
929
+ const fetchPromise = this.driver.fetchAgentTimeline(this.agentId, {
974
930
  direction: "tail",
975
931
  limit: TAIL_LIMIT,
976
932
  projection: "projected",
977
- wholeTurns: false
933
+ wholeTurns: false,
934
+ timeout: 15e3
978
935
  });
979
- if (token !== this.token || load !== this.loads) return;
980
- if (this.timing) this.timing.fetchMs = Math.round(now() - fetchAt);
981
- this.replaceWith(page);
982
- this.flush();
983
- this.settle();
984
- } catch {
985
- this.fail(token);
986
- }
987
- }
988
- async catchUp() {
989
- if (this.state === "catching-up" || this.state === "loading") return;
990
- const token = this.token;
991
- this.state = "catching-up";
992
- try {
993
- for (let pages = 1;; pages++) {
994
- const before = this.lastSeq;
995
- const page = await this.driver.fetchAgentTimeline(this.agentId, {
996
- direction: "after",
997
- cursor: {
998
- epoch: this.epoch,
999
- seq: Math.max(0, this.lastSeq)
1000
- },
1001
- limit: CATCH_UP_PAGE,
1002
- projection: "projected"
1003
- });
1004
- if (token !== this.token) return;
1005
- if (page.reset || page.epoch !== this.epoch) {
1006
- this.replaceWith(page);
1007
- break;
936
+ const timeoutPromise = new Promise((_, reject) => {
937
+ timeoutHandle = setTimeout(() => reject(/* @__PURE__ */ new Error("Timeline fetch timed out")), PULL_TIMEOUT_MS);
938
+ timeoutHandle.unref?.();
939
+ });
940
+ const page = await Promise.race([fetchPromise, timeoutPromise]);
941
+ if (token !== this.token) return;
942
+ this.lastPullAt = now();
943
+ if (this.timing) this.timing.fetchMs = Math.round(this.lastPullAt - fetchAt);
944
+ this.consecutiveFailures = 0;
945
+ if (initial || this.state !== "live" || page.reset || page.epoch !== this.epoch) {
946
+ this.replaceWith(page);
947
+ this.state = "live";
948
+ this.loaded();
949
+ this.broadcast([this.resetOp()]);
950
+ } else {
951
+ const ops = this.mergeTail(page);
952
+ if (ops.length) this.broadcast(ops);
953
+ }
954
+ if (checkMetricsSettle && !this.active) {
955
+ const latest = [...page.entries].reverse().find((e) => e.item.type === "assistant_message" || e.item.type === "tool_call");
956
+ if (latest && !latest.turnMetrics?.length) {
957
+ clearTimeout(this.settleTimer);
958
+ this.settleTimer = setTimeout(() => {
959
+ this.settleTimer = void 0;
960
+ if (this.state === "live" && !this.active) this.pull(false, false);
961
+ }, METRICS_SETTLE_MS);
962
+ this.settleTimer.unref?.();
1008
963
  }
1009
- for (const entry of page.entries) this.upsertFetched(entry);
1010
- const end = page.endCursor?.seq ?? (page.hasNewer ? null : page.window.maxSeq);
1011
- if (end === null) break;
1012
- this.lastSeq = Math.max(this.lastSeq, end);
1013
- if (!page.hasNewer) break;
1014
- if (end <= before || pages >= MAX_CATCH_UP_PAGES) return void this.reload();
1015
964
  }
1016
- this.settle();
1017
965
  } catch {
1018
- this.fail(token);
1019
- }
1020
- }
1021
- /** Back to live: replay what arrived meanwhile, or reload if the buffer overflowed. */
1022
- settle() {
1023
- if (this.overflowed) {
1024
- this.overflowed = false;
1025
- this.buffer = [];
1026
- this.reload();
1027
- return;
1028
- }
1029
- this.state = "live";
1030
- const events = this.buffer;
1031
- this.buffer = [];
1032
- for (let i = 0; i < events.length; i++) {
1033
- this.process(events[i]);
1034
- if (this.state !== "live") {
1035
- this.buffer = this.buffer.concat(events.slice(i + 1));
1036
- return;
966
+ if (token !== this.token) return;
967
+ this.consecutiveFailures++;
968
+ if (this.timing) this.timing.failures++;
969
+ if (initial || this.state !== "live") {
970
+ this.state = "failed";
971
+ this.broadcast([{
972
+ type: "error",
973
+ message: "Timeline sync interrupted; retrying"
974
+ }]);
975
+ clearTimeout(this.retryTimer);
976
+ this.retryTimer = setTimeout(() => {
977
+ this.retryTimer = void 0;
978
+ if (this.state === "failed" && token === this.token) this.start();
979
+ }, RETRY_MS);
980
+ this.retryTimer.unref?.();
981
+ } else if (this.consecutiveFailures >= 3) this.broadcast([{
982
+ type: "error",
983
+ message: "Timeline sync interrupted; retrying"
984
+ }]);
985
+ } finally {
986
+ clearTimeout(timeoutHandle);
987
+ this.inFlight = false;
988
+ if (this.state === "live") {
989
+ if (this.pendingPull) {
990
+ const settle = this.pendingSettle;
991
+ this.pendingPull = false;
992
+ this.pendingSettle = false;
993
+ this.pull(false, settle);
994
+ } else if (this.active && this.watchers.size) this.schedulePoll();
1037
995
  }
1038
996
  }
1039
997
  }
1040
- fail(token) {
1041
- if (token !== this.token || this.state === "disposed") return;
1042
- this.state = "failed";
1043
- if (this.timing) this.timing.failures++;
1044
- this.broadcast([{
1045
- type: "error",
1046
- message: "Timeline sync interrupted; retrying"
1047
- }]);
1048
- clearTimeout(this.retryTimer);
1049
- this.retryTimer = setTimeout(() => {
1050
- if (this.state !== "failed" || token !== this.token) return;
1051
- this.subscription?.release().catch(() => {});
1052
- this.start();
1053
- }, RETRY_MS);
1054
- this.retryTimer.unref?.();
1055
- }
1056
998
  replaceWith(page) {
1057
999
  this.rows = [];
1058
1000
  this.bySeqStart.clear();
1059
- this.identities.clear();
1060
- this.pending.clear();
1061
- this.metricsTouched.clear();
1001
+ this.fingerprints.clear();
1002
+ this.rowRev.clear();
1062
1003
  this.epoch = page.epoch;
1063
- this.lastSeq = page.window.maxSeq;
1064
- this.resumeFloor = this.lastSeq;
1004
+ this.revision++;
1005
+ this.resumeFloor = this.revision;
1065
1006
  this.hasOlder = page.hasOlder;
1066
1007
  this.startCursor = page.startCursor;
1067
- this.complete = !page.hasOlder;
1068
- for (const entry of page.entries) this.upsertFetched(entry, false);
1069
- this.buffer = this.buffer.filter((e) => (e.epoch === void 0 || e.epoch === this.epoch) && e.seq > this.lastSeq);
1070
- this.pending.clear();
1071
- this.pendingReset = true;
1072
- this.scheduleFlush();
1073
- }
1074
- upsertFetched(entry, notify = true) {
1075
- const row = {
1076
- seqStart: entry.seqStart,
1077
- seqEnd: entry.seqEnd,
1078
- timestamp: entry.timestamp,
1079
- provider: entry.provider,
1080
- ...entry.turnId ? { turnId: entry.turnId } : {},
1081
- item: entry.item,
1082
- ...entry.turnMetrics?.length ? { turnMetrics: entry.turnMetrics } : {}
1083
- };
1084
- const existing = this.bySeqStart.get(row.seqStart);
1085
- if (existing) {
1086
- Object.assign(existing, row);
1087
- if (!row.turnId) delete existing.turnId;
1088
- if (!row.turnMetrics?.length) delete existing.turnMetrics;
1089
- this.noteIdentity(existing);
1090
- if (notify) this.mark(existing.seqStart, true);
1091
- return;
1008
+ for (const entry of page.entries) {
1009
+ const row = toRow(entry);
1010
+ this.insertRow(row, this.revision);
1011
+ }
1012
+ this.trimIfNeeded();
1013
+ }
1014
+ mergeTail(page) {
1015
+ if (this.rows.length > 0) {
1016
+ if (page.entries.length === 0) {
1017
+ this.replaceWith(page);
1018
+ return [this.resetOp()];
1019
+ }
1020
+ const tailFirst = page.entries[0].seqStart;
1021
+ const ourLast = this.rows.at(-1);
1022
+ if (page.hasOlder && tailFirst > ourLast.seqEnd + 1) {
1023
+ this.replaceWith(page);
1024
+ return [this.resetOp()];
1025
+ }
1026
+ const incomingStarts = new Set(page.entries.map((e) => e.seqStart));
1027
+ for (let i = this.rows.length - 1; i >= 0; i--) {
1028
+ const r = this.rows[i];
1029
+ if (r.seqStart < tailFirst) break;
1030
+ if (!incomingStarts.has(r.seqStart)) {
1031
+ this.replaceWith(page);
1032
+ return [this.resetOp()];
1033
+ }
1034
+ }
1035
+ }
1036
+ const ops = [];
1037
+ let touched = false;
1038
+ for (const entry of page.entries) {
1039
+ const row = toRow(entry);
1040
+ const fp = rowFingerprint(row);
1041
+ const existing = this.bySeqStart.get(row.seqStart);
1042
+ if (existing) {
1043
+ if (this.fingerprints.get(row.seqStart) === fp) continue;
1044
+ if (!touched) {
1045
+ this.revision++;
1046
+ touched = true;
1047
+ }
1048
+ Object.assign(existing, row);
1049
+ if (!row.turnId) delete existing.turnId;
1050
+ if (!row.turnMetrics?.length) delete existing.turnMetrics;
1051
+ this.fingerprints.set(row.seqStart, fp);
1052
+ this.rowRev.set(row.seqStart, this.revision);
1053
+ ops.push({
1054
+ type: "upsert",
1055
+ row: { ...existing },
1056
+ seq: this.revision
1057
+ });
1058
+ continue;
1059
+ }
1060
+ if (this.hasOlder && this.startCursor && row.seqStart < this.startCursor.seq) continue;
1061
+ if (!touched) {
1062
+ this.revision++;
1063
+ touched = true;
1064
+ }
1065
+ this.insertRow(row, this.revision);
1066
+ ops.push({
1067
+ type: "upsert",
1068
+ row: { ...row },
1069
+ seq: this.revision
1070
+ });
1092
1071
  }
1093
- if (this.hasOlder && this.startCursor && row.seqStart < this.startCursor.seq) return;
1094
- this.insert(row, notify);
1072
+ this.trimIfNeeded();
1073
+ return ops;
1095
1074
  }
1096
- insert(row, notify = true) {
1075
+ insertRow(row, rev) {
1076
+ const fp = rowFingerprint(row);
1097
1077
  let index = this.rows.length;
1098
1078
  if (index && this.rows[index - 1].seqStart > row.seqStart) {
1099
- let low = 0, high = index;
1079
+ let low = 0;
1080
+ let high = index;
1100
1081
  while (low < high) {
1101
- const middle = low + high >>> 1;
1102
- if (this.rows[middle].seqStart < row.seqStart) low = middle + 1;
1103
- else high = middle;
1082
+ const mid = low + high >>> 1;
1083
+ if (this.rows[mid].seqStart < row.seqStart) low = mid + 1;
1084
+ else high = mid;
1104
1085
  }
1105
1086
  index = low;
1106
1087
  }
1107
- if (index === this.rows.length) this.rows.push(row);
1108
- else this.rows.splice(index, 0, row);
1088
+ this.rows.splice(index, 0, row);
1109
1089
  this.bySeqStart.set(row.seqStart, row);
1110
- this.noteIdentity(row);
1111
- if (notify) this.mark(row.seqStart, true);
1112
- if (this.rows.length > MAX_ROWS) this.trim();
1113
- }
1114
- /** Like the daemon, an identity resolves to its most recent row. */
1115
- noteIdentity(row) {
1116
- const identity = timelineItemIdentity(row.item);
1117
- if (identity === null) return;
1118
- const current = this.identities.get(identity);
1119
- if (!current || current.seqStart <= row.seqStart) this.identities.set(identity, row);
1120
- }
1121
- trim() {
1122
- const removed = this.rows.splice(0, this.rows.length - KEEP_ROWS);
1123
- for (const row of removed) {
1124
- this.resumeFloor = Math.max(this.resumeFloor, row.seqEnd);
1090
+ this.fingerprints.set(row.seqStart, fp);
1091
+ this.rowRev.set(row.seqStart, rev);
1092
+ }
1093
+ trimIfNeeded() {
1094
+ if (this.rows.length <= MAX_ROWS) return;
1095
+ const dropped = this.rows.slice(0, this.rows.length - KEEP_ROWS);
1096
+ this.rows = this.rows.slice(this.rows.length - KEEP_ROWS);
1097
+ for (const row of dropped) {
1125
1098
  this.bySeqStart.delete(row.seqStart);
1126
- this.pending.delete(row.seqStart);
1127
- const identity = timelineItemIdentity(row.item);
1128
- if (identity !== null && this.identities.get(identity) === row) this.identities.delete(identity);
1099
+ this.fingerprints.delete(row.seqStart);
1100
+ this.rowRev.delete(row.seqStart);
1129
1101
  }
1130
- this.complete = false;
1131
1102
  this.hasOlder = true;
1132
- this.startCursor = {
1103
+ this.startCursor = this.rows[0] ? {
1133
1104
  epoch: this.epoch,
1134
1105
  seq: this.rows[0].seqStart
1135
- };
1136
- }
1137
- mark(seqStart, full, text = "") {
1138
- if (this.pendingReset || !this.watchers.size) return;
1139
- const pending = this.pending.get(seqStart);
1140
- if (!pending) this.pending.set(seqStart, {
1141
- full,
1142
- text: full ? "" : text
1143
- });
1144
- else if (full) {
1145
- pending.full = true;
1146
- pending.text = "";
1147
- } else if (!pending.full) pending.text += text;
1148
- this.scheduleFlush();
1149
- }
1150
- scheduleFlush() {
1151
- if (this.flushTimer || !this.watchers.size) return;
1152
- this.flushTimer = setTimeout(() => this.flush(), FLUSH_MS$2);
1153
- this.flushTimer.unref?.();
1154
- }
1155
- flush() {
1156
- clearTimeout(this.flushTimer);
1157
- this.flushTimer = void 0;
1158
- if (this.pendingReset) {
1159
- this.pendingReset = false;
1160
- this.pending.clear();
1161
- this.broadcast([this.resetOp()]);
1162
- return this.loaded();
1163
- }
1164
- if (!this.pending.size) return;
1165
- const ops = [];
1166
- for (const [seqStart, change] of this.pending) {
1167
- const row = this.bySeqStart.get(seqStart);
1168
- if (!row) continue;
1169
- ops.push(change.full ? {
1170
- type: "upsert",
1171
- row: { ...row }
1172
- } : {
1173
- type: "append",
1174
- seqStart,
1175
- seqEnd: row.seqEnd,
1176
- timestamp: row.timestamp,
1177
- text: change.text
1178
- });
1179
- }
1180
- this.pending.clear();
1181
- if (ops.length) this.broadcast(ops);
1106
+ } : this.startCursor;
1107
+ this.resumeFloor = this.revision;
1182
1108
  }
1183
- /**
1184
- * The rows a returning browser lacks: those from its window on that changed after `since.seq`,
1185
- * as upserts. Null when the view cannot tell (another epoch, history it no longer holds) or
1186
- * when so much changed that a reset is the smaller answer.
1187
- */
1188
1109
  resumeOps(since) {
1189
- if (since.epoch !== this.epoch || since.seq < this.resumeFloor || since.seq > this.lastSeq) return null;
1190
- const changed = this.rows.filter((row) => row.seqStart >= since.from && (row.seqEnd > since.seq || this.metricsTouched.has(row.seqStart)));
1110
+ if (since.epoch !== this.epoch || since.seq < this.resumeFloor || since.seq > this.revision) return null;
1111
+ const changed = this.rows.filter((row) => row.seqStart >= since.from && (this.rowRev.get(row.seqStart) ?? 0) > since.seq);
1191
1112
  if (changed.length > RESET_ROWS) return null;
1192
1113
  return changed.map((row) => ({
1193
1114
  type: "upsert",
1194
- row: { ...row }
1115
+ row: { ...row },
1116
+ seq: this.revision
1195
1117
  }));
1196
1118
  }
1197
1119
  resetOp() {
@@ -1206,10 +1128,9 @@ var TimelineView = class {
1206
1128
  epoch: this.epoch,
1207
1129
  seq: rows[0].seqStart
1208
1130
  } : this.startCursor,
1209
- seq: this.lastSeq
1131
+ seq: this.revision
1210
1132
  };
1211
1133
  }
1212
- /** The first rows went out: report how long the view, and anyone who joined it, waited. */
1213
1134
  loaded() {
1214
1135
  const timing = this.timing;
1215
1136
  const joiners = this.joiners;
@@ -1218,7 +1139,7 @@ var TimelineView = class {
1218
1139
  if (!timing || !this.watchers.size) return;
1219
1140
  const done = now();
1220
1141
  const base = {
1221
- subscribeMs: timing.subscribeMs,
1142
+ subscribeMs: null,
1222
1143
  fetchMs: timing.fetchMs,
1223
1144
  attempts: timing.attempts,
1224
1145
  failures: timing.failures
@@ -1234,43 +1155,6 @@ var TimelineView = class {
1234
1155
  kind: "join"
1235
1156
  });
1236
1157
  }
1237
- /**
1238
- * Pulls the last few projected entries after a turn finishes so daemon-computed turnMetrics
1239
- * (speed, tokens, cache, cost) attach to live rows without reloading the whole timeline.
1240
- */
1241
- async refreshTailMetrics(attempt = 0) {
1242
- if (this.state !== "live") return;
1243
- const token = this.token;
1244
- try {
1245
- const page = await this.driver.fetchAgentTimeline(this.agentId, {
1246
- direction: "tail",
1247
- limit: 12,
1248
- projection: "projected",
1249
- wholeTurns: false
1250
- });
1251
- if (token !== this.token || this.state !== "live" || page.epoch !== this.epoch) return;
1252
- let updated = false;
1253
- let latestHasMetrics = false;
1254
- const latestAssistant = [...page.entries].reverse().find((e) => e.item.type === "assistant_message" || e.item.type === "tool_call");
1255
- for (const entry of page.entries) {
1256
- if (!entry.turnMetrics?.length) continue;
1257
- if (latestAssistant && entry.seqStart === latestAssistant.seqStart) latestHasMetrics = true;
1258
- const existing = this.bySeqStart.get(entry.seqStart) ?? this.rows.find((r) => r.seqStart <= entry.seqEnd && r.seqEnd >= entry.seqStart);
1259
- if (!existing) continue;
1260
- if (JSON.stringify(existing.turnMetrics ?? null) === JSON.stringify(entry.turnMetrics)) continue;
1261
- existing.turnMetrics = entry.turnMetrics;
1262
- this.metricsTouched.add(existing.seqStart);
1263
- if (this.metricsTouched.size > 16) {
1264
- const oldest = this.metricsTouched.values().next().value;
1265
- if (oldest !== void 0) this.metricsTouched.delete(oldest);
1266
- }
1267
- this.mark(existing.seqStart, true);
1268
- updated = true;
1269
- }
1270
- if (updated) this.flush();
1271
- if (!latestHasMetrics && attempt < 2) setTimeout(() => void this.refreshTailMetrics(attempt + 1), attempt === 0 ? 600 : 1500).unref?.();
1272
- } catch {}
1273
- }
1274
1158
  broadcast(ops) {
1275
1159
  notifyListeners(this.watchers, ops);
1276
1160
  }
@@ -1343,6 +1227,10 @@ var HostSubagents = class {
1343
1227
  this.releaseUse();
1344
1228
  };
1345
1229
  }
1230
+ /** Current status of a provider subagent, if known. */
1231
+ statusOf(parentAgentId, subagentId) {
1232
+ return this.parents.get(parentAgentId)?.subagents.get(subagentId)?.status;
1233
+ }
1346
1234
  /** A TimelineView driver for one subagent conversation. */
1347
1235
  timelineDriver(parentAgentId, subagentId) {
1348
1236
  const key = `${parentAgentId}\0${subagentId}`;
@@ -1789,7 +1677,14 @@ var Hosts = class {
1789
1677
  attach(hostId, driver, api) {
1790
1678
  this.assertDetached(hostId);
1791
1679
  const directory = new HostDirectory(hostId, driver, this.agents, () => this.emit(this.agentListeners), (previous, next) => {
1792
- if (previous && next && (previous.turnActive || previous.state === "running") && !next.turnActive && next.state !== "running") this.timelines.get(next.key)?.view.refreshTailMetrics();
1680
+ if (next) {
1681
+ const view = this.timelines.get(next.key)?.view;
1682
+ if (view) {
1683
+ const running = next.turnActive === true || next.state === "running" || next.status === "running" || next.status === "initializing";
1684
+ view.setActive(running);
1685
+ if (!running && previous && (previous.activityAt !== next.activityAt || previous.state !== next.state)) view.wake();
1686
+ }
1687
+ }
1793
1688
  notifyListeners(this.transitionListeners, previous, next);
1794
1689
  });
1795
1690
  const workspaces = new HostWorkspaces(hostId, driver, this.workspaces, () => this.emit(this.agentListeners), this.emptyProjects);
@@ -1802,6 +1697,7 @@ var Hosts = class {
1802
1697
  });
1803
1698
  this.diagnostics.set(hostId, { lastConnectedAt: (/* @__PURE__ */ new Date()).toISOString() });
1804
1699
  this.clients.get(hostId)?.backgroundWork.reconnect();
1700
+ for (const entry of this.timelines.values()) if (entry.hostId === hostId && entry.view.watcherCount > 0) entry.view.wake();
1805
1701
  } else if (state.status === "connecting") this.diagnostics.set(hostId, {
1806
1702
  ...previous,
1807
1703
  nextRetryAt: void 0,
@@ -1929,9 +1825,21 @@ var Hosts = class {
1929
1825
  this.hostListeners.add(listener);
1930
1826
  return () => this.hostListeners.delete(listener);
1931
1827
  }
1828
+ /** Wakes a watched timeline view immediately (e.g. after sending a message or rewinding). */
1829
+ wakeTimeline(hostId, agentId, followUp = false) {
1830
+ const view = this.timelines.get(`${hostId}:${agentId}`)?.view;
1831
+ if (!view) return;
1832
+ view.wake(followUp);
1833
+ }
1932
1834
  /** The provider subagents of one agent, as they change. */
1933
1835
  watchSubagents(hostId, agentId, listener) {
1934
- return this.get(hostId).subagents.watch(agentId, listener);
1836
+ return this.get(hostId).subagents.watch(agentId, (subagents) => {
1837
+ for (const s of subagents) {
1838
+ const subView = this.timelines.get(`${hostId}:${agentId}:subagent:${s.id}`)?.view;
1839
+ if (subView) subView.setActive(s.status === "running");
1840
+ }
1841
+ listener(subagents);
1842
+ });
1935
1843
  }
1936
1844
  /** Background tasks and heartbeats of one agent, as they change. */
1937
1845
  watchBackgroundWork(hostId, agentId, listener) {
@@ -1961,6 +1869,9 @@ var Hosts = class {
1961
1869
  ...report
1962
1870
  });
1963
1871
  });
1872
+ const agent = subagentId ? void 0 : this.agents.get(key);
1873
+ const running = subagentId ? client.subagents.statusOf(agentId, subagentId) === "running" : !!(agent && (agent.turnActive === true || agent.state === "running" || agent.status === "running" || agent.status === "initializing"));
1874
+ view.setActive(running);
1964
1875
  entry = {
1965
1876
  view,
1966
1877
  hostId,
@@ -2003,7 +1914,7 @@ var Hosts = class {
2003
1914
  if (waiting && ops.some((op) => op.type === "reset")) finishWait(false);
2004
1915
  watcher(ops);
2005
1916
  }, since);
2006
- if (joined.state === "live" || joined.state === "catching-up") finishWait(false);
1917
+ if (joined.state === "live") finishWait(false);
2007
1918
  const current = entry;
2008
1919
  return () => {
2009
1920
  finishWait(true);
@@ -5455,6 +5366,7 @@ function registerTimelineRoutes(app, { store, hosts, streamValidity, encodingOf
5455
5366
  if (!rewindModes(facts.capabilities).includes(mode)) return c.json({ error: "This agent cannot rewind that way." }, 400);
5456
5367
  try {
5457
5368
  await client.driver.rewindAgent(c.req.param("agent"), messageId, mode);
5369
+ hosts.wakeTimeline(c.req.param("host"), c.req.param("agent"), true);
5458
5370
  return c.json({ ok: true });
5459
5371
  } catch (error) {
5460
5372
  return c.json({ error: errorMessage(error, "Rewind failed") }, 502);
@@ -5656,6 +5568,7 @@ function registerMessageRoutes(app, { store, hosts, attachments, sendQueue, stre
5656
5568
  ...prepared.attachments.length ? { attachments: prepared.attachments } : {}
5657
5569
  });
5658
5570
  attachments.consumed(input.attachmentIds);
5571
+ hosts.wakeTimeline(host, agent, true);
5659
5572
  return c.json({
5660
5573
  status: "acknowledged",
5661
5574
  messageId: input.messageId