@botbotgo/agent-harness 0.0.474 → 0.0.476
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/README.md +3 -1234
- package/README.zh.md +3 -1191
- package/dist/acp.js +1 -1
- package/dist/api.js +1 -404
- package/dist/benchmark/checkpoint-resume-cost-benchmark.js +1 -55
- package/dist/benchmark/deepagent-local-model-benchmark.js +2 -35
- package/dist/benchmark/upstream-runtime-ab-benchmark.js +1 -179
- package/dist/cli/chat-interactive.js +25 -244
- package/dist/cli/chat-rendering.js +6 -100
- package/dist/cli/chat-stream.js +23 -512
- package/dist/cli/chat-ui.js +21 -199
- package/dist/cli/chat-workspace.js +2 -210
- package/dist/cli/main.js +21 -428
- package/dist/cli/managed-service-commands.js +9 -63
- package/dist/cli/managed-service.js +2 -137
- package/dist/cli/options-init-chat.js +1 -108
- package/dist/cli/options-runtime.js +1 -158
- package/dist/cli/options-serve.js +1 -282
- package/dist/cli/options.js +2 -19
- package/dist/cli/process-guards.js +1 -139
- package/dist/cli/request-tree.js +7 -296
- package/dist/cli/runtime-commands.js +12 -258
- package/dist/cli/runtime-output.js +16 -155
- package/dist/cli/server-commands.js +16 -270
- package/dist/cli/workspace.js +1 -67
- package/dist/cli.js +1 -7
- package/dist/client/acp.js +1 -1
- package/dist/client/in-process.js +1 -67
- package/dist/client/index.js +1 -2
- package/dist/client/types.js +0 -1
- package/dist/client.js +1 -1
- package/dist/contracts/core.js +1 -1
- package/dist/contracts/runtime-evaluation.js +0 -1
- package/dist/contracts/runtime-memory.js +0 -1
- package/dist/contracts/runtime-observability.js +0 -1
- package/dist/contracts/runtime-requests.js +0 -1
- package/dist/contracts/runtime-scheduling.js +0 -1
- package/dist/contracts/runtime.js +1 -27
- package/dist/contracts/types.js +1 -3
- package/dist/contracts/workspace.js +0 -1
- package/dist/flow/build-flow-graph.js +1 -50
- package/dist/flow/export-mermaid.js +2 -464
- package/dist/flow/export-sequence-mermaid.js +2 -325
- package/dist/flow/flow-graph-normalization.js +1 -214
- package/dist/flow/flow-graph-runtime.js +1 -107
- package/dist/flow/flow-graph-upstream.js +1 -494
- package/dist/flow/index.js +1 -3
- package/dist/flow/types.js +0 -1
- package/dist/index.js +1 -5
- package/dist/init-project.js +1 -1
- package/dist/knowledge/config.js +1 -32
- package/dist/knowledge/contracts.js +0 -1
- package/dist/knowledge/index.js +1 -2
- package/dist/knowledge/module.js +12 -909
- package/dist/knowledge/procedural/config.js +1 -125
- package/dist/knowledge/procedural/index.js +1 -2
- package/dist/knowledge/procedural/manager.js +9 -345
- package/dist/mcp.js +1 -2
- package/dist/package-version.d.ts +2 -2
- package/dist/package-version.js +1 -2
- package/dist/persistence/file-store.js +3 -758
- package/dist/persistence/sqlite-request-context-store.js +5 -54
- package/dist/persistence/sqlite-request-queue-store.js +10 -108
- package/dist/persistence/sqlite-runtime.js +1 -86
- package/dist/persistence/sqlite-store.js +62 -810
- package/dist/persistence/types.js +0 -1
- package/dist/projections/presentation.js +37 -206
- package/dist/projections/request-events.js +2 -502
- package/dist/projections/upstream-events.js +1 -201
- package/dist/protocol/a2a/http-discovery.js +1 -178
- package/dist/protocol/a2a/http-rpc.js +6 -622
- package/dist/protocol/a2a/http.js +1 -138
- package/dist/protocol/a2a/task-state.js +3 -317
- package/dist/protocol/acp/client.js +8 -294
- package/dist/protocol/acp/harness-client.js +1 -218
- package/dist/protocol/acp/http.js +5 -130
- package/dist/protocol/acp/server.js +1 -310
- package/dist/protocol/acp/stdio.js +2 -69
- package/dist/protocol/ag-ui/http.js +3 -378
- package/dist/protocol/mcp/server.js +1 -428
- package/dist/resource/backend/workspace-scoped-backend.js +1 -319
- package/dist/resource/isolation.js +1 -237
- package/dist/resource/mcp/tool-support.js +3 -296
- package/dist/resource/mcp-tool-support.js +1 -2
- package/dist/resource/providers/resource-provider.js +1 -215
- package/dist/resource/resource-impl.js +1 -3
- package/dist/resource/resource-types.js +0 -1
- package/dist/resource/resource.js +1 -1
- package/dist/resource/sources.js +1 -247
- package/dist/resource/tools/function-tool-resolver.js +2 -272
- package/dist/runtime/adapter/compat/deepagent-compat.js +1 -29
- package/dist/runtime/adapter/compat/openai-compatible.js +1 -55
- package/dist/runtime/adapter/direct-builtin-utility.js +2 -90
- package/dist/runtime/adapter/flow/execution-context.js +1 -71
- package/dist/runtime/adapter/flow/invocation-flow.js +8 -411
- package/dist/runtime/adapter/flow/invoke-runtime.js +1 -20
- package/dist/runtime/adapter/flow/stream-runtime.js +11 -1395
- package/dist/runtime/adapter/invocation-result.js +2 -473
- package/dist/runtime/adapter/local-tool-invocation.js +6 -638
- package/dist/runtime/adapter/middleware/context-hygiene.js +1 -83
- package/dist/runtime/adapter/middleware-assembly.js +5 -477
- package/dist/runtime/adapter/model/invocation-request.js +3 -183
- package/dist/runtime/adapter/model/message-assembly.js +1 -28
- package/dist/runtime/adapter/model/model-providers.js +23 -1089
- package/dist/runtime/adapter/model/prompted-json-tool-call-capture.js +1 -40
- package/dist/runtime/adapter/model/prompted-json-tool-policy.js +1 -22
- package/dist/runtime/adapter/resilience.js +1 -104
- package/dist/runtime/adapter/runtime-adapter-support.js +3 -141
- package/dist/runtime/adapter/runtime-shell.js +5 -166
- package/dist/runtime/adapter/stream-event-projection.js +2 -622
- package/dist/runtime/adapter/stream-text-consumption.js +1 -18
- package/dist/runtime/adapter/terminal-status.js +2 -67
- package/dist/runtime/adapter/tool/builtin-middleware-tools.js +6 -627
- package/dist/runtime/adapter/tool/declared-middleware.js +1 -154
- package/dist/runtime/adapter/tool/interrupt-policy.js +1 -34
- package/dist/runtime/adapter/tool/provider-tool.js +1 -25
- package/dist/runtime/adapter/tool/resolved-tool.js +1 -225
- package/dist/runtime/adapter/tool/tool-arguments.js +3 -486
- package/dist/runtime/adapter/tool/tool-hitl.js +1 -346
- package/dist/runtime/adapter/tool/tool-name-mapping.js +1 -128
- package/dist/runtime/adapter/tool/tool-output-artifacts.js +2 -88
- package/dist/runtime/adapter/tool/tool-replay.js +1 -37
- package/dist/runtime/adapter/tool-resolution.js +1 -86
- package/dist/runtime/adapter/upstream-configurable-keys.js +1 -2
- package/dist/runtime/agent-runtime-adapter.js +60 -2338
- package/dist/runtime/agent-runtime-assembly.js +7 -249
- package/dist/runtime/env/runtime-env.js +1 -62
- package/dist/runtime/harness/background-runtime.js +1 -8
- package/dist/runtime/harness/bindings.js +1 -58
- package/dist/runtime/harness/events/event-bus.js +1 -16
- package/dist/runtime/harness/events/event-sink.js +1 -61
- package/dist/runtime/harness/events/events.js +1 -80
- package/dist/runtime/harness/events/listener-runtime.js +1 -13
- package/dist/runtime/harness/events/runtime-event-operations.js +1 -9
- package/dist/runtime/harness/events/streaming.js +1 -100
- package/dist/runtime/harness/events/timeline.js +1 -52
- package/dist/runtime/harness/public-shapes.js +1 -186
- package/dist/runtime/harness/run/artifact-paths.js +1 -15
- package/dist/runtime/harness/run/governance.js +1 -295
- package/dist/runtime/harness/run/helpers.js +1 -71
- package/dist/runtime/harness/run/inspection.js +1 -409
- package/dist/runtime/harness/run/operator-overview.js +1 -80
- package/dist/runtime/harness/run/queue-diagnostics.js +1 -15
- package/dist/runtime/harness/run/recovery.js +1 -162
- package/dist/runtime/harness/run/resources.js +1 -60
- package/dist/runtime/harness/run/resume.js +1 -56
- package/dist/runtime/harness/run/routing.js +1 -48
- package/dist/runtime/harness/run/run-lifecycle.js +1 -66
- package/dist/runtime/harness/run/run-operations.js +1 -217
- package/dist/runtime/harness/run/run-queue.js +1 -43
- package/dist/runtime/harness/run/run-slot-acquisition.js +1 -157
- package/dist/runtime/harness/run/session-records.js +1 -97
- package/dist/runtime/harness/run/start-run.js +1 -120
- package/dist/runtime/harness/run/startup-runtime.js +1 -69
- package/dist/runtime/harness/run/stream-run.js +8 -1418
- package/dist/runtime/harness/run/surface-semantics.js +1 -79
- package/dist/runtime/harness/runtime-defaults.js +1 -39
- package/dist/runtime/harness/system/boundary-analysis.js +1 -234
- package/dist/runtime/harness/system/health-monitor.js +1 -258
- package/dist/runtime/harness/system/inventory.js +1 -129
- package/dist/runtime/harness/system/mem0-ingestion-sync.js +5 -345
- package/dist/runtime/harness/system/policy-engine.js +1 -175
- package/dist/runtime/harness/system/runtime-memory-candidates.js +4 -110
- package/dist/runtime/harness/system/runtime-memory-consolidation.js +1 -51
- package/dist/runtime/harness/system/runtime-memory-manager.js +10 -693
- package/dist/runtime/harness/system/runtime-memory-policy.js +1 -155
- package/dist/runtime/harness/system/runtime-memory-records.js +11 -577
- package/dist/runtime/harness/system/runtime-memory-sync.js +5 -206
- package/dist/runtime/harness/system/session-memory-sync.js +3 -113
- package/dist/runtime/harness/system/skill-requirements.js +1 -112
- package/dist/runtime/harness/system/store.js +9 -365
- package/dist/runtime/harness/tool-gateway/index.js +1 -2
- package/dist/runtime/harness/tool-gateway/policy.js +1 -45
- package/dist/runtime/harness/tool-gateway/validation.js +1 -176
- package/dist/runtime/harness/tool-schema.js +1 -3
- package/dist/runtime/harness.js +3 -1490
- package/dist/runtime/index.js +1 -3
- package/dist/runtime/layout/runtime-layout.js +1 -31
- package/dist/runtime/maintenance/checkpoint-maintenance.js +2 -178
- package/dist/runtime/maintenance/file-checkpoint-saver.js +1 -106
- package/dist/runtime/maintenance/runtime-record-maintenance.js +2 -169
- package/dist/runtime/maintenance/sqlite-checkpoint-saver.js +4 -289
- package/dist/runtime/parsing/output-content.js +10 -550
- package/dist/runtime/parsing/output-parsing.js +1 -4
- package/dist/runtime/parsing/output-recovery.js +3 -213
- package/dist/runtime/parsing/output-tool-args.js +7 -663
- package/dist/runtime/parsing/stream-event-parsing.js +3 -362
- package/dist/runtime/prompts/runtime-prompts.js +4 -73
- package/dist/runtime/scheduling/system-schedule-manager.js +11 -532
- package/dist/runtime/skills/skill-metadata.js +1 -197
- package/dist/runtime/startup-tracing.js +2 -37
- package/dist/runtime/support/compiled-binding.js +1 -290
- package/dist/runtime/support/embedding-models.js +1 -118
- package/dist/runtime/support/harness-support.js +5 -137
- package/dist/runtime/support/llamaindex.js +1 -108
- package/dist/runtime/support/runtime-adapter-options.js +1 -29
- package/dist/runtime/support/runtime-factories.js +1 -51
- package/dist/runtime/support/vector-stores.js +9 -270
- package/dist/scaffold/init-project.js +54 -233
- package/dist/tooling/extensions.js +1 -311
- package/dist/tooling/module-loader.js +1 -55
- package/dist/tools.js +1 -176
- package/dist/utils/agent-display.js +1 -18
- package/dist/utils/bundled-text.js +4 -39
- package/dist/utils/compiled-binding.js +1 -33
- package/dist/utils/fs.js +2 -45
- package/dist/utils/id.js +1 -9
- package/dist/utils/message-content.js +1 -30
- package/dist/utils/object.js +1 -6
- package/dist/workspace/agent-binding-compiler.js +3 -613
- package/dist/workspace/compile.js +1 -472
- package/dist/workspace/framework-contract-validation.js +2 -322
- package/dist/workspace/index.js +1 -1
- package/dist/workspace/object-loader-paths.js +1 -71
- package/dist/workspace/object-loader-readers.js +1 -187
- package/dist/workspace/object-loader.js +1 -754
- package/dist/workspace/resource-compilers.js +1 -374
- package/dist/workspace/support/agent-capabilities.js +1 -37
- package/dist/workspace/support/agent-execution-config.js +1 -44
- package/dist/workspace/support/discovery.js +1 -147
- package/dist/workspace/support/source-collectors.js +1 -30
- package/dist/workspace/support/source-protocols.js +2 -192
- package/dist/workspace/support/workspace-ref-utils.js +1 -362
- package/dist/workspace/tool-hydration.js +1 -280
- package/dist/workspace/validate.js +1 -99
- package/dist/workspace/yaml-object-reader.js +1 -285
- package/package.json +7 -3
|
@@ -1,1395 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export class ExecutionReconciliationError extends Error {
|
|
12
|
-
constructor(message) {
|
|
13
|
-
super(message);
|
|
14
|
-
this.name = "ExecutionReconciliationError";
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
const CLOSE_REQUIRED_PLAN_RECOVERY_INSTRUCTION = [
|
|
18
|
-
"The current required todo board still has unfinished work.",
|
|
19
|
-
"Do not broaden the task, restart planning, or ask the user what to do next.",
|
|
20
|
-
"Use the existing tool evidence already available in this run.",
|
|
21
|
-
"Your next action must be write_todos: update every remaining pending or in_progress item to completed if evidence was gathered, or failed if it cannot be completed with the available tools.",
|
|
22
|
-
"After that write_todos call, provide the final answer required by the agent response format.",
|
|
23
|
-
].join("\n");
|
|
24
|
-
const RUN_EVIDENCE_AFTER_PREMATURE_PLAN_CLOSE_INSTRUCTION = [
|
|
25
|
-
"The required todo board was closed before any non-TODO evidence tool returned.",
|
|
26
|
-
"Do not call write_todos again yet.",
|
|
27
|
-
"Your next action must be exactly one non-TODO evidence tool call selected from the available tool descriptions and schemas.",
|
|
28
|
-
"Honor any structured workspace or runtime evidence-tool contract already supplied in this turn.",
|
|
29
|
-
"Do not choose tools by matching free-form request or TODO text.",
|
|
30
|
-
"After that evidence tool returns, update the todo board and then provide the final answer required by the agent response format.",
|
|
31
|
-
].join("\n");
|
|
32
|
-
function readPrimaryToolName(tool) {
|
|
33
|
-
return typeof tool.name === "string" ? tool.name.trim() : "";
|
|
34
|
-
}
|
|
35
|
-
function buildRunEvidenceAfterPlanInstruction(primaryTools) {
|
|
36
|
-
const toolNames = primaryTools
|
|
37
|
-
.map(readPrimaryToolName)
|
|
38
|
-
.filter((name) => name.length > 0 && !isPlanToolName(name));
|
|
39
|
-
if (toolNames.length === 0) {
|
|
40
|
-
return RUN_EVIDENCE_AFTER_PREMATURE_PLAN_CLOSE_INSTRUCTION;
|
|
41
|
-
}
|
|
42
|
-
return [
|
|
43
|
-
RUN_EVIDENCE_AFTER_PREMATURE_PLAN_CLOSE_INSTRUCTION,
|
|
44
|
-
"",
|
|
45
|
-
`Available non-planning tool names: ${toolNames.join(", ")}.`,
|
|
46
|
-
].join("\n");
|
|
47
|
-
}
|
|
48
|
-
const INITIAL_REQUIRED_PLAN_INSTRUCTION = [
|
|
49
|
-
"This agent has a required visible planning contract.",
|
|
50
|
-
"Your first action for this request must be write_todos with concrete task steps and statuses.",
|
|
51
|
-
"Do not call any domain/evidence tool and do not provide a final answer before the initial write_todos call succeeds.",
|
|
52
|
-
"After write_todos succeeds, do not call write_todos or read_todos again until one non-planning evidence tool returns.",
|
|
53
|
-
"After each evidence step, update the todo board. Before the final answer, close every todo as completed or failed.",
|
|
54
|
-
].join("\n");
|
|
55
|
-
function buildInitialRequiredPlanInstruction(primaryTools) {
|
|
56
|
-
const toolNames = primaryTools
|
|
57
|
-
.map(readPrimaryToolName)
|
|
58
|
-
.filter((name) => name.length > 0 && !isPlanToolName(name));
|
|
59
|
-
if (toolNames.length === 0) {
|
|
60
|
-
return INITIAL_REQUIRED_PLAN_INSTRUCTION;
|
|
61
|
-
}
|
|
62
|
-
return [
|
|
63
|
-
INITIAL_REQUIRED_PLAN_INSTRUCTION,
|
|
64
|
-
"",
|
|
65
|
-
`After the initial todo board, select the next non-planning tool from these declared tool names: ${toolNames.join(", ")}.`,
|
|
66
|
-
].join("\n");
|
|
67
|
-
}
|
|
68
|
-
function toVisibleContent(value) {
|
|
69
|
-
const extracted = extractVisibleOutput(value);
|
|
70
|
-
return extracted ? sanitizeVisibleText(extracted) : "";
|
|
71
|
-
}
|
|
72
|
-
function readTerminalEventVisibleOutput(event) {
|
|
73
|
-
if (typeof event !== "object" || event === null) {
|
|
74
|
-
return "";
|
|
75
|
-
}
|
|
76
|
-
const typed = event;
|
|
77
|
-
const eventName = typeof typed.event === "string" ? typed.event : "";
|
|
78
|
-
if (eventName !== "on_chat_model_end" && eventName !== "on_chain_end") {
|
|
79
|
-
return "";
|
|
80
|
-
}
|
|
81
|
-
return toVisibleContent(typed.data?.output);
|
|
82
|
-
}
|
|
83
|
-
function hasIncompletePlanStateInExecutedToolResults(executedToolResults) {
|
|
84
|
-
for (const latest of [...executedToolResults].reverse()) {
|
|
85
|
-
if (typeof latest.output !== "object" || latest.output === null) {
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
const summaryContainer = latest.output.summary;
|
|
89
|
-
if (typeof summaryContainer !== "object" || summaryContainer === null) {
|
|
90
|
-
continue;
|
|
91
|
-
}
|
|
92
|
-
const summary = summaryContainer.summary;
|
|
93
|
-
if (typeof summary !== "object" || summary === null) {
|
|
94
|
-
continue;
|
|
95
|
-
}
|
|
96
|
-
const typedSummary = summary;
|
|
97
|
-
const pending = typeof typedSummary.pending === "number" ? typedSummary.pending : 0;
|
|
98
|
-
const inProgress = typeof typedSummary.inProgress === "number" ? typedSummary.inProgress : 0;
|
|
99
|
-
return pending > 0 || inProgress > 0;
|
|
100
|
-
}
|
|
101
|
-
return false;
|
|
102
|
-
}
|
|
103
|
-
function hasIncompleteTodosArray(value) {
|
|
104
|
-
if (!Array.isArray(value)) {
|
|
105
|
-
return null;
|
|
106
|
-
}
|
|
107
|
-
return value.some((todo) => typeof todo === "object"
|
|
108
|
-
&& todo !== null
|
|
109
|
-
&& typeof todo.status === "string"
|
|
110
|
-
&& ["pending", "in_progress"].includes(todo.status.trim().toLowerCase()));
|
|
111
|
-
}
|
|
112
|
-
function hasIncompletePlanOutput(value) {
|
|
113
|
-
if (typeof value !== "object" || value === null) {
|
|
114
|
-
return null;
|
|
115
|
-
}
|
|
116
|
-
const typed = value;
|
|
117
|
-
if (typeof typed.pending === "number" || typeof typed.inProgress === "number") {
|
|
118
|
-
return (typeof typed.pending === "number" ? typed.pending : 0) > 0
|
|
119
|
-
|| (typeof typed.inProgress === "number" ? typed.inProgress : 0) > 0;
|
|
120
|
-
}
|
|
121
|
-
const directTodos = hasIncompleteTodosArray(typed.todos);
|
|
122
|
-
if (directTodos !== null) {
|
|
123
|
-
return directTodos;
|
|
124
|
-
}
|
|
125
|
-
for (const nested of [typed.summary, typed.update, typed.data, typed.output]) {
|
|
126
|
-
const nestedCompleteness = hasIncompletePlanOutput(nested);
|
|
127
|
-
if (nestedCompleteness !== null) {
|
|
128
|
-
return nestedCompleteness;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
return null;
|
|
132
|
-
}
|
|
133
|
-
function hasFailedTodos(value) {
|
|
134
|
-
if (Array.isArray(value)) {
|
|
135
|
-
return value.some((todo) => hasFailedTodos(todo));
|
|
136
|
-
}
|
|
137
|
-
if (typeof value !== "object" || value === null) {
|
|
138
|
-
return false;
|
|
139
|
-
}
|
|
140
|
-
const typed = value;
|
|
141
|
-
if (typeof typed.status === "string" && typed.status.trim().toLowerCase() === "failed") {
|
|
142
|
-
return true;
|
|
143
|
-
}
|
|
144
|
-
return hasFailedTodos(typed.todos)
|
|
145
|
-
|| hasFailedTodos(typed.update)
|
|
146
|
-
|| hasFailedTodos(typed.data)
|
|
147
|
-
|| hasFailedTodos(typed.output)
|
|
148
|
-
|| hasFailedTodos(typed.summary);
|
|
149
|
-
}
|
|
150
|
-
function hasFailedPlanStateInExecutedToolResults(executedToolResults) {
|
|
151
|
-
return executedToolResults.some((item) => isPlanToolName(item.toolName) && hasFailedTodos(item.output));
|
|
152
|
-
}
|
|
153
|
-
function normalizePlanToolName(toolName) {
|
|
154
|
-
return typeof toolName === "string" ? toolName.trim().toLowerCase().replace(/[\s-]+/gu, "_") : "";
|
|
155
|
-
}
|
|
156
|
-
function isPlanToolName(toolName) {
|
|
157
|
-
const normalized = normalizePlanToolName(toolName);
|
|
158
|
-
return normalized === "write_todos"
|
|
159
|
-
|| normalized === "read_todos"
|
|
160
|
-
|| normalized === "tool_call_write_todos"
|
|
161
|
-
|| normalized === "tool_call_read_todos"
|
|
162
|
-
|| normalized === "call_write_todos"
|
|
163
|
-
|| normalized === "call_read_todos";
|
|
164
|
-
}
|
|
165
|
-
function isCompletedPlanToolResultChunk(chunk) {
|
|
166
|
-
if (chunk.kind !== "tool-result" || !isPlanToolName(chunk.toolName)) {
|
|
167
|
-
return false;
|
|
168
|
-
}
|
|
169
|
-
return hasIncompletePlanOutput(chunk.output) === false;
|
|
170
|
-
}
|
|
171
|
-
function hasNonTodoToolEvidence(executedToolResults) {
|
|
172
|
-
return executedToolResults.some((item) => !isPlanToolName(item.toolName));
|
|
173
|
-
}
|
|
174
|
-
function hasSuccessfulNonTodoToolEvidence(executedToolResults) {
|
|
175
|
-
return executedToolResults.some((item) => item.isError !== true && !isPlanToolName(item.toolName));
|
|
176
|
-
}
|
|
177
|
-
function hasSuccessfulTaskToolEvidence(executedToolResults) {
|
|
178
|
-
return executedToolResults.some((item) => item.isError !== true && item.toolName === "task");
|
|
179
|
-
}
|
|
180
|
-
function requiresPlanEvidence(binding) {
|
|
181
|
-
return binding.harnessRuntime?.executionContract?.requiresPlan === true;
|
|
182
|
-
}
|
|
183
|
-
function withSuppressedInitialRequiredPlanInstruction(options) {
|
|
184
|
-
return {
|
|
185
|
-
...options,
|
|
186
|
-
suppressInitialRequiredPlanInstruction: true,
|
|
187
|
-
};
|
|
188
|
-
}
|
|
189
|
-
function withNonPlanningEvidenceToolPolicy(options) {
|
|
190
|
-
return {
|
|
191
|
-
...options,
|
|
192
|
-
state: {
|
|
193
|
-
...(typeof options.state === "object" && options.state !== null ? options.state : {}),
|
|
194
|
-
[PROMPTED_JSON_TOOL_POLICY_KEY]: "nonPlanningEvidence",
|
|
195
|
-
},
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
function hasParentLocalToolExecutionAfterDelegationFailure(originalEvidence, executedToolResults) {
|
|
199
|
-
return originalEvidence.hasFailedTaskDelegation
|
|
200
|
-
&& executedToolResults.some((item) => item.toolName !== "task");
|
|
201
|
-
}
|
|
202
|
-
function isDelegationFailureFinalReport(originalEvidence, executedToolResults, visibleOutput) {
|
|
203
|
-
return originalEvidence.hasFailedTaskDelegation
|
|
204
|
-
&& executedToolResults.length === 0
|
|
205
|
-
&& visibleOutput.trim().length > 0;
|
|
206
|
-
}
|
|
207
|
-
function buildExecutionRecoveryEvidence(params) {
|
|
208
|
-
const { projectionState, executedToolResults = [] } = params;
|
|
209
|
-
return {
|
|
210
|
-
hasToolResultEvidence: executedToolResults.length > 0 || projectionState.emittedToolResult || projectionState.emittedToolError,
|
|
211
|
-
hasSuccessfulToolResultEvidence: executedToolResults.some((item) => item.isError !== true) || projectionState.emittedSuccessfulToolResult,
|
|
212
|
-
hasNonTodoToolResultEvidence: hasNonTodoToolEvidence(executedToolResults) || projectionState.emittedNonTodoToolResult || projectionState.emittedToolError,
|
|
213
|
-
hasSuccessfulNonTodoToolResultEvidence: hasSuccessfulNonTodoToolEvidence(executedToolResults) || projectionState.emittedSuccessfulNonTodoToolResult,
|
|
214
|
-
hasIncompletePlanState: projectionState.hasIncompletePlanState || hasIncompletePlanStateInExecutedToolResults(executedToolResults),
|
|
215
|
-
hasFailedPlanState: (projectionState.hasFailedPlanState || hasFailedPlanStateInExecutedToolResults(executedToolResults))
|
|
216
|
-
&& !projectionState.emittedSuccessfulNonTodoToolResult
|
|
217
|
-
&& !hasSuccessfulNonTodoToolEvidence(executedToolResults),
|
|
218
|
-
hasPlanStateEvidence: projectionState.sawPlanState || hasIncompletePlanStateInExecutedToolResults(executedToolResults),
|
|
219
|
-
hasOpenTaskDelegation: projectionState.openTaskDelegations > 0,
|
|
220
|
-
hasFailedTaskDelegation: projectionState.hasFailedTaskDelegation
|
|
221
|
-
|| executedToolResults.some((item) => item.toolName === "task" && item.isError === true),
|
|
222
|
-
hasDelegatedAgentWithConfiguredTools: projectionState.sawDelegatedAgentWithConfiguredTools,
|
|
223
|
-
hasDelegatedExecutionToolEvidence: projectionState.emittedDelegatedExecutionToolResult,
|
|
224
|
-
hasOnlyPlaceholderTaskCompletion: projectionState.emittedSuccessfulTaskResult
|
|
225
|
-
&& projectionState.emittedPlaceholderTaskResult
|
|
226
|
-
&& !projectionState.emittedDelegatedTerminalOutput,
|
|
227
|
-
hasSuccessfulTaskToolEvidence: projectionState.emittedSuccessfulTaskResult || hasSuccessfulTaskToolEvidence(executedToolResults),
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
|
-
function hasUnresolvedExecution(evidence) {
|
|
231
|
-
return (evidence.hasIncompletePlanState
|
|
232
|
-
|| evidence.hasFailedPlanState
|
|
233
|
-
|| evidence.hasFailedTaskDelegation
|
|
234
|
-
|| evidence.hasOpenTaskDelegation);
|
|
235
|
-
}
|
|
236
|
-
function hasMissingDelegatedExecutionEvidence(evidence) {
|
|
237
|
-
if (!evidence.hasDelegatedAgentWithConfiguredTools) {
|
|
238
|
-
return false;
|
|
239
|
-
}
|
|
240
|
-
if (evidence.hasOpenTaskDelegation || evidence.hasFailedTaskDelegation) {
|
|
241
|
-
return false;
|
|
242
|
-
}
|
|
243
|
-
return !evidence.hasDelegatedExecutionToolEvidence && !evidence.hasSuccessfulTaskToolEvidence;
|
|
244
|
-
}
|
|
245
|
-
function hasMissingDelegatedFindings(evidence) {
|
|
246
|
-
return evidence.hasDelegatedAgentWithConfiguredTools && evidence.hasOnlyPlaceholderTaskCompletion;
|
|
247
|
-
}
|
|
248
|
-
function hasCompletedPlanWithEvidence(evidence) {
|
|
249
|
-
return evidence.hasPlanStateEvidence
|
|
250
|
-
&& !evidence.hasIncompletePlanState
|
|
251
|
-
&& evidence.hasSuccessfulNonTodoToolResultEvidence
|
|
252
|
-
&& !evidence.hasOpenTaskDelegation
|
|
253
|
-
&& !evidence.hasFailedTaskDelegation
|
|
254
|
-
&& !hasMissingDelegatedExecutionEvidence(evidence)
|
|
255
|
-
&& !hasMissingDelegatedFindings(evidence);
|
|
256
|
-
}
|
|
257
|
-
function buildDeterministicFinalFromStreamToolEvidence(executedToolResults) {
|
|
258
|
-
const evidence = executedToolResults
|
|
259
|
-
.filter((item) => item.isError !== true && !isPlanToolName(item.toolName))
|
|
260
|
-
.map((item) => {
|
|
261
|
-
const visible = toVisibleContent(item.output);
|
|
262
|
-
const raw = visible || stringifyToolOutput(item.output).trim();
|
|
263
|
-
const clipped = raw.length > 4000 ? `${raw.slice(0, 4000)}\n... [truncated]` : raw;
|
|
264
|
-
return `## ${item.toolName}\n${clipped}`;
|
|
265
|
-
});
|
|
266
|
-
return [
|
|
267
|
-
"Status: completed",
|
|
268
|
-
"",
|
|
269
|
-
"Summary:",
|
|
270
|
-
"- Completed the required TODO burn down after collecting tool evidence.",
|
|
271
|
-
"- Returning an evidence-grounded deterministic summary because the streamed model did not produce a clean final synthesis before the required plan completed.",
|
|
272
|
-
"",
|
|
273
|
-
"Evidence:",
|
|
274
|
-
evidence.length > 0 ? evidence.join("\n\n") : "(no non-planning tool evidence captured)",
|
|
275
|
-
].join("\n");
|
|
276
|
-
}
|
|
277
|
-
function latestStreamToolErrorRecoveryInstruction(executedToolResults) {
|
|
278
|
-
const latest = [...executedToolResults].reverse().find((item) => item.isError === true);
|
|
279
|
-
if (!latest) {
|
|
280
|
-
return null;
|
|
281
|
-
}
|
|
282
|
-
const message = typeof latest.output === "string" ? latest.output : JSON.stringify(latest.output);
|
|
283
|
-
return resolveToolCallRecoveryInstruction(new Error(message)) ?? REQUIRED_EXECUTION_CONTINUATION_INSTRUCTION;
|
|
284
|
-
}
|
|
285
|
-
function hasUsefulVisibleSynthesis(value) {
|
|
286
|
-
const trimmed = value.trim();
|
|
287
|
-
if (trimmed.length < 80) {
|
|
288
|
-
return false;
|
|
289
|
-
}
|
|
290
|
-
if (/^(?:model_request|tool_call|call_tool)/iu.test(trimmed)) {
|
|
291
|
-
return false;
|
|
292
|
-
}
|
|
293
|
-
if (/^(?:name|tool_call_id)\s*=/iu.test(trimmed)) {
|
|
294
|
-
return false;
|
|
295
|
-
}
|
|
296
|
-
if (/^(?:we\s+need\s+to|so\s+next\s+step\b)/iu.test(trimmed)) {
|
|
297
|
-
return false;
|
|
298
|
-
}
|
|
299
|
-
if (/^\{\s*"(?:name|arguments|args|argv|todos|symbol|query|market|count|stdout|stderr|exitCode)"\s*:/iu.test(trimmed)) {
|
|
300
|
-
return false;
|
|
301
|
-
}
|
|
302
|
-
if (/^(?:stdout|stderr|exitCode)\s*:/iu.test(trimmed)) {
|
|
303
|
-
return false;
|
|
304
|
-
}
|
|
305
|
-
if (/\b(?:must|need|needs|should|will)\s+(?:now\s+)?call\s+[A-Za-z_][A-Za-z0-9_]*\b/iu.test(trimmed)) {
|
|
306
|
-
return false;
|
|
307
|
-
}
|
|
308
|
-
if (/\b(?:let'?s\s+try|we\s+can(?:not|'t)?\s+(?:run|call|use|try)|cannot\s+.+\btry\b|can\s+run\s+[A-Za-z_][A-Za-z0-9_]*)/iu.test(trimmed)) {
|
|
309
|
-
return false;
|
|
310
|
-
}
|
|
311
|
-
try {
|
|
312
|
-
const parsed = JSON.parse(trimmed);
|
|
313
|
-
if (typeof parsed === "object" && parsed !== null) {
|
|
314
|
-
return false;
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
catch {
|
|
318
|
-
// Non-JSON prose can be useful synthesis.
|
|
319
|
-
}
|
|
320
|
-
return true;
|
|
321
|
-
}
|
|
322
|
-
function readBindingExecutionParams(binding) {
|
|
323
|
-
const params = binding.execution?.params ?? binding.deepAgentParams ?? binding.langchainAgentParams;
|
|
324
|
-
return {
|
|
325
|
-
tools: Array.isArray(params?.tools) ? params.tools : [],
|
|
326
|
-
subagents: Array.isArray(params?.subagents)
|
|
327
|
-
? (params.subagents ?? [])
|
|
328
|
-
: [],
|
|
329
|
-
};
|
|
330
|
-
}
|
|
331
|
-
function isDelegationOnlyBinding(binding) {
|
|
332
|
-
const { tools, subagents } = readBindingExecutionParams(binding);
|
|
333
|
-
const agent = binding.agent;
|
|
334
|
-
const configuredSubagents = [
|
|
335
|
-
...subagents,
|
|
336
|
-
...(agent?.subagentRefs ?? []),
|
|
337
|
-
...(agent?.subagentPathRefs ?? []),
|
|
338
|
-
...(agent?.asyncSubagents ?? []),
|
|
339
|
-
];
|
|
340
|
-
const configuredTools = [
|
|
341
|
-
...tools,
|
|
342
|
-
...(agent?.toolRefs ?? []),
|
|
343
|
-
...(agent?.toolBindings ?? []),
|
|
344
|
-
];
|
|
345
|
-
const skillRefs = agent?.skillPathRefs ?? [];
|
|
346
|
-
return configuredSubagents.length > 0 && configuredTools.length === 0 && skillRefs.length === 0;
|
|
347
|
-
}
|
|
348
|
-
function isOpenAICompatibleStreamingCompatibilityError(binding, error) {
|
|
349
|
-
const params = binding.execution?.params ?? binding.deepAgentParams ?? binding.langchainAgentParams;
|
|
350
|
-
const model = params?.model;
|
|
351
|
-
if (model?.provider !== "openai-compatible") {
|
|
352
|
-
return false;
|
|
353
|
-
}
|
|
354
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
355
|
-
return message.toLowerCase().includes("received empty response from chat model call");
|
|
356
|
-
}
|
|
357
|
-
function isGraphRecursionLimitError(error) {
|
|
358
|
-
const code = typeof error === "object" && error !== null && "lc_error_code" in error
|
|
359
|
-
? String(error.lc_error_code ?? "")
|
|
360
|
-
: "";
|
|
361
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
362
|
-
return code === "GRAPH_RECURSION_LIMIT"
|
|
363
|
-
|| /Recursion limit .* without hitting a stop condition|GRAPH_RECURSION_LIMIT/i.test(message);
|
|
364
|
-
}
|
|
365
|
-
function hasDelegationEvidence(evidence) {
|
|
366
|
-
return (evidence.hasSuccessfulTaskToolEvidence
|
|
367
|
-
|| evidence.hasOpenTaskDelegation
|
|
368
|
-
|| evidence.hasFailedTaskDelegation
|
|
369
|
-
|| evidence.hasDelegatedAgentWithConfiguredTools
|
|
370
|
-
|| evidence.hasDelegatedExecutionToolEvidence);
|
|
371
|
-
}
|
|
372
|
-
function resolveDelegationOnlyRecoveryInstruction(binding, evidence) {
|
|
373
|
-
if (!isDelegationOnlyBinding(binding) || hasDelegationEvidence(evidence)) {
|
|
374
|
-
return null;
|
|
375
|
-
}
|
|
376
|
-
return DELEGATION_ONLY_RECOVERY_INSTRUCTION;
|
|
377
|
-
}
|
|
378
|
-
function isRuntimeFailureOutput(value) {
|
|
379
|
-
return value.trim().startsWith("runtime_error=");
|
|
380
|
-
}
|
|
381
|
-
function resolveStreamedRuntimeFailureRecoveryInstruction(output, evidence) {
|
|
382
|
-
if (!isRuntimeFailureOutput(output)) {
|
|
383
|
-
return null;
|
|
384
|
-
}
|
|
385
|
-
const hasExecutionEvidence = evidence.hasToolResultEvidence
|
|
386
|
-
|| evidence.hasPlanStateEvidence
|
|
387
|
-
|| evidence.hasOpenTaskDelegation
|
|
388
|
-
|| evidence.hasDelegatedExecutionToolEvidence;
|
|
389
|
-
return hasExecutionEvidence ? null : EXECUTION_WITH_TOOL_EVIDENCE_RETRY_INSTRUCTION;
|
|
390
|
-
}
|
|
391
|
-
function resolveDelegatedExecutionRecoveryInstruction(evidence) {
|
|
392
|
-
if (evidence.hasFailedTaskDelegation) {
|
|
393
|
-
return DELEGATED_TASK_FAILURE_RECOVERY_INSTRUCTION;
|
|
394
|
-
}
|
|
395
|
-
if (hasMissingDelegatedFindings(evidence)
|
|
396
|
-
|| (evidence.hasOpenTaskDelegation
|
|
397
|
-
&& evidence.hasDelegatedAgentWithConfiguredTools
|
|
398
|
-
&& !evidence.hasPlanStateEvidence
|
|
399
|
-
&& !evidence.hasFailedTaskDelegation)) {
|
|
400
|
-
return EXECUTION_WITH_TOOL_EVIDENCE_RETRY_INSTRUCTION;
|
|
401
|
-
}
|
|
402
|
-
return null;
|
|
403
|
-
}
|
|
404
|
-
function createUnresolvedExecutionError(evidence) {
|
|
405
|
-
const reasons = [];
|
|
406
|
-
if (evidence.hasIncompletePlanState) {
|
|
407
|
-
reasons.push("plan state still has unfinished work");
|
|
408
|
-
}
|
|
409
|
-
if (evidence.hasFailedPlanState) {
|
|
410
|
-
reasons.push("plan state failed before non-TODO evidence returned");
|
|
411
|
-
}
|
|
412
|
-
if (evidence.hasFailedTaskDelegation) {
|
|
413
|
-
reasons.push("delegated task failed before surfacing final findings");
|
|
414
|
-
}
|
|
415
|
-
if (evidence.hasOpenTaskDelegation) {
|
|
416
|
-
reasons.push("delegated task has not finished");
|
|
417
|
-
}
|
|
418
|
-
if (hasMissingDelegatedFindings(evidence)) {
|
|
419
|
-
reasons.push("delegated task returned only the upstream placeholder result without surfaced final findings");
|
|
420
|
-
}
|
|
421
|
-
const detail = reasons.length > 0 ? `: ${reasons.join("; ")}` : "";
|
|
422
|
-
return new ExecutionReconciliationError(`Agent ended before execution was fully reconciled${detail}.`);
|
|
423
|
-
}
|
|
424
|
-
function createProfileStep(id, kind, name, action, status, detail) {
|
|
425
|
-
return {
|
|
426
|
-
kind: "profile",
|
|
427
|
-
step: {
|
|
428
|
-
id,
|
|
429
|
-
kind,
|
|
430
|
-
name,
|
|
431
|
-
action,
|
|
432
|
-
status,
|
|
433
|
-
...(detail ? { detail } : {}),
|
|
434
|
-
},
|
|
435
|
-
};
|
|
436
|
-
}
|
|
437
|
-
function startProfileStep(input) {
|
|
438
|
-
const startedAt = new Date().toISOString();
|
|
439
|
-
return {
|
|
440
|
-
startedAt,
|
|
441
|
-
chunk: createProfileStep(input.id, input.kind, input.name, input.action, "started", {
|
|
442
|
-
...(input.detail ?? {}),
|
|
443
|
-
startedAt,
|
|
444
|
-
}),
|
|
445
|
-
};
|
|
446
|
-
}
|
|
447
|
-
function finishProfileStep(input) {
|
|
448
|
-
const endedAt = new Date().toISOString();
|
|
449
|
-
return createProfileStep(input.id, input.kind, input.name, input.action, input.status, {
|
|
450
|
-
...(input.detail ?? {}),
|
|
451
|
-
startedAt: input.startedAt,
|
|
452
|
-
endedAt,
|
|
453
|
-
durationMs: Math.max(0, new Date(endedAt).getTime() - new Date(input.startedAt).getTime()),
|
|
454
|
-
...(input.error !== undefined ? { error: input.error instanceof Error ? input.error.message : String(input.error) } : {}),
|
|
455
|
-
});
|
|
456
|
-
}
|
|
457
|
-
function projectLocalToolExecutionProfileChunks(executedToolResults, prefix) {
|
|
458
|
-
const chunks = [];
|
|
459
|
-
executedToolResults.forEach((toolResult, index) => {
|
|
460
|
-
if (isPlanToolName(toolResult.toolName)) {
|
|
461
|
-
return;
|
|
462
|
-
}
|
|
463
|
-
const id = `${prefix}:${index + 1}:${toolResult.toolName}`;
|
|
464
|
-
const startedAt = new Date().toISOString();
|
|
465
|
-
chunks.push({
|
|
466
|
-
kind: "profile",
|
|
467
|
-
step: {
|
|
468
|
-
id,
|
|
469
|
-
kind: "tool",
|
|
470
|
-
name: toolResult.toolName,
|
|
471
|
-
action: "invoke",
|
|
472
|
-
status: "started",
|
|
473
|
-
startedAt,
|
|
474
|
-
},
|
|
475
|
-
});
|
|
476
|
-
chunks.push({
|
|
477
|
-
kind: "profile",
|
|
478
|
-
step: {
|
|
479
|
-
id,
|
|
480
|
-
kind: "tool",
|
|
481
|
-
name: toolResult.toolName,
|
|
482
|
-
action: "invoke",
|
|
483
|
-
status: toolResult.isError === true ? "failed" : "completed",
|
|
484
|
-
startedAt,
|
|
485
|
-
endedAt: new Date().toISOString(),
|
|
486
|
-
...(toolResult.isError === true ? { isError: true } : {}),
|
|
487
|
-
},
|
|
488
|
-
});
|
|
489
|
-
});
|
|
490
|
-
return chunks;
|
|
491
|
-
}
|
|
492
|
-
export async function* streamRuntimeExecution(options) {
|
|
493
|
-
let request = buildInvocationRequest(options.binding, options.history, options.input, options.runtimeOptions);
|
|
494
|
-
if (requiresPlanEvidence(options.binding)
|
|
495
|
-
&& options.runtimeOptions.suppressInitialRequiredPlanInstruction !== true) {
|
|
496
|
-
request = withPromptedJsonToolPolicy(appendToolRecoveryInstruction(request, buildInitialRequiredPlanInstruction(options.primaryTools)), "planning");
|
|
497
|
-
}
|
|
498
|
-
let emittedUnsafeStreamSideEffects = false;
|
|
499
|
-
const shouldProfile = options.runtimeOptions.profiling === true;
|
|
500
|
-
const shouldValidateStreamOutput = shouldValidateExecutionWithoutToolEvidence(request);
|
|
501
|
-
const deferredStreamContent = [];
|
|
502
|
-
let sawRetrySafeInvalidToolSelectionError = false;
|
|
503
|
-
const projectionState = createStreamEventProjectionState();
|
|
504
|
-
if (options.runtimeOptions.externalPlanEvidence === true) {
|
|
505
|
-
projectionState.sawPlanState = true;
|
|
506
|
-
yield {
|
|
507
|
-
kind: "commentary",
|
|
508
|
-
content: `${options.binding.agent.id}: TODO evidence observed.`,
|
|
509
|
-
agentId: options.binding.agent.id,
|
|
510
|
-
};
|
|
511
|
-
}
|
|
512
|
-
const requestId = options.runtimeOptions.requestId ?? options.sessionId;
|
|
513
|
-
const buildRunnableConfig = (extra) => ({
|
|
514
|
-
...(options.resolveInvocationConfig
|
|
515
|
-
? options.resolveInvocationConfig(options.binding, {
|
|
516
|
-
sessionId: options.sessionId,
|
|
517
|
-
requestId,
|
|
518
|
-
...(options.runtimeOptions.context ? { context: options.runtimeOptions.context } : {}),
|
|
519
|
-
...(options.runtimeOptions.toolRuntimeContext ? { toolRuntimeContext: options.runtimeOptions.toolRuntimeContext } : {}),
|
|
520
|
-
})
|
|
521
|
-
: {
|
|
522
|
-
configurable: { [UPSTREAM_SESSION_CONFIG_KEY]: options.sessionId, [UPSTREAM_REQUEST_CONFIG_KEY]: requestId },
|
|
523
|
-
...(options.runtimeOptions.context ? { context: options.runtimeOptions.context } : {}),
|
|
524
|
-
...(options.runtimeOptions.toolRuntimeContext ? { toolRuntimeContext: options.runtimeOptions.toolRuntimeContext } : {}),
|
|
525
|
-
}),
|
|
526
|
-
...(extra ?? {}),
|
|
527
|
-
});
|
|
528
|
-
const shouldDeferStreamContent = () => shouldValidateStreamOutput && !emittedUnsafeStreamSideEffects;
|
|
529
|
-
const flushDeferredStreamContent = async function* () {
|
|
530
|
-
while (deferredStreamContent.length > 0) {
|
|
531
|
-
const next = deferredStreamContent.shift();
|
|
532
|
-
if (next.kind === "content") {
|
|
533
|
-
if (next.content) {
|
|
534
|
-
yield { kind: "content", content: next.content };
|
|
535
|
-
}
|
|
536
|
-
continue;
|
|
537
|
-
}
|
|
538
|
-
yield next;
|
|
539
|
-
}
|
|
540
|
-
};
|
|
541
|
-
try {
|
|
542
|
-
if (options.isLangChainBinding(options.binding) && options.canUseDirectModelStream && options.langChainStreamModel?.stream) {
|
|
543
|
-
const modelStreamStart = startProfileStep({
|
|
544
|
-
id: "profile:agent:model-stream-start",
|
|
545
|
-
kind: "agent",
|
|
546
|
-
name: "model stream",
|
|
547
|
-
action: "start",
|
|
548
|
-
});
|
|
549
|
-
if (shouldProfile)
|
|
550
|
-
yield modelStreamStart.chunk;
|
|
551
|
-
let stream;
|
|
552
|
-
try {
|
|
553
|
-
const rawStreamInput = buildRawModelMessages(options.binding, options.getSystemPrompt(options.binding), options.history, options.input, options.runtimeOptions.memoryContext);
|
|
554
|
-
const streamInput = requiresPlanEvidence(options.binding)
|
|
555
|
-
&& options.runtimeOptions.suppressInitialRequiredPlanInstruction !== true
|
|
556
|
-
? withPromptedJsonToolPolicy(rawStreamInput, "planning")
|
|
557
|
-
: rawStreamInput;
|
|
558
|
-
stream = await options.withTimeout(() => options.langChainStreamModel.stream(streamInput), computeRemainingTimeoutMs(options.streamDeadlineAt, options.invokeTimeoutMs), "model stream start", "stream");
|
|
559
|
-
if (shouldProfile)
|
|
560
|
-
yield finishProfileStep({
|
|
561
|
-
id: "profile:agent:model-stream-start",
|
|
562
|
-
kind: "agent",
|
|
563
|
-
name: "model stream",
|
|
564
|
-
action: "start",
|
|
565
|
-
startedAt: modelStreamStart.startedAt,
|
|
566
|
-
status: "completed",
|
|
567
|
-
});
|
|
568
|
-
}
|
|
569
|
-
catch (error) {
|
|
570
|
-
if (shouldProfile)
|
|
571
|
-
yield finishProfileStep({
|
|
572
|
-
id: "profile:agent:model-stream-start",
|
|
573
|
-
kind: "agent",
|
|
574
|
-
name: "model stream",
|
|
575
|
-
action: "start",
|
|
576
|
-
startedAt: modelStreamStart.startedAt,
|
|
577
|
-
status: "failed",
|
|
578
|
-
error,
|
|
579
|
-
});
|
|
580
|
-
throw error;
|
|
581
|
-
}
|
|
582
|
-
let emitted = false;
|
|
583
|
-
const projected = projectTextStreamChunks(options.iterateWithTimeout(stream, options.streamIdleTimeoutMs, "model stream", options.streamDeadlineAt, options.invokeTimeoutMs));
|
|
584
|
-
const modelStreamConsume = startProfileStep({
|
|
585
|
-
id: "profile:agent:model-stream-consume",
|
|
586
|
-
kind: "agent",
|
|
587
|
-
name: "model stream",
|
|
588
|
-
action: "consume",
|
|
589
|
-
});
|
|
590
|
-
if (shouldProfile)
|
|
591
|
-
yield modelStreamConsume.chunk;
|
|
592
|
-
try {
|
|
593
|
-
let currentChunk = await projected.next();
|
|
594
|
-
while (!currentChunk.done) {
|
|
595
|
-
if (currentChunk.value.kind === "content") {
|
|
596
|
-
emitted = true;
|
|
597
|
-
emittedUnsafeStreamSideEffects = true;
|
|
598
|
-
}
|
|
599
|
-
yield currentChunk.value;
|
|
600
|
-
currentChunk = await projected.next();
|
|
601
|
-
}
|
|
602
|
-
if (shouldProfile)
|
|
603
|
-
yield finishProfileStep({
|
|
604
|
-
id: "profile:agent:model-stream-consume",
|
|
605
|
-
kind: "agent",
|
|
606
|
-
name: "model stream",
|
|
607
|
-
action: "consume",
|
|
608
|
-
startedAt: modelStreamConsume.startedAt,
|
|
609
|
-
status: "completed",
|
|
610
|
-
});
|
|
611
|
-
if (currentChunk.value.emittedContent || emitted) {
|
|
612
|
-
return;
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
catch (error) {
|
|
616
|
-
if (shouldProfile)
|
|
617
|
-
yield finishProfileStep({
|
|
618
|
-
id: "profile:agent:model-stream-consume",
|
|
619
|
-
kind: "agent",
|
|
620
|
-
name: "model stream",
|
|
621
|
-
action: "consume",
|
|
622
|
-
startedAt: modelStreamConsume.startedAt,
|
|
623
|
-
status: "failed",
|
|
624
|
-
error,
|
|
625
|
-
});
|
|
626
|
-
throw error;
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
const createRunnableStep = startProfileStep({
|
|
630
|
-
id: "profile:agent:create-runnable",
|
|
631
|
-
kind: "agent",
|
|
632
|
-
name: "create runnable",
|
|
633
|
-
action: "startup",
|
|
634
|
-
});
|
|
635
|
-
if (shouldProfile)
|
|
636
|
-
yield createRunnableStep.chunk;
|
|
637
|
-
let runnable;
|
|
638
|
-
try {
|
|
639
|
-
runnable = await options.createRunnable();
|
|
640
|
-
if (shouldProfile)
|
|
641
|
-
yield finishProfileStep({
|
|
642
|
-
id: "profile:agent:create-runnable",
|
|
643
|
-
kind: "agent",
|
|
644
|
-
name: "create runnable",
|
|
645
|
-
action: "startup",
|
|
646
|
-
startedAt: createRunnableStep.startedAt,
|
|
647
|
-
status: "completed",
|
|
648
|
-
});
|
|
649
|
-
}
|
|
650
|
-
catch (error) {
|
|
651
|
-
if (shouldProfile)
|
|
652
|
-
yield finishProfileStep({
|
|
653
|
-
id: "profile:agent:create-runnable",
|
|
654
|
-
kind: "agent",
|
|
655
|
-
name: "create runnable",
|
|
656
|
-
action: "startup",
|
|
657
|
-
startedAt: createRunnableStep.startedAt,
|
|
658
|
-
status: "failed",
|
|
659
|
-
error,
|
|
660
|
-
});
|
|
661
|
-
throw error;
|
|
662
|
-
}
|
|
663
|
-
if (!options.forceInvokeFallback && !isDelegationOnlyBinding(options.binding) && typeof runnable.streamEvents === "function") {
|
|
664
|
-
const streamEventsStart = startProfileStep({
|
|
665
|
-
id: "profile:agent:stream-events-start",
|
|
666
|
-
kind: "agent",
|
|
667
|
-
name: "streamEvents",
|
|
668
|
-
action: "start",
|
|
669
|
-
});
|
|
670
|
-
if (shouldProfile)
|
|
671
|
-
yield streamEventsStart.chunk;
|
|
672
|
-
let events;
|
|
673
|
-
try {
|
|
674
|
-
events = await options.withTimeout(() => runnable.streamEvents(request, buildRunnableConfig({ version: "v2" })), computeRemainingTimeoutMs(options.streamDeadlineAt, options.invokeTimeoutMs), "agent streamEvents start", "stream");
|
|
675
|
-
if (shouldProfile)
|
|
676
|
-
yield finishProfileStep({
|
|
677
|
-
id: "profile:agent:stream-events-start",
|
|
678
|
-
kind: "agent",
|
|
679
|
-
name: "streamEvents",
|
|
680
|
-
action: "start",
|
|
681
|
-
startedAt: streamEventsStart.startedAt,
|
|
682
|
-
status: "completed",
|
|
683
|
-
});
|
|
684
|
-
}
|
|
685
|
-
catch (error) {
|
|
686
|
-
if (shouldProfile)
|
|
687
|
-
yield finishProfileStep({
|
|
688
|
-
id: "profile:agent:stream-events-start",
|
|
689
|
-
kind: "agent",
|
|
690
|
-
name: "streamEvents",
|
|
691
|
-
action: "start",
|
|
692
|
-
startedAt: streamEventsStart.startedAt,
|
|
693
|
-
status: "failed",
|
|
694
|
-
error,
|
|
695
|
-
});
|
|
696
|
-
if (!emittedUnsafeStreamSideEffects
|
|
697
|
-
&& (isOpenAICompatibleStreamingCompatibilityError(options.binding, error) || isGraphRecursionLimitError(error))) {
|
|
698
|
-
deferredStreamContent.length = 0;
|
|
699
|
-
}
|
|
700
|
-
else {
|
|
701
|
-
throw error;
|
|
702
|
-
}
|
|
703
|
-
}
|
|
704
|
-
const streamedToolResults = [];
|
|
705
|
-
if (events) {
|
|
706
|
-
const streamEventsConsume = startProfileStep({
|
|
707
|
-
id: "profile:agent:stream-events-consume",
|
|
708
|
-
kind: "agent",
|
|
709
|
-
name: "streamEvents",
|
|
710
|
-
action: "consume",
|
|
711
|
-
});
|
|
712
|
-
if (shouldProfile)
|
|
713
|
-
yield streamEventsConsume.chunk;
|
|
714
|
-
try {
|
|
715
|
-
let sawCompletedPlanToolResult = false;
|
|
716
|
-
let sawSuccessfulNonTodoToolResult = false;
|
|
717
|
-
let earlyStreamRecoveryInstruction = null;
|
|
718
|
-
let earlyStreamRecoverySuppressInitialPlan = false;
|
|
719
|
-
let completedPlanToolResultCount = 0;
|
|
720
|
-
for await (const event of options.iterateWithTimeout(events, options.streamIdleTimeoutMs, "agent streamEvents", options.streamDeadlineAt, options.invokeTimeoutMs)) {
|
|
721
|
-
const projectedChunks = projectRuntimeStreamEvent({
|
|
722
|
-
event,
|
|
723
|
-
allowVisibleStreamDeltas: true,
|
|
724
|
-
includeStateStreamOutput: options.isDeepAgentBinding(options.binding),
|
|
725
|
-
rootAgentId: typeof options.binding.agent?.id === "string"
|
|
726
|
-
? options.binding.agent.id
|
|
727
|
-
: undefined,
|
|
728
|
-
countConfiguredToolsForAgentId: options.countConfiguredToolsForAgentId,
|
|
729
|
-
toolNameMapping: options.toolNameMapping,
|
|
730
|
-
primaryTools: options.primaryTools,
|
|
731
|
-
state: projectionState,
|
|
732
|
-
});
|
|
733
|
-
const eventContainsNonTodoToolResult = projectedChunks.some((chunk) => chunk.kind === "tool-result"
|
|
734
|
-
&& !isPlanToolName(chunk.toolName)
|
|
735
|
-
&& !(chunk.isError === true && isRetrySafeInvalidToolSelectionError(chunk.output)));
|
|
736
|
-
const eventContainsNonRetrySafeChunk = projectedChunks.some((chunk) => chunk.kind !== "upstream-event"
|
|
737
|
-
&& chunk.kind !== "content"
|
|
738
|
-
&& !(chunk.kind === "tool-result" && isPlanToolName(chunk.toolName))
|
|
739
|
-
&& !(chunk.kind === "tool-result" && chunk.isError === true)
|
|
740
|
-
&& !(chunk.kind === "tool-result" && chunk.isError === true && isRetrySafeInvalidToolSelectionError(chunk.output)));
|
|
741
|
-
const hadPriorPlanToolResult = completedPlanToolResultCount > 0;
|
|
742
|
-
const repeatedPlanToolResultBeforeEvidence = requiresPlanEvidence(options.binding)
|
|
743
|
-
&& !sawSuccessfulNonTodoToolResult
|
|
744
|
-
&& hadPriorPlanToolResult
|
|
745
|
-
&& projectedChunks.some((chunk) => chunk.kind === "tool-result" && isPlanToolName(chunk.toolName));
|
|
746
|
-
if (repeatedPlanToolResultBeforeEvidence) {
|
|
747
|
-
earlyStreamRecoveryInstruction = buildRunEvidenceAfterPlanInstruction(options.primaryTools);
|
|
748
|
-
earlyStreamRecoverySuppressInitialPlan = true;
|
|
749
|
-
break;
|
|
750
|
-
}
|
|
751
|
-
for (const chunk of projectedChunks) {
|
|
752
|
-
if (chunk.kind === "tool-result" && chunk.isError === true && isRetrySafeInvalidToolSelectionError(chunk.output)) {
|
|
753
|
-
sawRetrySafeInvalidToolSelectionError = true;
|
|
754
|
-
}
|
|
755
|
-
if (chunk.kind === "tool-result") {
|
|
756
|
-
streamedToolResults.push({
|
|
757
|
-
toolName: chunk.toolName,
|
|
758
|
-
output: chunk.output,
|
|
759
|
-
isError: chunk.isError,
|
|
760
|
-
});
|
|
761
|
-
}
|
|
762
|
-
if (chunk.kind === "tool-result" && !isPlanToolName(chunk.toolName) && chunk.isError !== true) {
|
|
763
|
-
sawSuccessfulNonTodoToolResult = true;
|
|
764
|
-
}
|
|
765
|
-
if (isCompletedPlanToolResultChunk(chunk)) {
|
|
766
|
-
sawCompletedPlanToolResult = true;
|
|
767
|
-
}
|
|
768
|
-
if (chunk.kind === "tool-result" && isPlanToolName(chunk.toolName)) {
|
|
769
|
-
completedPlanToolResultCount += 1;
|
|
770
|
-
}
|
|
771
|
-
if ((eventContainsNonTodoToolResult || eventContainsNonRetrySafeChunk) && deferredStreamContent.length > 0) {
|
|
772
|
-
yield* flushDeferredStreamContent();
|
|
773
|
-
}
|
|
774
|
-
if (eventContainsNonTodoToolResult || eventContainsNonRetrySafeChunk) {
|
|
775
|
-
emittedUnsafeStreamSideEffects = true;
|
|
776
|
-
}
|
|
777
|
-
const shouldDeferRequiredPlanContent = requiresPlanEvidence(options.binding)
|
|
778
|
-
&& projectionState.sawPlanState;
|
|
779
|
-
if (chunk.kind === "content"
|
|
780
|
-
&& (shouldDeferStreamContent() || projectionState.hasFailedTaskDelegation || shouldDeferRequiredPlanContent)) {
|
|
781
|
-
deferredStreamContent.push(chunk);
|
|
782
|
-
continue;
|
|
783
|
-
}
|
|
784
|
-
yield chunk;
|
|
785
|
-
}
|
|
786
|
-
const eventContainsPlanToolResult = projectedChunks.some((chunk) => chunk.kind === "tool-result" && isPlanToolName(chunk.toolName) && chunk.isError !== true);
|
|
787
|
-
if (requiresPlanEvidence(options.binding)
|
|
788
|
-
&& eventContainsPlanToolResult
|
|
789
|
-
&& (hadPriorPlanToolResult
|
|
790
|
-
|| projectedChunks.some((chunk) => isCompletedPlanToolResultChunk(chunk)))
|
|
791
|
-
&& !sawSuccessfulNonTodoToolResult) {
|
|
792
|
-
earlyStreamRecoveryInstruction = buildRunEvidenceAfterPlanInstruction(options.primaryTools);
|
|
793
|
-
earlyStreamRecoverySuppressInitialPlan = true;
|
|
794
|
-
break;
|
|
795
|
-
}
|
|
796
|
-
if (requiresPlanEvidence(options.binding) && sawCompletedPlanToolResult && sawSuccessfulNonTodoToolResult) {
|
|
797
|
-
if (hasUsefulVisibleSynthesis(projectionState.emittedOutput)) {
|
|
798
|
-
if (deferredStreamContent.length > 0) {
|
|
799
|
-
yield* flushDeferredStreamContent();
|
|
800
|
-
}
|
|
801
|
-
return;
|
|
802
|
-
}
|
|
803
|
-
deferredStreamContent.length = 0;
|
|
804
|
-
yield { kind: "content", content: buildDeterministicFinalFromStreamToolEvidence(streamedToolResults) };
|
|
805
|
-
return;
|
|
806
|
-
}
|
|
807
|
-
const eventExecutionEvidence = buildExecutionRecoveryEvidence({ projectionState });
|
|
808
|
-
if (requiresPlanEvidence(options.binding) && hasCompletedPlanWithEvidence(eventExecutionEvidence)) {
|
|
809
|
-
if (hasUsefulVisibleSynthesis(projectionState.emittedOutput)) {
|
|
810
|
-
if (deferredStreamContent.length > 0) {
|
|
811
|
-
yield* flushDeferredStreamContent();
|
|
812
|
-
}
|
|
813
|
-
return;
|
|
814
|
-
}
|
|
815
|
-
deferredStreamContent.length = 0;
|
|
816
|
-
yield { kind: "content", content: buildDeterministicFinalFromStreamToolEvidence(streamedToolResults) };
|
|
817
|
-
return;
|
|
818
|
-
}
|
|
819
|
-
const terminalVisibleOutput = readTerminalEventVisibleOutput(event);
|
|
820
|
-
if (terminalVisibleOutput) {
|
|
821
|
-
const terminalExecutionEvidence = buildExecutionRecoveryEvidence({ projectionState });
|
|
822
|
-
const terminalMissingPlanRecoveryInstruction = !terminalExecutionEvidence.hasDelegatedAgentWithConfiguredTools
|
|
823
|
-
&& !terminalExecutionEvidence.hasOpenTaskDelegation
|
|
824
|
-
&& !projectionState.emittedSuccessfulTaskResult
|
|
825
|
-
? resolveMissingPlanRecoveryInstruction({
|
|
826
|
-
request,
|
|
827
|
-
assistantText: terminalVisibleOutput,
|
|
828
|
-
requiresPlan: requiresPlanEvidence(options.binding),
|
|
829
|
-
hasPlanStateEvidence: terminalExecutionEvidence.hasPlanStateEvidence,
|
|
830
|
-
hasWriteTodosEvidence: terminalExecutionEvidence.hasPlanStateEvidence,
|
|
831
|
-
hasToolResultEvidence: terminalExecutionEvidence.hasSuccessfulNonTodoToolResultEvidence,
|
|
832
|
-
})
|
|
833
|
-
: null;
|
|
834
|
-
const terminalDelegationOnlyRecoveryInstruction = resolveDelegationOnlyRecoveryInstruction(options.binding, terminalExecutionEvidence);
|
|
835
|
-
const terminalPrematurePlanCloseRecoveryInstruction = requiresPlanEvidence(options.binding)
|
|
836
|
-
&& terminalExecutionEvidence.hasPlanStateEvidence
|
|
837
|
-
&& !terminalExecutionEvidence.hasSuccessfulNonTodoToolResultEvidence
|
|
838
|
-
? buildRunEvidenceAfterPlanInstruction(options.primaryTools)
|
|
839
|
-
: null;
|
|
840
|
-
if (!emittedUnsafeStreamSideEffects
|
|
841
|
-
&& (terminalMissingPlanRecoveryInstruction
|
|
842
|
-
|| terminalPrematurePlanCloseRecoveryInstruction
|
|
843
|
-
|| terminalDelegationOnlyRecoveryInstruction)) {
|
|
844
|
-
earlyStreamRecoveryInstruction =
|
|
845
|
-
terminalMissingPlanRecoveryInstruction
|
|
846
|
-
?? terminalPrematurePlanCloseRecoveryInstruction
|
|
847
|
-
?? terminalDelegationOnlyRecoveryInstruction;
|
|
848
|
-
earlyStreamRecoverySuppressInitialPlan = terminalPrematurePlanCloseRecoveryInstruction !== null;
|
|
849
|
-
break;
|
|
850
|
-
}
|
|
851
|
-
if (requiresPlanEvidence(options.binding) && hasCompletedPlanWithEvidence(terminalExecutionEvidence)) {
|
|
852
|
-
if (hasUsefulVisibleSynthesis(projectionState.emittedOutput)) {
|
|
853
|
-
if (deferredStreamContent.length > 0) {
|
|
854
|
-
yield* flushDeferredStreamContent();
|
|
855
|
-
}
|
|
856
|
-
return;
|
|
857
|
-
}
|
|
858
|
-
deferredStreamContent.length = 0;
|
|
859
|
-
yield { kind: "content", content: buildDeterministicFinalFromStreamToolEvidence(streamedToolResults) };
|
|
860
|
-
return;
|
|
861
|
-
}
|
|
862
|
-
if (!shouldDeferStreamContent()
|
|
863
|
-
&& !terminalExecutionEvidence.hasIncompletePlanState
|
|
864
|
-
&& !terminalExecutionEvidence.hasFailedTaskDelegation
|
|
865
|
-
&& !terminalExecutionEvidence.hasOpenTaskDelegation
|
|
866
|
-
&& !hasMissingDelegatedExecutionEvidence(terminalExecutionEvidence)
|
|
867
|
-
&& !hasMissingDelegatedFindings(terminalExecutionEvidence)
|
|
868
|
-
&& !terminalMissingPlanRecoveryInstruction
|
|
869
|
-
&& !terminalPrematurePlanCloseRecoveryInstruction
|
|
870
|
-
&& !terminalDelegationOnlyRecoveryInstruction) {
|
|
871
|
-
if (deferredStreamContent.length > 0) {
|
|
872
|
-
yield* flushDeferredStreamContent();
|
|
873
|
-
}
|
|
874
|
-
return;
|
|
875
|
-
}
|
|
876
|
-
}
|
|
877
|
-
}
|
|
878
|
-
if (earlyStreamRecoveryInstruction) {
|
|
879
|
-
const earlyRecoveryRuntimeOptions = earlyStreamRecoverySuppressInitialPlan
|
|
880
|
-
? {
|
|
881
|
-
...withNonPlanningEvidenceToolPolicy(withSuppressedInitialRequiredPlanInstruction(options.runtimeOptions)),
|
|
882
|
-
externalPlanEvidence: true,
|
|
883
|
-
}
|
|
884
|
-
: options.runtimeOptions;
|
|
885
|
-
const recovered = await options.invoke(options.applyToolRecoveryInstruction(options.binding, earlyStreamRecoveryInstruction), options.input, options.sessionId, options.runtimeOptions.requestId ?? options.sessionId, undefined, options.history, earlyRecoveryRuntimeOptions);
|
|
886
|
-
const recoveredToolResults = Array.isArray(recovered.metadata?.executedToolResults)
|
|
887
|
-
? recovered.metadata.executedToolResults
|
|
888
|
-
: [];
|
|
889
|
-
const originalExecutionEvidence = buildExecutionRecoveryEvidence({ projectionState });
|
|
890
|
-
const recoveredExecutionEvidence = buildExecutionRecoveryEvidence({
|
|
891
|
-
projectionState: createStreamEventProjectionState(),
|
|
892
|
-
executedToolResults: recoveredToolResults,
|
|
893
|
-
});
|
|
894
|
-
const recoveredCarriesExecutionEvidence = recoveredExecutionEvidence.hasSuccessfulNonTodoToolResultEvidence
|
|
895
|
-
|| recoveredExecutionEvidence.hasOpenTaskDelegation
|
|
896
|
-
|| recoveredExecutionEvidence.hasDelegatedExecutionToolEvidence;
|
|
897
|
-
const recoveredHasUnresolvedExecution = hasUnresolvedExecution(recoveredExecutionEvidence)
|
|
898
|
-
|| hasMissingDelegatedExecutionEvidence(recoveredExecutionEvidence)
|
|
899
|
-
|| hasMissingDelegatedFindings(recoveredExecutionEvidence)
|
|
900
|
-
|| (!recoveredCarriesExecutionEvidence
|
|
901
|
-
&& (hasUnresolvedExecution(originalExecutionEvidence)
|
|
902
|
-
|| hasMissingDelegatedExecutionEvidence(originalExecutionEvidence)
|
|
903
|
-
|| hasMissingDelegatedFindings(originalExecutionEvidence)));
|
|
904
|
-
if (recoveredHasUnresolvedExecution) {
|
|
905
|
-
const effectiveRecoveryEvidence = recoveredCarriesExecutionEvidence
|
|
906
|
-
? recoveredExecutionEvidence
|
|
907
|
-
: {
|
|
908
|
-
...recoveredExecutionEvidence,
|
|
909
|
-
hasIncompletePlanState: originalExecutionEvidence.hasIncompletePlanState,
|
|
910
|
-
hasFailedPlanState: originalExecutionEvidence.hasFailedPlanState,
|
|
911
|
-
hasFailedTaskDelegation: originalExecutionEvidence.hasFailedTaskDelegation,
|
|
912
|
-
hasOpenTaskDelegation: originalExecutionEvidence.hasOpenTaskDelegation,
|
|
913
|
-
hasDelegatedAgentWithConfiguredTools: originalExecutionEvidence.hasDelegatedAgentWithConfiguredTools,
|
|
914
|
-
hasDelegatedExecutionToolEvidence: originalExecutionEvidence.hasDelegatedExecutionToolEvidence,
|
|
915
|
-
};
|
|
916
|
-
throw createUnresolvedExecutionError(effectiveRecoveryEvidence);
|
|
917
|
-
}
|
|
918
|
-
for (const toolResult of recoveredToolResults) {
|
|
919
|
-
yield {
|
|
920
|
-
kind: "tool-result",
|
|
921
|
-
toolName: toolResult.toolName,
|
|
922
|
-
output: toolResult.output,
|
|
923
|
-
isError: toolResult.isError,
|
|
924
|
-
};
|
|
925
|
-
}
|
|
926
|
-
if (recovered.output) {
|
|
927
|
-
const visible = toVisibleContent(recovered.output);
|
|
928
|
-
if (visible) {
|
|
929
|
-
yield { kind: "content", content: visible };
|
|
930
|
-
}
|
|
931
|
-
}
|
|
932
|
-
return;
|
|
933
|
-
}
|
|
934
|
-
if (shouldProfile)
|
|
935
|
-
yield finishProfileStep({
|
|
936
|
-
id: "profile:agent:stream-events-consume",
|
|
937
|
-
kind: "agent",
|
|
938
|
-
name: "streamEvents",
|
|
939
|
-
action: "consume",
|
|
940
|
-
startedAt: streamEventsConsume.startedAt,
|
|
941
|
-
status: "completed",
|
|
942
|
-
});
|
|
943
|
-
}
|
|
944
|
-
catch (error) {
|
|
945
|
-
if (shouldProfile)
|
|
946
|
-
yield finishProfileStep({
|
|
947
|
-
id: "profile:agent:stream-events-consume",
|
|
948
|
-
kind: "agent",
|
|
949
|
-
name: "streamEvents",
|
|
950
|
-
action: "consume",
|
|
951
|
-
startedAt: streamEventsConsume.startedAt,
|
|
952
|
-
status: "failed",
|
|
953
|
-
error,
|
|
954
|
-
});
|
|
955
|
-
if (!emittedUnsafeStreamSideEffects
|
|
956
|
-
&& (isOpenAICompatibleStreamingCompatibilityError(options.binding, error) || isGraphRecursionLimitError(error))) {
|
|
957
|
-
deferredStreamContent.length = 0;
|
|
958
|
-
}
|
|
959
|
-
else {
|
|
960
|
-
throw error;
|
|
961
|
-
}
|
|
962
|
-
}
|
|
963
|
-
}
|
|
964
|
-
const streamedExecutionEvidence = buildExecutionRecoveryEvidence({ projectionState });
|
|
965
|
-
if (requiresPlanEvidence(options.binding) && hasCompletedPlanWithEvidence(streamedExecutionEvidence)) {
|
|
966
|
-
if (hasUsefulVisibleSynthesis(projectionState.emittedOutput)) {
|
|
967
|
-
if (deferredStreamContent.length > 0) {
|
|
968
|
-
yield* flushDeferredStreamContent();
|
|
969
|
-
}
|
|
970
|
-
return;
|
|
971
|
-
}
|
|
972
|
-
deferredStreamContent.length = 0;
|
|
973
|
-
yield { kind: "content", content: buildDeterministicFinalFromStreamToolEvidence(streamedToolResults) };
|
|
974
|
-
return;
|
|
975
|
-
}
|
|
976
|
-
const streamedDelegatedRecoveryInstruction = resolveDelegatedExecutionRecoveryInstruction(streamedExecutionEvidence);
|
|
977
|
-
const streamedDelegationOnlyRecoveryInstruction = resolveDelegationOnlyRecoveryInstruction(options.binding, streamedExecutionEvidence);
|
|
978
|
-
const streamedIncompletePlanRecoveryInstruction = requiresPlanEvidence(options.binding)
|
|
979
|
-
&& streamedExecutionEvidence.hasIncompletePlanState
|
|
980
|
-
&& streamedExecutionEvidence.hasSuccessfulNonTodoToolResultEvidence
|
|
981
|
-
? CLOSE_REQUIRED_PLAN_RECOVERY_INSTRUCTION
|
|
982
|
-
: null;
|
|
983
|
-
const streamedPrematurePlanCloseRecoveryInstruction = requiresPlanEvidence(options.binding)
|
|
984
|
-
&& streamedExecutionEvidence.hasPlanStateEvidence
|
|
985
|
-
&& !streamedExecutionEvidence.hasSuccessfulNonTodoToolResultEvidence
|
|
986
|
-
? buildRunEvidenceAfterPlanInstruction(options.primaryTools)
|
|
987
|
-
: null;
|
|
988
|
-
const delegatedExecutionRecoveryInstruction = !emittedUnsafeStreamSideEffects || streamedDelegatedRecoveryInstruction
|
|
989
|
-
? streamedDelegatedRecoveryInstruction
|
|
990
|
-
: null;
|
|
991
|
-
if (hasUnresolvedExecution(streamedExecutionEvidence)
|
|
992
|
-
&& !delegatedExecutionRecoveryInstruction
|
|
993
|
-
&& !streamedIncompletePlanRecoveryInstruction
|
|
994
|
-
&& !streamedPrematurePlanCloseRecoveryInstruction) {
|
|
995
|
-
throw createUnresolvedExecutionError(streamedExecutionEvidence);
|
|
996
|
-
}
|
|
997
|
-
const executionWithoutToolEvidenceInstruction = projectionState.emittedOutput
|
|
998
|
-
? resolveExecutionWithoutToolEvidenceTextInstruction(request, projectionState.emittedOutput, false, {
|
|
999
|
-
...streamedExecutionEvidence,
|
|
1000
|
-
hasMissingDelegatedExecutionEvidence: hasMissingDelegatedExecutionEvidence(streamedExecutionEvidence),
|
|
1001
|
-
requiresPlan: requiresPlanEvidence(options.binding),
|
|
1002
|
-
})
|
|
1003
|
-
: null;
|
|
1004
|
-
const streamedRuntimeFailureRecoveryInstruction = projectionState.emittedOutput
|
|
1005
|
-
? resolveStreamedRuntimeFailureRecoveryInstruction(projectionState.emittedOutput, streamedExecutionEvidence)
|
|
1006
|
-
: null;
|
|
1007
|
-
const streamedToolErrorRecoveryInstruction = !emittedUnsafeStreamSideEffects
|
|
1008
|
-
? latestStreamToolErrorRecoveryInstruction(streamedToolResults)
|
|
1009
|
-
: null;
|
|
1010
|
-
const missingPlanRecoveryInstruction = !hasUnresolvedExecution(streamedExecutionEvidence) && !delegatedExecutionRecoveryInstruction
|
|
1011
|
-
? resolveMissingPlanRecoveryInstruction({
|
|
1012
|
-
request,
|
|
1013
|
-
assistantText: projectionState.emittedOutput,
|
|
1014
|
-
requiresPlan: requiresPlanEvidence(options.binding),
|
|
1015
|
-
hasPlanStateEvidence: streamedExecutionEvidence.hasPlanStateEvidence,
|
|
1016
|
-
hasWriteTodosEvidence: streamedExecutionEvidence.hasPlanStateEvidence,
|
|
1017
|
-
hasToolResultEvidence: streamedExecutionEvidence.hasSuccessfulNonTodoToolResultEvidence,
|
|
1018
|
-
})
|
|
1019
|
-
: null;
|
|
1020
|
-
const retryInstruction = !emittedUnsafeStreamSideEffects && sawRetrySafeInvalidToolSelectionError
|
|
1021
|
-
? INVALID_TOOL_SELECTION_RECOVERY_INSTRUCTION
|
|
1022
|
-
: streamedToolErrorRecoveryInstruction
|
|
1023
|
-
?? delegatedExecutionRecoveryInstruction
|
|
1024
|
-
?? streamedIncompletePlanRecoveryInstruction
|
|
1025
|
-
?? streamedPrematurePlanCloseRecoveryInstruction
|
|
1026
|
-
?? streamedRuntimeFailureRecoveryInstruction
|
|
1027
|
-
?? missingPlanRecoveryInstruction
|
|
1028
|
-
?? streamedDelegationOnlyRecoveryInstruction
|
|
1029
|
-
?? executionWithoutToolEvidenceInstruction;
|
|
1030
|
-
if (retryInstruction) {
|
|
1031
|
-
const retryRuntimeOptions = retryInstruction === streamedIncompletePlanRecoveryInstruction
|
|
1032
|
-
|| retryInstruction === streamedPrematurePlanCloseRecoveryInstruction
|
|
1033
|
-
? withNonPlanningEvidenceToolPolicy(withSuppressedInitialRequiredPlanInstruction(options.runtimeOptions))
|
|
1034
|
-
: options.runtimeOptions;
|
|
1035
|
-
let retried;
|
|
1036
|
-
retried = await options.invoke(options.applyToolRecoveryInstruction(options.binding, retryInstruction), options.input, options.sessionId, options.runtimeOptions.requestId ?? options.sessionId, undefined, options.history, retryRuntimeOptions);
|
|
1037
|
-
const executedToolResults = Array.isArray(retried.metadata?.executedToolResults)
|
|
1038
|
-
? retried.metadata.executedToolResults
|
|
1039
|
-
: [];
|
|
1040
|
-
const originalExecutionEvidence = buildExecutionRecoveryEvidence({ projectionState });
|
|
1041
|
-
const retriedExecutionEvidence = buildExecutionRecoveryEvidence({
|
|
1042
|
-
projectionState: createStreamEventProjectionState(),
|
|
1043
|
-
executedToolResults,
|
|
1044
|
-
});
|
|
1045
|
-
if (hasParentLocalToolExecutionAfterDelegationFailure(originalExecutionEvidence, executedToolResults)) {
|
|
1046
|
-
throw new ExecutionReconciliationError("Agent attempted parent-local tool execution after delegated task failure; it must report a blocker or re-delegate with task.");
|
|
1047
|
-
}
|
|
1048
|
-
const retriedVisibleOutput = retried.output ? toVisibleContent(retried.output) : "";
|
|
1049
|
-
const retriedIsDelegationFailureFinalReport = isDelegationFailureFinalReport(originalExecutionEvidence, executedToolResults, retriedVisibleOutput);
|
|
1050
|
-
const retriedCarriesExecutionEvidence = retriedExecutionEvidence.hasSuccessfulNonTodoToolResultEvidence
|
|
1051
|
-
|| retriedExecutionEvidence.hasOpenTaskDelegation
|
|
1052
|
-
|| retriedExecutionEvidence.hasDelegatedExecutionToolEvidence;
|
|
1053
|
-
const retriedHasUnresolvedExecution = !retriedIsDelegationFailureFinalReport
|
|
1054
|
-
&& (hasUnresolvedExecution(retriedExecutionEvidence)
|
|
1055
|
-
|| hasMissingDelegatedExecutionEvidence(retriedExecutionEvidence)
|
|
1056
|
-
|| hasMissingDelegatedFindings(retriedExecutionEvidence)
|
|
1057
|
-
|| (!retriedCarriesExecutionEvidence
|
|
1058
|
-
&& (hasUnresolvedExecution(originalExecutionEvidence)
|
|
1059
|
-
|| hasMissingDelegatedExecutionEvidence(originalExecutionEvidence)
|
|
1060
|
-
|| hasMissingDelegatedFindings(originalExecutionEvidence))));
|
|
1061
|
-
const effectiveRecoveryEvidence = retriedCarriesExecutionEvidence
|
|
1062
|
-
? retriedExecutionEvidence
|
|
1063
|
-
: {
|
|
1064
|
-
...retriedExecutionEvidence,
|
|
1065
|
-
hasIncompletePlanState: originalExecutionEvidence.hasIncompletePlanState,
|
|
1066
|
-
hasFailedPlanState: originalExecutionEvidence.hasFailedPlanState,
|
|
1067
|
-
hasFailedTaskDelegation: originalExecutionEvidence.hasFailedTaskDelegation,
|
|
1068
|
-
hasOpenTaskDelegation: originalExecutionEvidence.hasOpenTaskDelegation,
|
|
1069
|
-
hasDelegatedAgentWithConfiguredTools: originalExecutionEvidence.hasDelegatedAgentWithConfiguredTools,
|
|
1070
|
-
hasDelegatedExecutionToolEvidence: originalExecutionEvidence.hasDelegatedExecutionToolEvidence,
|
|
1071
|
-
};
|
|
1072
|
-
if (retriedHasUnresolvedExecution) {
|
|
1073
|
-
throw createUnresolvedExecutionError(effectiveRecoveryEvidence);
|
|
1074
|
-
}
|
|
1075
|
-
for (const toolResult of executedToolResults) {
|
|
1076
|
-
yield {
|
|
1077
|
-
kind: "tool-result",
|
|
1078
|
-
toolName: toolResult.toolName,
|
|
1079
|
-
output: toolResult.output,
|
|
1080
|
-
isError: toolResult.isError,
|
|
1081
|
-
};
|
|
1082
|
-
}
|
|
1083
|
-
if (retried.output) {
|
|
1084
|
-
const visible = toVisibleContent(retried.output);
|
|
1085
|
-
if (visible)
|
|
1086
|
-
yield { kind: "content", content: visible };
|
|
1087
|
-
}
|
|
1088
|
-
return;
|
|
1089
|
-
}
|
|
1090
|
-
if (deferredStreamContent.length > 0) {
|
|
1091
|
-
yield* flushDeferredStreamContent();
|
|
1092
|
-
}
|
|
1093
|
-
if (hasMissingDelegatedExecutionEvidence(streamedExecutionEvidence)) {
|
|
1094
|
-
throw createUnresolvedExecutionError(streamedExecutionEvidence);
|
|
1095
|
-
}
|
|
1096
|
-
if (hasMissingDelegatedFindings(streamedExecutionEvidence)) {
|
|
1097
|
-
throw createUnresolvedExecutionError(streamedExecutionEvidence);
|
|
1098
|
-
}
|
|
1099
|
-
const hasUnresolvedStreamExecution = hasUnresolvedExecution(streamedExecutionEvidence);
|
|
1100
|
-
if (projectionState.emittedOutput
|
|
1101
|
-
|| ((projectionState.emittedToolResult || projectionState.emittedToolError) && !hasUnresolvedStreamExecution)) {
|
|
1102
|
-
return;
|
|
1103
|
-
}
|
|
1104
|
-
}
|
|
1105
|
-
if (!options.forceInvokeFallback
|
|
1106
|
-
&& options.isLangChainBinding(options.binding)
|
|
1107
|
-
&& !isDelegationOnlyBinding(options.binding)
|
|
1108
|
-
&& typeof runnable.stream === "function") {
|
|
1109
|
-
const streamStart = startProfileStep({
|
|
1110
|
-
id: "profile:agent:stream-start",
|
|
1111
|
-
kind: "agent",
|
|
1112
|
-
name: "stream",
|
|
1113
|
-
action: "start",
|
|
1114
|
-
});
|
|
1115
|
-
if (shouldProfile)
|
|
1116
|
-
yield streamStart.chunk;
|
|
1117
|
-
let stream;
|
|
1118
|
-
try {
|
|
1119
|
-
stream = await options.withTimeout(() => runnable.stream(request, buildRunnableConfig()), computeRemainingTimeoutMs(options.streamDeadlineAt, options.invokeTimeoutMs), "agent stream start", "stream");
|
|
1120
|
-
if (shouldProfile)
|
|
1121
|
-
yield finishProfileStep({
|
|
1122
|
-
id: "profile:agent:stream-start",
|
|
1123
|
-
kind: "agent",
|
|
1124
|
-
name: "stream",
|
|
1125
|
-
action: "start",
|
|
1126
|
-
startedAt: streamStart.startedAt,
|
|
1127
|
-
status: "completed",
|
|
1128
|
-
});
|
|
1129
|
-
}
|
|
1130
|
-
catch (error) {
|
|
1131
|
-
if (shouldProfile)
|
|
1132
|
-
yield finishProfileStep({
|
|
1133
|
-
id: "profile:agent:stream-start",
|
|
1134
|
-
kind: "agent",
|
|
1135
|
-
name: "stream",
|
|
1136
|
-
action: "start",
|
|
1137
|
-
startedAt: streamStart.startedAt,
|
|
1138
|
-
status: "failed",
|
|
1139
|
-
error,
|
|
1140
|
-
});
|
|
1141
|
-
throw error;
|
|
1142
|
-
}
|
|
1143
|
-
let emitted = false;
|
|
1144
|
-
const projected = projectTextStreamChunks(options.iterateWithTimeout(stream, options.streamIdleTimeoutMs, "agent stream", options.streamDeadlineAt, options.invokeTimeoutMs));
|
|
1145
|
-
const streamConsume = startProfileStep({
|
|
1146
|
-
id: "profile:agent:stream-consume",
|
|
1147
|
-
kind: "agent",
|
|
1148
|
-
name: "stream",
|
|
1149
|
-
action: "consume",
|
|
1150
|
-
});
|
|
1151
|
-
if (shouldProfile)
|
|
1152
|
-
yield streamConsume.chunk;
|
|
1153
|
-
try {
|
|
1154
|
-
let nextChunk = await projected.next();
|
|
1155
|
-
while (!nextChunk.done) {
|
|
1156
|
-
if (nextChunk.value.kind === "content") {
|
|
1157
|
-
emitted = true;
|
|
1158
|
-
emittedUnsafeStreamSideEffects = true;
|
|
1159
|
-
}
|
|
1160
|
-
yield nextChunk.value;
|
|
1161
|
-
nextChunk = await projected.next();
|
|
1162
|
-
}
|
|
1163
|
-
if (shouldProfile)
|
|
1164
|
-
yield finishProfileStep({
|
|
1165
|
-
id: "profile:agent:stream-consume",
|
|
1166
|
-
kind: "agent",
|
|
1167
|
-
name: "stream",
|
|
1168
|
-
action: "consume",
|
|
1169
|
-
startedAt: streamConsume.startedAt,
|
|
1170
|
-
status: "completed",
|
|
1171
|
-
});
|
|
1172
|
-
if (nextChunk.value.emittedContent || emitted) {
|
|
1173
|
-
return;
|
|
1174
|
-
}
|
|
1175
|
-
}
|
|
1176
|
-
catch (error) {
|
|
1177
|
-
if (shouldProfile)
|
|
1178
|
-
yield finishProfileStep({
|
|
1179
|
-
id: "profile:agent:stream-consume",
|
|
1180
|
-
kind: "agent",
|
|
1181
|
-
name: "stream",
|
|
1182
|
-
action: "consume",
|
|
1183
|
-
startedAt: streamConsume.startedAt,
|
|
1184
|
-
status: "failed",
|
|
1185
|
-
error,
|
|
1186
|
-
});
|
|
1187
|
-
throw error;
|
|
1188
|
-
}
|
|
1189
|
-
}
|
|
1190
|
-
const invokeFallback = startProfileStep({
|
|
1191
|
-
id: "profile:agent:invoke-fallback",
|
|
1192
|
-
kind: "agent",
|
|
1193
|
-
name: "invoke fallback",
|
|
1194
|
-
action: "invoke",
|
|
1195
|
-
});
|
|
1196
|
-
if (shouldProfile)
|
|
1197
|
-
yield invokeFallback.chunk;
|
|
1198
|
-
let result;
|
|
1199
|
-
try {
|
|
1200
|
-
result = await options.invoke(options.binding, options.input, options.sessionId, options.runtimeOptions.requestId ?? options.sessionId, undefined, options.history, options.runtimeOptions);
|
|
1201
|
-
if (shouldProfile)
|
|
1202
|
-
yield finishProfileStep({
|
|
1203
|
-
id: "profile:agent:invoke-fallback",
|
|
1204
|
-
kind: "agent",
|
|
1205
|
-
name: "invoke fallback",
|
|
1206
|
-
action: "invoke",
|
|
1207
|
-
startedAt: invokeFallback.startedAt,
|
|
1208
|
-
status: "completed",
|
|
1209
|
-
});
|
|
1210
|
-
}
|
|
1211
|
-
catch (error) {
|
|
1212
|
-
if (shouldProfile)
|
|
1213
|
-
yield finishProfileStep({
|
|
1214
|
-
id: "profile:agent:invoke-fallback",
|
|
1215
|
-
kind: "agent",
|
|
1216
|
-
name: "invoke fallback",
|
|
1217
|
-
action: "invoke",
|
|
1218
|
-
startedAt: invokeFallback.startedAt,
|
|
1219
|
-
status: "failed",
|
|
1220
|
-
error,
|
|
1221
|
-
});
|
|
1222
|
-
throw error;
|
|
1223
|
-
}
|
|
1224
|
-
const executedToolResults = Array.isArray(result.metadata?.executedToolResults)
|
|
1225
|
-
? result.metadata.executedToolResults
|
|
1226
|
-
: [];
|
|
1227
|
-
for (const chunk of projectLocalToolExecutionProfileChunks(executedToolResults, "local-tool:invoke-fallback")) {
|
|
1228
|
-
yield chunk;
|
|
1229
|
-
}
|
|
1230
|
-
const invokeExecutionEvidence = buildExecutionRecoveryEvidence({ projectionState, executedToolResults });
|
|
1231
|
-
if (hasUnresolvedExecution(invokeExecutionEvidence)) {
|
|
1232
|
-
throw createUnresolvedExecutionError(invokeExecutionEvidence);
|
|
1233
|
-
}
|
|
1234
|
-
const invokeFallbackRecoveryInstruction = result.output
|
|
1235
|
-
? resolveExecutionWithoutToolEvidenceTextInstruction(request, result.output, false, {
|
|
1236
|
-
...invokeExecutionEvidence,
|
|
1237
|
-
hasMissingDelegatedExecutionEvidence: hasMissingDelegatedExecutionEvidence(invokeExecutionEvidence),
|
|
1238
|
-
requiresPlan: requiresPlanEvidence(options.binding),
|
|
1239
|
-
})
|
|
1240
|
-
: resolveDelegatedExecutionRecoveryInstruction(invokeExecutionEvidence);
|
|
1241
|
-
const invokeFallbackMissingPlanRecoveryInstruction = !hasUnresolvedExecution(invokeExecutionEvidence) && !invokeFallbackRecoveryInstruction
|
|
1242
|
-
? resolveMissingPlanRecoveryInstruction({
|
|
1243
|
-
request,
|
|
1244
|
-
assistantText: typeof result.output === "string" ? result.output : "",
|
|
1245
|
-
requiresPlan: requiresPlanEvidence(options.binding),
|
|
1246
|
-
hasPlanStateEvidence: invokeExecutionEvidence.hasPlanStateEvidence,
|
|
1247
|
-
hasWriteTodosEvidence: invokeExecutionEvidence.hasPlanStateEvidence,
|
|
1248
|
-
hasToolResultEvidence: invokeExecutionEvidence.hasSuccessfulNonTodoToolResultEvidence,
|
|
1249
|
-
})
|
|
1250
|
-
: null;
|
|
1251
|
-
const invokeFallbackDelegationOnlyRecoveryInstruction = resolveDelegationOnlyRecoveryInstruction(options.binding, invokeExecutionEvidence);
|
|
1252
|
-
const invokeFallbackIncompletePlanRecoveryInstruction = requiresPlanEvidence(options.binding)
|
|
1253
|
-
&& invokeExecutionEvidence.hasIncompletePlanState
|
|
1254
|
-
&& invokeExecutionEvidence.hasSuccessfulNonTodoToolResultEvidence
|
|
1255
|
-
? CLOSE_REQUIRED_PLAN_RECOVERY_INSTRUCTION
|
|
1256
|
-
: null;
|
|
1257
|
-
const invokeFallbackPlanWithoutEvidenceRecoveryInstruction = requiresPlanEvidence(options.binding)
|
|
1258
|
-
&& invokeExecutionEvidence.hasPlanStateEvidence
|
|
1259
|
-
&& !invokeExecutionEvidence.hasSuccessfulNonTodoToolResultEvidence
|
|
1260
|
-
? buildRunEvidenceAfterPlanInstruction(options.primaryTools)
|
|
1261
|
-
: null;
|
|
1262
|
-
const effectiveInvokeFallbackRecoveryInstruction = invokeFallbackIncompletePlanRecoveryInstruction
|
|
1263
|
-
?? invokeFallbackPlanWithoutEvidenceRecoveryInstruction
|
|
1264
|
-
?? invokeFallbackMissingPlanRecoveryInstruction
|
|
1265
|
-
?? invokeFallbackDelegationOnlyRecoveryInstruction
|
|
1266
|
-
?? invokeFallbackRecoveryInstruction;
|
|
1267
|
-
if (effectiveInvokeFallbackRecoveryInstruction) {
|
|
1268
|
-
const invokeFallbackRuntimeOptions = effectiveInvokeFallbackRecoveryInstruction === invokeFallbackIncompletePlanRecoveryInstruction
|
|
1269
|
-
|| effectiveInvokeFallbackRecoveryInstruction === invokeFallbackPlanWithoutEvidenceRecoveryInstruction
|
|
1270
|
-
? {
|
|
1271
|
-
...withNonPlanningEvidenceToolPolicy(withSuppressedInitialRequiredPlanInstruction(options.runtimeOptions)),
|
|
1272
|
-
externalPlanEvidence: true,
|
|
1273
|
-
}
|
|
1274
|
-
: options.runtimeOptions;
|
|
1275
|
-
const recovered = await options.invoke(options.applyToolRecoveryInstruction(options.binding, effectiveInvokeFallbackRecoveryInstruction), options.input, options.sessionId, options.runtimeOptions.requestId ?? options.sessionId, undefined, options.history, invokeFallbackRuntimeOptions);
|
|
1276
|
-
const recoveredToolResults = Array.isArray(recovered.metadata?.executedToolResults)
|
|
1277
|
-
? recovered.metadata.executedToolResults
|
|
1278
|
-
: [];
|
|
1279
|
-
for (const chunk of projectLocalToolExecutionProfileChunks(recoveredToolResults, "local-tool:invoke-fallback-recovery")) {
|
|
1280
|
-
yield chunk;
|
|
1281
|
-
}
|
|
1282
|
-
const originalExecutionEvidence = buildExecutionRecoveryEvidence({ projectionState, executedToolResults });
|
|
1283
|
-
const recoveredExecutionEvidence = buildExecutionRecoveryEvidence({
|
|
1284
|
-
projectionState: createStreamEventProjectionState(),
|
|
1285
|
-
executedToolResults: recoveredToolResults,
|
|
1286
|
-
});
|
|
1287
|
-
if (hasParentLocalToolExecutionAfterDelegationFailure(originalExecutionEvidence, recoveredToolResults)) {
|
|
1288
|
-
throw new ExecutionReconciliationError("Agent attempted parent-local tool execution after delegated task failure; it must report a blocker or re-delegate with task.");
|
|
1289
|
-
}
|
|
1290
|
-
const recoveredVisibleOutput = recovered.output ? toVisibleContent(recovered.output) : "";
|
|
1291
|
-
const recoveredIsDelegationFailureFinalReport = isDelegationFailureFinalReport(originalExecutionEvidence, recoveredToolResults, recoveredVisibleOutput);
|
|
1292
|
-
const recoveredCarriesExecutionEvidence = recoveredExecutionEvidence.hasToolResultEvidence
|
|
1293
|
-
|| recoveredExecutionEvidence.hasOpenTaskDelegation
|
|
1294
|
-
|| recoveredExecutionEvidence.hasDelegatedExecutionToolEvidence;
|
|
1295
|
-
const recoveredHasUnresolvedExecution = !recoveredIsDelegationFailureFinalReport
|
|
1296
|
-
&& (hasUnresolvedExecution(recoveredExecutionEvidence)
|
|
1297
|
-
|| hasMissingDelegatedExecutionEvidence(recoveredExecutionEvidence)
|
|
1298
|
-
|| hasMissingDelegatedFindings(recoveredExecutionEvidence)
|
|
1299
|
-
|| (!recoveredCarriesExecutionEvidence
|
|
1300
|
-
&& (hasUnresolvedExecution(originalExecutionEvidence)
|
|
1301
|
-
|| hasMissingDelegatedExecutionEvidence(originalExecutionEvidence)
|
|
1302
|
-
|| hasMissingDelegatedFindings(originalExecutionEvidence))));
|
|
1303
|
-
const effectiveRecoveredEvidence = recoveredCarriesExecutionEvidence
|
|
1304
|
-
? recoveredExecutionEvidence
|
|
1305
|
-
: {
|
|
1306
|
-
...recoveredExecutionEvidence,
|
|
1307
|
-
hasIncompletePlanState: originalExecutionEvidence.hasIncompletePlanState,
|
|
1308
|
-
hasFailedPlanState: originalExecutionEvidence.hasFailedPlanState,
|
|
1309
|
-
hasFailedTaskDelegation: originalExecutionEvidence.hasFailedTaskDelegation,
|
|
1310
|
-
hasOpenTaskDelegation: originalExecutionEvidence.hasOpenTaskDelegation,
|
|
1311
|
-
hasDelegatedAgentWithConfiguredTools: originalExecutionEvidence.hasDelegatedAgentWithConfiguredTools,
|
|
1312
|
-
hasDelegatedExecutionToolEvidence: originalExecutionEvidence.hasDelegatedExecutionToolEvidence,
|
|
1313
|
-
};
|
|
1314
|
-
if (recoveredHasUnresolvedExecution
|
|
1315
|
-
|| (recoveredHasUnresolvedExecution && recoveredExecutionEvidence.hasToolResultEvidence && !recoveredVisibleOutput)) {
|
|
1316
|
-
throw createUnresolvedExecutionError(effectiveRecoveredEvidence);
|
|
1317
|
-
}
|
|
1318
|
-
for (const toolResult of recoveredToolResults) {
|
|
1319
|
-
yield {
|
|
1320
|
-
kind: "tool-result",
|
|
1321
|
-
toolName: toolResult.toolName,
|
|
1322
|
-
output: toolResult.output,
|
|
1323
|
-
isError: toolResult.isError,
|
|
1324
|
-
};
|
|
1325
|
-
}
|
|
1326
|
-
if (recovered.output) {
|
|
1327
|
-
const visible = toVisibleContent(recovered.output);
|
|
1328
|
-
if (visible) {
|
|
1329
|
-
yield { kind: "content", content: visible };
|
|
1330
|
-
}
|
|
1331
|
-
}
|
|
1332
|
-
return;
|
|
1333
|
-
}
|
|
1334
|
-
if (hasMissingDelegatedExecutionEvidence(invokeExecutionEvidence)) {
|
|
1335
|
-
throw createUnresolvedExecutionError(invokeExecutionEvidence);
|
|
1336
|
-
}
|
|
1337
|
-
if (hasMissingDelegatedFindings(invokeExecutionEvidence)) {
|
|
1338
|
-
throw createUnresolvedExecutionError(invokeExecutionEvidence);
|
|
1339
|
-
}
|
|
1340
|
-
for (const toolResult of executedToolResults) {
|
|
1341
|
-
yield {
|
|
1342
|
-
kind: "tool-result",
|
|
1343
|
-
toolName: toolResult.toolName,
|
|
1344
|
-
output: toolResult.output,
|
|
1345
|
-
isError: toolResult.isError,
|
|
1346
|
-
};
|
|
1347
|
-
}
|
|
1348
|
-
if (result.output) {
|
|
1349
|
-
const visible = toVisibleContent(result.output);
|
|
1350
|
-
if (visible) {
|
|
1351
|
-
yield { kind: "content", content: visible };
|
|
1352
|
-
}
|
|
1353
|
-
}
|
|
1354
|
-
}
|
|
1355
|
-
catch (error) {
|
|
1356
|
-
if (!emittedUnsafeStreamSideEffects && options.isRetryableProviderError?.(options.binding, error)) {
|
|
1357
|
-
const recovered = await options.invoke(options.binding, options.input, options.sessionId, options.runtimeOptions.requestId ?? options.sessionId, undefined, options.history, options.runtimeOptions);
|
|
1358
|
-
const executedToolResults = Array.isArray(recovered.metadata?.executedToolResults)
|
|
1359
|
-
? recovered.metadata.executedToolResults
|
|
1360
|
-
: [];
|
|
1361
|
-
for (const toolResult of executedToolResults) {
|
|
1362
|
-
yield {
|
|
1363
|
-
kind: "tool-result",
|
|
1364
|
-
toolName: toolResult.toolName,
|
|
1365
|
-
output: toolResult.output,
|
|
1366
|
-
isError: toolResult.isError,
|
|
1367
|
-
};
|
|
1368
|
-
}
|
|
1369
|
-
if (recovered.output) {
|
|
1370
|
-
const visible = toVisibleContent(recovered.output);
|
|
1371
|
-
if (visible)
|
|
1372
|
-
yield { kind: "content", content: visible };
|
|
1373
|
-
}
|
|
1374
|
-
return;
|
|
1375
|
-
}
|
|
1376
|
-
if (options.countConfiguredTools(options.binding) > 0 &&
|
|
1377
|
-
error instanceof Error &&
|
|
1378
|
-
error.message.includes("does not support tool binding")) {
|
|
1379
|
-
throw error;
|
|
1380
|
-
}
|
|
1381
|
-
if (!isToolCallRecoveryFailure(error)) {
|
|
1382
|
-
throw error;
|
|
1383
|
-
}
|
|
1384
|
-
const recoveryInstruction = resolveToolCallRecoveryInstruction(error);
|
|
1385
|
-
if (!recoveryInstruction) {
|
|
1386
|
-
throw error;
|
|
1387
|
-
}
|
|
1388
|
-
const retried = await options.invoke(options.applyToolRecoveryInstruction(options.binding, recoveryInstruction), options.input, options.sessionId, options.runtimeOptions.requestId ?? options.sessionId, undefined, options.history, options.runtimeOptions);
|
|
1389
|
-
if (retried.output) {
|
|
1390
|
-
const visible = toVisibleContent(retried.output);
|
|
1391
|
-
if (visible)
|
|
1392
|
-
yield { kind: "content", content: visible };
|
|
1393
|
-
}
|
|
1394
|
-
}
|
|
1395
|
-
}
|
|
1
|
+
import{extractVisibleOutput as Ge,isToolCallRecoveryFailure as Je,isRetrySafeInvalidToolSelectionError as ie,appendToolRecoveryInstruction as Ke,resolveMissingPlanRecoveryInstruction as se,resolveExecutionWithoutToolEvidenceTextInstruction as be,shouldValidateExecutionWithoutToolEvidence as Qe,resolveToolCallRecoveryInstruction as Ie,sanitizeVisibleText as Ze,EXECUTION_WITH_TOOL_EVIDENCE_RETRY_INSTRUCTION as Se,INVALID_TOOL_SELECTION_RECOVERY_INSTRUCTION as Xe}from"../../parsing/output-parsing.js";import{DELEGATED_TASK_FAILURE_RECOVERY_INSTRUCTION as et,DELEGATION_ONLY_RECOVERY_INSTRUCTION as tt,REQUIRED_EXECUTION_CONTINUATION_INSTRUCTION as nt}from"../../prompts/runtime-prompts.js";import{buildInvocationRequest as ot}from"../model/invocation-request.js";import{PROMPTED_JSON_TOOL_POLICY_KEY as at,withPromptedJsonToolPolicy as Oe}from"../model/prompted-json-tool-policy.js";import{buildRawModelMessages as rt}from"../model/message-assembly.js";import{projectRuntimeStreamEvent as it,createStreamEventProjectionState as ee}from"../stream-event-projection.js";import{projectTextStreamChunks as Pe}from"../stream-text-consumption.js";import{computeRemainingTimeoutMs as le}from"../resilience.js";import{stringifyToolOutput as st}from"../tool/tool-arguments.js";import{UPSTREAM_REQUEST_CONFIG_KEY as lt,UPSTREAM_SESSION_CONFIG_KEY as ut}from"../upstream-configurable-keys.js";class ue extends Error{constructor(t){super(t),this.name="ExecutionReconciliationError"}}const De=["The current required todo board still has unfinished work.","Do not broaden the task, restart planning, or ask the user what to do next.","Use the existing tool evidence already available in this run.","Your next action must be write_todos: update every remaining pending or in_progress item to completed if evidence was gathered, or failed if it cannot be completed with the available tools.","After that write_todos call, provide the final answer required by the agent response format."].join(`
|
|
2
|
+
`),xe=["The required todo board was closed before any non-TODO evidence tool returned.","Do not call write_todos again yet.","Your next action must be exactly one non-TODO evidence tool call selected from the available tool descriptions and schemas.","Honor any structured workspace or runtime evidence-tool contract already supplied in this turn.","Do not choose tools by matching free-form request or TODO text.","After that evidence tool returns, update the todo board and then provide the final answer required by the agent response format."].join(`
|
|
3
|
+
`);function Ae(e){return typeof e.name=="string"?e.name.trim():""}function J(e){const t=e.map(Ae).filter(n=>n.length>0&&!I(n));return t.length===0?xe:[xe,"",`Available non-planning tool names: ${t.join(", ")}.`].join(`
|
|
4
|
+
`)}const we=["This agent has a required visible planning contract.","Your first action for this request must be write_todos with concrete task steps and statuses.","Do not call any domain/evidence tool and do not provide a final answer before the initial write_todos call succeeds.","After write_todos succeeds, do not call write_todos or read_todos again until one non-planning evidence tool returns.","After each evidence step, update the todo board. Before the final answer, close every todo as completed or failed."].join(`
|
|
5
|
+
`);function dt(e){const t=e.map(Ae).filter(n=>n.length>0&&!I(n));return t.length===0?we:[we,"",`After the initial todo board, select the next non-planning tool from these declared tool names: ${t.join(", ")}.`].join(`
|
|
6
|
+
`)}function C(e){const t=Ge(e);return t?Ze(t):""}function ct(e){if(typeof e!="object"||e===null)return"";const t=e,n=typeof t.event=="string"?t.event:"";return n!=="on_chat_model_end"&&n!=="on_chain_end"?"":C(t.data?.output)}function Ce(e){for(const t of[...e].reverse()){if(typeof t.output!="object"||t.output===null)continue;const n=t.output.summary;if(typeof n!="object"||n===null)continue;const o=n.summary;if(typeof o!="object"||o===null)continue;const c=o,s=typeof c.pending=="number"?c.pending:0,_=typeof c.inProgress=="number"?c.inProgress:0;return s>0||_>0}return!1}function mt(e){return Array.isArray(e)?e.some(t=>typeof t=="object"&&t!==null&&typeof t.status=="string"&&["pending","in_progress"].includes(t.status.trim().toLowerCase())):null}function Ne(e){if(typeof e!="object"||e===null)return null;const t=e;if(typeof t.pending=="number"||typeof t.inProgress=="number")return(typeof t.pending=="number"?t.pending:0)>0||(typeof t.inProgress=="number"?t.inProgress:0)>0;const n=mt(t.todos);if(n!==null)return n;for(const o of[t.summary,t.update,t.data,t.output]){const c=Ne(o);if(c!==null)return c}return null}function V(e){if(Array.isArray(e))return e.some(n=>V(n));if(typeof e!="object"||e===null)return!1;const t=e;return typeof t.status=="string"&&t.status.trim().toLowerCase()==="failed"?!0:V(t.todos)||V(t.update)||V(t.data)||V(t.output)||V(t.summary)}function ft(e){return e.some(t=>I(t.toolName)&&V(t.output))}function gt(e){return typeof e=="string"?e.trim().toLowerCase().replace(/[\s-]+/gu,"_"):""}function I(e){const t=gt(e);return t==="write_todos"||t==="read_todos"||t==="tool_call_write_todos"||t==="tool_call_read_todos"||t==="call_write_todos"||t==="call_read_todos"}function _e(e){return e.kind!=="tool-result"||!I(e.toolName)?!1:Ne(e.output)===!1}function ht(e){return e.some(t=>!I(t.toolName))}function Fe(e){return e.some(t=>t.isError!==!0&&!I(t.toolName))}function Tt(e){return e.some(t=>t.isError!==!0&&t.toolName==="task")}function g(e){return e.harnessRuntime?.executionContract?.requiresPlan===!0}function de(e){return{...e,suppressInitialRequiredPlanInstruction:!0}}function ce(e){return{...e,state:{...typeof e.state=="object"&&e.state!==null?e.state:{},[at]:"nonPlanningEvidence"}}}function qe(e,t){return e.hasFailedTaskDelegation&&t.some(n=>n.toolName!=="task")}function Ue(e,t,n){return e.hasFailedTaskDelegation&&t.length===0&&n.trim().length>0}function N(e){const{projectionState:t,executedToolResults:n=[]}=e;return{hasToolResultEvidence:n.length>0||t.emittedToolResult||t.emittedToolError,hasSuccessfulToolResultEvidence:n.some(o=>o.isError!==!0)||t.emittedSuccessfulToolResult,hasNonTodoToolResultEvidence:ht(n)||t.emittedNonTodoToolResult||t.emittedToolError,hasSuccessfulNonTodoToolResultEvidence:Fe(n)||t.emittedSuccessfulNonTodoToolResult,hasIncompletePlanState:t.hasIncompletePlanState||Ce(n),hasFailedPlanState:(t.hasFailedPlanState||ft(n))&&!t.emittedSuccessfulNonTodoToolResult&&!Fe(n),hasPlanStateEvidence:t.sawPlanState||Ce(n),hasOpenTaskDelegation:t.openTaskDelegations>0,hasFailedTaskDelegation:t.hasFailedTaskDelegation||n.some(o=>o.toolName==="task"&&o.isError===!0),hasDelegatedAgentWithConfiguredTools:t.sawDelegatedAgentWithConfiguredTools,hasDelegatedExecutionToolEvidence:t.emittedDelegatedExecutionToolResult,hasOnlyPlaceholderTaskCompletion:t.emittedSuccessfulTaskResult&&t.emittedPlaceholderTaskResult&&!t.emittedDelegatedTerminalOutput,hasSuccessfulTaskToolEvidence:t.emittedSuccessfulTaskResult||Tt(n)}}function D(e){return e.hasIncompletePlanState||e.hasFailedPlanState||e.hasFailedTaskDelegation||e.hasOpenTaskDelegation}function O(e){return!e.hasDelegatedAgentWithConfiguredTools||e.hasOpenTaskDelegation||e.hasFailedTaskDelegation?!1:!e.hasDelegatedExecutionToolEvidence&&!e.hasSuccessfulTaskToolEvidence}function P(e){return e.hasDelegatedAgentWithConfiguredTools&&e.hasOnlyPlaceholderTaskCompletion}function me(e){return e.hasPlanStateEvidence&&!e.hasIncompletePlanState&&e.hasSuccessfulNonTodoToolResultEvidence&&!e.hasOpenTaskDelegation&&!e.hasFailedTaskDelegation&&!O(e)&&!P(e)}function te(e){const t=e.filter(n=>n.isError!==!0&&!I(n.toolName)).map(n=>{const c=C(n.output)||st(n.output).trim(),s=c.length>4e3?`${c.slice(0,4e3)}
|
|
7
|
+
... [truncated]`:c;return`## ${n.toolName}
|
|
8
|
+
${s}`});return["Status: completed","","Summary:","- Completed the required TODO burn down after collecting tool evidence.","- Returning an evidence-grounded deterministic summary because the streamed model did not produce a clean final synthesis before the required plan completed.","","Evidence:",t.length>0?t.join(`
|
|
9
|
+
|
|
10
|
+
`):"(no non-planning tool evidence captured)"].join(`
|
|
11
|
+
`)}function Et(e){const t=[...e].reverse().find(o=>o.isError===!0);if(!t)return null;const n=typeof t.output=="string"?t.output:JSON.stringify(t.output);return Ie(new Error(n))??nt}function ne(e){const t=e.trim();if(t.length<80||/^(?:model_request|tool_call|call_tool)/iu.test(t)||/^(?:name|tool_call_id)\s*=/iu.test(t)||/^(?:we\s+need\s+to|so\s+next\s+step\b)/iu.test(t)||/^\{\s*"(?:name|arguments|args|argv|todos|symbol|query|market|count|stdout|stderr|exitCode)"\s*:/iu.test(t)||/^(?:stdout|stderr|exitCode)\s*:/iu.test(t)||/\b(?:must|need|needs|should|will)\s+(?:now\s+)?call\s+[A-Za-z_][A-Za-z0-9_]*\b/iu.test(t)||/\b(?:let'?s\s+try|we\s+can(?:not|'t)?\s+(?:run|call|use|try)|cannot\s+.+\btry\b|can\s+run\s+[A-Za-z_][A-Za-z0-9_]*)/iu.test(t))return!1;try{const n=JSON.parse(t);if(typeof n=="object"&&n!==null)return!1}catch{}return!0}function vt(e){const t=e.execution?.params??e.deepAgentParams??e.langchainAgentParams;return{tools:Array.isArray(t?.tools)?t.tools:[],subagents:Array.isArray(t?.subagents)?t.subagents??[]:[]}}function fe(e){const{tools:t,subagents:n}=vt(e),o=e.agent,c=[...n,...o?.subagentRefs??[],...o?.subagentPathRefs??[],...o?.asyncSubagents??[]],s=[...t,...o?.toolRefs??[],...o?.toolBindings??[]],_=o?.skillPathRefs??[];return c.length>0&&s.length===0&&_.length===0}function je(e,t){return(e.execution?.params??e.deepAgentParams??e.langchainAgentParams)?.model?.provider!=="openai-compatible"?!1:(t instanceof Error?t.message:String(t)).toLowerCase().includes("received empty response from chat model call")}function Me(e){const t=typeof e=="object"&&e!==null&&"lc_error_code"in e?String(e.lc_error_code??""):"",n=e instanceof Error?e.message:String(e);return t==="GRAPH_RECURSION_LIMIT"||/Recursion limit .* without hitting a stop condition|GRAPH_RECURSION_LIMIT/i.test(n)}function yt(e){return e.hasSuccessfulTaskToolEvidence||e.hasOpenTaskDelegation||e.hasFailedTaskDelegation||e.hasDelegatedAgentWithConfiguredTools||e.hasDelegatedExecutionToolEvidence}function ge(e,t){return!fe(e)||yt(t)?null:tt}function pt(e){return e.trim().startsWith("runtime_error=")}function kt(e,t){return pt(e)?t.hasToolResultEvidence||t.hasPlanStateEvidence||t.hasOpenTaskDelegation||t.hasDelegatedExecutionToolEvidence?null:Se:null}function Le(e){return e.hasFailedTaskDelegation?et:P(e)||e.hasOpenTaskDelegation&&e.hasDelegatedAgentWithConfiguredTools&&!e.hasPlanStateEvidence&&!e.hasFailedTaskDelegation?Se:null}function q(e){const t=[];e.hasIncompletePlanState&&t.push("plan state still has unfinished work"),e.hasFailedPlanState&&t.push("plan state failed before non-TODO evidence returned"),e.hasFailedTaskDelegation&&t.push("delegated task failed before surfacing final findings"),e.hasOpenTaskDelegation&&t.push("delegated task has not finished"),P(e)&&t.push("delegated task returned only the upstream placeholder result without surfaced final findings");const n=t.length>0?`: ${t.join("; ")}`:"";return new ue(`Agent ended before execution was fully reconciled${n}.`)}function We(e,t,n,o,c,s){return{kind:"profile",step:{id:e,kind:t,name:n,action:o,status:c,...s?{detail:s}:{}}}}function j(e){const t=new Date().toISOString();return{startedAt:t,chunk:We(e.id,e.kind,e.name,e.action,"started",{...e.detail??{},startedAt:t})}}function p(e){const t=new Date().toISOString();return We(e.id,e.kind,e.name,e.action,e.status,{...e.detail??{},startedAt:e.startedAt,endedAt:t,durationMs:Math.max(0,new Date(t).getTime()-new Date(e.startedAt).getTime()),...e.error!==void 0?{error:e.error instanceof Error?e.error.message:String(e.error)}:{}})}function Ve(e,t){const n=[];return e.forEach((o,c)=>{if(I(o.toolName))return;const s=`${t}:${c+1}:${o.toolName}`,_=new Date().toISOString();n.push({kind:"profile",step:{id:s,kind:"tool",name:o.toolName,action:"invoke",status:"started",startedAt:_}}),n.push({kind:"profile",step:{id:s,kind:"tool",name:o.toolName,action:"invoke",status:o.isError===!0?"failed":"completed",startedAt:_,endedAt:new Date().toISOString(),...o.isError===!0?{isError:!0}:{}}})}),n}async function*Ct(e){let t=ot(e.binding,e.history,e.input,e.runtimeOptions);g(e.binding)&&e.runtimeOptions.suppressInitialRequiredPlanInstruction!==!0&&(t=Oe(Ke(t,dt(e.primaryTools)),"planning"));let n=!1;const o=e.runtimeOptions.profiling===!0,c=Qe(t),s=[];let _=!1;const d=ee();e.runtimeOptions.externalPlanEvidence===!0&&(d.sawPlanState=!0,yield{kind:"commentary",content:`${e.binding.agent.id}: TODO evidence observed.`,agentId:e.binding.agent.id});const he=e.runtimeOptions.requestId??e.sessionId,Te=f=>({...e.resolveInvocationConfig?e.resolveInvocationConfig(e.binding,{sessionId:e.sessionId,requestId:he,...e.runtimeOptions.context?{context:e.runtimeOptions.context}:{},...e.runtimeOptions.toolRuntimeContext?{toolRuntimeContext:e.runtimeOptions.toolRuntimeContext}:{}}):{configurable:{[ut]:e.sessionId,[lt]:he},...e.runtimeOptions.context?{context:e.runtimeOptions.context}:{},...e.runtimeOptions.toolRuntimeContext?{toolRuntimeContext:e.runtimeOptions.toolRuntimeContext}:{}},...f??{}}),Ee=()=>c&&!n,M=async function*(){for(;s.length>0;){const f=s.shift();if(f.kind==="content"){f.content&&(yield{kind:"content",content:f.content});continue}yield f}};try{if(e.isLangChainBinding(e.binding)&&e.canUseDirectModelStream&&e.langChainStreamModel?.stream){const l=j({id:"profile:agent:model-stream-start",kind:"agent",name:"model stream",action:"start"});o&&(yield l.chunk);let y;try{const i=rt(e.binding,e.getSystemPrompt(e.binding),e.history,e.input,e.runtimeOptions.memoryContext),L=g(e.binding)&&e.runtimeOptions.suppressInitialRequiredPlanInstruction!==!0?Oe(i,"planning"):i;y=await e.withTimeout(()=>e.langChainStreamModel.stream(L),le(e.streamDeadlineAt,e.invokeTimeoutMs),"model stream start","stream"),o&&(yield p({id:"profile:agent:model-stream-start",kind:"agent",name:"model stream",action:"start",startedAt:l.startedAt,status:"completed"}))}catch(i){throw o&&(yield p({id:"profile:agent:model-stream-start",kind:"agent",name:"model stream",action:"start",startedAt:l.startedAt,status:"failed",error:i})),i}let h=!1;const r=Pe(e.iterateWithTimeout(y,e.streamIdleTimeoutMs,"model stream",e.streamDeadlineAt,e.invokeTimeoutMs)),m=j({id:"profile:agent:model-stream-consume",kind:"agent",name:"model stream",action:"consume"});o&&(yield m.chunk);try{let i=await r.next();for(;!i.done;)i.value.kind==="content"&&(h=!0,n=!0),yield i.value,i=await r.next();if(o&&(yield p({id:"profile:agent:model-stream-consume",kind:"agent",name:"model stream",action:"consume",startedAt:m.startedAt,status:"completed"})),i.value.emittedContent||h)return}catch(i){throw o&&(yield p({id:"profile:agent:model-stream-consume",kind:"agent",name:"model stream",action:"consume",startedAt:m.startedAt,status:"failed",error:i})),i}}const f=j({id:"profile:agent:create-runnable",kind:"agent",name:"create runnable",action:"startup"});o&&(yield f.chunk);let U;try{U=await e.createRunnable(),o&&(yield p({id:"profile:agent:create-runnable",kind:"agent",name:"create runnable",action:"startup",startedAt:f.startedAt,status:"completed"}))}catch(l){throw o&&(yield p({id:"profile:agent:create-runnable",kind:"agent",name:"create runnable",action:"startup",startedAt:f.startedAt,status:"failed",error:l})),l}if(!e.forceInvokeFallback&&!fe(e.binding)&&typeof U.streamEvents=="function"){const l=j({id:"profile:agent:stream-events-start",kind:"agent",name:"streamEvents",action:"start"});o&&(yield l.chunk);let y;try{y=await e.withTimeout(()=>U.streamEvents(t,Te({version:"v2"})),le(e.streamDeadlineAt,e.invokeTimeoutMs),"agent streamEvents start","stream"),o&&(yield p({id:"profile:agent:stream-events-start",kind:"agent",name:"streamEvents",action:"start",startedAt:l.startedAt,status:"completed"}))}catch(A){if(o&&(yield p({id:"profile:agent:stream-events-start",kind:"agent",name:"streamEvents",action:"start",startedAt:l.startedAt,status:"failed",error:A})),!n&&(je(e.binding,A)||Me(A)))s.length=0;else throw A}const h=[];if(y){const A=j({id:"profile:agent:stream-events-consume",kind:"agent",name:"streamEvents",action:"consume"});o&&(yield A.chunk);try{let k=!1,F=!1,v=null,S=!1,Z=0;for await(const z of e.iterateWithTimeout(y,e.streamIdleTimeoutMs,"agent streamEvents",e.streamDeadlineAt,e.invokeTimeoutMs)){const R=it({event:z,allowVisibleStreamDeltas:!0,includeStateStreamOutput:e.isDeepAgentBinding(e.binding),rootAgentId:typeof e.binding.agent?.id=="string"?e.binding.agent.id:void 0,countConfiguredToolsForAgentId:e.countConfiguredToolsForAgentId,toolNameMapping:e.toolNameMapping,primaryTools:e.primaryTools,state:d}),W=R.some(a=>a.kind==="tool-result"&&!I(a.toolName)&&!(a.isError===!0&&ie(a.output))),b=R.some(a=>a.kind!=="upstream-event"&&a.kind!=="content"&&!(a.kind==="tool-result"&&I(a.toolName))&&!(a.kind==="tool-result"&&a.isError===!0)&&!(a.kind==="tool-result"&&a.isError===!0&&ie(a.output))),T=Z>0;if(g(e.binding)&&!F&&T&&R.some(a=>a.kind==="tool-result"&&I(a.toolName))){v=J(e.primaryTools),S=!0;break}for(const a of R){a.kind==="tool-result"&&a.isError===!0&&ie(a.output)&&(_=!0),a.kind==="tool-result"&&h.push({toolName:a.toolName,output:a.output,isError:a.isError}),a.kind==="tool-result"&&!I(a.toolName)&&a.isError!==!0&&(F=!0),_e(a)&&(k=!0),a.kind==="tool-result"&&I(a.toolName)&&(Z+=1),(W||b)&&s.length>0&&(yield*M()),(W||b)&&(n=!0);const G=g(e.binding)&&d.sawPlanState;if(a.kind==="content"&&(Ee()||d.hasFailedTaskDelegation||G)){s.push(a);continue}yield a}const ke=R.some(a=>a.kind==="tool-result"&&I(a.toolName)&&a.isError!==!0);if(g(e.binding)&&ke&&(T||R.some(a=>_e(a)))&&!F){v=J(e.primaryTools),S=!0;break}if(g(e.binding)&&k&&F){if(ne(d.emittedOutput)){s.length>0&&(yield*M());return}s.length=0,yield{kind:"content",content:te(h)};return}const w=N({projectionState:d});if(g(e.binding)&&me(w)){if(ne(d.emittedOutput)){s.length>0&&(yield*M());return}s.length=0,yield{kind:"content",content:te(h)};return}const Re=ct(z);if(Re){const a=N({projectionState:d}),G=!a.hasDelegatedAgentWithConfiguredTools&&!a.hasOpenTaskDelegation&&!d.emittedSuccessfulTaskResult?se({request:t,assistantText:Re,requiresPlan:g(e.binding),hasPlanStateEvidence:a.hasPlanStateEvidence,hasWriteTodosEvidence:a.hasPlanStateEvidence,hasToolResultEvidence:a.hasSuccessfulNonTodoToolResultEvidence}):null,re=ge(e.binding,a),X=g(e.binding)&&a.hasPlanStateEvidence&&!a.hasSuccessfulNonTodoToolResultEvidence?J(e.primaryTools):null;if(!n&&(G||X||re)){v=G??X??re,S=X!==null;break}if(g(e.binding)&&me(a)){if(ne(d.emittedOutput)){s.length>0&&(yield*M());return}s.length=0,yield{kind:"content",content:te(h)};return}if(!Ee()&&!a.hasIncompletePlanState&&!a.hasFailedTaskDelegation&&!a.hasOpenTaskDelegation&&!O(a)&&!P(a)&&!G&&!X&&!re){s.length>0&&(yield*M());return}}}if(v){const z=S?{...ce(de(e.runtimeOptions)),externalPlanEvidence:!0}:e.runtimeOptions,R=await e.invoke(e.applyToolRecoveryInstruction(e.binding,v),e.input,e.sessionId,e.runtimeOptions.requestId??e.sessionId,void 0,e.history,z),W=Array.isArray(R.metadata?.executedToolResults)?R.metadata.executedToolResults:[],b=N({projectionState:d}),T=N({projectionState:ee(),executedToolResults:W}),ae=T.hasSuccessfulNonTodoToolResultEvidence||T.hasOpenTaskDelegation||T.hasDelegatedExecutionToolEvidence;if(D(T)||O(T)||P(T)||!ae&&(D(b)||O(b)||P(b))){const w=ae?T:{...T,hasIncompletePlanState:b.hasIncompletePlanState,hasFailedPlanState:b.hasFailedPlanState,hasFailedTaskDelegation:b.hasFailedTaskDelegation,hasOpenTaskDelegation:b.hasOpenTaskDelegation,hasDelegatedAgentWithConfiguredTools:b.hasDelegatedAgentWithConfiguredTools,hasDelegatedExecutionToolEvidence:b.hasDelegatedExecutionToolEvidence};throw q(w)}for(const w of W)yield{kind:"tool-result",toolName:w.toolName,output:w.output,isError:w.isError};if(R.output){const w=C(R.output);w&&(yield{kind:"content",content:w})}return}o&&(yield p({id:"profile:agent:stream-events-consume",kind:"agent",name:"streamEvents",action:"consume",startedAt:A.startedAt,status:"completed"}))}catch(k){if(o&&(yield p({id:"profile:agent:stream-events-consume",kind:"agent",name:"streamEvents",action:"consume",startedAt:A.startedAt,status:"failed",error:k})),!n&&(je(e.binding,k)||Me(k)))s.length=0;else throw k}}const r=N({projectionState:d});if(g(e.binding)&&me(r)){if(ne(d.emittedOutput)){s.length>0&&(yield*M());return}s.length=0,yield{kind:"content",content:te(h)};return}const m=Le(r),i=ge(e.binding,r),L=g(e.binding)&&r.hasIncompletePlanState&&r.hasSuccessfulNonTodoToolResultEvidence?De:null,B=g(e.binding)&&r.hasPlanStateEvidence&&!r.hasSuccessfulNonTodoToolResultEvidence?J(e.primaryTools):null,H=!n||m?m:null;if(D(r)&&!H&&!L&&!B)throw q(r);const oe=d.emittedOutput?be(t,d.emittedOutput,!1,{...r,hasMissingDelegatedExecutionEvidence:O(r),requiresPlan:g(e.binding)}):null,x=d.emittedOutput?kt(d.emittedOutput,r):null,Be=n?null:Et(h),He=!D(r)&&!H?se({request:t,assistantText:d.emittedOutput,requiresPlan:g(e.binding),hasPlanStateEvidence:r.hasPlanStateEvidence,hasWriteTodosEvidence:r.hasPlanStateEvidence,hasToolResultEvidence:r.hasSuccessfulNonTodoToolResultEvidence}):null,Q=!n&&_?Xe:Be??H??L??B??x??He??i??oe;if(Q){const A=Q===L||Q===B?ce(de(e.runtimeOptions)):e.runtimeOptions;let k;k=await e.invoke(e.applyToolRecoveryInstruction(e.binding,Q),e.input,e.sessionId,e.runtimeOptions.requestId??e.sessionId,void 0,e.history,A);const F=Array.isArray(k.metadata?.executedToolResults)?k.metadata.executedToolResults:[],v=N({projectionState:d}),S=N({projectionState:ee(),executedToolResults:F});if(qe(v,F))throw new ue("Agent attempted parent-local tool execution after delegated task failure; it must report a blocker or re-delegate with task.");const Z=k.output?C(k.output):"",z=Ue(v,F,Z),R=S.hasSuccessfulNonTodoToolResultEvidence||S.hasOpenTaskDelegation||S.hasDelegatedExecutionToolEvidence,W=!z&&(D(S)||O(S)||P(S)||!R&&(D(v)||O(v)||P(v))),b=R?S:{...S,hasIncompletePlanState:v.hasIncompletePlanState,hasFailedPlanState:v.hasFailedPlanState,hasFailedTaskDelegation:v.hasFailedTaskDelegation,hasOpenTaskDelegation:v.hasOpenTaskDelegation,hasDelegatedAgentWithConfiguredTools:v.hasDelegatedAgentWithConfiguredTools,hasDelegatedExecutionToolEvidence:v.hasDelegatedExecutionToolEvidence};if(W)throw q(b);for(const T of F)yield{kind:"tool-result",toolName:T.toolName,output:T.output,isError:T.isError};if(k.output){const T=C(k.output);T&&(yield{kind:"content",content:T})}return}if(s.length>0&&(yield*M()),O(r)||P(r))throw q(r);const ze=D(r);if(d.emittedOutput||(d.emittedToolResult||d.emittedToolError)&&!ze)return}if(!e.forceInvokeFallback&&e.isLangChainBinding(e.binding)&&!fe(e.binding)&&typeof U.stream=="function"){const l=j({id:"profile:agent:stream-start",kind:"agent",name:"stream",action:"start"});o&&(yield l.chunk);let y;try{y=await e.withTimeout(()=>U.stream(t,Te()),le(e.streamDeadlineAt,e.invokeTimeoutMs),"agent stream start","stream"),o&&(yield p({id:"profile:agent:stream-start",kind:"agent",name:"stream",action:"start",startedAt:l.startedAt,status:"completed"}))}catch(i){throw o&&(yield p({id:"profile:agent:stream-start",kind:"agent",name:"stream",action:"start",startedAt:l.startedAt,status:"failed",error:i})),i}let h=!1;const r=Pe(e.iterateWithTimeout(y,e.streamIdleTimeoutMs,"agent stream",e.streamDeadlineAt,e.invokeTimeoutMs)),m=j({id:"profile:agent:stream-consume",kind:"agent",name:"stream",action:"consume"});o&&(yield m.chunk);try{let i=await r.next();for(;!i.done;)i.value.kind==="content"&&(h=!0,n=!0),yield i.value,i=await r.next();if(o&&(yield p({id:"profile:agent:stream-consume",kind:"agent",name:"stream",action:"consume",startedAt:m.startedAt,status:"completed"})),i.value.emittedContent||h)return}catch(i){throw o&&(yield p({id:"profile:agent:stream-consume",kind:"agent",name:"stream",action:"consume",startedAt:m.startedAt,status:"failed",error:i})),i}}const Y=j({id:"profile:agent:invoke-fallback",kind:"agent",name:"invoke fallback",action:"invoke"});o&&(yield Y.chunk);let E;try{E=await e.invoke(e.binding,e.input,e.sessionId,e.runtimeOptions.requestId??e.sessionId,void 0,e.history,e.runtimeOptions),o&&(yield p({id:"profile:agent:invoke-fallback",kind:"agent",name:"invoke fallback",action:"invoke",startedAt:Y.startedAt,status:"completed"}))}catch(l){throw o&&(yield p({id:"profile:agent:invoke-fallback",kind:"agent",name:"invoke fallback",action:"invoke",startedAt:Y.startedAt,status:"failed",error:l})),l}const $=Array.isArray(E.metadata?.executedToolResults)?E.metadata.executedToolResults:[];for(const l of Ve($,"local-tool:invoke-fallback"))yield l;const u=N({projectionState:d,executedToolResults:$});if(D(u))throw q(u);const ve=E.output?be(t,E.output,!1,{...u,hasMissingDelegatedExecutionEvidence:O(u),requiresPlan:g(e.binding)}):Le(u),Ye=!D(u)&&!ve?se({request:t,assistantText:typeof E.output=="string"?E.output:"",requiresPlan:g(e.binding),hasPlanStateEvidence:u.hasPlanStateEvidence,hasWriteTodosEvidence:u.hasPlanStateEvidence,hasToolResultEvidence:u.hasSuccessfulNonTodoToolResultEvidence}):null,$e=ge(e.binding,u),ye=g(e.binding)&&u.hasIncompletePlanState&&u.hasSuccessfulNonTodoToolResultEvidence?De:null,pe=g(e.binding)&&u.hasPlanStateEvidence&&!u.hasSuccessfulNonTodoToolResultEvidence?J(e.primaryTools):null,K=ye??pe??Ye??$e??ve;if(K){const l=K===ye||K===pe?{...ce(de(e.runtimeOptions)),externalPlanEvidence:!0}:e.runtimeOptions,y=await e.invoke(e.applyToolRecoveryInstruction(e.binding,K),e.input,e.sessionId,e.runtimeOptions.requestId??e.sessionId,void 0,e.history,l),h=Array.isArray(y.metadata?.executedToolResults)?y.metadata.executedToolResults:[];for(const x of Ve(h,"local-tool:invoke-fallback-recovery"))yield x;const r=N({projectionState:d,executedToolResults:$}),m=N({projectionState:ee(),executedToolResults:h});if(qe(r,h))throw new ue("Agent attempted parent-local tool execution after delegated task failure; it must report a blocker or re-delegate with task.");const i=y.output?C(y.output):"",L=Ue(r,h,i),B=m.hasToolResultEvidence||m.hasOpenTaskDelegation||m.hasDelegatedExecutionToolEvidence,H=!L&&(D(m)||O(m)||P(m)||!B&&(D(r)||O(r)||P(r))),oe=B?m:{...m,hasIncompletePlanState:r.hasIncompletePlanState,hasFailedPlanState:r.hasFailedPlanState,hasFailedTaskDelegation:r.hasFailedTaskDelegation,hasOpenTaskDelegation:r.hasOpenTaskDelegation,hasDelegatedAgentWithConfiguredTools:r.hasDelegatedAgentWithConfiguredTools,hasDelegatedExecutionToolEvidence:r.hasDelegatedExecutionToolEvidence};if(H||H&&m.hasToolResultEvidence&&!i)throw q(oe);for(const x of h)yield{kind:"tool-result",toolName:x.toolName,output:x.output,isError:x.isError};if(y.output){const x=C(y.output);x&&(yield{kind:"content",content:x})}return}if(O(u)||P(u))throw q(u);for(const l of $)yield{kind:"tool-result",toolName:l.toolName,output:l.output,isError:l.isError};if(E.output){const l=C(E.output);l&&(yield{kind:"content",content:l})}}catch(f){if(!n&&e.isRetryableProviderError?.(e.binding,f)){const E=await e.invoke(e.binding,e.input,e.sessionId,e.runtimeOptions.requestId??e.sessionId,void 0,e.history,e.runtimeOptions),$=Array.isArray(E.metadata?.executedToolResults)?E.metadata.executedToolResults:[];for(const u of $)yield{kind:"tool-result",toolName:u.toolName,output:u.output,isError:u.isError};if(E.output){const u=C(E.output);u&&(yield{kind:"content",content:u})}return}if(e.countConfiguredTools(e.binding)>0&&f instanceof Error&&f.message.includes("does not support tool binding")||!Je(f))throw f;const U=Ie(f);if(!U)throw f;const Y=await e.invoke(e.applyToolRecoveryInstruction(e.binding,U),e.input,e.sessionId,e.runtimeOptions.requestId??e.sessionId,void 0,e.history,e.runtimeOptions);if(Y.output){const E=C(Y.output);E&&(yield{kind:"content",content:E})}}}export{ue as ExecutionReconciliationError,Ct as streamRuntimeExecution};
|