atabey-shared 0.1.1-beta → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +79 -41
- package/dist/agent-identity.d.ts +1 -34
- package/dist/agent-identity.js +1 -76
- package/dist/agent-identity.js.map +1 -1
- package/dist/api-catalog.d.ts +1 -57
- package/dist/api-catalog.js +1 -253
- package/dist/api-catalog.js.map +1 -1
- package/dist/audit-chain.d.ts +1 -80
- package/dist/audit-chain.js +1 -261
- package/dist/audit-chain.js.map +1 -1
- package/dist/audit.d.ts +1 -121
- package/dist/audit.js +1 -321
- package/dist/audit.js.map +1 -1
- package/dist/constants.d.ts +1 -150
- package/dist/constants.js +1 -211
- package/dist/constants.js.map +1 -1
- package/dist/database.d.ts +1 -6
- package/dist/database.js +1 -15
- package/dist/database.js.map +1 -1
- package/dist/errors.d.ts +1 -46
- package/dist/errors.js +1 -68
- package/dist/errors.js.map +1 -1
- package/dist/file-lock.d.ts +1 -93
- package/dist/file-lock.js +1 -239
- package/dist/file-lock.js.map +1 -1
- package/dist/fs.d.ts +1 -9
- package/dist/fs.js +1 -53
- package/dist/fs.js.map +1 -1
- package/dist/index.d.ts +2 -31
- package/dist/index.js +2 -31
- package/dist/index.js.map +1 -1
- package/dist/job-queue.d.ts +1 -76
- package/dist/job-queue.js +1 -199
- package/dist/job-queue.js.map +1 -1
- package/dist/lock.d.ts +1 -82
- package/dist/lock.js +1 -158
- package/dist/lock.js.map +1 -1
- package/dist/logger.d.ts +1 -31
- package/dist/logger.js +1 -152
- package/dist/logger.js.map +1 -1
- package/dist/memory-prune.d.ts +1 -18
- package/dist/memory-prune.js +1 -47
- package/dist/memory-prune.js.map +1 -1
- package/dist/migrations.d.ts +1 -69
- package/dist/migrations.js +1 -393
- package/dist/migrations.js.map +1 -1
- package/dist/modules/agent/agent-identity.d.ts +34 -0
- package/dist/modules/agent/agent-identity.js +77 -0
- package/dist/modules/agent/agent-identity.js.map +1 -0
- package/dist/modules/agent/api-catalog.d.ts +57 -0
- package/dist/modules/agent/api-catalog.js +254 -0
- package/dist/modules/agent/api-catalog.js.map +1 -0
- package/dist/modules/agent/index.d.ts +3 -0
- package/dist/modules/agent/index.js +4 -0
- package/dist/modules/agent/index.js.map +1 -0
- package/dist/modules/agent/tokenizer.d.ts +54 -0
- package/dist/modules/agent/tokenizer.js +272 -0
- package/dist/modules/agent/tokenizer.js.map +1 -0
- package/dist/modules/concurrency/file-lock.d.ts +93 -0
- package/dist/modules/concurrency/file-lock.js +240 -0
- package/dist/modules/concurrency/file-lock.js.map +1 -0
- package/dist/modules/concurrency/index.d.ts +6 -0
- package/dist/modules/concurrency/index.js +7 -0
- package/dist/modules/concurrency/index.js.map +1 -0
- package/dist/modules/concurrency/job-queue.d.ts +76 -0
- package/dist/modules/concurrency/job-queue.js +200 -0
- package/dist/modules/concurrency/job-queue.js.map +1 -0
- package/dist/modules/concurrency/lock.d.ts +82 -0
- package/dist/modules/concurrency/lock.js +159 -0
- package/dist/modules/concurrency/lock.js.map +1 -0
- package/dist/modules/concurrency/resilience.d.ts +84 -0
- package/dist/modules/concurrency/resilience.js +217 -0
- package/dist/modules/concurrency/resilience.js.map +1 -0
- package/dist/modules/concurrency/worker-pool.d.ts +52 -0
- package/dist/modules/concurrency/worker-pool.js +229 -0
- package/dist/modules/concurrency/worker-pool.js.map +1 -0
- package/dist/modules/concurrency/worker-thread.d.ts +21 -0
- package/dist/modules/concurrency/worker-thread.js +67 -0
- package/dist/modules/concurrency/worker-thread.js.map +1 -0
- package/dist/modules/index.d.ts +14 -0
- package/dist/modules/index.js +15 -0
- package/dist/modules/index.js.map +1 -0
- package/dist/modules/observability/audit-chain.d.ts +80 -0
- package/dist/modules/observability/audit-chain.js +262 -0
- package/dist/modules/observability/audit-chain.js.map +1 -0
- package/dist/modules/observability/audit.d.ts +121 -0
- package/dist/modules/observability/audit.js +322 -0
- package/dist/modules/observability/audit.js.map +1 -0
- package/dist/modules/observability/index.d.ts +4 -0
- package/dist/modules/observability/index.js +5 -0
- package/dist/modules/observability/index.js.map +1 -0
- package/dist/modules/observability/logger.d.ts +31 -0
- package/dist/modules/observability/logger.js +153 -0
- package/dist/modules/observability/logger.js.map +1 -0
- package/dist/modules/observability/otel.d.ts +39 -0
- package/dist/modules/observability/otel.js +93 -0
- package/dist/modules/observability/otel.js.map +1 -0
- package/dist/modules/primitives/constants.d.ts +150 -0
- package/dist/modules/primitives/constants.js +212 -0
- package/dist/modules/primitives/constants.js.map +1 -0
- package/dist/modules/primitives/errors.d.ts +46 -0
- package/dist/modules/primitives/errors.js +69 -0
- package/dist/modules/primitives/errors.js.map +1 -0
- package/dist/modules/primitives/fs.d.ts +9 -0
- package/dist/modules/primitives/fs.js +54 -0
- package/dist/modules/primitives/fs.js.map +1 -0
- package/dist/modules/primitives/index.d.ts +7 -0
- package/dist/modules/primitives/index.js +8 -0
- package/dist/modules/primitives/index.js.map +1 -0
- package/dist/modules/primitives/outbound-http.d.ts +32 -0
- package/dist/modules/primitives/outbound-http.js +88 -0
- package/dist/modules/primitives/outbound-http.js.map +1 -0
- package/dist/modules/primitives/string.d.ts +1 -0
- package/dist/modules/primitives/string.js +30 -0
- package/dist/modules/primitives/string.js.map +1 -0
- package/dist/modules/primitives/time.d.ts +5 -0
- package/dist/modules/primitives/time.js +35 -0
- package/dist/modules/primitives/time.js.map +1 -0
- package/dist/modules/primitives/types.d.ts +27 -0
- package/dist/modules/primitives/types.js +17 -0
- package/dist/modules/primitives/types.js.map +1 -0
- package/dist/modules/sandbox/index.d.ts +3 -0
- package/dist/modules/sandbox/index.js +4 -0
- package/dist/modules/sandbox/index.js.map +1 -0
- package/dist/modules/sandbox/sandbox-fs.d.ts +28 -0
- package/dist/modules/sandbox/sandbox-fs.js +143 -0
- package/dist/modules/sandbox/sandbox-fs.js.map +1 -0
- package/dist/modules/sandbox/sandbox-runtime.d.ts +78 -0
- package/dist/modules/sandbox/sandbox-runtime.js +282 -0
- package/dist/modules/sandbox/sandbox-runtime.js.map +1 -0
- package/dist/modules/sandbox/sandbox.d.ts +54 -0
- package/dist/modules/sandbox/sandbox.js +179 -0
- package/dist/modules/sandbox/sandbox.js.map +1 -0
- package/dist/modules/security/index.d.ts +5 -0
- package/dist/modules/security/index.js +6 -0
- package/dist/modules/security/index.js.map +1 -0
- package/dist/modules/security/pii.d.ts +70 -0
- package/dist/modules/security/pii.js +276 -0
- package/dist/modules/security/pii.js.map +1 -0
- package/dist/modules/security/policy-gate.d.ts +42 -0
- package/dist/modules/security/policy-gate.js +172 -0
- package/dist/modules/security/policy-gate.js.map +1 -0
- package/dist/modules/security/retention.d.ts +68 -0
- package/dist/modules/security/retention.js +218 -0
- package/dist/modules/security/retention.js.map +1 -0
- package/dist/modules/security/secrets.d.ts +99 -0
- package/dist/modules/security/secrets.js +371 -0
- package/dist/modules/security/secrets.js.map +1 -0
- package/dist/modules/security/tls.d.ts +87 -0
- package/dist/modules/security/tls.js +154 -0
- package/dist/modules/security/tls.js.map +1 -0
- package/dist/modules/storage/database.d.ts +6 -0
- package/dist/modules/storage/database.js +16 -0
- package/dist/modules/storage/database.js.map +1 -0
- package/dist/modules/storage/index.d.ts +4 -0
- package/dist/modules/storage/index.js +5 -0
- package/dist/modules/storage/index.js.map +1 -0
- package/dist/modules/storage/memory-prune.d.ts +18 -0
- package/dist/modules/storage/memory-prune.js +48 -0
- package/dist/modules/storage/memory-prune.js.map +1 -0
- package/dist/modules/storage/migrations.d.ts +69 -0
- package/dist/modules/storage/migrations.js +394 -0
- package/dist/modules/storage/migrations.js.map +1 -0
- package/dist/modules/storage/storage.d.ts +341 -0
- package/dist/modules/storage/storage.js +1004 -0
- package/dist/modules/storage/storage.js.map +1 -0
- package/dist/otel.d.ts +1 -39
- package/dist/otel.js +1 -92
- package/dist/otel.js.map +1 -1
- package/dist/outbound-http.d.ts +1 -32
- package/dist/outbound-http.js +1 -87
- package/dist/outbound-http.js.map +1 -1
- package/dist/pii.d.ts +1 -70
- package/dist/pii.js +1 -275
- package/dist/pii.js.map +1 -1
- package/dist/policy-gate.d.ts +1 -42
- package/dist/policy-gate.js +1 -171
- package/dist/policy-gate.js.map +1 -1
- package/dist/resilience.d.ts +1 -84
- package/dist/resilience.js +1 -216
- package/dist/resilience.js.map +1 -1
- package/dist/retention.d.ts +1 -68
- package/dist/retention.js +1 -217
- package/dist/retention.js.map +1 -1
- package/dist/sandbox-fs.d.ts +1 -28
- package/dist/sandbox-fs.js +1 -142
- package/dist/sandbox-fs.js.map +1 -1
- package/dist/sandbox-runtime.d.ts +1 -78
- package/dist/sandbox-runtime.js +1 -281
- package/dist/sandbox-runtime.js.map +1 -1
- package/dist/sandbox.d.ts +1 -54
- package/dist/sandbox.js +1 -178
- package/dist/sandbox.js.map +1 -1
- package/dist/secrets.d.ts +1 -99
- package/dist/secrets.js +1 -370
- package/dist/secrets.js.map +1 -1
- package/dist/storage.d.ts +1 -341
- package/dist/storage.js +1 -1003
- package/dist/storage.js.map +1 -1
- package/dist/string.d.ts +1 -1
- package/dist/string.js +1 -29
- package/dist/string.js.map +1 -1
- package/dist/time.d.ts +1 -5
- package/dist/time.js +1 -34
- package/dist/time.js.map +1 -1
- package/dist/tls.d.ts +1 -87
- package/dist/tls.js +1 -153
- package/dist/tls.js.map +1 -1
- package/dist/tokenizer.d.ts +1 -54
- package/dist/tokenizer.js +1 -271
- package/dist/tokenizer.js.map +1 -1
- package/dist/types.d.ts +1 -27
- package/dist/types.js +1 -16
- package/dist/types.js.map +1 -1
- package/dist/worker-pool.d.ts +1 -52
- package/dist/worker-pool.js +1 -228
- package/dist/worker-pool.js.map +1 -1
- package/dist/worker-thread.d.ts +1 -21
- package/dist/worker-thread.js +1 -66
- package/dist/worker-thread.js.map +1 -1
- package/package.json +45 -34
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Job Queue — concurrency-limited async work dispatcher backed by SQLite database persistence.
|
|
3
|
+
*
|
|
4
|
+
* Prevents unbounded parallel LLM / agent work from ballooning memory
|
|
5
|
+
* or starving the main event loop. Job lifecycles (pending, running, completed, failed)
|
|
6
|
+
* are recorded in the database when initialized, providing persistence and auditability.
|
|
7
|
+
*/
|
|
8
|
+
export type JobStatus = "pending" | "running" | "completed" | "failed";
|
|
9
|
+
export interface JobRecord {
|
|
10
|
+
id: number;
|
|
11
|
+
queue_name: string;
|
|
12
|
+
payload: string;
|
|
13
|
+
status: JobStatus;
|
|
14
|
+
retries: number;
|
|
15
|
+
max_retries: number;
|
|
16
|
+
error_message?: string | null;
|
|
17
|
+
created_at: string;
|
|
18
|
+
updated_at: string;
|
|
19
|
+
}
|
|
20
|
+
export interface EnqueueOptions {
|
|
21
|
+
/** Optional JSON-serializable payload or description of the task */
|
|
22
|
+
payload?: unknown;
|
|
23
|
+
/** Max retries before permanent failure (default: 3) */
|
|
24
|
+
maxRetries?: number;
|
|
25
|
+
}
|
|
26
|
+
export interface JobQueueOptions {
|
|
27
|
+
/** Max concurrent running jobs. Default: os.cpus().length (min 1, max 16). */
|
|
28
|
+
maxConcurrency?: number;
|
|
29
|
+
/** Optional name for diagnostics and DB tracking. */
|
|
30
|
+
name?: string;
|
|
31
|
+
/** Called when a job fails (after the promise rejects to the caller). */
|
|
32
|
+
onError?: (error: unknown, jobId: number) => void;
|
|
33
|
+
}
|
|
34
|
+
export interface JobQueueStats {
|
|
35
|
+
name: string;
|
|
36
|
+
maxConcurrency: number;
|
|
37
|
+
running: number;
|
|
38
|
+
pending: number;
|
|
39
|
+
completed: number;
|
|
40
|
+
failed: number;
|
|
41
|
+
}
|
|
42
|
+
export declare class JobQueue {
|
|
43
|
+
private readonly maxConcurrency;
|
|
44
|
+
private readonly name;
|
|
45
|
+
private readonly onError?;
|
|
46
|
+
private readonly pending;
|
|
47
|
+
private running;
|
|
48
|
+
private nextId;
|
|
49
|
+
private completed;
|
|
50
|
+
private failed;
|
|
51
|
+
private closed;
|
|
52
|
+
constructor(options?: JobQueueOptions);
|
|
53
|
+
/**
|
|
54
|
+
* Enqueue an async job. Resolves/rejects with the job result.
|
|
55
|
+
* Back-pressures via wait in the queue — does not spawn unbounded work.
|
|
56
|
+
* If SQLite is initialized, records job creation in the database.
|
|
57
|
+
*/
|
|
58
|
+
enqueue<T>(fn: () => Promise<T>, options?: EnqueueOptions): Promise<T>;
|
|
59
|
+
/** Retrieve a job record from the database by ID. */
|
|
60
|
+
getJobRecord(id: number): JobRecord | null;
|
|
61
|
+
/** Retrieve job records from the database for this queue. */
|
|
62
|
+
getQueueRecords(status?: JobStatus, limit?: number): JobRecord[];
|
|
63
|
+
/** Current queue diagnostics. */
|
|
64
|
+
getStats(): JobQueueStats;
|
|
65
|
+
/** Reject pending jobs and stop accepting new ones. In-flight jobs finish. */
|
|
66
|
+
close(reason?: string): void;
|
|
67
|
+
/** Wait until running + pending reach zero (or timeout). */
|
|
68
|
+
drain(timeoutMs?: number): Promise<void>;
|
|
69
|
+
private pump;
|
|
70
|
+
private execute;
|
|
71
|
+
}
|
|
72
|
+
/** Backward compatibility alias */
|
|
73
|
+
export { JobQueue as InMemoryJobQueue };
|
|
74
|
+
export declare function getAgentJobQueue(options?: JobQueueOptions): JobQueue;
|
|
75
|
+
/** Test helper — reset singleton. */
|
|
76
|
+
export declare function resetAgentJobQueue(): void;
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Job Queue — concurrency-limited async work dispatcher backed by SQLite database persistence.
|
|
3
|
+
*
|
|
4
|
+
* Prevents unbounded parallel LLM / agent work from ballooning memory
|
|
5
|
+
* or starving the main event loop. Job lifecycles (pending, running, completed, failed)
|
|
6
|
+
* are recorded in the database when initialized, providing persistence and auditability.
|
|
7
|
+
*/
|
|
8
|
+
import os from "os";
|
|
9
|
+
import { databaseHolder } from "../storage/database.js";
|
|
10
|
+
export class JobQueue {
|
|
11
|
+
maxConcurrency;
|
|
12
|
+
name;
|
|
13
|
+
onError;
|
|
14
|
+
pending = [];
|
|
15
|
+
running = 0;
|
|
16
|
+
nextId = 1;
|
|
17
|
+
completed = 0;
|
|
18
|
+
failed = 0;
|
|
19
|
+
closed = false;
|
|
20
|
+
constructor(options = {}) {
|
|
21
|
+
const cpus = Math.max(1, os.cpus()?.length || 1);
|
|
22
|
+
const requested = options.maxConcurrency ?? cpus;
|
|
23
|
+
this.maxConcurrency = Math.max(1, Math.min(requested, 16));
|
|
24
|
+
this.name = options.name ?? "default";
|
|
25
|
+
this.onError = options.onError;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Enqueue an async job. Resolves/rejects with the job result.
|
|
29
|
+
* Back-pressures via wait in the queue — does not spawn unbounded work.
|
|
30
|
+
* If SQLite is initialized, records job creation in the database.
|
|
31
|
+
*/
|
|
32
|
+
enqueue(fn, options) {
|
|
33
|
+
if (this.closed) {
|
|
34
|
+
return Promise.reject(new Error(`[JobQueue:${this.name}] Queue is closed; cannot accept new jobs.`));
|
|
35
|
+
}
|
|
36
|
+
let dbId;
|
|
37
|
+
if (databaseHolder.hasDB()) {
|
|
38
|
+
try {
|
|
39
|
+
const db = databaseHolder.getDB();
|
|
40
|
+
const payloadStr = typeof options?.payload === "string"
|
|
41
|
+
? options.payload
|
|
42
|
+
: JSON.stringify(options?.payload ?? null);
|
|
43
|
+
const res = db.prepare(`INSERT INTO job_queue (queue_name, payload, status, retries, max_retries, created_at, updated_at)
|
|
44
|
+
VALUES (?, ?, 'pending', 0, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)`).run(this.name, payloadStr, options?.maxRetries ?? 3);
|
|
45
|
+
dbId = Number(res.lastInsertRowid);
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
// Table might not exist yet if running pre-migration
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return new Promise((resolve, reject) => {
|
|
52
|
+
const item = {
|
|
53
|
+
id: this.nextId++,
|
|
54
|
+
dbId,
|
|
55
|
+
run: fn,
|
|
56
|
+
resolve,
|
|
57
|
+
reject,
|
|
58
|
+
};
|
|
59
|
+
this.pending.push(item);
|
|
60
|
+
this.pump();
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
/** Retrieve a job record from the database by ID. */
|
|
64
|
+
getJobRecord(id) {
|
|
65
|
+
if (!databaseHolder.hasDB())
|
|
66
|
+
return null;
|
|
67
|
+
try {
|
|
68
|
+
const db = databaseHolder.getDB();
|
|
69
|
+
return db.prepare("SELECT * FROM job_queue WHERE id = ?").get(id) || null;
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/** Retrieve job records from the database for this queue. */
|
|
76
|
+
getQueueRecords(status, limit = 50) {
|
|
77
|
+
if (!databaseHolder.hasDB())
|
|
78
|
+
return [];
|
|
79
|
+
try {
|
|
80
|
+
const db = databaseHolder.getDB();
|
|
81
|
+
if (status) {
|
|
82
|
+
return db.prepare("SELECT * FROM job_queue WHERE queue_name = ? AND status = ? ORDER BY id DESC LIMIT ?").all(this.name, status, limit);
|
|
83
|
+
}
|
|
84
|
+
return db.prepare("SELECT * FROM job_queue WHERE queue_name = ? ORDER BY id DESC LIMIT ?").all(this.name, limit);
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
return [];
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/** Current queue diagnostics. */
|
|
91
|
+
getStats() {
|
|
92
|
+
return {
|
|
93
|
+
name: this.name,
|
|
94
|
+
maxConcurrency: this.maxConcurrency,
|
|
95
|
+
running: this.running,
|
|
96
|
+
pending: this.pending.length,
|
|
97
|
+
completed: this.completed,
|
|
98
|
+
failed: this.failed,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/** Reject pending jobs and stop accepting new ones. In-flight jobs finish. */
|
|
102
|
+
close(reason = "Queue closed") {
|
|
103
|
+
this.closed = true;
|
|
104
|
+
while (this.pending.length > 0) {
|
|
105
|
+
const item = this.pending.shift();
|
|
106
|
+
item?.reject(new Error(`[JobQueue:${this.name}] ${reason}`));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/** Wait until running + pending reach zero (or timeout). */
|
|
110
|
+
async drain(timeoutMs = 30_000) {
|
|
111
|
+
const start = Date.now();
|
|
112
|
+
while (this.running > 0 || this.pending.length > 0) {
|
|
113
|
+
if (Date.now() - start > timeoutMs) {
|
|
114
|
+
throw new Error(`[JobQueue:${this.name}] drain timed out after ${timeoutMs}ms`);
|
|
115
|
+
}
|
|
116
|
+
await new Promise((r) => setTimeout(r, 10));
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
pump() {
|
|
120
|
+
while (this.running < this.maxConcurrency && this.pending.length > 0) {
|
|
121
|
+
const item = this.pending.shift();
|
|
122
|
+
if (!item)
|
|
123
|
+
break;
|
|
124
|
+
this.running++;
|
|
125
|
+
void this.execute(item);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
async execute(item) {
|
|
129
|
+
if (item.dbId && databaseHolder.hasDB()) {
|
|
130
|
+
try {
|
|
131
|
+
const db = databaseHolder.getDB();
|
|
132
|
+
db.prepare("UPDATE job_queue SET status = 'running', updated_at = CURRENT_TIMESTAMP WHERE id = ?").run(item.dbId);
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
// Ignore DB error during execution transition
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
try {
|
|
139
|
+
const result = await item.run();
|
|
140
|
+
this.completed++;
|
|
141
|
+
if (item.dbId && databaseHolder.hasDB()) {
|
|
142
|
+
try {
|
|
143
|
+
const db = databaseHolder.getDB();
|
|
144
|
+
db.prepare("UPDATE job_queue SET status = 'completed', updated_at = CURRENT_TIMESTAMP WHERE id = ?").run(item.dbId);
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
// Ignore DB error
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
item.resolve(result);
|
|
151
|
+
}
|
|
152
|
+
catch (error) {
|
|
153
|
+
this.failed++;
|
|
154
|
+
if (item.dbId && databaseHolder.hasDB()) {
|
|
155
|
+
try {
|
|
156
|
+
const db = databaseHolder.getDB();
|
|
157
|
+
const errMsg = error instanceof Error ? error.message : String(error);
|
|
158
|
+
db.prepare("UPDATE job_queue SET status = 'failed', error_message = ?, retries = retries + 1, updated_at = CURRENT_TIMESTAMP WHERE id = ?").run(errMsg, item.dbId);
|
|
159
|
+
}
|
|
160
|
+
catch {
|
|
161
|
+
// Ignore DB error
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
try {
|
|
165
|
+
this.onError?.(error, item.id);
|
|
166
|
+
}
|
|
167
|
+
catch {
|
|
168
|
+
// ignore listener errors
|
|
169
|
+
}
|
|
170
|
+
item.reject(error);
|
|
171
|
+
}
|
|
172
|
+
finally {
|
|
173
|
+
this.running--;
|
|
174
|
+
this.pump();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/** Backward compatibility alias */
|
|
179
|
+
export { JobQueue as InMemoryJobQueue };
|
|
180
|
+
/** Shared singleton for agent orchestration (lazy). */
|
|
181
|
+
let agentJobQueue = null;
|
|
182
|
+
export function getAgentJobQueue(options) {
|
|
183
|
+
if (!agentJobQueue) {
|
|
184
|
+
const fromEnv = process.env.ATABEY_JOB_CONCURRENCY
|
|
185
|
+
? parseInt(process.env.ATABEY_JOB_CONCURRENCY, 10)
|
|
186
|
+
: undefined;
|
|
187
|
+
agentJobQueue = new JobQueue({
|
|
188
|
+
name: "agent",
|
|
189
|
+
maxConcurrency: options?.maxConcurrency ?? (Number.isFinite(fromEnv) ? fromEnv : undefined),
|
|
190
|
+
onError: options?.onError,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
return agentJobQueue;
|
|
194
|
+
}
|
|
195
|
+
/** Test helper — reset singleton. */
|
|
196
|
+
export function resetAgentJobQueue() {
|
|
197
|
+
agentJobQueue?.close("reset");
|
|
198
|
+
agentJobQueue = null;
|
|
199
|
+
}
|
|
200
|
+
//# sourceMappingURL=job-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CAAC;QAChB,CAAC;IACL,CAAC;CACJ;AAED,mCAAmC;AACnC,OAAO,EAAE,QAAQ,IAAI,gBAAgB,EAAE,CAAC;AAExC,uDAAuD;AACvD,IAAI,aAAa,GAAoB,IAAI,CAAC;AAE1C,MAAM,UAAU,gBAAgB,CAAC,OAAyB;IACtD,IAAI,CAAC,aAAa,EAAE,CAAC;QACjB,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,sBAAsB;YAC9C,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,sBAAsB,EAAE,EAAE,CAAC;YAClD,CAAC,CAAC,SAAS,CAAC;QAChB,aAAa,GAAG,IAAI,QAAQ,CAAC;YACzB,IAAI,EAAE,OAAO;YACb,cAAc,EAAE,OAAO,EAAE,cAAc,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC;YAC3F,OAAO,EAAE,OAAO,EAAE,OAAO;SAC5B,CAAC,CAAC;IACP,CAAC;IACD,OAAO,aAAa,CAAC;AACzB,CAAC;AAED,qCAAqC;AACrC,MAAM,UAAU,kBAAkB;IAC9B,aAAa,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;IAC9B,aAAa,GAAG,IAAI,CAAC;AACzB,CAAC"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* [LOCK] Multi-user Distributed Lock Mechanism
|
|
3
|
+
*
|
|
4
|
+
* Git-based lock system. Enables team members to work on the same
|
|
5
|
+
* files/resources without conflicts.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* const lock = new DistributedLock("database/schema.sql");
|
|
9
|
+
* if (lock.acquire("user-123", 60000)) {
|
|
10
|
+
* // Apply changes
|
|
11
|
+
* lock.release("user-123");
|
|
12
|
+
* }
|
|
13
|
+
*/
|
|
14
|
+
export type LockStatus = "LOCKED" | "AVAILABLE" | "STALE" | "EXPIRED";
|
|
15
|
+
export interface LockInfo {
|
|
16
|
+
resource: string;
|
|
17
|
+
ownerId: string;
|
|
18
|
+
ownerName: string;
|
|
19
|
+
acquiredAt: string;
|
|
20
|
+
expiresAt: string;
|
|
21
|
+
reason: string;
|
|
22
|
+
traceId?: string;
|
|
23
|
+
}
|
|
24
|
+
export declare class DistributedLock {
|
|
25
|
+
private static TABLE;
|
|
26
|
+
/**
|
|
27
|
+
* Initialize the locks table (run once at startup).
|
|
28
|
+
*/
|
|
29
|
+
static initialize(): void;
|
|
30
|
+
/**
|
|
31
|
+
* Attempts to acquire a lock on a resource.
|
|
32
|
+
*
|
|
33
|
+
* @param resource - Lock resource name (e.g., "apps/backend/src/types/user.ts")
|
|
34
|
+
* @param ownerId - Unique owner identifier (e.g., "yusuf", "ci-bot-01")
|
|
35
|
+
* @param options - Optional parameters
|
|
36
|
+
* @returns true if lock acquired, false if already locked
|
|
37
|
+
*/
|
|
38
|
+
static acquire(resource: string, ownerId: string, options?: {
|
|
39
|
+
ownerName?: string;
|
|
40
|
+
ttlMs?: number;
|
|
41
|
+
reason?: string;
|
|
42
|
+
traceId?: string;
|
|
43
|
+
force?: boolean;
|
|
44
|
+
}): boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Releases a lock. Only the lock owner can release.
|
|
47
|
+
*
|
|
48
|
+
* @param resource - Lock resource name
|
|
49
|
+
* @param ownerId - Owner identifier for verification
|
|
50
|
+
* @returns true if released successfully
|
|
51
|
+
*/
|
|
52
|
+
static release(resource: string, ownerId: string): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Checks if a resource is locked.
|
|
55
|
+
*/
|
|
56
|
+
static isLocked(resource: string): boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Gets lock information for a resource.
|
|
59
|
+
*/
|
|
60
|
+
static getLock(resource: string): LockInfo | null;
|
|
61
|
+
/**
|
|
62
|
+
* Lists all active locks.
|
|
63
|
+
*/
|
|
64
|
+
static listActiveLocks(): LockInfo[];
|
|
65
|
+
/**
|
|
66
|
+
* Lists all locks owned by a specific owner.
|
|
67
|
+
*/
|
|
68
|
+
static listOwnerLocks(ownerId: string): LockInfo[];
|
|
69
|
+
/**
|
|
70
|
+
* Releases all locks owned by a specific owner (e.g., on disconnect).
|
|
71
|
+
*/
|
|
72
|
+
static releaseAllByOwner(ownerId: string): number;
|
|
73
|
+
/**
|
|
74
|
+
* Releases all expired/stale locks.
|
|
75
|
+
*/
|
|
76
|
+
private static releaseStaleLocks;
|
|
77
|
+
/**
|
|
78
|
+
* Generates a unique owner ID for the current user/machine.
|
|
79
|
+
* Uses git config user.name or hostname as fallback.
|
|
80
|
+
*/
|
|
81
|
+
static getLocalOwnerId(): string;
|
|
82
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* [LOCK] Multi-user Distributed Lock Mechanism
|
|
3
|
+
*
|
|
4
|
+
* Git-based lock system. Enables team members to work on the same
|
|
5
|
+
* files/resources without conflicts.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* const lock = new DistributedLock("database/schema.sql");
|
|
9
|
+
* if (lock.acquire("user-123", 60000)) {
|
|
10
|
+
* // Apply changes
|
|
11
|
+
* lock.release("user-123");
|
|
12
|
+
* }
|
|
13
|
+
*/
|
|
14
|
+
import { execSync } from "child_process";
|
|
15
|
+
import os from "os";
|
|
16
|
+
import { databaseHolder } from "../storage/database.js";
|
|
17
|
+
export class DistributedLock {
|
|
18
|
+
static TABLE = "distributed_locks";
|
|
19
|
+
/**
|
|
20
|
+
* Initialize the locks table (run once at startup).
|
|
21
|
+
*/
|
|
22
|
+
static initialize() {
|
|
23
|
+
const db = databaseHolder.getDB();
|
|
24
|
+
db.exec(`
|
|
25
|
+
CREATE TABLE IF NOT EXISTS ${this.TABLE} (
|
|
26
|
+
resource TEXT PRIMARY KEY,
|
|
27
|
+
ownerId TEXT NOT NULL,
|
|
28
|
+
ownerName TEXT NOT NULL,
|
|
29
|
+
acquiredAt TEXT NOT NULL,
|
|
30
|
+
expiresAt TEXT NOT NULL,
|
|
31
|
+
reason TEXT,
|
|
32
|
+
traceId TEXT
|
|
33
|
+
)
|
|
34
|
+
`);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Attempts to acquire a lock on a resource.
|
|
38
|
+
*
|
|
39
|
+
* @param resource - Lock resource name (e.g., "apps/backend/src/types/user.ts")
|
|
40
|
+
* @param ownerId - Unique owner identifier (e.g., "yusuf", "ci-bot-01")
|
|
41
|
+
* @param options - Optional parameters
|
|
42
|
+
* @returns true if lock acquired, false if already locked
|
|
43
|
+
*/
|
|
44
|
+
static acquire(resource, ownerId, options = {}) {
|
|
45
|
+
const db = databaseHolder.getDB();
|
|
46
|
+
const now = new Date();
|
|
47
|
+
const expiresAt = new Date(now.getTime() + (options.ttlMs || 60000)).toISOString();
|
|
48
|
+
const ownerName = options.ownerName || ownerId;
|
|
49
|
+
const reason = options.reason || "No reason provided";
|
|
50
|
+
// Clean up stale locks first
|
|
51
|
+
this.releaseStaleLocks();
|
|
52
|
+
// If force=true, override existing lock directly using INSERT OR REPLACE
|
|
53
|
+
if (options.force) {
|
|
54
|
+
db.prepare(`
|
|
55
|
+
INSERT OR REPLACE INTO ${this.TABLE} (resource, ownerId, ownerName, acquiredAt, expiresAt, reason, traceId)
|
|
56
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
57
|
+
`).run(resource, ownerId, ownerName, now.toISOString(), expiresAt, reason, options.traceId || null);
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
// Acquire new lock atomically using INSERT OR IGNORE.
|
|
61
|
+
// If the resource is already locked, changes will be 0.
|
|
62
|
+
const result = db.prepare(`
|
|
63
|
+
INSERT OR IGNORE INTO ${this.TABLE} (resource, ownerId, ownerName, acquiredAt, expiresAt, reason, traceId)
|
|
64
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
65
|
+
`).run(resource, ownerId, ownerName, now.toISOString(), expiresAt, reason, options.traceId || null);
|
|
66
|
+
return result.changes > 0;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Releases a lock. Only the lock owner can release.
|
|
70
|
+
*
|
|
71
|
+
* @param resource - Lock resource name
|
|
72
|
+
* @param ownerId - Owner identifier for verification
|
|
73
|
+
* @returns true if released successfully
|
|
74
|
+
*/
|
|
75
|
+
static release(resource, ownerId) {
|
|
76
|
+
const db = databaseHolder.getDB();
|
|
77
|
+
const result = db.prepare(`DELETE FROM ${this.TABLE} WHERE resource = ? AND ownerId = ?`).run(resource, ownerId);
|
|
78
|
+
return result.changes > 0;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Checks if a resource is locked.
|
|
82
|
+
*/
|
|
83
|
+
static isLocked(resource) {
|
|
84
|
+
const db = databaseHolder.getDB();
|
|
85
|
+
const nowIso = new Date().toISOString();
|
|
86
|
+
const lock = db.prepare(`SELECT * FROM ${this.TABLE} WHERE resource = ? AND expiresAt > ?`).get(resource, nowIso);
|
|
87
|
+
return !!lock;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Gets lock information for a resource.
|
|
91
|
+
*/
|
|
92
|
+
static getLock(resource) {
|
|
93
|
+
const db = databaseHolder.getDB();
|
|
94
|
+
this.releaseStaleLocks();
|
|
95
|
+
const lock = db.prepare(`SELECT * FROM ${this.TABLE} WHERE resource = ?`).get(resource);
|
|
96
|
+
return lock || null;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Lists all active locks.
|
|
100
|
+
*/
|
|
101
|
+
static listActiveLocks() {
|
|
102
|
+
const db = databaseHolder.getDB();
|
|
103
|
+
this.releaseStaleLocks();
|
|
104
|
+
const nowIso = new Date().toISOString();
|
|
105
|
+
return db.prepare(`SELECT * FROM ${this.TABLE} WHERE expiresAt > ? ORDER BY acquiredAt DESC`).all(nowIso);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Lists all locks owned by a specific owner.
|
|
109
|
+
*/
|
|
110
|
+
static listOwnerLocks(ownerId) {
|
|
111
|
+
const db = databaseHolder.getDB();
|
|
112
|
+
return db.prepare(`SELECT * FROM ${this.TABLE} WHERE ownerId = ? ORDER BY acquiredAt DESC`).all(ownerId);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Releases all locks owned by a specific owner (e.g., on disconnect).
|
|
116
|
+
*/
|
|
117
|
+
static releaseAllByOwner(ownerId) {
|
|
118
|
+
const db = databaseHolder.getDB();
|
|
119
|
+
const result = db.prepare(`DELETE FROM ${this.TABLE} WHERE ownerId = ?`).run(ownerId);
|
|
120
|
+
return result.changes;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Releases all expired/stale locks.
|
|
124
|
+
*/
|
|
125
|
+
static releaseStaleLocks() {
|
|
126
|
+
const db = databaseHolder.getDB();
|
|
127
|
+
const nowIso = new Date().toISOString();
|
|
128
|
+
const result = db.prepare(`DELETE FROM ${this.TABLE} WHERE expiresAt < ?`).run(nowIso);
|
|
129
|
+
return result.changes;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Generates a unique owner ID for the current user/machine.
|
|
133
|
+
* Uses git config user.name or hostname as fallback.
|
|
134
|
+
*/
|
|
135
|
+
static getLocalOwnerId() {
|
|
136
|
+
// Try git config
|
|
137
|
+
try {
|
|
138
|
+
const name = execSync("git config user.name", { encoding: "utf8" }).trim();
|
|
139
|
+
if (name)
|
|
140
|
+
return name;
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
// Fallback
|
|
144
|
+
}
|
|
145
|
+
// Try environment
|
|
146
|
+
if (process.env.USER)
|
|
147
|
+
return process.env.USER;
|
|
148
|
+
if (process.env.USERNAME)
|
|
149
|
+
return process.env.USERNAME;
|
|
150
|
+
// Fallback to hostname
|
|
151
|
+
try {
|
|
152
|
+
return os.hostname();
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
return "unknown-user";
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
//# sourceMappingURL=lock.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lock.js","sourceRoot":"","sources":["../../../src/modules/concurrency/lock.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAcxD,MAAM,OAAO,eAAe;IAChB,MAAM,CAAC,KAAK,GAAG,mBAAmB,CAAC;IAE3C;;OAEG;IACI,MAAM,CAAC,UAAU;QACpB,MAAM,EAAE,GAAG,cAAc,CAAC,KAAK,EAAE,CAAC;QAClC,EAAE,CAAC,IAAI,CAAC;yCACyB,IAAI,CAAC,KAAK;;;;;;;;;SAS1C,CAAC,CAAC;IACP,CAAC;IAED;;;;;;;OAOG;IACI,MAAM,CAAC,OAAO,CACjB,QAAgB,EAChB,OAAe,EACf,UAMI,EAAE;QAEN,MAAM,EAAE,GAAG,cAAc,CAAC,KAAK,EAAE,CAAC;QAClC,MAAM,GAAG,GAAG,IAAI,IAAI,EAAE,CAAC;QACvB,MAAM,SAAS,GAAG,IAAI,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,GAAG,CAAC,OAAO,CAAC,KAAK,IAAI,KAAK,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC;QACnF,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,OAAO,CAAC;QAC/C,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,oBAAoB,CAAC;QAEtD,6BAA6B;QAC7B,IAAI,CAAC,iBAAiB,EAAE,CAAC;QAEzB,yEAAyE;QACzE,IAAI,OAAO,CAAC,KAAK,EAAE,CAAC;YAChB,EAAE,CAAC,OAAO,CAAC;yCACkB,IAAI,CAAC,KAAK;;aAEtC,CAAC,CAAC,GA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AW,EAAE,CAAC;QACxC,MAAM,MAAM,GAAG,EAAE,CAAC,OAAO,CACrB,eAAe,IAAI,CAAC,KAAK,sBAAsB,CAClD,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAEd,OAAO,MAAM,CAAC,OAAO,CAAC;IAC1B,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,eAAe;QACzB,iBAAiB;QACjB,IAAI,CAAC;YACD,MAAM,IAAI,GAAG,QAAQ,CAAC,sBAAsB,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;YAC3E,IAAI,IAAI;gBAAE,OAAO,IAAI,CAAC;QAC1B,CAAC;QAAC,MAAM,CAAC;YACL,WAAW;QACf,CAAC;QAED,kBAAkB;QAClB,IAAI,OAAO,CAAC,GAAG,CAAC,IAAI;YAAE,OAAO,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC;QAC9C,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ;YAAE,OAAO,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC;QAEtD,uBAAuB;QACvB,IAAI,CAAC;YACD,OAAO,EAAE,CAAC,QAAQ,EAAE,CAAC;QACzB,CAAC;QAAC,MAAM,CAAC;YACL,OAAO,cAAc,CAAC;QAC1B,CAAC;IACL,CAAC"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Polly-style resilience primitives — no external deps.
|
|
3
|
+
*
|
|
4
|
+
* - withRetry: exponential backoff + jitter for transient failures
|
|
5
|
+
* - CircuitBreaker: closed → open → half-open state machine
|
|
6
|
+
* - withResilience: compose retry + circuit breaker
|
|
7
|
+
*
|
|
8
|
+
* Typical use: LLM provider rate limits, timeouts, 5xx, network blips.
|
|
9
|
+
*/
|
|
10
|
+
export type CircuitState = "closed" | "open" | "half-open";
|
|
11
|
+
export interface RetryOptions {
|
|
12
|
+
/** Max attempts including the first try. Default: 3 */
|
|
13
|
+
maxAttempts?: number;
|
|
14
|
+
/** Initial delay in ms. Default: 100 */
|
|
15
|
+
baseDelayMs?: number;
|
|
16
|
+
/** Cap on delay in ms. Default: 10_000 */
|
|
17
|
+
maxDelayMs?: number;
|
|
18
|
+
/** Exponential factor. Default: 2 */
|
|
19
|
+
factor?: number;
|
|
20
|
+
/** Add random jitter [0, delay). Default: true */
|
|
21
|
+
jitter?: boolean;
|
|
22
|
+
/** Return true to retry this error. Default: retry all */
|
|
23
|
+
retryIf?: (error: unknown, attempt: number) => boolean;
|
|
24
|
+
/** Optional hook before each sleep */
|
|
25
|
+
onRetry?: (error: unknown, attempt: number, delayMs: number) => void;
|
|
26
|
+
/** Optional abort signal */
|
|
27
|
+
signal?: AbortSignal;
|
|
28
|
+
}
|
|
29
|
+
export interface CircuitBreakerOptions {
|
|
30
|
+
/** Failures in closed state before opening. Default: 5 */
|
|
31
|
+
failureThreshold?: number;
|
|
32
|
+
/** Successes in half-open before closing. Default: 2 */
|
|
33
|
+
successThreshold?: number;
|
|
34
|
+
/** Time to wait in open before half-open probe. Default: 30_000 */
|
|
35
|
+
openMs?: number;
|
|
36
|
+
/** Optional name for error messages */
|
|
37
|
+
name?: string;
|
|
38
|
+
}
|
|
39
|
+
export declare class CircuitOpenError extends Error {
|
|
40
|
+
readonly circuit: string;
|
|
41
|
+
readonly retryAfterMs: number;
|
|
42
|
+
constructor(circuit: string, retryAfterMs: number);
|
|
43
|
+
}
|
|
44
|
+
export declare class CircuitBreaker {
|
|
45
|
+
private state;
|
|
46
|
+
private failures;
|
|
47
|
+
private successes;
|
|
48
|
+
private openedAt;
|
|
49
|
+
private readonly failureThreshold;
|
|
50
|
+
private readonly successThreshold;
|
|
51
|
+
private readonly openMs;
|
|
52
|
+
private readonly name;
|
|
53
|
+
constructor(options?: CircuitBreakerOptions);
|
|
54
|
+
getState(): CircuitState;
|
|
55
|
+
getStats(): {
|
|
56
|
+
state: CircuitState;
|
|
57
|
+
failures: number;
|
|
58
|
+
successes: number;
|
|
59
|
+
name: string;
|
|
60
|
+
};
|
|
61
|
+
/** Manual reset (e.g. after operator intervention). */
|
|
62
|
+
reset(): void;
|
|
63
|
+
exec<T>(fn: () => Promise<T>): Promise<T>;
|
|
64
|
+
private maybeTransitionToHalfOpen;
|
|
65
|
+
private onSuccess;
|
|
66
|
+
private onFailure;
|
|
67
|
+
private trip;
|
|
68
|
+
}
|
|
69
|
+
export declare function computeBackoffDelay(attempt: number, options?: Pick<RetryOptions, "baseDelayMs" | "maxDelayMs" | "factor" | "jitter">): number;
|
|
70
|
+
export declare function withRetry<T>(fn: (attempt: number) => Promise<T>, options?: RetryOptions): Promise<T>;
|
|
71
|
+
export interface ResilienceOptions {
|
|
72
|
+
retry?: RetryOptions;
|
|
73
|
+
circuitBreaker?: CircuitBreaker;
|
|
74
|
+
/** If true, CircuitOpenError is not retried (default true). */
|
|
75
|
+
skipRetryOnOpenCircuit?: boolean;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Execute `fn` through optional circuit breaker + retry with exponential backoff.
|
|
79
|
+
*/
|
|
80
|
+
export declare function withResilience<T>(fn: (attempt: number) => Promise<T>, options?: ResilienceOptions): Promise<T>;
|
|
81
|
+
/** Heuristic: retry rate limits, timeouts, and common transient network errors. */
|
|
82
|
+
export declare function isTransientError(error: unknown): boolean;
|
|
83
|
+
export declare function getCircuitBreaker(name: string, options?: CircuitBreakerOptions): CircuitBreaker;
|
|
84
|
+
export declare function resetAllCircuitBreakers(): void;
|