Skip to content

[bug] opencode session delete allows deleting the active session, causing data loss #37975

Closed as not planned

Description

@rock-solid-sites

Description

opencode session delete <sessionID> accepts the currently-active session ID without warning or error. Deleting the active session removes its row from the SQLite database while opencode is still using it. On the next message, opencode fails with FOREIGN KEY constraint failed when trying to persist a step-completion message, permanently killing the session.

This is distinct from #21236 (TUI keybind deletion) — this is the CLI opencode session delete command.

Expected Behavior

opencode session delete should refuse to delete the currently-active session:

Error: Cannot delete the currently-active session. Close the session first.

Or require --force to proceed.

Actual Behavior

The session is deleted silently. The active opencode instance crashes on the next message:

FOREIGN KEY constraint failed
  at Session.updateMessage
  at SessionProcessor.cleanup
  at SessionProcessor.process
  at SessionPrompt.run

Impact

  • Data loss: Session conversation, context, and in-progress work permanently lost
  • Agent risk: AI agents performing batch cleanup can accidentally delete their own session
  • No recovery: No undo or trash mechanism for opencode session delete

Steps to reproduce

  1. Start an opencode session: opencode
  2. Note the session ID: grep 'created id=' ~/.local/share/opencode/log/opencode.log | tail -1
  3. In a separate terminal: opencode session delete <that-session-id>
  4. Return to the opencode session and send a message
  5. Session errors with FOREIGN KEY constraint failed and is unusable

Real-world occurrence

An agent session performing database cleanup:

  1. Classified 969 sessions into A (ephemeral), B (productive), C (low-activity)
  2. Built a filtered delete list (840 sessions, excluding productive)
  3. Generated a shell script via opencode session delete
  4. Script referenced wrong input file (all 969 instead of filtered 840)
  5. Script deleted the agent's own session mid-operation
  6. Agent crashed with FOREIGN KEY constraint failed

The agent was Category B (productive, 844 events) and should not have been deleted. The bug is that opencode session delete didn't prevent self-deletion.

Suggested Fix

if (sessionID === currentSessionID) {
  console.error("Error: Cannot delete the currently-active session.")
  process.exit(1)
}

Related Issues

Plugins

None

OpenCode version

v1.18.3

Operating System

Linux (Ubuntu, Hostkey VPS)

Terminal

CLI (non-TUI)

Activity

  1. github-actions commented on Sep 19, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions