@e4779/pi-fabric 0.92.41-fork.6 → 0.96.4-fork.10

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 (300) hide show
  1. package/LAWS.bend +502 -0
  2. package/PROOF.bend +452 -0
  3. package/README.md +5 -1
  4. package/THIRD_PARTY_NOTICES.md +9 -0
  5. package/dist/agents/handoff.d.ts +2 -1
  6. package/dist/agents/handoff.d.ts.map +1 -1
  7. package/dist/agents/manager.d.ts +2 -0
  8. package/dist/agents/manager.d.ts.map +1 -1
  9. package/dist/agents.js +6 -5
  10. package/dist/agents.js.map +1 -1
  11. package/dist/chunks/{chunk-3TXOQ5MG.js → chunk-3CJETB4S.js} +828 -204
  12. package/dist/chunks/chunk-3CJETB4S.js.map +7 -0
  13. package/dist/chunks/{chunk-IUCVOZRX.js → chunk-3SIXBW6A.js} +2 -2
  14. package/dist/chunks/chunk-4E63LJDD.js +468 -0
  15. package/dist/chunks/chunk-4E63LJDD.js.map +7 -0
  16. package/dist/chunks/{chunk-AAHHYDTO.js → chunk-4NF3S3NM.js} +1 -1
  17. package/dist/chunks/{chunk-AAHHYDTO.js.map → chunk-4NF3S3NM.js.map} +2 -2
  18. package/dist/chunks/{chunk-2SWRVIYJ.js → chunk-754HA6KO.js} +2 -2
  19. package/dist/chunks/{chunk-K6DHXXF5.js → chunk-B2IVWASP.js} +2 -2
  20. package/dist/chunks/{chunk-ZDNGXLEC.js → chunk-B4HECUJI.js} +2 -2
  21. package/dist/chunks/{chunk-H6DDV6E2.js → chunk-CSJSHBAT.js} +5 -5
  22. package/dist/chunks/{chunk-MW5VI7IW.js → chunk-D4B4CCTA.js} +2 -2
  23. package/dist/chunks/{chunk-EDL4JDW7.js → chunk-EBWMC3OO.js} +2 -2
  24. package/dist/chunks/{chunk-4NUTVNS2.js → chunk-F72MAIQY.js} +31 -368
  25. package/dist/chunks/chunk-F72MAIQY.js.map +7 -0
  26. package/dist/chunks/{chunk-W3FRE4VI.js → chunk-FDIEQEXS.js} +2 -2
  27. package/dist/chunks/{chunk-7A6NVHB5.js → chunk-FJSHRHOR.js} +7 -4
  28. package/dist/chunks/chunk-FJSHRHOR.js.map +7 -0
  29. package/dist/chunks/{chunk-PSNXUZMY.js → chunk-G2F2RDZI.js} +5 -47
  30. package/dist/chunks/chunk-G2F2RDZI.js.map +7 -0
  31. package/dist/chunks/{chunk-3WIGAX56.js → chunk-JCV54SSP.js} +2 -2
  32. package/dist/chunks/chunk-KS44WKJJ.js +51 -0
  33. package/dist/chunks/chunk-KS44WKJJ.js.map +7 -0
  34. package/dist/chunks/{chunk-3H7SDWZI.js → chunk-LNLQ2LX3.js} +22 -1
  35. package/dist/chunks/chunk-LNLQ2LX3.js.map +7 -0
  36. package/dist/chunks/{chunk-G4LADAYY.js → chunk-OINADFEF.js} +8 -3
  37. package/dist/chunks/chunk-OINADFEF.js.map +7 -0
  38. package/dist/chunks/{chunk-UQ4LTUH4.js → chunk-OUAC6O5C.js} +36 -9
  39. package/dist/chunks/chunk-OUAC6O5C.js.map +7 -0
  40. package/dist/chunks/{chunk-MQG5QKGI.js → chunk-Q5BMTL6A.js} +217 -95
  41. package/dist/chunks/chunk-Q5BMTL6A.js.map +7 -0
  42. package/dist/chunks/{chunk-IK6PPFWG.js → chunk-QFKHAAZA.js} +11 -11
  43. package/dist/chunks/chunk-QFKHAAZA.js.map +7 -0
  44. package/dist/chunks/{chunk-RTXZHKMS.js → chunk-QNMQJNXE.js} +7 -3
  45. package/dist/chunks/chunk-QNMQJNXE.js.map +7 -0
  46. package/dist/chunks/{chunk-U5FYOMG5.js → chunk-UBPFGMDQ.js} +6 -2
  47. package/dist/chunks/chunk-UBPFGMDQ.js.map +7 -0
  48. package/dist/chunks/{chunk-FLKGJPCN.js → chunk-ULQFHZJE.js} +4 -1
  49. package/dist/chunks/chunk-ULQFHZJE.js.map +7 -0
  50. package/dist/chunks/{chunk-7VAZ4N43.js → chunk-V3PIO7QL.js} +2 -2
  51. package/dist/chunks/{chunk-MGDLRV2R.js → chunk-VQGO4P7C.js} +2 -2
  52. package/dist/chunks/{chunk-XT4346N7.js → chunk-WXSELMFV.js} +9 -3
  53. package/dist/chunks/chunk-WXSELMFV.js.map +7 -0
  54. package/dist/chunks/{chunk-D5WKZONW.js → chunk-X3IRR7CV.js} +2 -2
  55. package/dist/chunks/{chunk-IJL3QK3J.js → chunk-Y57UHPIK.js} +1 -1
  56. package/dist/chunks/{chunk-IJL3QK3J.js.map → chunk-Y57UHPIK.js.map} +1 -1
  57. package/dist/chunks/{chunk-RKRVOBVO.js → chunk-YC3QQDZ3.js} +2 -2
  58. package/dist/chunks/{chunk-Q4XJ4PYL.js → chunk-ZRI433JP.js} +299 -83
  59. package/dist/chunks/chunk-ZRI433JP.js.map +7 -0
  60. package/dist/chunks/{chunk-36B4AIRT.js → chunk-ZTXZ6JDO.js} +2 -2
  61. package/dist/commands/fabric.d.ts.map +1 -1
  62. package/dist/compaction/bounds.d.ts +4 -0
  63. package/dist/compaction/bounds.d.ts.map +1 -1
  64. package/dist/compaction/branch-summary.d.ts.map +1 -1
  65. package/dist/compaction/dialogue.d.ts +24 -0
  66. package/dist/compaction/dialogue.d.ts.map +1 -0
  67. package/dist/compaction/hook.d.ts +6 -2
  68. package/dist/compaction/hook.d.ts.map +1 -1
  69. package/dist/compaction/hook.js +6 -5
  70. package/dist/compaction/instructions.d.ts +1 -0
  71. package/dist/compaction/instructions.d.ts.map +1 -1
  72. package/dist/compaction/projections.d.ts +3 -3
  73. package/dist/compaction/projections.d.ts.map +1 -1
  74. package/dist/compaction/qa.d.ts.map +1 -1
  75. package/dist/compaction/render.d.ts +11 -0
  76. package/dist/compaction/render.d.ts.map +1 -1
  77. package/dist/components/configuration.js +2 -3
  78. package/dist/components/effect-policy.d.ts +2 -0
  79. package/dist/components/effect-policy.d.ts.map +1 -1
  80. package/dist/components/effect-scope.d.ts.map +1 -1
  81. package/dist/components/supervisor.d.ts +1 -1
  82. package/dist/components/supervisor.d.ts.map +1 -1
  83. package/dist/core/action-registry.d.ts +14 -2
  84. package/dist/core/action-registry.d.ts.map +1 -1
  85. package/dist/core/action-registry.js +7 -5
  86. package/dist/core/provider-bindings.d.ts +8 -2
  87. package/dist/core/provider-bindings.d.ts.map +1 -1
  88. package/dist/core/provider-operations.d.ts +28 -0
  89. package/dist/core/provider-operations.d.ts.map +1 -0
  90. package/dist/core/provider-operations.js +201 -0
  91. package/dist/core/provider-operations.js.map +7 -0
  92. package/dist/core/shell-inbox.d.ts +13 -0
  93. package/dist/core/shell-inbox.d.ts.map +1 -0
  94. package/dist/core/shell-jobs.d.ts +51 -7
  95. package/dist/core/shell-jobs.d.ts.map +1 -1
  96. package/dist/core/shell-limits.d.ts +3 -0
  97. package/dist/core/shell-limits.d.ts.map +1 -0
  98. package/dist/core/shell-monitor.d.ts +51 -0
  99. package/dist/core/shell-monitor.d.ts.map +1 -0
  100. package/dist/entropy/index.js +8 -7
  101. package/dist/entropy/normal-form.d.ts.map +1 -1
  102. package/dist/execution-service.d.ts +1 -1
  103. package/dist/execution-service.d.ts.map +1 -1
  104. package/dist/fabric-runtime-state.d.ts +1 -1
  105. package/dist/fabric-runtime-state.d.ts.map +1 -1
  106. package/dist/fabric-runtime-state.js +1124 -325
  107. package/dist/fabric-runtime-state.js.map +4 -4
  108. package/dist/index.js +155 -44
  109. package/dist/index.js.map +3 -3
  110. package/dist/jev/observation.js +5 -3
  111. package/dist/jev.d.ts +0 -1
  112. package/dist/jev.d.ts.map +1 -1
  113. package/dist/jev.js +9 -13
  114. package/dist/memory/digest.d.ts.map +1 -1
  115. package/dist/memory/digest.js +2 -1
  116. package/dist/memory/discovery.js +6 -5
  117. package/dist/memory/expand-service.d.ts.map +1 -1
  118. package/dist/memory/file-worker.js +9 -8
  119. package/dist/memory/file-worker.js.map +1 -1
  120. package/dist/memory/index.d.ts.map +1 -1
  121. package/dist/memory/normalize.d.ts.map +1 -1
  122. package/dist/memory/normalize.js +5 -4
  123. package/dist/memory/search.js +7 -6
  124. package/dist/memory/worker-provider.js +10 -9
  125. package/dist/memory.js +9 -8
  126. package/dist/memory.js.map +1 -1
  127. package/dist/mesh/store.d.ts.map +1 -1
  128. package/dist/prewalk/handoff.d.ts +1 -1
  129. package/dist/prewalk/handoff.d.ts.map +1 -1
  130. package/dist/protocol.d.ts +2 -0
  131. package/dist/protocol.d.ts.map +1 -1
  132. package/dist/protocol.js +1 -1
  133. package/dist/providers/jev-provider.d.ts +1 -1
  134. package/dist/providers/jev-provider.d.ts.map +1 -1
  135. package/dist/providers/jev-provider.js +9 -7
  136. package/dist/providers/memory-provider.js +9 -8
  137. package/dist/providers/pi-tools-provider.d.ts.map +1 -1
  138. package/dist/providers/tasks-provider.d.ts +12 -0
  139. package/dist/providers/tasks-provider.d.ts.map +1 -0
  140. package/dist/residency/host.js +10 -9
  141. package/dist/residency/pi-entry.js +10 -9
  142. package/dist/residency/pi-entry.js.map +1 -1
  143. package/dist/runtime/core-override-guest-types.js +2 -2
  144. package/dist/runtime/guest-types.d.ts +1 -1
  145. package/dist/runtime/guest-types.d.ts.map +1 -1
  146. package/dist/runtime/guest-types.js +1 -1
  147. package/dist/runtime/monty-runtime.js +1 -2
  148. package/dist/runtime/monty-runtime.js.map +1 -1
  149. package/dist/runtime/typescript-kernel.js +2 -2
  150. package/dist/runtime-state-builtins.d.ts +1 -1
  151. package/dist/runtime-state-builtins.d.ts.map +1 -1
  152. package/dist/schema/controller.d.ts.map +1 -1
  153. package/dist/state/store.d.ts.map +1 -1
  154. package/dist/ui/controller.d.ts +1 -0
  155. package/dist/ui/controller.d.ts.map +1 -1
  156. package/dist/ui/conversation-targets.js +2 -2
  157. package/dist/ui/conversation.js +7 -7
  158. package/dist/ui/dashboard.js +9 -9
  159. package/dist/ui/highlight.d.ts.map +1 -1
  160. package/dist/ui/languages/bend.d.ts +4 -0
  161. package/dist/ui/languages/bend.d.ts.map +1 -0
  162. package/dist/ui/languages/bend.js +73 -0
  163. package/dist/ui/languages/bend.js.map +7 -0
  164. package/dist/ui/settings.js +1 -2
  165. package/dist/ui/settings.js.map +1 -1
  166. package/dist/ui/shell-hang-keys.d.ts.map +1 -1
  167. package/dist/ui/shell-tasks.d.ts +22 -0
  168. package/dist/ui/shell-tasks.d.ts.map +1 -0
  169. package/dist/ui/shell-tasks.js +161 -0
  170. package/dist/ui/shell-tasks.js.map +7 -0
  171. package/dist/ui/shiki-catalog.d.ts +2 -1
  172. package/dist/ui/shiki-catalog.d.ts.map +1 -1
  173. package/dist/ui/snapshot.d.ts.map +1 -1
  174. package/dist/ui/types.d.ts +1 -0
  175. package/dist/ui/types.d.ts.map +1 -1
  176. package/dist/ui/widget.d.ts.map +1 -1
  177. package/dist/verified/authority.d.ts +17 -0
  178. package/dist/verified/authority.d.ts.map +1 -0
  179. package/dist/verified/generated/authority-kernel.d.ts +10 -0
  180. package/dist/verified/generated/authority-kernel.js +116 -0
  181. package/dist/verified/generated/kernel.d.ts +31 -0
  182. package/dist/verified/generated/kernel.js +351 -0
  183. package/dist/verified/generated/lifecycle-kernel.d.ts +7 -0
  184. package/dist/verified/generated/lifecycle-kernel.js +155 -0
  185. package/dist/verified/generated/manifest.json +54 -0
  186. package/dist/verified/generated/provider-kernel.d.ts +9 -0
  187. package/dist/verified/generated/provider-kernel.js +80 -0
  188. package/dist/verified/generated/storage-kernel.d.ts +11 -0
  189. package/dist/verified/generated/storage-kernel.js +98 -0
  190. package/dist/verified/memory.d.ts +11 -0
  191. package/dist/verified/memory.d.ts.map +1 -0
  192. package/dist/verified/nat.d.ts +2 -0
  193. package/dist/verified/nat.d.ts.map +1 -0
  194. package/dist/verified/policy.d.ts +37 -0
  195. package/dist/verified/policy.d.ts.map +1 -0
  196. package/dist/verified/resources.d.ts +14 -0
  197. package/dist/verified/resources.d.ts.map +1 -0
  198. package/dist/verified/storage.d.ts +32 -0
  199. package/dist/verified/storage.d.ts.map +1 -0
  200. package/dist/worker/event-projection.d.ts +6 -0
  201. package/dist/worker/event-projection.d.ts.map +1 -0
  202. package/dist/worker/event-projection.js +99 -0
  203. package/dist/worker/event-projection.js.map +7 -0
  204. package/dist/worker/recovery-watchdog.d.ts +12 -0
  205. package/dist/worker/recovery-watchdog.d.ts.map +1 -0
  206. package/dist/worker/recovery-watchdog.js +41 -0
  207. package/dist/worker/recovery-watchdog.js.map +7 -0
  208. package/dist/worker.js +80 -22
  209. package/dist/worker.js.map +2 -2
  210. package/docs/agents.md +26 -14
  211. package/docs/architecture.md +1 -1
  212. package/docs/background-tasks.md +140 -0
  213. package/docs/compaction.md +91 -28
  214. package/docs/components.md +5 -1
  215. package/docs/configuration.md +6 -6
  216. package/docs/foreman.md +4 -4
  217. package/docs/harnesses.md +95 -0
  218. package/docs/interface.md +7 -0
  219. package/docs/jev.md +59 -52
  220. package/docs/kernels.md +2 -2
  221. package/docs/licenses/bend-apache-2.0.txt +202 -0
  222. package/docs/memory-recall.md +1 -1
  223. package/docs/programmatic-compaction.md +13 -2
  224. package/docs/provider-capabilities.md +197 -0
  225. package/docs/shell-middleware.md +3 -3
  226. package/docs/skills.md +1 -1
  227. package/docs/verified-kernels.md +278 -0
  228. package/package.json +14 -4
  229. package/proofs/abi.json +308 -0
  230. package/proofs/authority-abi.json +7 -0
  231. package/proofs/authority-kernel.bend +23 -0
  232. package/proofs/authority-proof.bend +74 -0
  233. package/proofs/authority-spec.bend +30 -0
  234. package/proofs/authority-state.bend +43 -0
  235. package/proofs/authority-types.d.ts +3 -0
  236. package/proofs/kernel.bend +139 -0
  237. package/proofs/lifecycle-abi.json +3 -0
  238. package/proofs/lifecycle-kernel.bend +11 -0
  239. package/proofs/lifecycle-proof.bend +249 -0
  240. package/proofs/lifecycle-spec.bend +261 -0
  241. package/proofs/lifecycle-types.d.ts +4 -0
  242. package/proofs/lifecycle.bend +119 -0
  243. package/proofs/provider-abi.json +4 -0
  244. package/proofs/provider-kernel.bend +14 -0
  245. package/proofs/provider-plans.bend +28 -0
  246. package/proofs/provider-spec.bend +14 -0
  247. package/proofs/provider-types.d.ts +5 -0
  248. package/proofs/resource-proof.bend +633 -0
  249. package/proofs/resource-spec.bend +133 -0
  250. package/proofs/resources.bend +160 -0
  251. package/proofs/state-kernel.bend +17 -0
  252. package/proofs/state-plans.bend +40 -0
  253. package/proofs/state-proof.bend +42 -0
  254. package/proofs/state-spec.bend +26 -0
  255. package/proofs/storage-abi.json +3 -0
  256. package/proofs/storage-kernel.bend +11 -0
  257. package/proofs/storage-plans.bend +100 -0
  258. package/proofs/storage-proof.bend +176 -0
  259. package/proofs/storage-spec.bend +70 -0
  260. package/proofs/storage-types.d.ts +8 -0
  261. package/proofs/types.d.ts +7 -0
  262. package/scripts/test-verified-kernels.mjs +107 -0
  263. package/scripts/verified-kernels.mjs +136 -0
  264. package/skillsets/python/fabric-jev/SKILL.md +13 -9
  265. package/skillsets/typescript/fabric-exec/SKILL.md +4 -2
  266. package/skillsets/typescript/fabric-jev/SKILL.md +13 -9
  267. package/dist/chunks/chunk-3H7SDWZI.js.map +0 -7
  268. package/dist/chunks/chunk-3TXOQ5MG.js.map +0 -7
  269. package/dist/chunks/chunk-4NUTVNS2.js.map +0 -7
  270. package/dist/chunks/chunk-77PDIBK2.js +0 -129
  271. package/dist/chunks/chunk-77PDIBK2.js.map +0 -7
  272. package/dist/chunks/chunk-7A6NVHB5.js.map +0 -7
  273. package/dist/chunks/chunk-FLKGJPCN.js.map +0 -7
  274. package/dist/chunks/chunk-G4LADAYY.js.map +0 -7
  275. package/dist/chunks/chunk-IK6PPFWG.js.map +0 -7
  276. package/dist/chunks/chunk-MQG5QKGI.js.map +0 -7
  277. package/dist/chunks/chunk-PSNXUZMY.js.map +0 -7
  278. package/dist/chunks/chunk-Q4XJ4PYL.js.map +0 -7
  279. package/dist/chunks/chunk-RTXZHKMS.js.map +0 -7
  280. package/dist/chunks/chunk-U5FYOMG5.js.map +0 -7
  281. package/dist/chunks/chunk-UQ4LTUH4.js.map +0 -7
  282. package/dist/chunks/chunk-XT4346N7.js.map +0 -7
  283. package/dist/jev/browser.d.ts +0 -37
  284. package/dist/jev/browser.d.ts.map +0 -1
  285. package/dist/jev/browser.js +0 -13
  286. package/dist/jev/browser.js.map +0 -7
  287. /package/dist/chunks/{chunk-IUCVOZRX.js.map → chunk-3SIXBW6A.js.map} +0 -0
  288. /package/dist/chunks/{chunk-2SWRVIYJ.js.map → chunk-754HA6KO.js.map} +0 -0
  289. /package/dist/chunks/{chunk-K6DHXXF5.js.map → chunk-B2IVWASP.js.map} +0 -0
  290. /package/dist/chunks/{chunk-ZDNGXLEC.js.map → chunk-B4HECUJI.js.map} +0 -0
  291. /package/dist/chunks/{chunk-H6DDV6E2.js.map → chunk-CSJSHBAT.js.map} +0 -0
  292. /package/dist/chunks/{chunk-MW5VI7IW.js.map → chunk-D4B4CCTA.js.map} +0 -0
  293. /package/dist/chunks/{chunk-EDL4JDW7.js.map → chunk-EBWMC3OO.js.map} +0 -0
  294. /package/dist/chunks/{chunk-W3FRE4VI.js.map → chunk-FDIEQEXS.js.map} +0 -0
  295. /package/dist/chunks/{chunk-3WIGAX56.js.map → chunk-JCV54SSP.js.map} +0 -0
  296. /package/dist/chunks/{chunk-7VAZ4N43.js.map → chunk-V3PIO7QL.js.map} +0 -0
  297. /package/dist/chunks/{chunk-MGDLRV2R.js.map → chunk-VQGO4P7C.js.map} +0 -0
  298. /package/dist/chunks/{chunk-D5WKZONW.js.map → chunk-X3IRR7CV.js.map} +0 -0
  299. /package/dist/chunks/{chunk-RKRVOBVO.js.map → chunk-YC3QQDZ3.js.map} +0 -0
  300. /package/dist/chunks/{chunk-36B4AIRT.js.map → chunk-ZTXZ6JDO.js.map} +0 -0
@@ -10,16 +10,21 @@ import {
10
10
  FABRIC_BRANCH_SUMMARY_VERSION,
11
11
  readFabricBranchSummaryDetails,
12
12
  readFabricProjectionTrace
13
- } from "./chunk-ZDNGXLEC.js";
13
+ } from "./chunk-B4HECUJI.js";
14
14
  import {
15
15
  MAX_SUMMARY_BYTES,
16
+ boundedExcerpt,
16
17
  clipUtf8,
17
18
  decodeCompactionInstructions,
18
19
  omissionLine,
19
20
  sampleAddressed,
20
21
  sampleAddressedFrom,
21
22
  utf8Bytes
22
- } from "./chunk-3H7SDWZI.js";
23
+ } from "./chunk-LNLQ2LX3.js";
24
+ import {
25
+ acceptCompactionCut,
26
+ acceptSummaryBounds
27
+ } from "./chunk-4E63LJDD.js";
23
28
 
24
29
  // src/compaction/hook.ts
25
30
  import "@earendil-works/pi-coding-agent";
@@ -615,12 +620,67 @@ var countErasedThinkingBlocks = (entries) => {
615
620
  return count;
616
621
  };
617
622
 
623
+ // src/compaction/dialogue.ts
624
+ var MAX_RECENT_EXCHANGES = 3;
625
+ var MAX_EXCHANGE_BYTES = 3968;
626
+ var recentDialogue = (events, retainedTail = []) => {
627
+ const exchanges = [];
628
+ let assistant;
629
+ const visit = (source, retained) => {
630
+ for (const event of source) {
631
+ if (event.kind === "assistantText" && event.text.trim()) {
632
+ if (assistant?.entryId === event.entryId) assistant.text += `
633
+ ${event.text}`;
634
+ else assistant = { entryId: event.entryId, text: event.text, retained };
635
+ } else if (event.kind === "user") {
636
+ if (!retained || assistant?.retained === false) {
637
+ exchanges.push({
638
+ user: { entryId: event.entryId, text: event.text, retained },
639
+ ...assistant ? { assistant } : {}
640
+ });
641
+ if (exchanges.length > MAX_RECENT_EXCHANGES) exchanges.shift();
642
+ }
643
+ assistant = void 0;
644
+ }
645
+ }
646
+ };
647
+ visit(events, false);
648
+ visit(retainedTail, true);
649
+ return exchanges;
650
+ };
651
+ var dialogueExcerpts = (exchange) => {
652
+ const heading = (role, message) => `${role} [entry ${message.entryId}${message.retained ? "; retained raw" : ""}]:`;
653
+ const userHeading = heading("User instruction", exchange.user);
654
+ const assistantHeading = exchange.assistant ? heading("Historical assistant response (not a verified outcome)", exchange.assistant) : "";
655
+ const available = Math.max(0, MAX_EXCHANGE_BYTES - utf8Bytes(userHeading + assistantHeading) - 8);
656
+ const userText = exchange.user.retained ? "" : exchange.user.text;
657
+ const assistantBytes = exchange.assistant && !exchange.assistant.retained ? utf8Bytes(exchange.assistant.text) : 0;
658
+ const userBudget = exchange.assistant ? Math.max(available / 2, available - assistantBytes) : available;
659
+ const user = boundedExcerpt(userText, Math.floor(userBudget));
660
+ const excerpts = [{ heading: userHeading, ...user }];
661
+ if (exchange.assistant) {
662
+ const assistant = boundedExcerpt(
663
+ exchange.assistant.retained ? "" : exchange.assistant.text,
664
+ available - utf8Bytes(user.text)
665
+ );
666
+ excerpts.unshift({ heading: assistantHeading, ...assistant });
667
+ }
668
+ return excerpts;
669
+ };
670
+ var projectDialogue = (exchanges) => {
671
+ let omittedBytes = 0;
672
+ const lines = exchanges.map((exchange) => dialogueExcerpts(exchange).map((excerpt) => {
673
+ omittedBytes += excerpt.omittedBytes;
674
+ return `${excerpt.heading}
675
+ ${excerpt.text}`;
676
+ }).join("\n\n"));
677
+ return { lines, omittedBytes };
678
+ };
679
+
618
680
  // src/compaction/projections.ts
619
681
  var MAX_LINE = 140;
620
682
  var FILE_TOOLS = /* @__PURE__ */ new Set(["read", "edit", "write", "grep", "find", "ls"]);
621
683
  var MODIFYING_TOOLS = /* @__PURE__ */ new Set(["edit", "write"]);
622
- var MAX_USER_GOAL_LINES = 3;
623
- var MAX_USER_GOAL_LINE = 1024;
624
684
  var MAX_USER_ONELINER = 120;
625
685
  var MAX_EARLIER_USER = 80;
626
686
  var MAX_STATUS_LINE = 140;
@@ -656,10 +716,6 @@ var customMessageLine = (event, maxText) => {
656
716
  const visibility = event.display ? "visible" : "hidden";
657
717
  return `custom ${quoted(event.customType, 80)} (${visibility}): ${quoted(event.text, maxText)}${customDetailsSuffix(event.details)}`;
658
718
  };
659
- var trailingEllipsis = (lines, max) => {
660
- if (lines.length <= max) return lines;
661
- return [...lines.slice(0, max), "\u2026"];
662
- };
663
719
  var pathOf = (args) => {
664
720
  const value = args.path ?? args.file ?? args.dir;
665
721
  return typeof value === "string" && value.trim() ? value : void 0;
@@ -745,27 +801,14 @@ var commonRoot = (paths) => {
745
801
  return `${first.slice(0, common).join("/")}/`;
746
802
  };
747
803
  var stripRoot = (root, path) => root ? path.replace(root, "") : path;
748
- var projectGoal = (events) => {
749
- const first = events.find(
750
- (event) => event.kind === "user"
751
- );
752
- if (!first) return { lines: [], omitted: 0 };
753
- const firstLines = first.text.split("\n").filter(
754
- (line, i, arr) => line.trim() !== "" || i === 0 && arr.length === 1
755
- ).map((line) => truncate(line, MAX_USER_GOAL_LINE));
756
- const lines = [...trailingEllipsis(firstLines, MAX_USER_GOAL_LINES)];
757
- function* laterUsers() {
758
- let skippedFirst = false;
804
+ var projectGoal = (events, protectedUsers) => {
805
+ const lines = [];
806
+ function* historicalUsers() {
759
807
  for (const event of events) {
760
- if (event.kind !== "user") continue;
761
- if (!skippedFirst) {
762
- skippedFirst = true;
763
- continue;
764
- }
765
- yield event;
808
+ if (event.kind === "user" && !protectedUsers.has(event.entryId)) yield event;
766
809
  }
767
810
  }
768
- const sampled = sampleAddressedFrom(laterUsers(), MAX_LATER_GOALS);
811
+ const sampled = sampleAddressedFrom(historicalUsers(), MAX_LATER_GOALS);
769
812
  for (let index = 0; index < sampled.values.length; index++) {
770
813
  if (sampled.omitted > 0 && index === sampled.splitIndex) {
771
814
  lines.push(omissionLine(
@@ -1079,8 +1122,10 @@ var projectTranscript = (events) => {
1079
1122
  }
1080
1123
  return { lines, omitted };
1081
1124
  };
1082
- var projectWithMetadata = (events) => {
1083
- const goal = projectGoal(events);
1125
+ var projectWithMetadata = (events, retainedDialogue = []) => {
1126
+ const exchanges = recentDialogue(events, retainedDialogue);
1127
+ const dialogue = projectDialogue(exchanges);
1128
+ const goal = projectGoal(events, new Set(exchanges.map(({ user }) => user.entryId)));
1084
1129
  const files = projectFiles(events);
1085
1130
  const activity = projectActivity(events);
1086
1131
  const outstanding = projectOutstandingWithMetadata(events);
@@ -1088,6 +1133,7 @@ var projectWithMetadata = (events) => {
1088
1133
  const transcript = projectTranscript(events);
1089
1134
  return {
1090
1135
  sections: {
1136
+ dialogue: dialogue.lines,
1091
1137
  goal: goal.lines,
1092
1138
  files: files.lines,
1093
1139
  activity: activity.lines,
@@ -1098,6 +1144,7 @@ var projectWithMetadata = (events) => {
1098
1144
  },
1099
1145
  omittedCounts: {
1100
1146
  goal: goal.omitted,
1147
+ dialogueBytes: dialogue.omittedBytes,
1101
1148
  files: files.omitted,
1102
1149
  activity: activity.omitted,
1103
1150
  outstanding: outstanding.omitted,
@@ -1108,8 +1155,9 @@ var projectWithMetadata = (events) => {
1108
1155
  };
1109
1156
 
1110
1157
  // src/compaction/render.ts
1111
- var SECTION_ORDER = [
1112
- { key: "goal", header: "[Session Goal]", maxBytes: 4096 },
1158
+ var SUMMARY_SECTIONS = [
1159
+ { key: "dialogue", header: "[Recent user directions and discussion]", maxBytes: 12288 },
1160
+ { key: "goal", header: "[Historical requests]", maxBytes: 4096 },
1113
1161
  { key: "files", header: "[Files And Changes]", maxBytes: 4608 },
1114
1162
  { key: "activity", header: "[Fabric Activity]", maxBytes: 2048 },
1115
1163
  { key: "outstanding", header: "[Outstanding Context]", maxBytes: 4608 },
@@ -1142,42 +1190,89 @@ var boundedBlock = (header, sourceLines, maxBytes) => {
1142
1190
  }
1143
1191
  return clipUtf8(header, maxBytes);
1144
1192
  };
1145
- var renderSummary = (sections, options) => {
1193
+ var boundedHistory = (blocks, maxBytes) => {
1194
+ const candidates = blocks.map((block) => {
1195
+ const text = boundedBlock(block.header, block.lines, block.maxBytes);
1196
+ const bytes = utf8Bytes(text);
1197
+ const minimum2 = block.lines.length === 1 ? bytes : Math.min(bytes, utf8Bytes([block.header, ...sampledLines(block.lines, 0)].join("\n")));
1198
+ return { ...block, text, bytes, minimum: minimum2 };
1199
+ });
1200
+ const total = candidates.reduce((sum, block) => sum + block.bytes, 0);
1201
+ if (total <= maxBytes) return candidates.map(({ text }) => text);
1202
+ const minimum = candidates.reduce((sum, block) => sum + block.minimum, 0);
1203
+ let remaining = maxBytes - minimum;
1204
+ let demand = total - minimum;
1205
+ return candidates.map((block) => {
1206
+ const wanted = block.bytes - block.minimum;
1207
+ const extra = demand === 0 ? 0 : Math.min(wanted, Math.floor(remaining * wanted / demand));
1208
+ const text = boundedBlock(block.header, block.lines, block.minimum + extra);
1209
+ remaining -= utf8Bytes(text) - block.minimum;
1210
+ demand -= wanted;
1211
+ return text;
1212
+ });
1213
+ };
1214
+ var protectedBlock = (header, items, maxBytes) => {
1215
+ const text = [header, ...items].join("\n\n");
1216
+ if (utf8Bytes(text) <= maxBytes) return { text, omittedBytes: 0 };
1217
+ let remaining = Math.max(0, maxBytes - utf8Bytes(header) - 2 * items.length);
1218
+ const share = Math.floor(remaining / items.length);
1219
+ const budgets = items.map((item) => Math.min(utf8Bytes(item), share));
1220
+ remaining -= budgets.reduce((sum, bytes) => sum + bytes, 0);
1221
+ let omittedBytes = 0;
1222
+ const excerpts = items.map((item, index) => {
1223
+ const extra = Math.min(remaining, utf8Bytes(item) - budgets[index]);
1224
+ remaining -= extra;
1225
+ const excerpt = boundedExcerpt(item, budgets[index] + extra);
1226
+ omittedBytes += excerpt.omittedBytes;
1227
+ return excerpt.text;
1228
+ });
1229
+ return { text: [header, ...excerpts].join("\n\n"), omittedBytes };
1230
+ };
1231
+ var renderSummaryWithMetadata = (sections, options) => {
1146
1232
  const blocks = [];
1147
- for (const { key, header, maxBytes } of SECTION_ORDER) {
1233
+ const history = [];
1234
+ let requestOmittedBytes = 0;
1235
+ let dialogueOmittedBytes = 0;
1236
+ const request = () => {
1237
+ if (!options.requestLines?.length) return;
1238
+ const rendered = protectedBlock("[Compaction Request]", options.requestLines, REQUEST_MAX_BYTES);
1239
+ blocks.push(rendered.text);
1240
+ requestOmittedBytes = rendered.omittedBytes;
1241
+ };
1242
+ for (const { key, header, maxBytes } of SUMMARY_SECTIONS) {
1148
1243
  const lines = sections[key];
1149
- if (lines.length === 0) continue;
1150
- blocks.push(boundedBlock(header, lines, maxBytes));
1151
- if (key === "goal" && options.requestLines && options.requestLines.length > 0) {
1152
- blocks.push(boundedBlock("[Compaction Request]", options.requestLines, REQUEST_MAX_BYTES));
1244
+ if (key === "dialogue") {
1245
+ if (lines.length > 0) {
1246
+ const rendered = protectedBlock(header, lines, maxBytes);
1247
+ blocks.push(rendered.text);
1248
+ dialogueOmittedBytes = rendered.omittedBytes;
1249
+ }
1250
+ request();
1251
+ } else if (lines.length > 0) {
1252
+ history.push({ header, lines, maxBytes });
1153
1253
  }
1154
1254
  }
1155
- if (sections.goal.length === 0 && options.requestLines && options.requestLines.length > 0) {
1156
- blocks.unshift(boundedBlock("[Compaction Request]", options.requestLines, REQUEST_MAX_BYTES));
1157
- }
1158
1255
  if (sections.transcript.length > 0) {
1159
- blocks.push(boundedBlock("---", sections.transcript, TRANSCRIPT_MAX_BYTES));
1256
+ history.push({ header: "---", lines: sections.transcript, maxBytes: TRANSCRIPT_MAX_BYTES });
1160
1257
  }
1161
1258
  const timestamp = options.lastTimestamp || "(unknown time)";
1162
1259
  const range = options.firstEntryId || options.lastEntryId ? `${options.firstEntryId || "(start)"} \u2192 ${options.lastEntryId || "(end)"}` : "(no entries)";
1163
1260
  const footer = options.summaryKind === "branch" ? `[branch summarized ${timestamp}; structural source entries ${range}]` : `[compacted ${timestamp}; cumulative source entries ${range}]`;
1164
- blocks.push(boundedBlock("---", [footer, POINTER_LINE], FOOTER_MAX_BYTES));
1261
+ const footerBlock = boundedBlock("---", [footer, POINTER_LINE], FOOTER_MAX_BYTES);
1262
+ const framingBytes = utf8Bytes(footerBlock) + 2 * (blocks.length + history.length) + 1;
1263
+ const protectedBytes = blocks.reduce((sum, block) => sum + utf8Bytes(block), 0);
1264
+ blocks.push(...boundedHistory(history, MAX_SUMMARY_BYTES - framingBytes - protectedBytes), footerBlock);
1165
1265
  const summary = `${blocks.join("\n\n")}
1166
1266
  `;
1167
- if (utf8Bytes(summary) <= MAX_SUMMARY_BYTES) return summary;
1168
- return `${clipUtf8(summary, MAX_SUMMARY_BYTES - 1, "")}
1169
- `;
1267
+ return {
1268
+ summary: utf8Bytes(summary) <= MAX_SUMMARY_BYTES ? summary : `${clipUtf8(summary, MAX_SUMMARY_BYTES - 1, "")}
1269
+ `,
1270
+ requestOmittedBytes,
1271
+ dialogueOmittedBytes
1272
+ };
1170
1273
  };
1171
1274
 
1172
1275
  // src/compaction/branch-summary.ts
1173
- var SECTION_HEADERS = [
1174
- { key: "goal", header: "[Session Goal]" },
1175
- { key: "files", header: "[Files And Changes]" },
1176
- { key: "activity", header: "[Fabric Activity]" },
1177
- { key: "outstanding", header: "[Outstanding Context]" },
1178
- { key: "earlierTurns", header: "[Earlier Turns]" },
1179
- { key: "status", header: "[Current Status]" }
1180
- ];
1181
1276
  var asJsonObject = (value) => {
1182
1277
  try {
1183
1278
  const cloned = JSON.parse(JSON.stringify(value));
@@ -1331,22 +1426,24 @@ var compileFabricBranchSummary = (entriesToSummarize, customInstructions, enrich
1331
1426
  if (events.length === 0) return void 0;
1332
1427
  const projected = projectWithMetadata(events);
1333
1428
  runEnrichers(enrichers, events, projected.sections);
1334
- const request = {
1335
- text: instructions.requestLines.join("\n"),
1336
- sourceBytes: instructions.policy.sourceBytes,
1337
- truncated: instructions.policy.truncated
1338
- };
1339
- const sections = SECTION_HEADERS.filter(({ key }) => projected.sections[key].length > 0).map(({ header }) => header);
1340
- if (request.text) sections.splice(1, 0, "[Compaction Request]");
1341
- const summary = renderSummary(projected.sections, {
1429
+ const sections = SUMMARY_SECTIONS.filter(({ key }) => projected.sections[key].length > 0).map(({ header }) => header);
1430
+ if (instructions.requestLines.length > 0) {
1431
+ sections.splice(projected.sections.dialogue.length > 0 ? 1 : 0, 0, "[Compaction Request]");
1432
+ }
1433
+ const rendered = renderSummaryWithMetadata(projected.sections, {
1342
1434
  firstEntryId: entriesToSummarize[0]?.id ?? "",
1343
1435
  lastEntryId: entriesToSummarize.at(-1)?.id ?? "",
1344
1436
  lastTimestamp: entriesToSummarize.at(-1)?.timestamp ?? "",
1345
1437
  ...instructions.requestLines.length > 0 ? { requestLines: instructions.requestLines } : {},
1346
1438
  summaryKind: "branch"
1347
1439
  });
1440
+ const request = {
1441
+ text: instructions.requestLines.join("\n"),
1442
+ sourceBytes: instructions.policy.sourceBytes,
1443
+ truncated: instructions.policy.truncated || rendered.requestOmittedBytes > 0
1444
+ };
1348
1445
  return {
1349
- summary,
1446
+ summary: rendered.summary,
1350
1447
  details: boundedDetails(entriesToSummarize, factsFromEvents(events), sections, request, oldLeafId)
1351
1448
  };
1352
1449
  };
@@ -1529,18 +1626,15 @@ var tokenCalibration = (branchEntries, tokensBefore, rawTokensBefore) => {
1529
1626
  );
1530
1627
  return { tokenScale, fixedOverheadTokens };
1531
1628
  };
1629
+ var continuityTailLimit = (value) => value !== void 0 && Number.isSafeInteger(value) && value >= 0 ? value : DEFAULT_COMPACTION_SETTINGS.keepRecentTokens;
1532
1630
  var continuityCutPlan = (branchEntries, tokensBefore, budget) => {
1533
- if (!Number.isFinite(budget.contextWindow) || budget.contextWindow <= 0) return void 0;
1631
+ if (typeof budget.contextWindow !== "number" || !Number.isFinite(budget.contextWindow) || budget.contextWindow <= 0 || typeof budget.targetContextRatio !== "number" || !Number.isFinite(budget.targetContextRatio)) return void 0;
1534
1632
  const contextWindow = Math.floor(budget.contextWindow);
1535
1633
  const reserveTokens = Math.max(0, Math.floor(budget.reserveTokens));
1536
- const keepRecentTokens = Math.max(0, Math.floor(budget.keepRecentTokens));
1634
+ const keepRecentTokens = continuityTailLimit(budget.keepRecentTokens);
1537
1635
  const targetContextRatio = Math.max(0.25, Math.min(0.85, budget.targetContextRatio));
1538
1636
  const rawTokensBefore = rawContextTokens(branchEntries);
1539
- const { tokenScale, fixedOverheadTokens } = tokenCalibration(
1540
- branchEntries,
1541
- tokensBefore,
1542
- rawTokensBefore
1543
- );
1637
+ const { tokenScale, fixedOverheadTokens } = budget.calibrateUsage === false ? { tokenScale: 1, fixedOverheadTokens: 0 } : tokenCalibration(branchEntries, tokensBefore, rawTokensBefore);
1544
1638
  const continuityTargetTokens = Math.max(1, Math.ceil(
1545
1639
  fixedOverheadTokens + (keepRecentTokens + SUMMARY_RAW_TOKEN_BUDGET) * tokenScale
1546
1640
  ));
@@ -1593,7 +1687,7 @@ var boundary = (summarized, firstKeptEntryId, budget) => {
1593
1687
  ...budget ? { budget } : {}
1594
1688
  };
1595
1689
  };
1596
- var computeContinuityCut = (branchEntries, live, plan) => {
1690
+ var computeContinuityCut = (branchEntries, live, plan, rawTailTokenBudget = plan?.rawTailTokenBudget ?? DEFAULT_COMPACTION_SETTINGS.keepRecentTokens) => {
1597
1691
  const suffixTokens = new Array(live.length + 1).fill(0);
1598
1692
  for (let index = live.length - 1; index >= 0; index--) {
1599
1693
  suffixTokens[index] = suffixTokens[index + 1] + live[index].estimatedTokens;
@@ -1604,13 +1698,13 @@ var computeContinuityCut = (branchEntries, live, plan) => {
1604
1698
  for (let index = 1; index < live.length; index++) {
1605
1699
  const item = live[index];
1606
1700
  if (item.branchIndex <= previousCompactionIndex) continue;
1607
- if (!item.cutPoint || suffixTokens[index] > plan.rawTailTokenBudget) continue;
1701
+ if (!item.cutPoint || suffixTokens[index] > rawTailTokenBudget) continue;
1608
1702
  if (!closureSafe(spans, item.branchIndex)) continue;
1609
1703
  cutIndex = index;
1610
1704
  break;
1611
1705
  }
1612
1706
  const retainedRawTokens = suffixTokens[cutIndex] ?? 0;
1613
- const details = {
1707
+ const details = plan ? {
1614
1708
  strategy: "continuity",
1615
1709
  contextWindow: plan.contextWindow,
1616
1710
  targetContextRatio: plan.targetContextRatio,
@@ -1627,7 +1721,7 @@ var computeContinuityCut = (branchEntries, live, plan) => {
1627
1721
  reductionCeilingTokens: plan.reductionCeilingTokens,
1628
1722
  rawTailTokenBudget: plan.rawTailTokenBudget,
1629
1723
  bindingConstraint: plan.bindingConstraint
1630
- };
1724
+ } : void 0;
1631
1725
  if (cutIndex >= live.length) {
1632
1726
  return boundary(live.map((item) => item.entry), "", details);
1633
1727
  }
@@ -1637,7 +1731,7 @@ var computeContinuityCut = (branchEntries, live, plan) => {
1637
1731
  details
1638
1732
  );
1639
1733
  };
1640
- var computeCut = (branchEntries, options) => {
1734
+ var proposeCut = (branchEntries, options) => {
1641
1735
  const live = collectLive(branchEntries);
1642
1736
  if (live.length === 0) return { ok: false, reason: "empty" };
1643
1737
  if (options) {
@@ -1645,8 +1739,12 @@ var computeCut = (branchEntries, options) => {
1645
1739
  if (plan) return computeContinuityCut(branchEntries, live, plan);
1646
1740
  }
1647
1741
  const lastBoundary = lastBoundaryIndex(live);
1648
- if (lastBoundary <= 0) return boundary(live.map((item) => item.entry), "");
1649
- const closed = closeCut(branchEntries, live, lastBoundary);
1742
+ const closed = lastBoundary > 0 ? closeCut(branchEntries, live, lastBoundary) : 0;
1743
+ const keepRecentTokens = continuityTailLimit(options?.budget.keepRecentTokens);
1744
+ const tailTokens = live.slice(closed).reduce((sum, item) => sum + item.estimatedTokens, 0);
1745
+ if (tailTokens > keepRecentTokens) {
1746
+ return computeContinuityCut(branchEntries, live, void 0, keepRecentTokens);
1747
+ }
1650
1748
  const previousCompactionIndex = findLastCompaction(branchEntries)?.index ?? -1;
1651
1749
  if (closed <= 0 || live[closed].branchIndex <= previousCompactionIndex) {
1652
1750
  return boundary(live.map((item) => item.entry), "");
@@ -1656,6 +1754,24 @@ var computeCut = (branchEntries, options) => {
1656
1754
  live[closed].entry.id
1657
1755
  );
1658
1756
  };
1757
+ var computeCut = (branchEntries, options) => {
1758
+ const proposed = proposeCut(branchEntries, options);
1759
+ if (!proposed.ok) return proposed;
1760
+ const live = collectLive(branchEntries);
1761
+ const kept = proposed.firstKeptEntryId ? live.findIndex((item) => item.entry.id === proposed.firstKeptEntryId) : live.length;
1762
+ if (kept < 0) return { ok: false, reason: "empty" };
1763
+ const boundaryIndex = live[kept]?.branchIndex ?? branchEntries.length;
1764
+ const retained = live.slice(kept).reduce((sum, item) => sum + item.estimatedTokens, 0);
1765
+ const budget = proposed.budget?.rawTailTokenBudget ?? continuityTailLimit(options?.budget.keepRecentTokens);
1766
+ if (!acceptCompactionCut({
1767
+ eligible: kept === live.length || live[kept].cutPoint,
1768
+ afterPrevious: boundaryIndex > (findLastCompaction(branchEntries)?.index ?? -1),
1769
+ retained,
1770
+ budget,
1771
+ boundary: boundaryIndex
1772
+ }, callResultSpans(branchEntries).values())) return { ok: false, reason: "empty" };
1773
+ return proposed;
1774
+ };
1659
1775
  var isRecord = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
1660
1776
  var isEntryRange = (value) => isRecord(value) && typeof value.first === "string" && typeof value.last === "string";
1661
1777
  var isStringArray = (value) => Array.isArray(value) && value.every((item) => typeof item === "string");
@@ -1708,7 +1824,7 @@ var isFabricV2Details = (value) => {
1708
1824
  "earlierTurns",
1709
1825
  "transcript",
1710
1826
  "preserve"
1711
- ]) && (value.omittedCounts.activity === void 0 || typeof value.omittedCounts.activity === "number" && Number.isFinite(value.omittedCounts.activity)) && (value.omittedCounts.commits === void 0 || typeof value.omittedCounts.commits === "number" && Number.isFinite(value.omittedCounts.commits)) && (value.omittedCounts.thinking === void 0 || typeof value.omittedCounts.thinking === "number" && Number.isFinite(value.omittedCounts.thinking)) && typeof value.instructionPolicy.mode === "string" && instructionModes.has(value.instructionPolicy.mode) && typeof value.instructionPolicy.canonicalized === "boolean" && typeof value.instructionPolicy.truncated === "boolean" && hasFiniteNumbers(value.instructionPolicy, [
1827
+ ]) && (value.omittedCounts.activity === void 0 || typeof value.omittedCounts.activity === "number" && Number.isFinite(value.omittedCounts.activity)) && (value.omittedCounts.commits === void 0 || typeof value.omittedCounts.commits === "number" && Number.isFinite(value.omittedCounts.commits)) && (value.omittedCounts.thinking === void 0 || typeof value.omittedCounts.thinking === "number" && Number.isFinite(value.omittedCounts.thinking)) && (value.omittedCounts.dialogueBytes === void 0 || typeof value.omittedCounts.dialogueBytes === "number" && Number.isFinite(value.omittedCounts.dialogueBytes) && value.omittedCounts.dialogueBytes >= 0) && (value.instructionPolicy.renderedOmittedBytes === void 0 || typeof value.instructionPolicy.renderedOmittedBytes === "number" && Number.isFinite(value.instructionPolicy.renderedOmittedBytes) && value.instructionPolicy.renderedOmittedBytes >= 0) && typeof value.instructionPolicy.mode === "string" && instructionModes.has(value.instructionPolicy.mode) && typeof value.instructionPolicy.canonicalized === "boolean" && typeof value.instructionPolicy.truncated === "boolean" && hasFiniteNumbers(value.instructionPolicy, [
1712
1828
  "sourceBytes",
1713
1829
  "preserveCount",
1714
1830
  "omittedPreserveCount"
@@ -1764,28 +1880,37 @@ var compileFabricSummary = (branchEntries, tokensBefore, enrichers = NO_BUILTIN_
1764
1880
  if (!cut.ok) return { cancel: true, reason: "fabric: nothing to compact" };
1765
1881
  const source = cumulativeSource(branchEntries, cut.firstKeptEntryId);
1766
1882
  if (source.events.length === 0) return { cancel: true, reason: "fabric: no raw cumulative source" };
1767
- const projected = projectWithMetadata(source.events);
1883
+ const retainedDialogue = cut.firstKeptEntryId ? normalizeEntries(branchEntries.slice(source.prefixEntries.length).filter((entry) => entry.type === "message" && (entry.message.role === "user" || entry.message.role === "assistant"))).filter((event) => event.kind === "user" || event.kind === "assistantText") : [];
1884
+ const projected = projectWithMetadata(source.events, retainedDialogue);
1768
1885
  const sections = projected.sections;
1769
1886
  runEnrichers(enrichers, source.events, sections);
1770
- const summary = renderSummary(sections, {
1887
+ const rendered = renderSummaryWithMetadata(sections, {
1771
1888
  firstEntryId: source.range.first,
1772
1889
  lastEntryId: source.range.last,
1773
1890
  lastTimestamp: source.timestamp,
1774
1891
  requestLines: instructions.requestLines
1775
1892
  });
1893
+ const { summary } = rendered;
1776
1894
  const projectedTokensAfter = cut.budget ? Math.ceil(
1777
1895
  cut.budget.fixedOverheadTokens + (cut.budget.retainedRawTokens + Math.ceil(summary.length / 4)) * cut.budget.tokenScale
1778
1896
  ) : void 0;
1779
1897
  const budgetDetails = cut.budget && projectedTokensAfter !== void 0 ? { ...cut.budget, projectedTokensAfter } : void 0;
1780
- if (budgetDetails && budgetDetails.projectedTokensAfter > budgetDetails.targetContextTokens) {
1898
+ if (!acceptSummaryBounds(
1899
+ utf8Bytes(summary),
1900
+ MAX_SUMMARY_BYTES,
1901
+ budgetDetails?.projectedTokensAfter ?? 0,
1902
+ budgetDetails?.targetContextTokens ?? 0
1903
+ )) {
1781
1904
  return {
1782
1905
  cancel: true,
1783
1906
  reason: "fabric: no deterministic summary fits the continuity context target"
1784
1907
  };
1785
1908
  }
1786
1909
  const versions = priorFabricVersions(branchEntries);
1787
- const sectionHeaders = SECTION_HEADERS2.filter(({ key }) => sections[key].length > 0).map(({ header }) => header);
1788
- if (instructions.requestLines.length > 0) sectionHeaders.splice(1, 0, "[Compaction Request]");
1910
+ const sectionHeaders = SUMMARY_SECTIONS.filter(({ key }) => sections[key].length > 0).map(({ header }) => header);
1911
+ if (instructions.requestLines.length > 0) {
1912
+ sectionHeaders.splice(sections.dialogue.length > 0 ? 1 : 0, 0, "[Compaction Request]");
1913
+ }
1789
1914
  const details = {
1790
1915
  compactor: "fabric",
1791
1916
  version: 2,
@@ -1807,13 +1932,18 @@ var compileFabricSummary = (branchEntries, tokensBefore, enrichers = NO_BUILTIN_
1807
1932
  },
1808
1933
  omittedCounts: {
1809
1934
  ...projected.omittedCounts,
1935
+ dialogueBytes: (projected.omittedCounts.dialogueBytes ?? 0) + rendered.dialogueOmittedBytes,
1810
1936
  preserve: instructions.policy.omittedPreserveCount,
1811
1937
  // Thinking parts never survive normalization into a rendered summary;
1812
1938
  // the structural count keeps that erasure auditable against the raw
1813
1939
  // prefix instead of letting deliberation vanish untracked.
1814
1940
  thinking: countErasedThinkingBlocks(source.prefixEntries)
1815
1941
  },
1816
- instructionPolicy: instructions.policy,
1942
+ instructionPolicy: {
1943
+ ...instructions.policy,
1944
+ truncated: instructions.policy.truncated || rendered.requestOmittedBytes > 0,
1945
+ renderedOmittedBytes: rendered.requestOmittedBytes
1946
+ },
1817
1947
  stableAddresses: {
1818
1948
  firstKeptEntryId: cut.firstKeptEntryId,
1819
1949
  cumulativeSourceRange: source.range,
@@ -1831,14 +1961,6 @@ var compileFabricSummary = (branchEntries, tokensBefore, enrichers = NO_BUILTIN_
1831
1961
  }
1832
1962
  };
1833
1963
  };
1834
- var SECTION_HEADERS2 = [
1835
- { key: "goal", header: "[Session Goal]" },
1836
- { key: "files", header: "[Files And Changes]" },
1837
- { key: "activity", header: "[Fabric Activity]" },
1838
- { key: "outstanding", header: "[Outstanding Context]" },
1839
- { key: "earlierTurns", header: "[Earlier Turns]" },
1840
- { key: "status", header: "[Current Status]" }
1841
- ];
1842
1964
  var notifyInstructionError = (context, error) => {
1843
1965
  if (!context?.hasUI) return;
1844
1966
  context.ui.notify(clipUtf8(`Fabric compaction rejected: ${error.code}: ${error.message}`, 512), "error");
@@ -1864,12 +1986,12 @@ var registerCompactionHook = (pi, options) => {
1864
1986
  const evidenceWindow = event.reason === "overflow" && typeof tokensBefore === "number" && Number.isFinite(tokensBefore) && tokensBefore > 0 ? Math.max(1, Math.floor(tokensBefore * OVERFLOW_WINDOW_EVIDENCE_RATIO)) : void 0;
1865
1987
  const advertisedWindow = typeof contextWindow === "number" && Number.isFinite(contextWindow) && contextWindow > 0 ? contextWindow : void 0;
1866
1988
  const effectiveWindow = evidenceWindow === void 0 ? advertisedWindow : advertisedWindow === void 0 ? evidenceWindow : Math.min(advertisedWindow, evidenceWindow);
1867
- const budget = effectiveWindow !== void 0 && typeof targetContextRatio === "number" && Number.isFinite(targetContextRatio) ? {
1868
- contextWindow: effectiveWindow,
1869
- targetContextRatio,
1989
+ const budget = {
1990
+ ...effectiveWindow !== void 0 ? { contextWindow: effectiveWindow } : {},
1991
+ ...typeof targetContextRatio === "number" && Number.isFinite(targetContextRatio) ? { targetContextRatio } : {},
1870
1992
  reserveTokens: settings.reserveTokens,
1871
1993
  keepRecentTokens: settings.keepRecentTokens
1872
- } : void 0;
1994
+ };
1873
1995
  const result = compileFabricSummary(
1874
1996
  branchEntries ?? [],
1875
1997
  preparation.tokensBefore,
@@ -1921,4 +2043,4 @@ export {
1921
2043
  compileFabricSummary,
1922
2044
  registerCompactionHook
1923
2045
  };
1924
- //# sourceMappingURL=chunk-MQG5QKGI.js.map
2046
+ //# sourceMappingURL=chunk-Q5BMTL6A.js.map