Skip to content

Conversation

@lss233
Copy link
Owner

@lss233 lss233 commented May 5, 2025

  • Introduced WorkflowConfig to manage execution settings, including max execution time.
  • Updated Workflow and WorkflowBuilder to incorporate configuration management.
  • Enhanced WorkflowExecutor to handle execution timeouts and log errors appropriately.
  • Modified API routes to support configuration in workflow definitions and updates.

好的,这是将拉取请求摘要翻译成中文的结果:

Sourcery 总结

添加工作流执行超时配置,以管理和限制工作流执行时间

新特性:

  • 引入 WorkflowConfig 来管理工作流执行设置
  • 增加了对工作流最大执行时间的支持

Bug 修复:

  • 为工作流执行超时添加了适当的错误处理

增强功能:

  • 增强了 WorkflowExecutor 以处理执行超时
  • 更新了工作流构建器和序列化,以支持配置管理
Original summary in English

Summary by Sourcery

Add workflow execution timeout configuration to manage and limit workflow execution time

New Features:

  • Introduced WorkflowConfig to manage workflow execution settings
  • Added support for maximum execution time in workflows

Bug Fixes:

  • Added proper error handling for workflow execution timeouts

Enhancements:

  • Enhanced WorkflowExecutor to handle execution timeouts
  • Updated workflow builder and serialization to support configuration management

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented May 5, 2025

## 审查者指南

此拉取请求引入了工作流执行超时功能。它添加了一个 `WorkflowConfig` 模型来保存诸如 `max_execution_time` 之类的设置,将此配置集成到 `Workflow` 和 `WorkflowBuilder` 类中(包括序列化/反序列化),修改了 `WorkflowExecutor` 以使用 `asyncio.timeout` 强制执行超时,并更新了相关的 API 端点以处理此新配置。

#### WorkflowExecutor 超时处理的序列图

```mermaid
sequenceDiagram
    participant WE as WorkflowExecutor
    participant asyncio
    participant Nodes as _execute_nodes

    WE->>WE: run()
    WE->>WE: Get max_timeout = workflow.config.max_execution_time
    alt max_timeout > 0
        WE->>asyncio: timeout(max_timeout)
        activate asyncio
        WE->>Nodes: _execute_nodes(...)
        alt Execution exceeds timeout
            asyncio-->>WE: raises TimeoutError
            WE->>WE: Catch TimeoutError
            WE-->>WE: raise WorkflowExecutionTimeoutException
        else Execution completes within timeout
            Nodes-->>WE: Return results
            WE-->>asyncio: Execution finished
            deactivate asyncio
        end
    else max_timeout <= 0
        WE->>Nodes: _execute_nodes(...)
        Nodes-->>WE: Return results
    end
    WE->>WE: Post WorkflowExecutionEnd event
    WE->>WE: Return results or raise exception

工作流超时功能的类图

classDiagram
    class WorkflowConfig {
        +int max_execution_time
    }
    note for WorkflowConfig "新的配置类"

    class Workflow {
        +string name
        +List~Block~ blocks
        +List~Wire~ wires
        +string id
        +WorkflowConfig config
        +__init__(name, blocks, wires, id, config)
    }
    note for Workflow "添加了 config 属性"

    class WorkflowBuilder {
        +string name
        +string description
        +List~Node~ nodes
        +WorkflowConfig config
        +build(container) Workflow
        +set_config(config) WorkflowBuilder
        +save_to_yaml(file_path, container)
        +load_from_yaml(file_path, container) WorkflowBuilder
    }
    note for WorkflowBuilder "添加了 config 属性和相关方法"

    class WorkflowExecutor {
        +Workflow workflow
        +run() Dict~str, Any~
        -_execute_nodes(entry_blocks, executor, loop)
    }
    note for WorkflowExecutor "run() 方法已更新以处理超时"

    class WorkflowDefinition {
      +string name
      +string description
      +List~BlockInstance~ blocks
      +List~Wire~ wires
      +WorkflowConfig config
      +Dict~str, Any~ metadata
    }
    note for WorkflowDefinition "添加了 config 属性 (API 模型)"

    class WorkflowExecutionTimeoutException {
      <<Exception>>
    }
    note for WorkflowExecutionTimeoutException "用于超时的新异常"

    Workflow --o WorkflowConfig : has a
    WorkflowBuilder --o WorkflowConfig : has a
    WorkflowExecutor ..> Workflow : uses
    WorkflowExecutor ..> WorkflowExecutionTimeoutException : raises
    WorkflowDefinition --o WorkflowConfig : has a
Loading

文件级别更改

变更 详情 文件
引入了用于工作流设置的 WorkflowConfig 模型。
  • 定义了具有 max_execution_timeWorkflowConfig 模型。
  • config 字段添加到 WorkflowDefinition API 模型。
kirara_ai/workflow/core/workflow/base.py
kirara_ai/workflow/core/workflow/__init__.py
kirara_ai/web/api/workflow/models.py
WorkflowConfig 集成到工作流构建、保存和加载过程中。
  • config 属性添加到 WorkflowWorkflowBuilder
  • 在 YAML 序列化/反序列化中包含配置处理。
  • set_config 方法添加到 WorkflowBuilder
kirara_ai/workflow/core/workflow/builder.py
kirara_ai/workflow/core/workflow/base.py
实现了工作流执行超时逻辑。
  • 添加了 WorkflowExecutionTimeoutException
  • WorkflowExecutor.run 中使用 asyncio.timeout 来强制执行来自配置的 max_execution_time
  • 在超时时引发 WorkflowExecutionTimeoutException
kirara_ai/workflow/core/execution/executor.py
kirara_ai/workflow/core/execution/exceptions.py
更新了 API 路由以支持工作流配置。
  • 修改了 get_workflow 以返回 config
  • 更新了 create_workflowupdate_workflow 以接受并保存 config
kirara_ai/web/api/workflow/routes.py
在调度器中添加了超时异常的错误处理。
  • 添加了 WorkflowExecutionTimeoutException 的异常处理以记录错误。
kirara_ai/workflow/core/dispatch/dispatcher.py

提示和命令

与 Sourcery 互动

  • 触发新的审查: 在拉取请求上评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审查评论。
  • 从审查评论生成 GitHub issue: 通过回复审查评论,要求 Sourcery 从审查评论创建一个 issue。您也可以回复审查评论并使用 @sourcery-ai issue 从中创建一个 issue。
  • 生成拉取请求标题: 在拉取请求标题中的任何位置写入 @sourcery-ai 以随时生成标题。您也可以在拉取请求上评论 @sourcery-ai title 以随时(重新)生成标题。
  • 生成拉取请求摘要: 在拉取请求正文中的任何位置写入 @sourcery-ai summary 以随时在您想要的位置生成 PR 摘要。您也可以在拉取请求上评论 @sourcery-ai summary 以随时(重新)生成摘要。
  • 生成审查者指南: 在拉取请求上评论 @sourcery-ai guide 以随时(重新)生成审查者指南。
  • 解决所有 Sourcery 评论: 在拉取请求上评论 @sourcery-ai resolve 以解决所有 Sourcery 评论。如果您已经解决了所有评论并且不想再看到它们,这将非常有用。
  • 驳回所有 Sourcery 审查: 在拉取请求上评论 @sourcery-ai dismiss 以驳回所有现有的 Sourcery 审查。如果您想重新开始新的审查,这将特别有用 - 不要忘记评论 @sourcery-ai review 以触发新的审查!

自定义您的体验

访问您的 仪表板 以:

  • 启用或禁用审查功能,例如 Sourcery 生成的拉取请求摘要、审查者指南等。
  • 更改审查语言。
  • 添加、删除或编辑自定义审查说明。
  • 调整其他审查设置。

获取帮助

```
Original review guide in English

