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

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,290 @@
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
16
  import type { ArtifactRef } from "../lib/artifacts";
17
+ import {
18
+ DEFAULT_MAX_WRAP_UP_MODEL_TURNS, TOOL_FAILURE_WRAP_UP_INSTRUCTION, ModelRecovery, ToolGovernance,
19
+ artifactManifestMessage, artifactRefFromMessage,
20
+ } from "./governance";
21
+
22
+ const DEFAULT_WRAP_UP_INSTRUCTION = "Finish this request now without using tools. Summarize the completed work and any remaining limitation.";
23
23
 
24
24
  export interface SessionHarnessLimits {
25
- /** Complete model turns committed before one Worker instance releases. */
26
25
  modelTurnsPerSlice?: number;
27
- /** Complete working turns allowed before the no-tool final response. */
28
26
  maxModelTurns?: number;
29
- /** Maximum text turns allowed after the working or Tool failure budget. */
27
+ /** Text turns allowed after the working or Tool failure budget before the Submission is stopped. */
30
28
  maxWrapUpModelTurns?: number;
31
29
  wrapUpInstruction?: string;
32
30
  }
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
- */
31
+ export interface SessionHarnessOptions {
32
+ storage: Storage | Promise<Storage>;
33
+ models: Models;
34
+ model: Model<Api>;
35
+ thinkingLevel?: ModelThinkingLevel;
36
+ systemPrompt?: string;
37
+ tools?: readonly ToolRegistration<any, any>[];
38
+ activeToolNames?: readonly string[];
44
39
  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)];
40
+ modelToolResultExemptions?: readonly string[];
41
+ toolsToActivate?: (name: string, result: import("@earendil-works/pi-agent-core").AgentToolResult<unknown>, isError: boolean) => readonly string[];
42
+ compaction?: Partial<CompactionPolicy>;
43
+ streamOptions?: { maxRetries?: number };
44
+ retry?: Partial<ConversationRetryPolicy>;
45
+ toolExecution?: "parallel" | "sequential";
46
+ limits?: SessionHarnessLimits;
47
+ /** Per-Submission provider error classification and provider Session identity. */
48
+ recovery?: ModelRecovery;
49
+ beforeTool?: ToolHooks["beforeTool"];
50
+ afterResponse?: GenerationHooks["afterResponse"];
98
51
  }
99
52
 
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];
53
+ /** Host terminal projection; native Submission records remain authoritative. */
54
+ export interface SessionRunOutcome {
55
+ status: "completed" | "failed" | "aborted" | "declined";
56
+ endedAt: number;
57
+ error?: { code: string; message: string };
117
58
  }
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;
59
+ export type SessionHarnessRunResult =
60
+ | { kind: "settled"; outcome: SessionRunOutcome }
61
+ | { kind: "released"; operationId: string; reason: "slice" | "approval" | "interaction" };
62
+ export interface SessionRunRequest { operationId: string; prompt?: UserMessage | string; }
63
+ export function submissionOutcome(record: SettledSubmissionRecord): SessionRunOutcome {
129
64
  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,
65
+ status: record.status === "done" ? "completed" : record.reason === "aborted" ? "aborted" : "failed",
66
+ endedAt: Date.now(),
67
+ ...(record.status === "unanswered" ? { error: { code: record.reason, message: typeof record.detail === "string" ? record.detail : record.reason } } : {}),
145
68
  };
146
69
  }
147
70
 
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;
71
+ /** The host binds its ports; Pi owns generation, Tool tasks, waiting and recovery. */
72
+ export class SessionHarness {
73
+ private submission?: Submission;
74
+ private requestId?: string;
75
+ private release!: (reason: "approval" | "interaction" | "slice") => void;
230
76
  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>();
77
+ private governance = new ToolGovernance();
242
78
  private readonly artifactRefs = new Map<string, ArtifactRef>();
243
-
79
+ private wrapUpStarted = false;
80
+ /** Completed model turns of the current Submission. */
81
+ private ownedTurns = 0;
82
+ private readonly released = new Promise<"approval" | "interaction" | "slice">((resolve) => { this.release = resolve; });
244
83
  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
- );
84
+ readonly harness: NativeHarness,
85
+ public conversation: Conversation,
86
+ readonly storage: Storage,
87
+ private readonly options: SessionHarnessOptions,
88
+ ) {}
89
+
90
+ get openOperation(): { operationId: string } | null {
91
+ return this.submission && this.requestId ? { operationId: this.requestId } : null;
276
92
  }
277
93
 
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",
94
+ static async open(options: SessionHarnessOptions, context: Context): Promise<SessionHarness> {
95
+ const storage = await options.storage;
96
+ const registry = createRegistry<ToolRegistration<any, any>>();
97
+ let runtime!: SessionHarness;
98
+ const exemptions = new Set(options.modelToolResultExemptions ?? []);
99
+ const extension = defineExtension({
100
+ name: "springbrand.runtime", tools: options.tools ?? [],
101
+ sections: [section("system", () => options.systemPrompt, { tag: false })],
102
+ hooks: [
103
+ hook(GenerationTask, {
104
+ beforeRequest: ({ messages }) => {
105
+ const projected = messages.map((message): Message => {
106
+ if (message.role !== "toolResult" || exemptions.has(message.toolName)) return message;
107
+ const next = projectToolOutputForModel({ content: message.content, details: message.details }) as Pick<typeof message, "content" | "details">;
108
+ return { ...message, ...next };
109
+ });
110
+ const manifest = artifactManifestMessage(runtime.artifactRefs);
111
+ const instruction = runtime.governance.wrapUp
112
+ ? TOOL_FAILURE_WRAP_UP_INSTRUCTION
113
+ : runtime.wrapUpStarted ? options.limits?.wrapUpInstruction ?? DEFAULT_WRAP_UP_INSTRUCTION : undefined;
114
+ return { messages: [
115
+ ...projected,
116
+ ...(manifest ? [manifest] : []),
117
+ ...(instruction ? [{ role: "user" as const, content: instruction, timestamp: 1 }] : []),
118
+ ] };
119
+ },
120
+ afterResponse: async (message, api, hookContext) => {
121
+ options.recovery?.classify(message);
122
+ await options.afterResponse?.(message, api, hookContext);
123
+ },
124
+ afterTools: async (_assistant, _results, _api, hookContext) => {
125
+ const entries = await runtime.entries(hookContext);
126
+ const turns = runtime.ownedTurns = (await runtime.ownedEntries(entries, hookContext)).filter((entry) => entry.kind === "pi.assistant").length;
127
+ const total = entries.filter((entry) => entry.kind === "pi.assistant").length;
128
+ const maxTurns = options.limits?.maxModelTurns ?? 300;
129
+ const wrapUpAt = Math.min(maxTurns, runtime.governance.wrapUpAt ?? Infinity);
130
+ if (turns >= wrapUpAt + (options.limits?.maxWrapUpModelTurns ?? DEFAULT_MAX_WRAP_UP_MODEL_TURNS)) {
131
+ // The wrap-up is still calling Tools it no longer has; end the Submission instead of looping.
132
+ void runtime.conversation.abort(BACKGROUND_CONTEXT).catch(() => {});
133
+ return;
134
+ }
135
+ if (turns >= wrapUpAt && !runtime.wrapUpStarted) {
136
+ runtime.wrapUpStarted = true;
137
+ // Tools return on the next Submission's configure; the instruction is per request, never stored.
138
+ await runtime.conversation.configure({ tools: [] }, hookContext);
139
+ }
140
+ if (total - runtime.turnsAtOpen >= (options.limits?.modelTurnsPerSlice ?? 30)) runtime.release("slice");
141
+ },
142
+ }),
143
+ hook(ToolTask, {
144
+ beforeTool: async (call, api, hookContext) => {
145
+ // Calls belong to the assistant turn after the last completed Tool round.
146
+ const block = runtime.governance.admit(call, runtime.ownedTurns + 1);
147
+ if (block !== undefined) return { block };
148
+ return options.beforeTool?.(call, api, hookContext);
149
+ },
150
+ afterTool: async (call, result) => {
151
+ runtime.governance.record(call, result.isError === true, runtime.ownedTurns + 1);
152
+ const ref = artifactRefFromMessage({ role: "toolResult", details: result.details });
153
+ if (ref) runtime.artifactRefs.set(ref.artifactId, ref);
154
+ const compiled = new Set(options.tools?.map((tool) => tool.name) ?? []);
155
+ const names = (options.toolsToActivate?.(call.name, { content: [...result.content ?? []], details: result.details }, result.isError === true) ?? [])
156
+ .filter((name) => compiled.has(name));
157
+ return names.length ? { ...result, control: { ...result.control, addTools: names } } : undefined;
158
+ },
159
+ }),
160
+ ],
161
+ });
162
+ registry.install(extension);
163
+ const harness = await Harness.open(storage, {
164
+ models: options.models, registry,
165
+ settings: {
166
+ stream: options.streamOptions, compaction: options.compaction, toolExecution: options.toolExecution,
167
+ // Read for every retry decision, after afterResponse has classified that response.
168
+ get retry() {
169
+ const retry = { maxRetries: 4, ...options.retry };
170
+ return options.recovery ? { ...retry, enabled: (retry.enabled ?? true) && options.recovery.retryAllowed } : retry;
171
+ },
172
+ },
284
173
  }, 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;
174
+ const pointer = await harness.snapshot(RuntimeConversation, context);
175
+ const conversation = pointer?.id ? await harness.conversation(pointer.id, context) : await harness.root(context);
176
+ if (!conversation) { await harness.close(context); throw new Error("The persisted Pi conversation is missing"); }
177
+ if (!pointer?.id) await harness.commit(async (tx) => { (await tx.doc(RuntimeConversation)).id = conversation.id; }, context);
178
+ runtime = new SessionHarness(harness, conversation, storage, options);
179
+ runtime.turnsAtOpen = (await runtime.entries(context)).filter((entry) => entry.kind === "pi.assistant").length;
180
+ 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"));
181
+ if (unfinished.length > 1) { await harness.close(context); throw new Error("Multiple host runs are active in one Pi conversation"); }
182
+ if (unfinished[0]) {
183
+ runtime.submission = await harness.submission(unfinished[0].id, context);
184
+ runtime.requestId = unfinished[0].requestId;
314
185
  }
186
+ await runtime.restoreGovernance(context);
187
+ return runtime;
315
188
  }
316
189
 
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;
322
- }
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;
190
+ async submissionFor(requestId: string, context: Context) {
191
+ return this.storage.submissionByRequest(this.conversation.id, requestId, context);
326
192
  }
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);
193
+ async entries(context: Context = BACKGROUND_CONTEXT): Promise<EntryRecord[]> {
194
+ const entries: EntryRecord[] = [];
195
+ let cursor;
196
+ do {
197
+ const page = await this.conversation.entries({ order: "ascending" }, 200, cursor, context);
198
+ entries.push(...page.items); cursor = page.next;
199
+ } while (cursor);
200
+ return entries;
341
201
  }
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;
202
+ /** Entries of the active Submission, from its input onward. */
203
+ async ownedEntries(entries: readonly EntryRecord[], context: Context): Promise<EntryRecord[]> {
204
+ const receipt = await this.submission?.status(context);
205
+ return receipt?.entry ? entries.filter((entry) => entry.id >= receipt.entry!) : [...entries];
346
206
  }
