@hassangameryt/freeflow 0.7.1 → 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 +1 -1
- package/.codex-plugin/plugin.json +1 -1
- package/.cursor-plugin/plugin.json +1 -1
- package/README.md +28 -23
- package/capabilities/cognitive-routing/SKILL.md +94 -311
- package/command-surface.json +7 -1
- package/gemini-extension.json +1 -1
- package/package.json +4 -4
- 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 +1 -1
- 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,320 +0,0 @@
|
|
|
1
|
-
export const PI_SESSION_MODEL_STATE_ENTRY = "freeflow-cognitive-routing-model-state";
|
|
2
|
-
export function supportsPiSessionModelStateApi(pi) {
|
|
3
|
-
if (!pi || typeof pi !== "object") return false;
|
|
4
|
-
const candidate = pi;
|
|
5
|
-
return (
|
|
6
|
-
typeof candidate.appendEntry === "function" &&
|
|
7
|
-
typeof candidate.setModel === "function" &&
|
|
8
|
-
typeof candidate.setThinkingLevel === "function"
|
|
9
|
-
);
|
|
10
|
-
}
|
|
11
|
-
const THINKING_LEVELS = new Set(["off", "minimal", "low", "medium", "high", "xhigh", "max"]);
|
|
12
|
-
function isRecord(value) {
|
|
13
|
-
return typeof value === "object" && value !== null;
|
|
14
|
-
}
|
|
15
|
-
function isThinkingLevel(value) {
|
|
16
|
-
return typeof value === "string" && THINKING_LEVELS.has(value);
|
|
17
|
-
}
|
|
18
|
-
function appendModelStateCommit(pi, commit) {
|
|
19
|
-
try {
|
|
20
|
-
pi.appendEntry(PI_SESSION_MODEL_STATE_ENTRY, commit);
|
|
21
|
-
return true;
|
|
22
|
-
} catch {
|
|
23
|
-
return false;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
function pairFrom(value) {
|
|
27
|
-
if (!isRecord(value)) return undefined;
|
|
28
|
-
if (
|
|
29
|
-
typeof value.provider !== "string" ||
|
|
30
|
-
typeof value.modelId !== "string" ||
|
|
31
|
-
!isThinkingLevel(value.thinkingLevel)
|
|
32
|
-
) {
|
|
33
|
-
return undefined;
|
|
34
|
-
}
|
|
35
|
-
return {
|
|
36
|
-
provider: value.provider,
|
|
37
|
-
modelId: value.modelId,
|
|
38
|
-
thinkingLevel: value.thinkingLevel,
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
function failureFrom(value) {
|
|
42
|
-
if (
|
|
43
|
-
value === "model-change-failed" ||
|
|
44
|
-
value === "thinking-level-change-failed" ||
|
|
45
|
-
value === "pair-verification-failed" ||
|
|
46
|
-
value === "rollback-failed" ||
|
|
47
|
-
value === "commit-persistence-failed" ||
|
|
48
|
-
value === "interrupted-transition"
|
|
49
|
-
) {
|
|
50
|
-
return value;
|
|
51
|
-
}
|
|
52
|
-
return undefined;
|
|
53
|
-
}
|
|
54
|
-
function originFrom(value) {
|
|
55
|
-
if (!isRecord(value)) return undefined;
|
|
56
|
-
if (value.source !== "pi" || value.operation !== "session-model-state-control") return undefined;
|
|
57
|
-
return { source: "pi", operation: "session-model-state-control" };
|
|
58
|
-
}
|
|
59
|
-
export function parsePiSessionModelStateCommit(value) {
|
|
60
|
-
if (!isRecord(value) || value.version !== 1) return undefined;
|
|
61
|
-
if (value.phase !== "prepared" && value.phase !== "committed" && value.phase !== "aborted") return undefined;
|
|
62
|
-
if (
|
|
63
|
-
value.status !== "prepared" &&
|
|
64
|
-
value.status !== "applied" &&
|
|
65
|
-
value.status !== "rolled-back" &&
|
|
66
|
-
value.status !== "rollback-failed"
|
|
67
|
-
) {
|
|
68
|
-
return undefined;
|
|
69
|
-
}
|
|
70
|
-
if (typeof value.correlationId !== "string") return undefined;
|
|
71
|
-
const fromPair = pairFrom(value.fromPair);
|
|
72
|
-
const target = pairFrom(value.target);
|
|
73
|
-
const origin = originFrom(value.origin);
|
|
74
|
-
if (!fromPair || !target || !origin) return undefined;
|
|
75
|
-
if (value.phase === "prepared" && value.status !== "prepared") return undefined;
|
|
76
|
-
if (value.phase === "committed" && value.status !== "applied") return undefined;
|
|
77
|
-
if (value.phase === "aborted" && (value.status === "prepared" || value.status === "applied")) return undefined;
|
|
78
|
-
const failure = failureFrom(value.failure);
|
|
79
|
-
if ((value.phase === "prepared" || value.phase === "committed") && value.failure !== undefined) return undefined;
|
|
80
|
-
if (value.phase === "aborted" && !failure) return undefined;
|
|
81
|
-
let observedPair;
|
|
82
|
-
if (value.observedPair !== undefined) {
|
|
83
|
-
observedPair = pairFrom(value.observedPair);
|
|
84
|
-
if (!observedPair) return undefined;
|
|
85
|
-
}
|
|
86
|
-
return {
|
|
87
|
-
version: 1,
|
|
88
|
-
phase: value.phase,
|
|
89
|
-
status: value.status,
|
|
90
|
-
correlationId: value.correlationId,
|
|
91
|
-
fromPair,
|
|
92
|
-
target,
|
|
93
|
-
...(failure ? { failure } : {}),
|
|
94
|
-
...(observedPair ? { observedPair } : {}),
|
|
95
|
-
origin,
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
function currentPair(ctx) {
|
|
99
|
-
const model = ctx.model;
|
|
100
|
-
if (!model || typeof model.provider !== "string" || typeof model.id !== "string") return undefined;
|
|
101
|
-
if (!ctx.thinkingLevel) return undefined;
|
|
102
|
-
return {
|
|
103
|
-
provider: model.provider,
|
|
104
|
-
modelId: model.id,
|
|
105
|
-
thinkingLevel: ctx.thinkingLevel,
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
function pairsEqual(left, right) {
|
|
109
|
-
return (
|
|
110
|
-
left?.provider === right.provider && left.modelId === right.modelId && left.thinkingLevel === right.thinkingLevel
|
|
111
|
-
);
|
|
112
|
-
}
|
|
113
|
-
async function restorePair(pi, ctx, pair) {
|
|
114
|
-
const model = ctx.modelRegistry?.find(pair.provider, pair.modelId);
|
|
115
|
-
if (!model) return false;
|
|
116
|
-
try {
|
|
117
|
-
if (!(await pi.setModel(model))) return false;
|
|
118
|
-
pi.setThinkingLevel(pair.thinkingLevel);
|
|
119
|
-
return pairsEqual(currentPair(ctx), pair);
|
|
120
|
-
} catch {
|
|
121
|
-
return false;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
export function createPiSessionControllerHost({ pi, ctx }) {
|
|
125
|
-
const control = createPiSessionModelStateControl({ pi, ctx });
|
|
126
|
-
return {
|
|
127
|
-
appendEntryDurable(customType, data) {
|
|
128
|
-
pi.appendEntry(customType, data);
|
|
129
|
-
},
|
|
130
|
-
acquireModelStateControl: control.acquireModelStateControl,
|
|
131
|
-
recoverPreparedModelState: control.recoverPreparedModelState,
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
export function createPiSessionModelStateControl({ pi, ctx }) {
|
|
135
|
-
let owner;
|
|
136
|
-
return {
|
|
137
|
-
async recoverPreparedModelState({ fromPair, target, correlationId }) {
|
|
138
|
-
const current = currentPair(ctx);
|
|
139
|
-
if (!current || !pairsEqual(current, fromPair)) {
|
|
140
|
-
if (!(await restorePair(pi, ctx, fromPair))) {
|
|
141
|
-
const rollbackFailed = {
|
|
142
|
-
version: 1,
|
|
143
|
-
phase: "aborted",
|
|
144
|
-
status: "rollback-failed",
|
|
145
|
-
correlationId,
|
|
146
|
-
fromPair,
|
|
147
|
-
target,
|
|
148
|
-
failure: "rollback-failed",
|
|
149
|
-
...(currentPair(ctx) ? { observedPair: currentPair(ctx) } : {}),
|
|
150
|
-
origin: { source: "pi", operation: "session-model-state-control" },
|
|
151
|
-
};
|
|
152
|
-
try {
|
|
153
|
-
pi.appendEntry(PI_SESSION_MODEL_STATE_ENTRY, rollbackFailed);
|
|
154
|
-
} catch {
|
|
155
|
-
return { status: "rejected" };
|
|
156
|
-
}
|
|
157
|
-
return { status: "rollback-failed" };
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
const aborted = {
|
|
161
|
-
version: 1,
|
|
162
|
-
phase: "aborted",
|
|
163
|
-
status: "rolled-back",
|
|
164
|
-
correlationId,
|
|
165
|
-
fromPair,
|
|
166
|
-
target,
|
|
167
|
-
failure: "interrupted-transition",
|
|
168
|
-
origin: { source: "pi", operation: "session-model-state-control" },
|
|
169
|
-
};
|
|
170
|
-
try {
|
|
171
|
-
pi.appendEntry(PI_SESSION_MODEL_STATE_ENTRY, aborted);
|
|
172
|
-
} catch {
|
|
173
|
-
return { status: "rejected" };
|
|
174
|
-
}
|
|
175
|
-
return { status: "restored" };
|
|
176
|
-
},
|
|
177
|
-
async acquireModelStateControl({ label }) {
|
|
178
|
-
if (owner !== undefined) return { status: "conflict", owner };
|
|
179
|
-
owner = label;
|
|
180
|
-
let released = false;
|
|
181
|
-
return {
|
|
182
|
-
status: "acquired",
|
|
183
|
-
lease: {
|
|
184
|
-
async setState(request) {
|
|
185
|
-
if (released) return { status: "rejected" };
|
|
186
|
-
const fromPair = currentPair(ctx);
|
|
187
|
-
const targetModel = ctx.modelRegistry?.find(request.provider, request.modelId);
|
|
188
|
-
if (!fromPair || !targetModel) return { status: "rejected" };
|
|
189
|
-
const target = {
|
|
190
|
-
provider: request.provider,
|
|
191
|
-
modelId: request.modelId,
|
|
192
|
-
thinkingLevel: request.thinkingLevel,
|
|
193
|
-
};
|
|
194
|
-
const prepared = {
|
|
195
|
-
version: 1,
|
|
196
|
-
phase: "prepared",
|
|
197
|
-
status: "prepared",
|
|
198
|
-
correlationId: request.correlationId,
|
|
199
|
-
fromPair,
|
|
200
|
-
target,
|
|
201
|
-
origin: { source: "pi", operation: "session-model-state-control" },
|
|
202
|
-
};
|
|
203
|
-
try {
|
|
204
|
-
pi.appendEntry(PI_SESSION_MODEL_STATE_ENTRY, prepared);
|
|
205
|
-
} catch {
|
|
206
|
-
return { status: "rejected" };
|
|
207
|
-
}
|
|
208
|
-
let failure;
|
|
209
|
-
try {
|
|
210
|
-
const applied = await pi.setModel(targetModel);
|
|
211
|
-
if (!applied) {
|
|
212
|
-
const aborted = {
|
|
213
|
-
version: 1,
|
|
214
|
-
phase: "aborted",
|
|
215
|
-
status: "rolled-back",
|
|
216
|
-
correlationId: request.correlationId,
|
|
217
|
-
fromPair,
|
|
218
|
-
target,
|
|
219
|
-
failure: "model-change-failed",
|
|
220
|
-
origin: { source: "pi", operation: "session-model-state-control" },
|
|
221
|
-
};
|
|
222
|
-
appendModelStateCommit(pi, aborted);
|
|
223
|
-
return { status: "rejected" };
|
|
224
|
-
}
|
|
225
|
-
try {
|
|
226
|
-
pi.setThinkingLevel(request.thinkingLevel);
|
|
227
|
-
} catch {
|
|
228
|
-
failure = "thinking-level-change-failed";
|
|
229
|
-
}
|
|
230
|
-
if (!failure && !pairsEqual(currentPair(ctx), target)) {
|
|
231
|
-
failure = "pair-verification-failed";
|
|
232
|
-
}
|
|
233
|
-
} catch {
|
|
234
|
-
failure = "model-change-failed";
|
|
235
|
-
}
|
|
236
|
-
if (failure) {
|
|
237
|
-
const restored = await restorePair(pi, ctx, fromPair);
|
|
238
|
-
if (!restored) {
|
|
239
|
-
const rollbackFailed = {
|
|
240
|
-
version: 1,
|
|
241
|
-
phase: "aborted",
|
|
242
|
-
status: "rollback-failed",
|
|
243
|
-
correlationId: request.correlationId,
|
|
244
|
-
fromPair,
|
|
245
|
-
target,
|
|
246
|
-
failure: "rollback-failed",
|
|
247
|
-
...(currentPair(ctx) ? { observedPair: currentPair(ctx) } : {}),
|
|
248
|
-
origin: { source: "pi", operation: "session-model-state-control" },
|
|
249
|
-
};
|
|
250
|
-
appendModelStateCommit(pi, rollbackFailed);
|
|
251
|
-
return { status: "rejected" };
|
|
252
|
-
}
|
|
253
|
-
const aborted = {
|
|
254
|
-
version: 1,
|
|
255
|
-
phase: "aborted",
|
|
256
|
-
status: "rolled-back",
|
|
257
|
-
correlationId: request.correlationId,
|
|
258
|
-
fromPair,
|
|
259
|
-
target,
|
|
260
|
-
failure,
|
|
261
|
-
origin: { source: "pi", operation: "session-model-state-control" },
|
|
262
|
-
};
|
|
263
|
-
appendModelStateCommit(pi, aborted);
|
|
264
|
-
return { status: "rejected" };
|
|
265
|
-
}
|
|
266
|
-
const commit = {
|
|
267
|
-
version: 1,
|
|
268
|
-
phase: "committed",
|
|
269
|
-
status: "applied",
|
|
270
|
-
correlationId: request.correlationId,
|
|
271
|
-
fromPair,
|
|
272
|
-
target,
|
|
273
|
-
origin: { source: "pi", operation: "session-model-state-control" },
|
|
274
|
-
};
|
|
275
|
-
try {
|
|
276
|
-
if (!appendModelStateCommit(pi, commit)) throw new Error("commit persistence failed");
|
|
277
|
-
return { status: "applied" };
|
|
278
|
-
} catch {
|
|
279
|
-
const restored = await restorePair(pi, ctx, fromPair);
|
|
280
|
-
if (!restored) {
|
|
281
|
-
const rollbackFailed = {
|
|
282
|
-
version: 1,
|
|
283
|
-
phase: "aborted",
|
|
284
|
-
status: "rollback-failed",
|
|
285
|
-
correlationId: request.correlationId,
|
|
286
|
-
fromPair,
|
|
287
|
-
target,
|
|
288
|
-
failure: "rollback-failed",
|
|
289
|
-
...(currentPair(ctx) ? { observedPair: currentPair(ctx) } : {}),
|
|
290
|
-
origin: { source: "pi", operation: "session-model-state-control" },
|
|
291
|
-
};
|
|
292
|
-
appendModelStateCommit(pi, rollbackFailed);
|
|
293
|
-
return { status: "rejected" };
|
|
294
|
-
}
|
|
295
|
-
const aborted = {
|
|
296
|
-
version: 1,
|
|
297
|
-
phase: "aborted",
|
|
298
|
-
status: "rolled-back",
|
|
299
|
-
correlationId: request.correlationId,
|
|
300
|
-
fromPair,
|
|
301
|
-
target,
|
|
302
|
-
failure: "commit-persistence-failed",
|
|
303
|
-
origin: { source: "pi", operation: "session-model-state-control" },
|
|
304
|
-
};
|
|
305
|
-
appendModelStateCommit(pi, aborted);
|
|
306
|
-
return { status: "rejected" };
|
|
307
|
-
}
|
|
308
|
-
},
|
|
309
|
-
async release() {
|
|
310
|
-
if (!released) {
|
|
311
|
-
released = true;
|
|
312
|
-
if (owner === label) owner = undefined;
|
|
313
|
-
}
|
|
314
|
-
return { status: "released" };
|
|
315
|
-
},
|
|
316
|
-
},
|
|
317
|
-
};
|
|
318
|
-
},
|
|
319
|
-
};
|
|
320
|
-
}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { resolveCognitiveRoutingConfig } from "./config.js";
|
|
2
|
-
import { preflightCognitiveRoutingProfiles } from "./host.js";
|
|
3
|
-
function blockingReason(code, message) {
|
|
4
|
-
return { code, message };
|
|
5
|
-
}
|
|
6
|
-
function baseState(config) {
|
|
7
|
-
return {
|
|
8
|
-
configured: config.configured,
|
|
9
|
-
configValid: config.valid,
|
|
10
|
-
enabled: config.enabled,
|
|
11
|
-
effective: false,
|
|
12
|
-
enabledSource: config.enabledSource,
|
|
13
|
-
profiles: config.profiles,
|
|
14
|
-
profileSources: config.profileSources,
|
|
15
|
-
sessionStart: config.sessionStart,
|
|
16
|
-
sessionStartSources: config.sessionStartSources,
|
|
17
|
-
resolvedProfiles: {},
|
|
18
|
-
blockingReason: blockingReason("disabled", "Cognitive Routing is disabled"),
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
export async function resolveCognitiveRoutingState(repositoryConfig, personalConfig, host) {
|
|
22
|
-
const config = resolveCognitiveRoutingConfig(repositoryConfig, personalConfig);
|
|
23
|
-
const state = baseState(config);
|
|
24
|
-
if (!config.valid) {
|
|
25
|
-
return {
|
|
26
|
-
...state,
|
|
27
|
-
blockingReason: blockingReason(
|
|
28
|
-
"config_invalid",
|
|
29
|
-
config.error?.message ?? "Cognitive Routing configuration is invalid",
|
|
30
|
-
),
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
if (!config.enabled) {
|
|
34
|
-
return state;
|
|
35
|
-
}
|
|
36
|
-
const preflight = await preflightCognitiveRoutingProfiles(config, host);
|
|
37
|
-
return {
|
|
38
|
-
...state,
|
|
39
|
-
effective: preflight.effective,
|
|
40
|
-
resolvedProfiles: preflight.resolvedProfiles,
|
|
41
|
-
blockingReason: preflight.blockingReason ?? blockingReason("disabled", "Cognitive Routing is not effective"),
|
|
42
|
-
};
|
|
43
|
-
}
|
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
import { textComponent } from "../ui/text-component.js";
|
|
2
|
-
export const COGNITIVE_ROUTING_SWITCH_TOOL_NAME = "freeflow_switch_profile";
|
|
3
|
-
export const COGNITIVE_ROUTING_HISTORY_TOOL_NAME = "freeflow_cognitive_routing_history";
|
|
4
|
-
export const COGNITIVE_ROUTING_HISTORY_PARAMETERS = {
|
|
5
|
-
type: "object",
|
|
6
|
-
additionalProperties: false,
|
|
7
|
-
properties: {
|
|
8
|
-
scope: {
|
|
9
|
-
type: "string",
|
|
10
|
-
enum: ["session", "active-branch"],
|
|
11
|
-
description: "History scope to read.",
|
|
12
|
-
},
|
|
13
|
-
anomaliesOnly: {
|
|
14
|
-
type: "boolean",
|
|
15
|
-
description: "Return only events whose integrity is anomaly.",
|
|
16
|
-
},
|
|
17
|
-
limit: {
|
|
18
|
-
type: "integer",
|
|
19
|
-
minimum: 1,
|
|
20
|
-
maximum: 100,
|
|
21
|
-
description: "Maximum number of newest events to return.",
|
|
22
|
-
},
|
|
23
|
-
},
|
|
24
|
-
required: [],
|
|
25
|
-
};
|
|
26
|
-
export const COGNITIVE_ROUTING_SWITCH_PARAMETERS = {
|
|
27
|
-
type: "object",
|
|
28
|
-
additionalProperties: false,
|
|
29
|
-
properties: {
|
|
30
|
-
target: {
|
|
31
|
-
type: "string",
|
|
32
|
-
enum: ["standard", "reasoning"],
|
|
33
|
-
description: "The configured Cognitive Routing profile to activate.",
|
|
34
|
-
},
|
|
35
|
-
reason: {
|
|
36
|
-
type: "string",
|
|
37
|
-
minLength: 1,
|
|
38
|
-
maxLength: 160,
|
|
39
|
-
description: "A concise one-line reason for the profile transition.",
|
|
40
|
-
},
|
|
41
|
-
},
|
|
42
|
-
required: ["target", "reason"],
|
|
43
|
-
};
|
|
44
|
-
function resultText(result) {
|
|
45
|
-
if (result.status === "active") {
|
|
46
|
-
const fields = [COGNITIVE_ROUTING_SWITCH_TOOL_NAME + "|active", `profile|${result.profile}`];
|
|
47
|
-
if (result.changed !== undefined) fields.push(`changed|${result.changed}`);
|
|
48
|
-
if (result.from) fields.push(`from|${result.from}`);
|
|
49
|
-
if (result.to) fields.push(`to|${result.to}`);
|
|
50
|
-
return fields.join("\n");
|
|
51
|
-
}
|
|
52
|
-
return `${COGNITIVE_ROUTING_SWITCH_TOOL_NAME}|${result.status}\nreason|${result.reason}`;
|
|
53
|
-
}
|
|
54
|
-
function blocked(reason) {
|
|
55
|
-
return { status: "blocked", reason };
|
|
56
|
-
}
|
|
57
|
-
function renderNothing() {
|
|
58
|
-
return {
|
|
59
|
-
render() {
|
|
60
|
-
return [];
|
|
61
|
-
},
|
|
62
|
-
invalidate() {},
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
function renderTransition(text, theme) {
|
|
66
|
-
return textComponent(theme?.fg ? theme.fg("accent", text) : text);
|
|
67
|
-
}
|
|
68
|
-
function isProfileName(value) {
|
|
69
|
-
return value === "standard" || value === "reasoning";
|
|
70
|
-
}
|
|
71
|
-
function resultFromPayload(payload) {
|
|
72
|
-
const details = payload?.details?.result ?? payload?.result ?? payload?.details;
|
|
73
|
-
if (details && (details.status === "active" || details.status === "blocked" || details.status === "inactive")) {
|
|
74
|
-
if (details.status !== "active") return details;
|
|
75
|
-
if (!isProfileName(details.profile)) return undefined;
|
|
76
|
-
return {
|
|
77
|
-
status: "active",
|
|
78
|
-
profile: details.profile,
|
|
79
|
-
...(typeof details.changed === "boolean" ? { changed: details.changed } : {}),
|
|
80
|
-
...(isProfileName(details.from) ? { from: details.from } : {}),
|
|
81
|
-
...(isProfileName(details.to) ? { to: details.to } : {}),
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
const text = Array.isArray(payload?.content)
|
|
85
|
-
? payload.content.find((part) => part?.type === "text")?.text
|
|
86
|
-
: undefined;
|
|
87
|
-
if (typeof text !== "string") return undefined;
|
|
88
|
-
const fields = new Map();
|
|
89
|
-
for (const line of text.split("\\n")) {
|
|
90
|
-
const separator = line.indexOf("|");
|
|
91
|
-
if (separator <= 0) continue;
|
|
92
|
-
fields.set(line.slice(0, separator), line.slice(separator + 1));
|
|
93
|
-
}
|
|
94
|
-
const status = fields.get(COGNITIVE_ROUTING_SWITCH_TOOL_NAME);
|
|
95
|
-
if (status === "active") {
|
|
96
|
-
const profile = fields.get("profile");
|
|
97
|
-
if (!isProfileName(profile)) return undefined;
|
|
98
|
-
const changed = fields.get("changed");
|
|
99
|
-
const from = fields.get("from");
|
|
100
|
-
const to = fields.get("to");
|
|
101
|
-
return {
|
|
102
|
-
status,
|
|
103
|
-
profile,
|
|
104
|
-
...(changed === "true" || changed === "false" ? { changed: changed === "true" } : {}),
|
|
105
|
-
...(isProfileName(from) ? { from } : {}),
|
|
106
|
-
...(isProfileName(to) ? { to } : {}),
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
if (status === "blocked" || status === "inactive") {
|
|
110
|
-
const reason = fields.get("reason");
|
|
111
|
-
return reason ? { status, reason } : undefined;
|
|
112
|
-
}
|
|
113
|
-
return undefined;
|
|
114
|
-
}
|
|
115
|
-
function historyOptions(params) {
|
|
116
|
-
return {
|
|
117
|
-
...(params.scope === "active-branch" ? { scope: "active-branch" } : {}),
|
|
118
|
-
...(params.anomaliesOnly === true ? { anomaliesOnly: true } : {}),
|
|
119
|
-
...(Number.isInteger(params.limit) ? { limit: Math.max(1, Math.min(100, params.limit)) } : {}),
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
function validateParams(params) {
|
|
123
|
-
if (params.target !== "standard" && params.target !== "reasoning") {
|
|
124
|
-
return { status: "invalid", reason: "target_must_be_standard_or_reasoning" };
|
|
125
|
-
}
|
|
126
|
-
if (typeof params.reason !== "string") {
|
|
127
|
-
return { status: "invalid", reason: "reason_required" };
|
|
128
|
-
}
|
|
129
|
-
const reason = params.reason.trim();
|
|
130
|
-
if (!reason) return { status: "invalid", reason: "reason_required" };
|
|
131
|
-
if (reason.includes("\n") || reason.includes("\r"))
|
|
132
|
-
return { status: "invalid", reason: "reason_must_be_single_line" };
|
|
133
|
-
if (reason.length > 160) return { status: "invalid", reason: "reason_too_long" };
|
|
134
|
-
return { status: "valid", target: params.target, reason };
|
|
135
|
-
}
|
|
136
|
-
export function registerCognitiveRoutingTool(pi, getController) {
|
|
137
|
-
pi.registerTool({
|
|
138
|
-
name: COGNITIVE_ROUTING_SWITCH_TOOL_NAME,
|
|
139
|
-
label: "Switch Cognitive Profile",
|
|
140
|
-
description: "Request one bounded Cognitive Routing profile transition while automatic control is active.",
|
|
141
|
-
promptSnippet: "Switch between the configured standard and reasoning profiles when the current task needs it.",
|
|
142
|
-
promptGuidelines: [
|
|
143
|
-
"Use only when the current task needs a different reasoning profile.",
|
|
144
|
-
"Give one concise sentence as the reason and make this the only tool call in the response.",
|
|
145
|
-
"Do not call this tool while a deterministic manual profile hold is active.",
|
|
146
|
-
],
|
|
147
|
-
parameters: COGNITIVE_ROUTING_SWITCH_PARAMETERS,
|
|
148
|
-
executionMode: "sequential",
|
|
149
|
-
async execute(_toolCallId, params, _signal, _onUpdate, _ctx) {
|
|
150
|
-
const controller = getController();
|
|
151
|
-
const input = validateParams(params ?? {});
|
|
152
|
-
let result;
|
|
153
|
-
if (input.status === "invalid") {
|
|
154
|
-
result = blocked(input.reason);
|
|
155
|
-
} else if (!controller) {
|
|
156
|
-
result = blocked("not_available");
|
|
157
|
-
} else if (!controller.state().effective) {
|
|
158
|
-
result = blocked("not_active");
|
|
159
|
-
} else if (controller.state().controlMode === "automatic") {
|
|
160
|
-
result = await controller.switchAutomaticProfile(input.target, input.reason);
|
|
161
|
-
} else {
|
|
162
|
-
result = blocked("manual_hold");
|
|
163
|
-
}
|
|
164
|
-
return {
|
|
165
|
-
content: [{ type: "text", text: resultText(result) }],
|
|
166
|
-
details: { result },
|
|
167
|
-
};
|
|
168
|
-
},
|
|
169
|
-
renderCall(_args, _theme) {
|
|
170
|
-
return renderNothing();
|
|
171
|
-
},
|
|
172
|
-
renderResult(result, _options, theme) {
|
|
173
|
-
const outcome = resultFromPayload(result);
|
|
174
|
-
if (outcome?.status === "active") {
|
|
175
|
-
if (outcome.changed === true && outcome.from && outcome.to) {
|
|
176
|
-
return renderTransition(`Cognitive Routing: ${outcome.from} → ${outcome.to}`, theme);
|
|
177
|
-
}
|
|
178
|
-
if (outcome.changed === false && outcome.to) {
|
|
179
|
-
return renderTransition(`Cognitive Routing: ${outcome.to} (already active)`, theme);
|
|
180
|
-
}
|
|
181
|
-
return renderTransition(`Cognitive Routing: active · ${outcome.profile}`, theme);
|
|
182
|
-
}
|
|
183
|
-
if (outcome) {
|
|
184
|
-
return renderTransition(`Cognitive Routing: ${outcome.status} · ${outcome.reason}`, theme);
|
|
185
|
-
}
|
|
186
|
-
return renderTransition(`Cognitive Routing: ${result?.isError ? "error" : "result unavailable"}`, theme);
|
|
187
|
-
},
|
|
188
|
-
});
|
|
189
|
-
}
|
|
190
|
-
export function registerCognitiveRoutingHistoryTool(pi, readHistory) {
|
|
191
|
-
pi.registerTool({
|
|
192
|
-
name: COGNITIVE_ROUTING_HISTORY_TOOL_NAME,
|
|
193
|
-
label: "Read Cognitive Routing History",
|
|
194
|
-
description:
|
|
195
|
-
"Read the deterministic Cognitive Routing transition history without changing routing or session state.",
|
|
196
|
-
promptSnippet: "Inspect Cognitive Routing transition history when debugging or monitoring profile behavior.",
|
|
197
|
-
promptGuidelines: [
|
|
198
|
-
"Use only when debugging or monitoring Cognitive Routing is requested.",
|
|
199
|
-
"This tool is read-only and does not change profile, control, branch, or session state.",
|
|
200
|
-
"Use scope=active-branch to inspect only the current branch ancestry.",
|
|
201
|
-
"Use anomaliesOnly=true to inspect only integrity anomalies.",
|
|
202
|
-
],
|
|
203
|
-
parameters: COGNITIVE_ROUTING_HISTORY_PARAMETERS,
|
|
204
|
-
async execute(_toolCallId, params, _signal, _onUpdate, context) {
|
|
205
|
-
const result = await readHistory(historyOptions(params ?? {}), context);
|
|
206
|
-
return {
|
|
207
|
-
content: [{ type: "text", text: `Cognitive Routing history\n${JSON.stringify(result)}` }],
|
|
208
|
-
details: { result },
|
|
209
|
-
};
|
|
210
|
-
},
|
|
211
|
-
});
|
|
212
|
-
}
|