@hassangameryt/freeflow 0.2.0 → 0.4.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 (312) hide show
  1. package/.agents/plugins/marketplace.json +1 -1
  2. package/.claude-plugin/marketplace.json +4 -4
  3. package/{plugins/freeflow/.claude-plugin → .claude-plugin}/plugin.json +5 -2
  4. package/{plugins/freeflow/.codex-plugin → .codex-plugin}/plugin.json +8 -9
  5. package/README.md +194 -127
  6. package/assets/feedback-loop.png +0 -0
  7. package/assets/freeflow-plugin-icon.png +0 -0
  8. package/assets/freeflow_workflow_map.png +0 -0
  9. package/assets/workflow.png +0 -0
  10. package/command-surface.json +129 -0
  11. package/hooks/freeflow-runtime-context.mjs +419 -0
  12. package/hooks/tests/check-runtime-context-hook.sh +223 -0
  13. package/package.json +41 -22
  14. package/pi-extension/dist/index.js +269 -0
  15. package/pi-extension/dist/integrations/mcp-config.js +29 -0
  16. package/pi-extension/dist/output-router/host-producer-identification.js +232 -0
  17. package/pi-extension/dist/output-router/native-safety-net.js +216 -0
  18. package/pi-extension/dist/output-router/observed-tool-routing.js +122 -0
  19. package/pi-extension/dist/output-router/renderers.js +1023 -0
  20. package/pi-extension/dist/output-router/router-tools.js +554 -0
  21. package/pi-extension/dist/output-router/schemas.js +383 -0
  22. package/pi-extension/dist/output-router/status.js +744 -0
  23. package/pi-extension/dist/output-router/utils.js +502 -0
  24. package/pi-extension/dist/runtime/runtime-context.js +798 -0
  25. package/pi-extension/dist/settings/settings-tui.js +314 -0
  26. package/pi-extension/dist/settings/settings-ui.js +1487 -0
  27. package/pi-extension/freeflow/index.js +1 -0
  28. package/pi-extension/src/index.ts +295 -0
  29. package/pi-extension/src/integrations/mcp-config.ts +32 -0
  30. package/pi-extension/src/output-router/host-producer-identification.ts +256 -0
  31. package/pi-extension/src/output-router/native-safety-net.ts +248 -0
  32. package/pi-extension/src/output-router/observed-tool-routing.ts +134 -0
  33. package/pi-extension/src/output-router/renderers.ts +1108 -0
  34. package/pi-extension/src/output-router/router-tools.ts +579 -0
  35. package/pi-extension/src/output-router/schemas.ts +387 -0
  36. package/pi-extension/src/output-router/status.ts +791 -0
  37. package/pi-extension/src/output-router/utils.ts +554 -0
  38. package/pi-extension/src/runtime/runtime-context.ts +895 -0
  39. package/pi-extension/src/settings/settings-tui.ts +408 -0
  40. package/pi-extension/src/settings/settings-ui.ts +1730 -0
  41. package/pi-extension/tests/integration/pi-extension.test.js +4018 -0
  42. package/pi-extension/tests/output-router/observed-producers.test.js +132 -0
  43. package/pi-extension/tests/output-router/search.test.js +722 -0
  44. package/pi-extension/tests/settings/settings-tui.test.js +243 -0
  45. package/pi-extension/tsconfig.json +16 -0
  46. package/{plugins/freeflow/router/dist → router/dist/benchmarks}/benchmark-harness.d.ts +16 -13
  47. package/router/dist/benchmarks/benchmark-harness.js +92 -0
  48. package/router/dist/benchmarks/benchmarks.d.ts +109 -0
  49. package/router/dist/benchmarks/benchmarks.js +979 -0
  50. package/router/dist/benchmarks/codex-qa-benchmarks.d.ts +95 -0
  51. package/router/dist/benchmarks/codex-qa-benchmarks.js +500 -0
  52. package/router/dist/benchmarks/command-benchmarks.d.ts +143 -0
  53. package/router/dist/benchmarks/command-benchmarks.js +698 -0
  54. package/router/dist/benchmarks/context-mode-normalized-benchmarks.d.ts +82 -0
  55. package/router/dist/benchmarks/context-mode-normalized-benchmarks.js +773 -0
  56. package/router/dist/benchmarks/context-mode-real-deep-benchmark.d.ts +132 -0
  57. package/router/dist/benchmarks/context-mode-real-deep-benchmark.js +1380 -0
  58. package/router/dist/benchmarks/context-saving-baseline.d.ts +60 -0
  59. package/router/dist/benchmarks/context-saving-baseline.js +470 -0
  60. package/router/dist/benchmarks/index-benchmarks.d.ts +103 -0
  61. package/router/dist/benchmarks/index-benchmarks.js +862 -0
  62. package/router/dist/benchmarks/storage-policy-benchmarks.d.ts +101 -0
  63. package/router/dist/benchmarks/storage-policy-benchmarks.js +618 -0
  64. package/router/dist/benchmarks/vault-index-benchmarks.d.ts +56 -0
  65. package/router/dist/benchmarks/vault-index-benchmarks.js +322 -0
  66. package/router/dist/config/config.d.ts +103 -0
  67. package/router/dist/config/config.js +632 -0
  68. package/{plugins/freeflow/router/dist → router/dist/config}/router-contract.d.ts +2 -2
  69. package/router/dist/config/router-contract.js +54 -0
  70. package/{plugins/freeflow/router/dist → router/dist/config}/schema.d.ts +19 -10
  71. package/router/dist/config/schema.js +648 -0
  72. package/router/dist/config/types.d.ts +573 -0
  73. package/router/dist/config/types.js +41 -0
  74. package/router/dist/evidence/bounded-evidence.d.ts +40 -0
  75. package/router/dist/evidence/bounded-evidence.js +323 -0
  76. package/router/dist/evidence/evidence-range-selector.d.ts +24 -0
  77. package/router/dist/evidence/evidence-range-selector.js +232 -0
  78. package/router/dist/evidence/evidence-search.d.ts +27 -0
  79. package/router/dist/evidence/evidence-search.js +490 -0
  80. package/{plugins/freeflow/router/dist → router/dist/evidence}/evidence.d.ts +18 -18
  81. package/router/dist/evidence/evidence.js +200 -0
  82. package/router/dist/evidence/failure-contracts.d.ts +37 -0
  83. package/router/dist/evidence/failure-contracts.js +166 -0
  84. package/router/dist/evidence/line-ranges.d.ts +24 -0
  85. package/router/dist/evidence/line-ranges.js +22 -0
  86. package/router/dist/experiments/local-index.d.ts +64 -0
  87. package/router/dist/experiments/local-index.js +581 -0
  88. package/router/dist/index.d.ts +30 -0
  89. package/router/dist/index.js +32 -0
  90. package/router/dist/local/local-traversal.d.ts +18 -0
  91. package/router/dist/local/local-traversal.js +334 -0
  92. package/router/dist/processing/engine.d.ts +132 -0
  93. package/router/dist/processing/engine.js +533 -0
  94. package/router/dist/processing/reducers.d.ts +280 -0
  95. package/router/dist/processing/reducers.js +1673 -0
  96. package/router/dist/processing/renderers.d.ts +31 -0
  97. package/router/dist/processing/renderers.js +195 -0
  98. package/router/dist/processing/scripts.d.ts +91 -0
  99. package/router/dist/processing/scripts.js +386 -0
  100. package/{plugins/freeflow/router/dist → router/dist/repo}/repo-traversal.d.ts +9 -9
  101. package/router/dist/repo/repo-traversal.js +245 -0
  102. package/router/dist/routing/observed-reducers.d.ts +41 -0
  103. package/router/dist/routing/observed-reducers.js +661 -0
  104. package/router/dist/routing/observed-routing.d.ts +26 -0
  105. package/router/dist/routing/observed-routing.js +378 -0
  106. package/router/dist/routing/parsers.d.ts +16 -0
  107. package/router/dist/routing/parsers.js +521 -0
  108. package/router/dist/routing/run-filters.d.ts +60 -0
  109. package/router/dist/routing/run-filters.js +238 -0
  110. package/router/dist/routing/run-reducers.d.ts +49 -0
  111. package/router/dist/routing/run-reducers.js +122 -0
  112. package/router/dist/sandbox/adapter-roots.d.ts +46 -0
  113. package/router/dist/sandbox/adapter-roots.js +66 -0
  114. package/router/dist/sandbox/eryx-python-adapter.d.ts +13 -0
  115. package/router/dist/sandbox/eryx-python-adapter.js +868 -0
  116. package/router/dist/sandbox/jq-wasm-adapter.d.ts +9 -0
  117. package/router/dist/sandbox/jq-wasm-adapter.js +429 -0
  118. package/router/dist/sandbox/quickjs-wasi-adapter.d.ts +13 -0
  119. package/router/dist/sandbox/quickjs-wasi-adapter.js +384 -0
  120. package/router/dist/sandbox/script-sandbox.d.ts +134 -0
  121. package/router/dist/sandbox/script-sandbox.js +337 -0
  122. package/router/dist/setup/script-transform-adapters.d.ts +32 -0
  123. package/router/dist/setup/script-transform-adapters.js +278 -0
  124. package/router/dist/tools/batch.d.ts +33 -0
  125. package/router/dist/tools/batch.js +586 -0
  126. package/router/dist/tools/run.d.ts +62 -0
  127. package/router/dist/tools/run.js +1304 -0
  128. package/router/dist/tools/search.d.ts +66 -0
  129. package/router/dist/tools/search.js +1364 -0
  130. package/router/dist/transform/engine.d.ts +168 -0
  131. package/router/dist/transform/engine.js +2072 -0
  132. package/router/dist/vault/vault-index.d.ts +125 -0
  133. package/router/dist/vault/vault-index.js +416 -0
  134. package/router/dist/vault/vault.d.ts +138 -0
  135. package/router/dist/vault/vault.js +630 -0
  136. package/runtime/interaction-contract.md +16 -0
  137. package/skills/bypass/SKILL.md +82 -0
  138. package/skills/commit-work/SKILL.md +106 -0
  139. package/skills/commit-work/references/staging-decisions.md +73 -0
  140. package/skills/decision-gate/SKILL.md +48 -0
  141. package/skills/design-for-depth/SKILL.md +106 -0
  142. package/skills/design-for-depth/references/design-pressure-signals.md +314 -0
  143. package/skills/design-for-depth/references/interface-design-loop.md +156 -0
  144. package/skills/design-for-depth/references/software-design-philosophy.md +248 -0
  145. package/skills/diagnose-failure/SKILL.md +105 -0
  146. package/skills/diagnose-failure/references/diagnostic-loop-catalog.md +36 -0
  147. package/skills/diagnose-failure/references/flaky-and-performance.md +43 -0
  148. package/skills/discuss/SKILL.md +121 -0
  149. package/skills/discuss/references/checkpoints.md +66 -0
  150. package/skills/evaluate-skill/SKILL.md +105 -0
  151. package/skills/evaluate-skill/references/definition-schema.md +345 -0
  152. package/skills/evaluate-skill/references/evaluation-design.md +86 -0
  153. package/skills/evaluate-skill/references/execution-and-evidence.md +85 -0
  154. package/skills/evaluate-skill/references/review-and-revision.md +51 -0
  155. package/skills/evaluate-skill/scripts/lib/definitions.mjs +296 -0
  156. package/skills/evaluate-skill/scripts/lib/evidence.mjs +46 -0
  157. package/skills/evaluate-skill/scripts/lib/grade-comparison.mjs +60 -0
  158. package/skills/evaluate-skill/scripts/lib/grade-interaction.mjs +148 -0
  159. package/skills/evaluate-skill/scripts/lib/grade-support.mjs +126 -0
  160. package/skills/evaluate-skill/scripts/lib/grade-workspace.mjs +192 -0
  161. package/skills/evaluate-skill/scripts/lib/grade.mjs +56 -0
  162. package/skills/evaluate-skill/scripts/lib/pi.mjs +489 -0
  163. package/skills/evaluate-skill/scripts/lib/process.mjs +79 -0
  164. package/skills/evaluate-skill/scripts/lib/rpc.mjs +444 -0
  165. package/skills/evaluate-skill/scripts/lib/runner.mjs +346 -0
  166. package/skills/evaluate-skill/scripts/lib/sandbox.mjs +409 -0
  167. package/skills/evaluate-skill/scripts/lib/view.mjs +337 -0
  168. package/skills/evaluate-skill/scripts/pi-guard.mjs +164 -0
  169. package/skills/evaluate-skill/scripts/skill-eval.mjs +101 -0
  170. package/skills/execute-work/SKILL.md +99 -0
  171. package/skills/execute-work/references/code-practices.md +78 -0
  172. package/skills/execute-work/references/execution-loop.md +77 -0
  173. package/skills/finish-branch/SKILL.md +117 -0
  174. package/skills/finish-branch/references/integration-options.md +80 -0
  175. package/skills/handoff/SKILL.md +92 -0
  176. package/skills/handoff/references/templates.md +141 -0
  177. package/skills/launch-work/SKILL.md +116 -0
  178. package/skills/launch-work/references/launch-readiness.md +85 -0
  179. package/skills/migration-work/SKILL.md +115 -0
  180. package/skills/migration-work/references/migration-lifecycle.md +82 -0
  181. package/skills/mode-contract/SKILL.md +82 -0
  182. package/skills/output-router/SKILL.md +206 -0
  183. package/skills/output-router/references/safety-policy.md +40 -0
  184. package/skills/release-work/SKILL.md +118 -0
  185. package/skills/release-work/references/release-evidence.md +78 -0
  186. package/skills/review-artifact/SKILL.md +126 -0
  187. package/skills/review-artifact/references/reviewer-prompt.md +148 -0
  188. package/skills/review-work/SKILL.md +126 -0
  189. package/skills/review-work/references/reviewer-prompt.md +134 -0
  190. package/skills/review-work/references/security-risk-lens.md +67 -0
  191. package/skills/setup-freeflow/SKILL.md +98 -0
  192. package/skills/setup-freeflow/references/activation-contract.md +120 -0
  193. package/skills/setup-freeflow/references/host-setup.md +72 -0
  194. package/skills/setup-freeflow/references/output-router-setup.md +110 -0
  195. package/skills/simplify-code/SKILL.md +80 -0
  196. package/skills/simplify-code/references/simplification-patterns.md +70 -0
  197. package/skills/tdd/SKILL.md +96 -0
  198. package/skills/tdd/references/test-design.md +96 -0
  199. package/skills/track-work/SKILL.md +84 -0
  200. package/skills/track-work/references/working-record-schema.md +378 -0
  201. package/skills/verify-work/SKILL.md +85 -0
  202. package/skills/verify-work/references/browser-runtime-evidence.md +55 -0
  203. package/skills/verify-work/references/integration-evidence.md +51 -0
  204. package/skills/verify-work/references/performance-evidence.md +57 -0
  205. package/skills/workflow/SKILL.md +91 -0
  206. package/skills/workflow/references/domain-skill-composition.md +57 -0
  207. package/skills/workflow/references/workflow-loop.md +92 -0
  208. package/skills/write-plan/SKILL.md +123 -0
  209. package/skills/write-plan/references/plan-shapes.md +156 -0
  210. package/skills/write-skill/SKILL.md +119 -0
  211. package/skills/write-skill/references/activation-boundaries.md +62 -0
  212. package/skills/write-skill/references/agent-first-instructions.md +87 -0
  213. package/skills/write-skill/references/progressive-disclosure.md +63 -0
  214. package/skills/write-skill/scripts/lib/skill-author.mjs +653 -0
  215. package/skills/write-skill/scripts/skill-author.mjs +104 -0
  216. package/skills/write-spec/SKILL.md +104 -0
  217. package/skills/write-spec/references/artifact-standards.md +86 -0
  218. package/skills/write-spec/references/decision-records.md +77 -0
  219. package/skills/write-spec/references/spec-shapes.md +128 -0
  220. package/plugins/freeflow/command-surface.json +0 -77
  221. package/plugins/freeflow/docs/README.md +0 -40
  222. package/plugins/freeflow/docs/adr/0001-three-modes.md +0 -27
  223. package/plugins/freeflow/docs/adr/0002-evals-before-hooks.md +0 -17
  224. package/plugins/freeflow/docs/adr/0003-release-boundary.md +0 -21
  225. package/plugins/freeflow/docs/adr/0004-research-replaces-shallow-discovery-skills.md +0 -26
  226. package/plugins/freeflow/docs/adr/README.md +0 -8
  227. package/plugins/freeflow/docs/architecture.md +0 -86
  228. package/plugins/freeflow/docs/output-router.md +0 -212
  229. package/plugins/freeflow/docs/release-evidence.md +0 -69
  230. package/plugins/freeflow/docs/skills.md +0 -43
  231. package/plugins/freeflow/docs/workflow.md +0 -57
  232. package/plugins/freeflow/hooks/freeflow-runtime-context.mjs +0 -257
  233. package/plugins/freeflow/pi-extension/index.js +0 -1176
  234. package/plugins/freeflow/router/dist/benchmark-harness.js +0 -92
  235. package/plugins/freeflow/router/dist/benchmarks.d.ts +0 -102
  236. package/plugins/freeflow/router/dist/benchmarks.js +0 -880
  237. package/plugins/freeflow/router/dist/bounded-evidence.d.ts +0 -40
  238. package/plugins/freeflow/router/dist/bounded-evidence.js +0 -310
  239. package/plugins/freeflow/router/dist/codex-qa-benchmarks.d.ts +0 -91
  240. package/plugins/freeflow/router/dist/codex-qa-benchmarks.js +0 -461
  241. package/plugins/freeflow/router/dist/command-benchmarks.d.ts +0 -128
  242. package/plugins/freeflow/router/dist/command-benchmarks.js +0 -652
  243. package/plugins/freeflow/router/dist/config.d.ts +0 -22
  244. package/plugins/freeflow/router/dist/config.js +0 -114
  245. package/plugins/freeflow/router/dist/evidence-range-selector.d.ts +0 -22
  246. package/plugins/freeflow/router/dist/evidence-range-selector.js +0 -180
  247. package/plugins/freeflow/router/dist/evidence-search.d.ts +0 -25
  248. package/plugins/freeflow/router/dist/evidence-search.js +0 -450
  249. package/plugins/freeflow/router/dist/evidence.js +0 -196
  250. package/plugins/freeflow/router/dist/experimental-local-index.d.ts +0 -1
  251. package/plugins/freeflow/router/dist/experimental-local-index.js +0 -1
  252. package/plugins/freeflow/router/dist/experiments/local-index.d.ts +0 -58
  253. package/plugins/freeflow/router/dist/experiments/local-index.js +0 -562
  254. package/plugins/freeflow/router/dist/index-benchmarks.d.ts +0 -84
  255. package/plugins/freeflow/router/dist/index-benchmarks.js +0 -458
  256. package/plugins/freeflow/router/dist/index.d.ts +0 -12
  257. package/plugins/freeflow/router/dist/index.js +0 -12
  258. package/plugins/freeflow/router/dist/line-ranges.d.ts +0 -22
  259. package/plugins/freeflow/router/dist/line-ranges.js +0 -22
  260. package/plugins/freeflow/router/dist/parsers.d.ts +0 -16
  261. package/plugins/freeflow/router/dist/parsers.js +0 -473
  262. package/plugins/freeflow/router/dist/repo-traversal.js +0 -242
  263. package/plugins/freeflow/router/dist/retrieve.d.ts +0 -33
  264. package/plugins/freeflow/router/dist/retrieve.js +0 -855
  265. package/plugins/freeflow/router/dist/router-contract.js +0 -54
  266. package/plugins/freeflow/router/dist/run.d.ts +0 -26
  267. package/plugins/freeflow/router/dist/run.js +0 -356
  268. package/plugins/freeflow/router/dist/schema.js +0 -354
  269. package/plugins/freeflow/router/dist/types.d.ts +0 -234
  270. package/plugins/freeflow/router/dist/types.js +0 -9
  271. package/plugins/freeflow/router/dist/vault.d.ts +0 -80
  272. package/plugins/freeflow/router/dist/vault.js +0 -484
  273. package/plugins/freeflow/skills/bypass/SKILL.md +0 -68
  274. package/plugins/freeflow/skills/commit-work/SKILL.md +0 -98
  275. package/plugins/freeflow/skills/commit-work/references/staging-decisions.md +0 -51
  276. package/plugins/freeflow/skills/diagnose-failure/SKILL.md +0 -95
  277. package/plugins/freeflow/skills/diagnose-failure/references/feedback-loop-catalog.md +0 -25
  278. package/plugins/freeflow/skills/diagnose-failure/references/flaky-and-performance.md +0 -31
  279. package/plugins/freeflow/skills/evaluate-skill/SKILL.md +0 -96
  280. package/plugins/freeflow/skills/evaluate-skill/references/eval-patterns.md +0 -41
  281. package/plugins/freeflow/skills/evaluate-skill/references/grading-priority.md +0 -33
  282. package/plugins/freeflow/skills/execute-plan/SKILL.md +0 -132
  283. package/plugins/freeflow/skills/execute-plan/references/execution-map.md +0 -122
  284. package/plugins/freeflow/skills/handoff/SKILL.md +0 -66
  285. package/plugins/freeflow/skills/handoff/references/templates.md +0 -64
  286. package/plugins/freeflow/skills/interview-gate/SKILL.md +0 -90
  287. package/plugins/freeflow/skills/mode-contract/SKILL.md +0 -79
  288. package/plugins/freeflow/skills/output-router/SKILL.md +0 -69
  289. package/plugins/freeflow/skills/output-router/references/safety-policy.md +0 -33
  290. package/plugins/freeflow/skills/research/ARTIFACT-DESTINATIONS.md +0 -58
  291. package/plugins/freeflow/skills/research/CHECKPOINTS.md +0 -74
  292. package/plugins/freeflow/skills/research/LANGUAGE.md +0 -61
  293. package/plugins/freeflow/skills/research/SKILL.md +0 -128
  294. package/plugins/freeflow/skills/review-artifact/SKILL.md +0 -115
  295. package/plugins/freeflow/skills/review-artifact/references/reviewer-prompt.md +0 -56
  296. package/plugins/freeflow/skills/review-work/SKILL.md +0 -96
  297. package/plugins/freeflow/skills/review-work/references/reviewer-prompt.md +0 -97
  298. package/plugins/freeflow/skills/setup-freeflow/SKILL.md +0 -94
  299. package/plugins/freeflow/skills/setup-freeflow/references/activation-contract.md +0 -99
  300. package/plugins/freeflow/skills/setup-freeflow/references/host-setup.md +0 -35
  301. package/plugins/freeflow/skills/setup-freeflow/references/output-router-setup.md +0 -52
  302. package/plugins/freeflow/skills/verify-work/SKILL.md +0 -62
  303. package/plugins/freeflow/skills/workflow/SKILL.md +0 -95
  304. package/plugins/freeflow/skills/workflow/references/workflow-map.md +0 -102
  305. package/plugins/freeflow/skills/write-plan/SKILL.md +0 -88
  306. package/plugins/freeflow/skills/write-plan/references/plan-shapes.md +0 -70
  307. package/plugins/freeflow/skills/write-skill/SKILL.md +0 -88
  308. package/plugins/freeflow/skills/write-spec/SKILL.md +0 -90
  309. package/plugins/freeflow/skills/write-spec/references/artifact-standards.md +0 -67
  310. package/plugins/freeflow/skills/write-spec/references/spec-shapes.md +0 -69
  311. /package/{plugins/freeflow/assets → assets}/freeflow-plugin-icon-transparent.png +0 -0
  312. /package/{plugins/freeflow/hooks → hooks}/hooks.json +0 -0
@@ -0,0 +1,1108 @@
1
+ import {
2
+ extractTextContent,
3
+ formatStatus,
4
+ oneLine,
5
+ shortenMiddle,
6
+ splitLines,
7
+ statusIcon,
8
+ textComponent,
9
+ themeBold,
10
+ themeFg,
11
+ truncateText,
12
+ } from "./utils.js";
13
+
14
+ export function commandLabel(command) {
15
+ if (Array.isArray(command)) {
16
+ return truncateText(command.join(" "), 120);
17
+ }
18
+ return truncateText(command ?? "...", 120);
19
+ }
20
+
21
+ function runProducerLabel(args) {
22
+ if (args?.script?.language) {
23
+ const label = args.script.label ? ` ${args.script.label}` : "";
24
+ return `script:${args.script.language}${label}`;
25
+ }
26
+ return `$ ${commandLabel(args?.command)}`;
27
+ }
28
+
29
+ function retrieveSourceLabel(source) {
30
+ if (!source || source.kind === "repo") {
31
+ return `repo ${shortenMiddle(source?.path ?? ".", 80)}`;
32
+ }
33
+ if (source.kind === "local") {
34
+ const path = source.path ? `:${source.path}` : "";
35
+ return `local ${shortenMiddle(source.root ?? ".", 60)}${shortenMiddle(path, 50)}`;
36
+ }
37
+ if (source.kind === "vault") {
38
+ const stream = source.stream ? `:${source.stream}` : "";
39
+ return `vault ${shortenMiddle(source.outputId ?? "...", 48)}${stream}`;
40
+ }
41
+ return oneLine(source.kind ?? "source");
42
+ }
43
+
44
+ function evidenceLabel(evidence) {
45
+ if (!evidence) {
46
+ return "no evidence";
47
+ }
48
+ const source = evidence.source;
49
+ const path =
50
+ evidence.path ??
51
+ (source?.kind === "repo"
52
+ ? source.path
53
+ : source?.kind === "local"
54
+ ? source.path
55
+ : source?.kind === "vault"
56
+ ? source.outputId
57
+ : "evidence");
58
+ const lines = evidence.lines ? `:${evidence.lines}` : "";
59
+ return `${shortenMiddle(path, 90)}${lines}`;
60
+ }
61
+
62
+ function excerptLines(theme, excerpt, indent = " ", maxLines = 8) {
63
+ const lines = splitLines(String(excerpt ?? ""));
64
+ const shown = lines
65
+ .slice(0, maxLines)
66
+ .map((line) => `${indent}${themeFg(theme, "toolOutput", truncateText(line, 140))}`);
67
+ const omitted = lines.length - shown.length;
68
+ if (omitted > 0) {
69
+ shown.push(`${indent}${themeFg(theme, "dim", `… ${omitted} more line(s)`)}`);
70
+ }
71
+ return shown;
72
+ }
73
+
74
+ function routeSummaryLine(theme, result) {
75
+ const route = result?.routing?.status ?? "unknown";
76
+ const preserve = result?.preserve ? ` • preserve: ${result.preserve}` : "";
77
+ return `${themeFg(theme, "muted", "routing:")} ${formatStatus(theme, route)}${themeFg(theme, "dim", preserve)}`;
78
+ }
79
+
80
+ function evidenceSourceSummary(source) {
81
+ if (!source || typeof source !== "object") {
82
+ return "unknown";
83
+ }
84
+ if (source.kind === "repo") {
85
+ return `repo ${source.path ?? "."}`;
86
+ }
87
+ if (source.kind === "local") {
88
+ const path = source.path ? `:${source.path}` : "";
89
+ return `local ${source.root ?? "."}${path}`;
90
+ }
91
+ if (source.kind === "vault") {
92
+ const stream = source.stream ? `:${source.stream}` : "";
93
+ return `vault ${source.outputId ?? "..."}${stream}`;
94
+ }
95
+ return oneLine(source.kind ?? "source");
96
+ }
97
+
98
+ function runFilterLabel(filters) {
99
+ if (!filters || typeof filters !== "object") {
100
+ return "";
101
+ }
102
+ const parts = [`stream=${filters.stream ?? "selected"}`];
103
+ if (Array.isArray(filters.include) && filters.include.length > 0) {
104
+ parts.push(`include=${filters.include.join("|")}`);
105
+ }
106
+ if (Array.isArray(filters.exclude) && filters.exclude.length > 0) {
107
+ parts.push(`exclude=${filters.exclude.join("|")}`);
108
+ }
109
+ if (filters.flags) {
110
+ parts.push(`flags=${filters.flags}`);
111
+ }
112
+ for (const key of ["head", "tail", "maxLines", "maxBytes"] as const) {
113
+ if (filters[key] !== undefined) {
114
+ parts.push(`${key}=${filters[key]}`);
115
+ }
116
+ }
117
+ if (filters.selectedLines !== undefined && filters.sourceLines !== undefined) {
118
+ parts.push(`selected=${filters.selectedLines}/${filters.sourceLines}`);
119
+ }
120
+ if (filters.fallbackPreservedFailureEvidence) {
121
+ parts.push("fallback=failure-evidence");
122
+ }
123
+ return parts.join(" ");
124
+ }
125
+
126
+ function runScriptProducerLabel(scriptProducer) {
127
+ if (!scriptProducer || typeof scriptProducer !== "object") {
128
+ return "";
129
+ }
130
+ const parts = [
131
+ `${scriptProducer.language ?? "script"}:${scriptProducer.status ?? "unknown"}`,
132
+ `policy=${scriptProducer.policy ?? "sandboxed"}`,
133
+ ];
134
+ if (scriptProducer.label) {
135
+ parts.push(`label=${scriptProducer.label}`);
136
+ }
137
+ if (scriptProducer.adapterId) {
138
+ parts.push(`adapter=${scriptProducer.adapterId}`);
139
+ }
140
+ if (scriptProducer.codeSha256) {
141
+ parts.push(`code=${scriptProducer.codeSha256}`);
142
+ }
143
+ if (scriptProducer.stdoutBytes !== undefined || scriptProducer.stderrBytes !== undefined) {
144
+ parts.push(`bytes=${scriptProducer.stdoutBytes ?? 0}/${scriptProducer.stderrBytes ?? 0}`);
145
+ }
146
+ if (scriptProducer.failure?.kind) {
147
+ parts.push(`failure=${scriptProducer.failure.kind}`);
148
+ }
149
+ return parts.join(" ");
150
+ }
151
+
152
+ function runScriptFilterLabel(scriptFilter) {
153
+ if (!scriptFilter || typeof scriptFilter !== "object") {
154
+ return "";
155
+ }
156
+ const parts = [`${scriptFilter.language ?? "script"}:${scriptFilter.status ?? "unknown"}`];
157
+ if (scriptFilter.label) {
158
+ parts.push(`label=${scriptFilter.label}`);
159
+ }
160
+ if (scriptFilter.outputId) {
161
+ parts.push(`transformed=${scriptFilter.outputId}`);
162
+ }
163
+ if (scriptFilter.failure?.kind) {
164
+ parts.push(`failure=${scriptFilter.failure.kind}`);
165
+ }
166
+ return parts.join(" ");
167
+ }
168
+
169
+ function appendStorageSection(lines, theme, routed, outputId, title = "Storage") {
170
+ const persistence = routed?.persistence;
171
+ const recoveryOutputId = persistence?.recoveryOutputId ?? persistence?.outputId ?? routed?.recovery?.outputId;
172
+ const hasStorage = routed?.decisionId || outputId || routed?.recordId || persistence?.status || recoveryOutputId;
173
+ if (!hasStorage) {
174
+ return;
175
+ }
176
+
177
+ lines.push("", themeFg(theme, "toolTitle", title));
178
+ if (routed?.decisionId) {
179
+ lines.push(` ${themeFg(theme, "muted", "decisionId:")} ${themeFg(theme, "accent", routed.decisionId)}`);
180
+ }
181
+ if (outputId) {
182
+ lines.push(` ${themeFg(theme, "muted", "outputId:")} ${themeFg(theme, "accent", outputId)}`);
183
+ }
184
+ if (routed?.recordId) {
185
+ lines.push(` ${themeFg(theme, "muted", "recordId:")} ${themeFg(theme, "accent", routed.recordId)}`);
186
+ }
187
+ if (persistence?.status) {
188
+ lines.push(
189
+ ` ${themeFg(theme, "muted", "persistence:")} ${persistence.status} / ${persistence.recoverability ?? "unknown"}`,
190
+ );
191
+ }
192
+ if (recoveryOutputId && recoveryOutputId !== outputId) {
193
+ lines.push(` ${themeFg(theme, "muted", "recoveryOutputId:")} ${themeFg(theme, "accent", recoveryOutputId)}`);
194
+ }
195
+ }
196
+
197
+ function exactRetrieveHintFromEvidence(packet) {
198
+ if (!packet?.lines) {
199
+ return "";
200
+ }
201
+ const source = packet.source;
202
+ if (source?.kind === "vault" && source.outputId) {
203
+ return `action=retrieve source.kind=vault lineRange=${packet.lines} stream=${source.stream ?? "raw"} outputId=${source.outputId}`;
204
+ }
205
+ if (source?.kind === "local") {
206
+ const path = packet.path ?? source.path;
207
+ return path ? `action=retrieve source.kind=local lineRange=${packet.lines} root=${source.root} path=${path}` : "";
208
+ }
209
+ const path = packet.path ?? (source?.kind === "repo" ? source.path : undefined);
210
+ if (path && source?.kind !== "vault") {
211
+ return `action=retrieve source.kind=repo lineRange=${packet.lines} path=${path}`;
212
+ }
213
+ return "";
214
+ }
215
+
216
+ function firstExactRetrieveHintFromEvidence(evidence) {
217
+ if (!Array.isArray(evidence)) {
218
+ return "";
219
+ }
220
+ for (const packet of evidence) {
221
+ const hint = exactRetrieveHintFromEvidence(packet);
222
+ if (hint) {
223
+ return hint;
224
+ }
225
+ }
226
+ return "";
227
+ }
228
+
229
+ function exactRetrieveHintFromImportantLines(outputId, importantLines) {
230
+ if (!outputId || !Array.isArray(importantLines)) {
231
+ return "";
232
+ }
233
+ const firstSpan = importantLines.find((line) => line?.lines);
234
+ if (!firstSpan) {
235
+ return "";
236
+ }
237
+ return `action=retrieve source.kind=vault lineRange=${firstSpan.lines} stream=${firstSpan.stream ?? "combined"} outputId=${outputId}`;
238
+ }
239
+
240
+ function recoveryStartingPoint(outputId) {
241
+ return outputId
242
+ ? `freeflow_search source.kind=vault outputId=${outputId} (choose stream + lineRange, or query/expand)`
243
+ : "";
244
+ }
245
+
246
+ function routerResultFromToolResult(result) {
247
+ return result?.details?.result;
248
+ }
249
+
250
+ function fallbackResultText(result) {
251
+ const text = extractTextContent(result?.content);
252
+ return text ? truncateText(text, 200) : "No Freeflow result details available.";
253
+ }
254
+
255
+ export function renderFreeflowSearchCall(args, theme) {
256
+ const title = themeFg(theme, "toolTitle", themeBold(theme, "freeflow_search"));
257
+ const action = themeFg(theme, "muted", args?.action ?? "query");
258
+ const source = themeFg(theme, "accent", retrieveSourceLabel(args?.source));
259
+ const query = args?.query ? ` ${themeFg(theme, "dim", `\"${truncateText(args.query, 70)}\"`)}` : "";
260
+ return textComponent(`${title} ${action} ${source}${query}`);
261
+ }
262
+
263
+ export function renderFreeflowRetrieveCall(args, theme) {
264
+ const title = themeFg(theme, "toolTitle", themeBold(theme, "freeflow_search"));
265
+ const action = themeFg(theme, "muted", args?.action ?? "query");
266
+ const source = themeFg(theme, "accent", retrieveSourceLabel(args?.source));
267
+ const query = args?.query ? ` ${themeFg(theme, "dim", `\"${truncateText(args.query, 70)}\"`)}` : "";
268
+ return textComponent(`${title} ${action} ${source}${query}`);
269
+ }
270
+
271
+ export function renderFreeflowRunCall(args, theme) {
272
+ const title = themeFg(theme, "toolTitle", themeBold(theme, "freeflow_run"));
273
+ const command = themeFg(theme, "accent", runProducerLabel(args));
274
+ const extras = [];
275
+ if (args?.preserve) {
276
+ extras.push(`preserve=${args.preserve}`);
277
+ }
278
+ if (args?.timeoutMs !== undefined) {
279
+ extras.push(`timeout=${args.timeoutMs}ms`);
280
+ }
281
+ if (args?.script?.language) {
282
+ extras.push(`producer=${args.script.language}`);
283
+ }
284
+ if (args?.scriptFilter?.language) {
285
+ extras.push(`scriptFilter=${args.scriptFilter.language}`);
286
+ }
287
+ const suffix = extras.length > 0 ? ` ${themeFg(theme, "dim", `(${extras.join(", ")})`)}` : "";
288
+ return textComponent(`${title} ${command}${suffix}`);
289
+ }
290
+
291
+ function transformOperationLabel(operation) {
292
+ if (!operation || typeof operation !== "object") {
293
+ return "operation";
294
+ }
295
+ return String(operation.kind ?? "operation");
296
+ }
297
+
298
+ export function renderFreeflowTransformCall(args, theme) {
299
+ const title = themeFg(theme, "toolTitle", themeBold(theme, "freeflow_search action=transform"));
300
+ const operation = themeFg(theme, "accent", transformOperationLabel(args?.operation));
301
+ const source = themeFg(theme, "muted", retrieveSourceLabel(args?.source));
302
+ const preserve = args?.preserve ? ` ${themeFg(theme, "dim", `(preserve=${args.preserve})`)}` : "";
303
+ return textComponent(`${title} ${operation} ${source}${preserve}`);
304
+ }
305
+
306
+ export function renderFreeflowBatchCall(args, theme) {
307
+ const title = themeFg(theme, "toolTitle", themeBold(theme, "freeflow_batch"));
308
+ const steps = Array.isArray(args?.steps) ? args.steps : [];
309
+ const kinds = steps
310
+ .slice(0, 4)
311
+ .map((step) => step?.kind ?? "step")
312
+ .join(", ");
313
+ const more = steps.length > 4 ? ` +${steps.length - 4}` : "";
314
+ const concurrency = args?.concurrency ? ` ${themeFg(theme, "dim", `(concurrency=${args.concurrency})`)}` : "";
315
+ return textComponent(
316
+ `${title} ${themeFg(theme, "accent", `${steps.length} step(s)`)} ${themeFg(theme, "muted", kinds + more)}${concurrency}`,
317
+ );
318
+ }
319
+
320
+ export function renderFreeflowBatchResult(result, { expanded }: any = {}, theme) {
321
+ const routed = routerResultFromToolResult(result);
322
+ if (!routed) {
323
+ return textComponent(fallbackResultText(result));
324
+ }
325
+
326
+ const failed = routed.failedCount ?? 0;
327
+ const icon = failed > 0 || routed.toolStatus === "error" ? "✗" : statusIcon(routed.toolStatus);
328
+ const steps = Array.isArray(routed.steps) ? routed.steps : [];
329
+ const lines = [
330
+ `${themeFg(theme, failed > 0 ? "warning" : "success", icon)} ${themeFg(theme, "toolTitle", "freeflow_batch")} ${themeFg(theme, "muted", `${routed.okCount ?? 0}/${routed.stepCount ?? steps.length} ok`)} • ${routeSummaryLine(theme, routed)}`,
331
+ ];
332
+ if (routed.summary) {
333
+ lines.push(themeFg(theme, "muted", truncateText(routed.summary, 160)));
334
+ }
335
+ const queryAnswers = Array.isArray(routed.queries) ? routed.queries : [];
336
+ if (queryAnswers.length > 0) {
337
+ lines.push(
338
+ themeFg(
339
+ theme,
340
+ "dim",
341
+ `${queryAnswers.filter((answer) => answer?.status === "answered").length}/${queryAnswers.length} query answer(s) from child evidence handles`,
342
+ ),
343
+ );
344
+ for (const answer of queryAnswers.slice(0, 3)) {
345
+ lines.push(themeFg(theme, "muted", truncateText(answer?.summary ?? answer?.query ?? "query", 180)));
346
+ }
347
+ }
348
+ lines.push(
349
+ themeFg(
350
+ theme,
351
+ "dim",
352
+ `concurrency=${routed.concurrency ?? "?"} • child outputs suppressed; full child results in details.result.steps`,
353
+ ),
354
+ );
355
+
356
+ if (!expanded) {
357
+ lines.push(themeFg(theme, "dim", "ctrl+o to expand child step statuses and recovery pointers"));
358
+ return textComponent(lines.join("\n"));
359
+ }
360
+
361
+ if (queryAnswers.length > 0) {
362
+ lines.push("", themeFg(theme, "toolTitle", "Query answers"));
363
+ queryAnswers.forEach((answer, index) => {
364
+ lines.push(
365
+ ` ${themeFg(theme, answer?.status === "answered" ? "accent" : "warning", `#${index + 1} ${answer?.status ?? "unknown"}`)} ${truncateText(answer?.query ?? "query", 120)}`,
366
+ );
367
+ if (answer?.summary) {
368
+ lines.push(` ${truncateText(answer.summary, 220)}`);
369
+ }
370
+ const matches = Array.isArray(answer?.matches) ? answer.matches : [];
371
+ matches.slice(0, 3).forEach((match) => {
372
+ lines.push(
373
+ ` ${themeFg(theme, "muted", `${match.stepId ?? "step"}:`)} ${truncateText(match.excerpt ?? "", 180)}`,
374
+ );
375
+ });
376
+ });
377
+ }
378
+
379
+ lines.push("", themeFg(theme, "toolTitle", "Steps"));
380
+ if (steps.length === 0) {
381
+ lines.push(` ${themeFg(theme, "dim", "No steps returned.")}`);
382
+ } else {
383
+ steps.forEach((step) => {
384
+ const child = step.result;
385
+ const outputId = child?.outputId ?? child?.recovery?.outputId;
386
+ const status = step.status ?? child?.toolStatus ?? "unknown";
387
+ const pieces = [`${step.kind}`, `status=${status}`, `${step.durationMs ?? 0}ms`];
388
+ if (child?.routing?.status) {
389
+ pieces.push(`routing=${child.routing.status}`);
390
+ }
391
+ if (child?.execution?.status) {
392
+ pieces.push(`execution=${child.execution.status}`);
393
+ }
394
+ if (outputId) {
395
+ pieces.push(`outputId=${outputId}`);
396
+ }
397
+ if (Array.isArray(child?.evidence)) {
398
+ pieces.push(`evidence=${child.evidence.length}`);
399
+ }
400
+ if (Array.isArray(child?.importantLines)) {
401
+ pieces.push(`spans=${child.importantLines.length}`);
402
+ }
403
+ lines.push(
404
+ ` ${themeFg(theme, status === "failed" ? "warning" : "accent", `#${step.index + 1} ${step.id}`)} ${themeFg(theme, "muted", pieces.join(" • "))}`,
405
+ );
406
+ const message = step.error ?? child?.failure?.message ?? child?.summary ?? child?.routing?.reason;
407
+ if (message) {
408
+ lines.push(` ${truncateText(message, 180)}`);
409
+ }
410
+ if (outputId) {
411
+ lines.push(` ${themeFg(theme, "muted", "recovery starting point:")} ${recoveryStartingPoint(outputId)}`);
412
+ }
413
+ });
414
+ }
415
+
416
+ lines.push("", themeFg(theme, "toolTitle", "Batch recovery"));
417
+ if (routed.recovery?.how) {
418
+ lines.push(` ${truncateText(routed.recovery.how, 220)}`);
419
+ }
420
+ lines.push(
421
+ ` ${themeFg(theme, "dim", "Use details.result.steps for complete child routed results; model-visible output intentionally omits child excerpts.")}`,
422
+ );
423
+ return textComponent(lines.join("\n"));
424
+ }
425
+
426
+ export function renderFreeflowStatusCall(args, theme) {
427
+ const title = themeFg(theme, "toolTitle", themeBold(theme, "freeflow_status"));
428
+ const action = themeFg(theme, "accent", args?.action ?? "status");
429
+ return textComponent(`${title} ${action}`);
430
+ }
431
+
432
+ export function renderFreeflowStatusResult(result, { expanded }: any = {}, theme) {
433
+ const report = routerResultFromToolResult(result);
434
+ if (!report) {
435
+ return textComponent(fallbackResultText(result));
436
+ }
437
+
438
+ const warnings = Array.isArray(report.configWarnings) ? report.configWarnings : [];
439
+ const localWarnings = Array.isArray(report.localConfigWarnings) ? report.localConfigWarnings : [];
440
+ const allWarnings = [...warnings, ...localWarnings];
441
+ const recommendations = Array.isArray(report.migration?.recommendations) ? report.migration.recommendations : [];
442
+ const core = report.effectiveConfig ?? {};
443
+ const sources = report.configuration?.sources ?? core.sources ?? {};
444
+ const router = core.outputRouter ?? {};
445
+ const observedRouting = report.observedRouting ?? core.observedRouting ?? {};
446
+ const unsafeProcessing = report.processing?.unsafeUnsandboxed?.enabled === true;
447
+ const modeActive = report.mode?.active === true;
448
+ const resolvedMode = report.mode?.resolvedMode ?? report.mode?.effectiveMode ?? "workflow";
449
+ const displayedMode = modeActive ? resolvedMode : `dormant (${resolvedMode})`;
450
+ const icon = statusIcon(report.toolStatus);
451
+ const lines = [
452
+ `${themeFg(theme, "success", icon)} ${themeFg(theme, "toolTitle", "freeflow_status")} ${themeFg(theme, "muted", report.action ?? "status")} • freeflow ${formatStatus(theme, core.enabled ? "ok" : "off")} • mode ${themeFg(theme, modeActive ? "accent" : "dim", displayedMode)} • router ${formatStatus(theme, router.enabled === false ? "off" : "ok")}`,
453
+ `${themeFg(theme, "muted", "vault:")} ${themeFg(theme, "accent", shortenMiddle(report.vault?.root ?? "unknown", 80))} ${themeFg(theme, "dim", report.vault?.writability?.status ?? "unknown")}`,
454
+ `${themeFg(theme, "muted", "routing:")} observed=${observedRouting.enabled ? "on" : "off"} • processing=${unsafeProcessing ? "unsafe/unsandboxed" : "safe-default"} • warnings=${allWarnings.length} • migrations=${recommendations.length}`,
455
+ ];
456
+
457
+ if (!expanded) {
458
+ lines.push(
459
+ themeFg(
460
+ theme,
461
+ "dim",
462
+ "ctrl+o to expand configuration sources, effective state, routing, warnings, and migrations",
463
+ ),
464
+ );
465
+ return textComponent(lines.join("\n"));
466
+ }
467
+
468
+ const repository = report.configuration?.repository ?? {};
469
+ const personal = report.configuration?.personal ?? {};
470
+ lines.push("", themeFg(theme, "toolTitle", "Configuration"));
471
+ lines.push(
472
+ ` ${themeFg(theme, "muted", "repository:")} ${repository.exists ? (repository.valid ? "valid" : themeFg(theme, "warning", "invalid")) : "missing"} ${themeFg(theme, "dim", shortenMiddle(repository.path ?? report.configPath ?? ".freeflow/config.json", 90))}`,
473
+ );
474
+ lines.push(
475
+ ` ${themeFg(theme, "muted", "personal:")} ${personal.exists ? (personal.valid ? "valid" : themeFg(theme, "warning", "invalid")) : "not set"} ${themeFg(theme, "dim", shortenMiddle(personal.path ?? report.localConfigPath ?? ".freeflow/local.json", 90))}`,
476
+ );
477
+ lines.push(
478
+ ` ${themeFg(theme, "muted", "enabled:")} ${String(Boolean(core.enabled))} ${themeFg(theme, "dim", `(${sources.enabled ?? "builtin"})`)}`,
479
+ );
480
+ lines.push(
481
+ ` ${themeFg(theme, "muted", "interactionContract:")} ${String(Boolean(core.interactionContract?.enabled))} ${themeFg(theme, "dim", `(${sources.interactionContract ?? "builtin"})`)}`,
482
+ );
483
+ lines.push(
484
+ ` ${themeFg(theme, "muted", "skills:")} ${String(Boolean(core.skills?.enabled))} ${themeFg(theme, "dim", `(${sources.skillsEnabled ?? "builtin"})`)}`,
485
+ );
486
+
487
+ lines.push("", themeFg(theme, "toolTitle", "Mode"));
488
+ lines.push(` ${themeFg(theme, "muted", "active:")} ${String(modeActive)}`);
489
+ lines.push(` ${themeFg(theme, "muted", "effective:")} ${report.mode?.effectiveMode ?? "none"}`);
490
+ lines.push(` ${themeFg(theme, "muted", "resolved:")} ${resolvedMode}`);
491
+ lines.push(
492
+ ` ${themeFg(theme, "muted", "repository default:")} ${report.mode?.repositoryDefaultMode ?? "workflow"} ${themeFg(theme, "dim", `(${report.mode?.repositoryDefaultModeSource ?? "builtin"})`)}`,
493
+ );
494
+ lines.push(
495
+ ` ${themeFg(theme, "muted", "personal default override:")} ${report.mode?.personalDefaultMode ?? "none"}`,
496
+ );
497
+ lines.push(
498
+ ` ${themeFg(theme, "muted", "configured default:")} ${report.mode?.defaultMode ?? "workflow"} ${themeFg(theme, "dim", `(${report.mode?.defaultModeSource ?? sources.defaultMode ?? "builtin"})`)}`,
499
+ );
500
+ lines.push(
501
+ ` ${themeFg(theme, "muted", "session override:")} ${report.mode?.sessionMode ?? report.mode?.currentMode ?? "none"}`,
502
+ );
503
+
504
+ lines.push("", themeFg(theme, "toolTitle", "Router"));
505
+ lines.push(` ${themeFg(theme, "muted", "enabled:")} ${String(router.enabled !== false)}`);
506
+ lines.push(` ${themeFg(theme, "muted", "profile:")} ${router.profile ?? "standard"}`);
507
+ lines.push(` ${themeFg(theme, "muted", "postToolRouting:")} ${router.postToolRouting ?? "off"}`);
508
+ lines.push(` ${themeFg(theme, "muted", "storagePolicy:")} ${router.storagePolicy ?? "hybrid-dedupe"}`);
509
+ if (router.thresholds) {
510
+ lines.push(` ${themeFg(theme, "muted", "thresholds:")} ${JSON.stringify(router.thresholds)}`);
511
+ }
512
+
513
+ const vaultIndex = report.vaultIndex;
514
+ if (vaultIndex) {
515
+ lines.push("", themeFg(theme, "toolTitle", "Vault index"));
516
+ lines.push(` ${themeFg(theme, "muted", "engine:")} ${vaultIndex.engine ?? "local-json-sidecar"}`);
517
+ lines.push(
518
+ ` ${themeFg(theme, "muted", "available:")} ${String(Boolean(vaultIndex.available))} degraded=${String(Boolean(vaultIndex.degraded))} stale=${String(Boolean(vaultIndex.stale))} rebuildRecommended=${String(Boolean(vaultIndex.rebuildRecommended))}`,
519
+ );
520
+ lines.push(
521
+ ` ${themeFg(theme, "muted", "entries:")} ${vaultIndex.entryCount ?? 0} text=${vaultIndex.textEntryCount ?? 0} metadata=${vaultIndex.metadataOnlyEntryCount ?? 0}`,
522
+ );
523
+ if (vaultIndex.lastError) {
524
+ lines.push(` ${themeFg(theme, "warning", `lastError: ${truncateText(vaultIndex.lastError, 160)}`)}`);
525
+ }
526
+ }
527
+
528
+ const scriptTransform = report.scriptTransform;
529
+ if (scriptTransform) {
530
+ lines.push("", themeFg(theme, "toolTitle", "Script transform"));
531
+ lines.push(` ${themeFg(theme, "muted", "enabled:")} ${String(Boolean(scriptTransform.enabled))}`);
532
+ lines.push(` ${themeFg(theme, "muted", "adapter:")} ${scriptTransform.adapterStatus ?? "unavailable"}`);
533
+ lines.push(
534
+ ` ${themeFg(theme, "muted", "languages:")} ${(scriptTransform.configuredLanguages ?? []).join(", ") || "none"}`,
535
+ );
536
+ lines.push(` ${themeFg(theme, "muted", "network:")} ${scriptTransform.network ?? "off"}`);
537
+ lines.push(
538
+ ` ${themeFg(theme, "muted", "rawScriptPersistence:")} ${scriptTransform.rawScriptPersistence ?? "disabled"}`,
539
+ );
540
+ if (scriptTransform.adapterHome) {
541
+ lines.push(` ${themeFg(theme, "muted", "adapterHome:")} ${shortenMiddle(scriptTransform.adapterHome, 90)}`);
542
+ }
543
+ if (scriptTransform.setupCommand && scriptTransform.adapterStatus !== "available") {
544
+ lines.push(` ${themeFg(theme, "muted", "setup:")} ${truncateText(scriptTransform.setupCommand, 140)}`);
545
+ }
546
+ }
547
+
548
+ const processing = report.processing?.unsafeUnsandboxed;
549
+ if (processing) {
550
+ lines.push("", themeFg(theme, "toolTitle", "Processing"));
551
+ lines.push(
552
+ ` ${themeFg(theme, "muted", "unsafeUnsandboxed:")} ${processing.enabled ? themeFg(theme, "warning", "enabled unsafe/unsandboxed") : "disabled"}`,
553
+ );
554
+ lines.push(` ${themeFg(theme, "muted", "source:")} ${processing.source ?? ".freeflow/local.json"}`);
555
+ const notes = Array.isArray(processing.notes) ? processing.notes : [];
556
+ notes
557
+ .slice(0, 2)
558
+ .forEach((note) =>
559
+ lines.push(` ${themeFg(theme, processing.enabled ? "warning" : "dim", truncateText(note, 180))}`),
560
+ );
561
+ }
562
+
563
+ lines.push("", themeFg(theme, "toolTitle", "Observed routing"));
564
+ lines.push(` ${themeFg(theme, "muted", "enabled:")} ${String(Boolean(observedRouting.enabled))}`);
565
+ lines.push(` ${themeFg(theme, "muted", "onRoutingFailure:")} ${observedRouting.onRoutingFailure ?? "fail-open"}`);
566
+ lines.push(
567
+ ` ${themeFg(theme, "muted", "host:")} ${observedRouting.host?.name ?? "pi"} outputReplacement=${observedRouting.host?.outputReplacement ?? "available"}`,
568
+ );
569
+ lines.push(` ${themeFg(theme, "muted", "mcp servers:")} ${observedRouting.mcp?.configuredServerCount ?? 0}`);
570
+ if (observedRouting.web || observedRouting.fetch || observedRouting.codeSearch) {
571
+ lines.push(
572
+ ` ${themeFg(theme, "muted", "web/fetch/codeSearch:")} web=${observedRouting.web?.enabled ? "on" : "off"} fetch=${observedRouting.fetch?.enabled ? "on" : "off"} codeSearch=${observedRouting.codeSearch?.enabled ? "on" : "off"}`,
573
+ );
574
+ }
575
+
576
+ if (allWarnings.length > 0) {
577
+ lines.push("", themeFg(theme, "toolTitle", "Warnings"));
578
+ allWarnings
579
+ .slice(0, 8)
580
+ .forEach((warning) => lines.push(` ${themeFg(theme, "warning", truncateText(warning, 180))}`));
581
+ }
582
+
583
+ if (recommendations.length > 0) {
584
+ lines.push("", themeFg(theme, "toolTitle", "Migration recommendations"));
585
+ recommendations
586
+ .slice(0, 8)
587
+ .forEach((item) =>
588
+ lines.push(
589
+ ` ${themeFg(theme, "accent", item.path)} ${themeFg(theme, "muted", item.action)} — ${truncateText(item.message, 160)}`,
590
+ ),
591
+ );
592
+ lines.push(
593
+ ` ${themeFg(theme, "dim", "No config was rewritten; confirmation is required before applying recommendations.")}`,
594
+ );
595
+ }
596
+
597
+ return textComponent(lines.join("\n"));
598
+ }
599
+
600
+ export function renderFreeflowSearchResult(result, options: any = {}, theme) {
601
+ const routed = routerResultFromToolResult(result);
602
+ if (routed?.implementation === "processing-engine-skeleton-v1") {
603
+ return renderFreeflowProcessingResult(result, options, theme);
604
+ }
605
+ if (routed?.routing?.route === "transform") {
606
+ return renderFreeflowTransformResult(result, options, theme, "freeflow_search");
607
+ }
608
+ return renderFreeflowRetrieveResult(result, options, theme, "freeflow_search");
609
+ }
610
+
611
+ function renderFreeflowProcessingResult(result, { expanded }: any = {}, theme) {
612
+ const routed = routerResultFromToolResult(result);
613
+ if (!routed) {
614
+ return textComponent(fallbackResultText(result));
615
+ }
616
+ const icon = routed.status === "ok" ? "✓" : routed.status === "blocked" ? "✗" : "!";
617
+ const unsafe = routed.script && "policy" in routed.script && routed.script.policy === "unsafe-unsandboxed";
618
+ const lines = [
619
+ `${themeFg(theme, unsafe ? "warning" : routed.status === "ok" ? "success" : "warning", icon)} ${themeFg(theme, "toolTitle", "freeflow_search")} ${themeFg(theme, "muted", "transform")} • ${formatStatus(theme, routed.status ?? "unknown")}${unsafe ? themeFg(theme, "warning", " • unsafe/unsandboxed") : ""}`,
620
+ ];
621
+ const visibleLines = splitLines(String(routed.visibleText ?? ""));
622
+ visibleLines
623
+ .slice(0, expanded ? 20 : 4)
624
+ .forEach((line) =>
625
+ lines.push(
626
+ themeFg(
627
+ theme,
628
+ unsafe && line.includes("unsafe/unsandboxed") ? "warning" : "toolOutput",
629
+ truncateText(line, 160),
630
+ ),
631
+ ),
632
+ );
633
+ if (visibleLines.length > (expanded ? 20 : 4)) {
634
+ lines.push(themeFg(theme, "dim", `… ${visibleLines.length - (expanded ? 20 : 4)} more line(s)`));
635
+ }
636
+ if (!expanded) {
637
+ lines.push(themeFg(theme, "dim", "ctrl+o to expand transform details and recovery"));
638
+ return textComponent(lines.join("\n"));
639
+ }
640
+ lines.push("", themeFg(theme, "toolTitle", "Source"));
641
+ lines.push(` ${themeFg(theme, "accent", routed.source?.displayPath ?? "source")}`);
642
+ if (routed.stats) {
643
+ lines.push(` ${themeFg(theme, "muted", "stats:")} ${routed.stats.bytes} bytes • ${routed.stats.lines} lines`);
644
+ }
645
+ if (routed.reducer?.status) {
646
+ lines.push("", themeFg(theme, "toolTitle", "Reducer"));
647
+ lines.push(` ${routed.reducer.status}${routed.reducer.selected?.name ? ` ${routed.reducer.selected.name}` : ""}`);
648
+ }
649
+ if (routed.script?.status) {
650
+ lines.push("", themeFg(theme, "toolTitle", "Script"));
651
+ lines.push(` ${themeFg(theme, "muted", "status:")} ${routed.script.status}`);
652
+ if (routed.script.policy) lines.push(` ${themeFg(theme, "muted", "policy:")} ${routed.script.policy}`);
653
+ if (routed.script.unsafeUnsandboxed) lines.push(` ${themeFg(theme, "warning", "unsafe/unsandboxed local-yolo")}`);
654
+ }
655
+ if (routed.recovery?.how) {
656
+ lines.push("", themeFg(theme, "toolTitle", "Recovery"));
657
+ lines.push(` ${truncateText(routed.recovery.how, 180)}`);
658
+ if (routed.recovery.outputId)
659
+ lines.push(` ${themeFg(theme, "muted", "outputId:")} ${themeFg(theme, "accent", routed.recovery.outputId)}`);
660
+ }
661
+ return textComponent(lines.join("\n"));
662
+ }
663
+
664
+ export function renderFreeflowRetrieveResult(result, { expanded }: any = {}, theme, toolTitle = "freeflow_search") {
665
+ const routed = routerResultFromToolResult(result);
666
+ if (!routed) {
667
+ return textComponent(fallbackResultText(result));
668
+ }
669
+
670
+ const evidence = Array.isArray(routed.evidence) ? routed.evidence : [];
671
+ const firstEvidence = evidence[0];
672
+ const icon = statusIcon(routed.toolStatus);
673
+ const lines = [
674
+ `${themeFg(theme, "success", icon)} ${themeFg(theme, "toolTitle", toolTitle)} ${themeFg(theme, "muted", `${evidence.length} evidence packet(s)`)} • ${routeSummaryLine(theme, routed)}`,
675
+ ];
676
+
677
+ if (firstEvidence) {
678
+ lines.push(
679
+ `${themeFg(theme, "accent", evidenceLabel(firstEvidence))}${firstEvidence.expandable ? themeFg(theme, "dim", " • expandable") : ""}`,
680
+ );
681
+ } else {
682
+ lines.push(
683
+ themeFg(theme, "warning", truncateText(routed.routing?.reason ?? "No matching evidence returned.", 140)),
684
+ );
685
+ }
686
+
687
+ if (!expanded) {
688
+ lines.push(themeFg(theme, "dim", "ctrl+o to expand evidence and recovery details"));
689
+ return textComponent(lines.join("\n"));
690
+ }
691
+
692
+ lines.push("", themeFg(theme, "toolTitle", "Source"));
693
+ lines.push(` ${themeFg(theme, "accent", retrieveSourceLabel(routed.source))}`);
694
+ if (routed.preserve) {
695
+ lines.push(` ${themeFg(theme, "muted", "preserve:")} ${routed.preserve}`);
696
+ }
697
+
698
+ appendStorageSection(lines, theme, routed, routed.outputId || routed.recovery?.outputId);
699
+
700
+ lines.push("", themeFg(theme, "toolTitle", "Routing"));
701
+ lines.push(` ${themeFg(theme, "muted", "toolStatus:")} ${formatStatus(theme, routed.toolStatus)}`);
702
+ lines.push(` ${themeFg(theme, "muted", "routing.status:")} ${formatStatus(theme, routed.routing?.status)}`);
703
+ lines.push(` ${themeFg(theme, "muted", "route:")} ${routed.routing?.route ?? "retrieve"}`);
704
+ if (routed.routing?.reason) {
705
+ lines.push(` ${themeFg(theme, "muted", "reason:")} ${truncateText(routed.routing.reason, 160)}`);
706
+ }
707
+
708
+ lines.push("", themeFg(theme, "toolTitle", "Evidence"));
709
+ if (evidence.length === 0) {
710
+ lines.push(` ${themeFg(theme, "dim", "No evidence packets returned.")}`);
711
+ } else {
712
+ evidence.forEach((packet, index) => {
713
+ lines.push(
714
+ ` ${themeFg(theme, "accent", `#${index + 1} ${evidenceLabel(packet)}`)} ${themeFg(theme, "dim", `window=${packet.window}`)}`,
715
+ );
716
+ if (packet.id) {
717
+ lines.push(` ${themeFg(theme, "muted", "evidenceId:")} ${themeFg(theme, "accent", packet.id)}`);
718
+ }
719
+ if (packet.source) {
720
+ lines.push(` ${themeFg(theme, "muted", "source:")} ${evidenceSourceSummary(packet.source)}`);
721
+ }
722
+ if (packet.expandable !== undefined) {
723
+ lines.push(` ${themeFg(theme, "muted", "expandable:")} ${String(Boolean(packet.expandable))}`);
724
+ }
725
+ const exactHint = exactRetrieveHintFromEvidence(packet);
726
+ if (exactHint) {
727
+ lines.push(` ${themeFg(theme, "muted", "exact search:")} ${exactHint}`);
728
+ }
729
+ if (packet.why) {
730
+ lines.push(` ${themeFg(theme, "muted", "why:")} ${truncateText(packet.why, 160)}`);
731
+ }
732
+ lines.push(...excerptLines(theme, packet.excerpt, " ", 8));
733
+ });
734
+ }
735
+
736
+ if (routed.recovery?.how) {
737
+ lines.push("", themeFg(theme, "toolTitle", "Recovery"));
738
+ lines.push(` ${truncateText(routed.recovery.how, 180)}`);
739
+ if (routed.recovery.outputId) {
740
+ lines.push(` ${themeFg(theme, "muted", "outputId:")} ${themeFg(theme, "accent", routed.recovery.outputId)}`);
741
+ }
742
+ const exactHint = firstExactRetrieveHintFromEvidence(evidence);
743
+ if (exactHint) {
744
+ lines.push(` ${themeFg(theme, "muted", "exact search:")} ${exactHint}`);
745
+ } else if (routed.recovery.outputId) {
746
+ lines.push(
747
+ ` ${themeFg(theme, "muted", "recovery starting point:")} ${recoveryStartingPoint(routed.recovery.outputId)}`,
748
+ );
749
+ }
750
+ if (routed.recovery.evidenceId) {
751
+ lines.push(` ${themeFg(theme, "muted", "evidenceId:")} ${themeFg(theme, "accent", routed.recovery.evidenceId)}`);
752
+ lines.push(
753
+ ` ${themeFg(theme, "muted", "expand hint:")} freeflow_search action=expand evidenceId=${routed.recovery.evidenceId}`,
754
+ );
755
+ }
756
+ }
757
+
758
+ return textComponent(lines.join("\n"));
759
+ }
760
+
761
+ export function renderFreeflowRunResult(result, { expanded }: any = {}, theme, context: any = {}) {
762
+ const routed = routerResultFromToolResult(result);
763
+ if (!routed) {
764
+ return textComponent(fallbackResultText(result));
765
+ }
766
+
767
+ const executionStatus = routed.execution?.status ?? routed.toolStatus;
768
+ const outputId = routed.outputId;
769
+ const exactRecoveryOutputId = routed.persistence?.recoverability === "exact" ? outputId : routed.recovery?.outputId;
770
+ const icon = statusIcon(executionStatus);
771
+ const importantLines = Array.isArray(routed.importantLines) ? routed.importantLines : [];
772
+ const lines = [
773
+ `${themeFg(theme, executionStatus === "failed" ? "error" : "success", icon)} ${themeFg(theme, "toolTitle", "freeflow_run")} ${themeFg(theme, "muted", "execution:")} ${formatStatus(theme, executionStatus)} • ${routeSummaryLine(theme, routed)}`,
774
+ ];
775
+
776
+ const statusParts = [];
777
+ if (routed.execution?.exitCode !== undefined && routed.execution?.exitCode !== null) {
778
+ statusParts.push(`exit ${routed.execution.exitCode}`);
779
+ }
780
+ if (routed.execution?.durationMs !== undefined) {
781
+ statusParts.push(`${routed.execution.durationMs}ms`);
782
+ }
783
+ if (outputId) {
784
+ statusParts.push(
785
+ `${routed.persistence?.recoverability === "metadata_only" ? "metadataId" : "outputId"} ${outputId}`,
786
+ );
787
+ }
788
+ if (exactRecoveryOutputId && exactRecoveryOutputId !== outputId) {
789
+ statusParts.push(`exact ${exactRecoveryOutputId}`);
790
+ }
791
+ if (routed.scriptFilter?.outputId) {
792
+ statusParts.push(`transformed ${routed.scriptFilter.outputId}`);
793
+ }
794
+ if (routed.reducer?.outputId) {
795
+ statusParts.push(`reduced ${routed.reducer.outputId}`);
796
+ }
797
+ if (routed.parser?.name) {
798
+ statusParts.push(`parser ${routed.parser.name} ${Number(routed.parser.confidence ?? 0).toFixed(2)}`);
799
+ }
800
+ if (statusParts.length > 0) {
801
+ lines.push(themeFg(theme, "accent", statusParts.join(" • ")));
802
+ }
803
+ if (routed.summary) {
804
+ lines.push(themeFg(theme, "muted", truncateText(routed.summary, 140)));
805
+ }
806
+ lines.push(
807
+ themeFg(
808
+ theme,
809
+ "dim",
810
+ `${importantLines.length} important span(s) • ${exactRecoveryOutputId ? (routed.scriptFilter?.outputId ? "raw and script output recoverable from vault" : routed.reducer?.outputId ? "raw and reducer output recoverable from vault" : "raw output recoverable from vault") : "metadata-only record; exact raw output not vaulted"}`,
811
+ ),
812
+ );
813
+
814
+ if (!expanded) {
815
+ lines.push(themeFg(theme, "dim", "ctrl+o to expand status, evidence, and vault recovery"));
816
+ return textComponent(lines.join("\n"));
817
+ }
818
+
819
+ lines.push("", themeFg(theme, "toolTitle", context.args?.script ? "Producer script" : "Command"));
820
+ lines.push(` ${themeFg(theme, "accent", runProducerLabel(context.args))}`);
821
+
822
+ lines.push("", themeFg(theme, "toolTitle", "Status"));
823
+ lines.push(` ${themeFg(theme, "muted", "toolStatus:")} ${formatStatus(theme, routed.toolStatus)}`);
824
+ lines.push(` ${themeFg(theme, "muted", "execution.status:")} ${formatStatus(theme, routed.execution?.status)}`);
825
+ lines.push(` ${themeFg(theme, "muted", "routing.status:")} ${formatStatus(theme, routed.routing?.status)}`);
826
+ if (routed.execution?.exitCode !== undefined) {
827
+ lines.push(` ${themeFg(theme, "muted", "exitCode:")} ${routed.execution.exitCode}`);
828
+ }
829
+ if (routed.execution?.durationMs !== undefined) {
830
+ lines.push(` ${themeFg(theme, "muted", "durationMs:")} ${routed.execution.durationMs}`);
831
+ }
832
+
833
+ appendStorageSection(lines, theme, routed, outputId);
834
+
835
+ if (routed.routing?.reason || routed.summary) {
836
+ lines.push("", themeFg(theme, "toolTitle", "Routing"));
837
+ if (routed.summary) {
838
+ lines.push(` ${themeFg(theme, "muted", "summary:")} ${truncateText(routed.summary, 160)}`);
839
+ }
840
+ if (routed.routing?.reason) {
841
+ lines.push(` ${themeFg(theme, "muted", "reason:")} ${truncateText(routed.routing.reason, 180)}`);
842
+ }
843
+ }
844
+
845
+ if (routed.filters) {
846
+ lines.push("", themeFg(theme, "toolTitle", "Filters"));
847
+ lines.push(` ${truncateText(runFilterLabel(routed.filters), 220)}`);
848
+ }
849
+
850
+ if (routed.scriptProducer) {
851
+ lines.push("", themeFg(theme, "toolTitle", "Script producer"));
852
+ lines.push(` ${truncateText(runScriptProducerLabel(routed.scriptProducer), 220)}`);
853
+ if (routed.scriptProducer.operation) {
854
+ lines.push(
855
+ ` ${themeFg(theme, "muted", "operation:")} ${truncateText(JSON.stringify(routed.scriptProducer.operation), 180)}`,
856
+ );
857
+ }
858
+ if (routed.scriptProducer.failure?.message) {
859
+ lines.push(` ${themeFg(theme, "warning", truncateText(routed.scriptProducer.failure.message, 180))}`);
860
+ }
861
+ }
862
+
863
+ if (routed.reducer) {
864
+ lines.push("", themeFg(theme, "toolTitle", "Reducer"));
865
+ lines.push(
866
+ ` ${routed.reducer.name}@${routed.reducer.version} confidence=${Number(routed.reducer.confidence ?? 0).toFixed(2)} status=${routed.reducer.status}`,
867
+ );
868
+ if (routed.reducer.rawOutputId) {
869
+ lines.push(
870
+ ` ${themeFg(theme, "muted", "rawOutputId:")} ${themeFg(theme, "accent", routed.reducer.rawOutputId)}`,
871
+ );
872
+ }
873
+ if (routed.reducer.outputId) {
874
+ lines.push(` ${themeFg(theme, "muted", "outputId:")} ${themeFg(theme, "accent", routed.reducer.outputId)}`);
875
+ }
876
+ if (Array.isArray(routed.reducer.facts) && routed.reducer.facts.length > 0) {
877
+ lines.push(` ${themeFg(theme, "muted", "facts:")} ${truncateText(JSON.stringify(routed.reducer.facts), 220)}`);
878
+ }
879
+ }
880
+
881
+ if (routed.scriptFilter) {
882
+ lines.push("", themeFg(theme, "toolTitle", "Script filter"));
883
+ lines.push(` ${truncateText(runScriptFilterLabel(routed.scriptFilter), 220)}`);
884
+ if (routed.scriptFilter.rawOutputId) {
885
+ lines.push(
886
+ ` ${themeFg(theme, "muted", "rawOutputId:")} ${themeFg(theme, "accent", routed.scriptFilter.rawOutputId)}`,
887
+ );
888
+ }
889
+ if (Array.isArray(routed.scriptFilter.sourceAliases)) {
890
+ lines.push(` ${themeFg(theme, "muted", "sources:")} ${routed.scriptFilter.sourceAliases.join(", ")}`);
891
+ }
892
+ if (routed.scriptFilter.operation) {
893
+ lines.push(
894
+ ` ${themeFg(theme, "muted", "operation:")} ${truncateText(JSON.stringify(routed.scriptFilter.operation), 180)}`,
895
+ );
896
+ }
897
+ if (routed.scriptFilter.failure?.message) {
898
+ lines.push(` ${themeFg(theme, "warning", truncateText(routed.scriptFilter.failure.message, 180))}`);
899
+ }
900
+ }
901
+
902
+ if (routed.parser?.name) {
903
+ lines.push("", themeFg(theme, "toolTitle", "Parser"));
904
+ lines.push(` ${themeFg(theme, "muted", "name:")} ${routed.parser.name}`);
905
+ lines.push(` ${themeFg(theme, "muted", "confidence:")} ${Number(routed.parser.confidence ?? 0).toFixed(2)}`);
906
+ lines.push(` ${themeFg(theme, "muted", "fidelity:")} ${routed.parser.fidelity ?? "exact"}`);
907
+ lines.push(` ${themeFg(theme, "muted", "compressed:")} ${String(Boolean(routed.parser.compressed))}`);
908
+ if (routed.parser.counts && Object.keys(routed.parser.counts).length > 0) {
909
+ lines.push(` ${themeFg(theme, "muted", "counts:")} ${truncateText(JSON.stringify(routed.parser.counts), 160)}`);
910
+ }
911
+ if (Array.isArray(routed.parser.references) && routed.parser.references.length > 0) {
912
+ const refs = routed.parser.references
913
+ .slice(0, 3)
914
+ .map(
915
+ (ref) =>
916
+ `${ref.path}${ref.line ? `:${ref.line}${ref.column ? `:${ref.column}` : ""}` : ""}${ref.code ? ` ${ref.code}` : ""}`,
917
+ );
918
+ lines.push(` ${themeFg(theme, "muted", "references:")} ${truncateText(refs.join(", "), 180)}`);
919
+ }
920
+ }
921
+
922
+ lines.push("", themeFg(theme, "toolTitle", "Evidence"));
923
+ if (importantLines.length === 0) {
924
+ lines.push(` ${themeFg(theme, "dim", "No important lines selected.")}`);
925
+ } else {
926
+ importantLines.forEach((line, index) => {
927
+ lines.push(` ${themeFg(theme, "accent", `#${index + 1} ${line.stream} lines ${line.lines}`)}`);
928
+ lines.push(...excerptLines(theme, line.excerpt, " ", 8));
929
+ });
930
+ }
931
+
932
+ if (routed.recovery?.how || outputId) {
933
+ lines.push("", themeFg(theme, "toolTitle", "Vault recovery"));
934
+ if (outputId) {
935
+ lines.push(` ${themeFg(theme, "muted", "outputId:")} ${themeFg(theme, "accent", outputId)}`);
936
+ }
937
+ if (routed.recovery?.how) {
938
+ lines.push(` ${truncateText(routed.recovery.how, 180)}`);
939
+ }
940
+ const evidenceOutputId = routed.scriptFilter?.outputId ?? routed.reducer?.outputId ?? exactRecoveryOutputId;
941
+ const exactHint = exactRetrieveHintFromImportantLines(evidenceOutputId, importantLines);
942
+ if (exactHint) {
943
+ const hint =
944
+ routed.scriptFilter?.outputId || routed.reducer?.outputId
945
+ ? exactHint.replace(/stream=(stdout|stderr|combined)/, "stream=raw")
946
+ : exactHint;
947
+ lines.push(` ${themeFg(theme, "muted", "exact search:")} ${hint}`);
948
+ if ((routed.scriptFilter?.outputId || routed.reducer?.outputId) && outputId) {
949
+ lines.push(` ${themeFg(theme, "muted", "raw command starting point:")} ${recoveryStartingPoint(outputId)}`);
950
+ }
951
+ } else if (exactRecoveryOutputId) {
952
+ lines.push(
953
+ ` ${themeFg(theme, "muted", "recovery starting point:")} ${recoveryStartingPoint(exactRecoveryOutputId)}`,
954
+ );
955
+ }
956
+ lines.push(` ${themeFg(theme, "dim", "Full structured result remains available in details.result.")}`);
957
+ }
958
+
959
+ return textComponent(lines.join("\n"));
960
+ }
961
+
962
+ export function renderFreeflowTransformResult(
963
+ result,
964
+ { expanded }: any = {},
965
+ theme,
966
+ toolTitle = "freeflow_search action=transform",
967
+ ) {
968
+ const routed = routerResultFromToolResult(result);
969
+ if (!routed) {
970
+ return textComponent(fallbackResultText(result));
971
+ }
972
+
973
+ const outputId = routed.outputId || routed.recovery?.outputId;
974
+ const operation = transformOperationLabel(routed.operation ?? { kind: routed.producer?.name });
975
+ const failed = routed.failure || routed.toolStatus === "error" || routed.routing?.status === "failed";
976
+ const icon = failed ? "✗" : statusIcon(routed.toolStatus);
977
+ const evidence = Array.isArray(routed.evidence) ? routed.evidence : [];
978
+ const sourceLabel = retrieveSourceLabel(routed.source);
979
+ const lines = [
980
+ `${themeFg(theme, failed ? "error" : "success", icon)} ${themeFg(theme, "toolTitle", toolTitle)} ${themeFg(theme, "accent", operation)} • ${routeSummaryLine(theme, routed)}`,
981
+ ];
982
+
983
+ const statusParts = [];
984
+ if (outputId) {
985
+ statusParts.push(`outputId ${outputId}`);
986
+ }
987
+ if (routed.recordId) {
988
+ statusParts.push(`recordId ${routed.recordId}`);
989
+ }
990
+ if (routed.persistence?.status) {
991
+ statusParts.push(`persistence ${routed.persistence.status}/${routed.persistence.recoverability}`);
992
+ }
993
+ if (routed.source?.outputId) {
994
+ statusParts.push(`source ${routed.source.outputId}${routed.source.stream ? `:${routed.source.stream}` : ""}`);
995
+ }
996
+ if (statusParts.length > 0) {
997
+ lines.push(themeFg(theme, "accent", statusParts.join(" • ")));
998
+ }
999
+
1000
+ if (routed.failure?.message) {
1001
+ lines.push(themeFg(theme, "warning", `${routed.failure.kind}: ${truncateText(routed.failure.message, 140)}`));
1002
+ } else if (routed.summary) {
1003
+ lines.push(themeFg(theme, "muted", truncateText(routed.summary, 140)));
1004
+ }
1005
+ lines.push(
1006
+ themeFg(
1007
+ theme,
1008
+ "dim",
1009
+ `${evidence.length} evidence packet(s)${outputId ? " • transformed output recoverable from vault" : ""}`,
1010
+ ),
1011
+ );
1012
+
1013
+ if (!expanded) {
1014
+ lines.push(themeFg(theme, "dim", "ctrl+o to expand source, operation, lineage, evidence, and recovery details"));
1015
+ return textComponent(lines.join("\n"));
1016
+ }
1017
+
1018
+ lines.push("", themeFg(theme, "toolTitle", "Status"));
1019
+ lines.push(` ${themeFg(theme, "muted", "toolStatus:")} ${formatStatus(theme, routed.toolStatus)}`);
1020
+ if (routed.transformExecution?.status) {
1021
+ lines.push(
1022
+ ` ${themeFg(theme, "muted", "transformExecution.status:")} ${formatStatus(theme, routed.transformExecution.status)}`,
1023
+ );
1024
+ }
1025
+ lines.push(` ${themeFg(theme, "muted", "routing.status:")} ${formatStatus(theme, routed.routing?.status)}`);
1026
+ if (routed.persistence?.status) {
1027
+ lines.push(
1028
+ ` ${themeFg(theme, "muted", "persistence:")} ${routed.persistence.status} / ${routed.persistence.recoverability}`,
1029
+ );
1030
+ }
1031
+ if (routed.routing?.reason) {
1032
+ lines.push(` ${themeFg(theme, "muted", "reason:")} ${truncateText(routed.routing.reason, 180)}`);
1033
+ }
1034
+
1035
+ appendStorageSection(lines, theme, routed, outputId);
1036
+
1037
+ lines.push("", themeFg(theme, "toolTitle", "Source"));
1038
+ lines.push(` ${themeFg(theme, "accent", sourceLabel)}`);
1039
+
1040
+ lines.push("", themeFg(theme, "toolTitle", "Operation"));
1041
+ if (routed.operation) {
1042
+ lines.push(` ${truncateText(JSON.stringify(routed.operation), 220)}`);
1043
+ } else {
1044
+ lines.push(` ${themeFg(theme, "dim", operation)}`);
1045
+ }
1046
+
1047
+ if (routed.lineage) {
1048
+ lines.push("", themeFg(theme, "toolTitle", "Lineage"));
1049
+ if (Array.isArray(routed.lineage.sourceOutputIds)) {
1050
+ lines.push(` ${themeFg(theme, "muted", "sourceOutputIds:")} ${routed.lineage.sourceOutputIds.join(", ")}`);
1051
+ }
1052
+ if (Array.isArray(routed.lineage.sourceRecordIds)) {
1053
+ lines.push(` ${themeFg(theme, "muted", "sourceRecordIds:")} ${routed.lineage.sourceRecordIds.join(", ")}`);
1054
+ }
1055
+ if (routed.lineage.operation) {
1056
+ lines.push(` ${themeFg(theme, "muted", "operation:")} ${routed.lineage.operation}`);
1057
+ }
1058
+ if (routed.lineage.operationHash) {
1059
+ lines.push(` ${themeFg(theme, "muted", "operationHash:")} ${shortenMiddle(routed.lineage.operationHash, 80)}`);
1060
+ }
1061
+ }
1062
+
1063
+ lines.push("", themeFg(theme, "toolTitle", "Evidence"));
1064
+ if (evidence.length === 0) {
1065
+ lines.push(` ${themeFg(theme, "dim", "No evidence packets returned.")}`);
1066
+ } else {
1067
+ evidence.forEach((packet, index) => {
1068
+ lines.push(
1069
+ ` ${themeFg(theme, "accent", `#${index + 1} ${evidenceLabel(packet)}`)} ${themeFg(theme, "dim", `window=${packet.window}`)}`,
1070
+ );
1071
+ if (packet.id) {
1072
+ lines.push(` ${themeFg(theme, "muted", "evidenceId:")} ${themeFg(theme, "accent", packet.id)}`);
1073
+ }
1074
+ if (packet.source) {
1075
+ lines.push(` ${themeFg(theme, "muted", "source:")} ${evidenceSourceSummary(packet.source)}`);
1076
+ }
1077
+ if (packet.expandable !== undefined) {
1078
+ lines.push(` ${themeFg(theme, "muted", "expandable:")} ${String(Boolean(packet.expandable))}`);
1079
+ }
1080
+ const exactHint = exactRetrieveHintFromEvidence(packet);
1081
+ if (exactHint) {
1082
+ lines.push(` ${themeFg(theme, "muted", "exact search:")} ${exactHint}`);
1083
+ }
1084
+ if (packet.why) {
1085
+ lines.push(` ${themeFg(theme, "muted", "why:")} ${truncateText(packet.why, 160)}`);
1086
+ }
1087
+ lines.push(...excerptLines(theme, packet.excerpt, " ", 8));
1088
+ });
1089
+ }
1090
+
1091
+ if (routed.recovery?.how || outputId) {
1092
+ lines.push("", themeFg(theme, "toolTitle", "Recovery"));
1093
+ if (outputId) {
1094
+ lines.push(` ${themeFg(theme, "muted", "outputId:")} ${themeFg(theme, "accent", outputId)}`);
1095
+ }
1096
+ if (routed.recovery?.how) {
1097
+ lines.push(` ${truncateText(routed.recovery.how, 180)}`);
1098
+ }
1099
+ const exactHint = firstExactRetrieveHintFromEvidence(evidence);
1100
+ if (exactHint) {
1101
+ lines.push(` ${themeFg(theme, "muted", "exact search:")} ${exactHint}`);
1102
+ } else if (outputId) {
1103
+ lines.push(` ${themeFg(theme, "muted", "recovery starting point:")} ${recoveryStartingPoint(outputId)}`);
1104
+ }
1105
+ }
1106
+
1107
+ return textComponent(lines.join("\n"));
1108
+ }