@cortexkit/common-auth 0.11.4 → 0.11.6

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.
@@ -7,7 +7,7 @@ export { type ClaustrumEnrollmentClient, type ClaustrumEnrollmentConnection, Cla
7
7
  export { ClaustrumConsumerError, type ClaustrumConsumerFailureKind, type ClaustrumLogger, } from './errors.js';
8
8
  export { assertHostSlotMatchesMode, assertNotCustodyPlaceholder, CUSTODY_PLACEHOLDER_PREFIX, classifyHostSlot, custodyPlaceholder, custodyPlaceholderKey, type HostSlotContent, isCustodyPlaceholder, isCustodyPlaceholderValue, } from './host-slot.js';
9
9
  export { acceptAccount, type DeclinedAccount, declineAccount, isDeclined, } from './interlock.js';
10
- export { type AccountMapper, acceptVaultRoute, DEFAULT_ROUTE_PREFIX, declineVaultRoute, mutateVaultRoster, type ProjectionOptions, projectVaultRoster, type QuotaReceipt, readVaultRoster, recordVaultQuota, refreshVaultRoster, resolveVaultPrimary, type VaultPrimary, type VaultPrimaryBinding, type VaultPrimaryUnavailableReason, type VaultRosterFile, type VaultRosterLockContext, type VaultRosterRow, vaultRoutingRows, } from './roster.js';
10
+ export { type AccountMapper, acceptVaultRoute, DEFAULT_ROUTE_PREFIX, declineVaultRoute, type MutateVaultRosterOptions, mutateVaultRoster, type ProjectionOptions, projectVaultRoster, type QuotaReceipt, readVaultRoster, recordVaultQuota, refreshVaultRoster, resolveVaultPrimary, type VaultPrimary, type VaultPrimaryBinding, type VaultPrimaryUnavailableReason, type VaultRosterFile, type VaultRosterLockContext, type VaultRosterRow, vaultRoutingRows, } from './roster.js';
11
11
  /**
12
12
  * Connect the client that lists and fetches this consumer's vault credentials
13
13
  * on the request path. `connectionFile` is required: this library
@@ -90,6 +90,16 @@ export declare function readVaultRoster(path: string): Promise<VaultRosterFile |
90
90
  export interface VaultRosterLockContext {
91
91
  assertOwned: () => Promise<void>;
92
92
  }
93
+ export interface MutateVaultRosterOptions {
94
+ /**
95
+ * Runs when the new roster is fully staged on disk, immediately before the
96
+ * rename that publishes it, so a caller can confirm a lease of its own at
97
+ * the moment of publication rather than when `change` returned. If it
98
+ * throws, nothing is published and the staged file is removed. The
99
+ * roster's own write lock is reasserted right after it.
100
+ */
101
+ beforePublish?: () => Promise<void>;
102
+ }
93
103
  /**
94
104
  * Read, change and write the roster under its write lock. `change` returns
95
105
  * undefined to leave the file as it is. Call `assertOwned()` immediately
@@ -101,7 +111,7 @@ export declare function mutateVaultRoster<T>(path: string, change: (current: Vau
101
111
  }> | {
102
112
  next?: VaultRosterFile;
103
113
  result: T;
104
- }): Promise<T>;
114
+ }, options?: MutateVaultRosterOptions): Promise<T>;
105
115
  /** Mark a vault account declined; it stays listed but never routes. */
106
116
  export declare function declineVaultRoute(path: string, routeId: string): Promise<undefined>;
107
117
  /** Lift the user's decline for a vault account and every alias of it. */
@@ -316,7 +316,7 @@ const DISCOVERY_LEASE = { name: 'claustrum-roster', ttlMs: 30_000 };
316
316
  * undefined to leave the file as it is. Call `assertOwned()` immediately
317
317
  * before any rename performed inside `change`; if it throws, do not rename.
318
318
  */
