fix: isolate sessions by workspace to prevent cross-workspace memory leak#634
fix: isolate sessions by workspace to prevent cross-workspace memory leak#634Kokeip wants to merge 2 commits intoHKUDS:mainfrom
Conversation
|
@Re-bin 大佬有空review一下吗 |
|
我来啦,这就review |
|
感谢这个 PR!workspace 隔离 session 的方向是对的。 不过目前 nanobot 主要还是单用户单 workspace 的使用场景,多 workspace 并行的需求还不多。另外合入后现有用户的 session 路径会变化,升级后旧的对话历史会找不到。 |
感谢大佬反馈!我已经补充了后向兼容的迁移逻辑。 实现思路: 代码改动: 第一个 commit:workspace 隔离(+3 行) 当然,这只是我的一点想法。如果您觉得当前时机不太合适或有其他考虑,完全理解,可以暂时先不合并,等后续有需求再说 :) |
|
甭客气,感谢修改,我再review下😄 |
|
我有个想法:与其用 hash 子目录( 代码也更简单,不需要 hashlib,直接 迁移逻辑还是需要的,把 |
很好的建议,我再补充一个观察,想一起确认下方向: 我看了下现有生态(比如 Codex / Claude Code),运行时状态基本都放在全局配置目录里,而不是项目 workspace。这个做法在隐私和“避免项目检索误命中”上更稳一些,防止模型把session和memory也和用户预期比较一致。
|
|
你说的隐私顾虑有道理。不过目前 memory/ 已经在 workspace 下了,sessions 放进去跟现有结构一致,代码也更简单。如果以后要把运行时状态统一挪到全局目录,memory 和 sessions 可以一起迁移,那是一个更大的架构调整。当前阶段先保持一致性比较好。🤔 |
请教一下,现在多个workspace的切换是手动切换的,还是根据channel/chat_id来进行workspace区分的啊? |
应该还是手动在config里面切换的 |
|
Because this issue has been inactive for a long time, I will close it. If there are any other problems, please feel free to open a new issue. |
…KUDS#661) When tiktoken is unavailable, the fallback `len(text) // 3` severely underestimates tokens for CJK text (Chinese/Japanese/Korean characters are ~1-2 tokens each, not 0.33). This causes text exceeding the 8192-token API limit to bypass chunking, resulting in BadRequestError. Use `max(len(text) // 3, len(text.encode("utf-8")) // 4)` instead, which picks the more conservative estimate. For ASCII-heavy text the char-based estimate still wins; for CJK text the byte-based estimate correctly produces ~0.75 tokens per character. Fixes HKUDS#616, fixes HKUDS#634 Signed-off-by: JiangNan <1394485448@qq.com>
Problem
When switching between workspaces, nanobot continued using the same conversation session, causing memory contamination across workspaces:
/newin workspace B would consolidate workspace A's history into workspace B's MEMORY.mdThis made workspace memory unreliable and mixed unrelated contexts.
Root Cause
Session files were stored globally at
~/.nanobot/sessions/and keyed only bychannel:chat_id, with no workspace isolation. Different workspaces using the same channel/chat_id would share the same session file.Solution
Added workspace-specific subdirectories using an MD5 hash of the workspace path:
Now each workspace maintains its own isolated session storage.
Changes
Testing
Verified the fix works correctly:
/newin workspace B, verified no workspace A content leaked into workspace B memory filesThe issue is resolved and workspaces now maintain independent memory contexts.