gobby 0.2.5__py3-none-any.whl
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.
- gobby/__init__.py +3 -0
- gobby/adapters/__init__.py +30 -0
- gobby/adapters/base.py +93 -0
- gobby/adapters/claude_code.py +276 -0
- gobby/adapters/codex.py +1292 -0
- gobby/adapters/gemini.py +343 -0
- gobby/agents/__init__.py +37 -0
- gobby/agents/codex_session.py +120 -0
- gobby/agents/constants.py +112 -0
- gobby/agents/context.py +362 -0
- gobby/agents/definitions.py +133 -0
- gobby/agents/gemini_session.py +111 -0
- gobby/agents/registry.py +618 -0
- gobby/agents/runner.py +968 -0
- gobby/agents/session.py +259 -0
- gobby/agents/spawn.py +916 -0
- gobby/agents/spawners/__init__.py +77 -0
- gobby/agents/spawners/base.py +142 -0
- gobby/agents/spawners/cross_platform.py +266 -0
- gobby/agents/spawners/embedded.py +225 -0
- gobby/agents/spawners/headless.py +226 -0
- gobby/agents/spawners/linux.py +125 -0
- gobby/agents/spawners/macos.py +277 -0
- gobby/agents/spawners/windows.py +308 -0
- gobby/agents/tty_config.py +319 -0
- gobby/autonomous/__init__.py +32 -0
- gobby/autonomous/progress_tracker.py +447 -0
- gobby/autonomous/stop_registry.py +269 -0
- gobby/autonomous/stuck_detector.py +383 -0
- gobby/cli/__init__.py +67 -0
- gobby/cli/__main__.py +8 -0
- gobby/cli/agents.py +529 -0
- gobby/cli/artifacts.py +266 -0
- gobby/cli/daemon.py +329 -0
- gobby/cli/extensions.py +526 -0
- gobby/cli/github.py +263 -0
- gobby/cli/init.py +53 -0
- gobby/cli/install.py +614 -0
- gobby/cli/installers/__init__.py +37 -0
- gobby/cli/installers/antigravity.py +65 -0
- gobby/cli/installers/claude.py +363 -0
- gobby/cli/installers/codex.py +192 -0
- gobby/cli/installers/gemini.py +294 -0
- gobby/cli/installers/git_hooks.py +377 -0
- gobby/cli/installers/shared.py +737 -0
- gobby/cli/linear.py +250 -0
- gobby/cli/mcp.py +30 -0
- gobby/cli/mcp_proxy.py +698 -0
- gobby/cli/memory.py +304 -0
- gobby/cli/merge.py +384 -0
- gobby/cli/projects.py +79 -0
- gobby/cli/sessions.py +622 -0
- gobby/cli/tasks/__init__.py +30 -0
- gobby/cli/tasks/_utils.py +658 -0
- gobby/cli/tasks/ai.py +1025 -0
- gobby/cli/tasks/commits.py +169 -0
- gobby/cli/tasks/crud.py +685 -0
- gobby/cli/tasks/deps.py +135 -0
- gobby/cli/tasks/labels.py +63 -0
- gobby/cli/tasks/main.py +273 -0
- gobby/cli/tasks/search.py +178 -0
- gobby/cli/tui.py +34 -0
- gobby/cli/utils.py +513 -0
- gobby/cli/workflows.py +927 -0
- gobby/cli/worktrees.py +481 -0
- gobby/config/__init__.py +129 -0
- gobby/config/app.py +551 -0
- gobby/config/extensions.py +167 -0
- gobby/config/features.py +472 -0
- gobby/config/llm_providers.py +98 -0
- gobby/config/logging.py +66 -0
- gobby/config/mcp.py +346 -0
- gobby/config/persistence.py +247 -0
- gobby/config/servers.py +141 -0
- gobby/config/sessions.py +250 -0
- gobby/config/tasks.py +784 -0
- gobby/hooks/__init__.py +104 -0
- gobby/hooks/artifact_capture.py +213 -0
- gobby/hooks/broadcaster.py +243 -0
- gobby/hooks/event_handlers.py +723 -0
- gobby/hooks/events.py +218 -0
- gobby/hooks/git.py +169 -0
- gobby/hooks/health_monitor.py +171 -0
- gobby/hooks/hook_manager.py +856 -0
- gobby/hooks/hook_types.py +575 -0
- gobby/hooks/plugins.py +813 -0
- gobby/hooks/session_coordinator.py +396 -0
- gobby/hooks/verification_runner.py +268 -0
- gobby/hooks/webhooks.py +339 -0
- gobby/install/claude/commands/gobby/bug.md +51 -0
- gobby/install/claude/commands/gobby/chore.md +51 -0
- gobby/install/claude/commands/gobby/epic.md +52 -0
- gobby/install/claude/commands/gobby/eval.md +235 -0
- gobby/install/claude/commands/gobby/feat.md +49 -0
- gobby/install/claude/commands/gobby/nit.md +52 -0
- gobby/install/claude/commands/gobby/ref.md +52 -0
- gobby/install/claude/hooks/HOOK_SCHEMAS.md +632 -0
- gobby/install/claude/hooks/hook_dispatcher.py +364 -0
- gobby/install/claude/hooks/validate_settings.py +102 -0
- gobby/install/claude/hooks-template.json +118 -0
- gobby/install/codex/hooks/hook_dispatcher.py +153 -0
- gobby/install/codex/prompts/forget.md +7 -0
- gobby/install/codex/prompts/memories.md +7 -0
- gobby/install/codex/prompts/recall.md +7 -0
- gobby/install/codex/prompts/remember.md +13 -0
- gobby/install/gemini/hooks/hook_dispatcher.py +268 -0
- gobby/install/gemini/hooks-template.json +138 -0
- gobby/install/shared/plugins/code_guardian.py +456 -0
- gobby/install/shared/plugins/example_notify.py +331 -0
- gobby/integrations/__init__.py +10 -0
- gobby/integrations/github.py +145 -0
- gobby/integrations/linear.py +145 -0
- gobby/llm/__init__.py +40 -0
- gobby/llm/base.py +120 -0
- gobby/llm/claude.py +578 -0
- gobby/llm/claude_executor.py +503 -0
- gobby/llm/codex.py +322 -0
- gobby/llm/codex_executor.py +513 -0
- gobby/llm/executor.py +316 -0
- gobby/llm/factory.py +34 -0
- gobby/llm/gemini.py +258 -0
- gobby/llm/gemini_executor.py +339 -0
- gobby/llm/litellm.py +287 -0
- gobby/llm/litellm_executor.py +303 -0
- gobby/llm/resolver.py +499 -0
- gobby/llm/service.py +236 -0
- gobby/mcp_proxy/__init__.py +29 -0
- gobby/mcp_proxy/actions.py +175 -0
- gobby/mcp_proxy/daemon_control.py +198 -0
- gobby/mcp_proxy/importer.py +436 -0
- gobby/mcp_proxy/lazy.py +325 -0
- gobby/mcp_proxy/manager.py +798 -0
- gobby/mcp_proxy/metrics.py +609 -0
- gobby/mcp_proxy/models.py +139 -0
- gobby/mcp_proxy/registries.py +215 -0
- gobby/mcp_proxy/schema_hash.py +381 -0
- gobby/mcp_proxy/semantic_search.py +706 -0
- gobby/mcp_proxy/server.py +549 -0
- gobby/mcp_proxy/services/__init__.py +0 -0
- gobby/mcp_proxy/services/fallback.py +306 -0
- gobby/mcp_proxy/services/recommendation.py +224 -0
- gobby/mcp_proxy/services/server_mgmt.py +214 -0
- gobby/mcp_proxy/services/system.py +72 -0
- gobby/mcp_proxy/services/tool_filter.py +231 -0
- gobby/mcp_proxy/services/tool_proxy.py +309 -0
- gobby/mcp_proxy/stdio.py +565 -0
- gobby/mcp_proxy/tools/__init__.py +27 -0
- gobby/mcp_proxy/tools/agents.py +1103 -0
- gobby/mcp_proxy/tools/artifacts.py +207 -0
- gobby/mcp_proxy/tools/hub.py +335 -0
- gobby/mcp_proxy/tools/internal.py +337 -0
- gobby/mcp_proxy/tools/memory.py +543 -0
- gobby/mcp_proxy/tools/merge.py +422 -0
- gobby/mcp_proxy/tools/metrics.py +283 -0
- gobby/mcp_proxy/tools/orchestration/__init__.py +23 -0
- gobby/mcp_proxy/tools/orchestration/cleanup.py +619 -0
- gobby/mcp_proxy/tools/orchestration/monitor.py +380 -0
- gobby/mcp_proxy/tools/orchestration/orchestrate.py +746 -0
- gobby/mcp_proxy/tools/orchestration/review.py +736 -0
- gobby/mcp_proxy/tools/orchestration/utils.py +16 -0
- gobby/mcp_proxy/tools/session_messages.py +1056 -0
- gobby/mcp_proxy/tools/task_dependencies.py +219 -0
- gobby/mcp_proxy/tools/task_expansion.py +591 -0
- gobby/mcp_proxy/tools/task_github.py +393 -0
- gobby/mcp_proxy/tools/task_linear.py +379 -0
- gobby/mcp_proxy/tools/task_orchestration.py +77 -0
- gobby/mcp_proxy/tools/task_readiness.py +522 -0
- gobby/mcp_proxy/tools/task_sync.py +351 -0
- gobby/mcp_proxy/tools/task_validation.py +843 -0
- gobby/mcp_proxy/tools/tasks/__init__.py +25 -0
- gobby/mcp_proxy/tools/tasks/_context.py +112 -0
- gobby/mcp_proxy/tools/tasks/_crud.py +516 -0
- gobby/mcp_proxy/tools/tasks/_factory.py +176 -0
- gobby/mcp_proxy/tools/tasks/_helpers.py +129 -0
- gobby/mcp_proxy/tools/tasks/_lifecycle.py +517 -0
- gobby/mcp_proxy/tools/tasks/_lifecycle_validation.py +301 -0
- gobby/mcp_proxy/tools/tasks/_resolution.py +55 -0
- gobby/mcp_proxy/tools/tasks/_search.py +215 -0
- gobby/mcp_proxy/tools/tasks/_session.py +125 -0
- gobby/mcp_proxy/tools/workflows.py +973 -0
- gobby/mcp_proxy/tools/worktrees.py +1264 -0
- gobby/mcp_proxy/transports/__init__.py +0 -0
- gobby/mcp_proxy/transports/base.py +95 -0
- gobby/mcp_proxy/transports/factory.py +44 -0
- gobby/mcp_proxy/transports/http.py +139 -0
- gobby/mcp_proxy/transports/stdio.py +213 -0
- gobby/mcp_proxy/transports/websocket.py +136 -0
- gobby/memory/backends/__init__.py +116 -0
- gobby/memory/backends/mem0.py +408 -0
- gobby/memory/backends/memu.py +485 -0
- gobby/memory/backends/null.py +111 -0
- gobby/memory/backends/openmemory.py +537 -0
- gobby/memory/backends/sqlite.py +304 -0
- gobby/memory/context.py +87 -0
- gobby/memory/manager.py +1001 -0
- gobby/memory/protocol.py +451 -0
- gobby/memory/search/__init__.py +66 -0
- gobby/memory/search/text.py +127 -0
- gobby/memory/viz.py +258 -0
- gobby/prompts/__init__.py +13 -0
- gobby/prompts/defaults/expansion/system.md +119 -0
- gobby/prompts/defaults/expansion/user.md +48 -0
- gobby/prompts/defaults/external_validation/agent.md +72 -0
- gobby/prompts/defaults/external_validation/external.md +63 -0
- gobby/prompts/defaults/external_validation/spawn.md +83 -0
- gobby/prompts/defaults/external_validation/system.md +6 -0
- gobby/prompts/defaults/features/import_mcp.md +22 -0
- gobby/prompts/defaults/features/import_mcp_github.md +17 -0
- gobby/prompts/defaults/features/import_mcp_search.md +16 -0
- gobby/prompts/defaults/features/recommend_tools.md +32 -0
- gobby/prompts/defaults/features/recommend_tools_hybrid.md +35 -0
- gobby/prompts/defaults/features/recommend_tools_llm.md +30 -0
- gobby/prompts/defaults/features/server_description.md +20 -0
- gobby/prompts/defaults/features/server_description_system.md +6 -0
- gobby/prompts/defaults/features/task_description.md +31 -0
- gobby/prompts/defaults/features/task_description_system.md +6 -0
- gobby/prompts/defaults/features/tool_summary.md +17 -0
- gobby/prompts/defaults/features/tool_summary_system.md +6 -0
- gobby/prompts/defaults/research/step.md +58 -0
- gobby/prompts/defaults/validation/criteria.md +47 -0
- gobby/prompts/defaults/validation/validate.md +38 -0
- gobby/prompts/loader.py +346 -0
- gobby/prompts/models.py +113 -0
- gobby/py.typed +0 -0
- gobby/runner.py +488 -0
- gobby/search/__init__.py +23 -0
- gobby/search/protocol.py +104 -0
- gobby/search/tfidf.py +232 -0
- gobby/servers/__init__.py +7 -0
- gobby/servers/http.py +636 -0
- gobby/servers/models.py +31 -0
- gobby/servers/routes/__init__.py +23 -0
- gobby/servers/routes/admin.py +416 -0
- gobby/servers/routes/dependencies.py +118 -0
- gobby/servers/routes/mcp/__init__.py +24 -0
- gobby/servers/routes/mcp/hooks.py +135 -0
- gobby/servers/routes/mcp/plugins.py +121 -0
- gobby/servers/routes/mcp/tools.py +1337 -0
- gobby/servers/routes/mcp/webhooks.py +159 -0
- gobby/servers/routes/sessions.py +582 -0
- gobby/servers/websocket.py +766 -0
- gobby/sessions/__init__.py +13 -0
- gobby/sessions/analyzer.py +322 -0
- gobby/sessions/lifecycle.py +240 -0
- gobby/sessions/manager.py +563 -0
- gobby/sessions/processor.py +225 -0
- gobby/sessions/summary.py +532 -0
- gobby/sessions/transcripts/__init__.py +41 -0
- gobby/sessions/transcripts/base.py +125 -0
- gobby/sessions/transcripts/claude.py +386 -0
- gobby/sessions/transcripts/codex.py +143 -0
- gobby/sessions/transcripts/gemini.py +195 -0
- gobby/storage/__init__.py +21 -0
- gobby/storage/agents.py +409 -0
- gobby/storage/artifact_classifier.py +341 -0
- gobby/storage/artifacts.py +285 -0
- gobby/storage/compaction.py +67 -0
- gobby/storage/database.py +357 -0
- gobby/storage/inter_session_messages.py +194 -0
- gobby/storage/mcp.py +680 -0
- gobby/storage/memories.py +562 -0
- gobby/storage/merge_resolutions.py +550 -0
- gobby/storage/migrations.py +860 -0
- gobby/storage/migrations_legacy.py +1359 -0
- gobby/storage/projects.py +166 -0
- gobby/storage/session_messages.py +251 -0
- gobby/storage/session_tasks.py +97 -0
- gobby/storage/sessions.py +817 -0
- gobby/storage/task_dependencies.py +223 -0
- gobby/storage/tasks/__init__.py +42 -0
- gobby/storage/tasks/_aggregates.py +180 -0
- gobby/storage/tasks/_crud.py +449 -0
- gobby/storage/tasks/_id.py +104 -0
- gobby/storage/tasks/_lifecycle.py +311 -0
- gobby/storage/tasks/_manager.py +889 -0
- gobby/storage/tasks/_models.py +300 -0
- gobby/storage/tasks/_ordering.py +119 -0
- gobby/storage/tasks/_path_cache.py +110 -0
- gobby/storage/tasks/_queries.py +343 -0
- gobby/storage/tasks/_search.py +143 -0
- gobby/storage/workflow_audit.py +393 -0
- gobby/storage/worktrees.py +547 -0
- gobby/sync/__init__.py +29 -0
- gobby/sync/github.py +333 -0
- gobby/sync/linear.py +304 -0
- gobby/sync/memories.py +284 -0
- gobby/sync/tasks.py +641 -0
- gobby/tasks/__init__.py +8 -0
- gobby/tasks/build_verification.py +193 -0
- gobby/tasks/commits.py +633 -0
- gobby/tasks/context.py +747 -0
- gobby/tasks/criteria.py +342 -0
- gobby/tasks/enhanced_validator.py +226 -0
- gobby/tasks/escalation.py +263 -0
- gobby/tasks/expansion.py +626 -0
- gobby/tasks/external_validator.py +764 -0
- gobby/tasks/issue_extraction.py +171 -0
- gobby/tasks/prompts/expand.py +327 -0
- gobby/tasks/research.py +421 -0
- gobby/tasks/tdd.py +352 -0
- gobby/tasks/tree_builder.py +263 -0
- gobby/tasks/validation.py +712 -0
- gobby/tasks/validation_history.py +357 -0
- gobby/tasks/validation_models.py +89 -0
- gobby/tools/__init__.py +0 -0
- gobby/tools/summarizer.py +170 -0
- gobby/tui/__init__.py +5 -0
- gobby/tui/api_client.py +281 -0
- gobby/tui/app.py +327 -0
- gobby/tui/screens/__init__.py +25 -0
- gobby/tui/screens/agents.py +333 -0
- gobby/tui/screens/chat.py +450 -0
- gobby/tui/screens/dashboard.py +377 -0
- gobby/tui/screens/memory.py +305 -0
- gobby/tui/screens/metrics.py +231 -0
- gobby/tui/screens/orchestrator.py +904 -0
- gobby/tui/screens/sessions.py +412 -0
- gobby/tui/screens/tasks.py +442 -0
- gobby/tui/screens/workflows.py +289 -0
- gobby/tui/screens/worktrees.py +174 -0
- gobby/tui/widgets/__init__.py +21 -0
- gobby/tui/widgets/chat.py +210 -0
- gobby/tui/widgets/conductor.py +104 -0
- gobby/tui/widgets/menu.py +132 -0
- gobby/tui/widgets/message_panel.py +160 -0
- gobby/tui/widgets/review_gate.py +224 -0
- gobby/tui/widgets/task_tree.py +99 -0
- gobby/tui/widgets/token_budget.py +166 -0
- gobby/tui/ws_client.py +258 -0
- gobby/utils/__init__.py +3 -0
- gobby/utils/daemon_client.py +235 -0
- gobby/utils/git.py +222 -0
- gobby/utils/id.py +38 -0
- gobby/utils/json_helpers.py +161 -0
- gobby/utils/logging.py +376 -0
- gobby/utils/machine_id.py +135 -0
- gobby/utils/metrics.py +589 -0
- gobby/utils/project_context.py +182 -0
- gobby/utils/project_init.py +263 -0
- gobby/utils/status.py +256 -0
- gobby/utils/validation.py +80 -0
- gobby/utils/version.py +23 -0
- gobby/workflows/__init__.py +4 -0
- gobby/workflows/actions.py +1310 -0
- gobby/workflows/approval_flow.py +138 -0
- gobby/workflows/artifact_actions.py +103 -0
- gobby/workflows/audit_helpers.py +110 -0
- gobby/workflows/autonomous_actions.py +286 -0
- gobby/workflows/context_actions.py +394 -0
- gobby/workflows/definitions.py +130 -0
- gobby/workflows/detection_helpers.py +208 -0
- gobby/workflows/engine.py +485 -0
- gobby/workflows/evaluator.py +669 -0
- gobby/workflows/git_utils.py +96 -0
- gobby/workflows/hooks.py +169 -0
- gobby/workflows/lifecycle_evaluator.py +613 -0
- gobby/workflows/llm_actions.py +70 -0
- gobby/workflows/loader.py +333 -0
- gobby/workflows/mcp_actions.py +60 -0
- gobby/workflows/memory_actions.py +272 -0
- gobby/workflows/premature_stop.py +164 -0
- gobby/workflows/session_actions.py +139 -0
- gobby/workflows/state_actions.py +123 -0
- gobby/workflows/state_manager.py +104 -0
- gobby/workflows/stop_signal_actions.py +163 -0
- gobby/workflows/summary_actions.py +344 -0
- gobby/workflows/task_actions.py +249 -0
- gobby/workflows/task_enforcement_actions.py +901 -0
- gobby/workflows/templates.py +52 -0
- gobby/workflows/todo_actions.py +84 -0
- gobby/workflows/webhook.py +223 -0
- gobby/workflows/webhook_executor.py +399 -0
- gobby/worktrees/__init__.py +5 -0
- gobby/worktrees/git.py +690 -0
- gobby/worktrees/merge/__init__.py +20 -0
- gobby/worktrees/merge/conflict_parser.py +177 -0
- gobby/worktrees/merge/resolver.py +485 -0
- gobby-0.2.5.dist-info/METADATA +351 -0
- gobby-0.2.5.dist-info/RECORD +383 -0
- gobby-0.2.5.dist-info/WHEEL +5 -0
- gobby-0.2.5.dist-info/entry_points.txt +2 -0
- gobby-0.2.5.dist-info/licenses/LICENSE.md +193 -0
- gobby-0.2.5.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,1103 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Internal MCP tools for Gobby Agent System.
|
|
3
|
+
|
|
4
|
+
Exposes functionality for:
|
|
5
|
+
- Starting agents (spawn subagents with prompts)
|
|
6
|
+
- Getting agent results (retrieve completed run output)
|
|
7
|
+
- Listing agents (view runs for a session)
|
|
8
|
+
- Cancelling agents (stop running agents)
|
|
9
|
+
|
|
10
|
+
These tools are registered with the InternalToolRegistry and accessed
|
|
11
|
+
via the downstream proxy pattern (call_tool, list_tools, get_tool_schema).
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import logging
|
|
17
|
+
import socket
|
|
18
|
+
from collections.abc import Callable
|
|
19
|
+
from pathlib import Path
|
|
20
|
+
from typing import TYPE_CHECKING, Any
|
|
21
|
+
|
|
22
|
+
from gobby.agents.context import (
|
|
23
|
+
ContextResolutionError,
|
|
24
|
+
ContextResolver,
|
|
25
|
+
format_injected_prompt,
|
|
26
|
+
)
|
|
27
|
+
from gobby.agents.registry import (
|
|
28
|
+
RunningAgent,
|
|
29
|
+
RunningAgentRegistry,
|
|
30
|
+
get_running_agent_registry,
|
|
31
|
+
)
|
|
32
|
+
from gobby.agents.spawn import (
|
|
33
|
+
EmbeddedSpawner,
|
|
34
|
+
HeadlessSpawner,
|
|
35
|
+
TerminalSpawner,
|
|
36
|
+
)
|
|
37
|
+
from gobby.mcp_proxy.tools.internal import InternalToolRegistry
|
|
38
|
+
from gobby.utils.project_context import get_project_context
|
|
39
|
+
|
|
40
|
+
if TYPE_CHECKING:
|
|
41
|
+
from gobby.agents.runner import AgentRunner
|
|
42
|
+
from gobby.config.app import ContextInjectionConfig
|
|
43
|
+
from gobby.llm.executor import ToolResult
|
|
44
|
+
from gobby.mcp_proxy.services.tool_proxy import ToolProxyService
|
|
45
|
+
from gobby.storage.session_messages import LocalSessionMessageManager
|
|
46
|
+
from gobby.storage.sessions import LocalSessionManager
|
|
47
|
+
|
|
48
|
+
logger = logging.getLogger(__name__)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def create_agents_registry(
|
|
52
|
+
runner: AgentRunner,
|
|
53
|
+
session_manager: LocalSessionManager | None = None,
|
|
54
|
+
message_manager: LocalSessionMessageManager | None = None,
|
|
55
|
+
context_config: ContextInjectionConfig | None = None,
|
|
56
|
+
get_session_context: Any | None = None,
|
|
57
|
+
running_registry: RunningAgentRegistry | None = None,
|
|
58
|
+
tool_proxy_getter: Callable[[], ToolProxyService | None] | None = None,
|
|
59
|
+
workflow_state_manager: Any | None = None,
|
|
60
|
+
) -> InternalToolRegistry:
|
|
61
|
+
"""
|
|
62
|
+
Create an agent tool registry with all agent-related tools.
|
|
63
|
+
|
|
64
|
+
Args:
|
|
65
|
+
runner: AgentRunner instance for executing agents.
|
|
66
|
+
session_manager: Session manager for context resolution.
|
|
67
|
+
message_manager: Message manager for transcript resolution.
|
|
68
|
+
context_config: Context injection configuration.
|
|
69
|
+
get_session_context: Optional callable returning current session context.
|
|
70
|
+
running_registry: Optional in-memory registry for running agents.
|
|
71
|
+
tool_proxy_getter: Optional callable that returns ToolProxyService for
|
|
72
|
+
routing tool calls in in-process agents. If not provided, tool calls
|
|
73
|
+
will fail with "tool not available".
|
|
74
|
+
workflow_state_manager: Optional WorkflowStateManager for stopping workflows
|
|
75
|
+
when agents are killed. If not provided, workflow stop will be skipped.
|
|
76
|
+
|
|
77
|
+
Returns:
|
|
78
|
+
InternalToolRegistry with all agent tools registered.
|
|
79
|
+
"""
|
|
80
|
+
registry = InternalToolRegistry(
|
|
81
|
+
name="gobby-agents",
|
|
82
|
+
description="Agent spawning - start, monitor, and manage subagents",
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
# Use provided registry or global singleton
|
|
86
|
+
agent_registry = running_registry or get_running_agent_registry()
|
|
87
|
+
|
|
88
|
+
# Create context resolver if managers are provided
|
|
89
|
+
context_resolver: ContextResolver | None = None
|
|
90
|
+
context_enabled = True # Default enabled
|
|
91
|
+
context_template: str | None = None # Custom template for injection
|
|
92
|
+
if session_manager and message_manager:
|
|
93
|
+
# Use config values if provided, otherwise use defaults
|
|
94
|
+
if context_config:
|
|
95
|
+
context_enabled = context_config.enabled
|
|
96
|
+
context_template = context_config.context_template
|
|
97
|
+
context_resolver = ContextResolver(
|
|
98
|
+
session_manager=session_manager,
|
|
99
|
+
message_manager=message_manager,
|
|
100
|
+
project_path=None, # Will be set per-request
|
|
101
|
+
max_file_size=context_config.max_file_size,
|
|
102
|
+
max_content_size=context_config.max_content_size,
|
|
103
|
+
max_transcript_messages=context_config.max_transcript_messages,
|
|
104
|
+
truncation_suffix=context_config.truncation_suffix,
|
|
105
|
+
)
|
|
106
|
+
else:
|
|
107
|
+
context_resolver = ContextResolver(
|
|
108
|
+
session_manager=session_manager,
|
|
109
|
+
message_manager=message_manager,
|
|
110
|
+
project_path=None, # Will be set per-request
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
@registry.tool(
|
|
114
|
+
name="start_agent",
|
|
115
|
+
description=(
|
|
116
|
+
"Spawn a subagent to execute a task. Can use a named agent definition "
|
|
117
|
+
"(e.g. 'validation-runner') or raw parameters. "
|
|
118
|
+
"Infers context from current project/session. "
|
|
119
|
+
"Use get_agent_result to poll for completion."
|
|
120
|
+
),
|
|
121
|
+
)
|
|
122
|
+
async def start_agent(
|
|
123
|
+
prompt: str,
|
|
124
|
+
workflow: str | None = None,
|
|
125
|
+
task: str | None = None,
|
|
126
|
+
agent: str | None = None,
|
|
127
|
+
session_context: str = "summary_markdown",
|
|
128
|
+
mode: str = "terminal",
|
|
129
|
+
terminal: str = "auto",
|
|
130
|
+
provider: str | None = None,
|
|
131
|
+
model: str | None = None,
|
|
132
|
+
worktree_id: str | None = None,
|
|
133
|
+
timeout: float = 120.0,
|
|
134
|
+
max_turns: int = 10,
|
|
135
|
+
# Optional explicit context (usually inferred)
|
|
136
|
+
parent_session_id: str | None = None,
|
|
137
|
+
project_id: str | None = None,
|
|
138
|
+
machine_id: str | None = None,
|
|
139
|
+
source: str = "claude",
|
|
140
|
+
) -> dict[str, Any]:
|
|
141
|
+
"""
|
|
142
|
+
Start a new agent to execute a task.
|
|
143
|
+
|
|
144
|
+
Args:
|
|
145
|
+
prompt: The task/prompt for the agent.
|
|
146
|
+
workflow: Workflow name or path to execute.
|
|
147
|
+
task: Task ID or 'next' for auto-select.
|
|
148
|
+
agent: Named agent definition to use.
|
|
149
|
+
session_context: Context source (summary_markdown, compact_markdown,
|
|
150
|
+
session_id:<id>, transcript:<n>, file:<path>).
|
|
151
|
+
mode: Execution mode (in_process, terminal, embedded, headless).
|
|
152
|
+
terminal: Terminal for terminal/embedded modes (auto, ghostty, iterm, etc.).
|
|
153
|
+
provider: LLM provider (claude, gemini, etc.). Defaults to claude.
|
|
154
|
+
model: Optional model override.
|
|
155
|
+
worktree_id: Existing worktree to use for terminal mode.
|
|
156
|
+
timeout: Execution timeout in seconds (default: 120).
|
|
157
|
+
max_turns: Maximum turns (default: 10).
|
|
158
|
+
parent_session_id: Explicit parent session ID (usually inferred).
|
|
159
|
+
project_id: Explicit project ID (usually inferred from context).
|
|
160
|
+
machine_id: Explicit machine ID (usually inferred from hostname).
|
|
161
|
+
source: CLI source (claude, gemini, codex).
|
|
162
|
+
|
|
163
|
+
Returns:
|
|
164
|
+
Dict with run_id, child_session_id, status.
|
|
165
|
+
"""
|
|
166
|
+
from gobby.agents.runner import AgentConfig
|
|
167
|
+
|
|
168
|
+
# Validate mode
|
|
169
|
+
supported_modes = {"in_process", "terminal", "embedded", "headless"}
|
|
170
|
+
if mode not in supported_modes:
|
|
171
|
+
return {
|
|
172
|
+
"success": False,
|
|
173
|
+
"error": f"Invalid mode '{mode}'. Supported: {supported_modes}",
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
# Validate workflow (reject lifecycle workflows)
|
|
177
|
+
if workflow:
|
|
178
|
+
from gobby.workflows.loader import WorkflowLoader
|
|
179
|
+
|
|
180
|
+
workflow_loader = WorkflowLoader()
|
|
181
|
+
is_valid, error_msg = workflow_loader.validate_workflow_for_agent(workflow)
|
|
182
|
+
if not is_valid:
|
|
183
|
+
return {
|
|
184
|
+
"success": False,
|
|
185
|
+
"error": error_msg,
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
# Infer context from project if not provided
|
|
189
|
+
ctx = get_project_context()
|
|
190
|
+
if project_id is None:
|
|
191
|
+
if ctx:
|
|
192
|
+
project_id = ctx.get("id")
|
|
193
|
+
project_path = ctx.get("project_path")
|
|
194
|
+
else:
|
|
195
|
+
return {
|
|
196
|
+
"success": False,
|
|
197
|
+
"error": "No project context found. Run from a Gobby project directory.",
|
|
198
|
+
}
|
|
199
|
+
else:
|
|
200
|
+
# project_id was provided - try to get project_path from context if it matches
|
|
201
|
+
if ctx and ctx.get("id") == project_id:
|
|
202
|
+
project_path = ctx.get("project_path")
|
|
203
|
+
else:
|
|
204
|
+
project_path = None
|
|
205
|
+
|
|
206
|
+
# Infer machine_id from hostname if not provided
|
|
207
|
+
if machine_id is None:
|
|
208
|
+
machine_id = socket.gethostname()
|
|
209
|
+
|
|
210
|
+
# Parent session is required for depth checking
|
|
211
|
+
if parent_session_id is None:
|
|
212
|
+
# TODO: In future, could look up current active session for project
|
|
213
|
+
return {
|
|
214
|
+
"success": False,
|
|
215
|
+
"error": "parent_session_id is required (session context inference not yet implemented)",
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
# Check if spawning is allowed
|
|
219
|
+
can_spawn, reason, _parent_depth = runner.can_spawn(parent_session_id)
|
|
220
|
+
if not can_spawn:
|
|
221
|
+
return {
|
|
222
|
+
"success": False,
|
|
223
|
+
"error": reason,
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
# Resolve context and inject into prompt
|
|
227
|
+
effective_prompt = prompt
|
|
228
|
+
context_was_injected = False
|
|
229
|
+
if context_resolver and context_enabled and session_context:
|
|
230
|
+
try:
|
|
231
|
+
# Update resolver's project path for file resolution
|
|
232
|
+
context_resolver._project_path = Path(project_path) if project_path else None
|
|
233
|
+
|
|
234
|
+
resolved_context = await context_resolver.resolve(
|
|
235
|
+
session_context, parent_session_id
|
|
236
|
+
)
|
|
237
|
+
if resolved_context:
|
|
238
|
+
effective_prompt = format_injected_prompt(
|
|
239
|
+
resolved_context, prompt, template=context_template
|
|
240
|
+
)
|
|
241
|
+
context_was_injected = True
|
|
242
|
+
logger.info(
|
|
243
|
+
f"Injected context from '{session_context}' into agent prompt "
|
|
244
|
+
f"({len(resolved_context)} chars)"
|
|
245
|
+
)
|
|
246
|
+
except ContextResolutionError as e:
|
|
247
|
+
logger.warning(f"Context resolution failed: {e}")
|
|
248
|
+
# Continue with original prompt - context injection is best-effort
|
|
249
|
+
pass
|
|
250
|
+
|
|
251
|
+
# Use provided provider or default
|
|
252
|
+
effective_provider = provider or "claude"
|
|
253
|
+
|
|
254
|
+
config = AgentConfig(
|
|
255
|
+
prompt=effective_prompt,
|
|
256
|
+
parent_session_id=parent_session_id,
|
|
257
|
+
project_id=project_id,
|
|
258
|
+
machine_id=machine_id,
|
|
259
|
+
source=source,
|
|
260
|
+
workflow=workflow,
|
|
261
|
+
task=task,
|
|
262
|
+
agent=agent,
|
|
263
|
+
session_context=session_context,
|
|
264
|
+
mode=mode,
|
|
265
|
+
terminal=terminal,
|
|
266
|
+
worktree_id=worktree_id,
|
|
267
|
+
provider=effective_provider,
|
|
268
|
+
model=model,
|
|
269
|
+
max_turns=max_turns,
|
|
270
|
+
timeout=timeout,
|
|
271
|
+
project_path=project_path,
|
|
272
|
+
context_injected=context_was_injected,
|
|
273
|
+
)
|
|
274
|
+
|
|
275
|
+
# Handle different execution modes
|
|
276
|
+
if mode == "in_process":
|
|
277
|
+
# In-process mode: run directly via runner
|
|
278
|
+
async def tool_handler(tool_name: str, arguments: dict[str, Any]) -> ToolResult:
|
|
279
|
+
from gobby.llm.executor import ToolResult
|
|
280
|
+
|
|
281
|
+
# Get tool proxy for routing calls
|
|
282
|
+
tool_proxy = tool_proxy_getter() if tool_proxy_getter else None
|
|
283
|
+
if tool_proxy is None:
|
|
284
|
+
return ToolResult(
|
|
285
|
+
tool_name=tool_name,
|
|
286
|
+
success=False,
|
|
287
|
+
error=f"Tool proxy not configured - cannot route tool {tool_name}",
|
|
288
|
+
)
|
|
289
|
+
|
|
290
|
+
# Route the tool call through the MCP proxy
|
|
291
|
+
try:
|
|
292
|
+
result = await tool_proxy.call_tool_by_name(tool_name, arguments)
|
|
293
|
+
|
|
294
|
+
# Handle error response format from call_tool_by_name
|
|
295
|
+
if isinstance(result, dict) and result.get("success") is False:
|
|
296
|
+
return ToolResult(
|
|
297
|
+
tool_name=tool_name,
|
|
298
|
+
success=False,
|
|
299
|
+
error=result.get("error", f"Tool {tool_name} failed"),
|
|
300
|
+
)
|
|
301
|
+
|
|
302
|
+
# Successful tool call
|
|
303
|
+
return ToolResult(
|
|
304
|
+
tool_name=tool_name,
|
|
305
|
+
success=True,
|
|
306
|
+
result=result,
|
|
307
|
+
)
|
|
308
|
+
except Exception as e:
|
|
309
|
+
logger.warning(f"Tool call failed for {tool_name}: {e}")
|
|
310
|
+
return ToolResult(
|
|
311
|
+
tool_name=tool_name,
|
|
312
|
+
success=False,
|
|
313
|
+
error=str(e),
|
|
314
|
+
)
|
|
315
|
+
|
|
316
|
+
# Load available tools for the agent
|
|
317
|
+
from gobby.llm.executor import ToolSchema
|
|
318
|
+
|
|
319
|
+
tool_schemas: list[ToolSchema] = []
|
|
320
|
+
tool_proxy = tool_proxy_getter() if tool_proxy_getter else None
|
|
321
|
+
if tool_proxy:
|
|
322
|
+
# Get internal servers that have tools
|
|
323
|
+
internal_servers = ["gobby-tasks", "gobby-memory", "gobby-sessions"]
|
|
324
|
+
for srv in internal_servers:
|
|
325
|
+
try:
|
|
326
|
+
tools_result = await tool_proxy.list_tools(srv)
|
|
327
|
+
if tools_result.get("success"):
|
|
328
|
+
for tool_brief in tools_result.get("tools", []):
|
|
329
|
+
# Get full schema for each tool
|
|
330
|
+
schema_result = await tool_proxy.get_tool_schema(
|
|
331
|
+
srv, tool_brief["name"]
|
|
332
|
+
)
|
|
333
|
+
if schema_result.get("success"):
|
|
334
|
+
tool_data = schema_result.get("tool", {})
|
|
335
|
+
tool_schemas.append(
|
|
336
|
+
ToolSchema(
|
|
337
|
+
name=tool_brief["name"],
|
|
338
|
+
description=tool_brief.get("brief", ""),
|
|
339
|
+
input_schema=tool_data.get("inputSchema", {}),
|
|
340
|
+
server_name=srv,
|
|
341
|
+
)
|
|
342
|
+
)
|
|
343
|
+
except Exception as e:
|
|
344
|
+
logger.debug(f"Could not load tools from {srv}: {e}")
|
|
345
|
+
|
|
346
|
+
# Set tools on config
|
|
347
|
+
config.tools = tool_schemas
|
|
348
|
+
logger.info(f"Loaded {len(tool_schemas)} tools for in-process agent")
|
|
349
|
+
|
|
350
|
+
result = await runner.run(config, tool_handler=tool_handler)
|
|
351
|
+
|
|
352
|
+
return {
|
|
353
|
+
"success": result.status in ("success", "partial"),
|
|
354
|
+
"run_id": result.run_id,
|
|
355
|
+
"status": result.status,
|
|
356
|
+
"output": result.output,
|
|
357
|
+
"error": result.error,
|
|
358
|
+
"turns_used": result.turns_used,
|
|
359
|
+
"tool_calls_count": len(result.tool_calls),
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
# Special handling for Gemini terminal mode: requires preflight session capture
|
|
363
|
+
# Gemini CLI in interactive mode can't introspect its session_id, so we:
|
|
364
|
+
# 1. Launch preflight to capture session_id from stream-json output
|
|
365
|
+
# 2. Create Gobby session with external_id = gemini's session_id
|
|
366
|
+
# 3. Launch interactive with -r {session_id} to resume
|
|
367
|
+
if mode == "terminal" and effective_provider == "gemini":
|
|
368
|
+
from gobby.agents.spawn import (
|
|
369
|
+
build_gemini_command_with_resume,
|
|
370
|
+
prepare_gemini_spawn_with_preflight,
|
|
371
|
+
)
|
|
372
|
+
|
|
373
|
+
# Ensure project_id is non-None for spawning
|
|
374
|
+
if project_id is None:
|
|
375
|
+
return {
|
|
376
|
+
"success": False,
|
|
377
|
+
"error": "project_id is required for spawning Gemini agent",
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
# Determine working directory
|
|
381
|
+
cwd = project_path or "."
|
|
382
|
+
|
|
383
|
+
try:
|
|
384
|
+
# Preflight capture: gets Gemini's session_id and creates linked Gobby session
|
|
385
|
+
spawn_context = await prepare_gemini_spawn_with_preflight(
|
|
386
|
+
session_manager=runner._child_session_manager,
|
|
387
|
+
parent_session_id=parent_session_id,
|
|
388
|
+
project_id=project_id,
|
|
389
|
+
machine_id=socket.gethostname(),
|
|
390
|
+
workflow_name=workflow,
|
|
391
|
+
git_branch=None, # Will be detected by hook
|
|
392
|
+
)
|
|
393
|
+
except FileNotFoundError as e:
|
|
394
|
+
return {
|
|
395
|
+
"success": False,
|
|
396
|
+
"error": str(e),
|
|
397
|
+
}
|
|
398
|
+
except Exception as e:
|
|
399
|
+
logger.error(f"Gemini preflight capture failed: {e}", exc_info=True)
|
|
400
|
+
return {
|
|
401
|
+
"success": False,
|
|
402
|
+
"error": f"Gemini preflight capture failed: {e}",
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
# Extract IDs from prepared spawn context
|
|
406
|
+
gobby_session_id = spawn_context.session_id
|
|
407
|
+
gemini_session_id = spawn_context.env_vars["GOBBY_GEMINI_EXTERNAL_ID"]
|
|
408
|
+
|
|
409
|
+
# Build command with session context injected into prompt
|
|
410
|
+
# build_gemini_command_with_resume handles the context prefix
|
|
411
|
+
cmd = build_gemini_command_with_resume(
|
|
412
|
+
gemini_external_id=gemini_session_id,
|
|
413
|
+
prompt=effective_prompt,
|
|
414
|
+
auto_approve=True, # Subagents need to work autonomously
|
|
415
|
+
gobby_session_id=gobby_session_id,
|
|
416
|
+
)
|
|
417
|
+
|
|
418
|
+
# Spawn in terminal
|
|
419
|
+
terminal_spawner = TerminalSpawner()
|
|
420
|
+
terminal_result = terminal_spawner.spawn(
|
|
421
|
+
command=cmd,
|
|
422
|
+
cwd=cwd,
|
|
423
|
+
terminal=terminal,
|
|
424
|
+
)
|
|
425
|
+
|
|
426
|
+
if not terminal_result.success:
|
|
427
|
+
return {
|
|
428
|
+
"success": False,
|
|
429
|
+
"error": terminal_result.error or terminal_result.message,
|
|
430
|
+
"child_session_id": gobby_session_id,
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
# Register in running agents registry
|
|
434
|
+
registry = get_running_agent_registry()
|
|
435
|
+
running_agent = RunningAgent(
|
|
436
|
+
run_id=f"gemini-{gemini_session_id[:8]}",
|
|
437
|
+
session_id=gobby_session_id,
|
|
438
|
+
parent_session_id=parent_session_id,
|
|
439
|
+
pid=terminal_result.pid,
|
|
440
|
+
mode="terminal",
|
|
441
|
+
provider="gemini",
|
|
442
|
+
workflow_name=workflow,
|
|
443
|
+
)
|
|
444
|
+
registry.add(running_agent)
|
|
445
|
+
|
|
446
|
+
return {
|
|
447
|
+
"success": True,
|
|
448
|
+
"run_id": running_agent.run_id,
|
|
449
|
+
"child_session_id": gobby_session_id,
|
|
450
|
+
"gemini_session_id": gemini_session_id,
|
|
451
|
+
"mode": "terminal",
|
|
452
|
+
"message": (f"Gemini agent spawned in terminal with session {gobby_session_id}"),
|
|
453
|
+
"pid": terminal_result.pid,
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
# Special handling for Codex terminal mode: requires preflight session capture
|
|
457
|
+
# Codex outputs session_id in startup banner, which we parse from `codex exec "exit"`
|
|
458
|
+
if mode == "terminal" and effective_provider == "codex":
|
|
459
|
+
from gobby.agents.spawn import (
|
|
460
|
+
build_codex_command_with_resume,
|
|
461
|
+
prepare_codex_spawn_with_preflight,
|
|
462
|
+
)
|
|
463
|
+
|
|
464
|
+
# Ensure project_id is non-None for spawning
|
|
465
|
+
if project_id is None:
|
|
466
|
+
return {
|
|
467
|
+
"success": False,
|
|
468
|
+
"error": "project_id is required for spawning Codex agent",
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
# Determine working directory
|
|
472
|
+
cwd = project_path or "."
|
|
473
|
+
|
|
474
|
+
try:
|
|
475
|
+
# Preflight capture: gets Codex's session_id and creates linked Gobby session
|
|
476
|
+
spawn_context = await prepare_codex_spawn_with_preflight(
|
|
477
|
+
session_manager=runner._child_session_manager,
|
|
478
|
+
parent_session_id=parent_session_id,
|
|
479
|
+
project_id=project_id,
|
|
480
|
+
machine_id=socket.gethostname(),
|
|
481
|
+
workflow_name=workflow,
|
|
482
|
+
git_branch=None, # Will be detected by hook
|
|
483
|
+
)
|
|
484
|
+
except FileNotFoundError as e:
|
|
485
|
+
return {
|
|
486
|
+
"success": False,
|
|
487
|
+
"error": str(e),
|
|
488
|
+
}
|
|
489
|
+
except Exception as e:
|
|
490
|
+
logger.error(f"Codex preflight capture failed: {e}", exc_info=True)
|
|
491
|
+
return {
|
|
492
|
+
"success": False,
|
|
493
|
+
"error": f"Codex preflight capture failed: {e}",
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
# Extract IDs from prepared spawn context
|
|
497
|
+
gobby_session_id = spawn_context.session_id
|
|
498
|
+
codex_session_id = spawn_context.env_vars["GOBBY_CODEX_EXTERNAL_ID"]
|
|
499
|
+
|
|
500
|
+
# Build command with session context injected into prompt
|
|
501
|
+
# build_codex_command_with_resume handles the context prefix
|
|
502
|
+
cmd = build_codex_command_with_resume(
|
|
503
|
+
codex_external_id=codex_session_id,
|
|
504
|
+
prompt=effective_prompt,
|
|
505
|
+
auto_approve=True, # --full-auto for sandboxed autonomy
|
|
506
|
+
gobby_session_id=gobby_session_id,
|
|
507
|
+
working_directory=cwd,
|
|
508
|
+
)
|
|
509
|
+
|
|
510
|
+
# Spawn in terminal
|
|
511
|
+
terminal_spawner = TerminalSpawner()
|
|
512
|
+
terminal_result = terminal_spawner.spawn(
|
|
513
|
+
command=cmd,
|
|
514
|
+
cwd=cwd,
|
|
515
|
+
terminal=terminal,
|
|
516
|
+
)
|
|
517
|
+
|
|
518
|
+
if not terminal_result.success:
|
|
519
|
+
return {
|
|
520
|
+
"success": False,
|
|
521
|
+
"error": terminal_result.error or terminal_result.message,
|
|
522
|
+
"child_session_id": gobby_session_id,
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
# Register in running agents registry
|
|
526
|
+
registry = get_running_agent_registry()
|
|
527
|
+
running_agent = RunningAgent(
|
|
528
|
+
run_id=f"codex-{codex_session_id[:8]}",
|
|
529
|
+
session_id=gobby_session_id,
|
|
530
|
+
parent_session_id=parent_session_id,
|
|
531
|
+
pid=terminal_result.pid,
|
|
532
|
+
mode="terminal",
|
|
533
|
+
provider="codex",
|
|
534
|
+
workflow_name=workflow,
|
|
535
|
+
)
|
|
536
|
+
registry.add(running_agent)
|
|
537
|
+
|
|
538
|
+
return {
|
|
539
|
+
"success": True,
|
|
540
|
+
"run_id": running_agent.run_id,
|
|
541
|
+
"child_session_id": gobby_session_id,
|
|
542
|
+
"codex_session_id": codex_session_id,
|
|
543
|
+
"mode": "terminal",
|
|
544
|
+
"message": (f"Codex agent spawned in terminal with session {gobby_session_id}"),
|
|
545
|
+
"pid": terminal_result.pid,
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
# Terminal, embedded, or headless mode: prepare run then spawn
|
|
549
|
+
# Use prepare_run to create session and run records
|
|
550
|
+
from gobby.llm.executor import AgentResult
|
|
551
|
+
|
|
552
|
+
prepare_result = runner.prepare_run(config)
|
|
553
|
+
if isinstance(prepare_result, AgentResult):
|
|
554
|
+
# prepare_run returns AgentResult on error
|
|
555
|
+
return {
|
|
556
|
+
"success": False,
|
|
557
|
+
"error": prepare_result.error,
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
# Successfully prepared - we have context with session and run
|
|
561
|
+
context = prepare_result
|
|
562
|
+
|
|
563
|
+
# Validate context has required session and run (should always be set after prepare_run)
|
|
564
|
+
if context.session is None or context.run is None:
|
|
565
|
+
return {
|
|
566
|
+
"success": False,
|
|
567
|
+
"error": "Internal error: context missing session or run after prepare_run",
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
# Type narrowing: assign to non-optional variables
|
|
571
|
+
child_session = context.session
|
|
572
|
+
agent_run = context.run
|
|
573
|
+
|
|
574
|
+
# Determine working directory
|
|
575
|
+
cwd = project_path or "."
|
|
576
|
+
|
|
577
|
+
# Ensure project_id is non-None for spawn calls
|
|
578
|
+
if project_id is None:
|
|
579
|
+
return {
|
|
580
|
+
"success": False,
|
|
581
|
+
"error": "project_id is required for spawning",
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
if mode == "terminal":
|
|
585
|
+
# Spawn in external terminal
|
|
586
|
+
terminal_spawner = TerminalSpawner()
|
|
587
|
+
terminal_result = terminal_spawner.spawn_agent(
|
|
588
|
+
cli=effective_provider, # claude, gemini, codex
|
|
589
|
+
cwd=cwd,
|
|
590
|
+
session_id=child_session.id,
|
|
591
|
+
parent_session_id=parent_session_id,
|
|
592
|
+
agent_run_id=agent_run.id,
|
|
593
|
+
project_id=project_id,
|
|
594
|
+
workflow_name=workflow,
|
|
595
|
+
agent_depth=child_session.agent_depth,
|
|
596
|
+
max_agent_depth=runner._child_session_manager.max_agent_depth,
|
|
597
|
+
terminal=terminal,
|
|
598
|
+
prompt=effective_prompt,
|
|
599
|
+
)
|
|
600
|
+
|
|
601
|
+
if not terminal_result.success:
|
|
602
|
+
return {
|
|
603
|
+
"success": False,
|
|
604
|
+
"error": terminal_result.error or terminal_result.message,
|
|
605
|
+
"run_id": agent_run.id,
|
|
606
|
+
"child_session_id": child_session.id,
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
# Register in running agents registry
|
|
610
|
+
running_agent = RunningAgent(
|
|
611
|
+
run_id=agent_run.id,
|
|
612
|
+
session_id=child_session.id,
|
|
613
|
+
parent_session_id=parent_session_id,
|
|
614
|
+
mode="terminal",
|
|
615
|
+
pid=terminal_result.pid,
|
|
616
|
+
terminal_type=terminal_result.terminal_type,
|
|
617
|
+
provider=effective_provider,
|
|
618
|
+
workflow_name=workflow,
|
|
619
|
+
worktree_id=worktree_id,
|
|
620
|
+
)
|
|
621
|
+
agent_registry.add(running_agent)
|
|
622
|
+
|
|
623
|
+
return {
|
|
624
|
+
"success": True,
|
|
625
|
+
"run_id": agent_run.id,
|
|
626
|
+
"child_session_id": child_session.id,
|
|
627
|
+
"status": "pending",
|
|
628
|
+
"message": f"Agent spawned in {terminal_result.terminal_type} (PID: {terminal_result.pid})",
|
|
629
|
+
"terminal_type": terminal_result.terminal_type,
|
|
630
|
+
"pid": terminal_result.pid,
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
elif mode == "embedded":
|
|
634
|
+
# Spawn with PTY for UI attachment
|
|
635
|
+
embedded_spawner = EmbeddedSpawner()
|
|
636
|
+
embedded_result = embedded_spawner.spawn_agent(
|
|
637
|
+
cli=effective_provider,
|
|
638
|
+
cwd=cwd,
|
|
639
|
+
session_id=child_session.id,
|
|
640
|
+
parent_session_id=parent_session_id,
|
|
641
|
+
agent_run_id=agent_run.id,
|
|
642
|
+
project_id=project_id,
|
|
643
|
+
workflow_name=workflow,
|
|
644
|
+
agent_depth=child_session.agent_depth,
|
|
645
|
+
max_agent_depth=runner._child_session_manager.max_agent_depth,
|
|
646
|
+
prompt=effective_prompt,
|
|
647
|
+
)
|
|
648
|
+
|
|
649
|
+
if not embedded_result.success:
|
|
650
|
+
return {
|
|
651
|
+
"success": False,
|
|
652
|
+
"error": embedded_result.error or embedded_result.message,
|
|
653
|
+
"run_id": agent_run.id,
|
|
654
|
+
"child_session_id": child_session.id,
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
# Register in running agents registry
|
|
658
|
+
running_agent = RunningAgent(
|
|
659
|
+
run_id=agent_run.id,
|
|
660
|
+
session_id=child_session.id,
|
|
661
|
+
parent_session_id=parent_session_id,
|
|
662
|
+
mode="embedded",
|
|
663
|
+
pid=embedded_result.pid,
|
|
664
|
+
master_fd=embedded_result.master_fd,
|
|
665
|
+
provider=effective_provider,
|
|
666
|
+
workflow_name=workflow,
|
|
667
|
+
worktree_id=worktree_id,
|
|
668
|
+
)
|
|
669
|
+
agent_registry.add(running_agent)
|
|
670
|
+
|
|
671
|
+
return {
|
|
672
|
+
"success": True,
|
|
673
|
+
"run_id": agent_run.id,
|
|
674
|
+
"child_session_id": child_session.id,
|
|
675
|
+
"status": "pending",
|
|
676
|
+
"message": f"Agent spawned with PTY (PID: {embedded_result.pid})",
|
|
677
|
+
"pid": embedded_result.pid,
|
|
678
|
+
"master_fd": embedded_result.master_fd,
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
else: # headless mode
|
|
682
|
+
# Spawn headless with output capture
|
|
683
|
+
headless_spawner = HeadlessSpawner()
|
|
684
|
+
headless_result = headless_spawner.spawn_agent(
|
|
685
|
+
cli=effective_provider,
|
|
686
|
+
cwd=cwd,
|
|
687
|
+
session_id=child_session.id,
|
|
688
|
+
parent_session_id=parent_session_id,
|
|
689
|
+
agent_run_id=agent_run.id,
|
|
690
|
+
project_id=project_id,
|
|
691
|
+
workflow_name=workflow,
|
|
692
|
+
agent_depth=child_session.agent_depth,
|
|
693
|
+
max_agent_depth=runner._child_session_manager.max_agent_depth,
|
|
694
|
+
prompt=effective_prompt,
|
|
695
|
+
)
|
|
696
|
+
|
|
697
|
+
if not headless_result.success:
|
|
698
|
+
return {
|
|
699
|
+
"success": False,
|
|
700
|
+
"error": headless_result.error or headless_result.message,
|
|
701
|
+
"run_id": agent_run.id,
|
|
702
|
+
"child_session_id": child_session.id,
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
# IMPORTANT: For headless mode with -p flag, hooks are NOT called.
|
|
706
|
+
# Claude's print mode bypasses the hook system entirely.
|
|
707
|
+
# We must manually mark the agent run as started.
|
|
708
|
+
try:
|
|
709
|
+
runner._run_storage.start(agent_run.id)
|
|
710
|
+
logger.info(f"Manually started headless agent run {agent_run.id}")
|
|
711
|
+
except Exception as e:
|
|
712
|
+
logger.warning(f"Failed to manually start agent run: {e}")
|
|
713
|
+
|
|
714
|
+
# Register in running agents registry
|
|
715
|
+
running_agent = RunningAgent(
|
|
716
|
+
run_id=agent_run.id,
|
|
717
|
+
session_id=child_session.id,
|
|
718
|
+
parent_session_id=parent_session_id,
|
|
719
|
+
mode="headless",
|
|
720
|
+
pid=headless_result.pid,
|
|
721
|
+
provider=effective_provider,
|
|
722
|
+
workflow_name=workflow,
|
|
723
|
+
worktree_id=worktree_id,
|
|
724
|
+
)
|
|
725
|
+
agent_registry.add(running_agent)
|
|
726
|
+
|
|
727
|
+
# Start background task to monitor process completion
|
|
728
|
+
import asyncio
|
|
729
|
+
|
|
730
|
+
async def monitor_headless_process() -> None:
|
|
731
|
+
"""Monitor headless process and update status on completion."""
|
|
732
|
+
try:
|
|
733
|
+
process = headless_result.process
|
|
734
|
+
if process is None:
|
|
735
|
+
return
|
|
736
|
+
|
|
737
|
+
# Wait for process to complete
|
|
738
|
+
loop = asyncio.get_running_loop()
|
|
739
|
+
return_code = await loop.run_in_executor(None, process.wait)
|
|
740
|
+
|
|
741
|
+
# Capture output
|
|
742
|
+
output = ""
|
|
743
|
+
if process.stdout:
|
|
744
|
+
output = process.stdout.read() or ""
|
|
745
|
+
|
|
746
|
+
# Update agent run status
|
|
747
|
+
if return_code == 0:
|
|
748
|
+
runner._run_storage.complete(
|
|
749
|
+
agent_run.id,
|
|
750
|
+
result=output,
|
|
751
|
+
tool_calls_count=0,
|
|
752
|
+
turns_used=1,
|
|
753
|
+
)
|
|
754
|
+
logger.info(f"Headless agent {agent_run.id} completed successfully")
|
|
755
|
+
else:
|
|
756
|
+
runner._run_storage.fail(
|
|
757
|
+
agent_run.id, error=f"Process exited with code {return_code}"
|
|
758
|
+
)
|
|
759
|
+
logger.warning(
|
|
760
|
+
f"Headless agent {agent_run.id} failed with code {return_code}"
|
|
761
|
+
)
|
|
762
|
+
|
|
763
|
+
# Remove from running agents registry
|
|
764
|
+
agent_registry.remove(agent_run.id)
|
|
765
|
+
|
|
766
|
+
except Exception as e:
|
|
767
|
+
logger.error(f"Error monitoring headless process: {e}")
|
|
768
|
+
try:
|
|
769
|
+
runner._run_storage.fail(agent_run.id, error=str(e))
|
|
770
|
+
agent_registry.remove(agent_run.id)
|
|
771
|
+
except Exception:
|
|
772
|
+
pass # nosec B110 - best-effort cleanup during error handling
|
|
773
|
+
|
|
774
|
+
# Schedule monitoring task and store reference to prevent GC
|
|
775
|
+
running_agent.monitor_task = asyncio.create_task(monitor_headless_process())
|
|
776
|
+
|
|
777
|
+
return {
|
|
778
|
+
"success": True,
|
|
779
|
+
"run_id": agent_run.id,
|
|
780
|
+
"child_session_id": child_session.id,
|
|
781
|
+
"status": "running", # Now "running" since we manually started it
|
|
782
|
+
"message": f"Agent spawned headless (PID: {headless_result.pid})",
|
|
783
|
+
"pid": headless_result.pid,
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
@registry.tool(
|
|
787
|
+
name="get_agent_result",
|
|
788
|
+
description="Get the result of a completed agent run.",
|
|
789
|
+
)
|
|
790
|
+
async def get_agent_result(run_id: str) -> dict[str, Any]:
|
|
791
|
+
"""
|
|
792
|
+
Get the result of an agent run.
|
|
793
|
+
|
|
794
|
+
Args:
|
|
795
|
+
run_id: The agent run ID.
|
|
796
|
+
|
|
797
|
+
Returns:
|
|
798
|
+
Dict with run details including status, result, error.
|
|
799
|
+
"""
|
|
800
|
+
run = runner.get_run(run_id)
|
|
801
|
+
if not run:
|
|
802
|
+
return {
|
|
803
|
+
"success": False,
|
|
804
|
+
"error": f"Agent run {run_id} not found",
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
return {
|
|
808
|
+
"success": True,
|
|
809
|
+
"run_id": run.id,
|
|
810
|
+
"status": run.status,
|
|
811
|
+
"result": run.result,
|
|
812
|
+
"error": run.error,
|
|
813
|
+
"provider": run.provider,
|
|
814
|
+
"model": run.model,
|
|
815
|
+
"prompt": run.prompt,
|
|
816
|
+
"tool_calls_count": run.tool_calls_count,
|
|
817
|
+
"turns_used": run.turns_used,
|
|
818
|
+
"started_at": run.started_at,
|
|
819
|
+
"completed_at": run.completed_at,
|
|
820
|
+
"child_session_id": run.child_session_id,
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
@registry.tool(
|
|
824
|
+
name="list_agents",
|
|
825
|
+
description="List agent runs for a session.",
|
|
826
|
+
)
|
|
827
|
+
async def list_agents(
|
|
828
|
+
parent_session_id: str,
|
|
829
|
+
status: str | None = None,
|
|
830
|
+
limit: int = 20,
|
|
831
|
+
) -> dict[str, Any]:
|
|
832
|
+
"""
|
|
833
|
+
List agent runs for a session.
|
|
834
|
+
|
|
835
|
+
Args:
|
|
836
|
+
parent_session_id: The parent session ID.
|
|
837
|
+
status: Optional status filter (pending, running, success, error, timeout, cancelled).
|
|
838
|
+
limit: Maximum results (default: 20).
|
|
839
|
+
|
|
840
|
+
Returns:
|
|
841
|
+
Dict with list of agent runs.
|
|
842
|
+
"""
|
|
843
|
+
runs = runner.list_runs(parent_session_id, status=status, limit=limit)
|
|
844
|
+
|
|
845
|
+
return {
|
|
846
|
+
"success": True,
|
|
847
|
+
"runs": [
|
|
848
|
+
{
|
|
849
|
+
"id": run.id,
|
|
850
|
+
"status": run.status,
|
|
851
|
+
"provider": run.provider,
|
|
852
|
+
"model": run.model,
|
|
853
|
+
"workflow_name": run.workflow_name,
|
|
854
|
+
"prompt": run.prompt[:100] + "..." if len(run.prompt) > 100 else run.prompt,
|
|
855
|
+
"started_at": run.started_at,
|
|
856
|
+
"completed_at": run.completed_at,
|
|
857
|
+
}
|
|
858
|
+
for run in runs
|
|
859
|
+
],
|
|
860
|
+
"count": len(runs),
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
@registry.tool(
|
|
864
|
+
name="stop_agent",
|
|
865
|
+
description="Stop a running agent (marks as cancelled in DB, does not kill process).",
|
|
866
|
+
)
|
|
867
|
+
async def stop_agent(run_id: str) -> dict[str, Any]:
|
|
868
|
+
"""
|
|
869
|
+
Stop a running agent by marking it as cancelled.
|
|
870
|
+
|
|
871
|
+
This only updates the database status - it does NOT kill the actual process.
|
|
872
|
+
Use kill_agent to terminate the process.
|
|
873
|
+
|
|
874
|
+
Args:
|
|
875
|
+
run_id: The agent run ID to stop.
|
|
876
|
+
|
|
877
|
+
Returns:
|
|
878
|
+
Dict with success status.
|
|
879
|
+
"""
|
|
880
|
+
success = runner.cancel_run(run_id)
|
|
881
|
+
if success:
|
|
882
|
+
# Also remove from running agents registry
|
|
883
|
+
agent_registry.remove(run_id)
|
|
884
|
+
return {
|
|
885
|
+
"success": True,
|
|
886
|
+
"message": f"Agent run {run_id} stopped",
|
|
887
|
+
}
|
|
888
|
+
else:
|
|
889
|
+
run = runner.get_run(run_id)
|
|
890
|
+
if not run:
|
|
891
|
+
return {
|
|
892
|
+
"success": False,
|
|
893
|
+
"error": f"Agent run {run_id} not found",
|
|
894
|
+
}
|
|
895
|
+
else:
|
|
896
|
+
return {
|
|
897
|
+
"success": False,
|
|
898
|
+
"error": f"Cannot stop agent in status: {run.status}",
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
@registry.tool(
|
|
902
|
+
name="kill_agent",
|
|
903
|
+
description="Kill a running agent process. Use stop=True to also end its workflow.",
|
|
904
|
+
)
|
|
905
|
+
async def kill_agent(
|
|
906
|
+
run_id: str,
|
|
907
|
+
signal: str = "TERM",
|
|
908
|
+
force: bool = False,
|
|
909
|
+
stop: bool = False,
|
|
910
|
+
) -> dict[str, Any]:
|
|
911
|
+
"""
|
|
912
|
+
Kill a running agent process.
|
|
913
|
+
|
|
914
|
+
This actually terminates the process (unlike stop_agent which only updates DB).
|
|
915
|
+
|
|
916
|
+
Args:
|
|
917
|
+
run_id: Agent run ID
|
|
918
|
+
signal: Signal to send (TERM, KILL, INT, HUP, QUIT). Default: TERM
|
|
919
|
+
force: Use SIGKILL immediately (equivalent to signal="KILL")
|
|
920
|
+
stop: Also end the agent's workflow (prevents restart)
|
|
921
|
+
|
|
922
|
+
Returns:
|
|
923
|
+
Dict with success status and kill details.
|
|
924
|
+
"""
|
|
925
|
+
if force:
|
|
926
|
+
signal = "KILL"
|
|
927
|
+
|
|
928
|
+
# Validate signal against allowlist to prevent injection
|
|
929
|
+
signal = signal.upper()
|
|
930
|
+
allowed_signals = {"TERM", "KILL", "INT", "HUP", "QUIT"}
|
|
931
|
+
if signal not in allowed_signals:
|
|
932
|
+
return {
|
|
933
|
+
"success": False,
|
|
934
|
+
"error": f"Invalid signal '{signal}'. Allowed: {', '.join(sorted(allowed_signals))}",
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
# Get agent info before killing (for session_id)
|
|
938
|
+
agent = agent_registry.get(run_id)
|
|
939
|
+
session_id = agent.session_id if agent else None
|
|
940
|
+
|
|
941
|
+
# Kill via registry (run in thread to avoid blocking event loop)
|
|
942
|
+
import asyncio
|
|
943
|
+
|
|
944
|
+
result = await asyncio.to_thread(agent_registry.kill, run_id, signal_name=signal)
|
|
945
|
+
|
|
946
|
+
if result.get("success"):
|
|
947
|
+
# Update database status
|
|
948
|
+
runner.cancel_run(run_id)
|
|
949
|
+
|
|
950
|
+
# Optionally end the workflow to prevent restart
|
|
951
|
+
if stop and session_id:
|
|
952
|
+
if workflow_state_manager is not None:
|
|
953
|
+
try:
|
|
954
|
+
workflow_state_manager.delete_state(session_id)
|
|
955
|
+
result["workflow_stopped"] = True
|
|
956
|
+
except Exception as e:
|
|
957
|
+
result["workflow_stop_error"] = str(e)
|
|
958
|
+
else:
|
|
959
|
+
result["workflow_stop_error"] = "WorkflowStateManager not configured"
|
|
960
|
+
|
|
961
|
+
return result
|
|
962
|
+
|
|
963
|
+
@registry.tool(
|
|
964
|
+
name="can_spawn_agent",
|
|
965
|
+
description="Check if an agent can be spawned from the current session.",
|
|
966
|
+
)
|
|
967
|
+
async def can_spawn_agent(parent_session_id: str) -> dict[str, Any]:
|
|
968
|
+
"""
|
|
969
|
+
Check if an agent can be spawned from the given session.
|
|
970
|
+
|
|
971
|
+
This checks the agent depth limit to prevent infinite nesting.
|
|
972
|
+
|
|
973
|
+
Args:
|
|
974
|
+
parent_session_id: The session that would spawn the agent.
|
|
975
|
+
|
|
976
|
+
Returns:
|
|
977
|
+
Dict with can_spawn boolean and reason.
|
|
978
|
+
"""
|
|
979
|
+
can_spawn, reason, _parent_depth = runner.can_spawn(parent_session_id)
|
|
980
|
+
return {
|
|
981
|
+
"can_spawn": can_spawn,
|
|
982
|
+
"reason": reason,
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
@registry.tool(
|
|
986
|
+
name="list_running_agents",
|
|
987
|
+
description="List all currently running agents (in-memory process state).",
|
|
988
|
+
)
|
|
989
|
+
async def list_running_agents(
|
|
990
|
+
parent_session_id: str | None = None,
|
|
991
|
+
mode: str | None = None,
|
|
992
|
+
) -> dict[str, Any]:
|
|
993
|
+
"""
|
|
994
|
+
List all currently running agents.
|
|
995
|
+
|
|
996
|
+
This returns in-memory process state for agents that are actively running,
|
|
997
|
+
including PIDs and process handles not stored in the database.
|
|
998
|
+
|
|
999
|
+
Args:
|
|
1000
|
+
parent_session_id: Optional filter by parent session.
|
|
1001
|
+
mode: Optional filter by execution mode (terminal, embedded, headless).
|
|
1002
|
+
|
|
1003
|
+
Returns:
|
|
1004
|
+
Dict with list of running agents.
|
|
1005
|
+
"""
|
|
1006
|
+
if parent_session_id:
|
|
1007
|
+
agents = agent_registry.list_by_parent(parent_session_id)
|
|
1008
|
+
elif mode:
|
|
1009
|
+
agents = agent_registry.list_by_mode(mode)
|
|
1010
|
+
else:
|
|
1011
|
+
agents = agent_registry.list_all()
|
|
1012
|
+
|
|
1013
|
+
return {
|
|
1014
|
+
"success": True,
|
|
1015
|
+
"agents": [agent.to_dict() for agent in agents],
|
|
1016
|
+
"count": len(agents),
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
@registry.tool(
|
|
1020
|
+
name="get_running_agent",
|
|
1021
|
+
description="Get in-memory process state for a running agent.",
|
|
1022
|
+
)
|
|
1023
|
+
async def get_running_agent(run_id: str) -> dict[str, Any]:
|
|
1024
|
+
"""
|
|
1025
|
+
Get the in-memory state for a running agent.
|
|
1026
|
+
|
|
1027
|
+
This returns process information like PID and PTY fd that aren't
|
|
1028
|
+
stored in the database.
|
|
1029
|
+
|
|
1030
|
+
Args:
|
|
1031
|
+
run_id: The agent run ID.
|
|
1032
|
+
|
|
1033
|
+
Returns:
|
|
1034
|
+
Dict with running agent details.
|
|
1035
|
+
"""
|
|
1036
|
+
agent = agent_registry.get(run_id)
|
|
1037
|
+
if not agent:
|
|
1038
|
+
return {
|
|
1039
|
+
"success": False,
|
|
1040
|
+
"error": f"No running agent found with ID {run_id}",
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
return {
|
|
1044
|
+
"success": True,
|
|
1045
|
+
"agent": agent.to_dict(),
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
@registry.tool(
|
|
1049
|
+
name="unregister_agent",
|
|
1050
|
+
description="Remove an agent from the in-memory running registry (internal use).",
|
|
1051
|
+
)
|
|
1052
|
+
async def unregister_agent(run_id: str) -> dict[str, Any]:
|
|
1053
|
+
"""
|
|
1054
|
+
Remove an agent from the running registry.
|
|
1055
|
+
|
|
1056
|
+
This is typically called automatically when a session ends,
|
|
1057
|
+
but can be called manually for cleanup.
|
|
1058
|
+
|
|
1059
|
+
Args:
|
|
1060
|
+
run_id: The agent run ID to unregister.
|
|
1061
|
+
|
|
1062
|
+
Returns:
|
|
1063
|
+
Dict with success status.
|
|
1064
|
+
"""
|
|
1065
|
+
removed = agent_registry.remove(run_id)
|
|
1066
|
+
if removed:
|
|
1067
|
+
return {
|
|
1068
|
+
"success": True,
|
|
1069
|
+
"message": f"Unregistered agent {run_id}",
|
|
1070
|
+
}
|
|
1071
|
+
else:
|
|
1072
|
+
return {
|
|
1073
|
+
"success": False,
|
|
1074
|
+
"error": f"No running agent found with ID {run_id}",
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
@registry.tool(
|
|
1078
|
+
name="running_agent_stats",
|
|
1079
|
+
description="Get statistics about running agents.",
|
|
1080
|
+
)
|
|
1081
|
+
async def running_agent_stats() -> dict[str, Any]:
|
|
1082
|
+
"""
|
|
1083
|
+
Get statistics about running agents.
|
|
1084
|
+
|
|
1085
|
+
Returns:
|
|
1086
|
+
Dict with counts by mode and parent.
|
|
1087
|
+
"""
|
|
1088
|
+
all_agents = agent_registry.list_all()
|
|
1089
|
+
by_mode: dict[str, int] = {}
|
|
1090
|
+
by_parent: dict[str, int] = {}
|
|
1091
|
+
|
|
1092
|
+
for agent in all_agents:
|
|
1093
|
+
by_mode[agent.mode] = by_mode.get(agent.mode, 0) + 1
|
|
1094
|
+
by_parent[agent.parent_session_id] = by_parent.get(agent.parent_session_id, 0) + 1
|
|
1095
|
+
|
|
1096
|
+
return {
|
|
1097
|
+
"success": True,
|
|
1098
|
+
"total": len(all_agents),
|
|
1099
|
+
"by_mode": by_mode,
|
|
1100
|
+
"by_parent_count": len(by_parent),
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
return registry
|