Reviewer's Guide

This pull request introduces a workflow execution timeout feature. It adds a WorkflowConfig model to hold settings like max_execution_time, integrates this configuration into the Workflow and WorkflowBuilder classes (including serialization/deserialization), modifies the WorkflowExecutor to enforce the timeout using asyncio.timeout, and updates the relevant API endpoints to handle this new configuration.

Sequence diagram for WorkflowExecutor Timeout Handling

sequenceDiagram
    participant WE as WorkflowExecutor
    participant asyncio
    participant Nodes as _execute_nodes

    WE->>WE: run()
    WE->>WE: Get max_timeout = workflow.config.max_execution_time
    alt max_timeout > 0
        WE->>asyncio: timeout(max_timeout)
        activate asyncio
        WE->>Nodes: _execute_nodes(...)
        alt Execution exceeds timeout
            asyncio-->>WE: raises TimeoutError
            WE->>WE: Catch TimeoutError
            WE-->>WE: raise WorkflowExecutionTimeoutException
        else Execution completes within timeout
            Nodes-->>WE: Return results
            WE-->>asyncio: Execution finished
            deactivate asyncio
        end
    else max_timeout <= 0
        WE->>Nodes: _execute_nodes(...)
        Nodes-->>WE: Return results
    end
    WE->>WE: Post WorkflowExecutionEnd event
    WE->>WE: Return results or raise exception
Loading

Class diagram for Workflow Timeout Feature

