Register a brand-new background agent on the runtime registry. Idempotent enough to be safe under spawn-then-resume races; if an entry already exists at ``agent_id`` it is replaced (the resume path explicitly wants this so the new event-loop wiring takes over). Returns the just
(
*,
agent_id: str,
description: str,
prompt: str,
agent_type: str,
selected_agent: Any = None,
model: str | None = None,
tool_use_id: str | None = None,
registry: "RuntimeTaskRegistry",
)
| 108 | |
| 109 | |
| 110 | def register_async_agent( |
| 111 | *, |
| 112 | agent_id: str, |
| 113 | description: str, |
| 114 | prompt: str, |
| 115 | agent_type: str, |
| 116 | selected_agent: Any = None, |
| 117 | model: str | None = None, |
| 118 | tool_use_id: str | None = None, |
| 119 | registry: "RuntimeTaskRegistry", |
| 120 | ) -> LocalAgentTaskState: |
| 121 | """Register a brand-new background agent on the runtime registry. |
| 122 | |
| 123 | Idempotent enough to be safe under spawn-then-resume races; if an |
| 124 | entry already exists at ``agent_id`` it is replaced (the resume |
| 125 | path explicitly wants this so the new event-loop wiring takes |
| 126 | over). |
| 127 | |
| 128 | Returns the just-registered state so callers can hold a reference |
| 129 | without re-fetching. |
| 130 | |
| 131 | The transcript path is computed (and the parent dir created) before |
| 132 | registration so any consumer reading ``state.output_file`` can rely |
| 133 | on the path being valid filesystem-side. ``output_file`` carries |
| 134 | the JSONL transcript per WI-2.2 — Phase 3 / WI-3.1 cites it in the |
| 135 | notification XML, Phase 7 / WI-7.4 reads it for auto-resume. |
| 136 | """ |
| 137 | # Local import — transcript depends on a CSPRNG-validated agent_id, |
| 138 | # registered task system imports back through here, defer to keep |
| 139 | # the cycle untangled. |
| 140 | from src.agent.transcript import get_agent_transcript_path |
| 141 | |
| 142 | output_file = get_agent_transcript_path(agent_id) |
| 143 | |
| 144 | state = LocalAgentTaskState( |
| 145 | id=agent_id, |
| 146 | type="local_agent", |
| 147 | status="running", |
| 148 | description=description, |
| 149 | start_time=time.time(), |
| 150 | output_file=output_file, |
| 151 | agent_id=agent_id, |
| 152 | agent_type=agent_type, |
| 153 | prompt=prompt, |
| 154 | selected_agent=selected_agent, |
| 155 | model=model, |
| 156 | tool_use_id=tool_use_id, |
| 157 | is_backgrounded=True, |
| 158 | ) |
| 159 | registry.upsert(state) |
| 160 | return state |
| 161 | |
| 162 | |
| 163 | def queue_pending_message( |