Fix resumed session reactivation#2518
Conversation
Greptile SummaryThis PR fixes a state-consistency bug where a resumed SDK session row could remain in
Confidence Score: 5/5The change is safe to merge — it fixes a real state-consistency bug with a narrow, well-tested UPDATE, and both parallel implementations are kept in sync. The reactivation logic is straightforward: read the status, conditionally issue one UPDATE, return the existing ID. Tests directly validate the status flip, timestamp reset, and cleared completion fields. No existing code path sets sdk_sessions.status = 'failed', so the unhandled terminal status is not a live defect today. No files require special attention. Both SessionStore.ts and sessions/create.ts are updated symmetrically. Important Files Changed
Sequence DiagramsequenceDiagram
participant Caller
participant createSDKSession
participant SQLite
Caller->>createSDKSession: createSDKSession(contentSessionId, ...)
createSDKSession->>SQLite: "SELECT id, platform_source, status FROM sdk_sessions WHERE content_session_id = ?"
SQLite-->>createSDKSession: "{ id, platform_source, status }"
alt "existing row AND status = 'completed'"
createSDKSession->>SQLite: "UPDATE sdk_sessions SET status='active', completed_at=NULL, completed_at_epoch=NULL, started_at=now, started_at_epoch=now WHERE id = existing.id"
createSDKSession-->>Caller: existing.id (reactivated)
else "existing row AND status != 'completed'"
createSDKSession-->>Caller: existing.id (unchanged)
else no existing row
createSDKSession->>SQLite: INSERT INTO sdk_sessions (...) VALUES (...)
createSDKSession->>SQLite: "SELECT id FROM sdk_sessions WHERE content_session_id = ?"
SQLite-->>createSDKSession: "{ id }"
createSDKSession-->>Caller: new id
end
Prompt To Fix All With AIFix the following 2 code review issues. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 2
src/services/sqlite/sessions/create.ts:64
The reactivation guard checks only for `'completed'`, but the schema's `CHECK` constraint also allows `'failed'` (`CHECK(status IN ('active', 'completed', 'failed'))`). If a session ever enters `'failed'` state, a resumed call with the same `content_session_id` would return the existing ID without reactivating it, leaving the row stuck as `failed` while the caller believes work is proceeding normally. No current code path sets `sdk_sessions.status = 'failed'`, but the guard would silently do nothing if that changes.
```suggestion
if (existing.status === 'completed' || existing.status === 'failed') {
```
### Issue 2 of 2
src/services/sqlite/SessionStore.ts:1695
Same gap as in `create.ts`: the reactivation check covers only `'completed'` but the schema permits `'failed'` as a third terminal status. A session stuck in `'failed'` would be returned as-is without reactivation if this code path is ever reached with that state.
```suggestion
if (existing.status === 'completed' || existing.status === 'failed') {
```
Reviews (2): Last reviewed commit: "Fix resumed session reactivation" | Re-trigger Greptile |
# Conflicts: # tests/session_store.test.ts
1665461 to
8566fef
Compare
Summary
Reactivates completed SDK sessions when the same
content_session_idreceives new work after being resumed.Changes include:
content_session_id.active.completed_atandcompleted_at_epochwhen a completed session is reactivated.SessionStoreand the standalone session creation helper.Motivation
A resumed content session can reuse the same SDK session row. If that row remains marked as
completed, later processing can treat an active resumed session as finished. Reactivating the row keeps session state consistent with new incoming work.Validation
This PR is opened as a draft so repository CI can validate it against current upstream. Local review was performed before publishing.