@cnc-tools/storm 0.6.0 → 0.7.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 (80) hide show
  1. package/dist/build-info.js +1 -1
  2. package/dist/commands/exec/command.d.ts +26 -0
  3. package/dist/commands/exec/live-view.js +29 -13
  4. package/dist/commands/exec/metadata.d.ts +4 -1
  5. package/dist/commands/exec/outcomes.d.ts +50 -0
  6. package/dist/commands/exec/outcomes.js +9 -0
  7. package/dist/commands/exec/progress.d.ts +14 -2
  8. package/dist/commands/exec/progress.js +54 -9
  9. package/dist/commands/exec/run-command.d.ts +8 -2
  10. package/dist/commands/exec/run-command.js +41 -8
  11. package/dist/commands/exec/run-metadata.d.ts +5 -2
  12. package/dist/commands/exec/run-metadata.js +5 -2
  13. package/dist/commands/exec/run-record.d.ts +83 -4
  14. package/dist/commands/exec/run-record.js +49 -1
  15. package/dist/commands/exec/view.js +36 -4
  16. package/dist/commands/runs/facts.d.ts +25 -0
  17. package/dist/commands/runs/facts.js +36 -3
  18. package/dist/commands/runs/list/command.d.ts +25 -0
  19. package/dist/commands/runs/list/outcomes.d.ts +25 -0
  20. package/dist/commands/runs/show/command.d.ts +25 -0
  21. package/dist/commands/runs/show/metadata.js +1 -1
  22. package/dist/commands/runs/show/outcomes.d.ts +25 -0
  23. package/dist/commands/runs/show/outcomes.js +30 -1
  24. package/dist/commands/vitest/command.d.ts +26 -0
  25. package/dist/commands/vitest/metadata.d.ts +4 -1
  26. package/dist/commands/vitest/outcomes.d.ts +25 -0
  27. package/dist/core/events.d.ts +9 -7
  28. package/dist/core/run-state.d.ts +30 -6
  29. package/dist/core/run-state.js +19 -4
  30. package/dist/core/run.d.ts +17 -4
  31. package/dist/core/run.js +61 -25
  32. package/dist/runner/events.d.ts +64 -1
  33. package/dist/runner/gateways.d.ts +17 -6
  34. package/dist/runner/index.d.ts +5 -4
  35. package/dist/runner/index.js +4 -3
  36. package/dist/runner/live/snapshot-records.js +18 -4
  37. package/dist/runner/prepare.d.ts +23 -4
  38. package/dist/runner/prepare.js +13 -3
  39. package/dist/runner/ready.d.ts +7 -3
  40. package/dist/runner/ready.js +11 -5
  41. package/dist/runner/scope.d.ts +17 -1
  42. package/dist/runner/scope.js +4 -0
  43. package/dist/runner/snapshot-build.d.ts +3 -1
  44. package/dist/runner/snapshot-build.js +12 -2
  45. package/dist/runner/snapshot-cache.d.ts +46 -8
  46. package/dist/runner/snapshot-cache.js +60 -11
  47. package/dist/runner/snapshot-key.d.ts +12 -1
  48. package/dist/runner/snapshot-key.js +17 -1
  49. package/dist/runner/snapshot-save.d.ts +53 -0
  50. package/dist/runner/snapshot-save.js +148 -0
  51. package/dist/story/align.d.ts +1 -1
  52. package/dist/story/align.js +1 -1
  53. package/dist/story/definition.d.ts +22 -15
  54. package/dist/story/definition.js +2 -2
  55. package/dist/story/errors.js +1 -1
  56. package/dist/story/gateways.d.ts +45 -25
  57. package/dist/story/gateways.js +2 -2
  58. package/dist/story/host.d.ts +11 -8
  59. package/dist/story/host.js +37 -37
  60. package/dist/story/index.d.ts +1 -1
  61. package/dist/story/index.js +4 -4
  62. package/dist/story/json.d.ts +1 -1
  63. package/dist/story/json.js +2 -2
  64. package/dist/story/live/recording-gateway.d.ts +2 -0
  65. package/dist/story/live/{artifact-gateway.js → recording-gateway.js} +33 -12
  66. package/dist/story/mode.d.ts +2 -2
  67. package/dist/story/mode.js +1 -1
  68. package/dist/story/orphans.d.ts +23 -5
  69. package/dist/story/orphans.js +44 -8
  70. package/dist/story/recording.d.ts +62 -0
  71. package/dist/story/{artifact.js → recording.js} +31 -27
  72. package/dist/story/report.d.ts +21 -9
  73. package/dist/story/report.js +37 -25
  74. package/dist/story/settle.d.ts +23 -21
  75. package/dist/story/settle.js +17 -16
  76. package/dist/story/{run.d.ts → start.d.ts} +18 -17
  77. package/dist/story/{run.js → start.js} +66 -43
  78. package/package.json +1 -1
  79. package/dist/story/artifact.d.ts +0 -60
  80. package/dist/story/live/artifact-gateway.d.ts +0 -2
