-
Notifications
You must be signed in to change notification settings - Fork 6.5k
feat: remote compaction #6795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: remote compaction #6795
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
7bf4525
feat: remote compaction
jif-oai bcb040a
Add feature flag
jif-oai 67f2408
Comment
jif-oai e49abd6
Fix a test
jif-oai a42864f
Add auth header
jif-oai 68902c4
Integration tests
jif-oai 51947f0
Updates
jif-oai d36e397
Fix tests
jif-oai 1e5606f
Process comment
jif-oai 5a85992
Merge branch 'main' into jif/client-compaction
pakrym-oai 98ba298
core: refactor remote history compaction to use Prompt; remove unused…
pakrym-oai 5e17334
core: add trace log for POST payload in ModelClient and expose get_co…
pakrym-oai b14edc4
Fixes
jif-oai 70f756b
Merge branch 'main' into jif/client-compaction
jif-oai c945c8f
Fixes
jif-oai 66c1ba6
Merge remote-tracking branch 'origin/jif/client-compaction' into jif/…
jif-oai 1608198
clippy
jif-oai 108fbf9
Fix tests
jif-oai 32e5b0b
Clippy
jif-oai 6944a99
Fix windows
jif-oai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| use std::sync::Arc; | ||
|
|
||
| use crate::Prompt; | ||
| use crate::codex::Session; | ||
| use crate::codex::TurnContext; | ||
| use crate::error::Result as CodexResult; | ||
| use crate::protocol::AgentMessageEvent; | ||
| use crate::protocol::CompactedItem; | ||
| use crate::protocol::ErrorEvent; | ||
| use crate::protocol::EventMsg; | ||
| use crate::protocol::RolloutItem; | ||
| use crate::protocol::TaskStartedEvent; | ||
| use codex_protocol::models::ResponseInputItem; | ||
| use codex_protocol::models::ResponseItem; | ||
| use codex_protocol::user_input::UserInput; | ||
|
|
||
| pub(crate) async fn run_remote_compact_task( | ||
| sess: Arc<Session>, | ||
| turn_context: Arc<TurnContext>, | ||
| input: Vec<UserInput>, | ||
| ) -> Option<String> { | ||
| let start_event = EventMsg::TaskStarted(TaskStartedEvent { | ||
| model_context_window: turn_context.client.get_model_context_window(), | ||
| }); | ||
| sess.send_event(&turn_context, start_event).await; | ||
|
|
||
| match run_remote_compact_task_inner(&sess, &turn_context, input).await { | ||
| Ok(()) => { | ||
| let event = EventMsg::AgentMessage(AgentMessageEvent { | ||
| message: "Compact task completed".to_string(), | ||
| }); | ||
| sess.send_event(&turn_context, event).await; | ||
| } | ||
| Err(err) => { | ||
| let event = EventMsg::Error(ErrorEvent { | ||
| message: err.to_string(), | ||
| }); | ||
| sess.send_event(&turn_context, event).await; | ||
| } | ||
| } | ||
|
|
||
| None | ||
| } | ||
|
|
||
| async fn run_remote_compact_task_inner( | ||
| sess: &Arc<Session>, | ||
| turn_context: &Arc<TurnContext>, | ||
| input: Vec<UserInput>, | ||
| ) -> CodexResult<()> { | ||
| let mut history = sess.clone_history().await; | ||
| if !input.is_empty() { | ||
| let initial_input_for_turn: ResponseInputItem = ResponseInputItem::from(input); | ||
| history.record_items(&[initial_input_for_turn.into()]); | ||
| } | ||
|
|
||
| let prompt = Prompt { | ||
| input: history.get_history_for_prompt(), | ||
| tools: vec![], | ||
| parallel_tool_calls: false, | ||
| base_instructions_override: turn_context.base_instructions.clone(), | ||
| output_schema: None, | ||
| }; | ||
|
|
||
| let mut new_history = turn_context | ||
| .client | ||
| .compact_conversation_history(&prompt) | ||
| .await?; | ||
| // Required to keep `/undo` available after compaction | ||
| let ghost_snapshots: Vec<ResponseItem> = history | ||
| .get_history() | ||
| .iter() | ||
| .filter(|item| matches!(item, ResponseItem::GhostSnapshot { .. })) | ||
| .cloned() | ||
| .collect(); | ||
|
|
||
| if !ghost_snapshots.is_empty() { | ||
| new_history.extend(ghost_snapshots); | ||
| } | ||
| sess.replace_history(new_history.clone()).await; | ||
|
|
||
| if let Some(estimated_tokens) = sess | ||
| .clone_history() | ||
| .await | ||
| .estimate_token_count(turn_context.as_ref()) | ||
| { | ||
| sess.override_last_token_usage_estimate(turn_context.as_ref(), estimated_tokens) | ||
| .await; | ||
| } | ||
|
|
||
| let compacted_item = CompactedItem { | ||
| message: String::new(), | ||
| replacement_history: Some(new_history), | ||
| }; | ||
| sess.persist_rollout_items(&[RolloutItem::Compacted(compacted_item)]) | ||
| .await; | ||
| Ok(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: share request building
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will refactor this in a dedicated PR. This code is way too messy