fascicle 0.8.12 → 0.8.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # Changelog
2
2
 
3
+ ## v0.8.13 — 2026-07-07
4
+
5
+ ### Added
6
+
7
+ - Two opt-in, provider-agnostic reliability options for `generate` that make local-model tool loops survivable. Both default to off, so existing behavior is unchanged.
8
+ - `tool_call_repair_attempts` (default `0`): when a step returns text with no structured tool calls, salvage a call the model emitted as assistant text in Hermes (`<tool_call>{...}</tool_call>`), bare/`json`-fenced, or Qwen3-Coder XML form. A candidate runs only if its name resolves and its arguments validate against the tool's `input_schema`, so plain JSON-in-prose never false-positives. Salvaged calls run the normal execute path and are marked `salvaged` (with `salvaged_format`) on their `ToolCallRecord`, plus a `tool_call_salvaged` trajectory event. The budget is shared across the whole call, including schema-repair passes.
9
+ - `max_tool_calls_per_step` (default unlimited, must be `>= 1`): execute only the first N tool calls of a step and drop the rest for that turn (the model can re-issue them next turn). Dropped calls surface as `ToolCallRecord`s with `error.message: 'dropped_max_tool_calls_per_step'` and a `tool_calls_dropped` event. Set to `1` for runtimes that mishandle parallel tool calls.
10
+ - Both options are threaded through `EngineDefaults` (same per-call-wins merge rule) and `model_call`. The subprocess `claude_cli` provider records `option_ignored` for each, since it does not run the shared tool loop.
11
+
3
12
  ## v0.8.12 — 2026-07-04
4
13
 
5
14
  ### Fixed
@@ -93,6 +93,7 @@ type CostBreakdown = {
93
93
  currency: 'USD';
94
94
  is_estimate: true;
95
95
  };
96
+ type SalvageFormat = 'hermes' | 'json' | 'qwen_xml';
96
97
  type ToolCallRecord = {
97
98
  id: string;
98
99
  name: string;
@@ -104,6 +105,12 @@ type ToolCallRecord = {
104
105
  };
105
106
  duration_ms: number;
106
107
  started_at: number;
108
+ /**
109
+ * Present only when the call was recovered from assistant text rather than
110
+ * returned structurally by the provider (tool_call_repair_attempts > 0).
111
+ */
112
+ salvaged?: true;
113
+ salvaged_format?: SalvageFormat;
107
114
  };