319
- export async function mutateVaultRoster(path, change) {
319
+ export async function mutateVaultRoster(path, change, options = {}) {
320
320
  return withLock(path, WRITE_LOCK, async (lock) => {
321
321
  const current = await readVaultRoster(path);
322
322
  const { next, result } = await change(current, {
@@ -324,7 +324,14 @@ export async function mutateVaultRoster(path, change) {
324
324
  });
325
325
  if (next) {
326
326
  await lock.assertOwned();
327
- await writeJsonAtomic(path, next);
327
+ // Staging the file takes several awaited steps; check both the caller's
328
+ // lease and this lock again at the last moment before the rename.
329
+ await writeJsonAtomic(path, next, {
330
+ beforeRename: async () => {
331
+ await options.beforePublish?.();
332
+ await lock.assertOwned();
333
+ },
334
+ });
328
335
  }
329
336
  return result;
330
337
  });
@@ -1,6 +1,6 @@
1
1
  export type { AccountsSectionOptions, LimitsSectionOptions, LoginOutcome, QuotaSectionOptions, RoutingSectionOptions, } from './builtins.js';
2
2
  export { MENU_DISABLED_REASON } from './builtins.js';
3
- export type { CommandMenu, CommandMenuOptions } from './menu.js';
3
+ export type { CommandMenu, CommandMenuOptions, StoreSectionSlot, } from './menu.js';
4
4
  export { createCommandMenu, parseApplyRequest } from './menu.js';
5
5
  export type { ActionDefinition, ActionInput, ActionOutcome, CommandApplyRequest, CommandApplyResult, CommandDialogPayload, CommandInvocation, CommandMenuModel, ItemDefinition, KnobValue, KnobValues, MenuAccount, MenuAction, MenuChoice, MenuConfirmation, MenuItem, MenuKnob, MenuSection, NotifyKind, PluginExtraSection, PluginSection, SectionContent, SectionSlot, } from './model.js';
6
6
  export { SECTION_SLOTS } from './model.js';
@@ -3,12 +3,26 @@ import type { PoolLockSpec, PoolStore } from '../store/index.js';
3
3
  import { type AccountsSectionOptions, type LimitsSectionOptions, type QuotaSectionOptions, type RoutingSectionOptions } from './builtins.js';
4
4
  import type { CommandApplyRequest, CommandApplyResult, CommandDialogPayload, CommandInvocation, PluginExtraSection, PluginSection } from './model.js';
5
5
  import { type SeamLogger } from './seam.js';
6
+ /** The four slots the library can build from a pool store. */
7
+ export type StoreSectionSlot = 'accounts' | 'quota' | 'routing' | 'limits';
6
8
  export interface CommandMenuOptions {
7
9
  /** The slash command's name without the slash (`openai`, `claude`). */
8
10
  command: string;
9
11
  /** The dialog title. */
10
12
  title: string;
11
- store: PoolStore;
13
+ /**
14
+ * The pool store the built-in Accounts, Quota, Routing and Limits sections
15
+ * read and write. Required unless `replace` supplies all four.
16
+ */
17
+ store?: PoolStore;
18
+ /**
19
+ * Plugin sections that take the place of built-in ones, keeping their slot
20
+ * and the fixed order. For a plugin whose accounts are not pool rows (a
21
+ * single host login, or rows it must not expose by id), so it renders its
22
+ * own items and actions without a store. A replaced slot's built-in
23
+ * options (`accounts`, `quota`, ...) must not be given.
24
+ */
25
+ replace?: Partial<Record<StoreSectionSlot, PluginSection>>;
12
26
  /** The plugin's legacy locks, passed to every store write the menu makes. */
13
27
  extraLocks?: readonly PoolLockSpec[];
14
28
  accounts?: AccountsSectionOptions;
@@ -13,6 +13,12 @@ const BUILTIN_IDS = new Set([
13
13
  'cache',
14
14
  'diagnostics',
15
15
  ]);
16
+ const STORE_SLOTS = [
17
+ 'accounts',
18
+ 'quota',
19
+ 'routing',
20
+ 'limits',
21
+ ];
16
22
  /**
17
23
  * A private copy of the caller's context, taken before the first await. Work
18
24
  * an invocation leaves running reports through this copy, so a host that
@@ -102,6 +108,15 @@ export function createCommandMenu(options) {
102
108
  const seam = { logger, redact };
103
109
  const now = options.now ?? Date.now;
104
110
  const extras = options.extras ?? [];
111
+ const replaced = options.replace ?? {};
112
+ const builtSlots = STORE_SLOTS.filter((slot) => !replaced[slot]);
113
+ for (const slot of STORE_SLOTS) {
114
+ if (replaced[slot] && options[slot] !== undefined)
115
+ throw new Error(`the ${slot} section is replaced; its built-in options cannot also be given`);
116
+ }
117
+ const store = options.store;
118
+ if (builtSlots.length > 0 && !store)
119
+ throw new Error(`a store is required for the built-in ${builtSlots.join(', ')} section${builtSlots.length > 1 ? 's' : ''}`);
105
120
  const seen = new Set();
106
121
  for (const extra of extras) {
107
122
  if (BUILTIN_IDS.has(extra.id) || seen.has(extra.id))
@@ -134,15 +149,30 @@ export function createCommandMenu(options) {
134
149
  }
135
150
  /** Every section, in the fixed slot order. */
136
151
  async function sections(invocation) {
137
- const out = await builtinSections({
138
- store: options.store,
139
- now,
140
- ...(options.extraLocks ? { extraLocks: options.extraLocks } : {}),
141
- ...(options.accounts ? { accounts: options.accounts } : {}),
142
- ...(options.quota ? { quota: options.quota } : {}),
143
- ...(options.routing ? { routing: options.routing } : {}),
144
- ...(options.limits ? { limits: options.limits } : {}),
145
- });
152
+ // Built-in sections read the store once; a fully replaced menu never
153
+ // touches it.
154
+ const built = store && builtSlots.length > 0
155
+ ? await builtinSections({
156
+ store,
157
+ now,
158
+ ...(options.extraLocks ? { extraLocks: options.extraLocks } : {}),
159
+ ...(options.accounts ? { accounts: options.accounts } : {}),
160
+ ...(options.quota ? { quota: options.quota } : {}),
161
+ ...(options.routing ? { routing: options.routing } : {}),
162
+ ...(options.limits ? { limits: options.limits } : {}),
163
+ })
164
+ : [];
165
+ const out = [];
166
+ for (const slot of STORE_SLOTS) {
167
+ const replacement = replaced[slot];
168
+ if (replacement)
169
+ out.push(await plugin(slot, slot, replacement, invocation));
170
+ else {
171
+ const section = built.find((entry) => entry.slot === slot);
172
+ if (section)
173
+ out.push(section);
174
+ }
175
+ }
146
176
  if (options.cache)
147
177
  out.push(await plugin('cache', 'cache', options.cache, invocation));
148
178
  if (options.diagnostics)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/common-auth",
3
- "version": "0.11.4",
3
+ "version": "0.11.6",
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": {