@evandu/super-pi-server 0.0.0-stage → 0.1.259

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 (370) hide show
  1. package/README.md +248 -3
  2. package/agents/code-auditor.md +64 -0
  3. package/agents/code-designer.md +64 -0
  4. package/agents/coder.md +24 -0
  5. package/agents/critic.md +24 -0
  6. package/agents/document-organizer.md +48 -0
  7. package/agents/patcher.md +27 -0
  8. package/agents/planner.md +30 -0
  9. package/agents/project-leader.md +28 -0
  10. package/agents/reviewer.md +27 -0
  11. package/agents/scout.md +27 -0
  12. package/agents/solution-designer.md +62 -0
  13. package/agents/super-pi-documents/index.ts +92 -0
  14. package/agents/super-pi-feishu/index.ts +88 -0
  15. package/agents/super-pi-knowledge/index.ts +70 -0
  16. package/agents/super-pi-managed-model/index.ts +9 -0
  17. package/agents/super-pi-memory/index.ts +75 -0
  18. package/agents/super-pi-native-tools/index.ts +1 -0
  19. package/agents/super-pi-task/core.ts +2262 -0
  20. package/agents/super-pi-task/index.ts +814 -0
  21. package/agents/super-pi-task/workflow-schema.ts +34 -0
  22. package/agents/super-pi-web/index.ts +32 -0
  23. package/agents/verifier.md +25 -0
  24. package/assets/fonts/LICENSE +92 -0
  25. package/assets/fonts/NotoSansSC-Bold.otf +0 -0
  26. package/assets/fonts/NotoSansSC-Regular.otf +0 -0
  27. package/assets/knowledge-model/README.md +37 -0
  28. package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/config.json +31 -0
  29. package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/onnx/model_quantized.onnx +0 -0
  30. package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/special_tokens_map.json +7 -0
  31. package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/tokenizer.json +21278 -0
  32. package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/tokenizer_config.json +15 -0
  33. package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/vocab.txt +21128 -0
  34. package/bin/npm-prefix.d.ts +1 -0
  35. package/bin/npm-prefix.js +10 -0
  36. package/bin/super-pi.js +349 -0
  37. package/dist/client/.super-pi-client-sources.json +82 -0
  38. package/dist/client/assets/CodeMirrorEditor-mi9rGUce.js +2 -0
  39. package/dist/client/assets/GitDiffEditor-CpoDDQrA.css +1 -0
  40. package/dist/client/assets/GitDiffEditor-FQBfGxOY.js +29 -0
  41. package/dist/client/assets/diff-ChtP43wD.js +1 -0
  42. package/dist/client/assets/dist-AW5lyI3I.js +1 -0
  43. package/dist/client/assets/dist-B3-a6uF0.js +7 -0
  44. package/dist/client/assets/dist-BGxkyh6e.js +1 -0
  45. package/dist/client/assets/dist-BJxfd7yb.js +11 -0
  46. package/dist/client/assets/dist-BRVQKlcp.js +1 -0
  47. package/dist/client/assets/dist-BV64A9Ji.js +1 -0
  48. package/dist/client/assets/dist-BXAOrsjq.js +1 -0
  49. package/dist/client/assets/dist-CKF2Jksy.js +1 -0
  50. package/dist/client/assets/dist-CX_Hm4GS.js +1 -0
  51. package/dist/client/assets/dist-Cus6_oaF.js +2 -0
  52. package/dist/client/assets/dist-Cx00EZKR.js +23 -0
  53. package/dist/client/assets/dist-D71y2EI1.js +1 -0
  54. package/dist/client/assets/dist-D978o95T.js +5 -0
  55. package/dist/client/assets/dist-Dc1MBGAG.js +1 -0
  56. package/dist/client/assets/dist-Dds-qLxY.js +9 -0
  57. package/dist/client/assets/dist-DeD9copn.js +1 -0
  58. package/dist/client/assets/dist-DuvGReu-.js +6 -0
  59. package/dist/client/assets/dist-Dvb0SqcK.js +2 -0
  60. package/dist/client/assets/dist-J6XWqRpB.js +3 -0
  61. package/dist/client/assets/dist-QzbOCAU6.js +1 -0
  62. package/dist/client/assets/dist-moiMRpw_.js +1 -0
  63. package/dist/client/assets/dist-scBkrwv-.js +2 -0
  64. package/dist/client/assets/dockerfile-BDePf5PZ.js +1 -0
  65. package/dist/client/assets/index-Cn7hdrDw.js +350 -0
  66. package/dist/client/assets/index-UfzqdECL.css +1 -0
  67. package/dist/client/assets/powershell-D8WpQPaX.js +1 -0
  68. package/dist/client/assets/properties-C357ku5U.js +1 -0
  69. package/dist/client/assets/rolldown-runtime-XKyaHtl9.js +1 -0
  70. package/dist/client/assets/ruby-ChfNwlCY.js +1 -0
  71. package/dist/client/assets/shell-DwuoZtxw.js +1 -0
  72. package/dist/client/assets/swift-DKB6_1j6.js +1 -0
  73. package/dist/client/assets/toml-BPTmHmyx.js +1 -0
  74. package/dist/client/favicon.ico +0 -0
  75. package/dist/client/favicon.png +0 -0
  76. package/dist/client/favicon.svg +10 -0
  77. package/dist/client/index.html +18 -0
  78. package/dist/extension.d.ts +15 -0
  79. package/dist/extension.js +126 -0
  80. package/dist/server/active-memory.d.ts +247 -0
  81. package/dist/server/active-memory.js +669 -0
  82. package/dist/server/agents.d.ts +245 -0
  83. package/dist/server/agents.js +701 -0
  84. package/dist/server/application/catalog-service.d.ts +98 -0
  85. package/dist/server/application/catalog-service.js +245 -0
  86. package/dist/server/application/conversation-service.d.ts +245 -0
  87. package/dist/server/application/conversation-service.js +654 -0
  88. package/dist/server/application/document-service.d.ts +68 -0
  89. package/dist/server/application/document-service.js +120 -0
  90. package/dist/server/application/extension-ui-service.d.ts +44 -0
  91. package/dist/server/application/extension-ui-service.js +135 -0
  92. package/dist/server/application/feishu-service.d.ts +179 -0
  93. package/dist/server/application/feishu-service.js +358 -0
  94. package/dist/server/application/group-chat-service.d.ts +75 -0
  95. package/dist/server/application/group-chat-service.js +953 -0
  96. package/dist/server/application/group-execution.d.ts +15 -0
  97. package/dist/server/application/group-execution.js +77 -0
  98. package/dist/server/application/project-service.d.ts +464 -0
  99. package/dist/server/application/project-service.js +2523 -0
  100. package/dist/server/application/provider-service.d.ts +174 -0
  101. package/dist/server/application/provider-service.js +510 -0
  102. package/dist/server/application/run-service.d.ts +547 -0
  103. package/dist/server/application/run-service.js +2683 -0
  104. package/dist/server/application/scheduled-task-service.d.ts +184 -0
  105. package/dist/server/application/scheduled-task-service.js +391 -0
  106. package/dist/server/application/session-orchestrator-service.d.ts +388 -0
  107. package/dist/server/application/session-orchestrator-service.js +1146 -0
  108. package/dist/server/application/session-reminder-service.d.ts +59 -0
  109. package/dist/server/application/session-reminder-service.js +141 -0
  110. package/dist/server/application/session-watchdog.d.ts +171 -0
  111. package/dist/server/application/session-watchdog.js +284 -0
  112. package/dist/server/application/workspace-service.d.ts +52 -0
  113. package/dist/server/application/workspace-service.js +186 -0
  114. package/dist/server/application/worktree-service.d.ts +13 -0
  115. package/dist/server/application/worktree-service.js +76 -0
  116. package/dist/server/browser-access.d.ts +34 -0
  117. package/dist/server/browser-access.js +89 -0
  118. package/dist/server/capability.d.ts +105 -0
  119. package/dist/server/capability.js +170 -0
  120. package/dist/server/command-handler.d.ts +93 -0
  121. package/dist/server/command-handler.js +540 -0
  122. package/dist/server/content-search.d.ts +16 -0
  123. package/dist/server/content-search.js +235 -0
  124. package/dist/server/conversation-usage.d.ts +22 -0
  125. package/dist/server/conversation-usage.js +43 -0
  126. package/dist/server/documents.d.ts +152 -0
  127. package/dist/server/documents.js +998 -0
  128. package/dist/server/engineering-learning.d.ts +30 -0
  129. package/dist/server/engineering-learning.js +106 -0
  130. package/dist/server/exec-service.d.ts +45 -0
  131. package/dist/server/exec-service.js +167 -0
  132. package/dist/server/explicit-file-open.d.ts +34 -0
  133. package/dist/server/explicit-file-open.js +105 -0
  134. package/dist/server/failure-text.d.ts +18 -0
  135. package/dist/server/failure-text.js +44 -0
  136. package/dist/server/feishu-cli.d.ts +223 -0
  137. package/dist/server/feishu-cli.js +464 -0
  138. package/dist/server/file-changes.d.ts +119 -0
  139. package/dist/server/file-changes.js +280 -0
  140. package/dist/server/file-diff.d.ts +41 -0
  141. package/dist/server/file-diff.js +97 -0
  142. package/dist/server/file-index.d.ts +100 -0
  143. package/dist/server/file-index.js +296 -0
  144. package/dist/server/file-name-index.d.ts +160 -0
  145. package/dist/server/file-name-index.js +411 -0
  146. package/dist/server/files.d.ts +269 -0
  147. package/dist/server/files.js +748 -0
  148. package/dist/server/git.d.ts +245 -0
  149. package/dist/server/git.js +903 -0
  150. package/dist/server/http-server.d.ts +45 -0
  151. package/dist/server/http-server.js +3830 -0
  152. package/dist/server/index.d.ts +1 -0
  153. package/dist/server/index.js +485 -0
  154. package/dist/server/inline-thinking.d.ts +90 -0
  155. package/dist/server/inline-thinking.js +181 -0
  156. package/dist/server/json-comments.d.ts +14 -0
  157. package/dist/server/json-comments.js +71 -0
  158. package/dist/server/knowledge-embedding-worker.d.ts +1 -0
  159. package/dist/server/knowledge-embedding-worker.js +27 -0
  160. package/dist/server/knowledge-vectors.d.ts +150 -0
  161. package/dist/server/knowledge-vectors.js +385 -0
  162. package/dist/server/knowledge.d.ts +273 -0
  163. package/dist/server/knowledge.js +848 -0
  164. package/dist/server/length-continuation.d.ts +83 -0
  165. package/dist/server/length-continuation.js +118 -0
  166. package/dist/server/logging.d.ts +32 -0
  167. package/dist/server/logging.js +185 -0
  168. package/dist/server/managed-http-tunnel.d.ts +6 -0
  169. package/dist/server/managed-http-tunnel.js +85 -0
  170. package/dist/server/managed-http.d.ts +4 -0
  171. package/dist/server/managed-http.js +85 -0
  172. package/dist/server/managed-model-adapter.d.ts +17 -0
  173. package/dist/server/managed-model-adapter.js +75 -0
  174. package/dist/server/managed-model-gateway.d.ts +49 -0
  175. package/dist/server/managed-model-gateway.js +73 -0
  176. package/dist/server/managed-servers.d.ts +55 -0
  177. package/dist/server/managed-servers.js +392 -0
  178. package/dist/server/managed-ssh.d.ts +26 -0
  179. package/dist/server/managed-ssh.js +73 -0
  180. package/dist/server/manager-vault.d.ts +11 -0
  181. package/dist/server/manager-vault.js +67 -0
  182. package/dist/server/memory-recall.d.ts +25 -0
  183. package/dist/server/memory-recall.js +45 -0
  184. package/dist/server/memory.d.ts +67 -0
  185. package/dist/server/memory.js +216 -0
  186. package/dist/server/message-media.d.ts +7 -0
  187. package/dist/server/message-media.js +54 -0
  188. package/dist/server/model-context-window.d.ts +186 -0
  189. package/dist/server/model-context-window.js +444 -0
  190. package/dist/server/model-filter.d.ts +23 -0
  191. package/dist/server/model-filter.js +48 -0
  192. package/dist/server/models-store.d.ts +36 -0
  193. package/dist/server/models-store.js +132 -0
  194. package/dist/server/native-tool-settings.d.ts +25 -0
  195. package/dist/server/native-tool-settings.js +41 -0
  196. package/dist/server/native-tools-extension.d.ts +2 -0
  197. package/dist/server/native-tools-extension.js +39 -0
  198. package/dist/server/native-tools-service.d.ts +39 -0
  199. package/dist/server/native-tools-service.js +240 -0
  200. package/dist/server/persistence/app-state-repository.d.ts +65 -0
  201. package/dist/server/persistence/app-state-repository.js +1 -0
  202. package/dist/server/persistence/json-state-repository.d.ts +14 -0
  203. package/dist/server/persistence/json-state-repository.js +43 -0
  204. package/dist/server/persistence/open-state-store.d.ts +3 -0
  205. package/dist/server/persistence/open-state-store.js +35 -0
  206. package/dist/server/persistence/rename.d.ts +22 -0
  207. package/dist/server/persistence/rename.js +44 -0
  208. package/dist/server/persistence/sqlite-state-repository.d.ts +12 -0
  209. package/dist/server/persistence/sqlite-state-repository.js +264 -0
  210. package/dist/server/pi-command.d.ts +94 -0
  211. package/dist/server/pi-command.js +265 -0
  212. package/dist/server/pi-native.d.ts +12 -0
  213. package/dist/server/pi-native.js +16 -0
  214. package/dist/server/pi-session-events.d.ts +158 -0
  215. package/dist/server/pi-session-events.js +373 -0
  216. package/dist/server/pi-session-pool.d.ts +233 -0
  217. package/dist/server/pi-session-pool.js +382 -0
  218. package/dist/server/pi-session.d.ts +642 -0
  219. package/dist/server/pi-session.js +655 -0
  220. package/dist/server/presentations/schema.d.ts +120 -0
  221. package/dist/server/presentations/schema.js +128 -0
  222. package/dist/server/provider-network.d.ts +16 -0
  223. package/dist/server/provider-network.js +106 -0
  224. package/dist/server/provider-proxy.d.ts +18 -0
  225. package/dist/server/provider-proxy.js +200 -0
  226. package/dist/server/providers.d.ts +333 -0
  227. package/dist/server/providers.js +989 -0
  228. package/dist/server/remote-bootstrap.d.ts +17 -0
  229. package/dist/server/remote-bootstrap.js +119 -0
  230. package/dist/server/rpc-channel.d.ts +131 -0
  231. package/dist/server/rpc-channel.js +135 -0
  232. package/dist/server/runtime.d.ts +1393 -0
  233. package/dist/server/runtime.js +3286 -0
  234. package/dist/server/scheduled-tasks.d.ts +101 -0
  235. package/dist/server/scheduled-tasks.js +138 -0
  236. package/dist/server/server-context.d.ts +119 -0
  237. package/dist/server/server-context.js +1 -0
  238. package/dist/server/session-cwd.d.ts +19 -0
  239. package/dist/server/session-cwd.js +58 -0
  240. package/dist/server/session-ledger.d.ts +67 -0
  241. package/dist/server/session-ledger.js +76 -0
  242. package/dist/server/session-memory-scope.d.ts +4 -0
  243. package/dist/server/session-memory-scope.js +30 -0
  244. package/dist/server/session-repository.d.ts +100 -0
  245. package/dist/server/session-repository.js +246 -0
  246. package/dist/server/session-tree-scope.d.ts +125 -0
  247. package/dist/server/session-tree-scope.js +337 -0
  248. package/dist/server/skills.d.ts +23 -0
  249. package/dist/server/skills.js +190 -0
  250. package/dist/server/store.d.ts +1062 -0
  251. package/dist/server/store.js +3305 -0
  252. package/dist/server/subagent-runs.d.ts +185 -0
  253. package/dist/server/subagent-runs.js +383 -0
  254. package/dist/server/subagents.d.ts +200 -0
  255. package/dist/server/subagents.js +619 -0
  256. package/dist/server/task-tool.d.ts +282 -0
  257. package/dist/server/task-tool.js +507 -0
  258. package/dist/server/tavily.d.ts +32 -0
  259. package/dist/server/tavily.js +136 -0
  260. package/dist/server/team-context.d.ts +12 -0
  261. package/dist/server/team-context.js +180 -0
  262. package/dist/server/terminal.d.ts +163 -0
  263. package/dist/server/terminal.js +557 -0
  264. package/dist/server/thinking-level-discovery.d.ts +38 -0
  265. package/dist/server/thinking-level-discovery.js +183 -0
  266. package/dist/server/time-zone.d.ts +20 -0
  267. package/dist/server/time-zone.js +32 -0
  268. package/dist/server/tool-calls.d.ts +130 -0
  269. package/dist/server/tool-calls.js +341 -0
  270. package/dist/server/tool-llm.d.ts +72 -0
  271. package/dist/server/tool-llm.js +146 -0
  272. package/dist/server/tool-registry.d.ts +129 -0
  273. package/dist/server/tool-registry.js +249 -0
  274. package/dist/server/tool-runner.d.ts +112 -0
  275. package/dist/server/tool-runner.js +306 -0
  276. package/dist/server/turn-failure.d.ts +48 -0
  277. package/dist/server/turn-failure.js +148 -0
  278. package/dist/server/types.d.ts +2315 -0
  279. package/dist/server/types.js +11 -0
  280. package/dist/server/update-status.d.ts +14 -0
  281. package/dist/server/update-status.js +69 -0
  282. package/dist/server/update.d.ts +46 -0
  283. package/dist/server/update.js +104 -0
  284. package/dist/server/upload.d.ts +35 -0
  285. package/dist/server/upload.js +177 -0
  286. package/dist/server/version.d.ts +33 -0
  287. package/dist/server/version.js +111 -0
  288. package/dist/server/web.d.ts +27 -0
  289. package/dist/server/web.js +183 -0
  290. package/dist/server/workflow-engine.d.ts +47 -0
  291. package/dist/server/workflow-engine.js +255 -0
  292. package/dist/server/workflow.d.ts +329 -0
  293. package/dist/server/workflow.js +469 -0
  294. package/dist/server/workspace-file-indexes.d.ts +2 -0
  295. package/dist/server/workspace-file-indexes.js +61 -0
  296. package/dist/server/workspace-file-operations.d.ts +36 -0
  297. package/dist/server/workspace-file-operations.js +281 -0
  298. package/dist/server/workspace-file-response.d.ts +7 -0
  299. package/dist/server/workspace-file-response.js +79 -0
  300. package/dist/server/ws-server.d.ts +149 -0
  301. package/dist/server/ws-server.js +640 -0
  302. package/dist/shared/agent-order.d.ts +44 -0
  303. package/dist/shared/agent-order.js +50 -0
  304. package/dist/shared/archive-decisions.d.ts +44 -0
  305. package/dist/shared/archive-decisions.js +98 -0
  306. package/dist/shared/background-task.d.ts +25 -0
  307. package/dist/shared/background-task.js +183 -0
  308. package/dist/shared/child-session-lock.d.ts +37 -0
  309. package/dist/shared/child-session-lock.js +59 -0
  310. package/dist/shared/content-search.d.ts +22 -0
  311. package/dist/shared/content-search.js +1 -0
  312. package/dist/shared/conversation-title.d.ts +4 -0
  313. package/dist/shared/conversation-title.js +4 -0
  314. package/dist/shared/file-mentions.d.ts +17 -0
  315. package/dist/shared/file-mentions.js +91 -0
  316. package/dist/shared/git-commit.d.ts +11 -0
  317. package/dist/shared/git-commit.js +1 -0
  318. package/dist/shared/git-graph.d.ts +42 -0
  319. package/dist/shared/git-graph.js +30 -0
  320. package/dist/shared/group-chat.d.ts +132 -0
  321. package/dist/shared/group-chat.js +6 -0
  322. package/dist/shared/managed-servers.d.ts +36 -0
  323. package/dist/shared/managed-servers.js +1 -0
  324. package/dist/shared/message-media.d.ts +12 -0
  325. package/dist/shared/message-media.js +27 -0
  326. package/dist/shared/model-window-label.d.ts +39 -0
  327. package/dist/shared/model-window-label.js +55 -0
  328. package/dist/shared/native-tools.d.ts +43 -0
  329. package/dist/shared/native-tools.js +1 -0
  330. package/dist/shared/pi-cache-warming.d.ts +9 -0
  331. package/dist/shared/pi-cache-warming.js +4 -0
  332. package/dist/shared/project-overview.d.ts +107 -0
  333. package/dist/shared/project-overview.js +92 -0
  334. package/dist/shared/project-presentation.d.ts +55 -0
  335. package/dist/shared/project-presentation.js +111 -0
  336. package/dist/shared/provider-network.d.ts +30 -0
  337. package/dist/shared/provider-network.js +1 -0
  338. package/dist/shared/session-reporting.d.ts +40 -0
  339. package/dist/shared/session-reporting.js +97 -0
  340. package/dist/shared/session-search.d.ts +14 -0
  341. package/dist/shared/session-search.js +1 -0
  342. package/dist/shared/team-context-view.d.ts +33 -0
  343. package/dist/shared/team-context-view.js +49 -0
  344. package/dist/shared/team-context.d.ts +59 -0
  345. package/dist/shared/team-context.js +3 -0
  346. package/dist/shared/terminal-protocol.d.ts +71 -0
  347. package/dist/shared/terminal-protocol.js +1 -0
  348. package/dist/shared/thinking-levels.d.ts +53 -0
  349. package/dist/shared/thinking-levels.js +85 -0
  350. package/dist/shared/tool-glyph.d.ts +7 -0
  351. package/dist/shared/tool-glyph.js +19 -0
  352. package/dist/shared/transcript-order.d.ts +7 -0
  353. package/dist/shared/transcript-order.js +12 -0
  354. package/dist/shared/update-status.d.ts +28 -0
  355. package/dist/shared/update-status.js +1 -0
  356. package/dist/shared/workspace-file-preview.d.ts +4 -0
  357. package/dist/shared/workspace-file-preview.js +28 -0
  358. package/package.json +448 -4
  359. package/scripts/diagnose.mjs +603 -0
  360. package/scripts/patch-brace-expansion.mjs +260 -0
  361. package/scripts/restart-server.mjs +104 -0
  362. package/skills/presentation/SKILL.md +30 -0
  363. package/skills/presentation/references/data.md +10 -0
  364. package/skills/presentation/references/quality.md +12 -0
  365. package/skills/presentation/references/story.md +12 -0
  366. package/skills/presentation/references/templates-and-editing.md +11 -0
  367. package/skills/presentation/references/visual-system.md +10 -0
  368. package/skills/super-pi-agent-development/SKILL.md +25 -0
  369. package/skills/super-pi-development/SKILL.md +31 -0
  370. package/skills/super-pi-tool-development/SKILL.md +39 -0
@@ -0,0 +1,373 @@
1
+ /**
2
+ * THE EVENT BRIDGE: turns a Pi session's event stream into the turn-shaped stream Super Pi consumes.
3
+ *
4
+ * WHY A BRIDGE AND NOT AN INLINE LOOP. The old design read a child's stdout in a `while` loop and
5
+ * dispatched records inline. The new design has no child and no stdout: Pi emits EVENTS through a
6
+ * subscription. A subscription is a push, and Super Pi's turn is a pull - `answer()` is an async
7
+ * generator that yields text deltas to its caller.
8
+ *
9
+ * A push has to become a pull somewhere, and this is where. The bridge keeps a queue of pending items
10
+ * and hands them out one at a time; the generator awaits the next item when the queue is empty. The
11
+ * ordering that matters is preserved exactly: the subscription callback pushes, and the queue is FIFO,
12
+ * so the order the reader sees is the order Pi emitted.
13
+ *
14
+ * THE BOUNDARY IS THE SAME AS BEFORE. Only the events Super Pi already consumed are routed; everything
15
+ * else is counted and reported once, exactly as the old `noteUnhandled` did. A record type that used to
16
+ * be announced as unhandled is still announced as unhandled - this bridge changes WHERE the events come
17
+ * from, not what they mean.
18
+ */
19
+ import { describeToolCall, describeToolCallArgs, extractPartialResultText, readQuestionCall } from "./tool-calls.js";
20
+ import { toolTargetPath } from "./file-changes.js";
21
+ import { LengthExceededError } from "./turn-failure.js";
22
+ /** Keeps only real strings, so a malformed queue snapshot cannot put objects into a broadcast. */
23
+ const textList = (value) => Array.isArray(value) ? value.filter((item) => typeof item === "string") : undefined;
24
+ /**
25
+ * Reads a `queue_update` payload, or returns undefined when the record is not one.
26
+ *
27
+ * The same recogniser the RPC drain loop uses, and deliberately so: Pi's RPC mode forwards the session's
28
+ * own events verbatim, so a `queue_update` from a persistent SDK session and one from a child's stdout
29
+ * are the same fact in the same shape - Pi reporting what its in-memory steering/follow-up queues hold
30
+ * (`agent-session.ts:_emitQueueUpdate`). It is the ONLY proof that a steer was actually picked up: Pi
31
+ * removes the entry from its queue and emits this snapshot BEFORE it emits the delivered `message_start`,
32
+ * so a host that ignored it would have no way to tell "queued in Pi" from "delivered to the model".
33
+ */
34
+ export function describePiQueue(record) {
35
+ const event = record;
36
+ if (event?.type !== "queue_update")
37
+ return undefined;
38
+ return { steering: textList(event.steering) ?? [], followUp: textList(event.followUp) ?? [] };
39
+ }
40
+ /**
41
+ * Reads the text of a steer Pi has just delivered, or undefined when this record is not one.
42
+ *
43
+ * A `message_start` carrying a `user` message is exactly that, and nothing else is: the prompt that
44
+ * opened the turn is never re-announced this way, and `assistant` messages are not deliveries.
45
+ *
46
+ * Content is joined with a newline rather than taken from the first block, so a multi-block steer comes
47
+ * back as the text the user actually wrote instead of its first paragraph. The text is a MATCHING HINT
48
+ * only - Pi identifies its own queued entry by `indexOf` on this string (`agent-session.ts:1123`) and so
49
+ * cannot tell two identical messages apart either, which is why the caller resolves a delivery by
50
+ * position and not by comparing this.
51
+ */
52
+ export function describeDeliveredUserMessage(record) {
53
+ const event = record;
54
+ if (event?.type !== "message_start")
55
+ return undefined;
56
+ const message = event.message;
57
+ if (!message || typeof message !== "object" || message.role !== "user")
58
+ return undefined;
59
+ const content = message.content;
60
+ if (!Array.isArray(content))
61
+ return undefined;
62
+ const parts = content
63
+ .map((block) => (block && typeof block === "object" && typeof block.text === "string" ? block.text : undefined))
64
+ .filter((part) => part !== undefined);
65
+ return parts.length ? parts.join("\n") : undefined;
66
+ }
67
+ /**
68
+ * Reads the failure out of a `message_end` record, or undefined when it is not a failed one.
69
+ *
70
+ * ONLY THE ASSISTANT'S MESSAGE COUNTS. `message_end` also fires for user messages, tool results and
71
+ * custom messages, and none of those is a turn failing.
72
+ */
73
+ export function describeMessageFailure(record) {
74
+ const event = record;
75
+ if (event?.type !== "message_end")
76
+ return undefined;
77
+ const message = event.message;
78
+ if (!message || typeof message !== "object" || message.role !== "assistant")
79
+ return undefined;
80
+ const text = typeof message.errorMessage === "string" ? message.errorMessage.trim() : "";
81
+ if (message.stopReason === "error") {
82
+ return { message: text || "Pi 报告模型或服务提供方错误。", stopReason: "error" };
83
+ }
84
+ if (message.stopReason === "length") {
85
+ // A response that hit the length limit is an INCOMPLETE answer. Pi compacts and continues after a
86
+ // recoverable one, so a turn that SETTLES with this reason is one whose answer is missing its
87
+ // end - and saying so is better than a turn that looks finished.
88
+ return { message: text || "模型输出达到本轮长度上限,回答未完成。", stopReason: "length" };
89
+ }
90
+ return undefined;
91
+ }
92
+ /**
93
+ * A queue of turn items, with the waiters that are blocked on it.
94
+ *
95
+ * The `settled` flag matters: a turn that ends must release every waiter, including one that is
96
+ * waiting for text that will never arrive. A generator awaiting an item on a queue nobody will ever
97
+ * push to is a turn that hangs forever, and a hung turn is a conversation that never answers.
98
+ */
99
+ export class TurnQueue {
100
+ items = [];
101
+ waiters = [];
102
+ settled = false;
103
+ /**
104
+ * Resolves when Pi has SETTLED the turn, which is the real end of a turn.
105
+ *
106
+ * The distinction matters: Pi's `prompt()` resolves when the message has been accepted, which is
107
+ * long before the agent has settled. A caller that treats the prompt's resolution as the end of the
108
+ * turn would release the reader while the model was still producing - and the next turn would then
109
+ * arrive at a session Pi considers busy, which Pi refuses outright.
110
+ */
111
+ settledSignal;
112
+ markSettled;
113
+ constructor() {
114
+ this.settledSignal = new Promise((resolvePromise) => { this.markSettled = resolvePromise; });
115
+ }
116
+ push(item) {
117
+ if (this.settled)
118
+ return;
119
+ if (item.kind === "end" || item.kind === "error") {
120
+ this.settled = true;
121
+ // THE TERMINATING ITEM GOES AFTER THE TEXT THAT IS ALREADY QUEUED. The model produced those
122
+ // deltas, they are part of the answer, and dropping them would truncate the reply at exactly
123
+ // the moment the turn ended - which is the moment the answer is longest. So the terminal item is
124
+ // APPENDED, and a consumer reads the text first and the end second.
125
+ //
126
+ // A waiter that is already blocked is released immediately, because it is waiting for the NEXT
127
+ // item and the queue is finished - there will never be another one.
128
+ if (this.items.length === 0) {
129
+ const waiter = this.waiters.shift();
130
+ if (waiter) {
131
+ waiter(item);
132
+ this.markSettled();
133
+ return;
134
+ }
135
+ }
136
+ this.items.push(item);
137
+ // Any waiter still blocked gets the terminal item, so a consumer that is behind by one wake-up
138
+ // still terminates rather than hanging.
139
+ while (this.waiters.length)
140
+ this.waiters.shift()?.(item);
141
+ return;
142
+ }
143
+ const waiter = this.waiters.shift();
144
+ if (waiter)
145
+ waiter(item);
146
+ else
147
+ this.items.push(item);
148
+ }
149
+ /** Settles the queue with an error, for a turn that failed before it produced anything. */
150
+ fail(error) {
151
+ this.push({ kind: "error", error });
152
+ }
153
+ async next() {
154
+ // The queue is drained BEFORE the settled flag is consulted: the terminal item was APPENDED after
155
+ // the text, so a settled queue can still have text in it, and that text is the model's answer.
156
+ const item = this.items.shift();
157
+ if (item) {
158
+ // The settled signal fires when the terminal item is TAKEN, not when it is queued. A caller
159
+ // waiting for "the turn is over" must not be released while the answer is still unread.
160
+ if (item.kind === "end" || item.kind === "error")
161
+ this.markSettled();
162
+ return item;
163
+ }
164
+ if (this.settled) {
165
+ this.markSettled();
166
+ return { kind: "end" };
167
+ }
168
+ return new Promise((resolve) => { this.waiters.push(resolve); });
169
+ }
170
+ get isSettled() { return this.settled; }
171
+ }
172
+ /**
173
+ * Handles one Pi event, routing it to the queue and to the turn's callbacks.
174
+ *
175
+ * Returns whether the event was consumed. An unconsumed event is reported once per distinct type and
176
+ * then counted - the same policy the RPC loop applied, and for the same reason: an event type nobody
177
+ * understands is worth saying out loud exactly once.
178
+ */
179
+ export function handleSessionEvent(event, queue, handlers, report, failure) {
180
+ const record = event;
181
+ if (!record || typeof record.type !== "string")
182
+ return false;
183
+ switch (record.type) {
184
+ case "message_end": {
185
+ // THE AUTHORITATIVE END OF AN ASSISTANT MESSAGE. `message_end` is the completed message, and its
186
+ // `stopReason` is the only place Pi says whether the model actually answered. The bridge used to
187
+ // ignore this event, which meant a provider that failed was indistinguishable from one that
188
+ // finished: the turn settled, the status became "completed", and the reader saw a turn that
189
+ // looked fine and had no answer in it.
190
+ //
191
+ // A failure is HELD, not thrown, because Pi retries on its own - the message that failed may be
192
+ // followed by another one. The retry starts a new assistant message, and THAT clears the held
193
+ // failure, so only the last message of the turn decides.
194
+ const reported = describeMessageFailure(record);
195
+ if (reported && failure)
196
+ failure.current = reported;
197
+ return true;
198
+ }
199
+ case "message_start": {
200
+ // A NEW ASSISTANT MESSAGE MEANS A NEW ATTEMPT. Pi's retry starts one, and the failure of the
201
+ // attempt before it is no longer the failure of this turn.
202
+ if (record.message?.role === "assistant" && failure)
203
+ failure.current = undefined;
204
+ // A STEER THIS TURN DELIVERED. Pi announces the moment a queued message stops waiting and starts
205
+ // being answered by emitting `message_start` with the user message it picked up, immediately after
206
+ // the `queue_update` above that shows the entry gone. The host uses it to split the turn so the
207
+ // answer lands under the message it answers.
208
+ //
209
+ // A NON-user `message_start` is not a delivery: it gets the same once-per-type report every other
210
+ // unrouted event gets, which is the same policy the RPC drain loop applies.
211
+ const delivered = describeDeliveredUserMessage(record);
212
+ if (delivered === undefined) {
213
+ if (record.message?.role === "assistant")
214
+ return true;
215
+ report(`type ${JSON.stringify(record.type)}`);
216
+ return true;
217
+ }
218
+ try {
219
+ handlers.onSteerDelivered?.(delivered);
220
+ }
221
+ catch (error) {
222
+ console.warn(`PiRpcRuntime: the steer delivery handler threw: ${error instanceof Error ? error.message : "unknown error"}`);
223
+ }
224
+ return true;
225
+ }
226
+ case "message_update": {
227
+ const assistant = record.assistantMessageEvent;
228
+ if (!assistant)
229
+ return false;
230
+ if (assistant.type === "text_delta") {
231
+ queue.push({ kind: "text", delta: assistant.delta ?? "" });
232
+ return true;
233
+ }
234
+ if (assistant.type === "thinking_delta") {
235
+ handlers.onThinking?.(assistant.delta ?? "");
236
+ return true;
237
+ }
238
+ if (assistant.type === "thinking_end") {
239
+ handlers.onThinkingEnd?.();
240
+ return true;
241
+ }
242
+ if (assistant.type === "toolcall_start" && typeof assistant.contentIndex === "number") {
243
+ handlers.onToolPreparing?.({ contentIndex: assistant.contentIndex, name: toolNameOf(assistant) });
244
+ return true;
245
+ }
246
+ if (assistant.type === "toolcall_end") {
247
+ // The name is announced with the START, and the end is what says the arguments are complete.
248
+ return true;
249
+ }
250
+ return false;
251
+ }
252
+ case "tool_execution_start": {
253
+ const id = record.toolCallId ?? "";
254
+ if (!id)
255
+ return true;
256
+ const name = record.toolName ?? "unknown";
257
+ try {
258
+ // A `question` CALL IS READ HERE, AT `tool_execution_start` - the same place the one-shot path
259
+ // reads it, and for the same reason: the card on screen must be the tool call the model is
260
+ // actually blocked in, with the same identity and the same lifecycle. This path left the
261
+ // question out, so the host never saw a pending question and the model was told the session
262
+ // had no question waiting for an answer - the `question` tool was simply dead on the SDK
263
+ // session path, which is the PRODUCTION default (ledger TEST-037). Two paths must give the
264
+ // same answer to "what is a question call".
265
+ const question = readQuestionCall(name, record.args);
266
+ handlers.onToolCall?.({ id, name, detail: describeToolCall(name, record.args), args: describeToolCallArgs(record.args), ...(question ? { question } : {}) });
267
+ }
268
+ catch (error) {
269
+ console.warn(`PiRpcRuntime: the tool call handler threw: ${error instanceof Error ? error.message : "unknown error"}`);
270
+ }
271
+ // The file-writing call, announced before it runs - the only moment the old content is still on
272
+ // the disk. Fired and forgotten: the listener does its own I/O, off this handler's path.
273
+ const writePath = toolTargetPath(name, record.args);
274
+ if (writePath) {
275
+ try {
276
+ handlers.onFileWrite?.(writePath, name, id);
277
+ }
278
+ catch (error) {
279
+ console.warn(`PiRpcRuntime: the file-write handler threw: ${error instanceof Error ? error.message : "unknown error"}`);
280
+ }
281
+ }
282
+ return true;
283
+ }
284
+ case "tool_execution_update": {
285
+ const id = record.toolCallId ?? "";
286
+ if (!id)
287
+ return true;
288
+ const name = record.toolName ?? "unknown";
289
+ const live = extractPartialResultText(record.partialResult);
290
+ try {
291
+ handlers.onToolUpdate?.({ id, name, detail: describeToolCall(name, record.args), args: describeToolCallArgs(record.args), ...(live ? { live } : {}) });
292
+ }
293
+ catch (error) {
294
+ console.warn(`PiRpcRuntime: the tool update handler threw: ${error instanceof Error ? error.message : "unknown error"}`);
295
+ }
296
+ return true;
297
+ }
298
+ case "tool_execution_end": {
299
+ const id = record.toolCallId ?? "";
300
+ if (!id)
301
+ return true;
302
+ const name = record.toolName ?? "unknown";
303
+ try {
304
+ handlers.onToolEnd?.({
305
+ id,
306
+ name,
307
+ status: record.isError === true ? "error" : "ok",
308
+ ...(typeof record.durationMs === "number" && Number.isFinite(record.durationMs) ? { durationMs: Math.max(0, Math.round(record.durationMs)) } : {}),
309
+ });
310
+ }
311
+ catch (error) {
312
+ console.warn(`PiRpcRuntime: the tool end handler threw: ${error instanceof Error ? error.message : "unknown error"}`);
313
+ }
314
+ return true;
315
+ }
316
+ case "queue_update": {
317
+ // Pi's own queue, pushed as a full snapshot whenever it changes. This is the only proof that a
318
+ // steer was really picked up - Pi removes the entry and emits this BEFORE the delivered
319
+ // `message_start` below - so it is routed, not dropped.
320
+ const snapshot = describePiQueue(record);
321
+ if (!snapshot)
322
+ return false;
323
+ try {
324
+ handlers.onPiQueue?.(snapshot);
325
+ }
326
+ catch (error) {
327
+ console.warn(`PiRpcRuntime: the pi queue handler threw: ${error instanceof Error ? error.message : "unknown error"}`);
328
+ }
329
+ return true;
330
+ }
331
+ case "agent_settled": {
332
+ // THE TURN IS OVER, AND IT MAY HAVE FAILED. Pi settling is the end of the turn, and a turn that
333
+ // ended because the provider failed has to say so: a failure that is not raised is a failed turn
334
+ // that the reader sees as a completed one, with no answer in it and no banner above it.
335
+ //
336
+ // The failure is raised HERE and not when it arrived, because Pi retries on its own - a message
337
+ // that failed may be followed by another one, and only the last one decides.
338
+ const held = failure?.current;
339
+ if (held) {
340
+ failure.current = undefined;
341
+ const err = held.stopReason === "length"
342
+ ? new LengthExceededError(held.message)
343
+ : new Error(held.message);
344
+ queue.push({ kind: "error", error: err });
345
+ return true;
346
+ }
347
+ queue.push({ kind: "end" });
348
+ return true;
349
+ }
350
+ default:
351
+ // AN EVENT NOBODY UNDERSTANDS IS REPORTED, HERE. The reporting lives inside this function rather
352
+ // than in its caller so that a caller cannot forget it: a new event type that nobody handles
353
+ // would then be silently dropped, and the "it looks wired up but nothing happens" failure is
354
+ // exactly the one that is hardest to diagnose from the outside.
355
+ report(`type ${JSON.stringify(record.type)}`);
356
+ return true;
357
+ }
358
+ }
359
+ /**
360
+ * The name of a tool call, read the way the RPC side reads it.
361
+ *
362
+ * `toolcall_start` may carry the name directly, or only inside a partial content block - the RPC side
363
+ * reads the direct field first and falls back to the block, and the same order applies here, because
364
+ * the same Pi produces both shapes depending on how much of the call has been written.
365
+ */
366
+ function toolNameOf(event) {
367
+ const direct = event.toolName;
368
+ if (typeof direct === "string" && direct)
369
+ return direct;
370
+ const content = event.partial?.content;
371
+ const block = Array.isArray(content) ? content.find((item) => item?.type === "toolCall") : undefined;
372
+ return typeof block?.name === "string" ? block.name : "";
373
+ }
@@ -0,0 +1,233 @@
1
+ /**
2
+ * THE SESSION POOL: one live Pi session per conversation, kept across turns.
3
+ *
4
+ * WHAT IT OWNS. A conversation's Pi session, its fingerprint, and the rule that decides when the live
5
+ * session has gone stale and must be thrown away. Everything that can make a session stale is a
6
+ * FINGERPRINT (see `pi-session.ts`): the agent's prompt, the tool allowlist, the mounted extensions,
7
+ * the workspace, the model. Pi decides all of those when a session is created and cannot change any of
8
+ * them afterwards, so a session whose fingerprint no longer matches the conversation's current
9
+ * requirements is not "slightly wrong" - it is the wrong session, and the only honest response is to
10
+ * replace it.
11
+ *
12
+ * WHY THE POOL IS EXPLICIT ABOUT EVICTING. A session holds a model client, a session file handle and a
13
+ * conversation context. Left alone, a server that has seen fifty conversations would hold fifty of
14
+ * them, and the ones nobody is using would be the ones costing the most memory. So an idle session is
15
+ * evicted after a while, and the conversation's context is not lost with it: Pi writes the transcript
16
+ * to disk, and the next session for that conversation is rebuilt from that file.
17
+ *
18
+ * THE REVERT CASE. `revert` truncates the transcript on disk. A live session's in-memory context does
19
+ * NOT change, and nothing in Pi would notice: the model would keep answering from history the user
20
+ * has withdrawn. So a revert must EVICT the session, and the next turn rebuilds from the truncated
21
+ * file. This is the one case where eviction is not an optimization but a correctness requirement.
22
+ */
23
+ import { type PiSessionFactory, type PiSessionLike } from "./pi-session.js";
24
+ /** What the pool needs to build a session for a conversation. */
25
+ export interface SessionRequest {
26
+ conversationId: string;
27
+ cwd: string;
28
+ /** The recorded transcript locator, verified against the real header before use. */
29
+ sessionFile?: string;
30
+ /** The agent's id, or null. */
31
+ agentId: string | null;
32
+ /** The environment Super Pi's extensions read when they are loaded. */
33
+ extensionEnv: Record<string, string>;
34
+ /** The tool allowlist, or undefined for Pi's own defaults. */
35
+ tools?: string[];
36
+ /** The tool denylist. */
37
+ excludeTools?: string[];
38
+ /** The model, when the conversation pins one. */
39
+ model?: {
40
+ provider: string;
41
+ modelId: string;
42
+ };
43
+ /** The thinking level, when the conversation pins one. */
44
+ thinkingLevel?: string;
45
+ /** The agent's system prompt file, when the conversation runs as an agent. */
46
+ systemPromptFile?: string;
47
+ /** Whether the agent prompt replaces Pi's own. */
48
+ systemPromptMode?: "replace" | "append";
49
+ }
50
+ /**
51
+ * The pool.
52
+ *
53
+ * One instance per server. It is deliberately not a general-purpose pool: the sessions it holds differ
54
+ * from each other in their agent, their tools and their workspace, so there is nothing to share between
55
+ * them and no reason to try.
56
+ */
57
+ export declare class PiSessionPool {
58
+ /** Where Pi keeps its session files. */
59
+ private readonly sessionDir;
60
+ /** The repository root, for locating Super Pi's own extensions. */
61
+ private readonly repositoryRoot;
62
+ /** Pi's global configuration directory, when it is not Pi's own default. */
63
+ private readonly agentDir?;
64
+ /**
65
+ * Called when a session is destroyed, so the caller can release whatever the session was holding.
66
+ *
67
+ * A session holds a capability: the token its extension read out of the environment when it was
68
+ * loaded. When the session goes, that token must go with it - a credential whose holder no longer
69
+ * exists is exactly the shape the capability model exists to prevent. The pool does not know what a
70
+ * capability is, so it says WHEN and the caller says WHAT.
71
+ */
72
+ private readonly onSessionDestroyed?;
73
+ /**
74
+ * How a session is built. Injectable so a test can drive a session without a real Pi behind it.
75
+ */
76
+ private readonly factory;
77
+ /**
78
+ * Mints the credential the session's extension will hold. Injected rather than imported so the pool
79
+ * does not have to know what a capability is - it needs to know WHEN one is needed, and the caller
80
+ * knows what one looks like.
81
+ */
82
+ private readonly mintCapability;
83
+ /**
84
+ * How many times the set of models Pi knows about has changed.
85
+ *
86
+ * Injected rather than imported for the same reason the capability minting is: the pool needs to
87
+ * know WHEN a session is stale, and the caller knows what changed. A provider added through the
88
+ * settings window makes every live session unable to reach the new model, and the next turn is
89
+ * where that gets fixed.
90
+ */
91
+ private readonly modelEpoch;
92
+ /**
93
+ * Mints the OTHER per-session credentials the extensions need, at the moment the session is born.
94
+ *
95
+ * WHY A SECOND CALLBACK AND NOT PART OF THE CAPABILITY. The memory and knowledge extensions each
96
+ * need their own credential - the memory one even carries per-caller flags (read-only for a
97
+ * sub-agent) that a single session capability cannot express. The one-shot path has always minted
98
+ * them per turn; the SESSION path minted none, so those two extensions returned before registering
99
+ * a single tool or injecting a single line of prompt, and both features were absent in production -
100
+ * the SDK session path is the default backend, and only the test scripts use the one-shot path
101
+ * (ledger TEST-039).
102
+ *
103
+ * They are minted HERE, where the session is born, for the same reason the capability is: the
104
+ * extension reads its environment ONCE, when it is loaded, so a token minted per turn would be
105
+ * replaced before the second turn and the extension would still be holding the first turn's value.
106
+ */
107
+ private readonly mintExtensionTokens;
108
+ private readonly sessions;
109
+ constructor(
110
+ /** Where Pi keeps its session files. */
111
+ sessionDir: string,
112
+ /** The repository root, for locating Super Pi's own extensions. */
113
+ repositoryRoot: string,
114
+ /** Pi's global configuration directory, when it is not Pi's own default. */
115
+ agentDir?: string | undefined,
116
+ /**
117
+ * Called when a session is destroyed, so the caller can release whatever the session was holding.
118
+ *
119
+ * A session holds a capability: the token its extension read out of the environment when it was
120
+ * loaded. When the session goes, that token must go with it - a credential whose holder no longer
121
+ * exists is exactly the shape the capability model exists to prevent. The pool does not know what a
122
+ * capability is, so it says WHEN and the caller says WHAT.
123
+ */
124
+ onSessionDestroyed?: ((conversationId: string) => void) | undefined,
125
+ /**
126
+ * How a session is built. Injectable so a test can drive a session without a real Pi behind it.
127
+ */
128
+ factory?: PiSessionFactory,
129
+ /**
130
+ * Mints the credential the session's extension will hold. Injected rather than imported so the pool
131
+ * does not have to know what a capability is - it needs to know WHEN one is needed, and the caller
132
+ * knows what one looks like.
133
+ */
134
+ mintCapability?: (conversationId: string) => string,
135
+ /**
136
+ * How many times the set of models Pi knows about has changed.
137
+ *
138
+ * Injected rather than imported for the same reason the capability minting is: the pool needs to
139
+ * know WHEN a session is stale, and the caller knows what changed. A provider added through the
140
+ * settings window makes every live session unable to reach the new model, and the next turn is
141
+ * where that gets fixed.
142
+ */
143
+ modelEpoch?: () => number,
144
+ /**
145
+ * Mints the OTHER per-session credentials the extensions need, at the moment the session is born.
146
+ *
147
+ * WHY A SECOND CALLBACK AND NOT PART OF THE CAPABILITY. The memory and knowledge extensions each
148
+ * need their own credential - the memory one even carries per-caller flags (read-only for a
149
+ * sub-agent) that a single session capability cannot express. The one-shot path has always minted
150
+ * them per turn; the SESSION path minted none, so those two extensions returned before registering
151
+ * a single tool or injecting a single line of prompt, and both features were absent in production -
152
+ * the SDK session path is the default backend, and only the test scripts use the one-shot path
153
+ * (ledger TEST-039).
154
+ *
155
+ * They are minted HERE, where the session is born, for the same reason the capability is: the
156
+ * extension reads its environment ONCE, when it is loaded, so a token minted per turn would be
157
+ * replaced before the second turn and the extension would still be holding the first turn's value.
158
+ */
159
+ mintExtensionTokens?: (conversationId: string) => Promise<Record<string, string>> | Record<string, string>);
160
+ /**
161
+ * The session for a conversation, built if there is none and REPLACED if the live one is stale.
162
+ *
163
+ * The fingerprint comparison is what makes a stale session impossible to use: it happens BEFORE the
164
+ * session is handed out, not after, so a caller can never accidentally run a turn on a session whose
165
+ * tool allowlist has changed.
166
+ */
167
+ acquire(request: SessionRequest): Promise<PiSessionLike>;
168
+ /** Keep an accidental session fan-out from retaining one full transcript context per conversation. */
169
+ private evictOverflow;
170
+ private build;
171
+ /**
172
+ * Puts a live session on the model and level the conversation is asking for.
173
+ *
174
+ * Best-effort in the way that matters: a session that cannot change its model is still a working
175
+ * session, and refusing the message because Pi declined a model switch would be a worse answer than
176
+ * running on the model that is loaded. So the failure is a warning, and the turn goes on.
177
+ *
178
+ * The comparison is against what the session is ALREADY running, because Pi can report its model in
179
+ * two shapes (an id or a provider/id pair) and a mismatch on shape alone would mean re-setting the
180
+ * model on every single turn.
181
+ */
182
+ private applyModelAndLevel;
183
+ private fingerprintOf;
184
+ /**
185
+ * The live session for a conversation, without building one.
186
+ *
187
+ * Absent when there is none - including when the session is still being built, because a session
188
+ * that is not ready cannot be steered into.
189
+ */
190
+ get(conversationId: string): PiSessionLike | undefined;
191
+ /**
192
+ * Whether a conversation has a live session, without building one.
193
+ *
194
+ * Used by the paths that need to know whether a session EXISTS - `revert` has to know whether there is
195
+ * an in-memory context to invalidate - and by the tests.
196
+ */
197
+ has(conversationId: string): boolean;
198
+ /**
199
+ * Throws away a conversation's session.
200
+ *
201
+ * The reason this is a PUBLIC operation and not an internal detail is that the callers are the ones
202
+ * that know WHY the session is stale. `revert` calls it because the disk changed under the session;
203
+ * a configuration change calls it because the requirements changed. The pool does not second-guess
204
+ * either - it disposes what it is told to dispose.
205
+ */
206
+ evict(conversationId: string): Promise<void>;
207
+ /**
208
+ * Throws away every session that has not been used for a while.
209
+ *
210
+ * The conversation is not lost: Pi wrote the transcript to disk, and the next turn rebuilds the
211
+ * session from that file. What is lost is the in-memory context, which is exactly what an idle
212
+ * conversation does not need.
213
+ */
214
+ evictIdle(now?: number): Promise<void>;
215
+ /** Throws away every session. Used at shutdown. */
216
+ disposeAll(): Promise<void>;
217
+ private closeSession;
218
+ /** The conversations with a live session. */
219
+ get conversationIds(): string[];
220
+ }
221
+ /**
222
+ * Where Super Pi's repository root is, found from the compiled server's own location.
223
+ *
224
+ * The extensions are mounted by PATH, and Pi resolves a relative path against the CONVERSATION's
225
+ * workspace rather than the server's directory - so the path has to be absolute, and it has to be
226
+ * derived from something that is the same in every way the server runs (built to `dist/server/`, or run
227
+ * from `src/server/` under tsx). `import.meta.dirname` is that thing.
228
+ */
229
+ export declare function repositoryRootFrom(from?: string): string;
230
+ /** Whether Super Pi's own extensions are present on disk. */
231
+ export declare function extensionsAvailable(repositoryRoot: string): boolean;
232
+ /** Whether the extension directory exists at all, for a clearer failure message. */
233
+ export declare function extensionDirectoryExists(repositoryRoot: string): boolean;