wm-tools 9.1.2

Curated tool implementations for the WhiteMagic MCP server.
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
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
//! Multi-agent coordination — file-based claim leases (`code.claim` v0).
//!
//! Phase 2 of the v7→v9 strategy: the coordination substrate from the
//! two-writer postmortem. One writer per checkout is the rule; these tools
//! make the rule *visible*. Leases live in the git common dir
//! (`$(git rev-parse --git-common-dir)/wm-leases.json`) so every worktree
//! of a checkout reads and writes the same ledger.
//!
//! Design (from `planning/STRATEGY_V7_V9.md` Phase 2):
//! - Advisory only — claims are coordination signals, not locks. No
//!   enforcement hooks, no merge radar; those are v1/v2.
//! - Entries carry a mandatory intent, an owner session, and a TTL.
//!   Expired leases prune lazily and the scope frees itself — a dead
//!   session cannot hold a scope forever.
//! - All mutations are lockfile-guarded read-modify-write with atomic
//!   rename, so two agents racing on one ledger cannot corrupt it.
//! - Claim/release/denied publish to the Gan Ying bus when one is wired.
//!
//! Research surface: registered on the full profile only (curated stays
//! the alpha contract surface).

#![forbid(unsafe_code)]

use async_trait::async_trait;

use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use wm_cognitive::{EventType, GanYingBus};
use wm_core::{Context, CoreError, EffectRow, Gana, Resource, Tool, ToolStats};

/// Default lease TTL — one hour covers a focused work stretch; renewal is
/// a single re-claim with the same owner.
const DEFAULT_TTL_SECS: i64 = 3600;
/// Maximum TTL — a claim is coordination state, not a tombstone.
const MAX_TTL_SECS: i64 = 86_400;
/// Lockfile older than this is stale (a crashed writer) and gets stolen.
const STALE_LOCK_SECS: i64 = 30;
const LOCK_ATTEMPTS: usize = 150;
const LOCK_SLEEP_MS: u64 = 10;

/// One claim lease in the shared ledger.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Lease {
    /// What is claimed — a path, a subtree, or any resource label the
    /// agents agree on (`"src/expansion/"`, `"benchmarks/harness"`, …).
    pub scope: String,
    /// Why the scope is claimed. Mandatory — an intent-less claim is
    /// noise, and the conflict result must be able to explain the holder.
    pub intent: String,
    /// Claiming session (session id from `session.start`, or a stable
    /// agent label). Release requires a matching owner.
    pub owner_session: String,
    /// RFC 3339 claim time.
    pub claimed_at: String,
    /// RFC 3339 expiry — past this, the lease prunes lazily.
    pub expires_at: String,
    /// Requested TTL in seconds (recorded for diagnosis).
    pub ttl_secs: i64,
}

#[derive(Debug, Default, Serialize, Deserialize)]
struct LeaseFile {
    version: u8,
    leases: Vec<Lease>,
}

/// The shared ledger: `<git-common-dir>/wm-leases.json`.
#[derive(Debug, Clone)]
pub struct LeaseLedger {
    path: PathBuf,
}

impl LeaseLedger {
    /// Discover the ledger for a repository root. Requires a git checkout
    /// (worktree or bare-adjacent): the common dir is what makes leases
    /// visible across all worktrees.
    pub fn discover(root: &Path) -> wm_core::Result<Self> {
        let out = std::process::Command::new("git")
            .args(["rev-parse", "--git-common-dir"])
            .current_dir(root)
            .output()
            .map_err(|e| {
                CoreError::Tool(format!(
                    "code.claim could not run git ({e}) — pass root=<repo path> or set WM_PROJECT_ROOT to a git checkout"
                ))
            })?;
        if !out.status.success() {
            return Err(CoreError::Tool(
                "code.claim requires a git repository — pass root=<repo path> (or set WM_PROJECT_ROOT) pointing at a checkout; leases live in <git-common-dir>/wm-leases.json".into(),
            ));
        }
        let dir = String::from_utf8_lossy(&out.stdout).trim().to_string();
        if dir.is_empty() {
            return Err(CoreError::Tool(
                "git rev-parse --git-common-dir returned nothing for this root".into(),
            ));
        }
        let common = PathBuf::from(&dir);
        let common = if common.is_absolute() {
            common
        } else {
            root.join(common)
        };
        Ok(Self {
            path: common.join("wm-leases.json"),
        })
    }

    #[must_use]
    pub fn path(&self) -> &Path {
        &self.path
    }

    fn lock_path(&self) -> PathBuf {
        let name = self.path.file_name().map_or_else(
            || "wm-leases.json.lock".to_string(),
            |n| format!("{}.lock", n.to_string_lossy()),
        );
        self.path.with_file_name(name)
    }

    /// Acquire the ledger lock. create_new gives us an atomic test-and-set;
    /// a lockfile older than `STALE_LOCK_SECS` belongs to a crashed writer
    /// and is stolen.
    fn acquire_lock(&self) -> wm_core::Result<()> {
        let lock = self.lock_path();
        for _ in 0..LOCK_ATTEMPTS {
            match std::fs::OpenOptions::new()
                .write(true)
                .create_new(true)
                .open(&lock)
            {
                Ok(_) => return Ok(()),
                Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {
                    if let Ok(meta) = std::fs::metadata(&lock) {
                        if let Ok(modified) = meta.modified() {
                            let age = DateTime::<Utc>::from(modified);
                            if Utc::now() - age > Duration::seconds(STALE_LOCK_SECS) {
                                let _ = std::fs::remove_file(&lock);
                                continue;
                            }
                        }
                    }
                    std::thread::sleep(std::time::Duration::from_millis(LOCK_SLEEP_MS));
                }
                Err(e) => {
                    return Err(CoreError::Tool(format!(
                        "could not create lease lock {}: {e}",
                        lock.display()
                    )));
                }
            }
        }
        Err(CoreError::Tool(format!(
            "lease ledger is busy (lock held past {}s) — retry shortly: {}",
            (LOCK_ATTEMPTS as u64 * LOCK_SLEEP_MS) / 1000,
            lock.display()
        )))
    }

    fn release_lock(&self) {
        let _ = std::fs::remove_file(self.lock_path());
    }

    fn parse_file(&self) -> Vec<Lease> {
        let Ok(raw) = std::fs::read_to_string(&self.path) else {
            return Vec::new();
        };
        let Ok(parsed) = serde_json::from_str::<LeaseFile>(&raw) else {
            // A corrupt ledger must never wedge coordination: treat it as
            // empty (advisory system; the next successful write rebuilds).
            tracing::warn!(
                path = %self.path.display(),
                "wm-leases.json unreadable — treating ledger as empty"
            );
            return Vec::new();
        };
        parsed.leases
    }

    /// Run `f` against the active leases under the ledger lock, prune
    /// expired entries, and atomically persist the result. Expired leases
    /// are returned so callers can surface the transition.
    fn mutate<T>(
        &self,
        f: impl FnOnce(&mut Vec<Lease>, &[Lease]) -> wm_core::Result<T>,
    ) -> wm_core::Result<T> {
        if let Some(parent) = self.path.parent() {
            std::fs::create_dir_all(parent).map_err(|e| {
                CoreError::Tool(format!("could not create {}: {e}", parent.display()))
            })?;
        }
        self.acquire_lock()?;
        let result = (|| {
            let now = Utc::now();
            let all = self.parse_file();
            let (mut active, expired): (Vec<Lease>, Vec<Lease>) =
                all.into_iter()
                    .partition(|l| match DateTime::parse_from_rfc3339(&l.expires_at) {
                        Ok(exp) => exp.with_timezone(&Utc) > now,
                        Err(_) => false, // unparseable expiry = expired
                    });
            let pre = active.clone();
            let out = f(&mut active, &expired)?;
            // Persist only on real change — pure reads (check/list on a
            // stable ledger) must not rewrite the file, and an expired
            // lease is reported once, at its discovery transition.
            if active == pre && expired.is_empty() {
                return Ok(out);
            }
            let file = LeaseFile {
                version: 1,
                leases: active,
            };
            let tmp = self
                .path
                .with_file_name(format!("wm-leases.json.tmp.{}", std::process::id()));
            let body = serde_json::to_string_pretty(&file)
                .map_err(|e| CoreError::Tool(format!("lease serialization failed: {e}")))?;
            std::fs::write(&tmp, body)
                .map_err(|e| CoreError::Tool(format!("lease write failed: {e}")))?;
            std::fs::rename(&tmp, &self.path)
                .map_err(|e| CoreError::Tool(format!("lease atomic rename failed: {e}")))?;
            Ok(out)
        })();
        self.release_lock();
        result
    }

    /// Active leases plus anything that expired since the last write.
    pub(crate) fn snapshot(&self) -> wm_core::Result<(Vec<Lease>, Vec<Lease>)> {
        self.mutate(|active, expired| Ok((active.clone(), expired.to_vec())))
    }

    // ── Bridge API (F-1): mesh-side scope coordination ────────────────
    //
    // `wm-leases.json` is the durable, cross-process substrate for scope
    // coordination; the sangha `ResourceLockManager` is the per-process
    // mesh-runtime view. These methods let mesh tools record their scope
    // claims in the durable ledger so agents coordinating through either
    // surface see the same truth (`code.list`, `code.check`, or plain bash
    // against the ledger file).

    /// Claim a scope on behalf of `owner`, atomically.
    ///
    /// Same-owner re-claim is a renewal; a live claim by another owner is a
    /// conflict naming the holder. Expired entries prune lazily as in every
    /// other mutate.
    pub fn try_claim(
        &self,
        scope: &str,
        intent: &str,
        owner: &str,
        ttl_secs: i64,
    ) -> wm_core::Result<Result<Lease, Lease>> {
        let claimed_at = now_rfc3339();
        let expires_at = (Utc::now() + Duration::seconds(ttl_secs))
            .to_rfc3339_opts(chrono::SecondsFormat::Secs, true);
        self.mutate(|leases, _expired| {
            if let Some(existing) = leases.iter_mut().find(|l| l.scope == scope) {
                if existing.owner_session == owner {
                    existing.intent = intent.to_string();
                    existing.claimed_at.clone_from(&claimed_at);
                    existing.expires_at.clone_from(&expires_at);
                    existing.ttl_secs = ttl_secs;
                    return Ok(Ok(existing.clone()));
                }
                return Ok(Err(existing.clone()));
            }
            let lease = Lease {
                scope: scope.to_string(),
                intent: intent.to_string(),
                owner_session: owner.to_string(),
                claimed_at: claimed_at.clone(),
                expires_at: expires_at.clone(),
                ttl_secs,
            };
            leases.push(lease.clone());
            Ok(Ok(lease))
        })
    }

    /// Release a scope owner-matched. `Ok(Ok(true))` = released;
    /// `Ok(Ok(false))` = scope was already free (idempotent);
    /// `Ok(Err(holder))` = live claim held by another owner.
    pub fn release_scope(&self, scope: &str, owner: &str) -> wm_core::Result<Result<bool, Lease>> {
        self.mutate(|leases, _expired| {
            let Some(pos) = leases.iter().position(|l| l.scope == scope) else {
                return Ok(Ok(false));
            };
            if leases[pos].owner_session != owner {
                return Ok(Err(leases[pos].clone()));
            }
            leases.remove(pos);
            Ok(Ok(true))
        })
    }

    /// Force-release every live claim held by `peer` whose scope starts with
    /// `prefix`. This is the community override behind the bad-apple rule:
    /// quarantine must not leave a cut-off peer holding shared scopes.
    /// Deliberately bypasses owner matching — the mesh revoked the peer.
    pub fn force_release_peer(&self, peer: &str, prefix: &str) -> wm_core::Result<Vec<String>> {
        self.mutate(|leases, _expired| {
            let mut freed = Vec::new();
            leases.retain(|l| {
                if l.owner_session == peer && l.scope.starts_with(prefix) {
                    freed.push(l.scope.clone());
                    return false;
                }
                true
            });
            Ok(freed)
        })
    }
}

/// RFC 3339 at second precision — the human-facing lease ledger format
/// (convention: `wm_core::time`, unit registry in
/// `docs/TIMESTAMP_CONVENTIONS.md`).
fn now_rfc3339() -> String {
    wm_core::time::now_rfc3339()
}

pub(crate) fn clamp_ttl(ttl: i64) -> wm_core::Result<i64> {
    if !(1..=MAX_TTL_SECS).contains(&ttl) {
        return Err(CoreError::InvalidArgs(format!(
            "ttl_secs must be between 1 and {MAX_TTL_SECS} (a claim is coordination state, not a tombstone)"
        )));
    }
    Ok(ttl)
}

pub(crate) fn require_str(args: &Value, key: &str) -> wm_core::Result<String> {
    args.get(key)
        .and_then(|v| v.as_str())
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .map(str::to_string)
        .ok_or_else(|| {
            CoreError::InvalidArgs(format!(
                "'{key}' is required and must be a non-empty string"
            ))
        })
}

pub(crate) fn resolve_root(args: &Value) -> wm_core::Result<PathBuf> {
    args.get("root")
        .and_then(|v| v.as_str())
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .map(PathBuf::from)
        .or_else(|| {
            std::env::var("WM_PROJECT_ROOT")
                .ok()
                .filter(|s| !s.trim().is_empty())
                .map(PathBuf::from)
        })
        .ok_or_else(|| {
            CoreError::InvalidArgs(
                "no repository root — pass root=<repo path> or set WM_PROJECT_ROOT".into(),
            )
        })
}

fn emit(gan_ying: Option<&Arc<Mutex<GanYingBus>>>, event_type: EventType, payload: Value) {
    if let Some(bus) = gan_ying {
        if let Ok(mut gy) = bus.lock() {
            gy.emit(event_type, "code.coordination", payload);
        }
    }
}

fn lease_json(lease: &Lease) -> Value {
    json!({
        "lease_id": lease.scope,
        "scope": lease.scope,
        "intent": lease.intent,
        "owner_session": lease.owner_session,
        "claimed_at": lease.claimed_at,
        "expires_at": lease.expires_at,
        "ttl_secs": lease.ttl_secs,
    })
}

const CONFLICT_NEXT_ACTION: &str =
    "wait for expiry, ask the holder to code.release the scope, or claim a different scope";

// ── code.claim ────────────────────────────────────────────────────────

/// `code.claim` — claim a scope before shared-tree edits.
pub struct CodeClaimTool {
    stats: ToolStats,
    effects: EffectRow,
    gan_ying: Option<Arc<Mutex<GanYingBus>>>,
}

impl CodeClaimTool {
    #[must_use]
    pub fn new(gan_ying: Option<Arc<Mutex<GanYingBus>>>) -> Self {
        Self {
            stats: ToolStats::default(),
            // EffectRow declares data-plane effects (the ledger file), per
            // the session-tools precedent: the fixed-argv `git rev-parse`
            // lookup is the same trust class as checkpoint's git capture,
            // and declaring spawns would push every coordination call
            // against the Yama spawn budget (found live: a two-agent
            // negotiation burst exceeds 6 spawns/min).
            effects: EffectRow {
                reads: vec![Resource::Filesystem],
                writes: vec![Resource::Filesystem],
                ..Default::default()
            },
            gan_ying,
        }
    }
}

#[async_trait]
impl Tool for CodeClaimTool {
    fn name(&self) -> &str {
        "code.claim"
    }
    fn gana(&self) -> Gana {
        Gana::Room
    }
    fn effects(&self) -> &EffectRow {
        &self.effects
    }
    fn input_schema(&self) -> Value {
        super::common::schema(
            &json!({
                "scope": super::common::str_prop("Scope to claim — path, subtree, or resource label (e.g. 'src/expansion/')"),
                "intent": super::common::str_prop("Why this scope is claimed (mandatory — surfaced to conflicting agents)"),
                "owner_session": super::common::str_prop("Claiming session id (session.start result) or stable agent label"),
                "ttl_secs": super::common::int_prop("Lease TTL in seconds (default 3600, max 86400; expired claims free themselves)"),
                "root": super::common::str_prop("Repository root (default: WM_PROJECT_ROOT env)"),
            }),
            &["scope", "intent", "owner_session"],
        )
    }
    fn description(&self) -> &str {
        "Claim a scope before shared-tree edits (advisory file-based lease with TTL in the git common dir; visible to every worktree). Conflict results name the holder and their intent."
    }
    async fn call(&self, _ctx: &mut Context, args: Value) -> wm_core::Result<Value> {
        let scope = require_str(&args, "scope")?;
        let intent = require_str(&args, "intent")?;
        let owner = require_str(&args, "owner_session")?;
        let ttl = match args.get("ttl_secs").and_then(serde_json::Value::as_i64) {
            Some(t) => clamp_ttl(t)?,
            None => DEFAULT_TTL_SECS,
        };
        let root = resolve_root(&args)?;
        let ledger = LeaseLedger::discover(&root)?;
        let claimed_at = now_rfc3339();
        let expires_at = (Utc::now() + Duration::seconds(ttl))
            .to_rfc3339_opts(chrono::SecondsFormat::Secs, true);

        let mut newly_expired: Vec<Lease> = Vec::new();
        let result = ledger.mutate(|leases, expired| {
            newly_expired = expired.to_vec();
            if let Some(existing) = leases.iter_mut().find(|l| l.scope == scope) {
                if existing.owner_session == owner {
                    // Renewal: refresh intent and TTL, keep the claim.
                    existing.intent.clone_from(&intent);
                    existing.claimed_at.clone_from(&claimed_at);
                    existing.expires_at.clone_from(&expires_at);
                    existing.ttl_secs = ttl;
                    return Ok(json!({
                        "status": "success",
                        "renewed": true,
                        "lease_id": scope,
                        "scope": scope,
                        "intent": intent,
                        "owner_session": owner,
                        "claimed_at": claimed_at,
                        "expires_at": expires_at,
                        "ttl_secs": ttl,
                        "note": "advisory lease renewed — release with code.release when done"
                    }));
                }
                let holder = existing.clone();
                return Ok(json!({
                    "status": "conflict",
                    "scope": scope,
                    "requested_by": owner,
                    "holder": holder.owner_session,
                    "holder_intent": holder.intent,
                    "claimed_at": holder.claimed_at,
                    "expires_at": holder.expires_at,
                    "next_action": CONFLICT_NEXT_ACTION,
                    "advisory": true,
                }));
            }
            leases.push(Lease {
                scope: scope.clone(),
                intent: intent.clone(),
                owner_session: owner.clone(),
                claimed_at: claimed_at.clone(),
                expires_at: expires_at.clone(),
                ttl_secs: ttl,
            });
            Ok(json!({
                "status": "success",
                "lease_id": scope,
                "scope": scope,
                "intent": intent,
                "owner_session": owner,
                "claimed_at": claimed_at,
                "expires_at": expires_at,
                "ttl_secs": ttl,
                "note": "advisory lease — release with code.release when done"
            }))
        });

        for lease in &newly_expired {
            emit(
                self.gan_ying.as_ref(),
                EventType::CoordinationClaimExpired,
                json!({"scope": lease.scope, "owner_session": lease.owner_session}),
            );
        }

        let result = result?;
        if result["status"] == "success" {
            emit(
                self.gan_ying.as_ref(),
                EventType::CoordinationClaimAcquired,
                json!({"scope": scope, "owner_session": owner, "intent": intent}),
            );
        } else if result["status"] == "conflict" {
            emit(
                self.gan_ying.as_ref(),
                EventType::CoordinationClaimDenied,
                json!({"scope": scope, "requested_by": owner, "holder": result["holder"]}),
            );
        }
        Ok(result)
    }
    fn stats(&self) -> &ToolStats {
        &self.stats
    }
}

// ── code.check ────────────────────────────────────────────────────────

/// `code.check` — is a scope claimed?
pub struct CodeCheckTool {
    stats: ToolStats,
    effects: EffectRow,
    gan_ying: Option<Arc<Mutex<GanYingBus>>>,
}

