Skip to content

Task state machine - #938

Open
Eliot Horowitz (erh) wants to merge 5 commits into
mainfrom
task-state-machine
Open

Eliot Horowitz (erh) wants to merge 5 commits into
mainfrom
task-state-machine

Conversation

@erh

Copy link
Copy Markdown
Member

needs more work - but close

Eliot Horowitz (erh) and others added 2 commits October 16, 2026 05:11
A task is a unit of robot work with a start, an end and an outcome:
Run streams progress and ends with a Result, GetStatus reports idle or
running. A state machine is a configured graph of states that runs tasks
and reacts to events: GetState, SendEvent, GetHistory, GetDefinition,
Stop and Reset.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018vMsNdievPPiRY8gB5KrKR
Run returns the result when the task is finished; progress is read with
GetStatus rather than streamed.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018vMsNdievPPiRY8gB5KrKR
@viambot viambot added the safe to test committer is a member of this org label Nov 2, 2026
Comment thread proto/viam/service/task/v1/task.proto Outdated
Comment on lines +30 to +32
rpc GetStatus(GetStatusRequest) returns (GetStatusResponse) {
option (google.api.http) = {get: "/viam/api/v1/service/task/{name}/status"};
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it potentially confusing that this method uses different request and response types from the standard GetStatus method on every other resource including the StateMachine service?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed — it both read oddly next to every other resource and left tasks without the standard status. Pushed in 2cf3ea2: the typed RPC is now GetTaskStatus(GetTaskStatusRequest) → GetTaskStatusResponse (/task_status), and GetStatus is the usual common.v1 one, same as StateMachineService.


Generated by Claude Code

@viam-overwatch

viam-overwatch Bot commented Nov 9, 2026

Copy link
Copy Markdown

Hey Eliot Horowitz (@erh) — this PR has been approved and CI has been green for 5+ business days. Ready to merge?

Auto-comment from overwatch. Will not re-nudge for 7 days.

@viam-overwatch

Copy link
Copy Markdown

Hey Eliot Horowitz (@erh) — this PR has been approved and CI has been green for 6+ business days. Ready to merge?

Auto-comment from overwatch. Will not re-nudge for 7 days.

Eliot Horowitz (erh) and others added 2 commits November 23, 2026 22:13
…tatus

GetDefinition now returns the machine's states (path, initial child,
handled triggers, whether it runs a task), its top-level transitions and
every trigger, instead of an untyped Struct. GetCurrentJobId identifies
the current run of the machine: a new job starts at startup and on Reset.

The task service's typed status RPC is GetTaskStatus; GetStatus is the
standard resource status every other service exposes.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018vMsNdievPPiRY8gB5KrKR

@HipsterBrown Nick Hehr (HipsterBrown) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some feedback from building a small toy program against this API: a task implementation, a minimal state-machine engine, and a client that drives it.

repeated StateDefinition states = 2;
// Every trigger any state or the machine itself handles. Task outcomes and
// external events share this namespace; an agent may send any of them.
repeated string events = 3;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Task outcomes and external events share one namespace, and this says an agent may send any of them. In practice that means a client can send an outcome like done while a task is running; the machine cancels the task and transitions as if it had finished. The only trace in history is an empty task_result.

Suggest separating the two namespaces (e.g. outcomes vs events here), or having SendEvent reject names that are only task outcomes.

}

// Transition is one recorded change of active state.
message Transition {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two fields would help here:

  • source (event vs. task outcome vs. stop/reset), so history shows what actually caused a transition without inferring it from whether task_result is set.
  • job_id, so history can be grouped by job.

Relatedly, returning job_id from GetStateResponse would avoid a race: today a client calls GetState and GetCurrentJobId separately, and the job can change between the two calls.

// The active state path after the transition.
repeated string to = 2;
// What caused it: the task outcome, the event name, "timeout", "stop" or "reset".
string trigger = 3;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"stop" is listed as a trigger, but Stop holds the current state rather than changing it, so it is unclear when a transition would record it. "timeout" is listed too, but StateDefinition has no timeout field, so a client cannot see which states time out or after how long. Also, entering the initial state at startup does not appear to produce a transition; is a "start" trigger intended?

// Name of the state machine service
string name = 1;
// Maximum number of transitions to return, newest first. 0 means all retained.
int32 limit = 2;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ordering docs disagree: the rpc says "newest last", the response says "oldest first", and limit says "newest first". Suggest one wording, e.g. "returns the most recent limit transitions, oldest first".

// The event name, as written in a state's "on" map.
string event = 2;
// Event payload, merged into the input of the task the target state runs.
google.protobuf.Struct payload = 3;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The merge rules are unspecified, and every engine will have to pick its own:

  • Which wins on a key clash, context or payload?
  • Is the merge shallow or deep? (Same question for task output into context.)
  • Does the payload persist in context, or only reach this one task input?

If payload does not persist, a state that waits for an event and then re-enters a task needs the client to resend all the original data. That is worth stating either way.

// are the special targets).
map<string, string> on = 3;
// Whether the state runs a task.
bool has_task = 4;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest adding the task resource name (e.g. string task) alongside or in place of has_task, plus an optional timeout if timeouts are configurable per state. Without the name, a client cannot call GetTaskStatus on the task itself or link to it in a UI.

// hardware in a safe state before returning. Only one execution may be in
// flight per task resource; a concurrent Run fails with FAILED_PRECONDITION.
// Progress while it runs is read with GetTaskStatus.
rpc Run(RunRequest) returns (RunResponse) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The failure and cancellation contract needs pinning down. When writing a task, there are three plausible ways to fail: return a gRPC error, return an "error" outcome, or return a custom outcome like bad_input. On cancel, it is unclear whether to return ctx.Err() or a result.

Suggestion: failures always return an "error" outcome with details in output (no gRPC error), and a reserved "cancelled" outcome for cancellation. Also worth noting: one execution per resource means two states or machines sharing a task will get FAILED_PRECONDITION, which the machine will likely treat as an error and fault.

message Result {
// The outcome name, e.g. "done", "no_glass", "error". A state machine looks
// this up in the current state's transition table.
string outcome = 1;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since outcomes are free strings, a typo ("Done" vs "done") only shows up at runtime. The state machine doc covers an unhandled "error" (FAULTED) but not other unhandled outcomes: the machine is left in a state with no running task while still reporting RUNNING. Suggest defining that case (e.g. treat any unhandled outcome as FAULTED) and listing the reserved outcomes (error, and cancelled if adopted) here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

safe to test committer is a member of this org

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants