Repository navigation
Description
Bug
POST /session/{id}/command returns HTTP 500 when the session's stored directory in the database points to a path that no longer exists (e.g. the project was moved or deleted after the session was created).
Steps to Reproduce
- Open a project at
/path/to/old-locationin the web UI - Send a command message — works fine
- Move the project directory:
mv /path/to/old-location /path/to/new-location - Restart opencode (
opencode web), open the web UI - The old session appears in the session list
- Type a message and send → HTTP 500 returned
noReply: truemessages (e.g./compact) succeed because they bypass the code path that validates the directory
Root Cause
The WorkspaceRoutingMiddleware extracts session?.directory from the session's database row and sets it as the routing directory. This directory is used by InstanceContextMiddleware to load the instance context, which initializes a FileSystem service rooted at that path. When the directory no longer exists, FileSystem.realPath() fails with ENOENT.
Because SystemPrompt.environment() (system.ts) has signature Effect<string[]> (no error type), the Fail from a missing directory is converted to a Die defect. This Die bypasses the handler's Effect.mapError(() => new HttpApiError.BadRequest({})) — which only catches Fail causes — and hits the default 500 error handler.
Error chain:
SessionHttpApi.command
→ SessionPrompt.command → prompt → loop → ensureRunning → runLoop
→ SystemPrompt.environment()
→ ctx.directory (from stale session row)
→ LocationServiceMap.get(ref) → FileSystem.realPath(stale directory) → ENOENT
→ Die defect → bypasses Effect.mapError(→BadRequest) → 500
Affected code
packages/opencode/src/session/system.ts—SystemPrompt.environment()has no error channel, so any location boot failure becomes a Diepackages/opencode/src/project/project.ts—fromDirectory()does not relink stale session directories when a project movespackages/opencode/src/server/routes/instance/httpapi/handlers/session.ts—requireSession()does not validate that the stored directory still exists on disk
Related issues
- Moving a project directory breaks all existing sessions created in the original one #33909 — CLI
opencode run --session <id>hangs silently when session.directory is stale - Sessions become orphaned when project directory is renamed #23248 — Sessions under project_id=global with old directory paths become orphaned
Suggested fix approach
Three fixes that together cover all three related reports:
- system.ts: Wrap the location layer call with
Effect.catchCauseso ENOENT degrades to empty references instead of a Die defect - project.ts: In
fromDirectory(), auto-relink sessions under the same project_id whose directory no longer exists on disk - handlers/session.ts: In
requireSession(), check that the stored directory exists on disk and return400 BadRequestif not
Activity
github-actions commentedon Jul 6, 2026
This issue might be a duplicate of existing issues. Please check:
project.worktreeafter project folder is renamed on disk; remote clients keep reopening the dead path #35240: Server keeps staleproject.worktreeafter project folder is renamed on diskIf this is intentionally a refined repost of #35427, please close the earlier one and note it there.
enihcam commentedon Jul 6, 2026
Closing as duplicate of #35427 — that issue has the same root cause with more code-location detail. The fix PR #35492 now references #35427.
Hona commentedon Sep 2, 2026
Fixed for opencode2 launch via #46713
You can try the desktop beta here: https://github.com/anomalyco/opencode-beta