Skip to content

core: make AGENTS.md react to environment changes#29810

Merged
sayan-oai merged 2 commits into
mainfrom
codex/dynamic-environment-agents-md
Jun 25, 2026
Merged

core: make AGENTS.md react to environment changes#29810
sayan-oai merged 2 commits into
mainfrom
codex/dynamic-environment-agents-md

Conversation

@sayan-oai

@sayan-oai sayan-oai commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator

Why

With deferred executors, a turn can begin before a remote environment attaches. AGENTS.md discovery previously ran only during session setup, so instructions from a later environment never reached the model or the session instruction sources.

WorldState persistence has now landed, so this uses the durable model-visible baseline directly instead of carrying a temporary resume/fork compatibility path.

What

  • Add an AgentsMdManager in SessionServices to own host instructions, loaded state, and refresh caching.
  • When DeferredExecutor is enabled, refresh AGENTS.md when attached environment selections change and freeze the result in the corresponding StepContext.
  • Represent AGENTS.md as a persisted WorldState section for every session, with bounded initial, replacement, and removal updates.
  • Remove duplicate AGENTS.md state and rendering from SessionConfiguration and TurnContext.
  • Build initial context, per-request updates, and compaction context from the same step-scoped value.
  • On resume and fork, compare current instructions with the restored WorldState baseline and inject a replacement exactly once when they differ.

Builds on #29833, #29835, and #29837.

Tests

  • Covers a remote environment becoming ready mid-turn, with AGENTS.md appearing on the next request exactly once and updating canonical instruction sources.
  • Covers full, unchanged, replaced, and removed AGENTS.md WorldState rendering.
  • Covers changed instructions across cold resume and fork without duplicate reinjection.
  • Covers remote-v2 compaction retaining creation-time instructions in the live session and cold resume appending one replacement when the source changed.
  • Ran focused codex-core AGENTS.md, WorldState, and context-update test suites.

@sayan-oai sayan-oai requested a review from a team as a code owner June 24, 2026 05:49
pub(crate) struct AgentsMdState {
instructions: Option<UserInstructions>,
// TODO(sayan): remove this fallback once rollouts persist and restore AGENTS.md state.
historical_state_unknown: bool,

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

an artifact of not having worldstate persistence yet. working on that as well

Comment thread codex-rs/core/src/session/environment_refresh.rs Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1dd2af58d3

ℹ️ 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 codex-rs/core/src/context/world_state/agents_md.rs
Comment on lines +49 to +50
// TODO(sayan): rollouts don't persist AGENTS.md state yet, so conservatively supersede history on resume.
world_state.add_section(AgentsMdState::unknown());

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

see other comment, this will be cleaned up when we persist worldstate correctly

@sayan-oai sayan-oai changed the title [codex] Refresh AGENTS.md when environments attach core: make agents.md react to environment changes Jun 24, 2026
send_environment_info(&mut websocket).await;
}

async fn serve_environment_with_agents_md(

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.

hm, at some point we'll have to start using a real server, you can't keep reimplementing it's features in mocks.

Comment thread codex-rs/core/src/compact_token_budget.rs
Comment thread codex-rs/core/src/session/mod.rs Outdated
Comment thread codex-rs/core/src/session/session.rs Outdated
Comment thread codex-rs/core/src/session/mod.rs Outdated
Comment on lines +3272 to +3277
// TODO(sayan): Implement WorldState persistence (including AGENTS.md) across
// resume, fork, rollback, and compaction. Then delete this direct rendering,
// remove the gate in build_world_state_for_step(), and let WorldState render
// AGENTS.md for all sessions. Keep calling AgentsMdManager::refresh() only for
// deferred executors so flag-off sessions keep their creation-time instructions.
if !turn_context

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

kept this split for now to avoid changing the behavior with the flag off until worldstate persistence lands. the long-term intention is to not have special injections and let agents.md always be rendered via worldstate.

@sayan-oai

Copy link
Copy Markdown
Collaborator Author

@codex review this

@chatgpt-codex-connector

Copy link
Copy Markdown
Contributor

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 898a6733b1

ℹ️ 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 codex-rs/core/src/context/world_state/agents_md.rs Outdated
Comment thread codex-rs/core/src/session/mod.rs

@pakrym-oai pakrym-oai left a comment

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.

The only concern is whether we re-inject agents.md on resume.

Worth an integration test if we missed it?

@sayan-oai sayan-oai force-pushed the codex/dynamic-environment-agents-md branch from 83e33b2 to fdff14a Compare June 25, 2026 05:50
@sayan-oai sayan-oai changed the title core: make agents.md react to environment changes core: make AGENTS.md react to environment changes Jun 25, 2026
@sayan-oai sayan-oai merged commit f2f80ef into main Jun 25, 2026
63 checks passed
@sayan-oai sayan-oai deleted the codex/dynamic-environment-agents-md branch June 25, 2026 05:57
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 25, 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