lean-ctx-engine 3.12.3

LeanCTX | OSS library: the open-source runtime for the Context Gateway for AI agents behind the lean-ctx CLI, MCP server and proxy integrations.
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
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
// SPDX-License-Identifier: Apache-2.0
//! Emspace provider — the pages, tasks and search of the space a project is
//! linked to, read through the local `emspace` CLI.
//!
//! A project is linked by a `.emspace` file at or above its root; the CLI
//! resolves that link itself, so the provider only runs it from the project
//! root. Every call is read-only: page reads go through `emspace run --query`,
//! which refuses a write, and the other actions are list/search commands.
//!
//! Pages are indexed as wiki pages whose references are the repository paths
//! their `derived` sources pin, so `ctx_read` of a documented file gets a
//! `documents` hint naming the page. Only sources under a registered emspace
//! dir that belongs to this repository (same checkout or same git remote)
//! become references; a dir of another repository would name the wrong file.
//!
//! The CLI is optional: without the binary or the link the provider reports
//! itself unavailable and never runs anything.

use std::io::{Seek, Write};
use std::path::{Path, PathBuf};
use std::time::Duration;

use serde::Deserialize;

use super::provider_trait::{CapabilityLevel, ConnectorDescriptor};
use super::{ContextProvider, ProviderItem, ProviderParams, ProviderResult};

const DEFAULT_PAGE_LIMIT: usize = 1000;
const MAX_PAGE_LIMIT: usize = 5000;
const DEFAULT_LIMIT: usize = 20;
const MAX_LIMIT: usize = 200;
/// Indexed body prefix per page in a listing; `id=<slug>` reads one page whole.
const LISTED_BODY_CHARS: usize = 6000;
/// Files one glob pin may contribute as references.
const MAX_GLOB_FILES: usize = 256;
const MAX_STDOUT_BYTES: usize = 16 * 1024 * 1024;
const MAX_STDERR_BYTES: usize = 16_384;
const CALL_TIMEOUT: Duration = Duration::from_secs(30);

/// Read-only program for `emspace run --query`: the pages, their derived
/// source locators and the registered dirs. `__PARAMS__` is replaced by a
/// JSON object, which is also a JavaScript literal.
const PAGES_PROGRAM: &str = r"const P = __PARAMS__;
const all = P.slug === null ? space.pages.list() : [space.pages.get(P.slug)].filter(Boolean);
const pages = all.filter(p => P.status === null || p.status === P.status
  || (P.status === 'stale' && p.stale) || (P.status === 'noted' && p.noted));
const listed = pages.slice(0, P.limit);
const head = space.journal({ limit: 1 });
return {
  lsn: head.length ? head[0].lsn : 0,
  dirs: space.dirs.list().map(d => ({ name: d.name, localPath: d.localPath || null, remote: d.remote || null })),
  total: pages.length,
  pages: listed.map(p => ({
    slug: p.slug, title: p.title, type: p.type, status: p.status, stale: p.stale, noted: p.noted,
    description: p.description || null, createdAt: p.createdAt, updatedAt: p.updatedAt,
    body: P.bodyChars === null || p.body.length <= P.bodyChars ? p.body : p.body.slice(0, P.bodyChars),
    derived: space.pages.sources(p.slug).filter(s => s.role === 'derived').map(s => s.locator),
  })),
};
";

/// Read-only program for the newest journal LSN: every write to the space
/// (pages, their sources, dirs, tasks, notes, sessions) advances it, so an
/// unchanged LSN means an unchanged space. About 40 ms.
const HEAD_PROGRAM: &str =
    "const head = space.journal({ limit: 1 }); return { lsn: head.length ? head[0].lsn : 0 };";

pub struct EmspaceProvider {
    project_root: PathBuf,
    binary: Option<PathBuf>,
}

impl EmspaceProvider {
    /// The provider for `project_root`; available only when the root is linked
    /// to a space and the `emspace` binary can be found.
    pub fn new(project_root: &Path) -> Self {
        let binary = find_link(project_root).and_then(|_| find_binary());
        Self {
            project_root: project_root.to_path_buf(),
            binary,
        }
    }

    fn binary(&self) -> Result<&Path, String> {
        self.binary.as_deref().ok_or_else(|| {
            "emspace not available (needs a .emspace link and the emspace CLI)".into()
        })
    }

    /// Run `emspace <command…> --json [-- <operands>]`: the flag goes before a
    /// `--`, where it would otherwise be read as an operand.
    fn run_cli(&self, args: &[&str], stdin: Option<&str>) -> Result<serde_json::Value, String> {
        let split = args.iter().position(|a| *a == "--").unwrap_or(args.len());
        let mut command = std::process::Command::new(self.binary()?);
        command
            .args(&args[..split])
            .arg("--json")
            .args(&args[split..])
            .current_dir(&self.project_root)
            // A bound session would stamp nothing (every call reads), but the
            // provider must not borrow the host agent's session either.
            .env_remove("EMSPACE_SESSION");
        match stdin {
            Some(program) => {
                let mut file = tempfile::tempfile()
                    .map_err(|e| format!("emspace program buffer failed: {e}"))?;
                file.write_all(program.as_bytes())
                    .and_then(|()| file.rewind())
                    .map_err(|e| format!("emspace program buffer failed: {e}"))?;
                command.stdin(std::process::Stdio::from(file));
            }
            // Never inherit the server's stdin: under MCP it is the protocol pipe.
            None => {
                command.stdin(std::process::Stdio::null());
            }
        }
        let captured = crate::core::process_capture::run_with_output_limits(
            &mut command,
            Some(CALL_TIMEOUT),
            MAX_STDOUT_BYTES,
            MAX_STDERR_BYTES,
        )
        .map_err(|_| "emspace command failed or exceeded its output bound".to_owned())?;
        if captured.timed_out {
            return Err("emspace command exceeded its deadline".into());
        }
        if captured.cancelled || !captured.output.status.success() {
            let stderr = String::from_utf8_lossy(&captured.output.stderr);
            let reason = stderr.lines().next().unwrap_or("no diagnostics").trim();
            return Err(format!(
                "emspace {} failed: {}",
                args.first().copied().unwrap_or(""),
                crate::core::redaction::redact_text_if_enabled(reason)
            ));
        }
        serde_json::from_slice(&captured.output.stdout)
            .map_err(|e| format!("emspace returned invalid JSON: {e}"))
    }

    fn pages(&self, params: &ProviderParams) -> Result<ProviderResult, String> {
        self.pages_at(params).map(|(result, _)| result)
    }

    /// The newest journal LSN of the space, without reading any page.
    fn head_lsn(&self) -> Result<u64, String> {
        let answer = self.run_cli(&["run", "--query"], Some(HEAD_PROGRAM))?;
        answer
            .pointer("/result/lsn")
            .and_then(serde_json::Value::as_u64)
            .ok_or_else(|| "unexpected emspace journal answer".to_owned())
    }

    /// [`Self::pages`] and the journal LSN the listing was read at, from the
    /// same read-only transaction.
    fn pages_at(&self, params: &ProviderParams) -> Result<(ProviderResult, u64), String> {
        let slug = params.id.as_deref().filter(|s| !s.is_empty());
        let status = params.state.as_deref().filter(|s| !s.is_empty());
        if let Some(status) = status
            && !matches!(status, "draft" | "canonical" | "stale" | "noted")
        {
            return Err(format!(
                "unknown page state '{status}' (draft, canonical, stale or noted)"
            ));
        }
        let limit = params
            .limit
            .unwrap_or(DEFAULT_PAGE_LIMIT)
            .clamp(1, MAX_PAGE_LIMIT);
        let program_params = serde_json::json!({
            "slug": slug,
            "status": status,
            "limit": limit,
            "bodyChars": if slug.is_some() { None } else { Some(LISTED_BODY_CHARS) },
        });
        let program = PAGES_PROGRAM.replacen("__PARAMS__", &program_params.to_string(), 1);
        let answer = self.run_cli(&["run", "--query"], Some(&program))?;
        let listing: PagesAnswer = serde_json::from_value(
            answer
                .get("result")
                .cloned()
                .unwrap_or(serde_json::Value::Null),
        )
        .map_err(|e| format!("unexpected emspace page listing: {e}"))?;
        let repo_dirs = repository_dirs(&listing.dirs, &self.project_root);
        let items: Vec<ProviderItem> = listing
            .pages
            .into_iter()
            .map(|page| page_item(page, &repo_dirs, &self.project_root))
            .collect();
        Ok((
            ProviderResult {
                provider: "emspace".into(),
                resource_type: "pages".into(),
                truncated: listing.total > items.len(),
                total_count: Some(listing.total),
                items,
            },
            listing.lsn,
        ))
    }

    fn search(&self, params: &ProviderParams) -> Result<ProviderResult, String> {
        let query = params
            .query
            .as_deref()
            .map(str::trim)
            .filter(|q| !q.is_empty())
            .ok_or("search needs a query")?;
        let limit = params.limit.unwrap_or(DEFAULT_LIMIT).clamp(1, MAX_LIMIT);
        let limit_arg = limit.to_string();
        let answer = self.run_cli(&["search", "--limit", &limit_arg, "--", query], None)?;
        let hits: Vec<SearchHit> = serde_json::from_value(answer)
            .map_err(|e| format!("unexpected emspace search answer: {e}"))?;
        let items: Vec<ProviderItem> = hits.into_iter().map(search_item).collect();
        Ok(ProviderResult {
            provider: "emspace".into(),
            resource_type: "search".into(),
            total_count: Some(items.len()),
            truncated: items.len() >= limit,
            items,
        })
    }

    fn tasks(&self, params: &ProviderParams) -> Result<ProviderResult, String> {
        let limit = params.limit.unwrap_or(DEFAULT_LIMIT).clamp(1, MAX_LIMIT);
        let limit_arg = limit.to_string();
        let mut args = vec!["task", "list", "--limit", limit_arg.as_str()];
        if let Some(status) = params.state.as_deref().filter(|s| !s.is_empty()) {
            if !matches!(status, "backlog" | "todo" | "doing" | "done" | "dropped") {
                return Err(format!(
                    "unknown task state '{status}' (backlog, todo, doing, done or dropped)"
                ));
            }
            args.extend(["--status", status]);
        }
        let answer = self.run_cli(&args, None)?;
        let tasks: Vec<TaskRecord> = serde_json::from_value(answer)
            .map_err(|e| format!("unexpected emspace task listing: {e}"))?;
        let items: Vec<ProviderItem> = tasks.into_iter().map(task_item).collect();
        Ok(ProviderResult {
            provider: "emspace".into(),
            resource_type: "tasks".into(),
            total_count: Some(items.len()),
            truncated: items.len() >= limit,
            items,
        })
    }
}

impl EmspaceProvider {
    /// One page to read (`mode="text"`): `id=<slug>`, and in `query`
    /// nothing (the whole body), `outline` (its sections with their sizes),
    /// or one section — by its slug path (`purpose`, `how-it-works`), its
    /// last segment or its heading text. Reading the outline and then the
    /// section a hint points at costs a fraction of the whole body.
    fn page(&self, params: &ProviderParams) -> Result<ProviderResult, String> {
        let slug = params
            .id
            .as_deref()
            .filter(|s| !s.is_empty())
            .ok_or("page needs id=<slug>")?;
        let one = ProviderParams {
            id: Some(slug.to_owned()),
            ..ProviderParams::default()
        };
        let (listing, _) = self.pages_at(&one)?;
        let mut item = listing
            .items
            .into_iter()
            .next()
            .ok_or_else(|| format!("no page '{slug}'"))?;
        let body = item.body.take().unwrap_or_default();
        item.body = Some(match params.query.as_deref().map(str::trim) {
            None | Some("") => body,
            Some("outline") => page_outline(&body),
            Some(section) => page_section(&body, section)?,
        });
        Ok(ProviderResult {
            provider: "emspace".into(),
            resource_type: "page".into(),
            total_count: Some(1),
            truncated: false,
            items: vec![item],
        })
    }

    /// The space's sessions: the open ones (what other agents declared they
    /// are doing) unless `state` is `closed` or `all`.
    fn sessions(&self, params: &ProviderParams) -> Result<ProviderResult, String> {
        let limit = params.limit.unwrap_or(DEFAULT_LIMIT).clamp(1, MAX_LIMIT);
        let limit_arg = limit.to_string();
        let mut args = vec!["session", "list", "--limit", limit_arg.as_str()];
        match params.state.as_deref().filter(|s| !s.is_empty()) {
            None | Some("open") => args.push("--open"),
            Some("closed") => args.push("--closed"),
            Some("all") => {}
            Some(other) => {
                return Err(format!(
                    "unknown session state '{other}' (open, closed or all)"
                ));
            }
        }
        let answer = self.run_cli(&args, None)?;
        let sessions: Vec<SessionRecord> = serde_json::from_value(answer)
            .map_err(|e| format!("unexpected emspace session listing: {e}"))?;
        let items: Vec<ProviderItem> = sessions.into_iter().map(session_item).collect();
        Ok(ProviderResult {
            provider: "emspace".into(),
            resource_type: "sessions".into(),
            total_count: Some(items.len()),
            truncated: items.len() >= limit,
            items,
        })
    }
}

impl ContextProvider for EmspaceProvider {
    fn id(&self) -> &'static str {
        "emspace"
    }

    fn display_name(&self) -> &'static str {
        "Emspace"
    }

    fn supported_actions(&self) -> &[&str] {
        &["pages", "page", "search", "tasks", "sessions"]
    }

    fn execute(&self, action: &str, params: &ProviderParams) -> Result<ProviderResult, String> {
        match action {
            "pages" => self.pages(params),
            "page" => self.page(params),
            "search" => self.search(params),
            "tasks" => self.tasks(params),
            "sessions" => self.sessions(params),
            _ => Err(format!("Unsupported action: {action}")),
        }
    }

    fn cache_ttl_secs(&self) -> u64 {
        60
    }

    /// Only the page listing is a source pass; a page, search hit, task or
    /// session read would index a second copy beside it.
    fn consolidates(&self, action: &str) -> bool {
        action == "pages"
    }

    fn descriptor(&self) -> ConnectorDescriptor {
        descriptor()
    }

    fn requires_auth(&self) -> bool {
        false
    }

    fn is_available(&self) -> bool {
        self.binary.is_some()
    }
}

pub(crate) fn descriptor() -> ConnectorDescriptor {
    use CapabilityLevel as L;
    ConnectorDescriptor {
        principal_binding: "none: the local user's emspace daemon, selected by the project's .emspace link",
        search: L::Supported,
        list: L::Supported,
        get: L::Supported,
        delta: L::Unsupported,
        revalidate_acl: L::Unsupported,
        revalidate_revision: L::Unsupported,
        delete_event: L::Unsupported,
        content_trust: ConnectorDescriptor::UNTRUSTED,
        write_surface: ConnectorDescriptor::NO_WRITES,
        failure_policy: ConnectorDescriptor::FAIL_CLOSED,
        notes: "no principal, so no ACL to re-check: personal-local use only, withheld from governed reuse; \
                reads run as `emspace run --query`, which refuses writes; optional CLI, unavailable without it",
    }
}

#[derive(Debug, Deserialize)]
struct PagesAnswer {
    #[serde(default)]
    lsn: u64,
    #[serde(default)]
    dirs: Vec<DirRecord>,
    #[serde(default)]
    total: usize,
    #[serde(default)]
    pages: Vec<PageRecord>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct DirRecord {
    name: String,
    local_path: Option<String>,
    remote: Option<String>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct PageRecord {
    slug: String,
    title: String,
    #[serde(rename = "type")]
    page_type: Option<String>,
    status: Option<String>,
    #[serde(default)]
    stale: bool,
    #[serde(default)]
    noted: bool,
    description: Option<String>,
    created_at: Option<String>,
    updated_at: Option<String>,
    #[serde(default)]
    body: String,
    #[serde(default)]
    derived: Vec<String>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct SearchHit {
    slug: String,
    title: String,
    anchor: Option<String>,
    status: Option<String>,
    #[serde(default)]
    stale: bool,
    snippet: Option<String>,
    description: Option<String>,
    updated_at: Option<String>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct TaskRecord {
    id: String,
    title: String,
    status: Option<String>,
    priority: Option<String>,
    board_slug: Option<String>,
    author: Option<String>,
    body: Option<String>,
    created_at: Option<String>,
    updated_at: Option<String>,
    #[serde(default)]
    page_slugs: Vec<String>,
}

/// Page state as one word: freshness first, since a stale page is knowledge
/// to repair rather than to trust.
fn page_state(stale: bool, noted: bool, status: Option<&str>) -> String {
    if stale {
        "stale".into()
    } else if noted {
        "noted".into()
    } else {
        status.unwrap_or("draft").into()
    }
}

fn page_item(page: PageRecord, repo_dirs: &[&str], project_root: &Path) -> ProviderItem {
    let mut body = String::new();
    if let Some(description) = page.description.as_deref().filter(|d| !d.is_empty()) {
        body.push_str(description);
        body.push_str("\n\n");
    }
    body.push_str(&page.body);
    let state = page_state(page.stale, page.noted, page.status.as_deref());
    let mut labels: Vec<String> = page.page_type.into_iter().collect();
    labels.push(state.clone());
    ProviderItem {
        url: Some(format!("emspace:/{}", page.slug)),
        references: page
            .derived
            .iter()
            .flat_map(|locator| repository_paths(locator, repo_dirs, project_root))
            .collect(),
        id: page.slug,
        title: page.title,
        state: Some(state),
        created_at: page.created_at,
        updated_at: page.updated_at,
        labels,
        body: Some(body),
        ..ProviderItem::default()
    }
}

fn search_item(hit: SearchHit) -> ProviderItem {
    let id = match hit.anchor.as_deref().filter(|a| !a.is_empty()) {
        Some(anchor) => format!("{}#{anchor}", hit.slug),
        None => hit.slug.clone(),
    };
    let body = hit.snippet.or(hit.description);
    ProviderItem {
        url: Some(format!("emspace:/{}", hit.slug)),
        id,
        title: hit.title,
        state: Some(page_state(hit.stale, false, hit.status.as_deref())),
        updated_at: hit.updated_at,
        body,
        ..ProviderItem::default()
    }
}

fn task_item(task: TaskRecord) -> ProviderItem {
    let mut labels: Vec<String> = task.priority.into_iter().collect();
    labels.extend(task.board_slug);
    labels.extend(task.page_slugs.iter().map(|slug| format!("page:{slug}")));
    ProviderItem {
        url: Some(format!("emspace:tasks/{}", task.id)),
        id: task.id,
        title: task.title,
        state: task.status,
        author: task.author,
        created_at: task.created_at,
        updated_at: task.updated_at,
        labels,
        body: task.body,
        ..ProviderItem::default()
    }
}

/// A page's `##`… sections, one line each: the slug path to ask for and the
/// section's size, so a reader picks one before reading anything.
fn page_outline(body: &str) -> String {
    use std::fmt::Write as _;
    let mut out = String::new();
    for (path, range) in section_paths(body) {
        let lines = body[range].lines().count();
        let _ = writeln!(out, "  {path} ({lines} lines)");
    }
    if out.is_empty() {
        out.push_str("  (no sections)\n");
    }
    out
}

/// The section `wanted` names: its slug path, its last segment or its
/// heading text.
fn page_section(body: &str, wanted: &str) -> Result<String, String> {
    let sections = section_paths(body);
    let slug = crate::core::knowledge::anchors::heading_slug(wanted);
    let wanted = wanted.trim_start_matches('#');
    sections
        .iter()
        .find(|(path, _)| {
            path == wanted || path == &slug || path.rsplit('/').next() == Some(slug.as_str())
        })
        .map(|(_, range)| body[range.clone()].to_owned())
        .ok_or_else(|| {
            let known: Vec<&str> = sections.iter().map(|(path, _)| path.as_str()).collect();
            format!("no section '{wanted}'; sections: {}", known.join(", "))
        })
}

/// [`crate::core::knowledge::anchors::markdown_sections`] without the page
/// title: an emspace body opens with one `# ` heading that every section path
/// would otherwise repeat (`core-shell-output-patterns/purpose`).
fn section_paths(body: &str) -> Vec<(String, std::ops::Range<usize>)> {
    let sections = crate::core::knowledge::anchors::markdown_sections(body);
    let title = sections
        .first()
        .filter(|(path, _)| !path.contains('/'))
        .map(|(path, _)| format!("{path}/"));
    sections
        .into_iter()
        .filter_map(|(path, range)| match &title {
            Some(title) => path
                .strip_prefix(title.as_str())
                .map(|rest| (rest.to_owned(), range)),
            None => Some((path, range)),
        })
        .collect()
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct SessionRecord {
    id: String,
    title: Option<String>,
    plan: Option<String>,
    summary: Option<String>,
    author: Option<String>,
    created_at: Option<String>,
    closed_at: Option<String>,
}

fn session_item(session: SessionRecord) -> ProviderItem {
    let state = if session.closed_at.is_some() {
        "closed"
    } else {
        "open"
    };
    let body = match (session.plan, session.summary) {
        (Some(plan), Some(summary)) => Some(format!("{plan}\n\n{summary}")),
        (plan, summary) => plan.or(summary),
    };
    ProviderItem {
        url: Some(format!("emspace:sessions/{}", session.id)),
        id: session.id,
        title: session.title.unwrap_or_else(|| "(untitled)".into()),
        state: Some(state.into()),
        author: session.author,
        created_at: session.created_at,
        updated_at: session.closed_at,
        body,
        ..ProviderItem::default()
    }
}

// ---------------------------------------------------------------------------
// Use without a provider call: link detection, background indexing and the
// open sessions shown on the agent bus. All state is per project root and
// in-process; a root without a link or CLI costs one check per interval.
// ---------------------------------------------------------------------------

/// URI prefix of indexed pages (chunks and cross-source edges).
pub(crate) const PAGES_URI_PREFIX: &str = "emspace://pages/";
/// The last completed pass, beside the project's graph (`lsn unix_secs`).
const MARKER_FILE: &str = "emspace-index.state";
/// How often the link and the CLI are looked for again.
const LINK_RECHECK_INTERVAL: Duration = Duration::from_mins(10);
/// How often a process asks the space whether it changed (one journal read,
/// about 40 ms, in the background).
const CHANGE_CHECK_INTERVAL: Duration = Duration::from_mins(1);
/// The longest an index pass is skipped while the journal stands still: it
/// covers what the journal does not record, such as a new checkout file that
/// a glob pin now selects, or stores rebuilt without the index marker.
const FULL_PASS_INTERVAL: Duration = Duration::from_hours(1);
/// How long the open sessions shown on the bus are reused.
const SESSIONS_TTL: Duration = Duration::from_secs(60);
/// Characters of a session plan shown on the bus.
const SESSION_PLAN_CHARS: usize = 240;

#[derive(Default)]
struct RootState {
    checked_at: Option<std::time::Instant>,
    available: bool,
    indexed_at: Option<std::time::Instant>,
    sessions: Option<(std::time::Instant, Option<String>)>,
}

fn with_root_state<T>(project_root: &str, f: impl FnOnce(&mut RootState) -> T) -> T {
    use std::collections::HashMap;
    use std::sync::{Mutex, OnceLock};
    static STATES: OnceLock<Mutex<HashMap<String, RootState>>> = OnceLock::new();
    let mut states = STATES
        .get_or_init(|| Mutex::new(HashMap::new()))
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner);
    let state = states.entry(project_root.to_owned()).or_default();
    if state
        .checked_at
        .is_none_or(|at| at.elapsed() >= LINK_RECHECK_INTERVAL)
    {
        state.available = !project_root.is_empty()
            && find_link(Path::new(project_root)).is_some()
            && find_binary().is_some();
        state.checked_at = Some(std::time::Instant::now());
    }
    f(state)
}

/// Whether `project_root` is linked to a space this machine can read.
pub fn is_linked(project_root: &str) -> bool {
    with_root_state(project_root, |state| state.available)
}

/// Keep the linked space's index current in the background, so `ctx_read`
/// hints need no provider call. At most once per `CHANGE_CHECK_INTERVAL`
/// a detached pass asks the journal for its newest LSN and re-reads the
/// pages only when it moved since the last pass of any process (or after
/// `FULL_PASS_INTERVAL`). A re-index replaces the previous pass (removed
/// pages lose their chunks and edges); a failed read leaves it in place.
/// Returns immediately; respects `providers.enabled` and `providers.auto_index`.
pub fn refresh_index_if_due(project_root: &str) {
    let due = with_root_state(project_root, |state| {
        let due = state.available
            && state
                .indexed_at
                .is_none_or(|at| at.elapsed() >= CHANGE_CHECK_INTERVAL);
        if due {
            // Claimed before the thread starts, so concurrent reads spawn one pass.
            state.indexed_at = Some(std::time::Instant::now());
        }
        due
    });
    if !due {
        return;
    }
    let cfg = crate::core::config::Config::load();
    if !cfg.providers.enabled || !cfg.providers.auto_index {
        return;
    }
    let root = project_root.to_owned();
    crate::core::policy::runtime::spawn_for_project(root.clone(), move || {
        index_pages(&root);
    });
}

fn index_pages(project_root: &str) {
    // At most one pass per project at a time, across every lean-ctx process
    // that serves it (several MCP servers share one project's stores). A
    // pass that finds the lock taken leaves the work to the holder.
    let Some(_lock) = index_lock(project_root) else {
        tracing::debug!("[emspace] another process is indexing {project_root}");
        return;
    };
    let dir = crate::core::property_graph::graph_dir(project_root);
    let provider = EmspaceProvider::new(Path::new(project_root));
    let head = match provider.head_lsn() {
        Ok(lsn) => lsn,
        Err(error) => {
            tracing::debug!("[emspace] background index skipped: {error}");
            return;
        }
    };
    let now = unix_secs();
    if !pass_needed(read_marker(&dir), head, now) {
        return;
    }
    let (result, lsn) = match provider.pages_at(&ProviderParams::default()) {
        Ok(read) => read,
        Err(error) => {
            tracing::debug!("[emspace] background index skipped: {error}");
            return;
        }
    };
    let mut chunks = super::registry::result_to_chunks(&result);
    if !crate::core::workspace_trust::is_trusted(Path::new(project_root)) {
        // The strict screen `ctx_provider` applies in an untrusted workspace.
        chunks
            .retain(|chunk| crate::core::immune_detector::screen_strict(&chunk.content).is_none());
    }
    let mut artifacts = crate::core::consolidation::consolidate(&chunks);
    mark_page_freshness(&mut artifacts.edges, &result.items);
    let prune = crate::core::consolidation::PrunePrior {
        bm25_prefix: Some(PAGES_URI_PREFIX.to_owned()),
        edge_uri_prefix: Some(PAGES_URI_PREFIX.to_owned()),
        ..crate::core::consolidation::PrunePrior::default()
    };
    crate::core::consolidation::apply_artifacts_to_stores(&artifacts, project_root, &prune);
    write_marker(&dir, lsn, now);
    tracing::debug!(
        "[emspace] indexed {} pages ({} edges) at lsn {lsn}",
        result.items.len(),
        artifacts.edges.len()
    );
}

/// Carry a page's freshness into the relation its hint shows: a page whose
/// pinned source moved is `documents, stale` (knowledge to repair, not to
/// trust), one whose drift a note accounts for is `documents, noted`. The
/// agent reads that next to the page name and needs no `emspace status`.
fn mark_page_freshness(edges: &mut [crate::core::graph_index::IndexEdge], pages: &[ProviderItem]) {
    let label = |uri: &str| -> Option<&'static str> {
        let slug = uri.strip_prefix(PAGES_URI_PREFIX)?;
        match pages.iter().find(|p| p.id == slug)?.state.as_deref()? {
            "stale" => Some("stale"),
            "noted" => Some("noted"),
            _ => None,
        }
    };
    for edge in edges.iter_mut() {
        let page = if edge.from.starts_with(PAGES_URI_PREFIX) {
            &edge.from
        } else {
            &edge.to
        };
        if let Some(state) = label(page) {
            edge.kind = format!("{}, {state}", edge.kind);
        }
    }
}

/// The `(lsn, unix seconds)` of the last completed pass of any process,
/// kept beside the project's graph so it goes when the graph goes.
fn read_marker(dir: &Path) -> Option<(u64, u64)> {
    let text = std::fs::read_to_string(dir.join(MARKER_FILE)).ok()?;
    let (lsn, at) = text.trim().split_once(' ')?;
    Some((lsn.parse().ok()?, at.parse().ok()?))
}

fn write_marker(dir: &Path, lsn: u64, at: u64) {
    let _ = crate::core::atomic_fs::try_atomic_write(
        &dir.join(MARKER_FILE),
        format!("{lsn} {at}\n").as_bytes(),
        None,
    );
}

/// Whether a pass has work: never indexed, the journal moved past the last
/// pass, or the last pass is older than [`FULL_PASS_INTERVAL`].
fn pass_needed(marker: Option<(u64, u64)>, head: u64, now: u64) -> bool {
    marker.is_none_or(|(lsn, at)| {
        head != lsn || now.saturating_sub(at) >= FULL_PASS_INTERVAL.as_secs()
    })
}

fn unix_secs() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_or(0, |d| d.as_secs())
}

/// The exclusive index lock of `project_root`, held while the returned file
/// is open; `None` when another process holds it or it cannot be created.
fn index_lock(project_root: &str) -> Option<std::fs::File> {
    index_lock_in(&crate::core::property_graph::graph_dir(project_root))
}

fn index_lock_in(dir: &Path) -> Option<std::fs::File> {
    use fs2::FileExt;
    std::fs::create_dir_all(dir).ok()?;
    let file = std::fs::OpenOptions::new()
        .create(true)
        .truncate(false)
        .write(true)
        .open(dir.join("emspace-index.lock"))
        .ok()?;
    file.try_lock_exclusive().ok()?;
    Some(file)
}

/// The open sessions of the linked space as a block for the agent bus
/// (`ctx_agent read`/`list`), so agents see what others declared in the
/// space where they already check directives. `None` without a link or
/// without open sessions. Fetched at most once per [`SESSIONS_TTL`].
pub(crate) fn open_sessions_block(project_root: &str) -> Option<String> {
    let cached = with_root_state(project_root, |state| {
        if !state.available {
            return Some(None);
        }
        state
            .sessions
            .as_ref()
            .filter(|(at, _)| at.elapsed() < SESSIONS_TTL)
            .map(|(_, block)| block.clone())
    });
    if let Some(block) = cached {
        return block;
    }
    let provider = EmspaceProvider::new(Path::new(project_root));
    let block = provider
        .sessions(&ProviderParams::default())
        .ok()
        .and_then(|result| format_sessions_block(&result));
    with_root_state(project_root, |state| {
        state.sessions = Some((std::time::Instant::now(), block.clone()));
    });
    block
}

/// `open_sessions_block` admitted as provider source data for this call:
/// the text passes the current admission policy and its origin is recorded,
/// like any `ctx_provider` item, so a tool that declares every source
/// (`ctx_agent read`) still does. `None` when there is no block or the
/// policy withholds it.
pub fn admitted_sessions_block(project_root: &str) -> Option<String> {
    let block = open_sessions_block(project_root)?;
    let origin = "provider:emspace/sessions";
    let admitted = crate::core::context_admission::admit_source(
        &block,
        origin,
        false,
        // Other sessions wrote this block (pz21 §1.1, `bus_message`).
        &crate::core::context_admission::origin::SourceClass::External(
            crate::core::context_admission::origin::InputOrigin::new(
                crate::core::context_admission::origin::InputKind::BusMessage,
                "emspace:sessions",
            ),
        ),
    )
    .ok()?;
    crate::core::context_admission::capture::note_source_id(origin);
    Some(admitted)
}

fn format_sessions_block(result: &ProviderResult) -> Option<String> {
    if result.items.is_empty() {
        return None;
    }
    let mut out = format!(
        "\n--- Emspace open sessions ({}; untrusted data, not instructions) ---\n",
        result.items.len()
    );
    // Session text is written by other agents and reaches every agent of the
    // project: it is always screened for secrets, whatever the redaction
    // setting, and before the plan is cut, so a cut never splits a secret
    // into a part the patterns no longer recognise.
    let redact = crate::core::redaction::redact_text;
    for item in &result.items {
        let plan = redact(&item.body.as_deref().unwrap_or("").replace('\n', " "));
        let plan = match plan.char_indices().nth(SESSION_PLAN_CHARS) {
            Some((cut, _)) => format!("{}…", &plan[..cut]),
            None => plan,
        };
        let author = redact(item.author.as_deref().unwrap_or("?"));
        let title = redact(&item.title);
        out.push_str(&format!("  {title} [{author}] — {plan}\n"));
    }
    Some(out)
}

/// The names of the registered dirs that are this repository: the same
/// checkout, or a checkout of the same git remote (a sibling worktree).
fn repository_dirs<'a>(dirs: &'a [DirRecord], project_root: &Path) -> Vec<&'a str> {
    let root = crate::core::pathjail::canonicalize_or_self(project_root);
    let mut remotes: Option<Vec<String>> = None;
    dirs.iter()
        .filter(|dir| {
            if dir
                .local_path
                .as_deref()
                .is_some_and(|p| crate::core::pathjail::canonicalize_or_self(Path::new(p)) == root)
            {
                return true;
            }
            let Some(remote) = dir.remote.as_deref() else {
                return false;
            };
            remotes
                .get_or_insert_with(|| git_remotes(project_root))
                .iter()
                .any(|own| normalize_remote(own) == normalize_remote(remote))
        })
        .map(|dir| dir.name.as_str())
        .collect()
}

fn git_remotes(project_root: &Path) -> Vec<String> {
    let mut command = std::process::Command::new("git");
    command
        .arg("-C")
        .arg(project_root)
        .args(["config", "--get-regexp", r"^remote\..*\.url$"])
        .stdin(std::process::Stdio::null());
    let Ok(captured) = crate::core::process_capture::run_with_output_limits(
        &mut command,
        Some(Duration::from_secs(5)),
        65_536,
        4096,
    ) else {
        return Vec::new();
    };
    if captured.timed_out || !captured.output.status.success() {
        return Vec::new();
    }
    String::from_utf8_lossy(&captured.output.stdout)
        .lines()
        .filter_map(|line| line.split_once(' ').map(|(_, url)| url.trim().to_owned()))
        .collect()
}

fn normalize_remote(url: &str) -> &str {
    let url = url.trim().trim_end_matches('/');
    url.strip_suffix(".git").unwrap_or(url)
}

/// The repository-relative paths a `dir:<name>/<path>` locator pins, when
/// `<name>` is one of this repository's dirs. A file or directory pin is its
/// path; a glob is expanded against the checkout to the files it selects
/// here (at most [`MAX_GLOB_FILES`]), since the directory in front of it —
/// `rust/src/core` for `rust/src/core/adaptive*.rs` — would claim every file
/// below. The whole dir (`dir:<name>`) pins nothing useful and is skipped.
fn repository_paths(locator: &str, repo_dirs: &[&str], project_root: &Path) -> Vec<String> {
    let Some((name, path)) = locator
        .strip_prefix("dir:")
        .and_then(|rest| rest.split_once('/'))
    else {
        return Vec::new();
    };
    let path = path.trim_matches('/');
    if !repo_dirs.contains(&name)
        || path.is_empty()
        || path.split('/').any(|part| part == ".." || part == ".")
    {
        return Vec::new();
    }
    if !path.contains(['*', '?', '[', '{']) {
        return vec![path.to_owned()];
    }
    let pattern = project_root.join(path);
    let Ok(entries) = glob::glob(&pattern.to_string_lossy()) else {
        return Vec::new();
    };
    let mut paths: Vec<String> = entries
        .filter_map(Result::ok)
        .filter(|file| file.is_file())
        .filter_map(|file| {
            let relative = file.strip_prefix(project_root).ok()?;
            Some(relative.to_string_lossy().replace('\\', "/"))
        })
        .take(MAX_GLOB_FILES)
        .collect();
    paths.sort();
    paths
}