classDiagram
    class WorkflowConfig {
        +int max_execution_time
    }
    note for WorkflowConfig "New configuration class"

    class Workflow {
        +string name
        +List~Block~ blocks
        +List~Wire~ wires
        +string id
        +WorkflowConfig config
        +__init__(name, blocks, wires, id, config)
    }
    note for Workflow "Added config attribute"

    class WorkflowBuilder {
        +string name
        +string description
        +List~Node~ nodes
        +WorkflowConfig config
        +build(container) Workflow
        +set_config(config) WorkflowBuilder
        +save_to_yaml(file_path, container)
        +load_from_yaml(file_path, container) WorkflowBuilder
    }
    note for WorkflowBuilder "Added config attribute and related methods"

    class WorkflowExecutor {
        +Workflow workflow
        +run() Dict~str, Any~
        -_execute_nodes(entry_blocks, executor, loop)
    }
    note for WorkflowExecutor "run() method updated to handle timeout"

    class WorkflowDefinition {
      +string name
      +string description
      +List~BlockInstance~ blocks
      +List~Wire~ wires
      +WorkflowConfig config
      +Dict~str, Any~ metadata
    }
    note for WorkflowDefinition "Added config attribute (API Model)"

    class WorkflowExecutionTimeoutException {
      <<Exception>>
    }
    note for WorkflowExecutionTimeoutException "New exception for timeout"

    Workflow --o WorkflowConfig : has a
    WorkflowBuilder --o WorkflowConfig : has a
    WorkflowExecutor ..> Workflow : uses
    WorkflowExecutor ..> WorkflowExecutionTimeoutException : raises
    WorkflowDefinition --o WorkflowConfig : has a
Loading

File-Level Changes

Change Details Files
Introduced WorkflowConfig model for workflow settings.
  • Defined WorkflowConfig model with max_execution_time.
  • Added config field to WorkflowDefinition API model.
kirara_ai/workflow/core/workflow/base.py
kirara_ai/workflow/core/workflow/__init__.py
kirara_ai/web/api/workflow/models.py
Integrated WorkflowConfig into workflow build, save, and load processes.
  • Added config attribute to Workflow and WorkflowBuilder.
  • Included config handling in YAML serialization/deserialization.
  • Added set_config method to WorkflowBuilder.
kirara_ai/workflow/core/workflow/builder.py
kirara_ai/workflow/core/workflow/base.py
Implemented workflow execution timeout logic.
  • Added WorkflowExecutionTimeoutException.
  • Used asyncio.timeout in WorkflowExecutor.run to enforce max_execution_time from config.
  • Raised WorkflowExecutionTimeoutException on timeout.
kirara_ai/workflow/core/execution/executor.py
kirara_ai/workflow/core/execution/exceptions.py
Updated API routes to support workflow configuration.
  • Modified get_workflow to return the config.
  • Updated create_workflow and update_workflow to accept and save the config.
kirara_ai/web/api/workflow/routes.py
Added error handling for timeout exceptions in the dispatcher.
  • Added exception handling for WorkflowExecutionTimeoutException to log errors.
kirara_ai/workflow/core/dispatch/dispatcher.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions
Copy link

github-actions bot commented May 5, 2025

MyPy 类型检查通过 ✅

PR 修改的代码行通过了类型检查。

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lss233 - 我已经审查了你的更改,它们看起来很棒!

以下是我在审查期间查看的内容
  • 🟡 一般问题:发现 1 个问题
  • 🟢 安全性:一切看起来都很好
  • 🟢 测试:一切看起来都很好
  • 🟢 复杂性:一切看起来都很好
  • 🟢 文档:一切看起来都很好

Sourcery 对开源是免费的 - 如果你喜欢我们的评论,请考虑分享它们 ✨
帮助我变得更有用!请点击每个评论上的 👍 或 👎,我将使用反馈来改进你的评论。
Original comment in English

Hey @lss233 - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.


if max_timeout > 0:
try:
async with asyncio.timeout(max_timeout):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

问题 (bug_risk): 审查执行块中 asyncio.timeout 的使用。

验证在 _execute_nodes 周围使用 asyncio.timeout 是否会将未完成的子任务留在不一致的状态,以及是否在超时时运行所需的清理。

Original comment in English

question (bug_risk): Review the use of asyncio.timeout in the execution block.

Verify that using asyncio.timeout around _execute_nodes doesn’t leave incomplete subtasks in an inconsistent state, and that required cleanup runs on timeout.

- Introduced WorkflowConfig to manage execution settings, including max execution time.
- Updated Workflow and WorkflowBuilder to incorporate configuration management.
- Enhanced WorkflowExecutor to handle execution timeouts and log errors appropriately.
- Modified API routes to support configuration in workflow definitions and updates.
@lss233 lss233 force-pushed the feature/workflow_timeout branch from ef9e8fb to 20e5707 Compare May 5, 2025 16:30
@codecov
Copy link

codecov bot commented May 5, 2025

Codecov Report

Attention: Patch coverage is 82.35294% with 6 lines in your changes missing coverage. Please review.

Project coverage is 65.88%. Comparing base (55e8837) to head (20e5707).
Report is 2 commits behind head on master.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
kirara_ai/workflow/core/dispatch/dispatcher.py 25.00% 3 Missing ⚠️
kirara_ai/workflow/core/execution/executor.py 70.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1476      +/-   ##
==========================================
+ Coverage   65.84%   65.88%   +0.03%     
==========================================
  Files         161      161              
  Lines        8148     8175      +27     
==========================================
+ Hits         5365     5386      +21     
- Misses       2783     2789       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@lss233 lss233 merged commit 417ca3e into master May 5, 2025
6 of 12 checks passed
@lss233 lss233 deleted the feature/workflow_timeout branch May 5, 2025 16:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants