atabey 0.1.1-beta → 0.1.2
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 +144 -78
- package/dist/atabey/src/cli/commands/coverage.js +17 -12
- package/dist/atabey/src/cli/commands/coverage.js.map +1 -1
- package/dist/atabey/src/cli/commands/lint.d.ts +2 -1
- package/dist/atabey/src/cli/commands/lint.js +20 -1
- package/dist/atabey/src/cli/commands/lint.js.map +1 -1
- package/dist/atabey/src/cli/commands/mcp.js +56 -23
- package/dist/atabey/src/cli/commands/mcp.js.map +1 -1
- package/dist/atabey/src/cli/commands/orchestrate.d.ts +6 -6
- package/dist/shared/src/agent-identity.d.ts +1 -34
- package/dist/shared/src/agent-identity.js +1 -76
- package/dist/shared/src/agent-identity.js.map +1 -1
- package/dist/shared/src/api-catalog.d.ts +1 -57
- package/dist/shared/src/api-catalog.js +1 -253
- package/dist/shared/src/api-catalog.js.map +1 -1
- package/dist/shared/src/audit-chain.d.ts +1 -80
- package/dist/shared/src/audit-chain.js +1 -261
- package/dist/shared/src/audit-chain.js.map +1 -1
- package/dist/shared/src/audit.d.ts +1 -121
- package/dist/shared/src/audit.js +1 -321
- package/dist/shared/src/audit.js.map +1 -1
- package/dist/shared/src/constants.d.ts +1 -150
- package/dist/shared/src/constants.js +1 -211
- package/dist/shared/src/constants.js.map +1 -1
- package/dist/shared/src/database.d.ts +1 -6
- package/dist/shared/src/database.js +1 -15
- package/dist/shared/src/database.js.map +1 -1
- package/dist/shared/src/errors.d.ts +1 -46
- package/dist/shared/src/errors.js +1 -68
- package/dist/shared/src/errors.js.map +1 -1
- package/dist/shared/src/file-lock.d.ts +1 -93
- package/dist/shared/src/file-lock.js +1 -239
- package/dist/shared/src/file-lock.js.map +1 -1
- package/dist/shared/src/fs.d.ts +1 -9
- package/dist/shared/src/fs.js +1 -53
- package/dist/shared/src/fs.js.map +1 -1
- package/dist/shared/src/index.d.ts +2 -31
- package/dist/shared/src/index.js +2 -31
- package/dist/shared/src/index.js.map +1 -1
- package/dist/shared/src/job-queue.d.ts +1 -76
- package/dist/shared/src/job-queue.js +1 -199
- package/dist/shared/src/job-queue.js.map +1 -1
- package/dist/shared/src/lock.d.ts +1 -82
- package/dist/shared/src/lock.js +1 -158
- package/dist/shared/src/lock.js.map +1 -1
- package/dist/shared/src/logger.d.ts +1 -31
- package/dist/shared/src/logger.js +1 -152
- package/dist/shared/src/logger.js.map +1 -1
- package/dist/shared/src/memory-prune.d.ts +1 -18
- package/dist/shared/src/memory-prune.js +1 -47
- package/dist/shared/src/memory-prune.js.map +1 -1
- package/dist/shared/src/migrations.d.ts +1 -69
- package/dist/shared/src/migrations.js +1 -393
- package/dist/shared/src/migrations.js.map +1 -1
- package/dist/shared/src/modules/agent/agent-identity.d.ts +34 -0
- package/dist/shared/src/modules/agent/agent-identity.js +77 -0
- package/dist/shared/src/modules/agent/agent-identity.js.map +1 -0
- package/dist/shared/src/modules/agent/api-catalog.d.ts +57 -0
- package/dist/shared/src/modules/agent/api-catalog.js +254 -0
- package/dist/shared/src/modules/agent/api-catalog.js.map +1 -0
- package/dist/shared/src/modules/agent/index.d.ts +3 -0
- package/dist/shared/src/modules/agent/index.js +4 -0
- package/dist/shared/src/modules/agent/index.js.map +1 -0
- package/dist/shared/src/modules/agent/tokenizer.d.ts +54 -0
- package/dist/shared/src/modules/agent/tokenizer.js +272 -0
- package/dist/shared/src/modules/agent/tokenizer.js.map +1 -0
- package/dist/shared/src/modules/concurrency/file-lock.d.ts +93 -0
- package/dist/shared/src/modules/concurrency/file-lock.js +240 -0
- package/dist/shared/src/modules/concurrency/file-lock.js.map +1 -0
- package/dist/shared/src/modules/concurrency/index.d.ts +6 -0
- package/dist/shared/src/modules/concurrency/index.js +7 -0
- package/dist/shared/src/modules/concurrency/index.js.map +1 -0
- package/dist/shared/src/modules/concurrency/job-queue.d.ts +76 -0
- package/dist/shared/src/modules/concurrency/job-queue.js +200 -0
- package/dist/shared/src/modules/concurrency/job-queue.js.map +1 -0
- package/dist/shared/src/modules/concurrency/lock.d.ts +82 -0
- package/dist/shared/src/modules/concurrency/lock.js +159 -0
- package/dist/shared/src/modules/concurrency/lock.js.map +1 -0
- package/dist/shared/src/modules/concurrency/resilience.d.ts +84 -0
- package/dist/shared/src/modules/concurrency/resilience.js +217 -0
- package/dist/shared/src/modules/concurrency/resilience.js.map +1 -0
- package/dist/shared/src/modules/concurrency/worker-pool.d.ts +52 -0
- package/dist/shared/src/modules/concurrency/worker-pool.js +229 -0
- package/dist/shared/src/modules/concurrency/worker-pool.js.map +1 -0
- package/dist/shared/src/modules/concurrency/worker-thread.d.ts +21 -0
- package/dist/shared/src/modules/concurrency/worker-thread.js +67 -0
- package/dist/shared/src/modules/concurrency/worker-thread.js.map +1 -0
- package/dist/shared/src/modules/index.d.ts +14 -0
- package/dist/shared/src/modules/index.js +15 -0
- package/dist/shared/src/modules/index.js.map +1 -0
- package/dist/shared/src/modules/observability/audit-chain.d.ts +80 -0
- package/dist/shared/src/modules/observability/audit-chain.js +262 -0
- package/dist/shared/src/modules/observability/audit-chain.js.map +1 -0
- package/dist/shared/src/modules/observability/audit.d.ts +121 -0
- package/dist/shared/src/modules/observability/audit.js +322 -0
- package/dist/shared/src/modules/observability/audit.js.map +1 -0
- package/dist/shared/src/modules/observability/index.d.ts +4 -0
- package/dist/shared/src/modules/observability/index.js +5 -0
- package/dist/shared/src/modules/observability/index.js.map +1 -0
- package/dist/shared/src/modules/observability/logger.d.ts +31 -0
- package/dist/shared/src/modules/observability/logger.js +153 -0
- package/dist/shared/src/modules/observability/logger.js.map +1 -0
- package/dist/shared/src/modules/observability/otel.d.ts +39 -0
- package/dist/shared/src/modules/observability/otel.js +93 -0
- package/dist/shared/src/modules/observability/otel.js.map +1 -0
- package/dist/shared/src/modules/primitives/constants.d.ts +150 -0
- package/dist/shared/src/modules/primitives/constants.js +212 -0
- package/dist/shared/src/modules/primitives/constants.js.map +1 -0
- package/dist/shared/src/modules/primitives/errors.d.ts +46 -0
- package/dist/shared/src/modules/primitives/errors.js +69 -0
- package/dist/shared/src/modules/primitives/errors.js.map +1 -0
- package/dist/shared/src/modules/primitives/fs.d.ts +9 -0
- package/dist/shared/src/modules/primitives/fs.js +54 -0
- package/dist/shared/src/modules/primitives/fs.js.map +1 -0
- package/dist/shared/src/modules/primitives/index.d.ts +7 -0
- package/dist/shared/src/modules/primitives/index.js +8 -0
- package/dist/shared/src/modules/primitives/index.js.map +1 -0
- package/dist/shared/src/modules/primitives/outbound-http.d.ts +32 -0
- package/dist/shared/src/modules/primitives/outbound-http.js +88 -0
- package/dist/shared/src/modules/primitives/outbound-http.js.map +1 -0
- package/dist/shared/src/modules/primitives/string.d.ts +1 -0
- package/dist/shared/src/modules/primitives/string.js +30 -0
- package/dist/shared/src/modules/primitives/string.js.map +1 -0
- package/dist/shared/src/modules/primitives/time.d.ts +5 -0
- package/dist/shared/src/modules/primitives/time.js +35 -0
- package/dist/shared/src/modules/primitives/time.js.map +1 -0
- package/dist/shared/src/modules/primitives/types.d.ts +27 -0
- package/dist/shared/src/modules/primitives/types.js +17 -0
- package/dist/shared/src/modules/primitives/types.js.map +1 -0
- package/dist/shared/src/modules/sandbox/index.d.ts +3 -0
- package/dist/shared/src/modules/sandbox/index.js +4 -0
- package/dist/shared/src/modules/sandbox/index.js.map +1 -0
- package/dist/shared/src/modules/sandbox/sandbox-fs.d.ts +28 -0
- package/dist/shared/src/modules/sandbox/sandbox-fs.js +143 -0
- package/dist/shared/src/modules/sandbox/sandbox-fs.js.map +1 -0
- package/dist/shared/src/modules/sandbox/sandbox-runtime.d.ts +78 -0
- package/dist/shared/src/modules/sandbox/sandbox-runtime.js +282 -0
- package/dist/shared/src/modules/sandbox/sandbox-runtime.js.map +1 -0
- package/dist/shared/src/modules/sandbox/sandbox.d.ts +54 -0
- package/dist/shared/src/modules/sandbox/sandbox.js +179 -0
- package/dist/shared/src/modules/sandbox/sandbox.js.map +1 -0
- package/dist/shared/src/modules/security/index.d.ts +5 -0
- package/dist/shared/src/modules/security/index.js +6 -0
- package/dist/shared/src/modules/security/index.js.map +1 -0
- package/dist/shared/src/modules/security/pii.d.ts +70 -0
- package/dist/shared/src/modules/security/pii.js +276 -0
- package/dist/shared/src/modules/security/pii.js.map +1 -0
- package/dist/shared/src/modules/security/policy-gate.d.ts +42 -0
- package/dist/shared/src/modules/security/policy-gate.js +172 -0
- package/dist/shared/src/modules/security/policy-gate.js.map +1 -0
- package/dist/shared/src/modules/security/retention.d.ts +68 -0
- package/dist/shared/src/modules/security/retention.js +218 -0
- package/dist/shared/src/modules/security/retention.js.map +1 -0
- package/dist/shared/src/modules/security/secrets.d.ts +99 -0
- package/dist/shared/src/modules/security/secrets.js +371 -0
- package/dist/shared/src/modules/security/secrets.js.map +1 -0
- package/dist/shared/src/modules/security/tls.d.ts +87 -0
- package/dist/shared/src/modules/security/tls.js +154 -0
- package/dist/shared/src/modules/security/tls.js.map +1 -0
- package/dist/shared/src/modules/storage/database.d.ts +6 -0
- package/dist/shared/src/modules/storage/database.js +16 -0
- package/dist/shared/src/modules/storage/database.js.map +1 -0
- package/dist/shared/src/modules/storage/index.d.ts +4 -0
- package/dist/shared/src/modules/storage/index.js +5 -0
- package/dist/shared/src/modules/storage/index.js.map +1 -0
- package/dist/shared/src/modules/storage/memory-prune.d.ts +18 -0
- package/dist/shared/src/modules/storage/memory-prune.js +48 -0
- package/dist/shared/src/modules/storage/memory-prune.js.map +1 -0
- package/dist/shared/src/modules/storage/migrations.d.ts +69 -0
- package/dist/shared/src/modules/storage/migrations.js +394 -0
- package/dist/shared/src/modules/storage/migrations.js.map +1 -0
- package/dist/shared/src/modules/storage/storage.d.ts +341 -0
- package/dist/shared/src/modules/storage/storage.js +1004 -0
- package/dist/shared/src/modules/storage/storage.js.map +1 -0
- package/dist/shared/src/otel.d.ts +1 -39
- package/dist/shared/src/otel.js +1 -92
- package/dist/shared/src/otel.js.map +1 -1
- package/dist/shared/src/outbound-http.d.ts +1 -32
- package/dist/shared/src/outbound-http.js +1 -87
- package/dist/shared/src/outbound-http.js.map +1 -1
- package/dist/shared/src/pii.d.ts +1 -70
- package/dist/shared/src/pii.js +1 -275
- package/dist/shared/src/pii.js.map +1 -1
- package/dist/shared/src/policy-gate.d.ts +1 -42
- package/dist/shared/src/policy-gate.js +1 -171
- package/dist/shared/src/policy-gate.js.map +1 -1
- package/dist/shared/src/resilience.d.ts +1 -84
- package/dist/shared/src/resilience.js +1 -216
- package/dist/shared/src/resilience.js.map +1 -1
- package/dist/shared/src/retention.d.ts +1 -68
- package/dist/shared/src/retention.js +1 -217
- package/dist/shared/src/retention.js.map +1 -1
- package/dist/shared/src/sandbox-fs.d.ts +1 -28
- package/dist/shared/src/sandbox-fs.js +1 -142
- package/dist/shared/src/sandbox-fs.js.map +1 -1
- package/dist/shared/src/sandbox-runtime.d.ts +1 -78
- package/dist/shared/src/sandbox-runtime.js +1 -281
- package/dist/shared/src/sandbox-runtime.js.map +1 -1
- package/dist/shared/src/sandbox.d.ts +1 -54
- package/dist/shared/src/sandbox.js +1 -178
- package/dist/shared/src/sandbox.js.map +1 -1
- package/dist/shared/src/secrets.d.ts +1 -99
- package/dist/shared/src/secrets.js +1 -370
- package/dist/shared/src/secrets.js.map +1 -1
- package/dist/shared/src/storage.d.ts +1 -341
- package/dist/shared/src/storage.js +1 -1003
- package/dist/shared/src/storage.js.map +1 -1
- package/dist/shared/src/string.d.ts +1 -1
- package/dist/shared/src/string.js +1 -29
- package/dist/shared/src/string.js.map +1 -1
- package/dist/shared/src/time.d.ts +1 -5
- package/dist/shared/src/time.js +1 -34
- package/dist/shared/src/time.js.map +1 -1
- package/dist/shared/src/tls.d.ts +1 -87
- package/dist/shared/src/tls.js +1 -153
- package/dist/shared/src/tls.js.map +1 -1
- package/dist/shared/src/tokenizer.d.ts +1 -54
- package/dist/shared/src/tokenizer.js +1 -271
- package/dist/shared/src/tokenizer.js.map +1 -1
- package/dist/shared/src/types.d.ts +1 -27
- package/dist/shared/src/types.js +1 -16
- package/dist/shared/src/types.js.map +1 -1
- package/dist/shared/src/worker-pool.d.ts +1 -52
- package/dist/shared/src/worker-pool.js +1 -228
- package/dist/shared/src/worker-pool.js.map +1 -1
- package/dist/shared/src/worker-thread.d.ts +1 -21
- package/dist/shared/src/worker-thread.js +1 -66
- package/dist/shared/src/worker-thread.js.map +1 -1
- package/package.json +4 -4
|
@@ -1,240 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
* Atomic File-Based Locking — OS-level exclusive create via open('wx').
|
|
3
|
-
*
|
|
4
|
-
* Replaces check-then-act lock races. No Redis/Redlock required.
|
|
5
|
-
*
|
|
6
|
-
* Pattern:
|
|
7
|
-
* resource → .atabey/locks/<relative>.lock (created with O_EXCL / 'wx')
|
|
8
|
-
* holder writes JSON metadata after exclusive create
|
|
9
|
-
* release unlinks the lock file
|
|
10
|
-
*
|
|
11
|
-
* Stale locks (TTL exceeded) are reclaimed atomically when possible.
|
|
12
|
-
*/
|
|
13
|
-
import fs from "fs";
|
|
14
|
-
import os from "os";
|
|
15
|
-
import path from "path";
|
|
16
|
-
export class AtomicFileLock {
|
|
17
|
-
locksDir;
|
|
18
|
-
projectRoot;
|
|
19
|
-
ttlMs;
|
|
20
|
-
retryMs;
|
|
21
|
-
timeoutMs;
|
|
22
|
-
constructor(options = {}) {
|
|
23
|
-
this.projectRoot = options.projectRoot ?? process.cwd();
|
|
24
|
-
this.locksDir = options.locksDir
|
|
25
|
-
? path.resolve(options.locksDir)
|
|
26
|
-
: path.join(this.projectRoot, ".atabey", "locks");
|
|
27
|
-
this.ttlMs = options.ttlMs ?? 5 * 60 * 1000;
|
|
28
|
-
this.retryMs = options.retryMs ?? 50;
|
|
29
|
-
this.timeoutMs = options.timeoutMs ?? 10_000;
|
|
30
|
-
}
|
|
31
|
-
/** Absolute path of the lock file for a resource. */
|
|
32
|
-
getLockPath(resource) {
|
|
33
|
-
const abs = path.isAbsolute(resource)
|
|
34
|
-
? resource
|
|
35
|
-
: path.resolve(this.projectRoot, resource);
|
|
36
|
-
let relative = path.relative(this.projectRoot, abs);
|
|
37
|
-
if (relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) {
|
|
38
|
-
// Outside project root — hash path to avoid escaping locks dir
|
|
39
|
-
relative = `_ext/${Buffer.from(abs).toString("base64url")}`;
|
|
40
|
-
}
|
|
41
|
-
// Normalize separators for cross-platform lock paths
|
|
42
|
-
relative = relative.split(path.sep).join("/");
|
|
43
|
-
return path.join(this.locksDir, `${relative}.lock`);
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Try to acquire a lock once (non-blocking).
|
|
47
|
-
* Uses atomic exclusive file create (`wx` = O_CREAT | O_EXCL | O_WRONLY).
|
|
48
|
-
*/
|
|
49
|
-
tryAcquire(resource, ownerId, reason, _reclaimDepth = 0) {
|
|
50
|
-
const lockPath = this.getLockPath(resource);
|
|
51
|
-
fs.mkdirSync(path.dirname(lockPath), { recursive: true });
|
|
52
|
-
// Reclaim stale lock first (best-effort, still atomic on create)
|
|
53
|
-
this.reclaimIfStale(lockPath);
|
|
54
|
-
const now = Date.now();
|
|
55
|
-
const meta = {
|
|
56
|
-
ownerId,
|
|
57
|
-
resource,
|
|
58
|
-
acquiredAt: new Date(now).toISOString(),
|
|
59
|
-
expiresAt: new Date(now + this.ttlMs).toISOString(),
|
|
60
|
-
reason,
|
|
61
|
-
pid: process.pid,
|
|
62
|
-
hostname: os.hostname(),
|
|
63
|
-
};
|
|
64
|
-
try {
|
|
65
|
-
// Atomic exclusive create WITH content in one syscall sequence
|
|
66
|
-
// (writeFileSync + flag 'wx' = O_CREAT|O_EXCL|O_WRONLY): there is no
|
|
67
|
-
// window where the lock file exists empty for a reader to misread.
|
|
68
|
-
fs.writeFileSync(lockPath, JSON.stringify(meta, null, 2), { encoding: "utf8", flag: "wx" });
|
|
69
|
-
return { acquired: true, meta };
|
|
70
|
-
}
|
|
71
|
-
catch (err) {
|
|
72
|
-
const code = err.code;
|
|
73
|
-
if (code === "EEXIST") {
|
|
74
|
-
// Another process holds it. Reclaim only if corrupt/empty or expired.
|
|
75
|
-
const heldBy = this.readMeta(lockPath) ?? undefined;
|
|
76
|
-
if ((!heldBy || this.isExpired(heldBy)) && _reclaimDepth < 1) {
|
|
77
|
-
this.forceRemove(lockPath, heldBy ?? null);
|
|
78
|
-
return this.tryAcquire(resource, ownerId, reason, _reclaimDepth + 1);
|
|
79
|
-
}
|
|
80
|
-
return { acquired: false, heldBy };
|
|
81
|
-
}
|
|
82
|
-
throw err;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Acquire with async retry until timeout.
|
|
87
|
-
*/
|
|
88
|
-
async acquireAsync(resource, ownerId, options) {
|
|
89
|
-
const timeoutMs = options?.timeoutMs ?? this.timeoutMs;
|
|
90
|
-
const retryMs = options?.retryMs ?? this.retryMs;
|
|
91
|
-
const start = Date.now();
|
|
92
|
-
while (true) {
|
|
93
|
-
const result = this.tryAcquire(resource, ownerId, options?.reason);
|
|
94
|
-
if (result.acquired && result.meta) {
|
|
95
|
-
return result.meta;
|
|
96
|
-
}
|
|
97
|
-
if (Date.now() - start >= timeoutMs) {
|
|
98
|
-
const holder = result.heldBy?.ownerId ?? "unknown";
|
|
99
|
-
throw new Error(`[AtomicFileLock] Timeout acquiring lock for "${resource}" after ${timeoutMs}ms (held by ${holder})`);
|
|
100
|
-
}
|
|
101
|
-
await sleep(retryMs);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Release lock only if owned by ownerId (or force=true).
|
|
106
|
-
*/
|
|
107
|
-
release(resource, ownerId, force = false) {
|
|
108
|
-
const lockPath = this.getLockPath(resource);
|
|
109
|
-
if (!fs.existsSync(lockPath))
|
|
110
|
-
return true;
|
|
111
|
-
const meta = this.readMeta(lockPath);
|
|
112
|
-
if (!meta) {
|
|
113
|
-
// Corrupt/empty lock — safe to drop (atomic create means a real
|
|
114
|
-
// holder always has readable meta).
|
|
115
|
-
this.forceRemove(lockPath, null);
|
|
116
|
-
return true;
|
|
117
|
-
}
|
|
118
|
-
if (!force && meta.ownerId !== ownerId) {
|
|
119
|
-
return false;
|
|
120
|
-
}
|
|
121
|
-
this.forceRemove(lockPath, meta);
|
|
122
|
-
return true;
|
|
123
|
-
}
|
|
124
|
-
isLocked(resource) {
|
|
125
|
-
const lockPath = this.getLockPath(resource);
|
|
126
|
-
if (!fs.existsSync(lockPath))
|
|
127
|
-
return null;
|
|
128
|
-
const meta = this.readMeta(lockPath);
|
|
129
|
-
if (!meta)
|
|
130
|
-
return null;
|
|
131
|
-
if (this.isExpired(meta)) {
|
|
132
|
-
this.forceRemove(lockPath, meta);
|
|
133
|
-
return null;
|
|
134
|
-
}
|
|
135
|
-
return meta;
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Run `fn` while holding the lock.
|
|
139
|
-
* ALWAYS releases in `finally` even if `fn` throws (disk full, parse errors, etc.).
|
|
140
|
-
* Without this, a leftover `.lock` file would deadlock every subsequent writer until TTL.
|
|
141
|
-
*
|
|
142
|
-
* Prefer this API over bare acquire/release for any mutating work.
|
|
143
|
-
*/
|
|
144
|
-
async withLock(resource, ownerId, fn, options) {
|
|
145
|
-
await this.acquireAsync(resource, ownerId, options);
|
|
146
|
-
try {
|
|
147
|
-
return await fn();
|
|
148
|
-
}
|
|
149
|
-
finally {
|
|
150
|
-
// Never let release errors mask the original failure or leave a lock.
|
|
151
|
-
try {
|
|
152
|
-
this.release(resource, ownerId);
|
|
153
|
-
}
|
|
154
|
-
catch {
|
|
155
|
-
this.forceRemove(this.getLockPath(resource), null);
|
|
156
|
-
}
|
|
157
|
-
// Belt-and-suspenders: if our lock is somehow still present, force drop it.
|
|
158
|
-
const stillHeld = this.isLocked(resource);
|
|
159
|
-
if (stillHeld && stillHeld.ownerId === ownerId) {
|
|
160
|
-
this.forceRemove(this.getLockPath(resource), stillHeld);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* Synchronous withLock — same try/finally release guarantee.
|
|
166
|
-
*/
|
|
167
|
-
withLockSync(resource, ownerId, fn, options) {
|
|
168
|
-
const result = this.tryAcquire(resource, ownerId, options?.reason);
|
|
169
|
-
if (!result.acquired) {
|
|
170
|
-
const holder = result.heldBy?.ownerId ?? "unknown";
|
|
171
|
-
throw new Error(`[AtomicFileLock] Could not acquire lock for "${resource}" (held by ${holder})`);
|
|
172
|
-
}
|
|
173
|
-
try {
|
|
174
|
-
return fn();
|
|
175
|
-
}
|
|
176
|
-
finally {
|
|
177
|
-
try {
|
|
178
|
-
this.release(resource, ownerId);
|
|
179
|
-
}
|
|
180
|
-
catch {
|
|
181
|
-
this.forceRemove(this.getLockPath(resource), null);
|
|
182
|
-
}
|
|
183
|
-
const stillHeld = this.isLocked(resource);
|
|
184
|
-
if (stillHeld && stillHeld.ownerId === ownerId) {
|
|
185
|
-
this.forceRemove(this.getLockPath(resource), stillHeld);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
reclaimIfStale(lockPath) {
|
|
190
|
-
if (!fs.existsSync(lockPath))
|
|
191
|
-
return;
|
|
192
|
-
if (this.isReclaimable(lockPath)) {
|
|
193
|
-
this.forceRemove(lockPath, this.readMeta(lockPath));
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* True when an existing lock file may be safely stolen: unparseable/empty
|
|
198
|
-
* (corrupt, e.g. crashed mid-write) or past its TTL.
|
|
199
|
-
*/
|
|
200
|
-
isReclaimable(lockPath) {
|
|
201
|
-
const meta = this.readMeta(lockPath);
|
|
202
|
-
if (!meta)
|
|
203
|
-
return true;
|
|
204
|
-
return this.isExpired(meta);
|
|
205
|
-
}
|
|
206
|
-
isExpired(meta) {
|
|
207
|
-
const exp = Date.parse(meta.expiresAt);
|
|
208
|
-
if (Number.isNaN(exp)) {
|
|
209
|
-
// Fallback: acquiredAt + ttl
|
|
210
|
-
const acq = Date.parse(meta.acquiredAt);
|
|
211
|
-
return Number.isNaN(acq) ? true : Date.now() - acq > this.ttlMs;
|
|
212
|
-
}
|
|
213
|
-
return Date.now() > exp;
|
|
214
|
-
}
|
|
215
|
-
readMeta(lockPath) {
|
|
216
|
-
try {
|
|
217
|
-
const raw = fs.readFileSync(lockPath, "utf8");
|
|
218
|
-
return JSON.parse(raw);
|
|
219
|
-
}
|
|
220
|
-
catch {
|
|
221
|
-
return null;
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
forceRemove(lockPath, _meta) {
|
|
225
|
-
try {
|
|
226
|
-
fs.unlinkSync(lockPath);
|
|
227
|
-
}
|
|
228
|
-
catch {
|
|
229
|
-
// ignore ENOENT races
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
function sleep(ms) {
|
|
234
|
-
return new Promise((r) => setTimeout(r, ms));
|
|
235
|
-
}
|
|
236
|
-
/** Convenience: lock under project `.atabey/locks`. */
|
|
237
|
-
export function createProjectFileLock(projectRoot, options) {
|
|
238
|
-
return new AtomicFileLock({ ...options, projectRoot });
|
|
239
|
-
}
|
|
1
|
+
export * from "./modules/concurrency/file-lock.js";
|
|
240
2
|
//# sourceMappingURL=file-lock.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"file-lock.js","sourceRoot":"","sources":["../../../../shared/src/file-lock.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"file-lock.js","sourceRoot":"","sources":["../../../../shared/src/file-lock.ts"],"names":[],"mappings":"AAAA,cAAc,oCAAoC,CAAC"}
|
package/dist/shared/src/fs.d.ts
CHANGED
|
@@ -1,9 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
/**
|
|
3
|
-
* Writes content to a file atomically by using a temporary file.
|
|
4
|
-
* This prevents data corruption during unexpected system failures.
|
|
5
|
-
*/
|
|
6
|
-
export declare function writeTextFile(filePath: string, content: string, dryRun?: boolean): void;
|
|
7
|
-
export declare function appendFile(filePath: string, content: string, dryRun?: boolean): void;
|
|
8
|
-
export declare function writeJsonFile(filePath: string, value: unknown, dryRun?: boolean): void;
|
|
9
|
-
export declare function runCommandQuiet(command: string, args: string[], cwd: string): void;
|
|
1
|
+
export * from "./modules/primitives/fs.js";
|
package/dist/shared/src/fs.js
CHANGED
|
@@ -1,54 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
import fs from "fs";
|
|
3
|
-
import path from "path";
|
|
4
|
-
import { logger } from "./logger.js";
|
|
5
|
-
export function ensureDir(dirPath, dryRun = false) {
|
|
6
|
-
if (!fs.existsSync(dirPath)) {
|
|
7
|
-
if (dryRun) {
|
|
8
|
-
logger.info(`[DRY RUN] Would create directory: ${dirPath}`);
|
|
9
|
-
}
|
|
10
|
-
else {
|
|
11
|
-
fs.mkdirSync(dirPath, { recursive: true });
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Writes content to a file atomically by using a temporary file.
|
|
17
|
-
* This prevents data corruption during unexpected system failures.
|
|
18
|
-
*/
|
|
19
|
-
export function writeTextFile(filePath, content, dryRun = false) {
|
|
20
|
-
if (dryRun) {
|
|
21
|
-
logger.info(`[DRY RUN] Would write file: ${filePath}`);
|
|
22
|
-
return;
|
|
23
|
-
}
|
|
24
|
-
const dir = path.dirname(filePath);
|
|
25
|
-
ensureDir(dir);
|
|
26
|
-
const tempPath = `${filePath}.${Math.random().toString(36).slice(2, 9)}.tmp`;
|
|
27
|
-
const finalContent = content.endsWith("\n") ? content : `${content}\n`;
|
|
28
|
-
try {
|
|
29
|
-
fs.writeFileSync(tempPath, finalContent, "utf8");
|
|
30
|
-
fs.renameSync(tempPath, filePath);
|
|
31
|
-
}
|
|
32
|
-
catch (err) {
|
|
33
|
-
if (fs.existsSync(tempPath))
|
|
34
|
-
fs.unlinkSync(tempPath);
|
|
35
|
-
throw err;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
export function appendFile(filePath, content, dryRun = false) {
|
|
39
|
-
if (dryRun) {
|
|
40
|
-
logger.info(`[DRY RUN] Would append to file: ${filePath}`);
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
ensureDir(path.dirname(filePath));
|
|
44
|
-
// Atomic append: oku → ekle → atomic yaz
|
|
45
|
-
const existing = fs.existsSync(filePath) ? fs.readFileSync(filePath, "utf8") : "";
|
|
46
|
-
writeTextFile(filePath, existing + content);
|
|
47
|
-
}
|
|
48
|
-
export function writeJsonFile(filePath, value, dryRun = false) {
|
|
49
|
-
writeTextFile(filePath, JSON.stringify(value, null, 2), dryRun);
|
|
50
|
-
}
|
|
51
|
-
export function runCommandQuiet(command, args, cwd) {
|
|
52
|
-
execFileSync(command, args, { cwd, stdio: "ignore" });
|
|
53
|
-
}
|
|
1
|
+
export * from "./modules/primitives/fs.js";
|
|
54
2
|
//# sourceMappingURL=fs.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fs.js","sourceRoot":"","sources":["../../../../shared/src/fs.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"fs.js","sourceRoot":"","sources":["../../../../shared/src/fs.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAC"}
|
|
@@ -1,31 +1,2 @@
|
|
|
1
|
-
export * from "./
|
|
2
|
-
export * from "./
|
|
3
|
-
export * from "./lock.js";
|
|
4
|
-
export * from "./audit.js";
|
|
5
|
-
export * from "./audit-chain.js";
|
|
6
|
-
export * from "./logger.js";
|
|
7
|
-
export * from "./retention.js";
|
|
8
|
-
export * from "./errors.js";
|
|
9
|
-
export * from "./constants.js";
|
|
10
|
-
export * from "./string.js";
|
|
11
|
-
export * from "./types.js";
|
|
12
|
-
export * from "./fs.js";
|
|
13
|
-
export * from "./job-queue.js";
|
|
14
|
-
export * from "./worker-pool.js";
|
|
15
|
-
export * from "./file-lock.js";
|
|
16
|
-
export * from "./sandbox.js";
|
|
17
|
-
export * from "./sandbox-runtime.js";
|
|
18
|
-
export * from "./sandbox-fs.js";
|
|
19
|
-
export * from "./resilience.js";
|
|
20
|
-
export * from "./policy-gate.js";
|
|
21
|
-
export * from "./storage.js";
|
|
22
|
-
export * from "./time.js";
|
|
23
|
-
export * from "./secrets.js";
|
|
24
|
-
export * from "./tokenizer.js";
|
|
25
|
-
export * from "./migrations.js";
|
|
26
|
-
export * from "./tls.js";
|
|
27
|
-
export * from "./outbound-http.js";
|
|
28
|
-
export * from "./api-catalog.js";
|
|
29
|
-
export * from "./agent-identity.js";
|
|
30
|
-
export * from "./memory-prune.js";
|
|
31
|
-
export * from "./otel.js";
|
|
1
|
+
export * from "./modules/index.js";
|
|
2
|
+
export * as Modules from "./modules/index.js";
|
package/dist/shared/src/index.js
CHANGED
|
@@ -1,32 +1,3 @@
|
|
|
1
|
-
export * from "./
|
|
2
|
-
export * from "./
|
|
3
|
-
export * from "./lock.js";
|
|
4
|
-
export * from "./audit.js";
|
|
5
|
-
export * from "./audit-chain.js";
|
|
6
|
-
export * from "./logger.js";
|
|
7
|
-
export * from "./retention.js";
|
|
8
|
-
export * from "./errors.js";
|
|
9
|
-
export * from "./constants.js";
|
|
10
|
-
export * from "./string.js";
|
|
11
|
-
export * from "./types.js";
|
|
12
|
-
export * from "./fs.js";
|
|
13
|
-
export * from "./job-queue.js";
|
|
14
|
-
export * from "./worker-pool.js";
|
|
15
|
-
export * from "./file-lock.js";
|
|
16
|
-
export * from "./sandbox.js";
|
|
17
|
-
export * from "./sandbox-runtime.js";
|
|
18
|
-
export * from "./sandbox-fs.js";
|
|
19
|
-
export * from "./resilience.js";
|
|
20
|
-
export * from "./policy-gate.js";
|
|
21
|
-
export * from "./storage.js";
|
|
22
|
-
export * from "./time.js";
|
|
23
|
-
export * from "./secrets.js";
|
|
24
|
-
export * from "./tokenizer.js";
|
|
25
|
-
export * from "./migrations.js";
|
|
26
|
-
export * from "./tls.js";
|
|
27
|
-
export * from "./outbound-http.js";
|
|
28
|
-
export * from "./api-catalog.js";
|
|
29
|
-
export * from "./agent-identity.js";
|
|
30
|
-
export * from "./memory-prune.js";
|
|
31
|
-
export * from "./otel.js";
|
|
1
|
+
export * from "./modules/index.js";
|
|
2
|
+
export * as Modules from "./modules/index.js";
|
|
32
3
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../shared/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../shared/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,OAAO,KAAK,OAAO,MAAM,oBAAoB,CAAC"}
|
|
@@ -1,76 +1 @@
|
|
|
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;
|
|
1
|
+
export * from "./modules/concurrency/job-queue.js";
|