Agent task execution
Agent task execution runs a configured AI agent CLI against a project repository. A task supplies a validated prompt and task context. Semaphore prepares a short-lived agent home, gives the agent approved MCP access and task secrets, streams structured progress into the task log, and exposes that progress through the API and web console.
Agent application model
An agent application is a template application that runs an AI coding agent rather than a script or infrastructure tool. The supported applications are Claude Code and Codex. Both use the same task model and log contract, while each CLI receives its own launch settings. db/Template.go• L38-42 and db_lib/AppFactory.go• L8-39.
Application configuration
The template selects the agent application and stores its agent settings in task_params. The template prompt is the main instruction. A template can also allow a task creator to replace that prompt. Agent templates use an MCP inventory, which defines the remote tools available to the agent. db/Template.go• L307-313 and db/Template.go• L44-58.
The task carries agent-specific parameters in AgentTaskParams. At present, its main task-level value is an optional replacement prompt. The API validates that a prompt exists and that a replacement is allowed before it stores the task. db/Task.go• L30-34, db/Template.go• L449-517, and api/…/tasks_agent_test.go• L32-62.
References: db/Template.go• L307-313 db/Template.go• L449-517 db/Task.go• L30-34 api/…/tasks_agent_test.go• L14-62
MCP access
MCP, or Model Context Protocol, connects the agent to tools such as project services. Enabled MCP servers are passed to the selected CLI. Their URLs and token variable names go into the agent configuration. Token values stay in the process environment and are never placed in command arguments or configuration files. db_lib/AgentLaunch.go• L14-16 and db_lib/AgentLaunch.go• L105-143.
The launch permits the tools from the selected inventory without asking for an interactive approval. This matters because agent tasks run without a person at the terminal. The task log reports the servers and their connection status when the CLI provides that information. db_lib/AgentLaunch.go• L161-199 and pkg/…/agentlog.go• L63-67.
References: db_lib/AgentLaunch.go• L105-143 db_lib/AgentLaunch.go• L161-199 pkg/…/agentlog.go• L63-67
Prompt model
The prompt is chosen before the agent process starts. A template prompt may read task variables through expressions. A prompt supplied directly by a task is treated as literal text, so a task-level override cannot use the template's variable and secret expansion path. db/Template.go• L449-517.
Prompt sources
Prompt values come from the same project environments used by other task types. The system separates ordinary variables, environment variables, and secrets. Survey values follow their declared target. An unanswered survey value is available to the template as an empty string. db_lib/AgentPrompt.go• L12-96.
This separation lets a template build a useful instruction from task context while keeping secret values eligible for log masking. Secret values that the agent can read are added to the launch mask before execution. db_lib/AgentPrompt.go• L99-108 and db_lib/AgentApp.go• L89-115.
References: db_lib/AgentPrompt.go• L12-108 db_lib/AgentApp.go• L84-115
Prompt expansion
The API checks the prompt shape when it creates a task. During execution, the local executor builds the prompt value set and the agent application expands the template prompt. Expansion returns both the text sent to standard input and the secret values that must be hidden from the log. services/…/local_executor.go• L1165-1191 and db/Template.go• L474-517.
The agent receives the final prompt on standard input. Template and task command arguments are kept separate from the prompt. Secret variables and extra task variables are not added to agent command arguments because process arguments can be visible to other processes. db_lib/AgentApp.go• L120-135 and services/…/local_executor.go• L1177-1190.
References: services/…/local_executor.go• L1165-1191 db/Template.go• L474-517 db_lib/AgentApp.go• L120-135
Launch lifecycle
The local executor owns the task lifecycle. It prepares the repository and task context, runs the selected agent app, then removes temporary resources. The same preparation data can support a later executor that dispatches the run elsewhere. services/…/local_executor.go• L837-878 and services/…/local_executor.go• L1057-1066.
flowchart TD
A[API creates agent task] --> B[Prepare task context]
B --> C[Checkout repository]
C --> D[Build prompt and launch settings]
D --> E[Create temporary agent home]
E --> F[Run Claude Code or Codex]
F --> G[Convert CLI output to task log records]
G --> H[Return task result]
H --> I[Cleanup keys files and agent home]
Preparation phase
Preparation resolves the repository, inventory, environment, parameters, and command arguments. For an agent task, it also creates the prompt value set. The agent receives only the template and task arguments from this phase. services/…/local_executor.go• L1066-1191.
The repository or its selected subdirectory becomes the agent's working directory. The agent configuration uses a separate temporary home under the project task directory. This keeps run-specific CLI state apart from the checked-out project files. db_lib/AgentApp.go• L57-76 and db_lib/AgentApp.go• L98-106.
References: services/…/local_executor.go• L1066-1191 db_lib/AgentApp.go• L57-76 db_lib/AgentApp.go• L98-106
Execution phase
The launch description gives the executor a command, arguments, environment entries, configuration files, and MCP token entries. The executor writes the configuration files with restricted permissions, places the prompt on standard input, and starts the CLI in the repository directory. Claude Code and Codex each report progress in a machine-readable stream. db_lib/AgentLaunch.go• L18-64, db_lib/AgentApp.go• L106-147, and db_lib/AgentLaunch.go• L146-221.
The launch combines the task environment with agent-owned settings. Agent-owned values are added last so task input cannot redirect the agent configuration or replace a required token. API key names are checked before the process starts, but the CLI remains responsible for reporting an authentication failure. db_lib/AgentApp.go• L124-133 and db_lib/AgentApp.go• L188-199.
References: db_lib/AgentLaunch.go• L18-64 db_lib/AgentApp.go• L106-147 db_lib/AgentApp.go• L124-133 db_lib/AgentLaunch.go• L146-221
Cleanup phase
Cleanup runs after the app finishes or preparation fails part way through. It removes installed task credentials, temporary task files, captured outputs, and the agent configuration directory. A stopped task does not continue to collect agent output after cancellation. services/…/local_executor.go• L851-878 and services/…/local_executor.go• L1256-1271.
References: services/…/local_executor.go• L851-878 services/…/local_executor.go• L1256-1271
Task log records
Agent progress uses one JSON record per task-log line. The record starts with {"@agent":"..."} so clients can distinguish agent events from normal task output. The shared event model covers run setup, agent text, reasoning, tool calls, tool results, retries, and turn completion. pkg/…/agentlog.go• L19-36 and pkg/…/agentlog.go• L69-108.
Structured event types
The event stream represents the relationship between an agent and its tools. A tool call has an ID. Its later result uses that ID. A subagent event carries the tool ID that started the subagent. A completion event carries the outcome and the usage values reported by the CLI. pkg/…/agentlog.go• L69-108.
| Event | Purpose |
|---|---|
init | Identifies the model and MCP server status. |
text | Shows agent output. |
thinking | Shows reasoning when the CLI reports it. |
tool | Shows a tool name, ID, and bounded arguments. |
result | Shows the matching tool result and error state. |
retry | Shows a failed API request that will be retried. |
done | Closes a turn and reports errors, duration, tokens, or cost. |
Arguments and results are masked and bounded before they enter the log. This keeps secrets out of records and prevents a large file or command result from becoming the task log. pkg/…/agentlog.go• L38-60 and pkg/…/agentlog.go• L148-190.
References: pkg/…/agentlog.go• L19-108 pkg/…/agentlog.go• L148-190
Output conversion
Claude Code and Codex have different CLI event formats, but both are converted to the shared agent event format. Plain CLI lines that are not recognized events remain ordinary task-log lines. This preserves connection errors and other process messages instead of hiding them. db_lib/AgentOutput.go• L209-244 and db_lib/AgentOutputCodex.go• L97-139.
The conversion also handles streamed text and tool results. It holds incomplete text until it can apply masking safely. When output ends, it flushes the remaining text so the task log does not lose the final line. db_lib/AgentOutput.go• L246-283.
References: db_lib/AgentOutput.go• L12-20 db_lib/AgentOutput.go• L209-246 db_lib/AgentOutput.go• L268-283 db_lib/AgentOutputCodex.go• L97-139
API and web console
Agent tasks use the normal task API. The task resource identifies the template and its parameters. The task output resource returns stored log records as JSON. The raw-output resource converts agent events to readable plain text for clients that do not render structured records. api/…/tasks.go• L367-395 and api/…/tasks.go• L397-424.
API representations
The API keeps structured records as the source form. A caller that requests raw output gets a readable projection: tool calls become command-like lines, results become indented text, and non-agent records stay ordinary output. ANSI control codes are removed from this projection. api/…/tasks.go• L384-395 and pkg/…/agentlog.go• L139-145.
The two output views serve different clients. JSON output preserves tool IDs and fields needed to join calls with results. Raw output is suited to downloads, command-line display, and simple log readers. The routes are available under the project task management API. api/router.go• L601-602.
References: api/…/tasks.go• L367-424 pkg/…/agentlog.go• L124-145 api/router.go• L601-602
Console transcript
The web console turns incoming task records into a transcript rather than rendering every record as a separate row. AgentLog joins a tool result to its call, tracks subagent names, updates repeated tool calls, and keeps the final completion row as the run summary. web/…/agentLog.js• L206-226 and web/…/agentLog.js• L278-393.
The agent row shows text, tool arguments and results, MCP status, retries, subagents, truncation notices, and final usage details. Values are displayed as text. The console does not treat agent or tool content as HTML. web/…/TaskLogAgentRecord.vue• L1-6 and web/…/TaskLogAgentRecord.vue• L21-151.
The task log view feeds stored and live records into the same transcript builder. It can therefore update tool rows as results arrive while keeping normal task output as separate rows. web/…/TaskLogView.vue• L318-323 and web/…/TaskLogView.vue• L491-507.
References: web/…/agentLog.js• L28-59 web/…/agentLog.js• L206-393 web/…/TaskLogAgentRecord.vue• L1-151 web/…/TaskLogView.vue• L318-323 web/…/TaskLogView.vue• L491-507
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the Claude Code and Codex launch descriptions mapped to the same
agentlog.Eventcontract. - Keep secret collection aligned with every place a secret can reach the prompt, process environment, MCP token environment, or tool result.
- Keep the
@agentevent fields aligned with both web/…/agentLog.js and the plain-text API projection. - Keep task prompt validation aligned with the rule that task-level prompts are literal unless the template allows replacement.
- Keep the MCP inventory type restriction aligned with the agent application selection.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| An agent template uses a non-MCP inventory | The task does not start | Validate the template inventory type before launch. |
| An enabled MCP server requires a token that was not delivered | Launch creation fails instead of starting without that server's access | Require the token during launch construction. |
| A task sends a replacement prompt without template permission | The API rejects the task and does not store it | Validate allow_override_prompt before task creation. |
| No configured API key reaches the process | The CLI reports an authentication failure | Log the expected key names before launch and configure the needed secret. |
| A tool result arrives without its call record | The console shows a separate result row | Preserve the result as an orphan event rather than dropping it. |
| Agent output contains a secret or oversized tool value | Sensitive text could leak or the log could grow without bound | Mask secrets and cap event arguments and results before storage. |
Key files
| File | Why to read it |
|---|---|
| db/Template.go | Defines agent template settings, prompt selection, validation, and inventory rules. |
| db/Task.go | Defines the task-level agent parameter shape. |
| services/…/local_executor.go | Shows how preparation passes task context and prompt values into the app lifecycle. |
| db_lib/AgentApp.go | Shows the working directory, temporary agent home, environment ordering, process start, and cleanup boundary. |
| db_lib/AgentLaunch.go | Describes the common launch contract and the Claude Code and Codex launch variants. |
| db_lib/AgentOutput.go and db_lib/AgentOutputCodex.go | Convert CLI streams into shared structured task-log events. |
| pkg/…/agentlog.go | Defines the event schema and the plain-text projection. |
| api/…/tasks.go | Exposes structured and raw task output through the API. |
| web/…/agentLog.js | Joins event records into console rows. |
| web/…/TaskLogAgentRecord.vue | Defines the visible agent transcript rows and their safe text display. |