@cortexkit/anthropic-auth-core 2.5.4 → 2.5.5

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/index.js CHANGED
@@ -8509,13 +8509,20 @@ async function mutateVaultRoster(path, change, options = {}) {
8509
8509
  });
8510
8510
  if (next) {
8511
8511
  await lock.assertOwned();
8512
- await writeJsonAtomic(path, next, {
8513
- beforeRename: async () => {
8514
- await options.beforePublish?.();
8515
- options.signal?.throwIfAborted();
8516
- await lock.assertOwned();
8517
- }
8518
- });
8512
+ try {
8513
+ await writeJsonAtomic(path, next, {
8514
+ beforeRename: async () => {
8515
+ await options.beforePublish?.();
8516
+ options.signal?.throwIfAborted();
8517
+ await lock.assertOwned();
8518
+ }
8519
+ });
8520
+ } catch (error) {
8521
+ if (error && typeof error === "object" && "published" in error && error.published === true)
8522
+ options.onPublished?.();
8523
+ throw error;
8524
+ }
8525
+ options.onPublished?.();
8519
8526
  }
8520
8527
  return result;
8521
8528
  });
@@ -17243,7 +17250,7 @@ async function recordQuota(rt, id, attribution, observation) {
17243
17250
  }
17244
17251
 
17245
17252
  // ../../node_modules/@cortexkit/common-auth/dist/store/limits-namespace.js
17246
- async function updatePoolLimitsNamespace(rt, mutator) {
17253
+ async function updatePoolLimitsNamespace(rt, mutator, options = {}) {
17247
17254
  assertLimitsWriteAllowed();
17248
17255
  const { ctx } = rt;
17249
17256
  await runOperation(ctx, "updateLimitsNamespace", undefined, undefined, async (locks, progress) => {
@@ -17263,13 +17270,21 @@ async function updatePoolLimitsNamespace(rt, mutator) {
17263
17270
  operation: "updateLimitsNamespace",
17264
17271
  rowId: undefined
17265
17272
  };
17266
- await writeJsonAtomic(ctx.configPath, config, {
17267
- beforeRename: async () => {
17268
- await ctx.onStep?.("before-config-write", info);
17269
- await locks.assertAll();
17270
- }
17271
- });
17273
+ try {
17274
+ await writeJsonAtomic(ctx.configPath, config, {
17275
+ beforeRename: async () => {
17276
+ await ctx.onStep?.("before-config-write", info);
17277
+ await locks.assertAll();
17278
+ options.beforePublish?.();
17279
+ }
17280
+ });
17281
+ } catch (error) {
17282
+ if (error && typeof error === "object" && "published" in error && error.published === true)
17283
+ options.onPublished?.();
17284
+ throw error;
17285
+ }
17272
17286
  progress.writes++;
17287
+ options.onPublished?.();
17273
17288
  await ctx.onStep?.("after-config-write", info);
17274
17289
  });
17275
17290
  }
@@ -19257,7 +19272,7 @@ function openPoolStore(options) {
19257
19272
  reorder: (ids, callOptions) => reorderRows(rt, ids, callOptions),
19258
19273
  readSettings: () => readPoolSettings(rt),
19259
19274
  updateSettings: (mutator, callOptions) => updatePoolSettings(rt, mutator, callOptions),
19260
- updateLimitsNamespace: (mutator) => updatePoolLimitsNamespace(rt, mutator),
19275
+ updateLimitsNamespace: (mutator, options2) => updatePoolLimitsNamespace(rt, mutator, options2),
19261
19276
  recordIdentity: (id, identity2, attribution, callOptions) => recordRowIdentity(rt, id, identity2, attribution, callOptions),
19262
19277
  refresh: (id, provider, callOptions) => refreshRow(rt, id, provider, callOptions),
19263
19278
  recordQuota: record2,
@@ -41132,6 +41147,136 @@ function isRecord20(value) {
41132
41147
  function supportsTurnEndNote(model) {
41133
41148
  return resolveClaudeModelCapabilities(model)?.turnEndNote === true;
41134
41149
  }
41150
+ var runnerReports = new Map;
41151
+ var SKIP_REPORT_LIMIT = 512;
41152
+ var RUNNER_REPORT_SESSION_LIMIT = 1024;
41153
+ function readRunnerNotes(handle) {
41154
+ try {
41155
+ const read = handle.turn_notes;
41156
+ if (typeof read !== "function")
41157
+ return [];
41158
+ const answer = read.call(handle);
41159
+ if (answer !== null && (typeof answer === "object" || typeof answer === "function") && typeof Reflect.get(answer, "then") === "function") {
41160
+ Promise.resolve(answer).catch(() => {});
41161
+ return [];
41162
+ }
41163
+ if (!Array.isArray(answer))
41164
+ return [];
41165
+ const notes = [];
41166
+ for (let index = 0;index < answer.length; index++) {
41167
+ try {
41168
+ const entry2 = answer[index];
41169
+ if (!isRecord20(entry2) || Array.isArray(entry2))
41170
+ continue;
41171
+ const { after_tool_use_id, text: text6 } = entry2;
41172
+ if (typeof after_tool_use_id !== "string" || typeof text6 !== "string" || !text6.trim() || new TextEncoder().encode(text6).byteLength > 16384)
41173
+ continue;
41174
+ notes.push({ after_tool_use_id, text: text6 });
41175
+ } catch {}
41176
+ }
41177
+ return notes;
41178
+ } catch {
41179
+ return [];
41180
+ }
41181
+ }
41182
+ function runnerNotes(sessionId) {
41183
+ try {
41184
+ const routing = Reflect.get(globalThis, Symbol.for("cortexkit.routing"));
41185
+ if (typeof routing !== "function")
41186
+ return;
41187
+ const handle = routing(sessionId);
41188
+ if (isRecord20(handle) && typeof handle.turn_notes === "function")
41189
+ return { handle, notes: readRunnerNotes(handle) };
41190
+ } catch {
41191
+ return;
41192
+ }
41193
+ }
41194
+ function reportRunnerNote(handle, payload) {
41195
+ Promise.resolve().then(() => {
41196
+ const report = handle.report;
41197
+ if (typeof report === "function")
41198
+ return report.call(handle, payload);
41199
+ }).catch(() => {});
41200
+ }
41201
+ function applyRunnerTurnNotes(body, options) {
41202
+ if (!options.sessionId)
41203
+ return 0;
41204
+ const runner = runnerNotes(options.sessionId);
41205
+ if (!runner)
41206
+ return 0;
41207
+ let reported3 = runnerReports.get(options.sessionId);
41208
+ if (reported3) {
41209
+ runnerReports.delete(options.sessionId);
41210
+ } else {
41211
+ reported3 = { models: new Map, skipped: new Set };
41212
+ if (runnerReports.size >= RUNNER_REPORT_SESSION_LIMIT) {
41213
+ const oldest = runnerReports.keys().next();
41214
+ if (!oldest.done)
41215
+ runnerReports.delete(oldest.value);
41216
+ }
41217
+ }
41218
+ runnerReports.set(options.sessionId, reported3);
41219
+ const supported = options.isOAuth && supportsTurnEndNote(body.model);
41220
+ const model = options.modelId;
41221
+ if (reported3.models.get(model) !== supported) {
41222
+ reported3.models.set(model, supported);
41223
+ reportRunnerNote(runner.handle, {
41224
+ kind: "turn_notes_supported",
41225
+ model,
41226
+ supported
41227
+ });
41228
+ }
41229
+ const messages = Array.isArray(body.messages) ? body.messages : [];
41230
+ const anchors = new Map;
41231
+ for (const [index, message] of messages.entries()) {
41232
+ if (!isRecord20(message) || message.role !== "user" || !Array.isArray(message.content))
41233
+ continue;
41234
+ for (const block of message.content) {
41235
+ if (isRecord20(block) && block.type === "tool_result" && typeof block.tool_use_id === "string" && !anchors.has(block.tool_use_id))
41236
+ anchors.set(block.tool_use_id, index);
41237
+ }
41238
+ }
41239
+ const insertions = new Map;
41240
+ let inserted = 0;
41241
+ for (const note of runner.notes) {
41242
+ const index = anchors.get(note.after_tool_use_id);
41243
+ const next = index === undefined ? undefined : messages[index + 1];
41244
+ const reason = !options.isOAuth ? "not_oauth" : !supported ? "unsupported_model" : index === undefined ? "anchor_missing" : isRecord20(next) && next.role === "user" ? "before_user_message" : undefined;
41245
+ if (reason) {
41246
+ if (!reported3.skipped.has(note.after_tool_use_id)) {
41247
+ if (reported3.skipped.size >= SKIP_REPORT_LIMIT) {
41248
+ const oldest = reported3.skipped.values().next();
41249
+ if (!oldest.done)
41250
+ reported3.skipped.delete(oldest.value);
41251
+ }
41252
+ reported3.skipped.add(note.after_tool_use_id);
41253
+ reportRunnerNote(runner.handle, {
41254
+ kind: "turn_note_skipped",
41255
+ after_tool_use_id: note.after_tool_use_id,
41256
+ reason
41257
+ });
41258
+ }
41259
+ continue;
41260
+ }
41261
+ if (index === undefined)
41262
+ continue;
41263
+ const bucket = insertions.get(index) ?? [];
41264
+ bucket.push(note);
41265
+ insertions.set(index, bucket);
41266
+ inserted++;
41267
+ }
41268
+ if (inserted) {
41269
+ body.messages = messages.flatMap((message, index) => [
41270
+ message,
41271
+ ...(insertions.get(index) ?? []).map((note) => ({
41272
+ role: "system",
41273
+ clear_at: "next_user_message",
41274
+ content: note.text
41275
+ }))
41276
+ ]);
41277
+ }
41278
+ return inserted;
41279
+ }
41135
41280
  function applyTurnEndNotes(body, options = {}) {
41136
41281
  if (!supportsTurnEndNote(body.model) || !Array.isArray(body.messages))
41137
41282
  return 0;
@@ -41591,6 +41736,7 @@ export {
41591
41736
  askRouting,
41592
41737
  applyTurnEndNotes,
41593
41738
  applyThinkingBindingControls,
41739
+ applyRunnerTurnNotes,
41594
41740
  applyMidConversationOutputConfig,
41595
41741
  applyCustomHeaders,
41596
41742
  applyClaudeCodeMetadata,
@@ -120,6 +120,8 @@ export interface VaultRosterLockContext {
120
120
  }
121
121
  export interface MutateVaultRosterOptions {
122
122
  signal?: AbortSignal;
123
+ /** Runs under the roster lock once published, even if a later step fails. Must not throw. */
124
+ onPublished?: () => void;
123
125
  /**
124
126
  * Runs when the new roster is fully staged on disk, immediately before the
125
127
  * rename that publishes it, so a caller can confirm a lease of its own at
@@ -3,7 +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
+ export type { PoolLimitsMutator, PoolLimitsRow, PoolLimitsWriteOptions, } from './limits-namespace.js';
7
7
  export type { HoldPoint, InitializeOutcome, WriteStep } from './mutate.js';
8
8
  export type { OpenPoolStoreOptions, PoolLoad, PoolStore, } from './pool.js';
9
9
  export { openPoolStore } from './pool.js';
@@ -3,5 +3,11 @@ import type { PoolRow } from './schema.js';
3
3
  /** Row IDs, credential epochs and identities only; mutators never receive credentials. */
4
4
  export type PoolLimitsRow = Pick<PoolRow, 'id' | 'credentialEpoch' | 'identity'>;
5
5
  export type PoolLimitsMutator = (envelope: unknown, rows: readonly PoolLimitsRow[]) => unknown | Promise<unknown>;
6
+ /** Publication bookkeeping runs under the same locks as the namespace merge. */
7
+ export interface PoolLimitsWriteOptions {
8
+ readonly beforePublish?: () => void;
9
+ /** Runs once publication happened, even if a later step fails. Must not throw. */
10
+ readonly onPublished?: () => void;
11
+ }
6
12
  /** 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>;
13
+ export declare function updatePoolLimitsNamespace(rt: StoreRuntime, mutator: PoolLimitsMutator, options?: PoolLimitsWriteOptions): Promise<void>;
@@ -202,7 +202,7 @@ export interface PoolStore {
202
202
  * onPersisted/onFailure hook or a pull, but not in a lock-held mutator.
203
203
  * Optional so existing structural implementations remain compatible.
204
204
  */
205
- updateLimitsNamespace?(mutator: PoolLimitsMutator): Promise<void>;
205
+ updateLimitsNamespace?(mutator: PoolLimitsMutator, options?: import('./limits-namespace.js').PoolLimitsWriteOptions): Promise<void>;
206
206
  /**
207
207
  * Records the identity a lookup found for the row's credential. Since
208
208
  * 0.3.1 it takes the credential epoch the lookup was issued for, and
@@ -8,6 +8,13 @@ export declare const TURN_END_NOTE_CONTENT = "Your reply above has already reach
8
8
  * mid-conversation-system-clear-at beta that Claude Code sends for these models.
9
9
  */
10
10
  export declare function supportsTurnEndNote(model: unknown): boolean;
11
+ /** Apply after our own turn-end notes, so runner notes land before them. */
12
+ export declare function applyRunnerTurnNotes(body: Record<string, unknown>, options: {
13
+ sessionId?: string;
14
+ /** Raw model id supplied by the host, not the wire body model. */
15
+ modelId: string;
16
+ isOAuth: boolean;
17
+ }): number;
11
18
  /**
12
19
  * Call only on OAuth histories, after effort-marker validation and stripping.
13
20
  * Map configured host names with the same stateless mapping used for history,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/anthropic-auth-core",
3
- "version": "2.5.4",
3
+ "version": "2.5.5",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -30,7 +30,7 @@
30
30
  "prepublishOnly": "bun run build"
31
31
  },
32
32
  "devDependencies": {
33
- "@cortexkit/common-auth": "0.37.1"
33
+ "@cortexkit/common-auth": "0.38.0"
34
34
  },
35
35
  "dependencies": {
36
36
  "@cortexkit/claustrum-client": "0.13.0",