@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.
- package/dist/build-info.js +1 -1
- package/dist/commands/exec/command.d.ts +26 -0
- package/dist/commands/exec/live-view.js +29 -13
- package/dist/commands/exec/metadata.d.ts +4 -1
- package/dist/commands/exec/outcomes.d.ts +50 -0
- package/dist/commands/exec/outcomes.js +9 -0
- package/dist/commands/exec/progress.d.ts +14 -2
- package/dist/commands/exec/progress.js +54 -9
- package/dist/commands/exec/run-command.d.ts +8 -2
- package/dist/commands/exec/run-command.js +41 -8
- package/dist/commands/exec/run-metadata.d.ts +5 -2
- package/dist/commands/exec/run-metadata.js +5 -2
- package/dist/commands/exec/run-record.d.ts +83 -4
- package/dist/commands/exec/run-record.js +49 -1
- package/dist/commands/exec/view.js +36 -4
- package/dist/commands/runs/facts.d.ts +25 -0
- package/dist/commands/runs/facts.js +36 -3
- package/dist/commands/runs/list/command.d.ts +25 -0
- package/dist/commands/runs/list/outcomes.d.ts +25 -0
- package/dist/commands/runs/show/command.d.ts +25 -0
- package/dist/commands/runs/show/metadata.js +1 -1
- package/dist/commands/runs/show/outcomes.d.ts +25 -0
- package/dist/commands/runs/show/outcomes.js +30 -1
- package/dist/commands/vitest/command.d.ts +26 -0
- package/dist/commands/vitest/metadata.d.ts +4 -1
- package/dist/commands/vitest/outcomes.d.ts +25 -0
- package/dist/core/events.d.ts +9 -7
- 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/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/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/package.json +1 -1
- package/dist/story/artifact.d.ts +0 -60
- package/dist/story/live/artifact-gateway.d.ts +0 -2
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
// Saving a runner run's runner sandbox as a branch snapshot once its command ended (`--save-snapshot`): reset the
|
|
2
|
+
// working tree to its base and empty the temp directories, save the sandbox as a runner snapshot, and record it in the
|
|
3
|
+
// cache for the run's branch in its checkout. The snapshot keeps the rest of what the command left, a run secret it
|
|
4
|
+
// wrote included.
|
|
5
|
+
import { lineSplitter, outputTail, splitLines } from "./output.js";
|
|
6
|
+
import { SNAPSHOT_EXPIRATION_MS } from "./snapshot-cache.js";
|
|
7
|
+
import { REMOTE_ROOT, RESET_TO_BASE } from "./snapshot-key.js";
|
|
8
|
+
import { STEP_ENV } from "./steps.js";
|
|
9
|
+
/** How many of a failed script's last output lines its note keeps. */
|
|
10
|
+
const FAILURE_TAIL_LINES = 3;
|
|
11
|
+
/** The directories the temp scrub empties: `/tmp`, `/var/tmp`, and `$TMPDIR` when it is set. */
|
|
12
|
+
export const TEMP_DIRECTORIES = ["/tmp", "/var/tmp", '"${TMPDIR:-}"'];
|
|
13
|
+
/**
|
|
14
|
+
* Resets the working tree in `root` to its root commit, the base's tracked files (`baseIndexScript`), and removes
|
|
15
|
+
* untracked files and nested Git repositories that `.gitignore` doesn't match: the same reset the next run's apply
|
|
16
|
+
* starts with (`RESET_TO_BASE`). Ignored files, such as `node_modules`, build outputs and the turbo cache, stay.
|
|
17
|
+
*
|
|
18
|
+
* It leaves `.git` alone, so the run's commit and anything else the command put in Git stay in the snapshot. Cleaning
|
|
19
|
+
* history adds no correctness: the next run resets before it applies. Nor does it hide the run's changes: the ignored
|
|
20
|
+
* files the snapshot keeps hold their build outputs. And `git prune` can't promise it either, since it removes only
|
|
21
|
+
* loose objects, not packed ones.
|
|
22
|
+
*/
|
|
23
|
+
export function scrubGitScript(root = REMOTE_ROOT) {
|
|
24
|
+
return ["set -euo pipefail", `cd ${root}`, ...RESET_TO_BASE].join("\n");
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Empties each of `directories`, shell words that may expand to nothing: deletes what it holds, then, for anything
|
|
28
|
+
* left, tries again with `sudo -n`. Fails, naming the directory and what is left, when one still holds something.
|
|
29
|
+
*/
|
|
30
|
+
export function scrubTempScript(directories = TEMP_DIRECTORIES) {
|
|
31
|
+
return [
|
|
32
|
+
"set -euo pipefail",
|
|
33
|
+
`for dir in ${directories.join(" ")}; do`,
|
|
34
|
+
' [ -n "$dir" ] && [ -d "$dir" ] || continue',
|
|
35
|
+
' find "$dir" -mindepth 1 -maxdepth 1 -exec rm -rf -- {} + 2>/dev/null || true',
|
|
36
|
+
' if [ -n "$(find "$dir" -mindepth 1 -maxdepth 1 -print -quit)" ]; then',
|
|
37
|
+
' sudo -n find "$dir" -mindepth 1 -maxdepth 1 -exec rm -rf -- {} + 2>/dev/null || true',
|
|
38
|
+
" fi",
|
|
39
|
+
' left=$(find "$dir" -mindepth 1 -maxdepth 1 -print -quit)',
|
|
40
|
+
' if [ -n "$left" ]; then echo "couldn\'t empty $dir: $left is left" >&2; exit 1; fi',
|
|
41
|
+
"done",
|
|
42
|
+
].join("\n");
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Saves the runner sandbox, once the command ended, as a branch snapshot of the run's branch. The run's plan gave the
|
|
46
|
+
* scope no keep time (`RunnerSandboxPlan`), so however the save ends, closing stops and deletes the sandbox. It resets
|
|
47
|
+
* the working tree to the base the sandbox started from, leaving `.git` alone, empties the temp directories, saves the
|
|
48
|
+
* sandbox as a runner snapshot, which the scope then deletes, and records the entry: the started-from entry's owner,
|
|
49
|
+
* recipe, snapshot key and base, with the branch, the HEAD commit and the checkout's root. Its entry replaces the
|
|
50
|
+
* branch's entry in that checkout at that base; the snapshot it replaces is left to expire, since a run on this
|
|
51
|
+
* machine may be starting from it. Nothing here changes the run's outcome: each way it ends is a note.
|
|
52
|
+
*
|
|
53
|
+
* It searches nothing for the run's secrets: the snapshot keeps what the command wrote to `$HOME` and the ignored
|
|
54
|
+
* files, such as a resource's token, as a kept runner sandbox does. The token is limited to one test resource, and
|
|
55
|
+
* only a later run on the branch from this checkout, which gets the same token, starts from the snapshot.
|
|
56
|
+
*/
|
|
57
|
+
export async function saveBranchSnapshot(context, request, control, progress) {
|
|
58
|
+
const save = await saveOnce(context, request, control, progress);
|
|
59
|
+
if (save.kind !== "saved")
|
|
60
|
+
await progress.emit({ kind: "branch-snapshot-not-saved", save });
|
|
61
|
+
return save;
|
|
62
|
+
}
|
|
63
|
+
async function saveOnce(context, request, control, progress) {
|
|
64
|
+
const { ready, branch, head } = request;
|
|
65
|
+
const { scope, signal } = control;
|
|
66
|
+
const stopped = () => signal.aborted || scope.closed;
|
|
67
|
+
if (stopped())
|
|
68
|
+
return { kind: "stopped" };
|
|
69
|
+
const { sandbox, startedFrom } = ready;
|
|
70
|
+
const startedAt = context.clock.now().getTime();
|
|
71
|
+
await progress.emit({ kind: "branch-snapshot-saving", branch });
|
|
72
|
+
for (const script of [scrubGitScript(), scrubTempScript()]) {
|
|
73
|
+
if (stopped())
|
|
74
|
+
return { kind: "stopped" };
|
|
75
|
+
const ran = await runSaveScript(context, sandbox, script, signal);
|
|
76
|
+
if (ran.kind === "stopped")
|
|
77
|
+
return ran;
|
|
78
|
+
if (ran.kind === "failed")
|
|
79
|
+
return { kind: "failed", step: "scrub", message: ran.message };
|
|
80
|
+
}
|
|
81
|
+
if (stopped())
|
|
82
|
+
return { kind: "stopped" };
|
|
83
|
+
const saved = await scope.saveSnapshot(sandbox, { expirationMs: SNAPSHOT_EXPIRATION_MS });
|
|
84
|
+
if (!saved.ok)
|
|
85
|
+
return saved.failure.kind === "aborted"
|
|
86
|
+
? { kind: "stopped" }
|
|
87
|
+
: { kind: "failed", step: "save-snapshot", message: saved.failure.message };
|
|
88
|
+
// A signal while the snapshot saved: the snapshot exists, but the run records nothing more.
|
|
89
|
+
if (signal.aborted)
|
|
90
|
+
return { kind: "stopped" };
|
|
91
|
+
const savedAt = context.clock.now();
|
|
92
|
+
const entry = {
|
|
93
|
+
teamId: startedFrom.teamId,
|
|
94
|
+
projectId: startedFrom.projectId,
|
|
95
|
+
region: startedFrom.region,
|
|
96
|
+
runtime: startedFrom.runtime,
|
|
97
|
+
recipeName: startedFrom.recipeName,
|
|
98
|
+
snapshotKey: startedFrom.snapshotKey,
|
|
99
|
+
base: startedFrom.base,
|
|
100
|
+
branch,
|
|
101
|
+
head,
|
|
102
|
+
repoRoot: ready.repoRoot,
|
|
103
|
+
snapshotId: saved.value.snapshotId,
|
|
104
|
+
createdAt: savedAt.toISOString(),
|
|
105
|
+
expiresAt: saved.value.expiresAt ?? new Date(savedAt.getTime() + SNAPSHOT_EXPIRATION_MS).toISOString(),
|
|
106
|
+
};
|
|
107
|
+
const written = await context.runnerSnapshots.writeEntry(entry);
|
|
108
|
+
if (!written.ok)
|
|
109
|
+
await progress.emit({ kind: "snapshot-entry-not-written", failure: written.failure });
|
|
110
|
+
const ms = savedAt.getTime() - startedAt;
|
|
111
|
+
await progress.emit({
|
|
112
|
+
kind: "branch-snapshot-saved",
|
|
113
|
+
snapshotId: entry.snapshotId,
|
|
114
|
+
branch,
|
|
115
|
+
head,
|
|
116
|
+
ms,
|
|
117
|
+
sizeBytes: saved.value.sizeBytes,
|
|
118
|
+
});
|
|
119
|
+
return {
|
|
120
|
+
kind: "saved",
|
|
121
|
+
snapshotId: entry.snapshotId,
|
|
122
|
+
base: entry.base,
|
|
123
|
+
branch,
|
|
124
|
+
head,
|
|
125
|
+
ms,
|
|
126
|
+
sizeBytes: saved.value.sizeBytes,
|
|
127
|
+
entryWritten: written.ok,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
async function runSaveScript(context, sandbox, script, signal) {
|
|
131
|
+
const splitters = { stdout: lineSplitter(), stderr: lineSplitter() };
|
|
132
|
+
const tail = outputTail(FAILURE_TAIL_LINES);
|
|
133
|
+
const keep = (text) => {
|
|
134
|
+
for (const line of splitLines(text))
|
|
135
|
+
tail.add(line);
|
|
136
|
+
};
|
|
137
|
+
const ran = await context.runnerSandboxes.runScript(sandbox, { script, env: STEP_ENV }, ({ stream, data }) => keep(splitters[stream].push(data)), signal);
|
|
138
|
+
keep(splitters.stdout.flush());
|
|
139
|
+
keep(splitters.stderr.flush());
|
|
140
|
+
if (!ran.ok)
|
|
141
|
+
return ran.failure.kind === "aborted" ? { kind: "stopped" } : { kind: "failed", message: ran.failure.message };
|
|
142
|
+
const { exitCode } = ran.value;
|
|
143
|
+
if (exitCode === 0)
|
|
144
|
+
return { kind: "ok" };
|
|
145
|
+
const lines = tail.lines();
|
|
146
|
+
const ended = exitCode === null ? "ended without an exit code" : `exited ${exitCode}`;
|
|
147
|
+
return { kind: "failed", message: lines.length === 0 ? ended : `${ended}: ${lines.join(" / ")}` };
|
|
148
|
+
}
|
package/dist/story/align.d.ts
CHANGED
package/dist/story/align.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Aligning the calls a story made with the calls its
|
|
1
|
+
// Aligning the calls a story made with the calls in its recording. Pure.
|
|
2
2
|
import { canonicalJson, sameJson } from "./json.js";
|
|
3
3
|
import { diffSequences, MAX_SEQUENCE_CELLS } from "./sequence.js";
|
|
4
4
|
/** A call's key, (agent, tool, input), as one string: two calls have the same key when these are equal. */
|
|
@@ -1,23 +1,27 @@
|
|
|
1
1
|
import type { z } from "zod";
|
|
2
2
|
import type { StoryJson } from "./json.ts";
|
|
3
|
-
/** One story's session: what the tools run against. Story
|
|
3
|
+
/** One story's session: what the tools run against. Story core closes it once the story ends, pass or fail. */
|
|
4
4
|
export interface StorySession {
|
|
5
5
|
close(): void | Promise<void>;
|
|
6
6
|
}
|
|
7
7
|
/**
|
|
8
8
|
* A tool an agent calls: `input`, the schema its input must match; `run`, which does the work against the story's
|
|
9
|
-
* session with the parsed input and returns a
|
|
10
|
-
*
|
|
11
|
-
* methods, so a tool of any input and
|
|
9
|
+
* session with the parsed input and returns a value; and `observe`, which turns that return value into the
|
|
10
|
+
* observation, the JSON story core records and compares. The story gets the return value itself; only the observation
|
|
11
|
+
* reaches the recording. `run` and `observe` are methods, so a tool of any input and return value fits an agent's
|
|
12
|
+
* `tools` map (`AnyStoryTool`).
|
|
12
13
|
*/
|
|
13
|
-
export interface StoryTool<Session, Schema extends z.ZodType,
|
|
14
|
+
export interface StoryTool<Session, Schema extends z.ZodType, Returned> {
|
|
14
15
|
readonly input: Schema;
|
|
15
|
-
run(session: Session, input: z.output<Schema>):
|
|
16
|
-
observe(
|
|
16
|
+
run(session: Session, input: z.output<Schema>): Returned | Promise<Returned>;
|
|
17
|
+
observe(returned: Returned): StoryJson;
|
|
17
18
|
}
|
|
18
|
-
/** A tool with any input and
|
|
19
|
+
/** A tool with any input and return value, as an agent's `tools` map holds it. */
|
|
19
20
|
export type AnyStoryTool<Session> = StoryTool<Session, z.ZodType, unknown>;
|
|
20
|
-
/**
|
|
21
|
+
/**
|
|
22
|
+
* An agent: a name in the harness's `agents`, such as `tester` or `user`, with the tools a story calls through it. The
|
|
23
|
+
* name is part of each tool call's key; story core gives it no meaning.
|
|
24
|
+
*/
|
|
21
25
|
export interface StoryAgent<Session> {
|
|
22
26
|
readonly tools: {
|
|
23
27
|
readonly [tool: string]: AnyStoryTool<Session>;
|
|
@@ -27,14 +31,17 @@ export interface StoryAgent<Session> {
|
|
|
27
31
|
export type StoryAgents<Session> = {
|
|
28
32
|
readonly [agent: string]: StoryAgent<Session>;
|
|
29
33
|
};
|
|
30
|
-
/**
|
|
34
|
+
/**
|
|
35
|
+
* What a set of stories declares once and passes to `createStory`: `start()`, which makes each story's session, and
|
|
36
|
+
* its agents with their tools.
|
|
37
|
+
*/
|
|
31
38
|
export interface StoryHarness<Session extends StorySession, Agents extends StoryAgents<Session>> {
|
|
32
39
|
/** Starts the story's session, at its first tool call. A story that calls no tool starts none. */
|
|
33
40
|
readonly start: () => Session | Promise<Session>;
|
|
34
41
|
readonly agents: Agents;
|
|
35
42
|
}
|
|
36
|
-
/** How a story calls one tool: with the tool's input, resolving with its
|
|
37
|
-
export type StoryToolCall<Tool> = Tool extends StoryTool<infer _Session, infer Schema, infer
|
|
43
|
+
/** How a story calls one tool: with the tool's input, resolving with its return value. */
|
|
44
|
+
export type StoryToolCall<Tool> = Tool extends StoryTool<infer _Session, infer Schema, infer Returned> ? (input: z.input<Schema>) => Promise<Returned> : never;
|
|
38
45
|
/** What a story's body gets: one handle per agent, each with a function per tool. */
|
|
39
46
|
export type StoryHandles<Agents> = {
|
|
40
47
|
readonly [Agent in keyof Agents]: {
|
|
@@ -46,7 +53,7 @@ export type StoryHandles<Agents> = {
|
|
|
46
53
|
};
|
|
47
54
|
};
|
|
48
55
|
/**
|
|
49
|
-
* `tool`, typed: TypeScript infers its input from `input`, its session from `run`'s first parameter, and its
|
|
50
|
-
* from what `run` returns, so `run` and `observe` need no other annotation. Annotate `run`'s session parameter.
|
|
56
|
+
* `tool`, typed: TypeScript infers its input from `input`, its session from `run`'s first parameter, and its return
|
|
57
|
+
* value from what `run` returns, so `run` and `observe` need no other annotation. Annotate `run`'s session parameter.
|
|
51
58
|
*/
|
|
52
|
-
export declare function storyTool<Session, Schema extends z.ZodType,
|
|
59
|
+
export declare function storyTool<Session, Schema extends z.ZodType, Returned>(tool: StoryTool<Session, Schema, Returned>): StoryTool<Session, Schema, Returned>;
|
package/dist/story/definition.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// What a story harness declares: how to start a session, and the agents with their tools. Types only, but for
|
|
2
2
|
// `storyTool`, which returns its argument.
|
|
3
3
|
/**
|
|
4
|
-
* `tool`, typed: TypeScript infers its input from `input`, its session from `run`'s first parameter, and its
|
|
5
|
-
* from what `run` returns, so `run` and `observe` need no other annotation. Annotate `run`'s session parameter.
|
|
4
|
+
* `tool`, typed: TypeScript infers its input from `input`, its session from `run`'s first parameter, and its return
|
|
5
|
+
* value from what `run` returns, so `run` and `observe` need no other annotation. Annotate `run`'s session parameter.
|
|
6
6
|
*/
|
|
7
7
|
export function storyTool(tool) {
|
|
8
8
|
return tool;
|
package/dist/story/errors.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// The errors a story fails with when its body passed but its calls don't fit its
|
|
1
|
+
// The errors a story fails with when its body passed but its calls don't fit its recording.
|
|
2
2
|
/** A story made a call whose observation differs from the recorded one: the tool behaved differently. */
|
|
3
3
|
export class StoryDivergenceError extends Error {
|
|
4
4
|
name = "StoryDivergenceError";
|
package/dist/story/gateways.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
/**
|
|
3
|
-
export type
|
|
1
|
+
import type { StoryRecording } from "./recording.ts";
|
|
2
|
+
/** A recording, read: found, missing, unreadable (not a recording, with why), or a read that failed. */
|
|
3
|
+
export type StoryRecordingRead = {
|
|
4
4
|
readonly kind: "found";
|
|
5
|
-
readonly
|
|
5
|
+
readonly recording: StoryRecording;
|
|
6
6
|
} | {
|
|
7
7
|
readonly kind: "missing";
|
|
8
8
|
} | {
|
|
@@ -13,10 +13,10 @@ export type StoryArtifactRead = {
|
|
|
13
13
|
readonly message: string;
|
|
14
14
|
};
|
|
15
15
|
/**
|
|
16
|
-
*
|
|
16
|
+
* A recording, written: written, unchanged (its bytes were already there, so nothing was written), canceled (the
|
|
17
17
|
* signal aborted first, so nothing was written), or failed.
|
|
18
18
|
*/
|
|
19
|
-
export type
|
|
19
|
+
export type StoryRecordingWrite = {
|
|
20
20
|
readonly kind: "written";
|
|
21
21
|
} | {
|
|
22
22
|
readonly kind: "unchanged";
|
|
@@ -26,8 +26,8 @@ export type StoryArtifactWrite = {
|
|
|
26
26
|
readonly kind: "failed";
|
|
27
27
|
readonly message: string;
|
|
28
28
|
};
|
|
29
|
-
/**
|
|
30
|
-
export type
|
|
29
|
+
/** A recording, removed: removed, missing (nothing was there), canceled (the signal aborted first), or failed. */
|
|
30
|
+
export type StoryRecordingRemove = {
|
|
31
31
|
readonly kind: "removed";
|
|
32
32
|
} | {
|
|
33
33
|
readonly kind: "missing";
|
|
@@ -37,38 +37,58 @@ export type StoryArtifactRemove = {
|
|
|
37
37
|
readonly kind: "failed";
|
|
38
38
|
readonly message: string;
|
|
39
39
|
};
|
|
40
|
-
/**
|
|
41
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Paths listed: the recordings in one story file's recording directory, or the recording directories in one
|
|
42
|
+
* `__story__/` directory, by absolute path, sorted; none when the directory doesn't exist.
|
|
43
|
+
*/
|
|
44
|
+
export type StoryRecordingList = {
|
|
42
45
|
readonly kind: "listed";
|
|
43
46
|
readonly paths: readonly string[];
|
|
44
47
|
} | {
|
|
45
48
|
readonly kind: "failed";
|
|
46
49
|
readonly message: string;
|
|
47
50
|
};
|
|
51
|
+
/** A story file, looked for: found, missing (no file is there), or a look that failed. */
|
|
52
|
+
export type StoryFileFind = {
|
|
53
|
+
readonly kind: "found";
|
|
54
|
+
} | {
|
|
55
|
+
readonly kind: "missing";
|
|
56
|
+
} | {
|
|
57
|
+
readonly kind: "failed";
|
|
58
|
+
readonly message: string;
|
|
59
|
+
};
|
|
48
60
|
/**
|
|
49
|
-
* Where story
|
|
50
|
-
* nothing and returns `canceled`; the signal is checked again just before
|
|
51
|
-
* while the method waits still stops it.
|
|
61
|
+
* Where story recordings live: one per story, by absolute path, in its story file's recording directory. A write or a
|
|
62
|
+
* removal given an aborted `signal` changes nothing and returns `canceled`; the signal is checked again just before
|
|
63
|
+
* the change is made, so one that aborts while the method waits still stops it.
|
|
52
64
|
*/
|
|
53
|
-
export interface
|
|
54
|
-
|
|
65
|
+
export interface StoryRecordingGateway {
|
|
66
|
+
readRecording(request: {
|
|
55
67
|
readonly path: string;
|
|
56
|
-
}): Promise<
|
|
68
|
+
}): Promise<StoryRecordingRead>;
|
|
57
69
|
/**
|
|
58
|
-
* Writes `
|
|
59
|
-
* already there. A reader never sees half
|
|
70
|
+
* Writes `recording` at `path` as `serializeRecording` gives it, making missing directories, unless those bytes are
|
|
71
|
+
* already there. A reader never sees half a recording.
|
|
60
72
|
*/
|
|
61
|
-
|
|
73
|
+
writeRecording(request: {
|
|
62
74
|
readonly path: string;
|
|
63
|
-
readonly
|
|
75
|
+
readonly recording: StoryRecording;
|
|
64
76
|
readonly signal?: AbortSignal;
|
|
65
|
-
}): Promise<
|
|
66
|
-
|
|
77
|
+
}): Promise<StoryRecordingWrite>;
|
|
78
|
+
removeRecording(request: {
|
|
67
79
|
readonly path: string;
|
|
68
80
|
readonly signal?: AbortSignal;
|
|
69
|
-
}): Promise<
|
|
81
|
+
}): Promise<StoryRecordingRemove>;
|
|
70
82
|
/** The `.json` files directly in `directory`. */
|
|
71
|
-
|
|
83
|
+
listRecordings(request: {
|
|
72
84
|
readonly directory: string;
|
|
73
|
-
}): Promise<
|
|
85
|
+
}): Promise<StoryRecordingList>;
|
|
86
|
+
/** The directories directly in `root`, a `__story__/` directory: each a story file's recording directory. */
|
|
87
|
+
listRecordingDirectories(request: {
|
|
88
|
+
readonly root: string;
|
|
89
|
+
}): Promise<StoryRecordingList>;
|
|
90
|
+
/** Whether a story file is at `path`. */
|
|
91
|
+
findStoryFile(request: {
|
|
92
|
+
readonly path: string;
|
|
93
|
+
}): Promise<StoryFileFind>;
|
|
74
94
|
}
|
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;
|