Introducing a new way to review your PRs.

Read the announcement

semaphoreui / semaphore

Modern UI and powerful API for Ansible, Terraform/OpenTofu/Terragrunt, PowerShell and other DevOps tools.

View on GitHub

21 documents · Updated Nov 23, 2026

docs / task-and-runner-api

Task and runner API

The task and runner API coordinates work from launch to completion. Authenticated users create and control project tasks, read their history and output, and receive live status events. Runners register with the server, poll for assigned work, report progress, and receive stop instructions.

API boundary

Project task routes use the normal authenticated API and apply project, template, and workflow permissions. Runner protocol routes use runner credentials instead. Global task and runner views are administrative operations, while project runner operations manage runners assigned to one project. See the HTTP API and access control page for the shared authentication model and the identity, sessions, and security page for session and token details.

API areaMain purposeAccess boundary
Project tasksLaunch, list, inspect, control, and remove task recordsProject membership and task-related permissions
Global task stateInspect active queue and running workGlobal system management permission
Runner protocolRegister, poll, and report task progressRunner token in X-Runner-Token
Runner managementCreate, update, enable, disable, rotate, clear, and delete runnersGlobal or project resource management permission
Live eventsStream task log and status messagesAuthenticated user WebSocket

Task lifecycle

The task lifecycle starts with a project task request and ends when the server stores a terminal result. The task pool assigns eligible work to a runner, while the task record remains the source for later queries and output reads.

sequenceDiagram
    participant U as Authenticated client
    participant S as API server
    participant P as Task pool
    participant R as Runner
    participant W as WebSocket client

    U->>S: POST /api/project/{project_id}/tasks
    S->>P: Queue task
    R->>S: GET /api/internal/runners
    S-->>R: New job and task data
    R->>S: PUT /api/internal/runners
    S-->>W: Task log and status events
    U->>S: GET task, output, or stages
    S-->>U: Current or stored result

Task launch

POST /api/project/{project_id}/tasks creates a task from a project template. The request may identify the template by ID or name. The server removes workflow binding, output, and runner assignment fields supplied by the client, then places the task in the execution pool. A successful request returns the created task with 201 Created.

References: api/router.go• L331-335 api/…/tasks.go• L40-123

Task query

Task query operations serve two needs: project history and active execution state. Project members can list tasks for a project or template, page through recent tasks with a before cursor, inspect one task, and read stage records or output. Global administrators can view the in-memory queue and running set through /api/tasks and /api/tasks/{task_id}.

OperationResult
GET /api/project/{project_id}/tasksProject task history
GET /api/project/{project_id}/tasks/lastRecent tasks with keyset paging
GET /api/project/{project_id}/templates/{template_id}/tasksTasks for one template
GET /api/project/{project_id}/tasks/{task_id}One task record
GET /api/project/{project_id}/tasks/{task_id}/stagesStructured stage results
GET /api/project/{project_id}/tasks/{task_id}/outputOutput lines as JSON
GET /api/project/{project_id}/tasks/{task_id}/raw_outputOutput as plain text
GET /api/tasksActive queue and running tasks for administrators

Recent-task pages accept count and before. The older limit name remains accepted. The response uses X-Has-Next to show whether an older page exists.

References: api/router.go• L347-352 api/router.go• L517-529 api/router.go• L579-588 api/router.go• L274-278 api/…/tasks.go• L125-225

Task control

Task control changes execution state rather than deleting task history. A normal stop asks the runner to stop and can leave the task in a stopping state until the runner reports completion. A forced stop marks the task stopped at once and can terminate an active process. Approval routes resolve tasks that wait for confirmation.

OperationPurpose
POST /api/project/{project_id}/tasks/{task_id}/stopStop one task; body may set force
POST /api/project/{project_id}/tasks/{task_id}/confirmApprove a task waiting for confirmation
POST /api/project/{project_id}/tasks/{task_id}/rejectReject a task waiting for confirmation
POST /api/project/{project_id}/templates/{template_id}/stop_all_tasksStop all active tasks for a template
DELETE /api/project/{project_id}/tasks/{task_id}Delete stored task history after the task is inactive
DELETE /api/cluster/tasksClear selected active-state records for cluster recovery

The server does not allow removal of an active task. An administrator must stop it first. The cluster clear operation is a maintenance action for a stuck task-state store, not a normal task-control path.

References: api/router.go• L337-341 api/router.go• L517-530 api/router.go• L579-585 api/router.go• L244-246 api/…/tasks.go• L429-567 api/cluster.go• L88-134

Task output

Task output is available in two forms so clients can choose structured data or a display stream. JSON output keeps each stored line as a record. Raw output joins lines into plain text and removes terminal color codes. Workflow task results are accepted from the runner with the success report and are stored before the task reaches its final success state.

References: api/…/tasks.go• L332-427 api/…/runners.go• L489-520 services/…/types.go• L63-76

Task event stream

GET /api/ws opens an authenticated WebSocket for live task events. The server sends log messages and status updates to the users attached to the task. The stream complements persisted task output: clients can show current activity from the stream and use task query endpoints to recover state after reconnecting.

Task messages identify the event type and include the task and project IDs. In a high-availability deployment, the server can relay these messages across nodes so the user does not need to connect to the node that runs the task.

References: api/router.go• L196-199 api/…/handler.go• L150-193 services/…/TaskRunner_logging.go• L22-57 services/…/TaskRunner.go• L117-140

Runner lifecycle

Runners are execution agents that pull work from the server. Their protocol is deliberately server-led: the runner registers once, polls for a state document, executes the jobs it receives, and sends status and log records back to the same protocol.

flowchart LR
    A[Registration token] --> B[POST /api/internal/runners]
    B --> C[Runner token]
    C --> D[GET /api/internal/runners]
    D --> E[Jobs and access data]
    E --> F[Runner executes tasks]
    F --> G[PUT /api/internal/runners]
    G --> H[Status, logs, commits, outputs]
    H --> D

Runner registration

POST /api/internal/runners exchanges a valid registration token for a runner token. A shared global registration token creates a new runner. A one-time token registers a specific unregistered runner. The returned token authenticates later polling and progress calls.

Registration data can bind the runner to a project and can declare its name, tags, enabled state, and task capacity. The registration token is not the same as the runner token used after registration.

References: api/router.go• L175-182 api/…/runners.go• L588-688 services/…/types.go• L78-89

Runner polling

GET /api/internal/runners is the runner health and work endpoint. The runner sends its token in X-Runner-Token and may send X-Runner-Started-At to identify a process restart. The response reports current jobs, new jobs, required access keys, and any cache-clear instruction.

Each new job carries the access data needed for that task. MCP server tokens are sent as task-specific access keys rather than in the shared access-key map, so two queued tasks can use different token snapshots even when they refer to the same server key. If an MCP token is not available, the server fails that dispatch instead of sending a job without the required credential.

Polling also acts as the runner heartbeat. A runner that stops polling becomes offline after the configured timeout. If the server detects a restart or stale heartbeat, it updates runner health and reconciles work that the runner may have lost.

References: api/…/runners.go• L24-181 api/…/runners.go• L184-283 api/…/runners.go• L386-425 services/…/types.go• L11-37 api/…/runners_mcp_test.go• L24-124

Runner progress

PUT /api/internal/runners accepts a batch of job reports. Each report can carry a task status, timestamped log records, commit data, and workflow outputs. The server checks that the task still belongs to that runner and accepts only known task states. It returns terminated_jobs when the server has already stopped or removed a job, so the runner can stop its local process and discard further reports.

Terminal progress finalizes the task on the server. This releases task and runner capacity and allows downstream workflow actions to continue without relying on the node that first dispatched the work.

References: api/…/runners.go• L427-586 services/…/types.go• L49-76 services/…/job_pool.go• L600-686

Runner management

