@modelprofile.com/flexharness-agent 13.3.0 → 15.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,6 +12,7 @@ import {
12
12
  type IGenerationBegunEvent,
13
13
  type IGenerationExecutionCompletedEvent,
14
14
  type IGenerationExecutionStartedEvent,
15
+ type IAgentContextUsage,
15
16
  type IGenerationOutcomeEvent,
16
17
  type IRuntimeEvent,
17
18
  type IToolCallEvent,
@@ -63,6 +64,13 @@ import { validateAgentEventSnapshot } from './smartagent.persistence.js';
63
64
  import { MAX_RETRY_ATTEMPTS, planModelCallRetry } from './smartagent.retry.js';
64
65
  import { endModelStreamAtAbort } from './smartagent.modelabort.js';
65
66
  import { AgentModelCallUsageRecorder } from './smartagent.usage.js';
67
+ import {
68
+ assertAgentContextCompactionSetting,
69
+ assertAgentContextTokens,
70
+ estimateAgentContextTokens,
71
+ resolveAgentContextCompactionPoint,
72
+ type TAgentContextCompactionPoint,
73
+ } from './smartagent.contextcompaction.js';
66
74
  import { AgentToolCallScheduler } from './smartagent.toolscheduler.js';
67
75
  import type { IAgentToolExecutionOptions } from './smartagent.toolscheduler.js';
68
76
  import type {
@@ -126,10 +134,12 @@ interface IEffectiveGenerationRuntime {
126
134
  tools: plugins.ToolSet;
127
135
  isReadOnlyToolCall?: TAgentToolCallClassifier;
128
136
  providerOptions?: plugins.ProviderOptions;
129
- maxSteps: number;
137
+ maxSteps?: number;
130
138
  maxOutputTokens?: number;
131
139
  /** The generation's cache setting gives this provider cache hints: its prefix may be read back. */
132
140
  cachesPrefix: boolean;
141
+ /** Where the generation compacts settled history before its first inference. */
142
+ contextCompactionPoint: TAgentContextCompactionPoint;
133
143
  }
134
144
 
135
145
  interface IGenerationBackgroundJob {
@@ -273,6 +283,9 @@ const parseToolInput = (input: unknown): unknown => {
273
283
  }
274
284
  };
275
285
 
286
+ const textEncoder = new TextEncoder();
287
+ const utf8JsonBytes = (value: unknown): number => textEncoder.encode(JSON.stringify(value)).byteLength;
288
+
276
289
  const errorToString = (error: unknown): string =>
277
290
  error instanceof Error ? error.message : String(error);
278
291
 
@@ -341,6 +354,11 @@ export class AgentSessionEngine implements IAgentSession {
341
354
  private readonly events: TAgentEvent[] = [];
342
355
  private readonly eventIds = new Set<string>();
343
356
  private readonly generationHandles = new Map<string, IAgentGenerationHandle>();
357
+ /**
358
+ * The latest usage reading of each open transactional generation, written onto its outcome. It
359
+ * lives as long as the generation's handle: its outcome commit or the session's close ends both.
360
+ */
361
+ private readonly generationContextUsage = new Map<string, IAgentContextUsage>();
344
362
  private readonly unresolvedLeaseCleanups = new Set<IGenerationLeaseCleanupOwnership>();
345
363
  private sequence = 0;
346
364
  private persistenceRevision = 0;
@@ -420,14 +438,14 @@ export class AgentSessionEngine implements IAgentSession {
420
438
  if (!Number.isSafeInteger(maxContextOverflowRetries) || maxContextOverflowRetries < 0) {
421
439
  throw new Error('maxContextOverflowRetries must be a non-negative integer.');
422
440
  }
423
- if (options.contextCompactionBytes !== undefined && options.contextCompactionBytes !== false
424
- && (!Number.isSafeInteger(options.contextCompactionBytes)
425
- || options.contextCompactionBytes < 1 || options.contextCompactionBytes > 32 * 1024 * 1024)) {
426
- throw new Error('contextCompactionBytes must be false or an integer from 1 through 33554432.');
427
- }
428
- if (typeof options.contextCompactionBytes === 'number' && !options.contextCompactor) {
429
- throw new Error('contextCompactionBytes requires a contextCompactor.');
441
+ if (options.contextCompaction !== undefined) {
442
+ assertAgentContextCompactionSetting(options.contextCompaction);
443
+ if (options.contextCompaction !== false && !options.contextCompactor) {
444
+ throw new Error('contextCompaction requires a contextCompactor.');
445
+ }
430
446
  }
447
+ assertAgentContextTokens(options.contextWindow, 'contextWindow');
448
+ assertAgentContextTokens(options.contextCompactionDefaultTokens, 'contextCompactionDefaultTokens');
431
449
  if (options.attachmentResolver !== undefined) assertAttachmentResolver(options.attachmentResolver);
432
450
  const changeListenerTimeoutMs = options.changeListenerTimeoutMs ?? 30_000;
433
451
  if (!Number.isSafeInteger(changeListenerTimeoutMs) || changeListenerTimeoutMs <= 0) {
@@ -732,6 +750,7 @@ export class AgentSessionEngine implements IAgentSession {
732
750
  this.eventIds.add(event.id);
733
751
  if (event.type === 'generation-outcome') {
734
752
  this.generationHandles.delete(event.generationId);
753
+ this.generationContextUsage.delete(event.generationId);
735
754
  }
736
755
  }
737
756
  if (notify && committedEvents.length > 0) {
@@ -936,12 +955,14 @@ export class AgentSessionEngine implements IAgentSession {
936
955
  const parent = transaction.executionCompleted
937
956
  ?? transaction.executionStarted
938
957
  ?? transaction.begun;
958
+ const contextUsage = this.generationContextUsage.get(generationId);
939
959
  this.commitEvent(createAgentEvent<IGenerationOutcomeEvent>({
940
960
  type: 'generation-outcome',
941
961
  generationId,
942
962
  outcome,
943
963
  reason,
944
964
  ...(stopped ? { stopped: true } : {}),
965
+ ...(contextUsage ? { contextUsage } : {}),
945
966
  parentIds: [parent.id],
946
967
  }));
947
968
  await this.flushPersistence();
@@ -985,7 +1006,7 @@ export class AgentSessionEngine implements IAgentSession {
985
1006
  undefined,
986
1007
  options.stopped === true,
987
1008
  );
988
- if (this.options.contextCompactor && this.options.contextCompactionBytes !== false
1009
+ if (this.options.contextCompactor
989
1010
  && this.options.eventStore?.archive
990
1011
  && getOpenAgentGenerationTransactions(this.events).length === 0
991
1012
  && getUncertainToolExecutionIntents(this.events).length === 0) {
@@ -1100,6 +1121,37 @@ export class AgentSessionEngine implements IAgentSession {
1100
1121
  : buildModelMessages(visibleEvents);
1101
1122
  }
1102
1123
 
1124
+ /**
1125
+ * The session's context in tokens. The latest provider usage reading counts while the events it
1126
+ * saw still build the same model messages and no compaction followed them; what came after it
1127
+ * is estimated. Without such a reading the whole context is estimated.
1128
+ */
1129
+ private measureContextTokens(): { tokens: number; basis: 'usage' | 'estimate' } {
1130
+ const totalBytes = utf8JsonBytes(this.getModelMessages());
1131
+ const reading = this.latestContextUsage();
1132
+ if (reading && !this.events.some((event) =>
1133
+ event.type === 'context-compaction' && event.sequence! > reading.throughSequence)) {
1134
+ const readingBytes = utf8JsonBytes(this.buildContext(
1135
+ this.events.filter((event) => event.sequence! <= reading.throughSequence),
1136
+ ));
1137
+ if (readingBytes === reading.contextBytes) {
1138
+ return {
1139
+ tokens: reading.inputTokens + estimateAgentContextTokens(totalBytes - reading.contextBytes),
1140
+ basis: 'usage',
1141
+ };
1142
+ }
1143
+ }
1144
+ return { tokens: estimateAgentContextTokens(totalBytes), basis: 'estimate' };
1145
+ }
1146
+
1147
+ private latestContextUsage(): IAgentContextUsage | undefined {
1148
+ for (let index = this.events.length - 1; index >= 0; index--) {
1149
+ const event = this.events[index];
1150
+ if (event.type === 'generation-outcome' && event.contextUsage) return event.contextUsage;
1151
+ }
1152
+ return undefined;
1153
+ }
1154
+
1103
1155
  public subscribe(listener: TAgentSessionChangeListener): () => void {
1104
1156
  if (this.closed) throw new Error('AgentSession is closed.');
1105
1157
  const existing = this.listeners.get(listener);
@@ -1640,6 +1692,10 @@ export class AgentSessionEngine implements IAgentSession {
1640
1692
  scheduler: AgentToolCallScheduler,
1641
1693
  isReadOnlyToolCall: TAgentToolCallClassifier | undefined,
1642
1694
  earlyStarts: Map<string, (toolCallId: string, input: unknown) => void>,
1695
+ observeEarlyExecution: (
1696
+ call: { toolCallId: string; toolName: string; input: unknown },
1697
+ run: () => unknown,
1698
+ ) => unknown,
1643
1699
  ): plugins.ToolSet {
1644
1700
  const wrappedTools: plugins.ToolSet = {};
1645
1701
  for (const [toolName, toolDefinition] of Object.entries(tools)) {
@@ -1674,7 +1730,10 @@ export class AgentSessionEngine implements IAgentSession {
1674
1730
  context: undefined,
1675
1731
  confirmed,
1676
1732
  };
1677
- return this.invokeGenerationCallback(() => originalExecute(input, earlyExecutionOptions));
1733
+ return observeEarlyExecution({ toolCallId, toolName, input }, () => {
1734
+ abortSignal.throwIfAborted();
1735
+ return this.invokeGenerationCallback(() => originalExecute(input, earlyExecutionOptions));
1736
+ });
1678
1737
  });
1679
1738
  });
1680
1739
  }
@@ -1714,6 +1773,7 @@ export class AgentSessionEngine implements IAgentSession {
1714
1773
  if (existing.signature !== signature) {
1715
1774
  throw new Error(`Tool call id collision for "${toolCallId}" in generation "${generationId}".`);
1716
1775
  }
1776
+ scheduler.adopt(toolCallId, signature, readOnly(input));
1717
1777
  return existing.promise;
1718
1778
  }
1719
1779
  const contextualExecutionOptions = {
@@ -1980,8 +2040,8 @@ export class AgentSessionEngine implements IAgentSession {
1980
2040
  ?? options.providerOptions
1981
2041
  ?? this.options.providerOptions;
1982
2042
  const cache = lease?.cache ?? options.cache ?? this.options.cache ?? 'auto';
1983
- const maxSteps = lease?.maxSteps ?? options.maxSteps ?? this.options.maxSteps ?? 20;
1984
- if (!Number.isSafeInteger(maxSteps) || maxSteps <= 0) {
2043
+ const maxSteps = lease?.maxSteps ?? options.maxSteps ?? this.options.maxSteps;
2044
+ if (maxSteps !== undefined && (!Number.isSafeInteger(maxSteps) || maxSteps <= 0)) {
1985
2045
  throw new Error('maxSteps must be a positive integer.');
1986
2046
  }
1987
2047
  const maxOutputTokens = lease?.maxOutputTokens;
@@ -2021,6 +2081,21 @@ export class AgentSessionEngine implements IAgentSession {
2021
2081
  },
2022
2082
  ],
2023
2083
  });
2084
+ if (lease && !lease.model
2085
+ && (lease.contextWindow !== undefined || lease.contextCompactionDefaultTokens !== undefined)) {
2086
+ throw new Error('A generation lease states a context window only together with its model.');
2087
+ }
2088
+ // The window belongs to the model: a lease or generate options naming a model name its window.
2089
+ const windowSource = lease?.model ? lease : options.model ? undefined : this.options;
2090
+ const contextCompactionPoint = resolveAgentContextCompactionPoint({
2091
+ ...(windowSource?.contextWindow === undefined ? {} : { contextWindow: windowSource.contextWindow }),
2092
+ ...(windowSource?.contextCompactionDefaultTokens === undefined
2093
+ ? {}
2094
+ : { contextCompactionDefaultTokens: windowSource.contextCompactionDefaultTokens }),
2095
+ ...((lease?.contextCompaction ?? this.options.contextCompaction) === undefined
2096
+ ? {}
2097
+ : { setting: lease?.contextCompaction ?? this.options.contextCompaction }),
2098
+ });
2024
2099
  return {
2025
2100
  model: effectiveModel,
2026
2101
  system,
@@ -2031,6 +2106,7 @@ export class AgentSessionEngine implements IAgentSession {
2031
2106
  ...(maxOutputTokens === undefined ? {} : { maxOutputTokens }),
2032
2107
  cachesPrefix: messageCacheProvider !== undefined
2033
2108
  || plugins.getSmartAiCacheProviderOptions({ provider: model.provider, modelId: model.modelId, cache }) !== undefined,
2109
+ contextCompactionPoint,
2034
2110
  };
2035
2111
  }
2036
2112
 
@@ -2145,6 +2221,9 @@ export class AgentSessionEngine implements IAgentSession {
2145
2221
  let retriedMs = 0;
2146
2222
  let contextOverflowRetries = 0;
2147
2223
  let automaticCompactionChecked = false;
2224
+ // The context the current inference sends, for its usage reading; only a transactional
2225
+ // generation's reading persists, on its outcome.
2226
+ let inferenceContext: Omit<IAgentContextUsage, 'inputTokens'> | undefined;
2148
2227
  let totalInput = 0;
2149
2228
  let totalOutput = 0;
2150
2229
  let totalCacheRead = 0;
@@ -2165,8 +2244,16 @@ export class AgentSessionEngine implements IAgentSession {
2165
2244
  const reportCompactionUsage: TAgentModelCallUsageReporter = (call) =>
2166
2245
  recordModelCallUsage({ ...call, source: 'compaction', generationId });
2167
2246
  let currentInferenceId: string | undefined;
2168
- /** Text the current inference streamed so far, kept if the inference is interrupted. */
2169
- let inferenceText = '';
2247
+ // Observations belong to one inference, not one generation or SDK retry. Completed steps
2248
+ // use the SDK response; failed/stopped steps retain only what actually reached onChunk.
2249
+ let inferenceContent: Exclude<IAssistantMessageEvent['content'], string> = [];
2250
+ const inferenceResultConverters = new Map<number, () => Promise<TAgentToolResultOutput>>();
2251
+ const inferenceConversions = new Map<string, Promise<TAgentToolResultOutput>>();
2252
+ const localToolConversionErrors: unknown[] = [];
2253
+ const inferenceTextParts = new Map<string, Extract<
2254
+ Exclude<IAssistantMessageEvent['content'], string>[number],
2255
+ { type: 'text' | 'reasoning' }
2256
+ >>();
2170
2257
  let latestInferenceVisibleIds = new Set(this.events.map((event) => event.id));
2171
2258
  const completedInferenceIds = new Set<string>();
2172
2259
  const reasoningTextById = new Map<string, string>();
@@ -2235,7 +2322,11 @@ export class AgentSessionEngine implements IAgentSession {
2235
2322
  : undefined,
2236
2323
  }));
2237
2324
  currentInferenceId = marker.id;
2238
- inferenceText = '';
2325
+ inferenceContent = [];
2326
+ inferenceTextParts.clear();
2327
+ inferenceResultConverters.clear();
2328
+ inferenceConversions.clear();
2329
+ localToolConversionErrors.length = 0;
2239
2330
  latestInferenceVisibleIds = new Set(this.events.map((event) => event.id));
2240
2331
  return marker.id;
2241
2332
  };
@@ -2281,42 +2372,265 @@ export class AgentSessionEngine implements IAgentSession {
2281
2372
  throw new AggregateError(callbackErrors, 'Reporting applied pending input failed.');
2282
2373
  }
2283
2374
  };
2284
- /**
2285
- * Keeps what a stopped inference produced: the text it streamed and the tool calls it
2286
- * issued, as one assistant message of the generation. Tool calls without a result are
2287
- * closed when the stopped generation is rendered for the model.
2288
- */
2289
- const commitInterruptedInference = async (): Promise<void> => {
2375
+ /** Preserve observed output on failure as well as Stop, without inventing a result. */
2376
+ const commitInterruptedInference = async (): Promise<unknown[]> => {
2290
2377
  const inferenceId = currentInferenceId;
2291
- if (!options.stop?.aborted || !inferenceId || completedInferenceIds.has(inferenceId)) return;
2292
- completedInferenceIds.add(inferenceId);
2378
+ if (!inferenceId || completedInferenceIds.has(inferenceId)) return [];
2293
2379
  if (this.events.some((event) =>
2294
- event.type === 'assistant-message' && event.inferenceId === inferenceId)) return;
2295
- const toolCallParts: TAgentToolCallPart[] = this.events
2296
- .filter((event): event is IToolCallEvent =>
2380
+ event.type === 'assistant-message' && event.inferenceId === inferenceId)) return [];
2381
+ const conversionErrors: unknown[] = [];
2382
+ for (const [index, convert] of inferenceResultConverters) {
2383
+ const part = inferenceContent[index];
2384
+ try {
2385
+ if (part.type === 'tool-result') part.output = await convert();
2386
+ } catch (error) {
2387
+ // Conversion is not execution: keep the observed result, not an invented tool
2388
+ // failure or unknown effect. Report the conversion failure after history is durable.
2389
+ conversionErrors.push(error);
2390
+ }
2391
+ }
2392
+ inferenceResultConverters.clear();
2393
+ const content = [...inferenceContent];
2394
+ // Execution hooks can precede the corresponding onChunk notification.
2395
+ for (const event of this.events) {
2396
+ if (event.type !== 'tool-call' || event.generationId !== generationId
2397
+ || event.inferenceId !== inferenceId) continue;
2398
+ if (content.some((part) => part.type === 'tool-call' && part.toolCallId === event.toolCallId)) continue;
2399
+ content.push({
2400
+ type: 'tool-call', toolCallId: event.toolCallId, toolName: event.toolName,
2401
+ input: event.input, providerExecuted: event.providerExecuted,
2402
+ providerOptions: event.providerOptions,
2403
+ });
2404
+ }
2405
+ for (const part of inferenceContent) {
2406
+ if (part.type !== 'tool-call' || part.providerExecuted !== true) continue;
2407
+ if (content.some((result) => result.type === 'tool-result' && result.toolCallId === part.toolCallId)) continue;
2408
+ content.push({ type: 'text', text:
2409
+ `[Provider-executed tool "${part.toolName}" (${part.toolCallId}): no result was observed; its effect is unknown. Do not automatically repeat its effects.]`,
2410
+ });
2411
+ }
2412
+ if (content.length === 0) return conversionErrors;
2413
+ this.commitResponseMessages([{ role: 'assistant', content }], generationId, inferenceId, executionIntentIds);
2414
+ await this.flushPersistence();
2415
+ return conversionErrors;
2416
+ };
2417
+ const prepareToolCall = (toolCall: { toolCallId: string; toolName: string; input: unknown }): Promise<void> => {
2418
+ const input = parseToolInput(toolCall.input);
2419
+ const signature = toolSignature(toolCall.toolName, input);
2420
+ const existing = preparations.get(toolCall.toolCallId);
2421
+ if (existing) {
2422
+ if (existing.signature !== signature) {
2423
+ throw new Error(`Tool call id collision for "${toolCall.toolCallId}" in generation "${generationId}".`);
2424
+ }
2425
+ return existing.promise;
2426
+ }
2427
+ const promise = Promise.resolve().then(async () => {
2428
+ const startedBeforeInference = !currentInferenceId;
2429
+ const inferenceId = currentInferenceId ?? beginInference();
2430
+ const approvalAssistantEvent = startedBeforeInference
2431
+ ? [...this.events].reverse().find((candidate): candidate is IAssistantMessageEvent =>
2432
+ candidate.type === 'assistant-message'
2433
+ && Array.isArray(candidate.content)
2434
+ && candidate.content.some((part) =>
2435
+ part.type === 'tool-call' && part.toolCallId === toolCall.toolCallId,
2436
+ ),
2437
+ )
2438
+ : undefined;
2439
+ let callEvent = [...this.events].reverse().find((event): event is IToolCallEvent =>
2297
2440
  event.type === 'tool-call'
2298
- && event.generationId === generationId
2299
- && event.inferenceId === inferenceId)
2300
- .map((event) => ({
2301
- type: 'tool-call',
2302
- toolCallId: event.toolCallId,
2303
- toolName: event.toolName,
2304
- input: event.input,
2305
- ...(event.providerExecuted === undefined ? {} : { providerExecuted: event.providerExecuted }),
2306
- ...(event.providerOptions === undefined ? {} : { providerOptions: event.providerOptions }),
2307
- }));
2308
- if (!inferenceText && toolCallParts.length === 0) return;
2309
- this.commitEvent(createAgentEvent<IAssistantMessageEvent>({
2310
- type: 'assistant-message',
2311
- content: [
2312
- ...(inferenceText ? [{ type: 'text' as const, text: inferenceText }] : []),
2313
- ...toolCallParts,
2314
- ],
2441
+ && event.toolCallId === toolCall.toolCallId
2442
+ && (
2443
+ event.generationId === generationId
2444
+ || (
2445
+ !!approvalAssistantEvent?.id
2446
+ && event.parentIds?.includes(approvalAssistantEvent.id) === true
2447
+ )
2448
+ ));
2449
+ if (callEvent && toolSignature(callEvent.toolName, callEvent.input) !== signature) {
2450
+ throw new Error(`Tool call id collision for "${toolCall.toolCallId}" in generation "${generationId}".`);
2451
+ }
2452
+ if (!callEvent) {
2453
+ callEvent = this.commitEvent(createAgentEvent<IToolCallEvent>({
2454
+ type: 'tool-call',
2455
+ toolCallId: toolCall.toolCallId,
2456
+ toolName: toolCall.toolName,
2457
+ input,
2458
+ generationId,
2459
+ inferenceId,
2460
+ causationId: inferenceId,
2461
+ parentIds: [inferenceId],
2462
+ }));
2463
+ }
2464
+ if (options.transaction) {
2465
+ let intent = [...this.events].reverse().find(
2466
+ (event): event is IToolExecutionIntentEvent =>
2467
+ event.type === 'tool-execution-intent'
2468
+ && event.generationId === generationId
2469
+ && event.toolCallId === toolCall.toolCallId,
2470
+ );
2471
+ if (intent && toolSignature(intent.toolName, intent.input) !== signature) {
2472
+ throw new Error(`Tool call id collision for "${toolCall.toolCallId}" in generation "${generationId}".`);
2473
+ }
2474
+ if (!intent) {
2475
+ intent = this.commitEvent(createAgentEvent<IToolExecutionIntentEvent>({
2476
+ type: 'tool-execution-intent',
2477
+ generationId,
2478
+ inferenceId: callEvent.inferenceId,
2479
+ causationId: callEvent.id,
2480
+ toolCallId: toolCall.toolCallId,
2481
+ toolName: toolCall.toolName,
2482
+ input,
2483
+ parentIds: [callEvent.id],
2484
+ }));
2485
+ }
2486
+ executionIntentIds.set(toolCall.toolCallId, intent.id);
2487
+ }
2488
+ // One save makes the call and its execution intent durable before the tool runs.
2489
+ await this.flushPersistence();
2490
+ recordToolCall(toolCalls, toolCallIndexes, toolCall);
2491
+ completedPreparations.add(toolCall.toolCallId);
2492
+ const callbackEvent: IAgentToolCallStartEvent = {
2493
+ toolCallId: toolCall.toolCallId,
2494
+ toolName: toolCall.toolName,
2495
+ input,
2496
+ };
2497
+ this.options.onToolCallStart?.(callbackEvent);
2498
+ this.options.onToolCall?.(callbackEvent.toolName, callbackEvent.input);
2499
+ });
2500
+ preparations.set(toolCall.toolCallId, { signature, promise });
2501
+ void promise.catch((error) => {
2502
+ if (!completedPreparations.has(toolCall.toolCallId)) {
2503
+ preparationErrors.set(toolCall.toolCallId, error);
2504
+ }
2505
+ });
2506
+ return promise;
2507
+ };
2508
+ const finishToolCall = async (finish: {
2509
+ toolCall: { toolCallId: string; toolName: string; input: unknown };
2510
+ toolOutput: { type: 'tool-result'; output: unknown } | { type: 'tool-error'; error: unknown };
2511
+ }, unknownEffect = false): Promise<void> => {
2512
+ toolScheduler.settle(finish.toolCall.toolCallId);
2513
+ finishedToolCallIds.add(finish.toolCall.toolCallId);
2514
+ preliminaryToolOutputs.delete(finish.toolCall.toolCallId);
2515
+ const inferenceId = currentInferenceId ?? beginInference();
2516
+ const previousResult = this.currentGenerationEvent(
2517
+ generationId,
2518
+ 'tool-result',
2519
+ finish.toolCall.toolCallId,
2520
+ );
2521
+ if (this.currentGenerationEvent(
2522
+ generationId,
2523
+ 'tool-result',
2524
+ finish.toolCall.toolCallId,
2525
+ inferenceId,
2526
+ )) return;
2527
+ const input = parseToolInput(finish.toolCall.input);
2528
+ const startEvent: IAgentToolCallStartEvent = {
2529
+ toolCallId: finish.toolCall.toolCallId,
2530
+ toolName: finish.toolCall.toolName,
2531
+ input,
2532
+ };
2533
+ const finishEvent: TAgentToolCallFinishEvent = finish.toolOutput.type === 'tool-result'
2534
+ ? { ...startEvent, success: true, output: finish.toolOutput.output }
2535
+ : { ...startEvent, success: false, error: errorToString(finish.toolOutput.error) };
2536
+ const parentCall = this.currentGenerationEvent(
2537
+ generationId,
2538
+ 'tool-call',
2539
+ finish.toolCall.toolCallId,
2540
+ inferenceId,
2541
+ );
2542
+ const toolWithModelOutput = allTools[finish.toolCall.toolName] as {
2543
+ toModelOutput?: (options: {
2544
+ toolCallId: string;
2545
+ input: unknown;
2546
+ output: unknown;
2547
+ }) => Promise<TAgentToolResultOutput> | TAgentToolResultOutput;
2548
+ } | undefined;
2549
+ let modelOutput: TAgentToolResultOutput | undefined;
2550
+ let conversionFailed = false;
2551
+ let conversionError: unknown;
2552
+ try {
2553
+ modelOutput = finishEvent.success && toolWithModelOutput?.toModelOutput
2554
+ ? await this.invokeGenerationCallback(
2555
+ () => toolWithModelOutput.toModelOutput!({
2556
+ toolCallId: finish.toolCall.toolCallId,
2557
+ input,
2558
+ output: finishEvent.output,
2559
+ }),
2560
+ )
2561
+ : fallbackModelOutput(
2562
+ finishEvent.success ? finishEvent.output : undefined,
2563
+ finishEvent.success ? undefined : finishEvent.error,
2564
+ );
2565
+ } catch (error) {
2566
+ // Presentation failed, not execution. Save the actual outcome without inventing
2567
+ // model output, then propagate the converter error after observations are durable.
2568
+ conversionFailed = true;
2569
+ conversionError = error;
2570
+ localToolConversionErrors.push(error);
2571
+ }
2572
+ // A protocol-closing unknown result must not settle the recovery intent.
2573
+ const executionIntentId = unknownEffect ? undefined : executionIntentIds.get(finish.toolCall.toolCallId);
2574
+ this.commitEvent(createAgentEvent<IToolResultEvent>({
2575
+ type: 'tool-result',
2576
+ toolCallId: finish.toolCall.toolCallId,
2577
+ toolName: finish.toolCall.toolName,
2578
+ ...(executionIntentId ? { executionIntentId } : {}),
2579
+ output: finishEvent.success ? finishEvent.output : undefined,
2580
+ error: finishEvent.success ? undefined : finishEvent.error,
2581
+ modelOutput,
2315
2582
  generationId,
2316
2583
  inferenceId,
2317
2584
  causationId: inferenceId,
2585
+ parentIds: parentCall ? [parentCall.id] : [inferenceId],
2318
2586
  }));
2319
2587
  await this.flushPersistence();
2588
+ recordToolCall(
2589
+ toolCalls,
2590
+ toolCallIndexes,
2591
+ finish.toolCall,
2592
+ finishEvent.success ? { output: finishEvent.output } : { error: finishEvent.error },
2593
+ );
2594
+ if (!previousResult) {
2595
+ this.options.onToolCallFinish?.(finishEvent);
2596
+ this.options.onToolResult?.(
2597
+ finishEvent.toolName,
2598
+ finishEvent.success ? finishEvent.output : undefined,
2599
+ );
2600
+ }
2601
+ if (conversionFailed) throw conversionError;
2602
+ };
2603
+ // Track actual early executions independently of SDK adoption. A failed transport does
2604
+ // not undo a tool that already ran, nor authorize replay of a still-unknown execution.
2605
+ const earlyExecutions = new Map<string, {
2606
+ call: { toolCallId: string; toolName: string; input: unknown };
2607
+ promise: Promise<unknown>;
2608
+ outcome?: { type: 'tool-result'; output: unknown } | { type: 'tool-error'; error: unknown };
2609
+ }>();
2610
+ let unknownEarlyExecution = false;
2611
+ const retainUnadoptedEarlyExecutions = async (): Promise<unknown[]> => {
2612
+ const errors: unknown[] = [];
2613
+ for (const [id, execution] of earlyExecutions) {
2614
+ if (ledger.has(id)) continue; // SDK adoption already owns its result.
2615
+ try {
2616
+ await preparations.get(id)?.promise;
2617
+ const outcome = execution.outcome;
2618
+ const unknown = new Error(
2619
+ 'The early tool call ended before its result was observed; its effect is unknown. Do not automatically repeat its effects.',
2620
+ );
2621
+ if (!outcome) unknownEarlyExecution = true;
2622
+ const promise = outcome ? execution.promise : Promise.reject(unknown);
2623
+ void promise.catch(() => undefined);
2624
+ ledger.set(id, { signature: toolSignature(execution.call.toolName, execution.call.input), promise });
2625
+ await finishToolCall({ toolCall: execution.call,
2626
+ toolOutput: outcome ?? { type: 'tool-error', error: unknown } }, !outcome);
2627
+ } catch (error) {
2628
+ // One result's conversion/reporting must not abandon any other actual execution.
2629
+ errors.push(error);
2630
+ }
2631
+ }
2632
+ earlyExecutions.clear();
2633
+ return errors;
2320
2634
  };
2321
2635
  const toolScheduler = new AgentToolCallScheduler(combinedAbort.signal);
2322
2636
  const earlyToolStarts = new Map<string, (toolCallId: string, input: unknown) => void>();
@@ -2343,8 +2657,56 @@ export class AgentSessionEngine implements IAgentSession {
2343
2657
  toolScheduler,
2344
2658
  runtime.isReadOnlyToolCall,
2345
2659
  earlyToolStarts,
2660
+ (call, run) => {
2661
+ const existing = ledger.get(call.toolCallId);
2662
+ if (existing) {
2663
+ if (existing.signature !== toolSignature(call.toolName, call.input)) {
2664
+ throw new Error(`Tool call id collision for "${call.toolCallId}" in generation "${generationId}".`);
2665
+ }
2666
+ return existing.promise;
2667
+ }
2668
+ const execution: {
2669
+ call: typeof call;
2670
+ promise: Promise<unknown>;
2671
+ outcome?: { type: 'tool-result'; output: unknown } | { type: 'tool-error'; error: unknown };
2672
+ } = { call, promise: Promise.resolve() };
2673
+ execution.promise = prepareToolCall(call).then(run).then((value) => {
2674
+ if (isAsyncIterable(value)) {
2675
+ return this.trackToolExecutionIterable(value,
2676
+ (output) => { execution.outcome = { type: 'tool-result', output }; },
2677
+ (error) => { execution.outcome = { type: 'tool-error', error }; },
2678
+ () => undefined);
2679
+ }
2680
+ execution.outcome = { type: 'tool-result', output: value };
2681
+ return value;
2682
+ }, (error: unknown) => {
2683
+ execution.outcome = { type: 'tool-error', error };
2684
+ throw error;
2685
+ });
2686
+ earlyExecutions.set(call.toolCallId, execution);
2687
+ return execution.promise;
2688
+ },
2346
2689
  );
2347
2690
 
2691
+ // The SDK converts completed responses; interrupted responses convert here instead.
2692
+ // Share the attempt so an SDK conversion failure is neither retried nor allowed to
2693
+ // erase the actual provider result when the interrupted history is committed.
2694
+ for (const [toolName, definition] of Object.entries(allTools)) {
2695
+ if (definition.type !== 'provider' || !definition.toModelOutput) continue;
2696
+ const convert = definition.toModelOutput;
2697
+ allTools[toolName] = {
2698
+ ...definition,
2699
+ toModelOutput: (options: Parameters<typeof convert>[0]) => {
2700
+ let conversion = inferenceConversions.get(options.toolCallId);
2701
+ if (!conversion) {
2702
+ conversion = Promise.resolve().then(() => this.invokeGenerationCallback(() => convert(options)));
2703
+ inferenceConversions.set(options.toolCallId, conversion);
2704
+ }
2705
+ return conversion;
2706
+ },
2707
+ };
2708
+ }
2709
+
2348
2710
  const flushReasoning = (): void => {
2349
2711
  for (const [id, reasoningText] of reasoningTextById) {
2350
2712
  this.options.onReasoningEnd?.(id, reasoningText);
@@ -2353,15 +2715,19 @@ export class AgentSessionEngine implements IAgentSession {
2353
2715
  };
2354
2716
 
2355
2717
  while (true) {
2356
- const remainingSteps = maxSteps - stepCount;
2357
- if (remainingSteps <= 0) {
2718
+ const remainingSteps = maxSteps === undefined ? undefined : maxSteps - stepCount;
2719
+ if (remainingSteps !== undefined && remainingSteps <= 0) {
2358
2720
  throw new Error(`Agent generation exhausted its ${maxSteps} step limit during retry.`);
2359
2721
  }
2360
2722
  let streamError: unknown;
2361
2723
  let finished!: { text: string; finishReason: IAgentGenerateResult['finishReason'] };
2362
2724
  try {
2363
2725
  currentInferenceId = undefined;
2364
- inferenceText = '';
2726
+ inferenceContent = [];
2727
+ inferenceTextParts.clear();
2728
+ inferenceResultConverters.clear();
2729
+ inferenceConversions.clear();
2730
+ localToolConversionErrors.length = 0;
2365
2731
  const result = plugins.streamText({
2366
2732
  model: runtime.model,
2367
2733
  instructions: runtime.system,
@@ -2372,17 +2738,19 @@ export class AgentSessionEngine implements IAgentSession {
2372
2738
  ...(runtime.maxOutputTokens === undefined ? {} : { maxOutputTokens: runtime.maxOutputTokens }),
2373
2739
  // A step whose stream reported an error fails the model call, and no step follows it,
2374
2740
  // even when the provider still finished the stream and its tool calls executed.
2375
- stopWhen: [plugins.stepCountIs(remainingSteps), () => streamError !== undefined],
2741
+ stopWhen: [
2742
+ ...(remainingSteps === undefined ? [] : [plugins.stepCountIs(remainingSteps)]),
2743
+ () => streamError !== undefined,
2744
+ ],
2376
2745
  maxRetries: 0,
2377
2746
  abortSignal: combinedAbort.signal,
2378
2747
  ...(attachmentDownload ? { experimental_download: attachmentDownload } : {}),
2379
2748
  prepareStep: async () => {
2380
2749
  await applyPendingInput();
2381
- if (!automaticCompactionChecked && this.options.contextCompactor
2382
- && this.options.contextCompactionBytes !== false) {
2750
+ const compactionPoint = runtime.contextCompactionPoint;
2751
+ if (!automaticCompactionChecked && this.options.contextCompactor && 'tokens' in compactionPoint) {
2383
2752
  automaticCompactionChecked = true;
2384
- const bytes = new TextEncoder().encode(JSON.stringify(this.getModelMessages())).byteLength;
2385
- if (bytes > (this.options.contextCompactionBytes ?? 256 * 1024)) {
2753
+ if (this.measureContextTokens().tokens >= compactionPoint.tokens) {
2386
2754
  // The turns' tools let the summary call read the cached prefix; without a
2387
2755
  // cache they would only add input.
2388
2756
  await this.performCompaction(
@@ -2398,7 +2766,11 @@ export class AgentSessionEngine implements IAgentSession {
2398
2766
  }
2399
2767
  }
2400
2768
  beginInference();
2401
- return { messages: this.buildContext(this.eventsByIds(latestInferenceVisibleIds)) };
2769
+ const messages = this.buildContext(this.eventsByIds(latestInferenceVisibleIds));
2770
+ inferenceContext = options.transaction && this.options.contextCompactor
2771
+ ? { throughSequence: this.sequence, contextBytes: utf8JsonBytes(messages) }
2772
+ : undefined;
2773
+ return { messages };
2402
2774
  },
2403
2775
  onError: ({ error }) => {
2404
2776
  streamError = error;
@@ -2408,6 +2780,12 @@ export class AgentSessionEngine implements IAgentSession {
2408
2780
  },
2409
2781
  onLanguageModelCallEnd: ({ modelId, usage }) => {
2410
2782
  modelCalls.end(modelId, usage);
2783
+ const inputTokens = usage.inputTokens;
2784
+ // A call that ends after its generation's outcome was committed has nothing to update.
2785
+ if (inferenceContext && this.generationHandles.has(generationId) && typeof inputTokens === 'number'
2786
+ && Number.isSafeInteger(inputTokens) && inputTokens >= 0) {
2787
+ this.generationContextUsage.set(generationId, { inputTokens, ...inferenceContext });
2788
+ }
2411
2789
  },
2412
2790
  repairToolCall: async ({ toolCall, tools: availableTools, error }) => {
2413
2791
  const lowerName = toolCall.toolName.toLowerCase();
@@ -2424,11 +2802,55 @@ export class AgentSessionEngine implements IAgentSession {
2424
2802
  };
2425
2803
  },
2426
2804
  onChunk: ({ chunk }) => {
2805
+ if (chunk.type === 'text-start' || chunk.type === 'reasoning-start'
2806
+ || chunk.type === 'text-delta' || chunk.type === 'reasoning-delta'
2807
+ || chunk.type === 'text-end' || chunk.type === 'reasoning-end') {
2808
+ const type: 'text' | 'reasoning' = chunk.type.startsWith('text-') ? 'text' : 'reasoning';
2809
+ const key = `${type}:${chunk.id}`;
2810
+ const existingPart = inferenceTextParts.get(key);
2811
+ const part = existingPart ?? (type === 'text'
2812
+ ? { type: 'text' as const, text: '' }
2813
+ : { type: 'reasoning' as const, text: '' });
2814
+ if (!existingPart) {
2815
+ inferenceTextParts.set(key, part);
2816
+ inferenceContent.push(part);
2817
+ }
2818
+ if (chunk.type === 'text-delta' || chunk.type === 'reasoning-delta') part.text += chunk.text;
2819
+ if (chunk.providerMetadata !== undefined) part.providerOptions = chunk.providerMetadata;
2820
+ }
2821
+ if (chunk.type === 'tool-call') {
2822
+ inferenceContent.push({
2823
+ type: 'tool-call', toolCallId: chunk.toolCallId, toolName: chunk.toolName,
2824
+ input: chunk.input, providerExecuted: chunk.providerExecuted,
2825
+ providerOptions: chunk.providerMetadata,
2826
+ });
2827
+ }
2828
+ if ((chunk.type === 'tool-result' || chunk.type === 'tool-error')
2829
+ && chunk.providerExecuted === true
2830
+ && !(chunk.type === 'tool-result' && chunk.preliminary === true)) {
2831
+ const definition = allTools[chunk.toolName];
2832
+ const jsonError = chunk.type === 'tool-error'
2833
+ ? fallbackModelOutput(chunk.error) as Extract<TAgentToolResultOutput, { type: 'text' | 'json' }>
2834
+ : undefined;
2835
+ const output: TAgentToolResultOutput = chunk.type === 'tool-error'
2836
+ ? { type: 'error-json', value: jsonError!.value }
2837
+ : fallbackModelOutput(chunk.output);
2838
+ if (chunk.type === 'tool-result' && definition?.toModelOutput) {
2839
+ inferenceResultConverters.set(inferenceContent.length, async () => this.invokeGenerationCallback(
2840
+ () => definition.toModelOutput!({
2841
+ toolCallId: chunk.toolCallId, input: chunk.input, output: chunk.output,
2842
+ }),
2843
+ ));
2844
+ }
2845
+ inferenceContent.push({
2846
+ type: 'tool-result', toolCallId: chunk.toolCallId, toolName: chunk.toolName,
2847
+ output, providerOptions: chunk.providerMetadata,
2848
+ });
2849
+ }
2427
2850
  const chunkType = String((chunk as { type?: unknown }).type ?? '');
2428
2851
  if (chunkType === 'text-delta') {
2429
2852
  const textChunk = chunk as { delta?: string; textDelta?: string; text?: string };
2430
2853
  const delta = textChunk.delta ?? textChunk.textDelta ?? textChunk.text ?? '';
2431
- inferenceText += delta;
2432
2854
  this.options.onToken?.(delta);
2433
2855
  return;
2434
2856
  }
@@ -2503,180 +2925,33 @@ export class AgentSessionEngine implements IAgentSession {
2503
2925
  this.options.onReasoningEnd?.(id, text, reasoningChunk.providerMetadata);
2504
2926
  }
2505
2927
  },
2506
- onToolExecutionStart: ({ toolCall }) => {
2507
- const input = parseToolInput(toolCall.input);
2508
- const signature = toolSignature(toolCall.toolName, input);
2509
- const existing = preparations.get(toolCall.toolCallId);
2510
- if (existing) {
2511
- if (existing.signature !== signature) {
2512
- throw new Error(`Tool call id collision for "${toolCall.toolCallId}" in generation "${generationId}".`);
2513
- }
2514
- return existing.promise;
2928
+ onToolExecutionStart: ({ toolCall }) => prepareToolCall(toolCall),
2929
+ onToolExecutionEnd: finishToolCall,
2930
+ onStepEnd: async (step) => {
2931
+ endModelCallToolCalls();
2932
+ const earlyErrors = await retainUnadoptedEarlyExecutions();
2933
+ if (earlyErrors.length === 1) throw earlyErrors[0];
2934
+ if (earlyErrors.length > 1) {
2935
+ throw new AggregateError(earlyErrors, 'Retaining early tool executions failed.');
2515
2936
  }
2516
- const promise = Promise.resolve().then(async () => {
2517
- const startedBeforeInference = !currentInferenceId;
2518
- const inferenceId = currentInferenceId ?? beginInference();
2519
- const approvalAssistantEvent = startedBeforeInference
2520
- ? [...this.events].reverse().find((candidate): candidate is IAssistantMessageEvent =>
2521
- candidate.type === 'assistant-message'
2522
- && Array.isArray(candidate.content)
2523
- && candidate.content.some((part) =>
2524
- part.type === 'tool-call' && part.toolCallId === toolCall.toolCallId,
2525
- ),
2526
- )
2527
- : undefined;
2528
- let callEvent = [...this.events].reverse().find((event): event is IToolCallEvent =>
2529
- event.type === 'tool-call'
2530
- && event.toolCallId === toolCall.toolCallId
2531
- && (
2532
- event.generationId === generationId
2533
- || (
2534
- !!approvalAssistantEvent?.id
2535
- && event.parentIds?.includes(approvalAssistantEvent.id) === true
2536
- )
2537
- ));
2538
- if (callEvent && toolSignature(callEvent.toolName, callEvent.input) !== signature) {
2539
- throw new Error(`Tool call id collision for "${toolCall.toolCallId}" in generation "${generationId}".`);
2540
- }
2541
- if (!callEvent) {
2542
- callEvent = this.commitEvent(createAgentEvent<IToolCallEvent>({
2543
- type: 'tool-call',
2544
- toolCallId: toolCall.toolCallId,
2545
- toolName: toolCall.toolName,
2546
- input,
2547
- generationId,
2548
- inferenceId,
2549
- causationId: inferenceId,
2550
- parentIds: [inferenceId],
2551
- }));
2552
- }
2553
- if (options.transaction) {
2554
- let intent = [...this.events].reverse().find(
2555
- (event): event is IToolExecutionIntentEvent =>
2556
- event.type === 'tool-execution-intent'
2557
- && event.generationId === generationId
2558
- && event.toolCallId === toolCall.toolCallId,
2559
- );
2560
- if (intent && toolSignature(intent.toolName, intent.input) !== signature) {
2561
- throw new Error(`Tool call id collision for "${toolCall.toolCallId}" in generation "${generationId}".`);
2937
+ // Failed steps are committed from observations in the catch path, not from an
2938
+ // SDK response that may be incomplete or include unobserved content.
2939
+ if (streamError !== undefined) {
2940
+ // The SDK already converted these outputs. Reuse that conversion, but retain
2941
+ // only results we observed; abrupt streams use the deferred converter instead.
2942
+ for (const message of step.response.messages) {
2943
+ if (message.role !== 'assistant' || !Array.isArray(message.content)) continue;
2944
+ for (const part of message.content) {
2945
+ if (part.type !== 'tool-result') continue;
2946
+ const index = inferenceContent.findIndex((observed) =>
2947
+ observed.type === 'tool-result' && observed.toolCallId === part.toolCallId);
2948
+ if (index < 0) continue;
2949
+ inferenceContent[index] = part;
2950
+ inferenceResultConverters.delete(index);
2562
2951
  }
2563
- if (!intent) {
2564
- intent = this.commitEvent(createAgentEvent<IToolExecutionIntentEvent>({
2565
- type: 'tool-execution-intent',
2566
- generationId,
2567
- inferenceId: callEvent.inferenceId,
2568
- causationId: callEvent.id,
2569
- toolCallId: toolCall.toolCallId,
2570
- toolName: toolCall.toolName,
2571
- input,
2572
- parentIds: [callEvent.id],
2573
- }));
2574
- }
2575
- executionIntentIds.set(toolCall.toolCallId, intent.id);
2576
2952
  }
2577
- // One save makes the call and its execution intent durable before the tool runs.
2578
- await this.flushPersistence();
2579
- recordToolCall(toolCalls, toolCallIndexes, toolCall);
2580
- completedPreparations.add(toolCall.toolCallId);
2581
- const callbackEvent: IAgentToolCallStartEvent = {
2582
- toolCallId: toolCall.toolCallId,
2583
- toolName: toolCall.toolName,
2584
- input,
2585
- };
2586
- this.options.onToolCallStart?.(callbackEvent);
2587
- this.options.onToolCall?.(callbackEvent.toolName, callbackEvent.input);
2588
- });
2589
- preparations.set(toolCall.toolCallId, { signature, promise });
2590
- void promise.catch((error) => {
2591
- if (!completedPreparations.has(toolCall.toolCallId)) {
2592
- preparationErrors.set(toolCall.toolCallId, error);
2593
- }
2594
- });
2595
- return promise;
2596
- },
2597
- onToolExecutionEnd: async (finish) => {
2598
- toolScheduler.settle(finish.toolCall.toolCallId);
2599
- finishedToolCallIds.add(finish.toolCall.toolCallId);
2600
- preliminaryToolOutputs.delete(finish.toolCall.toolCallId);
2601
- const inferenceId = currentInferenceId ?? beginInference();
2602
- const previousResult = this.currentGenerationEvent(
2603
- generationId,
2604
- 'tool-result',
2605
- finish.toolCall.toolCallId,
2606
- );
2607
- if (this.currentGenerationEvent(
2608
- generationId,
2609
- 'tool-result',
2610
- finish.toolCall.toolCallId,
2611
- inferenceId,
2612
- )) return;
2613
- const input = parseToolInput(finish.toolCall.input);
2614
- const startEvent: IAgentToolCallStartEvent = {
2615
- toolCallId: finish.toolCall.toolCallId,
2616
- toolName: finish.toolCall.toolName,
2617
- input,
2618
- };
2619
- const finishEvent: TAgentToolCallFinishEvent = finish.toolOutput.type === 'tool-result'
2620
- ? { ...startEvent, success: true, output: finish.toolOutput.output }
2621
- : { ...startEvent, success: false, error: errorToString(finish.toolOutput.error) };
2622
- const parentCall = this.currentGenerationEvent(
2623
- generationId,
2624
- 'tool-call',
2625
- finish.toolCall.toolCallId,
2626
- inferenceId,
2627
- );
2628
- const toolWithModelOutput = allTools[finish.toolCall.toolName] as {
2629
- toModelOutput?: (options: {
2630
- toolCallId: string;
2631
- input: unknown;
2632
- output: unknown;
2633
- }) => Promise<TAgentToolResultOutput> | TAgentToolResultOutput;
2634
- } | undefined;
2635
- const modelOutput = finishEvent.success && toolWithModelOutput?.toModelOutput
2636
- ? await this.invokeGenerationCallback(
2637
- () => toolWithModelOutput.toModelOutput!({
2638
- toolCallId: finish.toolCall.toolCallId,
2639
- input,
2640
- output: finishEvent.output,
2641
- }),
2642
- )
2643
- : fallbackModelOutput(
2644
- finishEvent.success ? finishEvent.output : undefined,
2645
- finishEvent.success ? undefined : finishEvent.error,
2646
- );
2647
- const executionIntentId = executionIntentIds.get(finish.toolCall.toolCallId);
2648
- this.commitEvent(createAgentEvent<IToolResultEvent>({
2649
- type: 'tool-result',
2650
- toolCallId: finish.toolCall.toolCallId,
2651
- toolName: finish.toolCall.toolName,
2652
- ...(executionIntentId ? { executionIntentId } : {}),
2653
- output: finishEvent.success ? finishEvent.output : undefined,
2654
- error: finishEvent.success ? undefined : finishEvent.error,
2655
- modelOutput,
2656
- generationId,
2657
- inferenceId,
2658
- causationId: inferenceId,
2659
- parentIds: parentCall ? [parentCall.id] : [inferenceId],
2660
- }));
2661
- await this.flushPersistence();
2662
- recordToolCall(
2663
- toolCalls,
2664
- toolCallIndexes,
2665
- finish.toolCall,
2666
- finishEvent.success ? { output: finishEvent.output } : { error: finishEvent.error },
2667
- );
2668
- if (previousResult) return;
2669
- this.options.onToolCallFinish?.(finishEvent);
2670
- this.options.onToolResult?.(
2671
- finishEvent.toolName,
2672
- finishEvent.success ? finishEvent.output : undefined,
2673
- );
2674
- },
2675
- onStepEnd: async (step) => {
2676
- endModelCallToolCalls();
2677
- // The model call failed: it is retried or fails the generation, and like a call whose
2678
- // stream threw, its partial response is not committed.
2679
- if (streamError !== undefined) return;
2953
+ return;
2954
+ }
2680
2955
  if (!currentInferenceId) throw new Error('Model step finished outside an inference.');
2681
2956
  if (!completedInferenceIds.has(currentInferenceId)) {
2682
2957
  completedInferenceIds.add(currentInferenceId);
@@ -2718,17 +2993,29 @@ export class AgentSessionEngine implements IAgentSession {
2718
2993
  await this.flushPersistence();
2719
2994
  finished = { text, finishReason };
2720
2995
  } catch (error) {
2721
- // A read-only call that started early while the failed response streamed is discarded.
2996
+ // Abort unadopted calls and reject confirmation, but retain their observed outcomes.
2722
2997
  endModelCallToolCalls();
2998
+ const earlyErrors = await retainUnadoptedEarlyExecutions();
2723
2999
  modelCalls.settle(combinedAbort.signal.aborted ? 'aborted' : 'failed');
2724
3000
  flushReasoning();
2725
3001
  const effectiveError = streamError ?? error;
3002
+ const conversionErrors = [
3003
+ ...localToolConversionErrors,
3004
+ ...earlyErrors,
3005
+ ...await commitInterruptedInference(),
3006
+ ];
3007
+ if (conversionErrors.length > 0) {
3008
+ const cause = combinedAbort.signal.aborted
3009
+ ? combinedAbort.signal.reason ?? new DOMException('Aborted', 'AbortError')
3010
+ : effectiveError;
3011
+ const errors = [...new Set([cause, ...conversionErrors])];
3012
+ if (errors.length === 1) throw errors[0];
3013
+ throw new AggregateError(errors,
3014
+ `Inference interrupted and observed tool output conversion failed: ${errors.map(errorToString).join('; ')}`);
3015
+ }
2726
3016
  if (combinedAbort.signal.aborted) {
2727
- await commitInterruptedInference();
2728
3017
  throw combinedAbort.signal.reason ?? new DOMException('Aborted', 'AbortError');
2729
3018
  }
2730
- // A failed attempt is retried or fails the generation; its partial text is not kept.
2731
- inferenceText = '';
2732
3019
  if (effectiveError instanceof DOMException && effectiveError.name === 'AbortError') {
2733
3020
  throw effectiveError;
2734
3021
  }
@@ -2746,7 +3033,12 @@ export class AgentSessionEngine implements IAgentSession {
2746
3033
  stepCount++;
2747
3034
  }
2748
3035
 
2749
- const retry = stepCount < maxSteps
3036
+ // Provider execution is outside our local exactly-once ledger. Once observed, a
3037
+ // retry could repeat remote effects, even when its result never arrived.
3038
+ const providerExecutionObserved = inferenceContent.some((part) =>
3039
+ part.type === 'tool-call' && part.providerExecuted === true);
3040
+ if (providerExecutionObserved || unknownEarlyExecution) throw effectiveError;
3041
+ const retry = (maxSteps === undefined || stepCount < maxSteps)
2750
3042
  ? planModelCallRetry(effectiveError, {
2751
3043
  attempt,
2752
3044
  retriedMs,
@@ -2797,7 +3089,7 @@ export class AgentSessionEngine implements IAgentSession {
2797
3089
 
2798
3090
  // The model ended the generation. Input committed to it after its model's last look,
2799
3091
  // such as a background result, is taken in with another step while steps remain.
2800
- if (stepCount < maxSteps && (options.beforeFinish || this.generationBackgroundJobs?.entries.size)) {
3092
+ if ((maxSteps === undefined || stepCount < maxSteps) && (options.beforeFinish || this.generationBackgroundJobs?.entries.size)) {
2801
3093
  // Finish waits are scoped to this boundary. Steering, owned completions and the
2802
3094
  // bounded timer all lead back to normal inference without cancelling background work.
2803
3095
  const finishWait = new AbortController();
@@ -2853,7 +3145,7 @@ export class AgentSessionEngine implements IAgentSession {
2853
3145
  }
2854
3146
  if ((options.pendingInput?.size ?? 0) > 0) continue;
2855
3147
  }
2856
- if (stepCount < maxSteps && this.events.some((event) =>
3148
+ if ((maxSteps === undefined || stepCount < maxSteps) && this.events.some((event) =>
2857
3149
  event.type === 'user-message'
2858
3150
  && event.generationId === generationId
2859
3151
  && !latestInferenceVisibleIds.has(event.id))) {
@@ -2869,7 +3161,7 @@ export class AgentSessionEngine implements IAgentSession {
2869
3161
  messages: this.buildContext(this.eventsByIds(resultVisibleIds)),
2870
3162
  steps: stepCount,
2871
3163
  finishReason: finished.finishReason,
2872
- terminationReason: stepCount >= maxSteps && (
3164
+ terminationReason: maxSteps !== undefined && stepCount >= maxSteps && (
2873
3165
  finished.finishReason === 'tool-calls'
2874
3166
  || (this.generationBackgroundJobs?.entries.size ?? 0) > 0
2875
3167
  || (options.pendingInput?.size ?? 0) > 0
@@ -3079,6 +3371,7 @@ export class AgentSessionEngine implements IAgentSession {
3079
3371
  }
3080
3372
  }
3081
3373
  this.generationHandles.clear();
3374
+ this.generationContextUsage.clear();
3082
3375
  try {
3083
3376
  while (this.notificationDispatches.size > 0) {
3084
3377
  await Promise.all(this.notificationDispatches);