@cnc-tools/storm 0.6.0 → 0.8.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 (174) hide show
  1. package/dist/build-info.js +1 -1
  2. package/dist/commands/exec/cleanup.d.ts +3 -3
  3. package/dist/commands/exec/cleanup.js +1 -1
  4. package/dist/commands/exec/command.d.ts +797 -1
  5. package/dist/commands/exec/command.js +1 -1
  6. package/dist/commands/exec/live-view.js +32 -15
  7. package/dist/commands/exec/metadata.d.ts +5 -2
  8. package/dist/commands/exec/metadata.js +4 -0
  9. package/dist/commands/exec/mint-failed.d.ts +28 -0
  10. package/dist/commands/exec/outcomes.d.ts +526 -0
  11. package/dist/commands/exec/outcomes.js +9 -0
  12. package/dist/commands/exec/progress.d.ts +14 -2
  13. package/dist/commands/exec/progress.js +57 -14
  14. package/dist/commands/exec/run-command.d.ts +10 -4
  15. package/dist/commands/exec/run-command.js +41 -8
  16. package/dist/commands/exec/run-metadata.d.ts +6 -3
  17. package/dist/commands/exec/run-metadata.js +6 -3
  18. package/dist/commands/exec/run-record.d.ts +83 -4
  19. package/dist/commands/exec/run-record.js +49 -1
  20. package/dist/commands/exec/tests-summary.d.ts +1 -1
  21. package/dist/commands/exec/tests-summary.js +1 -1
  22. package/dist/commands/exec/view.js +36 -4
  23. package/dist/commands/runs/facts.d.ts +25 -0
  24. package/dist/commands/runs/facts.js +36 -3
  25. package/dist/commands/runs/list/command.d.ts +154 -1
  26. package/dist/commands/runs/list/command.js +1 -1
  27. package/dist/commands/runs/list/outcomes.d.ts +41 -0
  28. package/dist/commands/runs/logs/command.d.ts +263 -1
  29. package/dist/commands/runs/logs/command.js +1 -1
  30. package/dist/commands/runs/logs/outcomes.d.ts +105 -0
  31. package/dist/commands/runs/show/command.d.ts +354 -1
  32. package/dist/commands/runs/show/command.js +1 -1
  33. package/dist/commands/runs/show/metadata.js +1 -1
  34. package/dist/commands/runs/show/outcomes.d.ts +96 -0
  35. package/dist/commands/runs/show/outcomes.js +30 -1
  36. package/dist/commands/vitest/command.d.ts +912 -1
  37. package/dist/commands/vitest/command.js +1 -1
  38. package/dist/commands/vitest/metadata.d.ts +5 -2
  39. package/dist/commands/vitest/metadata.js +4 -0
  40. package/dist/commands/vitest/outcomes.d.ts +320 -0
  41. package/dist/config/errors.d.ts +73 -0
  42. package/dist/config/schema.d.ts +1 -1
  43. package/dist/config-file/define.d.ts +4 -3
  44. package/dist/config-file/define.js +1 -1
  45. package/dist/config-file/toolchain.js +1 -1
  46. package/dist/core/command-slots.d.ts +8 -2
  47. package/dist/core/command-slots.js +6 -2
  48. package/dist/core/events.d.ts +9 -7
  49. package/dist/core/index.d.ts +1 -1
  50. package/dist/core/run-state.d.ts +30 -6
  51. package/dist/core/run-state.js +19 -4
  52. package/dist/core/run.d.ts +17 -4
  53. package/dist/core/run.js +61 -25
  54. package/dist/credential/index.d.ts +1 -0
  55. package/dist/credential/index.js +1 -0
  56. package/dist/credential/settle.d.ts +20 -0
  57. package/dist/credential/target.d.ts +6 -0
  58. package/dist/credential/target.js +7 -0
  59. package/dist/resources/github/command.d.ts +54 -2
  60. package/dist/resources/github/config.js +1 -1
  61. package/dist/resources/github/index.js +1 -1
  62. package/dist/resources/github/resource.js +2 -2
  63. package/dist/resources/testserver/command.d.ts +208 -10
  64. package/dist/resources/testserver/command.js +7 -0
  65. package/dist/resources/testserver/commands/create/command.d.ts +808 -1
  66. package/dist/resources/testserver/commands/create/command.js +64 -23
  67. package/dist/resources/testserver/commands/create/metadata.js +6 -2
  68. package/dist/resources/testserver/commands/create/outcomes.d.ts +347 -0
  69. package/dist/resources/testserver/commands/create/outcomes.js +14 -1
  70. package/dist/resources/testserver/commands/list/command.d.ts +84 -1
  71. package/dist/resources/testserver/commands/list/command.js +1 -1
  72. package/dist/resources/testserver/commands/list/outcomes.d.ts +16 -0
  73. package/dist/resources/testserver/config.js +14 -4
  74. package/dist/resources/testserver/events.d.ts +9 -0
  75. package/dist/resources/testserver/index.js +8 -7
  76. package/dist/resources/testserver/lane.js +1 -0
  77. package/dist/resources/testserver/live/gateways.d.ts +2 -0
  78. package/dist/resources/testserver/live/gateways.js +16 -3
  79. package/dist/resources/testserver/pool/channels.d.ts +5 -2
  80. package/dist/resources/testserver/pool/channels.js +1 -1
  81. package/dist/resources/testserver/pool/create.d.ts +6 -2
  82. package/dist/resources/testserver/pool/create.js +6 -5
  83. package/dist/resources/testserver/pool/index.d.ts +1 -0
  84. package/dist/resources/testserver/pool/index.js +1 -0
  85. package/dist/resources/testserver/pool/pull-request.d.ts +89 -0
  86. package/dist/resources/testserver/pool/pull-request.js +85 -0
  87. package/dist/resources/testserver/pr-build-facts.d.ts +130 -0
  88. package/dist/resources/testserver/pr-build-facts.js +261 -0
  89. package/dist/resources/testserver/presentation.d.ts +2 -2
  90. package/dist/resources/testserver/presentation.js +12 -3
  91. package/dist/resources/testserver/provider.d.ts +16 -6
  92. package/dist/resources/testserver/provider.js +46 -15
  93. package/dist/resources/testserver/pull-request/gateways.d.ts +117 -0
  94. package/dist/resources/testserver/pull-request/gateways.js +5 -0
  95. package/dist/resources/testserver/pull-request/index.d.ts +5 -0
  96. package/dist/resources/testserver/pull-request/index.js +6 -0
  97. package/dist/resources/testserver/pull-request/live/github-rest.d.ts +28 -0
  98. package/dist/resources/testserver/pull-request/live/github-rest.js +114 -0
  99. package/dist/resources/testserver/pull-request/live/github-token-gateway.d.ts +12 -0
  100. package/dist/resources/testserver/pull-request/live/github-token-gateway.js +39 -0
  101. package/dist/resources/testserver/pull-request/live/pull-request-gateway.d.ts +10 -0
  102. package/dist/resources/testserver/pull-request/live/pull-request-gateway.js +27 -0
  103. package/dist/resources/testserver/pull-request/live/testserver-workflow-gateway.d.ts +21 -0
  104. package/dist/resources/testserver/pull-request/live/testserver-workflow-gateway.js +64 -0
  105. package/dist/resources/testserver/ran.d.ts +5 -0
  106. package/dist/resources/testserver/ran.js +19 -3
  107. package/dist/resources/testserver/resource.d.ts +5 -1
  108. package/dist/resources/testserver/resource.js +2 -2
  109. package/dist/resources/testserver/sandbox/build-reference.d.ts +38 -0
  110. package/dist/resources/testserver/sandbox/build-reference.js +79 -0
  111. package/dist/resources/testserver/sandbox/index.d.ts +1 -0
  112. package/dist/resources/testserver/sandbox/index.js +1 -0
  113. package/dist/resources.d.ts +265 -15
  114. package/dist/runner/events.d.ts +64 -1
  115. package/dist/runner/gateways.d.ts +17 -6
  116. package/dist/runner/index.d.ts +5 -4
  117. package/dist/runner/index.js +4 -3
  118. package/dist/runner/live/snapshot-records.js +18 -4
  119. package/dist/runner/prepare.d.ts +23 -4
  120. package/dist/runner/prepare.js +13 -3
  121. package/dist/runner/ready.d.ts +7 -3
  122. package/dist/runner/ready.js +11 -5
  123. package/dist/runner/scope.d.ts +17 -1
  124. package/dist/runner/scope.js +4 -0
  125. package/dist/runner/snapshot-build.d.ts +3 -1
  126. package/dist/runner/snapshot-build.js +12 -2
  127. package/dist/runner/snapshot-cache.d.ts +46 -8
  128. package/dist/runner/snapshot-cache.js +60 -11
  129. package/dist/runner/snapshot-key.d.ts +12 -1
  130. package/dist/runner/snapshot-key.js +17 -1
  131. package/dist/runner/snapshot-save.d.ts +53 -0
  132. package/dist/runner/snapshot-save.js +148 -0
  133. package/dist/sdk/index.js +2 -2
  134. package/dist/story/align.d.ts +1 -1
  135. package/dist/story/align.js +1 -1
  136. package/dist/story/command-line.d.ts +34 -0
  137. package/dist/story/command-line.js +100 -0
  138. package/dist/story/definition.d.ts +22 -15
  139. package/dist/story/definition.js +2 -2
  140. package/dist/story/errors.js +1 -1
  141. package/dist/story/gateways.d.ts +45 -25
  142. package/dist/story/gateways.js +2 -2
  143. package/dist/story/host.d.ts +11 -8
  144. package/dist/story/host.js +37 -37
  145. package/dist/story/index.d.ts +2 -1
  146. package/dist/story/index.js +6 -5
  147. package/dist/story/json.d.ts +1 -1
  148. package/dist/story/json.js +2 -2
  149. package/dist/story/live/recording-gateway.d.ts +2 -0
  150. package/dist/story/live/{artifact-gateway.js → recording-gateway.js} +33 -12
  151. package/dist/story/mode.d.ts +2 -2
  152. package/dist/story/mode.js +1 -1
  153. package/dist/story/orphans.d.ts +23 -5
  154. package/dist/story/orphans.js +44 -8
  155. package/dist/story/recording.d.ts +62 -0
  156. package/dist/story/{artifact.js → recording.js} +31 -27
  157. package/dist/story/report.d.ts +21 -9
  158. package/dist/story/report.js +37 -25
  159. package/dist/story/settle.d.ts +23 -21
  160. package/dist/story/settle.js +17 -16
  161. package/dist/story/{run.d.ts → start.d.ts} +18 -17
  162. package/dist/story/{run.js → start.js} +66 -43
  163. package/dist/vercel-cli/context.js +1 -1
  164. package/dist/vercel-cli/index.js +1 -1
  165. package/dist/vitest/config.js +2 -2
  166. package/dist/vitest/fixtures.d.ts +1 -1
  167. package/dist/vitest/fixtures.js +1 -1
  168. package/dist/vitest/index.js +3 -3
  169. package/dist/vitest/plan.js +1 -1
  170. package/package.json +3 -3
  171. package/skills/overview.md +2 -0
  172. package/skills/testserver-builds.md +80 -0
  173. package/dist/story/artifact.d.ts +0 -60
  174. package/dist/story/live/artifact-gateway.d.ts +0 -2
