Skip to content

fix: preserve PDF fallback text and load live-test env#1082

Merged
BigSimmo merged 4 commits into
mainfrom
codex/reconcile-non-rag-p2
Jul 22, 2026
Merged

fix: preserve PDF fallback text and load live-test env#1082
BigSimmo merged 4 commits into
mainfrom
codex/reconcile-non-rag-p2

Conversation

@BigSimmo

@BigSimmo BigSimmo commented Jul 22, 2026

Copy link
Copy Markdown
Owner

Summary

  • Skip malformed image records returned by the JavaScript PDF fallback while preserving usable text and valid images.
  • Load Next environment files before the explicit live-provider permission check, so approved live tests can use repository-local credentials.
  • Keep the already-landed retryable-auth startup fix unchanged; that archived candidate was a duplicate.

RAG impact: no retrieval behaviour change — only malformed PDF image tolerance and live-test environment setup changed.

Verification

  • Red proof: npm exec vitest run tests/pdf-extraction-budget.test.ts tests/test-runner-safety.test.ts — 2 expected failures (undefined image data; missing Next env initialization).
  • npm exec vitest run tests/pdf-extraction-budget.test.ts tests/test-runner-safety.test.ts tests/private-client-auth.test.ts — 37 passed.
  • npm run verify:cheap — passed; 360 files, 3,203 passed / 1 skipped.
  • npm run verify:pr-local -- --files scripts/run-live-tests.mjs,src/lib/extractors/document.ts,tests/pdf-extraction-budget.test.ts,tests/test-runner-safety.test.ts — passed; format, lint, typecheck, full unit suite, production build, client-secret scan, offline RAG fixtures.
  • npm run check:production-readiness — local configuration gate reached; expected failure because this isolated worktree intentionally has no .env.local, Supabase service key, or OpenAI key.

Verification not run: live provider tests, Supabase operations, Railway operations, browser matrix, and live RAG evaluation were not needed for these two guards.

Risk and rollout

  • Risk: low; invalid fallback image records are ignored instead of aborting the document, while valid images retain the existing budget path. The live runner still requires ALLOW_PROVIDER_TESTS=true.
  • Rollback: revert commit 068f0197d97d119d32f37dcb08c9b74007bf1673.
  • Provider or production effects: None. No provider command was run and no deployment is included.

Clinical Governance Preflight

  • Source-backed claims still require linked source verification before clinical use
  • No patient-identifiable document workflow was introduced or expanded without explicit governance approval
  • Supabase target remains Clinical KB Database (sjrfecxgysukkwxsowpy)
  • Service-role keys and private document access remain server-only
  • Demo/synthetic content remains clearly separated from real clinical sources
  • Source metadata, review status, and outdated/unknown-source behavior remain conservative
  • Deployment classification/TGA SaMD impact was checked when clinical decision-support behavior changed

Notes

  • A successful JavaScript fallback may now retain text when one embedded-image record is unusable.
  • The public-upload moderation model, access control, retrieval, ranking, answer generation, publication, and database schema are unchanged.

Note

Low Risk
Ingestion tolerance and test-runner env ordering only; live tests still require explicit ALLOW_PROVIDER_TESTS=true and retrieval behavior is unchanged.

Overview
JavaScript PDF fallback no longer fails the whole extraction when pdf-parse returns bad image records (missing data, non-integer dimensions). Invalid entries are skipped via isUsableFallbackPdfImage; text and valid images still go through the existing budget path. Per-page image counts for needsOcr are taken from the parser’s image list (including skipped entries), so short text plus reported images still flags image-only pages.

Live provider test runner calls Next’s loadEnvConfig so repo-local env files can supply credentials, while ALLOW_PROVIDER_TESTS is captured before that load and passed into requireProviderTestPermission so permission cannot be granted only from a persisted env file.

Tests cover malformed fallback images and assert the snapshot → load env → permission-check ordering in run-live-tests.mjs.

Reviewed by Cursor Bugbot for commit f3380b0. Bugbot is set up for automated code reviews on this repo. Configure here.

