Skip to content

Fix /cwd session handling and reduce WeChat reply latency#50

Open
yuanlehome wants to merge 3 commits intofastclaw-ai:mainfrom
yuanlehome:main
Open

Fix /cwd session handling and reduce WeChat reply latency#50
yuanlehome wants to merge 3 commits intofastclaw-ai:mainfrom
yuanlehome:main

Conversation

@yuanlehome
Copy link
Copy Markdown

Background

当前有两个比较直接的问题:

  1. /cwd 只更新了运行中 agent 的工作目录,没有重置当前用户会话,导致后续请求仍可能复用旧 session,实际读取到的还是旧 workspace。
  2. 发送微信“正在输入”状态时,每次都会先调用一次 getconfig 获取 typing_ticket,这会额外增加一次网络往返,拉长首个可见反馈时间。

Changes

  • 调整 /cwd 处理逻辑:

    • 将请求上下文和当前用户 ID 传入 handleCwd
    • 在更新运行中 agent 的 cwd 后,主动重置当前用户会话
    • 保证切换目录后,下一次提问立即基于新 workspace 生效
    • agentWorkDirs 增加防御性初始化,避免未初始化 map 的写入问题
  • 优化 typing indicator 发送链路:

    • typing_ticket 增加短时缓存
    • 优先复用缓存 ticket,避免每条消息都触发一次 getconfig
    • 当缓存 ticket 失效或发送失败时,自动回退到重新获取并刷新缓存
    • 缓存采用过期机制,避免长期持有无效 ticket

Expected Impact

  • /cwd 后的仓库切换行为更加符合直觉,后续对话不再意外沿用旧目录上下文。
  • 微信侧首个“正在输入”反馈更快,减少一次不必要的 API 往返。
  • 在不改变现有消息分发和 agent 调用方式的前提下,降低了用户感知延迟。

Testing

  • go test ./...
  • 覆盖了 /cwd 后会话重置的行为测试
  • 覆盖了 typing_ticket 缓存命中、过期和失效清理测试

Copilot AI review requested due to automatic review settings April 7, 2026 03:50
@yuanlehome
Copy link
Copy Markdown
Author

@idoubi Kindly review and merge the changes. Thank you.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 /cwd handling to pass request context + userID, update running agents’ cwd, and reset the current user session after switching workspace.
  • Add a short-lived typing_ticket cache to speed up typing indicator sending, with fallback to re-fetch on failure/expiry.
  • Add tests covering /cwd session 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.

Comment on lines +14 to +24
const typingTicketTTL = 2 * time.Minute

type typingTicketCacheEntry struct {
ticket string
expiresAt time.Time
}

var (
typingTicketCache sync.Map
nowFunc = time.Now
)
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants