@ouro.bot/cli 0.1.0-alpha.9 → 0.1.0-alpha.91

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.
Files changed (128) hide show
  1. package/AdoptionSpecialist.ouro/agent.json +70 -9
  2. package/AdoptionSpecialist.ouro/psyche/SOUL.md +5 -2
  3. package/AdoptionSpecialist.ouro/psyche/identities/monty.md +2 -2
  4. package/README.md +147 -205
  5. package/assets/ouroboros.png +0 -0
  6. package/changelog.json +536 -0
  7. package/dist/heart/active-work.js +251 -0
  8. package/dist/heart/bridges/manager.js +358 -0
  9. package/dist/heart/bridges/state-machine.js +135 -0
  10. package/dist/heart/bridges/store.js +123 -0
  11. package/dist/heart/commitments.js +109 -0
  12. package/dist/heart/config.js +68 -23
  13. package/dist/heart/core.js +452 -93
  14. package/dist/heart/cross-chat-delivery.js +146 -0
  15. package/dist/heart/daemon/agent-discovery.js +81 -0
  16. package/dist/heart/daemon/auth-flow.js +430 -0
  17. package/dist/heart/daemon/daemon-cli.js +1738 -269
  18. package/dist/heart/daemon/daemon-entry.js +55 -6
  19. package/dist/heart/daemon/daemon-runtime-sync.js +212 -0
  20. package/dist/heart/daemon/daemon.js +216 -10
  21. package/dist/heart/daemon/hatch-animation.js +10 -3
  22. package/dist/heart/daemon/hatch-flow.js +7 -82
  23. package/dist/heart/daemon/hooks/bundle-meta.js +92 -0
  24. package/dist/heart/daemon/launchd.js +159 -0
  25. package/dist/heart/daemon/log-tailer.js +4 -3
  26. package/dist/heart/daemon/message-router.js +17 -8
  27. package/dist/heart/daemon/ouro-bot-entry.js +0 -0
  28. package/dist/heart/daemon/ouro-bot-global-installer.js +128 -0
  29. package/dist/heart/daemon/ouro-entry.js +0 -0
  30. package/dist/heart/daemon/ouro-path-installer.js +260 -0
  31. package/dist/heart/daemon/ouro-uti.js +11 -2
  32. package/dist/heart/daemon/ouro-version-manager.js +171 -0
  33. package/dist/heart/daemon/process-manager.js +14 -1
  34. package/dist/heart/daemon/run-hooks.js +37 -0
  35. package/dist/heart/daemon/runtime-logging.js +58 -15
  36. package/dist/heart/daemon/runtime-metadata.js +219 -0
  37. package/dist/heart/daemon/runtime-mode.js +67 -0
  38. package/dist/heart/daemon/sense-manager.js +307 -0
  39. package/dist/heart/daemon/skill-management-installer.js +94 -0
  40. package/dist/heart/daemon/socket-client.js +202 -0
  41. package/dist/heart/daemon/specialist-orchestrator.js +53 -84
  42. package/dist/heart/daemon/specialist-prompt.js +63 -11
  43. package/dist/heart/daemon/specialist-tools.js +211 -60
  44. package/dist/heart/daemon/staged-restart.js +114 -0
  45. package/dist/heart/daemon/thoughts.js +507 -0
  46. package/dist/heart/daemon/update-checker.js +111 -0
  47. package/dist/heart/daemon/update-hooks.js +138 -0
  48. package/dist/heart/daemon/wrapper-publish-guard.js +86 -0
  49. package/dist/heart/delegation.js +62 -0
  50. package/dist/heart/identity.js +126 -21
  51. package/dist/heart/kicks.js +1 -19
  52. package/dist/heart/model-capabilities.js +48 -0
  53. package/dist/heart/obligations.js +191 -0
  54. package/dist/heart/progress-story.js +42 -0
  55. package/dist/heart/providers/anthropic.js +74 -9
  56. package/dist/heart/providers/azure.js +86 -7
  57. package/dist/heart/providers/github-copilot.js +149 -0
  58. package/dist/heart/providers/minimax.js +4 -0
  59. package/dist/heart/providers/openai-codex.js +12 -3
  60. package/dist/heart/safe-workspace.js +362 -0
  61. package/dist/heart/sense-truth.js +61 -0
  62. package/dist/heart/session-activity.js +169 -0
  63. package/dist/heart/session-recall.js +116 -0
  64. package/dist/heart/streaming.js +100 -22
  65. package/dist/heart/target-resolution.js +123 -0
  66. package/dist/heart/turn-coordinator.js +28 -0
  67. package/dist/mind/associative-recall.js +14 -2
  68. package/dist/mind/bundle-manifest.js +70 -0
  69. package/dist/mind/context.js +57 -11
  70. package/dist/mind/first-impressions.js +16 -2
  71. package/dist/mind/friends/channel.js +35 -0
  72. package/dist/mind/friends/group-context.js +144 -0
  73. package/dist/mind/friends/store-file.js +19 -0
  74. package/dist/mind/friends/trust-explanation.js +74 -0
  75. package/dist/mind/friends/types.js +8 -0
  76. package/dist/mind/memory.js +27 -26
  77. package/dist/mind/obligation-steering.js +31 -0
  78. package/dist/mind/pending.js +76 -9
  79. package/dist/mind/phrases.js +1 -0
  80. package/dist/mind/prompt.js +467 -77
  81. package/dist/mind/token-estimate.js +8 -12
  82. package/dist/nerves/cli-logging.js +15 -2
  83. package/dist/nerves/coverage/run-artifacts.js +1 -1
  84. package/dist/nerves/index.js +12 -0
  85. package/dist/repertoire/ado-client.js +4 -2
  86. package/dist/repertoire/coding/feedback.js +180 -0
  87. package/dist/repertoire/coding/index.js +4 -1
  88. package/dist/repertoire/coding/manager.js +69 -4
  89. package/dist/repertoire/coding/spawner.js +21 -3
  90. package/dist/repertoire/coding/tools.js +105 -2
  91. package/dist/repertoire/data/ado-endpoints.json +188 -0
  92. package/dist/repertoire/guardrails.js +290 -0
  93. package/dist/repertoire/mcp-client.js +254 -0
  94. package/dist/repertoire/mcp-manager.js +195 -0
  95. package/dist/repertoire/skills.js +3 -26
  96. package/dist/repertoire/tasks/board.js +12 -0
  97. package/dist/repertoire/tasks/index.js +23 -9
  98. package/dist/repertoire/tasks/transitions.js +1 -2
  99. package/dist/repertoire/tools-base.js +714 -249
  100. package/dist/repertoire/tools-bluebubbles.js +93 -0
  101. package/dist/repertoire/tools-teams.js +58 -25
  102. package/dist/repertoire/tools.js +106 -53
  103. package/dist/senses/bluebubbles-client.js +210 -5
  104. package/dist/senses/bluebubbles-entry.js +2 -0
  105. package/dist/senses/bluebubbles-inbound-log.js +109 -0
  106. package/dist/senses/bluebubbles-media.js +339 -0
  107. package/dist/senses/bluebubbles-model.js +12 -4
  108. package/dist/senses/bluebubbles-mutation-log.js +45 -5
  109. package/dist/senses/bluebubbles-runtime-state.js +109 -0
  110. package/dist/senses/bluebubbles-session-cleanup.js +72 -0
  111. package/dist/senses/bluebubbles.js +894 -45
  112. package/dist/senses/cli-layout.js +187 -0
  113. package/dist/senses/cli.js +400 -164
  114. package/dist/senses/continuity.js +94 -0
  115. package/dist/senses/debug-activity.js +154 -0
  116. package/dist/senses/inner-dialog-worker.js +47 -18
  117. package/dist/senses/inner-dialog.js +377 -83
  118. package/dist/senses/pipeline.js +307 -0
  119. package/dist/senses/teams.js +573 -129
  120. package/dist/senses/trust-gate.js +112 -2
  121. package/package.json +14 -3
  122. package/subagents/README.md +4 -70
  123. package/dist/heart/daemon/specialist-session.js +0 -142
  124. package/dist/heart/daemon/subagent-installer.js +0 -125
  125. package/dist/inner-worker-entry.js +0 -4
  126. package/subagents/work-doer.md +0 -233
  127. package/subagents/work-merger.md +0 -624
  128. package/subagents/work-planner.md +0 -373
@@ -7,7 +7,9 @@ exports.getModel = getModel;
7
7
  exports.getProvider = getProvider;
8
8
  exports.createSummarize = createSummarize;
9
9
  exports.getProviderDisplayLabel = getProviderDisplayLabel;
10
+ exports.getFinalAnswerRetryError = getFinalAnswerRetryError;
10
11
  exports.stripLastToolCalls = stripLastToolCalls;
12
+ exports.repairOrphanedToolCalls = repairOrphanedToolCalls;
11
13
  exports.isTransientError = isTransientError;
12
14
  exports.classifyTransientError = classifyTransientError;
13
15
  exports.runAgent = runAgent;
@@ -15,9 +17,6 @@ const config_1 = require("./config");
15
17
  const identity_1 = require("./identity");
16
18
  const tools_1 = require("../repertoire/tools");
17
19
  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
20
  const runtime_1 = require("../nerves/runtime");
22
21
  const context_1 = require("../mind/context");
23
22
  const prompt_1 = require("../mind/prompt");
@@ -26,13 +25,47 @@ const anthropic_1 = require("./providers/anthropic");
26
25
  const azure_1 = require("./providers/azure");
27
26
  const minimax_1 = require("./providers/minimax");
28
27
  const openai_codex_1 = require("./providers/openai-codex");
28
+ const github_copilot_1 = require("./providers/github-copilot");
29
+ const pending_1 = require("../mind/pending");
30
+ const identity_2 = require("./identity");
31
+ const socket_client_1 = require("./daemon/socket-client");
32
+ const obligations_1 = require("./obligations");
29
33
  let _providerRuntime = null;
