@wrongstack/tools 0.260.0 → 0.265.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/dist/audit.js +154 -10
- package/dist/audit.js.map +1 -1
- package/dist/bash.js +138 -2
- package/dist/bash.js.map +1 -1
- package/dist/batch-tool-use.js +1 -0
- package/dist/batch-tool-use.js.map +1 -1
- package/dist/builtin.d.ts +20 -1
- package/dist/builtin.js +674 -333
- package/dist/builtin.js.map +1 -1
- package/dist/circuit-breaker.d.ts +20 -0
- package/dist/circuit-breaker.js +40 -2
- package/dist/circuit-breaker.js.map +1 -1
- package/dist/codebase-index/index.d.ts +16 -0
- package/dist/codebase-index/index.js +62 -27
- package/dist/codebase-index/index.js.map +1 -1
- package/dist/codebase-index/worker.js +59 -27
- package/dist/codebase-index/worker.js.map +1 -1
- package/dist/diff.js +14 -6
- package/dist/diff.js.map +1 -1
- package/dist/document.js +18 -11
- package/dist/document.js.map +1 -1
- package/dist/edit.js +22 -14
- package/dist/edit.js.map +1 -1
- package/dist/exec.js +140 -3
- package/dist/exec.js.map +1 -1
- package/dist/fetch.d.ts +19 -1
- package/dist/fetch.js +2 -1
- package/dist/fetch.js.map +1 -1
- package/dist/format.js +153 -10
- package/dist/format.js.map +1 -1
- package/dist/git.js +1 -0
- package/dist/git.js.map +1 -1
- package/dist/glob.js +14 -6
- package/dist/glob.js.map +1 -1
- package/dist/grep.js +14 -6
- package/dist/grep.js.map +1 -1
- package/dist/index.d.ts +55 -3
- package/dist/index.js +833 -336
- package/dist/index.js.map +1 -1
- package/dist/install.js +154 -10
- package/dist/install.js.map +1 -1
- package/dist/json.js +2 -0
- package/dist/json.js.map +1 -1
- package/dist/lint.js +153 -10
- package/dist/lint.js.map +1 -1
- package/dist/logs.js +14 -6
- package/dist/logs.js.map +1 -1
- package/dist/memory.js +1 -0
- package/dist/memory.js.map +1 -1
- package/dist/mode.js +1 -0
- package/dist/mode.js.map +1 -1
- package/dist/outdated.js +16 -7
- package/dist/outdated.js.map +1 -1
- package/dist/pack.js +643 -332
- package/dist/pack.js.map +1 -1
- package/dist/patch.js +14 -6
- package/dist/patch.js.map +1 -1
- package/dist/process-registry.d.ts +56 -2
- package/dist/process-registry.js +138 -3
- package/dist/process-registry.js.map +1 -1
- package/dist/read.js +24 -18
- package/dist/read.js.map +1 -1
- package/dist/replace.js +14 -6
- package/dist/replace.js.map +1 -1
- package/dist/scaffold.js +14 -6
- package/dist/scaffold.js.map +1 -1
- package/dist/search.js +3 -3
- package/dist/search.js.map +1 -1
- package/dist/test.js +153 -10
- package/dist/test.js.map +1 -1
- package/dist/todo.js +1 -0
- package/dist/todo.js.map +1 -1
- package/dist/tool-help.js +1 -0
- package/dist/tool-help.js.map +1 -1
- package/dist/tool-icons.d.ts +20 -0
- package/dist/tool-icons.js +130 -0
- package/dist/tool-icons.js.map +1 -0
- package/dist/tool-search.js +1 -0
- package/dist/tool-search.js.map +1 -1
- package/dist/tool-use.js +1 -0
- package/dist/tool-use.js.map +1 -1
- package/dist/tree.js +14 -6
- package/dist/tree.js.map +1 -1
- package/dist/typecheck.js +153 -10
- package/dist/typecheck.js.map +1 -1
- package/dist/write.js +22 -14
- package/dist/write.js.map +1 -1
- package/package.json +6 -2
|
@@ -71,7 +71,27 @@ declare class CircuitBreaker {
|
|
|
71
71
|
private lastSlowAt;
|
|
72
72
|
/** Timestamp when the breaker was opened (for cooldown calculation). */
|
|
73
73
|
private openedAt;
|
|
74
|
+
/**
|
|
75
|
+
* Master enable flag. When false the breaker is bypassed: `beforeCall`
|
|
76
|
+
* always returns true and `afterCall` records nothing. The class itself
|
|
77
|
+
* defaults to enabled (so the standalone unit tests exercise tripping); the
|
|
78
|
+
* ProcessRegistry flips this off until the user opts in via `/settings`.
|
|
79
|
+
*/
|
|
80
|
+
private enabled;
|
|
81
|
+
/**
|
|
82
|
+
* Fired (best-effort) when the breaker transitions into the `open` state.
|
|
83
|
+
* The registry uses this to arm its auto kill/reset countdown.
|
|
84
|
+
*/
|
|
85
|
+
onTrip?: (() => void) | undefined;
|
|
86
|
+
/**
|
|
87
|
+
* Fired (best-effort) when the breaker returns to `closed` after having been
|
|
88
|
+
* open/half-open. The registry uses this to cancel a pending kill/reset.
|
|
89
|
+
*/
|
|
90
|
+
onReset?: (() => void) | undefined;
|
|
74
91
|
constructor(config?: CircuitBreakerConfig);
|
|
92
|
+
/** Toggle the master enable. Disabling resets to a clean `closed` state. */
|
|
93
|
+
setEnabled(enabled: boolean): void;
|
|
94
|
+
get isEnabled(): boolean;
|
|
75
95
|
/**
|
|
76
96
|
* Returns true if the circuit allows a new call to proceed.
|
|
77
97
|
* When false, callers should abort the tool call and return a
|
package/dist/circuit-breaker.js
CHANGED
|
@@ -19,6 +19,23 @@ var CircuitBreaker = class {
|
|
|
19
19
|
lastSlowAt = null;
|
|
20
20
|
/** Timestamp when the breaker was opened (for cooldown calculation). */
|
|
21
21
|
openedAt = null;
|
|
22
|
+
/**
|
|
23
|
+
* Master enable flag. When false the breaker is bypassed: `beforeCall`
|
|
24
|
+
* always returns true and `afterCall` records nothing. The class itself
|
|
25
|
+
* defaults to enabled (so the standalone unit tests exercise tripping); the
|
|
26
|
+
* ProcessRegistry flips this off until the user opts in via `/settings`.
|
|
27
|
+
*/
|
|
28
|
+
enabled = true;
|
|
29
|
+
/**
|
|
30
|
+
* Fired (best-effort) when the breaker transitions into the `open` state.
|
|
31
|
+
* The registry uses this to arm its auto kill/reset countdown.
|
|
32
|
+
*/
|
|
33
|
+
onTrip;
|
|
34
|
+
/**
|
|
35
|
+
* Fired (best-effort) when the breaker returns to `closed` after having been
|
|
36
|
+
* open/half-open. The registry uses this to cancel a pending kill/reset.
|
|
37
|
+
*/
|
|
38
|
+
onReset;
|
|
22
39
|
constructor(config = {}) {
|
|
23
40
|
this.maxConsecutiveFailures = config.maxConsecutiveFailures ?? DEFAULT_MAX_CONSECUTIVE_FAILURES;
|
|
24
41
|
this.slowCallThresholdMs = config.slowCallThresholdMs ?? DEFAULT_SLOW_CALL_THRESHOLD_MS;
|
|
@@ -27,12 +44,22 @@ var CircuitBreaker = class {
|
|
|
27
44
|
this.maxCallsPerWindow = config.maxCallsPerWindow ?? DEFAULT_MAX_CALLS_PER_WINDOW;
|
|
28
45
|
this.cooldownMs = config.cooldownMs ?? DEFAULT_COOLDOWN_MS;
|
|
29
46
|
}
|
|
47
|
+
/** Toggle the master enable. Disabling resets to a clean `closed` state. */
|
|
48
|
+
setEnabled(enabled) {
|
|
49
|
+
if (this.enabled === enabled) return;
|
|
50
|
+
this.enabled = enabled;
|
|
51
|
+
if (!enabled) this._reset();
|
|
52
|
+
}
|
|
53
|
+
get isEnabled() {
|
|
54
|
+
return this.enabled;
|
|
55
|
+
}
|
|
30
56
|
/**
|
|
31
57
|
* Returns true if the circuit allows a new call to proceed.
|
|
32
58
|
* When false, callers should abort the tool call and return a
|
|
33
59
|
* circuit-breaker error instead of spawning a process.
|
|
34
60
|
*/
|
|
35
61
|
get canProceed() {
|
|
62
|
+
if (!this.enabled) return true;
|
|
36
63
|
this._checkStateTransition();
|
|
37
64
|
return this.state !== "open";
|
|
38
65
|
}
|
|
@@ -68,7 +95,7 @@ var CircuitBreaker = class {
|
|
|
68
95
|
* not affect breaker state.
|
|
69
96
|
*/
|
|
70
97
|
beforeCall(bypass = false) {
|
|
71
|
-
if (bypass) return true;
|
|
98
|
+
if (bypass || !this.enabled) return true;
|
|
72
99
|
this._checkStateTransition();
|
|
73
100
|
if (this.state === "open") return false;
|
|
74
101
|
return true;
|
|
@@ -83,7 +110,7 @@ var CircuitBreaker = class {
|
|
|
83
110
|
* Use for background/fire-and-forget processes.
|
|
84
111
|
*/
|
|
85
112
|
afterCall(durationMs, failed, bypass = false) {
|
|
86
|
-
if (bypass) return;
|
|
113
|
+
if (bypass || !this.enabled) return;
|
|
87
114
|
const now = Date.now();
|
|
88
115
|
if (this.state === "half-open") {
|
|
89
116
|
if (failed) {
|
|
@@ -129,12 +156,23 @@ var CircuitBreaker = class {
|
|
|
129
156
|
if (this.state === "open") return;
|
|
130
157
|
this.state = "open";
|
|
131
158
|
this.openedAt = Date.now();
|
|
159
|
+
try {
|
|
160
|
+
this.onTrip?.();
|
|
161
|
+
} catch {
|
|
162
|
+
}
|
|
132
163
|
}
|
|
133
164
|
_reset() {
|
|
165
|
+
const wasRecovering = this.state !== "closed";
|
|
134
166
|
this.state = "closed";
|
|
135
167
|
this.consecutiveFailures = 0;
|
|
136
168
|
this.window = [];
|
|
137
169
|
this.openedAt = null;
|
|
170
|
+
if (wasRecovering) {
|
|
171
|
+
try {
|
|
172
|
+
this.onReset?.();
|
|
173
|
+
} catch {
|
|
174
|
+
}
|
|
175
|
+
}
|
|
138
176
|
}
|
|
139
177
|
/** Transition from open → half-open when cooldown elapses. */
|
|
140
178
|
_checkStateTransition() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/circuit-breaker.ts"],"names":[],"mappings":";AA6DA,IAAM,gCAAA,GAAmC,CAAA;AACzC,IAAM,8BAAA,GAAiC,IAAA;AAIvC,IAAM,sBAAA,GAAyB,CAAA;AAC/B,IAAM,iBAAA,GAAoB,GAAA;AAC1B,IAAM,4BAAA,GAA+B,EAAA;AACrC,IAAM,mBAAA,GAAsB,GAAA;AAarB,IAAM,iBAAN,MAAqB;AAAA,EACT,sBAAA;AAAA,EACA,mBAAA;AAAA,EACA,YAAA;AAAA,EACA,QAAA;AAAA,EACA,iBAAA;AAAA,EACA,UAAA;AAAA,EAET,KAAA,GAAsB,QAAA;AAAA,EACtB,mBAAA,GAAsB,CAAA;AAAA,EACtB,SAAuB,EAAC;AAAA,EACxB,aAAA,GAA+B,IAAA;AAAA,EAC/B,UAAA,GAA4B,IAAA;AAAA;AAAA,EAE5B,QAAA,GAA0B,IAAA;AAAA,EAElC,WAAA,CAAY,MAAA,GAA+B,EAAC,EAAG;AAC7C,IAAA,IAAA,CAAK,sBAAA,GAAyB,OAAO,sBAAA,IAA0B,gCAAA;AAC/D,I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AAW,KAAK,UAAA,EAAY;AAC9B,MAAA,IAAA,CAAK,KAAA,GAAQ,WAAA;AACb,MAAA,IAAA,CAAK,QAAA,GAAW,IAAA;AAAA,IAClB;AAAA,EACF;AAAA,EAEQ,aAAa,GAAA,EAAmB;AACtC,IAAA,MAAM,MAAA,GAAS,MAAM,IAAA,CAAK,QAAA;AAC1B,IAAA,IAAA,CAAK,MAAA,GAAS,KAAK,MAAA,CAAO,MAAA,CAAO,CAAC,CAAA,KAAM,CAAA,CAAE,MAAM,MAAM,CAAA;AAAA,EACxD;AACF","file":"circuit-breaker.js","sourcesContent":["/**\n * CircuitBreaker — prevents runaway bash/exec tool chains by:\n *\n * - Tripping on consecutive failures (models that keep repeating the\n * same failing command, e.g. `npm install` with wrong args in a loop)\n * - Tripping on slow call ratio (too many long-running commands suggest\n * a hung subprocess that the model doesn't know how to kill)\n * - Rate-limiting bursts (rapid succession of commands without reading\n * output suggests the model isn't processing results)\n * - Auto-recovering after a cooldown period so a fixed model can resume\n *\n * The breaker is owned by the ProcessRegistry so any tool that registers\n * a process participates in the same circuit. \"Per-tool\" isolation is\n * intentionally NOT implemented — the model treats bash/exec as one\n * resource pool; isolating them would let the model route around the\n * breaker by alternating which tool it uses.\n */\n\nexport interface CircuitBreakerConfig {\n /**\n * Consecutive failures before trip. Default: 5.\n * A single success resets this counter to 0.\n */\n maxConsecutiveFailures?: number | undefined;\n /**\n * Slow-call threshold in ms. A call that runs longer than this is\n * counted as \"slow\". Default: 60_000 (1 minute).\n */\n slowCallThresholdMs?: number | undefined;\n /**\n * Max slow calls before trip (within the sliding window). Default: 3.\n */\n maxSlowCalls?: number | undefined;\n /**\n * Sliding window for rate-limit and slow-call counting, in ms.\n * Default: 60_000 (1 minute).\n */\n windowMs?: number | undefined;\n /**\n * Max calls within the sliding window. Default: 30.\n * Burst exceeding this trips the breaker immediately.\n */\n maxCallsPerWindow?: number | undefined;\n /**\n * Cooldown before auto-recovery attempt, in ms. Default: 30_000 (30s).\n * After this the breaker enters \"half-open\" state and allows one call\n * through to test whether the problem is resolved.\n */\n cooldownMs?: number | undefined;\n}\n\ninterface CallRecord {\n at: number;\n /** True if the call threw or returned an is_error result. */\n failed: boolean;\n /** True if elapsed time exceeded slowCallThresholdMs. */\n slow: boolean;\n}\n\ntype BreakerState = 'closed' | 'open' | 'half-open';\n\nconst DEFAULT_MAX_CONSECUTIVE_FAILURES = 5;\nconst DEFAULT_SLOW_CALL_THRESHOLD_MS = 180_000;\n// 3 minutes — balanced against the 5-minute bash timeout. Commands\n// running <3min are normal; 3-5min are \"slow\" and count toward the\n// breaker. 3 consecutive slow calls trip the circuit.\nconst DEFAULT_MAX_SLOW_CALLS = 3;\nconst DEFAULT_WINDOW_MS = 60_000;\nconst DEFAULT_MAX_CALLS_PER_WINDOW = 30;\nconst DEFAULT_COOLDOWN_MS = 30_000;\n\nexport interface CircuitBreakerSnapshot {\n state: 'closed' | 'open' | 'half-open';\n consecutiveFailures: number;\n slowCallsInWindow: number;\n callsInWindow: number;\n windowMs: number;\n cooldownRemainingMs: number | null;\n lastFailureAt: number | null;\n lastSlowAt: number | null;\n}\n\nexport class CircuitBreaker {\n private readonly maxConsecutiveFailures: number;\n private readonly slowCallThresholdMs: number;\n private readonly maxSlowCalls: number;\n private readonly windowMs: number;\n private readonly maxCallsPerWindow: number;\n private readonly cooldownMs: number;\n\n private state: BreakerState = 'closed';\n private consecutiveFailures = 0;\n private window: CallRecord[] = [];\n private lastFailureAt: number | null = null;\n private lastSlowAt: number | null = null;\n /** Timestamp when the breaker was opened (for cooldown calculation). */\n private openedAt: number | null = null;\n\n constructor(config: CircuitBreakerConfig = {}) {\n this.maxConsecutiveFailures = config.maxConsecutiveFailures ?? DEFAULT_MAX_CONSECUTIVE_FAILURES;\n this.slowCallThresholdMs = config.slowCallThresholdMs ?? DEFAULT_SLOW_CALL_THRESHOLD_MS;\n this.maxSlowCalls = config.maxSlowCalls ?? DEFAULT_MAX_SLOW_CALLS;\n this.windowMs = config.windowMs ?? DEFAULT_WINDOW_MS;\n this.maxCallsPerWindow = config.maxCallsPerWindow ?? DEFAULT_MAX_CALLS_PER_WINDOW;\n this.cooldownMs = config.cooldownMs ?? DEFAULT_COOLDOWN_MS;\n }\n\n /**\n * Returns true if the circuit allows a new call to proceed.\n * When false, callers should abort the tool call and return a\n * circuit-breaker error instead of spawning a process.\n */\n get canProceed(): boolean {\n this._checkStateTransition();\n return this.state !== 'open';\n }\n\n /**\n * Snapshot of the current breaker state for observability (`/kill`).\n */\n snapshot(): CircuitBreakerSnapshot {\n this._checkStateTransition();\n const now = Date.now();\n let cooldownRemaining: number | null = null;\n if (this.openedAt !== null && this.state === 'open') {\n const elapsed = now - this.openedAt;\n cooldownRemaining = Math.max(0, this.cooldownMs - elapsed);\n }\n return {\n state: this.state,\n consecutiveFailures: this.consecutiveFailures,\n slowCallsInWindow: this.window.filter((c) => c.slow).length,\n callsInWindow: this.window.length,\n windowMs: this.windowMs,\n cooldownRemainingMs: cooldownRemaining,\n lastFailureAt: this.lastFailureAt,\n lastSlowAt: this.lastSlowAt,\n };\n }\n\n /**\n * Call this BEFORE spawning a bash/exec process.\n * Returns true if the call is allowed; false if the breaker is open.\n * When false, callers MUST NOT spawn a process.\n *\n * @param bypass - If true, skip the circuit breaker check entirely.\n * Use for background/fire-and-forget processes that should\n * not affect breaker state.\n */\n beforeCall(bypass = false): boolean {\n if (bypass) return true;\n this._checkStateTransition();\n if (this.state === 'open') return false;\n return true;\n }\n\n /**\n * Call this AFTER a bash/exec process finishes (success or failure).\n * `durationMs` is the wall-clock time the process ran.\n * `failed` is true when the process returned a non-zero exit code or\n * threw an exception before spawning.\n *\n * @param bypass - If true, do not update breaker state.\n * Use for background/fire-and-forget processes.\n */\n afterCall(durationMs: number, failed: boolean, bypass = false): void {\n if (bypass) return;\n\n const now = Date.now();\n\n if (this.state === 'half-open') {\n // First call through after cooldown — if it failed, go back to open.\n if (failed) {\n this._trip();\n return;\n }\n // Success in half-open → reset to closed.\n this._reset();\n return;\n }\n\n // Prune old records outside the sliding window.\n this._pruneWindow(now);\n\n const slow = durationMs >= this.slowCallThresholdMs;\n this.window.push({ at: now, failed, slow });\n\n if (failed) {\n this.consecutiveFailures++;\n this.lastFailureAt = now;\n if (this.consecutiveFailures >= this.maxConsecutiveFailures) {\n this._trip();\n }\n return;\n }\n\n // Success: reset consecutive failure counter.\n this.consecutiveFailures = 0;\n\n if (slow) {\n this.lastSlowAt = now;\n const slowCount = this.window.filter((c) => c.slow).length;\n if (slowCount >= this.maxSlowCalls) {\n this._trip();\n }\n }\n\n const callCount = this.window.length;\n if (callCount >= this.maxCallsPerWindow) {\n // Rate limit exceeded. This is a soft trip — we reset the window\n // and let the next call try immediately (the caller will still see\n // canProceed=false until the window drains naturally).\n this._trip();\n }\n }\n\n /** Force the breaker open. Used by /kill force and Ctrl+C. */\n forceOpen(): void {\n this._trip();\n }\n\n /** Force a reset to closed. Used by tests and /kill reset. */\n forceReset(): void {\n this._reset();\n }\n\n private _trip(): void {\n if (this.state === 'open') return; // already open\n this.state = 'open';\n this.openedAt = Date.now();\n }\n\n private _reset(): void {\n this.state = 'closed';\n this.consecutiveFailures = 0;\n this.window = [];\n this.openedAt = null;\n }\n\n /** Transition from open → half-open when cooldown elapses. */\n private _checkStateTransition(): void {\n if (this.state !== 'open' || this.openedAt === null) return;\n const elapsed = Date.now() - this.openedAt;\n if (elapsed >= this.cooldownMs) {\n this.state = 'half-open';\n this.openedAt = null;\n }\n }\n\n private _pruneWindow(now: number): void {\n const cutoff = now - this.windowMs;\n this.window = this.window.filter((c) => c.at >= cutoff);\n }\n}"]}
|
|
1
|
+
{"version":3,"sources":["../src/circuit-breaker.ts"],"names":[],"mappings":";AA6DA,IAAM,gCAAA,GAAmC,CAAA;AACzC,IAAM,8BAAA,GAAiC,IAAA;AAIvC,IAAM,sBAAA,GAAyB,CAAA;AAC/B,IAAM,iBAAA,GAAoB,GAAA;AAC1B,IAAM,4BAAA,GAA+B,EAAA;AACrC,IAAM,m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AU,IAAA,CAAK,GAAA,EAAI,GAAI,IAAA,CAAK,QAAA;AAClC,IAAA,IAAI,OAAA,IAAW,KAAK,UAAA,EAAY;AAC9B,MAAA,IAAA,CAAK,KAAA,GAAQ,WAAA;AACb,MAAA,IAAA,CAAK,QAAA,GAAW,IAAA;AAAA,IAClB;AAAA,EACF;AAAA,EAEQ,aAAa,GAAA,EAAmB;AACtC,IAAA,MAAM,MAAA,GAAS,MAAM,IAAA,CAAK,QAAA;AAC1B,IAAA,IAAA,CAAK,MAAA,GAAS,KAAK,MAAA,CAAO,MAAA,CAAO,CAAC,CAAA,KAAM,CAAA,CAAE,MAAM,MAAM,CAAA;AAAA,EACxD;AACF","file":"circuit-breaker.js","sourcesContent":["/**\n * CircuitBreaker — prevents runaway bash/exec tool chains by:\n *\n * - Tripping on consecutive failures (models that keep repeating the\n * same failing command, e.g. `npm install` with wrong args in a loop)\n * - Tripping on slow call ratio (too many long-running commands suggest\n * a hung subprocess that the model doesn't know how to kill)\n * - Rate-limiting bursts (rapid succession of commands without reading\n * output suggests the model isn't processing results)\n * - Auto-recovering after a cooldown period so a fixed model can resume\n *\n * The breaker is owned by the ProcessRegistry so any tool that registers\n * a process participates in the same circuit. \"Per-tool\" isolation is\n * intentionally NOT implemented — the model treats bash/exec as one\n * resource pool; isolating them would let the model route around the\n * breaker by alternating which tool it uses.\n */\n\nexport interface CircuitBreakerConfig {\n /**\n * Consecutive failures before trip. Default: 5.\n * A single success resets this counter to 0.\n */\n maxConsecutiveFailures?: number | undefined;\n /**\n * Slow-call threshold in ms. A call that runs longer than this is\n * counted as \"slow\". Default: 60_000 (1 minute).\n */\n slowCallThresholdMs?: number | undefined;\n /**\n * Max slow calls before trip (within the sliding window). Default: 3.\n */\n maxSlowCalls?: number | undefined;\n /**\n * Sliding window for rate-limit and slow-call counting, in ms.\n * Default: 60_000 (1 minute).\n */\n windowMs?: number | undefined;\n /**\n * Max calls within the sliding window. Default: 30.\n * Burst exceeding this trips the breaker immediately.\n */\n maxCallsPerWindow?: number | undefined;\n /**\n * Cooldown before auto-recovery attempt, in ms. Default: 30_000 (30s).\n * After this the breaker enters \"half-open\" state and allows one call\n * through to test whether the problem is resolved.\n */\n cooldownMs?: number | undefined;\n}\n\ninterface CallRecord {\n at: number;\n /** True if the call threw or returned an is_error result. */\n failed: boolean;\n /** True if elapsed time exceeded slowCallThresholdMs. */\n slow: boolean;\n}\n\ntype BreakerState = 'closed' | 'open' | 'half-open';\n\nconst DEFAULT_MAX_CONSECUTIVE_FAILURES = 5;\nconst DEFAULT_SLOW_CALL_THRESHOLD_MS = 180_000;\n// 3 minutes — balanced against the 5-minute bash timeout. Commands\n// running <3min are normal; 3-5min are \"slow\" and count toward the\n// breaker. 3 consecutive slow calls trip the circuit.\nconst DEFAULT_MAX_SLOW_CALLS = 3;\nconst DEFAULT_WINDOW_MS = 60_000;\nconst DEFAULT_MAX_CALLS_PER_WINDOW = 30;\nconst DEFAULT_COOLDOWN_MS = 30_000;\n\nexport interface CircuitBreakerSnapshot {\n state: 'closed' | 'open' | 'half-open';\n consecutiveFailures: number;\n slowCallsInWindow: number;\n callsInWindow: number;\n windowMs: number;\n cooldownRemainingMs: number | null;\n lastFailureAt: number | null;\n lastSlowAt: number | null;\n}\n\nexport class CircuitBreaker {\n private readonly maxConsecutiveFailures: number;\n private readonly slowCallThresholdMs: number;\n private readonly maxSlowCalls: number;\n private readonly windowMs: number;\n private readonly maxCallsPerWindow: number;\n private readonly cooldownMs: number;\n\n private state: BreakerState = 'closed';\n private consecutiveFailures = 0;\n private window: CallRecord[] = [];\n private lastFailureAt: number | null = null;\n private lastSlowAt: number | null = null;\n /** Timestamp when the breaker was opened (for cooldown calculation). */\n private openedAt: number | null = null;\n\n /**\n * Master enable flag. When false the breaker is bypassed: `beforeCall`\n * always returns true and `afterCall` records nothing. The class itself\n * defaults to enabled (so the standalone unit tests exercise tripping); the\n * ProcessRegistry flips this off until the user opts in via `/settings`.\n */\n private enabled = true;\n\n /**\n * Fired (best-effort) when the breaker transitions into the `open` state.\n * The registry uses this to arm its auto kill/reset countdown.\n */\n onTrip?: (() => void) | undefined;\n /**\n * Fired (best-effort) when the breaker returns to `closed` after having been\n * open/half-open. The registry uses this to cancel a pending kill/reset.\n */\n onReset?: (() => void) | undefined;\n\n constructor(config: CircuitBreakerConfig = {}) {\n this.maxConsecutiveFailures = config.maxConsecutiveFailures ?? DEFAULT_MAX_CONSECUTIVE_FAILURES;\n this.slowCallThresholdMs = config.slowCallThresholdMs ?? DEFAULT_SLOW_CALL_THRESHOLD_MS;\n this.maxSlowCalls = config.maxSlowCalls ?? DEFAULT_MAX_SLOW_CALLS;\n this.windowMs = config.windowMs ?? DEFAULT_WINDOW_MS;\n this.maxCallsPerWindow = config.maxCallsPerWindow ?? DEFAULT_MAX_CALLS_PER_WINDOW;\n this.cooldownMs = config.cooldownMs ?? DEFAULT_COOLDOWN_MS;\n }\n\n /** Toggle the master enable. Disabling resets to a clean `closed` state. */\n setEnabled(enabled: boolean): void {\n if (this.enabled === enabled) return;\n this.enabled = enabled;\n if (!enabled) this._reset();\n }\n\n get isEnabled(): boolean {\n return this.enabled;\n }\n\n /**\n * Returns true if the circuit allows a new call to proceed.\n * When false, callers should abort the tool call and return a\n * circuit-breaker error instead of spawning a process.\n */\n get canProceed(): boolean {\n if (!this.enabled) return true;\n this._checkStateTransition();\n return this.state !== 'open';\n }\n\n /**\n * Snapshot of the current breaker state for observability (`/kill`).\n */\n snapshot(): CircuitBreakerSnapshot {\n this._checkStateTransition();\n const now = Date.now();\n let cooldownRemaining: number | null = null;\n if (this.openedAt !== null && this.state === 'open') {\n const elapsed = now - this.openedAt;\n cooldownRemaining = Math.max(0, this.cooldownMs - elapsed);\n }\n return {\n state: this.state,\n consecutiveFailures: this.consecutiveFailures,\n slowCallsInWindow: this.window.filter((c) => c.slow).length,\n callsInWindow: this.window.length,\n windowMs: this.windowMs,\n cooldownRemainingMs: cooldownRemaining,\n lastFailureAt: this.lastFailureAt,\n lastSlowAt: this.lastSlowAt,\n };\n }\n\n /**\n * Call this BEFORE spawning a bash/exec process.\n * Returns true if the call is allowed; false if the breaker is open.\n * When false, callers MUST NOT spawn a process.\n *\n * @param bypass - If true, skip the circuit breaker check entirely.\n * Use for background/fire-and-forget processes that should\n * not affect breaker state.\n */\n beforeCall(bypass = false): boolean {\n if (bypass || !this.enabled) return true;\n this._checkStateTransition();\n if (this.state === 'open') return false;\n return true;\n }\n\n /**\n * Call this AFTER a bash/exec process finishes (success or failure).\n * `durationMs` is the wall-clock time the process ran.\n * `failed` is true when the process returned a non-zero exit code or\n * threw an exception before spawning.\n *\n * @param bypass - If true, do not update breaker state.\n * Use for background/fire-and-forget processes.\n */\n afterCall(durationMs: number, failed: boolean, bypass = false): void {\n if (bypass || !this.enabled) return;\n\n const now = Date.now();\n\n if (this.state === 'half-open') {\n // First call through after cooldown — if it failed, go back to open.\n if (failed) {\n this._trip();\n return;\n }\n // Success in half-open → reset to closed.\n this._reset();\n return;\n }\n\n // Prune old records outside the sliding window.\n this._pruneWindow(now);\n\n const slow = durationMs >= this.slowCallThresholdMs;\n this.window.push({ at: now, failed, slow });\n\n if (failed) {\n this.consecutiveFailures++;\n this.lastFailureAt = now;\n if (this.consecutiveFailures >= this.maxConsecutiveFailures) {\n this._trip();\n }\n return;\n }\n\n // Success: reset consecutive failure counter.\n this.consecutiveFailures = 0;\n\n if (slow) {\n this.lastSlowAt = now;\n const slowCount = this.window.filter((c) => c.slow).length;\n if (slowCount >= this.maxSlowCalls) {\n this._trip();\n }\n }\n\n const callCount = this.window.length;\n if (callCount >= this.maxCallsPerWindow) {\n // Rate limit exceeded. This is a soft trip — we reset the window\n // and let the next call try immediately (the caller will still see\n // canProceed=false until the window drains naturally).\n this._trip();\n }\n }\n\n /** Force the breaker open. Used by /kill force and Ctrl+C. */\n forceOpen(): void {\n this._trip();\n }\n\n /** Force a reset to closed. Used by tests and /kill reset. */\n forceReset(): void {\n this._reset();\n }\n\n private _trip(): void {\n if (this.state === 'open') return; // already open\n this.state = 'open';\n this.openedAt = Date.now();\n // Best-effort: never let a listener failure corrupt breaker state.\n try {\n this.onTrip?.();\n } catch {\n /* ignored — observability hook only */\n }\n }\n\n private _reset(): void {\n const wasRecovering = this.state !== 'closed';\n this.state = 'closed';\n this.consecutiveFailures = 0;\n this.window = [];\n this.openedAt = null;\n // Only notify on a real recovery (open/half-open → closed), not on the\n // initial closed state or an idempotent re-reset.\n if (wasRecovering) {\n try {\n this.onReset?.();\n } catch {\n /* ignored — observability hook only */\n }\n }\n }\n\n /** Transition from open → half-open when cooldown elapses. */\n private _checkStateTransition(): void {\n if (this.state !== 'open' || this.openedAt === null) return;\n const elapsed = Date.now() - this.openedAt;\n if (elapsed >= this.cooldownMs) {\n this.state = 'half-open';\n this.openedAt = null;\n }\n }\n\n private _pruneWindow(now: number): void {\n const cutoff = now - this.windowMs;\n this.window = this.window.filter((c) => c.at >= cutoff);\n }\n}"]}
|
|
@@ -128,6 +128,18 @@ declare class IndexStore {
|
|
|
128
128
|
* Replaces any existing refs from the same source (idempotent on re-index).
|
|
129
129
|
*/
|
|
130
130
|
insertRefs(fromId: number, refs: Ref[]): void;
|
|
131
|
+
/**
|
|
132
|
+
* Bulk-insert refs for many source symbols in a single transaction.
|
|
133
|
+
*
|
|
134
|
+
* Unlike {@link insertRefs} this does NOT delete per source id — the caller
|
|
135
|
+
* (the indexer) has already cleared stale refs for the file via
|
|
136
|
+
* {@link deleteRefsForFile}, so the per-source DELETE would be redundant work
|
|
137
|
+
* repeated once per symbol. One transaction for the whole file instead of one
|
|
138
|
+
* per symbol turns an O(symbols) transaction count into O(1).
|
|
139
|
+
*
|
|
140
|
+
* Each ref's own {@link Ref.fromId} is used; pass an empty array to no-op.
|
|
141
|
+
*/
|
|
142
|
+
insertRefsBatch(refs: Ref[]): void;
|
|
131
143
|
/**
|
|
132
144
|
* Delete all refs whose source symbols are in a given file.
|
|
133
145
|
* Used when re-indexing a file to clear stale refs.
|
|
@@ -136,6 +148,10 @@ declare class IndexStore {
|
|
|
136
148
|
/**
|
|
137
149
|
* Resolve `to_name` → `to_id` for all refs that have a name but no id.
|
|
138
150
|
* Call this after all symbols have been inserted to fill in cross-references.
|
|
151
|
+
*
|
|
152
|
+
* Single statement: the `to_name IN (SELECT name FROM symbols)` guard restricts
|
|
153
|
+
* the UPDATE to refs that will actually resolve, so `.changes` counts only refs
|
|
154
|
+
* that found a target — matching the previous per-row loop's return value.
|
|
139
155
|
*/
|
|
140
156
|
resolveRefs(): number;
|
|
141
157
|
/**
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { resolveWstackPaths, expectDefined, compileGlob, truncate } from '@wrongstack/core';
|
|
2
|
+
import { toErrorMessage } from '@wrongstack/core/utils';
|
|
2
3
|
import { createRequire } from 'node:module';
|
|
3
4
|
import * as fs from 'node:fs';
|
|
4
5
|
import { writeFileSync, mkdirSync } from 'node:fs';
|
|
@@ -278,7 +279,7 @@ function loadDatabaseSync() {
|
|
|
278
279
|
DatabaseSyncCtor = req("node:sqlite").DatabaseSync;
|
|
279
280
|
} catch (err) {
|
|
280
281
|
throw new Error(
|
|
281
|
-
`The codebase index needs Node's built-in SQLite (node:sqlite), available since Node 22.5. This runtime doesn't provide it: ${
|
|
282
|
+
`The codebase index needs Node's built-in SQLite (node:sqlite), available since Node 22.5. This runtime doesn't provide it: ${toErrorMessage(err)}`
|
|
282
283
|
);
|
|
283
284
|
}
|
|
284
285
|
return DatabaseSyncCtor;
|
|
@@ -724,39 +725,57 @@ var IndexStore = class {
|
|
|
724
725
|
}
|
|
725
726
|
});
|
|
726
727
|
}
|
|
728
|
+
/**
|
|
729
|
+
* Bulk-insert refs for many source symbols in a single transaction.
|
|
730
|
+
*
|
|
731
|
+
* Unlike {@link insertRefs} this does NOT delete per source id — the caller
|
|
732
|
+
* (the indexer) has already cleared stale refs for the file via
|
|
733
|
+
* {@link deleteRefsForFile}, so the per-source DELETE would be redundant work
|
|
734
|
+
* repeated once per symbol. One transaction for the whole file instead of one
|
|
735
|
+
* per symbol turns an O(symbols) transaction count into O(1).
|
|
736
|
+
*
|
|
737
|
+
* Each ref's own {@link Ref.fromId} is used; pass an empty array to no-op.
|
|
738
|
+
*/
|
|
739
|
+
insertRefsBatch(refs) {
|
|
740
|
+
if (refs.length === 0) return;
|
|
741
|
+
this.runWithRetry(() => {
|
|
742
|
+
const stmt = this.db.prepare(
|
|
743
|
+
`INSERT INTO refs(from_id, to_name, to_id, call_type, line)
|
|
744
|
+
VALUES (?, ?, ?, ?, ?)`
|
|
745
|
+
);
|
|
746
|
+
for (const ref of refs) {
|
|
747
|
+
stmt.run(ref.fromId, ref.toName, ref.toId ?? null, ref.callType, ref.line);
|
|
748
|
+
}
|
|
749
|
+
});
|
|
750
|
+
}
|
|
727
751
|
/**
|
|
728
752
|
* Delete all refs whose source symbols are in a given file.
|
|
729
753
|
* Used when re-indexing a file to clear stale refs.
|
|
730
754
|
*/
|
|
731
755
|
deleteRefsForFile(file) {
|
|
732
756
|
this.runWithRetry(() => {
|
|
733
|
-
|
|
734
|
-
"SELECT id FROM symbols WHERE file = ?"
|
|
735
|
-
).
|
|
736
|
-
if (!ids.length) return;
|
|
737
|
-
const placeholders = ids.map(() => "?").join(",");
|
|
738
|
-
this.db.prepare(`DELETE FROM refs WHERE from_id IN (${placeholders})`).run(...ids.map((r) => r.id));
|
|
757
|
+
this.db.prepare(
|
|
758
|
+
"DELETE FROM refs WHERE from_id IN (SELECT id FROM symbols WHERE file = ?)"
|
|
759
|
+
).run(file);
|
|
739
760
|
});
|
|
740
761
|
}
|
|
741
762
|
/**
|
|
742
763
|
* Resolve `to_name` → `to_id` for all refs that have a name but no id.
|
|
743
764
|
* Call this after all symbols have been inserted to fill in cross-references.
|
|
765
|
+
*
|
|
766
|
+
* Single statement: the `to_name IN (SELECT name FROM symbols)` guard restricts
|
|
767
|
+
* the UPDATE to refs that will actually resolve, so `.changes` counts only refs
|
|
768
|
+
* that found a target — matching the previous per-row loop's return value.
|
|
744
769
|
*/
|
|
745
770
|
resolveRefs() {
|
|
746
771
|
return this.runWithRetry(() => {
|
|
747
|
-
const
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
if (first) {
|
|
755
|
-
this.db.prepare("UPDATE refs SET to_id = ? WHERE id = ?").run(first.id, row.id);
|
|
756
|
-
resolved++;
|
|
757
|
-
}
|
|
758
|
-
}
|
|
759
|
-
return resolved;
|
|
772
|
+
const result = this.db.prepare(
|
|
773
|
+
`UPDATE refs SET to_id = (
|
|
774
|
+
SELECT id FROM symbols WHERE name = refs.to_name LIMIT 1
|
|
775
|
+
) WHERE to_id IS NULL AND to_name IS NOT NULL
|
|
776
|
+
AND to_name IN (SELECT name FROM symbols)`
|
|
777
|
+
).run();
|
|
778
|
+
return result.changes ?? 0;
|
|
760
779
|
});
|
|
761
780
|
}
|
|
762
781
|
/**
|
|
@@ -863,7 +882,7 @@ function getSignature(node, sourceFile) {
|
|
|
863
882
|
}
|
|
864
883
|
function getJsDoc(node, sourceFile) {
|
|
865
884
|
const fullText = sourceFile.getFullText();
|
|
866
|
-
const nodePos = node.
|
|
885
|
+
const nodePos = node.getFullStart();
|
|
867
886
|
const comments = ts.getLeadingCommentRanges(fullText, nodePos);
|
|
868
887
|
if (!comments) return "";
|
|
869
888
|
for (const range of comments) {
|
|
@@ -2231,13 +2250,26 @@ async function runIndexerWithStore(store, opts) {
|
|
|
2231
2250
|
symbolsIndexed += count;
|
|
2232
2251
|
langStats[lang] = (langStats[lang] ?? 0) + count;
|
|
2233
2252
|
if (parsed.refs && parsed.refs.length > 0) {
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
if (
|
|
2238
|
-
|
|
2239
|
-
|
|
2253
|
+
const refsByLine = /* @__PURE__ */ new Map();
|
|
2254
|
+
for (const r of parsed.refs) {
|
|
2255
|
+
let arr = refsByLine.get(r.line);
|
|
2256
|
+
if (!arr) {
|
|
2257
|
+
arr = [];
|
|
2258
|
+
refsByLine.set(r.line, arr);
|
|
2240
2259
|
}
|
|
2260
|
+
arr.push(r);
|
|
2261
|
+
}
|
|
2262
|
+
const batch = [];
|
|
2263
|
+
for (const sym of symbolsWithIds) {
|
|
2264
|
+
const symRefs = refsByLine.get(sym.line);
|
|
2265
|
+
if (symRefs) {
|
|
2266
|
+
for (const r of symRefs) {
|
|
2267
|
+
batch.push({ ...r, fromId: sym.id });
|
|
2268
|
+
}
|
|
2269
|
+
}
|
|
2270
|
+
}
|
|
2271
|
+
if (batch.length > 0) {
|
|
2272
|
+
store.insertRefsBatch(batch);
|
|
2241
2273
|
}
|
|
2242
2274
|
}
|
|
2243
2275
|
store.upsertFile({
|
|
@@ -2630,6 +2662,7 @@ async function codebaseIndexStats(args, opts = {}) {
|
|
|
2630
2662
|
var codebaseIndexTool = {
|
|
2631
2663
|
name: "codebase-index",
|
|
2632
2664
|
category: "Project",
|
|
2665
|
+
icon: "index",
|
|
2633
2666
|
description: "Build or incrementally update the project-wide symbol index. This powers fast codebase search and understanding. By default it only processes files that have changed since the last indexing run.",
|
|
2634
2667
|
usageHint: "IMPORTANT FOR LARGE CODEBASES:\n\n- First run (or after major changes): consider `force: true` for a clean rebuild.\n- Normal usage: call without arguments for fast incremental updates.\n- Use `langs` to restrict to specific languages if you only care about certain parts of the project.\nThis tool is relatively expensive \u2014 do not call it on every turn. Use it when the index is stale or before heavy codebase-search sessions.",
|
|
2635
2668
|
permission: "confirm",
|
|
@@ -2686,6 +2719,7 @@ var codebaseIndexTool = {
|
|
|
2686
2719
|
var codebaseSearchTool = {
|
|
2687
2720
|
name: "codebase-search",
|
|
2688
2721
|
category: "Project",
|
|
2722
|
+
icon: "index",
|
|
2689
2723
|
description: "Semantic/keyword search over the indexed codebase symbols (functions, classes, interfaces, etc.). Uses BM25 ranking. Much more powerful and structured than raw `grep` for finding code by name or concept.",
|
|
2690
2724
|
usageHint: "PREFERRED FOR CODE UNDERSTANDING:\n\n- Use when you need to find where something is defined or used by name.\n- `kind` filter is very useful (e.g. only functions or only interfaces).\n- Combine with `file` filter to scope to a specific directory or module.\nThis is generally better than `grep` when you are looking for symbols rather than arbitrary text patterns.",
|
|
2691
2725
|
permission: "auto",
|
|
@@ -2774,6 +2808,7 @@ var codebaseSearchTool = {
|
|
|
2774
2808
|
var codebaseStatsTool = {
|
|
2775
2809
|
name: "codebase-stats",
|
|
2776
2810
|
category: "Project",
|
|
2811
|
+
icon: "index",
|
|
2777
2812
|
description: "Return health and statistics about the current symbol index (total symbols, files, language/kind breakdown, size, last update). Useful to decide whether to re-index.",
|
|
2778
2813
|
usageHint: "CALL BEFORE HEAVY CODEBASE-SEARCH WORK:\n\n- Use to see if the index is up-to-date or needs a refresh.\n- No arguments required.\n- Helps avoid wasting tokens on searches against a stale index.\nLightweight and safe to call frequently.",
|
|
2779
2814
|
permission: "auto",
|