@sema-agent/core 7.68.5 → 7.69.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.
- package/CHANGELOG.md +1611 -0
- package/dist/agents/agent-definition.d.ts +32 -3
- package/dist/agents/agent-definition.js +39 -1
- package/dist/agents/agent-transcript-tool.js +16 -5
- package/dist/agents/cascade.js +9 -6
- package/dist/agents/cc-uds-peer-channel.js +2 -1
- package/dist/agents/cumulative-stats.d.ts +3 -1
- package/dist/agents/cumulative-stats.js +4 -1
- package/dist/agents/delegation-review-faces.d.ts +35 -0
- package/dist/agents/delegation-review-faces.js +26 -0
- package/dist/agents/inherited-tool-pool.d.ts +4 -2
- package/dist/agents/list-agents-tool.d.ts +8 -19
- package/dist/agents/list-agents-tool.js +19 -34
- package/dist/agents/peer-directory.js +3 -2
- package/dist/agents/peer-session-drain.js +3 -3
- package/dist/agents/repair-loop.js +12 -8
- package/dist/agents/retain-ledger.d.ts +122 -58
- package/dist/agents/retain-ledger.js +48 -21
- package/dist/agents/root-session-lane.d.ts +17 -0
- package/dist/agents/root-session-lane.js +9 -0
- package/dist/agents/send-message-tool.js +44 -31
- package/dist/agents/subagent-steps.d.ts +0 -11
- package/dist/agents/subagent-steps.js +1 -14
- package/dist/agents/subagent-transcript-placement.d.ts +4 -3
- package/dist/agents/subagent.d.ts +19 -8
- package/dist/agents/subagent.js +250 -207
- package/dist/agents/tool-filter.d.ts +31 -1
- package/dist/agents/tool-filter.js +28 -4
- package/dist/agents/verifier-independence.js +3 -2
- package/dist/agents/verify.d.ts +5 -1
- package/dist/agents/verify.js +10 -6
- package/dist/agents/yield-adoption.d.ts +17 -0
- package/dist/agents/yield-adoption.js +22 -2
- package/dist/brain/anthropic.js +42 -14
- package/dist/brain/claude-model-id.d.ts +36 -0
- package/dist/brain/claude-model-id.js +44 -0
- package/dist/brain/failover.js +14 -3
- package/dist/brain/fast-mode.d.ts +272 -0
- package/dist/brain/fast-mode.js +130 -0
- package/dist/brain/media-degrade.d.ts +5 -0
- package/dist/brain/media-degrade.js +1 -0
- package/dist/brain/model-capability.d.ts +122 -18
- package/dist/brain/model-capability.js +84 -7
- package/dist/brain/model-lane.d.ts +40 -16
- package/dist/brain/model-lane.js +97 -39
- package/dist/brain/model-presets.d.ts +4 -1
- package/dist/brain/model-presets.js +10 -1
- package/dist/brain/open-responses.js +37 -14
- package/dist/brain/openai.js +27 -15
- package/dist/brain/provider-dialect.d.ts +9 -0
- package/dist/brain/provider-dialect.js +4 -0
- package/dist/brain/provider-message.d.ts +2 -2
- package/dist/brain/provider-message.js +8 -8
- package/dist/brain/reasoning-request.d.ts +19 -0
- package/dist/brain/reasoning-request.js +8 -4
- package/dist/brain/reasoning-stamp.d.ts +3 -2
- package/dist/brain/reasoning.d.ts +8 -4
- package/dist/brain/reasoning.js +4 -3
- package/dist/brain/reference-model-traits.d.ts +59 -0
- package/dist/brain/reference-model-traits.js +46 -0
- package/dist/brain/request-params.js +1 -0
- package/dist/brain/route-adjudicator.d.ts +107 -106
- package/dist/brain/route-adjudicator.js +217 -51
- package/dist/brain/route-conformance.d.ts +6 -4
- package/dist/brain/routing.d.ts +2 -1
- package/dist/brain/routing.js +21 -5
- package/dist/brain/status-sink.d.ts +52 -10
- package/dist/brain/stream-engine.d.ts +68 -11
- package/dist/brain/stream-engine.js +512 -93
- package/dist/brain/stream-shared.d.ts +34 -8
- package/dist/brain/stream-shared.js +20 -7
- package/dist/brain/usage-reading.d.ts +15 -0
- package/dist/brain/usage-reading.js +18 -5
- package/dist/core/a2a.js +28 -21
- package/dist/core/advisor-verdict.d.ts +5 -4
- package/dist/core/advisor-verdict.js +2 -2
- package/dist/core/agent-definition.d.ts +4 -1
- package/dist/core/agent-state.d.ts +25 -2
- package/dist/core/agent-state.js +50 -3
- package/dist/core/allow-entry.d.ts +6 -6
- package/dist/core/allow-entry.js +27 -25
- package/dist/core/ask-headline.d.ts +28 -16
- package/dist/core/ask-headline.js +18 -8
- package/dist/core/ask-origin.d.ts +105 -30
- package/dist/core/ask-origin.js +40 -11
- package/dist/core/ask-question.d.ts +9 -1
- package/dist/core/ask-question.js +3 -0
- package/dist/core/ask-unresolvable-notice.d.ts +10 -2
- package/dist/core/ask-unresolvable-notice.js +5 -2
- package/dist/core/attachment-source.d.ts +1 -1
- package/dist/core/attachment-source.js +1 -0
- package/dist/core/auto-compaction.d.ts +4 -4
- package/dist/core/auto-compaction.js +4 -3
- package/dist/core/auto-mode-directive.d.ts +30 -60
- package/dist/core/auto-mode-directive.js +36 -38
- package/dist/core/auto-mode.d.ts +40 -9
- package/dist/core/auto-mode.js +20 -5
- package/dist/core/background-agent-store.d.ts +87 -1
- package/dist/core/background-agent-store.js +72 -1
- package/dist/core/background-shell.d.ts +3 -0
- package/dist/core/background-shell.js +1 -0
- package/dist/core/cache-break-detector.js +1 -1
- package/dist/core/caller-text.d.ts +27 -8
- package/dist/core/caller-text.js +47 -1
- package/dist/core/checkpoint-features.d.ts +17 -9
- package/dist/core/checkpoint-identity.d.ts +53 -0
- package/dist/core/checkpoint-identity.js +29 -0
- package/dist/core/checkpoint-queries.d.ts +4 -3
- package/dist/core/checkpoint-queries.js +5 -3
- package/dist/core/checkpoint-rekey.d.ts +70 -0
- package/dist/core/checkpoint-rekey.js +107 -0
- package/dist/core/checkpoint-requirements.d.ts +65 -10
- package/dist/core/checkpoint-requirements.js +47 -13
- package/dist/core/checkpoint-row-verdict.d.ts +5 -3
- package/dist/core/checkpoint-row-verdict.js +37 -4
- package/dist/core/checkpoint-store.d.ts +71 -5
- package/dist/core/checkpoint-store.js +38 -7
- package/dist/core/child-clamp-seats.js +2 -1
- package/dist/core/classifier-category.d.ts +25 -0
- package/dist/core/classifier-category.js +93 -0
- package/dist/core/classifier-unavailable-arm.d.ts +62 -8
- package/dist/core/classifier-unavailable-arm.js +19 -2
- package/dist/core/command-sandbox-dispatch.d.ts +14 -2
- package/dist/core/command-sandbox.d.ts +209 -28
- package/dist/core/command-sandbox.js +195 -20
- package/dist/core/config-refusal-codes.d.ts +41 -1
- package/dist/core/config-refusal-codes.js +16 -0
- package/dist/core/context-edit.d.ts +2 -1
- package/dist/core/context-edit.js +2 -1
- package/dist/core/context-settings.js +5 -4
- package/dist/core/cross-boundary-taint.d.ts +15 -1
- package/dist/core/cross-boundary-taint.js +6 -0
- package/dist/core/delegation-frames.d.ts +36 -6
- package/dist/core/delegation-frames.js +3 -0
- package/dist/core/delegation-name.d.ts +10 -4
- package/dist/core/delegation-name.js +53 -23
- package/dist/core/engine-attestation.d.ts +10 -2
- package/dist/core/engine-attestation.js +2 -0
- package/dist/core/engine-capabilities.d.ts +40 -3
- package/dist/core/engine-capabilities.js +1 -1
- package/dist/core/engine-notice.d.ts +197 -90
- package/dist/core/engine-notice.js +29 -1
- package/dist/core/engine-version.d.ts +1 -0
- package/dist/core/engine-version.js +1 -0
- package/dist/core/extension-call-id.d.ts +4 -0
- package/dist/core/extension-call-id.js +8 -0
- package/dist/core/fast-tier-door.d.ts +36 -0
- package/dist/core/fast-tier-door.js +65 -0
- package/dist/core/file-history-store.d.ts +343 -67
- package/dist/core/file-history-store.js +320 -93
- package/dist/core/frame-settlement.d.ts +4 -0
- package/dist/core/frame-settlement.js +5 -0
- package/dist/core/gate-fold.js +13 -4
- package/dist/core/gate-lanes.js +27 -20
- package/dist/core/gate-outcome.d.ts +87 -11
- package/dist/core/gate-outcome.js +94 -4
- package/dist/core/git-worktree-env.d.ts +29 -10
- package/dist/core/git-worktree-env.js +31 -12
- package/dist/core/governance-codes.d.ts +232 -7
- package/dist/core/governance-codes.js +195 -6
- package/dist/core/hooks.d.ts +27 -16
- package/dist/core/hooks.js +84 -82
- package/dist/core/host-floor.d.ts +2 -8
- package/dist/core/host-floor.js +6 -42
- package/dist/core/identity-rekey.d.ts +275 -0
- package/dist/core/identity-rekey.js +198 -0
- package/dist/core/image-admission.d.ts +7 -4
- package/dist/core/image-admission.js +24 -11
- package/dist/core/isolation-scope.d.ts +12 -0
- package/dist/core/isolation-scope.js +4 -0
- package/dist/core/leg-identity.d.ts +32 -2
- package/dist/core/leg-identity.js +16 -2
- package/dist/core/locked-config.d.ts +26 -6
- package/dist/core/locked-config.js +31 -14
- package/dist/core/loopback-host.d.ts +18 -0
- package/dist/core/loopback-host.js +40 -0
- package/dist/core/mcp-connection-pool.d.ts +57 -33
- package/dist/core/mcp-connection-pool.js +47 -23
- package/dist/core/mcp-failure.d.ts +41 -19
- package/dist/core/mcp-failure.js +14 -8
- package/dist/core/mcp-host-read.js +5 -4
- package/dist/core/mcp-host-seat.js +3 -2
- package/dist/core/mcp-name-judge.js +2 -1
- package/dist/core/mcp-server-link.js +2 -1
- package/dist/core/mcp-server-spec.js +2 -2
- package/dist/core/mcp-server-unavailable-notice.d.ts +26 -16
- package/dist/core/mcp-server-unavailable-notice.js +25 -4
- package/dist/core/mcp.d.ts +15 -9
- package/dist/core/mcp.js +46 -34
- package/dist/core/memory-engine/consolidation-driver.js +8 -8
- package/dist/core/memory-engine/consolidation-write-gate.js +3 -2
- package/dist/core/memory-engine/engine.js +9 -9
- package/dist/core/memory-engine/layout.d.ts +6 -5
- package/dist/core/memory-engine/layout.js +1 -1
- package/dist/core/memory.d.ts +5 -0
- package/dist/core/memory.js +3 -0
- package/dist/core/model-fallbacks.d.ts +13 -7
- package/dist/core/model-fallbacks.js +5 -3
- package/dist/core/model-receipt.d.ts +15 -0
- package/dist/core/model-receipt.js +54 -1
- package/dist/core/model-seat.d.ts +17 -15
- package/dist/core/mods/agent-stations.js +5 -4
- package/dist/core/mods/extension-call.d.ts +0 -4
- package/dist/core/mods/extension-call.js +0 -8
- package/dist/core/mods/host.d.ts +18 -0
- package/dist/core/mods/host.js +7 -0
- package/dist/core/mods/kernel/events.d.ts +12 -0
- package/dist/core/mods/kernel/events.js +18 -9
- package/dist/core/mods/kernel/registration.d.ts +13 -0
- package/dist/core/mods/kernel/registration.js +34 -1
- package/dist/core/mods/message-display.js +1 -1
- package/dist/core/mods/package.js +3 -3
- package/dist/core/mods/prompt-submit.js +2 -2
- package/dist/core/mods/session-append.js +3 -3
- package/dist/core/mods/session-compact.js +5 -5
- package/dist/core/mods/session-receive.js +3 -3
- package/dist/core/mods/session-send.js +3 -3
- package/dist/core/mods/stations.d.ts +8 -5
- package/dist/core/mods/stations.js +13 -12
- package/dist/core/mods/tool-call.js +12 -12
- package/dist/core/mounted-mcp.d.ts +10 -1
- package/dist/core/mounted-mcp.js +4 -0
- package/dist/core/output-style.d.ts +14 -8
- package/dist/core/output-style.js +6 -6
- package/dist/core/own-property-receipt.js +3 -5
- package/dist/core/owner-identity.d.ts +68 -0
- package/dist/core/owner-identity.js +39 -0
- package/dist/core/parent-spec-seat.d.ts +5 -1
- package/dist/core/parent-spec-seat.js +2 -0
- package/dist/core/park-resumability.js +2 -2
- package/dist/core/park-selfcheck.js +15 -4
- package/dist/core/peer-seat.d.ts +37 -11
- package/dist/core/peer-seat.js +35 -9
- package/dist/core/permission-rule-model.d.ts +5 -0
- package/dist/core/permission-rule-model.js +27 -6
- package/dist/core/permission-rules.d.ts +42 -5
- package/dist/core/permission-rules.js +74 -10
- package/dist/core/persisted-rule-arms.d.ts +8 -6
- package/dist/core/persisted-rule-arms.js +13 -20
- package/dist/core/physical-path.d.ts +12 -8
- package/dist/core/physical-path.js +24 -14
- package/dist/core/present-plan-tool.js +2 -1
- package/dist/core/pricing.d.ts +65 -8
- package/dist/core/pricing.js +51 -0
- package/dist/core/prompt-section-seat.js +6 -5
- package/dist/core/protocol-http-auth.d.ts +87 -10
- package/dist/core/protocol-http-auth.js +155 -23
- package/dist/core/reminder-mint.d.ts +4 -0
- package/dist/core/reminder-mint.js +1 -0
- package/dist/core/remote-env.d.ts +46 -1
- package/dist/core/remote-env.js +2 -0
- package/dist/core/resume-at-refusal.d.ts +47 -0
- package/dist/core/resume-at-refusal.js +9 -0
- package/dist/core/resume-startup-cause.d.ts +8 -3
- package/dist/core/resume-startup-cause.js +3 -1
- package/dist/core/retired-keys.d.ts +4 -0
- package/dist/core/retired-keys.js +4 -0
- package/dist/core/roles.d.ts +11 -5
- package/dist/core/roles.js +14 -3
- package/dist/core/rule-store-fault.d.ts +8 -1
- package/dist/core/rule-store-fault.js +5 -0
- package/dist/core/runner/advisor-instructions.d.ts +12 -12
- package/dist/core/runner/advisor-instructions.js +7 -24
- package/dist/core/runner/announce-once-ledger.d.ts +23 -8
- package/dist/core/runner/announce-once-ledger.js +69 -18
- package/dist/core/runner/assemble-result.d.ts +9 -0
- package/dist/core/runner/assemble-result.js +3 -2
- package/dist/core/runner/auto-strip-notice.js +1 -1
- package/dist/core/runner/branch-window.js +5 -3
- package/dist/core/runner/call-hook-stations.d.ts +3 -0
- package/dist/core/runner/call-hook-stations.js +4 -3
- package/dist/core/runner/caller-roster.js +2 -1
- package/dist/core/runner/capability-withheld-seat.js +47 -5
- package/dist/core/runner/classifier-ladder.d.ts +48 -8
- package/dist/core/runner/classifier-ladder.js +88 -38
- package/dist/core/runner/clock-and-limits.d.ts +13 -2
- package/dist/core/runner/clock-and-limits.js +11 -8
- package/dist/core/runner/command-sandbox-frame.d.ts +56 -0
- package/dist/core/runner/command-sandbox-frame.js +107 -0
- package/dist/core/runner/compact-session.d.ts +3 -1
- package/dist/core/runner/compact-session.js +16 -10
- package/dist/core/runner/compaction-call-options.d.ts +2 -2
- package/dist/core/runner/compaction-restate.d.ts +4 -4
- package/dist/core/runner/compaction-restate.js +3 -1
- package/dist/core/runner/construction-doors.js +9 -0
- package/dist/core/runner/continuation-gate.d.ts +39 -0
- package/dist/core/runner/continuation-gate.js +42 -0
- package/dist/core/runner/continues-from.d.ts +57 -0
- package/dist/core/runner/continues-from.js +76 -0
- package/dist/core/runner/contracts.d.ts +133 -55
- package/dist/core/runner/credential-refusal-seat.js +31 -12
- package/dist/core/runner/cross-boundary-lane.d.ts +2 -2
- package/dist/core/runner/cross-boundary-lane.js +26 -13
- package/dist/core/runner/delegation-owner.d.ts +99 -9
- package/dist/core/runner/delegation-owner.js +107 -4
- package/dist/core/runner/denial-limit-arms.js +13 -12
- package/dist/core/runner/derived-route-fallback.d.ts +15 -19
- package/dist/core/runner/derived-route-fallback.js +4 -4
- package/dist/core/runner/earlier-grammar-notice.d.ts +6 -0
- package/dist/core/runner/earlier-grammar-notice.js +9 -0
- package/dist/core/runner/env-capability-census.js +2 -1
- package/dist/core/runner/execution-record.d.ts +4 -2
- package/dist/core/runner/execution-record.js +1 -1
- package/dist/core/runner/fast-mode-seat.d.ts +89 -0
- package/dist/core/runner/fast-mode-seat.js +215 -0
- package/dist/core/runner/file-history-restore.d.ts +52 -14
- package/dist/core/runner/file-history-restore.js +50 -8
- package/dist/core/runner/forward-sink.js +2 -2
- package/dist/core/runner/gate-exit.d.ts +41 -3
- package/dist/core/runner/gate-exit.js +55 -10
- package/dist/core/runner/git-leg-delivery.js +6 -3
- package/dist/core/runner/git-status-frame.d.ts +33 -25
- package/dist/core/runner/git-status-frame.js +54 -36
- package/dist/core/runner/halt-attribution.js +2 -2
- package/dist/core/runner/hook-progress-lane.d.ts +26 -0
- package/dist/core/runner/hook-progress-lane.js +72 -0
- package/dist/core/runner/initial-run-state.js +2 -2
- package/dist/core/runner/language-frame.d.ts +10 -8
- package/dist/core/runner/language-frame.js +7 -11
- package/dist/core/runner/left-as-is-seat.d.ts +20 -0
- package/dist/core/runner/left-as-is-seat.js +11 -0
- package/dist/core/runner/leg-receipt.d.ts +38 -0
- package/dist/core/runner/leg-receipt.js +22 -0
- package/dist/core/runner/listing-fold.d.ts +4 -3
- package/dist/core/runner/listing-fold.js +5 -5
- package/dist/core/runner/listing-replay.d.ts +41 -0
- package/dist/core/runner/listing-replay.js +75 -0
- package/dist/core/runner/main-call-account.d.ts +2 -1
- package/dist/core/runner/main-call-account.js +18 -5
- package/dist/core/runner/mcp-redial.d.ts +4 -2
- package/dist/core/runner/mcp-redial.js +3 -0
- package/dist/core/runner/memory-consolidation-driver.js +3 -2
- package/dist/core/runner/model-fallback-seat.d.ts +12 -9
- package/dist/core/runner/model-fallback-seat.js +24 -20
- package/dist/core/runner/nested-memory-lane.d.ts +16 -8
- package/dist/core/runner/nested-memory-lane.js +64 -53
- package/dist/core/runner/permission-rule-lanes.d.ts +4 -3
- package/dist/core/runner/permission-rule-lanes.js +2 -0
- package/dist/core/runner/person-path-rows.js +1 -1
- package/dist/core/runner/placement-fence.js +2 -2
- package/dist/core/runner/policy-throw.d.ts +3 -3
- package/dist/core/runner/policy-throw.js +7 -5
- package/dist/core/runner/prepare-acquire-reconcile.d.ts +7 -1
- package/dist/core/runner/prepare-acquire-reconcile.js +9 -5
- package/dist/core/runner/prepare-advisor.d.ts +3 -0
- package/dist/core/runner/prepare-advisor.js +2 -2
- package/dist/core/runner/prepare-artifact.d.ts +7 -2
- package/dist/core/runner/prepare-artifact.js +18 -7
- package/dist/core/runner/prepare-ask-lane.d.ts +7 -0
- package/dist/core/runner/prepare-ask-lane.js +4 -3
- package/dist/core/runner/prepare-caps-and-workflow.d.ts +9 -5
- package/dist/core/runner/prepare-caps-and-workflow.js +28 -20
- package/dist/core/runner/prepare-config-doors.d.ts +13 -3
- package/dist/core/runner/prepare-config-doors.js +35 -24
- package/dist/core/runner/prepare-context-lane.d.ts +3 -4
- package/dist/core/runner/prepare-context-lane.js +12 -15
- package/dist/core/runner/prepare-defer-classify.d.ts +12 -0
- package/dist/core/runner/prepare-defer-classify.js +5 -2
- package/dist/core/runner/prepare-delegation-surface.d.ts +10 -8
- package/dist/core/runner/prepare-delegation-surface.js +6 -7
- package/dist/core/runner/prepare-execution-env.d.ts +46 -4
- package/dist/core/runner/prepare-execution-env.js +47 -18
- package/dist/core/runner/prepare-file-history.d.ts +4 -1
- package/dist/core/runner/prepare-file-history.js +8 -9
- package/dist/core/runner/prepare-gate-stations.d.ts +17 -9
- package/dist/core/runner/prepare-gate-stations.js +22 -38
- package/dist/core/runner/prepare-hands-readface.d.ts +13 -9
- package/dist/core/runner/prepare-hands-readface.js +26 -35
- package/dist/core/runner/prepare-inherited-gate.d.ts +9 -2
- package/dist/core/runner/prepare-inherited-gate.js +10 -9
- package/dist/core/runner/prepare-leg-adoption.d.ts +36 -0
- package/dist/core/runner/prepare-leg-adoption.js +24 -0
- package/dist/core/runner/prepare-listings.d.ts +3 -0
- package/dist/core/runner/prepare-listings.js +2 -2
- package/dist/core/runner/prepare-lsp.d.ts +0 -3
- package/dist/core/runner/prepare-lsp.js +1 -2
- package/dist/core/runner/prepare-memory-engine-session.js +1 -1
- package/dist/core/runner/prepare-memory.js +2 -2
- package/dist/core/runner/prepare-offload-wrappers.d.ts +5 -5
- package/dist/core/runner/prepare-offload-wrappers.js +5 -5
- package/dist/core/runner/prepare-park-ask.d.ts +9 -2
- package/dist/core/runner/prepare-park-ask.js +5 -3
- package/dist/core/runner/prepare-policy-chain.d.ts +20 -5
- package/dist/core/runner/prepare-policy-chain.js +32 -26
- package/dist/core/runner/prepare-prompt-assembly.d.ts +11 -2
- package/dist/core/runner/prepare-prompt-assembly.js +6 -3
- package/dist/core/runner/prepare-prompt-inputs.d.ts +3 -3
- package/dist/core/runner/prepare-prompt-inputs.js +1 -1
- package/dist/core/runner/prepare-protocol-tools.d.ts +9 -4
- package/dist/core/runner/prepare-protocol-tools.js +11 -11
- package/dist/core/runner/prepare-run-refs.d.ts +7 -0
- package/dist/core/runner/prepare-run-refs.js +6 -2
- package/dist/core/runner/prepare-suspend-saga.d.ts +4 -1
- package/dist/core/runner/prepare-suspend-saga.js +5 -2
- package/dist/core/runner/prepare-task.d.ts +1 -30
- package/dist/core/runner/prepare-task.js +111 -147
- package/dist/core/runner/prepare-turn-wiring.d.ts +10 -3
- package/dist/core/runner/prepare-turn-wiring.js +7 -4
- package/dist/core/runner/prepare-wiring-manifest.d.ts +11 -3
- package/dist/core/runner/prepare-wiring-manifest.js +20 -16
- package/dist/core/runner/prompt-block.js +2 -2
- package/dist/core/runner/prompt-suggestions.d.ts +3 -8
- package/dist/core/runner/prompt-suggestions.js +4 -1
- package/dist/core/runner/reply-pricing.d.ts +8 -4
- package/dist/core/runner/reply-pricing.js +6 -5
- package/dist/core/runner/resume-admission.js +2 -0
- package/dist/core/runner/resume-apply.js +4 -4
- package/dist/core/runner/resume-checkpoint-screen.d.ts +23 -2
- package/dist/core/runner/resume-checkpoint-screen.js +34 -27
- package/dist/core/runner/resume-preflight.js +3 -3
- package/dist/core/runner/rewind-files.js +21 -14
- package/dist/core/runner/role-resolution-table.d.ts +10 -7
- package/dist/core/runner/role-resolution-table.js +8 -7
- package/dist/core/runner/role-seat-defaulted.js +2 -2
- package/dist/core/runner/rule-store-unreadable-notice.d.ts +4 -2
- package/dist/core/runner/rule-store-unreadable-notice.js +21 -7
- package/dist/core/runner/run-attachment-seats.js +8 -15
- package/dist/core/runner/run-brain-sinks.js +1 -0
- package/dist/core/runner/run-compaction-machinery.d.ts +1 -1
- package/dist/core/runner/run-compaction-machinery.js +2 -3
- package/dist/core/runner/run-harness-handlers.d.ts +2 -2
- package/dist/core/runner/run-harness-handlers.js +18 -5
- package/dist/core/runner/run-identity-wiring.js +11 -9
- package/dist/core/runner/run-leg.js +33 -12
- package/dist/core/runner/run-notification-lane.d.ts +4 -1
- package/dist/core/runner/run-notification-lane.js +21 -8
- package/dist/core/runner/run-recovery-lanes.js +1 -1
- package/dist/core/runner/run-settle-and-teardown.d.ts +6 -1
- package/dist/core/runner/run-settle-and-teardown.js +30 -32
- package/dist/core/runner/run-stop-and-final-verify.d.ts +4 -1
- package/dist/core/runner/run-stop-and-final-verify.js +12 -49
- package/dist/core/runner/run-telemetry-and-budget-seats.js +29 -10
- package/dist/core/runner/run-terminal-adoption.js +5 -2
- package/dist/core/runner/run-turn-boundary.js +13 -11
- package/dist/core/runner/runtask.d.ts +16 -7
- package/dist/core/runner/runtask.js +49 -28
- package/dist/core/runner/sandbox-network-seat.d.ts +52 -0
- package/dist/core/runner/sandbox-network-seat.js +40 -0
- package/dist/core/runner/seat-model-doors.d.ts +16 -0
- package/dist/core/runner/seat-model-doors.js +13 -0
- package/dist/core/runner/session-file-state-replay.d.ts +20 -0
- package/dist/core/runner/session-file-state-replay.js +17 -5
- package/dist/core/runner/session-read-file-states.d.ts +37 -0
- package/dist/core/runner/session-read-file-states.js +19 -0
- package/dist/core/runner/session-rule-policy.d.ts +2 -1
- package/dist/core/runner/session-rule-policy.js +3 -2
- package/dist/core/runner/session-verb-doors.js +2 -1
- package/dist/core/runner/session-write-ledger.d.ts +5 -1
- package/dist/core/runner/session-write-ledger.js +19 -5
- package/dist/core/runner/shared-read-classifier.d.ts +48 -0
- package/dist/core/runner/shared-read-classifier.js +33 -0
- package/dist/core/runner/shell-env-withheld.d.ts +6 -3
- package/dist/core/runner/shell-env-withheld.js +25 -0
- package/dist/core/runner/side-call-account.d.ts +30 -2
- package/dist/core/runner/side-call-account.js +38 -5
- package/dist/core/runner/skipped-rules-notice.d.ts +43 -0
- package/dist/core/runner/skipped-rules-notice.js +49 -0
- package/dist/core/runner/steering-preview.js +1 -1
- package/dist/core/runner/stop-tail-notice.d.ts +40 -0
- package/dist/core/runner/stop-tail-notice.js +35 -0
- package/dist/core/runner/stream-halt-verbs.js +1 -1
- package/dist/core/runner/stream-mode-verb.d.ts +11 -4
- package/dist/core/runner/stream-mode-verb.js +19 -7
- package/dist/core/runner/stream-settle-backstop.js +3 -1
- package/dist/core/runner/stream-steer-verb.js +4 -3
- package/dist/core/runner/synthetic-tools.d.ts +11 -11
- package/dist/core/runner/synthetic-tools.js +17 -20
- package/dist/core/runner/task-start-doors.d.ts +86 -0
- package/dist/core/runner/task-start-doors.js +108 -0
- package/dist/core/runner/teardown-bounded.d.ts +5 -5
- package/dist/core/runner/teardown-bounded.js +6 -6
- package/dist/core/runner/terminal-account.d.ts +3 -2
- package/dist/core/runner/terminal-account.js +5 -3
- package/dist/core/runner/tool-disclosure.js +8 -9
- package/dist/core/runner/tool-end-body.d.ts +2 -0
- package/dist/core/runner/tool-end-body.js +1 -0
- package/dist/core/runner/tool-face-overlay.js +1 -0
- package/dist/core/runner/tool-occurrence.d.ts +22 -24
- package/dist/core/runner/tool-occurrence.js +9 -15
- package/dist/core/runner/tool-progress-lane.js +1 -1
- package/dist/core/runner/tool-result-media-seat.d.ts +7 -4
- package/dist/core/runner/tool-result-media-seat.js +16 -4
- package/dist/core/runner/tool-table-view.d.ts +7 -1
- package/dist/core/runner/tool-table-view.js +1 -1
- package/dist/core/runner/tool-turn-repair-seat.d.ts +22 -2
- package/dist/core/runner/tool-turn-repair-seat.js +23 -5
- package/dist/core/runner/turn-attachments.d.ts +74 -148
- package/dist/core/runner/turn-attachments.js +153 -223
- package/dist/core/runner/usage-accounting.d.ts +15 -0
- package/dist/core/runner/usage-accounting.js +12 -4
- package/dist/core/runner/verdict-refusal.d.ts +45 -0
- package/dist/core/runner/verdict-refusal.js +39 -0
- package/dist/core/runner/write-protection-station.d.ts +5 -0
- package/dist/core/runner/write-protection-station.js +6 -6
- package/dist/core/runner-deps.d.ts +15 -5
- package/dist/core/runtime-caps.d.ts +5 -3
- package/dist/core/secret-env.d.ts +19 -14
- package/dist/core/secret-env.js +19 -12
- package/dist/core/session-checkout.d.ts +18 -1
- package/dist/core/session-checkout.js +23 -8
- package/dist/core/session-log-transfer.js +2 -2
- package/dist/core/session-policy-store.d.ts +220 -31
- package/dist/core/session-policy-store.js +333 -47
- package/dist/core/session-reconcile.d.ts +12 -0
- package/dist/core/session-reconcile.js +1 -1
- package/dist/core/session-store.d.ts +5 -3
- package/dist/core/session-store.js +11 -11
- package/dist/core/session.d.ts +31 -22
- package/dist/core/session.js +3 -2
- package/dist/core/shared-memory/normalize.js +2 -1
- package/dist/core/side-query.d.ts +4 -0
- package/dist/core/side-query.js +7 -4
- package/dist/core/spec-contract.d.ts +4 -2
- package/dist/core/store-contracts/background-agent-store-contract.js +149 -0
- package/dist/core/store-contracts/checkpoint-store-contract.js +55 -0
- package/dist/core/store-contracts/contract-kit-version.d.ts +4 -4
- package/dist/core/store-contracts/file-history-store-contract.d.ts +1 -1
- package/dist/core/store-contracts/file-history-store-contract.js +282 -22
- package/dist/core/store-contracts/session-policy-store-contract.js +202 -0
- package/dist/core/store-contracts/session-store-contract.js +48 -14
- package/dist/core/store-contracts/tool-result-store-contract.d.ts +1 -1
- package/dist/core/store-contracts/tool-result-store-contract.js +128 -0
- package/dist/core/store-contracts/workflow-journal-store-contract.js +90 -1
- package/dist/core/swappable-deps.d.ts +16 -0
- package/dist/core/swappable-deps.js +61 -1
- package/dist/core/synthetic-read-verdict.d.ts +23 -0
- package/dist/core/synthetic-read-verdict.js +43 -18
- package/dist/core/task-event.d.ts +123 -50
- package/dist/core/task-intake.d.ts +21 -5
- package/dist/core/task-intake.js +29 -3
- package/dist/core/task-limits.d.ts +3 -1
- package/dist/core/task-notification.d.ts +171 -59
- package/dist/core/task-notification.js +162 -104
- package/dist/core/task-registry-agent.d.ts +12 -0
- package/dist/core/task-registry-agent.js +23 -9
- package/dist/core/task-registry-shared.d.ts +4 -2
- package/dist/core/task-registry-shared.js +1 -1
- package/dist/core/task-registry-workflow.js +8 -5
- package/dist/core/task-registry.d.ts +16 -1
- package/dist/core/task-registry.js +35 -23
- package/dist/core/task-result.d.ts +127 -10
- package/dist/core/task-spec-members.d.ts +5 -2
- package/dist/core/task-spec-members.js +9 -5
- package/dist/core/task-spec.d.ts +31 -12
- package/dist/core/task-start-judge.d.ts +41 -0
- package/dist/core/task-start-judge.js +26 -0
- package/dist/core/task-stream.d.ts +12 -1
- package/dist/core/terminal-cause.d.ts +23 -9
- package/dist/core/tool-catalog-entries.js +6 -5
- package/dist/core/tool-face-lists.d.ts +3 -2
- package/dist/core/tool-face-lists.js +6 -0
- package/dist/core/tool-face-reader.d.ts +3 -2
- package/dist/core/tool-face-reader.js +2 -2
- package/dist/core/tool-face.d.ts +27 -3
- package/dist/core/tool-face.js +2 -2
- package/dist/core/tool-model-gate.js +14 -13
- package/dist/core/tool-mount-facts.d.ts +16 -3
- package/dist/core/tool-mount-facts.js +10 -0
- package/dist/core/tool-mount-plan.d.ts +79 -10
- package/dist/core/tool-mount-plan.js +79 -24
- package/dist/core/tool-mount-static.d.ts +52 -6
- package/dist/core/tool-mount-static.js +82 -28
- package/dist/core/tool-name-aliases.js +1 -1
- package/dist/core/tool-policy.d.ts +25 -5
- package/dist/core/tool-policy.js +8 -6
- package/dist/core/tool-registry.d.ts +12 -2
- package/dist/core/tool-registry.js +9 -2
- package/dist/core/tool-result-budget.d.ts +2 -2
- package/dist/core/tool-result-bytes.d.ts +28 -9
- package/dist/core/tool-result-bytes.js +7 -5
- package/dist/core/tool-result-store.d.ts +310 -38
- package/dist/core/tool-result-store.js +325 -30
- package/dist/core/tool-roster.d.ts +20 -8
- package/dist/core/tool-roster.js +16 -6
- package/dist/core/tool-search-setting.js +2 -2
- package/dist/core/tool-spec.d.ts +10 -10
- package/dist/core/types.d.ts +5 -5
- package/dist/core/types.js +2 -2
- package/dist/core/unc-spelling.d.ts +114 -3
- package/dist/core/unc-spelling.js +110 -0
- package/dist/core/untrusted-egress.d.ts +82 -10
- package/dist/core/untrusted-egress.js +102 -9
- package/dist/core/untrusted-text.d.ts +28 -0
- package/dist/core/untrusted-text.js +67 -3
- package/dist/core/user-attachment.d.ts +18 -3
- package/dist/core/user-attachment.js +37 -31
- package/dist/core/user-row-origin.d.ts +22 -0
- package/dist/core/user-row-origin.js +15 -0
- package/dist/core/version.d.ts +1 -1
- package/dist/core/version.js +2 -12
- package/dist/core/wiring-manifest.d.ts +124 -30
- package/dist/core/wiring-manifest.js +59 -17
- package/dist/core/workflow-journal-store.d.ts +186 -16
- package/dist/core/workflow-journal-store.js +156 -1
- package/dist/core/workflow-run-store-contract.js +82 -1
- package/dist/core/workflow-run-store.d.ts +40 -4
- package/dist/core/workflow-run-store.js +9 -2
- package/dist/core/workspace-snapshot.d.ts +4 -1
- package/dist/core/workspace-snapshot.js +1 -1
- package/dist/engine/compaction/compaction.d.ts +3 -3
- package/dist/engine/compaction/compaction.js +16 -18
- package/dist/engine/execution-env/node-execution-env.d.ts +14 -6
- package/dist/engine/execution-env/node-execution-env.js +49 -14
- package/dist/engine/harness/agent-harness.d.ts +17 -7
- package/dist/engine/harness/agent-harness.js +66 -16
- package/dist/engine/harness/types.d.ts +31 -2
- package/dist/engine/harness/types.js +14 -1
- package/dist/engine/llm/tool-turns.d.ts +19 -2
- package/dist/engine/llm/tool-turns.js +29 -2
- package/dist/engine/llm/types.d.ts +103 -22
- package/dist/engine/llm/types.js +1 -1
- package/dist/engine/loop/agent-loop.d.ts +18 -9
- package/dist/engine/loop/agent-loop.js +115 -24
- package/dist/engine/loop/types.d.ts +14 -6
- package/dist/engine/lsp/node-lsp-manager.d.ts +6 -4
- package/dist/engine/lsp/node-lsp-manager.js +22 -21
- package/dist/engine/session/import-validate.js +2 -0
- package/dist/engine/session/repo-utils.d.ts +13 -7
- package/dist/engine/session/repo-utils.js +9 -4
- package/dist/index.d.ts +48 -27
- package/dist/index.js +38 -20
- package/dist/internal/harness.d.ts +1 -1
- package/dist/internal/llm.d.ts +2 -2
- package/dist/internal/llm.js +1 -1
- package/dist/orchestration/goal.js +1 -1
- package/dist/orchestration/governance-baseline-validity.d.ts +20 -5
- package/dist/orchestration/governance-baseline-validity.js +87 -16
- package/dist/orchestration/run-workflow-tool.d.ts +15 -12
- package/dist/orchestration/run-workflow-tool.js +757 -331
- package/dist/orchestration/workflow-governance.d.ts +1 -1
- package/dist/orchestration/workflow-observe.d.ts +22 -2
- package/dist/orchestration/workflow-observe.js +20 -9
- package/dist/orchestration/workflow-park-adoption.d.ts +380 -0
- package/dist/orchestration/workflow-park-adoption.js +617 -0
- package/dist/orchestration/workflow-size-guideline.d.ts +6 -8
- package/dist/orchestration/workflow-size-guideline.js +12 -14
- package/dist/orchestration/workflow-types.d.ts +97 -17
- package/dist/orchestration/workflow-types.js +39 -8
- package/dist/orchestration/workflow.d.ts +70 -26
- package/dist/orchestration/workflow.js +495 -199
- package/dist/prompt-assembly/artifact.js +4 -3
- package/dist/prompt-assembly/assemble.d.ts +2 -0
- package/dist/prompt-assembly/assemble.js +4 -1
- package/dist/prompt-assembly/bands.d.ts +3 -0
- package/dist/prompt-assembly/bands.js +6 -0
- package/dist/prompt-assembly/catalog/constitution.d.ts +1 -1
- package/dist/prompt-assembly/catalog/constitution.js +74 -9
- package/dist/prompt-assembly/catalog/core-behavior.d.ts +5 -4
- package/dist/prompt-assembly/catalog/core-behavior.js +46 -9
- package/dist/prompt-assembly/catalog/engine.d.ts +110 -5
- package/dist/prompt-assembly/catalog/engine.js +13 -0
- package/dist/prompt-assembly/catalog/events.d.ts +852 -0
- package/dist/prompt-assembly/catalog/events.js +62 -0
- package/dist/prompt-assembly/catalog/index.js +2 -1
- package/dist/prompt-assembly/catalog/locales/index.d.ts +2 -2
- package/dist/prompt-assembly/catalog/locales/index.js +4 -2
- package/dist/prompt-assembly/catalog/locales/zh-Hans/constitution.js +53 -1
- package/dist/prompt-assembly/catalog/locales/zh-Hans/core-behavior.js +45 -9
- package/dist/prompt-assembly/catalog/locales/zh-Hans/engine.js +12 -0
- package/dist/prompt-assembly/catalog/locales/zh-Hans/events.d.ts +7 -0
- package/dist/prompt-assembly/catalog/locales/zh-Hans/events.js +57 -0
- package/dist/prompt-assembly/catalog/locales/zh-Hans/mechanism.d.ts +8 -0
- package/dist/prompt-assembly/catalog/locales/zh-Hans/mechanism.js +56 -0
- package/dist/prompt-assembly/catalog/locales/zh-Hans/modes.js +8 -2
- package/dist/prompt-assembly/catalog/locales/zh-Hans/role.js +37 -9
- package/dist/prompt-assembly/catalog/mechanism.d.ts +427 -0
- package/dist/prompt-assembly/catalog/mechanism.js +62 -0
- package/dist/prompt-assembly/catalog/memory.d.ts +3 -3
- package/dist/prompt-assembly/catalog/memory.js +12 -3
- package/dist/prompt-assembly/catalog/modes.d.ts +2 -2
- package/dist/prompt-assembly/catalog/modes.js +7 -1
- package/dist/prompt-assembly/catalog/role.d.ts +1 -1
- package/dist/prompt-assembly/catalog/role.js +43 -14
- package/dist/prompt-assembly/catalog/schema.d.ts +43 -20
- package/dist/prompt-assembly/catalog/schema.js +17 -6
- package/dist/prompt-assembly/catalog/sections.d.ts +1 -1
- package/dist/prompt-assembly/catalog/sections.js +20 -12
- package/dist/prompt-assembly/composer.d.ts +3 -0
- package/dist/prompt-assembly/composer.js +11 -5
- package/dist/prompt-assembly/engine-text.d.ts +14 -4
- package/dist/prompt-assembly/engine-text.js +18 -9
- package/dist/prompt-assembly/event-registry.d.ts +38 -31
- package/dist/prompt-assembly/event-registry.js +185 -25
- package/dist/prompt-assembly/explain.d.ts +24 -0
- package/dist/prompt-assembly/explain.js +11 -0
- package/dist/prompt-assembly/locale-review.js +3 -2
- package/dist/prompt-assembly/overlay.d.ts +25 -7
- package/dist/prompt-assembly/overlay.js +68 -14
- package/dist/prompt-assembly/select.d.ts +4 -3
- package/dist/prompt-assembly/select.js +10 -2
- package/dist/prompt-assembly/sugar.d.ts +3 -2
- package/dist/prompt-assembly/sugar.js +2 -2
- package/dist/prompt-assembly/tool-catalog.js +3 -2
- package/dist/prompt-assembly/turn-snapshot.d.ts +1 -0
- package/dist/prompt-assembly/turn-snapshot.js +1 -0
- package/dist/prompt-assembly/types.d.ts +3 -4
- package/dist/prompts/default.d.ts +6 -45
- package/dist/prompts/default.js +3 -21
- package/dist/stores/file/adoption/adopt.d.ts +13 -3
- package/dist/stores/file/adoption/adopt.js +90 -95
- package/dist/stores/file/adoption/marker.d.ts +2 -1
- package/dist/stores/file/background-agent-store.d.ts +61 -0
- package/dist/stores/file/background-agent-store.js +64 -15
- package/dist/stores/file/checkpoint-store.d.ts +30 -2
- package/dist/stores/file/checkpoint-store.js +72 -23
- package/dist/stores/file/file-history-store.d.ts +13 -7
- package/dist/stores/file/file-history-store.js +170 -46
- package/dist/stores/file/fs-atomic.d.ts +57 -2
- package/dist/stores/file/fs-atomic.js +116 -48
- package/dist/stores/file/index.d.ts +2 -0
- package/dist/stores/file/index.js +2 -0
- package/dist/stores/file/ledger-damage.d.ts +87 -0
- package/dist/stores/file/ledger-damage.js +103 -0
- package/dist/stores/file/session-policy-store.d.ts +36 -4
- package/dist/stores/file/session-policy-store.js +177 -6
- package/dist/stores/file/shared-ledger.d.ts +123 -11
- package/dist/stores/file/shared-ledger.js +113 -18
- package/dist/stores/file/tool-result-store.d.ts +30 -1
- package/dist/stores/file/tool-result-store.js +60 -6
- package/dist/stores/file/workflow-journal-store.d.ts +73 -26
- package/dist/stores/file/workflow-journal-store.js +195 -32
- package/dist/stores/file/workflow-run-store.d.ts +47 -2
- package/dist/stores/file/workflow-run-store.js +17 -14
- package/dist/tools/artifact/artifact-tool.d.ts +9 -6
- package/dist/tools/artifact/artifact-tool.js +76 -40
- package/dist/tools/fs/env-error-detail.d.ts +11 -0
- package/dist/tools/fs/env-error-detail.js +13 -2
- package/dist/tools/fs/fs-bash.js +30 -21
- package/dist/tools/fs/fs-pdf.d.ts +1 -1
- package/dist/tools/fs/fs-pdf.js +2 -2
- package/dist/tools/fs/fs-read.js +4 -4
- package/dist/tools/fs/fs-shared.d.ts +26 -9
- package/dist/tools/fs/fs-shared.js +12 -6
- package/dist/tools/fs/fs-write.d.ts +6 -4
- package/dist/tools/fs/fs-write.js +24 -18
- package/dist/tools/fs/pattern-kill-guard.js +2 -1
- package/dist/tools/fs/pdf.d.ts +8 -0
- package/dist/tools/fs/pdf.js +10 -0
- package/dist/tools/fs/program-read-model.d.ts +4 -0
- package/dist/tools/fs/program-read-model.rows.js +3 -3
- package/dist/tools/fs/read-floor.d.ts +11 -9
- package/dist/tools/fs/read-floor.js +7 -10
- package/dist/tools/fs/removal-targets.d.ts +4 -3
- package/dist/tools/fs/removal-targets.js +27 -9
- package/dist/tools/fs/safety.d.ts +29 -107
- package/dist/tools/fs/safety.js +16 -118
- package/dist/tools/fs/shell-read-check.d.ts +4 -2
- package/dist/tools/fs/shell-read-check.js +6 -2
- package/dist/tools/fs/shell-read-environment.d.ts +10 -2
- package/dist/tools/fs/shell-read-environment.js +20 -11
- package/dist/tools/fs/shell-read-judge.d.ts +19 -7
- package/dist/tools/fs/shell-read-judge.js +9 -3
- package/dist/tools/fs/shell-reentry-text.d.ts +8 -0
- package/dist/tools/fs/shell-reentry-text.js +56 -0
- package/dist/tools/monitor.js +7 -5
- package/dist/tools/scheduler-tools.d.ts +39 -1
- package/dist/tools/scheduler-tools.js +84 -35
- package/dist/tools/task-list.d.ts +12 -4
- package/dist/tools/task-list.js +10 -7
- package/dist/tools/web.d.ts +2 -8
- package/dist/tools/web.js +5 -5
- package/dist/tools/worktree.js +18 -15
- package/package.json +6 -3
- package/test/export-surface.snapshot.json +344 -14
- package/wire-changes/core-stores-sql@7.69.0.json +39 -0
- package/wire-changes/core@7.69.0.json +9561 -0
- package/wire-changes/mods-kernel@7.69.0.json +80 -0
- package/wire-changes/primitives@7.69.0.json +79 -0
- package/wire-changes/wiki-gate@7.69.0.json +39 -0
|
@@ -1,16 +1,23 @@
|
|
|
1
1
|
import { createAssistantMessageEventStream, } from "../internal/llm.js";
|
|
2
2
|
import { STREAM_TORN_WARNING } from "./stream-shared.js";
|
|
3
3
|
import { FLOOR_OUTPUT_TOKENS, parseContextOverflow, planOutputCapAdjustment } from "./context-overflow.js";
|
|
4
|
-
import { abortLikeName, BrainError, brainErrorCodeOf, classifyFetchThrow, classifyHttp, describeNetworkError, isProviderEchoShape, namesTheFailure, proxyTunnelRefusedSentence, readConnectFailure, readProviderErrorCode, readProviderRequestId, refusedTheRequest, thrownBrainFailure } from "./errors.js";
|
|
4
|
+
import { abortLikeName, BrainError, brainErrorCodeOf, classifyFetchThrow, classifyHttp, describeNetworkError, extractErrorCode, isProviderEchoShape, namesTheFailure, proxyTunnelRefusedSentence, readConnectFailure, readProviderErrorCode, readProviderRequestId, refusedTheRequest, statedEnvelope, thrownBrainFailure } from "./errors.js";
|
|
5
5
|
import { stripUrlCredentials } from "./route-adjudicator.js";
|
|
6
6
|
import { admittedRequestRoute } from "./request-route.js";
|
|
7
7
|
import { stampReasoning } from "./reasoning-stamp.js";
|
|
8
|
-
import {
|
|
8
|
+
import { resolutionOf } from "./reasoning-request.js";
|
|
9
|
+
import { isTransientStatus, isProviderCondition, isProviderConditionSource, judgeHttpFailure, PROVIDER_CONDITION_TRAITS, resolveProviderDialect, thinkingRefusalStated, } from "./provider-dialect.js";
|
|
9
10
|
import { FAST_MAX_BACKOFF_MS, providerWaitHint, retryBackoffMs } from "./retry.js";
|
|
10
|
-
import {
|
|
11
|
+
import { FAST_MODE_BETA, fastModeBetaRefused, fastModeFallbackOf, fastModeRouteOf, fastModeServerError, fastSpeedKeyOf, observeFastMode } from "./fast-mode.js";
|
|
12
|
+
import { headerListEntries } from "./request-params.js";
|
|
13
|
+
import { markRefusedBeforeAnyStream } from "./usage-reading.js";
|
|
14
|
+
import { bindBrainStatusEmitter, emitBrainStatus, emitBrainTelemetry, reportCapabilityWithheld, reportExtraBodyDropped } from "./status-sink.js";
|
|
15
|
+
import { capabilityOf, noteCapabilityBrokeOff, noteCapabilityCured, noteCapabilityTaken, rememberedCapabilityWithhold } from "./model-capability.js";
|
|
16
|
+
import { carryLeftBehind, leftEntriesOf } from "./model-lane.js";
|
|
11
17
|
import { createAttemptController, firstByteWindow, refuseRetiredTimeoutKnobs, resolveFirstByteRetries, resolveWatchdogMs, STALL_FIRST_BYTE_MS, STALL_FIRST_TOKEN_MS, STALL_IDLE_MS, } from "./timeout.js";
|
|
12
18
|
import { readMember, thrownText } from "../core/thrown-value.js";
|
|
13
19
|
import { remoteWords } from "../core/protocol-http-auth.js";
|
|
20
|
+
import { modelEntryOf } from "../core/model-receipt.js";
|
|
14
21
|
import { admittedProviderMessage, readProviderMessage } from "./provider-message.js";
|
|
15
22
|
const DEFAULT_MAX_RETRIES = 10;
|
|
16
23
|
const MAX_RETRIES_ENV_CEILING = 15;
|
|
@@ -66,6 +73,18 @@ function leafHolder(root, path) {
|
|
|
66
73
|
return undefined;
|
|
67
74
|
return { holder: cur, leaf };
|
|
68
75
|
}
|
|
76
|
+
function requestBodyObjectOf(raw) {
|
|
77
|
+
let parsed;
|
|
78
|
+
try {
|
|
79
|
+
parsed = JSON.parse(raw);
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed))
|
|
85
|
+
return undefined;
|
|
86
|
+
return parsed;
|
|
87
|
+
}
|
|
69
88
|
export function sameRequestModuloCap(original, rebuilt, imposedCap) {
|
|
70
89
|
if (rebuilt.url !== original.url || !sameHeaders(rebuilt.headers, original.headers))
|
|
71
90
|
return false;
|
|
@@ -73,20 +92,8 @@ export function sameRequestModuloCap(original, rebuilt, imposedCap) {
|
|
|
73
92
|
return false;
|
|
74
93
|
if (rebuilt.body === original.body)
|
|
75
94
|
return true;
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
try {
|
|
79
|
-
parsed = JSON.parse(raw);
|
|
80
|
-
}
|
|
81
|
-
catch {
|
|
82
|
-
return undefined;
|
|
83
|
-
}
|
|
84
|
-
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed))
|
|
85
|
-
return undefined;
|
|
86
|
-
return parsed;
|
|
87
|
-
};
|
|
88
|
-
const a = parse(original.body);
|
|
89
|
-
const b = parse(rebuilt.body);
|
|
95
|
+
const a = requestBodyObjectOf(original.body);
|
|
96
|
+
const b = requestBodyObjectOf(rebuilt.body);
|
|
90
97
|
if (a === undefined || b === undefined)
|
|
91
98
|
return false;
|
|
92
99
|
for (const path of new Set([...(original.capDerivedPaths ?? []), ...(rebuilt.capDerivedPaths ?? [])])) {
|
|
@@ -107,6 +114,35 @@ export function sameRequestModuloCap(original, rebuilt, imposedCap) {
|
|
|
107
114
|
}
|
|
108
115
|
return canonicalJson(a) === canonicalJson(b);
|
|
109
116
|
}
|
|
117
|
+
export function sameRequestModuloSpeed(original, rebuilt, api) {
|
|
118
|
+
const key = fastSpeedKeyOf(api);
|
|
119
|
+
if (key === undefined || rebuilt.url !== original.url || rebuilt.outputCapTokens !== original.outputCapTokens)
|
|
120
|
+
return false;
|
|
121
|
+
const withoutFastBeta = (headers) => {
|
|
122
|
+
const out = { ...headers };
|
|
123
|
+
const list = out["anthropic-beta"];
|
|
124
|
+
if (list === undefined)
|
|
125
|
+
return out;
|
|
126
|
+
const rest = headerListEntries(list).filter((entry) => entry !== FAST_MODE_BETA);
|
|
127
|
+
if (rest.length > 0)
|
|
128
|
+
out["anthropic-beta"] = rest.join(",");
|
|
129
|
+
else
|
|
130
|
+
delete out["anthropic-beta"];
|
|
131
|
+
return out;
|
|
132
|
+
};
|
|
133
|
+
if (!sameHeaders(withoutFastBeta(rebuilt.headers), withoutFastBeta(original.headers)))
|
|
134
|
+
return false;
|
|
135
|
+
const a = requestBodyObjectOf(original.body);
|
|
136
|
+
const b = requestBodyObjectOf(rebuilt.body);
|
|
137
|
+
if (a === undefined || b === undefined || Object.prototype.hasOwnProperty.call(b, key))
|
|
138
|
+
return false;
|
|
139
|
+
const { [key]: _speed, ...rest } = a;
|
|
140
|
+
return canonicalJson(rest) === canonicalJson(b);
|
|
141
|
+
}
|
|
142
|
+
function carriesFastModeBeta(headers) {
|
|
143
|
+
const list = headers["anthropic-beta"];
|
|
144
|
+
return list !== undefined && headerListEntries(list).includes(FAST_MODE_BETA);
|
|
145
|
+
}
|
|
110
146
|
const ERROR_BODY_BYTE_CAP = 64 * 1024;
|
|
111
147
|
const ERROR_BODY_READ_TIMEOUT_MS = 5_000;
|
|
112
148
|
async function readErrorBody(res, signal) {
|
|
@@ -196,8 +232,25 @@ export function shouldRetryHeaderVerdict(res) {
|
|
|
196
232
|
return undefined;
|
|
197
233
|
}
|
|
198
234
|
const RETRY_STATUS_SLICE_MS = 30_000;
|
|
199
|
-
export const
|
|
200
|
-
export const
|
|
235
|
+
export const WAITING_AFTER_MS = 30_000;
|
|
236
|
+
export const WAITING_EVERY_MS = 30_000;
|
|
237
|
+
const WAIT_DETAIL = {
|
|
238
|
+
response_headers: "waiting for the first token",
|
|
239
|
+
first_token: "waiting for the first token",
|
|
240
|
+
next_token: "waiting for the response to continue",
|
|
241
|
+
};
|
|
242
|
+
const KEPT_CUT_WAIT_DETAIL = "the response was cut; what had arrived was kept";
|
|
243
|
+
const NO_WAIT_DISCLOSURE = { stop: () => undefined, expire: () => undefined, restart: () => undefined };
|
|
244
|
+
function cutArmOf(snap, left) {
|
|
245
|
+
if (snap.terminalFrameSeen)
|
|
246
|
+
return "whole";
|
|
247
|
+
if (snap.hasSubstantiveText || snap.hasCompletedToolCall)
|
|
248
|
+
return "keep";
|
|
249
|
+
if (snap.hasOnlyThinking)
|
|
250
|
+
return left.thinking ? "resend_thinking" : "fail";
|
|
251
|
+
return left.shared ? "resend" : "fail";
|
|
252
|
+
}
|
|
253
|
+
const WAIT_OUTCOME_OF_CUT = { whole: undefined, keep: "keep", resend_thinking: "retry", resend: "retry", fail: "fail" };
|
|
201
254
|
const THINKING_RETRY_BUDGET = 2;
|
|
202
255
|
const EMPTY_USAGE = {
|
|
203
256
|
input: 0,
|
|
@@ -209,6 +262,22 @@ function reportRequestStrip(model, req) {
|
|
|
209
262
|
if (req.extraBodyDropped !== undefined && req.extraBodyDropped.length > 0)
|
|
210
263
|
reportExtraBodyDropped({ api: model.api, modelId: model.id, keys: req.extraBodyDropped });
|
|
211
264
|
}
|
|
265
|
+
class TriedAttemptEnded extends Error {
|
|
266
|
+
ended;
|
|
267
|
+
constructor(ended) {
|
|
268
|
+
super(ended.errorMessage ?? "the response ended short of an answer");
|
|
269
|
+
this.ended = ended;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
function speedOfRequest(req) {
|
|
273
|
+
return req.fastSpeed === true ? "fast" : "standard";
|
|
274
|
+
}
|
|
275
|
+
function triedCapabilityOf(model, req) {
|
|
276
|
+
return thinkingKeyWritten(model, req) && capabilityOf(model, "reasoning") === "unknown" ? "reasoning" : undefined;
|
|
277
|
+
}
|
|
278
|
+
function thinkingKeyWritten(model, req) {
|
|
279
|
+
return req.reasoning !== undefined && req.reasoning.level !== "off" && resolutionOf(req.reasoning.level, req.reasoning.carried, model.api).dropped !== true;
|
|
280
|
+
}
|
|
212
281
|
function emptyAssistant(model) {
|
|
213
282
|
return {
|
|
214
283
|
role: "assistant",
|
|
@@ -271,6 +340,7 @@ const RETRY_PHASE_OF_ERR_CLASS = {
|
|
|
271
340
|
server: "retrying",
|
|
272
341
|
http: "retrying",
|
|
273
342
|
output_cap: "retrying",
|
|
343
|
+
capability: "retrying",
|
|
274
344
|
};
|
|
275
345
|
Object.freeze(RETRY_PHASE_OF_ERR_CLASS);
|
|
276
346
|
const TRANSPORT_RETRY_DETAIL = "connection lost, reconnecting";
|
|
@@ -296,24 +366,38 @@ export function runStreamingBrain(args) {
|
|
|
296
366
|
const out = createAssistantMessageEventStream();
|
|
297
367
|
let reasoningSeen;
|
|
298
368
|
const pushEvent = out.push.bind(out);
|
|
369
|
+
let callEnd;
|
|
299
370
|
out.push = (event) => {
|
|
300
371
|
const terminal = event.type === "done" ? event.message : event.type === "error" ? event.error : undefined;
|
|
301
372
|
if (terminal !== undefined && reasoningSeen !== undefined)
|
|
302
|
-
stampReasoning(terminal, { model, ...reasoningSeen });
|
|
373
|
+
stampReasoning(terminal, { model: modelEntryOf(model), ...reasoningSeen });
|
|
374
|
+
if (terminal !== undefined)
|
|
375
|
+
carryLeftBehind(terminal, leftBehind);
|
|
376
|
+
if (terminal !== undefined)
|
|
377
|
+
callEnd = { failed: event.type === "error", partialFinalized: terminal.partialFinalized === true };
|
|
378
|
+
if (terminal !== undefined && (terminal.isApiErrorMessage === true || terminal.partialFinalized === true))
|
|
379
|
+
brokeOff?.();
|
|
303
380
|
pushEvent(event);
|
|
304
381
|
};
|
|
305
382
|
let cleanup;
|
|
306
383
|
let announcedRetry = false;
|
|
307
384
|
let announcedWait = false;
|
|
308
|
-
let
|
|
385
|
+
let lastWaitSegment;
|
|
309
386
|
let firstTokenArrived = false;
|
|
310
|
-
let
|
|
311
|
-
let terminalRetryDetail = "recovered after retrying";
|
|
387
|
+
let thrownDetail;
|
|
312
388
|
let requestIdSeen;
|
|
313
389
|
let attemptPartial;
|
|
314
390
|
let attemptOutput;
|
|
391
|
+
let refusedOutright;
|
|
315
392
|
let routeSeen;
|
|
316
393
|
let declaredSeen = [];
|
|
394
|
+
let triedFailure;
|
|
395
|
+
let declaredRefused = false;
|
|
396
|
+
const leftBehind = [];
|
|
397
|
+
let brokeOff;
|
|
398
|
+
const failureNotes = () => `${declaredRefused ? ` — model "${model.id}" declares reasoning (Model.reasoning: true) and the endpoint refused the thinking parameters; nothing was withheld: correct the model's catalog entry or the endpoint` : ""}${triedFailure !== undefined
|
|
399
|
+
? ` — before this, the same request carrying the thinking parameters failed (${namesTheFailure(triedFailure.status) ? `HTTP ${triedFailure.status}` : triedFailure.code !== undefined ? `[${triedFailure.code}]` : "no answer"}); the model's entry does not declare reasoning, so it was sent again without them`
|
|
400
|
+
: ""}`;
|
|
317
401
|
const echoCarried = (value, field) => {
|
|
318
402
|
if (remoteWords(value, declaredSeen) === value)
|
|
319
403
|
return value;
|
|
@@ -323,15 +407,16 @@ export function runStreamingBrain(args) {
|
|
|
323
407
|
void run()
|
|
324
408
|
.catch((err) => {
|
|
325
409
|
const aborted = signal?.aborted === true || isAbortError(err);
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
410
|
+
thrownDetail = {
|
|
411
|
+
retry: aborted ? "cancelled while retrying" : "retries exhausted",
|
|
412
|
+
wait: firstTokenArrived
|
|
413
|
+
? aborted
|
|
414
|
+
? "cancelled after the first token arrived"
|
|
415
|
+
: "the call failed after the first token arrived"
|
|
416
|
+
: aborted
|
|
417
|
+
? "cancelled while waiting for the first token"
|
|
418
|
+
: "the first-token wait ended in failure",
|
|
419
|
+
};
|
|
335
420
|
const errorMsg = emptyAssistant(model);
|
|
336
421
|
const kept = aborted ? undefined : attemptOutput;
|
|
337
422
|
if (kept !== undefined) {
|
|
@@ -340,7 +425,7 @@ export function runStreamingBrain(args) {
|
|
|
340
425
|
}
|
|
341
426
|
errorMsg.stopReason = aborted ? "aborted" : "error";
|
|
342
427
|
const said = thrownText(err);
|
|
343
|
-
errorMsg.errorMessage = brainErrorCodeOf(err) !== undefined ? said : remoteWords(said, declaredSeen)
|
|
428
|
+
errorMsg.errorMessage = `${brainErrorCodeOf(err) !== undefined ? said : remoteWords(said, declaredSeen)}${aborted ? "" : failureNotes()}`;
|
|
344
429
|
const failure = aborted ? {} : thrownBrainFailure(err);
|
|
345
430
|
if (failure.code !== undefined) {
|
|
346
431
|
if (failure.errorKind !== undefined)
|
|
@@ -384,6 +469,8 @@ export function runStreamingBrain(args) {
|
|
|
384
469
|
}
|
|
385
470
|
if (kept === undefined || kept.usageMissing === true)
|
|
386
471
|
errorMsg.usageMissing = true;
|
|
472
|
+
if (!aborted && kept === undefined && refusedOutright === true)
|
|
473
|
+
markRefusedBeforeAnyStream(errorMsg.usage);
|
|
387
474
|
const seenResponseModel = attemptPartial?.responseModel;
|
|
388
475
|
if (seenResponseModel !== undefined)
|
|
389
476
|
errorMsg.responseModel = seenResponseModel;
|
|
@@ -394,7 +481,21 @@ export function runStreamingBrain(args) {
|
|
|
394
481
|
if (!announcedRetry && !announcedWait)
|
|
395
482
|
return;
|
|
396
483
|
try {
|
|
397
|
-
|
|
484
|
+
const failed = callEnd?.failed !== false;
|
|
485
|
+
const detail = thrownDetail !== undefined
|
|
486
|
+
? announcedRetry
|
|
487
|
+
? thrownDetail.retry
|
|
488
|
+
: thrownDetail.wait
|
|
489
|
+
: failed
|
|
490
|
+
? "the response ended in an error"
|
|
491
|
+
: callEnd?.partialFinalized === true
|
|
492
|
+
? KEPT_CUT_WAIT_DETAIL
|
|
493
|
+
: announcedRetry
|
|
494
|
+
? "recovered after retrying"
|
|
495
|
+
: lastWaitSegment === "next_token"
|
|
496
|
+
? "the response went on"
|
|
497
|
+
: "first token arrived";
|
|
498
|
+
emitBrainStatus({ phase: failed ? "gave_up" : "recovered", detail });
|
|
398
499
|
}
|
|
399
500
|
catch {
|
|
400
501
|
}
|
|
@@ -402,6 +503,9 @@ export function runStreamingBrain(args) {
|
|
|
402
503
|
return out;
|
|
403
504
|
async function run() {
|
|
404
505
|
let req = buildRequest();
|
|
506
|
+
const tried = triedCapabilityOf(model, req);
|
|
507
|
+
let untriedOverrides = tried === undefined ? undefined : { withhold: [...(req.withheld ?? []), tried] };
|
|
508
|
+
let untried = untriedOverrides === undefined ? undefined : buildRequest(untriedOverrides);
|
|
405
509
|
const commit = (r) => {
|
|
406
510
|
r.onCommitted?.();
|
|
407
511
|
reportRequestStrip(model, r);
|
|
@@ -411,7 +515,125 @@ export function runStreamingBrain(args) {
|
|
|
411
515
|
};
|
|
412
516
|
commit(req);
|
|
413
517
|
const dialect = resolveProviderDialect(req.url, model.api);
|
|
414
|
-
let
|
|
518
|
+
let overrides = { withhold: req.withheld ?? [] };
|
|
519
|
+
let cureOpen = false;
|
|
520
|
+
let withheldAt = speedOfRequest(req);
|
|
521
|
+
let fallbackAnnouncement;
|
|
522
|
+
const taken = () => {
|
|
523
|
+
if (untried === undefined || tried === undefined)
|
|
524
|
+
return;
|
|
525
|
+
untried = undefined;
|
|
526
|
+
untriedOverrides = undefined;
|
|
527
|
+
const speed = speedOfRequest(req);
|
|
528
|
+
noteCapabilityTaken(model, req.url, speed, tried);
|
|
529
|
+
brokeOff = () => noteCapabilityBrokeOff(model, req.url, speed, tried);
|
|
530
|
+
};
|
|
531
|
+
const succeeded = () => {
|
|
532
|
+
if (untried !== undefined)
|
|
533
|
+
return;
|
|
534
|
+
const asked = req.reasoning?.level;
|
|
535
|
+
if (asked === undefined || asked === "off" || !(req.withheld ?? []).includes("reasoning"))
|
|
536
|
+
return;
|
|
537
|
+
const cure = triedFailure?.refusal === true && cureOpen ? { why: triedFailure.named ? "named" : "refused" } : undefined;
|
|
538
|
+
if (cure !== undefined) {
|
|
539
|
+
cureOpen = false;
|
|
540
|
+
noteCapabilityCured(model, req.url, withheldAt, "reasoning", cure);
|
|
541
|
+
}
|
|
542
|
+
const memory = rememberedCapabilityWithhold(model, req.url, withheldAt, "reasoning");
|
|
543
|
+
const why = memory?.why ?? cure?.why;
|
|
544
|
+
if (why === undefined)
|
|
545
|
+
return;
|
|
546
|
+
reportCapabilityWithheld({
|
|
547
|
+
modelId: model.id,
|
|
548
|
+
capability: "reasoning",
|
|
549
|
+
cause: "downgraded",
|
|
550
|
+
requested: asked,
|
|
551
|
+
why,
|
|
552
|
+
remembered: memory !== undefined,
|
|
553
|
+
speed: withheldAt,
|
|
554
|
+
budgetForm: model.api === "anthropic-messages" && model.compat?.thinkingMode !== "adaptive",
|
|
555
|
+
});
|
|
556
|
+
};
|
|
557
|
+
const refusesDeclaredThinking = (envelope) => envelope !== undefined && capabilityOf(model, "reasoning") === "declared_on" && thinkingKeyWritten(model, req) && thinkingRefusalStated(envelope);
|
|
558
|
+
let fastReplayForbidden = false;
|
|
559
|
+
let fastServerErrors = 0;
|
|
560
|
+
let fastShortWaitSpentMs = 0;
|
|
561
|
+
const resend = (base, change, same) => {
|
|
562
|
+
const next = { ...base.overrides, ...change };
|
|
563
|
+
let rebuilt;
|
|
564
|
+
try {
|
|
565
|
+
rebuilt = buildRequest(next);
|
|
566
|
+
}
|
|
567
|
+
catch {
|
|
568
|
+
return { declined: "unbuilt" };
|
|
569
|
+
}
|
|
570
|
+
try {
|
|
571
|
+
return same(base.request, rebuilt) ? { request: rebuilt, overrides: next } : { declined: "moved" };
|
|
572
|
+
}
|
|
573
|
+
catch {
|
|
574
|
+
return { declined: "moved" };
|
|
575
|
+
}
|
|
576
|
+
};
|
|
577
|
+
const reshapeCall = (change, same) => {
|
|
578
|
+
const committed = resend({ request: req, overrides }, change, same);
|
|
579
|
+
if ("declined" in committed)
|
|
580
|
+
return committed;
|
|
581
|
+
const twin = untried !== undefined && untriedOverrides !== undefined ? resend({ request: untried, overrides: untriedOverrides }, change, same) : undefined;
|
|
582
|
+
if (twin !== undefined && "declined" in twin)
|
|
583
|
+
return twin;
|
|
584
|
+
req = committed.request;
|
|
585
|
+
overrides = committed.overrides;
|
|
586
|
+
if (twin !== undefined) {
|
|
587
|
+
untried = twin.request;
|
|
588
|
+
untriedOverrides = twin.overrides;
|
|
589
|
+
}
|
|
590
|
+
cureOpen = false;
|
|
591
|
+
commit(req);
|
|
592
|
+
return undefined;
|
|
593
|
+
};
|
|
594
|
+
const fastRuled = (r) => req.fastSpeed === true || (args.fastMode !== undefined && !isTransientStatus(r.status) && carriesFastModeBeta(req.headers));
|
|
595
|
+
const retryTelemetry = (r) => emitBrainTelemetry({ kind: "retry", attempt: attempt + 1, phase: "connect", errClass: r.status === 429 ? "rate_limit" : r.status >= 500 ? "server" : "http", nextDelayMs: 0 });
|
|
596
|
+
const fastModeRule = (r, body, retryWaitMs) => {
|
|
597
|
+
const route = fastModeRouteOf(model);
|
|
598
|
+
const withoutBeta = (original, rebuilt) => rebuilt.fastSpeed !== true && !carriesFastModeBeta(rebuilt.headers) && sameRequestModuloSpeed(original, rebuilt, model.api);
|
|
599
|
+
if (carriesFastModeBeta(req.headers) && fastModeBetaRefused(r.status, body) && reshapeCall({ standardSpeed: true, withoutFastModeBeta: true }, withoutBeta) === undefined) {
|
|
600
|
+
observeFastMode(args.fastMode, { kind: "beta_refused", route, modelId: model.id });
|
|
601
|
+
retryTelemetry(r);
|
|
602
|
+
return "standard";
|
|
603
|
+
}
|
|
604
|
+
if (req.fastSpeed !== true)
|
|
605
|
+
return "keep";
|
|
606
|
+
if (fastModeServerError(r.status, body))
|
|
607
|
+
fastServerErrors++;
|
|
608
|
+
const fallback = fastModeFallbackOf({
|
|
609
|
+
status: r.status,
|
|
610
|
+
headers: r.headers,
|
|
611
|
+
...(body !== undefined ? { body } : {}),
|
|
612
|
+
route,
|
|
613
|
+
modelId: model.id,
|
|
614
|
+
serverErrors: fastServerErrors,
|
|
615
|
+
retriesLeft: attempt < maxRetries,
|
|
616
|
+
shortWaitSpentMs: fastShortWaitSpentMs,
|
|
617
|
+
retryWaitMs: retryWaitMs(),
|
|
618
|
+
now: Date.now(),
|
|
619
|
+
});
|
|
620
|
+
if (fallback.kind === "keep") {
|
|
621
|
+
if (fallback.shortWaitMs === undefined)
|
|
622
|
+
return "keep";
|
|
623
|
+
fastShortWaitSpentMs += fallback.shortWaitMs;
|
|
624
|
+
return "wait";
|
|
625
|
+
}
|
|
626
|
+
if (fallback.observation !== undefined)
|
|
627
|
+
observeFastMode(args.fastMode, fallback.observation);
|
|
628
|
+
if (fallback.kind === "standard" && reshapeCall({ standardSpeed: true }, (original, rebuilt) => rebuilt.fastSpeed !== true && sameRequestModuloSpeed(original, rebuilt, model.api)) === undefined) {
|
|
629
|
+
retryTelemetry(r);
|
|
630
|
+
if (fallback.counted === true)
|
|
631
|
+
attempt++;
|
|
632
|
+
return "standard";
|
|
633
|
+
}
|
|
634
|
+
fastReplayForbidden = true;
|
|
635
|
+
return "ends";
|
|
636
|
+
};
|
|
415
637
|
const maxRetries = Math.min(resolveMaxRetries(config.maxRetries), callRetryCap);
|
|
416
638
|
const baseDelay = config.retryDelayMs ?? 500;
|
|
417
639
|
const deterministicConnectBudget = Math.min(resolveDeterministicConnectRetries(config.deterministicConnectRetries), maxRetries);
|
|
@@ -421,29 +643,42 @@ export function runStreamingBrain(args) {
|
|
|
421
643
|
const firstTokenTimeoutMs = resolveWatchdogMs(config.firstTokenTimeoutMs, stallTimeouts?.firstTokenMs, STALL_FIRST_TOKEN_MS, "firstTokenTimeoutMs");
|
|
422
644
|
const idleTimeoutMs = resolveWatchdogMs(config.idleTimeoutMs, stallTimeouts?.idleMs, STALL_IDLE_MS, "idleTimeoutMs");
|
|
423
645
|
const firstByteRetryBudget = resolveFirstByteRetries(config.firstByteRetries);
|
|
424
|
-
const armWaitDisclosure = (ceilingMs, waitSegment) => {
|
|
425
|
-
if (!(ceilingMs >=
|
|
426
|
-
return
|
|
427
|
-
|
|
646
|
+
const armWaitDisclosure = (ceilingMs, waitSegment, cutOf) => {
|
|
647
|
+
if (!(ceilingMs >= WAITING_AFTER_MS))
|
|
648
|
+
return NO_WAIT_DISCLOSURE;
|
|
649
|
+
let startedAt = Date.now();
|
|
428
650
|
const emitWaitFrame = bindBrainStatusEmitter();
|
|
429
651
|
let timer;
|
|
430
652
|
let stopped = false;
|
|
431
653
|
let disclosed = false;
|
|
654
|
+
let dueMs = WAITING_AFTER_MS;
|
|
432
655
|
const disclose = () => {
|
|
656
|
+
const atTimeout = cutOf();
|
|
657
|
+
if (atTimeout === undefined)
|
|
658
|
+
return;
|
|
433
659
|
disclosed = true;
|
|
434
660
|
announcedWait = true;
|
|
435
|
-
|
|
661
|
+
lastWaitSegment = waitSegment;
|
|
662
|
+
emitWaitFrame({ phase: "waiting", detail: WAIT_DETAIL[waitSegment], elapsedMs: Date.now() - startedAt, timeoutMs: ceilingMs, waitSegment, atTimeout });
|
|
436
663
|
};
|
|
437
664
|
const schedule = (delayMs) => {
|
|
438
665
|
timer = setTimeout(() => {
|
|
439
666
|
timer = undefined;
|
|
440
|
-
if (stopped
|
|
667
|
+
if (stopped)
|
|
668
|
+
return;
|
|
669
|
+
const waited = Date.now() - startedAt;
|
|
670
|
+
if (waited >= ceilingMs)
|
|
441
671
|
return;
|
|
672
|
+
if (waited < dueMs) {
|
|
673
|
+
schedule(dueMs - waited);
|
|
674
|
+
return;
|
|
675
|
+
}
|
|
442
676
|
disclose();
|
|
443
|
-
|
|
677
|
+
dueMs = waited + WAITING_EVERY_MS;
|
|
678
|
+
schedule(WAITING_EVERY_MS);
|
|
444
679
|
}, delayMs);
|
|
445
680
|
};
|
|
446
|
-
schedule(
|
|
681
|
+
schedule(WAITING_AFTER_MS);
|
|
447
682
|
const stop = () => {
|
|
448
683
|
stopped = true;
|
|
449
684
|
if (timer)
|
|
@@ -457,6 +692,15 @@ export function runStreamingBrain(args) {
|
|
|
457
692
|
disclose();
|
|
458
693
|
stop();
|
|
459
694
|
},
|
|
695
|
+
restart: () => {
|
|
696
|
+
if (stopped)
|
|
697
|
+
return;
|
|
698
|
+
startedAt = Date.now();
|
|
699
|
+
dueMs = WAITING_AFTER_MS;
|
|
700
|
+
disclosed = false;
|
|
701
|
+
if (timer === undefined)
|
|
702
|
+
schedule(WAITING_AFTER_MS);
|
|
703
|
+
},
|
|
460
704
|
};
|
|
461
705
|
};
|
|
462
706
|
const waitAnnouncingRetry = async (announcement, lane) => {
|
|
@@ -478,17 +722,61 @@ export function runStreamingBrain(args) {
|
|
|
478
722
|
let firstByteStalls = 0;
|
|
479
723
|
const firstByteWindowsWaited = [];
|
|
480
724
|
let startEmitted = false;
|
|
725
|
+
const firstByteRetryLeft = () => firstByteStalls < firstByteRetryBudget;
|
|
726
|
+
const streamCutOf = (snap) => cutArmOf(snap, { thinking: thinkingRetries < THINKING_RETRY_BUDGET, shared: attempt < maxRetries });
|
|
727
|
+
const followedBy = (code) => (signal?.aborted !== true && code !== "auth" ? untried : undefined);
|
|
728
|
+
const fallBack = (failure) => {
|
|
729
|
+
const next = followedBy(failure.code);
|
|
730
|
+
if (next === undefined)
|
|
731
|
+
return false;
|
|
732
|
+
const named = failure.statement !== undefined && thinkingRefusalStated(failure.statement.envelope);
|
|
733
|
+
const refusal = failure.statement !== undefined && (named || (failure.code === "invalid_request" && !failure.statement.inputTooLong));
|
|
734
|
+
triedFailure = { code: failure.code, status: failure.status, refusal, named };
|
|
735
|
+
const spent = emptyAssistant(model);
|
|
736
|
+
if (failure.spent !== undefined)
|
|
737
|
+
spent.usage = failure.spent.usage;
|
|
738
|
+
if (failure.spent === undefined || failure.spent.usageMissing === true)
|
|
739
|
+
spent.usageMissing = true;
|
|
740
|
+
if (failure.spent === undefined && refusedTheRequest(failure.status))
|
|
741
|
+
markRefusedBeforeAnyStream(spent.usage);
|
|
742
|
+
leftBehind.push(...leftEntriesOf(spent, model.id));
|
|
743
|
+
req = next;
|
|
744
|
+
cureOpen = true;
|
|
745
|
+
withheldAt = speedOfRequest(next);
|
|
746
|
+
overrides = untriedOverrides ?? { withhold: next.withheld ?? [] };
|
|
747
|
+
untried = undefined;
|
|
748
|
+
untriedOverrides = undefined;
|
|
749
|
+
commit(req);
|
|
750
|
+
firstByteWindowsWaited.length = 0;
|
|
751
|
+
fallbackAnnouncement = { errorStatus: namesTheFailure(failure.status) ? failure.status : undefined, waitMs: failure.waitMs ?? 0 };
|
|
752
|
+
return true;
|
|
753
|
+
};
|
|
481
754
|
for (;;) {
|
|
482
755
|
let res;
|
|
483
756
|
let connect;
|
|
484
757
|
for (;;) {
|
|
758
|
+
if (fallbackAnnouncement !== undefined && signal?.aborted !== true) {
|
|
759
|
+
const { errorStatus, waitMs } = fallbackAnnouncement;
|
|
760
|
+
fallbackAnnouncement = undefined;
|
|
761
|
+
emitBrainTelemetry({ kind: "retry", attempt: 1, phase: "connect", errClass: "capability", nextDelayMs: waitMs });
|
|
762
|
+
announcedRetry = true;
|
|
763
|
+
await sleepAnnouncingRetry(waitMs, signal, (remainingMs) => ({
|
|
764
|
+
phase: RETRY_PHASE_OF_ERR_CLASS.capability,
|
|
765
|
+
detail: "the request carrying the thinking parameters failed before any answer; sending it without them (the model's entry does not declare reasoning)",
|
|
766
|
+
...(waitMs > 0 ? { retryInSec: Math.ceil(remainingMs / 1000), retryInMs: remainingMs } : {}),
|
|
767
|
+
attempt: 1,
|
|
768
|
+
maxRetries: 1,
|
|
769
|
+
errClass: "capability",
|
|
770
|
+
...(errorStatus !== undefined ? { errorStatus } : {}),
|
|
771
|
+
}));
|
|
772
|
+
}
|
|
485
773
|
if (signal?.aborted) {
|
|
486
774
|
const e = new Error("aborted");
|
|
487
775
|
e.name = "AbortError";
|
|
488
776
|
throw e;
|
|
489
777
|
}
|
|
490
778
|
const window = firstByteWindow(firstByteTimeoutMs, Buffer.byteLength(req.body), firstByteStalls);
|
|
491
|
-
const headerWait = armWaitDisclosure(window.windowMs, "response_headers");
|
|
779
|
+
const headerWait = armWaitDisclosure(window.windowMs, "response_headers", () => (firstByteRetryLeft() ? "retry" : "fail"));
|
|
492
780
|
const cc = createAttemptController(window.windowMs, signal);
|
|
493
781
|
let r;
|
|
494
782
|
let netErr;
|
|
@@ -549,6 +837,38 @@ export function runStreamingBrain(args) {
|
|
|
549
837
|
else
|
|
550
838
|
headerWait.stop();
|
|
551
839
|
}
|
|
840
|
+
refusedOutright = (refusedOutright ?? true) && r !== undefined && refusedTheRequest(r.status);
|
|
841
|
+
let errorBody;
|
|
842
|
+
let speedRuled;
|
|
843
|
+
if (!(r && r.ok && r.body) && followedBy(r !== undefined ? classifyHttp(r.status) : "network") !== undefined) {
|
|
844
|
+
cc.clearTimer();
|
|
845
|
+
try {
|
|
846
|
+
if (r !== undefined)
|
|
847
|
+
errorBody = await readErrorBody(r, signal);
|
|
848
|
+
}
|
|
849
|
+
finally {
|
|
850
|
+
cc.abort();
|
|
851
|
+
cc.dispose();
|
|
852
|
+
}
|
|
853
|
+
let fallBackWait;
|
|
854
|
+
const fallBackWaitMs = () => (fallBackWait ??= { ms: providerWaitHint(r)?.ms }).ms;
|
|
855
|
+
if (r !== undefined && fastRuled(r))
|
|
856
|
+
speedRuled = fastModeRule(r, errorBody, () => fallBackWaitMs() ?? 0);
|
|
857
|
+
if (speedRuled === "standard")
|
|
858
|
+
continue;
|
|
859
|
+
if (speedRuled !== "ends") {
|
|
860
|
+
const stated = r !== undefined && namesTheFailure(r.status) ? judgeHttpFailure({ dialect, status: r.status, headers: r.headers, body: errorBody }) : undefined;
|
|
861
|
+
const envelope = statedEnvelope(errorBody);
|
|
862
|
+
fallBack({
|
|
863
|
+
code: r !== undefined ? classifyHttp(r.status) : "network",
|
|
864
|
+
status: r?.status,
|
|
865
|
+
statement: envelope !== undefined && shouldRetryHeaderVerdict(r) !== true ? { envelope, inputTooLong: stated?.inputTooLong === "yes" } : undefined,
|
|
866
|
+
spent: undefined,
|
|
867
|
+
waitMs: fallBackWaitMs(),
|
|
868
|
+
});
|
|
869
|
+
continue;
|
|
870
|
+
}
|
|
871
|
+
}
|
|
552
872
|
if (r !== undefined && (r.type === "opaqueredirect" || (r.status >= 300 && r.status < 400))) {
|
|
553
873
|
const status = r.status;
|
|
554
874
|
discardResponseBody(r);
|
|
@@ -575,7 +895,7 @@ export function runStreamingBrain(args) {
|
|
|
575
895
|
continue;
|
|
576
896
|
}
|
|
577
897
|
if (firstByteStall !== undefined) {
|
|
578
|
-
if (
|
|
898
|
+
if (!firstByteRetryLeft())
|
|
579
899
|
throw firstByteStall.err;
|
|
580
900
|
firstByteStalls++;
|
|
581
901
|
const delayMs = retryBackoffMs(baseDelay, firstByteStalls - 1);
|
|
@@ -584,34 +904,17 @@ export function runStreamingBrain(args) {
|
|
|
584
904
|
await waitAnnouncingRetry(retryAnnouncement(firstByteStall), { phase: "connect", attempt: firstByteStalls, maxRetries: firstByteRetryBudget, delayMs });
|
|
585
905
|
continue;
|
|
586
906
|
}
|
|
587
|
-
let errorBody;
|
|
588
907
|
let overflowDeclineNote = "";
|
|
589
908
|
if (r !== undefined && r.status === 400) {
|
|
590
|
-
const read = await readErrorBody(r, signal);
|
|
909
|
+
const read = errorBody ?? (await readErrorBody(r, signal));
|
|
591
910
|
errorBody = read;
|
|
592
911
|
const report = read.endedNormally ? parseContextOverflow(r.status, read.text) : undefined;
|
|
593
912
|
if (report !== undefined) {
|
|
594
|
-
const plan = planOutputCapAdjustment(report, [
|
|
913
|
+
const plan = planOutputCapAdjustment(report, [overrides.maxOutputTokens, req.outputCapTokens]);
|
|
595
914
|
if (plan.outcome === "adjust" && attempt < maxRetries) {
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
rebuilt = buildRequest({ maxOutputTokens: plan.maxOutputTokens });
|
|
599
|
-
}
|
|
600
|
-
catch {
|
|
601
|
-
rebuilt = undefined;
|
|
602
|
-
}
|
|
603
|
-
let acceptable = false;
|
|
604
|
-
try {
|
|
605
|
-
acceptable = rebuilt !== undefined && sameRequestModuloCap(req, rebuilt, plan.maxOutputTokens);
|
|
606
|
-
}
|
|
607
|
-
catch {
|
|
608
|
-
acceptable = false;
|
|
609
|
-
}
|
|
610
|
-
if (acceptable && rebuilt !== undefined) {
|
|
915
|
+
const declined = reshapeCall({ maxOutputTokens: plan.maxOutputTokens }, (original, rebuilt) => sameRequestModuloCap(original, rebuilt, plan.maxOutputTokens));
|
|
916
|
+
if (declined === undefined) {
|
|
611
917
|
const previousCap = plan.current;
|
|
612
|
-
maxOutputTokensOverride = plan.maxOutputTokens;
|
|
613
|
-
req = rebuilt;
|
|
614
|
-
commit(req);
|
|
615
918
|
emitBrainTelemetry({ kind: "retry", attempt: attempt + 1, phase: "connect", errClass: "output_cap", nextDelayMs: 0 });
|
|
616
919
|
announcedRetry = true;
|
|
617
920
|
emitBrainStatus({
|
|
@@ -626,7 +929,7 @@ export function runStreamingBrain(args) {
|
|
|
626
929
|
continue;
|
|
627
930
|
}
|
|
628
931
|
overflowDeclineNote =
|
|
629
|
-
|
|
932
|
+
declined.declined === "unbuilt"
|
|
630
933
|
? ` — output-cap recovery declined: the request could not be rebuilt with a smaller cap`
|
|
631
934
|
: ` — output-cap recovery declined: rebuilding the request changed more than the output cap (its destination, headers, or some other body field moved), so this would no longer be the same request with a smaller cap`;
|
|
632
935
|
}
|
|
@@ -640,13 +943,26 @@ export function runStreamingBrain(args) {
|
|
|
640
943
|
}
|
|
641
944
|
}
|
|
642
945
|
}
|
|
946
|
+
let sharedWait;
|
|
947
|
+
const sharedWaitMs = () => (sharedWait ??= retryBackoffMs(baseDelay, attempt, r));
|
|
948
|
+
if (r !== undefined && speedRuled === undefined && fastRuled(r)) {
|
|
949
|
+
if (errorBody === undefined)
|
|
950
|
+
errorBody = await readErrorBody(r, signal);
|
|
951
|
+
speedRuled = fastModeRule(r, errorBody, sharedWaitMs);
|
|
952
|
+
if (speedRuled === "standard") {
|
|
953
|
+
discardResponseBody(r);
|
|
954
|
+
cc.abort();
|
|
955
|
+
cc.dispose();
|
|
956
|
+
continue;
|
|
957
|
+
}
|
|
958
|
+
}
|
|
643
959
|
if (r !== undefined && errorBody === undefined && namesTheFailure(r.status) && !isTransientStatus(r.status))
|
|
644
960
|
errorBody = await readErrorBody(r, signal);
|
|
645
961
|
const judge = () => r !== undefined && namesTheFailure(r.status) ? judgeHttpFailure({ dialect, status: r.status, headers: r.headers, body: errorBody }) : undefined;
|
|
646
962
|
const verdict = judge();
|
|
647
963
|
const retryMessage = verdict !== undefined ? readProviderMessage(errorBody, req.declared) : undefined;
|
|
648
964
|
const retryableByCondition = netErr !== undefined || (verdict !== undefined && PROVIDER_CONDITION_TRAITS[verdict.condition].retryable);
|
|
649
|
-
const retryable = shouldRetryHeaderVerdict(r) ?? retryableByCondition;
|
|
965
|
+
const retryable = speedRuled === "wait" || (shouldRetryHeaderVerdict(r) ?? retryableByCondition);
|
|
650
966
|
const connectReading = netErr !== undefined ? readConnectFailure(netErr) : undefined;
|
|
651
967
|
const deterministicConnect = connectReading?.verdict === "deterministic";
|
|
652
968
|
const fastLane = deterministicConnect && deterministicLaneActive;
|
|
@@ -660,10 +976,8 @@ export function runStreamingBrain(args) {
|
|
|
660
976
|
: r !== undefined && (r.status >= 500 || r.status === 408 || r.status === 409)
|
|
661
977
|
? "server"
|
|
662
978
|
: "http";
|
|
663
|
-
if (retryable && attempt < laneMaxRetries) {
|
|
664
|
-
const delayMs = fastLane
|
|
665
|
-
? retryBackoffMs(baseDelay, attempt, r, Math.random, FAST_MAX_BACKOFF_MS)
|
|
666
|
-
: retryBackoffMs(baseDelay, attempt, r);
|
|
979
|
+
if (retryable && attempt < laneMaxRetries && !fastReplayForbidden) {
|
|
980
|
+
const delayMs = fastLane ? retryBackoffMs(baseDelay, attempt, r, Math.random, FAST_MAX_BACKOFF_MS) : sharedWaitMs();
|
|
667
981
|
const waitHint = providerWaitHint(r);
|
|
668
982
|
const truncationNote = waitHint?.rawMs !== undefined
|
|
669
983
|
? ` (the requested wait was longer than the cap; honoring ${Math.ceil(waitHint.ms / 1000)}s instead of ${Math.ceil(waitHint.rawMs / 1000)}s)`
|
|
@@ -710,6 +1024,7 @@ export function runStreamingBrain(args) {
|
|
|
710
1024
|
const providerCode = refusedTheRequest(status) ? readProviderErrorCode(errorBody) : undefined;
|
|
711
1025
|
const terminalMessage = terminalVerdict !== undefined ? readProviderMessage(errorBody, req.declared) : undefined;
|
|
712
1026
|
const refusedTool = terminalVerdict?.toolSchemaAt !== undefined ? req.toolNames?.[terminalVerdict.toolSchemaAt] : undefined;
|
|
1027
|
+
declaredRefused = refusesDeclaredThinking(statedEnvelope(errorBody));
|
|
713
1028
|
throw new BrainError(classifyHttp(status), `${httpLabel} HTTP ${status || "ERR"}: ${detail.slice(0, 500)}${overflowDeclineNote}`, status, {
|
|
714
1029
|
apiError: true,
|
|
715
1030
|
...(itl === "yes" ? { inputTooLong: true } : itl === "no" ? { inputTooLongRuledOut: true } : {}),
|
|
@@ -738,7 +1053,9 @@ export function runStreamingBrain(args) {
|
|
|
738
1053
|
let firstTokenSeen = false;
|
|
739
1054
|
let firedFirstTokenDeadlineMs;
|
|
740
1055
|
let ftTimer;
|
|
741
|
-
let firstTokenWait =
|
|
1056
|
+
let firstTokenWait = NO_WAIT_DISCLOSURE;
|
|
1057
|
+
let nextTokenWait = NO_WAIT_DISCLOSURE;
|
|
1058
|
+
const cutOutcome = () => WAIT_OUTCOME_OF_CUT[streamCutOf(parser.snapshot())];
|
|
742
1059
|
const streamSuspend = connect.suspend;
|
|
743
1060
|
let suspendedInStream;
|
|
744
1061
|
const cutOnSuspend = (wait) => (sleptMs) => {
|
|
@@ -746,7 +1063,7 @@ export function runStreamingBrain(args) {
|
|
|
746
1063
|
void reader.cancel().catch(() => undefined);
|
|
747
1064
|
};
|
|
748
1065
|
if (firstTokenTimeoutMs && firstTokenTimeoutMs > 0) {
|
|
749
|
-
firstTokenWait = armWaitDisclosure(firstTokenTimeoutMs, "first_token");
|
|
1066
|
+
firstTokenWait = armWaitDisclosure(firstTokenTimeoutMs, "first_token", cutOutcome);
|
|
750
1067
|
streamSuspend.arm(firstTokenTimeoutMs, cutOnSuspend("first-token"));
|
|
751
1068
|
ftTimer = setTimeout(() => {
|
|
752
1069
|
if (streamSuspend.check())
|
|
@@ -766,16 +1083,31 @@ export function runStreamingBrain(args) {
|
|
|
766
1083
|
suspendedInStream = undefined;
|
|
767
1084
|
if (idleTimer)
|
|
768
1085
|
clearTimeout(idleTimer);
|
|
1086
|
+
if (nextTokenWait === NO_WAIT_DISCLOSURE)
|
|
1087
|
+
nextTokenWait = armWaitDisclosure(idleTimeoutMs, "next_token", cutOutcome);
|
|
1088
|
+
else
|
|
1089
|
+
nextTokenWait.restart();
|
|
769
1090
|
streamSuspend.arm(idleTimeoutMs, cutOnSuspend("stream idle"));
|
|
770
1091
|
idleTimer = setTimeout(() => {
|
|
771
1092
|
if (streamSuspend.check())
|
|
772
1093
|
return;
|
|
773
1094
|
streamSuspend.disarm();
|
|
774
1095
|
firedIdleDeadlineMs = idleTimeoutMs;
|
|
1096
|
+
nextTokenWait.expire();
|
|
775
1097
|
void reader.cancel().catch(() => undefined);
|
|
776
1098
|
}, idleTimeoutMs);
|
|
777
1099
|
};
|
|
778
|
-
|
|
1100
|
+
let successSettled = false;
|
|
1101
|
+
const keep = () => {
|
|
1102
|
+
taken();
|
|
1103
|
+
if (successSettled)
|
|
1104
|
+
return;
|
|
1105
|
+
successSettled = true;
|
|
1106
|
+
succeeded();
|
|
1107
|
+
};
|
|
1108
|
+
let inBandEnvelope;
|
|
1109
|
+
let producedBeforeFrame = false;
|
|
1110
|
+
const sawProgress = () => {
|
|
779
1111
|
if (!firstTokenSeen) {
|
|
780
1112
|
firstTokenSeen = true;
|
|
781
1113
|
firstTokenArrived = true;
|
|
@@ -790,12 +1122,18 @@ export function runStreamingBrain(args) {
|
|
|
790
1122
|
}
|
|
791
1123
|
bumpIdle();
|
|
792
1124
|
};
|
|
1125
|
+
const sawContentToken = sawProgress;
|
|
1126
|
+
const keptOutput = () => {
|
|
1127
|
+
const s = parser.snapshot();
|
|
1128
|
+
return s.hasSubstantiveText || s.hasCompletedToolCall;
|
|
1129
|
+
};
|
|
793
1130
|
cleanup = () => {
|
|
794
1131
|
if (ftTimer) {
|
|
795
1132
|
clearTimeout(ftTimer);
|
|
796
1133
|
ftTimer = undefined;
|
|
797
1134
|
}
|
|
798
1135
|
firstTokenWait.stop();
|
|
1136
|
+
nextTokenWait.stop();
|
|
799
1137
|
if (idleTimer) {
|
|
800
1138
|
clearTimeout(idleTimer);
|
|
801
1139
|
idleTimer = undefined;
|
|
@@ -811,19 +1149,68 @@ export function runStreamingBrain(args) {
|
|
|
811
1149
|
msg.partialFinalized = true;
|
|
812
1150
|
msg.errorMessage = msg.errorMessage ? `${msg.errorMessage}\n${midstreamWarning}` : midstreamWarning;
|
|
813
1151
|
}
|
|
1152
|
+
if (ev.type === "toolcall_end" || (ev.type === "text_delta" && !successSettled && keptOutput()))
|
|
1153
|
+
keep();
|
|
1154
|
+
if (ev.type === "done" || ev.type === "error") {
|
|
1155
|
+
if (keptOutput())
|
|
1156
|
+
keep();
|
|
1157
|
+
else if (untried !== undefined)
|
|
1158
|
+
throw new TriedAttemptEnded(ev.type === "error" ? ev.error : ev.message);
|
|
1159
|
+
}
|
|
1160
|
+
if (ev.type === "error") {
|
|
1161
|
+
const notes = failureNotes();
|
|
1162
|
+
if (notes !== "")
|
|
1163
|
+
ev.error.errorMessage = `${ev.error.errorMessage ?? ""}${notes}`;
|
|
1164
|
+
}
|
|
814
1165
|
out.push(ev);
|
|
815
1166
|
};
|
|
816
1167
|
const parser = makeParser({
|
|
817
1168
|
out: parserOut,
|
|
818
1169
|
partial,
|
|
819
1170
|
sawContentToken,
|
|
1171
|
+
sawProgress,
|
|
820
1172
|
cancel: () => void reader.cancel().catch(() => undefined),
|
|
821
1173
|
dialect,
|
|
822
1174
|
keepOutput: (output) => {
|
|
823
1175
|
attemptOutput = output;
|
|
824
1176
|
},
|
|
825
1177
|
providerWords: (words) => remoteWords(words, req.declared),
|
|
1178
|
+
inBandFrame: (envelope) => {
|
|
1179
|
+
if (inBandEnvelope !== undefined)
|
|
1180
|
+
return;
|
|
1181
|
+
inBandEnvelope = envelope;
|
|
1182
|
+
producedBeforeFrame = firstTokenSeen;
|
|
1183
|
+
},
|
|
826
1184
|
});
|
|
1185
|
+
const fellBackFromStream = (failure) => {
|
|
1186
|
+
if (keptOutput()) {
|
|
1187
|
+
keep();
|
|
1188
|
+
return false;
|
|
1189
|
+
}
|
|
1190
|
+
if (!fallBack(failure))
|
|
1191
|
+
return false;
|
|
1192
|
+
parser.sealForRetry();
|
|
1193
|
+
return true;
|
|
1194
|
+
};
|
|
1195
|
+
const parserFailed = (e) => {
|
|
1196
|
+
const ended = e instanceof TriedAttemptEnded ? e.ended : undefined;
|
|
1197
|
+
const envelope = ended === undefined ? inBandEnvelope : undefined;
|
|
1198
|
+
const kept = attemptOutput;
|
|
1199
|
+
const failure = {
|
|
1200
|
+
code: ended !== undefined ? extractErrorCode(ended.errorMessage) : brainErrorCodeOf(e),
|
|
1201
|
+
status: undefined,
|
|
1202
|
+
statement: envelope !== undefined && !producedBeforeFrame ? { envelope, inputTooLong: readMember(e, "inputTooLong") === true } : undefined,
|
|
1203
|
+
spent: ended !== undefined
|
|
1204
|
+
? { usage: ended.usage, ...(ended.usageMissing === true ? { usageMissing: true } : {}) }
|
|
1205
|
+
: kept !== undefined
|
|
1206
|
+
? { usage: kept.usage, ...(kept.usageMissing === true ? { usageMissing: true } : {}) }
|
|
1207
|
+
: undefined,
|
|
1208
|
+
};
|
|
1209
|
+
if (fellBackFromStream(failure))
|
|
1210
|
+
return true;
|
|
1211
|
+
declaredRefused = refusesDeclaredThinking(envelope);
|
|
1212
|
+
return false;
|
|
1213
|
+
};
|
|
827
1214
|
const INBAND_OVERLOAD_WARNING = "API overloaded mid-response. The response above may be incomplete.";
|
|
828
1215
|
const onLineOverloadTierA = (line) => {
|
|
829
1216
|
try {
|
|
@@ -837,6 +1224,7 @@ export function runStreamingBrain(args) {
|
|
|
837
1224
|
}
|
|
838
1225
|
};
|
|
839
1226
|
let failure;
|
|
1227
|
+
let fellBack = false;
|
|
840
1228
|
readLoop: for (;;) {
|
|
841
1229
|
let readRes;
|
|
842
1230
|
try {
|
|
@@ -857,15 +1245,23 @@ export function runStreamingBrain(args) {
|
|
|
857
1245
|
buffer += decoder.decode(value, { stream: true });
|
|
858
1246
|
let nl;
|
|
859
1247
|
let start = 0;
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
1248
|
+
try {
|
|
1249
|
+
while ((nl = buffer.indexOf("\n", start)) !== -1) {
|
|
1250
|
+
const line = buffer.slice(start, nl);
|
|
1251
|
+
start = nl + 1;
|
|
1252
|
+
if (onLineOverloadTierA(line)) {
|
|
1253
|
+
midstreamWarning = INBAND_OVERLOAD_WARNING;
|
|
1254
|
+
parser.finalize({ truncated: true });
|
|
1255
|
+
return;
|
|
1256
|
+
}
|
|
867
1257
|
}
|
|
868
1258
|
}
|
|
1259
|
+
catch (e) {
|
|
1260
|
+
if (!parserFailed(e))
|
|
1261
|
+
throw e;
|
|
1262
|
+
fellBack = true;
|
|
1263
|
+
break readLoop;
|
|
1264
|
+
}
|
|
869
1265
|
if (start > 0)
|
|
870
1266
|
buffer = buffer.slice(start);
|
|
871
1267
|
}
|
|
@@ -874,6 +1270,7 @@ export function runStreamingBrain(args) {
|
|
|
874
1270
|
ftTimer = undefined;
|
|
875
1271
|
}
|
|
876
1272
|
firstTokenWait.stop();
|
|
1273
|
+
nextTokenWait.stop();
|
|
877
1274
|
if (idleTimer) {
|
|
878
1275
|
clearTimeout(idleTimer);
|
|
879
1276
|
idleTimer = undefined;
|
|
@@ -899,19 +1296,43 @@ export function runStreamingBrain(args) {
|
|
|
899
1296
|
err: new BrainError("network", `stream idle timeout after ${firedIdleDeadlineMs}ms`, undefined, { apiError: true }),
|
|
900
1297
|
};
|
|
901
1298
|
}
|
|
1299
|
+
if (fellBack || (failure !== undefined && !parser.snapshot().terminalFrameSeen && fellBackFromStream({ code: brainErrorCodeOf(failure.err), status: undefined, statement: undefined, spent: undefined }))) {
|
|
1300
|
+
cleanup?.();
|
|
1301
|
+
cleanup = undefined;
|
|
1302
|
+
continue;
|
|
1303
|
+
}
|
|
902
1304
|
if (!failure) {
|
|
903
1305
|
buffer += decoder.decode();
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
1306
|
+
try {
|
|
1307
|
+
if (buffer.trim() && onLineOverloadTierA(buffer))
|
|
1308
|
+
midstreamWarning = INBAND_OVERLOAD_WARNING;
|
|
1309
|
+
parser.finalize(midstreamWarning !== undefined ? { truncated: true } : undefined);
|
|
1310
|
+
}
|
|
1311
|
+
catch (e) {
|
|
1312
|
+
if (!parserFailed(e))
|
|
1313
|
+
throw e;
|
|
1314
|
+
cleanup?.();
|
|
1315
|
+
cleanup = undefined;
|
|
1316
|
+
continue;
|
|
1317
|
+
}
|
|
907
1318
|
return;
|
|
908
1319
|
}
|
|
909
1320
|
const snap = parser.snapshot();
|
|
910
|
-
|
|
911
|
-
|
|
1321
|
+
const arm = streamCutOf(snap);
|
|
1322
|
+
if (arm === "whole") {
|
|
1323
|
+
try {
|
|
1324
|
+
parser.finalize();
|
|
1325
|
+
}
|
|
1326
|
+
catch (e) {
|
|
1327
|
+
if (!parserFailed(e))
|
|
1328
|
+
throw e;
|
|
1329
|
+
cleanup?.();
|
|
1330
|
+
cleanup = undefined;
|
|
1331
|
+
continue;
|
|
1332
|
+
}
|
|
912
1333
|
return;
|
|
913
1334
|
}
|
|
914
|
-
if (
|
|
1335
|
+
if (arm === "keep") {
|
|
915
1336
|
midstreamWarning =
|
|
916
1337
|
failure.kind === "stalled"
|
|
917
1338
|
? "Response stalled mid-stream. The response above may be incomplete."
|
|
@@ -924,17 +1345,15 @@ export function runStreamingBrain(args) {
|
|
|
924
1345
|
cleanup?.();
|
|
925
1346
|
cleanup = undefined;
|
|
926
1347
|
const announcement = retryAnnouncement(failure);
|
|
927
|
-
if (
|
|
928
|
-
|
|
929
|
-
|
|
1348
|
+
if (arm === "fail")
|
|
1349
|
+
throw failure.err;
|
|
1350
|
+
if (arm === "resend_thinking") {
|
|
930
1351
|
thinkingRetries++;
|
|
931
1352
|
parser.sealForRetry();
|
|
932
1353
|
const delayMs = 100 * thinkingRetries;
|
|
933
1354
|
await waitAnnouncingRetry(announcement, { phase: "midstream", attempt: thinkingRetries, maxRetries: THINKING_RETRY_BUDGET, delayMs });
|
|
934
1355
|
continue;
|
|
935
1356
|
}
|
|
936
|
-
if (attempt >= maxRetries)
|
|
937
|
-
throw failure.err;
|
|
938
1357
|
const delayMs = retryBackoffMs(baseDelay, attempt);
|
|
939
1358
|
attempt++;
|
|
940
1359
|
await waitAnnouncingRetry(announcement, { phase: "midstream", attempt, maxRetries, delayMs });
|