@vinkius-core/mcp-fusion 2.7.0 → 2.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/dist/cli/fusion.d.ts +101 -0
  2. package/dist/cli/fusion.d.ts.map +1 -0
  3. package/dist/cli/fusion.js +333 -0
  4. package/dist/cli/fusion.js.map +1 -0
  5. package/dist/index.d.ts +41 -1
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +22 -0
  8. package/dist/index.js.map +1 -1
  9. package/dist/introspection/BehaviorDigest.d.ts +112 -0
  10. package/dist/introspection/BehaviorDigest.d.ts.map +1 -0
  11. package/dist/introspection/BehaviorDigest.js +146 -0
  12. package/dist/introspection/BehaviorDigest.js.map +1 -0
  13. package/dist/introspection/CapabilityLockfile.d.ts +259 -0
  14. package/dist/introspection/CapabilityLockfile.d.ts.map +1 -0
  15. package/dist/introspection/CapabilityLockfile.js +391 -0
  16. package/dist/introspection/CapabilityLockfile.js.map +1 -0
  17. package/dist/introspection/ContractAwareSelfHealing.d.ts +90 -0
  18. package/dist/introspection/ContractAwareSelfHealing.d.ts.map +1 -0
  19. package/dist/introspection/ContractAwareSelfHealing.js +132 -0
  20. package/dist/introspection/ContractAwareSelfHealing.js.map +1 -0
  21. package/dist/introspection/ContractDiff.d.ts +91 -0
  22. package/dist/introspection/ContractDiff.d.ts.map +1 -0
  23. package/dist/introspection/ContractDiff.js +466 -0
  24. package/dist/introspection/ContractDiff.js.map +1 -0
  25. package/dist/introspection/CryptoAttestation.d.ts +143 -0
  26. package/dist/introspection/CryptoAttestation.d.ts.map +1 -0
  27. package/dist/introspection/CryptoAttestation.js +194 -0
  28. package/dist/introspection/CryptoAttestation.js.map +1 -0
  29. package/dist/introspection/EntitlementScanner.d.ts +124 -0
  30. package/dist/introspection/EntitlementScanner.d.ts.map +1 -0
  31. package/dist/introspection/EntitlementScanner.js +244 -0
  32. package/dist/introspection/EntitlementScanner.js.map +1 -0
  33. package/dist/introspection/GovernanceObserver.d.ts +88 -0
  34. package/dist/introspection/GovernanceObserver.d.ts.map +1 -0
  35. package/dist/introspection/GovernanceObserver.js +132 -0
  36. package/dist/introspection/GovernanceObserver.js.map +1 -0
  37. package/dist/introspection/SemanticProbe.d.ts +207 -0
  38. package/dist/introspection/SemanticProbe.d.ts.map +1 -0
  39. package/dist/introspection/SemanticProbe.js +255 -0
  40. package/dist/introspection/SemanticProbe.js.map +1 -0
  41. package/dist/introspection/TokenEconomics.d.ts +210 -0
  42. package/dist/introspection/TokenEconomics.d.ts.map +1 -0
  43. package/dist/introspection/TokenEconomics.js +286 -0
  44. package/dist/introspection/TokenEconomics.js.map +1 -0
  45. package/dist/introspection/ToolContract.d.ts +159 -0
  46. package/dist/introspection/ToolContract.d.ts.map +1 -0
  47. package/dist/introspection/ToolContract.js +191 -0
  48. package/dist/introspection/ToolContract.js.map +1 -0
  49. package/dist/introspection/canonicalize.d.ts +20 -0
  50. package/dist/introspection/canonicalize.d.ts.map +1 -0
  51. package/dist/introspection/canonicalize.js +51 -0
  52. package/dist/introspection/canonicalize.js.map +1 -0
  53. package/dist/introspection/index.d.ts +20 -0
  54. package/dist/introspection/index.d.ts.map +1 -1
  55. package/dist/introspection/index.js +20 -0
  56. package/dist/introspection/index.js.map +1 -1
  57. package/dist/observability/DebugObserver.d.ts +26 -1
  58. package/dist/observability/DebugObserver.d.ts.map +1 -1
  59. package/dist/observability/DebugObserver.js +8 -1
  60. package/dist/observability/DebugObserver.js.map +1 -1
  61. package/dist/observability/index.d.ts +1 -1
  62. package/dist/observability/index.d.ts.map +1 -1
  63. package/dist/observability/index.js.map +1 -1
  64. package/dist/server/ServerAttachment.d.ts +41 -0
  65. package/dist/server/ServerAttachment.d.ts.map +1 -1
  66. package/dist/server/ServerAttachment.js +25 -1
  67. package/dist/server/ServerAttachment.js.map +1 -1
  68. package/package.json +8 -1
