@ftrdotdev/pi-delegates 0.0.0-stage → 0.1.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 +77 -0
- package/LICENSE +21 -0
- package/OPERATIONS.md +57 -0
- package/README.md +448 -2
- package/ROADMAP.md +158 -0
- package/TESTING.md +1061 -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 +93 -4
- package/skills/pi-delegates/SKILL.md +147 -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 +156 -0
- package/src/domain/definitions.test.ts +41 -0
- package/src/domain/definitions.ts +161 -0
- package/src/domain/ids.ts +13 -0
- package/src/domain/names.ts +35 -0
- package/src/domain/orchestration/admission.ts +78 -0
- package/src/domain/orchestration/control.test.ts +1298 -0
- package/src/domain/orchestration/control.ts +436 -0
- package/src/domain/orchestration/fanout.test.ts +419 -0
- package/src/domain/orchestration/fanout.ts +334 -0
- package/src/domain/orchestration/gates.test.ts +686 -0
- package/src/domain/orchestration/gates.ts +360 -0
- package/src/domain/orchestration/launch.test.ts +173 -0
- package/src/domain/orchestration/launch.ts +581 -0
- package/src/domain/orchestration/requests.test.ts +700 -0
- package/src/domain/orchestration/requests.ts +769 -0
- package/src/domain/orchestration/review.test.ts +214 -0
- package/src/domain/orchestration/review.ts +168 -0
- package/src/domain/orchestration/run-wait.test.ts +262 -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 +1425 -0
- package/src/domain/orchestration/workflow.ts +705 -0
- package/src/domain/run-records.test.ts +27 -0
- package/src/domain/run-records.ts +527 -0
- package/src/domain/shapes.test.ts +165 -0
- package/src/domain/skills.test.ts +466 -0
- package/src/domain/skills.ts +97 -0
- package/src/domain/tool-schema.test.ts +46 -0
- package/src/domain/tool-schema.ts +20 -0
- package/src/domain/toolkit.test.ts +201 -0
- package/src/domain/toolkit.ts +379 -0
- package/src/domain/view.test.ts +739 -0
- package/src/domain/view.ts +459 -0
- package/src/example.test.ts +15 -0
- package/src/extension/commands.test.ts +481 -0
- package/src/extension/commands.ts +227 -0
- package/src/extension/entry.test.ts +1118 -0
- package/src/extension/entry.ts +342 -0
- package/src/extension/gate-alerts.test.ts +235 -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 +1191 -0
- package/src/extension/inspector-command.ts +638 -0
- package/src/extension/prompt.test.ts +110 -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/revocation.test.ts +81 -0
- package/src/extension/revocation.ts +84 -0
- package/src/extension/shortcut.test.ts +64 -0
- package/src/extension/shortcut.ts +105 -0
- package/src/extension/tools.test.ts +993 -0
- package/src/extension/tools.ts +505 -0
- package/src/extension/version.test.ts +123 -0
- package/src/extension/version.ts +97 -0
- package/src/harness-policy.test.ts +74 -0
- package/src/package-manifest.test.ts +157 -0
- package/src/presentation/actions.test.ts +221 -0
- package/src/presentation/actions.ts +85 -0
- package/src/presentation/activity.test.ts +387 -0
- package/src/presentation/activity.ts +66 -0
- package/src/presentation/close.test.ts +361 -0
- package/src/presentation/inspector.test.ts +679 -0
- package/src/presentation/inspector.ts +440 -0
- package/src/presentation/overlay.test.ts +555 -0
- package/src/presentation/overlay.ts +229 -0
- package/src/presentation/repaint.test.ts +329 -0
- package/src/presentation/shortcut.ts +2 -0
- package/src/presentation/snapshot.test.ts +530 -0
- package/src/presentation/snapshot.ts +306 -0
- package/src/presentation/strip.test.ts +196 -0
- package/src/presentation/strip.ts +74 -0
- package/src/presentation/wording.test.ts +238 -0
- package/src/presentation/wording.ts +222 -0
- package/src/runtime/bridge.test.ts +131 -0
- package/src/runtime/bridge.ts +134 -0
- package/src/runtime/config.test.ts +44 -0
- package/src/runtime/config.ts +87 -0
- package/src/runtime/context.test.ts +19 -0
- package/src/runtime/context.ts +9 -0
- package/src/runtime/execution-env.test.ts +35 -0
- package/src/runtime/execution-env.ts +15 -0
- package/src/runtime/extensions.test.ts +86 -0
- package/src/runtime/extensions.ts +140 -0
- package/src/runtime/log.test.ts +128 -0
- package/src/runtime/log.ts +68 -0
- package/src/runtime/mcp.test.ts +1453 -0
- package/src/runtime/mcp.ts +938 -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/session.test.ts +359 -0
- package/src/runtime/session.ts +425 -0
- package/src/runtime/timers.test.ts +31 -0
- package/src/runtime/timers.ts +10 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { MemoryStorage } from "@earendil-works/pi-durable";
|
|
2
|
+
import { assert, it } from "@effect/vitest";
|
|
3
|
+
import { PROPERTY_RUNS } from "@ftrdotdev/pi-test-kit";
|
|
4
|
+
import {
|
|
5
|
+
dispositionArbitrary,
|
|
6
|
+
dispositionsArbitrary,
|
|
7
|
+
freeTextArbitrary,
|
|
8
|
+
noteArbitrary,
|
|
9
|
+
} from "../../../test-support/arbitraries.ts";
|
|
10
|
+
import { mkdtemp } from "node:fs/promises";
|
|
11
|
+
import { tmpdir } from "node:os";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
import { stubHostSource } from "../../../test-support/stub-model-source.ts";
|
|
14
|
+
import { createCatalogue } from "../definitions.ts";
|
|
15
|
+
import { launch, type HostDefaults } from "./launch.ts";
|
|
16
|
+
import {
|
|
17
|
+
aggregate,
|
|
18
|
+
parseDisposition,
|
|
19
|
+
ReviewDoc,
|
|
20
|
+
type Disposition as DispositionType,
|
|
21
|
+
} from "./review.ts";
|
|
22
|
+
import { viewSession } from "../view.ts";
|
|
23
|
+
import { DEFAULTS } from "../../runtime/config.ts";
|
|
24
|
+
import { HostModelAdapter } from "../../runtime/models.ts";
|
|
25
|
+
import { openSessionRuntime, type SessionIdentity } from "../../runtime/session.ts";
|
|
26
|
+
|
|
27
|
+
const host: HostDefaults = {
|
|
28
|
+
model: { provider: "stub", modelId: "stub-1" },
|
|
29
|
+
thinkingLevel: "low",
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const identityFor = async (sessionId: string): Promise<SessionIdentity> => {
|
|
33
|
+
const sessionDir = await mkdtemp(join(tmpdir(), "delegates-review-"));
|
|
34
|
+
return { sessionId, sessionDir, cwd: sessionDir, agentDir: sessionDir };
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const corpus = (texts: readonly string[]) => stubHostSource({ texts });
|
|
38
|
+
|
|
39
|
+
const reviewers = [{ agent: "reviewer" }, { agent: "reviewer" }, { agent: "reviewer" }];
|
|
40
|
+
|
|
41
|
+
it("each reviewer gets a disposition row", async () => {
|
|
42
|
+
const runtime = await openSessionRuntime({
|
|
43
|
+
identity: await identityFor("s-1"),
|
|
44
|
+
models: new HostModelAdapter(
|
|
45
|
+
corpus([
|
|
46
|
+
'{"disposition":"accepted"}',
|
|
47
|
+
'{"disposition":"changes_requested","note":"fix it"}',
|
|
48
|
+
'{"disposition":"accepted"}',
|
|
49
|
+
]),
|
|
50
|
+
),
|
|
51
|
+
storage: new MemoryStorage(),
|
|
52
|
+
});
|
|
53
|
+
try {
|
|
54
|
+
const result = await launch(
|
|
55
|
+
{ kind: "review", reviewers, task: "review the diff" },
|
|
56
|
+
{ runtime, catalogue: createCatalogue(), host, waitMs: 20_000 },
|
|
57
|
+
);
|
|
58
|
+
const record = await runtime.snapshot(ReviewDoc, result.view.runId);
|
|
59
|
+
assert.strictEqual(record?.reviewers.length, 3);
|
|
60
|
+
assert.deepStrictEqual(record?.reviewers.map((row) => row.disposition).sort(), [
|
|
61
|
+
"accepted",
|
|
62
|
+
"accepted",
|
|
63
|
+
"changes_requested",
|
|
64
|
+
]);
|
|
65
|
+
assert.strictEqual(record?.aggregate, "changes_requested");
|
|
66
|
+
assert.strictEqual(result.view.answer, "changes_requested");
|
|
67
|
+
assert.strictEqual(result.text, `Run ${result.view.runId}: changes_requested`);
|
|
68
|
+
} finally {
|
|
69
|
+
await runtime.close();
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const cases: ReadonlyArray<readonly [readonly DispositionType[], DispositionType]> = [
|
|
74
|
+
[["rejected", "accepted"], "rejected"],
|
|
75
|
+
[["changes_requested", "accepted"], "changes_requested"],
|
|
76
|
+
[["inconclusive", "accepted"], "inconclusive"],
|
|
77
|
+
[["accepted", "accepted"], "accepted"],
|
|
78
|
+
];
|
|
79
|
+
|
|
80
|
+
it.each(cases)("aggregation follows the policy order: %j", (dispositions, expected) => {
|
|
81
|
+
assert.strictEqual(aggregate(dispositions), expected);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("a reviewer that fails yields inconclusive", async () => {
|
|
85
|
+
const runtime = await openSessionRuntime({
|
|
86
|
+
identity: await identityFor("s-1"),
|
|
87
|
+
models: new HostModelAdapter(
|
|
88
|
+
stubHostSource({
|
|
89
|
+
failAt: [2],
|
|
90
|
+
texts: ['{"disposition":"accepted"}', "", '{"disposition":"accepted"}'],
|
|
91
|
+
}),
|
|
92
|
+
),
|
|
93
|
+
storage: new MemoryStorage(),
|
|
94
|
+
config: { ...DEFAULTS, laneCap: 1, sessionCeiling: 8, diagnostics: [] },
|
|
95
|
+
});
|
|
96
|
+
try {
|
|
97
|
+
const result = await launch(
|
|
98
|
+
{ kind: "review", reviewers, task: "review the diff" },
|
|
99
|
+
{ runtime, catalogue: createCatalogue(), host, waitMs: 20_000 },
|
|
100
|
+
);
|
|
101
|
+
const record = await runtime.snapshot(ReviewDoc, result.view.runId);
|
|
102
|
+
assert.strictEqual(record?.reviewers[1]?.disposition, "inconclusive");
|
|
103
|
+
assert.ok(record?.reviewers[1]?.diagnostic !== null);
|
|
104
|
+
assert.strictEqual(record?.aggregate, "inconclusive");
|
|
105
|
+
} finally {
|
|
106
|
+
await runtime.close();
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("a malformed answer is inconclusive and keeps the raw text", async () => {
|
|
111
|
+
const runtime = await openSessionRuntime({
|
|
112
|
+
identity: await identityFor("s-1"),
|
|
113
|
+
models: new HostModelAdapter(corpus(["looks good to me", '{"disposition":"maybe"}'])),
|
|
114
|
+
storage: new MemoryStorage(),
|
|
115
|
+
});
|
|
116
|
+
try {
|
|
117
|
+
const result = await launch(
|
|
118
|
+
{ kind: "review", reviewers: reviewers.slice(0, 2), task: "review the diff" },
|
|
119
|
+
{ runtime, catalogue: createCatalogue(), host, waitMs: 20_000 },
|
|
120
|
+
);
|
|
121
|
+
const record = await runtime.snapshot(ReviewDoc, result.view.runId);
|
|
122
|
+
assert.deepStrictEqual(
|
|
123
|
+
record?.reviewers.map((row) => row.disposition),
|
|
124
|
+
["inconclusive", "inconclusive"],
|
|
125
|
+
);
|
|
126
|
+
assert.strictEqual(record?.reviewers[0]?.diagnostic, "looks good to me");
|
|
127
|
+
assert.strictEqual(record?.reviewers[1]?.diagnostic, '{"disposition":"maybe"}');
|
|
128
|
+
} finally {
|
|
129
|
+
await runtime.close();
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it.prop(
|
|
134
|
+
"only a well-formed answer parses",
|
|
135
|
+
{ disposition: dispositionArbitrary, note: noteArbitrary },
|
|
136
|
+
({ disposition, note }) => {
|
|
137
|
+
const json = JSON.stringify({ disposition, note });
|
|
138
|
+
assert.deepStrictEqual(parseDisposition(json), { disposition, note });
|
|
139
|
+
assert.deepStrictEqual(parseDisposition(`Here is my review.\n${json}`), {
|
|
140
|
+
disposition,
|
|
141
|
+
note,
|
|
142
|
+
});
|
|
143
|
+
assert.deepStrictEqual(parseDisposition(` ${json} `), { disposition, note });
|
|
144
|
+
},
|
|
145
|
+
{ arbitrary: { runs: PROPERTY_RUNS } },
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
it.prop(
|
|
149
|
+
"aggregation follows the policy order",
|
|
150
|
+
{ dispositions: dispositionsArbitrary },
|
|
151
|
+
({ dispositions }) => {
|
|
152
|
+
const value = aggregate(dispositions);
|
|
153
|
+
if (dispositions.includes("rejected")) assert.strictEqual(value, "rejected");
|
|
154
|
+
else if (dispositions.includes("changes_requested")) {
|
|
155
|
+
assert.strictEqual(value, "changes_requested");
|
|
156
|
+
} else if (dispositions.includes("inconclusive") || dispositions.length === 0) {
|
|
157
|
+
assert.strictEqual(value, "inconclusive");
|
|
158
|
+
} else {
|
|
159
|
+
assert.strictEqual(value, "accepted");
|
|
160
|
+
}
|
|
161
|
+
assert.strictEqual(aggregate([...dispositions, "accepted"]), value);
|
|
162
|
+
},
|
|
163
|
+
{ arbitrary: { runs: PROPERTY_RUNS } },
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
it.prop(
|
|
167
|
+
"text without a disposition object never parses",
|
|
168
|
+
{ text: freeTextArbitrary },
|
|
169
|
+
({ text }) => {
|
|
170
|
+
if (text.includes("{")) return;
|
|
171
|
+
assert.strictEqual(parseDisposition(text), undefined);
|
|
172
|
+
},
|
|
173
|
+
{ arbitrary: { runs: PROPERTY_RUNS } },
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
it("dispositions survive a reopen", async () => {
|
|
177
|
+
const identity = await identityFor("s-1");
|
|
178
|
+
const first = await openSessionRuntime({
|
|
179
|
+
identity,
|
|
180
|
+
models: new HostModelAdapter(
|
|
181
|
+
corpus(['{"disposition":"accepted"}', '{"disposition":"rejected"}']),
|
|
182
|
+
),
|
|
183
|
+
});
|
|
184
|
+
const result = await launch(
|
|
185
|
+
{ kind: "review", reviewers: reviewers.slice(0, 2), task: "review" },
|
|
186
|
+
{ runtime: first, catalogue: createCatalogue(), host, waitMs: 20_000 },
|
|
187
|
+
);
|
|
188
|
+
await first.close();
|
|
189
|
+
|
|
190
|
+
const second = await openSessionRuntime({ identity, models: new HostModelAdapter(corpus([])) });
|
|
191
|
+
try {
|
|
192
|
+
const session = await viewSession(second);
|
|
193
|
+
const review = session.reviews[0];
|
|
194
|
+
assert.strictEqual(review?.reviewers.length, 2);
|
|
195
|
+
assert.strictEqual(review?.aggregate, "rejected");
|
|
196
|
+
assert.strictEqual(review?.runId, result.view.runId);
|
|
197
|
+
} finally {
|
|
198
|
+
await second.close();
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it("ignores a disposition object followed by trailing prose", () => {
|
|
203
|
+
assert.strictEqual(parseDisposition('{"disposition":"accepted"}\nthanks for reading'), undefined);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("chooses the last valid object after prose with two objects", () => {
|
|
207
|
+
const answer =
|
|
208
|
+
'First I thought {"disposition":"inconclusive"}; then I decided ' +
|
|
209
|
+
'{"disposition":"changes_requested","note":"fix it"}';
|
|
210
|
+
assert.deepStrictEqual(parseDisposition(answer), {
|
|
211
|
+
disposition: "changes_requested",
|
|
212
|
+
note: "fix it",
|
|
213
|
+
});
|
|
214
|
+
});
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { defineDocFamily, type JsonObject } from "@earendil-works/pi-durable";
|
|
2
|
+
import { Option, Schema } from "effect";
|
|
3
|
+
|
|
4
|
+
export const Disposition = Schema.Literals([
|
|
5
|
+
"accepted",
|
|
6
|
+
"changes_requested",
|
|
7
|
+
"rejected",
|
|
8
|
+
"inconclusive",
|
|
9
|
+
]);
|
|
10
|
+
export type Disposition = typeof Disposition.Type;
|
|
11
|
+
|
|
12
|
+
export const DISPOSITIONS = ["accepted", "changes_requested", "rejected", "inconclusive"] as const;
|
|
13
|
+
|
|
14
|
+
/** The reviewer's whole answer. One Schema serves decoding, generation and the message. */
|
|
15
|
+
export const DispositionAnswer = Schema.Struct({
|
|
16
|
+
disposition: Disposition,
|
|
17
|
+
note: Schema.optional(Schema.String),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
export const ReviewEntry = Schema.Struct({
|
|
21
|
+
child: Schema.Finite,
|
|
22
|
+
runId: Schema.String,
|
|
23
|
+
disposition: Disposition,
|
|
24
|
+
note: Schema.NullOr(Schema.String),
|
|
25
|
+
diagnostic: Schema.NullOr(Schema.String),
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
export const ReviewRecord = Schema.Struct({
|
|
29
|
+
runId: Schema.String,
|
|
30
|
+
reviewers: Schema.Array(ReviewEntry),
|
|
31
|
+
aggregate: Disposition,
|
|
32
|
+
});
|
|
33
|
+
export type ReviewRecord = typeof ReviewRecord.Type;
|
|
34
|
+
export type ReviewEntry = typeof ReviewEntry.Type;
|
|
35
|
+
|
|
36
|
+
export const ReviewDoc = defineDocFamily<
|
|
37
|
+
ReviewRecord & JsonObject,
|
|
38
|
+
{ readonly runId: string } & JsonObject
|
|
39
|
+
>({
|
|
40
|
+
kind: "delegates.review",
|
|
41
|
+
version: 1,
|
|
42
|
+
scope: "session",
|
|
43
|
+
family: true,
|
|
44
|
+
initial: (seed) => ({ runId: seed.runId, reviewers: [], aggregate: "inconclusive" }),
|
|
45
|
+
checkpointWhen: () => true,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
/** Rejection wins; then a change request; then inconclusive; otherwise accepted. */
|
|
49
|
+
export const aggregate = (dispositions: readonly Disposition[]): Disposition => {
|
|
50
|
+
if (dispositions.includes("rejected")) return "rejected";
|
|
51
|
+
if (dispositions.includes("changes_requested")) return "changes_requested";
|
|
52
|
+
if (dispositions.includes("inconclusive") || dispositions.length === 0) return "inconclusive";
|
|
53
|
+
return "accepted";
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export interface ReviewOutcome {
|
|
57
|
+
readonly disposition: Disposition;
|
|
58
|
+
readonly note: string | null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const decode = Schema.decodeUnknownOption(DispositionAnswer);
|
|
62
|
+
|
|
63
|
+
const parseObject = (text: string): ReviewOutcome | undefined => {
|
|
64
|
+
let value: unknown;
|
|
65
|
+
try {
|
|
66
|
+
value = JSON.parse(text);
|
|
67
|
+
} catch {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
const result = decode(value);
|
|
71
|
+
if (Option.isSome(result)) {
|
|
72
|
+
return {
|
|
73
|
+
disposition: result.value.disposition,
|
|
74
|
+
note: result.value.note ?? null,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
return undefined;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/** The brace-balanced objects in the text, outermost first. */
|
|
81
|
+
const objects = (text: string): readonly string[] => {
|
|
82
|
+
const found: string[] = [];
|
|
83
|
+
for (let start = 0; start < text.length; start += 1) {
|
|
84
|
+
if (text[start] !== "{") continue;
|
|
85
|
+
let depth = 0;
|
|
86
|
+
let inString = false;
|
|
87
|
+
let escaped = false;
|
|
88
|
+
for (let end = start; end < text.length; end += 1) {
|
|
89
|
+
const character = text[end];
|
|
90
|
+
if (inString) {
|
|
91
|
+
if (escaped) escaped = false;
|
|
92
|
+
else if (character === "\\") escaped = true;
|
|
93
|
+
else if (character === '"') inString = false;
|
|
94
|
+
} else if (character === '"') inString = true;
|
|
95
|
+
else if (character === "{") depth += 1;
|
|
96
|
+
else if (character === "}") {
|
|
97
|
+
depth -= 1;
|
|
98
|
+
if (depth === 0) {
|
|
99
|
+
found.push(text.slice(start, end + 1));
|
|
100
|
+
start = end;
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return found;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/** The last object in the text that decodes as a disposition answer and ends the text. */
|
|
110
|
+
const lastObject = (text: string): ReviewOutcome | undefined => {
|
|
111
|
+
const trimmed = text.trimEnd();
|
|
112
|
+
for (const candidate of [...objects(trimmed)].reverse()) {
|
|
113
|
+
if (!trimmed.endsWith(candidate)) continue;
|
|
114
|
+
const parsed = parseObject(candidate);
|
|
115
|
+
if (parsed !== undefined) return parsed;
|
|
116
|
+
}
|
|
117
|
+
return undefined;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Accepts the whole trimmed answer as one JSON object, or the last such object
|
|
122
|
+
* at the end of a prose answer. Any other text yields `undefined`; never throws.
|
|
123
|
+
*/
|
|
124
|
+
export const parseDisposition = (answer: string): ReviewOutcome | undefined =>
|
|
125
|
+
parseObject(answer.trim()) ?? lastObject(answer);
|
|
126
|
+
|
|
127
|
+
export const REVIEW_INSTRUCTION =
|
|
128
|
+
'End your answer with one JSON object: {"disposition":"accepted"|"changes_requested"|"rejected"|"inconclusive","note":"..."}';
|
|
129
|
+
|
|
130
|
+
export const reviewRow = (input: {
|
|
131
|
+
readonly child: number;
|
|
132
|
+
readonly runId: string;
|
|
133
|
+
readonly answer: string | null;
|
|
134
|
+
readonly failure?: string;
|
|
135
|
+
}): ReviewEntry => {
|
|
136
|
+
if (input.answer === null) {
|
|
137
|
+
return {
|
|
138
|
+
child: input.child,
|
|
139
|
+
runId: input.runId,
|
|
140
|
+
disposition: "inconclusive",
|
|
141
|
+
note: null,
|
|
142
|
+
diagnostic: input.failure ?? "no answer",
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
const parsed = parseDisposition(input.answer);
|
|
146
|
+
if (parsed === undefined) {
|
|
147
|
+
return {
|
|
148
|
+
child: input.child,
|
|
149
|
+
runId: input.runId,
|
|
150
|
+
disposition: "inconclusive",
|
|
151
|
+
note: null,
|
|
152
|
+
diagnostic: input.answer,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
child: input.child,
|
|
157
|
+
runId: input.runId,
|
|
158
|
+
disposition: parsed.disposition,
|
|
159
|
+
note: parsed.note,
|
|
160
|
+
diagnostic: null,
|
|
161
|
+
};
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
export const reviewRecord = (runId: string, rows: readonly ReviewEntry[]): ReviewRecord => ({
|
|
165
|
+
runId,
|
|
166
|
+
reviewers: [...rows],
|
|
167
|
+
aggregate: aggregate(rows.map((row) => row.disposition)),
|
|
168
|
+
});
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { assert, it } from "@effect/vitest";
|
|
2
|
+
import type { SessionRuntime } from "../../runtime/session.ts";
|
|
3
|
+
import type { Timers } from "../../runtime/timers.ts";
|
|
4
|
+
import { fakeDocRuntime } from "../../../test-support/fake-doc-runtime.ts";
|
|
5
|
+
import { textFor } from "./launch.ts";
|
|
6
|
+
import { ATTENTION_BACKSTOP_MS, ATTENTION_RETRY_MS, waitForRun, waitForTask } from "./run-wait.ts";
|
|
7
|
+
import { gateKey } from "./gates.ts";
|
|
8
|
+
import type { RequestIndexRow } from "./requests.ts";
|
|
9
|
+
import type { RunView } from "../view.ts";
|
|
10
|
+
|
|
11
|
+
const fakeTimers = () => {
|
|
12
|
+
const armed = new Map<number, () => void>();
|
|
13
|
+
let next = 1;
|
|
14
|
+
const timers: Timers = {
|
|
15
|
+
setTimeout: (callback) => {
|
|
16
|
+
const id = next;
|
|
17
|
+
next += 1;
|
|
18
|
+
armed.set(id, callback);
|
|
19
|
+
return id;
|
|
20
|
+
},
|
|
21
|
+
clearTimeout: (handle) => {
|
|
22
|
+
armed.delete(handle as number);
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
return {
|
|
26
|
+
timers,
|
|
27
|
+
/** Fires every armed callback once, as an elapsed deadline would. */
|
|
28
|
+
fire: () => {
|
|
29
|
+
const callbacks = [...armed.values()];
|
|
30
|
+
armed.clear();
|
|
31
|
+
for (const callback of callbacks) callback();
|
|
32
|
+
},
|
|
33
|
+
size: () => armed.size,
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** A runtime whose harness wait never settles and whose call rejects on abort. */
|
|
38
|
+
const neverRuntime = (state: { aborted: boolean }): SessionRuntime =>
|
|
39
|
+
({
|
|
40
|
+
run: <A>(_operation: unknown, signal?: AbortSignal): Promise<A> =>
|
|
41
|
+
new Promise<A>((_resolve, reject) => {
|
|
42
|
+
const abort = () => {
|
|
43
|
+
state.aborted = true;
|
|
44
|
+
reject(new Error("aborted"));
|
|
45
|
+
};
|
|
46
|
+
if (signal?.aborted ?? false) abort();
|
|
47
|
+
else signal?.addEventListener("abort", abort, { once: true });
|
|
48
|
+
}),
|
|
49
|
+
harness: { waitForTask: () => new Promise(() => {}) },
|
|
50
|
+
}) as unknown as SessionRuntime;
|
|
51
|
+
|
|
52
|
+
const flush = (): Promise<void> => new Promise((resolve) => setTimeout(resolve, 0));
|
|
53
|
+
|
|
54
|
+
const requestRow = (input: Partial<RequestIndexRow> = {}): RequestIndexRow => ({
|
|
55
|
+
key: "12:c1",
|
|
56
|
+
runId: "run:abc",
|
|
57
|
+
requestId: "12:c1",
|
|
58
|
+
requestedAt: 1_000,
|
|
59
|
+
deadline: 2_000,
|
|
60
|
+
status: "pending",
|
|
61
|
+
...input,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const waitingView = (input: Partial<RunView> = {}): RunView => ({
|
|
65
|
+
runId: "run:abc",
|
|
66
|
+
definition: "scout",
|
|
67
|
+
mode: "single",
|
|
68
|
+
lane: "default",
|
|
69
|
+
conversationId: null,
|
|
70
|
+
taskId: 7,
|
|
71
|
+
status: "waiting",
|
|
72
|
+
answer: null,
|
|
73
|
+
durationMs: null,
|
|
74
|
+
children: [],
|
|
75
|
+
missing: [],
|
|
76
|
+
gates: [],
|
|
77
|
+
requests: [],
|
|
78
|
+
diagnostic: null,
|
|
79
|
+
...input,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const emptyIndexes = {
|
|
83
|
+
"delegates.gate-index": { keys: [] },
|
|
84
|
+
"delegates.request-index": { rows: [] },
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
it("a timeout returns and cancels the harness wait", async () => {
|
|
88
|
+
const fake = fakeTimers();
|
|
89
|
+
const state = { aborted: false };
|
|
90
|
+
const pending = waitForTask(
|
|
91
|
+
{ runtime: neverRuntime(state), timers: fake.timers },
|
|
92
|
+
{
|
|
93
|
+
taskId: 7,
|
|
94
|
+
waitMs: 1_000,
|
|
95
|
+
},
|
|
96
|
+
);
|
|
97
|
+
fake.fire();
|
|
98
|
+
|
|
99
|
+
assert.strictEqual(await pending, "timeout");
|
|
100
|
+
assert.strictEqual(state.aborted, true);
|
|
101
|
+
assert.strictEqual(fake.size(), 0);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("the caller signal ends the wait as aborted", async () => {
|
|
105
|
+
const fake = fakeTimers();
|
|
106
|
+
const controller = new AbortController();
|
|
107
|
+
const pending = waitForTask(
|
|
108
|
+
{ runtime: neverRuntime({ aborted: false }), timers: fake.timers },
|
|
109
|
+
{ taskId: 7, waitMs: 60_000, signal: controller.signal },
|
|
110
|
+
);
|
|
111
|
+
controller.abort();
|
|
112
|
+
|
|
113
|
+
assert.strictEqual(await pending, "aborted");
|
|
114
|
+
assert.strictEqual(fake.size(), 0);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("a waiting parent returns on the commit, not on a poll", async () => {
|
|
118
|
+
const fake = fakeDocRuntime({ now: () => 1_500, docs: emptyIndexes });
|
|
119
|
+
const pending = waitForRun(
|
|
120
|
+
{ runtime: fake.runtime, timers: fake.timers },
|
|
121
|
+
{ runId: "run:abc", taskId: 7, waitMs: 5_000 },
|
|
122
|
+
);
|
|
123
|
+
await flush();
|
|
124
|
+
fake.commitDoc("delegates.request-index", { rows: [requestRow()] });
|
|
125
|
+
|
|
126
|
+
assert.strictEqual(await pending, "request");
|
|
127
|
+
assert.ok(!fake.delays().includes(50), fake.delays().join(","));
|
|
128
|
+
assert.strictEqual(
|
|
129
|
+
textFor(waitingView({ requests: ["12:c1"] })),
|
|
130
|
+
"Run run:abc waits for request 12:c1.",
|
|
131
|
+
);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("a spurious advance re-checks instead of returning", async () => {
|
|
135
|
+
const fake = fakeDocRuntime({ now: () => 1_500, docs: emptyIndexes });
|
|
136
|
+
let outcome: string | undefined;
|
|
137
|
+
const pending = waitForRun(
|
|
138
|
+
{ runtime: fake.runtime, timers: fake.timers },
|
|
139
|
+
{ runId: "run:abc", taskId: 7, waitMs: 5_000 },
|
|
140
|
+
).then((value) => {
|
|
141
|
+
outcome = value;
|
|
142
|
+
return value;
|
|
143
|
+
});
|
|
144
|
+
await flush();
|
|
145
|
+
const other = requestRow({ key: "12:c2", requestId: "12:c2", runId: "run:other" });
|
|
146
|
+
fake.commitDoc("delegates.request-index", { rows: [other] });
|
|
147
|
+
await flush();
|
|
148
|
+
assert.strictEqual(outcome, undefined);
|
|
149
|
+
fake.commitDoc("delegates.request-index", { rows: [other, requestRow()] });
|
|
150
|
+
|
|
151
|
+
assert.strictEqual(await pending, "request");
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("the backstop re-checks after a missed push", async () => {
|
|
155
|
+
const fake = fakeDocRuntime({ now: () => 1_500, docs: emptyIndexes });
|
|
156
|
+
const pending = waitForRun(
|
|
157
|
+
{ runtime: fake.runtime, timers: fake.timers },
|
|
158
|
+
{ runId: "run:abc", taskId: 7, waitMs: 60_000 },
|
|
159
|
+
);
|
|
160
|
+
await flush();
|
|
161
|
+
// A commit whose push is lost: the document moves and no observer hears it.
|
|
162
|
+
fake.setDoc("delegates.request-index", { rows: [requestRow()] });
|
|
163
|
+
fake.fireDelay(ATTENTION_BACKSTOP_MS);
|
|
164
|
+
|
|
165
|
+
assert.strictEqual(await pending, "request");
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("an absent index attaches on a retry, not a read loop", async () => {
|
|
169
|
+
const fake = fakeDocRuntime({
|
|
170
|
+
now: () => 1_500,
|
|
171
|
+
docs: { "delegates.gate-index": { keys: [] } },
|
|
172
|
+
});
|
|
173
|
+
const pending = waitForRun(
|
|
174
|
+
{ runtime: fake.runtime, timers: fake.timers },
|
|
175
|
+
{ runId: "run:abc", taskId: 7, waitMs: 60_000 },
|
|
176
|
+
);
|
|
177
|
+
await flush();
|
|
178
|
+
fake.setDoc("delegates.request-index", { rows: [requestRow()] });
|
|
179
|
+
fake.fireDelay(ATTENTION_RETRY_MS);
|
|
180
|
+
|
|
181
|
+
assert.strictEqual(await pending, "request");
|
|
182
|
+
assert.strictEqual(
|
|
183
|
+
fake.delays().filter((delay) => delay === ATTENTION_RETRY_MS).length,
|
|
184
|
+
1,
|
|
185
|
+
fake.delays().join(","),
|
|
186
|
+
);
|
|
187
|
+
assert.ok(!fake.delays().includes(50), fake.delays().join(","));
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("a gate wakes the wait and stops the poll", async () => {
|
|
191
|
+
const key = gateKey("run:abc", "approve-deploy");
|
|
192
|
+
const fake = fakeDocRuntime({
|
|
193
|
+
now: () => 1_500,
|
|
194
|
+
docs: {
|
|
195
|
+
"delegates.gate-index": { keys: [] },
|
|
196
|
+
"delegates.request-index": { rows: [] },
|
|
197
|
+
[`delegates.gate:${key}`]: {
|
|
198
|
+
runId: "run:abc",
|
|
199
|
+
gate: "approve-deploy",
|
|
200
|
+
status: "pending",
|
|
201
|
+
tool: null,
|
|
202
|
+
taskId: 7,
|
|
203
|
+
taskIds: [7],
|
|
204
|
+
childRunIds: ["run:abc"],
|
|
205
|
+
requestedAt: 0,
|
|
206
|
+
deadline: Number.MAX_SAFE_INTEGER,
|
|
207
|
+
decision: null,
|
|
208
|
+
decidedBy: null,
|
|
209
|
+
reason: null,
|
|
210
|
+
},
|
|
211
|
+
},
|
|
212
|
+
});
|
|
213
|
+
const pending = waitForRun(
|
|
214
|
+
{ runtime: fake.runtime, timers: fake.timers },
|
|
215
|
+
{ runId: "run:abc", taskId: 7, waitMs: 5_000 },
|
|
216
|
+
);
|
|
217
|
+
await flush();
|
|
218
|
+
fake.commitDoc("delegates.gate-index", { keys: [key] });
|
|
219
|
+
|
|
220
|
+
assert.strictEqual(await pending, "gate");
|
|
221
|
+
await flush();
|
|
222
|
+
assert.deepStrictEqual([...fake.detaches].sort(), [
|
|
223
|
+
"delegates.gate-index",
|
|
224
|
+
"delegates.request-index",
|
|
225
|
+
]);
|
|
226
|
+
assert.ok(!fake.delays().includes(50), fake.delays().join(","));
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("a run waiting on a gate and a request names both", async () => {
|
|
230
|
+
const key = gateKey("run:abc", "deploy");
|
|
231
|
+
const fake = fakeDocRuntime({
|
|
232
|
+
now: () => 1_500,
|
|
233
|
+
docs: {
|
|
234
|
+
"delegates.gate-index": { keys: [key] },
|
|
235
|
+
"delegates.request-index": { rows: [requestRow()] },
|
|
236
|
+
[`delegates.gate:${key}`]: {
|
|
237
|
+
runId: "run:abc",
|
|
238
|
+
gate: "deploy",
|
|
239
|
+
status: "pending",
|
|
240
|
+
tool: "bash",
|
|
241
|
+
taskId: 7,
|
|
242
|
+
taskIds: [7],
|
|
243
|
+
childRunIds: ["run:abc"],
|
|
244
|
+
requestedAt: 0,
|
|
245
|
+
deadline: Number.MAX_SAFE_INTEGER,
|
|
246
|
+
decision: null,
|
|
247
|
+
decidedBy: null,
|
|
248
|
+
reason: null,
|
|
249
|
+
},
|
|
250
|
+
},
|
|
251
|
+
});
|
|
252
|
+
const outcome = await waitForRun(
|
|
253
|
+
{ runtime: fake.runtime, timers: fake.timers },
|
|
254
|
+
{ runId: "run:abc", taskId: 7, waitMs: 5_000 },
|
|
255
|
+
);
|
|
256
|
+
|
|
257
|
+
assert.strictEqual(outcome, "gate");
|
|
258
|
+
assert.strictEqual(
|
|
259
|
+
textFor(waitingView({ gates: ["deploy"], requests: ["12:c1"] })),
|
|
260
|
+
"Run run:abc waits for gate deploy and request 12:c1.",
|
|
261
|
+
);
|
|
262
|
+
});
|