@ontrails/trails 1.0.0-beta.2 → 1.0.0-beta.22

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