@ouro.bot/cli 0.1.0-alpha.13 → 0.1.0-alpha.131
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/AdoptionSpecialist.ouro/psyche/SOUL.md +2 -2
- package/AdoptionSpecialist.ouro/psyche/identities/monty.md +2 -2
- package/README.md +147 -205
- package/changelog.json +814 -0
- package/dist/heart/active-work.js +622 -0
- package/dist/heart/bridges/manager.js +358 -0
- package/dist/heart/bridges/state-machine.js +135 -0
- package/dist/heart/bridges/store.js +123 -0
- package/dist/heart/commitments.js +105 -0
- package/dist/heart/config.js +66 -21
- package/dist/heart/core.js +518 -100
- package/dist/heart/cross-chat-delivery.js +146 -0
- package/dist/heart/daemon/agent-discovery.js +81 -0
- package/dist/heart/daemon/auth-flow.js +457 -0
- package/dist/heart/daemon/daemon-cli.js +1516 -195
- package/dist/heart/daemon/daemon-entry.js +43 -2
- package/dist/heart/daemon/daemon-runtime-sync.js +212 -0
- package/dist/heart/daemon/daemon.js +261 -1
- package/dist/heart/daemon/hatch-animation.js +10 -3
- package/dist/heart/daemon/hatch-flow.js +7 -72
- package/dist/heart/daemon/hooks/bundle-meta.js +92 -0
- package/dist/heart/daemon/launchd.js +159 -0
- package/dist/heart/daemon/log-tailer.js +4 -3
- package/dist/heart/daemon/message-router.js +17 -8
- package/dist/heart/daemon/ouro-bot-global-installer.js +128 -0
- package/dist/heart/daemon/ouro-path-installer.js +57 -29
- package/dist/heart/daemon/ouro-version-manager.js +171 -0
- package/dist/heart/daemon/process-manager.js +13 -0
- package/dist/heart/daemon/run-hooks.js +37 -0
- package/dist/heart/daemon/runtime-logging.js +58 -15
- package/dist/heart/daemon/runtime-metadata.js +219 -0
- package/dist/heart/daemon/runtime-mode.js +67 -0
- package/dist/heart/daemon/sense-manager.js +50 -2
- package/dist/heart/daemon/skill-management-installer.js +94 -0
- package/dist/heart/daemon/socket-client.js +202 -0
- package/dist/heart/daemon/specialist-orchestrator.js +2 -2
- package/dist/heart/daemon/specialist-prompt.js +7 -4
- package/dist/heart/daemon/specialist-tools.js +52 -3
- package/dist/heart/daemon/staged-restart.js +114 -0
- package/dist/heart/daemon/thoughts.js +507 -0
- package/dist/heart/daemon/update-checker.js +111 -0
- package/dist/heart/daemon/update-hooks.js +138 -0
- package/dist/heart/daemon/wrapper-publish-guard.js +86 -0
- package/dist/heart/delegation.js +62 -0
- package/dist/heart/identity.js +64 -21
- package/dist/heart/kicks.js +1 -19
- package/dist/heart/model-capabilities.js +48 -0
- package/dist/heart/obligations.js +197 -0
- package/dist/heart/progress-story.js +42 -0
- package/dist/heart/provider-failover.js +88 -0
- package/dist/heart/provider-ping.js +159 -0
- package/dist/heart/providers/anthropic-token.js +163 -0
- package/dist/heart/providers/anthropic.js +195 -34
- package/dist/heart/providers/azure.js +115 -9
- package/dist/heart/providers/github-copilot.js +157 -0
- package/dist/heart/providers/minimax.js +33 -3
- package/dist/heart/providers/openai-codex.js +49 -14
- package/dist/heart/safe-workspace.js +381 -0
- package/dist/heart/session-activity.js +173 -0
- package/dist/heart/session-recall.js +216 -0
- package/dist/heart/streaming.js +108 -24
- package/dist/heart/target-resolution.js +123 -0
- package/dist/heart/tool-loop.js +194 -0
- package/dist/heart/turn-coordinator.js +28 -0
- package/dist/mind/associative-recall.js +14 -2
- package/dist/mind/bundle-manifest.js +12 -0
- package/dist/mind/context.js +60 -14
- package/dist/mind/first-impressions.js +16 -2
- package/dist/mind/friends/channel.js +35 -0
- package/dist/mind/friends/group-context.js +144 -0
- package/dist/mind/friends/store-file.js +19 -0
- package/dist/mind/friends/trust-explanation.js +74 -0
- package/dist/mind/friends/types.js +8 -0
- package/dist/mind/memory.js +27 -26
- package/dist/mind/obligation-steering.js +221 -0
- package/dist/mind/pending.js +76 -9
- package/dist/mind/phrases.js +1 -0
- package/dist/mind/prompt.js +456 -77
- package/dist/mind/token-estimate.js +8 -12
- package/dist/nerves/cli-logging.js +15 -2
- package/dist/nerves/coverage/run-artifacts.js +1 -1
- package/dist/nerves/index.js +12 -0
- package/dist/nerves/runtime.js +5 -1
- package/dist/repertoire/ado-client.js +4 -2
- package/dist/repertoire/coding/context-pack.js +254 -0
- package/dist/repertoire/coding/feedback.js +301 -0
- package/dist/repertoire/coding/index.js +4 -1
- package/dist/repertoire/coding/manager.js +210 -4
- package/dist/repertoire/coding/spawner.js +39 -9
- package/dist/repertoire/coding/tools.js +171 -4
- package/dist/repertoire/data/ado-endpoints.json +188 -0
- package/dist/repertoire/guardrails.js +290 -0
- package/dist/repertoire/mcp-client.js +254 -0
- package/dist/repertoire/mcp-manager.js +198 -0
- package/dist/repertoire/skills.js +3 -26
- package/dist/repertoire/tasks/board.js +12 -0
- package/dist/repertoire/tasks/index.js +23 -9
- package/dist/repertoire/tasks/transitions.js +1 -2
- package/dist/repertoire/tools-base.js +925 -250
- package/dist/repertoire/tools-bluebubbles.js +93 -0
- package/dist/repertoire/tools-teams.js +58 -25
- package/dist/repertoire/tools.js +106 -53
- package/dist/senses/bluebubbles-client.js +210 -5
- package/dist/senses/bluebubbles-entry.js +2 -0
- package/dist/senses/bluebubbles-inbound-log.js +109 -0
- package/dist/senses/bluebubbles-media.js +339 -0
- package/dist/senses/bluebubbles-model.js +12 -4
- package/dist/senses/bluebubbles-mutation-log.js +45 -5
- package/dist/senses/bluebubbles-runtime-state.js +109 -0
- package/dist/senses/bluebubbles-session-cleanup.js +72 -0
- package/dist/senses/bluebubbles.js +915 -45
- package/dist/senses/cli-layout.js +187 -0
- package/dist/senses/cli.js +374 -131
- package/dist/senses/continuity.js +94 -0
- package/dist/senses/debug-activity.js +154 -0
- package/dist/senses/inner-dialog-worker.js +47 -18
- package/dist/senses/inner-dialog.js +388 -83
- package/dist/senses/pipeline.js +444 -0
- package/dist/senses/teams.js +607 -129
- package/dist/senses/trust-gate.js +112 -2
- package/package.json +9 -3
- package/subagents/README.md +4 -70
- package/dist/heart/daemon/subagent-installer.js +0 -134
- package/subagents/work-doer.md +0 -233
- package/subagents/work-merger.md +0 -624
- package/subagents/work-planner.md +0 -373
package/dist/heart/core.js
CHANGED
|
@@ -7,7 +7,10 @@ exports.getModel = getModel;
|
|
|
7
7
|
exports.getProvider = getProvider;
|
|
8
8
|
exports.createSummarize = createSummarize;
|
|
9
9
|
exports.getProviderDisplayLabel = getProviderDisplayLabel;
|
|
10
|
+
exports.isExternalStateQuery = isExternalStateQuery;
|
|
11
|
+
exports.getFinalAnswerRetryError = getFinalAnswerRetryError;
|
|
10
12
|
exports.stripLastToolCalls = stripLastToolCalls;
|
|
13
|
+
exports.repairOrphanedToolCalls = repairOrphanedToolCalls;
|
|
11
14
|
exports.isTransientError = isTransientError;
|
|
12
15
|
exports.classifyTransientError = classifyTransientError;
|
|
13
16
|
exports.runAgent = runAgent;
|
|
@@ -15,9 +18,6 @@ const config_1 = require("./config");
|
|
|
15
18
|
const identity_1 = require("./identity");
|
|
16
19
|
const tools_1 = require("../repertoire/tools");
|
|
17
20
|
const channel_1 = require("../mind/friends/channel");
|
|
18
|
-
// Kick detection preserved but disabled — see comment in agent loop below.
|
|
19
|
-
// import { detectKick } from "./kicks";
|
|
20
|
-
// import type { KickReason } from "./kicks";
|
|
21
21
|
const runtime_1 = require("../nerves/runtime");
|
|
22
22
|
const context_1 = require("../mind/context");
|
|
23
23
|
const prompt_1 = require("../mind/prompt");
|
|
@@ -26,13 +26,48 @@ const anthropic_1 = require("./providers/anthropic");
|
|
|
26
26
|
const azure_1 = require("./providers/azure");
|
|
27
27
|
const minimax_1 = require("./providers/minimax");
|
|
28
28
|
const openai_codex_1 = require("./providers/openai-codex");
|
|
29
|
+
const github_copilot_1 = require("./providers/github-copilot");
|
|
30
|
+
const pending_1 = require("../mind/pending");
|
|
31
|
+
const identity_2 = require("./identity");
|
|
32
|
+
const socket_client_1 = require("./daemon/socket-client");
|
|
33
|
+
const obligations_1 = require("./obligations");
|
|
34
|
+
const tool_loop_1 = require("./tool-loop");
|
|
29
35
|
let _providerRuntime = null;
|
|
36
|
+
function getProviderRuntimeFingerprint() {
|
|
37
|
+
const provider = (0, identity_1.loadAgentConfig)().provider;
|
|
38
|
+
/* v8 ignore next -- switch: not all provider branches exercised in CI @preserve */
|
|
39
|
+
switch (provider) {
|
|
40
|
+
case "azure": {
|
|
41
|
+
const { apiKey, endpoint, deployment, modelName, apiVersion, managedIdentityClientId } = (0, config_1.getAzureConfig)();
|
|
42
|
+
return JSON.stringify({ provider, apiKey, endpoint, deployment, modelName, apiVersion, managedIdentityClientId });
|
|
43
|
+
}
|
|
44
|
+
case "anthropic": {
|
|
45
|
+
const { model, setupToken } = (0, config_1.getAnthropicConfig)();
|
|
46
|
+
return JSON.stringify({ provider, model, setupToken });
|
|
47
|
+
}
|
|
48
|
+
case "minimax": {
|
|
49
|
+
const { apiKey, model } = (0, config_1.getMinimaxConfig)();
|
|
50
|
+
return JSON.stringify({ provider, apiKey, model });
|
|
51
|
+
}
|
|
52
|
+
case "openai-codex": {
|
|
53
|
+
const { model, oauthAccessToken } = (0, config_1.getOpenAICodexConfig)();
|
|
54
|
+
return JSON.stringify({ provider, model, oauthAccessToken });
|
|
55
|
+
}
|
|
56
|
+
/* v8 ignore start -- fingerprint: tested via provider init tests @preserve */
|
|
57
|
+
case "github-copilot": {
|
|
58
|
+
const { model, githubToken, baseUrl } = (0, config_1.getGithubCopilotConfig)();
|
|
59
|
+
return JSON.stringify({ provider, model, githubToken, baseUrl });
|
|
60
|
+
}
|
|
61
|
+
/* v8 ignore stop */
|
|
62
|
+
}
|
|
63
|
+
}
|
|
30
64
|
function createProviderRegistry() {
|
|
31
65
|
const factories = {
|
|
32
66
|
azure: azure_1.createAzureProviderRuntime,
|
|
33
67
|
anthropic: anthropic_1.createAnthropicProviderRuntime,
|
|
34
68
|
minimax: minimax_1.createMinimaxProviderRuntime,
|
|
35
69
|
"openai-codex": openai_codex_1.createOpenAICodexProviderRuntime,
|
|
70
|
+
"github-copilot": github_copilot_1.createGithubCopilotProviderRuntime,
|
|
36
71
|
};
|
|
37
72
|
return {
|
|
38
73
|
resolve() {
|
|
@@ -42,42 +77,44 @@ function createProviderRegistry() {
|
|
|
42
77
|
};
|
|
43
78
|
}
|
|
44
79
|
function getProviderRuntime() {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const msg = error instanceof Error ? error.message : String(error);
|
|
51
|
-
(0, runtime_1.emitNervesEvent)({
|
|
52
|
-
level: "error",
|
|
53
|
-
event: "engine.provider_init_error",
|
|
54
|
-
component: "engine",
|
|
55
|
-
message: msg,
|
|
56
|
-
meta: {},
|
|
57
|
-
});
|
|
58
|
-
// eslint-disable-next-line no-console -- pre-boot guard: provider init failure
|
|
59
|
-
console.error(`\n[fatal] ${msg}\n`);
|
|
60
|
-
process.exit(1);
|
|
61
|
-
throw new Error("unreachable");
|
|
62
|
-
}
|
|
63
|
-
if (!_providerRuntime) {
|
|
64
|
-
(0, runtime_1.emitNervesEvent)({
|
|
65
|
-
level: "error",
|
|
66
|
-
event: "engine.provider_init_error",
|
|
67
|
-
component: "engine",
|
|
68
|
-
message: "provider runtime could not be initialized.",
|
|
69
|
-
meta: {},
|
|
70
|
-
});
|
|
71
|
-
process.exit(1);
|
|
72
|
-
throw new Error("unreachable");
|
|
80
|
+
try {
|
|
81
|
+
const fingerprint = getProviderRuntimeFingerprint();
|
|
82
|
+
if (!_providerRuntime || _providerRuntime.fingerprint !== fingerprint) {
|
|
83
|
+
const runtime = createProviderRegistry().resolve();
|
|
84
|
+
_providerRuntime = runtime ? { fingerprint, runtime } : null;
|
|
73
85
|
}
|
|
74
86
|
}
|
|
75
|
-
|
|
87
|
+
catch (error) {
|
|
88
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
89
|
+
(0, runtime_1.emitNervesEvent)({
|
|
90
|
+
level: "error",
|
|
91
|
+
event: "engine.provider_init_error",
|
|
92
|
+
component: "engine",
|
|
93
|
+
message: msg,
|
|
94
|
+
meta: {},
|
|
95
|
+
});
|
|
96
|
+
// eslint-disable-next-line no-console -- pre-boot guard: provider init failure
|
|
97
|
+
console.error(`\n[fatal] ${msg}\n`);
|
|
98
|
+
process.exit(1);
|
|
99
|
+
throw new Error("unreachable");
|
|
100
|
+
}
|
|
101
|
+
if (!_providerRuntime) {
|
|
102
|
+
(0, runtime_1.emitNervesEvent)({
|
|
103
|
+
level: "error",
|
|
104
|
+
event: "engine.provider_init_error",
|
|
105
|
+
component: "engine",
|
|
106
|
+
message: "provider runtime could not be initialized.",
|
|
107
|
+
meta: {},
|
|
108
|
+
});
|
|
109
|
+
process.exit(1);
|
|
110
|
+
throw new Error("unreachable");
|
|
111
|
+
}
|
|
112
|
+
return _providerRuntime.runtime;
|
|
76
113
|
}
|
|
77
114
|
/**
|
|
78
|
-
* Clear the cached provider runtime so the next
|
|
79
|
-
*
|
|
80
|
-
*
|
|
115
|
+
* Clear the cached provider runtime so the next access re-creates it from
|
|
116
|
+
* current config. Runtime access also auto-refreshes when the selected
|
|
117
|
+
* provider fingerprint changes on disk.
|
|
81
118
|
*/
|
|
82
119
|
function resetProviderRuntime() {
|
|
83
120
|
_providerRuntime = null;
|
|
@@ -104,14 +141,19 @@ function createSummarize() {
|
|
|
104
141
|
};
|
|
105
142
|
}
|
|
106
143
|
function getProviderDisplayLabel() {
|
|
107
|
-
const
|
|
144
|
+
const provider = (0, identity_1.loadAgentConfig)().provider;
|
|
108
145
|
const providerLabelBuilders = {
|
|
109
|
-
azure: () =>
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
146
|
+
azure: () => {
|
|
147
|
+
const config = (0, config_1.getAzureConfig)();
|
|
148
|
+
return `azure openai (${config.deployment || "default"}, model: ${config.modelName || "unknown"})`;
|
|
149
|
+
},
|
|
150
|
+
anthropic: () => `anthropic (${(0, config_1.getAnthropicConfig)().model || "unknown"})`,
|
|
151
|
+
minimax: () => `minimax (${(0, config_1.getMinimaxConfig)().model || "unknown"})`,
|
|
152
|
+
"openai-codex": () => `openai codex (${(0, config_1.getOpenAICodexConfig)().model || "unknown"})`,
|
|
153
|
+
/* v8 ignore next -- branch: tested via display label unit test @preserve */
|
|
154
|
+
"github-copilot": () => `github copilot (${(0, config_1.getGithubCopilotConfig)().model || "unknown"})`,
|
|
113
155
|
};
|
|
114
|
-
return providerLabelBuilders[
|
|
156
|
+
return providerLabelBuilders[provider]();
|
|
115
157
|
}
|
|
116
158
|
// Re-export tools, execTool, summarizeArgs from ./tools for backward compat
|
|
117
159
|
var tools_2 = require("../repertoire/tools");
|
|
@@ -128,6 +170,100 @@ Object.defineProperty(exports, "toResponsesTools", { enumerable: true, get: func
|
|
|
128
170
|
// Re-export prompt functions for backward compat
|
|
129
171
|
var prompt_2 = require("../mind/prompt");
|
|
130
172
|
Object.defineProperty(exports, "buildSystem", { enumerable: true, get: function () { return prompt_2.buildSystem; } });
|
|
173
|
+
const DELEGATION_REASON_PROSE_HANDOFF = {
|
|
174
|
+
explicit_reflection: "something in the conversation called for reflection",
|
|
175
|
+
cross_session: "this touches other conversations",
|
|
176
|
+
bridge_state: "there's shared work spanning sessions",
|
|
177
|
+
task_state: "there are active tasks that relate to this",
|
|
178
|
+
non_fast_path_tool: "this needs tools beyond a simple reply",
|
|
179
|
+
unresolved_obligation: "there's an unresolved commitment from an earlier conversation",
|
|
180
|
+
};
|
|
181
|
+
function buildGoInwardHandoffPacket(params) {
|
|
182
|
+
const reasons = params.delegationDecision?.reasons ?? [];
|
|
183
|
+
const reasonProse = reasons.length > 0
|
|
184
|
+
? reasons.map((r) => DELEGATION_REASON_PROSE_HANDOFF[r]).join("; ")
|
|
185
|
+
: "this felt like it needed more thought";
|
|
186
|
+
const returnAddress = params.currentSession
|
|
187
|
+
? `${params.currentSession.friendId}/${params.currentSession.channel}/${params.currentSession.key}`
|
|
188
|
+
: "no specific return -- just thinking";
|
|
189
|
+
let obligationLine;
|
|
190
|
+
if (params.outwardClosureRequired && params.currentSession) {
|
|
191
|
+
obligationLine = `i need to come back to ${params.currentSession.friendId} with something`;
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
obligationLine = "no obligation -- just thinking";
|
|
195
|
+
}
|
|
196
|
+
return [
|
|
197
|
+
"## what i need to think about",
|
|
198
|
+
params.content,
|
|
199
|
+
"",
|
|
200
|
+
"## why this came up",
|
|
201
|
+
reasonProse,
|
|
202
|
+
"",
|
|
203
|
+
"## where to bring it back",
|
|
204
|
+
returnAddress,
|
|
205
|
+
"",
|
|
206
|
+
"## what i owe",
|
|
207
|
+
obligationLine,
|
|
208
|
+
"",
|
|
209
|
+
"## thinking mode",
|
|
210
|
+
params.mode,
|
|
211
|
+
].join("\n");
|
|
212
|
+
}
|
|
213
|
+
function parseFinalAnswerPayload(argumentsText) {
|
|
214
|
+
try {
|
|
215
|
+
const parsed = JSON.parse(argumentsText);
|
|
216
|
+
if (typeof parsed === "string") {
|
|
217
|
+
return { answer: parsed };
|
|
218
|
+
}
|
|
219
|
+
if (!parsed || typeof parsed !== "object") {
|
|
220
|
+
return {};
|
|
221
|
+
}
|
|
222
|
+
const answer = typeof parsed.answer === "string" ? parsed.answer : undefined;
|
|
223
|
+
const rawIntent = parsed.intent;
|
|
224
|
+
const intent = rawIntent === "complete" || rawIntent === "blocked" || rawIntent === "direct_reply"
|
|
225
|
+
? rawIntent
|
|
226
|
+
: undefined;
|
|
227
|
+
return { answer, intent };
|
|
228
|
+
}
|
|
229
|
+
catch {
|
|
230
|
+
return {};
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
/** Returns true when a tool call queries external state (GitHub, npm registry). */
|
|
234
|
+
function isExternalStateQuery(toolName, args) {
|
|
235
|
+
if (toolName !== "shell")
|
|
236
|
+
return false;
|
|
237
|
+
const cmd = String(args.command ?? "");
|
|
238
|
+
return /\bgh\s+(pr|run|api|issue)\b/.test(cmd) || /\bnpm\s+(view|info|show)\b/.test(cmd);
|
|
239
|
+
}
|
|
240
|
+
function getFinalAnswerRetryError(mustResolveBeforeHandoff, intent, sawSteeringFollowUp, _delegationDecision, sawSendMessageSelf, sawGoInward, _sawQuerySession, currentObligation, innerJob, sawExternalStateQuery) {
|
|
241
|
+
// Delegation adherence removed: the delegation decision is surfaced in the
|
|
242
|
+
// system prompt as a suggestion. Hard-gating final_answer caused infinite
|
|
243
|
+
// rejection loops where the agent couldn't respond to the user at all.
|
|
244
|
+
// The agent is free to follow or ignore the delegation hint.
|
|
245
|
+
// 2. Pending obligation not addressed
|
|
246
|
+
if (innerJob?.obligationStatus === "pending" && !sawSendMessageSelf && !sawGoInward) {
|
|
247
|
+
return "you're still holding something from an earlier conversation -- someone is waiting for your answer. finish the thought first, or go_inward to keep working on it privately.";
|
|
248
|
+
}
|
|
249
|
+
// 3. mustResolveBeforeHandoff + missing intent
|
|
250
|
+
if (mustResolveBeforeHandoff && !intent) {
|
|
251
|
+
return "your final_answer is missing required intent. when you must keep going until done or blocked, call final_answer again with answer plus intent=complete, blocked, or direct_reply.";
|
|
252
|
+
}
|
|
253
|
+
// 4. mustResolveBeforeHandoff + direct_reply without follow-up
|
|
254
|
+
if (mustResolveBeforeHandoff && intent === "direct_reply" && !sawSteeringFollowUp) {
|
|
255
|
+
return "your final_answer used intent=direct_reply without a newer steering follow-up. continue the unresolved work, or call final_answer again with intent=complete or blocked when appropriate.";
|
|
256
|
+
}
|
|
257
|
+
// 5. mustResolveBeforeHandoff + complete while a live return loop is still active
|
|
258
|
+
if (mustResolveBeforeHandoff && intent === "complete" && currentObligation && !sawSteeringFollowUp) {
|
|
259
|
+
return "you still owe the live session a visible return on this work. don't end the turn yet — continue until you've brought back the external-state update, or use intent=blocked with the concrete blocker.";
|
|
260
|
+
}
|
|
261
|
+
// 6. External-state grounding: obligation + complete requires fresh external verification
|
|
262
|
+
if (intent === "complete" && currentObligation && !sawExternalStateQuery && !sawSteeringFollowUp) {
|
|
263
|
+
return "you're claiming this work is complete, but the external state hasn't been verified this turn. ground your claim with a fresh check (gh pr view, npm view, gh run view, etc.) before calling final_answer.";
|
|
264
|
+
}
|
|
265
|
+
return null;
|
|
266
|
+
}
|
|
131
267
|
// Re-export kick utilities for backward compat
|
|
132
268
|
var kicks_1 = require("./kicks");
|
|
133
269
|
Object.defineProperty(exports, "hasToolIntent", { enumerable: true, get: function () { return kicks_1.hasToolIntent; } });
|
|
@@ -160,6 +296,68 @@ function stripLastToolCalls(messages) {
|
|
|
160
296
|
}
|
|
161
297
|
}
|
|
162
298
|
}
|
|
299
|
+
// Roles that end a tool-result scan. When scanning forward from an assistant
|
|
300
|
+
// message, stop at the next assistant or user message (tool results must be
|
|
301
|
+
// adjacent to their originating assistant message).
|
|
302
|
+
const TOOL_SCAN_BOUNDARY_ROLES = new Set(["assistant", "user"]);
|
|
303
|
+
// Repair orphaned tool_calls and tool results anywhere in the message history.
|
|
304
|
+
// 1. If an assistant message has tool_calls but missing tool results, inject synthetic error results.
|
|
305
|
+
// 2. If a tool result's tool_call_id doesn't match any tool_calls in a preceding assistant message, remove it.
|
|
306
|
+
// This prevents 400 errors from the API after an aborted turn.
|
|
307
|
+
function repairOrphanedToolCalls(messages) {
|
|
308
|
+
// Pass 1: collect all valid tool_call IDs from assistant messages
|
|
309
|
+
const validCallIds = new Set();
|
|
310
|
+
for (const msg of messages) {
|
|
311
|
+
if (msg.role === "assistant") {
|
|
312
|
+
const asst = msg;
|
|
313
|
+
if (asst.tool_calls) {
|
|
314
|
+
for (const tc of asst.tool_calls)
|
|
315
|
+
validCallIds.add(tc.id);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
// Pass 2: remove orphaned tool results (tool_call_id not in any assistant's tool_calls)
|
|
320
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
321
|
+
if (messages[i].role === "tool") {
|
|
322
|
+
const toolMsg = messages[i];
|
|
323
|
+
if (!validCallIds.has(toolMsg.tool_call_id)) {
|
|
324
|
+
messages.splice(i, 1);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
// Pass 3: inject synthetic results for tool_calls missing their tool results
|
|
329
|
+
for (let i = 0; i < messages.length; i++) {
|
|
330
|
+
const msg = messages[i];
|
|
331
|
+
if (msg.role !== "assistant")
|
|
332
|
+
continue;
|
|
333
|
+
const asst = msg;
|
|
334
|
+
if (!asst.tool_calls || asst.tool_calls.length === 0)
|
|
335
|
+
continue;
|
|
336
|
+
// Collect tool result IDs that follow this assistant message
|
|
337
|
+
const resultIds = new Set();
|
|
338
|
+
for (let j = i + 1; j < messages.length; j++) {
|
|
339
|
+
const following = messages[j];
|
|
340
|
+
if (following.role === "tool") {
|
|
341
|
+
resultIds.add(following.tool_call_id);
|
|
342
|
+
}
|
|
343
|
+
else if (TOOL_SCAN_BOUNDARY_ROLES.has(following.role)) {
|
|
344
|
+
break;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
const missing = asst.tool_calls.filter((tc) => !resultIds.has(tc.id));
|
|
348
|
+
if (missing.length > 0) {
|
|
349
|
+
const syntheticResults = missing.map((tc) => ({
|
|
350
|
+
role: "tool",
|
|
351
|
+
tool_call_id: tc.id,
|
|
352
|
+
content: "error: tool call was interrupted (previous turn timed out or was aborted)",
|
|
353
|
+
}));
|
|
354
|
+
let insertAt = i + 1;
|
|
355
|
+
while (insertAt < messages.length && messages[insertAt].role === "tool")
|
|
356
|
+
insertAt++;
|
|
357
|
+
messages.splice(insertAt, 0, ...syntheticResults);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
163
361
|
// Detect context overflow errors from Azure or MiniMax
|
|
164
362
|
function isContextOverflow(err) {
|
|
165
363
|
if (!(err instanceof Error))
|
|
@@ -215,6 +413,14 @@ function classifyTransientError(err) {
|
|
|
215
413
|
}
|
|
216
414
|
const MAX_RETRIES = 3;
|
|
217
415
|
const RETRY_BASE_MS = 2000;
|
|
416
|
+
const TRANSIENT_RETRY_LABELS = {
|
|
417
|
+
"auth-failure": "auth error",
|
|
418
|
+
"usage-limit": "usage limit",
|
|
419
|
+
"rate-limit": "rate limited",
|
|
420
|
+
"server-error": "server error",
|
|
421
|
+
"network-error": "network error",
|
|
422
|
+
"unknown": "error",
|
|
423
|
+
};
|
|
218
424
|
async function runAgent(messages, callbacks, channel, signal, options) {
|
|
219
425
|
const providerRuntime = getProviderRuntime();
|
|
220
426
|
const provider = providerRuntime.id;
|
|
@@ -242,7 +448,12 @@ async function runAgent(messages, callbacks, channel, signal, options) {
|
|
|
242
448
|
// so turn execution remains consistent and non-fatal.
|
|
243
449
|
if (channel) {
|
|
244
450
|
try {
|
|
245
|
-
const
|
|
451
|
+
const buildSystemOptions = {
|
|
452
|
+
...options,
|
|
453
|
+
providerCapabilities: providerRuntime.capabilities,
|
|
454
|
+
supportedReasoningEfforts: providerRuntime.supportedReasoningEfforts,
|
|
455
|
+
};
|
|
456
|
+
const refreshed = await (0, prompt_1.buildSystem)(channel, buildSystemOptions, currentContext);
|
|
246
457
|
upsertSystemPrompt(messages, refreshed);
|
|
247
458
|
}
|
|
248
459
|
catch (error) {
|
|
@@ -265,20 +476,39 @@ async function runAgent(messages, callbacks, channel, signal, options) {
|
|
|
265
476
|
}
|
|
266
477
|
}
|
|
267
478
|
await (0, associative_recall_1.injectAssociativeRecall)(messages);
|
|
268
|
-
// kickCount and lastKickReason preserved but unused while kick detection is disabled.
|
|
269
|
-
// let kickCount = 0;
|
|
270
|
-
// let lastKickReason: KickReason | null = null;
|
|
271
479
|
let done = false;
|
|
272
480
|
let lastUsage;
|
|
273
481
|
let overflowRetried = false;
|
|
274
482
|
let retryCount = 0;
|
|
483
|
+
let outcome = "complete";
|
|
484
|
+
let completion;
|
|
485
|
+
let terminalError;
|
|
486
|
+
let terminalErrorClassification;
|
|
487
|
+
let sawSteeringFollowUp = false;
|
|
488
|
+
let mustResolveBeforeHandoffActive = options?.mustResolveBeforeHandoff === true;
|
|
489
|
+
let currentReasoningEffort = "medium";
|
|
490
|
+
let sawSendMessageSelf = false;
|
|
491
|
+
let sawGoInward = false;
|
|
492
|
+
let sawQuerySession = false;
|
|
493
|
+
let sawBridgeManage = false;
|
|
494
|
+
let sawExternalStateQuery = false;
|
|
495
|
+
const toolLoopState = (0, tool_loop_1.createToolLoopState)();
|
|
275
496
|
// Prevent MaxListenersExceeded warning — each iteration adds a listener
|
|
276
497
|
try {
|
|
277
498
|
require("events").setMaxListeners(50, signal);
|
|
278
499
|
}
|
|
279
500
|
catch { /* unsupported */ }
|
|
280
501
|
const toolPreferences = currentContext?.friend?.toolPreferences;
|
|
281
|
-
const baseTools = options?.tools ?? (0, tools_1.getToolsForChannel)(channel ? (0, channel_1.getChannelCapabilities)(channel) : undefined, toolPreferences && Object.keys(toolPreferences).length > 0 ? toolPreferences : undefined);
|
|
502
|
+
const baseTools = options?.tools ?? (0, tools_1.getToolsForChannel)(channel ? (0, channel_1.getChannelCapabilities)(channel) : undefined, toolPreferences && Object.keys(toolPreferences).length > 0 ? toolPreferences : undefined, currentContext, providerRuntime.capabilities);
|
|
503
|
+
// Augment tool context with reasoning effort controls from provider
|
|
504
|
+
const augmentedToolContext = options?.toolContext
|
|
505
|
+
? {
|
|
506
|
+
...options.toolContext,
|
|
507
|
+
supportedReasoningEfforts: providerRuntime.supportedReasoningEfforts,
|
|
508
|
+
setReasoningEffort: (level) => { currentReasoningEffort = level; },
|
|
509
|
+
activeWorkFrame: options?.activeWorkFrame,
|
|
510
|
+
}
|
|
511
|
+
: undefined;
|
|
282
512
|
// Rebase provider-owned turn state from canonical messages at user-turn start.
|
|
283
513
|
// This prevents stale provider caches from replaying prior-turn context.
|
|
284
514
|
providerRuntime.resetTurnState(messages);
|
|
@@ -287,9 +517,23 @@ async function runAgent(messages, callbacks, channel, signal, options) {
|
|
|
287
517
|
// so the model can signal completion. With tool_choice: required, the
|
|
288
518
|
// model must call a tool every turn — final_answer is how it exits.
|
|
289
519
|
// Overridable via options.toolChoiceRequired = false (e.g. CLI).
|
|
290
|
-
const activeTools = toolChoiceRequired
|
|
520
|
+
const activeTools = toolChoiceRequired
|
|
521
|
+
? [...baseTools, tools_1.goInwardTool, ...(currentContext?.isGroupChat ? [tools_1.noResponseTool] : []), tools_1.finalAnswerTool]
|
|
522
|
+
: baseTools;
|
|
291
523
|
const steeringFollowUps = options?.drainSteeringFollowUps?.() ?? [];
|
|
292
524
|
if (steeringFollowUps.length > 0) {
|
|
525
|
+
const hasSupersedingFollowUp = steeringFollowUps.some((followUp) => followUp.effect === "clear_and_supersede");
|
|
526
|
+
if (hasSupersedingFollowUp) {
|
|
527
|
+
mustResolveBeforeHandoffActive = false;
|
|
528
|
+
options?.setMustResolveBeforeHandoff?.(false);
|
|
529
|
+
outcome = "superseded";
|
|
530
|
+
break;
|
|
531
|
+
}
|
|
532
|
+
if (steeringFollowUps.some((followUp) => followUp.effect === "set_no_handoff")) {
|
|
533
|
+
mustResolveBeforeHandoffActive = true;
|
|
534
|
+
options?.setMustResolveBeforeHandoff?.(true);
|
|
535
|
+
}
|
|
536
|
+
sawSteeringFollowUp = true;
|
|
293
537
|
for (const followUp of steeringFollowUps) {
|
|
294
538
|
messages.push({ role: "user", content: followUp.text });
|
|
295
539
|
}
|
|
@@ -297,8 +541,10 @@ async function runAgent(messages, callbacks, channel, signal, options) {
|
|
|
297
541
|
}
|
|
298
542
|
// Yield so pending I/O (stdin Ctrl-C) can be processed between iterations
|
|
299
543
|
await new Promise((r) => setImmediate(r));
|
|
300
|
-
if (signal?.aborted)
|
|
544
|
+
if (signal?.aborted) {
|
|
545
|
+
outcome = "aborted";
|
|
301
546
|
break;
|
|
547
|
+
}
|
|
302
548
|
try {
|
|
303
549
|
callbacks.onModelStart();
|
|
304
550
|
const result = await providerRuntime.streamTurn({
|
|
@@ -308,6 +554,8 @@ async function runAgent(messages, callbacks, channel, signal, options) {
|
|
|
308
554
|
signal,
|
|
309
555
|
traceId,
|
|
310
556
|
toolChoiceRequired,
|
|
557
|
+
reasoningEffort: currentReasoningEffort,
|
|
558
|
+
eagerFinalAnswerStreaming: true,
|
|
311
559
|
});
|
|
312
560
|
// Track usage from the latest API call
|
|
313
561
|
if (result.usage)
|
|
@@ -331,49 +579,42 @@ async function runAgent(messages, callbacks, channel, signal, options) {
|
|
|
331
579
|
if (reasoningItems.length > 0) {
|
|
332
580
|
msg._reasoning_items = reasoningItems;
|
|
333
581
|
}
|
|
582
|
+
// Store thinking blocks (Anthropic) on the assistant message for round-tripping
|
|
583
|
+
const thinkingItems = result.outputItems.filter((item) => "type" in item && (item.type === "thinking" || item.type === "redacted_thinking"));
|
|
584
|
+
if (thinkingItems.length > 0) {
|
|
585
|
+
msg._thinking_blocks = thinkingItems;
|
|
586
|
+
}
|
|
587
|
+
// Phase annotation for Codex provider
|
|
588
|
+
const hasPhaseAnnotation = providerRuntime.capabilities.has("phase-annotation");
|
|
589
|
+
const isSoleFinalAnswer = result.toolCalls.length === 1 && result.toolCalls[0].name === "final_answer";
|
|
590
|
+
if (hasPhaseAnnotation) {
|
|
591
|
+
msg.phase = isSoleFinalAnswer ? "final_answer" : "commentary";
|
|
592
|
+
}
|
|
334
593
|
if (!result.toolCalls.length) {
|
|
335
|
-
//
|
|
336
|
-
//
|
|
337
|
-
//
|
|
338
|
-
// accept the response as-is rather than risk false-positive kicks.
|
|
339
|
-
//
|
|
340
|
-
// Preserved for future use — re-enable by uncommenting:
|
|
341
|
-
// const kick = detectKick(result.content, options);
|
|
342
|
-
// if (kick) {
|
|
343
|
-
// kickCount++;
|
|
344
|
-
// lastKickReason = kick.reason;
|
|
345
|
-
// callbacks.onKick?.();
|
|
346
|
-
// const kickContent = result.content
|
|
347
|
-
// ? result.content + "\n\n" + kick.message
|
|
348
|
-
// : kick.message;
|
|
349
|
-
// messages.push({ role: "assistant", content: kickContent });
|
|
350
|
-
// providerRuntime.resetTurnState(messages);
|
|
351
|
-
// continue;
|
|
352
|
-
// }
|
|
594
|
+
// No tool calls — accept response as-is.
|
|
595
|
+
// (Kick detection disabled; tool_choice: required + final_answer
|
|
596
|
+
// is the primary loop control. See src/heart/kicks.ts to re-enable.)
|
|
353
597
|
messages.push(msg);
|
|
354
598
|
done = true;
|
|
355
599
|
}
|
|
356
600
|
else {
|
|
357
601
|
// Check for final_answer sole call: intercept before tool execution
|
|
358
|
-
const isSoleFinalAnswer = result.toolCalls.length === 1 && result.toolCalls[0].name === "final_answer";
|
|
359
602
|
if (isSoleFinalAnswer) {
|
|
360
603
|
// Extract answer from the tool call arguments.
|
|
361
|
-
// Supports: {"answer":"text"}
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
}
|
|
376
|
-
if (answer != null) {
|
|
604
|
+
// Supports: {"answer":"text","intent":"..."} or "text" (JSON string).
|
|
605
|
+
const { answer, intent } = parseFinalAnswerPayload(result.toolCalls[0].arguments);
|
|
606
|
+
const retryError = getFinalAnswerRetryError(mustResolveBeforeHandoffActive, intent, sawSteeringFollowUp, options?.delegationDecision, sawSendMessageSelf, sawGoInward, sawQuerySession, options?.currentObligation ?? null, options?.activeWorkFrame?.inner?.job, sawExternalStateQuery);
|
|
607
|
+
const deliveredAnswer = answer;
|
|
608
|
+
const validDirectReply = mustResolveBeforeHandoffActive && intent === "direct_reply" && sawSteeringFollowUp;
|
|
609
|
+
const validTerminalIntent = intent === "complete" || intent === "blocked";
|
|
610
|
+
const validClosure = deliveredAnswer != null
|
|
611
|
+
&& !retryError
|
|
612
|
+
&& (!mustResolveBeforeHandoffActive || validDirectReply || validTerminalIntent);
|
|
613
|
+
if (validClosure) {
|
|
614
|
+
completion = {
|
|
615
|
+
answer: deliveredAnswer,
|
|
616
|
+
intent: validDirectReply ? "direct_reply" : intent === "blocked" ? "blocked" : "complete",
|
|
617
|
+
};
|
|
377
618
|
if (result.finalAnswerStreamed) {
|
|
378
619
|
// The streaming layer already parsed and emitted the answer
|
|
379
620
|
// progressively via FinalAnswerParser. Skip clearing and
|
|
@@ -384,29 +625,145 @@ async function runAgent(messages, callbacks, channel, signal, options) {
|
|
|
384
625
|
callbacks.onClearText?.();
|
|
385
626
|
// Emit the answer through the callback pipeline so channels receive it.
|
|
386
627
|
// Never truncate -- channel adapters handle splitting long messages.
|
|
387
|
-
callbacks.onTextChunk(
|
|
628
|
+
callbacks.onTextChunk(deliveredAnswer);
|
|
388
629
|
}
|
|
389
|
-
// Keep the full assistant message (with tool_calls) for debuggability,
|
|
390
|
-
// plus a synthetic tool response so the conversation stays valid on resume.
|
|
391
630
|
messages.push(msg);
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
631
|
+
if (validDirectReply) {
|
|
632
|
+
const resumeWork = "direct reply delivered. resume the unresolved obligation now and keep working until you can finish or clearly report that you are blocked.";
|
|
633
|
+
messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: resumeWork });
|
|
634
|
+
providerRuntime.appendToolOutput(result.toolCalls[0].id, resumeWork);
|
|
635
|
+
}
|
|
636
|
+
else {
|
|
637
|
+
const delivered = "(delivered)";
|
|
638
|
+
messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: delivered });
|
|
639
|
+
providerRuntime.appendToolOutput(result.toolCalls[0].id, delivered);
|
|
640
|
+
outcome = intent === "blocked" ? "blocked" : "complete";
|
|
641
|
+
done = true;
|
|
642
|
+
}
|
|
395
643
|
}
|
|
396
644
|
else {
|
|
397
645
|
// Answer is undefined -- the model's final_answer was incomplete or
|
|
398
646
|
// malformed. Clear any partial streamed text or noise, then push the
|
|
399
647
|
// assistant msg + error tool result and let the model try again.
|
|
400
648
|
callbacks.onClearText?.();
|
|
401
|
-
const retryError = "your final_answer was incomplete or malformed. call final_answer again with your complete response.";
|
|
402
649
|
messages.push(msg);
|
|
403
|
-
|
|
404
|
-
|
|
650
|
+
const toolRetryMessage = retryError
|
|
651
|
+
?? "your final_answer was incomplete or malformed. call final_answer again with your complete response.";
|
|
652
|
+
messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: toolRetryMessage });
|
|
653
|
+
providerRuntime.appendToolOutput(result.toolCalls[0].id, toolRetryMessage);
|
|
654
|
+
}
|
|
655
|
+
continue;
|
|
656
|
+
}
|
|
657
|
+
// Check for no_response sole call: intercept before tool execution
|
|
658
|
+
const isSoleNoResponse = result.toolCalls.length === 1 && result.toolCalls[0].name === "no_response";
|
|
659
|
+
if (isSoleNoResponse) {
|
|
660
|
+
let reason;
|
|
661
|
+
try {
|
|
662
|
+
const parsed = JSON.parse(result.toolCalls[0].arguments);
|
|
663
|
+
if (typeof parsed?.reason === "string")
|
|
664
|
+
reason = parsed.reason;
|
|
405
665
|
}
|
|
666
|
+
catch { /* ignore */ }
|
|
667
|
+
(0, runtime_1.emitNervesEvent)({
|
|
668
|
+
component: "engine",
|
|
669
|
+
event: "engine.no_response",
|
|
670
|
+
message: "agent declined to respond in group chat",
|
|
671
|
+
meta: { ...(reason ? { reason } : {}) },
|
|
672
|
+
});
|
|
673
|
+
messages.push(msg);
|
|
674
|
+
const silenced = "(silenced)";
|
|
675
|
+
messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: silenced });
|
|
676
|
+
providerRuntime.appendToolOutput(result.toolCalls[0].id, silenced);
|
|
677
|
+
outcome = "no_response";
|
|
678
|
+
done = true;
|
|
679
|
+
continue;
|
|
680
|
+
}
|
|
681
|
+
// Check for go_inward sole call: intercept before tool execution
|
|
682
|
+
const isSoleGoInward = result.toolCalls.length === 1 && result.toolCalls[0].name === "go_inward";
|
|
683
|
+
if (isSoleGoInward) {
|
|
684
|
+
let parsedArgs = {};
|
|
685
|
+
try {
|
|
686
|
+
parsedArgs = JSON.parse(result.toolCalls[0].arguments);
|
|
687
|
+
}
|
|
688
|
+
catch { /* ignore */ }
|
|
689
|
+
/* v8 ignore next -- defensive: content always string from model @preserve */
|
|
690
|
+
const content = typeof parsedArgs.content === "string" ? parsedArgs.content : "";
|
|
691
|
+
const answer = typeof parsedArgs.answer === "string" ? parsedArgs.answer : undefined;
|
|
692
|
+
const parsedMode = parsedArgs.mode === "reflect" || parsedArgs.mode === "plan" || parsedArgs.mode === "relay"
|
|
693
|
+
? parsedArgs.mode
|
|
694
|
+
: undefined;
|
|
695
|
+
const mode = parsedMode || "reflect";
|
|
696
|
+
// Emit outward answer if provided
|
|
697
|
+
if (answer) {
|
|
698
|
+
callbacks.onClearText?.();
|
|
699
|
+
callbacks.onTextChunk(answer);
|
|
700
|
+
}
|
|
701
|
+
// Build handoff packet and enqueue
|
|
702
|
+
const handoffContent = buildGoInwardHandoffPacket({
|
|
703
|
+
content,
|
|
704
|
+
mode,
|
|
705
|
+
delegationDecision: options?.delegationDecision,
|
|
706
|
+
currentSession: options?.toolContext?.currentSession ?? null,
|
|
707
|
+
currentObligation: options?.currentObligation ?? null,
|
|
708
|
+
outwardClosureRequired: options?.delegationDecision?.outwardClosureRequired ?? false,
|
|
709
|
+
});
|
|
710
|
+
const pendingDir = (0, pending_1.getInnerDialogPendingDir)((0, identity_2.getAgentName)());
|
|
711
|
+
const currentSession = options?.toolContext?.currentSession;
|
|
712
|
+
const isInnerChannel = currentSession?.friendId === "self" && currentSession?.channel === "inner";
|
|
713
|
+
const envelope = {
|
|
714
|
+
from: (0, identity_2.getAgentName)(),
|
|
715
|
+
friendId: "self",
|
|
716
|
+
channel: "inner",
|
|
717
|
+
key: "dialog",
|
|
718
|
+
content: handoffContent,
|
|
719
|
+
timestamp: Date.now(),
|
|
720
|
+
mode,
|
|
721
|
+
...(currentSession && !isInnerChannel ? {
|
|
722
|
+
delegatedFrom: {
|
|
723
|
+
friendId: currentSession.friendId,
|
|
724
|
+
channel: currentSession.channel,
|
|
725
|
+
key: currentSession.key,
|
|
726
|
+
},
|
|
727
|
+
obligationStatus: "pending",
|
|
728
|
+
} : {}),
|
|
729
|
+
};
|
|
730
|
+
(0, pending_1.queuePendingMessage)(pendingDir, envelope);
|
|
731
|
+
if (currentSession && !isInnerChannel) {
|
|
732
|
+
try {
|
|
733
|
+
(0, obligations_1.createObligation)((0, identity_2.getAgentRoot)(), {
|
|
734
|
+
origin: {
|
|
735
|
+
friendId: currentSession.friendId,
|
|
736
|
+
channel: currentSession.channel,
|
|
737
|
+
key: currentSession.key,
|
|
738
|
+
},
|
|
739
|
+
content,
|
|
740
|
+
});
|
|
741
|
+
}
|
|
742
|
+
catch {
|
|
743
|
+
/* v8 ignore next -- defensive: obligation store write failure should not break go_inward @preserve */
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
try {
|
|
747
|
+
await (0, socket_client_1.requestInnerWake)((0, identity_2.getAgentName)());
|
|
748
|
+
}
|
|
749
|
+
catch { /* daemon may not be running */ }
|
|
750
|
+
sawGoInward = true;
|
|
751
|
+
messages.push(msg);
|
|
752
|
+
const ack = "(going inward)";
|
|
753
|
+
messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: ack });
|
|
754
|
+
providerRuntime.appendToolOutput(result.toolCalls[0].id, ack);
|
|
755
|
+
(0, runtime_1.emitNervesEvent)({
|
|
756
|
+
component: "engine",
|
|
757
|
+
event: "engine.go_inward",
|
|
758
|
+
message: "taking thread inward",
|
|
759
|
+
meta: { mode, hasAnswer: answer !== undefined, contentSnippet: content.slice(0, 80) },
|
|
760
|
+
});
|
|
761
|
+
outcome = "go_inward";
|
|
762
|
+
done = true;
|
|
406
763
|
continue;
|
|
407
764
|
}
|
|
408
765
|
messages.push(msg);
|
|
409
|
-
// SHARED: execute tools (final_answer in mixed calls
|
|
766
|
+
// SHARED: execute tools (final_answer, no_response, go_inward in mixed calls are rejected inline)
|
|
410
767
|
for (const tc of result.toolCalls) {
|
|
411
768
|
if (signal?.aborted)
|
|
412
769
|
break;
|
|
@@ -417,6 +774,20 @@ async function runAgent(messages, callbacks, channel, signal, options) {
|
|
|
417
774
|
providerRuntime.appendToolOutput(tc.id, rejection);
|
|
418
775
|
continue;
|
|
419
776
|
}
|
|
777
|
+
// Intercept no_response in mixed call: reject it
|
|
778
|
+
if (tc.name === "no_response") {
|
|
779
|
+
const rejection = "rejected: no_response must be the only tool call. call no_response alone when you want to stay silent.";
|
|
780
|
+
messages.push({ role: "tool", tool_call_id: tc.id, content: rejection });
|
|
781
|
+
providerRuntime.appendToolOutput(tc.id, rejection);
|
|
782
|
+
continue;
|
|
783
|
+
}
|
|
784
|
+
// Intercept go_inward in mixed call: reject it
|
|
785
|
+
if (tc.name === "go_inward") {
|
|
786
|
+
const rejection = "rejected: go_inward must be the only tool call. finish your other work first, then call go_inward alone.";
|
|
787
|
+
messages.push({ role: "tool", tool_call_id: tc.id, content: rejection });
|
|
788
|
+
providerRuntime.appendToolOutput(tc.id, rejection);
|
|
789
|
+
continue;
|
|
790
|
+
}
|
|
420
791
|
let args = {};
|
|
421
792
|
try {
|
|
422
793
|
args = JSON.parse(tc.arguments);
|
|
@@ -424,7 +795,28 @@ async function runAgent(messages, callbacks, channel, signal, options) {
|
|
|
424
795
|
catch {
|
|
425
796
|
/* ignore */
|
|
426
797
|
}
|
|
798
|
+
if (tc.name === "send_message" && args.friendId === "self") {
|
|
799
|
+
sawSendMessageSelf = true;
|
|
800
|
+
}
|
|
801
|
+
/* v8 ignore next -- flag tested via truth-check integration tests @preserve */
|
|
802
|
+
if (tc.name === "query_session")
|
|
803
|
+
sawQuerySession = true;
|
|
804
|
+
/* v8 ignore next -- flag tested via truth-check integration tests @preserve */
|
|
805
|
+
if (tc.name === "bridge_manage")
|
|
806
|
+
sawBridgeManage = true;
|
|
807
|
+
/* v8 ignore next -- flag tested via truth-check integration tests @preserve */
|
|
808
|
+
if (isExternalStateQuery(tc.name, args))
|
|
809
|
+
sawExternalStateQuery = true;
|
|
427
810
|
const argSummary = (0, tools_1.summarizeArgs)(tc.name, args);
|
|
811
|
+
const toolLoop = (0, tool_loop_1.detectToolLoop)(toolLoopState, tc.name, args);
|
|
812
|
+
if (toolLoop.stuck) {
|
|
813
|
+
const rejection = `loop guard: ${toolLoop.message}`;
|
|
814
|
+
callbacks.onToolStart(tc.name, args);
|
|
815
|
+
callbacks.onToolEnd(tc.name, argSummary, false);
|
|
816
|
+
messages.push({ role: "tool", tool_call_id: tc.id, content: rejection });
|
|
817
|
+
providerRuntime.appendToolOutput(tc.id, rejection);
|
|
818
|
+
continue;
|
|
819
|
+
}
|
|
428
820
|
// Confirmation check for mutate tools
|
|
429
821
|
if ((0, tools_1.isConfirmationRequired)(tc.name) && !options?.skipConfirmation) {
|
|
430
822
|
let decision = "denied";
|
|
@@ -445,13 +837,14 @@ async function runAgent(messages, callbacks, channel, signal, options) {
|
|
|
445
837
|
let success;
|
|
446
838
|
try {
|
|
447
839
|
const execToolFn = options?.execTool ?? tools_1.execTool;
|
|
448
|
-
toolResult = await execToolFn(tc.name, args, options?.toolContext);
|
|
840
|
+
toolResult = await execToolFn(tc.name, args, augmentedToolContext ?? options?.toolContext);
|
|
449
841
|
success = true;
|
|
450
842
|
}
|
|
451
843
|
catch (e) {
|
|
452
844
|
toolResult = `error: ${e}`;
|
|
453
845
|
success = false;
|
|
454
846
|
}
|
|
847
|
+
(0, tool_loop_1.recordToolOutcome)(toolLoopState, tc.name, args, toolResult, success);
|
|
455
848
|
callbacks.onToolEnd(tc.name, argSummary, success);
|
|
456
849
|
messages.push({ role: "tool", tool_call_id: tc.id, content: toolResult });
|
|
457
850
|
providerRuntime.appendToolOutput(tc.id, toolResult);
|
|
@@ -462,6 +855,7 @@ async function runAgent(messages, callbacks, channel, signal, options) {
|
|
|
462
855
|
// Abort is not an error — just stop cleanly
|
|
463
856
|
if (signal?.aborted) {
|
|
464
857
|
stripLastToolCalls(messages);
|
|
858
|
+
outcome = "aborted";
|
|
465
859
|
break;
|
|
466
860
|
}
|
|
467
861
|
// Context overflow: trim aggressively and retry once
|
|
@@ -479,7 +873,16 @@ async function runAgent(messages, callbacks, channel, signal, options) {
|
|
|
479
873
|
if (isTransientError(e) && retryCount < MAX_RETRIES) {
|
|
480
874
|
retryCount++;
|
|
481
875
|
const delay = RETRY_BASE_MS * Math.pow(2, retryCount - 1);
|
|
482
|
-
|
|
876
|
+
let classification;
|
|
877
|
+
try {
|
|
878
|
+
classification = providerRuntime.classifyError(e instanceof Error ? e : /* v8 ignore next -- defensive: errors are always Error instances @preserve */ new Error(String(e)));
|
|
879
|
+
}
|
|
880
|
+
catch {
|
|
881
|
+
/* v8 ignore next -- defensive: classifyError should not throw @preserve */
|
|
882
|
+
classification = classifyTransientError(e);
|
|
883
|
+
}
|
|
884
|
+
/* v8 ignore next -- defensive: all classifications have labels @preserve */
|
|
885
|
+
const cause = TRANSIENT_RETRY_LABELS[classification] ?? classification;
|
|
483
886
|
callbacks.onError(new Error(`${cause}, retrying in ${delay / 1000}s (${retryCount}/${MAX_RETRIES})...`), "transient");
|
|
484
887
|
// Wait with abort support
|
|
485
888
|
const aborted = await new Promise((resolve) => {
|
|
@@ -496,21 +899,31 @@ async function runAgent(messages, callbacks, channel, signal, options) {
|
|
|
496
899
|
});
|
|
497
900
|
if (aborted) {
|
|
498
901
|
stripLastToolCalls(messages);
|
|
902
|
+
outcome = "aborted";
|
|
499
903
|
break;
|
|
500
904
|
}
|
|
501
905
|
providerRuntime.resetTurnState(messages);
|
|
502
906
|
continue;
|
|
503
907
|
}
|
|
504
|
-
|
|
908
|
+
terminalError = e instanceof Error ? e : new Error(String(e));
|
|
909
|
+
try {
|
|
910
|
+
terminalErrorClassification = providerRuntime.classifyError(terminalError);
|
|
911
|
+
}
|
|
912
|
+
catch {
|
|
913
|
+
/* v8 ignore next -- defensive: classifyError should not throw @preserve */
|
|
914
|
+
terminalErrorClassification = "unknown";
|
|
915
|
+
}
|
|
916
|
+
callbacks.onError(terminalError, "terminal");
|
|
505
917
|
(0, runtime_1.emitNervesEvent)({
|
|
506
918
|
level: "error",
|
|
507
919
|
event: "engine.error",
|
|
508
920
|
trace_id: traceId,
|
|
509
921
|
component: "engine",
|
|
510
|
-
message:
|
|
511
|
-
meta: {},
|
|
922
|
+
message: terminalError.message,
|
|
923
|
+
meta: { errorClassification: terminalErrorClassification },
|
|
512
924
|
});
|
|
513
925
|
stripLastToolCalls(messages);
|
|
926
|
+
outcome = "errored";
|
|
514
927
|
done = true;
|
|
515
928
|
}
|
|
516
929
|
}
|
|
@@ -519,7 +932,12 @@ async function runAgent(messages, callbacks, channel, signal, options) {
|
|
|
519
932
|
trace_id: traceId,
|
|
520
933
|
component: "engine",
|
|
521
934
|
message: "runAgent turn completed",
|
|
522
|
-
meta: { done },
|
|
935
|
+
meta: { done, sawGoInward, sawQuerySession, sawBridgeManage },
|
|
523
936
|
});
|
|
524
|
-
return {
|
|
937
|
+
return {
|
|
938
|
+
usage: lastUsage,
|
|
939
|
+
outcome,
|
|
940
|
+
completion,
|
|
941
|
+
...(terminalError ? { error: terminalError, errorClassification: terminalErrorClassification } : {}),
|
|
942
|
+
};
|
|
525
943
|
}
|