mirror of
https://wget.la/https://github.com/leookun/cursor-byok
synced 2026-12-01 09:15:33 +08:00
fix: harden concurrent persistence and task recovery
This commit is contained in:
1 parent
9deb42915c
commit
df053c3720
21 files changed
+1083
-90
No files matched your search
@@ -1,4 +1,4 @@
|
||||
use std::collections::BTreeSet;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use crate::{cursor::proto::agent::v1 as pb, Error, Result};
|
||||
|
||||
@@ -35,8 +35,7 @@ pub(super) fn project(
|
||||
));
|
||||
}
|
||||
|
||||
let mut identities = BTreeSet::new();
|
||||
let mut contexts = Vec::with_capacity(action.completions.len());
|
||||
let mut completions = BTreeMap::new();
|
||||
let mut has_shell = false;
|
||||
let mut has_subagent = false;
|
||||
for completion in &action.completions {
|
||||
@@ -90,15 +89,21 @@ pub(super) fn project(
|
||||
};
|
||||
let identity = agent_id.unwrap_or(&completion.task_id);
|
||||
let identity = format!("{}:{identity}", kind.as_str_name());
|
||||
if !identities.insert(identity.clone()) {
|
||||
let context = completion_context(completion, kind, agent_id)?;
|
||||
if completions
|
||||
.insert(identity.clone(), (completion, context))
|
||||
.is_some()
|
||||
{
|
||||
return Err(Error::Protocol(format!(
|
||||
"duplicate background task completion: {identity}"
|
||||
)));
|
||||
}
|
||||
contexts.push(completion_context(completion, kind, agent_id)?);
|
||||
}
|
||||
|
||||
let first = &action.completions[0];
|
||||
let (first, _) = completions
|
||||
.values()
|
||||
.next()
|
||||
.expect("background completion action was validated as non-empty");
|
||||
let text = match (has_shell, has_subagent) {
|
||||
(true, false) => SHELL_FOLLOW_UP.into(),
|
||||
(false, true) => FOLLOW_UP.into(),
|
||||
@@ -106,12 +111,16 @@ pub(super) fn project(
|
||||
(false, false) => unreachable!(),
|
||||
};
|
||||
Ok(Projection {
|
||||
context: contexts.join("\n\n"),
|
||||
context: completions
|
||||
.values()
|
||||
.map(|(_, context)| context.as_str())
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n\n"),
|
||||
turn_user: pb::UserMessage {
|
||||
text,
|
||||
message_id: format!(
|
||||
"background-completed:{}",
|
||||
identities.into_iter().collect::<Vec<_>>().join(":")
|
||||
completions.keys().cloned().collect::<Vec<_>>().join(":")
|
||||
),
|
||||
mode,
|
||||
is_simulated_msg: Some(true),
|
||||
@@ -272,6 +281,32 @@ mod tests {
|
||||
assert!(projection.turn_user.text.contains(FOLLOW_UP));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completion_batch_projection_is_independent_of_input_order() {
|
||||
let first = completion();
|
||||
let mut second = completion();
|
||||
second.task_id = "child-id-2".into();
|
||||
second.subagent_id = Some("child-id-2".into());
|
||||
second.tool_call_id = Some("task-call-2".into());
|
||||
let forward = project(
|
||||
&pb::BackgroundTaskCompletionAction {
|
||||
completions: vec![first.clone(), second.clone()],
|
||||
},
|
||||
pb::AgentMode::Multitask as i32,
|
||||
)
|
||||
.unwrap();
|
||||
let reversed = project(
|
||||
&pb::BackgroundTaskCompletionAction {
|
||||
completions: vec![second, first],
|
||||
},
|
||||
pb::AgentMode::Multitask as i32,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(forward.turn_user, reversed.turn_user);
|
||||
assert_eq!(forward.context, reversed.context);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completion_requires_the_captured_subagent_identity_and_terminal_reason() {
|
||||
let mut value = completion();
|
||||
|
||||
@@ -181,7 +181,7 @@ pub(crate) async fn prepare(
|
||||
None => proposed_base_revision_id,
|
||||
};
|
||||
let existing_runtime = match event_id.as_deref() {
|
||||
Some(event_id) if !background_completion => {
|
||||
Some(event_id) => {
|
||||
store
|
||||
.message(&conversation_id, &format!("runtime:{event_id}"))
|
||||
.await?
|
||||
@@ -207,14 +207,22 @@ pub(crate) async fn prepare(
|
||||
} else {
|
||||
match (turn_user.clone(), event_id) {
|
||||
(Some(mut user), Some(event_id)) if background_completion => {
|
||||
let (message, text) = runtime::compile_background(
|
||||
event_id,
|
||||
&user,
|
||||
&request_context,
|
||||
&action_context,
|
||||
blob_sync,
|
||||
)
|
||||
.await?;
|
||||
let (message, text) = match existing_runtime {
|
||||
Some(message) => {
|
||||
let text = runtime_message_text(&message)?;
|
||||
(message, text)
|
||||
}
|
||||
None => {
|
||||
runtime::compile_background(
|
||||
event_id,
|
||||
&user,
|
||||
&request_context,
|
||||
&action_context,
|
||||
blob_sync,
|
||||
)
|
||||
.await?
|
||||
}
|
||||
};
|
||||
user.text = text;
|
||||
turn_user = Some(user);
|
||||
vec![message]
|
||||
@@ -306,6 +314,20 @@ pub(crate) async fn prepare(
|
||||
))
|
||||
}
|
||||
|
||||
fn runtime_message_text(message: &CanonicalMessage) -> Result<String> {
|
||||
let MessageContent::Parts { parts } = &message.content else {
|
||||
return Err(Error::Protocol(
|
||||
"stored runtime message does not contain parts".into(),
|
||||
));
|
||||
};
|
||||
let Some(ContentPart::Text { text }) = parts.first() else {
|
||||
return Err(Error::Protocol(
|
||||
"stored runtime message does not start with text".into(),
|
||||
));
|
||||
};
|
||||
Ok(text.clone())
|
||||
}
|
||||
|
||||
fn run_kind(subagent_type_name: Option<&str>, parent: Option<(RunId, String)>) -> Result<RunKind> {
|
||||
match (subagent_type_name, parent) {
|
||||
(None | Some("side-chat"), _) => Ok(RunKind::Root),
|
||||
|
||||
Reference in new issue
Block a user