File size: 11,508 Bytes
51c026d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import { type AssistantMessageFrame, createModels } from "@earendil-works/pi-ai";
import { afterEach, describe, expect, it, vi } from "vitest";
import type { WatchHandle } from "../../../src/harness/agent-harness.ts";
import { DEFAULT_COMPACTION_SETTINGS } from "../../../src/harness/compaction/compaction.ts";
import { BACKGROUND_CONTEXT, type Context, withAbortSignal } from "../../../src/harness/context.ts";
import { HookRegistry } from "../../../src/harness/hooks.ts";
import { Lane } from "../../../src/harness/runtime/lane.ts";
import { openFrameProgress, openToolProgress } from "../../../src/harness/runtime/progress.ts";
import { type Config, Drive, type LaneState as RuntimeLaneState } from "../../../src/harness/runtime/types.ts";
import { MemoryStorage } from "../../../src/harness/session/memory.ts";
import { StorageBackedSession } from "../../../src/harness/session/session.ts";
import {
	type AssistantEffectPendingOperation,
	type CheckpointOperation,
	type OperationState,
	operationScopeOf,
	type Session,
	type Write,
} from "../../../src/harness/session/types.ts";
import * as storedValues from "../../../src/harness/session/values.ts";
import { deferred } from "./test-utils.ts";

const sessions: Session[] = [];
const operationId = "operation";
const configuration = {
	model: { provider: "test", modelId: "model" },
	thinkingLevel: "off" as const,
	activeToolNames: [],
};

class FailingStorage extends MemoryStorage {
	failure: Error | undefined;
	beforeNextCommit: (() => Promise<void>) | undefined;

	override async commit(writes: Write[], context: Context) {
		const beforeCommit = this.beforeNextCommit;
		this.beforeNextCommit = undefined;
		await beforeCommit?.();
		if (this.failure === undefined) return super.commit(writes, context);
		const failure = this.failure;
		this.failure = undefined;
		throw failure;
	}
}

const runtimeConfig: Config<undefined> = {
	tools: [],
	resources: {},
	streamOptions: {},
	retryPolicy: { enabled: true, maxRetries: 3, baseDelayMs: 1_000 },
	compaction: DEFAULT_COMPACTION_SETTINGS,
	steeringMode: "all",
	followUpMode: "all",
	toolExecution: "parallel",
	toolContext: undefined,
	systemPrompt: undefined,
	toProviderMessages: () => [],
	entryProjectors: {},
};

function unusedWatch<T>(): WatchHandle<T> {
	throw new Error("watch is not used by progress tests");
}

function runScope() {
	return {
		control: { status: "running" as const },
		settings: {
			compaction: DEFAULT_COMPACTION_SETTINGS,
			steeringMode: "all" as const,
			followUpMode: "all" as const,
			toolExecution: "parallel" as const,
		},
		inbox: { steer: [], followUp: [], writes: [] },
		latestAssistantEntryId: null,
	};
}

function assistantEffectPending(responseEntryId: string): AssistantEffectPendingOperation {
	return {
		...runScope(),
		at: "assistant.effect_pending",
		generationContext: {
			stepId: "step",
			triggerEntryId: "trigger",
			configuration,
			streamOptions: {},
			retryPolicy: { maxAttempts: 2, baseDelayMs: 1, maxAgentDelayMs: 30_000 },
			overflowRecoveryUsed: false,
		},
		attempt: 1,
		responseEntryId,
		usageId: "usage",
		intendedOutputLimit: 100,
		contextWindow: 1_000,
	};
}

async function createFixture(
	state: OperationState,
	storage = new FailingStorage(),
): Promise<{ lane: Lane<undefined>; drive: Drive; storage: FailingStorage }> {
	const session = new StorageBackedSession(
		{ id: `progress-${sessions.length}`, createdAt: 1, storageVersion: 1 },
		storage,
	);
	sessions.push(session);
	const meta = {
		operationId,
		lane: "main",
		sourceTipId: null,
		startedAt: 1,
		intent: { kind: "run" as const, promptEntryIds: [] },
	};
	const projection: RuntimeLaneState = {
		tipId: null,
		configuration,
		inbox: [],
		lastOperationId: null,
		operation: { meta, state },
	};
	const lane = new Lane<undefined>(
		"main",
		session,
		createModels(),
		new HookRegistry(() => {}),
		projection,
		(cause) => (cause instanceof Error ? cause : new Error(String(cause))),
		() => Promise.resolve(),
		unusedWatch,
		() => runtimeConfig,
	);
	await session.mutate(
		(mutator) =>
			mutator.commit(
				[
					storedValues.setValue(storedValues.branchTip("main"), null),
					storedValues.setValue(storedValues.laneConfig("main"), configuration),
					storedValues.setValue(storedValues.laneState("main"), {
						currentOperationId: operationId,
						lastOperationId: null,
						inbox: [],
					}),
					storedValues.setValue(storedValues.operationMeta(operationId), meta),
					storedValues.setValue(storedValues.operationState(operationId), state),
				],
				BACKGROUND_CONTEXT,
			),
		BACKGROUND_CONTEXT,
	);
	const drive = new Drive({ operationId }, BACKGROUND_CONTEXT);
	lane.activeDrive = drive;
	return { lane, drive, storage };
}

afterEach(async () => {
	for (const session of sessions.splice(0)) await session.close(BACKGROUND_CONTEXT);
});

describe("runtime active drive ownership", () => {
	it("settles or rejects the shared completion without exposing owner controls through the gate", async () => {
		const settled = new Drive({ operationId }, BACKGROUND_CONTEXT);
		const outcome = { kind: "waiting", operationId, reason: "retry", notBefore: 10 } as const;
		settled.settle(outcome);
		settled.fail(new Error("late failure"));
		await expect(settled.completion).resolves.toBe(outcome);

		const failed = new Drive({ operationId }, BACKGROUND_CONTEXT);
		const error = new Error("drive failed");
		failed.fail(error);
		await expect(failed.completion).rejects.toBe(error);
		expect("beginAbort" in failed.gate).toBe(false);
	});

	it("strips invocation cancellation from pass context and owns policy and gate control", () => {
		const controller = new AbortController();
		const drive = new Drive(
			{ operationId, waitForRetry: true, pollDeferred: true },
			withAbortSignal(controller.signal, BACKGROUND_CONTEXT),
		);

		expect(drive.operationId).toBe(operationId);
		expect(drive.context.abortSignal).toBeUndefined();
		expect(drive.waitForRetry).toBe(true);
		expect(drive.deferredPermits).toBe(1);

		const closed = new Error("closed");
		drive.closeGate(closed);
		expect(drive.gate.signal.aborted).toBe(true);
		expect(() => drive.gate.admit(() => undefined)).toThrow(closed);
	});
});

describe("runtime progress channels", () => {
	it("enqueues assistant frames in order, seals admission, and exposes the settlement delete", async () => {
		const responseEntryId = "response";
		const { lane, drive } = await createFixture(assistantEffectPending(responseEntryId));
		const progress = openFrameProgress(lane, drive, responseEntryId);
		const frames: AssistantMessageFrame[] = [
			{ type: "text_delta", contentIndex: 0, delta: "a" },
			{ type: "text_delta", contentIndex: 0, delta: "b" },
		];

		progress.write(frames[0]!);
		progress.write(frames[1]!);
		progress.seal();
		progress.write({ type: "text_delta", contentIndex: 0, delta: "late" });
		await progress.drain();

		expect(
			(
				await lane.session.readList(
					storedValues.pendingAssistantFrames(operationId, responseEntryId),
					undefined,
					BACKGROUND_CONTEXT,
				)
			).map(({ value }) => value),
		).toEqual(frames);
	});

	it("declines a queued frame after the authoritative projection leaves its phase without control reads", async () => {
		const responseEntryId = "response";
		const { lane, drive, storage } = await createFixture(assistantEffectPending(responseEntryId));
		const progress = openFrameProgress(lane, drive, responseEntryId);
		const commitStarted = deferred();
		const releaseCommit = deferred();
		storage.beforeNextCommit = async () => {
			commitStarted.resolve();
			await releaseCommit.promise;
		};
		const getValue = vi.spyOn(storage, "getValue");
		const moving = lane.command((state) => {
			const operation = state.operation;
			if (operation === null) throw new Error("missing operation");
			const nextState: CheckpointOperation = {
				...operationScopeOf(operation.state),
				at: "checkpoint",
				continuation: { kind: "may_finish", includeFinalAssistant: true },
				triggerEntryId: responseEntryId,
			};
			return {
				kind: "commit",
				writes: [storedValues.setValue(storedValues.operationState(operationId), nextState)],
				next: { ...state, operation: { meta: operation.meta, state: nextState } },
				materialize: () => undefined,
			};
		}, BACKGROUND_CONTEXT);
		await commitStarted.promise;
		progress.write({ type: "text_delta", contentIndex: 0, delta: "late" });
		releaseCommit.resolve();
		await moving;
		await progress.drain();

		expect(
			await lane.session.readList(
				storedValues.pendingAssistantFrames(operationId, responseEntryId),
				undefined,
				BACKGROUND_CONTEXT,
			),
		).toEqual([]);
		expect(getValue).not.toHaveBeenCalled();
	});

	it("declines a queued frame after terminal projection publication", async () => {
		const responseEntryId = "response";
		const { lane, drive, storage } = await createFixture(assistantEffectPending(responseEntryId));
		const progress = openFrameProgress(lane, drive, responseEntryId);
		const commitStarted = deferred();
		const releaseCommit = deferred();
		storage.beforeNextCommit = async () => {
			commitStarted.resolve();
			await releaseCommit.promise;
		};
		const getValue = vi.spyOn(storage, "getValue");
		const ending = lane.command(
			(state) => ({
				kind: "commit",
				writes: [
					storedValues.deleteValue(storedValues.operationMeta(operationId)),
					storedValues.deleteValue(storedValues.operationState(operationId)),
					storedValues.setValue(storedValues.laneState("main"), {
						currentOperationId: null,
						lastOperationId: null,
						inbox: state.inbox,
					}),
				],
				next: { ...state, operation: null },
				materialize: () => undefined,
			}),
			BACKGROUND_CONTEXT,
		);
		await commitStarted.promise;
		progress.write({ type: "text_delta", contentIndex: 0, delta: "late" });
		releaseCommit.resolve();
		await ending;
		await progress.drain();

		expect(
			await lane.session.readList(
				storedValues.pendingAssistantFrames(operationId, responseEntryId),
				undefined,
				BACKGROUND_CONTEXT,
			),
		).toEqual([]);
		expect(getValue).not.toHaveBeenCalled();
	});

	it("replaces tool checkpoints in invocation order", async () => {
		const invocationId = "result";
		const { lane, drive } = await createFixture({
			...runScope(),
			at: "tools",
			batch: {
				assistantEntryId: "assistant",
				configuration,
				turnId: "turn",
				calls: [{ status: "effect_pending", sourceIndex: 0, resultEntryId: invocationId, replay: "safe" }],
			},
		} satisfies OperationState);
		const progress = openToolProgress(lane, drive, "turn", 0, invocationId);
		progress.write({ content: [{ type: "text", text: "first" }], details: {} });
		progress.write({ content: [{ type: "text", text: "second" }], details: {} });
		await progress.drain();
		expect(
			(await lane.session.getValue(storedValues.pendingToolOutput(operationId, invocationId), BACKGROUND_CONTEXT))
				?.value.content,
		).toEqual([{ type: "text", text: "second" }]);
	});

	it("retains the rejecting write promise so drain propagates commit failure", async () => {
		const responseEntryId = "response";
		const { lane, drive, storage } = await createFixture(assistantEffectPending(responseEntryId));
		const failure = new Error("frame commit failed");
		storage.failure = failure;
		const progress = openFrameProgress(lane, drive, responseEntryId);

		progress.write({ type: "text_delta", contentIndex: 0, delta: "lost" });

		await expect(progress.drain()).rejects.toBe(failure);
	});
});