fix(opencode): handle stale session.directory gracefully - #35479
Conversation
Fixes three related issues caused by stale session.directory values stored in the database after a project directory is moved or deleted: - anomalyco#35427: HTTP 500 from POST /session/{id}/command when session.directory points to a missing directory. SystemPrompt.environment() propagated a Die defect that bypassed the BadRequest handler. - anomalyco#33909: CLI opencode run --session <id> --dir <new-path> hangs at 'loop step=0'. The --dir flag was ignored because session loading always used session.directory from the DB. - anomalyco#23248: Sessions under project_id=global with old directory paths become orphaned when the project directory is renamed. Changes: - session/system.ts: wrap references computation in catchCause so a failing location layer (ENOENT) degrades to empty references instead of crashing - project/project.ts: add relinkStaleSessions in fromDirectory() that updates session.directory for sessions under the same project whose stored directory no longer exists on disk - handlers/session.ts: add directory existence check in requireSession that returns BadRequest for sessions with stale directories - groups/session.ts: add HttpApiError.BadRequest to share/unshare error schemas - Add regression tests in test/session/system.test.ts (catchCause), test/project/migrate-global.test.ts (relinkStaleSessions), and test/server/httpapi-session.test.ts (requireSession BadRequest)
|
This PR doesn't fully meet our contributing guidelines and PR template. What needs to be fixed:
Please edit this PR description to address the above within 2 hours, or it will be automatically closed. If you believe this was flagged incorrectly, please let a maintainer know. |
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
|
The following comment was made by an LLM, it may be inaccurate: Based on the search results, I found two potentially related PRs that address similar issues:
These PRs handle related problems (moved/deleted projects) but appear to be in different packages (desktop and core) rather than the opencode package. PR #35479 (current PR) specifically targets the opencode package's session management layer with the relinking and error handling fixes. The current PR is comprehensive in addressing issues #35427, #33909, and #23248 specifically for session directory staleness, and doesn't appear to duplicate the scope of these other PRs. |
|
This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window. Feel free to open a new pull request that follows our guidelines. |
Summary
Fixes three related issues caused by stale values stored in the database after a project directory is moved or deleted:
Changes
Fix 1: — catchCause in SystemPrompt.environment()
Wrapped the computation with so a failing location layer (ENOENT) degrades to empty references instead of crashing with a defect.
Fix 2: — relinkStaleSessions in fromDirectory()
Added a helper that finds sessions under the current whose doesn't exist on disk and updates them to the current directory. Auto-relinks sessions when a project moves.
Fix 3: — fail-fast in requireSession()
Added directory existence check in . If doesn't exist on disk, returns .
Fix 4: — HttpApiError.BadRequest in error schemas
Added to and error schemas.
Tests
All 33 tests pass (4 original + 1 new system tests, 7 original + 3 new migrate-global tests, 21 original httpapi-session tests).
Verification