@ontrails/core 0.2.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 (86) hide show
  1. package/CHANGELOG.md +849 -0
  2. package/README.md +190 -0
  3. package/package.json +36 -0
  4. package/src/activation-provenance.ts +116 -0
  5. package/src/activation-source-compatibility.ts +430 -0
  6. package/src/activation-source-derivation.ts +227 -0
  7. package/src/activation-source.ts +93 -0
  8. package/src/blob-ref.ts +90 -0
  9. package/src/branded.ts +135 -0
  10. package/src/collections.ts +99 -0
  11. package/src/compose-batch.ts +69 -0
  12. package/src/compose-schema.ts +36 -0
  13. package/src/context.ts +66 -0
  14. package/src/derive.ts +485 -0
  15. package/src/detours.ts +8 -0
  16. package/src/diagnostics.ts +21 -0
  17. package/src/draft.ts +350 -0
  18. package/src/entity.ts +346 -0
  19. package/src/error-rendering.ts +87 -0
  20. package/src/errors.ts +483 -0
  21. package/src/execute.ts +1577 -0
  22. package/src/fetch.ts +138 -0
  23. package/src/fire.ts +1172 -0
  24. package/src/glob.ts +81 -0
  25. package/src/guards.ts +37 -0
  26. package/src/index.ts +704 -0
  27. package/src/internal/fork-ctx.ts +69 -0
  28. package/src/layer-field-rendering.ts +193 -0
  29. package/src/layer.ts +81 -0
  30. package/src/observe.ts +361 -0
  31. package/src/path-scope.ts +66 -0
  32. package/src/path-security.ts +98 -0
  33. package/src/patterns/bulk.ts +16 -0
  34. package/src/patterns/change.ts +12 -0
  35. package/src/patterns/date-range.ts +12 -0
  36. package/src/patterns/index.ts +8 -0
  37. package/src/patterns/pagination.ts +22 -0
  38. package/src/patterns/progress.ts +13 -0
  39. package/src/patterns/sorting.ts +14 -0
  40. package/src/patterns/status.ts +11 -0
  41. package/src/patterns/timestamps.ts +12 -0
  42. package/src/permits.ts +12 -0
  43. package/src/queue.ts +163 -0
  44. package/src/redaction/index.ts +3 -0
  45. package/src/redaction/patterns.ts +50 -0
  46. package/src/redaction/redactor.ts +178 -0
  47. package/src/resilience.ts +234 -0
  48. package/src/resource-config.ts +804 -0
  49. package/src/resource.ts +194 -0
  50. package/src/result.ts +212 -0
  51. package/src/run.ts +76 -0
  52. package/src/runtime-builtins.ts +69 -0
  53. package/src/schedule-runtime.ts +689 -0
  54. package/src/schedule.ts +326 -0
  55. package/src/serialization.ts +265 -0
  56. package/src/sha256.ts +136 -0
  57. package/src/signal-diagnostics.ts +633 -0
  58. package/src/signal-ref.ts +111 -0
  59. package/src/signal.ts +104 -0
  60. package/src/store/accessor-protocol.ts +56 -0
  61. package/src/store/index.ts +4 -0
  62. package/src/structured-examples.ts +248 -0
  63. package/src/surface-derivation.ts +91 -0
  64. package/src/surface-filter.ts +101 -0
  65. package/src/surface-overlay.ts +694 -0
  66. package/src/surface-versioning.ts +42 -0
  67. package/src/topo.ts +835 -0
  68. package/src/tracing.ts +346 -0
  69. package/src/trail-id-glob.ts +15 -0
  70. package/src/trail.ts +1351 -0
  71. package/src/trails/derive-trail.ts +835 -0
  72. package/src/trails/index.ts +9 -0
  73. package/src/trails/ingest.ts +152 -0
  74. package/src/trails-db.ts +212 -0
  75. package/src/transport-error-map.ts +163 -0
  76. package/src/type-utils.ts +87 -0
  77. package/src/types.ts +300 -0
  78. package/src/validate-established-topo.ts +73 -0
  79. package/src/validate-topo.ts +725 -0
  80. package/src/validation.ts +330 -0
  81. package/src/version-marker.ts +716 -0
  82. package/src/version-resolution.ts +308 -0
  83. package/src/version-runtime.ts +120 -0
  84. package/src/webhook.ts +461 -0
  85. package/src/workspace.ts +244 -0
  86. package/src/zod-wrappers.ts +72 -0
package/src/tracing.ts ADDED
@@ -0,0 +1,346 @@
1
+ /**
2
+ * Intrinsic tracing primitives.
3
+ *
4
+ * This module is the home for the trace record type, the sink interface,
5
+ * the sink registry, and the helpers `executeTrail` uses to create root
6
+ * trace records and child spans. Core keeps this minimal contract public so
7
+ * `@ontrails/observability`, adapters, and tests share the
8
+ * same intrinsic execution record shape.
9
+ *
10
+ * Tracing is intrinsic: every `executeTrail` call automatically produces a
11
+ * root `TraceRecord`, `ctx.trace(label, fn)` creates nested child spans, and
12
+ * the signal runtime records lifecycle points underneath the active producer
13
+ * trace. A default no-op sink is installed at module load so core never
14
+ * crashes when no real sink has been registered.
15
+ */
16
+
17
+ /** Signal lifecycle records emitted by the typed signal runtime. */
18
+ export type SignalTraceRecordName =
19
+ | 'signal.fired'
20
+ | 'signal.handler.completed'
21
+ | 'signal.handler.failed'
22
+ | 'signal.handler.invoked'
23
+ | 'signal.handler.predicate_failed'
24
+ | 'signal.handler.predicate_matched'
25
+ | 'signal.handler.predicate_skipped'
26
+ | 'signal.invalid';
27
+
28
+ /** Activation boundary records emitted by runtime materializers. */
29
+ export type ActivationTraceRecordName =
30
+ | 'activation.cycle_detected'
31
+ | 'activation.queue'
32
+ | 'activation.scheduled'
33
+ | 'activation.webhook'
34
+ | 'activation.webhook.invalid';
35
+
36
+ /** Evidence of a single trail execution, manual span, activation boundary, or signal lifecycle point. */
37
+ export interface TraceRecord {
38
+ readonly id: string;
39
+ readonly traceId: string;
40
+ readonly rootId: string;
41
+ readonly parentId?: string | undefined;
42
+ readonly kind: 'activation' | 'signal' | 'span' | 'trail';
43
+ readonly name: string;
44
+ readonly trailId?: string | undefined;
45
+ readonly surface?: 'cli' | 'mcp' | 'http' | 'ws' | undefined;
46
+ readonly intent?: 'read' | 'write' | 'destroy' | undefined;
47
+ readonly startedAt: number;
48
+ readonly endedAt?: number | undefined;
49
+ readonly status: 'ok' | 'err' | 'cancelled';
50
+ readonly errorCategory?: string | undefined;
51
+ readonly sampled?: boolean | undefined;
52
+ readonly permit?:
53
+ | { readonly id: string; readonly tenantId?: string }
54
+ | undefined;
55
+ readonly attrs: Readonly<Record<string, unknown>>;
56
+ }
57
+
58
+ /**
59
+ * Minimal shape a tracing sink must satisfy.
60
+ *
61
+ * Kept intentionally tiny so adapters in `@ontrails/observability`,
62
+ * `@ontrails/observability`, and user code can all satisfy it without
63
+ * additional dependencies.
64
+ */
65
+ export interface TraceSink {
66
+ readonly write: (record: TraceRecord) => void | Promise<void>;
67
+ }
68
+
69
+ /** Trace context carried through trail execution via `ctx.extensions`. */
70
+ export interface TraceContext {
71
+ readonly traceId: string;
72
+ readonly spanId: string;
73
+ readonly rootId: string;
74
+ readonly sampled: boolean;
75
+ }
76
+
77
+ /** Key used to store trace context in `ctx.extensions`. */
78
+ export const TRACE_CONTEXT_KEY = '__trace_context';
79
+
80
+ /** Read trace context from trail context extensions. */
81
+ export const getTraceContext = (ctx: {
82
+ readonly extensions?: Readonly<Record<string, unknown>> | undefined;
83
+ }): TraceContext | undefined =>
84
+ ctx.extensions?.[TRACE_CONTEXT_KEY] as TraceContext | undefined;
85
+
86
+ // ---------------------------------------------------------------------------
87
+ // Default no-op sink + sink registry
88
+ // ---------------------------------------------------------------------------
89
+
90
+ /** No-op sink installed by default so core never crashes without configuration. */
91
+ export const NOOP_SINK: TraceSink = {
92
+ // oxlint-disable-next-line no-empty-function -- intentional no-op
93
+ write: () => {},
94
+ };
95
+
96
+ // oxlint-disable-next-line eslint-plugin-jest/require-hook -- module-level sink registry, not test setup
97
+ let currentSink: TraceSink = NOOP_SINK;
98
+
99
+ /**
100
+ * Register a trace sink globally.
101
+ *
102
+ * All trails executed via `executeTrail` will write their completed trace
103
+ * records to this sink, as will every `ctx.trace(label, fn)` child span.
104
+ * Registering `undefined` or calling {@link clearTraceSink} resets back to
105
+ * the default no-op sink.
106
+ */
107
+ export const registerTraceSink = (sink: TraceSink | undefined): void => {
108
+ currentSink = sink ?? NOOP_SINK;
109
+ };
110
+
111
+ /** Retrieve the currently registered sink (never undefined). */
112
+ export const getTraceSink = (): TraceSink => currentSink;
113
+
114
+ /** True when tracing is effectively disabled and executeTrail should skip allocation. */
115
+ export const isTracingDisabled = (sink: TraceSink = currentSink): boolean =>
116
+ sink === NOOP_SINK;
117
+
118
+ /** Reset the sink registry back to the default no-op sink. */
119
+ export const clearTraceSink = (): void => {
120
+ currentSink = NOOP_SINK;
121
+ };
122
+
123
+ // ---------------------------------------------------------------------------
124
+ // Record + span helpers
125
+ // ---------------------------------------------------------------------------
126
+
127
+ /** Options for creating a trail-scoped {@link TraceRecord}. */
128
+ interface CreateTraceRecordOptions {
129
+ readonly trailId: string;
130
+ readonly traceId?: string | undefined;
131
+ readonly parentId?: string | undefined;
132
+ readonly rootId?: string | undefined;
133
+ readonly surface?: TraceRecord['surface'];
134
+ readonly intent?: TraceRecord['intent'];
135
+ readonly sampled?: boolean | undefined;
136
+ readonly permit?:
137
+ | { readonly id: string; readonly tenantId?: string }
138
+ | undefined;
139
+ }
140
+
141
+ interface CreateActivationTraceRecordOptions {
142
+ readonly attrs?: Readonly<Record<string, unknown>> | undefined;
143
+ readonly parentId?: string | undefined;
144
+ readonly rootId?: string | undefined;
145
+ readonly traceId?: string | undefined;
146
+ readonly sampled?: boolean | undefined;
147
+ }
148
+
149
+ /** Create a fresh trail-kind {@link TraceRecord}. */
150
+ export const createTraceRecord = (
151
+ options: CreateTraceRecordOptions
152
+ ): TraceRecord => {
153
+ const id = Bun.randomUUIDv7();
154
+ const traceId = options.traceId ?? Bun.randomUUIDv7();
155
+
156
+ return {
157
+ attrs: {},
158
+ endedAt: undefined,
159
+ id,
160
+ intent: options.intent,
161
+ kind: 'trail',
162
+ name: options.trailId,
163
+ parentId: options.parentId,
164
+ permit: options.permit,
165
+ rootId: options.rootId ?? id,
166
+ sampled: options.sampled,
167
+ startedAt: Date.now(),
168
+ status: 'ok',
169
+ surface: options.surface,
170
+ traceId,
171
+ trailId: options.trailId,
172
+ };
173
+ };
174
+
175
+ /** Create an activation-kind {@link TraceRecord}. */
176
+ export const createActivationTraceRecord = (
177
+ name: ActivationTraceRecordName,
178
+ options: CreateActivationTraceRecordOptions = {}
179
+ ): TraceRecord => {
180
+ const id = Bun.randomUUIDv7();
181
+ const traceId = options.traceId ?? Bun.randomUUIDv7();
182
+
183
+ return {
184
+ attrs: options.attrs ?? {},
185
+ endedAt: undefined,
186
+ errorCategory: undefined,
187
+ id,
188
+ intent: undefined,
189
+ kind: 'activation',
190
+ name,
191
+ parentId: options.parentId,
192
+ permit: undefined,
193
+ rootId: options.rootId ?? id,
194
+ sampled: options.sampled,
195
+ startedAt: Date.now(),
196
+ status: 'ok',
197
+ surface: undefined,
198
+ traceId,
199
+ trailId: undefined,
200
+ };
201
+ };
202
+
203
+ /** Build a span record from a parent trace context. */
204
+ export const createSpanRecord = (
205
+ parent: TraceContext,
206
+ label: string
207
+ ): TraceRecord => ({
208
+ attrs: {},
209
+ endedAt: undefined,
210
+ errorCategory: undefined,
211
+ id: Bun.randomUUIDv7(),
212
+ intent: undefined,
213
+ kind: 'span',
214
+ name: label,
215
+ parentId: parent.spanId,
216
+ rootId: parent.rootId,
217
+ sampled: parent.sampled,
218
+ startedAt: Date.now(),
219
+ status: 'ok',
220
+ surface: undefined,
221
+ traceId: parent.traceId,
222
+ trailId: undefined,
223
+ });
224
+
225
+ /** Build a signal lifecycle record from a parent trace context. */
226
+ export const createSignalTraceRecord = (
227
+ parent: TraceContext,
228
+ name: SignalTraceRecordName,
229
+ attrs: Readonly<Record<string, unknown>> = {}
230
+ ): TraceRecord => ({
231
+ attrs,
232
+ endedAt: undefined,
233
+ errorCategory: undefined,
234
+ id: Bun.randomUUIDv7(),
235
+ intent: undefined,
236
+ kind: 'signal',
237
+ name,
238
+ parentId: parent.spanId,
239
+ rootId: parent.rootId,
240
+ sampled: parent.sampled,
241
+ startedAt: Date.now(),
242
+ status: 'ok',
243
+ surface: undefined,
244
+ traceId: parent.traceId,
245
+ trailId: undefined,
246
+ });
247
+
248
+ /** Use a completed record as the current trace parent for subsequent trail execution. */
249
+ export const traceContextFromRecord = (record: TraceRecord): TraceContext => ({
250
+ rootId: record.rootId,
251
+ sampled: record.sampled ?? true,
252
+ spanId: record.id,
253
+ traceId: record.traceId,
254
+ });
255
+
256
+ /** Mark a record as completed with timing and status. */
257
+ export const completeRecord = (
258
+ record: TraceRecord,
259
+ status: TraceRecord['status'],
260
+ errorCategory?: string | undefined
261
+ ): TraceRecord => ({
262
+ ...record,
263
+ endedAt: Date.now(),
264
+ errorCategory,
265
+ status,
266
+ });
267
+
268
+ /**
269
+ * Best-effort sink write that never throws.
270
+ *
271
+ * Returns `true` when the sink accepted the record, `false` when the write
272
+ * threw. Most callers can ignore the return value -- it exists so that
273
+ * callers that hand the written record back as a parent trace context
274
+ * (e.g. {@link writeActivationTraceRecord}) can refuse to do so when the
275
+ * record never actually made it to storage.
276
+ */
277
+ export const writeToSink = async (
278
+ sink: TraceSink,
279
+ record: TraceRecord
280
+ ): Promise<boolean> => {
281
+ try {
282
+ await Promise.resolve(sink.write(record));
283
+ return true;
284
+ } catch {
285
+ // Sink failures must never affect trail result delivery.
286
+ return false;
287
+ }
288
+ };
289
+
290
+ /** Best-effort write for signal lifecycle records, no-op when tracing is disabled. */
291
+ export const writeSignalTraceRecord = async (
292
+ ctx: { readonly extensions?: Readonly<Record<string, unknown>> | undefined },
293
+ name: SignalTraceRecordName,
294
+ attrs: Readonly<Record<string, unknown>>,
295
+ status: TraceRecord['status'] = 'ok',
296
+ errorCategory?: string | undefined,
297
+ sink: TraceSink = getTraceSink()
298
+ ): Promise<void> => {
299
+ const parent = getTraceContext(ctx);
300
+ if (parent === undefined || isTracingDisabled(sink)) {
301
+ return;
302
+ }
303
+ await writeToSink(
304
+ sink,
305
+ completeRecord(
306
+ createSignalTraceRecord(parent, name, attrs),
307
+ status,
308
+ errorCategory
309
+ )
310
+ );
311
+ };
312
+
313
+ /** Best-effort write for activation boundary records, no-op when tracing is disabled. */
314
+ export const writeActivationTraceRecord = async (
315
+ name: ActivationTraceRecordName,
316
+ attrs: Readonly<Record<string, unknown>>,
317
+ status: TraceRecord['status'] = 'ok',
318
+ errorCategory?: string | undefined,
319
+ parent?: TraceContext | undefined,
320
+ sink: TraceSink = getTraceSink()
321
+ ): Promise<TraceRecord | undefined> => {
322
+ if (isTracingDisabled(sink)) {
323
+ return undefined;
324
+ }
325
+ const record = completeRecord(
326
+ createActivationTraceRecord(name, {
327
+ attrs,
328
+ parentId: parent?.spanId,
329
+ rootId: parent?.rootId,
330
+ // Parentless activations are the root span of their trace tree. Default
331
+ // sampled to true so the activation boundary stays consistent with
332
+ // child trail records, which default sampled=true via
333
+ // traceContextFromRecord. Inconsistent sampled flags within a trace
334
+ // break filters/exporters that gate on the activation boundary.
335
+ sampled: parent?.sampled ?? true,
336
+ traceId: parent?.traceId,
337
+ }),
338
+ status,
339
+ errorCategory
340
+ );
341
+ const written = await writeToSink(sink, record);
342
+ // When the sink dropped the record we must not hand it back to callers as
343
+ // a parent trace context -- subsequent child writes would reference an
344
+ // activation span that never reached storage, producing broken lineage.
345
+ return written ? record : undefined;
346
+ };
@@ -0,0 +1,15 @@
1
+ import { matchesAnyGlob, matchesGlob } from './glob.js';
2
+
3
+ declare const trailIdGlobBrand: unique symbol;
4
+
5
+ export type TrailIdGlob = string & {
6
+ readonly [trailIdGlobBrand]: 'TrailIdGlob';
7
+ };
8
+
9
+ export const matchesTrailIdGlob = (id: string, pattern: string): boolean =>
10
+ matchesGlob(id, pattern, { separator: '.' });
11
+
12
+ export const matchesAnyTrailIdGlob = (
13
+ id: string,
14
+ patterns: readonly string[] | undefined
15
+ ): boolean => matchesAnyGlob(id, patterns, { separator: '.' });