feat (sql-executor): 实现 SQL 自纠错循环,执行失败后自动重试生成 - #229
Open
Sully-y wants to merge 1 commit into
Open
Conversation
重试 3 次,确保流程在可恢复错误下能自动修正并继续。
Author
|
你好,只是友好地提醒一下这个PR。您有空的时候能看一下吗?谢谢! @apconw |
Owner
|
收到 最近比较忙 后面会统一处理 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概述
实现 SQL 执行失败后的自动恢复机制。当 sql_executor 执行失败时,将具体错误信息回填至生成提示词,驱动 sql_generator 重新生成 SQL,并支持最多 3 次重试。解决执行异常被静默吞掉、attempts / correct_attempts 字段闲置、{error_msg} 占位符从未填充以及无条件边导致的流程错误传播等问题。
Related Issues
Closes #227
具体改动
1. 异常分类
在 execute_sql 中细化异常捕获,区分 可重试错误(如语法错误、连接超时、表不存在)与 不可重试错误(如权限不足、资源不存在)。
只有可重试错误才触发重试逻辑,不可重试错误直接终止并返回明确错误信息。
2. 激活 attempts 计数
在流程状态中启用 attempts 字段,初始值 0,每次执行失败并决定重试前自增。
重试条件:attempts < 3 且错误类型为可重试。
3. 失败时回填 error_msg
在重试分支中,将捕获的异常对象转换为描述字符串,并填入 prompt 模板的 {error_msg} 占位符。
更新相关 prompt 模板,确保 {error_msg} 在重试时有效渲染,为模型提供具体失败上下文。
4. 条件路由改造
将 sql_executor 后的无条件边替换为条件路由,逻辑如下:
成功 → 输出结果节点。
失败 & attempts < 3 → 回填 error_msg,返回 sql_generator 节点重新生成 SQL。
失败 & attempts ≥ 3(或不可重试) → 终止流程,返回最终错误信息。
关于测试
自动化测试,新增测试文件 tests/test_aimodel_service.py(随附件),覆盖核心场景,所有测试均已通过。

☑ 所有单元测试均已通过
☑ 代码已通过 linter 检查
☑ 新增异常分支均有对应测试
附件清单:
pytest代码,测试截图与自动测试日志。
tests.zip