@animalabs/membrane 0.5.80 → 0.5.81

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 (151) hide show
  1. package/dist/context/index.d.ts +1 -1
  2. package/dist/context/index.d.ts.map +1 -1
  3. package/dist/context/index.js +1 -1
  4. package/dist/context/index.js.map +1 -1
  5. package/dist/context/process.d.ts +3 -1
  6. package/dist/context/process.d.ts.map +1 -1
  7. package/dist/context/process.js +230 -49
  8. package/dist/context/process.js.map +1 -1
  9. package/dist/context/types.d.ts +30 -0
  10. package/dist/context/types.d.ts.map +1 -1
  11. package/dist/context/types.js +16 -0
  12. package/dist/context/types.js.map +1 -1
  13. package/dist/floating-cache-marker.test.js +21 -0
  14. package/dist/floating-cache-marker.test.js.map +1 -1
  15. package/dist/formatters/anthropic-xml.d.ts +2 -0
  16. package/dist/formatters/anthropic-xml.d.ts.map +1 -1
  17. package/dist/formatters/anthropic-xml.js +57 -19
  18. package/dist/formatters/anthropic-xml.js.map +1 -1
  19. package/dist/formatters/normalize-tool-pairs.d.ts +7 -0
  20. package/dist/formatters/normalize-tool-pairs.d.ts.map +1 -1
  21. package/dist/formatters/normalize-tool-pairs.js +45 -0
  22. package/dist/formatters/normalize-tool-pairs.js.map +1 -1
  23. package/dist/formatters/types.d.ts +29 -0
  24. package/dist/formatters/types.d.ts.map +1 -1
  25. package/dist/membrane.d.ts +137 -8
  26. package/dist/membrane.d.ts.map +1 -1
  27. package/dist/membrane.js +812 -310
  28. package/dist/membrane.js.map +1 -1
  29. package/dist/providers/anthropic.d.ts +26 -0
  30. package/dist/providers/anthropic.d.ts.map +1 -1
  31. package/dist/providers/anthropic.js +142 -25
  32. package/dist/providers/anthropic.js.map +1 -1
  33. package/dist/providers/bedrock.d.ts +8 -0
  34. package/dist/providers/bedrock.d.ts.map +1 -1
  35. package/dist/providers/bedrock.js +24 -1
  36. package/dist/providers/bedrock.js.map +1 -1
  37. package/dist/providers/gemini.d.ts +11 -0
  38. package/dist/providers/gemini.d.ts.map +1 -1
  39. package/dist/providers/gemini.js +104 -82
  40. package/dist/providers/gemini.js.map +1 -1
  41. package/dist/providers/mock.d.ts +2 -0
  42. package/dist/providers/mock.d.ts.map +1 -1
  43. package/dist/providers/mock.js +2 -0
  44. package/dist/providers/mock.js.map +1 -1
  45. package/dist/providers/openai-compatible.d.ts +7 -0
  46. package/dist/providers/openai-compatible.d.ts.map +1 -1
  47. package/dist/providers/openai-compatible.js +99 -48
  48. package/dist/providers/openai-compatible.js.map +1 -1
  49. package/dist/providers/openai-completions.d.ts +5 -0
  50. package/dist/providers/openai-completions.d.ts.map +1 -1
  51. package/dist/providers/openai-completions.js +107 -44
  52. package/dist/providers/openai-completions.js.map +1 -1
  53. package/dist/providers/openai-responses-api.d.ts +16 -0
  54. package/dist/providers/openai-responses-api.d.ts.map +1 -1
  55. package/dist/providers/openai-responses-api.js +38 -10
  56. package/dist/providers/openai-responses-api.js.map +1 -1
  57. package/dist/providers/openai-responses.d.ts +2 -0
  58. package/dist/providers/openai-responses.d.ts.map +1 -1
  59. package/dist/providers/openai-responses.js +8 -1
  60. package/dist/providers/openai-responses.js.map +1 -1
  61. package/dist/providers/openai.d.ts +7 -0
  62. package/dist/providers/openai.d.ts.map +1 -1
  63. package/dist/providers/openai.js +92 -44
  64. package/dist/providers/openai.js.map +1 -1
  65. package/dist/providers/openrouter.d.ts +9 -0
  66. package/dist/providers/openrouter.d.ts.map +1 -1
  67. package/dist/providers/openrouter.js +118 -62
  68. package/dist/providers/openrouter.js.map +1 -1
  69. package/dist/providers/utils.d.ts +74 -2
  70. package/dist/providers/utils.d.ts.map +1 -1
  71. package/dist/providers/utils.js +184 -3
  72. package/dist/providers/utils.js.map +1 -1
  73. package/dist/registry/default-pricing.d.ts +22 -0
  74. package/dist/registry/default-pricing.d.ts.map +1 -1
  75. package/dist/registry/default-pricing.js +162 -28
  76. package/dist/registry/default-pricing.js.map +1 -1
  77. package/dist/types/content.d.ts +13 -0
  78. package/dist/types/content.d.ts.map +1 -1
  79. package/dist/types/content.js.map +1 -1
  80. package/dist/types/errors.d.ts +15 -0
  81. package/dist/types/errors.d.ts.map +1 -1
  82. package/dist/types/errors.js +26 -0
  83. package/dist/types/errors.js.map +1 -1
  84. package/dist/types/index.d.ts +3 -3
  85. package/dist/types/index.d.ts.map +1 -1
  86. package/dist/types/index.js +1 -1
  87. package/dist/types/index.js.map +1 -1
  88. package/dist/types/provider.d.ts +50 -0
  89. package/dist/types/provider.d.ts.map +1 -1
  90. package/dist/types/response.d.ts +85 -4
  91. package/dist/types/response.d.ts.map +1 -1
  92. package/dist/types/response.js.map +1 -1
  93. package/dist/types/streaming.d.ts +6 -1
  94. package/dist/types/streaming.d.ts.map +1 -1
  95. package/dist/utils/cache-marker-budget.d.ts +82 -0
  96. package/dist/utils/cache-marker-budget.d.ts.map +1 -0
  97. package/dist/utils/cache-marker-budget.js +189 -0
  98. package/dist/utils/cache-marker-budget.js.map +1 -0
  99. package/dist/utils/cost.d.ts +22 -0
  100. package/dist/utils/cost.d.ts.map +1 -1
  101. package/dist/utils/cost.js +55 -0
  102. package/dist/utils/cost.js.map +1 -1
  103. package/dist/utils/thinking-carriers.d.ts +80 -0
  104. package/dist/utils/thinking-carriers.d.ts.map +1 -0
  105. package/dist/utils/thinking-carriers.js +115 -0
  106. package/dist/utils/thinking-carriers.js.map +1 -0
  107. package/dist/utils/tool-parser.d.ts +24 -0
  108. package/dist/utils/tool-parser.d.ts.map +1 -1
  109. package/dist/utils/tool-parser.js +367 -161
  110. package/dist/utils/tool-parser.js.map +1 -1
  111. package/dist/utils/usage.d.ts +71 -0
  112. package/dist/utils/usage.d.ts.map +1 -0
  113. package/dist/utils/usage.js +161 -0
  114. package/dist/utils/usage.js.map +1 -0
  115. package/dist/yielding-stream.d.ts +8 -0
  116. package/dist/yielding-stream.d.ts.map +1 -1
  117. package/dist/yielding-stream.js +49 -3
  118. package/dist/yielding-stream.js.map +1 -1
  119. package/package.json +1 -1
  120. package/src/context/index.ts +1 -0
  121. package/src/context/process.ts +305 -59
  122. package/src/context/types.ts +38 -0
  123. package/src/floating-cache-marker.test.ts +24 -0
  124. package/src/formatters/anthropic-xml.ts +62 -20
  125. package/src/formatters/normalize-tool-pairs.ts +49 -0
  126. package/src/formatters/types.ts +32 -0
  127. package/src/membrane.ts +963 -313
  128. package/src/providers/anthropic.ts +161 -19
  129. package/src/providers/bedrock.ts +25 -1
  130. package/src/providers/gemini.ts +125 -87
  131. package/src/providers/mock.ts +3 -0
  132. package/src/providers/openai-compatible.ts +102 -45
  133. package/src/providers/openai-completions.ts +112 -46
  134. package/src/providers/openai-responses-api.ts +45 -15
  135. package/src/providers/openai-responses.ts +8 -1
  136. package/src/providers/openai.ts +95 -42
  137. package/src/providers/openrouter.ts +125 -63
  138. package/src/providers/utils.ts +227 -6
  139. package/src/registry/default-pricing.ts +165 -28
  140. package/src/types/content.ts +14 -0
  141. package/src/types/errors.ts +28 -0
  142. package/src/types/index.ts +6 -0
  143. package/src/types/provider.ts +55 -0
  144. package/src/types/response.ts +109 -8
  145. package/src/types/streaming.ts +8 -2
  146. package/src/utils/cache-marker-budget.ts +208 -0
  147. package/src/utils/cost.ts +64 -0
  148. package/src/utils/thinking-carriers.ts +125 -0
  149. package/src/utils/tool-parser.ts +473 -174
  150. package/src/utils/usage.ts +184 -0
  151. package/src/yielding-stream.ts +54 -3
@@ -20,8 +20,10 @@ import {
20
20
  authError,
21
21
  serverError,
22
22
  abortError,
23
+ unsupportedError,
23
24
  } from '../types/index.js';
24
25
  import { flattenRootSchemaUnion } from './anthropic-tool-schema.js';
26
+ import { assertTerminalEventObserved } from './utils.js';
25
27
  import { CacheKeepalive, type CacheKeepaliveConfig } from '../cache-keepalive.js';
26
28
 
27
29
  // ============================================================================
@@ -144,6 +146,18 @@ export interface AnthropicAdapterConfig {
144
146
 
145
147
  /** Default headers to include with Anthropic requests */
146
148
  defaultHeaders?: ClientOptions['defaultHeaders'];
149
+
150
+ /**
151
+ * Live per-request headers, evaluated at request time — for values that
152
+ * change between calls (e.g. household telemetry stamps such as
153
+ * `x-gate-debt-chunks`, read by an inference gateway and stripped there
154
+ * before the vendor ever sees them). Merged over the per-request beta
155
+ * headers for the OUTGOING request only; cache-keepalive replays
156
+ * deliberately resend their recorded headers, so a telemetry stamp is
157
+ * never replayed stale — an unstamped touch is honest, a stale stamp lies.
158
+ * null/undefined/'' values are dropped.
159
+ */
160
+ dynamicHeaders?: () => Record<string, string | number | null | undefined>;
147
161
 
148
162
  /** Default max tokens */
149
163
  defaultMaxTokens?: number;
@@ -164,6 +178,14 @@ export interface AnthropicAdapterConfig {
164
178
 
165
179
  export class AnthropicAdapter implements ProviderAdapter {
166
180
  readonly name = 'anthropic';
181
+
182
+ /**
183
+ * Verified live 2026-08-25 (claude-haiku-4-5, 4,650-token cached system
184
+ * prompt): call 1 returned input_tokens 8 / cache_creation_input_tokens 4650,
185
+ * call 2 input_tokens 8 / cache_read_input_tokens 4650. `input_tokens` never
186
+ * counts the cached span.
187
+ */
188
+ readonly usageCacheConvention = 'cache-excluded' as const;
167
189
  private client: Anthropic;
168
190
  private defaultMaxTokens: number;
169
191
  /** Any anthropic-beta value from defaultHeaders (e.g. the oauth beta for
@@ -173,6 +195,8 @@ export class AnthropicAdapter implements ProviderAdapter {
173
195
  private defaultBeta: string | undefined;
174
196
  /** Holds idle agents' cached prefixes warm; undefined when disabled. */
175
197
  readonly cacheKeepalive: CacheKeepalive | undefined;
198
+ /** Live per-request header source (see AnthropicAdapterConfig.dynamicHeaders). */
199
+ private readonly dynamicHeaders?: () => Record<string, string | number | null | undefined>;
176
200
 
177
201
  constructor(config: AnthropicAdapterConfig = {}) {
178
202
  const clientOptions: ClientOptions = {
@@ -180,6 +204,7 @@ export class AnthropicAdapter implements ProviderAdapter {
180
204
  defaultHeaders: config.defaultHeaders,
181
205
  };
182
206
  this.defaultBeta = extractBetaHeader(config.defaultHeaders);
207
+ this.dynamicHeaders = config.dynamicHeaders;
183
208
 
184
209
  if (config.authToken !== undefined) {
185
210
  clientOptions.authToken = config.authToken;
@@ -225,7 +250,7 @@ export class AnthropicAdapter implements ProviderAdapter {
225
250
  try {
226
251
  const response = await this.client.messages.create(fullRequest, {
227
252
  signal: options?.signal,
228
- headers,
253
+ headers: this.liveHeaders(headers),
229
254
  });
230
255
 
231
256
  return this.parseResponse(response, fullRequest);
@@ -298,7 +323,7 @@ export class AnthropicAdapter implements ProviderAdapter {
298
323
  try {
299
324
  const stream = await this.client.messages.stream(anthropicRequest, {
300
325
  signal: idleAbort.signal,
301
- headers: this.betaHeaders(request),
326
+ headers: this.liveHeaders(this.betaHeaders(request)),
302
327
  });
303
328
 
304
329
  // Accumulate response metadata from SSE events directly, so we can
@@ -314,6 +339,7 @@ export class AnthropicAdapter implements ProviderAdapter {
314
339
  let inferenceGeo: string | undefined;
315
340
  let serviceTier: string | undefined;
316
341
  let stopReason: string = 'end_turn';
342
+ let sawTerminalEvent = false;
317
343
  let stopSequence: string | undefined;
318
344
  let stopDetails: unknown;
319
345
 
@@ -329,6 +355,43 @@ export class AnthropicAdapter implements ProviderAdapter {
329
355
  // thinking blocks with no thinking_delta at all (signature only).
330
356
  const wrapThinkingTags = options?.wrapThinkingTags === true;
331
357
  let thinkingTagOpen = false;
358
+ // Index of a block that has started and not yet been stopped. A stream
359
+ // can end with one still open: measured live against claude-haiku-4-5
360
+ // (2026-08-25), a tool call truncated by max_tokens emits
361
+ // content_block_start + input_json_delta fragments and then goes
362
+ // straight to message_delta — no content_block_stop at all.
363
+ let openBlockIndex = -1;
364
+
365
+ const finalizeContentBlock = (blockIdx: number, sawBlockStop: boolean): void => {
366
+ const block = contentBlocks[blockIdx];
367
+ if (block) {
368
+ if (block.type === 'text') {
369
+ block.text = currentBlockContent;
370
+ } else if (block.type === 'thinking') {
371
+ block.thinking = currentBlockContent;
372
+ if (thinkingTagOpen) {
373
+ callbacks.onChunk('</thinking>\n');
374
+ thinkingTagOpen = false;
375
+ }
376
+ } else if (block.type === 'tool_use') {
377
+ if (!sawBlockStop) {
378
+ // Arguments never finished arriving, so `input` is still the
379
+ // empty object content_block_start carried — a plausible no-arg
380
+ // call that nothing downstream can distinguish from a real one.
381
+ block.unparseableInput = currentBlockInputJson;
382
+ } else if (currentBlockInputJson) {
383
+ try {
384
+ block.input = JSON.parse(currentBlockInputJson);
385
+ } catch {
386
+ // Same fabrication, reached by a block that did stop: keep the
387
+ // raw accumulation and mark it so consumers can refuse.
388
+ block.unparseableInput = currentBlockInputJson;
389
+ }
390
+ }
391
+ }
392
+ }
393
+ callbacks.onContentBlock?.(blockIdx, contentBlocks[blockIdx]);
394
+ };
332
395
 
333
396
  for await (const event of stream) {
334
397
  sawEvent = true;
@@ -354,6 +417,7 @@ export class AnthropicAdapter implements ProviderAdapter {
354
417
 
355
418
  } else if (event.type === 'content_block_start') {
356
419
  currentBlockIndex = event.index;
420
+ openBlockIndex = event.index;
357
421
  currentBlockContent = '';
358
422
  currentBlockInputJson = '';
359
423
  contentBlocks[currentBlockIndex] = { ...event.content_block };
@@ -387,22 +451,8 @@ export class AnthropicAdapter implements ProviderAdapter {
387
451
 
388
452
  } else if (event.type === 'content_block_stop') {
389
453
  // Finalize block — use event.index for defensive correctness
390
- const blockIdx = (event as { index: number }).index;
391
- const block = contentBlocks[blockIdx];
392
- if (block) {
393
- if (block.type === 'text') {
394
- block.text = currentBlockContent;
395
- } else if (block.type === 'thinking') {
396
- block.thinking = currentBlockContent;
397
- if (thinkingTagOpen) {
398
- callbacks.onChunk('</thinking>\n');
399
- thinkingTagOpen = false;
400
- }
401
- } else if (block.type === 'tool_use' && currentBlockInputJson) {
402
- try { block.input = JSON.parse(currentBlockInputJson); } catch { /* partial JSON */ }
403
- }
404
- }
405
- callbacks.onContentBlock?.(blockIdx, contentBlocks[blockIdx]);
454
+ finalizeContentBlock((event as { index: number }).index, true);
455
+ openBlockIndex = -1;
406
456
 
407
457
  } else if (event.type === 'message_delta') {
408
458
  // All content blocks are finalized by the time message_delta arrives.
@@ -414,6 +464,7 @@ export class AnthropicAdapter implements ProviderAdapter {
414
464
  stop_details?: unknown;
415
465
  };
416
466
  stopReason = delta.stop_reason ?? 'end_turn';
467
+ sawTerminalEvent = true;
417
468
  stopSequence = delta.stop_sequence ?? undefined;
418
469
  // stop_details carries refusal metadata (e.g., category: 'reasoning_extraction')
419
470
  stopDetails = delta.stop_details ?? undefined;
@@ -438,9 +489,22 @@ export class AnthropicAdapter implements ProviderAdapter {
438
489
  if (idleTimer) clearTimeout(idleTimer);
439
490
  options?.signal?.removeEventListener('abort', onExternalAbort);
440
491
 
492
+ // A block still open here never received its content_block_stop: the
493
+ // turn ended mid-block. Finalize it so the accumulated text is not lost
494
+ // and a truncated tool call is marked rather than persisted as `{}`.
495
+ if (openBlockIndex >= 0) {
496
+ finalizeContentBlock(openBlockIndex, false);
497
+ openBlockIndex = -1;
498
+ }
499
+
441
500
  // Force-close the HTTP connection so we don't block on SSE drain
442
501
  try { stream.controller.abort(); } catch { /* already closed */ }
443
502
 
503
+ // message_delta is this adapter's terminal event — the loop breaks on
504
+ // it. Falling out of the for-await without one means the SSE connection
505
+ // closed mid-turn, and stopReason is still its 'end_turn' initialiser.
506
+ assertTerminalEventObserved(sawTerminalEvent, 'Anthropic', fullRequest);
507
+
444
508
  return {
445
509
  content: contentBlocks,
446
510
  stopReason,
@@ -506,6 +570,19 @@ export class AnthropicAdapter implements ProviderAdapter {
506
570
  * the SDK replaces same-key defaults instead of merging, and the API
507
571
  * accepts comma-separated betas. Undefined when nothing to add, so the
508
572
  * defaults apply untouched. */
573
+ /** Base headers + the live dynamicHeaders stamp. Request time only: the
574
+ * keepalive recorder receives the base headers BEFORE this merge, so
575
+ * replayed touches never carry a stale telemetry value. */
576
+ private liveHeaders(base: Record<string, string> | undefined): Record<string, string> | undefined {
577
+ const dyn = this.dynamicHeaders?.();
578
+ if (!dyn) return base;
579
+ const out: Record<string, string> = { ...(base ?? {}) };
580
+ for (const [k, v] of Object.entries(dyn)) {
581
+ if (v !== null && v !== undefined && v !== '') out[k] = String(v);
582
+ }
583
+ return Object.keys(out).length ? out : undefined;
584
+ }
585
+
509
586
  private betaHeaders(request: ProviderRequest): Record<string, string> | undefined {
510
587
  if (!thinkingEnabled(request) || !needsInterleavedThinkingBeta(request.model)) {
511
588
  return undefined;
@@ -672,6 +749,10 @@ export class AnthropicAdapter implements ProviderAdapter {
672
749
  }
673
750
 
674
751
  private handleError(error: unknown, rawRequest?: unknown): MembraneError {
752
+ // Already-classified failures (e.g. the stream-integrity guards) keep
753
+ // their type and retryability instead of being re-derived from a string.
754
+ if (error instanceof MembraneError) return error;
755
+
675
756
  if (error instanceof Anthropic.APIError) {
676
757
  // Mid-stream SSE `error` events are rethrown by the SDK as APIError
677
758
  // with status === undefined (sdk core/streaming.js), so the HTTP
@@ -880,6 +961,23 @@ export function toAnthropicContent(blocks: ContentBlock[]): Anthropic.ContentBlo
880
961
  media_type: block.source.mediaType as 'application/pdf',
881
962
  data: block.source.data,
882
963
  },
964
+ // Anthropic's document block carries the filename as `title`; it was
965
+ // being dropped, so the model lost the one hint about what the PDF is.
966
+ ...(block.filename ? { title: block.filename } : {}),
967
+ });
968
+ break;
969
+
970
+ case 'generated_image':
971
+ // A provider-generated image is a base64 image with a MIME type, which
972
+ // is exactly Anthropic's image block — carrying it across costs nothing
973
+ // and lets an image Gemini produced re-enter Anthropic history.
974
+ result.push({
975
+ type: 'image',
976
+ source: {
977
+ type: 'base64',
978
+ media_type: block.mimeType as 'image/jpeg' | 'image/png' | 'image/gif' | 'image/webp',
979
+ data: block.data,
980
+ },
883
981
  });
884
982
  break;
885
983
 
@@ -919,12 +1017,53 @@ export function toAnthropicContent(blocks: ContentBlock[]): Anthropic.ContentBlo
919
1017
  data: (block as any).data,
920
1018
  } as any);
921
1019
  break;
1020
+
1021
+ default: {
1022
+ // The REQUEST path cannot degrade gracefully: a dropped block reaches
1023
+ // the model as an absence, and it answers about content it was never
1024
+ // shown. `audio` and `video` have no Anthropic Messages representation
1025
+ // at all, and a block type added later would silently join them. Fail
1026
+ // loudly at the boundary instead — the caller can strip or transcode.
1027
+ const unsupportedType = (block as { type?: string }).type ?? 'unknown';
1028
+ throw unsupportedError(
1029
+ `Anthropic has no representation for a "${unsupportedType}" content block, and dropping`
1030
+ + ' it would send the model a message missing content the caller supplied.'
1031
+ + ' Remove or convert the block before sending it on this provider.'
1032
+ );
1033
+ }
922
1034
  }
923
1035
  }
924
1036
 
925
1037
  return result;
926
1038
  }
927
1039
 
1040
+ /** Unrecognised response block types warn once each, not once per conversion. */
1041
+ const warnedUnconvertibleResponseBlocks = new Set<string>();
1042
+
1043
+ /**
1044
+ * The RESPONSE path can degrade gracefully where the request path cannot: the
1045
+ * provider's own block is in hand, so keeping it verbatim on a zero-width
1046
+ * carrier loses nothing recoverable and lets formatters replay it. Warn once
1047
+ * per type so a new provider block type surfaces without flooding the log.
1048
+ */
1049
+ function preserveUnconvertibleBlock(block: unknown, sourceLabel: string): ContentBlock {
1050
+ const blockType = (block as { type?: string })?.type ?? 'unknown';
1051
+ if (!warnedUnconvertibleResponseBlocks.has(blockType)) {
1052
+ warnedUnconvertibleResponseBlocks.add(blockType);
1053
+ console.warn(
1054
+ `[membrane:${sourceLabel}] no normalized ContentBlock for provider block type`
1055
+ + ` "${blockType}" — preserving it verbatim as a rawItem carrier so it can be`
1056
+ + ' replayed, but its content is not visible to normalized consumers.'
1057
+ );
1058
+ }
1059
+ return { type: 'text', text: '', rawItem: block } as ContentBlock;
1060
+ }
1061
+
1062
+ /** Test seam: the once-per-type warn latch is process-wide otherwise. */
1063
+ export function resetUnconvertibleBlockWarnings(): void {
1064
+ warnedUnconvertibleResponseBlocks.clear();
1065
+ }
1066
+
928
1067
  /**
929
1068
  * Convert Anthropic response content to normalized format
930
1069
  */
@@ -955,11 +1094,14 @@ export function fromAnthropicContent(blocks: Anthropic.ContentBlock[]): ContentB
955
1094
  break;
956
1095
 
957
1096
  default:
958
- // Handle redacted_thinking or unknown types
959
1097
  if ((block as any).type === 'redacted_thinking') {
960
1098
  // Preserve the encrypted `data` payload — without it the block
961
1099
  // cannot be round-tripped and prior reasoning is lost.
962
1100
  result.push({ type: 'redacted_thinking', data: (block as any).data } as any);
1101
+ } else {
1102
+ // server_tool_use, web_search_tool_result, search_result, mcp_tool_use
1103
+ // and anything Anthropic adds later used to fall out here silently.
1104
+ result.push(preserveUnconvertibleBlock(block, 'anthropic'));
963
1105
  }
964
1106
  break;
965
1107
  }
@@ -20,7 +20,7 @@ import {
20
20
  serverError,
21
21
  abortError,
22
22
  } from '../types/index.js';
23
- import { createCombinedSignal } from './utils.js';
23
+ import { createCombinedSignal, isDeadlineAbort, deadlineTimeoutError, assertTerminalEventObserved } from './utils.js';
24
24
  import {
25
25
  INTERLEAVED_THINKING_BETA,
26
26
  needsInterleavedThinkingBeta,
@@ -273,6 +273,15 @@ async function signRequest(
273
273
  export class BedrockAdapter implements ProviderAdapter {
274
274
  readonly name = 'bedrock';
275
275
 
276
+ /**
277
+ * Bedrock serves Anthropic models over the Anthropic Messages payload shape
278
+ * and this adapter reads Anthropic's own field names
279
+ * (`cache_read_input_tokens` / `cache_creation_input_tokens`), so it inherits
280
+ * the convention verified live against api.anthropic.com on 2026-08-25.
281
+ * Derived from the wire contract, not measured against Bedrock directly.
282
+ */
283
+ readonly usageCacheConvention = 'cache-excluded' as const;
284
+
276
285
  private accessKeyId: string;
277
286
  private secretAccessKey: string;
278
287
  private sessionToken?: string;
@@ -610,6 +619,7 @@ export class BedrockAdapter implements ProviderAdapter {
610
619
  let cacheCreationTokens: number | undefined;
611
620
  let cacheReadTokens: number | undefined;
612
621
  let stopReason: string = 'end_turn';
622
+ let sawTerminalEvent = false;
613
623
  let stopSequence: string | undefined;
614
624
  let fullText = '';
615
625
 
@@ -782,6 +792,7 @@ export class BedrockAdapter implements ProviderAdapter {
782
792
  }
783
793
  callbacks.onContentBlock?.(blockIdx, contentBlocks[blockIdx]);
784
794
  } else if (eventData.type === 'message_delta') {
795
+ sawTerminalEvent = true;
785
796
  if (eventData.usage) {
786
797
  outputTokens = eventData.usage.output_tokens;
787
798
  // message_delta carries cumulative cache metrics — use as
@@ -833,6 +844,11 @@ export class BedrockAdapter implements ProviderAdapter {
833
844
  );
834
845
  }
835
846
 
847
+ // The empty-response guard above only catches a stream that produced
848
+ // NOTHING; a stream truncated after any content passed it and was
849
+ // reported as a clean end_turn. message_delta is the terminal event.
850
+ assertTerminalEventObserved(sawTerminalEvent, 'Bedrock', { modelId, ...request });
851
+
836
852
  // Build response from accumulated data
837
853
  finalMessage = {
838
854
  id: 'msg_stream',
@@ -917,6 +933,14 @@ export class BedrockAdapter implements ProviderAdapter {
917
933
  }
918
934
 
919
935
  private handleError(error: unknown, rawRequest?: unknown): MembraneError {
936
+ // A deadline abort is a timeout and stays one. Collapsing it into a bare
937
+ // abortError() here is what erased the identity before Membrane's
938
+ // caller-signal > timeout > error ladder could read it.
939
+ if (isDeadlineAbort(error)) return deadlineTimeoutError(error, rawRequest);
940
+ // Already-classified failures (e.g. the stream-integrity guards) keep
941
+ // their type and retryability instead of being re-derived from a string.
942
+ if (error instanceof MembraneError) return error;
943
+
920
944
  if (error instanceof BedrockError) {
921
945
  const status = error.status;
922
946
  const message = error.message;
@@ -28,7 +28,7 @@ import {
28
28
  abortError,
29
29
  networkError,
30
30
  } from '../types/index.js';
31
- import { createCombinedSignal } from './utils.js';
31
+ import { createCombinedSignal, isDeadlineAbort, deadlineTimeoutError, throwOnStreamErrorFrame, assertTerminalEventObserved } from './utils.js';
32
32
 
33
33
  // ============================================================================
34
34
  // Gemini API Types
@@ -77,11 +77,58 @@ interface GeminiResponse {
77
77
  candidatesTokenCount?: number;
78
78
  totalTokenCount?: number;
79
79
  cachedContentTokenCount?: number;
80
+ /**
81
+ * Reasoning ("thinking") tokens. DISJOINT from candidatesTokenCount and
82
+ * billed at the output rate, so generated output is candidates + thoughts.
83
+ * Live receipt 2026-08-25, gemini-3.5-flash-lite, thinkingBudget 512:
84
+ * prompt 35, candidates 2, thoughts 228, total 265 — 35+2+228 === 265.
85
+ */
86
+ thoughtsTokenCount?: number;
80
87
  };
81
88
  modelVersion?: string;
82
89
  error?: { code: number; message: string; status: string };
83
90
  }
84
91
 
92
+ /**
93
+ * Map Gemini's `usageMetadata` onto membrane's usage shape.
94
+ *
95
+ * `thoughtsTokenCount` is disjoint from `candidatesTokenCount` and billed at
96
+ * the output rate, so generated output is the SUM of the two; reading only
97
+ * candidates reported a thinking turn at a fraction of its real size. The
98
+ * reconciliation check makes the next such omission loud rather than silent:
99
+ * Google's own total is the independent witness, and a mismatch means a
100
+ * usageMetadata field membrane does not read is carrying tokens.
101
+ */
102
+ function geminiUsageToProviderUsage(
103
+ usageMetadata: GeminiResponse['usageMetadata']
104
+ ): ProviderResponse['usage'] {
105
+ const promptTokens = usageMetadata?.promptTokenCount ?? 0;
106
+ const candidatesTokens = usageMetadata?.candidatesTokenCount ?? 0;
107
+ const thoughtsTokens = usageMetadata?.thoughtsTokenCount;
108
+ const totalTokens = usageMetadata?.totalTokenCount;
109
+
110
+ if (usageMetadata && totalTokens != null) {
111
+ const accountedTokens = promptTokens + candidatesTokens + (thoughtsTokens ?? 0);
112
+ if (accountedTokens !== totalTokens) {
113
+ console.warn(
114
+ `[membrane:gemini] usageMetadata does not reconcile: promptTokenCount(${promptTokens})`
115
+ + ` + candidatesTokenCount(${candidatesTokens}) + thoughtsTokenCount(${thoughtsTokens ?? 0})`
116
+ + ` = ${accountedTokens}, but totalTokenCount = ${totalTokens}.`
117
+ + ' Some billed tokens are in a usageMetadata field membrane does not read.'
118
+ );
119
+ }
120
+ }
121
+
122
+ return {
123
+ inputTokens: promptTokens,
124
+ outputTokens: candidatesTokens + (thoughtsTokens ?? 0),
125
+ ...(thoughtsTokens != null ? { thinkingTokens: thoughtsTokens } : {}),
126
+ cacheReadTokens: usageMetadata?.cachedContentTokenCount
127
+ ? usageMetadata.cachedContentTokenCount
128
+ : undefined,
129
+ };
130
+ }
131
+
85
132
  // ============================================================================
86
133
  // Adapter Configuration
87
134
  // ============================================================================
@@ -103,6 +150,18 @@ export interface GeminiAdapterConfig {
103
150
 
104
151
  export class GeminiAdapter implements ProviderAdapter {
105
152
  readonly name = 'gemini';
153
+
154
+ /**
155
+ * NOT ESTABLISHED. Google documents `cachedContentTokenCount` but the probes
156
+ * available on 2026-08-25 could not produce a cache hit to measure against:
157
+ * three identical 10,893-token calls to gemini-3.5-flash-lite never reported
158
+ * the field (implicit caching did not trigger), and explicit `cachedContents`
159
+ * is refused on the free tier (429,
160
+ * TotalCachedContentStorageTokensPerModelFreeTier limit=0). Declared honestly
161
+ * rather than guessed — membrane passes the counts through and warns once if
162
+ * a cache read ever arrives.
163
+ */
164
+ readonly usageCacheConvention = 'unknown' as const;
106
165
  private apiKey: string;
107
166
  private baseURL: string;
108
167
  private defaultMaxTokens: number;
@@ -188,11 +247,65 @@ export class GeminiAdapter implements ProviderAdapter {
188
247
  const decoder = new TextDecoder();
189
248
  let accumulated = '';
190
249
  let finishReason = 'STOP';
250
+ let sawTerminalEvent = false;
191
251
  let toolCalls: { name: string; args: Record<string, unknown> }[] = [];
192
252
  let images: { data: string; mimeType: string }[] = [];
193
253
  let lastUsage: GeminiResponse['usageMetadata'] | undefined;
254
+ // The resolved model Google actually served, echoed on stream frames.
255
+ // Reporting the requested id instead hides alias/auto-upgrade routing.
256
+ let lastModelVersion: string | undefined;
194
257
  let buffer = '';
195
258
 
259
+ // One frame handler for both the streaming lines and the trailing
260
+ // buffer — the two used to carry byte-identical copies of this logic,
261
+ // so any fix (error frames, terminal observation) had to be made twice.
262
+ const processDataLine = (dataLine: string): void => {
263
+ let parsed: GeminiResponse;
264
+ try {
265
+ parsed = JSON.parse(dataLine) as GeminiResponse;
266
+ } catch {
267
+ return; // Ignore parse errors in stream chunks
268
+ }
269
+
270
+ throwOnStreamErrorFrame(parsed, 'Gemini', geminiRequest);
271
+
272
+ const candidate = parsed.candidates?.[0];
273
+
274
+ if (candidate?.content?.parts) {
275
+ for (const part of candidate.content.parts) {
276
+ if (part.text) {
277
+ accumulated += part.text;
278
+ callbacks.onChunk(part.text);
279
+ }
280
+ if (part.inlineData) {
281
+ images.push({
282
+ data: part.inlineData.data,
283
+ mimeType: part.inlineData.mimeType,
284
+ });
285
+ }
286
+ if (part.functionCall) {
287
+ toolCalls.push({
288
+ name: part.functionCall.name,
289
+ args: part.functionCall.args,
290
+ });
291
+ }
292
+ }
293
+ }
294
+
295
+ if (candidate?.finishReason) {
296
+ finishReason = candidate.finishReason;
297
+ sawTerminalEvent = true;
298
+ }
299
+
300
+ if (parsed.usageMetadata) {
301
+ lastUsage = parsed.usageMetadata;
302
+ }
303
+
304
+ if (parsed.modelVersion) {
305
+ lastModelVersion = parsed.modelVersion;
306
+ }
307
+ };
308
+
196
309
  while (true) {
197
310
  const { done, value } = await reader.read();
198
311
  if (done) break;
@@ -206,42 +319,7 @@ export class GeminiAdapter implements ProviderAdapter {
206
319
  if (!line.startsWith('data: ')) continue;
207
320
  const data = line.slice(6).trim();
208
321
  if (!data || data === '[DONE]') continue;
209
-
210
- try {
211
- const parsed = JSON.parse(data) as GeminiResponse;
212
- const candidate = parsed.candidates?.[0];
213
-
214
- if (candidate?.content?.parts) {
215
- for (const part of candidate.content.parts) {
216
- if (part.text) {
217
- accumulated += part.text;
218
- callbacks.onChunk(part.text);
219
- }
220
- if (part.inlineData) {
221
- images.push({
222
- data: part.inlineData.data,
223
- mimeType: part.inlineData.mimeType,
224
- });
225
- }
226
- if (part.functionCall) {
227
- toolCalls.push({
228
- name: part.functionCall.name,
229
- args: part.functionCall.args,
230
- });
231
- }
232
- }
233
- }
234
-
235
- if (candidate?.finishReason) {
236
- finishReason = candidate.finishReason;
237
- }
238
-
239
- if (parsed.usageMetadata) {
240
- lastUsage = parsed.usageMetadata;
241
- }
242
- } catch {
243
- // Ignore parse errors in stream chunks
244
- }
322
+ processDataLine(data);
245
323
  }
246
324
  }
247
325
 
@@ -250,56 +328,18 @@ export class GeminiAdapter implements ProviderAdapter {
250
328
  const remaining = buffer.trim();
251
329
  const dataLine = remaining.startsWith('data: ') ? remaining.slice(6).trim() : remaining;
252
330
  if (dataLine && dataLine !== '[DONE]') {
253
- try {
254
- const parsed = JSON.parse(dataLine) as GeminiResponse;
255
- const candidate = parsed.candidates?.[0];
256
-
257
- if (candidate?.content?.parts) {
258
- for (const part of candidate.content.parts) {
259
- if (part.text) {
260
- accumulated += part.text;
261
- callbacks.onChunk(part.text);
262
- }
263
- if (part.inlineData) {
264
- images.push({
265
- data: part.inlineData.data,
266
- mimeType: part.inlineData.mimeType,
267
- });
268
- }
269
- if (part.functionCall) {
270
- toolCalls.push({
271
- name: part.functionCall.name,
272
- args: part.functionCall.args,
273
- });
274
- }
275
- }
276
- }
277
-
278
- if (candidate?.finishReason) {
279
- finishReason = candidate.finishReason;
280
- }
281
-
282
- if (parsed.usageMetadata) {
283
- lastUsage = parsed.usageMetadata;
284
- }
285
- } catch {
286
- // Final buffer wasn't valid JSON — nothing to do
287
- }
331
+ processDataLine(dataLine);
288
332
  }
289
333
  }
290
334
 
335
+ assertTerminalEventObserved(sawTerminalEvent, 'Gemini', geminiRequest);
336
+
291
337
  return {
292
338
  content: this.buildContentBlocks(accumulated, toolCalls, images),
293
339
  stopReason: this.mapFinishReason(finishReason),
294
340
  stopSequence: undefined,
295
- usage: {
296
- inputTokens: lastUsage?.promptTokenCount ?? 0,
297
- outputTokens: lastUsage?.candidatesTokenCount ?? 0,
298
- cacheReadTokens: lastUsage?.cachedContentTokenCount
299
- ? lastUsage.cachedContentTokenCount
300
- : undefined,
301
- },
302
- model: request.model,
341
+ usage: geminiUsageToProviderUsage(lastUsage),
342
+ model: lastModelVersion ?? request.model,
303
343
  rawRequest: geminiRequest,
304
344
  raw: { finishReason, usage: lastUsage },
305
345
  };
@@ -563,13 +603,7 @@ export class GeminiAdapter implements ProviderAdapter {
563
603
  content: this.buildContentBlocks(text, toolCalls, images),
564
604
  stopReason: this.mapFinishReason(candidate?.finishReason),
565
605
  stopSequence: undefined,
566
- usage: {
567
- inputTokens: response.usageMetadata?.promptTokenCount ?? 0,
568
- outputTokens: response.usageMetadata?.candidatesTokenCount ?? 0,
569
- cacheReadTokens: response.usageMetadata?.cachedContentTokenCount
570
- ? response.usageMetadata.cachedContentTokenCount
571
- : undefined,
572
- },
606
+ usage: geminiUsageToProviderUsage(response.usageMetadata),
573
607
  model: response.modelVersion ?? requestedModel,
574
608
  rawRequest,
575
609
  raw: response,
@@ -630,6 +664,10 @@ export class GeminiAdapter implements ProviderAdapter {
630
664
  // --------------------------------------------------------------------------
631
665
 
632
666
  private handleError(error: unknown, rawRequest?: unknown): MembraneError {
667
+ // A deadline abort is a timeout and stays one. Collapsing it into a bare
668
+ // abortError() here is what erased the identity before Membrane's
669
+ // caller-signal > timeout > error ladder could read it.
670
+ if (isDeadlineAbort(error)) return deadlineTimeoutError(error, rawRequest);
633
671
  if (error instanceof MembraneError) return error;
634
672
 
635
673
  if (error instanceof Error) {
@@ -52,6 +52,9 @@ const DEFAULT_CONFIG: Required<Omit<MockAdapterConfig, 'responseGenerator'>> = {
52
52
  export class MockAdapter implements ProviderAdapter {
53
53
  readonly name = 'mock';
54
54
 
55
+ /** Test double; it reports no cache tokens, so the convention never bites. */
56
+ readonly usageCacheConvention = 'cache-excluded' as const;
57
+
55
58
  private config: Required<Omit<MockAdapterConfig, 'responseGenerator'>> & Pick<MockAdapterConfig, 'responseGenerator'>;
56
59
  private responseQueue: string[];
57
60
  private requestLog: Array<{ timestamp: number; request: ProviderRequest }> = [];