Workflowsdoc/{zh,en}/design/runtime/workflow.md

Workflows in Starlark

简体中文 · Status: W1–W2 built 2026-11-26 (section 9); W3–W4 to come. What runs today: external runtimes sections 6d–6e. Decision: ADR 0014.

简体中文 · Status: W1–W2 built 2026-11-26 (section 9); W3–W4 to come. What runs today: external runtimes sections 6d–6e. Decision: ADR 0014.

1. Why

The workflow runtime (R4) runs a list of five step kinds from YAML, with a hand-written template language ({{#if review}}, {{history}} …). Real use has shown its limits:

  • ◆It cannot branch. It has no early exit, no "ask a person when the reviewers disagree", and no "if the tests fail, fix them before reviewing again".
  • ◆Every new need becomes another template variable ({{roles}}, {{history}}) or another step option.
  • ◆The workflow keeps its own copy of the goal's state (goals/<id>.json, the summary trigger), beside the Host's. The two judge the same round twice, and can disagree.

The owner's decisions of 2026-11-26:

  1. Control flow is code, in Starlark. Starlark is the Python dialect that Bazel uses (go.starlark.net, pure Go). It is deterministic and has no file, network, clock or randomness access. It replaces the step language; the two shipped workflows are rewritten in it.
  2. The Host stays the judge. A script cannot declare its goal met. The Host judges each round from its own records, as R4b does.
  3. Static and natural-language workflows on one engine (the owner, 2026-11-26). An operator writes a script in the config and people call it with /command. Or a person describes a workflow in chat, and the assistant writes the script and starts it with a run_workflow tool (section 3a). Both run the same way.
  4. A thorough refactor: the step language is deleted, with no compatibility path; every existing workflow is rewritten in Starlark.
  5. Not now: per-call model, effort, tools or instructions. They have no scenario yet (AGENTS.md section 11).

2. A script

A workflow instance points at a script, inline or in a file:

yaml
- id: develop
  plugin: workflow
  uses: [claude, codex, kimi, localenv]
  config:
    command: develop
    roles: {dev: claude, review: codex}
    model: kimi              # chat()'s default
    script_file: develop.star

A script defines round(ctx) to run as a goal, or main(ctx) to run once. Defining both is a load error. It may also define report(ctx), which writes the final reply after a goal ends. develop.star:

python
def round(ctx):
    last = ctx.state.get("review")
    task = ctx.task
    if last:
        task += "\nFix these review findings:\n" + last
    run("dev", ctx.roles["dev"], task, session="dev")
    test = check("tests", ctx.params.get("check", "go test ./..."))
    if not test.passed:
        # Tests first: no review of a change that does not build.
        run("dev-fix", ctx.roles["dev"], "The check failed:\n" + test.tail, session="dev")
        test = check("tests-again", test.command)
    diff = shell("diff", "git --no-pager diff --stat; git --no-pager diff").out
    rev = run("review", ctx.roles["review"], review_prompt(ctx.task, diff, test),
              review=True, workspace="read-only", session="review")
    return {"review": rev.findings}   # the next round's ctx.state

def review_prompt(task, diff, test):
    return "Review this change for: %s\nTests: %s\n%s" % (task, test.summary, diff)

def report(ctx):
    return chat("report", "Summarize for the person who asked: " + ctx.task + "\n" + ctx.history)

ask-all.star is a main with parallel and chat.

2.1 What a script can call

Every call that reaches the Host is keyed. The key is the call's first argument, unique within the run. A run that restarts replays the script from the start, and each keyed call returns its stored outcome instead of doing it again (section 4).

CallDoesReturns
run(key, runtime, text, session="", workspace="task", review=False)runs a runtime as a child (ADR 0013); review=True reads the reply as findings and asks once more if it does not parse; workspace="read-only" as built (#23).text, .ok, .error, .runtime, and .verdict and .findings for a review
parallel(calls)several runs at once, each a dict of run's arguments; waits for all of them; one failing does not stop the othersa list in the calls' order
check(key, command)runs the command through the run's shell tool; the Host judges the round by the last exit code of each command.passed, .exit, .tail, .command, .summary
shell(key, command)the same, but not judged, e.g. a diff.exit, .out
chat(key, prompt, model="")one model call (the config's model by default)its text
ask(key, prompt, options=[])a question to the person who started it, as a durable interactionthe answer's text
progress(text)a progress line—
attach(name, text)a file for the final reply—
stop(reason)ends the goal as blocked, with the reason (the agent's goal tool does the same)—

ctx is frozen: task, roles (each role's runtime, after the call's role= overrides), params (from the config), round, state (what the last round returned), history (one line per earlier round, written by the Host: the round's checks and review verdicts), locale.

Other runtimes' replies are data, never instructions. run returns them as they are, and a script that puts them into another runtime's input wraps them with quote(result). quote marks the text as untrusted, the same way sub-agent answers are (results.Untrusted).

2.2 The sandbox

  • ◆Builtins: only json is predeclared; load is refused. There is no file, network, environment, time or randomness access.
  • ◆Language: while and recursion are refused. A loop runs over a list or a range, and a round's code cannot loop forever.
  • ◆Limit: a step cap (max_exec_steps, default 1,000,000 Starlark steps per run) stops a runaway script with workflow.step_limit.
  • ◆Checked at load: the script must parse, and every runtime it names in roles must be in uses; the owner's R4 answer applies. A script error makes the instance unavailable, with its line.

3. The goal, judged by the Host

round makes the command's target goal, as in R4b. A round is one run of the workflow.

  1. /develop <task> checks the call (plugin.CommandChecker). Then the Host sets the session's goal on the workflow's runtime and freezes the script in it: the new goals.plan column holds the source and the call. A config change does not alter a goal already running.
  2. Each round runs round(ctx). What it returns is saved as the goal's state (the new goals.round_data column, through the new plugin.Result.Data), in the same transaction that settles the round. This replaces the workflow's goals/<id>.json.
  3. The Host judges the round from its own records, as R4b does today:
  • ◆met: for each check command, its last run in the round passed (a check that failed, was fixed and then passed counts as passed), and every review=True child passed;
  • ◆blocked: the same findings three rounds in a row, the same failing check three times, a failed round, or stop(reason);
  • ◆exhausted: the goal's budget is used up;
  • ◆cancelled: /goal stop, /cancel or /new.
  1. The final reply (new): when a goal on a workflow ends, met, blocked or exhausted, the Host starts one more run on that workflow. In it RunInput.Goal carries the goal's end state and reason, and the workflow runs report(ctx); without report, the Host's notice alone ends the goal. The workflow never guesses whether the goal ended, so its private copy of the rules goes away.

3a. A workflow built from natural language

A person says, for example: "have Codex and Claude each review this repo, keep the findings both agree on, have Claude fix them, and run the tests until they pass". The assistant writes a Starlark script for it and calls the run_workflow tool.

  • ◆The tool: the workflow plugin also has the tool role, and its run_workflow tool takes task, script (at most 32 KiB, defining round or main) and roles. The agent offers it only when its uses include a workflow instance. The tool description gives the calls of section 2.1 and an example, written for the model. A workflow instance without script, script_file and command runs only these scripts: it has no command and is not offered as a child runtime (plugin.ChildOffer).
  • ◆The same checks as a static script: the script must parse and pass the sandbox rules, and it may name only runtimes that are in that workflow instance's uses and that the person may use. A refused script returns its error with the line number, and the assistant corrects it.
  • ◆The person approves first: before anything runs, the person gets an approval card. Its summary is read from the script itself, not from the model: the task, each role's runtimes, every check and shell command (written out, or "(computed)" when the script builds it), and whether it runs as a goal or once. The script follows on the card (its first 4,000 characters), and workflow.star comes with the assistant's reply. The card's key is the digest of the task, script and roles, so an approval starts only that plan. This holds for every script the assistant writes; the owner may later narrow it to scripts that write or run commands. Rejecting the card starts nothing, and the rest of that request gets no new card (workflow.rejected): the assistant says it was not started. The tool declares asks (ToolSpec.Asks: it shows its own card and the Host adds none), so the agent tells the model that it asks for approval before it runs.
  • ◆It runs as its own goal or run: after approval the workflow starts itself through plugin.SelfStarter on its tool host: the Host sets a goal for a round script, with the script frozen in goals.plan and its first round queued behind the assistant's run as later rounds are, or starts one run for a main script with the plan in runs.plan (RunInput.Plan). The tool returns at once with "started" and the goal's or run's id; progress and the report arrive as for /develop, and /goal show|stop|continue work. While the conversation has an active goal, every call is refused with goal.active before any card. The same plan asked again while its goal or run is active returns that one. The assistant's own run is not held open. A call from a child run is refused, so the depth of children stays one (ADR 0013).
  • ◆Judged as any goal: a script written by a model is not trusted any more than a static one; the Host's rules of section 3 decide. Its budget is the goal's, within the connection's goal_limits.

4. State, recovery and failures

StateOwner
goal, plan, state between rounds, judgmentthe Host (goals)
each child and its resultthe Host (run_children, ADR 0013)
each check and shell callthe Host (tool ledger, tool_calls)
each chat, ask and attach within a runthe workflow's checkpoint (memo) and the Host's interactions
the script's local variablesnobody: rebuilt on each replay
  • ◆Restart: the round's run resumes and replays round(ctx) from the top. Finished children, checks and chats return at once from storage; a child that was still running resumes from its own checkpoint. Because Starlark is deterministic, the same calls come in the same order.
  • ◆Parked question: an ask the person has not answered parks the run, as any question does. The answer resumes it, and the replay reaches the same ask, which now returns the answer.
  • ◆A child fails: run returns .ok = False and .error; the script decides what to do. An uncaught script error, or workflow.step_limit, fails the round, and a failed round blocks the goal.
  • ◆Cancel: /cancel cancels the run, its children and the script's thread.
  • ◆Budgets: external runtimes' usage is counted against the goal, not only recorded (section 6).

5. Sub-agents: on the same engine?

The owner asked whether the agent's sub-agents should move onto Starlark too. Recommendation: no. They should share the layer underneath, the Host's ChildRunner, not the scripting:

  • ◆Who decides. Sub-agents are chosen by the parent model, mid-conversation (delegate). Starlark is for flow an operator writes in advance. Putting a script between the model and its sub-agent adds a language without adding control.
  • ◆What they need is already there. Sub-agents have instructions, a model, a tool subset, a step limit, a timeout with a last answer, prompt patches, parallel readers, and worktree isolation with merge. A workflow can already use an agent instance as a runtime, via run(key, "agent-reviewer", …). A configured sub-agent profile cannot be named that way yet; that would be a small later step.
  • ◆Where they could converge. Sub-agent state lives in the parent's checkpoint, and cross-runtime children live in the Host's run_children. The two could merge, so a sub-agent becomes a Host child, but only once a scenario needs it. Such a scenario would be a sub-agent resumed alone after a restart, or one shown in /goal show. An agent running as a Host child also needs childHost.Commit to save its state, which is a no-op today.

So the agent keeps delegate for one task at a time. Fixed fan-outs and loops go into a workflow script.

6. Contracts and changes

  • ◆New dependency: go.starlark.net (BSD-3-Clause, pure Go). It needs no other runtime and builds the same on macOS, Linux and Windows.
  • ◆pkg/plugin: Result.Data (JSON, optional; saved by the Host for a goal's round) and GoalView.Plan, GoalView.Data, GoalView.Ended, GoalView.Reason (the final run).
  • ◆Host:
  • ◆Schema: goals.plan and goals.round_data (one migration).
  • ◆The final run: a goal on a workflow gets one more run when it ends.
  • ◆Budgets: ReportUsage counts against the goal's budget.
  • ◆Review flag: run_children.review stays, set by run(review=True).
  • ◆plugins/workflow:
  • ◆Added: starlark.go (the script thread, replay, step cap), calls.go (the calls in 2.1, wired to the Host), values.go (Go ↔ Starlark values).
  • ◆Removed: the step language (steps, the templates, goal.go).
  • ◆Shipped: develop.star and ask-all.star, as examples.
  • ◆Error codes: workflow.script_error (with the line; a syntax error also quotes what is on it), workflow.step_limit, workflow.role_denied, workflow.model_denied, workflow.no_children, workflow.no_shell.
  • ◆Size: about 1,100 lines leave and about 1,200 come in (W4 included). Production code now stands at about 71.1k lines of the 75k budget.
  • ◆Compatibility:
  • ◆Old config: removed outright. steps: is an unknown field and the instance does not load. The owner's bot, the acceptance apps, the tests and the veruqelo cases that embed it are all rewritten in the same change; the bot's config switches with the build that refuses the old one.
  • ◆Unchanged: /develop, /ask-all, role=, [role=…], summary= (now the default model of chat), /goal show|stop|continue.

7. Slices and acceptance

SliceContentAcceptance
W1Host: goals.plan and goals.round_data, Result.Data, the final run, usage counted against the goalengine tests: a round's state reaches the next round; the final run gets the end state; a restart mid-round neither reruns finished children nor loses the state
W2Starlark engine and calls; develop.star and ask-all.star; the step language removedunit tests per call, replay after a crash, step_limit, a script error with its line; the console-develop and console-askall cases pass with the .star versions (met in two rounds, the read-only reviewer, /goal stop, a failing member)
W3the owner's bot moved to .star; docs and skillsthe bot's /develop and /ask-all in Feishu
W4run_workflow: the tool role, the approval card with the Host's summary, the goal or run it startsreal e2e: a workflow described in chat is written, approved and run, and judged by the Host; a refused card starts nothing; a script naming a runtime the person may not use is refused with its line

8. Questions for the owner

  1. Where scripts live: built as both, script_file (relative to the config file) and inline script.
  2. The final reply: with report the reply is the script's; without it, only the Host's notice. Should the shipped scripts always have report? (Recommended: yes.)
  3. while: refused (recommended, so a round cannot spin), or allowed under the step cap.

9. Built: W1–W2 (2026-11-26), W4 (2026-11-27)

  • ◆W1, Host:
  • ◆goals.plan and goals.round_data (migrations 49–52, after the schedules; #30 repaired a database marked past them). CommandChecker.CheckCommand returns the plan the goal freezes. Each round's Result.Data reaches the next as GoalView.Data.
  • ◆The report run (goal:report) after a workflow goal ends, with GoalView.Ended and Reason.
  • ◆ReportUsage counted through usage.Add.
  • ◆The judge takes each check command's last run in the round. Config.BaseDir reaches plugins as Env.ConfigDir.
  • ◆Test: TestGoalPlanDataAndReport.
  • ◆W2, plugins/workflow:
  • ◆config.go, script.go (the sandbox), values.go, calls.go (run, parallel, quote), hostcalls.go (check, shell, chat, ask, progress, attach, stop) and workflow.go.
  • ◆ctx.roles is a dict (ctx.roles["dev"]); a role configured as a list stays a list.
  • ◆The workflow keeps the round's script return and a history line per round in the round data.
  • ◆The step language is deleted. The shipped scripts are examples/workflows/develop.star and ask-all.star.
  • ◆Tests: plugins/workflow (load checks, ask-all.star, a develop.star round, the step cap, stop, report); the engine's TestWorkflowGoal* and TestPreflightWorkflowRoles on scripts.
  • ◆W4, run_workflow:
  • ◆pkg/plugin: SelfStarter (StartRuntimes, StartSelf with Check), ChildOffer, and RunInput.Plan; runs.plan (migration 53).
  • ◆Host: internal/host/engine/selfstart.go; childChoices lists child runtimes for ChildRuntimes and goal commands alike.
  • ◆plugins/workflow: tool.go (the checks, the card, the start), summary.go (the card's summary from the syntax tree), guide.md (how to write the script, which a call with task alone returns, so that the tool's description every request carries stays short; context refactor slice 2b); a workflow without a script runs the plan it is given.
  • ◆Tests: TestRunWorkflowChecksTheScript, TestRunWorkflowCardAndStart, TestScriptlessRunsFrozenPlan, the engine's TestWorkflowStartsItself; e2e on console-agent-workflow.