@@ -0,0 +1,207 @@
1
+ /**
2
+ * SemanticProbe — LLM-as-a-Judge for Opaque Behavior Detection
3
+ *
4
+ * **Evolution 2: Semantic Probing**
5
+ *
6
+ * Provides a framework for using an LLM to evaluate whether
7
+ * a tool handler's actual runtime behavior matches its declared
8
+ * behavioral contract. This detects "semantic drift" — situations
9
+ * where the handler's output changes meaning even when the
10
+ * egress schema and system rules remain structurally identical.
11
+ *
12
+ * **Architecture**: This module defines the probe protocol,
13
+ * types, and evaluation pipeline. The actual LLM invocation
14
+ * is delegated to user-provided adapters — the module never
15
+ * makes LLM calls directly, maintaining the "no hidden
16
+ * network dependencies" principle.
17
+ *
18
+ * **Testing integration**: Designed to be integrated with
19
+ * `FusionTester.callAction()` for automated regression
20
+ * testing: "given these inputs, does the output semantically
21
+ * match the previous known-good output?"
22
+ *
23
+ * Pure-function module for probe construction and evaluation;
24
+ * LLM interaction is async via pluggable adapters.
25
+ *
26
+ * @module
27
+ */
28
+ /**
29
+ * Configuration for semantic probing.
30
+ */
31
+ export interface SemanticProbeConfig {
32
+ /** The LLM adapter to use for evaluation */
33
+ readonly adapter: SemanticProbeAdapter;
34
+ /** Risk thresholds for classification */
35
+ readonly thresholds?: Partial<SemanticThresholds>;
36
+ /** Maximum number of probes to run in parallel */
37
+ readonly concurrency?: number;
38
+ /** Whether to include raw LLM responses in results */
39
+ readonly includeRawResponses?: boolean;
40
+ }
41
+ /**
42
+ * Pluggable LLM adapter for semantic evaluation.
43
+ *
44
+ * Implementations should call an LLM with the provided prompt
45
+ * and return the structured evaluation result.
46
+ */
47
+ export interface SemanticProbeAdapter {
48
+ /** Human-readable name (e.g., 'claude-3.5', 'gpt-4o') */
49
+ readonly name: string;
50
+ /**
51
+ * Send a semantic evaluation prompt to the LLM.
52
+ *
53
+ * @param prompt - Complete evaluation prompt
54
+ * @returns Raw LLM response text
55
+ */
56
+ evaluate(prompt: string): Promise<string>;
57
+ }
58
+ /**
59
+ * Thresholds for semantic drift classification.
60
+ */
61
+ export interface SemanticThresholds {
62
+ /** Score below which drift is considered 'high' (default: 0.5) */
63
+ readonly highDriftThreshold: number;
64
+ /** Score below which drift is considered 'medium' (default: 0.75) */
65
+ readonly mediumDriftThreshold: number;
66
+ }
67
+ /**
68
+ * A semantic probe definition — a structured test case
69
+ * for LLM-based behavioral evaluation.
70
+ */
71
+ export interface SemanticProbe {
72
+ /** Unique identifier for this probe */
73
+ readonly id: string;
74
+ /** Tool name being probed */
75
+ readonly toolName: string;
76
+ /** Action key being probed */
77
+ readonly actionKey: string;
78
+ /** Description of what this probe tests */
79
+ readonly description: string;
80
+ /** Input arguments to the tool */
81
+ readonly input: Record<string, unknown>;
82
+ /** Expected output (known-good baseline) */
83
+ readonly expectedOutput: unknown;
84
+ /** Actual output from the current handler */
85
+ readonly actualOutput: unknown;
86
+ /** Behavioral contract context for the judge */
87
+ readonly contractContext: ProbeContractContext;
88
+ }
89
+ /**
90
+ * Contract context injected into the LLM judge prompt.
91
+ *
92
+ * Provides the judge with enough information to evaluate
93
+ * whether the behavioral contract was violated.
94
+ */
95
+ export interface ProbeContractContext {
96
+ /** Tool description */
97
+ readonly description: string | undefined;
98
+ /** Whether the action is declared readOnly */
99
+ readonly readOnly: boolean;
100
+ /** Whether the action is declared destructive */
101
+ readonly destructive: boolean;
102
+ /** System rules that should be respected */
103
+ readonly systemRules: readonly string[];
104
+ /** Schema field names (expected output shape) */
105
+ readonly schemaKeys: readonly string[];
106
+ }
107
+ /**
108
+ * Result of a single semantic probe evaluation.
109
+ */
110
+ export interface SemanticProbeResult {
111
+ /** The probe that was evaluated */
112
+ readonly probe: SemanticProbe;
113
+ /** Semantic similarity score (0.0 = completely different, 1.0 = identical) */
114
+ readonly similarityScore: number;
115
+ /** Drift classification */
116
+ readonly driftLevel: DriftLevel;
117
+ /** Whether the behavioral contract was violated */
118
+ readonly contractViolated: boolean;
119
+ /** Specific violations detected by the judge */
120
+ readonly violations: readonly string[];
121
+ /** LLM judge's reasoning */
122
+ readonly reasoning: string;
123
+ /** Raw LLM response (if configured) */
124
+ readonly rawResponse: string | null;
125
+ /** ISO-8601 timestamp of evaluation */
126
+ readonly evaluatedAt: string;
127
+ }
128
+ /** Drift level classification */
129
+ export type DriftLevel = 'none' | 'low' | 'medium' | 'high';
130
+ /**
131
+ * Aggregated result of multiple semantic probes.
132
+ */
133
+ export interface SemanticProbeReport {
134
+ /** Tool name */
135
+ readonly toolName: string;
136
+ /** All individual probe results */
137
+ readonly results: readonly SemanticProbeResult[];
138
+ /** Overall drift assessment */
139
+ readonly overallDrift: DriftLevel;
140
+ /** Number of contract violations */
141
+ readonly violationCount: number;
142
+ /** Whether the tool is considered semantically stable */
143
+ readonly stable: boolean;
144
+ /** Human-readable summary */
145
+ readonly summary: string;
146
+ /** ISO-8601 timestamp */
147
+ readonly completedAt: string;
148
+ }
149
+ /**
150
+ * Create a semantic probe from input/output pairs.
151
+ *
152
+ * @param toolName - Tool name
153
+ * @param actionKey - Action key
154
+ * @param input - Input arguments
155
+ * @param expectedOutput - Known-good baseline output
156
+ * @param actualOutput - Current handler output
157
+ * @param contractContext - Behavioral contract context
158
+ * @returns A structured semantic probe
159
+ */
160
+ export declare function createProbe(toolName: string, actionKey: string, input: Record<string, unknown>, expectedOutput: unknown, actualOutput: unknown, contractContext: ProbeContractContext): SemanticProbe;
161
+ /**
162
+ * Build the evaluation prompt for the LLM judge.
163
+ *
164
+ * The prompt is structured to elicit a JSON-formatted response
165
+ * with specific fields for programmatic parsing.
166
+ *
167
+ * @param probe - The semantic probe to evaluate
168
+ * @returns Complete evaluation prompt
169
+ */
170
+ export declare function buildJudgePrompt(probe: SemanticProbe): string;
171
+ /**
172
+ * Parse the LLM judge's response into a structured result.
173
+ *
174
+ * Handles malformed responses gracefully by falling back
175
+ * to conservative defaults.
176
+ *
177
+ * @param probe - The probe that was evaluated
178
+ * @param rawResponse - Raw LLM response text
179
+ * @param config - Probe configuration
180
+ * @returns Structured probe result
181
+ */
182
+ export declare function parseJudgeResponse(probe: SemanticProbe, rawResponse: string, config: SemanticProbeConfig): SemanticProbeResult;
183
+ /**
184
+ * Run a complete semantic probe evaluation.
185
+ *
186
+ * @param probe - The probe to evaluate
187
+ * @param config - Probe configuration (includes LLM adapter)
188
+ * @returns Evaluation result
189
+ */
190
+ export declare function evaluateProbe(probe: SemanticProbe, config: SemanticProbeConfig): Promise<SemanticProbeResult>;
191
+ /**
192
+ * Run multiple probes and aggregate results.
193
+ *
194
+ * @param probes - Array of probes to evaluate
195
+ * @param config - Probe configuration
196
+ * @returns Aggregated report
197
+ */
198
+ export declare function evaluateProbes(probes: readonly SemanticProbe[], config: SemanticProbeConfig): Promise<SemanticProbeReport>;
199
+ /**
200
+ * Aggregate individual probe results into a report.
201
+ *
202
+ * @param toolName - Tool name
203
+ * @param results - Individual probe results
204
+ * @returns Aggregated report
205
+ */
206
+ export declare function aggregateResults(toolName: string, results: readonly SemanticProbeResult[]): SemanticProbeReport;
207
+ //# sourceMappingURL=SemanticProbe.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SemanticProbe.d.ts","sourceRoot":"","sources":["../../src/introspection/SemanticProbe.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAMH;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAChC,4CAA4C;IAC5C,QAAQ,CAAC,OAAO,EAAE,oBAAoB,CAAC;IACvC,yCAAyC;IACzC,QAAQ,CAAC,UAAU,CAAC,EAAE,OAAO,CAAC,kBAAkB,CAAC,CAAC;IAClD,kDAAkD;IAClD,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,sDAAsD;IACtD,QAAQ,CAAC,mBAAmB,CAAC,EAAE,OAAO,CAAC;CAC1C;AAED;;;;;GAKG;AACH,MAAM,WAAW,oBAAoB;IACjC,yDAAyD;IACzD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB;;;;;OAKG;IACH,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CAC7C;AAED;;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gB,CAAC,KAAK,EAAE,aAAa,GAAG,MAAM,CAqD7D;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CAC9B,KAAK,EAAE,aAAa,EACpB,WAAW,EAAE,MAAM,EACnB,MAAM,EAAE,mBAAmB,GAC5B,mBAAmB,CAoCrB;AAED;;;;;;GAMG;AACH,wBAAsB,aAAa,CAC/B,KAAK,EAAE,aAAa,EACpB,MAAM,EAAE,mBAAmB,GAC5B,OAAO,CAAC,mBAAmB,CAAC,CAI9B;AAED;;;;;;GAMG;AACH,wBAAsB,cAAc,CAChC,MAAM,EAAE,SAAS,aAAa,EAAE,EAChC,MAAM,EAAE,mBAAmB,GAC5B,OAAO,CAAC,mBAAmB,CAAC,CAc9B;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAC5B,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,SAAS,mBAAmB,EAAE,GACxC,mBAAmB,CA8BrB"}
@@ -0,0 +1,255 @@
1
+ /**
2
+ * SemanticProbe — LLM-as-a-Judge for Opaque Behavior Detection
3
+ *
4
+ * **Evolution 2: Semantic Probing**
5
+ *
6
+ * Provides a framework for using an LLM to evaluate whether
7
+ * a tool handler's actual runtime behavior matches its declared
8
+ * behavioral contract. This detects "semantic drift" — situations
9
+ * where the handler's output changes meaning even when the
10
+ * egress schema and system rules remain structurally identical.
11
+ *
12
+ * **Architecture**: This module defines the probe protocol,
13
+ * types, and evaluation pipeline. The actual LLM invocation
14
+ * is delegated to user-provided adapters — the module never
15
+ * makes LLM calls directly, maintaining the "no hidden
16
+ * network dependencies" principle.
17
+ *
18
+ * **Testing integration**: Designed to be integrated with
19
+ * `FusionTester.callAction()` for automated regression
20
+ * testing: "given these inputs, does the output semantically
21
+ * match the previous known-good output?"
22
+ *
23
+ * Pure-function module for probe construction and evaluation;
24
+ * LLM interaction is async via pluggable adapters.
25
+ *
26
+ * @module
27
+ */
28
+ // ============================================================================
29
+ // Probe Construction
30
+ // ============================================================================
31
+ /**
32
+ * Create a semantic probe from input/output pairs.
33
+ *
34
+ * @param toolName - Tool name
35
+ * @param actionKey - Action key
36
+ * @param input - Input arguments
37
+ * @param expectedOutput - Known-good baseline output
38
+ * @param actualOutput - Current handler output
39
+ * @param contractContext - Behavioral contract context
40
+ * @returns A structured semantic probe
41
+ */
42
+ export function createProbe(toolName, actionKey, input, expectedOutput, actualOutput, contractContext) {
43
+ const id = `${toolName}::${actionKey}::${Date.now()}`;
44
+ return {
45
+ id,
46
+ toolName,
47
+ actionKey,
48
+ description: `Semantic probe for ${toolName}.${actionKey}`,
49
+ input,
50
+ expectedOutput,
51
+ actualOutput,
52
+ contractContext,
53
+ };
54
+ }
55
+ /**
56
+ * Build the evaluation prompt for the LLM judge.
57
+ *
58
+ * The prompt is structured to elicit a JSON-formatted response
59
+ * with specific fields for programmatic parsing.
60
+ *
61
+ * @param probe - The semantic probe to evaluate
62
+ * @returns Complete evaluation prompt
63
+ */
64
+ export function buildJudgePrompt(probe) {
65
+ return `You are a semantic evaluation judge for an MCP (Model Context Protocol) tool.
66
+
67
+ Your task is to compare two outputs from the same tool handler and determine:
68
+ 1. Whether they are semantically equivalent
69
+ 2. Whether the current output violates the tool's behavioral contract
70
+
71
+ ## Tool Information
72
+ - **Tool**: ${probe.toolName}
73
+ - **Action**: ${probe.actionKey}
74
+ - **Description**: ${probe.contractContext.description ?? 'No description'}
75
+ - **Read-Only**: ${probe.contractContext.readOnly}
76
+ - **Destructive**: ${probe.contractContext.destructive}
77
+
78
+ ## Behavioral Contract
79
+ ${probe.contractContext.systemRules.length > 0
80
+ ? `### System Rules\n${probe.contractContext.systemRules.map((r, i) => `${i + 1}. ${r}`).join('\n')}`
81
+ : 'No system rules declared.'}
82
+
83
+ ### Expected Output Schema Fields
84
+ ${probe.contractContext.schemaKeys.join(', ') || 'No schema declared'}
85
+
86
+ ## Input Arguments
87
+ \`\`\`json
88
+ ${JSON.stringify(probe.input, null, 2)}
89
+ \`\`\`
90
+
91
+ ## Expected Output (Baseline)
92
+ \`\`\`json
93
+ ${JSON.stringify(probe.expectedOutput, null, 2)}
94
+ \`\`\`
95
+
96
+ ## Actual Output (Current)
97
+ \`\`\`json
98
+ ${JSON.stringify(probe.actualOutput, null, 2)}
99
+ \`\`\`
100
+
101
+ ## Evaluation Instructions
102
+ Compare the Expected Output with the Actual Output. Consider:
103
+ - Are the outputs semantically equivalent (same meaning, even if format differs)?
104
+ - Does the Actual Output violate any system rules?
105
+ - Does the Actual Output return fields not in the expected schema?
106
+ - Has the behavior meaningfully changed from the baseline?
107
+
108
+ Respond with ONLY a JSON object in this exact format:
109
+ \`\`\`json
110
+ {
111
+ "similarityScore": <number 0.0-1.0>,
112
+ "contractViolated": <boolean>,
113
+ "violations": [<string descriptions of violations>],
114
+ "reasoning": "<brief explanation of your assessment>"
115
+ }
116
+ \`\`\``;
117
+ }
118
+ /**
119
+ * Parse the LLM judge's response into a structured result.
120
+ *
121
+ * Handles malformed responses gracefully by falling back
122
+ * to conservative defaults.
123
+ *
124
+ * @param probe - The probe that was evaluated
125
+ * @param rawResponse - Raw LLM response text
126
+ * @param config - Probe configuration
127
+ * @returns Structured probe result
128
+ */
129
+ export function parseJudgeResponse(probe, rawResponse, config) {
130
+ const thresholds = resolveThresholds(config);
131
+ try {
132
+ // Extract JSON from response (handle markdown code blocks)
133
+ const jsonMatch = rawResponse.match(/\{[\s\S]*\}/);
134
+ if (!jsonMatch) {
135
+ return fallbackResult(probe, rawResponse, config);
136
+ }
137
+ const parsed = JSON.parse(jsonMatch[0]);
138
+ const similarityScore = typeof parsed.similarityScore === 'number'
139
+ ? Math.max(0, Math.min(1, parsed.similarityScore))
140
+ : 0.5;
141
+ const driftLevel = classifyDrift(similarityScore, thresholds);
142
+ return {
143
+ probe,
144
+ similarityScore,
145
+ driftLevel,
146
+ contractViolated: parsed.contractViolated ?? false,
147
+ violations: parsed.violations ?? [],
148
+ reasoning: parsed.reasoning ?? 'No reasoning provided',
149
+ rawResponse: config.includeRawResponses ? rawResponse : null,
150
+ evaluatedAt: new Date().toISOString(),
151
+ };
152
+ }
153
+ catch {
154
+ return fallbackResult(probe, rawResponse, config);
155
+ }
156
+ }
157
+ /**
158
+ * Run a complete semantic probe evaluation.
159
+ *
160
+ * @param probe - The probe to evaluate
161
+ * @param config - Probe configuration (includes LLM adapter)
162
+ * @returns Evaluation result
163
+ */
164
+ export async function evaluateProbe(probe, config) {
165
+ const prompt = buildJudgePrompt(probe);
166
+ const rawResponse = await config.adapter.evaluate(prompt);
167
+ return parseJudgeResponse(probe, rawResponse, config);
168
+ }
169
+ /**
170
+ * Run multiple probes and aggregate results.
171
+ *
172
+ * @param probes - Array of probes to evaluate
173
+ * @param config - Probe configuration
174
+ * @returns Aggregated report
175
+ */
176
+ export async function evaluateProbes(probes, config) {
177
+ const concurrency = config.concurrency ?? 3;
178
+ // Run probes with concurrency control
179
+ const results = [];
180
+ for (let i = 0; i < probes.length; i += concurrency) {
181
+ const batch = probes.slice(i, i + concurrency);
182
+ const batchResults = await Promise.all(batch.map(probe => evaluateProbe(probe, config)));
183
+ results.push(...batchResults);
184
+ }
185
+ return aggregateResults(probes[0]?.toolName ?? 'unknown', results);
186
+ }
187
+ /**
188
+ * Aggregate individual probe results into a report.
189
+ *
190
+ * @param toolName - Tool name
191
+ * @param results - Individual probe results
192
+ * @returns Aggregated report
193
+ */
194
+ export function aggregateResults(toolName, results) {
195
+ const violationCount = results.filter(r => r.contractViolated).length;
196
+ const avgSimilarity = results.length > 0
197
+ ? results.reduce((sum, r) => sum + r.similarityScore, 0) / results.length
198
+ : 1.0;
199
+ const overallDrift = results.length > 0
200
+ ? classifyDrift(avgSimilarity, {
201
+ highDriftThreshold: 0.5,
202
+ mediumDriftThreshold: 0.75,
203
+ })
204
+ : 'none';
205
+ const stable = overallDrift === 'none' || overallDrift === 'low';
206
+ const summary = results.length === 0
207
+ ? 'No probes evaluated.'
208
+ : `${results.length} probes evaluated. Avg similarity: ${(avgSimilarity * 100).toFixed(1)}%. ` +
209
+ `Drift: ${overallDrift}. Violations: ${violationCount}. ` +
210
+ `Status: ${stable ? 'STABLE' : 'UNSTABLE'}`;
211
+ return {
212
+ toolName,
213
+ results,
214
+ overallDrift,
215
+ violationCount,
216
+ stable,
217
+ summary,
218
+ completedAt: new Date().toISOString(),
219
+ };
220
+ }
221
+ // ============================================================================
222
+ // Internals
223
+ // ============================================================================
224
+ const DEFAULT_THRESHOLDS = {
225
+ highDriftThreshold: 0.5,
226
+ mediumDriftThreshold: 0.75,
227
+ };
228
+ function resolveThresholds(config) {
229
+ return {
230
+ highDriftThreshold: config.thresholds?.highDriftThreshold ?? DEFAULT_THRESHOLDS.highDriftThreshold,
231
+ mediumDriftThreshold: config.thresholds?.mediumDriftThreshold ?? DEFAULT_THRESHOLDS.mediumDriftThreshold,
232
+ };
233
+ }
234
+ function classifyDrift(similarity, thresholds) {
235
+ if (similarity >= 0.95)
236
+ return 'none';
237
+ if (similarity >= thresholds.mediumDriftThreshold)
238
+ return 'low';
239
+ if (similarity >= thresholds.highDriftThreshold)
240
+ return 'medium';
241
+ return 'high';
242
+ }
243
+ function fallbackResult(probe, rawResponse, config) {
244
+ return {
245
+ probe,
246
+ similarityScore: 0.5,
247
+ driftLevel: 'medium',
248
+ contractViolated: false,
249
+ violations: ['Unable to parse LLM judge response'],
250
+ reasoning: 'Fallback: LLM response could not be parsed as JSON',
251
+ rawResponse: config.includeRawResponses ? rawResponse : null,
252
+ evaluatedAt: new Date().toISOString(),
253
+ };
254
+ }
255
+ //# sourceMappingURL=SemanticProbe.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SemanticProbe.js","sourceRoot":"","sources":["../../src/introspection/SemanticProbe.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAuIH,+EAA+E;AAC/E,qBAAqB;AACrB,+EAA+E;AAE/E;;;;;;;;;;GAUG;AACH,MAAM,UAAU,WAAW,CACvB,QAAgB,EAChB,SAAiB,EACjB,KAA8B,E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B,IAAI,kBAAkB,CAAC,oBAAoB;KAC3G,CAAC;AACN,CAAC;AAED,SAAS,aAAa,CAAC,UAAkB,EAAE,UAA8B;IACrE,IAAI,UAAU,IAAI,IAAI;QAAE,OAAO,MAAM,CAAC;IACtC,IAAI,UAAU,IAAI,UAAU,CAAC,oBAAoB;QAAE,OAAO,KAAK,CAAC;IAChE,IAAI,UAAU,IAAI,UAAU,CAAC,kBAAkB;QAAE,OAAO,QAAQ,CAAC;IACjE,OAAO,MAAM,CAAC;AAClB,CAAC;AAED,SAAS,cAAc,CACnB,KAAoB,EACpB,WAAmB,EACnB,MAA2B;IAE3B,OAAO;QACH,KAAK;QACL,eAAe,EAAE,GAAG;QACpB,UAAU,EAAE,QAAQ;QACpB,gBAAgB,EAAE,KAAK;QACvB,UAAU,EAAE,CAAC,oCAAoC,CAAC;QAClD,SAAS,EAAE,oDAAoD;QAC/D,WAAW,EAAE,MAAM,CAAC,mBAAmB,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI;QAC5D,WAAW,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;KACxC,CAAC;AACN,CAAC"}
@@ -0,0 +1,210 @@
1
+ /**
2
+ * TokenEconomics — Cognitive Overload Detection
3
+ *
4
+ * **Evolution 3: Token Economics**
5
+ *
6
+ * Profiles the token density and context spread of MCP tool
7
+ * responses to detect cognitive overload scenarios where the
8
+ * LLM's working memory is flooded by verbose tool output,
9
+ * evicting system rules and degrading reasoning quality.
10
+ *
11
+ * **Key insight**: An MCP tool that returns 50KB of JSON per
12
+ * call will rapidly exhaust the context window. If that output
13
+ * isn't constrained by `agentLimit` or `egressMaxBytes`, the
14
+ * system rules injected by the Presenter's `addRules()` will
15
+ * be pushed out of the LLM's attention window — silently
16
+ * degrading behavioral correctness.
17
+ *
18
+ * This module provides:
19
+ *
20
+ * 1. **Static analysis**: Estimate token density from Presenter
21
+ * schema and guardrail configuration (zero-cost at runtime).
22
+ *
23
+ * 2. **Runtime profiling**: Measure actual token counts of
24
+ * response blocks after Presenter rendering (opt-in).
25
+ *
26
+ * 3. **Overload classification**: Classify responses into
27
+ * risk levels based on configurable thresholds.
28
+ *
29
+ * 4. **Integration with BehaviorDigest**: Token economics risk
30
+ * level is part of the behavioral contract — changes to
31
+ * the token profile are tracked as contract deltas.
32
+ *
33
+ * Pure-function module for analysis; runtime profiling hooks
34
+ * are designed for zero-overhead when not configured.
35
+ *
36
+ * @module
37
+ */
38
+ /**
39
+ * Token economics analysis for a single tool response.
40
+ */
41
+ export interface TokenAnalysis {
42
+ /** Tool name */
43
+ readonly toolName: string;
44
+ /** Action key, if applicable */
45
+ readonly actionKey: string | null;
46
+ /** Estimated total tokens in the response */
47
+ readonly estimatedTokens: number;
48
+ /** Number of content blocks in the response */
49
+ readonly blockCount: number;
50
+ /** Per-block token breakdown */
51
+ readonly blocks: readonly BlockTokenProfile[];
52
+ /** Overhead tokens (rules, affordances, UI decorators) */
53
+ readonly overheadTokens: number;
54
+ /** Data payload tokens (actual tool output) */
55
+ readonly dataTokens: number;
56
+ /** Overhead-to-data ratio (higher = more overhead) */
57
+ readonly overheadRatio: number;
58
+ /** Risk classification */
59
+ readonly risk: TokenRisk;
60
+ /** Human-readable advisory */
61
+ readonly advisory: string | null;
62
+ }
63
+ /**
64
+ * Token profile for a single content block.
65
+ */
66
+ export interface BlockTokenProfile {
67
+ /** Block type (e.g., 'text', 'resource', 'image') */
68
+ readonly type: string;
69
+ /** Estimated tokens in this block */
70
+ readonly estimatedTokens: number;
71
+ /** Raw byte size */
72
+ readonly bytes: number;
73
+ }
74
+ /**
75
+ * Token risk classification.
76
+ */
77
+ export type TokenRisk = 'low' | 'medium' | 'high' | 'critical';
78
+ /**
79
+ * Thresholds for token risk classification.
80
+ */
81
+ export interface TokenThresholds {
82
+ /** Maximum tokens for 'low' risk (default: 1000) */
83
+ readonly low: number;
84
+ /** Maximum tokens for 'medium' risk (default: 4000) */
85
+ readonly medium: number;
86
+ /** Maximum tokens for 'high' risk (default: 8000) */
87
+ readonly high: number;
88
+ }
89
+ /**
90
+ * Configuration for the token economics profiler.
91
+ */
92
+ export interface TokenEconomicsConfig {
93
+ /** Custom thresholds (defaults provided) */
94
+ readonly thresholds?: Partial<TokenThresholds>;
95
+ /** Whether to emit warnings to debug observer */
96
+ readonly emitWarnings?: boolean;
97
+ /** Maximum acceptable overhead ratio (default: 0.3) */
98
+ readonly maxOverheadRatio?: number;
99
+ }
100
+ /**
101
+ * Static token profile derived from Presenter configuration.
102
+ * Computed once at build time — zero runtime cost.
103
+ */
104
+ export interface StaticTokenProfile {
105
+ /** Tool name */
106
+ readonly toolName: string;
107
+ /** Estimated minimum tokens per response */
108
+ readonly minTokens: number;
109
+ /** Estimated maximum tokens per response (with agentLimit) */
110
+ readonly maxTokens: number;
111
+ /** Whether the maximum is bounded (agentLimit/egressMaxBytes set) */
112
+ readonly bounded: boolean;
113
+ /** Per-field estimated token cost */
114
+ readonly fieldBreakdown: readonly FieldTokenEstimate[];
115
+ /** Risk classification based on max estimate */
116
+ readonly risk: TokenRisk;
117
+ /** Recommendations for reducing token cost */
118
+ readonly recommendations: readonly string[];
119
+ }
120
+ /**
121
+ * Per-field token estimate.
122
+ */
123
+ export interface FieldTokenEstimate {
124
+ /** Field name */
125
+ readonly name: string;
126
+ /** Estimated tokens per occurrence */
127
+ readonly estimatedTokens: number;
128
+ /** Whether this field is a collection/array type */
129
+ readonly isCollection: boolean;
130
+ }
131
+ /**
132
+ * Estimate token count from a string using the ~4 chars/token heuristic.
133
+ *
134
+ * This is a fast approximation suitable for profiling. For precise
135
+ * token counting, use a tokenizer library (tiktoken, etc.).
136
+ *
137
+ * @param text - The text to estimate tokens for
138
+ * @returns Estimated token count
139
+ */
140
+ export declare function estimateTokens(text: string): number;
141
+ /**
142
+ * Estimate tokens for a content block structure (MCP response block).
143
+ *
144
+ * @param block - The content block with `type` and `text` fields
145
+ * @returns Block-level token profile
146
+ */
147
+ export declare function profileBlock(block: {
148
+ type: string;
149
+ text?: string;
150
+ }): BlockTokenProfile;
151
+ /**
152
+ * Profile a complete tool response for token economics.
153
+ *
154
+ * Analyzes all content blocks in a tool response to compute
155
+ * total token usage, overhead ratio, and risk classification.
156
+ *
157
+ * @param toolName - Name of the tool that produced the response
158
+ * @param actionKey - Action key (if applicable)
159
+ * @param blocks - Content blocks from the tool response
160
+ * @param overheadBlocks - Number of blocks that are overhead (rules, UI)
161
+ * @param config - Token economics configuration
162
+ * @returns Complete token analysis
163
+ */
164
+ export declare function profileResponse(toolName: string, actionKey: string | null, blocks: readonly {
165
+ type: string;
166
+ text?: string;
167
+ }[], overheadBlocks?: number, config?: TokenEconomicsConfig): TokenAnalysis;
168
+ /**
169
+ * Compute a static token profile from Presenter metadata.
170
+ *
171
+ * This runs once at manifest compilation time and produces
172
+ * a zero-cost profile that estimates the worst-case token
173
+ * usage for a tool based on its schema and guardrail config.
174
+ *
175
+ * @param toolName - Tool name
176
+ * @param schemaKeys - Presenter schema field names
177
+ * @param agentLimitMax - Maximum items from agentLimit() config
178
+ * @param egressMaxBytes - Maximum bytes from egressMaxBytes() config
179
+ * @returns Static token profile with recommendations
180
+ */
181
+ export declare function computeStaticProfile(toolName: string, schemaKeys: readonly string[], agentLimitMax: number | null, egressMaxBytes: number | null): StaticTokenProfile;
182
+ /**
183
+ * Aggregate static profiles into a server-level summary.
184
+ *
185
+ * @param profiles - All static profiles for the server
186
+ * @returns Server-level token economics summary
187
+ */
188
+ export declare function aggregateProfiles(profiles: readonly StaticTokenProfile[]): ServerTokenSummary;
189
+ /**
190
+ * Server-level token economics summary.
191
+ */
192
+ export interface ServerTokenSummary {
193
+ /** Total number of tools */
194
+ readonly toolCount: number;
195
+ /** Sum of all tools' minimum token estimates */
196
+ readonly totalMinTokens: number;
197
+ /** Sum of all tools' maximum token estimates */
198
+ readonly totalMaxTokens: number;
199
+ /** Number of tools without bounded output */
200
+ readonly unboundedToolCount: number;
201
+ /** Names of unbounded tools */
202
+ readonly unboundedToolNames: readonly string[];
203
+ /** Overall risk classification */
204
+ readonly overallRisk: TokenRisk;
205
+ /** Names of critical-risk tools */
206
+ readonly criticalToolNames: readonly string[];
207
+ /** Aggregated recommendations */
208
+ readonly recommendations: readonly string[];
209
+ }
210
+ //# sourceMappingURL=TokenEconomics.d.ts.map