mixdog 1.4.4 → 1.4.5

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 (218) hide show
  1. package/package.json +1 -2
  2. package/src/cli.mjs +5 -1
  3. package/src/defaults/shipped-history.json +26 -2
  4. package/src/defaults/skills/browser-use/SKILL.md +148 -273
  5. package/src/defaults/skills/browser-use/references/devtools.md +14 -0
  6. package/src/defaults/skills/browser-use/references/page-flows.md +66 -0
  7. package/src/defaults/skills/browser-use/references/sign-in.md +70 -0
  8. package/src/defaults/skills/code-tidy/SKILL.md +60 -84
  9. package/src/defaults/skills/code-tidy/references/agent-cleanup.md +27 -2
  10. package/src/defaults/skills/computer-use/SKILL.md +145 -240
  11. package/src/defaults/skills/computer-use/references/flows.md +35 -0
  12. package/src/defaults/skills/computer-use/references/operations.md +22 -0
  13. package/src/defaults/skills/computer-use/references/platforms.md +12 -0
  14. package/src/defaults/skills/docx/SKILL.md +23 -31
  15. package/src/defaults/skills/docx/references/native-authoring.md +26 -3
  16. package/src/defaults/skills/docx/references/redlining.md +15 -0
  17. package/src/defaults/skills/image/SKILL.md +37 -77
  18. package/src/defaults/skills/image/references/prompt-forms.md +51 -0
  19. package/src/defaults/skills/image/references/text.md +6 -0
  20. package/src/defaults/skills/local-provider/SKILL.md +75 -105
  21. package/src/defaults/skills/local-provider/references/lifecycle.md +14 -0
  22. package/src/defaults/skills/local-provider/references/maintenance.md +19 -0
  23. package/src/defaults/skills/local-provider/references/recovery.md +30 -0
  24. package/src/defaults/skills/memory-management/SKILL.md +16 -20
  25. package/src/defaults/skills/pdf/SKILL.md +21 -23
  26. package/src/defaults/skills/pdf/references/blocks.md +20 -0
  27. package/src/defaults/skills/pdf/references/forms.md +12 -0
  28. package/src/defaults/skills/pdf/references/html.md +2 -2
  29. package/src/defaults/skills/pdf/references/ocr.md +7 -0
  30. package/src/defaults/skills/pdf/references/security.md +8 -0
  31. package/src/defaults/skills/pptx/SKILL.md +30 -109
  32. package/src/defaults/skills/pptx/references/audit.md +45 -0
  33. package/src/defaults/skills/pptx/references/brief-critique.md +47 -0
  34. package/src/defaults/skills/pptx/references/editing.md +24 -0
  35. package/src/defaults/skills/pptx/references/html.md +1 -1
  36. package/src/defaults/skills/pptx/references/script.md +33 -0
  37. package/src/defaults/skills/setup/SKILL.md +34 -102
  38. package/src/defaults/skills/setup/references/actions.md +72 -13
  39. package/src/defaults/skills/setup/references/surfaces.md +23 -8
  40. package/src/defaults/skills/setup-guide/SKILL.md +28 -115
  41. package/src/defaults/skills/setup-guide/references/features.md +36 -0
  42. package/src/defaults/skills/setup-guide/references/glossary.md +38 -0
  43. package/src/defaults/skills/setup-guide/references/workflow-models.md +62 -0
  44. package/src/defaults/skills/test-suite/SKILL.md +15 -127
  45. package/src/defaults/skills/test-suite/references/case-management.md +17 -9
  46. package/src/defaults/skills/test-suite/references/diagnose.md +23 -0
  47. package/src/defaults/skills/test-suite/references/prune.md +27 -0
  48. package/src/defaults/skills/test-suite/references/write.md +44 -0
  49. package/src/defaults/skills/video/SKILL.md +28 -42
  50. package/src/defaults/skills/video/references/prompting.md +31 -0
  51. package/src/defaults/skills/visualize/SKILL.md +73 -107
  52. package/src/defaults/skills/visualize/references/craft.md +1 -2
  53. package/src/defaults/skills/visualize/references/feedback.md +36 -0
  54. package/src/defaults/skills/visualize/references/showing.md +13 -0
  55. package/src/defaults/skills/xlsx/SKILL.md +23 -50
  56. package/src/defaults/skills/xlsx/references/editing.md +15 -0
  57. package/src/defaults/skills/xlsx/references/model-conventions.md +1 -1
  58. package/src/defaults/skills/xlsx/references/recalculation.md +37 -0
  59. package/src/defaults/skills/xlsx/references/report-design.md +18 -2
  60. package/src/lib/keychain-cjs.cjs +22 -1
  61. package/src/lib/rules-builder.cjs +1 -1
  62. package/src/rules/agent/AGENT.md +3 -0
  63. package/src/rules/lead/LEAD.md +3 -1
  64. package/src/runtime/agent/orchestrator/agent-runtime/cache-strategy.mjs +8 -30
  65. package/src/runtime/agent/orchestrator/agent-runtime/title-completion.mjs +5 -0
  66. package/src/runtime/agent/orchestrator/mcp/oauth-request.mjs +21 -2
  67. package/src/runtime/agent/orchestrator/providers/account-pool.mjs +3 -0
  68. package/src/runtime/agent/orchestrator/providers/admission-scheduler.mjs +2 -2
  69. package/src/runtime/agent/orchestrator/providers/anthropic-api-transport.mjs +2 -0
  70. package/src/runtime/agent/orchestrator/providers/anthropic-message-threads.mjs +583 -0
  71. package/src/runtime/agent/orchestrator/providers/anthropic-oauth-client-version.mjs +1 -1
  72. package/src/runtime/agent/orchestrator/providers/anthropic-oauth.mjs +81 -19
  73. package/src/runtime/agent/orchestrator/providers/anthropic-sse.mjs +180 -13
  74. package/src/runtime/agent/orchestrator/providers/anthropic.mjs +56 -6
  75. package/src/runtime/agent/orchestrator/providers/cursor-wire.mjs +4 -2
  76. package/src/runtime/agent/orchestrator/providers/gemini-error-contract.mjs +6 -6
  77. package/src/runtime/agent/orchestrator/providers/gemini-model-redirect.mjs +44 -0
  78. package/src/runtime/agent/orchestrator/providers/gemini-reasoning-relay.mjs +5 -0
  79. package/src/runtime/agent/orchestrator/providers/gemini-request-body.mjs +34 -5
  80. package/src/runtime/agent/orchestrator/providers/gemini-response.mjs +10 -0
  81. package/src/runtime/agent/orchestrator/providers/gemini-schema.mjs +11 -6
  82. package/src/runtime/agent/orchestrator/providers/gemini-sdk-request.mjs +4 -100
  83. package/src/runtime/agent/orchestrator/providers/gemini-stream.mjs +3 -227
  84. package/src/runtime/agent/orchestrator/providers/gemini.mjs +226 -118
  85. package/src/runtime/agent/orchestrator/providers/lib/anthropic-request-utils.mjs +11 -1
  86. package/src/runtime/agent/orchestrator/providers/lib/claude-bridge-schema.mjs +15 -3
  87. package/src/runtime/agent/orchestrator/providers/lib/gemini-model-catalog.mjs +2 -1
  88. package/src/runtime/agent/orchestrator/providers/openai-direct-request.mjs +5 -28
  89. package/src/runtime/agent/orchestrator/providers/openai-ws-pool.mjs +50 -28
  90. package/src/runtime/agent/orchestrator/providers/openai-ws-stream/socket-handlers.mjs +14 -10
  91. package/src/runtime/agent/orchestrator/providers/openai-ws.mjs +1 -3
  92. package/src/runtime/agent/orchestrator/providers/stream-json-pool/worker-slots/slot-spawn.mjs +8 -3
  93. package/src/runtime/agent/orchestrator/providers/vertex-ai-maas.mjs +2 -2
  94. package/src/runtime/agent/orchestrator/runtime-core/config-helpers.mjs +4 -5
  95. package/src/runtime/agent/orchestrator/runtime-core/goal-tool-defs.mjs +2 -2
  96. package/src/runtime/agent/orchestrator/runtime-core/provider-request-tools.mjs +15 -8
  97. package/src/runtime/agent/orchestrator/runtime-core/session-title.mjs +1 -0
  98. package/src/runtime/agent/orchestrator/runtime-core/todo-list.mjs +9 -32
  99. package/src/runtime/agent/orchestrator/runtime-core/todo-tool-defs.mjs +4 -5
  100. package/src/runtime/agent/orchestrator/session/compact/summary-route.mjs +1 -3
  101. package/src/runtime/agent/orchestrator/session/loop/compact-policy.mjs +5 -2
  102. package/src/runtime/agent/orchestrator/session/loop/no-tool-turn/todo-check.mjs +2 -17
  103. package/src/runtime/agent/orchestrator/session/loop/usage.mjs +7 -3
  104. package/src/runtime/agent/orchestrator/session/manager/ask-turn-settle.mjs +9 -2
  105. package/src/runtime/agent/orchestrator/session/manager/session-close.mjs +26 -13
  106. package/src/runtime/agent/orchestrator/session/manager/session-lifecycle.mjs +4 -5
  107. package/src/runtime/agent/orchestrator/session/manager/usage-metrics.mjs +16 -5
  108. package/src/runtime/agent/orchestrator/session/store/lifecycle-barriers.mjs +22 -1
  109. package/src/runtime/agent/orchestrator/session/store/live-state.mjs +4 -0
  110. package/src/runtime/agent/orchestrator/tools/builtin/cache-layers.mjs +11 -9
  111. package/src/runtime/agent/orchestrator/tools/builtin/grep-formatting.mjs +2 -8
  112. package/src/runtime/agent/orchestrator/tools/builtin/lib/search-input-helpers.mjs +1 -9
  113. package/src/runtime/agent/orchestrator/tools/builtin/local-search-telemetry.mjs +2 -2
  114. package/src/runtime/agent/orchestrator/tools/builtin/native-search-client.mjs +1 -1
  115. package/src/runtime/agent/orchestrator/tools/builtin/runtime-capabilities.mjs +12 -3
  116. package/src/runtime/agent/orchestrator/tools/code-graph/ast-calls.mjs +9 -2
  117. package/src/runtime/agent/orchestrator/tools/code-graph/build.mjs +137 -7
  118. package/src/runtime/agent/orchestrator/tools/code-graph/calls-cache.mjs +43 -14
  119. package/src/runtime/agent/orchestrator/tools/code-graph/calls-sidecar-worker.mjs +33 -0
  120. package/src/runtime/agent/orchestrator/tools/code-graph/disk-cache.mjs +146 -11
  121. package/src/runtime/agent/orchestrator/tools/code-graph/dispatch.mjs +31 -24
  122. package/src/runtime/agent/orchestrator/tools/code-graph/graph-binary/spawn-jsonl.mjs +172 -25
  123. package/src/runtime/agent/orchestrator/tools/code-graph/graph-model.mjs +100 -2
  124. package/src/runtime/agent/orchestrator/tools/code-graph/main-thread-slice.mjs +18 -0
  125. package/src/runtime/agent/orchestrator/tools/code-graph/mask-pool.mjs +219 -0
  126. package/src/runtime/agent/orchestrator/tools/code-graph/mask-worker.mjs +12 -0
  127. package/src/runtime/agent/orchestrator/tools/code-graph/memory-cache.mjs +6 -1
  128. package/src/runtime/agent/orchestrator/tools/code-graph/modes/calls.mjs +9 -2
  129. package/src/runtime/agent/orchestrator/tools/code-graph/modes/dependents.mjs +5 -1
  130. package/src/runtime/agent/orchestrator/tools/code-graph/modes/structure.mjs +8 -2
  131. package/src/runtime/agent/orchestrator/tools/code-graph/modes/symbols.mjs +14 -11
  132. package/src/runtime/agent/orchestrator/tools/code-graph/search-references.mjs +41 -18
  133. package/src/runtime/agent/orchestrator/tools/code-graph/search.mjs +391 -83
  134. package/src/runtime/agent/orchestrator/tools/code-graph/symbol-index.mjs +109 -9
  135. package/src/runtime/agent/orchestrator/tools/code-graph/text-mask.mjs +73 -29
  136. package/src/runtime/agent/orchestrator/tools/code-graph-prewarm-worker.mjs +6 -5
  137. package/src/runtime/agent/orchestrator/tools/lib/native-spawn-client.mjs +7 -3
  138. package/src/runtime/channels/lib/crash-log.mjs +21 -1
  139. package/src/runtime/channels/lib/worker-main.mjs +8 -25
  140. package/src/runtime/media/jobs.mjs +5 -0
  141. package/src/runtime/media/store.mjs +146 -3
  142. package/src/runtime/office/core/office-write-ledger.mjs +2 -2
  143. package/src/runtime/shared/agent-reap-state.mjs +19 -0
  144. package/src/runtime/shared/desktop-notice-host.d.mts +13 -0
  145. package/src/runtime/shared/desktop-notice-host.mjs +87 -0
  146. package/src/runtime/shared/file-lock.mjs +2 -2
  147. package/src/runtime/shared/heap-cap.mjs +9 -11
  148. package/src/runtime/shared/llm/http-agent.mjs +11 -1
  149. package/src/runtime/shared/llm/system-proxy.mjs +100 -25
  150. package/src/runtime/shared/llm/usage-accounting.mjs +18 -7
  151. package/src/runtime/shared/llm/usage-context.mjs +2 -2
  152. package/src/runtime/shared/llm/usage-ledger-requests.mjs +10 -3
  153. package/src/runtime/shared/llm/usage-ledger.mjs +13 -1
  154. package/src/runtime/shared/provider-accounts.mjs +22 -0
  155. package/src/runtime/shared/provider-auth-binding.mjs +2 -2
  156. package/src/runtime/shared/resource-admission.mjs +2 -2
  157. package/src/runtime/shared/root-async-context.mjs +51 -0
  158. package/src/runtime/shared/run-child.mjs +18 -1
  159. package/src/runtime/shared/slow-span.mjs +2 -2
  160. package/src/runtime/shared/tool-execution-owner.mjs +2 -2
  161. package/src/runtime/shared/tool-work-units.mjs +2 -0
  162. package/src/runtime/shared/turn-snapshot-store.mjs +75 -18
  163. package/src/runtime/shared/turn-worktree-snapshot.mjs +38 -8
  164. package/src/runtime/shared/user-cwd.mjs +2 -2
  165. package/src/runtime/shared/worker-requests.mjs +3 -1
  166. package/src/runtime/shared/worktrees/lock.mjs +2 -2
  167. package/src/runtime/visualize-bridge/client.mjs +26 -4
  168. package/src/runtime/visualize-bridge/tool-defs.mjs +46 -5
  169. package/src/session-runtime/boot/apis.mjs +1 -0
  170. package/src/session-runtime/boot/begin.mjs +2 -0
  171. package/src/session-runtime/media-api.mjs +15 -0
  172. package/src/session-runtime/provider-readiness.mjs +10 -2
  173. package/src/session-runtime/runtime-core.mjs +35 -8
  174. package/src/session-runtime/runtime-review-api.mjs +3 -1
  175. package/src/session-runtime/services/agent-tool/close-flow.mjs +6 -1
  176. package/src/session-runtime/services/agent-tool/execute-resume.mjs +94 -0
  177. package/src/session-runtime/services/agent-tool/execute-send.mjs +77 -27
  178. package/src/session-runtime/services/agent-tool/execute-spawn.mjs +28 -7
  179. package/src/session-runtime/services/agent-tool/execute.mjs +6 -1
  180. package/src/session-runtime/services/agent-tool/helpers.mjs +11 -0
  181. package/src/session-runtime/services/agent-tool/job-views/job-render.mjs +13 -10
  182. package/src/session-runtime/services/agent-tool/render.mjs +2 -0
  183. package/src/session-runtime/services/agent-tool/send-flow.mjs +1 -0
  184. package/src/session-runtime/services/agent-tool/spawn-flow/spawn-prep.mjs +6 -0
  185. package/src/session-runtime/services/agent-tool/spawn-flow/spawn-run.mjs +2 -1
  186. package/src/session-runtime/services/agent-tool/tag-registry/reaped-sessions.mjs +82 -0
  187. package/src/session-runtime/services/agent-tool/tag-registry/record-facts-worker.mjs +36 -0
  188. package/src/session-runtime/services/agent-tool/tag-registry.mjs +4 -0
  189. package/src/session-runtime/services/agent-tool/terminal-reaper/row-helpers.mjs +29 -0
  190. package/src/session-runtime/services/agent-tool/terminal-reaper/scan-admission.mjs +11 -3
  191. package/src/session-runtime/services/agent-tool/terminal-reaper/stale-rows.mjs +19 -8
  192. package/src/session-runtime/services/agent-tool/terminal-reaper.mjs +5 -2
  193. package/src/session-runtime/services/agent-tool/worker-index.mjs +1 -0
  194. package/src/session-runtime/services/daemon-crash-capture/daemon-exit-record.mjs +2 -2
  195. package/src/session-runtime/session-create.mjs +5 -0
  196. package/src/session-runtime/setup-tool/settings-contract.mjs +2 -0
  197. package/src/session-runtime/usage-stats-api.mjs +3 -0
  198. package/src/session-runtime/workspace/admission-gate.mjs +25 -7
  199. package/src/session-runtime/workspace/transitions.mjs +4 -0
  200. package/src/standalone/daemon-log.mjs +28 -4
  201. package/src/standalone/daemon-owner.mjs +41 -7
  202. package/src/standalone/daemon-work-gate.mjs +139 -0
  203. package/src/standalone/daemon.mjs +80 -11
  204. package/src/standalone/desktop-service-registry.mjs +11 -4
  205. package/src/standalone/session-client.mjs +57 -5
  206. package/src/standalone/session-protocol.mjs +5 -0
  207. package/src/standalone/session-service/agent-tree/agent-cancel.mjs +17 -1
  208. package/src/standalone/session-service/agent-tree.mjs +7 -0
  209. package/src/standalone/session-service/session-calls/session-turns.mjs +12 -5
  210. package/src/standalone/session-transport/call-route.mjs +10 -4
  211. package/src/standalone/session-transport/routes.mjs +21 -0
  212. package/src/standalone/session-transport/stack.mjs +3 -1
  213. package/src/standalone/session-transport.mjs +5 -0
  214. package/src/tui/dist/index.mjs +495 -477
  215. package/src/tui/session/session-api/media.mjs +5 -0
  216. package/src/tui/session/turn.mjs +13 -0
  217. package/src/runtime/agent/orchestrator/providers/gemini-sdk-stream/cancellation.mjs +0 -71
  218. package/src/runtime/agent/orchestrator/providers/gemini-sdk-stream/reader.mjs +0 -108
