visioncraft 0.3.0

Explicit quality plans, bounded execution receipts and deterministic evidence gates
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
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
//! Executed Deepsec 2.3.10 gates with explicit policy and retained error states.
//!
//! This adapter knows no Agentbox manifest, memory service or network address.
//! Hosts project their policy into [`crate::deepsec::DeepsecPolicy`]. Credentials remain named
//! environment variables and are passed only to the model-processing child.

use crate::process::{Termination, private_directory, private_file, run_command_with_env};
use crate::{Invocation, from_json, sha256};
use clap::{ArgGroup, Args};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use std::collections::BTreeMap;
use std::fs::{self, File, OpenOptions};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

#[path = "deepsec_evidence.rs"]
mod recorded;

static PROTOCOL: std::sync::LazyLock<Value> = std::sync::LazyLock::new(|| {
    serde_json::from_str(include_str!("../contracts/deepsec.json"))
        .expect("checked-in Deepsec release contract must be valid JSON")
});
fn supported_version() -> &'static str {
    PROTOCOL["package"]["version"]
        .as_str()
        .expect("release contract version")
}
const SEVERITIES: [&str; 6] = ["CRITICAL", "HIGH", "MEDIUM", "HIGH_BUG", "BUG", "LOW"];
static ATTEMPT: AtomicU64 = AtomicU64::new(0);

/// Host-projected policy. Values are configuration; credential values are absent.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct DeepsecPolicy {
    /// Whether paid/model processing is allowed at all.
    pub enabled: bool,
    /// Deepsec agent plugin: claude, codex or pi.
    pub agent: String,
    /// Authentication route: local, direct or custom.
    pub model_auth: String,
    /// Optional model override; empty leaves selection to the pinned tool.
    pub model: String,
    /// Deepsec thinking-level parameter.
    pub thinking_level: String,
    /// Inclusive severity threshold; lower findings remain visible.
    pub fail_on: String,
    /// Whole attempt deadline, including probes/export (for example `45m`).
    pub max_duration: String,
    /// Files per model batch.
    pub batch_size: u32,
    /// Maximum concurrent batches inside Deepsec.
    pub concurrency: u32,
    /// Provider identifier for direct/custom authentication.
    pub ai_provider: String,
    /// Name of a credential environment variable, never its value.
    pub ai_api_key_env: String,
    /// Optional custom/direct base URL, without credentials or query strings.
    pub ai_base_url: String,
    /// Non-secret policy origin for receipts.
    pub policy_source: String,
    /// Combined stdout/stderr bound for each child, at most one GiB.
    pub max_output_bytes: u64,
}

impl Default for DeepsecPolicy {
    fn default() -> Self {
        Self {
            enabled: true,
            agent: "claude".into(),
            model_auth: "local".into(),
            model: String::new(),
            thinking_level: "high".into(),
            fail_on: "HIGH".into(),
            max_duration: "45m".into(),
            batch_size: 5,
            concurrency: 2,
            ai_provider: String::new(),
            ai_api_key_env: String::new(),
            ai_base_url: String::new(),
            policy_source: "defaults".into(),
            max_output_bytes: 16 * 1024 * 1024,
        }
    }
}

/// Adapter input/runtime error with the documented legacy-compatible exit code.
#[derive(Debug, thiserror::Error)]
#[error("{message}")]
pub struct GateError {
    /// 70 runtime, 78 configuration/unavailable, 124 timeout, or 130 interruption.
    pub code: i32,
    /// Message deliberately excludes raw configuration and credential values.
    pub message: String,
}

fn error(code: i32, message: impl Into<String>) -> GateError {
    GateError {
        code,
        message: message.into(),
    }
}
fn io_error(_: std::io::Error) -> GateError {
    error(70, "evidence filesystem operation failed")
}

fn duration_ms(text: &str) -> Option<u64> {
    let (number, scale) = if let Some(n) = text.strip_suffix("ms") {
        (n, 1_u64)
    } else if let Some(n) = text.strip_suffix('s') {
        (n, 1000)
    } else if let Some(n) = text.strip_suffix('m') {
        (n, 60_000)
    } else if let Some(n) = text.strip_suffix('h') {
        (n, 3_600_000)
    } else if let Some(n) = text.strip_suffix('d') {
        (n, 86_400_000)
    } else {
        (text, 1000)
    };
    let (whole, fraction) = number.split_once('.').unwrap_or((number, ""));
    if whole.is_empty()
        || !whole.bytes().all(|c| c.is_ascii_digit())
        || fraction.len() > 3
        || !fraction.bytes().all(|c| c.is_ascii_digit())
    {
        return None;
    }
    let base = whole.parse::<u64>().ok()?.checked_mul(scale)?;
    let part = if fraction.is_empty() {
        0
    } else {
        fraction.parse::<u64>().ok()?.checked_mul(scale)? / 10_u64.pow(fraction.len() as u32)
    };
    base.checked_add(part)
        .filter(|ms| (1..=86_400_000).contains(ms))
}

impl DeepsecPolicy {
    /// Validate policy and return the whole-attempt deadline in milliseconds.
    /// No environment is read and no process is started.
    pub fn validate(&self) -> Result<u64, GateError> {
        if !["claude", "codex", "pi"].contains(&self.agent.as_str()) {
            return Err(error(78, "unsupported Deepsec agent"));
        }
        if !["local", "direct", "custom"].contains(&self.model_auth.as_str()) {
            return Err(error(78, "unsupported model authentication route"));
        }
        if !["minimal", "low", "medium", "high", "xhigh"].contains(&self.thinking_level.as_str()) {
            return Err(error(78, "unsupported thinking level"));
        }
        if !SEVERITIES.contains(&self.fail_on.as_str()) {
            return Err(error(78, "unsupported severity threshold"));
        }
        if !(1..=100).contains(&self.batch_size)
            || !(1..=32).contains(&self.concurrency)
            || !(1..=1_073_741_824).contains(&self.max_output_bytes)
        {
            return Err(error(78, "invalid batch, concurrency or output bound"));
        }
        if [&self.model, &self.policy_source, &self.ai_provider]
            .iter()
            .any(|x| x.contains('\0'))
        {
            return Err(error(78, "invalid policy string"));
        }
        if !self.ai_api_key_env.is_empty()
            && !(self
                .ai_api_key_env
                .as_bytes()
                .first()
                .is_some_and(|c| c.is_ascii_alphabetic() || *c == b'_')
                && self
                    .ai_api_key_env
                    .bytes()
                    .all(|c| c.is_ascii_alphanumeric() || c == b'_'))
        {
            return Err(error(78, "invalid credential environment name"));
        }
        if !self.ai_base_url.is_empty() {
            let url = url::Url::parse(&self.ai_base_url)
                .map_err(|_| error(78, "invalid model base URL"))?;
            if !["http", "https"].contains(&url.scheme())
                || url.host_str().is_none()
                || !url.username().is_empty()
                || url.password().is_some()
                || url.query().is_some()
                || url.fragment().is_some()
            {
                return Err(error(
                    78,
                    "base URL must be HTTP(S), without credentials, query or fragment",
                ));
            }
        }
        match self.model_auth.as_str() {
            "local" if self.agent == "pi" => {
                return Err(error(78, "local authentication requires claude or codex"));
            }
            "direct"
                if !["anthropic", "openai"].contains(&self.ai_provider.as_str())
                    || self.ai_api_key_env.is_empty() =>
            {
                return Err(error(
                    78,
                    "direct authentication requires a supported provider and credential name",
                ));
            }
            "custom"
                if self.agent != "pi"
                    || self.ai_api_key_env.is_empty()
                    || self.ai_base_url.is_empty() =>
            {
                return Err(error(
                    78,
                    "custom authentication requires pi, a credential name and a base URL",
                ));
            }
            _ => (),
        }
        duration_ms(&self.max_duration).ok_or_else(|| {
            error(
                78,
                "invalid duration; use positive seconds/ms/m/h/d up to one day",
            )
        })
    }
}

/// Deepsec gate flags. Mutually exclusive source modes are checked by Clap.
#[derive(Debug, Clone, Args)]
#[command(group(ArgGroup::new("source").required(true).multiple(false).args(["diff", "diff_working", "diff_staged", "files_from", "full", "scan_only"])))]
pub struct DeepsecOptions {
    /// Compare against this Git reference.
    #[arg(long)]
    pub diff: Option<String>,
    /// Review working and untracked changes.
    #[arg(long)]
    pub diff_working: bool,
    /// Review staged changes.
    #[arg(long)]
    pub diff_staged: bool,
    /// Read newline-delimited file paths, or `-` for stdin.
    #[arg(long)]
    pub files_from: Option<PathBuf>,
    /// Scan the repository and process its pending candidates.
    #[arg(long)]
    pub full: bool,
    /// Scan without model processing; result is SCANNED, not review PASS.
    #[arg(long)]
    pub scan_only: bool,
    /// Source root, otherwise the nearest containing Git worktree or cwd.
    #[arg(long)]
    pub root: Option<PathBuf>,
    /// Fresh output directory; existing directories are refused.
    #[arg(long)]
    pub report_dir: Option<PathBuf>,
    /// Typed JSON policy from a host adapter; contains credential names only.
    #[arg(long)]
    pub policy_json: Option<String>,
    /// Override the severity threshold.
    #[arg(long)]
    pub fail_on: Option<String>,
    /// Override the thinking level.
    #[arg(long)]
    pub thinking_level: Option<String>,
    /// Override the Deepsec agent.
    #[arg(long)]
    pub agent: Option<String>,
    /// Override the model identifier.
    #[arg(long)]
    pub model: Option<String>,
    /// Override the authentication route.
    #[arg(long)]
    pub model_auth: Option<String>,
    /// Print the resolved plan without running any command or writing evidence.
    #[arg(long)]
    pub dry_run: bool,
}

fn root_for(options: &DeepsecOptions) -> Result<PathBuf, GateError> {
    let path = if let Some(root) = &options.root {
        root.clone()
    } else {
        let cwd = std::env::current_dir().map_err(io_error)?;
        cwd.ancestors()
            .find(|p| p.join(".git").exists())
            .unwrap_or(&cwd)
            .to_owned()
    };
    let path = path
        .canonicalize()
        .map_err(|_| error(78, "source root is unavailable"))?;
    if !path.is_dir() {
        return Err(error(78, "source root must be a directory"));
    }
    Ok(path)
}

fn environment(policy: &DeepsecPolicy, model: bool) -> Vec<String> {
    let mut names: Vec<String> = [
        "PATH",
        "HOME",
        "LANG",
        "LC_ALL",
        "TMPDIR",
        "SSL_CERT_FILE",
        "SSL_CERT_DIR",
        "NODE_EXTRA_CA_CERTS",
        "HTTP_PROXY",
        "HTTPS_PROXY",
        "ALL_PROXY",
        "NO_PROXY",
        "http_proxy",
        "https_proxy",
        "all_proxy",
        "no_proxy",
    ]
    .into_iter()
    .map(str::to_owned)
    .collect();
    if model {
        names.extend(
            ["CLAUDE_CODE_EXECUTABLE", "CLAUDE_CONFIG_DIR", "CODEX_HOME"]
                .into_iter()
                .map(str::to_owned),
        );
        if policy.model_auth == "local" {
            names.push("CLAUDE_CODE_OAUTH_TOKEN".into());
        } else {
            names.push(policy.ai_api_key_env.clone());
        }
    }
    names.sort();
    names.dedup();
    names
}

fn write_new(path: &Path, bytes: &[u8]) -> Result<(), GateError> {
    let parent = path
        .parent()
        .ok_or_else(|| error(70, "evidence path has no parent"))?;
    let mut temporary = tempfile::NamedTempFile::new_in(parent).map_err(io_error)?;
    temporary.write_all(bytes).map_err(io_error)?;
    temporary.as_file().sync_all().map_err(io_error)?;
    temporary
        .persist_noclobber(path)
        .map_err(|_| error(70, "evidence output already exists or cannot be finalised"))?;
    Ok(())
}

fn read_bounded(path: &Path, limit: u64) -> Result<Vec<u8>, GateError> {
    let meta = fs::symlink_metadata(path).map_err(io_error)?;
    if !meta.is_file() || meta.file_type().is_symlink() || meta.len() > limit {
        return Err(error(70, "evidence must be a bounded regular file"));
    }
    let mut bytes = Vec::new();
    File::open(path)
        .map_err(io_error)?
        .take(limit + 1)
        .read_to_end(&mut bytes)
        .map_err(io_error)?;
    if bytes.len() as u64 > limit {
        return Err(error(70, "evidence exceeded its byte bound"));
    }
    Ok(bytes)
}

fn findings(bytes: &[u8]) -> Result<Vec<Value>, GateError> {
    let value: StrictJson = serde_json::from_slice(bytes)
        .map_err(|_| error(70, "malformed or ambiguous findings JSON"))?;
    let value = value.0;
    let rows = value
        .as_array()
        .or_else(|| value.get("findings").and_then(Value::as_array))
        .ok_or_else(|| error(70, "unrecognised findings export shape"))?;
    let mut output = Vec::new();
    for row in rows {
        if row.get("severity").is_some() {
            output.push(row.clone());
        } else if let Some(nested) = row.get("findings").and_then(Value::as_array) {
            for finding in nested {
                let mut object = finding
                    .as_object()
                    .cloned()
                    .ok_or_else(|| error(70, "finding must be an object"))?;
                if let Some(path) = row.get("filePath") {
                    object.entry("filePath").or_insert_with(|| path.clone());
                }
                output.push(Value::Object(object));
            }
        } else {
            return Err(error(70, "unrecognised finding row"));
        }
    }
    for finding in &mut output {
        let severity = finding
            .get("severity")
            .and_then(Value::as_str)
            .ok_or_else(|| error(70, "finding lacks a string severity"))?
            .to_ascii_uppercase();
        if !SEVERITIES.contains(&severity.as_str()) {
            return Err(error(70, "finding has an unrecognised severity"));
        }
        finding["severity"] = Value::String(severity);
    }
    Ok(output)
}

