Fix /cwd session handling and reduce WeChat reply latency#50
Fix /cwd session handling and reduce WeChat reply latency#50yuanlehome wants to merge 3 commits intofastclaw-ai:mainfrom
Conversation
|
@idoubi Kindly review and merge the changes. Thank you. |
There was a problem hiding this comment.
Pull request overview
This PR improves WeChat bot session/workspace correctness and reduces perceived reply latency by fixing /cwd to reset the active user session and by caching typing_ticket to avoid an extra getconfig round-trip on every typing indicator send.
Changes:
- Update
/cwdhandling to pass request context + userID, update running agents’ cwd, and reset the current user session after switching workspace. - Add a short-lived
typing_ticketcache to speed up typing indicator sending, with fallback to re-fetch on failure/expiry. - Add tests covering
/cwdsession reset behavior and typing ticket cache hit/expiry/invalidation.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| messaging/sender.go | Adds typing_ticket TTL cache and uses it to avoid redundant GetConfig calls. |
| messaging/sender_test.go | Adds unit tests for cache hit, expiry, and invalidation logic. |
| messaging/handler.go | Updates /cwd command path to reset the user session after applying cwd to running agents; adds defensive init for agentWorkDirs. |
| messaging/handler_test.go | Adds a fake agent and test ensuring /cwd triggers ResetSession for the current user. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const typingTicketTTL = 2 * time.Minute | ||
|
|
||
| type typingTicketCacheEntry struct { | ||
| ticket string | ||
| expiresAt time.Time | ||
| } | ||
|
|
||
| var ( | ||
| typingTicketCache sync.Map | ||
| nowFunc = time.Now | ||
| ) |
There was a problem hiding this comment.
typingTicketCache is an unbounded global cache keyed by userID, and expired entries are only removed on a subsequent read for that same user (getCachedTypingTicket deletes on access). If many unique userIDs interact once, this can grow indefinitely and retain expired tickets forever. Consider adding bounded eviction and/or periodic cleanup (e.g., opportunistic Range cleanup every N stores, or a small TTL cache with max size) so memory usage can’t grow without limit.
Background
当前有两个比较直接的问题:
/cwd只更新了运行中 agent 的工作目录,没有重置当前用户会话,导致后续请求仍可能复用旧 session,实际读取到的还是旧 workspace。getconfig获取typing_ticket,这会额外增加一次网络往返,拉长首个可见反馈时间。Changes
调整
/cwd处理逻辑:handleCwdagentWorkDirs增加防御性初始化,避免未初始化 map 的写入问题优化 typing indicator 发送链路:
typing_ticket增加短时缓存getconfigExpected Impact
/cwd后的仓库切换行为更加符合直觉,后续对话不再意外沿用旧目录上下文。Testing
go test ./.../cwd后会话重置的行为测试typing_ticket缓存命中、过期和失效清理测试