@@ -1,23 +1,24 @@
1
- // `story()` on Vitest: the one file in `src/story/` that imports vitest. Each story is a Vitest test whose artifact
2
- // sits beside its file, and runs over the live artifact gateway.
1
+ // `story()` on Vitest: the one file in `src/story/` that imports vitest. Each story is a Vitest test whose recording
2
+ // sits beside its file, and runs over the live recording gateway.
3
3
  import { basename } from "node:path";
4
4
  import { test } from "vitest";
5
- import { artifactPath } from "./artifact.js";
6
- import { liveStoryArtifactGateway } from "./live/artifact-gateway.js";
5
+ import { recordingPath } from "./recording.js";
6
+ import { liveStoryRecordingGateway } from "./live/recording-gateway.js";
7
7
  import { resolveStoryRecordLevel } from "./mode.js";
8
- import { findOrphans } from "./orphans.js";
9
- import { startStoryRun } from "./run.js";
10
- /** The test ids of the stories that ended, by story file, for finding each file's last story. */
11
- const endedStories = new Map();
8
+ import { findOrphans, looksForOrphans } from "./orphans.js";
9
+ import { startStory } from "./start.js";
12
10
  /**
13
- * The `story` function for `harness`. A story's name is its full test name, its `describe` names first; its artifact
14
- * is `artifactPath(<story file>, <name>)`. When the story ends, each call becomes a Vitest annotation of type `story`
15
- * (`storyEventLines`), and a line saying what it changed goes to stderr too. A story whose body threw prints its
16
- * report on stderr, when there is one, and fails with its body's error; one that diverged or went stale fails with
17
- * the report as the error. A story Vitest stops (a timeout, or a canceled run) ends then, without waiting for its body,
18
- * and counts as one whose body threw: its session closes, and it writes nothing and removes no orphans, even when its
19
- * body returns later or the stop comes while its end is changing the artifact. When every story in the file runs (no
20
- * test name filter, `.only` or `.skip` left one out), the last to end also finds the file's orphans.
11
+ * The `story` function for `harness`. A story's name is its full test name, its `describe` names first; its recording
12
+ * is `recordingPath(<story file>, <name>)`. When the story ends, each call becomes a Vitest annotation of type `story`
13
+ * (`storyEventLines`), and what a passing story changed, naming each call it rewrote (`storyChangeReport`), goes to
14
+ * stderr and becomes an annotation too. A story whose body threw prints its report on stderr, when there is one, and
15
+ * fails with its body's error; one that diverged or went stale fails with the report as the error. A story Vitest
16
+ * stops (a timeout, or a canceled Vitest run) ends then, without waiting for its body, and counts as one whose body
17
+ * threw: its session closes, and it writes nothing and removes no orphan recording, even when its body returns later
18
+ * or the stop comes while its end is changing the recording. When every story in the file runs (no test name filter,
19
+ * `.only` or `.skip` left one out), the last story the file declares also finds the orphan recordings when it ends
20
+ * (`looksForOrphans`, `findOrphans`): the file's own, and those in the recording directory of each story file that is
21
+ * gone.
21
22
  */
