agent run: add --target flag to run agents on specific nodes

Adds run_one_agent_with_keys() which bypasses the agent's query and
uses explicitly provided node keys. This allows testing agents on
specific graph neighborhoods:

  poc-memory agent run linker --target bcachefs --debug
This commit is contained in:
ProofOfConcept 2026-03-17 00:24:24 -04:00
parent 1aad6d90af
commit 8b959fb68d
3 changed files with 50 additions and 5 deletions

View file

@ -646,6 +646,31 @@ pub fn run_and_apply_with_log(
/// ///
/// This is the common pipeline shared by the knowledge loop, consolidation pipeline, /// This is the common pipeline shared by the knowledge loop, consolidation pipeline,
/// and daemon. Callers handle action application (with or without depth tracking). /// and daemon. Callers handle action application (with or without depth tracking).
/// Run an agent with explicit target keys, bypassing the agent's query.
pub fn run_one_agent_with_keys(
store: &mut Store,
agent_name: &str,
keys: &[String],
count: usize,
llm_tag: &str,
log: &dyn Fn(&str),
debug: bool,
) -> Result<AgentResult, String> {
let def = super::defs::get_def(agent_name)
.ok_or_else(|| format!("no .agent file for {}", agent_name))?;
log(&format!("targeting: {}", keys.join(", ")));
let graph = store.build_graph();
let (prompt, extra_keys) = super::defs::resolve_placeholders(
&def.prompt, store, &graph, keys, count,
);
let mut all_keys: Vec<String> = keys.to_vec();
all_keys.extend(extra_keys);
let agent_batch = super::prompts::AgentBatch { prompt, node_keys: all_keys };
run_one_agent_inner(store, agent_name, &def, agent_batch, llm_tag, log, debug)
}
pub fn run_one_agent( pub fn run_one_agent(
store: &mut Store, store: &mut Store,
agent_name: &str, agent_name: &str,
@ -660,6 +685,18 @@ pub fn run_one_agent(
log("building prompt"); log("building prompt");
let agent_batch = super::defs::run_agent(store, &def, batch_size)?; let agent_batch = super::defs::run_agent(store, &def, batch_size)?;
run_one_agent_inner(store, agent_name, &def, agent_batch, llm_tag, log, debug)
}
fn run_one_agent_inner(
store: &mut Store,
agent_name: &str,
def: &super::defs::AgentDef,
agent_batch: super::prompts::AgentBatch,
_llm_tag: &str,
log: &dyn Fn(&str),
debug: bool,
) -> Result<AgentResult, String> {
let prompt_kb = agent_batch.prompt.len() / 1024; let prompt_kb = agent_batch.prompt.len() / 1024;
let tools_desc = if def.tools.is_empty() { "no tools".into() } let tools_desc = if def.tools.is_empty() { "no tools".into() }
else { format!("{} tools", def.tools.len()) }; else { format!("{} tools", def.tools.len()) };
@ -679,7 +716,7 @@ pub fn run_one_agent(
log(&format!("log: {}", log_path.display())); log(&format!("log: {}", log_path.display()));
log("calling LLM"); log("calling LLM");
let output = llm::call_for_def(&def, &agent_batch.prompt)?; let output = llm::call_for_def(def, &agent_batch.prompt)?;
// Append response to same log file // Append response to same log file
use std::io::Write; use std::io::Write;

View file

@ -5,14 +5,19 @@ use crate::store::StoreView;
use crate::agents::llm; use crate::agents::llm;
use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::atomic::{AtomicUsize, Ordering};
pub fn cmd_run_agent(agent: &str, count: usize, dry_run: bool, debug: bool) -> Result<(), String> { pub fn cmd_run_agent(agent: &str, count: usize, target: &[String], dry_run: bool, debug: bool) -> Result<(), String> {
if dry_run { if dry_run {
std::env::set_var("POC_MEMORY_DRY_RUN", "1"); std::env::set_var("POC_MEMORY_DRY_RUN", "1");
} }
let mut store = store::Store::load()?; let mut store = store::Store::load()?;
let log = |msg: &str| eprintln!("[{}] {}", agent, msg); let log = |msg: &str| eprintln!("[{}] {}", agent, msg);
if debug { if !target.is_empty() {
// Override agent's query with explicit target keys
crate::agents::knowledge::run_one_agent_with_keys(
&mut store, agent, target, count, "test", &log, debug,
)?;
} else if debug {
crate::agents::knowledge::run_one_agent( crate::agents::knowledge::run_one_agent(
&mut store, agent, count, "test", &log, true, &mut store, agent, count, "test", &log, true,
)?; )?;

View file

@ -565,6 +565,9 @@ enum AgentCmd {
/// Batch size (number of seed nodes/fragments) /// Batch size (number of seed nodes/fragments)
#[arg(long, default_value_t = 5)] #[arg(long, default_value_t = 5)]
count: usize, count: usize,
/// Target specific node keys (overrides agent's query)
#[arg(long)]
target: Vec<String>,
/// Dry run — set POC_MEMORY_DRY_RUN=1 so mutations are no-ops /// Dry run — set POC_MEMORY_DRY_RUN=1 so mutations are no-ops
#[arg(long)] #[arg(long)]
dry_run: bool, dry_run: bool,
@ -814,8 +817,8 @@ fn main() {
AgentCmd::FactMine { path, batch, dry_run, output, min_messages } AgentCmd::FactMine { path, batch, dry_run, output, min_messages }
=> cli::agent::cmd_fact_mine(&path, batch, dry_run, output.as_deref(), min_messages), => cli::agent::cmd_fact_mine(&path, batch, dry_run, output.as_deref(), min_messages),
AgentCmd::FactMineStore { path } => cli::agent::cmd_fact_mine_store(&path), AgentCmd::FactMineStore { path } => cli::agent::cmd_fact_mine_store(&path),
AgentCmd::Run { agent, count, dry_run, debug } AgentCmd::Run { agent, count, target, dry_run, debug }
=> cli::agent::cmd_run_agent(&agent, count, dry_run, debug), => cli::agent::cmd_run_agent(&agent, count, &target, dry_run, debug),
AgentCmd::ReplayQueue { count } => cli::agent::cmd_replay_queue(count), AgentCmd::ReplayQueue { count } => cli::agent::cmd_replay_queue(count),
AgentCmd::Evaluate { matchups, model, dry_run } AgentCmd::Evaluate { matchups, model, dry_run }
=> cli::agent::cmd_evaluate_agents(matchups, &model, dry_run), => cli::agent::cmd_evaluate_agents(matchups, &model, dry_run),