@theoremjs/agents 0.3.4-next.811.gf4e3b3f → 0.3.4-next.815.g7f5fa76

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.
@@ -157,7 +157,10 @@ function withPublicLabels(event, labels) {
157
157
  event.tool.phase === 'error' &&
158
158
  event.tool.failure.error === undefined) {
159
159
  const { tool } = event;
160
- const error = kindText(tool.failure.kind, labels, { tool: tool.name });
160
+ // why: a tool's network failure names the tool; error.network names the model provider.
161
+ const error = tool.failure.kind === 'network'
162
+ ? labelText('tool.unreachable', { tool: tool.name }, labels)
163
+ : kindText(tool.failure.kind, labels, { tool: tool.name });
161
164
  return { ...event, tool: { ...tool, failure: { ...tool.failure, error } } };
162
165
  }
163
166
  return event;
@@ -1,7 +1,7 @@
1
1
  /** Values that fill a label template's `{param}` placeholders. */
2
2
  export type LabelParams = Record<string, string | number>;
3
3
  /** Keys are stable API. */
4
- export declare const LABEL_KEYS: readonly ["provider.decision_failed", "provider.decision_unavailable", "provider.decision_model_missing", "provider.decision_ended", "provider.decision_images_off", "provider.decision_box_model", "provider.budget_exhausted", "provider.adapter_version", "provider.adapter_methods", "provider.adapter_schemas", "provider.fallback_slot", "provider.model_fallback_slot", "provider.host_model", "turn.media_refused_image", "turn.media_refused_video", "turn.media_refused_file", "provider.turn_profile", "provider.model_binding_missing", "provider.session_closed", "provider.settled_call", "provider.session_unavailable", "provider.live_input", "provider.tool_cancel_missing", "provider.kernel_gate", "provider.event_invalid", "provider.response_identity", "provider.credential_resolution", "provider.credential_shape", "provider.stored_continuation", "provider.continuation_mismatch", "provider.continuation_contract", "provider.stateless_history", "provider.events_after_completion", "provider.duplicate_call", "provider.tool_cancel_live", "provider.complete_unavailable", "provider.parallel_tools", "provider.binding_identity", "provider.profile_type", "provider.event_type", "provider.feature", "provider.input_kind", "provider.output_kind", "provider.builtin", "provider.credential_missing", "provider.profile_configuration", "continue.instruction", "canary.bind_note", "user_data.note", "context.note", "taint.blocked", "taint.reason_steered", "taint.reason_tainted", "taint.destination_blocked", "taint.destination_confirm", "advisory.notice_elevated", "advisory.notice_high", "advisory.guidance", "attachments.too_many_files", "attachments.too_many_of_kind", "attachments.file_too_large", "attachments.turn_too_large", "attachments.not_accepted", "attachments.mime_not_allowed", "attachments.limits_unconfigured", "quota.exhausted", "quota.busy", "error.config", "error.request", "error.conflict", "error.not_found", "error.input", "error.action", "error.auth", "error.rate_limit", "error.busy", "error.unsupported", "error.unavailable", "error.bad_response", "error.network", "error.timeout", "error.safety", "error.blocked", "error.declined", "error.failed", "error.cancelled", "error.internal", "repair.default_guidance", "repair.items_guidance", "repair.prompt_header", "repair.prompt_intro", "repair.prompt_instructions", "repair.history_heading", "repair.section_previous_output", "repair.section_validator_rejection", "repair.section_repair_guidance", "repair.section_instructions", "structured.answer_tool", "structured.answer_missing", "structured.cut_off", "structured.not_json", "compaction.request", "compaction.tool_call", "compaction.tool_result", "compaction.tool_files", "decision.box_image", "detect.blocked", "detect.call_blocked", "detect.output_blocked", "detect.shown_blocked", "detect.hint.ids", "detect.hint.financial", "detect.hint.network", "detect.hint.credentials", "detect.hint.injection", "detect.hint.tool_instructions", "detect.hint.canary_leak", "detect.hint.prompt_leak", "detect.hint.marker_leak", "detect.hint.ungiven_images", "detect.hint.ungiven_links", "detect.hint.tool_leak", "detect.hint.own", "egress.default_repair_guidance", "egress.refusal", "egress.refusal_cut", "egress.rejection", "egress.rejection_found", "egress.hint_unscannable", "egress.hint_provider_tool_leak", "thought.omitted_image", "thought.omitted_link", "thought.omitted_instructions", "session.abandon_gated", "session.tool_denied", "session.tool_aborted", "session.sign_in", "sign_in.link", "sign_in.pending", "sign_in.done", "sign_in.declined", "sign_in.expired", "sign_in.out_of_scope", "session.gate_expired", "session.provider_state_unowned", "session.conversation_unknown", "session.full", "session.busy", "session.turn_ended", "session.gate_pending", "session.part_skipped", "live.session_ended", "voice.unsupported", "voice.permission", "voice.unavailable", "voice.failed", "voice.empty", "stop.cancelled", "stop.interrupted", "stop.length", "stop.filtered", "stop.provider_error", "stop.stream_incomplete", "stop.step_limit", "composer.model", "composer.effort", "tool.awaiting_user", "tool.completed_hidden", "tool.t2_loader_needs_snapshot", "tool.t2_loader_shape", "tool.t2_loader_output_invalid", "tool.execution_uncertain", "tool.input_invalid", "tool.input_invalid_after_mutate", "tool.output_invalid_after_mutate", "tool.handler_no_output", "tool.did_not_run", "tool.page_no_answer", "tool.page_timed_out", "tool.output_invalid", "tool.not_loaded_t2", "tool.wait_unknown", "tool.wait_cycle", "tool.after_field", "tool.background_field", "tool.background_started", "tool.late_result", "tool.not_visible", "tool.builtin_not_enabled", "tool.provider_native", "tool.not_registered", "tool.builtin_needs_snapshot", "tool.not_allowed", "tool.not_eligible", "tool.unsupported_type", "tool.agent_call_limit", "tool.agent_failed", "tool.activity_failed", "protocol.not_served", "protocol.profile_type", "protocol.options", "protocol.option_clash", "protocol.base_options", "protocol.id_clash", "protocol.ws_headers", "protocol.builtin_unwritten", "protocol.provider_missing", "protocol.connection", "protocol.part_schema", "protocol.part_invalid", "protocol.key_invalid", "protocol.no_key", "protocol.reply_shape", "protocol.endpoint_url", "protocol.endpoint_host", "protocol.http", "protocol.no_operation", "protocol.definition", "protocol.provider_id_clash", "protocol.history_needed"];
4
+ export declare const LABEL_KEYS: readonly ["provider.decision_failed", "provider.decision_unavailable", "provider.decision_model_missing", "provider.decision_ended", "provider.decision_images_off", "provider.decision_box_model", "provider.budget_exhausted", "provider.adapter_version", "provider.adapter_methods", "provider.adapter_schemas", "provider.fallback_slot", "provider.model_fallback_slot", "provider.host_model", "turn.media_refused_image", "turn.media_refused_video", "turn.media_refused_file", "provider.turn_profile", "provider.model_binding_missing", "provider.session_closed", "provider.settled_call", "provider.session_unavailable", "provider.live_input", "provider.tool_cancel_missing", "provider.kernel_gate", "provider.event_invalid", "provider.response_identity", "provider.credential_resolution", "provider.credential_shape", "provider.stored_continuation", "provider.continuation_mismatch", "provider.continuation_contract", "provider.stateless_history", "provider.events_after_completion", "provider.duplicate_call", "provider.tool_cancel_live", "provider.complete_unavailable", "provider.parallel_tools", "provider.binding_identity", "provider.profile_type", "provider.event_type", "provider.feature", "provider.input_kind", "provider.output_kind", "provider.builtin", "provider.credential_missing", "provider.profile_configuration", "continue.instruction", "canary.bind_note", "user_data.note", "context.note", "taint.blocked", "taint.reason_steered", "taint.reason_tainted", "taint.destination_blocked", "taint.destination_confirm", "advisory.notice_elevated", "advisory.notice_high", "advisory.guidance", "attachments.too_many_files", "attachments.too_many_of_kind", "attachments.file_too_large", "attachments.turn_too_large", "attachments.not_accepted", "attachments.mime_not_allowed", "attachments.limits_unconfigured", "quota.exhausted", "quota.busy", "error.config", "error.request", "error.conflict", "error.not_found", "error.input", "error.action", "error.auth", "error.rate_limit", "error.busy", "error.unsupported", "error.unavailable", "error.bad_response", "error.network", "error.timeout", "error.safety", "error.blocked", "error.declined", "error.failed", "error.cancelled", "error.internal", "repair.default_guidance", "repair.items_guidance", "repair.cut_off_guidance", "repair.prompt_header", "repair.prompt_intro", "repair.prompt_instructions", "repair.history_heading", "repair.section_previous_output", "repair.section_validator_rejection", "repair.section_repair_guidance", "repair.section_instructions", "structured.answer_tool", "structured.answer_missing", "structured.cut_off", "structured.not_json", "compaction.request", "compaction.tool_call", "compaction.tool_result", "compaction.tool_files", "decision.box_image", "detect.blocked", "detect.call_blocked", "detect.output_blocked", "detect.shown_blocked", "detect.hint.ids", "detect.hint.financial", "detect.hint.network", "detect.hint.credentials", "detect.hint.injection", "detect.hint.tool_instructions", "detect.hint.canary_leak", "detect.hint.prompt_leak", "detect.hint.marker_leak", "detect.hint.ungiven_images", "detect.hint.ungiven_links", "detect.hint.tool_leak", "detect.hint.own", "egress.default_repair_guidance", "egress.refusal", "egress.refusal_cut", "egress.rejection", "egress.rejection_found", "egress.hint_unscannable", "egress.hint_provider_tool_leak", "thought.omitted_image", "thought.omitted_link", "thought.omitted_instructions", "session.abandon_gated", "session.tool_denied", "session.tool_aborted", "session.sign_in", "sign_in.link", "sign_in.pending", "sign_in.done", "sign_in.declined", "sign_in.expired", "sign_in.out_of_scope", "session.gate_expired", "session.provider_state_unowned", "session.conversation_unknown", "session.full", "session.busy", "session.turn_ended", "session.gate_pending", "session.part_skipped", "live.session_ended", "voice.unsupported", "voice.permission", "voice.unavailable", "voice.failed", "voice.empty", "stop.cancelled", "stop.interrupted", "stop.length", "stop.filtered", "stop.provider_error", "stop.stream_incomplete", "stop.step_limit", "composer.model", "composer.effort", "tool.awaiting_user", "tool.completed_hidden", "tool.t2_loader_needs_snapshot", "tool.t2_loader_shape", "tool.t2_loader_output_invalid", "tool.execution_uncertain", "tool.input_invalid", "tool.input_invalid_after_mutate", "tool.output_invalid_after_mutate", "tool.handler_no_output", "tool.did_not_run", "tool.unreachable", "tool.page_no_answer", "tool.page_timed_out", "tool.output_invalid", "tool.not_loaded_t2", "tool.wait_unknown", "tool.wait_cycle", "tool.after_field", "tool.background_field", "tool.background_started", "tool.late_result", "tool.not_visible", "tool.builtin_not_enabled", "tool.provider_native", "tool.not_registered", "tool.builtin_needs_snapshot", "tool.not_allowed", "tool.not_eligible", "tool.unsupported_type", "tool.agent_call_limit", "tool.agent_failed", "tool.activity_failed", "protocol.not_served", "protocol.profile_type", "protocol.options", "protocol.option_clash", "protocol.base_options", "protocol.id_clash", "protocol.ws_headers", "protocol.builtin_unwritten", "protocol.provider_missing", "protocol.connection", "protocol.part_schema", "protocol.part_invalid", "protocol.key_invalid", "protocol.no_key", "protocol.reply_shape", "protocol.endpoint_url", "protocol.endpoint_host", "protocol.http", "protocol.no_operation", "protocol.definition", "protocol.provider_id_clash", "protocol.history_needed"];
5
5
  /** One of {@linkcode LABEL_KEYS}. */
6
6
  export type LabelKey = (typeof LABEL_KEYS)[number];
7
7
  /**
@@ -10,7 +10,7 @@ export type LabelKey = (typeof LABEL_KEYS)[number];
10
10
  * puts into history. Only these overrides travel to the browser; the rest
11
11
  * (repair prompts, canary, taint, egress) stay on the host.
12
12
  */
13
- export declare const CLIENT_LABEL_KEYS: readonly ["attachments.too_many_files", "attachments.too_many_of_kind", "attachments.file_too_large", "attachments.turn_too_large", "attachments.not_accepted", "attachments.mime_not_allowed", "attachments.limits_unconfigured", "detect.blocked", "error.config", "error.request", "error.conflict", "error.not_found", "error.input", "error.action", "error.auth", "error.rate_limit", "error.busy", "error.unsupported", "error.unavailable", "error.bad_response", "error.network", "error.timeout", "error.safety", "error.blocked", "error.declined", "error.failed", "error.cancelled", "error.internal", "session.abandon_gated", "session.tool_denied", "session.sign_in", "session.gate_expired", "session.turn_ended", "session.gate_pending", "session.part_skipped", "live.session_ended", "tool.awaiting_user", "tool.completed_hidden", "tool.execution_uncertain", "voice.unsupported", "voice.permission", "voice.unavailable", "voice.failed", "voice.empty", "stop.cancelled", "stop.interrupted", "stop.length", "stop.filtered", "stop.provider_error", "stop.stream_incomplete", "stop.step_limit", "composer.model", "composer.effort"];
13
+ export declare const CLIENT_LABEL_KEYS: readonly ["attachments.too_many_files", "attachments.too_many_of_kind", "attachments.file_too_large", "attachments.turn_too_large", "attachments.not_accepted", "attachments.mime_not_allowed", "attachments.limits_unconfigured", "detect.blocked", "error.config", "error.request", "error.conflict", "error.not_found", "error.input", "error.action", "error.auth", "error.rate_limit", "error.busy", "error.unsupported", "error.unavailable", "error.bad_response", "error.network", "error.timeout", "error.safety", "error.blocked", "error.declined", "error.failed", "error.cancelled", "error.internal", "session.abandon_gated", "session.tool_denied", "session.sign_in", "session.gate_expired", "session.turn_ended", "session.gate_pending", "session.part_skipped", "live.session_ended", "tool.awaiting_user", "tool.completed_hidden", "tool.execution_uncertain", "tool.unreachable", "voice.unsupported", "voice.permission", "voice.unavailable", "voice.failed", "voice.empty", "stop.cancelled", "stop.interrupted", "stop.length", "stop.filtered", "stop.provider_error", "stop.stream_incomplete", "stop.step_limit", "composer.model", "composer.effort"];
14
14
  /**
15
15
  * Lines only {@linkcode overrideLabels} changes: builder and host errors from providers and
16
16
  * protocols, and the decision box hint. A profile's `labels` never reach them.
@@ -92,6 +92,7 @@ export const LABEL_KEYS = [
92
92
  'error.internal',
93
93
  'repair.default_guidance',
94
94
  'repair.items_guidance',
95
+ 'repair.cut_off_guidance',
95
96
  'repair.prompt_header',
96
97
  'repair.prompt_intro',
97
98
  'repair.prompt_instructions',
@@ -180,6 +181,7 @@ export const LABEL_KEYS = [
180
181
  'tool.output_invalid_after_mutate',
181
182
  'tool.handler_no_output',
182
183
  'tool.did_not_run',
184
+ 'tool.unreachable',
183
185
  'tool.page_no_answer',
184
186
  'tool.page_timed_out',
185
187
  'tool.output_invalid',
@@ -270,6 +272,7 @@ export const CLIENT_LABEL_KEYS = [
270
272
  'tool.awaiting_user',
271
273
  'tool.completed_hidden',
272
274
  'tool.execution_uncertain',
275
+ 'tool.unreachable',
273
276
  'voice.unsupported',
274
277
  'voice.permission',
275
278
  'voice.unavailable',
@@ -431,6 +434,7 @@ const DEFAULTS = {
431
434
  'error.internal': 'Sorry, something went wrong.',
432
435
  'repair.default_guidance': 'Revise the previous output so it satisfies the validator rejection. Preserve the intended user-facing substance unless the guidance says otherwise.',
433
436
  'repair.items_guidance': 'The previous output holds only the items that were rejected; the rest of your reply is kept. Return only these items, corrected, in the same order.',
437
+ 'repair.cut_off_guidance': 'Your answer was cut off before it ended. Give the whole answer again, written shorter so it fits. Keep what matters most.',
434
438
  'repair.prompt_header': '## OUTPUT REPAIR REQUEST',
435
439
  'repair.prompt_intro': 'The previous assistant output was rejected by a host validator and must be revised.',
436
440
  'repair.prompt_instructions': '1. Inspect the previous output and validator rejection.\n2. Apply the repair guidance without inventing unsupported facts.\n3. Return only the corrected assistant output required by the active profile.',
@@ -517,6 +521,7 @@ const DEFAULTS = {
517
521
  'tool.execution_uncertain': 'This action has an unresolved execution. Check the destination before trying it again.',
518
522
  'tool.input_invalid': 'Tool input validation failed',
519
523
  'tool.did_not_run': "Tool '{tool}' did not run.",
524
+ 'tool.unreachable': "Sorry, a tool couldn't be reached. Please try again.",
520
525
  'tool.input_invalid_after_mutate': 'Tool input validation failed after mutate',
521
526
  'tool.output_invalid_after_mutate': 'Tool output validation failed after mutate',
522
527
  'tool.handler_no_output': 'Handler returned no output',
@@ -648,7 +653,7 @@ export const LABEL_NOTES = {
648
653
  'error.unsupported': 'Shown to the user when the model or its route cannot serve the request. Takes {tool} when one step failed.',
649
654
  'error.unavailable': 'Shown to the user when the model provider is down. Takes {tool} when one step failed.',
650
655
  'error.bad_response': 'Shown to the user when the model provider answers with something that cannot be used. Takes {tool} when one step failed.',
651
- 'error.network': 'Shown to the user when the request never reached the model provider. Takes {tool} when one step failed.',
656
+ 'error.network': 'Shown to the user when the request never reached the model provider. A tool that could not be reached shows tool.unreachable.',
652
657
  'error.timeout': 'Shown to the user when the model takes longer than the host allows. Takes {tool} when one step failed.',
653
658
  'error.safety': 'Shown to the user when Theorem or the model provider holds the reply back. Takes {tool} when one step failed.',
654
659
  'error.blocked': "Shown to the user when a guardrail or host policy stops one of the agent's steps. Takes {tool} when one step failed.",
@@ -658,6 +663,7 @@ export const LABEL_NOTES = {
658
663
  'error.internal': 'Shown to the user when something inside Theorem breaks. Takes {tool} when one step failed.',
659
664
  'repair.default_guidance': "Sent to the model when a validator rejects its output and the host gives no guidance of its own; also the Repair guidance setting's default.",
660
665
  'repair.items_guidance': 'Sent to the model when an item rule rejects some items of a list in its structured reply and asks for those items alone to be rewritten.',
666
+ 'repair.cut_off_guidance': 'Sent to the model when its answer was cut off at the output limit and it is asked to give it again, shorter.',
661
667
  'repair.prompt_header': 'The heading that opens the repair request sent to the model after a validator or the egress check rejects its output.',
662
668
  'repair.prompt_intro': 'The first line of the repair request, saying why the model is asked to revise.',
663
669
  'repair.prompt_instructions': 'The numbered steps at the end of the repair request.',
@@ -743,6 +749,7 @@ export const LABEL_NOTES = {
743
749
  'tool.execution_uncertain': 'Shown when a tool operation has no confirmed outcome and cannot execute again.',
744
750
  'tool.input_invalid': "Told to the model when a tool call's input fails the tool's input schema.",
745
751
  'tool.did_not_run': 'Given by runTool and runToolStep for a call that never settled when the invoke gave no error of its own. Takes {tool}.',
752
+ 'tool.unreachable': 'Shown to the user when a tool call never reached its host. Takes {tool}.',
746
753
  'tool.input_invalid_after_mutate': "Told to the model when a tool call's input fails its schema after a guardrail changed it.",
747
754
  'tool.output_invalid_after_mutate': "Told to the model when a tool's output fails its schema after a guardrail changed it.",
748
755
  'tool.handler_no_output': "Told to the model when a tool's handler returns nothing.",
@@ -845,11 +852,16 @@ const LABEL_PLACEHOLDERS = {
845
852
  'tool.not_allowed': ['tool', 'profile'],
846
853
  'tool.not_eligible': TOOL,
847
854
  'tool.did_not_run': TOOL,
855
+ 'tool.unreachable': TOOL,
848
856
  'tool.unsupported_type': TOOL,
849
857
  'tool.agent_call_limit': TOOL,
850
858
  'tool.agent_failed': TOOL,
851
859
  'tool.activity_failed': TOOL,
852
- ...Object.fromEntries(LABEL_KEYS.filter((key) => key.startsWith('error.')).map((key) => [key, TOOL])),
860
+ // why: a tool that could not be reached is worded by tool.unreachable, so error.network never names one.
861
+ ...Object.fromEntries(LABEL_KEYS.filter((key) => key.startsWith('error.') && key !== 'error.network').map((key) => [
862
+ key,
863
+ TOOL,
864
+ ])),
853
865
  };
854
866
  /** Placeholders an override for a key must keep (mechanism-critical tokens). */
