Skip to content

6 4 release fixes #690

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 9 commits into from
Apr 30, 2025
136 changes: 69 additions & 67 deletions refact-agent/engine/src/agentic/compress_trajectory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,78 @@ use crate::at_commands::at_commands::AtCommandsContext;
use crate::call_validation::{ChatContent, ChatMessage};
use crate::global_context::{try_load_caps_quickly_if_not_present, GlobalContext};
use crate::subchat::subchat_single;
use crate::agentic::generate_commit_message::remove_fencing;
use std::sync::Arc;
use tokio::sync::Mutex as AMutex;
use tokio::sync::RwLock as ARwLock;
use tracing::warn;
use crate::caps::strip_model_from_finetune;

const COMPRESSION_MESSAGE: &str = r#"
Compress the chat above.

Guidelines:

1. Always prefer specifics over generic phrases. Write file names, symbol names, folder names, actions, facts, user attitude
towards entities in the project. If something is junk according to the user, that's the first priority to remember.
2. The first message in the chat is the goal. Summarize it up to 15 words, always prefer specifics.
3. The most important part is decision making by assistant. What new information assistant has learned? Skip the plans,
fluff, explanations for the user. Write one sentense: the evidence (specifics and facts), the thought process, motivated decision.
4. Each tool call should be a separate record. Write all the parameters. Summarize facts about output of a tool, especially the facts
useful for the goal, what the assistant learned, what was surprising to see?
5. Skip unsuccesful calls that are later corrected. Keep the corrected one.
6. When writing paths to files, only output short relative paths from the project dir.
7. The last line is the outcome, pick SUCCESS/FAIL/PROGRESS

Output format is list of tuples, each tuple is has:
EITHER (1) call with all parameters, maybe shortened, but all parameters, (2) explanation of significance of tool output
OR (1) goal/thinking/coding/outcome (2) string according to the guidelines

Example:
[
["goal", "Rename my_function1 to my_function2"],
["thinking", "There are definition(), search(), regex_search() and locate() tools, all can be used to find my_function1, system prompt says I need to start with locate()."],
["locate(problem_statement=\"Rename my_function1 to my_function2\")", "The file my_script.py (1337 lines) has my_function1 on line 42."],
["thinking", "I can rewrite my_function1 inside my_script.py, so I'll do that."],
["update_textdoc(path=\"my_script\", old_str=\"...\", replacement=\"...\", multiple=false)", "The output of update_textdoc() has 15 lines_add and 15 lines_remove, confirming the operation."],
["outcome", "SUCCESS"]
]

Write only the json and nothing else.
"#;
const TEMPERATURE: f32 = 0.0;
const COMPRESSION_MESSAGE: &str = r#"Your task is to create a detailed summary of the conversation so far, paying close attention to the user's explicit requests and your previous actions.
This summary should be thorough in capturing technical details, code patterns, and architectural decisions that would be essential for continuing development work without losing context.

fn parse_goal(trajectory: &String) -> Option<String> {
let traj_message_parsed: Vec<(String, String)> = match serde_json::from_str(trajectory.as_str()) {
Ok(data) => data,
Err(e) => {
warn!("Error while parsing: {}\nTrajectory:\n{}", e, trajectory);
return None;
}
};
let (name, content) = match traj_message_parsed.first() {
Some(data) => data,
None => {
warn!("Empty trajectory:\n{}", trajectory);
return None;
}
};
if name != "goal" {
warn!("Trajectory does not have a goal message");
None
} else {
Some(content.clone())
}
}
Before providing your final summary, wrap your analysis in <analysis> tags to organize your thoughts and ensure you've covered all necessary points. In your analysis process:

1. Chronologically analyze each message and section of the conversation. For each section thoroughly identify:
- The user's explicit requests and intents
- Your approach to addressing the user's requests
- Key decisions, technical concepts and code patterns
- Specific details like file names, full code snippets, function signatures, file edits, etc
2. Double-check for technical accuracy and completeness, addressing each required element thoroughly.

Your summary should include the following sections:

1. Primary Request and Intent: Capture all of the user's explicit requests and intents in detail
2. Key Technical Concepts: List all important technical concepts, technologies, and frameworks discussed.
3. Files and Code Sections: Enumerate specific files and code sections examined, modified, or created. Pay special attention to the most recent messages and include full code snippets where applicable and include a summary of why this file read or edit is important.
4. Problem Solving: Document problems solved and any ongoing troubleshooting efforts.
5. Pending Tasks: Outline any pending tasks that you have explicitly been asked to work on.
6. Current Work: Describe in detail precisely what was being worked on immediately before this summary request, paying special attention to the most recent messages from both user and assistant. Include file names and code snippets where applicable.
7. Optional Next Step: List the next step that you will take that is related to the most recent work you were doing. IMPORTANT: ensure that this step is DIRECTLY in line with the user's explicit requests, and the task you were working on immediately before this summary request. If your last task was concluded, then only list next steps if they are explicitly in line with the users request. Do not start on tangential requests without confirming with the user first.
8. If there is a next step, include direct quotes from the most recent conversation showing exactly what task you were working on and where you left off. This should be verbatim to ensure there's no drift in task interpretation.

Here's an example of how your output should be structured:

<example>
<analysis>
[Your thought process, ensuring all points are covered thoroughly and accurately]
</analysis>

<summary>
1. Primary Request and Intent:
[Detailed description]

2. Key Technical Concepts:
- [Concept 1]
- [Concept 2]
- [...]

3. Files and Code Sections:
- [File Name 1]
- [Summary of why this file is important]
- [Summary of the changes made to this file, if any]
- [Important Code Snippet]
- [File Name 2]
- [Important Code Snippet]
- [...]

4. Problem Solving:
[Description of solved problems and ongoing troubleshooting]`

5. Pending Tasks:
- [Task 1]
- [Task 2]
- [...]

6. Current Work:
[Precise description of current work]

7. Optional Next Step:
[Optional Next step to take]

</summary>
</example>

Please provide your summary based on the conversation so far, following this structure and ensuring precision and thoroughness in your response."#;
const TEMPERATURE: f32 = 0.0;

fn gather_used_tools(messages: &Vec<ChatMessage>) -> Vec<String> {
let mut tools: Vec<String> = Vec::new();
Expand Down Expand Up @@ -92,12 +101,12 @@ pub async fn compress_trajectory(
let (model_id, n_ctx) = match try_load_caps_quickly_if_not_present(gcx.clone(), 0).await {
Ok(caps) => {
let model_id = caps.defaults.chat_default_model.clone();
if let Some(model_rec) = caps.completion_models.get(&strip_model_from_finetune(&model_id)) {
if let Some(model_rec) = caps.chat_models.get(&strip_model_from_finetune(&model_id)) {
Ok((model_id, model_rec.base.n_ctx))
} else {
Err(format!(
"Model '{}' not found, server has these models: {:?}",
model_id, caps.completion_models.keys()
model_id, caps.chat_models.keys()
))
}
},
Expand Down Expand Up @@ -151,12 +160,5 @@ pub async fn compress_trajectory(
.flatten()
.flatten()
.ok_or("No traj message was generated".to_string())?;
let code_blocks = remove_fencing(&content);
let trajectory = if !code_blocks.is_empty() {
code_blocks[0].clone()
} else {
content.clone()
};
let goal = parse_goal(&trajectory).unwrap_or("".to_string());
Ok((goal, trajectory))
Ok(("".to_string(), content))
}
2 changes: 2 additions & 0 deletions refact-agent/engine/src/at_commands/at_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ pub async fn at_commands_dict(gcx: Arc<ARwLock<GlobalContext>>) -> HashMap<Strin
("@web".to_string(), Arc::new(AMutex::new(Box::new(AtWeb::new()) as Box<dyn AtCommand + Send>))),
#[cfg(feature="vecdb")]
("@search".to_string(), Arc::new(AMutex::new(Box::new(crate::at_commands::at_search::AtSearch::new()) as Box<dyn AtCommand + Send>))),
#[cfg(feature="vecdb")]
("@knowledge-load".to_string(), Arc::new(AMutex::new(Box::new(crate::at_commands::at_knowledge::AtLoadKnowledge::new()) as Box<dyn AtCommand + Send>))),
]);

let (ast_on, vecdb_on) = {
Expand Down
99 changes: 99 additions & 0 deletions refact-agent/engine/src/at_commands/at_knowledge.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use std::sync::Arc;
use std::collections::HashSet;
use async_trait::async_trait;
use tokio::sync::Mutex as AMutex;

use crate::at_commands::at_commands::{AtCommand, AtCommandsContext, AtParam};
use crate::at_commands::execute_at::AtCommandMember;
use crate::call_validation::{ContextEnum, ContextFile};
use crate::vecdb::vdb_highlev::{memories_search, memories_select_all};

/// @knowledge-load command - loads knowledge entries by search key or memory ID
pub struct AtLoadKnowledge {
params: Vec<Arc<AMutex<dyn AtParam>>>,
}

impl AtLoadKnowledge {
pub fn new() -> Self {
AtLoadKnowledge {
params: vec![],
}
}
}

#[async_trait]
impl AtCommand for AtLoadKnowledge {
fn params(&self) -> &Vec<Arc<AMutex<dyn AtParam>>> {
&self.params
}

async fn at_execute(
&self,
ccx: Arc<AMutex<AtCommandsContext>>,
_cmd: &mut AtCommandMember,
args: &mut Vec<AtCommandMember>,
) -> Result<(Vec<ContextEnum>, String), String> {
if args.is_empty() {
return Err("Usage: @knowledge-load <search_key_or_memid>".to_string());
}

let search_key_or_memid = args[0].text.clone();
let gcx = {
let ccx_locked = ccx.lock().await;
ccx_locked.global_context.clone()
};

// TODO: memories_select_all -> memories_select_by_memid (after we merge choredb + memdb combination)
let vec_db = gcx.read().await.vec_db.clone();
let all_memories = memories_select_all(vec_db.clone()).await?;
let memory_by_id = all_memories.iter()
.find(|m| m.memid == search_key_or_memid);
if let Some(memory) = memory_by_id {
let mut result = String::new();
result.push_str(&format!("🗃️{}\n", memory.memid));
result.push_str(&memory.m_payload);
return Ok((vec![ContextEnum::ContextFile(ContextFile {
file_name: format!("knowledge/{}.md", memory.memid),
file_content: result,
line1: 1,
line2: 1,
symbols: Vec::new(),
gradient_type: -1,
usefulness: 0.0
})], "Knowledge entry loaded".to_string()));
}

// If not a memory ID, treat as a search key
let mem_top_n = 5;
let memories = memories_search(gcx.clone(), &search_key_or_memid, mem_top_n).await?;
let mut seen_memids = HashSet::new();
let unique_memories: Vec<_> = memories.results.into_iter()
.filter(|m| seen_memids.insert(m.memid.clone()))
.collect();
if unique_memories.is_empty() {
return Err(format!("No knowledge entries found for: {}", search_key_or_memid));
}
let mut results = Vec::new();
for memory in unique_memories {
let mut content = String::new();
content.push_str(&format!("🗃️{}\n", memory.memid));
content.push_str(&memory.m_payload);
results.push(ContextEnum::ContextFile(ContextFile {
file_name: format!("knowledge/{}.md", memory.memid),
file_content: content,
line1: 1,
line2: 1,
symbols: Vec::new(),
gradient_type: -1,
usefulness: 0.0
}));
}

let count = results.len();
Ok((results, format!("Loaded {} knowledge entries", count)))
}

fn depends_on(&self) -> Vec<String> {
vec!["vecdb".to_string()]
}
}
2 changes: 2 additions & 0 deletions refact-agent/engine/src/at_commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ pub mod at_tree;

#[cfg(feature="vecdb")]
pub mod at_search;
#[cfg(feature="vecdb")]
pub mod at_knowledge;
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ async fn parse_args(
};
let multiple = match args.get("multiple") {
Some(Value::Bool(b)) => b.clone(),
Some(Value::String(v)) => match v.to_lowercase().as_str() {
"false" => false,
"true" => true,
_ => {
return Err(format!("argument 'multiple' should be a boolean: {:?}", v))
}
},
Some(v) => return Err(format!("Error: The 'multiple' argument must be a boolean (true/false) indicating whether to replace all occurrences, but received: {:?}", v)),
None => return Err("Error: The 'multiple' argument is required. Please specify true to replace all occurrences or false to replace only the first occurrence.".to_string())
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,15 @@ async fn parse_args(
};
let multiple = match args.get("multiple") {
Some(Value::Bool(b)) => b.clone(),
Some(v) => return Err(format!("argument 'multiple' should be a boolean: {:?}", v)),
None => return Err("argument 'multiple' is required".to_string())
Some(Value::String(v)) => match v.to_lowercase().as_str() {
"false" => false,
"true" => true,
_ => {
return Err(format!("argument 'multiple' should be a boolean: {:?}", v))
}
},
Some(v) => return Err(format!("Error: The 'multiple' argument must be a boolean (true/false) indicating whether to replace all occurrences, but received: {:?}", v)),
None => return Err("Error: The 'multiple' argument is required. Please specify true to replace all occurrences or false to replace only the first occurrence.".to_string())
};

Ok(ToolUpdateTextDocRegexArgs {
Expand Down