@bitfab/sdk 0.18.1 → 0.19.0

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
@@ -335,6 +335,14 @@ declare class ReplayEnvironment {
335
335
  get active(): boolean;
336
336
  /** Non-throwing variant for callers that handle the inactive case. */
337
337
  snapshot(): ReplayEnvironmentSnapshot | null;
338
+ /**
339
+ * Record on the replay context that customer code obtained the branch
340
+ * URL. Only `databaseUrl` and `snapshot()` count — `active`, `readOnly`
341
+ * and friends inspect the lease without exposing the connection string,
342
+ * so they don't prove the replayed code could have connected to the
343
+ * branch.
344
+ */
345
+ private markAccessed;
338
346
  private read;
339
347
  private require;
340
348
  }
@@ -1055,7 +1063,7 @@ declare class BitfabFunction {
1055
1063
  /**
1056
1064
  * SDK version from package.json (injected at build time)
1057
1065
  */
1058
- declare const __version__ = "0.18.1";
1066
+ declare const __version__ = "0.19.0";
1059
1067
 
1060
1068
  /**
1061
1069
  * Constants for the Bitfab SDK.
package/dist/index.d.ts CHANGED
@@ -335,6 +335,14 @@ declare class ReplayEnvironment {
335
335
  get active(): boolean;
336
336
  /** Non-throwing variant for callers that handle the inactive case. */
337
337
  snapshot(): ReplayEnvironmentSnapshot | null;
338
+ /**
339
+ * Record on the replay context that customer code obtained the branch
340
+ * URL. Only `databaseUrl` and `snapshot()` count — `active`, `readOnly`
341
+ * and friends inspect the lease without exposing the connection string,
342
+ * so they don't prove the replayed code could have connected to the
343
+ * branch.
344
+ */
345
+ private markAccessed;
338
346
  private read;
339
347
  private require;
340
348
  }
@@ -1055,7 +1063,7 @@ declare class BitfabFunction {
1055
1063
  /**
1056
1064
  * SDK version from package.json (injected at build time)
1057
1065
  */
1058
- declare const __version__ = "0.18.1";
1066
+ declare const __version__ = "0.19.0";
1059
1067
 
1060
1068
  /**
1061
1069
  * Constants for the Bitfab SDK.
package/dist/index.js CHANGED
@@ -11,10 +11,10 @@ import {
11
11
  flushTraces,
12
12
  getCurrentSpan,
13
13
  getCurrentTrace
14
- } from "./chunk-ILIUTS5D.js";
14
+ } from "./chunk-FA6DBCAT.js";
15
15
  import {
16
16
  BitfabError
17
- } from "./chunk-QT7HWOKU.js";
17
+ } from "./chunk-EQI6ZJC3.js";
18
18
  export {
19
19
  Bitfab,
20
20
  BitfabClaudeAgentHandler,
package/dist/node.cjs CHANGED
@@ -91,28 +91,6 @@ var init_errors = __esm({
91
91
  }
92
92
  });
93
93
 
94
- // src/replayContext.ts
95
- function getReplayContext() {
96
- return replayContextStorage?.getStore() ?? null;
97
- }
98
- function runWithReplayContext(ctx, fn) {
99
- if (replayContextStorage) {
100
- return replayContextStorage.run(ctx, fn);
101
- }
102
- return fn();
103
- }
104
- var replayContextStorage, replayContextReady;
105
- var init_replayContext = __esm({
106
- "src/replayContext.ts"() {
107
- "use strict";
108
- init_asyncStorage();
109
- replayContextStorage = null;
110
- replayContextReady = asyncStorageReady.then(() => {
111
- replayContextStorage = createAsyncLocalStorage();
112
- });
113
- }
114
- });
115
-
116
94
  // src/serialize.ts
117
95
  function describeValue(value) {
118
96
  try {
@@ -163,12 +141,89 @@ function deserializeValue(serialized) {
163
141
  meta: serialized.meta
164
142
  });
165
143
  }
166
- var import_superjson, MAX_SERIALIZED_BYTES;
144
+ function toJsonSafe(value) {
145
+ return toJsonSafeInner(value, 0, /* @__PURE__ */ new WeakSet());
146
+ }
147
+ function toJsonSafeInner(value, depth, seen) {
148
+ if (value === null || value === void 0) {
149
+ return value;
150
+ }
151
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
152
+ return value;
153
+ }
154
+ const className = value?.constructor?.name ?? typeof value;
155
+ if (depth > MAX_SAFE_DEPTH) {
156
+ return `<${className}>`;
157
+ }
158
+ if (typeof value !== "object") {
159
+ try {
160
+ return String(value);
161
+ } catch {
162
+ return `<${className}>`;
163
+ }
164
+ }
165
+ if (seen.has(value)) {
166
+ return `<cycle ${className}>`;
167
+ }
168
+ seen.add(value);
169
+ let result;
170
+ if (Array.isArray(value)) {
171
+ result = value.map((item) => toJsonSafeInner(item, depth + 1, seen));
172
+ } else if (typeof value.toJSON === "function") {
173
+ try {
174
+ result = toJsonSafeInner(
175
+ value.toJSON(),
176
+ depth + 1,
177
+ seen
178
+ );
179
+ } catch {
180
+ result = `<${className}>`;
181
+ }
182
+ } else {
183
+ try {
184
+ const obj = {};
185
+ for (const [k, v] of Object.entries(value)) {
186
+ if (!k.startsWith("_")) {
187
+ obj[k] = toJsonSafeInner(v, depth + 1, seen);
188
+ }
189
+ }
190
+ result = obj;
191
+ } catch {
192
+ result = `<${className}>`;
193
+ }
194
+ }
195
+ seen.delete(value);
196
+ return result;
197
+ }
198
+ var import_superjson, MAX_SERIALIZED_BYTES, MAX_SAFE_DEPTH;
167
199
  var init_serialize = __esm({
168
200
  "src/serialize.ts"() {
169
201
  "use strict";
170
202
  import_superjson = __toESM(require("superjson"), 1);
171
203
  MAX_SERIALIZED_BYTES = 512e3;
204
+ MAX_SAFE_DEPTH = 6;
205
+ }
206
+ });
207
+
208
+ // src/replayContext.ts
209
+ function getReplayContext() {
210
+ return replayContextStorage?.getStore() ?? null;
211
+ }
212
+ function runWithReplayContext(ctx, fn) {
213
+ if (replayContextStorage) {
214
+ return replayContextStorage.run(ctx, fn);
215
+ }
216
+ return fn();
217
+ }
218
+ var replayContextStorage, replayContextReady;
219
+ var init_replayContext = __esm({
220
+ "src/replayContext.ts"() {
221
+ "use strict";
222
+ init_asyncStorage();
223
+ replayContextStorage = null;
224
+ replayContextReady = asyncStorageReady.then(() => {
225
+ replayContextStorage = createAsyncLocalStorage();
226
+ });
172
227
  }
173
228
  });
174
229
 
@@ -447,13 +502,93 @@ registerAsyncLocalStorageClass(
447
502
  );
448
503
 
449
504
  // src/version.generated.ts
450
- var __version__ = "0.18.1";
505
+ var __version__ = "0.19.0";
451
506
 
452
507
  // src/constants.ts
453
508
  var DEFAULT_SERVICE_URL = "https://bitfab.ai";
454
509
 
455
510
  // src/http.ts
456
511
  init_errors();
512
+ function serializePayloadBody(payload) {
513
+ try {
514
+ return { body: JSON.stringify(payload), dropped: [] };
515
+ } catch {
516
+ const dropped = [];
517
+ const sanitize = (value, seen) => {
518
+ const t = typeof value;
519
+ if (value === null || t === "string" || t === "number" || t === "boolean") {
520
+ return value;
521
+ }
522
+ if (t === "bigint") {
523
+ dropped.push("BigInt");
524
+ return "<unserializable: BigInt>";
525
+ }
526
+ if (t === "function") {
527
+ const name = value.name || "Function";
528
+ dropped.push(name);
529
+ return `<unserializable: ${name}>`;
530
+ }
531
+ if (t === "symbol") {
532
+ dropped.push("Symbol");
533
+ return "<unserializable: Symbol>";
534
+ }
535
+ if (t !== "object") {
536
+ return void 0;
537
+ }
538
+ const obj = value;
539
+ const className = obj.constructor?.name || "object";
540
+ if (seen.has(obj)) {
541
+ dropped.push(className);
542
+ return `<cycle: ${className}>`;
543
+ }
544
+ seen.add(obj);
545
+ let result;
546
+ if (Array.isArray(obj)) {
547
+ result = obj.map((item) => sanitize(item, seen));
548
+ } else if (typeof obj.toJSON === "function") {
549
+ try {
550
+ result = sanitize(obj.toJSON(), seen);
551
+ } catch {
552
+ dropped.push(className);
553
+ result = `<unserializable: ${className}>`;
554
+ }
555
+ } else {
556
+ const out = {};
557
+ for (const [k, v] of Object.entries(obj)) {
558
+ out[k] = sanitize(v, seen);
559
+ }
560
+ result = out;
561
+ }
562
+ seen.delete(obj);
563
+ return result;
564
+ };
565
+ let sanitized;
566
+ try {
567
+ sanitized = sanitize(payload, /* @__PURE__ */ new WeakSet());
568
+ } catch (error) {
569
+ const message = error instanceof Error ? error.message : String(error);
570
+ return {
571
+ body: JSON.stringify({ error: `payload_serialize_failed: ${message}` }),
572
+ dropped
573
+ };
574
+ }
575
+ if (dropped.length > 0 && typeof sanitized === "object" && sanitized !== null && !Array.isArray(sanitized)) {
576
+ const obj = sanitized;
577
+ const existing = Array.isArray(obj.errors) ? obj.errors : [];
578
+ obj.errors = [
579
+ ...existing,
580
+ {
581
+ source: "sdk",
582
+ step: "json_serialize",
583
+ error: `stubbed non-serializable value(s): ${[
584
+ ...new Set(dropped)
585
+ ].join(", ")}`
586
+ }
587
+ ];
588
+ }
589
+ return { body: JSON.stringify(sanitized), dropped };
590
+ }
591
+ }
457
592
  var pendingTracePromises = /* @__PURE__ */ new Set();
458
593
  function awaitOnExit(promise) {
459
594
  pendingTracePromises.add(promise);
@@ -512,23 +647,14 @@ var HttpClient = class {
512
647
  const method = options?.method ?? "POST";
513
648
  const controller = new AbortController();
514
649
  const timeoutId = setTimeout(() => controller.abort(), timeout);
515
- let body;
516
- let serializationError;
517
- try {
518
- body = JSON.stringify(payload);
519
- } catch (error) {
520
- serializationError = error instanceof Error ? error.message : String(error);
521
- body = JSON.stringify({
522
- ...Object.fromEntries(
523
- Object.entries(payload).filter(
524
- ([, v]) => typeof v === "string" || typeof v === "number"
525
- )
526
- ),
527
- rawSpan: {},
528
- errors: [
529
- { source: "sdk", step: "json_serialize", error: serializationError }
530
- ]
531
- });
650
+ const { body, dropped } = serializePayloadBody(payload);
651
+ if (dropped.length > 0) {
652
+ try {
653
+ console.warn(
654
+ `Bitfab: request body to ${endpoint} held ${dropped.length} non-serializable value(s) (${[...new Set(dropped)].join(", ")}); they were stubbed so the span still sends, but the trace may be incomplete or not replayable. Capture a JSON-safe projection of this input to make it replayable.`
655
+ );
656
+ } catch {
657
+ }
532
658
  }
533
659
  try {
534
660
  const response = await fetch(url, {
@@ -786,33 +912,11 @@ var HttpClient = class {
786
912
  };
787
913
 
788
914
  // src/claudeAgentSdk.ts
915
+ init_serialize();
789
916
  function nowIso() {
790
917
  return (/* @__PURE__ */ new Date()).toISOString();
791
918
  }
792
- function safeSerialize(value) {
793
- if (value === null || value === void 0) {
794
- return value;
795
- }
796
- if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
797
- return value;
798
- }
799
- if (Array.isArray(value)) {
800
- return value.map(safeSerialize);
801
- }
802
- if (typeof value === "object") {
803
- if (typeof value.toJSON === "function") {
804
- return value.toJSON();
805
- }
806
- const result = {};
807
- for (const [k, v] of Object.entries(value)) {
808
- if (!k.startsWith("_")) {
809
- result[k] = safeSerialize(v);
810
- }
811
- }
812
- return result;
813
- }
814
- return String(value);
815
- }
919
+ var safeSerialize = toJsonSafe;
816
920
  function extractContentBlocks(content) {
817
921
  if (!Array.isArray(content)) {
818
922
  return [];
@@ -1568,6 +1672,7 @@ function buildSnapshotRef(config, sdkWallClockBeforeFn) {
1568
1672
  }
1569
1673
 
1570
1674
  // src/langgraph.ts
1675
+ init_serialize();
1571
1676
  var LANGSMITH_HIDDEN_TAG = "langsmith:hidden";
1572
1677
  var LANGGRAPH_METADATA_KEYS = [
1573
1678
  "langgraph_step",
@@ -1579,56 +1684,7 @@ var LANGGRAPH_METADATA_KEYS = [
1579
1684
  function nowIso2() {
1580
1685
  return (/* @__PURE__ */ new Date()).toISOString();
1581
1686
  }
1582
- var MAX_SERIALIZE_DEPTH = 6;
1583
- function safeSerialize2(value) {
1584
- return safeSerializeInner(value, 0, /* @__PURE__ */ new WeakSet());
1585
- }
1586
- function safeSerializeInner(value, depth, seen) {
1587
- if (value === null || value === void 0) {
1588
- return value;
1589
- }
1590
- if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
1591
- return value;
1592
- }
1593
- const className = value?.constructor?.name ?? typeof value;
1594
- if (depth > MAX_SERIALIZE_DEPTH) {
1595
- return `<${className}>`;
1596
- }
1597
- if (typeof value === "object") {
1598
- if (seen.has(value)) {
1599
- return `<cycle ${className}>`;
1600
- }
1601
- seen.add(value);
1602
- }
1603
- if (Array.isArray(value)) {
1604
- return value.map((item) => safeSerializeInner(item, depth + 1, seen));
1605
- }
1606
- if (typeof value === "object") {
1607
- if (typeof value.toJSON === "function") {
1608
- try {
1609
- return value.toJSON();
1610
- } catch {
1611
- return `<${className}>`;
1612
- }
1613
- }
1614
- try {
1615
- const result = {};
1616
- for (const [k, v] of Object.entries(value)) {
1617
- if (!k.startsWith("_")) {
1618
- result[k] = safeSerializeInner(v, depth + 1, seen);
1619
- }
1620
- }
1621
- return result;
1622
- } catch {
1623
- return `<${className}>`;
1624
- }
1625
- }
1626
- try {
1627
- return String(value);
1628
- } catch {
1629
- return `<${className}>`;
1630
- }
1631
- }
1687
+ var safeSerialize2 = toJsonSafe;
1632
1688
  function convertMessage(message) {
1633
1689
  if (typeof message !== "object" || message === null) {
1634
1690
  return { role: "unknown", content: String(message) };
@@ -2162,7 +2218,9 @@ var ReplayEnvironment = class {
2162
2218
  * Throws if read outside a replay item.
2163
2219
  */
2164
2220
  get databaseUrl() {
2165
- return this.require().databaseUrl;
2221
+ const snapshot = this.require();
2222
+ this.markAccessed();
2223
+ return snapshot.databaseUrl;
2166
2224
  }
2167
2225
  /** When the per-trace branch URL stops being valid. ISO-8601. */
2168
2226
  get expiresAt() {
@@ -2190,7 +2248,24 @@ var ReplayEnvironment = class {
2190
2248
  }
2191
2249
  /** Non-throwing variant for callers that handle the inactive case. */
2192
2250
  snapshot() {
2193
- return this.read();
2251
+ const snapshot = this.read();
2252
+ if (snapshot) {
2253
+ this.markAccessed();
2254
+ }
2255
+ return snapshot;
2256
+ }
2257
+ /**
2258
+ * Record on the replay context that customer code obtained the branch
2259
+ * URL. Only `databaseUrl` and `snapshot()` count — `active`, `readOnly`
2260
+ * and friends inspect the lease without exposing the connection string,
2261
+ * so they don't prove the replayed code could have connected to the
2262
+ * branch.
2263
+ */
2264
+ markAccessed() {
2265
+ const ctx = getReplayContext();
2266
+ if (ctx?.dbBranchLease) {
2267
+ ctx.dbSnapshotAccessed = true;
2268
+ }
2194
2269
  }
2195
2270
  read() {
2196
2271
  const ctx = getReplayContext();
@@ -3122,7 +3197,19 @@ var Bitfab = class {
3122
3197
  contexts: traceState?.contexts ?? [],
3123
3198
  testRunId: traceState?.testRunId,
3124
3199
  inputSourceTraceId: traceState?.inputSourceTraceId,
3125
- dbSnapshotRef: traceState?.dbSnapshotRef
3200
+ dbSnapshotRef: traceState?.dbSnapshotRef,
3201
+ // Built AFTER the wrapped fn finished, so `accessed` reflects
3202
+ // whether customer code obtained the branch URL during this
3203
+ // item. Omitted entirely when no lease was attached, so the
3204
+ // server can distinguish "no branch" from "branch ignored".
3205
+ ...replayCtx?.dbBranchLease && {
3206
+ dbSnapshotUsage: {
3207
+ neonBranchId: replayCtx.dbBranchLease.neonBranchId,
3208
+ snapshotTimestamp: replayCtx.dbBranchLease.snapshotTimestamp,
3209
+ sourceTraceId: replayCtx.sourceBitfabTraceId,
3210
+ accessed: replayCtx.dbSnapshotAccessed === true
3211
+ }
3212
+ }
3126
3213
  });
3127
3214
  activeTraceStates.delete(traceId);
3128
3215
  if (persistenceCollector) {
@@ -3294,6 +3381,18 @@ var Bitfab = class {
3294
3381
  if (params.dbSnapshotRef) {
3295
3382
  rawTrace.db_snapshot_ref = params.dbSnapshotRef;
3296
3383
  }
3384
+ if (params.dbSnapshotUsage) {
3385
+ rawTrace.db_snapshot_usage = {
3386
+ neon_branch_id: params.dbSnapshotUsage.neonBranchId,
3387
+ ...params.dbSnapshotUsage.snapshotTimestamp && {
3388
+ snapshot_timestamp: params.dbSnapshotUsage.snapshotTimestamp
3389
+ },
3390
+ ...params.dbSnapshotUsage.sourceTraceId && {
3391
+ source_trace_id: params.dbSnapshotUsage.sourceTraceId
3392
+ },
3393
+ accessed: params.dbSnapshotUsage.accessed
3394
+ };
3395
+ }
3297
3396
  return this.httpClient.sendExternalTrace({
3298
3397
  type: "sdk-function",
3299
3398
  source: "typescript-sdk-function",