Skip to content

fix(cli): prevent tests from deleting session database - #11316

Merged
marius-kilocode merged 1 commit into
mainfrom
delirious-lung
Jun 16, 2026
Merged

fix(cli): prevent tests from deleting session database#11316
marius-kilocode merged 1 commit into
mainfrom
delirious-lung

Conversation

@marius-kilocode

Copy link
Copy Markdown
Collaborator

The shared database reset fixture could delete the real local Kilo session database when a test was launched without the package test preload. This is destructive because the fixture removed the resolved SQLite database together with its WAL and SHM files, while treating test-runner configuration as its only safety boundary. A missed preload could therefore permanently remove every conversation stored only in the shared database.

The complete failure requires all of these conditions:

  1. A database-resetting test, specifically packages/opencode/test/server/httpapi-sdk.test.ts in the observed incident, is run from a directory where Bun does not discover packages/opencode/bunfig.toml.
  2. Because that Bun configuration is not loaded, packages/opencode/test/preload.ts is not applied and KILO_DB=:memory: is never set.
  3. The test process inherits KILO_DISABLE_CHANNEL_DB=true. The managed VS Code backend adds that variable in packages/kilo-vscode/src/services/cli-backend/server-manager.ts so extension contexts use one shared session database.
  4. With no KILO_DB override, Database.getPath() in packages/opencode/src/storage/db.ts honors the inherited channel setting and resolves to ~/.local/share/kilo/kilo.db.
  5. resetDatabase() in packages/opencode/test/fixture/db.ts closes the client and unlinks that resolved path, kilo.db-wal, and kilo.db-shm. The helper runs from both the afterEach cleanup and the default-versus-raw server parity reset in httpapi-sdk.test.ts.
  6. The test opens a new database at the same path. Its session lifecycle scenario creates the identifiable parent and child sessions, leaving a valid but nearly empty replacement database.

At that point, conversations stored only in the previous SQLite database are gone. Agent Manager worktrees, git branches, and .kilo/agent-manager.json remain because they are stored separately, which makes the UI look as though its sessions disappeared while the worktree metadata still exists.

The unsafe helper originated in upstream OpenCode PR #15120, where tests used a temporary disk-backed XDG data directory and deleting the database was intended to reset state. Upstream PRs #24836 and #24853 expanded the SDK route and parity coverage that invokes this reset, including the parent and child lifecycle scenario. Kilo later switched package tests to an in-memory database, so filesystem deletion was no longer required.

Kilo PR #11031 adapted the fixture from the former static database path to Database.getPath(). Kilo PR #11087 intentionally added KILO_DISABLE_CHANNEL_DB=true to the managed VS Code server environment to restore one shared session database across extension contexts. Each change was reasonable in isolation, but together they allowed a test process that missed its preload to resolve the cleanup target to the real shared database.

This change removes every filesystem deletion from resetDatabase(). The fixture now accepts only the exact :memory: database, then disposes shared instances and closes the in-memory SQLite connection. Closing that connection clears its contents and resets the lazy client, so the next test still receives a clean database. Any disk-backed path fails before disposal or closure, regardless of the current directory or inherited environment.

A Kilo-owned regression test documents the incident and enforces both safety boundaries. It points KILO_DB at a disposable sentinel file and proves the reset is rejected without changing the file. It also scans the test tree for direct deletion code combined with Database.getPath(), Database.Path, or a kilo.db path, so the removed database, WAL, and SHM cleanup cannot be reintroduced elsewhere. The existing HTTP API SDK coverage remains unchanged.

@kilo-code-bot

kilo-code-bot Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (2 files)
  • packages/opencode/test/fixture/db.ts
  • packages/opencode/test/kilocode/database-reset-safety.test.ts

Reviewed by deepseek-v4-pro-20260423 · 400,736 tokens

Review guidance: REVIEW.md from base branch main

@marius-kilocode
marius-kilocode disabled auto-merge June 16, 2026 17:10
@marius-kilocode
marius-kilocode merged commit 1b65317 into main Jun 16, 2026
20 checks passed
@marius-kilocode
marius-kilocode deleted the delirious-lung branch June 16, 2026 17:19
t7tran pushed a commit to t7tran/kilocode that referenced this pull request Aug 14, 2026
fix(cli): prevent tests from deleting session database
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.

2 participants