Skip to content

fix(agent): retain pending cancellation while run events are appended - #46568

Merged
BeautyyuYanli merged 2 commits into
langgenius:mainfrom
Wang-Zephyr:fix/agent-redis-keep-cancellation-intent
Nov 18, 2026
Merged

BeautyyuYanli merged 2 commits into
langgenius:mainfrom
Wang-Zephyr:fix/agent-redis-keep-cancellation-intent

Conversation

@Wang-Zephyr

Copy link
Copy Markdown
Contributor

Fixes #46567

Summary

Refresh the existing cancellation key's TTL inside the same Redis Lua script that appends a live event and refreshes the run record. Redis EXPIRE leaves an absent cancellation key absent. Terminal runs still reject late writes without refreshing retention.

Verification

  • The new Redis-store regression failed on the base because the intent's TTL stayed at 10 seconds after an append. With the fix, the TTL is refreshed and the original cancellation reason is preserved.
  • All 82 tests passed with uv run --no-sync pytest -q tests/local/dify_agent/storage/test_redis_run_store.py tests/local/dify_agent/runtime/test_run_scheduler.py. The regression executes Lua using fakeredis.
  • Ruff formatting and package lint, package Pyrefly, and the baseline-aware test-doubles guard passed.

Checklist

  • Linked the issue and verified the regression against the base and fix.

From Codex

@github-actions github-actions Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Nov 17, 2026
@dosubot

dosubot Bot commented Nov 17, 2026

Copy link
Copy Markdown

📄 Knowledge review

✏️ Suggested updates

1 page suggestion needs review.

Page Library Status
Dify Agent Server 模块分析 (Commit 55f95dbc) dify 🟡 Review
📝 Dify Agent Server 模块分析 (Commit 55f95db)
@@ -170,7 +170,7 @@
 
 - **Run 记录**:以 JSON 字符串存储在 Redis Key 中,记录 `run_id`、`status`、`created_at`、`updated_at`、`error`
 - **事件流**:每个 run 对应一条 Redis Stream,事件以 `xadd` 追加写入,并应用 MAXLEN 近似最大长度(默认 5000 条目)防止事件流无限增长
-- **TTL 刷新**:每次写入状态或事件时,同步刷新 run 记录和事件流的过期时间(默认保留 2 小时,从之前的 3 天(259200 秒)减少为 7200 秒)
+- **TTL 刷新**:每次写入状态或事件时,同步刷新 run 记录和事件流的过期时间(默认保留 2 小时,从之前的 3 天(259200 秒)减少为 7200 秒)。运行仍为 `running` 时,`append_event()` 在同一个 Redis Lua 脚本中将已有取消意图键的 TTL 刷新为相同的保留时长,保留原有取消原因;取消意图键不存在时不会创建它。运行进入终态后,迟到的非终端事件写入会被拒绝,不会刷新保留时间。
 - **事件合并**:文本增量在可配置的去抖窗口(默认 100 毫秒)和字符阈值(默认 4096 字符)内合并,然后再发布到 Redis。仅相同响应部分的连续文本增量和提供者元数据会被合并,保留事件顺序
 - **取消观察**:`wait_for_cancellation(run_id)` 方法阻塞等待第一个被接受的取消意图从私有取消意图 Redis Stream 到达。该方法仅监听 `run_cancel_intent_key` 流(私有取消意图流),一旦可用即返回 `RunCancellationIntent`
 - **事件迭代**:`iter_events(run_id, after)` 先回放已有事件,再以阻塞 `XREAD` 读取实时事件。**迭代在产出第一个终端事件(run_succeeded、run_failed 或 run_cancelled)后立即返回**,无论该事件来自回放还是实时推送。方法不再在终端事件后继续阻塞循环

Accept · Edit · Decline


Leave Feedback Ask Dosu about dify

@BeautyyuYanli BeautyyuYanli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(This message is automated while I'm responsible for it.)

Reviewed the _APPEND_EVENT_SCRIPT / RedisRunStore.append_event() TTL refresh and the AsyncFakeRedis Lua regression tests: refreshing KEYS[3] (run_cancel_intent_key) via EXPIRE on accepted running appends preserves existing pending cancellation intents across long-running event streams without synthesizing missing keys or extending retention on rejected post-terminal writes.

Pushed a small follow-up commit (1673a2e55dc6) updating the RedisRunStore class docstring and dify-agent/docs/dify-agent/guide/index.md so the documented retention contract matches this behavior.

@BeautyyuYanli
BeautyyuYanli added this pull request to the merge queue Nov 18, 2026
Merged via the queue into langgenius:main with commit 2b04cb5 Nov 18, 2026
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dify Agent can expire a pending cancellation before its run expires

2 participants