Skip to content

[context] Store context messages in rollouts - #2243

Merged
dylan-hurd-oai merged 6 commits into
mainfrom
dh--stable-prefix-messages
Aug 14, 2025
Merged

dylan-hurd-oai merged 6 commits into
mainfrom
dh--stable-prefix-messages

Conversation

@dylan-hurd-oai

Copy link
Copy Markdown
Contributor

Summary

Currently, we use request-time logic to determine the user_instructions and environment_context messages. This means that neither of these values can change over time as conversations go on. We want to add in additional details here, so we're migrating these to save these messages to the rollout file instead. This is simpler for the client, and allows us to append additional environment_context messages to each turn if we want

Testing

  • Integration test coverage
  • Tested locally with a few turns, confirmed model could reference environment context and cached token metrics were reasonably high

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

// Always record an initial environment context snapshot.
let ec_msg: ResponseItem = EnvironmentContext::from(sess_arc.as_ref()).into();
sess_arc.record_conversation_items(&[ec_msg]).await;

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.

Intentionally changing the order here, since we are no currently planning on re-reading user_instructions, but are planning on sending multiple environment_context messages. I don't think it's material, but noting for posterity

@github-actions

Copy link
Copy Markdown
Contributor

Summary
This PR moves environment context into a dedicated module and shifts injection of user instructions/environment context from request-time to conversation history. It adds formatting helpers on Prompt, records stable messages once per session in submission_loop, updates tests for new ordering, and makes Session::sandbox_policy pub(crate).

Notable changes

  • New core/src/environment_context.rs with EnvironmentContext and Display + From<&Session>.
  • Prompt no longer carries user_instructions/environment_context; adds format_*_message helpers.
  • submission_loop records initial user instructions and environment context into history.
  • Tests updated: expected input order is user_instructions then environment_context.

Review
The direction (persisting stable messages and simplifying request assembly) looks good and fits the PR motivation. A few minor suggestions for robustness and test clarity:

  • codex-rs/core/src/codex.rs @@ -904,0 +905,17

    • Ensure the “record once” behavior can’t run multiple times per session (e.g., if submission_loop iterates). Consider a flag in Session or a history check to avoid duplicate inserts.
  • codex-rs/core/tests/client.rs @@ -394,2 +394,2 and @@ -397,2 +397,2

    • Prefer deep equality on the entire input array over starts_with/ends_with string checks to serve as executable documentation and cover structure more completely.
  • codex-rs/core/tests/prompt_caching.rs @@ -119 +119

    • Nice: order change captured. Consider adding an assertion that the two prefix messages appear exactly once across multiple turns.
  • codex-rs/core/src/environment_context.rs (new)

    • Consider deriving PartialEq to enable deep assertions in tests that compare full formatted messages.
  • codex-rs/core/src/client_common.rs @@ -109,4 +66,9 and @@ -115,15 +77,8

    • The new format_*_message helpers tighten responsibilities; good consolidation. Optional: unit tests for these helpers to lock the message shape.

View workflow run

@dylan-hurd-oai
dylan-hurd-oai force-pushed the dh--stable-prefix-messages branch from 2f76c1f to 12dd2ee Compare August 14, 2025 02:01
@dylan-hurd-oai dylan-hurd-oai changed the title [context] Store context in rollouts [context] Store context messages in rollouts Aug 14, 2025
@dylan-hurd-oai
dylan-hurd-oai force-pushed the dh--stable-prefix-messages branch from 12dd2ee to 56ce1cd Compare August 14, 2025 16:47
@dylan-hurd-oai
dylan-hurd-oai force-pushed the dh--stable-prefix-messages branch from 56ce1cd to 1dca123 Compare August 14, 2025 17:02
@dylan-hurd-oai
dylan-hurd-oai force-pushed the dh--stable-prefix-messages branch from e5a5c9b to 1cd3eb0 Compare August 14, 2025 17:05
Comment thread codex-rs/core/src/codex.rs Outdated
.history
.record_items(&[Prompt::format_user_instructions_message(&user_instructions)]);
}
state

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.

Don't we want to store it in a more structured way so it's easier to parse?

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.

That is handled by #2272! They are independent and this change is more urgent

sess.get_sandbox_policy().clone(),
))])
.await;

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.

@bolinfest is this the preferred spot where we'd like to add this? I know we also have #2305

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.

At a minimum, we should combine this into one call to record_conversation_items() so we only have one lock/await. I will try to fix this in #2305.

@dylan-hurd-oai
dylan-hurd-oai merged commit 544980c into main Aug 14, 2025
@dylan-hurd-oai
dylan-hurd-oai deleted the dh--stable-prefix-messages branch August 14, 2025 18:51
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 14, 2025
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.

3 participants