@autumnsgrove/lyra 0.1.0 → 0.3.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/adapters/cloudflare/index.d.ts +1 -0
- package/dist/adapters/cloudflare/index.d.ts.map +1 -1
- package/dist/adapters/cloudflare/index.js +1 -0
- package/dist/adapters/cloudflare/index.js.map +1 -1
- package/dist/adapters/cloudflare/session.d.ts +102 -0
- package/dist/adapters/cloudflare/session.d.ts.map +1 -0
- package/dist/adapters/cloudflare/session.js +595 -0
- package/dist/adapters/cloudflare/session.js.map +1 -0
- package/dist/adapters/openai/errors.d.ts +36 -0
- package/dist/adapters/openai/errors.d.ts.map +1 -0
- package/dist/adapters/openai/errors.js +117 -0
- package/dist/adapters/openai/errors.js.map +1 -0
- package/dist/adapters/openai/index.d.ts +18 -0
- package/dist/adapters/openai/index.d.ts.map +1 -0
- package/dist/adapters/openai/index.js +18 -0
- package/dist/adapters/openai/index.js.map +1 -0
- package/dist/adapters/openai/model.d.ts +50 -0
- package/dist/adapters/openai/model.d.ts.map +1 -0
- package/dist/adapters/openai/model.js +169 -0
- package/dist/adapters/openai/model.js.map +1 -0
- package/dist/adapters/openai/presets.d.ts +36 -0
- package/dist/adapters/openai/presets.d.ts.map +1 -0
- package/dist/adapters/openai/presets.js +43 -0
- package/dist/adapters/openai/presets.js.map +1 -0
- package/dist/adapters/openai/request.d.ts +47 -0
- package/dist/adapters/openai/request.d.ts.map +1 -0
- package/dist/adapters/openai/request.js +93 -0
- package/dist/adapters/openai/request.js.map +1 -0
- package/dist/adapters/openai/sse.d.ts +44 -0
- package/dist/adapters/openai/sse.d.ts.map +1 -0
- package/dist/adapters/openai/sse.js +410 -0
- package/dist/adapters/openai/sse.js.map +1 -0
- package/dist/core/approval.d.ts +38 -0
- package/dist/core/approval.d.ts.map +1 -0
- package/dist/core/approval.js +47 -0
- package/dist/core/approval.js.map +1 -0
- package/dist/core/compaction.d.ts +193 -0
- package/dist/core/compaction.d.ts.map +1 -0
- package/dist/core/compaction.js +460 -0
- package/dist/core/compaction.js.map +1 -0
- package/dist/core/define-recipe.d.ts +8 -1
- package/dist/core/define-recipe.d.ts.map +1 -1
- package/dist/core/define-recipe.js +13 -0
- package/dist/core/define-recipe.js.map +1 -1
- package/dist/core/events.d.ts +32 -2
- package/dist/core/events.d.ts.map +1 -1
- package/dist/core/events.js +16 -1
- package/dist/core/events.js.map +1 -1
- package/dist/core/execute-tools.d.ts +7 -0
- package/dist/core/execute-tools.d.ts.map +1 -1
- package/dist/core/execute-tools.js +6 -0
- package/dist/core/execute-tools.js.map +1 -1
- package/dist/core/hash.d.ts +10 -0
- package/dist/core/hash.d.ts.map +1 -0
- package/dist/core/hash.js +14 -0
- package/dist/core/hash.js.map +1 -0
- package/dist/core/model.d.ts +12 -0
- package/dist/core/model.d.ts.map +1 -1
- package/dist/core/propose.d.ts +29 -0
- package/dist/core/propose.d.ts.map +1 -0
- package/dist/core/propose.js +75 -0
- package/dist/core/propose.js.map +1 -0
- package/dist/core/recipe.d.ts +2 -0
- package/dist/core/recipe.d.ts.map +1 -1
- package/dist/core/recipe.js +5 -8
- package/dist/core/recipe.js.map +1 -1
- package/dist/core/resume.d.ts +19 -1
- package/dist/core/resume.d.ts.map +1 -1
- package/dist/core/resume.js +64 -6
- package/dist/core/resume.js.map +1 -1
- package/dist/core/retry.d.ts +9 -0
- package/dist/core/retry.d.ts.map +1 -0
- package/dist/core/retry.js +9 -0
- package/dist/core/retry.js.map +1 -0
- package/dist/core/run-handle.d.ts +5 -0
- package/dist/core/run-handle.d.ts.map +1 -1
- package/dist/core/run-handle.js +1 -0
- package/dist/core/run-handle.js.map +1 -1
- package/dist/core/run.d.ts +29 -2
- package/dist/core/run.d.ts.map +1 -1
- package/dist/core/run.js +526 -132
- package/dist/core/run.js.map +1 -1
- package/dist/core/runtime.d.ts +12 -0
- package/dist/core/runtime.d.ts.map +1 -1
- package/dist/core/runtime.js +34 -2
- package/dist/core/runtime.js.map +1 -1
- package/dist/core/system-clock.d.ts.map +1 -1
- package/dist/core/system-clock.js +5 -1
- package/dist/core/system-clock.js.map +1 -1
- package/dist/core/tool.d.ts +55 -0
- package/dist/core/tool.d.ts.map +1 -1
- package/dist/core/tool.js +16 -0
- package/dist/core/tool.js.map +1 -1
- package/dist/core/usage.d.ts +27 -0
- package/dist/core/usage.d.ts.map +1 -0
- package/dist/core/usage.js +67 -0
- package/dist/core/usage.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -1
- package/dist/protocol/answers.d.ts +33 -0
- package/dist/protocol/answers.d.ts.map +1 -0
- package/dist/protocol/answers.js +86 -0
- package/dist/protocol/answers.js.map +1 -0
- package/dist/protocol/commands.d.ts +84 -0
- package/dist/protocol/commands.d.ts.map +1 -0
- package/dist/protocol/commands.js +232 -0
- package/dist/protocol/commands.js.map +1 -0
- package/dist/protocol/events.d.ts +145 -0
- package/dist/protocol/events.d.ts.map +1 -0
- package/dist/protocol/events.js +195 -0
- package/dist/protocol/events.js.map +1 -0
- package/dist/protocol/frames.d.ts +72 -0
- package/dist/protocol/frames.d.ts.map +1 -0
- package/dist/protocol/frames.js +134 -0
- package/dist/protocol/frames.js.map +1 -0
- package/dist/protocol/http.d.ts +52 -0
- package/dist/protocol/http.d.ts.map +1 -0
- package/dist/protocol/http.js +357 -0
- package/dist/protocol/http.js.map +1 -0
- package/dist/protocol/reader.d.ts +35 -0
- package/dist/protocol/reader.d.ts.map +1 -0
- package/dist/protocol/reader.js +71 -0
- package/dist/protocol/reader.js.map +1 -0
- package/dist/protocol/session.d.ts +85 -0
- package/dist/protocol/session.d.ts.map +1 -0
- package/dist/protocol/session.js +801 -0
- package/dist/protocol/session.js.map +1 -0
- package/dist/record/entries.d.ts +110 -2
- package/dist/record/entries.d.ts.map +1 -1
- package/dist/record/entries.js +337 -10
- package/dist/record/entries.js.map +1 -1
- package/dist/record/replay.d.ts +6 -0
- package/dist/record/replay.d.ts.map +1 -1
- package/dist/record/replay.js +11 -3
- package/dist/record/replay.js.map +1 -1
- package/dist/record/truncate.d.ts +12 -0
- package/dist/record/truncate.d.ts.map +1 -1
- package/dist/record/truncate.js +72 -1
- package/dist/record/truncate.js.map +1 -1
- package/dist/testkit/cassette.d.ts +91 -0
- package/dist/testkit/cassette.d.ts.map +1 -0
- package/dist/testkit/cassette.js +250 -0
- package/dist/testkit/cassette.js.map +1 -0
- package/dist/testkit/fake-model-server.d.ts +104 -0
- package/dist/testkit/fake-model-server.d.ts.map +1 -0
- package/dist/testkit/fake-model-server.js +251 -0
- package/dist/testkit/fake-model-server.js.map +1 -0
- package/dist/testkit/index.d.ts +5 -2
- package/dist/testkit/index.d.ts.map +1 -1
- package/dist/testkit/index.js +5 -2
- package/dist/testkit/index.js.map +1 -1
- package/dist/testkit/misbehaving-model.d.ts +40 -0
- package/dist/testkit/misbehaving-model.d.ts.map +1 -0
- package/dist/testkit/misbehaving-model.js +134 -0
- package/dist/testkit/misbehaving-model.js.map +1 -0
- package/dist/testkit/model.d.ts +20 -0
- package/dist/testkit/model.d.ts.map +1 -1
- package/dist/testkit/model.js +50 -3
- package/dist/testkit/model.js.map +1 -1
- package/dist/testkit/scenario.d.ts +36 -2
- package/dist/testkit/scenario.d.ts.map +1 -1
- package/dist/testkit/scenario.js +98 -13
- package/dist/testkit/scenario.js.map +1 -1
- package/dist/testkit/timeline-recipe.d.ts.map +1 -1
- package/dist/testkit/timeline-recipe.js +3 -0
- package/dist/testkit/timeline-recipe.js.map +1 -1
- package/dist/testkit/tools.d.ts +15 -1
- package/dist/testkit/tools.d.ts.map +1 -1
- package/dist/testkit/tools.js +19 -0
- package/dist/testkit/tools.js.map +1 -1
- package/package.json +5 -1
package/dist/testkit/index.js
CHANGED
|
@@ -7,9 +7,12 @@
|
|
|
7
7
|
* enforces.
|
|
8
8
|
*/
|
|
9
9
|
export { fakeClock, FAKE_CLOCK_START } from "./clock.js";
|
|
10
|
-
export {
|
|
10
|
+
export { fakeModelServer, sseFor, } from "./fake-model-server.js";
|
|
11
|
+
export { scriptedArguments, scriptedModel, } from "./model.js";
|
|
12
|
+
export { decodeCassette, encodeCassette, recordingFetch, replayCassette, scanForSecrets, scrubExchange, } from "./cassette.js";
|
|
13
|
+
export { misbehavingModel, misbehavingScript, MISBEHAVING_KINDS, } from "./misbehaving-model.js";
|
|
11
14
|
export { failingStorage } from "./storage.js";
|
|
12
15
|
export { crashingStorage, } from "./crashing-storage.js";
|
|
13
16
|
export { renderHandles, renderPage, timelineTestRecipe, } from "./timeline-recipe.js";
|
|
14
|
-
export { createFinishTool, executionCounter, toolReturning, toolReturningError, toolTakingTime, toolThatThrows, } from "./tools.js";
|
|
17
|
+
export { createFinishTool, executionCounter, toolAsking, toolReturning, toolReturningError, toolTakingTime, toolThatThrows, } from "./tools.js";
|
|
15
18
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/testkit/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAkB,MAAM,YAAY,CAAC;AACzE,OAAO,EACN,aAAa,GAMb,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,cAAc,EAAmD,MAAM,cAAc,CAAC;AAC/F,OAAO,EACN,eAAe,GAGf,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACN,aAAa,EACb,UAAU,EACV,kBAAkB,GAMlB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EACN,gBAAgB,EAChB,gBAAgB,EAChB,aAAa,EACb,kBAAkB,EAClB,cAAc,EACd,cAAc,GAGd,MAAM,YAAY,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/testkit/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAkB,MAAM,YAAY,CAAC;AACzE,OAAO,EACN,eAAe,EACf,MAAM,GAKN,MAAM,wBAAwB,CAAC;AAChC,OAAO,EACN,iBAAiB,EACjB,aAAa,GAMb,MAAM,YAAY,CAAC;AACpB,OAAO,EACN,cAAc,EACd,cAAc,EACd,cAAc,EACd,cAAc,EACd,cAAc,EACd,aAAa,GASb,MAAM,eAAe,CAAC;AACvB,OAAO,EACN,gBAAgB,EAChB,iBAAiB,EACjB,iBAAiB,GAKjB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,cAAc,EAAmD,MAAM,cAAc,CAAC;AAC/F,OAAO,EACN,eAAe,GAGf,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACN,aAAa,EACb,UAAU,EACV,kBAAkB,GAMlB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EACN,gBAAgB,EAChB,gBAAgB,EAChB,UAAU,EACV,aAAa,EACb,kBAAkB,EAClB,cAAc,EACd,cAAc,GAGd,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A model that is wrong on purpose, from a seed (docs/engineering-practices.md,
|
|
3
|
+
* KIT12 and KIT13; decision 64).
|
|
4
|
+
*
|
|
5
|
+
* The scripted model says what a well-behaved provider does. This one says what
|
|
6
|
+
* a real one sometimes does instead: half-written arguments, a tool nobody has,
|
|
7
|
+
* the same call again, an empty answer, a reply cut off by the token limit, and
|
|
8
|
+
* usage with no cost. The run in front of it must still end exactly once with a
|
|
9
|
+
* record that loads, whatever it is given, and the seed is what makes a failure
|
|
10
|
+
* repeatable.
|
|
11
|
+
*
|
|
12
|
+
* One script is drawn per seed, so the same seed produces the same run. The
|
|
13
|
+
* generator assumes the run has a `finish` tool, because that is how a run ends
|
|
14
|
+
* well; a run without one parks, which is still a correct ending.
|
|
15
|
+
*/
|
|
16
|
+
import type { Clock } from "../core/clock.js";
|
|
17
|
+
import type { ModelSlot } from "../core/model.js";
|
|
18
|
+
import { type ScriptedTurn } from "./model.js";
|
|
19
|
+
/** The wrong replies this can send, named so a test can say what it got. */
|
|
20
|
+
export declare const MISBEHAVING_KINDS: readonly ["done", "truncated", "unknown", "repeat", "empty", "cut-off", "no-cost"];
|
|
21
|
+
export type MisbehavingKind = (typeof MISBEHAVING_KINDS)[number];
|
|
22
|
+
export interface MisbehavingTurn {
|
|
23
|
+
turn: MisbehavingKind;
|
|
24
|
+
script: ScriptedTurn;
|
|
25
|
+
}
|
|
26
|
+
export interface MisbehavingScriptOptions {
|
|
27
|
+
/** How many turns to draw. A run with a step limit needs no more than that. */
|
|
28
|
+
turns?: number;
|
|
29
|
+
}
|
|
30
|
+
export declare function misbehavingScript(seed: number, options?: MisbehavingScriptOptions): MisbehavingTurn[];
|
|
31
|
+
export interface MisbehavingModel extends ModelSlot {
|
|
32
|
+
readonly seed: number;
|
|
33
|
+
/** What each turn was drawn as, in order. */
|
|
34
|
+
readonly turns: readonly MisbehavingKind[];
|
|
35
|
+
}
|
|
36
|
+
export declare function misbehavingModel(seed: number, options: {
|
|
37
|
+
clock: Clock;
|
|
38
|
+
turns?: number;
|
|
39
|
+
}): MisbehavingModel;
|
|
40
|
+
//# sourceMappingURL=misbehaving-model.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"misbehaving-model.d.ts","sourceRoot":"","sources":["../../src/testkit/misbehaving-model.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAiB,KAAK,YAAY,EAAE,MAAM,YAAY,CAAC;AAE9D,4EAA4E;AAC5E,eAAO,MAAM,iBAAiB,YAC7B,MAAM,EACN,WAAW,EACX,SAAS,EACT,QAAQ,EACR,OAAO,EACP,SAAS,EACT,SAAS,CACA,CAAC;AAEX,MAAM,MAAM,eAAe,GAAG,CAAC,OAAO,iBAAiB,CAAC,CAAC,MAAM,CAAC,CAAC;AAEjE,MAAM,WAAW,eAAe;IAC/B,IAAI,EAAE,eAAe,CAAC;IACtB,MAAM,EAAE,YAAY,CAAC;CACrB;AAED,MAAM,WAAW,wBAAwB;IACxC,+EAA+E;IAC/E,KAAK,CAAC,EAAE,MAAM,CAAC;CACf;AAoCD,wBAAgB,iBAAiB,CAChC,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,wBAA6B,GACpC,eAAe,EAAE,CAkEnB;AAED,MAAM,WAAW,gBAAiB,SAAQ,SAAS;IAClD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,6CAA6C;IAC7C,QAAQ,CAAC,KAAK,EAAE,SAAS,eAAe,EAAE,CAAC;CAC3C;AAED,wBAAgB,gBAAgB,CAC/B,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE;IAAE,KAAK,EAAE,KAAK,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,GACvC,gBAAgB,CAclB"}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A model that is wrong on purpose, from a seed (docs/engineering-practices.md,
|
|
3
|
+
* KIT12 and KIT13; decision 64).
|
|
4
|
+
*
|
|
5
|
+
* The scripted model says what a well-behaved provider does. This one says what
|
|
6
|
+
* a real one sometimes does instead: half-written arguments, a tool nobody has,
|
|
7
|
+
* the same call again, an empty answer, a reply cut off by the token limit, and
|
|
8
|
+
* usage with no cost. The run in front of it must still end exactly once with a
|
|
9
|
+
* record that loads, whatever it is given, and the seed is what makes a failure
|
|
10
|
+
* repeatable.
|
|
11
|
+
*
|
|
12
|
+
* One script is drawn per seed, so the same seed produces the same run. The
|
|
13
|
+
* generator assumes the run has a `finish` tool, because that is how a run ends
|
|
14
|
+
* well; a run without one parks, which is still a correct ending.
|
|
15
|
+
*/
|
|
16
|
+
import { scriptedModel } from "./model.js";
|
|
17
|
+
/** The wrong replies this can send, named so a test can say what it got. */
|
|
18
|
+
export const MISBEHAVING_KINDS = [
|
|
19
|
+
"done",
|
|
20
|
+
"truncated",
|
|
21
|
+
"unknown",
|
|
22
|
+
"repeat",
|
|
23
|
+
"empty",
|
|
24
|
+
"cut-off",
|
|
25
|
+
"no-cost",
|
|
26
|
+
];
|
|
27
|
+
/** A small deterministic generator: the same seed gives the same numbers. */
|
|
28
|
+
function draw(seed, index) {
|
|
29
|
+
let x = (seed * 2_654_435_761 + index * 40_503 + 1) >>> 0;
|
|
30
|
+
x ^= x << 13;
|
|
31
|
+
x >>>= 0;
|
|
32
|
+
x ^= x >>> 17;
|
|
33
|
+
x ^= x << 5;
|
|
34
|
+
x >>>= 0;
|
|
35
|
+
return x / 0x1_0000_0000;
|
|
36
|
+
}
|
|
37
|
+
/** How often each wrong reply comes up, out of 100. */
|
|
38
|
+
const WEIGHTS = [
|
|
39
|
+
["done", 22],
|
|
40
|
+
["truncated", 16],
|
|
41
|
+
["unknown", 14],
|
|
42
|
+
["repeat", 10],
|
|
43
|
+
["empty", 10],
|
|
44
|
+
["cut-off", 12],
|
|
45
|
+
["no-cost", 16],
|
|
46
|
+
];
|
|
47
|
+
function kindFor(seed, index) {
|
|
48
|
+
const roll = draw(seed, index) * 100;
|
|
49
|
+
let seen = 0;
|
|
50
|
+
for (const [kind, weight] of WEIGHTS) {
|
|
51
|
+
seen += weight;
|
|
52
|
+
if (roll < seen) {
|
|
53
|
+
return kind;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return "done";
|
|
57
|
+
}
|
|
58
|
+
export function misbehavingScript(seed, options = {}) {
|
|
59
|
+
const count = options.turns ?? 16;
|
|
60
|
+
const turns = [];
|
|
61
|
+
for (let index = 0; index < count; index += 1) {
|
|
62
|
+
const kind = kindFor(seed, index);
|
|
63
|
+
const id = `call_${seed}_${index}`;
|
|
64
|
+
const previous = turns.at(-1);
|
|
65
|
+
switch (kind) {
|
|
66
|
+
case "done":
|
|
67
|
+
turns.push({
|
|
68
|
+
turn: kind,
|
|
69
|
+
script: { toolCalls: [{ id, name: "finish", args: {} }] },
|
|
70
|
+
});
|
|
71
|
+
break;
|
|
72
|
+
case "truncated":
|
|
73
|
+
// Half-written JSON, which is what a reply cut off mid-token looks like.
|
|
74
|
+
turns.push({
|
|
75
|
+
turn: kind,
|
|
76
|
+
script: { toolCalls: [{ id, name: "finish", args: '{"done":' }] },
|
|
77
|
+
});
|
|
78
|
+
break;
|
|
79
|
+
case "unknown":
|
|
80
|
+
turns.push({
|
|
81
|
+
turn: kind,
|
|
82
|
+
script: { toolCalls: [{ id, name: "not_a_tool", args: {} }] },
|
|
83
|
+
});
|
|
84
|
+
break;
|
|
85
|
+
case "repeat": {
|
|
86
|
+
// The same call again, which the stuck safeguard is there to notice.
|
|
87
|
+
const repeated = previous?.script.toolCalls?.[0];
|
|
88
|
+
turns.push({
|
|
89
|
+
turn: kind,
|
|
90
|
+
script: {
|
|
91
|
+
toolCalls: [
|
|
92
|
+
repeated === undefined
|
|
93
|
+
? { id, name: "not_a_tool", args: {} }
|
|
94
|
+
: { id, name: repeated.name, args: repeated.args },
|
|
95
|
+
],
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
case "empty":
|
|
101
|
+
turns.push({ turn: kind, script: { text: "" } });
|
|
102
|
+
break;
|
|
103
|
+
case "cut-off":
|
|
104
|
+
turns.push({
|
|
105
|
+
turn: kind,
|
|
106
|
+
script: {
|
|
107
|
+
toolCalls: [{ id, name: "finish", args: {} }],
|
|
108
|
+
finishReason: "length",
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
break;
|
|
112
|
+
case "no-cost":
|
|
113
|
+
turns.push({
|
|
114
|
+
turn: kind,
|
|
115
|
+
script: {
|
|
116
|
+
text: "I am not sure what to do next",
|
|
117
|
+
usage: { inputTokens: 40, outputTokens: 6 },
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return turns;
|
|
124
|
+
}
|
|
125
|
+
export function misbehavingModel(seed, options) {
|
|
126
|
+
const script = misbehavingScript(seed, options.turns === undefined ? {} : { turns: options.turns });
|
|
127
|
+
const model = scriptedModel(script.map((entry) => entry.script), { clock: options.clock, model: `misbehaving/seed-${seed}` });
|
|
128
|
+
return {
|
|
129
|
+
seed,
|
|
130
|
+
turns: script.map((entry) => entry.turn),
|
|
131
|
+
complete: (request) => model.complete(request),
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
//# sourceMappingURL=misbehaving-model.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"misbehaving-model.js","sourceRoot":"","sources":["../../src/testkit/misbehaving-model.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAIH,OAAO,EAAE,aAAa,EAAqB,MAAM,YAAY,CAAC;AAE9D,4EAA4E;AAC5E,MAAM,CAAC,MAAM,iBAAiB,GAAG;IAChC,MAAM;IACN,WAAW;IACX,SAAS;IACT,QAAQ;IACR,OAAO;IACP,SAAS;IACT,SAAS;CACA,CAAC;AAcX,6EAA6E;AAC7E,SAAS,IAAI,CAAC,IAAY,EAAE,KAAa;IACxC,IAAI,CAAC,GAAG,CAAC,IAAI,GAAG,aAAa,GAAG,KAAK,GAAG,MAAM,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;IAC1D,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;IACb,CAAC,MAAM,CAAC,CAAC;IACT,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;IACd,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACZ,CAAC,MAAM,CAAC,CAAC;IACT,OAAO,CAAC,GAAG,aAAa,CAAC;AAC1B,CAAC;AAED,uDAAuD;AACvD,MAAM,OAAO,GAAyC;IACrD,CAAC,MAAM,EAAE,EAAE,CAAC;IACZ,CAAC,WAAW,EAAE,EAAE,CAAC;IACjB,CAA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
45
45
|
/** The name a reply reports. Defaults to `scripted/model`. */
|
|
46
46
|
model?: string;
|
|
47
|
+
/**
|
|
48
|
+
* The answers to compaction's summary calls, in order. A summary request is
|
|
49
|
+
* told apart by its `purpose`, never by its position, because it arrives in the
|
|
50
|
+
* middle of the run it is summarizing.
|
|
51
|
+
*/
|
|
52
|
+
summaries?: readonly ScriptedTurn[];
|
|
53
|
+
/**
|
|
54
|
+
* How a run request's turn is chosen.
|
|
55
|
+
*
|
|
56
|
+
* `"conversation"`, the default, counts the assistant messages already in the
|
|
57
|
+
* request, which is what makes a request asked again after a kill get the same
|
|
58
|
+
* answer (KIT03). `"sequence"` counts the run requests answered so far, which is
|
|
59
|
+
* what a test wants once a compaction folds older turns into a summary: the
|
|
60
|
+
* conversation's length then stops counting the turns that were taken.
|
|
61
|
+
*/
|
|
62
|
+
position?: "conversation" | "sequence";
|
|
63
|
+
/** Where a `"sequence"` script starts, so a resumed run continues its script. */
|
|
64
|
+
startTurn?: number;
|
|
47
65
|
}
|
|
48
66
|
export interface ScriptedModel extends ModelSlot {
|
|
49
67
|
/** Every request received, in order. */
|
|
50
68
|
requests: ModelRequest[];
|
|
51
69
|
}
|
|
70
|
+
/** An argument value as the wire would carry it: an object serialized, a string as it is. */
|
|
71
|
+
export declare function scriptedArguments(args: unknown): string;
|
|
52
72
|
export declare function scriptedModel(turns: readonly ScriptedTurn[], options: ScriptedModelOptions): ScriptedModel;
|
|
53
73
|
//# sourceMappingURL=model.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../../src/testkit/model.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,KAAK,EAAE,YAAY,EAAc,YAAY,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACjG,OAAO,KAAK,EAAE,SAAS,EAAU,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AAW,eAAe;IAC/B,IAAI,CAAC,EAAE,SAAS,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,YAAY;IAC5B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,gBAAgB,EAAE,CAAC;IAC/B,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,YAAY,CAAC,EAAE,YAAY,CAAC;IAC5B,yEAAyE;IACzE,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,qDAAqD;IACrD,QAAQ,CAAC,EAAE,eAAe,EAAE,CAAC;CAC7B;AAED,MAAM,WAAW,oBAAoB;IACpC,KAAK,EAAE,KAAK,CAAC;IACb,8DAA8D;IAC9D,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;;OAIG;IACH,SAAS,CAAC,EAAE,SAAS,YAAY,EAAE,CAAC;IACpC;;;;;;;;OAQG;IACH,QAAQ,CAAC,EAAE,cAAc,GAAG,UAAU,CAAC;IACvC,iFAAiF;IACjF,SAAS,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,aAAc,SAAQ,SAAS;IAC/C,wCAAwC;IACxC,QAAQ,EAAE,YAAY,EAAE,CAAC;CACzB;AAED,6FAA6F;AAC7F,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,CAKvD;AAqBD,wBAAgB,aAAa,CAC5B,KAAK,EAAE,SAAS,YAAY,EAAE,EAC9B,OAAO,EAAE,oBAAoB,GAC3B,aAAa,CAkGf"}
|
package/dist/testkit/model.js
CHANGED
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
* exercised without a network. A script with no turn left is a test bug, so it
|
|
12
12
|
* answers with a loud non-retryable error rather than looping.
|
|
13
13
|
*/
|
|
14
|
-
|
|
14
|
+
/** An argument value as the wire would carry it: an object serialized, a string as it is. */
|
|
15
|
+
export function scriptedArguments(args) {
|
|
15
16
|
if (args === undefined) {
|
|
16
17
|
return "{}";
|
|
17
18
|
}
|
|
@@ -21,7 +22,7 @@ function replyFor(turn, model) {
|
|
|
21
22
|
const toolCalls = (turn.toolCalls ?? []).map((call) => ({
|
|
22
23
|
id: call.id,
|
|
23
24
|
name: call.name,
|
|
24
|
-
arguments:
|
|
25
|
+
arguments: scriptedArguments(call.args),
|
|
25
26
|
}));
|
|
26
27
|
const reply = {
|
|
27
28
|
text: turn.text ?? "",
|
|
@@ -40,11 +41,56 @@ export function scriptedModel(turns, options) {
|
|
|
40
41
|
const requests = [];
|
|
41
42
|
/** How many of a turn's scripted failures have been spent. */
|
|
42
43
|
const spent = new Map();
|
|
44
|
+
/** How many run requests have been answered, for a `"sequence"` script. */
|
|
45
|
+
let servedRuns = options.startTurn ?? 0;
|
|
46
|
+
/** How many summaries have been answered, which is which script turn they get. */
|
|
47
|
+
let summariesServed = 0;
|
|
48
|
+
/** How many of a summary turn's scripted failures have been spent. */
|
|
49
|
+
const summaryFailures = new Map();
|
|
43
50
|
return {
|
|
44
51
|
requests,
|
|
45
52
|
async complete(request) {
|
|
46
53
|
requests.push(request);
|
|
47
|
-
|
|
54
|
+
if (request.purpose === "compaction") {
|
|
55
|
+
const position = summariesServed;
|
|
56
|
+
const summaries = options.summaries ?? [];
|
|
57
|
+
const turn = summaries[position];
|
|
58
|
+
if (turn === undefined) {
|
|
59
|
+
return {
|
|
60
|
+
ok: false,
|
|
61
|
+
error: {
|
|
62
|
+
code: "internal",
|
|
63
|
+
message: `the scripted model has no summary ${position + 1}: the script has ${summaries.length}`,
|
|
64
|
+
retryable: false,
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
const usedFailure = summaryFailures.get(position) ?? 0;
|
|
69
|
+
const failure = turn.failures?.[usedFailure];
|
|
70
|
+
if (failure !== undefined) {
|
|
71
|
+
summaryFailures.set(position, usedFailure + 1);
|
|
72
|
+
const error = {
|
|
73
|
+
code: failure.code ?? "provider_error",
|
|
74
|
+
message: failure.message ??
|
|
75
|
+
`scripted summary failure ${usedFailure + 1} of ${position + 1}`,
|
|
76
|
+
retryable: failure.retryable ?? true,
|
|
77
|
+
};
|
|
78
|
+
if (failure.retryAfterMs !== undefined) {
|
|
79
|
+
error.retryAfterMs = failure.retryAfterMs;
|
|
80
|
+
}
|
|
81
|
+
return { ok: false, error };
|
|
82
|
+
}
|
|
83
|
+
if (turn.delayMs !== undefined && turn.delayMs > 0) {
|
|
84
|
+
await options.clock.sleep(turn.delayMs, request.signal);
|
|
85
|
+
}
|
|
86
|
+
summariesServed += 1;
|
|
87
|
+
// A summary request carries no tools, so a scripted call on one would be
|
|
88
|
+
// an answer the model could not have given.
|
|
89
|
+
return { ok: true, value: replyFor({ ...turn, toolCalls: [] }, model) };
|
|
90
|
+
}
|
|
91
|
+
const position = options.position === "sequence"
|
|
92
|
+
? servedRuns
|
|
93
|
+
: request.messages.filter((message) => message.role === "assistant").length;
|
|
48
94
|
const turn = turns[position];
|
|
49
95
|
if (turn === undefined) {
|
|
50
96
|
return {
|
|
@@ -73,6 +119,7 @@ export function scriptedModel(turns, options) {
|
|
|
73
119
|
if (turn.delayMs !== undefined && turn.delayMs > 0) {
|
|
74
120
|
await options.clock.sleep(turn.delayMs, request.signal);
|
|
75
121
|
}
|
|
122
|
+
servedRuns += 1;
|
|
76
123
|
return { ok: true, value: replyFor(turn, model) };
|
|
77
124
|
},
|
|
78
125
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"model.js","sourceRoot":"","sources":["../../src/testkit/model.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;
|
|
1
|
+
{"version":3,"file":"model.js","sourceRoot":"","sources":["../../src/testkit/model.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAiEH,6FAA6F;AAC7F,MAAM,UAAU,iBAAiB,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,MAAM,IAAI,GAAG,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;YACtC,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC,IAAI,CAAC,CAAC;YACtC,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;gBAC3B,KAAK,CAAC,GAAG,CAAC,QAAQ,EAAE,IAAI,GAAG,CAAC,CAAC,CAAC;gBAC9B,MAAM,KAAK,GAAe;oBACzB,IAAI,EAAE,OAAO,CAAC,IAAI,IAAI,gBAAgB;oBACtC,OAAO,EACN,OAAO,CAAC,OAAO,IAAI,oBAAoB,IAAI,GAAG,CAAC,YAAY,QAAQ,GAAG,CAAC,EAAE;oBAC1E,SAAS,EAAE,OAAO,CAAC,SAAS,IAAI,IAAI;iBACpC,CAAC;gBACF,IAAI,OAAO,CAAC,YAAY,KAAK,SAAS,EAAE,CAAC;oBACxC,KAAK,CAAC,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;gBAC3C,CAAC;gBACD,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;YAC7B,CAAC;YAED,IAAI,IAAI,CAAC,OAAO,KAAK,SAAS,IAAI,IAAI,CAAC,OAAO,GAAG,CAAC,EAAE,CAAC;gBACpD,MAAM,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;YACzD,CAAC;YAED,UAAU,IAAI,CAAC,CAAC;YAChB,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC,EAAE,CAAC;QACnD,CAAC;KACD,CAAC;AACH,CAAC"}
|
|
@@ -11,22 +11,35 @@
|
|
|
11
11
|
* decoding is hand-written like everything else at a boundary, and a bad file is
|
|
12
12
|
* refused with a message naming the field.
|
|
13
13
|
*/
|
|
14
|
+
import type { FakeClock } from "./clock.js";
|
|
15
|
+
import type { ModelSlot } from "../core/model.js";
|
|
14
16
|
import type { RunEvent } from "../core/events.js";
|
|
15
17
|
import { type JsonValue } from "../core/json.js";
|
|
18
|
+
import type { CompactionSettings } from "../core/compaction.js";
|
|
19
|
+
import { type ToolPolicy } from "../core/approval.js";
|
|
16
20
|
import { type Result } from "../core/result.js";
|
|
17
21
|
import { type RunResult } from "../core/run.js";
|
|
22
|
+
import type { Tool } from "../core/tool.js";
|
|
18
23
|
import { type LoadedRecord } from "../record/load.js";
|
|
19
|
-
import {
|
|
24
|
+
import { fakeClock } from "./clock.js";
|
|
25
|
+
import { type ScriptedModelOptions, type ScriptedTurn } from "./model.js";
|
|
20
26
|
export type ScenarioToolBehavior = "ok" | "error" | "throw" | "slow";
|
|
21
27
|
export interface ScenarioTool {
|
|
22
28
|
name: string;
|
|
23
29
|
behavior: ScenarioToolBehavior;
|
|
30
|
+
/** Allow, ask or deny, which is what an approval needs to exercise (decision 35). */
|
|
31
|
+
policy?: ToolPolicy;
|
|
24
32
|
content?: string;
|
|
25
33
|
/** For a `slow` tool: how long it takes on the fake clock. */
|
|
26
34
|
ms?: number;
|
|
27
35
|
}
|
|
28
36
|
/** An expectation is matched as a subset, so extra fields on the actual value are fine. */
|
|
29
37
|
export type Expectation = Record<string, unknown>;
|
|
38
|
+
/** A scenario that compacts: the recipe's settings, and the answers to its summary. */
|
|
39
|
+
export interface ScenarioCompaction {
|
|
40
|
+
settings: CompactionSettings;
|
|
41
|
+
summaries: ScriptedTurn[];
|
|
42
|
+
}
|
|
30
43
|
export interface Scenario {
|
|
31
44
|
name: string;
|
|
32
45
|
task: string;
|
|
@@ -41,6 +54,8 @@ export interface Scenario {
|
|
|
41
54
|
expectRecord: Expectation[];
|
|
42
55
|
/** Stop the run when the nth event arrives, for the stop scenarios. */
|
|
43
56
|
abortAfter?: number;
|
|
57
|
+
/** How the run compacts, and how its summaries are answered. */
|
|
58
|
+
compaction?: ScenarioCompaction;
|
|
44
59
|
}
|
|
45
60
|
export interface ScenarioOutcome {
|
|
46
61
|
result: RunResult;
|
|
@@ -51,5 +66,24 @@ export interface ScenarioOutcome {
|
|
|
51
66
|
problems: string[];
|
|
52
67
|
}
|
|
53
68
|
export declare function decodeScenario(value: unknown): Result<Scenario>;
|
|
54
|
-
export declare function
|
|
69
|
+
export declare function makeScenarioTool(tool: ScenarioTool, clock: ReturnType<typeof fakeClock>): Tool;
|
|
70
|
+
/**
|
|
71
|
+
* Events are matched as a subsequence, so an unrelated new event does not break a
|
|
72
|
+
* scenario. Exported because the workerd runner matches protocol events with it,
|
|
73
|
+
* and one matcher is what keeps the two levels honest.
|
|
74
|
+
*/
|
|
75
|
+
export declare function matchScenarioEvents(expected: Expectation[], events: readonly {
|
|
76
|
+
type: string;
|
|
77
|
+
}[], problems: string[]): void;
|
|
78
|
+
/** Record entries are matched in order, each as a subset of the next entry that fits. */
|
|
79
|
+
export declare function matchScenarioRecord(expected: Expectation[], record: LoadedRecord, problems: string[]): void;
|
|
80
|
+
export interface ScenarioRunOptions {
|
|
81
|
+
/**
|
|
82
|
+
* How a run gets its model. The default is the in-process scripted model; a test
|
|
83
|
+
* that wants a real adapter and a fake server passes one here, so a scenario runs
|
|
84
|
+
* through the wire as well (SCN04).
|
|
85
|
+
*/
|
|
86
|
+
makeModel?: (turns: readonly ScriptedTurn[], clock: FakeClock, options: ScriptedModelOptions) => ModelSlot;
|
|
87
|
+
}
|
|
88
|
+
export declare function runScenario(scenario: Scenario, options?: ScenarioRunOptions): Promise<ScenarioOutcome>;
|
|
55
89
|
//# sourceMappingURL=scenario.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scenario.d.ts","sourceRoot":"","sources":["../../src/testkit/scenario.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,EAA8B,KAAK,SAAS,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
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pB,QAAQ,EAAE,MAAM,EAAE,GAChB,IAAI,CAmCN;AAKD,MAAM,WAAW,kBAAkB;IAClC;;;;OAIG;IACH,SAAS,CAAC,EAAE,CACX,KAAK,EAAE,SAAS,YAAY,EAAE,EAC9B,KAAK,EAAE,SAAS,EAChB,OAAO,EAAE,oBAAoB,KACzB,SAAS,CAAC;CACf;AAoBD,wBAAsB,WAAW,CAChC,QAAQ,EAAE,QAAQ,EAClB,OAAO,GAAE,kBAAuB,GAC9B,OAAO,CAAC,eAAe,CAAC,CA2F1B"}
|
package/dist/testkit/scenario.js
CHANGED
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
* refused with a message naming the field.
|
|
13
13
|
*/
|
|
14
14
|
import { isJsonValue, isPlainObject } from "../core/json.js";
|
|
15
|
+
import { TOOL_POLICIES, isToolPolicy } from "../core/approval.js";
|
|
15
16
|
import { isFinishReason } from "../core/decode-reply.js";
|
|
16
17
|
import { fail, isErrorCode, ok } from "../core/result.js";
|
|
17
18
|
import { runTask } from "../core/run.js";
|
|
@@ -166,6 +167,49 @@ function decodeTurn(value, where) {
|
|
|
166
167
|
}
|
|
167
168
|
return ok(turn);
|
|
168
169
|
}
|
|
170
|
+
/** The scenario words for the settings, and the field each one sets. */
|
|
171
|
+
const COMPACTION_FIELDS = [
|
|
172
|
+
["context_window", "contextWindow"],
|
|
173
|
+
["reserve_tokens", "reserveTokens"],
|
|
174
|
+
["keep_tokens", "keepTokens"],
|
|
175
|
+
];
|
|
176
|
+
function decodeCompaction(value) {
|
|
177
|
+
if (!isPlainObject(value)) {
|
|
178
|
+
return bad("scenario.compaction must be an object");
|
|
179
|
+
}
|
|
180
|
+
const settings = {};
|
|
181
|
+
for (const [word, field] of COMPACTION_FIELDS) {
|
|
182
|
+
const raw = value[word];
|
|
183
|
+
if (raw === undefined) {
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
if (typeof raw !== "number" || !Number.isSafeInteger(raw) || raw < 0) {
|
|
187
|
+
return bad(`scenario.compaction.${word} must be a whole number of tokens`);
|
|
188
|
+
}
|
|
189
|
+
if (field === "contextWindow") {
|
|
190
|
+
settings.contextWindow = raw;
|
|
191
|
+
}
|
|
192
|
+
else if (field === "reserveTokens") {
|
|
193
|
+
settings.reserveTokens = raw;
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
settings.keepTokens = raw;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
const rawSummaries = value["summaries"];
|
|
200
|
+
if (!Array.isArray(rawSummaries) || rawSummaries.length === 0) {
|
|
201
|
+
return bad("scenario.compaction.summaries must be a non-empty list of turns");
|
|
202
|
+
}
|
|
203
|
+
const summaries = [];
|
|
204
|
+
for (const [index, raw] of rawSummaries.entries()) {
|
|
205
|
+
const turn = decodeTurn(raw, `scenario.compaction.summaries[${index}]`);
|
|
206
|
+
if (!turn.ok) {
|
|
207
|
+
return turn;
|
|
208
|
+
}
|
|
209
|
+
summaries.push(turn.value);
|
|
210
|
+
}
|
|
211
|
+
return ok({ settings, summaries });
|
|
212
|
+
}
|
|
169
213
|
function decodeExpectations(value, where) {
|
|
170
214
|
if (value === undefined) {
|
|
171
215
|
return ok([]);
|
|
@@ -235,6 +279,13 @@ export function decodeScenario(value) {
|
|
|
235
279
|
return bad(`${where}.behavior must be one of ${BEHAVIORS.join(", ")}`);
|
|
236
280
|
}
|
|
237
281
|
const tool = { name: toolName.value, behavior };
|
|
282
|
+
const policy = raw["policy"];
|
|
283
|
+
if (policy !== undefined) {
|
|
284
|
+
if (!isToolPolicy(policy)) {
|
|
285
|
+
return bad(`${where}.policy must be one of ${TOOL_POLICIES.join(", ")}`);
|
|
286
|
+
}
|
|
287
|
+
tool.policy = policy;
|
|
288
|
+
}
|
|
238
289
|
const content = optionalString(raw, "content", where);
|
|
239
290
|
if (!content.ok) {
|
|
240
291
|
return content;
|
|
@@ -285,6 +336,14 @@ export function decodeScenario(value) {
|
|
|
285
336
|
if (recipe.value !== undefined) {
|
|
286
337
|
scenario.recipe = recipe.value;
|
|
287
338
|
}
|
|
339
|
+
const rawCompaction = value["compaction"];
|
|
340
|
+
if (rawCompaction !== undefined) {
|
|
341
|
+
const compaction = decodeCompaction(rawCompaction);
|
|
342
|
+
if (!compaction.ok) {
|
|
343
|
+
return compaction;
|
|
344
|
+
}
|
|
345
|
+
scenario.compaction = compaction.value;
|
|
346
|
+
}
|
|
288
347
|
const abortAfter = value["abort_after"];
|
|
289
348
|
if (abortAfter !== undefined) {
|
|
290
349
|
if (typeof abortAfter !== "number" || !Number.isInteger(abortAfter) || abortAfter < 1) {
|
|
@@ -294,16 +353,17 @@ export function decodeScenario(value) {
|
|
|
294
353
|
}
|
|
295
354
|
return ok(scenario);
|
|
296
355
|
}
|
|
297
|
-
function
|
|
356
|
+
export function makeScenarioTool(tool, clock) {
|
|
357
|
+
const options = tool.policy === undefined ? {} : { policy: tool.policy };
|
|
298
358
|
switch (tool.behavior) {
|
|
299
359
|
case "error":
|
|
300
|
-
return toolReturningError(tool.name, tool.content ?? `${tool.name} says no
|
|
360
|
+
return toolReturningError(tool.name, tool.content ?? `${tool.name} says no`, options);
|
|
301
361
|
case "throw":
|
|
302
|
-
return toolThatThrows(tool.name, tool.content ?? `${tool.name} broke
|
|
362
|
+
return toolThatThrows(tool.name, tool.content ?? `${tool.name} broke`, options);
|
|
303
363
|
case "slow":
|
|
304
|
-
return toolTakingTime(tool.name, tool.ms ?? 100, clock);
|
|
364
|
+
return toolTakingTime(tool.name, tool.ms ?? 100, clock, options);
|
|
305
365
|
case "ok":
|
|
306
|
-
return toolReturning(tool.name, tool.content ?? `${tool.name} says yes
|
|
366
|
+
return toolReturning(tool.name, tool.content ?? `${tool.name} says yes`, options);
|
|
307
367
|
}
|
|
308
368
|
}
|
|
309
369
|
/** Compare an expectation with an actual value as a subset, collecting what differs. */
|
|
@@ -350,8 +410,12 @@ function closeness(expected, actual) {
|
|
|
350
410
|
function describeEvent(event) {
|
|
351
411
|
return event === undefined ? "none" : JSON.stringify(event).slice(0, 200);
|
|
352
412
|
}
|
|
353
|
-
/**
|
|
354
|
-
|
|
413
|
+
/**
|
|
414
|
+
* Events are matched as a subsequence, so an unrelated new event does not break a
|
|
415
|
+
* scenario. Exported because the workerd runner matches protocol events with it,
|
|
416
|
+
* and one matcher is what keeps the two levels honest.
|
|
417
|
+
*/
|
|
418
|
+
export function matchScenarioEvents(expected, events, problems) {
|
|
355
419
|
let cursor = 0;
|
|
356
420
|
for (const expectation of expected) {
|
|
357
421
|
let found = -1;
|
|
@@ -389,7 +453,7 @@ function matchEvents(expected, events, problems) {
|
|
|
389
453
|
}
|
|
390
454
|
}
|
|
391
455
|
/** Record entries are matched in order, each as a subset of the next entry that fits. */
|
|
392
|
-
function
|
|
456
|
+
export function matchScenarioRecord(expected, record, problems) {
|
|
393
457
|
let cursor = 0;
|
|
394
458
|
for (const expectation of expected) {
|
|
395
459
|
let found = -1;
|
|
@@ -426,14 +490,31 @@ function matchRecord(expected, record, problems) {
|
|
|
426
490
|
}
|
|
427
491
|
/** The testkit recipes a scenario may name, by name. */
|
|
428
492
|
const RECIPES = new Map([[timelineTestRecipe.name, timelineTestRecipe]]);
|
|
429
|
-
|
|
493
|
+
/**
|
|
494
|
+
* The model a scenario runs against. A scenario that compacts is scripted by
|
|
495
|
+
* request order, because a compaction folds older turns into a summary and the
|
|
496
|
+
* conversation's length stops counting the turns that were taken.
|
|
497
|
+
*/
|
|
498
|
+
function modelFor(scenario, clock, options) {
|
|
499
|
+
const compaction = scenario.compaction;
|
|
500
|
+
const settings = { clock };
|
|
501
|
+
if (compaction !== undefined) {
|
|
502
|
+
settings.summaries = compaction.summaries;
|
|
503
|
+
settings.position = "sequence";
|
|
504
|
+
}
|
|
505
|
+
if (options.makeModel === undefined) {
|
|
506
|
+
return scriptedModel(scenario.model, settings);
|
|
507
|
+
}
|
|
508
|
+
return options.makeModel(scenario.model, clock, settings);
|
|
509
|
+
}
|
|
510
|
+
export async function runScenario(scenario, options = {}) {
|
|
430
511
|
const clock = fakeClock();
|
|
431
512
|
const storage = memoryStorage();
|
|
432
513
|
const events = [];
|
|
433
514
|
const controller = new AbortController();
|
|
434
515
|
const recipe = scenario.recipe === undefined ? undefined : RECIPES.get(scenario.recipe);
|
|
435
516
|
const tools = recipe === undefined
|
|
436
|
-
? [...scenario.tools.map((tool) =>
|
|
517
|
+
? [...scenario.tools.map((tool) => makeScenarioTool(tool, clock)), createFinishTool()]
|
|
437
518
|
: [...recipe.tools];
|
|
438
519
|
// A recipe brings its own first message, state and instructions; a plain scenario
|
|
439
520
|
// still sends its input text straight in.
|
|
@@ -451,10 +532,14 @@ export async function runScenario(scenario) {
|
|
|
451
532
|
...recipeParts,
|
|
452
533
|
system: recipe?.instructions ?? scenario.system,
|
|
453
534
|
tools,
|
|
454
|
-
model:
|
|
535
|
+
model: modelFor(scenario, clock, options),
|
|
455
536
|
storage,
|
|
456
537
|
clock,
|
|
457
538
|
ids: counterIds(),
|
|
539
|
+
// A scenario runs as a host where a person is reachable, so an `ask` policy
|
|
540
|
+
// parks the run rather than being denied (decision 35).
|
|
541
|
+
canAsk: true,
|
|
542
|
+
...(scenario.compaction === undefined ? {} : { compaction: scenario.compaction.settings }),
|
|
458
543
|
toolDeadlineMs: 60_000,
|
|
459
544
|
signal: controller.signal,
|
|
460
545
|
onEvent: (event) => {
|
|
@@ -493,8 +578,8 @@ export async function runScenario(scenario) {
|
|
|
493
578
|
for (const note of loaded.value.notes) {
|
|
494
579
|
problems.push(`the record needed a note at line ${note.line}: ${note.problem}`);
|
|
495
580
|
}
|
|
496
|
-
|
|
497
|
-
|
|
581
|
+
matchScenarioEvents(scenario.expectEvents, events, problems);
|
|
582
|
+
matchScenarioRecord(scenario.expectRecord, loaded.value, problems);
|
|
498
583
|
// A scenario that expected a reason and got another is already caught above, but
|
|
499
584
|
// the run's own ending is worth stating when it is unexpected.
|
|
500
585
|
if (problems.length > 0) {
|