Summary by CodeRabbit

  • Bug Fixes

    • Improved PDF JavaScript fallback extraction by skipping malformed fallback images (for example, missing image data) while preserving extracted text.
    • Ensured fallback extraction doesn’t count unusable images toward limits and now reports malformed-image pages via a new warning.
    • Live-test permissions now correctly incorporate the project’s environment configuration.
  • Tests

    • Added scenarios covering malformed fallback image data and image-fetch errors during PDF extraction.
    • Added verification that environment configuration is loaded before the live-test permission check.

@supabase

supabase Bot commented Jul 22, 2026

Copy link
Copy Markdown

This pull request has been ignored for the connected project sjrfecxgysukkwxsowpy because there are no changes detected in supabase directory. You can change this behaviour in Project Integrations Settings ↗︎.


Preview Branches by Supabase.
Learn more about Supabase Branching ↗︎.

@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The live-test runner loads Next.js environment configuration before permission checks. JavaScript PDF fallback extraction validates image entries, skips unusable images, preserves page text, and reports malformed-image pages.

Changes

Live-test runner environment

Layer / File(s) Summary
Load Next.js environment before permission checks
scripts/run-live-tests.mjs, tests/test-runner-safety.test.ts
The runner captures the permission variable, calls loadEnvConfig(projectRoot), passes the captured value to requireProviderTestPermission(), and tests this ordering.

PDF fallback image safety

Layer / File(s) Summary
Validate and skip malformed fallback images
src/lib/extractors/document.ts, tests/pdf-extraction-budget.test.ts
Fallback images must have non-empty data and finite positive integer dimensions; unusable entries are skipped before budget and artifact processing, warnings are emitted, page text is preserved, and both malformed-image cases are tested.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main changes: PDF fallback text preservation and live-test env loading.
Description check ✅ Passed The PR description follows the template with summary, verification, risk/rollout, governance, and notes filled in.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/reconcile-non-rag-p2

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
tests/pdf-extraction-budget.test.ts (1)

245-268: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Cover invalid dimensions in the fallback regression test.

This test verifies missing image data only. Add cases for zero, negative, non-finite, and fractional dimensions so every validation branch is protected and malformed entries remain skipped.

🤖 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 `@tests/pdf-extraction-budget.test.ts` around lines 245 - 268, Add
invalid-dimension cases to the “skips malformed image entries returned by the
JavaScript fallback” test by including image entries with zero, negative,
non-finite, and fractional width or height values alongside the missing-data
entry. Keep the extraction assertions verifying all malformed images are skipped
and valid page text remains preserved.
🤖 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 `@src/lib/extractors/document.ts`:
- Around line 59-73: Update isUsableFallbackPdfImage to require image.width and
image.height to be positive integers, matching extractedImageSchema; retain the
existing data presence and finite-value checks for otherwise valid fallback
images.

---

Nitpick comments:
In `@tests/pdf-extraction-budget.test.ts`:
- Around line 245-268: Add invalid-dimension cases to the “skips malformed image
entries returned by the JavaScript fallback” test by including image entries
with zero, negative, non-finite, and fractional width or height values alongside
the missing-data entry. Keep the extraction assertions verifying all malformed
images are skipped and valid page text remains preserved.
🪄 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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: f68dd447-0b5e-4769-b55c-933aa97bff5a

📥 Commits

Reviewing files that changed from the base of the PR and between a00638a and 068f019.

📒 Files selected for processing (4)
  • scripts/run-live-tests.mjs
  • src/lib/extractors/document.ts
  • tests/pdf-extraction-budget.test.ts
  • tests/test-runner-safety.test.ts

Comment thread src/lib/extractors/document.ts

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 068f0197d9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/lib/extractors/document.ts
@BigSimmo

Copy link
Copy Markdown
Owner Author

@codex resolve actionable Codex review findings for this pull request and current head using the repository instructions. This is the pull request's single automatic repair pass: do not perform a fresh review, create new standalone findings, or request another review. Work only the existing unresolved Codex threads on the current head. Always fix P0 and P1 findings. For P2 and lower findings, fix only clear, scoped, low-risk issues; otherwise disposition them with a concise reason. After fixing or dispositioning a thread, reply in that thread with as the first line, followed by a concise summary; that marker authorizes the workflow to close that exact thread. If human input or new authorization is required, do not use the marker and leave the thread open with the blocker. Finish only after every actionable thread is fixed or dispositioned and closed, or explicitly left open for a human decision. Do not update the branch from main, address unrelated reviews, broaden scope, or create more than one scoped fix commit. Do not use external APIs, paid services, credentials, dependency changes, or broad refactors unless explicitly authorized. Add targeted tests where behavior changes and run the narrowest relevant validation.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 068f0197d9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread scripts/run-live-tests.mjs
Comment thread src/lib/extractors/document.ts

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8f46a4c814

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/lib/extractors/document.ts

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 `@src/lib/extractors/document.ts`:
- Around line 337-339: Update the image-counting logic near the page.images loop
in the document extraction flow so imageCountByPage only includes entries that
pass isUsableFallbackPdfImage(image). Ensure pages containing only malformed or
unusable records are not marked needsOcr, while preserving counting and emission
for usable images.
🪄 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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 70d8dad7-692c-4e45-824b-b33871d463e1

📥 Commits

Reviewing files that changed from the base of the PR and between 068f019 and f3380b0.

📒 Files selected for processing (4)
  • scripts/run-live-tests.mjs
  • src/lib/extractors/document.ts
  • tests/pdf-extraction-budget.test.ts
  • tests/test-runner-safety.test.ts
🚧 Files skipped from review as they are similar to previous changes (3)
  • tests/pdf-extraction-budget.test.ts
  • tests/test-runner-safety.test.ts
  • scripts/run-live-tests.mjs

Comment thread src/lib/extractors/document.ts
@BigSimmo
BigSimmo merged commit d302be1 into main Jul 22, 2026
41 checks passed
@BigSimmo
BigSimmo deleted the codex/reconcile-non-rag-p2 branch July 22, 2026 12:44

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: eca5af958d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

const vitestBin = path.join(projectRoot, "node_modules", "vitest", "vitest.mjs");
const { loadEnvConfig } = nextEnv;
const providerTestPermission = process.env.ALLOW_PROVIDER_TESTS;
loadEnvConfig(projectRoot);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Set test mode before loading live-test env files

When ALLOW_PROVIDER_TESTS=true npm run test:live is run from a normal shell, this call happens before the script sets NODE_ENV: "test" for Vitest, so Next's env loader resolves the non-test mode and can load .env.production.local/.env.production values into lock.environment before passing them to the test child. In a checkout that keeps production Supabase/OpenAI credentials in those files, the explicit live-test approval can therefore run the provider suite against production-shaped configuration instead of the intended test configuration; set the intended mode before loadEnvConfig (or pass the appropriate dev/test loading mode) so the env files match the test process.

Useful? React with 👍 / 👎.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/lib/extractors/document.ts (1)

353-353: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Record filtered image entries in malformedImagePages.

When getImage returns an invalid image object, Line [353] skips it without marking the page. The malformed_fallback_images warning therefore omits this malformed-record case. Add malformedImagePages.add(page.pageNumber) before continuing.

Proposed fix
-            if (!isUsableFallbackPdfImage(image)) continue;
+            if (!isUsableFallbackPdfImage(image)) {
+              malformedImagePages.add(page.pageNumber);
+              continue;
+            }

Also applies to: 392-396

🤖 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 `@src/lib/extractors/document.ts` at line 353, Update the image filtering
branches in the document extraction flow, including the check around
isUsableFallbackPdfImage and the corresponding logic near the additional
referenced range, to add page.pageNumber to malformedImagePages before
continuing when getImage returns an invalid image object.
🤖 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.

Outside diff comments:
In `@src/lib/extractors/document.ts`:
- Line 353: Update the image filtering branches in the document extraction flow,
including the check around isUsableFallbackPdfImage and the corresponding logic
near the additional referenced range, to add page.pageNumber to
malformedImagePages before continuing when getImage returns an invalid image
object.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: f9a58417-412c-4721-b553-9958fbf72312

📥 Commits

Reviewing files that changed from the base of the PR and between f3380b0 and eca5af9.

📒 Files selected for processing (2)
  • src/lib/extractors/document.ts
  • tests/pdf-extraction-budget.test.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant