fix: align applied Supabase migration history#552
Conversation
Record the generated production migration versions and keep the legacy unfenced commit overload unavailable to service_role. Regenerated drift state and verified live drift, readiness, concurrency, and retrieval contracts.
|
Updates to Preview Branch (codex/live-migration-history-alignment) ↗︎
Tasks are run on every commit but only new migration files are pushed.
View logs for this Workflow Run ↗︎. |
📝 WalkthroughWalkthroughFour migrations update lexical retrieval, lease-fenced indexing commits, enrichment job processing, and scheduled RAG cache cleanup. Schema permissions, privilege tests, drift metadata, and rollout documentation reflect the applied remediation state. ChangesRetrieval and indexing remediation
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant EnrichmentRequester
participant request_indexing_v3_enrichment
participant indexing_v3_agent_jobs
EnrichmentRequester->>request_indexing_v3_enrichment: request enrichment
request_indexing_v3_enrichment->>indexing_v3_agent_jobs: insert or lock job and read status
indexing_v3_agent_jobs-->>request_indexing_v3_enrichment: return job status
request_indexing_v3_enrichment-->>EnrichmentRequester: accept pending request or raise enrichment_active
Caution Pre-merge checks failedPlease resolve all errors before merging. Addressing warnings is optional.
❌ Failed checks (2 errors, 1 warning)
✅ Passed checks (8 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
✨ Simplify code
Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@supabase/migrations/20260713062139_route_enrichment_through_agent.sql`:
- Around line 56-66: Update the metadata cleanup in the document reset statement
to also remove indexing_v3_agent_attempt_count and
indexing_v3_agent_max_attempts, alongside the existing lock, error, and
scheduling fields. Keep the pending status fields and updated_at assignment
unchanged.
- Around line 13-16: Update request_indexing_v3_enrichment so it acquires locks
in the same order as claim_indexing_v3_agent_jobs: lock the relevant
indexing_v3_agent_jobs row before locking or updating the public.documents row.
Preserve the existing document ownership, ID, and indexed-status checks while
eliminating the document-first lock order.
- Around line 41-54: Update the v_job_id creation branch in the enrichment RPC
to make the indexing_v3_agent_jobs insert conflict-safe: add ON CONFLICT
(document_id) DO NOTHING, then read the existing row id when the insert does not
create one. Preserve the enrichment_active behavior by checking the resulting
job’s status and returning that error for an already-processing job instead of
propagating a 23505 uniqueness failure.
🪄 Autofix (Beta)
❌ Autofix failed (check again to retry)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: e940efe9-d395-406c-8c5d-a5a9def311c5
📒 Files selected for processing (9)
docs/branch-review-ledger.mddocs/forward-codify-retrieval-rpcs-workorder.mdsupabase/drift-manifest.jsonsupabase/migrations/20260713062107_restore_text_fallback_lexical_score.sqlsupabase/migrations/20260713062125_fence_index_generation_commit.sqlsupabase/migrations/20260713062132_purge_expired_rag_response_cache.sqlsupabase/migrations/20260713062139_route_enrichment_through_agent.sqlsupabase/schema.sqltests/supabase-schema.test.ts
There was a problem hiding this comment.
Caution
Inline review comments failed to post. This is likely due to GitHub's internal server error or limits when posting large numbers of comments. If you are seeing this consistently it is likely a permissions issue. Please check "Moderation" -> "Code review limits" under your organization settings.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@supabase/migrations/20260713062139_route_enrichment_through_agent.sql`:
- Around line 56-66: Update the metadata cleanup in the document reset statement
to also remove indexing_v3_agent_attempt_count and
indexing_v3_agent_max_attempts, alongside the existing lock, error, and
scheduling fields. Keep the pending status fields and updated_at assignment
unchanged.
- Around line 13-16: Update request_indexing_v3_enrichment so it acquires locks
in the same order as claim_indexing_v3_agent_jobs: lock the relevant
indexing_v3_agent_jobs row before locking or updating the public.documents row.
Preserve the existing document ownership, ID, and indexed-status checks while
eliminating the document-first lock order.
- Around line 41-54: Update the v_job_id creation branch in the enrichment RPC
to make the indexing_v3_agent_jobs insert conflict-safe: add ON CONFLICT
(document_id) DO NOTHING, then read the existing row id when the insert does not
create one. Preserve the enrichment_active behavior by checking the resulting
job’s status and returning that error for an already-processing job instead of
propagating a 23505 uniqueness failure.
🪄 Autofix (Beta)
❌ Autofix failed (check again to retry)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: e940efe9-d395-406c-8c5d-a5a9def311c5
📒 Files selected for processing (9)
docs/branch-review-ledger.mddocs/forward-codify-retrieval-rpcs-workorder.mdsupabase/drift-manifest.jsonsupabase/migrations/20260713062107_restore_text_fallback_lexical_score.sqlsupabase/migrations/20260713062125_fence_index_generation_commit.sqlsupabase/migrations/20260713062132_purge_expired_rag_response_cache.sqlsupabase/migrations/20260713062139_route_enrichment_through_agent.sqlsupabase/schema.sqltests/supabase-schema.test.ts
🛑 Comments failed to post (3)
supabase/migrations/20260713062139_route_enrichment_through_agent.sql (3)
13-16: 🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash set -euo pipefail git status --short git ls-files supabase/migrations/20260713062139_route_enrichment_through_agent.sql ast-grep outline supabase/migrations/20260713062139_route_enrichment_through_agent.sql --view expanded sed -n '1,220p' supabase/migrations/20260713062139_route_enrichment_through_agent.sqlRepository: BigSimmo/Database
Length of output: 6782
🏁 Script executed:
#!/bin/bash set -euo pipefail nl -ba supabase/migrations/20260713062139_route_enrichment_through_agent.sql | sed -n '1,220p' | sed -n '1,180p'Repository: BigSimmo/Database
Length of output: 195
🏁 Script executed:
#!/bin/bash set -euo pipefail awk '{printf "%4d %s\n", NR, $0}' supabase/migrations/20260713062139_route_enrichment_through_agent.sql | sed -n '1,180p'Repository: BigSimmo/Database
Length of output: 7688
Keep document/job lock order consistent to avoid deadlocks.
request_indexing_v3_enrichmentlocks the document row before touchingindexing_v3_agent_jobs, whileclaim_indexing_v3_agent_jobslocks the job row first and only then updates the document row. If both run against the same document at the same time, Postgres can end up with a circular wait and abort one transaction.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@supabase/migrations/20260713062139_route_enrichment_through_agent.sql` around lines 13 - 16, Update request_indexing_v3_enrichment so it acquires locks in the same order as claim_indexing_v3_agent_jobs: lock the relevant indexing_v3_agent_jobs row before locking or updating the public.documents row. Preserve the existing document ownership, ID, and indexed-status checks while eliminating the document-first lock order.
41-54: 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash set -euo pipefail printf '\n== target migration excerpt ==\n' sed -n '1,140p' supabase/migrations/20260713062139_route_enrichment_through_agent.sql printf '\n== claim function excerpt ==\n' rg -n -A80 -B20 'claim_indexing_v3_agent_jobs' supabase/migrations printf '\n== unique index / constraint references ==\n' rg -n 'indexing_v3_agent_jobs_document_id_idx|UNIQUE INDEX .*indexing_v3_agent_jobs|create unique index .*indexing_v3_agent_jobs' supabase -S printf '\n== any triggers or locks on documents / agent jobs ==\n' rg -n 'FOR UPDATE|lock.*documents|indexing_v3_agent_jobs' supabase/migrations -SRepository: BigSimmo/Database
Length of output: 50374
🏁 Script executed:
#!/bin/bash set -euo pipefail printf '\n== metadata key usage ==\n' rg -n 'indexing_v3_agent_attempt_count|indexing_v3_agent_max_attempts' supabase -S printf '\n== job row writes/reads around request/claim status ==\n' rg -n 'attempt_count|max_attempts|enrichment_status|indexing_v3_agent_status' supabase/migrations/20260713062139_route_enrichment_through_agent.sql supabase/migrations/20260702190000_indexing_v3_agent_jobs_table.sql -n -SRepository: BigSimmo/Database
Length of output: 12971
Avoid the concurrent insert race here
public.indexing_v3_agent_jobs.document_idis unique, and theFOR UPDATEondocumentsdoesn’t protect this insert. A concurrentclaim_indexing_v3_agent_jobsinsert can still win and make this RPC fail with23505instead of returningenrichment_active; addON CONFLICT (document_id) DO NOTHINGand read back the existing row id.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@supabase/migrations/20260713062139_route_enrichment_through_agent.sql` around lines 41 - 54, Update the v_job_id creation branch in the enrichment RPC to make the indexing_v3_agent_jobs insert conflict-safe: add ON CONFLICT (document_id) DO NOTHING, then read the existing row id when the insert does not create one. Preserve the enrichment_active behavior by checking the resulting job’s status and returning that error for an already-processing job instead of propagating a 23505 uniqueness failure.
56-66: 🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win
Reset doesn't clear stale
indexing_v3_agent_attempt_count/_max_attemptsfrom document metadata.
claim_indexing_v3_agent_jobswritesindexing_v3_agent_attempt_countandindexing_v3_agent_max_attemptsintodocuments.metadata(lines 143-144 below) whenever a job is claimed. This reset only strips_locked_by,_locked_at,_last_error,_next_run_at, so after a user-triggered reset the document metadata can still show a stale attempt count from the prior cycle even though the job row'sattempt_countwas reset to 0.🛠️ Proposed fix
update public.documents set metadata = (coalesce(metadata, '{}'::jsonb) - 'indexing_v3_agent_locked_by' - 'indexing_v3_agent_locked_at' - - 'indexing_v3_agent_last_error' - 'indexing_v3_agent_next_run_at') + - 'indexing_v3_agent_last_error' - 'indexing_v3_agent_next_run_at' + - 'indexing_v3_agent_attempt_count' - 'indexing_v3_agent_max_attempts') || jsonb_build_object(📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.update public.documents set metadata = (coalesce(metadata, '{}'::jsonb) - 'indexing_v3_agent_locked_by' - 'indexing_v3_agent_locked_at' - 'indexing_v3_agent_last_error' - 'indexing_v3_agent_next_run_at' - 'indexing_v3_agent_attempt_count' - 'indexing_v3_agent_max_attempts') || jsonb_build_object( 'enrichment_status', 'pending', 'indexing_v3_agent_status', 'pending', 'indexing_v3_agent_updated_at', now() ), updated_at = now() where id = p_document_id and owner_id = p_owner_id;🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@supabase/migrations/20260713062139_route_enrichment_through_agent.sql` around lines 56 - 66, Update the metadata cleanup in the document reset statement to also remove indexing_v3_agent_attempt_count and indexing_v3_agent_max_attempts, alongside the existing lock, error, and scheduling fields. Keep the pending status fields and updated_at assignment unchanged.
|
@copilot resolve the merge conflicts on this branch. |
|
Note Autofix is a beta feature. Expect some limitations and changes as we gather feedback and continue to improve it. Autofix skipped. No unresolved CodeRabbit review comments with fix instructions found. |
|
Note Autofix is a beta feature. Expect some limitations and changes as we gather feedback and continue to improve it. Autofix skipped. No unresolved CodeRabbit review comments with fix instructions found. |
…history-alignment # Conflicts: # docs/branch-review-ledger.md
Head branch was pushed to by a user without write access
Done — the only conflict was in |
|
@codex fix all issues on this PR including comments |
|
Codex couldn't complete this request. Try again later. |
|
@codex fix failing CI testing |
Summary
Testing
|
|
@copilot resolve the merge conflicts on this branch. |
|
@codex fix all issues, comments and failing CI testing |
…history-alignment # Conflicts: # supabase/drift-manifest.json
Head branch was pushed to by a user without write access
Done — the only conflict was in |
All three actionable concurrency/reset findings were fixed in 9ed693e, mirrored in schema.sql, covered by focused tests, and the verification claims were rewritten as explicit command/result pairs.
|
Resolved the requested changes in |
Summary
Testing
|
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@docs/forward-codify-retrieval-rpcs-workorder.md`:
- Around line 3-6: Update the execution procedure in Steps 1–7 to clearly
indicate it is historical and already completed, or replace those instructions
with the recorded migration completion evidence. Remove stale imperative
guidance telling operators to apply the migrations or remove allowlist entries,
while preserving the documented applied migration versions and validation
results.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 32e746a7-d1d0-44d4-aa73-ed00fb7b06ff
📒 Files selected for processing (6)
docs/branch-review-ledger.mddocs/forward-codify-retrieval-rpcs-workorder.mdsupabase/drift-manifest.jsonsupabase/migrations/20260713062139_route_enrichment_through_agent.sqlsupabase/schema.sqltests/supabase-schema.test.ts
| **Status: complete.** The production-current definitions were captured read-only and preserved under | ||
| their actual migration versions. Ran the scratch PostgreSQL replay: passed. Ran the reviewed live | ||
| migration apply: applied `20260713062107`, `20260713062125`, `20260713062132`, and `20260713062139` | ||
| on 2026-07-13. Ran `npm run check:drift`: passed. Ran `npm run check:production-readiness`: READY. |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Mark the execution procedure as completed or historical.
The status now says the migrations were applied on July 13, 2026, but the later procedure still tells operators to apply them and remove the allowlist entries. That stale imperative text can cause a future operator to repeat an already-completed live migration workflow. Mark Steps 1–7 as historical or replace them with the recorded completion evidence.
🧰 Tools
🪛 LanguageTool
[style] ~6-~6: Three successive sentences begin with the same word. Consider rewording the sentence or use a thesaurus to find a synonym.
Context: ...-13. Ran npm run check:drift: passed. Ran npm run check:production-readiness: R...
(ENGLISH_WORD_REPEAT_BEGINNING_RULE)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@docs/forward-codify-retrieval-rpcs-workorder.md` around lines 3 - 6, Update
the execution procedure in Steps 1–7 to clearly indicate it is historical and
already completed, or replace those instructions with the recorded migration
completion evidence. Remove stale imperative guidance telling operators to apply
the migrations or remove allowlist entries, while preserving the documented
applied migration versions and validation results.
Summary
service_roleaccess revoked from the legacy unfenced generation-commit overloadLive state
Applied migrations:
20260713062107_restore_text_fallback_lexical_score20260713062125_fence_index_generation_commit20260713062132_purge_expired_rag_response_cache20260713062139_route_enrichment_through_agentDirect live assertions previously confirmed zero duplicate open-ingestion groups, zero active ingestion/enrichment overlap, the fenced overload executable by
service_role, and the legacy overload inaccessible.Verification
npm test -- --run tests/supabase-schema.test.ts: 45/45 passed on the final review-fix commit.npm run drift:manifest: scratch PostgreSQL schema replay passed and regenerated the manifest.git diff --check: passed.npm run check:driftafter the recorded live apply: passed.npm run check:production-readiness: READY.Clinical governance
This PR aligns already-applied production migration history and hardens indexing concurrency. It does not change clinical answer policy or source-governance thresholds.
Context
PR #540 was squash-merged while post-apply verification was running. This follow-up contains the production-history alignment and the scoped enrichment concurrency fixes requested in review.