chore(platform): clean up sub-threads when parent thread is deleted#486
Conversation
When a parent thread is archived, schedule an async cleanup that archives its sub-threads. Sub-thread IDs are parsed from the parent's summary JSON and the cleanup runs via ctx.scheduler.runAfter(0, ...) to avoid blocking the deletion mutation. Refs #73
📝 WalkthroughWalkthroughThe changes implement sub-thread cleanup functionality triggered when a parent thread is deleted. A new Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related issues
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@services/platform/convex/threads/delete_chat_thread.ts`:
- Around line 10-17: The code calls components.agent.threads.updateThread
unconditionally after fetching the thread with
components.agent.threads.getThread; add an explicit guard on the fetched thread
(e.g., check if thread is null/undefined) and skip or return early (or throw a
404) instead of calling updateThread when no thread exists; ensure you reference
the same threadId and preserve the existing patch { status: 'archived' }
behavior when the thread is present.
Greptile SummaryThis PR adds sub-thread cleanup when a parent chat thread is deleted (archived). The
Confidence Score: 4/5
|
| Filename | Overview |
|---|---|
| services/platform/convex/threads/delete_chat_thread.ts | Adds getThread query before archiving to extract sub-thread IDs from summary, then schedules async cleanup. Defensive parsing with try/catch. Archives parent even when thread not found (pre-existing behavior). |
| services/platform/convex/threads/internal_mutations.ts | Adds cleanupOrphanedSubThreads internal mutation that iterates sub-thread IDs, checks each is active, and archives them sequentially. Includes logging of archived count. |
| services/platform/convex/threads/delete_chat_thread.test.ts | New test file with thorough unit tests for parseSubThreadIds (7 edge cases) and deleteChatThread (4 scenarios covering archive, no sub-threads, with sub-threads, and missing thread). No tests for the cleanupOrphanedSubThreads mutation itself. |
Sequence Diagram
sequenceDiagram
participant Client
participant deleteChatThread
participant AgentSDK as Agent SDK (Threads)
participant Scheduler
participant cleanupMutation as cleanupOrphanedSubThreads
Client->>deleteChatThread: delete(threadId)
deleteChatThread->>AgentSDK: getThread(threadId)
AgentSDK-->>deleteChatThread: thread (with summary)
deleteChatThread->>AgentSDK: updateThread(archived)
deleteChatThread->>deleteChatThread: parseSubThreadIds(summary)
alt Sub-threads exist
deleteChatThread->>Scheduler: runAfter(0, cleanupOrphanedSubThreads)
Scheduler->>cleanupMutation: execute(parentThreadId, subThreadIds)
loop For each subThreadId
cleanupMutation->>AgentSDK: getThread(subThreadId)
AgentSDK-->>cleanupMutation: subThread
alt subThread is active
cleanupMutation->>AgentSDK: updateThread(archived)
end
end
cleanupMutation-->>Scheduler: { archivedCount }
end
deleteChatThread-->>Client: void
Last reviewed commit: cadc6b6
Summary
parseSubThreadIdshelper to extract sub-thread IDs from the thread summary JSONcleanupOrphanedSubThreadsinternal mutation that archives active sub-threadsTest plan
parseSubThreadIdscovering edge cases (undefined, empty, invalid JSON, valid data)deleteChatThreadverifying sub-thread cleanup is scheduled only when sub-threads exist🤖 Generated with Claude Code
Summary by CodeRabbit
Release Notes