@kernel.chat/kbot 2.13.1 → 2.14.1
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/README.md +38 -2
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +65 -4
- package/dist/agent.js.map +1 -1
- package/dist/emergent-swarm.d.ts +41 -0
- package/dist/emergent-swarm.d.ts.map +1 -0
- package/dist/emergent-swarm.js +246 -0
- package/dist/emergent-swarm.js.map +1 -0
- package/dist/entropy-context.d.ts +40 -0
- package/dist/entropy-context.d.ts.map +1 -0
- package/dist/entropy-context.js +144 -0
- package/dist/entropy-context.js.map +1 -0
- package/dist/error-correction.d.ts +37 -0
- package/dist/error-correction.d.ts.map +1 -0
- package/dist/error-correction.js +174 -0
- package/dist/error-correction.js.map +1 -0
- package/dist/godel-limits.d.ts +46 -0
- package/dist/godel-limits.d.ts.map +1 -0
- package/dist/godel-limits.js +251 -0
- package/dist/godel-limits.js.map +1 -0
- package/dist/simulation.d.ts +50 -0
- package/dist/simulation.d.ts.map +1 -0
- package/dist/simulation.js +240 -0
- package/dist/simulation.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// Error-Correcting Code patterns — inspired by James Gates' discovery of
|
|
2
|
+
// error-correcting codes in string theory. Classifies errors by type and
|
|
3
|
+
// applies targeted corrections instead of blind retry.
|
|
4
|
+
import { loadConfig } from './auth.js';
|
|
5
|
+
export var ErrorType;
|
|
6
|
+
(function (ErrorType) {
|
|
7
|
+
ErrorType["hallucination"] = "hallucination";
|
|
8
|
+
ErrorType["wrong_tool"] = "wrong_tool";
|
|
9
|
+
ErrorType["stale_context"] = "stale_context";
|
|
10
|
+
ErrorType["incomplete"] = "incomplete";
|
|
11
|
+
ErrorType["off_topic"] = "off_topic";
|
|
12
|
+
ErrorType["syntax_error"] = "syntax_error";
|
|
13
|
+
ErrorType["logic_error"] = "logic_error";
|
|
14
|
+
})(ErrorType || (ErrorType = {}));
|
|
15
|
+
export const CORRECTION_STRATEGIES = {
|
|
16
|
+
[ErrorType.hallucination]: {
|
|
17
|
+
errorType: ErrorType.hallucination,
|
|
18
|
+
description: 'Response contains unsupported claims',
|
|
19
|
+
correctionPrompt: 'Your previous response contained claims not supported by the provided context. Stick strictly to what\'s in the conversation and tool results. Do not invent facts, file paths, function names, or behaviors.',
|
|
20
|
+
severity: 4,
|
|
21
|
+
},
|
|
22
|
+
[ErrorType.wrong_tool]: {
|
|
23
|
+
errorType: ErrorType.wrong_tool,
|
|
24
|
+
description: 'Incorrect tool selection',
|
|
25
|
+
correctionPrompt: 'You used an incorrect tool for this task. Reconsider which tool best fits the user\'s request. Re-read the available tools and their descriptions before choosing.',
|
|
26
|
+
severity: 3,
|
|
27
|
+
},
|
|
28
|
+
[ErrorType.stale_context]: {
|
|
29
|
+
errorType: ErrorType.stale_context,
|
|
30
|
+
description: 'Referenced outdated information',
|
|
31
|
+
correctionPrompt: 'Your response referenced outdated information. Focus on the most recent context and tool results. Earlier conversation turns may contain stale state — prioritize the latest data.',
|
|
32
|
+
severity: 3,
|
|
33
|
+
},
|
|
34
|
+
[ErrorType.incomplete]: {
|
|
35
|
+
errorType: ErrorType.incomplete,
|
|
36
|
+
description: 'Response did not address all parts',
|
|
37
|
+
correctionPrompt: 'Your response was incomplete. Address all parts of the user\'s request. Re-read the original message and ensure every question or requirement is covered.',
|
|
38
|
+
severity: 2,
|
|
39
|
+
},
|
|
40
|
+
[ErrorType.off_topic]: {
|
|
41
|
+
errorType: ErrorType.off_topic,
|
|
42
|
+
description: 'Response diverged from the question',
|
|
43
|
+
correctionPrompt: 'Your response diverged from the user\'s question. Re-read the original request and answer directly. Do not add tangential information.',
|
|
44
|
+
severity: 2,
|
|
45
|
+
},
|
|
46
|
+
[ErrorType.syntax_error]: {
|
|
47
|
+
errorType: ErrorType.syntax_error,
|
|
48
|
+
description: 'Code output has syntax errors',
|
|
49
|
+
correctionPrompt: 'Your code output contained syntax errors. Fix the syntax and verify it compiles. Check for missing brackets, semicolons, type annotations, and import statements.',
|
|
50
|
+
severity: 5,
|
|
51
|
+
},
|
|
52
|
+
[ErrorType.logic_error]: {
|
|
53
|
+
errorType: ErrorType.logic_error,
|
|
54
|
+
description: 'Code has logical flaws',
|
|
55
|
+
correctionPrompt: 'Your code has a logical error. Trace through the logic step by step. Check edge cases, off-by-one errors, null handling, and conditional branches.',
|
|
56
|
+
severity: 4,
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
const CLASSIFY_PROMPT = `You are an error classifier for an AI agent's output. Analyze the response and classify any errors found.
|
|
60
|
+
|
|
61
|
+
USER QUERY:
|
|
62
|
+
{query}
|
|
63
|
+
|
|
64
|
+
AGENT RESPONSE:
|
|
65
|
+
{response}
|
|
66
|
+
|
|
67
|
+
{context_section}
|
|
68
|
+
|
|
69
|
+
Classify the primary error (if any). Respond in JSON:
|
|
70
|
+
{"errorType": "hallucination|wrong_tool|stale_context|incomplete|off_topic|syntax_error|logic_error|none", "confidence": 0.0-1.0, "evidence": "brief explanation"}
|
|
71
|
+
|
|
72
|
+
If no error found, use errorType "none" with confidence 1.0.
|
|
73
|
+
Respond ONLY with the JSON object.`;
|
|
74
|
+
export async function classifyError(query, response, context) {
|
|
75
|
+
const config = loadConfig();
|
|
76
|
+
if (!config)
|
|
77
|
+
return null;
|
|
78
|
+
const contextSection = context
|
|
79
|
+
? `CONTEXT PROVIDED:\n${context.slice(0, 2000)}`
|
|
80
|
+
: '';
|
|
81
|
+
const prompt = CLASSIFY_PROMPT
|
|
82
|
+
.replace('{query}', query)
|
|
83
|
+
.replace('{response}', response.slice(0, 3000))
|
|
84
|
+
.replace('{context_section}', contextSection);
|
|
85
|
+
try {
|
|
86
|
+
const text = await callFastModel(config, prompt);
|
|
87
|
+
const match = text.match(/\{[\s\S]*\}/);
|
|
88
|
+
if (!match)
|
|
89
|
+
return null;
|
|
90
|
+
const parsed = JSON.parse(match[0]);
|
|
91
|
+
if (parsed.errorType === 'none')
|
|
92
|
+
return null;
|
|
93
|
+
const errorType = parsed.errorType;
|
|
94
|
+
if (!Object.values(ErrorType).includes(errorType))
|
|
95
|
+
return null;
|
|
96
|
+
return {
|
|
97
|
+
errorType,
|
|
98
|
+
confidence: Math.max(0, Math.min(1, parsed.confidence || 0)),
|
|
99
|
+
evidence: parsed.evidence || '',
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
export function applyCorrection(query, response, errorType, evidence) {
|
|
107
|
+
const strategy = CORRECTION_STRATEGIES[errorType];
|
|
108
|
+
return [
|
|
109
|
+
`[ERROR CORRECTION — ${strategy.description}]`,
|
|
110
|
+
strategy.correctionPrompt,
|
|
111
|
+
`Evidence of error: ${evidence}`,
|
|
112
|
+
`Original query: ${query}`,
|
|
113
|
+
'Please provide a corrected response.',
|
|
114
|
+
].join('\n');
|
|
115
|
+
}
|
|
116
|
+
export async function withErrorCorrection(generateFn, query, context, maxRetries = 2) {
|
|
117
|
+
const corrections = [];
|
|
118
|
+
let response = await generateFn();
|
|
119
|
+
let retries = 0;
|
|
120
|
+
for (let attempt = 0; attempt < maxRetries; attempt++) {
|
|
121
|
+
const classification = await classifyError(query, response, context);
|
|
122
|
+
if (!classification || classification.confidence < 0.7)
|
|
123
|
+
break;
|
|
124
|
+
const correctionPrompt = applyCorrection(query, response, classification.errorType, classification.evidence);
|
|
125
|
+
corrections.push({
|
|
126
|
+
errorType: classification.errorType,
|
|
127
|
+
confidence: classification.confidence,
|
|
128
|
+
evidence: classification.evidence,
|
|
129
|
+
correctionApplied: CORRECTION_STRATEGIES[classification.errorType].description,
|
|
130
|
+
attempt: attempt + 1,
|
|
131
|
+
});
|
|
132
|
+
response = await generateFn(correctionPrompt);
|
|
133
|
+
retries++;
|
|
134
|
+
}
|
|
135
|
+
return { response, corrections, retries };
|
|
136
|
+
}
|
|
137
|
+
// ── Multi-provider fast model call ──────────────────────────────────
|
|
138
|
+
async function callFastModel(config, prompt) {
|
|
139
|
+
const provider = config.provider || 'anthropic';
|
|
140
|
+
const key = config.apiKey || config.key;
|
|
141
|
+
if (provider === 'anthropic' || provider === 'claude') {
|
|
142
|
+
const res = await fetch('https://api.anthropic.com/v1/messages', {
|
|
143
|
+
method: 'POST',
|
|
144
|
+
headers: {
|
|
145
|
+
'Content-Type': 'application/json',
|
|
146
|
+
'x-api-key': key,
|
|
147
|
+
'anthropic-version': '2023-06-01',
|
|
148
|
+
},
|
|
149
|
+
body: JSON.stringify({
|
|
150
|
+
model: 'claude-haiku-4-5-20251001',
|
|
151
|
+
max_tokens: 256,
|
|
152
|
+
messages: [{ role: 'user', content: prompt }],
|
|
153
|
+
}),
|
|
154
|
+
});
|
|
155
|
+
const data = await res.json();
|
|
156
|
+
return data?.content?.[0]?.text || '';
|
|
157
|
+
}
|
|
158
|
+
if (provider === 'openai') {
|
|
159
|
+
const res = await fetch('https://api.openai.com/v1/chat/completions', {
|
|
160
|
+
method: 'POST',
|
|
161
|
+
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${key}` },
|
|
162
|
+
body: JSON.stringify({
|
|
163
|
+
model: 'gpt-4o-mini',
|
|
164
|
+
max_tokens: 256,
|
|
165
|
+
messages: [{ role: 'user', content: prompt }],
|
|
166
|
+
}),
|
|
167
|
+
});
|
|
168
|
+
const data = await res.json();
|
|
169
|
+
return data?.choices?.[0]?.message?.content || '';
|
|
170
|
+
}
|
|
171
|
+
// Fallback: skip classification for unsupported providers
|
|
172
|
+
return '';
|
|
173
|
+
}
|
|
174
|
+
//# sourceMappingURL=error-correction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-correction.js","sourceRoot":"","sources":["../src/error-correction.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,yEAAyE;AACzE,uDAAuD;AAEvD,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AAEtC,MAAM,CAAN,IAAY,SA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CAAC,EAAE,IAAI,IAAI,EAAE,CAAA;IACvC,CAAC;IAED,IAAI,QAAQ,KAAK,QAAQ,EAAE,CAAC;QAC1B,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,4CAA4C,EAAE;YACpE,MAAM,EAAE,MAAM;YACd,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE,aAAa,EAAE,UAAU,GAAG,EAAE,EAAE;YAC/E,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC;gBACnB,KAAK,EAAE,aAAa;gBACpB,UAAU,EAAE,GAAG;gBACf,QAAQ,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;aAC9C,CAAC;SACH,CAAC,CAAA;QACF,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,IAAI,EAAS,CAAA;QACpC,OAAO,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,IAAI,EAAE,CAAA;IACnD,CAAC;IAED,0DAA0D;IAC1D,OAAO,EAAE,CAAA;AACX,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export declare enum LoopPattern {
|
|
2
|
+
tool_repetition = "tool_repetition",
|
|
3
|
+
output_oscillation = "output_oscillation",
|
|
4
|
+
cost_spiral = "cost_spiral",
|
|
5
|
+
context_exhaustion = "context_exhaustion",
|
|
6
|
+
semantic_stagnation = "semantic_stagnation",
|
|
7
|
+
circular_reasoning = "circular_reasoning"
|
|
8
|
+
}
|
|
9
|
+
export interface DecidabilityScore {
|
|
10
|
+
decidable: boolean;
|
|
11
|
+
confidence: number;
|
|
12
|
+
pattern?: LoopPattern;
|
|
13
|
+
evidence: string;
|
|
14
|
+
recommendation: 'continue' | 'simplify' | 'handoff' | 'decompose';
|
|
15
|
+
tokensBurned: number;
|
|
16
|
+
costBurned: number;
|
|
17
|
+
}
|
|
18
|
+
interface Options {
|
|
19
|
+
maxToolRepeats: number;
|
|
20
|
+
maxCostUsd: number;
|
|
21
|
+
maxTokens: number;
|
|
22
|
+
similarityThreshold: number;
|
|
23
|
+
}
|
|
24
|
+
export declare function jaccardSimilarity(a: string, b: string): number;
|
|
25
|
+
export declare function detectOscillation(outputs: string[]): boolean;
|
|
26
|
+
export declare class LoopDetector {
|
|
27
|
+
private toolHistory;
|
|
28
|
+
private outputs;
|
|
29
|
+
private totalCost;
|
|
30
|
+
private costHistory;
|
|
31
|
+
private totalTokens;
|
|
32
|
+
private opts;
|
|
33
|
+
constructor(options?: Partial<Options>);
|
|
34
|
+
recordToolCall(toolName: string, args: string, result: string): void;
|
|
35
|
+
recordOutput(output: string): void;
|
|
36
|
+
recordCost(costUsd: number): void;
|
|
37
|
+
recordTokens(tokens: number): void;
|
|
38
|
+
check(): DecidabilityScore;
|
|
39
|
+
reset(): void;
|
|
40
|
+
private checkToolRepetition;
|
|
41
|
+
private checkCostSpiral;
|
|
42
|
+
private checkSemanticStagnation;
|
|
43
|
+
private checkCircularReasoning;
|
|
44
|
+
}
|
|
45
|
+
export {};
|
|
46
|
+
//# sourceMappingURL=godel-limits.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"godel-limits.d.ts","sourceRoot":"","sources":["../src/godel-limits.ts"],"names":[],"mappings":"AAIA,oBAAY,WAAW;IACrB,eAAe,oBAAoB;IACnC,kBAAkB,uBAAuB;IACzC,WAAW,gBAAgB;IAC3B,kBAAkB,uBAAuB;IACzC,mBAAmB,wBAAwB;IAC3C,kBAAkB,uBAAuB;CAC1C;AAED,MAAM,WAAW,iBAAiB;IAChC,SAAS,EAAE,OAAO,CAAA;IAClB,UAAU,EAAE,MAAM,CAAA;IAClB,OAAO,CAAC,EAAE,WAAW,CAAA;IACrB,QAAQ,EAAE,MAAM,CAAA;IAChB,cAAc,EAAE,UAAU,GAAG,UAAU,GAAG,SAAS,GAAG,WAAW,CAAA;IACjE,YAAY,EAAE,MAAM,CAAA;IACpB,UAAU,EAAE,MAAM,CAAA;CACnB;AASD,UAAU,OAAO;IACf,cAAc,EAAE,MAAM,CAAA;IACtB,UAAU,EAAE,MAAM,CAAA;IAClB,SAAS,EAAE,MAAM,CAAA;IACjB,mBAAmB,EAAE,MAAM,CAAA;CAC5B;AAoBD,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAU9D;AAED,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,OAAO,CAiB5D;AAED,qBAAa,YAAY;IACvB,OAAO,CAAC,WAAW,CAAmB;IACtC,OAAO,CAAC,OAAO,CAAe;IAC9B,OAAO,CAAC,SAAS,CAAI;IACrB,OAAO,CAAC,WAAW,CAAe;IAClC,OAAO,CAAC,WAAW,CAAI;IACvB,OAAO,CAAC,IAAI,CAAS;gBAET,OAAO,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC;IAStC,cAAc,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI;IAIpE,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAIlC,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAKjC,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAIlC,KAAK,IAAI,iBAAiB;IAgF1B,KAAK,IAAI,IAAI;IAQb,OAAO,CAAC,mBAAmB;IAqC3B,OAAO,CAAC,eAAe;IAevB,OAAO,CAAC,uBAAuB;IAU/B,OAAO,CAAC,sBAAsB;CAsB/B"}
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
// Computational decidability detection — inspired by Gödel's incompleteness
|
|
2
|
+
// theorem and the UBC Okanagan research proving some problems can't be solved
|
|
3
|
+
// algorithmically. Detects when the agent is stuck and should hand off.
|
|
4
|
+
export var LoopPattern;
|
|
5
|
+
(function (LoopPattern) {
|
|
6
|
+
LoopPattern["tool_repetition"] = "tool_repetition";
|
|
7
|
+
LoopPattern["output_oscillation"] = "output_oscillation";
|
|
8
|
+
LoopPattern["cost_spiral"] = "cost_spiral";
|
|
9
|
+
LoopPattern["context_exhaustion"] = "context_exhaustion";
|
|
10
|
+
LoopPattern["semantic_stagnation"] = "semantic_stagnation";
|
|
11
|
+
LoopPattern["circular_reasoning"] = "circular_reasoning";
|
|
12
|
+
})(LoopPattern || (LoopPattern = {}));
|
|
13
|
+
const RECOMMENDATIONS = {
|
|
14
|
+
[LoopPattern.tool_repetition]: 'simplify',
|
|
15
|
+
[LoopPattern.output_oscillation]: 'handoff',
|
|
16
|
+
[LoopPattern.cost_spiral]: 'handoff',
|
|
17
|
+
[LoopPattern.context_exhaustion]: 'decompose',
|
|
18
|
+
[LoopPattern.semantic_stagnation]: 'simplify',
|
|
19
|
+
[LoopPattern.circular_reasoning]: 'handoff',
|
|
20
|
+
};
|
|
21
|
+
const EVIDENCE_MESSAGES = {
|
|
22
|
+
[LoopPattern.tool_repetition]: 'Same tool called repeatedly with similar arguments — try a different approach.',
|
|
23
|
+
[LoopPattern.output_oscillation]: 'Output alternating between two states — going back and forth without progress.',
|
|
24
|
+
[LoopPattern.cost_spiral]: 'Cost is accelerating without convergence — getting expensive without results.',
|
|
25
|
+
[LoopPattern.context_exhaustion]: 'Context window nearly full — too much information for one pass.',
|
|
26
|
+
[LoopPattern.semantic_stagnation]: 'Last several outputs are nearly identical — not making progress.',
|
|
27
|
+
[LoopPattern.circular_reasoning]: 'Tool results being fed back as inputs — reasoning in circles.',
|
|
28
|
+
};
|
|
29
|
+
export function jaccardSimilarity(a, b) {
|
|
30
|
+
const setA = new Set(a.toLowerCase().split(/\s+/).filter(w => w.length > 2));
|
|
31
|
+
const setB = new Set(b.toLowerCase().split(/\s+/).filter(w => w.length > 2));
|
|
32
|
+
if (setA.size === 0 && setB.size === 0)
|
|
33
|
+
return 1;
|
|
34
|
+
if (setA.size === 0 || setB.size === 0)
|
|
35
|
+
return 0;
|
|
36
|
+
let intersection = 0;
|
|
37
|
+
for (const w of setA) {
|
|
38
|
+
if (setB.has(w))
|
|
39
|
+
intersection++;
|
|
40
|
+
}
|
|
41
|
+
const union = new Set([...setA, ...setB]).size;
|
|
42
|
+
return union === 0 ? 0 : intersection / union;
|
|
43
|
+
}
|
|
44
|
+
export function detectOscillation(outputs) {
|
|
45
|
+
if (outputs.length < 4)
|
|
46
|
+
return false;
|
|
47
|
+
const recent = outputs.slice(-6);
|
|
48
|
+
if (recent.length < 4)
|
|
49
|
+
return false;
|
|
50
|
+
// Check A-B-A-B pattern: compare [0] with [2] and [1] with [3]
|
|
51
|
+
for (let i = 0; i <= recent.length - 4; i++) {
|
|
52
|
+
const simAC = jaccardSimilarity(recent[i], recent[i + 2]);
|
|
53
|
+
const simBD = jaccardSimilarity(recent[i + 1], recent[i + 3]);
|
|
54
|
+
const simAB = jaccardSimilarity(recent[i], recent[i + 1]);
|
|
55
|
+
// A≈C and B≈D but A≠B → oscillation
|
|
56
|
+
if (simAC > 0.8 && simBD > 0.8 && simAB < 0.5)
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
export class LoopDetector {
|
|
62
|
+
toolHistory = [];
|
|
63
|
+
outputs = [];
|
|
64
|
+
totalCost = 0;
|
|
65
|
+
costHistory = [];
|
|
66
|
+
totalTokens = 0;
|
|
67
|
+
opts;
|
|
68
|
+
constructor(options) {
|
|
69
|
+
this.opts = {
|
|
70
|
+
maxToolRepeats: options?.maxToolRepeats ?? 5,
|
|
71
|
+
maxCostUsd: options?.maxCostUsd ?? 1.0,
|
|
72
|
+
maxTokens: options?.maxTokens ?? 50000,
|
|
73
|
+
similarityThreshold: options?.similarityThreshold ?? 0.85,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
recordToolCall(toolName, args, result) {
|
|
77
|
+
this.toolHistory.push({ name: toolName, args, result, timestamp: Date.now() });
|
|
78
|
+
}
|
|
79
|
+
recordOutput(output) {
|
|
80
|
+
this.outputs.push(output);
|
|
81
|
+
}
|
|
82
|
+
recordCost(costUsd) {
|
|
83
|
+
this.totalCost += costUsd;
|
|
84
|
+
this.costHistory.push(this.totalCost);
|
|
85
|
+
}
|
|
86
|
+
recordTokens(tokens) {
|
|
87
|
+
this.totalTokens += tokens;
|
|
88
|
+
}
|
|
89
|
+
check() {
|
|
90
|
+
const base = {
|
|
91
|
+
tokensBurned: this.totalTokens,
|
|
92
|
+
costBurned: this.totalCost,
|
|
93
|
+
};
|
|
94
|
+
// 1. Tool repetition
|
|
95
|
+
const repetition = this.checkToolRepetition();
|
|
96
|
+
if (repetition)
|
|
97
|
+
return { ...base, ...repetition };
|
|
98
|
+
// 2. Output oscillation
|
|
99
|
+
if (detectOscillation(this.outputs)) {
|
|
100
|
+
return {
|
|
101
|
+
...base,
|
|
102
|
+
decidable: false,
|
|
103
|
+
confidence: 0.85,
|
|
104
|
+
pattern: LoopPattern.output_oscillation,
|
|
105
|
+
evidence: EVIDENCE_MESSAGES[LoopPattern.output_oscillation],
|
|
106
|
+
recommendation: RECOMMENDATIONS[LoopPattern.output_oscillation],
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
// 3. Cost spiral
|
|
110
|
+
if (this.checkCostSpiral()) {
|
|
111
|
+
return {
|
|
112
|
+
...base,
|
|
113
|
+
decidable: false,
|
|
114
|
+
confidence: 0.9,
|
|
115
|
+
pattern: LoopPattern.cost_spiral,
|
|
116
|
+
evidence: `${EVIDENCE_MESSAGES[LoopPattern.cost_spiral]} ($${this.totalCost.toFixed(2)} spent)`,
|
|
117
|
+
recommendation: RECOMMENDATIONS[LoopPattern.cost_spiral],
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
// 4. Context exhaustion
|
|
121
|
+
if (this.totalTokens > this.opts.maxTokens) {
|
|
122
|
+
return {
|
|
123
|
+
...base,
|
|
124
|
+
decidable: false,
|
|
125
|
+
confidence: 0.95,
|
|
126
|
+
pattern: LoopPattern.context_exhaustion,
|
|
127
|
+
evidence: `${EVIDENCE_MESSAGES[LoopPattern.context_exhaustion]} (${this.totalTokens} tokens used)`,
|
|
128
|
+
recommendation: RECOMMENDATIONS[LoopPattern.context_exhaustion],
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
// 5. Semantic stagnation
|
|
132
|
+
if (this.checkSemanticStagnation()) {
|
|
133
|
+
return {
|
|
134
|
+
...base,
|
|
135
|
+
decidable: false,
|
|
136
|
+
confidence: 0.8,
|
|
137
|
+
pattern: LoopPattern.semantic_stagnation,
|
|
138
|
+
evidence: EVIDENCE_MESSAGES[LoopPattern.semantic_stagnation],
|
|
139
|
+
recommendation: RECOMMENDATIONS[LoopPattern.semantic_stagnation],
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
// 6. Circular reasoning
|
|
143
|
+
if (this.checkCircularReasoning()) {
|
|
144
|
+
return {
|
|
145
|
+
...base,
|
|
146
|
+
decidable: false,
|
|
147
|
+
confidence: 0.75,
|
|
148
|
+
pattern: LoopPattern.circular_reasoning,
|
|
149
|
+
evidence: EVIDENCE_MESSAGES[LoopPattern.circular_reasoning],
|
|
150
|
+
recommendation: RECOMMENDATIONS[LoopPattern.circular_reasoning],
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
// All clear
|
|
154
|
+
return {
|
|
155
|
+
...base,
|
|
156
|
+
decidable: true,
|
|
157
|
+
confidence: 1.0,
|
|
158
|
+
evidence: 'No loop patterns detected.',
|
|
159
|
+
recommendation: 'continue',
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
reset() {
|
|
163
|
+
this.toolHistory = [];
|
|
164
|
+
this.outputs = [];
|
|
165
|
+
this.totalCost = 0;
|
|
166
|
+
this.costHistory = [];
|
|
167
|
+
this.totalTokens = 0;
|
|
168
|
+
}
|
|
169
|
+
checkToolRepetition() {
|
|
170
|
+
if (this.toolHistory.length < this.opts.maxToolRepeats)
|
|
171
|
+
return null;
|
|
172
|
+
// Group recent calls by tool name
|
|
173
|
+
const recent = this.toolHistory.slice(-10);
|
|
174
|
+
const groups = new Map();
|
|
175
|
+
for (const record of recent) {
|
|
176
|
+
if (!groups.has(record.name))
|
|
177
|
+
groups.set(record.name, []);
|
|
178
|
+
groups.get(record.name).push(record);
|
|
179
|
+
}
|
|
180
|
+
for (const [name, calls] of groups) {
|
|
181
|
+
if (calls.length < this.opts.maxToolRepeats)
|
|
182
|
+
continue;
|
|
183
|
+
// Check if args are similar
|
|
184
|
+
let similarCount = 0;
|
|
185
|
+
for (let i = 1; i < calls.length; i++) {
|
|
186
|
+
if (jaccardSimilarity(calls[i].args, calls[0].args) > this.opts.similarityThreshold) {
|
|
187
|
+
similarCount++;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (similarCount >= this.opts.maxToolRepeats - 1) {
|
|
191
|
+
return {
|
|
192
|
+
decidable: false,
|
|
193
|
+
confidence: 0.9,
|
|
194
|
+
pattern: LoopPattern.tool_repetition,
|
|
195
|
+
evidence: `${EVIDENCE_MESSAGES[LoopPattern.tool_repetition]} (${name} called ${calls.length}x)`,
|
|
196
|
+
recommendation: RECOMMENDATIONS[LoopPattern.tool_repetition],
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
checkCostSpiral() {
|
|
203
|
+
if (this.totalCost < this.opts.maxCostUsd)
|
|
204
|
+
return false;
|
|
205
|
+
if (this.costHistory.length < 4)
|
|
206
|
+
return true; // Over budget with few steps = spiral
|
|
207
|
+
// Check if cost rate is accelerating
|
|
208
|
+
const recent = this.costHistory.slice(-4);
|
|
209
|
+
const deltas = [];
|
|
210
|
+
for (let i = 1; i < recent.length; i++) {
|
|
211
|
+
deltas.push(recent[i] - recent[i - 1]);
|
|
212
|
+
}
|
|
213
|
+
// Accelerating if each delta is larger than the previous
|
|
214
|
+
return deltas.length >= 2 && deltas[deltas.length - 1] > deltas[0];
|
|
215
|
+
}
|
|
216
|
+
checkSemanticStagnation() {
|
|
217
|
+
if (this.outputs.length < 3)
|
|
218
|
+
return false;
|
|
219
|
+
const recent = this.outputs.slice(-3);
|
|
220
|
+
for (let i = 1; i < recent.length; i++) {
|
|
221
|
+
if (jaccardSimilarity(recent[i], recent[0]) < 0.9)
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
return true;
|
|
225
|
+
}
|
|
226
|
+
checkCircularReasoning() {
|
|
227
|
+
if (this.toolHistory.length < 3)
|
|
228
|
+
return false;
|
|
229
|
+
const recent = this.toolHistory.slice(-5);
|
|
230
|
+
for (let i = 1; i < recent.length; i++) {
|
|
231
|
+
const prevResult = recent[i - 1].result;
|
|
232
|
+
const currArgs = recent[i].args;
|
|
233
|
+
// If >40% of words in current args appeared in previous result
|
|
234
|
+
if (prevResult.length > 20 && currArgs.length > 20) {
|
|
235
|
+
const resultWords = new Set(prevResult.toLowerCase().split(/\s+/).filter(w => w.length > 3));
|
|
236
|
+
const argWords = currArgs.toLowerCase().split(/\s+/).filter(w => w.length > 3);
|
|
237
|
+
if (argWords.length === 0)
|
|
238
|
+
continue;
|
|
239
|
+
let overlap = 0;
|
|
240
|
+
for (const w of argWords) {
|
|
241
|
+
if (resultWords.has(w))
|
|
242
|
+
overlap++;
|
|
243
|
+
}
|
|
244
|
+
if (overlap / argWords.length > 0.4)
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
//# sourceMappingURL=godel-limits.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"godel-limits.js","sourceRoot":"","sources":["../src/godel-limits.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,8EAA8E;AAC9E,wEAAwE;AAExE,MAAM,CAAN,IAAY,WAOX;AAPD,WAAY,WAAW;IACrB,kDAAmC,CAAA;IACnC,wDAAyC,CAAA;IACzC,0CAA2B,CAAA;IAC3B,wDA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AAG,CAAC,CAAC,CAAC;wBAAE,OAAO,EAAE,CAAA;gBAAC,CAAC;gBAC/D,IAAI,OAAO,GAAG,QAAQ,CAAC,MAAM,GAAG,GAAG;oBAAE,OAAO,IAAI,CAAA;YAClD,CAAC;QACH,CAAC;QAED,OAAO,KAAK,CAAA;IACd,CAAC;CACF"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export interface SimulationScenario {
|
|
2
|
+
description: string;
|
|
3
|
+
targetFiles: string[];
|
|
4
|
+
changeType: 'refactor' | 'add_feature' | 'delete' | 'migrate' | 'upgrade';
|
|
5
|
+
constraints?: string[];
|
|
6
|
+
}
|
|
7
|
+
export interface BreakingChange {
|
|
8
|
+
file: string;
|
|
9
|
+
line?: number;
|
|
10
|
+
description: string;
|
|
11
|
+
severity: 'warning' | 'error';
|
|
12
|
+
suggestedFix?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface SimulationResult {
|
|
15
|
+
scenario: SimulationScenario;
|
|
16
|
+
predictedOutcome: string;
|
|
17
|
+
riskLevel: 'low' | 'medium' | 'high' | 'critical';
|
|
18
|
+
breakingChanges: BreakingChange[];
|
|
19
|
+
estimatedEffort: string;
|
|
20
|
+
confidence: number;
|
|
21
|
+
recommendations: string[];
|
|
22
|
+
}
|
|
23
|
+
export interface ComparisonResult {
|
|
24
|
+
scenarios: SimulationResult[];
|
|
25
|
+
recommended: number;
|
|
26
|
+
reasoning: string;
|
|
27
|
+
}
|
|
28
|
+
export interface FileNode {
|
|
29
|
+
path: string;
|
|
30
|
+
exports: string[];
|
|
31
|
+
imports: string[];
|
|
32
|
+
size: number;
|
|
33
|
+
}
|
|
34
|
+
export interface DependencyGraph {
|
|
35
|
+
nodes: Map<string, FileNode>;
|
|
36
|
+
edges: Map<string, string[]>;
|
|
37
|
+
}
|
|
38
|
+
export declare function buildDependencyGraph(rootDir: string): Promise<DependencyGraph>;
|
|
39
|
+
export declare function findImpactedFiles(graph: DependencyGraph, changedFiles: string[]): string[];
|
|
40
|
+
export declare function simulateChange(scenario: SimulationScenario, graph?: DependencyGraph): Promise<SimulationResult>;
|
|
41
|
+
export declare class Simulator {
|
|
42
|
+
private rootDir;
|
|
43
|
+
private graph;
|
|
44
|
+
constructor(rootDir?: string);
|
|
45
|
+
init(): Promise<void>;
|
|
46
|
+
simulate(scenario: SimulationScenario): Promise<SimulationResult>;
|
|
47
|
+
compareScenarios(scenarios: SimulationScenario[]): Promise<ComparisonResult>;
|
|
48
|
+
getGraph(): DependencyGraph | null;
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=simulation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"simulation.d.ts","sourceRoot":"","sources":["../src/simulation.ts"],"names":[],"mappings":"AAQA,MAAM,WAAW,kBAAkB;IACjC,WAAW,EAAE,MAAM,CAAA;IACnB,WAAW,EAAE,MAAM,EAAE,CAAA;IACrB,UAAU,EAAE,UAAU,GAAG,aAAa,GAAG,QAAQ,GAAG,SAAS,GAAG,SAAS,CAAA;IACzE,WAAW,CAAC,EAAE,MAAM,EAAE,CAAA;CACvB;AAED,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,WAAW,EAAE,MAAM,CAAA;IACnB,QAAQ,EAAE,SAAS,GAAG,OAAO,CAAA;IAC7B,YAAY,CAAC,EAAE,MAAM,CAAA;CACtB;AAED,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,EAAE,kBAAkB,CAAA;IAC5B,gBAAgB,EAAE,MAAM,CAAA;IACxB,SAAS,EAAE,KAAK,GAAG,QAAQ,GAAG,MAAM,GAAG,UAAU,CAAA;IACjD,eAAe,EAAE,cAAc,EAAE,CAAA;IACjC,eAAe,EAAE,MAAM,CAAA;IACvB,UAAU,EAAE,MAAM,CAAA;IAClB,eAAe,EAAE,MAAM,EAAE,CAAA;CAC1B;AAED,MAAM,WAAW,gBAAgB;IAC/B,SAAS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