:Worker 并发机制、执行模式选择与实时进度监控)
在 Goose 中并行运行子配方Subrecipes In ParallelWorker 并发机制、执行模式选择与实时进度监控【免费下载链接】goosean open source, extensible AI agent that goes beyond code suggestions - install, execute, edit, and test with any LLM项目地址: https://gitcode.com/GitHub_Trending/goose3/goosegoose 的 子配方Subrecipe 机制允许把复杂工作流拆成可复用的独立任务。当任务彼此独立时将它们并行执行可以大幅缩短整体耗时。本文基于 goose 官方教程Running Subrecipes In Parallel系统讲解并行执行的内部机制、默认行为与覆盖方式in parallel/sequentially/sequential_when_repeated、可复制的完整 YAML 配方示例以及 CLI 中的实时进度仪表盘。读完你可以在自己的配方中安全地开启并行子配方并在需要串行时准确做出配置选择。:::warning 实验性功能 并行子配方是一个正在积极开发中的实验性功能其行为与配置可能在后续版本中发生变化。在生产工作流中大规模使用前请先在低风险任务上验证。 :::并行子配方的典型应用场景goose recipes 可以使用相互隔离的 worker 进程并发执行多个子配方实例从而支持高效批处理、不同任务的并行加工以及复杂工作流更快完成。以下是几个常见场景Monorepo 构建失败诊断一次 monorepo 构建中有 3 个服务失败时用同一个 “diagnose failure” 子配方分别传入每个构建 URL并行诊断全部失败文档批量摘要处理一个包含文档链接的 CSV 文件时对每一行链接同时运行一个 “summarize document” 子配方跨仓库代码分析同时对数个代码库执行安全、质量与性能分析。在深入了解配置方式前可以先回顾 Goose Subrecipes 指南其中说明了sub_recipes字段name/path/values、参数处理方式与“子配方不能嵌套”等基础规则——并行机制正是建立在这些概念之上的。并行执行如何工作并行子配方执行使用一套隔离的 worker 系统来自动管理并发任务goose 为每个子配方实例创建独立任务并将它们分配到最多10 个并发 worker上执行。从源码结构看这种隔离体现在执行模型层面在 crates/goose/src/execution/manager.rs 中执行模式被划分为Interactive、Background与SubTask { parent_session }三种子配方 / 委托任务以SubTask形式运行并绑定父会话 ID。每个子任务在独立 session 中执行、拥有独立上下文不共享会话历史、记忆或状态相关约定见 subrecipes.md因此并发执行不会互相污染。goose 决定某个场景“如何并行”的依据取决于你运行的是不同子配方还是同一子配方的多个实例。下表概括了默认行为与覆盖手段场景默认行为覆盖方式不同子配方串行在 prompt 中加入 “in parallel”同一子配方不同参数并行• 设置sequential_when_repeated: true• 在 prompt 中加入 “sequentially”情况一运行不同的子配方当一次运行涉及多个不同子配方时goose 按以下顺序判定执行模式用户的显式请求在 prompt 中写明 “in parallel” 或 “sequentially”默认串行执行除非显式要求并行否则不同子配方一个接一个运行。因此只需在调用不同子配方的 prompt 中自然提及 “in parallel”goose 就会并行调度它们prompt: | run the following subrecipes in parallel: - use weather subrecipe to get the weather for Sydney - use things-to-do subrecipe to find activities in Sydney情况二运行同一子配方的多个实例不同参数当 prompt 暗示要对不同参数重复执行同一个子配方时goose 会识别这一意图并自动创建多个并行实例。判定顺序为配方级配置sequential_when_repeated置为true时强制串行见下文 选择执行模式用户请求在 prompt 中用 “sequentially” 覆盖默认的并行行为默认并行同一子配方的多个实例默认并发运行。例如下面的 prompt 暗示需要多次执行天气子配方prompt: | get the weather for three biggest cities in Australiagoose 会识别出 “three biggest cities” 意味着针对不同城市多次运行 weather 子配方于是并行执行它们。如果你希望它们逐个串行完成直接告诉 goose 即可prompt: | get the weather for three biggest cities in Australia one at a time实时进度监控在 CLI 中并行运行多个任务时执行期间会自动出现一个实时仪表盘dashboard用于跟踪进度实时进度跟踪实时监控任务完成情况提供 completed已完成、running运行中、failed失败、pending待处理等统计数据任务详情可查看每个任务的唯一任务 ID、参数集、执行耗时、输出预览以及错误信息任务会依次经历Pending → Running → Completed/Failed状态流转。这让“同时推进几十个子任务”不再是一个黑盒哪条卡住、哪条报错、参数是什么都可以在仪表盘上一眼定位。完整示例一并行运行两个不同的子配方以下plan_trip.yaml主配方并行调用weather与things-to-do两个子配方来规划悉尼之行# plan_trip.yaml version: 1.0.0 title: Plan Your Trip description: Get weather forecast and find things to do for your destination instructions: You are a travel planning assistant that helps users prepare for their trips. prompt: | run the following subrecipes in parallel to plan my trip: - use weather subrecipe to get the weather forecast for Sydney - use things-to-do subrecipe to find activities and attractions in Sydney sub_recipes: - name: weather path: ./subrecipes/weather.yaml values: city: Sydney - name: things-to-do path: ./subrecipes/things-to-do.yaml values: city: Sydney duration: 3 days extensions: - type: builtin name: developer timeout: 300 bundled: true运行方式与其他配方一致以子配方示例为准见 subrecipes.mdgoose run --recipe plan_trip.yaml完整示例二同一子配方并行处理多组参数下面的multi_city_weather.yaml会针对澳大利亚三大城市并行运行三个weather子配方实例# multi_city_weather.yaml version: 1.0.0 title: Multi-City Weather Comparison description: Compare weather across multiple cities for trip planning instructions: You are a travel weather specialist helping users compare conditions across cities. prompt: | get the weather forecast for the three biggest cities in Australia to help me decide where to visit sub_recipes: - name: weather path: ./subrecipes/weather.yaml extensions: - type: builtin name: developer timeout: 300 bundled: true提示这里sub_recipes没有写死values城市列表由 AI 从 prompt 的自然语言中抽取并生成多个子任务。如果你需要固定参数例如固定城市集合可在sub_recipes条目中声明values预置值会优先于上下文抽取的参数见 subrecipes.md。配套子配方weatheryaml # subrecipes/weather.yaml version: 1.0.0 title: Find weather description: Get weather data for a city instructions: You are a weather expert. You will be given a city and you will need to return the weather data for that city. prompt: | Get the weather forecast for {{ city }} for today and the next few days. parameters: - key: city input_type: string requirement: required description: city name extensions: - type: stdio name: weather cmd: uvx args: - mcp_weatherlatest timeout: 300 things-to-doyaml # subrecipes/things-to-do.yaml version: 1.0.0 title: Things to do in a city description: Find activities and attractions for travelers instructions: You are a local travel expert who knows the best activities, attractions, and experiences in cities around the world. prompt: | Suggest the best things to do in {{ city }} for a {{ duration }} trip. Include a mix of popular attractions, local experiences, and hidden gems. {% if weather_context %} Consider the weather conditions: {{ weather_context }} {% endif %} parameters: - key: city input_type: string requirement: required description: city name - key: duration input_type: string requirement: required description: trip duration (e.g., 2 days, 1 week) - key: weather_context input_type: string requirement: optional default: description: weather conditions to consider for activity recommendations 两个子配方都用{{ city }}、{{ duration }}这类模板变量接收主配方传入的参数things-to-do还演示了用{% if weather_context %}做条件分支——当上个任务带回天气结果时让推荐更贴合实际天气。若需要把多行值安全嵌入 YAML可使用indent()过滤器例如{{ content | indent(2) }}。sub_recipes 字段与配方级配置sub_recipes数组中每个条目的字段如下完整 schema 见 Recipe 参考手册Subrecipes字段类型必填说明nameString✅子配方的唯一标识用于生成工具名pathString✅子配方文件的相对或绝对路径valuesObject-预配置、始终传给子配方的参数sequential_when_repeatedBoolean-强制对同一子配方的多个实例串行执行descriptionString-子配方的可选描述这些字段与源码中SubRecipe结构体一一对应。在 crates/goose/src/recipe/mod.rs 中SubRecipe声明了name、path、values、sequential_when_repeated与description其中sequential_when_repeated通过#[serde(default)]默认为false——即只要不显式声明同一子配方的多实例默认并行与本文前述规则一致。构建配方时goose 会为每个sub_recipes条目生成一个子配方工具见 crates/goose/src/recipe/build_recipe/mod.rs并在加载清单时解析其真实文件路径见 crates/goose/src/recipe/manifest.rs。此外正如 Recipe 参考手册 与 subrecipes.md 所述只要配方定义了sub_recipessummon平台扩展就会被自动注入因此你无需在extensions中显式列出即可直接使用delegate工具并发调度正是在这一扩展上展开。选择执行模式Recipe-Level Configuration对于永远不应并行的子配方设置sequential_when_repeated: true会覆盖用户的并行请求强制始终串行sub_recipes: - name: database-migration path: ./subrecipes/migrate.yaml sequential_when_repeated: true # Always sequential典型例子是数据库迁移类任务——多个实例若并发执行会同时改动同一份共享资源造成不可预期的状态。如何权衡串行与并行并行能带来速度优势但串行在某些场景下仍然必要或更优。请据此决策应当串行当任务会修改共享资源数据库、配置文件、同一目录产物执行顺序有强依赖后一个任务依赖前一个任务的输出存在内存或 CPU 资源约束需要调试并行模式下的复杂失败。应当并行当任务之间相互独立追求更快的整体完成时间系统资源足以支撑最多 10 个并发 worker 同时运行需要处理大数据集或多个文件批处理、批量分析。相关的运行时限制与调优提示max_turns限制每个子配方可通过自身的settings.max_turns控制执行上限若不指定则继承父配方的max_turns相关配置优先级为子代理工具调用覆盖 配方settings.max_turnsGOOSE_SUBAGENT_MAX_TURNS环境变量 默认值详见 Recipe 参考手册settings。环境变量summon/delegate相关并发能力还暴露了如GOOSE_MAX_BACKGROUND_TASKS之类的运行时可调参数见 crates/goose/src/agents/platform_extensions/summon.rs用于控制后台任务并发上限。最佳实践小结先单后并先验证子配方能独立正确运行再组合成并行编排参考 subrecipes.md 最佳实践单一职责每个子配方只做一件事参数命名与描述要清晰固定值用values预置会变的值留给 AI 从上下文抽取共享资源务必串行凡是涉及迁移、发布、写入共享路径的任务优先声明sequential_when_repeated: true而不是依赖 prompt 措辞关注仪表盘并行任务多时用 CLI 实时仪表盘的状态流转Pending → Running → Completed/Failed快速定位失败项再决定是否降级为串行重试。延伸阅读Goose Recipes 指南总览 —— 更多配方文档、工具与资源Subrecipes面向专门任务的子配方 —— 子配方的字段定义、参数优先级与条件 / 顺序处理示例Recipe 参考手册 ——sub_recipesschema、settings.max_turns、summon自动注入等完整参考博客Subrecipes in Goose —— 子配方概念与设计思路【免费下载链接】goosean open source, extensible AI agent that goes beyond code suggestions - install, execute, edit, and test with any LLM项目地址: https://gitcode.com/GitHub_Trending/goose3/goose创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考