@@ -33,6 +33,8 @@ import { noteShellOwnerSessionClosed } from '../../tools/builtin/lib/shell-job-r
33
33
  import { forgetSideQuestion } from '../side-question-store.mjs';
34
34
  import { cancelBackgroundTasks } from '../../../../shared/background-tasks.mjs';
35
35
  import { releaseWorkerSnapshots } from '../../../../shared/worktrees/worker-snapshots.mjs';
36
+ import { accountScopedSessionIdsOf } from '../../../../shared/provider-accounts.mjs';
37
+ import { registerSessionPurgeHook } from '../store/session-observers.mjs';
36
38
 
37
39
  /**
38
40
  * Close a session. Plants a `closed=true` tombstone on disk with a bumped
@@ -56,7 +58,7 @@ export function closeSession(id, reason = 'manual', opts = {}) {
56
58
  const outcome = { absent: false };
57
59
  const newGen = tombstone
58
60
  ? markSessionClosed(id, reason, { outcome })
59
- : bumpSessionGeneration(id, reason, { outcome });
61
+ : bumpSessionGeneration(id, reason, { outcome, ownerClose: opts.ownerClose === true });
60
62
  return finishClose({ id, reason, tombstone, entry, closeSnapshot, newGen, outcome });
61
63
  }
62
64
 
@@ -79,7 +81,7 @@ export async function closeSessionAsync(id, reason = 'manual', opts = {}) {
79
81
  const outcome = { absent: false };
80
82
  const newGen = tombstone
81
83
  ? markSessionClosedAttempt(id, reason, { outcome }, attempt)
82
- : bumpSessionGenerationAttempt(id, reason, { outcome }, attempt);
84
+ : bumpSessionGenerationAttempt(id, reason, { outcome, ownerClose: opts.ownerClose === true }, attempt);
83
85
  if (!isLifecycleRetry(newGen)) {
84
86
  return finishClose({ id, reason, tombstone, entry, closeSnapshot, newGen, outcome });
85
87
  }
@@ -283,14 +285,25 @@ function teardownSessionRuntime({ id, entry, reason, tombstone, newGen }) {
283
285
  });
284
286
  }
285
287
 
286
- function closeProviderConnections(id, cause) {
287
- try {
288
- globalThis.__mixdogCloseProviderConnectionsForSession?.(id, cause);
289
- } catch {
290
- /* ignore */
288
+ // The session's own connections and those an account pool keyed by its
289
+ // account-scoped ids. A close, delete or reap ends the session: every
290
+ // connection goes and the record is dropped. A turn abort ends one turn: only
291
+ // a connection mid-response goes; idle ones keep their response chain.
292
+ function closeProviderConnections(id, cause, { inFlightOnly = false } = {}) {
293
+ const options = inFlightOnly ? { inFlightOnly } : undefined;
294
+ for (const key of [id, ...accountScopedSessionIdsOf(id, { forget: !inFlightOnly })]) {
295
+ try {
296
+ globalThis.__mixdogCloseProviderConnectionsForSession?.(key, cause, options);
297
+ } catch {
298
+ /* ignore */
299
+ }
291
300
  }
292
301
  }
293
302
 
303
+ // Reap: a stored session deleted without a runtime close (sub-agent retention,
304
+ // tombstone sweep) takes its connections and record along.
305
+ registerSessionPurgeHook((id) => closeProviderConnections(id, 'session-deleted'));
306
+
294
307
  // Session-scoped read caches: pure caches a resumed turn rebuilds from the
295
308
  // transcript/disk. The save delta baselines (parent and save-worker thread)
296
309
  // are one of them: a resumed session's next save simply goes out full; the
@@ -333,17 +346,18 @@ export function abortSessionTurn(id, reason = 'turn-abort') {
333
346
  } catch {
334
347
  /* ignore */
335
348
  }
336
- closeProviderConnections(id, `turn-abort:${reason}`);
349
+ closeProviderConnections(id, `turn-abort:${reason}`, { inFlightOnly: true });
337
350
  return true;
338
351
  }
339
352
 
340
353
  /**
341
354
  * Runtime-only unload for a session whose work is DONE.
342
355
  *
343
- * Releases the heavy process-local runtime a finished agent still pins —
344
- * pooled provider connections, the read-dedup cache, and the liveness/metric
345
- * runtime entry — WITHOUT
346
- * touching lifecycle state. Deliberately NOT a close:
356
+ * Releases the heavy process-local runtime a finished agent still pins — the
357
+ * read-dedup cache and the liveness/metric runtime entry — WITHOUT
358
+ * touching lifecycle state. Pooled provider connections stay: a follow-up
359
+ * continues its response chain on them, and their idle and liveness eviction
360
+ * still apply; close, delete and reap release them. Deliberately NOT a close:
347
361
  * - no markSessionClosed() tombstone and no bumpSessionGeneration(): the
348
362
  * session file stays open and resumable, so a same-tag follow-up
349
363
  * (`agent type=send`) resumes the SAME session with its full transcript
@@ -377,7 +391,6 @@ export function unloadSessionRuntime(id, reason = 'runtime-unload') {
377
391
  }
378
392
  _stopToolActivityHeartbeat(id);
379
393
  forgetSideQuestion(id, { cancel: true });
380
- closeProviderConnections(id, `runtime-unload:${reason}`);
381
394
  releaseSessionReadCaches(id);
382
395
  _clearSessionRuntime(id);
383
396
  // A settled worker turn's read-only snapshots go with it (only ones it made).
@@ -64,9 +64,8 @@ export { prefetchSession, prepareSessionProjection } from './prepared-resume-cac
64
64
  function buildSessionProviderCacheOpts(providerName, sessionId, agent = null, config = null, session = null) {
65
65
  // Keep this in sync with createSession's provider-cache policy: only
66
66
  // explicit-breakpoint providers get BP cache opts here; key-prefix
67
- // providers use promptCacheKey and request-time strategy instead (plus the
68
- // OpenAI API's Lead-class 12h prefix TTL and a one-shot role's shared
69
- // prompt-cache scope).
67
+ // providers use promptCacheKey and request-time strategy instead (plus a
68
+ // one-shot role's shared prompt-cache scope).
70
69
  if (cacheCapabilityForProvider(providerName) !== 'explicit-breakpoint') {
71
70
  return roleProviderCacheOpts(providerName, agent, { session });
72
71
  }
@@ -259,8 +258,8 @@ export function createSession(createOpts) {
259
258
  // today, so that branch never fires); build it directly here so every
260
259
  // session still gets a cache strategy. Lead-class sessions (the Lead, raw
261
260
  // callers, bot home threads and other agent-mode Leads) get their BP4
262
- // message tail TTL linked to the user's autoClear idle-sweep config and,
263
- // on the OpenAI API, the 12h stable-prefix TTL; sub-agents and hidden roles
261
+ // message tail TTL linked to the user's autoClear idle-sweep config;
262
+ // sub-agents and hidden roles
264
263
  // keep the agent defaults (see cache-strategy.mjs docs).
265
264
  const providerCacheOpts =
266
265
  resolved.providerCacheOpts || buildSessionProviderCacheOpts(providerName, id, opts.agent, null, { ...opts, id });
@@ -399,7 +399,8 @@ export function applyAskTerminalUsageTotals(session, result, options = {}) {
399
399
  });
400
400
  }
401
401
  const lastTurn = result.lastTurnUsage || result.usage || {};
402
- const measuredInput = Number(lastTurn.inputTokens) || 0;
402
+ const contextInput = lastTurn.contextInputTokens ?? lastTurn.inputTokens;
403
+ const measuredInput = Number(contextInput) || 0;
403
404
  const measuredCache = providerInputExcludesCache(session.provider)
404
405
  ? (Number(lastTurn.cachedTokens) || 0) + (Number(lastTurn.cacheWriteTokens) || 0)
405
406
  : 0;
@@ -414,10 +415,11 @@ export function applyAskTerminalUsageTotals(session, result, options = {}) {
414
415
  return;
415
416
  }
416
417
  setLastContextTokens(session, {
417
- input: lastTurn.inputTokens ?? 0,
418
+ input: contextInput ?? 0,
418
419
  output: lastTurn.outputTokens ?? 0,
419
- cachedRead: lastTurn.cachedTokens ?? 0,
420
+ cachedRead: lastTurn.contextCachedTokens ?? lastTurn.cachedTokens ?? 0,
420
421
  cacheWrite: lastTurn.cacheWriteTokens ?? 0,
422
+ outputInWindow: lastTurn.contextInputTokens != null,
421
423
  });
422
424
  }
423
425
 
@@ -440,13 +442,18 @@ function clearLastContextTokens(session, ts) {
440
442
  // cache (cache_read is a separate field), so the cached portion must be
441
443
  // added back to reflect real context size; openai/grok/gemini already fold
442
444
  // cached tokens INTO the input count, so input alone is the footprint.
443
- function setLastContextTokens(session, { input, output, cachedRead, cacheWrite, ts }) {
445
+ // `outputInWindow`: the input is the context's prompt part and the generated
446
+ // output is in the window too (a billed input that repeats hop input, Gemini
447
+ // long decode), so the footprint adds it as the compaction pressure does.
448
+ function setLastContextTokens(session, { input, output, cachedRead, cacheWrite, outputInWindow = false, ts }) {
444
449
  session.lastInputTokens = input;
445
450
  session.lastOutputTokens = output;
446
451
  session.lastCachedReadTokens = cachedRead;
447
452
  session.lastCacheWriteTokens = cacheWrite;
448
453
  session.lastUncachedInputTokens = uncachedInputTokensForProvider(session.provider, input, cachedRead, cacheWrite);
449
- session.lastContextTokens = providerInputExcludesCache(session.provider) ? input + cachedRead + cacheWrite : input;
454
+ session.lastContextTokens =
455
+ (providerInputExcludesCache(session.provider) ? input + cachedRead + cacheWrite : input) +
456
+ (outputInWindow ? output : 0);
450
457
  session.lastContextTokensUpdatedAt = ts || Date.now();
451
458
  session.lastContextTokensStaleAfterCompact = false;
452
459
  }
@@ -485,6 +492,7 @@ function recordLastContextTokens(session, context) {
485
492
  contextCachedReadTokens,
486
493
  contextCacheWriteTokens,
487
494
  contextMeasuredTokens,
495
+ contextOutputInWindow,
488
496
  ts,
489
497
  } = context;
490
498
  const inputExcludesCache = providerInputExcludesCache(session.provider);
@@ -504,6 +512,7 @@ function recordLastContextTokens(session, context) {
504
512
  output: contextOutputTokens || 0,
505
513
  cachedRead: contextCachedReadTokens || 0,
506
514
  cacheWrite: contextCacheWriteTokens || 0,
515
+ outputInWindow: contextOutputInWindow === true,
507
516
  ts,
508
517
  });
509
518
  }
@@ -522,6 +531,7 @@ function iterationContext(delta) {
522
531
  contextCachedReadTokens = delta.deltaCachedRead,
523
532
  contextCacheWriteTokens = delta.deltaCacheWrite,
524
533
  contextMeasuredTokens = null,
534
+ contextOutputInWindow = false,
525
535
  ts,
526
536
  } = delta;
527
537
  return {
@@ -530,6 +540,7 @@ function iterationContext(delta) {
530
540
  contextCachedReadTokens,
531
541
  contextCacheWriteTokens,
532
542
  contextMeasuredTokens,
543
+ contextOutputInWindow,
533
544
  ts,
534
545
  };
535
546
  }
@@ -289,6 +289,8 @@ function _markSessionClosed(id, reason, options, attempt) {
289
289
  * late write's own _shouldDrop() check (generation-as-ownership-counter
290
290
  * rule, see store/write-admission.mjs) sees disk generation > expected and drops itself instead
291
291
  * of clobbering whatever the resumed session writes next.
292
+ * `options.ownerClose` (the agent tool's owner close) also stamps
293
+ * `ownerClosedAt` on the record.
292
294
  * Returns the new generation, or null if the session file doesn't exist.
293
295
  */
294
296
  export function bumpSessionGeneration(id, reason = 'detach', options = {}) {
@@ -322,7 +324,16 @@ function _bumpSessionGeneration(id, reason, options, attempt) {
322
324
  const existing = loadSession(id);
323
325
  if (!existing) return _recordAbsent(options);
324
326
  const newGen = (typeof existing.generation === 'number' ? existing.generation : 0) + 1;
325
- const detached = { ...existing, generation: newGen, updatedAt: Date.now(), detachedReason: reason };
327
+ const now = Date.now();
328
+ const detached = {
329
+ ...existing,
330
+ generation: newGen,
331
+ updatedAt: now,
332
+ detachedReason: reason,
333
+ // The agent's owner closed it: the record keeps that for its lifetime
334
+ // (agent-reap-state.mjs ownerClosedSinceLastTurn).
335
+ ...(options.ownerClose === true ? { ownerClosedAt: now } : {}),
336
+ };
326
337
  const wrote = _writeLifecycleRecord(id, detached, existing, reason, attempt);
327
338
  if (wrote !== true) return wrote || null;
328
339
  _savePending.delete(id);
@@ -371,6 +382,16 @@ async function _runBarrierAttempts(run) {
371
382
  }
372
383
  }
373
384
 
385
+ /** Stamp the agent owner's close (`ownerClosedAt`) on a session record that
386
+ * stays open: bumpSessionGeneration with `ownerClose`, never blocking the
387
+ * event loop on a rename retry. For a close whose runtime close wrote no such
388
+ * stamp (already terminal, or closed earlier for another reason). */
389
+ export function markSessionOwnerClosedAsync(id, reason = 'cli-agent-close', options = {}) {
390
+ return _runBarrierAttempts((attempt) =>
391
+ _bumpSessionGeneration(id, reason, { ...options, ownerClose: true }, attempt)
392
+ );
393
+ }
394
+
374
395
  /** markSessionClosed without ever blocking the event loop on a rename retry. */
375
396
  export function markSessionClosedAsync(id, reason = 'manual', options = {}) {
376
397
  return _runBarrierAttempts((attempt) => _markSessionClosed(id, reason, options, attempt));
@@ -1,5 +1,6 @@
1
1
  import { contentCarriesLiveMedia } from '../../providers/media-normalization.mjs';
2
2
  import { publishSessionPersistence } from './session-persistence-events.mjs';
3
+ import { raiseDesktopNotice } from '../../../../shared/desktop-notice-host.mjs';
3
4
 
4
5
  const _lastSaveError = new Map(); // id -> { message, at }
5
6
 
@@ -243,6 +244,9 @@ function _failedSaveClone(session) {
243
244
  */
244
245
  export function _recordSaveFailure(id, err, epoch = null, session = null) {
245
246
  if (!id) return;
247
+ // The only notice of a transcript the disk no longer keeps up with; told
248
+ // once per run, whichever session it was.
249
+ raiseDesktopNotice('conversation-save', 'conversation-save-failed');
246
250
  _lastSaveError.set(id, { message: err?.message ?? String(err), at: Date.now() });
247
251
  _droppedSaveIds.add(id);
248
252
  _markFailureEpoch(id, epoch);
@@ -1,7 +1,7 @@
1
1
  import { statSync } from 'node:fs';
2
2
  import * as fsPromises from 'node:fs/promises';
3
- import { AsyncLocalStorage } from 'node:async_hooks';
4
3
  import { isAbsolute, sep } from 'node:path';
4
+ import { runtimeAsyncLocalStorage } from '../../../../shared/root-async-context.mjs';
5
5
  import { canonicalCachePath, deleteReadRangeIndexForPath } from './read-range-index.mjs';
6
6
  import { resolveAgainstCwd } from './path-utils.mjs';
7
7
  import { fileVersion, sameFileVersion } from './file-version.mjs';
@@ -9,8 +9,8 @@ import { canCacheReadFile } from './read-cache-policy.mjs';
9
9
 
10
10
  const RESULT_CACHE = new Map(); // key → { ts, value, paths, scopes, readSnapshotMeta, contentPrefixHash, bytes }
11
11
  const RESULT_CACHE_INFLIGHT = new Map(); // key → { promise, controller, subscribers, settled }
12
- const RESULT_CACHE_COMPUTE = new AsyncLocalStorage();
13
- const READ_CACHE_PUBLICATION = new AsyncLocalStorage();
12
+ const RESULT_CACHE_COMPUTE = runtimeAsyncLocalStorage();
13
+ const READ_CACHE_PUBLICATION = runtimeAsyncLocalStorage();
14
14
  // Only active reads retain a slot. Invalidating an inactive path creates
15
15
  // no history, and the final reader releases its path in a finally block.
16
16
  const ACTIVE_READ_CACHE_PATHS = new Map();
@@ -491,10 +491,10 @@ export function registerCacheInvalidationListener(fn) {
491
491
  if (typeof fn === 'function') EXTRA_INVALIDATION_LISTENERS.add(fn);
492
492
  return () => EXTRA_INVALIDATION_LISTENERS.delete(fn);
493
493
  }
494
- function runExtraInvalidationListeners(affectedPaths = null) {
494
+ function runExtraInvalidationListeners(affectedPaths = null, source = {}) {
495
495
  for (const fn of EXTRA_INVALIDATION_LISTENERS) {
496
496
  try {
497
- fn(affectedPaths);
497
+ fn(affectedPaths, source);
498
498
  } catch {
499
499
  /* best-effort: one listener must not block others */
500
500
  }
@@ -517,7 +517,7 @@ function cacheInvalidateAll() {
517
517
  runExtraInvalidationListeners();
518
518
  }
519
519
 
520
- function cacheInvalidatePaths(paths) {
520
+ function cacheInvalidatePaths(paths, source) {
521
521
  const affectedPaths = normalizeCacheMetaPaths(Array.isArray(paths) ? paths : [paths]);
522
522
  if (affectedPaths.length === 0) {
523
523
  cacheInvalidateAll();
@@ -559,12 +559,14 @@ function cacheInvalidatePaths(paths) {
559
559
  for (const affected of affectedPaths) {
560
560
  deleteReadRangeIndexForPath(affected);
561
561
  }
562
- runExtraInvalidationListeners(affectedPaths);
562
+ runExtraInvalidationListeners(affectedPaths, source);
563
563
  }
564
564
 
565
- export function invalidateBuiltinResultCache(paths = null) {
565
+ /** `source.canonical`: the paths come from a link-free watch on canonical
566
+ * roots, so listeners may match them by spelling alone. */
567
+ export function invalidateBuiltinResultCache(paths = null, source = {}) {
566
568
  if (Array.isArray(paths) ? paths.length > 0 : Boolean(paths)) {
567
- cacheInvalidatePaths(paths);
569
+ cacheInvalidatePaths(paths, source);
568
570
  return;
569
571
  }
570
572
  cacheInvalidateAll();
@@ -13,7 +13,7 @@ export function splitGrepLinePrefix(line, matchOnly = false) {
13
13
  const text = String(line || '');
14
14
  if (!text) return null;
15
15
  const searchFrom = /^[A-Za-z]:/.test(text) ? 2 : 0;
16
- // content/count modes — rg emits, after any optional drive-letter prefix:
16
+ // Content mode — rg emits, after any optional drive-letter prefix:
17
17
  // match: path:lineNo:content (output_mode content default)
18
18
  // context: path-lineNo-content (-A / -B / -C context lines)
19
19
  // Caller context decides whether line-number-only output is possible. A
@@ -87,16 +87,10 @@ export function normalizeGrepLine(
87
87
  // There is no path prefix to normalise; everything after ^\d+[:\-] is
88
88
  // source content and must remain byte-exact.
89
89
  if (filenameOmitted && splitGrepLineNumberOnlyPrefix(line)) return line;
90
- const split = splitGrepLinePrefix(line, matchOnly);
90
+ const split = outputMode === 'count' ? splitGrepCountPrefix(line) : splitGrepLinePrefix(line, matchOnly);
91
91
  if (split) {
92
92
  return line.slice(0, split.pathEnd).replace(/\\/g, '/') + line.slice(split.pathEnd);
93
93
  }
94
- if (outputMode === 'count') {
95
- const countSplit = splitGrepCountPrefix(line);
96
- if (countSplit) {
97
- return line.slice(0, countSplit.pathEnd).replace(/\\/g, '/') + line.slice(countSplit.pathEnd);
98
- }
99
- }
100
94
  // No confirmed path prefix. Do not fall back to "first colon" slicing:
101
95
  // single-file context lines are `N-content`, so their first colon may be a
102
96
  // ternary/object literal inside CONTENT. Replacing `\` before that colon
@@ -15,18 +15,10 @@ export function relativeGrepLine(
15
15
  if (!workDir) return normalized;
16
16
  if (pathOnly) return relativePathPrefix(normalized, workDir);
17
17
  if (filenameOmitted) return normalized;
18
- const split = splitGrepLinePrefix(normalized, matchOnly);
18
+ const split = outputMode === 'count' ? splitGrepCountPrefix(normalized) : splitGrepLinePrefix(normalized, matchOnly);
19
19
  if (split) {
20
20
  return relativePathPrefix(normalized.slice(0, split.pathEnd), workDir) + normalized.slice(split.pathEnd);
21
21
  }
22
- if (outputMode === 'count') {
23
- const countSplit = splitGrepCountPrefix(normalized);
24
- if (countSplit) {
25
- return (
26
- relativePathPrefix(normalized.slice(0, countSplit.pathEnd), workDir) + normalized.slice(countSplit.pathEnd)
27
- );
28
- }
29
- }
30
22
  return normalized;
31
23
  }
32
24
 
@@ -1,6 +1,6 @@
1
- import { AsyncLocalStorage } from 'node:async_hooks';
1
+ import { runtimeAsyncLocalStorage } from '../../../../shared/root-async-context.mjs';
2
2
 
3
- const localSearchTelemetry = new AsyncLocalStorage();
3
+ const localSearchTelemetry = runtimeAsyncLocalStorage();
4
4
 
5
5
  export function runWithLocalSearchTelemetry(telemetry, run) {
6
6
  return telemetry && typeof telemetry === 'object' ? localSearchTelemetry.run(telemetry, run) : run();
@@ -338,7 +338,7 @@ function _ensureServer() {
338
338
  return;
339
339
  }
340
340
  if (message?.event === 'invalidate' && Array.isArray(message.paths)) {
341
- invalidateBuiltinResultCache(message.paths.map(String));
341
+ invalidateBuiltinResultCache(message.paths.map(String), { canonical: true });
342
342
  return;
343
343
  }
344
344
  if (message?.event === 'cancelled') {
@@ -355,11 +355,15 @@ function _gitIgnoredEntries(directory, names) {
355
355
  * Boot-time prewarm: start the off-thread `git status` and `git check-ignore`
356
356
  * probes for directories a session is likely to open, so the first session's
357
357
  * prompt composition finds finished answers instead of spawning git on the
358
- * event loop. Never waits and never throws; a directory whose prewarm has not
359
- * answered yet when a session composes its prompt takes the synchronous probe
360
- * exactly as without prewarm.
358
+ * event loop. Never throws; a directory whose prewarm has not answered yet
359
+ * when a session composes its prompt takes the synchronous probe exactly as
360
+ * without prewarm. The returned promise (never rejects) settles once the
361
+ * probes of these directories that were not yet answered have: an async
362
+ * session-creation path awaits it before the synchronous composition, so that
363
+ * composition never has to take the blocking probe.
361
364
  */
362
365
  export function prewarmGitStartupProbes(directories) {
366
+ const unanswered = [];
363
367
  for (const raw of Array.isArray(directories) ? directories : []) {
364
368
  if (!raw) continue;
365
369
  let directory;
@@ -392,7 +396,12 @@ export function prewarmGitStartupProbes(directories) {
392
396
  entry.pending = null;
393
397
  });
394
398
  }
399
+ const changeState = _repositoryChangeStates.get(found.root);
400
+ if (!changeState.ready) unanswered.push(changeState.pending);
401
+ const ignored = _gitIgnoreObservations.get(directory);
402
+ if (!ignored.observation) unanswered.push(ignored.pending);
395
403
  }
404
+ return Promise.all(unanswered).then(() => {});
396
405
  }
397
406
 
398
407
  /** Resolves once every in-flight startup git probe has finished. */
@@ -30,6 +30,7 @@
30
30
  import { _graphRel } from './source-access.mjs';
31
31
  import { CONTAINER_SYMBOL_KINDS } from './constants.mjs';
32
32
  import { _symbolParentIndex, _symbolAncestors } from './text-columns.mjs';
33
+ import { _mainThreadSlice } from './main-thread-slice.mjs';
33
34
 
34
35
  // Receivers that keep a `method` call attributable to the declaring file
35
36
  // itself (an unqualified / self call), independent of imports.
@@ -253,8 +254,9 @@ function _astReachableTargetRels(graph, targetRels, name) {
253
254
  //
254
255
  // Node order: the cheap reachability/scope filters run BEFORE `_astCalls`, so
255
256
  // a query normalizes the call arrays of the declaring files and their
256
- // importers only — never of every node in the graph.
257
- export function _astCallerCallSites(
257
+ // importers only — never of every node in the graph. The walk is time-sliced:
258
+ // a common name reaches thousands of importers on a large root.
259
+ export async function _astCallerCallSites(
258
260
  graph,
259
261
  symbol,
260
262
  targetRels,
@@ -266,12 +268,17 @@ export function _astCallerCallSites(
266
268
  if (!name || !roots.length || !graph?.nodes) return out;
267
269
  const cwd = graph.cwd || '';
268
270
  const targets = _astReachableTargetRels(graph, roots, name);
271
+ const slice = _mainThreadSlice();
269
272
  for (const node of graph.nodes.values()) {
273
+ if (slice.expired()) await slice.yield();
270
274
  if (language && node.lang !== language) continue;
271
275
  if (!_nodeInAstScope(node, fileRel, scopeRelPrefix)) continue;
272
276
  const sameFile = targets.has(node.rel);
273
277
  const importsTarget = _astImportedRels(node, cwd).some((rel) => targets.has(rel));
274
278
  if (!sameFile && !importsTarget && !_astImportsUnresolved(node, cwd)) continue;
279
+ // No raw tuple named S → no row from this file, valid payload or not, so
280
+ // its call list is never normalized (thousands of importers on a large root).
281
+ if (!node.calls?.some?.((raw) => Array.isArray(raw) && raw[0] === name)) continue;
275
282
  const calls = _astCalls(node);
276
283
  if (!calls?.length) continue;
277
284
  for (const call of calls) {
@@ -29,7 +29,13 @@ import {
29
29
  CODE_GRAPH_WORKER_TIMEOUT_MS,
30
30
  SYMBOL_SCHEMA_VERSION,
31
31
  } from './constants.mjs';
32
- import { _computeGraphSignature, _deserializeGraph, _attachGraphRuntimeCaches } from './graph-model.mjs';
32
+ import {
33
+ _computeGraphSignature,
34
+ _deserializeGraph,
35
+ _attachGraphRuntimeCaches,
36
+ _estimateGraphBaseBytes,
37
+ _seedGraphBaseBytes,
38
+ } from './graph-model.mjs';
33
39
  import { _touchCodeGraphCache, _setCodeGraphCache } from './memory-cache.mjs';
34
40
  import {
35
41
  ensureDiskCodeGraphLoaded,
@@ -40,7 +46,7 @@ import {
40
46
  } from './disk-cache.mjs';
41
47
  import { _runGraphManifest, callsWireSignatureToken, awaitCallsWireProbe } from './graph-binary.mjs';
42
48
  import { assembleGraphNodes, linkGraphImports, resolveGraphFileInfos } from './build-file-infos.mjs';
43
- import { _lookupCandidateNodes } from './symbol-index.mjs';
49
+ import { _ensureTokenIndex, _lookupCandidateNodes } from './symbol-index.mjs';
44
50
  import { _findDirProjectRoot } from './project-root.mjs';
45
51
  import { raceAbort } from './dispatch/abort-race.mjs';
46
52
 
@@ -172,6 +178,37 @@ async function _validateDiskCodeGraphHit({
172
178
  return { graph, manifest, signature };
173
179
  }
174
180
 
181
+ // A resident graph past its TTL is revalidated with the same manifest
182
+ // signature check a disk entry gets, instead of being rebuilt in a Worker and
183
+ // structured-cloned back onto the main thread (hundreds of MB for a large
184
+ // root). A mismatch hands the manifest to the Worker so it is not re-run.
185
+ export async function _revalidateResidentCodeGraph({
186
+ graphCwd,
187
+ entry,
188
+ genAtStart,
189
+ signal = null,
190
+ acquireSlot = (signal) => acquireChildSpawnSlot(signal, 'code-graph-query'),
191
+ runManifest = _runGraphManifest,
192
+ computeSignature = _graphSignatureForCapability,
193
+ getGeneration = _getCodeGraphGen,
194
+ setMemoryCache = _setCodeGraphCache,
195
+ }) {
196
+ const release = await acquireSlot(signal || null);
197
+ let manifest;
198
+ try {
199
+ _throwIfAborted(signal);
200
+ manifest = await runManifest(graphCwd, signal);
201
+ } finally {
202
+ release();
203
+ }
204
+ _throwIfAborted(signal);
205
+ const signature = computeSignature(manifest);
206
+ if (entry.signature !== signature) return { graph: null, manifest, signature };
207
+ if (getGeneration(graphCwd) !== genAtStart) throw codeGraphBuildInvalidatedError(entry.graph);
208
+ setMemoryCache(graphCwd, { ...entry, ts: Date.now() });
209
+ return { graph: entry.graph, manifest, signature };
210
+ }
211
+
175
212
  // Validates the disk graph under a read-only query slot; a miss releases it
176
213
  // and spawnWorker queues for a build slot, so queries never wait behind
177
214
  // builds. Keeping this seam dependency-injectable makes slot/abort behavior
@@ -223,14 +260,89 @@ function _prepareDiskCodeGraphFastPath({
223
260
  // Worker disk writes are debounced with an unref'd timer. Drain before sending
224
261
  // success so every loaded cwd persists before this Worker can
225
262
  // exit; a drain failure is intentionally thrown to the worker's failure path.
226
- export function _postCodeGraphWorkerSuccess(
263
+ //
264
+ // The graph goes out in pieces: a header carrying the graph with every Map
265
+ // emptied, then each Map's entries in bounded chunks, then `done`. A single
266
+ // message holding the whole graph is deserialized on the receiving main thread
267
+ // in one uninterruptible structured-clone read (150-450 ms for a large root);
268
+ // chunks are each a short read. `nextChunk` (the worker awaits the receiver's
269
+ // ack) paces them one per main-thread event-loop turn — a port delivers every
270
+ // message already queued in one go, so unpaced chunks would be read back to
271
+ // back. Map order is preserved, so node iteration order — which answers depend
272
+ // on — is too.
273
+ const GRAPH_CHUNK_WEIGHT = 50_000;
274
+
275
+ function _graphChunkWeight(value) {
276
+ if (value instanceof Set) return value.size + 1;
277
+ if (typeof value?.text === 'string') return (value.text.length >> 6) + 1;
278
+ const calls = Array.isArray(value?.calls) ? value.calls.length : 0;
279
+ const tokens = Array.isArray(value?.tokenSymbols) ? value.tokenSymbols.length : 0;
280
+ const symbols = Array.isArray(value?.symbols) ? value.symbols.length : 0;
281
+ return 16 + calls * 8 + tokens + symbols * 8;
282
+ }
283
+
284
+ export async function _postCodeGraphWorkerSuccess(
227
285
  graph,
228
286
  postMessage,
229
287
  drainCache = drainCodeGraphCacheStrict,
230
- { cache = true } = {}
288
+ { cache = true, nextChunk = null } = {}
231
289
  ) {
232
290
  if (cache) drainCache();
233
- postMessage({ ok: true, signature: graph.signature, graph });
291
+ const baseBytes = _estimateGraphBaseBytes(graph);
292
+ const shell = {};
293
+ const maps = [];
294
+ for (const [key, value] of Object.entries(graph)) {
295
+ if (value instanceof Map) {
296
+ shell[key] = new Map();
297
+ maps.push([key, value]);
298
+ } else {
299
+ shell[key] = value;
300
+ }
301
+ }
302
+ postMessage({ ok: true, signature: graph.signature, shell, baseBytes });
303
+ const post = async (message) => {
304
+ await nextChunk?.();
305
+ postMessage(message);
306
+ };
307
+ for (const [key, map] of maps) {
308
+ let entries = [];
309
+ let weight = 0;
310
+ for (const entry of map) {
311
+ entries.push(entry);
312
+ weight += _graphChunkWeight(entry[1]);
313
+ if (weight >= GRAPH_CHUNK_WEIGHT) {
314
+ await post({ key, entries });
315
+ entries = [];
316
+ weight = 0;
317
+ }
318
+ }
319
+ if (entries.length) await post({ key, entries });
320
+ }
321
+ await post({ done: true });
322
+ }
323
+
324
+ // The receiver's ack that lets a paced sender post its next chunk.
325
+ export const CODE_GRAPH_CHUNK_ACK = 'code-graph-chunk-ack';
326
+
327
+ // Main-thread side of the chunked protocol: feed every worker message in
328
+ // order; returns null until the result is complete, then the result message
329
+ // ({ ok, signature, graph, baseBytes }) — or a failure message as is.
330
+ export function _codeGraphWorkerReceiver() {
331
+ let header = null;
332
+ return (message) => {
333
+ if (!header) {
334
+ if (!message?.ok || !message.shell) return message;
335
+ header = message;
336
+ return null;
337
+ }
338
+ if (typeof message?.key === 'string') {
339
+ const map = header.shell[message.key];
340
+ for (const [key, value] of message.entries) map.set(key, value);
341
+ return null;
342
+ }
343
+ if (!message?.done) return null;
344
+ return { ok: true, signature: header.signature, graph: header.shell, baseBytes: header.baseBytes };
345
+ };
234
346
  }
235
347
 
236
348
  // Structured keys avoid both separator collisions inside roots/prefixes and
@@ -258,8 +370,9 @@ export function prewarmCodeGraphSymbols(cwd, symbols, { language = null } = {})
258
370
  if (!cwd) return;
259
371
  const wanted = (Array.isArray(symbols) ? symbols : [symbols]).map((s) => String(s || '').trim()).filter(Boolean);
260
372
  buildCodeGraphAsync(cwd, null, { bestEffort: true })
261
- .then((graph) => {
373
+ .then(async (graph) => {
262
374
  if (!graph) return;
375
+ await _ensureTokenIndex(graph);
263
376
  for (const symbol of wanted) {
264
377
  try {
265
378
  _lookupCandidateNodes(graph, symbol, language);
@@ -318,6 +431,12 @@ export async function buildCodeGraphAsync(
318
431
  cacheResult: false,
319
432
  });
320
433
  }
434
+ const resident = _codeGraphCache.get(graphCwd);
435
+ if (resident?.graph) {
436
+ const hit = await _revalidateResidentCodeGraph({ graphCwd, entry: resident, genAtStart, signal });
437
+ if (hit.graph) return hit.graph;
438
+ return _spawnCodeGraphWorker(cwd, graphCwd, genAtStart, signal, hit.manifest, hit.signature);
439
+ }
321
440
  return _prepareDiskCodeGraphFastPath({
322
441
  graphCwd,
323
442
  runFastPath: (diskProbe) =>
@@ -453,11 +572,22 @@ export function _spawnCodeGraphWorker(
453
572
  };
454
573
  signal.addEventListener('abort', _onSignalAbort, { once: true });
455
574
  }
456
- w.once('message', (msg) => {
575
+ const receive = _codeGraphWorkerReceiver();
576
+ w.on('message', (part) => {
577
+ if (settled) return;
457
578
  try {
579
+ const msg = receive(part);
580
+ if (!msg) {
581
+ // Next chunk on a later turn, so other work runs between reads.
582
+ setImmediate(() => {
583
+ if (!settled) w.postMessage?.(CODE_GRAPH_CHUNK_ACK);
584
+ });
585
+ return;
586
+ }
458
587
  if (msg?.ok && msg.graph && typeof msg.signature === 'string') {
459
588
  const genStillCurrent = getGeneration(graphCwd) === genAtStart;
460
589
  if (genStillCurrent && cacheResult) {
590
+ _seedGraphBaseBytes(msg.graph, msg.baseBytes);
461
591
  setMemoryCache(graphCwd, { ts: Date.now(), signature: msg.signature, graph: msg.graph });
462
592
  // The Worker strictly persisted this graph before posting
463
593
  // success. Adopt it in the main process without scheduling a