@caupulican/pi-adaptative 0.81.37 → 0.81.39

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 (451) hide show
  1. package/CHANGELOG.md +91 -0
  2. package/README.md +1 -1
  3. package/dist/bundled-resources/extensions/tmux-agent-manager/README.md +18 -1
  4. package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.d.ts +131 -0
  5. package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.d.ts.map +1 -0
  6. package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.js +196 -0
  7. package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.js.map +1 -0
  8. package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.ts +308 -0
  9. package/dist/bundled-resources/extensions/tmux-agent-manager/index.d.ts +22 -2
  10. package/dist/bundled-resources/extensions/tmux-agent-manager/index.d.ts.map +1 -1
  11. package/dist/bundled-resources/extensions/tmux-agent-manager/index.js +585 -24
  12. package/dist/bundled-resources/extensions/tmux-agent-manager/index.js.map +1 -1
  13. package/dist/bundled-resources/extensions/tmux-agent-manager/index.ts +749 -27
  14. package/dist/bundled-resources/runtimes/pi-shell-engine/commands/__init__.py +43 -0
  15. package/dist/bundled-resources/runtimes/pi-shell-engine/commands/fs.py +270 -0
  16. package/dist/bundled-resources/runtimes/pi-shell-engine/commands/search.py +252 -0
  17. package/dist/bundled-resources/runtimes/pi-shell-engine/commands/strings.py +399 -0
  18. package/dist/bundled-resources/runtimes/pi-shell-engine/commands/text.py +575 -0
  19. package/dist/bundled-resources/runtimes/pi-shell-engine/context.py +52 -0
  20. package/dist/bundled-resources/runtimes/pi-shell-engine/errors.py +49 -0
  21. package/dist/bundled-resources/runtimes/pi-shell-engine/exec.py +734 -0
  22. package/dist/bundled-resources/runtimes/pi-shell-engine/expand.py +238 -0
  23. package/dist/bundled-resources/runtimes/pi-shell-engine/main.py +132 -0
  24. package/dist/bundled-resources/runtimes/pi-shell-engine/nodes.py +116 -0
  25. package/dist/bundled-resources/runtimes/pi-shell-engine/parser.py +287 -0
  26. package/dist/bundled-resources/runtimes/pi-shell-engine/proc.py +137 -0
  27. package/dist/bundled-resources/runtimes/pi-shell-engine/state.py +100 -0
  28. package/dist/bundled-resources/runtimes/pi-shell-engine/tokens.py +579 -0
  29. package/dist/bundled-resources/skills/tool-call-repair/SKILL.md +19 -9
  30. package/dist/bundled-resources/skills/tool-call-repair/references/failure-grammar.md +22 -5
  31. package/dist/bundled-resources/skills/tool-call-repair/references/repair-catalogue.md +5 -5
  32. package/dist/bundled-resources/skills/tool-call-repair/references/text-protocol-grammar.md +28 -7
  33. package/dist/cli/args.d.ts +3 -0
  34. package/dist/cli/args.d.ts.map +1 -1
  35. package/dist/cli/args.js +15 -0
  36. package/dist/cli/args.js.map +1 -1
  37. package/dist/core/agent-paths.d.ts +49 -0
  38. package/dist/core/agent-paths.d.ts.map +1 -0
  39. package/dist/core/agent-paths.js +107 -0
  40. package/dist/core/agent-paths.js.map +1 -0
  41. package/dist/core/agent-session-services.d.ts.map +1 -1
  42. package/dist/core/agent-session-services.js +3 -3
  43. package/dist/core/agent-session-services.js.map +1 -1
  44. package/dist/core/agent-session.d.ts +234 -17
  45. package/dist/core/agent-session.d.ts.map +1 -1
  46. package/dist/core/agent-session.js +484 -62
  47. package/dist/core/agent-session.js.map +1 -1
  48. package/dist/core/autonomy/contracts.d.ts +9 -0
  49. package/dist/core/autonomy/contracts.d.ts.map +1 -1
  50. package/dist/core/autonomy/contracts.js.map +1 -1
  51. package/dist/core/autonomy/lane-tracker.d.ts +10 -1
  52. package/dist/core/autonomy/lane-tracker.d.ts.map +1 -1
  53. package/dist/core/autonomy/lane-tracker.js +5 -1
  54. package/dist/core/autonomy/lane-tracker.js.map +1 -1
  55. package/dist/core/background-lane-controller.d.ts +122 -6
  56. package/dist/core/background-lane-controller.d.ts.map +1 -1
  57. package/dist/core/background-lane-controller.js +447 -90
  58. package/dist/core/background-lane-controller.js.map +1 -1
  59. package/dist/core/bash-execution-controller.d.ts +4 -0
  60. package/dist/core/bash-execution-controller.d.ts.map +1 -1
  61. package/dist/core/bash-execution-controller.js +7 -1
  62. package/dist/core/bash-execution-controller.js.map +1 -1
  63. package/dist/core/compaction-support.d.ts +13 -3
  64. package/dist/core/compaction-support.d.ts.map +1 -1
  65. package/dist/core/compaction-support.js +43 -7
  66. package/dist/core/compaction-support.js.map +1 -1
  67. package/dist/core/context/context-audit.d.ts +33 -1
  68. package/dist/core/context/context-audit.d.ts.map +1 -1
  69. package/dist/core/context/context-audit.js +31 -5
  70. package/dist/core/context/context-audit.js.map +1 -1
  71. package/dist/core/context/sqlite-runtime-index.d.ts.map +1 -1
  72. package/dist/core/context/sqlite-runtime-index.js +13 -7
  73. package/dist/core/context/sqlite-runtime-index.js.map +1 -1
  74. package/dist/core/context-gc.d.ts.map +1 -1
  75. package/dist/core/context-gc.js +16 -1
  76. package/dist/core/context-gc.js.map +1 -1
  77. package/dist/core/context-pipeline.d.ts +26 -4
  78. package/dist/core/context-pipeline.d.ts.map +1 -1
  79. package/dist/core/context-pipeline.js +137 -14
  80. package/dist/core/context-pipeline.js.map +1 -1
  81. package/dist/core/cost-guard.d.ts +19 -3
  82. package/dist/core/cost-guard.d.ts.map +1 -1
  83. package/dist/core/cost-guard.js +18 -3
  84. package/dist/core/cost-guard.js.map +1 -1
  85. package/dist/core/delegation/session-worker-result.d.ts +34 -4
  86. package/dist/core/delegation/session-worker-result.d.ts.map +1 -1
  87. package/dist/core/delegation/session-worker-result.js +64 -4
  88. package/dist/core/delegation/session-worker-result.js.map +1 -1
  89. package/dist/core/delegation/worker-actions.d.ts.map +1 -1
  90. package/dist/core/delegation/worker-actions.js +1 -1
  91. package/dist/core/delegation/worker-actions.js.map +1 -1
  92. package/dist/core/delegation/worker-result.d.ts +42 -1
  93. package/dist/core/delegation/worker-result.d.ts.map +1 -1
  94. package/dist/core/delegation/worker-result.js +68 -0
  95. package/dist/core/delegation/worker-result.js.map +1 -1
  96. package/dist/core/extensions/loader.d.ts.map +1 -1
  97. package/dist/core/extensions/loader.js +23 -7
  98. package/dist/core/extensions/loader.js.map +1 -1
  99. package/dist/core/extensions/runner.d.ts.map +1 -1
  100. package/dist/core/extensions/runner.js +1 -0
  101. package/dist/core/extensions/runner.js.map +1 -1
  102. package/dist/core/extensions/types.d.ts +51 -0
  103. package/dist/core/extensions/types.d.ts.map +1 -1
  104. package/dist/core/extensions/types.js.map +1 -1
  105. package/dist/core/goal-loop-controller.d.ts +17 -1
  106. package/dist/core/goal-loop-controller.d.ts.map +1 -1
  107. package/dist/core/goal-loop-controller.js +79 -11
  108. package/dist/core/goal-loop-controller.js.map +1 -1
  109. package/dist/core/goals/goal-continuation-controller.d.ts +48 -2
  110. package/dist/core/goals/goal-continuation-controller.d.ts.map +1 -1
  111. package/dist/core/goals/goal-continuation-controller.js +77 -0
  112. package/dist/core/goals/goal-continuation-controller.js.map +1 -1
  113. package/dist/core/goals/goal-continuation-defaults.d.ts +39 -0
  114. package/dist/core/goals/goal-continuation-defaults.d.ts.map +1 -1
  115. package/dist/core/goals/goal-continuation-defaults.js +42 -0
  116. package/dist/core/goals/goal-continuation-defaults.js.map +1 -1
  117. package/dist/core/goals/goal-continuation-prompt.d.ts +4 -0
  118. package/dist/core/goals/goal-continuation-prompt.d.ts.map +1 -1
  119. package/dist/core/goals/goal-continuation-prompt.js +51 -10
  120. package/dist/core/goals/goal-continuation-prompt.js.map +1 -1
  121. package/dist/core/goals/goal-runtime-snapshot.d.ts +101 -2
  122. package/dist/core/goals/goal-runtime-snapshot.d.ts.map +1 -1
  123. package/dist/core/goals/goal-runtime-snapshot.js +87 -4
  124. package/dist/core/goals/goal-runtime-snapshot.js.map +1 -1
  125. package/dist/core/goals/goal-state.d.ts +89 -1
  126. package/dist/core/goals/goal-state.d.ts.map +1 -1
  127. package/dist/core/goals/goal-state.js +71 -5
  128. package/dist/core/goals/goal-state.js.map +1 -1
  129. package/dist/core/goals/goal-tool-core.d.ts +56 -2
  130. package/dist/core/goals/goal-tool-core.d.ts.map +1 -1
  131. package/dist/core/goals/goal-tool-core.js +111 -5
  132. package/dist/core/goals/goal-tool-core.js.map +1 -1
  133. package/dist/core/goals/session-goal-state.d.ts +11 -2
  134. package/dist/core/goals/session-goal-state.d.ts.map +1 -1
  135. package/dist/core/goals/session-goal-state.js +32 -17
  136. package/dist/core/goals/session-goal-state.js.map +1 -1
  137. package/dist/core/keybindings.d.ts +10 -0
  138. package/dist/core/keybindings.d.ts.map +1 -1
  139. package/dist/core/keybindings.js +10 -2
  140. package/dist/core/keybindings.js.map +1 -1
  141. package/dist/core/learning/observation-store.d.ts +12 -3
  142. package/dist/core/learning/observation-store.d.ts.map +1 -1
  143. package/dist/core/learning/observation-store.js +30 -15
  144. package/dist/core/learning/observation-store.js.map +1 -1
  145. package/dist/core/learning/skill-curator.d.ts +5 -1
  146. package/dist/core/learning/skill-curator.d.ts.map +1 -1
  147. package/dist/core/learning/skill-curator.js +21 -19
  148. package/dist/core/learning/skill-curator.js.map +1 -1
  149. package/dist/core/local-runtime-controller.d.ts +65 -3
  150. package/dist/core/local-runtime-controller.d.ts.map +1 -1
  151. package/dist/core/local-runtime-controller.js +186 -26
  152. package/dist/core/local-runtime-controller.js.map +1 -1
  153. package/dist/core/memory/providers/file-store.d.ts +1 -1
  154. package/dist/core/memory/providers/file-store.d.ts.map +1 -1
  155. package/dist/core/memory/providers/file-store.js +6 -6
  156. package/dist/core/memory/providers/file-store.js.map +1 -1
  157. package/dist/core/model-capability.d.ts +34 -0
  158. package/dist/core/model-capability.d.ts.map +1 -1
  159. package/dist/core/model-capability.js +42 -1
  160. package/dist/core/model-capability.js.map +1 -1
  161. package/dist/core/model-router/tool-escalation.d.ts +15 -0
  162. package/dist/core/model-router/tool-escalation.d.ts.map +1 -1
  163. package/dist/core/model-router/tool-escalation.js +23 -1
  164. package/dist/core/model-router/tool-escalation.js.map +1 -1
  165. package/dist/core/model-router-controller.d.ts +34 -7
  166. package/dist/core/model-router-controller.d.ts.map +1 -1
  167. package/dist/core/model-router-controller.js +95 -16
  168. package/dist/core/model-router-controller.js.map +1 -1
  169. package/dist/core/models/adaptation-store.d.ts +7 -0
  170. package/dist/core/models/adaptation-store.d.ts.map +1 -1
  171. package/dist/core/models/adaptation-store.js +47 -11
  172. package/dist/core/models/adaptation-store.js.map +1 -1
  173. package/dist/core/models/default-model-suggestions.d.ts.map +1 -1
  174. package/dist/core/models/default-model-suggestions.js +17 -0
  175. package/dist/core/models/default-model-suggestions.js.map +1 -1
  176. package/dist/core/models/fitness-store.d.ts +3 -0
  177. package/dist/core/models/fitness-store.d.ts.map +1 -1
  178. package/dist/core/models/fitness-store.js +11 -2
  179. package/dist/core/models/fitness-store.js.map +1 -1
  180. package/dist/core/models/llamacpp-runtime.d.ts +180 -0
  181. package/dist/core/models/llamacpp-runtime.d.ts.map +1 -0
  182. package/dist/core/models/llamacpp-runtime.js +475 -0
  183. package/dist/core/models/llamacpp-runtime.js.map +1 -0
  184. package/dist/core/models/local-registration.d.ts +40 -0
  185. package/dist/core/models/local-registration.d.ts.map +1 -1
  186. package/dist/core/models/local-registration.js +94 -5
  187. package/dist/core/models/local-registration.js.map +1 -1
  188. package/dist/core/models/local-runtime.d.ts.map +1 -1
  189. package/dist/core/models/local-runtime.js +9 -7
  190. package/dist/core/models/local-runtime.js.map +1 -1
  191. package/dist/core/models/model-ref.d.ts +7 -0
  192. package/dist/core/models/model-ref.d.ts.map +1 -1
  193. package/dist/core/models/model-ref.js +26 -0
  194. package/dist/core/models/model-ref.js.map +1 -1
  195. package/dist/core/models/needle-runtime.d.ts +257 -0
  196. package/dist/core/models/needle-runtime.d.ts.map +1 -0
  197. package/dist/core/models/needle-runtime.js +519 -0
  198. package/dist/core/models/needle-runtime.js.map +1 -0
  199. package/dist/core/models/prism-llamacpp-lifecycle.d.ts +89 -0
  200. package/dist/core/models/prism-llamacpp-lifecycle.d.ts.map +1 -0
  201. package/dist/core/models/prism-llamacpp-lifecycle.js +121 -0
  202. package/dist/core/models/prism-llamacpp-lifecycle.js.map +1 -0
  203. package/dist/core/package-manager.d.ts.map +1 -1
  204. package/dist/core/package-manager.js +11 -10
  205. package/dist/core/package-manager.js.map +1 -1
  206. package/dist/core/process-matrix/codes.d.ts +72 -0
  207. package/dist/core/process-matrix/codes.d.ts.map +1 -0
  208. package/dist/core/process-matrix/codes.js +15 -0
  209. package/dist/core/process-matrix/codes.js.map +1 -0
  210. package/dist/core/process-matrix/runtime.d.ts +63 -0
  211. package/dist/core/process-matrix/runtime.d.ts.map +1 -0
  212. package/dist/core/process-matrix/runtime.js +310 -0
  213. package/dist/core/process-matrix/runtime.js.map +1 -0
  214. package/dist/core/process-matrix/store.d.ts +22 -0
  215. package/dist/core/process-matrix/store.d.ts.map +1 -0
  216. package/dist/core/process-matrix/store.js +80 -0
  217. package/dist/core/process-matrix/store.js.map +1 -0
  218. package/dist/core/process-matrix/supervisor.d.ts +72 -0
  219. package/dist/core/process-matrix/supervisor.d.ts.map +1 -0
  220. package/dist/core/process-matrix/supervisor.js +130 -0
  221. package/dist/core/process-matrix/supervisor.js.map +1 -0
  222. package/dist/core/profile-registry.d.ts +7 -0
  223. package/dist/core/profile-registry.d.ts.map +1 -1
  224. package/dist/core/profile-registry.js +20 -0
  225. package/dist/core/profile-registry.js.map +1 -1
  226. package/dist/core/python-runtime.d.ts.map +1 -1
  227. package/dist/core/python-runtime.js +3 -3
  228. package/dist/core/python-runtime.js.map +1 -1
  229. package/dist/core/reflection-controller.d.ts +29 -5
  230. package/dist/core/reflection-controller.d.ts.map +1 -1
  231. package/dist/core/reflection-controller.js +215 -126
  232. package/dist/core/reflection-controller.js.map +1 -1
  233. package/dist/core/reload-blockers.d.ts +36 -0
  234. package/dist/core/reload-blockers.d.ts.map +1 -1
  235. package/dist/core/reload-blockers.js +44 -0
  236. package/dist/core/reload-blockers.js.map +1 -1
  237. package/dist/core/resource-loader.d.ts.map +1 -1
  238. package/dist/core/resource-loader.js +8 -7
  239. package/dist/core/resource-loader.js.map +1 -1
  240. package/dist/core/runtime-builder.d.ts +58 -5
  241. package/dist/core/runtime-builder.d.ts.map +1 -1
  242. package/dist/core/runtime-builder.js +209 -25
  243. package/dist/core/runtime-builder.js.map +1 -1
  244. package/dist/core/scout-controller.d.ts +6 -0
  245. package/dist/core/scout-controller.d.ts.map +1 -1
  246. package/dist/core/scout-controller.js +66 -52
  247. package/dist/core/scout-controller.js.map +1 -1
  248. package/dist/core/sdk.d.ts.map +1 -1
  249. package/dist/core/sdk.js +3 -3
  250. package/dist/core/sdk.js.map +1 -1
  251. package/dist/core/session-role.d.ts +31 -0
  252. package/dist/core/session-role.d.ts.map +1 -0
  253. package/dist/core/session-role.js +52 -0
  254. package/dist/core/session-role.js.map +1 -0
  255. package/dist/core/settings-manager.d.ts +42 -3
  256. package/dist/core/settings-manager.d.ts.map +1 -1
  257. package/dist/core/settings-manager.js +94 -60
  258. package/dist/core/settings-manager.js.map +1 -1
  259. package/dist/core/system-prompt-builder.d.ts +12 -0
  260. package/dist/core/system-prompt-builder.d.ts.map +1 -1
  261. package/dist/core/system-prompt-builder.js +6 -0
  262. package/dist/core/system-prompt-builder.js.map +1 -1
  263. package/dist/core/system-prompt.d.ts +14 -1
  264. package/dist/core/system-prompt.d.ts.map +1 -1
  265. package/dist/core/system-prompt.js +20 -3
  266. package/dist/core/system-prompt.js.map +1 -1
  267. package/dist/core/tasks/session-task-state.d.ts +11 -2
  268. package/dist/core/tasks/session-task-state.d.ts.map +1 -1
  269. package/dist/core/tasks/session-task-state.js +27 -11
  270. package/dist/core/tasks/session-task-state.js.map +1 -1
  271. package/dist/core/tasks/task-contract-monitor.d.ts +45 -0
  272. package/dist/core/tasks/task-contract-monitor.d.ts.map +1 -0
  273. package/dist/core/tasks/task-contract-monitor.js +56 -0
  274. package/dist/core/tasks/task-contract-monitor.js.map +1 -0
  275. package/dist/core/tasks/task-state.d.ts +8 -0
  276. package/dist/core/tasks/task-state.d.ts.map +1 -1
  277. package/dist/core/tasks/task-state.js +28 -1
  278. package/dist/core/tasks/task-state.js.map +1 -1
  279. package/dist/core/tool-gate-controller.d.ts.map +1 -1
  280. package/dist/core/tool-gate-controller.js +5 -0
  281. package/dist/core/tool-gate-controller.js.map +1 -1
  282. package/dist/core/tool-recovery-log-records.d.ts +7 -0
  283. package/dist/core/tool-recovery-log-records.d.ts.map +1 -1
  284. package/dist/core/tool-recovery-log-records.js +14 -6
  285. package/dist/core/tool-recovery-log-records.js.map +1 -1
  286. package/dist/core/tool-selection/promotion.d.ts +54 -0
  287. package/dist/core/tool-selection/promotion.d.ts.map +1 -0
  288. package/dist/core/tool-selection/promotion.js +81 -0
  289. package/dist/core/tool-selection/promotion.js.map +1 -0
  290. package/dist/core/tool-selection/tool-performance-store.d.ts +37 -0
  291. package/dist/core/tool-selection/tool-performance-store.d.ts.map +1 -1
  292. package/dist/core/tool-selection/tool-performance-store.js +87 -3
  293. package/dist/core/tool-selection/tool-performance-store.js.map +1 -1
  294. package/dist/core/tool-selection/tool-selection-controller.d.ts +45 -0
  295. package/dist/core/tool-selection/tool-selection-controller.d.ts.map +1 -1
  296. package/dist/core/tool-selection/tool-selection-controller.js +96 -0
  297. package/dist/core/tool-selection/tool-selection-controller.js.map +1 -1
  298. package/dist/core/tools/bash.d.ts +18 -0
  299. package/dist/core/tools/bash.d.ts.map +1 -1
  300. package/dist/core/tools/bash.js +97 -18
  301. package/dist/core/tools/bash.js.map +1 -1
  302. package/dist/core/tools/delegate-status.d.ts +14 -0
  303. package/dist/core/tools/delegate-status.d.ts.map +1 -1
  304. package/dist/core/tools/delegate-status.js +82 -7
  305. package/dist/core/tools/delegate-status.js.map +1 -1
  306. package/dist/core/tools/delegate.d.ts.map +1 -1
  307. package/dist/core/tools/delegate.js +25 -8
  308. package/dist/core/tools/delegate.js.map +1 -1
  309. package/dist/core/tools/find.d.ts.map +1 -1
  310. package/dist/core/tools/find.js +52 -44
  311. package/dist/core/tools/find.js.map +1 -1
  312. package/dist/core/tools/goal.d.ts +94 -3
  313. package/dist/core/tools/goal.d.ts.map +1 -1
  314. package/dist/core/tools/goal.js +165 -15
  315. package/dist/core/tools/goal.js.map +1 -1
  316. package/dist/core/tools/grep.d.ts.map +1 -1
  317. package/dist/core/tools/grep.js +5 -4
  318. package/dist/core/tools/grep.js.map +1 -1
  319. package/dist/core/tools/model-fitness.d.ts +7 -0
  320. package/dist/core/tools/model-fitness.d.ts.map +1 -1
  321. package/dist/core/tools/model-fitness.js +2 -2
  322. package/dist/core/tools/model-fitness.js.map +1 -1
  323. package/dist/core/tools/render-utils.d.ts.map +1 -1
  324. package/dist/core/tools/render-utils.js +1 -1
  325. package/dist/core/tools/render-utils.js.map +1 -1
  326. package/dist/core/tools/shell-contract-router.d.ts +6 -1
  327. package/dist/core/tools/shell-contract-router.d.ts.map +1 -1
  328. package/dist/core/tools/shell-contract-router.js +69 -13
  329. package/dist/core/tools/shell-contract-router.js.map +1 -1
  330. package/dist/core/tools/shell-session.d.ts +89 -0
  331. package/dist/core/tools/shell-session.d.ts.map +1 -0
  332. package/dist/core/tools/shell-session.js +432 -0
  333. package/dist/core/tools/shell-session.js.map +1 -0
  334. package/dist/core/tools/task-steps.d.ts +4 -0
  335. package/dist/core/tools/task-steps.d.ts.map +1 -1
  336. package/dist/core/tools/task-steps.js +63 -8
  337. package/dist/core/tools/task-steps.js.map +1 -1
  338. package/dist/core/tools/tmux-dispatch.d.ts +86 -0
  339. package/dist/core/tools/tmux-dispatch.d.ts.map +1 -0
  340. package/dist/core/tools/tmux-dispatch.js +91 -0
  341. package/dist/core/tools/tmux-dispatch.js.map +1 -0
  342. package/dist/core/tools/windows-shell-engine.d.ts +42 -0
  343. package/dist/core/tools/windows-shell-engine.d.ts.map +1 -0
  344. package/dist/core/tools/windows-shell-engine.js +153 -0
  345. package/dist/core/tools/windows-shell-engine.js.map +1 -0
  346. package/dist/core/tools/windows-shell-state.d.ts +40 -0
  347. package/dist/core/tools/windows-shell-state.d.ts.map +1 -0
  348. package/dist/core/tools/windows-shell-state.js +59 -0
  349. package/dist/core/tools/windows-shell-state.js.map +1 -0
  350. package/dist/core/tools/worktree-sync.d.ts +24 -0
  351. package/dist/core/tools/worktree-sync.d.ts.map +1 -0
  352. package/dist/core/tools/worktree-sync.js +338 -0
  353. package/dist/core/tools/worktree-sync.js.map +1 -0
  354. package/dist/core/trust-manager.d.ts +4 -1
  355. package/dist/core/trust-manager.d.ts.map +1 -1
  356. package/dist/core/trust-manager.js +20 -2
  357. package/dist/core/trust-manager.js.map +1 -1
  358. package/dist/core/util/atomic-file.d.ts +55 -0
  359. package/dist/core/util/atomic-file.d.ts.map +1 -0
  360. package/dist/core/util/atomic-file.js +255 -0
  361. package/dist/core/util/atomic-file.js.map +1 -0
  362. package/dist/core/util/minimatch-cache.d.ts +33 -0
  363. package/dist/core/util/minimatch-cache.d.ts.map +1 -0
  364. package/dist/core/util/minimatch-cache.js +0 -0
  365. package/dist/core/util/minimatch-cache.js.map +1 -0
  366. package/dist/core/worktree-sync/codes.d.ts +227 -0
  367. package/dist/core/worktree-sync/codes.d.ts.map +1 -0
  368. package/dist/core/worktree-sync/codes.js +14 -0
  369. package/dist/core/worktree-sync/codes.js.map +1 -0
  370. package/dist/core/worktree-sync/git-engine.d.ts +156 -0
  371. package/dist/core/worktree-sync/git-engine.d.ts.map +1 -0
  372. package/dist/core/worktree-sync/git-engine.js +1191 -0
  373. package/dist/core/worktree-sync/git-engine.js.map +1 -0
  374. package/dist/core/worktree-sync/lane-gate.d.ts +75 -0
  375. package/dist/core/worktree-sync/lane-gate.d.ts.map +1 -0
  376. package/dist/core/worktree-sync/lane-gate.js +360 -0
  377. package/dist/core/worktree-sync/lane-gate.js.map +1 -0
  378. package/dist/core/worktree-sync/runtime.d.ts +47 -0
  379. package/dist/core/worktree-sync/runtime.d.ts.map +1 -0
  380. package/dist/core/worktree-sync/runtime.js +96 -0
  381. package/dist/core/worktree-sync/runtime.js.map +1 -0
  382. package/dist/core/worktree-sync/store.d.ts +69 -0
  383. package/dist/core/worktree-sync/store.d.ts.map +1 -0
  384. package/dist/core/worktree-sync/store.js +247 -0
  385. package/dist/core/worktree-sync/store.js.map +1 -0
  386. package/dist/core/worktree-sync/watcher.d.ts +29 -0
  387. package/dist/core/worktree-sync/watcher.d.ts.map +1 -0
  388. package/dist/core/worktree-sync/watcher.js +93 -0
  389. package/dist/core/worktree-sync/watcher.js.map +1 -0
  390. package/dist/main.d.ts.map +1 -1
  391. package/dist/main.js +94 -0
  392. package/dist/main.js.map +1 -1
  393. package/dist/migrations.d.ts +9 -0
  394. package/dist/migrations.d.ts.map +1 -1
  395. package/dist/migrations.js +38 -0
  396. package/dist/migrations.js.map +1 -1
  397. package/dist/modes/interactive/auto-learn-controller.d.ts +16 -1
  398. package/dist/modes/interactive/auto-learn-controller.d.ts.map +1 -1
  399. package/dist/modes/interactive/auto-learn-controller.js +50 -8
  400. package/dist/modes/interactive/auto-learn-controller.js.map +1 -1
  401. package/dist/modes/interactive/components/profile-resource-editor.d.ts.map +1 -1
  402. package/dist/modes/interactive/components/profile-resource-editor.js +23 -1
  403. package/dist/modes/interactive/components/profile-resource-editor.js.map +1 -1
  404. package/dist/modes/interactive/interactive-mode.d.ts +1 -0
  405. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  406. package/dist/modes/interactive/interactive-mode.js +4 -0
  407. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  408. package/dist/modes/interactive/local-model-commands.d.ts +43 -0
  409. package/dist/modes/interactive/local-model-commands.d.ts.map +1 -1
  410. package/dist/modes/interactive/local-model-commands.js +290 -3
  411. package/dist/modes/interactive/local-model-commands.js.map +1 -1
  412. package/dist/modes/interactive/session-flow-commands.d.ts.map +1 -1
  413. package/dist/modes/interactive/session-flow-commands.js +24 -4
  414. package/dist/modes/interactive/session-flow-commands.js.map +1 -1
  415. package/dist/utils/fs-watch.d.ts +11 -0
  416. package/dist/utils/fs-watch.d.ts.map +1 -1
  417. package/dist/utils/fs-watch.js +20 -2
  418. package/dist/utils/fs-watch.js.map +1 -1
  419. package/dist/utils/highlight-js-languages.d.ts +4 -0
  420. package/dist/utils/highlight-js-languages.d.ts.map +1 -0
  421. package/dist/utils/highlight-js-languages.js +573 -0
  422. package/dist/utils/highlight-js-languages.js.map +1 -0
  423. package/dist/utils/shell.d.ts +7 -1
  424. package/dist/utils/shell.d.ts.map +1 -1
  425. package/dist/utils/shell.js +39 -9
  426. package/dist/utils/shell.js.map +1 -1
  427. package/dist/utils/syntax-highlight.d.ts.map +1 -1
  428. package/dist/utils/syntax-highlight.js +53 -5
  429. package/dist/utils/syntax-highlight.js.map +1 -1
  430. package/dist/utils/tools-manager.d.ts.map +1 -1
  431. package/dist/utils/tools-manager.js +112 -1
  432. package/dist/utils/tools-manager.js.map +1 -1
  433. package/docs/development.md +2 -0
  434. package/docs/packages.md +1 -1
  435. package/docs/process-matrix.md +120 -0
  436. package/docs/settings.md +5 -2
  437. package/docs/tmux-agent-manager.md +85 -2
  438. package/docs/windows.md +52 -3
  439. package/docs/work-directory.md +29 -0
  440. package/docs/worktree-sync.md +250 -0
  441. package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
  442. package/examples/extensions/custom-provider-anthropic/package.json +1 -1
  443. package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
  444. package/examples/extensions/sandbox/package-lock.json +2 -2
  445. package/examples/extensions/sandbox/package.json +1 -1
  446. package/examples/extensions/with-deps/package-lock.json +2 -2
  447. package/examples/extensions/with-deps/package.json +1 -1
  448. package/npm-shrinkwrap.json +12 -12
  449. package/package.json +10 -4
  450. package/docs/integration-sweep-builder-blueprint-2026-07-09.md +0 -365
  451. package/docs/integration-sweep-resume-2026-07-09.md +0 -407
@@ -0,0 +1,43 @@
1
+ """Aggregates every pure builtin into REGISTRY: dict[str, BuiltinFn].
2
+
3
+ Imports the three WP-D chunks by module name (text, fs, strings, search). WP-D3 authors this
4
+ file per the frozen module/function naming in windows-shell-workpackages-2026-07-19.md §3
5
+ WP-D; it does not create/stub text.py or fs.py — those land from the sibling WP-D1/WP-D2
6
+ chunks in the same wave.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ from context import BuiltinFn
12
+
13
+ from . import fs, search, strings, text
14
+
15
+ REGISTRY: dict[str, BuiltinFn] = {
16
+ "echo": strings.cmd_echo,
17
+ "printf": strings.cmd_printf,
18
+ "basename": strings.cmd_basename,
19
+ "dirname": strings.cmd_dirname,
20
+ "which": strings.cmd_which,
21
+ "true": strings.cmd_true,
22
+ "false": strings.cmd_false,
23
+ "pwd": strings.cmd_pwd,
24
+ "test": strings.cmd_test,
25
+ "[": strings.cmd_test,
26
+ "grep": search.cmd_grep,
27
+ "sed": search.cmd_sed,
28
+ "cat": text.cat,
29
+ "head": text.head,
30
+ "tail": text.tail,
31
+ "wc": text.wc,
32
+ "sort": text.sort,
33
+ "uniq": text.uniq,
34
+ "cut": text.cut,
35
+ "tr": text.tr,
36
+ "ls": fs.ls,
37
+ "find": fs.find,
38
+ "rm": fs.rm,
39
+ "cp": fs.cp,
40
+ "mv": fs.mv,
41
+ "mkdir": fs.mkdir,
42
+ "touch": fs.touch,
43
+ }
@@ -0,0 +1,270 @@
1
+ """Filesystem builtins: ls, find, rm, cp, mv, mkdir, touch.
2
+
3
+ Owned by WP-D2. Pure stdlib; imports only context/errors/nodes (never exec/state/proc).
4
+ See windows-shell-workpackages-2026-07-19.md §2.2 (matrix) and §3 (WP-D spec).
5
+
6
+ Every path operand resolves against `ctx.cwd` (never `os.chdir`, never touches
7
+ process-global state). A builtin never mutates `ctx.state` (it has none - only cwd/env
8
+ strings on `BuiltinContext`).
9
+ """
10
+
11
+ from __future__ import annotations
12
+
13
+ import fnmatch
14
+ import os
15
+ import shutil
16
+ from typing import TYPE_CHECKING
17
+
18
+ from errors import UnsupportedConstruct
19
+
20
+ if TYPE_CHECKING:
21
+ from context import BuiltinContext
22
+
23
+
24
+ def _resolve(cwd: str, path: str) -> str:
25
+ """Resolve `path` against `cwd` without ever touching process cwd."""
26
+ if os.path.isabs(path):
27
+ return os.path.normpath(path)
28
+ return os.path.normpath(os.path.join(cwd, path))
29
+
30
+
31
+ def _to_posix(path: str) -> str:
32
+ return path.replace("\\", "/")
33
+
34
+
35
+ def _split_flags(argv: list[str], allowed: set[str], name: str) -> tuple[set[str], list[str]]:
36
+ """Parse `-x`/`-xy` short flags plus `--` end-of-options; positional args pass through.
37
+
38
+ Raises UnsupportedConstruct("unsupported-flag", ...) for any flag char outside `allowed`.
39
+ """
40
+ flags: set[str] = set()
41
+ positional: list[str] = []
42
+ end_opts = False
43
+ for arg in argv:
44
+ if not end_opts and arg == "--":
45
+ end_opts = True
46
+ continue
47
+ if not end_opts and len(arg) > 1 and arg[0] == "-":
48
+ for ch in arg[1:]:
49
+ if ch not in allowed:
50
+ raise UnsupportedConstruct("unsupported-flag", f"{name}: unsupported flag -{ch}")
51
+ flags.add(ch)
52
+ continue
53
+ positional.append(arg)
54
+ return flags, positional
55
+
56
+
57
+ def ls(ctx: "BuiltinContext") -> int:
58
+ flags, positional = _split_flags(ctx.argv[1:], {"a", "A", "1", "r"}, "ls")
59
+ if len(positional) > 1:
60
+ if any(os.path.isdir(_resolve(ctx.cwd, p)) for p in positional):
61
+ raise UnsupportedConstruct("unsupported-flag", "ls: only one directory operand is supported")
62
+ ordered = sorted(positional)
63
+ lines = []
64
+ exit_code = 0
65
+ for operand in ordered:
66
+ abs_operand = _resolve(ctx.cwd, operand)
67
+ if os.path.isfile(abs_operand):
68
+ lines.append(operand)
69
+ else:
70
+ lines.append(f"ls: {operand}: No such file or directory")
71
+ exit_code = 1
72
+ ctx.stdout.write("".join(line + "\n" for line in lines).encode())
73
+ return exit_code
74
+ target = positional[0] if positional else "."
75
+ abs_target = _resolve(ctx.cwd, target)
76
+ if not os.path.exists(abs_target):
77
+ ctx.stdout.write(f"ls: cannot access '{target}': No such file or directory\n".encode())
78
+ return 1
79
+ if not os.path.isdir(abs_target):
80
+ ctx.stdout.write(f"ls: cannot access '{target}': Not a directory\n".encode())
81
+ return 1
82
+ show_all = "a" in flags
83
+ almost_all = "A" in flags
84
+ entries: list[str] = []
85
+ if show_all:
86
+ entries.append(".")
87
+ entries.append("..")
88
+ for name in os.listdir(abs_target):
89
+ if name.startswith(".") and not (show_all or almost_all):
90
+ continue
91
+ entries.append(name)
92
+ entries.sort()
93
+ if "r" in flags:
94
+ entries.reverse()
95
+ lines = []
96
+ for name in entries:
97
+ full = os.path.join(abs_target, name)
98
+ suffix = "/" if os.path.isdir(full) else ""
99
+ lines.append(name + suffix)
100
+ ctx.stdout.write("".join(line + "\n" for line in lines).encode())
101
+ return 0
102
+
103
+
104
+ def find(ctx: "BuiltinContext") -> int:
105
+ argv = ctx.argv[1:]
106
+ path: str | None = None
107
+ type_filter: str | None = None
108
+ name_pattern: str | None = None
109
+ i = 0
110
+ while i < len(argv):
111
+ arg = argv[i]
112
+ if arg == "-type":
113
+ i += 1
114
+ if i >= len(argv) or argv[i] not in ("f", "d"):
115
+ raise UnsupportedConstruct("unsupported-flag", "find: -type requires f or d")
116
+ type_filter = argv[i]
117
+ elif arg == "-name":
118
+ i += 1
119
+ if i >= len(argv):
120
+ raise UnsupportedConstruct("unsupported-flag", "find: -name requires a pattern")
121
+ name_pattern = argv[i]
122
+ elif arg.startswith("-") and arg != "-":
123
+ raise UnsupportedConstruct("unsupported-flag", f"find: unsupported flag {arg}")
124
+ else:
125
+ if path is not None:
126
+ raise UnsupportedConstruct("unsupported-flag", "find: only one path operand is supported")
127
+ path = arg
128
+ i += 1
129
+ if path is None:
130
+ path = "."
131
+ abs_root = _resolve(ctx.cwd, path)
132
+ if not os.path.exists(abs_root):
133
+ ctx.stdout.write(f"find: '{path}': No such file or directory\n".encode())
134
+ return 1
135
+ all_paths = [abs_root]
136
+ for dirpath, dirnames, filenames in os.walk(abs_root):
137
+ for d in dirnames:
138
+ all_paths.append(os.path.join(dirpath, d))
139
+ for f in filenames:
140
+ all_paths.append(os.path.join(dirpath, f))
141
+ results: list[str] = []
142
+ for p in all_paths:
143
+ if type_filter == "f" and not os.path.isfile(p):
144
+ continue
145
+ if type_filter == "d" and not os.path.isdir(p):
146
+ continue
147
+ if name_pattern is not None and not fnmatch.fnmatch(os.path.basename(p), name_pattern):
148
+ continue
149
+ rel = os.path.relpath(p, abs_root)
150
+ display = path if rel == "." else os.path.join(path, rel)
151
+ results.append(_to_posix(display))
152
+ results.sort()
153
+ ctx.stdout.write("".join(r + "\n" for r in results).encode())
154
+ return 0
155
+
156
+
157
+ def rm(ctx: "BuiltinContext") -> int:
158
+ flags, paths = _split_flags(ctx.argv[1:], {"f", "r", "R"}, "rm")
159
+ if not paths:
160
+ ctx.stdout.write(b"rm: missing operand\n")
161
+ return 1
162
+ recursive = "r" in flags or "R" in flags
163
+ force = "f" in flags
164
+ exit_code = 0
165
+ for p in paths:
166
+ abs_p = _resolve(ctx.cwd, p)
167
+ if not os.path.lexists(abs_p):
168
+ if not force:
169
+ ctx.stdout.write(f"rm: cannot remove '{p}': No such file or directory\n".encode())
170
+ exit_code = 1
171
+ continue
172
+ if os.path.isdir(abs_p) and not os.path.islink(abs_p):
173
+ if not recursive:
174
+ ctx.stdout.write(f"rm: cannot remove '{p}': Is a directory\n".encode())
175
+ exit_code = 1
176
+ continue
177
+ shutil.rmtree(abs_p)
178
+ else:
179
+ os.remove(abs_p)
180
+ return exit_code
181
+
182
+
183
+ def cp(ctx: "BuiltinContext") -> int:
184
+ flags, positional = _split_flags(ctx.argv[1:], {"r", "R"}, "cp")
185
+ if len(positional) != 2:
186
+ ctx.stdout.write(b"cp: missing file operand\n")
187
+ return 1
188
+ recursive = "r" in flags or "R" in flags
189
+ src, dst = positional
190
+ abs_src = _resolve(ctx.cwd, src)
191
+ abs_dst = _resolve(ctx.cwd, dst)
192
+ if not os.path.exists(abs_src):
193
+ ctx.stdout.write(f"cp: cannot stat '{src}': No such file or directory\n".encode())
194
+ return 1
195
+ if os.path.isdir(abs_src):
196
+ if not recursive:
197
+ ctx.stdout.write(f"cp: -r not specified; omitting directory '{src}' (use -r)\n".encode())
198
+ return 1
199
+ dest = abs_dst
200
+ if os.path.isdir(abs_dst):
201
+ dest = os.path.join(abs_dst, os.path.basename(os.path.normpath(abs_src)))
202
+ shutil.copytree(abs_src, dest, dirs_exist_ok=True)
203
+ else:
204
+ dest = abs_dst
205
+ if os.path.isdir(abs_dst):
206
+ dest = os.path.join(abs_dst, os.path.basename(abs_src))
207
+ shutil.copy2(abs_src, dest)
208
+ return 0
209
+
210
+
211
+ def mv(ctx: "BuiltinContext") -> int:
212
+ _flags, positional = _split_flags(ctx.argv[1:], set(), "mv")
213
+ if len(positional) != 2:
214
+ ctx.stdout.write(b"mv: missing file operand\n")
215
+ return 1
216
+ src, dst = positional
217
+ abs_src = _resolve(ctx.cwd, src)
218
+ abs_dst = _resolve(ctx.cwd, dst)
219
+ if not os.path.exists(abs_src):
220
+ ctx.stdout.write(f"mv: cannot stat '{src}': No such file or directory\n".encode())
221
+ return 1
222
+ dest = abs_dst
223
+ if os.path.isdir(abs_dst):
224
+ dest = os.path.join(abs_dst, os.path.basename(os.path.normpath(abs_src)))
225
+ shutil.move(abs_src, dest)
226
+ return 0
227
+
228
+
229
+ def mkdir(ctx: "BuiltinContext") -> int:
230
+ flags, dirs = _split_flags(ctx.argv[1:], {"p"}, "mkdir")
231
+ if not dirs:
232
+ ctx.stdout.write(b"mkdir: missing operand\n")
233
+ return 1
234
+ parents = "p" in flags
235
+ exit_code = 0
236
+ for d in dirs:
237
+ abs_d = _resolve(ctx.cwd, d)
238
+ if parents:
239
+ os.makedirs(abs_d, exist_ok=True)
240
+ continue
241
+ if os.path.exists(abs_d):
242
+ ctx.stdout.write(f"mkdir: cannot create directory '{d}': File exists\n".encode())
243
+ exit_code = 1
244
+ continue
245
+ try:
246
+ os.mkdir(abs_d)
247
+ except FileNotFoundError:
248
+ ctx.stdout.write(f"mkdir: cannot create directory '{d}': No such file or directory\n".encode())
249
+ exit_code = 1
250
+ return exit_code
251
+
252
+
253
+ def touch(ctx: "BuiltinContext") -> int:
254
+ _flags, files = _split_flags(ctx.argv[1:], set(), "touch")
255
+ if not files:
256
+ ctx.stdout.write(b"touch: missing file operand\n")
257
+ return 1
258
+ exit_code = 0
259
+ for f in files:
260
+ abs_f = _resolve(ctx.cwd, f)
261
+ if os.path.exists(abs_f):
262
+ os.utime(abs_f, None)
263
+ continue
264
+ parent = os.path.dirname(abs_f)
265
+ if parent and not os.path.isdir(parent):
266
+ ctx.stdout.write(f"touch: cannot touch '{f}': No such file or directory\n".encode())
267
+ exit_code = 1
268
+ continue
269
+ open(abs_f, "ab").close()
270
+ return exit_code
@@ -0,0 +1,252 @@
1
+ """Search/transform builtins: grep, sed.
2
+
3
+ Pure builtins per windows-shell-workpackages-2026-07-19.md §2.2. Regex dialect is Python
4
+ `re` (documented divergence from POSIX BRE/ERE).
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import os
10
+ import re
11
+
12
+ from context import BuiltinContext
13
+ from errors import UnsupportedConstruct
14
+
15
+ _GREP_FLAGS = set("ivnclwFE")
16
+
17
+
18
+ def _resolve(cwd: str, path: str) -> str:
19
+ """Resolve `path` against `cwd` (see commands/fs.py's `_resolve`: main.py never os.chdir()s
20
+ to the request cwd, so every relative FILE operand must resolve against ctx.cwd here too)."""
21
+ if os.path.isabs(path):
22
+ return os.path.normpath(path)
23
+ return os.path.normpath(os.path.join(cwd, path))
24
+
25
+
26
+ def cmd_grep(ctx: BuiltinContext) -> int:
27
+ args = ctx.argv[1:]
28
+ flags: set[str] = set()
29
+ idx = 0
30
+ while idx < len(args):
31
+ arg = args[idx]
32
+ if arg == "--":
33
+ idx += 1
34
+ break
35
+ if arg.startswith("-") and arg != "-" and len(arg) > 1:
36
+ for c in arg[1:]:
37
+ if c not in _GREP_FLAGS:
38
+ raise UnsupportedConstruct("unsupported-flag", f"grep: unsupported flag '-{c}'")
39
+ flags.add(c)
40
+ idx += 1
41
+ continue
42
+ break
43
+ remaining = args[idx:]
44
+ if not remaining:
45
+ raise UnsupportedConstruct("unsupported-flag", "grep: PATTERN operand required")
46
+ pattern = remaining[0]
47
+ files = remaining[1:]
48
+
49
+ ignore_case = "i" in flags
50
+ invert = "v" in flags
51
+ show_lineno = "n" in flags
52
+ count_only = "c" in flags
53
+ files_only = "l" in flags
54
+ whole_word = "w" in flags
55
+ fixed = "F" in flags
56
+
57
+ if fixed:
58
+ needle = pattern
59
+ else:
60
+ pat = pattern
61
+ if whole_word:
62
+ pat = r"\b" + pat + r"\b"
63
+ re_flags = re.IGNORECASE if ignore_case else 0
64
+ try:
65
+ compiled = re.compile(pat, re_flags)
66
+ except re.error as exc:
67
+ raise UnsupportedConstruct("malformed-syntax", f"grep: invalid pattern: {exc}") from exc
68
+
69
+ def matches(line: str) -> bool:
70
+ if fixed:
71
+ hay = line.lower() if ignore_case else line
72
+ n = needle.lower() if ignore_case else needle
73
+ if whole_word:
74
+ return re.search(r"(?<!\w)" + re.escape(n) + r"(?!\w)", hay) is not None
75
+ return n in hay
76
+ return compiled.search(line) is not None
77
+
78
+ def read_lines(data: bytes) -> list[str]:
79
+ text = data.decode("utf-8", errors="replace")
80
+ if text == "":
81
+ return []
82
+ lines = text.split("\n")
83
+ if lines and lines[-1] == "":
84
+ lines.pop()
85
+ return lines
86
+
87
+ multi_file = len(files) > 1
88
+ any_match = False
89
+ any_error = False
90
+ out_lines: list[str] = []
91
+
92
+ def process(name: str, lines: list[str]) -> None:
93
+ nonlocal any_match
94
+ matched = [ln for ln in lines if matches(ln) != invert]
95
+ if matched:
96
+ any_match = True
97
+ if count_only:
98
+ prefix = f"{name}:" if multi_file else ""
99
+ out_lines.append(f"{prefix}{len(matched)}")
100
+ return
101
+ if files_only:
102
+ if matched:
103
+ out_lines.append(name)
104
+ return
105
+ for i, ln in enumerate(lines):
106
+ if matches(ln) == invert:
107
+ continue
108
+ prefix = ""
109
+ if multi_file:
110
+ prefix += f"{name}:"
111
+ if show_lineno:
112
+ prefix += f"{i + 1}:"
113
+ out_lines.append(f"{prefix}{ln}")
114
+
115
+ if not files:
116
+ data = ctx.stdin.read()
117
+ process("(standard input)", read_lines(data))
118
+ else:
119
+ for name in files:
120
+ try:
121
+ with open(_resolve(ctx.cwd, name), "rb") as fh:
122
+ data = fh.read()
123
+ except OSError as exc:
124
+ out_lines_msg = f"grep: {name}: {exc.strerror or exc}"
125
+ ctx.stdout.write((out_lines_msg + "\n").encode("utf-8"))
126
+ any_error = True
127
+ continue
128
+ process(name, read_lines(data))
129
+
130
+ if out_lines:
131
+ ctx.stdout.write(("\n".join(out_lines) + "\n").encode("utf-8"))
132
+
133
+ if any_error:
134
+ return 2
135
+ return 0 if any_match else 1
136
+
137
+
138
+ def _parse_sed_replacement(repl: str) -> str:
139
+ """Translate sed REPL (\\1..\\9, &, \\&) into Python re replacement syntax."""
140
+ out: list[str] = []
141
+ i = 0
142
+ n = len(repl)
143
+ while i < n:
144
+ ch = repl[i]
145
+ if ch == "\\" and i + 1 < n:
146
+ nxt = repl[i + 1]
147
+ if nxt.isdigit():
148
+ out.append("\\g<" + nxt + ">")
149
+ i += 2
150
+ continue
151
+ if nxt == "&":
152
+ out.append("&")
153
+ i += 2
154
+ continue
155
+ if nxt == "\\":
156
+ out.append("\\\\")
157
+ i += 2
158
+ continue
159
+ out.append(nxt)
160
+ i += 2
161
+ continue
162
+ if ch == "&":
163
+ out.append("\\g<0>")
164
+ i += 1
165
+ continue
166
+ if ch == "\\":
167
+ out.append("\\\\")
168
+ i += 1
169
+ continue
170
+ out.append(ch)
171
+ i += 1
172
+ return "".join(out)
173
+
174
+
175
+ def _parse_sed_script(script: str) -> tuple[str, str, bool, bool]:
176
+ if len(script) < 2 or script[0] != "s":
177
+ raise UnsupportedConstruct("unsupported-flag", "sed: only s/// scripts are supported")
178
+ delim = script[1]
179
+ if not delim or delim.isalnum() or delim == "\\":
180
+ raise UnsupportedConstruct("malformed-syntax", "sed: invalid delimiter")
181
+ parts: list[str] = []
182
+ current: list[str] = []
183
+ i = 2
184
+ n = len(script)
185
+ while i < n:
186
+ ch = script[i]
187
+ if ch == "\\" and i + 1 < n and script[i + 1] == delim:
188
+ current.append(delim)
189
+ i += 2
190
+ continue
191
+ if ch == delim:
192
+ parts.append("".join(current))
193
+ current = []
194
+ i += 1
195
+ if len(parts) == 2:
196
+ break
197
+ continue
198
+ current.append(ch)
199
+ i += 1
200
+ if len(parts) != 2:
201
+ raise UnsupportedConstruct("malformed-syntax", "sed: unterminated s/// script")
202
+ remainder = script[i:]
203
+ global_flag = "g" in remainder
204
+ icase_flag = "i" in remainder
205
+ for c in remainder:
206
+ if c not in "gi":
207
+ raise UnsupportedConstruct("unsupported-flag", f"sed: unsupported flag '{c}'")
208
+ pattern, repl = parts
209
+ return pattern, repl, global_flag, icase_flag
210
+
211
+
212
+ def cmd_sed(ctx: BuiltinContext) -> int:
213
+ args = ctx.argv[1:]
214
+ if not args:
215
+ raise UnsupportedConstruct("unsupported-flag", "sed: SCRIPT operand required")
216
+ for a in args:
217
+ if a.startswith("-") and a != "-":
218
+ raise UnsupportedConstruct("unsupported-flag", f"sed: unsupported flag {a!r}")
219
+ script = args[0]
220
+ files = args[1:]
221
+ pattern, repl, global_flag, icase_flag = _parse_sed_script(script)
222
+ re_flags = re.IGNORECASE if icase_flag else 0
223
+ try:
224
+ compiled = re.compile(pattern, re_flags)
225
+ except re.error as exc:
226
+ raise UnsupportedConstruct("malformed-syntax", f"sed: invalid pattern: {exc}") from exc
227
+ py_repl = _parse_sed_replacement(repl)
228
+ count = 0 if global_flag else 1
229
+
230
+ def transform(data: bytes) -> bytes:
231
+ text = data.decode("utf-8", errors="replace")
232
+ if text == "":
233
+ return b""
234
+ had_trailing_newline = text.endswith("\n")
235
+ body = text[:-1] if had_trailing_newline else text
236
+ lines = body.split("\n") if body or had_trailing_newline else [""]
237
+ if body == "" and not had_trailing_newline:
238
+ lines = [""]
239
+ result_lines = [compiled.sub(py_repl, ln, count=count) for ln in lines]
240
+ out = "\n".join(result_lines)
241
+ if had_trailing_newline:
242
+ out += "\n"
243
+ return out.encode("utf-8")
244
+
245
+ if not files:
246
+ ctx.stdout.write(transform(ctx.stdin.read()))
247
+ return 0
248
+ for name in files:
249
+ with open(_resolve(ctx.cwd, name), "rb") as fh:
250
+ data = fh.read()
251
+ ctx.stdout.write(transform(data))
252
+ return 0