Skip to content

[codex] Use background agent task auth for backend calls#18094

Merged
adrian-openai merged 4 commits intomainfrom
dev/adrian/codex/agent-identity-background-task
Apr 20, 2026
Merged

[codex] Use background agent task auth for backend calls#18094
adrian-openai merged 4 commits intomainfrom
dev/adrian/codex/agent-identity-background-task

Conversation

@adrian-openai
Copy link
Copy Markdown
Contributor

@adrian-openai adrian-openai commented Apr 16, 2026

Summary

Introduces a single background/control-plane agent task for ChatGPT backend requests that do not have a thread-scoped task, with AuthManager owning the default ChatGPT backend authorization decision.

Callers now ask AuthManager for the default ChatGPT backend authorization header. AuthManager decides whether that is bearer or background AgentAssertion based on config/internal state, while low-level bootstrap paths can explicitly request bearer-only auth.

This PR is stacked on PR4 and focuses on the shared background task auth plumbing plus the first tranche of backend/control-plane consumers. The remaining callsite wiring is split into PR4.2 to keep review size down.

Stack

What Changed

  • add background task registration and assertion minting inside codex-login
  • persist agent_identity.background_task_id separately from per-session task state
  • make BackgroundAgentTaskManager private to codex-login; call sites do not instantiate or pass it around
  • teach AuthManager the ChatGPT backend base URL and feature-derived background auth mode from resolved config
  • expose bearer-only helpers for bootstrap/registration/refresh-style paths that must not use AgentAssertion
  • wire AuthManager default ChatGPT authorization through app listing, connector directory listing, remote plugins, MCP status/listing, analytics, and core-skills remote calls
  • preserve bearer fallback when the feature is disabled, the backend host is unsupported, or background task registration is not available

Validation

  • just fmt
  • cargo check -p codex-core -p codex-login -p codex-analytics -p codex-app-server -p codex-cloud-requirements -p codex-cloud-tasks -p codex-models-manager -p codex-chatgpt -p codex-model-provider -p codex-mcp -p codex-core-skills
  • cargo test -p codex-login agent_identity
  • cargo test -p codex-model-provider bearer_auth_provider
  • cargo test -p codex-core agent_assertion
  • cargo test -p codex-app-server remote_control
  • cargo test -p codex-cloud-requirements fetch_cloud_requirements
  • cargo test -p codex-models-manager manager::tests
  • cargo test -p codex-chatgpt
  • cargo test -p codex-cloud-tasks
  • just fix -p codex-core -p codex-login -p codex-analytics -p codex-app-server -p codex-cloud-requirements -p codex-cloud-tasks -p codex-models-manager -p codex-chatgpt -p codex-model-provider -p codex-mcp -p codex-core-skills
  • just fix -p codex-app-server
  • git diff --check