// Findings come from an external protocol. Reject duplicate keys at every level,
// including severity and findings, rather than silently accepting the last key.
struct StrictJson(Value);
impl<'de> Deserialize<'de> for StrictJson {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        struct Visitor;
        impl<'de> serde::de::Visitor<'de> for Visitor {
            type Value = StrictJson;
            fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                f.write_str("unambiguous JSON")
            }
            fn visit_bool<E: serde::de::Error>(self, v: bool) -> Result<StrictJson, E> {
                Ok(StrictJson(Value::Bool(v)))
            }
            fn visit_i64<E: serde::de::Error>(self, v: i64) -> Result<StrictJson, E> {
                Ok(StrictJson(Value::Number(v.into())))
            }
            fn visit_u64<E: serde::de::Error>(self, v: u64) -> Result<StrictJson, E> {
                Ok(StrictJson(Value::Number(v.into())))
            }
            fn visit_f64<E: serde::de::Error>(self, v: f64) -> Result<StrictJson, E> {
                serde_json::Number::from_f64(v)
                    .map(|n| StrictJson(Value::Number(n)))
                    .ok_or_else(|| E::custom("non-finite JSON"))
            }
            fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<StrictJson, E> {
                Ok(StrictJson(Value::String(v.into())))
            }
            fn visit_string<E: serde::de::Error>(self, v: String) -> Result<StrictJson, E> {
                Ok(StrictJson(Value::String(v)))
            }
            fn visit_unit<E: serde::de::Error>(self) -> Result<StrictJson, E> {
                Ok(StrictJson(Value::Null))
            }
            fn visit_seq<A: serde::de::SeqAccess<'de>>(
                self,
                mut seq: A,
            ) -> Result<StrictJson, A::Error> {
                let mut values = Vec::new();
                while let Some(value) = seq.next_element::<StrictJson>()? {
                    values.push(value.0);
                }
                Ok(StrictJson(Value::Array(values)))
            }
            fn visit_map<A: serde::de::MapAccess<'de>>(
                self,
                mut map: A,
            ) -> Result<StrictJson, A::Error> {
                let mut values = serde_json::Map::new();
                while let Some(key) = map.next_key::<String>()? {
                    if values.contains_key(&key) {
                        return Err(serde::de::Error::custom("duplicate JSON key"));
                    }
                    values.insert(key, map.next_value::<StrictJson>()?.0);
                }
                Ok(StrictJson(Value::Object(values)))
            }
        }
        deserializer.deserialize_any(Visitor)
    }
}

struct Attempt<'a> {
    policy: &'a DeepsecPolicy,
    report: &'a Path,
    cancelled: &'a AtomicBool,
    started: Instant,
    deadline: Duration,
    sequence: u32,
}

impl Attempt<'_> {
    fn command(
        &mut self,
        stage: &str,
        program: &str,
        args: Vec<String>,
        cwd: &Path,
        model: bool,
        stdin: bool,
    ) -> Result<(i32, Vec<u8>), GateError> {
        let remaining = self
            .deadline
            .checked_sub(self.started.elapsed())
            .filter(|v| !v.is_zero())
            .ok_or_else(|| error(124, "whole-attempt deadline reached"))?;
        let directory = self.report.join(format!("{:02}-{stage}", self.sequence));
        self.sequence += 1;
        private_directory(&directory).map_err(io_error)?;
        let (args, env, fixed) = if program == "git" {
            let mut hardened = crate::git_metadata::prefix();
            hardened.extend(args);
            (
                hardened,
                vec!["PATH".into()],
                crate::git_metadata::fixed_environment(),
            )
        } else {
            (args, environment(self.policy, model), BTreeMap::new())
        };
        let invocation = Invocation {
            program: program.into(),
            args,
            cwd: ".".into(),
            env,
            timeout_ms: remaining.as_millis().max(1).try_into().unwrap_or(u64::MAX),
            max_output_bytes: self.policy.max_output_bytes,
        };
        write_new(
            &directory.join("invocation.json"),
            &serde_json::to_vec_pretty(&invocation)
                .map_err(|_| error(70, "invocation serialisation failed"))?,
        )?;
        let run = run_command_with_env(&invocation, cwd, &directory, self.cancelled, stdin, &fixed)
            .map_err(io_error)?;
        let stdout = read_bounded(&run.stdout, self.policy.max_output_bytes)?;
        let stderr = read_bounded(&run.stderr, self.policy.max_output_bytes)?;
        let mut log = OpenOptions::new()
            .append(true)
            .open(self.report.join("deepsec.log"))
            .map_err(io_error)?;
        log.write_all(&stdout)
            .and_then(|_| log.write_all(&stderr))
            .map_err(io_error)?;
        let execution = json!({"stage":stage,"process":format!("{:?}",run.termination),"exit_code":run.exit_code,"signal":run.signal,"started_at_ms":run.started_at_ms,"finished_at_ms":run.finished_at_ms,"elapsed_ms":run.elapsed_ms,"stdout_sha256":sha256(&stdout),"stderr_sha256":sha256(&stderr),"error":run.error});
        write_new(
            &directory.join("execution.json"),
            &serde_json::to_vec_pretty(&execution)
                .map_err(|_| error(70, "execution serialisation failed"))?,
        )?;
        match run.termination {
            Termination::Completed if run.signal.is_none() => Ok((
                run.exit_code
                    .ok_or_else(|| error(70, "missing exit status"))?,
                stdout,
            )),
            Termination::Unavailable => Err(error(78, "required executable is unavailable")),
            Termination::TimedOut => Err(error(
                124,
                "whole-attempt deadline reached; checkpoint evidence retained",
            )),
            Termination::Interrupted => Err(error(
                130,
                "review interrupted; checkpoint evidence retained",
            )),
            _ => Err(error(70, "review process or capture did not complete")),
        }
    }
}

