Skip to content

fix(opencode): handle stale session.directory when project moves#35492

Open
enihcam wants to merge 2 commits into
anomalyco:devfrom
enihcam:stale-session-directory-relink
Open

fix(opencode): handle stale session.directory when project moves#35492
enihcam wants to merge 2 commits into
anomalyco:devfrom
enihcam:stale-session-directory-relink

Conversation

@enihcam

@enihcam enihcam commented Jul 6, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #35427

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Three bug fixes addressing the same root cause: stale session.directory in the database never updates when the project directory moves or is deleted.

Fix 1 (system.ts) — catches location layer boot failures so ENOENT degrades to empty references instead of propagating a Die defect that bypasses handler error mapping.

Fix 2 (project.ts fromDirectory) — new relinkStaleSessions helper auto-relinks sessions under the current project_id whose stored directory no longer exists on disk.

Fix 3 (handlers/session.ts requireSession) — fail-fast with 400 BadRequest when the session directory doesn't exist, instead of hanging or 500-ing downstream.

How did you verify your code works?

  • bun typecheck on packages/opencode: only baseline pre-existing errors (not introduced by this PR)
  • All 33 tests pass (4 system + 7 migrate-global + 21 httpapi-session + 5 new tests)
  • Each new test confirmed to FAIL without its corresponding fix
  • Manual verification that the catchCause path returns empty references when location layer is down

Screenshots / recordings

N/A — backend-only changes.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

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)
@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:compliance This means the issue will auto-close after 2 hours. labels Jul 6, 2026
@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

500 error on POST /session/{id}/command when session.directory points to a moved/deleted project

1 participant