canon-mcp 0.2.0

Canon Protocol — cryptographic audit trails for AI-assisted development
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
//! Canon Protocol MCP Server
//!
//! Provides cryptographic audit trails for AI-assisted development.
//! Runs as an MCP server over stdio, spawned by Claude Code, Cursor, Windsurf, etc.
//!
//! Also provides CLI subcommands for hook integration:
//! - `canon-mcp` (no args) — MCP server on stdio
//! - `canon-mcp session-proof` — Generate a session proof from .canon/session.jsonl

mod parser;
mod search;
mod server;
mod watcher;

use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
use canon_core::proof::{self, FileSnapshot, GitContext, SessionEvent, SessionProof, UserPrompt};
// NOTE: SemanticSearchResult/SemanticHit deliberately NOT imported — session proofs
// no longer fabricate post-hoc semantic context. Search proofs (from canon_search MCP
// tool calls) are the real proof of what Canon served to the AI.
use canon_embed::EmbeddingEngine;
use canon_store::GraphStore;
use canon_core::DeviceIdentity;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use tracing::info;

#[derive(Parser)]
#[command(name = "canon-mcp")]
#[command(about = "Canon Protocol MCP server — cryptographic audit trails for AI")]
struct Cli {
    #[command(subcommand)]
    command: Option<Commands>,

    /// Directory to watch for file changes
    #[arg(long)]
    watch: Option<PathBuf>,

    /// Data directory for substrate storage (default: .canon)
    #[arg(long, default_value = ".canon")]
    data_dir: PathBuf,
}

#[derive(Subcommand)]
enum Commands {
    /// Initialize Canon Protocol in the current project.
    /// Creates .mcp.json, hooks, and settings for automatic proof generation.
    Init,
    /// Generate a session proof from the accumulated session log.
    /// Called by the Stop hook after Claude finishes a response.
    SessionProof {
        /// Data directory (default: .canon)
        #[arg(long, default_value = ".canon")]
        data_dir: PathBuf,
    },
    /// Verify a session proof or search proof receipt.
    Verify {
        /// Path to the proof JSON file
        path: PathBuf,
    },
}

/// Detect git context for the current directory.
fn detect_git_context() -> Option<GitContext> {
    use std::process::Command;

    let commit = Command::new("git")
        .args(["rev-parse", "HEAD"])
        .output()
        .ok()
        .filter(|o| o.status.success())
        .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string());

    let branch = Command::new("git")
        .args(["branch", "--show-current"])
        .output()
        .ok()
        .filter(|o| o.status.success())
        .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
        .filter(|s| !s.is_empty());

    let name = Command::new("git")
        .args(["config", "user.name"])
        .output()
        .ok()
        .filter(|o| o.status.success())
        .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string());

    let email = Command::new("git")
        .args(["config", "user.email"])
        .output()
        .ok()
        .filter(|o| o.status.success())
        .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string());

    let author = match (name, email) {
        (Some(n), Some(e)) => Some(format!("{} <{}>", n, e)),
        (Some(n), None) => Some(n),
        _ => None,
    };

    let dirty = Command::new("git")
        .args(["status", "--porcelain"])
        .output()
        .ok()
        .map(|o| !o.stdout.is_empty())
        .unwrap_or(false);

    commit.as_ref()?;

    Some(GitContext {
        commit,
        branch,
        author,
        dirty,
    })
}

/// Try to load an Ed25519 SSH signing key from git config.
fn try_load_git_signing_key() -> Option<[u8; 32]> {
    use std::process::Command;

    let gpg_format = Command::new("git")
        .args(["config", "gpg.format"])
        .output()
        .ok()
        .filter(|o| o.status.success())
        .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())?;

    if gpg_format != "ssh" {
        return None;
    }

    let signing_key = Command::new("git")
        .args(["config", "user.signingkey"])
        .output()
        .ok()
        .filter(|o| o.status.success())
        .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())?;

    let key_path = if signing_key.starts_with('~') {
        dirs_or_home().map(|h| h.join(&signing_key[2..]))?
    } else {
        PathBuf::from(&signing_key)
    };

    if !key_path.exists() {
        return None;
    }

    let key_data = std::fs::read_to_string(&key_path).ok()?;
    parse_openssh_ed25519_seed(&key_data)
}

/// Parse the 32-byte Ed25519 seed from an unencrypted OpenSSH private key.
fn parse_openssh_ed25519_seed(pem: &str) -> Option<[u8; 32]> {
    use base64::Engine;

    let b64: String = pem
        .lines()
        .filter(|l| !l.starts_with("-----"))
        .collect();

    let decoded = base64::engine::general_purpose::STANDARD.decode(&b64).ok()?;

    let magic = b"openssh-key-v1\0";
    if decoded.len() < magic.len() || &decoded[..magic.len()] != magic {
        return None;
    }

    let mut pos = magic.len();

    let read_u32 = |data: &[u8], p: &mut usize| -> Option<u32> {
        if *p + 4 > data.len() { return None; }
        let val = u32::from_be_bytes(data[*p..*p + 4].try_into().ok()?);
        *p += 4;
        Some(val)
    };

    let read_string = |data: &[u8], p: &mut usize| -> Option<Vec<u8>> {
        let len = read_u32(data, p)? as usize;
        if *p + len > data.len() { return None; }
        let val = data[*p..*p + len].to_vec();
        *p += len;
        Some(val)
    };

    let cipher = read_string(&decoded, &mut pos)?;
    if cipher != b"none" {
        return None;
    }

    let _kdf = read_string(&decoded, &mut pos)?;
    let _kdfopts = read_string(&decoded, &mut pos)?;
    let nkeys = read_u32(&decoded, &mut pos)?;
    if nkeys != 1 { return None; }
    let _pubkey = read_string(&decoded, &mut pos)?;
    let priv_section = read_string(&decoded, &mut pos)?;

    let mut pp = 0;
    let check1 = read_u32(&priv_section, &mut pp)?;
    let check2 = read_u32(&priv_section, &mut pp)?;
    if check1 != check2 { return None; }

    let key_type = read_string(&priv_section, &mut pp)?;
    if key_type != b"ssh-ed25519" { return None; }

    let _pub_data = read_string(&priv_section, &mut pp)?;

    let priv_data = read_string(&priv_section, &mut pp)?;
    if priv_data.len() != 64 { return None; }

    let mut seed = [0u8; 32];
    seed.copy_from_slice(&priv_data[..32]);
    Some(seed)
}

fn dirs_or_home() -> Option<PathBuf> {
    std::env::var("HOME").ok().map(PathBuf::from)
}

/// Load or create device identity.
fn load_or_create_identity(data_dir: &std::path::Path) -> Result<DeviceIdentity> {
    if let Some(seed) = try_load_git_signing_key() {
        info!("Using git Ed25519 SSH signing key for device identity");
        return Ok(DeviceIdentity::from_seed(seed));
    }

    let key_path = data_dir.join("cp.key");

    if key_path.exists() {
        let seed_bytes = std::fs::read(&key_path)?;
        if seed_bytes.len() == 32 {
            let mut seed = [0u8; 32];
            seed.copy_from_slice(&seed_bytes);
            return Ok(DeviceIdentity::from_seed(seed));
        }
    }

    let identity = DeviceIdentity::generate();
    std::fs::write(&key_path, &identity.export_seed())?;
    info!("Generated new device identity at {:?}", key_path);

    Ok(identity)
}

/// Ensure .canon/.gitignore exists.
fn ensure_gitignore(data_dir: &std::path::Path) -> Result<()> {
    let gitignore_path = data_dir.join(".gitignore");
    if !gitignore_path.exists() {
        std::fs::write(
            &gitignore_path,
            "graph.db\ncp.key\nhnsw.idx\n*.usearch\nsession.jsonl\n",
        )?;
    }
    Ok(())
}

// ============================================================================
// Init command — set up Canon Protocol in the current project
// ============================================================================

fn init_project() -> Result<()> {
    let canon_mcp_path = std::env::current_exe()
        .unwrap_or_else(|_| PathBuf::from("canon-mcp"))
        .to_string_lossy()
        .to_string();

    // 1. Create directories
    std::fs::create_dir_all(".canon/hooks")?;
    std::fs::create_dir_all(".canon/proofs")?;
    std::fs::create_dir_all(".claude")?;

    // 2. .mcp.json — MCP server config for Claude Code / Cursor / Windsurf
    let mcp_json_path = PathBuf::from(".mcp.json");
    if mcp_json_path.exists() {
        println!("  .mcp.json already exists, skipping");
    } else {
        let mcp_json = format!(r#"{{
  "mcpServers": {{
    "canon": {{
      "command": "{}",
      "args": ["--watch", ".", "--data-dir", ".canon"]
    }}
  }}
}}
"#, canon_mcp_path);
        std::fs::write(&mcp_json_path, mcp_json)?;
        println!("  Created .mcp.json");
    }

    // 3. Post-tool hook — logs every tool call
    let post_tool = r#"#!/bin/bash
# Canon Protocol — PostToolUse hook
# Logs every tool call with context: what was sent and what came back.

INPUT=$(cat)

TOOL_NAME=$(echo "$INPUT" | jq -r '.tool_name // "unknown"')
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // empty')
SESSION_ID=$(echo "$INPUT" | jq -r '.session_id // "unknown"')
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")

INPUT_PREVIEW=""
OUTPUT_PREVIEW=""

case "$TOOL_NAME" in
  Read)
    OUTPUT_PREVIEW=$(echo "$INPUT" | jq -r '.tool_response // empty' | head -c 1000)
    ;;
  Write)
    INPUT_PREVIEW=$(echo "$INPUT" | jq -r '.tool_input.content // empty' | head -c 1000)
    ;;
  Edit)
    OLD=$(echo "$INPUT" | jq -r '.tool_input.old_string // empty' | head -c 500)
    NEW=$(echo "$INPUT" | jq -r '.tool_input.new_string // empty' | head -c 500)
    INPUT_PREVIEW=$(printf "-%s\n+%s" "$OLD" "$NEW")
    ;;
  Bash)
    INPUT_PREVIEW=$(echo "$INPUT" | jq -r '.tool_input.command // empty' | head -c 500)
    OUTPUT_PREVIEW=$(echo "$INPUT" | jq -r '.tool_response // empty' | head -c 1000)
    ;;
  Grep|Glob)
    INPUT_PREVIEW=$(echo "$INPUT" | jq -r '.tool_input.pattern // empty')
    OUTPUT_PREVIEW=$(echo "$INPUT" | jq -r '.tool_response // empty' | head -c 1000)
    ;;
esac

CANON_DIR=".canon"
if [ ! -d "$CANON_DIR" ]; then
  CANON_DIR="$(pwd)/.canon"
fi

jq -cn \
  --arg tool_name "$TOOL_NAME" \
  --arg file_path "$FILE_PATH" \
  --arg session_id "$SESSION_ID" \
  --arg timestamp "$TIMESTAMP" \
  --arg input_preview "$INPUT_PREVIEW" \
  --arg output_preview "$OUTPUT_PREVIEW" \
  '{tool_name: $tool_name, tool_input: {file_path: $file_path}, session_id: $session_id, timestamp: $timestamp, input_preview: $input_preview, output_preview: $output_preview}' \
  >> "$CANON_DIR/session.jsonl"
"#;
    std::fs::write(".canon/hooks/post-tool.sh", post_tool)?;
    set_executable(".canon/hooks/post-tool.sh")?;
    println!("  Created .canon/hooks/post-tool.sh");

    // 4. Prompt hook — captures user prompts
    let prompt_hook = r#"#!/bin/bash
# Canon Protocol — UserPromptSubmit hook
# Logs the user's prompt so we know what the AI was asked.

INPUT=$(cat)

PROMPT=$(echo "$INPUT" | jq -r '.prompt // empty')
SESSION_ID=$(echo "$INPUT" | jq -r '.session_id // "unknown"')
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")

PROMPT_TRUNC=$(echo "$PROMPT" | head -c 2000)

CANON_DIR=".canon"
if [ ! -d "$CANON_DIR" ]; then
  CANON_DIR="$(pwd)/.canon"
fi

jq -cn \
  --arg type "user_prompt" \
  --arg prompt "$PROMPT_TRUNC" \
  --arg session_id "$SESSION_ID" \
  --arg timestamp "$TIMESTAMP" \
  '{type: $type, prompt: $prompt, session_id: $session_id, timestamp: $timestamp}' \
  >> "$CANON_DIR/session.jsonl"
"#;
    std::fs::write(".canon/hooks/prompt.sh", prompt_hook)?;
    set_executable(".canon/hooks/prompt.sh")?;
    println!("  Created .canon/hooks/prompt.sh");

    // 5. Stop hook — generates session proof when AI finishes
    let stop_hook = format!(r#"#!/bin/bash
# Canon Protocol — Stop hook
# When Claude finishes responding, generate a signed session proof.

CANON_MCP="{}"

if [ -s ".canon/session.jsonl" ]; then
  "$CANON_MCP" session-proof --data-dir .canon 2>&1
fi
"#, canon_mcp_path);
    std::fs::write(".canon/hooks/stop.sh", &stop_hook)?;
    set_executable(".canon/hooks/stop.sh")?;
    println!("  Created .canon/hooks/stop.sh");

    // 6. Claude Code hooks config
    let settings_path = PathBuf::from(".claude/settings.local.json");
    if settings_path.exists() {
        // Merge hooks into existing settings
        let existing = std::fs::read_to_string(&settings_path)?;
        if existing.contains("PostToolUse") {
            println!("  .claude/settings.local.json already has hooks, skipping");
        } else {
            // Parse and add hooks
            if let Ok(mut val) = serde_json::from_str::<serde_json::Value>(&existing) {
                let hooks = serde_json::json!({
                    "UserPromptSubmit": [{
                        "hooks": [{"type": "command", "command": ".canon/hooks/prompt.sh", "timeout": 5}]
                    }],
                    "PostToolUse": [{
                        "matcher": "Read|Write|Edit|Bash|Grep|Glob",
                        "hooks": [{"type": "command", "command": ".canon/hooks/post-tool.sh", "timeout": 10}]
                    }],
                    "Stop": [{
                        "hooks": [{"type": "command", "command": ".canon/hooks/stop.sh", "timeout": 30}]
                    }]
                });
                val.as_object_mut().unwrap().insert("hooks".to_string(), hooks);
                std::fs::write(&settings_path, serde_json::to_string_pretty(&val)?)?;
                println!("  Updated .claude/settings.local.json with hooks");
            }
        }
    } else {
        let settings = serde_json::json!({
            "hooks": {
                "UserPromptSubmit": [{
                    "hooks": [{"type": "command", "command": ".canon/hooks/prompt.sh", "timeout": 5}]
                }],
                "PostToolUse": [{
                    "matcher": "Read|Write|Edit|Bash|Grep|Glob",
                    "hooks": [{"type": "command", "command": ".canon/hooks/post-tool.sh", "timeout": 10}]
                }],
                "Stop": [{
                    "hooks": [{"type": "command", "command": ".canon/hooks/stop.sh", "timeout": 30}]
                }]
            }
        });
        std::fs::write(&settings_path, serde_json::to_string_pretty(&settings)?)?;
        println!("  Created .claude/settings.local.json");
    }

    // 7. .canon/.gitignore — keep substrate out of git
    let canon_gitignore = "graph.db\ncp.key\nhnsw.idx\n*.usearch\nsession.jsonl\n";
    std::fs::write(".canon/.gitignore", canon_gitignore)?;
    println!("  Created .canon/.gitignore");

    // 8. Append to project .gitignore if it exists
    let project_gitignore = PathBuf::from(".gitignore");
    let canon_entries = "\n# Canon Protocol\n.canon/graph.db\n.canon/cp.key\n.canon/hnsw.idx\n.canon/*.usearch\n.canon/session.jsonl\n";
    if project_gitignore.exists() {
        let existing = std::fs::read_to_string(&project_gitignore)?;
        if !existing.contains(".canon/graph.db") {
            let mut f = std::fs::OpenOptions::new().append(true).open(&project_gitignore)?;
            use std::io::Write;
            f.write_all(canon_entries.as_bytes())?;
            println!("  Updated .gitignore");
        }
    } else {
        std::fs::write(&project_gitignore, canon_entries.trim_start())?;
        println!("  Created .gitignore");
    }

    println!("\nCanon Protocol initialized. Start Claude Code and proofs will be generated automatically.");
    println!("Verify proofs with: canon-mcp verify .canon/proofs/<proof>.json");

    Ok(())
}

#[cfg(unix)]
fn set_executable(path: &str) -> Result<()> {
    use std::os::unix::fs::PermissionsExt;
    let perms = std::fs::Permissions::from_mode(0o755);
    std::fs::set_permissions(path, perms)?;
    Ok(())
}

#[cfg(not(unix))]
fn set_executable(_path: &str) -> Result<()> {
    Ok(())
}

// ============================================================================
// Session proof generation (called by Stop hook)
// ============================================================================

fn generate_session_proof(data_dir: &std::path::Path) -> Result<()> {
    let session_log = data_dir.join("session.jsonl");

    if !session_log.exists() {
        eprintln!("No session log found at {:?}", session_log);
        return Ok(());
    }

    let content = std::fs::read_to_string(&session_log)?;
    let lines: Vec<&str> = content.lines().filter(|l| !l.trim().is_empty()).collect();

    if lines.is_empty() {
        eprintln!("Session log is empty");
        return Ok(());
    }

    // Parse events and user prompts
    let mut events: Vec<SessionEvent> = Vec::new();
    let mut user_prompts: Vec<UserPrompt> = Vec::new();
    let mut files_seen: HashMap<String, String> = HashMap::new(); // path -> action

    for line in &lines {
        if let Ok(entry) = serde_json::from_str::<serde_json::Value>(line) {
            // Check if this is a user prompt entry
            if entry.get("type").and_then(|v| v.as_str()) == Some("user_prompt") {
                let prompt = entry.get("prompt").and_then(|v| v.as_str()).unwrap_or("").to_string();
                let timestamp = entry.get("timestamp").and_then(|v| v.as_str()).unwrap_or("").to_string();
                if !prompt.is_empty() {
                    user_prompts.push(UserPrompt { timestamp, prompt });
                }
                continue;
            }

            // Tool call event
            let tool = entry.get("tool_name").and_then(|v| v.as_str()).unwrap_or("unknown");
            let action = match tool {
                "Read" => "read",
                "Write" => "write",
                "Edit" => "edit",
                "Bash" => "bash",
                "Grep" | "Glob" => "search",
                _ => "other",
            };

            let file_path = entry.get("tool_input")
                .and_then(|v| v.get("file_path"))
                .and_then(|v| v.as_str())
                .map(|s| s.to_string());

            let timestamp = entry.get("timestamp")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string();

            let input_preview = entry.get("input_preview")
                .and_then(|v| v.as_str())
                .filter(|s| !s.is_empty())
                .map(|s| s.to_string());

            let output_preview = entry.get("output_preview")
                .and_then(|v| v.as_str())
                .filter(|s| !s.is_empty())
                .map(|s| s.to_string());

            events.push(SessionEvent {
                timestamp,
                tool: tool.to_string(),
                action: action.to_string(),
                file_path: file_path.clone(),
                input_preview,
                output_preview,
            });

            if let Some(path) = file_path {
                let existing = files_seen.get(&path).map(|s| s.as_str());
                let should_update = match (existing, action) {
                    (None, _) => true,
                    (Some("read"), "write" | "edit") => true,
                    (Some("edit"), "write") => true,
                    _ => false,
                };
                if should_update {
                    files_seen.insert(path, action.to_string());
                }
            }
        }
    }

    // Build file snapshots
    let mut files_read: Vec<FileSnapshot> = Vec::new();
    let mut files_written: Vec<FileSnapshot> = Vec::new();

    for (path, action) in &files_seen {
        let file_path = PathBuf::from(path);
        if !file_path.exists() {
            continue;
        }

        let file_content = match std::fs::read(&file_path) {
            Ok(c) => c,
            Err(_) => continue,
        };

        let content_hash = *blake3::hash(&file_content).as_bytes();
        let size_bytes = file_content.len() as u64;
        let snippet = String::from_utf8_lossy(&file_content[..file_content.len().min(500)]).to_string();

        let snapshot = FileSnapshot {
            path: path.clone(),
            content_hash,
            action: action.clone(),
            size_bytes,
            snippet,
        };

        match action.as_str() {
            "write" | "edit" => files_written.push(snapshot),
            _ => files_read.push(snapshot),
        }
    }

    let all_files: Vec<FileSnapshot> = files_read.iter()
        .chain(files_written.iter())
        .cloned()
        .collect();
    let files_root = proof::compute_files_root(&all_files);

    // NOTE: We deliberately do NOT run semantic searches here. The old code
    // would re-query Canon's substrate for each user prompt and claim those
    // results were "what the AI knew." That was dishonest — Canon's search
    // engine is not the AI's search engine. We have no visibility into what
    // context the model actually used.
    //
    // What IS provable:
    // - User prompts (captured by hooks) — what was asked
    // - Tool call events (captured by hooks) — what the AI actually did
    // - File snapshots (hashed at proof time) — what files were touched
    // - Search proof receipts (from canon_search MCP calls) — what Canon served
    //
    // If the AI called canon_search during the session, those search proof
    // receipts are already saved in .canon/proofs/ with real Merkle proofs.
    // Those are ground truth. This session proof covers the rest.

    // Load identity and git context
    let identity = load_or_create_identity(data_dir)?;
    let git_context = detect_git_context();

    let session_id = content.lines().next()
        .and_then(|l| serde_json::from_str::<serde_json::Value>(l).ok())
        .and_then(|v| v.get("session_id").and_then(|s| s.as_str()).map(|s| s.to_string()))
        .unwrap_or_else(|| "unknown".to_string());

    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default();
    let timestamp = server::format_timestamp(now.as_secs());

    // Build and sign the session proof
    let mut proof = SessionProof {
        version: 3,
        session_id,
        timestamp: timestamp.clone(),
        user_prompts,
        semantic_context: Vec::new(), // v3: no longer fabricated post-hoc
        events,
        files_read,
        files_written,
        files_root,
        signature: [0u8; 64],
        signer_public_key: identity.public_key,
        device_id: identity.device_id,
        git: git_context,
    };

    let sig = identity.sign(&proof.signing_bytes());
    proof.signature = sig;

    // Save
    std::fs::create_dir_all(data_dir.join("proofs"))?;
    let root_prefix: String = proof.files_root.iter().take(4).map(|b| format!("{:02x}", b)).collect();
    let ts_safe = timestamp.replace(':', "-").replace('.', "-");
    let filename = format!("session_{}_{}.json", ts_safe, root_prefix);
    let path = data_dir.join("proofs").join(&filename);

    let json = serde_json::to_string_pretty(&proof)?;
    std::fs::write(&path, &json)?;

    // Clear the session log for next turn
    std::fs::write(&session_log, "")?;

    eprintln!("Session proof saved: {} ({} prompts, {} events, {} files)",
        path.display(),
        proof.user_prompts.len(),
        proof.events.len(),
        proof.files_read.len() + proof.files_written.len());

    Ok(())
}

// ============================================================================
// Verify command
// ============================================================================

fn verify_proof(path: &std::path::Path) -> Result<()> {
    let json = std::fs::read_to_string(path)
        .context("Failed to read proof file")?;

    // Try session proof first, then search proof
    if let Ok(proof) = serde_json::from_str::<SessionProof>(&json) {
        verify_session_proof(&proof);
    } else if let Ok(proof) = serde_json::from_str::<canon_core::ProofReceipt>(&json) {
        verify_search_proof(&proof);
    } else {
        anyhow::bail!("Unrecognized proof format");
    }

    Ok(())
}

fn verify_session_proof(proof: &SessionProof) {
    println!("╔══════════════════════════════════════════════════════════════╗");
    println!("║            CANON PROTOCOL — SESSION AUDIT TRAIL             ║");
    println!("╚══════════════════════════════════════════════════════════════╝");
    println!();
    println!("  Session:   {}", &proof.session_id[..12.min(proof.session_id.len())]);
    println!("  Timestamp: {}", proof.timestamp);
    println!("  Events:    {}", proof.events.len());
    println!("  Prompts:   {}", proof.user_prompts.len());

    // Verification checks
    println!("\n┌─ Cryptographic Verification ─────────────────────────────────┐");

    match proof.verify_signature() {
        Ok(()) => println!("│  [PASS] Ed25519 signature valid                              │"),
        Err(e) => println!("│  [FAIL] Ed25519 signature: {}", pad_right(&format!("{}", e), 30)),
    }

    match proof.verify_files_root() {
        Ok(()) => println!("│  [PASS] Files Merkle root valid                              │"),
        Err(e) => println!("│  [FAIL] Files root: {}", pad_right(&format!("{}", e), 37)),
    }

    if proof.git.is_some() {
        println!("│  [PASS] Git context cryptographically bound                  │");
    }

    if !proof.user_prompts.is_empty() {
        println!("│  [PASS] User prompts cryptographically bound                 │");
    }

    match proof.verify_all() {
        Ok(()) => {
            println!("│                                                              │");
            println!("│  Result: ALL CHECKS PASSED                                  │");
        }
        Err(e) => {
            println!("│                                                              │");
            println!("│  Result: VERIFICATION FAILED - {}", pad_right(&format!("{}", e), 25));
        }
    }
    println!("└──────────────────────────────────────────────────────────────┘");

    // Git context
    if let Some(ref gc) = proof.git {
        println!("\n┌─ Git Context (signed) ──────────────────────────────────────┐");
        if let Some(ref author) = gc.author { println!("│  Author:  {}", author); }
        if let Some(ref commit) = gc.commit { println!("│  Commit:  {}", commit); }
        if let Some(ref branch) = gc.branch { println!("│  Branch:  {}", branch); }
        println!("│  Dirty:   {}", gc.dirty);
        println!("└──────────────────────────────────────────────────────────────┘");
    }

    // What the user asked — THE KEY SECTION
    if !proof.user_prompts.is_empty() {
        println!("\n════════════════════════════════════════════════════════════════");
        println!("  WHAT THE USER ASKED ({} prompt{}):",
            proof.user_prompts.len(),
            if proof.user_prompts.len() == 1 { "" } else { "s" });
        println!("════════════════════════════════════════════════════════════════");
        for (i, p) in proof.user_prompts.iter().enumerate() {
            println!("\n  [Prompt {}{}]", i + 1, p.timestamp);
            println!("  > {}", p.prompt.replace('\n', "\n  > "));
        }
    }

    // Activity timeline — ground truth from hook-captured tool calls
    println!("\n════════════════════════════════════════════════════════════════");
    println!("  TOOL CALLS ({} events — hook-captured ground truth):",
        proof.events.len());
    println!("════════════════════════════════════════════════════════════════");
    for (i, event) in proof.events.iter().enumerate() {
        let file = event.file_path.as_deref().unwrap_or("");
        let file_short = if file.len() > 60 {
            format!("...{}", &file[file.len()-57..])
        } else {
            file.to_string()
        };

        match event.action.as_str() {
            "read" => {
                println!("\n  {}. READ {}", i + 1, file_short);
                if let Some(ref preview) = event.output_preview {
                    let lines: Vec<&str> = preview.lines().take(8).collect();
                    println!("     ┌─ Content the AI received ─────────────────");
                    for line in &lines {
                        println!("{}", truncate_line(line, 60));
                    }
                    if preview.lines().count() > 8 {
                        println!("     │ ... ({} more lines)", preview.lines().count() - 8);
                    }
                    println!("     └─────────────────────────────────────────────");
                }
            }
            "write" => {
                println!("\n  {}. WROTE {}", i + 1, file_short);
                if let Some(ref preview) = event.input_preview {
                    let lines: Vec<&str> = preview.lines().take(8).collect();
                    println!("     ┌─ Content the AI wrote ────────────────────");
                    for line in &lines {
                        println!("{}", truncate_line(line, 60));
                    }
                    if preview.lines().count() > 8 {
                        println!("     │ ... ({} more lines)", preview.lines().count() - 8);
                    }
                    println!("     └─────────────────────────────────────────────");
                }
            }
            "edit" => {
                println!("\n  {}. EDITED {}", i + 1, file_short);
                if let Some(ref preview) = event.input_preview {
                    let lines: Vec<&str> = preview.lines().take(10).collect();
                    println!("     ┌─ Diff ──────────────────────────────────────");
                    for line in &lines {
                        println!("{}", truncate_line(line, 60));
                    }
                    println!("     └─────────────────────────────────────────────");
                }
            }
            "bash" => {
                let cmd = event.input_preview.as_deref().unwrap_or("(unknown command)");
                println!("\n  {}. RAN $ {}", i + 1, truncate_line(cmd, 60));
                if let Some(ref preview) = event.output_preview {
                    let lines: Vec<&str> = preview.lines().take(5).collect();
                    if !lines.is_empty() && !lines[0].is_empty() {
                        println!("     ┌─ Output ────────────────────────────────────");
                        for line in &lines {
                            println!("{}", truncate_line(line, 60));
                        }
                        if preview.lines().count() > 5 {
                            println!("     │ ... ({} more lines)", preview.lines().count() - 5);
                        }
                        println!("     └─────────────────────────────────────────────");
                    }
                }
            }
            "search" => {
                let pattern = event.input_preview.as_deref().unwrap_or("(unknown)");
                println!("\n  {}. SEARCHED for \"{}\"", i + 1, pattern);
                if let Some(ref preview) = event.output_preview {
                    let lines: Vec<&str> = preview.lines().take(5).collect();
                    if !lines.is_empty() {
                        println!("     ┌─ Results ───────────────────────────────────");
                        for line in &lines {
                            println!("{}", truncate_line(line, 60));
                        }
                        if preview.lines().count() > 5 {
                            println!("     │ ... ({} more)", preview.lines().count() - 5);
                        }
                        println!("     └─────────────────────────────────────────────");
                    }
                }
            }
            _ => {
                println!("\n  {}. [{}] {}{}", i + 1, event.action, event.tool, file_short);
            }
        }
    }

    // Files the AI read — full snapshots with hashes
    if !proof.files_read.is_empty() {
        println!("\n════════════════════════════════════════════════════════════════");
        println!("  FILE SNAPSHOTS — READ ({}):", proof.files_read.len());
        println!("════════════════════════════════════════════════════════════════");
        for f in &proof.files_read {
            let hash_hex: String = f.content_hash.iter().take(4).map(|b| format!("{:02x}", b)).collect();
            println!("\n  {} ({} bytes, BLAKE3: {})", f.path, f.size_bytes, hash_hex);
            println!("  ┌────────────────────────────────────────────────────────────");
            for line in f.snippet.lines().take(12) {
                println!("{}", truncate_line(line, 60));
            }
            if f.snippet.lines().count() > 12 || f.size_bytes > 500 {
                println!("  │ ... ({} total bytes)", f.size_bytes);
            }
            println!("  └────────────────────────────────────────────────────────────");
        }
    }

    // Files the AI wrote — full snapshots with hashes
    if !proof.files_written.is_empty() {
        println!("\n════════════════════════════════════════════════════════════════");
        println!("  FILE SNAPSHOTS — WRITTEN/EDITED ({}):", proof.files_written.len());
        println!("════════════════════════════════════════════════════════════════");
        for f in &proof.files_written {
            let hash_hex: String = f.content_hash.iter().take(4).map(|b| format!("{:02x}", b)).collect();
            println!("\n  {} ({} bytes, BLAKE3: {})", f.path, f.size_bytes, hash_hex);
            println!("  ┌────────────────────────────────────────────────────────────");
            for line in f.snippet.lines().take(12) {
                println!("{}", truncate_line(line, 60));
            }
            if f.snippet.lines().count() > 12 || f.size_bytes > 500 {
                println!("  │ ... ({} total bytes)", f.size_bytes);
            }
            println!("  └────────────────────────────────────────────────────────────");
        }
    }

    if proof.verify_all().is_err() {
        std::process::exit(1);
    }
}

fn truncate_line(s: &str, max: usize) -> &str {
    if s.len() <= max { s } else { &s[..max] }
}

fn pad_right(s: &str, width: usize) -> String {
    if s.len() >= width { s.to_string() } else { format!("{}{}", s, " ".repeat(width - s.len())) }
}

fn verify_search_proof(proof: &canon_core::ProofReceipt) {
    println!("Search Proof Receipt v{}", proof.version);
    println!("  Query:     {}", proof.query);
    println!("  Timestamp: {}", proof.timestamp);

    println!("\nVerification:");

    match proof.verify_signature() {
        Ok(()) => println!("  [PASS] Ed25519 signature valid"),
        Err(e) => println!("  [FAIL] Ed25519 signature: {}", e),
    }

    match proof.verify_chunk_proofs() {
        Ok(()) => println!("  [PASS] Merkle proofs valid ({}/{} chunks)",
            proof.chunk_proofs.len(), proof.chunk_proofs.len()),
        Err(e) => println!("  [FAIL] Merkle proofs: {}", e),
    }

    if proof.git.is_some() {
        println!("  [PASS] Git context cryptographically bound");
    }

    match proof.verify_all() {
        Ok(()) => println!("\nResult: ALL CHECKS PASSED"),
        Err(e) => {
            println!("\nResult: VERIFICATION FAILED - {}", e);
            std::process::exit(1);
        }
    }

    if let Some(ref gc) = proof.git {
        println!("\nGit Context (signed):");
        if let Some(ref author) = gc.author { println!("  Author:  {}", author); }
        if let Some(ref commit) = gc.commit { println!("  Commit:  {}", commit); }
        if let Some(ref branch) = gc.branch { println!("  Branch:  {}", branch); }
        println!("  Dirty:   {}", gc.dirty);
    }

    println!("\n════════════════════════════════════════");
    println!("What Canon served ({} chunks):", proof.sources.len());
    println!("════════════════════════════════════════");
    for (i, src) in proof.sources.iter().enumerate() {
        println!("\n--- Chunk {} of {} ---", i + 1, proof.sources.len());
        println!("Source: {} (sequence {})", src.document_path, src.chunk_sequence);
        println!("Relevance: {:.3}", src.relevance_score);
        println!("Content:\n{}", src.chunk_text.trim());
    }
}

// ============================================================================
// Main
// ============================================================================

#[tokio::main]
async fn main() -> Result<()> {
    let cli = Cli::parse();

    match cli.command {
        Some(Commands::Init) => {
            init_project()
        }

        Some(Commands::SessionProof { data_dir }) => {
            // Lightweight mode — no embedding engine, no MCP server
            tracing_subscriber::fmt()
                .with_writer(std::io::stderr)
                .with_env_filter("warn")
                .init();

            std::fs::create_dir_all(&data_dir)?;
            generate_session_proof(&data_dir)
        }

        Some(Commands::Verify { path }) => {
            verify_proof(&path)
        }

        None => {
            // Default: MCP server mode
            tracing_subscriber::fmt()
                .with_writer(std::io::stderr)
                .with_env_filter(
                    tracing_subscriber::EnvFilter::from_default_env()
                        .add_directive("canon_mcp=info".parse()?)
                        .add_directive(tracing::Level::WARN.into()),
                )
                .init();

            std::fs::create_dir_all(&cli.data_dir)
                .context("Failed to create data directory")?;

            ensure_gitignore(&cli.data_dir)?;

            let db_path = cli.data_dir.join("graph.db");
            let graph = GraphStore::open(db_path.to_str().unwrap())
                .context("Failed to open graph store")?;
            let graph = Arc::new(Mutex::new(graph));

            let identity = load_or_create_identity(&cli.data_dir)
                .context("Failed to load device identity")?;

            info!(
                "Device identity: {}",
                identity.device_id.iter().map(|b| format!("{:02x}", b)).collect::<String>()
            );

            let embedder = Arc::new(EmbeddingEngine::new().context("Failed to initialize embedding engine")?);
            let query_engine = Arc::new(crate::search::QueryEngine::new(graph.clone(), embedder.clone()));

            let git_context = detect_git_context();
            if let Some(ref gc) = git_context {
                if let Some(ref commit) = gc.commit {
                    info!("Git context: commit={}, branch={:?}", &commit[..8.min(commit.len())], gc.branch);
                }
            }

            let _watcher_handle = if let Some(watch_dir) = cli.watch {
                let watch_path = watch_dir.canonicalize().unwrap_or(watch_dir);
                info!("Watching directory: {:?}", watch_path);
                let handle = watcher::start_watcher(
                    watch_path,
                    graph.clone(),
                    embedder.clone(),
                    cli.data_dir.clone(),
                )?;
                Some(handle)
            } else {
                None
            };

            std::fs::create_dir_all(cli.data_dir.join("proofs"))?;

            let canon_server = server::CanonServer::new(
                graph,
                query_engine,
                embedder,
                identity,
                cli.data_dir,
                git_context,
            );

            info!("Starting Canon Protocol MCP server on stdio...");
            canon_server.run_stdio().await?;

            Ok(())
        }
    }
}