Skip to main content

mainspring-workflow

图编排与多 agent 外挂(T4-2):条件节点、并行 fan-out + reducer、supervisor、 agents-as-tools(委托)与 handoff(控制权转移)。只复用 core 的 Runner / Session / Hook / Tracing。

设计小节

  1. 工作流不是第二套 runtime:节点准备输入 → 调 core Runner.run;会话/Hook/追踪全部随 Agent 与 Runner 走。
  2. 根 run 虚拟 wf_<hex>;子 run 用 RunConfig.parent_run_id 挂回根,OTel 父子与用量归集的唯一接缝。
  3. fan-out 分支各拿浅拷贝 state,asyncio.gather + 信号量(默认 50),reducer 按声明顺序聚合。
  4. 路由优先级:节点显式 next > 条件边 router > 静态边 > 隐式插入顺序;max_steps 防死循环。
  5. agents-as-tools = 委托取结果(专家独立会话、只带自己的工具,结论回填主线)。
  6. handoff = 控制权转移(工具只登记,编排层执行目标并丢弃调用者终答)。
  7. 子 agent 挂起(待审批)时工作流显式报错,不把 pending 当结论。

fan-out + reducer

from mainspring_workflow import FunctionNode, Workflow

wf = Workflow(runner=runner)
wf.add_parallel(
    "research",
    [FunctionNode(f"source-{i}", lambda state, i=i: state["input"] + f" via {i}") for i in range(50)],
    reducer=lambda outs: "\n".join(outs),
)
result = await wf.run("调研主题")
print(result.usage)          # 所有分支用量归集到根

supervisor

from mainspring_workflow import Supervisor

result = await Supervisor(dispatcher, [search_agent, writer_agent], runner=runner, max_rounds=5).run(task)
# result.worker_runs / result.handoffs 可审计;调度 agent 的终答只在它不再转交时生效

委托与 handoff

from mainspring_workflow import agent_as_tool, handoff_tool

parent = Agent(..., tools=[agent_as_tool(expert)])          # 委托:结论回到主线
caller = Agent(..., tools=[handoff_tool(expert)])           # 转移:调用者终答作废

验收覆盖(tests/test_workflow.py):条件路由、50 路并发 barrier、Session/Hook/Tracing 复用、 parent_run_id 全事件注入、Usage 归集、专家上下文隔离、handoff 覆盖终答、supervisor 轮次、循环护栏。

Metadata

Release files for mainspring-workflow 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mainspring-workflow 0.1.2
File Size Uploaded Provenance Actions
mainspring_workflow-0.1.2.tar.gz 11.9 kB With provenance Details for mainspring_workflow-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mainspring-workflow 0.1.2
File Interpreter ABI Platform Actions
mainspring_workflow-0.1.2-py3-none-any.whl Python 3 none any Details for mainspring_workflow-0.1.2-py3-none-any.whl

Total release size: 27.2 kB

Release files / mainspring_workflow-0.1.2.tar.gz

Download URL mainspring_workflow-0.1.2.tar.gz
Size 11.9 kB
Tags Source
SHA-256 checksum
How to use checksums
6805df1c40c7118241dcee77ba69cb448934b283dfea0d309cb071abad7d536f
BLAKE2b-256 checksum
How to use checksums
1b34a297923a3b031141fca3117273b79fecb7c1f3cac2bbbce7ca0adc28c661
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 6, 2026.

Transparency log

Release files / mainspring_workflow-0.1.2-py3-none-any.whl

Download URL mainspring_workflow-0.1.2-py3-none-any.whl
Size 15.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
110377d5ab0c1043b02b398ee8df51c7f325e2e81adf7a59bcb2c27fde333111
BLAKE2b-256 checksum
How to use checksums
91e367b52bec7c6eab04eeea4f5467ef4ea41098f876cffda815c3c4543b7e22
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 6, 2026.

Transparency log

Security for mainspring-workflow 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for mainspring_workflow-0.1.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 6, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page