@narumitw/pi-otel 0.0.0-stage → 0.55.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.
Files changed (57) hide show
  1. package/CHANGELOG.md +170 -0
  2. package/LICENSE +21 -0
  3. package/README.md +371 -2
  4. package/dist/chunks/chunk-IXR5MVGN.ts +156 -0
  5. package/dist/chunks/chunk-IXR5MVGN.ts.map +7 -0
  6. package/dist/chunks/runtime-KWIFBNQ2.ts +404 -0
  7. package/dist/chunks/runtime-KWIFBNQ2.ts.map +7 -0
  8. package/dist/config.d.ts +36 -0
  9. package/dist/durable-projection.d.ts +9 -0
  10. package/dist/durable-recorder.d.ts +57 -0
  11. package/dist/durable-state.d.ts +43 -0
  12. package/dist/durable.d.ts +25 -0
  13. package/dist/durable.js +897 -0
  14. package/dist/durable.js.map +7 -0
  15. package/dist/git.d.ts +5 -0
  16. package/dist/index.d.ts +2 -0
  17. package/dist/index.js +1736 -0
  18. package/dist/index.js.map +7 -0
  19. package/dist/index.ts +1878 -0
  20. package/dist/index.ts.map +7 -0
  21. package/dist/library-chunks/chunk-BBL32ZV5.js +84 -0
  22. package/dist/library-chunks/chunk-BBL32ZV5.js.map +7 -0
  23. package/dist/library-chunks/chunk-ROM6FIM7.js +78 -0
  24. package/dist/library-chunks/chunk-ROM6FIM7.js.map +7 -0
  25. package/dist/library-chunks/chunk-ZCEZBO2T.js +158 -0
  26. package/dist/library-chunks/chunk-ZCEZBO2T.js.map +7 -0
  27. package/dist/library-chunks/runtime-7TSAKORH.js +406 -0
  28. package/dist/library-chunks/runtime-7TSAKORH.js.map +7 -0
  29. package/dist/otel.d.ts +13 -0
  30. package/dist/otlp-settings.d.ts +9 -0
  31. package/dist/pi-session.d.ts +43 -0
  32. package/dist/public-api.d.ts +2 -0
  33. package/dist/runtime-api.d.ts +6 -0
  34. package/dist/runtime-core.d.ts +26 -0
  35. package/dist/runtime-defaults.d.ts +1 -0
  36. package/dist/runtime.d.ts +26 -0
  37. package/dist/sanitizer.d.ts +2 -0
  38. package/dist/tracing.d.ts +199 -0
  39. package/docs/durable.md +128 -0
  40. package/package.json +87 -4
  41. package/src/config.ts +330 -0
  42. package/src/durable-projection.ts +112 -0
  43. package/src/durable-recorder.ts +539 -0
  44. package/src/durable-state.ts +135 -0
  45. package/src/durable.ts +364 -0
  46. package/src/git.ts +64 -0
  47. package/src/index.ts +12 -0
  48. package/src/otel.ts +397 -0
  49. package/src/otlp-settings.ts +80 -0
  50. package/src/pi-session.ts +570 -0
  51. package/src/public-api.ts +13 -0
  52. package/src/runtime-api.ts +16 -0
  53. package/src/runtime-core.ts +114 -0
  54. package/src/runtime-defaults.ts +1 -0
  55. package/src/runtime.ts +509 -0
  56. package/src/sanitizer.ts +157 -0
  57. package/src/tracing.ts +946 -0
@@ -0,0 +1,897 @@
1
+ // @generated by scripts/build-runtime.mjs; do not edit.
2
+ // @ts-nocheck -- the generated entry uses a .ts extension for Pi's Jiti loader.
3
+ import {
4
+ createLangfuseRuntime,
5
+ createOtelRuntime,
6
+ sanitizeTraceValue
7
+ } from "./library-chunks/chunk-ZCEZBO2T.js";
8
+ import {
9
+ createLangfuseRuntimeFromBackend,
10
+ getLangfuseRuntimeInternal
11
+ } from "./library-chunks/chunk-BBL32ZV5.js";
12
+
13
+ // src/durable.ts
14
+ import { awaitWithContext, withCancel } from "@earendil-works/chord/context";
15
+
16
+ // src/durable-projection.ts
17
+ var MAX_PROJECTED_ITEMS = 199;
18
+ function durableAttributes(attributes) {
19
+ const copy = { ...attributes };
20
+ if (attributes.metadata) {
21
+ const value = sanitizeTraceValue(attributes.metadata, true);
22
+ copy.metadata = value && typeof value === "object" && !Array.isArray(value) ? value : { "pi.durable.metadata_omitted": true };
23
+ }
24
+ for (const key of ["name", "model", "sessionId", "userId", "statusMessage", "version"]) {
25
+ if (typeof copy[key] === "string") copy[key] = String(sanitizeTraceValue(copy[key], true));
26
+ }
27
+ return copy;
28
+ }
29
+ function durableDiagnosticCount(entry) {
30
+ const data = entry?.data;
31
+ return data && typeof data === "object" && !Array.isArray(data) && "diagnostics" in data && Array.isArray(data.diagnostics) ? data.diagnostics.length : 0;
32
+ }
33
+ function durableContent(messages, capture, diagnosticCount = 0) {
34
+ if (!capture) return "[content capture disabled]";
35
+ const projected = messages?.slice(0, MAX_PROJECTED_ITEMS).map((message) => {
36
+ if (message.role === "toolResult" && message.isError)
37
+ return { role: message.role, content: "[tool error content omitted]" };
38
+ if (message.role === "assistant" && message.stopReason === "error")
39
+ return { role: message.role, content: "[assistant error content omitted]" };
40
+ const omitDiagnosticBlock = message.role === "toolResult" && diagnosticCount > 0 && message.content.at(-1)?.type === "text";
41
+ const blockCount = message.content.length - (omitDiagnosticBlock ? 1 : 0);
42
+ return {
43
+ role: message.role,
44
+ content: typeof message.content === "string" ? message.content : message.content.slice(0, Math.min(MAX_PROJECTED_ITEMS, blockCount)).map((block) => {
45
+ switch (block.type) {
46
+ case "text":
47
+ return { type: "text", text: block.text };
48
+ case "thinking":
49
+ return { type: "thinking", thinking: block.thinking };
50
+ case "toolCall":
51
+ return { type: "toolCall", id: block.id, name: block.name, arguments: block.arguments };
52
+ case "image":
53
+ return { type: "image", mimeType: block.mimeType, data: "[base64 omitted]" };
54
+ default:
55
+ return { type: "unsupported", content: "[block omitted]" };
56
+ }
57
+ }),
58
+ ...Array.isArray(message.content) && blockCount > MAX_PROJECTED_ITEMS ? { blocksOmitted: blockCount - MAX_PROJECTED_ITEMS } : {},
59
+ ...omitDiagnosticBlock ? { diagnosticsOmitted: diagnosticCount } : {}
60
+ };
61
+ });
62
+ if (messages && messages.length > MAX_PROJECTED_ITEMS)
63
+ projected?.push({ messagesOmitted: messages.length - MAX_PROJECTED_ITEMS });
64
+ return sanitizeTraceValue(projected, true);
65
+ }
66
+ function durableUsage(message) {
67
+ if (message.role !== "assistant" && message.role !== "toolResult" || !message.usage) return {};
68
+ const usageDetails = {};
69
+ const costDetails = {};
70
+ for (const [key, field] of [
71
+ ["input", "input"],
72
+ ["output", "output"],
73
+ ["cacheRead", "cache_read_input_tokens"],
74
+ ["cacheWrite", "cache_creation_input_tokens"],
75
+ ["totalTokens", "total"]
76
+ ]) {
77
+ const value = message.usage[key];
78
+ if (typeof value === "number" && Number.isFinite(value) && value >= 0) usageDetails[field] = value;
79
+ }
80
+ for (const [key, field] of [
81
+ ["input", "input"],
82
+ ["output", "output"],
83
+ ["cacheRead", "cache_read"],
84
+ ["cacheWrite", "cache_write"],
85
+ ["total", "total"]
86
+ ]) {
87
+ const value = message.usage.cost[key];
88
+ if (typeof value === "number" && Number.isFinite(value) && value >= 0) costDetails[field] = value;
89
+ }
90
+ return {
91
+ usageDetails,
92
+ costDetails,
93
+ ...typeof message.usage.cost.totalFromProvider === "boolean" ? { metadata: { "pi.usage.cost.total_from_provider": message.usage.cost.totalFromProvider } } : {}
94
+ };
95
+ }
96
+
97
+ // src/durable-recorder.ts
98
+ var SAFE_UNANSWERED_REASONS = /* @__PURE__ */ new Set([
99
+ "aborted",
100
+ "model_error",
101
+ "no_model",
102
+ "reset",
103
+ "stale",
104
+ "faulted",
105
+ "missing_task",
106
+ "task_too_old",
107
+ "migration_failed",
108
+ "orphaned",
109
+ "interrupted",
110
+ "restart_limit",
111
+ "nothing_to_answer",
112
+ "turn_limit",
113
+ "run_failed"
114
+ ]);
115
+ var DurableRecorder = class {
116
+ constructor(backend, options) {
117
+ this.backend = backend;
118
+ this.options = options;
119
+ this.root = this.start(options.traceName ?? "pi.durable.conversation", "agent", void 0, {
120
+ metadata: {
121
+ ...this.customMetadata(),
122
+ "pi.runtime": "durable",
123
+ "pi.durable.schema_version": "1",
124
+ "pi.durable.conversation_id": options.conversationId,
125
+ ...options.applicationId ? { "pi.application.id": options.applicationId } : {}
126
+ }
127
+ });
128
+ this.safe(() => {
129
+ this.root.observation?.updateTrace?.(
130
+ durableAttributes({
131
+ name: options.traceName ?? "pi.durable.conversation",
132
+ sessionId: options.sessionId ?? String(options.conversationId),
133
+ ...options.userId ? { userId: options.userId } : {},
134
+ metadata: this.customMetadata(),
135
+ version: "durable-1"
136
+ })
137
+ );
138
+ });
139
+ const traceId = this.root.observation?.traceId;
140
+ if (traceId) this.callback("trace-id callback", () => options.onTraceId?.(traceId));
141
+ }
142
+ backend;
143
+ options;
144
+ root;
145
+ run;
146
+ submissions = /* @__PURE__ */ new Map();
147
+ pendingSubmissions = /* @__PURE__ */ new Set();
148
+ entries = /* @__PURE__ */ new Set();
149
+ completedRuns = /* @__PURE__ */ new Set();
150
+ completedGenerations = /* @__PURE__ */ new Set();
151
+ live = { tools: [] };
152
+ baselineEntry = 0;
153
+ disposed = false;
154
+ gaps = 0;
155
+ get submissionIds() {
156
+ return [...this.pendingSubmissions];
157
+ }
158
+ report(stage) {
159
+ this.callback("error callback", () => this.options.onError?.(stage), false);
160
+ }
161
+ callback(stage, action, report = true) {
162
+ try {
163
+ Promise.resolve(action()).catch(() => {
164
+ if (report) this.report(stage);
165
+ });
166
+ } catch {
167
+ if (report) this.report(stage);
168
+ }
169
+ }
170
+ customMetadata() {
171
+ const value = sanitizeTraceValue(this.options.metadata, true);
172
+ if (!value || typeof value !== "object" || Array.isArray(value)) return {};
173
+ return Object.fromEntries(Object.entries(value).filter(([key]) => !key.startsWith("pi.")));
174
+ }
175
+ safe(action) {
176
+ try {
177
+ action();
178
+ } catch {
179
+ this.report("exporter observation");
180
+ }
181
+ }
182
+ start(name, type, parent, attributes = {}) {
183
+ if (this.disposed || parent && (parent.ended || !parent.observation)) return { ended: true };
184
+ const span = { ended: false };
185
+ const inherited = parent === this.run?.span ? this.correlationAttributes(this.run?.correlation ?? {}) : {};
186
+ this.safe(() => {
187
+ span.observation = this.backend.start(
188
+ String(sanitizeTraceValue(name, true)),
189
+ durableAttributes({
190
+ version: "durable-1",
191
+ sessionId: this.options.sessionId ?? String(this.options.conversationId),
192
+ ...this.options.userId ? { userId: this.options.userId } : {},
193
+ ...inherited,
194
+ ...attributes,
195
+ metadata: { ...inherited.metadata, ...attributes.metadata }
196
+ }),
197
+ { asType: type, parent: parent?.observation }
198
+ );
199
+ });
200
+ if (!span.observation) span.ended = true;
201
+ if (this.disposed) this.interrupted(span, "Observer disposed during exporter start.");
202
+ return span;
203
+ }
204
+ update(span, attributes) {
205
+ if (!span.ended)
206
+ this.safe(() => {
207
+ span.observation?.update(durableAttributes(attributes));
208
+ });
209
+ }
210
+ end(span, attributes = {}) {
211
+ if (!span || span.ended) return;
212
+ span.ended = true;
213
+ const observation = span.observation;
214
+ span.observation = void 0;
215
+ this.safe(() => {
216
+ observation?.update(durableAttributes(attributes));
217
+ });
218
+ this.safe(() => {
219
+ observation?.end();
220
+ });
221
+ }
222
+ interrupted(span, reason) {
223
+ this.end(span, {
224
+ level: "WARNING",
225
+ statusMessage: reason,
226
+ metadata: { "pi.durable.outcome": "observation_interrupted" }
227
+ });
228
+ }
229
+ correlationAttributes(correlation) {
230
+ return {
231
+ metadata: {
232
+ ...correlation.userId ? { "pi.user.id": correlation.userId } : {},
233
+ ...correlation.requestId ? { "pi.request.id": correlation.requestId } : {},
234
+ ...correlation.applicationId ? { "pi.application.id": correlation.applicationId } : {}
235
+ }
236
+ };
237
+ }
238
+ async submission(record, readEntry, correlation, snapshot = false) {
239
+ if (this.disposed || record.conversationId !== this.options.conversationId) return;
240
+ let state = this.submissions.get(record.id);
241
+ if (!state) {
242
+ let host = {};
243
+ try {
244
+ host = this.options.submissionCorrelation?.(record) ?? {};
245
+ } catch {
246
+ this.report("submission correlation");
247
+ }
248
+ if (this.disposed) return;
249
+ const ids = sanitizeTraceValue(
250
+ {
251
+ requestId: record.requestId,
252
+ userId: this.options.userId,
253
+ applicationId: this.options.applicationId,
254
+ ...host,
255
+ ...correlation
256
+ },
257
+ true
258
+ );
259
+ state = {
260
+ span: this.start("pi.durable.submission", "span", this.root, {
261
+ ...this.correlationAttributes(ids),
262
+ metadata: {
263
+ ...this.correlationAttributes(ids).metadata,
264
+ "pi.durable.submission_id": record.id,
265
+ "pi.durable.submission_type": record.type,
266
+ "pi.durable.snapshot_observation": snapshot
267
+ }
268
+ }),
269
+ correlation: ids,
270
+ terminal: false
271
+ };
272
+ if (this.disposed) return;
273
+ this.submissions.set(record.id, state);
274
+ this.pendingSubmissions.add(record.id);
275
+ }
276
+ if (state.terminal) return;
277
+ if (correlation) {
278
+ state.correlation = sanitizeTraceValue({ ...state.correlation, ...correlation }, true);
279
+ this.update(state.span, this.correlationAttributes(state.correlation));
280
+ }
281
+ const run = this.run;
282
+ if (run?.primary === record.id) {
283
+ run.correlation = state.correlation;
284
+ const attributes = this.correlationAttributes(state.correlation);
285
+ this.update(run.span, attributes);
286
+ if (run.generation) this.update(run.generation, attributes);
287
+ for (const tool of run.tools.values()) this.update(tool, attributes);
288
+ }
289
+ this.update(state.span, { metadata: { "pi.durable.submission_status": record.status } });
290
+ if (record.entry !== void 0 && record.type === "input") {
291
+ const entry = await readEntry(record.entry);
292
+ if (this.disposed || state.terminal) return;
293
+ this.update(state.span, { input: durableContent(entry?.model, this.options.captureContent ?? true) });
294
+ }
295
+ if (record.status === "done" || record.status === "unanswered") {
296
+ let output;
297
+ if (record.status === "done" && record.type !== "write") {
298
+ const entry = await readEntry(record.answer);
299
+ if (this.disposed || state.terminal) return;
300
+ output = durableContent(entry?.model, this.options.captureContent ?? true);
301
+ }
302
+ const reason = record.status === "unanswered" && SAFE_UNANSWERED_REASONS.has(record.reason) ? record.reason : void 0;
303
+ state.terminal = true;
304
+ this.pendingSubmissions.delete(record.id);
305
+ this.end(state.span, {
306
+ ...output !== void 0 ? { output } : {},
307
+ level: record.status === "done" ? "DEFAULT" : reason === "model_error" || reason === "no_model" || reason === "faulted" ? "ERROR" : "WARNING",
308
+ // Only known structural reason codes are safe; arbitrary reason/detail can contain provider secrets.
309
+ metadata: {
310
+ "pi.durable.submission_status": record.status,
311
+ "pi.durable.answer_entry_id": record.answer,
312
+ ...record.status === "unanswered" ? { "pi.durable.unanswered_reason": reason ?? "unknown" } : {}
313
+ }
314
+ });
315
+ }
316
+ }
317
+ snapshot(snapshot, initial = false) {
318
+ if (this.disposed) return;
319
+ for (const entry of snapshot.entries) this.baselineEntry = Math.max(this.baselineEntry, entry.id);
320
+ if (!initial) {
321
+ this.gaps += 1;
322
+ this.update(this.root, {
323
+ metadata: { "pi.durable.resync_count": this.gaps, "pi.durable.history_complete": false }
324
+ });
325
+ } else
326
+ this.update(this.root, {
327
+ metadata: { "pi.durable.history_complete": false, "pi.durable.attachment": "snapshot" }
328
+ });
329
+ if (this.run && !this.sameRun(snapshot.live.run))
330
+ this.closeRun("Run boundary not observed; snapshot replaced the observation.");
331
+ if (this.run) this.closeRound("Snapshot replaced generation/tool observation; activity may have been missed.");
332
+ this.live = snapshot.live;
333
+ if (snapshot.live.run) {
334
+ this.beginRun(snapshot.live.run, true);
335
+ if (this.run) this.update(this.run.span, { metadata: { "pi.durable.observation_incomplete": true } });
336
+ }
337
+ }
338
+ sameRun(run) {
339
+ if (!run || !this.run) return false;
340
+ return this.run.primary !== void 0 && run.inputs[0] === this.run.primary || run.afterRun && this.live.run?.taskId === run.taskId;
341
+ }
342
+ beginRun(live, snapshot = false) {
343
+ if (this.disposed) return;
344
+ const primary = live.inputs[0];
345
+ if (this.sameRun(live)) {
346
+ if (!this.run) return;
347
+ this.run.inputs = [...live.inputs];
348
+ this.update(this.run.span, { metadata: { "pi.durable.inputs": this.run.inputs } });
349
+ return;
350
+ }
351
+ const identity = primary === void 0 ? `task:${live.taskId}` : `submission:${primary}`;
352
+ if (this.completedRuns.has(identity)) return;
353
+ if (this.run) this.closeRun("Unexpected run replacement.");
354
+ const correlation = (primary === void 0 ? void 0 : this.submissions.get(primary)?.correlation) ?? {};
355
+ this.run = {
356
+ span: this.start("pi.durable.run", "agent", this.root, {
357
+ ...this.correlationAttributes(correlation),
358
+ metadata: {
359
+ ...this.correlationAttributes(correlation).metadata,
360
+ ...primary === void 0 ? {} : { "pi.durable.primary_submission_id": primary },
361
+ "pi.durable.inputs": [...live.inputs],
362
+ "pi.durable.snapshot_observation": snapshot
363
+ }
364
+ }),
365
+ primary,
366
+ identity,
367
+ inputs: [...live.inputs],
368
+ correlation,
369
+ round: 0,
370
+ tools: /* @__PURE__ */ new Map(),
371
+ completedTools: /* @__PURE__ */ new Set()
372
+ };
373
+ }
374
+ closeRound(reason) {
375
+ const run = this.run;
376
+ if (!run) return;
377
+ this.interrupted(run.generation, reason);
378
+ run.generation = void 0;
379
+ for (const tool of run.tools.values()) this.interrupted(tool, reason);
380
+ run.tools.clear();
381
+ }
382
+ closeRun(reason) {
383
+ const run = this.run;
384
+ if (!run) return;
385
+ this.closeRound(reason ?? "Run ended without an observed descendant completion.");
386
+ this.end(
387
+ run.span,
388
+ reason ? { level: "WARNING", statusMessage: reason, metadata: { "pi.durable.observation_incomplete": true } } : {}
389
+ );
390
+ this.completedRuns.add(run.identity);
391
+ this.run = void 0;
392
+ }
393
+ generation(key) {
394
+ const run = this.run;
395
+ if (this.disposed || !run || key && this.completedGenerations.has(key)) return void 0;
396
+ if (run.generation && key && run.generationKey !== key) {
397
+ this.interrupted(run.generation, "Generation boundary was not observed.");
398
+ run.generation = void 0;
399
+ }
400
+ if (!run.generation) {
401
+ run.round += 1;
402
+ run.generationKey = key;
403
+ run.generation = this.start("pi.durable.llm", "generation", run.span, {
404
+ metadata: { "pi.durable.observed_generation_index": run.round }
405
+ });
406
+ }
407
+ return run.generation;
408
+ }
409
+ entry(entry, retryAttempt) {
410
+ if (this.disposed || entry.id <= this.baselineEntry || this.entries.has(entry.id)) return;
411
+ this.entries.add(entry.id);
412
+ const message = entry.model?.[0];
413
+ const run = this.run;
414
+ if (!run) return;
415
+ if (message?.role === "assistant") {
416
+ const generation = this.generation();
417
+ if (!generation) return;
418
+ const usage = durableUsage(message);
419
+ this.end(generation, {
420
+ output: durableContent(entry.model, this.options.captureContent ?? true),
421
+ model: message.model,
422
+ ...usage,
423
+ level: message.stopReason === "error" ? "ERROR" : message.stopReason === "aborted" ? "WARNING" : "DEFAULT",
424
+ metadata: {
425
+ ...usage.metadata,
426
+ "pi.durable.entry_id": entry.id,
427
+ "pi.durable.task_id": entry.byTaskId,
428
+ "pi.durable.stop_reason": message.stopReason,
429
+ "pi.durable.provider": message.provider,
430
+ ...retryAttempt === void 0 ? {} : { "pi.durable.retry_attempt": retryAttempt }
431
+ }
432
+ });
433
+ if (run.generationKey) this.completedGenerations.add(run.generationKey);
434
+ run.generation = void 0;
435
+ } else if (message?.role === "toolResult") {
436
+ const key = entry.byTaskId === void 0 ? `entry:${entry.id}` : `task:${entry.byTaskId}`;
437
+ this.finishTool(key, message.toolCallId, entry);
438
+ }
439
+ }
440
+ finishTool(key, callId, entry) {
441
+ const run = this.run;
442
+ if (!run || run.completedTools.has(key)) return;
443
+ run.completedTools.add(key);
444
+ const tool = run.tools.get(key) ?? this.start("pi.durable.tool", "tool", run.span, {
445
+ metadata: { "pi.durable.start_not_observed": true, "pi.durable.tool_call_id": callId }
446
+ });
447
+ const message = entry?.model?.[0];
448
+ const usage = message ? durableUsage(message) : {};
449
+ this.end(tool, {
450
+ ...entry ? { output: durableContent(entry.model, this.options.captureContent ?? true, durableDiagnosticCount(entry)) } : {},
451
+ ...usage,
452
+ level: message?.role === "toolResult" && message.isError ? "ERROR" : entry ? "DEFAULT" : "WARNING",
453
+ metadata: {
454
+ ...usage.metadata,
455
+ "pi.durable.result_entry_id": entry?.id,
456
+ "pi.durable.result_missing": !entry
457
+ }
458
+ });
459
+ run.tools.delete(key);
460
+ }
461
+ task(task, live) {
462
+ const run = this.run;
463
+ if (this.disposed || !run) return;
464
+ if (task.failed) this.update(run.span, { level: "ERROR", metadata: { "pi.durable.failed_task_id": task.id } });
465
+ if (task.kind === "pi.generation") {
466
+ if (task.phase === "request" || task.phase === "poll")
467
+ this.generation(`${task.id}:${task.attempt ?? 0}:${task.cutoff ?? "unknown"}`);
468
+ else if (task.phase === "retry") {
469
+ this.end(run.generation, {
470
+ level: "ERROR",
471
+ statusMessage: "Provider attempt will retry.",
472
+ metadata: { "pi.durable.retry_attempt": task.attempt }
473
+ });
474
+ if (run.generationKey) this.completedGenerations.add(run.generationKey);
475
+ run.generation = void 0;
476
+ }
477
+ return;
478
+ }
479
+ if (task.kind !== "pi.tool" || task.callId === void 0) return;
480
+ const key = `task:${task.id}`;
481
+ if (run.completedTools.has(key)) return;
482
+ if (task.arguments !== void 0 && !run.tools.has(key)) {
483
+ const slot = live.tools.find((slot2) => slot2.taskId === task.id);
484
+ run.tools.set(
485
+ key,
486
+ this.start("pi.durable.tool", "tool", run.span, {
487
+ input: this.options.captureContent === false ? "[content capture disabled]" : sanitizeTraceValue(task.arguments, true),
488
+ metadata: {
489
+ "pi.durable.tool_call_id": task.callId,
490
+ "pi.durable.tool_name": slot?.name ?? task.name,
491
+ "pi.durable.task_id": task.id,
492
+ "pi.durable.observed_generation_index": run.round
493
+ }
494
+ })
495
+ );
496
+ }
497
+ if (task.terminal) this.finishTool(key, task.callId);
498
+ }
499
+ async commit(commit, readEntry) {
500
+ if (this.disposed) return;
501
+ for (const record of [...commit.submissions].sort((a, b) => a.id - b.id)) {
502
+ try {
503
+ await this.submission(record, readEntry);
504
+ } catch {
505
+ if (!this.disposed) this.report("observer delivery");
506
+ }
507
+ if (this.disposed) return;
508
+ }
509
+ const next = commit.live ?? this.live;
510
+ if (!this.run && next.run) this.beginRun(next.run);
511
+ for (const task of commit.tasks) if (!task.terminal && task.phase !== "retry") this.task(task, next);
512
+ for (const entry of [...commit.entries].sort((a, b) => a.id - b.id)) {
513
+ const retry = commit.tasks.find((task) => task.id === entry.byTaskId && task.phase === "retry");
514
+ this.entry(entry, retry?.attempt);
515
+ }
516
+ for (const task of commit.tasks) if (task.terminal || task.phase === "retry") this.task(task, next);
517
+ if (this.disposed) return;
518
+ if (commit.live) {
519
+ if (this.run && !this.sameRun(next.run)) this.closeRun();
520
+ if (next.run) this.beginRun(next.run);
521
+ this.live = next;
522
+ }
523
+ }
524
+ dispose(reason = "Tracing observer disposed; execution outcome is unknown.") {
525
+ if (this.disposed) return;
526
+ this.disposed = true;
527
+ this.closeRun(reason);
528
+ for (const state of this.submissions.values()) if (!state.terminal) this.interrupted(state.span, reason);
529
+ this.end(this.root, { metadata: { "pi.durable.resync_count": this.gaps } });
530
+ this.submissions.clear();
531
+ this.pendingSubmissions.clear();
532
+ this.entries.clear();
533
+ this.completedRuns.clear();
534
+ this.completedGenerations.clear();
535
+ }
536
+ };
537
+
538
+ // src/durable-state.ts
539
+ import {
540
+ InboxDoc,
541
+ LiveDoc
542
+ } from "@earendil-works/pi-durable";
543
+ function liveProjection(live = {}) {
544
+ return {
545
+ ...live.run ? { run: { taskId: live.run.taskId, inputs: live.run.inputs, afterRun: live.run.afterRun !== void 0 } } : {},
546
+ tools: [...live.tools ?? [], ...live.nestedTools ?? []].map(({ taskId, callId, name, status }) => ({
547
+ taskId,
548
+ callId,
549
+ name,
550
+ status
551
+ }))
552
+ };
553
+ }
554
+ function durableSnapshot(view) {
555
+ return {
556
+ entries: view.entries,
557
+ live: liveProjection(view.docs[LiveDoc.definition.kind]),
558
+ inbox: (view.docs[InboxDoc.definition.kind]?.items ?? []).map(({ id }) => id)
559
+ };
560
+ }
561
+ function durableCommit(publication, conversationId) {
562
+ const entries = [];
563
+ const submissions = [];
564
+ const tasks = [];
565
+ let live;
566
+ let inbox;
567
+ for (const change of publication.changes) {
568
+ if (change.type === "document") {
569
+ if (change.conversationId !== conversationId || change.record.key !== void 0) continue;
570
+ if (change.record.kind === LiveDoc.definition.kind)
571
+ live = liveProjection(change.kind === "change" ? change.value : void 0);
572
+ else if (change.record.kind === InboxDoc.definition.kind)
573
+ inbox = change.kind === "change" ? change.value.items.map(({ id }) => id) : [];
574
+ } else if (change.type !== "document.copy" && change.type !== "conversation" && change.value.conversationId === conversationId) {
575
+ if (change.type === "entry") entries.push(change.value);
576
+ else if (change.type === "submission") submissions.push(change.value);
577
+ else if (change.type === "task") {
578
+ const task = change.value;
579
+ const terminal = task.state.status === "terminal" || task.state.status === "completing";
580
+ const failed = terminal && (task.state.outcome?.status === "faulted" || task.state.outcome?.status === "orphaned");
581
+ if (task.kind !== "pi.tool" && task.kind !== "pi.generation") {
582
+ if (failed) tasks.push({ id: task.id, kind: "other", terminal, failed });
583
+ continue;
584
+ }
585
+ const checkpoint = task.state.checkpoint;
586
+ const record = {
587
+ id: task.id,
588
+ kind: task.kind,
589
+ terminal,
590
+ failed,
591
+ ...checkpoint && typeof checkpoint === "object" && "phase" in checkpoint ? { phase: String(checkpoint.phase) } : {}
592
+ };
593
+ if (task.kind === "pi.tool") {
594
+ const input = task.input;
595
+ record.callId = input.kind === "nested" ? input.call.id : input.callId;
596
+ if (input.kind === "nested") record.name = input.call.name;
597
+ const intent = checkpoint;
598
+ if (intent?.phase === "execute") record.arguments = intent.arguments;
599
+ } else if (checkpoint && typeof checkpoint === "object" && "attempt" in checkpoint) {
600
+ const attempt = checkpoint.attempt;
601
+ if (typeof attempt === "number" && Number.isFinite(attempt)) record.attempt = attempt;
602
+ if ("cutoff" in checkpoint && typeof checkpoint.cutoff === "number") record.cutoff = checkpoint.cutoff;
603
+ }
604
+ tasks.push(record);
605
+ }
606
+ }
607
+ }
608
+ return { entries, submissions, tasks, ...live ? { live } : {}, ...inbox ? { inbox } : {} };
609
+ }
610
+ function advanceDurableSnapshot(snapshot, commit) {
611
+ return {
612
+ // This is only the bounded overflow baseline, not a second transcript or a historical replay buffer.
613
+ entries: [...snapshot.entries, ...commit.entries].slice(-100),
614
+ live: commit.live ?? snapshot.live,
615
+ inbox: commit.inbox ?? snapshot.inbox
616
+ };
617
+ }
618
+
619
+ // src/durable.ts
620
+ var MAX_PENDING_PUBLICATIONS = 100;
621
+ var observedConversations = /* @__PURE__ */ new WeakMap();
622
+ async function createPiLangfuseDurableConversation(runtime, options) {
623
+ options = { ...options };
624
+ const { context, cancel } = withCancel(options.context);
625
+ let view;
626
+ let commits;
627
+ let latestView;
628
+ let committed;
629
+ let recorder;
630
+ let releaseRuntime;
631
+ let disposed = false;
632
+ let ready = false;
633
+ let sourceEnded = false;
634
+ let sourceClosing = false;
635
+ let sourceListenerError = false;
636
+ let recordingFailed = false;
637
+ let ownsConversation = false;
638
+ let initialization;
639
+ let delivery = Promise.resolve();
640
+ let scheduled = false;
641
+ let disposePromise;
642
+ const pending = [];
643
+ let pendingPublications = 0;
644
+ let resolveClosed;
645
+ const closed = new Promise((resolve) => {
646
+ resolveClosed = resolve;
647
+ });
648
+ function report(stage) {
649
+ if (recorder) recorder.report(stage);
650
+ else {
651
+ try {
652
+ Promise.resolve(options.onError?.(stage)).catch(() => void 0);
653
+ } catch {
654
+ }
655
+ }
656
+ }
657
+ async function readEntry(id) {
658
+ if (disposed) return void 0;
659
+ const entry = await options.harness.commit((tx) => tx.entry(id), context);
660
+ return disposed ? void 0 : entry;
661
+ }
662
+ async function reconcile(id, correlation) {
663
+ if (disposed) return;
664
+ try {
665
+ const submission = await options.harness.submission(id, context);
666
+ if (disposed || !submission) return;
667
+ const record = await submission.status(context);
668
+ if (disposed) return;
669
+ await recorder?.submission(record, readEntry, correlation, true);
670
+ } catch {
671
+ if (!disposed) report("submission status read");
672
+ }
673
+ }
674
+ function clearPending() {
675
+ for (const item of pending.splice(0)) if (item.kind === "submission") item.done();
676
+ pendingPublications = 0;
677
+ }
678
+ function schedule() {
679
+ if (disposed || scheduled) return;
680
+ scheduled = true;
681
+ delivery = Promise.resolve().then(drain).catch(() => {
682
+ if (!disposed) report("observer delivery");
683
+ }).finally(() => {
684
+ scheduled = false;
685
+ if (!disposed && recorder && (pending.length || sourceEnded || recordingFailed)) schedule();
686
+ });
687
+ }
688
+ function enqueue(item) {
689
+ if (disposed) {
690
+ if (item.kind === "submission") item.done();
691
+ return;
692
+ }
693
+ if (item.kind !== "submission" && pendingPublications >= MAX_PENDING_PUBLICATIONS) {
694
+ let retained = 0;
695
+ for (const queued of pending) if (queued.kind === "submission") pending[retained++] = queued;
696
+ pending.length = retained;
697
+ pendingPublications = 0;
698
+ if (committed) {
699
+ pending.push({ kind: "snapshot", snapshot: committed });
700
+ pendingPublications += 1;
701
+ }
702
+ if (item.kind === "commit") {
703
+ schedule();
704
+ return;
705
+ }
706
+ }
707
+ pending.push(item);
708
+ if (item.kind !== "submission") pendingPublications += 1;
709
+ schedule();
710
+ }
711
+ async function drain() {
712
+ if (!recorder || disposed) return;
713
+ if (recordingFailed) {
714
+ recordingFailed = false;
715
+ report("observer recording");
716
+ void dispose();
717
+ return;
718
+ }
719
+ while (!disposed) {
720
+ const item = pending.shift();
721
+ if (!item) break;
722
+ if (item.kind !== "submission") pendingPublications -= 1;
723
+ try {
724
+ if (item.kind === "commit") await recorder.commit(item.commit, readEntry);
725
+ else if (item.kind === "submission") await reconcile(item.id, item.correlation);
726
+ else {
727
+ recorder.snapshot(item.snapshot);
728
+ const ids = /* @__PURE__ */ new Set([
729
+ ...recorder.submissionIds,
730
+ ...item.snapshot.live.run?.inputs ?? [],
731
+ ...item.snapshot.inbox
732
+ ]);
733
+ for (const id of ids) {
734
+ await reconcile(id);
735
+ if (disposed) break;
736
+ }
737
+ }
738
+ } catch {
739
+ if (!disposed) report("observer delivery");
740
+ } finally {
741
+ if (item.kind === "submission") item.done();
742
+ }
743
+ }
744
+ if (!disposed && sourceEnded) {
745
+ if (sourceListenerError) report("observer closure");
746
+ else if (sourceClosing) {
747
+ try {
748
+ const end = await awaitWithContext(options.harness.closed, context);
749
+ if (disposed) return;
750
+ if (end.kind === "failed") report("observer closure");
751
+ } catch {
752
+ if (!disposed) report("observer closure");
753
+ }
754
+ }
755
+ if (!disposed) void dispose();
756
+ }
757
+ }
758
+ function recordEnd(end) {
759
+ sourceEnded = true;
760
+ sourceClosing ||= end.kind === "failed";
761
+ sourceListenerError ||= end.kind === "listener_error";
762
+ schedule();
763
+ }
764
+ function observeSubmission(id, correlation) {
765
+ return new Promise((done) => enqueue({ kind: "submission", id, correlation, done }));
766
+ }
767
+ function dispose() {
768
+ if (disposePromise) return disposePromise;
769
+ disposePromise = Promise.resolve().then(async () => {
770
+ await initialization;
771
+ await delivery;
772
+ if (ownsConversation) {
773
+ observedConversations.get(options.harness)?.delete(options.conversationId);
774
+ ownsConversation = false;
775
+ }
776
+ releaseRuntime?.();
777
+ releaseRuntime = void 0;
778
+ resolveClosed();
779
+ });
780
+ disposed = true;
781
+ ready = false;
782
+ cancel();
783
+ context.abortSignal?.removeEventListener("abort", onAbort);
784
+ clearPending();
785
+ try {
786
+ commits?.detach();
787
+ } catch {
788
+ report("observer stop");
789
+ }
790
+ try {
791
+ view?.detach();
792
+ } catch {
793
+ report("observer stop");
794
+ }
795
+ recorder?.dispose();
796
+ latestView = void 0;
797
+ committed = void 0;
798
+ return disposePromise;
799
+ }
800
+ function onAbort() {
801
+ void dispose();
802
+ }
803
+ const controller = {
804
+ get active() {
805
+ return !disposed && !runtime.closed && ready;
806
+ },
807
+ closed,
808
+ observeSubmission,
809
+ dispose
810
+ };
811
+ async function initialize() {
812
+ try {
813
+ context.abortSignal?.throwIfAborted();
814
+ const conversations = observedConversations.get(options.harness) ?? /* @__PURE__ */ new Set();
815
+ if (conversations.has(options.conversationId)) throw new Error("Conversation already observed.");
816
+ conversations.add(options.conversationId);
817
+ observedConversations.set(options.harness, conversations);
818
+ ownsConversation = true;
819
+ const internal = getLangfuseRuntimeInternal(runtime);
820
+ releaseRuntime = internal.registerSession(dispose);
821
+ context.abortSignal?.addEventListener("abort", onAbort, { once: true });
822
+ commits = options.harness.observeCommits({
823
+ advance(publication) {
824
+ if (disposed || !ready || !committed) return;
825
+ try {
826
+ const commit = durableCommit(publication, options.conversationId);
827
+ if (!commit.live && !commit.inbox && !commit.entries.length && !commit.tasks.length && !commit.submissions.length)
828
+ return;
829
+ committed = advanceDurableSnapshot(committed, commit);
830
+ enqueue({ kind: "commit", commit });
831
+ } catch {
832
+ recordingFailed = true;
833
+ schedule();
834
+ }
835
+ },
836
+ end: recordEnd
837
+ });
838
+ const attached = await options.harness.observeConversation(
839
+ options.conversationId,
840
+ {
841
+ advance(call) {
842
+ if (disposed || ready || call.kind !== "value") return;
843
+ try {
844
+ latestView = durableSnapshot(call.value);
845
+ } catch {
846
+ recordingFailed = true;
847
+ }
848
+ },
849
+ end: recordEnd
850
+ },
851
+ context
852
+ );
853
+ view = attached;
854
+ if (disposed || runtime.closed) {
855
+ attached.detach();
856
+ void dispose();
857
+ return;
858
+ }
859
+ const initial = durableSnapshot(attached.value);
860
+ await options.harness.commit((tx) => tx.conversation(options.conversationId), context);
861
+ if (disposed || runtime.closed) {
862
+ void dispose();
863
+ return;
864
+ }
865
+ if (sourceEnded || recordingFailed) throw new Error("Observation ended while attaching.");
866
+ const baseline = latestView ?? initial;
867
+ committed = baseline;
868
+ ready = true;
869
+ recorder = new DurableRecorder(internal.backend, { ...options, metadata: { ...options.metadata } });
870
+ if (disposed || runtime.closed) {
871
+ recorder.dispose();
872
+ void dispose();
873
+ return;
874
+ }
875
+ recorder.snapshot(baseline, true);
876
+ latestView = void 0;
877
+ const ids = /* @__PURE__ */ new Set([...baseline.live.run?.inputs ?? [], ...baseline.inbox]);
878
+ const baselineReads = [...ids].map((id) => observeSubmission(id));
879
+ schedule();
880
+ await Promise.all(baselineReads);
881
+ } catch {
882
+ if (!disposed) report("observer initialization");
883
+ void dispose();
884
+ }
885
+ }
886
+ initialization = Promise.resolve().then(initialize);
887
+ await initialization;
888
+ if (disposed) await dispose();
889
+ return controller;
890
+ }
891
+ export {
892
+ createLangfuseRuntime,
893
+ createLangfuseRuntimeFromBackend,
894
+ createOtelRuntime,
895
+ createPiLangfuseDurableConversation
896
+ };
897
+ //# sourceMappingURL=durable.js.map