@hassangameryt/freeflow 0.4.0 → 0.6.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 (285) hide show
  1. package/.claude-plugin/marketplace.json +1 -1
  2. package/.claude-plugin/plugin.json +2 -1
  3. package/.codex-plugin/plugin.json +2 -1
  4. package/README.md +150 -155
  5. package/capabilities/cognitive-routing/SKILL.md +229 -0
  6. package/capabilities/context-virtualization/SKILL.md +103 -0
  7. package/capabilities/conversation-history/SKILL.md +53 -0
  8. package/command-surface.json +17 -13
  9. package/hooks/freeflow-runtime-context.mjs +393 -159
  10. package/hooks/hooks.json +26 -1
  11. package/hooks/tests/check-runtime-context-hook.sh +223 -54
  12. package/package.json +25 -16
  13. package/pi-extension/dist/cognitive-routing/commands.js +96 -0
  14. package/pi-extension/dist/cognitive-routing/config.js +236 -0
  15. package/pi-extension/dist/cognitive-routing/controller.js +862 -0
  16. package/pi-extension/dist/cognitive-routing/history.js +439 -0
  17. package/pi-extension/dist/cognitive-routing/host.js +126 -0
  18. package/pi-extension/dist/cognitive-routing/runtime.js +43 -0
  19. package/pi-extension/dist/cognitive-routing/tool.js +211 -0
  20. package/pi-extension/dist/cognitive-routing/types.js +6 -0
  21. package/pi-extension/dist/context-virtualization/commands.js +89 -0
  22. package/pi-extension/dist/context-virtualization/projector.js +42 -0
  23. package/pi-extension/dist/context-virtualization/runtime.js +356 -0
  24. package/pi-extension/dist/context-virtualization/state.js +57 -0
  25. package/pi-extension/dist/context-virtualization/types.js +10 -0
  26. package/pi-extension/dist/conversation-history/passages.js +66 -0
  27. package/pi-extension/dist/conversation-history/ranking.js +105 -0
  28. package/pi-extension/dist/conversation-history/runtime.js +226 -0
  29. package/pi-extension/dist/conversation-history/search.js +56 -0
  30. package/pi-extension/dist/conversation-history/types.js +1 -0
  31. package/pi-extension/dist/freeflow-context/resolver.js +120 -0
  32. package/pi-extension/dist/freeflow-context/runtime.js +80 -0
  33. package/pi-extension/dist/freeflow-context/source-projector.js +95 -0
  34. package/pi-extension/dist/freeflow-context/tool.js +308 -0
  35. package/pi-extension/dist/freeflow-context/types.js +12 -0
  36. package/pi-extension/dist/freeflow-context/visibility.js +28 -0
  37. package/pi-extension/dist/index.js +516 -98
  38. package/pi-extension/dist/runtime/runtime-context.js +318 -271
  39. package/pi-extension/dist/runtime/runtime-identity.js +5 -0
  40. package/pi-extension/dist/settings/settings-tui.js +78 -0
  41. package/pi-extension/dist/settings/settings-ui.js +593 -563
  42. package/pi-extension/dist/ui/text-component.js +57 -0
  43. package/pi-extension/src/cognitive-routing/commands.ts +119 -0
  44. package/pi-extension/src/cognitive-routing/config.ts +307 -0
  45. package/pi-extension/src/cognitive-routing/controller.ts +1118 -0
  46. package/pi-extension/src/cognitive-routing/history.ts +638 -0
  47. package/pi-extension/src/cognitive-routing/host.ts +172 -0
  48. package/pi-extension/src/cognitive-routing/runtime.ts +57 -0
  49. package/pi-extension/src/cognitive-routing/tool.ts +272 -0
  50. package/pi-extension/src/cognitive-routing/types.ts +89 -0
  51. package/pi-extension/src/context-virtualization/commands.ts +111 -0
  52. package/pi-extension/src/context-virtualization/projector.ts +62 -0
  53. package/pi-extension/src/context-virtualization/runtime.ts +406 -0
  54. package/pi-extension/src/context-virtualization/state.ts +79 -0
  55. package/pi-extension/src/context-virtualization/types.ts +92 -0
  56. package/pi-extension/src/conversation-history/passages.ts +81 -0
  57. package/pi-extension/src/conversation-history/ranking.ts +155 -0
  58. package/pi-extension/src/conversation-history/runtime.ts +250 -0
  59. package/pi-extension/src/conversation-history/search.ts +79 -0
  60. package/pi-extension/src/conversation-history/types.ts +31 -0
  61. package/pi-extension/src/freeflow-context/resolver.ts +139 -0
  62. package/pi-extension/src/freeflow-context/runtime.ts +100 -0
  63. package/pi-extension/src/freeflow-context/source-projector.ts +106 -0
  64. package/pi-extension/src/freeflow-context/tool.ts +360 -0
  65. package/pi-extension/src/freeflow-context/types.ts +70 -0
  66. package/pi-extension/src/freeflow-context/visibility.ts +53 -0
  67. package/pi-extension/src/index.ts +553 -101
  68. package/pi-extension/src/runtime/runtime-context.ts +339 -281
  69. package/pi-extension/src/runtime/runtime-identity.ts +8 -0
  70. package/pi-extension/src/settings/settings-tui.ts +93 -0
  71. package/pi-extension/src/settings/settings-ui.ts +700 -613
  72. package/pi-extension/src/ui/text-component.ts +66 -0
  73. package/pi-extension/tests/cognitive-routing/commands.test.js +134 -0
  74. package/pi-extension/tests/cognitive-routing/controller.test.js +1099 -0
  75. package/pi-extension/tests/cognitive-routing/extension-lifecycle.test.js +1071 -0
  76. package/pi-extension/tests/cognitive-routing/history.test.js +274 -0
  77. package/pi-extension/tests/cognitive-routing/host-fixture.js +4 -0
  78. package/pi-extension/tests/cognitive-routing/host-identity.test.js +63 -0
  79. package/pi-extension/tests/cognitive-routing/profile-resolution.test.js +236 -0
  80. package/pi-extension/tests/cognitive-routing/tool.test.js +230 -0
  81. package/pi-extension/tests/context-virtualization/extension.test.js +749 -0
  82. package/pi-extension/tests/context-virtualization/projector.test.js +110 -0
  83. package/pi-extension/tests/context-virtualization/runtime.test.js +216 -0
  84. package/pi-extension/tests/conversation-history/runtime.test.js +98 -0
  85. package/pi-extension/tests/conversation-history/search.test.js +97 -0
  86. package/pi-extension/tests/integration/pi-extension.test.js +928 -2047
  87. package/pi-extension/tests/integration/prompt-architecture.test.js +305 -0
  88. package/runtime/prompts/cognitive-routing.md +9 -0
  89. package/runtime/prompts/context-virtualization.md +3 -0
  90. package/runtime/prompts/conversation-history.md +3 -0
  91. package/runtime/prompts/core.md +13 -0
  92. package/runtime/prompts/interaction-contract.md +7 -0
  93. package/runtime/prompts/skills.md +65 -0
  94. package/skills/action-selection/SKILL.md +91 -0
  95. package/skills/bypass/SKILL.md +3 -2
  96. package/skills/commit-work/SKILL.md +1 -1
  97. package/skills/commit-work/references/staging-decisions.md +1 -1
  98. package/skills/decision-gate/SKILL.md +1 -1
  99. package/skills/design-for-depth/SKILL.md +14 -33
  100. package/skills/design-for-depth/references/design-pressure-signals.md +1 -1
  101. package/skills/design-for-depth/references/interface-design-loop.md +1 -1
  102. package/skills/design-for-depth/references/software-design-philosophy.md +1 -1
  103. package/skills/diagnose-failure/SKILL.md +20 -14
  104. package/skills/diagnose-failure/references/diagnostic-loop-catalog.md +1 -3
  105. package/skills/diagnose-failure/references/{flaky-and-performance.md → flaky-and-performance-diagnosis.md} +1 -1
  106. package/skills/discuss/SKILL.md +34 -22
  107. package/skills/discuss/references/discussion-continuity.md +71 -0
  108. package/skills/evaluate-skill/SKILL.md +5 -5
  109. package/skills/evaluate-skill/references/{definition-schema.md → evaluation-definition-schema.md} +42 -2
  110. package/skills/evaluate-skill/references/execution-and-evidence.md +8 -3
  111. package/skills/evaluate-skill/scripts/lib/definitions.mjs +114 -3
  112. package/skills/evaluate-skill/scripts/lib/grade-interaction.mjs +156 -1
  113. package/skills/evaluate-skill/scripts/lib/grade.mjs +9 -1
  114. package/skills/evaluate-skill/scripts/lib/pi.mjs +147 -32
  115. package/skills/evaluate-skill/scripts/lib/rpc.mjs +4 -4
  116. package/skills/evaluate-skill/scripts/lib/runner.mjs +36 -14
  117. package/skills/evaluate-skill/scripts/lib/sandbox.mjs +92 -0
  118. package/skills/evaluate-skill/scripts/pi-guard.mjs +1 -1
  119. package/skills/evaluate-skill/scripts/pi-observer.mjs +66 -0
  120. package/skills/evaluate-skill/scripts/skill-eval.mjs +6 -5
  121. package/skills/execute-work/SKILL.md +18 -15
  122. package/skills/execute-work/references/code-practices.md +1 -1
  123. package/skills/execute-work/references/execute-work-edges.md +42 -0
  124. package/skills/finish-branch/SKILL.md +2 -2
  125. package/skills/finish-branch/references/integration-options.md +1 -1
  126. package/skills/handoff/SKILL.md +3 -3
  127. package/skills/launch-work/SKILL.md +1 -1
  128. package/skills/launch-work/references/launch-readiness.md +2 -2
  129. package/skills/migration-work/SKILL.md +2 -2
  130. package/skills/migration-work/references/migration-lifecycle.md +1 -1
  131. package/skills/mode-contract/SKILL.md +32 -10
  132. package/skills/release-work/SKILL.md +1 -1
  133. package/skills/release-work/references/release-evidence.md +1 -1
  134. package/skills/review-artifact/SKILL.md +28 -26
  135. package/skills/review-work/SKILL.md +28 -26
  136. package/skills/review-work/references/{reviewer-prompt.md → independent-work-reviewer-contract.md} +1 -1
  137. package/skills/review-work/references/security-risk-lens.md +1 -1
  138. package/skills/setup-freeflow/SKILL.md +6 -10
  139. package/skills/setup-freeflow/references/activation-contract.md +5 -10
  140. package/skills/setup-freeflow/references/host-setup.md +3 -7
  141. package/skills/simplify-code/SKILL.md +1 -1
  142. package/skills/simplify-code/references/simplification-patterns.md +1 -1
  143. package/skills/tdd/SKILL.md +18 -38
  144. package/skills/tdd/references/test-design.md +2 -2
  145. package/skills/track-work/SKILL.md +362 -58
  146. package/skills/track-work/scripts/lib/codec.mjs +570 -0
  147. package/skills/track-work/scripts/lib/commands.mjs +568 -0
  148. package/skills/track-work/scripts/lib/contract.mjs +303 -0
  149. package/skills/track-work/scripts/lib/model.mjs +506 -0
  150. package/skills/track-work/scripts/lib/operations.mjs +965 -0
  151. package/skills/track-work/scripts/lib/record-store.mjs +264 -0
  152. package/skills/track-work/scripts/lib/result.mjs +37 -0
  153. package/skills/track-work/scripts/lib/rewrites.mjs +740 -0
  154. package/skills/track-work/scripts/lib/schema.mjs +277 -0
  155. package/skills/track-work/scripts/lib/source-inventory.mjs +162 -0
  156. package/skills/track-work/scripts/lib/views.mjs +443 -0
  157. package/skills/track-work/scripts/lib/workspace.mjs +134 -0
  158. package/skills/track-work/scripts/working-record.mjs +122 -0
  159. package/skills/verify-work/SKILL.md +14 -2
  160. package/skills/verify-work/references/browser-runtime-evidence.md +1 -1
  161. package/skills/verify-work/references/integration-evidence.md +1 -1
  162. package/skills/verify-work/references/performance-evidence.md +1 -1
  163. package/skills/workflow/SKILL.md +105 -53
  164. package/skills/workflow/references/domain-skill-composition.md +22 -42
  165. package/skills/write-plan/SKILL.md +1 -1
  166. package/skills/write-skill/SKILL.md +3 -2
  167. package/skills/write-skill/references/progressive-disclosure.md +17 -3
  168. package/pi-extension/dist/output-router/host-producer-identification.js +0 -232
  169. package/pi-extension/dist/output-router/native-safety-net.js +0 -216
  170. package/pi-extension/dist/output-router/observed-tool-routing.js +0 -122
  171. package/pi-extension/dist/output-router/renderers.js +0 -1023
  172. package/pi-extension/dist/output-router/router-tools.js +0 -554
  173. package/pi-extension/dist/output-router/schemas.js +0 -383
  174. package/pi-extension/dist/output-router/status.js +0 -744
  175. package/pi-extension/dist/output-router/utils.js +0 -502
  176. package/pi-extension/src/output-router/host-producer-identification.ts +0 -256
  177. package/pi-extension/src/output-router/native-safety-net.ts +0 -248
  178. package/pi-extension/src/output-router/observed-tool-routing.ts +0 -134
  179. package/pi-extension/src/output-router/renderers.ts +0 -1108
  180. package/pi-extension/src/output-router/router-tools.ts +0 -579
  181. package/pi-extension/src/output-router/schemas.ts +0 -387
  182. package/pi-extension/src/output-router/status.ts +0 -791
  183. package/pi-extension/src/output-router/utils.ts +0 -554
  184. package/pi-extension/tests/output-router/observed-producers.test.js +0 -132
  185. package/pi-extension/tests/output-router/search.test.js +0 -722
  186. package/router/dist/benchmarks/benchmark-harness.d.ts +0 -35
  187. package/router/dist/benchmarks/benchmark-harness.js +0 -92
  188. package/router/dist/benchmarks/benchmarks.d.ts +0 -109
  189. package/router/dist/benchmarks/benchmarks.js +0 -979
  190. package/router/dist/benchmarks/codex-qa-benchmarks.d.ts +0 -95
  191. package/router/dist/benchmarks/codex-qa-benchmarks.js +0 -500
  192. package/router/dist/benchmarks/command-benchmarks.d.ts +0 -143
  193. package/router/dist/benchmarks/command-benchmarks.js +0 -698
  194. package/router/dist/benchmarks/context-mode-normalized-benchmarks.d.ts +0 -82
  195. package/router/dist/benchmarks/context-mode-normalized-benchmarks.js +0 -773
  196. package/router/dist/benchmarks/context-mode-real-deep-benchmark.d.ts +0 -132
  197. package/router/dist/benchmarks/context-mode-real-deep-benchmark.js +0 -1380
  198. package/router/dist/benchmarks/context-saving-baseline.d.ts +0 -60
  199. package/router/dist/benchmarks/context-saving-baseline.js +0 -470
  200. package/router/dist/benchmarks/index-benchmarks.d.ts +0 -103
  201. package/router/dist/benchmarks/index-benchmarks.js +0 -862
  202. package/router/dist/benchmarks/storage-policy-benchmarks.d.ts +0 -101
  203. package/router/dist/benchmarks/storage-policy-benchmarks.js +0 -618
  204. package/router/dist/benchmarks/vault-index-benchmarks.d.ts +0 -56
  205. package/router/dist/benchmarks/vault-index-benchmarks.js +0 -322
  206. package/router/dist/config/config.d.ts +0 -103
  207. package/router/dist/config/config.js +0 -632
  208. package/router/dist/config/router-contract.d.ts +0 -10
  209. package/router/dist/config/router-contract.js +0 -54
  210. package/router/dist/config/schema.d.ts +0 -27
  211. package/router/dist/config/schema.js +0 -648
  212. package/router/dist/config/types.d.ts +0 -573
  213. package/router/dist/config/types.js +0 -41
  214. package/router/dist/evidence/bounded-evidence.d.ts +0 -40
  215. package/router/dist/evidence/bounded-evidence.js +0 -323
  216. package/router/dist/evidence/evidence-range-selector.d.ts +0 -24
  217. package/router/dist/evidence/evidence-range-selector.js +0 -232
  218. package/router/dist/evidence/evidence-search.d.ts +0 -27
  219. package/router/dist/evidence/evidence-search.js +0 -490
  220. package/router/dist/evidence/evidence.d.ts +0 -38
  221. package/router/dist/evidence/evidence.js +0 -200
  222. package/router/dist/evidence/failure-contracts.d.ts +0 -37
  223. package/router/dist/evidence/failure-contracts.js +0 -166
  224. package/router/dist/evidence/line-ranges.d.ts +0 -24
  225. package/router/dist/evidence/line-ranges.js +0 -22
  226. package/router/dist/experiments/local-index.d.ts +0 -64
  227. package/router/dist/experiments/local-index.js +0 -581
  228. package/router/dist/index.d.ts +0 -30
  229. package/router/dist/index.js +0 -32
  230. package/router/dist/local/local-traversal.d.ts +0 -18
  231. package/router/dist/local/local-traversal.js +0 -334
  232. package/router/dist/processing/engine.d.ts +0 -132
  233. package/router/dist/processing/engine.js +0 -533
  234. package/router/dist/processing/reducers.d.ts +0 -280
  235. package/router/dist/processing/reducers.js +0 -1673
  236. package/router/dist/processing/renderers.d.ts +0 -31
  237. package/router/dist/processing/renderers.js +0 -195
  238. package/router/dist/processing/scripts.d.ts +0 -91
  239. package/router/dist/processing/scripts.js +0 -386
  240. package/router/dist/repo/repo-traversal.d.ts +0 -18
  241. package/router/dist/repo/repo-traversal.js +0 -245
  242. package/router/dist/routing/observed-reducers.d.ts +0 -41
  243. package/router/dist/routing/observed-reducers.js +0 -661
  244. package/router/dist/routing/observed-routing.d.ts +0 -26
  245. package/router/dist/routing/observed-routing.js +0 -378
  246. package/router/dist/routing/parsers.d.ts +0 -16
  247. package/router/dist/routing/parsers.js +0 -521
  248. package/router/dist/routing/run-filters.d.ts +0 -60
  249. package/router/dist/routing/run-filters.js +0 -238
  250. package/router/dist/routing/run-reducers.d.ts +0 -49
  251. package/router/dist/routing/run-reducers.js +0 -122
  252. package/router/dist/sandbox/adapter-roots.d.ts +0 -46
  253. package/router/dist/sandbox/adapter-roots.js +0 -66
  254. package/router/dist/sandbox/eryx-python-adapter.d.ts +0 -13
  255. package/router/dist/sandbox/eryx-python-adapter.js +0 -868
  256. package/router/dist/sandbox/jq-wasm-adapter.d.ts +0 -9
  257. package/router/dist/sandbox/jq-wasm-adapter.js +0 -429
  258. package/router/dist/sandbox/quickjs-wasi-adapter.d.ts +0 -13
  259. package/router/dist/sandbox/quickjs-wasi-adapter.js +0 -384
  260. package/router/dist/sandbox/script-sandbox.d.ts +0 -134
  261. package/router/dist/sandbox/script-sandbox.js +0 -337
  262. package/router/dist/setup/script-transform-adapters.d.ts +0 -32
  263. package/router/dist/setup/script-transform-adapters.js +0 -278
  264. package/router/dist/tools/batch.d.ts +0 -33
  265. package/router/dist/tools/batch.js +0 -586
  266. package/router/dist/tools/run.d.ts +0 -62
  267. package/router/dist/tools/run.js +0 -1304
  268. package/router/dist/tools/search.d.ts +0 -66
  269. package/router/dist/tools/search.js +0 -1364
  270. package/router/dist/transform/engine.d.ts +0 -168
  271. package/router/dist/transform/engine.js +0 -2072
  272. package/router/dist/vault/vault-index.d.ts +0 -125
  273. package/router/dist/vault/vault-index.js +0 -416
  274. package/router/dist/vault/vault.d.ts +0 -138
  275. package/router/dist/vault/vault.js +0 -630
  276. package/runtime/interaction-contract.md +0 -16
  277. package/skills/discuss/references/checkpoints.md +0 -66
  278. package/skills/execute-work/references/execution-loop.md +0 -77
  279. package/skills/output-router/SKILL.md +0 -206
  280. package/skills/output-router/references/safety-policy.md +0 -40
  281. package/skills/setup-freeflow/references/output-router-setup.md +0 -110
  282. package/skills/track-work/references/working-record-schema.md +0 -378
  283. package/skills/workflow/references/workflow-loop.md +0 -92
  284. /package/skills/handoff/references/{templates.md → handoff-templates.md} +0 -0
  285. /package/skills/review-artifact/references/{reviewer-prompt.md → independent-artifact-reviewer-contract.md} +0 -0
@@ -0,0 +1,1071 @@
1
+ import assert from "node:assert/strict";
2
+ import { cp, mkdir, mkdtemp, readFile, rm, symlink, writeFile } from "node:fs/promises";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { pathToFileURL } from "node:url";
6
+ import test from "node:test";
7
+ import freeflowExtension from "../../dist/index.js";
8
+ import { PIFLOW_HOST } from "./host-fixture.js";
9
+
10
+ function createExtensionHost(
11
+ { rejectReturnRestore = false, rejectAcquire = false } = {},
12
+ extension = freeflowExtension,
13
+ ) {
14
+ const handlers = new Map();
15
+ const tools = [];
16
+ const commands = [];
17
+ const shortcuts = [];
18
+ const entries = [];
19
+ const operations = [];
20
+ let activeToolNames;
21
+ let shouldRejectAcquire = rejectAcquire;
22
+ const state = {
23
+ model: { provider: "faux", id: "return" },
24
+ thinkingLevel: "medium",
25
+ };
26
+ const pi = {
27
+ host: PIFLOW_HOST,
28
+ registerTool(tool) {
29
+ tools.push(tool);
30
+ },
31
+ registerCommand(name, definition) {
32
+ commands.push({ name, definition });
33
+ },
34
+ registerShortcut(shortcut, definition) {
35
+ shortcuts.push({ shortcut, definition });
36
+ },
37
+ on(event, handler) {
38
+ handlers.set(event, handler);
39
+ },
40
+ appendEntry(customType, data) {
41
+ entries.push({ type: "custom", customType, data });
42
+ },
43
+ appendEntryDurable(customType, data) {
44
+ operations.push("prepare");
45
+ entries.push({ type: "custom", customType, data });
46
+ },
47
+ async acquireModelStateControl() {
48
+ operations.push("acquire");
49
+ if (shouldRejectAcquire) return { status: "busy" };
50
+ return {
51
+ status: "acquired",
52
+ lease: {
53
+ async setState(request) {
54
+ operations.push("setState");
55
+ if (rejectReturnRestore && request.modelId === "return") return { status: "rejected" };
56
+ state.model = { provider: request.provider, id: request.modelId };
57
+ state.thinkingLevel = request.thinkingLevel;
58
+ entries.push({
59
+ type: "model_state_change",
60
+ provider: request.provider,
61
+ modelId: request.modelId,
62
+ thinkingLevel: request.thinkingLevel,
63
+ correlationId: request.correlationId,
64
+ });
65
+ return { status: "applied" };
66
+ },
67
+ async release() {
68
+ operations.push("release");
69
+ return { status: "released" };
70
+ },
71
+ },
72
+ };
73
+ },
74
+ getAllTools() {
75
+ return tools.map((tool) => ({ name: tool.name }));
76
+ },
77
+ getActiveTools() {
78
+ return tools.map((tool) => tool.name);
79
+ },
80
+ setActiveTools(names) {
81
+ activeToolNames = [...names];
82
+ },
83
+ sendUserMessage() {},
84
+ };
85
+ extension(pi);
86
+ return {
87
+ commands,
88
+ entries,
89
+ handlers,
90
+ operations,
91
+ pi,
92
+ shortcuts,
93
+ state,
94
+ tools,
95
+ activeToolNames: () => activeToolNames ?? [],
96
+ setRejectAcquire(value) {
97
+ shouldRejectAcquire = value;
98
+ },
99
+ };
100
+ }
101
+
102
+ function createContext(cwd, host) {
103
+ return {
104
+ cwd,
105
+ mode: "print",
106
+ hasUI: false,
107
+ model: host.state.model,
108
+ thinkingLevel: host.state.thinkingLevel,
109
+ modelRegistry: {
110
+ find(provider, modelId) {
111
+ if (provider !== "faux" || !["standard", "reasoning", "return"].includes(modelId)) return undefined;
112
+ return { provider, id: modelId };
113
+ },
114
+ async getApiKeyAndHeaders() {
115
+ return { ok: true };
116
+ },
117
+ clampThinkingLevel(_model, level) {
118
+ return level;
119
+ },
120
+ },
121
+ modelStateProvenance: {},
122
+ sessionManager: {
123
+ getEntries() {
124
+ return host.entries;
125
+ },
126
+ getBranch() {
127
+ return host.entries;
128
+ },
129
+ buildContextEntries() {
130
+ return host.entries;
131
+ },
132
+ },
133
+ ui: {
134
+ notify() {},
135
+ setStatus() {},
136
+ },
137
+ };
138
+ }
139
+
140
+ test("registered shortcuts cycle manual holds and automatic profiles", async () => {
141
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-shortcuts-"));
142
+ await mkdir(join(cwd, ".freeflow"));
143
+ await writeFile(
144
+ join(cwd, ".freeflow", "config.json"),
145
+ JSON.stringify({
146
+ cognitiveRouting: {
147
+ enabled: true,
148
+ profiles: {
149
+ standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
150
+ reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
151
+ },
152
+ },
153
+ }),
154
+ );
155
+ const host = createExtensionHost();
156
+ const ctx = createContext(cwd, host);
157
+ ctx.isIdle = () => true;
158
+ try {
159
+ await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
160
+ const cycle = host.shortcuts.find(({ shortcut }) => shortcut === "ctrl+shift+r");
161
+ const automatic = host.shortcuts.find(({ shortcut }) => shortcut === "ctrl+shift+a");
162
+ assert.ok(cycle);
163
+ assert.ok(automatic);
164
+
165
+ await cycle.definition.handler(ctx);
166
+ assert.equal(host.entries.at(-1).modelId, "reasoning");
167
+ await automatic.definition.handler(ctx);
168
+ assert.equal(host.entries.at(-1).customType, "freeflow-cognitive-routing-control");
169
+ assert.equal(host.entries.filter((entry) => entry.modelId).at(-1).modelId, "reasoning");
170
+ await automatic.definition.handler(ctx);
171
+ assert.equal(host.entries.at(-1).modelId, "standard");
172
+ await automatic.definition.handler(ctx);
173
+ assert.equal(host.entries.at(-1).modelId, "reasoning");
174
+ const automaticIntent = host.entries
175
+ .filter((entry) => entry.customType === "freeflow-cognitive-routing-intent")
176
+ .at(-1);
177
+ assert.equal(automaticIntent.data.source, "user");
178
+ assert.equal(automaticIntent.data.mechanism, "profile-shortcut");
179
+ assert.equal(automaticIntent.data.reason, "Cycle automatic profile to reasoning.");
180
+ assert.deepEqual(host.operations, [
181
+ "prepare",
182
+ "acquire",
183
+ "setState",
184
+ "prepare",
185
+ "setState",
186
+ "prepare",
187
+ "prepare",
188
+ "setState",
189
+ "prepare",
190
+ "setState",
191
+ ]);
192
+ } finally {
193
+ await rm(cwd, { recursive: true, force: true });
194
+ }
195
+ });
196
+
197
+ test("explicit startup model provenance suppresses lifecycle activation", async () => {
198
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-suppressed-"));
199
+ await mkdir(join(cwd, ".freeflow"));
200
+ await writeFile(
201
+ join(cwd, ".freeflow", "config.json"),
202
+ JSON.stringify({
203
+ cognitiveRouting: {
204
+ enabled: true,
205
+ profiles: {
206
+ standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
207
+ reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
208
+ },
209
+ },
210
+ }),
211
+ );
212
+ const host = createExtensionHost();
213
+ const ctx = createContext(cwd, host);
214
+ ctx.modelStateProvenance = { explicitModel: true };
215
+ try {
216
+ await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
217
+ assert.deepEqual(host.operations, []);
218
+ assert.deepEqual(host.entries, []);
219
+ assert.deepEqual(host.state, { model: { provider: "faux", id: "return" }, thinkingLevel: "medium" });
220
+
221
+ const before = await host.handlers.get("before_agent_start")({ systemPrompt: "base prompt" }, ctx);
222
+ assert.equal(before.message, undefined);
223
+ assert.doesNotMatch(before.systemPrompt, /## Cognitive Routing Cue/);
224
+ const resources = await host.handlers.get("resources_discover")({ cwd }, ctx);
225
+ assert.ok(!resources.skillPaths.some((path) => path.endsWith("/capabilities/cognitive-routing/SKILL.md")));
226
+ assert.ok(!host.activeToolNames().includes("freeflow_switch_profile"));
227
+ const providerContext = await host.handlers.get("context")({ messages: [] }, ctx);
228
+ assert.match(providerContext.messages.at(-1).content, /Cognitive Routing: unavailable/);
229
+ assert.match(providerContext.messages.at(-1).content, /Control: `unavailable`/);
230
+ assert.match(providerContext.messages.at(-1).content, /Profile: `unavailable`/);
231
+ } finally {
232
+ await rm(cwd, { recursive: true, force: true });
233
+ }
234
+ });
235
+
236
+ test("Cognitive Routing activation failure hides model-facing routing surfaces", async () => {
237
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-activation-failure-"));
238
+ await mkdir(join(cwd, ".freeflow"));
239
+ await writeFile(
240
+ join(cwd, ".freeflow", "config.json"),
241
+ JSON.stringify({
242
+ cognitiveRouting: {
243
+ enabled: true,
244
+ profiles: {
245
+ standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
246
+ reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
247
+ },
248
+ },
249
+ }),
250
+ );
251
+ const host = createExtensionHost({ rejectAcquire: true });
252
+ const ctx = createContext(cwd, host);
253
+ try {
254
+ await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
255
+ const before = await host.handlers.get("before_agent_start")({ systemPrompt: "base prompt" }, ctx);
256
+ assert.equal(before.message, undefined);
257
+ assert.doesNotMatch(before.systemPrompt, /## Cognitive Routing Cue/);
258
+ const resources = await host.handlers.get("resources_discover")({ cwd }, ctx);
259
+ assert.ok(!resources.skillPaths.some((path) => path.endsWith("/capabilities/cognitive-routing/SKILL.md")));
260
+ assert.ok(!host.activeToolNames().includes("freeflow_switch_profile"));
261
+ const providerContext = await host.handlers.get("context")({ messages: [] }, ctx);
262
+ assert.match(providerContext.messages.at(-1).content, /Cognitive Routing: unavailable/);
263
+ assert.match(providerContext.messages.at(-1).content, /Control: `unavailable`/);
264
+ assert.match(providerContext.messages.at(-1).content, /Profile: `unavailable`/);
265
+ } finally {
266
+ await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
267
+ await rm(cwd, { recursive: true, force: true });
268
+ }
269
+ });
270
+
271
+ test("Cognitive Routing retries activation after a transient failure", async () => {
272
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-activation-retry-"));
273
+ await mkdir(join(cwd, ".freeflow"));
274
+ await writeFile(
275
+ join(cwd, ".freeflow", "config.json"),
276
+ JSON.stringify({
277
+ cognitiveRouting: {
278
+ enabled: true,
279
+ profiles: {
280
+ standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
281
+ reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
282
+ },
283
+ },
284
+ }),
285
+ );
286
+ const host = createExtensionHost({ rejectAcquire: true });
287
+ const ctx = createContext(cwd, host);
288
+ try {
289
+ await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
290
+ const first = await host.handlers.get("before_agent_start")({ systemPrompt: "base prompt" }, ctx);
291
+ assert.doesNotMatch(first.systemPrompt, /## Cognitive Routing Cue/);
292
+ host.setRejectAcquire(false);
293
+ const second = await host.handlers.get("before_agent_start")({ systemPrompt: "base prompt" }, ctx);
294
+ assert.match(second.systemPrompt, /## Cognitive Routing Cue/);
295
+ const providerContext = await host.handlers.get("context")({ messages: [] }, ctx);
296
+ assert.match(providerContext.messages.at(-1).content, /Cognitive Routing: active/);
297
+ assert.match(providerContext.messages.at(-1).content, /Control: `automatic`/);
298
+ assert.match(providerContext.messages.at(-1).content, /Profile: `standard`/);
299
+ assert.ok(host.operations.includes("setState"));
300
+ } finally {
301
+ await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
302
+ await rm(cwd, { recursive: true, force: true });
303
+ }
304
+ });
305
+
306
+ test("missing Cognitive Routing prompt prevents lifecycle activation", async () => {
307
+ const root = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-prompt-missing-"));
308
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-prompt-missing-repo-"));
309
+ await mkdir(join(cwd, ".freeflow"));
310
+ await writeFile(
311
+ join(cwd, ".freeflow", "config.json"),
312
+ JSON.stringify({
313
+ cognitiveRouting: {
314
+ enabled: true,
315
+ profiles: {
316
+ standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
317
+ reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
318
+ },
319
+ },
320
+ }),
321
+ );
322
+ try {
323
+ await cp(join(process.cwd(), "pi-extension", "dist"), join(root, "pi-extension", "dist"), { recursive: true });
324
+ await cp(join(process.cwd(), "runtime", "prompts"), join(root, "runtime", "prompts"), { recursive: true });
325
+ await symlink(join(process.cwd(), "node_modules"), join(root, "node_modules"), "dir");
326
+ await rm(join(root, "runtime", "prompts", "cognitive-routing.md"));
327
+ const extension = (
328
+ await import(
329
+ `${pathToFileURL(join(root, "pi-extension", "dist", "index.js")).href}?missing-routing=${Date.now()}`
330
+ )
331
+ ).default;
332
+ const host = createExtensionHost({}, extension);
333
+ const ctx = createContext(cwd, host);
334
+ await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
335
+ const before = await host.handlers.get("before_agent_start")({ systemPrompt: "base prompt" }, ctx);
336
+
337
+ assert.deepEqual(host.operations, []);
338
+ assert.doesNotMatch(before.systemPrompt, /## Cognitive Routing Cue/);
339
+ assert.ok(!host.activeToolNames().includes("freeflow_switch_profile"));
340
+ const providerContext = await host.handlers.get("context")({ messages: [] }, ctx);
341
+ assert.match(providerContext.messages.at(-1).content, /Cognitive Routing: unavailable/);
342
+ } finally {
343
+ await rm(root, { recursive: true, force: true });
344
+ await rm(cwd, { recursive: true, force: true });
345
+ }
346
+ });
347
+
348
+ test("new empty sessions do not persist Cognitive Routing state until the first prompt", async () => {
349
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-lazy-start-"));
350
+ await mkdir(join(cwd, ".freeflow"));
351
+ await writeFile(
352
+ join(cwd, ".freeflow", "config.json"),
353
+ JSON.stringify({
354
+ cognitiveRouting: {
355
+ enabled: true,
356
+ profiles: {
357
+ standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
358
+ reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
359
+ },
360
+ },
361
+ }),
362
+ );
363
+ const host = createExtensionHost();
364
+ const ctx = createContext(cwd, host);
365
+ try {
366
+ await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
367
+
368
+ assert.deepEqual(host.operations, []);
369
+ assert.deepEqual(host.entries, []);
370
+
371
+ const beforeAgentStart = await host.handlers.get("before_agent_start")(
372
+ { prompt: "hello", systemPrompt: "base prompt" },
373
+ ctx,
374
+ );
375
+ assert.deepEqual(host.operations.slice(0, 3), ["prepare", "acquire", "setState"]);
376
+ assert.equal(host.entries[0].customType, "freeflow-cognitive-routing-intent");
377
+ assert.match(beforeAgentStart.systemPrompt, /## Cognitive Routing Cue/);
378
+ assert.doesNotMatch(beforeAgentStart.systemPrompt, /^# Automatic Routing Kernel$/m);
379
+ assert.equal(beforeAgentStart.message, undefined);
380
+ } finally {
381
+ await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
382
+ await rm(cwd, { recursive: true, force: true });
383
+ }
384
+ });
385
+
386
+ test("new sessions honor each configured Cognitive Routing startup combination", async () => {
387
+ const combinations = [
388
+ ["automatic", "standard"],
389
+ ["automatic", "reasoning"],
390
+ ["manual", "standard"],
391
+ ["manual", "reasoning"],
392
+ ];
393
+
394
+ for (const [control, profile] of combinations) {
395
+ const cwd = await mkdtemp(join(tmpdir(), `freeflow-cognitive-routing-start-${control}-${profile}-`));
396
+ await mkdir(join(cwd, ".freeflow"));
397
+ await writeFile(
398
+ join(cwd, ".freeflow", "config.json"),
399
+ JSON.stringify({
400
+ cognitiveRouting: {
401
+ enabled: true,
402
+ sessionStart: { control, profile },
403
+ profiles: {
404
+ standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
405
+ reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
406
+ },
407
+ },
408
+ }),
409
+ );
410
+
411
+ const host = createExtensionHost();
412
+ const ctx = createContext(cwd, host);
413
+ try {
414
+ await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
415
+ await host.handlers.get("before_agent_start")({ prompt: "hello", systemPrompt: "base prompt" }, ctx);
416
+
417
+ assert.equal(host.entries.filter((entry) => entry.modelId).at(-1).modelId, profile);
418
+ const activation = host.entries.find(
419
+ (entry) => entry.customType === "freeflow-cognitive-routing-intent" && entry.data.kind === "activation",
420
+ );
421
+ assert.equal(activation.data.profile, profile);
422
+ assert.equal(activation.data.control, control);
423
+ } finally {
424
+ await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
425
+ await rm(cwd, { recursive: true, force: true });
426
+ }
427
+ }
428
+ });
429
+
430
+ test("editing session-start settings does not activate the current empty session", async () => {
431
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-session-start-current-session-"));
432
+ await mkdir(join(cwd, ".freeflow"));
433
+ await writeFile(
434
+ join(cwd, ".freeflow", "config.json"),
435
+ JSON.stringify({
436
+ cognitiveRouting: {
437
+ enabled: true,
438
+ profiles: {
439
+ standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
440
+ reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
441
+ },
442
+ },
443
+ }),
444
+ );
445
+
446
+ const host = createExtensionHost();
447
+ const ctx = createContext(cwd, host);
448
+ ctx.mode = "tui";
449
+ ctx.hasUI = true;
450
+ ctx.isIdle = () => true;
451
+ ctx.ui.custom = async (factory) => {
452
+ let result;
453
+ const theme = {
454
+ fg(_color, text) {
455
+ return text;
456
+ },
457
+ bold(text) {
458
+ return text;
459
+ },
460
+ };
461
+ const component = factory({ requestRender() {} }, theme, {}, (value) => {
462
+ result = value;
463
+ });
464
+ for (let index = 0; index < 5; index += 1) component.handleInput("\u001b[B");
465
+ component.handleInput("\r");
466
+ for (let index = 0; index < 3; index += 1) component.handleInput("\u001b[B");
467
+ component.handleInput("\r");
468
+ component.handleInput("\u001b[B");
469
+ component.handleInput("\u001b[B");
470
+ component.handleInput("\r");
471
+ await component.waitForWrites();
472
+ component.handleInput("\u001b");
473
+ component.handleInput("\u001b");
474
+ return result;
475
+ };
476
+
477
+ try {
478
+ await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
479
+ host.operations.length = 0;
480
+ await host.commands.find((command) => command.name === "freeflow").definition.handler("settings", ctx);
481
+
482
+ assert.deepEqual(host.operations, []);
483
+ assert.deepEqual(host.state, { model: { provider: "faux", id: "return" }, thinkingLevel: "medium" });
484
+ assert.equal(host.entries.filter((entry) => entry.modelId).length, 0);
485
+ const saved = JSON.parse(await readFile(join(cwd, ".freeflow", "local.json"), "utf8"));
486
+ assert.equal(saved.cognitiveRouting.sessionStart.control, "manual");
487
+ } finally {
488
+ await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
489
+ await rm(cwd, { recursive: true, force: true });
490
+ }
491
+ });
492
+
493
+ test("child extension activation does not deactivate the parent routing controller", async () => {
494
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-child-activation-"));
495
+ await mkdir(join(cwd, ".freeflow"));
496
+ await writeFile(
497
+ join(cwd, ".freeflow", "config.json"),
498
+ JSON.stringify({
499
+ cognitiveRouting: {
500
+ enabled: true,
501
+ profiles: {
502
+ standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
503
+ reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
504
+ },
505
+ },
506
+ }),
507
+ );
508
+ const parent = createExtensionHost();
509
+ const parentContext = createContext(cwd, parent);
510
+ const child = createExtensionHost();
511
+ const childContext = createContext(cwd, child);
512
+ childContext.modelStateProvenance = { explicitModel: true, explicitThinking: true };
513
+ try {
514
+ await parent.handlers.get("session_start")({ type: "session_start", reason: "startup" }, parentContext);
515
+ await parent.handlers.get("before_agent_start")({ prompt: "hello", systemPrompt: "base prompt" }, parentContext);
516
+ assert.deepEqual(parent.state, { model: { provider: "faux", id: "standard" }, thinkingLevel: "high" });
517
+
518
+ await child.handlers.get("session_start")({ type: "session_start", reason: "startup" }, childContext);
519
+
520
+ assert.deepEqual(parent.state, { model: { provider: "faux", id: "standard" }, thinkingLevel: "high" });
521
+ assert.equal(
522
+ parent.entries.some((entry) => entry.kind === "closing"),
523
+ false,
524
+ );
525
+ } finally {
526
+ await child.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, childContext);
527
+ await parent.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, parentContext);
528
+ await rm(cwd, { recursive: true, force: true });
529
+ }
530
+ });
531
+
532
+ test("registered profile commands create manual holds and release them without a model entry", async () => {
533
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-command-"));
534
+ await mkdir(join(cwd, ".freeflow"));
535
+ await writeFile(
536
+ join(cwd, ".freeflow", "config.json"),
537
+ JSON.stringify({
538
+ cognitiveRouting: {
539
+ enabled: true,
540
+ profiles: {
541
+ standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
542
+ reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
543
+ },
544
+ },
545
+ }),
546
+ );
547
+ const host = createExtensionHost();
548
+ const ctx = createContext(cwd, host);
549
+ try {
550
+ await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
551
+ const command = host.commands.find(({ name }) => name === "freeflow");
552
+ assert.ok(command);
553
+ assert.deepEqual(
554
+ command.definition.getArgumentCompletions("profile r").map(({ value }) => value),
555
+ ["profile reasoning"],
556
+ );
557
+
558
+ await command.definition.handler("profile reasoning", ctx);
559
+ assert.deepEqual(host.state, { model: { provider: "faux", id: "reasoning" }, thinkingLevel: "max" });
560
+ assert.deepEqual(host.operations, ["prepare", "acquire", "setState", "prepare", "setState"]);
561
+ assert.ok(!host.activeToolNames().includes("freeflow_switch_profile"));
562
+
563
+ await command.definition.handler("profile auto", ctx);
564
+ assert.deepEqual(host.state, { model: { provider: "faux", id: "reasoning" }, thinkingLevel: "max" });
565
+ assert.deepEqual(host.operations, ["prepare", "acquire", "setState", "prepare", "setState", "prepare"]);
566
+ assert.equal(host.entries.at(-1).customType, "freeflow-cognitive-routing-control");
567
+ assert.ok(host.activeToolNames().includes("freeflow_switch_profile"));
568
+
569
+ const switchTool = host.tools.find((tool) => tool.name === "freeflow_switch_profile");
570
+ const switchResult = await switchTool.execute(
571
+ "call-1",
572
+ { target: "standard", reason: "Return to the standard profile." },
573
+ undefined,
574
+ undefined,
575
+ ctx,
576
+ );
577
+ assert.deepEqual(switchResult.details.result, {
578
+ status: "active",
579
+ changed: true,
580
+ from: "reasoning",
581
+ to: "standard",
582
+ profile: "standard",
583
+ });
584
+ assert.deepEqual(host.state, { model: { provider: "faux", id: "standard" }, thinkingLevel: "high" });
585
+ assert.deepEqual(host.operations, [
586
+ "prepare",
587
+ "acquire",
588
+ "setState",
589
+ "prepare",
590
+ "setState",
591
+ "prepare",
592
+ "prepare",
593
+ "setState",
594
+ ]);
595
+ } finally {
596
+ await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
597
+ await rm(cwd, { recursive: true, force: true });
598
+ }
599
+ });
600
+
601
+ test("settled runs preserve automatic reasoning and manual holds", async () => {
602
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-settled-"));
603
+ await mkdir(join(cwd, ".freeflow"));
604
+ await writeFile(
605
+ join(cwd, ".freeflow", "config.json"),
606
+ JSON.stringify({
607
+ cognitiveRouting: {
608
+ enabled: true,
609
+ profiles: {
610
+ standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
611
+ reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
612
+ },
613
+ },
614
+ }),
615
+ );
616
+ const host = createExtensionHost();
617
+ const ctx = createContext(cwd, host);
618
+ try {
619
+ await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
620
+ await host.handlers.get("before_agent_start")({ prompt: "hello", systemPrompt: "base prompt" }, ctx);
621
+ const switchTool = host.tools.find((tool) => tool.name === "freeflow_switch_profile");
622
+ await switchTool.execute(
623
+ "call-1",
624
+ { target: "reasoning", reason: "Need a deeper analysis." },
625
+ undefined,
626
+ undefined,
627
+ ctx,
628
+ );
629
+ assert.deepEqual(host.state, { model: { provider: "faux", id: "reasoning" }, thinkingLevel: "max" });
630
+
631
+ const callsBeforeSettledAutomatic = host.operations.length;
632
+ await host.handlers.get("agent_settled")({ type: "agent_settled" }, ctx);
633
+ assert.equal(host.operations.length, callsBeforeSettledAutomatic);
634
+ assert.deepEqual(host.state, { model: { provider: "faux", id: "reasoning" }, thinkingLevel: "max" });
635
+
636
+ const command = host.commands.find(({ name }) => name === "freeflow");
637
+ await command.definition.handler("profile reasoning", ctx);
638
+ const callsBeforeSettledManual = host.operations.length;
639
+ await host.handlers.get("agent_settled")({ type: "agent_settled" }, ctx);
640
+ assert.equal(host.operations.length, callsBeforeSettledManual);
641
+ assert.deepEqual(host.state, { model: { provider: "faux", id: "reasoning" }, thinkingLevel: "max" });
642
+ } finally {
643
+ await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
644
+ await rm(cwd, { recursive: true, force: true });
645
+ }
646
+ });
647
+
648
+ test("reload preserves a manual reasoning hold after lease rotation", async () => {
649
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-reload-manual-"));
650
+ await mkdir(join(cwd, ".freeflow"));
651
+ await writeFile(
652
+ join(cwd, ".freeflow", "config.json"),
653
+ JSON.stringify({
654
+ cognitiveRouting: {
655
+ enabled: true,
656
+ profiles: {
657
+ standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
658
+ reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
659
+ },
660
+ },
661
+ }),
662
+ );
663
+ const host = createExtensionHost();
664
+ const ctx = createContext(cwd, host);
665
+ ctx.isIdle = () => true;
666
+ try {
667
+ await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
668
+ await host.commands.find(({ name }) => name === "freeflow").definition.handler("profile reasoning", ctx);
669
+ await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "reload" }, ctx);
670
+
671
+ assert.deepEqual(host.state, { model: { provider: "faux", id: "return" }, thinkingLevel: "medium" });
672
+ await host.handlers.get("session_start")({ type: "session_start", reason: "reload" }, ctx);
673
+
674
+ assert.deepEqual(host.state, { model: { provider: "faux", id: "reasoning" }, thinkingLevel: "max" });
675
+ assert.ok(!host.activeToolNames().includes("freeflow_switch_profile"));
676
+ assert.deepEqual(host.state, { model: { provider: "faux", id: "reasoning" }, thinkingLevel: "max" });
677
+
678
+ const operationsBeforeCompaction = [...host.operations];
679
+ await host.handlers.get("session_compact")({ type: "session_compact" }, ctx);
680
+ assert.deepEqual(host.operations, operationsBeforeCompaction);
681
+ assert.deepEqual(host.state, { model: { provider: "faux", id: "reasoning" }, thinkingLevel: "max" });
682
+ assert.ok(!host.activeToolNames().includes("freeflow_switch_profile"));
683
+ } finally {
684
+ await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
685
+ await rm(cwd, { recursive: true, force: true });
686
+ }
687
+ });
688
+
689
+ test("tree navigation retains the current profile without a supported branch choice", async () => {
690
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-tree-"));
691
+ await mkdir(join(cwd, ".freeflow"));
692
+ await writeFile(
693
+ join(cwd, ".freeflow", "config.json"),
694
+ JSON.stringify({
695
+ cognitiveRouting: {
696
+ enabled: true,
697
+ profiles: {
698
+ standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
699
+ reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
700
+ },
701
+ },
702
+ }),
703
+ );
704
+ const host = createExtensionHost();
705
+ const ctx = createContext(cwd, host);
706
+ try {
707
+ await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
708
+ const standardBranch = [...host.entries];
709
+ const command = host.commands.find(({ name }) => name === "freeflow");
710
+ await command.definition.handler("profile reasoning", ctx);
711
+ assert.deepEqual(host.state, { model: { provider: "faux", id: "reasoning" }, thinkingLevel: "max" });
712
+
713
+ ctx.sessionManager.getBranch = () => standardBranch;
714
+ await host.handlers.get("session_tree")(
715
+ { type: "session_tree", newLeafId: "standard", oldLeafId: "reasoning" },
716
+ ctx,
717
+ );
718
+
719
+ assert.deepEqual(host.state, { model: { provider: "faux", id: "reasoning" }, thinkingLevel: "max" });
720
+ assert.ok(!host.activeToolNames().includes("freeflow_switch_profile"));
721
+ } finally {
722
+ await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
723
+ await rm(cwd, { recursive: true, force: true });
724
+ }
725
+ });
726
+
727
+ test("compaction reconciles without changing an active routing pair", async () => {
728
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-compact-"));
729
+ await mkdir(join(cwd, ".freeflow"));
730
+ await writeFile(
731
+ join(cwd, ".freeflow", "config.json"),
732
+ JSON.stringify({
733
+ cognitiveRouting: {
734
+ enabled: true,
735
+ profiles: {
736
+ standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
737
+ reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
738
+ },
739
+ },
740
+ }),
741
+ );
742
+ const host = createExtensionHost();
743
+ const ctx = createContext(cwd, host);
744
+ try {
745
+ await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
746
+ await host.handlers.get("before_agent_start")({ prompt: "hello", systemPrompt: "base prompt" }, ctx);
747
+ const operationsBeforeCompaction = [...host.operations];
748
+ await host.handlers.get("session_compact")({ type: "session_compact" }, ctx);
749
+
750
+ assert.deepEqual(host.operations, operationsBeforeCompaction);
751
+ assert.deepEqual(host.state, { model: { provider: "faux", id: "standard" }, thinkingLevel: "high" });
752
+ assert.ok(host.activeToolNames().includes("freeflow_switch_profile"));
753
+ } finally {
754
+ await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
755
+ await rm(cwd, { recursive: true, force: true });
756
+ }
757
+ });
758
+
759
+ test("reload with disabled configuration stays inactive after successful cleanup", async () => {
760
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-reload-disabled-"));
761
+ await mkdir(join(cwd, ".freeflow"));
762
+ const configPath = join(cwd, ".freeflow", "config.json");
763
+ await writeFile(
764
+ configPath,
765
+ JSON.stringify({
766
+ cognitiveRouting: {
767
+ enabled: true,
768
+ profiles: {
769
+ standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
770
+ reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
771
+ },
772
+ },
773
+ }),
774
+ );
775
+ const host = createExtensionHost();
776
+ const ctx = createContext(cwd, host);
777
+ try {
778
+ await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
779
+ await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "reload" }, ctx);
780
+ await writeFile(configPath, JSON.stringify({ cognitiveRouting: { enabled: false } }));
781
+ const operationsBeforeDisabledStart = [...host.operations];
782
+
783
+ await host.handlers.get("session_start")({ type: "session_start", reason: "reload" }, ctx);
784
+
785
+ assert.deepEqual(host.operations, operationsBeforeDisabledStart);
786
+ assert.deepEqual(host.state, { model: { provider: "faux", id: "return" }, thinkingLevel: "medium" });
787
+ assert.ok(!host.activeToolNames().includes("freeflow_switch_profile"));
788
+ } finally {
789
+ await rm(cwd, { recursive: true, force: true });
790
+ }
791
+ });
792
+
793
+ test("session replacement restores the pair before releasing routing ownership", async () => {
794
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-replacement-"));
795
+ await mkdir(join(cwd, ".freeflow"));
796
+ await writeFile(
797
+ join(cwd, ".freeflow", "config.json"),
798
+ JSON.stringify({
799
+ cognitiveRouting: {
800
+ enabled: true,
801
+ profiles: {
802
+ standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
803
+ reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
804
+ },
805
+ },
806
+ }),
807
+ );
808
+ const host = createExtensionHost();
809
+ const ctx = createContext(cwd, host);
810
+ try {
811
+ await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
812
+ await host.handlers.get("before_agent_start")({ prompt: "hello", systemPrompt: "base prompt" }, ctx);
813
+ await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "new" }, ctx);
814
+
815
+ assert.deepEqual(host.operations, ["prepare", "acquire", "setState", "prepare", "setState", "release"]);
816
+ assert.deepEqual(host.state, { model: { provider: "faux", id: "return" }, thinkingLevel: "medium" });
817
+ } finally {
818
+ await rm(cwd, { recursive: true, force: true });
819
+ }
820
+ });
821
+
822
+ test("reload recovery stays inactive after an unmatched closing intent", async () => {
823
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-reload-failure-"));
824
+ await mkdir(join(cwd, ".freeflow"));
825
+ await writeFile(
826
+ join(cwd, ".freeflow", "config.json"),
827
+ JSON.stringify({
828
+ cognitiveRouting: {
829
+ enabled: true,
830
+ profiles: {
831
+ standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
832
+ reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
833
+ },
834
+ },
835
+ }),
836
+ );
837
+ const host = createExtensionHost({ rejectReturnRestore: true });
838
+ const ctx = createContext(cwd, host);
839
+ try {
840
+ await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
841
+ await host.handlers.get("before_agent_start")({ prompt: "hello", systemPrompt: "base prompt" }, ctx);
842
+ await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "reload" }, ctx);
843
+ assert.deepEqual(host.operations, ["prepare", "acquire", "setState", "prepare", "setState", "release"]);
844
+ assert.equal(host.entries.at(-1).customType, "freeflow-cognitive-routing-intent");
845
+ assert.equal(host.entries.at(-1).data.kind, "closing");
846
+
847
+ await host.handlers.get("session_start")({ type: "session_start", reason: "reload" }, ctx);
848
+ assert.deepEqual(host.operations, [
849
+ "prepare",
850
+ "acquire",
851
+ "setState",
852
+ "prepare",
853
+ "setState",
854
+ "release",
855
+ "prepare",
856
+ "acquire",
857
+ "setState",
858
+ "release",
859
+ ]);
860
+ assert.equal(host.entries.at(-1).data.phase, "prepared");
861
+ assert.ok(!host.activeToolNames().includes("freeflow_switch_profile"));
862
+ } finally {
863
+ await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
864
+ await rm(cwd, { recursive: true, force: true });
865
+ }
866
+ });
867
+
868
+ test("Pi delivers a compact Cognitive Routing cue and volatile state per turn", async () => {
869
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-compact-delivery-"));
870
+ await mkdir(join(cwd, ".freeflow"));
871
+ await writeFile(
872
+ join(cwd, ".freeflow", "config.json"),
873
+ JSON.stringify({
874
+ cognitiveRouting: {
875
+ enabled: true,
876
+ profiles: {
877
+ standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
878
+ reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
879
+ },
880
+ },
881
+ }),
882
+ );
883
+ const host = createExtensionHost();
884
+ const ctx = createContext(cwd, host);
885
+ try {
886
+ await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
887
+ const beforeAgentStart = host.handlers.get("before_agent_start");
888
+ const first = await beforeAgentStart({ systemPrompt: "base prompt" }, ctx);
889
+
890
+ assert.equal(first.message, undefined);
891
+ assert.match(first.systemPrompt, /## Cognitive Routing Cue/);
892
+ assert.match(
893
+ first.systemPrompt,
894
+ /governs compute placement across Freeflow methods.*skill has not been read in the current context.*read it before applying another Freeflow skill or taking task Act/s,
895
+ );
896
+ assert.match(
897
+ first.systemPrompt,
898
+ /every authorized execution-bearing bounded activity is Reasoning-led.*Standard takes task Act only inside delegation/s,
899
+ );
900
+ assert.ok(
901
+ first.systemPrompt.indexOf("## Action Selection Cue") < first.systemPrompt.indexOf("## Cognitive Routing Cue"),
902
+ );
903
+ assert.doesNotMatch(first.systemPrompt, /^# Cognitive Routing$/m);
904
+ assert.doesNotMatch(first.systemPrompt, /^# Automatic Routing Kernel$/m);
905
+
906
+ const contextHandler = host.handlers.get("context");
907
+ const firstContext = await contextHandler({ messages: [] }, ctx);
908
+ assert.equal(firstContext.messages.length, 1);
909
+ assert.equal(firstContext.messages[0].customType, "freeflow-runtime-state");
910
+ assert.match(firstContext.messages[0].content, /Cognitive Routing: active/);
911
+ assert.match(firstContext.messages[0].content, /Control: `automatic`/);
912
+ assert.match(firstContext.messages[0].content, /Profile: `standard`/);
913
+
914
+ const second = await beforeAgentStart({ systemPrompt: "base prompt" }, ctx);
915
+ assert.equal(second.message, undefined);
916
+ assert.equal((second.systemPrompt.match(/## Cognitive Routing Cue/g) ?? []).length, 1);
917
+ } finally {
918
+ await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
919
+ await rm(cwd, { recursive: true, force: true });
920
+ }
921
+ });
922
+
923
+ test("Pi refreshes volatile Cognitive Routing state after a profile switch", async () => {
924
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-volatile-state-"));
925
+ await mkdir(join(cwd, ".freeflow"));
926
+ await writeFile(
927
+ join(cwd, ".freeflow", "config.json"),
928
+ JSON.stringify({
929
+ cognitiveRouting: {
930
+ enabled: true,
931
+ profiles: {
932
+ standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
933
+ reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
934
+ },
935
+ },
936
+ }),
937
+ );
938
+ const host = createExtensionHost();
939
+ const ctx = createContext(cwd, host);
940
+ try {
941
+ await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
942
+ await host.handlers.get("before_agent_start")({ systemPrompt: "base prompt" }, ctx);
943
+
944
+ const contextHandler = host.handlers.get("context");
945
+ const standardContext = await contextHandler({ messages: [] }, ctx);
946
+ assert.match(standardContext.messages[0].content, /Profile: `standard`/);
947
+
948
+ const switchTool = host.tools.find((tool) => tool.name === "freeflow_switch_profile");
949
+ const result = await switchTool.execute(
950
+ "call-volatile-state",
951
+ { target: "reasoning", reason: "Need a deeper analysis." },
952
+ undefined,
953
+ undefined,
954
+ ctx,
955
+ );
956
+ assert.equal(result.details.result.to, "reasoning");
957
+
958
+ const reasoningContext = await contextHandler({ messages: standardContext.messages }, ctx);
959
+ assert.equal(reasoningContext.messages.length, 1);
960
+ assert.match(reasoningContext.messages[0].content, /Control: `automatic`/);
961
+ assert.match(reasoningContext.messages[0].content, /Profile: `reasoning`/);
962
+ assert.doesNotMatch(reasoningContext.messages[0].content, /Profile: `standard`/);
963
+ } finally {
964
+ await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
965
+ await rm(cwd, { recursive: true, force: true });
966
+ }
967
+ });
968
+
969
+ test("Pi keeps Cognitive Routing behind the Skills parent gate", async () => {
970
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-skills-gate-"));
971
+ await mkdir(join(cwd, ".freeflow"));
972
+ await writeFile(
973
+ join(cwd, ".freeflow", "config.json"),
974
+ JSON.stringify({
975
+ skills: { enabled: false },
976
+ cognitiveRouting: {
977
+ enabled: true,
978
+ profiles: {
979
+ standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
980
+ reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
981
+ },
982
+ },
983
+ }),
984
+ );
985
+ const host = createExtensionHost();
986
+ const ctx = createContext(cwd, host);
987
+ try {
988
+ await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
989
+ const result = await host.handlers.get("before_agent_start")({ systemPrompt: "base prompt" }, ctx);
990
+ assert.equal(result.message, undefined);
991
+ assert.doesNotMatch(result.systemPrompt, /## Cognitive Routing Cue/);
992
+ assert.doesNotMatch(result.systemPrompt, /^# Cognitive Routing$/m);
993
+ assert.ok(!host.activeToolNames().includes("freeflow_switch_profile"));
994
+ const providerContext = await host.handlers.get("context")({ messages: [] }, ctx);
995
+ assert.match(providerContext.messages[0].content, /Skills: inactive/);
996
+ assert.match(providerContext.messages[0].content, /Cognitive Routing: inactive/);
997
+ } finally {
998
+ await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
999
+ await rm(cwd, { recursive: true, force: true });
1000
+ }
1001
+ });
1002
+
1003
+ test("Pi lifecycle prepares, activates, restores, and releases Cognitive Routing ownership", async () => {
1004
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-extension-"));
1005
+ await mkdir(join(cwd, ".freeflow"));
1006
+ await writeFile(
1007
+ join(cwd, ".freeflow", "config.json"),
1008
+ JSON.stringify({
1009
+ cognitiveRouting: {
1010
+ enabled: true,
1011
+ profiles: {
1012
+ standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
1013
+ reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
1014
+ },
1015
+ },
1016
+ }),
1017
+ );
1018
+ const host = createExtensionHost();
1019
+ const ctx = createContext(cwd, host);
1020
+ try {
1021
+ await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
1022
+ assert.deepEqual(host.operations, []);
1023
+
1024
+ const beforeAgentStart = await host.handlers.get("before_agent_start")(
1025
+ { prompt: "hello", systemPrompt: "base prompt" },
1026
+ ctx,
1027
+ );
1028
+ assert.deepEqual(host.operations.slice(0, 3), ["prepare", "acquire", "setState"]);
1029
+ assert.deepEqual(host.state, { model: { provider: "faux", id: "standard" }, thinkingLevel: "high" });
1030
+ assert.equal(host.entries[0].customType, "freeflow-cognitive-routing-intent");
1031
+ assert.ok(host.activeToolNames().includes("freeflow_switch_profile"));
1032
+ assert.match(beforeAgentStart.systemPrompt, /## Cognitive Routing Cue/);
1033
+ assert.doesNotMatch(beforeAgentStart.systemPrompt, /^# Cognitive Routing$/m);
1034
+ assert.doesNotMatch(beforeAgentStart.systemPrompt, /^# Automatic Routing Kernel$/m);
1035
+ assert.equal(beforeAgentStart.message, undefined);
1036
+
1037
+ const switchTool = host.tools.find((tool) => tool.name === "freeflow_switch_profile");
1038
+ await switchTool.execute(
1039
+ "call-1",
1040
+ { target: "reasoning", reason: "Continue the unresolved architecture decision." },
1041
+ undefined,
1042
+ undefined,
1043
+ ctx,
1044
+ );
1045
+ assert.deepEqual(host.state, { model: { provider: "faux", id: "reasoning" }, thinkingLevel: "max" });
1046
+
1047
+ await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "reload" }, ctx);
1048
+
1049
+ assert.deepEqual(host.operations, [
1050
+ "prepare",
1051
+ "acquire",
1052
+ "setState",
1053
+ "prepare",
1054
+ "setState",
1055
+ "prepare",
1056
+ "setState",
1057
+ "release",
1058
+ ]);
1059
+ assert.deepEqual(host.state, { model: { provider: "faux", id: "return" }, thinkingLevel: "medium" });
1060
+ assert.equal(host.entries.at(-1).type, "model_state_change");
1061
+ assert.equal(host.entries.at(-1).modelId, "return");
1062
+
1063
+ await host.handlers.get("session_start")({ type: "session_start", reason: "reload" }, ctx);
1064
+ assert.deepEqual(host.operations.slice(-3), ["prepare", "acquire", "setState"]);
1065
+ assert.deepEqual(host.state, { model: { provider: "faux", id: "reasoning" }, thinkingLevel: "max" });
1066
+ assert.ok(host.activeToolNames().includes("freeflow_switch_profile"));
1067
+ await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
1068
+ } finally {
1069
+ await rm(cwd, { recursive: true, force: true });
1070
+ }
1071
+ });