855
867
  const REQUIRED_PLACEHOLDERS = {
@@ -353,14 +353,21 @@ function startItemFix(args) {
353
353
  }
354
354
  /** A structured reply the attempt wrote that never parsed, and what went wrong with it. */
355
355
  function unparsedReply(state, generation, profile) {
356
- if (!generation.structured || answersByTool(generation))
356
+ if (!generation.structured)
357
357
  return undefined;
358
- const text = collectAttemptText(state.attemptEvents);
358
+ const byTool = answersByTool(generation);
359
+ const text = byTool ? (state.cutOffAnswer ?? '') : collectAttemptText(state.attemptEvents);
359
360
  if (!text.trim())
360
361
  return undefined;
361
362
  if (state.lastStop?.kind === 'length') {
362
- return { text, rejection: labelText('structured.cut_off', {}, profile.labels) };
363
+ return {
364
+ text,
365
+ rejection: labelText('structured.cut_off', {}, profile.labels),
366
+ guidance: labelText('repair.cut_off_guidance', {}, profile.labels),
367
+ };
363
368
  }
369
+ if (byTool)
370
+ return undefined;
364
371
  const error = findLast(state.attemptEvents, (event) => event.type === 'error');
365
372
  if (state.lastStop?.kind === 'provider_error' &&
366
373
  error?.errorKind === 'bad_response' &&
@@ -452,6 +459,38 @@ held) {
452
459
  yield failure;
453
460
  return 'pass';
454
461
  }
462
+ /**
463
+ * A structured reply the output limit cut off: a repair that asks for it again, shorter, while the
464
+ * budget lasts, else a `bad_response` failure. The budget is one repair, or `outputs.validation.maxRetries`.
465
+ *
466
+ * why: A cut-off reply holds no answer to check, with or without `outputs.validation`; without a
467
+ * repair the turn would end with none.
468
+ */
469
+ function* handleCutOffReply(flow, state, profile, latestStructured,
470
+ /** True when the attempt's events go out at its end (`yieldBufferedAttemptEvents`). */
471
+ held) {
472
+ if (latestStructured !== undefined || flow.pendingItemFix)
473
+ return 'pass';
474
+ const unparsed = unparsedReply(state, flow.currentGen, profile);
475
+ if (!unparsed?.guidance)
476
+ return 'pass';
477
+ if (flow.currentAttempt < cutOffBudget(profile)) {
478
+ const nextRequest = buildRepairRequest(flow.currentReq, unparsed.text, unparsed.rejection, unparsed.guidance);
479
+ yield updateFlowForRetry(flow, state, profile, nextRequest, 'validation');
480
+ return 'continue';
481
+ }
482
+ const failure = toErrorEvent(new TheoremError('bad_response', 'the reply was cut off at the output limit'));
483
+ recordStepEvent(failure, state);
484
+ state.lastStop = { kind: 'provider_error' };
485
+ if (!held)
486
+ yield failure;
487
+ return 'pass';
488
+ }
489
+ /** How many times a structured reply cut off at the output limit is repaired. */
490
+ function cutOffBudget(profile) {
491
+ const outputs = profileTurnOutputs(profile);
492
+ return outputs?.structured ? Math.max(1, outputs.validation?.maxRetries ?? 0) : 0;
493
+ }
455
494
  /** Puts on the attempt's structured output the fields that still failed. */
456
495
  function markFailed(state, failed) {
457
496
  const held = findLast(state.attemptEvents, (event) => event.type === 'structured');
@@ -476,6 +515,7 @@ async function* executeSingleAttemptCycle(args) {
476
515
  // invariant: The attempt's reply spans its before_end re-entries; only a new attempt starts it again.
477
516
  state.replyText = '';
478
517
  state.replyMessages = [];
518
+ state.cutOffAnswer = undefined;
479
519
  // invariant: The leak check's carry is the end of this attempt's reply; a retry's is read on its own.
480
520
  state.canaryCarry = undefined;
481
521
  let latestStructured;
@@ -545,7 +585,11 @@ async function* checkAttemptReply(flow, state, profile, system, structured) {
545
585
  }
546
586
  latestStructured = projectOutbound(state.attemptEvents).structured ?? latestStructured;
547
587
  }
548
- if ((yield* handleMissingAnswer(flow, state, profile, latestStructured, Boolean(validation) || judged)) === 'continue') {
588
+ const held = Boolean(validation) || judged;
589
+ if ((yield* handleMissingAnswer(flow, state, profile, latestStructured, held)) === 'continue') {
590
+ return { status: 'continue' };
591
+ }
592
+ if ((yield* handleCutOffReply(flow, state, profile, latestStructured, held)) === 'continue') {
549
593
  return { status: 'continue' };
550
594
  }
551
595
  if (validation) {
@@ -567,7 +611,7 @@ async function* checkAttemptReply(flow, state, profile, system, structured) {
567
611
  async function* runAttemptsWithValidation(safe, profile, generation, system, provider, state) {
568
612
  const { blockedReply } = resolveGuardrailPolicy(profile.guardrails);
569
613
  // why: Each gate counts the attempts against its own cap; the turn runs while either has one left.
570
- const maxRetries = Math.max(profileTurnOutputs(profile)?.validation?.maxRetries ?? 0, blockedReply.onBlock === 'retry' ? blockedReply.maxRetries : 0);
614
+ const maxRetries = Math.max(profileTurnOutputs(profile)?.validation?.maxRetries ?? 0, blockedReply.onBlock === 'retry' ? blockedReply.maxRetries : 0, cutOffBudget(profile));
571
615
  const flow = {
572
616
  currentAttempt: 0,
573
617
  currentGen: generation,
@@ -59,6 +59,8 @@ interface StepExecutionState {
59
59
  message: TurnHistoryMessage;
60
60
  at: number;
61
61
  }[];
62
+ /** The answer tool's arguments as this attempt wrote them before the output limit cut them off. */
63
+ cutOffAnswer?: string;
62
64
  /** The message `input.lateResults` opened, until the run can emit it. */
63
65
  lateResults?: string;
64
66
  /** The canary opening the last provider call ended on, read in front of the next call's reply. */
@@ -144,12 +144,33 @@ async function* showAnswer(shown, tally, step) {
144
144
  yield* shown.show(event);
145
145
  }
146
146
  /**
147
- * A tool call's preview. The answer tool's streams as the structured reply's draft.
147
+ * An answer the output limit cut off: its drafts, kept on the attempt for the repair. The call
148
+ * stops at `length`, and the failure its broken arguments gave is not the turn's.
149
+ *
150
+ * why: A protocol hands over a call whose arguments do not parse as a bare `bad_response`, and
151
+ * drops the call; the stream then stops it as a `provider_error`. Its drafts are what the model
152
+ * wrote, and the cut is why it broke.
153
+ */
154
+ function keepCutOffAnswer(tally, state, generation) {
155
+ if (!tally.cut || !answersByTool(generation) || !tally.answerDraft.trim())
156
+ return;
157
+ if (tally.calls.some((call) => call.name === ANSWER_TOOL && !call.failure))
158
+ return;
159
+ state.cutOffAnswer = tally.answerDraft;
160
+ tally.failures = tally.failures.filter((failure) => failure.errorKind !== 'bad_response');
161
+ tally.stop = { kind: 'length' };
162
+ state.lastStop = tally.stop;
163
+ }
164
+ /**
165
+ * A tool call's preview. The answer tool's streams as the structured reply's draft, and the call
166
+ * keeps it, for the repair of an answer the output limit cuts off (`keepCutOffAnswer`).
148
167
  *
149
168
  * why: A preview of the call that follows, which owns the arguments: not the turn's record. It streams
150
169
  * as the reply's text does, through its own gate, and like that text is held only when delivery is buffered.
151
170
  */
152
- async function* showToolDraft(event, shown, generation, buffer) {
171
+ async function* showToolDraft(event, shown, generation, buffer, tally) {
172
+ if (event.name === ANSWER_TOOL)
173
+ tally.answerDraft += event.text;
153
174
  if (buffer.holdVisible)
154
175
  return;
155
176
  if (event.name === ANSWER_TOOL && answersByTool(generation))
@@ -253,7 +274,15 @@ async function* executeAutonomousStep(args, state, buffer = {
253
274
  }) {
254
275
  const { profile, generation, system, provider, signal } = args;
255
276
  const { usage, genForStep, request, call } = startProviderCall(generation, system, state, isAnswerOnlyStep(profile, generation, state.stepCount), profile.labels);
256
- const tally = { calls: [], reasoning: [], text: '', failures: [], replied: false };
277
+ const tally = {
278
+ calls: [],
279
+ reasoning: [],
280
+ text: '',
281
+ failures: [],
282
+ replied: false,
283
+ answerDraft: '',
284
+ cut: false,
285
+ };
257
286
  const control = {
258
287
  withholdVisible: false,
259
288
  ...(state.canaryCarry ? { canaryCarry: state.canaryCarry } : {}),
@@ -288,12 +317,13 @@ async function* executeAutonomousStep(args, state, buffer = {
288
317
  }
289
318
  if (event.type === 'done') {
290
319
  state.providerState = event.providerState;
320
+ tally.cut ||= event.stop.kind === 'length';
291
321
  tally.stop = event.stop;
292
322
  state.lastStop = event.stop;
293
323
  continue;
294
324
  }
295
325
  if (event.type === 'tool_draft') {
296
- yield* showToolDraft(event, shown, generation, buffer);
326
+ yield* showToolDraft(event, shown, generation, buffer, tally);
297
327
  continue;
298
328
  }
299
329
  // why: The model's history keeps what it wrote; only the reply the host gets is set apart.
@@ -310,6 +340,7 @@ async function* executeAutonomousStep(args, state, buffer = {
310
340
  throw err;
311
341
  }
312
342
  yield* shown.end();
343
+ keepCutOffAnswer(tally, state, generation);
313
344
  yield* showAnswer(shown, tally, { generation, state, control, buffer, call });
314
345
  keepStreamCarry(state, control);
315
346
  const resume = resumeOf(args.mayResume, profile, tally) ?? mediaResumeOf(state, profile, tally);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theoremjs/agents",
3
- "version": "0.3.4-next.811.gf4e3b3f",
3
+ "version": "0.3.4-next.815.g7f5fa76",
4
4
  "description": "A flat TypeScript agent kernel for typed profiles, deterministic turn execution, registered tools, provider adapters, guardrails, and host-injected traces.",
5
5
  "keywords": [
6
6
  "agent",