blun-king-cli 9.1.562 → 9.1.563
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/LIESMICH.txt +2 -2
- package/README.md +1 -1
- package/agent-spine-plugin/.claude-plugin/marketplace.json +1 -1
- package/agent-spine-plugin/.claude-plugin/plugin.json +1 -1
- package/agent-spine-plugin/.codex-plugin/plugin.json +1 -1
- package/agent-spine-plugin/blun.plugin.json +3 -3
- package/agent-spine-plugin/hooks/codex.json +2 -2
- package/agent-spine-plugin/hooks/hooks.json +1 -1
- package/agent-spine-plugin/hooks/version.json +1 -1
- package/agent-spine-plugin/package.json +1 -1
- package/agent-spine-plugin/scripts/check-hosts.js +199 -196
- package/agent-spine-plugin/skills/agent-spine/SKILL.md +1 -1
- package/agent-spine-plugin/src/cli.js +390 -7
- package/agent-spine-plugin/src/hook.js +80 -10
- package/agent-spine-plugin/src/index.js +4 -2
- package/agent-spine-plugin/src/lib/audit.js +19 -1
- package/agent-spine-plugin/src/lib/filesystem-retry.js +5 -0
- package/agent-spine-plugin/src/lib/learning.js +5575 -128
- package/agent-spine-plugin/src/lib/owned-file-lock.js +143 -0
- package/agent-spine-plugin/src/version.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { open, readFile, stat, unlink, utimes } from "node:fs/promises";
|
|
3
|
+
import { isFileLockContention, isTransientLockMetadataError } from "./filesystem-retry.js";
|
|
4
|
+
|
|
5
|
+
const LOCK_SCHEMA = "agentspine.owned-file-lock/v1";
|
|
6
|
+
|
|
7
|
+
function delay(milliseconds) {
|
|
8
|
+
return new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function lockPayload(token, acquiredAt, leaseMs) {
|
|
12
|
+
return {
|
|
13
|
+
schema: LOCK_SCHEMA,
|
|
14
|
+
token,
|
|
15
|
+
acquiredAt,
|
|
16
|
+
leaseMs,
|
|
17
|
+
authority: "state-coordination-only"
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async function readOwner(path) {
|
|
22
|
+
try {
|
|
23
|
+
const value = JSON.parse(await readFile(path, "utf8"));
|
|
24
|
+
if (!value || typeof value !== "object" || Array.isArray(value)
|
|
25
|
+
|| value.schema !== LOCK_SCHEMA || typeof value.token !== "string") return null;
|
|
26
|
+
return value;
|
|
27
|
+
} catch (error) {
|
|
28
|
+
if (error.code === "ENOENT" || error instanceof SyntaxError) return null;
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function sameFile(left, right) {
|
|
34
|
+
return left.dev === right.dev && left.ino === right.ino && left.size === right.size
|
|
35
|
+
&& left.mtimeMs === right.mtimeMs && left.ctimeMs === right.ctimeMs;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function removeStaleLock(path, staleAfterMs) {
|
|
39
|
+
let before;
|
|
40
|
+
try {
|
|
41
|
+
before = await stat(path);
|
|
42
|
+
} catch (error) {
|
|
43
|
+
if (isTransientLockMetadataError(error)) return false;
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
if (Date.now() - before.mtimeMs <= staleAfterMs) return false;
|
|
47
|
+
await readOwner(path);
|
|
48
|
+
let after;
|
|
49
|
+
try {
|
|
50
|
+
after = await stat(path);
|
|
51
|
+
} catch (error) {
|
|
52
|
+
if (isTransientLockMetadataError(error)) return false;
|
|
53
|
+
throw error;
|
|
54
|
+
}
|
|
55
|
+
if (!sameFile(before, after) || Date.now() - after.mtimeMs <= staleAfterMs) return false;
|
|
56
|
+
try {
|
|
57
|
+
await unlink(path);
|
|
58
|
+
return true;
|
|
59
|
+
} catch (error) {
|
|
60
|
+
if (isFileLockContention(error) || isTransientLockMetadataError(error)) return false;
|
|
61
|
+
throw error;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export async function withOwnedFileLock(path, task, {
|
|
66
|
+
staleAfterMs = 15000,
|
|
67
|
+
heartbeatIntervalMs = 1000,
|
|
68
|
+
retryDelayMs = 25,
|
|
69
|
+
maxAttempts = 80
|
|
70
|
+
} = {}) {
|
|
71
|
+
if (typeof task !== "function") throw new Error("owned file lock requires a task");
|
|
72
|
+
if (!Number.isInteger(staleAfterMs) || staleAfterMs < 50
|
|
73
|
+
|| !Number.isInteger(heartbeatIntervalMs) || heartbeatIntervalMs < 10
|
|
74
|
+
|| heartbeatIntervalMs * 3 >= staleAfterMs
|
|
75
|
+
|| !Number.isInteger(retryDelayMs) || retryDelayMs < 1
|
|
76
|
+
|| !Number.isInteger(maxAttempts) || maxAttempts < 1) {
|
|
77
|
+
throw new Error("owned file lock timing is invalid");
|
|
78
|
+
}
|
|
79
|
+
const token = randomUUID();
|
|
80
|
+
const acquiredAt = new Date().toISOString();
|
|
81
|
+
let acquired = false;
|
|
82
|
+
for (let attempt = 0; attempt < maxAttempts; attempt += 1) {
|
|
83
|
+
let handle;
|
|
84
|
+
try {
|
|
85
|
+
handle = await open(path, "wx", 0o600);
|
|
86
|
+
const payload = `${JSON.stringify(lockPayload(token, acquiredAt, staleAfterMs))}\n`;
|
|
87
|
+
await handle.writeFile(payload, "utf8");
|
|
88
|
+
acquired = true;
|
|
89
|
+
break;
|
|
90
|
+
} catch (error) {
|
|
91
|
+
if (!isFileLockContention(error)) {
|
|
92
|
+
if (handle) {
|
|
93
|
+
await handle.close();
|
|
94
|
+
handle = null;
|
|
95
|
+
await unlink(path).catch((cleanupError) => {
|
|
96
|
+
if (cleanupError.code !== "ENOENT") error.cleanupError = cleanupError;
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
await removeStaleLock(path, staleAfterMs);
|
|
102
|
+
if (attempt + 1 < maxAttempts) await delay(retryDelayMs);
|
|
103
|
+
} finally {
|
|
104
|
+
await handle?.close();
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (!acquired) throw new Error("state is busy; retry shortly");
|
|
108
|
+
|
|
109
|
+
let ownershipError = null;
|
|
110
|
+
let heartbeat = Promise.resolve();
|
|
111
|
+
const assertOwned = async () => {
|
|
112
|
+
if (ownershipError) throw ownershipError;
|
|
113
|
+
const owner = await readOwner(path);
|
|
114
|
+
if (!owner || owner.token !== token) {
|
|
115
|
+
ownershipError = new Error("state lock ownership was lost; mutation aborted");
|
|
116
|
+
throw ownershipError;
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
const renew = async () => {
|
|
120
|
+
await assertOwned();
|
|
121
|
+
const now = new Date();
|
|
122
|
+
await utimes(path, now, now);
|
|
123
|
+
};
|
|
124
|
+
const timer = setInterval(() => {
|
|
125
|
+
heartbeat = heartbeat.then(renew).catch((error) => { ownershipError ||= error; });
|
|
126
|
+
}, heartbeatIntervalMs);
|
|
127
|
+
timer.unref?.();
|
|
128
|
+
|
|
129
|
+
try {
|
|
130
|
+
const result = await task({ token, acquiredAt, assertOwned });
|
|
131
|
+
await assertOwned();
|
|
132
|
+
return result;
|
|
133
|
+
} finally {
|
|
134
|
+
clearInterval(timer);
|
|
135
|
+
await heartbeat;
|
|
136
|
+
const owner = await readOwner(path).catch(() => null);
|
|
137
|
+
if (owner?.token === token) {
|
|
138
|
+
await unlink(path).catch((error) => {
|
|
139
|
+
if (error.code !== "ENOENT") throw error;
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = "0.
|
|
1
|
+
export const VERSION = "0.49.0";
|