spec_driven_docs/services/
policy.rs1use std::collections::BTreeSet;
18
19use camino::Utf8Path;
20
21use camino::Utf8PathBuf;
22
23use crate::adapters::fs::write_atomic;
24use crate::domain::debt::Debt;
25use crate::domain::ownership::Sha256;
26use crate::domain::policy::{SENTINELS, Sentinel};
27use crate::domain::profile::{DocsRoot, resolve_destination};
28use crate::domain::rule_id::RuleId;
29use crate::error::AppError;
30
31#[derive(Debug, Clone, PartialEq, Eq)]
33pub struct Reconciliation {
34 pub sentinel: &'static Sentinel,
36}
37
38impl Reconciliation {
39 #[must_use]
41 pub fn note(&self, docs_root: DocsRoot) -> String {
42 format!(
43 "note: {} and no local specification defines `{}`; {} owns it; run 'sdd policy reconcile'",
44 self.sentinel.declares,
45 self.sentinel.rule,
46 crate::domain::profile::resolve_destination(&self.sentinel.destination, docs_root)
47 )
48 }
49}
50
51pub fn local_rule_ids(
57 target: &Utf8Path,
58 docs_root: DocsRoot,
59) -> Result<BTreeSet<String>, AppError> {
60 let specs = target.join(docs_root.as_str()).join("specs");
61 let mut ids = BTreeSet::new();
62 let Ok(entries) = specs.read_dir_utf8() else {
63 return Ok(ids);
64 };
65 for entry in entries.filter_map(Result::ok) {
66 let path = entry.path();
67 #[allow(
68 clippy::case_sensitive_file_extension_comparisons,
69 reason = "the corpus convention is lowercase"
70 )]
71 if !path.as_str().ends_with(".md") {
72 continue;
73 }
74 let text = std::fs::read_to_string(path)?;
75 ids.extend(crate::embedded::rule_ids_in(&text));
76 }
77 Ok(ids)
78}
79
80fn active(target: &Utf8Path, sentinel: &Sentinel) -> bool {
86 match sentinel.rule {
87 RuleId::RecordedDimensionOnlyShrinks => {
88 Debt::read(target).is_ok_and(|debt| !debt.is_empty())
89 }
90 RuleId::ProjectSelectsOneSource => {
91 crate::domain::instance_config::InstanceConfig::read(target).is_ok_and(|declaration| {
92 declaration.writing_style.source
93 != crate::domain::instance_config::WritingSource::Builtin
94 })
95 }
96 _ => false,
97 }
98}
99
100pub fn needed(target: &Utf8Path, docs_root: DocsRoot) -> Result<Vec<Reconciliation>, AppError> {
106 let defined = local_rule_ids(target, docs_root)?;
107 Ok(SENTINELS
108 .iter()
109 .filter(|sentinel| active(target, sentinel))
110 .filter(|sentinel| !defined.contains(sentinel.rule.as_str()))
111 .map(|sentinel| Reconciliation { sentinel })
112 .collect())
113}
114
115#[derive(Debug, Clone, PartialEq, Eq)]
117pub enum Action {
118 Seed {
120 destination: Utf8PathBuf,
122 bytes: Vec<u8>,
124 },
125 Append {
128 destination: Utf8PathBuf,
130 block: String,
132 rewritten: String,
134 },
135 Checklist {
138 destination: Utf8PathBuf,
140 block: String,
142 },
143}
144
145#[derive(Debug, Clone, PartialEq, Eq)]
147pub struct Plan {
148 pub reconciliation: Reconciliation,
150 pub action: Action,
152}
153
154#[must_use]
157pub fn rule_block(seed: &str, rule: RuleId) -> Option<String> {
158 let heading = format!("### `{rule}`");
159 let mut lines = seed.lines().skip_while(|line| !line.starts_with(&heading));
160 let first = lines.next()?;
161 let mut block = format!("{first}\n");
162 for line in lines {
163 if line.starts_with("### ") || line.starts_with("## ") {
164 break;
165 }
166 block.push_str(line);
167 block.push('\n');
168 }
169 Some(format!("{}\n", block.trim_end_matches('\n')))
170}
171
172#[must_use]
175pub fn append_to_requirements(text: &str, block: &str) -> Option<String> {
176 let lines: Vec<&str> = text.lines().collect();
177 let start = lines.iter().position(|line| *line == "## Requirements")?;
178 let end = lines[start + 1..]
179 .iter()
180 .position(|line| line.starts_with("## "))
181 .map_or(lines.len(), |offset| start + 1 + offset);
182 let mut out = String::new();
183 for line in &lines[..end] {
184 out.push_str(line);
185 out.push('\n');
186 }
187 let trimmed = out.trim_end_matches('\n').to_string();
188 out = format!("{trimmed}\n\n{block}");
189 if end < lines.len() {
190 out.push('\n');
191 for line in &lines[end..] {
192 out.push_str(line);
193 out.push('\n');
194 }
195 }
196 Some(out)
197}
198
199pub fn plan(
205 target: &Utf8Path,
206 docs_root: DocsRoot,
207 bundle: &dyn crate::release::ReleaseBundle,
208) -> Result<Vec<Plan>, AppError> {
209 let mut plans = Vec::new();
210 for reconciliation in needed(target, docs_root)? {
211 let sentinel = reconciliation.sentinel;
212 let seed = bundle.artifact(&sentinel.source)?;
213 let seed_text = std::str::from_utf8(&seed).map_err(anyhow::Error::from)?;
214 let block = rule_block(seed_text, sentinel.rule).ok_or_else(|| {
215 anyhow::anyhow!("{} does not define {}", sentinel.source, sentinel.rule)
216 })?;
217 let destination = resolve_destination(&sentinel.destination, docs_root);
218 let full = target.join(&destination);
219 let action = if full.is_file() {
220 let text = std::fs::read_to_string(&full)?;
221 match append_to_requirements(&text, &block) {
222 Some(rewritten)
223 if crate::embedded::rule_ids_in(&rewritten)
224 .any(|id| id == sentinel.rule.as_str()) =>
225 {
226 Action::Append {
227 destination,
228 block,
229 rewritten,
230 }
231 }
232 _ => Action::Checklist { destination, block },
233 }
234 } else {
235 Action::Seed {
236 destination,
237 bytes: seed.clone(),
238 }
239 };
240 plans.push(Plan {
241 reconciliation,
242 action,
243 });
244 }
245 Ok(plans)
246}
247
248fn with_adopted_record(
254 document: &mut serde_json::Value,
255 source: &str,
256 destination: &Utf8Path,
257 bytes: &[u8],
258 baseline: &[u8],
259) -> Result<(), AppError> {
260 let digest = Sha256::of(bytes).to_string();
261 let Some(entries) = document
262 .get_mut("adopted_files")
263 .and_then(serde_json::Value::as_array_mut)
264 else {
265 return Err(AppError::ManifestInvalid(
266 "adopted_files is not an array".to_string(),
267 ));
268 };
269 let recorded = entries.iter_mut().find(|entry| {
270 entry.get("destination").and_then(serde_json::Value::as_str) == Some(destination.as_str())
271 });
272 match recorded {
273 Some(entry) => entry["sha256"] = serde_json::Value::String(digest),
274 None => entries.push(serde_json::json!({
275 "source": source,
276 "destination": destination.as_str(),
277 "sha256": digest,
278 "baseline_sha256": Sha256::of(baseline).to_string(),
279 })),
280 }
281 Ok(())
282}
283
284type Write = (Utf8PathBuf, Vec<u8>, &'static Sentinel);
285
286fn preflight(target: &Utf8Path, plans: &[Plan]) -> Result<Vec<Write>, AppError> {
289 let mut writes: Vec<Write> = Vec::new();
290 for plan in plans {
291 let sentinel = plan.reconciliation.sentinel;
292 match &plan.action {
293 Action::Seed { destination, bytes } => {
294 writes.push((destination.clone(), bytes.clone(), sentinel));
295 }
296 Action::Append {
297 destination,
298 rewritten,
299 ..
300 } => writes.push((
301 destination.clone(),
302 rewritten.clone().into_bytes(),
303 sentinel,
304 )),
305 Action::Checklist { destination, .. } => {
306 return Err(AppError::Refused(format!(
307 "{destination} is not in a shape this command rewrites; add the rule by hand"
308 )));
309 }
310 }
311 }
312 for (destination, _, _) in &writes {
313 crate::adapters::fs::check_destination(target, destination)
314 .map_err(|refusal| AppError::Refused(format!("{destination}: {refusal}")))?;
315 }
316 let manifest_relative = Utf8Path::new(crate::domain::manifest::MANIFEST_PATH);
317 crate::adapters::fs::check_destination(target, manifest_relative)
318 .map_err(|refusal| AppError::Refused(format!("{manifest_relative}: {refusal}")))?;
319 Ok(writes)
320}
321
322fn restore(target: &Utf8Path, backups: &[(Utf8PathBuf, Option<Vec<u8>>)]) -> Vec<Utf8PathBuf> {
328 let mut unrestored = Vec::new();
329 for (destination, previous) in backups {
330 let full = target.join(destination);
331 let current = match std::fs::read(&full) {
335 Ok(bytes) => Some(Some(bytes)),
336 Err(error) if error.kind() == std::io::ErrorKind::NotFound => Some(None),
337 Err(_) => None,
338 };
339 if current.as_ref() == Some(previous) {
340 continue;
341 }
342 let put_back = previous.as_ref().map_or_else(
345 || match std::fs::remove_file(&full) {
346 Ok(()) => true,
347 Err(error) => error.kind() == std::io::ErrorKind::NotFound,
348 },
349 |bytes| write_atomic(&full, bytes).is_ok(),
350 );
351 if !put_back {
352 unrestored.push(destination.clone());
353 }
354 }
355 unrestored
356}
357
358pub fn apply_all(
375 target: &Utf8Path,
376 plans: &[Plan],
377 bundle: &dyn crate::release::ReleaseBundle,
378) -> Result<Vec<Utf8PathBuf>, AppError> {
379 let manifest_relative = Utf8Path::new(crate::domain::manifest::MANIFEST_PATH);
380 let writes = preflight(target, plans)?;
381 let manifest_text = std::fs::read_to_string(target.join(manifest_relative))?;
382 let mut document: serde_json::Value = serde_json::from_str(&manifest_text)
383 .map_err(|error| AppError::ManifestInvalid(error.to_string()))?;
384
385 let mut backups: Vec<(Utf8PathBuf, Option<Vec<u8>>)> = Vec::new();
386 let mut attempt = |backups: &mut Vec<(Utf8PathBuf, Option<Vec<u8>>)>| -> Result<(), AppError> {
387 for (destination, bytes, sentinel) in &writes {
388 let full = target.join(destination);
389 let previous = if full.is_file() {
390 Some(std::fs::read(&full)?)
391 } else {
392 None
393 };
394 backups.push((destination.clone(), previous));
395 write_atomic(&full, bytes)?;
396 let written = std::fs::read_to_string(&full)?;
397 if !crate::embedded::rule_ids_in(&written).any(|id| id == sentinel.rule.as_str()) {
398 return Err(AppError::Refused(format!(
399 "{destination} did not define `{}` after the rewrite",
400 sentinel.rule
401 )));
402 }
403 let seed = bundle.artifact(&sentinel.source)?;
404 with_adopted_record(&mut document, &sentinel.source, destination, bytes, &seed)?;
405 }
406 backups.push((
407 manifest_relative.to_path_buf(),
408 Some(manifest_text.clone().into_bytes()),
409 ));
410 let rendered = serde_json::to_string_pretty(&document)
411 .map_err(|error| AppError::ManifestInvalid(error.to_string()))?;
412 write_atomic(
413 &target.join(manifest_relative),
414 format!("{rendered}\n").as_bytes(),
415 )?;
416 Ok(())
417 };
418 if let Err(error) = attempt(&mut backups) {
419 let unrestored = restore(target, &backups);
420 let cause = match error {
421 AppError::Refused(reason) => reason,
422 other => format!("reconciliation aborted: {other}"),
423 };
424 if unrestored.is_empty() {
425 return Err(AppError::Refused(format!(
426 "{cause}; every file is restored"
427 )));
428 }
429 let paths: Vec<&str> = unrestored.iter().map(|p| p.as_str()).collect();
430 return Err(AppError::Refused(format!(
431 "{cause}; restoration is incomplete, verify by hand: {}",
432 paths.join(" ")
433 )));
434 }
435 Ok(writes
436 .into_iter()
437 .map(|(destination, _, _)| destination)
438 .collect())
439}
440
441#[cfg(test)]
442mod tests {
443 use super::*;
444
445 const SPEC: &str = "# Sample\n\n## Purpose\n\nOurs.\n\n## Requirements\n\n### `sample:first` — First\n\nThe author MUST keep it.\n\n#### Scenario: One\n\n- GIVEN x\n- WHEN y\n- THEN z\n\nVerify: `true`\n\n## Unenforced\n\n| Rule | Reviewer confirms |\n| --- | --- |\n";
446
447 const BLOCK: &str = "### `sample:second` — Second\n\nThe author MUST add it.\n\n#### Scenario: Two\n\n- GIVEN a\n- WHEN b\n- THEN c\n\nVerify: `true`\n";
448
449 #[test]
450 fn the_rule_block_runs_from_its_heading_to_the_next() {
451 let seed = format!("{SPEC}\n{BLOCK}");
452 let block = rule_block(&seed, RuleId::RecordedDimensionOnlyShrinks);
453 assert!(block.is_none(), "a rule the seed lacks is not found");
454 let debt =
455 std::str::from_utf8(crate::embedded::asset("_docs/specs/SPEC-budget-debt.md").unwrap())
456 .unwrap();
457 let block = rule_block(debt, RuleId::RecordedDimensionOnlyShrinks).unwrap();
458 assert!(block.starts_with("### `budget-debt:a-recorded-dimension-only-shrinks`"));
459 assert!(block.contains("Verify:"));
460 assert!(!block.contains("debt-is-created-by-an-explicit-act"));
461 assert!(block.ends_with('\n') && !block.ends_with("\n\n"));
462 }
463
464 #[test]
465 fn the_block_is_appended_before_the_next_section_and_everything_else_survives() {
466 let out = append_to_requirements(SPEC, BLOCK).unwrap();
467 let ids: Vec<String> = crate::embedded::rule_ids_in(&out).collect();
468 assert_eq!(
469 ids,
470 vec!["sample:first".to_string(), "sample:second".to_string()]
471 );
472 assert!(out.contains("Verify: `true`\n\n### `sample:second`"));
473 assert!(out.contains("Verify: `true`\n\n## Unenforced\n"));
474 assert!(out.ends_with("| --- | --- |\n"));
475 assert!(out.starts_with("# Sample\n\n## Purpose\n\nOurs.\n"));
476 }
477
478 #[test]
479 fn a_file_whose_requirements_close_it_takes_the_block_at_the_end() {
480 let spec = SPEC.split("## Unenforced").next().unwrap();
481 let out = append_to_requirements(spec, BLOCK).unwrap();
482 assert!(out.ends_with(&format!("\n\n{BLOCK}")));
483 }
484
485 #[test]
486 fn a_file_without_a_requirements_section_is_not_rewritten() {
487 assert!(append_to_requirements("# Ours\n\nProse only.\n", BLOCK).is_none());
488 }
489}