#[cfg(unix)]
fn workspace_lock(workspace: &Path) -> Result<nix::fcntl::Flock<File>, GateError> {
    use nix::fcntl::{Flock, FlockArg};
    use std::os::unix::fs::OpenOptionsExt;
    let path = workspace.join(".visioncraft.lock");
    if fs::symlink_metadata(&path).is_ok_and(|m| !m.is_file() || m.file_type().is_symlink()) {
        return Err(error(78, "invalid review workspace lock"));
    }
    let file = OpenOptions::new()
        .create(true)
        .truncate(false)
        .read(true)
        .write(true)
        .mode(0o600)
        .open(path)
        .map_err(io_error)?;
    Flock::lock(file, FlockArg::LockExclusiveNonblock)
        .map_err(|_| error(78, "another review owns this workspace"))
}

#[cfg(unix)]
fn validate_workspace_tree(workspace: &Path) -> Result<(), GateError> {
    use std::os::unix::fs::MetadataExt;
    // Deepsec owns durable checkpoint state here. Reject redirected/special
    // entries before passing that tree to an external tool with credentials.
    // This does not isolate concurrent writers running as the same Unix user.
    let mut pending = vec![(workspace.to_owned(), 0_usize)];
    let mut count = 0_usize;
    while let Some((directory, depth)) = pending.pop() {
        if depth > 64 {
            return Err(error(78, "review workspace exceeds traversal bounds"));
        }
        for entry in fs::read_dir(directory).map_err(io_error)? {
            count += 1;
            if count > 100_000 {
                return Err(error(78, "review workspace exceeds traversal bounds"));
            }
            let entry = entry.map_err(io_error)?;
            let name = entry.file_name();
            if depth == 0
                && ![
                    "data",
                    "reports",
                    "deepsec.config.mjs",
                    ".gitignore",
                    ".visioncraft.lock",
                ]
                .iter()
                .any(|allowed| name == std::ffi::OsStr::new(allowed))
            {
                return Err(error(
                    78,
                    "review workspace contains unmanaged loader inputs",
                ));
            }
            let meta = fs::symlink_metadata(entry.path()).map_err(io_error)?;
            if meta.file_type().is_symlink()
                || !(meta.is_dir() || meta.is_file())
                || (meta.is_file() && meta.nlink() != 1)
            {
                return Err(error(
                    78,
                    "review workspace contains redirected or special entries",
                ));
            }
            if meta.is_dir() {
                // Past report contents are never active reviewer state. Fresh
                // outputs separately check every parent and refuse existing paths.
                if depth == 0 && name == "reports" {
                    continue;
                }
                pending.push((entry.path(), depth + 1));
            }
        }
    }
    Ok(())
}