108
115
  type StepRecord = {
109
116
  index: number;
@@ -224,6 +231,20 @@ type GenerateOptions<t = string> = {
224
231
  retry?: RetryPolicy;
225
232
  tool_error_policy?: 'feed_back' | 'throw';
226
233
  schema_repair_attempts?: number;
234
+ /**
235
+ * Budget for salvaging tool calls the model emitted as assistant text
236
+ * instead of structured tool_calls (a common local-runtime failure).
237
+ * 0 (the default) disables salvage entirely. The budget is shared across
238
+ * the whole generate call, including schema-repair re-invocations.
239
+ */
240
+ tool_call_repair_attempts?: number;
241
+ /**
242
+ * Cap on tool calls executed per step. Calls beyond the cap are dropped
243
+ * for that step (the model can re-issue them next turn) and surfaced via
244
+ * ToolCallRecord errors and a tool_calls_dropped trajectory event.
245
+ * undefined (the default) leaves the count unlimited.
246
+ */
247
+ max_tool_calls_per_step?: number;
227
248
  on_tool_approval?: ToolApprovalHandler;
228
249
  provider_options?: Record<string, unknown>;
229
250
  };
@@ -291,7 +312,7 @@ type ProviderConfigMap = {
291
312
  * | --------------------------------------------------------------------- | ----------------------------------------- |
292
313
  * | `model` | per-call wins; else default; else `sonnet` |
293
314
  * | `provider` | per-call wins; else default; else sole configured provider; else `anthropic` |
294
- * | `system`, `effort`, `max_steps`, `tool_error_policy`, `schema_repair_attempts` | per-call wins (nullish coalesce) |
315
+ * | `system`, `effort`, `max_steps`, `tool_error_policy`, `schema_repair_attempts`, `tool_call_repair_attempts`, `max_tool_calls_per_step` | per-call wins (nullish coalesce) |
295
316
  * | `retry_policy` | per-call replaces wholesale |
296
317
  * | `provider_options` | two-level: per-provider-key shallow merge |
297
318
  * | `prompt`, `tools`, `schema`, `abort`, `trajectory`, `on_chunk` | not defaultable; always call-supplied |
@@ -305,6 +326,8 @@ type EngineDefaults = {
305
326
  readonly retry_policy?: RetryPolicy;
306
327
  readonly tool_error_policy?: 'feed_back' | 'throw';
307
328
  readonly schema_repair_attempts?: number;
329
+ readonly tool_call_repair_attempts?: number;
330
+ readonly max_tool_calls_per_step?: number;
308
331
  readonly provider_options?: Readonly<Record<string, Readonly<Record<string, unknown>>>>;
309
332
  };
310
333
  type EngineConfig = {
@@ -427,4 +450,4 @@ declare class provider_auth_error extends Error {
427
450
  }
428
451
  //#endregion
429
452
  export { RetryFailureKind as A, UserContentPart as B, GenerateResult as C, ProviderConfigMap as D, PricingTable as E, ToolApprovalHandler as F, ToolApprovalRequest as I, ToolCallRecord as L, StepRecord as M, StreamChunk as N, ProviderInit as O, Tool as P, ToolExecContext as R, GenerateOptions as S, Pricing as T, EffortLevel as _, on_chunk_error as a, EngineDefaults as b, provider_error as c, schema_validation_error as d, tool_approval_denied_error as f, CostBreakdown as g, AssistantContentPart as h, model_required_error as i, RetryPolicy as j, ResolvedModel as k, provider_not_configured_error as l, create_engine as m, engine_config_error as n, provider_auth_error as o, tool_error as p, engine_disposed_error as r, provider_capability_error as s, claude_cli_error as t, rate_limit_error as u, Engine as v, Message as w, FinishReason as x, EngineConfig as y, UsageTotals as z };
430
- //# sourceMappingURL=index-Cy7EUeiP.d.ts.map
453
+ //# sourceMappingURL=index-C3NcdaSY.d.ts.map
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { A as fallback, B as describe, C as checkpoint, D as LoopResult, E as LoopGuardResult, F as parallel, G as Step, H as run, I as sequence, J as TrajectoryLogger, K as StepMetadata, L as DescribeOptions, M as pipe, N as map, O as loop, P as branch, R as FlowNode, S as suspend, T as LoopConfig, U as CheckpointStore, V as step, W as RunContext, Y as __json_default_export, _ as suspended_error, a as SpanStartEvent, b as stash, c as span_end_event_schema, d as STEP_KINDS, f as StepKind, g as resume_validation_error, h as describe_cycle_error, i as SpanEndEvent, j as retry, k as timeout, l as span_start_event_schema, m as aborted_error, n as EmitEvent, o as custom_event_schema, p as is_step_kind, q as TrajectoryEvent, r as ParsedTrajectoryEvent, s as emit_event_schema, t as CustomTrajectoryEvent, u as trajectory_event_schema, v as timeout_error, w as compose, x as use, y as scope, z as FlowValue } from "./index-CEj6hY9z.js";
2
- import { A as RetryFailureKind, B as UserContentPart, C as GenerateResult, D as ProviderConfigMap, E as PricingTable, F as ToolApprovalHandler, I as ToolApprovalRequest, L as ToolCallRecord, M as StepRecord, N as StreamChunk, O as ProviderInit, P as Tool, R as ToolExecContext, S as GenerateOptions, T as Pricing, _ as EffortLevel, a as on_chunk_error, b as EngineDefaults, c as provider_error, d as schema_validation_error, f as tool_approval_denied_error, g as CostBreakdown, h as AssistantContentPart, i as model_required_error, j as RetryPolicy, k as ResolvedModel, l as provider_not_configured_error, m as create_engine, n as engine_config_error, o as provider_auth_error, p as tool_error, r as engine_disposed_error, s as provider_capability_error, t as claude_cli_error, u as rate_limit_error, v as Engine, w as Message, x as FinishReason, y as EngineConfig, z as UsageTotals } from "./index-Cy7EUeiP.js";
2
+ import { A as RetryFailureKind, B as UserContentPart, C as GenerateResult, D as ProviderConfigMap, E as PricingTable, F as ToolApprovalHandler, I as ToolApprovalRequest, L as ToolCallRecord, M as StepRecord, N as StreamChunk, O as ProviderInit, P as Tool, R as ToolExecContext, S as GenerateOptions, T as Pricing, _ as EffortLevel, a as on_chunk_error, b as EngineDefaults, c as provider_error, d as schema_validation_error, f as tool_approval_denied_error, g as CostBreakdown, h as AssistantContentPart, i as model_required_error, j as RetryPolicy, k as ResolvedModel, l as provider_not_configured_error, m as create_engine, n as engine_config_error, o as provider_auth_error, p as tool_error, r as engine_disposed_error, s as provider_capability_error, t as claude_cli_error, u as rate_limit_error, v as Engine, w as Message, x as FinishReason, y as EngineConfig, z as UsageTotals } from "./index-C3NcdaSY.js";
3
3
  import { z } from "zod";
4
4
 
5
5
  //#region src/composites/adversarial.d.ts
@@ -298,6 +298,8 @@ type ModelCallConfig<T = string> = {
298
298
  readonly retry_policy?: RetryPolicy;
299
299
  readonly tool_error_policy?: 'feed_back' | 'throw';
300
300
  readonly schema_repair_attempts?: number;
301
+ readonly tool_call_repair_attempts?: number;
302
+ readonly max_tool_calls_per_step?: number;
301
303
  readonly on_tool_approval?: ToolApprovalHandler;
302
304
  };
303
305
  declare function model_call<T = string>(cfg: ModelCallConfig<T>): Step<ModelCallInput, GenerateResult<T>>;
package/dist/index.js CHANGED
@@ -3098,6 +3098,37 @@ function record_schema_validation_failed(trajectory, meta) {
3098
3098
  raw_text: meta.raw_text
3099
3099
  });
3100
3100
  }
3101
+ /**
3102
+ * Emitted once per step whose tool calls were recovered from assistant text
3103
+ * rather than returned structurally (tool_call_repair_attempts). The per-call
3104
+ * tool_call/tool_result events still fire for each salvaged call; this event
3105
+ * answers why the step has calls the provider never emitted, and persists the
3106
+ * raw text for debugging (precedent: schema_validation_failed).
3107
+ */
3108
+ function record_tool_call_salvaged(trajectory, meta) {
3109
+ if (trajectory === void 0) return;
3110
+ trajectory.record({
3111
+ kind: "tool_call_salvaged",
3112
+ step_index: meta.step_index,
3113
+ calls: meta.calls,
3114
+ raw_text: meta.raw_text
3115
+ });
3116
+ }
3117
+ /**
3118
+ * Emitted when max_tool_calls_per_step drops calls beyond the cap. Dropped
3119
+ * calls also get ToolCallRecords with an error, but only this event carries
3120
+ * the configured cap alongside what was kept.
3121
+ */
3122
+ function record_tool_calls_dropped(trajectory, meta) {
3123
+ if (trajectory === void 0) return;
3124
+ trajectory.record({
3125
+ kind: "tool_calls_dropped",
3126
+ step_index: meta.step_index,
3127
+ max_tool_calls_per_step: meta.max_tool_calls_per_step,
3128
+ kept: meta.kept,
3129
+ dropped: meta.dropped
3130
+ });
3131
+ }
3101
3132
  function record_tool_approval(trajectory, kind, meta) {
3102
3133
  if (trajectory === void 0) return;
3103
3134
  trajectory.record({
@@ -3262,6 +3293,8 @@ function create_claude_cli_adapter(init) {
3262
3293
  if (opts.max_steps !== void 0) option_ignored.emit("max_steps", PROVIDER_NAME);
3263
3294
  if (opts.tool_error_policy !== void 0) option_ignored.emit("tool_error_policy", PROVIDER_NAME);
3264
3295
  if (opts.on_tool_approval !== void 0) option_ignored.emit("on_tool_approval", PROVIDER_NAME);
3296
+ if (opts.tool_call_repair_attempts !== void 0) option_ignored.emit("tool_call_repair_attempts", PROVIDER_NAME);
3297
+ if (opts.max_tool_calls_per_step !== void 0) option_ignored.emit("max_tool_calls_per_step", PROVIDER_NAME);
3265
3298
  if (count_user_messages(opts.prompt) >= 2) throw new provider_capability_error(PROVIDER_NAME, "multi_turn_history", "use provider_options.claude_cli.session_id instead");
3266
3299
  const tools_list = opts.tools ?? [];
3267
3300
  const tool_bridge = call_opts.tool_bridge ?? "allowlist_only";
@@ -3464,7 +3497,7 @@ function merge_provider_options(defaults, call) {
3464
3497
  for (const [provider, inner] of Object.entries(defaults)) out[provider] = { ...inner };
3465
3498
  for (const [provider, inner] of Object.entries(call)) {
3466
3499
  const existing = out[provider];
3467
- if (is_plain_object(existing) && is_plain_object(inner)) out[provider] = {
3500
+ if (is_plain_object$1(existing) && is_plain_object$1(inner)) out[provider] = {
3468
3501
  ...existing,
3469
3502
  ...inner
3470
3503
  };
@@ -3472,7 +3505,7 @@ function merge_provider_options(defaults, call) {
3472
3505
  }
3473
3506
  return out;
3474
3507
  }
3475
- function is_plain_object(value) {
3508
+ function is_plain_object$1(value) {
3476
3509
  return value !== null && typeof value === "object" && !Array.isArray(value);
3477
3510
  }
3478
3511
  //#endregion
@@ -3539,6 +3572,274 @@ function sum_usage(steps) {
3539
3572
  return total;
3540
3573
  }
3541
3574
  //#endregion
3575
+ //#region src/engine/tool_call_salvage.ts
3576
+ const TOOL_CALL_OPEN = "<tool_call>";
3577
+ const TOOL_CALL_CLOSE = "</tool_call>";
3578
+ const FUNCTION_RE = /<function=([^>\s]+)>([\s\S]*?)<\/function>/g;
3579
+ const PARAMETER_RE = /<parameter=([^>\s]+)>([\s\S]*?)<\/parameter>/g;
3580
+ const FENCE_RE = /```([^\n]*)\n([\s\S]*?)```/g;
3581
+ const EMPTY_BLOCK_RE = /<tool_call>\s*<\/tool_call>/g;
3582
+ /**
3583
+ * Find the extent of one balanced JSON object starting at `start_index`
3584
+ * (which must point at `{`), respecting strings and escapes so braces inside
3585
+ * string values do not affect the depth count. The scanner only finds the
3586
+ * extent; JSON.parse remains the sole semantic authority.
3587
+ */
3588
+ function scan_balanced_json(text, start_index) {
3589
+ if (text[start_index] !== "{") return void 0;
3590
+ let depth = 0;
3591
+ let in_string = false;
3592
+ let escaped = false;
3593
+ for (let i = start_index; i < text.length; i += 1) {
3594
+ const ch = text[i];
3595
+ if (in_string) {
3596
+ if (escaped) escaped = false;
3597
+ else if (ch === "\\") escaped = true;
3598
+ else if (ch === "\"") in_string = false;
3599
+ continue;
3600
+ }
3601
+ if (ch === "\"") in_string = true;
3602
+ else if (ch === "{") depth += 1;
3603
+ else if (ch === "}") {
3604
+ depth -= 1;
3605
+ if (depth === 0) {
3606
+ const end = i + 1;
3607
+ try {
3608
+ return {
3609
+ value: JSON.parse(text.slice(start_index, end)),
3610
+ end
3611
+ };
3612
+ } catch {
3613
+ return;
3614
+ }
3615
+ }
3616
+ }
3617
+ }
3618
+ }
3619
+ function is_plain_object(value) {
3620
+ return typeof value === "object" && value !== null && !Array.isArray(value);
3621
+ }
3622
+ /**
3623
+ * Exactly {name, arguments} with a string name and object arguments. This is
3624
+ * the shape firewall against prose that happens to contain JSON.
3625
+ */
3626
+ function call_shape(value) {
3627
+ if (!is_plain_object(value)) return void 0;
3628
+ if (Object.keys(value).length !== 2) return void 0;
3629
+ const name = value["name"];
3630
+ const args = value["arguments"];
3631
+ if (typeof name !== "string" || !is_plain_object(args)) return void 0;
3632
+ return {
3633
+ name,
3634
+ args
3635
+ };
3636
+ }
3637
+ function overlaps_any(span, masks) {
3638
+ return masks.some((m) => span.start < m.end && m.start < span.end);
3639
+ }
3640
+ function mask_containing(masks, index) {
3641
+ return masks.find((m) => index >= m.start && index < m.end);
3642
+ }
3643
+ function trim_one_newline(value) {
3644
+ let out = value;
3645
+ if (out.startsWith("\n")) out = out.slice(1);
3646
+ if (out.endsWith("\n")) out = out.slice(0, -1);
3647
+ return out;
3648
+ }
3649
+ function scan_qwen_functions(inner_start, inner) {
3650
+ const out = [];
3651
+ for (const fn of inner.matchAll(FUNCTION_RE)) {
3652
+ const body = fn[2] ?? "";
3653
+ const params = {};
3654
+ let leftover = body;
3655
+ for (const pm of body.matchAll(PARAMETER_RE)) {
3656
+ params[pm[1] ?? ""] = trim_one_newline(pm[2] ?? "");
3657
+ leftover = leftover.replace(pm[0], "");
3658
+ }
3659
+ if (leftover.trim().length !== 0) continue;
3660
+ out.push({
3661
+ name: fn[1] ?? "",
3662
+ args: params,
3663
+ format: "qwen_xml",
3664
+ span: {
3665
+ start: inner_start + fn.index,
3666
+ end: inner_start + fn.index + fn[0].length
3667
+ }
3668
+ });
3669
+ }
3670
+ return out;
3671
+ }
3672
+ function scan_tool_call_blocks(text) {
3673
+ const candidates = [];
3674
+ const masks = [];
3675
+ let pos = 0;
3676
+ while (true) {
3677
+ const open = text.indexOf(TOOL_CALL_OPEN, pos);
3678
+ if (open === -1) break;
3679
+ const close = text.indexOf(TOOL_CALL_CLOSE, open + 11);
3680
+ if (close === -1) {
3681
+ masks.push({
3682
+ start: open,
3683
+ end: text.length
3684
+ });
3685
+ break;
3686
+ }
3687
+ const block_end = close + 12;
3688
+ masks.push({
3689
+ start: open,
3690
+ end: block_end
3691
+ });
3692
+ const inner_start = open + 11;
3693
+ const inner = text.slice(inner_start, close);
3694
+ const trimmed = inner.trim();
3695
+ if (trimmed.startsWith("<function=")) candidates.push(...scan_qwen_functions(inner_start, inner));
3696
+ else if (trimmed.startsWith("{")) {
3697
+ const brace = text.indexOf("{", inner_start);
3698
+ const scanned = brace === -1 ? void 0 : scan_balanced_json(text, brace);
3699
+ const shaped = scanned === void 0 ? void 0 : call_shape(scanned.value);
3700
+ if (shaped !== void 0) candidates.push({
3701
+ name: shaped.name,
3702
+ args: shaped.args,
3703
+ format: "hermes",
3704
+ span: {
3705
+ start: open,
3706
+ end: block_end
3707
+ }
3708
+ });
3709
+ }
3710
+ pos = block_end;
3711
+ }
3712
+ return {
3713
+ candidates,
3714
+ masks
3715
+ };
3716
+ }
3717
+ function scan_fences(text, block_masks) {
3718
+ const candidates = [];
3719
+ const masks = [];
3720
+ for (const m of text.matchAll(FENCE_RE)) {
3721
+ const span = {
3722
+ start: m.index,
3723
+ end: m.index + m[0].length
3724
+ };
3725
+ if (overlaps_any(span, block_masks)) continue;
3726
+ masks.push(span);
3727
+ const info = (m[1] ?? "").trim();
3728
+ if (info !== "" && info !== "json") continue;
3729
+ let value;
3730
+ try {
3731
+ value = JSON.parse((m[2] ?? "").trim());
3732
+ } catch {
3733
+ continue;
3734
+ }
3735
+ const shaped = call_shape(value);
3736
+ if (shaped === void 0) continue;
3737
+ candidates.push({
3738
+ name: shaped.name,
3739
+ args: shaped.args,
3740
+ format: "json",
3741
+ span
3742
+ });
3743
+ }
3744
+ return {
3745
+ candidates,
3746
+ masks
3747
+ };
3748
+ }
3749
+ function scan_bare_json(text, masks) {
3750
+ const out = [];
3751
+ let i = 0;
3752
+ while (i < text.length) {
3753
+ const masked = mask_containing(masks, i);
3754
+ if (masked !== void 0) {
3755
+ i = masked.end;
3756
+ continue;
3757
+ }
3758
+ if (text[i] !== "{") {
3759
+ i += 1;
3760
+ continue;
3761
+ }
3762
+ const scanned = scan_balanced_json(text, i);
3763
+ if (scanned === void 0) {
3764
+ i += 1;
3765
+ continue;
3766
+ }
3767
+ const span = {
3768
+ start: i,
3769
+ end: scanned.end
3770
+ };
3771
+ const shaped = overlaps_any(span, masks) ? void 0 : call_shape(scanned.value);
3772
+ if (shaped !== void 0) out.push({
3773
+ name: shaped.name,
3774
+ args: shaped.args,
3775
+ format: "json",
3776
+ span
3777
+ });
3778
+ i = scanned.end;
3779
+ }
3780
+ return out;
3781
+ }
3782
+ function validate_candidate(candidate, tool_map) {
3783
+ const tool = tool_map.get(candidate.name);
3784
+ if (tool === void 0) return void 0;
3785
+ const schema = tool.input_schema;
3786
+ const raw = schema.safeParse(candidate.args);
3787
+ if (raw.success) return {
3788
+ name: candidate.name,
3789
+ input: raw.data,
3790
+ format: candidate.format,
3791
+ span: candidate.span
3792
+ };
3793
+ if (candidate.format !== "qwen_xml") return void 0;
3794
+ const coerced = {};
3795
+ for (const [k, v] of Object.entries(candidate.args)) {
3796
+ if (typeof v !== "string") {
3797
+ coerced[k] = v;
3798
+ continue;
3799
+ }
3800
+ try {
3801
+ coerced[k] = JSON.parse(v);
3802
+ } catch {
3803
+ coerced[k] = v;
3804
+ }
3805
+ }
3806
+ const retried = schema.safeParse(coerced);
3807
+ if (!retried.success) return void 0;
3808
+ return {
3809
+ name: candidate.name,
3810
+ input: retried.data,
3811
+ format: candidate.format,
3812
+ span: candidate.span
3813
+ };
3814
+ }
3815
+ function strip_spans(text, spans) {
3816
+ let out = text;
3817
+ const descending = spans.toSorted((a, b) => b.start - a.start);
3818
+ for (const s of descending) out = out.slice(0, s.start) + out.slice(s.end);
3819
+ return out.replace(EMPTY_BLOCK_RE, "").trim();
3820
+ }
3821
+ function salvage_tool_calls(text, tool_map) {
3822
+ if (text.length === 0) return void 0;
3823
+ const blocks = scan_tool_call_blocks(text);
3824
+ const fences = scan_fences(text, blocks.masks);
3825
+ const all_masks = [...blocks.masks, ...fences.masks];
3826
+ const candidates = [
3827
+ ...blocks.candidates,
3828
+ ...fences.candidates,
3829
+ ...scan_bare_json(text, all_masks)
3830
+ ].toSorted((a, b) => a.span.start - b.span.start);
3831
+ const calls = [];
3832
+ for (const c of candidates) {
3833
+ const validated = validate_candidate(c, tool_map);
3834
+ if (validated !== void 0) calls.push(validated);
3835
+ }
3836
+ if (calls.length === 0) return void 0;
3837
+ return {
3838
+ calls,
3839
+ stripped_text: strip_spans(text, calls.map((c) => c.span))
3840
+ };
3841
+ }
3842
+ //#endregion
3542
3843
  //#region src/engine/tool_loop.ts
3543
3844
  function throw_if_aborted(abort, step_index) {
3544
3845
  if (!abort.aborted) return;
@@ -3732,10 +4033,68 @@ async function run_tool_loop(config) {
3732
4033
  end_step_span(config.trajectory, step_span, { error: message });
3733
4034
  throw err;
3734
4035
  }
4036
+ let effective_calls = turn.tool_calls;
4037
+ let history_text = turn.text;
4038
+ let salvage;
4039
+ const salvaged_formats = /* @__PURE__ */ new Map();
4040
+ if (turn.tool_calls.length === 0 && (turn.finish_reason === "stop" || turn.finish_reason === "length") && config.tools.length > 0 && config.salvage_budget !== void 0 && config.salvage_budget.remaining > 0) {
4041
+ salvage = salvage_tool_calls(turn.text, tool_map);
4042
+ if (salvage !== void 0) {
4043
+ config.salvage_budget.remaining -= 1;
4044
+ history_text = salvage.stripped_text;
4045
+ effective_calls = salvage.calls.map((c, n) => {
4046
+ const id = `salvaged_${step_index}_${n}`;
4047
+ salvaged_formats.set(id, c.format);
4048
+ return {
4049
+ id,
4050
+ name: c.name,
4051
+ input: c.input
4052
+ };
4053
+ });
4054
+ record_tool_call_salvaged(config.trajectory, {
4055
+ step_index,
4056
+ calls: effective_calls.map((c) => ({
4057
+ tool_call_id: c.id,
4058
+ name: c.name,
4059
+ format: salvaged_formats.get(c.id) ?? "json"
4060
+ })),
4061
+ raw_text: turn.text
4062
+ });
4063
+ if (config.dispatch_chunk !== void 0) for (const c of effective_calls) {
4064
+ await config.dispatch_chunk({
4065
+ kind: "tool_call_start",
4066
+ id: c.id,
4067
+ name: c.name,
4068
+ step_index
4069
+ });
4070
+ await config.dispatch_chunk({
4071
+ kind: "tool_call_end",
4072
+ id: c.id,
4073
+ input: c.input,
4074
+ step_index
4075
+ });
4076
+ }
4077
+ }
4078
+ }
4079
+ let dropped_calls = [];
4080
+ const per_step_cap = config.max_tool_calls_per_step;
4081
+ if (per_step_cap !== void 0 && effective_calls.length > per_step_cap) {
4082
+ dropped_calls = effective_calls.slice(per_step_cap);
4083
+ effective_calls = effective_calls.slice(0, per_step_cap);
4084
+ record_tool_calls_dropped(config.trajectory, {
4085
+ step_index,
4086
+ max_tool_calls_per_step: per_step_cap,
4087
+ kept: effective_calls.length,
4088
+ dropped: dropped_calls.map((d) => ({
4089
+ tool_call_id: d.id,
4090
+ name: d.name
4091
+ }))
4092
+ });
4093
+ }
3735
4094
  const step_tool_records = [];
3736
- const assistant_message = build_assistant_message(turn.text, turn.tool_calls);
4095
+ const assistant_message = build_assistant_message(history_text, effective_calls);
3737
4096
  config.messages.push(assistant_message);
3738
- if (turn.tool_calls.length === 0) {
4097
+ if (effective_calls.length === 0) {
3739
4098
  text = turn.text;
3740
4099
  finish_reason = turn.finish_reason;
3741
4100
  const record_step = {
@@ -3757,7 +4116,7 @@ async function run_tool_loop(config) {
3757
4116
  }
3758
4117
  const would_exceed_after = step_index + 1 >= config.max_steps;
3759
4118
  const tool_results_to_feed = [];
3760
- for (const raw_call of turn.tool_calls) {
4119
+ for (const raw_call of effective_calls) {
3761
4120
  throw_if_aborted_in_flight(config.abort, step_index, {
3762
4121
  id: raw_call.id,
3763
4122
  name: raw_call.name
@@ -3962,6 +4321,26 @@ async function run_tool_loop(config) {
3962
4321
  });
3963
4322
  await dispatch_tool_result_chunk(config.dispatch_chunk, step_index, raw_call.id, output);
3964
4323
  }
4324
+ for (const d of dropped_calls) {
4325
+ const record = {
4326
+ id: d.id,
4327
+ name: d.name,
4328
+ input: d.input,
4329
+ error: { message: "dropped_max_tool_calls_per_step" },
4330
+ duration_ms: 0,
4331
+ started_at: Date.now()
4332
+ };
4333
+ step_tool_records.push(record);
4334
+ tool_calls_all.push(record);
4335
+ await dispatch_tool_result_chunk(config.dispatch_chunk, step_index, d.id, void 0, { message: "dropped_max_tool_calls_per_step" });
4336
+ }
4337
+ for (const r of step_tool_records) {
4338
+ const format = salvaged_formats.get(r.id);
4339
+ if (format !== void 0) {
4340
+ r.salvaged = true;
4341
+ r.salvaged_format = format;
4342
+ }
4343
+ }
3965
4344
  for (const r of step_tool_records) record_tool_result(config.trajectory, {
3966
4345
  step_index,
3967
4346
  name: r.name,
@@ -3970,7 +4349,7 @@ async function run_tool_loop(config) {
3970
4349
  ...r.error !== void 0 ? { error: r.error } : { output: r.output }
3971
4350
  });
3972
4351
  for (const m of tool_results_to_feed) config.messages.push(m);
3973
- const turn_finish_reason = would_exceed_after ? "max_steps" : turn.finish_reason;
4352
+ const turn_finish_reason = would_exceed_after ? "max_steps" : salvage !== void 0 ? "tool_calls" : turn.finish_reason;
3974
4353
  const step_record = {
3975
4354
  index: step_index,
3976
4355
  text: turn.text,
@@ -4399,6 +4778,10 @@ async function generate(opts_in, engine) {
4399
4778
  const max_steps = opts.max_steps ?? engine.default_max_steps;
4400
4779
  const tool_error_policy = opts.tool_error_policy ?? engine.default_tool_error_policy ?? "feed_back";
4401
4780
  const schema_repair_attempts = opts.schema_repair_attempts ?? engine.default_schema_repair_attempts ?? 1;
4781
+ const tool_call_repair_attempts = opts.tool_call_repair_attempts ?? engine.default_tool_call_repair_attempts ?? 0;
4782
+ if (tool_call_repair_attempts < 0) throw new engine_config_error("tool_call_repair_attempts must be >= 0");
4783
+ const max_tool_calls_per_step = opts.max_tool_calls_per_step ?? engine.default_max_tool_calls_per_step;
4784
+ if (max_tool_calls_per_step !== void 0 && max_tool_calls_per_step < 1) throw new engine_config_error("max_tool_calls_per_step must be >= 1");
4402
4785
  const retry_policy = opts.retry ?? engine.default_retry;
4403
4786
  const schema_prefix = opts.schema !== void 0 ? "You must respond with a single JSON value that conforms to the expected schema. Return ONLY the JSON value, with no markdown or commentary." : void 0;
4404
4787
  const initial_messages = build_initial_messages(opts);
@@ -4495,6 +4878,7 @@ async function generate(opts_in, engine) {
4495
4878
  let repair_remaining = schema_repair_attempts;
4496
4879
  let content_parsed;
4497
4880
  let schema_satisfied = opts.schema === void 0;
4881
+ const salvage_budget = tool_call_repair_attempts > 0 ? { remaining: tool_call_repair_attempts } : void 0;
4498
4882
  try {
4499
4883
  while (true) {
4500
4884
  const loop_result = await run_tool_loop({
@@ -4512,7 +4896,9 @@ async function generate(opts_in, engine) {
4512
4896
  provider: target.provider,
4513
4897
  model_id: target.model_id,
4514
4898
  resolve_pricing,
4515
- pricing_dedup
4899
+ pricing_dedup,
4900
+ ...salvage_budget !== void 0 ? { salvage_budget } : {},
4901
+ ...max_tool_calls_per_step !== void 0 ? { max_tool_calls_per_step } : {}
4516
4902
  });
4517
4903
  for (const s of loop_result.steps) steps_accum.push(s);
4518
4904
  for (const tc of loop_result.tool_calls) tool_calls_accum.push(tc);
@@ -4645,6 +5031,8 @@ function create_engine(config) {
4645
5031
  const default_retry = defaults?.retry_policy ?? config.default_retry ?? DEFAULT_RETRY;
4646
5032
  const default_effort = defaults?.effort ?? config.default_effort ?? "none";
4647
5033
  const default_max_steps = defaults?.max_steps ?? config.default_max_steps ?? 10;
5034
+ if (defaults?.tool_call_repair_attempts !== void 0 && defaults.tool_call_repair_attempts < 0) throw new engine_config_error("defaults.tool_call_repair_attempts must be >= 0");
5035
+ if (defaults?.max_tool_calls_per_step !== void 0 && defaults.max_tool_calls_per_step < 1) throw new engine_config_error("defaults.max_tool_calls_per_step must be >= 1");
4648
5036
  const get_internals = () => ({
4649
5037
  pricing,
4650
5038
  adapters,
@@ -4656,6 +5044,8 @@ function create_engine(config) {
4656
5044
  ...defaults?.system !== void 0 ? { default_system: defaults.system } : {},
4657
5045
  ...defaults?.tool_error_policy !== void 0 ? { default_tool_error_policy: defaults.tool_error_policy } : {},
4658
5046
  ...defaults?.schema_repair_attempts !== void 0 ? { default_schema_repair_attempts: defaults.schema_repair_attempts } : {},
5047
+ ...defaults?.tool_call_repair_attempts !== void 0 ? { default_tool_call_repair_attempts: defaults.tool_call_repair_attempts } : {},
5048
+ ...defaults?.max_tool_calls_per_step !== void 0 ? { default_max_tool_calls_per_step: defaults.max_tool_calls_per_step } : {},
4659
5049
  ...defaults?.provider_options !== void 0 ? { default_provider_options: defaults.provider_options } : {}
4660
5050
  });
4661
5051
  let disposed = false;
@@ -4787,6 +5177,8 @@ function model_call(cfg) {
4787
5177
  if (cfg.retry_policy !== void 0) opts.retry = cfg.retry_policy;
4788
5178
  if (cfg.tool_error_policy !== void 0) opts.tool_error_policy = cfg.tool_error_policy;
4789
5179
  if (cfg.schema_repair_attempts !== void 0) opts.schema_repair_attempts = cfg.schema_repair_attempts;
5180
+ if (cfg.tool_call_repair_attempts !== void 0) opts.tool_call_repair_attempts = cfg.tool_call_repair_attempts;
5181
+ if (cfg.max_tool_calls_per_step !== void 0) opts.max_tool_calls_per_step = cfg.max_tool_calls_per_step;
4790
5182
  if (cfg.on_tool_approval !== void 0) opts.on_tool_approval = cfg.on_tool_approval;
4791
5183
  if (ctx.streaming) opts.on_chunk = (chunk) => {
4792
5184
  ctx.emit({