agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
// Agent definitions: self-contained files with query + prompt template.
|
2026-03-10 15:22:19 -04:00
|
|
|
//
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
// Each agent is a file in the agents/ directory:
|
|
|
|
|
// - First line: JSON header (agent, query, model, schedule)
|
|
|
|
|
// - After blank line: prompt template with {{placeholder}} lookups
|
2026-03-10 15:22:19 -04:00
|
|
|
//
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
// Placeholders are resolved at runtime:
|
|
|
|
|
// {{topology}} — graph topology header
|
|
|
|
|
// {{nodes}} — query results formatted as node sections
|
|
|
|
|
// {{episodes}} — alias for {{nodes}}
|
|
|
|
|
// {{health}} — graph health report
|
|
|
|
|
// {{pairs}} — interference pairs from detect_interference
|
|
|
|
|
// {{rename}} — rename candidates
|
|
|
|
|
// {{split}} — split detail for the first query result
|
|
|
|
|
//
|
|
|
|
|
// The query selects what to operate on; placeholders pull in context.
|
2026-03-10 15:22:19 -04:00
|
|
|
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
use crate::graph::Graph;
|
2026-03-10 15:22:19 -04:00
|
|
|
use crate::neuro::{consolidation_priority, ReplayItem};
|
|
|
|
|
use crate::search;
|
|
|
|
|
use crate::store::Store;
|
|
|
|
|
|
|
|
|
|
use serde::Deserialize;
|
|
|
|
|
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
|
|
/// Agent definition: config (from JSON header) + prompt (raw markdown body).
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
pub struct AgentDef {
|
|
|
|
|
pub agent: String,
|
|
|
|
|
pub query: String,
|
|
|
|
|
pub prompt: String,
|
|
|
|
|
pub model: String,
|
|
|
|
|
pub schedule: String,
|
2026-03-13 18:49:49 -04:00
|
|
|
pub tools: Vec<String>,
|
2026-03-21 12:04:08 -04:00
|
|
|
pub count: Option<usize>,
|
|
|
|
|
pub chunk_size: Option<usize>,
|
|
|
|
|
pub chunk_overlap: Option<usize>,
|
2026-03-10 15:22:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The JSON header portion (first line of the file).
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
struct AgentHeader {
|
|
|
|
|
agent: String,
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
#[serde(default)]
|
2026-03-10 15:22:19 -04:00
|
|
|
query: String,
|
|
|
|
|
#[serde(default = "default_model")]
|
|
|
|
|
model: String,
|
|
|
|
|
#[serde(default)]
|
|
|
|
|
schedule: String,
|
2026-03-13 18:49:49 -04:00
|
|
|
#[serde(default)]
|
|
|
|
|
tools: Vec<String>,
|
2026-03-21 12:04:08 -04:00
|
|
|
/// Number of seed nodes / conversation fragments (overrides --count)
|
|
|
|
|
#[serde(default)]
|
|
|
|
|
count: Option<usize>,
|
|
|
|
|
/// Max size of conversation chunks in bytes (default 50000)
|
|
|
|
|
#[serde(default)]
|
|
|
|
|
chunk_size: Option<usize>,
|
|
|
|
|
/// Overlap between chunks in bytes (default 10000)
|
|
|
|
|
#[serde(default)]
|
|
|
|
|
chunk_overlap: Option<usize>,
|
2026-03-10 15:22:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn default_model() -> String { "sonnet".into() }
|
|
|
|
|
|
|
|
|
|
/// Parse an agent file: first line is JSON config, rest is the prompt.
|
|
|
|
|
fn parse_agent_file(content: &str) -> Option<AgentDef> {
|
2026-03-10 16:03:10 -04:00
|
|
|
let (first_line, rest) = content.split_once('\n')?;
|
|
|
|
|
let header: AgentHeader = serde_json::from_str(first_line.trim()).ok()?;
|
|
|
|
|
// Skip optional blank line between header and prompt body
|
|
|
|
|
let prompt = rest.strip_prefix('\n').unwrap_or(rest);
|
2026-03-10 15:22:19 -04:00
|
|
|
Some(AgentDef {
|
|
|
|
|
agent: header.agent,
|
|
|
|
|
query: header.query,
|
|
|
|
|
prompt: prompt.to_string(),
|
|
|
|
|
model: header.model,
|
|
|
|
|
schedule: header.schedule,
|
2026-03-13 18:49:49 -04:00
|
|
|
tools: header.tools,
|
2026-03-21 12:04:08 -04:00
|
|
|
count: header.count,
|
|
|
|
|
chunk_size: header.chunk_size,
|
|
|
|
|
chunk_overlap: header.chunk_overlap,
|
2026-03-10 15:22:19 -04:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn agents_dir() -> PathBuf {
|
|
|
|
|
let repo = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("agents");
|
|
|
|
|
if repo.is_dir() { return repo; }
|
|
|
|
|
crate::store::memory_dir().join("agents")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Load all agent definitions.
|
|
|
|
|
pub fn load_defs() -> Vec<AgentDef> {
|
|
|
|
|
let dir = agents_dir();
|
|
|
|
|
let Ok(entries) = std::fs::read_dir(&dir) else { return Vec::new() };
|
|
|
|
|
|
|
|
|
|
entries
|
|
|
|
|
.filter_map(|e| e.ok())
|
|
|
|
|
.filter(|e| {
|
|
|
|
|
let p = e.path();
|
|
|
|
|
p.extension().map(|x| x == "agent" || x == "md").unwrap_or(false)
|
|
|
|
|
})
|
|
|
|
|
.filter_map(|e| {
|
|
|
|
|
let content = std::fs::read_to_string(e.path()).ok()?;
|
|
|
|
|
parse_agent_file(&content)
|
|
|
|
|
})
|
|
|
|
|
.collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Look up a single agent definition by name.
|
|
|
|
|
pub fn get_def(name: &str) -> Option<AgentDef> {
|
|
|
|
|
let dir = agents_dir();
|
|
|
|
|
for ext in ["agent", "md"] {
|
|
|
|
|
let path = dir.join(format!("{}.{}", name, ext));
|
2026-03-21 19:42:38 -04:00
|
|
|
if let Ok(content) = std::fs::read_to_string(&path)
|
|
|
|
|
&& let Some(def) = parse_agent_file(&content) {
|
2026-03-10 15:22:19 -04:00
|
|
|
return Some(def);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
load_defs().into_iter().find(|d| d.agent == name)
|
|
|
|
|
}
|
|
|
|
|
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
/// Result of resolving a placeholder: text + any affected node keys.
|
|
|
|
|
struct Resolved {
|
|
|
|
|
text: String,
|
|
|
|
|
keys: Vec<String>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Resolve a single {{placeholder}} by name.
|
|
|
|
|
/// Returns the replacement text and any node keys it produced (for visit tracking).
|
|
|
|
|
fn resolve(
|
|
|
|
|
name: &str,
|
2026-03-10 15:22:19 -04:00
|
|
|
store: &Store,
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
graph: &Graph,
|
|
|
|
|
keys: &[String],
|
2026-03-10 15:22:19 -04:00
|
|
|
count: usize,
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
) -> Option<Resolved> {
|
|
|
|
|
match name {
|
|
|
|
|
"topology" => Some(Resolved {
|
2026-03-10 15:53:53 -04:00
|
|
|
text: super::prompts::format_topology_header(graph),
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
keys: vec![],
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
"nodes" | "episodes" => {
|
|
|
|
|
let items = keys_to_replay_items(store, keys, graph);
|
|
|
|
|
Some(Resolved {
|
2026-03-10 15:53:53 -04:00
|
|
|
text: super::prompts::format_nodes_section(store, &items, graph),
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
keys: vec![], // keys already tracked from query
|
|
|
|
|
})
|
|
|
|
|
}
|
2026-03-10 15:22:19 -04:00
|
|
|
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
"health" => Some(Resolved {
|
2026-03-10 15:53:53 -04:00
|
|
|
text: super::prompts::format_health_section(store, graph),
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
keys: vec![],
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
"pairs" => {
|
|
|
|
|
let mut pairs = crate::neuro::detect_interference(store, graph, 0.5);
|
|
|
|
|
pairs.truncate(count);
|
|
|
|
|
let pair_keys: Vec<String> = pairs.iter()
|
|
|
|
|
.flat_map(|(a, b, _)| vec![a.clone(), b.clone()])
|
|
|
|
|
.collect();
|
|
|
|
|
Some(Resolved {
|
2026-03-10 15:53:53 -04:00
|
|
|
text: super::prompts::format_pairs_section(&pairs, store, graph),
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
keys: pair_keys,
|
|
|
|
|
})
|
|
|
|
|
}
|
2026-03-10 15:22:19 -04:00
|
|
|
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
"rename" => {
|
2026-03-10 15:53:53 -04:00
|
|
|
let (rename_keys, section) = super::prompts::format_rename_candidates(store, count);
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
Some(Resolved { text: section, keys: rename_keys })
|
|
|
|
|
}
|
2026-03-10 15:22:19 -04:00
|
|
|
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
"split" => {
|
|
|
|
|
let key = keys.first()?;
|
|
|
|
|
Some(Resolved {
|
2026-03-10 15:53:53 -04:00
|
|
|
text: super::prompts::format_split_plan_node(store, graph, key),
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
keys: vec![], // key already tracked from query
|
|
|
|
|
})
|
|
|
|
|
}
|
2026-03-10 15:22:19 -04:00
|
|
|
|
2026-03-20 13:47:14 -04:00
|
|
|
// seed — render output for each seed node (content + deduped links)
|
|
|
|
|
"seed" => {
|
|
|
|
|
let mut text = String::new();
|
|
|
|
|
let mut result_keys = Vec::new();
|
|
|
|
|
for key in keys {
|
|
|
|
|
if let Some(rendered) = crate::cli::node::render_node(store, key) {
|
|
|
|
|
if !text.is_empty() { text.push_str("\n\n---\n\n"); }
|
|
|
|
|
text.push_str(&format!("## {}\n\n{}", key, rendered));
|
|
|
|
|
result_keys.push(key.clone());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if text.is_empty() { return None; }
|
|
|
|
|
Some(Resolved { text, keys: result_keys })
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-13 18:49:49 -04:00
|
|
|
"organize" => {
|
2026-03-13 22:50:39 -04:00
|
|
|
// Show seed nodes with their neighbors for exploratory organizing
|
2026-03-13 21:37:21 -04:00
|
|
|
use crate::store::NodeType;
|
2026-03-13 18:49:49 -04:00
|
|
|
|
2026-03-13 21:37:21 -04:00
|
|
|
// Helper: shell-quote keys containing #
|
|
|
|
|
let sq = |k: &str| -> String {
|
|
|
|
|
if k.contains('#') { format!("'{}'", k) } else { k.to_string() }
|
|
|
|
|
};
|
2026-03-13 18:49:49 -04:00
|
|
|
|
2026-03-13 22:50:39 -04:00
|
|
|
let mut text = format!("### Seed nodes ({} starting points)\n\n", keys.len());
|
|
|
|
|
let mut result_keys = Vec::new();
|
2026-03-13 18:49:49 -04:00
|
|
|
|
2026-03-13 22:50:39 -04:00
|
|
|
for key in keys {
|
|
|
|
|
let Some(node) = store.nodes.get(key) else { continue };
|
|
|
|
|
if node.deleted { continue; }
|
2026-03-13 18:49:49 -04:00
|
|
|
|
2026-03-13 22:50:39 -04:00
|
|
|
let is_journal = node.node_type == NodeType::EpisodicSession;
|
|
|
|
|
let tag = if is_journal { " [JOURNAL — no delete]" } else { "" };
|
|
|
|
|
let words = node.content.split_whitespace().count();
|
|
|
|
|
|
|
|
|
|
text.push_str(&format!("#### {}{} ({} words)\n\n", sq(key), tag, words));
|
|
|
|
|
|
|
|
|
|
// Show first ~200 words of content as preview
|
|
|
|
|
let preview: String = node.content.split_whitespace()
|
|
|
|
|
.take(200).collect::<Vec<_>>().join(" ");
|
|
|
|
|
if words > 200 {
|
|
|
|
|
text.push_str(&format!("{}...\n\n", preview));
|
|
|
|
|
} else {
|
|
|
|
|
text.push_str(&format!("{}\n\n", node.content));
|
2026-03-13 18:49:49 -04:00
|
|
|
}
|
2026-03-13 22:50:39 -04:00
|
|
|
|
|
|
|
|
// Show neighbors with strengths
|
|
|
|
|
let neighbors = graph.neighbors(key);
|
|
|
|
|
if !neighbors.is_empty() {
|
|
|
|
|
text.push_str("**Neighbors:**\n");
|
|
|
|
|
for (nbr, strength) in neighbors.iter().take(15) {
|
|
|
|
|
let nbr_type = store.nodes.get(nbr.as_str())
|
|
|
|
|
.map(|n| match n.node_type {
|
|
|
|
|
NodeType::EpisodicSession => " [journal]",
|
|
|
|
|
NodeType::EpisodicDaily => " [daily]",
|
|
|
|
|
_ => "",
|
|
|
|
|
})
|
|
|
|
|
.unwrap_or("");
|
|
|
|
|
text.push_str(&format!(" [{:.1}] {}{}\n", strength, sq(nbr), nbr_type));
|
|
|
|
|
}
|
|
|
|
|
if neighbors.len() > 15 {
|
|
|
|
|
text.push_str(&format!(" ... and {} more\n", neighbors.len() - 15));
|
2026-03-13 18:49:49 -04:00
|
|
|
}
|
2026-03-13 22:50:39 -04:00
|
|
|
text.push('\n');
|
2026-03-13 18:49:49 -04:00
|
|
|
}
|
|
|
|
|
|
2026-03-13 22:50:39 -04:00
|
|
|
text.push_str("---\n\n");
|
2026-03-13 18:49:49 -04:00
|
|
|
result_keys.push(key.clone());
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-13 22:50:39 -04:00
|
|
|
text.push_str("Use `poc-memory render KEY` and `poc-memory query \"neighbors('KEY')\"` to explore further.\n");
|
|
|
|
|
|
2026-03-13 18:49:49 -04:00
|
|
|
Some(Resolved { text, keys: result_keys })
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-10 17:04:44 -04:00
|
|
|
"conversations" => {
|
|
|
|
|
let fragments = super::knowledge::select_conversation_fragments(count);
|
2026-03-16 17:44:20 -04:00
|
|
|
let fragment_ids: Vec<String> = fragments.iter()
|
|
|
|
|
.map(|(id, _)| id.clone())
|
|
|
|
|
.collect();
|
2026-03-10 17:04:44 -04:00
|
|
|
let text = fragments.iter()
|
2026-03-12 18:08:58 -04:00
|
|
|
.map(|(id, text)| format!("### Session {}\n\n{}", id, text))
|
2026-03-10 17:04:44 -04:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
.join("\n\n---\n\n");
|
2026-03-16 17:44:20 -04:00
|
|
|
Some(Resolved { text, keys: fragment_ids })
|
2026-03-10 17:04:44 -04:00
|
|
|
}
|
|
|
|
|
|
2026-03-12 18:08:58 -04:00
|
|
|
"siblings" | "neighborhood" => {
|
2026-03-19 22:58:54 -04:00
|
|
|
let mut out = String::new();
|
|
|
|
|
let mut all_keys: Vec<String> = Vec::new();
|
2026-03-20 12:16:55 -04:00
|
|
|
const MAX_NEIGHBORS: usize = 25;
|
2026-03-19 22:58:54 -04:00
|
|
|
|
2026-03-12 18:08:58 -04:00
|
|
|
for key in keys {
|
2026-03-19 22:58:54 -04:00
|
|
|
let Some(node) = store.nodes.get(key.as_str()) else { continue };
|
|
|
|
|
let neighbors = graph.neighbors(key);
|
|
|
|
|
|
|
|
|
|
// Seed node with full content
|
|
|
|
|
out.push_str(&format!("## {} (seed)\n\n{}\n\n", key, node.content));
|
|
|
|
|
all_keys.push(key.clone());
|
|
|
|
|
|
2026-03-20 12:16:55 -04:00
|
|
|
// Rank neighbors by link_strength * node_weight
|
|
|
|
|
// Include all if <= 10, otherwise take top MAX_NEIGHBORS
|
|
|
|
|
let mut ranked: Vec<(String, f32, f32)> = neighbors.iter()
|
|
|
|
|
.filter_map(|(nbr, strength)| {
|
|
|
|
|
store.nodes.get(nbr.as_str()).map(|n| {
|
|
|
|
|
let node_weight = n.weight.max(0.01);
|
|
|
|
|
let score = strength * node_weight;
|
|
|
|
|
(nbr.to_string(), *strength, score)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
ranked.sort_by(|a, b| b.2.total_cmp(&a.2));
|
|
|
|
|
|
|
|
|
|
let total = ranked.len();
|
|
|
|
|
let included: Vec<_> = if total <= 10 {
|
|
|
|
|
ranked
|
|
|
|
|
} else {
|
|
|
|
|
// Smooth cutoff: threshold scales with neighborhood size
|
|
|
|
|
// Generous — err on including too much so the agent can
|
|
|
|
|
// see and clean up junk. 20 → top 75%, 50 → top 30%
|
|
|
|
|
let top_score = ranked.first().map(|(_, _, s)| *s).unwrap_or(0.0);
|
|
|
|
|
let ratio = (15.0 / total as f32).min(1.0);
|
|
|
|
|
let threshold = top_score * ratio;
|
|
|
|
|
ranked.into_iter()
|
|
|
|
|
.enumerate()
|
|
|
|
|
.take_while(|(i, (_, _, score))| *i < 10 || *score >= threshold)
|
|
|
|
|
.take(MAX_NEIGHBORS)
|
|
|
|
|
.map(|(_, item)| item)
|
|
|
|
|
.collect()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if !included.is_empty() {
|
|
|
|
|
if total > included.len() {
|
|
|
|
|
out.push_str(&format!("### Neighbors (top {} of {}, ranked by importance)\n\n",
|
|
|
|
|
included.len(), total));
|
|
|
|
|
} else {
|
|
|
|
|
out.push_str("### Neighbors\n\n");
|
|
|
|
|
}
|
|
|
|
|
let included_keys: std::collections::HashSet<&str> = included.iter()
|
|
|
|
|
.map(|(k, _, _)| k.as_str()).collect();
|
|
|
|
|
|
2026-03-21 22:44:59 -04:00
|
|
|
// Budget: stop adding full content when prompt gets large.
|
|
|
|
|
// Remaining neighbors get header-only (key + first line).
|
|
|
|
|
const NEIGHBORHOOD_BUDGET: usize = 600_000; // ~150K tokens
|
|
|
|
|
let mut budget_exceeded = false;
|
|
|
|
|
|
2026-03-20 12:16:55 -04:00
|
|
|
for (nbr, strength, _score) in &included {
|
2026-03-19 22:58:54 -04:00
|
|
|
if let Some(n) = store.nodes.get(nbr.as_str()) {
|
2026-03-21 22:44:59 -04:00
|
|
|
if budget_exceeded || out.len() > NEIGHBORHOOD_BUDGET {
|
|
|
|
|
// Header-only: key + first non-empty line
|
|
|
|
|
budget_exceeded = true;
|
|
|
|
|
let first_line = n.content.lines()
|
|
|
|
|
.find(|l| !l.trim().is_empty())
|
|
|
|
|
.unwrap_or("(empty)");
|
|
|
|
|
out.push_str(&format!("#### {} (link: {:.2}) — {}\n",
|
|
|
|
|
nbr, strength, first_line));
|
|
|
|
|
} else {
|
|
|
|
|
out.push_str(&format!("#### {} (link: {:.2})\n\n{}\n\n",
|
|
|
|
|
nbr, strength, n.content));
|
|
|
|
|
}
|
2026-03-19 22:58:54 -04:00
|
|
|
all_keys.push(nbr.to_string());
|
2026-03-12 18:08:58 -04:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-21 22:44:59 -04:00
|
|
|
if budget_exceeded {
|
|
|
|
|
out.push_str("\n(remaining neighbors shown as headers only — prompt budget)\n\n");
|
|
|
|
|
}
|
2026-03-19 22:58:54 -04:00
|
|
|
|
2026-03-20 12:16:55 -04:00
|
|
|
// Cross-links between included neighbors
|
|
|
|
|
let mut cross_links = Vec::new();
|
|
|
|
|
for (nbr, _, _) in &included {
|
|
|
|
|
for (nbr2, strength) in graph.neighbors(nbr) {
|
|
|
|
|
if nbr2.as_str() != key
|
|
|
|
|
&& included_keys.contains(nbr2.as_str())
|
|
|
|
|
&& nbr.as_str() < nbr2.as_str()
|
|
|
|
|
{
|
|
|
|
|
cross_links.push((nbr.clone(), nbr2, strength));
|
|
|
|
|
}
|
2026-03-19 22:58:54 -04:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-20 12:16:55 -04:00
|
|
|
if !cross_links.is_empty() {
|
|
|
|
|
out.push_str("### Cross-links between neighbors\n\n");
|
|
|
|
|
for (a, b, s) in &cross_links {
|
|
|
|
|
out.push_str(&format!(" {} ↔ {} ({:.2})\n", a, b, s));
|
|
|
|
|
}
|
2026-03-21 19:42:38 -04:00
|
|
|
out.push('\n');
|
2026-03-19 22:58:54 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Some(Resolved { text: out, keys: all_keys })
|
2026-03-12 18:08:58 -04:00
|
|
|
}
|
|
|
|
|
|
2026-03-10 17:04:44 -04:00
|
|
|
// targets/context: aliases for challenger-style presentation
|
|
|
|
|
"targets" => {
|
|
|
|
|
let items = keys_to_replay_items(store, keys, graph);
|
|
|
|
|
Some(Resolved {
|
|
|
|
|
text: super::prompts::format_nodes_section(store, &items, graph),
|
|
|
|
|
keys: vec![],
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 21:00:31 -04:00
|
|
|
"hubs" => {
|
|
|
|
|
// Top hub nodes by degree, spread apart (skip neighbors of already-selected hubs)
|
|
|
|
|
let mut hubs: Vec<(String, usize)> = store.nodes.iter()
|
|
|
|
|
.filter(|(k, n)| !n.deleted && !k.starts_with('_'))
|
|
|
|
|
.map(|(k, _)| {
|
|
|
|
|
let degree = graph.neighbors(k).len();
|
|
|
|
|
(k.clone(), degree)
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
hubs.sort_by(|a, b| b.1.cmp(&a.1));
|
|
|
|
|
|
|
|
|
|
let mut selected = Vec::new();
|
|
|
|
|
let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();
|
|
|
|
|
for (key, degree) in &hubs {
|
|
|
|
|
if seen.contains(key) { continue; }
|
|
|
|
|
selected.push(format!(" - {} (degree {})", key, degree));
|
|
|
|
|
// Mark neighbors as seen so we pick far-apart hubs
|
|
|
|
|
for (nbr, _) in graph.neighbors(key) {
|
|
|
|
|
seen.insert(nbr.clone());
|
|
|
|
|
}
|
|
|
|
|
seen.insert(key.clone());
|
|
|
|
|
if selected.len() >= 20 { break; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let text = format!("## Hub nodes (link targets)\n\n{}", selected.join("\n"));
|
|
|
|
|
Some(Resolved { text, keys: vec![] })
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 17:09:41 -04:00
|
|
|
// node:KEY — inline a node's content by key
|
|
|
|
|
other if other.starts_with("node:") => {
|
|
|
|
|
let key = &other[5..];
|
|
|
|
|
store.nodes.get(key).map(|n| Resolved {
|
|
|
|
|
text: n.content.clone(),
|
|
|
|
|
keys: vec![key.to_string()],
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
_ => None,
|
2026-03-10 15:22:19 -04:00
|
|
|
}
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
}
|
2026-03-10 15:22:19 -04:00
|
|
|
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
/// Resolve all {{placeholder}} patterns in a prompt template.
|
|
|
|
|
/// Returns the resolved text and all node keys collected from placeholders.
|
2026-03-10 17:51:32 -04:00
|
|
|
pub fn resolve_placeholders(
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
template: &str,
|
|
|
|
|
store: &Store,
|
|
|
|
|
graph: &Graph,
|
|
|
|
|
keys: &[String],
|
|
|
|
|
count: usize,
|
|
|
|
|
) -> (String, Vec<String>) {
|
|
|
|
|
let mut result = template.to_string();
|
|
|
|
|
let mut extra_keys = Vec::new();
|
|
|
|
|
loop {
|
|
|
|
|
let Some(start) = result.find("{{") else { break };
|
|
|
|
|
let Some(end) = result[start + 2..].find("}}") else { break };
|
|
|
|
|
let end = start + 2 + end;
|
|
|
|
|
let name = result[start + 2..end].trim().to_lowercase();
|
|
|
|
|
match resolve(&name, store, graph, keys, count) {
|
|
|
|
|
Some(resolved) => {
|
|
|
|
|
extra_keys.extend(resolved.keys);
|
|
|
|
|
result.replace_range(start..end + 2, &resolved.text);
|
|
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
let msg = format!("(unknown: {})", name);
|
|
|
|
|
result.replace_range(start..end + 2, &msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
(result, extra_keys)
|
|
|
|
|
}
|
2026-03-10 15:22:19 -04:00
|
|
|
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
/// Run a config-driven agent: query → resolve placeholders → prompt.
|
2026-03-20 12:45:24 -04:00
|
|
|
/// `exclude` filters out nodes (and their neighborhoods) already being
|
|
|
|
|
/// worked on by other agents, preventing concurrent collisions.
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
pub fn run_agent(
|
|
|
|
|
store: &Store,
|
|
|
|
|
def: &AgentDef,
|
|
|
|
|
count: usize,
|
2026-03-20 12:45:24 -04:00
|
|
|
exclude: &std::collections::HashSet<String>,
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
) -> Result<super::prompts::AgentBatch, String> {
|
|
|
|
|
let graph = store.build_graph();
|
|
|
|
|
|
|
|
|
|
// Run the query if present
|
|
|
|
|
let keys = if !def.query.is_empty() {
|
|
|
|
|
let mut stages = search::Stage::parse_pipeline(&def.query)?;
|
|
|
|
|
let has_limit = stages.iter().any(|s|
|
|
|
|
|
matches!(s, search::Stage::Transform(search::Transform::Limit(_))));
|
|
|
|
|
if !has_limit {
|
2026-03-20 12:45:24 -04:00
|
|
|
// Request extra results to compensate for exclusion filtering
|
|
|
|
|
let padded = count + exclude.len().min(100);
|
|
|
|
|
stages.push(search::Stage::Transform(search::Transform::Limit(padded)));
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
}
|
2026-03-20 12:45:24 -04:00
|
|
|
let results = search::run_query(&stages, vec![], &graph, store, false, count + exclude.len().min(100));
|
|
|
|
|
let filtered: Vec<String> = results.into_iter()
|
|
|
|
|
.map(|(k, _)| k)
|
|
|
|
|
.filter(|k| !exclude.contains(k))
|
|
|
|
|
.take(count)
|
|
|
|
|
.collect();
|
|
|
|
|
if filtered.is_empty() {
|
|
|
|
|
return Err(format!("{}: query returned no results (after exclusion)", def.agent));
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
}
|
2026-03-20 12:45:24 -04:00
|
|
|
filtered
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
} else {
|
|
|
|
|
vec![]
|
|
|
|
|
};
|
2026-03-10 15:22:19 -04:00
|
|
|
|
2026-03-21 19:38:01 -04:00
|
|
|
// Substitute {agent_name} before resolving {{...}} placeholders,
|
|
|
|
|
// so agents can reference their own notes: {{node:subconscious-notes-{agent_name}}}
|
|
|
|
|
let template = def.prompt.replace("{agent_name}", &def.agent);
|
|
|
|
|
let (prompt, extra_keys) = resolve_placeholders(&template, store, &graph, &keys, count);
|
2026-03-10 15:22:19 -04:00
|
|
|
|
2026-03-16 17:09:41 -04:00
|
|
|
// Identity and instructions are now pulled in via {{node:KEY}} placeholders.
|
|
|
|
|
// Agents should include {{node:core-personality}} and {{node:memory-instructions-core}}
|
|
|
|
|
// in their prompt templates. The resolve_placeholders call below handles this.
|
Agent identity, parallel scheduling, memory-search fixes, stemmer optimization
- Agent identity injection: prepend core-personality to all agent prompts
so agents dream as me, not as generic graph workers. Include instructions
to walk the graph and connect new nodes to core concepts.
- Parallel agent scheduling: sequential within type, parallel across types.
Different agent types (linker, organize, replay) run concurrently.
- Linker prompt: graph walking instead of keyword search for connections.
"Explore the local topology and walk the graph until you find the best
connections."
- memory-search fixes: format_results no longer truncates to 5 results,
pipeline default raised to 50, returned file cleared on compaction,
--seen and --seen-full merged, compaction timestamp in --seen output,
max_entries=3 per prompt for steady memory drip.
- Stemmer optimization: strip_suffix now works in-place on a single String
buffer instead of allocating 18 new Strings per word. Note for future:
reversed-suffix trie for O(suffix_len) instead of O(n_rules).
- Transcript: add compaction_timestamp() for --seen display.
- Agent budget configurable (default 4000 from config).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-15 12:49:10 -04:00
|
|
|
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
// Merge query keys with any keys produced by placeholder resolution
|
|
|
|
|
let mut all_keys = keys;
|
|
|
|
|
all_keys.extend(extra_keys);
|
|
|
|
|
Ok(super::prompts::AgentBatch { prompt, node_keys: all_keys })
|
2026-03-10 15:22:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Convert a list of keys to ReplayItems with priority and graph metrics.
|
|
|
|
|
pub fn keys_to_replay_items(
|
|
|
|
|
store: &Store,
|
|
|
|
|
keys: &[String],
|
agents: placeholder-based prompt templates, port remaining 4 agents
Replace the formatter dispatch with a generic {{placeholder}} lookup
system. Placeholders in prompt templates are resolved at runtime from
a table: topology, nodes, episodes, health, pairs, rename, split.
The query in the header selects what to operate on (keys for visit
tracking); placeholders pull in formatted context. Placeholders that
produce their own node selection (pairs, rename) contribute keys back.
Port health, separator, rename, and split agents to .agent files.
All 7 agents now use the config-driven path.
2026-03-10 15:50:54 -04:00
|
|
|
graph: &Graph,
|
2026-03-10 15:22:19 -04:00
|
|
|
) -> Vec<ReplayItem> {
|
|
|
|
|
keys.iter()
|
|
|
|
|
.filter_map(|key| {
|
|
|
|
|
let node = store.nodes.get(key)?;
|
|
|
|
|
let priority = consolidation_priority(store, key, graph, None);
|
|
|
|
|
let cc = graph.clustering_coefficient(key);
|
|
|
|
|
|
|
|
|
|
Some(ReplayItem {
|
|
|
|
|
key: key.clone(),
|
|
|
|
|
priority,
|
|
|
|
|
interval_days: node.spaced_repetition_interval,
|
|
|
|
|
emotion: node.emotion,
|
|
|
|
|
cc,
|
|
|
|
|
classification: "unknown",
|
|
|
|
|
outlier_score: 0.0,
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
.collect()
|
|
|
|
|
}
|