@springbrand/agent-runtime 0.3.0-alpha.16 → 0.3.0-alpha.17

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.
@@ -1,646 +1,207 @@
1
1
  import {
2
- AgentHarness,
3
- type AgentMessage,
4
- type AgentHarness as PiAgentHarness,
5
- type AgentHarnessOptions,
6
- type AgentLane,
7
- type AgentToolResult,
8
- type DriveOutcome,
9
- type DriveOptions,
10
- type OpenOperation,
11
- type OperationRequest,
12
- type ThinkingLevel,
13
- } from "@earendil-works/pi-agent-core";
14
- import type { Context } from "@earendil-works/pi-agent-core/harness/context";
15
- import type { Entry, SettledAssistantMessage } from "@earendil-works/pi-agent-core/harness/session";
2
+ Harness, GenerationTask, ToolTask, AgentDoc,
3
+ } from "@earendil-works/pi-durable";
16
4
  import {
17
- isModelStreamStallMessage,
18
- isOpenRouterServerToolTimeout,
19
- isRecoverableAssistantError,
20
- } from "../models";
5
+ createRegistry, defineExtension, hook, section, watchEvents,
6
+ type ConversationRetryPolicy, type AgentEventStream, type CompactionPolicy, type Conversation,
7
+ type EntryId, type EntryRecord, type GenerationHooks, type Harness as NativeHarness,
8
+ type SettledSubmissionRecord, type Storage, type Submission,
9
+ type ToolHooks, type ToolRegistration,
10
+ } from "@earendil-works/pi-durable";
11
+ import type { Context } from "@earendil-works/chord";
12
+ import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
13
+ import type { Api, Message, Model, Models, ModelThinkingLevel, UserMessage } from "@earendil-works/pi-ai";
14
+ import { RuntimeConversation } from "../session";
21
15
  import { projectToolOutputForModel } from "../context/memory/budget/gate";
22
- import type { ArtifactRef } from "../lib/artifacts";
23
16
 
24
17
  export interface SessionHarnessLimits {
25
- /** Complete model turns committed before one Worker instance releases. */
26
18
  modelTurnsPerSlice?: number;
27
- /** Complete working turns allowed before the no-tool final response. */
28
19
  maxModelTurns?: number;
29
- /** Maximum text turns allowed after the working or Tool failure budget. */
30
- maxWrapUpModelTurns?: number;
31
20
  wrapUpInstruction?: string;
32
21
  }
33
-
34
- export interface SessionHarnessOptions<TContext extends object | undefined>
35
- extends AgentHarnessOptions<TContext> {
36
- limits?: SessionHarnessLimits;
37
- modelToolResultExemptions?: readonly string[];
38
- /** Base provider Session identity, retained except after a server Tool timeout. */
39
- modelSessionId?: string;
40
- /**
41
- * Compiled tools omitted from the initial active set. A later successful
42
- * result may add them through {@link SessionHarnessOptions.toolsToActivate}.
43
- */
22
+ export interface SessionHarnessOptions {
23
+ storage: Storage | Promise<Storage>;
24
+ models: Models;
25
+ model: Model<Api>;
26
+ thinkingLevel?: ModelThinkingLevel;
27
+ systemPrompt?: string;
28
+ tools?: readonly ToolRegistration<any, any>[];
29
+ activeToolNames?: readonly string[];
44
30
  onDemandToolNames?: readonly string[];
45
- /**
46
- * Names to add with `setActiveTools` from `after_tool`. That hook runs
47
- * before the next generation snapshots `activeToolNames`; Pi then writes
48
- * the difference as transcript `toolsAdded`. Do not call `setActiveTools`
49
- * from `tool_end`: event delivery is serialized and would deadlock.
50
- */
51
- toolsToActivate?: (
52
- toolName: string,
53
- result: AgentToolResult<unknown>,
54
- isError: boolean,
55
- ) => readonly string[];
56
- }
57
-
58
- export type SessionHarnessRunResult = DriveOutcome | {
59
- kind: "released";
60
- operationId: string;
61
- reason: "slice" | "budget" | "tool-failure" | "approval" | "interaction";
62
- };
63
-
64
- const DEFAULT_MODEL_TURNS_PER_SLICE = 30;
65
- const DEFAULT_MAX_MODEL_TURNS = 300;
66
- const DEFAULT_MAX_WRAP_UP_MODEL_TURNS = 2;
67
- const RECOVERY_MAX_FAILURES = 5;
68
- const STALL_MAX_FAILURES = 3;
69
- const SERVER_TOOL_TIMEOUT_MAX_FAILURES = 2;
70
- const DEFAULT_WRAP_UP_INSTRUCTION =
71
- "Finish this request now without using tools. Summarize the completed work and any remaining limitation.";
72
- const TOOL_FAILURE_WRAP_UP_INSTRUCTION =
73
- "This turn reached its tool failure limit. Stop calling tools now. Reply with a final message that explains what failed and the exact next step the user should take. Do not start new work.";
74
- const MAX_TOOL_RETRIES = 3;
75
- const TURN_FAILURE_CEILING = 8;
76
- const ARTIFACT_MANIFEST_MAX_CHARS = 12 * 1024;
77
-
78
- function canonicalJson(value: unknown): string {
79
- if (Array.isArray(value)) {
80
- return `[${value.map(canonicalJson).join(",")}]`;
81
- }
82
- if (value !== null && typeof value === "object") {
83
- return `{${Object.entries(value)
84
- .filter(([, item]) => item !== undefined)
85
- .sort(([left], [right]) => left.localeCompare(right))
86
- .map(([key, item]) => `${JSON.stringify(key)}:${canonicalJson(item)}`)
87
- .join(",")}}`;
88
- }
89
- return JSON.stringify(value) ?? "null";
90
- }
91
-
92
- function toolCallKey(toolName: string, args: unknown): string {
93
- return `${toolName}\0${canonicalJson(args)}`;
94
- }
95
-
96
- function uniqueToolNames(names: readonly string[]): string[] {
97
- return [...new Set(names)];
31
+ modelToolResultExemptions?: readonly string[];
32
+ toolsToActivate?: (name: string, result: import("@earendil-works/pi-agent-core").AgentToolResult<unknown>, isError: boolean) => readonly string[];
33
+ compaction?: Partial<CompactionPolicy>;
34
+ streamOptions?: { maxRetries?: number };
35
+ retry?: Partial<ConversationRetryPolicy>;
36
+ toolExecution?: "parallel" | "sequential";
37
+ limits?: SessionHarnessLimits;
38
+ beforeTool?: ToolHooks["beforeTool"];
39
+ afterResponse?: GenerationHooks["afterResponse"];
98
40
  }
99
41
 
100
- function projectToolResultsForModel(
101
- messages: readonly AgentMessage[],
102
- exemptions: ReadonlySet<string>,
103
- ): AgentMessage[] {
104
- let changed = false;
105
- const projected = messages.map((message) => {
106
- if (
107
- message.role !== "toolResult" ||
108
- exemptions.has(message.toolName)
109
- ) return message;
110
- const payload = { content: message.content, details: message.details };
111
- const next = projectToolOutputForModel(payload) as typeof payload;
112
- if (next === payload) return message;
113
- changed = true;
114
- return { ...message, content: next.content, details: next.details };
115
- });
116
- return changed ? projected : [...messages];
42
+ /** Host terminal projection; native Submission records remain authoritative. */
43
+ export interface SessionRunOutcome {
44
+ status: "completed" | "failed" | "aborted" | "declined";
45
+ endedAt: number;
46
+ error?: { code: string; message: string };
117
47
  }
118
-
119
- function artifactRefFromMessage(message: AgentMessage): ArtifactRef | undefined {
120
- if (message.role !== "toolResult" || message.details === null ||
121
- typeof message.details !== "object" ||
122
- (message.details as { kind?: unknown }).kind !== "artifact_ref") {
123
- return undefined;
124
- }
125
- const ref = message.details as Partial<ArtifactRef>;
126
- if (typeof ref.path !== "string" || typeof ref.hash !== "string" ||
127
- typeof ref.bytes !== "number" || typeof ref.preview !== "string" ||
128
- typeof ref.note !== "string") return undefined;
48
+ export type SessionHarnessRunResult =
49
+ | { kind: "settled"; outcome: SessionRunOutcome }
50
+ | { kind: "released"; operationId: string; reason: "slice" | "approval" | "interaction" };
51
+ export interface SessionRunRequest { operationId: string; prompt?: UserMessage | string; }
52
+ export function submissionOutcome(record: SettledSubmissionRecord): SessionRunOutcome {
129
53
  return {
130
- kind: "artifact_ref",
131
- artifactId: typeof ref.artifactId === "string"
132
- ? ref.artifactId
133
- : `artifact_${ref.hash.slice(0, 24)}`,
134
- path: ref.path,
135
- bytes: ref.bytes,
136
- hash: ref.hash,
137
- workspaceRevision: typeof ref.workspaceRevision === "string"
138
- ? ref.workspaceRevision
139
- : "unknown",
140
- availability: ref.availability === "unavailable"
141
- ? "unavailable"
142
- : "available",
143
- preview: ref.preview,
144
- note: ref.note,
54
+ status: record.status === "done" ? "completed" : record.reason === "aborted" ? "aborted" : "failed",
55
+ endedAt: Date.now(),
56
+ ...(record.status === "unanswered" ? { error: { code: record.reason, message: typeof record.detail === "string" ? record.detail : record.reason } } : {}),
145
57
  };
146
58
  }
147
59
 
148
- function artifactManifestMessage(
149
- refs: ReadonlyMap<string, ArtifactRef>,
150
- ): AgentMessage | undefined {
151
- if (refs.size === 0) return undefined;
152
- const lines = [
153
- "<workspace_artifact_manifest>",
154
- "These are lightweight references to prior Tool output. Do not treat previews as complete. Read an available artifact with the Workspace readFile/readFilePage tool only when the current request needs its evidence.",
155
- ];
156
- let omitted = 0;
157
- for (const ref of refs.values()) {
158
- const preview = ref.preview.replace(/[\r\n]+/g, " ").slice(0, 160);
159
- const line = [
160
- `path=${ref.path}`,
161
- ...(ref.availability === "unavailable" ? ["unavailable"] : []),
162
- `preview=${preview}`,
163
- ].join(" ");
164
- if ([...lines, line, "</workspace_artifact_manifest>"].join("\n").length > ARTIFACT_MANIFEST_MAX_CHARS) {
165
- omitted += 1;
166
- continue;
167
- }
168
- lines.push(line);
169
- }
170
- if (omitted > 0) lines.push(`omitted=${omitted} additional artifact references due to manifest budget`);
171
- lines.push("</workspace_artifact_manifest>");
172
- return {
173
- role: "user",
174
- content: lines.join("\n"),
175
- timestamp: 1,
176
- };
177
- }
178
-
179
- function deferred<T>(): {
180
- promise: Promise<T>;
181
- resolve(value: T): void;
182
- } {
183
- let resolve!: (value: T) => void;
184
- const promise = new Promise<T>((done) => {
185
- resolve = done;
186
- });
187
- return { promise, resolve };
188
- }
189
-
190
- function positiveInteger(value: number | undefined, fallback: number): number {
191
- const resolved = value ?? fallback;
192
- if (!Number.isInteger(resolved) || resolved <= 0) {
193
- throw new Error("Harness model turn limits must be positive integers");
194
- }
195
- return resolved;
196
- }
197
-
198
- /**
199
- * The only adapter that owns a live Pi AgentHarness for a Session.
200
- *
201
- * Submission and Cloudflare lifecycle code may accept, drive, abort, watch, or
202
- * close through this object. It deliberately does not know how Host config or
203
- * business repositories are loaded.
204
- */
205
- export class SessionHarness<TContext extends object | undefined> {
206
- private _openOperation: OpenOperation | null;
207
- private readonly model: AgentHarnessOptions<TContext>["model"];
208
- private readonly thinkingLevel: ThinkingLevel;
209
- private readonly modelTurnsPerSlice: number;
210
- private readonly maxModelTurns: number;
211
- private readonly maxWrapUpModelTurns: number;
212
- private readonly modelSessionId: string | undefined;
213
- private readonly wrapUpInstruction: string;
214
- private readonly configuredToolNames: string[];
215
- private readonly compiledToolNames: ReadonlySet<string>;
216
- private readonly onDemandToolNames: ReadonlySet<string>;
217
- private readonly toolsToActivate:
218
- | ((
219
- toolName: string,
220
- result: AgentToolResult<unknown>,
221
- isError: boolean,
222
- ) => readonly string[])
223
- | undefined;
224
- private readonly modelToolResultExemptions: ReadonlySet<string>;
225
- private readonly toolFailures = new Map<string, number>();
226
- private totalToolFailures = 0;
227
- private toolFailureWrapUp = false;
228
- private toolFailureWrapUpAt: number | undefined;
229
- private operationStartTip: string | null = null;
60
+ /** The host binds its ports; Pi owns generation, Tool tasks, waiting and recovery. */
61
+ export class SessionHarness {
62
+ private submission?: Submission;
63
+ private requestId?: string;
64
+ private release!: (reason: "approval" | "interaction" | "slice") => void;
230
65
  private turnsAtOpen = 0;
231
- private wrappingUp = false;
232
- private releaseReason:
233
- | "slice"
234
- | "budget"
235
- | "tool-failure"
236
- | "approval"
237
- | "interaction"
238
- | undefined;
239
- private readonly releaseRequest = deferred<"approval" | "interaction">();
240
- private readonly activeTools = new Set<string>();
241
- private readonly toolsIdle = new Set<() => void>();
242
- private readonly artifactRefs = new Map<string, ArtifactRef>();
243
-
66
+ private readonly released = new Promise<"approval" | "interaction" | "slice">((resolve) => { this.release = resolve; });
244
67
  private constructor(
245
- readonly harness: PiAgentHarness<TContext>,
246
- readonly lane: AgentLane,
247
- openOperation: OpenOperation | null,
248
- options: SessionHarnessOptions<TContext>,
249
- ) {
250
- this._openOperation = openOperation;
251
- this.model = options.model;
252
- this.thinkingLevel = options.thinkingLevel ?? "off";
253
- this.modelTurnsPerSlice = positiveInteger(
254
- options.limits?.modelTurnsPerSlice,
255
- DEFAULT_MODEL_TURNS_PER_SLICE,
256
- );
257
- this.maxModelTurns = positiveInteger(
258
- options.limits?.maxModelTurns,
259
- DEFAULT_MAX_MODEL_TURNS,
260
- );
261
- this.maxWrapUpModelTurns = positiveInteger(
262
- options.limits?.maxWrapUpModelTurns,
263
- DEFAULT_MAX_WRAP_UP_MODEL_TURNS,
264
- );
265
- this.modelSessionId = options.modelSessionId;
266
- this.wrapUpInstruction = options.limits?.wrapUpInstruction ??
267
- DEFAULT_WRAP_UP_INSTRUCTION;
268
- this.configuredToolNames = options.activeToolNames ??
269
- (options.tools ?? []).map(({ name }) => name);
270
- this.compiledToolNames = new Set((options.tools ?? []).map(({ name }) => name));
271
- this.onDemandToolNames = new Set(options.onDemandToolNames ?? []);
272
- this.toolsToActivate = options.toolsToActivate;
273
- this.modelToolResultExemptions = new Set(
274
- options.modelToolResultExemptions ?? [],
275
- );
276
- }
277
-
278
- private async operationEntries(context: Context): Promise<Entry[]> {
279
- // Stop boundaries follow traversal order; walk back from the current tip.
280
- const entries = await this.lane.findEntries({
281
- ...(this.operationStartTip === null ? {} : { stopAtId: this.operationStartTip }),
282
- type: "message",
283
- order: "newestFirst",
68
+ readonly harness: NativeHarness,
69
+ public conversation: Conversation,
70
+ readonly storage: Storage,
71
+ private readonly options: SessionHarnessOptions,
72
+ ) {}
73
+
74
+ get openOperation(): { operationId: string } | null {
75
+ return this.submission && this.requestId ? { operationId: this.requestId } : null;
76
+ }
77
+
78
+ static async open(options: SessionHarnessOptions, context: Context): Promise<SessionHarness> {
79
+ const storage = await options.storage;
80
+ const registry = createRegistry<ToolRegistration<any, any>>();
81
+ let runtime!: SessionHarness;
82
+ const exemptions = new Set(options.modelToolResultExemptions ?? []);
83
+ const extension = defineExtension({
84
+ name: "springbrand.runtime", tools: options.tools ?? [],
85
+ sections: [section("system", () => options.systemPrompt, { tag: false })],
86
+ hooks: [
87
+ hook(GenerationTask, {
88
+ beforeRequest: ({ messages }) => ({ messages: messages.map((message): Message => {
89
+ if (message.role !== "toolResult" || exemptions.has(message.toolName)) return message;
90
+ const next = projectToolOutputForModel({ content: message.content, details: message.details }) as Pick<typeof message, "content" | "details">;
91
+ return { ...message, ...next };
92
+ }) }),
93
+ afterResponse: options.afterResponse,
94
+ afterTools: async (_assistant, _results, _api, hookContext) => {
95
+ const entries = await runtime.entries(hookContext);
96
+ const receipt = await runtime.submission?.status(hookContext);
97
+ const owned = receipt?.entry ? entries.filter((entry) => entry.id >= receipt.entry!) : entries;
98
+ const turns = owned.filter((entry) => entry.kind === "pi.assistant").length;
99
+ const total = entries.filter((entry) => entry.kind === "pi.assistant").length;
100
+ if (total - runtime.turnsAtOpen >= (options.limits?.modelTurnsPerSlice ?? 30)) runtime.release("slice");
101
+ if (turns >= (options.limits?.maxModelTurns ?? 300)) await runtime.conversation.configure({ tools: [], instructions: options.limits?.wrapUpInstruction ?? "Finish this request now without using tools. Summarize the completed work and any remaining limitation." }, hookContext);
102
+ },
103
+ }),
104
+ hook(ToolTask, {
105
+ beforeTool: options.beforeTool,
106
+ afterTool: async (call, result) => {
107
+ const names = options.toolsToActivate?.(call.name, { content: [...result.content ?? []], details: result.details }, result.isError === true) ?? [];
108
+ return names.length ? { ...result, control: { ...result.control, addTools: names } } : undefined;
109
+ },
110
+ }),
111
+ ],
112
+ });
113
+ registry.install(extension);
114
+ const harness = await Harness.open(storage, {
115
+ models: options.models, registry,
116
+ settings: { stream: options.streamOptions, compaction: options.compaction, toolExecution: options.toolExecution, retry: options.retry ?? { maxRetries: 4 } },
284
117
  }, context);
285
- return entries.filter(({ id }) => id !== this.operationStartTip).reverse();
286
- }
287
-
288
- private async restoreToolGovernance(context: Context): Promise<void> {
289
- this.toolFailures.clear();
290
- this.totalToolFailures = 0;
291
- this.toolFailureWrapUp = false;
292
- this.toolFailureWrapUpAt = undefined;
293
- let turns = 0;
294
- for (const entry of await this.operationEntries(context)) {
295
- if (entry.type !== "message") continue;
296
- const message = entry.message;
297
- if (message.role === "assistant") {
298
- turns += 1;
299
- continue;
300
- }
301
- this.recordCanonicalToolOutcome(entry);
302
- if (this.toolFailureWrapUp) this.toolFailureWrapUpAt ??= turns;
303
- }
304
- }
305
-
306
- private recordCanonicalToolOutcome(entry: Entry): void {
307
- if (entry.type !== "message" || entry.message.role !== "toolResult") return;
308
- const message = entry.message;
309
- if (entry.toolExecution?.status === "executed") {
310
- this.recordToolOutcome(message.toolName, entry.toolExecution.args, message.isError);
311
- } else if (entry.toolExecution?.status === "blocked" && entry.toolExecution.args !== undefined) {
312
- const key = toolCallKey(message.toolName, entry.toolExecution.args);
313
- if ((this.toolFailures.get(key) ?? 0) >= MAX_TOOL_RETRIES) this.toolFailureWrapUp = true;
314
- }
315
- }
316
-
317
- private recordToolOutcome(toolName: string, args: unknown, isError: boolean): void {
318
- const key = toolCallKey(toolName, args);
319
- if (!isError) {
320
- this.toolFailures.delete(key);
321
- return;
118
+ const pointer = await harness.snapshot(RuntimeConversation, context);
119
+ const conversation = pointer?.id ? await harness.conversation(pointer.id, context) : await harness.root(context);
120
+ if (!conversation) { await harness.close(context); throw new Error("The persisted Pi conversation is missing"); }
121
+ if (!pointer?.id) await harness.commit(async (tx) => { (await tx.doc(RuntimeConversation)).id = conversation.id; }, context);
122
+ runtime = new SessionHarness(harness, conversation, storage, options);
123
+ runtime.turnsAtOpen = (await runtime.entries(context)).filter((entry) => entry.kind === "pi.assistant").length;
124
+ const unfinished = (await harness.inspect(context)).submissions.filter((record) => record.conversationId === conversation.id && record.type === "input" && !record.requestId?.startsWith("steer:") && (record.status === "placed" || record.status === "queued"));
125
+ if (unfinished.length > 1) { await harness.close(context); throw new Error("Multiple host runs are active in one Pi conversation"); }
126
+ if (unfinished[0]) {
127
+ runtime.submission = await harness.submission(unfinished[0].id, context);
128
+ runtime.requestId = unfinished[0].requestId;
322
129
  }
323
- this.toolFailures.set(key, (this.toolFailures.get(key) ?? 0) + 1);
324
- this.totalToolFailures += 1;
325
- if (this.totalToolFailures >= TURN_FAILURE_CEILING) this.toolFailureWrapUp = true;
326
- }
327
-
328
- private async shouldRetry(
329
- message: SettledAssistantMessage,
330
- context: Context,
331
- recovery: boolean,
332
- ): Promise<boolean> {
333
- const stalled = isModelStreamStallMessage(message.errorMessage);
334
- const serverToolTimeout = isOpenRouterServerToolTimeout(message);
335
- if (!recovery && !stalled && !serverToolTimeout && !isRecoverableAssistantError(message)) return false;
336
- const failures = 1 + (await this.operationEntries(context)).filter((entry) =>
337
- entry.type === "message" && entry.message.role === "assistant" && entry.message.stopReason === "error"
338
- ).length;
339
- return failures < (stalled ? STALL_MAX_FAILURES
340
- : serverToolTimeout ? SERVER_TOOL_TIMEOUT_MAX_FAILURES : RECOVERY_MAX_FAILURES);
341
- }
342
-
343
- private async shouldFinishRun(context: Context): Promise<boolean> {
344
- const wrapUpAt = Math.min(this.maxModelTurns, this.toolFailureWrapUpAt ?? Infinity);
345
- return await this.completedModelTurns(context) >= wrapUpAt + this.maxWrapUpModelTurns;
346
- }
347
-
348
- get openOperation(): OpenOperation | null {
349
- return this._openOperation;
130
+ return runtime;
350
131
  }
351
132
 
352
- static async open<TContext extends object | undefined>(
353
- options: SessionHarnessOptions<TContext>,
354
- context: Context,
355
- ): Promise<SessionHarness<TContext>> {
356
- const {
357
- limits: _limits,
358
- modelToolResultExemptions: _modelToolResultExemptions,
359
- modelSessionId: _modelSessionId,
360
- onDemandToolNames: _onDemandToolNames,
361
- toolsToActivate: _toolsToActivate,
362
- ...harnessOptions
363
- } = options;
364
- let runtime!: SessionHarness<TContext>;
365
- const { harness, open } = await AgentHarness.create({
366
- ...harnessOptions,
367
- retry: options.retry ?? { enabled: true, maxRetries: RECOVERY_MAX_FAILURES - 1, baseDelayMs: 1_000 },
368
- recoveryPolicy: async (event, policyContext) =>
369
- await runtime.shouldRetry(event.message, policyContext, event.recovery) &&
370
- (options.recoveryPolicy === undefined || await options.recoveryPolicy(event, policyContext)),
371
- shouldFinishRun: async (event, policyContext) =>
372
- await runtime.shouldFinishRun(policyContext) ||
373
- (options.shouldFinishRun !== undefined && await options.shouldFinishRun(event, policyContext)),
133
+ async submissionFor(requestId: string, context: Context) {
134
+ return this.storage.submissionByRequest(this.conversation.id, requestId, context);
135
+ }
136
+ async entries(context: Context = BACKGROUND_CONTEXT): Promise<EntryRecord[]> {
137
+ const entries: EntryRecord[] = [];
138
+ let cursor;
139
+ do {
140
+ const page = await this.conversation.entries({ order: "ascending" }, 200, cursor, context);
141
+ entries.push(...page.items); cursor = page.next;
142
+ } while (cursor);
143
+ return entries;
144
+ }
145
+ watch(context: Context): Promise<AgentEventStream> { return watchEvents(this.harness, this.conversation.id, context); }
146
+ requestHumanRelease(reason: "approval" | "interaction"): void { this.release(reason); }
147
+ async fork(at: EntryId | null, context: Context): Promise<void> {
148
+ await this.conversation.abort(context);
149
+ this.submission = undefined;
150
+ this.requestId = undefined;
151
+ const created = await this.harness.commit(async (tx) => {
152
+ const fork = at === null ? await tx.createConversation({ ownership: { kind: "ownerless" } }) : await tx.forkConversation(this.conversation.id, at, { ownership: { kind: "ownerless" } });
153
+ (await tx.doc(RuntimeConversation)).id = fork.id;
154
+ return fork;
374
155
  }, context);
375
- const foreign = open.find((operation) => operation.lane !== "main");
376
- if (foreign) {
377
- await harness.close(context);
378
- throw new Error(`Unexpected open Harness lane: ${foreign.lane}`);
379
- }
380
- if (open.length > 1) {
381
- await harness.close(context);
382
- throw new Error("Session has multiple open operations on the main lane");
156
+ this.conversation = (await this.harness.conversation(created.id, context))!;
157
+ this.turnsAtOpen = (await this.entries(context)).filter((entry) => entry.kind === "pi.assistant").length;
158
+ }
159
+ async admit(request: SessionRunRequest, context: Context): Promise<void> {
160
+ if (this.submission && this.requestId !== request.operationId) throw new Error("A native Pi Submission is already active");
161
+ const existing = await this.submissionFor(request.operationId, context);
162
+ if (existing) {
163
+ this.requestId = request.operationId;
164
+ this.submission = await this.harness.submission(existing.id, context);
165
+ } else {
166
+ if (request.prompt === undefined) throw new Error("A new native Pi Submission needs input");
167
+ const names = new Set(this.options.activeToolNames ?? this.options.tools?.map((tool) => tool.name) ?? []);
168
+ const previous = await this.harness.snapshot(AgentDoc, this.conversation.id, context);
169
+ if (Array.isArray(previous?.tools)) for (const name of previous.tools) if (this.options.onDemandToolNames?.includes(name)) names.add(name);
170
+ await this.conversation.configure({ model: { provider: this.options.model.provider, modelId: this.options.model.id }, thinkingLevel: this.options.thinkingLevel ?? "off", tools: this.options.tools?.filter((tool) => names.has(tool.name)) ?? [] }, context);
171
+ this.requestId = request.operationId;
172
+ this.submission = await this.conversation.submit({ type: "input", requestId: request.operationId, content: typeof request.prompt === "string" ? request.prompt : request.prompt.content, whenBusy: "reject" }, context);
383
173
  }
384
- runtime = new SessionHarness(
385
- harness,
386
- await harness.lane("main", context),
387
- open[0] ?? null,
388
- options,
389
- );
390
- await runtime.install(context);
391
- return runtime;
392
174
  }
393
-
394
- private async install(context: Context): Promise<void> {
395
- for (const entry of await this.lane.findEntries({ type: "message", order: "oldestFirst" }, context)) {
396
- if (entry.type === "message") {
397
- const ref = artifactRefFromMessage(entry.message);
398
- if (ref) this.artifactRefs.set(ref.artifactId, ref);
399
- }
400
- }
401
- if (this._openOperation) {
402
- const watch = await this.lane.watch(context);
403
- this.operationStartTip = watch.snapshot.operation?.fromTipId ?? null;
404
- watch.unsubscribe();
405
- await this.restoreToolGovernance(context);
406
- this.turnsAtOpen = await this.completedModelTurns(context);
407
- if (this.turnsAtOpen >= this.maxModelTurns) {
408
- this.wrappingUp = true;
409
- }
410
- if (this.wrappingUp || this.toolFailureWrapUp) {
411
- await this.lane.setActiveTools([], context);
412
- }
413
- }
414
- this.harness.hooks.on("before_request", async (event, requestContext) => {
415
- if (event.step !== "assistant") return;
416
- const resets = (await this.operationEntries(requestContext)).filter((entry) =>
417
- entry.type === "message" && entry.message.role === "assistant" && isOpenRouterServerToolTimeout(entry.message)
418
- ).length;
419
- if (resets > 0) {
420
- return { streamOptions: { sessionId: `${this.modelSessionId ?? event.runId}:server-tool-recovery:${resets}` } };
421
- }
422
- if (this.modelSessionId !== undefined) return { streamOptions: { sessionId: this.modelSessionId } };
423
- });
424
- this.harness.hooks.on("transform_context", (event) => {
425
- const messages = projectToolResultsForModel(
426
- event.messages,
427
- this.modelToolResultExemptions,
428
- );
429
- const manifest = artifactManifestMessage(this.artifactRefs);
430
- const instruction = this.toolFailureWrapUp
431
- ? TOOL_FAILURE_WRAP_UP_INSTRUCTION
432
- : this.wrappingUp
433
- ? this.wrapUpInstruction
434
- : undefined;
435
- const contextMessages = manifest ? [...messages, manifest] : messages;
436
- return {
437
- messages: instruction
438
- ? [...contextMessages, {
439
- role: "user" as const,
440
- content: instruction,
441
- timestamp: 1,
442
- }]
443
- : contextMessages,
444
- };
445
- });
446
- this.harness.hooks.on("before_tool", (event) => {
447
- if (this.toolFailureWrapUp) {
448
- return {
449
- block: {
450
- reason: "This turn reached its tool failure limit. Stop calling tools now.",
451
- terminate: true,
452
- },
453
- };
454
- }
455
- if (
456
- (this.toolFailures.get(toolCallKey(event.toolName, event.args)) ?? 0) <
457
- MAX_TOOL_RETRIES
458
- ) return;
459
- this.toolFailureWrapUp = true;
460
- return {
461
- block: {
462
- reason:
463
- `This exact ${event.toolName} call has already failed ${MAX_TOOL_RETRIES} times — change the input or approach instead of retrying it verbatim.`,
464
- },
465
- };
466
- });
467
- this.harness.events.on("entry_added", (event) => {
468
- if (event.lane !== this.lane.name) return;
469
- this.recordCanonicalToolOutcome(event.entry);
470
- if (event.entry.type === "message") {
471
- const ref = artifactRefFromMessage(event.entry.message);
472
- if (ref) this.artifactRefs.set(ref.artifactId, ref);
473
- }
474
- });
475
- this.harness.events.on("turn_end", async (_event, eventContext) => {
476
- const total = await this.completedModelTurns(eventContext);
477
- if (this.toolFailureWrapUp && this.toolFailureWrapUpAt === undefined) {
478
- this.toolFailureWrapUpAt = total;
479
- this.releaseReason = "tool-failure";
480
- } else if (this.wrappingUp || this.toolFailureWrapUp) {
481
- return;
482
- } else if (total >= this.maxModelTurns) {
483
- this.releaseReason = "budget";
484
- } else if (total - this.turnsAtOpen >= this.modelTurnsPerSlice) {
485
- this.releaseReason = "slice";
486
- } else {
487
- return;
488
- }
489
- await this.harness.close(eventContext);
490
- });
491
- this.harness.hooks.on("after_tool", async (event, eventContext) => {
492
- await this.activateToolsFromResult({
493
- toolName: event.toolName,
494
- result: {
495
- content: event.content,
496
- details: event.details,
497
- },
498
- isError: event.isError,
499
- }, eventContext);
500
- return undefined;
501
- });
502
- this.harness.events.on("tool_start", (event) => {
503
- this.activeTools.add(event.toolCallId);
504
- });
505
- this.harness.events.on("tool_end", (event) => {
506
- this.activeTools.delete(event.toolCallId);
507
- if (this.activeTools.size === 0) {
508
- for (const resolve of this.toolsIdle) resolve();
509
- this.toolsIdle.clear();
510
- }
175
+ async run(request: SessionRunRequest | undefined, context: Context): Promise<SessionHarnessRunResult> {
176
+ if (request) await this.admit(request, context);
177
+ if (!this.submission || !this.requestId) throw new Error("No native Pi Submission to resume");
178
+ const waiting = this.submission.wait(context).then(async (record) => {
179
+ // One input can be answered while Pi is still working on a later steer.
180
+ await this.conversation.waitForIdle(context);
181
+ let cursor;
182
+ do {
183
+ const page = await this.storage.scanSubmissions({ conversationId: this.conversation.id, order: "descending" }, 200, cursor, context);
184
+ for (const next of page.items) {
185
+ if (next.id <= record.id) return record;
186
+ if (next.type === "input" && next.requestId?.startsWith("steer:") && (next.status === "done" || next.status === "unanswered")) return next as SettledSubmissionRecord;
187
+ }
188
+ cursor = page.next;
189
+ } while (cursor);
190
+ return record;
511
191
  });
512
- }
513
-
514
- /** Ask the active drive to detach after already-started parallel Tools settle. */
515
- requestHumanRelease(reason: "approval" | "interaction"): void {
516
- this.releaseRequest.resolve(reason);
517
- }
518
-
519
- private async waitForToolsToSettle(): Promise<void> {
520
- if (this.activeTools.size === 0) return;
521
- await new Promise<void>((resolve) => this.toolsIdle.add(resolve));
522
- }
523
-
524
- private async activeToolsForNewOperation(context: Context): Promise<string[]> {
525
- const previous = await this.lane.getActiveTools(context);
526
- const keepOnDemand = previous.filter((name) =>
527
- this.onDemandToolNames.has(name) && this.compiledToolNames.has(name)
528
- );
529
- return uniqueToolNames([...this.configuredToolNames, ...keepOnDemand]);
530
- }
531
-
532
- private async activateToolsFromResult(
533
- event: {
534
- toolName: string;
535
- result: AgentToolResult<unknown>;
536
- isError: boolean;
537
- },
538
- context: Context,
539
- ): Promise<void> {
540
- const requested = this.toolsToActivate?.(
541
- event.toolName,
542
- event.result,
543
- event.isError,
544
- ) ?? [];
545
- const allowed = requested.filter((name) => this.compiledToolNames.has(name));
546
- if (allowed.length === 0) return;
547
- const current = await this.lane.getActiveTools(context);
548
- if (allowed.every((name) => current.includes(name))) return;
549
- await this.lane.setActiveTools(uniqueToolNames([...current, ...allowed]), context);
550
- }
551
-
552
- private async completedModelTurns(context: Context): Promise<number> {
553
- return (await this.operationEntries(context)).filter((entry) =>
554
- entry.type === "message" && entry.message.role === "assistant"
555
- ).length;
556
- }
557
-
558
- async run(
559
- request: OperationRequest | undefined,
560
- context: Context,
561
- driveOptions: Omit<DriveOptions, "operationId"> = {},
562
- ): Promise<SessionHarnessRunResult> {
563
- let operationId = this.openOperation?.operationId;
564
- if (request) {
565
- if (operationId) {
566
- throw new Error(`Harness operation ${operationId} is already open`);
567
- }
568
- this.operationStartTip = await this.lane.getTipId(context);
569
- this.turnsAtOpen = 0;
570
- this.wrappingUp = false;
571
- this.toolFailures.clear();
572
- this.totalToolFailures = 0;
573
- this.toolFailureWrapUp = false;
574
- this.toolFailureWrapUpAt = undefined;
575
- this.releaseReason = undefined;
576
- // Pi restores persisted lane settings; current settings apply only to new operations.
577
- await this.lane.setModel({
578
- provider: this.model.provider,
579
- modelId: this.model.id,
580
- }, context);
581
- await this.lane.setThinkingLevel(this.thinkingLevel, context);
582
- await this.lane.setActiveTools(
583
- await this.activeToolsForNewOperation(context),
584
- context,
585
- );
586
- const admission = await this.lane.accept(request, context);
587
- if (!admission.ok) throw admission.error;
588
- operationId = admission.value.operationId;
589
- this._openOperation = {
590
- lane: "main",
591
- operationId,
592
- kind: admission.value.kind,
593
- startedAt: admission.value.startedAt,
594
- };
595
- }
596
- if (!operationId) throw new Error("No Harness operation to drive");
597
- try {
598
- const driving = this.lane.drive({ ...driveOptions, operationId }, context);
599
- const winner = await Promise.race([
600
- driving.then((result) => ({ kind: "drive" as const, result })),
601
- this.releaseRequest.promise.then((reason) => ({
602
- kind: "release" as const,
603
- reason,
604
- })),
605
- ]);
606
- if (winner.kind === "release") {
607
- this.releaseReason = winner.reason;
608
- await this.waitForToolsToSettle();
609
- await this.harness.close(context);
610
- await driving.catch(() => undefined);
611
- return { kind: "released", operationId, reason: winner.reason };
612
- }
613
- const result = winner.result;
614
- if (!result.ok) throw result.error;
615
- if (result.value.kind === "settled") {
616
- this._openOperation = null;
617
- }
618
- return result.value;
619
- } catch (error) {
620
- if (
621
- this.releaseReason && error instanceof Error &&
622
- error.name === "HarnessClosed"
623
- ) {
624
- return { kind: "released", operationId, reason: this.releaseReason };
625
- }
626
- throw error;
192
+ waiting.catch(() => {});
193
+ const result = await Promise.race([waiting.then((record) => ({ record })), this.released.then((reason) => ({ reason }))]);
194
+ if ("record" in result) {
195
+ this.submission = undefined;
196
+ return { kind: "settled", outcome: submissionOutcome(result.record) };
627
197
  }
628
- }
629
-
630
- async requestAbort(operationId: string, context: Context): Promise<boolean> {
631
- const result = await this.lane.requestAbort(operationId, context);
632
- if (!result.ok) throw result.error;
633
- return result.value.newlyRequested;
634
- }
635
-
636
- close(context: Context): Promise<void> {
637
- return this.harness.close(context);
638
- }
639
- }
640
-
641
- export function openSessionHarness<TContext extends object | undefined>(
642
- options: SessionHarnessOptions<TContext>,
643
- context: Context,
644
- ): Promise<SessionHarness<TContext>> {
645
- return SessionHarness.open(options, context);
198
+ // A pending approval is before intent. Let already-started parallel effects commit before detaching.
199
+ const executing = (await this.harness.inspect(context)).tasks.filter(({ record }) => record.kind === "pi.tool" && record.state.status === "running" && (record.state.checkpoint as { phase?: string }).phase === "execute");
200
+ await Promise.all(executing.map(({ record }) => this.harness.waitForTask(record.id, context)));
201
+ await this.harness.close(BACKGROUND_CONTEXT);
202
+ return { kind: "released", operationId: this.requestId, reason: result.reason };
203
+ }
204
+ async abort(context: Context): Promise<void> { await this.conversation.abort(context); }
205
+ async close(context: Context): Promise<void> { await this.harness.close(context); }
646
206
  }
207
+ export const openSessionHarness = SessionHarness.open;