@cortexkit/common-auth 0.36.0 → 0.37.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.
@@ -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 = timedMarkKeyId(clear.key);
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;
@@ -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(snapshot.tombstones.map((t) => [timedMarkKeyId(t.key), t.revision]));
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 entries = mergeTimedMarkEntries(mark.mergeLaw, mark.entries, now, clears.get(timedMarkKeyId(mark.key)));
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
- const clear = tombstones.find((t) => timedMarkKeyId(t.key) === id);
118
- if (clear && command.baseRevision < clear.revision) {
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, clear?.revision);
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
- tombstones = tombstones.filter((t) => !ids.has(timedMarkKeyId(t.key)));
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({ key, revision });
190
+ tombstones.push({
191
+ key,
192
+ revision,
193
+ ...(cause === undefined ? {} : { cause }),
194
+ });
162
195
  }
163
196
  }
164
197
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/common-auth",
3
- "version": "0.36.0",
3
+ "version": "0.37.0",
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": {