@myagentroam/agent 0.13.10 → 0.13.11
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.
|
@@ -32,7 +32,9 @@ export async function openJsonlIndex(root) {
|
|
|
32
32
|
index = disk;
|
|
33
33
|
else {
|
|
34
34
|
const entries = await readdir(directory, { withFileTypes: true });
|
|
35
|
-
if (entries.some((entry) => entry.isDirectory()
|
|
35
|
+
if (entries.some((entry) => entry.isDirectory() &&
|
|
36
|
+
entry.name !== 'index.lock' &&
|
|
37
|
+
!entry.name.startsWith('index.lock.pending-')))
|
|
36
38
|
throw new AgentError('MAR_AGENT_SESSION_CORRUPT', 'Session index is missing.');
|
|
37
39
|
index = { version: 2, revision: 0, sessions: [] };
|
|
38
40
|
await persistFormatSelection(path, index);
|
|
@@ -1,35 +1,40 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { mkdtemp, readFile, readdir, rename, rm, rmdir, unlink, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { join } from 'node:path';
|
|
2
4
|
import { setTimeout as delay } from 'node:timers/promises';
|
|
3
5
|
import { AgentError } from '../error.js';
|
|
6
|
+
/** Own one nonempty lock directory; stale cleanup never removes another incarnation's owner. */
|
|
4
7
|
export async function acquireFileLock(path, message = 'Session index is busy.', waitMs = 5000, options = {}) {
|
|
5
8
|
const deadline = Date.now() + waitMs;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
9
|
+
options.signal?.throwIfAborted();
|
|
10
|
+
const pending = await mkdtemp(`${path}.pending-`);
|
|
11
|
+
const owner = randomUUID();
|
|
12
|
+
let published = false;
|
|
13
|
+
try {
|
|
14
|
+
await writeFile(join(pending, owner), JSON.stringify({ pid: process.pid, createdAt: new Date().toISOString() }), { mode: 0o600 });
|
|
15
|
+
for (;;) {
|
|
16
|
+
options.signal?.throwIfAborted();
|
|
17
|
+
try {
|
|
18
|
+
// Publishing a nonempty directory cannot replace another owner's nonempty directory.
|
|
19
|
+
await rename(pending, path);
|
|
20
|
+
published = true;
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
catch (cause) {
|
|
24
|
+
if (!['EEXIST', 'ENOTEMPTY', 'ENOTDIR', 'EPERM'].includes(cause.code ?? ''))
|
|
25
|
+
throw cause;
|
|
26
|
+
if (await removeDeadOwner(path))
|
|
27
|
+
continue;
|
|
28
|
+
if (Date.now() >= deadline)
|
|
29
|
+
throw new AgentError(options.code ?? 'MAR_AGENT_SESSION_LOCKED', message, { cause });
|
|
30
|
+
}
|
|
19
31
|
await delay(25 + Math.floor(Math.random() * 25), undefined, options.signal ? { signal: options.signal } : {});
|
|
20
|
-
continue;
|
|
21
32
|
}
|
|
22
33
|
try {
|
|
23
|
-
await handle.writeFile(JSON.stringify({ pid: process.pid, createdAt: new Date().toISOString() }));
|
|
24
34
|
options.signal?.throwIfAborted();
|
|
25
35
|
}
|
|
26
36
|
catch (error) {
|
|
27
|
-
|
|
28
|
-
await handle.close();
|
|
29
|
-
}
|
|
30
|
-
finally {
|
|
31
|
-
await rm(path, { force: true });
|
|
32
|
-
}
|
|
37
|
+
await removeOwner(path, owner).catch(() => undefined);
|
|
33
38
|
throw error;
|
|
34
39
|
}
|
|
35
40
|
let released = false;
|
|
@@ -37,47 +42,74 @@ export async function acquireFileLock(path, message = 'Session index is busy.',
|
|
|
37
42
|
if (released)
|
|
38
43
|
return;
|
|
39
44
|
released = true;
|
|
40
|
-
await
|
|
41
|
-
await rm(path, { force: true });
|
|
45
|
+
await removeOwner(path, owner);
|
|
42
46
|
};
|
|
43
47
|
}
|
|
48
|
+
finally {
|
|
49
|
+
// An unpublished candidate only reaches here on failure; cleanup must preserve that error.
|
|
50
|
+
if (!published)
|
|
51
|
+
await rm(pending, { recursive: true, force: true }).catch(() => undefined);
|
|
52
|
+
}
|
|
44
53
|
}
|
|
45
54
|
async function removeDeadOwner(path) {
|
|
46
|
-
|
|
47
|
-
let reclaimer;
|
|
55
|
+
let owners;
|
|
48
56
|
try {
|
|
49
|
-
|
|
50
|
-
reclaimer = await open(reclaimerPath, 'wx', 0o600);
|
|
57
|
+
owners = await readdir(path);
|
|
51
58
|
}
|
|
52
59
|
catch (error) {
|
|
53
|
-
if (error.code === '
|
|
60
|
+
if (error.code === 'ENOENT')
|
|
61
|
+
return false;
|
|
62
|
+
if (error.code === 'ENOTDIR')
|
|
54
63
|
return false;
|
|
55
64
|
throw error;
|
|
56
65
|
}
|
|
66
|
+
if (owners.length === 0)
|
|
67
|
+
return removeEmptyDirectory(path);
|
|
68
|
+
if (owners.length !== 1)
|
|
69
|
+
return false;
|
|
70
|
+
const owner = owners[0];
|
|
57
71
|
try {
|
|
58
|
-
const
|
|
59
|
-
if (typeof
|
|
72
|
+
const metadata = JSON.parse(await readFile(join(path, owner), 'utf8'));
|
|
73
|
+
if (typeof metadata.pid !== 'number' ||
|
|
74
|
+
!Number.isSafeInteger(metadata.pid) ||
|
|
75
|
+
metadata.pid <= 0)
|
|
60
76
|
return false;
|
|
61
77
|
try {
|
|
62
|
-
process.kill(
|
|
78
|
+
process.kill(metadata.pid, 0);
|
|
63
79
|
return false;
|
|
64
80
|
}
|
|
65
81
|
catch (error) {
|
|
66
82
|
if (error.code !== 'ESRCH')
|
|
67
83
|
return false;
|
|
68
|
-
await rm(path, { force: true });
|
|
69
|
-
return true;
|
|
70
84
|
}
|
|
71
85
|
}
|
|
72
86
|
catch {
|
|
73
87
|
return false;
|
|
74
88
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
89
|
+
return removeOwner(path, owner);
|
|
90
|
+
}
|
|
91
|
+
/** Remove only this incarnation's owner; rmdir cannot remove a newly published nonempty lock. */
|
|
92
|
+
async function removeOwner(path, owner) {
|
|
93
|
+
try {
|
|
94
|
+
await unlink(join(path, owner));
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
if (error.code !== 'ENOENT')
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
return removeEmptyDirectory(path);
|
|
101
|
+
}
|
|
102
|
+
async function removeEmptyDirectory(path) {
|
|
103
|
+
try {
|
|
104
|
+
await rmdir(path);
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
const code = error.code;
|
|
109
|
+
if (code === 'ENOENT')
|
|
110
|
+
return true;
|
|
111
|
+
if (code === 'ENOTEMPTY' || code === 'EEXIST')
|
|
112
|
+
return false;
|
|
113
|
+
throw error;
|
|
82
114
|
}
|
|
83
115
|
}
|