@deepstrike/sdk 0.2.21 → 0.2.23

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.
@@ -11,7 +11,7 @@ import { defaultSubAgentOrchestrator } from "./sub-agent-orchestrator.js";
11
11
  import { extractJsonValue, schemaInstruction, schemaRetryInstruction, validateAgainstSchema, } from "./output-schema.js";
12
12
  import { resolveReducer } from "./reducers.js";
13
13
  import { loopInstruction, classifyInstruction, judgeGoal, extractLoopContinue, extractClassifyBranch, extractJudgeWinner, } from "./workflow-control-flow.js";
14
- import { governancePolicyToKernelEvent } from "../governance.js";
14
+ import { governancePolicyToKernelEvent, governanceFilterSchema } from "../governance.js";
15
15
  import { kernelObservationToSessionEvent, withCategory } from "./kernel-event-log.js";
16
16
  import { assertNativeProfile } from "./os-profile.js";
17
17
  import { LargeResultSpool } from "./large-result-spool.js";
@@ -973,6 +973,26 @@ export class RuntimeRunner {
973
973
  message: attachmentsToKernelMessage(attachments),
974
974
  });
975
975
  }
976
+ // I4: pre-fetch memory into the knowledge partition before the first LLM turn. Skipped on
977
+ // resumes (memory was already on the prior context) and when dreamStore/agentId is absent.
978
+ if (!resumeMidRun && this.opts.preQueryMemory && this.opts.dreamStore && this.opts.agentId) {
979
+ try {
980
+ const queries = await this.opts.preQueryMemory({ goal, runSpec: this.opts.runSpec });
981
+ const entries = [];
982
+ for (const q of queries ?? []) {
983
+ if (typeof q !== "string" || !q.trim())
984
+ continue;
985
+ const hits = await this.opts.dreamStore.search(this.opts.agentId, q, 5);
986
+ for (const hit of hits) {
987
+ entries.push({ content: `[memory score=${hit.score.toFixed(3)}] ${hit.text}`, source: "memory" });
988
+ }
989
+ }
990
+ if (entries.length > 0) {
991
+ kernelApply(runtime, this.pendingObservations, { kind: "page_in", entries });
992
+ }
993
+ }
994
+ catch { /* errs-open — a faulty pre-fetch never breaks the run */ }
995
+ }
976
996
  let action = resumeMidRun
977
997
  ? kernelAction(runtime, this.pendingObservations, { kind: "resume" })
978
998
  : kernelAction(runtime, this.pendingObservations, startPayload);
