Repository navigation
fix: handle Dify workflow stream errors - #10545
Conversation
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="astrbot/core/agent/runners/dify/dify_agent_runner.py" line_range="243" />
<code_context>
+ f"Dify workflow error (status: {chunk.get('status')}, "
+ f"code: {chunk.get('code')}): "
+ f"{chunk.get('message') or 'No error details provided.'}"
+ )
case "workflow_started":
logger.info(
</code_context>
<issue_to_address>
**Dify diagnostics reach chat users**
When a Dify workflow error contains sensitive diagnostic details and an ordinary chat user triggers that error, `step()` embeds the exception text in the `err` response, and `run_third_party_agent` forwards that chain to the requester, exposing internal workflow diagnostics.
Send a generic error message to the requester and retain detailed diagnostics only in access-controlled logs.
Also at `astrbot/core/agent/runners/dify/dify_agent_runner.py:274`.
</issue_to_address>
### Comment 2
<location path="astrbot/core/agent/runners/dify/dify_agent_runner.py" line_range="268-271" />
<code_context>
- f"Dify 工作流出现错误:{chunk['data']['error']}"
- )
+ status = chunk["data"].get("status")
+ if chunk["data"].get("error") or status not in (
+ "succeeded",
+ "partial-succeeded",
+ ):
raise Exception(
- f"Dify 工作流出现错误:{chunk['data']['error']}"
</code_context>
<issue_to_address>
**Partial results become errors**
When dify returns `partial-succeeded` with a non-empty `data.error`, the `workflow_finished` check raises before the output is read, so `step()` returns an error instead of the available partial result.
Honor the `partial-succeeded` status before treating `data.error` as fatal.
</issue_to_address>Sourcery assessment
Approval pending. 2 findings to address first.
Blocking findings: astrbot/core/agent/runners/dify/dify_agent_runner.py:243, astrbot/core/agent/runners/dify/dify_agent_runner.py:271
| if chunk["data"].get("error") or status not in ( | ||
| "succeeded", | ||
| "partial-succeeded", | ||
| ): |
There was a problem hiding this comment.
🟡 Medium · Partial results become errors
When dify returns partial-succeeded with a non-empty data.error, the workflow_finished check raises before the output is read, so step() returns an error instead of the available partial result.
Honor the partial-succeeded status before treating data.error as fatal.
Prompt for AI agents
In `astrbot/core/agent/runners/dify/dify_agent_runner.py` at lines 268-271:
**Partial results become errors**
When dify returns `partial-succeeded` with a non-empty `data.error`, the `workflow_finished` check raises before the output is read, so `step()` returns an error instead of the available partial result.
Honor the `partial-succeeded` status before treating `data.error` as fatal.|
@sourcery-ai review |
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="astrbot/core/agent/runners/dify/dify_agent_runner.py" line_range="271" />
<code_context>
logger.debug(f"Dify 工作流结果:{chunk}")
- if chunk["data"]["error"]:
+ status = chunk["data"].get("status")
+ if chunk["data"].get("error") or status not in (
+ "succeeded",
+ "partial-succeeded",
</code_context>
<issue_to_address>
**Partial workflow outputs are discarded**
When dify emits a `workflow_finished` event with status `partial-succeeded`, a nonempty `data.error`, and the configured output available, the `data.error` check rejects the event before the configured output is checked or parsed, so the runner returns an error instead of the partial result.
Accept `partial-succeeded` events despite `data.error` and parse their available configured output.
</issue_to_address>Sourcery assessment
Approval pending. 1 finding to address first.
Blocking findings: astrbot/core/agent/runners/dify/dify_agent_runner.py:271
| logger.debug(f"Dify 工作流结果:{chunk}") | ||
| if chunk["data"]["error"]: | ||
| status = chunk["data"].get("status") | ||
| if chunk["data"].get("error") or status not in ( |
There was a problem hiding this comment.
🟡 Medium · Partial workflow outputs are discarded
When dify emits a workflow_finished event with status partial-succeeded, a nonempty data.error, and the configured output available, the data.error check rejects the event before the configured output is checked or parsed, so the runner returns an error instead of the partial result.
Accept partial-succeeded events despite data.error and parse their available configured output.
Prompt for AI agents
In `astrbot/core/agent/runners/dify/dify_agent_runner.py` at line 271:
**Partial workflow outputs are discarded**
When dify emits a `workflow_finished` event with status `partial-succeeded`, a nonempty `data.error`, and the configured output available, the `data.error` check rejects the event before the configured output is checked or parsed, so the runner returns an error instead of the partial result.
Accept `partial-succeeded` events despite `data.error` and parse their available configured output.
Dify workflow errors could be reported as empty success or leave partial replies without an error notice.
Modifications / 改动点
Surface stream errors and reject streams that end without a valid workflow result.
Validate completion status while preserving
partial-succeededoutputs and existing output checks.This is NOT a breaking change. / 这不是一个破坏性变更。
Screenshots or Test Results / 运行截图或测试结果
ruff format --check .,ruff check ., andgit diff --checkpassed.Checklist / 检查清单
😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
/ 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
/ 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。
📚 I checked the affected WebUI instructions and screenshots in
docs/zhanddocs/enagainst the changed navigation, page structure, and labels, and updated them in this PR (or explained why no documentation update is needed). For renamed, moved, or merged entry points, I included an old entry → new entry mapping in the documentation and changelog./ 我已对照变化后的 WebUI 入口、页面结构和术语,核对并在本 PR 中更新
docs/zh和docs/en的相关操作说明与截图(或说明无需更新文档的原因)。入口改名、移动或合并时,已在文档和 changelog 中补充 旧入口 → 新入口 对照。🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in
requirements.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。
Summary by Sourcery
Ensure Dify workflow streaming reports execution failures and requires a valid completed workflow result.
Bug Fixes:
Enhancements: