Skip to content

feat: prototype Starlark workflow runs in chatd (do not merge) - #31189

Draft
ThomasK33 wants to merge 8 commits into
mainfrom
tk/chat-workflows-prototype
Draft

ThomasK33 wants to merge 8 commits into
mainfrom
tk/chat-workflows-prototype

Conversation

@ThomasK33

@ThomasK33 ThomasK33 commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

Warning

Do not merge. This is a throwaway prototype. It exists to inform an RFC about workflow scripts in Coder Agents. The code goes away after the RFC.

Summary

A root chat can run a workflow: a Starlark script that starts sub-agent steps, waits for their structured results, and combines them. The work sits behind the user-scoped chat-workflows experiment.

  1. run_workflow compiles the script, starts it in a goroutine on the coderd replica, and returns a run_id at once.
  2. agent() and spawn() create child subagent chats (general or explore). Each child gets a server-side workflow_report tool whose input schema is the step schema. chatd checks the value with google/jsonschema-go. A valid value ends the child turn. A child that stops without a value gets one reminder, then the step fails (wait() gives None, agent() fails the script).
  3. await_workflow waits up to 780 s (default 600 s). If the run ends while no call waits, chatd sends the parent a user message that names the run, so an idle parent starts a new turn and calls await_workflow. A queued message is deleted when an await_workflow call reads the result first.
  4. Run IDs come from the tool call ID and step IDs from the run ID plus a hash of the step input. After a coderd restart the model calls run_workflow with resume_run_id, and finished steps are reused instead of run again. There is no new table.
  5. run_workflow takes the script inline in script, or as script_path: the absolute path of a file in the chat's workspace. chatd reads the file through the agent connection one time, when the run starts. The model can then fix a script with small edits and start it again without resending it. run_workflow runs after the other tool calls of its step, so a write_file or edit_files call in the same batch is done before the read.

How to try it

  1. Turn the experiment on as an admin: PUT /api/experimental/experiments/rules/chat-workflows with {"mode": "on", "expected_revision": 0}, or start coderd with --experiments chat-workflows. The preview eph-pr-31189 has the rule on: https://3000--dev--eph-pr-31189--tk--apps.dogfood.cdr.dev (sign in as [email protected]).
  2. In a new agent chat, ask the agent to use run_workflow. Example script:
REVIEW = {"type": "object", "required": ["bugs"],
          "properties": {"bugs": {"type": "array", "items": {"type": "string"}}}}

def main(args):
    steps = [spawn("Review %s for bugs." % f, schema = REVIEW, title = f) for f in args["files"]]
    reviews = wait(steps)
    return {"bugs": [b for r in reviews if r != None for b in r["bugs"]]}

Limits of the prototype

  • A run lives in the memory of one coderd replica. An interrupt of the parent does not stop it, and there is no cancel tool.
  • The user-scoped experiment is evaluated on each build of a root turn environment, not once for each turn (one extra rules read for each build).
  • A user-supplied dynamic tool named workflow_report is ignored.
  • ARCHITECTURE.md has two TODOs for the human author.

Validation

  • go test ./coderd/x/chatd/... ./codersdk/ -count=1 passes. New tests: TestWorkflowReport, TestWorkflowReplay, TestWorkflowRemind, TestWorkflowNotice, TestWorkflowLimit (also with -race -count=2), TestWorkflowScriptPath.
  • golangci-lint run ./coderd/x/chatd/... ./codersdk/ ./scripts/ reports 0 issues.
  • Dogfood on the preview with Opus 5.5 and GPT 6 Astra: 11 runs, 64 steps, all completed. The results for the six RFC questions and the evidence are in the PR comments. Two more chats test script_path: one writes a script, runs it, edits it in place and runs it again. The other edits the file and calls run_workflow in one batch, and the run logs the edited line.
📋 Implementation plan (claims of the approved html-plan)
  1. A root chat starts a workflow run and gets a run ID at once. Only root chats get the two tools, and only with the experiment on. The script defines main(args) and calls agent(), spawn() and wait(). It cannot read the clock, files or the network.
  2. Each agent() step returns a value that matches its schema, or fails. The child gets workflow_report with the step schema as input. chatd runs it on the server. The tool checks the value in Go. If the child stops without a value, it gets one reminder.
  3. A run continues in the background. The parent waits or gets a message. await_workflow waits 780 s at most. If no call waits when the run ends, chatd sends the parent a message. Run and step IDs come from stored data. After a restart the model resumes the run and reuses finished steps.
  4. Steps in one wait() run at the same time. A failed step gives None. Steps above the subagent limit wait in the queue. A run can create 50 steps at most.
  5. The prototype answers six questions for the RFC: language, reports, replay, capacity, size, wake.

Decisions: Starlark; the model calls again with the run ID plus a background run that messages the parent; a draft PR with a preview deployment.


Generated with xum • Model: anthropic:claude-opus-5-5 • Thinking: xhigh

Throwaway prototype to inform an RFC. Root chats get run_workflow and
await_workflow behind the user-scoped chat-workflows experiment.

- run_workflow compiles a Starlark script, starts it in a background
  goroutine, and returns a run ID at once.
- Each agent()/spawn() step is a child subagent chat with a
  deterministic ID. The child gets a server-side workflow_report tool
  whose input schema is the step schema; a valid value ends its turn.
- await_workflow waits up to 780 s. When a run ends and no call waits,
  chatd sends the parent a user message so the model awaits the run.
- After a restart, run_workflow with resume_run_id reuses finished steps.

---
_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
The sender can still hold the queued message ID when await_workflow reads the result. It then deletes the message itself, so the test waits for the delete.
chat-workflows is user-scoped, so the rules list now has one more entry.
@ThomasK33

ThomasK33 commented Oct 23, 2026 •

Copy link
Copy Markdown
Member Author

Workflow prototype: results for the RFC

The prototype answers the six RFC questions. All 11 workflow runs on the preview completed. After a coderd restart, no step got a second child chat. Queued steps waited for a 30-second poll, not for a free slot.

  • Where: preview eph-pr-31189, commit 9f3bd772d3 (full mode), from draft PR #31189. The chat-workflows experiment rule was on for all users.
  • Models: Opus 5.5 and GPT 6 Astra, through the dogfood AI Gateway. Step chats use the model of their parent chat.
  • When: 2026-10-23, 11:32 to 11:50 UTC. 11 runs, 64 steps.
  • Terms: a run is one workflow script execution. A step is one child chat that a run starts with agent() or spawn(). The end message is the user message that chatd sends to the parent chat when a run ends and no await_workflow call waits.

Answers

Question Answer Evidence
Q1 Language: does a model write a working script in 2 tries? Yes, for prompts that describe the structure of the workflow. Each model wrote a working script on the first try, 7 times out of 7. No script had a compile or runtime error. 3 tasks for each model (parallel facts, a writer and critic loop, ticket classification), plus 1 chat started in the UI.
Q2 Reports: how many steps need a reminder, and how many fail? 0 of 62 normal steps needed a reminder. 0 of 64 steps failed. Every normal step called workflow_report one time with a valid value. 2 steps were told to break the rules (see below).
Q3 Replay: after a coderd restart, does a run repeat a step? No. A 6-step run made 6 child chats after 1 restart of 1 replica. The model resumed the run without help from the user. Restart during step 3. Steps 1 to 4 were reused, steps 5 and 6 were new.
Q4 Capacity: how long do 15 parallel steps wait for 10 slots? Steps 11 to 15 waited about 28 s. One step took about 13 s. Steps 1 to 10 replied after 12 to 15 s. Steps 11 to 15 replied after 41 to 47 s.
Q5 Size: how many lines does each part take? 1,117 lines of new Go (943 without blank lines and comments), 437 lines of tests, and about 50 lines in existing files. Table in the Q5 section.
Q6 Wake: does the end message start a parent turn, and does the model then call await_workflow? Yes, 4 times out of 4 (Opus 3 times, Astra 1 time), when the prompt told the model to end its turn after run_workflow. The model called await_workflow 1.7 to 2.2 s after the end message. chatd sent the end message in the same second as the last step result.

