@cortexkit/common-auth 0.37.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.
@@ -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
- const current = await readVaultRoster(path);
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
- const metadata = await lstat(path);
6
- // Do not follow a symlink: its target can change without changing the link.
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;
@@ -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.37.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.14.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.12",
181
+ "solid-js": "1.9.18",
182
182
  "typescript": "7.0.2"
183
183
  }
184
184
  }