@cnc-tools/storm 0.5.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.
- package/dist/build-info.js +1 -1
- package/dist/combine-errors.js +2 -2
- package/dist/commands/dashboard.js +1 -1
- package/dist/commands/{run → exec}/cleanup.d.ts +1 -1
- package/dist/commands/{run → exec}/cleanup.js +1 -1
- package/dist/commands/{run/outcomes.d.ts → exec/command.d.ts} +103 -112
- package/dist/commands/exec/command.js +23 -0
- package/dist/commands/{run → exec}/job-summary.d.ts +5 -1
- package/dist/commands/{run → exec}/job-summary.js +5 -2
- package/dist/commands/{run → exec}/live-view.js +29 -13
- package/dist/commands/exec/metadata.d.ts +69 -0
- package/dist/commands/exec/metadata.js +55 -0
- package/dist/commands/{run → exec}/mint-failed.js +4 -4
- package/dist/commands/exec/outcomes.d.ts +664 -0
- package/dist/commands/exec/outcomes.js +347 -0
- package/dist/commands/{run → exec}/progress.d.ts +14 -2
- package/dist/commands/{run → exec}/progress.js +54 -9
- package/dist/commands/exec/run-command.d.ts +227 -0
- package/dist/commands/exec/run-command.js +696 -0
- package/dist/commands/exec/run-metadata.d.ts +59 -0
- package/dist/commands/exec/run-metadata.js +61 -0
- package/dist/commands/{run → exec}/run-record.d.ts +110 -5
- package/dist/commands/{run → exec}/run-record.js +57 -1
- package/dist/commands/exec/testserver-flags.d.ts +48 -0
- package/dist/commands/exec/testserver-flags.js +80 -0
- package/dist/commands/{run → exec}/view.js +36 -4
- package/dist/commands/runs/facts.d.ts +25 -0
- package/dist/commands/runs/facts.js +38 -5
- package/dist/commands/runs/list/command.d.ts +25 -0
- package/dist/commands/runs/list/outcomes.d.ts +25 -0
- package/dist/commands/runs/logs/metadata.js +1 -1
- package/dist/commands/runs/logs/outcomes.js +4 -1
- package/dist/commands/runs/past-run.d.ts +2 -2
- package/dist/commands/runs/past-run.js +2 -2
- package/dist/commands/runs/show/command.d.ts +25 -0
- package/dist/commands/runs/show/metadata.js +2 -2
- package/dist/commands/runs/show/outcomes.d.ts +25 -0
- package/dist/commands/runs/show/outcomes.js +37 -5
- package/dist/commands/vitest/command.d.ts +325 -0
- package/dist/commands/vitest/command.js +138 -0
- package/dist/commands/vitest/metadata.d.ts +69 -0
- package/dist/commands/vitest/metadata.js +35 -0
- package/dist/commands/vitest/outcomes.d.ts +298 -0
- package/dist/commands/vitest/outcomes.js +77 -0
- package/dist/config/index.d.ts +2 -1
- package/dist/config/index.js +1 -0
- package/dist/config/resolve.d.ts +3 -3
- package/dist/config/resolve.js +3 -3
- package/dist/config/resource-config.d.ts +2 -2
- package/dist/config/schema.d.ts +1 -1
- package/dist/config/set.d.ts +15 -4
- package/dist/config/set.js +28 -12
- package/dist/config-file/define.d.ts +8 -6
- package/dist/context.d.ts +6 -3
- package/dist/context.js +4 -2
- package/dist/core/command-provider.d.ts +1 -1
- package/dist/core/events.d.ts +9 -7
- package/dist/core/presentation.js +1 -1
- package/dist/core/presentations.js +1 -1
- package/dist/core/run-state.d.ts +30 -6
- package/dist/core/run-state.js +19 -4
- package/dist/core/run.d.ts +17 -4
- package/dist/core/run.js +61 -25
- package/dist/resources/testserver/command.d.ts +1 -1
- package/dist/resources/testserver/command.js +1 -1
- package/dist/resources/testserver/config.d.ts +1 -1
- package/dist/resources/testserver/config.js +3 -2
- package/dist/resources/testserver/credential-facts.js +1 -1
- package/dist/resources/testserver/resource.d.ts +2 -2
- package/dist/resources/testserver/sandbox/create.d.ts +2 -2
- package/dist/resources/testserver/sandbox/create.js +2 -2
- package/dist/resources.d.ts +9 -6
- package/dist/resources.js +3 -3
- package/dist/runner/events.d.ts +64 -1
- package/dist/runner/gateways.d.ts +17 -6
- package/dist/runner/index.d.ts +5 -4
- package/dist/runner/index.js +4 -3
- package/dist/runner/live/snapshot-records.js +18 -4
- package/dist/runner/prepare.d.ts +23 -4
- package/dist/runner/prepare.js +13 -3
- package/dist/runner/ready.d.ts +7 -3
- package/dist/runner/ready.js +11 -5
- package/dist/runner/scope.d.ts +17 -1
- package/dist/runner/scope.js +4 -0
- package/dist/runner/snapshot-build.d.ts +3 -1
- package/dist/runner/snapshot-build.js +12 -2
- package/dist/runner/snapshot-cache.d.ts +46 -8
- package/dist/runner/snapshot-cache.js +60 -11
- package/dist/runner/snapshot-key.d.ts +12 -1
- package/dist/runner/snapshot-key.js +17 -1
- package/dist/runner/snapshot-save.d.ts +53 -0
- package/dist/runner/snapshot-save.js +148 -0
- package/dist/sdk/index.js +1 -1
- package/dist/sdk/payload/payload.d.ts +2 -2
- package/dist/sdk/payload/payload.js +2 -2
- package/dist/story/align.d.ts +1 -1
- package/dist/story/align.js +1 -1
- package/dist/story/definition.d.ts +22 -15
- package/dist/story/definition.js +2 -2
- package/dist/story/errors.js +1 -1
- package/dist/story/gateways.d.ts +45 -25
- package/dist/story/gateways.js +2 -2
- package/dist/story/host.d.ts +11 -8
- package/dist/story/host.js +37 -37
- package/dist/story/index.d.ts +1 -1
- package/dist/story/index.js +4 -4
- package/dist/story/json.d.ts +1 -1
- package/dist/story/json.js +2 -2
- package/dist/story/live/recording-gateway.d.ts +2 -0
- package/dist/story/live/{artifact-gateway.js → recording-gateway.js} +33 -12
- package/dist/story/mode.d.ts +2 -2
- package/dist/story/mode.js +1 -1
- package/dist/story/orphans.d.ts +23 -5
- package/dist/story/orphans.js +44 -8
- package/dist/story/recording.d.ts +62 -0
- package/dist/story/{artifact.js → recording.js} +31 -27
- package/dist/story/report.d.ts +21 -9
- package/dist/story/report.js +37 -25
- package/dist/story/settle.d.ts +23 -21
- package/dist/story/settle.js +17 -16
- package/dist/story/{run.d.ts → start.d.ts} +18 -17
- package/dist/story/{run.js → start.js} +66 -43
- package/dist/vercel-cli/driver.d.ts +1 -1
- package/dist/vercel-cli/entry.d.ts +1 -1
- package/dist/vercel-cli/entry.js +1 -1
- package/dist/vercel-cli/verbose.d.ts +2 -2
- package/dist/vercel-cli/verbose.js +2 -2
- package/dist/vitest/config.d.ts +5 -5
- package/dist/vitest/config.js +5 -5
- package/dist/vitest/find-vitest.d.ts +7 -0
- package/dist/vitest/find-vitest.js +28 -0
- package/dist/vitest/fixtures.d.ts +2 -2
- package/dist/vitest/fixtures.js +3 -3
- package/dist/vitest/gateways.d.ts +37 -6
- package/dist/vitest/gateways.js +3 -2
- package/dist/vitest/live/list-gateway.d.ts +7 -3
- package/dist/vitest/live/list-gateway.js +55 -8
- package/dist/vitest/plan.d.ts +41 -8
- package/dist/vitest/plan.js +47 -16
- package/dist/vitest/sandbox.d.ts +1 -1
- package/dist/vitest/sandbox.js +1 -1
- package/dist/vitest/test-events.d.ts +2 -2
- package/dist/vitest/test-events.js +3 -3
- package/dist/vitest/test-results.d.ts +1 -1
- package/dist/vitest/test-results.js +1 -1
- package/dist/vitest/types.d.ts +1 -1
- package/package.json +2 -2
- package/skills/overview.md +5 -4
- package/skills/read-run-output.md +1 -1
- package/dist/commands/run/command.d.ts +0 -327
- package/dist/commands/run/command.js +0 -620
- package/dist/commands/run/metadata.d.ts +0 -47
- package/dist/commands/run/metadata.js +0 -83
- package/dist/commands/run/outcomes.js +0 -332
- package/dist/story/artifact.d.ts +0 -60
- package/dist/story/live/artifact-gateway.d.ts +0 -2
- /package/dist/commands/{run → exec}/live-view.d.ts +0 -0
- /package/dist/commands/{run → exec}/mint-failed.d.ts +0 -0
- /package/dist/commands/{run → exec}/redaction.d.ts +0 -0
- /package/dist/commands/{run → exec}/redaction.js +0 -0
- /package/dist/commands/{run → exec}/run-collector.d.ts +0 -0
- /package/dist/commands/{run → exec}/run-collector.js +0 -0
- /package/dist/commands/{run → exec}/tests-summary.d.ts +0 -0
- /package/dist/commands/{run → exec}/tests-summary.js +0 -0
- /package/dist/commands/{run → exec}/text.d.ts +0 -0
- /package/dist/commands/{run → exec}/text.js +0 -0
- /package/dist/commands/{run → exec}/view.d.ts +0 -0
- /package/dist/commands/{run → exec}/waterfall.d.ts +0 -0
- /package/dist/commands/{run → exec}/waterfall.js +0 -0
- /package/dist/commands/{run → exec}/worker-chart.d.ts +0 -0
- /package/dist/commands/{run → exec}/worker-chart.js +0 -0
package/dist/story/gateways.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
//
|
|
2
|
-
// `test/fakes/story/
|
|
1
|
+
// Story core's gateway: where story recordings live. The live gateway is `live/recording-gateway.ts`; the fake is
|
|
2
|
+
// `test/fakes/story/recording-gateway.ts`.
|
|
3
3
|
export {};
|
package/dist/story/host.d.ts
CHANGED
|
@@ -15,13 +15,16 @@ export interface StoryHostOptions {
|
|
|
15
15
|
/** Declares one story, a Vitest test named `name`, whose body gets one handle per agent. */
|
|
16
16
|
export type StoryFunction<Agents> = (name: string, body: (agents: StoryHandles<Agents>) => Promise<void>) => void;
|
|
17
17
|
/**
|
|
18
|
-
* The `story` function for `harness`. A story's name is its full test name, its `describe` names first; its
|
|
19
|
-
* is `
|
|
20
|
-
* (`storyEventLines`), and a
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
18
|
+
* The `story` function for `harness`. A story's name is its full test name, its `describe` names first; its recording
|
|
19
|
+
* is `recordingPath(<story file>, <name>)`. When the story ends, each call becomes a Vitest annotation of type `story`
|
|
20
|
+
* (`storyEventLines`), and what a passing story changed, naming each call it rewrote (`storyChangeReport`), goes to
|
|
21
|
+
* stderr and becomes an annotation too. A story whose body threw prints its report on stderr, when there is one, and
|
|
22
|
+
* fails with its body's error; one that diverged or went stale fails with the report as the error. A story Vitest
|
|
23
|
+
* stops (a timeout, or a canceled Vitest run) ends then, without waiting for its body, and counts as one whose body
|
|
24
|
+
* threw: its session closes, and it writes nothing and removes no orphan recording, even when its body returns later
|
|
25
|
+
* or the stop comes while its end is changing the recording. When every story in the file runs (no test name filter,
|
|
26
|
+
* `.only` or `.skip` left one out), the last story the file declares also finds the orphan recordings when it ends
|
|
27
|
+
* (`looksForOrphans`, `findOrphans`): the file's own, and those in the recording directory of each story file that is
|
|
28
|
+
* gone.
|
|
26
29
|
*/
|
|
27
30
|
export declare function createStory<Session extends StorySession, Agents extends StoryAgents<Session>>(harness: StoryHarness<Session, Agents>, options?: StoryHostOptions): StoryFunction<Agents>;
|
package/dist/story/host.js
CHANGED
|
@@ -1,23 +1,24 @@
|
|
|
1
|
-
// `story()` on Vitest: the one file in `src/story/` that imports vitest. Each story is a Vitest test whose
|
|
2
|
-
// sits beside its file, and runs over the live
|
|
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 {
|
|
6
|
-
import {
|
|
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 {
|
|
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
|
|
14
|
-
* is `
|
|
15
|
-
* (`storyEventLines`), and a
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
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
|
|
29
|
-
const
|
|
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
|
|
32
|
-
bodyPassed:
|
|
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.
|
|
37
|
-
console.error(end.
|
|
38
|
-
annotations.push(context.annotate(end.
|
|
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 (!
|
|
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
|
|
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
|
|
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
|
|
58
|
-
* returns later. A body still running then keeps running: nothing can stop it, but the story ends without
|
|
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
|
|
82
|
-
* in the file runs; none otherwise.
|
|
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
|
-
|
|
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) =>
|
|
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) {
|
package/dist/story/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export {
|
|
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
3
|
export { StoryDivergenceError, StoryStaleError } from "./errors.ts";
|
|
4
4
|
export { createStory, type StoryFunction, type StoryHostOptions } from "./host.ts";
|
package/dist/story/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
// authoring guide. It depends only on Node builtins, `zod`, and `vitest` (an optional peer dependency of
|
|
1
|
+
// Story core, exported as `@cnc-tools/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
4
|
// `@cnc-tools/storm`), and on nothing else in storm; `.oxlintrc.json` checks the boundary.
|
|
5
|
-
export {
|
|
5
|
+
export { recordingDirectory, recordingPath, parseRecording, STORY_RECORDING_SCHEMA_VERSION, } from "./recording.js";
|
|
6
6
|
export { storyTool, } from "./definition.js";
|
|
7
7
|
export { StoryDivergenceError, StoryStaleError } from "./errors.js";
|
|
8
8
|
export { createStory } from "./host.js";
|
package/dist/story/json.d.ts
CHANGED
package/dist/story/json.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// The JSON a story
|
|
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
|
|
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;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
// The live
|
|
2
|
-
// and is renamed into place, so a reader never sees half
|
|
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 {
|
|
8
|
-
export const
|
|
9
|
-
async
|
|
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 =
|
|
18
|
-
return parsed.ok ? { kind: "found",
|
|
17
|
+
const parsed = parseRecording(text);
|
|
18
|
+
return parsed.ok ? { kind: "found", recording: parsed.recording } : { kind: "unreadable", problem: parsed.problem };
|
|
19
19
|
},
|
|
20
|
-
async
|
|
20
|
+
async writeRecording({ path, recording, signal }) {
|
|
21
21
|
if (aborted(signal))
|
|
22
22
|
return { kind: "canceled" };
|
|
23
|
-
const text =
|
|
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
|
|
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
|
|
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) {
|
package/dist/story/mode.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* How much a story
|
|
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
|
|
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
|
*/
|
package/dist/story/mode.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// How much a story
|
|
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"];
|
package/dist/story/orphans.d.ts
CHANGED
|
@@ -1,4 +1,16 @@
|
|
|
1
|
-
import type {
|
|
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
|
|
11
|
-
*
|
|
12
|
-
*
|
|
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:
|
|
35
|
+
}, gateway: StoryRecordingGateway): Promise<StoryOrphans>;
|
package/dist/story/orphans.js
CHANGED
|
@@ -1,15 +1,51 @@
|
|
|
1
|
-
// Finding a story file's
|
|
2
|
-
//
|
|
3
|
-
|
|
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
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
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
|
|
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
|
-
|
|
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
|
|
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
|
|
8
|
-
export const
|
|
9
|
-
/** How much of the story's name its
|
|
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
|
|
12
|
-
schemaVersion: z.literal(
|
|
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
|
|
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
|
|
19
|
+
* file owns a recording, whatever the recording holds.
|
|
20
20
|
*/
|
|
21
|
-
export function
|
|
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
|
|
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
|
|
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(
|
|
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
|
|
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
|
|
56
|
+
* newline. The same recording always gives the same bytes.
|
|
53
57
|
*/
|
|
54
|
-
export function
|
|
58
|
+
export function serializeRecording(recording) {
|
|
55
59
|
const ordered = {
|
|
56
|
-
schemaVersion:
|
|
57
|
-
story: { file:
|
|
58
|
-
calls:
|
|
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
|
|
69
|
-
*
|
|
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
|
|
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 !==
|
|
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 ${
|
|
87
|
+
problem: `schemaVersion is ${JSON.stringify(version) ?? "missing"}, and this story core reads ${STORY_RECORDING_SCHEMA_VERSION}`,
|
|
84
88
|
};
|
|
85
89
|
}
|
|
86
|
-
const parsed =
|
|
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
|
|
90
|
-
return { ok: false, problem: `${where}: ${issue?.message ?? "not
|
|
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,
|
|
96
|
+
return { ok: true, recording: parsed.data };
|
|
93
97
|
}
|