@goodtiming/baton-sdk 0.6.2 → 0.6.4

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.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { z } from 'zod';
2
2
 
3
- declare const SDK_VERSION = "ts-0.6.2";
3
+ declare const SDK_VERSION = "ts-0.6.4";
4
4
 
5
5
  /**
6
6
  * Zod schemas + TS types for the Baton event stream per SPEC §11.4.
@@ -2232,8 +2232,8 @@ declare function identityScrub(value: unknown): unknown;
2232
2232
  * for the same reason as Python: MCP stdio transport reserves stdout for
2233
2233
  * JSON-RPC framing).
2234
2234
  * - `FileSink` — JSONL appended to a file.
2235
- * - `HttpSink` — POST to any HTTP endpoint with bearer auth; bounded
2236
- * buffer, retry with backoff, circuit breaker. This is the contract a
2235
+ * - `HttpSink` — POST batches to any HTTP endpoint with bearer auth;
2236
+ * bounded buffer, retry with backoff, circuit breaker. This is the contract a
2237
2237
  * Console or any compatible collector consumes.
2238
2238
  * - `MultiSink` — every event to each of several sinks.
2239
2239
  */
@@ -2246,9 +2246,14 @@ interface Sink {
2246
2246
  /** Wait for any buffered events to reach their destination (or be
2247
2247
  * dropped per the sink's policy). No-op for synchronous sinks. */
2248
2248
  flush(): Promise<void>;
2249
- /** Flush + release resources. After `aclose`, `write` rejects. */
2249
+ /** Flush + release resources. After `aclose`, `write` rejects with
2250
+ * `SinkClosedError`. */
2250
2251
  aclose(): Promise<void>;
2251
2252
  }
2253
+ /** `write` was called after `aclose`. */
2254
+ declare class SinkClosedError extends Error {
2255
+ name: string;
2256
+ }
2252
2257
  /** Fail-open wrapper around `sink.write`.
2253
2258
  *
2254
2259
  * The MCP tool wrapper sits BEFORE the vendor's tool handler runs. A reject
@@ -2308,14 +2313,20 @@ interface HttpSinkOptions {
2308
2313
  /** How long the flush at process exit, or an explicit `flush()` or
2309
2314
  * `aclose()`, may take in total, in ms, at most 2^31 - 1. Default 3000. */
2310
2315
  shutdownFlushTimeoutMs?: number;
2316
+ /** The longest an event waits before its batch is sent, in ms. Default 5000. */
2317
+ flushIntervalMs?: number;
2311
2318
  /** Injectable for tests; defaults to the global `fetch`. */
2312
2319
  fetchImpl?: typeof fetch;
2313
2320
  }
2314
- /** POST events to an HTTP endpoint. Bounded buffer + retry + circuit breaker.
2321
+ /** POST events to an HTTP endpoint in batches. Bounded buffer + retry +
2322
+ * circuit breaker.
2315
2323
  *
2316
2324
  * This is the contract a Console or any compatible collector consumes:
2317
- * `POST {url}/v0/events` with `Authorization: Bearer {apiKey}` and a JSON
2318
- * body matching the SPEC §11.4 envelope.
2325
+ * `POST {url}/v0/events/batch` (SPEC §2.5) with `Authorization: Bearer
2326
+ * {apiKey}` and `{"events": [...]}` of SPEC §11.4 envelopes. A batch goes when
2327
+ * 100 events wait, when the next would take the body over 1 MiB, or
2328
+ * `flushIntervalMs` after the oldest waiting one was written. A collector
2329
+ * with no batch endpoint gets one event per `POST {url}/v0/events`.
2319
2330
  *
2320
2331
  * Failures are bounded — buffer overflow drops the oldest event; network
2321
2332
  * failures retry with backoff; the circuit breaker opens after consecutive
@@ -2323,12 +2334,17 @@ interface HttpSinkOptions {
2323
2334
  declare class HttpSink implements Sink {
2324
2335
  private readonly url;
2325
2336
  private readonly apiKey;
2337
+ /** Waiting events only: a batch being sent is out of it, so the bound
2338
+ * never pushes out an event in flight (SPEC §11.2 rule 4). */
2326
2339
  private readonly buffer;
2340
+ private bufferedBytes;
2341
+ private sendingEvents;
2327
2342
  private readonly bufferSize;
2328
2343
  private readonly requestTimeoutMs;
2329
2344
  private readonly shutdownFlushTimeoutMs;
2330
- /** Set when an exit pass ends; cleared once a normal drain empties the
2331
- * buffer, which means the process went on. */
2345
+ private readonly flushIntervalMs;
2346
+ /** Set when an exit pass ends; cleared once a normal drain sends
2347
+ * everything, which means the process went on. */
2332
2348
  private exitPassRan;
2333
2349
  /** Whether the exit pass warned about every event now buffered. */
2334
2350
  private unsentWarned;
@@ -2338,7 +2354,8 @@ declare class HttpSink implements Sink {
2338
2354
  private readonly circuit;
2339
2355
  private readonly fetchImpl;
2340
2356
  private overflowWarned;
2341
- /** Per status, so a new kind of refusal, such as a 401 after a 422, still warns. */
2357
+ /** Per status or refusal code, so a new kind of refusal, such as a 401
2358
+ * after a 422, still warns. */
2342
2359
  private readonly refusals;
2343
2360
  /** Aborted once `aclose()` returns: nothing Baton started may keep the
2344
2361
  * process alive after the vendor closed it. */
@@ -2348,6 +2365,16 @@ declare class HttpSink implements Sink {
2348
2365
  private skipWarnedFor;
2349
2366
  private closeOnce;
2350
2367
  private drainChain;
2368
+ private queuedDrain;
2369
+ /** Whether the queued pass sends everything waiting, not only what is due. */
2370
+ private forceNext;
2371
+ private flushTimer;
2372
+ /** Set by a failed batch: only the timer, a flush or a close tries again. */
2373
+ private waitForTimer;
2374
+ /** Nothing is sent before this `Date.now()`, from a 429's `Retry-After`. */
2375
+ private retryAt;
2376
+ /** Set for the life of the sink by a 404 or 405 on the batch path. */
2377
+ private batchless;
2351
2378
  private exitHooks;
2352
2379
  /** The least time between two of this sink's loss reports. */
2353
2380
  private static reportIntervalMs;
@@ -2355,7 +2382,9 @@ declare class HttpSink implements Sink {
2355
2382
  private reportedSum;
2356
2383
  private lastReportAt;
2357
2384
  private reportsStopped;
2358
- private reportInFlight;
2385
+ /** The envelope of a report due to ride the next batch, and when it was set up. */
2386
+ private pendingReport;
2387
+ private reportSetAt;
2359
2388
  constructor(url: string, options: HttpSinkOptions);
2360
2389
  /**
2361
2390
  * What this sink shows when something prints it — never the bearer.
@@ -2370,6 +2399,9 @@ declare class HttpSink implements Sink {
2370
2399
  */
2371
2400
  toJSON(): Record<string, unknown>;
2372
2401
  private get closed();
2402
+ private get unsent();
2403
+ /** Encodes at once, not when the batch is cut, because the byte cap
2404
+ * decides when a batch goes. */
2373
2405
  write(event: Event): Promise<void>;
2374
2406
  flush(): Promise<void>;
2375
2407
  /** A second call waits for the first (SPEC §11.2 rule 7). */
@@ -2383,54 +2415,87 @@ declare class HttpSink implements Sink {
2383
2415
  * drain is not cancelled here, so after `flush()` it goes on sending;
2384
2416
  * `aclose()` stops it. */
2385
2417
  private drainWithinBudget;
2386
- /** A full buffer may already have pushed `event` out while it was being sent. */
2387
- private dequeue;
2388
2418
  private enqueue;
2389
- /** Serializes every call to `drainLocked` — from `write()`'s background
2390
- * drain and from explicit `flush()` alike — onto one promise chain, so
2391
- * concurrent tool calls (the normal case once the MCP interceptor is
2392
- * wired up) can never run two drains at once racing on `buffer[0]`/
2393
- * `shift()`. Mirrors Python's `asyncio.Lock` around `_drain_locked`. */
2419
+ private dropOverflow;
2420
+ private batchIsFull;
2421
+ /** The timer must not keep the vendor's process alive: the exit hooks send
2422
+ * what is left. */
2423
+ private armTimer;
2424
+ private clearTimer;
2425
+ /** Runs `drainLocked` after the pass under way, one pass at a time, so two
2426
+ * never send at once or race on the buffer. A pass not yet started is
2427
+ * shared by every caller: one per write would grow without bound while a
2428
+ * slow collector holds a pass. */
2394
2429
  private scheduleDrain;
2395
2430
  /** Best-effort flush when the process is about to exit without an
2396
2431
  * explicit `aclose()`. SIGKILL, or SIGTERM with no installed handler, skip
2397
2432
  * both hooks: a vendor needing that guarantee must call `aclose()` from
2398
2433
  * their own handler. */
2399
2434
  private ensureExitHooksRegistered;
2400
- /** One attempt per event, no retry, within `shutdownFlushTimeoutMs` in
2435
+ /** One attempt per batch, no retry, within `shutdownFlushTimeoutMs` in
2401
2436
  * total (SPEC §11.2); what is left is reported, not sent. */
2402
2437
  private exitPass;
2403
2438
  private unsentAtExit;
2404
- private events_url;
2405
2439
  private authHeaders;
2406
2440
  private static classifyStatus;
2407
- /** A refused event is otherwise dropped silently, which looks the same from
2408
- * the vendor's process as an accepted one. The collector's own message is
2409
- * logged because it names the field at fault; it is capped and escaped
2441
+ /** Counts `n` refused events and logs them, at most once a minute per
2442
+ * kind. A refused event is otherwise dropped silently, which looks the same
2443
+ * from the vendor's process as an accepted one. The collector's own message
2444
+ * is logged because it names the field at fault; it is capped and escaped
2410
2445
  * because it is a remote server's text landing in the vendor's log. */
2411
- private static logRefusal;
2446
+ private refuse;
2412
2447
  private warnUnloggedRefusals;
2413
- /** How many refusals with `status` went unlogged before this one, or `null`
2414
- * when this one goes unlogged too. */
2448
+ /** How many refusals of `kind` went unlogged before these `n`, or `null`
2449
+ * when these go unlogged too. */
2415
2450
  private refusalToLog;
2416
2451
  /** Enough of the body for `REFUSAL_BODY_MAX_LEN` code points; the rest is
2417
2452
  * cancelled unread, so a large error page does not hold the drain. */
2418
2453
  private static readHead;
2454
+ /** Sends what is due, one batch at a time; `force` (a flush or close) sends
2455
+ * everything waiting. A report set up before the pass rides along; one set
2456
+ * up by this pass's own delivery waits for the next batch. */
2419
2457
  private drainLocked;
2420
- /** Starts a report of this process's loss totals (SPEC §11.4.7) beside the
2421
- * drain and outside the buffer, so it never delays an event and is never
2422
- * counted as one. */
2458
+ /** A due report makes the next batch due only when it can ride it; the
2459
+ * timer is armed the same way, so a pass that sends nothing never re-arms
2460
+ * it at a time already past. */
2461
+ private isDue;
2462
+ /** A pending report can go in the next batch: alone, or with the head event's tenant and vendor. */
2463
+ private reportRidesHead;
2464
+ /** Takes the head of the buffer that fits one batch: one tenant and vendor
2465
+ * (SPEC §2.5), at most 100 events and 1 MiB. A due report with the head
2466
+ * event's tenant and vendor takes one of those places; otherwise it is not
2467
+ * built, and waits for a batch it can ride. Without a batch endpoint the same events go one
2468
+ * per request. */
2469
+ private takeBatch;
2470
+ /** Puts a kept batch's events back at the head of the buffer; `true` when
2471
+ * there were none. */
2472
+ private putBack;
2473
+ /** After a delivery, sets up a report of this process's loss totals (SPEC
2474
+ * §11.4.7) to ride the next batch. It stays outside the buffer, so it never
2475
+ * pushes an event out and is never counted as one. */
2423
2476
  private maybeReport;
2424
2477
  private stopReports;
2425
- private sendReport;
2426
- private sendWithRetry;
2478
+ private buildReport;
2479
+ /** Sends one batch, splitting it on 413, until every event in it is
2480
+ * delivered or refused. Returns what a failure kept, to go back in the
2481
+ * buffer. */
2482
+ private sendBatch;
2483
+ private sendEachAlone;
2484
+ /** `alone` is one event per request (SPEC §2.5), so a whole refusal there is that event's own. */
2485
+ private settle;
2427
2486
  /** `null`, with a warning, for an event `JSON.stringify` rejects. */
2428
2487
  private encode;
2429
2488
  private sleepUnlessClosed;
2430
2489
  /** Aborted by the timeout or by `aclose()`. Built by hand: `AbortSignal.any`
2431
2490
  * is missing before Node 20.3, and on some later versions it keeps every
2432
- * combined signal alive as long as the long-lived `closing` signal. */
2433
- private postOnce;
2491
+ * combined signal alive as long as the long-lived `closing` signal. Any body
2492
+ * read is under the same signal, so a stalled body cannot hold the drain. */
2493
+ private post;
2494
+ /** Every answer but a 2xx and a refusal of the whole batch. */
2495
+ private static otherAnswer;
2496
+ /** The refused results by `event_id` (SPEC §2.5). An event with no readable
2497
+ * result was delivered. */
2498
+ private static refusedIn;
2434
2499
  }
2435
2500
 
2436
2501
  /** Principal identity — what a vendor's resolver hands back, and how it goes
@@ -3083,4 +3148,4 @@ interface IntentParam {
3083
3148
  }
3084
3149
  declare const INTENT_PARAMS: readonly IntentParam[];
3085
3150
 
3086
- export { type AnnotateOptions, type AnnotationEvent, AnnotationEventSchema, type AnnotationPayload, AnnotationPayloadSchema, type AuthInfo, type Baton, type BatonConfig, BatonHandle, Client, type ClientOptions, type ClientReportEvent, ClientReportEventSchema, type ClientReportPayload, ClientReportPayloadSchema, DEFAULT_CONSENT_TOKEN, DEPTH_LIMIT, DEPTH_MARKER, type Event, EventSchema, type EventType, EventTypeSchema, FileSink, type HeaderInput, HttpSink, type HttpSinkOptions, INTENT_INSTRUCTIONS, INTENT_PARAMS, type IntentParam, type ListToolsOptions, MultiSink, type ObservedError, type Organization, PRINCIPAL_FORM_HASHED, PRINCIPAL_FORM_RAW, PRINCIPAL_SOURCE_ASSERTED, type Principal, type PrincipalForm, type PrincipalResolutionContext, PrincipalWireSchema, type PromptGetEndEvent, PromptGetEndEventSchema, type PromptGetEndPayload, PromptGetEndPayloadSchema, type PromptGetErrorEvent, PromptGetErrorEventSchema, type PromptGetErrorPayload, PromptGetErrorPayloadSchema, type PromptGetStartEvent, PromptGetStartEventSchema, type PromptGetStartPayload, PromptGetStartPayloadSchema, type PromptListEndEvent, PromptListEndEventSchema, type PromptListEndPayload, PromptListEndPayloadSchema, type PromptListErrorEvent, PromptListErrorEventSchema, type PromptListErrorPayload, PromptListErrorPayloadSchema, type PromptListStartEvent, PromptListStartEventSchema, type PromptListStartPayload, PromptListStartPayloadSchema, type ResolvePrincipalHook, type ResourceListEndEvent, ResourceListEndEventSchema, type ResourceListEndPayload, ResourceListEndPayloadSchema, type ResourceListErrorEvent, ResourceListErrorEventSchema, type ResourceListErrorPayload, ResourceListErrorPayloadSchema, type ResourceListStartEvent, ResourceListStartEventSchema, type ResourceListStartPayload, ResourceListStartPayloadSchema, type ResourceReadEndEvent, ResourceReadEndEventSchema, type ResourceReadEndPayload, ResourceReadEndPayloadSchema, type ResourceReadErrorEvent, ResourceReadErrorEventSchema, type ResourceReadErrorPayload, ResourceReadErrorPayloadSchema, type ResourceReadStartEvent, ResourceReadStartEventSchema, type ResourceReadStartPayload, ResourceReadStartPayloadSchema, type ResultCaptureMode, SDK_VERSION, Scrubber, type SessionOptions, type Sink, StdoutSink, type StdoutSinkOptions, type SupportedMcpServer, type SurfaceSnapshotEvent, SurfaceSnapshotEventSchema, type SurfaceSnapshotPayload, SurfaceSnapshotPayloadSchema, type ToolCallEndEvent, ToolCallEndEventSchema, type ToolCallEndPayload, ToolCallEndPayloadSchema, type ToolCallErrorEvent, ToolCallErrorEventSchema, type ToolCallErrorPayload, ToolCallErrorPayloadSchema, type ToolCallStartEvent, ToolCallStartEventSchema, type ToolCallStartPayload, ToolCallStartPayloadSchema, type ToolListEndEvent, ToolListEndEventSchema, type ToolListEndPayload, ToolListEndPayloadSchema, type ToolListErrorEvent, ToolListErrorEventSchema, type ToolListErrorPayload, ToolListErrorPayloadSchema, type ToolListStartEvent, ToolListStartEventSchema, type ToolListStartPayload, ToolListStartPayloadSchema, Trace, type TraceOptions, createBaton, identityScrub, principalFromOAuthEmail, principalFromOAuthSub, safeWrite, withBaton };
3151
+ export { type AnnotateOptions, type AnnotationEvent, AnnotationEventSchema, type AnnotationPayload, AnnotationPayloadSchema, type AuthInfo, type Baton, type BatonConfig, BatonHandle, Client, type ClientOptions, type ClientReportEvent, ClientReportEventSchema, type ClientReportPayload, ClientReportPayloadSchema, DEFAULT_CONSENT_TOKEN, DEPTH_LIMIT, DEPTH_MARKER, type Event, EventSchema, type EventType, EventTypeSchema, FileSink, type HeaderInput, HttpSink, type HttpSinkOptions, INTENT_INSTRUCTIONS, INTENT_PARAMS, type IntentParam, type ListToolsOptions, MultiSink, type ObservedError, type Organization, PRINCIPAL_FORM_HASHED, PRINCIPAL_FORM_RAW, PRINCIPAL_SOURCE_ASSERTED, type Principal, type PrincipalForm, type PrincipalResolutionContext, PrincipalWireSchema, type PromptGetEndEvent, PromptGetEndEventSchema, type PromptGetEndPayload, PromptGetEndPayloadSchema, type PromptGetErrorEvent, PromptGetErrorEventSchema, type PromptGetErrorPayload, PromptGetErrorPayloadSchema, type PromptGetStartEvent, PromptGetStartEventSchema, type PromptGetStartPayload, PromptGetStartPayloadSchema, type PromptListEndEvent, PromptListEndEventSchema, type PromptListEndPayload, PromptListEndPayloadSchema, type PromptListErrorEvent, PromptListErrorEventSchema, type PromptListErrorPayload, PromptListErrorPayloadSchema, type PromptListStartEvent, PromptListStartEventSchema, type PromptListStartPayload, PromptListStartPayloadSchema, type ResolvePrincipalHook, type ResourceListEndEvent, ResourceListEndEventSchema, type ResourceListEndPayload, ResourceListEndPayloadSchema, type ResourceListErrorEvent, ResourceListErrorEventSchema, type ResourceListErrorPayload, ResourceListErrorPayloadSchema, type ResourceListStartEvent, ResourceListStartEventSchema, type ResourceListStartPayload, ResourceListStartPayloadSchema, type ResourceReadEndEvent, ResourceReadEndEventSchema, type ResourceReadEndPayload, ResourceReadEndPayloadSchema, type ResourceReadErrorEvent, ResourceReadErrorEventSchema, type ResourceReadErrorPayload, ResourceReadErrorPayloadSchema, type ResourceReadStartEvent, ResourceReadStartEventSchema, type ResourceReadStartPayload, ResourceReadStartPayloadSchema, type ResultCaptureMode, SDK_VERSION, Scrubber, type SessionOptions, type Sink, SinkClosedError, StdoutSink, type StdoutSinkOptions, type SupportedMcpServer, type SurfaceSnapshotEvent, SurfaceSnapshotEventSchema, type SurfaceSnapshotPayload, SurfaceSnapshotPayloadSchema, type ToolCallEndEvent, ToolCallEndEventSchema, type ToolCallEndPayload, ToolCallEndPayloadSchema, type ToolCallErrorEvent, ToolCallErrorEventSchema, type ToolCallErrorPayload, ToolCallErrorPayloadSchema, type ToolCallStartEvent, ToolCallStartEventSchema, type ToolCallStartPayload, ToolCallStartPayloadSchema, type ToolListEndEvent, ToolListEndEventSchema, type ToolListEndPayload, ToolListEndPayloadSchema, type ToolListErrorEvent, ToolListErrorEventSchema, type ToolListErrorPayload, ToolListErrorPayloadSchema, type ToolListStartEvent, ToolListStartEventSchema, type ToolListStartPayload, ToolListStartPayloadSchema, Trace, type TraceOptions, createBaton, identityScrub, principalFromOAuthEmail, principalFromOAuthSub, safeWrite, withBaton };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { z } from 'zod';
2
2
 
3
- declare const SDK_VERSION = "ts-0.6.2";
3
+ declare const SDK_VERSION = "ts-0.6.4";
4
4
 
5
5
  /**
6
6
  * Zod schemas + TS types for the Baton event stream per SPEC §11.4.
@@ -2232,8 +2232,8 @@ declare function identityScrub(value: unknown): unknown;
2232
2232
  * for the same reason as Python: MCP stdio transport reserves stdout for
2233
2233
  * JSON-RPC framing).
2234
2234
  * - `FileSink` — JSONL appended to a file.
2235
- * - `HttpSink` — POST to any HTTP endpoint with bearer auth; bounded
2236
- * buffer, retry with backoff, circuit breaker. This is the contract a
2235
+ * - `HttpSink` — POST batches to any HTTP endpoint with bearer auth;
2236
+ * bounded buffer, retry with backoff, circuit breaker. This is the contract a
2237
2237
  * Console or any compatible collector consumes.
2238
2238
  * - `MultiSink` — every event to each of several sinks.
2239
2239
  */
@@ -2246,9 +2246,14 @@ interface Sink {
2246
2246
  /** Wait for any buffered events to reach their destination (or be
2247
2247
  * dropped per the sink's policy). No-op for synchronous sinks. */
2248
2248
  flush(): Promise<void>;
2249
- /** Flush + release resources. After `aclose`, `write` rejects. */
2249
+ /** Flush + release resources. After `aclose`, `write` rejects with
2250
+ * `SinkClosedError`. */
2250
2251
  aclose(): Promise<void>;
2251
2252
  }
2253
+ /** `write` was called after `aclose`. */
2254
+ declare class SinkClosedError extends Error {
2255
+ name: string;
2256
+ }
2252
2257
  /** Fail-open wrapper around `sink.write`.
2253
2258
  *
2254
2259
  * The MCP tool wrapper sits BEFORE the vendor's tool handler runs. A reject
@@ -2308,14 +2313,20 @@ interface HttpSinkOptions {
2308
2313
  /** How long the flush at process exit, or an explicit `flush()` or
2309
2314
  * `aclose()`, may take in total, in ms, at most 2^31 - 1. Default 3000. */
2310
2315
  shutdownFlushTimeoutMs?: number;
2316
+ /** The longest an event waits before its batch is sent, in ms. Default 5000. */
2317
+ flushIntervalMs?: number;
2311
2318
  /** Injectable for tests; defaults to the global `fetch`. */
2312
2319
  fetchImpl?: typeof fetch;
2313
2320
  }
2314
- /** POST events to an HTTP endpoint. Bounded buffer + retry + circuit breaker.
2321
+ /** POST events to an HTTP endpoint in batches. Bounded buffer + retry +
2322
+ * circuit breaker.
2315
2323
  *
2316
2324
  * This is the contract a Console or any compatible collector consumes:
2317
- * `POST {url}/v0/events` with `Authorization: Bearer {apiKey}` and a JSON
2318
- * body matching the SPEC §11.4 envelope.
2325
+ * `POST {url}/v0/events/batch` (SPEC §2.5) with `Authorization: Bearer
2326
+ * {apiKey}` and `{"events": [...]}` of SPEC §11.4 envelopes. A batch goes when
2327
+ * 100 events wait, when the next would take the body over 1 MiB, or
2328
+ * `flushIntervalMs` after the oldest waiting one was written. A collector
2329
+ * with no batch endpoint gets one event per `POST {url}/v0/events`.
2319
2330
  *
2320
2331
  * Failures are bounded — buffer overflow drops the oldest event; network
2321
2332
  * failures retry with backoff; the circuit breaker opens after consecutive
@@ -2323,12 +2334,17 @@ interface HttpSinkOptions {
2323
2334
  declare class HttpSink implements Sink {
2324
2335
  private readonly url;
2325
2336
  private readonly apiKey;
2337
+ /** Waiting events only: a batch being sent is out of it, so the bound
2338
+ * never pushes out an event in flight (SPEC §11.2 rule 4). */
2326
2339
  private readonly buffer;
2340
+ private bufferedBytes;
2341
+ private sendingEvents;
2327
2342
  private readonly bufferSize;
2328
2343
  private readonly requestTimeoutMs;
2329
2344
  private readonly shutdownFlushTimeoutMs;
2330
- /** Set when an exit pass ends; cleared once a normal drain empties the
2331
- * buffer, which means the process went on. */
2345
+ private readonly flushIntervalMs;
2346
+ /** Set when an exit pass ends; cleared once a normal drain sends
2347
+ * everything, which means the process went on. */
2332
2348
  private exitPassRan;
2333
2349
  /** Whether the exit pass warned about every event now buffered. */
2334
2350
  private unsentWarned;
@@ -2338,7 +2354,8 @@ declare class HttpSink implements Sink {
2338
2354
  private readonly circuit;
2339
2355
  private readonly fetchImpl;
2340
2356
  private overflowWarned;
2341
- /** Per status, so a new kind of refusal, such as a 401 after a 422, still warns. */
2357
+ /** Per status or refusal code, so a new kind of refusal, such as a 401
2358
+ * after a 422, still warns. */
2342
2359
  private readonly refusals;
2343
2360
  /** Aborted once `aclose()` returns: nothing Baton started may keep the
2344
2361
  * process alive after the vendor closed it. */
@@ -2348,6 +2365,16 @@ declare class HttpSink implements Sink {
2348
2365
  private skipWarnedFor;
2349
2366
  private closeOnce;
2350
2367
  private drainChain;
2368
+ private queuedDrain;
2369
+ /** Whether the queued pass sends everything waiting, not only what is due. */
2370
+ private forceNext;
2371
+ private flushTimer;
2372
+ /** Set by a failed batch: only the timer, a flush or a close tries again. */
2373
+ private waitForTimer;
2374
+ /** Nothing is sent before this `Date.now()`, from a 429's `Retry-After`. */
2375
+ private retryAt;
2376
+ /** Set for the life of the sink by a 404 or 405 on the batch path. */
2377
+ private batchless;
2351
2378
  private exitHooks;
2352
2379
  /** The least time between two of this sink's loss reports. */
2353
2380
  private static reportIntervalMs;
@@ -2355,7 +2382,9 @@ declare class HttpSink implements Sink {
2355
2382
  private reportedSum;
2356
2383
  private lastReportAt;
2357
2384
  private reportsStopped;
2358
- private reportInFlight;
2385
+ /** The envelope of a report due to ride the next batch, and when it was set up. */
2386
+ private pendingReport;
2387
+ private reportSetAt;
2359
2388
  constructor(url: string, options: HttpSinkOptions);
2360
2389
  /**
2361
2390
  * What this sink shows when something prints it — never the bearer.
@@ -2370,6 +2399,9 @@ declare class HttpSink implements Sink {
2370
2399
  */
2371
2400
  toJSON(): Record<string, unknown>;
2372
2401
  private get closed();
2402
+ private get unsent();
2403
+ /** Encodes at once, not when the batch is cut, because the byte cap
2404
+ * decides when a batch goes. */
2373
2405
  write(event: Event): Promise<void>;
2374
2406
  flush(): Promise<void>;
2375
2407
  /** A second call waits for the first (SPEC §11.2 rule 7). */
@@ -2383,54 +2415,87 @@ declare class HttpSink implements Sink {
2383
2415
  * drain is not cancelled here, so after `flush()` it goes on sending;
2384
2416
  * `aclose()` stops it. */
2385
2417
  private drainWithinBudget;
2386
- /** A full buffer may already have pushed `event` out while it was being sent. */
2387
- private dequeue;
2388
2418
  private enqueue;
2389
- /** Serializes every call to `drainLocked` — from `write()`'s background
2390
- * drain and from explicit `flush()` alike — onto one promise chain, so
2391
- * concurrent tool calls (the normal case once the MCP interceptor is
2392
- * wired up) can never run two drains at once racing on `buffer[0]`/
2393
- * `shift()`. Mirrors Python's `asyncio.Lock` around `_drain_locked`. */
2419
+ private dropOverflow;
2420
+ private batchIsFull;
2421
+ /** The timer must not keep the vendor's process alive: the exit hooks send
2422
+ * what is left. */
2423
+ private armTimer;
2424
+ private clearTimer;
2425
+ /** Runs `drainLocked` after the pass under way, one pass at a time, so two
2426
+ * never send at once or race on the buffer. A pass not yet started is
2427
+ * shared by every caller: one per write would grow without bound while a
2428
+ * slow collector holds a pass. */
2394
2429
  private scheduleDrain;
2395
2430
  /** Best-effort flush when the process is about to exit without an
2396
2431
  * explicit `aclose()`. SIGKILL, or SIGTERM with no installed handler, skip
2397
2432
  * both hooks: a vendor needing that guarantee must call `aclose()` from
2398
2433
  * their own handler. */
2399
2434
  private ensureExitHooksRegistered;
2400
- /** One attempt per event, no retry, within `shutdownFlushTimeoutMs` in
2435
+ /** One attempt per batch, no retry, within `shutdownFlushTimeoutMs` in
2401
2436
  * total (SPEC §11.2); what is left is reported, not sent. */
2402
2437
  private exitPass;
2403
2438
  private unsentAtExit;
2404
- private events_url;
2405
2439
  private authHeaders;
2406
2440
  private static classifyStatus;
2407
- /** A refused event is otherwise dropped silently, which looks the same from
2408
- * the vendor's process as an accepted one. The collector's own message is
2409
- * logged because it names the field at fault; it is capped and escaped
2441
+ /** Counts `n` refused events and logs them, at most once a minute per
2442
+ * kind. A refused event is otherwise dropped silently, which looks the same
2443
+ * from the vendor's process as an accepted one. The collector's own message
2444
+ * is logged because it names the field at fault; it is capped and escaped
2410
2445
  * because it is a remote server's text landing in the vendor's log. */
2411
- private static logRefusal;
2446
+ private refuse;
2412
2447
  private warnUnloggedRefusals;
2413
- /** How many refusals with `status` went unlogged before this one, or `null`
2414
- * when this one goes unlogged too. */
2448
+ /** How many refusals of `kind` went unlogged before these `n`, or `null`
2449
+ * when these go unlogged too. */
2415
2450
  private refusalToLog;
2416
2451
  /** Enough of the body for `REFUSAL_BODY_MAX_LEN` code points; the rest is
2417
2452
  * cancelled unread, so a large error page does not hold the drain. */
2418
2453
  private static readHead;
2454
+ /** Sends what is due, one batch at a time; `force` (a flush or close) sends
2455
+ * everything waiting. A report set up before the pass rides along; one set
2456
+ * up by this pass's own delivery waits for the next batch. */
2419
2457
  private drainLocked;
2420
- /** Starts a report of this process's loss totals (SPEC §11.4.7) beside the
2421
- * drain and outside the buffer, so it never delays an event and is never
2422
- * counted as one. */
2458
+ /** A due report makes the next batch due only when it can ride it; the
2459
+ * timer is armed the same way, so a pass that sends nothing never re-arms
2460
+ * it at a time already past. */
2461
+ private isDue;
2462
+ /** A pending report can go in the next batch: alone, or with the head event's tenant and vendor. */
2463
+ private reportRidesHead;
2464
+ /** Takes the head of the buffer that fits one batch: one tenant and vendor
2465
+ * (SPEC §2.5), at most 100 events and 1 MiB. A due report with the head
2466
+ * event's tenant and vendor takes one of those places; otherwise it is not
2467
+ * built, and waits for a batch it can ride. Without a batch endpoint the same events go one
2468
+ * per request. */
2469
+ private takeBatch;
2470
+ /** Puts a kept batch's events back at the head of the buffer; `true` when
2471
+ * there were none. */
2472
+ private putBack;
2473
+ /** After a delivery, sets up a report of this process's loss totals (SPEC
2474
+ * §11.4.7) to ride the next batch. It stays outside the buffer, so it never
2475
+ * pushes an event out and is never counted as one. */
2423
2476
  private maybeReport;
2424
2477
  private stopReports;
2425
- private sendReport;
2426
- private sendWithRetry;
2478
+ private buildReport;
2479
+ /** Sends one batch, splitting it on 413, until every event in it is
2480
+ * delivered or refused. Returns what a failure kept, to go back in the
2481
+ * buffer. */
2482
+ private sendBatch;
2483
+ private sendEachAlone;
2484
+ /** `alone` is one event per request (SPEC §2.5), so a whole refusal there is that event's own. */
2485
+ private settle;
2427
2486
  /** `null`, with a warning, for an event `JSON.stringify` rejects. */
2428
2487
  private encode;
2429
2488
  private sleepUnlessClosed;
2430
2489
  /** Aborted by the timeout or by `aclose()`. Built by hand: `AbortSignal.any`
2431
2490
  * is missing before Node 20.3, and on some later versions it keeps every
2432
- * combined signal alive as long as the long-lived `closing` signal. */
2433
- private postOnce;
2491
+ * combined signal alive as long as the long-lived `closing` signal. Any body
2492
+ * read is under the same signal, so a stalled body cannot hold the drain. */
2493
+ private post;
2494
+ /** Every answer but a 2xx and a refusal of the whole batch. */
2495
+ private static otherAnswer;
2496
+ /** The refused results by `event_id` (SPEC §2.5). An event with no readable
2497
+ * result was delivered. */
2498
+ private static refusedIn;
2434
2499
  }
2435
2500
 
2436
2501
  /** Principal identity — what a vendor's resolver hands back, and how it goes
@@ -3083,4 +3148,4 @@ interface IntentParam {
3083
3148
  }
3084
3149
  declare const INTENT_PARAMS: readonly IntentParam[];
3085
3150
 
3086
- export { type AnnotateOptions, type AnnotationEvent, AnnotationEventSchema, type AnnotationPayload, AnnotationPayloadSchema, type AuthInfo, type Baton, type BatonConfig, BatonHandle, Client, type ClientOptions, type ClientReportEvent, ClientReportEventSchema, type ClientReportPayload, ClientReportPayloadSchema, DEFAULT_CONSENT_TOKEN, DEPTH_LIMIT, DEPTH_MARKER, type Event, EventSchema, type EventType, EventTypeSchema, FileSink, type HeaderInput, HttpSink, type HttpSinkOptions, INTENT_INSTRUCTIONS, INTENT_PARAMS, type IntentParam, type ListToolsOptions, MultiSink, type ObservedError, type Organization, PRINCIPAL_FORM_HASHED, PRINCIPAL_FORM_RAW, PRINCIPAL_SOURCE_ASSERTED, type Principal, type PrincipalForm, type PrincipalResolutionContext, PrincipalWireSchema, type PromptGetEndEvent, PromptGetEndEventSchema, type PromptGetEndPayload, PromptGetEndPayloadSchema, type PromptGetErrorEvent, PromptGetErrorEventSchema, type PromptGetErrorPayload, PromptGetErrorPayloadSchema, type PromptGetStartEvent, PromptGetStartEventSchema, type PromptGetStartPayload, PromptGetStartPayloadSchema, type PromptListEndEvent, PromptListEndEventSchema, type PromptListEndPayload, PromptListEndPayloadSchema, type PromptListErrorEvent, PromptListErrorEventSchema, type PromptListErrorPayload, PromptListErrorPayloadSchema, type PromptListStartEvent, PromptListStartEventSchema, type PromptListStartPayload, PromptListStartPayloadSchema, type ResolvePrincipalHook, type ResourceListEndEvent, ResourceListEndEventSchema, type ResourceListEndPayload, ResourceListEndPayloadSchema, type ResourceListErrorEvent, ResourceListErrorEventSchema, type ResourceListErrorPayload, ResourceListErrorPayloadSchema, type ResourceListStartEvent, ResourceListStartEventSchema, type ResourceListStartPayload, ResourceListStartPayloadSchema, type ResourceReadEndEvent, ResourceReadEndEventSchema, type ResourceReadEndPayload, ResourceReadEndPayloadSchema, type ResourceReadErrorEvent, ResourceReadErrorEventSchema, type ResourceReadErrorPayload, ResourceReadErrorPayloadSchema, type ResourceReadStartEvent, ResourceReadStartEventSchema, type ResourceReadStartPayload, ResourceReadStartPayloadSchema, type ResultCaptureMode, SDK_VERSION, Scrubber, type SessionOptions, type Sink, StdoutSink, type StdoutSinkOptions, type SupportedMcpServer, type SurfaceSnapshotEvent, SurfaceSnapshotEventSchema, type SurfaceSnapshotPayload, SurfaceSnapshotPayloadSchema, type ToolCallEndEvent, ToolCallEndEventSchema, type ToolCallEndPayload, ToolCallEndPayloadSchema, type ToolCallErrorEvent, ToolCallErrorEventSchema, type ToolCallErrorPayload, ToolCallErrorPayloadSchema, type ToolCallStartEvent, ToolCallStartEventSchema, type ToolCallStartPayload, ToolCallStartPayloadSchema, type ToolListEndEvent, ToolListEndEventSchema, type ToolListEndPayload, ToolListEndPayloadSchema, type ToolListErrorEvent, ToolListErrorEventSchema, type ToolListErrorPayload, ToolListErrorPayloadSchema, type ToolListStartEvent, ToolListStartEventSchema, type ToolListStartPayload, ToolListStartPayloadSchema, Trace, type TraceOptions, createBaton, identityScrub, principalFromOAuthEmail, principalFromOAuthSub, safeWrite, withBaton };
3151
+ export { type AnnotateOptions, type AnnotationEvent, AnnotationEventSchema, type AnnotationPayload, AnnotationPayloadSchema, type AuthInfo, type Baton, type BatonConfig, BatonHandle, Client, type ClientOptions, type ClientReportEvent, ClientReportEventSchema, type ClientReportPayload, ClientReportPayloadSchema, DEFAULT_CONSENT_TOKEN, DEPTH_LIMIT, DEPTH_MARKER, type Event, EventSchema, type EventType, EventTypeSchema, FileSink, type HeaderInput, HttpSink, type HttpSinkOptions, INTENT_INSTRUCTIONS, INTENT_PARAMS, type IntentParam, type ListToolsOptions, MultiSink, type ObservedError, type Organization, PRINCIPAL_FORM_HASHED, PRINCIPAL_FORM_RAW, PRINCIPAL_SOURCE_ASSERTED, type Principal, type PrincipalForm, type PrincipalResolutionContext, PrincipalWireSchema, type PromptGetEndEvent, PromptGetEndEventSchema, type PromptGetEndPayload, PromptGetEndPayloadSchema, type PromptGetErrorEvent, PromptGetErrorEventSchema, type PromptGetErrorPayload, PromptGetErrorPayloadSchema, type PromptGetStartEvent, PromptGetStartEventSchema, type PromptGetStartPayload, PromptGetStartPayloadSchema, type PromptListEndEvent, PromptListEndEventSchema, type PromptListEndPayload, PromptListEndPayloadSchema, type PromptListErrorEvent, PromptListErrorEventSchema, type PromptListErrorPayload, PromptListErrorPayloadSchema, type PromptListStartEvent, PromptListStartEventSchema, type PromptListStartPayload, PromptListStartPayloadSchema, type ResolvePrincipalHook, type ResourceListEndEvent, ResourceListEndEventSchema, type ResourceListEndPayload, ResourceListEndPayloadSchema, type ResourceListErrorEvent, ResourceListErrorEventSchema, type ResourceListErrorPayload, ResourceListErrorPayloadSchema, type ResourceListStartEvent, ResourceListStartEventSchema, type ResourceListStartPayload, ResourceListStartPayloadSchema, type ResourceReadEndEvent, ResourceReadEndEventSchema, type ResourceReadEndPayload, ResourceReadEndPayloadSchema, type ResourceReadErrorEvent, ResourceReadErrorEventSchema, type ResourceReadErrorPayload, ResourceReadErrorPayloadSchema, type ResourceReadStartEvent, ResourceReadStartEventSchema, type ResourceReadStartPayload, ResourceReadStartPayloadSchema, type ResultCaptureMode, SDK_VERSION, Scrubber, type SessionOptions, type Sink, SinkClosedError, StdoutSink, type StdoutSinkOptions, type SupportedMcpServer, type SurfaceSnapshotEvent, SurfaceSnapshotEventSchema, type SurfaceSnapshotPayload, SurfaceSnapshotPayloadSchema, type ToolCallEndEvent, ToolCallEndEventSchema, type ToolCallEndPayload, ToolCallEndPayloadSchema, type ToolCallErrorEvent, ToolCallErrorEventSchema, type ToolCallErrorPayload, ToolCallErrorPayloadSchema, type ToolCallStartEvent, ToolCallStartEventSchema, type ToolCallStartPayload, ToolCallStartPayloadSchema, type ToolListEndEvent, ToolListEndEventSchema, type ToolListEndPayload, ToolListEndPayloadSchema, type ToolListErrorEvent, ToolListErrorEventSchema, type ToolListErrorPayload, ToolListErrorPayloadSchema, type ToolListStartEvent, ToolListStartEventSchema, type ToolListStartPayload, ToolListStartPayloadSchema, Trace, type TraceOptions, createBaton, identityScrub, principalFromOAuthEmail, principalFromOAuthSub, safeWrite, withBaton };