fix(session): sanitize '/' and '\' in session keys so forum topic key…#1330
Conversation
…s don't create invalid paths
|
@alexhoshina @dimonb I'd appreciate you review this PR |
nikolasdehor
left a comment
There was a problem hiding this comment.
Clean, well-scoped fix. The session key sanitization for '/' and '\' is the right approach to prevent composite IDs (Telegram forum topics, Slack threads) from being interpreted as directory separators.
A few notes:
-
Collision risk acknowledged -- the existing comment about lossy mapping ("telegram:123" vs "telegram_123") was removed. The same concern applies to '/' being flattened to '_'. Two keys like "group:-100/12" and "group:-100_12" would now map to the same file. This is acceptable given the real-world key formats, but worth keeping in mind.
-
Test coverage is good -- the new test case for the Telegram forum topic key and the updated path traversal tests (now verifying that "foo/bar" is sanitized rather than rejected) are exactly right.
-
Both code paths updated -- good that both
pkg/memory/jsonl.goandpkg/session/manager.gowere updated in lockstep, maintaining the mirror contract mentioned in the comments. -
The removal of the
strings.ContainsAny(filename, "/\\")guard inSave()is correct sincesanitizeFilenamenow handles this before the check.
LGTM.
|
Now telegram forum works well without any issues. Me and @dimonb confirmed. It's okay to merge this PR if code has no problem anymore. Thanks |
…lash fix(session): sanitize '/' and '\' in session keys so forum topic key…
|
@statxc Great investigation on this one. Composite IDs from Telegram forum topics and Slack threads breaking session file paths is the kind of bug that's tricky to catch until it hits production. Sanitizing both / and \ while keeping the logical key unchanged is the right approach. We're running a PicoClaw Dev Group on Discord for contributors to connect. If you'd like to join, email |
…lash fix(session): sanitize '/' and '\' in session keys so forum topic key…
…lash fix(session): sanitize '/' and '\' in session keys so forum topic key…
📝 Description
Session keys can contain a slash when they represent a composite ID, for example:
agent:main:telegram:group:-1003822706455/12(chat ID + thread ID)agent:main:slack:channel:C01234/1234567890.123456(channel + thread TS)Previously, only the colon (
:) was replaced with_when building session file paths. The slash was left as-is, so the path was interpreted as a directory separator and the code tried to open a file likesessions/agent_main_telegram_group_-1003822706455/12.jsonl. The subdirectory was never created, which led to "no such file or directory" when adding messages.This PR updates the session key sanitization in both the JSONL memory store and the legacy session manager:
/and\are now replaced with_so that:sessions/.\is a path separator.The logical session key (e.g.
agent:main:telegram:group:-1003822706455/12) is unchanged in memory and in the bus; only the filename used on disk is sanitized.🗣️ Type of Change
🤖 AI Code Generation
🔗 Related Issue
#1270
📚 Technical Context (Skip for Docs)
/and\keeps a single-file-per-session layout and fixes the error.🧪 Test Environment
📸 Evidence (Optional)
Click to view Logs/Screenshots
Before:
open .picoclaw/workspace/sessions/agent_main_telegram_group_-1003822706455/12.jsonl: no such file or directoryAfter: session file is created as
agent_main_telegram_group_-1003822706455_12.jsonlundersessions/and messages are stored correctly.☑️ Checklist