Skip to content

fix(import): ingest decimal-bearing ChatGPT exports - #1871

Merged
Sinity merged 2 commits into
masterfrom
feature/fix/chatgpt-export-import-readiness
Jun 14, 2026
Merged

Sinity merged 2 commits into
masterfrom
feature/fix/chatgpt-export-import-readiness

Conversation

@Sinity

@Sinity Sinity commented Jun 14, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fixes ChatGPT account-export ZIP ingestion when streamed JSON records contain Decimal values from ijson, and fixes the FTS readiness invariant that made a correctly rebuilt search index look stale.

Closes #1870

Problem

The dogfood archive had ChatGPT export ZIPs available but no chatgpt-export sessions in the archive. The daemon had excluded staged inbox copies as unknown-export after repeated failures. Proper investigation showed the 2026 export was not HTML-only: it contains conversations-000.json through conversations-024.json with 2,403 session-bearing ChatGPT records.

The root cause was at the ZIP acquisition boundary. ijson emits numeric values as Decimal; acquisition rejected those records as non-JSONValue, replaced them with {}, and provider detection could only classify them as unknown. A related operational failure surfaced after import: exact FTS readiness counted blocks.text, while the FTS writer and triggers index blocks.search_text.

Solution

  • Add shared JSON Decimal normalization in polylogue.core.json.
  • Normalize Decimal values before ZIP acquisition/provider detection.
  • Normalize Decimal values before direct parser dispatch record lowering.
  • Align exact messages_fts readiness with the writer/trigger predicate, blocks.search_text != ''.
  • Add focused regressions for Decimal-bearing ChatGPT records and direct ChatGPT dispatch.

Live repair/import evidence from /realm/db/polylogue after applying the fix locally:

  • chatgpt-export: 2,491 indexed sessions.
  • 2,424 unique normal conversation sessions from conversations.json / conversations-*.json.
  • 67 real shared_conversations.json sessions.
  • Removed 4 bogus projections from message_feedback.json / user.json created by the earlier broad live-batch attempt.
  • Exact FTS check from the patched checkout: messages_ready=True, 3,633,598/3,633,598, missing=0, excess=0, duplicate=0.

Verification

  • devtools test tests/unit/sources/test_dispatch_payloads.py tests/unit/sources/test_source_laws.py -k 'streaming_decimals or decimal_bearing_chatgpt or locks_provider_after_first_detected_payload'
    • 4 passed
  • devtools test tests/unit/daemon/test_fts_readiness_fallback.py -k 'exact_coverage_counts_tool_blocks_as_indexable or falls_back_to_exact'
    • 2 passed
  • devtools verify --quick
    • all quick steps passed locally
  • git push pre-push quick gate
    • all quick steps passed again on 8c013f249980208471969acdc568f7da378dec6c

Operational note

The installed polylogued.service still uses the previous Nix-store build until this change is merged/deployed/rebuilt. The active archive data is repaired/imported; readiness from the old service can still report the pre-fix false fts_not_fresh result.

Problem: ChatGPT account-export ZIP members decoded through ijson carry Decimal values for numeric fields. The acquisition JSON contract rejected those records, replaced them with empty objects, classified the ZIP as unknown-export, and left the daemon retrying/excluding valid ChatGPT exports. The same session also exposed an FTS readiness false negative: exact readiness counted blocks.text while the FTS writer indexes blocks.search_text.\n\nWhat changed: normalize Decimal values before acquisition/provider detection and before direct parser dispatch record lowering, so ChatGPT conversation records keep their mapping/title/timestamps. Align the exact messages_fts invariant with the writer/trigger predicate by counting blocks.search_text. Add focused regressions for Decimal-bearing ChatGPT records.\n\nVerification: devtools test tests/unit/sources/test_dispatch_payloads.py tests/unit/sources/test_source_laws.py -k 'streaming_decimals or decimal_bearing_chatgpt or locks_provider_after_first_detected_payload' passed. devtools test tests/unit/daemon/test_fts_readiness_fallback.py -k 'exact_coverage_counts_tool_blocks_as_indexable or falls_back_to_exact' passed. Local exact readiness against /realm/db/polylogue/index.db reports messages_ready=True with 3,632,991/3,632,991 rows and 0 missing/excess/duplicate rows.\n\nRef #1870
@coderabbitai

coderabbitai Bot commented Jun 14, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Two independent fixes: source_acquisition_components.py and dispatch.py gain a _normalize_json_decimal helper that recursively converts Decimal to int/float before JSON-type validation; fts_lifecycle.py updates the messages_fts invariant SQL to use blocks.search_text != '' instead of NULLIF(text, '') IS NOT NULL for indexability. Regression tests cover both.

Changes

Decimal Normalization in Payload Handling

Layer / File(s) Summary
Decimal normalization implementation
polylogue/sources/source_acquisition_components.py, polylogue/sources/dispatch.py
_normalize_json_decimal recursively converts Decimal to int (integral) or float (fractional) for lists and dicts; _artifact_payload and _payload_record both call it before JSON-type validation so Decimal-bearing payloads are accepted.
Regression tests
tests/unit/sources/test_dispatch_payloads.py, tests/unit/sources/test_source_laws.py
test_payload_record_normalizes_streaming_decimals_for_chatgpt_parse verifies create_time converts to float and parse_payload returns the correct session; test_iter_entry_payloads_preserves_decimal_bearing_chatgpt_records verifies fractional and integral Decimal timestamps survive round-trip serialization.

FTS Invariant SQL Fix

Layer / File(s) Summary
messages_fts indexability condition
polylogue/storage/fts/fts_lifecycle.py
missing_sql and excess_sql in fts_invariant_snapshot_sync now use b.search_text != '' instead of NULLIF(b.text, '') IS NOT NULL, aligning the invariant with FTS trigger population criteria.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Poem

🐇 A decimal drifted, all fractional and free,
But the payload said "JSON? That cannot be!"
So I normalized smartly, converting with care,
Float or int — either fits, I declare.
The FTS search_text now matches the trigger's decree,
All tidy and tested — as a rabbit should see! 🌿

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 9.09% 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 accurately summarizes the main change: fixing ingestion of decimal-bearing ChatGPT exports by normalizing Decimal values.
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.
Description check ✅ Passed The pull request description follows the template structure with all required sections present: Summary, Problem, Solution, Verification, and includes an Operational note for deployment context.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 feature/fix/chatgpt-export-import-readiness

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

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

@coderabbitai coderabbitai 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.

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 `@polylogue/sources/source_acquisition_components.py`:
- Around line 133-140: Extract the `_normalize_json_decimal` function to
eliminate duplication across files. Create a shared module at
polylogue/core/json.py (or polylogue/core/normalization.py) and move the
function implementation there as a public function. In
polylogue/sources/source_acquisition_components.py (lines 133-140), delete the
local implementation and import the shared function from the new module,
ensuring it is called from `_artifact_payload`. In polylogue/sources/dispatch.py
(lines 74-81), delete the duplicate implementation and import the shared
function from the new module, ensuring it is called from `_payload_record` and
`_payload_sequence`.
🪄 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: ASSERTIVE

Plan: Pro

Run ID: bc7e4e73-321e-4ed0-9a26-183edceb4e97

📥 Commits

Reviewing files that changed from the base of the PR and between a7d0cc8 and 39593fa.

📒 Files selected for processing (5)
  • polylogue/sources/dispatch.py
  • polylogue/sources/source_acquisition_components.py
  • polylogue/storage/fts/fts_lifecycle.py
  • tests/unit/sources/test_dispatch_payloads.py
  • tests/unit/sources/test_source_laws.py

Problem: the ChatGPT import fix needed Decimal normalization in both ZIP acquisition and provider dispatch, and the initial implementation duplicated the helper across those boundaries.

What changed: move the recursive Decimal-to-JSON-number lowering into polylogue.core.json and import it from both callers so Decimal-bearing streamed payloads keep one JSON contract.

Verification:
- devtools test tests/unit/sources/test_dispatch_payloads.py tests/unit/sources/test_source_laws.py -k 'streaming_decimals or decimal_bearing_chatgpt or locks_provider_after_first_detected_payload'
- devtools test tests/unit/daemon/test_fts_readiness_fallback.py -k 'exact_coverage_counts_tool_blocks_as_indexable or falls_back_to_exact'
- devtools verify --quick
@Sinity
Sinity merged commit a14c05d into master Jun 14, 2026
12 of 15 checks passed
@Sinity
Sinity deleted the feature/fix/chatgpt-export-import-readiness branch June 14, 2026 01:13
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.

fix(import): ingest ChatGPT export ZIPs or reject unsupported shapes

1 participant