Skip to content

fix(retain): preserve document created_at across upsert + UI edit flow#1194

Merged
nicoloboschi merged 1 commit into
mainfrom
fix/doc-preserve-created-at
Apr 22, 2026
Merged

fix(retain): preserve document created_at across upsert + UI edit flow#1194
nicoloboschi merged 1 commit into
mainfrom
fix/doc-preserve-created-at

Conversation

@nicoloboschi

Copy link
Copy Markdown
Collaborator

Summary

  • Fix: re-ingesting a document via retain with the same document_id used to reset created_at to NOW() because the explicit DELETE on the documents row ran before the INSERT ... ON CONFLICT DO UPDATE — so the ON CONFLICT branch was never reached. Now we RETURNING created_at on the DELETE and pass it through to the INSERT (COALESCE($7, NOW())), preserving the original creation timestamp. updated_at still advances on every write.
  • UI: file upload defaults document_id to the file name instead of letting the server generate a UUID.
  • UI: documents table gains an Updated column alongside Created.
  • UI: document detail panel supports editing original_text. Save calls retain with the same document_id plus the original context, event_date, metadata, and tags (preserved from retain_params), triggering the upsert path — which now correctly keeps created_at.

Test plan

  • uv run pytest tests/test_retain.py::test_document_upsert_preserves_created_at — new regression test (passes)
  • uv run pytest tests/test_retain.py::test_document_upsert_behavior tests/test_delta_retain_duplicates.py — existing upsert + concurrent-upsert suite (5 passed)
  • ./scripts/hooks/lint.sh — green
  • Manual: upload a file via the Add Document → Upload tab; confirm document_id defaults to the file name
  • Manual: open a document, edit its content, save; confirm updated_at advances while created_at stays stable, and tags/metadata/context are preserved

Re-ingesting a document via retain with the same document_id deletes and
reinserts the documents row, which reset created_at to NOW(). The
ON CONFLICT DO UPDATE branch preserved it, but was never reached because
the explicit DELETE removed the row first.

- Capture created_at via RETURNING on the DELETE and pass it through to
  _upsert_document_row, which now uses COALESCE($7, NOW()) on INSERT.
- updated_at continues to advance on every insert/update.

Control plane:
- File upload defaults document_id to the file name so uploads keep a
  meaningful identifier instead of a server-generated UUID.
- Documents table shows an "Updated" column alongside "Created".
- Document detail panel supports editing original_text; Save calls retain
  with the same document_id and preserves the original context, event
  date, metadata, and tags, triggering the upsert path.

Regression test added for created_at preservation.
@nicoloboschi
nicoloboschi merged commit 1078566 into main Apr 22, 2026
53 of 54 checks passed
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