34
+ function getProviderRuntimeFingerprint() {
35
+ const provider = (0, identity_1.loadAgentConfig)().provider;
36
+ /* v8 ignore next -- switch: not all provider branches exercised in CI @preserve */
37
+ switch (provider) {
38
+ case "azure": {
39
+ const { apiKey, endpoint, deployment, modelName, apiVersion, managedIdentityClientId } = (0, config_1.getAzureConfig)();
40
+ return JSON.stringify({ provider, apiKey, endpoint, deployment, modelName, apiVersion, managedIdentityClientId });
41
+ }
42
+ case "anthropic": {
43
+ const { model, setupToken } = (0, config_1.getAnthropicConfig)();
44
+ return JSON.stringify({ provider, model, setupToken });
45
+ }
46
+ case "minimax": {
47
+ const { apiKey, model } = (0, config_1.getMinimaxConfig)();
48
+ return JSON.stringify({ provider, apiKey, model });
49
+ }
50
+ case "openai-codex": {
51
+ const { model, oauthAccessToken } = (0, config_1.getOpenAICodexConfig)();
52
+ return JSON.stringify({ provider, model, oauthAccessToken });
53
+ }
54
+ /* v8 ignore start -- fingerprint: tested via provider init tests @preserve */
55
+ case "github-copilot": {
56
+ const { model, githubToken, baseUrl } = (0, config_1.getGithubCopilotConfig)();
57
+ return JSON.stringify({ provider, model, githubToken, baseUrl });
58
+ }
59
+ /* v8 ignore stop */
60
+ }
61
+ }
30
62
  function createProviderRegistry() {
31
63
  const factories = {
32
64
  azure: azure_1.createAzureProviderRuntime,
33
65
  anthropic: anthropic_1.createAnthropicProviderRuntime,
34
66
  minimax: minimax_1.createMinimaxProviderRuntime,
35
67
  "openai-codex": openai_codex_1.createOpenAICodexProviderRuntime,
68
+ "github-copilot": github_copilot_1.createGithubCopilotProviderRuntime,
36
69
  };
37
70
  return {
38
71
  resolve() {
@@ -42,42 +75,44 @@ function createProviderRegistry() {
42
75
  };
43
76
  }
44
77
  function getProviderRuntime() {
45
- if (!_providerRuntime) {
46
- try {
47
- _providerRuntime = createProviderRegistry().resolve();
48
- }
49
- catch (error) {
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");
78
+ try {
79
+ const fingerprint = getProviderRuntimeFingerprint();
80
+ if (!_providerRuntime || _providerRuntime.fingerprint !== fingerprint) {
81
+ const runtime = createProviderRegistry().resolve();
82
+ _providerRuntime = runtime ? { fingerprint, runtime } : null;
73
83
  }
74
84
  }
75
- return _providerRuntime;
85
+ catch (error) {
86
+ const msg = error instanceof Error ? error.message : String(error);
87
+ (0, runtime_1.emitNervesEvent)({
88
+ level: "error",
89
+ event: "engine.provider_init_error",
90
+ component: "engine",
91
+ message: msg,
92
+ meta: {},
93
+ });
94
+ // eslint-disable-next-line no-console -- pre-boot guard: provider init failure
95
+ console.error(`\n[fatal] ${msg}\n`);
96
+ process.exit(1);
97
+ throw new Error("unreachable");
98
+ }
99
+ if (!_providerRuntime) {
100
+ (0, runtime_1.emitNervesEvent)({
101
+ level: "error",
102
+ event: "engine.provider_init_error",
103
+ component: "engine",
104
+ message: "provider runtime could not be initialized.",
105
+ meta: {},
106
+ });
107
+ process.exit(1);
108
+ throw new Error("unreachable");
109
+ }
110
+ return _providerRuntime.runtime;
76
111
  }
77
112
  /**
78
- * Clear the cached provider runtime so the next call to getProviderRuntime()
79
- * re-creates it from current config. Used by the adoption specialist to
80
- * switch provider context without restarting the process.
113
+ * Clear the cached provider runtime so the next access re-creates it from
114
+ * current config. Runtime access also auto-refreshes when the selected
115
+ * provider fingerprint changes on disk.
81
116
  */
82
117
  function resetProviderRuntime() {
83
118
  _providerRuntime = null;
@@ -104,14 +139,19 @@ function createSummarize() {
104
139
  };
105
140
  }
106
141
  function getProviderDisplayLabel() {
107
- const model = getModel();
142
+ const provider = (0, identity_1.loadAgentConfig)().provider;
108
143
  const providerLabelBuilders = {
109
- azure: () => `azure openai (${(0, config_1.getAzureConfig)().deployment || "default"}, model: ${model})`,
110
- anthropic: () => `anthropic (${model})`,
111
- minimax: () => `minimax (${model})`,
112
- "openai-codex": () => `openai codex (${model})`,
144
+ azure: () => {
145
+ const config = (0, config_1.getAzureConfig)();
146
+ return `azure openai (${config.deployment || "default"}, model: ${config.modelName || "unknown"})`;
147
+ },
148
+ anthropic: () => `anthropic (${(0, config_1.getAnthropicConfig)().model || "unknown"})`,
149
+ minimax: () => `minimax (${(0, config_1.getMinimaxConfig)().model || "unknown"})`,
150
+ "openai-codex": () => `openai codex (${(0, config_1.getOpenAICodexConfig)().model || "unknown"})`,
151
+ /* v8 ignore next -- branch: tested via display label unit test @preserve */
152
+ "github-copilot": () => `github copilot (${(0, config_1.getGithubCopilotConfig)().model || "unknown"})`,
113
153
  };
114
- return providerLabelBuilders[getProvider()]();
154
+ return providerLabelBuilders[provider]();
115
155
  }
116
156
  // Re-export tools, execTool, summarizeArgs from ./tools for backward compat
117
157
  var tools_2 = require("../repertoire/tools");
@@ -128,6 +168,95 @@ Object.defineProperty(exports, "toResponsesTools", { enumerable: true, get: func
128
168
  // Re-export prompt functions for backward compat
129
169
  var prompt_2 = require("../mind/prompt");
130
170
  Object.defineProperty(exports, "buildSystem", { enumerable: true, get: function () { return prompt_2.buildSystem; } });
171
+ const DELEGATION_REASON_PROSE_HANDOFF = {
172
+ explicit_reflection: "something in the conversation called for reflection",
173
+ cross_session: "this touches other conversations",
174
+ bridge_state: "there's shared work spanning sessions",
175
+ task_state: "there are active tasks that relate to this",
176
+ non_fast_path_tool: "this needs tools beyond a simple reply",
177
+ unresolved_obligation: "there's an unresolved commitment from an earlier conversation",
178
+ };
179
+ function buildGoInwardHandoffPacket(params) {
180
+ const reasons = params.delegationDecision?.reasons ?? [];
181
+ const reasonProse = reasons.length > 0
182
+ ? reasons.map((r) => DELEGATION_REASON_PROSE_HANDOFF[r]).join("; ")
183
+ : "this felt like it needed more thought";
184
+ const returnAddress = params.currentSession
185
+ ? `${params.currentSession.friendId}/${params.currentSession.channel}/${params.currentSession.key}`
186
+ : "no specific return -- just thinking";
187
+ let obligationLine;
188
+ if (params.outwardClosureRequired && params.currentSession) {
189
+ obligationLine = `i need to come back to ${params.currentSession.friendId} with something`;
190
+ }
191
+ else {
192
+ obligationLine = "no obligation -- just thinking";
193
+ }
194
+ return [
195
+ "## what i need to think about",
196
+ params.content,
197
+ "",
198
+ "## why this came up",
199
+ reasonProse,
200
+ "",
201
+ "## where to bring it back",
202
+ returnAddress,
203
+ "",
204
+ "## what i owe",
205
+ obligationLine,
206
+ "",
207
+ "## thinking mode",
208
+ params.mode,
209
+ ].join("\n");
210
+ }
211
+ function parseFinalAnswerPayload(argumentsText) {
212
+ try {
213
+ const parsed = JSON.parse(argumentsText);
214
+ if (typeof parsed === "string") {
215
+ return { answer: parsed };
216
+ }
217
+ if (!parsed || typeof parsed !== "object") {
218
+ return {};
219
+ }
220
+ const answer = typeof parsed.answer === "string" ? parsed.answer : undefined;
221
+ const rawIntent = parsed.intent;
222
+ const intent = rawIntent === "complete" || rawIntent === "blocked" || rawIntent === "direct_reply"
223
+ ? rawIntent
224
+ : undefined;
225
+ return { answer, intent };
226
+ }
227
+ catch {
228
+ return {};
229
+ }
230
+ }
231
+ function getFinalAnswerRetryError(mustResolveBeforeHandoff, intent, sawSteeringFollowUp, delegationDecision, sawSendMessageSelf, sawGoInward, sawQuerySession, innerJob) {
232
+ // 1. Delegation adherence: delegate-inward without evidence of inward action
233
+ if (delegationDecision?.target === "delegate-inward" && !sawSendMessageSelf && !sawGoInward && !sawQuerySession) {
234
+ (0, runtime_1.emitNervesEvent)({
235
+ event: "engine.delegation_adherence_rejected",
236
+ component: "engine",
237
+ message: "delegation adherence check rejected final_answer",
238
+ meta: {
239
+ target: delegationDecision.target,
240
+ reasons: delegationDecision.reasons,
241
+ },
242
+ });
243
+ return "you're reaching for a final answer, but part of you knows this needs more thought. take it inward -- go_inward will let you think privately, or send_message(self) if you just want to leave yourself a note.";
244
+ }
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. Default malformed fallback
258
+ return "your final_answer was incomplete or malformed. call final_answer again with your complete response.";
259
+ }
131
260
  // Re-export kick utilities for backward compat
132
261
  var kicks_1 = require("./kicks");
133
262
  Object.defineProperty(exports, "hasToolIntent", { enumerable: true, get: function () { return kicks_1.hasToolIntent; } });
@@ -160,6 +289,68 @@ function stripLastToolCalls(messages) {
160
289
  }
161
290
  }
162
291
  }
292
+ // Roles that end a tool-result scan. When scanning forward from an assistant
293
+ // message, stop at the next assistant or user message (tool results must be
294
+ // adjacent to their originating assistant message).
295
+ const TOOL_SCAN_BOUNDARY_ROLES = new Set(["assistant", "user"]);
296
+ // Repair orphaned tool_calls and tool results anywhere in the message history.
297
+ // 1. If an assistant message has tool_calls but missing tool results, inject synthetic error results.
298
+ // 2. If a tool result's tool_call_id doesn't match any tool_calls in a preceding assistant message, remove it.
299
+ // This prevents 400 errors from the API after an aborted turn.
300
+ function repairOrphanedToolCalls(messages) {
301
+ // Pass 1: collect all valid tool_call IDs from assistant messages
302
+ const validCallIds = new Set();
303
+ for (const msg of messages) {
304
+ if (msg.role === "assistant") {
305
+ const asst = msg;
306
+ if (asst.tool_calls) {
307
+ for (const tc of asst.tool_calls)
308
+ validCallIds.add(tc.id);
309
+ }
310
+ }
311
+ }
312
+ // Pass 2: remove orphaned tool results (tool_call_id not in any assistant's tool_calls)
313
+ for (let i = messages.length - 1; i >= 0; i--) {
314
+ if (messages[i].role === "tool") {
315
+ const toolMsg = messages[i];
316
+ if (!validCallIds.has(toolMsg.tool_call_id)) {
317
+ messages.splice(i, 1);
318
+ }
319
+ }
320
+ }
321
+ // Pass 3: inject synthetic results for tool_calls missing their tool results
322
+ for (let i = 0; i < messages.length; i++) {
323
+ const msg = messages[i];
324
+ if (msg.role !== "assistant")
325
+ continue;
326
+ const asst = msg;
327
+ if (!asst.tool_calls || asst.tool_calls.length === 0)
328
+ continue;
329
+ // Collect tool result IDs that follow this assistant message
330
+ const resultIds = new Set();
331
+ for (let j = i + 1; j < messages.length; j++) {
332
+ const following = messages[j];
333
+ if (following.role === "tool") {
334
+ resultIds.add(following.tool_call_id);
335
+ }
336
+ else if (TOOL_SCAN_BOUNDARY_ROLES.has(following.role)) {
337
+ break;
338
+ }
339
+ }
340
+ const missing = asst.tool_calls.filter((tc) => !resultIds.has(tc.id));
341
+ if (missing.length > 0) {
342
+ const syntheticResults = missing.map((tc) => ({
343
+ role: "tool",
344
+ tool_call_id: tc.id,
345
+ content: "error: tool call was interrupted (previous turn timed out or was aborted)",
346
+ }));
347
+ let insertAt = i + 1;
348
+ while (insertAt < messages.length && messages[insertAt].role === "tool")
349
+ insertAt++;
350
+ messages.splice(insertAt, 0, ...syntheticResults);
351
+ }
352
+ }
353
+ }
163
354
  // Detect context overflow errors from Azure or MiniMax
164
355
  function isContextOverflow(err) {
165
356
  if (!(err instanceof Error))
@@ -242,7 +433,12 @@ async function runAgent(messages, callbacks, channel, signal, options) {
242
433
  // so turn execution remains consistent and non-fatal.
243
434
  if (channel) {
244
435
  try {
245
- const refreshed = await (0, prompt_1.buildSystem)(channel, options, currentContext);
436
+ const buildSystemOptions = {
437
+ ...options,
438
+ providerCapabilities: providerRuntime.capabilities,
439
+ supportedReasoningEfforts: providerRuntime.supportedReasoningEfforts,
440
+ };
441
+ const refreshed = await (0, prompt_1.buildSystem)(channel, buildSystemOptions, currentContext);
246
442
  upsertSystemPrompt(messages, refreshed);
247
443
  }
248
444
  catch (error) {
@@ -265,20 +461,34 @@ async function runAgent(messages, callbacks, channel, signal, options) {
265
461
  }
266
462
  }
267
463
  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
464
  let done = false;
272
465
  let lastUsage;
273
466
  let overflowRetried = false;
274
467
  let retryCount = 0;
468
+ let outcome = "complete";
469
+ let completion;
470
+ let sawSteeringFollowUp = false;
471
+ let mustResolveBeforeHandoffActive = options?.mustResolveBeforeHandoff === true;
472
+ let currentReasoningEffort = "medium";
473
+ let sawSendMessageSelf = false;
474
+ let sawGoInward = false;
475
+ let sawQuerySession = false;
476
+ let sawBridgeManage = false;
275
477
  // Prevent MaxListenersExceeded warning — each iteration adds a listener
276
478
  try {
277
479
  require("events").setMaxListeners(50, signal);
278
480
  }
279
481
  catch { /* unsupported */ }
280
482
  const toolPreferences = currentContext?.friend?.toolPreferences;
281
- const baseTools = (0, tools_1.getToolsForChannel)(channel ? (0, channel_1.getChannelCapabilities)(channel) : undefined, toolPreferences && Object.keys(toolPreferences).length > 0 ? toolPreferences : undefined);
483
+ 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);
484
+ // Augment tool context with reasoning effort controls from provider
485
+ const augmentedToolContext = options?.toolContext
486
+ ? {
487
+ ...options.toolContext,
488
+ supportedReasoningEfforts: providerRuntime.supportedReasoningEfforts,
489
+ setReasoningEffort: (level) => { currentReasoningEffort = level; },
490
+ }
491
+ : undefined;
282
492
  // Rebase provider-owned turn state from canonical messages at user-turn start.
283
493
  // This prevents stale provider caches from replaying prior-turn context.
284
494
  providerRuntime.resetTurnState(messages);
@@ -287,9 +497,23 @@ async function runAgent(messages, callbacks, channel, signal, options) {
287
497
  // so the model can signal completion. With tool_choice: required, the
288
498
  // model must call a tool every turn — final_answer is how it exits.
289
499
  // Overridable via options.toolChoiceRequired = false (e.g. CLI).
290
- const activeTools = toolChoiceRequired ? [...baseTools, tools_1.finalAnswerTool] : baseTools;
500
+ const activeTools = toolChoiceRequired
501
+ ? [...baseTools, tools_1.goInwardTool, ...(currentContext?.isGroupChat ? [tools_1.noResponseTool] : []), tools_1.finalAnswerTool]
502
+ : baseTools;
291
503
  const steeringFollowUps = options?.drainSteeringFollowUps?.() ?? [];
292
504
  if (steeringFollowUps.length > 0) {
505
+ const hasSupersedingFollowUp = steeringFollowUps.some((followUp) => followUp.effect === "clear_and_supersede");
506
+ if (hasSupersedingFollowUp) {
507
+ mustResolveBeforeHandoffActive = false;
508
+ options?.setMustResolveBeforeHandoff?.(false);
509
+ outcome = "superseded";
510
+ break;
511
+ }
512
+ if (steeringFollowUps.some((followUp) => followUp.effect === "set_no_handoff")) {
513
+ mustResolveBeforeHandoffActive = true;
514
+ options?.setMustResolveBeforeHandoff?.(true);
515
+ }
516
+ sawSteeringFollowUp = true;
293
517
  for (const followUp of steeringFollowUps) {
294
518
  messages.push({ role: "user", content: followUp.text });
295
519
  }
@@ -297,8 +521,10 @@ async function runAgent(messages, callbacks, channel, signal, options) {
297
521
  }
298
522
  // Yield so pending I/O (stdin Ctrl-C) can be processed between iterations
299
523
  await new Promise((r) => setImmediate(r));
300
- if (signal?.aborted)
524
+ if (signal?.aborted) {
525
+ outcome = "aborted";
301
526
  break;
527
+ }
302
528
  try {
303
529
  callbacks.onModelStart();
304
530
  const result = await providerRuntime.streamTurn({
@@ -308,6 +534,7 @@ async function runAgent(messages, callbacks, channel, signal, options) {
308
534
  signal,
309
535
  traceId,
310
536
  toolChoiceRequired,
537
+ reasoningEffort: currentReasoningEffort,
311
538
  });
312
539
  // Track usage from the latest API call
313
540
  if (result.usage)
@@ -331,49 +558,39 @@ async function runAgent(messages, callbacks, channel, signal, options) {
331
558
  if (reasoningItems.length > 0) {
332
559
  msg._reasoning_items = reasoningItems;
333
560
  }
561
+ // Store thinking blocks (Anthropic) on the assistant message for round-tripping
562
+ const thinkingItems = result.outputItems.filter((item) => "type" in item && (item.type === "thinking" || item.type === "redacted_thinking"));
563
+ if (thinkingItems.length > 0) {
564
+ msg._thinking_blocks = thinkingItems;
565
+ }
566
+ // Phase annotation for Codex provider
567
+ const hasPhaseAnnotation = providerRuntime.capabilities.has("phase-annotation");
568
+ const isSoleFinalAnswer = result.toolCalls.length === 1 && result.toolCalls[0].name === "final_answer";
569
+ if (hasPhaseAnnotation) {
570
+ msg.phase = isSoleFinalAnswer ? "final_answer" : "commentary";
571
+ }
334
572
  if (!result.toolCalls.length) {
335
- // Kick detection is disabled while tool_choice: required + final_answer
336
- // is the primary loop control mechanism. The model should never reach
337
- // this path (tool_choice: required forces a tool call), but if it does,
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
- // }
573
+ // No tool calls accept response as-is.
574
+ // (Kick detection disabled; tool_choice: required + final_answer
575
+ // is the primary loop control. See src/heart/kicks.ts to re-enable.)
353
576
  messages.push(msg);
354
577
  done = true;
355
578
  }
356
579
  else {
357
580
  // Check for final_answer sole call: intercept before tool execution
358
- const isSoleFinalAnswer = result.toolCalls.length === 1 && result.toolCalls[0].name === "final_answer";
359
581
  if (isSoleFinalAnswer) {
360
582
  // Extract answer from the tool call arguments.
361
- // Supports: {"answer":"text"}, "text" (JSON string), retry on failure.
362
- let answer;
363
- try {
364
- const parsed = JSON.parse(result.toolCalls[0].arguments);
365
- if (typeof parsed === "string") {
366
- answer = parsed;
367
- }
368
- else if (parsed.answer != null) {
369
- answer = parsed.answer;
370
- }
371
- // else: valid JSON but no answer field — answer stays undefined (retry)
372
- }
373
- catch {
374
- // JSON parsing failed (e.g. truncated output) — answer stays undefined (retry)
375
- }
376
- if (answer != null) {
583
+ // Supports: {"answer":"text","intent":"..."} or "text" (JSON string).
584
+ const { answer, intent } = parseFinalAnswerPayload(result.toolCalls[0].arguments);
585
+ const validDirectReply = mustResolveBeforeHandoffActive && intent === "direct_reply" && sawSteeringFollowUp;
586
+ const validTerminalIntent = intent === "complete" || intent === "blocked";
587
+ const validClosure = answer != null
588
+ && (!mustResolveBeforeHandoffActive || validDirectReply || validTerminalIntent);
589
+ if (validClosure) {
590
+ completion = {
591
+ answer,
592
+ intent: validDirectReply ? "direct_reply" : intent === "blocked" ? "blocked" : "complete",
593
+ };
377
594
  if (result.finalAnswerStreamed) {
378
595
  // The streaming layer already parsed and emitted the answer
379
596
  // progressively via FinalAnswerParser. Skip clearing and
@@ -386,27 +603,142 @@ async function runAgent(messages, callbacks, channel, signal, options) {
386
603
  // Never truncate -- channel adapters handle splitting long messages.
387
604
  callbacks.onTextChunk(answer);
388
605
  }
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
606
  messages.push(msg);
392
- messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: "(delivered)" });
393
- providerRuntime.appendToolOutput(result.toolCalls[0].id, "(delivered)");
394
- done = true;
607
+ if (validDirectReply) {
608
+ const resumeWork = "direct reply delivered. resume the unresolved obligation now and keep working until you can finish or clearly report that you are blocked.";
609
+ messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: resumeWork });
610
+ providerRuntime.appendToolOutput(result.toolCalls[0].id, resumeWork);
611
+ }
612
+ else {
613
+ const delivered = "(delivered)";
614
+ messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: delivered });
615
+ providerRuntime.appendToolOutput(result.toolCalls[0].id, delivered);
616
+ outcome = intent === "blocked" ? "blocked" : "complete";
617
+ done = true;
618
+ }
395
619
  }
396
620
  else {
397
621
  // Answer is undefined -- the model's final_answer was incomplete or
398
622
  // malformed. Clear any partial streamed text or noise, then push the
399
623
  // assistant msg + error tool result and let the model try again.
400
624
  callbacks.onClearText?.();
401
- const retryError = "your final_answer was incomplete or malformed. call final_answer again with your complete response.";
625
+ const retryError = getFinalAnswerRetryError(mustResolveBeforeHandoffActive, intent, sawSteeringFollowUp, options?.delegationDecision, sawSendMessageSelf, sawGoInward);
402
626
  messages.push(msg);
403
627
  messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: retryError });
404
628
  providerRuntime.appendToolOutput(result.toolCalls[0].id, retryError);
405
629
  }
406
630
  continue;
407
631
  }
632
+ // Check for no_response sole call: intercept before tool execution
633
+ const isSoleNoResponse = result.toolCalls.length === 1 && result.toolCalls[0].name === "no_response";
634
+ if (isSoleNoResponse) {
635
+ let reason;
636
+ try {
637
+ const parsed = JSON.parse(result.toolCalls[0].arguments);
638
+ if (typeof parsed?.reason === "string")
639
+ reason = parsed.reason;
640
+ }
641
+ catch { /* ignore */ }
642
+ (0, runtime_1.emitNervesEvent)({
643
+ component: "engine",
644
+ event: "engine.no_response",
645
+ message: "agent declined to respond in group chat",
646
+ meta: { ...(reason ? { reason } : {}) },
647
+ });
648
+ messages.push(msg);
649
+ const silenced = "(silenced)";
650
+ messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: silenced });
651
+ providerRuntime.appendToolOutput(result.toolCalls[0].id, silenced);
652
+ outcome = "no_response";
653
+ done = true;
654
+ continue;
655
+ }
656
+ // Check for go_inward sole call: intercept before tool execution
657
+ const isSoleGoInward = result.toolCalls.length === 1 && result.toolCalls[0].name === "go_inward";
658
+ if (isSoleGoInward) {
659
+ let parsedArgs = {};
660
+ try {
661
+ parsedArgs = JSON.parse(result.toolCalls[0].arguments);
662
+ }
663
+ catch { /* ignore */ }
664
+ /* v8 ignore next -- defensive: content always string from model @preserve */
665
+ const content = typeof parsedArgs.content === "string" ? parsedArgs.content : "";
666
+ const answer = typeof parsedArgs.answer === "string" ? parsedArgs.answer : undefined;
667
+ const parsedMode = parsedArgs.mode === "reflect" || parsedArgs.mode === "plan" || parsedArgs.mode === "relay"
668
+ ? parsedArgs.mode
669
+ : undefined;
670
+ const mode = parsedMode || "reflect";
671
+ // Emit outward answer if provided
672
+ if (answer) {
673
+ callbacks.onClearText?.();
674
+ callbacks.onTextChunk(answer);
675
+ }
676
+ // Build handoff packet and enqueue
677
+ const handoffContent = buildGoInwardHandoffPacket({
678
+ content,
679
+ mode,
680
+ delegationDecision: options?.delegationDecision,
681
+ currentSession: options?.toolContext?.currentSession ?? null,
682
+ currentObligation: options?.currentObligation ?? null,
683
+ outwardClosureRequired: options?.delegationDecision?.outwardClosureRequired ?? false,
684
+ });
685
+ const pendingDir = (0, pending_1.getInnerDialogPendingDir)((0, identity_2.getAgentName)());
686
+ const currentSession = options?.toolContext?.currentSession;
687
+ const isInnerChannel = currentSession?.friendId === "self" && currentSession?.channel === "inner";
688
+ const envelope = {
689
+ from: (0, identity_2.getAgentName)(),
690
+ friendId: "self",
691
+ channel: "inner",
692
+ key: "dialog",
693
+ content: handoffContent,
694
+ timestamp: Date.now(),
695
+ mode,
696
+ ...(currentSession && !isInnerChannel ? {
697
+ delegatedFrom: {
698
+ friendId: currentSession.friendId,
699
+ channel: currentSession.channel,
700
+ key: currentSession.key,
701
+ },
702
+ obligationStatus: "pending",
703
+ } : {}),
704
+ };
705
+ (0, pending_1.queuePendingMessage)(pendingDir, envelope);
706
+ if (currentSession && !isInnerChannel) {
707
+ try {
708
+ (0, obligations_1.createObligation)((0, identity_2.getAgentRoot)(), {
709
+ origin: {
710
+ friendId: currentSession.friendId,
711
+ channel: currentSession.channel,
712
+ key: currentSession.key,
713
+ },
714
+ content,
715
+ });
716
+ }
717
+ catch {
718
+ /* v8 ignore next -- defensive: obligation store write failure should not break go_inward @preserve */
719
+ }
720
+ }
721
+ try {
722
+ await (0, socket_client_1.requestInnerWake)((0, identity_2.getAgentName)());
723
+ }
724
+ catch { /* daemon may not be running */ }
725
+ sawGoInward = true;
726
+ messages.push(msg);
727
+ const ack = "(going inward)";
728
+ messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: ack });
729
+ providerRuntime.appendToolOutput(result.toolCalls[0].id, ack);
730
+ (0, runtime_1.emitNervesEvent)({
731
+ component: "engine",
732
+ event: "engine.go_inward",
733
+ message: "taking thread inward",
734
+ meta: { mode, hasAnswer: answer !== undefined, contentSnippet: content.slice(0, 80) },
735
+ });
736
+ outcome = "go_inward";
737
+ done = true;
738
+ continue;
739
+ }
408
740
  messages.push(msg);
409
- // SHARED: execute tools (final_answer in mixed calls is rejected inline)
741
+ // SHARED: execute tools (final_answer, no_response, go_inward in mixed calls are rejected inline)
410
742
  for (const tc of result.toolCalls) {
411
743
  if (signal?.aborted)
412
744
  break;
@@ -417,6 +749,20 @@ async function runAgent(messages, callbacks, channel, signal, options) {
417
749
  providerRuntime.appendToolOutput(tc.id, rejection);
418
750
  continue;
419
751
  }
752
+ // Intercept no_response in mixed call: reject it
753
+ if (tc.name === "no_response") {
754
+ const rejection = "rejected: no_response must be the only tool call. call no_response alone when you want to stay silent.";
755
+ messages.push({ role: "tool", tool_call_id: tc.id, content: rejection });
756
+ providerRuntime.appendToolOutput(tc.id, rejection);
757
+ continue;
758
+ }
759
+ // Intercept go_inward in mixed call: reject it
760
+ if (tc.name === "go_inward") {
761
+ const rejection = "rejected: go_inward must be the only tool call. finish your other work first, then call go_inward alone.";
762
+ messages.push({ role: "tool", tool_call_id: tc.id, content: rejection });
763
+ providerRuntime.appendToolOutput(tc.id, rejection);
764
+ continue;
765
+ }
420
766
  let args = {};
421
767
  try {
422
768
  args = JSON.parse(tc.arguments);
@@ -424,6 +770,15 @@ async function runAgent(messages, callbacks, channel, signal, options) {
424
770
  catch {
425
771
  /* ignore */
426
772
  }
773
+ if (tc.name === "send_message" && args.friendId === "self") {
774
+ sawSendMessageSelf = true;
775
+ }
776
+ /* v8 ignore next -- flag tested via truth-check integration tests @preserve */
777
+ if (tc.name === "query_session")
778
+ sawQuerySession = true;
779
+ /* v8 ignore next -- flag tested via truth-check integration tests @preserve */
780
+ if (tc.name === "bridge_manage")
781
+ sawBridgeManage = true;
427
782
  const argSummary = (0, tools_1.summarizeArgs)(tc.name, args);
428
783
  // Confirmation check for mutate tools
429
784
  if ((0, tools_1.isConfirmationRequired)(tc.name) && !options?.skipConfirmation) {
@@ -444,7 +799,8 @@ async function runAgent(messages, callbacks, channel, signal, options) {
444
799
  let toolResult;
445
800
  let success;
446
801
  try {
447
- toolResult = await (0, tools_1.execTool)(tc.name, args, options?.toolContext);
802
+ const execToolFn = options?.execTool ?? tools_1.execTool;
803
+ toolResult = await execToolFn(tc.name, args, augmentedToolContext ?? options?.toolContext);
448
804
  success = true;
449
805
  }
450
806
  catch (e) {
@@ -461,6 +817,7 @@ async function runAgent(messages, callbacks, channel, signal, options) {
461
817
  // Abort is not an error — just stop cleanly
462
818
  if (signal?.aborted) {
463
819
  stripLastToolCalls(messages);
820
+ outcome = "aborted";
464
821
  break;
465
822
  }
466
823
  // Context overflow: trim aggressively and retry once
@@ -495,6 +852,7 @@ async function runAgent(messages, callbacks, channel, signal, options) {
495
852
  });
496
853
  if (aborted) {
497
854
  stripLastToolCalls(messages);
855
+ outcome = "aborted";
498
856
  break;
499
857
  }
500
858
  providerRuntime.resetTurnState(messages);
@@ -510,6 +868,7 @@ async function runAgent(messages, callbacks, channel, signal, options) {
510
868
  meta: {},
511
869
  });
512
870
  stripLastToolCalls(messages);
871
+ outcome = "errored";
513
872
  done = true;
514
873
  }
515
874
  }
@@ -518,7 +877,7 @@ async function runAgent(messages, callbacks, channel, signal, options) {
518
877
  trace_id: traceId,
519
878
  component: "engine",
520
879
  message: "runAgent turn completed",
521
- meta: { done },
880
+ meta: { done, sawGoInward, sawQuerySession, sawBridgeManage },
522
881
  });
523
- return { usage: lastUsage };
882
+ return { usage: lastUsage, outcome, completion };
524
883
  }