@@ -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;
@@ -1,5 +1,5 @@
1
- // What a story says about its calls: one event line per call, the report of a failed story, and the line for what a
2
- // passing story changed. Pure.
1
+ // What a story says about its calls: one event line per call, the report of a failed story, and what a passing story
2
+ // changed. Pure.
3
3
  import { describeObservationDiff } from "./diff.js";
4
4
  import { STORY_RECORD_VARIABLE } from "./mode.js";
5
5
  /** How much of a call's input its line keeps. */
@@ -10,22 +10,24 @@ export function describeCall(call) {
10
10
  return `${call.agent} calls ${call.tool} ${input.length > INPUT_CHARS ? `${input.slice(0, INPUT_CHARS - 1)}…` : input}`;
11
11
  }
12
12
  /**
13
- * One line per aligned call, then one per unreadable artifact and orphan, each with its status in brackets:
14
- * `matched`, `divergence`, `unrecorded` or `unmade`, `orphan` or `unreadable`. When the story's end `applied` its
15
- * change, an unrecorded call reads `recorded`, an accepted divergence `divergence accepted`, an unmade call
16
- * `unmade removed`, an unreadable artifact `unreadable rewritten` and an orphan `orphan removed`.
13
+ * One line per aligned call, then one per unreadable recording and orphan, each with its status in brackets:
14
+ * `matched`, `divergence`, `unrecorded` or `unmade`, `orphan` or `unreadable`. When the story's end `applied` the
15
+ * change to its recording, an unrecorded call reads `recorded`, an accepted divergence `divergence accepted`, an
16
+ * unmade call `unmade removed` and an unreadable recording `unreadable rewritten`; each orphan it removed reads
17
+ * `orphan removed`.
17
18
  */
18
19
  export function storyEventLines(settlement, applied) {
19
- const written = applied && settlement.change.kind !== "leave";
20
- const calls = settlement.aligned.map((each) => `${describeAligned(each)} [${callStatus(each, written, settlement.accepted)}]`);
21
- const files = settlement.stale.flatMap((entry) => entry.kind === "unreadable"
22
- ? [`artifact ${entry.path} [${written ? "unreadable rewritten" : "unreadable"}]`]
20
+ const calls = settlement.aligned.map((each) => `${describeAligned(each)} [${callStatus(each, applied.recording, settlement.accepted)}]`);
21
+ return [...calls, ...recordingLines(settlement, applied)];
22
+ }
23
+ /** One line per unreadable recording and orphan recording, each with its status, as `storyEventLines` gives it. */
24
+ function recordingLines(settlement, applied) {
25
+ const removed = new Set(applied.orphans);
26
+ return settlement.stale.flatMap((entry) => entry.kind === "unreadable"
27
+ ? [`recording ${entry.path} [${applied.recording ? "unreadable rewritten" : "unreadable"}]`]
23
28
  : entry.kind === "orphan"
24
- ? [
25
- `artifact ${entry.path} [${applied && settlement.removeOrphans.includes(entry.path) ? "orphan removed" : "orphan"}]`,
26
- ]
29
+ ? [`recording ${entry.path} [${removed.has(entry.path) ? "orphan removed" : "orphan"}]`]
27
30
  : []);
28
- return [...calls, ...files];
29
31
  }
30
32
  function describeAligned(each) {
31
33
  return describeCall(each.kind === "unmade" ? each.recorded : each.live);
@@ -85,18 +87,20 @@ function summary(input) {
85
87
  if (input.bodyFailed)
86
88
  return `${counts} The story threw, so nothing was written.`;
87
89
  if (divergences.length > 0) {
88
- return `${counts} A divergence means the tool behaved differently. If that is intended, record it with ${STORY_RECORD_VARIABLE}=all.`;
90
+ return `${counts} A divergence means the tool behaved differently. If that is intended, accept it with ${STORY_RECORD_VARIABLE}=all.`;
89
91
  }
90
- return `${counts} The recording no longer fits the story. Record it again with ${STORY_RECORD_VARIABLE}=stale.`;
92
+ return `${counts} The recording no longer fits the story. Rewrite it with ${STORY_RECORD_VARIABLE}=stale.`;
91
93
  }
92
94
  /**
93
- * The line for what a passing story's end changed, or `undefined` when it changed nothing: how many calls it
94
- * recorded, divergences it accepted, unmade calls it removed, unreadable artifacts it rewrote and orphans it removed.
95
+ * What a passing story's end `applied`, or `undefined` when it changed nothing. Its first line counts what it did: the
96
+ * calls it recorded, the divergences it accepted, the unmade calls it removed, an unreadable recording it rewrote and
97
+ * the orphans it removed. One line follows for each call it rewrote and each recording it rewrote or removed, as
98
+ * `storyEventLines` gives it, so a call whose input changed with its answer shows as the unmade call removed and the
99
+ * new call recorded.
95
100
  */
96
- export function storyChangeLine(story, path, settlement) {
101
+ export function storyChangeReport(story, path, settlement, applied) {
97
102
  const parts = [];
98
- const written = settlement.change.kind !== "leave";
99
- if (written) {
103
+ if (applied.recording) {
100
104
  const kinds = (kind) => settlement.stale.filter((entry) => entry.kind === kind).length;
101
105
  if (kinds("unrecorded") > 0)
102
106
  parts.push(`recorded ${count(kinds("unrecorded"), "call")}`);
@@ -105,13 +109,21 @@ export function storyChangeLine(story, path, settlement) {
105
109
  if (kinds("unmade") > 0)
106
110
  parts.push(`removed ${count(kinds("unmade"), "unmade call")}`);
107
111
  if (kinds("unreadable") > 0)
108
- parts.push("rewrote an unreadable artifact");
112
+ parts.push("rewrote an unreadable recording");
109
113
  }
110
- if (settlement.removeOrphans.length > 0)
111
- parts.push(`removed ${count(settlement.removeOrphans.length, "orphan")}`);
114
+ if (applied.orphans.length > 0)
115
+ parts.push(`removed ${count(applied.orphans.length, "orphan recording")}`);
112
116
  if (parts.length === 0)
113
117
  return undefined;
114
- return `story "${story.name}": ${parts.join(", ")} (${path})`;
118
+ const changed = [
119
+ ...(applied.recording ? rewrittenCallLines(settlement) : []),
120
+ ...recordingLines(settlement, applied),
121
+ ];
122
+ return [`story "${story.name}": ${parts.join(", ")} (${path})`, ...changed.map((line) => ` ${line}`)].join("\n");
123
+ }
124
+ /** The event line of each aligned call a written recording changed: every one but the matched ones. */
125
+ function rewrittenCallLines(settlement) {
126
+ return settlement.aligned.flatMap((each) => each.kind === "matched" ? [] : [`${describeAligned(each)} [${callStatus(each, true, settlement.accepted)}]`]);
115
127
  }
116
128
  function count(n, one, many = `${one}s`) {
117
129
  return `${n} ${n === 1 ? one : many}`;
@@ -1,5 +1,5 @@
1
1
  import { type StoryAlignedCall } from "./align.ts";
2
- import type { StoryArtifact, StoryCall, StoryIdentity } from "./artifact.ts";
2
+ import type { StoryRecording, StoryCall, StoryIdentity } from "./recording.ts";
3
3
  import type { StoryRecordLevel } from "./mode.ts";
4
4
  /** A recorded call and a live call with the same key and different observations: the tool behaved differently. */
5
5
  export interface StoryDivergence {
@@ -11,8 +11,9 @@ export interface StoryDivergence {
11
11
  *
12
12
  * - `unrecorded`: a live call with no recorded call.
13
13
  * - `unmade`: a recorded call with no live call.
14
- * - `orphan`: an artifact in the story file's artifact directory that belongs to no story in the file.
15
- * - `unreadable`: the story's artifact doesn't parse, or has another schema version.
14
+ * - `orphan`: a recording in the story file's recording directory that belongs to no story in the file, or one in the
15
+ * recording directory of a story file that is gone.
16
+ * - `unreadable`: the story's recording doesn't parse, or has another schema version.
16
17
  */
17
18
  export type StoryStaleEntry = {
18
19
  readonly kind: "unrecorded";
@@ -28,20 +29,20 @@ export type StoryStaleEntry = {
28
29
  readonly path: string;
29
30
  readonly problem: string;
30
31
  };
31
- /** The story's artifact as its run found it. */
32
- export type StoryRecording = {
32
+ /** The story's recording as the story's end read it: found, missing, or unreadable (a read that didn't fail). */
33
+ export type RecordingRead = {
33
34
  readonly kind: "found";
34
- readonly artifact: StoryArtifact;
35
+ readonly recording: StoryRecording;
35
36
  } | {
36
37
  readonly kind: "missing";
37
38
  } | {
38
39
  readonly kind: "unreadable";
39
40
  readonly problem: string;
40
41
  };
41
- /** What to do with the story's own artifact: write it, remove it, or leave it. */
42
- export type StoryArtifactChange = {
42
+ /** What to do with the story's own recording: write it, remove it, or leave it. */
43
+ export type RecordingChange = {
43
44
  readonly kind: "write";
44
- readonly artifact: StoryArtifact;
45
+ readonly recording: StoryRecording;
45
46
  } | {
46
47
  readonly kind: "remove";
47
48
  } | {
@@ -49,15 +50,15 @@ export type StoryArtifactChange = {
49
50
  };
50
51
  export interface StorySettleInput {
51
52
  readonly story: StoryIdentity;
52
- /** The story's artifact path, which an unreadable entry names. */
53
+ /** The story's recording path, which an unreadable entry names. */
53
54
  readonly path: string;
54
55
  readonly level: StoryRecordLevel;
55
56
  /** Whether the story's body returned without throwing. */
56
57
  readonly bodyPassed: boolean;
57
- readonly recording: StoryRecording;
58
+ readonly read: RecordingRead;
58
59
  /** The calls the story made, in order. */
59
60
  readonly live: readonly StoryCall[];
60
- /** The story file's orphans, when this story's end checks for them; none otherwise. */
61
+ /** The orphan recordings, when this story's end looks for them (`findOrphans`); none otherwise. */
61
62
  readonly orphans: readonly string[];
62
63
  }
63
64
  export interface StorySettlement {
@@ -70,9 +71,9 @@ export interface StorySettlement {
70
71
  readonly unreached: number;
71
72
  readonly divergences: readonly StoryDivergence[];
72
73
  readonly stale: readonly StoryStaleEntry[];
73
- readonly change: StoryArtifactChange;
74
- /** The orphans to remove. */
75
- readonly removeOrphans: readonly string[];
74
+ readonly change: RecordingChange;
75
+ /** Whether the end removes the orphan recordings, the `orphan` entries in `stale`. */
76
+ readonly removesOrphans: boolean;
76
77
  /** Whether the divergences are recorded as the new observations (`STORY_RECORD=all`). */
77
78
  readonly accepted: boolean;
78
79
  /** Why the story fails, beyond its body: a divergence, or a stale entry at `none`. */
@@ -80,15 +81,16 @@ export interface StorySettlement {
80
81
  }
81
82
  /**
82
83
  * Settles one story's end. Its live calls are aligned with the recorded ones (`alignCalls`); each `divergence` is a
83
- * divergence, and each `unrecorded` and `unmade` call, an unreadable artifact and each orphan is a stale entry. A
84
- * story whose body threw changes nothing and fails with its own error; the recorded calls after its last aligned call
85
- * count as unreached, not unmade. Otherwise, by level:
84
+ * divergence, and each `unrecorded` and `unmade` call, an unreadable recording and each orphan recording is a stale
85
+ * entry. A story whose body threw changes nothing and fails with its own error; the recorded calls after its last
86
+ * aligned call count as unreached, not unmade. Otherwise, by level:
86
87
  *
87
88
  * - `none` changes nothing, and fails on a divergence, else on a stale entry.
88
- * - `stale` fails on a divergence, changing nothing; otherwise it writes the artifact and removes the orphans.
89
- * - `all` writes the artifact and removes the orphans, accepting any divergence.
89
+ * - `stale` fails on a divergence, changing nothing; otherwise it writes the recording and removes the orphan
90
+ * recordings.
91
+ * - `all` writes the recording and removes the orphan recordings, accepting any divergence.
90
92
  *
91
- * The artifact written holds the live calls. A story that made no call has no artifact, so writing it removes one
93
+ * The recording written holds the live calls. A story that made no call has no recording, so writing it removes one
92
94
  * that is there.
93
95
  */
94
96
  export declare function settleStory(input: StorySettleInput): StorySettlement;