@ontrails/trails 1.0.0-beta.3 → 1.0.0-beta.30

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 (157) hide show
  1. package/CHANGELOG.md +886 -0
  2. package/README.md +27 -0
  3. package/package.json +30 -7
  4. package/src/app.ts +125 -2
  5. package/src/cli.ts +344 -11
  6. package/src/completions.ts +240 -0
  7. package/src/lifecycle-source-io.ts +33 -0
  8. package/src/load-app-mirror.ts +202 -0
  9. package/src/local-state-io.ts +173 -0
  10. package/src/mcp-app.ts +40 -0
  11. package/src/mcp-options.ts +76 -0
  12. package/src/mcp.ts +8 -0
  13. package/src/project-writes.ts +377 -0
  14. package/src/release/bindings.ts +39 -0
  15. package/src/release/check.ts +844 -0
  16. package/src/release/config.ts +63 -0
  17. package/src/release/contract-facts.ts +425 -0
  18. package/src/release/index.ts +125 -0
  19. package/src/release/native-bun-publish.ts +651 -0
  20. package/src/release/native-bun-registry.ts +754 -0
  21. package/src/release/pack-coherence.ts +457 -0
  22. package/src/release/packed-artifacts-smoke.ts +236 -0
  23. package/src/release/policy.ts +1684 -0
  24. package/src/release/semver.ts +104 -0
  25. package/src/release/smoke.ts +46 -0
  26. package/src/release/wayfinder-dogfood-smoke.ts +760 -0
  27. package/src/retired-topo-command.ts +36 -0
  28. package/src/run-adapter-check.ts +76 -0
  29. package/src/run-collision.ts +126 -0
  30. package/src/run-completions-install.ts +179 -0
  31. package/src/run-example.ts +149 -0
  32. package/src/run-examples.ts +148 -0
  33. package/src/run-quiet.ts +75 -0
  34. package/src/run-release-check.ts +74 -0
  35. package/src/run-schema.ts +41 -0
  36. package/src/run-trace.ts +273 -0
  37. package/src/run-warden.ts +39 -0
  38. package/src/run-watch.ts +432 -0
  39. package/src/run-wayfind-outline.ts +166 -0
  40. package/src/scaffold-version-sync.ts +183 -0
  41. package/src/scaffold-versions.generated.ts +12 -0
  42. package/src/trails/adapter-check.ts +244 -0
  43. package/src/trails/add-surface.ts +94 -40
  44. package/src/trails/add-trail.ts +79 -41
  45. package/src/trails/add-verify.ts +95 -25
  46. package/src/trails/compile.ts +59 -0
  47. package/src/trails/completions-complete.ts +165 -0
  48. package/src/trails/completions.ts +47 -0
  49. package/src/trails/create-adapter.ts +1084 -0
  50. package/src/trails/create-scaffold.ts +399 -104
  51. package/src/trails/create-versions.ts +62 -0
  52. package/src/trails/create.ts +186 -72
  53. package/src/trails/deprecate.ts +59 -0
  54. package/src/trails/dev-clean.ts +82 -0
  55. package/src/trails/dev-reset.ts +50 -0
  56. package/src/trails/dev-stats.ts +72 -0
  57. package/src/trails/dev-support.ts +360 -0
  58. package/src/trails/doctor.ts +77 -0
  59. package/src/trails/draft-promote.ts +949 -0
  60. package/src/trails/guide.ts +71 -69
  61. package/src/trails/load-app.ts +1193 -16
  62. package/src/trails/operator-context.ts +66 -0
  63. package/src/trails/project.ts +17 -3
  64. package/src/trails/regrade.ts +72 -0
  65. package/src/trails/release-check.ts +105 -0
  66. package/src/trails/release-smoke.ts +48 -0
  67. package/src/trails/revise.ts +53 -0
  68. package/src/trails/root-dir.ts +21 -0
  69. package/src/trails/run-example.ts +475 -0
  70. package/src/trails/run-examples.ts +129 -0
  71. package/src/trails/run.ts +434 -0
  72. package/src/trails/scaffold-json.ts +58 -0
  73. package/src/trails/survey.ts +896 -227
  74. package/src/trails/topo-activation.ts +385 -0
  75. package/src/trails/topo-constants.ts +2 -0
  76. package/src/trails/topo-history.ts +47 -0
  77. package/src/trails/topo-output-schemas.ts +259 -0
  78. package/src/trails/topo-pin.ts +38 -0
  79. package/src/trails/topo-read-support.ts +363 -0
  80. package/src/trails/topo-reports.ts +809 -0
  81. package/src/trails/topo-store-support.ts +307 -0
  82. package/src/trails/topo-support.ts +219 -0
  83. package/src/trails/topo-unpin.ts +61 -0
  84. package/src/trails/topo.ts +92 -0
  85. package/src/trails/validate.ts +27 -0
  86. package/src/trails/version-lifecycle-support.ts +935 -0
  87. package/src/trails/warden-guide.ts +129 -0
  88. package/src/trails/warden.ts +183 -58
  89. package/src/trails/wayfind.ts +1001 -0
  90. package/src/versions.ts +31 -0
  91. package/.turbo/turbo-build.log +0 -1
  92. package/.turbo/turbo-lint.log +0 -3
  93. package/.turbo/turbo-typecheck.log +0 -1
  94. package/__tests__/examples.test.ts +0 -6
  95. package/dist/bin/trails.d.ts +0 -3
  96. package/dist/bin/trails.d.ts.map +0 -1
  97. package/dist/bin/trails.js +0 -4
  98. package/dist/bin/trails.js.map +0 -1
  99. package/dist/src/app.d.ts +0 -2
  100. package/dist/src/app.d.ts.map +0 -1
  101. package/dist/src/app.js +0 -11
  102. package/dist/src/app.js.map +0 -1
  103. package/dist/src/clack.d.ts +0 -9
  104. package/dist/src/clack.d.ts.map +0 -1
  105. package/dist/src/clack.js +0 -84
  106. package/dist/src/clack.js.map +0 -1
  107. package/dist/src/cli.d.ts +0 -2
  108. package/dist/src/cli.d.ts.map +0 -1
  109. package/dist/src/cli.js +0 -13
  110. package/dist/src/cli.js.map +0 -1
  111. package/dist/src/trails/add-surface.d.ts +0 -13
  112. package/dist/src/trails/add-surface.d.ts.map +0 -1
  113. package/dist/src/trails/add-surface.js +0 -88
  114. package/dist/src/trails/add-surface.js.map +0 -1
  115. package/dist/src/trails/add-trail.d.ts +0 -11
  116. package/dist/src/trails/add-trail.d.ts.map +0 -1
  117. package/dist/src/trails/add-trail.js +0 -85
  118. package/dist/src/trails/add-trail.js.map +0 -1
  119. package/dist/src/trails/add-verify.d.ts +0 -10
  120. package/dist/src/trails/add-verify.d.ts.map +0 -1
  121. package/dist/src/trails/add-verify.js +0 -67
  122. package/dist/src/trails/add-verify.js.map +0 -1
  123. package/dist/src/trails/create-scaffold.d.ts +0 -15
  124. package/dist/src/trails/create-scaffold.d.ts.map +0 -1
  125. package/dist/src/trails/create-scaffold.js +0 -288
  126. package/dist/src/trails/create-scaffold.js.map +0 -1
  127. package/dist/src/trails/create.d.ts +0 -22
  128. package/dist/src/trails/create.d.ts.map +0 -1
  129. package/dist/src/trails/create.js +0 -121
  130. package/dist/src/trails/create.js.map +0 -1
  131. package/dist/src/trails/guide.d.ts +0 -11
  132. package/dist/src/trails/guide.d.ts.map +0 -1
  133. package/dist/src/trails/guide.js +0 -80
  134. package/dist/src/trails/guide.js.map +0 -1
  135. package/dist/src/trails/load-app.d.ts +0 -4
  136. package/dist/src/trails/load-app.d.ts.map +0 -1
  137. package/dist/src/trails/load-app.js +0 -24
  138. package/dist/src/trails/load-app.js.map +0 -1
  139. package/dist/src/trails/project.d.ts +0 -8
  140. package/dist/src/trails/project.d.ts.map +0 -1
  141. package/dist/src/trails/project.js +0 -43
  142. package/dist/src/trails/project.js.map +0 -1
  143. package/dist/src/trails/survey.d.ts +0 -33
  144. package/dist/src/trails/survey.d.ts.map +0 -1
  145. package/dist/src/trails/survey.js +0 -225
  146. package/dist/src/trails/survey.js.map +0 -1
  147. package/dist/src/trails/warden.d.ts +0 -19
  148. package/dist/src/trails/warden.d.ts.map +0 -1
  149. package/dist/src/trails/warden.js +0 -88
  150. package/dist/src/trails/warden.js.map +0 -1
  151. package/dist/tsconfig.tsbuildinfo +0 -1
  152. package/src/__tests__/create.test.ts +0 -349
  153. package/src/__tests__/guide.test.ts +0 -91
  154. package/src/__tests__/load-app.test.ts +0 -15
  155. package/src/__tests__/survey.test.ts +0 -161
  156. package/src/__tests__/warden.test.ts +0 -74
  157. package/tsconfig.json +0 -9
@@ -0,0 +1,475 @@
1
+ /**
2
+ * `run.example` trail -- run one named example and compare the result.
3
+ */
4
+
5
+ import {
6
+ NotFoundError,
7
+ Result,
8
+ TrailsError,
9
+ deriveStructuredTrailExamples,
10
+ run,
11
+ trail,
12
+ } from '@ontrails/core';
13
+ import type { BasePermit, StructuredTrailExample, Topo } from '@ontrails/core';
14
+ import { z } from 'zod';
15
+
16
+ import { withFreshAppLease, withOperatorRootDir } from './operator-context.js';
17
+ import { resolveRunModulePath } from './run.js';
18
+ import { createIsolatedExampleInput } from './topo-support.js';
19
+
20
+ export const RUN_EXAMPLE_COMPARISON_KIND = 'example-comparison' as const;
21
+
22
+ export const runExampleComparisonSchema = z.object({
23
+ actual: z.unknown(),
24
+ diff: z.array(z.string()).readonly().optional(),
25
+ exampleName: z.string(),
26
+ expected: z.unknown(),
27
+ input: z.unknown(),
28
+ kind: z.literal(RUN_EXAMPLE_COMPARISON_KIND),
29
+ match: z.boolean(),
30
+ mode: z.union([
31
+ z.literal('expected'),
32
+ z.literal('expectedMatch'),
33
+ z.literal('error'),
34
+ z.literal('none'),
35
+ ]),
36
+ trailId: z.string(),
37
+ });
38
+
39
+ export type RunExampleComparison = z.infer<typeof runExampleComparisonSchema>;
40
+ export type RunExampleComparisonMode = RunExampleComparison['mode'];
41
+
42
+ interface ActualOutcomeOk {
43
+ readonly outcome: 'ok';
44
+ readonly value: unknown;
45
+ }
46
+
47
+ interface ActualOutcomeErr {
48
+ readonly errorCategory?: string;
49
+ readonly errorClassName: string;
50
+ readonly errorMessage: string;
51
+ readonly outcome: 'err';
52
+ }
53
+
54
+ type ActualOutcome = ActualOutcomeOk | ActualOutcomeErr;
55
+
56
+ const buildHappyExampleInput = (): {
57
+ readonly exampleName: string;
58
+ readonly id: string;
59
+ readonly module: string;
60
+ readonly rootDir: string;
61
+ } => ({
62
+ ...createIsolatedExampleInput('run-example-happy'),
63
+ exampleName: 'Brief capability report',
64
+ id: 'survey.brief',
65
+ });
66
+
67
+ const projectActual = (result: Result<unknown, Error>): ActualOutcome => {
68
+ if (result.isOk()) {
69
+ return { outcome: 'ok', value: result.value };
70
+ }
71
+ const { error } = result;
72
+ return {
73
+ errorClassName: error.constructor.name,
74
+ errorMessage: error.message,
75
+ outcome: 'err',
76
+ ...(error instanceof TrailsError ? { errorCategory: error.category } : {}),
77
+ };
78
+ };
79
+
80
+ const formatLeaf = (value: unknown): string => {
81
+ try {
82
+ const encoded = JSON.stringify(value);
83
+ return encoded === undefined ? String(value) : encoded;
84
+ } catch {
85
+ return String(value);
86
+ }
87
+ };
88
+
89
+ const formatPath = (segments: readonly string[]): string =>
90
+ segments.length === 0 ? 'value' : `value.${segments.join('.')}`;
91
+
92
+ const isPlainObject = (value: unknown): value is Record<string, unknown> =>
93
+ typeof value === 'object' && value !== null && !Array.isArray(value);
94
+
95
+ const deepEqualWithDiff = (
96
+ actual: unknown,
97
+ expected: unknown,
98
+ path: readonly string[],
99
+ diffs: string[]
100
+ ): boolean => {
101
+ if (Array.isArray(expected)) {
102
+ if (!Array.isArray(actual)) {
103
+ diffs.push(`${formatPath(path)}: expected array, got ${typeof actual}`);
104
+ return false;
105
+ }
106
+ if (actual.length !== expected.length) {
107
+ diffs.push(
108
+ `${formatPath(path)}: array length ${actual.length} != ${expected.length}`
109
+ );
110
+ return false;
111
+ }
112
+ let ok = true;
113
+ for (let i = 0; i < expected.length; i += 1) {
114
+ if (
115
+ !deepEqualWithDiff(actual[i], expected[i], [...path, `[${i}]`], diffs)
116
+ ) {
117
+ ok = false;
118
+ }
119
+ }
120
+ return ok;
121
+ }
122
+
123
+ if (isPlainObject(expected)) {
124
+ if (!isPlainObject(actual)) {
125
+ diffs.push(`${formatPath(path)}: expected object, got ${typeof actual}`);
126
+ return false;
127
+ }
128
+ let ok = true;
129
+ for (const key of Object.keys(expected)) {
130
+ if (!(key in actual)) {
131
+ diffs.push(`${formatPath([...path, key])}: missing in actual`);
132
+ ok = false;
133
+ continue;
134
+ }
135
+ if (
136
+ !deepEqualWithDiff(actual[key], expected[key], [...path, key], diffs)
137
+ ) {
138
+ ok = false;
139
+ }
140
+ }
141
+ for (const key of Object.keys(actual)) {
142
+ if (!(key in expected)) {
143
+ diffs.push(`${formatPath([...path, key])}: unexpected key in actual`);
144
+ ok = false;
145
+ }
146
+ }
147
+ return ok;
148
+ }
149
+
150
+ if (actual !== expected) {
151
+ if (
152
+ typeof actual === 'number' &&
153
+ typeof expected === 'number' &&
154
+ Number.isNaN(actual) &&
155
+ Number.isNaN(expected)
156
+ ) {
157
+ return true;
158
+ }
159
+ diffs.push(
160
+ `${formatPath(path)}: ${formatLeaf(actual)} != ${formatLeaf(expected)}`
161
+ );
162
+ return false;
163
+ }
164
+ return true;
165
+ };
166
+
167
+ const partialMatchWithDiff = (
168
+ actual: unknown,
169
+ expected: unknown,
170
+ path: readonly string[],
171
+ diffs: string[]
172
+ ): boolean => {
173
+ if (Array.isArray(expected)) {
174
+ if (!Array.isArray(actual)) {
175
+ diffs.push(`${formatPath(path)}: expected array, got ${typeof actual}`);
176
+ return false;
177
+ }
178
+ let ok = true;
179
+ const consumed = new Set<number>();
180
+ for (const [index, expectedEntry] of expected.entries()) {
181
+ const matchIndex = actual.findIndex((candidate, candidateIndex) => {
182
+ if (consumed.has(candidateIndex)) {
183
+ return false;
184
+ }
185
+ const probe: string[] = [];
186
+ return partialMatchWithDiff(candidate, expectedEntry, [], probe);
187
+ });
188
+ if (matchIndex === -1) {
189
+ diffs.push(
190
+ `${formatPath([...path, `[${index}]`])}: expected array to contain ${formatLeaf(expectedEntry)}`
191
+ );
192
+ ok = false;
193
+ continue;
194
+ }
195
+ consumed.add(matchIndex);
196
+ }
197
+ return ok;
198
+ }
199
+
200
+ if (isPlainObject(expected)) {
201
+ if (!isPlainObject(actual)) {
202
+ diffs.push(`${formatPath(path)}: expected object, got ${typeof actual}`);
203
+ return false;
204
+ }
205
+ let ok = true;
206
+ for (const key of Object.keys(expected)) {
207
+ if (!(key in actual)) {
208
+ diffs.push(`${formatPath([...path, key])}: missing in actual`);
209
+ ok = false;
210
+ continue;
211
+ }
212
+ if (
213
+ !partialMatchWithDiff(actual[key], expected[key], [...path, key], diffs)
214
+ ) {
215
+ ok = false;
216
+ }
217
+ }
218
+ return ok;
219
+ }
220
+
221
+ if (actual !== expected) {
222
+ diffs.push(
223
+ `${formatPath(path)}: ${formatLeaf(actual)} != ${formatLeaf(expected)}`
224
+ );
225
+ return false;
226
+ }
227
+ return true;
228
+ };
229
+
230
+ const compareExpected = (
231
+ result: Result<unknown, Error>,
232
+ expected: unknown
233
+ ): {
234
+ readonly diff?: readonly string[] | undefined;
235
+ readonly match: boolean;
236
+ } => {
237
+ if (result.isErr()) {
238
+ return {
239
+ diff: [
240
+ `value: expected Result.ok(...), got Result.err(${result.error.constructor.name}: ${result.error.message})`,
241
+ ],
242
+ match: false,
243
+ };
244
+ }
245
+ const diffs: string[] = [];
246
+ const match = deepEqualWithDiff(result.value, expected, [], diffs);
247
+ return { diff: match ? undefined : diffs, match };
248
+ };
249
+
250
+ const compareExpectedMatch = (
251
+ result: Result<unknown, Error>,
252
+ expectedMatch: unknown
253
+ ): {
254
+ readonly diff?: readonly string[] | undefined;
255
+ readonly match: boolean;
256
+ } => {
257
+ if (result.isErr()) {
258
+ return {
259
+ diff: [
260
+ `value: expected Result.ok(...), got Result.err(${result.error.constructor.name}: ${result.error.message})`,
261
+ ],
262
+ match: false,
263
+ };
264
+ }
265
+ const diffs: string[] = [];
266
+ const match = partialMatchWithDiff(result.value, expectedMatch, [], diffs);
267
+ return { diff: match ? undefined : diffs, match };
268
+ };
269
+
270
+ const compareError = (
271
+ result: Result<unknown, Error>,
272
+ expectedErrorName: string
273
+ ): {
274
+ readonly diff?: readonly string[] | undefined;
275
+ readonly match: boolean;
276
+ } => {
277
+ if (result.isOk()) {
278
+ return {
279
+ diff: [
280
+ `value: expected Result.err(${expectedErrorName}), got Result.ok(${formatLeaf(result.value)})`,
281
+ ],
282
+ match: false,
283
+ };
284
+ }
285
+ const className = result.error.constructor.name;
286
+ if (className === expectedErrorName) {
287
+ return { diff: undefined, match: true };
288
+ }
289
+ return {
290
+ diff: [
291
+ `value: expected Result.err(${expectedErrorName}), got Result.err(${className}: ${result.error.message})`,
292
+ ],
293
+ match: false,
294
+ };
295
+ };
296
+
297
+ const findExample = (
298
+ app: Topo,
299
+ trailId: string,
300
+ exampleName: string
301
+ ): Result<StructuredTrailExample, Error> => {
302
+ const target = app.get(trailId);
303
+ if (target === undefined) {
304
+ return Result.err(
305
+ new NotFoundError(
306
+ `Trail '${trailId}' was not found in the resolved app.`,
307
+ { context: { trailId } }
308
+ )
309
+ );
310
+ }
311
+
312
+ const structured = deriveStructuredTrailExamples(target.examples) ?? [];
313
+ const match = structured.find((entry) => entry.name === exampleName);
314
+ if (match !== undefined) {
315
+ return Result.ok(match);
316
+ }
317
+
318
+ const available = structured.map((entry) => entry.name);
319
+ const listing = available.length === 0 ? '<none>' : available.join(', ');
320
+ return Result.err(
321
+ new NotFoundError(
322
+ `Example '${exampleName}' not found on trail '${trailId}'. Available: ${listing}.`,
323
+ {
324
+ context: {
325
+ available,
326
+ exampleName,
327
+ trailId,
328
+ },
329
+ }
330
+ )
331
+ );
332
+ };
333
+
334
+ const determineMode = (
335
+ example: StructuredTrailExample
336
+ ): RunExampleComparisonMode => {
337
+ if (example.error !== undefined) {
338
+ return 'error';
339
+ }
340
+ if (example.expectedMatch !== undefined) {
341
+ return 'expectedMatch';
342
+ }
343
+ if (example.expected !== undefined) {
344
+ return 'expected';
345
+ }
346
+ return 'none';
347
+ };
348
+
349
+ const buildComparisonEnvelope = async (
350
+ app: Topo,
351
+ trailId: string,
352
+ exampleName: string,
353
+ permit: BasePermit | undefined
354
+ ): Promise<Result<RunExampleComparison, Error>> => {
355
+ const exampleResult = findExample(app, trailId, exampleName);
356
+ if (exampleResult.isErr()) {
357
+ return exampleResult;
358
+ }
359
+ const example = exampleResult.value;
360
+ const mode = determineMode(example);
361
+ const executed = await run(app, trailId, example.input, {
362
+ ctx: permit === undefined ? {} : { permit },
363
+ });
364
+ const actual = projectActual(executed);
365
+
366
+ if (mode === 'error') {
367
+ const expectedName = example.error ?? '';
368
+ const { diff, match } = compareError(executed, expectedName);
369
+ return Result.ok({
370
+ actual,
371
+ diff,
372
+ exampleName,
373
+ expected: { errorClassName: expectedName },
374
+ input: example.input,
375
+ kind: RUN_EXAMPLE_COMPARISON_KIND,
376
+ match,
377
+ mode,
378
+ trailId,
379
+ });
380
+ }
381
+ if (mode === 'expectedMatch') {
382
+ const { diff, match } = compareExpectedMatch(
383
+ executed,
384
+ example.expectedMatch
385
+ );
386
+ return Result.ok({
387
+ actual,
388
+ diff,
389
+ exampleName,
390
+ expected: example.expectedMatch,
391
+ input: example.input,
392
+ kind: RUN_EXAMPLE_COMPARISON_KIND,
393
+ match,
394
+ mode,
395
+ trailId,
396
+ });
397
+ }
398
+ if (mode === 'none') {
399
+ return Result.ok({
400
+ actual,
401
+ exampleName,
402
+ expected: undefined,
403
+ input: example.input,
404
+ kind: RUN_EXAMPLE_COMPARISON_KIND,
405
+ match: true,
406
+ mode,
407
+ trailId,
408
+ });
409
+ }
410
+
411
+ const { diff, match } = compareExpected(executed, example.expected);
412
+ return Result.ok({
413
+ actual,
414
+ diff,
415
+ exampleName,
416
+ expected: example.expected,
417
+ input: example.input,
418
+ kind: RUN_EXAMPLE_COMPARISON_KIND,
419
+ match,
420
+ mode,
421
+ trailId,
422
+ });
423
+ };
424
+
425
+ const runExampleTrailInputSchema = z.object({
426
+ app: z
427
+ .string()
428
+ .optional()
429
+ .describe(
430
+ 'Workspace app to resolve the trail ID against; required when the ID is exposed by more than one app'
431
+ ),
432
+ exampleName: z.string().describe('Name of the example to run'),
433
+ id: z.string().describe('Trail ID whose example should run'),
434
+ module: z.string().optional().describe('Path to the app module'),
435
+ rootDir: z.string().optional().describe('Workspace root directory'),
436
+ });
437
+
438
+ type RunExampleTrailInput = z.output<typeof runExampleTrailInputSchema>;
439
+
440
+ export const runExampleTrail = trail('run.example', {
441
+ args: ['id', 'exampleName'],
442
+ blaze: async (input: RunExampleTrailInput, ctx) =>
443
+ withOperatorRootDir(input, ctx, async (rootDir) => {
444
+ const moduleResolution = await resolveRunModulePath(
445
+ rootDir,
446
+ input.module,
447
+ input.id,
448
+ input.app
449
+ );
450
+ if (moduleResolution.isErr()) {
451
+ return moduleResolution;
452
+ }
453
+
454
+ return withFreshAppLease(moduleResolution.value, rootDir, (lease) =>
455
+ buildComparisonEnvelope(
456
+ lease.app,
457
+ input.id,
458
+ input.exampleName,
459
+ ctx.permit
460
+ )
461
+ );
462
+ }),
463
+ description: 'Run a named example on a trail and compare actual vs expected',
464
+ examples: [
465
+ {
466
+ description: 'Run a named example on a target trail',
467
+ input: buildHappyExampleInput(),
468
+ name: 'Run named example',
469
+ },
470
+ ],
471
+ input: runExampleTrailInputSchema,
472
+ intent: 'write',
473
+ output: runExampleComparisonSchema,
474
+ permit: { scopes: ['trails:run'] },
475
+ });
@@ -0,0 +1,129 @@
1
+ /**
2
+ * `run.examples` trail -- list examples for a target trail.
3
+ */
4
+
5
+ import {
6
+ NotFoundError,
7
+ Result,
8
+ deriveStructuredTrailExamples,
9
+ trail,
10
+ } from '@ontrails/core';
11
+ import type { StructuredTrailExample, Topo } from '@ontrails/core';
12
+ import { z } from 'zod';
13
+
14
+ import { withFreshAppLease, withOperatorRootDir } from './operator-context.js';
15
+ import { resolveRunModulePath } from './run.js';
16
+ import { createIsolatedExampleInput } from './topo-support.js';
17
+
18
+ export const RUN_EXAMPLES_LISTING_KIND = 'examples-listing' as const;
19
+
20
+ export const structuredTrailExampleSchema = z
21
+ .object({
22
+ description: z.string().optional(),
23
+ error: z.string().optional(),
24
+ expected: z.unknown().optional(),
25
+ expectedMatch: z.unknown().optional(),
26
+ input: z.unknown(),
27
+ kind: z.union([z.literal('success'), z.literal('error')]),
28
+ name: z.string(),
29
+ provenance: z.object({ source: z.literal('trail.examples') }),
30
+ signals: z
31
+ .array(
32
+ z.object({
33
+ payload: z.unknown().optional(),
34
+ payloadMatch: z.unknown().optional(),
35
+ signalId: z.string(),
36
+ times: z.number().optional(),
37
+ })
38
+ )
39
+ .readonly()
40
+ .optional(),
41
+ })
42
+ .passthrough();
43
+
44
+ export const runExamplesListingSchema = z.object({
45
+ examples: z.array(structuredTrailExampleSchema).readonly(),
46
+ kind: z.literal(RUN_EXAMPLES_LISTING_KIND),
47
+ trailId: z.string(),
48
+ });
49
+
50
+ export type RunExamplesListing = z.infer<typeof runExamplesListingSchema>;
51
+
52
+ const buildHappyExampleInput = (): {
53
+ readonly id: string;
54
+ readonly module: string;
55
+ readonly rootDir: string;
56
+ } => ({
57
+ ...createIsolatedExampleInput('run-examples-happy'),
58
+ id: 'survey.brief',
59
+ });
60
+
61
+ const buildExamplesListing = (
62
+ app: Topo,
63
+ trailId: string
64
+ ): Result<RunExamplesListing, Error> => {
65
+ const target = app.get(trailId);
66
+ if (target === undefined) {
67
+ return Result.err(
68
+ new NotFoundError(
69
+ `Trail '${trailId}' was not found in the resolved app.`,
70
+ { context: { trailId } }
71
+ )
72
+ );
73
+ }
74
+
75
+ const structured =
76
+ (deriveStructuredTrailExamples(target.examples) as
77
+ | readonly StructuredTrailExample[]
78
+ | undefined) ?? [];
79
+ return Result.ok({
80
+ examples: structured as unknown as RunExamplesListing['examples'],
81
+ kind: RUN_EXAMPLES_LISTING_KIND,
82
+ trailId,
83
+ });
84
+ };
85
+
86
+ const runExamplesTrailInputSchema = z.object({
87
+ app: z
88
+ .string()
89
+ .optional()
90
+ .describe(
91
+ 'Workspace app to resolve the trail ID against; required when the ID is exposed by more than one app'
92
+ ),
93
+ id: z.string().describe('Trail ID whose examples should be listed'),
94
+ module: z.string().optional().describe('Path to the app module'),
95
+ rootDir: z.string().optional().describe('Workspace root directory'),
96
+ });
97
+
98
+ type RunExamplesTrailInput = z.output<typeof runExamplesTrailInputSchema>;
99
+
100
+ export const runExamplesTrail = trail('run.examples', {
101
+ args: ['id'],
102
+ blaze: async (input: RunExamplesTrailInput, ctx) =>
103
+ withOperatorRootDir(input, ctx, async (rootDir) => {
104
+ const moduleResolution = await resolveRunModulePath(
105
+ rootDir,
106
+ input.module,
107
+ input.id,
108
+ input.app
109
+ );
110
+ if (moduleResolution.isErr()) {
111
+ return moduleResolution;
112
+ }
113
+
114
+ return withFreshAppLease(moduleResolution.value, rootDir, (lease) =>
115
+ buildExamplesListing(lease.app, input.id)
116
+ );
117
+ }),
118
+ description: "List a trail's examples without executing it",
119
+ examples: [
120
+ {
121
+ description: 'List examples authored on a target trail',
122
+ input: buildHappyExampleInput(),
123
+ name: 'List trail examples',
124
+ },
125
+ ],
126
+ input: runExamplesTrailInputSchema,
127
+ intent: 'read',
128
+ output: runExamplesListingSchema,
129
+ });