- Notifications
You must be signed in to change notification settings - Fork 0
fix: close bug-hunter stale-state paths (re-open)#146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6b7e98c
fix: close bug-hunter stale-state paths
BigSimmo 470df91
test: align bug-hunter expectations with main error contract
BigSimmo 2e0a47a
Merge branch 'main' into reopen-bug-hunter-fixes
BigSimmo 214c6fe
fix: harden rollback guards for review feedback
BigSimmo 6f58313
Merge branch 'main' into reopen-bug-hunter-fixes
BigSimmo b65cfd8
test: type remove storage mock as QueryError union
BigSimmo 60e5d23
Merge remote-tracking branch 'origin/main' into reopen-bug-hunter-fixes
BigSimmo ab6d70b
fix: fence stale-state rollbacks against concurrent queue mutations
BigSimmo e627b71
Merge branch 'main' into reopen-bug-hunter-fixes
BigSimmo 1d11515
Merge branch 'main' into reopen-bug-hunter-fixes
BigSimmo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -5,7 +5,11 @@ import { env, isDemoMode } from "@/lib/env"; | ||
| import { upsertDocumentEnrichment } from "@/lib/document-enrichment"; | ||
| import { upsertDocumentDeepMemory } from "@/lib/deep-memory"; | ||
| import { jsonError } from "@/lib/http"; | ||
| import { checkIngestionMutationSafety, ingestionMutationSafetyPayload } from "@/lib/ingestion-mutation-safety"; | ||
| import { | ||
| checkIngestionMutationSafety, | ||
| ingestionMutationSafetyPayload, | ||
| ingestionRollbackFenceStamp, | ||
| } from "@/lib/ingestion-mutation-safety"; | ||
| import { consumeApiRateLimit, rateLimitJsonResponse } from "@/lib/api-rate-limit"; | ||
| import { | ||
| committedIndexGeneration, | ||
| @@ -111,7 +115,7 @@ export async function POST(request: Request, { params }: { params: Promise<{ id: | ||
| const { data: document, error: documentError } = await supabase | ||
| .from("documents") | ||
| .select("id,owner_id,title,file_name,source_path,import_batch_id,status,metadata") | ||
| .select("id,owner_id,title,file_name,source_path,import_batch_id,status,error_message,page_count,chunk_count,image_count,metadata") | ||
| .eq("id", id) | ||
| .eq("owner_id", user.id) | ||
| .maybeSingle(); | ||
| @@ -180,12 +184,36 @@ export async function POST(request: Request, { params }: { params: Promise<{ id: | ||
| } | ||
| const atomicReindex = isAtomicReindexCandidate(document); | ||
| // Rollback fence: the queue-state write stamps updated_at with a | ||
| // per-request value and the rollback below matches on that stamp, making | ||
| // it a single conditional UPDATE that is atomic server-side. An | ||
| // overlapping reindex/retry re-stamps the row before enqueueing its own | ||
| // job, so a stale rollback from this request matches zero rows instead of | ||
| // reverting the newer queue state. The competing-job SELECT below is only | ||
| // a cheap fast path; the fence is what closes the check-then-write race. | ||
| const rollbackFence = ingestionRollbackFenceStamp(); | ||
| const rollbackDocumentPayload = atomicReindex | ||
| ? { error_message: document.error_message ?? null } | ||
| : { | ||
| status: document.status ?? null, | ||
| error_message: document.error_message ?? null, | ||
| page_count: document.page_count ?? 0, | ||
| chunk_count: document.chunk_count ?? 0, | ||
| image_count: document.image_count ?? 0, | ||
| }; | ||
| const { error: updateError } = await supabase | ||
| .from("documents") | ||
| .update( | ||
| atomicReindex | ||
| ? { error_message: null } | ||
| : { status: "queued", error_message: null, page_count: 0, chunk_count: 0, image_count: 0 }, | ||
| ? { error_message: null, updated_at: rollbackFence } | ||
| : { | ||
| status: "queued", | ||
| error_message: null, | ||
| page_count: 0, | ||
| chunk_count: 0, | ||
| image_count: 0, | ||
| updated_at: rollbackFence, | ||
| }, | ||
| ) | ||
| .eq("id", id) | ||
| .eq("owner_id", user.id); | ||
| @@ -204,7 +232,29 @@ export async function POST(request: Request, { params }: { params: Promise<{ id: | ||
| .select() | ||
| .single(); | ||
| if (jobError) throw new Error(jobError.message); | ||
| if (jobError) { | ||
| const { data: competingJobs, error: competingJobsError } = await supabase | ||
| .from("ingestion_jobs") | ||
| .select("id") | ||
| .eq("document_id", id) | ||
| .in("status", ["pending", "processing"]) | ||
| .limit(1); | ||
| if (competingJobsError) { | ||
| throw new Error(`Failed to enqueue reindex job: ${jobError.message}; competing-job check failed: ${competingJobsError.message}`); | ||
| } | ||
| if ((competingJobs?.length ?? 0) === 0) { | ||
| const { error: rollbackError } = await supabase | ||
| .from("documents") | ||
| .update(rollbackDocumentPayload) | ||
| .eq("id", id) | ||
| .eq("owner_id", user.id) | ||
| .eq("updated_at", rollbackFence); | ||
BigSimmo marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if (rollbackError) { | ||
| throw new Error(`Failed to enqueue reindex job: ${jobError.message}; rollback failed: ${rollbackError.message}`); | ||
| } | ||
| } | ||
| throw new Error(jobError.message); | ||
| } | ||
| return NextResponse.json({ job }, { status: 201 }); | ||
| } catch (error) { | ||
| if (error instanceof AuthenticationError) return unauthorizedResponse(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.