@llblab/pi-actors 0.23.0 → 0.24.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/AGENTS.md +125 -60
- package/BACKLOG.md +93 -188
- package/CHANGELOG.md +22 -0
- package/README.md +13 -0
- package/dist/fixtures/protocol/actor-message-branch.json +13 -0
- package/dist/fixtures/protocol/artifact-manifest.json +9 -0
- package/dist/fixtures/protocol/mailbox-contract.json +15 -0
- package/dist/fixtures/protocol/recipe-summary.json +16 -0
- package/dist/fixtures/protocol/room-message.json +11 -0
- package/dist/fixtures/protocol/room-roster.json +11 -0
- package/dist/fixtures/protocol/run-inbox-message.json +9 -0
- package/dist/fixtures/protocol/run-outbox-event.json +9 -0
- package/dist/fixtures/protocol/run-state.json +10 -0
- package/dist/index.js +3 -2
- package/dist/lib/actor-inspector-tui.js +5 -32
- package/dist/lib/actor-rooms.d.ts +8 -0
- package/dist/lib/actor-rooms.js +64 -20
- package/dist/lib/actor-worker.d.ts +13 -0
- package/dist/lib/actor-worker.js +86 -0
- package/dist/lib/async-runner.d.ts +5 -0
- package/dist/lib/async-runner.js +134 -0
- package/dist/lib/async-runs.js +9 -28
- package/dist/lib/build-dist.d.ts +5 -0
- package/dist/lib/build-dist.js +24 -0
- package/dist/lib/conformance.d.ts +12 -0
- package/dist/lib/conformance.js +28 -0
- package/dist/lib/coordinator.d.ts +5 -0
- package/dist/lib/coordinator.js +557 -0
- package/dist/lib/locker.d.ts +5 -0
- package/dist/lib/locker.js +310 -0
- package/dist/lib/mailbox-loop.d.ts +41 -0
- package/dist/lib/mailbox-loop.js +62 -0
- package/dist/lib/observability.d.ts +2 -2
- package/dist/lib/observability.js +51 -53
- package/dist/lib/prompts.d.ts +1 -1
- package/dist/lib/prompts.js +1 -1
- package/dist/lib/recipe-references.js +25 -2
- package/dist/lib/recipe-utils.d.ts +5 -0
- package/dist/lib/recipe-utils.js +385 -0
- package/dist/lib/runtime-notifier.js +3 -7
- package/dist/lib/state-readers.d.ts +21 -0
- package/dist/lib/state-readers.js +74 -0
- package/dist/lib/tools.js +1 -1
- package/dist/lib/validate-recipe.d.ts +6 -0
- package/dist/lib/validate-recipe.js +104 -0
- package/dist/recipes/actor-worker.json +35 -0
- package/dist/recipes/coordinator-locker.json +45 -0
- package/dist/recipes/lens-swarm.json +66 -0
- package/dist/recipes/locker.json +45 -0
- package/dist/recipes/music-player.json +38 -0
- package/dist/recipes/pipeline-architect-coordinator.json +95 -0
- package/dist/recipes/pipeline-artifact-bundle.json +100 -0
- package/dist/recipes/pipeline-artifact-report.json +58 -0
- package/dist/recipes/pipeline-artifact-write.json +72 -0
- package/dist/recipes/pipeline-async-run-ops.json +70 -0
- package/dist/recipes/pipeline-checkpoint-continuation.json +67 -0
- package/dist/recipes/pipeline-development-tasking.json +81 -0
- package/dist/recipes/pipeline-docs-maintenance.json +80 -0
- package/dist/recipes/pipeline-media-library.json +59 -0
- package/dist/recipes/pipeline-quorum-review.json +79 -0
- package/dist/recipes/pipeline-release-readiness.json +110 -0
- package/dist/recipes/pipeline-release-summary.json +88 -0
- package/dist/recipes/pipeline-repo-health.json +89 -0
- package/dist/recipes/pipeline-research-synthesis.json +94 -0
- package/dist/recipes/pipeline-review-readiness.json +54 -0
- package/dist/recipes/pipeline-room-swarm.json +50 -0
- package/dist/recipes/subagent-artifact.json +32 -0
- package/dist/recipes/subagent-checkpoint.json +33 -0
- package/dist/recipes/subagent-conflict-report.json +32 -0
- package/dist/recipes/subagent-contradiction-map.json +33 -0
- package/dist/recipes/subagent-critic.json +35 -0
- package/dist/recipes/subagent-evidence-map.json +33 -0
- package/dist/recipes/subagent-followup.json +33 -0
- package/dist/recipes/subagent-judge.json +33 -0
- package/dist/recipes/subagent-merge.json +33 -0
- package/dist/recipes/subagent-message.json +34 -0
- package/dist/recipes/subagent-normalize.json +31 -0
- package/dist/recipes/subagent-plan.json +33 -0
- package/dist/recipes/subagent-prompt.json +28 -0
- package/dist/recipes/subagent-quorum.json +43 -0
- package/dist/recipes/subagent-review-coordinator.json +114 -0
- package/dist/recipes/subagent-review.json +37 -0
- package/dist/recipes/subagent-task-card.json +35 -0
- package/dist/recipes/subagent-tools.json +27 -0
- package/dist/recipes/subagent-verify.json +34 -0
- package/dist/recipes/subagents-prompts.json +51 -0
- package/dist/recipes/utility-actor-message.json +23 -0
- package/dist/recipes/utility-artifact-manifest.json +16 -0
- package/dist/recipes/utility-artifact-write.json +16 -0
- package/dist/recipes/utility-changelog-head.json +11 -0
- package/dist/recipes/utility-changelog-section.json +13 -0
- package/dist/recipes/utility-coordinator-lock-snapshot.json +13 -0
- package/dist/recipes/utility-git-log.json +11 -0
- package/dist/recipes/utility-git-status.json +9 -0
- package/dist/recipes/utility-jsonl-tail.json +10 -0
- package/dist/recipes/utility-markdown-index.json +14 -0
- package/dist/recipes/utility-package-summary.json +11 -0
- package/dist/recipes/utility-playlist-build.json +17 -0
- package/dist/recipes/utility-playlist-scan.json +11 -0
- package/dist/recipes/utility-run-ops-snapshot.json +17 -0
- package/dist/recipes/utility-run-state-files.json +13 -0
- package/dist/recipes/utility-run-summary.json +11 -0
- package/dist/recipes/utility-skill-summary.json +13 -0
- package/dist/recipes/utility-validate-recipe.json +13 -0
- package/dist/recipes/utility-validation-wrapper.json +13 -0
- package/dist/scripts/actor-worker.mjs +31 -0
- package/dist/scripts/async-runner.mjs +31 -0
- package/dist/scripts/build-dist.mjs +25 -0
- package/dist/scripts/conformance.mjs +33 -0
- package/dist/scripts/coordinator.mjs +31 -0
- package/dist/scripts/locker.mjs +33 -0
- package/dist/scripts/music-player.mjs +964 -0
- package/dist/scripts/recipe-utils.mjs +31 -0
- package/dist/scripts/validate-recipe.mjs +34 -0
- package/dist/skills/actors/SKILL.md +375 -0
- package/dist/skills/swarm/SKILL.md +467 -0
- package/dist/skills/swarm/references/development-swarm.md +596 -0
- package/docs/actor-messages.md +2 -2
- package/docs/async-runs.md +11 -0
- package/docs/template-recipes.md +1 -1
- package/fixtures/protocol/actor-message-branch.json +13 -0
- package/fixtures/protocol/artifact-manifest.json +9 -0
- package/fixtures/protocol/mailbox-contract.json +15 -0
- package/fixtures/protocol/recipe-summary.json +16 -0
- package/fixtures/protocol/room-message.json +11 -0
- package/fixtures/protocol/room-roster.json +11 -0
- package/fixtures/protocol/run-inbox-message.json +9 -0
- package/fixtures/protocol/run-outbox-event.json +9 -0
- package/fixtures/protocol/run-state.json +10 -0
- package/index.ts +3 -0
- package/lib/actor-inspector-tui.ts +11 -34
- package/lib/actor-rooms.ts +88 -18
- package/lib/actor-worker.ts +118 -0
- package/lib/async-runner.ts +173 -0
- package/lib/async-runs.ts +12 -22
- package/lib/build-dist.ts +30 -0
- package/lib/conformance.ts +46 -0
- package/lib/coordinator.ts +649 -0
- package/lib/locker.ts +340 -0
- package/lib/mailbox-loop.ts +148 -0
- package/lib/observability.ts +24 -19
- package/lib/prompts.ts +1 -1
- package/lib/recipe-references.ts +37 -2
- package/lib/recipe-utils.ts +486 -0
- package/lib/runtime-notifier.ts +4 -6
- package/lib/state-readers.ts +93 -0
- package/lib/tools.ts +1 -1
- package/lib/validate-recipe.ts +110 -0
- package/package.json +10 -2
- package/recipes/actor-worker.json +35 -0
- package/recipes/pipeline-quorum-review.json +12 -7
- package/scripts/actor-worker.mjs +31 -0
- package/scripts/async-runner.mjs +11 -201
- package/scripts/build-dist.mjs +25 -0
- package/scripts/conformance.mjs +21 -35
- package/scripts/coordinator.mjs +15 -625
- package/scripts/locker.mjs +20 -332
- package/scripts/recipe-utils.mjs +17 -477
- package/scripts/validate-recipe.mjs +18 -121
- package/skills/actors/SKILL.md +5 -2
- package/skills/swarm/SKILL.md +3 -1
package/scripts/locker.mjs
CHANGED
|
@@ -1,345 +1,33 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Local coordination locker service.
|
|
4
|
+
* Local coordination locker service shim.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* queue work items, hand out task leases, renew/release resource locks, and emit
|
|
9
|
-
* compact snapshots for inspection.
|
|
10
|
-
*
|
|
11
|
-
* Keep it generic: no project policy, no recipe-specific prompts, and no actor
|
|
12
|
-
* orchestration decisions belong in this layer.
|
|
6
|
+
* Runtime logic lives in lib/locker.ts and is compiled to dist/lib/locker.js
|
|
7
|
+
* for installed JS-only packages.
|
|
13
8
|
*/
|
|
14
9
|
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
appendFileSync,
|
|
19
|
-
existsSync,
|
|
20
|
-
mkdirSync,
|
|
21
|
-
readFileSync,
|
|
22
|
-
rmSync,
|
|
23
|
-
writeFileSync,
|
|
24
|
-
} from "node:fs";
|
|
25
|
-
import { createServer } from "node:net";
|
|
26
|
-
import { dirname, join, resolve } from "node:path";
|
|
27
|
-
import readline from "node:readline";
|
|
28
|
-
|
|
29
|
-
function parseArgs(argv) {
|
|
30
|
-
const args = { mode: "serve", stateDir: "", leaseMs: 600000, lines: 20 };
|
|
31
|
-
for (let index = 0; index < argv.length; index += 1) {
|
|
32
|
-
const arg = argv[index];
|
|
33
|
-
if (arg === "serve" || arg === "snapshot") args.mode = arg;
|
|
34
|
-
else if (arg === "--state-dir") args.stateDir = argv[++index] ?? "";
|
|
35
|
-
else if (arg === "--lease-ms")
|
|
36
|
-
args.leaseMs = Number(argv[++index] ?? args.leaseMs);
|
|
37
|
-
else if (arg === "--lines")
|
|
38
|
-
args.lines = Number(argv[++index] ?? args.lines);
|
|
39
|
-
}
|
|
40
|
-
if (!args.stateDir) throw new Error("--state-dir is required");
|
|
41
|
-
if (!Number.isFinite(args.leaseMs) || args.leaseMs <= 0)
|
|
42
|
-
args.leaseMs = 600000;
|
|
43
|
-
if (!Number.isFinite(args.lines) || args.lines <= 0) args.lines = 20;
|
|
44
|
-
return args;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const { mode, stateDir, leaseMs, lines } = parseArgs(process.argv.slice(2));
|
|
48
|
-
const queuePath = join(stateDir, "queue.json");
|
|
49
|
-
const locksPath = join(stateDir, "locks.json");
|
|
50
|
-
const journalPath = join(stateDir, "journal.jsonl");
|
|
51
|
-
const outboxPath = join(stateDir, "outbox.jsonl");
|
|
52
|
-
const controlPath = join(stateDir, "control.fifo");
|
|
53
|
-
mkdirSync(stateDir, { recursive: true });
|
|
54
|
-
|
|
55
|
-
function readJson(path, fallback) {
|
|
56
|
-
if (!existsSync(path)) return fallback;
|
|
57
|
-
try {
|
|
58
|
-
return JSON.parse(readFileSync(path, "utf8"));
|
|
59
|
-
} catch {
|
|
60
|
-
return fallback;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
function writeJson(path, value) {
|
|
64
|
-
mkdirSync(dirname(path), { recursive: true });
|
|
65
|
-
writeFileSync(path, `${JSON.stringify(value, null, 2)}\n`, "utf8");
|
|
66
|
-
}
|
|
67
|
-
function getControlEndpoint() {
|
|
68
|
-
if (process.platform !== "win32") return { path: controlPath, type: "fifo" };
|
|
69
|
-
const hash = createHash("sha256")
|
|
70
|
-
.update(resolve(stateDir))
|
|
71
|
-
.digest("hex")
|
|
72
|
-
.slice(0, 20);
|
|
73
|
-
return { path: `\\\\.\\pipe\\pi-actors-locker-${hash}`, type: "named-pipe" };
|
|
74
|
-
}
|
|
75
|
-
function writeControlEndpoint(endpoint) {
|
|
76
|
-
writeJson(join(stateDir, "control.json"), endpoint);
|
|
77
|
-
const runPath = join(stateDir, "run.json");
|
|
78
|
-
const run = readJson(runPath, undefined);
|
|
79
|
-
if (run && typeof run === "object")
|
|
80
|
-
writeJson(runPath, { ...run, control: endpoint });
|
|
81
|
-
}
|
|
82
|
-
function journal(event, data = {}) {
|
|
83
|
-
appendFileSync(
|
|
84
|
-
journalPath,
|
|
85
|
-
`${JSON.stringify({ event, ts: new Date().toISOString(), ...data })}\n`,
|
|
86
|
-
);
|
|
87
|
-
}
|
|
88
|
-
function outbox(type, summary, body = {}, level = "info") {
|
|
89
|
-
appendFileSync(
|
|
90
|
-
outboxPath,
|
|
91
|
-
`${JSON.stringify({ to: "coordinator", from: `run:${process.env.run_id ?? "locker"}`, type, event: type, summary, body, data: body, delivery: "followup", level, ts: new Date().toISOString() })}\n`,
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
function now() {
|
|
95
|
-
return Date.now();
|
|
96
|
-
}
|
|
97
|
-
function cleanExpiredLocks(locks) {
|
|
98
|
-
const current = now();
|
|
99
|
-
const kept = {};
|
|
100
|
-
for (const [key, lock] of Object.entries(locks)) {
|
|
101
|
-
if (Number(lock.expiresAt) > current) kept[key] = lock;
|
|
102
|
-
else journal("lock.expired", { resource: key, owner: lock.owner });
|
|
103
|
-
}
|
|
104
|
-
return kept;
|
|
105
|
-
}
|
|
106
|
-
function normalizeMessage(line) {
|
|
107
|
-
const trimmed = line.trim();
|
|
108
|
-
if (!trimmed) return undefined;
|
|
109
|
-
if (["stop", "quit", "exit", "cancel"].includes(trimmed.toLowerCase())) {
|
|
110
|
-
return { type: "control.stop", body: {} };
|
|
111
|
-
}
|
|
112
|
-
try {
|
|
113
|
-
return JSON.parse(trimmed);
|
|
114
|
-
} catch {
|
|
115
|
-
return { type: "lock.enqueue", body: { task: trimmed } };
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
function tailJournal(count) {
|
|
119
|
-
if (!existsSync(journalPath)) return [];
|
|
120
|
-
return readFileSync(journalPath, "utf8")
|
|
121
|
-
.trimEnd()
|
|
122
|
-
.split("\n")
|
|
123
|
-
.filter(Boolean)
|
|
124
|
-
.slice(-count)
|
|
125
|
-
.map((line) => {
|
|
126
|
-
try {
|
|
127
|
-
return JSON.parse(line);
|
|
128
|
-
} catch {
|
|
129
|
-
return { raw: line };
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
function printSnapshot() {
|
|
134
|
-
const locks = cleanExpiredLocks(readJson(locksPath, {}));
|
|
135
|
-
writeJson(locksPath, locks);
|
|
136
|
-
const queue = readJson(queuePath, { items: [] });
|
|
137
|
-
console.log(
|
|
138
|
-
JSON.stringify(
|
|
139
|
-
{
|
|
140
|
-
queueDepth: Array.isArray(queue.items) ? queue.items.length : 0,
|
|
141
|
-
queue,
|
|
142
|
-
locks,
|
|
143
|
-
journal: tailJournal(lines),
|
|
144
|
-
},
|
|
145
|
-
null,
|
|
146
|
-
2,
|
|
147
|
-
),
|
|
148
|
-
);
|
|
149
|
-
}
|
|
150
|
-
function nextTask(queue, locks) {
|
|
151
|
-
const items = Array.isArray(queue.items) ? queue.items : [];
|
|
152
|
-
const index = items.findIndex((item) => {
|
|
153
|
-
const resources = Array.isArray(item.resources) ? item.resources : [];
|
|
154
|
-
return resources.every((resource) => !locks[resource]);
|
|
155
|
-
});
|
|
156
|
-
if (index < 0) return undefined;
|
|
157
|
-
return items.splice(index, 1)[0];
|
|
158
|
-
}
|
|
159
|
-
function handle(message) {
|
|
160
|
-
const type = message.type || message.event || "lock.enqueue";
|
|
161
|
-
const body =
|
|
162
|
-
message.body && typeof message.body === "object" ? message.body : message;
|
|
163
|
-
let queue = readJson(queuePath, { items: [] });
|
|
164
|
-
let locks = cleanExpiredLocks(readJson(locksPath, {}));
|
|
165
|
-
if (type === "control.stop" || type === "control.cancel") {
|
|
166
|
-
writeJson(locksPath, locks);
|
|
167
|
-
journal("control.stop", {});
|
|
168
|
-
outbox("lock.stopped", "Locker stopped", {
|
|
169
|
-
queueDepth: queue.items?.length ?? 0,
|
|
170
|
-
});
|
|
171
|
-
process.exit(0);
|
|
172
|
-
}
|
|
173
|
-
if (type === "lock.enqueue" || type === "coord.enqueue") {
|
|
174
|
-
const item = {
|
|
175
|
-
id: body.id || `task-${Date.now()}`,
|
|
176
|
-
task: body.task ?? body,
|
|
177
|
-
resources: body.resources ?? [],
|
|
178
|
-
enqueuedAt: new Date().toISOString(),
|
|
179
|
-
};
|
|
180
|
-
queue.items = [...(queue.items ?? []), item];
|
|
181
|
-
writeJson(queuePath, queue);
|
|
182
|
-
writeJson(locksPath, locks);
|
|
183
|
-
journal("lock.enqueued", { id: item.id, resources: item.resources });
|
|
184
|
-
outbox("lock.enqueued", `Queued task ${item.id}`, {
|
|
185
|
-
id: item.id,
|
|
186
|
-
queueDepth: queue.items.length,
|
|
187
|
-
});
|
|
188
|
-
return;
|
|
189
|
-
}
|
|
190
|
-
if (type === "lock.claim" || type === "coord.claim") {
|
|
191
|
-
const owner = body.owner || message.from || "worker";
|
|
192
|
-
const item = nextTask(queue, locks);
|
|
193
|
-
if (!item) {
|
|
194
|
-
writeJson(queuePath, queue);
|
|
195
|
-
writeJson(locksPath, locks);
|
|
196
|
-
outbox("lock.empty", "No claimable task", {
|
|
197
|
-
owner,
|
|
198
|
-
queueDepth: queue.items?.length ?? 0,
|
|
199
|
-
});
|
|
200
|
-
return;
|
|
201
|
-
}
|
|
202
|
-
for (const resource of item.resources ?? [])
|
|
203
|
-
locks[resource] = { owner, task: item.id, expiresAt: now() + leaseMs };
|
|
204
|
-
writeJson(queuePath, queue);
|
|
205
|
-
writeJson(locksPath, locks);
|
|
206
|
-
journal("lock.assigned", {
|
|
207
|
-
id: item.id,
|
|
208
|
-
owner,
|
|
209
|
-
resources: item.resources,
|
|
210
|
-
});
|
|
211
|
-
outbox("lock.assigned", `Assigned task ${item.id}`, { owner, task: item });
|
|
212
|
-
return;
|
|
213
|
-
}
|
|
214
|
-
if (type === "lock.acquire") {
|
|
215
|
-
const resource = body.resource;
|
|
216
|
-
const owner = body.owner || message.from || "worker";
|
|
217
|
-
if (!resource) throw new Error("lock.acquire body.resource is required");
|
|
218
|
-
if (locks[resource])
|
|
219
|
-
outbox(
|
|
220
|
-
"lock.denied",
|
|
221
|
-
`Lock denied ${resource}`,
|
|
222
|
-
{ resource, owner, current: locks[resource] },
|
|
223
|
-
"warning",
|
|
224
|
-
);
|
|
225
|
-
else {
|
|
226
|
-
locks[resource] = { owner, expiresAt: now() + leaseMs };
|
|
227
|
-
outbox("lock.granted", `Lock granted ${resource}`, { resource, owner });
|
|
228
|
-
}
|
|
229
|
-
writeJson(locksPath, locks);
|
|
230
|
-
return;
|
|
231
|
-
}
|
|
232
|
-
if (type === "lock.renew") {
|
|
233
|
-
const resource = body.resource;
|
|
234
|
-
const owner = body.owner || message.from || "worker";
|
|
235
|
-
if (!resource) throw new Error("lock.renew body.resource is required");
|
|
236
|
-
const current = locks[resource];
|
|
237
|
-
if (!current) {
|
|
238
|
-
outbox(
|
|
239
|
-
"lock.denied",
|
|
240
|
-
`Lock renew denied ${resource}`,
|
|
241
|
-
{ resource, owner, reason: "missing" },
|
|
242
|
-
"warning",
|
|
243
|
-
);
|
|
244
|
-
} else if (current.owner !== owner) {
|
|
245
|
-
outbox(
|
|
246
|
-
"lock.denied",
|
|
247
|
-
`Lock renew denied ${resource}`,
|
|
248
|
-
{ resource, owner, current },
|
|
249
|
-
"warning",
|
|
250
|
-
);
|
|
251
|
-
} else {
|
|
252
|
-
locks[resource] = { ...current, expiresAt: now() + leaseMs };
|
|
253
|
-
outbox("lock.renewed", `Lock renewed ${resource}`, { resource, owner });
|
|
254
|
-
}
|
|
255
|
-
writeJson(locksPath, locks);
|
|
256
|
-
return;
|
|
257
|
-
}
|
|
258
|
-
if (type === "lock.release") {
|
|
259
|
-
const resource = body.resource;
|
|
260
|
-
if (resource) delete locks[resource];
|
|
261
|
-
writeJson(locksPath, locks);
|
|
262
|
-
outbox("lock.released", `Lock released ${resource}`, { resource });
|
|
263
|
-
return;
|
|
264
|
-
}
|
|
265
|
-
if (
|
|
266
|
-
type === "lock.complete" ||
|
|
267
|
-
type === "lock.fail" ||
|
|
268
|
-
type === "coord.complete" ||
|
|
269
|
-
type === "coord.fail"
|
|
270
|
-
) {
|
|
271
|
-
const eventType = type.startsWith("coord.")
|
|
272
|
-
? type.replace("coord.", "lock.")
|
|
273
|
-
: type;
|
|
274
|
-
journal(eventType, body);
|
|
275
|
-
outbox(
|
|
276
|
-
eventType,
|
|
277
|
-
`${eventType} ${body.id ?? ""}`.trim(),
|
|
278
|
-
body,
|
|
279
|
-
eventType === "lock.fail" ? "error" : "info",
|
|
280
|
-
);
|
|
281
|
-
writeJson(locksPath, locks);
|
|
282
|
-
writeJson(queuePath, queue);
|
|
283
|
-
return;
|
|
284
|
-
}
|
|
285
|
-
journal("lock.unknown", { type, body });
|
|
286
|
-
outbox("lock.unknown", `Unknown message ${type}`, { type, body }, "warning");
|
|
287
|
-
}
|
|
10
|
+
import { existsSync } from "node:fs";
|
|
11
|
+
import { basename, dirname, join } from "node:path";
|
|
12
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
288
13
|
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
14
|
+
function packageRoot() {
|
|
15
|
+
const scriptDir = dirname(fileURLToPath(import.meta.url));
|
|
16
|
+
const parent = dirname(scriptDir);
|
|
17
|
+
return basename(parent) === "dist" ? dirname(parent) : parent;
|
|
292
18
|
}
|
|
293
19
|
|
|
294
|
-
function
|
|
295
|
-
const
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
handle(message);
|
|
299
|
-
} catch (error) {
|
|
300
|
-
const text = error instanceof Error ? error.message : String(error);
|
|
301
|
-
journal("lock.error", { error: text });
|
|
302
|
-
outbox("lock.error", text, { error: text }, "error");
|
|
303
|
-
}
|
|
20
|
+
function mainModulePath() {
|
|
21
|
+
const root = packageRoot();
|
|
22
|
+
const compiled = join(root, "dist", "lib", "locker.js");
|
|
23
|
+
return existsSync(compiled) ? compiled : join(root, "lib", "locker.ts");
|
|
304
24
|
}
|
|
305
25
|
|
|
306
|
-
|
|
307
|
-
if (!existsSync(endpoint.path)) {
|
|
308
|
-
const result = spawnSync("mkfifo", [endpoint.path]);
|
|
309
|
-
if (result.status !== 0)
|
|
310
|
-
throw new Error(`mkfifo failed: ${result.stderr?.toString?.() ?? ""}`);
|
|
311
|
-
}
|
|
312
|
-
writeControlEndpoint(endpoint);
|
|
313
|
-
while (true) {
|
|
314
|
-
const stream = await import("node:fs").then((fs) =>
|
|
315
|
-
fs.createReadStream(endpoint.path, { encoding: "utf8" }),
|
|
316
|
-
);
|
|
317
|
-
const rl = readline.createInterface({ input: stream, crlfDelay: Infinity });
|
|
318
|
-
for await (const line of rl) handleLine(line);
|
|
319
|
-
}
|
|
320
|
-
}
|
|
26
|
+
const { runLocker } = await import(pathToFileURL(mainModulePath()).href);
|
|
321
27
|
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
});
|
|
328
|
-
await new Promise((resolveReady, rejectReady) => {
|
|
329
|
-
server.once("error", rejectReady);
|
|
330
|
-
server.listen(endpoint.path, () => {
|
|
331
|
-
server.off("error", rejectReady);
|
|
332
|
-
writeControlEndpoint(endpoint);
|
|
333
|
-
resolveReady();
|
|
334
|
-
});
|
|
335
|
-
});
|
|
336
|
-
await new Promise(() => {});
|
|
28
|
+
try {
|
|
29
|
+
await runLocker(process.argv.slice(2));
|
|
30
|
+
} catch (error) {
|
|
31
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
32
|
+
process.exit(1);
|
|
337
33
|
}
|
|
338
|
-
|
|
339
|
-
const endpoint = getControlEndpoint();
|
|
340
|
-
writeJson(queuePath, readJson(queuePath, { items: [] }));
|
|
341
|
-
writeJson(locksPath, cleanExpiredLocks(readJson(locksPath, {})));
|
|
342
|
-
journal("lock.started", { leaseMs, control: endpoint.type });
|
|
343
|
-
outbox("lock.started", "Locker ready", { leaseMs, control: endpoint.type });
|
|
344
|
-
if (endpoint.type === "named-pipe") await serveNamedPipe(endpoint);
|
|
345
|
-
else await serveFifo(endpoint);
|