impl CodeCheckTool {
    #[must_use]
    pub fn new(gan_ying: Option<Arc<Mutex<GanYingBus>>>) -> Self {
        Self {
            stats: ToolStats::default(),
            effects: EffectRow {
                reads: vec![Resource::Filesystem],
                ..Default::default()
            },
            gan_ying,
        }
    }
}

#[async_trait]
impl Tool for CodeCheckTool {
    fn name(&self) -> &str {
        "code.check"
    }
    fn gana(&self) -> Gana {
        Gana::Room
    }
    fn effects(&self) -> &EffectRow {
        &self.effects
    }
    fn input_schema(&self) -> Value {
        super::common::schema(
            &json!({
                "scope": super::common::str_prop("Scope to check"),
                "root": super::common::str_prop("Repository root (default: WM_PROJECT_ROOT env)"),
            }),
            &["scope"],
        )
    }
    fn description(&self) -> &str {
        "Check whether a scope is claimed — reports the holder, their intent, and expiry when claimed; 'free' means no active lease."
    }
    async fn call(&self, _ctx: &mut Context, args: Value) -> wm_core::Result<Value> {
        let scope = require_str(&args, "scope")?;
        let root = resolve_root(&args)?;
        let ledger = LeaseLedger::discover(&root)?;

        let mut newly_expired: Vec<Lease> = Vec::new();
        let holder = ledger.mutate(|leases, expired| {
            newly_expired = expired.to_vec();
            Ok(leases.iter().find(|l| l.scope == scope).map(lease_json))
        })?;

        for lease in &newly_expired {
            emit(
                self.gan_ying.as_ref(),
                EventType::CoordinationClaimExpired,
                json!({"scope": lease.scope, "owner_session": lease.owner_session}),
            );
        }

        match holder {
            Some(h) => Ok(json!({
                "status": "success",
                "scope": scope,
                "state": "claimed",
                "holder": h["owner_session"],
                "intent": h["intent"],
                "expires_at": h["expires_at"],
                "next_action": CONFLICT_NEXT_ACTION,
            })),
            None => Ok(json!({
                "status": "success",
                "scope": scope,
                "state": "free",
            })),
        }
    }
    fn stats(&self) -> &ToolStats {
        &self.stats
    }
}

// ── code.release ──────────────────────────────────────────────────────

/// `code.release` — release a claimed scope (owner must match).
pub struct CodeReleaseTool {
    stats: ToolStats,
    effects: EffectRow,
    gan_ying: Option<Arc<Mutex<GanYingBus>>>,
}

impl CodeReleaseTool {
    #[must_use]
    pub fn new(gan_ying: Option<Arc<Mutex<GanYingBus>>>) -> Self {
        Self {
            stats: ToolStats::default(),
            // EffectRow declares data-plane effects (the ledger file), per
            // the session-tools precedent: the fixed-argv `git rev-parse`
            // lookup is the same trust class as checkpoint's git capture,
            // and declaring spawns would push every coordination call
            // against the Yama spawn budget (found live: a two-agent
            // negotiation burst exceeds 6 spawns/min).
            effects: EffectRow {
                reads: vec![Resource::Filesystem],
                writes: vec![Resource::Filesystem],
                ..Default::default()
            },
            gan_ying,
        }
    }
}

#[async_trait]
impl Tool for CodeReleaseTool {
    fn name(&self) -> &str {
        "code.release"
    }
    fn gana(&self) -> Gana {
        Gana::Room
    }
    fn effects(&self) -> &EffectRow {
        &self.effects
    }
    fn input_schema(&self) -> Value {
        super::common::schema(
            &json!({
                "scope": super::common::str_prop("Scope to release (the claim's lease_id)"),
                "owner_session": super::common::str_prop("Releasing session id — must match the claim's owner"),
                "root": super::common::str_prop("Repository root (default: WM_PROJECT_ROOT env)"),
            }),
            &["scope", "owner_session"],
        )
    }
    fn description(&self) -> &str {
        "Release a claimed scope when work is done — only the owning session can release; releasing a free scope is an idempotent no-op."
    }
    async fn call(&self, _ctx: &mut Context, args: Value) -> wm_core::Result<Value> {
        let scope = require_str(&args, "scope")?;
        let owner = require_str(&args, "owner_session")?;
        let root = resolve_root(&args)?;
        let ledger = LeaseLedger::discover(&root)?;

        let outcome = ledger.mutate(|leases, _expired| {
            let Some(pos) = leases.iter().position(|l| l.scope == scope) else {
                return Ok(json!({
                    "status": "success",
                    "scope": scope,
                    "state": "free",
                    "note": "no active claim on this scope (already released or expired)"
                }));
            };
            let lease = &leases[pos];
            if lease.owner_session != owner {
                return Ok(json!({
                    "status": "not_owner",
                    "scope": scope,
                    "holder": lease.owner_session,
                    "holder_intent": lease.intent,
                    "expires_at": lease.expires_at,
                    "note": "only the owning session can release a claim",
                }));
            }
            leases.remove(pos);
            Ok(json!({
                "status": "success",
                "scope": scope,
                "state": "released",
                "owner_session": owner,
            }))
        })?;

        if outcome["status"] == "success" && outcome["state"] == "released" {
            emit(
                self.gan_ying.as_ref(),
                EventType::CoordinationClaimReleased,
                json!({"scope": scope, "owner_session": owner}),
            );
        }
        Ok(outcome)
    }
    fn stats(&self) -> &ToolStats {
        &self.stats
    }
}

