Skip to content

fix(opencode): handle stale session.directory gracefully - #35479

Closed
enihcam wants to merge 1 commit into
anomalyco:devfrom
enihcam:stale-session-directory-relink
Closed

fix(opencode): handle stale session.directory gracefully#35479
enihcam wants to merge 1 commit into
anomalyco:devfrom
enihcam:stale-session-directory-relink

Conversation

@enihcam

@enihcam enihcam commented Jul 5, 2026

Copy link
Copy Markdown

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

  • : Regression test verifying succeeds when location layer fails to boot
  • : 3 tests for (no-op when dir exists, relinks when dir gone, skips wrong project)
  • : Test for returning 400 for stale directory

All 33 tests pass (4 original + 1 new system tests, 7 original + 3 new migrate-global tests, 21 original httpapi-session tests).

Verification

bun typecheck --cwd packages/opencode  # ✅ Only pre-existing baseline errors
bun test --cwd packages/opencode test/session/system.test.ts test/project/migrate-global.test.ts test/server/httpapi-session.test.ts  # ✅ 33 pass

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 the needs:compliance This means the issue will auto-close after 2 hours. label Jul 5, 2026
@github-actions

github-actions Bot commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

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.

@github-actions

github-actions Bot commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions

github-actions Bot commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

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:

  1. PR fix(desktop): handle moved projects and deleted paths #34137 - fix(desktop): handle moved projects and deleted paths

    • Addresses similar concerns about handling moved projects and deleted paths, but in the desktop context
  2. PR fix(core): relocate moved project checkouts #34142 - fix(core): relocate moved project checkouts

    • Deals with relocating moved project checkouts, which overlaps conceptually with the session directory relinking logic

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.

@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

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.

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Jul 6, 2026
@github-actions github-actions Bot closed this Jul 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant