ceulen 0.5.0 → 0.7.0

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 (358) hide show
  1. package/CHANGELOG.md +317 -12
  2. package/README.md +87 -91
  3. package/extensions/lib/advisor-marker.ts +33 -0
  4. package/extensions/lib/panel.ts +25 -2
  5. package/extensions/lib/rate.ts +24 -0
  6. package/extensions/lib/registry.ts +93 -7
  7. package/extensions/lib/tools.ts +47 -0
  8. package/extensions/lib/usage-store.ts +33 -0
  9. package/extensions/modules/advisor/commands/advisor.ts +244 -0
  10. package/extensions/modules/advisor/configPanel.ts +194 -0
  11. package/extensions/modules/advisor/index.ts +217 -0
  12. package/extensions/modules/advisor/lib/config.ts +248 -0
  13. package/extensions/modules/advisor/lib/emission-guard.ts +153 -0
  14. package/extensions/modules/advisor/lib/isolated-model.ts +160 -0
  15. package/extensions/modules/advisor/lib/model-picker.ts +47 -0
  16. package/extensions/modules/advisor/lib/watcher.ts +286 -0
  17. package/extensions/modules/advisor/test/compliance.test.ts +125 -0
  18. package/extensions/modules/advisor/test/config.test.ts +202 -0
  19. package/extensions/modules/advisor/test/configPanel.test.ts +223 -0
  20. package/extensions/modules/advisor/test/emission-guard.test.ts +201 -0
  21. package/extensions/modules/advisor/test/isolated-model.test.ts +311 -0
  22. package/extensions/modules/advisor/test/model-picker.test.ts +32 -0
  23. package/extensions/modules/advisor/test/module.test.ts +586 -0
  24. package/extensions/modules/advisor/test/watcher.test.ts +478 -0
  25. package/extensions/modules/classifier/configPanel.ts +104 -0
  26. package/extensions/modules/classifier/index.ts +297 -0
  27. package/extensions/modules/classifier/lib/settings.ts +95 -0
  28. package/extensions/modules/classifier/test/classifier.test.ts +475 -0
  29. package/extensions/modules/composer/index.ts +152 -3
  30. package/extensions/modules/composer/lib/footer.ts +119 -0
  31. package/extensions/modules/composer/lib/shapes.ts +265 -26
  32. package/extensions/modules/composer/test/editor.test.ts +1 -1
  33. package/extensions/modules/composer/test/footer.test.ts +96 -0
  34. package/extensions/modules/composer/test/module.test.ts +113 -11
  35. package/extensions/modules/composer/test/screen.test.ts +29 -1
  36. package/extensions/modules/composer/test/shapes.test.ts +224 -8
  37. package/extensions/modules/config/defaultTools.ts +96 -0
  38. package/extensions/modules/config/index.ts +147 -7
  39. package/extensions/modules/config/piSettings.ts +254 -69
  40. package/extensions/modules/config/test/defaultTools.test.ts +122 -0
  41. package/extensions/modules/config/test/menu.test.ts +64 -0
  42. package/extensions/modules/config/test/panel.test.ts +38 -23
  43. package/extensions/modules/config/test/piSettings.test.ts +130 -3
  44. package/extensions/modules/config/test/renderTools.test.ts +71 -0
  45. package/extensions/modules/config/test/tools.test.ts +146 -0
  46. package/extensions/modules/fff/index.ts +2094 -0
  47. package/extensions/modules/fff/lib/query.ts +129 -0
  48. package/extensions/modules/fff/test/index.test.ts +607 -0
  49. package/extensions/modules/fff/test/query.test.ts +71 -0
  50. package/extensions/modules/munin/configPanel.ts +105 -0
  51. package/extensions/modules/munin/index.ts +641 -0
  52. package/extensions/modules/munin/lib/helpers.ts +471 -0
  53. package/extensions/modules/munin/lib/retry.ts +20 -0
  54. package/extensions/modules/munin/lib/sdk.ts +218 -0
  55. package/extensions/modules/munin/test/config.test.ts +168 -0
  56. package/extensions/modules/munin/test/configPanel.test.ts +161 -0
  57. package/extensions/modules/munin/test/extension.test.ts +224 -0
  58. package/extensions/modules/munin/test/helpers.test.ts +523 -0
  59. package/extensions/modules/munin/test/retry.test.ts +145 -0
  60. package/extensions/modules/munin/test/stale-protocol.test.ts +554 -0
  61. package/extensions/modules/plan/configPanel.ts +152 -0
  62. package/extensions/modules/plan/index.ts +917 -0
  63. package/extensions/modules/plan/lib/plan-tools.ts +72 -0
  64. package/extensions/modules/plan/lib/prompt.ts +65 -0
  65. package/extensions/modules/plan/lib/settings.ts +166 -0
  66. package/extensions/modules/plan/lib/shell-gate.ts +200 -0
  67. package/extensions/modules/plan/test/configPanel.test.ts +96 -0
  68. package/extensions/modules/plan/test/harness.ts +133 -0
  69. package/extensions/modules/plan/test/module.test.ts +305 -0
  70. package/extensions/modules/plan/test/settings.test.ts +114 -0
  71. package/extensions/modules/plan/test/shell-gate.test.ts +103 -0
  72. package/extensions/modules/ponytail/index.ts +23 -73
  73. package/extensions/modules/ponytail/lib/config.ts +1 -6
  74. package/extensions/modules/ponytail/test/extension.test.ts +1 -85
  75. package/extensions/modules/repair/configPanel.ts +96 -0
  76. package/extensions/modules/repair/index.ts +414 -0
  77. package/extensions/modules/repair/lib/auto-bg.ts +333 -0
  78. package/extensions/modules/repair/lib/edit-repair.ts +155 -0
  79. package/extensions/modules/repair/lib/editor.ts +251 -0
  80. package/extensions/modules/repair/lib/guards.ts +34 -0
  81. package/extensions/modules/repair/lib/input-repair.ts +324 -0
  82. package/extensions/modules/repair/lib/patch.ts +481 -0
  83. package/extensions/modules/repair/lib/settings.ts +114 -0
  84. package/extensions/modules/repair/test/auto-bg.test.ts +405 -0
  85. package/extensions/modules/repair/test/edit-repair.test.ts +224 -0
  86. package/extensions/modules/repair/test/editor.test.ts +141 -0
  87. package/extensions/modules/repair/test/guards.test.ts +64 -0
  88. package/extensions/modules/repair/test/input-repair.test.ts +231 -0
  89. package/extensions/modules/repair/test/patch.test.ts +563 -0
  90. package/extensions/modules/repair/test/settings.test.ts +167 -0
  91. package/extensions/modules/repair/test/wrapper.test.ts +210 -0
  92. package/extensions/modules/router/commands/commands.ts +4 -0
  93. package/extensions/modules/router/lib/client.ts +61 -0
  94. package/extensions/modules/router/lib/provider.ts +34 -13
  95. package/extensions/modules/router/lib/systemone.ts +152 -0
  96. package/extensions/modules/router/test/unit.test.ts +179 -0
  97. package/extensions/modules/rtk/findFallback.js +68 -0
  98. package/extensions/modules/rtk/index.ts +248 -0
  99. package/extensions/modules/rtk/safe-rewrite.js +74 -0
  100. package/extensions/modules/rtk/test/findFallback.test.js +66 -0
  101. package/extensions/modules/rtk/test/index.test.js +131 -0
  102. package/extensions/modules/rtk/test/safe-rewrite.test.js +96 -0
  103. package/extensions/modules/rtk/version-gate.js +34 -0
  104. package/extensions/modules/serena/index.ts +629 -0
  105. package/extensions/modules/serena/lib/detect.ts +73 -0
  106. package/extensions/modules/serena/lib/env.ts +76 -0
  107. package/extensions/modules/serena/lib/guidance.ts +26 -0
  108. package/extensions/modules/serena/lib/normalize.ts +27 -0
  109. package/extensions/modules/serena/lib/symbol-key.ts +20 -0
  110. package/extensions/modules/serena/lib/truncate.ts +20 -0
  111. package/extensions/modules/serena/test/env.test.ts +122 -0
  112. package/extensions/modules/serena/test/index.test.ts +347 -0
  113. package/extensions/modules/serena/test/worker.test.ts +562 -0
  114. package/extensions/modules/serena/worker.ts +858 -0
  115. package/extensions/modules/steering/configPanel.ts +122 -0
  116. package/extensions/modules/steering/index.ts +616 -0
  117. package/extensions/modules/steering/lib/anchor.ts +175 -0
  118. package/extensions/modules/steering/lib/errors.ts +253 -0
  119. package/extensions/modules/steering/lib/family.ts +33 -0
  120. package/extensions/modules/steering/lib/guidance.ts +172 -0
  121. package/extensions/modules/steering/lib/history.ts +170 -0
  122. package/extensions/modules/steering/lib/settings.ts +127 -0
  123. package/extensions/modules/steering/test/anchor-regressions.test.ts +93 -0
  124. package/extensions/modules/steering/test/anchor-wiring.test.ts +99 -0
  125. package/extensions/modules/steering/test/anchor.test.ts +160 -0
  126. package/extensions/modules/steering/test/cache-stats.test.ts +43 -0
  127. package/extensions/modules/steering/test/errors.test.ts +309 -0
  128. package/extensions/modules/steering/test/family.test.ts +43 -0
  129. package/extensions/modules/steering/test/guidance.test.ts +357 -0
  130. package/extensions/modules/steering/test/harness.ts +93 -0
  131. package/extensions/modules/steering/test/history.test.ts +118 -0
  132. package/extensions/modules/steering/test/settings.test.ts +182 -0
  133. package/extensions/modules/steering/test/status.test.ts +71 -0
  134. package/extensions/modules/steering/test/toolCall.test.ts +99 -0
  135. package/extensions/modules/subagent/agents/planner.md +14 -0
  136. package/extensions/modules/subagent/agents/reviewer.md +42 -0
  137. package/extensions/modules/subagent/agents/scout.md +47 -0
  138. package/extensions/modules/subagent/agents/tester.md +12 -0
  139. package/extensions/modules/subagent/agents/worker.md +18 -0
  140. package/extensions/modules/subagent/configPanel.ts +219 -0
  141. package/extensions/modules/subagent/index.ts +2451 -0
  142. package/extensions/modules/subagent/lib/agents.ts +408 -0
  143. package/extensions/modules/subagent/lib/background.ts +383 -0
  144. package/extensions/modules/subagent/lib/herdr.ts +1019 -0
  145. package/extensions/modules/subagent/lib/history.ts +126 -0
  146. package/extensions/modules/subagent/lib/model.ts +156 -0
  147. package/extensions/modules/subagent/lib/render.ts +335 -0
  148. package/extensions/modules/subagent/lib/result.ts +110 -0
  149. package/extensions/modules/subagent/lib/roles.ts +296 -0
  150. package/extensions/modules/subagent/lib/routing.ts +223 -0
  151. package/extensions/modules/subagent/lib/runner.ts +559 -0
  152. package/extensions/modules/subagent/lib/schema.ts +21 -0
  153. package/extensions/modules/subagent/lib/security.ts +752 -0
  154. package/extensions/modules/subagent/lib/settings.ts +159 -0
  155. package/extensions/modules/subagent/lib/thread-viewer.ts +319 -0
  156. package/extensions/modules/subagent/lib/threads.ts +135 -0
  157. package/extensions/modules/subagent/lib/types.ts +18 -0
  158. package/extensions/modules/subagent/lib/widget.ts +475 -0
  159. package/extensions/modules/subagent/test/configPanel.test.ts +132 -0
  160. package/extensions/modules/subagent/test/herdr-advisor.test.ts +40 -0
  161. package/extensions/modules/subagent/test/routing.test.ts +350 -0
  162. package/extensions/modules/subagent/test/widget.test.ts +208 -0
  163. package/extensions/modules/usage/index.ts +59 -16
  164. package/extensions/modules/usage/test/genrate.test.ts +70 -0
  165. package/extensions/modules/usage/test/parsers.test.ts +13 -0
  166. package/extensions/modules/ux/UPSTREAM +24 -0
  167. package/extensions/modules/ux/index.ts +393 -0
  168. package/extensions/modules/ux/lib/audit.ts +562 -0
  169. package/extensions/modules/ux/lib/config.ts +136 -0
  170. package/extensions/modules/ux/lib/instructions.ts +77 -0
  171. package/extensions/modules/ux/test/audit.test.ts +756 -0
  172. package/extensions/modules/ux/test/extension.test.ts +394 -0
  173. package/extensions/modules/ux/test/instructions.test.ts +19 -0
  174. package/extensions/modules/web/configPanel.ts +193 -0
  175. package/extensions/modules/web/index.ts +978 -0
  176. package/extensions/modules/web/lib/agy.ts +209 -0
  177. package/extensions/modules/web/lib/brave.ts +42 -0
  178. package/extensions/modules/web/lib/cdp.ts +686 -0
  179. package/extensions/modules/web/lib/chatapi.ts +101 -0
  180. package/extensions/modules/web/lib/chatgpt.ts +796 -0
  181. package/extensions/modules/web/lib/chrome.ts +328 -0
  182. package/extensions/modules/web/lib/config.ts +136 -0
  183. package/extensions/modules/web/lib/content.ts +99 -0
  184. package/extensions/modules/web/lib/crawl4ai.ts +172 -0
  185. package/extensions/modules/web/lib/extract.ts +235 -0
  186. package/extensions/modules/web/lib/firecrawl.ts +56 -0
  187. package/extensions/modules/web/lib/format.ts +197 -0
  188. package/extensions/modules/web/lib/gemini-auth.ts +286 -0
  189. package/extensions/modules/web/lib/gemini-dr.ts +338 -0
  190. package/extensions/modules/web/lib/gemini.ts +551 -0
  191. package/extensions/modules/web/lib/imageapi.ts +559 -0
  192. package/extensions/modules/web/lib/retry.ts +71 -0
  193. package/extensions/modules/web/lib/search.ts +237 -0
  194. package/extensions/modules/web/lib/searxng.ts +69 -0
  195. package/extensions/modules/web/lib/settings.ts +148 -0
  196. package/extensions/modules/web/scripts/cdp-smoke.ts +77 -0
  197. package/extensions/modules/web/scripts/gemini-smoke.ts +107 -0
  198. package/extensions/modules/web/test/agy.test.ts +383 -0
  199. package/extensions/modules/web/test/cdp.test.ts +717 -0
  200. package/extensions/modules/web/test/chat.test.ts +171 -0
  201. package/extensions/modules/web/test/chatgpt.test.ts +633 -0
  202. package/extensions/modules/web/test/chrome.test.ts +282 -0
  203. package/extensions/modules/web/test/config.test.ts +218 -0
  204. package/extensions/modules/web/test/configPanel.test.ts +132 -0
  205. package/extensions/modules/web/test/crawl.test.ts +134 -0
  206. package/extensions/modules/web/test/crawl4ai.test.ts +105 -0
  207. package/extensions/modules/web/test/extension.test.ts +118 -0
  208. package/extensions/modules/web/test/extract.test.ts +436 -0
  209. package/extensions/modules/web/test/fixtures/dr-confirm.bin +12 -0
  210. package/extensions/modules/web/test/fixtures/dr-plan.bin +20 -0
  211. package/extensions/modules/web/test/fixtures/interaction.html +31 -0
  212. package/extensions/modules/web/test/format.test.ts +230 -0
  213. package/extensions/modules/web/test/gemini-auth.test.ts +465 -0
  214. package/extensions/modules/web/test/gemini-dr.test.ts +249 -0
  215. package/extensions/modules/web/test/gemini.test.ts +472 -0
  216. package/extensions/modules/web/test/hook.test.ts +101 -0
  217. package/extensions/modules/web/test/image.test.ts +1089 -0
  218. package/extensions/modules/web/test/retry.test.ts +119 -0
  219. package/extensions/modules/web/test/screenshot.test.ts +109 -0
  220. package/extensions/modules/web/test/search.test.ts +210 -0
  221. package/extensions/modules/web/test/settings.test.ts +165 -0
  222. package/extensions/modules/web/test/skill.test.ts +72 -0
  223. package/extensions/modules/web/types.d.ts +3 -0
  224. package/extensions/modules/web/vendor/package.json +1 -0
  225. package/extensions/modules/web/vendor/readability/LICENSE.md +13 -0
  226. package/extensions/modules/web/vendor/readability/Readability-readerable.js +123 -0
  227. package/extensions/modules/web/vendor/readability/Readability.js +2787 -0
  228. package/extensions/modules/web/vendor/readability/index.d.ts +64 -0
  229. package/extensions/modules/web/vendor/readability/index.js +9 -0
  230. package/extensions/modules/web/vendor/turndown/LICENSE +21 -0
  231. package/extensions/modules/web/vendor/turndown/turndown.cjs.js +784 -0
  232. package/extensions/modules/web/vendor/turndown-plugin-gfm/LICENSE +21 -0
  233. package/extensions/modules/web/vendor/turndown-plugin-gfm/turndown-plugin-gfm.cjs.js +163 -0
  234. package/extensions/modules/zai/configPanel.ts +151 -0
  235. package/extensions/modules/zai/index.ts +186 -0
  236. package/extensions/modules/zai/lib/anthropic.ts +159 -0
  237. package/extensions/modules/zai/lib/settings.ts +198 -0
  238. package/extensions/modules/zai/lib/signing.ts +718 -0
  239. package/extensions/modules/zai/lib/throttle.ts +198 -0
  240. package/extensions/modules/zai/test/anthropic.test.ts +132 -0
  241. package/extensions/modules/zai/test/configPanel.test.ts +207 -0
  242. package/extensions/modules/zai/test/extension.test.ts +249 -0
  243. package/extensions/modules/zai/test/settings.test.ts +183 -0
  244. package/extensions/modules/zai/test/signing-wiring.test.ts +108 -0
  245. package/extensions/modules/zai/test/signing.test.ts +345 -0
  246. package/extensions/modules/zai/test/throttle.test.ts +329 -0
  247. package/package.json +19 -4
  248. package/scripts/validate-themes.mjs +75 -0
  249. package/skills/munin/SKILL.md +117 -0
  250. package/skills/ux-capture/SKILL.md +202 -0
  251. package/skills/ux-design/SKILL.md +279 -0
  252. package/skills/ux-presets/SKILL.md +484 -0
  253. package/skills/ux-routing/SKILL.md +46 -0
  254. package/skills/web/SKILL.md +153 -0
  255. package/themes/alabaster.json +93 -0
  256. package/themes/amethyst.json +96 -0
  257. package/themes/anthracite.json +93 -0
  258. package/themes/basalt.json +91 -0
  259. package/themes/birch.json +95 -0
  260. package/themes/catppuccin-mocha.json +91 -0
  261. package/themes/dark-abyss.json +91 -0
  262. package/themes/dark-arctic.json +104 -0
  263. package/themes/dark-aurora.json +95 -0
  264. package/themes/dark-catppuccin.json +107 -0
  265. package/themes/dark-cavern.json +91 -0
  266. package/themes/dark-celestial.json +106 -0
  267. package/themes/dark-copper.json +95 -0
  268. package/themes/dark-cosmos.json +90 -0
  269. package/themes/dark-cyberpunk.json +102 -0
  270. package/themes/dark-dracula.json +98 -0
  271. package/themes/dark-eclipse.json +91 -0
  272. package/themes/dark-ember.json +95 -0
  273. package/themes/dark-equinox.json +90 -0
  274. package/themes/dark-forest.json +96 -0
  275. package/themes/dark-github.json +105 -0
  276. package/themes/dark-gruvbox.json +112 -0
  277. package/themes/dark-lavender.json +95 -0
  278. package/themes/dark-lunar.json +89 -0
  279. package/themes/dark-midnight.json +95 -0
  280. package/themes/dark-monochrome.json +94 -0
  281. package/themes/dark-monokai.json +98 -0
  282. package/themes/dark-nebula.json +90 -0
  283. package/themes/dark-neon-noir.json +100 -0
  284. package/themes/dark-nord.json +98 -0
  285. package/themes/dark-ocean.json +101 -0
  286. package/themes/dark-one.json +100 -0
  287. package/themes/dark-poimandres.json +115 -0
  288. package/themes/dark-rainforest.json +91 -0
  289. package/themes/dark-reef.json +91 -0
  290. package/themes/dark-retro.json +92 -0
  291. package/themes/dark-rose-pine.json +96 -0
  292. package/themes/dark-sakura.json +95 -0
  293. package/themes/dark-slate.json +95 -0
  294. package/themes/dark-solarized.json +97 -0
  295. package/themes/dark-solstice.json +90 -0
  296. package/themes/dark-starfall.json +91 -0
  297. package/themes/dark-sunset.json +99 -0
  298. package/themes/dark-swamp.json +90 -0
  299. package/themes/dark-synthwave.json +103 -0
  300. package/themes/dark-taiga.json +91 -0
  301. package/themes/dark-terminal.json +95 -0
  302. package/themes/dark-tokyo-night.json +101 -0
  303. package/themes/dark-tundra.json +91 -0
  304. package/themes/dark-twilight.json +91 -0
  305. package/themes/dark-volcanic.json +91 -0
  306. package/themes/dark.json +80 -0
  307. package/themes/graphite.json +92 -0
  308. package/themes/light-arctic.json +107 -0
  309. package/themes/light-aurora-day.json +91 -0
  310. package/themes/light-canyon.json +91 -0
  311. package/themes/light-catppuccin.json +106 -0
  312. package/themes/light-cirrus.json +90 -0
  313. package/themes/light-coral.json +95 -0
  314. package/themes/light-cyberpunk.json +96 -0
  315. package/themes/light-dawn.json +90 -0
  316. package/themes/light-dunes.json +91 -0
  317. package/themes/light-eucalyptus.json +95 -0
  318. package/themes/light-forest.json +100 -0
  319. package/themes/light-frost.json +95 -0
  320. package/themes/light-github.json +115 -0
  321. package/themes/light-glacier.json +91 -0
  322. package/themes/light-gruvbox.json +108 -0
  323. package/themes/light-haze.json +90 -0
  324. package/themes/light-honeycomb.json +95 -0
  325. package/themes/light-lagoon.json +91 -0
  326. package/themes/light-lavender.json +95 -0
  327. package/themes/light-meadow.json +91 -0
  328. package/themes/light-mint.json +95 -0
  329. package/themes/light-monochrome.json +101 -0
  330. package/themes/light-ocean.json +99 -0
  331. package/themes/light-one.json +99 -0
  332. package/themes/light-opal.json +91 -0
  333. package/themes/light-orchard.json +91 -0
  334. package/themes/light-paper.json +95 -0
  335. package/themes/light-poimandres.json +115 -0
  336. package/themes/light-prism.json +90 -0
  337. package/themes/light-retro.json +98 -0
  338. package/themes/light-sand.json +95 -0
  339. package/themes/light-savanna.json +91 -0
  340. package/themes/light-solarized.json +102 -0
  341. package/themes/light-soleil.json +90 -0
  342. package/themes/light-sunset.json +99 -0
  343. package/themes/light-synthwave.json +98 -0
  344. package/themes/light-tokyo-night.json +111 -0
  345. package/themes/light-wetland.json +91 -0
  346. package/themes/light-zenith.json +89 -0
  347. package/themes/light.json +80 -0
  348. package/themes/limestone.json +94 -0
  349. package/themes/mahogany.json +97 -0
  350. package/themes/marble.json +93 -0
  351. package/themes/mirage.json +79 -0
  352. package/themes/obsidian.json +91 -0
  353. package/themes/onyx.json +91 -0
  354. package/themes/pearl.json +93 -0
  355. package/themes/porcelain.json +91 -0
  356. package/themes/quartz.json +96 -0
  357. package/themes/sandstone.json +95 -0
  358. package/themes/titanium.json +90 -0
