mirror of
https://wget.la/https://github.com/leookun/cursor-byok
synced 2026-12-01 01:05:10 +08:00
fix: preempt root loop on context injection
This commit is contained in:
1 parent
aa68205735
commit
ee915ee760
12 files changed
+1027
-79
No files matched your search
+82
-14
@@ -249,14 +249,14 @@ impl RunEngine {
|
||||
let mut pending_insertions = Vec::new();
|
||||
let cycle = loop {
|
||||
tokio::select! {
|
||||
result = &mut cycle => break result,
|
||||
biased;
|
||||
command = client.commands.recv() => {
|
||||
let message = match command {
|
||||
Some(ClientCommand::InsertMessages(insertion)) => {
|
||||
pending_insertions.push(insertion);
|
||||
continue;
|
||||
}
|
||||
Some(ClientCommand::RuntimeMessage(message)) => message,
|
||||
Some(ClientCommand::InterruptWithMessage(message)) => message,
|
||||
Some(ClientCommand::RuntimeEvent(event)) => event.into_message(),
|
||||
Some(ClientCommand::Cancel) => {
|
||||
cycle_cancellation.cancel();
|
||||
@@ -321,7 +321,8 @@ impl RunEngine {
|
||||
Err(outcome) => return (outcome, usage),
|
||||
};
|
||||
continue 'model;
|
||||
}
|
||||
},
|
||||
result = &mut cycle => break result,
|
||||
}
|
||||
};
|
||||
let cycle = match cycle {
|
||||
@@ -558,23 +559,68 @@ impl RunEngine {
|
||||
let cycle_cancellation = cancellation.child_token();
|
||||
let (silent_events, mut discarded_events) = tokio::sync::mpsc::channel(256);
|
||||
let drain = tokio::spawn(async move { while discarded_events.recv().await.is_some() {} });
|
||||
let cycle = consume_model_cycle(
|
||||
self.provider.stream(invocation, cycle_cancellation.clone()),
|
||||
&silent_events,
|
||||
&cycle_cancellation,
|
||||
)
|
||||
.await;
|
||||
let mut pending_insertions = Vec::new();
|
||||
let mut interrupted_message = None;
|
||||
let cycle = {
|
||||
let cycle = consume_model_cycle(
|
||||
self.provider.stream(invocation, cycle_cancellation.clone()),
|
||||
&silent_events,
|
||||
&cycle_cancellation,
|
||||
);
|
||||
tokio::pin!(cycle);
|
||||
loop {
|
||||
tokio::select! {
|
||||
biased;
|
||||
command = client.commands.recv() => match command {
|
||||
Some(ClientCommand::InsertMessages(insertion)) => {
|
||||
pending_insertions.push(insertion);
|
||||
}
|
||||
Some(ClientCommand::InterruptWithMessage(message)) => {
|
||||
cycle_cancellation.cancel();
|
||||
interrupted_message = Some(message);
|
||||
break cycle.await;
|
||||
}
|
||||
Some(ClientCommand::RuntimeEvent(event)) => {
|
||||
cycle_cancellation.cancel();
|
||||
interrupted_message = Some(event.into_message());
|
||||
break cycle.await;
|
||||
}
|
||||
Some(ClientCommand::Cancel) => {
|
||||
cycle_cancellation.cancel();
|
||||
return Err(RunOutcome::Cancelled);
|
||||
}
|
||||
Some(ClientCommand::ClientClosed { error }) => {
|
||||
cycle_cancellation.cancel();
|
||||
return Err(RunOutcome::Failed(RunFailure::Client(error)));
|
||||
}
|
||||
Some(ClientCommand::ToolResult(_)) => {
|
||||
cycle_cancellation.cancel();
|
||||
return Err(RunOutcome::Failed(RunFailure::Protocol(
|
||||
"received a tool result while automatic compaction was running".into(),
|
||||
)));
|
||||
}
|
||||
None => {
|
||||
cycle_cancellation.cancel();
|
||||
return Err(client_failure());
|
||||
}
|
||||
},
|
||||
result = &mut cycle => break result,
|
||||
}
|
||||
}
|
||||
};
|
||||
drop(silent_events);
|
||||
let _ = drain.await;
|
||||
let (summary, compaction_usage) = match cycle {
|
||||
Ok(cycle) if cycle.calls.is_empty() && !cycle.text.trim().is_empty() => {
|
||||
let (summary, compaction_usage) = match (interrupted_message.is_some(), cycle) {
|
||||
(true, Ok(cycle)) => (fallback_summary(&compactable), cycle.usage),
|
||||
(true, Err(failure)) => (fallback_summary(&compactable), failure.usage),
|
||||
(false, Ok(cycle)) if cycle.calls.is_empty() && !cycle.text.trim().is_empty() => {
|
||||
(cycle.text.trim().to_string(), cycle.usage)
|
||||
}
|
||||
Ok(cycle) => {
|
||||
(false, Ok(cycle)) => {
|
||||
tracing::warn!("automatic compaction returned no usable summary; using fallback");
|
||||
(fallback_summary(&compactable), cycle.usage)
|
||||
}
|
||||
Err(failure) => {
|
||||
(false, Err(failure)) => {
|
||||
tracing::warn!(error = ?failure.failure, "automatic compaction model failed; using fallback");
|
||||
(fallback_summary(&compactable), failure.usage)
|
||||
}
|
||||
@@ -594,7 +640,7 @@ impl RunEngine {
|
||||
let mut replacement = retained_request_context.into_iter().collect::<Vec<_>>();
|
||||
replacement.push(summary_message);
|
||||
replacement.extend(prepared.initial_messages.iter().cloned());
|
||||
let revision = self
|
||||
let mut revision = self
|
||||
.store
|
||||
.replace_revision(
|
||||
&prepared.conversation_id,
|
||||
@@ -620,6 +666,28 @@ impl RunEngine {
|
||||
emit(client, ClientEvent::AutoCompactionCompleted)
|
||||
.await
|
||||
.map_err(|_| client_failure())?;
|
||||
revision = append_insertions(
|
||||
&self.store,
|
||||
prepared,
|
||||
client,
|
||||
cancellation,
|
||||
revision,
|
||||
pending_insertions,
|
||||
)
|
||||
.await?
|
||||
.0;
|
||||
if let Some(message) = interrupted_message {
|
||||
revision = append_runtime_message(
|
||||
&self.store,
|
||||
prepared,
|
||||
client,
|
||||
cancellation,
|
||||
revision,
|
||||
message,
|
||||
)
|
||||
.await?
|
||||
.0;
|
||||
}
|
||||
Ok((revision, compaction_usage))
|
||||
}
|
||||
}
|
||||
|
||||
+102
-30
@@ -1,3 +1,5 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
use crate::{
|
||||
@@ -5,7 +7,7 @@ use crate::{
|
||||
ClientCommand, ClientEvent, ClientPort, CommitBarrier, CommitCause, MessageInsertion,
|
||||
StateCommitted,
|
||||
},
|
||||
model::{PreparedRun, RevisionId, ToolCall, ToolRoundAssistant, ToolRoundId},
|
||||
model::{PreparedRun, RevisionId, ToolCall, ToolResult, ToolRoundAssistant, ToolRoundId},
|
||||
store::Store,
|
||||
};
|
||||
|
||||
@@ -70,6 +72,7 @@ pub(super) async fn execute(
|
||||
.await?;
|
||||
|
||||
let mut remaining = calls.len();
|
||||
let mut completed_call_ids = HashSet::new();
|
||||
let mut pending_runtime_messages = insertions
|
||||
.into_iter()
|
||||
.map(PendingRuntimeMessage::Insertion)
|
||||
@@ -92,6 +95,7 @@ pub(super) async fn execute(
|
||||
.await
|
||||
.map_err(failed)?;
|
||||
revision = committed.revision_id;
|
||||
completed_call_ids.insert(call_id.clone());
|
||||
tracing::info!(
|
||||
round_id = %round_id,
|
||||
call_id,
|
||||
@@ -113,7 +117,10 @@ pub(super) async fn execute(
|
||||
ClientEvent::StateCommitted(StateCommitted {
|
||||
revision_id: revision,
|
||||
tool_round_version: committed.tool_round_version,
|
||||
cause: CommitCause::ToolResult { call_id },
|
||||
cause: CommitCause::ToolResult {
|
||||
call_id,
|
||||
interrupted: false,
|
||||
},
|
||||
barrier,
|
||||
}),
|
||||
)
|
||||
@@ -125,8 +132,66 @@ pub(super) async fn execute(
|
||||
Some(ClientCommand::RuntimeEvent(event)) => {
|
||||
pending_runtime_messages.push(PendingRuntimeMessage::Message(event.into_message()));
|
||||
}
|
||||
Some(ClientCommand::RuntimeMessage(message)) => {
|
||||
pending_runtime_messages.push(PendingRuntimeMessage::Message(message));
|
||||
Some(ClientCommand::InterruptWithMessage(message)) => {
|
||||
for call in calls
|
||||
.iter()
|
||||
.filter(|call| !completed_call_ids.contains(&call.call_id))
|
||||
{
|
||||
let result = ToolResult {
|
||||
call_id: call.call_id.clone(),
|
||||
content: "Tool execution was interrupted by a newer user message.".into(),
|
||||
is_error: true,
|
||||
image: None,
|
||||
};
|
||||
let committed = store
|
||||
.commit_tool_result(
|
||||
&prepared.conversation_id,
|
||||
&prepared.run_id,
|
||||
&round_id,
|
||||
&result,
|
||||
)
|
||||
.await
|
||||
.map_err(failed)?;
|
||||
revision = committed.revision_id;
|
||||
let (barrier, ready) = if committed.settled {
|
||||
let (barrier, ready) = CommitBarrier::before_continue();
|
||||
(barrier, Some(ready))
|
||||
} else {
|
||||
(CommitBarrier::None, None)
|
||||
};
|
||||
send(
|
||||
client,
|
||||
ClientEvent::StateCommitted(StateCommitted {
|
||||
revision_id: revision,
|
||||
tool_round_version: committed.tool_round_version,
|
||||
cause: CommitCause::ToolResult {
|
||||
call_id: call.call_id.clone(),
|
||||
interrupted: true,
|
||||
},
|
||||
barrier,
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
if let Some(ready) = ready {
|
||||
super::engine::wait_for_state_ready(ready, cancellation).await?;
|
||||
}
|
||||
}
|
||||
for pending in pending_runtime_messages {
|
||||
revision =
|
||||
append_pending(store, prepared, client, cancellation, revision, pending)
|
||||
.await?;
|
||||
}
|
||||
revision = super::engine::append_runtime_message(
|
||||
store,
|
||||
prepared,
|
||||
client,
|
||||
cancellation,
|
||||
revision,
|
||||
message,
|
||||
)
|
||||
.await?
|
||||
.0;
|
||||
return Ok(revision);
|
||||
}
|
||||
Some(ClientCommand::InsertMessages(insertion)) => {
|
||||
pending_runtime_messages.push(PendingRuntimeMessage::Insertion(insertion))
|
||||
@@ -139,32 +204,7 @@ pub(super) async fn execute(
|
||||
}
|
||||
}
|
||||
for pending in pending_runtime_messages {
|
||||
match pending {
|
||||
PendingRuntimeMessage::Message(message) => {
|
||||
revision = super::engine::append_runtime_message(
|
||||
store,
|
||||
prepared,
|
||||
client,
|
||||
cancellation,
|
||||
revision,
|
||||
message,
|
||||
)
|
||||
.await?
|
||||
.0;
|
||||
}
|
||||
PendingRuntimeMessage::Insertion(insertion) => {
|
||||
revision = super::engine::append_insertions(
|
||||
store,
|
||||
prepared,
|
||||
client,
|
||||
cancellation,
|
||||
revision,
|
||||
vec![insertion],
|
||||
)
|
||||
.await?
|
||||
.0;
|
||||
}
|
||||
}
|
||||
revision = append_pending(store, prepared, client, cancellation, revision, pending).await?;
|
||||
}
|
||||
Ok(revision)
|
||||
}
|
||||
@@ -174,6 +214,38 @@ enum PendingRuntimeMessage {
|
||||
Insertion(MessageInsertion),
|
||||
}
|
||||
|
||||
async fn append_pending(
|
||||
store: &Store,
|
||||
prepared: &PreparedRun,
|
||||
client: &mut ClientPort,
|
||||
cancellation: &CancellationToken,
|
||||
revision: RevisionId,
|
||||
pending: PendingRuntimeMessage,
|
||||
) -> std::result::Result<RevisionId, RunOutcome> {
|
||||
match pending {
|
||||
PendingRuntimeMessage::Message(message) => Ok(super::engine::append_runtime_message(
|
||||
store,
|
||||
prepared,
|
||||
client,
|
||||
cancellation,
|
||||
revision,
|
||||
message,
|
||||
)
|
||||
.await?
|
||||
.0),
|
||||
PendingRuntimeMessage::Insertion(insertion) => Ok(super::engine::append_insertions(
|
||||
store,
|
||||
prepared,
|
||||
client,
|
||||
cancellation,
|
||||
revision,
|
||||
vec![insertion],
|
||||
)
|
||||
.await?
|
||||
.0),
|
||||
}
|
||||
}
|
||||
|
||||
async fn send(client: &ClientPort, event: ClientEvent) -> std::result::Result<(), RunOutcome> {
|
||||
client
|
||||
.events
|
||||
|
||||
Reference in new issue
Block a user