fix(server): retrying a new thread's first send no longer fails with "already exists" #7608
Closedpc-style(GitHub profile, opens in a new tab)wants to merge 4 commits into
main fromrefs/pull/7608/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XS
- apps/server/src/orchestration/commandInvariants.ts line 163Resolvedby pc-style(GitHub profile, opens in a new tab)@@ -156,4 +156,9 @@156156 readonly command: OrchestrationCommand;157157 readonly threadId: ThreadId;158158 }): Effect.Effect<void, OrchestrationCommandInvariantError> {159- if (!findThreadById(input.readModel, input.threadId)) {159+ // Thread deletion is a soft delete, and thread IDs are client-generated and160+ // stable per draft: after a failed first-turn bootstrap deletes its thread,161+ // the retry re-creates the same ID. A deleted thread therefore counts as162+ // absent; `thread.created` projections reset the row wholesale.163+ const thread = findThreadById(input.readModel, input.threadId);164+ if (!thread || thread.deletedAt !== null) {
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:XS
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
- apps/server/src/orchestration/Layers/ProjectionPipeline.ts line 611OutdatedResolvedby pc-style(GitHub profile, opens in a new tab)@@ -601,4 +601,11 @@601601 "applyThreadsProjection",602602 )(function* (event, attachmentSideEffects) {603603 switch (event.type) {604- case "thread.created":604+ case "thread.created": {605+ // A draft retry can re-create a soft-deleted thread id. Purge the606+ // old incarnation's dependent rows so the re-created thread starts607+ // clean, like the in-memory projector's wholesale replace.608+ const existingRow = yield* projectionThreadRepository.getById({609+ threadId: event.payload.threadId,610+ });611+ if (Option.isSome(existingRow)) {
- apps/server/src/orchestration/Layers/ProjectionPipeline.ts line 638OutdatedResolvedby pc-style(GitHub profile, opens in a new tab)@@ -604,0 +618,21 @@618+ yield* projectionThreadProposedPlanRepository.deleteByThreadId({619+ threadId: event.payload.threadId,620+ });621+ yield* projectionThreadSessionRepository.deleteByThreadId({622+ threadId: event.payload.threadId,623+ });624+ yield* projectionTurnRepository.deleteByThreadId({625+ threadId: event.payload.threadId,626+ });627+ const pendingApprovals = yield* projectionPendingApprovalRepository.listByThreadId({628+ threadId: event.payload.threadId,629+ });630+ yield* Effect.forEach(631+ pendingApprovals,632+ (approval) =>633+ projectionPendingApprovalRepository.deleteByRequestId({634+ requestId: approval.requestId,635+ }),636+ { concurrency: 1 },637+ ).pipe(Effect.asVoid);638+ }
- apps/server/src/orchestration/commandInvariants.ts line 163Resolvedby pc-style(GitHub profile, opens in a new tab)@@ -156,4 +156,8 @@156156 readonly command: OrchestrationCommand;157157 readonly threadId: ThreadId;158158 }): Effect.Effect<void, OrchestrationCommandInvariantError> {159- if (!findThreadById(input.readModel, input.threadId)) {159+ // Deletion is a soft delete and draft retries reuse their client-generated160+ // thread id, so a deleted thread counts as absent. Projections reset the161+ // row wholesale on thread.created.162+ const thread = findThreadById(input.readModel, input.threadId);163+ if (!thread || thread.deletedAt !== null) {
- apps/server/src/orchestration/Layers/ThreadDeletionReactor.ts line 76Resolvedby pc-style(GitHub profile, opens in a new tab)@@ -60,3 +72,7 @@6072 event: ThreadDeletedEvent,6173 ) {6274 const { threadId } = event.payload;75+ const thread = yield* projectionThreadRepository76+ .getById({ threadId })77+ .pipe(Effect.orElseSucceed(() => Option.none<ProjectionThread>()));78+ if (shouldSkipThreadDeletionCleanup(thread)) {apps/server/src/orchestration/Layers/ThreadDeletionReactor.ts lines 75 to 77OutdatedResolvedby pc-style(GitHub profile, opens in a new tab)@@ -60,3 +72,6 @@6072 event: ThreadDeletedEvent,6173 ) {6274 const { threadId } = event.payload;75+ const thread = yield* projectionThreadRepository76+ .getById({ threadId })77+ .pipe(Effect.orElseSucceed(() => Option.none<ProjectionThread>()));
- apps/server/src/orchestration/Layers/ThreadDeletionReactor.ts line 79Resolvedby pc-style(GitHub profile, opens in a new tab)@@ -60,3 +72,8 @@6072 event: ThreadDeletedEvent,6173 ) {6274 const { threadId } = event.payload;75+ const thread = yield* projectionThreadRepository.getById({ threadId });76+ if (shouldSkipThreadDeletionCleanup(thread)) {77+ yield* Effect.logDebug("thread deletion cleanup skipped a re-created thread", { threadId });78+ return;79+ }
shivamhwp(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
af55f9d4-9aaf-4606-8be3-9f5d6b3631afYou can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share