use std::path::{Path, PathBuf};
use crate::dag::{Node, Plan};
use crate::worker::{ContextPacket, InlinedFile, WorkerFamily};
use crate::{Result, BLACKBOARD_DIR};
use rinne_types::Blackboard;
const COMMON_NAMES: &[&str] = &["new", "run", "get", "set", "build", "main", "init", "from", "into"];
const MIN_IDENT_LEN: usize = 4;
pub fn resolve_symbols(
graph: &dyn rinne_types::graph::CodeGraph,
instruction: &str,
mentioned: &[std::path::PathBuf],
known: &[String],
) -> Vec<String> {
let known_set: std::collections::HashSet<&str> = known.iter().map(|s| s.as_str()).collect();
let mut out: Vec<String> = Vec::new();
for tok in instruction.split(|c: char| !c.is_alphanumeric() && c != '_') {
if tok.len() >= MIN_IDENT_LEN
&& !COMMON_NAMES.contains(&tok)
&& known_set.contains(tok)
&& !out.iter().any(|o| o == tok)
{
out.push(tok.to_string());
}
}
for m in mentioned {
if let Some(stem) = m.file_stem().and_then(|s| s.to_str()) {
if known_set.contains(stem) && !out.iter().any(|o| o == stem) {
out.push(stem.to_string());
}
}
}
let _ = graph;
out
}
pub struct ContextAssembler<'a> {
blackboard: &'a dyn Blackboard,
plan: &'a Plan,
graph: Option<&'a dyn rinne_types::graph::CodeGraph>,
}
impl<'a> ContextAssembler<'a> {
pub fn new(
blackboard: &'a dyn Blackboard,
plan: &'a Plan,
graph: Option<&'a dyn rinne_types::graph::CodeGraph>,
) -> Self {
Self { blackboard, plan, graph }
}
pub fn build(
&self,
node: &Node,
family: WorkerFamily,
critique: Option<String>,
) -> Result<ContextPacket> {
let mentioned = &self.plan.mentioned;
let input_artifacts: Vec<String> = node
.inputs
.iter()
.filter(|i| i.as_str() != "diff") .cloned()
.collect();
let mut packet = ContextPacket {
critique,
..Default::default()
};
match family {
WorkerFamily::Harness => {
for m in mentioned {
packet.pinned_paths.push(m.clone());
}
for name in &input_artifacts {
if self.blackboard.artifact_exists(name) {
packet
.pinned_paths
.push(artifact_rel_path(name));
}
}
}
WorkerFamily::Api => {
let workspace = self.blackboard.workspace();
for m in mentioned {
if let Some(file) = read_inlined(workspace, m) {
packet.inlined_files.push(file);
}
}
for name in &input_artifacts {
if let Ok(contents) = self.blackboard.read_artifact(name) {
packet.inlined_files.push(InlinedFile {
path: artifact_rel_path(name),
contents,
});
}
}
}
}
if let Some(graph) = self.graph {
let known = graph.symbol_names();
let picked = resolve_symbols(graph, &node.instruction, mentioned, &known);
for name in &picked {
if let Some(neighborhood) = graph.neighborhood(name) {
packet.symbol_map.push(neighborhood);
}
}
}
Ok(packet)
}
}
fn artifact_rel_path(name: &str) -> PathBuf {
Path::new(BLACKBOARD_DIR).join("artifacts").join(name)
}
const MAX_INLINE_BYTES: usize = 256 * 1024;
fn read_inlined(workspace: &Path, rel: &Path) -> Option<InlinedFile> {
let abs = if rel.is_absolute() {
rel.to_path_buf()
} else {
workspace.join(rel)
};
let mut contents = std::fs::read_to_string(&abs).ok()?;
if contents.len() > MAX_INLINE_BYTES {
let mut cut = MAX_INLINE_BYTES;
while cut > 0 && !contents.is_char_boundary(cut) {
cut -= 1;
}
contents.truncate(cut);
contents.push_str("\n…[file truncated: exceeded inline size limit]\n");
}
Some(InlinedFile {
path: rel.to_path_buf(),
contents,
})
}
#[cfg(test)]
mod graph_tests {
use super::*;
use rinne_types::graph::{CodeGraph, Neighborhood, SymbolRef};
struct FakeGraph;
impl CodeGraph for FakeGraph {
fn neighborhood(&self, symbol: &str) -> Option<Neighborhood> {
(symbol == "HttpTransport").then(|| Neighborhood {
definition: SymbolRef { name: "HttpTransport".into(), file: "t.rs".into(), line: 10 },
callers: vec![SymbolRef { name: "send".into(), file: "s.rs".into(), line: 3 }],
callees: vec![],
imports: vec![],
stale: false,
})
}
fn resolve_in_file(&self, _f: &str, _n: &str) -> Option<SymbolRef> { None }
fn symbol_names(&self) -> Vec<String> { vec!["HttpTransport".into()] }
}
#[test]
fn attaches_neighborhood_for_exact_identifier_in_instruction() {
let names = FakeGraph.symbol_names();
let picked = resolve_symbols(&FakeGraph, "add retry to HttpTransport", &[], &names);
assert_eq!(picked, vec!["HttpTransport".to_string()]);
}
#[test]
fn skips_common_short_names() {
let names = vec!["new".to_string(), "run".to_string()];
let picked = resolve_symbols(&FakeGraph, "run the new thing", &[], &names);
assert!(picked.is_empty());
}
}