@wrongstack/tools 0.5.6 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bash.d.ts +2 -1
- package/dist/bash.js +361 -3
- package/dist/bash.js.map +1 -1
- package/dist/builtin.js +408 -12
- 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 +355 -4
- package/dist/exec.js.map +1 -1
- package/dist/fetch.js.map +1 -1
- package/dist/grep.js +9 -4
- package/dist/grep.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +412 -13
- package/dist/index.js.map +1 -1
- package/dist/logs.js +9 -4
- package/dist/logs.js.map +1 -1
- package/dist/pack.js +408 -12
- 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/dist/replace.js +9 -4
- package/dist/replace.js.map +1 -1
- package/dist/scaffold.js +2 -1
- package/dist/scaffold.js.map +1 -1
- 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;AA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I,IAAA,CAAK,KAAA,KAAU,MAAA,IAAU,IAAA,CAAK,aAAa,IAAA,EAAM;AACrD,IAAA,MAAM,OAAA,GAAU,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;\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}"]}
|