@code-yeongyu/senpi 2026.9.25 → 2026.9.27

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 (152) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/dist/bun-runtime.d.ts +18 -2
  3. package/dist/bun-runtime.d.ts.map +1 -1
  4. package/dist/bun-runtime.js +60 -8
  5. package/dist/bun-runtime.js.map +1 -1
  6. package/dist/bundle/chunks/{app-server-command-RB6QUPRI.js → app-server-command-7WEJGW7D.js} +1 -1
  7. package/dist/bundle/chunks/bun-extension-importer-R4HXF3GD.js +14 -0
  8. package/dist/bundle/chunks/{chunk-JJ3NJZBD.js → chunk-3QCAGZFO.js} +19 -18
  9. package/dist/bundle/chunks/chunk-HCXLCGMF.js +8 -0
  10. package/dist/bundle/chunks/{chunk-OZCB6BP3.js → chunk-HH3D5ZYA.js} +3 -3
  11. package/dist/bundle/chunks/chunk-W3D3ZYDS.js +2 -0
  12. package/dist/bundle/chunks/{cli-main-KO53KYDN.js → cli-main-YLZ4MMLV.js} +1 -1
  13. package/dist/bundle/chunks/{interactive-host-runtime-YI75GFD5.js → interactive-host-runtime-6WY6424X.js} +1 -1
  14. package/dist/bundle/chunks/{interactive-mode-ABDNLJSI.js → interactive-mode-6AHR5TEM.js} +1 -1
  15. package/dist/bundle/chunks/{multi-session-host-JV3MNWXZ.js → multi-session-host-5IWJEFDF.js} +1 -1
  16. package/dist/bundle/chunks/{package-manager-cli-ZYYAVCXU.js → package-manager-cli-FZKBCLWV.js} +1 -1
  17. package/dist/bundle/chunks/{rpc-mode-Q2OUJURB.js → rpc-mode-JSCHAE6O.js} +1 -1
  18. package/dist/bundle/chunks/session-worker.js +119 -118
  19. package/dist/bundle/cli.js +1 -1
  20. package/dist/bundle/index.js +1 -1
  21. package/dist/bundle/rpc-entry.js +1 -1
  22. package/dist/core/agent-session.d.ts.map +1 -1
  23. package/dist/core/agent-session.js +10 -5
  24. package/dist/core/agent-session.js.map +1 -1
  25. package/dist/core/dynamic-prompt/handoff.js +1 -1
  26. package/dist/core/dynamic-prompt/handoff.js.map +1 -1
  27. package/dist/core/extensions/builtin/todotools/state.d.ts +1 -1
  28. package/dist/core/extensions/builtin/todotools/state.d.ts.map +1 -1
  29. package/dist/core/extensions/builtin/todotools/state.js +1 -1
  30. package/dist/core/extensions/builtin/todotools/state.js.map +1 -1
  31. package/dist/core/extensions/builtin/ttsr/detectors/repetitive-turns.d.ts.map +1 -1
  32. package/dist/core/extensions/builtin/ttsr/detectors/repetitive-turns.js +3 -1
  33. package/dist/core/extensions/builtin/ttsr/detectors/repetitive-turns.js.map +1 -1
  34. package/dist/core/extensions/bun-extension-importer.d.ts.map +1 -1
  35. package/dist/core/extensions/bun-extension-importer.js +10 -4
  36. package/dist/core/extensions/bun-extension-importer.js.map +1 -1
  37. package/dist/core/extensions/bun-extension-registry.js +6 -5
  38. package/dist/core/extensions/bun-extension-registry.js.map +1 -1
  39. package/dist/core/messages.d.ts.map +1 -1
  40. package/dist/core/messages.js +4 -2
  41. package/dist/core/messages.js.map +1 -1
  42. package/dist/core/provider-rejected-images.d.ts +18 -0
  43. package/dist/core/provider-rejected-images.d.ts.map +1 -0
  44. package/dist/core/provider-rejected-images.js +107 -0
  45. package/dist/core/provider-rejected-images.js.map +1 -0
  46. package/dist/core/session-title-generator.d.ts.map +1 -1
  47. package/dist/core/session-title-generator.js +21 -3
  48. package/dist/core/session-title-generator.js.map +1 -1
  49. package/dist/core/session-title-reasoning.d.ts +14 -0
  50. package/dist/core/session-title-reasoning.d.ts.map +1 -0
  51. package/dist/core/session-title-reasoning.js +31 -0
  52. package/dist/core/session-title-reasoning.js.map +1 -0
  53. package/dist/core/tools/path-utils.d.ts.map +1 -1
  54. package/dist/core/tools/path-utils.js +13 -0
  55. package/dist/core/tools/path-utils.js.map +1 -1
  56. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  57. package/dist/modes/interactive/interactive-mode.js +2 -0
  58. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  59. package/dist/modes/interactive/runtime-notice-presenter.d.ts +8 -0
  60. package/dist/modes/interactive/runtime-notice-presenter.d.ts.map +1 -0
  61. package/dist/modes/interactive/runtime-notice-presenter.js +69 -0
  62. package/dist/modes/interactive/runtime-notice-presenter.js.map +1 -0
  63. package/dist/modes/interactive/runtime-notice.d.ts +50 -0
  64. package/dist/modes/interactive/runtime-notice.d.ts.map +1 -0
  65. package/dist/modes/interactive/runtime-notice.js +104 -0
  66. package/dist/modes/interactive/runtime-notice.js.map +1 -0
  67. package/docs/environment-variables.md +2 -0
  68. package/docs/quickstart.md +7 -1
  69. package/node_modules/@code-yeongyu/senpi-codemode/CHANGELOG.md +24 -0
  70. package/node_modules/@code-yeongyu/senpi-codemode/package.json +4 -4
  71. package/node_modules/@earendil-works/pi-agent-core/package.json +3 -3
  72. package/node_modules/@earendil-works/pi-ai/dist/providers/data/.manifest.json +1 -1
  73. package/node_modules/@earendil-works/pi-ai/dist/providers/data/fireworks.json +1 -1
  74. package/node_modules/@earendil-works/pi-ai/dist/providers/data/opencode-go.json +1 -1
  75. package/node_modules/@earendil-works/pi-ai/dist/providers/data/opencode.json +1 -1
  76. package/node_modules/@earendil-works/pi-ai/dist/providers/data/openrouter.json +1 -1
  77. package/node_modules/@earendil-works/pi-ai/dist/providers/data/vercel-ai-gateway.json +1 -1
  78. package/node_modules/@earendil-works/pi-ai/package.json +2 -2
  79. package/node_modules/@earendil-works/pi-pty/native/prebuilds/darwin-arm64/senpi_pty.darwin-arm64.node +0 -0
  80. package/node_modules/@earendil-works/pi-pty/package.json +1 -1
  81. package/node_modules/@earendil-works/pi-telemetry/package.json +1 -1
  82. package/node_modules/@earendil-works/pi-tui/package.json +1 -1
  83. package/package.json +7 -22
  84. package/dist/bundle/chunks/bun-extension-importer-H4IJCWWQ.js +0 -14
  85. package/dist/bundle/chunks/chunk-34HMVB33.js +0 -2
  86. package/dist/bundle/chunks/chunk-SGYEOIOJ.js +0 -8
  87. package/node_modules/@code-yeongyu/senpi-desktop-engine/README.md +0 -31
  88. package/node_modules/@code-yeongyu/senpi-desktop-engine/dist/handshake.d.ts +0 -38
  89. package/node_modules/@code-yeongyu/senpi-desktop-engine/dist/handshake.d.ts.map +0 -1
  90. package/node_modules/@code-yeongyu/senpi-desktop-engine/dist/handshake.js +0 -117
  91. package/node_modules/@code-yeongyu/senpi-desktop-engine/dist/handshake.js.map +0 -1
  92. package/node_modules/@code-yeongyu/senpi-desktop-engine/dist/index.d.ts +0 -3
  93. package/node_modules/@code-yeongyu/senpi-desktop-engine/dist/index.d.ts.map +0 -1
  94. package/node_modules/@code-yeongyu/senpi-desktop-engine/dist/index.js +0 -3
  95. package/node_modules/@code-yeongyu/senpi-desktop-engine/dist/index.js.map +0 -1
  96. package/node_modules/@code-yeongyu/senpi-desktop-engine/dist/locator.d.ts +0 -42
  97. package/node_modules/@code-yeongyu/senpi-desktop-engine/dist/locator.d.ts.map +0 -1
  98. package/node_modules/@code-yeongyu/senpi-desktop-engine/dist/locator.js +0 -79
  99. package/node_modules/@code-yeongyu/senpi-desktop-engine/dist/locator.js.map +0 -1
  100. package/node_modules/@code-yeongyu/senpi-desktop-engine/native/check-prebuild-fresh.mjs +0 -160
  101. package/node_modules/@code-yeongyu/senpi-desktop-engine/native/index.d.ts +0 -20
  102. package/node_modules/@code-yeongyu/senpi-desktop-engine/native/index.js +0 -23
  103. package/node_modules/@code-yeongyu/senpi-desktop-engine/native/prebuilds/darwin-arm64/senpi-desktop-engine +0 -0
  104. package/node_modules/@code-yeongyu/senpi-desktop-engine/package.json +0 -53
  105. package/node_modules/@code-yeongyu/senpi-desktop-prelude/README.md +0 -20
  106. package/node_modules/@code-yeongyu/senpi-desktop-prelude/dist/index.d.ts +0 -2
  107. package/node_modules/@code-yeongyu/senpi-desktop-prelude/dist/index.d.ts.map +0 -1
  108. package/node_modules/@code-yeongyu/senpi-desktop-prelude/dist/index.js +0 -2
  109. package/node_modules/@code-yeongyu/senpi-desktop-prelude/dist/index.js.map +0 -1
  110. package/node_modules/@code-yeongyu/senpi-desktop-prelude/package.json +0 -44
  111. package/node_modules/@code-yeongyu/senpi-desktop-protocol/README.md +0 -20
  112. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/audit.d.ts +0 -38
  113. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/audit.d.ts.map +0 -1
  114. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/audit.js +0 -2
  115. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/audit.js.map +0 -1
  116. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/call.d.ts +0 -28
  117. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/call.d.ts.map +0 -1
  118. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/call.js +0 -102
  119. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/call.js.map +0 -1
  120. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/engine-schema.generated.d.ts +0 -14
  121. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/engine-schema.generated.d.ts.map +0 -1
  122. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/engine-schema.generated.js +0 -71
  123. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/engine-schema.generated.js.map +0 -1
  124. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/index.d.ts +0 -7
  125. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/index.d.ts.map +0 -1
  126. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/index.js +0 -4
  127. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/index.js.map +0 -1
  128. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/json-rpc.d.ts +0 -66
  129. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/json-rpc.d.ts.map +0 -1
  130. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/json-rpc.js +0 -8
  131. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/json-rpc.js.map +0 -1
  132. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/session.d.ts +0 -44
  133. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/session.d.ts.map +0 -1
  134. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/session.js +0 -2
  135. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/session.js.map +0 -1
  136. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/wire.d.ts +0 -59
  137. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/wire.d.ts.map +0 -1
  138. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/wire.js +0 -2
  139. package/node_modules/@code-yeongyu/senpi-desktop-protocol/dist/wire.js.map +0 -1
  140. package/node_modules/@code-yeongyu/senpi-desktop-protocol/package.json +0 -45
  141. package/node_modules/@code-yeongyu/senpi-desktop-service/README.md +0 -27
  142. package/node_modules/@code-yeongyu/senpi-desktop-service/dist/index.d.ts +0 -2
  143. package/node_modules/@code-yeongyu/senpi-desktop-service/dist/index.d.ts.map +0 -1
  144. package/node_modules/@code-yeongyu/senpi-desktop-service/dist/index.js +0 -2
  145. package/node_modules/@code-yeongyu/senpi-desktop-service/dist/index.js.map +0 -1
  146. package/node_modules/@code-yeongyu/senpi-desktop-service/package.json +0 -44
  147. package/node_modules/@code-yeongyu/senpi-desktop-tool/README.md +0 -20
  148. package/node_modules/@code-yeongyu/senpi-desktop-tool/dist/index.d.ts +0 -2
  149. package/node_modules/@code-yeongyu/senpi-desktop-tool/dist/index.d.ts.map +0 -1
  150. package/node_modules/@code-yeongyu/senpi-desktop-tool/dist/index.js +0 -2
  151. package/node_modules/@code-yeongyu/senpi-desktop-tool/dist/index.js.map +0 -1
  152. package/node_modules/@code-yeongyu/senpi-desktop-tool/package.json +0 -44