22
23
  export function createStory(harness, options = {}) {
23
24
  return (name, body) => {
@@ -25,38 +26,39 @@ export function createStory(harness, options = {}) {
25
26
  const file = context.task.file.filepath;
26
27
  const story = { file: basename(file), name: context.task.fullTestName };
27
28
  const level = resolveStoryRecordLevel(options.env ?? process.env);
28
- const run = startStoryRun(harness, { story, path: artifactPath(file, story.name), level, signal: context.signal }, liveStoryArtifactGateway);
29
- const ended = await bodyEnd(() => body(run.handles), context.signal);
29
+ const started = startStory(harness, { story, path: recordingPath(file, story.name), level, signal: context.signal }, liveStoryRecordingGateway);
30
+ const bodyEnded = await bodyEnd(() => body(started.handles), context.signal);
30
31
  const orphans = await fileOrphans(context);
31
- const end = await run.finish({
32
- bodyPassed: ended.passed,
32
+ const end = await started.end({
33
+ bodyPassed: bodyEnded.passed,
33
34
  orphans: orphans.kind === "found" ? orphans.paths : [],
34
35
  });
35
36
  const annotations = end.events.map((line) => context.annotate(line, "story"));
36
- if (end.changeLine !== undefined) {
37
- console.error(end.changeLine);
38
- annotations.push(context.annotate(end.changeLine, "story"));
37
+ if (end.changeReport !== undefined) {
38
+ console.error(end.changeReport);
39
+ annotations.push(context.annotate(end.changeReport, "story"));
39
40
  }
40
41
  await Promise.all(annotations);
41
- if (!ended.passed) {
42
+ if (!bodyEnded.passed) {
42
43
  if (end.report !== undefined)
43
44
  console.error(end.report);
44
45
  if (end.error !== undefined)
45
46
  console.error(end.error.message);
46
- throw ended.error;
47
+ throw bodyEnded.error;
47
48
  }
48
49
  if (end.error !== undefined)
49
50
  throw end.error;
50
- if (orphans.kind === "failed")
51
- throw new Error(`story: couldn't look for orphans: ${orphans.message}`);
51
+ if (orphans.kind === "failed") {
52
+ throw new Error(`story: couldn't look for orphan recordings: ${orphans.message}`);
53
+ }
52
54
  });
53
55
  };
54
56
  }
55
57
  /**
56
58
  * Runs `body` until it settles or `signal` aborts, whichever comes first. Vitest aborts the signal when it times the
57
- * story out or cancels the run, and has already failed the test by then, so a stopped body has not passed, even if it
58
- * returns later. A body still running then keeps running: nothing can stop it, but the story ends without it, so its
59
- * session closes, and any tool it calls after that throws.
59
+ * story out or cancels its Vitest run, and has already failed the test by then, so a stopped body has not passed,
60
+ * even if it returns later. A body still running then keeps running: nothing can stop it, but the story ends without
61
+ * it, so its session closes, and any tool it calls after that throws.
60
62
  */
61
63
  function bodyEnd(body, signal) {
62
64
  const stopped = () => ({
@@ -78,18 +80,16 @@ function bodyEnd(body, signal) {
78
80
  });
79
81
  }
80
82
  /**
81
- * The orphans of the running story's file, found only by the last of its stories to end, and only when every story
82
- * in the file runs; none otherwise. Records that this story ended.
83
+ * The orphan recordings the running story's end finds (`findOrphans`): found only by the last story its file declares,
84
+ * and only when every story in the file runs (`looksForOrphans`); none otherwise.
83
85
  */
84
86
  async function fileOrphans(context) {
85
87
  const file = context.task.file;
86
- const ended = endedStories.get(file.filepath) ?? new Set();
87
- endedStories.set(file.filepath, ended);
88
- ended.add(context.task.id);
89
88
  const stories = storyTests(file.tasks);
90
- if (!stories.every((each) => each.mode === "run" && ended.has(each.id)))
89
+ const declared = stories.map((each) => ({ id: each.id, runs: each.mode === "run" }));
90
+ if (!looksForOrphans(declared, context.task.id))
91
91
  return { kind: "found", paths: [] };
92
- return findOrphans({ storyFile: file.filepath, expected: stories.map((each) => artifactPath(file.filepath, each.fullTestName)) }, liveStoryArtifactGateway);
92
+ return findOrphans({ storyFile: file.filepath, expected: stories.map((each) => recordingPath(file.filepath, each.fullTestName)) }, liveStoryRecordingGateway);
93
93
  }
94
94
  /** Every test `story()` declared among `tasks`, in suites too. */
95
95
  function storyTests(tasks) {
@@ -1,5 +1,6 @@
1
- export { artifactDirectory, artifactPath, parseArtifact, type ParsedStoryArtifact, STORY_ARTIFACT_SCHEMA_VERSION, type StoryArtifact, type StoryCall, type StoryIdentity, } from "./artifact.ts";
1
+ export { recordingDirectory, recordingPath, parseRecording, type ParsedStoryRecording, STORY_RECORDING_SCHEMA_VERSION, type StoryRecording, type StoryCall, type StoryIdentity, } from "./recording.ts";
2
2
  export { type AnyStoryTool, type StoryAgent, type StoryAgents, type StoryHandles, type StoryHarness, type StorySession, type StoryTool, type StoryToolCall, storyTool, } from "./definition.ts";
3
+ export { InvalidCommandLine, splitCommandLine, type StoryExecutable, type StoryExecutableOptions, storyExecutable, } from "./command-line.ts";
3
4
  export { StoryDivergenceError, StoryStaleError } from "./errors.ts";
4
5
  export { createStory, type StoryFunction, type StoryHostOptions } from "./host.ts";
5
6
  export type { StoryJson } from "./json.ts";
@@ -1,9 +1,10 @@
1
- // story, exported as `@cnc-tools/storm/story`: stories whose agents call tools, each call's observation recorded in
2
- // an artifact beside the story file and compared on every run. `README.md` beside this file is its design and
3
- // authoring guide. It depends only on Node builtins, `zod`, and `vitest` (an optional peer dependency of
4
- // `@cnc-tools/storm`), and on nothing else in storm; `.oxlintrc.json` checks the boundary.
5
- export { artifactDirectory, artifactPath, parseArtifact, STORY_ARTIFACT_SCHEMA_VERSION, } from "./artifact.js";
1
+ // Story core, exported as `@vercel/storm/story`: stories whose agents call tools, each call's observation kept in
2
+ // a recording beside the story file and compared on every later Vitest run. `README.md` beside this file is its design
3
+ // and authoring guide. It depends only on Node builtins, `zod`, and `vitest` (an optional peer dependency of
4
+ // `@vercel/storm`), and on nothing else in storm; `.oxlintrc.json` checks the boundary.
5
+ export { recordingDirectory, recordingPath, parseRecording, STORY_RECORDING_SCHEMA_VERSION, } from "./recording.js";
6
6
  export { storyTool, } from "./definition.js";
7
+ export { InvalidCommandLine, splitCommandLine, storyExecutable, } from "./command-line.js";
7
8
  export { StoryDivergenceError, StoryStaleError } from "./errors.js";
8
9
  export { createStory } from "./host.js";
9
10
  export { STORY_RECORD_VARIABLE } from "./mode.js";
@@ -1,4 +1,4 @@
1
- /** A JSON value, as a story artifact holds it. */
1
+ /** A JSON value, as a story recording holds it. */
2
2
  export type StoryJson = null | boolean | number | string | readonly StoryJson[] | {
3
3
  readonly [key: string]: StoryJson;
4
4
  };
@@ -1,4 +1,4 @@
1
- // The JSON a story artifact holds: tool inputs and observations. Pure.
1
+ // The JSON a story recording holds: tool inputs and observations. Pure.
2
2
  /**
3
3
  * `value` as `StoryJson`, a copy, or a `TypeError` that says `what` and why `value` isn't JSON: a function, a
4
4
  * `bigint`, a symbol, `undefined` (in an array, or as the whole value), a number that isn't finite, an object that
@@ -29,7 +29,7 @@ export function sortKeys(value) {
29
29
  }
30
30
  return value;
31
31
  }
32
- /** Why `value` isn't JSON a story artifact can hold, or `undefined` when it is. */
32
+ /** Why `value` isn't JSON a story recording can hold, or `undefined` when it is. */
33
33
  function jsonProblem(value, seen) {
34
34
  if (value === null || typeof value === "string" || typeof value === "boolean")
35
35
  return undefined;
@@ -0,0 +1,2 @@
1
+ import type { StoryRecordingGateway } from "../gateways.ts";
2
+ export declare const liveStoryRecordingGateway: StoryRecordingGateway;
@@ -1,12 +1,12 @@
1
- // The live artifact gateway: one JSON file per story on disk. A write goes to a temporary file beside the artifact
2
- // and is renamed into place, so a reader never sees half an artifact. A write or removal checks its signal before it
1
+ // The live recording gateway: one JSON file per story on disk. A write goes to a temporary file beside the recording
2
+ // and is renamed into place, so a reader never sees half a recording. A write or removal checks its signal before it
3
3
  // starts and again just before the rename or `rm` that makes the change.
4
4
  import { randomBytes } from "node:crypto";
5
- import { mkdir, readdir, readFile, rename, rm, writeFile } from "node:fs/promises";
5
+ import { mkdir, readdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
6
6
  import { dirname, join } from "node:path";
7
- import { parseArtifact, serializeArtifact } from "../artifact.js";
8
- export const liveStoryArtifactGateway = {
9
- async readArtifact({ path }) {
7
+ import { parseRecording, serializeRecording } from "../recording.js";
8
+ export const liveStoryRecordingGateway = {
9
+ async readRecording({ path }) {
10
10
  let text;
11
11
  try {
12
12
  text = await readFile(path, "utf8");
@@ -14,13 +14,13 @@ export const liveStoryArtifactGateway = {
14
14
  catch (error) {
15
15
  return codeOf(error) === "ENOENT" ? { kind: "missing" } : { kind: "failed", message: messageOf(error) };
16
16
  }
17
- const parsed = parseArtifact(text);
18
- return parsed.ok ? { kind: "found", artifact: parsed.artifact } : { kind: "unreadable", problem: parsed.problem };
17
+ const parsed = parseRecording(text);
18
+ return parsed.ok ? { kind: "found", recording: parsed.recording } : { kind: "unreadable", problem: parsed.problem };
19
19
  },
20
- async writeArtifact({ path, artifact, signal }) {
20
+ async writeRecording({ path, recording, signal }) {
21
21
  if (aborted(signal))
22
22
  return { kind: "canceled" };
23
- const text = serializeArtifact(artifact);
23
+ const text = serializeRecording(recording);
24
24
  try {
25
25
  if ((await readFile(path, "utf8")) === text)
26
26
  return { kind: "unchanged" };
@@ -45,7 +45,7 @@ export const liveStoryArtifactGateway = {
45
45
  return { kind: "failed", message: messageOf(error) };
46
46
  }
47
47
  },
48
- async removeArtifact({ path, signal }) {
48
+ async removeRecording({ path, signal }) {
49
49
  if (aborted(signal))
50
50
  return { kind: "canceled" };
51
51
  try {
@@ -56,7 +56,7 @@ export const liveStoryArtifactGateway = {
56
56
  return codeOf(error) === "ENOENT" ? { kind: "missing" } : { kind: "failed", message: messageOf(error) };
57
57
  }
58
58
  },
59
- async listArtifacts({ directory }) {
59
+ async listRecordings({ directory }) {
60
60
  try {
61
61
  const entries = await readdir(directory, { withFileTypes: true });
62
62
  const paths = entries
@@ -69,6 +69,27 @@ export const liveStoryArtifactGateway = {
69
69
  return codeOf(error) === "ENOENT" ? { kind: "listed", paths: [] } : { kind: "failed", message: messageOf(error) };
70
70
  }
71
71
  },
72
+ async listRecordingDirectories({ root }) {
73
+ try {
74
+ const entries = await readdir(root, { withFileTypes: true });
75
+ const paths = entries
76
+ .filter((entry) => entry.isDirectory())
77
+ .map((entry) => join(root, entry.name))
78
+ .toSorted();
79
+ return { kind: "listed", paths };
80
+ }
81
+ catch (error) {
82
+ return codeOf(error) === "ENOENT" ? { kind: "listed", paths: [] } : { kind: "failed", message: messageOf(error) };
83
+ }
84
+ },
85
+ async findStoryFile({ path }) {
86
+ try {
87
+ return (await stat(path)).isFile() ? { kind: "found" } : { kind: "missing" };
88
+ }
89
+ catch (error) {
90
+ return codeOf(error) === "ENOENT" ? { kind: "missing" } : { kind: "failed", message: messageOf(error) };
91
+ }
92
+ },
72
93
  };
73
94
  /** Whether `signal` has aborted, read afresh at each check: a signal aborts while a method awaits. */
74
95
  function aborted(signal) {
@@ -1,8 +1,8 @@
1
1
  /**
2
- * How much a story run may change its artifacts:
2
+ * How much a story may change its recording:
3
3
  *
4
4
  * - `none` compares and writes nothing. A divergence or a stale entry fails the story.
5
- * - `stale` rewrites the artifact so it fits the story again, when the only problems are stale entries. A divergence
5
+ * - `stale` rewrites the recording so it fits the story again, when the only problems are stale entries. A divergence
6
6
  * still fails the story, and then nothing is written.
7
7
  * - `all` also accepts divergences, recording the new observations.
8
8
  */
@@ -1,4 +1,4 @@
1
- // How much a story run may change its artifacts, from `STORY_RECORD`. Pure.
1
+ // How much a story may change its recording, from `STORY_RECORD`. Pure.
2
2
  /** The variable that sets the level. */
3
3
  export const STORY_RECORD_VARIABLE = "STORY_RECORD";
4
4
  const LEVELS = ["none", "stale", "all"];
@@ -1,4 +1,16 @@
1
- import type { StoryArtifactGateway } from "./gateways.ts";
1
+ import type { StoryRecordingGateway } from "./gateways.ts";
2
+ /** A story as its story file declares it: its id, and whether this Vitest run runs it. */
3
+ export interface DeclaredStory {
4
+ readonly id: string;
5
+ readonly runs: boolean;
6
+ }
7
+ /**
8
+ * Whether the story `id` looks for its file's orphan recordings, given the stories its file declares, in order: only
9
+ * the last of them, and only when every one of them runs (no test name filter, `.only` or `.skip` left one out). The
10
+ * search works from the stories' recording paths and lists only recordings, never a recording being written, so it
11
+ * needs none of the file's other stories to have ended.
12
+ */
13
+ export declare function looksForOrphans(declared: readonly DeclaredStory[], id: string): boolean;
2
14
  export type StoryOrphans = {
3
15
  readonly kind: "found";
4
16
  readonly paths: readonly string[];
@@ -7,11 +19,17 @@ export type StoryOrphans = {
7
19
  readonly message: string;
8
20
  };
9
21
  /**
10
- * The orphans of `storyFile`, whose stories' artifacts are at `expected`: every artifact in its artifact directory
11
- * but those, sorted. The directory is the file's alone (`artifactDirectory`), so an artifact there that no story
12
- * expects is an orphan whatever it holds, an unreadable one too, and another file's artifacts are never listed.
22
+ * The orphan recordings `storyFile`'s end finds, when its stories' recordings are at `expected`, those in its own
23
+ * recording directory first, then each other directory's, each part sorted:
24
+ *
25
+ * - every recording in its own recording directory but those. The directory is the file's alone
26
+ * (`recordingDirectory`), so a recording there that no story expects is an orphan whatever it holds, an unreadable
27
+ * one too.
28
+ * - every recording in each other recording directory beside it, under the same `__story__/`, whose story file is
29
+ * gone. Such a directory belongs to no story file. One whose story file is there is another story file's, and
30
+ * nothing in it is listed or read.
13
31
  */
14
32
  export declare function findOrphans(request: {
15
33
  readonly storyFile: string;
16
34
  readonly expected: readonly string[];
17
- }, gateway: StoryArtifactGateway): Promise<StoryOrphans>;
35
+ }, gateway: StoryRecordingGateway): Promise<StoryOrphans>;
@@ -1,15 +1,51 @@
1
- // Finding a story file's orphans: artifacts in its artifact directory that belong to none of its stories. Domain
2
- // logic over the artifact gateway.
3
- import { artifactDirectory } from "./artifact.js";
1
+ // Finding a story file's orphan recordings: those in its recording directory that belong to none of its stories, and
2
+ // those in the recording directory of a story file that is gone, and which of the file's stories looks for them.
3
+ // Domain logic over the recording gateway.
4
+ import { dirname } from "node:path";
5
+ import { recordingDirectory, storyFileOf } from "./recording.js";
4
6
  /**
5
- * The orphans of `storyFile`, whose stories' artifacts are at `expected`: every artifact in its artifact directory
6
- * but those, sorted. The directory is the file's alone (`artifactDirectory`), so an artifact there that no story
7
- * expects is an orphan whatever it holds, an unreadable one too, and another file's artifacts are never listed.
7
+ * Whether the story `id` looks for its file's orphan recordings, given the stories its file declares, in order: only
8
+ * the last of them, and only when every one of them runs (no test name filter, `.only` or `.skip` left one out). The
9
+ * search works from the stories' recording paths and lists only recordings, never a recording being written, so it
10
+ * needs none of the file's other stories to have ended.
11
+ */
12
+ export function looksForOrphans(declared, id) {
13
+ return declared.every((story) => story.runs) && declared.at(-1)?.id === id;
14
+ }
15
+ /**
16
+ * The orphan recordings `storyFile`'s end finds, when its stories' recordings are at `expected`, those in its own
17
+ * recording directory first, then each other directory's, each part sorted:
18
+ *
19
+ * - every recording in its own recording directory but those. The directory is the file's alone
20
+ * (`recordingDirectory`), so a recording there that no story expects is an orphan whatever it holds, an unreadable
21
+ * one too.
22
+ * - every recording in each other recording directory beside it, under the same `__story__/`, whose story file is
23
+ * gone. Such a directory belongs to no story file. One whose story file is there is another story file's, and
24
+ * nothing in it is listed or read.
8
25
  */
9
26
  export async function findOrphans(request, gateway) {
10
- const listed = await gateway.listArtifacts({ directory: artifactDirectory(request.storyFile) });
27
+ const own = recordingDirectory(request.storyFile);
28
+ const listed = await gateway.listRecordings({ directory: own });
11
29
  if (listed.kind === "failed")
12
30
  return listed;
13
31
  const expected = new Set(request.expected);
14
- return { kind: "found", paths: listed.paths.filter((path) => !expected.has(path)) };
32
+ const recordings = listed.paths.filter((path) => !expected.has(path));
33
+ const others = await gateway.listRecordingDirectories({ root: dirname(own) });
34
+ if (others.kind === "failed")
35
+ return others;
36
+ const gone = [];
37
+ for (const directory of others.paths) {
38
+ if (directory === own)
39
+ continue;
40
+ const storyFile = await gateway.findStoryFile({ path: storyFileOf(directory) });
41
+ if (storyFile.kind === "failed")
42
+ return storyFile;
43
+ if (storyFile.kind === "found")
44
+ continue;
45
+ const held = await gateway.listRecordings({ directory });
46
+ if (held.kind === "failed")
47
+ return held;
48
+ gone.push(...held.paths);
49
+ }
50
+ return { kind: "found", paths: [...recordings, ...gone] };
15
51
  }
@@ -0,0 +1,62 @@
1
+ import { type StoryJson } from "./json.ts";
2
+ /** The recording format this version of story core writes and reads. */
3
+ export declare const STORY_RECORDING_SCHEMA_VERSION = 1;
4
+ /** Which story a recording belongs to: the story file's name (no directory) and the story's full test name. */
5
+ export interface StoryIdentity {
6
+ readonly file: string;
7
+ readonly name: string;
8
+ }
9
+ /**
10
+ * One tool call: the agent, the tool, the input the tool ran with (after its schema parsed it), and what its
11
+ * `observe` returned. (agent, tool, input) is the call's key.
12
+ */
13
+ export interface StoryCall {
14
+ readonly agent: string;
15
+ readonly tool: string;
16
+ readonly input: StoryJson;
17
+ readonly observation: StoryJson;
18
+ }
19
+ /** One story's recording. */
20
+ export interface StoryRecording {
21
+ readonly schemaVersion: typeof STORY_RECORDING_SCHEMA_VERSION;
22
+ readonly story: StoryIdentity;
23
+ readonly calls: readonly StoryCall[];
24
+ }
25
+ /** A recording's text, read back: the recording, or why it can't be one. */
26
+ export type ParsedStoryRecording = {
27
+ readonly ok: true;
28
+ readonly recording: StoryRecording;
29
+ } | {
30
+ readonly ok: false;
31
+ readonly problem: string;
32
+ };
33
+ /**
34
+ * The directory that holds a story file's recordings: `<story file dir>/__story__/<story file name>`
35
+ * (`__story__/slot-surface.story.test.ts`). Each story file has a directory of its own, so the path alone says which
36
+ * file owns a recording, whatever the recording holds.
37
+ */
38
+ export declare function recordingDirectory(storyFile: string): string;
39
+ /** The story file whose recording directory is `directory`: the inverse of `recordingDirectory`. */
40
+ export declare function storyFileOf(directory: string): string;
41
+ /**
42
+ * Where a story's recording lives: `<recordingDirectory>/<slug>-<id8>.json`. The slug is the story's name for people,
43
+ * cut to 80 characters; `id8`, the first 8 hex characters of the SHA-256 of the canonical JSON of
44
+ * `{ file, story }` (the file's name and the story's full name), keeps two stories whose names slug alike apart.
45
+ */
46
+ export declare function recordingPath(storyFile: string, story: string): string;
47
+ /**
48
+ * `text` lowercased, each run of characters other than ASCII letters and digits replaced by one `-`, with no `-` at
49
+ * either end, cut to `max` characters; `story` when nothing is left.
50
+ */
51
+ export declare function slugify(text: string, max: number): string;
52
+ /**
53
+ * The recording's text: its fields in a fixed order (`schemaVersion`, `story`, `calls`, and each call's `agent`,
54
+ * `tool`, `input`, `observation`), each input and observation's keys sorted, indented by 2 spaces, with a trailing
55
+ * newline. The same recording always gives the same bytes.
56
+ */
57
+ export declare function serializeRecording(recording: StoryRecording): string;
58
+ /**
59
+ * `text` as a recording, or the problem: it isn't JSON, its `schemaVersion` isn't 1, or its fields don't have the
60
+ * recording's shape (unknown fields included).
61
+ */
62
+ export declare function parseRecording(text: string): ParsedStoryRecording;
@@ -1,37 +1,41 @@
1
- // A story's artifact: the tool calls one story made, each with its observation, in story order. Where it lives,
1
+ // A story's recording: the tool calls one story made, each with its observation, in story order. Where it lives,
2
2
  // how it is written and how it is read back. Pure, but for hashing with `node:crypto`.
3
3
  import { createHash } from "node:crypto";
4
4
  import { basename, dirname, join } from "node:path";
5
5
  import { z } from "zod";
6
6
  import { canonicalJson, sortKeys } from "./json.js";
7
- /** The artifact format this version of story writes and reads. */
8
- export const STORY_ARTIFACT_SCHEMA_VERSION = 1;
9
- /** How much of the story's name its artifact's file name keeps. */
7
+ /** The recording format this version of story core writes and reads. */
8
+ export const STORY_RECORDING_SCHEMA_VERSION = 1;
9
+ /** How much of the story's name its recording's file name keeps. */
10
10
  const STORY_SLUG_CHARS = 80;
11
- const artifactSchema = z.strictObject({
12
- schemaVersion: z.literal(STORY_ARTIFACT_SCHEMA_VERSION),
11
+ const recordingSchema = z.strictObject({
12
+ schemaVersion: z.literal(STORY_RECORDING_SCHEMA_VERSION),
13
13
  story: z.strictObject({ file: z.string(), name: z.string() }),
14
14
  calls: z.array(z.strictObject({ agent: z.string(), tool: z.string(), input: z.json(), observation: z.json() })),
15
15
  });
16
16
  /**
17
- * The directory that holds a story file's artifacts: `<story file dir>/__story__/<story file name>`
17
+ * The directory that holds a story file's recordings: `<story file dir>/__story__/<story file name>`
18
18
  * (`__story__/slot-surface.story.test.ts`). Each story file has a directory of its own, so the path alone says which
19
- * file owns an artifact, whatever the artifact holds.
19
+ * file owns a recording, whatever the recording holds.
20
20
  */
21
- export function artifactDirectory(storyFile) {
21
+ export function recordingDirectory(storyFile) {
22
22
  return join(dirname(storyFile), "__story__", basename(storyFile));
23
23
  }
24
+ /** The story file whose recording directory is `directory`: the inverse of `recordingDirectory`. */
25
+ export function storyFileOf(directory) {
26
+ return join(dirname(dirname(directory)), basename(directory));
27
+ }
24
28
  /**
25
- * Where a story's artifact lives: `<artifactDirectory>/<slug>-<id8>.json`. The slug is the story's name for people,
29
+ * Where a story's recording lives: `<recordingDirectory>/<slug>-<id8>.json`. The slug is the story's name for people,
26
30
  * cut to 80 characters; `id8`, the first 8 hex characters of the SHA-256 of the canonical JSON of
27
31
  * `{ file, story }` (the file's name and the story's full name), keeps two stories whose names slug alike apart.
28
32
  */
29
- export function artifactPath(storyFile, story) {
33
+ export function recordingPath(storyFile, story) {
30
34
  const id = createHash("sha256")
31
35
  .update(canonicalJson({ file: basename(storyFile), story }))
32
36
  .digest("hex")
33
37
  .slice(0, 8);
34
- return join(artifactDirectory(storyFile), `${slugify(story, STORY_SLUG_CHARS)}-${id}.json`);
38
+ return join(recordingDirectory(storyFile), `${slugify(story, STORY_SLUG_CHARS)}-${id}.json`);
35
39
  }
36
40
  /**
37
41
  * `text` lowercased, each run of characters other than ASCII letters and digits replaced by one `-`, with no `-` at
@@ -47,15 +51,15 @@ export function slugify(text, max) {
47
51
  return slug === "" ? "story" : slug;
48
52
  }
49
53
  /**
50
- * The artifact's text: its fields in a fixed order (`schemaVersion`, `story`, `calls`, and each call's `agent`,
54
+ * The recording's text: its fields in a fixed order (`schemaVersion`, `story`, `calls`, and each call's `agent`,
51
55
  * `tool`, `input`, `observation`), each input and observation's keys sorted, indented by 2 spaces, with a trailing
52
- * newline. The same artifact always gives the same bytes.
56
+ * newline. The same recording always gives the same bytes.
53
57
  */
54
- export function serializeArtifact(artifact) {
58
+ export function serializeRecording(recording) {
55
59
  const ordered = {
56
- schemaVersion: artifact.schemaVersion,
57
- story: { file: artifact.story.file, name: artifact.story.name },
58
- calls: artifact.calls.map((call) => ({
60
+ schemaVersion: recording.schemaVersion,
61
+ story: { file: recording.story.file, name: recording.story.name },
62
+ calls: recording.calls.map((call) => ({
59
63
  agent: call.agent,
60
64
  tool: call.tool,
61
65
  input: sortKeys(call.input),
@@ -65,10 +69,10 @@ export function serializeArtifact(artifact) {
65
69
  return `${JSON.stringify(ordered, null, 2)}\n`;
66
70
  }
67
71
  /**
68
- * `text` as an artifact, or the problem: it isn't JSON, its `schemaVersion` isn't 1, or its fields don't have the
69
- * artifact's shape (unknown fields included).
72
+ * `text` as a recording, or the problem: it isn't JSON, its `schemaVersion` isn't 1, or its fields don't have the
73
+ * recording's shape (unknown fields included).
70
74
  */
71
- export function parseArtifact(text) {
75
+ export function parseRecording(text) {
72
76
  let value;
73
77
  try {
74
78
  value = JSON.parse(text);
@@ -77,17 +81,17 @@ export function parseArtifact(text) {
77
81
  return { ok: false, problem: `not JSON: ${error.message}` };
78
82
  }
79
83
  const version = typeof value === "object" && value !== null ? value.schemaVersion : undefined;
80
- if (version !== STORY_ARTIFACT_SCHEMA_VERSION) {
84
+ if (version !== STORY_RECORDING_SCHEMA_VERSION) {
81
85
  return {
82
86
  ok: false,
83
- problem: `schemaVersion is ${JSON.stringify(version) ?? "missing"}, and this story reads ${STORY_ARTIFACT_SCHEMA_VERSION}`,
87
+ problem: `schemaVersion is ${JSON.stringify(version) ?? "missing"}, and this story core reads ${STORY_RECORDING_SCHEMA_VERSION}`,
84
88
  };
85
89
  }
86
- const parsed = artifactSchema.safeParse(value);
90
+ const parsed = recordingSchema.safeParse(value);
87
91
  if (!parsed.success) {
88
92
  const issue = parsed.error.issues[0];
89
- const where = issue === undefined || issue.path.length === 0 ? "the artifact" : issue.path.join(".");
90
- return { ok: false, problem: `${where}: ${issue?.message ?? "not an artifact"}` };
93
+ const where = issue === undefined || issue.path.length === 0 ? "the recording" : issue.path.join(".");
94
+ return { ok: false, problem: `${where}: ${issue?.message ?? "not a recording"}` };
91
95
  }
92
- return { ok: true, artifact: parsed.data };
96
+ return { ok: true, recording: parsed.data };
93
97
  }
@@ -1,15 +1,24 @@
1
- import type { StoryCall, StoryIdentity } from "./artifact.ts";
1
+ import type { StoryCall, StoryIdentity } from "./recording.ts";
2
2
  import { type StoryRecordLevel } from "./mode.ts";
3
3
  import type { StorySettlement } from "./settle.ts";
4
4
  /** `user calls slot {"command":"list --json"}`, the input as compact JSON cut to 100 characters. */
5
5
  export declare function describeCall(call: Pick<StoryCall, "agent" | "tool" | "input">): string;
6
6
  /**
7
- * One line per aligned call, then one per unreadable artifact and orphan, each with its status in brackets:
8
- * `matched`, `divergence`, `unrecorded` or `unmade`, `orphan` or `unreadable`. When the story's end `applied` its
9
- * change, an unrecorded call reads `recorded`, an accepted divergence `divergence accepted`, an unmade call
10
- * `unmade removed`, an unreadable artifact `unreadable rewritten` and an orphan `orphan removed`.
7
+ * What a story's end changed: whether it wrote or removed the story's own recording, and the orphan recordings it
8
+ * removed, or found already gone. An end that fails partway keeps what it changed before the failure.
11
9
  */
12
- export declare function storyEventLines(settlement: StorySettlement, applied: boolean): readonly string[];
10
+ export interface StoryApplied {
11
+ readonly recording: boolean;
12
+ readonly orphans: readonly string[];
13
+ }
14
+ /**
15
+ * One line per aligned call, then one per unreadable recording and orphan, each with its status in brackets:
16
+ * `matched`, `divergence`, `unrecorded` or `unmade`, `orphan` or `unreadable`. When the story's end `applied` the
17
+ * change to its recording, an unrecorded call reads `recorded`, an accepted divergence `divergence accepted`, an
18
+ * unmade call `unmade removed` and an unreadable recording `unreadable rewritten`; each orphan it removed reads
19
+ * `orphan removed`.
20
+ */
21
+ export declare function storyEventLines(settlement: StorySettlement, applied: StoryApplied): readonly string[];
13
22
  export interface StoryReportInput {
14
23
  readonly story: StoryIdentity;
15
24
  readonly level: StoryRecordLevel;
@@ -24,7 +33,10 @@ export interface StoryReportInput {
24
33
  */
25
34
  export declare function storyReport(input: StoryReportInput): string;
26
35
  /**
27
- * The line for what a passing story's end changed, or `undefined` when it changed nothing: how many calls it
28
- * recorded, divergences it accepted, unmade calls it removed, unreadable artifacts it rewrote and orphans it removed.
36
+ * What a passing story's end `applied`, or `undefined` when it changed nothing. Its first line counts what it did: the
37
+ * calls it recorded, the divergences it accepted, the unmade calls it removed, an unreadable recording it rewrote and
38
+ * the orphans it removed. One line follows for each call it rewrote and each recording it rewrote or removed, as
39
+ * `storyEventLines` gives it, so a call whose input changed with its answer shows as the unmade call removed and the
40
+ * new call recorded.
29
41
  */
30
- export declare function storyChangeLine(story: StoryIdentity, path: string, settlement: StorySettlement): string | undefined;
42
+ export declare function storyChangeReport(story: StoryIdentity, path: string, settlement: StorySettlement, applied: StoryApplied): string | undefined;