| 294 | } |
| 295 | |
| 296 | func (rt *AgentTaskRuntime) SpawnWorker(ctx context.Context, cfg config.Config, registry *coretools.ToolRegistry, definition AgentDef, parentState *AgentState, description, prompt, agentType, modelOverride, workerLabel string, reusable bool, parentScopeID, parentTaskID string, extraContext coretools.ExecutionContext) *AgentTaskRecord { |
| 297 | now := nowSeconds() |
| 298 | taskID := "subagent-" + agentType + "-" + uuid.NewString()[:8] |
| 299 | record := &AgentTaskRecord{ |
| 300 | TaskID: taskID, ParentTaskID: parentTaskID, ParentScopeID: parentScopeID, |
| 301 | WorkerLabel: workerLabel, Description: description, AgentType: agentType, |
| 302 | Reusable: reusable, Status: "queued", CreatedAt: now, UpdatedAt: now, |
| 303 | } |
| 304 | childCtx, cancel := context.WithCancel(context.Background()) |
| 305 | workerState := BuildSubagentState(parentState, definition.PermissionMode) |
| 306 | workerExtraContext := coretools.ExecutionContext{} |
| 307 | for key, value := range extraContext { |
| 308 | workerExtraContext[key] = value |
| 309 | } |
| 310 | workerExtraContext["allowed_read_roots"] = []string{cfg.CWD} |
| 311 | workerExtraContext["task_runtime"] = rt |
| 312 | workerExtraContext["scope_id"] = taskID |
| 313 | workerExtraContext["current_task_id"] = taskID |
| 314 | workerExtraContext["parent_task_id"] = parentTaskID |
| 315 | workerExtraContext["parent_scope_id"] = parentScopeID |
| 316 | workerExtraContext["current_agent_type"] = agentType |
| 317 | workerExtraContext["_drain_pending_notifications"] = rt.DrainPendingNotifications |
| 318 | workerExtraContext["abort_check"] = func() bool { return childCtx.Err() != nil } |
| 319 | rt.mu.Lock() |
| 320 | rt.registerRecordLocked(record, true) |
| 321 | rt.workers[taskID] = cancel |
| 322 | rt.workerSpecs[taskID] = workerSpec{ |
| 323 | cfg: cfg, registry: registry, definition: definition, parentState: &workerState, |
| 324 | agentType: agentType, modelOverride: modelOverride, extraContext: workerExtraContext, |
| 325 | } |
| 326 | rt.mu.Unlock() |
| 327 | go rt.runWorker(childCtx, taskID, prompt) |
| 328 | return record |
| 329 | } |
| 330 | |
| 331 | func (rt *AgentTaskRuntime) SendMessage(taskID, scopeID, prompt string) any { |
| 332 | rt.mu.Lock() |