WebSocket Protocol — hpilot Daemon Mode
Audience: Developers — Systems integrators building WebSocket clients against the daemon.
Protocol Version: 1.0 (pong tasks extension: v2 — includes cwd, end_stage, end_before per-task fields) Transport: WebSocket (text frames carrying JSON) Default Endpoint: workspace-local Unix socket .hpilot/run/daemon.sock (explicit TCP remains supported)
All messages are JSON objects. The server never sends messages to a client that has no active task. Each message carries a type field that determines its structure.
Common Fields
Every message includes:
| Field | Type | Description |
|---|---|---|
type | string | Message type identifier |
task_id | string | UUID of the task this message pertains to (present on server→client events; required on client→server requests) |
timestamp | string | ISO 8601 UTC timestamp when the server generated the message |
Client → Server Messages
run — Start a New Task
{
"type": "run",
"prompt": "Implement feature X for the auth module",
"cwd": "/workspace/project",
"config": "hpilot.yaml",
"start_stage": null,
"end_stage": null,
"end_before": null,
"task_id": "550e8400-e29b-41d4-a716-446655440000",
"resume_last_session": false
}| Field | Required | Description |
|---|---|---|
prompt | Conditional | Task description / objective. Required when any stage in the effective slice uses {objective} in its prompt_template; optional when every stage has a self-contained prompt_template (config-driven promptless execution). Omit or pass an empty string for promptless runs. |
cwd | Yes | Absolute path of the client's working directory; used to resolve workspace-local .hpilot/config.yaml for per-task config reload |
config | No | Non-empty workspace-relative path to hpilot.yaml (default: hpilot.yaml); absolute paths and .. segments are rejected |
start_stage | No | Non-empty stage name to begin at (default: first stage); must match a stage name in the resolved config |
end_stage | No | Non-empty stage name to stop after; the named stage is executed, then the pipeline stops; must match a stage name in the resolved config; mutually exclusive with end_before; updatable mid-run via set_stop_stage |
end_before | No | Non-empty stage name to stop before; must match a stage name in the resolved config; mutually exclusive with end_stage |
task_id | Yes | Non-empty client-generated UUID for this task |
resume_last_session | No | Boolean (default false). When true, the pipeline attempts to continue from the last captured agent session at start_stage (requires start_stage to also be set). Non-boolean values and true without start_stage are rejected with invalid_message (resume_last_session must be a boolean when provided and resume_last_session requires start_stage). If metadata is absent, mismatched, or the adapter does not support continuation, the stage starts fresh with a logged fallback reason. |
Malformed run messages are rejected with an error event using code invalid_message; the daemon does not synthesize missing required fields. Config selection is limited to files inside the current workspace so the daemon stays aligned with the documented local workflow. Stage names in start_stage, end_stage, and end_before are validated against the stages defined in the resolved config; unknown names result in invalid_message. Supplying both end_stage and end_before results in mutually_exclusive_end_stage. resume_last_session must be a boolean when provided and true requires start_stage; violations are rejected with invalid_message and a descriptive message.
Server response: run_accepted when the task is admitted. If another task is executing, the accepted task waits in the daemon's serialized execution queue. Invalid messages and duplicate task IDs receive an error.
set_stop_stage — Update End Stage Mid-Task
{
"type": "set_stop_stage",
"stage": "Verify",
"end_before": null,
"clear": false,
"task_id": "550e8400-e29b-41d4-a716-446655440000",
"cwd": "/workspace/project"
}| Field | Required | Description |
|---|---|---|
stage | No | Name of the stage to stop after; the named stage is executed, then the pipeline stops; must match a stage name in the task's pipeline config; mutually exclusive with end_before and a true clear |
end_before | No | Name of the stage to stop before; the named stage is skipped (not executed); must match a stage name in the task's pipeline config; mutually exclusive with stage and a true clear |
clear | No | Boolean, default false; when true, removes both the stop-after and stop-before boundary and is mutually exclusive with stage and end_before |
task_id | No | The active task to modify; if absent or empty, the server searches for the running task in cwd |
cwd | Yes | Working directory of the requesting client; used for CWD-based task lookup when task_id is absent |
Exactly one mode is required: a stage, an end_before, or clear: true. Supplying multiple modes results in mutually_exclusive_end_stage; omitting every mode or providing a non-boolean clear results in invalid_message.
When task_id is absent or empty, the server finds the non-completed, non-cancelled task in the matching cwd (path-normalized). If exactly one such task exists, its stop stage is updated. If none exist, unknown_task is returned. If multiple exist, multiple_tasks is returned.
An explicit task that is already completed is rejected with invalid_message; use an active task ID for updates and clears.
Server response: stop_stage_updated or error.
cancel — Abort Active Task
{
"type": "cancel",
"task_id": "550e8400-e29b-41d4-a716-446655440000"
}| Field | Required | Description |
|---|---|---|
task_id | No | Non-empty task ID to cancel; if absent or empty, the server searches for the running task in cwd and cancels it |
cwd | Yes | Working directory of the requesting client; used for CWD-based task lookup when task_id is absent |
When task_id is absent or empty, the server finds the non-completed, non-cancelled task in the matching cwd (path-normalized). If exactly one such task exists, it is cancelled. If none exist, unknown_task is returned. If multiple exist, multiple_tasks are returned.
Sends SIGINT to the active subprocess group. The pipeline aborts gracefully via its built-in interrupt handling.
Server response: pipeline_abort followed by run_complete.
resume — Reconnect and Replay Events
{
"type": "resume",
"task_id": "550e8400-e29b-41d4-a716-446655440000",
"after_sequence": 42
}Sent by a client that disconnected mid-task. after_sequence is an optional non-negative cursor; when present, replay contains only task events with a larger sequence, while an omitted cursor retains full-history attach/log behavior. The daemon serializes replay and the replay-to-live handoff so an event produced at the boundary is delivered once and in order. If the cursor is older than the retained ring-buffer window, the server returns error with code replay_gap rather than silently skipping events. Large replay payloads use ordered event_replay_chunk frames. Completed-task replay remains available until reconnect TTL cleanup with live_resume: false.
Server response: event_replay (if task found and the cursor is available), error with unknown_task (task not found or TTL expired), or error with replay_gap (cursor predates retained history).
ping — Keepalive
{
"type": "ping"
}Client may send ping at any time. Server responds with pong.
Server → Client Messages
run_accepted — Task Queued
{
"type": "run_accepted",
"task_id": "550e8400-e29b-41d4-a716-446655440000",
"timestamp": "2026-04-02T12:00:00.000Z"
}stage_start — Stage Execution Began
{
"type": "stage_start",
"task_id": "550e8400-e29b-41d4-a716-446655440000",
"stage_name": "Spec",
"attempt": 1,
"retry_mode": "fresh",
"goal": false,
"timestamp": "2026-04-02T12:00:01.000Z"
}retry_mode is optional. Values: fresh (first attempt or unsupported adapter), continued (resumed session), fallback (continuation attempted but unavailable). When retry_mode is fallback, an optional fallback_reason string is also included (for example, "adapter_does_not_support_continuation", "no_prior_session_id", or "conversation_unavailable"). Agent stages include the resolved goal boolean so task logs preserve whether native Codex goal mode was active.
agent_output — Live Task Output
{
"type": "agent_output",
"task_id": "550e8400-e29b-41d4-a716-446655440000",
"message": "[Verify][codex] native goal status: complete",
"level": "info",
"timestamp": "2026-04-02T12:00:04.000Z"
}The daemon forwards the same human-readable task diagnostics shown by standalone execution, including adapter lifecycle, tool, reply, terminal native-goal status and recovery messages. Repeated active native-goal updates are omitted from both daemon logging and forwarded output. submit consumes agent_output events without printing them; attach displays them, and logs exposes them as JSON. level preserves the original debug, info, warning, error, or critical destination for displayed agent output. Buffered events are replayed with the rest of the task history.
stage_complete — Stage Finished
{
"type": "stage_complete",
"task_id": "550e8400-e29b-41d4-a716-446655440000",
"stage_name": "Spec",
"success": true,
"attempt": 1,
"timestamp": "2026-04-02T12:00:05.000Z"
}stage_retry — Stage Failed, Retrying
{
"type": "stage_retry",
"task_id": "550e8400-e29b-41d4-a716-446655440000",
"stage_name": "Spec",
"attempt": 2,
"max_retries": 3,
"retry_mode": "continued",
"error": "Agent exited with code 1",
"timestamp": "2026-04-02T12:00:05.000Z"
}retry_mode is optional. Values: fresh (unsupported adapter, starting new session), continued (resumed session via adapter-native mechanism), fallback (continuation attempted but unavailable). When retry_mode is fallback, an optional fallback_reason string is also included (for example, "adapter_does_not_support_continuation", "no_prior_session_id", or "conversation_unavailable").
stage_failed — Stage Exhausted Retries
{
"type": "stage_failed",
"task_id": "550e8400-e29b-41d4-a716-446655440000",
"stage_name": "Spec",
"attempt": 3,
"error": "Retries exhausted",
"timestamp": "2026-04-02T12:00:10.000Z"
}artifact_validated — Contract Artifact Validated
{
"type": "artifact_validated",
"task_id": "550e8400-e29b-41d4-a716-446655440000",
"stage_name": "Review Code",
"artifact_status": "approved",
"timestamp": "2026-04-02T12:00:05.000Z"
}routing_decision — Next Stage Resolved
{
"type": "routing_decision",
"task_id": "550e8400-e29b-41d4-a716-446655440000",
"stage_name": "Review Code",
"next_stage": "Verify",
"decision_source": "contract_route",
"ignore_max_iterations": true,
"iteration_limit_reached": true,
"timestamp": "2026-04-02T12:00:05.000Z"
}ignore_max_iterations and iteration_limit_reached are optional additive booleans emitted for routing decisions from stages with max_iterations > 0. The first reports the selected contract route's setting, and the second reports whether the limit had been reached for that completion. Their combination lets consumers distinguish an ordinary route from a route that remained authoritative at the limit. Both fields are omitted for stages without an iteration limit, so clients that ignore unknown fields remain compatible.
pipeline_abort — Pipeline Aborted
{
"type": "pipeline_abort",
"task_id": "550e8400-e29b-41d4-a716-446655440000",
"reason": "cancelled_by_client",
"stage_name": "Implement",
"timestamp": "2026-04-02T12:00:08.000Z"
}run_complete — Task Finished
{
"type": "run_complete",
"task_id": "550e8400-e29b-41d4-a716-446655440000",
"success": true,
"halted_at_end_stage": "Plan",
"operator_end_before": "Plan",
"timestamp": "2026-04-02T12:05:00.000Z"
}halted_at_end_stage is optional. It is present when a submit-time boundary or an operator's hpilot end --after/--before update halted a task. The value is the reached stop target, allowing a batch client to retain the current plan and stop before submitting later plans.
operator_end_after and operator_end_before are optional, mutually exclusive additive fields. A successful completion includes the active operator-updated boundary so a batch client can roll an unconsumed boundary into the next daemon task. This matters when the current task completed naturally because it had already passed the requested stage: the batch archives that completed plan, submits the next plan with the carried boundary, and lets the next task halt at the requested stage. Both fields are omitted for submit-time-only boundaries, cleared operator boundaries, failures, and completions without an operator end request.
event_replay — Historical Events on Resume
{
"type": "event_replay",
"task_id": "550e8400-e29b-41d4-a716-446655440000",
"events": [
{ ... },
{ ... }
],
"live_resume": true,
"timestamp": "2026-04-02T12:00:10.000Z"
}| Field | Type | Description |
|---|---|---|
events | array | Ordered server event objects; each buffered task event includes a monotonically increasing integer sequence |
live_resume | boolean | true if task is still running; false if already complete |
stop_stage_updated — Confirm Stop Stage Change
{
"type": "stop_stage_updated",
"task_id": "550e8400-e29b-41d4-a716-446655440000",
"stage": "Verify",
"cleared": false,
"timestamp": "2026-04-02T12:00:06.000Z"
}For a clear, stage is absent and cleared is true. Normal stage updates set cleared to false; a clear is represented by the explicit flag rather than an empty or null stage name:
{
"type": "stop_stage_updated",
"task_id": "550e8400-e29b-41d4-a716-446655440000",
"cleared": true,
"timestamp": "2026-04-02T12:00:06.000Z"
}pong — Keepalive Response
{
"type": "pong",
"task_id": "",
"timestamp": "2026-04-02T12:00:00.000Z"
}Extended pong (protocol v2 — when active tasks are present):
{
"type": "pong",
"task_id": "",
"timestamp": "2026-04-02T12:00:00.000Z",
"tasks": [
{
"task_id": "550e8400-e29b-41d4-a716-446655440000",
"active_stage": "Spec",
"attempt": 1,
"status": "running",
"cwd": "/workspace/project",
"end_stage": "Verify",
"end_before": null
}
]
}Note: The end_before field is present in v2 pongs even when null, matching the end_stage / end_before pair shown in the task snapshot field table above. Pre-feature daemons emit a bare pong without the tasks field entirely (no null value — the key is absent).
| Task snapshot field | Type | Description |
|---|---|---|
task_id | string | Unique task identifier |
active_stage | string | null | Current stage name; null while queued or before first stage execution (displayed as "initializing") |
attempt | integer | Current attempt count for the active stage |
status | string | "running" (active), "completed", or "cancelled" |
cwd | string | Working directory of the task at submission time |
end_stage | string | null | Bound: stage to stop after (executed, then pipeline stops), if set |
end_before | string | null | Bound: stage to stop before, if set |
Note: pong is a connection-level keepalive response, not tied to any specific task. The task_id field is included for protocol consistency but is always empty for pong events. The optional tasks field contains a snapshot of all non-abandoned tasks in the registry. When no tasks are active, tasks is an empty array []. When the daemon predates this extension, tasks is absent entirely (clients treat it as []). The active_stage field may be null during the window between task registration and first stage execution (displayed as "initializing" by status clients). The status field values are "running" (active, neither completed nor cancelled), "completed", or "cancelled". Each task snapshot also includes cwd and the effective end_stage and end_before; updates and clears are reflected immediately, so cleared boundaries appear as null.
error — Protocol or Execution Error
{
"type": "error",
"task_id": "550e8400-e29b-41d4-a716-446655440000",
"code": "task_exists",
"message": "Task 550e8400-e29b-41d4-a716-446655440000 already exists",
"timestamp": "2026-04-02T12:00:00.000Z"
}| Error Code | Description |
|---|---|
task_exists | The submitted task ID already exists in the active registry |
unknown_task | task_id not found (e.g., TTL expired) |
multiple_tasks | Multiple running tasks found in the same directory; disambiguate with an explicit task_id |
not_cancellable | Task is not in a cancellable state |
invalid_message | Message failed validation, including requests that are no longer valid for the task's current lifecycle state |
mutually_exclusive_end_stage | Multiple boundary modes were provided: both end_stage and end_before on run, or more than one of stage, end_before, and true clear on set_stop_stage |
server_error | Internal server error |
Connection Lifecycle
- Client opens a WebSocket connection through the resolved Unix socket or explicit
ws://host:portTCP endpoint; framing and messages are identical - Server accepts connection; no authentication in v1
- Client sends a message (typically
runorresume) - Server and client exchange messages as described above
- Client disconnects at any time; server continues task execution
- If client reconnects and sends
resumewith the sametask_id, server replays buffered history then continues live streaming - Task eventually completes; server sends
run_complete - Client may send a new
runon the same connection afterrun_complete
Large Payload Chunking
Only oversized event_replay responses are chunked. Normal live events such as artifact_validated are always sent as their native event type.
When replay history exceeds 64 KB, the server emits ordered event_replay_chunk frames:
{
"type": "event_replay_chunk",
"task_id": "...",
"chunk_id": "ch-0",
"chunk_index": 0,
"chunk_count": 3,
"events": [ ... ],
"live_resume": true,
"timestamp": "2026-04-02T12:00:10.000Z"
}| Chunk frame field | Type | Description |
|---|---|---|
chunk_id | string | Stable identifier grouping all frames of the same payload; format: ch-{index} on server-side chunking, {task_id}_{connection_generation} when emitted from resume replay |
chunk_index | integer | Zero-based frame index within the payload |
chunk_count | integer | Total number of frames in the payload |
events | array | Slice of server event objects for this frame |
live_resume | boolean | true if the task is still running at replay time; false if the task has completed |
Client reassembles by chunk_index ascending until chunk_count frames have been received.
Running the Integration Suite
Run the daemon integration suite with:
uv run pytest --run-integration tests/integration/test_server_ws.pyThis suite is intended to run in local development and CI/review. It exercises the required daemon acceptance coverage for ping/pong, set_stop_stage, cancellation ordering, resume replay, replay chunking, and error handling.
Example Session
Client → Server:
{ "type": "run", "prompt": "build auth feature", "config": "hpilot.yaml", "task_id": "abc" }
Server → Client:
{ "type": "run_accepted", "task_id": "abc", "timestamp": "..." }
Server → Client:
{ "type": "stage_start", "task_id": "abc", "stage_name": "Spec", "attempt": 1, "timestamp": "..." }
... (client reconnects here) ...
Client → Server:
{ "type": "resume", "task_id": "abc" }
Server → Client:
{ "type": "event_replay", "task_id": "abc", "events": [{ "type": "run_accepted", ... }, { "type": "stage_start", ... }], "live_resume": true, "timestamp": "..." }
Server → Client:
{ "type": "stage_complete", "task_id": "abc", "stage_name": "Spec", "success": true, "attempt": 1, "timestamp": "..." }
...