somora 2026.930.1
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/LICENSE +21 -0
- package/README.md +364 -0
- package/bin/node-version.d.mts +3 -0
- package/bin/node-version.mjs +45 -0
- package/bin/somora.mjs +77 -0
- package/config.example.yaml +896 -0
- package/docs/agents.md +677 -0
- package/docs/api.md +3194 -0
- package/docs/browser.md +317 -0
- package/docs/builder.md +331 -0
- package/docs/cache-strategy.md +373 -0
- package/docs/compaction.md +253 -0
- package/docs/display.md +154 -0
- package/docs/dream-phases.md +891 -0
- package/docs/files.md +387 -0
- package/docs/imagegen.md +366 -0
- package/docs/images/somora-hero.png +0 -0
- package/docs/images/somora-mobile-2026-05-23.png +0 -0
- package/docs/images/somora-tui-2026-05-23.png +0 -0
- package/docs/images/somora-web-2026-05-23.png +0 -0
- package/docs/index.md +178 -0
- package/docs/lsp.md +93 -0
- package/docs/mcp.md +332 -0
- package/docs/memory.md +407 -0
- package/docs/mobile.md +310 -0
- package/docs/models.md +379 -0
- package/docs/projects.md +425 -0
- package/docs/realtime-voice.md +293 -0
- package/docs/resources.md +230 -0
- package/docs/sampling.md +128 -0
- package/docs/security.md +189 -0
- package/docs/sentinel.md +295 -0
- package/docs/setup.md +1217 -0
- package/docs/skills.md +501 -0
- package/docs/team.md +189 -0
- package/docs/thinking.md +463 -0
- package/docs/tmux.md +399 -0
- package/docs/tools.md +117 -0
- package/docs/videogen.md +191 -0
- package/docs/voice.md +288 -0
- package/docs/web.md +893 -0
- package/docs/wiki.md +583 -0
- package/npm-shrinkwrap.json +5670 -0
- package/package.json +116 -0
- package/src/attachments/store.ts +196 -0
- package/src/browser/change-stream.ts +31 -0
- package/src/browser/display.ts +108 -0
- package/src/browser/policy.ts +121 -0
- package/src/browser/screencast.ts +374 -0
- package/src/browser/service.ts +1337 -0
- package/src/browser/session.ts +15 -0
- package/src/browser/snapshot.ts +40 -0
- package/src/cli/auth.ts +117 -0
- package/src/cli/codex.ts +41 -0
- package/src/cli/lsp.ts +71 -0
- package/src/cli/resolvers/clawhub.ts +400 -0
- package/src/cli/setup.ts +835 -0
- package/src/cli/skill.ts +619 -0
- package/src/cli/somora.ts +548 -0
- package/src/cli/systemd-unit.ts +72 -0
- package/src/cli/team.ts +148 -0
- package/src/cli/tui/activity-stream.ts +121 -0
- package/src/cli/tui/api.ts +479 -0
- package/src/cli/tui/app.tsx +1169 -0
- package/src/cli/tui/autocomplete.tsx +55 -0
- package/src/cli/tui/commands.ts +913 -0
- package/src/cli/tui/dream-glyphs.ts +22 -0
- package/src/cli/tui/footer.tsx +15 -0
- package/src/cli/tui/format.ts +28 -0
- package/src/cli/tui/header.tsx +279 -0
- package/src/cli/tui/index.tsx +120 -0
- package/src/cli/tui/markdown.tsx +63 -0
- package/src/cli/tui/sampling.ts +162 -0
- package/src/cli/tui/separator.tsx +13 -0
- package/src/cli/tui/state.ts +42 -0
- package/src/cli/tui/stream.ts +330 -0
- package/src/cli/tui/thinking-text.ts +54 -0
- package/src/cli/tui/turn-views.tsx +499 -0
- package/src/cli/tui/types.ts +410 -0
- package/src/cli/tui/work-queue.ts +161 -0
- package/src/cli/update-args.ts +55 -0
- package/src/cli/wiki.ts +406 -0
- package/src/compaction/index.ts +28 -0
- package/src/compaction/midturn.ts +210 -0
- package/src/compaction/policy.ts +154 -0
- package/src/compaction/summarize.ts +691 -0
- package/src/compaction/template.ts +90 -0
- package/src/compaction/types.ts +148 -0
- package/src/config/loader.ts +276 -0
- package/src/config/reload.ts +43 -0
- package/src/config/types.ts +2292 -0
- package/src/dream/deep-actions.ts +324 -0
- package/src/dream/deep-dispatcher.ts +237 -0
- package/src/dream/deep-llm.ts +388 -0
- package/src/dream/deep-prompts.ts +106 -0
- package/src/dream/deep-runner.ts +757 -0
- package/src/dream/deep-skip-cache.ts +199 -0
- package/src/dream/deep-worker.ts +229 -0
- package/src/dream/json-salvage.ts +34 -0
- package/src/dream/judge.ts +188 -0
- package/src/dream/loop-state.ts +286 -0
- package/src/dream/lucid-actions.ts +303 -0
- package/src/dream/lucid-batches.ts +94 -0
- package/src/dream/lucid-prompt.ts +82 -0
- package/src/dream/lucid-runner.ts +744 -0
- package/src/dream/lucid-storage.ts +277 -0
- package/src/dream/lucid-structure.ts +102 -0
- package/src/dream/lucid-types.ts +137 -0
- package/src/dream/lucid-worker.ts +224 -0
- package/src/dream/rem-dedup.ts +502 -0
- package/src/dream/rem-extract.ts +782 -0
- package/src/dream/rem-reset-run.ts +67 -0
- package/src/dream/rem-runner.ts +696 -0
- package/src/dream/rem-worker.ts +527 -0
- package/src/dream/review-window.ts +71 -0
- package/src/dream/scheduler-state.ts +129 -0
- package/src/dream/storage.ts +497 -0
- package/src/dream/types.ts +133 -0
- package/src/dream/wiki-context.ts +133 -0
- package/src/engine/a2a.ts +33 -0
- package/src/engine/availability.ts +63 -0
- package/src/engine/claude-cli.ts +969 -0
- package/src/engine/codex-app-server-client.ts +294 -0
- package/src/engine/codex-bin.ts +49 -0
- package/src/engine/codex-cli.ts +1014 -0
- package/src/engine/codex-dynamic-tools.ts +239 -0
- package/src/engine/codex-home.ts +63 -0
- package/src/engine/codex-thread-config.ts +74 -0
- package/src/engine/context-budget.ts +257 -0
- package/src/engine/engine-meta-labels.ts +211 -0
- package/src/engine/grok-cli.ts +941 -0
- package/src/engine/inline-think.ts +59 -0
- package/src/engine/model-availability.ts +105 -0
- package/src/engine/openai-compatible.ts +2464 -0
- package/src/engine/reasoning-retry.ts +83 -0
- package/src/engine/registry.ts +13 -0
- package/src/engine/replay.ts +195 -0
- package/src/engine/sampling.ts +90 -0
- package/src/engine/thinking-params.ts +264 -0
- package/src/engine/types.ts +269 -0
- package/src/engine/user-tag.ts +24 -0
- package/src/imagegen/aspect.ts +128 -0
- package/src/imagegen/generate.ts +642 -0
- package/src/imagegen/references.ts +75 -0
- package/src/imagegen/types.ts +154 -0
- package/src/lsp/client.ts +183 -0
- package/src/lsp/fake-server.helper.mjs +42 -0
- package/src/lsp/format.ts +29 -0
- package/src/lsp/index.ts +21 -0
- package/src/lsp/manager.ts +238 -0
- package/src/lsp/registry.ts +151 -0
- package/src/mcp/config.ts +117 -0
- package/src/mcp/hub/audit.ts +55 -0
- package/src/mcp/hub/bridge.ts +115 -0
- package/src/mcp/hub/catalog.ts +69 -0
- package/src/mcp/hub/credentials.ts +406 -0
- package/src/mcp/hub/env-expand.ts +14 -0
- package/src/mcp/hub/manager.ts +770 -0
- package/src/mcp/hub/normalize.ts +213 -0
- package/src/mcp/server.ts +368 -0
- package/src/mcp/tool-schema.ts +20 -0
- package/src/media/capabilities.ts +392 -0
- package/src/media/records.ts +175 -0
- package/src/media/store.ts +217 -0
- package/src/media/types.ts +69 -0
- package/src/memory/chunking.ts +168 -0
- package/src/memory/embeddings.ts +262 -0
- package/src/memory/index.ts +15 -0
- package/src/memory/inject.ts +197 -0
- package/src/memory/manager.ts +996 -0
- package/src/memory/registry.ts +176 -0
- package/src/memory/rescan.ts +101 -0
- package/src/memory/retrieval.ts +586 -0
- package/src/memory/shared-index.ts +372 -0
- package/src/memory/slug.ts +39 -0
- package/src/memory/storage.ts +385 -0
- package/src/memory/watcher.ts +134 -0
- package/src/multimodal/blocks.ts +101 -0
- package/src/multimodal/dimensions.ts +214 -0
- package/src/multimodal/load.ts +86 -0
- package/src/multimodal/mime.ts +135 -0
- package/src/multimodal/pdf-render.ts +80 -0
- package/src/multimodal/user-content.ts +266 -0
- package/src/persona/files.ts +144 -0
- package/src/persona/loader.ts +598 -0
- package/src/persona/skill-gating-store.ts +59 -0
- package/src/persona/tool-gating-store.ts +113 -0
- package/src/projects/focus.ts +186 -0
- package/src/projects/prompt-block.ts +106 -0
- package/src/projects/scheme.ts +107 -0
- package/src/projects/store.ts +144 -0
- package/src/projects/types.ts +73 -0
- package/src/sentinel/dispatcher.ts +97 -0
- package/src/sentinel/schedule.ts +271 -0
- package/src/sentinel/scheduler.ts +840 -0
- package/src/sentinel/store.ts +259 -0
- package/src/sentinel/types.ts +221 -0
- package/src/server/activity-stream.ts +160 -0
- package/src/server/ask-calls.ts +137 -0
- package/src/server/ask-wait-graph.ts +171 -0
- package/src/server/async-tasks.ts +199 -0
- package/src/server/builder-busy.ts +86 -0
- package/src/server/builder-prompt.ts +204 -0
- package/src/server/builder-questions.ts +117 -0
- package/src/server/builder-session.ts +249 -0
- package/src/server/chat-aborts.ts +83 -0
- package/src/server/claude-config-dir.ts +71 -0
- package/src/server/claude-credential-sync.ts +454 -0
- package/src/server/env-file.ts +100 -0
- package/src/server/env.ts +115 -0
- package/src/server/heal-session.ts +151 -0
- package/src/server/history-page.ts +46 -0
- package/src/server/index.ts +7244 -0
- package/src/server/lockfile.ts +89 -0
- package/src/server/logger.ts +67 -0
- package/src/server/logs.ts +187 -0
- package/src/server/loopback-fetch.ts +130 -0
- package/src/server/openai-client.ts +89 -0
- package/src/server/prompt-assembly.ts +334 -0
- package/src/server/restart-reconcile.ts +139 -0
- package/src/server/run-turn-fallback.ts +275 -0
- package/src/server/run-turn-types.ts +91 -0
- package/src/server/run-turn.ts +1474 -0
- package/src/server/sanitize-assistant-text.ts +112 -0
- package/src/server/session-export.ts +220 -0
- package/src/server/session-queue.ts +376 -0
- package/src/server/session-workdir.ts +49 -0
- package/src/server/sse-h2-safe.ts +61 -0
- package/src/server/sse-liveness.ts +162 -0
- package/src/server/sse-serializer.ts +161 -0
- package/src/server/start-turn.ts +249 -0
- package/src/server/steer-inbox.ts +107 -0
- package/src/server/tls-keeper.ts +153 -0
- package/src/server/tool-format.ts +167 -0
- package/src/server/turn-framing.ts +52 -0
- package/src/server/turn-origin-kind.ts +74 -0
- package/src/server/turn-origin.ts +40 -0
- package/src/server/work-ledger.ts +975 -0
- package/src/server/workspace.ts +114 -0
- package/src/setup/config-edit.ts +201 -0
- package/src/setup/presets.ts +71 -0
- package/src/setup/prompt.ts +163 -0
- package/src/setup/tailscale.ts +44 -0
- package/src/skills/bin-checks.ts +227 -0
- package/src/skills/bootstrap.ts +347 -0
- package/src/skills/env-scope.ts +175 -0
- package/src/skills/gating.ts +71 -0
- package/src/skills/lint.ts +202 -0
- package/src/skills/load.ts +368 -0
- package/src/skills/path-helpers.ts +102 -0
- package/src/skills/registry.ts +178 -0
- package/src/ssh/exec.ts +142 -0
- package/src/ssh/index.ts +12 -0
- package/src/ssh/known-hosts.ts +109 -0
- package/src/ssh/pool.ts +307 -0
- package/src/storage/sessions.ts +602 -0
- package/src/team/render.ts +149 -0
- package/src/team/resolve.ts +122 -0
- package/src/team/store.ts +124 -0
- package/src/team/types.ts +111 -0
- package/src/team/write.ts +95 -0
- package/src/testing/isolated-home.mts +46 -0
- package/src/tmux/attention-watcher.ts +272 -0
- package/src/tmux/attention.ts +336 -0
- package/src/tmux/list.ts +91 -0
- package/src/tmux/origin-store.ts +102 -0
- package/src/tmux/tui-markers.ts +267 -0
- package/src/tools/agents/ask-cancel.ts +94 -0
- package/src/tools/agents/ask-result.ts +235 -0
- package/src/tools/agents/ask.ts +704 -0
- package/src/tools/agents/builder-dispatch.ts +217 -0
- package/src/tools/agents/images.ts +104 -0
- package/src/tools/agents/index.ts +42 -0
- package/src/tools/agents/long-task-timeouts.ts +39 -0
- package/src/tools/agents/session-list.ts +136 -0
- package/src/tools/agents/session-model.ts +106 -0
- package/src/tools/agents/spawn.ts +845 -0
- package/src/tools/agents/status.ts +589 -0
- package/src/tools/browser/index.ts +6 -0
- package/src/tools/browser/ops.ts +145 -0
- package/src/tools/browser/tools.ts +184 -0
- package/src/tools/builder/short-descriptions.ts +80 -0
- package/src/tools/builder/tools.ts +222 -0
- package/src/tools/docs/index.ts +1 -0
- package/src/tools/docs/tools.ts +228 -0
- package/src/tools/dream/index.ts +11 -0
- package/src/tools/dream/tools.ts +1002 -0
- package/src/tools/exec/allowlist.ts +233 -0
- package/src/tools/exec/background-detach.helper.mts +27 -0
- package/src/tools/exec/blacklist.ts +336 -0
- package/src/tools/exec/concurrency.ts +150 -0
- package/src/tools/exec/fit-output.ts +82 -0
- package/src/tools/exec/index.ts +7 -0
- package/src/tools/exec/internal-env.ts +89 -0
- package/src/tools/exec/job-store.ts +403 -0
- package/src/tools/exec/local.ts +649 -0
- package/src/tools/exec/remote.ts +399 -0
- package/src/tools/exec/tools.ts +934 -0
- package/src/tools/file/analyze.ts +378 -0
- package/src/tools/file/index.ts +2 -0
- package/src/tools/file/local.ts +707 -0
- package/src/tools/file/lsp-hook.ts +72 -0
- package/src/tools/file/persona-guard.ts +61 -0
- package/src/tools/file/policy.ts +292 -0
- package/src/tools/file/read-format.ts +129 -0
- package/src/tools/file/remote.ts +680 -0
- package/src/tools/file/replace.ts +507 -0
- package/src/tools/file/search-window.ts +224 -0
- package/src/tools/file/tools.ts +658 -0
- package/src/tools/file/write-scope.ts +182 -0
- package/src/tools/gating.ts +106 -0
- package/src/tools/image/budget.ts +82 -0
- package/src/tools/image/index.ts +7 -0
- package/src/tools/image/tools.ts +601 -0
- package/src/tools/index.ts +99 -0
- package/src/tools/memory/index.ts +12 -0
- package/src/tools/memory/tools.ts +399 -0
- package/src/tools/projects/index.ts +1 -0
- package/src/tools/projects/tools.ts +643 -0
- package/src/tools/registry.ts +427 -0
- package/src/tools/resources/index.ts +8 -0
- package/src/tools/resources/tools.ts +176 -0
- package/src/tools/resources/visibility.ts +60 -0
- package/src/tools/sentinel/index.ts +1 -0
- package/src/tools/sentinel/tools.ts +592 -0
- package/src/tools/skill/index.ts +4 -0
- package/src/tools/skill/tools.ts +285 -0
- package/src/tools/time/index.ts +5 -0
- package/src/tools/time/tools.ts +123 -0
- package/src/tools/tmux/index.ts +1 -0
- package/src/tools/tmux/local.ts +277 -0
- package/src/tools/tmux/remote.ts +168 -0
- package/src/tools/tmux/tools.ts +1064 -0
- package/src/tools/tool-output.ts +97 -0
- package/src/tools/types.ts +227 -0
- package/src/tools/video/index.ts +1 -0
- package/src/tools/video/tools.ts +425 -0
- package/src/tools/web/fetch.ts +337 -0
- package/src/tools/web/index.ts +4 -0
- package/src/tools/web/tools.ts +186 -0
- package/src/tools/wiki/index.ts +1 -0
- package/src/tools/wiki/tools.ts +445 -0
- package/src/tts/cache-gc.ts +128 -0
- package/src/tts/prepare-for-tts.ts +114 -0
- package/src/tts/service.ts +332 -0
- package/src/types/events.ts +695 -0
- package/src/types/turn-origin.ts +53 -0
- package/src/version.ts +21 -0
- package/src/videogen/dialects.ts +222 -0
- package/src/videogen/generate.ts +484 -0
- package/src/videogen/jobs.ts +178 -0
- package/src/videogen/runner.ts +125 -0
- package/src/videogen/wake.ts +119 -0
- package/src/voice/realtime/call.ts +948 -0
- package/src/voice/realtime/consult.ts +293 -0
- package/src/voice/realtime/fake-provider.ts +134 -0
- package/src/voice/realtime/manager.ts +384 -0
- package/src/voice/realtime/openai-provider.ts +471 -0
- package/src/voice/realtime/persona.ts +227 -0
- package/src/voice/realtime/session-match.ts +116 -0
- package/src/voice/realtime/types.ts +182 -0
- package/src/wiki/conflict.ts +113 -0
- package/src/wiki/explorer.ts +578 -0
- package/src/wiki/index-builder.ts +172 -0
- package/src/wiki/language.ts +134 -0
- package/src/wiki/log-builder.ts +150 -0
- package/src/wiki/map.ts +258 -0
- package/src/wiki/migration/analyze.ts +304 -0
- package/src/wiki/migration/execute.ts +689 -0
- package/src/wiki/migration/refine.ts +402 -0
- package/src/wiki/migration/store.ts +100 -0
- package/src/wiki/move-page.ts +57 -0
- package/src/wiki/structure-file.ts +192 -0
- package/src/wiki/taxonomy.ts +140 -0
- package/src/wiki/templates.ts +163 -0
- package/src/wiki/types.ts +133 -0
- package/templates/skills/README.md +24 -0
- package/templates/skills/_templates/api-wrapper/SKILL.md +26 -0
- package/templates/skills/_templates/cli-wrapper/BOOTSTRAP.md +32 -0
- package/templates/skills/_templates/cli-wrapper/SKILL.md +24 -0
- package/templates/skills/_templates/default/SKILL.md +25 -0
- package/templates/skills/builder-handover/SKILL.md +96 -0
- package/templates/skills/builder-handover/assets/ORDER.md +12 -0
- package/templates/skills/skill-author/SKILL.md +110 -0
- package/tsconfig.json +22 -0
- package/web/dist/assets/index-BaVv6pB2.js +631 -0
- package/web/dist/assets/index-BaVv6pB2.js.map +1 -0
- package/web/dist/assets/index-V3FV3Z25.css +32 -0
- package/web/dist/favicon.svg +8 -0
- package/web/dist/index.html +20 -0
- package/web/package.json +36 -0
- package/web/tsconfig.json +25 -0
- package/web-mobile/dist/apple-touch-icon.png +0 -0
- package/web-mobile/dist/assets/index-BqKt7IMx.css +1 -0
- package/web-mobile/dist/assets/index-X_vRr6cd.js +88 -0
- package/web-mobile/dist/assets/index-X_vRr6cd.js.map +1 -0
- package/web-mobile/dist/favicon.svg +8 -0
- package/web-mobile/dist/icon-192.png +0 -0
- package/web-mobile/dist/icon-512.png +0 -0
- package/web-mobile/dist/index.html +27 -0
- package/web-mobile/dist/manifest.webmanifest +32 -0
- package/web-mobile/dist/service-worker.js +90 -0
- package/web-mobile/package.json +28 -0
- package/web-mobile/tsconfig.json +25 -0
|
@@ -0,0 +1,704 @@
|
|
|
1
|
+
// agent_ask — A2A live messaging (Phase 6c, Modus 2). One agent calls
|
|
2
|
+
// agent_ask({agent: '<other-agent>', message: '...'}); the target agent
|
|
3
|
+
// receives the message in ITS session as a user_message with from_agent
|
|
4
|
+
// set to the caller's name, runs a normal turn (memory auto-inject,
|
|
5
|
+
// tools, persona — all of it), and the reply text comes back inline.
|
|
6
|
+
//
|
|
7
|
+
// Session targeting: an explicit `session` always wins. Without one,
|
|
8
|
+
// the default is context-sensitive (2026-09-06 report — replies to a
|
|
9
|
+
// project session kept landing in main): when the CURRENT turn is
|
|
10
|
+
// itself an A2A turn from agent X (or a sub-agent whose parent is X)
|
|
11
|
+
// and the target is X, the message goes back to the session X wrote
|
|
12
|
+
// from (GET /a2a/turn-origin). Otherwise 'main' — the target agent's
|
|
13
|
+
// canonical "talk to it" entry point, same surface a human types into.
|
|
14
|
+
//
|
|
15
|
+
// Every call carries from_session so the receiver's header reads
|
|
16
|
+
// `[Message from agent hans, session cerebrocraft]` and it can address
|
|
17
|
+
// a follow-up. An unknown session slug returns the target's existing
|
|
18
|
+
// sessions instead of a bare 404.
|
|
19
|
+
//
|
|
20
|
+
// Lock + queue (src/server/session-queue.ts): an agent_ask call queues
|
|
21
|
+
// on the target session like every other turn — first come, first
|
|
22
|
+
// served, no class jumps ahead (Rene, 2026-09-12). The 'agent' label it
|
|
23
|
+
// carries is diagnostics for /health, not an order. Sub-spawns to fresh
|
|
24
|
+
// sessions (spawn_subagent's sub-xxx-yyy) are uncontended; this lock
|
|
25
|
+
// only matters when multiple flows hit the SAME session.
|
|
26
|
+
//
|
|
27
|
+
// Timeout + pending: timeoutFromInput defaults to
|
|
28
|
+
// agentLoop.longTaskDefaultTimeoutMs (5 min) and clamps at
|
|
29
|
+
// longTaskMaxTimeoutMs (30 min). On timeout the tool returns
|
|
30
|
+
// state:'pending' (NOT an error) — the underlying call may still be
|
|
31
|
+
// queued or running on the target side. The outcome is retrievable by
|
|
32
|
+
// call_id through agent_ask_result (src/tools/agents/ask-result.ts,
|
|
33
|
+
// backed by src/server/ask-calls.ts) — never by re-sending the message.
|
|
34
|
+
//
|
|
35
|
+
// Self-call guard: agent_ask({agent: ctx.agent}) would deadlock the
|
|
36
|
+
// caller's own session lock. We refuse early with a clear error
|
|
37
|
+
// pointing at spawn_subagent (sealed self-clone) as the right tool.
|
|
38
|
+
|
|
39
|
+
import { randomUUID } from 'node:crypto';
|
|
40
|
+
import { z } from 'zod';
|
|
41
|
+
import { logger } from '../../server/logger.ts';
|
|
42
|
+
import { classifyFetchError, loopbackFetch } from '../../server/loopback-fetch.ts';
|
|
43
|
+
import {
|
|
44
|
+
IMAGES_FIELD_DESCRIPTION,
|
|
45
|
+
MAX_IMAGES_PER_MESSAGE,
|
|
46
|
+
uploadLocalAttachments,
|
|
47
|
+
} from './images.ts';
|
|
48
|
+
import type { ToolDefinition } from '../types.ts';
|
|
49
|
+
import { longTaskDefaultMs, longTaskMaxMs } from './long-task-timeouts.ts';
|
|
50
|
+
|
|
51
|
+
const TIMEOUT_BUFFER_MS = 2_000;
|
|
52
|
+
|
|
53
|
+
const AskInput = z
|
|
54
|
+
.object({
|
|
55
|
+
agent: z
|
|
56
|
+
.string()
|
|
57
|
+
.min(1)
|
|
58
|
+
.describe(
|
|
59
|
+
'Target agent name (e.g. "<agent-name>", "<other-agent>"). Must NOT be yourself — ' +
|
|
60
|
+
'use spawn_subagent for self-clone tasks.',
|
|
61
|
+
),
|
|
62
|
+
message: z
|
|
63
|
+
.string()
|
|
64
|
+
.min(1)
|
|
65
|
+
.describe(
|
|
66
|
+
'The message to send. Will appear in the target\'s session as a user_message ' +
|
|
67
|
+
'with from_agent=<your name> so the target knows it\'s an A2A turn.',
|
|
68
|
+
),
|
|
69
|
+
session: z
|
|
70
|
+
.string()
|
|
71
|
+
.min(1)
|
|
72
|
+
.optional()
|
|
73
|
+
.describe(
|
|
74
|
+
'Target session slug (or id). Default: if you are answering a message that THIS agent ' +
|
|
75
|
+
'sent you (A2A, or it is your spawning parent), the session they wrote from — ' +
|
|
76
|
+
'otherwise "main". Pass it explicitly to talk to a specific project session; an unknown ' +
|
|
77
|
+
'slug returns the target\'s existing sessions.',
|
|
78
|
+
),
|
|
79
|
+
create_session: z
|
|
80
|
+
.boolean()
|
|
81
|
+
.optional()
|
|
82
|
+
.describe(
|
|
83
|
+
'Create `session` on the target if it does not exist yet (named project slugs only — ' +
|
|
84
|
+
'not main, ids or sub-* sessions). Off by default so a typo stays an error listing the ' +
|
|
85
|
+
'existing sessions. Use it when the user asks for a project session that the target ' +
|
|
86
|
+
'may not have yet.',
|
|
87
|
+
),
|
|
88
|
+
model: z
|
|
89
|
+
.string()
|
|
90
|
+
.min(1)
|
|
91
|
+
.optional()
|
|
92
|
+
.describe(
|
|
93
|
+
'Only with create_session: model (alias or provider/id) pinned on the NEW session, e.g. ' +
|
|
94
|
+
'"fable" or "astra". Ignored with a note when the session already exists — to change ' +
|
|
95
|
+
'the model of an existing session use the session_model tool.',
|
|
96
|
+
),
|
|
97
|
+
images: z
|
|
98
|
+
.array(z.string().min(1))
|
|
99
|
+
.max(MAX_IMAGES_PER_MESSAGE)
|
|
100
|
+
.optional()
|
|
101
|
+
.describe(IMAGES_FIELD_DESCRIPTION),
|
|
102
|
+
wait: z
|
|
103
|
+
.boolean()
|
|
104
|
+
.optional()
|
|
105
|
+
.describe(
|
|
106
|
+
'Default true: wait for the reply (up to timeout_ms). false: hand the message over and ' +
|
|
107
|
+
'return at once with state:"pending" and a call_id — you are woken with an ' +
|
|
108
|
+
'[agent answer] turn when the reply lands, or read it with agent_ask_result. Use ' +
|
|
109
|
+
'false for work that may take minutes; the old trick of a 1-second timeout_ms is ' +
|
|
110
|
+
'no longer needed.',
|
|
111
|
+
),
|
|
112
|
+
timeout_ms: z
|
|
113
|
+
.number()
|
|
114
|
+
.int()
|
|
115
|
+
.min(1_000)
|
|
116
|
+
.max(7_200_000) // 2h hard schema cap; config caps further
|
|
117
|
+
.optional()
|
|
118
|
+
.describe(
|
|
119
|
+
'Max ms to wait for the target\'s reply. Defaults to ' +
|
|
120
|
+
'agentLoop.longTaskDefaultTimeoutMs (5 min); clamped at ' +
|
|
121
|
+
'longTaskMaxTimeoutMs (30 min). On timeout the tool returns ' +
|
|
122
|
+
'state:"pending" — the call may still complete; the reply ' +
|
|
123
|
+
'lands in the target\'s session either way.',
|
|
124
|
+
),
|
|
125
|
+
})
|
|
126
|
+
.strict();
|
|
127
|
+
|
|
128
|
+
interface AskDoneResult {
|
|
129
|
+
ok: true;
|
|
130
|
+
state: 'done';
|
|
131
|
+
call_id: string;
|
|
132
|
+
target_agent: string;
|
|
133
|
+
target_session: string;
|
|
134
|
+
/** True when `session` was omitted and the reply-back default picked
|
|
135
|
+
* the asker's source session instead of main. */
|
|
136
|
+
session_inferred?: boolean;
|
|
137
|
+
/** Why the message went to `target_session`: named by the caller,
|
|
138
|
+
* sent back to the session that started this turn, or left out and
|
|
139
|
+
* therefore `main`. */
|
|
140
|
+
routing_reason?: RoutingReason;
|
|
141
|
+
/** Only with `default_main` from a non-main session: says that the
|
|
142
|
+
* message went to `main` and how to address a specific session. */
|
|
143
|
+
routing_note?: string;
|
|
144
|
+
/** True when this call created the target session (create_session). */
|
|
145
|
+
session_created?: boolean;
|
|
146
|
+
/** provider/modelId pinned on a session this call created. */
|
|
147
|
+
session_model?: string;
|
|
148
|
+
/** e.g. "session already existed — model ignored". */
|
|
149
|
+
session_note?: string;
|
|
150
|
+
response: string;
|
|
151
|
+
ms: number;
|
|
152
|
+
usage?: {
|
|
153
|
+
tokens_in: number;
|
|
154
|
+
tokens_out: number;
|
|
155
|
+
tokens_in_cached?: number;
|
|
156
|
+
tokens_out_reasoning?: number;
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export type RoutingReason = 'explicit' | 'reply_to_origin' | 'default_main';
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Say where the message went and why. A left-out `session` means
|
|
164
|
+
* `main` unless this turn was started by the target (reply-back). That
|
|
165
|
+
* default is silent, and it is wrong whenever the caller is doing
|
|
166
|
+
* project work in a session of its own: turns started by tmux,
|
|
167
|
+
* sentinel, a wake or a person have no origin to reply to, so the
|
|
168
|
+
* report lands in the target's `main`, which has none of the context
|
|
169
|
+
* (2026-09-14: a project report answered by `main` with instructions
|
|
170
|
+
* that did not fit the project). The message is still delivered — the
|
|
171
|
+
* caller is told, so it can re-address instead of finding out later.
|
|
172
|
+
* Exported for tests.
|
|
173
|
+
*/
|
|
174
|
+
export function describeRouting(args: {
|
|
175
|
+
explicit: boolean;
|
|
176
|
+
inferred: boolean;
|
|
177
|
+
callerSession: string | undefined;
|
|
178
|
+
targetAgent: string;
|
|
179
|
+
}): { routing_reason: RoutingReason; routing_note?: string } {
|
|
180
|
+
if (args.explicit) return { routing_reason: 'explicit' };
|
|
181
|
+
if (args.inferred) return { routing_reason: 'reply_to_origin' };
|
|
182
|
+
if (args.callerSession && args.callerSession !== 'main') {
|
|
183
|
+
return {
|
|
184
|
+
routing_reason: 'default_main',
|
|
185
|
+
routing_note:
|
|
186
|
+
`No session was named, so this went to ${args.targetAgent}'s 'main' — you are working in ` +
|
|
187
|
+
`'${args.callerSession}', and 'main' may not know that context. If this belongs to a specific ` +
|
|
188
|
+
`conversation of ${args.targetAgent} (a project session, the session that briefed you), pass ` +
|
|
189
|
+
`session: "<its slug>" next time; an unknown slug returns the list of existing ones.`,
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
return { routing_reason: 'default_main' };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
interface AskPendingResult {
|
|
196
|
+
ok: false;
|
|
197
|
+
state: 'pending';
|
|
198
|
+
call_id: string;
|
|
199
|
+
target_agent: string;
|
|
200
|
+
target_session: string;
|
|
201
|
+
session_inferred?: boolean;
|
|
202
|
+
/** Why the message went to `target_session`: named by the caller,
|
|
203
|
+
* sent back to the session that started this turn, or left out and
|
|
204
|
+
* therefore `main`. */
|
|
205
|
+
routing_reason?: RoutingReason;
|
|
206
|
+
/** Only with `default_main` from a non-main session: says that the
|
|
207
|
+
* message went to `main` and how to address a specific session. */
|
|
208
|
+
routing_note?: string;
|
|
209
|
+
session_created?: boolean;
|
|
210
|
+
session_model?: string;
|
|
211
|
+
session_note?: string;
|
|
212
|
+
hint: string;
|
|
213
|
+
ms: number;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** The target's turn ran and failed — an engine error, or the person
|
|
217
|
+
* stopped it. Until 2026-09-13 this came back as `done` with an empty
|
|
218
|
+
* response, which read as "the target had nothing to say" (birdseye L3). */
|
|
219
|
+
interface AskFailedResult {
|
|
220
|
+
ok: false;
|
|
221
|
+
state: 'failed';
|
|
222
|
+
call_id: string;
|
|
223
|
+
target_agent: string;
|
|
224
|
+
target_session: string;
|
|
225
|
+
session_inferred?: boolean;
|
|
226
|
+
/** Why the message went to `target_session`: named by the caller,
|
|
227
|
+
* sent back to the session that started this turn, or left out and
|
|
228
|
+
* therefore `main`. */
|
|
229
|
+
routing_reason?: RoutingReason;
|
|
230
|
+
/** Only with `default_main` from a non-main session: says that the
|
|
231
|
+
* message went to `main` and how to address a specific session. */
|
|
232
|
+
routing_note?: string;
|
|
233
|
+
error: string;
|
|
234
|
+
hint: string;
|
|
235
|
+
ms: number;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
type AskResult = AskDoneResult | AskPendingResult | AskFailedResult;
|
|
239
|
+
|
|
240
|
+
export const agentAsk: ToolDefinition<z.infer<typeof AskInput>, AskResult> = {
|
|
241
|
+
name: 'agent_ask',
|
|
242
|
+
toolset: 'agents',
|
|
243
|
+
description:
|
|
244
|
+
'Send a live message to another somora agent and wait for their reply. Unlike spawn_subagent ' +
|
|
245
|
+
'(which runs a sealed task in a fresh session), agent_ask writes into the target\'s actual ' +
|
|
246
|
+
'session — so the target uses their full memory, persona, and conversation history to answer, ' +
|
|
247
|
+
'and the exchange is visible in their session afterwards. Use this when you need the TARGET\'s ' +
|
|
248
|
+
'expertise/state ("<agent>, what did the user say last week about X?"), NOT when you have a ' +
|
|
249
|
+
'self-contained task you could delegate (use spawn_subagent for that). ' +
|
|
250
|
+
'Session: when you are replying to an agent that wrote to you (their message carries ' +
|
|
251
|
+
'"[Message from agent X, session Y]") and omit `session`, the message goes back to that ' +
|
|
252
|
+
'session Y; otherwise to the target\'s main session. Pass `session` explicitly for a specific ' +
|
|
253
|
+
'project session; add create_session:true (optionally with model:"<alias>") when the user ' +
|
|
254
|
+
'wants the target in a project session it may not have yet — the session is created, the ' +
|
|
255
|
+
'model pinned on it, and the target told. Two ways to wait: wait:true (default) blocks up ' +
|
|
256
|
+
'to timeout_ms (5 min default, 30 min cap — slow local models routinely need minutes) and ' +
|
|
257
|
+
'returns state:"pending" (NOT an error) when time runs out; wait:false hands the message ' +
|
|
258
|
+
'over and returns pending at once. Either way a pending call still completes on the ' +
|
|
259
|
+
'target — you are woken with an [agent answer] turn when it does, or fetch it with ' +
|
|
260
|
+
'agent_ask_result(call_id); never re-send the message. ' +
|
|
261
|
+
'IMPORTANT: cannot ask yourself — use spawn_subagent for self-clone tasks. ' +
|
|
262
|
+
'Calls queue on the target session first come, first served, like typed turns. ' +
|
|
263
|
+
'state:"failed" carries the reason: a model or engine error on the target, or ' +
|
|
264
|
+
'"stopped by the user" — a person ended that turn; do not re-send it. ' +
|
|
265
|
+
'agent_ask is REQUEST-RESPONSE, not a message bus: if YOU received a question via agent_ask ' +
|
|
266
|
+
'(a user_message with from_agent set), your answer is your normal turn output — the asking ' +
|
|
267
|
+
'agent receives it automatically as the result of their pending call. Never agent_ask your ' +
|
|
268
|
+
'caller back to deliver an answer; circular calls are rejected (deadlock guard). ' +
|
|
269
|
+
'If you answer "I am working on it with sub-agents and will report back", you do not have to: ' +
|
|
270
|
+
'when work you started while answering finishes later, somora sends its result to the asker ' +
|
|
271
|
+
'on its own as a follow-up message from you. Likewise, a target that told YOU it would report ' +
|
|
272
|
+
'back does so through such a follow-up turn — do not re-ask.',
|
|
273
|
+
inputSchema: AskInput,
|
|
274
|
+
jsonSchema: {
|
|
275
|
+
type: 'object',
|
|
276
|
+
properties: {
|
|
277
|
+
agent: { type: 'string', description: 'Target agent name.' },
|
|
278
|
+
message: { type: 'string', description: 'Message text.' },
|
|
279
|
+
session: {
|
|
280
|
+
type: 'string',
|
|
281
|
+
description:
|
|
282
|
+
'Target session slug or id. Default: the session your asker wrote from when the target ' +
|
|
283
|
+
'is that asker (A2A reply-back / your spawning parent), otherwise "main".',
|
|
284
|
+
},
|
|
285
|
+
create_session: {
|
|
286
|
+
type: 'boolean',
|
|
287
|
+
description:
|
|
288
|
+
'Create `session` on the target if missing (named project slugs only). Default false — ' +
|
|
289
|
+
'an unknown slug is then an error listing the existing sessions.',
|
|
290
|
+
},
|
|
291
|
+
model: {
|
|
292
|
+
type: 'string',
|
|
293
|
+
description:
|
|
294
|
+
'Only with create_session: model alias or provider/id pinned on the NEW session. Ignored ' +
|
|
295
|
+
'with a note when the session already exists.',
|
|
296
|
+
},
|
|
297
|
+
images: {
|
|
298
|
+
type: 'array',
|
|
299
|
+
items: { type: 'string' },
|
|
300
|
+
maxItems: MAX_IMAGES_PER_MESSAGE,
|
|
301
|
+
description: IMAGES_FIELD_DESCRIPTION,
|
|
302
|
+
},
|
|
303
|
+
timeout_ms: {
|
|
304
|
+
type: 'integer',
|
|
305
|
+
minimum: 1000,
|
|
306
|
+
description:
|
|
307
|
+
'Max wait in ms. Default agentLoop.longTaskDefaultTimeoutMs (5 min); ' +
|
|
308
|
+
'capped at longTaskMaxTimeoutMs (30 min).',
|
|
309
|
+
},
|
|
310
|
+
wait: {
|
|
311
|
+
type: 'boolean',
|
|
312
|
+
description:
|
|
313
|
+
'Default true: wait for the reply. false: hand over and return pending at once; ' +
|
|
314
|
+
'you are woken when the reply lands, or read it with agent_ask_result.',
|
|
315
|
+
},
|
|
316
|
+
},
|
|
317
|
+
required: ['agent', 'message'],
|
|
318
|
+
additionalProperties: false,
|
|
319
|
+
},
|
|
320
|
+
// Engine race must accommodate the inner wait + 2s buffer (OpenClaw
|
|
321
|
+
// pattern). Read at call time so config changes apply without restart.
|
|
322
|
+
defaultTimeoutMs: 30_000,
|
|
323
|
+
timeoutFromInput: (input) => {
|
|
324
|
+
const requested = input.timeout_ms ?? longTaskDefaultMs();
|
|
325
|
+
const clamped = Math.min(requested, longTaskMaxMs());
|
|
326
|
+
return clamped + TIMEOUT_BUFFER_MS;
|
|
327
|
+
},
|
|
328
|
+
// Static safety fence; actual cap is dynamic via timeoutFromInput.
|
|
329
|
+
maxTimeoutMs: 7_200_000 + TIMEOUT_BUFFER_MS,
|
|
330
|
+
async handler(input, ctx): Promise<AskResult> {
|
|
331
|
+
const targetAgent = input.agent;
|
|
332
|
+
|
|
333
|
+
if (targetAgent === ctx.agent) {
|
|
334
|
+
throw new Error(
|
|
335
|
+
`agent_ask: can't ask yourself ('${ctx.agent}'). For a self-clone task in a fresh ` +
|
|
336
|
+
`session, use spawn_subagent({ task: '...' }) (omit persona).`,
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
if (input.model && !input.create_session) {
|
|
341
|
+
throw new Error(
|
|
342
|
+
`agent_ask: 'model' only applies together with create_session:true (it pins the model on a ` +
|
|
343
|
+
`session this call creates). To change the model of an existing session, use ` +
|
|
344
|
+
`session_model({ agent: "${targetAgent}", session: "<slug>", model: "${input.model}" }).`,
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
if (input.create_session && !input.session) {
|
|
348
|
+
throw new Error(`agent_ask: create_session:true needs an explicit 'session' slug to create.`);
|
|
349
|
+
}
|
|
350
|
+
const callId = randomUUID();
|
|
351
|
+
const timeoutMs = Math.min(input.timeout_ms ?? longTaskDefaultMs(), longTaskMaxMs());
|
|
352
|
+
|
|
353
|
+
const host = process.env.SOMORA_HOST || '127.0.0.1';
|
|
354
|
+
const port = process.env.SOMORA_PORT || '18737';
|
|
355
|
+
// SOMORA_TLS=1 → server is HTTP/2-over-TLS; A2A loopback must use
|
|
356
|
+
// https://<publicHost> (see spawn.ts:runChatTurnViaHttp).
|
|
357
|
+
const scheme = process.env.SOMORA_TLS === '1' ? 'https' : 'http';
|
|
358
|
+
const base = `${scheme}://${host}:${port}`;
|
|
359
|
+
|
|
360
|
+
// Reply-back default: no explicit session + this turn was started
|
|
361
|
+
// by the target (A2A inbound or spawning parent) → their session.
|
|
362
|
+
let targetSession = input.session ?? 'main';
|
|
363
|
+
let sessionInferred = false;
|
|
364
|
+
if (!input.session && ctx.session) {
|
|
365
|
+
const origin = await lookupTurnOrigin(base, ctx.agent, ctx.session);
|
|
366
|
+
if (origin && origin.agent === targetAgent && origin.session) {
|
|
367
|
+
targetSession = origin.session;
|
|
368
|
+
sessionInferred = true;
|
|
369
|
+
logger.info({
|
|
370
|
+
msg: 'agent_ask.session_inferred',
|
|
371
|
+
from: ctx.agent,
|
|
372
|
+
from_session: ctx.session,
|
|
373
|
+
to: targetAgent,
|
|
374
|
+
session: targetSession,
|
|
375
|
+
origin_kind: origin.kind,
|
|
376
|
+
call_id: callId,
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
const routing = describeRouting({
|
|
382
|
+
explicit: input.session !== undefined,
|
|
383
|
+
inferred: sessionInferred,
|
|
384
|
+
callerSession: ctx.session,
|
|
385
|
+
targetAgent,
|
|
386
|
+
});
|
|
387
|
+
if (routing.routing_note) {
|
|
388
|
+
logger.warn({
|
|
389
|
+
msg: 'agent_ask.default_main_from_work_session',
|
|
390
|
+
from: ctx.agent,
|
|
391
|
+
from_session: ctx.session,
|
|
392
|
+
to: targetAgent,
|
|
393
|
+
call_id: callId,
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
logger.info({
|
|
398
|
+
msg: 'agent_ask.start',
|
|
399
|
+
from: ctx.agent,
|
|
400
|
+
to: targetAgent,
|
|
401
|
+
target_session: targetSession,
|
|
402
|
+
call_id: callId,
|
|
403
|
+
timeout_ms: timeoutMs,
|
|
404
|
+
message_len: input.message.length,
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
const start = Date.now();
|
|
408
|
+
|
|
409
|
+
// AbortController hard-bounds the wait. If the timer fires before
|
|
410
|
+
// the response comes back, the fetch errors with AbortError and we
|
|
411
|
+
// return state:'pending'. The server-side call may still be queued
|
|
412
|
+
// (in which case it'll eventually run and write to the target's
|
|
413
|
+
// JSONL anyway) or in-flight (which we don't cancel — letting the
|
|
414
|
+
// turn finish keeps the conversation intact in the target's session
|
|
415
|
+
// even if the caller stopped listening).
|
|
416
|
+
// Upload before the wait starts: a bad path should fail fast and
|
|
417
|
+
// loudly, not eat into the target's answer time.
|
|
418
|
+
const attachments =
|
|
419
|
+
input.images && input.images.length > 0
|
|
420
|
+
? await uploadLocalAttachments(input.images, base)
|
|
421
|
+
: [];
|
|
422
|
+
if (attachments.length > 0) {
|
|
423
|
+
logger.info({
|
|
424
|
+
msg: 'agent_ask.attachments',
|
|
425
|
+
from: ctx.agent,
|
|
426
|
+
to: targetAgent,
|
|
427
|
+
call_id: callId,
|
|
428
|
+
files: attachments.map((a) => `${a.name} (${a.kind}, ${a.size}B)`),
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
const detach = input.wait === false;
|
|
433
|
+
const ac = new AbortController();
|
|
434
|
+
// A detached call answers in milliseconds; the timer only guards the
|
|
435
|
+
// hand-over itself.
|
|
436
|
+
const timer = setTimeout(() => ac.abort(), detach ? 30_000 : timeoutMs);
|
|
437
|
+
|
|
438
|
+
try {
|
|
439
|
+
const res = await loopbackFetch(`${base}/chat/send-sync`, {
|
|
440
|
+
method: 'POST',
|
|
441
|
+
headers: { 'Content-Type': 'application/json' },
|
|
442
|
+
body: JSON.stringify({
|
|
443
|
+
agent: targetAgent,
|
|
444
|
+
session: targetSession,
|
|
445
|
+
text: input.message,
|
|
446
|
+
...(attachments.length > 0 ? { attachments } : {}),
|
|
447
|
+
from_agent: ctx.agent,
|
|
448
|
+
// from_session lets the target address a follow-up to the
|
|
449
|
+
// session this question came from (header + reply-back default).
|
|
450
|
+
...(ctx.session ? { from_session: ctx.session } : {}),
|
|
451
|
+
...(input.create_session ? { create_session: true } : {}),
|
|
452
|
+
...(input.create_session && input.model ? { create_model: input.model } : {}),
|
|
453
|
+
// waiter_* register this turn in the server's A2A wait-graph
|
|
454
|
+
// (circular-wait detection, src/server/ask-wait-graph.ts).
|
|
455
|
+
// Missing ctx.session (shouldn't happen — MCP children get
|
|
456
|
+
// SOMORA_SESSION per turn) just degrades to no detection for
|
|
457
|
+
// this call.
|
|
458
|
+
...(ctx.session && !detach ? { waiter_agent: ctx.agent, waiter_session: ctx.session } : {}),
|
|
459
|
+
agent_ask_call_id: callId,
|
|
460
|
+
...(detach ? { detach: true } : {}),
|
|
461
|
+
}),
|
|
462
|
+
signal: ac.signal,
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
if (detach && res.status === 202) {
|
|
466
|
+
const handed = (await res.json()) as { call_id: string; session_id?: string; session_created?: boolean; session_model?: string; session_note?: string };
|
|
467
|
+
logger.info({ msg: 'agent_ask.handed_over', from: ctx.agent, to: targetAgent, call_id: callId, session: handed.session_id ?? targetSession });
|
|
468
|
+
return {
|
|
469
|
+
ok: false,
|
|
470
|
+
state: 'pending',
|
|
471
|
+
call_id: callId,
|
|
472
|
+
target_agent: targetAgent,
|
|
473
|
+
target_session: handed.session_id ?? targetSession,
|
|
474
|
+
...(sessionInferred ? { session_inferred: true } : {}),
|
|
475
|
+
...routing,
|
|
476
|
+
...(handed.session_created ? { session_created: true } : {}),
|
|
477
|
+
...(handed.session_model ? { session_model: handed.session_model } : {}),
|
|
478
|
+
...(handed.session_note ? { session_note: handed.session_note } : {}),
|
|
479
|
+
hint:
|
|
480
|
+
`Handed over to ${targetAgent}/${handed.session_id ?? targetSession}. You will be woken with an ` +
|
|
481
|
+
`[agent answer] turn when the reply lands; agent_ask_result({ call_id: "${callId}" }) reads it ` +
|
|
482
|
+
'any time. Do NOT re-send the message.',
|
|
483
|
+
ms: Date.now() - start,
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
if (!res.ok) {
|
|
488
|
+
const body = await res.text().catch(() => '');
|
|
489
|
+
// 409 circular_wait — the target's turn is (directly or via a
|
|
490
|
+
// chain) already blocked waiting on THIS turn. Teach the model
|
|
491
|
+
// the request-response pattern instead of surfacing a raw HTTP
|
|
492
|
+
// error; this is the self-healing path for the Gideon↔Donna
|
|
493
|
+
// deadlock (2026-06-01 feedback).
|
|
494
|
+
if (res.status === 409 && body.includes('"circular_wait":true')) {
|
|
495
|
+
let chain = '';
|
|
496
|
+
try {
|
|
497
|
+
const parsed = JSON.parse(body) as { chain?: string[] };
|
|
498
|
+
if (Array.isArray(parsed.chain)) chain = parsed.chain.join(' → ');
|
|
499
|
+
} catch {
|
|
500
|
+
/* keep chain empty */
|
|
501
|
+
}
|
|
502
|
+
throw new Error(
|
|
503
|
+
`agent_ask: rejected — ${targetAgent} is already blocked waiting on YOUR current ` +
|
|
504
|
+
`turn` +
|
|
505
|
+
(chain ? ` (wait chain: ${chain})` : '') +
|
|
506
|
+
`. Your turn's final output is exactly what unblocks that wait: an agent_ask ` +
|
|
507
|
+
`caller receives it as their tool result, a spawning parent receives it as the ` +
|
|
508
|
+
`sub's result. Do NOT open a new agent_ask toward an agent that is waiting on ` +
|
|
509
|
+
`you; just finish your current reply with the answer.`,
|
|
510
|
+
);
|
|
511
|
+
}
|
|
512
|
+
if (res.status === 400 && body.includes('"known_models"')) {
|
|
513
|
+
let known: string[] = [];
|
|
514
|
+
try {
|
|
515
|
+
const parsed = JSON.parse(body) as { known_models?: string[] };
|
|
516
|
+
if (Array.isArray(parsed.known_models)) known = parsed.known_models;
|
|
517
|
+
} catch {
|
|
518
|
+
/* keep known empty */
|
|
519
|
+
}
|
|
520
|
+
throw new Error(
|
|
521
|
+
`agent_ask: model '${input.model}' is not configured — session '${targetSession}' was NOT ` +
|
|
522
|
+
`created. Known models: ${known.join(', ')}. Fix the alias and call again.`,
|
|
523
|
+
);
|
|
524
|
+
}
|
|
525
|
+
if (res.status === 404 && body.includes('"known_sessions"')) {
|
|
526
|
+
let known: string[] = [];
|
|
527
|
+
try {
|
|
528
|
+
const parsed = JSON.parse(body) as { known_sessions?: string[] };
|
|
529
|
+
if (Array.isArray(parsed.known_sessions)) known = parsed.known_sessions;
|
|
530
|
+
} catch {
|
|
531
|
+
/* keep known empty */
|
|
532
|
+
}
|
|
533
|
+
throw new Error(
|
|
534
|
+
`agent_ask: session '${targetSession}' does not exist for agent ${targetAgent}. ` +
|
|
535
|
+
(known.length > 0
|
|
536
|
+
? `Existing sessions: ${known.join(', ')}. Pick one of these (or omit session ` +
|
|
537
|
+
`for the default) — do NOT fall back to main for project work.`
|
|
538
|
+
: `That agent has no sessions yet; omit session to use main.`),
|
|
539
|
+
);
|
|
540
|
+
}
|
|
541
|
+
throw new Error(
|
|
542
|
+
`agent_ask: target ${targetAgent}/${targetSession} returned HTTP ${res.status}: ${body.slice(0, 300)}`,
|
|
543
|
+
);
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
const data = (await res.json()) as {
|
|
547
|
+
finalText?: string;
|
|
548
|
+
usage?: AskDoneResult['usage'];
|
|
549
|
+
session_id?: string;
|
|
550
|
+
session_created?: boolean;
|
|
551
|
+
session_model?: string;
|
|
552
|
+
session_note?: string;
|
|
553
|
+
outcome?: string;
|
|
554
|
+
error?: string;
|
|
555
|
+
};
|
|
556
|
+
if (data.error) {
|
|
557
|
+
logger.info({
|
|
558
|
+
msg: 'agent_ask.failed',
|
|
559
|
+
from: ctx.agent,
|
|
560
|
+
to: targetAgent,
|
|
561
|
+
call_id: callId,
|
|
562
|
+
ms: Date.now() - start,
|
|
563
|
+
err: data.error,
|
|
564
|
+
});
|
|
565
|
+
return {
|
|
566
|
+
ok: false,
|
|
567
|
+
state: 'failed',
|
|
568
|
+
call_id: callId,
|
|
569
|
+
target_agent: targetAgent,
|
|
570
|
+
target_session: targetSession,
|
|
571
|
+
...(sessionInferred ? { session_inferred: true } : {}),
|
|
572
|
+
...routing,
|
|
573
|
+
error: data.error,
|
|
574
|
+
hint:
|
|
575
|
+
`${targetAgent}'s turn ran and failed: ${data.error}. ` +
|
|
576
|
+
'If a person stopped it, they had a reason — tell your user, do not re-send. ' +
|
|
577
|
+
'For a model or engine error, agent_ask_result({ call_id }) shows the same failure; ' +
|
|
578
|
+
're-send only if the user wants a retry.',
|
|
579
|
+
ms: Date.now() - start,
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
if (data.session_created) {
|
|
583
|
+
logger.info({
|
|
584
|
+
msg: 'agent_ask.session_created',
|
|
585
|
+
from: ctx.agent,
|
|
586
|
+
to: targetAgent,
|
|
587
|
+
session: data.session_id ?? targetSession,
|
|
588
|
+
model: data.session_model ?? null,
|
|
589
|
+
call_id: callId,
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
logger.info({
|
|
594
|
+
msg: 'agent_ask.done',
|
|
595
|
+
from: ctx.agent,
|
|
596
|
+
to: targetAgent,
|
|
597
|
+
call_id: callId,
|
|
598
|
+
ms: Date.now() - start,
|
|
599
|
+
reply_len: data.finalText?.length ?? 0,
|
|
600
|
+
});
|
|
601
|
+
|
|
602
|
+
return {
|
|
603
|
+
ok: true,
|
|
604
|
+
state: 'done',
|
|
605
|
+
call_id: callId,
|
|
606
|
+
target_agent: targetAgent,
|
|
607
|
+
target_session: targetSession,
|
|
608
|
+
...(sessionInferred ? { session_inferred: true } : {}),
|
|
609
|
+
...routing,
|
|
610
|
+
...(data.session_created ? { session_created: true } : {}),
|
|
611
|
+
...(data.session_model ? { session_model: data.session_model } : {}),
|
|
612
|
+
...(data.session_note ? { session_note: data.session_note } : {}),
|
|
613
|
+
response: data.finalText ?? '',
|
|
614
|
+
ms: Date.now() - start,
|
|
615
|
+
...(data.usage ? { usage: data.usage } : {}),
|
|
616
|
+
};
|
|
617
|
+
} catch (err) {
|
|
618
|
+
// Our own crafted errors (HTTP-status branch above, incl. the
|
|
619
|
+
// circular-wait teaching message) are already actionable — pass
|
|
620
|
+
// them through instead of re-wrapping via classifyFetchError.
|
|
621
|
+
if (err instanceof Error && err.message.startsWith('agent_ask:')) {
|
|
622
|
+
throw err;
|
|
623
|
+
}
|
|
624
|
+
// AbortError when our timer fired — translate to pending. Real
|
|
625
|
+
// network errors (target unreachable, malformed response) bubble,
|
|
626
|
+
// but we classify them first so the agent gets actionable info
|
|
627
|
+
// instead of the bare `fetch failed` undici emits.
|
|
628
|
+
if (ac.signal.aborted) {
|
|
629
|
+
logger.info({
|
|
630
|
+
msg: 'agent_ask.pending',
|
|
631
|
+
from: ctx.agent,
|
|
632
|
+
to: targetAgent,
|
|
633
|
+
call_id: callId,
|
|
634
|
+
waited_ms: Date.now() - start,
|
|
635
|
+
});
|
|
636
|
+
return {
|
|
637
|
+
ok: false,
|
|
638
|
+
state: 'pending',
|
|
639
|
+
call_id: callId,
|
|
640
|
+
target_agent: targetAgent,
|
|
641
|
+
target_session: targetSession,
|
|
642
|
+
...(sessionInferred ? { session_inferred: true } : {}),
|
|
643
|
+
...routing,
|
|
644
|
+
hint:
|
|
645
|
+
`${targetAgent} did not reply within timeout_ms (${timeoutMs}ms). ` +
|
|
646
|
+
`The call is still queued or running on ${targetAgent}/${targetSession} — do NOT ` +
|
|
647
|
+
`re-send the message (it would run the work twice). Fetch the outcome with ` +
|
|
648
|
+
`agent_ask_result({ call_id: "${callId}" }) — add wait_until_done:true to block ` +
|
|
649
|
+
`until it finishes.`,
|
|
650
|
+
ms: Date.now() - start,
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
const classified = classifyFetchError(err);
|
|
654
|
+
logger.warn({
|
|
655
|
+
msg: 'agent_ask.fetch_error',
|
|
656
|
+
from: ctx.agent,
|
|
657
|
+
to: targetAgent,
|
|
658
|
+
call_id: callId,
|
|
659
|
+
waited_ms: Date.now() - start,
|
|
660
|
+
category: classified.category,
|
|
661
|
+
code: classified.code,
|
|
662
|
+
err: classified.message,
|
|
663
|
+
});
|
|
664
|
+
throw new Error(
|
|
665
|
+
`agent_ask [${classified.category}${classified.code ? '/' + classified.code : ''}]: ` +
|
|
666
|
+
`${classified.message}` +
|
|
667
|
+
(classified.hint ? ` — hint: ${classified.hint}` : ''),
|
|
668
|
+
);
|
|
669
|
+
} finally {
|
|
670
|
+
clearTimeout(timer);
|
|
671
|
+
}
|
|
672
|
+
},
|
|
673
|
+
};
|
|
674
|
+
|
|
675
|
+
interface TurnOriginInfo {
|
|
676
|
+
agent: string;
|
|
677
|
+
session: string;
|
|
678
|
+
kind: 'a2a' | 'subagent';
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/** Who started the turn `agent/session` is running right now — the
|
|
682
|
+
* A2A asker, or the spawning parent for a sub-session. Null for a
|
|
683
|
+
* plain human/system turn or when the lookup fails (never blocks a
|
|
684
|
+
* call; the default then stays main). */
|
|
685
|
+
async function lookupTurnOrigin(
|
|
686
|
+
base: string,
|
|
687
|
+
agent: string,
|
|
688
|
+
session: string,
|
|
689
|
+
): Promise<TurnOriginInfo | null> {
|
|
690
|
+
try {
|
|
691
|
+
const res = await loopbackFetch(
|
|
692
|
+
`${base}/a2a/turn-origin/${encodeURIComponent(agent)}/${encodeURIComponent(session)}`,
|
|
693
|
+
{ signal: AbortSignal.timeout(3_000) },
|
|
694
|
+
);
|
|
695
|
+
if (!res.ok) return null;
|
|
696
|
+
const data = (await res.json()) as { origin?: TurnOriginInfo | null };
|
|
697
|
+
return data.origin && typeof data.origin.agent === 'string' && typeof data.origin.session === 'string'
|
|
698
|
+
? data.origin
|
|
699
|
+
: null;
|
|
700
|
+
} catch (err) {
|
|
701
|
+
logger.warn({ msg: 'agent_ask.turn_origin_lookup_failed', agent, session, err: String(err) });
|
|
702
|
+
return null;
|
|
703
|
+
}
|
|
704
|
+
}
|