Repository navigation
fix(agent): retain pending cancellation while run events are appended - #46568
Merged
BeautyyuYanli merged 2 commits intoNov 18, 2026
Merged
BeautyyuYanli merged 2 commits into
BeautyyuYanli merged 2 commits into
Conversation
📄 Knowledge review✏️ Suggested updates1 page suggestion needs 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)后立即返回**,无论该事件来自回放还是实时推送。方法不再在终端事件后继续阻塞循环 |
BeautyyuYanli
approved these changes
Nov 18, 2026
BeautyyuYanli
left a comment
Contributor
There was a problem hiding this comment.
(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.
3 of 5 tasks
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.
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
EXPIREleaves an absent cancellation key absent. Terminal runs still reject late writes without refreshing retention.Verification
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.Checklist
From Codex