Q1 Language

The models wrote these Starlark constructs without help: lists of spawn() handles with one wait(), a while loop around agent(), list and dict comprehensions, zip(), % formatting, and "enum", "minItems" and "additionalProperties" in step schemas.

Limit of this test: each prompt described the structure of the workflow. I did not test vague tasks, long scripts, or a model that writes a wrong script first. The Q1 answer is "yes for clear tasks".

Q2 Reports

Steps Reminders Rejected values Steps without a value
64 1 1 0

Both counted events came from 2 steps that I told to break the rules:

  1. A step told "Reply in plain text. Do not call any tool." replied with text. chatd sent the one reminder. The step then called workflow_report with {"text": "hello"}.
  2. A step told to send "five" for an integer field got the schema error from chatd. The step sent 5 on the next call.

Limit of this test: most steps were short (2 to 25 s). Only 3 steps used workspace tools (3 read_file calls and 1 execute call on Go files, in a docker workspace). Long steps with many tool calls can forget the report more often. I did not test that.

Q3 Replay

The run had 6 sequential agent() steps. I ran systemctl restart eph-dev while step 3 ran.

  1. The old coderd process kept running during its graceful stop. In that time it created step 4.
  2. coderd was down for about 10 s.
  3. chatd ran the interrupted await_workflow call again. The call returned "run not found".
  4. The model called run_workflow with the same script and resume_run_id. Nobody sent a message.
  5. The new runner reused steps 1 to 4. Step 4 was in flight during the restart. chatd resumed its chat, and the step reported one time. I did not check whether its model call started again. The runner waited for step 4, then created steps 5 and 6.
  6. The run ended 78 s after it started. The parent chat had 6 child chats.

Q4 Capacity

chatd runs at most 10 sub-agent chats at the same time. A run with 15 parallel steps gave these times from step creation to first reply:

Steps First reply after
1 to 10 12 to 15 s
11 to 15 41 to 47 s

No other chats ran at that time. The limit applies to the whole deployment, so on a shared deployment other users' sub-agents also take slots. The first 10 steps ended after about 14 s. Steps 11 to 15 did not start then. They started about 15 s later. The likely cause is that a queued chat starts on the next acquire pass, and chatd runs that pass every 30 s (DefaultPendingChatAcquireInterval). I read this in the code. I did not trace it in the logs.

For the RFC: each wave of steps above the limit adds up to 30 s of wait, in addition to the step time.

Q5 Size

Part File and lines Lines Keep for the RFC?
Tool surface: descriptions, input and output types, run_workflow and await_workflow workflow.go 25 to 176 152 Keep the shape.
Run ID, step ID, in-memory registry workflow.go 178 to 223 46 Keep the IDs. Replace the registry.
Run lifecycle: start, execute, end message, await, log workflow.go 225 to 506 282 Replace with a stored run and a lease.
Steps: create or reuse a child chat, wait, one reminder workflow.go 508 to 654 147 Keep.
Starlark host: compile, run, agent, spawn, wait, log, json workflow_starlark.go 262 Keep if Starlark stays.
Report tool: server-side tool, schema check, read the report workflow_report.go 201 Keep.
Wiring in existing files chatd.go, subagent.go, turn_environment.go, tasks.go, chatstate/synthetics.go, codersdk about 50 Replace the reserved dynamic tool name with a real column.
Tests workflow_internal_test.go 437

Replace before production:

  1. The run lives in the memory of one coderd replica. Other replicas cannot see it, and there is no cancel.
  2. The step schema is stored in chats.dynamic_tools under the reserved name workflow_report. A real feature needs its own column.
  3. The end message is a plain user message (see the other findings).
  4. resume_run_id accepts any run ID when the run is not in memory (workflow.go 286 to 298). chatd refuses step chats of another parent chat (workflow.go 551), but a stored run needs an owner check.