// ── code.list ─────────────────────────────────────────────────────────

/// `code.list` — list claims in the shared ledger.
pub struct CodeListTool {
    stats: ToolStats,
    effects: EffectRow,
}

impl CodeListTool {
    #[must_use]
    pub fn new() -> Self {
        Self {
            stats: ToolStats::default(),
            effects: EffectRow {
                reads: vec![Resource::Filesystem],
                ..Default::default()
            },
        }
    }
}

impl Default for CodeListTool {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl Tool for CodeListTool {
    fn name(&self) -> &str {
        "code.list"
    }
    fn gana(&self) -> Gana {
        Gana::Room
    }
    fn effects(&self) -> &EffectRow {
        &self.effects
    }
    fn input_schema(&self) -> Value {
        super::common::schema(
            &json!({
                "include_expired": super::common::bool_prop("Include expired leases in the listing (default false)"),
                "root": super::common::str_prop("Repository root (default: WM_PROJECT_ROOT env)"),
            }),
            &[],
        )
    }
    fn description(&self) -> &str {
        "List active claims in the shared lease ledger — what each agent is holding and until when."
    }
    async fn call(&self, _ctx: &mut Context, args: Value) -> wm_core::Result<Value> {
        let include_expired = args
            .get("include_expired")
            .and_then(serde_json::Value::as_bool)
            .unwrap_or(false);
        let root = resolve_root(&args)?;
        let ledger = LeaseLedger::discover(&root)?;
        let (active, expired) = ledger.snapshot()?;
        let mut leases: Vec<Value> = active.iter().map(lease_json).collect();
        if include_expired {
            let mut expired_json: Vec<Value> = expired
                .iter()
                .map(|l| {
                    let mut v = lease_json(l);
                    v["expired"] = json!(true);
                    v
                })
                .collect();
            leases.append(&mut expired_json);
        }
        let count = leases.len();
        Ok(json!({
            "status": "success",
            "count": count,
            "leases": leases,
            "file": ledger.path().display().to_string(),
        }))
    }
    fn stats(&self) -> &ToolStats {
        &self.stats
    }
}

/// Register the coordination tools (full-profile surface).
#[must_use]
pub fn register_coordination(
    registry: &wm_dispatch::ToolRegistry,
    gan_ying_bus: Option<&Arc<Mutex<GanYingBus>>>,
) -> wm_dispatch::ToolRegistry {
    registry
        .register(Arc::new(CodeClaimTool::new(gan_ying_bus.cloned())))
        .register(Arc::new(CodeCheckTool::new(gan_ying_bus.cloned())))
        .register(Arc::new(CodeReleaseTool::new(gan_ying_bus.cloned())))
        .register(Arc::new(CodeListTool::new()))
}

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

    /// Fresh local git repository with one empty initial commit.
    fn git_repo() -> (tempfile::TempDir, PathBuf) {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path().to_path_buf();
        let run = |args: &[&str]| {
            std::process::Command::new("git")
                .args(args)
                .current_dir(&root)
                .output()
                .expect("git must be available")
        };
        assert!(run(&["init", "-q"]).status.success());
        assert!(run(&["config", "user.email", "t@t"]).status.success());
        assert!(run(&["config", "user.name", "t"]).status.success());
        assert!(
            run(&["commit", "--allow-empty", "-m", "c1"])
                .status
                .success()
        );
        (dir, root)
    }

    fn root_str(root: &Path) -> Value {
        json!(root.display().to_string())
    }

    #[tokio::test]
    async fn claim_then_conflict_names_holder_and_intent() {
        let (_guard, root) = git_repo();
        let a = CodeClaimTool::new(None);
        let b = CodeClaimTool::new(None);
        let mut ctx = Context::default();

        let first = a
            .call(
                &mut ctx,
                json!({
                    "scope": "src/expansion/",
                    "intent": "refactoring session tools",
                    "owner_session": "session-aaa",
                    "root": root_str(&root),
                }),
            )
            .await
            .unwrap();
        assert_eq!(first["status"], "success", "got: {first}");
        assert_eq!(first["lease_id"], "src/expansion/");
        assert_eq!(first["owner_session"], "session-aaa");

        let second = b
            .call(
                &mut ctx,
                json!({
                    "scope": "src/expansion/",
                    "intent": "unrelated edits",
                    "owner_session": "session-bbb",
                    "root": root_str(&root),
                }),
            )
            .await
            .unwrap();
        assert_eq!(second["status"], "conflict", "got: {second}");
        assert_eq!(second["holder"], "session-aaa");
        assert_eq!(second["holder_intent"], "refactoring session tools");
        assert!(
            second["next_action"]
                .as_str()
                .unwrap()
                .contains("code.release")
        );
    }