/// The `.emspace` link at or above `start`.
fn find_link(start: &Path) -> Option<PathBuf> {
    start
        .ancestors()
        .map(|dir| dir.join(".emspace"))
        .find(|candidate| candidate.is_file())
}

/// The `emspace` binary on `PATH`, else in `~/.local/bin` (its installer's
/// default, often missing from a GUI-started agent's `PATH`).
fn find_binary() -> Option<PathBuf> {
    let name = if cfg!(windows) {
        "emspace.exe"
    } else {
        "emspace"
    };
    let on_path = std::env::var_os("PATH").and_then(|path| {
        std::env::split_paths(&path)
            .map(|dir| dir.join(name))
            .find(|candidate| candidate.is_file())
    });
    on_path.or_else(|| {
        dirs::home_dir()
            .map(|home| home.join(".local/bin").join(name))
            .filter(|candidate| candidate.is_file())
    })
}

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

    #[test]
    fn locators_of_this_repository_become_paths() {
        let repo = ["repo"];
        let root = tempfile::tempdir().unwrap();
        let core = root.path().join("src/core");
        std::fs::create_dir_all(&core).unwrap();
        for file in ["adaptive.rs", "adaptive_mode.rs", "budget.rs"] {
            std::fs::write(core.join(file), "").unwrap();
        }
        let paths = |locator: &str| repository_paths(locator, &repo, root.path());
        assert_eq!(
            paths("dir:repo/src/core/budget.rs"),
            vec!["src/core/budget.rs"]
        );
        assert_eq!(paths("dir:repo/src/dashboard/"), vec!["src/dashboard"]);
        // A glob names the files it selects in this checkout, never its parent dir.
        assert_eq!(
            paths("dir:repo/src/core/adaptive*.rs"),
            vec!["src/core/adaptive.rs", "src/core/adaptive_mode.rs"]
        );
        assert_eq!(paths("dir:repo/src/**/*.rs").len(), 3);
        // Another repository's dir, a whole dir, other kinds and escapes name nothing.
        for locator in [
            "dir:site/src/main.rs",
            "dir:repo",
            "url:https://example.com/a.md",
            "dir:repo/../etc/passwd",
            "dir:repo/src/../../*.rs",
        ] {
            assert!(paths(locator).is_empty(), "{locator}");
        }
    }

    #[test]
    fn a_dir_belongs_to_the_repository_by_checkout_or_remote() {
        let root = tempfile::tempdir().unwrap();
        let dirs = vec![
            DirRecord {
                name: "here".into(),
                local_path: Some(root.path().display().to_string()),
                remote: None,
            },
            DirRecord {
                name: "elsewhere".into(),
                local_path: Some("/nonexistent/other".into()),
                remote: None,
            },
        ];
        assert_eq!(repository_dirs(&dirs, root.path()), vec!["here"]);
        assert_eq!(
            normalize_remote("git@host:o/r.git/"),
            normalize_remote("git@host:o/r")
        );
    }

    #[test]
    fn a_page_becomes_a_wiki_item_with_its_pinned_paths() {
        let page: PageRecord = serde_json::from_value(serde_json::json!({
            "slug": "core/patterns",
            "title": "Core: Shell Output Patterns",
            "type": "component",
            "status": "draft",
            "stale": true,
            "noted": false,
            "description": "Shell output pattern engine",
            "body": "# Core: Shell Output Patterns\n\nCompresses CLI output.",
            "derived": ["dir:repo/rust/src/core/patterns", "dir:site/x/y.ts"],
        }))
        .unwrap();
        let item = page_item(page, &["repo"], Path::new("/nonexistent"));
        assert_eq!(item.id, "core/patterns");
        assert_eq!(item.state.as_deref(), Some("stale"));
        assert_eq!(item.labels, vec!["component", "stale"]);
        assert_eq!(item.references, vec!["rust/src/core/patterns"]);
        assert!(
            item.body
                .as_deref()
                .unwrap()
                .starts_with("Shell output pattern engine\n\n# Core")
        );

        let result = ProviderResult {
            provider: "emspace".into(),
            resource_type: "pages".into(),
            items: vec![item],
            total_count: Some(1),
            truncated: false,
        };
        let chunks = super::super::registry::result_to_chunks(&result);
        assert_eq!(chunks[0].kind, crate::core::bm25_index::ChunkKind::WikiPage);
        assert!(
            chunks[0]
                .references
                .contains(&"rust/src/core/patterns".to_owned())
        );
    }

    #[test]
    fn open_sessions_render_as_one_bounded_line_each() {
        let open: SessionRecord = serde_json::from_value(serde_json::json!({
            "id": "01a", "title": "Codebase Cleanup Plan", "author": "claude-code/2.1",
            "plan": format!("Analyse the engine\n{}", "x".repeat(400)),
        }))
        .unwrap();
        let item = session_item(open);
        assert_eq!(item.state.as_deref(), Some("open"));
        let result = ProviderResult {
            provider: "emspace".into(),
            resource_type: "sessions".into(),
            items: vec![item],
            total_count: Some(1),
            truncated: false,
        };
        let block = format_sessions_block(&result).unwrap();
        let line = block.lines().nth(2).unwrap();
        assert!(
            line.starts_with("  Codebase Cleanup Plan [claude-code/2.1] — Analyse the engine x")
        );
        assert!(line.ends_with('…'));
        assert!(line.chars().count() < SESSION_PLAN_CHARS + 60);
        assert!(block.contains("untrusted data, not instructions"));

        let none = ProviderResult {
            items: Vec::new(),
            ..result
        };
        assert_eq!(format_sessions_block(&none), None);
    }

    #[test]
    fn a_secret_in_a_session_never_reaches_the_bus_even_where_the_plan_is_cut() {
        let secret = concat!("gh", "p_", "Zq8LmW3vXc9TbN2rKp7YdF4sHj6Ga1Ue");
        let session = |plan: String| ProviderItem {
            title: "Deploy".into(),
            author: Some("agent".into()),
            body: Some(plan),
            ..ProviderItem::default()
        };
        // Once in the middle, once straddling the cut at SESSION_PLAN_CHARS.
        let straddling = format!("{}{secret} rest", "x".repeat(SESSION_PLAN_CHARS - 10));
        let result = ProviderResult {
            provider: "emspace".into(),
            resource_type: "sessions".into(),
            items: vec![
                session(format!("rotate {secret} after the deploy")),
                session(straddling),
            ],
            total_count: Some(2),
            truncated: false,
        };
        let block = format_sessions_block(&result).unwrap();
        let tail = &secret[4..];
        for len in [8, 12] {
            assert!(!block.contains(&tail[..len]), "secret leaked:\n{block}");
        }
        assert!(block.contains("[REDACTED"), "{block}");
    }

    const BODY: &str = "Shell output engine\n\n# Core: Shell Output Patterns\n\nLede.\n\n\
## Purpose\n\nCompresses output.\n\n## How It Works\n\nSteps.\n\n### Dispatch\n\nTable.\n\n\
```md\n## not a heading\n```\n\n## Known Gaps\n\nNone.\n";

    #[test]
    fn a_page_outline_names_each_section_once_without_the_title() {
        let outline = page_outline(BODY);
        let paths: Vec<&str> = outline
            .lines()
            .map(|l| l.trim().split(' ').next().unwrap())
            .collect();
        assert_eq!(
            paths,
            vec![
                "purpose",
                "how-it-works",
                "how-it-works/dispatch",
                "known-gaps"
            ]
        );
        assert_eq!(page_outline("no headings at all"), "  (no sections)\n");
    }

    #[test]
    fn a_section_is_found_by_path_last_segment_or_heading_text() {
        let by_path = page_section(BODY, "how-it-works/dispatch").unwrap();
        assert!(by_path.starts_with("### Dispatch") && by_path.contains("Table."));
        assert_eq!(page_section(BODY, "dispatch").unwrap(), by_path);
        assert_eq!(
            page_section(BODY, "## Known Gaps").unwrap(),
            page_section(BODY, "known-gaps").unwrap()
        );
        // A section runs to the next heading of its level, nested ones included.
        let how = page_section(BODY, "How It Works").unwrap();
        assert!(how.contains("### Dispatch") && !how.contains("## Known Gaps"));
        let missing = page_section(BODY, "nope").unwrap_err();
        assert!(
            missing.contains("sections: purpose, how-it-works"),
            "{missing}"
        );
    }

    #[test]
    fn a_stale_or_noted_page_says_so_in_its_hint_relation() {
        let edge = |from: &str, to: &str, kind: &str| crate::core::graph_index::IndexEdge {
            from: from.into(),
            to: to.into(),
            kind: kind.into(),
            weight: 0.8,
        };
        let page = |id: &str, state: &str| ProviderItem {
            id: id.into(),
            state: Some(state.into()),
            ..ProviderItem::default()
        };
        let mut edges = vec![
            edge("emspace://pages/core/patterns", "src/a.rs", "documents"),
            edge("src/a.rs", "emspace://pages/core/patterns", "mentioned_in"),
            edge("emspace://pages/cockpit", "src/b.rs", "documents"),
            edge("emspace://pages/build", "src/c.rs", "documents"),
        ];
        let pages = [
            page("core/patterns", "stale"),
            page("cockpit", "noted"),
            page("build", "draft"),
        ];
        mark_page_freshness(&mut edges, &pages);
        let kinds: Vec<&str> = edges.iter().map(|e| e.kind.as_str()).collect();
        assert_eq!(
            kinds,
            vec![
                "documents, stale",
                "mentioned_in, stale",
                "documents, noted",
                "documents"
            ]
        );
    }

    #[test]
    fn a_pass_reads_pages_only_when_the_journal_moved_or_an_hour_passed() {
        let hour = FULL_PASS_INTERVAL.as_secs();
        assert!(pass_needed(None, 7, 1_000), "never indexed");
        assert!(
            !pass_needed(Some((7, 1_000)), 7, 1_000 + 60),
            "unchanged space"
        );
        assert!(
            pass_needed(Some((7, 1_000)), 8, 1_000 + 60),
            "the journal moved"
        );
        assert!(
            pass_needed(Some((7, 1_000)), 7, 1_000 + hour),
            "the hourly full pass"
        );
        // A clock that went back never blocks the next change.
        assert!(pass_needed(Some((7, 5_000)), 9, 1_000));
    }

    #[test]
    fn the_index_marker_round_trips_and_a_damaged_one_reads_as_none() {
        let dir = tempfile::tempdir().unwrap();
        assert_eq!(read_marker(dir.path()), None);
        write_marker(dir.path(), 5167, 1_761_900_000);
        assert_eq!(read_marker(dir.path()), Some((5167, 1_761_900_000)));
        std::fs::write(dir.path().join(MARKER_FILE), "garbage").unwrap();
        assert_eq!(read_marker(dir.path()), None);
    }

    #[test]
    fn only_one_index_pass_per_project_holds_the_lock() {
        let dir = tempfile::tempdir().unwrap();
        let first = index_lock_in(dir.path()).expect("first pass takes the lock");
        assert!(
            index_lock_in(dir.path()).is_none(),
            "a second pass must skip"
        );
        drop(first);
        assert!(
            index_lock_in(dir.path()).is_some(),
            "the lock is released with the pass"
        );
    }

    #[test]
    fn without_a_link_nothing_is_linked_indexed_or_listed() {
        let root = tempfile::tempdir().unwrap();
        if find_link(root.path()).is_some() {
            return;
        }
        let root = root.path().to_string_lossy().into_owned();
        assert!(!is_linked(&root));
        refresh_index_if_due(&root);
        assert_eq!(open_sessions_block(&root), None);
        assert!(!is_linked(""));
    }

    #[test]
    fn without_a_link_the_provider_is_unavailable_and_runs_nothing() {
        let root = tempfile::tempdir().unwrap();
        // tempdir has no .emspace at or above it on any CI host.
        if find_link(root.path()).is_some() {
            return;
        }
        let provider = EmspaceProvider::new(root.path());
        assert!(!provider.is_available());
        let err = provider
            .execute("pages", &ProviderParams::default())
            .unwrap_err();
        assert!(err.contains("not available"), "{err}");
    }

    #[test]
    fn invalid_states_fail_before_any_process_runs() {
        let provider = EmspaceProvider {
            project_root: PathBuf::from("/nonexistent"),
            binary: Some(PathBuf::from("/nonexistent/emspace")),
        };
        let params = ProviderParams {
            state: Some("everything".into()),
            ..ProviderParams::default()
        };
        assert!(
            provider
                .pages(&params)
                .unwrap_err()
                .contains("unknown page state")
        );
        assert!(
            provider
                .tasks(&params)
                .unwrap_err()
                .contains("unknown task state")
        );
        let empty = ProviderParams::default();
        assert!(
            provider
                .search(&empty)
                .unwrap_err()
                .contains("needs a query")
        );
    }
}