@cortexkit/common-auth 0.36.0 → 0.37.1
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/claustrum/roster.js +14 -1
- package/dist/fs/file-read-cache.d.ts +3 -0
- package/dist/fs/file-read-cache.js +10 -5
- package/dist/limits/envelope-loss.js +3 -2
- package/dist/limits/namespace.d.ts +5 -0
- package/dist/limits/namespace.js +40 -7
- package/dist/setup/enroll.js +7 -1
- package/package.json +4 -4
package/dist/claustrum/roster.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto';
|
|
2
2
|
import { stat } from 'node:fs/promises';
|
|
3
3
|
import { resolve } from 'node:path';
|
|
4
|
+
import { FileReadCache } from '../fs/file-read-cache.js';
|
|
4
5
|
import { acquireRefreshFileLock, LockContentionError, LockOwnershipError, lockPathFor, PrivateFileError, withLock, writeJsonAtomic, } from '../fs/index.js';
|
|
5
6
|
import { readPrivateText, windowsPrivateFiles } from '../fs/private-files.js';
|
|
6
7
|
import { decodeQuotaMap, isQuotaObservation, mergeQuotaObservation, } from '../quota/index.js';
|
|
@@ -333,7 +334,18 @@ function decodeRoster(value) {
|
|
|
333
334
|
export function decodedVaultRoster(roster) {
|
|
334
335
|
return decodeRoster(JSON.parse(JSON.stringify(roster)));
|
|
335
336
|
}
|
|
337
|
+
const rosterReadCache = new FileReadCache({
|
|
338
|
+
followSymlinks: true,
|
|
339
|
+
});
|
|
336
340
|
export async function readVaultRoster(path) {
|
|
341
|
+
// Windows permission, owner and ancestry checks must still run on every read.
|
|
342
|
+
if (windowsPrivateFiles)
|
|
343
|
+
return readFreshVaultRoster(path);
|
|
344
|
+
const roster = await rosterReadCache.read([path], ([resolvedPath = path]) => readFreshVaultRoster(resolvedPath));
|
|
345
|
+
// Callers edit rows and nested limits data; none may own the cached objects.
|
|
346
|
+
return structuredClone(roster);
|
|
347
|
+
}
|
|
348
|
+
async function readFreshVaultRoster(path) {
|
|
337
349
|
let text;
|
|
338
350
|
try {
|
|
339
351
|
text = await readPrivateText(path, windowsPrivateFiles);
|
|
@@ -366,7 +378,8 @@ const DISCOVERY_LEASE = { name: 'claustrum-roster', ttlMs: 30_000 };
|
|
|
366
378
|
*/
|
|
367
379
|
export async function mutateVaultRoster(path, change, options = {}) {
|
|
368
380
|
return withLock(path, { ...WRITE_LOCK, signal: options.signal }, async (lock) => {
|
|
369
|
-
|
|
381
|
+
// A read-modify-write always observes the file under its write lock.
|
|
382
|
+
const current = await readFreshVaultRoster(path);
|
|
370
383
|
const { next, result } = await change(current, {
|
|
371
384
|
assertOwned: () => lock.assertOwned(),
|
|
372
385
|
});
|
|
@@ -3,6 +3,9 @@ type FileIdentity = Stats | 'absent';
|
|
|
3
3
|
/** Process-local reads, capped at 64 path sets with one value per resolved set. */
|
|
4
4
|
export declare class FileReadCache<T> {
|
|
5
5
|
#private;
|
|
6
|
+
constructor(options?: {
|
|
7
|
+
followSymlinks?: boolean;
|
|
8
|
+
});
|
|
6
9
|
read(paths: readonly string[], read: (resolvedPaths: string[]) => Promise<T>, validateHit?: (identities: readonly FileIdentity[]) => void): Promise<T>;
|
|
7
10
|
}
|
|
8
11
|
export {};
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { lstat } from 'node:fs/promises';
|
|
1
|
+
import { lstat, stat } from 'node:fs/promises';
|
|
2
2
|
import { resolve } from 'node:path';
|
|
3
|
-
async function identity(path) {
|
|
3
|
+
async function identity(path, followSymlinks) {
|
|
4
4
|
try {
|
|
5
|
-
|
|
6
|
-
//
|
|
5
|
+
// Match the reader: private readers reject links, while readFile follows
|
|
6
|
+
// them and needs the target's identity, not the link's identity.
|
|
7
|
+
const metadata = await (followSymlinks ? stat(path) : lstat(path));
|
|
7
8
|
return metadata.isFile() ? metadata : undefined;
|
|
8
9
|
}
|
|
9
10
|
catch (error) {
|
|
@@ -23,11 +24,15 @@ function sameIdentity(left, right) {
|
|
|
23
24
|
}
|
|
24
25
|
/** Process-local reads, capped at 64 path sets with one value per resolved set. */
|
|
25
26
|
export class FileReadCache {
|
|
27
|
+
#followSymlinks;
|
|
26
28
|
#entries = new Map();
|
|
29
|
+
constructor(options = {}) {
|
|
30
|
+
this.#followSymlinks = options.followSymlinks ?? false;
|
|
31
|
+
}
|
|
27
32
|
async read(paths, read, validateHit) {
|
|
28
33
|
const resolvedPaths = paths.map((path) => resolve(path));
|
|
29
34
|
const key = JSON.stringify(resolvedPaths);
|
|
30
|
-
const probed = await Promise.all(resolvedPaths.map(identity));
|
|
35
|
+
const probed = await Promise.all(resolvedPaths.map((path) => identity(path, this.#followSymlinks)));
|
|
31
36
|
const identities = probed.every((entry) => entry !== undefined)
|
|
32
37
|
? probed
|
|
33
38
|
: undefined;
|
|
@@ -2,7 +2,7 @@ import { AsyncResource } from 'node:async_hooks';
|
|
|
2
2
|
import { randomUUID } from 'node:crypto';
|
|
3
3
|
import { isDeepStrictEqual } from 'node:util';
|
|
4
4
|
import { mergeAccessState } from './merge-laws.js';
|
|
5
|
-
import { createLimitsNamespace, pruneLimitsNamespace, timedMarkKeyId, } from './namespace.js';
|
|
5
|
+
import { clearTombstoneId, createLimitsNamespace, pruneLimitsNamespace, timedMarkKeyId, tombstoneCoversCause, } from './namespace.js';
|
|
6
6
|
const detached = new AsyncResource('common-auth-limits-recovery');
|
|
7
7
|
/** Readers never join the write lock; notify them after a detached repair lands. */
|
|
8
8
|
export class EnvelopeRepair {
|
|
@@ -82,6 +82,7 @@ export class EnvelopeHistory {
|
|
|
82
82
|
present.revision < seen.revision ||
|
|
83
83
|
seen.tombstones.some((clear) => clear.revision > present.lowWatermark &&
|
|
84
84
|
!present.tombstones.some((current) => timedMarkKeyId(current.key) === timedMarkKeyId(clear.key) &&
|
|
85
|
+
tombstoneCoversCause(current, clear.cause) &&
|
|
85
86
|
current.revision >= clear.revision)));
|
|
86
87
|
}
|
|
87
88
|
/** Preview a restrictive merge, or apply it against the envelope under the write lock. */
|
|
@@ -111,7 +112,7 @@ export class EnvelopeHistory {
|
|
|
111
112
|
highest = Math.max(highest, entry.baseRevision, entry.committedRevision);
|
|
112
113
|
}
|
|
113
114
|
for (const clear of source.tombstones) {
|
|
114
|
-
const id =
|
|
115
|
+
const id = clearTombstoneId(clear);
|
|
115
116
|
const old = tombstones.get(id);
|
|
116
117
|
if (!old || old.revision < clear.revision)
|
|
117
118
|
tombstones.set(id, clear);
|
|
@@ -19,7 +19,12 @@ export interface NamespaceMark {
|
|
|
19
19
|
export interface ClearTombstone {
|
|
20
20
|
readonly key: TimedMarkKey;
|
|
21
21
|
readonly revision: number;
|
|
22
|
+
/** Omitted by legacy writers and all-cause clears; only stored enum values belong here. */
|
|
23
|
+
readonly cause?: LimitMarkCause;
|
|
22
24
|
}
|
|
25
|
+
/** An absent cause is the conservative all-cause fence used by older writers. */
|
|
26
|
+
export declare function tombstoneCoversCause(clear: ClearTombstone, cause: LimitMarkCause | undefined): boolean;
|
|
27
|
+
export declare function clearTombstoneId(clear: ClearTombstone): string;
|
|
23
28
|
export interface CommittedOperation {
|
|
24
29
|
readonly producerId: string;
|
|
25
30
|
readonly operationId: string;
|
package/dist/limits/namespace.js
CHANGED
|
@@ -2,6 +2,18 @@ import { validateIdentityFence } from './identity.js';
|
|
|
2
2
|
import { mergeAccessState, mergeTimedMarkEntries } from './merge-laws.js';
|
|
3
3
|
import { sameAccount } from './request-history.js';
|
|
4
4
|
export const LIMITS_HISTORY_BOUND = 1024;
|
|
5
|
+
function storedCause(cause) {
|
|
6
|
+
return cause === 'rate-limit' || cause === 'payment-balance'
|
|
7
|
+
? cause
|
|
8
|
+
: undefined;
|
|
9
|
+
}
|
|
10
|
+
/** An absent cause is the conservative all-cause fence used by older writers. */
|
|
11
|
+
export function tombstoneCoversCause(clear, cause) {
|
|
12
|
+
return storedCause(clear.cause) === undefined || clear.cause === cause;
|
|
13
|
+
}
|
|
14
|
+
export function clearTombstoneId(clear) {
|
|
15
|
+
return JSON.stringify([timedMarkKeyId(clear.key), storedCause(clear.cause)]);
|
|
16
|
+
}
|
|
5
17
|
export function createLimitsNamespace(namespace, generation) {
|
|
6
18
|
return {
|
|
7
19
|
schemaVersion: 1,
|
|
@@ -40,9 +52,21 @@ function operationId(op) {
|
|
|
40
52
|
}
|
|
41
53
|
/** Apply tombstones before compacting them; the watermark then fences delayed commands. */
|
|
42
54
|
export function pruneLimitsNamespace(snapshot, now) {
|
|
43
|
-
const clears = new Map(
|
|
55
|
+
const clears = new Map();
|
|
56
|
+
for (const clear of snapshot.tombstones) {
|
|
57
|
+
const id = timedMarkKeyId(clear.key);
|
|
58
|
+
const list = clears.get(id);
|
|
59
|
+
if (list)
|
|
60
|
+
list.push(clear);
|
|
61
|
+
else
|
|
62
|
+
clears.set(id, [clear]);
|
|
63
|
+
}
|
|
44
64
|
const marks = snapshot.marks.flatMap((mark) => {
|
|
45
|
-
const
|
|
65
|
+
const fences = clears.get(timedMarkKeyId(mark.key));
|
|
66
|
+
const entries = mergeTimedMarkEntries(mark.mergeLaw, fences
|
|
67
|
+
? mark.entries.filter((entry) => !fences.some((clear) => tombstoneCoversCause(clear, entry.cause) &&
|
|
68
|
+
entry.baseRevision < clear.revision))
|
|
69
|
+
: mark.entries, now);
|
|
46
70
|
return entries.length ? [{ ...mark, entries }] : [];
|
|
47
71
|
});
|
|
48
72
|
let lowWatermark = snapshot.lowWatermark;
|
|
@@ -114,8 +138,9 @@ export function applyLimitsCommands(initial, commands, context) {
|
|
|
114
138
|
const key = { account, scope: command.scope, window: command.window };
|
|
115
139
|
const id = timedMarkKeyId(key);
|
|
116
140
|
const previous = marks.find((m) => timedMarkKeyId(m.key) === id);
|
|
117
|
-
|
|
118
|
-
|
|
141
|
+
if (tombstones.some((clear) => command.baseRevision < clear.revision &&
|
|
142
|
+
tombstoneCoversCause(clear, command.cause) &&
|
|
143
|
+
timedMarkKeyId(clear.key) === id)) {
|
|
119
144
|
reject('cleared');
|
|
120
145
|
continue;
|
|
121
146
|
}
|
|
@@ -135,12 +160,13 @@ export function applyLimitsCommands(initial, commands, context) {
|
|
|
135
160
|
? { cause: command.cause }
|
|
136
161
|
: {}),
|
|
137
162
|
},
|
|
138
|
-
], context.now
|
|
163
|
+
], context.now);
|
|
139
164
|
marks = marks.filter((m) => timedMarkKeyId(m.key) !== id);
|
|
140
165
|
if (entries.length)
|
|
141
166
|
marks.push({ key, mergeLaw: command.mergeLaw, entries });
|
|
142
167
|
}
|
|
143
168
|
else if (command.type === 'clear') {
|
|
169
|
+
const cause = storedCause(command.cause);
|
|
144
170
|
const keys = command.keys
|
|
145
171
|
.map((key) => ({ ...key, account }))
|
|
146
172
|
.filter((key) => command.cause === undefined ||
|
|
@@ -155,10 +181,17 @@ export function applyLimitsCommands(initial, commands, context) {
|
|
|
155
181
|
continue;
|
|
156
182
|
}
|
|
157
183
|
marks = marks.filter((m) => !ids.has(timedMarkKeyId(m.key)));
|
|
158
|
-
|
|
184
|
+
// Keep earlier fences for other causes, including an earlier all-cause clear.
|
|
185
|
+
// Replacing them would either lose a fence or extend it to unrelated evidence.
|
|
186
|
+
tombstones = tombstones.filter((t) => !ids.has(timedMarkKeyId(t.key)) ||
|
|
187
|
+
(cause !== undefined && t.cause !== cause));
|
|
159
188
|
// A clear enumerates covered keys, not all scopes on the account.
|
|
160
189
|
for (const key of new Map(keys.map((k) => [timedMarkKeyId(k), k])).values()) {
|
|
161
|
-
tombstones.push({
|
|
190
|
+
tombstones.push({
|
|
191
|
+
key,
|
|
192
|
+
revision,
|
|
193
|
+
...(cause === undefined ? {} : { cause }),
|
|
194
|
+
});
|
|
162
195
|
}
|
|
163
196
|
}
|
|
164
197
|
else {
|
package/dist/setup/enroll.js
CHANGED
|
@@ -77,6 +77,12 @@ export async function connectSetupHost(plugin, host, io, terminal, fence) {
|
|
|
77
77
|
await writeEnrollmentTokenFile(path, file);
|
|
78
78
|
},
|
|
79
79
|
});
|
|
80
|
+
await fence();
|
|
81
|
+
const initial = await manager.status();
|
|
82
|
+
if (initial.state === 'approved') {
|
|
83
|
+
printLine(terminal, `Already connected as ${initial.approvedName ?? initial.proposedName}.`);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
80
86
|
printLine(terminal, `Asking the vault to connect ${name}…`);
|
|
81
87
|
const deadline = now() + ENROLLMENT_WAIT_MS;
|
|
82
88
|
let printedRequest;
|
|
@@ -84,7 +90,6 @@ export async function connectSetupHost(plugin, host, io, terminal, fence) {
|
|
|
84
90
|
let reportedCode;
|
|
85
91
|
let failedProposals = 0;
|
|
86
92
|
for (;;) {
|
|
87
|
-
await fence();
|
|
88
93
|
const status = await manager.reconcile();
|
|
89
94
|
if (status.state === 'approved') {
|
|
90
95
|
printLine(terminal, `Connected ${host} to Claustrum.`);
|
|
@@ -135,6 +140,7 @@ export async function connectSetupHost(plugin, host, io, terminal, fence) {
|
|
|
135
140
|
if (remaining <= 0)
|
|
136
141
|
break;
|
|
137
142
|
await wait(Math.min(pause, remaining));
|
|
143
|
+
await fence();
|
|
138
144
|
}
|
|
139
145
|
if (printedRequest)
|
|
140
146
|
throw new HostNotEnrolledError(`Claustrum approval is still pending. In another terminal, run: ${approveCommand(printedRequest, name)} and then rerun setup.`, {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cortexkit/common-auth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.37.1",
|
|
4
4
|
"description": "Shared code for the CortexKit auth plugins: account pool, quota and routing, commands and auth menu, OpenCode 2 hooks, Claustrum custody, and plumbing (loopback RPC, file locks, logger, sidebar state, TUI preferences and build).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -141,7 +141,7 @@
|
|
|
141
141
|
"@opencode/plugin": ">=2.0.21",
|
|
142
142
|
"@opentui/core": ">=0.4.5",
|
|
143
143
|
"@opentui/solid": ">=0.4.5",
|
|
144
|
-
"solid-js": "1.9.12"
|
|
144
|
+
"solid-js": ">=1.9.12 <2"
|
|
145
145
|
},
|
|
146
146
|
"peerDependenciesMeta": {
|
|
147
147
|
"@opentui/core": {
|
|
@@ -165,7 +165,7 @@
|
|
|
165
165
|
},
|
|
166
166
|
"devDependencies": {
|
|
167
167
|
"@biomejs/biome": "2.5.14",
|
|
168
|
-
"@cortexkit/claustrum-client": "^0.
|
|
168
|
+
"@cortexkit/claustrum-client": "^0.15.0",
|
|
169
169
|
"@cortexkit/subc-client": "0.22.2",
|
|
170
170
|
"@earendil-works/pi-coding-agent": "1.0.4",
|
|
171
171
|
"@opencode/plugin": "2.0.21",
|
|
@@ -178,7 +178,7 @@
|
|
|
178
178
|
"lefthook": "2.1.14",
|
|
179
179
|
"pi-coding-agent-086": "npm:@earendil-works/pi-coding-agent@0.86.1",
|
|
180
180
|
"proper-lockfile": "4.1.2",
|
|
181
|
-
"solid-js": "1.9.
|
|
181
|
+
"solid-js": "1.9.18",
|
|
182
182
|
"typescript": "7.0.2"
|
|
183
183
|
}
|
|
184
184
|
}
|