@goodtiming/baton-sdk 0.6.4 → 0.6.6

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.4";
3
+ declare const SDK_VERSION = "ts-0.6.6";
4
4
 
5
5
  /**
6
6
  * Zod schemas + TS types for the Baton event stream per SPEC §11.4.
@@ -2188,7 +2188,7 @@ declare const DEPTH_MARKER = "[REDACTED:depth-limit]";
2188
2188
  /**
2189
2189
  * Stateful recursive scrubber with the default ruleset baked in.
2190
2190
  *
2191
- * Construct one per `withBaton` call and reuse for every event. The
2191
+ * Construct one per `withBaton` or `createBaton` and reuse for every event. The
2192
2192
  * `counts` map accumulates per-category redaction counts across all calls
2193
2193
  * so a downstream report can surface "N emails, M bearer tokens" at render
2194
2194
  * time. Nothing on the wire carries these today — they exist for parity
@@ -2382,7 +2382,8 @@ declare class HttpSink implements Sink {
2382
2382
  private reportedSum;
2383
2383
  private lastReportAt;
2384
2384
  private reportsStopped;
2385
- /** The envelope of a report due to ride the next batch, and when it was set up. */
2385
+ /** The envelope of a report due to ride the next batch, and when it was set up.
2386
+ * `built` is the report once taken: a kept batch resends it unchanged (SPEC §11.4.7). */
2386
2387
  private pendingReport;
2387
2388
  private reportSetAt;
2388
2389
  constructor(url: string, options: HttpSinkOptions);
@@ -2463,12 +2464,14 @@ declare class HttpSink implements Sink {
2463
2464
  private reportRidesHead;
2464
2465
  /** Takes the head of the buffer that fits one batch: one tenant and vendor
2465
2466
  * (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. */
2467
+ * event's tenant and vendor takes one of those places, and goes alone when
2468
+ * the head event fits only without it. A report that cannot ride the head
2469
+ * waits for a batch it can ride. Without a batch endpoint the same events
2470
+ * go one per request. */
2469
2471
  private takeBatch;
2470
- /** Puts a kept batch's events back at the head of the buffer; `true` when
2471
- * there were none. */
2472
+ /** Puts what a failure `kept` of the batch `taken` back at the head of the
2473
+ * buffer, and keeps its report unless `taken` had no events; `true` when
2474
+ * nothing was kept. */
2472
2475
  private putBack;
2473
2476
  /** After a delivery, sets up a report of this process's loss totals (SPEC
2474
2477
  * §11.4.7) to ride the next batch. It stays outside the buffer, so it never
@@ -2656,7 +2659,8 @@ interface BatonConfig {
2656
2659
  * (`{vendorId}_annotate`). This is not the account — see `tenantId`.
2657
2660
  *
2658
2661
  * Required unless a `dsn` supplies it — the DSN's last path segment IS this
2659
- * value, and it is what the key is BOUND to. */
2662
+ * value, and it is what the key is BOUND to. With no DSN, `tenantId` or sink
2663
+ * either, the install adds nothing instead (SPEC §11.2 rule 10). */
2660
2664
  vendorId?: string;
2661
2665
  /** Account identifier for the envelope's `tenant_id` (SPEC §11.4).
2662
2666
  *
@@ -2703,7 +2707,7 @@ interface BatonConfig {
2703
2707
  * one. Adding the read here would make this arm honour a variable the
2704
2708
  * equivalent Python door ignores. */
2705
2709
  consentToken?: string;
2706
- /** Where events go. Defaults to `new StdoutSink()` — zero-config dev mode. */
2710
+ /** Where events go. With a `vendorId` and no DSN, defaults to `new StdoutSink()`. */
2707
2711
  sink?: Sink;
2708
2712
  /** PII scrubber per SPEC §7, applied to tool params/results, `_meta`,
2709
2713
  * intent strings and error bodies before they reach the sink. Defaults to
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.4";
3
+ declare const SDK_VERSION = "ts-0.6.6";
4
4
 
5
5
  /**
6
6
  * Zod schemas + TS types for the Baton event stream per SPEC §11.4.
@@ -2188,7 +2188,7 @@ declare const DEPTH_MARKER = "[REDACTED:depth-limit]";
2188
2188
  /**
2189
2189
  * Stateful recursive scrubber with the default ruleset baked in.
2190
2190
  *
2191
- * Construct one per `withBaton` call and reuse for every event. The
2191
+ * Construct one per `withBaton` or `createBaton` and reuse for every event. The
2192
2192
  * `counts` map accumulates per-category redaction counts across all calls
2193
2193
  * so a downstream report can surface "N emails, M bearer tokens" at render
2194
2194
  * time. Nothing on the wire carries these today — they exist for parity
@@ -2382,7 +2382,8 @@ declare class HttpSink implements Sink {
2382
2382
  private reportedSum;
2383
2383
  private lastReportAt;
2384
2384
  private reportsStopped;
2385
- /** The envelope of a report due to ride the next batch, and when it was set up. */
2385
+ /** The envelope of a report due to ride the next batch, and when it was set up.
2386
+ * `built` is the report once taken: a kept batch resends it unchanged (SPEC §11.4.7). */
2386
2387
  private pendingReport;
2387
2388
  private reportSetAt;
2388
2389
  constructor(url: string, options: HttpSinkOptions);
@@ -2463,12 +2464,14 @@ declare class HttpSink implements Sink {
2463
2464
  private reportRidesHead;
2464
2465
  /** Takes the head of the buffer that fits one batch: one tenant and vendor
2465
2466
  * (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. */
2467
+ * event's tenant and vendor takes one of those places, and goes alone when
2468
+ * the head event fits only without it. A report that cannot ride the head
2469
+ * waits for a batch it can ride. Without a batch endpoint the same events
2470
+ * go one per request. */
2469
2471
  private takeBatch;
2470
- /** Puts a kept batch's events back at the head of the buffer; `true` when
2471
- * there were none. */
2472
+ /** Puts what a failure `kept` of the batch `taken` back at the head of the
2473
+ * buffer, and keeps its report unless `taken` had no events; `true` when
2474
+ * nothing was kept. */
2472
2475
  private putBack;
2473
2476
  /** After a delivery, sets up a report of this process's loss totals (SPEC
2474
2477
  * §11.4.7) to ride the next batch. It stays outside the buffer, so it never
@@ -2656,7 +2659,8 @@ interface BatonConfig {
2656
2659
  * (`{vendorId}_annotate`). This is not the account — see `tenantId`.
2657
2660
  *
2658
2661
  * Required unless a `dsn` supplies it — the DSN's last path segment IS this
2659
- * value, and it is what the key is BOUND to. */
2662
+ * value, and it is what the key is BOUND to. With no DSN, `tenantId` or sink
2663
+ * either, the install adds nothing instead (SPEC §11.2 rule 10). */
2660
2664
  vendorId?: string;
2661
2665
  /** Account identifier for the envelope's `tenant_id` (SPEC §11.4).
2662
2666
  *
@@ -2703,7 +2707,7 @@ interface BatonConfig {
2703
2707
  * one. Adding the read here would make this arm honour a variable the
2704
2708
  * equivalent Python door ignores. */
2705
2709
  consentToken?: string;
2706
- /** Where events go. Defaults to `new StdoutSink()` — zero-config dev mode. */
2710
+ /** Where events go. With a `vendorId` and no DSN, defaults to `new StdoutSink()`. */
2707
2711
  sink?: Sink;
2708
2712
  /** PII scrubber per SPEC §7, applied to tool params/results, `_meta`,
2709
2713
  * intent strings and error bodies before they reach the sink. Defaults to
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/version.ts
2
- var SDK_VERSION = "ts-0.6.4";
2
+ var SDK_VERSION = "ts-0.6.6";
3
3
 
4
4
  // src/events.ts
5
5
  import { z } from "zod";
@@ -841,7 +841,8 @@ var HttpSink = class _HttpSink {
841
841
  reportedSum = 0;
842
842
  lastReportAt = -Infinity;
843
843
  reportsStopped = false;
844
- /** The envelope of a report due to ride the next batch, and when it was set up. */
844
+ /** The envelope of a report due to ride the next batch, and when it was set up.
845
+ * `built` is the report once taken: a kept batch resends it unchanged (SPEC §11.4.7). */
845
846
  pendingReport = null;
846
847
  reportSetAt = 0;
847
848
  constructor(url, options) {
@@ -1057,7 +1058,7 @@ var HttpSink = class _HttpSink {
1057
1058
  while (this.circuit.canRequest() && Date.now() >= this.retryAt && Date.now() < deadline) {
1058
1059
  const batch = this.takeBatch(false);
1059
1060
  if (batch === null) break;
1060
- if (!this.putBack(await this.sendBatch(batch, 0, deadline))) break;
1061
+ if (!this.putBack(await this.sendBatch(batch, 0, deadline), batch)) break;
1061
1062
  }
1062
1063
  this.exitPassRan = true;
1063
1064
  this.unsentWarned = true;
@@ -1144,7 +1145,7 @@ var HttpSink = class _HttpSink {
1144
1145
  const batch = this.takeBatch(reportDue);
1145
1146
  if (batch === null) break;
1146
1147
  if (batch.some((w) => w.reportSum !== void 0)) reportDue = false;
1147
- if (!this.putBack(await this.sendBatch(batch, this.maxRetries, Infinity))) {
1148
+ if (!this.putBack(await this.sendBatch(batch, this.maxRetries, Infinity), batch)) {
1148
1149
  failed = true;
1149
1150
  break;
1150
1151
  }
@@ -1175,19 +1176,20 @@ var HttpSink = class _HttpSink {
1175
1176
  }
1176
1177
  /** Takes the head of the buffer that fits one batch: one tenant and vendor
1177
1178
  * (SPEC §2.5), at most 100 events and 1 MiB. A due report with the head
1178
- * event's tenant and vendor takes one of those places; otherwise it is not
1179
- * built, and waits for a batch it can ride. Without a batch endpoint the same events go one
1180
- * per request. */
1179
+ * event's tenant and vendor takes one of those places, and goes alone when
1180
+ * the head event fits only without it. A report that cannot ride the head
1181
+ * waits for a batch it can ride. Without a batch endpoint the same events
1182
+ * go one per request. */
1181
1183
  takeBatch(withReport) {
1182
1184
  const batch = [];
1183
1185
  let bytes = BATCH_FRAME_BYTES - 1;
1184
1186
  let eventBytes = 0;
1185
- const fits = (w, maxEvents2 = BATCH_MAX_EVENTS, maxBytes2 = BATCH_MAX_BYTES) => batch.length === 0 || batch.length < maxEvents2 && sameBatch(w.event, batch[0].event) && bytes + 1 + w.bytes <= maxBytes2;
1186
- const report = withReport && this.reportRidesHead() ? this.buildReport() : null;
1187
+ const report = withReport && this.reportRidesHead() ? this.pendingReport.built ?? this.buildReport() : null;
1187
1188
  const maxEvents = BATCH_MAX_EVENTS - (report ? 1 : 0);
1188
1189
  const maxBytes = BATCH_MAX_BYTES - (report ? 1 + report.bytes : 0);
1190
+ const fits = (w) => bytes + 1 + w.bytes <= maxBytes && (batch.length === 0 || batch.length < maxEvents && sameBatch(w.event, batch[0].event));
1189
1191
  for (const waiting of this.buffer) {
1190
- if (!fits(waiting, maxEvents, maxBytes)) break;
1192
+ if (!fits(waiting)) break;
1191
1193
  batch.push(waiting);
1192
1194
  bytes += 1 + waiting.bytes;
1193
1195
  eventBytes += waiting.bytes;
@@ -1195,21 +1197,19 @@ var HttpSink = class _HttpSink {
1195
1197
  this.buffer.splice(0, batch.length);
1196
1198
  this.bufferedBytes -= eventBytes;
1197
1199
  this.sendingEvents = batch.length;
1198
- if (report !== null && fits(report)) {
1199
- report.alone = batch.length === 0;
1200
- batch.push(report);
1201
- }
1200
+ if (report !== null) batch.push(report);
1202
1201
  return batch.length > 0 ? batch : null;
1203
1202
  }
1204
- /** Puts a kept batch's events back at the head of the buffer; `true` when
1205
- * there were none. */
1206
- putBack(kept) {
1203
+ /** Puts what a failure `kept` of the batch `taken` back at the head of the
1204
+ * buffer, and keeps its report unless `taken` had no events; `true` when
1205
+ * nothing was kept. */
1206
+ putBack(kept, taken) {
1207
1207
  this.sendingEvents = 0;
1208
1208
  if (this.closed) return kept.length === 0;
1209
1209
  const events = [];
1210
1210
  for (const waiting of kept) {
1211
1211
  if (waiting.reportSum !== void 0) {
1212
- if (waiting.alone || this.batchless) this.pendingReport = null;
1212
+ if (taken.every((w) => w.reportSum !== void 0) || this.batchless) this.pendingReport = null;
1213
1213
  continue;
1214
1214
  }
1215
1215
  events.push(waiting);
@@ -1238,17 +1238,21 @@ var HttpSink = class _HttpSink {
1238
1238
  }
1239
1239
  buildReport() {
1240
1240
  if (this.pendingReport === null) return null;
1241
+ const { tenant_id, vendor_id, consent_token } = this.pendingReport;
1241
1242
  const totals = PROCESS_DROPS.total();
1242
1243
  try {
1243
1244
  const report = ClientReportEventSchema.parse({
1244
- ...this.pendingReport,
1245
+ tenant_id,
1246
+ vendor_id,
1247
+ consent_token,
1245
1248
  session_id: PROCESS_DROPS.instanceId,
1246
1249
  sequence_number: nextClockSequence(),
1247
1250
  captured_at: (/* @__PURE__ */ new Date()).toISOString(),
1248
1251
  payload: { instance_id: PROCESS_DROPS.instanceId, dropped: totals }
1249
1252
  });
1250
1253
  const json = JSON.stringify(report);
1251
- return { event: report, json, bytes: utf8Bytes(json), writtenAt: Date.now(), reportSum: sum(Object.values(totals)) };
1254
+ this.pendingReport.built = { event: report, json, bytes: utf8Bytes(json), writtenAt: Date.now(), reportSum: sum(Object.values(totals)) };
1255
+ return this.pendingReport.built;
1252
1256
  } catch {
1253
1257
  this.pendingReport = null;
1254
1258
  return null;
@@ -2357,6 +2361,18 @@ function resolveTenantId(explicit, vendorId2) {
2357
2361
  if (fromEnv2) return fromEnv2;
2358
2362
  return vendorId2;
2359
2363
  }
2364
+ var STAND_IN_VENDOR_ID = "unset";
2365
+ function resolveInstallConfig(config) {
2366
+ const unconfigured = !config.dsn && dsnFromEnvironment() === void 0 && !config.vendorId && !config.tenantId && config.sink === void 0;
2367
+ const resolved = resolveBatonConfig(
2368
+ unconfigured ? {
2369
+ ...config,
2370
+ vendorId: STAND_IN_VENDOR_ID,
2371
+ vendorDisplayName: config.vendorDisplayName ?? STAND_IN_VENDOR_ID
2372
+ } : config
2373
+ );
2374
+ return { resolved, unconfigured };
2375
+ }
2360
2376
  function resolveBatonConfig(config) {
2361
2377
  const dsnString = selectDsn(
2362
2378
  config.dsn,
@@ -2488,11 +2504,12 @@ function captureDisabled() {
2488
2504
  return null;
2489
2505
  }
2490
2506
  function logDisabled(switchName, surface) {
2491
- if (typeof process !== "undefined" && typeof process.emitWarning === "function") {
2492
- process.emitWarning(
2493
- `baton: ${switchName} is set, so capture is OFF \u2014 ${surface} installed nothing and will emit no events. Unset it to re-enable.`
2494
- );
2495
- }
2507
+ warn(
2508
+ `baton: ${switchName} is set, so capture is OFF \u2014 ${surface} installed nothing and will emit no events. Unset it to re-enable.`
2509
+ );
2510
+ }
2511
+ function logUnconfigured(surface) {
2512
+ warn(`baton: no DSN (dsn or BATON_DSN), so ${surface} installed nothing and records nothing`);
2496
2513
  }
2497
2514
  var DisabledSink = class {
2498
2515
  // The parameter is omitted rather than named-and-ignored: a narrower
@@ -2571,6 +2588,19 @@ var SessionCounter = class {
2571
2588
  return next;
2572
2589
  }
2573
2590
  };
2591
+ var ServerCounter = class extends SessionCounter {
2592
+ constructor(shared, ownSessionId) {
2593
+ super();
2594
+ this.shared = shared;
2595
+ this.ownSessionId = ownSessionId;
2596
+ }
2597
+ shared;
2598
+ ownSessionId;
2599
+ ownSequence = 0;
2600
+ next(sessionId) {
2601
+ return sessionId === this.ownSessionId ? ++this.ownSequence : this.shared.next(sessionId);
2602
+ }
2603
+ };
2574
2604
 
2575
2605
  // src/integrations/mcp/zodCompat.ts
2576
2606
  import * as z3rt from "zod/v3";
@@ -2788,6 +2818,7 @@ function buildSeamAugmentations(options) {
2788
2818
  var BATON_WRAPPED = /* @__PURE__ */ Symbol("batonWrapped");
2789
2819
  var wired = /* @__PURE__ */ new WeakSet();
2790
2820
  var CLEAN_OUTCOME = { failed: false, raised: false, result: void 0 };
2821
+ var INTENT_PARAM_NAMES = [USER_GOAL_PARAM_NAME, ...RETIRED_PARAM_NAMES];
2791
2822
  var callSlots = new AsyncLocalStorage();
2792
2823
  var SurfaceState = class {
2793
2824
  constructor(serverMeta, sentSurfaces) {
@@ -2850,14 +2881,14 @@ async function resolveEnvelopeParts(ctx, extra) {
2850
2881
  const session = await resolveSessionId(ctx.fallbackSessionId, extra);
2851
2882
  return { ...session, scrubbedMeta };
2852
2883
  }
2853
- async function openCall(ctx, toolName, extra, params, retired, registered = true) {
2884
+ async function openCall(ctx, toolName, extra, params, stripped, registered = true) {
2854
2885
  await maybeEmitSurfaceSnapshot(ctx);
2855
2886
  const { scrubbedMeta, ...session } = await resolveEnvelopeParts(ctx, extra);
2856
2887
  const dispositions = ctx.paramRegistry.get(toolName);
2857
- const rawIntent = extractGoalParam(params, USER_GOAL_PARAM_NAME, toolName, dispositions, registered);
2888
+ const rawIntent = stripped[USER_GOAL_PARAM_NAME] ?? extractGoalParam(params, USER_GOAL_PARAM_NAME, toolName, dispositions, registered);
2858
2889
  const scrubbedIntent = scrubOrNull(ctx.scrubber, rawIntent, USER_GOAL_PARAM_NAME);
2859
2890
  const [scrubbedExpected = null, scrubbedWorkflow = null] = RETIRED_PARAM_NAMES.map(
2860
- (name) => dispositions === void 0 || dispositions[name] === "native" ? null : scrubOrNull(ctx.scrubber, retired[name] ?? popText(params, name), name)
2891
+ (name) => dispositions === void 0 || dispositions[name] === "native" ? null : scrubOrNull(ctx.scrubber, stripped[name] ?? popText(params, name), name)
2861
2892
  );
2862
2893
  const caller = await resolveCaller(
2863
2894
  ctx.resolvePrincipal,
@@ -2907,9 +2938,9 @@ function batonWrap(nameRef, original, ctx) {
2907
2938
  if (slot) slot.innerFired = true;
2908
2939
  const extra = callArgs[callArgs.length - 1];
2909
2940
  const params = (callArgs.length > 1 ? callArgs[0] : void 0) ?? {};
2910
- const retired = slot?.retired ?? {};
2911
- if (slot) slot.retired = {};
2912
- const { common, session } = await openCall(ctx, toolName, extra, params, retired);
2941
+ const stripped = slot?.strippedFor === toolName ? slot.stripped : {};
2942
+ if (slot) slot.strippedFor = null;
2943
+ const { common, session } = await openCall(ctx, toolName, extra, params, stripped);
2913
2944
  const terminate = async (build, replacement) => {
2914
2945
  if (!slot) {
2915
2946
  if (build !== null) await emit(ctx.sink, build);
@@ -3085,19 +3116,19 @@ function popText(args, paramName) {
3085
3116
  delete args[paramName];
3086
3117
  return typeof raw === "string" && !isBlank(raw) ? raw : null;
3087
3118
  }
3088
- function stripRetiredParams(params, ctx) {
3119
+ function stripIntentParams(params, ctx) {
3089
3120
  const toolName = params?.name;
3090
3121
  const args = params?.arguments;
3091
3122
  if (typeof toolName !== "string") return {};
3092
3123
  if (toolName === ctx.annotationToolName) return {};
3093
3124
  if (typeof args !== "object" || args === null || Array.isArray(args)) return {};
3094
3125
  const dispositions = ctx.paramRegistry.get(toolName);
3095
- const retired = {};
3096
- for (const name of RETIRED_PARAM_NAMES) {
3126
+ const stripped = {};
3127
+ for (const name of INTENT_PARAM_NAMES) {
3097
3128
  if (dispositions === void 0 || dispositions[name] === "native" || !(name in args)) continue;
3098
- retired[name] = popText(args, name);
3129
+ stripped[name] = popText(args, name);
3099
3130
  }
3100
- return retired;
3131
+ return stripped;
3101
3132
  }
3102
3133
  async function maybeEmitSurfaceSnapshot(ctx) {
3103
3134
  if (!ctx.surfaceState.dirty) return;
@@ -3133,7 +3164,7 @@ async function maybeEmitSurfaceSnapshot(ctx) {
3133
3164
  }
3134
3165
  function declaredIntentParams(vendorSchema) {
3135
3166
  const declared2 = {};
3136
- for (const name of [USER_GOAL_PARAM_NAME, ...RETIRED_PARAM_NAMES]) {
3167
+ for (const name of INTENT_PARAM_NAMES) {
3137
3168
  if (declaresParam(vendorSchema, name)) declared2[name] = "native";
3138
3169
  }
3139
3170
  return declared2;
@@ -3242,10 +3273,9 @@ function installLifecycleSeam(lowLevel, ctx, spec) {
3242
3273
  } catch (err) {
3243
3274
  await fire(spec.schemas.error, {
3244
3275
  error_type: errorTypeOf(err),
3245
- // KEPT under every capture mode: a failed FETCH's message is the
3246
- // producer's own diagnostic, not anything a resource returned, so
3247
- // nothing here is result-derived (SPEC §11.4.4). The scrub and the cap
3248
- // are `scrubLifecyclePayload`'s, in that order.
3276
+ // KEPT under every capture mode (SPEC §11.4.4), though it can quote what
3277
+ // the handler fetched, so `"off"` warns instead: `warnAboutSettings`.
3278
+ // The scrub and the cap are `scrubLifecyclePayload`'s, in that order.
3249
3279
  error_body: messageOf(err),
3250
3280
  duration_ms: elapsed()
3251
3281
  });
@@ -3399,10 +3429,12 @@ function installToolsCallSeam(lowLevel, toolEntry, ctx) {
3399
3429
  TOOLS_CALL_SEAM,
3400
3430
  (handler) => async (...args) => {
3401
3431
  const request = args[0];
3432
+ const name = request?.params?.name;
3402
3433
  const slot = {
3403
3434
  innerFired: false,
3404
3435
  flushTerminal: null,
3405
- retired: stripRetiredParams(request?.params, ctx),
3436
+ stripped: stripIntentParams(request?.params, ctx),
3437
+ strippedFor: typeof name === "string" ? name : null,
3406
3438
  validation: "unseen"
3407
3439
  };
3408
3440
  let outcome = CLEAN_OUTCOME;
@@ -3468,7 +3500,7 @@ async function emitRequestSideFailure(outcome, args, slot, toolEntry, ctx) {
3468
3500
  toolName,
3469
3501
  args[1],
3470
3502
  params,
3471
- slot.retired,
3503
+ slot.stripped,
3472
3504
  entry != null
3473
3505
  );
3474
3506
  const failureKind = requestSideFailureKind(entry, slot.validation);
@@ -3577,23 +3609,13 @@ function withBaton(server, supplied = {}) {
3577
3609
  const disabledBy = captureDisabled();
3578
3610
  if (disabledBy !== null) {
3579
3611
  logDisabled(disabledBy, "withBaton");
3580
- return new BatonHandle({
3581
- // A sink the VENDOR constructed is held rather than dropped, so their
3582
- // `handle.aclose()` still releases it — we took ownership of that object
3583
- // the moment they passed it, and the switch does not undo that. Nothing
3584
- // writes to it: nothing is wrapped.
3585
- sink: supplied.sink ?? new DisabledSink(),
3586
- // Empty, and a constant, because when the switch is on nothing was
3587
- // resolved: inventing a session id would put a real identifier on a
3588
- // handle whose whole meaning is that no events exist under it.
3589
- vendorId: "",
3590
- sessionId: "baton-disabled",
3591
- annotationToolName: ""
3592
- });
3612
+ return inertHandle(supplied.sink ?? new DisabledSink(), "baton-disabled");
3593
3613
  }
3594
- const resolved = resolveBatonConfig(supplied);
3595
- warnUnusedReportSettings(resolved);
3596
- return install(server, supplied, resolved, new SentSurfaces(), /* @__PURE__ */ new Set());
3614
+ const { resolved, unconfigured } = resolveInstallConfig(supplied);
3615
+ const handle = install(server, supplied, resolved, newBatonState(resolved), unconfigured);
3616
+ if (unconfigured) logUnconfigured("withBaton");
3617
+ else warnAboutSettings(resolved);
3618
+ return handle;
3597
3619
  }
3598
3620
  function createBaton(supplied = {}) {
3599
3621
  const disabledBy = captureDisabled();
@@ -3607,40 +3629,61 @@ function createBaton(supplied = {}) {
3607
3629
  aclose: () => boundedSinkCall(unused, "aclose")
3608
3630
  };
3609
3631
  }
3610
- const resolved = resolveBatonConfig(supplied);
3611
- warnUnusedReportSettings(resolved);
3632
+ const { resolved, unconfigured } = resolveInstallConfig(supplied);
3612
3633
  if (resolved.reportMode === "on") deriveAnnotationToolName(resolved.vendorId, supplied.annotationToolName);
3613
- const sink = resolved.sink ?? new StdoutSink();
3634
+ if (unconfigured) logUnconfigured("createBaton");
3635
+ else warnAboutSettings(resolved);
3636
+ const sink = unconfigured ? new DisabledSink() : resolved.sink ?? new StdoutSink();
3614
3637
  const config = {
3615
3638
  ...resolved,
3616
3639
  sink,
3617
3640
  tenantId: resolveTenantId(resolved.tenantId, resolved.vendorId)
3618
3641
  };
3619
- const sentSurfaces = new SentSurfaces();
3620
- const warned3 = /* @__PURE__ */ new Set();
3642
+ const state = newBatonState(resolved);
3621
3643
  return {
3622
3644
  wrap: (server) => {
3623
- install(server, supplied, config, sentSurfaces, warned3);
3645
+ install(server, supplied, config, state, unconfigured);
3624
3646
  },
3625
3647
  flush: () => boundedSinkCall(sink, "flush"),
3626
3648
  aclose: () => boundedSinkCall(sink, "aclose")
3627
3649
  };
3628
3650
  }
3629
- function install(server, supplied, resolved, sentSurfaces, warned3) {
3651
+ function inertHandle(sink, sessionId) {
3652
+ return new BatonHandle({ sink, sessionId, vendorId: "", annotationToolName: "" });
3653
+ }
3654
+ function requireHighLevelServer(server) {
3655
+ const candidate = server;
3656
+ if (typeof candidate?.registerTool !== "function" || typeof candidate.server !== "object" || candidate.server === null) {
3657
+ throw new TypeError(
3658
+ "Baton needs a high-level McpServer (from @modelcontextprotocol/sdk/server/mcp.js or @modelcontextprotocol/server); the low-level Server is not supported."
3659
+ );
3660
+ }
3661
+ }
3662
+ function newBatonState(config) {
3663
+ return {
3664
+ sentSurfaces: new SentSurfaces(),
3665
+ warned: /* @__PURE__ */ new Set(),
3666
+ counter: new SessionCounter(),
3667
+ scrubber: config.scrubber ?? new Scrubber().scrub
3668
+ };
3669
+ }
3670
+ function install(server, supplied, resolved, { sentSurfaces, warned: warned3, counter: sharedCounter, scrubber }, unconfigured) {
3671
+ requireHighLevelServer(server);
3630
3672
  const internals = server;
3631
3673
  const serverName = usableServerName(() => ({
3632
3674
  name: internals.server._serverInfo?.name,
3633
3675
  className: internals.constructor.name
3634
3676
  }));
3635
3677
  const config = resolved.reportMode === "on" ? withServerDisplayName(resolved, supplied, serverName) : resolved;
3636
- const sink = config.sink ?? new StdoutSink();
3637
3678
  const reports = reportsFor(config);
3638
3679
  const annotationToolName = reports ? resolveAnnotationToolName(serverName, { ...config, vendorDisplayName: reports.vendorDisplayName }) : "";
3680
+ const ours = reports ? buildServerInstructions({ vendorDisplayName: reports.vendorDisplayName, annotationToolName }) : INTENT_INSTRUCTIONS;
3681
+ if (unconfigured) return inertHandle(new DisabledSink(), "baton-unconfigured");
3682
+ const sink = config.sink ?? new StdoutSink();
3639
3683
  const intentParamMode = config.intentParamMode ?? DEFAULT_INTENT_PARAM_MODE;
3640
- const counter2 = new SessionCounter();
3641
3684
  const fallbackSessionId = `sdk-${uuid7()}`;
3685
+ const counter2 = new ServerCounter(sharedCounter, fallbackSessionId);
3642
3686
  const tenantId = resolveTenantId(config.tenantId, config.vendorId);
3643
- const scrubber = config.scrubber ?? new Scrubber().scrub;
3644
3687
  const serverMeta = buildServerMeta(server.server);
3645
3688
  const surfaceState = new SurfaceState(serverMeta, sentSurfaces);
3646
3689
  const emitSurface = async (sessionId, digest, snapshot) => {
@@ -3735,7 +3778,6 @@ function install(server, supplied, resolved, sentSurfaces, warned3) {
3735
3778
  surfaceState,
3736
3779
  emitSurface
3737
3780
  };
3738
- const ours = reports ? buildServerInstructions({ vendorDisplayName: reports.vendorDisplayName, annotationToolName }) : INTENT_INSTRUCTIONS;
3739
3781
  internals.server._instructions = withVendorInstructions(
3740
3782
  internals.server,
3741
3783
  ours,
@@ -3796,13 +3838,18 @@ function reportsFor(config) {
3796
3838
  if (config.reportMode !== "on") return void 0;
3797
3839
  return { vendorDisplayName: config.vendorDisplayName ?? "" };
3798
3840
  }
3799
- function warnUnusedReportSettings(config) {
3841
+ function warnAboutSettings(config) {
3800
3842
  if (config.reportMode !== "on" && config.annotationToolName !== void 0) {
3801
3843
  process.stderr.write(
3802
3844
  `baton: annotationToolName=${JSON.stringify(config.annotationToolName)} has no effect with reportMode: "off", because no annotation tool is registered
3803
3845
  `
3804
3846
  );
3805
3847
  }
3848
+ if (config.resultCaptureMode === WITHHELD) {
3849
+ warn(
3850
+ 'baton: resultCaptureMode "off" does not withhold the message of a failed resource or prompt request. resource_list_error, resource_read_error, prompt_list_error and prompt_get_error carry that message, which can quote what your handler fetched. Throw a message that names the resource or prompt, not its contents.'
3851
+ );
3852
+ }
3806
3853
  }
3807
3854
 
3808
3855
  // src/integrations/mcp/oauthHooks.ts