fix(usage): normalize live ingest account ids - #1731
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a8a6c12a0e
ℹ️ 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".
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ab8d752215
ℹ️ 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".
ede2af1 to
3afaa02
Compare
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3afaa02365
ℹ️ 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".
3afaa02 to
6506670
Compare
6506670 to
ffbdde5
Compare
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ffbdde5a27
ℹ️ 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".
e0e8d08 to
f2ccfde
Compare
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f2ccfde440
ℹ️ 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".
| result = await to_thread.run_sync(lambda: run_upgrade(db_url, "head", bootstrap_legacy=False)) | ||
| assert result.current_revision == revision | ||
| assert result.current_revision == head_revision |
There was a problem hiding this comment.
Add round-trip coverage for the identity index
This change only advances the expected Alembic head while continuing to inspect telemetry columns, so the test passes even if the new account identity index is never created or its downgrade leaves it behind. Add upgrade and downgrade assertions for idx_accounts_chatgpt_account_id, including the PostgreSQL concurrent path where supported, to satisfy the repository's migration coverage gate.
AGENTS.md reference: AGENTS.md:L116-L120
Useful? React with 👍 / 👎.
| with op.get_context().autocommit_block(): | ||
| op.execute(sa.text(f"CREATE INDEX CONCURRENTLY IF NOT EXISTS {_INDEX} ON {_TABLE} (chatgpt_account_id)")) |
There was a problem hiding this comment.
Rebuild invalid indexes before concurrent creation
If PostgreSQL interrupts CREATE INDEX CONCURRENTLY, it can leave a same-named index with pg_index.indisvalid = false; on the next upgrade, IF NOT EXISTS accepts that unusable index and Alembic stamps the revision as complete, leaving every ChatGPT identity lookup without the intended index. The repository's other concurrent-index migrations explicitly detect and drop invalid leftovers before retrying, and this migration should do the same.
AGENTS.md reference: AGENTS.md:L116-L120
Useful? React with 👍 / 👎.
| async with get_background_session() as session: | ||
| rows = ( | ||
| (await session.execute(select(Account.id).where(Account.chatgpt_account_id == chatgpt_account_id))) | ||
| ( | ||
| await session.execute( | ||
| select(Account.id).where(Account.chatgpt_account_id == chatgpt_account_id).limit(2) | ||
| ) |
There was a problem hiding this comment.
Hold identity ownership stable through persistence
When a credential refresh moves a ChatGPT identity after this query completes but before _ingest opens its separate write session, the snapshot is still inserted for the former owner, despite the delta spec requiring attribution to the row that currently owns the identity. Resolve and persist within one transaction while locking the matched account row, or revalidate ownership immediately before the insert, so concurrent identity reassignment cannot contaminate another account's routing inputs.
AGENTS.md reference: AGENTS.md:L24-L26
Useful? React with 👍 / 👎.
| - Code: live usage ingestor identity resolution and coalescing. | ||
| - Tests: integration coverage for hub-published snapshots, unproven suffix | ||
| drops, and resolved-alias coalescing. | ||
| - Configuration, schema, response shapes, and background polling are unchanged. |
There was a problem hiding this comment.
Declare the database index in the change impact
The proposal says schema is unchanged, but this commit adds both an Alembic revision and idx_accounts_chatgpt_account_id to the SQLAlchemy schema. Because OpenSpec is the repository's schema-change SSOT and readiness gate, this understates the deployment impact and can cause reviewers or operators to overlook the required migration; list the new index and migration under Impact instead.
AGENTS.md reference: AGENTS.md:L92-L98
Useful? React with 👍 / 👎.
|
Folded into #1732 (live-ingest account-id normalization joins the hard-continuity owner-selection carrier); history preserved via merge. |
Summary
Validation
Live evidence: last-day codex-lb logs showed sqlite3.IntegrityError FOREIGN KEY constraint failed from app/modules/usage/live_ingest.py -> app/modules/usage/repository.py while inserting usage_history.account_id values that can differ from stored suffixed workspace Account.id rows.