@ouro.bot/cli 0.1.0-alpha.34 → 0.1.0-alpha.340
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +188 -187
- package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/agent.json +3 -2
- package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/SOUL.md +1 -1
- package/changelog.json +2031 -0
- package/dist/arc/attention-types.js +8 -0
- package/dist/arc/cares.js +140 -0
- package/dist/arc/episodes.js +117 -0
- package/dist/arc/intentions.js +133 -0
- package/dist/arc/json-store.js +117 -0
- package/dist/arc/obligations.js +237 -0
- package/dist/arc/packets.js +193 -0
- package/dist/arc/presence.js +185 -0
- package/dist/arc/task-lifecycle.js +65 -0
- package/dist/heart/active-work.js +832 -0
- package/dist/heart/agent-entry.js +37 -2
- package/dist/heart/attachments/image-normalize.js +194 -0
- package/dist/heart/attachments/materialize.js +97 -0
- package/dist/heart/attachments/originals.js +88 -0
- package/dist/heart/attachments/render.js +29 -0
- package/dist/heart/attachments/sources/adapter.js +2 -0
- package/dist/heart/attachments/sources/bluebubbles.js +156 -0
- package/dist/heart/attachments/sources/cli-local-file.js +78 -0
- package/dist/heart/attachments/sources/index.js +16 -0
- package/dist/heart/attachments/store.js +103 -0
- package/dist/heart/attachments/types.js +93 -0
- package/dist/heart/auth/auth-flow.js +463 -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/bundle-state.js +168 -0
- package/dist/heart/commitments.js +111 -0
- package/dist/heart/config-registry.js +304 -0
- package/dist/heart/config.js +53 -21
- package/dist/heart/core.js +695 -195
- package/dist/heart/cross-chat-delivery.js +131 -0
- package/dist/heart/daemon/agent-config-check.js +292 -0
- package/dist/heart/daemon/agent-discovery.js +79 -3
- package/dist/heart/daemon/agent-service.js +360 -0
- package/dist/heart/daemon/agentic-repair.js +170 -0
- package/dist/heart/daemon/bluebubbles-health-diagnostics.js +122 -0
- package/dist/heart/daemon/cadence.js +70 -0
- package/dist/heart/daemon/cli-defaults.js +591 -0
- package/dist/heart/daemon/cli-exec.js +2297 -0
- package/dist/heart/daemon/cli-help.js +306 -0
- package/dist/heart/daemon/cli-parse.js +824 -0
- package/dist/heart/daemon/cli-render-doctor.js +57 -0
- package/dist/heart/daemon/cli-render.js +512 -0
- package/dist/heart/daemon/cli-types.js +8 -0
- package/dist/heart/daemon/daemon-cli.js +30 -1171
- package/dist/heart/daemon/daemon-entry.js +358 -3
- package/dist/heart/daemon/daemon-health.js +141 -0
- package/dist/heart/daemon/daemon-runtime-sync.js +157 -12
- package/dist/heart/daemon/daemon-tombstone.js +236 -0
- package/dist/heart/daemon/daemon.js +751 -58
- package/dist/heart/daemon/doctor-types.js +8 -0
- package/dist/heart/daemon/doctor.js +401 -0
- package/dist/heart/daemon/health-monitor.js +79 -1
- package/dist/heart/daemon/hooks/agent-config-v2.js +33 -0
- package/dist/heart/daemon/hooks/bundle-meta.js +115 -1
- package/dist/heart/daemon/http-health-probe.js +80 -0
- package/dist/heart/daemon/inner-status.js +89 -0
- package/dist/heart/daemon/interactive-repair.js +91 -0
- package/dist/heart/daemon/launchd.js +46 -9
- package/dist/heart/daemon/log-tailer.js +82 -12
- package/dist/heart/daemon/logs-prune.js +105 -0
- package/dist/heart/daemon/message-router.js +17 -8
- package/dist/heart/daemon/os-cron-deps.js +134 -0
- package/dist/heart/daemon/ouro-bot-entry.js +1 -1
- package/dist/heart/daemon/process-manager.js +201 -0
- package/dist/heart/daemon/provider-discovery.js +105 -0
- package/dist/heart/daemon/pulse.js +463 -0
- package/dist/heart/daemon/run-hooks.js +2 -0
- package/dist/heart/daemon/runtime-logging.js +67 -16
- package/dist/heart/daemon/runtime-metadata.js +101 -0
- package/dist/heart/daemon/runtime-mode.js +67 -0
- package/dist/heart/daemon/safe-mode.js +161 -0
- package/dist/heart/daemon/sense-manager.js +72 -3
- package/dist/heart/daemon/session-id-resolver.js +131 -0
- package/dist/heart/daemon/skill-management-installer.js +94 -0
- package/dist/heart/daemon/socket-client.js +307 -0
- package/dist/heart/daemon/stale-bundle-prune.js +96 -0
- package/dist/heart/daemon/startup-tui.js +237 -0
- package/dist/heart/daemon/task-scheduler.js +3 -25
- package/dist/heart/daemon/thoughts.js +510 -0
- package/dist/heart/daemon/up-progress.js +135 -0
- package/dist/heart/delegation.js +62 -0
- package/dist/heart/habits/habit-migration.js +181 -0
- package/dist/heart/habits/habit-parser.js +140 -0
- package/dist/heart/habits/habit-scheduler.js +371 -0
- package/dist/heart/{daemon → hatch}/hatch-flow.js +32 -120
- package/dist/heart/{daemon → hatch}/hatch-specialist.js +3 -3
- package/dist/heart/{daemon → hatch}/specialist-prompt.js +10 -7
- package/dist/heart/{daemon → hatch}/specialist-tools.js +49 -3
- package/dist/heart/identity.js +154 -59
- package/dist/heart/kicks.js +2 -20
- package/dist/heart/mcp/mcp-server.js +653 -0
- package/dist/heart/migrate-config.js +127 -0
- package/dist/heart/model-capabilities.js +59 -0
- package/dist/heart/outlook/outlook-http-hooks.js +64 -0
- package/dist/heart/outlook/outlook-http-response.js +7 -0
- package/dist/heart/outlook/outlook-http-routes.js +232 -0
- package/dist/heart/outlook/outlook-http-static.js +99 -0
- package/dist/heart/outlook/outlook-http-transport.js +116 -0
- package/dist/heart/outlook/outlook-http.js +99 -0
- package/dist/heart/outlook/outlook-read.js +28 -0
- package/dist/heart/outlook/outlook-types.js +27 -0
- package/dist/heart/outlook/outlook-view.js +194 -0
- package/dist/heart/outlook/readers/agent-machine.js +355 -0
- package/dist/heart/outlook/readers/continuity-readers.js +332 -0
- package/dist/heart/outlook/readers/runtime-readers.js +660 -0
- package/dist/heart/outlook/readers/sessions.js +231 -0
- package/dist/heart/outlook/readers/shared.js +111 -0
- package/dist/heart/progress-story.js +42 -0
- package/dist/heart/provider-failover.js +135 -0
- package/dist/heart/provider-models.js +81 -0
- package/dist/heart/provider-ping.js +234 -0
- package/dist/heart/providers/anthropic-token.js +163 -0
- package/dist/heart/providers/anthropic.js +171 -50
- package/dist/heart/providers/azure.js +97 -11
- package/dist/heart/providers/error-classification.js +63 -0
- package/dist/heart/providers/github-copilot.js +135 -0
- package/dist/heart/providers/minimax-vlm.js +189 -0
- package/dist/heart/providers/minimax.js +23 -6
- package/dist/heart/providers/openai-codex.js +33 -23
- package/dist/heart/session-activity.js +190 -0
- package/dist/heart/session-events.js +726 -0
- package/dist/heart/session-recall.js +162 -0
- package/dist/heart/start-of-turn-packet.js +341 -0
- package/dist/heart/streaming.js +36 -27
- package/dist/heart/sync.js +332 -0
- package/dist/heart/target-resolution.js +127 -0
- package/dist/heart/tempo.js +93 -0
- package/dist/heart/temporal-view.js +41 -0
- package/dist/heart/tool-activity-callbacks.js +36 -0
- package/dist/heart/tool-description.js +135 -0
- package/dist/heart/tool-friction.js +55 -0
- package/dist/heart/tool-loop.js +200 -0
- package/dist/heart/turn-context.js +358 -0
- package/dist/heart/turn-coordinator.js +28 -0
- package/dist/heart/{daemon → versioning}/ouro-bot-global-installer.js +1 -1
- package/dist/heart/{daemon → versioning}/ouro-bot-wrapper.js +1 -1
- package/dist/heart/{daemon → versioning}/ouro-path-installer.js +78 -35
- package/dist/heart/versioning/ouro-version-manager.js +295 -0
- package/dist/heart/{daemon → versioning}/staged-restart.js +40 -8
- package/dist/heart/{daemon → versioning}/update-checker.js +12 -2
- package/dist/heart/{daemon → versioning}/update-hooks.js +63 -59
- package/dist/mind/associative-recall.js +137 -66
- package/dist/mind/bundle-manifest.js +7 -1
- package/dist/mind/context.js +89 -93
- package/dist/mind/diary-integrity.js +60 -0
- package/dist/mind/{memory.js → diary.js} +84 -96
- package/dist/mind/embedding-provider.js +60 -0
- package/dist/mind/file-state.js +179 -0
- package/dist/mind/first-impressions.js +14 -1
- package/dist/mind/friends/channel.js +56 -0
- package/dist/mind/friends/group-context.js +144 -0
- package/dist/mind/friends/resolver.js +37 -0
- package/dist/mind/friends/store-file.js +58 -3
- package/dist/mind/friends/trust-explanation.js +74 -0
- package/dist/mind/friends/types.js +8 -0
- package/dist/mind/journal-index.js +161 -0
- package/dist/mind/obligation-steering.js +221 -0
- package/dist/mind/pending.js +74 -7
- package/dist/mind/prompt.js +999 -111
- package/dist/mind/provenance-trust.js +26 -0
- package/dist/mind/scrutiny.js +173 -0
- package/dist/mind/token-estimate.js +8 -12
- package/dist/nerves/cli-logging.js +7 -1
- package/dist/nerves/coverage/audit.js +1 -1
- package/dist/nerves/coverage/file-completeness.js +83 -5
- package/dist/nerves/coverage/run-artifacts.js +1 -1
- package/dist/nerves/event-buffer.js +111 -0
- package/dist/nerves/index.js +224 -4
- package/dist/nerves/observation.js +20 -0
- package/dist/nerves/redact.js +79 -0
- package/dist/nerves/runtime.js +5 -1
- package/dist/outlook-ui/assets/index-DC7sZefn.js +61 -0
- package/dist/outlook-ui/assets/index-LwChZTgL.css +1 -0
- package/dist/outlook-ui/index.html +15 -0
- package/dist/repertoire/ado-client.js +15 -56
- package/dist/repertoire/ado-semantic.js +11 -10
- package/dist/repertoire/api-client.js +97 -0
- package/dist/repertoire/bitwarden-store.js +319 -0
- package/dist/repertoire/bundle-templates.js +72 -0
- package/dist/repertoire/bw-installer.js +79 -0
- package/dist/repertoire/coding/codex-jsonl.js +64 -0
- package/dist/repertoire/coding/context-pack.js +330 -0
- package/dist/repertoire/coding/feedback.js +197 -30
- package/dist/repertoire/coding/manager.js +158 -9
- package/dist/repertoire/coding/spawner.js +55 -9
- package/dist/repertoire/coding/tools.js +170 -7
- package/dist/repertoire/commerce-errors.js +109 -0
- package/dist/repertoire/commerce-self-test.js +156 -0
- package/dist/repertoire/credential-access.js +527 -0
- package/dist/repertoire/duffel-client.js +185 -0
- package/dist/repertoire/github-client.js +14 -55
- package/dist/repertoire/graph-client.js +11 -52
- package/dist/repertoire/guardrails.js +375 -0
- package/dist/repertoire/mcp-client.js +255 -0
- package/dist/repertoire/mcp-manager.js +305 -0
- package/dist/repertoire/mcp-tools.js +63 -0
- package/dist/repertoire/shell-sessions.js +133 -0
- package/dist/repertoire/skills.js +14 -23
- package/dist/repertoire/stripe-client.js +131 -0
- package/dist/repertoire/tasks/board.js +43 -5
- package/dist/repertoire/tasks/fix.js +182 -0
- package/dist/repertoire/tasks/index.js +28 -10
- package/dist/repertoire/tasks/lifecycle.js +2 -2
- package/dist/repertoire/tasks/parser.js +3 -2
- package/dist/repertoire/tasks/scanner.js +194 -37
- package/dist/repertoire/tasks/transitions.js +16 -79
- package/dist/repertoire/tool-results.js +29 -0
- package/dist/repertoire/tools-attachments.js +316 -0
- package/dist/repertoire/tools-base.js +45 -771
- package/dist/repertoire/tools-bluebubbles.js +1 -0
- package/dist/repertoire/tools-bridge.js +141 -0
- package/dist/repertoire/tools-bundle.js +984 -0
- package/dist/repertoire/tools-config.js +185 -0
- package/dist/repertoire/tools-continuity.js +248 -0
- package/dist/repertoire/tools-credential.js +182 -0
- package/dist/repertoire/tools-files.js +342 -0
- package/dist/repertoire/tools-flight.js +224 -0
- package/dist/repertoire/tools-flow.js +105 -0
- package/dist/repertoire/tools-github.js +1 -7
- package/dist/repertoire/tools-memory.js +376 -0
- package/dist/repertoire/tools-session.js +739 -0
- package/dist/repertoire/tools-shell.js +120 -0
- package/dist/repertoire/tools-stripe.js +180 -0
- package/dist/repertoire/tools-surface.js +243 -0
- package/dist/repertoire/tools-teams.js +12 -62
- package/dist/repertoire/tools-travel.js +125 -0
- package/dist/repertoire/tools-user-profile.js +144 -0
- package/dist/repertoire/tools-vault.js +110 -0
- package/dist/repertoire/tools.js +144 -138
- package/dist/repertoire/travel-api-client.js +360 -0
- package/dist/repertoire/user-profile.js +118 -0
- package/dist/repertoire/vault-setup.js +241 -0
- package/dist/scripts/claude-code-hook.js +41 -0
- package/dist/scripts/claude-code-stop-hook.js +47 -0
- package/dist/senses/attention-queue.js +116 -0
- package/dist/senses/bluebubbles/attachment-cache.js +53 -0
- package/dist/senses/bluebubbles/attachment-download.js +137 -0
- package/dist/senses/{bluebubbles-client.js → bluebubbles/client.js} +225 -9
- package/dist/senses/bluebubbles/entry.js +13 -0
- package/dist/senses/bluebubbles/inbound-log.js +113 -0
- package/dist/senses/bluebubbles/index.js +1590 -0
- package/dist/senses/{bluebubbles-media.js → bluebubbles/media.js} +121 -70
- package/dist/senses/{bluebubbles-model.js → bluebubbles/model.js} +43 -12
- package/dist/senses/{bluebubbles-mutation-log.js → bluebubbles/mutation-log.js} +46 -6
- package/dist/senses/bluebubbles/replay.js +129 -0
- package/dist/senses/bluebubbles/runtime-state.js +109 -0
- package/dist/senses/{bluebubbles-session-cleanup.js → bluebubbles/session-cleanup.js} +1 -1
- package/dist/senses/cli/bracketed-paste.js +82 -0
- package/dist/senses/cli/image-paste.js +287 -0
- package/dist/senses/cli/image-ref-navigation.js +75 -0
- package/dist/senses/cli/ink-app.js +156 -0
- package/dist/senses/cli/inline-diff.js +64 -0
- package/dist/senses/cli/input-keys.js +174 -0
- package/dist/senses/cli/kill-ring.js +86 -0
- package/dist/senses/cli/message-list.js +51 -0
- package/dist/senses/cli/ouro-tui.js +605 -0
- package/dist/senses/cli/spinner-imperative.js +135 -0
- package/dist/senses/cli/spinner.js +101 -0
- package/dist/senses/cli/status-line.js +60 -0
- package/dist/senses/cli/streaming-markdown.js +526 -0
- package/dist/senses/cli/tool-display.js +83 -0
- package/dist/senses/cli/tool-render.js +85 -0
- package/dist/senses/cli/tui-store.js +240 -0
- package/dist/senses/cli/virtual-list.js +35 -0
- package/dist/senses/cli-entry.js +1 -1
- package/dist/senses/cli-layout.js +187 -0
- package/dist/senses/cli.js +595 -246
- package/dist/senses/commands.js +65 -1
- package/dist/senses/continuity.js +94 -0
- package/dist/senses/habit-turn-message.js +108 -0
- package/dist/senses/inner-dialog-worker.js +112 -19
- package/dist/senses/inner-dialog.js +633 -86
- package/dist/senses/pipeline.js +567 -0
- package/dist/senses/shared-turn.js +199 -0
- package/dist/senses/surface-tool.js +68 -0
- package/dist/senses/teams.js +665 -160
- package/dist/senses/trust-gate.js +112 -2
- package/package.json +29 -7
- package/skills/agent-commerce.md +106 -0
- package/skills/browser-navigation.md +110 -0
- package/skills/commerce-setup-guide.md +116 -0
- package/skills/commerce-setup.md +84 -0
- package/skills/configure-dev-tools.md +81 -0
- package/skills/travel-planning.md +138 -0
- package/dist/heart/daemon/subagent-installer.js +0 -134
- package/dist/senses/bluebubbles-entry.js +0 -11
- package/dist/senses/bluebubbles.js +0 -547
- package/dist/senses/debug-activity.js +0 -124
- package/subagents/README.md +0 -73
- package/subagents/work-doer.md +0 -235
- package/subagents/work-merger.md +0 -618
- package/subagents/work-planner.md +0 -382
- /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/basilisk.md +0 -0
- /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/jafar.md +0 -0
- /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/jormungandr.md +0 -0
- /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/kaa.md +0 -0
- /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/medusa.md +0 -0
- /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/monty.md +0 -0
- /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/nagini.md +0 -0
- /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/ouroboros.md +0 -0
- /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/python.md +0 -0
- /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/quetzalcoatl.md +0 -0
- /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/sir-hiss.md +0 -0
- /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/the-serpent.md +0 -0
- /package/{AdoptionSpecialist.ouro → SerpentGuide.ouro}/psyche/identities/the-snake.md +0 -0
- /package/dist/heart/{daemon → hatch}/hatch-animation.js +0 -0
- /package/dist/heart/{daemon → hatch}/specialist-orchestrator.js +0 -0
- /package/dist/heart/{daemon → versioning}/ouro-uti.js +0 -0
- /package/dist/heart/{daemon → versioning}/wrapper-publish-guard.js +0 -0
package/dist/heart/core.js
CHANGED
|
@@ -1,24 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.hasToolIntent = exports.buildSystem = exports.toResponsesTools = exports.toResponsesInput = exports.streamResponsesApi = exports.streamChatCompletion = exports.getToolsForChannel = exports.summarizeArgs = exports.execTool = exports.tools = void 0;
|
|
4
3
|
exports.createProviderRegistry = createProviderRegistry;
|
|
5
4
|
exports.resetProviderRuntime = resetProviderRuntime;
|
|
6
5
|
exports.getModel = getModel;
|
|
7
6
|
exports.getProvider = getProvider;
|
|
8
7
|
exports.createSummarize = createSummarize;
|
|
9
8
|
exports.getProviderDisplayLabel = getProviderDisplayLabel;
|
|
9
|
+
exports.isExternalStateQuery = isExternalStateQuery;
|
|
10
|
+
exports.getSettleRetryError = getSettleRetryError;
|
|
10
11
|
exports.stripLastToolCalls = stripLastToolCalls;
|
|
11
12
|
exports.repairOrphanedToolCalls = repairOrphanedToolCalls;
|
|
12
|
-
exports.
|
|
13
|
-
exports.classifyTransientError = classifyTransientError;
|
|
13
|
+
exports.isRetryBlocked = isRetryBlocked;
|
|
14
14
|
exports.runAgent = runAgent;
|
|
15
15
|
const config_1 = require("./config");
|
|
16
16
|
const identity_1 = require("./identity");
|
|
17
17
|
const tools_1 = require("../repertoire/tools");
|
|
18
18
|
const channel_1 = require("../mind/friends/channel");
|
|
19
|
-
|
|
20
|
-
// import { detectKick } from "./kicks";
|
|
21
|
-
// import type { KickReason } from "./kicks";
|
|
19
|
+
const tools_2 = require("../repertoire/tools");
|
|
22
20
|
const runtime_1 = require("../nerves/runtime");
|
|
23
21
|
const context_1 = require("../mind/context");
|
|
24
22
|
const prompt_1 = require("../mind/prompt");
|
|
@@ -27,71 +25,98 @@ const anthropic_1 = require("./providers/anthropic");
|
|
|
27
25
|
const azure_1 = require("./providers/azure");
|
|
28
26
|
const minimax_1 = require("./providers/minimax");
|
|
29
27
|
const openai_codex_1 = require("./providers/openai-codex");
|
|
30
|
-
|
|
28
|
+
const github_copilot_1 = require("./providers/github-copilot");
|
|
29
|
+
const identity_2 = require("./identity");
|
|
30
|
+
const socket_client_1 = require("./daemon/socket-client");
|
|
31
|
+
const obligations_1 = require("../arc/obligations");
|
|
32
|
+
const tool_loop_1 = require("./tool-loop");
|
|
33
|
+
const packets_1 = require("../arc/packets");
|
|
34
|
+
const tool_friction_1 = require("./tool-friction");
|
|
35
|
+
const provider_models_1 = require("./provider-models");
|
|
36
|
+
const _providerRuntimes = {
|
|
37
|
+
human: null,
|
|
38
|
+
agent: null,
|
|
39
|
+
};
|
|
40
|
+
function getProviderRuntimeFingerprint(facing) {
|
|
41
|
+
const config = (0, identity_1.loadAgentConfig)();
|
|
42
|
+
const facingConfig = facing === "human" ? config.humanFacing : config.agentFacing;
|
|
43
|
+
const provider = facingConfig.provider;
|
|
44
|
+
const model = facingConfig.model;
|
|
45
|
+
const providerConfig = (0, config_1.getProviderConfig)(provider);
|
|
46
|
+
return JSON.stringify({ provider, model, ...providerConfig });
|
|
47
|
+
}
|
|
31
48
|
function createProviderRegistry() {
|
|
32
49
|
const factories = {
|
|
33
50
|
azure: azure_1.createAzureProviderRuntime,
|
|
34
51
|
anthropic: anthropic_1.createAnthropicProviderRuntime,
|
|
35
52
|
minimax: minimax_1.createMinimaxProviderRuntime,
|
|
36
53
|
"openai-codex": openai_codex_1.createOpenAICodexProviderRuntime,
|
|
54
|
+
"github-copilot": github_copilot_1.createGithubCopilotProviderRuntime,
|
|
37
55
|
};
|
|
38
56
|
return {
|
|
39
|
-
resolve() {
|
|
40
|
-
const
|
|
41
|
-
|
|
57
|
+
resolve(provider, model) {
|
|
58
|
+
const resolvedProvider = provider ?? (0, identity_1.loadAgentConfig)().humanFacing.provider;
|
|
59
|
+
const resolvedModel = model ?? (0, identity_1.loadAgentConfig)().humanFacing.model;
|
|
60
|
+
return factories[resolvedProvider](resolvedModel);
|
|
42
61
|
},
|
|
43
62
|
};
|
|
44
63
|
}
|
|
45
|
-
function getProviderRuntime() {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const
|
|
52
|
-
(
|
|
53
|
-
|
|
54
|
-
event: "engine.provider_init_error",
|
|
55
|
-
component: "engine",
|
|
56
|
-
message: msg,
|
|
57
|
-
meta: {},
|
|
58
|
-
});
|
|
59
|
-
// eslint-disable-next-line no-console -- pre-boot guard: provider init failure
|
|
60
|
-
console.error(`\n[fatal] ${msg}\n`);
|
|
61
|
-
process.exit(1);
|
|
62
|
-
throw new Error("unreachable");
|
|
63
|
-
}
|
|
64
|
-
if (!_providerRuntime) {
|
|
65
|
-
(0, runtime_1.emitNervesEvent)({
|
|
66
|
-
level: "error",
|
|
67
|
-
event: "engine.provider_init_error",
|
|
68
|
-
component: "engine",
|
|
69
|
-
message: "provider runtime could not be initialized.",
|
|
70
|
-
meta: {},
|
|
71
|
-
});
|
|
72
|
-
process.exit(1);
|
|
73
|
-
throw new Error("unreachable");
|
|
64
|
+
function getProviderRuntime(facing = "human") {
|
|
65
|
+
try {
|
|
66
|
+
const fingerprint = getProviderRuntimeFingerprint(facing);
|
|
67
|
+
const cached = _providerRuntimes[facing];
|
|
68
|
+
if (!cached || cached.fingerprint !== fingerprint) {
|
|
69
|
+
const config = (0, identity_1.loadAgentConfig)();
|
|
70
|
+
const facingConfig = facing === "human" ? config.humanFacing : config.agentFacing;
|
|
71
|
+
const runtime = createProviderRegistry().resolve(facingConfig.provider, facingConfig.model);
|
|
72
|
+
_providerRuntimes[facing] = runtime ? { fingerprint, runtime } : null;
|
|
74
73
|
}
|
|
75
74
|
}
|
|
76
|
-
|
|
75
|
+
catch (error) {
|
|
76
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
77
|
+
(0, runtime_1.emitNervesEvent)({
|
|
78
|
+
level: "error",
|
|
79
|
+
event: "engine.provider_init_error",
|
|
80
|
+
component: "engine",
|
|
81
|
+
message: msg,
|
|
82
|
+
meta: {},
|
|
83
|
+
});
|
|
84
|
+
// eslint-disable-next-line no-console -- pre-boot guard: provider init failure
|
|
85
|
+
console.error(`\n[fatal] ${msg}\n`);
|
|
86
|
+
process.exit(1);
|
|
87
|
+
throw new Error("unreachable");
|
|
88
|
+
}
|
|
89
|
+
if (!_providerRuntimes[facing]) {
|
|
90
|
+
(0, runtime_1.emitNervesEvent)({
|
|
91
|
+
level: "error",
|
|
92
|
+
event: "engine.provider_init_error",
|
|
93
|
+
component: "engine",
|
|
94
|
+
message: "provider runtime could not be initialized.",
|
|
95
|
+
meta: {},
|
|
96
|
+
});
|
|
97
|
+
process.exit(1);
|
|
98
|
+
throw new Error("unreachable");
|
|
99
|
+
}
|
|
100
|
+
return _providerRuntimes[facing].runtime;
|
|
77
101
|
}
|
|
78
102
|
/**
|
|
79
|
-
* Clear the cached provider runtime so the next
|
|
80
|
-
*
|
|
81
|
-
*
|
|
103
|
+
* Clear the cached provider runtime so the next access re-creates it from
|
|
104
|
+
* current config. Runtime access also auto-refreshes when the selected
|
|
105
|
+
* provider fingerprint changes on disk.
|
|
82
106
|
*/
|
|
83
107
|
function resetProviderRuntime() {
|
|
84
|
-
|
|
108
|
+
_providerRuntimes.human = null;
|
|
109
|
+
_providerRuntimes.agent = null;
|
|
85
110
|
}
|
|
86
|
-
function getModel() {
|
|
87
|
-
return getProviderRuntime().model;
|
|
111
|
+
function getModel(facing = "human") {
|
|
112
|
+
return getProviderRuntime(facing).model;
|
|
88
113
|
}
|
|
89
|
-
function getProvider() {
|
|
90
|
-
return getProviderRuntime().id;
|
|
114
|
+
function getProvider(facing = "human") {
|
|
115
|
+
return getProviderRuntime(facing).id;
|
|
91
116
|
}
|
|
92
|
-
function createSummarize() {
|
|
117
|
+
function createSummarize(facing = "human") {
|
|
93
118
|
return async (transcript, instruction) => {
|
|
94
|
-
const runtime = getProviderRuntime();
|
|
119
|
+
const runtime = getProviderRuntime(facing);
|
|
95
120
|
const client = runtime.client;
|
|
96
121
|
const response = await client.chat.completions.create({
|
|
97
122
|
model: runtime.model,
|
|
@@ -104,34 +129,138 @@ function createSummarize() {
|
|
|
104
129
|
return response.choices?.[0]?.message?.content ?? transcript;
|
|
105
130
|
};
|
|
106
131
|
}
|
|
107
|
-
function getProviderDisplayLabel() {
|
|
108
|
-
const
|
|
132
|
+
function getProviderDisplayLabel(facing = "human") {
|
|
133
|
+
const config = (0, identity_1.loadAgentConfig)();
|
|
134
|
+
const facingConfig = facing === "human" ? config.humanFacing : config.agentFacing;
|
|
135
|
+
const provider = facingConfig.provider;
|
|
136
|
+
const model = facingConfig.model || "unknown";
|
|
109
137
|
const providerLabelBuilders = {
|
|
110
|
-
azure: () =>
|
|
138
|
+
azure: () => {
|
|
139
|
+
const azureCfg = (0, config_1.getAzureConfig)();
|
|
140
|
+
return `azure openai (${azureCfg.deployment || "default"}, model: ${model})`;
|
|
141
|
+
},
|
|
111
142
|
anthropic: () => `anthropic (${model})`,
|
|
112
143
|
minimax: () => `minimax (${model})`,
|
|
113
144
|
"openai-codex": () => `openai codex (${model})`,
|
|
145
|
+
/* v8 ignore next -- branch: tested via display label unit test @preserve */
|
|
146
|
+
"github-copilot": () => `github copilot (${model})`,
|
|
147
|
+
};
|
|
148
|
+
return providerLabelBuilders[provider]();
|
|
149
|
+
}
|
|
150
|
+
// Sole-call tools must be the only tool call in a turn. When they appear
|
|
151
|
+
// alongside other tools, the sole-call tool is rejected with this message.
|
|
152
|
+
const SOLE_CALL_REJECTION = {
|
|
153
|
+
settle: "rejected: settle must be the only tool call. finish your work first, then call settle alone.",
|
|
154
|
+
observe: "rejected: observe must be the only tool call. call observe alone when you want to stay silent.",
|
|
155
|
+
rest: "rejected: rest must be the only tool call. finish your work first, then call rest alone.",
|
|
156
|
+
};
|
|
157
|
+
function parseSettlePayload(argumentsText) {
|
|
158
|
+
try {
|
|
159
|
+
const parsed = JSON.parse(argumentsText);
|
|
160
|
+
if (typeof parsed === "string") {
|
|
161
|
+
return { answer: parsed };
|
|
162
|
+
}
|
|
163
|
+
if (!parsed || typeof parsed !== "object") {
|
|
164
|
+
return {};
|
|
165
|
+
}
|
|
166
|
+
const answer = typeof parsed.answer === "string" ? parsed.answer : undefined;
|
|
167
|
+
const rawIntent = parsed.intent;
|
|
168
|
+
const intent = rawIntent === "complete" || rawIntent === "blocked" || rawIntent === "direct_reply"
|
|
169
|
+
? rawIntent
|
|
170
|
+
: undefined;
|
|
171
|
+
return { answer, intent };
|
|
172
|
+
}
|
|
173
|
+
catch {
|
|
174
|
+
return {};
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
function parsePonderPayload(argumentsText) {
|
|
178
|
+
try {
|
|
179
|
+
const parsed = JSON.parse(argumentsText);
|
|
180
|
+
return parsed && typeof parsed === "object" ? parsed : {};
|
|
181
|
+
}
|
|
182
|
+
catch {
|
|
183
|
+
return {};
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
function parseSuccessCriteria(raw) {
|
|
187
|
+
if (typeof raw !== "string")
|
|
188
|
+
return null;
|
|
189
|
+
const criteria = raw
|
|
190
|
+
.split("\n")
|
|
191
|
+
.map((line) => line.replace(/^\s*[-*]\s*/, "").trim())
|
|
192
|
+
.filter((line) => line.length > 0);
|
|
193
|
+
return criteria.length > 0 ? criteria : null;
|
|
194
|
+
}
|
|
195
|
+
function parsePacketPayload(raw) {
|
|
196
|
+
if (typeof raw !== "string")
|
|
197
|
+
return null;
|
|
198
|
+
try {
|
|
199
|
+
const parsed = JSON.parse(raw);
|
|
200
|
+
return parsed && typeof parsed === "object" && !Array.isArray(parsed)
|
|
201
|
+
? parsed
|
|
202
|
+
: null;
|
|
203
|
+
}
|
|
204
|
+
catch {
|
|
205
|
+
return null;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
function normalizeLegacyPonderArgs(parsed) {
|
|
209
|
+
if (typeof parsed.thought !== "string" || parsed.thought.trim().length === 0) {
|
|
210
|
+
return parsed;
|
|
211
|
+
}
|
|
212
|
+
return {
|
|
213
|
+
action: "create",
|
|
214
|
+
kind: "reflection",
|
|
215
|
+
objective: parsed.thought.trim(),
|
|
216
|
+
summary: typeof parsed.say === "string" ? parsed.say.trim() : "",
|
|
217
|
+
success_criteria: "- preserve the thread for later work",
|
|
218
|
+
payload_json: "{}",
|
|
114
219
|
};
|
|
115
|
-
return providerLabelBuilders[getProvider()]();
|
|
116
220
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
221
|
+
function buildPonderResult(packet, action, returnObligationId) {
|
|
222
|
+
return JSON.stringify({
|
|
223
|
+
ok: true,
|
|
224
|
+
packet_id: packet.id,
|
|
225
|
+
action,
|
|
226
|
+
status: packet.status,
|
|
227
|
+
return_obligation_id: returnObligationId,
|
|
228
|
+
}, null, 2);
|
|
229
|
+
}
|
|
230
|
+
/** Returns true when a tool call queries external state (GitHub, npm registry). */
|
|
231
|
+
function isExternalStateQuery(toolName, args) {
|
|
232
|
+
if (toolName !== "shell")
|
|
233
|
+
return false;
|
|
234
|
+
const cmd = String(args.command ?? "");
|
|
235
|
+
return /\bgh\s+(pr|run|api|issue)\b/.test(cmd) || /\bnpm\s+(view|info|show)\b/.test(cmd);
|
|
236
|
+
}
|
|
237
|
+
function getSettleRetryError(mustResolveBeforeHandoff, intent, sawSteeringFollowUp, _delegationDecision, sawSendMessageSelf, sawPonder, _sawQuerySession, currentObligation, innerJob, sawExternalStateQuery) {
|
|
238
|
+
// Delegation adherence removed: the delegation decision is surfaced in the
|
|
239
|
+
// system prompt as a suggestion. Hard-gating settle caused infinite
|
|
240
|
+
// rejection loops where the agent couldn't respond to the user at all.
|
|
241
|
+
// The agent is free to follow or ignore the delegation hint.
|
|
242
|
+
// 2. Pending obligation not addressed
|
|
243
|
+
if (innerJob?.obligationStatus === "pending" && !sawSendMessageSelf && !sawPonder) {
|
|
244
|
+
return "you're still holding something from an earlier conversation -- someone is waiting for your answer. finish the thought first, or ponder to keep working on it privately.";
|
|
245
|
+
}
|
|
246
|
+
// 3. mustResolveBeforeHandoff + missing intent
|
|
247
|
+
if (mustResolveBeforeHandoff && !intent) {
|
|
248
|
+
return "your settle is missing required intent. when you must keep going until done or blocked, call settle again with answer plus intent=complete, blocked, or direct_reply.";
|
|
249
|
+
}
|
|
250
|
+
// 4. mustResolveBeforeHandoff + direct_reply without follow-up
|
|
251
|
+
if (mustResolveBeforeHandoff && intent === "direct_reply" && !sawSteeringFollowUp) {
|
|
252
|
+
return "your settle used intent=direct_reply without a newer steering follow-up. continue the unresolved work, or call settle again with intent=complete or blocked when appropriate.";
|
|
253
|
+
}
|
|
254
|
+
// 5. mustResolveBeforeHandoff + complete while a live return loop is still active
|
|
255
|
+
if (mustResolveBeforeHandoff && intent === "complete" && currentObligation && !sawSteeringFollowUp) {
|
|
256
|
+
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.";
|
|
257
|
+
}
|
|
258
|
+
// 6. External-state grounding: obligation + complete requires fresh external verification
|
|
259
|
+
if (intent === "complete" && currentObligation && !sawExternalStateQuery && !sawSteeringFollowUp) {
|
|
260
|
+
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 settle.";
|
|
261
|
+
}
|
|
262
|
+
return null;
|
|
263
|
+
}
|
|
135
264
|
function upsertSystemPrompt(messages, systemText) {
|
|
136
265
|
const systemMessage = { role: "system", content: systemText };
|
|
137
266
|
if (messages[0]?.role === "system") {
|
|
@@ -237,49 +366,76 @@ function isContextOverflow(err) {
|
|
|
237
366
|
return true;
|
|
238
367
|
return false;
|
|
239
368
|
}
|
|
240
|
-
//
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
//
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
369
|
+
// HTTP statuses that will never become retryable on their own — the request is
|
|
370
|
+
// semantically wrong (malformed, unauthorized, missing route, etc.) and the
|
|
371
|
+
// caller has to do something different before it can succeed.
|
|
372
|
+
const NON_RETRYABLE_HTTP_STATUSES = new Set([
|
|
373
|
+
400, // Bad Request — malformed payload
|
|
374
|
+
401, // Unauthorized — credentials invalid/expired
|
|
375
|
+
403, // Forbidden — credentials lack permission
|
|
376
|
+
404, // Not Found — model/route doesn't exist
|
|
377
|
+
422, // Unprocessable Entity — semantic validation failure
|
|
378
|
+
]);
|
|
379
|
+
// Provider-classified error categories that we never retry. usage-limit is
|
|
380
|
+
// distinct from rate-limit: rate limits clear in seconds (retryable), usage
|
|
381
|
+
// limits are billing quotas that take hours/days to reset.
|
|
382
|
+
const NON_RETRYABLE_CLASSIFICATIONS = new Set([
|
|
383
|
+
"auth-failure",
|
|
384
|
+
"usage-limit",
|
|
385
|
+
]);
|
|
386
|
+
// Default policy: retry every error from the provider, EXCEPT the small set
|
|
387
|
+
// above. The user explicitly requested this — past behavior was to retry only
|
|
388
|
+
// on a known-transient list, which silently dropped real harness/SDK timeouts
|
|
389
|
+
// (e.g. OpenAI SDK's "Request timed out." has no err.code and no status, so
|
|
390
|
+
// the substring matchers missed it).
|
|
391
|
+
function isRetryBlocked(error, classification) {
|
|
392
|
+
const status = error.status;
|
|
393
|
+
if (status !== undefined && NON_RETRYABLE_HTTP_STATUSES.has(status))
|
|
252
394
|
return true;
|
|
253
|
-
if (
|
|
254
|
-
return true;
|
|
255
|
-
if (msg.includes("ECONNRESET") || msg.includes("ETIMEDOUT"))
|
|
256
|
-
return true;
|
|
257
|
-
if (msg.includes("socket hang up"))
|
|
258
|
-
return true;
|
|
259
|
-
if (msg.includes("getaddrinfo"))
|
|
260
|
-
return true;
|
|
261
|
-
// HTTP 429 / 500 / 502 / 503 / 504
|
|
262
|
-
const status = err.status;
|
|
263
|
-
if (status === 429 || status === 500 || status === 502 || status === 503 || status === 504)
|
|
395
|
+
if (NON_RETRYABLE_CLASSIFICATIONS.has(classification))
|
|
264
396
|
return true;
|
|
265
397
|
return false;
|
|
266
398
|
}
|
|
267
|
-
function classifyTransientError(err) {
|
|
268
|
-
if (!(err instanceof Error))
|
|
269
|
-
return "unknown error";
|
|
270
|
-
const status = err.status;
|
|
271
|
-
if (status === 429)
|
|
272
|
-
return "rate limited";
|
|
273
|
-
if (status === 401 || status === 403)
|
|
274
|
-
return "auth error";
|
|
275
|
-
if (status && status >= 500)
|
|
276
|
-
return "server error";
|
|
277
|
-
return "network error";
|
|
278
|
-
}
|
|
279
399
|
const MAX_RETRIES = 3;
|
|
280
400
|
const RETRY_BASE_MS = 2000;
|
|
401
|
+
const RETRY_LABELS = {
|
|
402
|
+
"auth-failure": "auth error",
|
|
403
|
+
"usage-limit": "usage limit",
|
|
404
|
+
"rate-limit": "rate limited",
|
|
405
|
+
"server-error": "server error",
|
|
406
|
+
"network-error": "network error",
|
|
407
|
+
"unknown": "error",
|
|
408
|
+
};
|
|
409
|
+
function buildAuthFailureGuidance(provider, model, agentName, detail) {
|
|
410
|
+
const mismatch = (0, provider_models_1.getProviderModelMismatchMessage)(provider, model);
|
|
411
|
+
const modelLabel = model
|
|
412
|
+
? mismatch
|
|
413
|
+
? `${provider} [configured model: ${model}]`
|
|
414
|
+
: `${provider} (${model})`
|
|
415
|
+
: provider;
|
|
416
|
+
const lines = [`${modelLabel} authentication failed.`];
|
|
417
|
+
const cleanDetail = detail.replace(/\s+/g, " ").trim();
|
|
418
|
+
if (cleanDetail)
|
|
419
|
+
lines.push(`provider detail: ${cleanDetail.length > 300 ? `${cleanDetail.slice(0, 297)}...` : cleanDetail}`);
|
|
420
|
+
lines.push("");
|
|
421
|
+
lines.push("To keep using this provider:");
|
|
422
|
+
lines.push(` 1. Run \`ouro auth --agent ${agentName} --provider ${provider}\``);
|
|
423
|
+
if (mismatch) {
|
|
424
|
+
const defaultModel = (0, provider_models_1.getDefaultModelForProvider)(provider);
|
|
425
|
+
lines.push("");
|
|
426
|
+
lines.push("Config warning:");
|
|
427
|
+
lines.push(` - ${mismatch}`);
|
|
428
|
+
lines.push(" - Repair the configured model with:");
|
|
429
|
+
lines.push(` \`ouro config model --agent ${agentName} --facing human ${defaultModel}\``);
|
|
430
|
+
lines.push(` \`ouro config model --agent ${agentName} --facing agent ${defaultModel}\``);
|
|
431
|
+
}
|
|
432
|
+
lines.push("");
|
|
433
|
+
lines.push(`To use another configured provider instead, run \`ouro auth switch --agent ${agentName} --provider <provider>\`.`);
|
|
434
|
+
return lines.join("\n");
|
|
435
|
+
}
|
|
281
436
|
async function runAgent(messages, callbacks, channel, signal, options) {
|
|
282
|
-
const
|
|
437
|
+
const facing = (0, channel_1.channelToFacing)(channel);
|
|
438
|
+
const providerRuntime = getProviderRuntime(facing);
|
|
283
439
|
const provider = providerRuntime.id;
|
|
284
440
|
const toolChoiceRequired = options?.toolChoiceRequired ?? true;
|
|
285
441
|
const traceId = options?.traceId;
|
|
@@ -305,7 +461,12 @@ async function runAgent(messages, callbacks, channel, signal, options) {
|
|
|
305
461
|
// so turn execution remains consistent and non-fatal.
|
|
306
462
|
if (channel) {
|
|
307
463
|
try {
|
|
308
|
-
const
|
|
464
|
+
const buildSystemOptions = {
|
|
465
|
+
...options,
|
|
466
|
+
providerCapabilities: providerRuntime.capabilities,
|
|
467
|
+
supportedReasoningEfforts: providerRuntime.supportedReasoningEfforts,
|
|
468
|
+
};
|
|
469
|
+
const refreshed = await (0, prompt_1.buildSystem)(channel, buildSystemOptions, currentContext);
|
|
309
470
|
upsertSystemPrompt(messages, refreshed);
|
|
310
471
|
}
|
|
311
472
|
catch (error) {
|
|
@@ -328,31 +489,78 @@ async function runAgent(messages, callbacks, channel, signal, options) {
|
|
|
328
489
|
}
|
|
329
490
|
}
|
|
330
491
|
await (0, associative_recall_1.injectAssociativeRecall)(messages);
|
|
331
|
-
// kickCount and lastKickReason preserved but unused while kick detection is disabled.
|
|
332
|
-
// let kickCount = 0;
|
|
333
|
-
// let lastKickReason: KickReason | null = null;
|
|
334
492
|
let done = false;
|
|
335
493
|
let lastUsage;
|
|
336
494
|
let overflowRetried = false;
|
|
337
495
|
let retryCount = 0;
|
|
496
|
+
let outcome = "settled";
|
|
497
|
+
let completion;
|
|
498
|
+
let terminalError;
|
|
499
|
+
let terminalErrorClassification;
|
|
500
|
+
let sawSteeringFollowUp = false;
|
|
501
|
+
let mustResolveBeforeHandoffActive = options?.mustResolveBeforeHandoff === true;
|
|
502
|
+
let currentReasoningEffort = "medium";
|
|
503
|
+
let sawSendMessageSelf = false;
|
|
504
|
+
let sawPonder = false;
|
|
505
|
+
let sawQuerySession = false;
|
|
506
|
+
let sawBridgeManage = false;
|
|
507
|
+
let sawExternalStateQuery = false;
|
|
508
|
+
const toolLoopState = (0, tool_loop_1.createToolLoopState)();
|
|
509
|
+
const toolFrictionLedger = (0, tool_friction_1.createToolFrictionLedger)();
|
|
338
510
|
// Prevent MaxListenersExceeded warning — each iteration adds a listener
|
|
339
511
|
try {
|
|
340
512
|
require("events").setMaxListeners(50, signal);
|
|
341
513
|
}
|
|
342
514
|
catch { /* unsupported */ }
|
|
343
515
|
const toolPreferences = currentContext?.friend?.toolPreferences;
|
|
344
|
-
const baseTools = options?.tools ?? (0, tools_1.getToolsForChannel)(channel ? (0, channel_1.getChannelCapabilities)(channel) : undefined, toolPreferences && Object.keys(toolPreferences).length > 0 ? toolPreferences : undefined, currentContext);
|
|
516
|
+
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, options?.mcpManager, providerRuntime.model);
|
|
517
|
+
// Augment tool context with reasoning effort controls from provider
|
|
518
|
+
const augmentedToolContext = options?.toolContext
|
|
519
|
+
? {
|
|
520
|
+
...options.toolContext,
|
|
521
|
+
supportedReasoningEfforts: providerRuntime.supportedReasoningEfforts,
|
|
522
|
+
setReasoningEffort: (level) => { currentReasoningEffort = level; },
|
|
523
|
+
activeWorkFrame: options?.activeWorkFrame,
|
|
524
|
+
}
|
|
525
|
+
: undefined;
|
|
345
526
|
// Rebase provider-owned turn state from canonical messages at user-turn start.
|
|
346
527
|
// This prevents stale provider caches from replaying prior-turn context.
|
|
347
528
|
providerRuntime.resetTurnState(messages);
|
|
348
529
|
while (!done) {
|
|
349
|
-
//
|
|
350
|
-
//
|
|
351
|
-
//
|
|
352
|
-
//
|
|
353
|
-
|
|
530
|
+
// Channel-based tool filtering:
|
|
531
|
+
// - Inner dialog: exclude send_message (delivery via surface), observe (no one to observe)
|
|
532
|
+
// - 1:1 sessions: exclude observe (can't ignore someone talking directly to you)
|
|
533
|
+
// - Group chats: observe available
|
|
534
|
+
//
|
|
535
|
+
// ponder, settle/rest, surface, and observe are always assembled based on channel context.
|
|
536
|
+
// ponder is available in ALL channels (outer: think privately, inner: keep turning).
|
|
537
|
+
// Inner dialog gets restTool instead of settleTool (rest = end turn, gated by attention queue).
|
|
538
|
+
// toolChoiceRequired only controls whether tool_choice: "required" is set in the API call.
|
|
539
|
+
const isInnerDialog = channel === "inner";
|
|
540
|
+
const filteredBaseTools = isInnerDialog
|
|
541
|
+
? baseTools.filter((t) => t.function.name !== "send_message")
|
|
542
|
+
: baseTools;
|
|
543
|
+
const activeTools = [
|
|
544
|
+
...filteredBaseTools,
|
|
545
|
+
tools_1.ponderTool,
|
|
546
|
+
...(isInnerDialog ? [tools_2.surfaceToolDef, tools_1.restTool] : []),
|
|
547
|
+
...((currentContext?.isGroupChat || options?.isReactionSignal) && !isInnerDialog ? [tools_1.observeTool] : []),
|
|
548
|
+
...(!isInnerDialog ? [tools_1.settleTool] : []),
|
|
549
|
+
];
|
|
354
550
|
const steeringFollowUps = options?.drainSteeringFollowUps?.() ?? [];
|
|
355
551
|
if (steeringFollowUps.length > 0) {
|
|
552
|
+
const hasSupersedingFollowUp = steeringFollowUps.some((followUp) => followUp.effect === "clear_and_supersede");
|
|
553
|
+
if (hasSupersedingFollowUp) {
|
|
554
|
+
mustResolveBeforeHandoffActive = false;
|
|
555
|
+
options?.setMustResolveBeforeHandoff?.(false);
|
|
556
|
+
outcome = "superseded";
|
|
557
|
+
break;
|
|
558
|
+
}
|
|
559
|
+
if (steeringFollowUps.some((followUp) => followUp.effect === "set_no_handoff")) {
|
|
560
|
+
mustResolveBeforeHandoffActive = true;
|
|
561
|
+
options?.setMustResolveBeforeHandoff?.(true);
|
|
562
|
+
}
|
|
563
|
+
sawSteeringFollowUp = true;
|
|
356
564
|
for (const followUp of steeringFollowUps) {
|
|
357
565
|
messages.push({ role: "user", content: followUp.text });
|
|
358
566
|
}
|
|
@@ -360,8 +568,10 @@ async function runAgent(messages, callbacks, channel, signal, options) {
|
|
|
360
568
|
}
|
|
361
569
|
// Yield so pending I/O (stdin Ctrl-C) can be processed between iterations
|
|
362
570
|
await new Promise((r) => setImmediate(r));
|
|
363
|
-
if (signal?.aborted)
|
|
571
|
+
if (signal?.aborted) {
|
|
572
|
+
outcome = "aborted";
|
|
364
573
|
break;
|
|
574
|
+
}
|
|
365
575
|
try {
|
|
366
576
|
callbacks.onModelStart();
|
|
367
577
|
const result = await providerRuntime.streamTurn({
|
|
@@ -371,6 +581,8 @@ async function runAgent(messages, callbacks, channel, signal, options) {
|
|
|
371
581
|
signal,
|
|
372
582
|
traceId,
|
|
373
583
|
toolChoiceRequired,
|
|
584
|
+
reasoningEffort: currentReasoningEffort,
|
|
585
|
+
eagerSettleStreaming: true,
|
|
374
586
|
});
|
|
375
587
|
// Track usage from the latest API call
|
|
376
588
|
if (result.usage)
|
|
@@ -394,52 +606,76 @@ async function runAgent(messages, callbacks, channel, signal, options) {
|
|
|
394
606
|
if (reasoningItems.length > 0) {
|
|
395
607
|
msg._reasoning_items = reasoningItems;
|
|
396
608
|
}
|
|
609
|
+
// Store thinking blocks (Anthropic) on the assistant message for round-tripping
|
|
610
|
+
const thinkingItems = result.outputItems.filter((item) => "type" in item && (item.type === "thinking" || item.type === "redacted_thinking"));
|
|
611
|
+
if (thinkingItems.length > 0) {
|
|
612
|
+
msg._thinking_blocks = thinkingItems;
|
|
613
|
+
}
|
|
614
|
+
// Phase annotation for Codex provider
|
|
615
|
+
const hasPhaseAnnotation = providerRuntime.capabilities.has("phase-annotation");
|
|
616
|
+
const isSoleSettle = result.toolCalls.length === 1 && result.toolCalls[0].name === "settle";
|
|
617
|
+
if (hasPhaseAnnotation) {
|
|
618
|
+
msg.phase = isSoleSettle ? "settle" : "commentary";
|
|
619
|
+
}
|
|
397
620
|
if (!result.toolCalls.length) {
|
|
398
|
-
//
|
|
399
|
-
//
|
|
400
|
-
//
|
|
401
|
-
// accept the response as-is rather than risk false-positive kicks.
|
|
402
|
-
//
|
|
403
|
-
// Preserved for future use — re-enable by uncommenting:
|
|
404
|
-
// const kick = detectKick(result.content, options);
|
|
405
|
-
// if (kick) {
|
|
406
|
-
// kickCount++;
|
|
407
|
-
// lastKickReason = kick.reason;
|
|
408
|
-
// callbacks.onKick?.();
|
|
409
|
-
// const kickContent = result.content
|
|
410
|
-
// ? result.content + "\n\n" + kick.message
|
|
411
|
-
// : kick.message;
|
|
412
|
-
// messages.push({ role: "assistant", content: kickContent });
|
|
413
|
-
// providerRuntime.resetTurnState(messages);
|
|
414
|
-
// continue;
|
|
415
|
-
// }
|
|
621
|
+
// No tool calls — accept response as-is.
|
|
622
|
+
// (Kick detection disabled; tool_choice: required + settle
|
|
623
|
+
// is the primary loop control. See src/heart/kicks.ts to re-enable.)
|
|
416
624
|
messages.push(msg);
|
|
417
625
|
done = true;
|
|
418
626
|
}
|
|
419
627
|
else {
|
|
420
|
-
// Check for
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
let answer;
|
|
426
|
-
try {
|
|
427
|
-
const parsed = JSON.parse(result.toolCalls[0].arguments);
|
|
428
|
-
if (typeof parsed === "string") {
|
|
429
|
-
answer = parsed;
|
|
430
|
-
}
|
|
431
|
-
else if (parsed.answer != null) {
|
|
432
|
-
answer = parsed.answer;
|
|
433
|
-
}
|
|
434
|
-
// else: valid JSON but no answer field — answer stays undefined (retry)
|
|
628
|
+
// Check for settle sole call: intercept before tool execution
|
|
629
|
+
if (isSoleSettle) {
|
|
630
|
+
/* v8 ignore next -- defensive: JSON.parse catch for malformed settle args @preserve */
|
|
631
|
+
const settleArgs = (() => { try {
|
|
632
|
+
return JSON.parse(result.toolCalls[0].arguments);
|
|
435
633
|
}
|
|
436
634
|
catch {
|
|
437
|
-
|
|
635
|
+
return {};
|
|
636
|
+
} })();
|
|
637
|
+
callbacks.onToolStart("settle", settleArgs);
|
|
638
|
+
// Inner dialog attention queue gate: reject settle if items remain
|
|
639
|
+
const attentionQueue = (augmentedToolContext ?? options?.toolContext)?.delegatedOrigins;
|
|
640
|
+
if (isInnerDialog && attentionQueue && attentionQueue.length > 0) {
|
|
641
|
+
callbacks.onToolEnd("settle", (0, tools_1.summarizeArgs)("settle", settleArgs), false);
|
|
642
|
+
callbacks.onClearText?.();
|
|
643
|
+
messages.push(msg);
|
|
644
|
+
const gateMessage = "you're holding thoughts someone is waiting for — surface them before you settle.";
|
|
645
|
+
messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: gateMessage });
|
|
646
|
+
providerRuntime.appendToolOutput(result.toolCalls[0].id, gateMessage);
|
|
647
|
+
continue;
|
|
438
648
|
}
|
|
439
|
-
|
|
440
|
-
|
|
649
|
+
// Extract answer from the tool call arguments.
|
|
650
|
+
// Supports: {"answer":"text","intent":"..."} or "text" (JSON string).
|
|
651
|
+
const { answer, intent } = parseSettlePayload(result.toolCalls[0].arguments);
|
|
652
|
+
// Inner dialog settle: no CompletionMetadata, "(settled)" ack
|
|
653
|
+
if (isInnerDialog) {
|
|
654
|
+
callbacks.onToolEnd("settle", (0, tools_1.summarizeArgs)("settle", settleArgs), true);
|
|
655
|
+
messages.push(msg);
|
|
656
|
+
const settled = "(settled)";
|
|
657
|
+
messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: settled });
|
|
658
|
+
providerRuntime.appendToolOutput(result.toolCalls[0].id, settled);
|
|
659
|
+
outcome = "settled";
|
|
660
|
+
done = true;
|
|
661
|
+
continue;
|
|
662
|
+
}
|
|
663
|
+
const retryError = getSettleRetryError(mustResolveBeforeHandoffActive, intent, sawSteeringFollowUp, options?.delegationDecision, sawSendMessageSelf, sawPonder, sawQuerySession, options?.currentObligation ?? null, options?.activeWorkFrame?.inner?.job, sawExternalStateQuery);
|
|
664
|
+
const deliveredAnswer = answer;
|
|
665
|
+
const validDirectReply = mustResolveBeforeHandoffActive && intent === "direct_reply" && sawSteeringFollowUp;
|
|
666
|
+
const validTerminalIntent = intent === "complete" || intent === "blocked";
|
|
667
|
+
const validClosure = deliveredAnswer != null
|
|
668
|
+
&& !retryError
|
|
669
|
+
&& (!mustResolveBeforeHandoffActive || validDirectReply || validTerminalIntent);
|
|
670
|
+
if (validClosure) {
|
|
671
|
+
callbacks.onToolEnd("settle", (0, tools_1.summarizeArgs)("settle", settleArgs), true);
|
|
672
|
+
completion = {
|
|
673
|
+
answer: deliveredAnswer,
|
|
674
|
+
intent: validDirectReply ? "direct_reply" : intent === "blocked" ? "blocked" : "complete",
|
|
675
|
+
};
|
|
676
|
+
if (result.settleStreamed) {
|
|
441
677
|
// The streaming layer already parsed and emitted the answer
|
|
442
|
-
// progressively via
|
|
678
|
+
// progressively via SettleParser. Skip clearing and
|
|
443
679
|
// re-emitting to avoid double-delivery.
|
|
444
680
|
}
|
|
445
681
|
else {
|
|
@@ -447,37 +683,110 @@ async function runAgent(messages, callbacks, channel, signal, options) {
|
|
|
447
683
|
callbacks.onClearText?.();
|
|
448
684
|
// Emit the answer through the callback pipeline so channels receive it.
|
|
449
685
|
// Never truncate -- channel adapters handle splitting long messages.
|
|
450
|
-
callbacks.onTextChunk(
|
|
686
|
+
callbacks.onTextChunk(deliveredAnswer);
|
|
451
687
|
}
|
|
452
|
-
// Keep the full assistant message (with tool_calls) for debuggability,
|
|
453
|
-
// plus a synthetic tool response so the conversation stays valid on resume.
|
|
454
688
|
messages.push(msg);
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
689
|
+
if (validDirectReply) {
|
|
690
|
+
const resumeWork = "direct reply delivered. resume the unresolved obligation now and keep working until you can finish or clearly report that you are blocked.";
|
|
691
|
+
messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: resumeWork });
|
|
692
|
+
providerRuntime.appendToolOutput(result.toolCalls[0].id, resumeWork);
|
|
693
|
+
}
|
|
694
|
+
else {
|
|
695
|
+
const delivered = "(delivered)";
|
|
696
|
+
messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: delivered });
|
|
697
|
+
providerRuntime.appendToolOutput(result.toolCalls[0].id, delivered);
|
|
698
|
+
outcome = intent === "blocked" ? "blocked" : "settled";
|
|
699
|
+
done = true;
|
|
700
|
+
}
|
|
458
701
|
}
|
|
459
702
|
else {
|
|
460
|
-
// Answer is undefined -- the model's
|
|
703
|
+
// Answer is undefined -- the model's settle was incomplete or
|
|
461
704
|
// malformed. Clear any partial streamed text or noise, then push the
|
|
462
705
|
// assistant msg + error tool result and let the model try again.
|
|
706
|
+
callbacks.onToolEnd("settle", (0, tools_1.summarizeArgs)("settle", settleArgs), false);
|
|
463
707
|
callbacks.onClearText?.();
|
|
464
|
-
const retryError = "your final_answer was incomplete or malformed. call final_answer again with your complete response.";
|
|
465
708
|
messages.push(msg);
|
|
466
|
-
|
|
467
|
-
|
|
709
|
+
const toolRetryMessage = retryError
|
|
710
|
+
?? "your settle was incomplete or malformed. call settle again with your complete response.";
|
|
711
|
+
messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: toolRetryMessage });
|
|
712
|
+
providerRuntime.appendToolOutput(result.toolCalls[0].id, toolRetryMessage);
|
|
713
|
+
}
|
|
714
|
+
continue;
|
|
715
|
+
}
|
|
716
|
+
// Check for observe sole call: intercept before tool execution
|
|
717
|
+
const isSoleObserve = result.toolCalls.length === 1 && result.toolCalls[0].name === "observe";
|
|
718
|
+
if (isSoleObserve) {
|
|
719
|
+
/* v8 ignore next -- defensive: JSON.parse catch for malformed observe args @preserve */
|
|
720
|
+
const observeArgs = (() => { try {
|
|
721
|
+
return JSON.parse(result.toolCalls[0].arguments);
|
|
722
|
+
}
|
|
723
|
+
catch {
|
|
724
|
+
return {};
|
|
725
|
+
} })();
|
|
726
|
+
let reason;
|
|
727
|
+
if (typeof observeArgs?.reason === "string")
|
|
728
|
+
reason = observeArgs.reason;
|
|
729
|
+
callbacks.onToolStart("observe", observeArgs);
|
|
730
|
+
(0, runtime_1.emitNervesEvent)({
|
|
731
|
+
component: "engine",
|
|
732
|
+
event: "engine.observe",
|
|
733
|
+
message: "agent observed without responding",
|
|
734
|
+
meta: { ...(reason ? { reason } : {}) },
|
|
735
|
+
});
|
|
736
|
+
callbacks.onToolEnd("observe", (0, tools_1.summarizeArgs)("observe", observeArgs), true);
|
|
737
|
+
messages.push(msg);
|
|
738
|
+
const silenced = "(silenced)";
|
|
739
|
+
messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: silenced });
|
|
740
|
+
providerRuntime.appendToolOutput(result.toolCalls[0].id, silenced);
|
|
741
|
+
outcome = "observed";
|
|
742
|
+
done = true;
|
|
743
|
+
continue;
|
|
744
|
+
}
|
|
745
|
+
// Check for rest sole call: intercept before tool execution
|
|
746
|
+
const isSoleRest = result.toolCalls.length === 1 && result.toolCalls[0].name === "rest";
|
|
747
|
+
if (isSoleRest) {
|
|
748
|
+
const restArgs = (() => { try {
|
|
749
|
+
return JSON.parse(result.toolCalls[0].arguments);
|
|
750
|
+
}
|
|
751
|
+
catch {
|
|
752
|
+
return {};
|
|
753
|
+
} })();
|
|
754
|
+
callbacks.onToolStart("rest", restArgs);
|
|
755
|
+
// Attention queue gate: reject rest if items remain
|
|
756
|
+
const attentionQueue = (augmentedToolContext ?? options?.toolContext)?.delegatedOrigins;
|
|
757
|
+
if (attentionQueue && attentionQueue.length > 0) {
|
|
758
|
+
callbacks.onToolEnd("rest", (0, tools_1.summarizeArgs)("rest", restArgs), false);
|
|
759
|
+
messages.push(msg);
|
|
760
|
+
const gateMessage = "you're holding thoughts someone is waiting for — surface them before you rest.";
|
|
761
|
+
messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: gateMessage });
|
|
762
|
+
providerRuntime.appendToolOutput(result.toolCalls[0].id, gateMessage);
|
|
763
|
+
continue;
|
|
468
764
|
}
|
|
765
|
+
callbacks.onToolEnd("rest", (0, tools_1.summarizeArgs)("rest", restArgs), true);
|
|
766
|
+
messages.push(msg);
|
|
767
|
+
const ack = "(resting)";
|
|
768
|
+
messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: ack });
|
|
769
|
+
providerRuntime.appendToolOutput(result.toolCalls[0].id, ack);
|
|
770
|
+
(0, runtime_1.emitNervesEvent)({
|
|
771
|
+
component: "engine",
|
|
772
|
+
event: "engine.rested",
|
|
773
|
+
message: "resting until next heartbeat",
|
|
774
|
+
meta: { ...(typeof restArgs?.status === "string" ? { status: restArgs.status } : {}) },
|
|
775
|
+
});
|
|
776
|
+
outcome = "rested";
|
|
777
|
+
done = true;
|
|
469
778
|
continue;
|
|
470
779
|
}
|
|
471
780
|
messages.push(msg);
|
|
472
|
-
//
|
|
781
|
+
// Execute tools (sole-call tools in mixed calls are rejected inline)
|
|
473
782
|
for (const tc of result.toolCalls) {
|
|
474
783
|
if (signal?.aborted)
|
|
475
784
|
break;
|
|
476
|
-
//
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
messages.push({ role: "tool", tool_call_id: tc.id, content:
|
|
480
|
-
providerRuntime.appendToolOutput(tc.id,
|
|
785
|
+
// Reject sole-call tools when mixed with other tool calls
|
|
786
|
+
const soleCallRejection = SOLE_CALL_REJECTION[tc.name];
|
|
787
|
+
if (soleCallRejection) {
|
|
788
|
+
messages.push({ role: "tool", tool_call_id: tc.id, content: soleCallRejection });
|
|
789
|
+
providerRuntime.appendToolOutput(tc.id, soleCallRejection);
|
|
481
790
|
continue;
|
|
482
791
|
}
|
|
483
792
|
let args = {};
|
|
@@ -487,37 +796,175 @@ async function runAgent(messages, callbacks, channel, signal, options) {
|
|
|
487
796
|
catch {
|
|
488
797
|
/* ignore */
|
|
489
798
|
}
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
799
|
+
if (tc.name === "send_message" && args.friendId === "self") {
|
|
800
|
+
sawSendMessageSelf = true;
|
|
801
|
+
}
|
|
802
|
+
if (tc.name === "ponder") {
|
|
803
|
+
const parsedArgs = normalizeLegacyPonderArgs(parsePonderPayload(tc.arguments));
|
|
804
|
+
const argSummary = (0, tools_1.summarizeArgs)(tc.name, parsedArgs);
|
|
805
|
+
callbacks.onToolStart(tc.name, parsedArgs);
|
|
806
|
+
let toolResult;
|
|
807
|
+
let success = false;
|
|
808
|
+
try {
|
|
809
|
+
const action = parsedArgs.action ?? "create";
|
|
810
|
+
const currentSession = (augmentedToolContext ?? options?.toolContext)?.currentSession;
|
|
811
|
+
const currentOrigin = currentSession
|
|
812
|
+
? { friendId: currentSession.friendId, channel: currentSession.channel, key: currentSession.key }
|
|
813
|
+
: undefined;
|
|
814
|
+
const isInnerChannel = currentOrigin?.friendId === "self" && currentOrigin?.channel === "inner";
|
|
815
|
+
const successCriteria = parseSuccessCriteria(parsedArgs.success_criteria);
|
|
816
|
+
const payload = parsePacketPayload(parsedArgs.payload_json);
|
|
817
|
+
let packet;
|
|
818
|
+
let returnObligationId = null;
|
|
819
|
+
let resultAction = "created";
|
|
820
|
+
if (action === "create") {
|
|
821
|
+
const kind = parsedArgs.kind;
|
|
822
|
+
const objective = typeof parsedArgs.objective === "string" ? parsedArgs.objective.trim() : "";
|
|
823
|
+
const summary = typeof parsedArgs.summary === "string" ? parsedArgs.summary.trim() : "";
|
|
824
|
+
if (!kind || !objective || !successCriteria || !payload) {
|
|
825
|
+
throw new Error("ponder create requires kind, objective, success_criteria, and valid payload_json.");
|
|
826
|
+
}
|
|
827
|
+
const agentRoot = (0, identity_2.getAgentRoot)();
|
|
828
|
+
let relatedObligationId;
|
|
829
|
+
if (currentOrigin && !isInnerChannel) {
|
|
830
|
+
try {
|
|
831
|
+
const obligation = (0, obligations_1.createObligation)(agentRoot, {
|
|
832
|
+
origin: currentOrigin,
|
|
833
|
+
content: objective,
|
|
834
|
+
});
|
|
835
|
+
relatedObligationId = obligation.id;
|
|
836
|
+
}
|
|
837
|
+
catch {
|
|
838
|
+
relatedObligationId = undefined;
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
const frictionSignature = kind === "harness_friction" && typeof payload.frictionSignature === "string"
|
|
842
|
+
? payload.frictionSignature
|
|
843
|
+
: null;
|
|
844
|
+
const existing = frictionSignature && currentOrigin
|
|
845
|
+
? (0, packets_1.findHarnessFrictionPacket)(agentRoot, currentOrigin, frictionSignature)
|
|
846
|
+
: null;
|
|
847
|
+
if (existing) {
|
|
848
|
+
resultAction = "revised";
|
|
849
|
+
returnObligationId = existing.relatedReturnObligationId ?? null;
|
|
850
|
+
packet = existing.status === "drafting"
|
|
851
|
+
? (0, packets_1.revisePonderPacket)(agentRoot, existing.id, {
|
|
852
|
+
kind,
|
|
853
|
+
objective,
|
|
854
|
+
summary,
|
|
855
|
+
successCriteria,
|
|
856
|
+
payload,
|
|
857
|
+
})
|
|
858
|
+
: existing;
|
|
859
|
+
}
|
|
860
|
+
else {
|
|
861
|
+
returnObligationId = (0, obligations_1.generateObligationId)(Date.now());
|
|
862
|
+
packet = (0, packets_1.createPonderPacket)(agentRoot, {
|
|
863
|
+
kind,
|
|
864
|
+
objective,
|
|
865
|
+
summary,
|
|
866
|
+
successCriteria,
|
|
867
|
+
...(currentOrigin ? { origin: currentOrigin } : {}),
|
|
868
|
+
...(relatedObligationId ? { relatedObligationId } : {}),
|
|
869
|
+
relatedReturnObligationId: returnObligationId,
|
|
870
|
+
...(parsedArgs.follows_packet_id ? { followsPacketId: parsedArgs.follows_packet_id } : {}),
|
|
871
|
+
payload,
|
|
872
|
+
});
|
|
873
|
+
(0, obligations_1.createReturnObligation)((0, identity_2.getAgentName)(), {
|
|
874
|
+
id: returnObligationId,
|
|
875
|
+
origin: currentOrigin ?? { friendId: "self", channel: "inner", key: "dialog" },
|
|
876
|
+
status: "queued",
|
|
877
|
+
delegatedContent: (summary || objective).length > 120 ? `${(summary || objective).slice(0, 117)}...` : (summary || objective),
|
|
878
|
+
packetId: packet.id,
|
|
879
|
+
createdAt: Date.now(),
|
|
880
|
+
});
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
else if (action === "revise") {
|
|
884
|
+
const packetId = typeof parsedArgs.packet_id === "string" ? parsedArgs.packet_id.trim() : "";
|
|
885
|
+
const kind = parsedArgs.kind;
|
|
886
|
+
const objective = typeof parsedArgs.objective === "string" ? parsedArgs.objective.trim() : "";
|
|
887
|
+
const summary = typeof parsedArgs.summary === "string" ? parsedArgs.summary.trim() : "";
|
|
888
|
+
if (!packetId || !kind || !objective || !successCriteria || !payload) {
|
|
889
|
+
throw new Error("ponder revise requires packet_id, kind, objective, success_criteria, and valid payload_json.");
|
|
890
|
+
}
|
|
891
|
+
packet = (0, packets_1.revisePonderPacket)((0, identity_2.getAgentRoot)(), packetId, {
|
|
892
|
+
kind,
|
|
893
|
+
objective,
|
|
894
|
+
summary,
|
|
895
|
+
successCriteria,
|
|
896
|
+
payload,
|
|
897
|
+
});
|
|
898
|
+
returnObligationId = packet.relatedReturnObligationId ?? null;
|
|
899
|
+
resultAction = "revised";
|
|
900
|
+
}
|
|
901
|
+
else {
|
|
902
|
+
throw new Error("ponder requires action=create or revise.");
|
|
903
|
+
}
|
|
904
|
+
try {
|
|
905
|
+
await (0, socket_client_1.requestInnerWake)((0, identity_2.getAgentName)());
|
|
906
|
+
}
|
|
907
|
+
catch { /* daemon may not be running */ }
|
|
908
|
+
sawPonder = true;
|
|
909
|
+
toolResult = buildPonderResult(packet, resultAction, returnObligationId);
|
|
910
|
+
success = true;
|
|
911
|
+
(0, runtime_1.emitNervesEvent)({
|
|
912
|
+
component: "engine",
|
|
913
|
+
event: "engine.ponder_packet",
|
|
914
|
+
message: "ponder packet touched",
|
|
915
|
+
meta: {
|
|
916
|
+
action: resultAction,
|
|
917
|
+
packetId: packet.id,
|
|
918
|
+
kind: packet.kind,
|
|
919
|
+
status: packet.status,
|
|
920
|
+
},
|
|
921
|
+
});
|
|
496
922
|
}
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
callbacks.onToolStart(tc.name, args);
|
|
500
|
-
callbacks.onToolEnd(tc.name, argSummary, false);
|
|
501
|
-
messages.push({ role: "tool", tool_call_id: tc.id, content: cancelled });
|
|
502
|
-
providerRuntime.appendToolOutput(tc.id, cancelled);
|
|
503
|
-
continue;
|
|
923
|
+
catch (error) {
|
|
924
|
+
toolResult = error instanceof Error ? error.message : String(error);
|
|
504
925
|
}
|
|
926
|
+
callbacks.onToolEnd(tc.name, argSummary, success);
|
|
927
|
+
messages.push({ role: "tool", tool_call_id: tc.id, content: toolResult });
|
|
928
|
+
providerRuntime.appendToolOutput(tc.id, toolResult);
|
|
929
|
+
continue;
|
|
930
|
+
}
|
|
931
|
+
/* v8 ignore next -- flag tested via truth-check integration tests @preserve */
|
|
932
|
+
if (tc.name === "query_session")
|
|
933
|
+
sawQuerySession = true;
|
|
934
|
+
/* v8 ignore next -- flag tested via truth-check integration tests @preserve */
|
|
935
|
+
if (tc.name === "bridge_manage")
|
|
936
|
+
sawBridgeManage = true;
|
|
937
|
+
/* v8 ignore next -- flag tested via truth-check integration tests @preserve */
|
|
938
|
+
if (isExternalStateQuery(tc.name, args))
|
|
939
|
+
sawExternalStateQuery = true;
|
|
940
|
+
const argSummary = (0, tools_1.summarizeArgs)(tc.name, args);
|
|
941
|
+
const toolLoop = (0, tool_loop_1.detectToolLoop)(toolLoopState, tc.name, args);
|
|
942
|
+
if (toolLoop.stuck) {
|
|
943
|
+
const rejection = `loop guard: ${toolLoop.message}`;
|
|
944
|
+
callbacks.onToolStart(tc.name, args);
|
|
945
|
+
callbacks.onToolEnd(tc.name, argSummary, false);
|
|
946
|
+
messages.push({ role: "tool", tool_call_id: tc.id, content: rejection });
|
|
947
|
+
providerRuntime.appendToolOutput(tc.id, rejection);
|
|
948
|
+
continue;
|
|
505
949
|
}
|
|
506
950
|
callbacks.onToolStart(tc.name, args);
|
|
507
951
|
let toolResult;
|
|
508
952
|
let success;
|
|
509
953
|
try {
|
|
510
954
|
const execToolFn = options?.execTool ?? tools_1.execTool;
|
|
511
|
-
toolResult = await execToolFn(tc.name, args, options?.toolContext);
|
|
955
|
+
toolResult = await execToolFn(tc.name, args, augmentedToolContext ?? options?.toolContext);
|
|
512
956
|
success = true;
|
|
513
957
|
}
|
|
514
958
|
catch (e) {
|
|
515
959
|
toolResult = `error: ${e}`;
|
|
516
960
|
success = false;
|
|
517
961
|
}
|
|
518
|
-
|
|
962
|
+
toolResult = (0, tool_friction_1.rewriteToolResultForModel)(tc.name, toolResult, toolFrictionLedger);
|
|
963
|
+
(0, tool_loop_1.recordToolOutcome)(toolLoopState, tc.name, args, toolResult, success);
|
|
964
|
+
callbacks.onToolEnd(tc.name, (0, tools_1.buildToolResultSummary)(tc.name, args, toolResult, success), success);
|
|
519
965
|
messages.push({ role: "tool", tool_call_id: tc.id, content: toolResult });
|
|
520
966
|
providerRuntime.appendToolOutput(tc.id, toolResult);
|
|
967
|
+
callbacks.onToolResult?.(messages);
|
|
521
968
|
}
|
|
522
969
|
}
|
|
523
970
|
}
|
|
@@ -525,6 +972,7 @@ async function runAgent(messages, callbacks, channel, signal, options) {
|
|
|
525
972
|
// Abort is not an error — just stop cleanly
|
|
526
973
|
if (signal?.aborted) {
|
|
527
974
|
stripLastToolCalls(messages);
|
|
975
|
+
outcome = "aborted";
|
|
528
976
|
break;
|
|
529
977
|
}
|
|
530
978
|
// Context overflow: trim aggressively and retry once
|
|
@@ -538,11 +986,45 @@ async function runAgent(messages, callbacks, channel, signal, options) {
|
|
|
538
986
|
callbacks.onError(new Error("context trimmed, retrying..."), "transient");
|
|
539
987
|
continue;
|
|
540
988
|
}
|
|
541
|
-
//
|
|
542
|
-
|
|
989
|
+
// Retry policy: retry every error EXCEPT those on the blocklist
|
|
990
|
+
// (NON_RETRYABLE_HTTP_STATUSES / NON_RETRYABLE_CLASSIFICATIONS).
|
|
991
|
+
// The classification still drives the user-facing label and the
|
|
992
|
+
// auth-failure guidance message below — it just no longer gates retries.
|
|
993
|
+
const errorForClassification = e instanceof Error ? e : /* v8 ignore next -- defensive @preserve */ new Error(String(e));
|
|
994
|
+
let providerClassification;
|
|
995
|
+
try {
|
|
996
|
+
providerClassification = providerRuntime.classifyError(errorForClassification);
|
|
997
|
+
}
|
|
998
|
+
catch {
|
|
999
|
+
/* v8 ignore next -- defensive: classifyError should not throw @preserve */
|
|
1000
|
+
providerClassification = "unknown";
|
|
1001
|
+
}
|
|
1002
|
+
const blocked = isRetryBlocked(errorForClassification, providerClassification);
|
|
1003
|
+
const shouldRetry = !blocked && retryCount < MAX_RETRIES;
|
|
1004
|
+
(0, runtime_1.emitNervesEvent)({
|
|
1005
|
+
level: shouldRetry ? "info" : "warn",
|
|
1006
|
+
event: shouldRetry ? "engine.provider_retry" : "engine.provider_retry_skip",
|
|
1007
|
+
component: "engine",
|
|
1008
|
+
message: shouldRetry
|
|
1009
|
+
? `provider error is retryable (attempt ${retryCount + 1}/${MAX_RETRIES})`
|
|
1010
|
+
: blocked
|
|
1011
|
+
? `provider error is on retry blocklist`
|
|
1012
|
+
: `provider error retries exhausted`,
|
|
1013
|
+
meta: {
|
|
1014
|
+
provider: providerRuntime.id,
|
|
1015
|
+
model: providerRuntime.model,
|
|
1016
|
+
retryCount,
|
|
1017
|
+
maxRetries: MAX_RETRIES,
|
|
1018
|
+
blocked,
|
|
1019
|
+
providerClassification,
|
|
1020
|
+
errorMessage: errorForClassification.message.slice(0, 200),
|
|
1021
|
+
httpStatus: e.status ?? null,
|
|
1022
|
+
},
|
|
1023
|
+
});
|
|
1024
|
+
if (shouldRetry) {
|
|
543
1025
|
retryCount++;
|
|
544
1026
|
const delay = RETRY_BASE_MS * Math.pow(2, retryCount - 1);
|
|
545
|
-
const cause =
|
|
1027
|
+
const cause = RETRY_LABELS[providerClassification];
|
|
546
1028
|
callbacks.onError(new Error(`${cause}, retrying in ${delay / 1000}s (${retryCount}/${MAX_RETRIES})...`), "transient");
|
|
547
1029
|
// Wait with abort support
|
|
548
1030
|
const aborted = await new Promise((resolve) => {
|
|
@@ -559,21 +1041,34 @@ async function runAgent(messages, callbacks, channel, signal, options) {
|
|
|
559
1041
|
});
|
|
560
1042
|
if (aborted) {
|
|
561
1043
|
stripLastToolCalls(messages);
|
|
1044
|
+
outcome = "aborted";
|
|
562
1045
|
break;
|
|
563
1046
|
}
|
|
564
1047
|
providerRuntime.resetTurnState(messages);
|
|
565
1048
|
continue;
|
|
566
1049
|
}
|
|
567
|
-
|
|
1050
|
+
terminalError = errorForClassification;
|
|
1051
|
+
terminalErrorClassification = providerClassification;
|
|
1052
|
+
/* v8 ignore start — auth-failure guidance: tested via provider error classification tests @preserve */
|
|
1053
|
+
if (terminalErrorClassification === "auth-failure") {
|
|
1054
|
+
const agentName = (0, identity_2.getAgentName)();
|
|
1055
|
+
const currentProvider = providerRuntime.id;
|
|
1056
|
+
callbacks.onError(new Error(buildAuthFailureGuidance(currentProvider, providerRuntime.model, agentName, terminalError.message)), "terminal");
|
|
1057
|
+
}
|
|
1058
|
+
else {
|
|
1059
|
+
callbacks.onError(terminalError, "terminal");
|
|
1060
|
+
}
|
|
1061
|
+
/* v8 ignore stop */
|
|
568
1062
|
(0, runtime_1.emitNervesEvent)({
|
|
569
1063
|
level: "error",
|
|
570
1064
|
event: "engine.error",
|
|
571
1065
|
trace_id: traceId,
|
|
572
1066
|
component: "engine",
|
|
573
|
-
message:
|
|
574
|
-
meta: {},
|
|
1067
|
+
message: terminalError.message,
|
|
1068
|
+
meta: { errorClassification: terminalErrorClassification },
|
|
575
1069
|
});
|
|
576
1070
|
stripLastToolCalls(messages);
|
|
1071
|
+
outcome = "errored";
|
|
577
1072
|
done = true;
|
|
578
1073
|
}
|
|
579
1074
|
}
|
|
@@ -582,7 +1077,12 @@ async function runAgent(messages, callbacks, channel, signal, options) {
|
|
|
582
1077
|
trace_id: traceId,
|
|
583
1078
|
component: "engine",
|
|
584
1079
|
message: "runAgent turn completed",
|
|
585
|
-
meta: { done },
|
|
1080
|
+
meta: { done, sawPonder, sawQuerySession, sawBridgeManage },
|
|
586
1081
|
});
|
|
587
|
-
return {
|
|
1082
|
+
return {
|
|
1083
|
+
usage: lastUsage,
|
|
1084
|
+
outcome,
|
|
1085
|
+
completion,
|
|
1086
|
+
...(terminalError ? { error: terminalError, errorClassification: terminalErrorClassification } : {}),
|
|
1087
|
+
};
|
|
588
1088
|
}
|