347
-
348
- get openOperation(): OpenOperation | null {
349
- return this._openOperation;
207
+ private async restoreGovernance(context: Context): Promise<void> {
208
+ const entries = await this.entries(context);
209
+ this.artifactRefs.clear();
210
+ for (const entry of entries) {
211
+ const message = entry.model?.[0];
212
+ const ref = message ? artifactRefFromMessage(message) : undefined;
213
+ if (ref) this.artifactRefs.set(ref.artifactId, ref);
214
+ }
215
+ this.governance = new ToolGovernance();
216
+ this.wrapUpStarted = false;
217
+ this.ownedTurns = 0;
218
+ if (!this.submission) return;
219
+ const owned = await this.ownedEntries(entries, context);
220
+ this.governance.restore(owned);
221
+ this.ownedTurns = owned.filter((entry) => entry.kind === "pi.assistant").length;
222
+ const wrapUpAt = Math.min(this.options.limits?.maxModelTurns ?? 300, this.governance.wrapUpAt ?? Infinity);
223
+ this.wrapUpStarted = this.ownedTurns >= wrapUpAt;
350
224
  }
351
-
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)),
225
+ watch(context: Context): Promise<AgentEventStream> { return watchEvents(this.harness, this.conversation.id, context); }
226
+ requestHumanRelease(reason: "approval" | "interaction"): void { this.release(reason); }
227
+ async fork(at: EntryId | null, context: Context): Promise<void> {
228
+ await this.conversation.abort(context);
229
+ this.submission = undefined;
230
+ this.requestId = undefined;
231
+ const created = await this.harness.commit(async (tx) => {
232
+ const fork = at === null ? await tx.createConversation({ ownership: { kind: "ownerless" } }) : await tx.forkConversation(this.conversation.id, at, { ownership: { kind: "ownerless" } });
233
+ (await tx.doc(RuntimeConversation)).id = fork.id;
234
+ return fork;
374
235
  }, 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");
383
- }
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;
236
+ this.conversation = (await this.harness.conversation(created.id, context))!;
237
+ this.turnsAtOpen = (await this.entries(context)).filter((entry) => entry.kind === "pi.assistant").length;
392
238
  }
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
- }
239
+ async admit(request: SessionRunRequest, context: Context): Promise<void> {
240
+ if (this.submission && this.requestId !== request.operationId) throw new Error("A native Pi Submission is already active");
241
+ const existing = await this.submissionFor(request.operationId, context);
242
+ if (existing) {
243
+ this.requestId = request.operationId;
244
+ this.submission = await this.harness.submission(existing.id, context);
245
+ } else {
246
+ if (request.prompt === undefined) throw new Error("A new native Pi Submission needs input");
247
+ const names = new Set(this.options.activeToolNames ?? this.options.tools?.map((tool) => tool.name) ?? []);
248
+ const previous = await this.harness.snapshot(AgentDoc, this.conversation.id, context);
249
+ if (Array.isArray(previous?.tools)) for (const name of previous.tools) if (this.options.onDemandToolNames?.includes(name)) names.add(name);
250
+ 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)) ?? [],
251
+ // Earlier runtimes stored the wrap-up instruction in the agent; it applies to one Submission only.
252
+ instructions: null }, context);
253
+ this.requestId = request.operationId;
254
+ this.submission = await this.conversation.submit({ type: "input", requestId: request.operationId, content: typeof request.prompt === "string" ? request.prompt : request.prompt.content, whenBusy: "reject" }, context);
400
255
  }
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
- }
511
- });
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));
256
+ await this.restoreGovernance(context);
522
257
  }
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;
258
+ async run(request: SessionRunRequest | undefined, context: Context): Promise<SessionHarnessRunResult> {
259
+ if (request) await this.admit(request, context);
260
+ if (!this.submission || !this.requestId) throw new Error("No native Pi Submission to resume");
261
+ const waiting = this.submission.wait(context).then(async (record) => {
262
+ // One input can be answered while Pi is still working on a later steer.
263
+ await this.conversation.waitForIdle(context);
264
+ let cursor;
265
+ do {
266
+ const page = await this.storage.scanSubmissions({ conversationId: this.conversation.id, order: "descending" }, 200, cursor, context);
267
+ for (const next of page.items) {
268
+ if (next.id <= record.id) return record;
269
+ if (next.type === "input" && next.requestId?.startsWith("steer:") && (next.status === "done" || next.status === "unanswered")) return next as SettledSubmissionRecord;
270
+ }
271
+ cursor = page.next;
272
+ } while (cursor);
273
+ return record;
274
+ });
275
+ waiting.catch(() => {});
276
+ const result = await Promise.race([waiting.then((record) => ({ record })), this.released.then((reason) => ({ reason }))]);
277
+ if ("record" in result) {
278
+ this.submission = undefined;
279
+ return { kind: "settled", outcome: submissionOutcome(result.record) };
627
280
  }
281
+ // A pending approval is before intent. Let already-started parallel effects commit before detaching.
282
+ 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");
283
+ await Promise.all(executing.map(({ record }) => this.harness.waitForTask(record.id, context)));
284
+ await this.harness.close(BACKGROUND_CONTEXT);
285
+ return { kind: "released", operationId: this.requestId, reason: result.reason };
628
286
  }
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);
287
+ async abort(context: Context): Promise<void> { await this.conversation.abort(context); }
288
+ async close(context: Context): Promise<void> { await this.harness.close(context); }
646
289
  }
290
+ export const openSessionHarness = SessionHarness.open;