@cortexkit/opencode-openai-auth 1.3.0 → 1.4.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/dist/bin.js +8 -8
- package/dist/{codex-request-B9D0e97Q.d.ts → codex-request-hw0XcldE.d.ts} +27 -27
- package/dist/index-1eq2s1cp.js +2 -0
- package/dist/{index-m3ctrabv.js → index-1tdnrt68.js} +1 -1
- package/dist/{index-8x1aj7q3.js → index-5230x6s8.js} +2 -2
- package/dist/{index-gn2b097f.js → index-ag04nyv7.js} +7 -7
- package/dist/index-brbqqer5.js +90 -0
- package/dist/{index-xwvze96g.js → index-d98f1xdp.js} +1 -1
- package/dist/index-jcyrm58f.js +65 -0
- package/dist/{index-2pgkrebq.js → index-rsvnabbe.js} +2 -2
- package/dist/{index-ftkhhr6d.js → index-s88n2bjn.js} +1 -1
- package/dist/{index-bc89akak.js → index-t5m0dhmq.js} +1 -1
- package/dist/index-vy729a0b.js +69 -0
- package/dist/index-wgwze391.js +2 -0
- package/dist/{index-tddr876f.js → index-yhw3hnsh.js} +2 -2
- package/dist/index.d.ts +11 -3
- package/dist/index.js +1 -1
- package/dist/{model-BdITTmeb.d.ts → model-DphbMQce.d.ts} +1 -1
- package/dist/{port-file-DbHIVC9a.d.ts → port-file-Cx-wwe5Q.d.ts} +1 -1
- package/dist/{protocol-MysYPAho.d.ts → protocol-CDqdjh74.d.ts} +1 -1
- package/dist/rpc/port-file.d.ts +1 -1
- package/dist/rpc/port-file.js +1 -1
- package/dist/rpc/protocol.d.ts +1 -1
- package/dist/rpc/protocol.js +1 -1
- package/dist/rpc/rpc-client.d.ts +3 -3
- package/dist/rpc/rpc-client.js +1 -1
- package/dist/rpc/rpc-dir.js +1 -1
- package/dist/{sidebar-state-CVSok7zv.d.ts → sidebar-state-pTd9gg9W.d.ts} +55 -7
- package/dist/sidebar-state.d.ts +2 -2
- package/dist/sidebar-state.js +1 -1
- package/dist/{token-z6qq7a1d.js → token-0z5g59p8.js} +1 -1
- package/dist/token-util-jf9g6stk.js +1 -0
- package/dist/tui-preferences.js +1 -1
- package/dist/tui.d.ts +30 -3
- package/dist/v2/server.d.ts +2 -2
- package/dist/v2/server.js +2 -2
- package/package.json +4 -4
- package/src/tui-compiled/raw/shared/0332701351b3eeec-format.js +50 -5
- package/src/tui-compiled/raw/shared/0f77ebc3c914c9c6-custody.js +70 -1
- package/src/tui-compiled/raw/shared/169b2b4bf30909bc-consumer.js +88 -61
- package/src/tui-compiled/raw/shared/2577ec69f9973ae9-quota-normalize.js +45 -1
- package/src/tui-compiled/raw/shared/30f5bc480f31f51c-roster.js +100 -43
- package/src/tui-compiled/raw/shared/39c3cb05738fcf6d-projection.js +3 -1
- package/src/tui-compiled/raw/shared/42ae93ab554fcec2-open-url.js +7 -9
- package/src/tui-compiled/raw/shared/4825e03209d6ae3e-with-lock.js +2 -0
- package/src/tui-compiled/raw/shared/4f36d333d081d040-merge.js +17 -3
- package/src/tui-compiled/raw/shared/81dddd203f1b92a3-quota-writer.js +253 -0
- package/src/tui-compiled/raw/shared/905bffc0220f43d0-map.js +31 -6
- package/src/tui-compiled/raw/shared/abd062d633e29fd2-pool-routing.js +116 -12
- package/src/tui-compiled/raw/shared/bfbdd0f9aae56b47-pool-quota.js +15 -3
- package/src/tui-compiled/raw/shared/d0f0b6be200311af-insula.js +64 -2
- package/src/tui-compiled/raw/shared/d5a8f1061ee2f114-index.js +2 -2
- package/src/tui-compiled/raw/shared/dbc61cd405941828-accounts.js +5 -4
- package/src/tui-compiled/raw/shared/dc9290c2a7552ba6-watch.js +2 -1
- package/src/tui-compiled/raw/shared/e8d3c8da776ef9cd-index.js +1 -0
- package/src/tui-compiled/raw/sidebar-state.js +32 -3
- package/src/tui-compiled/raw/tui/v2-dialog.js +2 -1
- package/src/tui-compiled/raw/tui.tsx +170 -26
- package/src/tui-compiled/runtime/shared/0332701351b3eeec-format.js +50 -5
- package/src/tui-compiled/runtime/shared/0f77ebc3c914c9c6-custody.js +70 -1
- package/src/tui-compiled/runtime/shared/169b2b4bf30909bc-consumer.js +88 -61
- package/src/tui-compiled/runtime/shared/2577ec69f9973ae9-quota-normalize.js +45 -1
- package/src/tui-compiled/runtime/shared/30f5bc480f31f51c-roster.js +100 -43
- package/src/tui-compiled/runtime/shared/39c3cb05738fcf6d-projection.js +3 -1
- package/src/tui-compiled/runtime/shared/42ae93ab554fcec2-open-url.js +7 -9
- package/src/tui-compiled/runtime/shared/4825e03209d6ae3e-with-lock.js +2 -0
- package/src/tui-compiled/runtime/shared/4f36d333d081d040-merge.js +17 -3
- package/src/tui-compiled/runtime/shared/81dddd203f1b92a3-quota-writer.js +253 -0
- package/src/tui-compiled/runtime/shared/905bffc0220f43d0-map.js +31 -6
- package/src/tui-compiled/runtime/shared/abd062d633e29fd2-pool-routing.js +116 -12
- package/src/tui-compiled/runtime/shared/bfbdd0f9aae56b47-pool-quota.js +15 -3
- package/src/tui-compiled/runtime/shared/d0f0b6be200311af-insula.js +64 -2
- package/src/tui-compiled/runtime/shared/d5a8f1061ee2f114-index.js +2 -2
- package/src/tui-compiled/runtime/shared/dbc61cd405941828-accounts.js +5 -4
- package/src/tui-compiled/runtime/shared/dc9290c2a7552ba6-watch.js +2 -1
- package/src/tui-compiled/runtime/shared/e8d3c8da776ef9cd-index.js +1 -0
- package/src/tui-compiled/runtime/sidebar-state.js +32 -3
- package/src/tui-compiled/runtime/tui/v2-dialog.js +2 -1
- package/src/tui-compiled/runtime/tui.js +350 -219
- package/dist/index-1mbgsxsr.js +0 -2
- package/dist/index-4v2fs42x.js +0 -65
- package/dist/index-ex6nx3mq.js +0 -2
- package/dist/index-psjyexj3.js +0 -69
- package/dist/index-tn8x5tz8.js +0 -90
- package/dist/token-util-cf0nyh7n.js +0 -1
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
|
-
import {
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { acquireRefreshFileLock, LockContentionError, withLock, writeJsonAtomic, } from "./ec971142548df90e-index.js";
|
|
3
4
|
import { readPrivateText, windowsPrivateFiles } from "./375d9cca2def037d-private-files.js";
|
|
4
|
-
import { decodeQuotaMap, mergeQuotaObservation, } from "./d5a8f1061ee2f114-index.js";
|
|
5
|
+
import { decodeQuotaMap, isQuotaObservation, mergeQuotaObservation, } from "./d5a8f1061ee2f114-index.js";
|
|
5
6
|
import { ClaustrumConsumerError } from "./ca0fccaba1d87af4-errors.js";
|
|
6
7
|
import { acceptAccount, declineAccount, isDeclined, } from "./7c140680fc81be92-interlock.js";
|
|
8
|
+
import { enqueueVaultQuota, pendingVaultQuota, } from "./81dddd203f1b92a3-quota-writer.js";
|
|
9
|
+
export { drainVaultQuota } from "./81dddd203f1b92a3-quota-writer.js";
|
|
7
10
|
export const DEFAULT_ROUTE_PREFIX = 'vault:';
|
|
8
11
|
function hash(value) {
|
|
9
12
|
return createHash('sha256').update(value).digest('hex');
|
|
@@ -313,6 +316,7 @@ const WRITE_LOCK = {
|
|
|
313
316
|
name: 'claustrum-roster-write',
|
|
314
317
|
ttlMs: 10_000,
|
|
315
318
|
timeoutMs: 5_000,
|
|
319
|
+
renew: true,
|
|
316
320
|
};
|
|
317
321
|
const DISCOVERY_LEASE = { name: 'claustrum-roster', ttlMs: 30_000 };
|
|
318
322
|
/**
|
|
@@ -321,7 +325,7 @@ const DISCOVERY_LEASE = { name: 'claustrum-roster', ttlMs: 30_000 };
|
|
|
321
325
|
* before any rename performed inside `change`; if it throws, do not rename.
|
|
322
326
|
*/
|
|
323
327
|
export async function mutateVaultRoster(path, change, options = {}) {
|
|
324
|
-
return withLock(path, WRITE_LOCK, async (lock) => {
|
|
328
|
+
return withLock(path, { ...WRITE_LOCK, signal: options.signal }, async (lock) => {
|
|
325
329
|
const current = await readVaultRoster(path);
|
|
326
330
|
const { next, result } = await change(current, {
|
|
327
331
|
assertOwned: () => lock.assertOwned(),
|
|
@@ -333,6 +337,7 @@ export async function mutateVaultRoster(path, change, options = {}) {
|
|
|
333
337
|
await writeJsonAtomic(path, next, {
|
|
334
338
|
beforeRename: async () => {
|
|
335
339
|
await options.beforePublish?.();
|
|
340
|
+
options.signal?.throwIfAborted();
|
|
336
341
|
await lock.assertOwned();
|
|
337
342
|
},
|
|
338
343
|
});
|
|
@@ -415,55 +420,73 @@ function receiptFence(row, receipt) {
|
|
|
415
420
|
* write is fenced on the receipt the reading was taken with: it lands only
|
|
416
421
|
* while the row still holds that receipt's credential (as representative or
|
|
417
422
|
* alias) and account, so a slow read for a replaced account can never land on
|
|
418
|
-
* its successor.
|
|
423
|
+
* its successor. Resolves false when the fence refuses the reading against the
|
|
424
|
+
* roster as it stands now (on disk, plus readings this process has not
|
|
425
|
+
* written yet), and true once the reading is queued for the next write. The
|
|
426
|
+
* write applies the fence again, so a rebind between the two still refuses.
|
|
419
427
|
*/
|
|
420
|
-
export function recordVaultQuota(path, input) {
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
// A bound account is `unclaimed`: the vault's list has not named it, and
|
|
428
|
-
// the next projection keeps it as the account this credential last
|
|
429
|
-
// logged into until the vault names one.
|
|
430
|
-
const bound = fence === 'bind'
|
|
431
|
-
? { accountIdentity: input.accountIdentity, unclaimed: true }
|
|
432
|
-
: {};
|
|
433
|
-
return {
|
|
434
|
-
next: {
|
|
435
|
-
...current,
|
|
436
|
-
rows: current.rows.map((entry) => entry === row ? { ...entry, ...bound, quota } : entry),
|
|
437
|
-
},
|
|
438
|
-
result: true,
|
|
439
|
-
};
|
|
428
|
+
export async function recordVaultQuota(path, input) {
|
|
429
|
+
const { routeId, observation, ...receipt } = input;
|
|
430
|
+
return enqueueFencedVaultQuota(path, {
|
|
431
|
+
routeId,
|
|
432
|
+
observation,
|
|
433
|
+
accountIdentity: receipt.accountIdentity,
|
|
434
|
+
receipt,
|
|
440
435
|
});
|
|
441
436
|
}
|
|
437
|
+
// The answer callers rely on (false for a replaced account) comes from the
|
|
438
|
+
// fence, so it runs here against the current roster before anything is
|
|
439
|
+
// queued. The read takes no lock: the roster file is replaced atomically.
|
|
440
|
+
async function enqueueFencedVaultQuota(path, publication) {
|
|
441
|
+
if (!isQuotaObservation(publication.observation))
|
|
442
|
+
return false;
|
|
443
|
+
const current = pendingVaultQuota(path, await readVaultRoster(path));
|
|
444
|
+
if (!applyVaultQuota(current, publication).kept)
|
|
445
|
+
return false;
|
|
446
|
+
return enqueueVaultQuota(path, publication);
|
|
447
|
+
}
|
|
442
448
|
/**
|
|
443
449
|
* Merge a quota observation that belongs to an account rather than to a
|
|
444
450
|
* served credential, such as a background reading from insula. The write
|
|
445
451
|
* lands only while the row is still bound to that exact known account, so a
|
|
446
452
|
* reading taken before a replacement never reaches the successor. Unlike
|
|
447
453
|
* `recordVaultQuota` it never binds an account: a row without a known account
|
|
448
|
-
* is refused.
|
|
454
|
+
* is refused. Resolves false when the row is not bound to that account now,
|
|
455
|
+
* and true once the reading is queued, as `recordVaultQuota` does.
|
|
449
456
|
*/
|
|
450
457
|
export function recordVaultAccountQuota(path, input) {
|
|
451
|
-
return
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
};
|
|
466
|
-
|
|
458
|
+
return enqueueFencedVaultQuota(path, input);
|
|
459
|
+
}
|
|
460
|
+
/** Apply the same identity fence to memory overlays and durable quota batches. */
|
|
461
|
+
export function applyVaultQuota(current, input) {
|
|
462
|
+
const row = current?.rows.find((entry) => entry.routeId === input.routeId);
|
|
463
|
+
const fence = !row
|
|
464
|
+
? 'refuse'
|
|
465
|
+
: input.receipt
|
|
466
|
+
? receiptFence(row, input.receipt)
|
|
467
|
+
: row.accountIdentity !== undefined &&
|
|
468
|
+
row.accountIdentity === input.accountIdentity
|
|
469
|
+
? 'hold'
|
|
470
|
+
: 'refuse';
|
|
471
|
+
if (!current || !row || fence === 'refuse')
|
|
472
|
+
return { roster: current, kept: false };
|
|
473
|
+
const quota = mergeQuotaObservation(row.quota, input.observation);
|
|
474
|
+
// Binding remembers which account this credential proved, without claiming
|
|
475
|
+
// that the vault's discovery has named that account yet.
|
|
476
|
+
const bound = fence === 'bind'
|
|
477
|
+
? { accountIdentity: input.accountIdentity, unclaimed: true }
|
|
478
|
+
: {};
|
|
479
|
+
return {
|
|
480
|
+
roster: {
|
|
481
|
+
...current,
|
|
482
|
+
rows: current.rows.map((entry) => entry === row ? { ...entry, ...bound, quota } : entry),
|
|
483
|
+
},
|
|
484
|
+
kept: true,
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
const pendingDiscoveries = new Map();
|
|
488
|
+
export function forgetPendingVaultDiscovery(path) {
|
|
489
|
+
pendingDiscoveries.delete(resolve(path));
|
|
467
490
|
}
|
|
468
491
|
/**
|
|
469
492
|
* Discover and commit the roster. The lease covers the list call and the
|
|
@@ -471,12 +494,16 @@ export function recordVaultAccountQuota(path, input) {
|
|
|
471
494
|
* from a newer one, so a run that lost its lease is refused at commit. A
|
|
472
495
|
* failed list, or custody switching off mid-run, never writes anything; a
|
|
473
496
|
* peer holding the lease means its last committed roster is served instead.
|
|
497
|
+
* A write-lock timeout keeps the discovered inventory for the next poll,
|
|
498
|
+
* unless a peer has superseded its base view in the meantime.
|
|
474
499
|
*/
|
|
475
500
|
export async function refreshVaultRoster(options) {
|
|
476
501
|
options.signal?.throwIfAborted();
|
|
477
502
|
const active = async () => (await options.isActive?.()) ?? true;
|
|
478
|
-
if (!(await active()))
|
|
503
|
+
if (!(await active())) {
|
|
504
|
+
forgetPendingVaultDiscovery(options.path);
|
|
479
505
|
return undefined;
|
|
506
|
+
}
|
|
480
507
|
const lease = await acquireRefreshFileLock({
|
|
481
508
|
...DISCOVERY_LEASE,
|
|
482
509
|
path: options.path,
|
|
@@ -489,8 +516,25 @@ export async function refreshVaultRoster(options) {
|
|
|
489
516
|
throw new ClaustrumConsumerError('roster-busy', 'Claustrum account discovery is already in progress');
|
|
490
517
|
return persisted;
|
|
491
518
|
}
|
|
519
|
+
const key = resolve(options.path);
|
|
520
|
+
let discovered;
|
|
492
521
|
try {
|
|
493
|
-
const
|
|
522
|
+
const previous = await readVaultRoster(options.path);
|
|
523
|
+
const pending = pendingDiscoveries.get(key);
|
|
524
|
+
// Opaque view cursors cannot be ordered. Reuse a discovery only while no
|
|
525
|
+
// peer has committed a different view since it was read.
|
|
526
|
+
discovered =
|
|
527
|
+
pending &&
|
|
528
|
+
(pending.previousView === previous?.view ||
|
|
529
|
+
pending.inventory.view === previous?.view)
|
|
530
|
+
? pending
|
|
531
|
+
: {
|
|
532
|
+
inventory: await options.custody.discover(options.signal),
|
|
533
|
+
previousView: previous?.view,
|
|
534
|
+
};
|
|
535
|
+
pendingDiscoveries.delete(key);
|
|
536
|
+
const { inventory } = discovered;
|
|
537
|
+
options.onInventory?.(inventory);
|
|
494
538
|
options.signal?.throwIfAborted();
|
|
495
539
|
return await mutateVaultRoster(options.path, async (current) => {
|
|
496
540
|
if (!(await active()))
|
|
@@ -503,8 +547,21 @@ export async function refreshVaultRoster(options) {
|
|
|
503
547
|
const next = projectVaultRoster(current, inventory, projection);
|
|
504
548
|
const changed = JSON.stringify(current) !== JSON.stringify(next);
|
|
505
549
|
return { ...(changed && { next }), result: next };
|
|
550
|
+
}, {
|
|
551
|
+
beforePublish: async () => {
|
|
552
|
+
options.signal?.throwIfAborted();
|
|
553
|
+
await lease.assertOwned();
|
|
554
|
+
},
|
|
506
555
|
});
|
|
507
556
|
}
|
|
557
|
+
catch (error) {
|
|
558
|
+
if (error instanceof LockContentionError &&
|
|
559
|
+
discovered &&
|
|
560
|
+
!options.signal?.aborted &&
|
|
561
|
+
(await active()))
|
|
562
|
+
pendingDiscoveries.set(key, discovered);
|
|
563
|
+
throw error;
|
|
564
|
+
}
|
|
508
565
|
finally {
|
|
509
566
|
await lease.release();
|
|
510
567
|
}
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
// scope, orders the result the way the primitives expect, and carries each
|
|
7
7
|
// entry's (scope, label) so a decision names the limit it judged rather than
|
|
8
8
|
// the position it occupied.
|
|
9
|
-
import { ALL_SCOPE, readQuotaCounters, } from "./905bffc0220f43d0-map.js";
|
|
9
|
+
import { ALL_SCOPE, isQuotaCredits, readQuotaCounters, } from "./905bffc0220f43d0-map.js";
|
|
10
10
|
function project(entry) {
|
|
11
11
|
if (entry.kind === 'retired') {
|
|
12
12
|
return {
|
|
@@ -95,12 +95,14 @@ export function projectQuota(map, scope = ALL_SCOPE) {
|
|
|
95
95
|
: undefined;
|
|
96
96
|
const budget = map?.budget?.kind === 'reading' ? projectBudget(map.budget) : undefined;
|
|
97
97
|
const counters = readQuotaCounters(map?.counters);
|
|
98
|
+
const credits = isQuotaCredits(map?.credits) ? map.credits : undefined;
|
|
98
99
|
return {
|
|
99
100
|
scope,
|
|
100
101
|
limits,
|
|
101
102
|
...(checkedAt === undefined ? {} : { checkedAt }),
|
|
102
103
|
...(budget === undefined ? {} : { budget }),
|
|
103
104
|
...(counters === undefined ? {} : { counters }),
|
|
105
|
+
...(credits === undefined ? {} : { credits }),
|
|
104
106
|
};
|
|
105
107
|
}
|
|
106
108
|
/**
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
spawn as defaultSpawn
|
|
4
|
-
} from "node:child_process";
|
|
5
|
-
export function openUrl(url, platform = process.platform, execFileSync = defaultExecFileSync, spawn = defaultSpawn) {
|
|
1
|
+
import { spawn as defaultSpawn } from "node:child_process";
|
|
2
|
+
export function openUrl(url, platform = process.platform, spawn = defaultSpawn) {
|
|
6
3
|
try {
|
|
7
4
|
if (platform === "win32") {
|
|
8
5
|
const command = `Start-Process '${url.replaceAll("'", "''")}'`, encodedCommand = Buffer.from(command, "utf16le").toString("base64"), child = spawn("powershell.exe", [
|
|
@@ -17,11 +14,12 @@ export function openUrl(url, platform = process.platform, execFileSync = default
|
|
|
17
14
|
child.unref();
|
|
18
15
|
return child.pid !== void 0;
|
|
19
16
|
}
|
|
20
|
-
|
|
21
|
-
stdio: "ignore"
|
|
22
|
-
timeout: 3000
|
|
17
|
+
const child = spawn(platform === "darwin" ? "open" : "xdg-open", [url], {
|
|
18
|
+
stdio: "ignore"
|
|
23
19
|
});
|
|
24
|
-
|
|
20
|
+
child.on("error", () => {});
|
|
21
|
+
child.unref();
|
|
22
|
+
return child.pid !== void 0;
|
|
25
23
|
} catch {
|
|
26
24
|
return !1;
|
|
27
25
|
}
|
|
@@ -21,6 +21,7 @@ export class LockOwnershipError extends Error {
|
|
|
21
21
|
export async function withLock(target, options, fn) {
|
|
22
22
|
const started = performance.now();
|
|
23
23
|
for (;;) {
|
|
24
|
+
options.signal?.throwIfAborted();
|
|
24
25
|
const lock = await acquireRefreshFileLock({
|
|
25
26
|
path: target,
|
|
26
27
|
name: options.name,
|
|
@@ -33,6 +34,7 @@ export async function withLock(target, options, fn) {
|
|
|
33
34
|
});
|
|
34
35
|
if (lock) {
|
|
35
36
|
try {
|
|
37
|
+
options.signal?.throwIfAborted();
|
|
36
38
|
return await fn(lock);
|
|
37
39
|
}
|
|
38
40
|
finally {
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
// no entry gets an absence record. A pair outside the coverage is never
|
|
12
12
|
// touched, so a header-shaped partial observation (which covers only what it
|
|
13
13
|
// carries) cannot erase a family limit or the budget.
|
|
14
|
-
import { ALL_SCOPE, decodeQuotaMap, entryTime, isQuotaCounters, limitKey, } from "./905bffc0220f43d0-map.js";
|
|
14
|
+
import { ALL_SCOPE, decodeQuotaMap, entryTime, isQuotaCounters, isQuotaCredits, limitKey, } from "./905bffc0220f43d0-map.js";
|
|
15
15
|
/** Thrown by `mergeQuotaObservation` when either argument is malformed. */
|
|
16
16
|
export class QuotaCodecError extends TypeError {
|
|
17
17
|
constructor(message) {
|
|
@@ -84,7 +84,8 @@ export function isQuotaObservation(value) {
|
|
|
84
84
|
if (!coverage.every(isObservedPair))
|
|
85
85
|
return false;
|
|
86
86
|
return ((value.budget === undefined || isObservedBudget(value.budget)) &&
|
|
87
|
-
(value.counters === undefined || isQuotaCounters(value.counters))
|
|
87
|
+
(value.counters === undefined || isQuotaCounters(value.counters)) &&
|
|
88
|
+
(value.credits === undefined || isQuotaCredits(value.credits)));
|
|
88
89
|
}
|
|
89
90
|
function compareEntries(left, right) {
|
|
90
91
|
if (left.scope !== right.scope)
|
|
@@ -110,6 +111,17 @@ function mergeCounters(stored, observed) {
|
|
|
110
111
|
}
|
|
111
112
|
return counters.size === 0 ? undefined : Object.fromEntries(counters);
|
|
112
113
|
}
|
|
114
|
+
// Credits merge like a single counter: the newer record wins, an equal time
|
|
115
|
+
// applies (the observation applied last wins), and an observation without
|
|
116
|
+
// credits leaves the stored record alone.
|
|
117
|
+
function mergeCredits(stored, observed) {
|
|
118
|
+
if (observed === undefined)
|
|
119
|
+
return stored;
|
|
120
|
+
if (stored !== undefined && observed.checkedAt < stored.checkedAt) {
|
|
121
|
+
return stored;
|
|
122
|
+
}
|
|
123
|
+
return { ...observed };
|
|
124
|
+
}
|
|
113
125
|
/**
|
|
114
126
|
* Applies `observation` to `stored` (undefined for a row with no map yet) and
|
|
115
127
|
* returns the new map. Pure: neither argument is modified. Throws
|
|
@@ -166,13 +178,15 @@ export function mergeQuotaObservation(stored, observation) {
|
|
|
166
178
|
: { scope, label: pair.label, kind: 'retired', retiredAt: at });
|
|
167
179
|
}
|
|
168
180
|
// Keys this module does not recognise are carried over untouched.
|
|
169
|
-
const { limits: _limits, budget: _budget, counters: _counters, ...unknownKeys } = base;
|
|
181
|
+
const { limits: _limits, budget: _budget, counters: _counters, credits: _credits, ...unknownKeys } = base;
|
|
170
182
|
const budget = mergeBudget(base.budget, observation.budget, at);
|
|
171
183
|
const counters = mergeCounters(base.counters, observation.counters);
|
|
184
|
+
const credits = mergeCredits(base.credits, observation.credits);
|
|
172
185
|
return {
|
|
173
186
|
...unknownKeys,
|
|
174
187
|
limits: [...entries.values()].sort(compareEntries),
|
|
175
188
|
...(budget === undefined ? {} : { budget }),
|
|
176
189
|
...(counters === undefined ? {} : { counters }),
|
|
190
|
+
...(credits === undefined ? {} : { credits }),
|
|
177
191
|
};
|
|
178
192
|
}
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
import { createLogger } from "./cc9e0702a752cea7-index.js";
|
|
3
|
+
import { isQuotaObservation } from "./d5a8f1061ee2f114-index.js";
|
|
4
|
+
import { applyVaultQuota, mutateVaultRoster, } from "./30f5bc480f31f51c-roster.js";
|
|
5
|
+
// Every consumer of the same file shares one mailbox and one lock waiter.
|
|
6
|
+
const writers = new Map();
|
|
7
|
+
const logger = createLogger('claustrum');
|
|
8
|
+
const COALESCE_MS = 100;
|
|
9
|
+
function writerFor(path) {
|
|
10
|
+
const key = resolve(path);
|
|
11
|
+
let writer = writers.get(key);
|
|
12
|
+
if (!writer) {
|
|
13
|
+
writer = {
|
|
14
|
+
path: key,
|
|
15
|
+
pending: new Map(),
|
|
16
|
+
listeners: new Set(),
|
|
17
|
+
wake: new Set(),
|
|
18
|
+
abort: new AbortController(),
|
|
19
|
+
failures: 0,
|
|
20
|
+
draining: false,
|
|
21
|
+
order: 0,
|
|
22
|
+
};
|
|
23
|
+
writers.set(key, writer);
|
|
24
|
+
}
|
|
25
|
+
return writer;
|
|
26
|
+
}
|
|
27
|
+
/** Retain the shared writer; the last consumer's close drains it. */
|
|
28
|
+
export function retainVaultQuotaWriter(path, listener) {
|
|
29
|
+
const writer = writerFor(path);
|
|
30
|
+
writer.listeners.add(listener);
|
|
31
|
+
let released;
|
|
32
|
+
return () => (released ??= (() => {
|
|
33
|
+
writer.listeners.delete(listener);
|
|
34
|
+
return writer.listeners.size ? Promise.resolve() : drainVaultQuota(path);
|
|
35
|
+
})());
|
|
36
|
+
}
|
|
37
|
+
// Split one observation into its independently dated parts: each rate-limit
|
|
38
|
+
// window (scope and label), each covered-but-unread window, the budget, each
|
|
39
|
+
// counter and the credits record. The mailbox keeps the newest of each part on
|
|
40
|
+
// its own, so a later observation that carries only some parts can't erase an
|
|
41
|
+
// older but still newest reading of another part, which is how
|
|
42
|
+
// mergeQuotaObservation treats them on disk. Each key also carries the
|
|
43
|
+
// receipt's identity, so the identity fence still applies to every part.
|
|
44
|
+
function fields(publication) {
|
|
45
|
+
const result = new Map();
|
|
46
|
+
const { observation } = publication;
|
|
47
|
+
const witness = JSON.stringify([
|
|
48
|
+
publication.routeId,
|
|
49
|
+
publication.receipt?.credentialId,
|
|
50
|
+
publication.receipt?.accountIdentitySource,
|
|
51
|
+
publication.receipt?.expectedAccountIdentity,
|
|
52
|
+
]);
|
|
53
|
+
const add = (key, at, part) => result.set(`${witness}:${key}`, {
|
|
54
|
+
at,
|
|
55
|
+
order: 0,
|
|
56
|
+
publication: { ...publication, observation: part },
|
|
57
|
+
});
|
|
58
|
+
const keyFor = (scope, label) => JSON.stringify([scope ?? 'all', label]);
|
|
59
|
+
const read = new Set();
|
|
60
|
+
for (const reading of observation.readings ?? []) {
|
|
61
|
+
const key = keyFor(reading.scope, reading.label);
|
|
62
|
+
read.add(key);
|
|
63
|
+
add(key, observation.checkedAt, {
|
|
64
|
+
checkedAt: observation.checkedAt,
|
|
65
|
+
readings: [{ ...reading }],
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
for (const pair of observation.coverage ?? []) {
|
|
69
|
+
const key = keyFor(pair.scope, pair.label);
|
|
70
|
+
if (!read.has(key))
|
|
71
|
+
add(key, observation.checkedAt, {
|
|
72
|
+
checkedAt: observation.checkedAt,
|
|
73
|
+
coverage: [{ ...pair }],
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
if (observation.budget)
|
|
77
|
+
add('budget', observation.checkedAt, {
|
|
78
|
+
checkedAt: observation.checkedAt,
|
|
79
|
+
budget: { ...observation.budget },
|
|
80
|
+
});
|
|
81
|
+
for (const [name, counter] of Object.entries(observation.counters ?? {})) {
|
|
82
|
+
add(`counter:${name}`, counter.checkedAt, {
|
|
83
|
+
checkedAt: observation.checkedAt,
|
|
84
|
+
counters: { [name]: { ...counter } },
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
if (observation.credits)
|
|
88
|
+
add('credits', observation.credits.checkedAt, {
|
|
89
|
+
checkedAt: observation.checkedAt,
|
|
90
|
+
credits: { ...observation.credits },
|
|
91
|
+
});
|
|
92
|
+
// Even an empty observation can bind an account proved by its receipt.
|
|
93
|
+
if (!result.size)
|
|
94
|
+
add('empty', observation.checkedAt, { checkedAt: observation.checkedAt });
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
97
|
+
/** Enqueue without filesystem work; each quota field retains its own receipt fence. */
|
|
98
|
+
export function enqueueVaultQuota(path, publication) {
|
|
99
|
+
if (!isQuotaObservation(publication.observation))
|
|
100
|
+
return Promise.resolve(false);
|
|
101
|
+
const writer = writerFor(path);
|
|
102
|
+
const account = JSON.stringify([
|
|
103
|
+
publication.accountIdentity ?? null,
|
|
104
|
+
publication.accountIdentity === undefined ? publication.routeId : null,
|
|
105
|
+
]);
|
|
106
|
+
const pending = writer.pending.get(account) ?? new Map();
|
|
107
|
+
for (const [key, field] of fields(publication)) {
|
|
108
|
+
const previous = pending.get(key);
|
|
109
|
+
if (!previous || field.at >= previous.at) {
|
|
110
|
+
field.order = ++writer.order;
|
|
111
|
+
// A covered disappearance after an unflushed reading is a retirement,
|
|
112
|
+
// not an absence. Retain just enough history to preserve that distinction.
|
|
113
|
+
if (field.publication.observation.coverage)
|
|
114
|
+
field.priorReading = previous?.publication.observation.readings
|
|
115
|
+
? previous.publication
|
|
116
|
+
: previous?.priorReading;
|
|
117
|
+
pending.set(key, field);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (pending.size)
|
|
121
|
+
writer.pending.set(account, pending);
|
|
122
|
+
for (const listener of writer.listeners)
|
|
123
|
+
listener(publication);
|
|
124
|
+
schedule(writer, COALESCE_MS);
|
|
125
|
+
return Promise.resolve(true);
|
|
126
|
+
}
|
|
127
|
+
function orderedFields(pending) {
|
|
128
|
+
// Equal observation times are last-enqueue-wins even across different
|
|
129
|
+
// credentials or account-only readings. Map insertion order cannot prove it.
|
|
130
|
+
return [...pending.values()]
|
|
131
|
+
.flatMap((account) => [...account.values()])
|
|
132
|
+
.sort((a, b) => a.order - b.order);
|
|
133
|
+
}
|
|
134
|
+
/** Overlay uncommitted observations when a consumer reloads the durable roster. */
|
|
135
|
+
export function pendingVaultQuota(path, roster) {
|
|
136
|
+
const writer = writers.get(resolve(path));
|
|
137
|
+
if (writer) {
|
|
138
|
+
for (const field of orderedFields(writer.pending)) {
|
|
139
|
+
if (field.priorReading)
|
|
140
|
+
roster = applyVaultQuota(roster, field.priorReading).roster;
|
|
141
|
+
roster = applyVaultQuota(roster, field.publication).roster;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return roster;
|
|
145
|
+
}
|
|
146
|
+
function schedule(writer, delay) {
|
|
147
|
+
if (writer.running ||
|
|
148
|
+
writer.timer ||
|
|
149
|
+
!writer.pending.size ||
|
|
150
|
+
writer.abort.signal.aborted)
|
|
151
|
+
return;
|
|
152
|
+
writer.timer = setTimeout(() => {
|
|
153
|
+
writer.timer = undefined;
|
|
154
|
+
flush(writer);
|
|
155
|
+
}, delay);
|
|
156
|
+
if (!writer.draining)
|
|
157
|
+
writer.timer.unref?.();
|
|
158
|
+
}
|
|
159
|
+
function flush(writer) {
|
|
160
|
+
if (writer.running || writer.abort.signal.aborted)
|
|
161
|
+
return;
|
|
162
|
+
const batch = new Map([...writer.pending].map(([account, pending]) => [
|
|
163
|
+
account,
|
|
164
|
+
new Map(pending),
|
|
165
|
+
]));
|
|
166
|
+
writer.running = (async () => {
|
|
167
|
+
try {
|
|
168
|
+
await mutateVaultRoster(writer.path, (current) => {
|
|
169
|
+
let next = current;
|
|
170
|
+
for (const field of orderedFields(batch)) {
|
|
171
|
+
if (field.priorReading)
|
|
172
|
+
next = applyVaultQuota(next, field.priorReading).roster;
|
|
173
|
+
next = applyVaultQuota(next, field.publication).roster;
|
|
174
|
+
}
|
|
175
|
+
return { ...(next !== current && { next }), result: undefined };
|
|
176
|
+
}, { signal: writer.abort.signal });
|
|
177
|
+
for (const [account, fields] of batch) {
|
|
178
|
+
const pending = writer.pending.get(account);
|
|
179
|
+
for (const [key, field] of fields) {
|
|
180
|
+
// A newer enqueue must survive completion of an older flush.
|
|
181
|
+
if (pending?.get(key) === field)
|
|
182
|
+
pending.delete(key);
|
|
183
|
+
}
|
|
184
|
+
if (!pending?.size)
|
|
185
|
+
writer.pending.delete(account);
|
|
186
|
+
}
|
|
187
|
+
writer.failures = 0;
|
|
188
|
+
}
|
|
189
|
+
catch (error) {
|
|
190
|
+
if (!writer.abort.signal.aborted && writer.failures++ === 0) {
|
|
191
|
+
logger.warn('vault quota publication delayed; retrying', {
|
|
192
|
+
error: error instanceof Error ? error.message : String(error),
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
// Keep the batch in the mailbox, including any fresher fields enqueued meanwhile.
|
|
196
|
+
}
|
|
197
|
+
finally {
|
|
198
|
+
writer.running = undefined;
|
|
199
|
+
for (const wake of writer.wake)
|
|
200
|
+
wake();
|
|
201
|
+
const backoff = writer.failures
|
|
202
|
+
? Math.min(5_000, 100 * 2 ** Math.min(writer.failures, 6)) *
|
|
203
|
+
(0.5 + Math.random())
|
|
204
|
+
: COALESCE_MS;
|
|
205
|
+
schedule(writer, backoff);
|
|
206
|
+
}
|
|
207
|
+
})();
|
|
208
|
+
}
|
|
209
|
+
/** Wait at most timeoutMs for detached publication; shutdown loss is acceptable. */
|
|
210
|
+
export async function drainVaultQuota(path, timeoutMs = 1_000) {
|
|
211
|
+
const key = resolve(path);
|
|
212
|
+
const writer = writers.get(key);
|
|
213
|
+
if (!writer)
|
|
214
|
+
return;
|
|
215
|
+
writer.draining = true;
|
|
216
|
+
clearTimeout(writer.timer);
|
|
217
|
+
writer.timer = undefined;
|
|
218
|
+
let deadline;
|
|
219
|
+
let wake;
|
|
220
|
+
const drained = new Promise((done) => {
|
|
221
|
+
wake = () => {
|
|
222
|
+
if (!writer.pending.size || writer.abort.signal.aborted)
|
|
223
|
+
done();
|
|
224
|
+
};
|
|
225
|
+
writer.wake.add(wake);
|
|
226
|
+
wake();
|
|
227
|
+
});
|
|
228
|
+
try {
|
|
229
|
+
if (writer.pending.size)
|
|
230
|
+
flush(writer);
|
|
231
|
+
await Promise.race([
|
|
232
|
+
drained,
|
|
233
|
+
new Promise((done) => {
|
|
234
|
+
deadline = setTimeout(done, Math.max(0, timeoutMs));
|
|
235
|
+
}),
|
|
236
|
+
]);
|
|
237
|
+
}
|
|
238
|
+
finally {
|
|
239
|
+
clearTimeout(deadline);
|
|
240
|
+
if (wake)
|
|
241
|
+
writer.wake.delete(wake);
|
|
242
|
+
writer.draining = false;
|
|
243
|
+
// A still-open consumer owns the mailbox, so another consumer's disposal
|
|
244
|
+
// must not discard its observations or stop its retries.
|
|
245
|
+
if (!writer.listeners.size) {
|
|
246
|
+
writer.abort.abort();
|
|
247
|
+
clearTimeout(writer.timer);
|
|
248
|
+
writer.pending.clear();
|
|
249
|
+
if (writers.get(key) === writer)
|
|
250
|
+
writers.delete(key);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
@@ -14,6 +14,9 @@
|
|
|
14
14
|
// reset clock. It is a tri-state: absent (never reported), a reading, or
|
|
15
15
|
// cleared (the provider reported that no budget exists).
|
|
16
16
|
//
|
|
17
|
+
// Provider-named counters and the prepaid credits record sit beside them too,
|
|
18
|
+
// each judged by its own observation time rather than the limits'.
|
|
19
|
+
//
|
|
17
20
|
// The map is plain JSON so the store can persist it without interpreting it.
|
|
18
21
|
/** The scope of a limit that binds every request regardless of model. */
|
|
19
22
|
export const ALL_SCOPE = 'all';
|
|
@@ -93,6 +96,18 @@ function isQuotaCounter(value) {
|
|
|
93
96
|
export function isQuotaCounters(value) {
|
|
94
97
|
return isRecord(value) && Object.values(value).every(isQuotaCounter);
|
|
95
98
|
}
|
|
99
|
+
function isBoolean(value) {
|
|
100
|
+
return typeof value === 'boolean';
|
|
101
|
+
}
|
|
102
|
+
/** True when `value` is a well-formed prepaid-credits record. */
|
|
103
|
+
export function isQuotaCredits(value) {
|
|
104
|
+
return (isRecord(value) &&
|
|
105
|
+
typeof value.hasCredits === 'boolean' &&
|
|
106
|
+
typeof value.unlimited === 'boolean' &&
|
|
107
|
+
optional(value, 'balance', isFiniteNumber) &&
|
|
108
|
+
optional(value, 'overageLimitReached', isBoolean) &&
|
|
109
|
+
isFiniteNumber(value.checkedAt));
|
|
110
|
+
}
|
|
96
111
|
/** Keep usable counters without letting one malformed count hide a quota map. */
|
|
97
112
|
export function readQuotaCounters(value) {
|
|
98
113
|
if (!isRecord(value))
|
|
@@ -102,20 +117,29 @@ export function readQuotaCounters(value) {
|
|
|
102
117
|
? undefined
|
|
103
118
|
: Object.fromEntries(entries);
|
|
104
119
|
}
|
|
105
|
-
/**
|
|
120
|
+
/**
|
|
121
|
+
* Decode persisted quota, dropping malformed counters or a malformed credits
|
|
122
|
+
* record but not limit or budget evidence.
|
|
123
|
+
*/
|
|
106
124
|
export function decodeQuotaMap(value) {
|
|
107
125
|
// A valid map is returned as the same object, so stored quota keeps the
|
|
108
|
-
// identity it had before counters existed
|
|
109
|
-
// counter is copied
|
|
126
|
+
// identity it had before counters and credits existed. A map with a
|
|
127
|
+
// malformed counter or credits record is copied with those malformed
|
|
128
|
+
// fields left out.
|
|
110
129
|
if (isQuotaMap(value))
|
|
111
130
|
return value;
|
|
112
131
|
if (!isRecord(value))
|
|
113
132
|
return undefined;
|
|
114
|
-
const { counters: rawCounters, ...rest } = value;
|
|
133
|
+
const { counters: rawCounters, credits: rawCredits, ...rest } = value;
|
|
115
134
|
if (!isQuotaMap(rest))
|
|
116
135
|
return undefined;
|
|
117
136
|
const counters = readQuotaCounters(rawCounters);
|
|
118
|
-
|
|
137
|
+
const credits = isQuotaCredits(rawCredits) ? rawCredits : undefined;
|
|
138
|
+
return {
|
|
139
|
+
...rest,
|
|
140
|
+
...(counters === undefined ? {} : { counters }),
|
|
141
|
+
...(credits === undefined ? {} : { credits }),
|
|
142
|
+
};
|
|
119
143
|
}
|
|
120
144
|
/**
|
|
121
145
|
* True when `value` is a well-formed quota map. Unrecognised top-level keys
|
|
@@ -136,5 +160,6 @@ export function isQuotaMap(value) {
|
|
|
136
160
|
seen.add(key);
|
|
137
161
|
}
|
|
138
162
|
return (optional(value, 'budget', isCreditBudgetEntry) &&
|
|
139
|
-
optional(value, 'counters', isQuotaCounters)
|
|
163
|
+
optional(value, 'counters', isQuotaCounters) &&
|
|
164
|
+
optional(value, 'credits', isQuotaCredits));
|
|
140
165
|
}
|