Skip to content

Add migration script to unlock conversations locked by sync soft cap bug - #5901

Merged
beastoin merged 5 commits into
mainfrom
fix/unlock-soft-cap-locked-5899
Mar 22, 2026
Merged

beastoin merged 5 commits into
mainfrom
fix/unlock-soft-cap-locked-5899

Conversation

@beastoin

@beastoin beastoin commented Mar 22, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Migration script to unlock conversations, memories, and action items incorrectly locked by the sync soft cap bug (PR #5878, fixed in PR #5896).

Script at backend/scripts/unlock_soft_cap_locked.py — reads UIDs from a file, verifies paid subscription (unlimited/pro), then unlocks all 3 collections. Dry-run by default.

Usage (Cloud Run job)

python scripts/unlock_soft_cap_locked.py --uids-file /tmp/impacted_uids.txt          # dry-run
python scripts/unlock_soft_cap_locked.py --uids-file /tmp/impacted_uids.txt --execute # apply

Impact

  • 8 users identified from prod logs, 6 are paid (unlimited), 2 basic (skipped)
  • 692 total items to unlock (649 conversations, 17 memories, 26 action items)

Closes #5899

Reads UIDs from a file, verifies paid subscription (unlimited/pro only),
then unlocks conversations, memories, and action items. Supports dry-run.
No UIDs hardcoded. Related: #5899, PR #5896.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@greptile-apps

greptile-apps Bot commented Mar 22, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds a one-off migration script (backend/scripts/unlock_soft_cap_locked.py) to remediate the data impact of a bug introduced in PR #5878, where the sync endpoint incorrectly applied should_lock=True to conversations for paid users who exceeded the 2-hour daily soft cap. The script reads impacted UIDs from a file, verifies each user is on an unlimited or pro plan, and batch-unlocks is_locked=True documents across three Firestore subcollections (conversations, memories, action_items).

  • Dry-run is the default mode; --execute must be passed explicitly to write changes, which is a good safety practice for a migration script.
  • Batch writes are correctly capped at 499 operations (under the Firestore 500-write limit), with proper flush-and-reset logic.
  • The plan guard (PAID_PLANS = {'unlimited', 'pro'}) correctly matches the PlanType enum string values stored in Firestore.
  • The script mirrors the unlock pattern already used by the payment upgrade flow in payment.py:484-486 (unlock_all_conversations, unlock_all_memories, unlock_all_action_items).
  • Minor: get_subscription_plan reads only the raw plan field and does not validate status or current_period_end, so an expired paid subscription would still pass the guard. Given the 8-user scope and short window between bug introduction and fix, this is unlikely to matter in practice, but worth a deliberate decision.

Confidence Score: 4/5

  • Safe to merge after confirming the dry-run output on prod looks correct; the script is narrow in scope with good guards and a safe default mode.
  • The script is well-structured with dry-run by default, correct Firestore batch sizing, and a paid-plan safety gate. The only non-trivial concern is that the subscription validity check doesn't verify active status or expiry period, but given the 8-user scope and the short time between the bug being introduced (PR Sync: fair-use gates with lock-on-exhaustion (v2, clean) #5878) and fixed (PR Fix: sync soft cap trigger incorrectly locking conversations #5896), a lapsed subscription overlap is highly unlikely. The logic is otherwise correct and mirrors the existing unlock pattern in payment.py.
  • No files require special attention beyond confirming the dry-run output against prod before executing.

Important Files Changed

Filename Overview
backend/scripts/unlock_soft_cap_locked.py New one-off migration script that reads UIDs from a file, checks each user's current paid plan in Firestore, and batch-unlocks is_locked=True documents in conversations/memories/action_items subcollections. Includes dry-run mode by default. Minor concern: subscription validity (status/expiry) is not verified, only the plan name — could unlock for lapsed paid subscriptions.

Sequence Diagram

sequenceDiagram
    participant Operator
    participant Script as unlock_soft_cap_locked.py
    participant Firestore

    Operator->>Script: python script.py --uids-file /tmp/uids.txt [--execute]
    Script->>Script: read_uids(filepath) → [uid1, uid2, ...]

    loop for each UID
        Script->>Firestore: users/{uid}.get(['subscription'])
        Firestore-->>Script: {plan: 'unlimited'|'pro'|'basic'|null}

        alt plan not in PAID_PLANS
            Script->>Script: SKIP (log warning)
        else plan in PAID_PLANS
            Script->>Firestore: count_locked(uid, 'conversations')
            Script->>Firestore: count_locked(uid, 'memories')
            Script->>Firestore: count_locked(uid, 'action_items')
            Firestore-->>Script: counts per collection

            alt dry-run (default)
                Script->>Script: log counts (no writes)
            else --execute
                Script->>Firestore: batch.update is_locked=False (conversations)
                Script->>Firestore: batch.update is_locked=False (memories)
                Script->>Firestore: batch.update is_locked=False (action_items)
                Firestore-->>Script: commits acknowledged
            end
        end
    end

    Script->>Operator: Summary log (total processed / skipped / unlocked)
Loading

Reviews (1): Last reviewed commit: "Add migration script to unlock conversat..." | Re-trigger Greptile

Comment on lines +46 to +53
def get_subscription_plan(db, uid: str) -> str | None:
"""Read the user's subscription plan from Firestore."""
doc = db.collection('users').document(uid).get(['subscription'])
if not doc.exists:
return None
data = doc.to_dict()
sub = data.get('subscription', {})
return sub.get('plan')

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.

P2 Subscription validity (status/expiry) not checked

get_subscription_plan reads only the raw plan field from Firestore. It does not check status or current_period_end, so a user whose paid subscription has since expired (or is inactive) would still pass the PAID_PLANS gate and have their conversations unlocked.

The backend already has get_user_valid_subscription in database/users.py that enforces this: for paid plans it verifies current_period_end >= now() before returning a valid subscription.

This is likely acceptable given the narrow 8-user scope and tight timeline (bug was introduced and fixed within a short window), but it's worth a conscious decision either way. If any affected user's subscription lapsed between the bug being introduced (PR #5878) and this fix running, this script would still unlock their data. Conversely, if you believe the lock was wrong regardless of current subscription status, the current approach is intentionally permissive.

If you want to add the validity guard, get_subscription_plan could additionally check sub.get('status') == 'active' and validate current_period_end against the current timestamp for paid plans.

Comment on lines +77 to +78
if count % 499 != 0:
batch.commit()

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.

P2 Empty batch committed when count is 0

When count == 0 (no locked documents found), 0 % 499 == 0 is True, so the in-loop commit never fires — but if count % 499 != 0 is also False, meaning the trailing batch.commit() is correctly skipped. This is actually fine.

However, if count is exactly a multiple of 499 (e.g., 499, 998, …), the loop commits the last full batch and resets batch = db.batch(). After the loop, count % 499 == 0 again, so the guard correctly avoids committing the newly-created empty batch. The logic is sound, though it could be made more explicit by tracking whether the trailing batch is actually non-empty:

if count > 0 and count % 499 != 0:
    batch.commit()

This makes the intent clearer to readers and eliminates any confusion about the count == 0 case.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

beastoin and others added 4 commits March 22, 2026 08:43
POST /v1/admin/fair-use/user/{uid}/unlock — verifies paid subscription
(unlimited/pro) then unlocks conversations, memories, and action items.
Uses existing unlock functions from payment flow. Related: #5899.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Unlock is not a fair-use operation — it's a general admin remediation
for the sync soft cap bug. Moving to users.py instead.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
General admin endpoint to unlock conversations, memories, and action
items for paid users. Verifies subscription before unlocking. Used to
remediate the sync soft cap locking bug. Related: #5899.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Manager prefers Cloud Run job pattern for one-off migrations over
permanent admin endpoints. Script at scripts/unlock_soft_cap_locked.py
will be run as a Cloud Run job instead.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@beastoin
beastoin merged commit b932827 into main Mar 22, 2026
2 checks passed
@beastoin
beastoin deleted the fix/unlock-soft-cap-locked-5899 branch March 22, 2026 08:52
@beastoin

Copy link
Copy Markdown
Collaborator Author

lgtm

@beastoin

Copy link
Copy Markdown
Collaborator Author

Migration Complete

Cloud Run job migration-unlock-soft-cap executed successfully in us-central1.

Results

  • 6 paid users (unlimited) unlocked: 649 conversations, 17 memories, 26 action items — 692 total items
  • 2 basic users correctly skipped (script verified subscription before unlocking)
  • All counts matched the dry-run exactly

Timeline

No further action needed. All impacted paid users are fully restored.

by AI for @beastoin

Glucksberg pushed a commit to Glucksberg/omi-local that referenced this pull request Apr 28, 2026
…bug (BasedHardware#5901)

## Summary
Migration script to unlock conversations, memories, and action items
incorrectly locked by the sync soft cap bug (PR BasedHardware#5878, fixed in PR
BasedHardware#5896).

Script at `backend/scripts/unlock_soft_cap_locked.py` — reads UIDs from
a file, verifies paid subscription (unlimited/pro), then unlocks all 3
collections. Dry-run by default.

## Usage (Cloud Run job)
```bash
python scripts/unlock_soft_cap_locked.py --uids-file /tmp/impacted_uids.txt          # dry-run
python scripts/unlock_soft_cap_locked.py --uids-file /tmp/impacted_uids.txt --execute # apply
```

## Impact
- 8 users identified from prod logs, 6 are paid (unlimited), 2 basic
(skipped)
- 692 total items to unlock (649 conversations, 17 memories, 26 action
items)

Closes BasedHardware#5899
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.

Unlock conversations incorrectly locked by sync soft cap trigger

1 participant