@tpsdev-ai/agent 0.8.0 → 0.9.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/bun.lock +27 -15
- package/dist/bin.js +4 -3
- package/dist/bin.js.map +1 -1
- package/dist/config.d.ts +8 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +38 -3
- package/dist/config.js.map +1 -1
- package/dist/home.d.ts +3 -0
- package/dist/home.d.ts.map +1 -0
- package/dist/home.js +18 -0
- package/dist/home.js.map +1 -0
- package/dist/index.d.ts +16 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -1
- package/dist/index.js.map +1 -1
- package/dist/io/flair.d.ts +1 -10
- package/dist/io/flair.d.ts.map +1 -1
- package/dist/io/flair.js +29 -28
- package/dist/io/flair.js.map +1 -1
- package/dist/io/mail.d.ts +184 -30
- package/dist/io/mail.d.ts.map +1 -1
- package/dist/io/mail.js +944 -185
- package/dist/io/mail.js.map +1 -1
- package/dist/lib/agent-keys.d.ts +106 -0
- package/dist/lib/agent-keys.d.ts.map +1 -0
- package/dist/lib/agent-keys.js +298 -0
- package/dist/lib/agent-keys.js.map +1 -0
- package/dist/lib/bridge-identity.d.ts +10 -0
- package/dist/lib/bridge-identity.d.ts.map +1 -0
- package/dist/lib/bridge-identity.js +56 -0
- package/dist/lib/bridge-identity.js.map +1 -0
- package/dist/lib/mail-lock-namespace.d.ts +19 -0
- package/dist/lib/mail-lock-namespace.d.ts.map +1 -0
- package/dist/lib/mail-lock-namespace.js +58 -0
- package/dist/lib/mail-lock-namespace.js.map +1 -0
- package/dist/lib/mail-lock.d.ts +81 -0
- package/dist/lib/mail-lock.d.ts.map +1 -0
- package/dist/lib/mail-lock.js +319 -0
- package/dist/lib/mail-lock.js.map +1 -0
- package/dist/lib/mailbox-filename.d.ts +2 -0
- package/dist/lib/mailbox-filename.d.ts.map +1 -0
- package/dist/lib/mailbox-filename.js +4 -0
- package/dist/lib/mailbox-filename.js.map +1 -0
- package/dist/lib/mailbox-policy.d.ts +245 -0
- package/dist/lib/mailbox-policy.d.ts.map +1 -0
- package/dist/lib/mailbox-policy.js +949 -0
- package/dist/lib/mailbox-policy.js.map +1 -0
- package/dist/lib/public-key.d.ts +6 -0
- package/dist/lib/public-key.d.ts.map +1 -0
- package/dist/lib/public-key.js +45 -0
- package/dist/lib/public-key.js.map +1 -0
- package/dist/lib/registry-key.d.ts +2 -0
- package/dist/lib/registry-key.d.ts.map +1 -0
- package/dist/lib/registry-key.js +10 -0
- package/dist/lib/registry-key.js.map +1 -0
- package/dist/lib/relay-lock.d.ts +7 -0
- package/dist/lib/relay-lock.d.ts.map +1 -0
- package/dist/lib/relay-lock.js +19 -0
- package/dist/lib/relay-lock.js.map +1 -0
- package/dist/lib/signEnvelope.d.ts +2 -0
- package/dist/lib/signEnvelope.d.ts.map +1 -1
- package/dist/lib/signEnvelope.js.map +1 -1
- package/dist/lib/signing-key.d.ts +2 -0
- package/dist/lib/signing-key.d.ts.map +1 -0
- package/dist/lib/signing-key.js +9 -0
- package/dist/lib/signing-key.js.map +1 -0
- package/dist/lib/topic-recipient.d.ts +2 -0
- package/dist/lib/topic-recipient.d.ts.map +1 -0
- package/dist/lib/topic-recipient.js +2 -0
- package/dist/lib/topic-recipient.js.map +1 -0
- package/dist/llm/provider.d.ts.map +1 -1
- package/dist/llm/provider.js +9 -7
- package/dist/llm/provider.js.map +1 -1
- package/dist/runtime/agent.d.ts.map +1 -1
- package/dist/runtime/agent.js +32 -25
- package/dist/runtime/agent.js.map +1 -1
- package/dist/runtime/event-loop.d.ts +21 -2
- package/dist/runtime/event-loop.d.ts.map +1 -1
- package/dist/runtime/event-loop.js +199 -54
- package/dist/runtime/event-loop.js.map +1 -1
- package/dist/runtime/types.d.ts +11 -0
- package/dist/runtime/types.d.ts.map +1 -1
- package/dist/runtime/types.js +13 -1
- package/dist/runtime/types.js.map +1 -1
- package/package.json +2 -3
- package/src/bin.ts +4 -3
- package/src/config.ts +46 -3
- package/src/home.ts +18 -0
- package/src/index.ts +33 -2
- package/src/io/flair.ts +23 -28
- package/src/io/mail.ts +1007 -200
- package/src/lib/agent-keys.ts +308 -0
- package/src/lib/bridge-identity.ts +49 -0
- package/src/lib/mail-lock-namespace.ts +64 -0
- package/src/lib/mail-lock.ts +337 -0
- package/src/lib/mailbox-filename.ts +3 -0
- package/src/lib/mailbox-policy.ts +1085 -0
- package/src/lib/public-key.ts +49 -0
- package/src/lib/registry-key.ts +8 -0
- package/src/lib/relay-lock.ts +20 -0
- package/src/lib/signEnvelope.ts +2 -0
- package/src/lib/signing-key.ts +8 -0
- package/src/lib/topic-recipient.ts +1 -0
- package/src/llm/provider.ts +9 -7
- package/src/runtime/agent.ts +30 -27
- package/src/runtime/event-loop.ts +221 -68
- package/src/runtime/types.ts +14 -0
- package/test/config.test.ts +85 -2
- package/test/core-loop.test.ts +6 -4
- package/test/governance.test.ts +4 -1
- package/test/helpers/event-loop-child.mjs +50 -0
- package/test/helpers/mail-delivery-worker.mjs +16 -0
- package/test/helpers/mail-kill-child.mjs +104 -0
- package/test/helpers/signed-relay-record.ts +31 -0
- package/test/home-per-call.test.ts +72 -0
- package/test/io.test.ts +81 -14
- package/test/mail-client-lock-namespace.test.ts +155 -0
- package/test/mail-client-repoint-root.test.ts +368 -0
- package/test/mail-delivery-at-least-once.test.ts +1238 -0
- package/test/mail-event-loop-delivery.test.ts +769 -0
- package/test/mail-intent-phases.test.ts +304 -0
- package/test/mail-interrupted-recovery.test.ts +393 -0
- package/test/mail-lock-pid-state.test.ts +238 -0
- package/test/mail-lock.test.ts +96 -0
- package/test/mail-partial-flair.test.ts +102 -0
- package/test/mail-placement-replay.test.ts +116 -0
- package/test/mail-promote-guard.test.ts +305 -21
- package/test/mail-relay-reservation.test.ts +60 -0
- package/test/mailbox-filename.test.ts +16 -0
- package/test/public-key.test.ts +108 -0
- package/test/runtime-key-encoding.test.ts +105 -0
- package/test/runtime-pending-key.test.ts +117 -0
- package/test/security/mail-trust.test.ts +406 -109
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mail-lock.ts — a small inter-process lock for maildir mutation.
|
|
3
|
+
*
|
|
4
|
+
* Moved from the CLI for use by both first-delivery paths.
|
|
5
|
+
*
|
|
6
|
+
* Why it exists: the replay ledger is read-modify-written (prune) and appended,
|
|
7
|
+
* and the promotion moves files; without mutual exclusion, two concurrent
|
|
8
|
+
* promotions can both pass the replay gate before either records the id, and an
|
|
9
|
+
* append can land between a prune's read and its rename and be lost. The lock
|
|
10
|
+
* must span the whole replay-check → promotion-commit/rollback critical section,
|
|
11
|
+
* including the ledger prune and append.
|
|
12
|
+
*
|
|
13
|
+
* Properties:
|
|
14
|
+
* - normal acquisition publishes a populated lock directory by rename;
|
|
15
|
+
* - ownership uses pid and process start time when readable; a live pid with
|
|
16
|
+
* an unverifiable token is retained;
|
|
17
|
+
* - acquisition and stale reclamation share an atomic claim;
|
|
18
|
+
* - a stranded claim requires operator recovery;
|
|
19
|
+
* - release checks the acquisition nonce;
|
|
20
|
+
* - nested (re)acquisition in one process fails loudly rather than deadlocking;
|
|
21
|
+
* - an unreadable owner, or one whose pid is not an integer in 1..2147483647,
|
|
22
|
+
* can cause a contention timeout returning null; other filesystem errors may throw.
|
|
23
|
+
*
|
|
24
|
+
* The critical section a holder runs must be SYNCHRONOUS (no await while held):
|
|
25
|
+
* the in-process reentrancy guard tracks a plain set, and an await inside the
|
|
26
|
+
* critical section would let a second concurrent acquisition see the set
|
|
27
|
+
* populated and mistake concurrency for nesting.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import { lstatSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
31
|
+
import { execFileSync } from "node:child_process";
|
|
32
|
+
import { randomBytes } from "node:crypto";
|
|
33
|
+
import { join } from "node:path";
|
|
34
|
+
|
|
35
|
+
export const MAIL_LOCK_DIR = ".mail-lock";
|
|
36
|
+
const OWNER_FILE = "owner.json";
|
|
37
|
+
const TEMP_PREFIX = `${MAIL_LOCK_DIR}.tmp.`;
|
|
38
|
+
// Best-effort cleanup of old build-temps; age does not prove inactivity.
|
|
39
|
+
const TEMP_MAX_AGE_MS = 10 * 60 * 1000;
|
|
40
|
+
|
|
41
|
+
export interface MailLock {
|
|
42
|
+
/** Release the lock. Ownership-checked; safe to call once. */
|
|
43
|
+
release(): void;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The lock directory for a mailbox root. */
|
|
47
|
+
export function mailLockPath(root: string): string {
|
|
48
|
+
return join(root, MAIL_LOCK_DIR);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Roots currently held by THIS process (reentrancy guard). */
|
|
52
|
+
const heldByThisProcess = new Set<string>();
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Fields of `/proc/<pid>/stat` after the `comm` field (2), or null when the
|
|
56
|
+
* file is unreadable or contains no ')'; a truncated line yields fewer fields,
|
|
57
|
+
* which callers treat as absent. `comm` may contain spaces and parentheses,
|
|
58
|
+
* so the fields are split after the LAST ')'. fields[0] is field 3 (state);
|
|
59
|
+
* starttime is field 22 → fields[19].
|
|
60
|
+
*/
|
|
61
|
+
function readProcStatFields(pid: number): string[] | null {
|
|
62
|
+
try {
|
|
63
|
+
const stat = readFileSync(`/proc/${pid}/stat`, "utf-8");
|
|
64
|
+
const rparen = stat.lastIndexOf(")");
|
|
65
|
+
if (rparen === -1) return null;
|
|
66
|
+
return stat.slice(rparen + 2).split(" ");
|
|
67
|
+
} catch {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* A stable per-process identity token — its kernel birth time — or null only
|
|
74
|
+
* when it cannot be read at all.
|
|
75
|
+
*
|
|
76
|
+
* Two sources, so it is portable: `/proc/<pid>/stat` (Linux; field 22 is
|
|
77
|
+
* starttime), and `ps -o lstart= -p <pid>` (Darwin and Linux). Without the
|
|
78
|
+
* fallback the token is always null on Darwin, and owner identity degrades to
|
|
79
|
+
* pid alone — exactly the pid-reuse confusion this token exists to prevent.
|
|
80
|
+
*/
|
|
81
|
+
export function processStartToken(pid: number): string | null {
|
|
82
|
+
const fields = readProcStatFields(pid);
|
|
83
|
+
if (fields) {
|
|
84
|
+
const token = fields[19];
|
|
85
|
+
if (token) return `proc:${token}`;
|
|
86
|
+
}
|
|
87
|
+
// Portable fallback — `lstart` is a stable per-process birth time string.
|
|
88
|
+
try {
|
|
89
|
+
const out = execFileSync("ps", ["-o", "lstart=", "-p", String(pid)], {
|
|
90
|
+
encoding: "utf-8",
|
|
91
|
+
timeout: 2000,
|
|
92
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
93
|
+
});
|
|
94
|
+
const token = out.trim();
|
|
95
|
+
if (token) return `ps:${token}`;
|
|
96
|
+
} catch {
|
|
97
|
+
/* unreadable */
|
|
98
|
+
}
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Error codes taken as process.kill rejecting its argument; Bun and Node raise ERR_INVALID_ARG_TYPE for a pid above 2147483647. */
|
|
103
|
+
const PID_ARGUMENT_ERRORS = new Set(["ERR_INVALID_ARG_TYPE", "ERR_OUT_OF_RANGE", "EINVAL", "ERANGE"]);
|
|
104
|
+
|
|
105
|
+
export type PidState = "alive" | "gone" | "invalid";
|
|
106
|
+
|
|
107
|
+
/** The classifier's integer-and-positive precheck: an integer of at least 1.
|
|
108
|
+
* pidState can still classify a larger integer as invalid after process.kill
|
|
109
|
+
* rejects it as an argument (PID_ARGUMENT_ERRORS). */
|
|
110
|
+
export function isValidPid(pid: number): boolean {
|
|
111
|
+
return Number.isInteger(pid) && pid >= 1;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Probe a recorded pid with `process.kill(pid, 0)`. "invalid": not an integer
|
|
116
|
+
* of at least 1 (0 and negative pids do not name one process), or a pid
|
|
117
|
+
* process.kill rejects as an argument. "gone": ESRCH, or on Linux a pid whose
|
|
118
|
+
* `/proc/<pid>/stat` state is `Z` (exited but not reaped). "alive": the probe
|
|
119
|
+
* succeeds or fails with any other error, EPERM (permission denied) included;
|
|
120
|
+
* an unreadable stat file keeps "alive".
|
|
121
|
+
*/
|
|
122
|
+
export function pidState(pid: number, readStat: (pid: number) => string[] | null = readProcStatFields): PidState {
|
|
123
|
+
if (!isValidPid(pid)) return "invalid";
|
|
124
|
+
try {
|
|
125
|
+
process.kill(pid, 0);
|
|
126
|
+
if (process.platform === "linux" && readStat(pid)?.[0] === "Z") return "gone";
|
|
127
|
+
return "alive";
|
|
128
|
+
} catch (error) {
|
|
129
|
+
return pidStateFromProbeError(error);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** How pidState classifies an error thrown by `process.kill(pid, 0)`. */
|
|
134
|
+
export function pidStateFromProbeError(error: unknown): PidState {
|
|
135
|
+
const code = typeof error === "object" && error !== null ? (error as { code?: unknown }).code : undefined;
|
|
136
|
+
if (code === "ESRCH") return "gone";
|
|
137
|
+
if (typeof code === "string" && PID_ARGUMENT_ERRORS.has(code)) return "invalid";
|
|
138
|
+
return "alive";
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
interface LockOwner {
|
|
142
|
+
pid?: unknown;
|
|
143
|
+
startToken?: unknown;
|
|
144
|
+
nonce?: unknown;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
type OwnerState = "alive" | "dead" | "unverifiable";
|
|
148
|
+
|
|
149
|
+
/** The source component of a birth token (`proc:` / `ps:`), or the whole string. */
|
|
150
|
+
function tokenSource(token: string): string {
|
|
151
|
+
const i = token.indexOf(":");
|
|
152
|
+
return i === -1 ? token : token.slice(0, i);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function readOwner(lockDir: string): LockOwner | null {
|
|
156
|
+
try {
|
|
157
|
+
return JSON.parse(readFileSync(join(lockDir, OWNER_FILE), "utf-8")) as LockOwner;
|
|
158
|
+
} catch {
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Classify a lock's owner.
|
|
165
|
+
*
|
|
166
|
+
* "dead" — pid gone, or pid alive with a DIFFERENT token FROM THE SAME SOURCE.
|
|
167
|
+
* A token from a different source (`proc:` vs `ps:`) for the same live process
|
|
168
|
+
* is "unverifiable", NOT dead — treating it as dead would break a live owner's
|
|
169
|
+
* lock (two owners), the race this lock exists to prevent. Everything
|
|
170
|
+
* unverifiable is neither broken on age nor mistaken for dead.
|
|
171
|
+
*/
|
|
172
|
+
function ownerState(
|
|
173
|
+
owner: LockOwner | null,
|
|
174
|
+
resolveToken: (pid: number) => string | null = processStartToken,
|
|
175
|
+
): OwnerState {
|
|
176
|
+
if (!owner || typeof owner.pid !== "number") return "unverifiable";
|
|
177
|
+
// An invalid pid is handled as an unreadable owner record is.
|
|
178
|
+
const pid = pidState(owner.pid);
|
|
179
|
+
if (pid === "invalid") return "unverifiable";
|
|
180
|
+
if (pid === "gone") return "dead";
|
|
181
|
+
const token = resolveToken(owner.pid);
|
|
182
|
+
if (token !== null && typeof owner.startToken === "string") {
|
|
183
|
+
if (tokenSource(token) !== tokenSource(owner.startToken)) return "unverifiable";
|
|
184
|
+
return token === owner.startToken ? "alive" : "dead";
|
|
185
|
+
}
|
|
186
|
+
// pid is alive and we cannot disprove it — never break.
|
|
187
|
+
return "unverifiable";
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* cli#617 slice (b): is the process named by (pid, start token) provably gone?
|
|
192
|
+
* The same rule a lock owner is reclaimed by: the pid is gone, or it is alive
|
|
193
|
+
* with a different start token from the same source. A live pid whose identity
|
|
194
|
+
* cannot be checked is NOT gone.
|
|
195
|
+
*/
|
|
196
|
+
export function processIsGone(pid: number, startToken: string | null): boolean {
|
|
197
|
+
return ownerState({ pid, startToken }) === "dead";
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
async function sleep(ms: number): Promise<void> {
|
|
201
|
+
await new Promise((r) => setTimeout(r, ms));
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Attempt to reap old lock build-temps. */
|
|
205
|
+
function reapStrandedLockTemps(root: string): void {
|
|
206
|
+
let names: string[];
|
|
207
|
+
try {
|
|
208
|
+
names = readdirSync(root);
|
|
209
|
+
} catch {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
const cutoff = Date.now() - TEMP_MAX_AGE_MS;
|
|
213
|
+
for (const name of names) {
|
|
214
|
+
if (!name.startsWith(TEMP_PREFIX)) continue;
|
|
215
|
+
const p = join(root, name);
|
|
216
|
+
try {
|
|
217
|
+
if (statSync(p).mtimeMs > cutoff) continue; // may be an in-flight build
|
|
218
|
+
rmSync(p, { recursive: true, force: true });
|
|
219
|
+
} catch {
|
|
220
|
+
/* best effort */
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Acquire the mailbox lock, polling until `timeoutMs`. Returns null on timeout
|
|
227
|
+
* (caller must not proceed). Throws on nested acquisition for the same root.
|
|
228
|
+
*/
|
|
229
|
+
export async function acquireMailLock(
|
|
230
|
+
root: string,
|
|
231
|
+
opts: { timeoutMs?: number; pollMs?: number } = {},
|
|
232
|
+
): Promise<MailLock | null> {
|
|
233
|
+
const attempt = mailLockAttempts(root, opts);
|
|
234
|
+
for (;;) {
|
|
235
|
+
const step = attempt.next();
|
|
236
|
+
if (step.done) return step.value;
|
|
237
|
+
await sleep(step.value);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export function acquireMailLockSync(
|
|
242
|
+
root: string,
|
|
243
|
+
opts: { timeoutMs?: number; pollMs?: number } = {},
|
|
244
|
+
): MailLock | null {
|
|
245
|
+
const attempt = mailLockAttempts(root, opts);
|
|
246
|
+
const wait = new Int32Array(new SharedArrayBuffer(4));
|
|
247
|
+
for (;;) {
|
|
248
|
+
const step = attempt.next();
|
|
249
|
+
if (step.done) return step.value;
|
|
250
|
+
Atomics.wait(wait, 0, 0, step.value);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function* mailLockAttempts(
|
|
255
|
+
root: string,
|
|
256
|
+
opts: { timeoutMs?: number; pollMs?: number },
|
|
257
|
+
): Generator<number, MailLock | null, void> {
|
|
258
|
+
// `pollMs` overrides the default retry interval of 2 ms.
|
|
259
|
+
const { timeoutMs = 2000, pollMs = 2 } = opts;
|
|
260
|
+
const lockDir = mailLockPath(root);
|
|
261
|
+
|
|
262
|
+
if (heldByThisProcess.has(lockDir)) {
|
|
263
|
+
throw new Error(`mail lock already held by this process for ${root} (nested acquisition)`);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
mkdirSync(root, { recursive: true });
|
|
267
|
+
reapStrandedLockTemps(root);
|
|
268
|
+
const deadline = Date.now() + timeoutMs;
|
|
269
|
+
const myToken = processStartToken(process.pid);
|
|
270
|
+
const nonce = randomBytes(16).toString("hex");
|
|
271
|
+
const claimDir = join(root, `${MAIL_LOCK_DIR}.claim`);
|
|
272
|
+
|
|
273
|
+
const makeLock = (): MailLock => {
|
|
274
|
+
let released = false;
|
|
275
|
+
return {
|
|
276
|
+
release: () => {
|
|
277
|
+
if (released) return;
|
|
278
|
+
released = true;
|
|
279
|
+
releaseMailLock(lockDir, nonce);
|
|
280
|
+
},
|
|
281
|
+
};
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
for (;;) {
|
|
285
|
+
let claimed = false;
|
|
286
|
+
try {
|
|
287
|
+
try {
|
|
288
|
+
mkdirSync(claimDir);
|
|
289
|
+
claimed = true;
|
|
290
|
+
} catch (err: any) {
|
|
291
|
+
if (err?.code !== "EEXIST") throw err;
|
|
292
|
+
}
|
|
293
|
+
if (claimed) {
|
|
294
|
+
if (heldByThisProcess.has(lockDir)) throw new Error(`mail lock already held by this process for ${root}`);
|
|
295
|
+
let lockExists = true;
|
|
296
|
+
try {
|
|
297
|
+
lstatSync(lockDir);
|
|
298
|
+
} catch (err: any) {
|
|
299
|
+
if (err?.code !== "ENOENT") throw err;
|
|
300
|
+
lockExists = false;
|
|
301
|
+
}
|
|
302
|
+
if (lockExists && ownerState(readOwner(lockDir)) === "dead") {
|
|
303
|
+
rmSync(lockDir, { recursive: true });
|
|
304
|
+
lockExists = false;
|
|
305
|
+
}
|
|
306
|
+
if (!lockExists) {
|
|
307
|
+
const tmpDir = join(root, `${TEMP_PREFIX}${process.pid}.${nonce}`);
|
|
308
|
+
try {
|
|
309
|
+
mkdirSync(tmpDir);
|
|
310
|
+
writeFileSync(join(tmpDir, OWNER_FILE), JSON.stringify({ pid: process.pid, startToken: myToken, nonce }), "utf-8");
|
|
311
|
+
renameSync(tmpDir, lockDir);
|
|
312
|
+
heldByThisProcess.add(lockDir);
|
|
313
|
+
return makeLock();
|
|
314
|
+
} finally {
|
|
315
|
+
rmSync(tmpDir, { recursive: true, force: true });
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
} finally {
|
|
320
|
+
if (claimed) rmSync(claimDir, { recursive: true });
|
|
321
|
+
}
|
|
322
|
+
if (Date.now() >= deadline) return null;
|
|
323
|
+
yield pollMs;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function releaseMailLock(lockDir: string, nonce: string): void {
|
|
328
|
+
heldByThisProcess.delete(lockDir);
|
|
329
|
+
const owner = readOwner(lockDir);
|
|
330
|
+
if (owner && owner.pid === process.pid && owner.nonce === nonce) {
|
|
331
|
+
try {
|
|
332
|
+
rmSync(lockDir, { recursive: true, force: true });
|
|
333
|
+
} catch {
|
|
334
|
+
/* best effort */
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|