Skip to content

fix(backend): consolidate Firestore mutation invariants - #8362

Merged
Git-on-my-level merged 3 commits into
mainfrom
david/zach-firestore-invariants
Jun 25, 2026
Merged

Git-on-my-level merged 3 commits into
mainfrom
david/zach-firestore-invariants

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Jun 25, 2026 •

Copy link
Copy Markdown
Member

Summary

This is an aggregated, systematic replacement for ZachL111's Firestore/data-mutation hardening PRs. Rather than merging a set of one-off patches that silently skip missing records, this PR adds explicit invariants and shared helpers:

  • shared deterministic document-id helpers for system folders and calendar meetings
  • idempotent calendar meeting create/upsert that preserves the original created_at
  • transactional speech-sample removal so speech_samples and speech_sample_transcripts stay aligned
  • explicit BatchMutationResult reporting for action-item batch updates/sync (updated_ids, missing_ids, noop_ids)
  • downstream vector updates now run only for successfully updated sync items
  • folder delete/reorder validates targets before mutating instead of silently skipping unknown IDs

Supersedes #8294, #8297, #8298, #8301, #8307, #8316, #8322.

Original issue discovery, reproductions, and candidate fixes by @ZachL111 — thank you for surfacing these reliability problems. This PR rewrites them into a smaller shared-contract shape to reduce long-term maintenance cost.

Test plan

  • uv run --with pytest --with pydantic python -m pytest tests/unit/test_firestore_invariant_helpers.py -q
  • python3 -m py_compile backend/database/document_ids.py backend/database/_client.py backend/database/folders.py backend/database/calendar_meetings.py backend/database/users.py backend/database/action_items.py backend/routers/action_items.py backend/routers/folders.py backend/models/folder.py backend/tests/unit/test_firestore_invariant_helpers.py

Notes / intentional differences from source PRs

Review in cubic

- add shared deterministic document ID helpers for system folders and calendar meetings
- make calendar meeting creates idempotent while preserving created_at
- remove speech samples transactionally to keep samples/transcripts aligned
- report missing action-item IDs explicitly and only update downstream vectors for successful sync writes
- validate folder deletion/reorder targets before mutating

Supersedes #8294, #8297, #8298, #8301, #8307, #8316, #8322.
Original issue discovery, reproductions, and candidate fixes by @ZachL111.

Co-authored-by: Zach <[email protected]>
Keep the legacy mobile happy-path response shape for action-item batch endpoints.
Only include detailed batch mutation fields when there is a partial outcome such
as missing, no-op, or locked IDs.

The hermetic backend e2e harness caught this compatibility regression.

Co-authored-by: Zach <[email protected]>
@Git-on-my-level

Copy link
Copy Markdown
Member Author

Follow-up on the hermetic e2e failure: the harness was right. It caught that the PR changed the mobile happy-path response body for PATCH /v1/action-items/batch from {status, updated_count} to include extra batch detail fields. Even though current Flutter code only checks statusCode, the e2e contract intentionally pins the compatibility shape.\n\nI pushed 59454b3 to preserve the legacy success response and only include detailed fields when there is an actual partial outcome (missing/no-op/locked IDs).\n\nLocal validation after the fix:\n- bash backend/testing/e2e/run.sh -q --tb=short → 73 passed, 6 skipped\n- uv run python -m pytest tests/unit/test_firestore_invariant_helpers.py -q → 3 passed

@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: 5b6d7fbfcb

ℹ️ 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 backend/routers/action_items.py Resolved

@cubic-dev-ai cubic-dev-ai 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.

1 issue found and verified against the latest diff

Confidence score: 3/5

  • backend/database/action_items.py still has a TOCTOU race: rows can disappear after get().exists but before batch.commit(), which can cause the entire mutation to fail and break the explicit partial-result behavior for callers. Before merging, move existence handling to commit-time (or catch commit-time missing-row failures) so deletes return partial results instead of aborting the whole request.

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread backend/database/action_items.py Resolved

@Git-on-my-level Git-on-my-level left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Addressed both unresolved review threads.

Changes:

  • Updated TestSyncBatchSkipsLocked so the mocked batch_sync_update_action_items returns the new BatchMutationResult-shaped contract used by _batch_mutation_response.
  • Removed pre-read existence checks from action-item partial update helpers. Each Firestore document update is now applied independently and catches NotFound, so a concurrent delete is reported in missing_ids instead of failing the whole mutation after a stale existence read.

Verification:

  • ENCRYPTION_SECRET=*** VIRTUAL_ENV= uv run pytest tests/unit/test_task_sharing.py::TestSyncBatchSkipsLocked::test_sync_batch_skips_locked_updates -q → 1 passed
  • ENCRYPTION_SECRET=*** VIRTUAL_ENV= uv run pytest tests/unit/test_task_sharing.py -q → 29 passed
  • python3 -m py_compile database/action_items.py tests/unit/test_task_sharing.py → 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.

2 participants