ceulen 0.5.1 → 0.8.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 (367) hide show
  1. package/CHANGELOG.md +365 -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.test.ts +67 -0
  7. package/extensions/lib/registry.ts +104 -8
  8. package/extensions/lib/tools.ts +47 -0
  9. package/extensions/lib/usage-store.ts +33 -0
  10. package/extensions/modules/advisor/commands/advisor.ts +244 -0
  11. package/extensions/modules/advisor/configPanel.ts +194 -0
  12. package/extensions/modules/advisor/index.ts +235 -0
  13. package/extensions/modules/advisor/lib/config.ts +248 -0
  14. package/extensions/modules/advisor/lib/emission-guard.ts +153 -0
  15. package/extensions/modules/advisor/lib/isolated-model.ts +160 -0
  16. package/extensions/modules/advisor/lib/model-picker.ts +47 -0
  17. package/extensions/modules/advisor/lib/watcher.ts +288 -0
  18. package/extensions/modules/advisor/test/compliance.test.ts +125 -0
  19. package/extensions/modules/advisor/test/config.test.ts +202 -0
  20. package/extensions/modules/advisor/test/configPanel.test.ts +223 -0
  21. package/extensions/modules/advisor/test/emission-guard.test.ts +201 -0
  22. package/extensions/modules/advisor/test/isolated-model.test.ts +311 -0
  23. package/extensions/modules/advisor/test/model-picker.test.ts +32 -0
  24. package/extensions/modules/advisor/test/module.test.ts +649 -0
  25. package/extensions/modules/advisor/test/watcher.test.ts +478 -0
  26. package/extensions/modules/classifier/configPanel.ts +104 -0
  27. package/extensions/modules/classifier/index.ts +297 -0
  28. package/extensions/modules/classifier/lib/settings.ts +95 -0
  29. package/extensions/modules/classifier/test/classifier.test.ts +475 -0
  30. package/extensions/modules/composer/index.ts +152 -3
  31. package/extensions/modules/composer/lib/footer.ts +119 -0
  32. package/extensions/modules/composer/lib/shapes.ts +265 -26
  33. package/extensions/modules/composer/test/editor.test.ts +1 -1
  34. package/extensions/modules/composer/test/footer.test.ts +96 -0
  35. package/extensions/modules/composer/test/module.test.ts +113 -11
  36. package/extensions/modules/composer/test/screen.test.ts +29 -1
  37. package/extensions/modules/composer/test/shapes.test.ts +224 -8
  38. package/extensions/modules/config/defaultTools.ts +96 -0
  39. package/extensions/modules/config/index.ts +147 -7
  40. package/extensions/modules/config/piSettings.ts +254 -69
  41. package/extensions/modules/config/test/defaultTools.test.ts +122 -0
  42. package/extensions/modules/config/test/menu.test.ts +64 -0
  43. package/extensions/modules/config/test/panel.test.ts +38 -23
  44. package/extensions/modules/config/test/piSettings.test.ts +130 -3
  45. package/extensions/modules/config/test/renderTools.test.ts +71 -0
  46. package/extensions/modules/config/test/tools.test.ts +146 -0
  47. package/extensions/modules/fff/index.ts +2094 -0
  48. package/extensions/modules/fff/lib/query.ts +129 -0
  49. package/extensions/modules/fff/test/index.test.ts +607 -0
  50. package/extensions/modules/fff/test/query.test.ts +71 -0
  51. package/extensions/modules/munin/configPanel.ts +108 -0
  52. package/extensions/modules/munin/index.ts +641 -0
  53. package/extensions/modules/munin/lib/helpers.ts +471 -0
  54. package/extensions/modules/munin/lib/retry.ts +20 -0
  55. package/extensions/modules/munin/lib/sdk.ts +218 -0
  56. package/extensions/modules/munin/test/config.test.ts +168 -0
  57. package/extensions/modules/munin/test/configPanel.test.ts +186 -0
  58. package/extensions/modules/munin/test/extension.test.ts +224 -0
  59. package/extensions/modules/munin/test/helpers.test.ts +523 -0
  60. package/extensions/modules/munin/test/retry.test.ts +145 -0
  61. package/extensions/modules/munin/test/stale-protocol.test.ts +554 -0
  62. package/extensions/modules/plan/configPanel.ts +152 -0
  63. package/extensions/modules/plan/index.ts +917 -0
  64. package/extensions/modules/plan/lib/plan-tools.ts +72 -0
  65. package/extensions/modules/plan/lib/prompt.ts +65 -0
  66. package/extensions/modules/plan/lib/settings.ts +166 -0
  67. package/extensions/modules/plan/lib/shell-gate.ts +200 -0
  68. package/extensions/modules/plan/test/configPanel.test.ts +96 -0
  69. package/extensions/modules/plan/test/harness.ts +133 -0
  70. package/extensions/modules/plan/test/module.test.ts +305 -0
  71. package/extensions/modules/plan/test/settings.test.ts +114 -0
  72. package/extensions/modules/plan/test/shell-gate.test.ts +103 -0
  73. package/extensions/modules/ponytail/index.ts +23 -73
  74. package/extensions/modules/ponytail/lib/config.ts +1 -6
  75. package/extensions/modules/ponytail/test/extension.test.ts +1 -85
  76. package/extensions/modules/repair/configPanel.ts +96 -0
  77. package/extensions/modules/repair/index.ts +414 -0
  78. package/extensions/modules/repair/lib/auto-bg.ts +333 -0
  79. package/extensions/modules/repair/lib/edit-repair.ts +155 -0
  80. package/extensions/modules/repair/lib/editor.ts +251 -0
  81. package/extensions/modules/repair/lib/guards.ts +34 -0
  82. package/extensions/modules/repair/lib/input-repair.ts +324 -0
  83. package/extensions/modules/repair/lib/patch.ts +481 -0
  84. package/extensions/modules/repair/lib/settings.ts +114 -0
  85. package/extensions/modules/repair/test/auto-bg.test.ts +405 -0
  86. package/extensions/modules/repair/test/edit-repair.test.ts +224 -0
  87. package/extensions/modules/repair/test/editor.test.ts +141 -0
  88. package/extensions/modules/repair/test/guards.test.ts +64 -0
  89. package/extensions/modules/repair/test/input-repair.test.ts +231 -0
  90. package/extensions/modules/repair/test/patch.test.ts +563 -0
  91. package/extensions/modules/repair/test/settings.test.ts +167 -0
  92. package/extensions/modules/repair/test/wrapper.test.ts +210 -0
  93. package/extensions/modules/router/commands/commands.ts +4 -0
  94. package/extensions/modules/router/configPanel.ts +24 -10
  95. package/extensions/modules/router/lib/client.ts +63 -0
  96. package/extensions/modules/router/lib/provider.ts +34 -13
  97. package/extensions/modules/router/lib/systemone.ts +152 -0
  98. package/extensions/modules/router/test/unit.test.ts +260 -0
  99. package/extensions/modules/rtk/findFallback.js +68 -0
  100. package/extensions/modules/rtk/index.ts +248 -0
  101. package/extensions/modules/rtk/safe-rewrite.js +74 -0
  102. package/extensions/modules/rtk/test/findFallback.test.js +66 -0
  103. package/extensions/modules/rtk/test/index.test.js +131 -0
  104. package/extensions/modules/rtk/test/safe-rewrite.test.js +96 -0
  105. package/extensions/modules/rtk/version-gate.js +34 -0
  106. package/extensions/modules/rules/index.ts +120 -0
  107. package/extensions/modules/rules/lib/rules.ts +348 -0
  108. package/extensions/modules/rules/test/lib.test.ts +263 -0
  109. package/extensions/modules/rules/test/module.test.ts +222 -0
  110. package/extensions/modules/serena/index.ts +629 -0
  111. package/extensions/modules/serena/lib/detect.ts +73 -0
  112. package/extensions/modules/serena/lib/env.ts +76 -0
  113. package/extensions/modules/serena/lib/guidance.ts +26 -0
  114. package/extensions/modules/serena/lib/normalize.ts +27 -0
  115. package/extensions/modules/serena/lib/symbol-key.ts +20 -0
  116. package/extensions/modules/serena/lib/truncate.ts +20 -0
  117. package/extensions/modules/serena/test/env.test.ts +122 -0
  118. package/extensions/modules/serena/test/index.test.ts +347 -0
  119. package/extensions/modules/serena/test/worker.test.ts +562 -0
  120. package/extensions/modules/serena/worker.ts +858 -0
  121. package/extensions/modules/steering/configPanel.ts +122 -0
  122. package/extensions/modules/steering/index.ts +616 -0
  123. package/extensions/modules/steering/lib/anchor.ts +175 -0
  124. package/extensions/modules/steering/lib/errors.ts +253 -0
  125. package/extensions/modules/steering/lib/family.ts +33 -0
  126. package/extensions/modules/steering/lib/guidance.ts +172 -0
  127. package/extensions/modules/steering/lib/history.ts +170 -0
  128. package/extensions/modules/steering/lib/settings.ts +127 -0
  129. package/extensions/modules/steering/test/anchor-regressions.test.ts +93 -0
  130. package/extensions/modules/steering/test/anchor-wiring.test.ts +99 -0
  131. package/extensions/modules/steering/test/anchor.test.ts +160 -0
  132. package/extensions/modules/steering/test/cache-stats.test.ts +43 -0
  133. package/extensions/modules/steering/test/errors.test.ts +309 -0
  134. package/extensions/modules/steering/test/family.test.ts +43 -0
  135. package/extensions/modules/steering/test/guidance.test.ts +357 -0
  136. package/extensions/modules/steering/test/harness.ts +93 -0
  137. package/extensions/modules/steering/test/history.test.ts +118 -0
  138. package/extensions/modules/steering/test/settings.test.ts +182 -0
  139. package/extensions/modules/steering/test/status.test.ts +71 -0
  140. package/extensions/modules/steering/test/toolCall.test.ts +99 -0
  141. package/extensions/modules/subagent/agents/planner.md +14 -0
  142. package/extensions/modules/subagent/agents/reviewer.md +42 -0
  143. package/extensions/modules/subagent/agents/scout.md +47 -0
  144. package/extensions/modules/subagent/agents/tester.md +12 -0
  145. package/extensions/modules/subagent/agents/worker.md +18 -0
  146. package/extensions/modules/subagent/configPanel.ts +219 -0
  147. package/extensions/modules/subagent/index.ts +2464 -0
  148. package/extensions/modules/subagent/lib/agents.ts +408 -0
  149. package/extensions/modules/subagent/lib/background.ts +383 -0
  150. package/extensions/modules/subagent/lib/herdr.ts +1019 -0
  151. package/extensions/modules/subagent/lib/history.ts +126 -0
  152. package/extensions/modules/subagent/lib/model.ts +156 -0
  153. package/extensions/modules/subagent/lib/render.ts +335 -0
  154. package/extensions/modules/subagent/lib/result.ts +110 -0
  155. package/extensions/modules/subagent/lib/roles.ts +296 -0
  156. package/extensions/modules/subagent/lib/routing.ts +228 -0
  157. package/extensions/modules/subagent/lib/runner.ts +559 -0
  158. package/extensions/modules/subagent/lib/schema.ts +21 -0
  159. package/extensions/modules/subagent/lib/security.ts +763 -0
  160. package/extensions/modules/subagent/lib/settings.ts +159 -0
  161. package/extensions/modules/subagent/lib/thread-viewer.ts +319 -0
  162. package/extensions/modules/subagent/lib/threads.ts +135 -0
  163. package/extensions/modules/subagent/lib/types.ts +18 -0
  164. package/extensions/modules/subagent/lib/widget.ts +475 -0
  165. package/extensions/modules/subagent/test/configPanel.test.ts +132 -0
  166. package/extensions/modules/subagent/test/herdr-advisor.test.ts +40 -0
  167. package/extensions/modules/subagent/test/routing.test.ts +524 -0
  168. package/extensions/modules/subagent/test/widget.test.ts +208 -0
  169. package/extensions/modules/todo/index.ts +478 -0
  170. package/extensions/modules/todo/test/todo.test.ts +252 -0
  171. package/extensions/modules/usage/index.ts +70 -16
  172. package/extensions/modules/usage/test/genrate.test.ts +70 -0
  173. package/extensions/modules/usage/test/parsers.test.ts +13 -0
  174. package/extensions/modules/usage/test/session-accumulators.test.ts +85 -0
  175. package/extensions/modules/ux/UPSTREAM +24 -0
  176. package/extensions/modules/ux/index.ts +393 -0
  177. package/extensions/modules/ux/lib/audit.ts +562 -0
  178. package/extensions/modules/ux/lib/config.ts +136 -0
  179. package/extensions/modules/ux/lib/instructions.ts +77 -0
  180. package/extensions/modules/ux/test/audit.test.ts +756 -0
  181. package/extensions/modules/ux/test/extension.test.ts +394 -0
  182. package/extensions/modules/ux/test/instructions.test.ts +19 -0
  183. package/extensions/modules/web/configPanel.ts +218 -0
  184. package/extensions/modules/web/index.ts +978 -0
  185. package/extensions/modules/web/lib/agy.ts +209 -0
  186. package/extensions/modules/web/lib/brave.ts +42 -0
  187. package/extensions/modules/web/lib/cdp.ts +686 -0
  188. package/extensions/modules/web/lib/chatapi.ts +101 -0
  189. package/extensions/modules/web/lib/chatgpt.ts +796 -0
  190. package/extensions/modules/web/lib/chrome.ts +328 -0
  191. package/extensions/modules/web/lib/config.ts +136 -0
  192. package/extensions/modules/web/lib/content.ts +99 -0
  193. package/extensions/modules/web/lib/crawl4ai.ts +172 -0
  194. package/extensions/modules/web/lib/extract.ts +235 -0
  195. package/extensions/modules/web/lib/firecrawl.ts +56 -0
  196. package/extensions/modules/web/lib/format.ts +197 -0
  197. package/extensions/modules/web/lib/gemini-auth.ts +286 -0
  198. package/extensions/modules/web/lib/gemini-dr.ts +338 -0
  199. package/extensions/modules/web/lib/gemini.ts +551 -0
  200. package/extensions/modules/web/lib/imageapi.ts +559 -0
  201. package/extensions/modules/web/lib/retry.ts +71 -0
  202. package/extensions/modules/web/lib/search.ts +237 -0
  203. package/extensions/modules/web/lib/searxng.ts +69 -0
  204. package/extensions/modules/web/lib/settings.ts +157 -0
  205. package/extensions/modules/web/scripts/cdp-smoke.ts +77 -0
  206. package/extensions/modules/web/scripts/gemini-smoke.ts +107 -0
  207. package/extensions/modules/web/test/agy.test.ts +383 -0
  208. package/extensions/modules/web/test/cdp.test.ts +717 -0
  209. package/extensions/modules/web/test/chat.test.ts +171 -0
  210. package/extensions/modules/web/test/chatgpt.test.ts +633 -0
  211. package/extensions/modules/web/test/chrome.test.ts +282 -0
  212. package/extensions/modules/web/test/config.test.ts +218 -0
  213. package/extensions/modules/web/test/configPanel.test.ts +198 -0
  214. package/extensions/modules/web/test/crawl.test.ts +134 -0
  215. package/extensions/modules/web/test/crawl4ai.test.ts +105 -0
  216. package/extensions/modules/web/test/extension.test.ts +118 -0
  217. package/extensions/modules/web/test/extract.test.ts +436 -0
  218. package/extensions/modules/web/test/fixtures/dr-confirm.bin +12 -0
  219. package/extensions/modules/web/test/fixtures/dr-plan.bin +20 -0
  220. package/extensions/modules/web/test/fixtures/interaction.html +31 -0
  221. package/extensions/modules/web/test/format.test.ts +230 -0
  222. package/extensions/modules/web/test/gemini-auth.test.ts +465 -0
  223. package/extensions/modules/web/test/gemini-dr.test.ts +249 -0
  224. package/extensions/modules/web/test/gemini.test.ts +472 -0
  225. package/extensions/modules/web/test/hook.test.ts +101 -0
  226. package/extensions/modules/web/test/image.test.ts +1089 -0
  227. package/extensions/modules/web/test/retry.test.ts +119 -0
  228. package/extensions/modules/web/test/screenshot.test.ts +109 -0
  229. package/extensions/modules/web/test/search.test.ts +210 -0
  230. package/extensions/modules/web/test/settings.test.ts +165 -0
  231. package/extensions/modules/web/test/skill.test.ts +72 -0
  232. package/extensions/modules/web/types.d.ts +3 -0
  233. package/extensions/modules/web/vendor/package.json +1 -0
  234. package/extensions/modules/web/vendor/readability/LICENSE.md +13 -0
  235. package/extensions/modules/web/vendor/readability/Readability-readerable.js +123 -0
  236. package/extensions/modules/web/vendor/readability/Readability.js +2787 -0
  237. package/extensions/modules/web/vendor/readability/index.d.ts +64 -0
  238. package/extensions/modules/web/vendor/readability/index.js +9 -0
  239. package/extensions/modules/web/vendor/turndown/LICENSE +21 -0
  240. package/extensions/modules/web/vendor/turndown/turndown.cjs.js +784 -0
  241. package/extensions/modules/web/vendor/turndown-plugin-gfm/LICENSE +21 -0
  242. package/extensions/modules/web/vendor/turndown-plugin-gfm/turndown-plugin-gfm.cjs.js +163 -0
  243. package/extensions/modules/zai/configPanel.ts +154 -0
  244. package/extensions/modules/zai/index.ts +186 -0
  245. package/extensions/modules/zai/lib/anthropic.ts +159 -0
  246. package/extensions/modules/zai/lib/settings.ts +198 -0
  247. package/extensions/modules/zai/lib/signing.ts +721 -0
  248. package/extensions/modules/zai/lib/throttle.ts +198 -0
  249. package/extensions/modules/zai/test/anthropic.test.ts +132 -0
  250. package/extensions/modules/zai/test/configPanel.test.ts +230 -0
  251. package/extensions/modules/zai/test/extension.test.ts +249 -0
  252. package/extensions/modules/zai/test/settings.test.ts +183 -0
  253. package/extensions/modules/zai/test/signing-wiring.test.ts +108 -0
  254. package/extensions/modules/zai/test/signing.test.ts +364 -0
  255. package/extensions/modules/zai/test/throttle.test.ts +329 -0
  256. package/package.json +19 -4
  257. package/scripts/validate-themes.mjs +75 -0
  258. package/skills/munin/SKILL.md +117 -0
  259. package/skills/ux-capture/SKILL.md +202 -0
  260. package/skills/ux-design/SKILL.md +279 -0
  261. package/skills/ux-presets/SKILL.md +484 -0
  262. package/skills/ux-routing/SKILL.md +46 -0
  263. package/skills/web/SKILL.md +153 -0
  264. package/themes/alabaster.json +93 -0
  265. package/themes/amethyst.json +96 -0
  266. package/themes/anthracite.json +93 -0
  267. package/themes/basalt.json +91 -0
  268. package/themes/birch.json +95 -0
  269. package/themes/catppuccin-mocha.json +91 -0
  270. package/themes/dark-abyss.json +91 -0
  271. package/themes/dark-arctic.json +104 -0
  272. package/themes/dark-aurora.json +95 -0
  273. package/themes/dark-catppuccin.json +107 -0
  274. package/themes/dark-cavern.json +91 -0
  275. package/themes/dark-celestial.json +106 -0
  276. package/themes/dark-copper.json +95 -0
  277. package/themes/dark-cosmos.json +90 -0
  278. package/themes/dark-cyberpunk.json +102 -0
  279. package/themes/dark-dracula.json +98 -0
  280. package/themes/dark-eclipse.json +91 -0
  281. package/themes/dark-ember.json +95 -0
  282. package/themes/dark-equinox.json +90 -0
  283. package/themes/dark-forest.json +96 -0
  284. package/themes/dark-github.json +105 -0
  285. package/themes/dark-gruvbox.json +112 -0
  286. package/themes/dark-lavender.json +95 -0
  287. package/themes/dark-lunar.json +89 -0
  288. package/themes/dark-midnight.json +95 -0
  289. package/themes/dark-monochrome.json +94 -0
  290. package/themes/dark-monokai.json +98 -0
  291. package/themes/dark-nebula.json +90 -0
  292. package/themes/dark-neon-noir.json +100 -0
  293. package/themes/dark-nord.json +98 -0
  294. package/themes/dark-ocean.json +101 -0
  295. package/themes/dark-one.json +100 -0
  296. package/themes/dark-poimandres.json +115 -0
  297. package/themes/dark-rainforest.json +91 -0
  298. package/themes/dark-reef.json +91 -0
  299. package/themes/dark-retro.json +92 -0
  300. package/themes/dark-rose-pine.json +96 -0
  301. package/themes/dark-sakura.json +95 -0
  302. package/themes/dark-slate.json +95 -0
  303. package/themes/dark-solarized.json +97 -0
  304. package/themes/dark-solstice.json +90 -0
  305. package/themes/dark-starfall.json +91 -0
  306. package/themes/dark-sunset.json +99 -0
  307. package/themes/dark-swamp.json +90 -0
  308. package/themes/dark-synthwave.json +103 -0
  309. package/themes/dark-taiga.json +91 -0
  310. package/themes/dark-terminal.json +95 -0
  311. package/themes/dark-tokyo-night.json +101 -0
  312. package/themes/dark-tundra.json +91 -0
  313. package/themes/dark-twilight.json +91 -0
  314. package/themes/dark-volcanic.json +91 -0
  315. package/themes/dark.json +80 -0
  316. package/themes/graphite.json +92 -0
  317. package/themes/light-arctic.json +107 -0
  318. package/themes/light-aurora-day.json +91 -0
  319. package/themes/light-canyon.json +91 -0
  320. package/themes/light-catppuccin.json +106 -0
  321. package/themes/light-cirrus.json +90 -0
  322. package/themes/light-coral.json +95 -0
  323. package/themes/light-cyberpunk.json +96 -0
  324. package/themes/light-dawn.json +90 -0
  325. package/themes/light-dunes.json +91 -0
  326. package/themes/light-eucalyptus.json +95 -0
  327. package/themes/light-forest.json +100 -0
  328. package/themes/light-frost.json +95 -0
  329. package/themes/light-github.json +115 -0
  330. package/themes/light-glacier.json +91 -0
  331. package/themes/light-gruvbox.json +108 -0
  332. package/themes/light-haze.json +90 -0
  333. package/themes/light-honeycomb.json +95 -0
  334. package/themes/light-lagoon.json +91 -0
  335. package/themes/light-lavender.json +95 -0
  336. package/themes/light-meadow.json +91 -0
  337. package/themes/light-mint.json +95 -0
  338. package/themes/light-monochrome.json +101 -0
  339. package/themes/light-ocean.json +99 -0
  340. package/themes/light-one.json +99 -0
  341. package/themes/light-opal.json +91 -0
  342. package/themes/light-orchard.json +91 -0
  343. package/themes/light-paper.json +95 -0
  344. package/themes/light-poimandres.json +115 -0
  345. package/themes/light-prism.json +90 -0
  346. package/themes/light-retro.json +98 -0
  347. package/themes/light-sand.json +95 -0
  348. package/themes/light-savanna.json +91 -0
  349. package/themes/light-solarized.json +102 -0
  350. package/themes/light-soleil.json +90 -0
  351. package/themes/light-sunset.json +99 -0
  352. package/themes/light-synthwave.json +98 -0
  353. package/themes/light-tokyo-night.json +111 -0
  354. package/themes/light-wetland.json +91 -0
  355. package/themes/light-zenith.json +89 -0
  356. package/themes/light.json +80 -0
  357. package/themes/limestone.json +94 -0
  358. package/themes/mahogany.json +97 -0
  359. package/themes/marble.json +93 -0
  360. package/themes/mirage.json +79 -0
  361. package/themes/obsidian.json +91 -0
  362. package/themes/onyx.json +91 -0
  363. package/themes/pearl.json +93 -0
  364. package/themes/porcelain.json +91 -0
  365. package/themes/quartz.json +96 -0
  366. package/themes/sandstone.json +95 -0
  367. package/themes/titanium.json +90 -0
