@songsid/agend 2.4.0-beta.20 → 2.4.0-beta.22

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 (256) hide show
  1. package/dist/agent-cli-instructions.md +1 -1
  2. package/dist/backend/claude-carry.d.ts +11 -0
  3. package/dist/backend/claude-carry.js +88 -6
  4. package/dist/backend/claude-carry.js.map +1 -1
  5. package/dist/backend/claude-code.js +3 -0
  6. package/dist/backend/claude-code.js.map +1 -1
  7. package/dist/build-provenance.json +1 -1
  8. package/dist/cache-ledger.d.ts +23 -21
  9. package/dist/cache-ledger.js +70 -6
  10. package/dist/cache-ledger.js.map +1 -1
  11. package/dist/cache-prices.d.ts +8 -6
  12. package/dist/cache-prices.js +12 -5
  13. package/dist/cache-prices.js.map +1 -1
  14. package/dist/cache-service.d.ts +9 -5
  15. package/dist/cache-service.js +202 -50
  16. package/dist/cache-service.js.map +1 -1
  17. package/dist/channel/mcp-tools.js +6 -4
  18. package/dist/channel/mcp-tools.js.map +1 -1
  19. package/dist/cli-audited.js +2 -1
  20. package/dist/cli-audited.js.map +1 -1
  21. package/dist/cli.js +4 -1
  22. package/dist/cli.js.map +1 -1
  23. package/dist/contact-auth-policy.js +1 -1
  24. package/dist/contact-auth-policy.js.map +1 -1
  25. package/dist/contact-auth-service.js +8 -2
  26. package/dist/contact-auth-service.js.map +1 -1
  27. package/dist/contact-auth-store.d.ts +3 -0
  28. package/dist/contact-auth-store.js +34 -8
  29. package/dist/contact-auth-store.js.map +1 -1
  30. package/dist/contact-auth-types.d.ts +3 -2
  31. package/dist/contact-auth-types.js.map +1 -1
  32. package/dist/contact-change-proposals.d.ts +47 -0
  33. package/dist/contact-change-proposals.js +132 -0
  34. package/dist/contact-change-proposals.js.map +1 -0
  35. package/dist/daemon.d.ts +31 -0
  36. package/dist/daemon.js +149 -2
  37. package/dist/daemon.js.map +1 -1
  38. package/dist/delivery-outbox.d.ts +10 -0
  39. package/dist/delivery-outbox.js +21 -0
  40. package/dist/delivery-outbox.js.map +1 -1
  41. package/dist/fleet-manager.d.ts +65 -1
  42. package/dist/fleet-manager.js +338 -20
  43. package/dist/fleet-manager.js.map +1 -1
  44. package/dist/general-knowledge/skills/concepts/SKILL.md +2 -1
  45. package/dist/general-knowledge/skills/hub/SKILL.md +45 -5
  46. package/dist/general-knowledge/skills/task-board/SKILL.md +4 -0
  47. package/dist/general-knowledge/skills/web-appearance/SKILL.md +1 -1
  48. package/dist/general-knowledge/skills/web-dashboard/SKILL.md +16 -6
  49. package/dist/hub/agent-pictures-model.d.ts +275 -0
  50. package/dist/hub/agent-pictures-model.js +103 -0
  51. package/dist/hub/agent-pictures-model.js.map +1 -0
  52. package/dist/hub/agent-pictures.d.ts +64 -0
  53. package/dist/hub/agent-pictures.js +255 -0
  54. package/dist/hub/agent-pictures.js.map +1 -0
  55. package/dist/hub/client.d.ts +4 -0
  56. package/dist/hub/client.js +23 -2
  57. package/dist/hub/client.js.map +1 -1
  58. package/dist/hub/compose/member.d.ts +14 -3
  59. package/dist/hub/compose/member.js +46 -10
  60. package/dist/hub/compose/member.js.map +1 -1
  61. package/dist/hub/compose/server.d.ts +6 -5
  62. package/dist/hub/compose/server.js +7 -3
  63. package/dist/hub/compose/server.js.map +1 -1
  64. package/dist/hub/direct-posts-model.d.ts +1 -0
  65. package/dist/hub/direct-posts-model.js +2 -0
  66. package/dist/hub/direct-posts-model.js.map +1 -0
  67. package/dist/hub/direct-posts.d.ts +36 -0
  68. package/dist/hub/direct-posts.js +145 -0
  69. package/dist/hub/direct-posts.js.map +1 -0
  70. package/dist/hub/http/compose.d.ts +5 -0
  71. package/dist/hub/http/compose.js +158 -20
  72. package/dist/hub/http/compose.js.map +1 -1
  73. package/dist/hub/http/core.d.ts +9 -1
  74. package/dist/hub/http/core.js +21 -1
  75. package/dist/hub/http/core.js.map +1 -1
  76. package/dist/hub/http/deps.d.ts +1 -0
  77. package/dist/hub/http/schema.js +7 -0
  78. package/dist/hub/http/schema.js.map +1 -1
  79. package/dist/hub/init/text.js +1 -1
  80. package/dist/hub/join/service.d.ts +2 -0
  81. package/dist/hub/join/service.js +7 -3
  82. package/dist/hub/join/service.js.map +1 -1
  83. package/dist/hub/member/agent-pictures.d.ts +76 -0
  84. package/dist/hub/member/agent-pictures.js +196 -0
  85. package/dist/hub/member/agent-pictures.js.map +1 -0
  86. package/dist/hub/member/direct-rooms.d.ts +9 -0
  87. package/dist/hub/member/direct-rooms.js +14 -0
  88. package/dist/hub/member/direct-rooms.js.map +1 -0
  89. package/dist/hub/member/room-ask.d.ts +22 -1
  90. package/dist/hub/member/room-ask.js +99 -5
  91. package/dist/hub/member/room-ask.js.map +1 -1
  92. package/dist/hub/member/room-asker-store.d.ts +48 -0
  93. package/dist/hub/member/room-asker-store.js +181 -0
  94. package/dist/hub/member/room-asker-store.js.map +1 -0
  95. package/dist/hub/member/room-post.d.ts +11 -10
  96. package/dist/hub/member/room-post.js +51 -7
  97. package/dist/hub/member/room-post.js.map +1 -1
  98. package/dist/hub/policy.d.ts +11 -2
  99. package/dist/hub/policy.js +60 -7
  100. package/dist/hub/policy.js.map +1 -1
  101. package/dist/hub/protocol.d.ts +254 -1
  102. package/dist/hub/protocol.js +20 -8
  103. package/dist/hub/protocol.js.map +1 -1
  104. package/dist/hub/representative.d.ts +38 -0
  105. package/dist/hub/representative.js +140 -12
  106. package/dist/hub/representative.js.map +1 -1
  107. package/dist/hub/room-asker-identity-model.d.ts +53 -0
  108. package/dist/hub/room-asker-identity-model.js +14 -0
  109. package/dist/hub/room-asker-identity-model.js.map +1 -0
  110. package/dist/hub/room-asker-identity.d.ts +22 -0
  111. package/dist/hub/room-asker-identity.js +43 -0
  112. package/dist/hub/room-asker-identity.js.map +1 -0
  113. package/dist/hub/rooms/agents.d.ts +2 -0
  114. package/dist/hub/rooms/agents.js +30 -3
  115. package/dist/hub/rooms/agents.js.map +1 -1
  116. package/dist/hub/rooms/asker-identity.d.ts +21 -0
  117. package/dist/hub/rooms/asker-identity.js +86 -0
  118. package/dist/hub/rooms/asker-identity.js.map +1 -0
  119. package/dist/hub/rooms/direct-view.d.ts +3 -2
  120. package/dist/hub/rooms/direct-view.js +23 -8
  121. package/dist/hub/rooms/direct-view.js.map +1 -1
  122. package/dist/hub/rooms/office.d.ts +27 -0
  123. package/dist/hub/rooms/office.js +12 -2
  124. package/dist/hub/rooms/office.js.map +1 -1
  125. package/dist/hub/rooms/service.d.ts +38 -1
  126. package/dist/hub/rooms/service.js +56 -3
  127. package/dist/hub/rooms/service.js.map +1 -1
  128. package/dist/hub/rooms/types.d.ts +5 -2
  129. package/dist/hub/rooms.d.ts +9 -0
  130. package/dist/hub/rooms.js +97 -26
  131. package/dist/hub/rooms.js.map +1 -1
  132. package/dist/hub/runtime.d.ts +1 -1
  133. package/dist/hub/server.js +24 -2
  134. package/dist/hub/server.js.map +1 -1
  135. package/dist/hub/session-directory-transition.d.ts +1 -0
  136. package/dist/hub/session-directory-transition.js +15 -9
  137. package/dist/hub/session-directory-transition.js.map +1 -1
  138. package/dist/hub/session-limits.d.ts +58 -0
  139. package/dist/hub/session-limits.js +121 -0
  140. package/dist/hub/session-limits.js.map +1 -0
  141. package/dist/hub/sessions.d.ts +1 -1
  142. package/dist/hub/sessions.js +13 -7
  143. package/dist/hub/sessions.js.map +1 -1
  144. package/dist/hub/settings-directory-decision.js +1 -1
  145. package/dist/hub/settings-directory-decision.js.map +1 -1
  146. package/dist/hub/types.d.ts +9 -2
  147. package/dist/hub-character-plan.d.ts +47 -0
  148. package/dist/hub-character-plan.js +70 -0
  149. package/dist/hub-character-plan.js.map +1 -0
  150. package/dist/hub-contact-windows.d.ts +13 -1
  151. package/dist/hub-contact-windows.js +35 -2
  152. package/dist/hub-contact-windows.js.map +1 -1
  153. package/dist/hub-picture-reduce.d.ts +34 -0
  154. package/dist/hub-picture-reduce.js +101 -0
  155. package/dist/hub-picture-reduce.js.map +1 -0
  156. package/dist/hub-picture-worker.d.ts +1 -0
  157. package/dist/hub-picture-worker.js +19 -0
  158. package/dist/hub-picture-worker.js.map +1 -0
  159. package/dist/hub-report-target.d.ts +6 -0
  160. package/dist/hub-report-target.js +6 -0
  161. package/dist/hub-report-target.js.map +1 -1
  162. package/dist/locale.js +357 -265
  163. package/dist/locale.js.map +1 -1
  164. package/dist/ls-rows.d.ts +2 -0
  165. package/dist/ls-rows.js.map +1 -1
  166. package/dist/needs-you.d.ts +6 -0
  167. package/dist/needs-you.js +3 -1
  168. package/dist/needs-you.js.map +1 -1
  169. package/dist/outbound-handlers.d.ts +8 -0
  170. package/dist/outbound-handlers.js +53 -6
  171. package/dist/outbound-handlers.js.map +1 -1
  172. package/dist/outbound-schemas.d.ts +2 -0
  173. package/dist/outbound-schemas.js +6 -4
  174. package/dist/outbound-schemas.js.map +1 -1
  175. package/dist/runtime-cli-install.js +4 -4
  176. package/dist/runtime-cli-install.js.map +1 -1
  177. package/dist/task-board-tool.d.ts +17 -0
  178. package/dist/task-board-tool.js +58 -6
  179. package/dist/task-board-tool.js.map +1 -1
  180. package/dist/tips.js +3 -3
  181. package/dist/tool-permissions.js +2 -2
  182. package/dist/tool-permissions.js.map +1 -1
  183. package/dist/ui/agent-export-strings.js +1 -1
  184. package/dist/ui/agent-page-strings.js +5 -5
  185. package/dist/ui/contact-auth-client.js +2 -2
  186. package/dist/ui/contact-tool-groups.js +1 -1
  187. package/dist/ui/fleet-cache.js +5 -5
  188. package/dist/ui/fleet-canvas.js +3 -2
  189. package/dist/ui/fleet-office-card.js +46 -7
  190. package/dist/ui/fleet-office-dialogue.d.ts +6 -3
  191. package/dist/ui/fleet-office-dialogue.js +13 -3
  192. package/dist/ui/fleet-office-layout.js +15 -15
  193. package/dist/ui/fleet-office-model.js +3 -2
  194. package/dist/ui/fleet-office-overview.js +1 -1
  195. package/dist/ui/fleet-office-pack.d.ts +17 -7
  196. package/dist/ui/fleet-office-pack.js +106 -38
  197. package/dist/ui/fleet-office-play.js +11 -4
  198. package/dist/ui/fleet-office-skin.js +41 -11
  199. package/dist/ui/fleet-office-store.js +10 -1
  200. package/dist/ui/fleet-office-talk.js +423 -0
  201. package/dist/ui/fleet-office-view.js +58 -17
  202. package/dist/ui/fleet-office-zones.js +20 -12
  203. package/dist/ui/fleet-office.js +250 -65
  204. package/dist/ui/fleet-org.js +3 -3
  205. package/dist/ui/fleet-task-common.js +3 -3
  206. package/dist/ui/fleet-task-sort.js +88 -0
  207. package/dist/ui/fleet-task-view.js +38 -9
  208. package/dist/ui/fleet-tasks.js +47 -15
  209. package/dist/ui/fleet-usage.js +6 -6
  210. package/dist/ui/hub-admin-overview.js +1 -1
  211. package/dist/ui/hub-chat-store.js +4 -3
  212. package/dist/ui/hub-chat-strings.js +3 -1
  213. package/dist/ui/hub-client.js +3 -0
  214. package/dist/ui/hub-my-agents-strings.js +2 -2
  215. package/dist/ui/hub-my-agents.js +3 -0
  216. package/dist/ui/hub-room-composer.js +24 -4
  217. package/dist/ui/hub-room-members.js +7 -3
  218. package/dist/ui/hub-room-office.js +71 -2
  219. package/dist/ui/hub-room-presence.js +14 -0
  220. package/dist/ui/hub-room-strings.js +18 -2
  221. package/dist/ui/hub-room-thread.js +82 -13
  222. package/dist/ui/hub-room-working.js +37 -0
  223. package/dist/ui/hub-session-limits.js +79 -0
  224. package/dist/ui/panel-fleet.js +5 -5
  225. package/dist/ui/panel-hub.js +41 -6
  226. package/dist/ui/panel-log.js +3 -3
  227. package/dist/ui/panel-needs.js +28 -2
  228. package/dist/ui/panel-runtimes.js +2 -2
  229. package/dist/ui/runtime-cli-install.js +1 -1
  230. package/dist/ui/runtime-update-status.js +1 -1
  231. package/dist/ui/settings-access-strings.js +1 -1
  232. package/dist/ui/settings-hub-setup-strings.js +1 -1
  233. package/dist/ui/settings-layout.js +2 -2
  234. package/dist/ui/settings-named-test.js +16 -16
  235. package/dist/ui/settings-public-session-strings.js +2 -2
  236. package/dist/ui/settings-revoke-strings.js +1 -1
  237. package/dist/ui/settings-shared-strings.js +2 -2
  238. package/dist/ui/settings-strings.js +40 -40
  239. package/dist/ui/shared/app-shell.js +1 -1
  240. package/dist/ui/shared/app.css +106 -6
  241. package/dist/ui/shared/chat-strings.js +9 -9
  242. package/dist/ui/shared/hub-owner.css +5 -0
  243. package/dist/ui/shared/hub-rooms-u2.css +3 -0
  244. package/dist/ui/shared/instance-nav.js +36 -2
  245. package/dist/ui/shared/office-dialogue.css +110 -0
  246. package/dist/ui/shared/task-board.css +103 -7
  247. package/dist/ui/shared/view-strings.js +27 -9
  248. package/dist/web-api.d.ts +5 -0
  249. package/dist/web-api.js +29 -0
  250. package/dist/web-api.js.map +1 -1
  251. package/dist/web-office-skins.d.ts +8 -2
  252. package/dist/web-office-skins.js +33 -14
  253. package/dist/web-office-skins.js.map +1 -1
  254. package/dist/web-office-store.js +5 -3
  255. package/dist/web-office-store.js.map +1 -1
  256. package/package.json +1 -1