config: &McpConfig,
) -> HashMap<String, McpServerConfig> {
with_codex_apps_mcp_with_authorization_header(
servers, auth, config, /*authorization_header_value*/ None,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

it is on purpose to be none atm?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, this is intentional. with_codex_apps_mcp is the legacy/default wrapper. Passing None means “no explicit authorization header override,” and then the helper falls back to the old bearer-token behavior.

The new AgentAssertion-aware path is with_codex_apps_mcp_with_authorization_header(...), where callers pass the background authorization header after applying the feature gate. I also checked workspace call sites: the old with_codex_apps_mcp(...) wrapper is basically just the compatibility/default path plus tests; the product paths that need background auth are using the new _with_authorization_header form.

Comment thread codex-rs/core/src/agent_identity.rs
Copy link
Copy Markdown
Collaborator

@shijie-oai shijie-oai left a comment

Choose a reason for hiding this comment

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

Overall it is fine but I wonder do non inference related request need to enforce task_id check? Or we are enforcing it for all requests to chatgpt? Just want to understand better cause we are covering a lot of items here.

Comment thread codex-rs/login/src/agent_identity.rs Outdated
const AGENT_IDENTITY_BISCUIT_TIMEOUT: Duration = Duration::from_secs(15);

#[derive(Clone)]
pub struct BackgroundAgentTaskManager {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Funky idea: (probably not great and break existing paradigm) why can we not just get a generic task id back when an agent identity is created for doing work outside of inferencing?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Discussed offline - best solution is to eventually move to a scope-based, inference can only be called with a task, and control plane calls can be made just by the agent identity itself, but until then, background task it is!

Comment on lines +55 to +56
#[serde(default, skip_serializing_if = "Option::is_none")]
pub background_task_id: Option<String>,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This looks longer living than a task id for a session?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yep! This is the background/control-plane task id for calls that do not naturally belong to a live Codex thread: models manager, cloud tasks, connectors, MCP app setup, etc. It is stored in auth.json alongside the persisted agent identity so those background calls can use AgentAssertion auth without needing a session rollout item.

@adrian-openai adrian-openai force-pushed the dev/adrian/codex/agent-identity-use-assertion-split branch from c72faed to 68a45b4 Compare April 20, 2026 00:37
@adrian-openai adrian-openai force-pushed the dev/adrian/codex/agent-identity-background-task branch 2 times, most recently from 40b89e8 to 42f625d Compare April 20, 2026 01:01
@adrian-openai adrian-openai force-pushed the dev/adrian/codex/agent-identity-use-assertion-split branch from 68a45b4 to c6d377f Compare April 20, 2026 01:10
@adrian-openai adrian-openai force-pushed the dev/adrian/codex/agent-identity-background-task branch 2 times, most recently from a722d39 to 72acf86 Compare April 20, 2026 03:30
@adrian-openai adrian-openai force-pushed the dev/adrian/codex/agent-identity-use-assertion-split branch from 2d2fd76 to 044222d Compare April 20, 2026 05:17
@adrian-openai adrian-openai force-pushed the dev/adrian/codex/agent-identity-background-task branch from 72acf86 to 797959a Compare April 20, 2026 05:18
adrian-openai added a commit that referenced this pull request Apr 20, 2026
## Summary

This is the AgentAssertion downstream slice for feature-gated agent
identity support, replacing the oversized AgentAssertion slice from PR
#17807.

It isolates task-scoped downstream AgentAssertion wiring on top of the
merged PR3.1 work without re-carrying the earlier agent registration,
task registration, or task-state history.

This PR includes the task-scoped bug-fix call sites from the review:
generic file upload auth, MCP OpenAI file upload auth, and ARC monitor
auth. Broader user/control-plane calls move to PR4.1 and PR4.2.

## Stack

- PR1: #17385 - add
`features.use_agent_identity`
- PR2: #17386 - register agent
identities when enabled
- PR3: #17387 - register agent tasks
when enabled
- PR3.1: #17978 - persist and
prewarm registered tasks per thread
- PR4: this PR - use task-scoped `AgentAssertion` downstream when
enabled
- PR4.1: #18094 - introduce
AuthManager-owned background/control-plane `AgentAssertion` auth
- PR4.2: #18260 - use background
task auth for additional backend/control-plane calls

## What Changed

- add AgentAssertion envelope generation in `codex-core`
- route downstream HTTP and websocket auth through AgentAssertion when
an agent task is present
- extend the model-provider auth provider so non-bearer authorization
schemes can be passed through cleanly
- make generic file uploads attach the full authorization header value
- make MCP OpenAI file uploads use the cached thread agent task
assertion when present
- make ARC monitor calls use the cached thread agent task assertion when
present

## Why

The original PR had drifted ancestry and showed a much larger diff than
the semantic change actually required. Restacking it onto PR3.1 keeps
the reviewable surface down to the downstream assertion slice.

## Validation

- `just fmt`
- `cargo check -p codex-core -p codex-login -p codex-analytics -p
codex-app-server -p codex-cloud-requirements -p codex-cloud-tasks -p
codex-models-manager -p codex-chatgpt -p codex-model-provider -p
codex-mcp -p codex-core-skills`
- `cargo test -p codex-model-provider bearer_auth_provider`
- `cargo test -p codex-core agent_assertion`
- `cargo test -p codex-app-server remote_control`
- `cargo test -p codex-cloud-requirements fetch_cloud_requirements`
- `cargo test -p codex-models-manager manager::tests`
- `cargo test -p codex-chatgpt`
- `cargo test -p codex-cloud-tasks`
- `cargo test -p codex-login agent_identity`
- `just fix -p codex-core -p codex-login -p codex-analytics -p
codex-app-server -p codex-cloud-requirements -p codex-cloud-tasks -p
codex-models-manager -p codex-chatgpt -p codex-model-provider -p
codex-mcp -p codex-core-skills`
- `just fix -p codex-app-server`
- `git diff --check`
Base automatically changed from dev/adrian/codex/agent-identity-use-assertion-split to main April 20, 2026 06:16
@adrian-openai adrian-openai force-pushed the dev/adrian/codex/agent-identity-background-task branch from 797959a to 44c3502 Compare April 20, 2026 06:19
@adrian-openai adrian-openai merged commit 904c751 into main Apr 20, 2026
25 checks passed
@adrian-openai adrian-openai deleted the dev/adrian/codex/agent-identity-background-task branch April 20, 2026 13:50
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 20, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants