fix(opencode): a purge takes its child sessions' 1.x rows, the background service alone holds no session, XDG_DATA_HOME is followed (Issue #60, review)

- Review 614b6263 (codex, P2): `opencode session delete` removes the child
  sessions with their parent, but their 1.x rows stayed and the parent's
  residual was empty. The child sessions (session_v2 and a 1.x session
  table, any depth) are taken before the delete; the 1.x tables (message,
  part, todo, session_share, session) are cleaned for all of them, and the
  residual also reports a child still on record (a failed delete).
- The in-use check counted any process holding the shared opencode.db; the
  per-user 2.x background service (`serve --service`) holds it for every
  session, so every opencode purge was refused while it ran (it does on
  this Mac). It no longer counts; any other holder and a command line
  naming the session still do.
- The data dir follows the caller's XDG_DATA_HOME when ctx is the caller's
  own home (else <home>/.local/share), for debug paths, the fallback and the
  residual scan (review 614b6263 / f4cedbae N4).
- resident_children = ('opencode',): the --standalone server stays the TUI's
  idle child; what runs under it still counts (review 1abe0a90).
- Tests: child sessions on a migrated database, a failed delete, the service
  holder, XDG, resident children, drift C pinning the parent over a newer
  child session (review f4cedbae N2), and the real-binary test no longer
  depends on PATH (N1).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VCgu9Fa9iT7TbJUSzdq4Ff
This commit is contained in:
GodopuandClaude Opus 5.5 committed 2026-10-18 15:46:04 +09:00
1 parent 6b4b96fa18
commit cb5f439cce
4 files changed
+245 -41

No files matched your search

+4 -4
View File
@@ -896,10 +896,10 @@ def _db(path: Path, ddl: list, rows: list):
conn.close()
def test_opencode_residual_reports_event_rows(home, tmp_path, monkeypatch):
def test_opencode_purge_leaves_no_rows(home, tmp_path, monkeypatch):
# A database migrated from 1.x (Issue #60): `opencode session delete`
# (the fake, as measured on 2.0.32) removes the 2.x rows, the purge
# deletes the 1.x session/message/part rows, and todo is reported.
# (the fake, as measured on 2.0.32) removes the 2.x rows and the purge
# deletes the rows of the 1.x tables 2.x no longer writes.
u, o = f"ses_{uuidlib.uuid4().hex}", f"ses_{uuidlib.uuid4().hex}"
db = home / ".local" / "share" / "opencode" / "opencode.db"
ddl = ["CREATE TABLE session (id TEXT PRIMARY KEY)",
@@ -931,7 +931,7 @@ def test_opencode_residual_reports_event_rows(home, tmp_path, monkeypatch):
assert adapter.purge_artifacts(u, ctx) == [f"opencode session (with its child sessions): {u}",
f"sqlite rows for session: {u}"]
assert adapter.residual_artifacts(u, ctx) == [f"{db} (todo.session_id: 1 rows)"]
assert adapter.residual_artifacts(u, ctx) == []
assert len(adapter.residual_artifacts(o, ctx)) == 8 # the other session is untouched: one entry per table