@@ -17,9 +17,9 @@
17
17
  * thread (kiro-usage-run.ts) — for the usage report only; the cache analysis leaves Kiro out.
18
18
  */
19
19
  import { mkdir, open, readdir, readFile, realpath, rename, stat, writeFile } from "node:fs/promises";
20
- import { dirname, join, resolve } from "node:path";
20
+ import { basename, dirname, join, resolve } from "node:path";
21
21
  import { scanJsonl } from "./cache-scan.js";
22
- import { addSpeed, addTurn, claudeLine, codexLine, emptyLedger, grokLine, museLine, KEEP_DAYS, pruneLedger, reviveLedger } from "./cache-ledger.js";
22
+ import { addSpeed, addTurn, claudeLine, codexLine, codexRebuild, emptyLedger, grokLine, museLine, KEEP_DAYS, pruneLedger, reviveLedger } from "./cache-ledger.js";
23
23
  import { analyzeLedger } from "./cache-analysis.js";
24
24
  import { PRICES_CHECKED, PRICE_SOURCES } from "./cache-prices.js";
25
25
  import { addCoverage, carryPruned, foldHours, pruneDays, readDays, readLifetime, writeDays } from "./usage-rollup.js";
@@ -30,6 +30,10 @@ import { profileAt, readProfileTimeline } from "./usage/profile-timeline.js";
30
30
  export const WINDOWS = { "24h": 86_400_000, "7d": 7 * 86_400_000, "30d": 30 * 86_400_000 };
31
31
  /** A window name as asked (a query string, a tool argument); anything unknown is the default, 7 days. */
32
32
  export const cacheWindow = (w) => (Object.prototype.hasOwnProperty.call(WINDOWS, w) ? w : "7d");
33
+ const rolloutSessionId = (path) => {
34
+ const stem = basename(path, ".jsonl");
35
+ return /^rollout-.*-([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})$/i.exec(stem)?.[1] ?? stem;
36
+ };
33
37
  const DAY = 86_400_000;
34
38
  const META_MAX_BYTES = 256 * 1024; // session_meta carries the instructions (~22 KB in real rollouts)
35
39
  const kindOf = (backend) => (backend === "claude-code" ? "claude" : backend === "codex" ? "codex" : backend === "kiro-cli" ? "kiro" : backend === "grok" ? "grok" : backend === "muse" ? "muse" : backend === "opencode" ? "opencode" : backend === "antigravity" ? "antigravity" : null);
@@ -125,7 +129,7 @@ export class CacheService {
125
129
  for (;;) {
126
130
  const left = await this.pass();
127
131
  if (left === 0) {
128
- this.caughtUp = true;
132
+ this.caughtUp = ![...this.ledgers.values()].some(l => l.codexRebuild);
129
133
  await this.recordCoverage();
130
134
  }
131
135
  // Stop looping when: all caught up, explicitly stopped, or closing (stop()
@@ -286,17 +290,48 @@ export class CacheService {
286
290
  await this.keepHistory(inst, cutoff);
287
291
  continue;
288
292
  }
289
- const ledger = await this.ledgerFor(inst);
293
+ const published = await this.ledgerFor(inst);
294
+ // A bounded, restartable replacement generation repairs already inflated recent hours. Do not publish a
295
+ // partially read replacement, nor tag old totals as repaired merely because new dedupe was enabled.
296
+ let ledger = kind === "codex" && (published.codexMethod !== 1 || published.codexRebuild)
297
+ ? (published.codexRebuild ??= codexRebuild(published, cutoff)) : published;
290
298
  if (kind === "kiro" || kind === "opencode" || kind === "antigravity") {
291
299
  pruneLedger(ledger, cutoff, await this.foldBeforePrune(inst, ledger, cutoff));
292
300
  continue;
293
301
  }
294
302
  const files = kind === "claude" ? await this.claudeFiles(inst, cutoff) : kind === "grok" ? await this.grokFiles(inst, cutoff)
295
303
  : kind === "muse" ? muse.get(inst.name) ?? [] : codex.get(inst.name) ?? [];
304
+ const listed = new Set(files.map((f) => f.path));
305
+ if (kind === "codex")
306
+ for (const generation of new Set([published, ledger])) {
307
+ for (const f of files) {
308
+ if (generation.files[f.path])
309
+ continue;
310
+ const id = "sessionId" in f && typeof f.sessionId === "string" ? f.sessionId : rolloutSessionId(f.path);
311
+ const moved = Object.entries(generation.files).find(([path, cur]) => !listed.has(path) && cur.kind === "codex" && cur.ino === f.ino && (cur.sessionId ?? rolloutSessionId(path)) === id);
312
+ if (!moved)
313
+ continue;
314
+ const [before, cur] = moved;
315
+ generation.files[f.path] = cur;
316
+ delete generation.files[before];
317
+ for (const event of Object.values(generation.codexEvents ?? {}))
318
+ if (event.path === before)
319
+ event.path = f.path;
320
+ this.dirty.add(inst.name);
321
+ }
322
+ }
323
+ if (kind === "codex" && ledger === published && files.some(f => "parentPath" in f && f.parentPath && published.files[f.path]?.orphaned)) {
324
+ ledger = published.codexRebuild = codexRebuild(published, cutoff);
325
+ }
296
326
  // A listing that could not read everything is no inventory: a cursor it missed stays, or the file would be counted
297
327
  // again from its start once it is listed (Prism #2605). An instance whose store stays unreadable keeps its cursors
298
328
  // until it is readable again — bounded by its own files.
299
- const listed = new Set(files.map((f) => f.path));
329
+ const terminalParents = new Set(files.flatMap(f => "missingParent" in f && typeof f.missingParent === "string" ? [f.missingParent] : []));
330
+ if (ledger !== published && Object.entries(published.files).some(([path, f]) => f.kind === "codex" && (f.last?.t ?? 0) >= cutoff && !listed.has(path) && !terminalParents.has(f.sessionId ?? rolloutSessionId(path)))) {
331
+ // A repair cannot turn unavailable recent source data into a measured zero. Keep the published generation;
332
+ // a later complete inventory can resume the candidate without erasing the only surviving accounting.
333
+ this.unread.add(inst.name);
334
+ }
300
335
  // #3066 §1d: a Codex turn's profile is the one its run was launched under (read once a pass, off the thread).
301
336
  const spans = kind === "codex" && inst.profileTimelinePath ? await readProfileTimeline(inst.profileTimelinePath) : null;
302
337
  const profileOf = spans && spans.length ? (t) => profileAt(spans, t) : inst.profile;
@@ -306,6 +341,7 @@ export class CacheService {
306
341
  delete ledger.files[path];
307
342
  this.dirty.add(inst.name);
308
343
  }
344
+ let instanceBehind = false;
309
345
  for (const f of files) {
310
346
  let cur = ledger.files[f.path];
311
347
  if (!cur) {
@@ -316,17 +352,32 @@ export class CacheService {
316
352
  cur = this.followFromEnd(ledger, f.path, cur, f.ino, f.size);
317
353
  this.dirty.add(inst.name);
318
354
  }
355
+ if ("sessionId" in f && typeof f.sessionId === "string" && cur.sessionId !== f.sessionId) {
356
+ cur.sessionId = f.sessionId;
357
+ this.dirty.add(inst.name);
358
+ }
359
+ if ("orphaned" in f && f.orphaned)
360
+ cur.orphaned = true;
361
+ else
362
+ delete cur.orphaned;
319
363
  if (cur.offset >= f.size)
320
364
  continue;
321
365
  // Only an unfinished line was left, and the file has not grown since: nothing new to read.
322
366
  if (cur.stalled === f.size)
323
367
  continue;
324
- if (budget <= 0) {
368
+ const parentPath = "parentPath" in f ? f.parentPath : undefined;
369
+ const parent = parentPath ? files.find(p => p.path === parentPath) : undefined;
370
+ const parentCursor = parent ? ledger.files[parent.path] : undefined;
371
+ const waitingForParent = parent && (parentCursor?.offset ?? 0) < parent.size && parentCursor?.stalled !== parent.size;
372
+ if (waitingForParent && this.unread.has(inst.name))
373
+ continue;
374
+ if (budget <= 0 || waitingForParent) {
325
375
  behindBytes += f.size - cur.offset;
326
376
  behindFiles++;
377
+ instanceBehind = true;
327
378
  continue;
328
379
  }
329
- const done = await this.read(ledger, cur, f.path, budget, cutoff, profileOf);
380
+ const done = await this.read(ledger, cur, f.path, budget, cutoff, profileOf, "identityOnly" in f && f.identityOnly === true);
330
381
  if (done.failed)
331
382
  this.unread.add(inst.name);
332
383
  this.dirty.add(inst.name);
@@ -334,8 +385,22 @@ export class CacheService {
334
385
  if (done.behind > 0) {
335
386
  behindBytes += done.behind;
336
387
  behindFiles++;
388
+ instanceBehind = true;
337
389
  }
338
390
  }
391
+ if (kind === "codex" && !this.unread.has(inst.name)) {
392
+ // Retain old fingerprints as long as an inventoried descendant still references them, including ancient
393
+ // inherited prefixes. Pruning by event date would recount an old prefix when a new fork appears today.
394
+ for (const [key, event] of Object.entries(ledger.codexEvents ?? {}))
395
+ if (!ledger.files[event.path])
396
+ delete ledger.codexEvents[key];
397
+ }
398
+ if (ledger !== published) {
399
+ this.dirty.add(inst.name);
400
+ if (instanceBehind || this.unread.has(inst.name))
401
+ continue;
402
+ this.ledgers.set(inst.name, ledger);
403
+ }
339
404
  // #2122: fold, then prune — the ledger never drops an hour the rollup does not durably hold.
340
405
  pruneLedger(ledger, cutoff, await this.foldBeforePrune(inst, ledger, cutoff));
341
406
  }
@@ -640,7 +705,7 @@ export class CacheService {
640
705
  next.ttl = cur.ttl;
641
706
  return ledger.files[path] = next;
642
707
  }
643
- async read(ledger, cur, path, budget, cutoff, profile) {
708
+ async read(ledger, cur, path, budget, cutoff, profile, identityOnly = false) {
644
709
  const pf = (t) => (typeof profile === "function" ? profile(t) : profile);
645
710
  try {
646
711
  const res = await scanJsonl(path, cur.offset, { maxBytes: budget, ino: cur.ino }, (line) => {
@@ -650,13 +715,30 @@ export class CacheService {
650
715
  return;
651
716
  }
652
717
  const turn = cur.kind === "claude" ? claudeLine(line, cur) : cur.kind === "muse" ? museLine(line, cur) : codexLine(line, cur);
718
+ let replay = identityOnly;
719
+ if (turn?.eventKey) {
720
+ const events = ledger.codexEvents ??= dict();
721
+ const original = events[turn.eventKey];
722
+ if (original !== undefined) {
723
+ replay = true;
724
+ turn.t = original.t;
725
+ if (!ledger.files[original.path])
726
+ original.path = path;
727
+ }
728
+ else
729
+ events[turn.eventKey] = { t: turn.t, path };
730
+ }
731
+ // Mark only the response matched by this count, not other responses sharing its timestamp.
732
+ if (replay && turn?.codexPending)
733
+ turn.codexPending.replay = true;
653
734
  if (turn)
654
- addTurn(ledger, cur, turn, cutoff, pf(turn.t));
735
+ addTurn(ledger, cur, turn, cutoff, pf(turn.t), !replay);
655
736
  // #3066 §2: responses timed by these lines (their last block seen) — counted into their hour, under the profile in
656
737
  // effect at their time (§1d), then dropped.
657
738
  if (cur.speeds?.length) {
658
- for (const s of cur.speeds)
659
- addSpeed(ledger, cur, s, cutoff, pf(s.t));
739
+ if (!identityOnly)
740
+ for (const s of cur.speeds)
741
+ addSpeed(ledger, cur, s, cutoff, pf(s.t));
660
742
  delete cur.speeds;
661
743
  }
662
744
  });
@@ -823,10 +905,10 @@ export class CacheService {
823
905
  this.museWorkspace.delete(p);
824
906
  return out;
825
907
  }
826
- /** Codex rollouts of the last KEEP_DAYS, by the instance whose working directory their session_meta names. */
908
+ /** Recent Codex rollouts, with their ancestors first even when those ancestors have old mtimes. */
827
909
  async codexFilesByInstance(insts, cutoff) {
828
910
  const out = new Map();
829
- const codexInsts = insts.filter((i) => kindOf(i.backend) === "codex");
911
+ const codexInsts = insts.filter(i => kindOf(i.backend) === "codex");
830
912
  if (!codexInsts.length)
831
913
  return out;
832
914
  const byCwd = new Map();
@@ -839,63 +921,132 @@ export class CacheService {
839
921
  for (const [p, v] of Object.entries(JSON.parse(await readFile(this.o.metaPath, "utf8")))) {
840
922
  const e = v;
841
923
  if (e && typeof e.ino === "number" && (typeof e.cwd === "string" || e.cwd === null))
842
- this.meta[p] = { ino: e.ino, cwd: e.cwd };
924
+ this.meta[p] = {
925
+ ino: e.ino, cwd: e.cwd,
926
+ ...(typeof e.id === "string" || e.id === null ? { id: e.id } : {}),
927
+ ...(typeof e.parent === "string" || e.parent === null ? { parent: e.parent } : {}),
928
+ };
843
929
  }
844
930
  }
845
931
  catch { /* none yet */ }
846
932
  }
847
- let rollouts = [];
848
- // A listing that could not read part of the tree may miss any codex instance's rollouts: none of them is covered.
849
- const unlisted = () => { for (const i of codexInsts)
933
+ const rollouts = new Map();
934
+ let complete = true;
935
+ const unlisted = () => { complete = false; for (const i of codexInsts)
850
936
  this.unread.add(i.name); };
851
- try {
852
- const root = this.o.codexSessionsDir();
853
- rollouts = this.o.listRollouts(root).filter((r) => r.mtimeMs >= cutoff);
854
- if ((this.o.rolloutsUnreadable?.(root) ?? 0) > 0)
855
- unlisted();
937
+ const root = this.o.codexSessionsDir();
938
+ for (const dir of new Set([root, join(dirname(root), "archived_sessions")])) {
939
+ try {
940
+ for (const r of this.o.listRollouts(dir))
941
+ if (!rollouts.has(r.path))
942
+ rollouts.set(r.path, { ...r, archived: dir !== root });
943
+ if ((this.o.rolloutsUnreadable?.(dir) ?? 0) > 0)
944
+ unlisted();
945
+ }
946
+ catch (err) {
947
+ if (!gone(err))
948
+ unlisted();
949
+ }
856
950
  }
857
- catch (err) {
858
- if (!gone(err))
859
- unlisted(); /* else no sessions yet */
951
+ const live = new Set(rollouts.keys());
952
+ const inventory = new Map();
953
+ const sessions = new Map();
954
+ // Codex filenames carry the session ID. Cached header IDs support older/custom names. Only recent files and
955
+ // ancestors they reference are stat'ed/read here; the shared listing already maintains the historical inventory.
956
+ for (const r of rollouts.values()) {
957
+ const id = rolloutSessionId(r.path);
958
+ sessions.set(id, r.path);
959
+ if (this.meta[r.path]?.id)
960
+ sessions.set(this.meta[r.path].id, r.path);
860
961
  }
861
- const live = new Set();
862
- for (const r of rollouts) {
863
- live.add(r.path);
962
+ const load = async (path) => {
963
+ const existing = inventory.get(path);
964
+ if (existing)
965
+ return existing;
966
+ const r = rollouts.get(path);
967
+ if (!r)
968
+ return;
864
969
  let st;
865
970
  try {
866
- st = await stat(r.path);
971
+ st = await stat(path);
867
972
  }
868
973
  catch (err) {
869
974
  if (!gone(err))
870
975
  unlisted();
871
- continue;
976
+ return;
872
977
  }
873
- let m = this.meta[r.path];
874
- // Read once per physical file; a first line still being written is read again next pass.
875
- if (!m || m.ino !== st.ino) {
876
- const read = await rolloutMeta(r.path);
877
- // Unreadable (not gone, not just unfinished): whose it is cannot be known, so no codex instance is covered.
978
+ let m = this.meta[path];
979
+ if (!m || m.ino !== st.ino || m.id === undefined || m.parent === undefined) {
980
+ const read = await rolloutMeta(path);
878
981
  if (read === "unreadable") {
879
982
  unlisted();
880
- delete this.meta[r.path];
881
- continue;
983
+ delete this.meta[path];
984
+ return;
882
985
  }
883
986
  if (read) {
884
- m = this.meta[r.path] = read;
987
+ m = this.meta[path] = read;
885
988
  this.metaDirty = true;
886
989
  }
887
990
  else {
888
- delete this.meta[r.path];
889
- continue;
991
+ delete this.meta[path];
992
+ return;
890
993
  }
891
994
  }
892
- const name = m.cwd ? byCwd.get(resolve(m.cwd)) : undefined;
893
- // The meta belongs to the inode it was read from; a file replaced since is read again next pass.
894
- if (!name || m.ino !== st.ino)
895
- continue;
896
- const list = out.get(name) ?? [];
897
- list.push({ path: r.path, ino: st.ino, size: st.size });
898
- out.set(name, list);
995
+ if (m.ino !== st.ino)
996
+ return;
997
+ const f = { path, ino: st.ino, size: st.size, meta: m, recent: r.mtimeMs >= cutoff, archived: r.archived };
998
+ inventory.set(path, f);
999
+ if (m.id)
1000
+ sessions.set(m.id, path);
1001
+ return f;
1002
+ };
1003
+ for (const r of rollouts.values())
1004
+ if (r.mtimeMs >= cutoff)
1005
+ await load(r.path);
1006
+ for (const inst of codexInsts) {
1007
+ const selected = new Map();
1008
+ const visiting = new Set();
1009
+ const visit = async (path) => {
1010
+ if (selected.has(path))
1011
+ return true;
1012
+ if (visiting.has(path)) {
1013
+ this.unread.add(inst.name);
1014
+ return false;
1015
+ }
1016
+ const f = await load(path);
1017
+ if (!f) {
1018
+ this.unread.add(inst.name);
1019
+ return false;
1020
+ }
1021
+ visiting.add(path);
1022
+ let parentPath = f.meta.parent ? sessions.get(f.meta.parent) : undefined;
1023
+ if (parentPath && (await load(parentPath))?.meta.id !== f.meta.parent)
1024
+ parentPath = undefined;
1025
+ let orphaned;
1026
+ if (f.meta.parent) {
1027
+ if (parentPath) {
1028
+ if (!await visit(parentPath)) {
1029
+ visiting.delete(path);
1030
+ return false;
1031
+ }
1032
+ }
1033
+ else if (complete)
1034
+ orphaned = true;
1035
+ else {
1036
+ visiting.delete(path);
1037
+ this.unread.add(inst.name);
1038
+ return false;
1039
+ }
1040
+ }
1041
+ visiting.delete(path);
1042
+ const owner = f.meta.cwd ? byCwd.get(resolve(f.meta.cwd)) : undefined;
1043
+ selected.set(path, { path, ino: f.ino, size: f.size, ...(f.meta.id ? { sessionId: f.meta.id } : {}), ...(parentPath ? { parentPath } : {}), ...(orphaned ? { orphaned, missingParent: f.meta.parent } : {}), ...(owner !== inst.name || (f.archived && !f.recent) ? { identityOnly: true } : {}) });
1044
+ return true;
1045
+ };
1046
+ for (const f of [...inventory.values()])
1047
+ if (f.recent && f.meta.cwd && byCwd.get(resolve(f.meta.cwd)) === inst.name)
1048
+ await visit(f.path);
1049
+ out.set(inst.name, [...selected.values()]);
899
1050
  }
900
1051
  for (const p of Object.keys(this.meta))
901
1052
  if (!live.has(p)) {
@@ -1055,13 +1206,14 @@ export async function rolloutMeta(path) {
1055
1206
  const { bytesRead } = await fh.read(buf, 0, META_MAX_BYTES, 0);
1056
1207
  const nl = buf.subarray(0, bytesRead).indexOf(10);
1057
1208
  if (nl === -1)
1058
- return bytesRead >= META_MAX_BYTES ? { ino, cwd: null } : null;
1209
+ return bytesRead >= META_MAX_BYTES ? { ino, cwd: null, id: null, parent: null } : null;
1059
1210
  try {
1060
1211
  const d = JSON.parse(buf.toString("utf8", 0, nl));
1061
- return { ino, cwd: d.type === "session_meta" && typeof d.payload?.cwd === "string" ? d.payload.cwd : null };
1212
+ const p = d.type === "session_meta" ? d.payload : undefined;
1213
+ return { ino, cwd: typeof p?.cwd === "string" ? p.cwd : null, id: typeof p?.id === "string" ? p.id : null, parent: typeof p?.forked_from_id === "string" ? p.forked_from_id : null };
1062
1214
  }
1063
1215
  catch {
1064
- return { ino, cwd: null };
1216
+ return { ino, cwd: null, id: null, parent: null };
1065
1217
  }
1066
1218
  }
1067
1219
  catch (err) {