speccoder-runtime 0.1.0

Application services of speccoder: the only crate that executes anything.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! The integrity audit of an attempt's diff.
//!
//! Line-based and deliberately suspicious: it errs on flagging. Any finding
//! fails the verdict with an `integrity:` reason; a person looks.

use std::collections::{BTreeMap, BTreeSet};
use std::path::Path;

use regex::Regex;
use sha2::{Digest, Sha256};

use crate::tools::paths::{Class, Protected};
use crate::tools::policy::Policy;
use crate::worktree::{Change, ChangeKind};

/// Directories a snapshot never descends into: build output and installed
/// dependencies, where no judge input lives.
const SKIPPED_DIRS: &[&str] = &[
    ".git",
    "target",
    "node_modules",
    ".venv",
    "venv",
    "__pycache__",
    ".tox",
    ".mypy_cache",
    ".pytest_cache",
];

/// The judge, intent and agent context inputs of a worktree by content hash,
/// git-ignored files included (`.claude/settings.local.json` is ignored by
/// default, and an agent still loads it).
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct JudgeSnapshot(BTreeMap<String, String>);

/// A judge input that changed between two snapshots.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
pub struct Drift {
    /// Workspace-relative path.
    pub path: String,
    /// `judge`, `intent` or `context`.
    pub class: &'static str,
    /// `added`, `modified` or `deleted`.
    pub change: &'static str,
}

impl JudgeSnapshot {
    /// Hashes every judge, intent and context input under `root` as `policy`
    /// sees them (a planning skill may write Spec Kit's artifacts).
    #[must_use]
    pub fn take(root: &Path, policy: &Policy) -> Self {
        let mut files = BTreeMap::new();
        let walk = ignore::WalkBuilder::new(root)
            .hidden(false)
            .ignore(false)
            .git_ignore(false)
            .git_global(false)
            .git_exclude(false)
            .parents(false)
            .follow_links(false)
            .filter_entry(|e| {
                !(e.depth() > 0
                    && e.file_type().is_some_and(|t| t.is_dir())
                    && SKIPPED_DIRS.contains(&e.file_name().to_string_lossy().as_ref()))
            })
            .build();
        for entry in walk.flatten() {
            if !entry.file_type().is_some_and(|t| t.is_file()) {
                continue;
            }
            let Ok(rel) = entry.path().strip_prefix(root) else {
                continue;
            };
            let rel = rel.to_string_lossy().replace('\\', "/");
            if class_of(policy, &rel).is_none() {
                continue;
            }
            if let Ok(bytes) = std::fs::read(entry.path()) {
                files.insert(rel, sha256_hex(&bytes));
            }
        }
        Self(files)
    }

    /// Path → content hash.
    #[must_use]
    pub fn entries(&self) -> &BTreeMap<String, String> {
        &self.0
    }

    /// A snapshot from recorded entries.
    #[must_use]
    pub fn from_entries(entries: BTreeMap<String, String>) -> Self {
        Self(entries)
    }

    /// Records `path`'s new content hash (the engine's own sanctioned write).
    pub fn set(&mut self, path: &str, hash: String) {
        self.0.insert(path.to_owned(), hash);
    }

    /// What changed from `self` to `later`, except `approved` paths (a
    /// person's grant to write them).
    #[must_use]
    pub fn drift(&self, later: &Self, policy: &Policy, approved: &BTreeSet<String>) -> Vec<Drift> {
        let mut out = Vec::new();
        let paths: BTreeSet<&String> = self.0.keys().chain(later.0.keys()).collect();
        for path in paths {
            let change = match (self.0.get(path), later.0.get(path)) {
                (None, Some(_)) => "added",
                (Some(_), None) => "deleted",
                (Some(a), Some(b)) if a != b => "modified",
                _ => continue,
            };
            if approved.contains(path.as_str()) {
                continue;
            }
            if let Some(class) = class_of(policy, path) {
                out.push(Drift {
                    path: path.clone(),
                    class,
                    change,
                });
            }
        }
        out
    }
}

fn class_of(policy: &Policy, rel: &str) -> Option<&'static str> {
    match policy.write_protected(rel) {
        Some(Class::Judge) => Some("judge"),
        Some(Class::Intent) => Some("intent"),
        Some(_) => None,
        None if policy.protected().is_context(rel) => Some("context"),
        None => None,
    }
}

/// Markers that declare a test.
const TEST_MARKERS: &str = r"#\[(tokio::)?test\b|^\s*def test_|^\s*(async\s+)?fn test_|\b(it|test)\s*\(\s*['\x22`]|@Test\b|^\s*func Test";
/// Markers that skip a test.
const SKIP_MARKERS: &str = r"#\[ignore\b|@pytest\.mark\.(skip|xfail)|\.skip\s*\(|\bx(it|describe|test)\s*\(|@Disabled\b|@Ignore\b|\bt\.Skip(Now)?\s*\(|@unittest\.skip|\.todo\s*\(";

/// Whether every line of `old` survives, unchanged and in order, in `new`:
/// the change only adds lines (`[tests] frozen = "additions"`).
#[must_use]
pub fn only_additions(old: &str, new: &str) -> bool {
    let mut new_lines = new.lines();
    old.lines().all(|line| new_lines.any(|n| n == line))
}

/// Findings for an attempt: `changes` and `patch` between its checkpoints,
/// `frozen` the test files that existed when it started. With
/// `additions_ok`, a frozen test whose diff only adds lines is no finding
/// (removed tests and skip markers still are). An agent context file
/// (`AGENTS.md`, …) changed without a person's grant in `approved` is one.
#[must_use]
pub fn audit(
    changes: &[Change],
    patch: &str,
    frozen: &BTreeSet<String>,
    additions_ok: bool,
    approved: &BTreeSet<String>,
) -> Vec<String> {
    audit_in(
        &Protected::default(),
        changes,
        patch,
        frozen,
        additions_ok,
        approved,
    )
}

/// [`audit`] against the protected set of the project's format.
#[must_use]
pub fn audit_in(
    protected: &Protected,
    changes: &[Change],
    patch: &str,
    frozen: &BTreeSet<String>,
    additions_ok: bool,
    approved: &BTreeSet<String>,
) -> Vec<String> {
    let mut findings = Vec::new();
    let removed_lines = removed_lines(patch);

    for c in changes {
        if frozen.contains(&c.path) {
            match c.kind {
                ChangeKind::Deleted => findings.push(format!("frozen test deleted: {}", c.path)),
                ChangeKind::Modified
                    if additions_ok && removed_lines.get(&c.path).copied().unwrap_or(0) == 0 => {}
                ChangeKind::Modified => findings.push(format!("frozen test modified: {}", c.path)),
                ChangeKind::Added => {}
            }
        }
        match protected.write_class(&c.path) {
            Some(Class::Judge) => findings.push(format!("verification input changed: {}", c.path)),
            Some(Class::Intent) => findings.push(format!("spec changed: {}", c.path)),
            None if protected.is_context(&c.path) && !approved.contains(&c.path) => {
                findings.push(format!("agent context file changed: {}", c.path));
            }
            _ => {}
        }
    }

    let (Ok(test), Ok(skip)) = (Regex::new(TEST_MARKERS), Regex::new(SKIP_MARKERS)) else {
        return findings;
    };
    let mut tests: BTreeMap<String, (usize, usize)> = BTreeMap::new(); // (added, removed)
    let mut skips: BTreeSet<String> = BTreeSet::new();
    let mut file = String::new();
    for line in patch.lines() {
        if let Some(header) = line.strip_prefix("diff --git a/") {
            // `diff --git a/<path> b/<path>` (no renames): the path is the
            // same on both sides, also for additions and deletions.
            file = header
                .split_once(" b/")
                .map_or(header, |(a, _)| a)
                .to_owned();
        } else if line.starts_with("+++ ") || line.starts_with("--- ") {
            // File headers; the name came from `diff --git`.
        } else if let Some(added) = line.strip_prefix('+') {
            if test.is_match(added) {
                tests.entry(file.clone()).or_default().0 += 1;
            }
            if skip.is_match(added) {
                skips.insert(file.clone());
            }
        } else if let Some(removed) = line.strip_prefix('-')
            && test.is_match(removed)
        {
            tests.entry(file.clone()).or_default().1 += 1;
        }
    }
    let deleted: BTreeSet<&str> = changes
        .iter()
        .filter(|c| c.kind == ChangeKind::Deleted)
        .map(|c| c.path.as_str())
        .collect();
    for (path, (added, removed)) in tests {
        // A deleted file is reported as such (frozen) or is new work undone.
        if removed > added && !deleted.contains(path.as_str()) {
            findings.push(format!(
                "test removed in {path} ({removed} removed, {added} added)"
            ));
        }
    }
    for path in skips {
        findings.push(format!("skip marker added in {path}"));
    }
    findings
}

