@goodtiming/baton-sdk 0.3.4 → 0.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -108,7 +108,7 @@ collected. One real line from that stream, pretty-printed:
108
108
  "consent_token": "customer-consented",
109
109
  "sdk_version": "ts-0.3.0",
110
110
  "agent_runtime": "unknown",
111
- "user_id": null,
111
+ "principal_id": null,
112
112
  "runtime_meta": null,
113
113
  "event_type": "tool_call_start",
114
114
  "payload": {
package/dist/index.cjs CHANGED
@@ -50,7 +50,7 @@ __export(index_exports, {
50
50
  ToolCallStartEventSchema: () => ToolCallStartEventSchema,
51
51
  ToolCallStartPayloadSchema: () => ToolCallStartPayloadSchema,
52
52
  VENDOR_HASH_SCHEME: () => VENDOR_HASH_SCHEME,
53
- hashUserId: () => hashUserId,
53
+ hashPrincipalId: () => hashPrincipalId,
54
54
  identityScrub: () => identityScrub,
55
55
  safeWrite: () => safeWrite,
56
56
  withBaton: () => withBaton
@@ -58,7 +58,7 @@ __export(index_exports, {
58
58
  module.exports = __toCommonJS(index_exports);
59
59
 
60
60
  // src/version.ts
61
- var SDK_VERSION = "ts-0.3.4";
61
+ var SDK_VERSION = "ts-0.3.5";
62
62
 
63
63
  // src/events.ts
64
64
  var import_zod = require("zod");
@@ -111,7 +111,7 @@ var envelopeShape = {
111
111
  consent_token: import_zod.z.string(),
112
112
  sdk_version: import_zod.z.string().default(SDK_VERSION),
113
113
  agent_runtime: import_zod.z.string().default("unknown"),
114
- user_id: import_zod.z.string().nullable().default(null),
114
+ principal_id: import_zod.z.string().nullable().default(null),
115
115
  call_id: import_zod.z.string().nullable().default(null),
116
116
  runtime_meta: import_zod.z.record(import_zod.z.string(), import_zod.z.unknown()).nullable().default(null)
117
117
  };
@@ -463,9 +463,9 @@ function capCodePoints(value, max) {
463
463
 
464
464
  // src/identity.ts
465
465
  var VENDOR_HASH_SCHEME = "v1";
466
- var RAW_USER_ID_MAX_LEN = 128;
467
- var USER_ID_MODE_HASHED = "hashed";
468
- var USER_ID_MODE_RAW = "raw";
466
+ var RAW_PRINCIPAL_ID_MAX_LEN = 128;
467
+ var PRINCIPAL_ID_MODE_HASHED = "hashed";
468
+ var PRINCIPAL_ID_MODE_RAW = "raw";
469
469
  var PYTHON_WHITESPACE = "\\t\\n\\v\\f\\r\\x1c-\\x1f \\x85\\xa0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000";
470
470
  var PYTHON_STRIP = new RegExp(`^[${PYTHON_WHITESPACE}]+|[${PYTHON_WHITESPACE}]+$`, "gu");
471
471
  var LONE_SURROGATE = /\p{Surrogate}/u;
@@ -475,7 +475,7 @@ function canonicalize(rawPrincipal) {
475
475
  function pythonStrip(value) {
476
476
  return value.replace(PYTHON_STRIP, "");
477
477
  }
478
- function hashUserId(rawPrincipal, options) {
478
+ function hashPrincipalId(rawPrincipal, options) {
479
479
  const { tenantId, key, issuer = null, scheme } = options;
480
480
  let message = `${tenantId}\0${canonicalize(rawPrincipal)}`;
481
481
  if (issuer !== null) {
@@ -488,19 +488,19 @@ function hashUserId(rawPrincipal, options) {
488
488
  function normalizePrincipal(result) {
489
489
  if (result === null || typeof result !== "object") return null;
490
490
  const candidate = result;
491
- if (typeof candidate.userId !== "string") return null;
492
- if (LONE_SURROGATE.test(candidate.userId)) return null;
493
- if (canonicalize(candidate.userId) === "") return null;
491
+ if (typeof candidate.principalId !== "string") return null;
492
+ if (LONE_SURROGATE.test(candidate.principalId)) return null;
493
+ if (canonicalize(candidate.principalId) === "") return null;
494
494
  const issuerRaw = typeof candidate.issuer === "string" && candidate.issuer ? candidate.issuer : null;
495
495
  const issuer = issuerRaw !== null && !LONE_SURROGATE.test(issuerRaw) ? issuerRaw : null;
496
- return { userId: candidate.userId, issuer };
496
+ return { principalId: candidate.principalId, issuer };
497
497
  }
498
- function userIdForPrincipal(principal, options) {
499
- if (options.mode === USER_ID_MODE_RAW) {
500
- return capCodePoints(principal.userId, RAW_USER_ID_MAX_LEN);
498
+ function principalIdFor(principal, options) {
499
+ if (options.mode === PRINCIPAL_ID_MODE_RAW) {
500
+ return capCodePoints(principal.principalId, RAW_PRINCIPAL_ID_MAX_LEN);
501
501
  }
502
502
  if (options.key === null || options.key === void 0) return null;
503
- return hashUserId(principal.userId, {
503
+ return hashPrincipalId(principal.principalId, {
504
504
  tenantId: options.tenantId,
505
505
  key: options.key,
506
506
  issuer: principal.issuer ?? null,
@@ -698,8 +698,8 @@ function extraHeaders(extra) {
698
698
  return null;
699
699
  }
700
700
 
701
- // src/integrations/mcp/userResolution.ts
702
- function buildUserResolutionContext(extra, toolName, args) {
701
+ // src/integrations/mcp/principalResolution.ts
702
+ function buildPrincipalResolutionContext(extra, toolName, args) {
703
703
  return {
704
704
  headers: extraHeaders(extra),
705
705
  meta: extraMeta(extra),
@@ -708,27 +708,40 @@ function buildUserResolutionContext(extra, toolName, args) {
708
708
  };
709
709
  }
710
710
  function warnIfIdentityCannotResolve(config) {
711
- if (config.resolveUser === void 0) return;
712
- if (config.userIdMode !== USER_ID_MODE_HASHED) return;
713
- if (config.userIdHmacKey !== void 0) return;
711
+ if (config.resolvePrincipal === void 0) return;
712
+ if (config.principalIdMode !== PRINCIPAL_ID_MODE_HASHED) return;
713
+ if (config.principalIdHmacKey !== void 0) return;
714
714
  if (typeof process === "undefined" || typeof process.emitWarning !== "function") return;
715
+ const renamed = process.env?.BATON_USER_ID_HMAC_KEY ? "BATON_USER_ID_HMAC_KEY is set, but it was renamed to BATON_PRINCIPAL_ID_HMAC_KEY in 0.3.5 and is no longer read. " : "";
715
716
  process.emitWarning(
716
- 'baton: resolveUser is configured but no user_id HMAC key is set, so user_id is dropped from every event (events still emit). Set BATON_USER_ID_HMAC_KEY or BatonConfig.userIdHmacKey, or pass userIdMode: "raw" if you intend to emit the subject verbatim.'
717
+ `baton: resolvePrincipal is configured but no principal_id HMAC key is set, so principal_id is dropped from every event (events still emit). ${renamed}Set BATON_PRINCIPAL_ID_HMAC_KEY or BatonConfig.principalIdHmacKey, or pass principalIdMode: "raw" if you intend to emit the subject verbatim.`
717
718
  );
718
719
  }
719
- async function resolveCallUserId(hook, call, options) {
720
+ var warnedPreRenameShape = false;
721
+ function warnIfPreRenameShape(result) {
722
+ const userId = result?.userId;
723
+ if (warnedPreRenameShape || typeof userId !== "string") return;
724
+ warnedPreRenameShape = true;
725
+ warn(
726
+ "baton: resolvePrincipal returned { userId }, which was renamed to { principalId } in 0.3.5, so principal_id is dropped from every event until the hook returns the new key."
727
+ );
728
+ }
729
+ async function resolveCallPrincipalId(hook, call, options) {
720
730
  if (hook === void 0) return null;
721
731
  let result;
722
732
  try {
723
- const context = buildUserResolutionContext(call.extra, call.toolName, call.arguments);
733
+ const context = buildPrincipalResolutionContext(call.extra, call.toolName, call.arguments);
724
734
  result = await hook(context);
725
735
  } catch {
726
736
  return null;
727
737
  }
728
738
  const principal = normalizePrincipal(result);
729
- if (principal === null) return null;
739
+ if (principal === null) {
740
+ warnIfPreRenameShape(result);
741
+ return null;
742
+ }
730
743
  try {
731
- return userIdForPrincipal(principal, {
744
+ return principalIdFor(principal, {
732
745
  mode: options.mode,
733
746
  tenantId: options.tenantId,
734
747
  key: options.key
@@ -827,13 +840,13 @@ function registerAnnotationTool(server, options) {
827
840
  if (args.signal_type === void 0) {
828
841
  options.tracker?.mark(sessionId);
829
842
  }
830
- const userId = await resolveCallUserId(
831
- options.resolveUser,
843
+ const principalId = await resolveCallPrincipalId(
844
+ options.resolvePrincipal,
832
845
  { extra, toolName: name, arguments: args },
833
846
  {
834
- mode: options.userIdMode,
847
+ mode: options.principalIdMode,
835
848
  tenantId: options.tenantId,
836
- key: options.userIdHmacKey
849
+ key: options.principalIdHmacKey
837
850
  }
838
851
  );
839
852
  await emit(
@@ -846,7 +859,7 @@ function registerAnnotationTool(server, options) {
846
859
  captured_at: (/* @__PURE__ */ new Date()).toISOString(),
847
860
  consent_token: options.consentToken,
848
861
  agent_runtime: runtime,
849
- user_id: userId,
862
+ principal_id: principalId,
850
863
  runtime_meta: scrubbedMeta,
851
864
  payload: {
852
865
  // Agent-facing names -> wire keys, as with `overall_task`
@@ -1084,16 +1097,16 @@ function sealKey(dsn) {
1084
1097
 
1085
1098
  // src/integrations/mcp/config.ts
1086
1099
  var INTENT_PARAM_MODES = /* @__PURE__ */ new Set(["optional", "required", "off"]);
1087
- var USER_ID_MODES = /* @__PURE__ */ new Set(["hashed", "raw"]);
1100
+ var PRINCIPAL_ID_MODES = /* @__PURE__ */ new Set(["hashed", "raw"]);
1088
1101
  function resolveTenantId(explicit, vendorId) {
1089
1102
  if (explicit) return explicit;
1090
1103
  const fromEnv = typeof process !== "undefined" ? process.env?.BATON_TENANT_ID : void 0;
1091
1104
  if (fromEnv) return fromEnv;
1092
1105
  return vendorId;
1093
1106
  }
1094
- function resolveUserIdHmacKey(explicit) {
1107
+ function resolvePrincipalIdHmacKey(explicit) {
1095
1108
  if (explicit !== void 0) return explicit;
1096
- const fromEnv = typeof process !== "undefined" ? process.env?.BATON_USER_ID_HMAC_KEY : void 0;
1109
+ const fromEnv = typeof process !== "undefined" ? process.env?.BATON_PRINCIPAL_ID_HMAC_KEY : void 0;
1097
1110
  return fromEnv ? fromEnv : void 0;
1098
1111
  }
1099
1112
  function resolveBatonConfig(config, serverName) {
@@ -1153,7 +1166,17 @@ function resolveBatonConfig(config, serverName) {
1153
1166
  sink: new HttpSink(dsn.origin, { apiKey: dsn.key })
1154
1167
  };
1155
1168
  }
1169
+ var RENAMED_KEYS = {
1170
+ resolveUser: "resolvePrincipal",
1171
+ userIdMode: "principalIdMode",
1172
+ userIdHmacKey: "principalIdHmacKey"
1173
+ };
1156
1174
  function validateBatonConfig(config) {
1175
+ for (const [was, now] of Object.entries(RENAMED_KEYS)) {
1176
+ if (was in config) {
1177
+ throw new Error(`BatonConfig.${was} was renamed to ${now} in 0.3.5.`);
1178
+ }
1179
+ }
1157
1180
  if (!config.vendorId) {
1158
1181
  throw new Error(
1159
1182
  "BatonConfig needs a vendorId \u2014 either directly, or via a dsn whose last path segment names the server (see /account)."
@@ -1184,15 +1207,15 @@ function validateBatonConfig(config) {
1184
1207
  `BatonConfig.intentParamMode ${JSON.stringify(config.intentParamMode)} must be one of ${JSON.stringify([...INTENT_PARAM_MODES].sort())}.`
1185
1208
  );
1186
1209
  }
1187
- if (config.resolveUser !== void 0 && typeof config.resolveUser !== "function") {
1188
- throw new Error("BatonConfig.resolveUser must be a function.");
1210
+ if (config.resolvePrincipal !== void 0 && typeof config.resolvePrincipal !== "function") {
1211
+ throw new Error("BatonConfig.resolvePrincipal must be a function.");
1189
1212
  }
1190
- if (config.userIdHmacKey !== void 0 && typeof config.userIdHmacKey !== "string" && !(config.userIdHmacKey instanceof Uint8Array)) {
1191
- throw new Error("BatonConfig.userIdHmacKey must be a string or a Uint8Array.");
1213
+ if (config.principalIdHmacKey !== void 0 && typeof config.principalIdHmacKey !== "string" && !(config.principalIdHmacKey instanceof Uint8Array)) {
1214
+ throw new Error("BatonConfig.principalIdHmacKey must be a string or a Uint8Array.");
1192
1215
  }
1193
- if (config.userIdMode !== void 0 && !USER_ID_MODES.has(config.userIdMode)) {
1216
+ if (config.principalIdMode !== void 0 && !PRINCIPAL_ID_MODES.has(config.principalIdMode)) {
1194
1217
  throw new Error(
1195
- `BatonConfig.userIdMode ${JSON.stringify(config.userIdMode)} must be one of ${JSON.stringify([...USER_ID_MODES].sort())}.`
1218
+ `BatonConfig.principalIdMode ${JSON.stringify(config.principalIdMode)} must be one of ${JSON.stringify([...PRINCIPAL_ID_MODES].sort())}.`
1196
1219
  );
1197
1220
  }
1198
1221
  }
@@ -1547,10 +1570,10 @@ function batonWrap(nameRef, original, ctx) {
1547
1570
  const scrubbedExpected = rawExpected !== null ? ctx.scrubber(rawExpected) : null;
1548
1571
  const scrubbedWorkflow = rawWorkflow !== null ? ctx.scrubber(rawWorkflow) : null;
1549
1572
  const callId = (0, import_uuid2.v7)();
1550
- const userId = await resolveCallUserId(
1551
- ctx.resolveUser,
1573
+ const principalId = await resolveCallPrincipalId(
1574
+ ctx.resolvePrincipal,
1552
1575
  { extra, toolName, arguments: params },
1553
- { mode: ctx.userIdMode, tenantId: ctx.tenantId, key: ctx.userIdHmacKey }
1576
+ { mode: ctx.principalIdMode, tenantId: ctx.tenantId, key: ctx.principalIdHmacKey }
1554
1577
  );
1555
1578
  const common = {
1556
1579
  tenant_id: ctx.tenantId,
@@ -1563,7 +1586,7 @@ function batonWrap(nameRef, original, ctx) {
1563
1586
  // (`middleware.py` 481/524/562/606). `surface_snapshot` is deliberately
1564
1587
  // NOT among them: it describes the SERVER and is captured outside any
1565
1588
  // call, so there is no caller to name (register D5).
1566
- user_id: userId,
1589
+ principal_id: principalId,
1567
1590
  runtime_meta: scrubbedMeta
1568
1591
  };
1569
1592
  if (scrubbedIntent !== null && ctx.tracker.claim(sessionId)) {
@@ -1870,9 +1893,9 @@ function withBaton(server, supplied = {}) {
1870
1893
  annotationToolName,
1871
1894
  intentParamMode,
1872
1895
  paramRegistry: /* @__PURE__ */ new Map(),
1873
- resolveUser: config.resolveUser,
1874
- userIdMode: config.userIdMode ?? "hashed",
1875
- userIdHmacKey: resolveUserIdHmacKey(config.userIdHmacKey),
1896
+ resolvePrincipal: config.resolvePrincipal,
1897
+ principalIdMode: config.principalIdMode ?? "hashed",
1898
+ principalIdHmacKey: resolvePrincipalIdHmacKey(config.principalIdHmacKey),
1876
1899
  vendorToolJsonSchema,
1877
1900
  bustSchemaMemo,
1878
1901
  tracker,
@@ -1893,14 +1916,14 @@ function withBaton(server, supplied = {}) {
1893
1916
  vendorDisplayName: config.vendorDisplayName,
1894
1917
  consentToken: ctx.consentToken,
1895
1918
  // Read off `ctx`, never re-resolved from `config`: the two paths must
1896
- // agree on the actor, and `userIdMode`/`userIdHmacKey` each have a
1919
+ // agree on the actor, and `principalIdMode`/`principalIdHmacKey` each have a
1897
1920
  // default and an env fallback that a second resolution could take
1898
1921
  // differently. Required (not optional) on the options type ON PURPOSE —
1899
1922
  // the compiler then refuses a registration that forgets them, which is
1900
1923
  // the mechanical version of "wiring two of four call sites".
1901
- resolveUser: ctx.resolveUser,
1902
- userIdMode: ctx.userIdMode,
1903
- userIdHmacKey: ctx.userIdHmacKey,
1924
+ resolvePrincipal: ctx.resolvePrincipal,
1925
+ principalIdMode: ctx.principalIdMode,
1926
+ principalIdHmacKey: ctx.principalIdHmacKey,
1904
1927
  fallbackSessionId: ctx.fallbackSessionId,
1905
1928
  scrubber: ctx.scrubber,
1906
1929
  // The RESOLVED name, never `config.annotationToolName`: handing over the
@@ -1950,7 +1973,7 @@ function withBaton(server, supplied = {}) {
1950
1973
  ToolCallStartEventSchema,
1951
1974
  ToolCallStartPayloadSchema,
1952
1975
  VENDOR_HASH_SCHEME,
1953
- hashUserId,
1976
+ hashPrincipalId,
1954
1977
  identityScrub,
1955
1978
  safeWrite,
1956
1979
  withBaton