/// Execute an explicitly requested security gate, returning its legacy exit code.
///
/// Dry-run prints only the resolved plan. Completed, failed and interrupted
/// attempts write fresh receipts; unknown exports and incomplete execution can
/// never be interpreted as an empty successful review.
pub fn execute(options: &DeepsecOptions, cancelled: &AtomicBool) -> Result<i32, GateError> {
    let modes = [
        options.diff.is_some(),
        options.diff_working,
        options.diff_staged,
        options.files_from.is_some(),
        options.full,
        options.scan_only,
    ];
    if modes.into_iter().filter(|x| *x).count() != 1
        || options.diff.as_ref().is_some_and(|v| v.trim().is_empty())
    {
        return Err(error(78, "choose exactly one nonempty source mode"));
    }
    #[cfg(not(all(target_os = "linux", not(target_env = "uclibc"))))]
    {
        let _ = (options, cancelled);
        Err(error(78, "Deepsec process adapter requires Linux"))
    }
    #[cfg(all(target_os = "linux", not(target_env = "uclibc")))]
    {
        let mut policy = match &options.policy_json {
            Some(text) => from_json::<DeepsecPolicy>(text.as_bytes())
                .map_err(|_| error(78, "invalid policy JSON"))?,
            None => DeepsecPolicy::default(),
        };
        if let Some(v) = &options.agent {
            policy.agent = v.clone();
        }
        if let Some(v) = &options.model {
            policy.model = v.clone();
        }
        if let Some(v) = &options.model_auth {
            policy.model_auth = v.clone();
        }
        if let Some(v) = &options.thinking_level {
            policy.thinking_level = v.clone();
        }
        if let Some(v) = &options.fail_on {
            policy.fail_on = v.to_ascii_uppercase();
        }
        let deadline_ms = policy.validate()?;
        if !policy.enabled {
            return Err(error(
                78,
                "Deepsec gate disabled by policy; record SKIPPED, not passed",
            ));
        }
        let root = root_for(options)?;
        let cwd = std::env::current_dir().map_err(io_error)?;
        let files = match &options.files_from {
            Some(path) if path == Path::new("-") => Some(path.clone()),
            Some(path) => {
                let path = cwd
                    .join(path)
                    .canonicalize()
                    .map_err(|_| error(78, "file manifest is unavailable"))?;
                read_bounded(&path, 8 * 1024 * 1024)
                    .map_err(|_| error(78, "file manifest must be a readable bounded file"))?;
                Some(path)
            }
            None => None,
        };
        let mode = if options.scan_only {
            "scan-only"
        } else if options.full {
            "full"
        } else if files.is_some() {
            "files-from"
        } else if options.diff_working {
            "diff-working"
        } else if options.diff_staged {
            "diff-staged"
        } else {
            "diff"
        };
        let workspace = root.join(".deepsec-gate");
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_millis();
        let attempt_id = format!(
            "{now}-{}-{}",
            std::process::id(),
            ATTEMPT.fetch_add(1, Ordering::SeqCst)
        );
        let report = options
            .report_dir
            .as_ref()
            .map(|p| cwd.join(p))
            .unwrap_or_else(|| workspace.join("reports").join(&attempt_id));
        let project_id: String = root
            .file_name()
            .map_or_else(|| "project".into(), |v| v.to_string_lossy())
            .chars()
            .map(|c| {
                if c.is_ascii_alphanumeric() || "_.-".contains(c) {
                    c
                } else {
                    '-'
                }
            })
            .collect();
        let mut route = match policy.model_auth.as_str() {
            "local" => json!({"mode":"local","provider":"local"}),
            "direct" => {
                json!({"mode":"direct","provider":policy.ai_provider,"apiKeyEnv":policy.ai_api_key_env})
            }
            _ => {
                json!({"mode":"custom","provider":if policy.ai_provider.is_empty(){"custom"}else{&policy.ai_provider},"apiKeyEnv":policy.ai_api_key_env,"credentialHeader":{"name":"authorization","scheme":"bearer"}})
            }
        };
        if !policy.ai_base_url.is_empty() {
            route["baseUrl"] = json!(policy.ai_base_url);
        }
        let plan = json!({"version":1,"attempt_id":attempt_id,"mode":mode,"diff_ref":options.diff,"files_from":files,"root":root,"project_id":project_id,"workspace":workspace,"report_dir":report,"deepsec_version_required":supported_version(),"deepsec_protocol_version":PROTOCOL["adapter_protocol_version"],"policy":policy,"route":route});
        if options.dry_run {
            println!(
                "{}",
                serde_json::to_string_pretty(&plan)
                    .map_err(|_| error(70, "plan serialisation failed"))?
            );
            return Ok(0);
        }
        if !options.scan_only
            && policy.model_auth != "local"
            && std::env::var_os(&policy.ai_api_key_env).is_none_or(|v| v.is_empty())
        {
            return Err(error(
                78,
                "configured credential environment variable is unset",
            ));
        }
        match fs::symlink_metadata(&workspace) {
            Ok(meta) if meta.is_dir() && !meta.file_type().is_symlink() => (),
            Ok(_) => return Err(error(78, "review workspace must be an ordinary directory")),
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                private_directory(&workspace).map_err(io_error)?
            }
            Err(_) => return Err(error(70, "review workspace unavailable")),
        }
        use std::os::unix::fs::PermissionsExt;
        fs::set_permissions(&workspace, fs::Permissions::from_mode(0o700)).map_err(io_error)?;
        let _lock = workspace_lock(&workspace)?;
        validate_workspace_tree(&workspace)?;
        if fs::symlink_metadata(&report).is_ok() {
            return Err(error(78, "report directory must be new"));
        }
        private_directory(&report).map_err(io_error)?;
        let _log = private_file(&report.join("deepsec.log")).map_err(io_error)?;
        write_new(
            &report.join("plan.json"),
            &serde_json::to_vec_pretty(&plan)
                .map_err(|_| error(70, "plan serialisation failed"))?,
        )?;
        let mut attempt = Attempt {
            policy: &policy,
            report: &report,
            cancelled,
            started: Instant::now(),
            deadline: Duration::from_millis(deadline_ms),
            sequence: 0,
        };
        let mut revision = "unknown".to_owned();
        let mut dirty: Option<bool> = None;
        let mut observed_version: Option<String> = None;
        let outcome = (|| -> Result<Value, GateError> {
            recorded::refuse_tracked_workspace(&mut attempt, &root)?;
            // Preserve repository-controlled bytes on refusal. Only after the
            // read-only index probe may managed reviewer files be replaced.
            let mut ignore = tempfile::NamedTempFile::new_in(&workspace).map_err(io_error)?;
            ignore.write_all(b"*\n").map_err(io_error)?;
            ignore
                .persist(workspace.join(".gitignore"))
                .map_err(|_| error(70, "cannot finalise review ignore file"))?;
            let config = workspace.join("deepsec.config.mjs");
            if fs::symlink_metadata(&config)
                .is_ok_and(|m| !m.is_file() || m.file_type().is_symlink())
            {
                return Err(error(78, "configuration path is not an ordinary file"));
            }
            let config_value =
                json!({"ai":route,"projects":[{"id":project_id,"root":root}],"dataDir":"./data"});
            let config_text = format!(
                "// Generated by VisionCraft; credential names only.\nexport default {};\n",
                serde_json::to_string_pretty(&config_value)
                    .map_err(|_| error(70, "configuration serialisation failed"))?
            );
            let mut staged = tempfile::NamedTempFile::new_in(&workspace).map_err(io_error)?;
            staged.write_all(config_text.as_bytes()).map_err(io_error)?;
            staged.as_file().sync_all().map_err(io_error)?;
            staged
                .persist(&config)
                .map_err(|_| error(70, "cannot finalise review configuration"))?;
            let (rc, version) = attempt.command(
                "version",
                "deepsec",
                vec!["--version".into()],
                &workspace,
                false,
                false,
            )?;
            let version_text = String::from_utf8_lossy(&version)
                .trim()
                .trim_start_matches("deepsec ")
                .to_owned();
            // Store only the bounded version token, never arbitrary subprocess output.
            if version_text.len() <= 64
                && version_text
                    .bytes()
                    .all(|b| b.is_ascii_alphanumeric() || b".+-_".contains(&b))
            {
                observed_version = Some(version_text.clone());
            }
            if rc != 0 || version_text != supported_version() {
                return Err(error(
                    78,
                    "Deepsec version does not match the verified 2.3.10 protocol",
                ));
            }
            let (rc, bytes) = attempt.command(
                "revision",
                "git",
                vec!["rev-parse".into(), "HEAD".into()],
                &root,
                false,
                false,
            )?;
            if rc == 0 {
                revision = String::from_utf8_lossy(&bytes).trim().to_owned();
            }
            let selected = recorded::select(options, files.as_deref(), &mut attempt, &root)?;
            dirty = Some(recorded::selected_dirty(&mut attempt, &root, &selected)?);
            let before = recorded::snapshot(&root, &selected)?;
            write_new(
                &report.join("inputs-before.json"),
                &serde_json::to_vec_pretty(&before)
                    .map_err(|_| error(70, "source encoding failed"))?,
            )?;
            let source_list = report.join("selected-files.txt");
            write_new(
                &source_list,
                format!("{}\n", selected.join("\n")).as_bytes(),
            )?;
            let data = workspace.join("data").join(&project_id);
            let common = vec![
                "--project-id".into(),
                project_id.clone(),
                "--root".into(),
                root.to_string_lossy().into_owned(),
            ];
            let mut model_args = vec![
                "--agent".into(),
                policy.agent.clone(),
                "--thinking-level".into(),
                policy.thinking_level.clone(),
                "--batch-size".into(),
                policy.batch_size.to_string(),
                "--concurrency".into(),
                policy.concurrency.to_string(),
            ];
            if !policy.model.is_empty() {
                model_args.extend(["--model".into(), policy.model.clone()]);
            }
            if options.scan_only || options.full {
                let mut args = vec!["scan".into()];
                args.extend(common.clone());
                let (rc, _) = attempt.command("scan", "deepsec", args, &workspace, false, false)?;
                if rc != 0 {
                    return Err(error(70, "Deepsec scan failed"));
                }
                if options.scan_only {
                    return Ok(
                        json!({"result":"SCANNED","deepsec_exit":0,"findings_by_severity":{},"blocking":[],"boundary":"Scan-only is not a completed model review."}),
                    );
                }
            }
            let expected = if options.full {
                recorded::pending(&data, &before)?
            } else {
                before.clone()
            };
            let prior_runs = recorded::run_files(&data)?;
            let mut args = vec!["process".into()];
            if !options.full {
                args.extend([
                    "--files-from".into(),
                    source_list.to_string_lossy().into_owned(),
                    "--no-ignore".into(),
                ]);
            }
            args.extend(common);
            args.extend(model_args);
            if !options.full {
                args.extend([
                    "--comment-out".into(),
                    report.join("comment.md").to_string_lossy().into_owned(),
                ]);
            }
            let (rc, _) = attempt.command("process", "deepsec", args, &workspace, true, false)?;
            if (options.full && rc != 0) || (!options.full && ![0, 1].contains(&rc)) {
                return Err(error(70, "Deepsec processing failed"));
            }
            if rc == 1
                && read_bounded(&report.join("comment.md"), policy.max_output_bytes)
                    .map_or(true, |b| b.is_empty())
            {
                return Err(error(
                    70,
                    "exit 1 without fresh completed findings evidence",
                ));
            }
            let after = recorded::snapshot(&root, &selected)?;
            write_new(
                &report.join("inputs-after.json"),
                &serde_json::to_vec_pretty(&after)
                    .map_err(|_| error(70, "source encoding failed"))?,
            )?;
            if before != after {
                return Err(error(70, "review inputs changed while the reviewer ran"));
            }
            let observed = recorded::validate_review(
                &data,
                &prior_runs,
                &expected,
                &root,
                &project_id,
                &report,
            )?;
            let args = vec![
                "export".into(),
                "--project-id".into(),
                project_id.clone(),
                "--format".into(),
                "json".into(),
                "--min-severity".into(),
                "LOW".into(),
                "--out".into(),
                report.join("findings.json").to_string_lossy().into_owned(),
            ];
            let (export_rc, _) =
                attempt.command("export", "deepsec", args, &workspace, false, false)?;
            if export_rc != 0 {
                return Err(error(70, "Deepsec findings export failed"));
            }
            let raw = read_bounded(&report.join("findings.json"), policy.max_output_bytes)?;
            let findings = findings(&raw)?;
            if rc == 1 && findings.is_empty() {
                return Err(error(70, "findings exit with an empty export"));
            }
            let mut counts: BTreeMap<&str, usize> =
                SEVERITIES.into_iter().map(|s| (s, 0)).collect();
            let threshold = SEVERITIES
                .iter()
                .position(|s| *s == policy.fail_on)
                .expect("validated threshold");
            let mut blocking = Vec::new();
            for finding in &findings {
                let severity = finding["severity"].as_str().expect("validated severity");
                *counts.get_mut(severity).expect("known severity") += 1;
                if SEVERITIES
                    .iter()
                    .position(|s| *s == severity)
                    .expect("known severity")
                    <= threshold
                {
                    blocking.push(finding.clone());
                }
            }
            Ok(
                json!({"result":if blocking.is_empty(){"PASS"}else{"BLOCK"},"deepsec_exit":rc,"findings_by_severity":counts,"blocking":blocking,"findings_total":findings.len(),"observed_review":observed,"raw_export_sha256":sha256(&raw),"net_new_reported_by_deepsec":if options.full{None}else{Some(rc==1)},"boundary":"Threshold result over the raw export; historical findings may be included. Observed coverage is listed separately; semantic truth is not inferred."}),
            )
        })();
        let (mut receipt, exit) = match outcome {
            Ok(value) => {
                let code = if value["result"] == "BLOCK" { 1 } else { 0 };
                (value, code)
            }
            Err(err) => (
                json!({"result":match err.code {124=>"TIMEOUT",130=>"INTERRUPTED",78=>"UNAVAILABLE",_=>"ERROR"},"error":err.message}),
                err.code,
            ),
        };
        let object = receipt.as_object_mut().expect("receipt object");
        object.extend(json!({"schema_version":1,"gate":"deepsec","attempt_id":attempt_id,"mode":mode,"revision":revision,"dirty":dirty,"deepsec_version":observed_version,"deepsec_version_required":supported_version(),"deepsec_git_head":PROTOCOL["package"]["git_head"],"deepsec_npm_integrity":PROTOCOL["package"]["integrity"],"deepsec_protocol_version":PROTOCOL["adapter_protocol_version"],"release_identity_boundary":"Expected upstream identity; installed package bytes are verified by the host. CLI version was observed separately.","policy_source":policy.policy_source,"fail_on":policy.fail_on,"report_dir":report,"state_dir":workspace,"elapsed_ms":attempt.started.elapsed().as_millis(),"plan_sha256":sha256(&serde_json::to_vec(&plan).map_err(|_|error(70,"plan encoding failed"))?)}).as_object().expect("object").clone());
        object.insert(
            "dirty_scope".into(),
            Value::String("selected-inputs".into()),
        );
        write_new(
            &report.join("receipt.json"),
            &serde_json::to_vec_pretty(&receipt)
                .map_err(|_| error(70, "receipt encoding failed"))?,
        )?;
        println!(
            "{}",
            serde_json::to_string_pretty(&receipt)
                .map_err(|_| error(70, "receipt encoding failed"))?
        );
        Ok(exit)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn duration_and_policy_refuse_unbounded_or_secret_urls() {
        assert_eq!(duration_ms("0.25s"), Some(250));
        assert_eq!(duration_ms("45m"), Some(2_700_000));
        for bad in ["NaNs", "-1s", "0s", "100d", "1.2.3s"] {
            assert_eq!(duration_ms(bad), None);
        }
        let policy = DeepsecPolicy {
            ai_base_url: "https://user:password@example.com".into(),
            ..DeepsecPolicy::default()
        };
        assert!(policy.validate().is_err());
        assert!(DeepsecPolicy::default().validate().is_ok());
    }
    #[test]
    fn export_shape_has_positive_and_negative_controls() {
        assert!(findings(b"[]").unwrap().is_empty());
        assert_eq!(
            findings(br#"[{"filePath":"a","findings":[{"severity":"high"}]}]"#).unwrap()[0]["severity"],
            "HIGH"
        );
        for bad in [
            b"[null]".as_slice(),
            b"{}",
            b"[{\"severity\":\"SUPERHIGH\"}]",
            b"[{\"findings\":[{}]}]",
            b"[",
            br#"[{"severity":"HIGH","severity":"LOW"}]"#,
            br#"{"findings":[{"severity":"HIGH"}],"findings":[]}"#,
        ] {
            assert!(findings(bad).is_err());
        }
    }
}