@@ -0,0 +1,248 @@
1
+ import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
2
+ import { createLocalBashOperations, isToolCallEventType } from "@earendil-works/pi-coding-agent";
3
+ import { hasUnsupportedRtkFind } from "./findFallback.js";
4
+ import { parseSemver, supportsFindPassthrough } from "./version-gate.js";
5
+ import { isSafeRewrite } from "./safe-rewrite.js";
6
+
7
+ const REWRITE_TIMEOUT_MS = 2_000;
8
+ const RTK_UNAVAILABLE_RETRY_MS = 30_000;
9
+ const MIN_SUPPORTED_RTK: [number, number, number] = [0, 23, 0];
10
+
11
+ // ponytail: deliberate duplicate of version-gate.js's private isAtLeastVersion —
12
+ // pi's jiti loader pairs a reloaded index.ts with stale cached siblings, so
13
+ // importing NEW exports from existing files crashes /reload (0.2.1 regression).
14
+ function isAtLeastVersion(current: [number, number, number], minimum: [number, number, number]): boolean {
15
+ for (let i = 0; i < minimum.length; i += 1) {
16
+ if (current[i] > minimum[i]) return true;
17
+ if (current[i] < minimum[i]) return false;
18
+ }
19
+ return true;
20
+ }
21
+ const RTK_SUBCOMMANDS = ["enable", "disable", "status"] as const;
22
+
23
+ let sessionEnabled = true;
24
+ let rtkUnavailableNotified = false;
25
+ let rtkAvailable: boolean | undefined;
26
+ let rtkLastCheckedAt = 0;
27
+ let rtkSupportsFindPassthrough = false;
28
+ let rtkVersion: string | null = null;
29
+
30
+ function rewritingEnabled(): boolean {
31
+ return sessionEnabled && !isRtkDisabled();
32
+ }
33
+
34
+ function isRtkDisabled(): boolean {
35
+ const val = process.env.RTK_DISABLED;
36
+ if (!val) return false;
37
+ const lower = val.trim().toLowerCase();
38
+ return lower === "1" || lower === "true" || lower === "yes" || lower === "y";
39
+ }
40
+
41
+ function notifyRtkUnavailable(ctx: ExtensionContext, message: string): void {
42
+ if (rtkUnavailableNotified) return;
43
+ rtkUnavailableNotified = true;
44
+ if (ctx.hasUI) ctx.ui.notify(message, "warning");
45
+ else console.warn(message);
46
+ }
47
+
48
+ async function getRtkVersion(pi: ExtensionAPI): Promise<string | null> {
49
+ const result = await pi.exec("rtk", ["--version"], { timeout: REWRITE_TIMEOUT_MS }).catch(() => undefined);
50
+ if (!result || result.code !== 0) return null;
51
+ return result.stdout.trim() || null;
52
+ }
53
+
54
+ async function checkRtkAvailable(pi: ExtensionAPI, ctx: ExtensionContext): Promise<boolean> {
55
+ // Availability gate: rtk >= 0.23.0 for `rtk rewrite`. A >= 0.46 binary does
56
+ // NOT affect availability — it only re-enables find passthrough below.
57
+ // Conservatively reset passthrough on every check; only a verified >=0.46
58
+ // binary re-enables it.
59
+ rtkSupportsFindPassthrough = false;
60
+ const version = await getRtkVersion(pi);
61
+ rtkVersion = version;
62
+ if (!version) {
63
+ rtkAvailable = false;
64
+ rtkLastCheckedAt = Date.now();
65
+ notifyRtkUnavailable(ctx, "[pi-rtk] rtk binary not found in PATH; shell command rewrites will pass through unchanged");
66
+ return false;
67
+ }
68
+
69
+ const parsedVersion = parseSemver(version.replace(/^rtk\s+/, ""));
70
+ if (parsedVersion && !isAtLeastVersion(parsedVersion, MIN_SUPPORTED_RTK)) {
71
+ rtkAvailable = false;
72
+ rtkLastCheckedAt = Date.now();
73
+ notifyRtkUnavailable(ctx, `[pi-rtk] ${version} is too old; need rtk >= 0.23.0 for \`rtk rewrite\`; shell command rewrites will pass through unchanged`);
74
+ return false;
75
+ }
76
+
77
+ // rtk 0.46 dispatches on find's grammar and passes unmodeled predicates
78
+ // through to real find (never-worse guard) — safe subset of find predicates
79
+ // no longer needs blocking there.
80
+ rtkSupportsFindPassthrough = !!parsedVersion && supportsFindPassthrough(version);
81
+ rtkAvailable = true;
82
+ rtkLastCheckedAt = Date.now();
83
+ rtkUnavailableNotified = false;
84
+ return true;
85
+ }
86
+
87
+ async function ensureRtkAvailableForRewrite(pi: ExtensionAPI, ctx: ExtensionContext): Promise<boolean> {
88
+ if (rtkAvailable !== false) return true;
89
+ if (Date.now() - rtkLastCheckedAt < RTK_UNAVAILABLE_RETRY_MS) return false;
90
+ return checkRtkAvailable(pi, ctx);
91
+ }
92
+
93
+ async function rewriteCommand(pi: ExtensionAPI, command: string, ctx: ExtensionContext, signal?: AbortSignal): Promise<string | null> {
94
+ if (!(await ensureRtkAvailableForRewrite(pi, ctx))) return null;
95
+
96
+ const result = await pi.exec("rtk", ["rewrite", command], {
97
+ timeout: REWRITE_TIMEOUT_MS,
98
+ signal,
99
+ }).catch(() => undefined);
100
+
101
+ if (!result) {
102
+ if (signal?.aborted) return null;
103
+ rtkAvailable = false;
104
+ rtkLastCheckedAt = Date.now();
105
+ notifyRtkUnavailable(ctx, "[pi-rtk] rtk rewrite failed to start; shell command rewrites will pass through unchanged");
106
+ return null;
107
+ }
108
+
109
+ rtkAvailable = true;
110
+ rtkLastCheckedAt = Date.now();
111
+ rtkUnavailableNotified = false;
112
+ if (result.killed) return null;
113
+ // rtk rewrite exit codes: 0 = no rewrite (empty stdout), 1 = error,
114
+ // 3 = successful rewrite (rewritten command in stdout).
115
+ // Accept both 0 and 3 as success; we read stdout regardless of exit code.
116
+ if (result.code !== 0 && result.code !== 3) return null;
117
+
118
+ const rewritten = result.stdout.trim();
119
+ if (hasUnsupportedRtkFind(rewritten, rtkSupportsFindPassthrough)) return null;
120
+ return rewritten.length > 0 ? rewritten : null;
121
+ }
122
+
123
+
124
+ async function maybeRewriteCommand(pi: ExtensionAPI, command: string, ctx: ExtensionContext, signal?: AbortSignal): Promise<string | null> {
125
+ if (!rewritingEnabled()) return null;
126
+ if (typeof command !== "string" || command.trim() === "") return null;
127
+ if (command.trimStart().startsWith("rtk ")) return null;
128
+ const rewritten = await rewriteCommand(pi, command, ctx, signal);
129
+ if (rewritten && rewritten !== command && !isSafeRewrite(command, rewritten)) return null;
130
+ return rewritten;
131
+ }
132
+
133
+ async function showRtkStatus(pi: ExtensionAPI, ctx: ExtensionContext): Promise<void> {
134
+ const available = await checkRtkAvailable(pi, ctx);
135
+ // Reuse the version checkRtkAvailable just fetched — no second spawn.
136
+ const version = available ? rtkVersion : null;
137
+ const envDisabled = isRtkDisabled();
138
+ const cacheState = rtkAvailable === false ? `unavailable (retry in ${Math.max(0, Math.ceil((RTK_UNAVAILABLE_RETRY_MS - (Date.now() - rtkLastCheckedAt)) / 1000))}s)` : "available";
139
+ const lines = [
140
+ `Session toggle: ${sessionEnabled ? "enabled" : "disabled"}`,
141
+ `RTK_DISABLED: ${envDisabled ? "1 (rewrites bypassed)" : "not set"}`,
142
+ `Runtime cache: ${cacheState}`,
143
+ `Binary: ${version ?? "rtk not detected on PATH"}`,
144
+ "Tip: use /rtk enable, /rtk disable, /rtk status; use RTK_DISABLED=1 for an environment-level bypass.",
145
+ ];
146
+ ctx.ui.notify(lines.join("\n"), "info");
147
+ }
148
+
149
+ async function handleRtkCommand(pi: ExtensionAPI, args: string, ctx: ExtensionContext): Promise<void> {
150
+ const subcommand = args.trim();
151
+ if (subcommand.length === 0) {
152
+ await showRtkStatus(pi, ctx);
153
+ return;
154
+ }
155
+
156
+ if (!(RTK_SUBCOMMANDS as readonly string[]).includes(subcommand)) {
157
+ ctx.ui.notify("Unknown /rtk subcommand. Valid forms: /rtk enable, /rtk disable, /rtk status.", "error");
158
+ return;
159
+ }
160
+
161
+ if (subcommand === "status") {
162
+ await showRtkStatus(pi, ctx);
163
+ return;
164
+ }
165
+
166
+ sessionEnabled = subcommand === "enable";
167
+ ctx.ui.notify(`pi-rtk ${sessionEnabled ? "enabled" : "disabled"} for this session`, "info");
168
+ }
169
+
170
+ export default function piRtkExtension(pi: ExtensionAPI) {
171
+ const localBashOperations = createLocalBashOperations();
172
+
173
+ pi.registerCommand("rtk", {
174
+ description: "Control pi-rtk shell command rewriting",
175
+ getArgumentCompletions: (prefix) => {
176
+ const items = RTK_SUBCOMMANDS
177
+ .filter((k) => k.startsWith(prefix.trim().toLowerCase()))
178
+ .map((k) => ({ value: k, label: k }));
179
+ return items.length > 0 ? items : null;
180
+ },
181
+ handler: async (args, ctx) => {
182
+ await handleRtkCommand(pi, args, ctx);
183
+ },
184
+ });
185
+
186
+ pi.on("session_start", async (_event, ctx) => {
187
+ // Reset per-session toggle
188
+ sessionEnabled = true;
189
+ rtkUnavailableNotified = false;
190
+ await checkRtkAvailable(pi, ctx);
191
+ });
192
+
193
+ pi.on("session_shutdown", async (_event, ctx) => {
194
+ });
195
+
196
+ pi.on("before_agent_start", async (event) => {
197
+ if (!rewritingEnabled()) return;
198
+ // Truth gate: when the binary is known-missing, rewrites pass through —
199
+ // don't tell the model its commands are rewritten. Re-checked every turn,
200
+ // so once the 30s re-probe flips rtkAvailable back to true, the note
201
+ // is injected again on subsequent turns.
202
+ if (rtkAvailable === false) return;
203
+ return {
204
+ systemPrompt: event.systemPrompt +
205
+ "\n\nYour bash commands are transparently rewritten through RTK for token savings. " +
206
+ "Command output reflects the rewritten command; the original command text in your tool calls is replaced before execution.",
207
+ };
208
+ });
209
+
210
+ pi.on("tool_call", async (event, ctx) => {
211
+ try {
212
+ if (!isToolCallEventType("bash", event)) return;
213
+
214
+ const originalCommand = event.input.command;
215
+ const rewritten = await maybeRewriteCommand(pi, originalCommand, ctx, ctx.signal);
216
+ if (rewritten && rewritten !== originalCommand) {
217
+ // Notify when a command is rewritten so the model sees the discrepancy
218
+ if (ctx.hasUI) {
219
+ ctx.ui.notify(`[pi-rtk] rewrote: ${originalCommand.slice(0, 80)} → ${rewritten.slice(0, 80)}`, "info");
220
+ }
221
+ event.input.command = rewritten;
222
+ }
223
+ } catch (error) {
224
+ const msg = "[pi-rtk] unexpected rewrite error; passing bash command through unchanged";
225
+ if (ctx.hasUI) ctx.ui.notify(msg, "warning");
226
+ else console.warn(msg, error);
227
+ }
228
+ });
229
+
230
+ pi.on("user_bash", async (event, ctx) => {
231
+ try {
232
+ if (event.excludeFromContext) return;
233
+
234
+ const rewritten = await maybeRewriteCommand(pi, event.command, ctx, ctx.signal);
235
+ if (!rewritten || rewritten === event.command) return;
236
+ return {
237
+ operations: {
238
+ exec: (_command, cwd, options) => localBashOperations.exec(rewritten, cwd, options),
239
+ },
240
+ };
241
+ } catch (error) {
242
+ const msg = "[pi-rtk] unexpected user_bash rewrite error; passing command through unchanged";
243
+ if (ctx.hasUI) ctx.ui.notify(msg, "warning");
244
+ else console.warn(msg, error);
245
+ return;
246
+ }
247
+ });
248
+ }
@@ -0,0 +1,74 @@
1
+ // Security gate for RTK rewrites — plain JS so node --test can exercise it
2
+ // without loading index.ts (which pulls the pi SDK peer dep).
3
+ // ponytail: kept in sync by index.ts import; do not inline copies back into index.ts.
4
+
5
+ // ponytail: reject RTK rewrites that change the first word or add shell operators
6
+ // Also reject rewrites of eval/script commands (node -e, python -c, bash -c, etc.)
7
+ // because RTK cannot safely transform arbitrary inline scripts. Shell
8
+ // interpreters (bash/sh/zsh/...) only match their eval form (-c); plain
9
+ // `bash script.sh` is untouched. An optional `env` prefix (with flags or
10
+ // VAR=value assignments) is skipped before the interpreter.
11
+ const SCRIPT_COMMAND_RE = /^(?:env\s+(?:-\S+\s+|[A-Za-z_]\w*=\S*\s+)*)?(?:(?:\/[\w/.-]+)?\b(?:node|python|python3|ruby|perl|php|deno|bun|lua|perl6|raku|tclsh|groovy|julia|Rscript|ghci|dart|swift|bash|sh|zsh|ksh|fish|dash|ash|csh|tcsh)\s+)(?:-\S+\s+)*(?:-[pecrE]{1,3}|--eval|--print|eval(?=\s|$)|-c(?=\s|$))\b/;
12
+
13
+ export function isEvalCommand(command) {
14
+ return SCRIPT_COMMAND_RE.test(command.trim());
15
+ }
16
+
17
+ // Classify each char of a shell command: "live" (unquoted, shell-active),
18
+ // "single" (inside '...'), "double" (inside "..."), "escaped" (after \).
19
+ // $, `, and escapes stay active inside double quotes; parens do not.
20
+ function shellCharKinds(s) {
21
+ const kinds = new Array(s.length).fill("live");
22
+ let i = 0;
23
+ while (i < s.length) {
24
+ const c = s[i];
25
+ if (c === "\\" && i + 1 < s.length) { kinds[i] = kinds[i + 1] = "escaped"; i += 2; continue; }
26
+ if (c === "'") {
27
+ kinds[i] = "single";
28
+ const j = s.indexOf("'", i + 1);
29
+ if (j === -1) break; // unterminated single quote
30
+ for (let k = i + 1; k < j; k++) kinds[k] = "single";
31
+ i = j + 1;
32
+ continue;
33
+ }
34
+ if (c === '"') {
35
+ kinds[i] = "double";
36
+ let j = i + 1;
37
+ while (j < s.length && s[j] !== '"') {
38
+ if (s[j] === "\\" && j + 1 < s.length) { kinds[j] = kinds[j + 1] = "double"; j += 2; continue; }
39
+ kinds[j] = "double";
40
+ j += 1;
41
+ }
42
+ i = j + 1;
43
+ continue;
44
+ }
45
+ i += 1;
46
+ }
47
+ return kinds;
48
+ }
49
+
50
+ export function isSafeRewrite(original, rewritten) {
51
+ // Never rewrite inline script commands — RTK can't transform arbitrary code
52
+ if (isEvalCommand(original) || isEvalCommand(rewritten)) return false;
53
+ const oTokens = original.trim().split(/\s+/);
54
+ const rTokens = rewritten.trim().split(/\s+/);
55
+ // RTK prepends "rtk" as the first token; compare against the original's first token
56
+ const rtkIdx = rTokens[0] === "rtk" ? 1 : 0;
57
+ const o = oTokens[0], n = rTokens[rtkIdx] ?? "";
58
+ if (o !== n) return false;
59
+ if (!/[|><;&`]/.test(rewritten)) {
60
+ // Quoted-aware injection check: reject $-substitution and subshell parens
61
+ // only where the shell would execute them. Quoted parens (git commit -m
62
+ // "fix (bug)", rtk ≥0.46 find groups) and escaped \$ \( stay allowed.
63
+ const kinds = shellCharKinds(rewritten);
64
+ for (let i = 0; i < rewritten.length; i++) {
65
+ const c = rewritten[i];
66
+ if (c === "\n" || c === "\r") return false; // newline = command split, quoted or not
67
+ if (kinds[i] === "single" || kinds[i] === "escaped") continue;
68
+ if (c === "$" && rewritten[i + 1] === "(") return false;
69
+ if ((c === "(" || c === ")") && kinds[i] === "live") return false;
70
+ }
71
+ return true;
72
+ }
73
+ return false;
74
+ }
@@ -0,0 +1,66 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import { hasUnsupportedRtkFind } from "../findFallback.js";
4
+ import { parseSemver, supportsFindPassthrough } from "../version-gate.js";
5
+ test("always rejects mutating/consumer find predicates (any rtk version)", () => {
6
+ assert.equal(hasUnsupportedRtkFind('rtk find . -type f -name "*.ts" -exec wc -l {} \\;'), true);
7
+ assert.equal(hasUnsupportedRtkFind('rtk find . -name "*.js" -execdir rm {} \\;'), true);
8
+ assert.equal(hasUnsupportedRtkFind("rtk find . -name '*.ts' -delete"), true);
9
+ assert.equal(hasUnsupportedRtkFind('rtk find . -name "*.ts" -print0 | head -20'), true);
10
+ assert.equal(hasUnsupportedRtkFind("rtk find . -name '*.ts' -regex '.+\\.ts'"), true);
11
+ });
12
+
13
+ test("rejects predicate tokens on rtk <0.46", () => {
14
+ assert.equal(hasUnsupportedRtkFind('rtk find . -type f -name "*.ts" -o -name "*.tsx"', false), true);
15
+ assert.equal(hasUnsupportedRtkFind("rtk find . -name '*.ts' -mtime -7", false), true);
16
+ assert.equal(hasUnsupportedRtkFind("rtk find . -type f '(' -name '*.ts' ')'", false), true);
17
+ assert.equal(hasUnsupportedRtkFind("rtk find . -newer x -type f", false), true);
18
+ assert.equal(hasUnsupportedRtkFind("rtk find . -size +1k", false), true);
19
+ });
20
+
21
+ test("allows predicate tokens on rtk >=0.46 (passthrough verified)", () => {
22
+ assert.equal(hasUnsupportedRtkFind('rtk find . -type f -name "*.ts" -o -name "*.tsx"', true), false);
23
+ assert.equal(hasUnsupportedRtkFind("rtk find . -name '*.ts' -mtime -7", true), false);
24
+ assert.equal(hasUnsupportedRtkFind("rtk find . -newer x -type f -perm 600 -empty", true), false);
25
+ // mutating tokens still rejected even with passthrough
26
+ assert.equal(hasUnsupportedRtkFind('rtk find . -name "*.ts" -exec rm {} \\;', true), true);
27
+ });
28
+
29
+ test("default (no version arg) keeps legacy strict behavior", () => {
30
+ assert.equal(hasUnsupportedRtkFind("rtk find . -name '*.ts' -mtime -7"), true);
31
+ assert.equal(hasUnsupportedRtkFind('rtk find . -name "*.ts" -type f'), false);
32
+ });
33
+
34
+ test("allows simple rtk find and non-find rewrites", () => {
35
+ assert.equal(hasUnsupportedRtkFind('rtk find . -name "*.ts" -type f', true), false);
36
+ assert.equal(hasUnsupportedRtkFind('rtk grep foo .', true), false);
37
+ assert.equal(hasUnsupportedRtkFind('rtk find . -name "*.ts" | head -20', true), false);
38
+ });
39
+
40
+ test("parseSemver handles version strings", () => {
41
+ assert.deepEqual(parseSemver("rtk 0.46.0"), [0, 46, 0]);
42
+ assert.deepEqual(parseSemver("0.45.9"), [0, 45, 9]);
43
+ assert.equal(parseSemver("garbage"), null);
44
+ assert.equal(parseSemver(""), null);
45
+ });
46
+
47
+ test("supportsFindPassthrough gates on rtk >= 0.46.0", () => {
48
+ assert.equal(supportsFindPassthrough("rtk 0.46.0"), true);
49
+ assert.equal(supportsFindPassthrough("rtk 0.47.1"), true);
50
+ assert.equal(supportsFindPassthrough("rtk 0.45.9"), false);
51
+ assert.equal(supportsFindPassthrough("rtk 0.23.0"), false);
52
+ assert.equal(supportsFindPassthrough("garbage"), false);
53
+ assert.equal(supportsFindPassthrough(""), false);
54
+ });
55
+
56
+ test("sibling module export surfaces are frozen (jiti reload staleness)", async () => {
57
+ // pi's jiti loader pairs a reloaded index.ts with stale cached copies of
58
+ // existing sibling modules, so a NEW export on these files crashes /reload
59
+ // in sessions that cached the older shape (0.2.1 regression). Freeze both.
60
+ const vg = await import("../version-gate.js");
61
+ assert.deepEqual(Object.keys(vg).sort(), ["RTK_FIND_PASSTHROUGH_VERSION", "parseSemver", "supportsFindPassthrough"]);
62
+ const ff = await import("../findFallback.js");
63
+ assert.deepEqual(Object.keys(ff), ["hasUnsupportedRtkFind"]);
64
+ const sr = await import("../safe-rewrite.js");
65
+ assert.deepEqual(Object.keys(sr).sort(), ["isEvalCommand", "isSafeRewrite"]);
66
+ });
@@ -0,0 +1,131 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+
4
+ // Orchestration tests for index.ts: availability gating, env bypass, and the
5
+ // safe-rewrite decision on the tool_call path, via a stubbed pi + ctx.
6
+ // Module-level cache state (rtkAvailable/rtkLastCheckedAt) persists across
7
+ // tests in this file — tests are ordered to lean on it, and session_start
8
+ // (which re-checks unconditionally) re-establishes availability where needed.
9
+
10
+ function createHarness() {
11
+ const state = { versionCalls: 0, rewriteCalls: 0, rewritten: null, versionAvailable: false };
12
+ const handlers = {};
13
+ const ctx = {
14
+ hasUI: false,
15
+ cwd: process.cwd(),
16
+ signal: { aborted: false },
17
+ ui: { setStatus() {}, notify() {} },
18
+ };
19
+ const pi = {
20
+ registerCommand() {},
21
+ on(name, fn) {
22
+ handlers[name] = fn;
23
+ },
24
+ exec(cmd, args) {
25
+ if (args[0] === "--version") {
26
+ state.versionCalls++;
27
+ return state.versionAvailable
28
+ ? Promise.resolve({ code: 0, stdout: "rtk 0.46.0\n" })
29
+ : Promise.resolve({ code: 1, stdout: "" });
30
+ }
31
+ if (args[0] === "rewrite") {
32
+ state.rewriteCalls++;
33
+ return state.rewritten === null
34
+ ? Promise.reject(new Error("spawn failed"))
35
+ : Promise.resolve({ code: 3, stdout: state.rewritten + "\n", killed: false });
36
+ }
37
+ return Promise.resolve({ code: 0, stdout: "" });
38
+ },
39
+ };
40
+ return { pi, ctx, handlers, state };
41
+ }
42
+
43
+ async function fireToolCall(h, command) {
44
+ const event = { type: "tool_call", toolCallId: "t1", toolName: "bash", input: { command } };
45
+ await h.handlers.tool_call(event, h.ctx);
46
+ return event.input.command;
47
+ }
48
+
49
+ test("rtk unavailable: command passes through unchanged and availability is cached", async () => {
50
+ const h = createHarness();
51
+ h.state.versionAvailable = false;
52
+ const ext = (await import("../index.ts")).default;
53
+ ext(h.pi);
54
+
55
+ // session_start performs the initial availability check (as in a real session).
56
+ await h.handlers.session_start({}, h.ctx);
57
+
58
+ const afterFirst = await fireToolCall(h, "git status");
59
+ assert.equal(afterFirst, "git status");
60
+ assert.equal(h.state.versionCalls, 1);
61
+
62
+ // Second call within the 30s negative-cache window must not re-check.
63
+ const afterSecond = await fireToolCall(h, "ls -la");
64
+ assert.equal(afterSecond, "ls -la");
65
+ assert.equal(h.state.versionCalls, 1);
66
+ assert.equal(h.state.rewriteCalls, 0);
67
+ });
68
+
69
+ test("RTK_DISABLED=1: no rewrite even when rtk is available", async () => {
70
+ const h = createHarness();
71
+ h.state.versionAvailable = true;
72
+ const ext = (await import("../index.ts")).default;
73
+ ext(h.pi);
74
+
75
+ // Fresh session check establishes availability first.
76
+ await h.handlers.session_start({}, h.ctx);
77
+ process.env.RTK_DISABLED = "1";
78
+ try {
79
+ const after = await fireToolCall(h, "git status");
80
+ assert.equal(after, "git status");
81
+ assert.equal(h.state.rewriteCalls, 0);
82
+ } finally {
83
+ delete process.env.RTK_DISABLED;
84
+ }
85
+ });
86
+
87
+ test("safe rewrite path applies the rewritten command", async () => {
88
+ const h = createHarness();
89
+ h.state.versionAvailable = true;
90
+ h.state.rewritten = "rtk git status";
91
+ const ext = (await import("../index.ts")).default;
92
+ ext(h.pi);
93
+
94
+ await h.handlers.session_start({}, h.ctx);
95
+ const after = await fireToolCall(h, "git status");
96
+ assert.equal(after, "rtk git status");
97
+ assert.equal(h.state.rewriteCalls, 1);
98
+ });
99
+
100
+ test("unsafe rewrite (isSafeRewrite false) keeps the original command", async () => {
101
+ const h = createHarness();
102
+ h.state.versionAvailable = true;
103
+ h.state.rewritten = "evil -rf /";
104
+ const ext = (await import("../index.ts")).default;
105
+ ext(h.pi);
106
+
107
+ await h.handlers.session_start({}, h.ctx);
108
+ const after = await fireToolCall(h, "git status");
109
+ assert.equal(after, "git status");
110
+ assert.equal(h.state.rewriteCalls, 1);
111
+ });
112
+
113
+ test("before_agent_start: prompt note injected only when rtk is available", async () => {
114
+ const ext = (await import("../index.ts")).default;
115
+
116
+ // rtkAvailable === false → rewrites pass through, so no rewrite note.
117
+ const off = createHarness();
118
+ off.state.versionAvailable = false;
119
+ ext(off.pi);
120
+ await off.handlers.session_start({}, off.ctx);
121
+ const resOff = await off.handlers.before_agent_start({ systemPrompt: "BASE" });
122
+ assert.equal(resOff, undefined, "no systemPrompt modification when unavailable");
123
+
124
+ // Available → the note is appended truthfully.
125
+ const on = createHarness();
126
+ on.state.versionAvailable = true;
127
+ ext(on.pi);
128
+ await on.handlers.session_start({}, on.ctx);
129
+ const resOn = await on.handlers.before_agent_start({ systemPrompt: "BASE" });
130
+ assert.match(resOn.systemPrompt, /transparently rewritten through RTK/);
131
+ });
@@ -0,0 +1,96 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import { isEvalCommand, isSafeRewrite } from "../safe-rewrite.js";
4
+
5
+ // Security gate for RTK command rewrites: a rewrite may only prepend `rtk`
6
+ // to the original command's first word, must never introduce shell operators,
7
+ // and must never touch eval/script commands (node -e, python -c, ...).
8
+
9
+ test("isSafeRewrite allows a plain rtk prepend", () => {
10
+ assert.equal(isSafeRewrite("git status", "rtk git status"), true);
11
+ assert.equal(isSafeRewrite("cargo build", "rtk cargo build"), true);
12
+ });
13
+
14
+ test("isSafeRewrite allows rewriting to a subcommand of the same first word", () => {
15
+ assert.equal(isSafeRewrite("git log", "rtk git log --oneline"), true);
16
+ });
17
+
18
+ test("isSafeRewrite rejects rewrites that change the first word", () => {
19
+ assert.equal(isSafeRewrite("git status", "rtk hg status"), false);
20
+ assert.equal(isSafeRewrite("ls -la", "rm -rf /"), false);
21
+ });
22
+
23
+ test("isSafeRewrite rejects shell operators in the rewrite", () => {
24
+ assert.equal(isSafeRewrite("cat a", "rtk cat a | rm -rf /"), false);
25
+ assert.equal(isSafeRewrite("cat a", "rtk cat a; rm -rf /"), false);
26
+ assert.equal(isSafeRewrite("cat a", "rtk cat a > /etc/passwd"), false);
27
+ assert.equal(isSafeRewrite("cat a", "rtk cat a && rm -rf /"), false);
28
+ assert.equal(isSafeRewrite("cat a", "rtk cat `whoami`"), false);
29
+ });
30
+
31
+ test("isSafeRewrite rejects shell-injection constructs", () => {
32
+ // command substitution executes even though there is no operator char
33
+ assert.equal(isSafeRewrite("cat a", "rtk cat a $(rm -rf /)"), false);
34
+ assert.equal(isSafeRewrite("cat a", 'rtk cat a "$(rm -rf /)"'), false); // executes inside double quotes too
35
+ // subshell parens
36
+ assert.equal(isSafeRewrite("cat a", "rtk cat a (rm -rf /)"), false);
37
+ assert.equal(isSafeRewrite("cat a", "rtk (cat a)"), false);
38
+ // newline splits into a second command
39
+ assert.equal(isSafeRewrite("cat a", "rtk cat a\nrm -rf /"), false);
40
+ assert.equal(isSafeRewrite("cat a", "rtk cat a\rrm -rf /"), false);
41
+ });
42
+
43
+ test("isSafeRewrite still allows quoted parens and escaped chars", () => {
44
+ assert.equal(isSafeRewrite('git commit -m "fix (bug)"', 'rtk git commit -m "fix (bug)"'), true);
45
+ });
46
+
47
+ test("isSafeRewrite rejects rewriting eval/script commands", () => {
48
+ assert.equal(isSafeRewrite("node -e 'console.log(1)'", "rtk node -e 'console.log(1)'"), false);
49
+ assert.equal(isSafeRewrite("python -c 'print(1)'", "rtk python -c 'print(1)'"), false);
50
+ assert.equal(isSafeRewrite("git status", "rtk node -e 'pwn()'"), false);
51
+ });
52
+
53
+ test("isSafeRewrite rejects rewriting shell-family inline eval", () => {
54
+ // bash/sh -c executes arbitrary inline scripts exactly like node -e
55
+ assert.equal(isSafeRewrite("bash -c 'rm -rf /'", "rtk bash -c 'rm -rf /'"), false);
56
+ assert.equal(isSafeRewrite("zsh -c 'echo hi'", "rtk zsh -c 'echo hi'"), false);
57
+ assert.equal(isSafeRewrite("git status", "rtk sh -c 'pwn()'"), false);
58
+ });
59
+
60
+ test("isEvalCommand detects inline-script interpreter invocations", () => {
61
+ assert.equal(isEvalCommand("node -e 'console.log(1)'"), true);
62
+ assert.equal(isEvalCommand("python -c 'print(1)'"), true);
63
+ assert.equal(isEvalCommand("python3 --eval x"), true);
64
+ assert.equal(isEvalCommand("ruby -e 'puts 1'"), true);
65
+ assert.equal(isEvalCommand("/usr/local/bin/node --print 1"), true);
66
+ assert.equal(isEvalCommand("node -p process.version"), true);
67
+ assert.equal(isEvalCommand("php -r 'echo 1;'"), true);
68
+ assert.equal(isEvalCommand("perl -E 'say 1'"), true);
69
+ assert.equal(isEvalCommand("deno eval 'console.log(1)'"), true);
70
+ });
71
+
72
+ test("isEvalCommand detects shell interpreters and env prefixes", () => {
73
+ // shell family: only -c is the eval form
74
+ assert.equal(isEvalCommand("bash -c 'echo hi'"), true);
75
+ assert.equal(isEvalCommand("sh -c 'echo hi'"), true);
76
+ assert.equal(isEvalCommand("zsh -c 'echo hi'"), true);
77
+ assert.equal(isEvalCommand("ksh -c 'echo hi'"), true);
78
+ assert.equal(isEvalCommand("dash -c 'echo hi'"), true);
79
+ assert.equal(isEvalCommand("fish -c 'echo hi'"), true);
80
+ // env-prefixed interpreters
81
+ assert.equal(isEvalCommand("env node -e 'pwn()'"), true);
82
+ assert.equal(isEvalCommand("env -i bash -c 'pwn()'"), true);
83
+ assert.equal(isEvalCommand("env CI=1 node -e 'pwn()'"), true);
84
+ });
85
+
86
+ test("isEvalCommand does not flag plain script-file runs", () => {
87
+ assert.equal(isEvalCommand("node script.js"), false);
88
+ assert.equal(isEvalCommand("python manage.py migrate"), false);
89
+ assert.equal(isEvalCommand("npm test"), false);
90
+ assert.equal(isEvalCommand("deno run script.ts"), false);
91
+ // bash script.sh is a file run, not inline eval
92
+ assert.equal(isEvalCommand("bash deploy.sh"), false);
93
+ assert.equal(isEvalCommand("sh build.sh --release"), false);
94
+ // env prefix with a non-interpreter command
95
+ assert.equal(isEvalCommand("env CI=1 npm test"), false);
96
+ });
@@ -0,0 +1,34 @@
1
+ // Version gate for pi-rtk's find-predicate blocklist.
2
+ //
3
+ // rtk 0.46 dispatches on find's grammar and passes unmodeled predicates through
4
+ // to real find (never-worse guard); older rtk needs the strict blocklist.
5
+ // ponytail: this export surface is FROZEN once shipped — pi's jiti loader
6
+ // pairs a reloaded index.ts with a stale cached copy of existing sibling
7
+ // modules, so a NEW export here crashes /reload in running sessions (bit us
8
+ // twice: "parseSemver is not a function", then "isAtLeastVersion is not a
9
+ // function" after 0.2.1 exported it). New helpers go in a brand-new file.
10
+ export const RTK_FIND_PASSTHROUGH_VERSION = [0, 46, 0];
11
+
12
+ // Minimal semver triple parse; returns null when unparseable (conservative).
13
+ /** Parse "x.y.z" into a [major, minor, patch] tuple; null when absent.
14
+ * @param {string} raw
15
+ * @returns {[number, number, number] | null}
16
+ */
17
+ export function parseSemver(raw) {
18
+ const match = raw.trim().match(/(\d+)\.(\d+)\.(\d+)/);
19
+ if (!match) return null;
20
+ return [Number.parseInt(match[1], 10), Number.parseInt(match[2], 10), Number.parseInt(match[3], 10)];
21
+ }
22
+
23
+ function isAtLeastVersion(current, minimum) {
24
+ for (let i = 0; i < minimum.length; i += 1) {
25
+ if (current[i] > minimum[i]) return true;
26
+ if (current[i] < minimum[i]) return false;
27
+ }
28
+ return true;
29
+ }
30
+
31
+ export function supportsFindPassthrough(versionOutput) {
32
+ const parsed = parseSemver(String(versionOutput ?? "").replace(/^rtk\s+/, ""));
33
+ return !!parsed && isAtLeastVersion(parsed, RTK_FIND_PASSTHROUGH_VERSION);
34
+ }