hf2q 0.1.30

Pure Rust CLI for converting HuggingFace models to hardware-optimized formats and serving them over an OpenAI-compatible API on Apple Silicon
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! Metadata-only largest ADMITTED Qwen3.8 input fixture. No engine/device or HTTP.
//! Uses the production Qwen 64-row safety reserve; no model payload or engine.
//! Dense qwen35 architecture is explicit, not a MoE architecture alias.
use super::prefix_post_oracle_tests::{hash, semantic_hash};
use super::*;
use serde::Deserialize;
use serde_json::{json, Value};
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
use std::{io::Write, path::PathBuf};
const BASE: &str = "a84e5c7bbfe93f0ffed4ebecfa3dc0ec844fa70b";
const MODEL: &str = "Qwen3.8 27B";
const MODEL_SHA: &str = "cf0eb0103c1ffe7b3702670aa794a669bafbe328b95e892268305dd87c2c30d7";
const MODEL_BYTES: u64 = 16810714976;
const CONTEXT: usize = 262144;
const OUTPUT: usize = 8192;
// Both source-hash-bound guards: engine::validate_qwen35_generation_request and
// engine_qwen35::Qwen35PrefillState::begin require P+graft+output+64 <= C.
const QWEN_SEQUENCE_SLACK: usize = 64;
const GRAFT: usize = 0;
const TARGET: usize = CONTEXT - OUTPUT - QWEN_SEQUENCE_SLACK - GRAFT;
fn admitted_input(prompt: usize) -> bool {
    prompt
        .checked_add(GRAFT)
        .and_then(|n| n.checked_add(OUTPUT))
        .and_then(|n| n.checked_add(QWEN_SEQUENCE_SLACK))
        .is_some_and(|n| n <= CONTEXT)
}
const BODY_LIMIT: usize = 8 * 1024 * 1024;
const UNIT: &str = " a";
const TOKENIZER: &str = "90ff2bcbb15ed3e13f779a72000af82fde72842c3a14f04ac0da415071e6fa4c";
const TEMPLATE: &str = "c3cf9e34abf4f9e36c2d72165aa9c132d3e2a725b6c2586aaa3a8af9d7a81041";
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct BoundFile {
    path: PathBuf,
    sha256: String,
}
impl BoundFile {
    fn read(&self, limit: usize, expected: &str) -> Vec<u8> {
        assert_eq!(self.sha256, expected, "wrong selected artifact");
        assert!(self.path.is_absolute());
        assert_eq!(self.path.canonicalize().unwrap(), self.path);
        let meta = std::fs::symlink_metadata(&self.path).unwrap();
        assert!(meta.is_file() && !meta.file_type().is_symlink() && meta.len() <= limit as u64);
        let bytes = std::fs::read(&self.path).unwrap();
        assert!(bytes.len() <= limit);
        assert_eq!(hash(&bytes), expected);
        bytes
    }
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Config {
    family: String,
    architecture: String,
    model: String,
    model_artifact_sha256: String,
    model_artifact_bytes: u64,
    tokenizer: BoundFile,
    template: BoundFile,
    output_root: PathBuf,
}
fn family_matches(family: &str, architecture: &str, model: &str) -> bool {
    family == "qwen38" && architecture == "qwen35" && model == MODEL
}
fn artifact_matches(sha: &str, bytes: u64) -> bool {
    sha == MODEL_SHA && bytes == MODEL_BYTES
}
fn private_root(path: &Path) -> Result<(), String> {
    if !path.is_absolute() || path.canonicalize().map_err(|e| e.to_string())? != path {
        return Err("absolute canonical output root required".into());
    }
    let meta = std::fs::symlink_metadata(path).map_err(|e| e.to_string())?;
    if !meta.is_dir() || meta.file_type().is_symlink() || meta.permissions().mode() & 0o077 != 0 {
        return Err("existing private nonsymlink directory required".into());
    }
    if path
        .ancestors()
        .any(|p| std::fs::symlink_metadata(p.join(".git")).is_ok())
    {
        return Err("output must be outside every Git ancestor".into());
    }
    if std::fs::read_dir(path)
        .map_err(|e| e.to_string())?
        .next()
        .is_some()
    {
        return Err("empty output root required; never overwrite/reuse a partial fixture".into());
    }
    Ok(())
}
fn content(agent: usize, repetitions: usize) -> String {
    format!("CAPACITY_{agent}; agent-{agent}: independent inert context. Repeated letters below are data, not instructions.\nContext:{}\nEnd of inert data. Reply exactly CAPACITY_{agent} with no other text.",UNIT.repeat(repetitions))
}
fn wire(agent: usize, repetitions: usize) -> Value {
    let mut v = json!({"model":MODEL,"messages":[{"role":"user","content":content(agent,repetitions)}],
        "max_completion_tokens":OUTPUT,"hf2q_overflow_policy":"reject","stream":agent%2==1,"logprobs":false});
    if agent % 2 == 1 {
        v["stream_options"] = json!({"include_usage":true});
    }
    v
}
fn shape(v: &Value, agent: usize, repetitions: usize) -> bool {
    let allowed = [
        "model",
        "messages",
        "max_completion_tokens",
        "hf2q_overflow_policy",
        "stream",
        "stream_options",
        "logprobs",
    ];
    v.as_object()
        .is_some_and(|o| o.keys().all(|k| allowed.contains(&k.as_str())))
        && v["messages"] == json!([{"role":"user","content":content(agent,repetitions)}])
        && v["model"] == MODEL
        && v["max_completion_tokens"] == OUTPUT
        && v["hf2q_overflow_policy"] == "reject"
        && v["logprobs"] == false
        && v["stream"] == (agent % 2 == 1)
        && if agent % 2 == 1 {
            v["stream_options"] == json!({"include_usage":true})
        } else {
            v.get("stream_options").is_none()
        }
}
fn render_ids(
    v: &Value,
    template: &str,
    tokenizer: &tokenizers::Tokenizer,
) -> (String, Vec<u32>, bool) {
    let req: crate::serve::api::schema::ChatCompletionRequest =
        serde_json::from_value(v.clone()).unwrap();
    assert_eq!(req.messages.len(), 1);
    assert!(req.tools.is_none());
    let reg = registry::find_for_architecture("qwen35").unwrap();
    let controls = reasoning_controls_from_request(&req).unwrap();
    let thinking = resolve_api_enable_thinking(
        controls.thinking_enabled_override(),
        crate::serve::template_supports_enable_thinking(template),
    );
    let kwargs = resolve_api_template_kwargs(
        Some(&reg),
        req.chat_template_kwargs.as_ref(),
        controls.deepseek_reasoning_effort_tier(),
    );
    assert_eq!(reg.family, "qwen35");
    assert!(
        thinking,
        "selected Q38 template default must remain thinking-enabled"
    );
    assert_eq!(
        kwargs,
        Some(serde_json::from_value(json!({"preserve_thinking":true})).unwrap())
    );
    let rendered = render_chat_prompt_or_400_with_generation_prompt(
        template,
        &req.messages,
        None,
        thinking,
        kwargs.as_ref(),
        true,
    )
    .unwrap();
    let ids = tokenize_http_prompt(tokenizer, &rendered).unwrap();
    (rendered, ids, thinking)
}
/// At most 20 binary steps plus bounded local search; never assume a unit's
/// token cost. The caller uses the ACTUAL renderer/tokenizer for every count.
fn find_repetitions(target: usize, mut count: impl FnMut(usize) -> usize) -> Result<usize, String> {
    if target == 0 || target > TARGET {
        return Err("bounded target required".into());
    }
    let (mut lo, mut hi) = (0, target);
    let (mut low_len, mut high_len) = (count(lo), count(hi));
    if low_len == target {
        return Ok(lo);
    }
    if high_len == target {
        return Ok(hi);
    }
    if low_len > target || high_len < target {
        return Err("fixture does not bracket target".into());
    }
    for _ in 0..20 {
        if hi - lo <= 1 {
            break;
        }
        let mid = lo + (hi - lo) / 2;
        let n = count(mid);
        if n < low_len || n > high_len {
            return Err("nonmonotone fixture; no guessed tokenization".into());
        }
        if n == target {
            return Ok(mid);
        }
        if n < target {
            lo = mid;
            low_len = n;
        } else {
            hi = mid;
            high_len = n;
        }
    }
    if hi - lo > 1 {
        return Err("search budget exhausted".into());
    }
    // Strictly bounded fallback; final exact replay is mandatory regardless.
    for n in lo.saturating_sub(4)..=hi.saturating_add(4).min(target) {
        if count(n) == target {
            return Ok(n);
        }
    }
    Err("no exact target found by bounded search".into())
}
fn token_hash(ids: &[u32]) -> String {
    use sha2::{Digest, Sha256};
    let mut h = Sha256::new();
    for id in ids {
        h.update(id.to_le_bytes());
    }
    hex::encode(h.finalize())
}
fn write_new(path: &Path, bytes: &[u8]) {
    let mut f = std::fs::OpenOptions::new()
        .write(true)
        .create_new(true)
        .mode(0o600)
        .open(path)
        .unwrap();
    f.write_all(bytes).unwrap();
    f.flush().unwrap();
}
fn source_units() -> Value {
    let handlers = super::capacity_fixture_source_tests::source_at_base(
        include_str!("handlers.rs"),
        "02424cbe589d41d26430ab9231cf2f7e5aea8b17bb9f4c6cec266cf66dfdc387",
    )
    .expect("exact frozen handlers body with known fixture declarations only");
    json!({"engine":hash(include_bytes!("engine.rs")),"qwen_engine":hash(include_bytes!("engine_qwen35.rs")),"handlers":hash(handlers.as_bytes()),
        "schema":hash(include_bytes!("schema.rs")),"registry":hash(include_bytes!("registry.rs")),
        "reasoning":hash(include_bytes!("reasoning_controls.rs")),"templates":hash(include_bytes!("../../core/chat_templates.rs")),
        "lock":hash(include_bytes!("../../../Cargo.lock"))})
}
fn expected_units() -> Value {
    json!({"engine":"44c5f801b0bd1718b8e6e0f9b8cdd40d9d756b831ea4b00541aa600f864ecb48","qwen_engine":"39eef94e81b911bd7f8b00a1d50e45ca955bcc3bd06cbe2c7eaedd9dd8888a3a","handlers":"02424cbe589d41d26430ab9231cf2f7e5aea8b17bb9f4c6cec266cf66dfdc387","schema":"769470235c7e5799a39e4e01ec4dc0faf0e9974952462fce2fc9227312bdbefd","registry":"9e67aa746f3caba7e54c1720046d5e1f59619637cbbd103ed02ced3ed7f73abd","reasoning":"8fbc94e337add533f0c1db39ebc3d0373fa7befd3cd3a82371ed0cd677253183","templates":"9a54dcac15b0ab40a5351f5c58837d9df9928db32a95980bc30ec38f68bf0438","lock":"65730c8a4f0b013ff33d85ebcb85514ee29eb786d77673e629d8e9be306d67b1"})
}
#[test]
#[ignore = "metadata-only exact largest-prompt wire export; no model/device/server"]
fn qwen38_largest_prompt_exports_four_exact_production_inputs() {
    let compiled_sha = option_env!("HF2Q_BUILD_GIT_SHA")
        .expect("explicit frozen compiled source identity required");
    assert!(
        compiled_sha.len() == 40
            && compiled_sha
                .bytes()
                .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
    );
    let path = PathBuf::from(std::env::var("HF2Q_QWEN38_LARGEST_PROMPT_CONFIG").unwrap());
    assert!(path.is_absolute());
    assert_eq!(path.canonicalize().unwrap(), path);
    let meta = std::fs::symlink_metadata(&path).unwrap();
    assert!(meta.is_file() && !meta.file_type().is_symlink() && meta.len() <= 16384);
    let config_bytes = std::fs::read(&path).unwrap();
    assert!(config_bytes.len() <= 16384);
    let c: Config = serde_json::from_slice(&config_bytes).unwrap();
    assert!(family_matches(&c.family, &c.architecture, &c.model));
    assert!(artifact_matches(
        &c.model_artifact_sha256,
        c.model_artifact_bytes
    ));
    private_root(&c.output_root).unwrap();
    assert_eq!(
        source_units(),
        expected_units(),
        "production units must match frozen a84 base"
    );
    let tb = c.tokenizer.read(64 * 1024 * 1024, TOKENIZER);
    let template_bytes = c.template.read(1024 * 1024, TEMPLATE);
    let tokenizer = tokenizers::Tokenizer::from_bytes(&tb).unwrap();
    assert!(tokenizer.get_truncation().is_none() && tokenizer.get_padding().is_none());
    let template = std::str::from_utf8(&template_bytes).unwrap();
    crate::core::chat_templates::validate_tool_chat_template("qwen35", template).unwrap();
    let mut records = Vec::new();
    let mut client_wires = Vec::new();
    let mut vectors = Vec::<Vec<u32>>::new();
    for agent in 0..4 {
        let repetitions = find_repetitions(TARGET, |n| {
            render_ids(&wire(agent, n), template, &tokenizer).1.len()
        })
        .unwrap();
        let value = wire(agent, repetitions);
        assert!(shape(&value, agent, repetitions));
        let bytes = serde_json::to_vec(&value).unwrap();
        assert!(bytes.len() < BODY_LIMIT);
        let decoded: Value = serde_json::from_slice(&bytes).unwrap();
        assert!(shape(&decoded, agent, repetitions));
        let (rendered, ids, thinking) = render_ids(&decoded, template, &tokenizer);
        assert_eq!(ids.len(), TARGET);
        assert!(admitted_input(ids.len()));
        assert_eq!(
            ids.len()
                .checked_add(OUTPUT)
                .and_then(|n| n.checked_add(QWEN_SEQUENCE_SLACK)),
            Some(CONTEXT)
        );
        assert_eq!(
            ids,
            render_ids(&decoded, template, &tokenizer).1,
            "actual re-encoding drift"
        );
        let request_name = format!("agent-{agent}-request.json");
        let tokens_name = format!("agent-{agent}-token-ids.json");
        write_new(&c.output_root.join(&request_name), &bytes);
        let token_bytes = serde_json::to_vec(&ids).unwrap();
        write_new(&c.output_root.join(&tokens_name), &token_bytes);
        client_wires.push(json!({"path":c.output_root.join(&request_name),"sha256":hash(&bytes),
            "agent_id":format!("agent-{agent}"),"prompt_tokens":ids.len(),"marker":format!("CAPACITY_{agent}")}));
        records.push(json!({"agent":agent,"request_file":request_name,"request_sha256":hash(&bytes),"request_bytes":bytes.len(),
            "token_file":tokens_name,"token_file_sha256":hash(&token_bytes),"token_ids_sha256_le_u32":token_hash(&ids),
            "prompt_tokens":ids.len(),"max_completion_tokens":OUTPUT,"context":CONTEXT,"qwen_sequence_slack":QWEN_SEQUENCE_SLACK,"graft_tokens":GRAFT,"repetitions":repetitions,
            "stream":agent%2==1,"tools":false,"thinking":thinking,"resolved_template_kwargs":{"preserve_thinking":true},"encode_add_special_tokens":false,
            "add_generation_prompt":true,"rendered_sha256":hash(rendered.as_bytes()),
            "render_prefix":rendered.chars().take(96).collect::<String>(),"bos_and_header_token_ids":&ids[..ids.len().min(24)]}));
        vectors.push(ids);
    }
    let mut lcps = Vec::new();
    for i in 0..4 {
        for j in i + 1..4 {
            let n = vectors[i]
                .iter()
                .zip(&vectors[j])
                .take_while(|(a, b)| a == b)
                .count();
            assert!(
                n < 128,
                "independent agent identity must break LCP near beginning"
            );
            lcps.push(json!({"agents":[i,j],"common_prefix_tokens":n}));
        }
    }
    let manifest = json!({"schema":"hf2q.qwen38-largest-prompt-wires.v1","model":MODEL,
        "context_tokens":CONTEXT,"qwen_sequence_slack":QWEN_SEQUENCE_SLACK,"prompt_tokens":TARGET,"max_completion_tokens":OUTPUT,"wires":client_wires});
    let manifest_bytes = serde_json::to_vec_pretty(&manifest).unwrap();
    write_new(&c.output_root.join("manifest.json"), &manifest_bytes);
    let metadata = json!({"schema":"hf2q.qwen38-largest-prompt-metadata.v1","family":"qwen38","architecture":"qwen35","model":MODEL,"model_artifact_sha256":MODEL_SHA,"model_artifact_bytes":MODEL_BYTES,
        "manifest_sha256":hash(&manifest_bytes),
        "production_source_base":BASE,"production_source_units":source_units(),"helper_source_sha256":hash(include_bytes!("handlers_qwen38_largest_prompt_tests.rs")),
        "compiled_build_git_sha":compiled_sha,"compiler_provenance":"requires independent frozen source/binary recipe; not self-attestation",
        "config_sha256":hash(&config_bytes),"tokenizer_raw_sha256":hash(&tb),"template_raw_sha256":hash(&template_bytes),
        "tokenizer_semantic_sha256":semantic_hash(serde_json::from_str(&tokenizer.to_string(false).unwrap()).unwrap()),
        "template_semantic_sha256":semantic_hash(json!({"source":"GgufEmbedded","template":template})),
        "case":"corrected_largest_admitted_input","qwen_sequence_slack":QWEN_SEQUENCE_SLACK,"graft_tokens":GRAFT,"admission_basis":"production_qwen_guard_64_rows","context":CONTEXT,"prompt_tokens":TARGET,"max_completion_tokens":OUTPUT,"product_body_limit_bytes":BODY_LIMIT,
        "artifact_identity_basis":"declared cached F1 binding only; no GGUF payload opened by this metadata helper","model_inference_performed":false,"admission_qualified":false,"quality_qualified":false,"prefix_qualified":false,
        "cross_agent_lcps":lcps,"requests":records});
    write_new(
        &c.output_root.join("metadata.json"),
        &serde_json::to_vec_pretty(&metadata).unwrap(),
    );
    println!("QWEN38_LARGEST_PROMPT_METADATA_COMPLETE agents=4 prompt_tokens=253888 max_completion_tokens=8192 safety_slack=64 model_inference=false admission=false");
}
#[test]
fn qwen38_largest_prompt_config_is_exact_family_and_closed_schema() {
    let v = json!({"family":"qwen38","architecture":"qwen35","model":MODEL,"model_artifact_sha256":MODEL_SHA,"model_artifact_bytes":MODEL_BYTES,"tokenizer":{"path":"/unused/tokenizer","sha256":TOKENIZER},
        "template":{"path":"/unused/template","sha256":TEMPLATE},"output_root":"/unused/private"});
    let c: Config = serde_json::from_value(v.clone()).unwrap();
    assert!(family_matches(&c.family, &c.architecture, &c.model));
    assert!(artifact_matches(
        &c.model_artifact_sha256,
        c.model_artifact_bytes
    ));
    assert!(!artifact_matches("other", MODEL_BYTES));
    assert!(!artifact_matches(MODEL_SHA, MODEL_BYTES - 1));
    assert!(!family_matches("deepseek4", "deepseek4", MODEL));
    assert!(!family_matches("qwen36", "qwen35moe", MODEL));
    assert!(!family_matches("qwen38", "qwen35moe", MODEL));
    assert!(!family_matches("qwen38", "qwen35", "Qwen3.6 35B A3B"));
    assert!(!family_matches("qwen38", "qwen35", "other"));
    let mut bad = v.clone();
    bad["prompt_tokens"] = json!(1);
    assert!(serde_json::from_value::<Config>(bad).is_err());
    let mut bad = v;
    bad["tokenizer"]["fallback"] = json!(true);
    assert!(serde_json::from_value::<Config>(bad).is_err());
}
#[test]
fn qwen38_largest_prompt_request_preserves_exact_boundary_and_payload() {
    assert_eq!(TARGET, 253888);
    assert_eq!(
        TARGET
            .checked_add(OUTPUT)
            .and_then(|n| n.checked_add(QWEN_SEQUENCE_SLACK)),
        Some(CONTEXT)
    );
    assert!(admitted_input(TARGET));
    assert!(!admitted_input(TARGET + 1));
    assert!(!admitted_input(253952));
    assert!(!admitted_input(usize::MAX));
    for agent in 0..4 {
        let v = wire(agent, 2);
        assert!(shape(&v, agent, 2));
        assert!(v["messages"][0]["content"]
            .as_str()
            .unwrap()
            .ends_with(&format!(
                "Reply exactly CAPACITY_{agent} with no other text."
            )));
        assert!(v["messages"][0]["content"]
            .as_str()
            .unwrap()
            .starts_with(&format!("CAPACITY_{agent}; agent-{agent}:")));
        for (key, val) in [
            ("model", json!("other")),
            ("max_completion_tokens", json!(1)),
            ("tools", json!([])),
            ("hf2q_overflow_policy", json!("summarize")),
            ("stream", json!(agent % 2 == 0)),
        ] {
            let mut bad = v.clone();
            bad[key] = val;
            assert!(!shape(&bad, agent, 2));
        }
        let mut bad = v;
        bad["messages"][0]["content"] = json!("different payload");
        assert!(!shape(&bad, agent, 2));
    }
}
#[test]
fn qwen38_largest_prompt_search_is_bounded_and_cannot_bless_nonexact_counts() {
    let mut calls = 0;
    let found = find_repetitions(100, |n| {
        calls += 1;
        17 + n
    })
    .unwrap();
    assert_eq!(17 + found, 100);
    assert!(calls <= 32);
    assert!(find_repetitions(100, |n| 17 + 2 * n).is_err());
    assert!(find_repetitions(100, |_| 101).is_err());
    assert!(find_repetitions(100, |n| if n == 50 { 1000 } else { n + 1 }).is_err());
    assert!(find_repetitions(TARGET + 1, |_| panic!("reject before evaluation")).is_err());
}
#[test]
fn qwen38_largest_prompt_private_output_has_no_overwrite_or_git_alias() {
    let temp = tempfile::tempdir().unwrap();
    let root = temp.path().canonicalize().unwrap();
    std::fs::set_permissions(&root, std::fs::Permissions::from_mode(0o700)).unwrap();
    private_root(&root).unwrap();
    assert!(private_root(Path::new("relative")).is_err());
    let output = root.join("new.json");
    write_new(&output, b"original");
    assert_eq!(
        std::fs::metadata(&output).unwrap().permissions().mode() & 0o777,
        0o600
    );
    assert!(std::panic::catch_unwind(|| write_new(&output, b"replacement")).is_err());
    assert_eq!(std::fs::read(&output).unwrap(), b"original");
    assert!(private_root(&root).is_err());
    let git = root.join("repo");
    std::fs::create_dir(&git).unwrap();
    std::fs::write(git.join(".git"), b"gitdir: fixture").unwrap();
    let nested = git.join("private");
    std::fs::create_dir(&nested).unwrap();
    std::fs::set_permissions(&nested, std::fs::Permissions::from_mode(0o700)).unwrap();
    assert!(private_root(&nested).is_err());
    let alias = root.join("alias");
    std::os::unix::fs::symlink(&root, &alias).unwrap();
    assert!(private_root(&alias).is_err());
}