1use crate::format::Wording;
10use agentplane::prelude::*;
11use serde_json::{Value, json};
12
13use super::attempt::{LoopSpec, tool_loop};
14use super::model_config::{CacheTtl, PromptStyle};
15use crate::tools::policy::Policy;
16
17pub const CAPABILITY: &str = "speccoder.feature.converge";
19
20pub const READ_ONLY: [&str; 5] = [
22 "read_file",
23 "list_files",
24 "search_code",
25 "git_status",
26 "git_diff",
27];
28
29pub fn policy(w: &Wording) -> String {
31 format!(
32 "You are the convergence step of speccoder. Assess the CURRENT code of a {product} \
33{unit} against its {documents} — the sole source of intent — with {rules} as \
34governing constraints. Ticked tasks are not evidence: read the code. You can only read \
35(read_file, list_files, search_code, git_status, git_diff); you change nothing. For \
36every {targets}, report a finding only where there is a gap, classified as `missing` (absent), \
37`partial` (exists but does not fully satisfy), `contradicts` (conflicts with stated \
38intent or {must}) or `unrequested` (code that exists although the {sources} \
39do not call for it). A requirement or rule the code does not meet is \
40ALWAYS missing, partial or contradicts — never unrequested. Severity: CRITICAL \
41({critical}), HIGH \
42(missing/partial on a core requirement), MEDIUM, LOW. For each finding give `task`: one \
43imperative sentence that a coding agent can complete (no task ID, no checkbox, no file \
44contents), `source_ref` (e.g. {refs}), and \
45`evidence` (the file or area you observed). Do not report what is already satisfied; do \
46not invent requirements. If nothing remains, return no findings. Finish with the \
47requested structured answer.",
48 product = w.product,
49 unit = w.unit,
50 documents = w.documents,
51 rules = w.assess.rules,
52 targets = w.assess.targets,
53 must = w.assess.must,
54 sources = w.assess.sources,
55 critical = w.assess.critical,
56 refs = w.assess.refs,
57 )
58}
59
60#[must_use]
62pub fn schema() -> Value {
63 json!({
64 "type": "object",
65 "properties": {
66 "findings": {
67 "type": "array",
68 "items": {
69 "type": "object",
70 "properties": {
71 "source_ref": { "type": "string" },
72 "gap_type": { "type": "string", "enum": ["missing", "partial", "contradicts", "unrequested"] },
73 "severity": { "type": "string", "enum": ["CRITICAL", "HIGH", "MEDIUM", "LOW"] },
74 "description": { "type": "string" },
75 "evidence": { "type": "string" },
76 "task": { "type": "string" },
77 },
78 "required": ["source_ref", "gap_type", "severity", "description", "evidence", "task"],
79 "additionalProperties": false,
80 },
81 },
82 "checked": { "type": "array", "items": { "type": "string" } },
83 },
84 "required": ["findings", "checked"],
85 "additionalProperties": false,
86 })
87}
88
89#[must_use]
92pub fn input(
93 feature: &str,
94 artifacts: [&str; 4], files: &[String],
96 changed: &[String],
97 max_turns: u32,
98 max_tool_calls: u32,
99) -> Tainted<Value> {
100 let [constitution, spec, plan, tasks] = artifacts;
101 let mut fields = vec![
103 ("feature", Tainted::trusted(json!(feature))),
104 ("constitution", Tainted::trusted(json!(constitution))),
105 ("spec", Tainted::trusted(json!(spec))),
106 ("plan", Tainted::trusted(json!(plan))),
107 ("tasks", Tainted::trusted(json!(tasks))),
108 (
109 "files",
110 Tainted::with_label(
111 json!(files),
112 super::provenance::from(super::provenance::WORKTREE),
113 ),
114 ),
115 (
116 "settings",
117 Tainted::trusted(json!({ "max_turns": max_turns, "max_tool_calls": max_tool_calls })),
118 ),
119 ];
120 if !changed.is_empty() {
124 fields.push(("changed", Tainted::trusted(json!(changed))));
125 }
126 Tainted::object(fields)
127}
128
129fn prompt_raw(input: &Value, ttl: CacheTtl, style: PromptStyle, w: &Wording) -> Value {
130 let system_policy = policy(w);
131 let text = |key: &str| {
132 let t = input[key].as_str().unwrap_or_default();
133 if t.trim().is_empty() {
134 "(none)".to_owned()
135 } else {
136 t.to_owned()
137 }
138 };
139 let files = input["files"]
140 .as_array()
141 .map(|f| {
142 f.iter()
143 .filter_map(Value::as_str)
144 .collect::<Vec<_>>()
145 .join("\n")
146 })
147 .unwrap_or_default();
148 let feature = input["feature"].as_str().unwrap_or_default();
149 let mut parts = vec![
150 format!("# {} ({feature})\n\n{}", w.files[0], text("spec")),
151 format!("# {}\n\n{}", w.files[1], text("plan")),
152 format!("# {}\n\n{}", w.files[2], text("tasks")),
153 format!("# Project files\n\n{files}"),
154 ];
155 if let Some(changed) = input["changed"].as_array().filter(|c| !c.is_empty()) {
156 let lines: Vec<&str> = changed.iter().filter_map(Value::as_str).collect();
157 parts.push(format!(
158 "# Changed since their tasks were verified\n\nThese requirements were reworded, added or removed after the tasks citing them were ticked: the code was judged against the earlier wording. Assess each against the wording now; a gap is `partial` or `contradicts`.\n\n{}",
159 lines.join("\n")
160 ));
161 }
162 let system = format!(
163 "{system_policy}\n\n# {}\n\n{}",
164 w.constitution,
165 text("constitution")
166 );
167 match style {
168 PromptStyle::OpenAi => json!({
169 "system": system,
170 "input": [{ "role": "user", "content": parts.iter().map(|t| json!({ "type": "input_text", "text": t })).collect::<Vec<_>>() }],
171 }),
172 PromptStyle::Anthropic | PromptStyle::Chat | PromptStyle::Parts { .. } => {
173 let breakpoint = match ttl {
174 CacheTtl::FiveMinutes => json!({ "type": "ephemeral" }),
175 CacheTtl::OneHour => json!({ "type": "ephemeral", "ttl": "1h" }),
176 };
177 let content: Vec<Value> = parts
178 .iter()
179 .enumerate()
180 .map(|(i, t)| {
181 if i == 1 {
182 json!({ "type": "text", "text": t, "cache_control": breakpoint })
183 } else {
184 json!({ "type": "text", "text": t })
185 }
186 })
187 .collect();
188 json!({
189 "system": system,
190 "messages": [{ "role": "user", "content": content }],
191 })
192 }
193 }
194}
195
196fn prompt(input: &Value, ttl: CacheTtl, style: PromptStyle, w: &Wording) -> Value {
197 let mut out = prompt_raw(input, ttl, style, w);
198 if style == PromptStyle::Chat {
199 super::model_config::strip_cache_control(&mut out);
200 }
201 if let PromptStyle::Parts { caches } = style {
202 super::model_config::to_parts(&mut out, caches);
203 }
204 out
205}
206
207#[derive(Debug)]
209pub struct Converge {
210 pub style: PromptStyle,
212 pub cache_ttl: CacheTtl,
214 pub format: &'static dyn crate::format::Adapter,
216}
217
218#[async_trait]
219impl Skill for Converge {
220 fn descriptor(&self) -> SkillDescriptor {
221 SkillDescriptor::new("speccoder.feature.converge").provides(CAPABILITY)
222 }
223
224 async fn invoke(
225 &self,
226 cx: &mut StepCtx<'_>,
227 input: Tainted<Value>,
228 ) -> Result<Outcome, SkillError> {
229 let (ttl, style) = (self.cache_ttl, self.style);
230 let settings = input.peek()["settings"].clone();
231 let limit = |key: &str, default: u32| {
232 settings[key]
233 .as_u64()
234 .map_or(default, |n| u32::try_from(n).unwrap_or(u32::MAX))
235 };
236 let prompt = super::provenance::labelled_prompt(
237 prompt(input.peek(), ttl, style, self.format.wording()),
238 &super::provenance::from(super::provenance::WORKTREE),
239 );
240 let tools = crate::tools::declarations()
241 .into_iter()
242 .filter(|d| READ_ONLY.contains(&d.name.as_str()))
243 .collect();
244 tool_loop(
245 cx,
246 &prompt,
247 LoopSpec {
248 reasoning: false,
250 tools,
251 policy: Policy::new(&[], &[]).rules(self.format.rules()),
252 max_turns: limit("max_turns", 30).max(1),
253 max_tool_calls: limit("max_tool_calls", 100),
254 schema: schema(),
255 ask: None,
256 run_approved: Vec::new(),
257 once: Vec::new(),
258 run: None,
259 asking: super::attempt::Asking::default(),
260 parts: Vec::new(),
261 mcp_digests: std::collections::BTreeMap::new(),
262 observer: None,
263 routes: Vec::new(),
264 root: None,
265 frozen_additions: false,
266 finish: |_| "completed",
267 },
268 )
269 .await
270 }
271}
272
273#[cfg(test)]
274mod tests {
275 use super::*;
276
277 fn text(input: &Tainted<Value>) -> String {
278 use crate::format::Adapter as _;
279 let w = crate::format::speckit::SpecKit.wording();
280 prompt(
281 input.peek(),
282 CacheTtl::FiveMinutes,
283 PromptStyle::Anthropic,
284 w,
285 )
286 .to_string()
287 }
288
289 #[test]
290 fn changed_requirements_reach_the_assessment_only_when_there_are_some() {
291 let artifacts = ["c", "spec", "plan", "tasks"];
292 let none = input("001-x", artifacts, &[], &[], 10, 20);
293 assert!(none.peek().get("changed").is_none());
294 assert!(!text(&none).contains("Changed since their tasks were verified"));
295 let changed = vec!["- T004 FR-001: was \"password\"; now \"passkey\"".to_owned()];
296 let some = input("001-x", artifacts, &[], &changed, 10, 20);
297 let t = text(&some);
298 assert!(t.contains("Changed since their tasks were verified"), "{t}");
299 assert!(t.contains("T004 FR-001"), "{t}");
300 }
301}