@@ -980,369 +1000,440 @@ export class RuntimeRunner {
980
1000
  // P0-C: the skill loaded and in effect going into the current turn (updated when the model's
981
1001
  // `skill` tool call resolves). Drives the per-turn `activeSkill` metric → dwell measurement.
982
1002
  let activeSkill;
983
- while (!runtime.isTerminal()) {
984
- // Page-in must run before appendObservations drains pending kernel observations.
985
- if (action.kind === "execute_tool") {
986
- await this.applyKernelPageIn(runtime, sessionId);
987
- }
988
- nextCompressedArchiveStart = await this.appendObservations(sessionId, runtime, nextCompressedArchiveStart);
989
- this.nextArchiveStart = nextCompressedArchiveStart;
990
- if (this.interrupted) {
991
- action = kernelAction(runtime, this.pendingObservations, { kind: "timeout" });
992
- break;
993
- }
994
- if (this.opts.signalSource) {
995
- const sig = await this.opts.signalSource.nextSignal();
996
- if (sig) {
997
- // Kernel-routed: the kernel decides disposition (dedup/queue/interrupt) and emits
998
- // `signal_disposed`. An actionable disposition yields a new action to adopt; queued/observed/
999
- // ignored yields none (kernel buffers).
1000
- const sigAction = kernelMaybeAction(runtime, this.pendingObservations, signalToKernelEvent(sig));
1001
- if (sigAction)
1002
- action = sigAction;
1003
+ // I0b: wrap the main loop so any uncaught kernel exception (typically a NAPI
1004
+ // Status::InvalidArg from a malformed input — e.g. RuntimeSignal.source with a wrong shape,
1005
+ // or an unrecognized event kind) is observable rather than silently propagating out of the
1006
+ // async generator. Without this wrap the runner emits no `run_terminal` event, so downstream
1007
+ // observability (session log, bench mechanism hooks) can't distinguish "the kernel rejected
1008
+ // an input" from "the run is still in progress."
1009
+ try {
1010
+ while (!runtime.isTerminal()) {
1011
+ // Page-in must run before appendObservations drains pending kernel observations.
1012
+ if (action.kind === "execute_tool") {
1013
+ await this.applyKernelPageIn(runtime, sessionId);
1003
1014
  }
1004
- }
1005
- if (runtime.isTerminal())
1006
- break;
1007
- if (action.kind === "call_provider") {
1008
- // M5 v2.1: top-level auto-pivot at the safe point. If the agent authored sub-workflow(s) via
1009
- // `start_workflow`, drive each in THIS kernel now (the kernel is in Reason / `call_provider`,
1010
- // NOT suspended — driving mid-suspend would clobber the single-slot suspend state), inject the
1011
- // outcome into context, and re-render. Loop-top placement (vs only after `tool_results`) catches
1012
- // EVERY path to `call_provider` — including resuming after an approval gate — so a queued spec
1013
- // is never stranded. Drains the queue; fires once per authored batch.
1014
- if (this.pendingAuthoredWorkflows.length > 0) {
1015
- action = await this.driveAuthoredWorkflows(runtime, action);
1015
+ nextCompressedArchiveStart = await this.appendObservations(sessionId, runtime, nextCompressedArchiveStart);
1016
+ this.nextArchiveStart = nextCompressedArchiveStart;
1017
+ if (this.interrupted) {
1018
+ action = kernelAction(runtime, this.pendingObservations, { kind: "timeout" });
1019
+ break;
1016
1020
  }
1017
- const finalToolCalls = [];
1018
- let finalText = "";
1019
- const context = action.context;
1020
- const tools = action.tools;
1021
- let turnTokens = 0;
1022
- let turnInputTokens = 0;
1023
- let turnOutputTokens = 0;
1024
- let turnCacheReadTokens = 0;
1025
- let turnCacheCreationTokens = 0;
1026
- let shouldRetry = false;
1027
- const abortSignal = this.abortController?.signal;
1028
- try {
1029
- for await (const evt of this.opts.provider.stream(context, tools, Object.keys(ext).length ? ext : undefined, providerState, abortSignal)) {
1030
- // #2-B-ii: a preempting `interrupt()` fires `abortController` — stop consuming the live
1031
- // stream immediately (providers that forward `signal` also abort the socket; the rest at
1032
- // least stop here at the next event). The loop-top `interrupted` check then ends the run.
1033
- if (abortSignal?.aborted)
1034
- break;
1035
- if (evt.type === "usage") {
1036
- const usageEvt = evt;
1037
- turnTokens = usageEvt.totalTokens;
1038
- turnInputTokens = usageEvt.inputTokens ?? 0;
1039
- turnOutputTokens = usageEvt.outputTokens ?? 0;
1040
- // P0-C: capture the prompt-cache split for the tool-gating hit-rate baseline.
1041
- turnCacheReadTokens = usageEvt.cacheReadInputTokens ?? 0;
1042
- turnCacheCreationTokens = usageEvt.cacheCreationInputTokens ?? 0;
1043
- continue;
1044
- }
1045
- yield evt;
1046
- if (evt.type === "text_delta")
1047
- finalText += evt.delta;
1048
- else if (evt.type === "tool_call") {
1049
- const tc = evt;
1050
- finalToolCalls.push({ id: tc.id, name: tc.name, arguments: JSON.stringify(tc.arguments) });
1051
- }
1021
+ if (this.opts.signalSource) {
1022
+ const sig = await this.opts.signalSource.nextSignal();
1023
+ if (sig) {
1024
+ // Kernel-routed: the kernel decides disposition (dedup/queue/interrupt) and emits
1025
+ // `signal_disposed`. An actionable disposition yields a new action to adopt; queued/observed/
1026
+ // ignored yields none (kernel buffers).
1027
+ const sigAction = kernelMaybeAction(runtime, this.pendingObservations, signalToKernelEvent(sig));
1028
+ if (sigAction)
1029
+ action = sigAction;
1030
+ // I0a: a Critical-urgency signal carries user_abort intent. The kernel disposes it as
1031
+ // InterruptNow (forces a Reason turn) but does NOT call abortController.abort() unless
1032
+ // sub-agents are suspended — so the no-sub-agent path (e.g. the signal-injection bench
1033
+ // scenario) wouldn't otherwise set `this.interrupted`, and the eventual run_terminal would
1034
+ // report `reason: "error"` indistinguishable from a crash. Mark it here so the final
1035
+ // classification in the run_terminal emit picks `user_abort`.
1036
+ if (sig.urgency === "critical")
1037
+ this.interrupted = true;
1052
1038
  }
1053
1039
  }
1054
- catch (err) {
1055
- // #2-B-ii: an aborted in-flight request surfaces as an AbortError — treat it as an interrupt
1056
- // (the loop-top `interrupted` check converts it to a clean `timeout`/UserAbort), not a crash.
1057
- if (abortSignal?.aborted) {
1058
- this.interrupted = true;
1040
+ if (runtime.isTerminal())
1041
+ break;
1042
+ if (action.kind === "call_provider") {
1043
+ // M5 v2.1: top-level auto-pivot at the safe point. If the agent authored sub-workflow(s) via
1044
+ // `start_workflow`, drive each in THIS kernel now (the kernel is in Reason / `call_provider`,
1045
+ // NOT suspended — driving mid-suspend would clobber the single-slot suspend state), inject the
1046
+ // outcome into context, and re-render. Loop-top placement (vs only after `tool_results`) catches
1047
+ // EVERY path to `call_provider` — including resuming after an approval gate — so a queued spec
1048
+ // is never stranded. Drains the queue; fires once per authored batch.
1049
+ if (this.pendingAuthoredWorkflows.length > 0) {
1050
+ action = await this.driveAuthoredWorkflows(runtime, action);
1059
1051
  }
1060
- const errMsg = String(err).toLowerCase();
1061
- if ((errMsg.includes("413") || errMsg.includes("too long") || errMsg.includes("context length exceeded") || errMsg.includes("context_length_exceeded")) &&
1062
- !hasAttemptedReactiveCompact) {
1063
- hasAttemptedReactiveCompact = true;
1064
- if (forceCompact(runtime, this.pendingObservations)) {
1065
- nextCompressedArchiveStart = await this.appendObservations(sessionId, runtime, nextCompressedArchiveStart);
1066
- shouldRetry = true;
1052
+ const finalToolCalls = [];
1053
+ let finalText = "";
1054
+ // I5: governance schema-level pre-filter. When a declarative GovernancePolicy is loaded
1055
+ // and `surfaceDeniedInSystem !== false`, drop denied tools from the schema BEFORE the
1056
+ // model sees them — the model can't plan a call it doesn't know about, so the rollback
1057
+ // overhead disappears. The list of denied names is appended to systemKnowledge so the
1058
+ // model knows not to plan around them.
1059
+ let context = action.context;
1060
+ let tools = action.tools;
1061
+ if (this.opts.governancePolicy && this.opts.governancePolicy.surfaceDeniedInSystem !== false) {
1062
+ const { allowed, denied } = governanceFilterSchema(tools, this.opts.governancePolicy);
1063
+ if (denied.length > 0) {
1064
+ tools = allowed;
1065
+ const note = `[governance] the following tools are denied for this run and will fail if called: ${denied.join(", ")}.`;
1066
+ context = {
1067
+ ...context,
1068
+ systemKnowledge: context.systemKnowledge
1069
+ ? `${context.systemKnowledge}\n\n${note}`
1070
+ : note,
1071
+ };
1067
1072
  }
1068
1073
  }
1069
- if (!shouldRetry) {
1070
- yield { type: "error", message: String(err) };
1074
+ let turnTokens = 0;
1075
+ let turnInputTokens = 0;
1076
+ let turnOutputTokens = 0;
1077
+ let turnCacheReadTokens = 0;
1078
+ let turnCacheCreationTokens = 0;
1079
+ let turnCacheReadBySlot;
1080
+ let shouldRetry = false;
1081
+ const abortSignal = this.abortController?.signal;
1082
+ try {
1083
+ for await (const evt of this.opts.provider.stream(context, tools, Object.keys(ext).length ? ext : undefined, providerState, abortSignal)) {
1084
+ // #2-B-ii: a preempting `interrupt()` fires `abortController` — stop consuming the live
1085
+ // stream immediately (providers that forward `signal` also abort the socket; the rest at
1086
+ // least stop here at the next event). The loop-top `interrupted` check then ends the run.
1087
+ if (abortSignal?.aborted)
1088
+ break;
1089
+ if (evt.type === "usage") {
1090
+ const usageEvt = evt;
1091
+ turnTokens = usageEvt.totalTokens;
1092
+ turnInputTokens = usageEvt.inputTokens ?? 0;
1093
+ turnOutputTokens = usageEvt.outputTokens ?? 0;
1094
+ // P0-C: capture the prompt-cache split for the tool-gating hit-rate baseline.
1095
+ turnCacheReadTokens = usageEvt.cacheReadInputTokens ?? 0;
1096
+ turnCacheCreationTokens = usageEvt.cacheCreationInputTokens ?? 0;
1097
+ // I1: per-slot attribution forwarded into TurnMetrics. Undefined when the provider
1098
+ // doesn't honor cache_control (OpenAI-family auto-cache).
1099
+ turnCacheReadBySlot = usageEvt.cacheReadInputTokensBySlot;
1100
+ continue;
1101
+ }
1102
+ yield evt;
1103
+ if (evt.type === "text_delta")
1104
+ finalText += evt.delta;
1105
+ else if (evt.type === "tool_call") {
1106
+ const tc = evt;
1107
+ finalToolCalls.push({ id: tc.id, name: tc.name, arguments: JSON.stringify(tc.arguments) });
1108
+ }
1109
+ }
1110
+ }
1111
+ catch (err) {
1112
+ // #2-B-ii: an aborted in-flight request surfaces as an AbortError — treat it as an interrupt
1113
+ // (the loop-top `interrupted` check converts it to a clean `timeout`/UserAbort), not a crash.
1114
+ if (abortSignal?.aborted) {
1115
+ this.interrupted = true;
1116
+ }
1117
+ const errMsg = String(err).toLowerCase();
1118
+ if ((errMsg.includes("413") || errMsg.includes("too long") || errMsg.includes("context length exceeded") || errMsg.includes("context_length_exceeded")) &&
1119
+ !hasAttemptedReactiveCompact) {
1120
+ hasAttemptedReactiveCompact = true;
1121
+ if (forceCompact(runtime, this.pendingObservations)) {
1122
+ nextCompressedArchiveStart = await this.appendObservations(sessionId, runtime, nextCompressedArchiveStart);
1123
+ shouldRetry = true;
1124
+ }
1125
+ }
1126
+ if (!shouldRetry) {
1127
+ yield { type: "error", message: String(err) };
1128
+ action = kernelAction(runtime, this.pendingObservations, { kind: "timeout" });
1129
+ break;
1130
+ }
1131
+ }
1132
+ // #2-B-ii: stream aborted (preempt/interrupt) via the break path (provider yielded no error) —
1133
+ // end the turn now with a timeout so the kernel terminates the run, rather than feeding the
1134
+ // partial assistant output as a normal turn.
1135
+ if (abortSignal?.aborted) {
1071
1136
  action = kernelAction(runtime, this.pendingObservations, { kind: "timeout" });
1072
1137
  break;
1073
1138
  }
1074
- }
1075
- // #2-B-ii: stream aborted (preempt/interrupt) via the break path (provider yielded no error) —
1076
- // end the turn now with a timeout so the kernel terminates the run, rather than feeding the
1077
- // partial assistant output as a normal turn.
1078
- if (abortSignal?.aborted) {
1079
- action = kernelAction(runtime, this.pendingObservations, { kind: "timeout" });
1080
- break;
1081
- }
1082
- if (shouldRetry) {
1083
- action = {
1084
- kind: "call_provider",
1085
- context: runtime.render(),
1086
- tools,
1139
+ if (shouldRetry) {
1140
+ action = {
1141
+ kind: "call_provider",
1142
+ context: runtime.render(),
1143
+ tools,
1144
+ };
1145
+ continue;
1146
+ }
1147
+ const assistantMessage = {
1148
+ role: "assistant",
1149
+ content: finalText,
1150
+ toolCalls: finalToolCalls,
1151
+ tokenCount: turnOutputTokens || turnTokens || undefined,
1087
1152
  };
1088
- continue;
1089
- }
1090
- const assistantMessage = {
1091
- role: "assistant",
1092
- content: finalText,
1093
- toolCalls: finalToolCalls,
1094
- tokenCount: turnOutputTokens || turnTokens || undefined,
1095
- };
1096
- const providerEvent = {
1097
- kind: "provider_result",
1098
- message: messageToKernelMessage(assistantMessage),
1099
- ...(turnInputTokens > 0 ? { observed_input_tokens: turnInputTokens } : {}),
1100
- ...(turnOutputTokens > 0 ? { observed_output_tokens: turnOutputTokens } : {}),
1101
- now_ms: Date.now(),
1102
- };
1103
- let nextAction = kernelMaybeAction(runtime, this.pendingObservations, providerEvent);
1104
- if (!nextAction && this.pendingObservations.some(o => o.kind === "suspended")) {
1105
- const resolved = await this.resolveKernelSuspend(runtime, sessionId);
1106
- for (const evt of resolved.events)
1107
- yield evt;
1108
- nextAction = kernelAction(runtime, this.pendingObservations, {
1109
- kind: "resume",
1110
- approved_calls: resolved.approved,
1111
- denied_calls: resolved.denied,
1112
- });
1113
- }
1114
- action = nextAction ?? kernelAction(runtime, this.pendingObservations, providerEvent);
1115
- const providerReplay = peekProviderReplay(this.opts.provider, finalText, finalToolCalls);
1116
- await this.opts.sessionLog.append(sessionId, buildLlmCompletedEvent({
1117
- turn: runtime.turn(),
1118
- content: finalText,
1119
- tokenCount: turnOutputTokens || turnTokens || undefined,
1120
- toolCalls: finalToolCalls,
1121
- providerReplay,
1122
- }));
1123
- // P0-C: emit per-turn tool-gating telemetry. `activeSkill` reflects the skill in effect
1124
- // GOING INTO this turn; a `skill` call here only takes effect next turn, so emit first, then
1125
- // advance. Wrapped so a faulty sink can never break the run (pure observation).
1126
- if (this.opts.onTurnMetrics) {
1127
- try {
1128
- this.opts.onTurnMetrics({
1129
- turn: runtime.turn(),
1130
- toolsExposed: tools.length,
1131
- toolsCalled: finalToolCalls.length,
1132
- activeSkill,
1133
- inputTokens: turnInputTokens,
1134
- cacheReadTokens: turnCacheReadTokens,
1135
- cacheCreationTokens: turnCacheCreationTokens,
1153
+ const providerEvent = {
1154
+ kind: "provider_result",
1155
+ message: messageToKernelMessage(assistantMessage),
1156
+ ...(turnInputTokens > 0 ? { observed_input_tokens: turnInputTokens } : {}),
1157
+ ...(turnOutputTokens > 0 ? { observed_output_tokens: turnOutputTokens } : {}),
1158
+ now_ms: Date.now(),
1159
+ };
1160
+ let nextAction = kernelMaybeAction(runtime, this.pendingObservations, providerEvent);
1161
+ if (!nextAction && this.pendingObservations.some(o => o.kind === "suspended")) {
1162
+ const resolved = await this.resolveKernelSuspend(runtime, sessionId);
1163
+ for (const evt of resolved.events)
1164
+ yield evt;
1165
+ nextAction = kernelAction(runtime, this.pendingObservations, {
1166
+ kind: "resume",
1167
+ approved_calls: resolved.approved,
1168
+ denied_calls: resolved.denied,
1136
1169
  });
1137
1170
  }
1138
- catch { /* metrics must never break the run */ }
1139
- }
1140
- const skillCall = finalToolCalls.find(c => c.name === "skill");
1141
- if (skillCall) {
1142
- try {
1143
- const name = JSON.parse(skillCall.arguments || "{}").name;
1144
- if (name)
1145
- activeSkill = name;
1146
- }
1147
- catch { /* malformed skill args — leave activeSkill unchanged */ }
1148
- }
1149
- }
1150
- else if (action.kind === "execute_tool") {
1151
- const allCalls = action.calls;
1152
- await this.opts.sessionLog.append(sessionId, { kind: "tool_requested", turn: runtime.turn(), calls: allCalls });
1153
- const runCtx = {
1154
- agentId: this.opts.agentId,
1155
- skillDir: this.opts.skillDir,
1156
- dreamStore: this.opts.dreamStore,
1157
- knowledgeSource: this.opts.knowledgeSource,
1158
- onToolSuspend: this.opts.onToolSuspend,
1159
- onPermissionRequest: this.opts.onPermissionRequest,
1160
- resultSpool: this.opts.resultSpool ?? new LargeResultSpool(),
1161
- };
1162
- const toolResults = [];
1163
- const normalCalls = allCalls.filter(c => c.name !== "update_plan" && c.name !== "submit_workflow_nodes" && c.name !== "start_workflow");
1164
- const planCalls = allCalls.filter(c => c.name === "update_plan");
1165
- // M5 v1: `start_workflow` (author a sub-workflow) flattens to the same append path as
1166
- // `submit_workflow_nodes` — a `WorkflowSpec` is a node batch. (v2 adds top-level bootstrap.)
1167
- const submitCalls = allCalls.filter(c => c.name === "submit_workflow_nodes" || c.name === "start_workflow");
1168
- for (const call of planCalls) {
1169
- const update = parseUpdatePlanArgs(call.arguments);
1170
- kernelApply(runtime, this.pendingObservations, {
1171
- kind: "update_task",
1172
- update: taskUpdateToKernel(update),
1173
- });
1174
- const result = { callId: call.id, output: "success", isError: false };
1175
- toolResults.push(result);
1176
- yield { type: "tool_result", callId: call.id, content: "success", isError: false };
1177
- }
1178
- // R3-1: `submit_workflow_nodes` cannot be applied to this runner's kernel — when this runner
1179
- // is a workflow node, the workflow lives in the *parent* kernel. Surface the requested nodes
1180
- // as a stream event; the orchestrator collects them onto the node's result and `runWorkflow`
1181
- // sends `submit_workflow_nodes` to the parent kernel. (When not a workflow node, the event is
1182
- // simply unconsumed — a no-op.)
1183
- for (const call of submitCalls) {
1184
- // M5 v2.1: a TOP-LEVEL agent authoring a whole sub-workflow via `start_workflow` — record the
1185
- // full spec and AUTO-PIVOT once this tool turn resolves (the loop drives it in this kernel and
1186
- // injects the outcome). A workflow-NODE's `start_workflow` (and every `submit_workflow_nodes`)
1187
- // instead FLATTENS: the batch is surfaced for the parent `runWorkflow` to append.
1188
- if (call.name === "start_workflow" && !this.opts.isWorkflowNode) {
1189
- const spec = parseStartWorkflowSpec(call.arguments);
1190
- if (spec) {
1191
- this.pendingAuthoredWorkflows.push(spec);
1192
- const out = "workflow authored; executing now";
1193
- toolResults.push({ callId: call.id, output: out, isError: false });
1194
- yield { type: "tool_result", callId: call.id, content: out, isError: false };
1195
- continue;
1196
- }
1197
- }
1198
- // `start_workflow` wraps the batch as `{ spec: { nodes } }`; `submit_workflow_nodes` is `{ nodes }`.
1199
- const nodes = call.name === "start_workflow"
1200
- ? parseStartWorkflowArgs(call.arguments)
1201
- : parseSubmitWorkflowNodesArgs(call.arguments);
1202
- yield { type: "workflow_nodes_submitted", nodes };
1203
- const result = { callId: call.id, output: "submitted", isError: false };
1204
- toolResults.push(result);
1205
- yield { type: "tool_result", callId: call.id, content: "submitted", isError: false };
1206
- }
1207
- if (normalCalls.length > 0) {
1208
- for await (const evt of this.opts.executionPlane.executeAll(normalCalls, runCtx)) {
1209
- yield evt;
1210
- if (evt.type === "tool_result") {
1211
- const tre = evt;
1212
- toolResults.push({
1213
- callId: tre.callId,
1214
- output: tre.content,
1215
- isError: tre.isError,
1216
- isFatal: tre.isFatal,
1217
- errorKind: tre.errorKind,
1218
- });
1219
- }
1220
- else if (evt.type === "tool_argument_repaired") {
1221
- const tare = evt;
1222
- await this.opts.sessionLog.append(sessionId, {
1223
- kind: "tool_argument_repaired",
1171
+ action = nextAction ?? kernelAction(runtime, this.pendingObservations, providerEvent);
1172
+ const providerReplay = peekProviderReplay(this.opts.provider, finalText, finalToolCalls);
1173
+ await this.opts.sessionLog.append(sessionId, buildLlmCompletedEvent({
1174
+ turn: runtime.turn(),
1175
+ content: finalText,
1176
+ tokenCount: turnOutputTokens || turnTokens || undefined,
1177
+ toolCalls: finalToolCalls,
1178
+ providerReplay,
1179
+ }));
1180
+ // P0-C: emit per-turn tool-gating telemetry. `activeSkill` reflects the skill in effect
1181
+ // GOING INTO this turn; a `skill` call here only takes effect next turn, so emit first, then
1182
+ // advance. Wrapped so a faulty sink can never break the run (pure observation).
1183
+ if (this.opts.onTurnMetrics) {
1184
+ try {
1185
+ this.opts.onTurnMetrics({
1224
1186
  turn: runtime.turn(),
1225
- tool: tare.name,
1226
- original_arguments: tare.originalArguments,
1227
- repaired_arguments: tare.repairedArguments,
1187
+ toolsExposed: tools.length,
1188
+ toolsCalled: finalToolCalls.length,
1189
+ activeSkill,
1190
+ inputTokens: turnInputTokens,
1191
+ cacheReadTokens: turnCacheReadTokens,
1192
+ cacheCreationTokens: turnCacheCreationTokens,
1193
+ ...(turnCacheReadBySlot ? { cacheReadTokensBySlot: turnCacheReadBySlot } : {}),
1228
1194
  });
1229
1195
  }
1230
- else if (evt.type === "tool_denied") {
1231
- const tde = evt;
1232
- await this.opts.sessionLog.append(sessionId, {
1233
- kind: "tool_denied",
1234
- turn: runtime.turn(),
1235
- call_id: tde.callId,
1236
- tool_name: tde.toolName,
1237
- reason: tde.reason,
1238
- });
1196
+ catch { /* metrics must never break the run */ }
1197
+ }
1198
+ const skillCall = finalToolCalls.find(c => c.name === "skill");
1199
+ if (skillCall) {
1200
+ try {
1201
+ const name = JSON.parse(skillCall.arguments || "{}").name;
1202
+ if (name)
1203
+ activeSkill = name;
1239
1204
  }
1240
- else if (evt.type === "permission_request") {
1241
- const pre = evt;
1242
- const turn = runtime.turn();
1243
- await this.opts.sessionLog.append(sessionId, {
1244
- kind: "permission_requested",
1245
- turn,
1246
- tool: pre.toolName,
1247
- arguments: pre.arguments,
1248
- reason: pre.reason,
1249
- });
1205
+ catch { /* malformed skill args — leave activeSkill unchanged */ }
1206
+ }
1207
+ }
1208
+ else if (action.kind === "execute_tool") {
1209
+ const allCalls = action.calls;
1210
+ await this.opts.sessionLog.append(sessionId, { kind: "tool_requested", turn: runtime.turn(), calls: allCalls });
1211
+ const runCtx = {
1212
+ agentId: this.opts.agentId,
1213
+ skillDir: this.opts.skillDir,
1214
+ dreamStore: this.opts.dreamStore,
1215
+ knowledgeSource: this.opts.knowledgeSource,
1216
+ onToolSuspend: this.opts.onToolSuspend,
1217
+ onPermissionRequest: this.opts.onPermissionRequest,
1218
+ resultSpool: this.opts.resultSpool ?? new LargeResultSpool(),
1219
+ };
1220
+ const toolResults = [];
1221
+ const normalCalls = allCalls.filter(c => c.name !== "update_plan" && c.name !== "submit_workflow_nodes" && c.name !== "start_workflow");
1222
+ const planCalls = allCalls.filter(c => c.name === "update_plan");
1223
+ // M5 v1: `start_workflow` (author a sub-workflow) flattens to the same append path as
1224
+ // `submit_workflow_nodes` — a `WorkflowSpec` is a node batch. (v2 adds top-level bootstrap.)
1225
+ const submitCalls = allCalls.filter(c => c.name === "submit_workflow_nodes" || c.name === "start_workflow");
1226
+ for (const call of planCalls) {
1227
+ const update = parseUpdatePlanArgs(call.arguments);
1228
+ kernelApply(runtime, this.pendingObservations, {
1229
+ kind: "update_task",
1230
+ update: taskUpdateToKernel(update),
1231
+ });
1232
+ const result = { callId: call.id, output: "success", isError: false };
1233
+ toolResults.push(result);
1234
+ yield { type: "tool_result", callId: call.id, content: "success", isError: false };
1235
+ }
1236
+ // R3-1: `submit_workflow_nodes` cannot be applied to this runner's kernel — when this runner
1237
+ // is a workflow node, the workflow lives in the *parent* kernel. Surface the requested nodes
1238
+ // as a stream event; the orchestrator collects them onto the node's result and `runWorkflow`
1239
+ // sends `submit_workflow_nodes` to the parent kernel. (When not a workflow node, the event is
1240
+ // simply unconsumed — a no-op.)
1241
+ for (const call of submitCalls) {
1242
+ // M5 v2.1: a TOP-LEVEL agent authoring a whole sub-workflow via `start_workflow` — record the
1243
+ // full spec and AUTO-PIVOT once this tool turn resolves (the loop drives it in this kernel and
1244
+ // injects the outcome). A workflow-NODE's `start_workflow` (and every `submit_workflow_nodes`)
1245
+ // instead FLATTENS: the batch is surfaced for the parent `runWorkflow` to append.
1246
+ if (call.name === "start_workflow" && !this.opts.isWorkflowNode) {
1247
+ const spec = parseStartWorkflowSpec(call.arguments);
1248
+ if (spec) {
1249
+ this.pendingAuthoredWorkflows.push(spec);
1250
+ const out = "workflow authored; executing now";
1251
+ toolResults.push({ callId: call.id, output: out, isError: false });
1252
+ yield { type: "tool_result", callId: call.id, content: out, isError: false };
1253
+ continue;
1254
+ }
1250
1255
  }
1251
- else if (evt.type === "permission_resolved") {
1252
- const resolved = evt;
1253
- const turn = runtime.turn();
1254
- await this.opts.sessionLog.append(sessionId, {
1255
- kind: "permission_resolved",
1256
- turn,
1257
- approved: resolved.approved,
1258
- responder: resolved.responder,
1259
- });
1256
+ // `start_workflow` wraps the batch as `{ spec: { nodes } }`; `submit_workflow_nodes` is `{ nodes }`.
1257
+ const nodes = call.name === "start_workflow"
1258
+ ? parseStartWorkflowArgs(call.arguments)
1259
+ : parseSubmitWorkflowNodesArgs(call.arguments);
1260
+ yield { type: "workflow_nodes_submitted", nodes };
1261
+ const result = { callId: call.id, output: "submitted", isError: false };
1262
+ toolResults.push(result);
1263
+ yield { type: "tool_result", callId: call.id, content: "submitted", isError: false };
1264
+ }
1265
+ if (normalCalls.length > 0) {
1266
+ for await (const evt of this.opts.executionPlane.executeAll(normalCalls, runCtx)) {
1267
+ yield evt;
1268
+ if (evt.type === "tool_result") {
1269
+ const tre = evt;
1270
+ toolResults.push({
1271
+ callId: tre.callId,
1272
+ output: tre.content,
1273
+ isError: tre.isError,
1274
+ isFatal: tre.isFatal,
1275
+ errorKind: tre.errorKind,
1276
+ });
1277
+ }
1278
+ else if (evt.type === "tool_argument_repaired") {
1279
+ const tare = evt;
1280
+ await this.opts.sessionLog.append(sessionId, {
1281
+ kind: "tool_argument_repaired",
1282
+ turn: runtime.turn(),
1283
+ tool: tare.name,
1284
+ original_arguments: tare.originalArguments,
1285
+ repaired_arguments: tare.repairedArguments,
1286
+ });
1287
+ }
1288
+ else if (evt.type === "tool_denied") {
1289
+ const tde = evt;
1290
+ await this.opts.sessionLog.append(sessionId, {
1291
+ kind: "tool_denied",
1292
+ turn: runtime.turn(),
1293
+ call_id: tde.callId,
1294
+ tool_name: tde.toolName,
1295
+ reason: tde.reason,
1296
+ });
1297
+ }
1298
+ else if (evt.type === "permission_request") {
1299
+ const pre = evt;
1300
+ const turn = runtime.turn();
1301
+ await this.opts.sessionLog.append(sessionId, {
1302
+ kind: "permission_requested",
1303
+ turn,
1304
+ tool: pre.toolName,
1305
+ arguments: pre.arguments,
1306
+ reason: pre.reason,
1307
+ });
1308
+ }
1309
+ else if (evt.type === "permission_resolved") {
1310
+ const resolved = evt;
1311
+ const turn = runtime.turn();
1312
+ await this.opts.sessionLog.append(sessionId, {
1313
+ kind: "permission_resolved",
1314
+ turn,
1315
+ approved: resolved.approved,
1316
+ responder: resolved.responder,
1317
+ });
1318
+ }
1260
1319
  }
1320
+ const names = normalCalls.map(c => c.name).join(", ");
1321
+ kernelApply(runtime, this.pendingObservations, {
1322
+ kind: "update_task",
1323
+ update: taskUpdateToKernel({ progress: `Executed tools: ${names}` }),
1324
+ });
1261
1325
  }
1262
- const names = normalCalls.map(c => c.name).join(", ");
1263
- kernelApply(runtime, this.pendingObservations, {
1264
- kind: "update_task",
1265
- update: taskUpdateToKernel({ progress: `Executed tools: ${names}` }),
1326
+ await this.opts.sessionLog.append(sessionId, {
1327
+ kind: "tool_completed",
1328
+ turn: runtime.turn(),
1329
+ results: toolResults.map(r => ({
1330
+ call_id: r.callId,
1331
+ output: r.output,
1332
+ is_error: r.isError,
1333
+ token_count: r.tokenCount,
1334
+ })),
1266
1335
  });
1267
- }
1268
- await this.opts.sessionLog.append(sessionId, {
1269
- kind: "tool_completed",
1270
- turn: runtime.turn(),
1271
- results: toolResults.map(r => ({
1272
- call_id: r.callId,
1273
- output: r.output,
1274
- is_error: r.isError,
1275
- token_count: r.tokenCount,
1276
- })),
1277
- });
1278
- for (const call of normalCalls) {
1279
- const result = toolResults.find(r => r.callId === call.id);
1280
- if (result) {
1281
- this.pendingSpoolOutputs.set(call.id, { tool: call.name, output: result.output });
1336
+ for (const call of normalCalls) {
1337
+ const result = toolResults.find(r => r.callId === call.id);
1338
+ if (result) {
1339
+ this.pendingSpoolOutputs.set(call.id, { tool: call.name, output: result.output });
1340
+ }
1282
1341
  }
1283
- }
1284
- // P1-B B3: a `skill` call that resolved successfully activates that skill in the kernel, so
1285
- // the next `call_provider` narrows the toolset to its declared tools. Fed before `tool_results`
1286
- // (which computes the next action). Errs-open: a failed/missing skill load doesn't activate.
1287
- for (const call of allCalls) {
1288
- if (call.name !== "skill")
1289
- continue;
1290
- const res = toolResults.find(r => r.callId === call.id);
1291
- if (!res || res.isError)
1292
- continue;
1293
- try {
1294
- const name = JSON.parse(call.arguments || "{}").name;
1295
- if (name)
1296
- kernelApply(runtime, this.pendingObservations, { kind: "skill_activated", name });
1342
+ // P1-B B3: a `skill` call that resolved successfully activates that skill in the kernel, so
1343
+ // the next `call_provider` narrows the toolset to its declared tools. Fed before `tool_results`
1344
+ // (which computes the next action). Errs-open: a failed/missing skill load doesn't activate.
1345
+ for (const call of allCalls) {
1346
+ if (call.name !== "skill")
1347
+ continue;
1348
+ const res = toolResults.find(r => r.callId === call.id);
1349
+ if (!res || res.isError)
1350
+ continue;
1351
+ try {
1352
+ const name = JSON.parse(call.arguments || "{}").name;
1353
+ if (name)
1354
+ kernelApply(runtime, this.pendingObservations, { kind: "skill_activated", name });
1355
+ }
1356
+ catch { /* malformed skill args — skip activation */ }
1297
1357
  }
1298
- catch { /* malformed skill args — skip activation */ }
1299
- }
1300
- action = kernelAction(runtime, this.pendingObservations, {
1301
- kind: "tool_results",
1302
- results: toolResults.map(toolResultToKernel),
1303
- });
1304
- }
1305
- else if (action.kind === "evaluate_milestone") {
1306
- const milestonePolicy = this.opts.milestonePolicy ?? "require_verifier";
1307
- if (milestonePolicy === "auto_pass") {
1308
1358
  action = kernelAction(runtime, this.pendingObservations, {
1309
- kind: "milestone_result",
1310
- result: milestoneCheckResultToKernel(milestoneCheckPass(action.phaseId)),
1359
+ kind: "tool_results",
1360
+ results: toolResults.map(toolResultToKernel),
1311
1361
  });
1312
- this.nextArchiveStart = await this.appendObservations(sessionId, runtime, this.nextArchiveStart);
1313
1362
  }
1314
- else if (this.opts.onMilestoneEvaluate) {
1315
- const check = await this.opts.onMilestoneEvaluate({
1316
- phaseId: action.phaseId,
1317
- criteria: action.criteria,
1318
- requiredEvidence: action.requiredEvidence,
1319
- });
1320
- action = kernelAction(runtime, this.pendingObservations, {
1321
- kind: "milestone_result",
1322
- result: milestoneCheckResultToKernel(check),
1323
- });
1324
- this.nextArchiveStart = await this.appendObservations(sessionId, runtime, this.nextArchiveStart);
1363
+ else if (action.kind === "evaluate_milestone") {
1364
+ const milestonePolicy = this.opts.milestonePolicy ?? "require_verifier";
1365
+ if (milestonePolicy === "auto_pass") {
1366
+ action = kernelAction(runtime, this.pendingObservations, {
1367
+ kind: "milestone_result",
1368
+ result: milestoneCheckResultToKernel(milestoneCheckPass(action.phaseId)),
1369
+ });
1370
+ this.nextArchiveStart = await this.appendObservations(sessionId, runtime, this.nextArchiveStart);
1371
+ }
1372
+ else if (this.opts.onMilestoneEvaluate) {
1373
+ const check = await this.opts.onMilestoneEvaluate({
1374
+ phaseId: action.phaseId,
1375
+ criteria: action.criteria,
1376
+ requiredEvidence: action.requiredEvidence,
1377
+ });
1378
+ action = kernelAction(runtime, this.pendingObservations, {
1379
+ kind: "milestone_result",
1380
+ result: milestoneCheckResultToKernel(check),
1381
+ });
1382
+ this.nextArchiveStart = await this.appendObservations(sessionId, runtime, this.nextArchiveStart);
1383
+ }
1384
+ else {
1385
+ this.nextArchiveStart = await this.appendObservations(sessionId, runtime, this.nextArchiveStart);
1386
+ const turnsUsed = Math.max(1, runtime.turn());
1387
+ await this.opts.sessionLog.append(sessionId, buildRunTerminalEvent({
1388
+ reason: "milestone_pending",
1389
+ turnsUsed,
1390
+ totalTokens: 0,
1391
+ }));
1392
+ yield { type: "done", iterations: turnsUsed, totalTokens: 0, status: "milestone_pending" };
1393
+ this.activeKernel = null;
1394
+ this.currentSessionId = null;
1395
+ return;
1396
+ }
1325
1397
  }
1326
- else {
1327
- this.nextArchiveStart = await this.appendObservations(sessionId, runtime, this.nextArchiveStart);
1328
- const turnsUsed = Math.max(1, runtime.turn());
1329
- await this.opts.sessionLog.append(sessionId, buildRunTerminalEvent({
1330
- reason: "milestone_pending",
1331
- turnsUsed,
1332
- totalTokens: 0,
1333
- }));
1334
- yield { type: "done", iterations: turnsUsed, totalTokens: 0, status: "milestone_pending" };
1335
- this.activeKernel = null;
1336
- this.currentSessionId = null;
1337
- return;
1398
+ else if (action.kind === "done") {
1399
+ break;
1338
1400
  }
1339
1401
  }
1340
- else if (action.kind === "done") {
1341
- break;
1402
+ }
1403
+ catch (err) {
1404
+ // I0b: kernel rejection (or any other thrown error inside the loop) reaches us here.
1405
+ // Classify by NAPI status code or message pattern — `invalid_arg` for surface-shape rejects,
1406
+ // `error` for everything else — then emit run_terminal so observability sees a clean end.
1407
+ // The yield-error path mirrors what the in-flight provider-stream catch does.
1408
+ const errMsg = err instanceof Error ? err.message : String(err);
1409
+ const code = err.code;
1410
+ const isInvalidArg = code === "InvalidArg" ||
1411
+ errMsg.toLowerCase().includes("invalidarg") ||
1412
+ errMsg.toLowerCase().includes("invalid argument");
1413
+ const reason = isInvalidArg ? "invalid_arg" : "error";
1414
+ yield { type: "error", message: errMsg };
1415
+ try {
1416
+ await this.opts.sessionLog.append(sessionId, buildRunTerminalEvent({
1417
+ reason,
1418
+ turnsUsed: runtime.turn() || 0,
1419
+ totalTokens: 0,
1420
+ }));
1342
1421
  }
1422
+ catch { /* session log failure must not mask the original error */ }
1423
+ yield { type: "done", iterations: runtime.turn() || 0, totalTokens: 0, status: reason };
1424
+ this.activeKernel = null;
1425
+ this.currentSessionId = null;
1426
+ this.dashboard = null;
1427
+ return;
1343
1428
  }
1344
1429
  const result = action.kind === "done" ? action.result : undefined;
1345
- const status = result?.termination ?? "error";
1430
+ // I0a: when the loop exits without a clean kernel-done — typically because a hard interrupt
1431
+ // aborted the in-flight LLM stream and the catch path sent `timeout` (which the kernel handles
1432
+ // by injecting a rollback note and continuing, not by terminating) — preserve the preempt
1433
+ // intent in the run_terminal reason. Without this, every interrupt-curtailed run reports
1434
+ // `reason: "error"` and the bench / observability layer can't distinguish preemption from a
1435
+ // genuine crash. Mirrors WASM/Python/Rust.
1436
+ const status = result?.termination ?? (this.interrupted ? "user_abort" : "error");
1346
1437
  const turnsUsed = result ? Math.max(1, result.turnsUsed) : runtime.turn() || 0;
1347
1438
  const totalTokens = result?.totalTokensUsed ?? 0;
1348
1439
  nextCompressedArchiveStart = await this.appendObservations(sessionId, runtime, nextCompressedArchiveStart);