Q6 Wake

Chat Model End message after the last step result await_workflow after the end message
Parallel facts Opus 5.5 0.0 s 1.7 s
Parallel facts GPT 6 Astra 0.0 s 2.1 s
Review of 3 Go files in a workspace Opus 5.5 0.0 s 1.9 s
Parallel facts, started in the UI Opus 5.5 0.0 s 2.2 s

In each chat the prompt told the model to end its turn after run_workflow. Each model did that, and then showed the result after the end message.

Other findings

  1. The end message looks like the user wrote it. The UI shows it as a normal user bubble. A reader cannot see that chatd sent it. The RFC needs a message origin, or a separate message type.
  2. The script is hard to read. The generic tool view shows the script as one JSON string with \n escapes. A run needs its own view with the script, the steps (with links to the step chats) and the status.
  3. A stopping coderd keeps running runs. During the graceful stop, the old runner created 1 more step. The deterministic step IDs made this safe. The RFC design must stop runners at shutdown, or depend on the IDs.
  4. Step titles can repeat. The writer and critic loop on Opus named 4 steps "Critic". Each step still got its own chat, because the step ID depends on the prompt.
  5. PR body error, fixed. The PR body gave /api/v2/experiments/rules/... for the experiment rule. The correct path is /api/experimental/experiments/rules/chat-workflows.

Not tested

  • More than one coderd replica.
  • Interrupt of the parent chat during a run, and cancel (the prototype has no cancel).
  • Runs longer than one await_workflow wait (780 s), so a second await_workflow call.
  • The 50-step limit and the failed-step path on a real model. Unit tests cover both.
  • A member user. All chats ran as the admin user.

Evidence

Chat ID
Q6 wake and Q1 task A (Opus) 30d944ee-d371-4585-ac23-462f46ea9f2d
Q1 task B (Opus) 646c7a50-b101-4f9b-aa62-6b76cbc5a839
Q1 task C (Opus) 979cc7d9-dcc3-4f72-8869-f654642b2463
Q1 task A (Astra) 6aca3e3b-cb14-4505-b036-9e97780fc404
Q1 task B (Astra) a96c960d-4cf4-4577-ad6e-577e803c83fc
Q1 task C (Astra) 6141e2b6-5426-4a70-bcd0-96979519a541
Q4 capacity: 15 parallel steps 78407430-5e29-4a05-963b-5b963328e406
Q3 restart: 6 sequential steps 60de75dc-e50d-49a2-8881-9346f034f7cd
Review of 3 Go files in a workspace 1473bf1d-22bd-48ed-a74c-c6b36c812126
Q2 steps that break the rules db5f9372-4986-43f8-91f4-21607ceb3823
Run started in the UI 0ef1e508-7aee-4bff-a528-73f0e6c7d119

Screenshots and video

A run started in the UI, in real time (32 s). The model starts the run, ends its turn, gets the end message, calls await_workflow, and shows the table. I made the video from screenshots taken about every 0.8 s, so it plays in real time at a low frame rate.

wake-demo.mp4

The parent chat after the end message. The end message shows as a normal user bubble.

Parent chat: run_workflow, the turn ends, the end message arrives, await_workflow returns the facts

The generic tool view shows the script as one escaped JSON string.

run_workflow tool input with the script as an escaped JSON string

The restart run. The first await_workflow call returns "run not found". The model calls run_workflow with resume_run_id, and the second await_workflow call shows "reused": true for the steps that ran before the restart.

Restart chat: await_workflow not found, run_workflow resume, await_workflow with reused steps

A step chat that replied without workflow_report. chatd sends the one reminder, and the step then reports.

Step chat: plain text reply, the reminder, then workflow_report


Generated with xum • Model: anthropic:claude-opus-5-5 • Thinking: xhigh

@linear-code

linear-code Bot commented Oct 26, 2026

Copy link
Copy Markdown

CODAGT-1467

…ce file

run_workflow takes script_path as an alternative to script. chatd reads the file from the chat's workspace through the agent connection, one time, when the run starts. The agent can then fix a script with small edits and start it again without sending the whole script in the tool call.

---
_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
A model often sends write_file and run_workflow with script_path in one batch. chatloop runs the calls of a batch at the same time, so run_workflow could read the file before the write. run_workflow now uses SerialToolCalls and runs after the other calls in its step.

---
_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
The PR conflicted with main, so GitHub ran no pull_request CI. The one conflict is the root tool list in turn_environment.go: it keeps the new acpTools from main and adds the workflow tools last.
ethanndickson added a commit that referenced this pull request Nov 2, 2026
…es (#28758)

Moves the subagent tools' logic (`spawn_agent`, `wait_agent`,
`message_agent`, `interrupt_agent`, `list_agents`,
`list_subagent_models`) out of large inline closures in `subagent.go`.
Child chat operations become typed `*Server` functions in
`subagent_operations.go` that other code can call, such as the workflow
prototype in #31189. The tools become adapters that parse arguments,
apply defaults and build the JSON.

## Changes

- `subagent_operations.go`: operations take chat IDs, a timeout or a
busy behavior and return plain values. `awaitSubagent` wraps
`awaitSubagentCompletion` with the computer_use desktop recording and a
last status check, and returns the chat, report and recording, a
timeout, or a `*subagentStatusError`. `sendSubagentMessage` and
`interruptSubagent` are main's, unchanged. `listSubagents` returns
unarchived children, most recently updated first. The helpers they call
move from main unchanged.
- `subagent.go`: keeps main's model override resolution. Each tool
parses `chat_id`, applies the wait timeout or list paging defaults,
reads the target chat so errors carry its `type`, and builds the JSON.
`spawnAgent` validates the `spawn_agent` arguments.
- Tool outputs are typed structs whose fields follow sorted key order,
so the JSON the model sees is byte-identical to main's map output.

## Behavior

No intended behavior change. Error strings, tool names, descriptions and
schemas, hook fail-closed handling, the wait loop and recording lifetime
match main. `wait_agent` reads the target chat once more, for the
recording decision.

> Xum (an AI coding agent) rebuilt this PR on behalf of @ibetitsmike.

---------

Co-authored-by: Ethan Dickson <[email protected]>
…low errors

A missing script_path file returned the agent's internal request URL,
because the codersdk.Error from ReadFile prints its method and URL. The
error now reads `read script_path "<path>": <message>`, plus the detail
when the agent sends one.

A compile error named workflow.star even when the script came from a
file. compileWorkflow now uses the script_path as the file name, so
compile errors and backtraces name the file the model edits.
run_flow is a declarative alternative to run_workflow for an A/B test
of the two formats. A flow is a JSON graph: nodes with prompt templates
and step schemas, edges with CEL conditions, and an optional CEL output.
Any program in the workspace can write the JSON and pass it in
flow_path, which reads like script_path.

The run lifecycle stays shared. startWorkflowRun now takes a compiled
workflowEngine, which the Starlark program and the new flowProgram both
implement. The flow runner walks the graph with one cursor: it renders
each node, starts its steps through startStep, waits for them, and
takes the first edge whose when is empty or true. for_each nodes start
one step per item at once and keep null for a failed item. max_visits
bounds loops. Resume reuses the step chats because the rendered specs
repeat, as for scripts.

compileFlow checks the whole manifest before any step runs and returns
every problem in one sorted list: unknown fields, bad IDs, types,
schemas and max_visits, CEL parse and type errors, unknown nodes.<id>
references, template syntax, dangling edges, unreachable nodes and the
step budget. Each place in a flow has its own CEL environment, so item
outside a for_each node or value outside when is a type error.

await_workflow serves both tools. run_workflow and its description are
unchanged.

This branch was previously deployed

1 inactive deployment
main — cef87bb7 Deployed Nov 20, 2026 by ThomasK33 via cla #260923
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant