atabey-shared 0.0.37 → 0.1.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/README.md +64 -25
- package/dist/agent-identity.d.ts +1 -0
- package/dist/agent-identity.js +2 -0
- package/dist/agent-identity.js.map +1 -0
- package/dist/api-catalog.d.ts +1 -0
- package/dist/api-catalog.js +2 -0
- package/dist/api-catalog.js.map +1 -0
- package/dist/audit-chain.d.ts +1 -51
- package/dist/audit-chain.js +1 -84
- package/dist/audit-chain.js.map +1 -1
- package/dist/audit.d.ts +1 -113
- package/dist/audit.js +1 -284
- package/dist/audit.js.map +1 -1
- package/dist/constants.d.ts +1 -152
- package/dist/constants.js +1 -215
- package/dist/constants.js.map +1 -1
- package/dist/database.d.ts +1 -5
- package/dist/database.js +1 -12
- 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 -88
- package/dist/file-lock.js +1 -250
- 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 -22
- package/dist/index.js +2 -22
- package/dist/index.js.map +1 -1
- package/dist/job-queue.d.ts +1 -53
- package/dist/job-queue.js +1 -126
- 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 -30
- package/dist/logger.js +1 -126
- package/dist/logger.js.map +1 -1
- package/dist/memory-prune.d.ts +1 -0
- package/dist/memory-prune.js +2 -0
- package/dist/memory-prune.js.map +1 -0
- package/dist/migrations.d.ts +1 -0
- package/dist/migrations.js +2 -0
- package/dist/migrations.js.map +1 -0
- 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 -0
- package/dist/otel.js +2 -0
- package/dist/otel.js.map +1 -0
- package/dist/outbound-http.d.ts +1 -0
- package/dist/outbound-http.js +2 -0
- package/dist/outbound-http.js.map +1 -0
- package/dist/pii.d.ts +1 -69
- package/dist/pii.js +1 -219
- package/dist/pii.js.map +1 -1
- package/dist/policy-gate.d.ts +1 -42
- package/dist/policy-gate.js +1 -147
- 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 -63
- package/dist/retention.js +1 -170
- package/dist/retention.js.map +1 -1
- package/dist/sandbox-fs.d.ts +1 -28
- package/dist/sandbox-fs.js +1 -149
- package/dist/sandbox-fs.js.map +1 -1
- package/dist/sandbox-runtime.d.ts +1 -85
- package/dist/sandbox-runtime.js +1 -249
- 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 -0
- package/dist/secrets.js +2 -0
- package/dist/secrets.js.map +1 -0
- package/dist/storage.d.ts +1 -274
- package/dist/storage.js +1 -723
- 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 -30
- package/dist/time.js.map +1 -1
- package/dist/tls.d.ts +1 -0
- package/dist/tls.js +2 -0
- package/dist/tls.js.map +1 -0
- package/dist/tokenizer.d.ts +1 -0
- package/dist/tokenizer.js +2 -0
- package/dist/tokenizer.js.map +1 -0
- 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 -209
- 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 +51 -29
package/dist/job-queue.d.ts
CHANGED
|
@@ -1,53 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* In-Memory Job Queue — concurrency-limited async work dispatcher.
|
|
3
|
-
*
|
|
4
|
-
* Prevents unbounded parallel LLM / agent work from ballooning memory
|
|
5
|
-
* or starving the main event loop. No Redis / BullMQ required.
|
|
6
|
-
*
|
|
7
|
-
* Jobs run on the calling process (async functions). Pair with WorkerPool
|
|
8
|
-
* when true thread isolation is needed for CPU-bound work.
|
|
9
|
-
*/
|
|
10
|
-
export interface JobQueueOptions {
|
|
11
|
-
/** Max concurrent running jobs. Default: os.cpus().length (min 1, max 16). */
|
|
12
|
-
maxConcurrency?: number;
|
|
13
|
-
/** Optional name for diagnostics. */
|
|
14
|
-
name?: string;
|
|
15
|
-
/** Called when a job fails (after the promise rejects to the caller). */
|
|
16
|
-
onError?: (error: unknown, jobId: number) => void;
|
|
17
|
-
}
|
|
18
|
-
export interface JobQueueStats {
|
|
19
|
-
name: string;
|
|
20
|
-
maxConcurrency: number;
|
|
21
|
-
running: number;
|
|
22
|
-
pending: number;
|
|
23
|
-
completed: number;
|
|
24
|
-
failed: number;
|
|
25
|
-
}
|
|
26
|
-
export declare class InMemoryJobQueue {
|
|
27
|
-
private readonly maxConcurrency;
|
|
28
|
-
private readonly name;
|
|
29
|
-
private readonly onError?;
|
|
30
|
-
private readonly pending;
|
|
31
|
-
private running;
|
|
32
|
-
private nextId;
|
|
33
|
-
private completed;
|
|
34
|
-
private failed;
|
|
35
|
-
private closed;
|
|
36
|
-
constructor(options?: JobQueueOptions);
|
|
37
|
-
/**
|
|
38
|
-
* Enqueue an async job. Resolves/rejects with the job result.
|
|
39
|
-
* Back-pressures via wait in the queue — does not spawn unbounded work.
|
|
40
|
-
*/
|
|
41
|
-
enqueue<T>(fn: () => Promise<T>): Promise<T>;
|
|
42
|
-
/** Current queue diagnostics. */
|
|
43
|
-
getStats(): JobQueueStats;
|
|
44
|
-
/** Reject pending jobs and stop accepting new ones. In-flight jobs finish. */
|
|
45
|
-
close(reason?: string): void;
|
|
46
|
-
/** Wait until running + pending reach zero (or timeout). */
|
|
47
|
-
drain(timeoutMs?: number): Promise<void>;
|
|
48
|
-
private pump;
|
|
49
|
-
private execute;
|
|
50
|
-
}
|
|
51
|
-
export declare function getAgentJobQueue(options?: JobQueueOptions): InMemoryJobQueue;
|
|
52
|
-
/** Test helper — reset singleton. */
|
|
53
|
-
export declare function resetAgentJobQueue(): void;
|
|
1
|
+
export * from "./modules/concurrency/job-queue.js";
|
package/dist/job-queue.js
CHANGED
|
@@ -1,127 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
* In-Memory Job Queue — concurrency-limited async work dispatcher.
|
|
3
|
-
*
|
|
4
|
-
* Prevents unbounded parallel LLM / agent work from ballooning memory
|
|
5
|
-
* or starving the main event loop. No Redis / BullMQ required.
|
|
6
|
-
*
|
|
7
|
-
* Jobs run on the calling process (async functions). Pair with WorkerPool
|
|
8
|
-
* when true thread isolation is needed for CPU-bound work.
|
|
9
|
-
*/
|
|
10
|
-
import os from "os";
|
|
11
|
-
export class InMemoryJobQueue {
|
|
12
|
-
maxConcurrency;
|
|
13
|
-
name;
|
|
14
|
-
onError;
|
|
15
|
-
pending = [];
|
|
16
|
-
running = 0;
|
|
17
|
-
nextId = 1;
|
|
18
|
-
completed = 0;
|
|
19
|
-
failed = 0;
|
|
20
|
-
closed = false;
|
|
21
|
-
constructor(options = {}) {
|
|
22
|
-
const cpus = Math.max(1, os.cpus()?.length || 1);
|
|
23
|
-
const requested = options.maxConcurrency ?? cpus;
|
|
24
|
-
this.maxConcurrency = Math.max(1, Math.min(requested, 16));
|
|
25
|
-
this.name = options.name ?? "default";
|
|
26
|
-
this.onError = options.onError;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Enqueue an async job. Resolves/rejects with the job result.
|
|
30
|
-
* Back-pressures via wait in the queue — does not spawn unbounded work.
|
|
31
|
-
*/
|
|
32
|
-
enqueue(fn) {
|
|
33
|
-
if (this.closed) {
|
|
34
|
-
return Promise.reject(new Error(`[JobQueue:${this.name}] Queue is closed; cannot accept new jobs.`));
|
|
35
|
-
}
|
|
36
|
-
return new Promise((resolve, reject) => {
|
|
37
|
-
const item = {
|
|
38
|
-
id: this.nextId++,
|
|
39
|
-
run: fn,
|
|
40
|
-
resolve,
|
|
41
|
-
reject,
|
|
42
|
-
};
|
|
43
|
-
this.pending.push(item);
|
|
44
|
-
this.pump();
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
/** Current queue diagnostics. */
|
|
48
|
-
getStats() {
|
|
49
|
-
return {
|
|
50
|
-
name: this.name,
|
|
51
|
-
maxConcurrency: this.maxConcurrency,
|
|
52
|
-
running: this.running,
|
|
53
|
-
pending: this.pending.length,
|
|
54
|
-
completed: this.completed,
|
|
55
|
-
failed: this.failed,
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
/** Reject pending jobs and stop accepting new ones. In-flight jobs finish. */
|
|
59
|
-
close(reason = "Queue closed") {
|
|
60
|
-
this.closed = true;
|
|
61
|
-
while (this.pending.length > 0) {
|
|
62
|
-
const item = this.pending.shift();
|
|
63
|
-
item?.reject(new Error(`[JobQueue:${this.name}] ${reason}`));
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
/** Wait until running + pending reach zero (or timeout). */
|
|
67
|
-
async drain(timeoutMs = 30_000) {
|
|
68
|
-
const start = Date.now();
|
|
69
|
-
while (this.running > 0 || this.pending.length > 0) {
|
|
70
|
-
if (Date.now() - start > timeoutMs) {
|
|
71
|
-
throw new Error(`[JobQueue:${this.name}] drain timed out after ${timeoutMs}ms`);
|
|
72
|
-
}
|
|
73
|
-
await new Promise((r) => setTimeout(r, 10));
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
pump() {
|
|
77
|
-
while (this.running < this.maxConcurrency && this.pending.length > 0) {
|
|
78
|
-
const item = this.pending.shift();
|
|
79
|
-
if (!item)
|
|
80
|
-
break;
|
|
81
|
-
this.running++;
|
|
82
|
-
void this.execute(item);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
async execute(item) {
|
|
86
|
-
try {
|
|
87
|
-
const result = await item.run();
|
|
88
|
-
this.completed++;
|
|
89
|
-
item.resolve(result);
|
|
90
|
-
}
|
|
91
|
-
catch (error) {
|
|
92
|
-
this.failed++;
|
|
93
|
-
try {
|
|
94
|
-
this.onError?.(error, item.id);
|
|
95
|
-
}
|
|
96
|
-
catch {
|
|
97
|
-
// ignore listener errors
|
|
98
|
-
}
|
|
99
|
-
item.reject(error);
|
|
100
|
-
}
|
|
101
|
-
finally {
|
|
102
|
-
this.running--;
|
|
103
|
-
this.pump();
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
/** Shared singleton for agent orchestration (lazy). */
|
|
108
|
-
let agentJobQueue = null;
|
|
109
|
-
export function getAgentJobQueue(options) {
|
|
110
|
-
if (!agentJobQueue) {
|
|
111
|
-
const fromEnv = process.env.ATABEY_JOB_CONCURRENCY
|
|
112
|
-
? parseInt(process.env.ATABEY_JOB_CONCURRENCY, 10)
|
|
113
|
-
: undefined;
|
|
114
|
-
agentJobQueue = new InMemoryJobQueue({
|
|
115
|
-
name: "agent",
|
|
116
|
-
maxConcurrency: options?.maxConcurrency ?? (Number.isFinite(fromEnv) ? fromEnv : undefined),
|
|
117
|
-
onError: options?.onError,
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
return agentJobQueue;
|
|
121
|
-
}
|
|
122
|
-
/** Test helper — reset singleton. */
|
|
123
|
-
export function resetAgentJobQueue() {
|
|
124
|
-
agentJobQueue?.close("reset");
|
|
125
|
-
agentJobQueue = null;
|
|
126
|
-
}
|
|
1
|
+
export * from "./modules/concurrency/job-queue.js";
|
|
127
2
|
//# sourceMappingURL=job-queue.js.map
|
package/dist/job-queue.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"job-queue.js","sourceRoot":"","sources":["../src/job-queue.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"job-queue.js","sourceRoot":"","sources":["../src/job-queue.ts"],"names":[],"mappings":"AAAA,cAAc,oCAAoC,CAAC"}
|
package/dist/lock.d.ts
CHANGED
|
@@ -1,82 +1 @@
|
|
|
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
|
-
}
|
|
1
|
+
export * from "./modules/concurrency/lock.js";
|
package/dist/lock.js
CHANGED
|
@@ -1,159 +1,2 @@
|
|
|
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 "./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
|
-
}
|
|
1
|
+
export * from "./modules/concurrency/lock.js";
|
|
159
2
|
//# sourceMappingURL=lock.js.map
|
package/dist/lock.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lock.js","sourceRoot":"","sources":["../src/lock.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"lock.js","sourceRoot":"","sources":["../src/lock.ts"],"names":[],"mappings":"AAAA,cAAc,+BAA+B,CAAC"}
|
package/dist/logger.d.ts
CHANGED
|
@@ -1,30 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
DEBUG = 0,
|
|
3
|
-
INFO = 1,
|
|
4
|
-
WARN = 2,
|
|
5
|
-
ERROR = 3,
|
|
6
|
-
FATAL = 4
|
|
7
|
-
}
|
|
8
|
-
export interface LoggerConfig {
|
|
9
|
-
minLevel: LogLevel;
|
|
10
|
-
enableColors: boolean;
|
|
11
|
-
jsonFormat: boolean;
|
|
12
|
-
logFile?: string;
|
|
13
|
-
enablePiiMasking: boolean;
|
|
14
|
-
piiWarning: boolean;
|
|
15
|
-
}
|
|
16
|
-
export declare class EnterpriseLogger {
|
|
17
|
-
private config;
|
|
18
|
-
private static instance;
|
|
19
|
-
private constructor();
|
|
20
|
-
static getInstance(): EnterpriseLogger;
|
|
21
|
-
configure(config: Partial<LoggerConfig>): void;
|
|
22
|
-
private formatMessage;
|
|
23
|
-
private log;
|
|
24
|
-
debug(message: string, meta?: unknown): void;
|
|
25
|
-
info(message: string, meta?: unknown): void;
|
|
26
|
-
warn(message: string, meta?: unknown): void;
|
|
27
|
-
error(message: string, meta?: unknown): void;
|
|
28
|
-
fatal(message: string, meta?: unknown): void;
|
|
29
|
-
}
|
|
30
|
-
export declare const logger: EnterpriseLogger;
|
|
1
|
+
export * from "./modules/observability/logger.js";
|
package/dist/logger.js
CHANGED
|
@@ -1,127 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
import path from "path";
|
|
3
|
-
import { appendFile, ensureDir } from "./fs.js";
|
|
4
|
-
import { containsPII, maskObject, maskText } from "./pii.js";
|
|
5
|
-
export var LogLevel;
|
|
6
|
-
(function (LogLevel) {
|
|
7
|
-
LogLevel[LogLevel["DEBUG"] = 0] = "DEBUG";
|
|
8
|
-
LogLevel[LogLevel["INFO"] = 1] = "INFO";
|
|
9
|
-
LogLevel[LogLevel["WARN"] = 2] = "WARN";
|
|
10
|
-
LogLevel[LogLevel["ERROR"] = 3] = "ERROR";
|
|
11
|
-
LogLevel[LogLevel["FATAL"] = 4] = "FATAL";
|
|
12
|
-
})(LogLevel || (LogLevel = {}));
|
|
13
|
-
export class EnterpriseLogger {
|
|
14
|
-
config;
|
|
15
|
-
static instance;
|
|
16
|
-
constructor() {
|
|
17
|
-
const isProd = process.env.NODE_ENV === "production";
|
|
18
|
-
this.config = {
|
|
19
|
-
minLevel: isProd ? LogLevel.INFO : LogLevel.DEBUG,
|
|
20
|
-
enableColors: !isProd,
|
|
21
|
-
jsonFormat: isProd,
|
|
22
|
-
enablePiiMasking: true, // KVKK: PII masking ON by default
|
|
23
|
-
piiWarning: true,
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
static getInstance() {
|
|
27
|
-
if (!EnterpriseLogger.instance) {
|
|
28
|
-
EnterpriseLogger.instance = new EnterpriseLogger();
|
|
29
|
-
}
|
|
30
|
-
return EnterpriseLogger.instance;
|
|
31
|
-
}
|
|
32
|
-
configure(config) {
|
|
33
|
-
this.config = { ...this.config, ...config };
|
|
34
|
-
if (this.config.logFile) {
|
|
35
|
-
try {
|
|
36
|
-
ensureDir(path.dirname(this.config.logFile));
|
|
37
|
-
}
|
|
38
|
-
catch (err) {
|
|
39
|
-
// Directly write to stderr — using console here would be circular
|
|
40
|
-
process.stderr.write(`[Logger] Failed to create log directory: ${err}\n`);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
formatMessage(level, message, meta) {
|
|
45
|
-
const timestamp = new Date().toISOString();
|
|
46
|
-
const pid = process.pid;
|
|
47
|
-
// [KVKK] PII masking — mask sensitive data before logging
|
|
48
|
-
const safeMessage = this.config.enablePiiMasking ? maskText(message) : message;
|
|
49
|
-
const safeMeta = this.config.enablePiiMasking && meta ? maskObject(meta) : meta;
|
|
50
|
-
// [KVKK] Warn if PII detected in production
|
|
51
|
-
if (this.config.piiWarning && (containsPII(message) || (meta && containsPII(JSON.stringify(meta))))) {
|
|
52
|
-
process.stderr.write(`[PII WARNING] Potential PII detected in log at ${timestamp}\n`);
|
|
53
|
-
}
|
|
54
|
-
if (this.config.jsonFormat) {
|
|
55
|
-
return JSON.stringify({
|
|
56
|
-
timestamp,
|
|
57
|
-
level: LogLevel[level],
|
|
58
|
-
pid,
|
|
59
|
-
message: safeMessage,
|
|
60
|
-
meta: safeMeta,
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
const levelName = LogLevel[level].padEnd(5);
|
|
64
|
-
let coloredLevel = levelName;
|
|
65
|
-
if (this.config.enableColors) {
|
|
66
|
-
switch (level) {
|
|
67
|
-
case LogLevel.DEBUG:
|
|
68
|
-
coloredLevel = chalk.blue(levelName);
|
|
69
|
-
break;
|
|
70
|
-
case LogLevel.INFO:
|
|
71
|
-
coloredLevel = chalk.green(levelName);
|
|
72
|
-
break;
|
|
73
|
-
case LogLevel.WARN:
|
|
74
|
-
coloredLevel = chalk.yellow(levelName);
|
|
75
|
-
break;
|
|
76
|
-
case LogLevel.ERROR:
|
|
77
|
-
coloredLevel = chalk.red(levelName);
|
|
78
|
-
break;
|
|
79
|
-
case LogLevel.FATAL:
|
|
80
|
-
coloredLevel = chalk.bgRed.white.bold(levelName);
|
|
81
|
-
break;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
const metaStr = safeMeta ? ` | Meta: ${JSON.stringify(safeMeta)}` : "";
|
|
85
|
-
return `[${timestamp}] [PID:${pid}] [${coloredLevel}]: ${safeMessage}${metaStr}`;
|
|
86
|
-
}
|
|
87
|
-
log(level, message, meta) {
|
|
88
|
-
if (level < this.config.minLevel)
|
|
89
|
-
return;
|
|
90
|
-
const formatted = this.formatMessage(level, message, meta);
|
|
91
|
-
// Route to stderr for ERROR/FATAL, stdout otherwise.
|
|
92
|
-
// Using process.write directly here is intentional — this IS the logger.
|
|
93
|
-
if (level >= LogLevel.ERROR) {
|
|
94
|
-
process.stderr.write(formatted + "\n");
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
process.stdout.write(formatted + "\n");
|
|
98
|
-
}
|
|
99
|
-
// Output to file if configured
|
|
100
|
-
if (this.config.logFile) {
|
|
101
|
-
try {
|
|
102
|
-
appendFile(this.config.logFile, formatted + "\n");
|
|
103
|
-
}
|
|
104
|
-
catch (err) {
|
|
105
|
-
// Directly write to stderr — using console here would be circular
|
|
106
|
-
process.stderr.write(`[Logger] Failed to write to log file: ${err}\n`);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
debug(message, meta) {
|
|
111
|
-
this.log(LogLevel.DEBUG, message, meta);
|
|
112
|
-
}
|
|
113
|
-
info(message, meta) {
|
|
114
|
-
this.log(LogLevel.INFO, message, meta);
|
|
115
|
-
}
|
|
116
|
-
warn(message, meta) {
|
|
117
|
-
this.log(LogLevel.WARN, message, meta);
|
|
118
|
-
}
|
|
119
|
-
error(message, meta) {
|
|
120
|
-
this.log(LogLevel.ERROR, message, meta);
|
|
121
|
-
}
|
|
122
|
-
fatal(message, meta) {
|
|
123
|
-
this.log(LogLevel.FATAL, message, meta);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
export const logger = EnterpriseLogger.getInstance();
|
|
1
|
+
export * from "./modules/observability/logger.js";
|
|
127
2
|
//# sourceMappingURL=logger.js.map
|
package/dist/logger.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA,cAAc,mCAAmC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./modules/storage/memory-prune.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory-prune.js","sourceRoot":"","sources":["../src/memory-prune.ts"],"names":[],"mappings":"AAAA,cAAc,mCAAmC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./modules/storage/migrations.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrations.js","sourceRoot":"","sources":["../src/migrations.ts"],"names":[],"mappings":"AAAA,cAAc,iCAAiC,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent Identity — cryptographically signed agent tokens.
|
|
3
|
+
*
|
|
4
|
+
* Problem: `ATABEY_ACTIVE_AGENT` is a plain env var, so any tool/subprocess can
|
|
5
|
+
* impersonate `@manager` or `@security`. When `ATABEY_AGENT_SECRET` is set,
|
|
6
|
+
* Atabey accepts an HMAC-signed `ATABEY_AGENT_TOKEN` and verifies it
|
|
7
|
+
* (fail-closed on tampering/expiry). Set `ATABEY_AGENT_IDENTITY_REQUIRED=true`
|
|
8
|
+
* to refuse plain-env identity entirely.
|
|
9
|
+
*
|
|
10
|
+
* Token format: `${agent}.${issuedAt}.${expiresAt}.${hmacSha256Base64url}`
|
|
11
|
+
*/
|
|
12
|
+
export interface AgentIdentity {
|
|
13
|
+
agent: string;
|
|
14
|
+
issuedAt: number;
|
|
15
|
+
expiresAt: number;
|
|
16
|
+
}
|
|
17
|
+
export interface AgentTokenResult {
|
|
18
|
+
valid: boolean;
|
|
19
|
+
agent?: string;
|
|
20
|
+
reason?: string;
|
|
21
|
+
}
|
|
22
|
+
/** True when agent-token verification is mandatory (plain env rejected). */
|
|
23
|
+
export declare function isAgentIdentityEnforced(): boolean;
|
|
24
|
+
/** Sign an agent identity into a compact token. */
|
|
25
|
+
export declare function signAgentToken(agent: string, options?: {
|
|
26
|
+
ttlMs?: number;
|
|
27
|
+
key?: string | Buffer;
|
|
28
|
+
}): string;
|
|
29
|
+
/** Verify an agent token (signature + expiry). */
|
|
30
|
+
export declare function verifyAgentToken(token: string, options?: {
|
|
31
|
+
key?: string | Buffer;
|
|
32
|
+
}): AgentTokenResult;
|
|
33
|
+
/** Decode a token's identity without verification (for diagnostics only). */
|
|
34
|
+
export declare function decodeAgentToken(token: string): AgentIdentity | null;
|