@hassangameryt/freeflow 0.5.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (301) hide show
  1. package/.claude-plugin/marketplace.json +1 -1
  2. package/.claude-plugin/plugin.json +2 -2
  3. package/.codex-plugin/plugin.json +4 -5
  4. package/README.md +141 -163
  5. package/capabilities/cognitive-routing/SKILL.md +387 -0
  6. package/capabilities/context-virtualization/SKILL.md +103 -0
  7. package/capabilities/conversation-history/SKILL.md +53 -0
  8. package/command-surface.json +6 -37
  9. package/hooks/freeflow-runtime-context.mjs +50 -541
  10. package/hooks/hooks.json +1 -26
  11. package/hooks/tests/check-runtime-context-hook.sh +114 -308
  12. package/package.json +27 -17
  13. package/pi-extension/dist/cognitive-routing/commands.js +96 -0
  14. package/pi-extension/dist/cognitive-routing/config.js +232 -0
  15. package/pi-extension/dist/cognitive-routing/controller.js +1292 -0
  16. package/pi-extension/dist/cognitive-routing/history.js +451 -0
  17. package/pi-extension/dist/cognitive-routing/host.js +188 -0
  18. package/pi-extension/dist/cognitive-routing/pi-routing-session.js +414 -0
  19. package/pi-extension/dist/cognitive-routing/pi-session-control.js +320 -0
  20. package/pi-extension/dist/cognitive-routing/runtime.js +43 -0
  21. package/pi-extension/dist/cognitive-routing/tool.js +212 -0
  22. package/pi-extension/dist/cognitive-routing/types.js +6 -0
  23. package/pi-extension/dist/context-virtualization/commands.js +89 -0
  24. package/pi-extension/dist/context-virtualization/projector.js +42 -0
  25. package/pi-extension/dist/context-virtualization/runtime.js +356 -0
  26. package/pi-extension/dist/context-virtualization/state.js +57 -0
  27. package/pi-extension/dist/context-virtualization/types.js +10 -0
  28. package/pi-extension/dist/conversation-history/passages.js +66 -0
  29. package/pi-extension/dist/conversation-history/ranking.js +105 -0
  30. package/pi-extension/dist/conversation-history/runtime.js +226 -0
  31. package/pi-extension/dist/conversation-history/search.js +56 -0
  32. package/pi-extension/dist/conversation-history/types.js +1 -0
  33. package/pi-extension/dist/freeflow-context/resolver.js +120 -0
  34. package/pi-extension/dist/freeflow-context/runtime.js +80 -0
  35. package/pi-extension/dist/freeflow-context/source-projector.js +95 -0
  36. package/pi-extension/dist/freeflow-context/tool.js +308 -0
  37. package/pi-extension/dist/freeflow-context/types.js +12 -0
  38. package/pi-extension/dist/freeflow-context/visibility.js +28 -0
  39. package/pi-extension/dist/index.js +564 -129
  40. package/pi-extension/dist/runtime/runtime-context.js +357 -470
  41. package/pi-extension/dist/runtime/runtime-identity.js +5 -0
  42. package/pi-extension/dist/settings/settings-tui.js +78 -0
  43. package/pi-extension/dist/settings/settings-ui.js +572 -826
  44. package/pi-extension/dist/ui/text-component.js +57 -0
  45. package/pi-extension/src/cognitive-routing/commands.ts +129 -0
  46. package/pi-extension/src/cognitive-routing/config.ts +303 -0
  47. package/pi-extension/src/cognitive-routing/controller.ts +1628 -0
  48. package/pi-extension/src/cognitive-routing/history.ts +650 -0
  49. package/pi-extension/src/cognitive-routing/host.ts +266 -0
  50. package/pi-extension/src/cognitive-routing/pi-routing-session.ts +538 -0
  51. package/pi-extension/src/cognitive-routing/pi-session-control.ts +444 -0
  52. package/pi-extension/src/cognitive-routing/runtime.ts +57 -0
  53. package/pi-extension/src/cognitive-routing/tool.ts +273 -0
  54. package/pi-extension/src/cognitive-routing/types.ts +89 -0
  55. package/pi-extension/src/context-virtualization/commands.ts +111 -0
  56. package/pi-extension/src/context-virtualization/projector.ts +62 -0
  57. package/pi-extension/src/context-virtualization/runtime.ts +406 -0
  58. package/pi-extension/src/context-virtualization/state.ts +79 -0
  59. package/pi-extension/src/context-virtualization/types.ts +92 -0
  60. package/pi-extension/src/conversation-history/passages.ts +81 -0
  61. package/pi-extension/src/conversation-history/ranking.ts +155 -0
  62. package/pi-extension/src/conversation-history/runtime.ts +250 -0
  63. package/pi-extension/src/conversation-history/search.ts +79 -0
  64. package/pi-extension/src/conversation-history/types.ts +31 -0
  65. package/pi-extension/src/freeflow-context/resolver.ts +139 -0
  66. package/pi-extension/src/freeflow-context/runtime.ts +100 -0
  67. package/pi-extension/src/freeflow-context/source-projector.ts +106 -0
  68. package/pi-extension/src/freeflow-context/tool.ts +360 -0
  69. package/pi-extension/src/freeflow-context/types.ts +70 -0
  70. package/pi-extension/src/freeflow-context/visibility.ts +53 -0
  71. package/pi-extension/src/index.ts +597 -135
  72. package/pi-extension/src/runtime/runtime-context.ts +373 -491
  73. package/pi-extension/src/runtime/runtime-identity.ts +8 -0
  74. package/pi-extension/src/settings/settings-tui.ts +93 -0
  75. package/pi-extension/src/settings/settings-ui.ts +679 -910
  76. package/pi-extension/src/ui/text-component.ts +66 -0
  77. package/pi-extension/tests/cognitive-routing/commands.test.js +134 -0
  78. package/pi-extension/tests/cognitive-routing/controller.test.js +1128 -0
  79. package/pi-extension/tests/cognitive-routing/extension-lifecycle.test.js +1142 -0
  80. package/pi-extension/tests/cognitive-routing/history.test.js +274 -0
  81. package/pi-extension/tests/cognitive-routing/host-fixture.js +4 -0
  82. package/pi-extension/tests/cognitive-routing/host-identity.test.js +63 -0
  83. package/pi-extension/tests/cognitive-routing/pi-normal-lifecycle.test.js +434 -0
  84. package/pi-extension/tests/cognitive-routing/pi-session-control.test.js +174 -0
  85. package/pi-extension/tests/cognitive-routing/pi-session-history.test.js +91 -0
  86. package/pi-extension/tests/cognitive-routing/profile-resolution.test.js +279 -0
  87. package/pi-extension/tests/cognitive-routing/tool.test.js +230 -0
  88. package/pi-extension/tests/context-virtualization/extension.test.js +749 -0
  89. package/pi-extension/tests/context-virtualization/projector.test.js +110 -0
  90. package/pi-extension/tests/context-virtualization/runtime.test.js +216 -0
  91. package/pi-extension/tests/conversation-history/runtime.test.js +98 -0
  92. package/pi-extension/tests/conversation-history/search.test.js +97 -0
  93. package/pi-extension/tests/integration/mode-free-core.test.js +218 -0
  94. package/pi-extension/tests/integration/pi-extension.test.js +532 -3701
  95. package/pi-extension/tests/integration/prompt-architecture.test.js +350 -0
  96. package/pi-extension/tests/integration/unaffected-regressions.test.js +411 -0
  97. package/runtime/prompts/cognitive-routing.md +10 -0
  98. package/runtime/prompts/context-virtualization.md +3 -0
  99. package/runtime/prompts/conversation-history.md +3 -0
  100. package/runtime/prompts/core.md +84 -0
  101. package/runtime/prompts/interaction-contract.md +11 -0
  102. package/skills/action-selection/SKILL.md +140 -0
  103. package/skills/action-selection/references/trajectory-stalls.md +71 -0
  104. package/skills/bypass/SKILL.md +109 -38
  105. package/skills/commit-work/SKILL.md +94 -55
  106. package/skills/commit-work/references/git-state-edges.md +94 -0
  107. package/skills/commit-work/references/simple-push.md +92 -0
  108. package/skills/commit-work/references/staging-decisions.md +79 -46
  109. package/skills/decision-gate/SKILL.md +80 -22
  110. package/skills/design-for-depth/SKILL.md +206 -60
  111. package/skills/design-for-depth/references/design-pressure-signals.md +108 -240
  112. package/skills/design-for-depth/references/interface-design-loop.md +77 -97
  113. package/skills/design-for-depth/references/module-and-dependency-design.md +147 -0
  114. package/skills/design-for-depth/references/software-design-philosophy.md +128 -174
  115. package/skills/design-for-depth/references/state-and-failure-boundaries.md +136 -0
  116. package/skills/diagnose-failure/SKILL.md +200 -48
  117. package/skills/diagnose-failure/references/diagnostic-loop-catalog.md +18 -6
  118. package/skills/diagnose-failure/references/{flaky-and-performance.md → flaky-and-performance-diagnosis.md} +13 -5
  119. package/skills/discuss/SKILL.md +119 -65
  120. package/skills/discuss/references/discussion-continuity.md +123 -0
  121. package/skills/evaluate-skill/SKILL.md +166 -52
  122. package/skills/evaluate-skill/references/{definition-schema.md → evaluation-definition-schema.md} +43 -3
  123. package/skills/evaluate-skill/references/evaluation-design.md +56 -30
  124. package/skills/evaluate-skill/references/execution-and-evidence.md +8 -3
  125. package/skills/evaluate-skill/references/review-and-revision.md +62 -21
  126. package/skills/evaluate-skill/scripts/lib/definitions.mjs +114 -3
  127. package/skills/evaluate-skill/scripts/lib/grade-interaction.mjs +156 -1
  128. package/skills/evaluate-skill/scripts/lib/grade.mjs +9 -1
  129. package/skills/evaluate-skill/scripts/lib/pi.mjs +147 -32
  130. package/skills/evaluate-skill/scripts/lib/rpc.mjs +4 -4
  131. package/skills/evaluate-skill/scripts/lib/runner.mjs +36 -14
  132. package/skills/evaluate-skill/scripts/lib/sandbox.mjs +92 -0
  133. package/skills/evaluate-skill/scripts/pi-guard.mjs +1 -1
  134. package/skills/evaluate-skill/scripts/pi-observer.mjs +66 -0
  135. package/skills/evaluate-skill/scripts/skill-eval.mjs +6 -5
  136. package/skills/execute-work/SKILL.md +99 -57
  137. package/skills/execute-work/references/code-practices.md +4 -4
  138. package/skills/execute-work/references/execute-work-edges.md +62 -0
  139. package/skills/finish-branch/SKILL.md +2 -2
  140. package/skills/finish-branch/references/integration-options.md +1 -1
  141. package/skills/handoff/SKILL.md +3 -3
  142. package/skills/launch-work/SKILL.md +1 -1
  143. package/skills/launch-work/references/launch-readiness.md +2 -2
  144. package/skills/migration-work/SKILL.md +2 -2
  145. package/skills/migration-work/references/migration-lifecycle.md +1 -1
  146. package/skills/release-work/SKILL.md +1 -1
  147. package/skills/release-work/references/release-evidence.md +1 -1
  148. package/skills/review-artifact/SKILL.md +129 -66
  149. package/skills/review-artifact/references/adjudicate-artifact-review.md +101 -0
  150. package/skills/review-artifact/references/independent-artifact-reviewer-contract.md +159 -0
  151. package/skills/review-work/SKILL.md +113 -68
  152. package/skills/review-work/references/adjudicate-work-review.md +97 -0
  153. package/skills/review-work/references/independent-work-reviewer-contract.md +139 -0
  154. package/skills/review-work/references/security-risk-lens.md +18 -16
  155. package/skills/setup-freeflow/SKILL.md +16 -17
  156. package/skills/setup-freeflow/references/activation-contract.md +24 -30
  157. package/skills/setup-freeflow/references/host-setup.md +6 -7
  158. package/skills/simplify-code/SKILL.md +97 -39
  159. package/skills/simplify-code/references/simplification-patterns.md +1 -1
  160. package/skills/tdd/SKILL.md +100 -53
  161. package/skills/tdd/references/test-design.md +3 -3
  162. package/skills/track-work/SKILL.md +195 -58
  163. package/skills/track-work/references/working-record-format.md +515 -0
  164. package/skills/track-work/scripts/lib/document.mjs +713 -0
  165. package/skills/track-work/scripts/lib/format.mjs +166 -0
  166. package/skills/track-work/scripts/lib/store.mjs +233 -0
  167. package/skills/track-work/scripts/lib/transitions.mjs +790 -0
  168. package/skills/track-work/scripts/lib/views.mjs +40 -0
  169. package/skills/track-work/scripts/working-record.mjs +316 -0
  170. package/skills/verify-work/SKILL.md +57 -19
  171. package/skills/verify-work/references/browser-runtime-evidence.md +2 -2
  172. package/skills/verify-work/references/integration-evidence.md +1 -7
  173. package/skills/verify-work/references/performance-evidence.md +2 -2
  174. package/skills/workflow/SKILL.md +137 -55
  175. package/skills/workflow/references/domain-skill-composition.md +26 -40
  176. package/skills/write-plan/SKILL.md +1 -1
  177. package/skills/write-skill/SKILL.md +218 -61
  178. package/skills/write-skill/references/activation-boundaries.md +63 -21
  179. package/skills/write-skill/references/agent-first-instructions.md +61 -27
  180. package/skills/write-skill/references/progressive-disclosure.md +97 -33
  181. package/skills/write-skill/references/skill-dependency-graphs.md +190 -0
  182. package/capabilities/output-router/SKILL.md +0 -206
  183. package/capabilities/output-router/references/safety-policy.md +0 -40
  184. package/capabilities/output-router/references/setup.md +0 -110
  185. package/pi-extension/dist/output-router/host-producer-identification.js +0 -232
  186. package/pi-extension/dist/output-router/native-safety-net.js +0 -216
  187. package/pi-extension/dist/output-router/observed-tool-routing.js +0 -122
  188. package/pi-extension/dist/output-router/renderers.js +0 -1023
  189. package/pi-extension/dist/output-router/router-tools.js +0 -554
  190. package/pi-extension/dist/output-router/schemas.js +0 -383
  191. package/pi-extension/dist/output-router/status.js +0 -744
  192. package/pi-extension/dist/output-router/utils.js +0 -502
  193. package/pi-extension/src/output-router/host-producer-identification.ts +0 -256
  194. package/pi-extension/src/output-router/native-safety-net.ts +0 -248
  195. package/pi-extension/src/output-router/observed-tool-routing.ts +0 -134
  196. package/pi-extension/src/output-router/renderers.ts +0 -1108
  197. package/pi-extension/src/output-router/router-tools.ts +0 -579
  198. package/pi-extension/src/output-router/schemas.ts +0 -387
  199. package/pi-extension/src/output-router/status.ts +0 -791
  200. package/pi-extension/src/output-router/utils.ts +0 -554
  201. package/pi-extension/tests/output-router/observed-producers.test.js +0 -132
  202. package/pi-extension/tests/output-router/search.test.js +0 -722
  203. package/router/dist/benchmarks/benchmark-harness.d.ts +0 -35
  204. package/router/dist/benchmarks/benchmark-harness.js +0 -92
  205. package/router/dist/benchmarks/benchmarks.d.ts +0 -109
  206. package/router/dist/benchmarks/benchmarks.js +0 -979
  207. package/router/dist/benchmarks/codex-qa-benchmarks.d.ts +0 -95
  208. package/router/dist/benchmarks/codex-qa-benchmarks.js +0 -500
  209. package/router/dist/benchmarks/command-benchmarks.d.ts +0 -143
  210. package/router/dist/benchmarks/command-benchmarks.js +0 -698
  211. package/router/dist/benchmarks/context-mode-normalized-benchmarks.d.ts +0 -82
  212. package/router/dist/benchmarks/context-mode-normalized-benchmarks.js +0 -773
  213. package/router/dist/benchmarks/context-mode-real-deep-benchmark.d.ts +0 -132
  214. package/router/dist/benchmarks/context-mode-real-deep-benchmark.js +0 -1380
  215. package/router/dist/benchmarks/context-saving-baseline.d.ts +0 -60
  216. package/router/dist/benchmarks/context-saving-baseline.js +0 -470
  217. package/router/dist/benchmarks/index-benchmarks.d.ts +0 -103
  218. package/router/dist/benchmarks/index-benchmarks.js +0 -862
  219. package/router/dist/benchmarks/storage-policy-benchmarks.d.ts +0 -101
  220. package/router/dist/benchmarks/storage-policy-benchmarks.js +0 -618
  221. package/router/dist/benchmarks/vault-index-benchmarks.d.ts +0 -56
  222. package/router/dist/benchmarks/vault-index-benchmarks.js +0 -322
  223. package/router/dist/config/config.d.ts +0 -103
  224. package/router/dist/config/config.js +0 -632
  225. package/router/dist/config/router-contract.d.ts +0 -10
  226. package/router/dist/config/router-contract.js +0 -54
  227. package/router/dist/config/schema.d.ts +0 -27
  228. package/router/dist/config/schema.js +0 -648
  229. package/router/dist/config/types.d.ts +0 -573
  230. package/router/dist/config/types.js +0 -41
  231. package/router/dist/evidence/bounded-evidence.d.ts +0 -40
  232. package/router/dist/evidence/bounded-evidence.js +0 -323
  233. package/router/dist/evidence/evidence-range-selector.d.ts +0 -24
  234. package/router/dist/evidence/evidence-range-selector.js +0 -232
  235. package/router/dist/evidence/evidence-search.d.ts +0 -27
  236. package/router/dist/evidence/evidence-search.js +0 -490
  237. package/router/dist/evidence/evidence.d.ts +0 -38
  238. package/router/dist/evidence/evidence.js +0 -200
  239. package/router/dist/evidence/failure-contracts.d.ts +0 -37
  240. package/router/dist/evidence/failure-contracts.js +0 -166
  241. package/router/dist/evidence/line-ranges.d.ts +0 -24
  242. package/router/dist/evidence/line-ranges.js +0 -22
  243. package/router/dist/experiments/local-index.d.ts +0 -64
  244. package/router/dist/experiments/local-index.js +0 -581
  245. package/router/dist/index.d.ts +0 -30
  246. package/router/dist/index.js +0 -32
  247. package/router/dist/local/local-traversal.d.ts +0 -18
  248. package/router/dist/local/local-traversal.js +0 -334
  249. package/router/dist/processing/engine.d.ts +0 -132
  250. package/router/dist/processing/engine.js +0 -533
  251. package/router/dist/processing/reducers.d.ts +0 -280
  252. package/router/dist/processing/reducers.js +0 -1673
  253. package/router/dist/processing/renderers.d.ts +0 -31
  254. package/router/dist/processing/renderers.js +0 -195
  255. package/router/dist/processing/scripts.d.ts +0 -91
  256. package/router/dist/processing/scripts.js +0 -386
  257. package/router/dist/repo/repo-traversal.d.ts +0 -18
  258. package/router/dist/repo/repo-traversal.js +0 -245
  259. package/router/dist/routing/observed-reducers.d.ts +0 -41
  260. package/router/dist/routing/observed-reducers.js +0 -661
  261. package/router/dist/routing/observed-routing.d.ts +0 -26
  262. package/router/dist/routing/observed-routing.js +0 -378
  263. package/router/dist/routing/parsers.d.ts +0 -16
  264. package/router/dist/routing/parsers.js +0 -521
  265. package/router/dist/routing/run-filters.d.ts +0 -60
  266. package/router/dist/routing/run-filters.js +0 -238
  267. package/router/dist/routing/run-reducers.d.ts +0 -49
  268. package/router/dist/routing/run-reducers.js +0 -122
  269. package/router/dist/sandbox/adapter-roots.d.ts +0 -46
  270. package/router/dist/sandbox/adapter-roots.js +0 -66
  271. package/router/dist/sandbox/eryx-python-adapter.d.ts +0 -13
  272. package/router/dist/sandbox/eryx-python-adapter.js +0 -868
  273. package/router/dist/sandbox/jq-wasm-adapter.d.ts +0 -9
  274. package/router/dist/sandbox/jq-wasm-adapter.js +0 -429
  275. package/router/dist/sandbox/quickjs-wasi-adapter.d.ts +0 -13
  276. package/router/dist/sandbox/quickjs-wasi-adapter.js +0 -384
  277. package/router/dist/sandbox/script-sandbox.d.ts +0 -134
  278. package/router/dist/sandbox/script-sandbox.js +0 -337
  279. package/router/dist/setup/script-transform-adapters.d.ts +0 -32
  280. package/router/dist/setup/script-transform-adapters.js +0 -278
  281. package/router/dist/tools/batch.d.ts +0 -33
  282. package/router/dist/tools/batch.js +0 -586
  283. package/router/dist/tools/run.d.ts +0 -62
  284. package/router/dist/tools/run.js +0 -1304
  285. package/router/dist/tools/search.d.ts +0 -66
  286. package/router/dist/tools/search.js +0 -1364
  287. package/router/dist/transform/engine.d.ts +0 -168
  288. package/router/dist/transform/engine.js +0 -2072
  289. package/router/dist/vault/vault-index.d.ts +0 -125
  290. package/router/dist/vault/vault-index.js +0 -416
  291. package/router/dist/vault/vault.d.ts +0 -138
  292. package/router/dist/vault/vault.js +0 -630
  293. package/runtime/interaction-contract.md +0 -16
  294. package/skills/discuss/references/checkpoints.md +0 -66
  295. package/skills/execute-work/references/execution-loop.md +0 -77
  296. package/skills/mode-contract/SKILL.md +0 -104
  297. package/skills/review-artifact/references/reviewer-prompt.md +0 -148
  298. package/skills/review-work/references/reviewer-prompt.md +0 -134
  299. package/skills/track-work/references/working-record-schema.md +0 -378
  300. package/skills/workflow/references/workflow-loop.md +0 -92
  301. /package/skills/handoff/references/{templates.md → handoff-templates.md} +0 -0
@@ -0,0 +1,434 @@
1
+ import assert from "node:assert/strict";
2
+ import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import test from "node:test";
6
+ import freeflowExtension from "../../dist/index.js";
7
+
8
+ const profiles = {
9
+ standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
10
+ reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
11
+ };
12
+
13
+ function createNormalPiHost({
14
+ failInactivePersistence = false,
15
+ failTargetThinking = false,
16
+ emitNativeEvents = false,
17
+ } = {}) {
18
+ let currentModel = {
19
+ provider: "faux",
20
+ id: "return",
21
+ reasoning: true,
22
+ thinkingLevelMap: { xhigh: "xhigh", max: "max" },
23
+ };
24
+ let currentThinkingLevel = "medium";
25
+ let activeToolNames;
26
+ const entries = [];
27
+ const tools = [];
28
+ const commands = [];
29
+ const shortcuts = [];
30
+ const handlers = new Map();
31
+ const calls = [];
32
+ const statuses = [];
33
+ const models = new Map([
34
+ ["faux/return", currentModel],
35
+ ["faux/standard", { provider: "faux", id: "standard", reasoning: true, thinkingLevelMap: { max: "max" } }],
36
+ ["faux/reasoning", { provider: "faux", id: "reasoning", reasoning: true, thinkingLevelMap: { max: "max" } }],
37
+ ]);
38
+
39
+ const appendNativeEntry = (entry) => entries.push({ type: entry.type, ...entry });
40
+ const pi = {
41
+ registerTool(tool) {
42
+ tools.push(tool);
43
+ },
44
+ registerCommand(name, definition) {
45
+ commands.push({ name, definition });
46
+ },
47
+ registerShortcut(shortcut, definition) {
48
+ shortcuts.push({ shortcut, definition });
49
+ },
50
+ on(event, handler) {
51
+ handlers.set(event, handler);
52
+ },
53
+ appendEntry(customType, data) {
54
+ if (failInactivePersistence && customType === "freeflow-cognitive-routing-inactive") {
55
+ throw new Error("simulated inactive persistence failure");
56
+ }
57
+ entries.push({ type: "custom", customType, data });
58
+ },
59
+ async setModel(model) {
60
+ calls.push(["setModel", model.provider, model.id]);
61
+ const previousModel = currentModel;
62
+ currentModel = model;
63
+ appendNativeEntry({ type: "model_change", provider: model.provider, modelId: model.id });
64
+ if (emitNativeEvents) {
65
+ await handlers.get("model_select")?.({ type: "model_select", model, previousModel, source: "set" }, ctx);
66
+ }
67
+ return true;
68
+ },
69
+ setThinkingLevel(level) {
70
+ calls.push(["setThinkingLevel", level]);
71
+ if (failTargetThinking && level === "max") throw new Error("simulated target thinking failure");
72
+ currentThinkingLevel = level;
73
+ appendNativeEntry({ type: "thinking_level_change", thinkingLevel: level });
74
+ },
75
+ getAllTools() {
76
+ return tools.map((tool) => ({ name: tool.name }));
77
+ },
78
+ getActiveTools() {
79
+ return activeToolNames ?? tools.map((tool) => tool.name);
80
+ },
81
+ setActiveTools(names) {
82
+ activeToolNames = [...names];
83
+ },
84
+ sendUserMessage() {},
85
+ };
86
+
87
+ const ctx = {
88
+ cwd: undefined,
89
+ mode: "print",
90
+ hasUI: false,
91
+ get model() {
92
+ return currentModel;
93
+ },
94
+ get thinkingLevel() {
95
+ return currentThinkingLevel;
96
+ },
97
+ modelRegistry: {
98
+ find(provider, modelId) {
99
+ return models.get(`${provider}/${modelId}`);
100
+ },
101
+ async getApiKeyAndHeaders() {
102
+ return { ok: true };
103
+ },
104
+ },
105
+ modelStateProvenance: {},
106
+ isIdle: () => true,
107
+ sessionManager: {
108
+ getEntries: () => entries,
109
+ getBranch: () => entries,
110
+ buildContextEntries: () => entries,
111
+ getLeafId: () => entries.at(-1)?.id ?? null,
112
+ },
113
+ ui: {
114
+ notify() {},
115
+ setStatus(_key, value) {
116
+ statuses.push(value);
117
+ },
118
+ },
119
+ };
120
+
121
+ return { pi, ctx, entries, tools, commands, shortcuts, handlers, calls, statuses, models };
122
+ }
123
+
124
+ async function configure(cwd) {
125
+ await mkdir(join(cwd, ".freeflow"));
126
+ await writeFile(
127
+ join(cwd, ".freeflow", "config.json"),
128
+ JSON.stringify({ cognitiveRouting: { enabled: true, profiles } }),
129
+ "utf8",
130
+ );
131
+ }
132
+
133
+ async function start(host) {
134
+ freeflowExtension(host.pi);
135
+ await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, host.ctx);
136
+ return host.handlers.get("before_agent_start")({ systemPrompt: "base prompt" }, host.ctx);
137
+ }
138
+
139
+ function switchTool(host) {
140
+ return host.tools.find((tool) => tool.name === "freeflow_switch_profile");
141
+ }
142
+
143
+ function runtimeMessages(host) {
144
+ return host.entries.filter((entry) => entry.customType === "freeflow-cognitive-routing-model-state");
145
+ }
146
+
147
+ test("normal Pi activates and switches Cognitive Routing through official APIs", async () => {
148
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-pi-normal-lifecycle-"));
149
+ const host = createNormalPiHost();
150
+ host.ctx.cwd = cwd;
151
+ try {
152
+ await configure(cwd);
153
+ const before = await start(host);
154
+
155
+ assert.match(before.systemPrompt, /## Cognitive Routing Cue/);
156
+ assert.equal(host.ctx.model.id, "reasoning");
157
+ assert.equal(host.ctx.thinkingLevel, "max");
158
+ assert.ok(host.pi.getActiveTools().includes("freeflow_switch_profile"));
159
+
160
+ const tool = switchTool(host);
161
+ assert.ok(tool);
162
+ assert.equal(tool.executionMode, "sequential");
163
+ const result = await tool.execute(
164
+ "normal-pi-switch",
165
+ { target: "standard", reason: "Use Standard for bounded execution." },
166
+ undefined,
167
+ undefined,
168
+ host.ctx,
169
+ );
170
+
171
+ assert.deepEqual(result.details.result, {
172
+ status: "active",
173
+ changed: true,
174
+ from: "reasoning",
175
+ to: "standard",
176
+ profile: "standard",
177
+ });
178
+ assert.equal(host.ctx.model.id, "standard");
179
+ assert.equal(host.ctx.thinkingLevel, "high");
180
+ assert.equal(runtimeMessages(host).at(-1).data.status, "applied");
181
+ } finally {
182
+ await rm(cwd, { recursive: true, force: true });
183
+ }
184
+ });
185
+
186
+ test("normal Pi ignores model events emitted by Freeflow-owned transitions", async () => {
187
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-pi-normal-internal-events-"));
188
+ const host = createNormalPiHost({ emitNativeEvents: true });
189
+ host.ctx.cwd = cwd;
190
+ try {
191
+ await configure(cwd);
192
+ const before = await start(host);
193
+
194
+ assert.equal(
195
+ host.entries.some((entry) => entry.customType === "freeflow-cognitive-routing-inactive"),
196
+ false,
197
+ );
198
+ assert.match(before.systemPrompt, /## Cognitive Routing Cue/);
199
+ assert.equal(host.ctx.model.id, "reasoning");
200
+ assert.equal(host.ctx.thinkingLevel, "max");
201
+ } finally {
202
+ await rm(cwd, { recursive: true, force: true });
203
+ }
204
+ });
205
+
206
+ test("normal Pi no-op profile switches preserve one runtime-state message", async () => {
207
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-pi-normal-no-op-"));
208
+ const host = createNormalPiHost();
209
+ host.ctx.cwd = cwd;
210
+ try {
211
+ await configure(cwd);
212
+ await start(host);
213
+ const contextHandler = host.handlers.get("context");
214
+ const first = await contextHandler({ messages: [] }, host.ctx);
215
+ const tool = switchTool(host);
216
+ const result = await tool.execute(
217
+ "normal-pi-no-op",
218
+ { target: "reasoning", reason: "Remain on the current profile." },
219
+ undefined,
220
+ undefined,
221
+ host.ctx,
222
+ );
223
+ assert.equal(result.details.result.changed, false);
224
+
225
+ await host.handlers.get("tool_result")({ type: "tool_result", toolName: "freeflow_switch_profile" }, host.ctx);
226
+ assert.equal(await contextHandler({ messages: first.messages }, host.ctx), undefined);
227
+ assert.equal(first.messages.filter((message) => message.customType === "freeflow-runtime-state").length, 1);
228
+ } finally {
229
+ await rm(cwd, { recursive: true, force: true });
230
+ }
231
+ });
232
+
233
+ test("normal Pi supports manual and automatic profile shortcuts", async () => {
234
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-pi-normal-shortcuts-"));
235
+ const host = createNormalPiHost();
236
+ host.ctx.cwd = cwd;
237
+ try {
238
+ await configure(cwd);
239
+ await start(host);
240
+ const manual = host.shortcuts.find(({ shortcut }) => shortcut === "ctrl+shift+r");
241
+ const automatic = host.shortcuts.find(({ shortcut }) => shortcut === "ctrl+shift+a");
242
+ assert.ok(manual);
243
+ assert.ok(automatic);
244
+
245
+ await manual.definition.handler(host.ctx);
246
+ assert.equal(host.ctx.model.id, "standard");
247
+ assert.match(host.statuses.at(-1), /standard · manual hold/);
248
+
249
+ await automatic.definition.handler(host.ctx);
250
+ assert.equal(host.ctx.model.id, "reasoning");
251
+ assert.equal(host.ctx.thinkingLevel, "max");
252
+ assert.match(host.statuses.at(-1), /reasoning · automatic/);
253
+ } finally {
254
+ await rm(cwd, { recursive: true, force: true });
255
+ }
256
+ });
257
+
258
+ test("normal Pi disables routing after a native model override and can reactivate explicitly", async () => {
259
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-pi-normal-native-override-"));
260
+ const host = createNormalPiHost();
261
+ host.ctx.cwd = cwd;
262
+ try {
263
+ await configure(cwd);
264
+ await start(host);
265
+ const previousModel = host.ctx.model;
266
+ const selectedModel = host.models.get("faux/standard");
267
+ await host.pi.setModel(selectedModel);
268
+ host.pi.setThinkingLevel("high");
269
+ await host.handlers.get("model_select")(
270
+ { type: "model_select", model: selectedModel, previousModel, source: "set" },
271
+ host.ctx,
272
+ );
273
+
274
+ assert.equal(host.pi.getActiveTools().includes("freeflow_switch_profile"), false);
275
+ assert.ok(host.entries.some((entry) => entry.customType === "freeflow-cognitive-routing-inactive"));
276
+ assert.match(host.statuses.at(-1), /cognitive/);
277
+
278
+ const automatic = host.shortcuts.find(({ shortcut }) => shortcut === "ctrl+shift+a");
279
+ await automatic.definition.handler(host.ctx);
280
+ assert.equal(host.ctx.model.id, "standard");
281
+ assert.equal(host.ctx.thinkingLevel, "high");
282
+ assert.equal(host.pi.getActiveTools().includes("freeflow_switch_profile"), true);
283
+ } finally {
284
+ await rm(cwd, { recursive: true, force: true });
285
+ }
286
+ });
287
+
288
+ test("normal Pi honors explicit startup selection and supports explicit reactivation", async () => {
289
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-pi-normal-startup-override-"));
290
+ const originalArgv = process.argv;
291
+ const host = createNormalPiHost();
292
+ host.ctx.cwd = cwd;
293
+ try {
294
+ await configure(cwd);
295
+ process.argv = [originalArgv[0], originalArgv[1], "--model", "faux/return"];
296
+ freeflowExtension(host.pi);
297
+ await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, host.ctx);
298
+ const before = await host.handlers.get("before_agent_start")({ systemPrompt: "base prompt" }, host.ctx);
299
+
300
+ assert.equal(host.ctx.model.id, "return");
301
+ assert.equal(host.ctx.thinkingLevel, "medium");
302
+ assert.ok(host.entries.some((entry) => entry.customType === "freeflow-cognitive-routing-inactive"));
303
+ assert.equal(host.pi.getActiveTools().includes("freeflow_switch_profile"), false);
304
+ assert.doesNotMatch(before.systemPrompt, /## Cognitive Routing Cue/);
305
+
306
+ const command = host.commands.find(({ name }) => name === "freeflow");
307
+ await command.definition.handler("profile standard", host.ctx);
308
+ assert.equal(host.ctx.model.id, "standard");
309
+ assert.equal(host.ctx.thinkingLevel, "high");
310
+ } finally {
311
+ process.argv = originalArgv;
312
+ await rm(cwd, { recursive: true, force: true });
313
+ }
314
+ });
315
+
316
+ test("normal Pi uses documented startup arguments instead of host provenance", async () => {
317
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-pi-normal-provenance-"));
318
+ const originalArgv = process.argv;
319
+ const host = createNormalPiHost();
320
+ host.ctx.cwd = cwd;
321
+ host.ctx.modelStateProvenance = { explicitModel: true, explicitThinking: true };
322
+ try {
323
+ await configure(cwd);
324
+ process.argv = [originalArgv[0], originalArgv[1]];
325
+ const before = await start(host);
326
+
327
+ assert.equal(host.ctx.model.id, "reasoning");
328
+ assert.equal(host.ctx.thinkingLevel, "max");
329
+ assert.match(before.systemPrompt, /## Cognitive Routing Cue/);
330
+ } finally {
331
+ process.argv = originalArgv;
332
+ await rm(cwd, { recursive: true, force: true });
333
+ }
334
+ });
335
+
336
+ test("normal Pi disables routing after a native thinking-level override", async () => {
337
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-pi-normal-native-thinking-"));
338
+ const host = createNormalPiHost();
339
+ host.ctx.cwd = cwd;
340
+ try {
341
+ await configure(cwd);
342
+ await start(host);
343
+ const previousLevel = host.ctx.thinkingLevel;
344
+ host.pi.setThinkingLevel("low");
345
+ await host.handlers.get("thinking_level_select")(
346
+ { type: "thinking_level_select", level: "low", previousLevel },
347
+ host.ctx,
348
+ );
349
+
350
+ assert.equal(host.ctx.model.id, "reasoning");
351
+ assert.equal(host.ctx.thinkingLevel, "low");
352
+ assert.equal(host.pi.getActiveTools().includes("freeflow_switch_profile"), false);
353
+ assert.ok(host.entries.some((entry) => entry.customType === "freeflow-cognitive-routing-inactive"));
354
+ } finally {
355
+ await rm(cwd, { recursive: true, force: true });
356
+ }
357
+ });
358
+
359
+ test("normal Pi fails closed when native inactivity cannot be persisted", async () => {
360
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-pi-normal-inactive-persistence-"));
361
+ const host = createNormalPiHost({ failInactivePersistence: true });
362
+ host.ctx.cwd = cwd;
363
+ try {
364
+ await configure(cwd);
365
+ await start(host);
366
+ const previousModel = host.ctx.model;
367
+ const selectedModel = host.models.get("faux/standard");
368
+ await host.pi.setModel(selectedModel);
369
+ host.pi.setThinkingLevel("high");
370
+ await host.handlers.get("model_select")(
371
+ { type: "model_select", model: selectedModel, previousModel, source: "set" },
372
+ host.ctx,
373
+ );
374
+
375
+ assert.equal(host.pi.getActiveTools().includes("freeflow_switch_profile"), false);
376
+ assert.equal(
377
+ host.entries.some((entry) => entry.customType === "freeflow-cognitive-routing-inactive"),
378
+ false,
379
+ );
380
+ assert.match(host.statuses.at(-1), /cognitive blocked/);
381
+ } finally {
382
+ await rm(cwd, { recursive: true, force: true });
383
+ }
384
+ });
385
+
386
+ test("normal Pi reload restores the active semantic profile", async () => {
387
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-pi-normal-reload-"));
388
+ const host = createNormalPiHost();
389
+ host.ctx.cwd = cwd;
390
+ try {
391
+ await configure(cwd);
392
+ await start(host);
393
+ const tool = switchTool(host);
394
+ await tool.execute(
395
+ "normal-pi-reload-switch",
396
+ { target: "standard", reason: "Use Standard for bounded execution." },
397
+ undefined,
398
+ undefined,
399
+ host.ctx,
400
+ );
401
+ await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "reload" }, host.ctx);
402
+ assert.equal(host.ctx.model.id, "return");
403
+ assert.equal(host.ctx.thinkingLevel, "medium");
404
+
405
+ freeflowExtension(host.pi);
406
+ await host.handlers.get("session_start")({ type: "session_start", reason: "reload" }, host.ctx);
407
+ await host.handlers.get("before_agent_start")({ systemPrompt: "base prompt" }, host.ctx);
408
+ assert.equal(host.ctx.model.id, "standard");
409
+ assert.equal(host.ctx.thinkingLevel, "high");
410
+ } finally {
411
+ await rm(cwd, { recursive: true, force: true });
412
+ }
413
+ });
414
+
415
+ test("normal Pi preserves Cognitive Routing skill and runtime surface without Conversation routing", async () => {
416
+ const cwd = await mkdtemp(join(tmpdir(), "freeflow-pi-normal-surface-"));
417
+ const host = createNormalPiHost();
418
+ host.ctx.cwd = cwd;
419
+ try {
420
+ await configure(cwd);
421
+ const before = await start(host);
422
+ assert.match(before.systemPrompt, /## Cognitive Routing Cue/);
423
+ assert.doesNotMatch(before.systemPrompt, /Conversation(?: Route| profile| selects)/);
424
+ const resources = await host.handlers.get("resources_discover")({}, host.ctx);
425
+ const routingSkillPath = resources.skillPaths.find((path) => path.endsWith("cognitive-routing/SKILL.md"));
426
+ assert.ok(routingSkillPath);
427
+ const state = await host.handlers.get("context")({ messages: [] }, host.ctx);
428
+ assert.match(state.messages.at(-1).content, /Control: `automatic`/);
429
+ assert.match(state.messages.at(-1).content, /Profile: `reasoning`/);
430
+ assert.doesNotMatch(state.messages.at(-1).content, /Conversation:/);
431
+ } finally {
432
+ await rm(cwd, { recursive: true, force: true });
433
+ }
434
+ });
@@ -0,0 +1,174 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import {
4
+ createPiSessionModelStateControl,
5
+ PI_SESSION_MODEL_STATE_ENTRY,
6
+ } from "../../dist/cognitive-routing/pi-session-control.js";
7
+
8
+ const fromPair = {
9
+ provider: "faux",
10
+ modelId: "standard",
11
+ thinkingLevel: "medium",
12
+ };
13
+ const targetPair = {
14
+ provider: "faux",
15
+ modelId: "reasoning",
16
+ thinkingLevel: "high",
17
+ };
18
+
19
+ function createHost({ failTargetThinking = false, failRollbackModel = false, failCommitPersistence = false } = {}) {
20
+ let currentModel = { provider: fromPair.provider, id: fromPair.modelId };
21
+ let currentThinkingLevel = fromPair.thinkingLevel;
22
+ const entries = [];
23
+ const calls = [];
24
+ const models = new Map([
25
+ ["faux/standard", currentModel],
26
+ ["faux/reasoning", { provider: targetPair.provider, id: targetPair.modelId }],
27
+ ]);
28
+
29
+ const pi = {
30
+ async setModel(model) {
31
+ calls.push(["setModel", model.provider, model.id]);
32
+ if (failRollbackModel && model.id === fromPair.modelId) return false;
33
+ currentModel = model;
34
+ return true;
35
+ },
36
+ setThinkingLevel(level) {
37
+ calls.push(["setThinkingLevel", level]);
38
+ currentThinkingLevel = level;
39
+ if (failTargetThinking && level === targetPair.thinkingLevel) {
40
+ throw new Error("simulated target thinking failure");
41
+ }
42
+ },
43
+ appendEntry(customType, data) {
44
+ calls.push(["appendEntry", customType, data]);
45
+ if (failCommitPersistence && data?.phase === "committed") {
46
+ throw new Error("simulated commit persistence failure");
47
+ }
48
+ entries.push({ type: "custom", customType, data });
49
+ },
50
+ };
51
+ const ctx = {
52
+ get model() {
53
+ return currentModel;
54
+ },
55
+ get thinkingLevel() {
56
+ return currentThinkingLevel;
57
+ },
58
+ modelRegistry: {
59
+ find(provider, modelId) {
60
+ return models.get(`${provider}/${modelId}`);
61
+ },
62
+ },
63
+ };
64
+
65
+ return { pi, ctx, entries, calls };
66
+ }
67
+
68
+ test("applies and commits a complete Pi session model-state transition", async () => {
69
+ const host = createHost();
70
+ const control = createPiSessionModelStateControl(host);
71
+ const acquisition = await control.acquireModelStateControl({ label: "Cognitive Routing" });
72
+
73
+ assert.equal(acquisition.status, "acquired");
74
+ const result = await acquisition.lease.setState({ ...targetPair, correlationId: "correlation-1" });
75
+
76
+ assert.deepEqual(result, { status: "applied" });
77
+ assert.deepEqual(
78
+ host.calls.map(([name]) => name),
79
+ ["appendEntry", "setModel", "setThinkingLevel", "appendEntry"],
80
+ );
81
+ assert.deepEqual(host.entries, [
82
+ {
83
+ type: "custom",
84
+ customType: PI_SESSION_MODEL_STATE_ENTRY,
85
+ data: {
86
+ version: 1,
87
+ phase: "prepared",
88
+ status: "prepared",
89
+ correlationId: "correlation-1",
90
+ fromPair,
91
+ target: targetPair,
92
+ origin: { source: "pi", operation: "session-model-state-control" },
93
+ },
94
+ },
95
+ {
96
+ type: "custom",
97
+ customType: PI_SESSION_MODEL_STATE_ENTRY,
98
+ data: {
99
+ version: 1,
100
+ phase: "committed",
101
+ status: "applied",
102
+ correlationId: "correlation-1",
103
+ fromPair,
104
+ target: targetPair,
105
+ origin: { source: "pi", operation: "session-model-state-control" },
106
+ },
107
+ },
108
+ ]);
109
+ assert.deepEqual(
110
+ { provider: host.ctx.model.provider, modelId: host.ctx.model.id, thinkingLevel: host.ctx.thinkingLevel },
111
+ targetPair,
112
+ );
113
+
114
+ await acquisition.lease.release();
115
+ });
116
+
117
+ test("rolls back a partially applied target and records an aborted transition", async () => {
118
+ const host = createHost({ failTargetThinking: true });
119
+ const control = createPiSessionModelStateControl(host);
120
+ const acquisition = await control.acquireModelStateControl({ label: "Cognitive Routing" });
121
+
122
+ assert.equal(acquisition.status, "acquired");
123
+ const result = await acquisition.lease.setState({ ...targetPair, correlationId: "correlation-rollback" });
124
+
125
+ assert.deepEqual(result, { status: "rejected" });
126
+ assert.deepEqual(
127
+ host.calls.map(([name]) => name),
128
+ ["appendEntry", "setModel", "setThinkingLevel", "setModel", "setThinkingLevel", "appendEntry"],
129
+ );
130
+ assert.deepEqual(
131
+ { provider: host.ctx.model.provider, modelId: host.ctx.model.id, thinkingLevel: host.ctx.thinkingLevel },
132
+ fromPair,
133
+ );
134
+ assert.equal(host.entries.at(-1).data.failure, "thinking-level-change-failed");
135
+
136
+ await acquisition.lease.release();
137
+ });
138
+
139
+ test("fails closed when rollback cannot restore the captured pair", async () => {
140
+ const host = createHost({ failTargetThinking: true, failRollbackModel: true });
141
+ const control = createPiSessionModelStateControl(host);
142
+ const acquisition = await control.acquireModelStateControl({ label: "Cognitive Routing" });
143
+
144
+ assert.equal(acquisition.status, "acquired");
145
+ const result = await acquisition.lease.setState({ ...targetPair, correlationId: "correlation-rollback-failed" });
146
+
147
+ assert.deepEqual(result, { status: "rejected" });
148
+ assert.deepEqual(
149
+ { provider: host.ctx.model.provider, modelId: host.ctx.model.id, thinkingLevel: host.ctx.thinkingLevel },
150
+ targetPair,
151
+ );
152
+ assert.equal(host.entries.at(-1).data.status, "rollback-failed");
153
+ assert.equal(host.entries.at(-1).data.failure, "rollback-failed");
154
+
155
+ await acquisition.lease.release();
156
+ });
157
+
158
+ test("restores the baseline when committed-marker persistence fails", async () => {
159
+ const host = createHost({ failCommitPersistence: true });
160
+ const control = createPiSessionModelStateControl(host);
161
+ const acquisition = await control.acquireModelStateControl({ label: "Cognitive Routing" });
162
+
163
+ assert.equal(acquisition.status, "acquired");
164
+ const result = await acquisition.lease.setState({ ...targetPair, correlationId: "correlation-commit-failed" });
165
+
166
+ assert.deepEqual(result, { status: "rejected" });
167
+ assert.deepEqual(
168
+ { provider: host.ctx.model.provider, modelId: host.ctx.model.id, thinkingLevel: host.ctx.thinkingLevel },
169
+ fromPair,
170
+ );
171
+ assert.equal(host.entries.at(-1).data.failure, "commit-persistence-failed");
172
+
173
+ await acquisition.lease.release();
174
+ });
@@ -0,0 +1,91 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import { projectCognitiveRoutingHistory } from "../../dist/cognitive-routing/history.js";
4
+ import { PI_SESSION_MODEL_STATE_ENTRY } from "../../dist/cognitive-routing/pi-session-control.js";
5
+
6
+ const standard = { provider: "faux", modelId: "standard", thinkingLevel: "high" };
7
+ const reasoning = { provider: "faux", modelId: "reasoning", thinkingLevel: "max" };
8
+
9
+ const intent = {
10
+ version: 2,
11
+ phase: "prepared",
12
+ kind: "profile",
13
+ control: "automatic",
14
+ source: "agent",
15
+ mechanism: "agent-tool",
16
+ decisionCorrelationId: "correlation-1",
17
+ fromPair: standard,
18
+ fromProfile: "standard",
19
+ epoch: "epoch-1",
20
+ correlationId: "correlation-1",
21
+ profile: "reasoning",
22
+ target: reasoning,
23
+ returnTarget: { provider: "faux", modelId: "return", thinkingLevel: "medium" },
24
+ reason: "Need deeper analysis.",
25
+ };
26
+
27
+ test("projects a committed Pi session marker as the completed transition", () => {
28
+ const entries = [
29
+ { id: "root", type: "message", parentId: null, timestamp: "2026-08-25T12:00:00.000Z" },
30
+ {
31
+ id: "intent-1",
32
+ type: "custom",
33
+ parentId: "root",
34
+ timestamp: "2026-08-25T12:00:01.000Z",
35
+ customType: "freeflow-cognitive-routing-intent",
36
+ data: intent,
37
+ },
38
+ {
39
+ id: "commit-1",
40
+ type: "custom",
41
+ parentId: "intent-1",
42
+ timestamp: "2026-08-25T12:00:02.000Z",
43
+ customType: PI_SESSION_MODEL_STATE_ENTRY,
44
+ data: {
45
+ version: 1,
46
+ phase: "committed",
47
+ status: "applied",
48
+ correlationId: "correlation-1",
49
+ fromPair: standard,
50
+ target: reasoning,
51
+ origin: { source: "pi", operation: "session-model-state-control" },
52
+ },
53
+ },
54
+ ];
55
+
56
+ const result = projectCognitiveRoutingHistory(entries, {
57
+ branchEntries: entries,
58
+ current: { control: "automatic", profile: "reasoning" },
59
+ });
60
+
61
+ assert.deepEqual(result.events, [
62
+ {
63
+ id: "intent:intent-1",
64
+ timestamp: "2026-08-25T12:00:02.000Z",
65
+ jsonlPosition: 2,
66
+ entryId: "commit-1",
67
+ parentId: "intent-1",
68
+ branchAnchor: "commit-1",
69
+ classification: "semantic-switch",
70
+ decisionSource: "agent",
71
+ mechanism: "agent-tool",
72
+ outcome: "completed",
73
+ changed: true,
74
+ integrity: "valid",
75
+ from: "standard",
76
+ to: "reasoning",
77
+ control: "automatic",
78
+ reason: "Need deeper analysis.",
79
+ epoch: "epoch-1",
80
+ correlationId: "correlation-1",
81
+ decisionCorrelationId: "correlation-1",
82
+ hostOrigin: { source: "pi", operation: "session-model-state-control" },
83
+ },
84
+ ]);
85
+ assert.deepEqual(result.summary, {
86
+ latestSemanticEventId: "intent:intent-1",
87
+ latestCompletedEventId: "intent:intent-1",
88
+ unresolvedCount: 0,
89
+ anomalyCount: 0,
90
+ });
91
+ });