## Starts and watches background tasks: separate agent runs working the same ## project at the same time as the chat. ## ## `start` returns an id straight away rather than the result — that is the ## whole point. The run keeps going between turns, so the chat can answer the ## user while a refactor grinds through forty files, then read the outcome ## with `status` when it matters. ## ## The cost of that is that nothing is awaited: a task the agent never checks ## on still runs, still spends tokens, and still edits files. class_name ClaudeGodotToolTask extends ClaudeGodotTool const DEFAULT_STEPS := 20 const MAX_STEPS := 60 func definition() -> Dictionary: return { "name": "task", "description": "Run a background task: a second agent with the same tools and model, working this project in parallel with the chat. 'start' returns a task id immediately and the run continues between your turns — use it for work that is well defined and slow (sweeping a change across many files, reading through a large directory, a long remote build) so you can keep talking to the user meanwhile. Check on it with 'status', which returns the trail so far and the result once it is finished. A background task has no user: it cannot ask anything, so the brief must be self-contained and must say what finishing looks like. It cannot start tasks of its own, restart the editor, or touch chat sessions. Do not use it for something short — a file you could just read is cheaper read directly. Limit of %d at once." % ClaudeGodotBackgroundTasks.MAX_CONCURRENT, "input_schema": { "type": "object", "properties": { "action": { "type": "string", "enum": ["start", "status", "list", "stop"], "description": "start a task, read one's status, list them all, or stop a running one." }, "brief": { "type": "string", "description": "For 'start': the whole instruction, written for someone who cannot ask you anything. Say what to do, which files or directories it concerns, and what a finished result looks like." }, "label": { "type": "string", "description": "For 'start': a few words naming the task, shown in the dock. Optional." }, "max_steps": { "type": "integer", "description": "For 'start': cap on model calls for this task, default %d, at most %d." % [DEFAULT_STEPS, MAX_STEPS] }, "task_id": { "type": "string", "description": "For 'status' and 'stop': the id returned by 'start'. Omit on 'status' to get every task at a glance." } }, "required": ["action"] } } func execute(input: Dictionary) -> Dictionary: match str(input.get("action", "")): "start": return _start(input) "status": return _status(input) "list": return _list() "stop": return _stop(input) "": return {"is_error": true, "content": "Missing 'action'. Use start, status, list or stop."} _: return {"is_error": true, "content": "Unknown action '%s'. Use start, status, list or stop." % input["action"]} func _start(input: Dictionary) -> Dictionary: var brief := str(input.get("brief", "")).strip_edges() if brief == "": return {"is_error": true, "content": "Missing 'brief'. A background task can't ask what you meant, so the brief has to carry the whole instruction."} var step_cap: int = clampi(int(input.get("max_steps", DEFAULT_STEPS)), 1, MAX_STEPS) # The run borrows the chat's provider, model, key and tools: a task is # meant to be the same agent working elsewhere, not a cheaper one. var host := ClaudeGodotBackgroundTasks.host() if host == null or not host.has_method("background_task_context"): return {"is_error": true, "content": "Background tasks aren't available: no agent is running."} var started := ClaudeGodotBackgroundTasks.start( brief, str(input.get("label", "")).strip_edges(), host.call("background_task_context"), step_cap, ) if started.has("error"): return {"is_error": true, "content": started["error"]} var run = started["run"] return {"content": "Started background task %s (%s), up to %d steps. It runs on its own from here — carry on, and read it later with task/status task_id=%s." % [ run.id, run.label, step_cap, run.id, ]} func _status(input: Dictionary) -> Dictionary: var id := str(input.get("task_id", "")).strip_edges() if id == "": return _list() var run = ClaudeGodotBackgroundTasks.find(id) if run == null: return {"is_error": true, "content": "No background task %s. %s" % [id, _known()]} return {"content": run.report(true)} func _list() -> Dictionary: var lines := ClaudeGodotBackgroundTasks.summary_lines() if lines == "": return {"content": "No background tasks have been started this session."} return {"content": "Background tasks:\n%s\n\nRead one in full with task/status and its id." % lines} func _stop(input: Dictionary) -> Dictionary: var id := str(input.get("task_id", "")).strip_edges() if id == "": return {"is_error": true, "content": "Missing 'task_id'. %s" % _known()} var run = ClaudeGodotBackgroundTasks.find(id) if run == null: return {"is_error": true, "content": "No background task %s. %s" % [id, _known()]} if not run.is_running(): return {"content": "Task %s already finished (%s)." % [id, run.state_name()]} run.stop() # The run unwinds on its next await, so the state here is still "running"; # what is guaranteed is that no further step starts. return {"content": "Asked task %s to stop. It stops after the call it is in the middle of; anything it already wrote stays written." % id} func _known() -> String: var lines := ClaudeGodotBackgroundTasks.summary_lines() return "No tasks this session." if lines == "" else "Tasks this session:\n%s" % lines