@hassangameryt/freeflow 0.7.0 → 0.7.2
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.
- package/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +2 -1
- package/.codex-plugin/plugin.json +2 -2
- package/.cursor-plugin/plugin.json +25 -0
- package/README.md +57 -26
- package/capabilities/cognitive-routing/SKILL.md +94 -311
- package/com.github.copilot/hooks/hooks.json +11 -0
- package/command-surface.json +7 -1
- package/gemini-extension.json +5 -0
- package/hooks/adapters/claude-session-start.mjs +16 -0
- package/hooks/adapters/codex-session-start.mjs +16 -0
- package/hooks/adapters/copilot-session-start.mjs +13 -0
- package/hooks/adapters/cursor-session-start.mjs +13 -0
- package/hooks/adapters/gemini-session-start.mjs +15 -0
- package/hooks/claude/hooks.json +18 -0
- package/hooks/codex/hooks.json +18 -0
- package/hooks/cursor/hooks.json +9 -0
- package/hooks/freeflow-runtime-context.mjs +3 -215
- package/hooks/hooks.json +4 -5
- package/hooks/shared/runtime-context.mjs +210 -0
- package/hooks/tests/check-host-adapters.test.mjs +82 -0
- package/hooks/tests/check-runtime-context-hook.sh +34 -3
- package/package.json +20 -6
- package/pi-extension/dist/cognitive-routing-v2/budget.js +42 -0
- package/pi-extension/dist/cognitive-routing-v2/config.js +120 -0
- package/pi-extension/dist/cognitive-routing-v2/projection.js +321 -0
- package/pi-extension/dist/cognitive-routing-v2/render.js +231 -0
- package/pi-extension/dist/cognitive-routing-v2/runtime.js +1585 -0
- package/pi-extension/dist/cognitive-routing-v2/schemas.js +107 -0
- package/pi-extension/dist/cognitive-routing-v2/state.js +612 -0
- package/pi-extension/dist/cognitive-routing-v2/tools.js +91 -0
- package/pi-extension/dist/cognitive-routing-v2/types.js +44 -0
- package/pi-extension/dist/freeflow-context/tool.js +5 -0
- package/pi-extension/dist/index.js +279 -624
- package/pi-extension/dist/runtime/runtime-context.js +63 -13
- package/pi-extension/dist/session-sources/events.js +195 -0
- package/pi-extension/dist/session-sources/read-only-session.js +148 -0
- package/pi-extension/dist/session-sources/sources.js +233 -0
- package/pi-extension/dist/settings/settings-ui.js +44 -117
- package/pi-extension/src/cognitive-routing-v2/budget.ts +42 -0
- package/pi-extension/src/cognitive-routing-v2/config.ts +143 -0
- package/pi-extension/src/cognitive-routing-v2/projection.ts +365 -0
- package/pi-extension/src/cognitive-routing-v2/render.ts +256 -0
- package/pi-extension/src/cognitive-routing-v2/runtime.ts +1634 -0
- package/pi-extension/src/cognitive-routing-v2/schemas.ts +109 -0
- package/pi-extension/src/cognitive-routing-v2/state.ts +622 -0
- package/pi-extension/src/cognitive-routing-v2/tools.ts +93 -0
- package/pi-extension/src/cognitive-routing-v2/types.ts +222 -0
- package/pi-extension/src/freeflow-context/tool.ts +5 -0
- package/pi-extension/src/index.ts +285 -674
- package/pi-extension/src/runtime/runtime-context.ts +75 -14
- package/pi-extension/src/session-sources/events.ts +208 -0
- package/pi-extension/src/session-sources/read-only-session.ts +170 -0
- package/pi-extension/src/session-sources/sources.ts +263 -0
- package/pi-extension/src/settings/settings-ui.ts +54 -131
- package/pi-extension/tests/cognitive-routing-v2/communication.test.js +484 -0
- package/pi-extension/tests/cognitive-routing-v2/compaction-input.test.js +183 -0
- package/pi-extension/tests/cognitive-routing-v2/native.test.js +441 -0
- package/pi-extension/tests/cognitive-routing-v2/performance.test.js +87 -0
- package/pi-extension/tests/cognitive-routing-v2/persistence.test.js +245 -0
- package/pi-extension/tests/cognitive-routing-v2/projection.test.js +241 -0
- package/pi-extension/tests/cognitive-routing-v2/recovery.test.js +236 -0
- package/pi-extension/tests/cognitive-routing-v2/render.test.js +230 -0
- package/pi-extension/tests/cognitive-routing-v2/runtime.test.js +153 -0
- package/pi-extension/tests/cognitive-routing-v2/stabilization.test.js +521 -0
- package/pi-extension/tests/cognitive-routing-v2/state.test.js +275 -0
- package/pi-extension/tests/context-virtualization/extension.test.js +16 -0
- package/pi-extension/tests/fixtures/routing-native.js +188 -0
- package/pi-extension/tests/integration/mode-free-core.test.js +3 -2
- package/pi-extension/tests/integration/pi-extension.test.js +154 -37
- package/pi-extension/tests/integration/prompt-architecture.test.js +87 -6
- package/pi-extension/tests/integration/unaffected-regressions.test.js +77 -10
- package/plugin.json +33 -0
- package/runtime/prompts/cognitive-routing.md +4 -5
- package/runtime/prompts/core.md +51 -45
- package/runtime/prompts/interaction-contract.md +5 -5
- package/skills/action-selection/SKILL.md +57 -97
- package/skills/design-for-depth/SKILL.md +58 -193
- package/skills/design-for-depth/references/implementation-decisions.md +76 -0
- package/skills/diagnose-failure/SKILL.md +64 -196
- package/skills/discuss/SKILL.md +64 -123
- package/skills/discuss/references/discussion-continuity.md +7 -3
- package/skills/execute-work/SKILL.md +69 -88
- package/skills/execute-work/references/execute-work-edges.md +9 -3
- package/skills/handoff/SKILL.md +44 -54
- package/skills/handoff/references/handoff-templates.md +15 -6
- package/skills/review-artifact/SKILL.md +80 -127
- package/skills/review-artifact/references/independent-artifact-reviewer-contract.md +1 -1
- package/skills/review-work/SKILL.md +65 -115
- package/skills/review-work/references/independent-work-reviewer-contract.md +1 -1
- package/skills/simplify-code/SKILL.md +47 -100
- package/skills/track-work/SKILL.md +128 -119
- package/skills/track-work/references/working-record-format.md +14 -2
- package/skills/verify-work/SKILL.md +58 -62
- package/skills/verify-work/references/integration-evidence.md +4 -2
- package/skills/verify-work/references/test-design.md +89 -0
- package/skills/workflow/SKILL.md +73 -119
- package/skills/write-plan/SKILL.md +46 -77
- package/skills/write-plan/references/plan-shapes.md +5 -5
- package/skills/write-spec/SKILL.md +51 -63
- package/skills/write-spec/references/artifact-standards.md +2 -2
- package/skills/write-spec/references/decision-records.md +3 -2
- package/skills/write-spec/references/spec-shapes.md +15 -0
- package/pi-extension/dist/cognitive-routing/commands.js +0 -96
- package/pi-extension/dist/cognitive-routing/config.js +0 -232
- package/pi-extension/dist/cognitive-routing/controller.js +0 -1292
- package/pi-extension/dist/cognitive-routing/history.js +0 -451
- package/pi-extension/dist/cognitive-routing/host.js +0 -188
- package/pi-extension/dist/cognitive-routing/pi-routing-session.js +0 -414
- package/pi-extension/dist/cognitive-routing/pi-session-control.js +0 -320
- package/pi-extension/dist/cognitive-routing/runtime.js +0 -43
- package/pi-extension/dist/cognitive-routing/tool.js +0 -212
- package/pi-extension/dist/cognitive-routing/types.js +0 -6
- package/pi-extension/src/cognitive-routing/commands.ts +0 -129
- package/pi-extension/src/cognitive-routing/config.ts +0 -303
- package/pi-extension/src/cognitive-routing/controller.ts +0 -1628
- package/pi-extension/src/cognitive-routing/history.ts +0 -650
- package/pi-extension/src/cognitive-routing/host.ts +0 -266
- package/pi-extension/src/cognitive-routing/pi-routing-session.ts +0 -538
- package/pi-extension/src/cognitive-routing/pi-session-control.ts +0 -444
- package/pi-extension/src/cognitive-routing/runtime.ts +0 -57
- package/pi-extension/src/cognitive-routing/tool.ts +0 -273
- package/pi-extension/src/cognitive-routing/types.ts +0 -89
- package/pi-extension/tests/cognitive-routing/commands.test.js +0 -134
- package/pi-extension/tests/cognitive-routing/controller.test.js +0 -1128
- package/pi-extension/tests/cognitive-routing/extension-lifecycle.test.js +0 -1142
- package/pi-extension/tests/cognitive-routing/history.test.js +0 -274
- package/pi-extension/tests/cognitive-routing/host-identity.test.js +0 -63
- package/pi-extension/tests/cognitive-routing/pi-normal-lifecycle.test.js +0 -434
- package/pi-extension/tests/cognitive-routing/pi-session-control.test.js +0 -174
- package/pi-extension/tests/cognitive-routing/pi-session-history.test.js +0 -91
- package/pi-extension/tests/cognitive-routing/profile-resolution.test.js +0 -279
- package/pi-extension/tests/cognitive-routing/tool.test.js +0 -230
- package/skills/tdd/SKILL.md +0 -143
- package/skills/tdd/references/test-design.md +0 -96
- /package/pi-extension/tests/{cognitive-routing/host-fixture.js → fixtures/pi-host.js} +0 -0
|
@@ -1,1142 +0,0 @@
|
|
|
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 establish automatic Reasoning", 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
|
-
await host.handlers.get("before_agent_start")({ systemPrompt: "base prompt" }, ctx);
|
|
161
|
-
const cycle = host.shortcuts.find(({ shortcut }) => shortcut === "ctrl+shift+r");
|
|
162
|
-
const automatic = host.shortcuts.find(({ shortcut }) => shortcut === "ctrl+shift+a");
|
|
163
|
-
assert.ok(cycle);
|
|
164
|
-
assert.ok(automatic);
|
|
165
|
-
|
|
166
|
-
await cycle.definition.handler(ctx);
|
|
167
|
-
assert.equal(host.entries.filter((entry) => entry.modelId).at(-1).modelId, "standard");
|
|
168
|
-
|
|
169
|
-
await automatic.definition.handler(ctx);
|
|
170
|
-
assert.equal(host.entries.filter((entry) => entry.modelId).at(-1).modelId, "reasoning");
|
|
171
|
-
const modelEntriesAfterAutomatic = host.entries.filter((entry) => entry.modelId).length;
|
|
172
|
-
|
|
173
|
-
await automatic.definition.handler(ctx);
|
|
174
|
-
assert.equal(host.entries.filter((entry) => entry.modelId).at(-1).modelId, "reasoning");
|
|
175
|
-
assert.equal(host.entries.filter((entry) => entry.modelId).length, modelEntriesAfterAutomatic);
|
|
176
|
-
|
|
177
|
-
const automaticIntent = host.entries
|
|
178
|
-
.filter((entry) => entry.customType === "freeflow-cognitive-routing-intent")
|
|
179
|
-
.at(-1);
|
|
180
|
-
assert.equal(automaticIntent.data.source, "user");
|
|
181
|
-
assert.equal(automaticIntent.data.mechanism, "profile-shortcut");
|
|
182
|
-
assert.equal(automaticIntent.data.reason, "automatic control selects the Reasoning profile");
|
|
183
|
-
} finally {
|
|
184
|
-
await rm(cwd, { recursive: true, force: true });
|
|
185
|
-
}
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
test("explicit startup model provenance suppresses lifecycle activation", async () => {
|
|
189
|
-
const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-suppressed-"));
|
|
190
|
-
await mkdir(join(cwd, ".freeflow"));
|
|
191
|
-
await writeFile(
|
|
192
|
-
join(cwd, ".freeflow", "config.json"),
|
|
193
|
-
JSON.stringify({
|
|
194
|
-
cognitiveRouting: {
|
|
195
|
-
enabled: true,
|
|
196
|
-
profiles: {
|
|
197
|
-
standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
|
|
198
|
-
reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
|
|
199
|
-
},
|
|
200
|
-
},
|
|
201
|
-
}),
|
|
202
|
-
);
|
|
203
|
-
const host = createExtensionHost();
|
|
204
|
-
const ctx = createContext(cwd, host);
|
|
205
|
-
ctx.modelStateProvenance = { explicitModel: true };
|
|
206
|
-
try {
|
|
207
|
-
await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
|
|
208
|
-
assert.deepEqual(host.operations, []);
|
|
209
|
-
assert.deepEqual(host.entries, []);
|
|
210
|
-
assert.deepEqual(host.state, { model: { provider: "faux", id: "return" }, thinkingLevel: "medium" });
|
|
211
|
-
|
|
212
|
-
const before = await host.handlers.get("before_agent_start")({ systemPrompt: "base prompt" }, ctx);
|
|
213
|
-
assert.equal(before.message, undefined);
|
|
214
|
-
assert.doesNotMatch(before.systemPrompt, /## Cognitive Routing Cue/);
|
|
215
|
-
const resources = await host.handlers.get("resources_discover")({ cwd }, ctx);
|
|
216
|
-
assert.ok(!resources.skillPaths.some((path) => path.endsWith("/capabilities/cognitive-routing/SKILL.md")));
|
|
217
|
-
assert.ok(!host.activeToolNames().includes("freeflow_switch_profile"));
|
|
218
|
-
const providerContext = await host.handlers.get("context")({ messages: [] }, ctx);
|
|
219
|
-
assert.match(providerContext.messages.at(-1).content, /Cognitive Routing: unavailable/);
|
|
220
|
-
assert.match(providerContext.messages.at(-1).content, /Control: `unavailable`/);
|
|
221
|
-
assert.match(providerContext.messages.at(-1).content, /Profile: `unavailable`/);
|
|
222
|
-
} finally {
|
|
223
|
-
await rm(cwd, { recursive: true, force: true });
|
|
224
|
-
}
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
test("Cognitive Routing activation failure hides model-facing routing surfaces", async () => {
|
|
228
|
-
const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-activation-failure-"));
|
|
229
|
-
await mkdir(join(cwd, ".freeflow"));
|
|
230
|
-
await writeFile(
|
|
231
|
-
join(cwd, ".freeflow", "config.json"),
|
|
232
|
-
JSON.stringify({
|
|
233
|
-
cognitiveRouting: {
|
|
234
|
-
enabled: true,
|
|
235
|
-
profiles: {
|
|
236
|
-
standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
|
|
237
|
-
reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
|
|
238
|
-
},
|
|
239
|
-
},
|
|
240
|
-
}),
|
|
241
|
-
);
|
|
242
|
-
const host = createExtensionHost({ rejectAcquire: true });
|
|
243
|
-
const ctx = createContext(cwd, host);
|
|
244
|
-
try {
|
|
245
|
-
await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
|
|
246
|
-
const before = await host.handlers.get("before_agent_start")({ systemPrompt: "base prompt" }, ctx);
|
|
247
|
-
assert.equal(before.message, undefined);
|
|
248
|
-
assert.doesNotMatch(before.systemPrompt, /## Cognitive Routing Cue/);
|
|
249
|
-
const resources = await host.handlers.get("resources_discover")({ cwd }, ctx);
|
|
250
|
-
assert.ok(!resources.skillPaths.some((path) => path.endsWith("/capabilities/cognitive-routing/SKILL.md")));
|
|
251
|
-
assert.ok(!host.activeToolNames().includes("freeflow_switch_profile"));
|
|
252
|
-
const providerContext = await host.handlers.get("context")({ messages: [] }, ctx);
|
|
253
|
-
assert.match(providerContext.messages.at(-1).content, /Cognitive Routing: unavailable/);
|
|
254
|
-
assert.match(providerContext.messages.at(-1).content, /Control: `unavailable`/);
|
|
255
|
-
assert.match(providerContext.messages.at(-1).content, /Profile: `unavailable`/);
|
|
256
|
-
} finally {
|
|
257
|
-
await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
|
|
258
|
-
await rm(cwd, { recursive: true, force: true });
|
|
259
|
-
}
|
|
260
|
-
});
|
|
261
|
-
|
|
262
|
-
test("Cognitive Routing retries activation after a transient failure", async () => {
|
|
263
|
-
const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-activation-retry-"));
|
|
264
|
-
await mkdir(join(cwd, ".freeflow"));
|
|
265
|
-
await writeFile(
|
|
266
|
-
join(cwd, ".freeflow", "config.json"),
|
|
267
|
-
JSON.stringify({
|
|
268
|
-
cognitiveRouting: {
|
|
269
|
-
enabled: true,
|
|
270
|
-
profiles: {
|
|
271
|
-
standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
|
|
272
|
-
reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
|
|
273
|
-
},
|
|
274
|
-
},
|
|
275
|
-
}),
|
|
276
|
-
);
|
|
277
|
-
const host = createExtensionHost({ rejectAcquire: true });
|
|
278
|
-
const ctx = createContext(cwd, host);
|
|
279
|
-
try {
|
|
280
|
-
await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
|
|
281
|
-
const first = await host.handlers.get("before_agent_start")({ systemPrompt: "base prompt" }, ctx);
|
|
282
|
-
assert.doesNotMatch(first.systemPrompt, /## Cognitive Routing Cue/);
|
|
283
|
-
host.setRejectAcquire(false);
|
|
284
|
-
const second = await host.handlers.get("before_agent_start")({ systemPrompt: "base prompt" }, ctx);
|
|
285
|
-
assert.match(second.systemPrompt, /## Cognitive Routing Cue/);
|
|
286
|
-
const providerContext = await host.handlers.get("context")({ messages: [] }, ctx);
|
|
287
|
-
assert.match(providerContext.messages.at(-1).content, /Cognitive Routing: active/);
|
|
288
|
-
assert.match(providerContext.messages.at(-1).content, /Control: `automatic`/);
|
|
289
|
-
assert.match(providerContext.messages.at(-1).content, /Profile: `reasoning`/);
|
|
290
|
-
assert.ok(host.operations.includes("setState"));
|
|
291
|
-
} finally {
|
|
292
|
-
await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
|
|
293
|
-
await rm(cwd, { recursive: true, force: true });
|
|
294
|
-
}
|
|
295
|
-
});
|
|
296
|
-
|
|
297
|
-
test("missing Cognitive Routing prompt prevents lifecycle activation", async () => {
|
|
298
|
-
const root = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-prompt-missing-"));
|
|
299
|
-
const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-prompt-missing-repo-"));
|
|
300
|
-
await mkdir(join(cwd, ".freeflow"));
|
|
301
|
-
await writeFile(
|
|
302
|
-
join(cwd, ".freeflow", "config.json"),
|
|
303
|
-
JSON.stringify({
|
|
304
|
-
cognitiveRouting: {
|
|
305
|
-
enabled: true,
|
|
306
|
-
profiles: {
|
|
307
|
-
standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
|
|
308
|
-
reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
|
|
309
|
-
},
|
|
310
|
-
},
|
|
311
|
-
}),
|
|
312
|
-
);
|
|
313
|
-
try {
|
|
314
|
-
await cp(join(process.cwd(), "pi-extension", "dist"), join(root, "pi-extension", "dist"), { recursive: true });
|
|
315
|
-
await cp(join(process.cwd(), "runtime", "prompts"), join(root, "runtime", "prompts"), { recursive: true });
|
|
316
|
-
await symlink(join(process.cwd(), "node_modules"), join(root, "node_modules"), "dir");
|
|
317
|
-
await rm(join(root, "runtime", "prompts", "cognitive-routing.md"));
|
|
318
|
-
const extension = (
|
|
319
|
-
await import(
|
|
320
|
-
`${pathToFileURL(join(root, "pi-extension", "dist", "index.js")).href}?missing-routing=${Date.now()}`
|
|
321
|
-
)
|
|
322
|
-
).default;
|
|
323
|
-
const host = createExtensionHost({}, extension);
|
|
324
|
-
const ctx = createContext(cwd, host);
|
|
325
|
-
await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
|
|
326
|
-
const before = await host.handlers.get("before_agent_start")({ systemPrompt: "base prompt" }, ctx);
|
|
327
|
-
|
|
328
|
-
assert.deepEqual(host.operations, []);
|
|
329
|
-
assert.doesNotMatch(before.systemPrompt, /## Cognitive Routing Cue/);
|
|
330
|
-
assert.ok(!host.activeToolNames().includes("freeflow_switch_profile"));
|
|
331
|
-
const providerContext = await host.handlers.get("context")({ messages: [] }, ctx);
|
|
332
|
-
assert.match(providerContext.messages.at(-1).content, /Cognitive Routing: unavailable/);
|
|
333
|
-
} finally {
|
|
334
|
-
await rm(root, { recursive: true, force: true });
|
|
335
|
-
await rm(cwd, { recursive: true, force: true });
|
|
336
|
-
}
|
|
337
|
-
});
|
|
338
|
-
|
|
339
|
-
test("new empty sessions do not persist Cognitive Routing state until the first prompt", async () => {
|
|
340
|
-
const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-lazy-start-"));
|
|
341
|
-
await mkdir(join(cwd, ".freeflow"));
|
|
342
|
-
await writeFile(
|
|
343
|
-
join(cwd, ".freeflow", "config.json"),
|
|
344
|
-
JSON.stringify({
|
|
345
|
-
cognitiveRouting: {
|
|
346
|
-
enabled: true,
|
|
347
|
-
profiles: {
|
|
348
|
-
standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
|
|
349
|
-
reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
|
|
350
|
-
},
|
|
351
|
-
},
|
|
352
|
-
}),
|
|
353
|
-
);
|
|
354
|
-
const host = createExtensionHost();
|
|
355
|
-
const ctx = createContext(cwd, host);
|
|
356
|
-
try {
|
|
357
|
-
await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
|
|
358
|
-
|
|
359
|
-
assert.deepEqual(host.operations, []);
|
|
360
|
-
assert.deepEqual(host.entries, []);
|
|
361
|
-
|
|
362
|
-
const beforeAgentStart = await host.handlers.get("before_agent_start")(
|
|
363
|
-
{ prompt: "hello", systemPrompt: "base prompt" },
|
|
364
|
-
ctx,
|
|
365
|
-
);
|
|
366
|
-
assert.deepEqual(host.operations.slice(0, 3), ["prepare", "acquire", "setState"]);
|
|
367
|
-
assert.equal(host.entries[0].customType, "freeflow-cognitive-routing-intent");
|
|
368
|
-
assert.match(beforeAgentStart.systemPrompt, /## Cognitive Routing Cue/);
|
|
369
|
-
assert.doesNotMatch(beforeAgentStart.systemPrompt, /^# Automatic Routing Kernel$/m);
|
|
370
|
-
assert.equal(beforeAgentStart.message, undefined);
|
|
371
|
-
} finally {
|
|
372
|
-
await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
|
|
373
|
-
await rm(cwd, { recursive: true, force: true });
|
|
374
|
-
}
|
|
375
|
-
});
|
|
376
|
-
|
|
377
|
-
test("new sessions honor each configured Cognitive Routing startup combination", async () => {
|
|
378
|
-
const combinations = [
|
|
379
|
-
["automatic", "standard", "reasoning"],
|
|
380
|
-
["automatic", "reasoning", "reasoning"],
|
|
381
|
-
["manual", "standard", "standard"],
|
|
382
|
-
["manual", "reasoning", "reasoning"],
|
|
383
|
-
];
|
|
384
|
-
|
|
385
|
-
for (const [control, profile, expectedProfile] of combinations) {
|
|
386
|
-
const cwd = await mkdtemp(join(tmpdir(), `freeflow-cognitive-routing-start-${control}-${profile}-`));
|
|
387
|
-
await mkdir(join(cwd, ".freeflow"));
|
|
388
|
-
await writeFile(
|
|
389
|
-
join(cwd, ".freeflow", "config.json"),
|
|
390
|
-
JSON.stringify({
|
|
391
|
-
cognitiveRouting: {
|
|
392
|
-
enabled: true,
|
|
393
|
-
sessionStart: { control, profile },
|
|
394
|
-
profiles: {
|
|
395
|
-
standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
|
|
396
|
-
reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
|
|
397
|
-
},
|
|
398
|
-
},
|
|
399
|
-
}),
|
|
400
|
-
);
|
|
401
|
-
|
|
402
|
-
const host = createExtensionHost();
|
|
403
|
-
const ctx = createContext(cwd, host);
|
|
404
|
-
try {
|
|
405
|
-
await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
|
|
406
|
-
await host.handlers.get("before_agent_start")({ prompt: "hello", systemPrompt: "base prompt" }, ctx);
|
|
407
|
-
|
|
408
|
-
assert.equal(host.entries.filter((entry) => entry.modelId).at(-1).modelId, expectedProfile);
|
|
409
|
-
const activation = host.entries.find(
|
|
410
|
-
(entry) => entry.customType === "freeflow-cognitive-routing-intent" && entry.data.kind === "activation",
|
|
411
|
-
);
|
|
412
|
-
assert.equal(activation.data.profile, expectedProfile);
|
|
413
|
-
assert.equal(activation.data.control, control);
|
|
414
|
-
} finally {
|
|
415
|
-
await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
|
|
416
|
-
await rm(cwd, { recursive: true, force: true });
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
});
|
|
420
|
-
|
|
421
|
-
test("editing session-start settings does not activate the current empty session", async () => {
|
|
422
|
-
const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-session-start-current-session-"));
|
|
423
|
-
await mkdir(join(cwd, ".freeflow"));
|
|
424
|
-
await writeFile(
|
|
425
|
-
join(cwd, ".freeflow", "config.json"),
|
|
426
|
-
JSON.stringify({
|
|
427
|
-
cognitiveRouting: {
|
|
428
|
-
enabled: true,
|
|
429
|
-
profiles: {
|
|
430
|
-
standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
|
|
431
|
-
reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
|
|
432
|
-
},
|
|
433
|
-
},
|
|
434
|
-
}),
|
|
435
|
-
);
|
|
436
|
-
|
|
437
|
-
const host = createExtensionHost();
|
|
438
|
-
const ctx = createContext(cwd, host);
|
|
439
|
-
ctx.mode = "tui";
|
|
440
|
-
ctx.hasUI = true;
|
|
441
|
-
ctx.isIdle = () => true;
|
|
442
|
-
ctx.ui.custom = async (factory) => {
|
|
443
|
-
let result;
|
|
444
|
-
const theme = {
|
|
445
|
-
fg(_color, text) {
|
|
446
|
-
return text;
|
|
447
|
-
},
|
|
448
|
-
bold(text) {
|
|
449
|
-
return text;
|
|
450
|
-
},
|
|
451
|
-
};
|
|
452
|
-
const component = factory({ requestRender() {} }, theme, {}, (value) => {
|
|
453
|
-
result = value;
|
|
454
|
-
});
|
|
455
|
-
component.handleInput("\u001b[B");
|
|
456
|
-
component.handleInput("\r");
|
|
457
|
-
for (let index = 0; index < 3; index += 1) component.handleInput("\u001b[B");
|
|
458
|
-
component.handleInput("\r");
|
|
459
|
-
component.handleInput("\u001b[B");
|
|
460
|
-
component.handleInput("\u001b[B");
|
|
461
|
-
component.handleInput("\r");
|
|
462
|
-
await component.waitForWrites();
|
|
463
|
-
component.handleInput("\u001b");
|
|
464
|
-
component.handleInput("\u001b");
|
|
465
|
-
return result;
|
|
466
|
-
};
|
|
467
|
-
|
|
468
|
-
try {
|
|
469
|
-
await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
|
|
470
|
-
host.operations.length = 0;
|
|
471
|
-
await host.commands.find((command) => command.name === "freeflow").definition.handler("settings", ctx);
|
|
472
|
-
|
|
473
|
-
assert.deepEqual(host.operations, []);
|
|
474
|
-
assert.deepEqual(host.state, { model: { provider: "faux", id: "return" }, thinkingLevel: "medium" });
|
|
475
|
-
assert.equal(host.entries.filter((entry) => entry.modelId).length, 0);
|
|
476
|
-
const saved = JSON.parse(await readFile(join(cwd, ".freeflow", "local.json"), "utf8"));
|
|
477
|
-
assert.equal(saved.cognitiveRouting.sessionStart.control, "manual");
|
|
478
|
-
} finally {
|
|
479
|
-
await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
|
|
480
|
-
await rm(cwd, { recursive: true, force: true });
|
|
481
|
-
}
|
|
482
|
-
});
|
|
483
|
-
|
|
484
|
-
test("child extension activation does not deactivate the parent routing controller", async () => {
|
|
485
|
-
const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-child-activation-"));
|
|
486
|
-
await mkdir(join(cwd, ".freeflow"));
|
|
487
|
-
await writeFile(
|
|
488
|
-
join(cwd, ".freeflow", "config.json"),
|
|
489
|
-
JSON.stringify({
|
|
490
|
-
cognitiveRouting: {
|
|
491
|
-
enabled: true,
|
|
492
|
-
profiles: {
|
|
493
|
-
standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
|
|
494
|
-
reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
|
|
495
|
-
},
|
|
496
|
-
},
|
|
497
|
-
}),
|
|
498
|
-
);
|
|
499
|
-
const parent = createExtensionHost();
|
|
500
|
-
const parentContext = createContext(cwd, parent);
|
|
501
|
-
const child = createExtensionHost();
|
|
502
|
-
const childContext = createContext(cwd, child);
|
|
503
|
-
childContext.modelStateProvenance = { explicitModel: true, explicitThinking: true };
|
|
504
|
-
try {
|
|
505
|
-
await parent.handlers.get("session_start")({ type: "session_start", reason: "startup" }, parentContext);
|
|
506
|
-
await parent.handlers.get("before_agent_start")({ prompt: "hello", systemPrompt: "base prompt" }, parentContext);
|
|
507
|
-
assert.deepEqual(parent.state, { model: { provider: "faux", id: "reasoning" }, thinkingLevel: "max" });
|
|
508
|
-
|
|
509
|
-
await child.handlers.get("session_start")({ type: "session_start", reason: "startup" }, childContext);
|
|
510
|
-
|
|
511
|
-
assert.deepEqual(parent.state, { model: { provider: "faux", id: "reasoning" }, thinkingLevel: "max" });
|
|
512
|
-
assert.equal(
|
|
513
|
-
parent.entries.some((entry) => entry.kind === "closing"),
|
|
514
|
-
false,
|
|
515
|
-
);
|
|
516
|
-
} finally {
|
|
517
|
-
await child.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, childContext);
|
|
518
|
-
await parent.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, parentContext);
|
|
519
|
-
await rm(cwd, { recursive: true, force: true });
|
|
520
|
-
}
|
|
521
|
-
});
|
|
522
|
-
|
|
523
|
-
test("registered profile commands create manual holds and release them without a model entry", async () => {
|
|
524
|
-
const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-command-"));
|
|
525
|
-
await mkdir(join(cwd, ".freeflow"));
|
|
526
|
-
await writeFile(
|
|
527
|
-
join(cwd, ".freeflow", "config.json"),
|
|
528
|
-
JSON.stringify({
|
|
529
|
-
cognitiveRouting: {
|
|
530
|
-
enabled: true,
|
|
531
|
-
profiles: {
|
|
532
|
-
standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
|
|
533
|
-
reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
|
|
534
|
-
},
|
|
535
|
-
},
|
|
536
|
-
}),
|
|
537
|
-
);
|
|
538
|
-
const host = createExtensionHost();
|
|
539
|
-
const ctx = createContext(cwd, host);
|
|
540
|
-
try {
|
|
541
|
-
await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
|
|
542
|
-
await host.handlers.get("before_agent_start")({ systemPrompt: "base prompt" }, ctx);
|
|
543
|
-
const command = host.commands.find(({ name }) => name === "freeflow");
|
|
544
|
-
assert.ok(command);
|
|
545
|
-
assert.deepEqual(
|
|
546
|
-
command.definition.getArgumentCompletions("profile r").map(({ value }) => value),
|
|
547
|
-
["profile reasoning"],
|
|
548
|
-
);
|
|
549
|
-
|
|
550
|
-
await command.definition.handler("profile reasoning", ctx);
|
|
551
|
-
assert.deepEqual(host.state, { model: { provider: "faux", id: "reasoning" }, thinkingLevel: "max" });
|
|
552
|
-
assert.deepEqual(host.operations, ["prepare", "acquire", "setState", "prepare", "setState"]);
|
|
553
|
-
assert.ok(!host.activeToolNames().includes("freeflow_switch_profile"));
|
|
554
|
-
|
|
555
|
-
await command.definition.handler("profile auto", ctx);
|
|
556
|
-
assert.deepEqual(host.state, { model: { provider: "faux", id: "reasoning" }, thinkingLevel: "max" });
|
|
557
|
-
assert.deepEqual(host.operations, ["prepare", "acquire", "setState", "prepare", "setState", "prepare"]);
|
|
558
|
-
assert.equal(host.entries.at(-1).customType, "freeflow-cognitive-routing-control");
|
|
559
|
-
assert.ok(host.activeToolNames().includes("freeflow_switch_profile"));
|
|
560
|
-
|
|
561
|
-
const switchTool = host.tools.find((tool) => tool.name === "freeflow_switch_profile");
|
|
562
|
-
const switchResult = await switchTool.execute(
|
|
563
|
-
"call-1",
|
|
564
|
-
{ target: "standard", reason: "Return to the standard profile." },
|
|
565
|
-
undefined,
|
|
566
|
-
undefined,
|
|
567
|
-
ctx,
|
|
568
|
-
);
|
|
569
|
-
assert.deepEqual(switchResult.details.result, {
|
|
570
|
-
status: "active",
|
|
571
|
-
changed: true,
|
|
572
|
-
from: "reasoning",
|
|
573
|
-
to: "standard",
|
|
574
|
-
profile: "standard",
|
|
575
|
-
});
|
|
576
|
-
assert.deepEqual(host.state, { model: { provider: "faux", id: "standard" }, thinkingLevel: "high" });
|
|
577
|
-
assert.deepEqual(host.operations, [
|
|
578
|
-
"prepare",
|
|
579
|
-
"acquire",
|
|
580
|
-
"setState",
|
|
581
|
-
"prepare",
|
|
582
|
-
"setState",
|
|
583
|
-
"prepare",
|
|
584
|
-
"prepare",
|
|
585
|
-
"setState",
|
|
586
|
-
]);
|
|
587
|
-
} finally {
|
|
588
|
-
await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
|
|
589
|
-
await rm(cwd, { recursive: true, force: true });
|
|
590
|
-
}
|
|
591
|
-
});
|
|
592
|
-
|
|
593
|
-
test("settled runs preserve automatic reasoning and manual holds", async () => {
|
|
594
|
-
const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-settled-"));
|
|
595
|
-
await mkdir(join(cwd, ".freeflow"));
|
|
596
|
-
await writeFile(
|
|
597
|
-
join(cwd, ".freeflow", "config.json"),
|
|
598
|
-
JSON.stringify({
|
|
599
|
-
cognitiveRouting: {
|
|
600
|
-
enabled: true,
|
|
601
|
-
profiles: {
|
|
602
|
-
standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
|
|
603
|
-
reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
|
|
604
|
-
},
|
|
605
|
-
},
|
|
606
|
-
}),
|
|
607
|
-
);
|
|
608
|
-
const host = createExtensionHost();
|
|
609
|
-
const ctx = createContext(cwd, host);
|
|
610
|
-
try {
|
|
611
|
-
await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
|
|
612
|
-
await host.handlers.get("before_agent_start")({ prompt: "hello", systemPrompt: "base prompt" }, ctx);
|
|
613
|
-
const switchTool = host.tools.find((tool) => tool.name === "freeflow_switch_profile");
|
|
614
|
-
await switchTool.execute(
|
|
615
|
-
"call-1",
|
|
616
|
-
{ target: "reasoning", reason: "Need a deeper analysis." },
|
|
617
|
-
undefined,
|
|
618
|
-
undefined,
|
|
619
|
-
ctx,
|
|
620
|
-
);
|
|
621
|
-
assert.deepEqual(host.state, { model: { provider: "faux", id: "reasoning" }, thinkingLevel: "max" });
|
|
622
|
-
|
|
623
|
-
const callsBeforeSettledAutomatic = host.operations.length;
|
|
624
|
-
await host.handlers.get("agent_settled")({ type: "agent_settled" }, ctx);
|
|
625
|
-
assert.equal(host.operations.length, callsBeforeSettledAutomatic);
|
|
626
|
-
assert.deepEqual(host.state, { model: { provider: "faux", id: "reasoning" }, thinkingLevel: "max" });
|
|
627
|
-
|
|
628
|
-
const command = host.commands.find(({ name }) => name === "freeflow");
|
|
629
|
-
await command.definition.handler("profile reasoning", ctx);
|
|
630
|
-
const callsBeforeSettledManual = host.operations.length;
|
|
631
|
-
await host.handlers.get("agent_settled")({ type: "agent_settled" }, ctx);
|
|
632
|
-
assert.equal(host.operations.length, callsBeforeSettledManual);
|
|
633
|
-
assert.deepEqual(host.state, { model: { provider: "faux", id: "reasoning" }, thinkingLevel: "max" });
|
|
634
|
-
} finally {
|
|
635
|
-
await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
|
|
636
|
-
await rm(cwd, { recursive: true, force: true });
|
|
637
|
-
}
|
|
638
|
-
});
|
|
639
|
-
|
|
640
|
-
test("reload preserves a manual reasoning hold after lease rotation", async () => {
|
|
641
|
-
const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-reload-manual-"));
|
|
642
|
-
await mkdir(join(cwd, ".freeflow"));
|
|
643
|
-
await writeFile(
|
|
644
|
-
join(cwd, ".freeflow", "config.json"),
|
|
645
|
-
JSON.stringify({
|
|
646
|
-
cognitiveRouting: {
|
|
647
|
-
enabled: true,
|
|
648
|
-
profiles: {
|
|
649
|
-
standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
|
|
650
|
-
reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
|
|
651
|
-
},
|
|
652
|
-
},
|
|
653
|
-
}),
|
|
654
|
-
);
|
|
655
|
-
const host = createExtensionHost();
|
|
656
|
-
const ctx = createContext(cwd, host);
|
|
657
|
-
ctx.isIdle = () => true;
|
|
658
|
-
try {
|
|
659
|
-
await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
|
|
660
|
-
await host.handlers.get("before_agent_start")({ systemPrompt: "base prompt" }, ctx);
|
|
661
|
-
await host.commands.find(({ name }) => name === "freeflow").definition.handler("profile reasoning", ctx);
|
|
662
|
-
await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "reload" }, ctx);
|
|
663
|
-
|
|
664
|
-
assert.deepEqual(host.state, { model: { provider: "faux", id: "return" }, thinkingLevel: "medium" });
|
|
665
|
-
await host.handlers.get("session_start")({ type: "session_start", reason: "reload" }, ctx);
|
|
666
|
-
|
|
667
|
-
assert.deepEqual(host.state, { model: { provider: "faux", id: "reasoning" }, thinkingLevel: "max" });
|
|
668
|
-
assert.ok(!host.activeToolNames().includes("freeflow_switch_profile"));
|
|
669
|
-
assert.deepEqual(host.state, { model: { provider: "faux", id: "reasoning" }, thinkingLevel: "max" });
|
|
670
|
-
|
|
671
|
-
const operationsBeforeCompaction = [...host.operations];
|
|
672
|
-
await host.handlers.get("session_compact")({ type: "session_compact" }, ctx);
|
|
673
|
-
assert.deepEqual(host.operations, operationsBeforeCompaction);
|
|
674
|
-
assert.deepEqual(host.state, { model: { provider: "faux", id: "reasoning" }, thinkingLevel: "max" });
|
|
675
|
-
assert.ok(!host.activeToolNames().includes("freeflow_switch_profile"));
|
|
676
|
-
} finally {
|
|
677
|
-
await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
|
|
678
|
-
await rm(cwd, { recursive: true, force: true });
|
|
679
|
-
}
|
|
680
|
-
});
|
|
681
|
-
|
|
682
|
-
test("tree navigation retains the current profile without a supported branch choice", async () => {
|
|
683
|
-
const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-tree-"));
|
|
684
|
-
await mkdir(join(cwd, ".freeflow"));
|
|
685
|
-
await writeFile(
|
|
686
|
-
join(cwd, ".freeflow", "config.json"),
|
|
687
|
-
JSON.stringify({
|
|
688
|
-
cognitiveRouting: {
|
|
689
|
-
enabled: true,
|
|
690
|
-
profiles: {
|
|
691
|
-
standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
|
|
692
|
-
reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
|
|
693
|
-
},
|
|
694
|
-
},
|
|
695
|
-
}),
|
|
696
|
-
);
|
|
697
|
-
const host = createExtensionHost();
|
|
698
|
-
const ctx = createContext(cwd, host);
|
|
699
|
-
try {
|
|
700
|
-
await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
|
|
701
|
-
await host.handlers.get("before_agent_start")({ systemPrompt: "base prompt" }, ctx);
|
|
702
|
-
const standardBranch = [...host.entries];
|
|
703
|
-
const command = host.commands.find(({ name }) => name === "freeflow");
|
|
704
|
-
await command.definition.handler("profile reasoning", ctx);
|
|
705
|
-
assert.deepEqual(host.state, { model: { provider: "faux", id: "reasoning" }, thinkingLevel: "max" });
|
|
706
|
-
|
|
707
|
-
ctx.sessionManager.getBranch = () => standardBranch;
|
|
708
|
-
await host.handlers.get("session_tree")(
|
|
709
|
-
{ type: "session_tree", newLeafId: "standard", oldLeafId: "reasoning" },
|
|
710
|
-
ctx,
|
|
711
|
-
);
|
|
712
|
-
|
|
713
|
-
assert.deepEqual(host.state, { model: { provider: "faux", id: "reasoning" }, thinkingLevel: "max" });
|
|
714
|
-
assert.ok(!host.activeToolNames().includes("freeflow_switch_profile"));
|
|
715
|
-
} finally {
|
|
716
|
-
await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
|
|
717
|
-
await rm(cwd, { recursive: true, force: true });
|
|
718
|
-
}
|
|
719
|
-
});
|
|
720
|
-
|
|
721
|
-
test("compaction reconciles without changing an active routing pair", async () => {
|
|
722
|
-
const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-compact-"));
|
|
723
|
-
await mkdir(join(cwd, ".freeflow"));
|
|
724
|
-
await writeFile(
|
|
725
|
-
join(cwd, ".freeflow", "config.json"),
|
|
726
|
-
JSON.stringify({
|
|
727
|
-
cognitiveRouting: {
|
|
728
|
-
enabled: true,
|
|
729
|
-
profiles: {
|
|
730
|
-
standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
|
|
731
|
-
reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
|
|
732
|
-
},
|
|
733
|
-
},
|
|
734
|
-
}),
|
|
735
|
-
);
|
|
736
|
-
const host = createExtensionHost();
|
|
737
|
-
const ctx = createContext(cwd, host);
|
|
738
|
-
try {
|
|
739
|
-
await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
|
|
740
|
-
await host.handlers.get("before_agent_start")({ prompt: "hello", systemPrompt: "base prompt" }, ctx);
|
|
741
|
-
const operationsBeforeCompaction = [...host.operations];
|
|
742
|
-
await host.handlers.get("session_compact")({ type: "session_compact" }, ctx);
|
|
743
|
-
|
|
744
|
-
assert.deepEqual(host.operations, operationsBeforeCompaction);
|
|
745
|
-
assert.deepEqual(host.state, { model: { provider: "faux", id: "reasoning" }, thinkingLevel: "max" });
|
|
746
|
-
assert.ok(host.activeToolNames().includes("freeflow_switch_profile"));
|
|
747
|
-
} finally {
|
|
748
|
-
await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
|
|
749
|
-
await rm(cwd, { recursive: true, force: true });
|
|
750
|
-
}
|
|
751
|
-
});
|
|
752
|
-
|
|
753
|
-
test("reload with disabled configuration stays inactive after successful cleanup", async () => {
|
|
754
|
-
const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-reload-disabled-"));
|
|
755
|
-
await mkdir(join(cwd, ".freeflow"));
|
|
756
|
-
const configPath = join(cwd, ".freeflow", "config.json");
|
|
757
|
-
await writeFile(
|
|
758
|
-
configPath,
|
|
759
|
-
JSON.stringify({
|
|
760
|
-
cognitiveRouting: {
|
|
761
|
-
enabled: true,
|
|
762
|
-
profiles: {
|
|
763
|
-
standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
|
|
764
|
-
reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
|
|
765
|
-
},
|
|
766
|
-
},
|
|
767
|
-
}),
|
|
768
|
-
);
|
|
769
|
-
const host = createExtensionHost();
|
|
770
|
-
const ctx = createContext(cwd, host);
|
|
771
|
-
try {
|
|
772
|
-
await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
|
|
773
|
-
await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "reload" }, ctx);
|
|
774
|
-
await writeFile(configPath, JSON.stringify({ cognitiveRouting: { enabled: false } }));
|
|
775
|
-
const operationsBeforeDisabledStart = [...host.operations];
|
|
776
|
-
|
|
777
|
-
await host.handlers.get("session_start")({ type: "session_start", reason: "reload" }, ctx);
|
|
778
|
-
|
|
779
|
-
assert.deepEqual(host.operations, operationsBeforeDisabledStart);
|
|
780
|
-
assert.deepEqual(host.state, { model: { provider: "faux", id: "return" }, thinkingLevel: "medium" });
|
|
781
|
-
assert.ok(!host.activeToolNames().includes("freeflow_switch_profile"));
|
|
782
|
-
} finally {
|
|
783
|
-
await rm(cwd, { recursive: true, force: true });
|
|
784
|
-
}
|
|
785
|
-
});
|
|
786
|
-
|
|
787
|
-
test("session replacement restores the pair before releasing routing ownership", async () => {
|
|
788
|
-
const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-replacement-"));
|
|
789
|
-
await mkdir(join(cwd, ".freeflow"));
|
|
790
|
-
await writeFile(
|
|
791
|
-
join(cwd, ".freeflow", "config.json"),
|
|
792
|
-
JSON.stringify({
|
|
793
|
-
cognitiveRouting: {
|
|
794
|
-
enabled: true,
|
|
795
|
-
profiles: {
|
|
796
|
-
standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
|
|
797
|
-
reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
|
|
798
|
-
},
|
|
799
|
-
},
|
|
800
|
-
}),
|
|
801
|
-
);
|
|
802
|
-
const host = createExtensionHost();
|
|
803
|
-
const ctx = createContext(cwd, host);
|
|
804
|
-
try {
|
|
805
|
-
await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
|
|
806
|
-
await host.handlers.get("before_agent_start")({ prompt: "hello", systemPrompt: "base prompt" }, ctx);
|
|
807
|
-
await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "new" }, ctx);
|
|
808
|
-
|
|
809
|
-
assert.deepEqual(host.operations, ["prepare", "acquire", "setState", "prepare", "setState", "release"]);
|
|
810
|
-
assert.deepEqual(host.state, { model: { provider: "faux", id: "return" }, thinkingLevel: "medium" });
|
|
811
|
-
} finally {
|
|
812
|
-
await rm(cwd, { recursive: true, force: true });
|
|
813
|
-
}
|
|
814
|
-
});
|
|
815
|
-
|
|
816
|
-
test("reload recovery stays inactive after an unmatched closing intent", async () => {
|
|
817
|
-
const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-reload-failure-"));
|
|
818
|
-
await mkdir(join(cwd, ".freeflow"));
|
|
819
|
-
await writeFile(
|
|
820
|
-
join(cwd, ".freeflow", "config.json"),
|
|
821
|
-
JSON.stringify({
|
|
822
|
-
cognitiveRouting: {
|
|
823
|
-
enabled: true,
|
|
824
|
-
profiles: {
|
|
825
|
-
standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
|
|
826
|
-
reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
|
|
827
|
-
},
|
|
828
|
-
},
|
|
829
|
-
}),
|
|
830
|
-
);
|
|
831
|
-
const host = createExtensionHost({ rejectReturnRestore: true });
|
|
832
|
-
const ctx = createContext(cwd, host);
|
|
833
|
-
try {
|
|
834
|
-
await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
|
|
835
|
-
await host.handlers.get("before_agent_start")({ prompt: "hello", systemPrompt: "base prompt" }, ctx);
|
|
836
|
-
await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "reload" }, ctx);
|
|
837
|
-
assert.deepEqual(host.operations, ["prepare", "acquire", "setState", "prepare", "setState", "release"]);
|
|
838
|
-
assert.equal(host.entries.at(-1).customType, "freeflow-cognitive-routing-intent");
|
|
839
|
-
assert.equal(host.entries.at(-1).data.kind, "closing");
|
|
840
|
-
|
|
841
|
-
await host.handlers.get("session_start")({ type: "session_start", reason: "reload" }, ctx);
|
|
842
|
-
assert.deepEqual(host.operations, [
|
|
843
|
-
"prepare",
|
|
844
|
-
"acquire",
|
|
845
|
-
"setState",
|
|
846
|
-
"prepare",
|
|
847
|
-
"setState",
|
|
848
|
-
"release",
|
|
849
|
-
"prepare",
|
|
850
|
-
"acquire",
|
|
851
|
-
"setState",
|
|
852
|
-
"release",
|
|
853
|
-
]);
|
|
854
|
-
assert.equal(host.entries.at(-1).data.phase, "prepared");
|
|
855
|
-
assert.ok(!host.activeToolNames().includes("freeflow_switch_profile"));
|
|
856
|
-
} finally {
|
|
857
|
-
await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
|
|
858
|
-
await rm(cwd, { recursive: true, force: true });
|
|
859
|
-
}
|
|
860
|
-
});
|
|
861
|
-
|
|
862
|
-
test("Pi delivers stable Cognitive Routing cues and refreshes runtime state only when needed", async () => {
|
|
863
|
-
const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-compact-delivery-"));
|
|
864
|
-
await mkdir(join(cwd, ".freeflow"));
|
|
865
|
-
await writeFile(
|
|
866
|
-
join(cwd, ".freeflow", "config.json"),
|
|
867
|
-
JSON.stringify({
|
|
868
|
-
cognitiveRouting: {
|
|
869
|
-
enabled: true,
|
|
870
|
-
profiles: {
|
|
871
|
-
standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
|
|
872
|
-
reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
|
|
873
|
-
},
|
|
874
|
-
},
|
|
875
|
-
}),
|
|
876
|
-
);
|
|
877
|
-
const host = createExtensionHost();
|
|
878
|
-
const ctx = createContext(cwd, host);
|
|
879
|
-
try {
|
|
880
|
-
await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
|
|
881
|
-
const beforeAgentStart = host.handlers.get("before_agent_start");
|
|
882
|
-
const first = await beforeAgentStart({ systemPrompt: "base prompt" }, ctx);
|
|
883
|
-
|
|
884
|
-
assert.equal(first.message, undefined);
|
|
885
|
-
assert.match(first.systemPrompt, /## Cognitive Routing Cue/);
|
|
886
|
-
assert.match(first.systemPrompt, /each user interaction begins in Reasoning/);
|
|
887
|
-
assert.match(first.systemPrompt, /If the full Cognitive Routing skill is not visible/);
|
|
888
|
-
assert.match(first.systemPrompt, /Make that read the only environment call/);
|
|
889
|
-
assert.match(first.systemPrompt, /it is not a route transition/);
|
|
890
|
-
assert.match(first.systemPrompt, /YIELD/);
|
|
891
|
-
assert.match(first.systemPrompt, /Delegate/);
|
|
892
|
-
assert.match(first.systemPrompt, /ACT_BOUNDED/);
|
|
893
|
-
assert.match(first.systemPrompt, /Automatic Standard only executes active Yield or Delegate contracts/);
|
|
894
|
-
assert.match(first.systemPrompt, /writes `YIELD HANDOFF` or `RETURN` and switches to Reasoning/);
|
|
895
|
-
assert.match(first.systemPrompt, /Standard never handles substantive user-facing interaction/);
|
|
896
|
-
assert.doesNotMatch(first.systemPrompt, /TASK ACT|OBSERVE/);
|
|
897
|
-
assert.ok(
|
|
898
|
-
first.systemPrompt.indexOf("## Action Selection Cue") < first.systemPrompt.indexOf("## Cognitive Routing Cue"),
|
|
899
|
-
);
|
|
900
|
-
assert.doesNotMatch(first.systemPrompt, /^# Cognitive Routing$/m);
|
|
901
|
-
assert.doesNotMatch(first.systemPrompt, /^# Automatic Routing Kernel$/m);
|
|
902
|
-
|
|
903
|
-
const contextHandler = host.handlers.get("context");
|
|
904
|
-
const firstContext = await contextHandler({ messages: [] }, ctx);
|
|
905
|
-
assert.equal(firstContext.messages.length, 1);
|
|
906
|
-
assert.equal(firstContext.messages[0].customType, "freeflow-runtime-state");
|
|
907
|
-
assert.match(firstContext.messages[0].content, /Cognitive Routing: active/);
|
|
908
|
-
assert.match(firstContext.messages[0].content, /Control: `automatic`/);
|
|
909
|
-
assert.match(firstContext.messages[0].content, /Profile: `reasoning`/);
|
|
910
|
-
|
|
911
|
-
const unchangedContext = await contextHandler({ messages: firstContext.messages }, ctx);
|
|
912
|
-
assert.equal(unchangedContext, undefined);
|
|
913
|
-
|
|
914
|
-
await host.handlers.get("session_tree")({ type: "session_tree", reason: "branch" }, ctx);
|
|
915
|
-
const branchContext = await contextHandler({ messages: firstContext.messages }, ctx);
|
|
916
|
-
assert.ok(branchContext);
|
|
917
|
-
assert.equal(branchContext.messages.length, 1);
|
|
918
|
-
assert.match(branchContext.messages[0].content, /Profile: `reasoning`/);
|
|
919
|
-
|
|
920
|
-
await host.handlers.get("session_compact")({ type: "session_compact", reason: "compaction" }, ctx);
|
|
921
|
-
const compactContext = await contextHandler({ messages: branchContext.messages }, ctx);
|
|
922
|
-
assert.ok(compactContext);
|
|
923
|
-
assert.equal(compactContext.messages.length, 1);
|
|
924
|
-
assert.match(compactContext.messages[0].content, /Profile: `reasoning`/);
|
|
925
|
-
|
|
926
|
-
const second = await beforeAgentStart({ systemPrompt: "base prompt" }, ctx);
|
|
927
|
-
assert.equal(second.message, undefined);
|
|
928
|
-
assert.equal((second.systemPrompt.match(/## Cognitive Routing Cue/g) ?? []).length, 1);
|
|
929
|
-
} finally {
|
|
930
|
-
await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
|
|
931
|
-
await rm(cwd, { recursive: true, force: true });
|
|
932
|
-
}
|
|
933
|
-
});
|
|
934
|
-
|
|
935
|
-
test("Pi preserves runtime state after a no-op profile switch", async () => {
|
|
936
|
-
const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-no-op-state-"));
|
|
937
|
-
await mkdir(join(cwd, ".freeflow"));
|
|
938
|
-
await writeFile(
|
|
939
|
-
join(cwd, ".freeflow", "config.json"),
|
|
940
|
-
JSON.stringify({
|
|
941
|
-
cognitiveRouting: {
|
|
942
|
-
enabled: true,
|
|
943
|
-
profiles: {
|
|
944
|
-
standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
|
|
945
|
-
reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
|
|
946
|
-
},
|
|
947
|
-
},
|
|
948
|
-
}),
|
|
949
|
-
);
|
|
950
|
-
const host = createExtensionHost();
|
|
951
|
-
const ctx = createContext(cwd, host);
|
|
952
|
-
try {
|
|
953
|
-
await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
|
|
954
|
-
await host.handlers.get("before_agent_start")({ systemPrompt: "base prompt" }, ctx);
|
|
955
|
-
|
|
956
|
-
const contextHandler = host.handlers.get("context");
|
|
957
|
-
const reasoningContext = await contextHandler({ messages: [] }, ctx);
|
|
958
|
-
const runtimeStateMessages = reasoningContext.messages.filter(
|
|
959
|
-
(message) =>
|
|
960
|
-
message.customType === "freeflow-runtime-state" ||
|
|
961
|
-
message.customType === "freeflow-cognitive-routing-runtime-state",
|
|
962
|
-
);
|
|
963
|
-
assert.equal(runtimeStateMessages.length, 1);
|
|
964
|
-
assert.equal(runtimeStateMessages[0].customType, "freeflow-runtime-state");
|
|
965
|
-
|
|
966
|
-
const switchTool = host.tools.find((tool) => tool.name === "freeflow_switch_profile");
|
|
967
|
-
const result = await switchTool.execute(
|
|
968
|
-
"call-no-op-volatile-state",
|
|
969
|
-
{ target: "reasoning", reason: "Remain on Reasoning for this check." },
|
|
970
|
-
undefined,
|
|
971
|
-
undefined,
|
|
972
|
-
ctx,
|
|
973
|
-
);
|
|
974
|
-
assert.equal(result.details.result.status, "active");
|
|
975
|
-
assert.equal(result.details.result.changed, false);
|
|
976
|
-
|
|
977
|
-
await host.handlers.get("tool_result")({ type: "tool_result", toolName: "freeflow_switch_profile" }, ctx);
|
|
978
|
-
const unchangedContext = await contextHandler({ messages: reasoningContext.messages }, ctx);
|
|
979
|
-
assert.equal(unchangedContext, undefined);
|
|
980
|
-
assert.equal(
|
|
981
|
-
reasoningContext.messages.filter(
|
|
982
|
-
(message) =>
|
|
983
|
-
message.customType === "freeflow-runtime-state" ||
|
|
984
|
-
message.customType === "freeflow-cognitive-routing-runtime-state",
|
|
985
|
-
).length,
|
|
986
|
-
1,
|
|
987
|
-
);
|
|
988
|
-
} finally {
|
|
989
|
-
await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
|
|
990
|
-
await rm(cwd, { recursive: true, force: true });
|
|
991
|
-
}
|
|
992
|
-
});
|
|
993
|
-
|
|
994
|
-
test("Pi refreshes volatile Cognitive Routing state after a profile switch", async () => {
|
|
995
|
-
const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-volatile-state-"));
|
|
996
|
-
await mkdir(join(cwd, ".freeflow"));
|
|
997
|
-
await writeFile(
|
|
998
|
-
join(cwd, ".freeflow", "config.json"),
|
|
999
|
-
JSON.stringify({
|
|
1000
|
-
cognitiveRouting: {
|
|
1001
|
-
enabled: true,
|
|
1002
|
-
profiles: {
|
|
1003
|
-
standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
|
|
1004
|
-
reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
|
|
1005
|
-
},
|
|
1006
|
-
},
|
|
1007
|
-
}),
|
|
1008
|
-
);
|
|
1009
|
-
const host = createExtensionHost();
|
|
1010
|
-
const ctx = createContext(cwd, host);
|
|
1011
|
-
try {
|
|
1012
|
-
await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
|
|
1013
|
-
await host.handlers.get("before_agent_start")({ systemPrompt: "base prompt" }, ctx);
|
|
1014
|
-
|
|
1015
|
-
const contextHandler = host.handlers.get("context");
|
|
1016
|
-
const reasoningContext = await contextHandler({ messages: [] }, ctx);
|
|
1017
|
-
assert.match(reasoningContext.messages[0].content, /Profile: `reasoning`/);
|
|
1018
|
-
|
|
1019
|
-
const switchTool = host.tools.find((tool) => tool.name === "freeflow_switch_profile");
|
|
1020
|
-
const result = await switchTool.execute(
|
|
1021
|
-
"call-volatile-state",
|
|
1022
|
-
{ target: "standard", reason: "Use Standard for bounded execution." },
|
|
1023
|
-
undefined,
|
|
1024
|
-
undefined,
|
|
1025
|
-
ctx,
|
|
1026
|
-
);
|
|
1027
|
-
assert.equal(result.details.result.to, "standard");
|
|
1028
|
-
|
|
1029
|
-
const standardContext = await contextHandler({ messages: reasoningContext.messages }, ctx);
|
|
1030
|
-
assert.equal(standardContext.messages.length, 1);
|
|
1031
|
-
assert.match(standardContext.messages[0].content, /Control: `automatic`/);
|
|
1032
|
-
assert.match(standardContext.messages[0].content, /Profile: `standard`/);
|
|
1033
|
-
assert.doesNotMatch(standardContext.messages[0].content, /Profile: `reasoning`/);
|
|
1034
|
-
} finally {
|
|
1035
|
-
await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
|
|
1036
|
-
await rm(cwd, { recursive: true, force: true });
|
|
1037
|
-
}
|
|
1038
|
-
});
|
|
1039
|
-
|
|
1040
|
-
test("Pi rejects the removed Skills configuration instead of gating Cognitive Routing", async () => {
|
|
1041
|
-
const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-skills-gate-"));
|
|
1042
|
-
await mkdir(join(cwd, ".freeflow"));
|
|
1043
|
-
await writeFile(
|
|
1044
|
-
join(cwd, ".freeflow", "config.json"),
|
|
1045
|
-
JSON.stringify({
|
|
1046
|
-
skills: { enabled: false },
|
|
1047
|
-
cognitiveRouting: {
|
|
1048
|
-
enabled: true,
|
|
1049
|
-
profiles: {
|
|
1050
|
-
standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
|
|
1051
|
-
reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
|
|
1052
|
-
},
|
|
1053
|
-
},
|
|
1054
|
-
}),
|
|
1055
|
-
);
|
|
1056
|
-
const host = createExtensionHost();
|
|
1057
|
-
const ctx = createContext(cwd, host);
|
|
1058
|
-
try {
|
|
1059
|
-
await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
|
|
1060
|
-
const result = await host.handlers.get("before_agent_start")({ systemPrompt: "base prompt" }, ctx);
|
|
1061
|
-
assert.equal(result.message, undefined);
|
|
1062
|
-
assert.doesNotMatch(result.systemPrompt, /## Cognitive Routing Cue/);
|
|
1063
|
-
assert.doesNotMatch(result.systemPrompt, /^# Cognitive Routing$/m);
|
|
1064
|
-
assert.ok(!host.activeToolNames().includes("freeflow_switch_profile"));
|
|
1065
|
-
const providerContext = await host.handlers.get("context")({ messages: [] }, ctx);
|
|
1066
|
-
assert.match(providerContext.messages[0].content, /Freeflow: config error/);
|
|
1067
|
-
assert.doesNotMatch(providerContext.messages[0].content, /Skills:/);
|
|
1068
|
-
} finally {
|
|
1069
|
-
await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
|
|
1070
|
-
await rm(cwd, { recursive: true, force: true });
|
|
1071
|
-
}
|
|
1072
|
-
});
|
|
1073
|
-
|
|
1074
|
-
test("Pi lifecycle prepares, activates, restores, and releases Cognitive Routing ownership", async () => {
|
|
1075
|
-
const cwd = await mkdtemp(join(tmpdir(), "freeflow-cognitive-routing-extension-"));
|
|
1076
|
-
await mkdir(join(cwd, ".freeflow"));
|
|
1077
|
-
await writeFile(
|
|
1078
|
-
join(cwd, ".freeflow", "config.json"),
|
|
1079
|
-
JSON.stringify({
|
|
1080
|
-
cognitiveRouting: {
|
|
1081
|
-
enabled: true,
|
|
1082
|
-
profiles: {
|
|
1083
|
-
standard: { provider: "faux", model: "standard", thinkingLevel: "high" },
|
|
1084
|
-
reasoning: { provider: "faux", model: "reasoning", thinkingLevel: "max" },
|
|
1085
|
-
},
|
|
1086
|
-
},
|
|
1087
|
-
}),
|
|
1088
|
-
);
|
|
1089
|
-
const host = createExtensionHost();
|
|
1090
|
-
const ctx = createContext(cwd, host);
|
|
1091
|
-
try {
|
|
1092
|
-
await host.handlers.get("session_start")({ type: "session_start", reason: "startup" }, ctx);
|
|
1093
|
-
assert.deepEqual(host.operations, []);
|
|
1094
|
-
|
|
1095
|
-
const beforeAgentStart = await host.handlers.get("before_agent_start")(
|
|
1096
|
-
{ prompt: "hello", systemPrompt: "base prompt" },
|
|
1097
|
-
ctx,
|
|
1098
|
-
);
|
|
1099
|
-
assert.deepEqual(host.operations.slice(0, 3), ["prepare", "acquire", "setState"]);
|
|
1100
|
-
assert.deepEqual(host.state, { model: { provider: "faux", id: "reasoning" }, thinkingLevel: "max" });
|
|
1101
|
-
assert.equal(host.entries[0].customType, "freeflow-cognitive-routing-intent");
|
|
1102
|
-
assert.ok(host.activeToolNames().includes("freeflow_switch_profile"));
|
|
1103
|
-
assert.match(beforeAgentStart.systemPrompt, /## Cognitive Routing Cue/);
|
|
1104
|
-
assert.doesNotMatch(beforeAgentStart.systemPrompt, /^# Cognitive Routing$/m);
|
|
1105
|
-
assert.doesNotMatch(beforeAgentStart.systemPrompt, /^# Automatic Routing Kernel$/m);
|
|
1106
|
-
assert.equal(beforeAgentStart.message, undefined);
|
|
1107
|
-
|
|
1108
|
-
const switchTool = host.tools.find((tool) => tool.name === "freeflow_switch_profile");
|
|
1109
|
-
await switchTool.execute(
|
|
1110
|
-
"call-1",
|
|
1111
|
-
{ target: "standard", reason: "Use Standard for the bounded execution pass." },
|
|
1112
|
-
undefined,
|
|
1113
|
-
undefined,
|
|
1114
|
-
ctx,
|
|
1115
|
-
);
|
|
1116
|
-
assert.deepEqual(host.state, { model: { provider: "faux", id: "standard" }, thinkingLevel: "high" });
|
|
1117
|
-
|
|
1118
|
-
await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "reload" }, ctx);
|
|
1119
|
-
|
|
1120
|
-
assert.deepEqual(host.operations, [
|
|
1121
|
-
"prepare",
|
|
1122
|
-
"acquire",
|
|
1123
|
-
"setState",
|
|
1124
|
-
"prepare",
|
|
1125
|
-
"setState",
|
|
1126
|
-
"prepare",
|
|
1127
|
-
"setState",
|
|
1128
|
-
"release",
|
|
1129
|
-
]);
|
|
1130
|
-
assert.deepEqual(host.state, { model: { provider: "faux", id: "return" }, thinkingLevel: "medium" });
|
|
1131
|
-
assert.equal(host.entries.at(-1).type, "model_state_change");
|
|
1132
|
-
assert.equal(host.entries.at(-1).modelId, "return");
|
|
1133
|
-
|
|
1134
|
-
await host.handlers.get("session_start")({ type: "session_start", reason: "reload" }, ctx);
|
|
1135
|
-
assert.deepEqual(host.operations.slice(-3), ["prepare", "acquire", "setState"]);
|
|
1136
|
-
assert.deepEqual(host.state, { model: { provider: "faux", id: "standard" }, thinkingLevel: "high" });
|
|
1137
|
-
assert.ok(host.activeToolNames().includes("freeflow_switch_profile"));
|
|
1138
|
-
await host.handlers.get("session_shutdown")({ type: "session_shutdown", reason: "test-cleanup" }, ctx);
|
|
1139
|
-
} finally {
|
|
1140
|
-
await rm(cwd, { recursive: true, force: true });
|
|
1141
|
-
}
|
|
1142
|
-
});
|