Skip to content

feat(core): store turn_id on ResponseItem metadata#28360

Open
owenlin0 wants to merge 3 commits into
mainfrom
owen/stamp_and_preserve_meta_turn_id
Open

feat(core): store turn_id on ResponseItem metadata#28360
owenlin0 wants to merge 3 commits into
mainfrom
owen/stamp_and_preserve_meta_turn_id

Conversation

@owenlin0

@owenlin0 owenlin0 commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

Description

This PR is a followup to the mechanical plumbing PR that added metadata on ResponseItem: #28355, and starts assigning metadata.turn_id to Responses API items created during a turn.

The goal is that items are stably associated with the turn_id that introduced them, even when using stateless HTTP or resending context to ResponsesAPI on thread/resume when using websockets.

What changed

  • Set metadata.turn_id on streamed output items, recorded conversation items, initial context, inter-agent communication items, and compaction trigger/input/summary items when missing.
  • Preserve existing item turn IDs instead of overwriting them during rollout persistence, resume reconstruction, compaction, and forked history paths.
  • Update request/snapshot helpers and focused assertions for stateless requests, websocket incrementals, compaction triggers, prompt caching, image rollout, extension history, subagent notifications, and token-budget follow-ups.

Validation

  • just fmt
  • cargo check -p codex-core -p codex-protocol -p codex-app-server-protocol
  • Tests not run; relying on CI for the test suites.

@owenlin0 owenlin0 changed the title [codex] stamp Responses API item turn ids feat(core): store turn_id on ResponseItem metadata Jun 15, 2026
@owenlin0 owenlin0 force-pushed the owen/carry_responses_api_item_metadata branch from 1537199 to e5daf04 Compare June 15, 2026 17:59
@owenlin0 owenlin0 force-pushed the owen/stamp_and_preserve_meta_turn_id branch 3 times, most recently from b4ee3c1 to 3e26c3d Compare June 15, 2026 18:35
@owenlin0 owenlin0 marked this pull request as ready for review June 15, 2026 18:38
@owenlin0 owenlin0 requested a review from a team as a code owner June 15, 2026 18:38
owenlin0 added a commit that referenced this pull request Jun 15, 2026
## Description

This PR adds an optional `metadata` field to `ResponseItem` for
Responses API calls. Only mechanical plumbing, no actual values
populated and sent yet. Turns out just adding a new field to
`ResponseItem` has quite a large blast radius already.

This change is backwards compatible because `metadata` is optional and
omitted when absent, so existing response items and rollout history
without it still deserialize and requests that do not set it keep the
same wire shape. For provider compatibility, we strip out `metadata`
before non-OpenAI Responses requests so Azure and AWS Bedrock never see
this field.

My followup PR here will actually make use of it to start storing and
passing along `turn_id`: #28360

## What changed

- Added `ResponseItemMetadata` with optional `turn_id`, plus optional
`metadata` on Responses API item variants and inter-agent communication.
- Preserved item metadata through response-item rewrites such as
truncation, missing tool-output synthesis, compaction history
rebuilding, visible-history conversion, rollout/resume, and generated
app-server schemas/types.
- Strip item metadata from non-OpenAI Responses requests while
preserving it for OpenAI-shaped requests.
- Updated the mechanical fixture/test construction churn required by the
new optional field.
Base automatically changed from owen/carry_responses_api_item_metadata to main June 15, 2026 22:05
@owenlin0 owenlin0 force-pushed the owen/stamp_and_preserve_meta_turn_id branch 3 times, most recently from 993ed6a to 9b81c0f Compare June 15, 2026 22:46
@owenlin0 owenlin0 force-pushed the owen/stamp_and_preserve_meta_turn_id branch from 9b81c0f to 0b66f2b Compare June 15, 2026 22:46
@owenlin0 owenlin0 requested a review from pakrym-oai June 15, 2026 22:53

impl ResponseItemMetadata {
/// Returns the non-empty turn ID carried by this metadata, if present.
pub(crate) fn turn_id(&self) -> Option<&str> {

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.

too many turn_id/set_turn_id_if_missing helpers everywhere.

"<token_budget>\nThread id {thread_id}.\nCurrent context window 1.\nYou have {EFFECTIVE_CONTEXT_WINDOW} tokens left in this context window.\n</token_budget>"
)]
);
let follow_up_body = requests[2].body_json();

@pakrym-oai pakrym-oai Jun 15, 2026

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 test is unrelated to your feature. revert?

communication: InterAgentCommunication,
mut communication: InterAgentCommunication,
) {
communication.set_turn_id_if_missing(&turn_context.sub_id);

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.

you are calling prepare_conversation_items_for_history below

message_item("second"),
]);
let mut first_user = message_item("hello");
first_user.set_turn_id_if_missing("turn-123");

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.

can the test churn be reduced?

@pakrym-oai pakrym-oai Jun 15, 2026

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 seems like an unrelated test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants