privateer-agent 0.15.4 → 0.15.6

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 (229) hide show
  1. package/README.md +11 -0
  2. package/bin/boot-bundle.d.mts +2 -0
  3. package/bin/boot-bundle.mjs +44 -0
  4. package/bin/fresh-supervisor.d.mts +5 -1
  5. package/bin/fresh-supervisor.mjs +47 -10
  6. package/bin/privateer-launch.mjs +27 -3
  7. package/bin/privateer-subagent.mjs +5 -2
  8. package/bin/privateer-verify.mjs +12 -0
  9. package/bin/session-recovery.d.mts +53 -0
  10. package/bin/session-recovery.mjs +240 -0
  11. package/bin/session-seal-preload.mjs +17 -0
  12. package/bin/session-seal.d.mts +25 -0
  13. package/bin/session-seal.mjs +242 -0
  14. package/dist-boot/boot.mjs +3 -0
  15. package/dist-boot/c-27JKSMWL.mjs +1460 -0
  16. package/dist-boot/c-2BMRFV24.mjs +269 -0
  17. package/dist-boot/c-2CTF6JA3.mjs +166 -0
  18. package/dist-boot/c-2G3BBHRJ.mjs +84 -0
  19. package/dist-boot/c-2IDLKXX6.mjs +250 -0
  20. package/dist-boot/c-2NJD5VYM.mjs +8 -0
  21. package/dist-boot/c-2S5EEQPW.mjs +1602 -0
  22. package/dist-boot/c-2WRDCUVI.mjs +25 -0
  23. package/dist-boot/c-2YN3M256.mjs +14 -0
  24. package/dist-boot/c-37GM3GMU.mjs +187 -0
  25. package/dist-boot/c-3STKRSVO.mjs +2480 -0
  26. package/dist-boot/c-3TPV6TQQ.mjs +2320 -0
  27. package/dist-boot/c-3UVHLQDJ.mjs +225 -0
  28. package/dist-boot/c-42PUUZYC.mjs +697 -0
  29. package/dist-boot/c-4D5UMWVW.mjs +683 -0
  30. package/dist-boot/c-4ETZRMTA.mjs +46 -0
  31. package/dist-boot/c-4QT25BD5.mjs +1482 -0
  32. package/dist-boot/c-4WVR32AP.mjs +2579 -0
  33. package/dist-boot/c-5EMM567G.mjs +152 -0
  34. package/dist-boot/c-5FA4CV63.mjs +145 -0
  35. package/dist-boot/c-5GV3X3RB.mjs +14 -0
  36. package/dist-boot/c-5JCEPRIY.mjs +52 -0
  37. package/dist-boot/c-5NDKIIWT.mjs +16 -0
  38. package/dist-boot/c-5QL3AR26.mjs +4665 -0
  39. package/dist-boot/c-5XY6MFKN.mjs +5142 -0
  40. package/dist-boot/c-65UZIZC7.mjs +2897 -0
  41. package/dist-boot/c-6FUXKHOK.mjs +1969 -0
  42. package/dist-boot/c-6JHWQE4V.mjs +18242 -0
  43. package/dist-boot/c-6LOBR4I6.mjs +5280 -0
  44. package/dist-boot/c-6QHOZXKQ.mjs +14394 -0
  45. package/dist-boot/c-6ZO2SADQ.mjs +178 -0
  46. package/dist-boot/c-6ZORVVZP.mjs +1449 -0
  47. package/dist-boot/c-76QH4UCP.mjs +2441 -0
  48. package/dist-boot/c-77GO5L5Y.mjs +20982 -0
  49. package/dist-boot/c-7M23XFTN.mjs +5052 -0
  50. package/dist-boot/c-7TC367E2.mjs +32 -0
  51. package/dist-boot/c-7UOG7POX.mjs +1524 -0
  52. package/dist-boot/c-7VHP3FLN.mjs +376 -0
  53. package/dist-boot/c-A73JHZP2.mjs +13207 -0
  54. package/dist-boot/c-ABXULAFL.mjs +71 -0
  55. package/dist-boot/c-ANDD2AQO.mjs +74 -0
  56. package/dist-boot/c-AOY77AZL.mjs +1430 -0
  57. package/dist-boot/c-AXEPZ22I.mjs +108 -0
  58. package/dist-boot/c-B2Y7VNHU.mjs +427 -0
  59. package/dist-boot/c-B7VJUH73.mjs +77 -0
  60. package/dist-boot/c-BFW6CDVL.mjs +24 -0
  61. package/dist-boot/c-BHNIF2AR.mjs +188 -0
  62. package/dist-boot/c-BKF2O6BL.mjs +23 -0
  63. package/dist-boot/c-BOZXFTZN.mjs +8 -0
  64. package/dist-boot/c-BRNYJWZL.mjs +28 -0
  65. package/dist-boot/c-C3N4Z2HO.mjs +93 -0
  66. package/dist-boot/c-C4IV7ZFG.mjs +717 -0
  67. package/dist-boot/c-C5NK6UYV.mjs +793 -0
  68. package/dist-boot/c-C67644MB.mjs +1659 -0
  69. package/dist-boot/c-C6S2YB53.mjs +620 -0
  70. package/dist-boot/c-CDBEJSBF.mjs +37 -0
  71. package/dist-boot/c-CG47HBKU.mjs +435 -0
  72. package/dist-boot/c-CKCTWCT5.mjs +51 -0
  73. package/dist-boot/c-CN77DCGF.mjs +60 -0
  74. package/dist-boot/c-CRBXNKFO.mjs +1795 -0
  75. package/dist-boot/c-CRIHASQ6.mjs +973 -0
  76. package/dist-boot/c-CU4NGAUO.mjs +3786 -0
  77. package/dist-boot/c-CWRFLGQU.mjs +1883 -0
  78. package/dist-boot/c-CYQB2USO.mjs +32 -0
  79. package/dist-boot/c-DEE67DJO.mjs +379 -0
  80. package/dist-boot/c-DIFDBYOP.mjs +1258 -0
  81. package/dist-boot/c-DOSGVXDZ.mjs +34879 -0
  82. package/dist-boot/c-DR7XQI32.mjs +381 -0
  83. package/dist-boot/c-DTGPSB46.mjs +14666 -0
  84. package/dist-boot/c-DTHDNA4P.mjs +320 -0
  85. package/dist-boot/c-E4Z3Y5B5.mjs +3864 -0
  86. package/dist-boot/c-E66Z2TI7.mjs +203 -0
  87. package/dist-boot/c-EBU6RLRV.mjs +1527 -0
  88. package/dist-boot/c-EBZZNGSF.mjs +432 -0
  89. package/dist-boot/c-EC3LJ2KF.mjs +1032 -0
  90. package/dist-boot/c-EQBKXWNX.mjs +15829 -0
  91. package/dist-boot/c-EUEPIW4S.mjs +574 -0
  92. package/dist-boot/c-EX4YVWAT.mjs +378 -0
  93. package/dist-boot/c-EYZGSAJU.mjs +98 -0
  94. package/dist-boot/c-F75VRKY4.mjs +172 -0
  95. package/dist-boot/c-FH4NXKHV.mjs +177 -0
  96. package/dist-boot/c-FHNUCTEM.mjs +140 -0
  97. package/dist-boot/c-FJ5T42DS.mjs +5349 -0
  98. package/dist-boot/c-FPCNRL7G.mjs +1728 -0
  99. package/dist-boot/c-G4DLN7Q4.mjs +8 -0
  100. package/dist-boot/c-G56GYQFZ.mjs +2507 -0
  101. package/dist-boot/c-GLIOERDQ.mjs +1200 -0
  102. package/dist-boot/c-GMJ5HUBM.mjs +14 -0
  103. package/dist-boot/c-H3HSGMAQ.mjs +461 -0
  104. package/dist-boot/c-HIKTO6P7.mjs +275 -0
  105. package/dist-boot/c-I2MCJ34O.mjs +464 -0
  106. package/dist-boot/c-I3C6LBW4.mjs +765 -0
  107. package/dist-boot/c-I54OKQHW.mjs +387 -0
  108. package/dist-boot/c-IFSP3R4D.mjs +80 -0
  109. package/dist-boot/c-IZK77VB7.mjs +171 -0
  110. package/dist-boot/c-J3Y25UGE.mjs +54 -0
  111. package/dist-boot/c-J5IESS7W.mjs +27750 -0
  112. package/dist-boot/c-JJWZBY7L.mjs +254 -0
  113. package/dist-boot/c-JKTFXTGF.mjs +463 -0
  114. package/dist-boot/c-JN57AXQ4.mjs +77 -0
  115. package/dist-boot/c-KFIO6LUW.mjs +1096 -0
  116. package/dist-boot/c-KIEMEFWW.mjs +478 -0
  117. package/dist-boot/c-KIVXWBYD.mjs +28 -0
  118. package/dist-boot/c-KP6CRIP4.mjs +580 -0
  119. package/dist-boot/c-L26VOJX4.mjs +11558 -0
  120. package/dist-boot/c-L5GHWG5G.mjs +5988 -0
  121. package/dist-boot/c-M7NSQAKK.mjs +353 -0
  122. package/dist-boot/c-MCEWOPBW.mjs +56 -0
  123. package/dist-boot/c-MGPIEG7I.mjs +20 -0
  124. package/dist-boot/c-NREXJV3M.mjs +393 -0
  125. package/dist-boot/c-O5PP6CTX.mjs +162 -0
  126. package/dist-boot/c-OBQDHSXM.mjs +1774 -0
  127. package/dist-boot/c-OLQHHDD4.mjs +21 -0
  128. package/dist-boot/c-OXW4JQQH.mjs +319 -0
  129. package/dist-boot/c-P3D7ZUWN.mjs +1288 -0
  130. package/dist-boot/c-P4PQZVV5.mjs +948 -0
  131. package/dist-boot/c-P63T3XL2.mjs +26373 -0
  132. package/dist-boot/c-P6N5DQGZ.mjs +194 -0
  133. package/dist-boot/c-P7MLKQFC.mjs +58 -0
  134. package/dist-boot/c-PB3PZJL5.mjs +1276 -0
  135. package/dist-boot/c-PTN4S6G2.mjs +1259 -0
  136. package/dist-boot/c-PYCE5J7U.mjs +1083 -0
  137. package/dist-boot/c-Q4HYCRLB.mjs +19410 -0
  138. package/dist-boot/c-QUXMETGG.mjs +68 -0
  139. package/dist-boot/c-RDMJZCOX.mjs +356 -0
  140. package/dist-boot/c-ROGJK76M.mjs +17 -0
  141. package/dist-boot/c-RQJJ26T4.mjs +708 -0
  142. package/dist-boot/c-RSKCUPJF.mjs +7559 -0
  143. package/dist-boot/c-RVJU4O6P.mjs +37 -0
  144. package/dist-boot/c-S2RVHR3M.mjs +165 -0
  145. package/dist-boot/c-SKMK3PEE.mjs +91120 -0
  146. package/dist-boot/c-TI7SM4EG.mjs +20 -0
  147. package/dist-boot/c-TILJ6GXD.mjs +1235 -0
  148. package/dist-boot/c-TT6BCUJD.mjs +114 -0
  149. package/dist-boot/c-U4ITOXAN.mjs +2107 -0
  150. package/dist-boot/c-UGERXVO2.mjs +7120 -0
  151. package/dist-boot/c-UPBH5AXY.mjs +1363 -0
  152. package/dist-boot/c-US2PUNZL.mjs +597 -0
  153. package/dist-boot/c-UU4WZPPE.mjs +20 -0
  154. package/dist-boot/c-UWVIYPQS.mjs +39 -0
  155. package/dist-boot/c-V4RHQBPQ.mjs +17683 -0
  156. package/dist-boot/c-V5MRVK47.mjs +351 -0
  157. package/dist-boot/c-V6CL266R.mjs +1755 -0
  158. package/dist-boot/c-VJTMFKYE.mjs +14975 -0
  159. package/dist-boot/c-VJTRUKIB.mjs +773 -0
  160. package/dist-boot/c-W4IFSHPW.mjs +43 -0
  161. package/dist-boot/c-WETGB2CF.mjs +113 -0
  162. package/dist-boot/c-WO3QSZUU.mjs +1337 -0
  163. package/dist-boot/c-WQLH73TY.mjs +242 -0
  164. package/dist-boot/c-XL735JW5.mjs +500 -0
  165. package/dist-boot/c-Y7HADL5B.mjs +1107 -0
  166. package/dist-boot/c-YGQHSVSK.mjs +41 -0
  167. package/dist-boot/c-YMP2AMEB.mjs +10350 -0
  168. package/dist-boot/c-YRDO2WEF.mjs +12012 -0
  169. package/dist-boot/c-YUSTRATJ.mjs +8 -0
  170. package/dist-boot/c-Z2262R22.mjs +2697 -0
  171. package/dist-boot/c-Z4ALQMJC.mjs +709 -0
  172. package/dist-boot/c-Z5ZUQVUY.mjs +62 -0
  173. package/dist-boot/c-ZDZUKXF7.mjs +874 -0
  174. package/dist-boot/c-ZJYLGIC3.mjs +32 -0
  175. package/dist-boot/c-ZNA5CDMI.mjs +77 -0
  176. package/dist-boot/c-ZSO6DI3M.mjs +204 -0
  177. package/dist-boot/c-ZWUVHKMK.mjs +100 -0
  178. package/dist-boot/c-ZY4MESEW.mjs +832 -0
  179. package/dist-boot/c-ZZ5REI4L.mjs +329 -0
  180. package/dist-boot/cli.mjs +51 -0
  181. package/dist-boot/ext-anthropic-attribution.mjs +207 -0
  182. package/dist-boot/ext-pi-background-tasks.mjs +7234 -0
  183. package/dist-boot/ext-pi-subagents.mjs +37 -0
  184. package/dist-boot/ext-privateer-account.mjs +4426 -0
  185. package/dist-boot/ext-privateer-brand.mjs +5274 -0
  186. package/dist-boot/ext-privateer-codemode.mjs +37 -0
  187. package/dist-boot/ext-privateer-computer.mjs +626 -0
  188. package/dist-boot/ext-privateer-connect.mjs +1862 -0
  189. package/dist-boot/ext-privateer-context.mjs +323 -0
  190. package/dist-boot/ext-privateer-desktop.mjs +92 -0
  191. package/dist-boot/ext-privateer-final-audit.mjs +48 -0
  192. package/dist-boot/ext-privateer-fresh.mjs +81 -0
  193. package/dist-boot/ext-privateer-gate.mjs +5959 -0
  194. package/dist-boot/ext-privateer-hints.mjs +236 -0
  195. package/dist-boot/ext-privateer-media.mjs +2626 -0
  196. package/dist-boot/ext-privateer-models.mjs +3397 -0
  197. package/dist-boot/ext-privateer-posture.mjs +3006 -0
  198. package/dist-boot/ext-privateer-privacy.mjs +4578 -0
  199. package/dist-boot/ext-privateer-recovery.mjs +287 -0
  200. package/dist-boot/ext-privateer-speak.mjs +4754 -0
  201. package/dist-boot/ext-privateer-terminals.mjs +725 -0
  202. package/dist-boot/ext-privateer-tools.mjs +528 -0
  203. package/dist-boot/ext-privateer-truncation.mjs +74 -0
  204. package/dist-boot/ext-privateer-update.mjs +210 -0
  205. package/dist-boot/ext-privateer-vision.mjs +148 -0
  206. package/dist-boot/ext-privateer-web.mjs +543 -0
  207. package/dist-boot/ext-rpiv-ask-user-question.mjs +710 -0
  208. package/dist-boot/manifest.json +9 -0
  209. package/extensions/privateer-brand.ts +3 -1
  210. package/extensions/privateer-context.ts +4 -2
  211. package/extensions/privateer-final-audit.ts +17 -0
  212. package/extensions/privateer-gate.ts +55 -9
  213. package/extensions/privateer-recovery.ts +175 -0
  214. package/extensions/privateer-web.ts +4 -4
  215. package/package.json +3 -1
  216. package/patches/@earendil-works+pi-coding-agent+1.3.0.patch +199 -2
  217. package/patches/pi-background-tasks+2.6.10.patch +91 -7
  218. package/src/cli/chat.ts +7 -1
  219. package/src/config/moatManifest.json +2 -0
  220. package/src/config/whatsNew.json +4 -4
  221. package/src/context.ts +33 -5
  222. package/src/engine/finalAudit.ts +38 -0
  223. package/src/engine/stallWatchdog.ts +8 -1
  224. package/src/providers/account.ts +34 -3
  225. package/src/remote/attentionPush.ts +40 -0
  226. package/src/remote/liveTaskSession.ts +7 -1
  227. package/src/remote/relayClient.ts +38 -4
  228. package/src/remote/remoteBridge.ts +17 -0
  229. package/src/remote/transcriptSnapshot.ts +104 -0
@@ -0,0 +1,464 @@
1
+ import{createRequire as __pvCR}from"node:module";import{fileURLToPath as __pvF}from"node:url";import{dirname as __pvD}from"node:path";const require=__pvCR(import.meta.url);const __pvUrl=(r)=>new URL(r,import.meta.url).href;const __pvFile=(r)=>__pvF(new URL(r,import.meta.url));
2
+ import {
3
+ canonicalJson
4
+ } from "./c-KIVXWBYD.mjs";
5
+ import {
6
+ isJsonObject
7
+ } from "./c-I3C6LBW4.mjs";
8
+ import {
9
+ buildSessionContext,
10
+ convertToLlm
11
+ } from "./c-SKMK3PEE.mjs";
12
+ import {
13
+ __name
14
+ } from "./c-CN77DCGF.mjs";
15
+
16
+ // node_modules/pi-background-tasks/src/core/context/visible-conversation-v2.ts?ext=pi-background-tasks
17
+ import { createHash } from "node:crypto";
18
+ var CONVERSATION_IMAGE_OMISSION_PREFIX = "[Image omitted from fusion text transcript: ";
19
+ var UnsupportedConversationBlockError = class extends Error {
20
+ static {
21
+ __name(this, "UnsupportedConversationBlockError");
22
+ }
23
+ label;
24
+ constructor(label) {
25
+ super(`fusion context projection encountered an unsupported conversation block: ${label}`);
26
+ this.name = "UnsupportedConversationBlockError";
27
+ this.label = label;
28
+ }
29
+ };
30
+ function utf8Bytes(value) {
31
+ return Buffer.byteLength(value, "utf8");
32
+ }
33
+ __name(utf8Bytes, "utf8Bytes");
34
+ function sha256Hex(bytes) {
35
+ return createHash("sha256").update(bytes).digest("hex");
36
+ }
37
+ __name(sha256Hex, "sha256Hex");
38
+ function uint64be(value) {
39
+ const out = Buffer.alloc(8);
40
+ out.writeBigUInt64BE(BigInt(value));
41
+ return out;
42
+ }
43
+ __name(uint64be, "uint64be");
44
+ function lengthPrefixed(bytes) {
45
+ return Buffer.concat([uint64be(bytes.length), bytes]);
46
+ }
47
+ __name(lengthPrefixed, "lengthPrefixed");
48
+ function ledgerLeafHash(index, record) {
49
+ return createHash("sha256").update(Buffer.from("pi-fusion-ledger-leaf-v1\0", "utf8")).update(uint64be(index)).update(lengthPrefixed(Buffer.from(canonicalJson(record), "utf8"))).digest();
50
+ }
51
+ __name(ledgerLeafHash, "ledgerLeafHash");
52
+ function ledgerRootHash(leaves) {
53
+ const hash = createHash("sha256").update(Buffer.from("pi-fusion-ledger-root-v1\0", "utf8")).update(uint64be(leaves.length));
54
+ for (const leaf of leaves) hash.update(leaf);
55
+ return hash.digest("hex");
56
+ }
57
+ __name(ledgerRootHash, "ledgerRootHash");
58
+ function unsupportedBlock(label) {
59
+ return new UnsupportedConversationBlockError(label);
60
+ }
61
+ __name(unsupportedBlock, "unsupportedBlock");
62
+ function compactCounts(counts) {
63
+ const out = {};
64
+ if (counts.assistant_thinking > 0) out.assistant_thinking = counts.assistant_thinking;
65
+ if (counts.tool_calls > 0) out.tool_calls = counts.tool_calls;
66
+ if (counts.tool_result_texts > 0) out.tool_result_texts = counts.tool_result_texts;
67
+ return out;
68
+ }
69
+ __name(compactCounts, "compactCounts");
70
+ function imageMarker(mimeType) {
71
+ return `${CONVERSATION_IMAGE_OMISSION_PREFIX}${mimeType}]`;
72
+ }
73
+ __name(imageMarker, "imageMarker");
74
+ var ProjectionBuilder = class {
75
+ static {
76
+ __name(this, "ProjectionBuilder");
77
+ }
78
+ entries = [];
79
+ ledger = [];
80
+ leaves = [];
81
+ projectionMap = [];
82
+ toolCallNames = /* @__PURE__ */ new Map();
83
+ pendingCounts;
84
+ pendingBytes = 0;
85
+ pendingLedgerFirst = 0;
86
+ pendingLedgerLast = 0;
87
+ pendingSourceFirst = 0;
88
+ pendingSourceLast = 0;
89
+ includedUserTextBytes = 0;
90
+ includedAssistantTextBytes = 0;
91
+ includedImageMarkers = 0;
92
+ emptyTextBlocks = 0;
93
+ addText(entry) {
94
+ this.flush();
95
+ this.entries.push(entry);
96
+ if (entry.role === "user") this.includedUserTextBytes += utf8Bytes(entry.text);
97
+ else this.includedAssistantTextBytes += utf8Bytes(entry.text);
98
+ }
99
+ addOmission(sourceOrdinal, blockOrdinal, kind, payload, extra = {}) {
100
+ const index = this.ledger.length;
101
+ const record = {
102
+ index,
103
+ source_ordinal: sourceOrdinal,
104
+ block_ordinal: blockOrdinal,
105
+ kind,
106
+ payload_bytes: payload.length,
107
+ payload_sha256: sha256Hex(payload)
108
+ };
109
+ if (extra.toolName !== void 0) record.tool_name = extra.toolName;
110
+ if (extra.toolCallId !== void 0) record.tool_call_id = extra.toolCallId;
111
+ if (extra.mimeType !== void 0) record.mime_type = extra.mimeType;
112
+ this.ledger.push(record);
113
+ this.leaves.push(ledgerLeafHash(index, record));
114
+ if (extra.toolName !== void 0 && kind === "tool_call") {
115
+ this.toolCallNames.set(extra.toolName, (this.toolCallNames.get(extra.toolName) ?? 0) + 1);
116
+ }
117
+ if (kind === "tool_result_image") {
118
+ this.flush();
119
+ this.addLedgerOnlyImageMap(index);
120
+ return;
121
+ }
122
+ if (this.pendingCounts === void 0) {
123
+ this.pendingCounts = {
124
+ assistant_thinking: 0,
125
+ tool_calls: 0,
126
+ tool_result_texts: 0
127
+ };
128
+ this.pendingBytes = 0;
129
+ this.pendingLedgerFirst = index;
130
+ this.pendingSourceFirst = sourceOrdinal;
131
+ }
132
+ this.pendingLedgerLast = index;
133
+ this.pendingSourceLast = sourceOrdinal;
134
+ this.pendingBytes += payload.length;
135
+ if (kind === "assistant_thinking") this.pendingCounts.assistant_thinking += 1;
136
+ else if (kind === "tool_call") this.pendingCounts.tool_calls += 1;
137
+ else this.pendingCounts.tool_result_texts += 1;
138
+ }
139
+ addLedgerOnlyImageMap(index) {
140
+ const previous = this.projectionMap[this.projectionMap.length - 1];
141
+ if (previous !== void 0 && previous.entry_kind === "ledger_only_tool_result_image" && previous.ledger_index_last + 1 === index) {
142
+ previous.ledger_index_last = index;
143
+ return;
144
+ }
145
+ this.projectionMap.push({
146
+ entry_kind: "ledger_only_tool_result_image",
147
+ ledger_index_first: index,
148
+ ledger_index_last: index
149
+ });
150
+ }
151
+ flush() {
152
+ const counts = this.pendingCounts;
153
+ if (counts === void 0) return;
154
+ const canonicalEntryIndex = this.entries.length;
155
+ const entry = {
156
+ at: [this.pendingSourceFirst, this.pendingSourceLast],
157
+ bytes: this.pendingBytes,
158
+ counts: compactCounts(counts),
159
+ kind: "omitted_activity"
160
+ };
161
+ this.entries.push(entry);
162
+ this.projectionMap.push({
163
+ canonical_entry_index: canonicalEntryIndex,
164
+ entry_kind: "omitted_activity",
165
+ ledger_index_first: this.pendingLedgerFirst,
166
+ ledger_index_last: this.pendingLedgerLast
167
+ });
168
+ this.pendingCounts = void 0;
169
+ this.pendingBytes = 0;
170
+ }
171
+ finish(messageCount) {
172
+ this.flush();
173
+ const rootSha256 = ledgerRootHash(this.leaves);
174
+ let omittedRunCount = 0;
175
+ let includedTextEntries = 0;
176
+ let receiptBytes = 0;
177
+ for (const entry of this.entries) {
178
+ if (entry.kind === "text") includedTextEntries += 1;
179
+ else {
180
+ omittedRunCount += 1;
181
+ receiptBytes += utf8Bytes(canonicalJson(entry));
182
+ }
183
+ }
184
+ let thinkingBytes = 0;
185
+ let toolCallCount = 0;
186
+ let toolArgumentBytes = 0;
187
+ let toolResultTextCount = 0;
188
+ let toolResultTextBytes = 0;
189
+ let toolResultImageCount = 0;
190
+ let toolResultImageBytes = 0;
191
+ for (const row of this.ledger) {
192
+ if (row.kind === "assistant_thinking") thinkingBytes += row.payload_bytes;
193
+ else if (row.kind === "tool_call") {
194
+ toolCallCount += 1;
195
+ toolArgumentBytes += row.payload_bytes;
196
+ } else if (row.kind === "tool_result_text") {
197
+ toolResultTextCount += 1;
198
+ toolResultTextBytes += row.payload_bytes;
199
+ } else {
200
+ toolResultImageCount += 1;
201
+ toolResultImageBytes += row.payload_bytes;
202
+ }
203
+ }
204
+ const toolCallNames = [...this.toolCallNames.entries()].map(([name, calls]) => ({ name, calls })).sort((left, right) => left.name < right.name ? -1 : left.name > right.name ? 1 : 0);
205
+ const accounting = {
206
+ message_count: messageCount,
207
+ included_text_entry_count: includedTextEntries,
208
+ included_user_text_bytes: this.includedUserTextBytes,
209
+ included_assistant_text_bytes: this.includedAssistantTextBytes,
210
+ included_image_marker_count: this.includedImageMarkers,
211
+ empty_text_block_count: this.emptyTextBlocks,
212
+ omitted_run_count: omittedRunCount,
213
+ omitted_event_count: this.ledger.length,
214
+ omitted_thinking_bytes: thinkingBytes,
215
+ omitted_tool_call_count: toolCallCount,
216
+ omitted_tool_call_argument_bytes: toolArgumentBytes,
217
+ omitted_tool_result_text_count: toolResultTextCount,
218
+ omitted_tool_result_text_bytes: toolResultTextBytes,
219
+ omitted_tool_result_image_count: toolResultImageCount,
220
+ omitted_tool_result_image_bytes: toolResultImageBytes,
221
+ tool_call_names: toolCallNames,
222
+ ledger_entry_count: this.ledger.length,
223
+ ledger_root_sha256: rootSha256,
224
+ omission_receipt_utf8_bytes: receiptBytes
225
+ };
226
+ return {
227
+ entries: this.entries,
228
+ accounting,
229
+ ledger: {
230
+ entries: this.ledger,
231
+ projection_map: this.projectionMap,
232
+ root_sha256: rootSha256
233
+ }
234
+ };
235
+ }
236
+ };
237
+ function projectUserMessage(builder, content, sourceOrdinal) {
238
+ if (typeof content === "string") {
239
+ if (content.length === 0) {
240
+ builder.emptyTextBlocks += 1;
241
+ return;
242
+ }
243
+ builder.addText({
244
+ kind: "text",
245
+ source_ordinal: sourceOrdinal,
246
+ block_ordinal: 0,
247
+ role: "user",
248
+ text: content
249
+ });
250
+ return;
251
+ }
252
+ for (const [blockOrdinal, block] of content.entries()) {
253
+ if (block.type === "text") {
254
+ if (block.text.length === 0) {
255
+ builder.emptyTextBlocks += 1;
256
+ continue;
257
+ }
258
+ builder.addText({
259
+ kind: "text",
260
+ source_ordinal: sourceOrdinal,
261
+ block_ordinal: blockOrdinal,
262
+ role: "user",
263
+ text: block.text
264
+ });
265
+ continue;
266
+ }
267
+ if (block.type === "image") {
268
+ builder.includedImageMarkers += 1;
269
+ builder.addText({
270
+ kind: "text",
271
+ source_ordinal: sourceOrdinal,
272
+ block_ordinal: blockOrdinal,
273
+ role: "user",
274
+ text: imageMarker(block.mimeType)
275
+ });
276
+ continue;
277
+ }
278
+ throw unsupportedBlock(`user block ${String(Reflect.get(block, "type"))}`);
279
+ }
280
+ }
281
+ __name(projectUserMessage, "projectUserMessage");
282
+ function projectAssistantMessage(builder, content, sourceOrdinal) {
283
+ for (const [blockOrdinal, block] of content.entries()) {
284
+ if (block.type === "text") {
285
+ if (block.text.length === 0) {
286
+ builder.emptyTextBlocks += 1;
287
+ continue;
288
+ }
289
+ builder.addText({
290
+ kind: "text",
291
+ source_ordinal: sourceOrdinal,
292
+ block_ordinal: blockOrdinal,
293
+ role: "assistant",
294
+ text: block.text
295
+ });
296
+ continue;
297
+ }
298
+ if (block.type === "thinking") {
299
+ builder.addOmission(
300
+ sourceOrdinal,
301
+ blockOrdinal,
302
+ "assistant_thinking",
303
+ Buffer.from(block.thinking, "utf8")
304
+ );
305
+ continue;
306
+ }
307
+ if (block.type === "toolCall") {
308
+ builder.addOmission(
309
+ sourceOrdinal,
310
+ blockOrdinal,
311
+ "tool_call",
312
+ Buffer.from(canonicalJson(block.arguments), "utf8"),
313
+ { toolName: block.name, toolCallId: block.id }
314
+ );
315
+ continue;
316
+ }
317
+ throw unsupportedBlock(`assistant block ${String(Reflect.get(block, "type"))}`);
318
+ }
319
+ }
320
+ __name(projectAssistantMessage, "projectAssistantMessage");
321
+ function projectToolResultMessage(builder, message, sourceOrdinal) {
322
+ const content = message.content;
323
+ if (typeof content === "string") {
324
+ builder.addOmission(sourceOrdinal, 0, "tool_result_text", Buffer.from(content, "utf8"), {
325
+ toolName: message.toolName,
326
+ toolCallId: message.toolCallId
327
+ });
328
+ return;
329
+ }
330
+ for (const [blockOrdinal, block] of content.entries()) {
331
+ if (block.type === "text") {
332
+ builder.addOmission(
333
+ sourceOrdinal,
334
+ blockOrdinal,
335
+ "tool_result_text",
336
+ Buffer.from(block.text, "utf8"),
337
+ { toolName: message.toolName, toolCallId: message.toolCallId }
338
+ );
339
+ continue;
340
+ }
341
+ if (block.type === "image") {
342
+ builder.addOmission(
343
+ sourceOrdinal,
344
+ blockOrdinal,
345
+ "tool_result_image",
346
+ Buffer.from(block.data, "utf8"),
347
+ {
348
+ toolName: message.toolName,
349
+ toolCallId: message.toolCallId,
350
+ mimeType: block.mimeType
351
+ }
352
+ );
353
+ continue;
354
+ }
355
+ throw unsupportedBlock(`tool result block ${String(Reflect.get(block, "type"))}`);
356
+ }
357
+ }
358
+ __name(projectToolResultMessage, "projectToolResultMessage");
359
+ function projectVisibleConversationV2(messages) {
360
+ const builder = new ProjectionBuilder();
361
+ for (const [sourceOrdinal, message] of messages.entries()) {
362
+ if (message.role === "user") projectUserMessage(builder, message.content, sourceOrdinal);
363
+ else if (message.role === "assistant")
364
+ projectAssistantMessage(builder, message.content, sourceOrdinal);
365
+ else if (message.role === "toolResult")
366
+ projectToolResultMessage(builder, message, sourceOrdinal);
367
+ else throw unsupportedBlock(`message role ${String(Reflect.get(message, "role"))}`);
368
+ }
369
+ return builder.finish(messages.length);
370
+ }
371
+ __name(projectVisibleConversationV2, "projectVisibleConversationV2");
372
+
373
+ // node_modules/pi-background-tasks/src/core/context/parent-snapshot.ts?ext=pi-background-tasks
374
+ function isConversationMessage(message) {
375
+ switch (message.role) {
376
+ case "system":
377
+ return false;
378
+ case "user":
379
+ case "assistant":
380
+ case "toolResult":
381
+ case "custom":
382
+ case "bashExecution":
383
+ case "branchSummary":
384
+ case "compactionSummary":
385
+ return true;
386
+ default:
387
+ throw new UnsupportedConversationBlockError(`message role ${message.role}`);
388
+ }
389
+ }
390
+ __name(isConversationMessage, "isConversationMessage");
391
+ function entriesById(entries) {
392
+ const byId = /* @__PURE__ */ new Map();
393
+ for (const entry of entries) byId.set(entry.id, entry);
394
+ return byId;
395
+ }
396
+ __name(entriesById, "entriesById");
397
+ function readArray(record, key) {
398
+ const value = record[key];
399
+ return Array.isArray(value) ? value : void 0;
400
+ }
401
+ __name(readArray, "readArray");
402
+ function recordOf(value) {
403
+ if (!isJsonObject(value) || Array.isArray(value)) return void 0;
404
+ return value;
405
+ }
406
+ __name(recordOf, "recordOf");
407
+ function entryMessage(entry) {
408
+ if (entry.type !== "message") return void 0;
409
+ return recordOf(entry.message);
410
+ }
411
+ __name(entryMessage, "entryMessage");
412
+ function toolCallPartMatches(part, toolCallId, toolName) {
413
+ const record = recordOf(part);
414
+ if (record === void 0 || record["type"] !== "toolCall") return false;
415
+ if (toolCallId !== void 0) return record["id"] === toolCallId;
416
+ return record["name"] === toolName;
417
+ }
418
+ __name(toolCallPartMatches, "toolCallPartMatches");
419
+ function messageContainsToolCall(message, toolCallId, toolName) {
420
+ if (message["role"] !== "assistant") return false;
421
+ const content = readArray(message, "content");
422
+ if (content === void 0) return false;
423
+ for (const part of content) {
424
+ if (toolCallPartMatches(part, toolCallId, toolName)) return true;
425
+ }
426
+ return false;
427
+ }
428
+ __name(messageContainsToolCall, "messageContainsToolCall");
429
+ function effectiveLeafForTool(sessionManager, toolCallId, toolName) {
430
+ const leaf = sessionManager.getLeafEntry();
431
+ if (leaf === void 0)
432
+ return { leafId: sessionManager.getLeafId(), activeToolCallLeafExcluded: false };
433
+ const message = entryMessage(leaf);
434
+ if (message !== void 0 && messageContainsToolCall(message, toolCallId, toolName)) {
435
+ return { leafId: leaf.parentId, activeToolCallLeafExcluded: true };
436
+ }
437
+ return { leafId: sessionManager.getLeafId(), activeToolCallLeafExcluded: false };
438
+ }
439
+ __name(effectiveLeafForTool, "effectiveLeafForTool");
440
+ function resolveEffectiveLeaf(sessionManager, options) {
441
+ if (!options.excludeActiveToolCallLeaf)
442
+ return { leafId: sessionManager.getLeafId(), activeToolCallLeafExcluded: false };
443
+ return effectiveLeafForTool(sessionManager, options.toolCallId, options.toolName);
444
+ }
445
+ __name(resolveEffectiveLeaf, "resolveEffectiveLeaf");
446
+ function snapshotParentConversation(ctx, options) {
447
+ const entries = ctx.sessionManager.getEntries();
448
+ const leaf = resolveEffectiveLeaf(ctx.sessionManager, options);
449
+ const sessionContext = buildSessionContext(entries, leaf.leafId, entriesById(entries));
450
+ const conversation = sessionContext.messages.filter(isConversationMessage);
451
+ return {
452
+ systemPrompt: ctx.getSystemPrompt(),
453
+ messages: convertToLlm(conversation),
454
+ leafId: leaf.leafId,
455
+ activeToolCallLeafExcluded: leaf.activeToolCallLeafExcluded
456
+ };
457
+ }
458
+ __name(snapshotParentConversation, "snapshotParentConversation");
459
+
460
+ export {
461
+ UnsupportedConversationBlockError,
462
+ projectVisibleConversationV2,
463
+ snapshotParentConversation
464
+ };