@@ -0,0 +1,405 @@
1
+ // ponytail: vendored from @bacnh85/pi-model-tools 0.9.5 —
2
+ // extensions/test/unit/bash-auto-bg.test.ts (import path adapted; the env
3
+ // config-toggle block moved to settings.test.ts — enablement/threshold now
4
+ // come from the `repair` settings section, not PI_MODEL_TOOLS_* env vars).
5
+ import assert from "node:assert/strict";
6
+ import { after, afterEach, describe, it } from "node:test";
7
+ import {
8
+ bashAutoBgClause,
9
+ wrapWithAutoBg,
10
+ abortAllBgJobs,
11
+ setBgDeliveryEnabled,
12
+ bgJobCount,
13
+ activeBgLogPaths,
14
+ type AutoBgDeps,
15
+ } from "../lib/auto-bg.js";
16
+ import type { BashOperations } from "@earendil-works/pi-coding-agent";
17
+
18
+ /**
19
+ * Controllable fake BashOperations. Models the real local ops' abort contract:
20
+ * a fired signal rejects the pending exec with Error("aborted").
21
+ */
22
+ function makeFakeOps() {
23
+ type Pending = { resolve: (r: { exitCode: number | null }) => void; reject: (e: Error) => void; options: any };
24
+ const pending: Pending[] = [];
25
+ const ops: BashOperations = {
26
+ exec: (command: string, cwd: string, options: any) =>
27
+ new Promise<{ exitCode: number | null }>((resolve, reject) => {
28
+ options.signal?.addEventListener("abort", () => reject(new Error("aborted")), { once: true });
29
+ pending.push({ resolve, reject, options });
30
+ }),
31
+ };
32
+ return { ops, pending };
33
+ }
34
+
35
+ function makeDeps(thresholdSecs = 0.05): { pi: any; deps: AutoBgDeps; messages: Array<{ message: any; options: any }> } {
36
+ const messages: Array<{ message: any; options: any }> = [];
37
+ const pi = {
38
+ sendMessage(message: unknown, options: unknown) {
39
+ messages.push({ message, options });
40
+ },
41
+ };
42
+ return { pi, deps: { pi: pi as any, thresholdSecs }, messages };
43
+ }
44
+
45
+ const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
46
+
47
+ // Logs the lib deliberately KEEPS (truncated ≥5MB) outlive the job by design —
48
+ // track the ones our tests create and remove them at suite end so runs don't
49
+ // leak 5MB files into the shared tmpdir.
50
+ const keptLogs: string[] = [];
51
+ after(async () => {
52
+ const { rm } = await import("node:fs/promises");
53
+ await Promise.all(keptLogs.map((l) => rm(l, { force: true }).catch(() => {})));
54
+ });
55
+
56
+ describe("bashAutoBgClause", () => {
57
+ it("OFF variant: anti-poll guidance, no wake promise", () => {
58
+ const c = bashAutoBgClause(false, 120);
59
+ assert.match(c, /NEVER wait by looping/);
60
+ assert.match(c, /nohup/);
61
+ assert.doesNotMatch(c, /you will be woken/);
62
+ });
63
+
64
+ it("ON variant: OMP wake contract and threshold", () => {
65
+ const c = bashAutoBgClause(true, 90);
66
+ assert.match(c, /~90s auto-background/);
67
+ assert.match(c, /NEVER poll a backgrounded job/);
68
+ assert.match(c, /you will be woken with its output/);
69
+ assert.match(c, /total-runtime kill/);
70
+ });
71
+
72
+ it("both variants are deterministic single strings (cache-stable description head)", () => {
73
+ assert.equal(bashAutoBgClause(true, 120), bashAutoBgClause(true, 120));
74
+ assert.equal(bashAutoBgClause(false, 120), bashAutoBgClause(false, 120));
75
+ });
76
+ });
77
+
78
+ describe("wrapWithAutoBg", () => {
79
+ // Leftover backgrounded jobs share one module-level registry — abort them so
80
+ // the 4-job cap never leaks across tests.
81
+ afterEach(() => abortAllBgJobs());
82
+
83
+ it("fast command: passthrough result, no receipt, no registry entry", async () => {
84
+ const { ops, pending } = makeFakeOps();
85
+ const { pi, deps, messages } = makeDeps();
86
+ const wrapped = wrapWithAutoBg(ops, deps);
87
+ const p = wrapped.exec("echo hi", "/tmp", { onData: () => {}, timeout: undefined, env: {} });
88
+ await sleep(5);
89
+ assert.equal(pending.length, 1);
90
+ assert.equal(bgJobCount(), 0);
91
+ pending[0].resolve({ exitCode: 0 });
92
+ assert.equal((await p).exitCode, 0);
93
+ assert.equal(messages.length, 0);
94
+ assert.equal(bgJobCount(), 0);
95
+ });
96
+
97
+ it("threshold hit: receipt in tool output, completion follow-up with output tail", async () => {
98
+ const { ops, pending } = makeFakeOps();
99
+ const { pi, deps, messages } = makeDeps(0.05);
100
+ const wrapped = wrapWithAutoBg(ops, deps);
101
+ const chunks: Buffer[] = [];
102
+ const p = wrapped.exec("sleep 30 && echo done", "/tmp", { onData: (d: Buffer) => chunks.push(d), timeout: undefined, env: {} });
103
+
104
+ await sleep(100); // cross the threshold
105
+ assert.equal(bgJobCount(), 1, "job registered");
106
+ assert.equal(messages.length, 0, "no separate receipt message — receipt rides the tool output");
107
+ const receipt = Buffer.concat(chunks).toString("utf8");
108
+ assert.match(receipt, /Backgrounded as job bg-\d+/);
109
+ assert.match(receipt, /NEVER poll/);
110
+ assert.match(receipt, /you will be woken with its output/);
111
+ pending[0].options.onData(Buffer.from("partial out\n")); // post-background output → tail
112
+
113
+ pending[0].resolve({ exitCode: 3 });
114
+ assert.equal((await p).exitCode, 0, "receipt resolves exit 0");
115
+ await sleep(10);
116
+ assert.equal(bgJobCount(), 0, "job removed on completion");
117
+ assert.equal(messages.length, 1, "exactly one wake-up: the completion");
118
+ const { message, options } = messages[0];
119
+ assert.match(message.content, /finished \(exit 3\)/);
120
+ assert.match(message.content, /partial out/);
121
+ assert.equal(message.customType, "ceulen-repair-bg");
122
+ assert.equal(options.deliverAs, "followUp");
123
+ assert.equal(options.triggerTurn, true);
124
+ assert.equal(message.display, true);
125
+ });
126
+
127
+ it("exec failure after backgrounding: failure follow-up with timeout translation", async () => {
128
+ const { ops, pending } = makeFakeOps();
129
+ const { pi, deps, messages } = makeDeps(0.05);
130
+ const wrapped = wrapWithAutoBg(ops, deps);
131
+ const p = wrapped.exec("sleep 30", "/tmp", { onData: () => {}, timeout: 5, env: {} });
132
+ await sleep(100);
133
+ assert.equal(bgJobCount(), 1);
134
+ assert.equal((await p).exitCode, 0);
135
+
136
+ pending[0].reject(new Error("timeout:5"));
137
+ await sleep(10);
138
+ assert.equal(messages.length, 1);
139
+ assert.match(messages[0].message.content, /timed out after 5 seconds/);
140
+ });
141
+
142
+ it("concurrent cap: jobs over cap stay foreground (no receipt)", async () => {
143
+ const { ops, pending } = makeFakeOps();
144
+ const { pi, deps, messages } = makeDeps(0.05);
145
+ const wrapped = wrapWithAutoBg(ops, { ...deps, maxConcurrent: 2 });
146
+ const chunks = [0, 1, 2].map(() => [] as Buffer[]);
147
+ const ps = [0, 1, 2].map((i) => wrapped.exec(`cmd-${i}`, "/tmp", { onData: (d: Buffer) => chunks[i].push(d), timeout: undefined, env: {} }));
148
+ await sleep(100);
149
+ assert.equal(bgJobCount(), 2, "cap respected");
150
+ const text = (i: number) => Buffer.concat(chunks[i]).toString("utf8");
151
+ assert.match(text(0), /Backgrounded as job bg-\d+/);
152
+ assert.match(text(1), /Backgrounded as job bg-\d+/);
153
+ assert.doesNotMatch(text(2), /Backgrounded/, "over-cap job stays foreground");
154
+
155
+ // The foreground job completes first: passthrough, no message.
156
+ pending[2].resolve({ exitCode: 0 });
157
+ assert.equal((await ps[2]).exitCode, 0);
158
+ assert.equal(messages.length, 0, "foreground completion does not message");
159
+ pending[0].resolve({ exitCode: 0 });
160
+ pending[1].resolve({ exitCode: 0 });
161
+ await sleep(10);
162
+ assert.equal(messages.length, 2, "both background jobs delivered wake-ups");
163
+ });
164
+
165
+ it("tool-signal abort during foreground wait rejects; after backgrounding abort is a no-op", async () => {
166
+ const { ops, pending } = makeFakeOps();
167
+ const { pi, deps, messages } = makeDeps(0.05);
168
+ const wrapped = wrapWithAutoBg(ops, deps);
169
+
170
+ // Foreground phase: abort propagates to the ops signal and rejects the tool call.
171
+ const controllerA = new AbortController();
172
+ const pa = wrapped.exec("a", "/tmp", { onData: () => {}, signal: controllerA.signal, timeout: undefined, env: {} });
173
+ await sleep(5);
174
+ assert.equal(pending.length, 1);
175
+ controllerA.abort();
176
+ await assert.rejects(pa, /aborted/);
177
+ assert.ok(pending[0].options.signal.aborted, "abort forwarded to real exec");
178
+
179
+ // Background phase: aborting the (already settled) tool signal changes nothing.
180
+ const controllerB = new AbortController();
181
+ const pb = wrapped.exec("b", "/tmp", { onData: () => {}, signal: controllerB.signal, timeout: undefined, env: {} });
182
+ await sleep(100);
183
+ assert.equal(bgJobCount(), 1);
184
+ assert.equal((await pb).exitCode, 0, "receipt already resolved");
185
+ const bgSignal = pending[1].options.signal as AbortSignal;
186
+ controllerB.abort();
187
+ assert.ok(!bgSignal.aborted, "tool abort NOT forwarded after backgrounding");
188
+ pending[1].resolve({ exitCode: 0 });
189
+ await sleep(10);
190
+ assert.equal(messages.length, 1, "completion still delivered");
191
+ });
192
+
193
+ it("abortAllBgJobs aborts live background execs → failure follow-ups", async () => {
194
+ const { ops, pending } = makeFakeOps();
195
+ const { pi, deps, messages } = makeDeps(0.05);
196
+ const wrapped = wrapWithAutoBg(ops, deps);
197
+ const p = wrapped.exec("sleep 30", "/tmp", { onData: () => {}, timeout: undefined, env: {} });
198
+ await sleep(100);
199
+ assert.equal(bgJobCount(), 1);
200
+ abortAllBgJobs();
201
+ assert.equal(bgJobCount(), 0);
202
+ await sleep(10);
203
+ assert.equal(messages.length, 1);
204
+ assert.match(messages[0].message.content, /Background job bg-\d+ failed: aborted/);
205
+ // The receipt promise already resolved; nothing hangs.
206
+ assert.equal((await p).exitCode, 0);
207
+ });
208
+ });
209
+
210
+ describe("bash auto-bg reviewer round (hardening)", () => {
211
+ // Leftover backgrounded jobs share one module-level registry — abort them so
212
+ // the 4-job cap never leaks across tests. Delivery gate reset per test.
213
+ afterEach(() => {
214
+ abortAllBgJobs(true);
215
+ setBgDeliveryEnabled(true);
216
+ });
217
+
218
+ it("abortAllBgJobs(suppressDelivery) delivers nothing (session_shutdown path)", async () => {
219
+ const { ops, pending } = makeFakeOps();
220
+ const { pi, deps, messages } = makeDeps(0.05);
221
+ const wrapped = wrapWithAutoBg(ops, deps);
222
+ const p = wrapped.exec("sleep 30", "/tmp", { onData: () => {}, timeout: undefined, env: {} });
223
+ await sleep(100);
224
+ assert.equal(bgJobCount(), 1);
225
+ assert.equal((await p).exitCode, 0);
226
+ abortAllBgJobs(true);
227
+ await sleep(10);
228
+ assert.equal(bgJobCount(), 0);
229
+ assert.equal(messages.length, 0, "no wake-up fired into a shutting-down session");
230
+ });
231
+
232
+ it("setBgDeliveryEnabled(false) gates completion wake-ups", async () => {
233
+ const { ops, pending } = makeFakeOps();
234
+ const { pi, deps, messages } = makeDeps(0.05);
235
+ const wrapped = wrapWithAutoBg(ops, deps);
236
+ const p = wrapped.exec("sleep 30", "/tmp", { onData: () => {}, timeout: undefined, env: {} });
237
+ await sleep(100);
238
+ assert.equal((await p).exitCode, 0);
239
+ setBgDeliveryEnabled(false);
240
+ pending[0].resolve({ exitCode: 0 });
241
+ await sleep(10);
242
+ assert.equal(messages.length, 0, "gated completion delivers nothing");
243
+ });
244
+
245
+ it("completion wake-up tail excludes the receipt boilerplate; null exitCode branch covered", async () => {
246
+ const { ops, pending } = makeFakeOps();
247
+ const { pi, deps, messages } = makeDeps(0.05);
248
+ const wrapped = wrapWithAutoBg(ops, deps);
249
+ const p = wrapped.exec("sleep 30", "/tmp", { onData: () => {}, timeout: undefined, env: {} });
250
+ await sleep(100);
251
+ assert.equal(bgJobCount(), 1);
252
+ pending[0].options.onData(Buffer.from("real output\n"));
253
+ pending[0].resolve({ exitCode: null });
254
+ assert.equal((await p).exitCode, 0);
255
+ await sleep(10);
256
+ assert.equal(messages.length, 1);
257
+ const content = String(messages[0].message.content);
258
+ assert.match(content, /terminated without an exit code/);
259
+ assert.match(content, /real output/);
260
+ const tailBlock = content.split("Output (tail):")[1] ?? "";
261
+ assert.ok(tailBlock, "tail block present");
262
+ assert.doesNotMatch(tailBlock, /Backgrounded as job/, "receipt boilerplate must not re-enter the wake-up tail");
263
+ });
264
+
265
+ it("on-disk log: random filename, owner-only perms, content written, deleted after wake-up", async () => {
266
+ const { stat, readFile } = await import("node:fs/promises");
267
+ const { ops, pending } = makeFakeOps();
268
+ const { pi, deps, messages } = makeDeps(0.05);
269
+ const wrapped = wrapWithAutoBg(ops, deps);
270
+ const p = wrapped.exec("sleep 30", "/tmp", { onData: () => {}, timeout: undefined, env: {} });
271
+ await sleep(100);
272
+ assert.equal(bgJobCount(), 1);
273
+ const logs = activeBgLogPaths();
274
+ assert.equal(logs.length, 1, "one live job log");
275
+ const logPath = logs[0];
276
+ assert.match(logPath, /ceulen-repair-bg-[0-9a-f-]{36}\.log$/, "filename carries the ceulen- namespace + a randomUUID");
277
+ pending[0].options.onData(Buffer.from("log line\n"));
278
+ await sleep(30); // allow the async open + write to land
279
+ const st = await stat(logPath);
280
+ assert.equal(st.mode & 0o777, 0o600, "log is owner-only (0600)");
281
+ assert.equal((await readFile(logPath, "utf8")).includes("log line"), true, "post-background output reached the log");
282
+ // Non-truncated log is removed once the wake-up carries the tail.
283
+ pending[0].resolve({ exitCode: 0 });
284
+ await p;
285
+ await sleep(30);
286
+ await assert.rejects(stat(logPath), /ENOENT/, "log deleted after wake-up");
287
+ });
288
+
289
+ it("job finishing before the async log open() resolves leaves no log file (j.finished race)", async () => {
290
+ const { stat } = await import("node:fs/promises");
291
+ const { ops, pending } = makeFakeOps();
292
+ const { pi, deps } = makeDeps(0.05);
293
+ const wrapped = wrapWithAutoBg(ops, deps);
294
+
295
+ const p = wrapped.exec("instant job", "/tmp", { onData: () => {}, timeout: undefined, env: {} });
296
+ await sleep(100); // threshold fires → backgrounded, open() still in flight
297
+ const logPath = activeBgLogPaths()[0];
298
+ // Resolve the exec IMMEDIATELY — finishJob runs before open()'s .then does.
299
+ pending[0].resolve({ exitCode: 0 });
300
+ await p;
301
+ // Wait past the threshold timer AND a tick so the racing open() has landed.
302
+ await sleep(100);
303
+ assert.equal(bgJobCount(), 0, "registry empty");
304
+ await assert.rejects(stat(logPath), /ENOENT/, "no log file remains — race closed the fd and removed it");
305
+ });
306
+ });
307
+
308
+ describe("bash auto-bg log size cap", () => {
309
+ afterEach(() => {
310
+ abortAllBgJobs(true);
311
+ setBgDeliveryEnabled(true);
312
+ });
313
+
314
+ it("chatty job: log capped, wake-up notes truncation", async () => {
315
+ const { stat } = await import("node:fs/promises");
316
+ const { ops, pending } = makeFakeOps();
317
+ const { pi, deps, messages } = makeDeps(0.05);
318
+ const wrapped = wrapWithAutoBg(ops, deps);
319
+ const p = wrapped.exec("yes | head -c 20m", "/tmp", { onData: () => {}, timeout: undefined, env: {} });
320
+ await sleep(100);
321
+ assert.equal(bgJobCount(), 1);
322
+ const mb = Buffer.alloc(1024 * 1024, "x");
323
+ for (let i = 0; i < 10; i++) pending[0].options.onData(mb); // 10MB after backgrounding
324
+ pending[0].resolve({ exitCode: 0 });
325
+ assert.equal((await p).exitCode, 0);
326
+ await sleep(50);
327
+ const content = String(messages[0].message.content);
328
+ assert.match(content, /Full log \(kept, truncated at 5120KB\)/, "wake-up notes the cap");
329
+ const logPath = content.match(/truncated at 5120KB\): (.+)$/m)![1];
330
+ const st = await stat(logPath);
331
+ assert.ok(st.size <= 5 * 1024 * 1024 + 1024 * 1024, `log bounded (got ${st.size})`);
332
+ keptLogs.push(logPath); // ≤ cap + one chunk overshoot
333
+ });
334
+ });
335
+
336
+ describe("bash auto-bg reviewer round 2 (foreground output + log lifecycle)", () => {
337
+ afterEach(() => {
338
+ abortAllBgJobs(true);
339
+ setBgDeliveryEnabled(true);
340
+ });
341
+
342
+ it("fast command: pre-threshold chunks stream to caller unchanged", async () => {
343
+ const { ops, pending } = makeFakeOps();
344
+ const { pi, deps, messages } = makeDeps();
345
+ const wrapped = wrapWithAutoBg(ops, deps);
346
+ const chunks: Buffer[] = [];
347
+ const p = wrapped.exec("echo hi", "/tmp", { onData: (d: Buffer) => chunks.push(d), timeout: undefined, env: {} });
348
+ await sleep(5);
349
+ pending[0].options.onData(Buffer.from("early "));
350
+ pending[0].options.onData(Buffer.from("output\n"));
351
+ pending[0].resolve({ exitCode: 0 });
352
+ await p;
353
+ assert.equal(Buffer.concat(chunks).toString("utf8"), "early output\n", "foreground chunks reach the caller");
354
+ assert.equal(messages.length, 0);
355
+ });
356
+
357
+ it("backgrounded job: pre-threshold output seeds the wake-up tail", async () => {
358
+ const { ops, pending } = makeFakeOps();
359
+ const { pi, deps, messages } = makeDeps(0.05);
360
+ const wrapped = wrapWithAutoBg(ops, deps);
361
+ const chunks: Buffer[] = [];
362
+ const p = wrapped.exec("slow", "/tmp", { onData: (d: Buffer) => chunks.push(d), timeout: undefined, env: {} });
363
+ pending[0].options.onData(Buffer.from("pre-threshold out\n")); // BEFORE threshold
364
+ await sleep(100); // crosses the threshold → backgrounded
365
+ assert.equal(bgJobCount(), 1);
366
+ pending[0].resolve({ exitCode: 0 });
367
+ await p;
368
+ await sleep(10);
369
+ assert.equal(messages.length, 1);
370
+ const content = String(messages[0].message.content);
371
+ assert.match(content, /pre-threshold out/, "wake-up includes output the model already saw");
372
+ // The pre-threshold chunk was ALSO forwarded live (exactly once).
373
+ assert.equal(Buffer.concat(chunks).toString("utf8").split("pre-threshold out\n").length - 1, 1, "forwarded exactly once");
374
+ });
375
+
376
+ it("non-truncated log removed at completion; truncated log kept", async () => {
377
+ const { stat } = await import("node:fs/promises");
378
+ const { ops, pending } = makeFakeOps();
379
+ const { pi, deps, messages } = makeDeps(0.05);
380
+ const wrapped = wrapWithAutoBg(ops, deps);
381
+
382
+ // Non-truncated: file deleted after wake-up.
383
+ const p1 = wrapped.exec("quiet job", "/tmp", { onData: () => {}, timeout: undefined, env: {} });
384
+ await sleep(100);
385
+ const log1 = activeBgLogPaths()[0];
386
+ pending[0].resolve({ exitCode: 0 });
387
+ await p1;
388
+ await sleep(30);
389
+ await assert.rejects(stat(log1), /ENOENT/, "non-truncated log removed");
390
+
391
+ // Truncated: file kept and advertised.
392
+ const p2 = wrapped.exec("chatty job", "/tmp", { onData: () => {}, timeout: undefined, env: {} });
393
+ await sleep(100);
394
+ const log2 = activeBgLogPaths()[0];
395
+ const mb = Buffer.alloc(1024 * 1024, "x");
396
+ for (let i = 0; i < 6; i++) pending[1].options.onData(mb);
397
+ pending[1].resolve({ exitCode: 0 });
398
+ await p2;
399
+ await sleep(30);
400
+ const content = String(messages[1].message.content);
401
+ assert.match(content, /Full log \(kept, truncated at 5120KB\)/);
402
+ await stat(log2); // must still exist
403
+ keptLogs.push(log2);
404
+ });
405
+ });
@@ -0,0 +1,224 @@
1
+ // ponytail: vendored from @bacnh85/pi-model-tools 0.9.5 —
2
+ // extensions/test/unit/edit-repair.test.ts (import path adapted).
3
+ import assert from "node:assert";
4
+ import { describe, it } from "node:test";
5
+ import {
6
+ stripReadContamination,
7
+ findTrimMatch,
8
+ fileBytesForBlock,
9
+ nearestBlock,
10
+ parseFailedEditIndex,
11
+ isEditMismatchError,
12
+ computeRetryEdit,
13
+ stripBom,
14
+ normalizeToLF,
15
+ } from "../lib/edit-repair.js";
16
+
17
+ describe("stripBom / normalizeToLF", () => {
18
+ it("strips a BOM", () => {
19
+ assert.strictEqual(stripBom("\uFEFFhello"), "hello");
20
+ assert.strictEqual(stripBom("hello"), "hello");
21
+ });
22
+ it("normalizes CRLF and lone CR to LF", () => {
23
+ assert.strictEqual(normalizeToLF("a\r\nb\rc\nd"), "a\nb\nc\nd");
24
+ });
25
+ });
26
+
27
+ describe("stripReadContamination", () => {
28
+ it("strips 'Showing lines' notice (lines limit)", () => {
29
+ const code = "const x = 1;\nconst y = 2;";
30
+ const contaminated = `${code}\n\n[Showing lines 1-2 of 50. Use offset=3 to continue.]`;
31
+ const r = stripReadContamination(contaminated);
32
+ assert.strictEqual(r.text, code);
33
+ assert.strictEqual(r.changed, true);
34
+ });
35
+ it("strips 'Showing lines' notice (bytes limit)", () => {
36
+ const code = "const x = 1;";
37
+ const contaminated = `${code}\n\n[Showing lines 1-1 of 10 (50KB limit). Use offset=2 to continue.]`;
38
+ assert.strictEqual(stripReadContamination(contaminated).text, code);
39
+ });
40
+ it("strips 'N more lines in file' notice", () => {
41
+ const code = "const x = 1;";
42
+ const contaminated = `${code}\n\n[48 more lines in file. Use offset=2 to continue.]`;
43
+ assert.strictEqual(stripReadContamination(contaminated).text, code);
44
+ });
45
+ it("strips 'Line X is SIZE' bash-fallback notice", () => {
46
+ const code = "const x = 1;";
47
+ const contaminated = `${code}\n[Line 5 is 80KB, exceeds 50KB limit. Use bash: sed -n '5p' x.ts | head -c 51200]`;
48
+ assert.strictEqual(stripReadContamination(contaminated).text, code);
49
+ });
50
+ it("is a no-op on clean code", () => {
51
+ const code = "function f() {\n return 1;\n}\n";
52
+ const r = stripReadContamination(code);
53
+ assert.strictEqual(r.text, code);
54
+ assert.strictEqual(r.changed, false);
55
+ });
56
+ it("does NOT strip a literal '[Showing lines' that is not a read notice", () => {
57
+ // A comment that merely mentions the words but lacks the exact notice shape.
58
+ const code = "// [Showing lines is not enough info]\nconst x = 1;";
59
+ const r = stripReadContamination(code);
60
+ assert.strictEqual(r.text, code);
61
+ assert.strictEqual(r.changed, false);
62
+ });
63
+ it("does NOT strip a notice-shaped string occurring mid-text (real content)", () => {
64
+ // e.g. editing this module's own edit-repair.ts — the notice is content.
65
+ const code = "const P = /\\[Showing lines \\d+ of \\d+\\. Use offset=\\d+ to continue\\.\\]/;\nconst x = 1;";
66
+ const r = stripReadContamination(code);
67
+ assert.strictEqual(r.text, code);
68
+ assert.strictEqual(r.changed, false);
69
+ });
70
+ it("strips a trailing notice even after trailing blank line", () => {
71
+ const r = stripReadContamination("const x = 1;\n\n[48 more lines in file. Use offset=2 to continue.]\n");
72
+ assert.strictEqual(r.text, "const x = 1;");
73
+ });
74
+ });
75
+
76
+ describe("findTrimMatch", () => {
77
+ it("matches exactly (count 1)", () => {
78
+ const content = "a\nb\nc\nd";
79
+ const r = findTrimMatch(content, "b\nc");
80
+ assert.strictEqual(r.count, 1);
81
+ assert.strictEqual(r.firstIndex, 1);
82
+ });
83
+ it("tolerates leading-whitespace drift (the Codex .trim() pass gap)", () => {
84
+ const content = " a\n b\n c\n d";
85
+ const oldText = "a\nb"; // model dropped indentation
86
+ const r = findTrimMatch(content, oldText);
87
+ assert.strictEqual(r.count, 1);
88
+ assert.strictEqual(r.firstIndex, 0);
89
+ });
90
+ it("counts multiple matches", () => {
91
+ const content = "x\nx\nx";
92
+ const r = findTrimMatch(content, "x");
93
+ assert.strictEqual(r.count, 3);
94
+ assert.strictEqual(r.firstIndex, 0);
95
+ });
96
+ it("returns count 0 when not found", () => {
97
+ const r = findTrimMatch("a\nb", "z");
98
+ assert.strictEqual(r.count, 0);
99
+ assert.strictEqual(r.firstIndex, -1);
100
+ });
101
+ it("ignores a single trailing empty pattern line", () => {
102
+ const content = "a\nb";
103
+ const r = findTrimMatch(content, "a\nb\n");
104
+ assert.strictEqual(r.count, 1);
105
+ assert.strictEqual(r.firstIndex, 0);
106
+ });
107
+ it("returns 0 when pattern longer than file", () => {
108
+ const r = findTrimMatch("a", "a\nb\nc");
109
+ assert.strictEqual(r.count, 0);
110
+ });
111
+ });
112
+
113
+ describe("fileBytesForBlock", () => {
114
+ it("returns actual file lines with real indentation", () => {
115
+ const content = " a\n b\n c";
116
+ // matched block starts at line 0, length 2
117
+ assert.strictEqual(fileBytesForBlock(content, 0, 2), " a\n b");
118
+ });
119
+ });
120
+
121
+ describe("nearestBlock", () => {
122
+ it("returns a numbered region with the most overlap", () => {
123
+ const content = "alpha\nbeta\ngamma\ndelta\nepsilon";
124
+ const oldText = "beta\ngamma";
125
+ const out = nearestBlock(content, oldText, 2);
126
+ assert.match(out, /Nearest matching region/);
127
+ assert.match(out, /\| beta/);
128
+ assert.match(out, /\| gamma/);
129
+ // includes line numbers
130
+ assert.match(out, /\d+ \|/);
131
+ });
132
+ it("returns empty string for empty oldText", () => {
133
+ assert.strictEqual(nearestBlock("a\nb", ""), "");
134
+ });
135
+ });
136
+
137
+ describe("parseFailedEditIndex", () => {
138
+ it("parses edits[N] index", () => {
139
+ assert.strictEqual(parseFailedEditIndex("Could not find edits[2] in foo."), 2);
140
+ });
141
+ it("defaults to 0 for single-edit errors", () => {
142
+ assert.strictEqual(parseFailedEditIndex("Could not find the exact text in foo."), 0);
143
+ });
144
+ });
145
+
146
+ describe("isEditMismatchError", () => {
147
+ it("matches could-not-find", () => {
148
+ assert.ok(isEditMismatchError("Could not find the exact text in x.ts."));
149
+ });
150
+ it("matches duplicate occurrences", () => {
151
+ assert.ok(isEditMismatchError("Found 3 occurrences of the text in x.ts."));
152
+ });
153
+ it("matches uniqueness", () => {
154
+ assert.ok(isEditMismatchError("The oldText must match exactly including all whitespace."));
155
+ });
156
+ it("rejects unrelated errors", () => {
157
+ assert.ok(!isEditMismatchError("Operation aborted"));
158
+ assert.ok(!isEditMismatchError("Error code: ENOENT"));
159
+ });
160
+ });
161
+
162
+ describe("computeRetryEdit", () => {
163
+ it("rebuilds oldText from the file's real bytes when there is one trim match (indentation drift)", () => {
164
+ // File uses 4-space indent; model's oldText dropped indentation.
165
+ const fileContent = " a\n b\n c";
166
+ const edits = [{ oldText: "a\nb", newText: "A\nB" }];
167
+ const r = computeRetryEdit(fileContent, edits, 0);
168
+ assert.ok(r, "expected a retry result");
169
+ // corrected oldText must use the FILE's real indentation so the core exact matcher passes
170
+ assert.equal(r!.fixedEdits[0].oldText, " a\n b");
171
+ assert.equal(r!.fixedEdits[0].newText, "A\nB");
172
+ });
173
+
174
+ it("returns null when the trim match is ambiguous (count > 1)", () => {
175
+ const fileContent = "x\nx\nx";
176
+ const r = computeRetryEdit(fileContent, [{ oldText: "x", newText: "y" }], 0);
177
+ assert.equal(r, null);
178
+ });
179
+
180
+ it("returns null when there is no trim match (count 0)", () => {
181
+ const r = computeRetryEdit("a\nb", [{ oldText: "zzz", newText: "y" }], 0);
182
+ assert.equal(r, null);
183
+ });
184
+
185
+ it("only rebuilds the failing edit index in a multi-edit array", () => {
186
+ const fileContent = "one\ntwo\nthree";
187
+ const edits = [
188
+ { oldText: "one", newText: "ONE" },
189
+ { oldText: "two", newText: "TWO" },
190
+ ];
191
+ const r = computeRetryEdit(fileContent, edits, 1);
192
+ assert.ok(r);
193
+ assert.equal(r!.fixedEdits[0].oldText, "one"); // untouched
194
+ assert.equal(r!.fixedEdits[1].oldText, "two"); // rebuilt (verbatim here, exact match)
195
+ assert.equal(r!.fixedEdits[1].newText, "TWO");
196
+ });
197
+
198
+ it("clamps failIdx past the array length to the last edit", () => {
199
+ const r = computeRetryEdit("a", [{ oldText: "a", newText: "A" }], 9);
200
+ assert.ok(r);
201
+ assert.equal(r!.fixedEdits[0].oldText, "a");
202
+ });
203
+
204
+ it("drops a single trailing empty pattern line (stays in sync with findTrimMatch)", () => {
205
+ const fileContent = "a\nb";
206
+ const r = computeRetryEdit(fileContent, [{ oldText: "a\nb\n", newText: "A\nB\n" }], 0);
207
+ assert.ok(r);
208
+ assert.equal(r!.fixedEdits[0].oldText, "a\nb");
209
+ });
210
+ });
211
+
212
+ // The no-op guard lives in the edit wrapper (index.ts); its decision logic is
213
+ // one expression — exercised here so the threshold contract stays visible
214
+ // (the wrapper itself is driven end-to-end in wrapper.test.ts).
215
+ describe("no-op edit guard contract", () => {
216
+ it("oldText === newText is a no-op and must be rejected pre-IO", () => {
217
+ const edits = [{ oldText: "same", newText: "same" }];
218
+ assert.ok(edits.some((e) => e.oldText === e.newText));
219
+ });
220
+ it("differing oldText/newText passes the guard", () => {
221
+ const edits = [{ oldText: "a", newText: "b" }];
222
+ assert.ok(!edits.some((e) => e.oldText === e.newText));
223
+ });
224
+ });