@ftrdotdev/pi-delegates 0.0.0-stage → 0.2.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/CHANGELOG.md +128 -0
- package/LICENSE +21 -0
- package/OPERATIONS.md +130 -0
- package/README.md +456 -2
- package/ROADMAP.md +179 -0
- package/TESTING.md +896 -0
- package/agents.ts +8 -0
- package/examples/README.md +114 -0
- package/examples/gated.ts +28 -0
- package/index.ts +9 -0
- package/package.json +94 -4
- package/skills/pi-delegates/SKILL.md +176 -0
- package/skills/pi-delegates/references/control.md +159 -0
- package/skills/pi-delegates/references/definitions.md +163 -0
- package/skills/pi-delegates/references/gates.md +175 -0
- package/skills/pi-delegates/references/launch.md +184 -0
- package/src/docs-contract.test.ts +106 -0
- package/src/domain/definitions.test.ts +41 -0
- package/src/domain/definitions.ts +163 -0
- package/src/domain/failure.ts +33 -0
- package/src/domain/ids.ts +13 -0
- package/src/domain/model-source.ts +30 -0
- package/src/domain/names.ts +35 -0
- package/src/domain/orchestration/admission.ts +78 -0
- package/src/domain/orchestration/control.test.ts +1332 -0
- package/src/domain/orchestration/control.ts +457 -0
- package/src/domain/orchestration/fanout.test.ts +524 -0
- package/src/domain/orchestration/fanout.ts +372 -0
- package/src/domain/orchestration/gates.test.ts +685 -0
- package/src/domain/orchestration/gates.ts +360 -0
- package/src/domain/orchestration/launch.test.ts +372 -0
- package/src/domain/orchestration/launch.ts +691 -0
- package/src/domain/orchestration/requests.test.ts +695 -0
- package/src/domain/orchestration/requests.ts +782 -0
- package/src/domain/orchestration/review.test.ts +292 -0
- package/src/domain/orchestration/review.ts +168 -0
- package/src/domain/orchestration/run-wait.test.ts +265 -0
- package/src/domain/orchestration/run-wait.ts +256 -0
- package/src/domain/orchestration/workflow-section.test.ts +62 -0
- package/src/domain/orchestration/workflow-section.ts +99 -0
- package/src/domain/orchestration/workflow.test.ts +1566 -0
- package/src/domain/orchestration/workflow.ts +778 -0
- package/src/domain/run-records.test.ts +27 -0
- package/src/domain/run-records.ts +689 -0
- package/src/domain/settlement.test.ts +186 -0
- package/src/domain/settlement.ts +137 -0
- package/src/domain/shapes.test.ts +185 -0
- package/src/domain/skills.test.ts +477 -0
- package/src/domain/skills.ts +114 -0
- package/src/domain/tool-schema.test.ts +46 -0
- package/src/domain/tool-schema.ts +20 -0
- package/src/domain/toolkit.test.ts +215 -0
- package/src/domain/toolkit.ts +374 -0
- package/src/domain/turn-budget.test.ts +37 -0
- package/src/domain/turn-budget.ts +23 -0
- package/src/domain/view.test.ts +803 -0
- package/src/domain/view.ts +491 -0
- package/src/example.test.ts +15 -0
- package/src/extension/commands.test.ts +485 -0
- package/src/extension/commands.ts +230 -0
- package/src/extension/entry.test.ts +1276 -0
- package/src/extension/entry.ts +380 -0
- package/src/extension/gate-alerts.test.ts +237 -0
- package/src/extension/gate-alerts.ts +143 -0
- package/src/extension/guard.test.ts +48 -0
- package/src/extension/guard.ts +31 -0
- package/src/extension/host-signals.test.ts +20 -0
- package/src/extension/host-signals.ts +13 -0
- package/src/extension/inspector-command.test.ts +1343 -0
- package/src/extension/inspector-command.ts +685 -0
- package/src/extension/prompt.test.ts +108 -0
- package/src/extension/prompt.ts +50 -0
- package/src/extension/request-alerts.test.ts +183 -0
- package/src/extension/request-alerts.ts +181 -0
- package/src/extension/retry-observer.test.ts +292 -0
- package/src/extension/retry-observer.ts +150 -0
- package/src/extension/revocation.test.ts +81 -0
- package/src/extension/revocation.ts +84 -0
- package/src/extension/settle-alerts.test.ts +579 -0
- package/src/extension/settle-alerts.ts +175 -0
- package/src/extension/shortcut.test.ts +62 -0
- package/src/extension/shortcut.ts +105 -0
- package/src/extension/tools.test.ts +1294 -0
- package/src/extension/tools.ts +553 -0
- package/src/extension/version.test.ts +115 -0
- package/src/extension/version.ts +97 -0
- package/src/goldens.test.ts +294 -0
- package/src/harness-policy.test.ts +76 -0
- package/src/layers.test.ts +309 -0
- package/src/layers.ts +405 -0
- package/src/package-manifest.test.ts +170 -0
- package/src/presentation/actions.test.ts +221 -0
- package/src/presentation/actions.ts +85 -0
- package/src/presentation/activity.test.ts +386 -0
- package/src/presentation/activity.ts +66 -0
- package/src/presentation/close.test.ts +359 -0
- package/src/presentation/inspector.test.ts +682 -0
- package/src/presentation/inspector.ts +442 -0
- package/src/presentation/overlay.test.ts +553 -0
- package/src/presentation/overlay.ts +229 -0
- package/src/presentation/repaint.test.ts +365 -0
- package/src/presentation/shortcut.ts +2 -0
- package/src/presentation/snapshot.test.ts +535 -0
- package/src/presentation/snapshot.ts +306 -0
- package/src/presentation/status.test.ts +43 -0
- package/src/presentation/status.ts +30 -0
- package/src/presentation/strip.test.ts +203 -0
- package/src/presentation/strip.ts +67 -0
- package/src/presentation/wording.test.ts +388 -0
- package/src/presentation/wording.ts +294 -0
- package/src/runtime/bridge.test.ts +131 -0
- package/src/runtime/bridge.ts +128 -0
- package/src/runtime/clock.test.ts +31 -0
- package/src/runtime/clock.ts +31 -0
- package/src/runtime/config.test.ts +63 -0
- package/src/runtime/config.ts +92 -0
- package/src/runtime/context.test.ts +19 -0
- package/src/runtime/context.ts +9 -0
- package/src/runtime/execution-env.test.ts +33 -0
- package/src/runtime/execution-env.ts +15 -0
- package/src/runtime/extensions.test.ts +86 -0
- package/src/runtime/extensions.ts +152 -0
- package/src/runtime/handle.ts +54 -0
- package/src/runtime/log.test.ts +140 -0
- package/src/runtime/log.ts +91 -0
- package/src/runtime/mcp.test.ts +1450 -0
- package/src/runtime/mcp.ts +1005 -0
- package/src/runtime/models.test.ts +145 -0
- package/src/runtime/models.ts +259 -0
- package/src/runtime/observe.test.ts +50 -0
- package/src/runtime/observe.ts +150 -0
- package/src/runtime/runtime-state.test.ts +25 -0
- package/src/runtime/runtime-state.ts +36 -0
- package/src/runtime/session-tag.ts +11 -0
- package/src/runtime/session.test.ts +352 -0
- package/src/runtime/session.ts +263 -0
- package/src/runtime/storage.ts +29 -0
- package/src/test-hygiene.test.ts +97 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { assert, it } from "@effect/vitest";
|
|
2
|
+
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
|
|
3
|
+
import { makeTempDir } from "../../test-support/platform.ts";
|
|
4
|
+
import { stubHostSource, type StubHostOptions } from "../../test-support/stub-model-source.ts";
|
|
5
|
+
import { HostModelAdapter } from "../runtime/models.ts";
|
|
6
|
+
import { DEFAULTS, type DelegatesConfig } from "../runtime/config.ts";
|
|
7
|
+
import type { LogFields, LogLevel, LogSink } from "../runtime/log.ts";
|
|
8
|
+
import { openTestSession, type TestSession } from "../../test-support/seams.ts";
|
|
9
|
+
import { createCatalogue } from "./definitions.ts";
|
|
10
|
+
import { launch, type HostDefaults } from "./orchestration/launch.ts";
|
|
11
|
+
import { RunDoc } from "./run-records.ts";
|
|
12
|
+
import { FAILURE_REASONS, remedyOf } from "./settlement.ts";
|
|
13
|
+
import { startRetryObserver } from "../extension/retry-observer.ts";
|
|
14
|
+
|
|
15
|
+
const host: HostDefaults = {
|
|
16
|
+
model: { provider: "stub", modelId: "stub-1" },
|
|
17
|
+
thinkingLevel: "low",
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const recordingSink = () => {
|
|
21
|
+
const entries: { level: LogLevel; code: string; fields: LogFields }[] = [];
|
|
22
|
+
const sink: LogSink = {
|
|
23
|
+
write: (level, code, fields = {}) => {
|
|
24
|
+
entries.push({ level, code, fields });
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
return { sink, entries };
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const open = async (
|
|
31
|
+
source: StubHostOptions,
|
|
32
|
+
options: {
|
|
33
|
+
readonly log?: LogSink;
|
|
34
|
+
readonly config?: DelegatesConfig;
|
|
35
|
+
} = {},
|
|
36
|
+
): Promise<TestSession> => {
|
|
37
|
+
const sessionDir = await makeTempDir("delegates-settlement-");
|
|
38
|
+
return openTestSession({
|
|
39
|
+
identity: { sessionId: "s-1", sessionDir, cwd: sessionDir, agentDir: sessionDir },
|
|
40
|
+
models: new HostModelAdapter(stubHostSource(source)),
|
|
41
|
+
...(options.log === undefined ? {} : { log: options.log }),
|
|
42
|
+
...(options.config === undefined ? {} : { config: options.config }),
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const launchScout = (runtime: TestSession, waitMs = 10_000) =>
|
|
47
|
+
launch(
|
|
48
|
+
{ kind: "single", agent: "scout", task: "look" },
|
|
49
|
+
{ runtime, catalogue: createCatalogue(), host, waitMs },
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
it("every failure reason has a remedy and the set is closed", () => {
|
|
53
|
+
assert.strictEqual(FAILURE_REASONS.length, 11);
|
|
54
|
+
const remedies = FAILURE_REASONS.map(remedyOf);
|
|
55
|
+
assert.ok(
|
|
56
|
+
remedies.every((remedy) => remedy !== undefined),
|
|
57
|
+
JSON.stringify(remedies),
|
|
58
|
+
);
|
|
59
|
+
assert.deepStrictEqual(remedies, [
|
|
60
|
+
"relaunch",
|
|
61
|
+
"fix_config",
|
|
62
|
+
"relaunch",
|
|
63
|
+
"relaunch",
|
|
64
|
+
"relaunch",
|
|
65
|
+
"relaunch",
|
|
66
|
+
"relaunch",
|
|
67
|
+
"relaunch",
|
|
68
|
+
"narrow",
|
|
69
|
+
"narrow",
|
|
70
|
+
"relaunch",
|
|
71
|
+
]);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("a provider failure persists its reason and canonical detail", async () => {
|
|
75
|
+
const runtime = await open({ failAt: [1], failMessage: "invalid request: no such model field" });
|
|
76
|
+
try {
|
|
77
|
+
const result = await launchScout(runtime);
|
|
78
|
+
assert.strictEqual(result.view.status, "failed");
|
|
79
|
+
const doc = await runtime.snapshot(RunDoc, result.view.runId);
|
|
80
|
+
assert.strictEqual(doc?.resolution, "failed");
|
|
81
|
+
assert.deepStrictEqual(doc?.failure, {
|
|
82
|
+
reason: "model_error",
|
|
83
|
+
detail: "invalid request: no such model field",
|
|
84
|
+
});
|
|
85
|
+
assert.strictEqual(doc?.settledTaskId, null);
|
|
86
|
+
assert.strictEqual(doc?.retryCount, 0);
|
|
87
|
+
} finally {
|
|
88
|
+
await runtime.close();
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("retryable comes from the observed count, not from the text", async () => {
|
|
93
|
+
const recorder = recordingSink();
|
|
94
|
+
const runtime = await open(
|
|
95
|
+
{ failAt: [1, 2, 3, 4], failMessage: "429: rate limit exceeded" },
|
|
96
|
+
{ log: recorder.sink },
|
|
97
|
+
);
|
|
98
|
+
// The count is the retry observer's evidence, started as the entry starts it.
|
|
99
|
+
const observer = startRetryObserver({ runtime, log: recorder.sink });
|
|
100
|
+
try {
|
|
101
|
+
const result = await launchScout(runtime, 20_000);
|
|
102
|
+
const doc = await runtime.snapshot(RunDoc, result.view.runId);
|
|
103
|
+
assert.ok((doc?.retryCount ?? 0) >= 1, String(doc?.retryCount));
|
|
104
|
+
const failures = recorder.entries.filter((entry) => entry.code === "run.failed");
|
|
105
|
+
assert.strictEqual(failures.length, 1);
|
|
106
|
+
const failure = failures[0];
|
|
107
|
+
assert.strictEqual(failure?.level, "error");
|
|
108
|
+
assert.strictEqual(failure?.fields.reason, "model_error");
|
|
109
|
+
assert.strictEqual(failure?.fields.retryable, true);
|
|
110
|
+
assert.strictEqual(failure?.fields.remedy, "relaunch");
|
|
111
|
+
assert.strictEqual(failure?.fields.detail, "429: rate limit exceeded");
|
|
112
|
+
} finally {
|
|
113
|
+
await observer.close();
|
|
114
|
+
await runtime.close();
|
|
115
|
+
}
|
|
116
|
+
}, 30_000);
|
|
117
|
+
|
|
118
|
+
it("a turn-limit run records the budget text as its detail", async () => {
|
|
119
|
+
const recorder = recordingSink();
|
|
120
|
+
const runtime = await open(
|
|
121
|
+
{ toolCall: { name: "read", arguments: { path: "x" } } },
|
|
122
|
+
{ log: recorder.sink, config: { ...DEFAULTS, maxTurns: 1, diagnostics: [] } },
|
|
123
|
+
);
|
|
124
|
+
try {
|
|
125
|
+
const result = await launchScout(runtime);
|
|
126
|
+
const doc = await runtime.snapshot(RunDoc, result.view.runId);
|
|
127
|
+
assert.strictEqual(doc?.failure?.reason, "turn_limit");
|
|
128
|
+
assert.strictEqual(doc?.failure?.detail, "1 turn");
|
|
129
|
+
const failures = recorder.entries.filter((entry) => entry.code === "run.failed");
|
|
130
|
+
assert.strictEqual(failures.length, 1);
|
|
131
|
+
assert.strictEqual(failures[0]?.fields.retryable, false);
|
|
132
|
+
assert.strictEqual(failures[0]?.fields.remedy, "narrow");
|
|
133
|
+
} finally {
|
|
134
|
+
await runtime.close();
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("a package-native failure keeps its diagnostic text under reason internal", async () => {
|
|
139
|
+
let release = (): void => {};
|
|
140
|
+
const held = new Promise<void>((resolve) => {
|
|
141
|
+
release = resolve;
|
|
142
|
+
});
|
|
143
|
+
const runtime = await open({ text: "slow", hold: () => held });
|
|
144
|
+
try {
|
|
145
|
+
const result = await launchScout(runtime, 0);
|
|
146
|
+
const runId = result.view.runId;
|
|
147
|
+
const { control } = await import("./orchestration/control.ts");
|
|
148
|
+
await control(
|
|
149
|
+
{ action: "stop", run: runId },
|
|
150
|
+
{ runtime, wait: { policy: "settle", waitMs: 10_000 } },
|
|
151
|
+
);
|
|
152
|
+
// The child conversation is gone before the settle phase reads it.
|
|
153
|
+
await runtime.harness.commit(async (tx) => {
|
|
154
|
+
const doc = await tx.doc(RunDoc, runId, { definition: "scout", lane: "default" });
|
|
155
|
+
doc.childConversationId = 9_999;
|
|
156
|
+
}, BACKGROUND_CONTEXT);
|
|
157
|
+
release();
|
|
158
|
+
await control(
|
|
159
|
+
{ action: "resume", run: runId },
|
|
160
|
+
{ runtime, wait: { policy: "settle", waitMs: 10_000 } },
|
|
161
|
+
);
|
|
162
|
+
const doc = await runtime.snapshot(RunDoc, runId);
|
|
163
|
+
assert.strictEqual(doc?.failure?.reason, "internal");
|
|
164
|
+
assert.strictEqual(doc?.failure?.detail, "child conversation is missing");
|
|
165
|
+
} finally {
|
|
166
|
+
await runtime.close();
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("the recorder signals a settlement only after its commit landed", async () => {
|
|
171
|
+
const runtime = await open({ text: "ok" });
|
|
172
|
+
const events: string[] = [];
|
|
173
|
+
const seen: Promise<string | null | undefined>[] = [];
|
|
174
|
+
const detach = runtime.settle.on((event) => {
|
|
175
|
+
events.push(event.runId);
|
|
176
|
+
seen.push(runtime.snapshot(RunDoc, event.runId).then((doc) => doc?.resolution));
|
|
177
|
+
});
|
|
178
|
+
try {
|
|
179
|
+
const result = await launchScout(runtime);
|
|
180
|
+
assert.deepStrictEqual(events, [result.view.runId]);
|
|
181
|
+
assert.deepStrictEqual(await Promise.all(seen), ["answered"]);
|
|
182
|
+
} finally {
|
|
183
|
+
detach();
|
|
184
|
+
await runtime.close();
|
|
185
|
+
}
|
|
186
|
+
});
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import type { Draft } from "@earendil-works/chord/delta";
|
|
2
|
+
import type { JsonObject } from "@earendil-works/pi-durable";
|
|
3
|
+
import { Schema } from "effect";
|
|
4
|
+
import { failureDetailText, type LogSink } from "../runtime/log.ts";
|
|
5
|
+
import { FAILURE_REASONS, FailureReason, type FailureFact, type Resolution } from "./failure.ts";
|
|
6
|
+
import type { RunMetadata } from "./run-records.ts";
|
|
7
|
+
|
|
8
|
+
export { Execution, FAILURE_REASONS, FailureFact, FailureReason, Resolution } from "./failure.ts";
|
|
9
|
+
|
|
10
|
+
export const Remedy = Schema.Literals(["relaunch", "narrow", "fix_config"]);
|
|
11
|
+
export type Remedy = typeof Remedy.Type;
|
|
12
|
+
|
|
13
|
+
/** Exhaustive: a new reason fails the type-check here, before any test runs. */
|
|
14
|
+
const REMEDIES: Readonly<Record<FailureReason, Remedy>> = {
|
|
15
|
+
model_error: "relaunch",
|
|
16
|
+
interrupted: "relaunch",
|
|
17
|
+
faulted: "relaunch",
|
|
18
|
+
orphaned: "relaunch",
|
|
19
|
+
restart_limit: "relaunch",
|
|
20
|
+
aborted: "relaunch",
|
|
21
|
+
reset: "relaunch",
|
|
22
|
+
internal: "relaunch",
|
|
23
|
+
turn_limit: "narrow",
|
|
24
|
+
nothing_to_answer: "narrow",
|
|
25
|
+
no_model: "fix_config",
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const remedyOf = (reason: FailureReason): Remedy => REMEDIES[reason];
|
|
29
|
+
|
|
30
|
+
/** Retryability is observed evidence, never a classification of the text. */
|
|
31
|
+
export const retryableOf = (retryCount: number): boolean => retryCount > 0;
|
|
32
|
+
|
|
33
|
+
/** A durable settlement reason; anything the package did not observe is `internal`. */
|
|
34
|
+
export const reasonFor = (reason: string | undefined): FailureReason =>
|
|
35
|
+
reason !== undefined && (FAILURE_REASONS as readonly string[]).includes(reason)
|
|
36
|
+
? (reason as FailureReason)
|
|
37
|
+
: "internal";
|
|
38
|
+
|
|
39
|
+
export interface RecordedSettlement {
|
|
40
|
+
readonly failure: FailureFact;
|
|
41
|
+
readonly retryable: boolean;
|
|
42
|
+
readonly remedy: Remedy;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface SettlementFact {
|
|
46
|
+
readonly resolution: Resolution;
|
|
47
|
+
readonly reason?: FailureReason;
|
|
48
|
+
readonly rawDetail?: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The settle outcome of one task, and no failure fact: a group ends with a
|
|
53
|
+
* resolution but writes no failure, so the notice still names it.
|
|
54
|
+
*/
|
|
55
|
+
export const recordResolution = (
|
|
56
|
+
doc: Draft<RunMetadata & JsonObject>,
|
|
57
|
+
resolution: Resolution,
|
|
58
|
+
): void => {
|
|
59
|
+
doc.resolution = resolution;
|
|
60
|
+
doc.failure = null;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The only writer of terminal facts: one document write per settlement, no log
|
|
65
|
+
* line. The caller already holds the run's draft, so this module never reads the
|
|
66
|
+
* store and the module graph stays acyclic. Returns the failure evidence for the
|
|
67
|
+
* log line, or `undefined` when the run did not fail.
|
|
68
|
+
*/
|
|
69
|
+
export const recordSettlement = async (
|
|
70
|
+
doc: Draft<RunMetadata & JsonObject>,
|
|
71
|
+
fact: SettlementFact,
|
|
72
|
+
): Promise<RecordedSettlement | undefined> => {
|
|
73
|
+
recordResolution(doc, fact.resolution);
|
|
74
|
+
if (fact.resolution !== "failed") return undefined;
|
|
75
|
+
const failure: FailureFact = {
|
|
76
|
+
reason: fact.reason ?? "internal",
|
|
77
|
+
detail: failureDetailText(fact.rawDetail),
|
|
78
|
+
};
|
|
79
|
+
doc.failure = failure;
|
|
80
|
+
return {
|
|
81
|
+
failure,
|
|
82
|
+
retryable: retryableOf(doc.retryCount),
|
|
83
|
+
remedy: remedyOf(failure.reason),
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/** Exactly one error line per failed run. */
|
|
88
|
+
export const logRunFailed = (
|
|
89
|
+
log: LogSink,
|
|
90
|
+
runId: string,
|
|
91
|
+
failure: FailureFact,
|
|
92
|
+
retryable: boolean,
|
|
93
|
+
): void => {
|
|
94
|
+
log.write("error", "run.failed", {
|
|
95
|
+
runId,
|
|
96
|
+
reason: failure.reason,
|
|
97
|
+
detail: failure.detail,
|
|
98
|
+
retryable,
|
|
99
|
+
remedy: remedyOf(failure.reason),
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
/** One settlement, announced in-process after its commit landed. */
|
|
104
|
+
export interface SettleEvent {
|
|
105
|
+
readonly runId: string;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The in-process carrier between the recorder (inside a run task) and the
|
|
110
|
+
* batcher (a session-level observer). One per session, so it cannot leak
|
|
111
|
+
* across sessions.
|
|
112
|
+
*/
|
|
113
|
+
export interface SettleSignal {
|
|
114
|
+
readonly emit: (event: SettleEvent) => void;
|
|
115
|
+
readonly on: (listener: (event: SettleEvent) => void) => () => void;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export const makeSettleSignal = (): SettleSignal => {
|
|
119
|
+
const listeners = new Set<(event: SettleEvent) => void>();
|
|
120
|
+
return {
|
|
121
|
+
emit: (event) => {
|
|
122
|
+
for (const listener of listeners) listener(event);
|
|
123
|
+
},
|
|
124
|
+
on: (listener) => {
|
|
125
|
+
listeners.add(listener);
|
|
126
|
+
return () => {
|
|
127
|
+
listeners.delete(listener);
|
|
128
|
+
};
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
/** The signal a runtime without one uses: no settlement is announced. */
|
|
134
|
+
export const nullSettleSignal: SettleSignal = {
|
|
135
|
+
emit: () => {},
|
|
136
|
+
on: () => () => {},
|
|
137
|
+
};
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { assert, it } from "@effect/vitest";
|
|
2
|
+
import { Schema } from "effect";
|
|
3
|
+
import {
|
|
4
|
+
decodeInlineDefinition,
|
|
5
|
+
DefinitionFieldNotAllowed,
|
|
6
|
+
InlineDefinition,
|
|
7
|
+
} from "./definitions.ts";
|
|
8
|
+
import { ControlRequest } from "./orchestration/control.ts";
|
|
9
|
+
import { FanoutRequest } from "./orchestration/fanout.ts";
|
|
10
|
+
import { ReviewRequest, SingleRequest } from "./orchestration/launch.ts";
|
|
11
|
+
import { ChildView, LaneView, RunView, SessionView } from "./view.ts";
|
|
12
|
+
|
|
13
|
+
it("the request shapes decode their wire values", () => {
|
|
14
|
+
assert.deepStrictEqual(
|
|
15
|
+
Schema.decodeSync(SingleRequest)({ kind: "single", agent: "scout", task: "look" }),
|
|
16
|
+
{
|
|
17
|
+
kind: "single",
|
|
18
|
+
agent: "scout",
|
|
19
|
+
task: "look",
|
|
20
|
+
},
|
|
21
|
+
);
|
|
22
|
+
assert.deepStrictEqual(
|
|
23
|
+
Schema.decodeSync(FanoutRequest)({
|
|
24
|
+
kind: "fanout",
|
|
25
|
+
lane: "review",
|
|
26
|
+
agents: [{ agent: "scout", task: "look" }],
|
|
27
|
+
policy: "failFast",
|
|
28
|
+
}),
|
|
29
|
+
{
|
|
30
|
+
kind: "fanout",
|
|
31
|
+
lane: "review",
|
|
32
|
+
agents: [{ agent: "scout", task: "look" }],
|
|
33
|
+
policy: "failFast",
|
|
34
|
+
},
|
|
35
|
+
);
|
|
36
|
+
assert.deepStrictEqual(
|
|
37
|
+
Schema.decodeSync(ReviewRequest)({
|
|
38
|
+
kind: "review",
|
|
39
|
+
task: "review",
|
|
40
|
+
reviewers: [{ agent: "reviewer" }],
|
|
41
|
+
}),
|
|
42
|
+
{ kind: "review", task: "review", reviewers: [{ agent: "reviewer" }] },
|
|
43
|
+
);
|
|
44
|
+
assert.deepStrictEqual(
|
|
45
|
+
Schema.decodeSync(ControlRequest)({ action: "steer", run: "r-1", message: "go on" }),
|
|
46
|
+
{ action: "steer", run: "r-1", message: "go on" },
|
|
47
|
+
);
|
|
48
|
+
assert.deepStrictEqual(
|
|
49
|
+
Schema.decodeSync(ControlRequest)({ action: "approve", run: "r-1", gate: "deploy" }),
|
|
50
|
+
{ action: "approve", run: "r-1", gate: "deploy" },
|
|
51
|
+
);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("the view shapes decode their values", () => {
|
|
55
|
+
const child = Schema.decodeSync(ChildView)({
|
|
56
|
+
child: 0,
|
|
57
|
+
runId: "r-1",
|
|
58
|
+
conversationId: null,
|
|
59
|
+
taskId: null,
|
|
60
|
+
agent: "scout",
|
|
61
|
+
status: "answered",
|
|
62
|
+
answer: "done",
|
|
63
|
+
durationMs: 3_000,
|
|
64
|
+
maxTurns: null,
|
|
65
|
+
failure: null,
|
|
66
|
+
});
|
|
67
|
+
const lane = Schema.decodeSync(LaneView)({
|
|
68
|
+
name: "default",
|
|
69
|
+
running: 0,
|
|
70
|
+
queued: 0,
|
|
71
|
+
answered: 1,
|
|
72
|
+
});
|
|
73
|
+
const run = Schema.decodeSync(RunView)({
|
|
74
|
+
runId: "r-1",
|
|
75
|
+
definition: "scout",
|
|
76
|
+
mode: "single",
|
|
77
|
+
lane: "default",
|
|
78
|
+
conversationId: null,
|
|
79
|
+
taskId: null,
|
|
80
|
+
status: "answered",
|
|
81
|
+
answer: "done",
|
|
82
|
+
durationMs: 3_000,
|
|
83
|
+
execution: "wait",
|
|
84
|
+
maxTurns: null,
|
|
85
|
+
failure: null,
|
|
86
|
+
children: [child],
|
|
87
|
+
missing: [],
|
|
88
|
+
gates: [],
|
|
89
|
+
requests: [],
|
|
90
|
+
diagnostic: null,
|
|
91
|
+
});
|
|
92
|
+
assert.deepStrictEqual(
|
|
93
|
+
Schema.decodeSync(SessionView)({
|
|
94
|
+
runs: [run],
|
|
95
|
+
lanes: [lane],
|
|
96
|
+
gates: [],
|
|
97
|
+
requests: [],
|
|
98
|
+
reviews: [],
|
|
99
|
+
}),
|
|
100
|
+
{ runs: [run], lanes: [lane], gates: [], requests: [], reviews: [] },
|
|
101
|
+
);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("an inline definition decodes as part of a single request", () => {
|
|
105
|
+
const definition = decodeInlineDefinition({
|
|
106
|
+
name: "custom",
|
|
107
|
+
role: "helper",
|
|
108
|
+
prompt: { role: "help" },
|
|
109
|
+
tools: ["read"],
|
|
110
|
+
});
|
|
111
|
+
assert.deepStrictEqual(
|
|
112
|
+
Schema.decodeSync(SingleRequest)({
|
|
113
|
+
kind: "single",
|
|
114
|
+
agent: "custom",
|
|
115
|
+
task: "look",
|
|
116
|
+
definition,
|
|
117
|
+
}),
|
|
118
|
+
{ kind: "single", agent: "custom", task: "look", definition },
|
|
119
|
+
);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const inlineValue = {
|
|
123
|
+
name: "custom",
|
|
124
|
+
role: "helper",
|
|
125
|
+
prompt: { role: "help" },
|
|
126
|
+
tools: ["read"],
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
it("an inline definition cannot carry extensions or approval", () => {
|
|
130
|
+
const forbidden = [
|
|
131
|
+
["extensions", ["coding"]],
|
|
132
|
+
["approval", { gates: [] }],
|
|
133
|
+
] as const;
|
|
134
|
+
for (const [field, value] of forbidden) {
|
|
135
|
+
let caught: unknown;
|
|
136
|
+
try {
|
|
137
|
+
decodeInlineDefinition({ ...inlineValue, [field]: value });
|
|
138
|
+
} catch (error) {
|
|
139
|
+
caught = error;
|
|
140
|
+
}
|
|
141
|
+
assert.ok(
|
|
142
|
+
caught instanceof DefinitionFieldNotAllowed &&
|
|
143
|
+
caught.field === field &&
|
|
144
|
+
caught.message.includes(field),
|
|
145
|
+
`${field} was not refused`,
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("an inline definition strips other unknown keys", () => {
|
|
151
|
+
const decoded = decodeInlineDefinition({ ...inlineValue, extra: true, nested: { value: 1 } });
|
|
152
|
+
assert.deepStrictEqual(decoded, inlineValue);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("a valid inline definition carries no extensions or approval", () => {
|
|
156
|
+
const decoded = Schema.decodeSync(InlineDefinition)({
|
|
157
|
+
...inlineValue,
|
|
158
|
+
model: { provider: "stub", modelId: "stub-1" },
|
|
159
|
+
thinkingLevel: "low",
|
|
160
|
+
cwd: "/tmp/work",
|
|
161
|
+
});
|
|
162
|
+
assert.deepStrictEqual(decoded, {
|
|
163
|
+
...inlineValue,
|
|
164
|
+
model: { provider: "stub", modelId: "stub-1" },
|
|
165
|
+
thinkingLevel: "low",
|
|
166
|
+
cwd: "/tmp/work",
|
|
167
|
+
});
|
|
168
|
+
assert.ok(!("extensions" in decoded));
|
|
169
|
+
assert.ok(!("approval" in decoded));
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("an inline definition cannot declare a turn budget", () => {
|
|
173
|
+
let caught: unknown;
|
|
174
|
+
try {
|
|
175
|
+
decodeInlineDefinition({ ...inlineValue, maxTurns: 500 });
|
|
176
|
+
} catch (error) {
|
|
177
|
+
caught = error;
|
|
178
|
+
}
|
|
179
|
+
assert.ok(
|
|
180
|
+
caught instanceof DefinitionFieldNotAllowed &&
|
|
181
|
+
caught.field === "maxTurns" &&
|
|
182
|
+
caught.message.includes("maxTurns"),
|
|
183
|
+
"maxTurns was not refused",
|
|
184
|
+
);
|
|
185
|
+
});
|