Conversation
…loading Codex sessions
Problem
- Users see correct assistant output during live websocket streaming.
- After refresh, restored session history can include hidden internal prompt content
(for example: "# AGENTS.md instructions ...", "<environment_context> ...")
that was never rendered in the live UI.
- This creates a confusing mismatch between live conversation and rehydrated history.
Root cause
- Realtime path and history path were sourcing messages differently:
- Realtime rendering (codex-response item events) shows assistant-visible content only.
- server/openai-codex.js:26-120
- src/components/chat/hooks/useChatRealtimeHandlers.ts:774-903
- Refresh path loads persisted JSONL and reconstructs chat from /api/codex/sessions/:id/messages:
- src/utils/api.js:51-67
- src/components/chat/hooks/useChatSessionState.ts:100-121
- server/routes/codex.js:71-83
- In Codex JSONL, `response_item` entries with role=user can include expanded internal context
(AGENTS instructions, env context, system/developer scaffolding), not only end-user text.
- Previous parser consumed `response_item.message` for both roles and only filtered
`<environment_context>` by string match, which is incomplete and let AGENTS content through.
- server/projects.js (before): around 1637-1658
What changed
1. Added a strict visibility guard for user messages:
- `isVisibleCodexUserMessage(payload)` in server/projects.js:1493-1513
- Accepts only:
- payload.type === "user_message"
- kind is absent or "plain"
- non-empty text
- not an environment_context scaffold
2. Updated Codex session metadata parsing to use visible user messages only:
- server/projects.js:1550-1556
- Session summary/messageCount now reflect real user prompts, not internal injected content.
3. Updated Codex history reconstruction:
- User messages now come from `event_msg.user_message` only:
- server/projects.js:1659-1669
- `response_item.message` is now accepted only for assistant role:
- server/projects.js:1671-1692
- This aligns restored history with live websocket behavior.
Behavior examples
Example A (simple "Hi" flow)
- JSONL contains:
- response_item role=user => "# AGENTS.md instructions ... + <environment_context> ..."
- event_msg user_message kind=plain => "Hi"
- response_item role=assistant => "Hi. What do you want to work on?"
- Before:
- Restored user history could show AGENTS.md block.
- After:
- Restored history shows only:
- user: "Hi"
- assistant: "Hi. What do you want to work on?"
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThis change introduces a centralized visibility filter ( Changes
Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary This PR fixes a Codex history parsing bug where session reload could show hidden internal prompt context (for example AGENTS.md instructions) that never appeared in live websocket responses. The fix aligns restored history with live behavior by sourcing user-visible user messages from
event_msg.user_message (plain)and restrictingresponse_item.messageto assistant role only.Issue
Hi.Root cause analysis
server/openai-codex.js:26-120transforms Codex stream items.src/components/chat/hooks/useChatRealtimeHandlers.ts:774-903renders assistant/tool/reasoning items.src/components/chat/hooks/useChatSessionState.ts:100-121calls session history endpoint.src/utils/api.js:51-67routes codex to/api/codex/sessions/:sessionId/messages.server/routes/codex.js:71-83callsgetCodexSessionMessages.server/projects.jspreviously reconstructed user messages fromresponse_item.message.response_item role=usermay include expanded internal context (AGENTS/system/developer/env payloads).event_msg user_messagewithkind=plainis the canonical end-user text.Solution
server/projects.js:1493-1513isVisibleCodexUserMessage(payload)ensures only real user-facing plain messages are accepted.server/projects.js:1550-1556server/projects.js:1659-1669add user messages fromevent_msg.user_messageonly.server/projects.js:1671-1692parseresponse_item.messageonly when role is assistant.Concrete examples
Example (target bug):
Hi# AGENTS.md instructions ...(incorrect)Hi. What do you want to work on?HiHi. What do you want to work on?Manual QA checklist
Hi.Hi+ assistant response are preserved.Summary by CodeRabbit