/// Removed lines per file in a unified diff.
fn removed_lines(patch: &str) -> BTreeMap<String, usize> {
    let mut out: BTreeMap<String, usize> = BTreeMap::new();
    let mut file = String::new();
    for line in patch.lines() {
        if let Some(header) = line.strip_prefix("diff --git a/") {
            file = header
                .split_once(" b/")
                .map_or(header, |(a, _)| a)
                .to_owned();
        } else if line.starts_with('-') && !line.starts_with("--- ") {
            *out.entry(file.clone()).or_default() += 1;
        }
    }
    out
}

/// Lowercase hex SHA-256 of `bytes`: the content hash of a judge input or a
/// task list, as the format adapters' write guards compare it.
#[must_use]
pub fn sha256_hex(bytes: &[u8]) -> String {
    Sha256::digest(bytes)
        .iter()
        .map(|b| format!("{b:02x}"))
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    fn change(path: &str, kind: ChangeKind) -> Change {
        Change {
            path: path.to_owned(),
            kind,
        }
    }

    #[test]
    fn flags_tampering_and_passes_clean_diffs() {
        let frozen: BTreeSet<String> = ["tests/api.rs".to_owned()].into();
        let patch = "diff --git a/src/lib.rs b/src/lib.rs\n--- a/src/lib.rs\n+++ b/src/lib.rs\n@@ -1 +1 @@\n-    #[test]\n+    #[ignore]\n+    #[test]\n";
        let f = audit(
            &[
                change("tests/api.rs", ChangeKind::Deleted),
                change("speccoder.toml", ChangeKind::Modified),
            ],
            patch,
            &frozen,
            false,
            &BTreeSet::new(),
        );
        assert_eq!(
            f,
            [
                "frozen test deleted: tests/api.rs",
                "verification input changed: speccoder.toml",
                "skip marker added in src/lib.rs",
            ]
        );

        let removed = "diff --git a/src/lib.rs b/src/lib.rs\n--- a/src/lib.rs\n+++ b/src/lib.rs\n@@ -5,4 +4,0 @@\n-    #[test]\n-    fn a() {}\n";
        assert_eq!(
            audit(&[], removed, &BTreeSet::new(), false, &BTreeSet::new()),
            ["test removed in src/lib.rs (1 removed, 0 added)"]
        );

        let clean = "diff --git a/src/lib.rs b/src/lib.rs\n--- a/src/lib.rs\n+++ b/src/lib.rs\n@@ -1 +1,4 @@\n+pub fn add() {}\n+    #[test]\n+    fn adds() {}\n";
        assert!(
            audit(
                &[
                    change("src/lib.rs", ChangeKind::Modified),
                    change("tests/new.rs", ChangeKind::Added)
                ],
                clean,
                &frozen,
                false,
                &BTreeSet::new()
            )
            .is_empty()
        );
    }

    #[test]
    fn additions_to_a_frozen_test_pass_only_in_additions_mode() {
        let frozen: BTreeSet<String> = ["tests/api.rs".to_owned()].into();
        let added = "diff --git a/tests/api.rs b/tests/api.rs\n--- a/tests/api.rs\n+++ b/tests/api.rs\n@@ -1,2 +1,4 @@\n #[test]\n fn a() {}\n+#[test]\n+fn b() {}\n";
        let changed = "diff --git a/tests/api.rs b/tests/api.rs\n--- a/tests/api.rs\n+++ b/tests/api.rs\n@@ -1,2 +1,2 @@\n #[test]\n-fn a() { assert!(x()) }\n+fn a() {}\n";
        let modified = [change("tests/api.rs", ChangeKind::Modified)];
        assert!(audit(&modified, added, &frozen, true, &BTreeSet::new()).is_empty());
        assert_eq!(
            audit(&modified, added, &frozen, false, &BTreeSet::new()),
            ["frozen test modified: tests/api.rs"]
        );
        assert_eq!(
            audit(&modified, changed, &frozen, true, &BTreeSet::new()),
            ["frozen test modified: tests/api.rs"]
        );

        assert!(only_additions("a\nb\n", "a\nnew\nb\nmore\n"));
        assert!(!only_additions("a\nb\n", "a\nB\n"));
        assert!(!only_additions("a\nb\n", "b\na\n"));
    }

    #[test]
    fn a_snapshot_sees_ignored_judge_inputs_and_reports_drift() {
        use crate::tools::policy::Policy;
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        std::fs::create_dir_all(root.join(".claude")).unwrap();
        std::fs::create_dir_all(root.join("target/debug")).unwrap();
        std::fs::write(root.join(".claude/settings.json"), "{}").unwrap();
        std::fs::write(root.join("AGENTS.md"), "a").unwrap();
        std::fs::write(root.join("speccoder.toml"), "x").unwrap();
        std::fs::write(root.join("src.rs"), "free").unwrap();
        std::fs::write(root.join("target/debug/speccoder.toml"), "skipped").unwrap();
        let policy = Policy::new(&[], &[]);
        let before = JudgeSnapshot::take(root, &policy);
        assert_eq!(
            before.0.len(),
            3,
            "{:?}",
            before.0.keys().collect::<Vec<_>>()
        );

        std::fs::write(root.join(".claude/settings.local.json"), "{}").unwrap();
        std::fs::write(root.join("AGENTS.md"), "b").unwrap();
        std::fs::remove_file(root.join("speccoder.toml")).unwrap();
        std::fs::write(root.join("src.rs"), "changed").unwrap();
        let after = JudgeSnapshot::take(root, &policy);
        let drift = before.drift(&after, &policy, &BTreeSet::new());
        let seen: Vec<(&str, &str, &str)> = drift
            .iter()
            .map(|d| (d.path.as_str(), d.class, d.change))
            .collect();
        assert_eq!(
            seen,
            [
                (".claude/settings.local.json", "judge", "added"),
                ("AGENTS.md", "context", "modified"),
                ("speccoder.toml", "judge", "deleted"),
            ]
        );
        // A person's grant for the context file exempts it.
        let approved: BTreeSet<String> = ["AGENTS.md".to_owned()].into();
        assert_eq!(before.drift(&after, &policy, &approved).len(), 2);
        // Restored content is no drift.
        std::fs::write(root.join("AGENTS.md"), "a").unwrap();
        std::fs::write(root.join("speccoder.toml"), "x").unwrap();
        std::fs::remove_file(root.join(".claude/settings.local.json")).unwrap();
        assert!(
            before
                .drift(
                    &JudgeSnapshot::take(root, &policy),
                    &policy,
                    &BTreeSet::new()
                )
                .is_empty()
        );
    }
}