pushkin 0.2.1

Schema-first enforcement harness that gates AI coding agents' file writes against project contracts
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
418
//! F58 — codex `*** Delete File:` and `*** Move to:` walk straight through the
//! protected-path gate.
//!
//! COMMITTED RED. A NEW file per N10.
//!
//! **What was probed live, 2026-08-18.** `parse_apply_patch` recognizes two of
//! the four `apply_patch` section markers. Against this repo's own manifest:
//! `*** Update File: pushkin.toml` was correctly BLOCKED, while
//! `*** Delete File: pushkin.toml` failed OPEN ("unrecognized codex hook
//! payload") and `*** Update File: <ungated>` + `*** Move to: pushkin.toml`
//! was allowed in silence. Same protected file, three routes, two of them open.
//!
//! **Why this outranks F48.** F48 fell open on rules that need CONTENT. This
//! falls open on `protected_path` and `read_only_path` — the two rules this
//! project declares UNWAIVABLE. No amount of Phase B synthesis reaches it
//! either: a delete carries no content by definition, so there is nothing to
//! reconstruct. The fix is in the parser.
//!
//! **The verdict a delete must get, and must not get.** A delete is fully
//! decidable by the path rules and by nothing else. It must NOT refuse under
//! `pushkin.content_unavailable` — that message says a content rule could not
//! be evaluated, and for a file that is going away no such rule applies. A
//! refusal that misdescribes why is the failure mode this program keeps
//! finding.

use assert_cmd::Command;
use std::fs;
use std::path::Path;
use std::process::Command as StdCommand;

type TestResult = Result<(), Box<dyn std::error::Error>>;

const MANIFEST: &str = r#"
version = 1
canonical = "json-schema-2020-12"
authoring = "zod"

[[contracts]]
name = "user"
source = "contracts/user.zod.ts"
emit = ["zod"]

[[mappings]]
glob = "app/api/**/*.ts"
contracts = ["user"]
require = "boundary-validation"

[gates]
suppression_comments = "deny"
protected_paths = ["pushkin.toml"]
read_only_paths = ["crates/**/tests/**"]
"#;

const PROTECTED: &str = "pushkin.toml";
const COMMITTED_TEST: &str = "crates/pushkin-cli/tests/committed_suite.rs";
const MAPPED: &str = "app/api/users/route.ts";
const ORDINARY: &str = "docs/notes.md";

const RULE_PROTECTED: &str = "pushkin.protected_path";
const RULE_READ_ONLY: &str = "pushkin.read_only_path";
const RULE_UNVALIDATED: &str = "contract.boundary.unvalidated_input";
const RULE_CONTENT_UNAVAILABLE: &str = "pushkin.content_unavailable";

const FAIL_OPEN: &str = "failing open";

fn git(dir: &Path, args: &[&str]) -> TestResult {
    let status = StdCommand::new("git")
        .current_dir(dir)
        .args(args)
        .status()?;
    assert!(status.success(), "git {args:?} failed");
    Ok(())
}

fn repo() -> Result<tempfile::TempDir, Box<dyn std::error::Error>> {
    let dir = tempfile::tempdir()?;
    fs::write(dir.path().join(PROTECTED), MANIFEST)?;
    fs::create_dir_all(dir.path().join("contracts"))?;
    fs::write(
        dir.path().join("contracts/user.zod.ts"),
        "export const user = 1;\n",
    )?;
    fs::create_dir_all(dir.path().join("app/api/users"))?;
    fs::write(dir.path().join(MAPPED), "export const handler = 1;\n")?;
    fs::create_dir_all(dir.path().join("crates/pushkin-cli/tests"))?;
    fs::write(dir.path().join(COMMITTED_TEST), "// committed suite\n")?;
    fs::create_dir_all(dir.path().join("docs"))?;
    fs::write(dir.path().join(ORDINARY), "notes\n")?;
    git(dir.path(), &["init", "-q", "."])?;
    git(dir.path(), &["add", "-A"])?;
    git(
        dir.path(),
        &[
            "-c",
            "user.name=F58 Suite",
            "-c",
            "user.email=f58@test",
            "commit",
            "-qm",
            "fixture",
        ],
    )?;
    Ok(dir)
}

/// Wraps patch body lines in the envelope codex actually sends.
fn patch_payload(body: &str) -> String {
    serde_json::json!({
        "session_id": "f58",
        "hook_event_name": "PreToolUse",
        "tool_name": "apply_patch",
        "tool_input": { "command": format!("*** Begin Patch\n{body}*** End Patch\n") },
    })
    .to_string()
}