@@ -3,7 +3,7 @@ export function buildHandoffSection(options = {}) {
3
3
  ? "The Next you name is executed in this same response with tool calls."
4
4
  : "The Next you name is executed in this same response with tool calls; a Next with nothing after it is a defect.";
5
5
  const betweenRule = options.briefUpdatesBetweenHandoffs
6
- ? "Between handoffs, one line on what you just found, ending with Now: [task]. Next: [task]., helps the user follow along."
6
+ ? "Between handoffs, a one-line update on what you just found, ending with `Now: [task]. Next: [task].`, helps the user follow along."
7
7
  : "Between handoffs, work without narration.";
8
8
  return `## Handoff
9
9
 
@@ -1 +1 @@
1
- {"version":3,"file":"handoff.js","sourceRoot":"","sources":["../../../src/core/dynamic-prompt/handoff.ts"],"names":[],"mappings":"AAeA,MAAM,UAAU,mBAAmB,CAAC,OAAO,GAA0B,EAAE;IACtE,MAAM,QAAQ,GAAG,OAAO,CAAC,0BAA0B;QAClD,CAAC,CAAC,sEAAsE;QACxE,CAAC,CAAC,gHAAgH,CAAC;IACpH,MAAM,WAAW,GAAG,OAAO,CAAC,2BAA2B;QACtD,CAAC,CAAC,yHAAyH;QAC3H,CAAC,CAAC,2CAA2C,CAAC;IAC/C,OAAO;;;;;;6CAMqC,QAAQ,IAAI,WAAW,EAAE,CAAC;AACvE,CAAC","sourcesContent":["export interface HandoffSectionOptions {\n\t/**\n\t * The caller's core already states a text-only turn-end rule (\"check your last paragraph\"),\n\t * so the block drops its own \"a Next with nothing after it is a defect\" clause instead of\n\t * stating that rule twice.\n\t */\n\tturnEndRuleStatedElsewhere?: boolean;\n\t/**\n\t * The model under-reports during long tool chains by default (Claude Fable 5.1 guide, \"Ask for\n\t * user-facing progress updates\": remove narration-suppressing lines, then say when updates are\n\t * wanted), so the quiet-between-handoffs sentence becomes a brief-update sentence.\n\t */\n\tbriefUpdatesBetweenHandoffs?: boolean;\n}\n\nexport function buildHandoffSection(options: HandoffSectionOptions = {}): string {\n\tconst nextRule = options.turnEndRuleStatedElsewhere\n\t\t? \"The Next you name is executed in this same response with tool calls.\"\n\t\t: \"The Next you name is executed in this same response with tool calls; a Next with nothing after it is a defect.\";\n\tconst betweenRule = options.briefUpdatesBetweenHandoffs\n\t\t? \"Between handoffs, one line on what you just found, ending with Now: [task]. Next: [task]., helps the user follow along.\"\n\t\t: \"Between handoffs, work without narration.\";\n\treturn `## Handoff\n\nA handoff is the todo list's creation (in the message that creates it, after the routing line, or the next one), each todo phase change, a blocker or plan change, and the final message; the routing line is not one. Before writing one, weigh what the user originally asked for and what they would want to know right now; then state it in one short block:\n\n> Ask: [the user's original request] - wanted: [the outcome they asked for]. For you: [what they need to know now - ledger N/M done, findings, blockers]. Now: [the todo task in progress]. Next: [the next open task].\n\nNow and Next are the todo labels verbatim. ${nextRule} ${betweenRule}`;\n}\n"]}
1
+ {"version":3,"file":"handoff.js","sourceRoot":"","sources":["../../../src/core/dynamic-prompt/handoff.ts"],"names":[],"mappings":"AAeA,MAAM,UAAU,mBAAmB,CAAC,OAAO,GAA0B,EAAE;IACtE,MAAM,QAAQ,GAAG,OAAO,CAAC,0BAA0B;QAClD,CAAC,CAAC,sEAAsE;QACxE,CAAC,CAAC,gHAAgH,CAAC;IACpH,MAAM,WAAW,GAAG,OAAO,CAAC,2BAA2B;QACtD,CAAC,CAAC,oIAAoI;QACtI,CAAC,CAAC,2CAA2C,CAAC;IAC/C,OAAO;;;;;;6CAMqC,QAAQ,IAAI,WAAW,EAAE,CAAC;AACvE,CAAC","sourcesContent":["export interface HandoffSectionOptions {\n\t/**\n\t * The caller's core already states a text-only turn-end rule (\"check your last paragraph\"),\n\t * so the block drops its own \"a Next with nothing after it is a defect\" clause instead of\n\t * stating that rule twice.\n\t */\n\tturnEndRuleStatedElsewhere?: boolean;\n\t/**\n\t * The model under-reports during long tool chains by default (Claude Fable 5.1 guide, \"Ask for\n\t * user-facing progress updates\": remove narration-suppressing lines, then say when updates are\n\t * wanted), so the quiet-between-handoffs sentence becomes a brief-update sentence.\n\t */\n\tbriefUpdatesBetweenHandoffs?: boolean;\n}\n\nexport function buildHandoffSection(options: HandoffSectionOptions = {}): string {\n\tconst nextRule = options.turnEndRuleStatedElsewhere\n\t\t? \"The Next you name is executed in this same response with tool calls.\"\n\t\t: \"The Next you name is executed in this same response with tool calls; a Next with nothing after it is a defect.\";\n\tconst betweenRule = options.briefUpdatesBetweenHandoffs\n\t\t? \"Between handoffs, a one-line update on what you just found, ending with `Now: [task]. Next: [task].`, helps the user follow along.\"\n\t\t: \"Between handoffs, work without narration.\";\n\treturn `## Handoff\n\nA handoff is the todo list's creation (in the message that creates it, after the routing line, or the next one), each todo phase change, a blocker or plan change, and the final message; the routing line is not one. Before writing one, weigh what the user originally asked for and what they would want to know right now; then state it in one short block:\n\n> Ask: [the user's original request] - wanted: [the outcome they asked for]. For you: [what they need to know now - ledger N/M done, findings, blockers]. Now: [the todo task in progress]. Next: [the next open task].\n\nNow and Next are the todo labels verbatim. ${nextRule} ${betweenRule}`;\n}\n"]}
@@ -1,4 +1,4 @@
1
- export { ASK_TEXT_LIMIT, captureListAsk, truncateAskText } from "./todo-ask.ts";
1
+ export { captureListAsk } from "./todo-ask.ts";
2
2
  export { type AskNowNext, describeAskNowNext, formatAskNowNextHeader, formatHandoffCue, formatSummary, getTodoMarker, getTodoResultLines, HANDOFF_CUES, type HandoffMoment, handoffMomentOf, isIncompleteTodo, isTerminalTodoStatus, sanitizeTodoText, } from "./todo-format.ts";
3
3
  export { appendItems, applyEntry, applyOpsToPhases, applyParams, initPhases, removeTasks, } from "./todo-operations.ts";
4
4
  export { findPhaseByName, findTaskByContent, getCompletionTransitions, nextActionableTask, normalizeInProgressTask, } from "./todo-query.ts";
@@ -1 +1 @@
1
- {"version":3,"file":"state.d.ts","sourceRoot":"","sources":["../../../../../src/core/extensions/builtin/todotools/state.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAChF,OAAO,EACN,KAAK,UAAU,EACf,kBAAkB,EAClB,sBAAsB,EACtB,gBAAgB,EAChB,aAAa,EACb,aAAa,EACb,kBAAkB,EAClB,YAAY,EACZ,KAAK,aAAa,EAClB,eAAe,EACf,gBAAgB,EAChB,oBAAoB,EACpB,gBAAgB,GAChB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACN,WAAW,EACX,UAAU,EACV,gBAAgB,EAChB,WAAW,EACX,UAAU,EACV,WAAW,GACX,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EACN,eAAe,EACf,iBAAiB,EACjB,wBAAwB,EACxB,kBAAkB,EAClB,uBAAuB,GACvB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,cAAc,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC/F,OAAO,EACN,WAAW,EACX,SAAS,EACT,gCAAgC,EAChC,mCAAmC,EACnC,+BAA+B,EAC/B,SAAS,EACT,UAAU,EACV,eAAe,EACf,WAAW,EACX,gBAAgB,GAChB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACN,kBAAkB,EAClB,yBAAyB,EACzB,qBAAqB,EACrB,KAAK,OAAO,EACZ,KAAK,wBAAwB,EAC7B,KAAK,QAAQ,EACb,KAAK,WAAW,EAChB,KAAK,aAAa,EAClB,KAAK,SAAS,EACd,KAAK,SAAS,EACd,KAAK,cAAc,EACnB,KAAK,UAAU,EACf,KAAK,eAAe,GACpB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACN,kBAAkB,EAClB,kBAAkB,EAClB,KAAK,eAAe,EACpB,KAAK,aAAa,GAClB,MAAM,kBAAkB,CAAC"}
1
+ {"version":3,"file":"state.d.ts","sourceRoot":"","sources":["../../../../../src/core/extensions/builtin/todotools/state.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,EACN,KAAK,UAAU,EACf,kBAAkB,EAClB,sBAAsB,EACtB,gBAAgB,EAChB,aAAa,EACb,aAAa,EACb,kBAAkB,EAClB,YAAY,EACZ,KAAK,aAAa,EAClB,eAAe,EACf,gBAAgB,EAChB,oBAAoB,EACpB,gBAAgB,GAChB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACN,WAAW,EACX,UAAU,EACV,gBAAgB,EAChB,WAAW,EACX,UAAU,EACV,WAAW,GACX,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EACN,eAAe,EACf,iBAAiB,EACjB,wBAAwB,EACxB,kBAAkB,EAClB,uBAAuB,GACvB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,cAAc,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC/F,OAAO,EACN,WAAW,EACX,SAAS,EACT,gCAAgC,EAChC,mCAAmC,EACnC,+BAA+B,EAC/B,SAAS,EACT,UAAU,EACV,eAAe,EACf,WAAW,EACX,gBAAgB,GAChB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACN,kBAAkB,EAClB,yBAAyB,EACzB,qBAAqB,EACrB,KAAK,OAAO,EACZ,KAAK,wBAAwB,EAC7B,KAAK,QAAQ,EACb,KAAK,WAAW,EAChB,KAAK,aAAa,EAClB,KAAK,SAAS,EACd,KAAK,SAAS,EACd,KAAK,cAAc,EACnB,KAAK,UAAU,EACf,KAAK,eAAe,GACpB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACN,kBAAkB,EAClB,kBAAkB,EAClB,KAAK,eAAe,EACpB,KAAK,aAAa,GAClB,MAAM,kBAAkB,CAAC"}
@@ -2,7 +2,7 @@
2
2
  // Copyright (c) 2025 Mario Zechner
3
3
  // Copyright (c) 2025-2026 Can Bölük
4
4
  // https://github.com/can1357/oh-my-pi
5
- export { ASK_TEXT_LIMIT, captureListAsk, truncateAskText } from "./todo-ask.js";
5
+ export { captureListAsk } from "./todo-ask.js";
6
6
  export { describeAskNowNext, formatAskNowNextHeader, formatHandoffCue, formatSummary, getTodoMarker, getTodoResultLines, HANDOFF_CUES, handoffMomentOf, isIncompleteTodo, isTerminalTodoStatus, sanitizeTodoText, } from "./todo-format.js";
7
7
  export { appendItems, applyEntry, applyOpsToPhases, applyParams, initPhases, removeTasks, } from "./todo-operations.js";
8
8
  export { findPhaseByName, findTaskByContent, getCompletionTransitions, nextActionableTask, normalizeInProgressTask, } from "./todo-query.js";
@@ -1 +1 @@
1
- {"version":3,"file":"state.js","sourceRoot":"","sources":["../../../../../src/core/extensions/builtin/todotools/state.ts"],"names":[],"mappings":"AAAA,8DAA8D;AAC9D,mCAAmC;AACnC,oCAAoC;AACpC,sCAAsC;AAEtC,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAChF,OAAO,EAEN,kBAAkB,EAClB,sBAAsB,EACtB,gBAAgB,EAChB,aAAa,EACb,aAAa,EACb,kBAAkB,EAClB,YAAY,EAEZ,eAAe,EACf,gBAAgB,EAChB,oBAAoB,EACpB,gBAAgB,GAChB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACN,WAAW,EACX,UAAU,EACV,gBAAgB,EAChB,WAAW,EACX,UAAU,EACV,WAAW,GACX,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EACN,eAAe,EACf,iBAAiB,EACjB,wBAAwB,EACxB,kBAAkB,EAClB,uBAAuB,GACvB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,cAAc,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC/F,OAAO,EACN,WAAW,EACX,SAAS,EACT,gCAAgC,EAChC,mCAAmC,EACnC,+BAA+B,EAC/B,SAAS,EACT,UAAU,EACV,eAAe,EACf,WAAW,EACX,gBAAgB,GAChB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACN,kBAAkB,EAClB,yBAAyB,EACzB,qBAAqB,GAWrB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACN,kBAAkB,EAClB,kBAAkB,GAGlB,MAAM,kBAAkB,CAAC","sourcesContent":["// Ported and adapted from oh-my-pi's todo tool (MIT License).\n// Copyright (c) 2025 Mario Zechner\n// Copyright (c) 2025-2026 Can Bölük\n// https://github.com/can1357/oh-my-pi\n\nexport { ASK_TEXT_LIMIT, captureListAsk, truncateAskText } from \"./todo-ask.ts\";\nexport {\n\ttype AskNowNext,\n\tdescribeAskNowNext,\n\tformatAskNowNextHeader,\n\tformatHandoffCue,\n\tformatSummary,\n\tgetTodoMarker,\n\tgetTodoResultLines,\n\tHANDOFF_CUES,\n\ttype HandoffMoment,\n\thandoffMomentOf,\n\tisIncompleteTodo,\n\tisTerminalTodoStatus,\n\tsanitizeTodoText,\n} from \"./todo-format.ts\";\nexport {\n\tappendItems,\n\tapplyEntry,\n\tapplyOpsToPhases,\n\tapplyParams,\n\tinitPhases,\n\tremoveTasks,\n} from \"./todo-operations.ts\";\nexport {\n\tfindPhaseByName,\n\tfindTaskByContent,\n\tgetCompletionTransitions,\n\tnextActionableTask,\n\tnormalizeInProgressTask,\n} from \"./todo-query.ts\";\nexport { getTaskTargets, resolvePhaseOrError, resolveTaskOrError } from \"./todo-resolution.ts\";\nexport {\n\tclonePhases,\n\tcloneTask,\n\tgetLatestPhasesFromBranchEntries,\n\tgetLatestTodoStateFromBranchEntries,\n\tgetLatestTodosFromBranchEntries,\n\tisTodoAsk,\n\tisTodoItem,\n\tisTodoItemArray,\n\tisTodoPhase,\n\tisTodoPhaseArray,\n} from \"./todo-storage.ts\";\nexport {\n\tDEFAULT_INIT_PHASE,\n\tTODO_RESTORE_REQUEST_TYPE,\n\tTODO_STATE_ENTRY_TYPE,\n\ttype TodoAsk,\n\ttype TodoCompletionTransition,\n\ttype TodoItem,\n\ttype TodoOpEntry,\n\ttype TodoOperation,\n\ttype TodoPhase,\n\ttype TodoState,\n\ttype TodoStateEntry,\n\ttype TodoStatus,\n\ttype TodoToolDetails,\n} from \"./todo-types.ts\";\nexport {\n\tgetTodoWidgetLines,\n\tgetTodoWidgetModel,\n\ttype TodoWidgetModel,\n\ttype TodoWidgetRow,\n} from \"./todo-widget.ts\";\n"]}
1
+ {"version":3,"file":"state.js","sourceRoot":"","sources":["../../../../../src/core/extensions/builtin/todotools/state.ts"],"names":[],"mappings":"AAAA,8DAA8D;AAC9D,mCAAmC;AACnC,oCAAoC;AACpC,sCAAsC;AAEtC,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,EAEN,kBAAkB,EAClB,sBAAsB,EACtB,gBAAgB,EAChB,aAAa,EACb,aAAa,EACb,kBAAkB,EAClB,YAAY,EAEZ,eAAe,EACf,gBAAgB,EAChB,oBAAoB,EACpB,gBAAgB,GAChB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACN,WAAW,EACX,UAAU,EACV,gBAAgB,EAChB,WAAW,EACX,UAAU,EACV,WAAW,GACX,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EACN,eAAe,EACf,iBAAiB,EACjB,wBAAwB,EACxB,kBAAkB,EAClB,uBAAuB,GACvB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,cAAc,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC/F,OAAO,EACN,WAAW,EACX,SAAS,EACT,gCAAgC,EAChC,mCAAmC,EACnC,+BAA+B,EAC/B,SAAS,EACT,UAAU,EACV,eAAe,EACf,WAAW,EACX,gBAAgB,GAChB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACN,kBAAkB,EAClB,yBAAyB,EACzB,qBAAqB,GAWrB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACN,kBAAkB,EAClB,kBAAkB,GAGlB,MAAM,kBAAkB,CAAC","sourcesContent":["// Ported and adapted from oh-my-pi's todo tool (MIT License).\n// Copyright (c) 2025 Mario Zechner\n// Copyright (c) 2025-2026 Can Bölük\n// https://github.com/can1357/oh-my-pi\n\nexport { captureListAsk } from \"./todo-ask.ts\";\nexport {\n\ttype AskNowNext,\n\tdescribeAskNowNext,\n\tformatAskNowNextHeader,\n\tformatHandoffCue,\n\tformatSummary,\n\tgetTodoMarker,\n\tgetTodoResultLines,\n\tHANDOFF_CUES,\n\ttype HandoffMoment,\n\thandoffMomentOf,\n\tisIncompleteTodo,\n\tisTerminalTodoStatus,\n\tsanitizeTodoText,\n} from \"./todo-format.ts\";\nexport {\n\tappendItems,\n\tapplyEntry,\n\tapplyOpsToPhases,\n\tapplyParams,\n\tinitPhases,\n\tremoveTasks,\n} from \"./todo-operations.ts\";\nexport {\n\tfindPhaseByName,\n\tfindTaskByContent,\n\tgetCompletionTransitions,\n\tnextActionableTask,\n\tnormalizeInProgressTask,\n} from \"./todo-query.ts\";\nexport { getTaskTargets, resolvePhaseOrError, resolveTaskOrError } from \"./todo-resolution.ts\";\nexport {\n\tclonePhases,\n\tcloneTask,\n\tgetLatestPhasesFromBranchEntries,\n\tgetLatestTodoStateFromBranchEntries,\n\tgetLatestTodosFromBranchEntries,\n\tisTodoAsk,\n\tisTodoItem,\n\tisTodoItemArray,\n\tisTodoPhase,\n\tisTodoPhaseArray,\n} from \"./todo-storage.ts\";\nexport {\n\tDEFAULT_INIT_PHASE,\n\tTODO_RESTORE_REQUEST_TYPE,\n\tTODO_STATE_ENTRY_TYPE,\n\ttype TodoAsk,\n\ttype TodoCompletionTransition,\n\ttype TodoItem,\n\ttype TodoOpEntry,\n\ttype TodoOperation,\n\ttype TodoPhase,\n\ttype TodoState,\n\ttype TodoStateEntry,\n\ttype TodoStatus,\n\ttype TodoToolDetails,\n} from \"./todo-types.ts\";\nexport {\n\tgetTodoWidgetLines,\n\tgetTodoWidgetModel,\n\ttype TodoWidgetModel,\n\ttype TodoWidgetRow,\n} from \"./todo-widget.ts\";\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"repetitive-turns.d.ts","sourceRoot":"","sources":["../../../../../../src/core/extensions/builtin/ttsr/detectors/repetitive-turns.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAEjD,eAAO,MAAM,0BAA0B,qBAAqB,CAAC;AAE7D,eAAO,MAAM,qCAAqC,OAAO,CAAC;AAC1D,eAAO,MAAM,8BAA8B,IAAI,CAAC;AAChD,eAA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,GAAG,MAAM,CAS3D;AAED,wBAAgB,6BAA6B,CAAC,mBAAmB,EAAE,MAAM,EAAE,kBAAkB,EAAE,MAAM,GAAG,OAAO,CAE9G;AAED,UAAU,SAAS;IAClB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,KAAK,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;CAC5B;AAED,MAAM,WAAW,oBAAoB;IACpC,QAAQ,CAAC,OAAO,EAAE,SAAS,EAAE,CAAC;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,OAAO,CAAC;CACjB;AAED,wBAAgB,0BAA0B,IAAI,oBAAoB,CAEjE;AAWD,wBAAgB,cAAc,CAAC,KAAK,EAAE,oBAAoB,EAAE,IAAI,EAAE,MAAM,GAAG,aAAa,GAAG,IAAI,CAkC9F"}
@@ -9,7 +9,9 @@ const HEX_LIKE_PATTERN = /\b[0-9a-f]{7,}\b/gi;
9
9
  const SAMPLE_LENGTH = 80;
10
10
  // The handoff contract restates the user's request verbatim after `Ask:` in every block, so that clause is
11
11
  // identical by design across turns; it ends where the block's own status starts (`For you:` / `You need:` / `Now:`).
12
- const HANDOFF_ASK_CLAUSE = /\bAsk:[\s\S]*?(?=\b(?:For you|You need|Now):|$)/g;
12
+ // Only a clause that such a label closes is a handoff Ask; any other `Ask:` stays in the text, so a looping turn
13
+ // that merely contains the word keeps its full repetition signal (senpi#2143).
14
+ const HANDOFF_ASK_CLAUSE = /\bAsk:[\s\S]*?(?=\b(?:For you|You need|Now):)/g;
13
15
  export function normalizeTurnText(text) {
14
16
  return text
15
17
  .replace(HANDOFF_ASK_CLAUSE, " ")
@@ -1 +1 @@
1
- {"version":3,"file":"repetitive-turns.js","sourceRoot":"","sources":["../../../../../../src/core/extensions/builtin/ttsr/detectors/repetitive-turns.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,0BAA0B,GAAG,kBAAkB,CAAC;AAE7D,MAAM,CAAC,MAAM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sD,KAAK,CAAC,MAAM,GAAG,CAAC,+BAA+B,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG;QACrI,kBAAkB,EAAE,CAAC;QACrB,kBAAkB,EAAE,CAAC;QACrB,MAAM,EAAE;YACP,SAAS,EAAE,YAAY;YACvB,MAAM,EAAE,KAAK,CAAC,MAAM,GAAG,CAAC;YACxB,UAAU,EAAE,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;YACzC,MAAM,EAAE,UAAU,CAAC,KAAK,CAAC,CAAC,EAAE,aAAa,CAAC;SAC1C;KACD,CAAC;AACH,CAAC","sourcesContent":["import type { DetectorMatch } from \"../types.ts\";\n\nexport const REPETITIVE_TURNS_RULE_NAME = \"repetitive-turns\";\n\nexport const REPETITIVE_TURNS_SIMILARITY_THRESHOLD = 0.55;\nexport const REPETITIVE_TURNS_STREAK_LENGTH = 3;\nexport const REPETITIVE_TURNS_MIN_NORMALIZED_CHARS = 40;\nexport const REPETITIVE_TURNS_HISTORY_CAPACITY = 6;\n\nconst WORD_PATTERN = /[\\p{L}\\p{N}#]+/gu;\nconst DIGIT_RUN_PATTERN = /\\d[\\d.,:/-]*/g;\nconst HEX_LIKE_PATTERN = /\\b[0-9a-f]{7,}\\b/gi;\nconst SAMPLE_LENGTH = 80;\n// The handoff contract restates the user's request verbatim after `Ask:` in every block, so that clause is\n// identical by design across turns; it ends where the block's own status starts (`For you:` / `You need:` / `Now:`).\nconst HANDOFF_ASK_CLAUSE = /\\bAsk:[\\s\\S]*?(?=\\b(?:For you|You need|Now):|$)/g;\n\nexport function normalizeTurnText(text: string): string {\n\treturn text\n\t\t.replace(HANDOFF_ASK_CLAUSE, \" \")\n\t\t.toLowerCase()\n\t\t.replace(HEX_LIKE_PATTERN, \"#\")\n\t\t.replace(DIGIT_RUN_PATTERN, \"#\")\n\t\t.split(/\\s+/)\n\t\t.filter((token) => token.length > 0)\n\t\t.join(\" \")\n\t\t.trim();\n}\n\nfunction wordTrigrams(normalized: string): Set<string> {\n\tconst words = normalized.match(WORD_PATTERN) ?? [];\n\tconst grams = new Set<string>();\n\tfor (let i = 0; i + 3 <= words.length; i++) {\n\t\tgrams.add(`${words[i]} ${words[i + 1]} ${words[i + 2]}`);\n\t}\n\treturn grams;\n}\n\nexport function trigramJaccard(a: string, b: string): number {\n\tconst gramsA = wordTrigrams(a);\n\tconst gramsB = wordTrigrams(b);\n\tif (gramsA.size === 0 || gramsB.size === 0) return 0;\n\tlet intersection = 0;\n\tfor (const gram of gramsA) {\n\t\tif (gramsB.has(gram)) intersection += 1;\n\t}\n\treturn intersection / (gramsA.size + gramsB.size - intersection);\n}\n\nexport function isNearDuplicateOfPreviousTurn(normalizedCandidate: string, normalizedPrevious: string): boolean {\n\treturn trigramJaccard(normalizedCandidate, normalizedPrevious) >= REPETITIVE_TURNS_SIMILARITY_THRESHOLD;\n}\n\ninterface TurnEntry {\n\treadonly normalized: string;\n\treadonly grams: Set<string>;\n}\n\nexport interface RepetitiveTurnsState {\n\treadonly history: TurnEntry[];\n\tstreak: number;\n\tlatched: boolean;\n}\n\nexport function createRepetitiveTurnsState(): RepetitiveTurnsState {\n\treturn { history: [], streak: 0, latched: false };\n}\n\nfunction jaccardSets(a: Set<string>, b: Set<string>): number {\n\tif (a.size === 0 || b.size === 0) return 0;\n\tlet intersection = 0;\n\tfor (const gram of a) {\n\t\tif (b.has(gram)) intersection += 1;\n\t}\n\treturn intersection / (a.size + b.size - intersection);\n}\n\nexport function recordTurnText(state: RepetitiveTurnsState, text: string): DetectorMatch | null {\n\tconst normalized = normalizeTurnText(text);\n\tif (normalized.length < REPETITIVE_TURNS_MIN_NORMALIZED_CHARS) {\n\t\treturn null;\n\t}\n\tconst grams = wordTrigrams(normalized);\n\tconst previous = state.history[state.history.length - 1];\n\tconst similarity = previous === undefined ? 0 : jaccardSets(grams, previous.grams);\n\tstate.history.push({ normalized, grams });\n\tif (state.history.length > REPETITIVE_TURNS_HISTORY_CAPACITY) {\n\t\tstate.history.shift();\n\t}\n\tif (similarity >= REPETITIVE_TURNS_SIMILARITY_THRESHOLD) {\n\t\tstate.streak += 1;\n\t} else {\n\t\tstate.streak = 0;\n\t\tif (state.latched) state.latched = false;\n\t}\n\tif (state.latched || state.streak < REPETITIVE_TURNS_STREAK_LENGTH - 1) {\n\t\treturn null;\n\t}\n\tstate.latched = true;\n\treturn {\n\t\trule: REPETITIVE_TURNS_RULE_NAME,\n\t\treason: `assistant repeated a near-identical message across ${state.streak + 1} consecutive turns (jaccard ${similarity.toFixed(2)})`,\n\t\tanomalyStartOffset: 0,\n\t\tgarbageStartOffset: 0,\n\t\tdetail: {\n\t\t\tmechanism: \"cross-turn\",\n\t\t\tstreak: state.streak + 1,\n\t\t\tsimilarity: Number(similarity.toFixed(3)),\n\t\t\tsample: normalized.slice(0, SAMPLE_LENGTH),\n\t\t},\n\t};\n}\n"]}
1
+ {"version":3,"file":"repetitive-turns.js","sourceRoot":"","sources":["../../../../../../src/core/extensions/builtin/ttsr/detectors/repetitive-turns.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,0BAA0B,GAAG,kBAAkB,CAAC;AAE7D,MAAM,CAAC,MAAM,qCAAqC,GAAG,IAAI,CAAC;AAC1D,MAAM,CAAC,MAAM,8BAA8B,GAAG,CAAC,CAAC;AAChD,MAAM,CAAC,MAAM,qCAAqC,GAAG,EAAE,CAAC;AACxD,MAAM,CAAC,MAAM,iCAAiC,GAAG,CAAC,CAAC;AAEnD,MAAM,YAAY,GAAG,kBAAkB,CAAC;AACxC,MAAM,iBAAiB,GAAG,eAAe,CAAC;AAC1C,MAAM,gBAAgB,GAAG,oBAAoB,CAAC;AAC9C,MAAM,aAAa,GAAG,EAAE,CAAC;AACzB,2GAA2G;AAC3G,qHAAqH;AACrH,iHAAiH;AACjH,+EAA+E;AAC/E,MAAM,kBAAkB,GAAG,gDAAgD,CAAC;AAE5E,MAAM,UAAU,iBAAiB,CAAC,IAAY;IAC7C,OAAO,IAAI;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ive-turns\";\n\nexport const REPETITIVE_TURNS_SIMILARITY_THRESHOLD = 0.55;\nexport const REPETITIVE_TURNS_STREAK_LENGTH = 3;\nexport const REPETITIVE_TURNS_MIN_NORMALIZED_CHARS = 40;\nexport const REPETITIVE_TURNS_HISTORY_CAPACITY = 6;\n\nconst WORD_PATTERN = /[\\p{L}\\p{N}#]+/gu;\nconst DIGIT_RUN_PATTERN = /\\d[\\d.,:/-]*/g;\nconst HEX_LIKE_PATTERN = /\\b[0-9a-f]{7,}\\b/gi;\nconst SAMPLE_LENGTH = 80;\n// The handoff contract restates the user's request verbatim after `Ask:` in every block, so that clause is\n// identical by design across turns; it ends where the block's own status starts (`For you:` / `You need:` / `Now:`).\n// Only a clause that such a label closes is a handoff Ask; any other `Ask:` stays in the text, so a looping turn\n// that merely contains the word keeps its full repetition signal (senpi#2143).\nconst HANDOFF_ASK_CLAUSE = /\\bAsk:[\\s\\S]*?(?=\\b(?:For you|You need|Now):)/g;\n\nexport function normalizeTurnText(text: string): string {\n\treturn text\n\t\t.replace(HANDOFF_ASK_CLAUSE, \" \")\n\t\t.toLowerCase()\n\t\t.replace(HEX_LIKE_PATTERN, \"#\")\n\t\t.replace(DIGIT_RUN_PATTERN, \"#\")\n\t\t.split(/\\s+/)\n\t\t.filter((token) => token.length > 0)\n\t\t.join(\" \")\n\t\t.trim();\n}\n\nfunction wordTrigrams(normalized: string): Set<string> {\n\tconst words = normalized.match(WORD_PATTERN) ?? [];\n\tconst grams = new Set<string>();\n\tfor (let i = 0; i + 3 <= words.length; i++) {\n\t\tgrams.add(`${words[i]} ${words[i + 1]} ${words[i + 2]}`);\n\t}\n\treturn grams;\n}\n\nexport function trigramJaccard(a: string, b: string): number {\n\tconst gramsA = wordTrigrams(a);\n\tconst gramsB = wordTrigrams(b);\n\tif (gramsA.size === 0 || gramsB.size === 0) return 0;\n\tlet intersection = 0;\n\tfor (const gram of gramsA) {\n\t\tif (gramsB.has(gram)) intersection += 1;\n\t}\n\treturn intersection / (gramsA.size + gramsB.size - intersection);\n}\n\nexport function isNearDuplicateOfPreviousTurn(normalizedCandidate: string, normalizedPrevious: string): boolean {\n\treturn trigramJaccard(normalizedCandidate, normalizedPrevious) >= REPETITIVE_TURNS_SIMILARITY_THRESHOLD;\n}\n\ninterface TurnEntry {\n\treadonly normalized: string;\n\treadonly grams: Set<string>;\n}\n\nexport interface RepetitiveTurnsState {\n\treadonly history: TurnEntry[];\n\tstreak: number;\n\tlatched: boolean;\n}\n\nexport function createRepetitiveTurnsState(): RepetitiveTurnsState {\n\treturn { history: [], streak: 0, latched: false };\n}\n\nfunction jaccardSets(a: Set<string>, b: Set<string>): number {\n\tif (a.size === 0 || b.size === 0) return 0;\n\tlet intersection = 0;\n\tfor (const gram of a) {\n\t\tif (b.has(gram)) intersection += 1;\n\t}\n\treturn intersection / (a.size + b.size - intersection);\n}\n\nexport function recordTurnText(state: RepetitiveTurnsState, text: string): DetectorMatch | null {\n\tconst normalized = normalizeTurnText(text);\n\tif (normalized.length < REPETITIVE_TURNS_MIN_NORMALIZED_CHARS) {\n\t\treturn null;\n\t}\n\tconst grams = wordTrigrams(normalized);\n\tconst previous = state.history[state.history.length - 1];\n\tconst similarity = previous === undefined ? 0 : jaccardSets(grams, previous.grams);\n\tstate.history.push({ normalized, grams });\n\tif (state.history.length > REPETITIVE_TURNS_HISTORY_CAPACITY) {\n\t\tstate.history.shift();\n\t}\n\tif (similarity >= REPETITIVE_TURNS_SIMILARITY_THRESHOLD) {\n\t\tstate.streak += 1;\n\t} else {\n\t\tstate.streak = 0;\n\t\tif (state.latched) state.latched = false;\n\t}\n\tif (state.latched || state.streak < REPETITIVE_TURNS_STREAK_LENGTH - 1) {\n\t\treturn null;\n\t}\n\tstate.latched = true;\n\treturn {\n\t\trule: REPETITIVE_TURNS_RULE_NAME,\n\t\treason: `assistant repeated a near-identical message across ${state.streak + 1} consecutive turns (jaccard ${similarity.toFixed(2)})`,\n\t\tanomalyStartOffset: 0,\n\t\tgarbageStartOffset: 0,\n\t\tdetail: {\n\t\t\tmechanism: \"cross-turn\",\n\t\t\tstreak: state.streak + 1,\n\t\t\tsimilarity: Number(similarity.toFixed(3)),\n\t\t\tsample: normalized.slice(0, SAMPLE_LENGTH),\n\t\t},\n\t};\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"bun-extension-importer.d.ts","sourceRoot":"","sources":["../../../src/core/extensions/bun-extension-importer.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAC;AAYxE,iFAAiF;AACjF,wBAAgB,0BAA0B,CACzC,cAAc,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;IA6HpE,MAAM,OAAO,MAAM,YAAY;QAAE,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAA;KAAE,GAAG,OAAO,CAAC,OAAO,CAAC;IAclF,aAAa,IAAI,SAAS,MAAM,EAAE;IAGlC,OAAO;EAQR"}
1
+ {"version":3,"file":"bun-extension-importer.d.ts","sourceRoot":"","sources":["../../../src/core/extensions/bun-extension-importer.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAC;AAYxE,iFAAiF;AACjF,wBAAgB,0BAA0B,CACzC,cAAc,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;IAiIpE,MAAM,OAAO,MAAM,YAAY;QAAE,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAA;KAAE,GAAG,OAAO,CAAC,OAAO,CAAC;IAiBlF,aAAa,IAAI,SAAS,MAAM,EAAE;IAGlC,OAAO;EAQR"}
@@ -6,7 +6,7 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
6
6
  }
7
7
  return path;
8
8
  };
9
- import { readFileSync, realpathSync } from "node:fs";
9
+ import { readFileSync, realpathSync, statSync } from "node:fs";
10
10
  import { createRequire, isBuiltin } from "node:module";
11
11
  import { dirname, extname, resolve } from "node:path";
12
12
  import { fileURLToPath } from "node:url";
@@ -30,6 +30,10 @@ export function createBunExtensionImporter(virtualModules) {
30
30
  throw new ExtensionGenerationDisposedError(registration.generation);
31
31
  };
32
32
  const moduleId = (filename) => `${extensionNamespace}:${registration.generation}/${encodeURIComponent(filename)}`;
33
+ // Only source the graph transpiles gets a graph id. Anything else (JSON, TOML, text, native
34
+ // addons) keeps its real path so Bun's own loader handles it: Bun 1.3.x cannot follow a
35
+ // plugin's hand-back to the `file` namespace from a runtime import() or require() (#2164).
36
+ const fileTarget = (resolved) => /\.[cm]?[jt]sx?$/.test(resolved) ? { id: moduleId(resolved), path: resolved } : { id: resolved };
33
37
  const resolveTarget = (specifier, filename) => {
34
38
  assertActive();
35
39
  if (Object.hasOwn(virtualModules, specifier) || isBuiltin(specifier) || specifier.startsWith("bun:"))
@@ -37,8 +41,7 @@ export function createBunExtensionImporter(virtualModules) {
37
41
  if (specifier.startsWith(`${extensionNamespace}:`))
38
42
  return { id: specifier };
39
43
  const path = specifier.startsWith("file:") ? fileURLToPath(specifier) : specifier;
40
- const resolved = realpathSync(Bun.resolveSync(path, dirname(filename)));
41
- return { id: moduleId(resolved), path: resolved };
44
+ return fileTarget(realpathSync(Bun.resolveSync(path, dirname(filename))));
42
45
  };
43
46
  const graph = {
44
47
  assertActive,
@@ -150,7 +153,10 @@ export function createBunExtensionImporter(virtualModules) {
150
153
  return {
151
154
  async import(path, _options) {
152
155
  assertActive();
153
- const id = moduleId(realpathSync(resolve(path)));
156
+ const absolute = realpathSync(resolve(path));
157
+ // A package directory loads through its main or index file, as a native import does.
158
+ const entry = statSync(absolute).isDirectory() ? realpathSync(Bun.resolveSync(absolute, absolute)) : absolute;
159
+ const { id } = fileTarget(entry);
154
160
  const module = await import(__rewriteRelativeImportExtension(id));
155
161
  const factory = module.default;
156
162
  if (typeof factory !== "function")
@@ -1 +1 @@
1
- {"version":3,"file":"bun-extension-importer.js","sourceRoot":"","sources":["../../../src/core/extensions/bun-extension-importer.ts"],"names":[],"mappings":";;;;;;;;AAAA,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACrD,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACvD,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,cAAc,EAAE,qBAAqB,EAAE,MAAM,6BAA6B,CAAC;AACpF,OAAO,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAChE,OAAO,EAGN,gCAAgC,EAChC,kBAAkB,EAElB,sBAAsB,GACtB,MAAM,6BAA6B,CAAC;AAErC,OAAO,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAC;AAYxE,iFAAiF;AACjF,MAAM,UAAU,0BAA0B,CACzC,cAA2E;IAE3E,MAAM,OAAO,GAAG,IAAI,GAAG,EAAwB,CAAC;IAChD,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAU,CAAC;IACnC,yFAAyF;IACzF,2FAA2F;IAC3F,oGAAoG;IACpG,MAAM,eAAe,GAAG,IAAI,GAAG,EAA0B,CAAC;IAC1D,MAAM,aAAa,GAAG,aAAa,CAAC,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC;IACrD,IAAI,MAAM,GAAG,IAAI,CAAC;IAClB,MAAM,YAAY,GAAG,GAAG,EAAE;QACzB,IAAI,CAAC,MAAM;YAAE,MAAM,IAAI,gCAAgC,CAAC,YAAY,CAAC,UAAU,CAAC,CAAC;IAClF,CAAC,CAAC;IACF,MAAM,QAAQ,GAAG,CAAC,QAAgB,EAAE,EAAE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AAC,CAAC;YACjD,MAAM,MAAM,GAAmC,MAAM,MAAM,kCAAC,EAAE,EAAC,CAAC;YAChE,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;YAC/B,IAAI,OAAO,OAAO,KAAK,UAAU;gBAAE,OAAO,OAAO,CAAC;YAClD,mEAAmE;YACnE,mEAAmE;YACnE,OAAO,UAAyB,GAAG,IAAe;gBACjD,KAAK,CAAC,YAAY,EAAE,CAAC;gBACrB,OAAO,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;YAClC,CAAC,CAAC;QACH,CAAC;QACD,iFAAiF;QACjF,aAAa;YACZ,OAAO,CAAC,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC;QAC5B,CAAC;QACD,OAAO;YACN,MAAM,GAAG,KAAK,CAAC;YACf,YAAY,CAAC,OAAO,EAAE,CAAC;YACvB,OAAO,CAAC,KAAK,EAAE,CAAC;YAChB,QAAQ,CAAC,KAAK,EAAE,CAAC;YACjB,eAAe,CAAC,KAAK,EAAE,CAAC;QACzB,CAAC;KACD,CAAC;AACH,CAAC","sourcesContent":["import { readFileSync, realpathSync } from \"node:fs\";\nimport { createRequire, isBuiltin } from \"node:module\";\nimport { dirname, extname, resolve } from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\nimport { parse } from \"es-module-lexer/js\";\nimport { isCommonJsFile, rewriteCommonJsImport } from \"./bun-extension-commonjs.ts\";\nimport { ExtensionSourceError } from \"./bun-extension-error.ts\";\nimport {\n\ttype CommonJsBody,\n\ttype CommonJsModule,\n\tExtensionGenerationDisposedError,\n\textensionNamespace,\n\ttype ModuleSource,\n\tregisterExtensionGraph,\n} from \"./bun-extension-registry.ts\";\n\nexport { bunExtensionImporterStats } from \"./bun-extension-registry.ts\";\n\n// Keep the source/Node build independent of Bun's ambient type declarations.\ndeclare const Bun: {\n\tresolveSync(specifier: string, directory: string): string;\n\tTranspiler: new (options: {\n\t\treadonly loader: \"ts\" | \"tsx\" | \"jsx\";\n\t\treadonly target: \"bun\";\n\t\treadonly define: Readonly<Record<string, string>>;\n\t}) => { transformSync(source: string): string };\n};\n\n/** A generation owns only source bookkeeping; Bun owns evaluation and cycles. */\nexport function createBunExtensionImporter(\n\tvirtualModules: Readonly<Record<string, Readonly<Record<string, unknown>>>>,\n) {\n\tconst sources = new Map<string, ModuleSource>();\n\tconst commonJs = new Set<string>();\n\t// Live `module` objects by id, registered while a CommonJS body runs: a require inside a\n\t// cycle receives the partially built exports, as in Node, instead of an unset ESM default.\n\t// A body that throws is evicted so a later require re-throws instead of seeing a half-built module.\n\tconst commonJsModules = new Map<string, CommonJsModule>();\n\tconst nativeRequire = createRequire(import.meta.url);\n\tlet active = true;\n\tconst assertActive = () => {\n\t\tif (!active) throw new ExtensionGenerationDisposedError(registration.generation);\n\t};\n\tconst moduleId = (filename: string) =>\n\t\t`${extensionNamespace}:${registration.generation}/${encodeURIComponent(filename)}`;\n\tconst resolveTarget = (specifier: string, filename: string): { readonly id: string; readonly path?: string } => {\n\t\tassertActive();\n\t\tif (Object.hasOwn(virtualModules, specifier) || isBuiltin(specifier) || specifier.startsWith(\"bun:\"))\n\t\t\treturn { id: specifier };\n\t\tif (specifier.startsWith(`${extensionNamespace}:`)) return { id: specifier };\n\t\tconst path = specifier.startsWith(\"file:\") ? fileURLToPath(specifier) : specifier;\n\t\tconst resolved = realpathSync(Bun.resolveSync(path, dirname(filename)));\n\t\treturn { id: moduleId(resolved), path: resolved };\n\t};\n\tconst graph = {\n\t\tassertActive,\n\t\tresolve(specifier: string, filename: string): string {\n\t\t\treturn resolveTarget(specifier, filename).id;\n\t\t},\n\t\trequire(specifier: string, filename: string): unknown {\n\t\t\tconst id = graph.resolve(specifier, filename);\n\t\t\tconst evaluating = commonJsModules.get(id);\n\t\t\tif (evaluating !== undefined) return evaluating.exports;\n\t\t\tconst result: { readonly default?: unknown } = nativeRequire(id);\n\t\t\treturn commonJs.has(id) ? result.default : result;\n\t\t},\n\t\tevaluateCommonJs(filename: string, body: CommonJsBody): unknown {\n\t\t\tassertActive();\n\t\t\tconst id = moduleId(filename);\n\t\t\tconst module: CommonJsModule = { exports: {} };\n\t\t\tcommonJsModules.set(id, module);\n\t\t\ttry {\n\t\t\t\tbody.call(module.exports, module.exports, module);\n\t\t\t} catch (error) {\n\t\t\t\tcommonJsModules.delete(id);\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t\treturn module.exports;\n\t\t},\n\t\tload(filename: string): ModuleSource {\n\t\t\tassertActive();\n\t\t\tconst existing = sources.get(filename);\n\t\t\tif (existing) return existing;\n\t\t\tconst source = readFileSync(filename, \"utf8\");\n\t\t\tlet name = \"__senpiExtensionMeta\";\n\t\t\twhile (source.includes(name)) name += \"_\";\n\t\t\tconst extension = extname(filename);\n\t\t\tconst transpiler = new Bun.Transpiler({\n\t\t\t\tloader: extension === \".tsx\" ? \"tsx\" : extension === \".jsx\" ? \"jsx\" : \"ts\",\n\t\t\t\ttarget: \"bun\",\n\t\t\t\tdefine: {\n\t\t\t\t\t\"import.meta\": name,\n\t\t\t\t\trequire: `${name}.require`,\n\t\t\t\t\t__filename: `${name}.path`,\n\t\t\t\t\t__dirname: `${name}.dir`,\n\t\t\t\t},\n\t\t\t});\n\t\t\tlet contents: string;\n\t\t\ttry {\n\t\t\t\tcontents = transpiler.transformSync(source);\n\t\t\t} catch (error) {\n\t\t\t\tif (error instanceof AggregateError) throw new ExtensionSourceError(filename, error);\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t\tconst [imports, , , hasModuleSyntax] = parse(contents, filename);\n\t\t\tconst edits: { readonly start: number; readonly end: number; readonly text: string }[] = [];\n\t\t\tlet commonJsImports = 0;\n\t\t\tfor (const edge of imports) {\n\t\t\t\tif (edge.type === \"dynamic\") {\n\t\t\t\t\t// Replace the keyword, not its argument: nested expressions, templates,\n\t\t\t\t\t// import attributes, and unavailable optional dependencies stay lazy.\n\t\t\t\t\tedits.push({ start: edge.importStart, end: edge.dynamicStart, text: `${name}.import` });\n\t\t\t\t} else if (edge.type === \"static\" || edge.type === \"reexport-star\") {\n\t\t\t\t\tconst target = resolveTarget(edge.specifier, filename);\n\t\t\t\t\tif (target.path !== undefined && isCommonJsFile(target.path)) {\n\t\t\t\t\t\t// \"import\" and \"export\" are both six characters, so the clause is\n\t\t\t\t\t\t// whatever sits between the keyword and the specifier in either form.\n\t\t\t\t\t\tconst clause = contents\n\t\t\t\t\t\t\t.slice(edge.importStart + \"import\".length, edge.start - 1)\n\t\t\t\t\t\t\t.replace(/\\bfrom\\s*$/, \"\");\n\t\t\t\t\t\t// This branch replaces the whole statement, so the attributes between the\n\t\t\t\t\t\t// specifier and the end of it have to travel with it: they pick the loader.\n\t\t\t\t\t\tconst attributes =\n\t\t\t\t\t\t\tedge.attributesStart < 0 ? \"\" : contents.slice(edge.end + 1, edge.importEnd).replace(/;\\s*$/, \"\");\n\t\t\t\t\t\tedits.push({\n\t\t\t\t\t\t\tstart: edge.importStart,\n\t\t\t\t\t\t\tend: edge.importEnd,\n\t\t\t\t\t\t\ttext: rewriteCommonJsImport(clause, target.id, `${name}Cjs${commonJsImports++}`, attributes),\n\t\t\t\t\t\t});\n\t\t\t\t\t} else {\n\t\t\t\t\t\tedits.push({ start: edge.start - 1, end: edge.end + 1, text: JSON.stringify(target.id) });\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (const edit of edits.sort((a, b) => b.start - a.start)) {\n\t\t\t\tcontents = contents.slice(0, edit.start) + edit.text + contents.slice(edit.end);\n\t\t\t}\n\t\t\t// A shebang is only a shebang on the first line, so it goes before any prologue.\n\t\t\tcontents = contents.replace(/^#![^\\n]*\\n/, \"\");\n\t\t\t// Runtime plugins load ESM, even for CommonJS source. Node's module\n\t\t\t// function wrapper preserves synchronous export assignment and keeps\n\t\t\t// `exports` and `module` reassignable bindings with `this` as the exports\n\t\t\t// object, as dependencies such as whatwg-url and jsdom require.\n\t\t\tif (!hasModuleSyntax && extension !== \".mjs\" && extension !== \".mts\") {\n\t\t\t\tcommonJs.add(moduleId(filename));\n\t\t\t\tcontents = `export default ${name}.commonJs(function (exports, module) {\\n${contents}\\n});`;\n\t\t\t}\n\t\t\tcontents = `import { metadata as ${name}Factory } from \"${extensionNamespace}:runtime\";\\nconst ${name} = ${name}Factory(${JSON.stringify(registration.generation)}, ${JSON.stringify(filename)});\\n${contents}`;\n\t\t\tconst prepared = { contents, loader: \"js\" } satisfies ModuleSource;\n\t\t\tsources.set(filename, prepared);\n\t\t\treturn prepared;\n\t\t},\n\t};\n\tconst registration = registerExtensionGraph(graph, virtualModules);\n\treturn {\n\t\tasync import(path: string, _options: { readonly default: true }): Promise<unknown> {\n\t\t\tassertActive();\n\t\t\tconst id = moduleId(realpathSync(resolve(path)));\n\t\t\tconst module: { readonly default?: unknown } = await import(id);\n\t\t\tconst factory = module.default;\n\t\t\tif (typeof factory !== \"function\") return factory;\n\t\t\t// This wrapper is not stored in Bun's permanent module registry. A\n\t\t\t// reachable old factory keeps its own computed-import graph alive.\n\t\t\treturn function (this: unknown, ...args: unknown[]) {\n\t\t\t\tgraph.assertActive();\n\t\t\t\treturn factory.apply(this, args);\n\t\t\t};\n\t\t},\n\t\t// The generation's staleness check reads these: every source file it transpiled.\n\t\tcompiledFiles(): readonly string[] {\n\t\t\treturn [...sources.keys()];\n\t\t},\n\t\tdispose() {\n\t\t\tactive = false;\n\t\t\tregistration.dispose();\n\t\t\tsources.clear();\n\t\t\tcommonJs.clear();\n\t\t\tcommonJsModules.clear();\n\t\t},\n\t};\n}\n"]}
1
+ {"version":3,"file":"bun-extension-importer.js","sourceRoot":"","sources":["../../../src/core/extensions/bun-extension-importer.ts"],"names":[],"mappings":";;;;;;;;AAAA,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACvD,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,O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,CAAC,GAAG,CAAC,WAAW,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;YAC9G,MAAM,EAAE,EAAE,EAAE,GAAG,UAAU,CAAC,KAAK,CAAC,CAAC;YACjC,MAAM,MAAM,GAAmC,MAAM,MAAM,kCAAC,EAAE,EAAC,CAAC;YAChE,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;YAC/B,IAAI,OAAO,OAAO,KAAK,UAAU;gBAAE,OAAO,OAAO,CAAC;YAClD,mEAAmE;YACnE,mEAAmE;YACnE,OAAO,UAAyB,GAAG,IAAe;gBACjD,KAAK,CAAC,YAAY,EAAE,CAAC;gBACrB,OAAO,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;YAClC,CAAC,CAAC;QACH,CAAC;QACD,iFAAiF;QACjF,aAAa;YACZ,OAAO,CAAC,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC;QAC5B,CAAC;QACD,OAAO;YACN,MAAM,GAAG,KAAK,CAAC;YACf,YAAY,CAAC,OAAO,EAAE,CAAC;YACvB,OAAO,CAAC,KAAK,EAAE,CAAC;YAChB,QAAQ,CAAC,KAAK,EAAE,CAAC;YACjB,eAAe,CAAC,KAAK,EAAE,CAAC;QACzB,CAAC;KACD,CAAC;AACH,CAAC","sourcesContent":["import { readFileSync, realpathSync, statSync } from \"node:fs\";\nimport { createRequire, isBuiltin } from \"node:module\";\nimport { dirname, extname, resolve } from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\nimport { parse } from \"es-module-lexer/js\";\nimport { isCommonJsFile, rewriteCommonJsImport } from \"./bun-extension-commonjs.ts\";\nimport { ExtensionSourceError } from \"./bun-extension-error.ts\";\nimport {\n\ttype CommonJsBody,\n\ttype CommonJsModule,\n\tExtensionGenerationDisposedError,\n\textensionNamespace,\n\ttype ModuleSource,\n\tregisterExtensionGraph,\n} from \"./bun-extension-registry.ts\";\n\nexport { bunExtensionImporterStats } from \"./bun-extension-registry.ts\";\n\n// Keep the source/Node build independent of Bun's ambient type declarations.\ndeclare const Bun: {\n\tresolveSync(specifier: string, directory: string): string;\n\tTranspiler: new (options: {\n\t\treadonly loader: \"ts\" | \"tsx\" | \"jsx\";\n\t\treadonly target: \"bun\";\n\t\treadonly define: Readonly<Record<string, string>>;\n\t}) => { transformSync(source: string): string };\n};\n\n/** A generation owns only source bookkeeping; Bun owns evaluation and cycles. */\nexport function createBunExtensionImporter(\n\tvirtualModules: Readonly<Record<string, Readonly<Record<string, unknown>>>>,\n) {\n\tconst sources = new Map<string, ModuleSource>();\n\tconst commonJs = new Set<string>();\n\t// Live `module` objects by id, registered while a CommonJS body runs: a require inside a\n\t// cycle receives the partially built exports, as in Node, instead of an unset ESM default.\n\t// A body that throws is evicted so a later require re-throws instead of seeing a half-built module.\n\tconst commonJsModules = new Map<string, CommonJsModule>();\n\tconst nativeRequire = createRequire(import.meta.url);\n\tlet active = true;\n\tconst assertActive = () => {\n\t\tif (!active) throw new ExtensionGenerationDisposedError(registration.generation);\n\t};\n\tconst moduleId = (filename: string) =>\n\t\t`${extensionNamespace}:${registration.generation}/${encodeURIComponent(filename)}`;\n\t// Only source the graph transpiles gets a graph id. Anything else (JSON, TOML, text, native\n\t// addons) keeps its real path so Bun's own loader handles it: Bun 1.3.x cannot follow a\n\t// plugin's hand-back to the `file` namespace from a runtime import() or require() (#2164).\n\tconst fileTarget = (resolved: string): { readonly id: string; readonly path?: string } =>\n\t\t/\\.[cm]?[jt]sx?$/.test(resolved) ? { id: moduleId(resolved), path: resolved } : { id: resolved };\n\tconst resolveTarget = (specifier: string, filename: string): { readonly id: string; readonly path?: string } => {\n\t\tassertActive();\n\t\tif (Object.hasOwn(virtualModules, specifier) || isBuiltin(specifier) || specifier.startsWith(\"bun:\"))\n\t\t\treturn { id: specifier };\n\t\tif (specifier.startsWith(`${extensionNamespace}:`)) return { id: specifier };\n\t\tconst path = specifier.startsWith(\"file:\") ? fileURLToPath(specifier) : specifier;\n\t\treturn fileTarget(realpathSync(Bun.resolveSync(path, dirname(filename))));\n\t};\n\tconst graph = {\n\t\tassertActive,\n\t\tresolve(specifier: string, filename: string): string {\n\t\t\treturn resolveTarget(specifier, filename).id;\n\t\t},\n\t\trequire(specifier: string, filename: string): unknown {\n\t\t\tconst id = graph.resolve(specifier, filename);\n\t\t\tconst evaluating = commonJsModules.get(id);\n\t\t\tif (evaluating !== undefined) return evaluating.exports;\n\t\t\tconst result: { readonly default?: unknown } = nativeRequire(id);\n\t\t\treturn commonJs.has(id) ? result.default : result;\n\t\t},\n\t\tevaluateCommonJs(filename: string, body: CommonJsBody): unknown {\n\t\t\tassertActive();\n\t\t\tconst id = moduleId(filename);\n\t\t\tconst module: CommonJsModule = { exports: {} };\n\t\t\tcommonJsModules.set(id, module);\n\t\t\ttry {\n\t\t\t\tbody.call(module.exports, module.exports, module);\n\t\t\t} catch (error) {\n\t\t\t\tcommonJsModules.delete(id);\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t\treturn module.exports;\n\t\t},\n\t\tload(filename: string): ModuleSource {\n\t\t\tassertActive();\n\t\t\tconst existing = sources.get(filename);\n\t\t\tif (existing) return existing;\n\t\t\tconst source = readFileSync(filename, \"utf8\");\n\t\t\tlet name = \"__senpiExtensionMeta\";\n\t\t\twhile (source.includes(name)) name += \"_\";\n\t\t\tconst extension = extname(filename);\n\t\t\tconst transpiler = new Bun.Transpiler({\n\t\t\t\tloader: extension === \".tsx\" ? \"tsx\" : extension === \".jsx\" ? \"jsx\" : \"ts\",\n\t\t\t\ttarget: \"bun\",\n\t\t\t\tdefine: {\n\t\t\t\t\t\"import.meta\": name,\n\t\t\t\t\trequire: `${name}.require`,\n\t\t\t\t\t__filename: `${name}.path`,\n\t\t\t\t\t__dirname: `${name}.dir`,\n\t\t\t\t},\n\t\t\t});\n\t\t\tlet contents: string;\n\t\t\ttry {\n\t\t\t\tcontents = transpiler.transformSync(source);\n\t\t\t} catch (error) {\n\t\t\t\tif (error instanceof AggregateError) throw new ExtensionSourceError(filename, error);\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t\tconst [imports, , , hasModuleSyntax] = parse(contents, filename);\n\t\t\tconst edits: { readonly start: number; readonly end: number; readonly text: string }[] = [];\n\t\t\tlet commonJsImports = 0;\n\t\t\tfor (const edge of imports) {\n\t\t\t\tif (edge.type === \"dynamic\") {\n\t\t\t\t\t// Replace the keyword, not its argument: nested expressions, templates,\n\t\t\t\t\t// import attributes, and unavailable optional dependencies stay lazy.\n\t\t\t\t\tedits.push({ start: edge.importStart, end: edge.dynamicStart, text: `${name}.import` });\n\t\t\t\t} else if (edge.type === \"static\" || edge.type === \"reexport-star\") {\n\t\t\t\t\tconst target = resolveTarget(edge.specifier, filename);\n\t\t\t\t\tif (target.path !== undefined && isCommonJsFile(target.path)) {\n\t\t\t\t\t\t// \"import\" and \"export\" are both six characters, so the clause is\n\t\t\t\t\t\t// whatever sits between the keyword and the specifier in either form.\n\t\t\t\t\t\tconst clause = contents\n\t\t\t\t\t\t\t.slice(edge.importStart + \"import\".length, edge.start - 1)\n\t\t\t\t\t\t\t.replace(/\\bfrom\\s*$/, \"\");\n\t\t\t\t\t\t// This branch replaces the whole statement, so the attributes between the\n\t\t\t\t\t\t// specifier and the end of it have to travel with it: they pick the loader.\n\t\t\t\t\t\tconst attributes =\n\t\t\t\t\t\t\tedge.attributesStart < 0 ? \"\" : contents.slice(edge.end + 1, edge.importEnd).replace(/;\\s*$/, \"\");\n\t\t\t\t\t\tedits.push({\n\t\t\t\t\t\t\tstart: edge.importStart,\n\t\t\t\t\t\t\tend: edge.importEnd,\n\t\t\t\t\t\t\ttext: rewriteCommonJsImport(clause, target.id, `${name}Cjs${commonJsImports++}`, attributes),\n\t\t\t\t\t\t});\n\t\t\t\t\t} else {\n\t\t\t\t\t\tedits.push({ start: edge.start - 1, end: edge.end + 1, text: JSON.stringify(target.id) });\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (const edit of edits.sort((a, b) => b.start - a.start)) {\n\t\t\t\tcontents = contents.slice(0, edit.start) + edit.text + contents.slice(edit.end);\n\t\t\t}\n\t\t\t// A shebang is only a shebang on the first line, so it goes before any prologue.\n\t\t\tcontents = contents.replace(/^#![^\\n]*\\n/, \"\");\n\t\t\t// Runtime plugins load ESM, even for CommonJS source. Node's module\n\t\t\t// function wrapper preserves synchronous export assignment and keeps\n\t\t\t// `exports` and `module` reassignable bindings with `this` as the exports\n\t\t\t// object, as dependencies such as whatwg-url and jsdom require.\n\t\t\tif (!hasModuleSyntax && extension !== \".mjs\" && extension !== \".mts\") {\n\t\t\t\tcommonJs.add(moduleId(filename));\n\t\t\t\tcontents = `export default ${name}.commonJs(function (exports, module) {\\n${contents}\\n});`;\n\t\t\t}\n\t\t\tcontents = `import { metadata as ${name}Factory } from \"${extensionNamespace}:runtime\";\\nconst ${name} = ${name}Factory(${JSON.stringify(registration.generation)}, ${JSON.stringify(filename)});\\n${contents}`;\n\t\t\tconst prepared = { contents, loader: \"js\" } satisfies ModuleSource;\n\t\t\tsources.set(filename, prepared);\n\t\t\treturn prepared;\n\t\t},\n\t};\n\tconst registration = registerExtensionGraph(graph, virtualModules);\n\treturn {\n\t\tasync import(path: string, _options: { readonly default: true }): Promise<unknown> {\n\t\t\tassertActive();\n\t\t\tconst absolute = realpathSync(resolve(path));\n\t\t\t// A package directory loads through its main or index file, as a native import does.\n\t\t\tconst entry = statSync(absolute).isDirectory() ? realpathSync(Bun.resolveSync(absolute, absolute)) : absolute;\n\t\t\tconst { id } = fileTarget(entry);\n\t\t\tconst module: { readonly default?: unknown } = await import(id);\n\t\t\tconst factory = module.default;\n\t\t\tif (typeof factory !== \"function\") return factory;\n\t\t\t// This wrapper is not stored in Bun's permanent module registry. A\n\t\t\t// reachable old factory keeps its own computed-import graph alive.\n\t\t\treturn function (this: unknown, ...args: unknown[]) {\n\t\t\t\tgraph.assertActive();\n\t\t\t\treturn factory.apply(this, args);\n\t\t\t};\n\t\t},\n\t\t// The generation's staleness check reads these: every source file it transpiled.\n\t\tcompiledFiles(): readonly string[] {\n\t\t\treturn [...sources.keys()];\n\t\t},\n\t\tdispose() {\n\t\t\tactive = false;\n\t\t\tregistration.dispose();\n\t\t\tsources.clear();\n\t\t\tcommonJs.clear();\n\t\t\tcommonJsModules.clear();\n\t\t},\n\t};\n}\n"]}
@@ -1,5 +1,5 @@
1
1
  import { existsSync } from "node:fs";
2
- import { dirname } from "node:path";
2
+ import { dirname, isAbsolute } from "node:path";
3
3
  import { fileURLToPath, pathToFileURL } from "node:url";
4
4
  import { publishRuntimeMetadata } from "./extension-runtime-module.js";
5
5
  // esbuild reads an import() only when its second argument is fully static, and these
@@ -61,7 +61,10 @@ function graphFor(generation) {
61
61
  function metadata(generation, filename) {
62
62
  const resolvePath = (specifier) => {
63
63
  const id = graphFor(generation).resolve(specifier, filename);
64
- return id.startsWith(`${extensionNamespace}:`) ? decodeURIComponent(id.slice(id.indexOf("/") + 1)) : undefined;
64
+ if (id.startsWith(`${extensionNamespace}:`))
65
+ return decodeURIComponent(id.slice(id.indexOf("/") + 1));
66
+ // Files Bun loads natively (JSON, assets) resolve to their real path, not a graph id.
67
+ return isAbsolute(id) ? id : undefined;
65
68
  };
66
69
  // CommonJS code calls `require.resolve` for sibling files (jsdom locates its XHR sync
67
70
  // worker that way), so `require` is a function object carrying Node's resolver shape.
@@ -117,9 +120,7 @@ function installRegistry(virtualModules) {
117
120
  if (parsed === undefined)
118
121
  throw new ExtensionModuleIdError(path);
119
122
  graphFor(parsed.generation);
120
- return /\.[cm]?[jt]sx?$/.test(parsed.filename)
121
- ? { path, namespace: extensionNamespace }
122
- : { path: parsed.filename, namespace: "file" };
123
+ return { path, namespace: extensionNamespace };
123
124
  });
124
125
  builder.onLoad({ filter: /.*/, namespace: extensionNamespace }, ({ path }) => {
125
126
  const parsed = splitModuleId(path);
@@ -1 +1 @@
1
- {"version":3,"file":"bun-extension-registry.js","sourceRoot":"","sources":["../../../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EACrB,cAA2E;IAE3E,eAAe,CAAC,cAAc,CAAC,CAAC;IAChC,MAAM,UAAU,GAAG,MAAM,CAAC,cAAc,EAAE,CAAC,CAAC;IAC5C,MAAM,CAAC,GAAG,CAAC,UAAU,EAAE,IAAI,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;IAC3C,SAAS,CAAC,QAAQ,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,CAAC,CAAC;IAC7C,OAAO;QACN,UAAU;QACV,OAAO;YACN,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;YAC1B,SAAS,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;QAC7B,CAAC;KACD,CAAC;AACH,CAAC;AAED,gFAAgF;AAChF,MAAM,UAAU,yBAAyB;IACxC,OAAO,EAAE,WAAW,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE,OAAO,EAAE,mBAAmB,EAAE,CAAC;AACpH,CAAC","sourcesContent":["import { existsSync } from \"node:fs\";\nimport { dirname } from \"node:path\";\nimport { fileURLToPath, pathToFileURL } from \"node:url\";\nimport { publishRuntimeMetadata } from \"./extension-runtime-module.ts\";\n\nexport type ModuleSource = { readonly contents: string; readonly loader: \"js\" };\nexport type Resolution = { readonly path: string; readonly namespace: string };\nexport type CommonJsModule = { exports: unknown };\nexport type CommonJsBody = (this: unknown, exports: unknown, module: CommonJsModule) => void;\nexport interface ExtensionGraph {\n\tresolve(specifier: string, filename: string): string;\n\tload(filename: string): ModuleSource;\n\trequire(specifier: string, filename: string): unknown;\n\tevaluateCommonJs(filename: string, body: CommonJsBody): unknown;\n}\ntype ModuleObject = { readonly exports: Readonly<Record<string, unknown>>; readonly loader: \"object\" };\ndeclare const Bun: {\n\tplugin(options: {\n\t\treadonly name: string;\n\t\treadonly setup: (builder: {\n\t\t\tmodule(name: string, load: () => ModuleObject): void;\n\t\t\tonResolve(\n\t\t\t\toptions: { readonly filter: RegExp; readonly namespace: string },\n\t\t\t\tresolve: (args: { readonly path: string }) => Resolution,\n\t\t\t): void;\n\t\t\tonLoad(\n\t\t\t\toptions: { readonly filter: RegExp; readonly namespace: string },\n\t\t\t\tload: (args: { readonly path: string }) => ModuleSource,\n\t\t\t): void;\n\t\t}) => void;\n\t}): void;\n};\n\n// esbuild reads an import() only when its second argument is fully static, and these\n// attributes are whatever the extension wrote. The call stays dynamic either way, so\n// the importer is built at runtime and the bundler never has to read it.\nconst importModule = new Function(\"specifier\", \"options\", \"return import(specifier, options)\") as (\n\tspecifier: string,\n\toptions?: ImportCallOptions,\n) => Promise<unknown>;\n\nexport const extensionNamespace = \"senpi-extension\";\nconst RUNTIME_SPECIFIER = \"runtime\";\n// Inside a `bun build --compile` binary the shim has no on-disk path (import.meta.url is a\n// $bunfs URL), so the file route is only available when the file is really there; the compiled\n// binary keeps the virtual module, which it never had a problem with.\nconst runtimeModulePath = resolveRuntimeModulePath();\n\nfunction resolveRuntimeModulePath(): string | undefined {\n\ttry {\n\t\tconst candidate = fileURLToPath(new URL(\"./extension-runtime-module.js\", import.meta.url));\n\t\treturn existsSync(candidate) ? candidate : undefined;\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\n// An id is `<generation>/<encodeURIComponent(filename)>`. The encoded half never contains a\n// literal \"/\" (Windows separators arrive as %5C), so the FIRST slash is always the generation\n// separator — but only when one exists. A bare id with no slash used to slice to nonsense and\n// surface as `Cannot find package '<generation>'` (omo#8427).\nfunction splitModuleId(id: string): { readonly generation: string; readonly filename: string } | undefined {\n\tconst slash = id.indexOf(\"/\");\n\tif (slash <= 0 || slash === id.length - 1) return undefined;\n\treturn { generation: id.slice(0, slash), filename: decodeURIComponent(id.slice(slash + 1)) };\n}\nconst graphs = new Map<string, WeakRef<ExtensionGraph>>();\nconst collected = new FinalizationRegistry<string>((generation) => graphs.delete(generation));\nlet nextGeneration = 0;\nlet installed = false;\nlet pluginRegistrations = 0;\nconst registeredHosts = new Set<string>();\n\nexport class ExtensionModuleIdError extends Error {\n\treadonly name = \"ExtensionModuleIdError\";\n\tconstructor(id: string) {\n\t\tsuper(`Extension module id is not \"<generation>/<encoded filename>\": ${id}`);\n\t}\n}\n\nexport class ExtensionGenerationDisposedError extends Error {\n\treadonly name = \"ExtensionGenerationDisposedError\";\n\treadonly generation: string;\n\tconstructor(generation: string) {\n\t\tsuper(`Extension generation ${generation} has been disposed`);\n\t\tthis.generation = generation;\n\t}\n}\nfunction graphFor(generation: string): ExtensionGraph {\n\tconst graph = graphs.get(generation)?.deref();\n\tif (!graph) throw new ExtensionGenerationDisposedError(generation);\n\treturn graph;\n}\n\n// Module-registry exports must never close over a graph: Bun retains modules.\n// Only the importer, returned factory wrappers, and live runtimes own graphs.\nfunction metadata(generation: string, filename: string) {\n\tconst resolvePath = (specifier: string) => {\n\t\tconst id = graphFor(generation).resolve(specifier, filename);\n\t\treturn id.startsWith(`${extensionNamespace}:`) ? decodeURIComponent(id.slice(id.indexOf(\"/\") + 1)) : undefined;\n\t};\n\t// CommonJS code calls `require.resolve` for sibling files (jsdom locates its XHR sync\n\t// worker that way), so `require` is a function object carrying Node's resolver shape.\n\tconst require = Object.assign((specifier: string) => graphFor(generation).require(specifier, filename), {\n\t\tresolve: (specifier: string) => resolvePath(specifier) ?? graphFor(generation).resolve(specifier, filename),\n\t});\n\treturn {\n\t\turl: pathToFileURL(filename).href,\n\t\tpath: filename,\n\t\tdir: dirname(filename),\n\t\trequire,\n\t\tcommonJs: (body: CommonJsBody) => graphFor(generation).evaluateCommonJs(filename, body),\n\t\timport: async (specifier: string, options?: ImportCallOptions) =>\n\t\t\timportModule(graphFor(generation).resolve(specifier, filename), options),\n\t\tresolve: (specifier: string) => {\n\t\t\tconst path = resolvePath(specifier);\n\t\t\treturn path === undefined ? graphFor(generation).resolve(specifier, filename) : pathToFileURL(path).href;\n\t\t},\n\t};\n}\n\n// A separate activation prevents permanent hooks from sharing a closure\n// environment with registerExtensionGraph's disposable graph reference.\nfunction installRegistry(virtualModules: Readonly<Record<string, Readonly<Record<string, unknown>>>>) {\n\tconst hosts = Object.entries(virtualModules).filter(([name]) => !registeredHosts.has(name));\n\tif (!installed || hosts.length > 0) {\n\t\tpluginRegistrations++;\n\t\tBun.plugin({\n\t\t\tname: extensionNamespace,\n\t\t\tsetup(builder) {\n\t\t\t\tfor (const [name, exports] of hosts) {\n\t\t\t\t\tbuilder.module(name, () => ({ exports, loader: \"object\" }));\n\t\t\t\t\tregisteredHosts.add(name);\n\t\t\t\t}\n\t\t\t\tif (installed) return;\n\t\t\t\t// \"runtime\" resolves to a real file, never a Bun.plugin virtual module. A\n\t\t\t\t// builder.module() registration is intermittently invisible to the resolver under\n\t\t\t\t// `bun test --parallel` on a loaded Windows shard: onResolve still fires and returns\n\t\t\t\t// the namespace, the registration is still listed, and Bun answers\n\t\t\t\t// `Cannot find package 'runtime'` anyway — generation 1 served 40 resolves and\n\t\t\t\t// refused the 41st in the same worker (omo#8427, run 35329245740). A file on disk\n\t\t\t\t// has no registration window to lose.\n\t\t\t\tpublishRuntimeMetadata(metadata);\n\t\t\t\tbuilder.module(`${extensionNamespace}:${RUNTIME_SPECIFIER}`, () => ({\n\t\t\t\t\texports: { metadata },\n\t\t\t\t\tloader: \"object\",\n\t\t\t\t}));\n\t\t\t\tbuilder.onResolve({ filter: /.*/, namespace: extensionNamespace }, ({ path }) => {\n\t\t\t\t\tif (path === RUNTIME_SPECIFIER)\n\t\t\t\t\t\treturn runtimeModulePath === undefined\n\t\t\t\t\t\t\t? { path, namespace: extensionNamespace }\n\t\t\t\t\t\t\t: { path: runtimeModulePath, namespace: \"file\" };\n\t\t\t\t\tconst parsed = splitModuleId(path);\n\t\t\t\t\tif (parsed === undefined) throw new ExtensionModuleIdError(path);\n\t\t\t\t\tgraphFor(parsed.generation);\n\t\t\t\t\treturn /\\.[cm]?[jt]sx?$/.test(parsed.filename)\n\t\t\t\t\t\t? { path, namespace: extensionNamespace }\n\t\t\t\t\t\t: { path: parsed.filename, namespace: \"file\" };\n\t\t\t\t});\n\t\t\t\tbuilder.onLoad({ filter: /.*/, namespace: extensionNamespace }, ({ path }) => {\n\t\t\t\t\tconst parsed = splitModuleId(path);\n\t\t\t\t\tif (parsed === undefined) throw new ExtensionModuleIdError(path);\n\t\t\t\t\treturn graphFor(parsed.generation).load(parsed.filename);\n\t\t\t\t});\n\t\t\t\tinstalled = true;\n\t\t\t},\n\t\t});\n\t}\n}\n\nexport function registerExtensionGraph(\n\tgraph: ExtensionGraph,\n\tvirtualModules: Readonly<Record<string, Readonly<Record<string, unknown>>>>,\n) {\n\tinstallRegistry(virtualModules);\n\tconst generation = String(nextGeneration++);\n\tgraphs.set(generation, new WeakRef(graph));\n\tcollected.register(graph, generation, graph);\n\treturn {\n\t\tgeneration,\n\t\tdispose() {\n\t\t\tgraphs.delete(generation);\n\t\t\tcollected.unregister(graph);\n\t\t},\n\t};\n}\n\n/** Internal lifecycle diagnostics, shared by regression and compiled probes. */\nexport function bunExtensionImporterStats() {\n\treturn { generations: [...graphs.values()].filter((entry) => entry.deref()).length, plugins: pluginRegistrations };\n}\n"]}
1
+ {"version":3,"file":"bun-extension-registry.js","sourceRoot":"","sources":["../../../src/core/extensions/bun-extension-registry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACxD,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AA8BvE,qFAAqF;AACrF,qFAAqF;AACrF,yEAAyE;AACzE,MAAM,YAAY,GAAG,IAAI,QAAQ,CAAC,WAAW,EAAE,SAAS,EAAE,mCAAmC,CAGxE,CAAC;AAEtB,MAAM,CAAC,MAAM,kBAAkB,GAAG,iB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AC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE,OAAO,EAAE,mBAAmB,EAAE,CAAC;AACpH,CAAC","sourcesContent":["import { existsSync } from \"node:fs\";\nimport { dirname, isAbsolute } from \"node:path\";\nimport { fileURLToPath, pathToFileURL } from \"node:url\";\nimport { publishRuntimeMetadata } from \"./extension-runtime-module.ts\";\n\nexport type ModuleSource = { readonly contents: string; readonly loader: \"js\" };\nexport type Resolution = { readonly path: string; readonly namespace: string };\nexport type CommonJsModule = { exports: unknown };\nexport type CommonJsBody = (this: unknown, exports: unknown, module: CommonJsModule) => void;\nexport interface ExtensionGraph {\n\tresolve(specifier: string, filename: string): string;\n\tload(filename: string): ModuleSource;\n\trequire(specifier: string, filename: string): unknown;\n\tevaluateCommonJs(filename: string, body: CommonJsBody): unknown;\n}\ntype ModuleObject = { readonly exports: Readonly<Record<string, unknown>>; readonly loader: \"object\" };\ndeclare const Bun: {\n\tplugin(options: {\n\t\treadonly name: string;\n\t\treadonly setup: (builder: {\n\t\t\tmodule(name: string, load: () => ModuleObject): void;\n\t\t\tonResolve(\n\t\t\t\toptions: { readonly filter: RegExp; readonly namespace: string },\n\t\t\t\tresolve: (args: { readonly path: string }) => Resolution,\n\t\t\t): void;\n\t\t\tonLoad(\n\t\t\t\toptions: { readonly filter: RegExp; readonly namespace: string },\n\t\t\t\tload: (args: { readonly path: string }) => ModuleSource,\n\t\t\t): void;\n\t\t}) => void;\n\t}): void;\n};\n\n// esbuild reads an import() only when its second argument is fully static, and these\n// attributes are whatever the extension wrote. The call stays dynamic either way, so\n// the importer is built at runtime and the bundler never has to read it.\nconst importModule = new Function(\"specifier\", \"options\", \"return import(specifier, options)\") as (\n\tspecifier: string,\n\toptions?: ImportCallOptions,\n) => Promise<unknown>;\n\nexport const extensionNamespace = \"senpi-extension\";\nconst RUNTIME_SPECIFIER = \"runtime\";\n// Inside a `bun build --compile` binary the shim has no on-disk path (import.meta.url is a\n// $bunfs URL), so the file route is only available when the file is really there; the compiled\n// binary keeps the virtual module, which it never had a problem with.\nconst runtimeModulePath = resolveRuntimeModulePath();\n\nfunction resolveRuntimeModulePath(): string | undefined {\n\ttry {\n\t\tconst candidate = fileURLToPath(new URL(\"./extension-runtime-module.js\", import.meta.url));\n\t\treturn existsSync(candidate) ? candidate : undefined;\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\n// An id is `<generation>/<encodeURIComponent(filename)>`. The encoded half never contains a\n// literal \"/\" (Windows separators arrive as %5C), so the FIRST slash is always the generation\n// separator — but only when one exists. A bare id with no slash used to slice to nonsense and\n// surface as `Cannot find package '<generation>'` (omo#8427).\nfunction splitModuleId(id: string): { readonly generation: string; readonly filename: string } | undefined {\n\tconst slash = id.indexOf(\"/\");\n\tif (slash <= 0 || slash === id.length - 1) return undefined;\n\treturn { generation: id.slice(0, slash), filename: decodeURIComponent(id.slice(slash + 1)) };\n}\nconst graphs = new Map<string, WeakRef<ExtensionGraph>>();\nconst collected = new FinalizationRegistry<string>((generation) => graphs.delete(generation));\nlet nextGeneration = 0;\nlet installed = false;\nlet pluginRegistrations = 0;\nconst registeredHosts = new Set<string>();\n\nexport class ExtensionModuleIdError extends Error {\n\treadonly name = \"ExtensionModuleIdError\";\n\tconstructor(id: string) {\n\t\tsuper(`Extension module id is not \"<generation>/<encoded filename>\": ${id}`);\n\t}\n}\n\nexport class ExtensionGenerationDisposedError extends Error {\n\treadonly name = \"ExtensionGenerationDisposedError\";\n\treadonly generation: string;\n\tconstructor(generation: string) {\n\t\tsuper(`Extension generation ${generation} has been disposed`);\n\t\tthis.generation = generation;\n\t}\n}\nfunction graphFor(generation: string): ExtensionGraph {\n\tconst graph = graphs.get(generation)?.deref();\n\tif (!graph) throw new ExtensionGenerationDisposedError(generation);\n\treturn graph;\n}\n\n// Module-registry exports must never close over a graph: Bun retains modules.\n// Only the importer, returned factory wrappers, and live runtimes own graphs.\nfunction metadata(generation: string, filename: string) {\n\tconst resolvePath = (specifier: string) => {\n\t\tconst id = graphFor(generation).resolve(specifier, filename);\n\t\tif (id.startsWith(`${extensionNamespace}:`)) return decodeURIComponent(id.slice(id.indexOf(\"/\") + 1));\n\t\t// Files Bun loads natively (JSON, assets) resolve to their real path, not a graph id.\n\t\treturn isAbsolute(id) ? id : undefined;\n\t};\n\t// CommonJS code calls `require.resolve` for sibling files (jsdom locates its XHR sync\n\t// worker that way), so `require` is a function object carrying Node's resolver shape.\n\tconst require = Object.assign((specifier: string) => graphFor(generation).require(specifier, filename), {\n\t\tresolve: (specifier: string) => resolvePath(specifier) ?? graphFor(generation).resolve(specifier, filename),\n\t});\n\treturn {\n\t\turl: pathToFileURL(filename).href,\n\t\tpath: filename,\n\t\tdir: dirname(filename),\n\t\trequire,\n\t\tcommonJs: (body: CommonJsBody) => graphFor(generation).evaluateCommonJs(filename, body),\n\t\timport: async (specifier: string, options?: ImportCallOptions) =>\n\t\t\timportModule(graphFor(generation).resolve(specifier, filename), options),\n\t\tresolve: (specifier: string) => {\n\t\t\tconst path = resolvePath(specifier);\n\t\t\treturn path === undefined ? graphFor(generation).resolve(specifier, filename) : pathToFileURL(path).href;\n\t\t},\n\t};\n}\n\n// A separate activation prevents permanent hooks from sharing a closure\n// environment with registerExtensionGraph's disposable graph reference.\nfunction installRegistry(virtualModules: Readonly<Record<string, Readonly<Record<string, unknown>>>>) {\n\tconst hosts = Object.entries(virtualModules).filter(([name]) => !registeredHosts.has(name));\n\tif (!installed || hosts.length > 0) {\n\t\tpluginRegistrations++;\n\t\tBun.plugin({\n\t\t\tname: extensionNamespace,\n\t\t\tsetup(builder) {\n\t\t\t\tfor (const [name, exports] of hosts) {\n\t\t\t\t\tbuilder.module(name, () => ({ exports, loader: \"object\" }));\n\t\t\t\t\tregisteredHosts.add(name);\n\t\t\t\t}\n\t\t\t\tif (installed) return;\n\t\t\t\t// \"runtime\" resolves to a real file, never a Bun.plugin virtual module. A\n\t\t\t\t// builder.module() registration is intermittently invisible to the resolver under\n\t\t\t\t// `bun test --parallel` on a loaded Windows shard: onResolve still fires and returns\n\t\t\t\t// the namespace, the registration is still listed, and Bun answers\n\t\t\t\t// `Cannot find package 'runtime'` anyway — generation 1 served 40 resolves and\n\t\t\t\t// refused the 41st in the same worker (omo#8427, run 35329245740). A file on disk\n\t\t\t\t// has no registration window to lose.\n\t\t\t\tpublishRuntimeMetadata(metadata);\n\t\t\t\tbuilder.module(`${extensionNamespace}:${RUNTIME_SPECIFIER}`, () => ({\n\t\t\t\t\texports: { metadata },\n\t\t\t\t\tloader: \"object\",\n\t\t\t\t}));\n\t\t\t\tbuilder.onResolve({ filter: /.*/, namespace: extensionNamespace }, ({ path }) => {\n\t\t\t\t\tif (path === RUNTIME_SPECIFIER)\n\t\t\t\t\t\treturn runtimeModulePath === undefined\n\t\t\t\t\t\t\t? { path, namespace: extensionNamespace }\n\t\t\t\t\t\t\t: { path: runtimeModulePath, namespace: \"file\" };\n\t\t\t\t\tconst parsed = splitModuleId(path);\n\t\t\t\t\tif (parsed === undefined) throw new ExtensionModuleIdError(path);\n\t\t\t\t\tgraphFor(parsed.generation);\n\t\t\t\t\treturn { path, namespace: extensionNamespace };\n\t\t\t\t});\n\t\t\t\tbuilder.onLoad({ filter: /.*/, namespace: extensionNamespace }, ({ path }) => {\n\t\t\t\t\tconst parsed = splitModuleId(path);\n\t\t\t\t\tif (parsed === undefined) throw new ExtensionModuleIdError(path);\n\t\t\t\t\treturn graphFor(parsed.generation).load(parsed.filename);\n\t\t\t\t});\n\t\t\t\tinstalled = true;\n\t\t\t},\n\t\t});\n\t}\n}\n\nexport function registerExtensionGraph(\n\tgraph: ExtensionGraph,\n\tvirtualModules: Readonly<Record<string, Readonly<Record<string, unknown>>>>,\n) {\n\tinstallRegistry(virtualModules);\n\tconst generation = String(nextGeneration++);\n\tgraphs.set(generation, new WeakRef(graph));\n\tcollected.register(graph, generation, graph);\n\treturn {\n\t\tgeneration,\n\t\tdispose() {\n\t\t\tgraphs.delete(generation);\n\t\t\tcollected.unregister(graph);\n\t\t},\n\t};\n}\n\n/** Internal lifecycle diagnostics, shared by regression and compiled probes. */\nexport function bunExtensionImporterStats() {\n\treturn { generations: [...graphs.values()].filter((entry) => entry.deref()).length, plugins: pluginRegistrations };\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../../src/core/messages.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAClE,OAAO,EAGN,KAAK,YAAY,EACjB,KAAK,OAAO,EACZ,KAAK,WAAW,EAChB,MAAM,uBAAuB,CAAC;AAG/B,eAAO,MAAM,yBAAyB,wGAGrC,CAAC;AAEF,eAAO,MAAM,yBAAyB,iBAC3B,CAAC;AAEZ,eAAO,MAAM,qBAAqB,iGAGjC,CAAC;AAEF,eAAO,MAAM,qBAAqB,eAAe,CAAC;AAElD,eAAO,MAAM,8BAA8B,sBAAsB,CAAC;AAElE;;;;GAIG;AACH,wBAAgB,8BAA8B,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAE3E;AAED;;;;;;;GAOG;AACH,wBAAgB,6BAA6B,CAAC,QAAQ,EAAE,YAAY,EAAE,GAAG,YAAY,EAAE,CAEtF;AAED;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACpC,IAAI,EAAE,eAAe,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B,SAAS,EAAE,OAAO,CAAC;IACnB,SAAS,EAAE,OAAO,CAAC;IACnB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,SAAS,EAAE,MAAM,CAAC;IAClB,qEAAqE;IACrE,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC7B;AAED;;;GAGG;AACH,MAAM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entContextIntoNextUserMessage } from "./environment-context.js";
9
+ import { omitProviderRejectedImages } from "./provider-rejected-images.js";
9
10
  export const COMPACTION_SUMMARY_PREFIX = `The conversation history before this point was compacted into the following summary:
10
11
 
11
12
  <summary>
@@ -161,8 +162,9 @@ export function convertToLlm(messages) {
161
162
  // deterministic per session state, so the cache-prefix guarantee above holds.
162
163
  // senpi#2118: the environment context then becomes the leading block of the
163
164
  // user message it precedes, so strict-alternation chat templates never see
164
- // two consecutive user messages.
165
- return foldEnvironmentContextIntoNextUserMessage(dropFailedAssistantTurns(converted), environmentMessages);
165
+ // two consecutive user messages. senpi#2170: images a failed turn proves the
166
+ // provider rejected are replaced first, while the failed turn is still visible.
167
+ return foldEnvironmentContextIntoNextUserMessage(dropFailedAssistantTurns(omitProviderRejectedImages(converted)), environmentMessages);
166
168
  }
167
169
  // ============================================================================
168
170
  // Transport image budget
@@ -1 +1 @@
1
- {"version":3,"file":"messages.js","sourceRoot":"","sources":["../../src/core/messages.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EACN,qBAAqB,EACrB,wBAAwB,GAIxB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,gCAAgC,EAAE,yCAAyC,EAAE,MAAM,0BAA0B,CAAC;AAEvH,MAAM,CAAC,MAAM,yBAAyB,GAAG;;;CAGxC,CAAC;AAEF,MAAM,CAAC,MAAM,yBAAyB,GAAG;WAC9B,CAAC;AAEZ,MAAM,CAAC,MAAM,qBAAqB,GAAG;;;CAGpC,CAAC;AAEF,MAAM,CAAC,MAAM,qBAAqB,GAAG,YAAY,CAAC;AAElD,MAAM,CAAC,MAAM,8BAA8B,GAAG,mBAAmB,CAAC;AAElE;;;;GAIG;AACH,MAAM,UAAU,8BAA8B,CAAC,WAAmB;IACjE,OAAO,KAAK,CAAC;AACd,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,6BAA6B,CAAC,QAAwB;IACrE,OAAO,QAAQ,CAAC;AACjB,CAAC;AA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CAAC,CAAC,KAAK,CAC9G,CAAC;QACF,OAAO;YACN,GAAG,OAAO;YACV,OAAO,EAAE,4BAA4B,CAAC,QAAQ,EAAE,yBAAyB,CAA2B;SACpG,CAAC;IACH,CAAC,CAAC,CAAC;AACJ,CAAC;AAED,mFAAmF;AACnF,MAAM,UAAU,wBAAwB,CAAC,QAAwB,EAAE,OAAgC;IAClG,MAAM,SAAS,GAAG,YAAY,CAAC,QAAQ,CAAC,CAAC;IACzC,IAAI,CAAC,OAAO,CAAC,WAAW;QAAE,OAAO,cAAc,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IAEpE,OAAO,SAAS,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE;QAChC,IAAI,CAAC,OAAO,CAAC,IAAI,KAAK,MAAM,IAAI,OAAO,CAAC,IAAI,KAAK,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;YACnG,OAAO,OAAO,CAAC;QAChB,CAAC;QACD,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,OAAO,CAAC;YAAE,OAAO,OAAO,CAAC;QAE7E,MAAM,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,EAA8B,EAAE,CAC1E,KAAK,CAAC,IAAI,KAAK,OAAO,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,yBAAyB,EAAE,CAAC,CAAC,CAAC,KAAK,CAClF,CAAC;QACF,OAAO;YACN,GAAG,OAAO;YACV,OAAO,EAAE,4BAA4B,CAAC,QAAQ,EAAE,yBAAyB,CAA2B;SACpG,CAAC;IACH,CAAC,CAAC,CAAC;AACJ,CAAC","sourcesContent":["/**\n * Custom message types and transformers for the coding agent.\n *\n * Extends the base AgentMessage type with coding-agent specific message types,\n * and provides a transformer to convert them to LLM-compatible messages.\n */\n\nimport type { AgentMessage } from \"@earendil-works/pi-agent-core\";\nimport {\n\tcopyContextProvenance,\n\tdropFailedAssistantTurns,\n\ttype ImageContent,\n\ttype Message,\n\ttype TextContent,\n} from \"@earendil-works/pi-ai\";\nimport { ENVIRONMENT_CONTEXT_MESSAGE_TYPE, foldEnvironmentContextIntoNextUserMessage } from \"./environment-context.ts\";\n\nexport const COMPACTION_SUMMARY_PREFIX = `The conversation history before this point was compacted into the following summary:\n\n<summary>\n`;\n\nexport const COMPACTION_SUMMARY_SUFFIX = `\n</summary>`;\n\nexport const BRANCH_SUMMARY_PREFIX = `The following is a summary of a branch that this conversation came back from:\n\n<summary>\n`;\n\nexport const BRANCH_SUMMARY_SUFFIX = `</summary>`;\n\nexport const GOAL_CONTINUATION_MESSAGE_TYPE = \"goal-continuation\";\n\n/**\n * Per-type exclusion must remain false: compaction and branch summarization\n * inspect entries independently, where excluding this type would hide the live\n * goal-continuation message.\n */\nexport function isContextExcludedCustomMessage(_customType: string): boolean {\n\treturn false;\n}\n\n/**\n * Whole-context filtering is deliberately a no-op. Goal continuations are\n * append-only: dropping an already-sent continuation would rewrite a\n * provider-visible turn, so request N would stop being a prefix of request N+1\n * and every cached token ahead of the edit would be invalidated (full re-read,\n * cost spike, 429 storms in team mode). Continuation history is bounded by\n * normal compaction instead of by per-request deletion.\n */\nexport function filterContextExcludedMessages(messages: AgentMessage[]): AgentMessage[] {\n\treturn messages;\n}\n\n/**\n * Message type for bash executions via the ! command.\n */\nexport interface BashExecutionMessage {\n\trole: \"bashExecution\";\n\tcommand: string;\n\toutput: string;\n\texitCode: number | undefined;\n\tcancelled: boolean;\n\ttruncated: boolean;\n\tfullOutputPath?: string;\n\ttimestamp: number;\n\t/** If true, this message is excluded from LLM context (!! prefix) */\n\texcludeFromContext?: boolean;\n}\n\n/**\n * Message type for extension-injected messages via sendMessage().\n * These are custom messages that extensions can inject into the conversation.\n */\nexport interface CustomMessage<T = unknown> {\n\trole: \"custom\";\n\tcustomType: string;\n\tcontent: string | (TextContent | ImageContent)[];\n\tdisplay: boolean;\n\tdetails?: T;\n\ttimestamp: number;\n}\n\nexport interface BranchSummaryMessage {\n\trole: \"branchSummary\";\n\tsummary: string;\n\tfromId: string | null;\n\ttimestamp: number;\n}\n\nexport interface CompactionSummaryMessage {\n\trole: \"compactionSummary\";\n\tsummary: string;\n\ttokensBefore: number;\n\tdetails?: unknown;\n\ttimestamp: number;\n}\n\nexport interface ConfigurationUpdateMessage {\n\trole: \"configurationUpdate\";\n\tcontent: (TextContent | ImageContent)[];\n\teffort: string;\n\ttimestamp: number;\n}\n\n// Extend CustomAgentMessages via declaration merging\ndeclare module \"@earendil-works/pi-agent-core\" {\n\tinterface CustomAgentMessages {\n\t\tbashExecution: BashExecutionMessage;\n\t\tcustom: CustomMessage;\n\t\tbranchSummary: BranchSummaryMessage;\n\t\tconfigurationUpdate: ConfigurationUpdateMessage;\n\t}\n}\n\n/**\n * Convert a BashExecutionMessage to user message text for LLM context.\n */\nexport function bashExecutionToText(msg: BashExecutionMessage): string {\n\tlet text = `Ran \\`${msg.command}\\`\\n`;\n\tif (msg.output) {\n\t\ttext += `\\`\\`\\`\\n${msg.output}\\n\\`\\`\\``;\n\t} else {\n\t\ttext += \"(no output)\";\n\t}\n\tif (msg.cancelled) {\n\t\ttext += \"\\n\\n(command cancelled)\";\n\t} else if (msg.exitCode !== null && msg.exitCode !== undefined && msg.exitCode !== 0) {\n\t\ttext += `\\n\\nCommand exited with code ${msg.exitCode}`;\n\t}\n\tif (msg.truncated && msg.fullOutputPath) {\n\t\ttext += `\\n\\n[Output truncated. Full output: ${msg.fullOutputPath}]`;\n\t}\n\treturn text;\n}\n\nexport function createBranchSummaryMessage(summary: string, fromId: string, timestamp: string): BranchSummaryMessage {\n\treturn {\n\t\trole: \"branchSummary\",\n\t\tsummary,\n\t\tfromId,\n\t\ttimestamp: new Date(timestamp).getTime(),\n\t};\n}\n\nexport function createCompactionSummaryMessage(\n\tsummary: string,\n\ttokensBefore: number,\n\ttimestamp: string,\n\tdetails?: unknown,\n): CompactionSummaryMessage {\n\treturn {\n\t\trole: \"compactionSummary\",\n\t\tsummary: summary,\n\t\ttokensBefore,\n\t\tdetails,\n\t\ttimestamp: new Date(timestamp).getTime(),\n\t};\n}\n\n/** Convert CustomMessageEntry to AgentMessage format */\nexport function createCustomMessage(\n\tcustomType: string,\n\tcontent: string | (TextContent | ImageContent)[],\n\tdisplay: boolean,\n\tdetails: unknown | undefined,\n\ttimestamp: string,\n): CustomMessage {\n\treturn {\n\t\trole: \"custom\",\n\t\tcustomType,\n\t\tcontent,\n\t\tdisplay,\n\t\tdetails,\n\t\ttimestamp: new Date(timestamp).getTime(),\n\t};\n}\n\n/**\n * Transform AgentMessages (including custom types) to LLM-compatible Messages.\n *\n * This is used by:\n * - Agent's transormToLlm option (for prompt calls and queued messages)\n * - Compaction's generateSummary (for summarization)\n * - Custom extensions and tools\n */\nexport function convertToLlm(messages: AgentMessage[]): Message[] {\n\tconst withContextProvenance = <T extends Message>(source: AgentMessage, target: T): T =>\n\t\tcopyContextProvenance(source, target);\n\t// Continuations are append-only here too: the transport array must extend the\n\t// previous request verbatim to keep the provider's cache prefix valid.\n\tconst environmentMessages = new Set<Message>();\n\tconst converted = messages\n\t\t.map((m): Message | undefined => {\n\t\t\tswitch (m.role) {\n\t\t\t\tcase \"bashExecution\":\n\t\t\t\t\t// Skip messages excluded from context (!! prefix)\n\t\t\t\t\tif (m.excludeFromContext) {\n\t\t\t\t\t\treturn undefined;\n\t\t\t\t\t}\n\t\t\t\t\treturn withContextProvenance(m, {\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tcontent: [{ type: \"text\", text: bashExecutionToText(m) }],\n\t\t\t\t\t\ttimestamp: m.timestamp,\n\t\t\t\t\t});\n\t\t\t\tcase \"custom\": {\n\t\t\t\t\tif (isContextExcludedCustomMessage(m.customType)) {\n\t\t\t\t\t\treturn undefined;\n\t\t\t\t\t}\n\n\t\t\t\t\tconst content = typeof m.content === \"string\" ? [{ type: \"text\" as const, text: m.content }] : m.content;\n\t\t\t\t\tconst converted = withContextProvenance(m, {\n\t\t\t\t\t\trole: \"user\" as const,\n\t\t\t\t\t\tcontent,\n\t\t\t\t\t\ttimestamp: m.timestamp,\n\t\t\t\t\t});\n\t\t\t\t\tif (m.customType === ENVIRONMENT_CONTEXT_MESSAGE_TYPE) environmentMessages.add(converted);\n\t\t\t\t\treturn converted;\n\t\t\t\t}\n\t\t\t\tcase \"branchSummary\":\n\t\t\t\t\treturn withContextProvenance(m, {\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tcontent: [{ type: \"text\" as const, text: BRANCH_SUMMARY_PREFIX + m.summary + BRANCH_SUMMARY_SUFFIX }],\n\t\t\t\t\t\ttimestamp: m.timestamp,\n\t\t\t\t\t});\n\t\t\t\tcase \"compactionSummary\":\n\t\t\t\t\treturn withContextProvenance(m, {\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t{ type: \"text\" as const, text: COMPACTION_SUMMARY_PREFIX + m.summary + COMPACTION_SUMMARY_SUFFIX },\n\t\t\t\t\t\t],\n\t\t\t\t\t\ttimestamp: m.timestamp,\n\t\t\t\t\t});\n\t\t\t\tcase \"configurationUpdate\":\n\t\t\t\t\treturn m;\n\t\t\t\tcase \"user\":\n\t\t\t\tcase \"assistant\":\n\t\t\t\tcase \"toolResult\":\n\t\t\t\t\treturn m;\n\t\t\t\tdefault:\n\t\t\t\t\t// biome-ignore lint/correctness/noSwitchDeclarations: fine\n\t\t\t\t\tconst _exhaustiveCheck: never = m;\n\t\t\t\t\treturn undefined;\n\t\t\t}\n\t\t})\n\t\t.filter((m) => m !== undefined);\n\t// Failed provider turns (stopReason error/aborted) must never reach an LLM\n\t// request: lanes that build requests straight from this output (claude-sdk\n\t// prompt bridge, cursor turns) would otherwise replay their partial text and\n\t// unexecuted tool calls, and token estimation would count them. Dropping is\n\t// deterministic per session state, so the cache-prefix guarantee above holds.\n\t// senpi#2118: the environment context then becomes the leading block of the\n\t// user message it precedes, so strict-alternation chat templates never see\n\t// two consecutive user messages.\n\treturn foldEnvironmentContextIntoNextUserMessage(dropFailedAssistantTurns(converted), environmentMessages);\n}\n\n// ============================================================================\n// Transport image budget\n// ============================================================================\n\n/**\n * Cap on inline image base64 (characters approximately equal wire bytes) per\n * provider request. Anthropic documents a 32 MB request limit; reserving the\n * remainder for text and request overhead keeps the transport below that wall.\n */\nexport const TRANSPORT_IMAGE_BUDGET_BYTES = 24 * 1024 * 1024;\n\nexport const IMAGE_ELISION_PLACEHOLDER =\n\t\"[Image elided: an older image was removed to keep the request within the provider's size limit. Re-read the source file if you need to view it again.]\";\n\nexport const BLOCKED_IMAGE_PLACEHOLDER = \"Image reading is disabled.\";\n\nexport interface ElideOldImagesOptions {\n\t/** Cumulative inline image base64 budget. Defaults to TRANSPORT_IMAGE_BUDGET_BYTES. */\n\tbudgetBytes?: number;\n\t/** Newest image blocks always kept regardless of budget. They still consume it. Defaults to 1. */\n\talwaysKeepNewest?: number;\n\t/** Keep at most this many images from turns completed by a later assistant response. */\n\tmaxHistoricalImages?: number;\n}\n\nexport interface TransportConvertOptions extends ElideOldImagesOptions {\n\t/** Replace every image when the images.blockImages setting is enabled. */\n\tblockImages: boolean;\n}\n\n/** Drop consecutive duplicates of a placeholder produced by adjacent image replacements. */\nexport function dedupeConsecutivePlaceholder(\n\tcontent: (TextContent | ImageContent)[],\n\tplaceholder: string,\n): (TextContent | ImageContent)[] {\n\treturn content.filter(\n\t\t(block, index, blocks) =>\n\t\t\t!(\n\t\t\t\tblock.type === \"text\" &&\n\t\t\t\tblock.text === placeholder &&\n\t\t\t\tindex > 0 &&\n\t\t\t\tblocks[index - 1].type === \"text\" &&\n\t\t\t\t(blocks[index - 1] as TextContent).text === placeholder\n\t\t\t),\n\t);\n}\n\n/**\n * Bound inline image payload at request-build time. Images are considered from\n * newest to oldest. Once an image would exceed the budget, it and every older\n * image are replaced by a text placeholder. The newest protected blocks are\n * kept regardless of size, but still consume the budget.\n *\n * The input and persisted session remain untouched. If every image fits, the\n * original array reference is returned.\n */\nexport function elideOldImages(messages: Message[], options?: ElideOldImagesOptions): Message[] {\n\tconst budgetBytes = options?.budgetBytes ?? TRANSPORT_IMAGE_BUDGET_BYTES;\n\tconst alwaysKeepNewest = options?.alwaysKeepNewest ?? 1;\n\tconst maxHistoricalImages = options?.maxHistoricalImages;\n\tconst limitHistory = maxHistoricalImages !== undefined;\n\tconst lastAssistantIndex = messages.findLastIndex((message) => message.role === \"assistant\");\n\tconst imagesToElide = new Set<string>();\n\tlet keptImages = 0;\n\tlet keptHistoricalImages = 0;\n\tlet imageBytes = 0;\n\tlet cutoffReached = false;\n\n\tfor (let messageIndex = messages.length - 1; messageIndex >= 0; messageIndex--) {\n\t\tconst message = messages[messageIndex];\n\t\tif ((message.role !== \"user\" && message.role !== \"toolResult\") || !Array.isArray(message.content)) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tfor (let blockIndex = message.content.length - 1; blockIndex >= 0; blockIndex--) {\n\t\t\tconst block = message.content[blockIndex];\n\t\t\tif (block.type !== \"image\") continue;\n\t\t\tconst isCurrentTurn = limitHistory && messageIndex > lastAssistantIndex;\n\t\t\tconst isProtectedHistory = limitHistory && !isCurrentTurn && keptHistoricalImages < maxHistoricalImages;\n\t\t\tconst exceedsHistoryLimit = limitHistory && !isCurrentTurn && !isProtectedHistory;\n\n\t\t\tif (exceedsHistoryLimit || (cutoffReached && !isCurrentTurn && !isProtectedHistory)) {\n\t\t\t\timagesToElide.add(`${messageIndex}:${blockIndex}`);\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (\n\t\t\t\tisCurrentTurn ||\n\t\t\t\tisProtectedHistory ||\n\t\t\t\tkeptImages < alwaysKeepNewest ||\n\t\t\t\timageBytes + block.data.length <= budgetBytes\n\t\t\t) {\n\t\t\t\tkeptImages++;\n\t\t\t\tif (!isCurrentTurn) keptHistoricalImages++;\n\t\t\t\timageBytes += block.data.length;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tcutoffReached = true;\n\t\t\timagesToElide.add(`${messageIndex}:${blockIndex}`);\n\t\t}\n\t}\n\n\tif (imagesToElide.size === 0) return messages;\n\n\treturn messages.map((message, messageIndex) => {\n\t\tif ((message.role !== \"user\" && message.role !== \"toolResult\") || !Array.isArray(message.content)) {\n\t\t\treturn message;\n\t\t}\n\t\tif (!message.content.some((_block, blockIndex) => imagesToElide.has(`${messageIndex}:${blockIndex}`))) {\n\t\t\treturn message;\n\t\t}\n\n\t\tconst replaced = message.content.map((block, blockIndex): TextContent | ImageContent =>\n\t\t\timagesToElide.has(`${messageIndex}:${blockIndex}`) ? { type: \"text\", text: IMAGE_ELISION_PLACEHOLDER } : block,\n\t\t);\n\t\treturn {\n\t\t\t...message,\n\t\t\tcontent: dedupeConsecutivePlaceholder(replaced, IMAGE_ELISION_PLACEHOLDER) as typeof message.content,\n\t\t};\n\t});\n}\n\n/** Convert messages for the main provider transport and apply its image policy. */\nexport function convertToLlmForTransport(messages: AgentMessage[], options: TransportConvertOptions): Message[] {\n\tconst converted = convertToLlm(messages);\n\tif (!options.blockImages) return elideOldImages(converted, options);\n\n\treturn converted.map((message) => {\n\t\tif ((message.role !== \"user\" && message.role !== \"toolResult\") || !Array.isArray(message.content)) {\n\t\t\treturn message;\n\t\t}\n\t\tif (!message.content.some((block) => block.type === \"image\")) return message;\n\n\t\tconst replaced = message.content.map((block): TextContent | ImageContent =>\n\t\t\tblock.type === \"image\" ? { type: \"text\", text: BLOCKED_IMAGE_PLACEHOLDER } : block,\n\t\t);\n\t\treturn {\n\t\t\t...message,\n\t\t\tcontent: dedupeConsecutivePlaceholder(replaced, BLOCKED_IMAGE_PLACEHOLDER) as typeof message.content,\n\t\t};\n\t});\n}\n"]}
1
+ {"version":3,"file":"messages.js","sourceRoot":"","sources":["../../src/core/messages.ts"],"names":[],"mappings":"AAAA;;;;;GAK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OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;YACnG,OAAO,OAAO,CAAC;QAChB,CAAC;QACD,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,OAAO,CAAC;YAAE,OAAO,OAAO,CAAC;QAE7E,MAAM,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,EAA8B,EAAE,CAC1E,KAAK,CAAC,IAAI,KAAK,OAAO,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,yBAAyB,EAAE,CAAC,CAAC,CAAC,KAAK,CAClF,CAAC;QACF,OAAO;YACN,GAAG,OAAO;YACV,OAAO,EAAE,4BAA4B,CAAC,QAAQ,EAAE,yBAAyB,CAA2B;SACpG,CAAC;IACH,CAAC,CAAC,CAAC;AACJ,CAAC","sourcesContent":["/**\n * Custom message types and transformers for the coding agent.\n *\n * Extends the base AgentMessage type with coding-agent specific message types,\n * and provides a transformer to convert them to LLM-compatible messages.\n */\n\nimport type { AgentMessage } from \"@earendil-works/pi-agent-core\";\nimport {\n\tcopyContextProvenance,\n\tdropFailedAssistantTurns,\n\ttype ImageContent,\n\ttype Message,\n\ttype TextContent,\n} from \"@earendil-works/pi-ai\";\nimport { ENVIRONMENT_CONTEXT_MESSAGE_TYPE, foldEnvironmentContextIntoNextUserMessage } from \"./environment-context.ts\";\nimport { omitProviderRejectedImages } from \"./provider-rejected-images.ts\";\n\nexport const COMPACTION_SUMMARY_PREFIX = `The conversation history before this point was compacted into the following summary:\n\n<summary>\n`;\n\nexport const COMPACTION_SUMMARY_SUFFIX = `\n</summary>`;\n\nexport const BRANCH_SUMMARY_PREFIX = `The following is a summary of a branch that this conversation came back from:\n\n<summary>\n`;\n\nexport const BRANCH_SUMMARY_SUFFIX = `</summary>`;\n\nexport const GOAL_CONTINUATION_MESSAGE_TYPE = \"goal-continuation\";\n\n/**\n * Per-type exclusion must remain false: compaction and branch summarization\n * inspect entries independently, where excluding this type would hide the live\n * goal-continuation message.\n */\nexport function isContextExcludedCustomMessage(_customType: string): boolean {\n\treturn false;\n}\n\n/**\n * Whole-context filtering is deliberately a no-op. Goal continuations are\n * append-only: dropping an already-sent continuation would rewrite a\n * provider-visible turn, so request N would stop being a prefix of request N+1\n * and every cached token ahead of the edit would be invalidated (full re-read,\n * cost spike, 429 storms in team mode). Continuation history is bounded by\n * normal compaction instead of by per-request deletion.\n */\nexport function filterContextExcludedMessages(messages: AgentMessage[]): AgentMessage[] {\n\treturn messages;\n}\n\n/**\n * Message type for bash executions via the ! command.\n */\nexport interface BashExecutionMessage {\n\trole: \"bashExecution\";\n\tcommand: string;\n\toutput: string;\n\texitCode: number | undefined;\n\tcancelled: boolean;\n\ttruncated: boolean;\n\tfullOutputPath?: string;\n\ttimestamp: number;\n\t/** If true, this message is excluded from LLM context (!! prefix) */\n\texcludeFromContext?: boolean;\n}\n\n/**\n * Message type for extension-injected messages via sendMessage().\n * These are custom messages that extensions can inject into the conversation.\n */\nexport interface CustomMessage<T = unknown> {\n\trole: \"custom\";\n\tcustomType: string;\n\tcontent: string | (TextContent | ImageContent)[];\n\tdisplay: boolean;\n\tdetails?: T;\n\ttimestamp: number;\n}\n\nexport interface BranchSummaryMessage {\n\trole: \"branchSummary\";\n\tsummary: string;\n\tfromId: string | null;\n\ttimestamp: number;\n}\n\nexport interface CompactionSummaryMessage {\n\trole: \"compactionSummary\";\n\tsummary: string;\n\ttokensBefore: number;\n\tdetails?: unknown;\n\ttimestamp: number;\n}\n\nexport interface ConfigurationUpdateMessage {\n\trole: \"configurationUpdate\";\n\tcontent: (TextContent | ImageContent)[];\n\teffort: string;\n\ttimestamp: number;\n}\n\n// Extend CustomAgentMessages via declaration merging\ndeclare module \"@earendil-works/pi-agent-core\" {\n\tinterface CustomAgentMessages {\n\t\tbashExecution: BashExecutionMessage;\n\t\tcustom: CustomMessage;\n\t\tbranchSummary: BranchSummaryMessage;\n\t\tconfigurationUpdate: ConfigurationUpdateMessage;\n\t}\n}\n\n/**\n * Convert a BashExecutionMessage to user message text for LLM context.\n */\nexport function bashExecutionToText(msg: BashExecutionMessage): string {\n\tlet text = `Ran \\`${msg.command}\\`\\n`;\n\tif (msg.output) {\n\t\ttext += `\\`\\`\\`\\n${msg.output}\\n\\`\\`\\``;\n\t} else {\n\t\ttext += \"(no output)\";\n\t}\n\tif (msg.cancelled) {\n\t\ttext += \"\\n\\n(command cancelled)\";\n\t} else if (msg.exitCode !== null && msg.exitCode !== undefined && msg.exitCode !== 0) {\n\t\ttext += `\\n\\nCommand exited with code ${msg.exitCode}`;\n\t}\n\tif (msg.truncated && msg.fullOutputPath) {\n\t\ttext += `\\n\\n[Output truncated. Full output: ${msg.fullOutputPath}]`;\n\t}\n\treturn text;\n}\n\nexport function createBranchSummaryMessage(summary: string, fromId: string, timestamp: string): BranchSummaryMessage {\n\treturn {\n\t\trole: \"branchSummary\",\n\t\tsummary,\n\t\tfromId,\n\t\ttimestamp: new Date(timestamp).getTime(),\n\t};\n}\n\nexport function createCompactionSummaryMessage(\n\tsummary: string,\n\ttokensBefore: number,\n\ttimestamp: string,\n\tdetails?: unknown,\n): CompactionSummaryMessage {\n\treturn {\n\t\trole: \"compactionSummary\",\n\t\tsummary: summary,\n\t\ttokensBefore,\n\t\tdetails,\n\t\ttimestamp: new Date(timestamp).getTime(),\n\t};\n}\n\n/** Convert CustomMessageEntry to AgentMessage format */\nexport function createCustomMessage(\n\tcustomType: string,\n\tcontent: string | (TextContent | ImageContent)[],\n\tdisplay: boolean,\n\tdetails: unknown | undefined,\n\ttimestamp: string,\n): CustomMessage {\n\treturn {\n\t\trole: \"custom\",\n\t\tcustomType,\n\t\tcontent,\n\t\tdisplay,\n\t\tdetails,\n\t\ttimestamp: new Date(timestamp).getTime(),\n\t};\n}\n\n/**\n * Transform AgentMessages (including custom types) to LLM-compatible Messages.\n *\n * This is used by:\n * - Agent's transormToLlm option (for prompt calls and queued messages)\n * - Compaction's generateSummary (for summarization)\n * - Custom extensions and tools\n */\nexport function convertToLlm(messages: AgentMessage[]): Message[] {\n\tconst withContextProvenance = <T extends Message>(source: AgentMessage, target: T): T =>\n\t\tcopyContextProvenance(source, target);\n\t// Continuations are append-only here too: the transport array must extend the\n\t// previous request verbatim to keep the provider's cache prefix valid.\n\tconst environmentMessages = new Set<Message>();\n\tconst converted = messages\n\t\t.map((m): Message | undefined => {\n\t\t\tswitch (m.role) {\n\t\t\t\tcase \"bashExecution\":\n\t\t\t\t\t// Skip messages excluded from context (!! prefix)\n\t\t\t\t\tif (m.excludeFromContext) {\n\t\t\t\t\t\treturn undefined;\n\t\t\t\t\t}\n\t\t\t\t\treturn withContextProvenance(m, {\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tcontent: [{ type: \"text\", text: bashExecutionToText(m) }],\n\t\t\t\t\t\ttimestamp: m.timestamp,\n\t\t\t\t\t});\n\t\t\t\tcase \"custom\": {\n\t\t\t\t\tif (isContextExcludedCustomMessage(m.customType)) {\n\t\t\t\t\t\treturn undefined;\n\t\t\t\t\t}\n\n\t\t\t\t\tconst content = typeof m.content === \"string\" ? [{ type: \"text\" as const, text: m.content }] : m.content;\n\t\t\t\t\tconst converted = withContextProvenance(m, {\n\t\t\t\t\t\trole: \"user\" as const,\n\t\t\t\t\t\tcontent,\n\t\t\t\t\t\ttimestamp: m.timestamp,\n\t\t\t\t\t});\n\t\t\t\t\tif (m.customType === ENVIRONMENT_CONTEXT_MESSAGE_TYPE) environmentMessages.add(converted);\n\t\t\t\t\treturn converted;\n\t\t\t\t}\n\t\t\t\tcase \"branchSummary\":\n\t\t\t\t\treturn withContextProvenance(m, {\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tcontent: [{ type: \"text\" as const, text: BRANCH_SUMMARY_PREFIX + m.summary + BRANCH_SUMMARY_SUFFIX }],\n\t\t\t\t\t\ttimestamp: m.timestamp,\n\t\t\t\t\t});\n\t\t\t\tcase \"compactionSummary\":\n\t\t\t\t\treturn withContextProvenance(m, {\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t{ type: \"text\" as const, text: COMPACTION_SUMMARY_PREFIX + m.summary + COMPACTION_SUMMARY_SUFFIX },\n\t\t\t\t\t\t],\n\t\t\t\t\t\ttimestamp: m.timestamp,\n\t\t\t\t\t});\n\t\t\t\tcase \"configurationUpdate\":\n\t\t\t\t\treturn m;\n\t\t\t\tcase \"user\":\n\t\t\t\tcase \"assistant\":\n\t\t\t\tcase \"toolResult\":\n\t\t\t\t\treturn m;\n\t\t\t\tdefault:\n\t\t\t\t\t// biome-ignore lint/correctness/noSwitchDeclarations: fine\n\t\t\t\t\tconst _exhaustiveCheck: never = m;\n\t\t\t\t\treturn undefined;\n\t\t\t}\n\t\t})\n\t\t.filter((m) => m !== undefined);\n\t// Failed provider turns (stopReason error/aborted) must never reach an LLM\n\t// request: lanes that build requests straight from this output (claude-sdk\n\t// prompt bridge, cursor turns) would otherwise replay their partial text and\n\t// unexecuted tool calls, and token estimation would count them. Dropping is\n\t// deterministic per session state, so the cache-prefix guarantee above holds.\n\t// senpi#2118: the environment context then becomes the leading block of the\n\t// user message it precedes, so strict-alternation chat templates never see\n\t// two consecutive user messages. senpi#2170: images a failed turn proves the\n\t// provider rejected are replaced first, while the failed turn is still visible.\n\treturn foldEnvironmentContextIntoNextUserMessage(\n\t\tdropFailedAssistantTurns(omitProviderRejectedImages(converted)),\n\t\tenvironmentMessages,\n\t);\n}\n\n// ============================================================================\n// Transport image budget\n// ============================================================================\n\n/**\n * Cap on inline image base64 (characters approximately equal wire bytes) per\n * provider request. Anthropic documents a 32 MB request limit; reserving the\n * remainder for text and request overhead keeps the transport below that wall.\n */\nexport const TRANSPORT_IMAGE_BUDGET_BYTES = 24 * 1024 * 1024;\n\nexport const IMAGE_ELISION_PLACEHOLDER =\n\t\"[Image elided: an older image was removed to keep the request within the provider's size limit. Re-read the source file if you need to view it again.]\";\n\nexport const BLOCKED_IMAGE_PLACEHOLDER = \"Image reading is disabled.\";\n\nexport interface ElideOldImagesOptions {\n\t/** Cumulative inline image base64 budget. Defaults to TRANSPORT_IMAGE_BUDGET_BYTES. */\n\tbudgetBytes?: number;\n\t/** Newest image blocks always kept regardless of budget. They still consume it. Defaults to 1. */\n\talwaysKeepNewest?: number;\n\t/** Keep at most this many images from turns completed by a later assistant response. */\n\tmaxHistoricalImages?: number;\n}\n\nexport interface TransportConvertOptions extends ElideOldImagesOptions {\n\t/** Replace every image when the images.blockImages setting is enabled. */\n\tblockImages: boolean;\n}\n\n/** Drop consecutive duplicates of a placeholder produced by adjacent image replacements. */\nexport function dedupeConsecutivePlaceholder(\n\tcontent: (TextContent | ImageContent)[],\n\tplaceholder: string,\n): (TextContent | ImageContent)[] {\n\treturn content.filter(\n\t\t(block, index, blocks) =>\n\t\t\t!(\n\t\t\t\tblock.type === \"text\" &&\n\t\t\t\tblock.text === placeholder &&\n\t\t\t\tindex > 0 &&\n\t\t\t\tblocks[index - 1].type === \"text\" &&\n\t\t\t\t(blocks[index - 1] as TextContent).text === placeholder\n\t\t\t),\n\t);\n}\n\n/**\n * Bound inline image payload at request-build time. Images are considered from\n * newest to oldest. Once an image would exceed the budget, it and every older\n * image are replaced by a text placeholder. The newest protected blocks are\n * kept regardless of size, but still consume the budget.\n *\n * The input and persisted session remain untouched. If every image fits, the\n * original array reference is returned.\n */\nexport function elideOldImages(messages: Message[], options?: ElideOldImagesOptions): Message[] {\n\tconst budgetBytes = options?.budgetBytes ?? TRANSPORT_IMAGE_BUDGET_BYTES;\n\tconst alwaysKeepNewest = options?.alwaysKeepNewest ?? 1;\n\tconst maxHistoricalImages = options?.maxHistoricalImages;\n\tconst limitHistory = maxHistoricalImages !== undefined;\n\tconst lastAssistantIndex = messages.findLastIndex((message) => message.role === \"assistant\");\n\tconst imagesToElide = new Set<string>();\n\tlet keptImages = 0;\n\tlet keptHistoricalImages = 0;\n\tlet imageBytes = 0;\n\tlet cutoffReached = false;\n\n\tfor (let messageIndex = messages.length - 1; messageIndex >= 0; messageIndex--) {\n\t\tconst message = messages[messageIndex];\n\t\tif ((message.role !== \"user\" && message.role !== \"toolResult\") || !Array.isArray(message.content)) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tfor (let blockIndex = message.content.length - 1; blockIndex >= 0; blockIndex--) {\n\t\t\tconst block = message.content[blockIndex];\n\t\t\tif (block.type !== \"image\") continue;\n\t\t\tconst isCurrentTurn = limitHistory && messageIndex > lastAssistantIndex;\n\t\t\tconst isProtectedHistory = limitHistory && !isCurrentTurn && keptHistoricalImages < maxHistoricalImages;\n\t\t\tconst exceedsHistoryLimit = limitHistory && !isCurrentTurn && !isProtectedHistory;\n\n\t\t\tif (exceedsHistoryLimit || (cutoffReached && !isCurrentTurn && !isProtectedHistory)) {\n\t\t\t\timagesToElide.add(`${messageIndex}:${blockIndex}`);\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (\n\t\t\t\tisCurrentTurn ||\n\t\t\t\tisProtectedHistory ||\n\t\t\t\tkeptImages < alwaysKeepNewest ||\n\t\t\t\timageBytes + block.data.length <= budgetBytes\n\t\t\t) {\n\t\t\t\tkeptImages++;\n\t\t\t\tif (!isCurrentTurn) keptHistoricalImages++;\n\t\t\t\timageBytes += block.data.length;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tcutoffReached = true;\n\t\t\timagesToElide.add(`${messageIndex}:${blockIndex}`);\n\t\t}\n\t}\n\n\tif (imagesToElide.size === 0) return messages;\n\n\treturn messages.map((message, messageIndex) => {\n\t\tif ((message.role !== \"user\" && message.role !== \"toolResult\") || !Array.isArray(message.content)) {\n\t\t\treturn message;\n\t\t}\n\t\tif (!message.content.some((_block, blockIndex) => imagesToElide.has(`${messageIndex}:${blockIndex}`))) {\n\t\t\treturn message;\n\t\t}\n\n\t\tconst replaced = message.content.map((block, blockIndex): TextContent | ImageContent =>\n\t\t\timagesToElide.has(`${messageIndex}:${blockIndex}`) ? { type: \"text\", text: IMAGE_ELISION_PLACEHOLDER } : block,\n\t\t);\n\t\treturn {\n\t\t\t...message,\n\t\t\tcontent: dedupeConsecutivePlaceholder(replaced, IMAGE_ELISION_PLACEHOLDER) as typeof message.content,\n\t\t};\n\t});\n}\n\n/** Convert messages for the main provider transport and apply its image policy. */\nexport function convertToLlmForTransport(messages: AgentMessage[], options: TransportConvertOptions): Message[] {\n\tconst converted = convertToLlm(messages);\n\tif (!options.blockImages) return elideOldImages(converted, options);\n\n\treturn converted.map((message) => {\n\t\tif ((message.role !== \"user\" && message.role !== \"toolResult\") || !Array.isArray(message.content)) {\n\t\t\treturn message;\n\t\t}\n\t\tif (!message.content.some((block) => block.type === \"image\")) return message;\n\n\t\tconst replaced = message.content.map((block): TextContent | ImageContent =>\n\t\t\tblock.type === \"image\" ? { type: \"text\", text: BLOCKED_IMAGE_PLACEHOLDER } : block,\n\t\t);\n\t\treturn {\n\t\t\t...message,\n\t\t\tcontent: dedupeConsecutivePlaceholder(replaced, BLOCKED_IMAGE_PLACEHOLDER) as typeof message.content,\n\t\t};\n\t});\n}\n"]}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Provider image rejections (senpi#2170). A provider that rejects an image
3
+ * rejects every later request that replays it, and `dropFailedAssistantTurns`
4
+ * removes the failed turn but not the image, so one bad image bricked the
5
+ * session. The failed assistant turn is persisted with its error, so the
6
+ * rejection is derived from history on every request (and after a restart):
7
+ * images no successful response had accepted before an image-rejection failure
8
+ * are replaced by a placeholder naming their source.
9
+ */
10
+ interface TurnMessage {
11
+ role: string;
12
+ }
13
+ export declare function isProviderImageRejection(errorMessage: string | undefined): boolean;
14
+ export declare function rejectedImagePlaceholder(source: string): string;
15
+ export declare function rejectedImageSources(messages: readonly TurnMessage[], failedTurn: TurnMessage): string[];
16
+ export declare function omitProviderRejectedImages<T extends TurnMessage>(messages: T[]): T[];
17
+ export {};
18
+ //# sourceMappingURL=provider-rejected-images.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider-rejected-images.d.ts","sourceRoot":"","sources":["../../src/core/provider-rejected-images.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAWH,UAAU,WAAW;IACpB,IAAI,EAAE,MAAM,CAAC;CACb;AAQD,wBAAgB,wBAAwB,CAAC,YAAY,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAElF;AAED,wBAAgB,wBAAwB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAE/D;AAiED,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,SAAS,WAAW,EAAE,EAAE,UAAU,EAAE,WAAW,GAAG,MAAM,EAAE,CAGxG;AAED,wBAAgB,0BAA0B,CAAC,CAAC,SAAS,WAAW,EAAE,QAAQ,EAAE,CAAC,EAAE,GAAG,CAAC,EAAE,CAmBpF"}