@cortexkit/opencode-antigravity-auth 3.0.29 → 3.0.30

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.
Files changed (173) hide show
  1. package/dist/cli.js +689 -183
  2. package/dist/cli.js.map +4 -4
  3. package/dist/cli.meta.json +130 -67
  4. package/dist/index.js +734 -191
  5. package/dist/index.js.map +4 -4
  6. package/dist/index.meta.json +159 -72
  7. package/dist/server.js +734 -194
  8. package/dist/server.js.map +4 -4
  9. package/dist/server.meta.json +1 -1
  10. package/dist/src/cli.d.ts +1 -0
  11. package/dist/src/cli.d.ts.map +1 -1
  12. package/dist/src/cli.js +22 -2
  13. package/dist/src/cli.js.map +1 -1
  14. package/dist/src/common-auth-embedded/claustrum/consumer.d.ts +12 -0
  15. package/dist/src/common-auth-embedded/claustrum/consumer.js +36 -0
  16. package/dist/src/common-auth-embedded/claustrum/custody.d.ts +16 -4
  17. package/dist/src/common-auth-embedded/claustrum/custody.js +74 -9
  18. package/dist/src/common-auth-embedded/claustrum/hub.d.ts +3 -2
  19. package/dist/src/common-auth-embedded/claustrum/hub.js +8 -0
  20. package/dist/src/common-auth-embedded/claustrum/index.d.ts +1 -0
  21. package/dist/src/common-auth-embedded/claustrum/roster.d.ts +2 -0
  22. package/dist/src/common-auth-embedded/claustrum/roster.js +2 -0
  23. package/dist/src/common-auth-embedded/claustrum/usage.d.ts +41 -0
  24. package/dist/src/common-auth-embedded/claustrum/usage.js +145 -0
  25. package/dist/src/common-auth-embedded/commands/builtins.d.ts +5 -0
  26. package/dist/src/common-auth-embedded/commands/builtins.js +38 -0
  27. package/dist/src/common-auth-embedded/fs/private-files.d.ts +5 -0
  28. package/dist/src/common-auth-embedded/fs/private-files.js +7 -0
  29. package/dist/src/common-auth-embedded/fs/refresh-file-lock.js +142 -47
  30. package/dist/src/common-auth-embedded/fs/with-lock.js +2 -5
  31. package/dist/src/common-auth-embedded/limits/activity.d.ts +26 -0
  32. package/dist/src/common-auth-embedded/limits/axes.d.ts +63 -0
  33. package/dist/src/common-auth-embedded/limits/decide.d.ts +141 -0
  34. package/dist/src/common-auth-embedded/limits/duration.d.ts +8 -0
  35. package/dist/src/common-auth-embedded/limits/families.d.ts +57 -0
  36. package/dist/src/common-auth-embedded/limits/marker.d.ts +64 -0
  37. package/dist/src/common-auth-embedded/limits/request-history.d.ts +77 -0
  38. package/dist/src/common-auth-embedded/limits/scope.d.ts +12 -0
  39. package/dist/src/common-auth-embedded/limits/signal.d.ts +41 -0
  40. package/dist/src/common-auth-embedded/limits/terminal.d.ts +52 -0
  41. package/dist/src/common-auth-embedded/limits/wake.d.ts +21 -0
  42. package/dist/src/common-auth-embedded/limits/wording.d.ts +25 -0
  43. package/dist/src/common-auth-embedded/opencode2/index.d.ts +1 -1
  44. package/dist/src/common-auth-embedded/opencode2/install.d.ts +2 -2
  45. package/dist/src/common-auth-embedded/opencode2/install.js +161 -18
  46. package/dist/src/common-auth-embedded/opencode2/types.d.ts +95 -9
  47. package/dist/src/common-auth-embedded/routing/admission.d.ts +7 -2
  48. package/dist/src/common-auth-embedded/routing/admission.js +17 -7
  49. package/dist/src/common-auth-embedded/routing-accessor/handle.js +10 -0
  50. package/dist/src/common-auth-embedded/routing-accessor/index.d.ts +13 -3
  51. package/dist/src/common-auth-embedded/routing-accessor/index.js +11 -10
  52. package/dist/src/common-auth-embedded/rpc/port-file.d.ts +12 -3
  53. package/dist/src/common-auth-embedded/rpc/port-file.js +46 -4
  54. package/dist/src/common-auth-embedded/source-output.json +254 -118
  55. package/dist/src/common-auth-embedded/store/attribution.js +3 -2
  56. package/dist/src/common-auth-embedded/store/errors.d.ts +1 -1
  57. package/dist/src/common-auth-embedded/store/hooks.d.ts +5 -1
  58. package/dist/src/common-auth-embedded/store/hooks.js +20 -5
  59. package/dist/src/common-auth-embedded/store/index.d.ts +1 -0
  60. package/dist/src/common-auth-embedded/store/limits-namespace.d.ts +7 -0
  61. package/dist/src/common-auth-embedded/store/limits-namespace.js +34 -0
  62. package/dist/src/common-auth-embedded/store/pool.d.ts +10 -1
  63. package/dist/src/common-auth-embedded/store/pool.js +2 -0
  64. package/dist/src/common-auth-embedded/store/provider-state.js +5 -5
  65. package/dist/src/common-auth-embedded/store/refresh.js +1 -1
  66. package/dist/src/common-auth-embedded/store/schema.js +9 -4
  67. package/dist/src/common-auth-embedded/store/settings.d.ts +2 -0
  68. package/dist/src/common-auth-embedded/store/settings.js +11 -4
  69. package/dist/src/plugin/__fixtures__/verified-store.js +2 -2
  70. package/dist/src/runtime-layout.d.ts +9 -2
  71. package/dist/src/runtime-layout.d.ts.map +1 -1
  72. package/dist/src/runtime-layout.js +8 -8
  73. package/dist/src/runtime-layout.js.map +1 -1
  74. package/dist/src/setup.d.ts.map +1 -1
  75. package/dist/src/setup.js +29 -40
  76. package/dist/src/setup.js.map +1 -1
  77. package/dist/tui-build-map.json +9 -9
  78. package/dist/tui-ga-build-map.json +1 -1
  79. package/dist/types/src/cli.d.ts +1 -0
  80. package/dist/types/src/cli.d.ts.map +1 -1
  81. package/dist/types/src/common-auth-embedded/claustrum/consumer.d.ts +12 -0
  82. package/dist/types/src/common-auth-embedded/claustrum/custody.d.ts +16 -4
  83. package/dist/types/src/common-auth-embedded/claustrum/hub.d.ts +3 -2
  84. package/dist/types/src/common-auth-embedded/claustrum/index.d.ts +1 -0
  85. package/dist/types/src/common-auth-embedded/claustrum/roster.d.ts +2 -0
  86. package/dist/types/src/common-auth-embedded/claustrum/usage.d.ts +41 -0
  87. package/dist/types/src/common-auth-embedded/commands/builtins.d.ts +5 -0
  88. package/dist/types/src/common-auth-embedded/limits/activity.d.ts +26 -0
  89. package/dist/types/src/common-auth-embedded/limits/axes.d.ts +63 -0
  90. package/dist/types/src/common-auth-embedded/limits/decide.d.ts +141 -0
  91. package/dist/types/src/common-auth-embedded/limits/duration.d.ts +8 -0
  92. package/dist/types/src/common-auth-embedded/limits/families.d.ts +57 -0
  93. package/dist/types/src/common-auth-embedded/limits/marker.d.ts +64 -0
  94. package/dist/types/src/common-auth-embedded/limits/request-history.d.ts +77 -0
  95. package/dist/types/src/common-auth-embedded/limits/scope.d.ts +12 -0
  96. package/dist/types/src/common-auth-embedded/limits/signal.d.ts +41 -0
  97. package/dist/types/src/common-auth-embedded/limits/terminal.d.ts +52 -0
  98. package/dist/types/src/common-auth-embedded/limits/wake.d.ts +21 -0
  99. package/dist/types/src/common-auth-embedded/limits/wording.d.ts +25 -0
  100. package/dist/types/src/common-auth-embedded/opencode2/index.d.ts +1 -1
  101. package/dist/types/src/common-auth-embedded/opencode2/install.d.ts +2 -2
  102. package/dist/types/src/common-auth-embedded/opencode2/types.d.ts +95 -9
  103. package/dist/types/src/common-auth-embedded/routing/admission.d.ts +7 -2
  104. package/dist/types/src/common-auth-embedded/routing-accessor/index.d.ts +13 -3
  105. package/dist/types/src/common-auth-embedded/store/errors.d.ts +1 -1
  106. package/dist/types/src/common-auth-embedded/store/hooks.d.ts +5 -1
  107. package/dist/types/src/common-auth-embedded/store/index.d.ts +1 -0
  108. package/dist/types/src/common-auth-embedded/store/limits-namespace.d.ts +7 -0
  109. package/dist/types/src/common-auth-embedded/store/pool.d.ts +10 -1
  110. package/dist/types/src/common-auth-embedded/store/settings.d.ts +2 -0
  111. package/dist/types/src/runtime-layout.d.ts +9 -2
  112. package/dist/types/src/runtime-layout.d.ts.map +1 -1
  113. package/dist/types/src/setup.d.ts.map +1 -1
  114. package/package.json +2 -2
  115. package/src/common-auth-embedded/claustrum/consumer.d.ts +12 -0
  116. package/src/common-auth-embedded/claustrum/consumer.js +36 -0
  117. package/src/common-auth-embedded/claustrum/custody.d.ts +16 -4
  118. package/src/common-auth-embedded/claustrum/custody.js +74 -9
  119. package/src/common-auth-embedded/claustrum/hub.d.ts +3 -2
  120. package/src/common-auth-embedded/claustrum/hub.js +8 -0
  121. package/src/common-auth-embedded/claustrum/index.d.ts +1 -0
  122. package/src/common-auth-embedded/claustrum/roster.d.ts +2 -0
  123. package/src/common-auth-embedded/claustrum/roster.js +2 -0
  124. package/src/common-auth-embedded/claustrum/usage.d.ts +41 -0
  125. package/src/common-auth-embedded/claustrum/usage.js +145 -0
  126. package/src/common-auth-embedded/commands/builtins.d.ts +5 -0
  127. package/src/common-auth-embedded/commands/builtins.js +38 -0
  128. package/src/common-auth-embedded/fs/private-files.d.ts +5 -0
  129. package/src/common-auth-embedded/fs/private-files.js +7 -0
  130. package/src/common-auth-embedded/fs/refresh-file-lock.js +142 -47
  131. package/src/common-auth-embedded/fs/with-lock.js +2 -5
  132. package/src/common-auth-embedded/limits/activity.d.ts +26 -0
  133. package/src/common-auth-embedded/limits/axes.d.ts +63 -0
  134. package/src/common-auth-embedded/limits/decide.d.ts +141 -0
  135. package/src/common-auth-embedded/limits/duration.d.ts +8 -0
  136. package/src/common-auth-embedded/limits/families.d.ts +57 -0
  137. package/src/common-auth-embedded/limits/marker.d.ts +64 -0
  138. package/src/common-auth-embedded/limits/request-history.d.ts +77 -0
  139. package/src/common-auth-embedded/limits/scope.d.ts +12 -0
  140. package/src/common-auth-embedded/limits/signal.d.ts +41 -0
  141. package/src/common-auth-embedded/limits/terminal.d.ts +52 -0
  142. package/src/common-auth-embedded/limits/wake.d.ts +21 -0
  143. package/src/common-auth-embedded/limits/wording.d.ts +25 -0
  144. package/src/common-auth-embedded/opencode2/index.d.ts +1 -1
  145. package/src/common-auth-embedded/opencode2/install.d.ts +2 -2
  146. package/src/common-auth-embedded/opencode2/install.js +161 -18
  147. package/src/common-auth-embedded/opencode2/types.d.ts +95 -9
  148. package/src/common-auth-embedded/routing/admission.d.ts +7 -2
  149. package/src/common-auth-embedded/routing/admission.js +17 -7
  150. package/src/common-auth-embedded/routing-accessor/handle.js +10 -0
  151. package/src/common-auth-embedded/routing-accessor/index.d.ts +13 -3
  152. package/src/common-auth-embedded/routing-accessor/index.js +11 -10
  153. package/src/common-auth-embedded/rpc/port-file.d.ts +12 -3
  154. package/src/common-auth-embedded/rpc/port-file.js +46 -4
  155. package/src/common-auth-embedded/source-output.json +254 -118
  156. package/src/common-auth-embedded/store/attribution.js +3 -2
  157. package/src/common-auth-embedded/store/errors.d.ts +1 -1
  158. package/src/common-auth-embedded/store/hooks.d.ts +5 -1
  159. package/src/common-auth-embedded/store/hooks.js +20 -5
  160. package/src/common-auth-embedded/store/index.d.ts +1 -0
  161. package/src/common-auth-embedded/store/limits-namespace.d.ts +7 -0
  162. package/src/common-auth-embedded/store/limits-namespace.js +34 -0
  163. package/src/common-auth-embedded/store/pool.d.ts +10 -1
  164. package/src/common-auth-embedded/store/pool.js +2 -0
  165. package/src/common-auth-embedded/store/provider-state.js +5 -5
  166. package/src/common-auth-embedded/store/refresh.js +1 -1
  167. package/src/common-auth-embedded/store/schema.js +9 -4
  168. package/src/common-auth-embedded/store/settings.d.ts +2 -0
  169. package/src/common-auth-embedded/store/settings.js +11 -4
  170. package/src/tui-compiled/common-auth-embedded/fs/private-files.js +7 -0
  171. package/src/tui-compiled/common-auth-embedded/rpc/port-file.js +46 -4
  172. package/src/tui-raw/common-auth-embedded/fs/private-files.js +7 -0
  173. package/src/tui-raw/common-auth-embedded/rpc/port-file.js +46 -4
@@ -1,6 +1,6 @@
1
1
  import type { StoredCredential } from './schema.js';
2
2
  /** Every library operation that can fail, as named in the failure value. */
3
- export type PoolOperation = 'initialize' | 'add' | 'replace' | 'rotate' | 'disable' | 'enable' | 'remove' | 'reorder' | 'updateSettings' | 'recordIdentity' | 'updateProviderState' | 'refresh' | 'pull' | 'publishRoster';
3
+ export type PoolOperation = 'initialize' | 'add' | 'replace' | 'rotate' | 'disable' | 'enable' | 'remove' | 'reorder' | 'updateSettings' | 'updateLimitsNamespace' | 'recordIdentity' | 'updateProviderState' | 'refresh' | 'pull' | 'publishRoster';
4
4
  /**
5
5
  * How far an operation got before it failed.
6
6
  *
@@ -1,7 +1,11 @@
1
1
  import { type PoolOperation } from './errors.js';
2
2
  /** Refuses a row operation or refresh called from inside a hook. */
3
3
  export declare function assertNotInsideHook(operation: PoolOperation): void;
4
- export declare function runInsideHook<T>(operation: PoolOperation, fn: () => Promise<T> | T): Promise<T>;
4
+ /** Namespace writes need only the store locks, not the row lock a hook may retain. */
5
+ export declare function assertLimitsWriteAllowed(): void;
6
+ /** Codecs may run under store locks even when no asynchronous hook is active. */
7
+ export declare function runInsideCodec<T>(fn: () => T): T;
8
+ export declare function runInsideHook<T>(operation: PoolOperation, fn: () => Promise<T> | T, storeLocksReleased?: boolean): Promise<T>;
5
9
  export interface PoolLogger {
6
10
  warn(message: string, data?: unknown): void;
7
11
  }
@@ -1,18 +1,33 @@
1
1
  import { AsyncLocalStorage } from 'node:async_hooks';
2
2
  import { PoolReentryError } from './errors.js';
3
3
  /**
4
- * Set while a hook of a lock-holding operation runs. Async context is
5
- * inherited by every timer, promise and continuation created inside the hook,
4
+ * Set while a store hook runs, recording whether its store locks are released.
5
+ * Async context is inherited by every timer, promise and continuation inside the hook,
6
6
  * so work a hook schedules is inside the guarded region too.
7
7
  */
8
8
  const insideHook = new AsyncLocalStorage();
9
+ const insideCodec = new AsyncLocalStorage();
9
10
  /** Refuses a row operation or refresh called from inside a hook. */
10
11
  export function assertNotInsideHook(operation) {
11
12
  if (insideHook.getStore() !== undefined)
12
13
  throw new PoolReentryError(operation);
13
14
  }
14
- export function runInsideHook(operation, fn) {
15
- return insideHook.run(operation, async () => await fn());
15
+ /** Namespace writes need only the store locks, not the row lock a hook may retain. */
16
+ export function assertLimitsWriteAllowed() {
17
+ if (insideHook.getStore()?.storeLocksReleased === false ||
18
+ insideCodec.getStore())
19
+ throw new PoolReentryError('updateLimitsNamespace');
20
+ }
21
+ /** Codecs may run under store locks even when no asynchronous hook is active. */
22
+ export function runInsideCodec(fn) {
23
+ return insideCodec.run(true, fn);
24
+ }
25
+ export function runInsideHook(operation, fn, storeLocksReleased = false) {
26
+ return insideHook.run({
27
+ operation,
28
+ storeLocksReleased: storeLocksReleased &&
29
+ insideHook.getStore()?.storeLocksReleased !== false,
30
+ }, async () => await fn());
16
31
  }
17
32
  /**
18
33
  * Runs a failure hook. A hook that throws never replaces the failure it was
@@ -22,7 +37,7 @@ export async function callFailureHook(operation, hook, rowId, error, logger) {
22
37
  if (!hook)
23
38
  return;
24
39
  try {
25
- await runInsideHook(operation, () => hook(rowId, error));
40
+ await runInsideHook(operation, () => hook(rowId, error), true);
26
41
  }
27
42
  catch (hookError) {
28
43
  logger?.warn('store failure hook threw; the original failure stands', {
@@ -3,6 +3,7 @@ export type { PoolFailureKind, PoolFailurePhase, PoolOperation, } from './errors
3
3
  export { PoolOperationError, PoolReentryError } from './errors.js';
4
4
  export type { PoolLogger } from './hooks.js';
5
5
  export { countUnknownIdentityRows, DUPLICATE_IDENTITY_REASON, } from './identity.js';
6
+ export type { PoolLimitsMutator, PoolLimitsRow } from './limits-namespace.js';
6
7
  export type { HoldPoint, InitializeOutcome, WriteStep } from './mutate.js';
7
8
  export type { OpenPoolStoreOptions, PoolLoad, PoolStore, } from './pool.js';
8
9
  export { openPoolStore } from './pool.js';
@@ -0,0 +1,7 @@
1
+ import type { StoreRuntime } from './runtime.js';
2
+ import type { PoolRow } from './schema.js';
3
+ /** Row IDs, credential epochs and identities only; mutators never receive credentials. */
4
+ export type PoolLimitsRow = Pick<PoolRow, 'id' | 'credentialEpoch' | 'identity'>;
5
+ export type PoolLimitsMutator = (envelope: unknown, rows: readonly PoolLimitsRow[]) => unknown | Promise<unknown>;
6
+ /** A namespace merge and its check of current row bindings share one locked config snapshot. */
7
+ export declare function updatePoolLimitsNamespace(rt: StoreRuntime, mutator: PoolLimitsMutator): Promise<void>;
@@ -0,0 +1,34 @@
1
+ import { writeJsonAtomic } from '../fs/atomic-write.js';
2
+ import { assertLimitsWriteAllowed, runInsideHook } from './hooks.js';
3
+ import { notReadyError, readPool, runOperation } from './mutate.js';
4
+ /** A namespace merge and its check of current row bindings share one locked config snapshot. */
5
+ export async function updatePoolLimitsNamespace(rt, mutator) {
6
+ assertLimitsWriteAllowed();
7
+ const { ctx } = rt;
8
+ await runOperation(ctx, 'updateLimitsNamespace', undefined, undefined, async (locks, progress) => {
9
+ for (const spec of ctx.storeLocks)
10
+ await locks.acquire(spec);
11
+ const result = await readPool(ctx);
12
+ if (result.status !== 'ready')
13
+ throw notReadyError(result, 'updateLimitsNamespace', undefined);
14
+ const rows = result.rows.map(({ id, credentialEpoch, identity }) => ({
15
+ id,
16
+ credentialEpoch,
17
+ identity,
18
+ }));
19
+ const envelope = await runInsideHook('updateLimitsNamespace', () => mutator(structuredClone(result.config.commonAuthLimits), rows));
20
+ const config = { ...result.config, commonAuthLimits: envelope };
21
+ const info = {
22
+ operation: 'updateLimitsNamespace',
23
+ rowId: undefined,
24
+ };
25
+ await writeJsonAtomic(ctx.configPath, config, {
26
+ beforeRename: async () => {
27
+ await ctx.onStep?.('before-config-write', info);
28
+ await locks.assertAll();
29
+ },
30
+ });
31
+ progress.writes++;
32
+ await ctx.onStep?.('after-config-write', info);
33
+ });
34
+ }
@@ -2,6 +2,7 @@ import type { QuotaCommitTimer } from '../quota/commit-interval.js';
2
2
  import { type Attribution } from './attribution.js';
3
3
  import type { PoolOperationError } from './errors.js';
4
4
  import type { PoolLogger } from './hooks.js';
5
+ import { type PoolLimitsMutator } from './limits-namespace.js';
5
6
  import { type HoldPoint, type InitializeOutcome, type StoreContext } from './mutate.js';
6
7
  import { type ProviderStateMutator, type UpdateProviderStateResult } from './provider-state.js';
7
8
  import { type PublicationReceipt, type PublishOptions, type PublishPlan, type PublishResult } from './publication.js';
@@ -191,9 +192,17 @@ export interface PoolStore {
191
192
  /**
192
193
  * One locked write of the plugin's settings (since 0.2.6) beside the pool,
193
194
  * in the config file. Refuses a result that sets a pool-owned key
194
- * (`invalid-input`). Takes `extraLocks`, then the store locks.
195
+ * (`invalid-input`). Carries commonAuthLimits through unchanged and refuses
196
+ * an explicit alteration to it. Takes `extraLocks`, then the store locks.
195
197
  */
196
198
  updateSettings(mutator: SettingsMutator, options?: UpdateSettingsOptions): Promise<UpdateSettingsResult>;
199
+ /**
200
+ * Merges commonAuthLimits with current row bindings in one config write.
201
+ * Requires a ready pool and takes only the store locks. May run in an
202
+ * onPersisted/onFailure hook or a pull, but not in a lock-held mutator.
203
+ * Optional so existing structural implementations remain compatible.
204
+ */
205
+ updateLimitsNamespace?(mutator: PoolLimitsMutator): Promise<void>;
197
206
  /**
198
207
  * Records the identity a lookup found for the row's credential. Since
199
208
  * 0.3.1 it takes the credential epoch the lookup was issued for, and
@@ -1,4 +1,5 @@
1
1
  import { recordQuota } from './attribution.js';
2
+ import { updatePoolLimitsNamespace, } from './limits-namespace.js';
2
3
  import { initializePool, readPool, } from './mutate.js';
3
4
  import { updateProviderStateRow, } from './provider-state.js';
4
5
  import { publication, publishRoster, } from './publication.js';
@@ -126,6 +127,7 @@ export function openPoolStore(options) {
126
127
  reorder: (ids, callOptions) => reorderRows(rt, ids, callOptions),
127
128
  readSettings: () => readPoolSettings(rt),
128
129
  updateSettings: (mutator, callOptions) => updatePoolSettings(rt, mutator, callOptions),
130
+ updateLimitsNamespace: (mutator) => updatePoolLimitsNamespace(rt, mutator),
129
131
  recordIdentity: (id, identity, attribution, callOptions) => recordRowIdentity(rt, id, identity, attribution, callOptions),
130
132
  refresh: (id, provider, callOptions) => refreshRow(rt, id, provider, callOptions),
131
133
  recordQuota: record,
@@ -1,4 +1,4 @@
1
- import { assertNotInsideHook, runInsideHook } from './hooks.js';
1
+ import { assertNotInsideHook, runInsideCodec, runInsideHook } from './hooks.js';
2
2
  import { runOperation, withTransaction } from './mutate.js';
3
3
  import { readRow, refusal, requireBound, rowLockSpec, unknownRow, } from './runtime.js';
4
4
  import { boundProviderStateDigest, CREDENTIAL_STAMP_KEY, credentialDigest, isCredentialEpoch, isRecord, PROVIDER_STATE_KEY, parseStamp, providerStateDigest, rowLockKey, } from './schema.js';
@@ -21,7 +21,7 @@ export function acceptProviderState(codec, operation, id, value, what = 'the pro
21
21
  }
22
22
  if (normalized === undefined)
23
23
  throw refusal(operation, id, 'invalid-provider-state', `${what} cannot be stored as JSON`);
24
- if (!codec.validate(normalized))
24
+ if (!runInsideCodec(() => codec.validate(normalized)))
25
25
  throw refusal(operation, id, 'invalid-provider-state', `the provider-state codec rejected ${what}`);
26
26
  return normalized;
27
27
  }
@@ -35,7 +35,7 @@ export function mergedProviderState(codec, operation, id, onDisk, incoming) {
35
35
  return { kind: 'set', value: incoming };
36
36
  return {
37
37
  kind: 'set',
38
- value: acceptProviderState(codec, operation, id, codec.merge(structuredClone(onDisk), structuredClone(incoming)), 'the merged provider state'),
38
+ value: acceptProviderState(codec, operation, id, runInsideCodec(() => codec.merge?.(structuredClone(onDisk), structuredClone(incoming))), 'the merged provider state'),
39
39
  };
40
40
  }
41
41
  /**
@@ -48,7 +48,7 @@ export function mergedProviderState(codec, operation, id, onDisk, incoming) {
48
48
  export function replacementProviderState(codec, row, credentialEpoch, identity, incoming) {
49
49
  const hook = codec?.onReplace;
50
50
  const next = hook
51
- ? hook(row.providerState === undefined
51
+ ? runInsideCodec(() => hook(row.providerState === undefined
52
52
  ? undefined
53
53
  : structuredClone(row.providerState), {
54
54
  id: row.id,
@@ -60,7 +60,7 @@ export function replacementProviderState(codec, row, credentialEpoch, identity,
60
60
  ...(incoming !== undefined
61
61
  ? { incoming: structuredClone(incoming) }
62
62
  : {}),
63
- })
63
+ }))
64
64
  : incoming;
65
65
  if (next === undefined)
66
66
  return { kind: 'clear' };
@@ -200,7 +200,7 @@ export async function refreshRow(rt, id, provider, options = {}) {
200
200
  if (options.onPersisted) {
201
201
  try {
202
202
  const persisted = options.onPersisted;
203
- await runInsideHook('refresh', () => persisted(id, commit.stored));
203
+ await runInsideHook('refresh', () => persisted(id, commit.stored), true);
204
204
  }
205
205
  catch (cause) {
206
206
  throw new PoolOperationError({
@@ -1,4 +1,5 @@
1
1
  import { createHash } from 'node:crypto';
2
+ import { runInsideCodec } from './hooks.js';
2
3
  /** Top-level key of the config file that holds everything the pool adds. */
3
4
  export const POOL_KEY = 'commonAuthPool';
4
5
  /** The pool schema this library reads and writes. */
@@ -182,7 +183,9 @@ export function stampFor(credential, credentialEpoch, binding, options = {}) {
182
183
  * digest the store writes.
183
184
  */
184
185
  export function providerStateDigest(codec, value) {
185
- const bound = codec?.credentialBound ? codec.credentialBound(value) : value;
186
+ const bound = codec?.credentialBound
187
+ ? runInsideCodec(() => codec.credentialBound?.(value))
188
+ : value;
186
189
  return createHash('sha256')
187
190
  .update(`provider-state\0${JSON.stringify(bound ?? null)}`)
188
191
  .digest('hex');
@@ -420,8 +423,10 @@ function parseEntry(raw, codec) {
420
423
  return undefined;
421
424
  if ('disabledReason' in raw && typeof raw.disabledReason !== 'string')
422
425
  return undefined;
423
- const quota = 'quota' in raw && codec.decode ? codec.decode(raw.quota) : raw.quota;
424
- if ('quota' in raw && !codec.validate(quota))
426
+ const quota = 'quota' in raw && codec.decode
427
+ ? runInsideCodec(() => codec.decode?.(raw.quota))
428
+ : raw.quota;
429
+ if ('quota' in raw && !runInsideCodec(() => codec.validate(quota)))
425
430
  return undefined;
426
431
  if ('staged' in raw && !parseReservation(raw.staged))
427
432
  return undefined;
@@ -666,7 +671,7 @@ function providerStateFields(account, credential, credentialEpoch, identity, cod
666
671
  const value = account[PROVIDER_STATE_KEY];
667
672
  // The value is validated before it is projected, so the projection only
668
673
  // ever sees a value of the shape the codec accepts.
669
- if (!codec?.validate(value))
674
+ if (!codec || !runInsideCodec(() => codec.validate(value)))
670
675
  return { providerStateDropped: 'invalid' };
671
676
  if (providerStateDigest(codec, value) !== digest)
672
677
  return { providerStateDropped: 'uncovered' };
@@ -59,5 +59,7 @@ export declare function readPoolSettings(rt: StoreRuntime): Promise<SettingsRead
59
59
  * pool-owned key refuses (`invalid-input`) with nothing written; a result
60
60
  * equal to the current settings writes nothing. The state file is never
61
61
  * touched, and the pool-owned keys are written back exactly as read.
62
+ * commonAuthLimits is also carried through unchanged, even when omitted by
63
+ * the mutator; an explicit change to it refuses before writing anything.
62
64
  */
63
65
  export declare function updatePoolSettings(rt: StoreRuntime, mutator: SettingsMutator, options?: UpdateSettingsOptions): Promise<UpdateSettingsResult>;
@@ -36,12 +36,13 @@ function defineKey(target, key, value) {
36
36
  /**
37
37
  * The next config: the pool-owned keys exactly as read, every settings key
38
38
  * from `next` in its existing position, settings keys `next` dropped left
39
- * out, and new settings keys appended.
39
+ * out, and new settings keys appended. The limits envelope is always carried
40
+ * through from disk; ordinary settings mutators do not own that namespace.
40
41
  */
41
42
  function composeConfig(config, next) {
42
43
  const out = {};
43
44
  for (const [key, value] of Object.entries(config)) {
44
- if (POOL_OWNED_KEYS.includes(key))
45
+ if (POOL_OWNED_KEYS.includes(key) || key === 'commonAuthLimits')
45
46
  defineKey(out, key, value);
46
47
  else if (Object.hasOwn(next, key))
47
48
  defineKey(out, key, next[key]);
@@ -82,6 +83,8 @@ export async function readPoolSettings(rt) {
82
83
  * pool-owned key refuses (`invalid-input`) with nothing written; a result
83
84
  * equal to the current settings writes nothing. The state file is never
84
85
  * touched, and the pool-owned keys are written back exactly as read.
86
+ * commonAuthLimits is also carried through unchanged, even when omitted by
87
+ * the mutator; an explicit change to it refuses before writing anything.
85
88
  */
86
89
  export async function updatePoolSettings(rt, mutator, options = {}) {
87
90
  assertNotInsideHook('updateSettings');
@@ -109,9 +112,13 @@ export async function updatePoolSettings(rt, mutator, options = {}) {
109
112
  const owned = POOL_OWNED_KEYS.filter((key) => Object.hasOwn(next, key));
110
113
  if (owned.length > 0)
111
114
  throw settingsRefusal(`settings cannot set the pool-owned key(s) ${owned.join(', ')}`);
112
- if (isDeepStrictEqual(next, current))
113
- return { settings: current, outcome: 'unchanged' };
115
+ if (Object.hasOwn(next, 'commonAuthLimits') &&
116
+ (!Object.hasOwn(current, 'commonAuthLimits') ||
117
+ !isDeepStrictEqual(next.commonAuthLimits, current.commonAuthLimits)))
118
+ throw settingsRefusal('settings cannot alter commonAuthLimits; use updateLimitsNamespace');
114
119
  const config = composeConfig(result.config, next);
120
+ if (isDeepStrictEqual(settingsOf(config), current))
121
+ return { settings: current, outcome: 'unchanged' };
115
122
  const info = { operation: 'updateSettings', rowId: undefined };
116
123
  await writeJsonAtomic(ctx.configPath, config, {
117
124
  beforeRename: async () => {
@@ -4,8 +4,8 @@ import { dirname, join } from 'node:path';
4
4
  import { loadCommonAuthStoreModules } from '@cortexkit/antigravity-auth-core';
5
5
  const PIN = {
6
6
  package: '@cortexkit/common-auth',
7
- version: '0.33.0',
8
- tarballSha256: 'bba9f56f7fb353c9c712113722c7c9361f0e13e930c73f57314185b13032a24c',
7
+ version: '0.34.0',
8
+ tarballSha256: '381a0a78b6ca4873922ce1be1378c5b891136686a5ba85ffe6d39c6a2624f549',
9
9
  };
10
10
  async function filesBelow(root, dir) {
11
11
  const out = [];
@@ -55,6 +55,11 @@ export interface RuntimeLayoutLogger {
55
55
  info(message: string, meta?: Record<string, unknown>): unknown;
56
56
  warn(message: string, meta?: Record<string, unknown>): unknown;
57
57
  }
58
+ interface OldFileCleanupIO {
59
+ platform: NodeJS.Platform;
60
+ readFile(path: string): Promise<Buffer>;
61
+ unlink(path: string): Promise<void>;
62
+ }
58
63
  export interface RuntimeLayoutOptions {
59
64
  /** This build's version, for the version fence. */
60
65
  version: string;
@@ -74,8 +79,8 @@ export interface RuntimeLayoutOptions {
74
79
  */
75
80
  destinationPrivate?: (dir: string) => Promise<boolean>;
76
81
  /**
77
- * Test seams, run at one step of the move. A step that throws stands in for
78
- * a crash there: nothing after it runs.
82
+ * Test seams for the move. A step callback that throws stands in for a
83
+ * crash there: nothing after it runs.
79
84
  */
80
85
  steps?: {
81
86
  afterManifest?: () => void | Promise<void>;
@@ -87,6 +92,8 @@ export interface RuntimeLayoutOptions {
87
92
  * check that guards it.
88
93
  */
89
94
  beforeRemove?: (path: string) => void | Promise<void>;
95
+ /** Isolate old-copy comparison/removal from OS sharing races in tests. */
96
+ oldFileIO?: OldFileCleanupIO;
90
97
  };
91
98
  /** Test seam: how long to wait for the layout lock. */
92
99
  layoutLockWaitMs?: number;
@@ -1 +1 @@
1
- {"version":3,"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ED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAsB,mBAAmB,CACvC,OAAO,EAAE,oBAAoB,GAC5B,OAAO,CAAC,kBAAkB,GAAG,SAAS,CAAC,CAgCzC;AA2PD,yEAAyE;AACzE,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,EAAE,MAAM,CAAA;IACd,+DAA+D;IAC/D,IAAI,EAAE,OAAO,CAAA;CACd;AAED,MAAM,MAAM,eAAe,GACvB;IAAE,MAAM,EAAE,WAAW,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,GACnC;IAAE,MAAM,EAAE,SAAS,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GACrC;IAAE,MAAM,EAAE,WAAW,CAAA;CAAE,CAAA;AAE3B;;;;;GAKG;AACH,wBAAsB,YAAY,CAChC,KAAK,EAAE,cAAc,GACpB,OAAO,CAAC,eAAe,CAAC,CAe1B;AAuJD;;;;;;GAMG;AACH,wBAAsB,0BAA0B,CAC9C,EAAE,EAAE,MAAM,EACV,WAAW,EAAE,MAAM,EACnB,YAAY,GAAE,MAA6D,GAC1E,OAAO,CAAC,IAAI,CAAC,CAYf;AA2cD;;;;;;;;GAQG;AACH,wBAAsB,6BAA6B,CACjD,QAAQ,EAAE,MAAM,EAChB,WAAW,CAAC,EAAE,MAAM,GACnB,OAAO,CAAC,IAAI,CAAC,CAsBf"}
@@ -1355,7 +1355,7 @@ async function removeOldCopies(location, files, present, layoutLock, input) {
1355
1355
  if (input.keptVaultState && (await sameBytes(file.old, file.next)))
1356
1356
  continue;
1357
1357
  }
1358
- if (!(await unlinkIfIdentical(file, location.stateHomeDir, () => beforeRemove(file.old, layoutLock, input.steps))))
1358
+ if (!(await unlinkIfIdentical(file, location.stateHomeDir, () => beforeRemove(file.old, layoutLock, input.steps), input.steps?.oldFileIO)))
1359
1359
  kept.push(file.old);
1360
1360
  }
1361
1361
  const [first] = kept;
@@ -1385,9 +1385,9 @@ async function sameBytes(a, b) {
1385
1385
  * a writer that has not closed it yet) can refuse to be read or removed for a
1386
1386
  * moment. These codes do not say whether the file exists or what it holds.
1387
1387
  */
1388
- function isWindowsSharingError(error) {
1388
+ function isWindowsSharingError(error, platform) {
1389
1389
  const code = error?.code;
1390
- return (process.platform === 'win32' &&
1390
+ return (platform === 'win32' &&
1391
1391
  (code === 'EBUSY' || code === 'EPERM' || code === 'EACCES'));
1392
1392
  }
1393
1393
  /**
@@ -1399,14 +1399,14 @@ function isWindowsSharingError(error) {
1399
1399
  * or because the removal failed for another reason. Differing files, or
1400
1400
  * files unreadable for any other reason, throw `credentialConflict`.
1401
1401
  */
1402
- async function unlinkIfIdentical(file, stateDir, guard) {
1402
+ async function unlinkIfIdentical(file, stateDir, guard, io = { platform: process.platform, readFile, unlink }) {
1403
1403
  const started = performance.now();
1404
1404
  for (const delay of SHARING_RETRY_DELAYS_MS) {
1405
1405
  if (delay > 0)
1406
1406
  await new Promise((done) => setTimeout(done, delay));
1407
1407
  let same;
1408
1408
  try {
1409
- same = (await readFile(file.old)).equals(await readFile(file.next));
1409
+ same = (await io.readFile(file.old)).equals(await io.readFile(file.next));
1410
1410
  }
1411
1411
  catch (error) {
1412
1412
  if (error.code === 'ENOENT' &&
@@ -1415,7 +1415,7 @@ async function unlinkIfIdentical(file, stateDir, guard) {
1415
1415
  // A file Windows holds open is not evidence that the two copies differ:
1416
1416
  // compare again shortly, and past the budget leave the old file for the
1417
1417
  // next start, as when the unlink itself is refused.
1418
- if (isWindowsSharingError(error)) {
1418
+ if (isWindowsSharingError(error, io.platform)) {
1419
1419
  if (performance.now() - started >= SHARING_RETRY_BUDGET_MS)
1420
1420
  return false;
1421
1421
  continue;
@@ -1428,13 +1428,13 @@ async function unlinkIfIdentical(file, stateDir, guard) {
1428
1428
  throw credentialConflict(stateDir, file);
1429
1429
  await guard();
1430
1430
  try {
1431
- await unlink(file.old);
1431
+ await io.unlink(file.old);
1432
1432
  return true;
1433
1433
  }
1434
1434
  catch (error) {
1435
1435
  if (error.code === 'ENOENT')
1436
1436
  return true;
1437
- if (!isWindowsSharingError(error) ||
1437
+ if (!isWindowsSharingError(error, io.platform) ||
1438
1438
  performance.now() - started >= SHARING_RETRY_BUDGET_MS)
1439
1439
  return false;
1440
1440
  }