fn hook(
    dir: &Path,
    payload: &str,
    daemon: Option<&str>,
) -> Result<String, Box<dyn std::error::Error>> {
    let mut cmd = Command::cargo_bin("pushkin")?;
    cmd.current_dir(dir).write_stdin(payload.to_owned());
    if let Some(mode) = daemon {
        cmd.env("PUSHKIN_DAEMON", mode);
    }
    let output = cmd.args(["hook", "codex"]).output()?;
    Ok(format!(
        "{}{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    ))
}

// ---------------------------------------------------------------------------
// Delete — the fail-open
// ---------------------------------------------------------------------------

/// The headline. Deleting the manifest is the most consequential write an agent
/// could make to this repo, and it was allowed.
#[test]
fn deleting_a_protected_file_is_denied() -> TestResult {
    let dir = repo()?;

    let output = hook(
        dir.path(),
        &patch_payload(&format!("*** Delete File: {PROTECTED}\n")),
        Some("off"),
    )?;

    assert!(
        output.contains(RULE_PROTECTED),
        "a delete is a write to the protected path: {output}"
    );
    Ok(())
}

/// And it must be denied as a JUDGED write, not merely be noisy. A payload the
/// parser cannot read fails open by design; the point of the fix is that this
/// payload is now READ.
#[test]
fn deleting_a_protected_file_is_not_an_unparsed_fail_open() -> TestResult {
    let dir = repo()?;

    let output = hook(
        dir.path(),
        &patch_payload(&format!("*** Delete File: {PROTECTED}\n")),
        Some("off"),
    )?;

    assert!(
        !output.contains(FAIL_OPEN),
        "the parser must RECOGNIZE the delete, not decline to judge it: {output}"
    );
    Ok(())
}

#[test]
fn deleting_a_committed_test_is_denied() -> TestResult {
    let dir = repo()?;

    let output = hook(
        dir.path(),
        &patch_payload(&format!("*** Delete File: {COMMITTED_TEST}\n")),
        Some("off"),
    )?;

    assert!(
        output.contains(RULE_READ_ONLY),
        "N10 covers deleting a committed suite, not just editing it: {output}"
    );
    Ok(())
}

/// The other half — a gate that denies every delete is not a fix.
#[test]
fn deleting_an_ordinary_file_is_allowed() -> TestResult {
    let dir = repo()?;

    let output = hook(
        dir.path(),
        &patch_payload(&format!("*** Delete File: {ORDINARY}\n")),
        Some("off"),
    )?;

    assert!(
        !output.contains(RULE_PROTECTED)
            && !output.contains(RULE_READ_ONLY)
            && !output.contains(RULE_CONTENT_UNAVAILABLE),
        "an ungated delete has nothing to object to: {output}"
    );
    Ok(())
}

/// A delete of a MAPPED path must not be refused under `content_unavailable`.
/// That rule says a content requirement could not be evaluated — but the file
/// is going away, so no content rule applies to it at all. Refusing with a
/// reason that does not hold is the defect class this project keeps closing.
#[test]
fn deleting_a_mapped_file_is_not_refused_as_content_unavailable() -> TestResult {
    let dir = repo()?;

    let output = hook(
        dir.path(),
        &patch_payload(&format!("*** Delete File: {MAPPED}\n")),
        Some("off"),
    )?;

    assert!(
        !output.contains(RULE_CONTENT_UNAVAILABLE),
        "a delete has no content to evaluate, so this refusal would misdescribe \
         its own reason: {output}"
    );
    Ok(())
}

// ---------------------------------------------------------------------------
// Move to — the destination nobody gated
// ---------------------------------------------------------------------------

/// Renaming an ungated file ONTO a protected path installs agent-authored
/// content at that path. The source was gated; the destination never was.
#[test]
fn renaming_onto_a_protected_path_is_denied() -> TestResult {
    let dir = repo()?;

    let output = hook(
        dir.path(),
        &patch_payload(&format!(
            "*** Update File: {ORDINARY}\n*** Move to: {PROTECTED}\n@@\n-notes\n+tampered\n"
        )),
        Some("off"),
    )?;

    assert!(
        output.contains(RULE_PROTECTED),
        "the rename DESTINATION is a write target and must be gated: {output}"
    );
    Ok(())
}

#[test]
fn renaming_onto_a_committed_test_is_denied() -> TestResult {
    let dir = repo()?;

    let output = hook(
        dir.path(),
        &patch_payload(&format!(
            "*** Update File: {ORDINARY}\n*** Move to: {COMMITTED_TEST}\n@@\n-notes\n+tampered\n"
        )),
        Some("off"),
    )?;

    assert!(
        output.contains(RULE_READ_ONLY),
        "overwriting a committed suite by rename is still overwriting it: {output}"
    );
    Ok(())
}

/// The source half must keep working — moving a protected file AWAY is equally
/// a change to the gate surface.
#[test]
fn renaming_a_protected_file_away_is_denied() -> TestResult {
    let dir = repo()?;

    let output = hook(
        dir.path(),
        &patch_payload(&format!(
            "*** Update File: {PROTECTED}\n*** Move to: {ORDINARY}\n@@\n-a\n+b\n"
        )),
        Some("off"),
    )?;

    assert!(
        output.contains(RULE_PROTECTED),
        "the source is still the protected path: {output}"
    );
    Ok(())
}

/// And an ordinary rename must not be denied on account of the new marker.
#[test]
fn an_ordinary_rename_is_not_denied_on_path_rules() -> TestResult {
    let dir = repo()?;

    let output = hook(
        dir.path(),
        &patch_payload(&format!(
            "*** Update File: {ORDINARY}\n*** Move to: docs/renamed.md\n@@\n-notes\n+edited\n"
        )),
        Some("off"),
    )?;

    assert!(
        !output.contains(RULE_PROTECTED) && !output.contains(RULE_READ_ONLY),
        "neither path is gated: {output}"
    );
    Ok(())
}

// ---------------------------------------------------------------------------
// Mixed patches, and warm/cold
// ---------------------------------------------------------------------------

/// A patch carrying both a delete and an add must not let the ADD escape the
/// content rules. One `ToolAction` carries one intent for every file it names,
/// so the intent chosen for a mixed patch must be the one that judges most.
#[test]
fn a_patch_mixing_a_delete_and_an_add_still_judges_the_added_content() -> TestResult {
    let dir = repo()?;

    let output = hook(
        dir.path(),
        &patch_payload(&format!(
            "*** Delete File: {ORDINARY}\n*** Add File: app/api/new/route.ts\n\
             +export async function POST(req: Request) {{\n\
             +  const body = await req.json();\n\
             +  return Response.json(body);\n\
             +}}\n"
        )),
        Some("off"),
    )?;

    assert!(
        output.contains(RULE_UNVALIDATED),
        "the added handler parses no contract and must still be caught: {output}"
    );
    Ok(())
}

/// A delete alongside an Update must not let the Update half escape judgement.
///
/// This assertion was STRENGTHENED when F52's hunk arm landed. It used to
/// require the Update's interim REFUSAL, which was the strongest verdict
/// available while hunks were unreconstructable. The hunk is reconstructed now,
/// so the test demands the real thing: the violation the update actually
/// introduces, judged under the content rule rather than deferred.
#[test]
fn a_delete_alongside_an_update_still_judges_the_update() -> TestResult {
    let dir = repo()?;

    let output = hook(
        dir.path(),
        &patch_payload(&format!(
            "*** Delete File: {ORDINARY}\n\
             *** Update File: {MAPPED}\n\
             @@\n\
             -export const handler = 1;\n\
             +export async function POST(req: Request) {{\n\
             +  const body = await req.json();\n\
             +  return Response.json(body);\n\
             +}}\n"
        )),
        Some("off"),
    )?;

    assert!(
        output.contains(RULE_UNVALIDATED),
        "the update introduces an unvalidated handler and must be caught: {output}"
    );
    assert!(
        !output.contains(RULE_CONTENT_UNAVAILABLE),
        "and judged, not deferred — the hunk is reconstructable now: {output}"
    );
    Ok(())
}

// F76 ruling 1C: the "delete verdict is identical warm and cold" test that
// lived here was deleted — a Delete never reaches the daemon (`check_or_cold`
// is Write-only), so the assertion compared two runs of the same cold code.
// The verdict it re-asserted is pinned discriminately by
// `deleting_a_protected_file_is_denied` and its siblings above.

/// Regression: an Add-only patch still carries whole content and is still
/// judged on it.
#[test]
fn an_add_only_patch_is_unaffected() -> TestResult {
    let dir = repo()?;

    let output = hook(
        dir.path(),
        &patch_payload(
            "*** Add File: app/api/other/route.ts\n\
             +export async function POST(req: Request) {\n\
             +  const body = await req.json();\n\
             +  return Response.json(body);\n\
             +}\n",
        ),
        Some("off"),
    )?;

    assert!(
        output.contains(RULE_UNVALIDATED),
        "Add sections are whole files and keep their content verdict: {output}"
    );
    Ok(())
}