    #[tokio::test]
    async fn check_reports_claimed_then_free_zero_false_free() {
        let (_guard, root) = git_repo();
        let claim = CodeClaimTool::new(None);
        let check = CodeCheckTool::new(None);
        let mut ctx = Context::default();

        let free = check
            .call(&mut ctx, json!({"scope": "docs/", "root": root_str(&root)}))
            .await
            .unwrap();
        assert_eq!(free["state"], "free");

        claim
            .call(
                &mut ctx,
                json!({
                    "scope": "docs/",
                    "intent": "doc rewrite",
                    "owner_session": "session-aaa",
                    "root": root_str(&root),
                }),
            )
            .await
            .unwrap();

        let claimed = check
            .call(&mut ctx, json!({"scope": "docs/", "root": root_str(&root)}))
            .await
            .unwrap();
        assert_eq!(claimed["state"], "claimed", "got: {claimed}");
        assert_eq!(claimed["holder"], "session-aaa");
        assert_eq!(claimed["intent"], "doc rewrite");
    }

    #[tokio::test]
    async fn release_requires_owner_then_scope_frees() {
        let (_guard, root) = git_repo();
        let claim = CodeClaimTool::new(None);
        let release = CodeReleaseTool::new(None);
        let check = CodeCheckTool::new(None);
        let mut ctx = Context::default();

        claim
            .call(
                &mut ctx,
                json!({
                    "scope": "src/foo.rs",
                    "intent": "bugfix",
                    "owner_session": "session-aaa",
                    "root": root_str(&root),
                }),
            )
            .await
            .unwrap();

        let wrong = release
            .call(
                &mut ctx,
                json!({
                    "scope": "src/foo.rs",
                    "owner_session": "session-bbb",
                    "root": root_str(&root),
                }),
            )
            .await
            .unwrap();
        assert_eq!(wrong["status"], "not_owner", "got: {wrong}");
        assert_eq!(wrong["holder"], "session-aaa");

        let still = check
            .call(
                &mut ctx,
                json!({"scope": "src/foo.rs", "root": root_str(&root)}),
            )
            .await
            .unwrap();
        assert_eq!(
            still["state"], "claimed",
            "release must not free others' claims"
        );

        let right = release
            .call(
                &mut ctx,
                json!({
                    "scope": "src/foo.rs",
                    "owner_session": "session-aaa",
                    "root": root_str(&root),
                }),
            )
            .await
            .unwrap();
        assert_eq!(right["status"], "success");
        assert_eq!(right["state"], "released");

        let freed = check
            .call(
                &mut ctx,
                json!({"scope": "src/foo.rs", "root": root_str(&root)}),
            )
            .await
            .unwrap();
        assert_eq!(
            freed["state"], "free",
            "zero false free: freed after owner release"
        );

        // Release again — idempotent no-op.
        let again = release
            .call(
                &mut ctx,
                json!({
                    "scope": "src/foo.rs",
                    "owner_session": "session-aaa",
                    "root": root_str(&root),
                }),
            )
            .await
            .unwrap();
        assert_eq!(again["status"], "success");
        assert_eq!(again["state"], "free");
    }

    #[tokio::test]
    async fn expired_lease_frees_scope() {
        let (_guard, root) = git_repo();
        let claim = CodeClaimTool::new(None);
        let check = CodeCheckTool::new(None);
        let mut ctx = Context::default();

        claim
            .call(
                &mut ctx,
                json!({
                    "scope": "src/stale.rs",
                    "intent": "dead session's claim",
                    "owner_session": "session-dead",
                    "root": root_str(&root),
                }),
            )
            .await
            .unwrap();

        // Backdate the expiry in the ledger directly — simulates the passage
        // of time without a sleep.
        let path = LeaseLedger::discover(&root).unwrap().path().to_path_buf();
        let raw = std::fs::read_to_string(&path).unwrap();
        let mut file: LeaseFile = serde_json::from_str(&raw).unwrap();
        file.leases[0].expires_at = "2020-01-01T00:00:00Z".into();
        std::fs::write(&path, serde_json::to_string(&file).unwrap()).unwrap();

        let freed = check
            .call(
                &mut ctx,
                json!({"scope": "src/stale.rs", "root": root_str(&root)}),
            )
            .await
            .unwrap();
        assert_eq!(freed["state"], "free", "expired claims free the scope");

        // A different owner can claim the freed scope immediately.
        let other = CodeClaimTool::new(None);
        let taken = other
            .call(
                &mut ctx,
                json!({
                    "scope": "src/stale.rs",
                    "intent": "rescued work",
                    "owner_session": "session-live",
                    "root": root_str(&root),
                }),
            )
            .await
            .unwrap();
        assert_eq!(taken["status"], "success", "got: {taken}");
    }

    #[tokio::test]
    async fn ledger_visible_across_independent_tool_instances() {
        // Two "agents" = two tool instances with no shared state except the
        // file in the git common dir (the deployment shape: separate server
        // processes over one checkout).
        let (_guard, root) = git_repo();
        let claim_a = CodeClaimTool::new(None);
        let list_b = CodeListTool::new();
        let check_b = CodeCheckTool::new(None);
        let mut ctx = Context::default();

        claim_a
            .call(
                &mut ctx,
                json!({
                    "scope": "worktree-A",
                    "intent": "agent A rewriting the harness",
                    "owner_session": "session-aaa",
                    "root": root_str(&root),
                }),
            )
            .await
            .unwrap();

        let listed = list_b
            .call(&mut ctx, json!({"root": root_str(&root)}))
            .await
            .unwrap();
        assert_eq!(listed["status"], "success");
        assert_eq!(listed["count"], 1, "got: {listed}");
        assert_eq!(listed["leases"][0]["scope"], "worktree-A");
        assert_eq!(
            listed["leases"][0]["intent"],
            "agent A rewriting the harness"
        );
        assert!(listed["file"].as_str().unwrap().contains("wm-leases.json"));

        let seen = check_b
            .call(
                &mut ctx,
                json!({"scope": "worktree-A", "root": root_str(&root)}),
            )
            .await
            .unwrap();
        assert_eq!(seen["state"], "claimed");
        assert_eq!(seen["holder"], "session-aaa");
    }

    #[tokio::test]
    async fn renew_own_claim_keeps_single_entry() {
        let (_guard, root) = git_repo();
        let claim = CodeClaimTool::new(None);
        let list = CodeListTool::new();
        let mut ctx = Context::default();

        for ttl in [7200i64, 60i64] {
            let r = claim
                .call(
                    &mut ctx,
                    json!({
                        "scope": "src/renewed.rs",
                        "intent": "long-running refactor",
                        "owner_session": "session-aaa",
                        "ttl_secs": ttl,
                        "root": root_str(&root),
                    }),
                )
                .await
                .unwrap();
            assert_eq!(r["status"], "success", "got: {r}");
        }
        let listed = list
            .call(&mut ctx, json!({"root": root_str(&root)}))
            .await
            .unwrap();
        assert_eq!(listed["count"], 1, "renewal must not duplicate entries");
        assert_eq!(listed["leases"][0]["ttl_secs"], 60);
    }

    #[tokio::test]
    async fn claim_rejects_missing_intent_and_bad_ttl() {
        let (_guard, root) = git_repo();
        let claim = CodeClaimTool::new(None);
        let mut ctx = Context::default();

        let no_intent = claim
            .call(
                &mut ctx,
                json!({
                    "scope": "src/x.rs",
                    "owner_session": "s",
                    "root": root_str(&root),
                }),
            )
            .await;
        assert!(no_intent.is_err(), "intent is mandatory");

        let bad_ttl = claim
            .call(
                &mut ctx,
                json!({
                    "scope": "src/x.rs",
                    "intent": "y",
                    "owner_session": "s",
                    "ttl_secs": 0,
                    "root": root_str(&root),
                }),
            )
            .await;
        assert!(bad_ttl.is_err(), "ttl_secs must be >= 1");
    }

    #[tokio::test]
    async fn non_git_root_is_a_clear_error() {
        let dir = tempfile::tempdir().unwrap();
        let claim = CodeClaimTool::new(None);
        let mut ctx = Context::default();
        let err = claim
            .call(
                &mut ctx,
                json!({
                    "scope": "src/x.rs",
                    "intent": "y",
                    "owner_session": "s",
                    "root": dir.path().display().to_string(),
                }),
            )
            .await
            .unwrap_err();
        assert!(err.to_string().contains("git repository"), "got: {err}");
    }

    #[tokio::test]
    async fn list_hides_expired_by_default_but_can_include_them() {
        let (_guard, root) = git_repo();
        let claim = CodeClaimTool::new(None);
        let list = CodeListTool::new();
        let mut ctx = Context::default();

        claim
            .call(
                &mut ctx,
                json!({
                    "scope": "src/old.rs",
                    "intent": "long gone",
                    "owner_session": "session-old",
                    "root": root_str(&root),
                }),
            )
            .await
            .unwrap();

        let path = LeaseLedger::discover(&root).unwrap().path().to_path_buf();
        let raw = std::fs::read_to_string(&path).unwrap();
        let mut file: LeaseFile = serde_json::from_str(&raw).unwrap();
        file.leases[0].expires_at = "2020-01-01T00:00:00Z".into();
        std::fs::write(&path, serde_json::to_string(&file).unwrap()).unwrap();

        // First observation discovers the expiry (and prunes it), so the
        // inclusive listing must come first.
        let with_expired = list
            .call(
                &mut ctx,
                json!({"root": root_str(&root), "include_expired": true}),
            )
            .await
            .unwrap();
        assert_eq!(with_expired["count"], 1);
        assert_eq!(with_expired["leases"][0]["expired"], true);

        let default_list = list
            .call(&mut ctx, json!({"root": root_str(&root)}))
            .await
            .unwrap();
        assert_eq!(default_list["count"], 0, "expired leases hidden by default");
    }
}