Skip to content

fix(accounts): key imported credentials by workspace - #865

Merged
Soju06 merged 48 commits into
Soju06:mainfrom
Komzpa:fix/account-workspace-identity
Jun 2, 2026
Merged

Soju06 merged 48 commits into
Soju06:mainfrom
Komzpa:fix/account-workspace-identity

Conversation

@Komzpa

@Komzpa Komzpa commented May 31, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • keys imported ChatGPT credentials by workspace when the upstream token exposes a workspace identity, so one email can hold separate personal/team/workspace credential slots
  • preserves workspace label and seat type as account-slot metadata across import, OAuth, token refresh, account summaries, and usage refresh guards
  • reconciles OAuth reauth by upstream ChatGPT identity plus workspace slot, without letting a workspace-less reauth clear a known workspace account
  • keeps distinct upstream identities under the same email as separate account slots and surfaces duplicate-email account pairs without treating placeholder emails as real duplicates
  • unifies account auth export UI/copy around credential slots instead of a single email-account assumption
  • adds the per-account force-probe admin endpoint and refresh path for targeted upstream usage/status recovery
  • skips usage/rate-limit writes when a legacy account receives a usage payload for a workspace slot already owned by another account

Folded PRs

Validation

  • uv run pytest tests/unit/test_accounts_repository_locks.py tests/integration/test_repositories.py::test_accounts_upsert_merge_by_chatgpt_identity_does_not_clear_workspace_on_workspace_less_reauth tests/integration/test_repositories.py::test_accounts_upsert_merge_by_chatgpt_identity_workspace_less_reauth_uses_unknown_row tests/integration/test_repositories.py::test_accounts_upsert_merge_by_chatgpt_identity_prefers_matching_workspace_row -q
  • uv run pytest tests/unit/test_accounts_service_probe.py tests/integration/test_accounts_api_probe.py -q
  • uv run ruff check app/modules/accounts/repository.py tests/integration/test_repositories.py tests/unit/test_accounts_repository_locks.py
  • uv run ruff format --check app/modules/accounts/repository.py tests/integration/test_repositories.py tests/unit/test_accounts_repository_locks.py
  • uv run ty check app/modules/accounts tests/integration/test_repositories.py tests/unit/test_accounts_repository_locks.py

Fixes #355

@github-actions github-actions Bot added the db migration PR changes Alembic database migrations; maintainer must coordinate merge order label May 31, 2026

@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: a81c814b7b

ℹ️ 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 app/modules/accounts/repository.py Resolved
@Komzpa
Komzpa force-pushed the fix/account-workspace-identity branch from a81c814 to 4256aa6 Compare May 31, 2026 17:24
@Soju06 Soju06 added the 🤖 codex: needs work [@codex review] raised an issue label May 31, 2026
@Komzpa
Komzpa force-pushed the fix/account-workspace-identity branch from 4256aa6 to 21dcebf Compare May 31, 2026 17:26
@Soju06 Soju06 removed the 🤖 codex: needs work [@codex review] raised an issue label May 31, 2026
@Komzpa
Komzpa force-pushed the fix/account-workspace-identity branch from 21dcebf to c03317b Compare May 31, 2026 17:34
@Komzpa

Komzpa commented May 31, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@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: c03317ba4b

ℹ️ 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 app/modules/accounts/repository.py Resolved
@Komzpa Komzpa added the 🤖 codex: needs work [@codex review] raised an issue label May 31, 2026
@Komzpa
Komzpa force-pushed the fix/account-workspace-identity branch from c03317b to 4acd470 Compare May 31, 2026 17:51
@Soju06 Soju06 removed the 🤖 codex: needs work [@codex review] raised an issue label May 31, 2026
@Komzpa Komzpa removed the 🤖 codex: needs work [@codex review] raised an issue label May 31, 2026
@Komzpa

Komzpa commented May 31, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@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: 4acd470e95

ℹ️ 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 app/modules/accounts/repository.py Resolved
@Komzpa
Komzpa force-pushed the fix/account-workspace-identity branch from 4acd470 to 7760f45 Compare May 31, 2026 18:14
@Komzpa

Komzpa commented May 31, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@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: 7760f45c23

ℹ️ 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 app/modules/accounts/repository.py Resolved
@Komzpa
Komzpa force-pushed the fix/account-workspace-identity branch from 7760f45 to 847d926 Compare May 31, 2026 18:30
@Komzpa

Komzpa commented May 31, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@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: 847d9263fc

ℹ️ 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 app/modules/usage/updater.py Resolved
@Komzpa
Komzpa force-pushed the fix/account-workspace-identity branch from 847d926 to af2cd5e Compare May 31, 2026 18:51
@Komzpa

Komzpa commented May 31, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@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: af2cd5ec4a

ℹ️ 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 app/modules/accounts/repository.py Resolved
@Komzpa
Komzpa force-pushed the fix/account-workspace-identity branch from af2cd5e to c47d550 Compare May 31, 2026 19:12
@Komzpa

Komzpa commented May 31, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

ℹ️ 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".

@Soju06 Soju06 added the 🤖 codex: ok [@codex review] says no issues found. label May 31, 2026
@Soju06

Soju06 commented Jun 1, 2026

Copy link
Copy Markdown
Owner

Hermes owner-review blocker for PR #865:

  1. AuthManager.refresh_account() can mutate an existing account row into a different workspace slot without re-keying or conflict checks. The new import/OAuth path keys credentials by (chatgpt_account_id, workspace_id) (app/modules/accounts/service.py, app/modules/oauth/service.py, AccountsRepository.upsert_account_slot()), but refresh writes result.workspace_id / workspace_label / seat_type back to the same account.id via update_tokens() (app/modules/accounts/auth_manager.py:223-241, app/modules/accounts/repository.py:290-326). If a refresh response reports another workspace, the row keeps the old primary key/history but now claims the new workspace; if another row already exists for that workspace there is no DB uniqueness guard to stop duplicate active slots. Please either only backfill workspace metadata when currently null, reject/log/no-op on non-null workspace mismatch, or route the change through an explicit slot migration/upsert with conflict/history policy and a regression test.

  2. Merge-gate issue coverage is missing. The PR body/commit do not include Fixes #N / Closes #N, and GitHub reports no closing issue reference. Per this repo's merge gates, please add a valid linked issue/closure wording or have the maintainer explicitly waive that gate.

Non-blocking follow-up: the usage-refresh mismatch guard currently returns fetch_succeeded=False, so a deterministic workspace/plan mismatch may be retried every refresh pass instead of being cached/cooldowned as a non-transient identity mismatch (app/modules/usage/updater.py:347-361).

@Soju06 Soju06 added the hermes: needs-followup Hermes left a blocker/comment that needs follow-up observation label Jun 1, 2026
@Soju06

Soju06 commented Jun 2, 2026

Copy link
Copy Markdown
Owner

Hermes owner-review blocker for current head 031df675:

  1. Forced probe still does not satisfy the manual recovery contract for stuck rate_limited accounts. probe_account() decrypts and sends the stored access token before any freshness check (app/modules/accounts/service.py:384-390), then only calls UsageUpdater.force_refresh() afterward (service.py:392-394), so an expired token can burn the wake-up probe and only refresh tokens too late. Separately, force_refresh() delegates to _refresh_account() (app/modules/usage/updater.py:309-329), but status recovery returns unless the account is QUOTA_EXCEEDED (updater.py:590-598); the current probe test even asserts a RATE_LIMITED account remains rate_limited after probe (tests/unit/test_accounts_service_probe.py:99-122). Please ensure the probe uses/retries with a fresh token and that a forced post-probe usage snapshot with available primary quota persists/returns rate_limited -> active immediately.

  2. OAuth/identity reconciliation can mutate hot-path account/API-key routing state without invalidating caches. _persist_tokens() calls upsert(... merge_by_chatgpt_identity=True) or upsert_account_slot(...) without clearing account-selection/API-key caches (app/modules/oauth/service.py:557-571). The reconciliation path can repoint/delete API-key assignments and duplicate account rows (app/modules/accounts/repository.py:323-359). Please invalidate the account selection cache and API-key validation cache/poller after these successful mutations, like the account delete path already does.

  3. The unified auth export dialog bypasses privacy mode and has an unnamed format combobox. It renders the account email directly (frontend/src/features/accounts/components/auth-export-dialog.tsx:143-145) instead of using usePrivacyStore / privacy-blur, and its visible <Label>Format</Label> is not associated with the Radix trigger (auth-export-dialog.tsx:151-161). Please preserve privacy blur for visible account identity and give the selector a real accessible name, with regressions.

  4. The backend duplicate indicator is workspace-aware, but the dashboard ignores it and recomputes duplicates by email/display name only. Backend maps duplicates by (email, chatgpt_account_id, workspace_id) (app/modules/accounts/mappers.py:55-78), while account-list.tsx / accounts-page.tsx still call buildDuplicateAccountIdSet(accounts) (frontend/src/features/accounts/components/account-list.tsx:62, accounts-page.tsx:57-59), whose key is just normalized email/display name (frontend/src/utils/account-identifiers.ts:7-32). That can still flag legitimate same-email accounts in distinct workspaces/identities, contradicting the new API contract/tests. Please consume isEmailDuplicate or make the frontend key match the backend.

  5. The auth-export OpenSpec delta still describes top-level tokens fields as snake_case (openspec/changes/unify-auth-export/specs/unified-auth-export/spec.md:13-15), but the dashboard response model serializes them camelCase. Please align the spec with the actual wire contract or adjust serialization/tests accordingly.

CI/OpenSpec/Codex are currently green, but these are semantic/product blockers from the folded changes, so I would hold merge until addressed.

@Soju06 Soju06 added the hermes: needs-followup Hermes left a blocker/comment that needs follow-up observation label Jun 2, 2026
- refresh account tokens before force-probe requests and recover rate_limited accounts after fresh usage
- invalidate account/API-key routing caches after OAuth identity-slot persistence
- honor backend duplicate indicators across account/dashboard surfaces
- preserve privacy blur and accessible labels in the auth export dialog
- align unified auth export OpenSpec token field casing
@Soju06

Soju06 commented Jun 2, 2026

Copy link
Copy Markdown
Owner

@codex review

@Soju06 Soju06 removed the 🤖 codex: ok [@codex review] says no issues found. label Jun 2, 2026

@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: 8e1647b9ea

ℹ️ 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 app/modules/accounts/repository.py Resolved
@Soju06 Soju06 added the 🤖 codex: needs work [@codex review] raised an issue label Jun 2, 2026
Keep deterministic-id workspace rows attached to their learned workspace metadata when a later OAuth reauth payload lacks workspace claims. This updates the existing slot instead of creating an unknown-workspace copy and preserves workspace label/seat metadata while still refreshing credentials and plan/status fields.

Add an integration regression for the deterministic-id legacy row path flagged by Codex review.
@Soju06 Soju06 removed the 🤖 codex: needs work [@codex review] raised an issue label Jun 2, 2026
@Soju06

Soju06 commented Jun 2, 2026

Copy link
Copy Markdown
Owner

@codex review

Soju06 added 2 commits June 2, 2026 14:54
Merge the account workspace identity migration with the request-log failure metadata head from current main so PR Soju06#865 keeps Alembic at a single head after updating the branch.
@Soju06

Soju06 commented Jun 2, 2026

Copy link
Copy Markdown
Owner

@codex review

@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: 944341e4d1

ℹ️ 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 app/modules/accounts/repository.py Resolved
Comment thread app/modules/accounts/service.py Resolved
- Treat same-email/same-workspace rows as occupied only when the incoming credential has no ChatGPT identity or the existing row has no identity fallback
- Preserve distinct ChatGPT identities in the same workspace during workspace discovery
- Return a structured dashboard conflict when account probe token refresh fails instead of surfacing a 500
- Cover both Codex review regressions with integration tests
@Soju06

Soju06 commented Jun 2, 2026

Copy link
Copy Markdown
Owner

@codex review

@Soju06 Soju06 added the 🤖 codex: needs work [@codex review] raised an issue label Jun 2, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

ℹ️ 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".

@Soju06 Soju06 added 🤖 codex: ok [@codex review] says no issues found. and removed 🤖 codex: needs work [@codex review] raised an issue hermes: needs-followup Hermes left a blocker/comment that needs follow-up observation labels Jun 2, 2026
@Soju06
Soju06 merged commit 47b0c36 into Soju06:main Jun 2, 2026
23 checks passed
vitalNohj pushed a commit to vitalNohj/codex-lb that referenced this pull request Jul 17, 2026
Key imported ChatGPT credentials by workspace so one email can hold distinct personal, team, and workspace credential slots

Preserve workspace metadata across import, OAuth, token refresh, summaries, usage refresh, and account auth export flows

Add targeted per-account force probe recovery and regression coverage for workspace slot reconciliation, duplicate handling, export privacy/accessibility, and probe refresh failures

Validation: GitHub CI green at a65a70e, latest Codex review clean, unresolved review threads 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🤖 codex: ok [@codex review] says no issues found. db migration PR changes Alembic database migrations; maintainer must coordinate merge order

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Entry overwritten when multiple workspaces on one account

5 participants