sigild 0.0.12 → 0.0.13
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 +15 -2
- package/THREAT_MODEL.md +2 -0
- package/dist/src/audit/index.d.ts +2 -1
- package/dist/src/audit/index.d.ts.map +1 -1
- package/dist/src/audit/index.js +1 -0
- package/dist/src/audit/index.js.map +1 -1
- package/dist/src/audit/log.d.ts +37 -3
- package/dist/src/audit/log.d.ts.map +1 -1
- package/dist/src/audit/log.js +224 -40
- package/dist/src/audit/log.js.map +1 -1
- package/dist/src/bin/sigil-mcp.js +11 -2
- package/dist/src/bin/sigil-mcp.js.map +1 -1
- package/dist/src/cli/main.d.ts.map +1 -1
- package/dist/src/cli/main.js +35 -1
- package/dist/src/cli/main.js.map +1 -1
- package/dist/src/cli/paths.d.ts +2 -0
- package/dist/src/cli/paths.d.ts.map +1 -1
- package/dist/src/cli/paths.js +1 -0
- package/dist/src/cli/paths.js.map +1 -1
- package/dist/src/cli/portal.d.ts.map +1 -1
- package/dist/src/cli/portal.js +11 -1
- package/dist/src/cli/portal.js.map +1 -1
- package/dist/src/daemon/methods.d.ts +8 -1
- package/dist/src/daemon/methods.d.ts.map +1 -1
- package/dist/src/daemon/methods.js +121 -7
- package/dist/src/daemon/methods.js.map +1 -1
- package/dist/src/fs/index.d.ts +2 -0
- package/dist/src/fs/index.d.ts.map +1 -0
- package/dist/src/fs/index.js +2 -0
- package/dist/src/fs/index.js.map +1 -0
- package/dist/src/fs/lock.d.ts +84 -0
- package/dist/src/fs/lock.d.ts.map +1 -0
- package/dist/src/fs/lock.js +329 -0
- package/dist/src/fs/lock.js.map +1 -0
- package/dist/src/policy/evaluate.d.ts.map +1 -1
- package/dist/src/policy/evaluate.js +69 -4
- package/dist/src/policy/evaluate.js.map +1 -1
- package/dist/src/policy/index.d.ts +2 -0
- package/dist/src/policy/index.d.ts.map +1 -1
- package/dist/src/policy/index.js +2 -0
- package/dist/src/policy/index.js.map +1 -1
- package/dist/src/policy/ledger.d.ts +83 -0
- package/dist/src/policy/ledger.d.ts.map +1 -0
- package/dist/src/policy/ledger.js +250 -0
- package/dist/src/policy/ledger.js.map +1 -0
- package/dist/src/policy/loader.d.ts.map +1 -1
- package/dist/src/policy/loader.js +45 -0
- package/dist/src/policy/loader.js.map +1 -1
- package/dist/src/policy/template.d.ts +2 -2
- package/dist/src/policy/template.d.ts.map +1 -1
- package/dist/src/policy/template.js +32 -0
- package/dist/src/policy/template.js.map +1 -1
- package/dist/src/policy/types.d.ts +26 -0
- package/dist/src/policy/types.d.ts.map +1 -1
- package/dist/src/policy/types.js.map +1 -1
- package/dist/src/policy/window.d.ts +22 -0
- package/dist/src/policy/window.d.ts.map +1 -0
- package/dist/src/policy/window.js +66 -0
- package/dist/src/policy/window.js.map +1 -0
- package/package.json +5 -5
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import { closeSync, mkdirSync, openSync, readdirSync, unlinkSync, writeSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { randomBytes } from 'node:crypto';
|
|
4
|
+
/**
|
|
5
|
+
* Cross-process mutex for the files several `sigil-mcp` instances append
|
|
6
|
+
* to concurrently (one per Claude window: the audit log, the per-portal
|
|
7
|
+
* spend ledgers). Node core has no flock(2) and sigil takes no native
|
|
8
|
+
* deps, so this is built from two atomic filesystem operations only:
|
|
9
|
+
* exclusive create and unlink of a process's *own* files.
|
|
10
|
+
*
|
|
11
|
+
* It is Lamport's bakery algorithm on a directory:
|
|
12
|
+
*
|
|
13
|
+
* 1. create c-<pid>-<token> "I am choosing a number"
|
|
14
|
+
* 2. read the directory; my number is 1 + the largest ticket number
|
|
15
|
+
* 3. create t-<number>-<pid>-<token> my ticket
|
|
16
|
+
* 4. unlink c-<pid>-<token>
|
|
17
|
+
* 5. wait until no other live process is choosing, and no other live
|
|
18
|
+
* process holds a ticket ordered before mine (number, pid, token)
|
|
19
|
+
* 6. critical section
|
|
20
|
+
* 7. unlink my ticket
|
|
21
|
+
*
|
|
22
|
+
* Why this and not a single lock file: with one file, recovering from a
|
|
23
|
+
* holder that died means deleting a path some other process may just
|
|
24
|
+
* have re-created, and every "check, then unlink" sequence is a race —
|
|
25
|
+
* in the lock, and again in any sidecar used to serialise the breaking.
|
|
26
|
+
* Here no process ever unlinks a live process's file. Tickets of dead
|
|
27
|
+
* pids are simply ignored (and swept, which is safe because their owner
|
|
28
|
+
* cannot act), so a crashed holder costs nothing and there is nothing to
|
|
29
|
+
* "break". Correctness needs only that a file present for the whole of a
|
|
30
|
+
* readdir is listed, which every filesystem gives.
|
|
31
|
+
*
|
|
32
|
+
* A process also recognises files carrying its own pid that no live
|
|
33
|
+
* acquisition in it holds: an orphan of its own (a release that failed)
|
|
34
|
+
* or a dead predecessor that had this pid. Both are swept.
|
|
35
|
+
*
|
|
36
|
+
* Residual gap, documented rather than hidden: pid reuse across
|
|
37
|
+
* *different* processes. A dead holder whose pid was recycled by an
|
|
38
|
+
* unrelated live process looks alive, and its ticket blocks everyone
|
|
39
|
+
* until that process exits or a human removes the file. That is an
|
|
40
|
+
* availability limit, not an exclusion failure; closing it needs OS-level
|
|
41
|
+
* locking.
|
|
42
|
+
*
|
|
43
|
+
* Upgrading: a daemon built before this change locks a single file
|
|
44
|
+
* `<target>.lock` and knows nothing of this directory, and its writer also
|
|
45
|
+
* has a currency check that a fresh chain can fool. Running old and new
|
|
46
|
+
* daemons against one log is therefore unsupported, and no bridge here can
|
|
47
|
+
* make it safe. This is not a new requirement: a running sigil-mcp never
|
|
48
|
+
* reloads code, so every Claude window must restart its sigil-mcp after any
|
|
49
|
+
* upgrade anyway (see README, "Multi-window behaviour").
|
|
50
|
+
*/
|
|
51
|
+
export class FileLockError extends Error {
|
|
52
|
+
constructor(msg) {
|
|
53
|
+
super(`file lock error: ${msg}`);
|
|
54
|
+
this.name = 'FileLockError';
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** @deprecated alias kept for the audit module's public surface. */
|
|
58
|
+
export const AuditLockError = FileLockError;
|
|
59
|
+
const DEFAULT_TIMEOUT_MS = 5_000;
|
|
60
|
+
const DEFAULT_POLL_MS = 25;
|
|
61
|
+
/**
|
|
62
|
+
* Test-only seams. `afterTicket` runs once our ticket exists (before the
|
|
63
|
+
* choosing marker is withdrawn); `betweenScans` runs between the marker
|
|
64
|
+
* scan and the ticket scan of each admission check. Either may throw to
|
|
65
|
+
* simulate a failure at that point. Never set by production code.
|
|
66
|
+
*/
|
|
67
|
+
export const _testHooks = {};
|
|
68
|
+
// Tokens this process currently holds (ticket or marker on disk that a
|
|
69
|
+
// live acquisition in this module instance owns). One module instance per
|
|
70
|
+
// process is assumed: worker threads or a second copy of this module would
|
|
71
|
+
// keep separate sets and could sweep each other's live files. sigil-mcp
|
|
72
|
+
// is single-threaded and loads this once.
|
|
73
|
+
const held = new Set();
|
|
74
|
+
function sleepSync(ms) {
|
|
75
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
76
|
+
}
|
|
77
|
+
export function writeAllSync(fd, data) {
|
|
78
|
+
const buf = Buffer.from(data, 'utf8');
|
|
79
|
+
let written = 0;
|
|
80
|
+
while (written < buf.length) {
|
|
81
|
+
const n = writeSync(fd, buf, written, buf.length - written);
|
|
82
|
+
if (n <= 0)
|
|
83
|
+
throw new Error(`short write (${written}/${buf.length} bytes)`);
|
|
84
|
+
written += n;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function isAlive(pid) {
|
|
88
|
+
try {
|
|
89
|
+
process.kill(pid, 0);
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
catch (err) {
|
|
93
|
+
// EPERM means the pid exists but belongs to another user — still alive.
|
|
94
|
+
return err.code === 'EPERM';
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const TICKET_RE = /^t-(\d+)-(\d+)-([0-9a-f]{16})$/;
|
|
98
|
+
const CHOOSING_RE = /^c-(\d+)-([0-9a-f]{16})$/;
|
|
99
|
+
function parseEntry(name) {
|
|
100
|
+
const t = TICKET_RE.exec(name);
|
|
101
|
+
if (t)
|
|
102
|
+
return { name, kind: 'ticket', seq: Number(t[1]), pid: Number(t[2]), token: t[3] };
|
|
103
|
+
const c = CHOOSING_RE.exec(name);
|
|
104
|
+
if (c)
|
|
105
|
+
return { name, kind: 'choosing', seq: 0, pid: Number(c[1]), token: c[2] };
|
|
106
|
+
return null; // not ours to interpret, never touched
|
|
107
|
+
}
|
|
108
|
+
function before(a, b) {
|
|
109
|
+
if (a.seq !== b.seq)
|
|
110
|
+
return a.seq < b.seq;
|
|
111
|
+
if (a.pid !== b.pid)
|
|
112
|
+
return a.pid < b.pid;
|
|
113
|
+
return a.token < b.token;
|
|
114
|
+
}
|
|
115
|
+
/** Exclusive create of an empty file; if close fails the file is removed. */
|
|
116
|
+
function createExclusive(path) {
|
|
117
|
+
const fd = openSync(path, 'wx', 0o600);
|
|
118
|
+
try {
|
|
119
|
+
closeSync(fd);
|
|
120
|
+
}
|
|
121
|
+
catch (err) {
|
|
122
|
+
unlinkQuiet(path);
|
|
123
|
+
throw err;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function unlinkQuiet(path) {
|
|
127
|
+
try {
|
|
128
|
+
unlinkSync(path);
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
/* already gone, or will be swept later */
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/** Unlink that treats ENOENT as success and rethrows anything else. */
|
|
135
|
+
function unlinkOwn(path) {
|
|
136
|
+
try {
|
|
137
|
+
unlinkSync(path);
|
|
138
|
+
}
|
|
139
|
+
catch (err) {
|
|
140
|
+
if (err.code !== 'ENOENT')
|
|
141
|
+
throw err;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* List the live contenders in `dir`, sweeping entries whose owner cannot
|
|
146
|
+
* act: dead pids, and this pid's own files that no live acquisition holds
|
|
147
|
+
* (an orphan of a failed release, or a dead predecessor that had this
|
|
148
|
+
* pid). Our own held entries are returned too, so a nested acquire sees
|
|
149
|
+
* the outer ticket as a contender and times out rather than deadlocking
|
|
150
|
+
* silently or entering twice.
|
|
151
|
+
*/
|
|
152
|
+
function liveEntries(dir) {
|
|
153
|
+
const out = [];
|
|
154
|
+
for (const name of readdirSync(dir)) {
|
|
155
|
+
const e = parseEntry(name);
|
|
156
|
+
if (!e)
|
|
157
|
+
continue;
|
|
158
|
+
const sweep = e.pid === process.pid ? !held.has(e.token) : !isAlive(e.pid);
|
|
159
|
+
if (sweep) {
|
|
160
|
+
unlinkQuiet(join(dir, name));
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
out.push(e);
|
|
164
|
+
}
|
|
165
|
+
return out;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Admission check. Two separate enumerations, in this order:
|
|
169
|
+
* 1. is any other live process choosing? (if so, not admitted)
|
|
170
|
+
* 2. does any other live ticket order before mine?
|
|
171
|
+
*
|
|
172
|
+
* Why two: readdir is not a snapshot. A contender that withdraws its
|
|
173
|
+
* marker and publishes its ticket while one enumeration is in flight can
|
|
174
|
+
* be missed by that enumeration on both counts. With the marker read
|
|
175
|
+
* strictly before the ticket read, the argument is:
|
|
176
|
+
* - If C's marker existed for the whole of scan 1, we see it and retry.
|
|
177
|
+
* - If C's marker was created after scan 1 began, C picks its number
|
|
178
|
+
* after that, and our ticket (created before scan 1) is present for
|
|
179
|
+
* the whole of C's selection scan, so C's number is larger than ours.
|
|
180
|
+
* - If C's marker was withdrawn during scan 1, C's ticket already existed
|
|
181
|
+
* when scan 1 ended, so it is present for the whole of scan 2 and we
|
|
182
|
+
* see it.
|
|
183
|
+
* This is exactly Lamport's per-variable "wait until not choosing, then
|
|
184
|
+
* read number", with each variable read as one enumeration.
|
|
185
|
+
*/
|
|
186
|
+
function admitted(dir, mine) {
|
|
187
|
+
const others = (es) => es.filter((e) => e.token !== mine.token);
|
|
188
|
+
if (others(liveEntries(dir)).some((e) => e.kind === 'choosing'))
|
|
189
|
+
return false;
|
|
190
|
+
_testHooks.betweenScans?.(dir);
|
|
191
|
+
return !others(liveEntries(dir)).some((e) => e.kind === 'ticket' && before(e, mine));
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Acquire an exclusive cross-process lock over `lockDir` (created if
|
|
195
|
+
* absent). Returns an idempotent, retryable release function. Throws
|
|
196
|
+
* FileLockError if the lock cannot be acquired within `timeoutMs`. Any
|
|
197
|
+
* failure to acquire — timeout or error — withdraws our files first.
|
|
198
|
+
*/
|
|
199
|
+
export function acquireLockSync(lockDir, opts = {}) {
|
|
200
|
+
const timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
201
|
+
const pollMs = opts.pollMs ?? DEFAULT_POLL_MS;
|
|
202
|
+
const deadline = Date.now() + timeoutMs;
|
|
203
|
+
mkdirSync(lockDir, { recursive: true, mode: 0o700 });
|
|
204
|
+
const token = randomBytes(8).toString('hex');
|
|
205
|
+
const markerPath = join(lockDir, `c-${process.pid}-${token}`);
|
|
206
|
+
let ticketPath;
|
|
207
|
+
let markerOnDisk = false;
|
|
208
|
+
// Withdraw everything we put on disk. Throws if something we own could
|
|
209
|
+
// not be removed (the caller's retry path handles that); the held flag
|
|
210
|
+
// is dropped first so a leftover is recognisably our own orphan.
|
|
211
|
+
const withdraw = () => {
|
|
212
|
+
held.delete(token);
|
|
213
|
+
if (ticketPath)
|
|
214
|
+
unlinkOwn(ticketPath);
|
|
215
|
+
if (markerOnDisk)
|
|
216
|
+
unlinkOwn(markerPath);
|
|
217
|
+
markerOnDisk = false;
|
|
218
|
+
};
|
|
219
|
+
let mine;
|
|
220
|
+
try {
|
|
221
|
+
held.add(token);
|
|
222
|
+
createExclusive(markerPath);
|
|
223
|
+
markerOnDisk = true;
|
|
224
|
+
let max = 0;
|
|
225
|
+
for (const e of liveEntries(lockDir))
|
|
226
|
+
if (e.kind === 'ticket' && e.seq > max)
|
|
227
|
+
max = e.seq;
|
|
228
|
+
const seq = max + 1;
|
|
229
|
+
// Fail closed rather than hand out a number that isn't strictly larger
|
|
230
|
+
// than a live holder's: past MAX_SAFE_INTEGER, max + 1 === max, and two
|
|
231
|
+
// holders with the same number could both be admitted. One line per
|
|
232
|
+
// sign op, so this is unreachable in practice; refusing is still safer
|
|
233
|
+
// than a silent tie. (Withdraw runs via the catch below.)
|
|
234
|
+
if (!Number.isSafeInteger(seq) || seq <= max) {
|
|
235
|
+
throw new FileLockError(`ticket number space exhausted in ${lockDir}`);
|
|
236
|
+
}
|
|
237
|
+
mine = {
|
|
238
|
+
name: `t-${seq}-${process.pid}-${token}`,
|
|
239
|
+
kind: 'ticket',
|
|
240
|
+
seq,
|
|
241
|
+
pid: process.pid,
|
|
242
|
+
token,
|
|
243
|
+
};
|
|
244
|
+
ticketPath = join(lockDir, mine.name);
|
|
245
|
+
createExclusive(ticketPath);
|
|
246
|
+
_testHooks.afterTicket?.(lockDir);
|
|
247
|
+
// A marker we fail to withdraw now is retried at release; meanwhile it
|
|
248
|
+
// is ours (held), so our own admission check ignores it.
|
|
249
|
+
try {
|
|
250
|
+
unlinkOwn(markerPath);
|
|
251
|
+
markerOnDisk = false;
|
|
252
|
+
}
|
|
253
|
+
catch {
|
|
254
|
+
/* release() will retry */
|
|
255
|
+
}
|
|
256
|
+
for (;;) {
|
|
257
|
+
if (admitted(lockDir, mine))
|
|
258
|
+
break;
|
|
259
|
+
const remaining = deadline - Date.now();
|
|
260
|
+
if (remaining <= 0) {
|
|
261
|
+
throw new FileLockError(`timed out after ${timeoutMs}ms waiting for ${lockDir}`);
|
|
262
|
+
}
|
|
263
|
+
sleepSync(Math.min(pollMs, remaining));
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
catch (err) {
|
|
267
|
+
try {
|
|
268
|
+
withdraw();
|
|
269
|
+
}
|
|
270
|
+
catch {
|
|
271
|
+
/* our leftovers are held-less now; this process sweeps them next time */
|
|
272
|
+
}
|
|
273
|
+
throw err;
|
|
274
|
+
}
|
|
275
|
+
let released = false;
|
|
276
|
+
return () => {
|
|
277
|
+
if (released)
|
|
278
|
+
return;
|
|
279
|
+
withdraw(); // throws on failure, leaving the closure retryable
|
|
280
|
+
released = true;
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
/** Sidecar lock directory for a data file: `<target>.lock.d`. */
|
|
284
|
+
export function lockPathFor(target) {
|
|
285
|
+
return `${target}.lock.d`;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Run `fn` while holding the sidecar lock for `target`. Synchronous by
|
|
289
|
+
* design (callers are synchronous append paths) and therefore not
|
|
290
|
+
* re-entrant: a nested acquire of the same target times out.
|
|
291
|
+
*/
|
|
292
|
+
export function withFileLock(target, fn, opts = {}) {
|
|
293
|
+
const lockDir = lockPathFor(target);
|
|
294
|
+
const release = acquireLockSync(lockDir, opts);
|
|
295
|
+
try {
|
|
296
|
+
return fn();
|
|
297
|
+
}
|
|
298
|
+
finally {
|
|
299
|
+
releaseWithRetry(release, lockDir);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* A ticket or marker with a live pid blocks every other session until it
|
|
304
|
+
* is gone, so a release that fails would stall them until this process
|
|
305
|
+
* exits. Retry a transient failure with backoff; if it still won't go,
|
|
306
|
+
* report on stderr — never replace fn's outcome (a committed append must
|
|
307
|
+
* not be reported as a failure, and fn's own error must not be masked).
|
|
308
|
+
* The files are not lost for good: no live acquisition holds their token
|
|
309
|
+
* any more, so this process sweeps them as its own orphans on its next
|
|
310
|
+
* acquire.
|
|
311
|
+
*/
|
|
312
|
+
const RELEASE_ATTEMPTS = 5;
|
|
313
|
+
export function releaseWithRetry(release, lockDir) {
|
|
314
|
+
let last;
|
|
315
|
+
for (let i = 0; i < RELEASE_ATTEMPTS; i++) {
|
|
316
|
+
try {
|
|
317
|
+
release();
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
catch (err) {
|
|
321
|
+
last = err;
|
|
322
|
+
sleepSync(10 * (i + 1));
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
process.stderr.write(`sigil: failed to release a ticket in ${lockDir} after ${RELEASE_ATTEMPTS} attempts ` +
|
|
326
|
+
`(${last?.message ?? String(last)}); this process will sweep it on its ` +
|
|
327
|
+
`next acquire, other sessions will wait until then\n`);
|
|
328
|
+
}
|
|
329
|
+
//# sourceMappingURL=lock.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lock.js","sourceRoot":"","sources":["../../../src/fs/lock.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,QAAQ,EAAE,WAAW,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAC7F,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH,MAAM,OAAO,aAAc,SAAQ,KAAK;IACtC,YAAY,GAAW;QACrB,KAAK,CAAC,oBAAoB,GAAG,EAAE,CAAC,CAAC;QACjC,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;IAC9B,CAAC;CACF;AACD,oEAAoE;AACpE,MAAM,CAAC,MAAM,cAAc,GAAG,aAAa,CAAC;AAO5C,MAAM,kBAAkB,GAAG,KAAK,CAAC;AACjC,MAAM,eAAe,GAAG,EAAE,CAAC;AAE3B;;;;;GAKG;AACH,MAAM,CAAC,MAAM,UAAU,GAGnB,EAAE,CAAC;AAEP,uEAAuE;AACvE,0EAA0E;AAC1E,2EAA2E;AAC3E,wEAAwE;AACxE,0CAA0C;AAC1C,MAAM,IAAI,GAAG,IAAI,GAAG,EAAU,CAAC;AAE/B,SAAS,SAAS,CAAC,EAAU;IAC3B,OAAO,CAAC,IAAI,CAAC,IAAI,UAAU,CA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AE,CAAC,EAAE,EAAE,CAAC;QAC1C,IAAI,CAAC;YACH,OAAO,EAAE,CAAC;YACV,OAAO;QACT,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,IAAI,GAAG,GAAG,CAAC;YACX,SAAS,CAAC,EAAE,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QAC1B,CAAC;IACH,CAAC;IACD,OAAO,CAAC,MAAM,CAAC,KAAK,CAClB,wCAAwC,OAAO,UAAU,gBAAgB,YAAY;QACnF,IAAK,IAAc,EAAE,OAAO,IAAI,MAAM,CAAC,IAAI,CAAC,uCAAuC;QACnF,qDAAqD,CACxD,CAAC;AACJ,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"evaluate.d.ts","sourceRoot":"","sources":["../../../src/policy/evaluate.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,cAAc,EAAE,aAAa,EAAmB,MAAM,YAAY,CAAC;AAEzF;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,QAAQ,CAAC,OAAO,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,GAAG,cAAc,
|
|
1
|
+
{"version":3,"file":"evaluate.d.ts","sourceRoot":"","sources":["../../../src/policy/evaluate.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,cAAc,EAAE,aAAa,EAAmB,MAAM,YAAY,CAAC;AAEzF;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,QAAQ,CAAC,OAAO,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,GAAG,cAAc,CAmD/E"}
|
|
@@ -47,13 +47,15 @@ export function evaluate(request, policy) {
|
|
|
47
47
|
kind: 'deny',
|
|
48
48
|
reason: 'personal_sign denied — strict mode + allow_message_signing=false',
|
|
49
49
|
};
|
|
50
|
-
case 'typed_data':
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
: {
|
|
50
|
+
case 'typed_data': {
|
|
51
|
+
if (!policy.allowTypedData) {
|
|
52
|
+
return {
|
|
54
53
|
kind: 'deny',
|
|
55
54
|
reason: 'EIP-712 typed-data denied — strict mode + allow_typed_data=false',
|
|
56
55
|
};
|
|
56
|
+
}
|
|
57
|
+
return evaluateTypedDataStrict(request.typedData, policy) ?? { kind: 'allow' };
|
|
58
|
+
}
|
|
57
59
|
case 'svm_message':
|
|
58
60
|
return policy.allowSvmMessageSigning
|
|
59
61
|
? { kind: 'allow' }
|
|
@@ -238,4 +240,67 @@ function formatWei(v) {
|
|
|
238
240
|
const fracStr = frac.toString().padStart(18, '0').slice(0, 6).replace(/0+$/, '');
|
|
239
241
|
return fracStr === '' ? whole.toString() : `${whole}.${fracStr}`;
|
|
240
242
|
}
|
|
243
|
+
/**
|
|
244
|
+
* Strict-mode EIP-712 checks, applied once allow_typed_data has passed.
|
|
245
|
+
* A typed-data signature can move funds (Permit, Permit2, exchange orders),
|
|
246
|
+
* so the domain gets the same treatment a transaction's `to` does:
|
|
247
|
+
* 1. domain.chainId must be present and in chain_ids — a signature for
|
|
248
|
+
* another chain is exactly the kind of thing an injected prompt asks
|
|
249
|
+
* for, and a chain-less domain can't be checked at all.
|
|
250
|
+
* 2. domain.verifyingContract must be allowlisted when the list is set.
|
|
251
|
+
* 3. primaryType must be allowlisted when the list is set.
|
|
252
|
+
* Domain fields are read defensively: the evaluator runs before sign-typed.ts
|
|
253
|
+
* validates the structure, and a malformed domain must deny, not throw.
|
|
254
|
+
*/
|
|
255
|
+
function evaluateTypedDataStrict(td, policy) {
|
|
256
|
+
if (typeof td.domain !== 'object' || td.domain === null || Array.isArray(td.domain)) {
|
|
257
|
+
return { kind: 'deny', reason: 'EIP-712 denied — domain must be an object' };
|
|
258
|
+
}
|
|
259
|
+
const domain = td.domain;
|
|
260
|
+
const chainId = domain['chainId'];
|
|
261
|
+
if (chainId === undefined) {
|
|
262
|
+
// A chain-less domain can't be checked against chain_ids, and a
|
|
263
|
+
// signature over one is valid wherever that domain is accepted. Strict
|
|
264
|
+
// mode therefore requires the domain to be chain-bound.
|
|
265
|
+
return {
|
|
266
|
+
kind: 'deny',
|
|
267
|
+
reason: 'EIP-712 denied — strict mode requires domain.chainId (chain-less domains cannot be checked against chain_ids)',
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
const id = exactChainId(chainId);
|
|
271
|
+
if (id === null || !policy.chainIds.some((c) => BigInt(c) === id)) {
|
|
272
|
+
return {
|
|
273
|
+
kind: 'deny',
|
|
274
|
+
reason: `EIP-712 denied — domain.chainId ${String(chainId)} not in chain_ids ${JSON.stringify(policy.chainIds)}`,
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
if (policy.typedDataVerifyingContracts.length > 0) {
|
|
278
|
+
const vc = domain['verifyingContract'];
|
|
279
|
+
const norm = typeof vc === 'string' ? vc.toLowerCase() : undefined;
|
|
280
|
+
if (norm === undefined || !policy.typedDataVerifyingContracts.includes(norm)) {
|
|
281
|
+
return {
|
|
282
|
+
kind: 'deny',
|
|
283
|
+
reason: `EIP-712 denied — domain.verifyingContract ${norm ?? '(absent)'} not in typed_data_verifying_contracts`,
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
if (policy.typedDataPrimaryTypes.length > 0) {
|
|
288
|
+
const pt = td.primaryType;
|
|
289
|
+
if (typeof pt !== 'string' || !policy.typedDataPrimaryTypes.includes(pt)) {
|
|
290
|
+
return {
|
|
291
|
+
kind: 'deny',
|
|
292
|
+
reason: `EIP-712 denied — primaryType ${typeof pt === 'string' ? pt : '(absent)'} not in typed_data_primary_types`,
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
298
|
+
/** Exact integer chain id as a bigint, or null if it isn't a safe integer / bigint. */
|
|
299
|
+
function exactChainId(v) {
|
|
300
|
+
if (typeof v === 'bigint')
|
|
301
|
+
return v >= 0n ? v : null;
|
|
302
|
+
if (typeof v === 'number')
|
|
303
|
+
return Number.isSafeInteger(v) && v >= 0 ? BigInt(v) : null;
|
|
304
|
+
return null;
|
|
305
|
+
}
|
|
241
306
|
//# sourceMappingURL=evaluate.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"evaluate.js","sourceRoot":"","sources":["../../../src/policy/evaluate.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,QAAQ,CAAC,OAAsB,EAAE,MAAc;IAC7D,IAAI,MAAM,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;QACjC,IAAI,OAAO,CAAC,IAAI,KAAK,aAAa,EAAE,CAAC;YACnC,MAAM,OAAO,GAAG,YAAY,CAAC,OAAO,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;YACjD,IAAI,OAAO;gBAAE,OAAO,OAAO,CAAC;QAC9B,CAAC;QACD,IAAI,OAAO,CAAC,IAAI,KAAK,iBAAiB,EAAE,CAAC;YACvC,MAAM,OAAO,GAAG,eAAe,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;YACjD,IAAI,OAAO;gBAAE,OAAO,OAAO,CAAC;QAC9B,CAAC;QACD,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;IAC3B,CAAC;IACD,QAAQ,OAAO,CAAC,IAAI,EAAE,CAAC;QACrB,KAAK,aAAa,CAAC,CAAC,CAAC;YACnB,MAAM,QAAQ,GAAG,yBAAyB,CAAC,OAAO,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;YAC/D,IAAI,QAAQ;gBAAE,OAAO,QAAQ,CAAC;YAC9B,MAAM,OAAO,GAAG,YAAY,CAAC,OAAO,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;YACjD,IAAI,OAAO;gBAAE,OAAO,OAAO,CAAC;YAC5B,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;QAC3B,CAAC;QACD,KAAK,SAAS;YACZ,OAAO,MAAM,CAAC,mBAAmB;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MAAM,CAAC,2BAA2B,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;YAC7E,OAAO;gBACL,IAAI,EAAE,MAAM;gBACZ,MAAM,EAAE,6CAA6C,IAAI,IAAI,UAAU,wCAAwC;aAChH,CAAC;QACJ,CAAC;IACH,CAAC;IACD,IAAI,MAAM,CAAC,qBAAqB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC5C,MAAM,EAAE,GAAG,EAAE,CAAC,WAAW,CAAC;QAC1B,IAAI,OAAO,EAAE,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,qBAAqB,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC;YACzE,OAAO;gBACL,IAAI,EAAE,MAAM;gBACZ,MAAM,EAAE,gCAAgC,OAAO,EAAE,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,UAAU,kCAAkC;aACnH,CAAC;QACJ,CAAC;IACH,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,uFAAuF;AACvF,SAAS,YAAY,CAAC,CAAU;IAC9B,IAAI,OAAO,CAAC,KAAK,QAAQ;QAAE,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IACrD,IAAI,OAAO,CAAC,KAAK,QAAQ;QAAE,OAAO,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IACvF,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
@@ -2,4 +2,6 @@ export { type Policy, type PolicyRequest, type PolicyDecision, type PolicyResolv
|
|
|
2
2
|
export { parsePolicy, FileSystemPolicyResolver, permissivePolicyResolver } from './loader.js';
|
|
3
3
|
export { evaluate } from './evaluate.js';
|
|
4
4
|
export { type PolicyMode, PERMISSIVE_TEMPLATE, STRICT_TEMPLATE, policyTemplate, } from './template.js';
|
|
5
|
+
export { type SpendAsset, type WindowCap, HOUR_MS, DAY_MS, windowCapsFor, checkWindowCaps, } from './window.js';
|
|
6
|
+
export { type SpendLedger, type SpendLedgerOpts, SpendLedgerError, MemorySpendLedger, FileSpendLedger, _ledgerTestHooks, } from './ledger.js';
|
|
5
7
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/policy/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,MAAM,EACX,KAAK,aAAa,EAClB,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,eAAe,GAChB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,WAAW,EAAE,wBAAwB,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAC9F,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EACL,KAAK,UAAU,EACf,mBAAmB,EACnB,eAAe,EACf,cAAc,GACf,MAAM,eAAe,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/policy/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,MAAM,EACX,KAAK,aAAa,EAClB,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,eAAe,GAChB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,WAAW,EAAE,wBAAwB,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAC9F,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EACL,KAAK,UAAU,EACf,mBAAmB,EACnB,eAAe,EACf,cAAc,GACf,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,KAAK,UAAU,EACf,KAAK,SAAS,EACd,OAAO,EACP,MAAM,EACN,aAAa,EACb,eAAe,GAChB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,KAAK,WAAW,EAChB,KAAK,eAAe,EACpB,gBAAgB,EAChB,iBAAiB,EACjB,eAAe,EACf,gBAAgB,GACjB,MAAM,aAAa,CAAC"}
|
package/dist/src/policy/index.js
CHANGED
|
@@ -2,4 +2,6 @@ export { PolicyLoadError, } from './types.js';
|
|
|
2
2
|
export { parsePolicy, FileSystemPolicyResolver, permissivePolicyResolver } from './loader.js';
|
|
3
3
|
export { evaluate } from './evaluate.js';
|
|
4
4
|
export { PERMISSIVE_TEMPLATE, STRICT_TEMPLATE, policyTemplate, } from './template.js';
|
|
5
|
+
export { HOUR_MS, DAY_MS, windowCapsFor, checkWindowCaps, } from './window.js';
|
|
6
|
+
export { SpendLedgerError, MemorySpendLedger, FileSpendLedger, _ledgerTestHooks, } from './ledger.js';
|
|
5
7
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/policy/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,eAAe,GAChB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,WAAW,EAAE,wBAAwB,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAC9F,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAEL,mBAAmB,EACnB,eAAe,EACf,cAAc,GACf,MAAM,eAAe,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/policy/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,eAAe,GAChB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,WAAW,EAAE,wBAAwB,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAC9F,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAEL,mBAAmB,EACnB,eAAe,EACf,cAAc,GACf,MAAM,eAAe,CAAC;AACvB,OAAO,EAGL,OAAO,EACP,MAAM,EACN,aAAa,EACb,eAAe,GAChB,MAAM,aAAa,CAAC;AACrB,OAAO,EAGL,gBAAgB,EAChB,iBAAiB,EACjB,eAAe,EACf,gBAAgB,GACjB,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { type SpendAsset, type WindowCap } from './window.js';
|
|
2
|
+
/**
|
|
3
|
+
* What the sign path needs from the spend ledger. `reserve` is the only
|
|
4
|
+
* mutating call: it checks every cap against what is already recorded and,
|
|
5
|
+
* only if all hold, records the new amount — atomically with respect to
|
|
6
|
+
* other sigil-mcp processes, so two windows can't both squeeze under a cap
|
|
7
|
+
* in the same instant.
|
|
8
|
+
*
|
|
9
|
+
* The ledger owns the clock. Time is sampled *inside* the critical section
|
|
10
|
+
* so a process that read the clock, got descheduled, and then took the lock
|
|
11
|
+
* cannot judge a competitor's later spend as "in the future" and ignore it.
|
|
12
|
+
* Entries dated after now (another process's later sample, or a clock that
|
|
13
|
+
* stepped back) are always counted: a spend that has happened has happened.
|
|
14
|
+
*/
|
|
15
|
+
export interface SpendLedger {
|
|
16
|
+
/** Total recorded for (handle, asset) with ts > now - windowMs, including any ts > now. */
|
|
17
|
+
spent(handle: string, asset: SpendAsset, windowMs: number): bigint;
|
|
18
|
+
/**
|
|
19
|
+
* Check-then-record. Returns null on success (amount recorded), or the
|
|
20
|
+
* deny reason (nothing recorded). A zero amount is a no-op success.
|
|
21
|
+
*/
|
|
22
|
+
reserve(handle: string, asset: SpendAsset, amount: bigint, caps: readonly WindowCap[]): string | null;
|
|
23
|
+
}
|
|
24
|
+
export declare class SpendLedgerError extends Error {
|
|
25
|
+
constructor(message: string);
|
|
26
|
+
}
|
|
27
|
+
export interface SpendLedgerOpts {
|
|
28
|
+
/** Clock override for tests. Defaults to Date.now. */
|
|
29
|
+
now?: () => number;
|
|
30
|
+
}
|
|
31
|
+
/** In-memory ledger for tests and for callers that don't persist. */
|
|
32
|
+
export declare class MemorySpendLedger implements SpendLedger {
|
|
33
|
+
#private;
|
|
34
|
+
constructor(opts?: SpendLedgerOpts);
|
|
35
|
+
spent(handle: string, asset: SpendAsset, windowMs: number): bigint;
|
|
36
|
+
reserve(handle: string, asset: SpendAsset, amount: bigint, caps: readonly WindowCap[]): string | null;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* File-backed ledger: one append-only JSONL file per portal at
|
|
40
|
+
* `<stateDir>/<handle>.ledger`, lines of `{"ts":…,"asset":"wei","amount":"…"}`.
|
|
41
|
+
*
|
|
42
|
+
* Every read and write runs under the cross-process file lock, the clock is
|
|
43
|
+
* sampled inside it, and each `reserve` re-reads the file before deciding,
|
|
44
|
+
* so the decision is always made against the union of every window's
|
|
45
|
+
* spends.
|
|
46
|
+
*
|
|
47
|
+
* Integrity is fail-closed: a line that does not parse makes every reserve
|
|
48
|
+
* for that portal deny with a message naming the file, until a human fixes
|
|
49
|
+
* or removes it. Skipping bad lines would silently loosen the cap, which is
|
|
50
|
+
* the one thing a rate limit must not do. A crash can leave a torn final
|
|
51
|
+
* line (writes are complete and fsynced, so only power loss does this); the
|
|
52
|
+
* next append starts on a fresh line so the torn fragment stays isolated —
|
|
53
|
+
* and still trips the check, on purpose.
|
|
54
|
+
*
|
|
55
|
+
* Entries older than the longest window sigil supports (24h) can never
|
|
56
|
+
* influence a decision again; when more than COMPACT_AFTER of them have
|
|
57
|
+
* piled up the file is rewritten (tmp + fsync + rename + directory fsync,
|
|
58
|
+
* under the same lock) with only the live tail. Entries dated in the future
|
|
59
|
+
* are always kept.
|
|
60
|
+
*
|
|
61
|
+
* Threat model: an attacker who can delete or rewrite this file can also
|
|
62
|
+
* rewrite the policy file beside it and remove the cap outright, so the
|
|
63
|
+
* ledger earns no extra protection beyond its 0600 mode inside the 0700
|
|
64
|
+
* sigil home; a missing file is an empty ledger. What it does defend
|
|
65
|
+
* against is the prompt-injected *agent*, which never touches the
|
|
66
|
+
* filesystem directly. The hash-chained audit log is the record.
|
|
67
|
+
*/
|
|
68
|
+
export declare class FileSpendLedger implements SpendLedger {
|
|
69
|
+
#private;
|
|
70
|
+
static readonly COMPACT_AFTER = 256;
|
|
71
|
+
constructor(stateDir: string, opts?: SpendLedgerOpts);
|
|
72
|
+
pathFor(handle: string): string;
|
|
73
|
+
spent(handle: string, asset: SpendAsset, windowMs: number): bigint;
|
|
74
|
+
reserve(handle: string, asset: SpendAsset, amount: bigint, caps: readonly WindowCap[]): string | null;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Test-only seam: replaces the directory fsync so a test can make it fail.
|
|
78
|
+
* Never set by production code.
|
|
79
|
+
*/
|
|
80
|
+
export declare const _ledgerTestHooks: {
|
|
81
|
+
fsyncDir?: (dir: string) => void;
|
|
82
|
+
};
|
|
83
|
+
//# sourceMappingURL=ledger.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ledger.d.ts","sourceRoot":"","sources":["../../../src/policy/ledger.ts"],"names":[],"mappings":"AAGA,OAAO,EAA2B,KAAK,UAAU,EAAE,KAAK,SAAS,EAAE,MAAM,aAAa,CAAC;AAEvF;;;;;;;;;;;;GAYG;AACH,MAAM,WAAW,WAAW;IAC1B,2FAA2F;IAC3F,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAC;IACnE;;;OAGG;IACH,OAAO,CACL,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,UAAU,EACjB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,SAAS,SAAS,EAAE,GACzB,MAAM,GAAG,IAAI,CAAC;CAClB;AAED,qBAAa,gBAAiB,SAAQ,KAAK;gBAC7B,OAAO,EAAE,MAAM;CAI5B;AAKD,MAAM,WAAW,eAAe;IAC9B,sDAAsD;IACtD,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CACpB;AAsBD,qEAAqE;AACrE,qBAAa,iBAAkB,YAAW,WAAW;;gBAIvC,IAAI,GAAE,eAAoB;IAatC,KAAK,CA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