@poolzin/pool-bot 2026.3.7 → 2026.3.10
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/CHANGELOG.md +40 -0
- package/README.md +147 -69
- package/dist/.buildstamp +1 -1
- package/dist/agents/error-classifier.js +251 -0
- package/dist/agents/skills/security.js +211 -0
- package/dist/build-info.json +3 -3
- package/dist/cli/cron-cli/register.cron-dashboard.js +339 -0
- package/dist/cli/cron-cli/register.js +2 -0
- package/dist/cli/errors.js +187 -0
- package/dist/cli/lazy-commands.example.js +113 -0
- package/dist/cli/lazy-commands.js +329 -0
- package/dist/cli/program/command-registry.js +26 -0
- package/dist/cli/program/register.maintenance.js +21 -0
- package/dist/cli/program/register.skills.js +4 -0
- package/dist/cli/program/register.subclis.js +9 -0
- package/dist/cli/swarm-cli/register.js +8 -0
- package/dist/cli/swarm-cli/register.swarm-status.js +488 -0
- package/dist/cli/telemetry-cli/register.js +10 -0
- package/dist/cli/telemetry-cli/register.telemetry-alerts.js +176 -0
- package/dist/cli/telemetry-cli/register.telemetry-metrics.js +323 -0
- package/dist/cli/telemetry-cli/register.telemetry-status.js +179 -0
- package/dist/commands/doctor-checks.js +498 -0
- package/dist/config/config.js +1 -0
- package/dist/config/secrets-integration.js +88 -0
- package/dist/context-engine/index.js +33 -0
- package/dist/context-engine/legacy.js +179 -0
- package/dist/context-engine/registry.js +86 -0
- package/dist/context-engine/summarizing.js +290 -0
- package/dist/context-engine/types.js +7 -0
- package/dist/cron/service/timer.js +18 -0
- package/dist/gateway/protocol/index.js +5 -2
- package/dist/gateway/protocol/schema/error-codes.js +1 -0
- package/dist/gateway/protocol/schema/swarm.js +80 -0
- package/dist/gateway/protocol/schema.js +1 -0
- package/dist/gateway/server-close.js +4 -0
- package/dist/gateway/server-constants.js +1 -0
- package/dist/gateway/server-cron.js +29 -0
- package/dist/gateway/server-maintenance.js +35 -2
- package/dist/gateway/server-methods/swarm.js +58 -0
- package/dist/gateway/server-methods/telemetry.js +71 -0
- package/dist/gateway/server-methods-list.js +8 -0
- package/dist/gateway/server-methods.js +9 -2
- package/dist/gateway/server.impl.js +33 -16
- package/dist/infra/abort-pattern.js +106 -0
- package/dist/infra/retry.js +96 -0
- package/dist/secrets/index.js +28 -0
- package/dist/secrets/resolver.js +185 -0
- package/dist/secrets/runtime.js +142 -0
- package/dist/secrets/types.js +11 -0
- package/dist/security/dangerous-tools.js +80 -0
- package/dist/security/types.js +12 -0
- package/dist/skills/commands.js +333 -0
- package/dist/skills/index.js +164 -0
- package/dist/skills/loader.js +282 -0
- package/dist/skills/parser.js +446 -0
- package/dist/skills/registry.js +394 -0
- package/dist/skills/security.js +312 -0
- package/dist/skills/types.js +21 -0
- package/dist/swarm/service.js +247 -0
- package/dist/telemetry/alert-engine.js +258 -0
- package/dist/telemetry/cron-instrumentation.js +49 -0
- package/dist/telemetry/gateway-instrumentation.js +80 -0
- package/dist/telemetry/instrumentation.js +66 -0
- package/dist/telemetry/service.js +345 -0
- package/dist/test-utils/index.js +219 -0
- package/dist/tui/components/assistant-message.js +6 -2
- package/dist/tui/components/hyperlink-markdown.js +32 -0
- package/dist/tui/components/searchable-select-list.js +12 -1
- package/dist/tui/components/user-message.js +6 -2
- package/dist/tui/index.js +611 -0
- package/dist/tui/theme/theme-detection.js +226 -0
- package/dist/tui/tui-command-handlers.js +20 -0
- package/dist/tui/tui-formatters.js +4 -3
- package/dist/tui/utils/ctrl-c-handler.js +67 -0
- package/dist/tui/utils/osc8-hyperlinks.js +208 -0
- package/dist/tui/utils/safe-stop.js +180 -0
- package/dist/tui/utils/session-key-utils.js +81 -0
- package/dist/tui/utils/text-sanitization.js +284 -0
- package/dist/utils/lru-cache.js +116 -0
- package/dist/utils/performance.js +199 -0
- package/dist/utils/retry.js +240 -0
- package/docs/INTEGRATION_PLAN.md +475 -0
- package/docs/INTEGRATION_SUMMARY.md +215 -0
- package/docs/MELHORIAS_IMPLEMENTADAS.md +228 -0
- package/docs/MELHORIAS_PROFISSIONAIS.md +282 -0
- package/docs/PLANO_ACAO_TUI.md +357 -0
- package/docs/PROGRESSO_TUI.md +66 -0
- package/docs/RELATORIO_FINAL.md +217 -0
- package/docs/diagnostico-shell-completion.md +265 -0
- package/docs/features/advanced-memory.md +585 -0
- package/docs/features/discord-components-v2.md +277 -0
- package/docs/features/swarm.md +100 -0
- package/docs/features/telemetry.md +284 -0
- package/docs/integrations/HEXSTRIKE_PLAN.md +796 -0
- package/docs/integrations/INTEGRATION_PLAN.md +744 -0
- package/docs/integrations/PAGE_AGENT_PLAN.md +370 -0
- package/docs/integrations/XYOPS_PLAN.md +978 -0
- package/docs/models/provider-infrastructure.md +400 -0
- package/docs/security/exec-approvals.md +294 -0
- package/docs/skills/IMPLEMENTATION_SUMMARY.md +145 -0
- package/docs/skills/SKILL.md +524 -0
- package/docs/skills.md +405 -0
- package/extensions/bluebubbles/package.json +1 -1
- package/extensions/copilot-proxy/package.json +1 -1
- package/extensions/diagnostics-otel/package.json +1 -1
- package/extensions/discord/package.json +1 -1
- package/extensions/feishu/package.json +1 -1
- package/extensions/google-antigravity-auth/package.json +1 -1
- package/extensions/google-gemini-cli-auth/package.json +1 -1
- package/extensions/googlechat/package.json +1 -1
- package/extensions/hexstrike-bridge/README.md +119 -0
- package/extensions/hexstrike-bridge/index.test.ts +247 -0
- package/extensions/hexstrike-bridge/index.ts +487 -0
- package/extensions/hexstrike-bridge/package.json +17 -0
- package/extensions/imessage/package.json +1 -1
- package/extensions/irc/package.json +1 -1
- package/extensions/line/package.json +1 -1
- package/extensions/llm-task/package.json +1 -1
- package/extensions/lobster/package.json +1 -1
- package/extensions/matrix/CHANGELOG.md +5 -0
- package/extensions/matrix/package.json +1 -1
- package/extensions/mattermost/package.json +1 -1
- package/extensions/mcp-server/index.ts +14 -0
- package/extensions/mcp-server/package.json +11 -0
- package/extensions/mcp-server/src/service.ts +540 -0
- package/extensions/memory-core/package.json +1 -1
- package/extensions/memory-lancedb/package.json +1 -1
- package/extensions/minimax-portal-auth/package.json +1 -1
- package/extensions/msteams/CHANGELOG.md +5 -0
- package/extensions/msteams/package.json +1 -1
- package/extensions/nextcloud-talk/package.json +1 -1
- package/extensions/nostr/CHANGELOG.md +5 -0
- package/extensions/nostr/package.json +1 -1
- package/extensions/open-prose/package.json +1 -1
- package/extensions/openai-codex-auth/package.json +1 -1
- package/extensions/signal/package.json +1 -1
- package/extensions/slack/package.json +1 -1
- package/extensions/telegram/package.json +1 -1
- package/extensions/tlon/package.json +1 -1
- package/extensions/twitch/CHANGELOG.md +5 -0
- package/extensions/twitch/package.json +1 -1
- package/extensions/voice-call/CHANGELOG.md +5 -0
- package/extensions/voice-call/package.json +1 -1
- package/extensions/whatsapp/package.json +1 -1
- package/extensions/zalo/CHANGELOG.md +5 -0
- package/extensions/zalo/package.json +1 -1
- package/extensions/zalouser/CHANGELOG.md +5 -0
- package/extensions/zalouser/package.json +1 -1
- package/package.json +8 -1
- package/skills/example-skill/SKILL.md +195 -0
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Performance monitoring utilities for PoolBot
|
|
3
|
+
*
|
|
4
|
+
* Features:
|
|
5
|
+
* - Function execution timing with automatic logging
|
|
6
|
+
* - Memory usage tracking
|
|
7
|
+
* - Async operation profiling
|
|
8
|
+
* - Performance markers for critical paths
|
|
9
|
+
* - Slow operation detection and warnings
|
|
10
|
+
*/
|
|
11
|
+
import { createSubsystemLogger } from "../logging/subsystem.js";
|
|
12
|
+
const perfLog = createSubsystemLogger("perf");
|
|
13
|
+
// Thresholds for slow operation warnings (in ms)
|
|
14
|
+
const SLOW_THRESHOLDS = {
|
|
15
|
+
default: 100,
|
|
16
|
+
config: 50,
|
|
17
|
+
gateway: 200,
|
|
18
|
+
memory: 500,
|
|
19
|
+
search: 1000,
|
|
20
|
+
doctor: 300,
|
|
21
|
+
completion: 100,
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Active performance markers
|
|
25
|
+
*/
|
|
26
|
+
const activeMarkers = new Map();
|
|
27
|
+
/**
|
|
28
|
+
* Start a performance marker
|
|
29
|
+
*/
|
|
30
|
+
export function startMarker(name, metadata) {
|
|
31
|
+
const id = `${name}_${Date.now()}_${Math.random().toString(36).slice(2, 9)}`;
|
|
32
|
+
activeMarkers.set(id, {
|
|
33
|
+
name,
|
|
34
|
+
startTime: performance.now(),
|
|
35
|
+
metadata,
|
|
36
|
+
});
|
|
37
|
+
return id;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* End a performance marker and log results
|
|
41
|
+
*/
|
|
42
|
+
export function endMarker(id, options = {}) {
|
|
43
|
+
const marker = activeMarkers.get(id);
|
|
44
|
+
if (!marker) {
|
|
45
|
+
perfLog.warn(`Performance marker not found: ${id}`);
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
activeMarkers.delete(id);
|
|
49
|
+
const endTime = performance.now();
|
|
50
|
+
const durationMs = Math.round(endTime - marker.startTime);
|
|
51
|
+
const threshold = options.threshold ?? SLOW_THRESHOLDS[marker.name] ?? SLOW_THRESHOLDS.default;
|
|
52
|
+
const slow = durationMs > threshold;
|
|
53
|
+
const metrics = {
|
|
54
|
+
operation: marker.name,
|
|
55
|
+
durationMs,
|
|
56
|
+
slow,
|
|
57
|
+
metadata: marker.metadata,
|
|
58
|
+
};
|
|
59
|
+
if (options.log ?? true) {
|
|
60
|
+
if (slow) {
|
|
61
|
+
perfLog.warn(`Slow operation: ${marker.name} took ${durationMs}ms (threshold: ${threshold}ms)`, marker.metadata);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
perfLog.debug(`${marker.name}: ${durationMs}ms`, marker.metadata);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return metrics;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Time a synchronous function execution
|
|
71
|
+
*/
|
|
72
|
+
export function timeSync(name, fn, options = {}) {
|
|
73
|
+
const id = startMarker(name, options.metadata);
|
|
74
|
+
try {
|
|
75
|
+
return fn();
|
|
76
|
+
}
|
|
77
|
+
finally {
|
|
78
|
+
endMarker(id, { log: options.log, threshold: options.threshold });
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Time an asynchronous function execution
|
|
83
|
+
*/
|
|
84
|
+
export async function timeAsync(name, fn, options = {}) {
|
|
85
|
+
const id = startMarker(name, options.metadata);
|
|
86
|
+
try {
|
|
87
|
+
return await fn();
|
|
88
|
+
}
|
|
89
|
+
finally {
|
|
90
|
+
endMarker(id, { log: options.log, threshold: options.threshold });
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Create a performance-monitored version of a function
|
|
95
|
+
*/
|
|
96
|
+
export function withPerformanceTracking(name, fn, options = {}) {
|
|
97
|
+
return (...args) => {
|
|
98
|
+
const metadata = options.logArgs ? { args: args.map((a) => String(a)) } : undefined;
|
|
99
|
+
return timeSync(name, () => fn(...args), { ...options, metadata });
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Create a performance-monitored version of an async function
|
|
104
|
+
*/
|
|
105
|
+
export function withAsyncPerformanceTracking(name, fn, options = {}) {
|
|
106
|
+
return async (...args) => {
|
|
107
|
+
const metadata = options.logArgs ? { args: args.map((a) => String(a)) } : undefined;
|
|
108
|
+
return timeAsync(name, () => fn(...args), { ...options, metadata });
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Get current memory usage
|
|
113
|
+
*/
|
|
114
|
+
export function getMemorySnapshot() {
|
|
115
|
+
const usage = process.memoryUsage();
|
|
116
|
+
return {
|
|
117
|
+
timestamp: Date.now(),
|
|
118
|
+
used: Math.round(usage.heapUsed / 1024 / 1024), // MB
|
|
119
|
+
total: Math.round(usage.heapTotal / 1024 / 1024), // MB
|
|
120
|
+
external: Math.round(usage.external / 1024 / 1024), // MB
|
|
121
|
+
arrayBuffers: Math.round(usage.arrayBuffers / 1024 / 1024), // MB
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Track memory before and after an operation
|
|
126
|
+
*/
|
|
127
|
+
export async function withMemoryTracking(name, fn, options = {}) {
|
|
128
|
+
const before = getMemorySnapshot();
|
|
129
|
+
const startId = startMarker(`${name}_memory`);
|
|
130
|
+
try {
|
|
131
|
+
return await fn();
|
|
132
|
+
}
|
|
133
|
+
finally {
|
|
134
|
+
const after = getMemorySnapshot();
|
|
135
|
+
endMarker(startId, { log: false });
|
|
136
|
+
const delta = after.used - before.used;
|
|
137
|
+
const warnThreshold = options.warnThresholdMB ?? 50;
|
|
138
|
+
if (options.log ?? true) {
|
|
139
|
+
if (delta > warnThreshold) {
|
|
140
|
+
perfLog.warn(`High memory usage in ${name}: +${delta}MB (${before.used}MB -> ${after.used}MB)`);
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
perfLog.debug(`${name} memory: ${before.used}MB -> ${after.used}MB (${delta >= 0 ? "+" : ""}${delta}MB)`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Get all active markers (for debugging)
|
|
150
|
+
*/
|
|
151
|
+
export function getActiveMarkers() {
|
|
152
|
+
return Array.from(activeMarkers.values());
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Clear all active markers
|
|
156
|
+
*/
|
|
157
|
+
export function clearAllMarkers() {
|
|
158
|
+
activeMarkers.clear();
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Create a performance collector for batch operations
|
|
162
|
+
*/
|
|
163
|
+
export function createPerformanceCollector() {
|
|
164
|
+
const operations = [];
|
|
165
|
+
return {
|
|
166
|
+
add: (metrics) => {
|
|
167
|
+
operations.push(metrics);
|
|
168
|
+
},
|
|
169
|
+
getReport: () => {
|
|
170
|
+
if (operations.length === 0) {
|
|
171
|
+
return {
|
|
172
|
+
operations: [],
|
|
173
|
+
summary: {
|
|
174
|
+
totalOperations: 0,
|
|
175
|
+
slowOperations: 0,
|
|
176
|
+
averageDurationMs: 0,
|
|
177
|
+
maxDurationMs: 0,
|
|
178
|
+
minDurationMs: 0,
|
|
179
|
+
},
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
const durations = operations.map((o) => o.durationMs);
|
|
183
|
+
const slowOperations = operations.filter((o) => o.slow).length;
|
|
184
|
+
return {
|
|
185
|
+
operations,
|
|
186
|
+
summary: {
|
|
187
|
+
totalOperations: operations.length,
|
|
188
|
+
slowOperations,
|
|
189
|
+
averageDurationMs: Math.round(durations.reduce((a, b) => a + b, 0) / durations.length),
|
|
190
|
+
maxDurationMs: Math.max(...durations),
|
|
191
|
+
minDurationMs: Math.min(...durations),
|
|
192
|
+
},
|
|
193
|
+
};
|
|
194
|
+
},
|
|
195
|
+
clear: () => {
|
|
196
|
+
operations.length = 0;
|
|
197
|
+
},
|
|
198
|
+
};
|
|
199
|
+
}
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retry utilities with exponential backoff and jitter
|
|
3
|
+
*
|
|
4
|
+
* Features:
|
|
5
|
+
* - Exponential backoff with configurable parameters
|
|
6
|
+
* - Jitter to prevent thundering herd
|
|
7
|
+
* - Abort signal support for cancellation
|
|
8
|
+
* - Custom retry predicates (decide which errors to retry)
|
|
9
|
+
* - OnRetry callback for logging/observability
|
|
10
|
+
* - Max retry duration support
|
|
11
|
+
*/
|
|
12
|
+
import { createSubsystemLogger } from "../logging/subsystem.js";
|
|
13
|
+
const retryLog = createSubsystemLogger("retry");
|
|
14
|
+
/**
|
|
15
|
+
* Calculate delay with exponential backoff and optional jitter
|
|
16
|
+
*/
|
|
17
|
+
function calculateDelay(attempt, initialDelayMs, maxDelayMs, backoffMultiplier, jitter) {
|
|
18
|
+
// Exponential backoff: initialDelay * (multiplier ^ attempt)
|
|
19
|
+
const exponentialDelay = initialDelayMs * Math.pow(backoffMultiplier, attempt);
|
|
20
|
+
// Apply jitter: random value between 0.5x and 1.5x of calculated delay
|
|
21
|
+
if (jitter) {
|
|
22
|
+
const jitterFactor = 0.5 + Math.random();
|
|
23
|
+
return Math.min(Math.round(exponentialDelay * jitterFactor), maxDelayMs);
|
|
24
|
+
}
|
|
25
|
+
return Math.min(exponentialDelay, maxDelayMs);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Default retry predicate - retries on network/timeout errors
|
|
29
|
+
*/
|
|
30
|
+
function defaultRetryIf(error) {
|
|
31
|
+
if (error instanceof Error) {
|
|
32
|
+
const errorMessage = error.message.toLowerCase();
|
|
33
|
+
// Network errors
|
|
34
|
+
if (errorMessage.includes("timeout") ||
|
|
35
|
+
errorMessage.includes("etimedout") ||
|
|
36
|
+
errorMessage.includes("enotfound") ||
|
|
37
|
+
errorMessage.includes("econnrefused") ||
|
|
38
|
+
errorMessage.includes("econnreset") ||
|
|
39
|
+
errorMessage.includes("network") ||
|
|
40
|
+
errorMessage.includes("socket") ||
|
|
41
|
+
errorMessage.includes("fetch failed") ||
|
|
42
|
+
errorMessage.includes("abort") ||
|
|
43
|
+
errorMessage.includes("temporary")) {
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
// HTTP status codes that indicate retryable errors
|
|
47
|
+
if (errorMessage.includes("429") || // Too Many Requests
|
|
48
|
+
errorMessage.includes("503") || // Service Unavailable
|
|
49
|
+
errorMessage.includes("502") || // Bad Gateway
|
|
50
|
+
errorMessage.includes("504") // Gateway Timeout
|
|
51
|
+
) {
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Sleep with abort signal support
|
|
59
|
+
*/
|
|
60
|
+
function sleep(ms, signal) {
|
|
61
|
+
return new Promise((resolve, reject) => {
|
|
62
|
+
if (signal?.aborted) {
|
|
63
|
+
reject(new Error("Operation aborted"));
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const timeout = setTimeout(resolve, ms);
|
|
67
|
+
if (signal) {
|
|
68
|
+
signal.addEventListener("abort", () => {
|
|
69
|
+
clearTimeout(timeout);
|
|
70
|
+
reject(new Error("Operation aborted"));
|
|
71
|
+
}, { once: true });
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Retry an async operation with exponential backoff
|
|
77
|
+
*/
|
|
78
|
+
export async function retryAsync(operation, options = {}) {
|
|
79
|
+
const { maxAttempts = 3, initialDelayMs = 100, maxDelayMs = 30000, backoffMultiplier = 2, jitter = true, maxDurationMs, retryIf = defaultRetryIf, onRetry, signal, operationName = "operation", } = options;
|
|
80
|
+
const startTime = Date.now();
|
|
81
|
+
let lastError;
|
|
82
|
+
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
83
|
+
// Check max duration
|
|
84
|
+
if (maxDurationMs !== undefined) {
|
|
85
|
+
const elapsed = Date.now() - startTime;
|
|
86
|
+
if (elapsed >= maxDurationMs) {
|
|
87
|
+
throw new Error(`${operationName} failed: max duration (${maxDurationMs}ms) exceeded after ${attempt} attempts`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
const result = await operation();
|
|
92
|
+
if (attempt > 0) {
|
|
93
|
+
retryLog.info(`${operationName} succeeded after ${attempt + 1} attempts`);
|
|
94
|
+
}
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
lastError = error;
|
|
99
|
+
// Check if we should retry this error
|
|
100
|
+
if (!retryIf(error, attempt)) {
|
|
101
|
+
retryLog.debug(`${operationName} not retryable: ${error}`);
|
|
102
|
+
throw error;
|
|
103
|
+
}
|
|
104
|
+
// Check if this was the last attempt
|
|
105
|
+
if (attempt === maxAttempts - 1) {
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
// Calculate delay for next attempt
|
|
109
|
+
const delayMs = calculateDelay(attempt, initialDelayMs, maxDelayMs, backoffMultiplier, jitter);
|
|
110
|
+
retryLog.debug(`${operationName} attempt ${attempt + 1} failed, retrying in ${delayMs}ms: ${error}`);
|
|
111
|
+
// Call onRetry callback
|
|
112
|
+
onRetry?.(error, attempt + 1, delayMs);
|
|
113
|
+
// Wait before next attempt
|
|
114
|
+
await sleep(delayMs, signal);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
throw lastError;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Retry an async operation and return detailed outcome
|
|
121
|
+
*/
|
|
122
|
+
export async function retryAsyncWithOutcome(operation, options = {}) {
|
|
123
|
+
const startTime = Date.now();
|
|
124
|
+
try {
|
|
125
|
+
const data = await retryAsync(operation, options);
|
|
126
|
+
return {
|
|
127
|
+
success: true,
|
|
128
|
+
data,
|
|
129
|
+
attempts: 1, // This would need to be tracked properly
|
|
130
|
+
totalDurationMs: Date.now() - startTime,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
return {
|
|
135
|
+
success: false,
|
|
136
|
+
error,
|
|
137
|
+
attempts: options.maxAttempts ?? 3,
|
|
138
|
+
totalDurationMs: Date.now() - startTime,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Create a retryable version of a function
|
|
144
|
+
*/
|
|
145
|
+
export function withRetry(fn, options = {}) {
|
|
146
|
+
return async (...args) => {
|
|
147
|
+
return retryAsync(() => fn(...args), {
|
|
148
|
+
...options,
|
|
149
|
+
operationName: options.operationName ?? fn.name,
|
|
150
|
+
});
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Retry configuration presets for common scenarios
|
|
155
|
+
*/
|
|
156
|
+
export const RetryPresets = {
|
|
157
|
+
/** Fast retry for quick operations (e.g., config reads) */
|
|
158
|
+
fast: {
|
|
159
|
+
maxAttempts: 3,
|
|
160
|
+
initialDelayMs: 50,
|
|
161
|
+
maxDelayMs: 500,
|
|
162
|
+
backoffMultiplier: 2,
|
|
163
|
+
},
|
|
164
|
+
/** Standard retry for most operations */
|
|
165
|
+
standard: {
|
|
166
|
+
maxAttempts: 3,
|
|
167
|
+
initialDelayMs: 100,
|
|
168
|
+
maxDelayMs: 5000,
|
|
169
|
+
backoffMultiplier: 2,
|
|
170
|
+
},
|
|
171
|
+
/** Aggressive retry for critical operations */
|
|
172
|
+
aggressive: {
|
|
173
|
+
maxAttempts: 5,
|
|
174
|
+
initialDelayMs: 100,
|
|
175
|
+
maxDelayMs: 30000,
|
|
176
|
+
backoffMultiplier: 2,
|
|
177
|
+
},
|
|
178
|
+
/** Patient retry for flaky external services */
|
|
179
|
+
patient: {
|
|
180
|
+
maxAttempts: 10,
|
|
181
|
+
initialDelayMs: 1000,
|
|
182
|
+
maxDelayMs: 60000,
|
|
183
|
+
backoffMultiplier: 2,
|
|
184
|
+
},
|
|
185
|
+
/** No retry - fail fast */
|
|
186
|
+
none: {
|
|
187
|
+
maxAttempts: 1,
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
export class CircuitBreaker {
|
|
191
|
+
options;
|
|
192
|
+
state = "closed";
|
|
193
|
+
failures = 0;
|
|
194
|
+
nextAttempt = 0;
|
|
195
|
+
halfOpenCalls = 0;
|
|
196
|
+
constructor(options) {
|
|
197
|
+
this.options = options;
|
|
198
|
+
}
|
|
199
|
+
async execute(operation) {
|
|
200
|
+
if (this.state === "open") {
|
|
201
|
+
if (Date.now() < this.nextAttempt) {
|
|
202
|
+
throw new Error("Circuit breaker is open");
|
|
203
|
+
}
|
|
204
|
+
this.state = "half-open";
|
|
205
|
+
this.halfOpenCalls = 0;
|
|
206
|
+
}
|
|
207
|
+
if (this.state === "half-open") {
|
|
208
|
+
if (this.halfOpenCalls >= this.options.halfOpenMaxCalls) {
|
|
209
|
+
throw new Error("Circuit breaker is half-open, too many calls");
|
|
210
|
+
}
|
|
211
|
+
this.halfOpenCalls++;
|
|
212
|
+
}
|
|
213
|
+
try {
|
|
214
|
+
const result = await operation();
|
|
215
|
+
this.onSuccess();
|
|
216
|
+
return result;
|
|
217
|
+
}
|
|
218
|
+
catch (error) {
|
|
219
|
+
this.onFailure();
|
|
220
|
+
throw error;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
onSuccess() {
|
|
224
|
+
if (this.state === "half-open") {
|
|
225
|
+
this.state = "closed";
|
|
226
|
+
this.failures = 0;
|
|
227
|
+
this.halfOpenCalls = 0;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
onFailure() {
|
|
231
|
+
this.failures++;
|
|
232
|
+
if (this.failures >= this.options.failureThreshold) {
|
|
233
|
+
this.state = "open";
|
|
234
|
+
this.nextAttempt = Date.now() + this.options.resetTimeoutMs;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
getState() {
|
|
238
|
+
return this.state;
|
|
239
|
+
}
|
|
240
|
+
}
|