编写和检查 Workflow
Workflow 是一个小 Python 包:Agent 实例是角色,Context 对象决定每次模型调用看到什么,普通函数决定调用顺序。openprogram programs check 通过后,入口就是可调用的工具。想演示而不是编写:录制演示。
README.md 里的约定
| 项目 | 必须写明 |
|---|---|
| 目的 | 具体任务和完成所需的证据 |
| 输入 | 入口参数、所需上下文、无效输入的行为 |
| 输出 | 返回类型、产出文件、如何验证成功 |
| 影响 | 模型、工具、文件、网络和外部提交行为 |
| 失败和取消 | 失败或中断后留下什么;重试是否安全 |
| 依赖 | 导入的 Workflow、需要的工具和服务 |
| 测试 | 行为检查、脚本化模型回复、单独的实机检查 |
| 用法 | 一次可复现的调用和预期结果 |
函数保持小。显式处理预期的无效输入,让运行时取消向上传递。
目录
~/.openprogram/programs/project_report/
├── .git/
├── README.md
├── __init__.py
├── workflow.py
├── steps/
│ └── prepare.py
└── tests/
└── test_workflow.py
目录名就是包名:小写 Python 标识符,不能是标准库模块或其他可导入的包。每个 Workflow 是自己的 Git 仓库。用自己的名字导入(project_report.steps.prepare);相对导入和导入其他 Workflow 都可以。
角色、Context、函数
| 对象 | 规则 |
|---|---|
| 角色 | 模块顶层的 Agent。只含配置:model、instructions、tools、effort。没有模型时用调用者的。 |
| Context | 在函数内构建,显式传给角色调用 |
| 函数 | 普通 Python:调用角色、转换、分支、循环 |
"""Prepare a project report from verified notes."""
from openprogram import Agent, Context
writer = Agent(instructions="You write project reports.")
reviewer = Agent(instructions="You check facts.", tools=[])
def project_report(notes: str) -> str:
"""Turn this week's project notes into a report."""
ctx = Context({"notes": notes})
draft = write_draft(ctx)
return check(draft, ctx)
def write_draft(ctx: Context) -> str:
"""Write three sections: progress, problems, plans. At most three items each."""
return writer("Write this week's report", context=ctx)
def check(draft: str, ctx: Context) -> str:
"""Only check that the draft matches the notes. Do not change the style."""
return reviewer("Fix any mismatch and return the final text",
context=ctx.derive({"draft": draft}))
PROGRAM_ENTRIES = [project_report]
role(prompt, context=ctx)用角色的工具运行一轮模型;role.choose(prompt, {id: label}, context=ctx)返回选项 ID。Workflow 代码不调用自由函数llm()或agent()。PROGRAM_ENTRIES列出工具。每个需要文档字符串(工具描述)和每个参数的类型注解(工具 schema)。- 没有装饰器、基类或分发表。用普通导入使用其他 Workflow。
一次模型调用看到什么
| 部分,按顺序 | 由谁定义 |
|---|---|
角色的 instructions,作为系统提示 | Agent(...) |
| 历史,默认空 | ctx 的 history |
| 发起调用的 Workflow 函数的文档字符串 | 该函数 |
ctx 的命名块,每个是 name: 加值 | 构建或派生 ctx 的代码 |
prompt | 这次调用 |
其他都不进入:没有聊天历史、召回的记忆或其他角色的输出。Context 不改变工具或权限。文档字符串取调用栈上最近的 Workflow 包内函数,只作用于这一次请求;没有文档字符串的辅助函数不添加内容。
Context
| 写法 | 效果 |
|---|---|
Context({"notes": notes})、ctx["notes"] = notes | 块 |
Context(providers={"today": lambda c: ...}) | 每次调用时求值的同步函数 |
ctx.derive({...}) | 独立副本,调用者的 ctx 不变 |
history=None | 无历史,角色调用的默认值 |
history="thread" | 用同一个 ctx 对象的之前调用 |
history="chat" | 调用此 Workflow 的对话 |
history=可调用对象 | (node, context) -> bool 选择对话节点 |
历史写在 Context(..., history=...) 或 ctx.derive(history=...)。
内置 provider 告诉模型它的调用在程序的什么位置:
from openprogram.runtime.context import call_path, workflow_source
ctx = Context(providers={"call path": call_path, "program": workflow_source})
call_path 列出栈上的 Workflow 函数(project_report → check);call_path(docs=True) 加上文档字符串。workflow_source 返回入口的源码。普通块如 {"flow": "Draft first, then review."} 也可以。
入口选项
ENTRY_OPTIONS = {
"project_report": {
"input": {"notes": {"description": "This week's notes", "multiline": True}},
},
}
ENTRY_OPTIONS 把入口映射到工具选项。"run_in_worker": True 在 worker 而不是 fork 的子进程里运行,用于浏览器会话这类 worker 持有的状态。
重启后恢复
from openprogram.runtime.agent.durable_steps import step
def project_report(notes: str) -> str:
"""Turn this week's project notes into a report."""
draft = step("draft", write_draft, notes)
return step("check", check, notes, draft)
ENTRY_OPTIONS = {"project_report": {"resumable": True}}
| 规则 | 说明 |
|---|---|
| 步骤 | 运行一次并保存结果。继续时入口从头运行,完成的步骤返回保存的结果,角色调用不重复。 |
| 步骤之外 | 会再次运行:保持确定性和无副作用,步骤名和顺序稳定 |
| 值 | 参数和结果是 JSON。Context 不能保留,在步骤内构建。 |
| 继续 | 重启:在 execution.auto_resume_window_seconds 内自动。暂停:从聊天继续。 |
| 代码变化 | 运行绑定提交。execution.code_change_policy:keep_original(默认)以 "Workflow code changed" 停止;use_latest 继续,已完成的步骤被移除或重排时停止。 |
| 限制 | 必须同步;不能用 run_in_worker |
执行参数
用户给任务、证据和交付物,不配置模型 ID、推理强度、迭代次数、超时、token 预算或重试次数。
| 情况 | 规则 |
|---|---|
| 固定值 | 放在角色或代码里 |
| 取决于任务的值 | 一次角色调用生成完整计划;代码校验键、类型、范围和限制关系,失败时回退到校验过的默认值 |
| 优先级 | 用户约束和应用限制优先于模型输出 |
| 恢复 | 可恢复的 Workflow 把计划存入检查点 |
行为测试
from openprogram.programs._authoring.testing import script_roles
from project_report import workflow as wf
def test_reviewer_sees_the_draft_but_not_the_writer_prompt():
with script_roles({wf.writer: ["Draft"], wf.reviewer: ["Final"]}) as calls:
assert wf.project_report("Shipped the parser.") == "Final"
request = calls[wf.reviewer][0]
assert "Do not change the style" in request.text # check() docstring
assert "Draft" in request.text # the draft block
assert "Write this week's report" not in request.text
assert request.history_text == ""
- 回复是字符串或可 JSON 序列化的值(用于
response_format);role.choose的回复是选项 ID。 - 脚本里的异常由调用抛出并留在队列,也会回答模型循环的重试。
request.text:指令、文档字符串、块、提示。request.history_text:历史。- 用
monkeypatch替换工具、文件和服务。实机服务商检查不能在 check 时运行,不要放进tests/。
检查和列出
openprogram programs check ~/.openprogram/programs/project_report
openprogram programs check ~/.openprogram/programs/project_report -m "Tighten the review step"
openprogram programs list
- 在单独进程导入包,校验
PROGRAM_ENTRIES和签名。 - 用 pytest 运行
tests/,有系统沙箱时在沙箱里:无网络、包只读、HOME是空临时目录。 - 全部通过后提交到 Workflow 的仓库,并把该提交记录到
refs/openprogram/checked。-m设提交信息;--json打印报告。
只有通过检查的提交会加载:普通的 git commit 不会,有未提交修改时继续服务通过检查的版本,重启后也一样。worker 监视 ~/.openprogram/programs,几秒内重载。programs list 和 Programs 页显示 loaded、error(带错误)或 pending(带原因,例如还没有通过检查的提交)。历史和回滚用 Git。
| 情况 | 行为 |
|---|---|
| 运行位置 | ~/.openprogram/programs-cache/<name>/<commit>/ 中通过检查的提交副本。代码旁读取的文件必须已提交;写入的内容放在包之外。 |
| 新版本导入失败 | 之前的版本继续运行。状态 error,备注 "Kept version <old>; version <new> failed."。Programs 页在标题下方的红色提示条里显示它,后面是原因和查看完整错误链接。 |
| 仍有调用在运行 | 新版本最多等待 30 秒,超时后下一轮再试。 |
运行
| 从 | 方式 |
|---|---|
| 聊天 | 每个入口是一个工具 |
| Programs 页 | Abilities → Programs,在 Applications 下选择,Use 在聊天里打开表单;发送前不运行 |
| 命令行 | openprogram programs run <entry> -a key=value |
| Python | 可导入函数 |
每次入口调用记录在会话图里,每次角色调用是一个子节点,保存确切的请求。普通函数不记录。