fix(sessions): tolerate invalid sessionFile metadata#16061
fix(sessions): tolerate invalid sessionFile metadata#16061haoyifan wants to merge 3 commits intoopenclaw:mainfrom
Conversation
bfc1ccb to
f92900f
Compare
AI-assisted: yes (Codex/OpenClaw agent).
Problem
- Some deployments end up with `sessions.json` entries containing a `sessionFile` field that is stale/corrupted
(e.g., absolute paths persisted historically, state dir migrations, or other bad metadata).
- `resolveSessionFilePath()` currently treats an invalid `sessionFile` as fatal and throws:
"Session file path must be within sessions directory".
- When this happens inside chat/channel handlers (observed with Telegram), the handler can fail mid-update,
leading to silent non-replies even though the message was received.
Fix
- Treat `entry.sessionFile` as optional, best-effort metadata.
- If validation fails, ignore it and fall back to the derived safe transcript path based on `sessionId`
(which is already validated and resolved within the agent sessions directory).
Security
- Containment checks are still enforced when a candidate is provided.
- This change does NOT allow escaping paths; it simply avoids taking down message handling due to invalid metadata.
Testing
- Local (Istanbul VM):
- `pnpm format:check` PASS
- `pnpm check` PASS
- `pnpm vitest run src/config/sessions/paths.test.ts` PASS
Concise output:
- "✓ src/config/sessions/paths.test.ts (12 tests)"
- Real-world repro:
- Reproduced intermittent Telegram handler failures caused by invalid/stale `sessionFile` metadata, and confirmed this
change prevents silent non-replies by falling back to the derived safe transcript path.
c06f155 to
bdce151
Compare
|
This pull request has been automatically marked as stale due to inactivity. |
|
Deep-dive merge-readiness pass: This branch looks close and mergeable, but the current Before merge, add one explicit regression test proving invalid Changelog guidance if merged: one |
|
Merge checklist (must-have):
Changelog line (if merged): Credit note: |
|
Thanks for the fix here. Closing as superseded by #24657, which now carries the merged session-path fallback hardening path. If you still see a gap on current main, reply with a repro and we can reopen quickly. |
|
Merged in #24657 due to overlapping PR changes, appreciate your contribution. |
fix(sessions): tolerate invalid sessionFile metadata
AI-assisted: yes (Codex/OpenClaw agent).
Problem
sessions.jsonentries containing asessionFilefield that is stale/corrupted(e.g., absolute paths persisted historically, state dir migrations, or other bad metadata).
resolveSessionFilePath()currently treats an invalidsessionFileas fatal and throws:"Session file path must be within sessions directory".
leading to silent non-replies even though the message was received.
Fix
entry.sessionFileas optional, best-effort metadata.sessionId(which is already validated and resolved within the agent sessions directory).
Security
Testing
Local (Istanbul VM):
pnpm format:checkPASSpnpm checkPASSpnpm vitest run src/config/sessions/paths.test.tsPASSConcise output:
Real-world repro:
sessionFilemetadata, and confirmed thischange prevents silent non-replies by falling back to the derived safe transcript path.
Greptile Summary
This PR makes
resolveSessionFilePathresilient to corrupted or stalesessionFilemetadata insessions.json. Instead of throwing (which could crash message handlers mid-update, causing silent non-replies on Telegram and other channels), invalid candidates now fall back to the derived safe transcript path based onsessionId.resolveSessionFilePathinpaths.tsnow wrapsresolvePathWithinSessionsDirin a try/catch; if the persistedsessionFilefails validation (path traversal, absolute path outside sessions dir, etc.), it silently falls back to{sessionId}.jsonlget-reply-run.ts,agent-runner.ts,doctor-state-integrity.ts) don't have their own try/catch around this function, confirming the fix is necessary at the sourceConfidence Score: 5/5
resolveSessionFilePathwith a safe fallback. Security containment checks remain enforced for valid candidates. The fallback path is derived fromsessionIdwhich is validated byvalidateSessionId(alphanumeric +._-only). Tests cover all key scenarios. No new dependencies or architectural changes.Last reviewed commit: c06f155
(2/5) Greptile learns from your feedback when you react with thumbs up/down!