@skmtc/core 0.36.1 → 0.38.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 (67) hide show
  1. package/esm/_dnt.polyfills.d.ts +6 -6
  2. package/esm/anchors/postPass.d.ts +6 -3
  3. package/esm/anchors/postPass.js +6 -3
  4. package/esm/context/CoreContext.d.ts +10 -2
  5. package/esm/context/CoreContext.js +158 -87
  6. package/esm/context/GenerateContext.d.ts +11 -0
  7. package/esm/context/GenerateContext.js +44 -16
  8. package/esm/context/LogLinesHandler.d.ts +28 -0
  9. package/esm/context/LogLinesHandler.js +65 -0
  10. package/esm/context/ParseContext.js +23 -4
  11. package/esm/context/ParseIssue.d.ts +12 -9
  12. package/esm/context/ParseIssue.js +7 -6
  13. package/esm/context/RenderContext.js +47 -65
  14. package/esm/context/ResultsHandler.d.ts +5 -3
  15. package/esm/context/ResultsHandler.js +11 -5
  16. package/esm/context/StackTrail.d.ts +14 -6
  17. package/esm/context/StackTrail.js +77 -29
  18. package/esm/context/documentTrail.d.ts +7 -3
  19. package/esm/context/documentTrail.js +6 -2
  20. package/esm/context/generateTypes.d.ts +21 -2
  21. package/esm/context/generateTypes.js +2 -1
  22. package/esm/helpers/ResultsLog.d.ts +2 -2
  23. package/esm/helpers/ResultsLog.js +7 -4
  24. package/esm/mod.d.ts +2 -0
  25. package/esm/mod.js +2 -0
  26. package/esm/run/toArtifacts.d.ts +9 -1
  27. package/esm/run/toArtifacts.js +4 -4
  28. package/esm/types/LogLine.d.ts +36 -0
  29. package/esm/types/LogLine.js +26 -0
  30. package/esm/types/Manifest.d.ts +1 -1
  31. package/esm/types/Manifest.js +16 -4
  32. package/esm/types/SerializedError.d.ts +25 -0
  33. package/esm/types/SerializedError.js +50 -0
  34. package/package.json +1 -1
  35. package/script/_dnt.polyfills.d.ts +6 -6
  36. package/script/anchors/postPass.d.ts +6 -3
  37. package/script/anchors/postPass.js +6 -3
  38. package/script/context/CoreContext.d.ts +10 -2
  39. package/script/context/CoreContext.js +158 -87
  40. package/script/context/GenerateContext.d.ts +11 -0
  41. package/script/context/GenerateContext.js +44 -16
  42. package/script/context/LogLinesHandler.d.ts +28 -0
  43. package/script/context/LogLinesHandler.js +69 -0
  44. package/script/context/ParseContext.js +23 -4
  45. package/script/context/ParseIssue.d.ts +12 -9
  46. package/script/context/ParseIssue.js +7 -6
  47. package/script/context/RenderContext.js +47 -65
  48. package/script/context/ResultsHandler.d.ts +5 -3
  49. package/script/context/ResultsHandler.js +11 -5
  50. package/script/context/StackTrail.d.ts +14 -6
  51. package/script/context/StackTrail.js +76 -28
  52. package/script/context/documentTrail.d.ts +7 -3
  53. package/script/context/documentTrail.js +9 -3
  54. package/script/context/generateTypes.d.ts +21 -2
  55. package/script/context/generateTypes.js +2 -1
  56. package/script/helpers/ResultsLog.d.ts +2 -2
  57. package/script/helpers/ResultsLog.js +7 -4
  58. package/script/mod.d.ts +2 -0
  59. package/script/mod.js +2 -0
  60. package/script/run/toArtifacts.d.ts +9 -1
  61. package/script/run/toArtifacts.js +4 -4
  62. package/script/types/LogLine.d.ts +36 -0
  63. package/script/types/LogLine.js +62 -0
  64. package/script/types/Manifest.d.ts +1 -1
  65. package/script/types/Manifest.js +16 -4
  66. package/script/types/SerializedError.d.ts +25 -0
  67. package/script/types/SerializedError.js +87 -0
@@ -1,3 +1,9 @@
1
+ declare global {
2
+ interface Error {
3
+ cause?: unknown;
4
+ }
5
+ }
6
+ export {};
1
7
  declare global {
2
8
  interface Object {
3
9
  /**
@@ -9,12 +15,6 @@ declare global {
9
15
  }
10
16
  }
11
17
  export {};
12
- declare global {
13
- interface Error {
14
- cause?: unknown;
15
- }
16
- }
17
- export {};
18
18
  /**
19
19
  * Based on [import-meta-ponyfill](https://github.com/gaubee/import-meta-ponyfill),
20
20
  * but instead of using npm to install additional dependencies,
@@ -4,9 +4,12 @@
4
4
  * AST-resolve each span's landmark + path, and build the sidecar.
5
5
  *
6
6
  * Pure function over its inputs — no I/O, no global state, no `toString`,
7
- * no pipeline hooks. The caller is `RenderContext` (folded in after the
8
- * single capture render), running this once per File when the run supplies
9
- * `attribution.postPass`. The CLI handles disk writes via a separate
7
+ * no pipeline hooks. The caller is `RenderContext`, which runs this right
8
+ * after each File renders, once per File, when the run supplies
9
+ * `attribution.postPass`. The capture channel's sink is still set at that
10
+ * point; it records nothing only because the sink is outside a
11
+ * `captureFile` call between files. So do not render snippets or call
12
+ * `toString` here. The CLI handles disk writes via a separate
10
13
  * `writeSidecar` wrapper.
11
14
  */
12
15
  import type { ParserAdapter } from './ParserAdapter.js';
@@ -4,9 +4,12 @@
4
4
  * AST-resolve each span's landmark + path, and build the sidecar.
5
5
  *
6
6
  * Pure function over its inputs — no I/O, no global state, no `toString`,
7
- * no pipeline hooks. The caller is `RenderContext` (folded in after the
8
- * single capture render), running this once per File when the run supplies
9
- * `attribution.postPass`. The CLI handles disk writes via a separate
7
+ * no pipeline hooks. The caller is `RenderContext`, which runs this right
8
+ * after each File renders, once per File, when the run supplies
9
+ * `attribution.postPass`. The capture channel's sink is still set at that
10
+ * point; it records nothing only because the sink is outside a
11
+ * `captureFile` call between files. So do not render snippets or call
12
+ * `toString` here. The CLI handles disk writes via a separate
10
13
  * `writeSidecar` wrapper.
11
14
  */
12
15
  import { attribute } from './attribute.js';
@@ -5,7 +5,8 @@ import type { OasDocument } from '../oas/document/Document.js';
5
5
  import type { ClientSettings } from '../types/Settings.js';
6
6
  import type { ResultType } from '../types/Results.js';
7
7
  import type { Logger } from '../types/Logger.js';
8
- import type { StackTrail } from './StackTrail.js';
8
+ import type { LogLineLevel } from '../types/LogLine.js';
9
+ import { StackTrail } from './StackTrail.js';
9
10
  import type { GeneratorsMapContainer } from '../types/GeneratorType.js';
10
11
  import type { OpenAPIV3 } from 'openapi-types';
11
12
  import type { ToArtifactsResult } from './generateTypes.js';
@@ -62,6 +63,8 @@ type CoreContextArgs = {
62
63
  spanId: string;
63
64
  logsPath?: string;
64
65
  silent: boolean;
66
+ /** The lowest level kept in the returned `logs`. Defaults to `'WARN'`. */
67
+ logLevel?: LogLineLevel;
65
68
  };
66
69
  /**
67
70
  * Arguments for the `toArtifacts` method of CoreContext.
@@ -182,7 +185,12 @@ export declare class CoreContext {
182
185
  * });
183
186
  * ```
184
187
  */
185
- constructor({ spanId, logsPath, silent }: CoreContextArgs);
188
+ constructor({ spanId, logsPath, silent, logLevel }: CoreContextArgs);
189
+ /**
190
+ * The trail of the subject being generated, or `undefined` outside one
191
+ * (parse, render, and generate work that is not a subject's variant).
192
+ */
193
+ get currentSubjectTrail(): StackTrail | undefined;
186
194
  /**
187
195
  * Parses an OpenAPI v3 document into internal OAS objects.
188
196
  *
@@ -9,16 +9,19 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _CoreContext_instances, _CoreContext_phase, _CoreContext_results, _CoreContext_setupLogger, _CoreContext_setupParsePhase, _CoreContext_setupGeneratePhase, _CoreContext_setupRenderPhase;
12
+ var _CoreContext_instances, _CoreContext_phase, _CoreContext_results, _CoreContext_logLines, _CoreContext_phaseName, _CoreContext_parseIssues, _CoreContext_setupLogger, _CoreContext_toLogTrail, _CoreContext_closeFileHandlers, _CoreContext_toRunFailedIssue, _CoreContext_setupParsePhase, _CoreContext_setupGeneratePhase, _CoreContext_setupRenderPhase;
13
13
  import { GenerateContext } from './GenerateContext.js';
14
14
  import { toInspection } from '../run/toInspection.js';
15
15
  import { RenderContext } from './RenderContext.js';
16
16
  import { ParseContext } from './ParseContext.js';
17
17
  import * as log from '../deps/jsr.io/@std/log/0.224.14/mod.js';
18
18
  import { ResultsHandler } from './ResultsHandler.js';
19
+ import { LogLinesHandler } from './LogLinesHandler.js';
20
+ import { StackTrail } from './StackTrail.js';
19
21
  import { ResultsLog } from '../helpers/ResultsLog.js';
20
22
  import { join } from '../deps/jsr.io/@std/path/1.1.6/join.js';
21
23
  import { bold, gray, red, yellow, blue } from '../deps/jsr.io/@std/fmt/1.0.10/colors.js';
24
+ import { toSerializedError } from '../types/SerializedError.js';
22
25
  /**
23
26
  * The main orchestration class for the SKMTC transformation pipeline.
24
27
  *
@@ -90,7 +93,7 @@ export class CoreContext {
90
93
  * });
91
94
  * ```
92
95
  */
93
- constructor({ spanId, logsPath, silent }) {
96
+ constructor({ spanId, logsPath, silent, logLevel = 'WARN' }) {
94
97
  _CoreContext_instances.add(this);
95
98
  /** Logger instance for the context */
96
99
  Object.defineProperty(this, "logger", {
@@ -110,10 +113,62 @@ export class CoreContext {
110
113
  writable: true,
111
114
  value: void 0
112
115
  });
116
+ /** Keeps this run's log lines for `toArtifacts` to return. */
117
+ _CoreContext_logLines.set(this, void 0);
118
+ /**
119
+ * The phase that is running. Set when a phase starts, before its
120
+ * context is set up, so a failure names the phase it happened in.
121
+ */
122
+ _CoreContext_phaseName.set(this, 'parse'
123
+ /**
124
+ * The issues of this run's parse context, kept after the generate and
125
+ * render phases replace `#phase`, so a later failure still returns them.
126
+ */
127
+ );
128
+ /**
129
+ * The issues of this run's parse context, kept after the generate and
130
+ * render phases replace `#phase`, so a later failure still returns them.
131
+ */
132
+ _CoreContext_parseIssues.set(this, []
133
+ /**
134
+ * Creates a new CoreContext instance.
135
+ *
136
+ * @param args - Configuration for the context
137
+ * @param args.spanId - Unique identifier for this transformation span
138
+ * @param args.logsPath - Optional directory path for log files
139
+ * @param args.silent - Whether to suppress console output
140
+ *
141
+ * @example
142
+ * ```typescript
143
+ * // Basic context with console logging
144
+ * const context = new CoreContext({
145
+ * spanId: 'my-generation',
146
+ * silent: false
147
+ * });
148
+ *
149
+ * // Context with file logging
150
+ * const context = new CoreContext({
151
+ * spanId: 'batch-job-123',
152
+ * logsPath: './logs/generation',
153
+ * silent: true
154
+ * });
155
+ * ```
156
+ */
157
+ );
113
158
  __classPrivateFieldSet(this, _CoreContext_results, new ResultsLog(), "f");
114
- this.logger = __classPrivateFieldGet(this, _CoreContext_instances, "m", _CoreContext_setupLogger).call(this, { spanId, logsPath });
159
+ __classPrivateFieldSet(this, _CoreContext_logLines, new LogLinesHandler(logLevel, {
160
+ toTrail: () => this.currentSubjectTrail?.toString()
161
+ }), "f");
162
+ this.logger = __classPrivateFieldGet(this, _CoreContext_instances, "m", _CoreContext_setupLogger).call(this, { spanId, logsPath, logLines: __classPrivateFieldGet(this, _CoreContext_logLines, "f") });
115
163
  this.silent = silent;
116
164
  }
165
+ /**
166
+ * The trail of the subject being generated, or `undefined` outside one
167
+ * (parse, render, and generate work that is not a subject's variant).
168
+ */
169
+ get currentSubjectTrail() {
170
+ return __classPrivateFieldGet(this, _CoreContext_phase, "f")?.type === 'generate' ? __classPrivateFieldGet(this, _CoreContext_phase, "f").context.currentSubjectTrail : undefined;
171
+ }
117
172
  /**
118
173
  * Parses an OpenAPI v3 document into internal OAS objects.
119
174
  *
@@ -226,8 +281,12 @@ export class CoreContext {
226
281
  // Parse phase: one unified ParseContext handles both protocols
227
282
  // via its internal protocol-discriminated state. `parse()` returns
228
283
  // a SkmtcParsedDocument; we collect issues from `context.issues`.
284
+ // A throw while the parse context is set up counts as parse.
285
+ __classPrivateFieldSet(this, _CoreContext_phaseName, 'parse', "f");
286
+ __classPrivateFieldSet(this, _CoreContext_parseIssues, [], "f");
229
287
  const phase = __classPrivateFieldGet(this, _CoreContext_instances, "m", _CoreContext_setupParsePhase).call(this, document);
230
288
  __classPrivateFieldSet(this, _CoreContext_phase, phase, "f");
289
+ __classPrivateFieldSet(this, _CoreContext_parseIssues, phase.context.issues, "f");
231
290
  const parsedDocument = stackTrail.trace('parse', st => phase.context.parse(st));
232
291
  // The shared attribution capture channel: snippets constructed
233
292
  // during generate hold the GenerateContext, whose `captureSink`
@@ -235,6 +294,7 @@ export class CoreContext {
235
294
  // render. One object per run wires the two phases together.
236
295
  const captureChannel = { sink: undefined };
237
296
  const { files, previews, inspection, enrichmentWarnings } = stackTrail.trace('generate', st => {
297
+ __classPrivateFieldSet(this, _CoreContext_phaseName, 'generate', "f");
238
298
  const generatePhase = __classPrivateFieldGet(this, _CoreContext_instances, "m", _CoreContext_setupGeneratePhase).call(this, {
239
299
  toGeneratorConfigMap,
240
300
  document: parsedDocument,
@@ -257,6 +317,7 @@ export class CoreContext {
257
317
  // post-pass, no re-render — the old `_rendered` cache that faked
258
318
  // "render once" across two passes is gone.
259
319
  const renderOutput = stackTrail.trace('render', st => {
320
+ __classPrivateFieldSet(this, _CoreContext_phaseName, 'render', "f");
260
321
  __classPrivateFieldSet(this, _CoreContext_phase, __classPrivateFieldGet(this, _CoreContext_instances, "m", _CoreContext_setupRenderPhase).call(this, {
261
322
  files,
262
323
  previews,
@@ -271,51 +332,38 @@ export class CoreContext {
271
332
  results: __classPrivateFieldGet(this, _CoreContext_results, "f").toTree(),
272
333
  parseIssues: phase.context.issues,
273
334
  enrichmentWarnings,
274
- inspection
335
+ inspection,
336
+ logs: [...__classPrivateFieldGet(this, _CoreContext_logLines, "f").lines]
275
337
  };
276
338
  }
277
339
  catch (error) {
278
340
  console.error(error);
279
- this.logger.error(error);
341
+ this.logger.error(toSerializedError(error).message, error);
280
342
  // Surface the fatal error as a synthesized parse-issue so
281
343
  // downstream consumers (CLI `generate`, the manifest writer)
282
344
  // can tell a successful 0-file run apart from a crashed run.
283
- // Before this, the catch returned `parseIssues: []` and an
284
- // empty `artifacts`, which read on the CLI side as "everything
285
- // generated, just nothing emitted" — silent failure.
286
345
  //
287
- // We pull whatever parse-phase issues were already recorded
288
- // before the throw, so any incremental diagnostics aren't lost,
289
- // and append the top-level failure on the end. Protocol is
290
- // hardcoded to 'oas' because the synthesized issue is
291
- // post-protocol-dispatch and we don't have a discriminator at
292
- // this catch site; OAS is the more common case and the
293
- // location format ('toArtifacts') is unambiguous regardless.
294
- const priorIssues = __classPrivateFieldGet(this, _CoreContext_phase, "f")?.type === 'parse' ? __classPrivateFieldGet(this, _CoreContext_phase, "f").context.issues : [];
295
- const message = error instanceof Error ? error.message : String(error);
296
- const fatalIssue = {
297
- protocol: 'oas',
298
- level: 'error',
299
- type: 'INVALID_SCHEMA',
300
- location: 'toArtifacts',
301
- message: `Top-level toArtifacts failure: ${message}`,
302
- cause: error
303
- };
346
+ // The parse issues recorded before the throw are kept, in any
347
+ // phase, so incremental diagnostics aren't lost, and the
348
+ // top-level failure is appended on the end.
349
+ const priorIssues = __classPrivateFieldGet(this, _CoreContext_parseIssues, "f");
350
+ const fatalIssue = __classPrivateFieldGet(this, _CoreContext_instances, "m", _CoreContext_toRunFailedIssue).call(this, {
351
+ entry: 'toArtifacts',
352
+ protocol: document.type,
353
+ error
354
+ });
304
355
  return {
305
356
  artifacts: {},
306
357
  files: {},
307
358
  previews: {},
308
359
  results: __classPrivateFieldGet(this, _CoreContext_results, "f").toTree(),
309
360
  parseIssues: [...priorIssues, fatalIssue],
310
- enrichmentWarnings: []
361
+ enrichmentWarnings: [],
362
+ logs: [...__classPrivateFieldGet(this, _CoreContext_logLines, "f").lines]
311
363
  };
312
364
  }
313
365
  finally {
314
- this.logger.handlers.forEach(handler => {
315
- if (handler instanceof log.FileHandler) {
316
- handler.flush();
317
- }
318
- });
366
+ __classPrivateFieldGet(this, _CoreContext_instances, "m", _CoreContext_closeFileHandlers).call(this);
319
367
  }
320
368
  }
321
369
  /**
@@ -325,10 +373,14 @@ export class CoreContext {
325
373
  */
326
374
  toSupportedSubjects({ document, settings, toGeneratorConfigMap, stackTrail }) {
327
375
  try {
376
+ __classPrivateFieldSet(this, _CoreContext_phaseName, 'parse', "f");
377
+ __classPrivateFieldSet(this, _CoreContext_parseIssues, [], "f");
328
378
  const phase = __classPrivateFieldGet(this, _CoreContext_instances, "m", _CoreContext_setupParsePhase).call(this, document);
329
379
  __classPrivateFieldSet(this, _CoreContext_phase, phase, "f");
380
+ __classPrivateFieldSet(this, _CoreContext_parseIssues, phase.context.issues, "f");
330
381
  const parsedDocument = stackTrail.trace('parse', st => phase.context.parse(st));
331
382
  const subjects = stackTrail.trace('generate', () => {
383
+ __classPrivateFieldSet(this, _CoreContext_phaseName, 'generate', "f");
332
384
  // Capability probing never renders, so the capture interval never
333
385
  // opens — a throwaway channel satisfies the wiring.
334
386
  const generatePhase = __classPrivateFieldGet(this, _CoreContext_instances, "m", _CoreContext_setupGeneratePhase).call(this, {
@@ -343,24 +395,19 @@ export class CoreContext {
343
395
  return { subjects, parseIssues: phase.context.issues };
344
396
  }
345
397
  catch (error) {
346
- this.logger.error(error);
347
- const priorIssues = __classPrivateFieldGet(this, _CoreContext_phase, "f")?.type === 'parse' ? __classPrivateFieldGet(this, _CoreContext_phase, "f").context.issues : [];
348
- const message = error instanceof Error ? error.message : String(error);
398
+ this.logger.error(toSerializedError(error).message, error);
399
+ const priorIssues = __classPrivateFieldGet(this, _CoreContext_parseIssues, "f");
349
400
  return {
350
401
  subjects: {},
351
402
  parseIssues: [
352
403
  ...priorIssues,
353
- {
354
- protocol: 'oas',
355
- level: 'error',
356
- type: 'INVALID_SCHEMA',
357
- location: 'toSupportedSubjects',
358
- message: `Top-level toSupportedSubjects failure: ${message}`,
359
- cause: error
360
- }
404
+ __classPrivateFieldGet(this, _CoreContext_instances, "m", _CoreContext_toRunFailedIssue).call(this, { entry: 'toSupportedSubjects', protocol: document.type, error })
361
405
  ]
362
406
  };
363
407
  }
408
+ finally {
409
+ __classPrivateFieldGet(this, _CoreContext_instances, "m", _CoreContext_closeFileHandlers).call(this);
410
+ }
364
411
  }
365
412
  /**
366
413
  * Seed-values sibling of {@link toSupportedSubjects}: parse the document, then
@@ -372,10 +419,14 @@ export class CoreContext {
372
419
  */
373
420
  toEnrichmentDefaults({ document, settings, toGeneratorConfigMap, stackTrail }) {
374
421
  try {
422
+ __classPrivateFieldSet(this, _CoreContext_phaseName, 'parse', "f");
423
+ __classPrivateFieldSet(this, _CoreContext_parseIssues, [], "f");
375
424
  const phase = __classPrivateFieldGet(this, _CoreContext_instances, "m", _CoreContext_setupParsePhase).call(this, document);
376
425
  __classPrivateFieldSet(this, _CoreContext_phase, phase, "f");
426
+ __classPrivateFieldSet(this, _CoreContext_parseIssues, phase.context.issues, "f");
377
427
  const parsedDocument = stackTrail.trace('parse', st => phase.context.parse(st));
378
428
  const enrichmentDefaults = stackTrail.trace('generate', () => {
429
+ __classPrivateFieldSet(this, _CoreContext_phaseName, 'generate', "f");
379
430
  // Seeding never renders, so the capture interval never opens — a
380
431
  // throwaway channel satisfies the wiring.
381
432
  const generatePhase = __classPrivateFieldGet(this, _CoreContext_instances, "m", _CoreContext_setupGeneratePhase).call(this, {
@@ -390,24 +441,19 @@ export class CoreContext {
390
441
  return { enrichmentDefaults, parseIssues: phase.context.issues };
391
442
  }
392
443
  catch (error) {
393
- this.logger.error(error);
394
- const priorIssues = __classPrivateFieldGet(this, _CoreContext_phase, "f")?.type === 'parse' ? __classPrivateFieldGet(this, _CoreContext_phase, "f").context.issues : [];
395
- const message = error instanceof Error ? error.message : String(error);
444
+ this.logger.error(toSerializedError(error).message, error);
445
+ const priorIssues = __classPrivateFieldGet(this, _CoreContext_parseIssues, "f");
396
446
  return {
397
447
  enrichmentDefaults: {},
398
448
  parseIssues: [
399
449
  ...priorIssues,
400
- {
401
- protocol: 'oas',
402
- level: 'error',
403
- type: 'INVALID_SCHEMA',
404
- location: 'toEnrichmentDefaults',
405
- message: `Top-level toEnrichmentDefaults failure: ${message}`,
406
- cause: error
407
- }
450
+ __classPrivateFieldGet(this, _CoreContext_instances, "m", _CoreContext_toRunFailedIssue).call(this, { entry: 'toEnrichmentDefaults', protocol: document.type, error })
408
451
  ]
409
452
  };
410
453
  }
454
+ finally {
455
+ __classPrivateFieldGet(this, _CoreContext_instances, "m", _CoreContext_closeFileHandlers).call(this);
456
+ }
411
457
  }
412
458
  /**
413
459
  * Captures a result at `stackTrail`. Each frame of the trail becomes one
@@ -428,42 +474,54 @@ export class CoreContext {
428
474
  __classPrivateFieldGet(this, _CoreContext_results, "f").capture(stackTrail, result);
429
475
  }
430
476
  }
431
- _CoreContext_phase = new WeakMap(), _CoreContext_results = new WeakMap(), _CoreContext_instances = new WeakSet(), _CoreContext_setupLogger = function _CoreContext_setupLogger({ spanId, logsPath }) {
432
- const filename = join(logsPath ?? '', `${spanId}.txt`);
433
- log.setup({
434
- handlers: {
435
- [`${spanId}-logs`]: new log.ConsoleHandler('DEBUG', {
436
- formatter: logRecord => skmtcFormatter({
437
- logRecord,
438
- stackTrail: 'SKIPPED'
439
- }),
477
+ _CoreContext_phase = new WeakMap(), _CoreContext_results = new WeakMap(), _CoreContext_logLines = new WeakMap(), _CoreContext_phaseName = new WeakMap(), _CoreContext_parseIssues = new WeakMap(), _CoreContext_instances = new WeakSet(), _CoreContext_setupLogger = function _CoreContext_setupLogger({ spanId, logsPath, logLines }) {
478
+ const fileHandlers = logsPath
479
+ ? [
480
+ new log.FileHandler('DEBUG', {
481
+ filename: join(logsPath, `${spanId}.txt`),
482
+ // JSON format for file logs (easier to parse and analyze)
483
+ formatter: logRecord => skmtcJsonFormatter({ logRecord, stackTrail: __classPrivateFieldGet(this, _CoreContext_instances, "m", _CoreContext_toLogTrail).call(this) })
484
+ })
485
+ ]
486
+ : [];
487
+ fileHandlers.forEach(handler => handler.setup());
488
+ return new log.Logger(spanId, 'DEBUG', {
489
+ handlers: [
490
+ new log.ConsoleHandler('DEBUG', {
491
+ formatter: logRecord => skmtcFormatter({ logRecord, stackTrail: __classPrivateFieldGet(this, _CoreContext_instances, "m", _CoreContext_toLogTrail).call(this) }),
440
492
  useColors: true
441
493
  }),
442
- ...(logsPath && {
443
- file: new log.FileHandler('DEBUG', {
444
- filename,
445
- // JSON format for file logs (easier to parse and analyze)
446
- formatter: logRecord => {
447
- return skmtcJsonFormatter({
448
- logRecord,
449
- stackTrail: 'SKIPPED'
450
- });
451
- }
452
- })
453
- }),
454
- [`${spanId}-results`]: new ResultsHandler('WARN', {
494
+ ...fileHandlers,
495
+ logLines,
496
+ new ResultsHandler('WARN', {
455
497
  formatter: ({ levelName }) => levelName,
456
498
  context: this
457
499
  })
458
- },
459
- loggers: {
460
- [spanId]: {
461
- level: 'DEBUG',
462
- handlers: [`${spanId}-logs`, `${spanId}-results`, 'file']
463
- }
500
+ ]
501
+ });
502
+ }, _CoreContext_toLogTrail = function _CoreContext_toLogTrail() {
503
+ return this.currentSubjectTrail?.toString() ?? 'SKIPPED';
504
+ }, _CoreContext_closeFileHandlers = function _CoreContext_closeFileHandlers() {
505
+ this.logger.handlers.forEach(handler => {
506
+ if (handler instanceof log.FileHandler) {
507
+ handler.destroy();
464
508
  }
465
509
  });
466
- return log.getLogger(spanId);
510
+ }, _CoreContext_toRunFailedIssue = function _CoreContext_toRunFailedIssue({ entry, protocol, error }) {
511
+ const phaseName = __classPrivateFieldGet(this, _CoreContext_phaseName, "f");
512
+ const cause = toSerializedError(error);
513
+ const location = new StackTrail([entry, phaseName]).toString();
514
+ const message = `${phaseName} phase failed: ${cause.message}`;
515
+ switch (protocol) {
516
+ case 'oas':
517
+ return { protocol, level: 'error', type: 'RUN_FAILED', location, message, cause };
518
+ case 'gql':
519
+ return { protocol, level: 'error', type: 'RUN_FAILED', location, message, cause };
520
+ default: {
521
+ const _exhaustive = protocol;
522
+ throw new Error(`Unhandled protocol type: ${JSON.stringify(_exhaustive)}`);
523
+ }
524
+ }
467
525
  }, _CoreContext_setupParsePhase = function _CoreContext_setupParsePhase(input) {
468
526
  const parseContext = new ParseContext({
469
527
  input,
@@ -548,7 +606,7 @@ export function skmtcFormatter({ logRecord, stackTrail }) {
548
606
  // Add stack trail if it's not "SKIPPED"
549
607
  const stackTrailLine = stackTrail !== 'SKIPPED' ? `\n${gray('Stack:')} ${stackTrail}` : '';
550
608
  // Format the message (preserve multi-line errors and stack traces)
551
- const formattedMessage = msg;
609
+ const formattedMessage = toMessageWithStack(logRecord);
552
610
  return `${header}${stackTrailLine}\n${formattedMessage}\n`;
553
611
  }
554
612
  /**
@@ -581,10 +639,23 @@ export function skmtcJsonFormatter({ logRecord, stackTrail }) {
581
639
  });
582
640
  }
583
641
  function flattenArgs(args) {
584
- if (args.length === 1) {
585
- return args[0];
642
+ const serializable = args.map(arg => (arg instanceof Error ? toSerializedError(arg) : arg));
643
+ if (serializable.length === 1) {
644
+ return serializable[0];
586
645
  }
587
- else if (args.length > 1) {
588
- return args;
646
+ else if (serializable.length > 1) {
647
+ return serializable;
648
+ }
649
+ }
650
+ /**
651
+ * `msg`, followed by the stack of the first `Error` in `args`. A line
652
+ * logged as `logger.error(error.message, error)` prints the stack alone,
653
+ * which starts with the message.
654
+ */
655
+ function toMessageWithStack({ msg, args }) {
656
+ const error = args.find(arg => arg instanceof Error);
657
+ if (error?.stack === undefined) {
658
+ return msg;
589
659
  }
660
+ return error.message === msg ? error.stack : `${msg}\n${error.stack}`;
590
661
  }
@@ -170,6 +170,17 @@ export declare class GenerateContext implements GenerateContextType {
170
170
  * shared channel's `sink`.
171
171
  */
172
172
  get captureSink(): CaptureSink | undefined;
173
+ /**
174
+ * The trail of the subject whose variant is being generated, or
175
+ * `undefined` outside one. Log lines and the results the logger
176
+ * records are addressed by it.
177
+ */
178
+ get currentSubjectTrail(): StackTrail | undefined;
179
+ /**
180
+ * Runs `fn` with {@link currentSubjectTrail} set to `stackTrail`, and
181
+ * restores the previous value afterwards.
182
+ */
183
+ withSubjectTrail<T>(stackTrail: StackTrail, fn: () => T): T;
173
184
  /**
174
185
  * @internal
175
186
  */