Runner management has global and project-scoped forms with the same basic lifecycle. Administrators can inspect status, create or update metadata, enable or disable a runner, rotate its registration token, clear its cache, and delete it. Project managers can perform the same actions for runners assigned to their project.

OperationGlobal routeProject route
List runnersGET /api/runnersGET /api/project/{project_id}/runners
Create runnerPOST /api/runnersPOST /api/project/{project_id}/runners
Read or update runner/api/runners/{runner_id}/api/project/{project_id}/runners/{runner_id}
Enable or disablePOST .../{runner_id}/activePOST .../{runner_id}/active
Rotate registration tokenPOST .../{runner_id}/registration-tokenPOST .../{runner_id}/registration-token
Clear runner cacheDELETE .../{runner_id}/cacheDELETE .../{runner_id}/cache
Delete runnerDELETE .../{runner_id}DELETE .../{runner_id}

The list and detail responses include derived registration and online status. Cache clearing is delivered to a polling runner rather than performed by the management request itself.

References: api/router.go• L248-261 api/router.go• L402-413 api/runners.go• L31-249 pro/…/runners.go• L11-60

Access model

The API separates human control from runner protocol access. Human task and runner-management requests pass through the authenticated API and then through project or global permission checks. Runner requests do not use a user session; the runner token identifies the agent and becomes the audit actor for runner activity.

Task reads are also filtered by effective project, template, and workflow-run permissions. A user who cannot read a task receives the same not-found behavior as an unknown task, which prevents task visibility from bypassing the project access model.

References: api/router.go• L196-202 api/router.go• L333-352 api/router.go• L579-588 api/…/runners.go• L24-64 api/…/tasks.go• L227-259

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the task request sanitization aligned with workflow binding and runner assignment. Client-supplied workflow or output fields must not become trusted task state.
  • Keep runner poll and progress payloads compatible with the runner job pool. Changes to terminal status handling must preserve terminated_jobs behavior.
  • Keep task-specific MCP token delivery separate from shared access keys. A shared key map can send one task's token to another task's server URL.
  • Keep live WebSocket event fields aligned with task status persistence so a reconnecting client can reconcile stream events with task reads.
  • Keep global and project runner routes aligned when changing token rotation, active state, or cache-clear behavior.
  • Keep forced-stop cleanup aligned with remote runner finalization so shared task and runner capacity is released.

Important failure modes

TriggerConsequenceGuard
Runner sends an invalid or stale tokenRegistration or polling fails and no work is deliveredValidate the registration token once and send the returned runner token in X-Runner-Token
Runner reports a task assigned to another runnerThe report is ignored and the task ID is returned in terminated_jobsStop the old local job before it can continue beside the new assignment
Client tries to delete an active taskThe request fails and task history remainsStop the task first; delete only after it leaves the active pool
Forced stop occurs while a remote runner is offlineThe server finishes the task before a later runner report arrivesTreat the terminal server state as authoritative and apply terminated_jobs on the next progress response
Workflow success report has invalid outputsThe task is changed to an error instead of claiming successValidate and store outputs before accepting the success status
MCP server token is missing when a job is preparedThe task fails before dispatch instead of running without the required tokenDecrypt task MCP tokens before publishing the job and keep them scoped to that job
Runner stops pollingThe runner becomes offline and its work may be reconciled or requeuedUse polling as the heartbeat and report process start time after every restart

Key files

FileWhy to read it
api/router.goMaps the human, runner, task, management, and WebSocket endpoints to their access boundaries.
api/…/tasks.goDefines project task launch, query, output, approval, stop, and removal behavior.
api/…/runners.goDefines runner authentication, polling, registration, progress exchange, and task-scoped credential delivery.
api/runners.goDefines global runner management and status operations.
services/…/types.goDefines the wire-level job, state, progress, and registration contracts.
api/…/runners_mcp_test.goShows the contract that MCP tokens stay with their own dispatched task and are not exposed in serialized inventory or shared keys.
api/…/handler.goDefines authenticated live event connections and message delivery.
services/…/TaskPool.goExplains task assignment, stop behavior, and remote finalization.