@wrongstack/tools 0.5.6 → 0.5.7
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/bash.d.ts +2 -1
- package/dist/bash.js +361 -3
- package/dist/bash.js.map +1 -1
- package/dist/builtin.js +389 -5
- package/dist/builtin.js.map +1 -1
- package/dist/circuit-breaker.d.ts +111 -0
- package/dist/circuit-breaker.js +150 -0
- package/dist/circuit-breaker.js.map +1 -0
- package/dist/exec.js +346 -2
- package/dist/exec.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +393 -6
- package/dist/index.js.map +1 -1
- package/dist/pack.js +389 -5
- package/dist/pack.js.map +1 -1
- package/dist/process-registry.d.ts +112 -0
- package/dist/process-registry.js +327 -0
- package/dist/process-registry.js.map +1 -0
- package/package.json +10 -2
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CircuitBreaker — prevents runaway bash/exec tool chains by:
|
|
3
|
+
*
|
|
4
|
+
* - Tripping on consecutive failures (models that keep repeating the
|
|
5
|
+
* same failing command, e.g. `npm install` with wrong args in a loop)
|
|
6
|
+
* - Tripping on slow call ratio (too many long-running commands suggest
|
|
7
|
+
* a hung subprocess that the model doesn't know how to kill)
|
|
8
|
+
* - Rate-limiting bursts (rapid succession of commands without reading
|
|
9
|
+
* output suggests the model isn't processing results)
|
|
10
|
+
* - Auto-recovering after a cooldown period so a fixed model can resume
|
|
11
|
+
*
|
|
12
|
+
* The breaker is owned by the ProcessRegistry so any tool that registers
|
|
13
|
+
* a process participates in the same circuit. "Per-tool" isolation is
|
|
14
|
+
* intentionally NOT implemented — the model treats bash/exec as one
|
|
15
|
+
* resource pool; isolating them would let the model route around the
|
|
16
|
+
* breaker by alternating which tool it uses.
|
|
17
|
+
*/
|
|
18
|
+
interface CircuitBreakerConfig {
|
|
19
|
+
/**
|
|
20
|
+
* Consecutive failures before trip. Default: 5.
|
|
21
|
+
* A single success resets this counter to 0.
|
|
22
|
+
*/
|
|
23
|
+
maxConsecutiveFailures?: number;
|
|
24
|
+
/**
|
|
25
|
+
* Slow-call threshold in ms. A call that runs longer than this is
|
|
26
|
+
* counted as "slow". Default: 60_000 (1 minute).
|
|
27
|
+
*/
|
|
28
|
+
slowCallThresholdMs?: number;
|
|
29
|
+
/**
|
|
30
|
+
* Max slow calls before trip (within the sliding window). Default: 3.
|
|
31
|
+
*/
|
|
32
|
+
maxSlowCalls?: number;
|
|
33
|
+
/**
|
|
34
|
+
* Sliding window for rate-limit and slow-call counting, in ms.
|
|
35
|
+
* Default: 60_000 (1 minute).
|
|
36
|
+
*/
|
|
37
|
+
windowMs?: number;
|
|
38
|
+
/**
|
|
39
|
+
* Max calls within the sliding window. Default: 30.
|
|
40
|
+
* Burst exceeding this trips the breaker immediately.
|
|
41
|
+
*/
|
|
42
|
+
maxCallsPerWindow?: number;
|
|
43
|
+
/**
|
|
44
|
+
* Cooldown before auto-recovery attempt, in ms. Default: 30_000 (30s).
|
|
45
|
+
* After this the breaker enters "half-open" state and allows one call
|
|
46
|
+
* through to test whether the problem is resolved.
|
|
47
|
+
*/
|
|
48
|
+
cooldownMs?: number;
|
|
49
|
+
}
|
|
50
|
+
interface CircuitBreakerSnapshot {
|
|
51
|
+
state: 'closed' | 'open' | 'half-open';
|
|
52
|
+
consecutiveFailures: number;
|
|
53
|
+
slowCallsInWindow: number;
|
|
54
|
+
callsInWindow: number;
|
|
55
|
+
windowMs: number;
|
|
56
|
+
cooldownRemainingMs: number | null;
|
|
57
|
+
lastFailureAt: number | null;
|
|
58
|
+
lastSlowAt: number | null;
|
|
59
|
+
}
|
|
60
|
+
declare class CircuitBreaker {
|
|
61
|
+
private readonly maxConsecutiveFailures;
|
|
62
|
+
private readonly slowCallThresholdMs;
|
|
63
|
+
private readonly maxSlowCalls;
|
|
64
|
+
private readonly windowMs;
|
|
65
|
+
private readonly maxCallsPerWindow;
|
|
66
|
+
private readonly cooldownMs;
|
|
67
|
+
private state;
|
|
68
|
+
private consecutiveFailures;
|
|
69
|
+
private window;
|
|
70
|
+
private lastFailureAt;
|
|
71
|
+
private lastSlowAt;
|
|
72
|
+
/** Timestamp when the breaker was opened (for cooldown calculation). */
|
|
73
|
+
private openedAt;
|
|
74
|
+
/** Timestamp when the last call ran (for half-open gate). */
|
|
75
|
+
private lastCallAt;
|
|
76
|
+
constructor(config?: CircuitBreakerConfig);
|
|
77
|
+
/**
|
|
78
|
+
* Returns true if the circuit allows a new call to proceed.
|
|
79
|
+
* When false, callers should abort the tool call and return a
|
|
80
|
+
* circuit-breaker error instead of spawning a process.
|
|
81
|
+
*/
|
|
82
|
+
get canProceed(): boolean;
|
|
83
|
+
/**
|
|
84
|
+
* Snapshot of the current breaker state for observability (`/kill`).
|
|
85
|
+
*/
|
|
86
|
+
snapshot(): CircuitBreakerSnapshot;
|
|
87
|
+
/**
|
|
88
|
+
* Call this BEFORE spawning a bash/exec process.
|
|
89
|
+
* Returns true if the call is allowed; false if the breaker is open.
|
|
90
|
+
* When false, callers MUST NOT spawn a process.
|
|
91
|
+
*/
|
|
92
|
+
beforeCall(): boolean;
|
|
93
|
+
/**
|
|
94
|
+
* Call this AFTER a bash/exec process finishes (success or failure).
|
|
95
|
+
* `durationMs` is the wall-clock time the process ran.
|
|
96
|
+
* `failed` is true when the process returned a non-zero exit code or
|
|
97
|
+
* threw an exception before spawning.
|
|
98
|
+
*/
|
|
99
|
+
afterCall(durationMs: number, failed: boolean): void;
|
|
100
|
+
/** Force the breaker open. Used by /kill force and Ctrl+C. */
|
|
101
|
+
forceOpen(): void;
|
|
102
|
+
/** Force a reset to closed. Used by tests and /kill reset. */
|
|
103
|
+
forceReset(): void;
|
|
104
|
+
private _trip;
|
|
105
|
+
private _reset;
|
|
106
|
+
/** Transition from open → half-open when cooldown elapses. */
|
|
107
|
+
private _checkStateTransition;
|
|
108
|
+
private _pruneWindow;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export { CircuitBreaker, type CircuitBreakerConfig, type CircuitBreakerSnapshot };
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
// src/circuit-breaker.ts
|
|
2
|
+
var DEFAULT_MAX_CONSECUTIVE_FAILURES = 5;
|
|
3
|
+
var DEFAULT_SLOW_CALL_THRESHOLD_MS = 6e4;
|
|
4
|
+
var DEFAULT_MAX_SLOW_CALLS = 3;
|
|
5
|
+
var DEFAULT_WINDOW_MS = 6e4;
|
|
6
|
+
var DEFAULT_MAX_CALLS_PER_WINDOW = 30;
|
|
7
|
+
var DEFAULT_COOLDOWN_MS = 3e4;
|
|
8
|
+
var CircuitBreaker = class {
|
|
9
|
+
maxConsecutiveFailures;
|
|
10
|
+
slowCallThresholdMs;
|
|
11
|
+
maxSlowCalls;
|
|
12
|
+
windowMs;
|
|
13
|
+
maxCallsPerWindow;
|
|
14
|
+
cooldownMs;
|
|
15
|
+
state = "closed";
|
|
16
|
+
consecutiveFailures = 0;
|
|
17
|
+
window = [];
|
|
18
|
+
lastFailureAt = null;
|
|
19
|
+
lastSlowAt = null;
|
|
20
|
+
/** Timestamp when the breaker was opened (for cooldown calculation). */
|
|
21
|
+
openedAt = null;
|
|
22
|
+
/** Timestamp when the last call ran (for half-open gate). */
|
|
23
|
+
lastCallAt = null;
|
|
24
|
+
constructor(config = {}) {
|
|
25
|
+
this.maxConsecutiveFailures = config.maxConsecutiveFailures ?? DEFAULT_MAX_CONSECUTIVE_FAILURES;
|
|
26
|
+
this.slowCallThresholdMs = config.slowCallThresholdMs ?? DEFAULT_SLOW_CALL_THRESHOLD_MS;
|
|
27
|
+
this.maxSlowCalls = config.maxSlowCalls ?? DEFAULT_MAX_SLOW_CALLS;
|
|
28
|
+
this.windowMs = config.windowMs ?? DEFAULT_WINDOW_MS;
|
|
29
|
+
this.maxCallsPerWindow = config.maxCallsPerWindow ?? DEFAULT_MAX_CALLS_PER_WINDOW;
|
|
30
|
+
this.cooldownMs = config.cooldownMs ?? DEFAULT_COOLDOWN_MS;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Returns true if the circuit allows a new call to proceed.
|
|
34
|
+
* When false, callers should abort the tool call and return a
|
|
35
|
+
* circuit-breaker error instead of spawning a process.
|
|
36
|
+
*/
|
|
37
|
+
get canProceed() {
|
|
38
|
+
this._checkStateTransition();
|
|
39
|
+
return this.state !== "open";
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Snapshot of the current breaker state for observability (`/kill`).
|
|
43
|
+
*/
|
|
44
|
+
snapshot() {
|
|
45
|
+
this._checkStateTransition();
|
|
46
|
+
const now = Date.now();
|
|
47
|
+
let cooldownRemaining = null;
|
|
48
|
+
if (this.openedAt !== null && this.state === "open") {
|
|
49
|
+
const elapsed = now - this.openedAt;
|
|
50
|
+
cooldownRemaining = Math.max(0, this.cooldownMs - elapsed);
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
state: this.state,
|
|
54
|
+
consecutiveFailures: this.consecutiveFailures,
|
|
55
|
+
slowCallsInWindow: this.window.filter((c) => c.slow).length,
|
|
56
|
+
callsInWindow: this.window.length,
|
|
57
|
+
windowMs: this.windowMs,
|
|
58
|
+
cooldownRemainingMs: cooldownRemaining,
|
|
59
|
+
lastFailureAt: this.lastFailureAt,
|
|
60
|
+
lastSlowAt: this.lastSlowAt
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Call this BEFORE spawning a bash/exec process.
|
|
65
|
+
* Returns true if the call is allowed; false if the breaker is open.
|
|
66
|
+
* When false, callers MUST NOT spawn a process.
|
|
67
|
+
*/
|
|
68
|
+
beforeCall() {
|
|
69
|
+
this._checkStateTransition();
|
|
70
|
+
if (this.state === "open") return false;
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Call this AFTER a bash/exec process finishes (success or failure).
|
|
75
|
+
* `durationMs` is the wall-clock time the process ran.
|
|
76
|
+
* `failed` is true when the process returned a non-zero exit code or
|
|
77
|
+
* threw an exception before spawning.
|
|
78
|
+
*/
|
|
79
|
+
afterCall(durationMs, failed) {
|
|
80
|
+
const now = Date.now();
|
|
81
|
+
this.lastCallAt = now;
|
|
82
|
+
if (this.state === "half-open") {
|
|
83
|
+
if (failed) {
|
|
84
|
+
this._trip();
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
this._reset();
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
this._pruneWindow(now);
|
|
91
|
+
const slow = durationMs >= this.slowCallThresholdMs;
|
|
92
|
+
this.window.push({ at: now, failed, slow });
|
|
93
|
+
if (failed) {
|
|
94
|
+
this.consecutiveFailures++;
|
|
95
|
+
this.lastFailureAt = now;
|
|
96
|
+
if (this.consecutiveFailures >= this.maxConsecutiveFailures) {
|
|
97
|
+
this._trip();
|
|
98
|
+
}
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
this.consecutiveFailures = 0;
|
|
102
|
+
if (slow) {
|
|
103
|
+
this.lastSlowAt = now;
|
|
104
|
+
const slowCount = this.window.filter((c) => c.slow).length;
|
|
105
|
+
if (slowCount >= this.maxSlowCalls) {
|
|
106
|
+
this._trip();
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const callCount = this.window.length;
|
|
110
|
+
if (callCount >= this.maxCallsPerWindow) {
|
|
111
|
+
this._trip();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/** Force the breaker open. Used by /kill force and Ctrl+C. */
|
|
115
|
+
forceOpen() {
|
|
116
|
+
this._trip();
|
|
117
|
+
}
|
|
118
|
+
/** Force a reset to closed. Used by tests and /kill reset. */
|
|
119
|
+
forceReset() {
|
|
120
|
+
this._reset();
|
|
121
|
+
}
|
|
122
|
+
_trip() {
|
|
123
|
+
if (this.state === "open") return;
|
|
124
|
+
this.state = "open";
|
|
125
|
+
this.openedAt = Date.now();
|
|
126
|
+
}
|
|
127
|
+
_reset() {
|
|
128
|
+
this.state = "closed";
|
|
129
|
+
this.consecutiveFailures = 0;
|
|
130
|
+
this.window = [];
|
|
131
|
+
this.openedAt = null;
|
|
132
|
+
}
|
|
133
|
+
/** Transition from open → half-open when cooldown elapses. */
|
|
134
|
+
_checkStateTransition() {
|
|
135
|
+
if (this.state !== "open" || this.openedAt === null) return;
|
|
136
|
+
const elapsed = Date.now() - this.openedAt;
|
|
137
|
+
if (elapsed >= this.cooldownMs) {
|
|
138
|
+
this.state = "half-open";
|
|
139
|
+
this.openedAt = null;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
_pruneWindow(now) {
|
|
143
|
+
const cutoff = now - this.windowMs;
|
|
144
|
+
this.window = this.window.filter((c) => c.at >= cutoff);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
export { CircuitBreaker };
|
|
149
|
+
//# sourceMappingURL=circuit-breaker.js.map
|
|
150
|
+
//# sourceMappingURL=circuit-breaker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/circuit-breaker.ts"],"names":[],"mappings":";AA6DA,IAAM,gCAAA,GAAmC,CAAA;AACzC,IAAM,8BAAA,GAAiC,GAAA;AACvC,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;AAAA,EAE1B,UAAA,GAA4B,IAAA;AAAA,EAEpC,WAAA,CAAY,MA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AY;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;\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;\n /**\n * Max slow calls before trip (within the sliding window). Default: 3.\n */\n maxSlowCalls?: number;\n /**\n * Sliding window for rate-limit and slow-call counting, in ms.\n * Default: 60_000 (1 minute).\n */\n windowMs?: number;\n /**\n * Max calls within the sliding window. Default: 30.\n * Burst exceeding this trips the breaker immediately.\n */\n maxCallsPerWindow?: number;\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;\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 = 60_000;\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 /** Timestamp when the last call ran (for half-open gate). */\n private lastCallAt: 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 beforeCall(): boolean {\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 afterCall(durationMs: number, failed: boolean): void {\n const now = Date.now();\n this.lastCallAt = 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}"]}
|
package/dist/exec.js
CHANGED
|
@@ -1,9 +1,327 @@
|
|
|
1
1
|
import { spawn } from 'child_process';
|
|
2
2
|
import * as path from 'path';
|
|
3
3
|
import { buildChildEnv } from '@wrongstack/core';
|
|
4
|
+
import * as os from 'os';
|
|
4
5
|
|
|
5
6
|
// src/exec.ts
|
|
6
7
|
|
|
8
|
+
// src/circuit-breaker.ts
|
|
9
|
+
var DEFAULT_MAX_CONSECUTIVE_FAILURES = 5;
|
|
10
|
+
var DEFAULT_SLOW_CALL_THRESHOLD_MS = 6e4;
|
|
11
|
+
var DEFAULT_MAX_SLOW_CALLS = 3;
|
|
12
|
+
var DEFAULT_WINDOW_MS = 6e4;
|
|
13
|
+
var DEFAULT_MAX_CALLS_PER_WINDOW = 30;
|
|
14
|
+
var DEFAULT_COOLDOWN_MS = 3e4;
|
|
15
|
+
var CircuitBreaker = class {
|
|
16
|
+
maxConsecutiveFailures;
|
|
17
|
+
slowCallThresholdMs;
|
|
18
|
+
maxSlowCalls;
|
|
19
|
+
windowMs;
|
|
20
|
+
maxCallsPerWindow;
|
|
21
|
+
cooldownMs;
|
|
22
|
+
state = "closed";
|
|
23
|
+
consecutiveFailures = 0;
|
|
24
|
+
window = [];
|
|
25
|
+
lastFailureAt = null;
|
|
26
|
+
lastSlowAt = null;
|
|
27
|
+
/** Timestamp when the breaker was opened (for cooldown calculation). */
|
|
28
|
+
openedAt = null;
|
|
29
|
+
/** Timestamp when the last call ran (for half-open gate). */
|
|
30
|
+
lastCallAt = null;
|
|
31
|
+
constructor(config = {}) {
|
|
32
|
+
this.maxConsecutiveFailures = config.maxConsecutiveFailures ?? DEFAULT_MAX_CONSECUTIVE_FAILURES;
|
|
33
|
+
this.slowCallThresholdMs = config.slowCallThresholdMs ?? DEFAULT_SLOW_CALL_THRESHOLD_MS;
|
|
34
|
+
this.maxSlowCalls = config.maxSlowCalls ?? DEFAULT_MAX_SLOW_CALLS;
|
|
35
|
+
this.windowMs = config.windowMs ?? DEFAULT_WINDOW_MS;
|
|
36
|
+
this.maxCallsPerWindow = config.maxCallsPerWindow ?? DEFAULT_MAX_CALLS_PER_WINDOW;
|
|
37
|
+
this.cooldownMs = config.cooldownMs ?? DEFAULT_COOLDOWN_MS;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Returns true if the circuit allows a new call to proceed.
|
|
41
|
+
* When false, callers should abort the tool call and return a
|
|
42
|
+
* circuit-breaker error instead of spawning a process.
|
|
43
|
+
*/
|
|
44
|
+
get canProceed() {
|
|
45
|
+
this._checkStateTransition();
|
|
46
|
+
return this.state !== "open";
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Snapshot of the current breaker state for observability (`/kill`).
|
|
50
|
+
*/
|
|
51
|
+
snapshot() {
|
|
52
|
+
this._checkStateTransition();
|
|
53
|
+
const now = Date.now();
|
|
54
|
+
let cooldownRemaining = null;
|
|
55
|
+
if (this.openedAt !== null && this.state === "open") {
|
|
56
|
+
const elapsed = now - this.openedAt;
|
|
57
|
+
cooldownRemaining = Math.max(0, this.cooldownMs - elapsed);
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
state: this.state,
|
|
61
|
+
consecutiveFailures: this.consecutiveFailures,
|
|
62
|
+
slowCallsInWindow: this.window.filter((c) => c.slow).length,
|
|
63
|
+
callsInWindow: this.window.length,
|
|
64
|
+
windowMs: this.windowMs,
|
|
65
|
+
cooldownRemainingMs: cooldownRemaining,
|
|
66
|
+
lastFailureAt: this.lastFailureAt,
|
|
67
|
+
lastSlowAt: this.lastSlowAt
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Call this BEFORE spawning a bash/exec process.
|
|
72
|
+
* Returns true if the call is allowed; false if the breaker is open.
|
|
73
|
+
* When false, callers MUST NOT spawn a process.
|
|
74
|
+
*/
|
|
75
|
+
beforeCall() {
|
|
76
|
+
this._checkStateTransition();
|
|
77
|
+
if (this.state === "open") return false;
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Call this AFTER a bash/exec process finishes (success or failure).
|
|
82
|
+
* `durationMs` is the wall-clock time the process ran.
|
|
83
|
+
* `failed` is true when the process returned a non-zero exit code or
|
|
84
|
+
* threw an exception before spawning.
|
|
85
|
+
*/
|
|
86
|
+
afterCall(durationMs, failed) {
|
|
87
|
+
const now = Date.now();
|
|
88
|
+
this.lastCallAt = now;
|
|
89
|
+
if (this.state === "half-open") {
|
|
90
|
+
if (failed) {
|
|
91
|
+
this._trip();
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
this._reset();
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
this._pruneWindow(now);
|
|
98
|
+
const slow = durationMs >= this.slowCallThresholdMs;
|
|
99
|
+
this.window.push({ at: now, failed, slow });
|
|
100
|
+
if (failed) {
|
|
101
|
+
this.consecutiveFailures++;
|
|
102
|
+
this.lastFailureAt = now;
|
|
103
|
+
if (this.consecutiveFailures >= this.maxConsecutiveFailures) {
|
|
104
|
+
this._trip();
|
|
105
|
+
}
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
this.consecutiveFailures = 0;
|
|
109
|
+
if (slow) {
|
|
110
|
+
this.lastSlowAt = now;
|
|
111
|
+
const slowCount = this.window.filter((c) => c.slow).length;
|
|
112
|
+
if (slowCount >= this.maxSlowCalls) {
|
|
113
|
+
this._trip();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const callCount = this.window.length;
|
|
117
|
+
if (callCount >= this.maxCallsPerWindow) {
|
|
118
|
+
this._trip();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/** Force the breaker open. Used by /kill force and Ctrl+C. */
|
|
122
|
+
forceOpen() {
|
|
123
|
+
this._trip();
|
|
124
|
+
}
|
|
125
|
+
/** Force a reset to closed. Used by tests and /kill reset. */
|
|
126
|
+
forceReset() {
|
|
127
|
+
this._reset();
|
|
128
|
+
}
|
|
129
|
+
_trip() {
|
|
130
|
+
if (this.state === "open") return;
|
|
131
|
+
this.state = "open";
|
|
132
|
+
this.openedAt = Date.now();
|
|
133
|
+
}
|
|
134
|
+
_reset() {
|
|
135
|
+
this.state = "closed";
|
|
136
|
+
this.consecutiveFailures = 0;
|
|
137
|
+
this.window = [];
|
|
138
|
+
this.openedAt = null;
|
|
139
|
+
}
|
|
140
|
+
/** Transition from open → half-open when cooldown elapses. */
|
|
141
|
+
_checkStateTransition() {
|
|
142
|
+
if (this.state !== "open" || this.openedAt === null) return;
|
|
143
|
+
const elapsed = Date.now() - this.openedAt;
|
|
144
|
+
if (elapsed >= this.cooldownMs) {
|
|
145
|
+
this.state = "half-open";
|
|
146
|
+
this.openedAt = null;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
_pruneWindow(now) {
|
|
150
|
+
const cutoff = now - this.windowMs;
|
|
151
|
+
this.window = this.window.filter((c) => c.at >= cutoff);
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
// src/process-registry.ts
|
|
156
|
+
var DEFAULT_GRACE_MS = 2e3;
|
|
157
|
+
var ProcessRegistryImpl = class {
|
|
158
|
+
processes = /* @__PURE__ */ new Map();
|
|
159
|
+
breaker;
|
|
160
|
+
constructor(breakerConfig) {
|
|
161
|
+
this.breaker = new CircuitBreaker(breakerConfig);
|
|
162
|
+
}
|
|
163
|
+
register(info) {
|
|
164
|
+
this.processes.set(info.pid, { ...info, killed: false });
|
|
165
|
+
}
|
|
166
|
+
/** Unregister a process by PID. Called on 'close' / 'exit' events. */
|
|
167
|
+
unregister(pid) {
|
|
168
|
+
this.processes.delete(pid);
|
|
169
|
+
}
|
|
170
|
+
/** Get a single process by PID. */
|
|
171
|
+
get(pid) {
|
|
172
|
+
return this.processes.get(pid);
|
|
173
|
+
}
|
|
174
|
+
/** Get all tracked processes. */
|
|
175
|
+
list() {
|
|
176
|
+
return Array.from(this.processes.values());
|
|
177
|
+
}
|
|
178
|
+
/** Get processes filtered by name (e.g. 'bash', 'exec'). */
|
|
179
|
+
byName(name) {
|
|
180
|
+
return this.list().filter((p) => p.name === name);
|
|
181
|
+
}
|
|
182
|
+
/** Get processes filtered by session. */
|
|
183
|
+
bySession(sessionId) {
|
|
184
|
+
return this.list().filter((p) => p.sessionId === sessionId);
|
|
185
|
+
}
|
|
186
|
+
/** Count of active (non-killed) processes. */
|
|
187
|
+
get activeCount() {
|
|
188
|
+
let n = 0;
|
|
189
|
+
for (const p of this.processes.values()) {
|
|
190
|
+
if (!p.killed) n++;
|
|
191
|
+
}
|
|
192
|
+
return n;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Combined stats for observability — used by /ps and the TUI status bar.
|
|
196
|
+
*/
|
|
197
|
+
stats() {
|
|
198
|
+
return {
|
|
199
|
+
activeCount: this.activeCount,
|
|
200
|
+
totalCount: this.processes.size,
|
|
201
|
+
breaker: this.breaker.snapshot()
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Returns true if the circuit allows a new bash/exec call to proceed.
|
|
206
|
+
* When false, callers MUST NOT spawn a process.
|
|
207
|
+
*/
|
|
208
|
+
get canProceed() {
|
|
209
|
+
return this.breaker.canProceed;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Called before spawning a process. Returns true if allowed; false if
|
|
213
|
+
* the circuit breaker is open.
|
|
214
|
+
*/
|
|
215
|
+
beforeCall() {
|
|
216
|
+
return this.breaker.beforeCall();
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Called after a process finishes. `durationMs` is wall-clock time;
|
|
220
|
+
* `failed` is true for non-zero exit codes.
|
|
221
|
+
*/
|
|
222
|
+
afterCall(durationMs, failed) {
|
|
223
|
+
this.breaker.afterCall(durationMs, failed);
|
|
224
|
+
}
|
|
225
|
+
/** Force-open the circuit breaker (Ctrl+C, /kill force). */
|
|
226
|
+
forceBreakerOpen() {
|
|
227
|
+
this.breaker.forceOpen();
|
|
228
|
+
}
|
|
229
|
+
/** Force-reset the circuit breaker to closed (/kill reset). */
|
|
230
|
+
forceBreakerReset() {
|
|
231
|
+
this.breaker.forceReset();
|
|
232
|
+
}
|
|
233
|
+
/** Kill a single process by PID.
|
|
234
|
+
*
|
|
235
|
+
* On POSIX: sends SIGTERM to the *process group* (-pid) so that
|
|
236
|
+
* runaway grandchild processes (`sleep 9999 & disown`) are also killed.
|
|
237
|
+
* After `graceMs` a SIGKILL is sent if the process hasn't exited.
|
|
238
|
+
*
|
|
239
|
+
* On Windows: `child.kill()` maps to TerminateProcess — process groups
|
|
240
|
+
* are not meaningfully supported. A second `force=true` call sends
|
|
241
|
+
* SIGKILL (which maps to TerminateProcess again — the distinction is
|
|
242
|
+
* in the exit code, not the signal).
|
|
243
|
+
*
|
|
244
|
+
* Returns true if the process was found and kill was attempted.
|
|
245
|
+
*/
|
|
246
|
+
kill(pid, opts = {}) {
|
|
247
|
+
const p = this.processes.get(pid);
|
|
248
|
+
if (!p) return false;
|
|
249
|
+
if (p.killed) return true;
|
|
250
|
+
const { force = false, graceMs = DEFAULT_GRACE_MS } = opts;
|
|
251
|
+
const isWin = os.platform() === "win32";
|
|
252
|
+
if (isWin) {
|
|
253
|
+
try {
|
|
254
|
+
p.child.kill(force ? "SIGKILL" : "SIGTERM");
|
|
255
|
+
} catch {
|
|
256
|
+
}
|
|
257
|
+
p.killed = true;
|
|
258
|
+
return true;
|
|
259
|
+
}
|
|
260
|
+
try {
|
|
261
|
+
if (force) {
|
|
262
|
+
try {
|
|
263
|
+
process.kill(-pid, "SIGKILL");
|
|
264
|
+
} catch {
|
|
265
|
+
p.child.kill("SIGKILL");
|
|
266
|
+
}
|
|
267
|
+
} else {
|
|
268
|
+
try {
|
|
269
|
+
process.kill(-pid, "SIGTERM");
|
|
270
|
+
} catch {
|
|
271
|
+
p.child.kill("SIGTERM");
|
|
272
|
+
}
|
|
273
|
+
const timer = setTimeout(() => {
|
|
274
|
+
if (this.processes.has(pid) && !p.child.killed) {
|
|
275
|
+
try {
|
|
276
|
+
process.kill(-pid, "SIGKILL");
|
|
277
|
+
} catch {
|
|
278
|
+
try {
|
|
279
|
+
p.child.kill("SIGKILL");
|
|
280
|
+
} catch {
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}, graceMs);
|
|
285
|
+
timer.unref?.();
|
|
286
|
+
}
|
|
287
|
+
} catch {
|
|
288
|
+
}
|
|
289
|
+
p.killed = true;
|
|
290
|
+
return true;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Kill all tracked processes.
|
|
294
|
+
* Returns the PIDs that were kill()ed.
|
|
295
|
+
*/
|
|
296
|
+
killAll(opts = {}) {
|
|
297
|
+
const pids = Array.from(this.processes.keys());
|
|
298
|
+
const killed = [];
|
|
299
|
+
for (const pid of pids) {
|
|
300
|
+
if (this.kill(pid, opts)) killed.push(pid);
|
|
301
|
+
}
|
|
302
|
+
return killed;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Kill all processes for a specific session.
|
|
306
|
+
* Returns the PIDs that were kill()ed.
|
|
307
|
+
*/
|
|
308
|
+
killSession(sessionId, opts = {}) {
|
|
309
|
+
const pids = this.bySession(sessionId).map((p) => p.pid);
|
|
310
|
+
const killed = [];
|
|
311
|
+
for (const pid of pids) {
|
|
312
|
+
if (this.kill(pid, opts)) killed.push(pid);
|
|
313
|
+
}
|
|
314
|
+
return killed;
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
var _registry;
|
|
318
|
+
function getProcessRegistry() {
|
|
319
|
+
if (!_registry) {
|
|
320
|
+
_registry = new ProcessRegistryImpl();
|
|
321
|
+
}
|
|
322
|
+
return _registry;
|
|
323
|
+
}
|
|
324
|
+
|
|
7
325
|
// src/exec.ts
|
|
8
326
|
var ALLOWED_COMMANDS = {
|
|
9
327
|
node: ["--version", "-r", "--input-type=module"],
|
|
@@ -102,6 +420,18 @@ var execTool = {
|
|
|
102
420
|
required: ["command"]
|
|
103
421
|
},
|
|
104
422
|
async execute(input, ctx, opts) {
|
|
423
|
+
const registry = getProcessRegistry();
|
|
424
|
+
if (!registry.canProceed) {
|
|
425
|
+
return {
|
|
426
|
+
command: input.command,
|
|
427
|
+
args: input.args ?? [],
|
|
428
|
+
stdout: "",
|
|
429
|
+
stderr: "Circuit breaker is open \u2014 too many consecutive failures. Use /kill reset to recover.",
|
|
430
|
+
exitCode: 1,
|
|
431
|
+
truncated: false,
|
|
432
|
+
allowed: false
|
|
433
|
+
};
|
|
434
|
+
}
|
|
105
435
|
const cmd = input.command.trim();
|
|
106
436
|
if (!cmd)
|
|
107
437
|
return {
|
|
@@ -161,15 +491,23 @@ function runCommand(cmd, args, cwd, timeout, signal, sessionId) {
|
|
|
161
491
|
let stdout = "";
|
|
162
492
|
let stderr = "";
|
|
163
493
|
let killed = false;
|
|
494
|
+
const startedAt = Date.now();
|
|
164
495
|
const child = spawn(cmd, args, {
|
|
165
496
|
cwd,
|
|
166
497
|
signal,
|
|
167
498
|
env: buildChildEnv(sessionId),
|
|
168
499
|
stdio: ["ignore", "pipe", "pipe"]
|
|
169
500
|
});
|
|
501
|
+
const registry = getProcessRegistry();
|
|
502
|
+
const pid = child.pid;
|
|
503
|
+
if (typeof pid === "number") {
|
|
504
|
+
const fullCommand = `${cmd} ${args.join(" ")}`;
|
|
505
|
+
registry.register({ pid, name: "exec", command: fullCommand, startedAt: Date.now(), sessionId, child });
|
|
506
|
+
}
|
|
170
507
|
const timer = setTimeout(() => {
|
|
171
508
|
killed = true;
|
|
172
|
-
|
|
509
|
+
if (typeof pid === "number") registry.kill(pid);
|
|
510
|
+
else child.kill("SIGTERM");
|
|
173
511
|
}, timeout);
|
|
174
512
|
child.stdout?.on("data", (chunk) => {
|
|
175
513
|
if (stdout.length < MAX_OUTPUT) stdout += chunk.toString();
|
|
@@ -179,18 +517,24 @@ function runCommand(cmd, args, cwd, timeout, signal, sessionId) {
|
|
|
179
517
|
});
|
|
180
518
|
child.on("close", (code) => {
|
|
181
519
|
clearTimeout(timer);
|
|
520
|
+
if (typeof pid === "number") registry.unregister(pid);
|
|
521
|
+
const durationMs = Date.now() - startedAt;
|
|
522
|
+
const exitCode = killed ? 124 : code ?? 1;
|
|
523
|
+
registry.afterCall(durationMs, exitCode !== 0);
|
|
182
524
|
resolve2({
|
|
183
525
|
command: cmd,
|
|
184
526
|
args,
|
|
185
527
|
stdout: stdout.slice(0, MAX_OUTPUT),
|
|
186
528
|
stderr: stderr.slice(0, MAX_OUTPUT),
|
|
187
|
-
exitCode
|
|
529
|
+
exitCode,
|
|
188
530
|
truncated: stdout.length >= MAX_OUTPUT || stderr.length >= MAX_OUTPUT,
|
|
189
531
|
allowed: true
|
|
190
532
|
});
|
|
191
533
|
});
|
|
192
534
|
child.on("error", (err) => {
|
|
193
535
|
clearTimeout(timer);
|
|
536
|
+
if (typeof pid === "number") registry.unregister(pid);
|
|
537
|
+
registry.afterCall(Date.now() - startedAt, true);
|
|
194
538
|
resolve2({
|
|
195
539
|
command: cmd,
|
|
196
540
|
args,
|