symforge 11.0.5

SymForge — in-memory code intelligence MCP server for Claude Code, Codex, and Gemini CLI
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
//! Feature 020 V11, T048 — the dark wrap table and the export delta.
//!
//! This module is the EMBED BOUNDARY's dark rehearsal: for every top-level
//! introduced atom it records HOW the internal type reaches the contract shape
//! — a wrapper, a keyword flip, never a 1:1 re-export of an internal path
//! (D12/D13). The wrappers that runtime-checkable oracles can pin are built
//! here; the rest carry their obligation in [`wrap_table`] so T049's harness
//! and the Slice 4 activation cut inherit a work list, not a guess.
//!
//! **Identity rendering (E3 ruling).** Contract accessors return `&str`; the
//! internal identities are opaque newtypes. Wrappers render KIND-PREFIXED
//! strings — `op-42`, `auth-17`, `hash-9f…` — STORED at construction, so the
//! `&str` borrows from the wrapper and is stable across calls. The renderer
//! owns the scheme; nothing drives it off `Display` of the newtype.
//!
//! **The sentinel (E2 ruling).** A refusal that examined no authority renders
//! [`EVIDENCE_ABSENT`] — a token the identity renderer can never emit, so
//! absence is never confusable with a real identity and nothing is minted.
//!
//! **Dark behavior is honest refusal.** No generation is bound to anything in
//! Slice 3, so the search and refresh wrappers REFUSE rather than fabricate
//! empty results — an empty result would be a claim about content that does
//! not exist.

use std::collections::BTreeSet;
use std::fmt;

use crate::lifecycle_identity::{OperationReceipt, SourceRefusal};
// T049: the contract's three enum atoms ARE the lifecycle_identity enums —
// minted contract-verbatim, so the wrap is nameability, not reshaping. This
// module is the boundary that makes them nameable; `lifecycle_identity` stays
// `pub(crate)` and invisible to the census.
pub use crate::lifecycle_identity::{OperationKind, RetryAdvice, SourceRefusalKind};

use super::embedded::ReceiptWaitError;
use super::process_runtime::ProcessIndexRuntime;

/// The closed sentinel for a refusal that examined no authority. Kind-prefixed
/// identities always render as `<kind>-<digits>`, so this token is outside the
/// renderer's image by construction.
pub const EVIDENCE_ABSENT: &str = "evidence-absent";

/// Auto-trait opt-out carried by every V11 handle type (T049). The frozen
/// contract pins the five handles `Send + Sync + Unpin` but NOT
/// `UnwindSafe`/`RefUnwindSafe` — the activated runtime's internals will not
/// be — so the dark stand-ins must already refuse those two impls, or the
/// activation cut would change the public trait surface.
pub(crate) type NotUnwindSafe = std::marker::PhantomData<Box<dyn std::any::Any + Send + Sync>>;

// ── Identity rendering ─────────────────────────────────────────────────────

fn render_operation_identity(receipt: &OperationReceipt) -> String {
    format!("op-{}", receipt.identity().raw_for_render())
}

fn render_argument_hash(receipt: &OperationReceipt) -> String {
    format!("hash-{:012x}", receipt.canonical_argument_hash().raw())
}

// ── The refusal wrapper ────────────────────────────────────────────────────

/// The contract-shaped operation receipt: `&str` identity accessors over
/// strings rendered once, at wrap time.
#[derive(Debug, Clone)]
pub struct EmbedOperationReceipt {
    identity: String,
    canonical_argument_hash: String,
    operation_kind: OperationKind,
    schema_version: u32,
}

impl EmbedOperationReceipt {
    fn wrap(receipt: &OperationReceipt) -> Self {
        Self {
            identity: render_operation_identity(receipt),
            canonical_argument_hash: render_argument_hash(receipt),
            operation_kind: receipt.operation_kind(),
            schema_version: receipt.schema_version(),
        }
    }

    pub fn identity(&self) -> &str {
        &self.identity
    }

    pub fn canonical_argument_hash(&self) -> &str {
        &self.canonical_argument_hash
    }

    pub fn operation_kind(&self) -> OperationKind {
        self.operation_kind
    }

    pub fn schema_version(&self) -> u32 {
        self.schema_version
    }
}

/// The contract-shaped refusal: opaque, `&str` evidence with the closed
/// sentinel for none, `Display` + `Error` implemented as the contract's
/// trait_impls demand.
#[derive(Debug)]
pub struct EmbedSourceRefusal {
    kind: SourceRefusalKind,
    operation: EmbedOperationReceipt,
    retry: RetryAdvice,
    evidence_identity: String,
}

impl EmbedSourceRefusal {
    pub(crate) fn wrap(refusal: &SourceRefusal) -> Self {
        Self {
            kind: refusal.kind(),
            operation: EmbedOperationReceipt::wrap(&refusal.operation()),
            retry: refusal.retry(),
            evidence_identity: match refusal.evidence_identity() {
                Some(identity) => format!("auth-{}", identity.raw_for_render()),
                None => EVIDENCE_ABSENT.to_string(),
            },
        }
    }

    pub fn kind(&self) -> SourceRefusalKind {
        self.kind
    }

    /// Stable display name of the kind, for oracles and diagnostics.
    pub fn kind_name(&self) -> &'static str {
        match self.kind {
            SourceRefusalKind::AdmissionUnavailable => "AdmissionUnavailable",
            SourceRefusalKind::InvalidSelection => "InvalidSelection",
            SourceRefusalKind::SelectionUnavailable => "SelectionUnavailable",
            SourceRefusalKind::SourceUnavailable => "SourceUnavailable",
        }
    }

    pub fn operation(&self) -> &EmbedOperationReceipt {
        &self.operation
    }

    pub fn retry(&self) -> RetryAdvice {
        self.retry
    }

    pub fn evidence_identity(&self) -> &str {
        &self.evidence_identity
    }
}

impl fmt::Display for EmbedSourceRefusal {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "source refusal: {} (operation {}, evidence {})",
            self.kind_name(),
            self.operation.identity(),
            self.evidence_identity
        )
    }
}

impl std::error::Error for EmbedSourceRefusal {}

// ── The search and view shapes, VERBATIM from the contract ─────────────────

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SymbolSearchRequest {
    pub query: Option<String>,
    pub path_prefix: Option<String>,
    pub limit: u32,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SymbolMatch {
    pub name: String,
    pub kind: String,
    pub path: String,
    pub start_line: u32,
    pub end_line: u32,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SymbolSearchResult {
    pub matches: Vec<SymbolMatch>,
    pub truncated: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TextSearchRequest {
    pub query: String,
    pub path_prefix: Option<String>,
    pub limit: u32,
    pub case_sensitive: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TextMatch {
    pub path: String,
    pub line: u32,
    pub byte_start: u64,
    pub byte_end: u64,
    pub preview: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TextSearchResult {
    pub matches: Vec<TextMatch>,
    pub truncated: bool,
}

/// The contract-shaped refresh ticket. Dark handles can never mint one — the
/// type exists so the refusal-returning signature is contract-true and the
/// Slice 4 wiring changes evidence, not shape.
#[derive(Debug)]
pub struct EmbedRefreshTicket {
    ticket_identity: String,
    operation: EmbedOperationReceipt,
    requested_source_version: u64,
    _not_unwind_safe: NotUnwindSafe,
}

impl EmbedRefreshTicket {
    pub fn ticket_identity(&self) -> &str {
        &self.ticket_identity
    }

    pub fn operation(&self) -> &EmbedOperationReceipt {
        &self.operation
    }

    pub fn requested_source_version(&self) -> u64 {
        self.requested_source_version
    }
}

// ── The claim family, contract-shaped (T049 wrap list) ─────────────────────
//
// Dark handles refuse every operation, so nothing in Slice 3 can mint a
// claim, an authority, or an evaluation — these types exist so the
// `Result<Claim<..>, ..>` signatures are contract-true and the Slice 4
// wiring changes evidence, not shape. No constructor is public; none is
// needed until something can honestly observe what these types report.

/// The contract-shaped atomic authority: `&str` identity over a string
/// rendered at wrap time, plus the stable kind name.
#[derive(Debug)]
pub struct EmbedAtomicAuthority {
    identity: String,
    kind_name: &'static str,
}

impl EmbedAtomicAuthority {
    pub fn identity(&self) -> &str {
        &self.identity
    }

    pub fn kind_name(&self) -> &'static str {
        self.kind_name
    }
}

/// The contract-shaped claim provenance: the closed authority set that was
/// actually examined. The count is measured from the set, never stored
/// separately — two fields that can disagree are one field too many.
#[derive(Debug)]
pub struct EmbedClaimProvenance {
    authorities: Vec<EmbedAtomicAuthority>,
    identity: String,
    kind_name: &'static str,
}

impl EmbedClaimProvenance {
    pub fn authorities(&self) -> &[EmbedAtomicAuthority] {
        &self.authorities
    }

    pub fn authority_count(&self) -> usize {
        self.authorities.len()
    }

    pub fn identity(&self) -> &str {
        &self.identity
    }

    pub fn kind_name(&self) -> &'static str {
        self.kind_name
    }
}

/// The contract-shaped evaluation provenance.
#[derive(Debug)]
pub struct EmbedEvaluationProvenance {
    identity: String,
}

impl EmbedEvaluationProvenance {
    pub fn identity(&self) -> &str {
        &self.identity
    }
}

/// The contract-shaped claim: a value carrying HOW it was produced.
#[derive(Debug)]
pub struct EmbedClaim<T> {
    value: T,
    provenance: EmbedClaimProvenance,
    operation: EmbedOperationReceipt,
    evaluation: Option<EmbedEvaluationProvenance>,
    producing_runtime_identity: String,
}

impl<T> EmbedClaim<T> {
    pub fn value(&self) -> &T {
        &self.value
    }

    pub fn provenance(&self) -> &EmbedClaimProvenance {
        &self.provenance
    }

    pub fn operation(&self) -> &EmbedOperationReceipt {
        &self.operation
    }

    pub fn evaluation(&self) -> Option<&EmbedEvaluationProvenance> {
        self.evaluation.as_ref()
    }

    pub fn producing_runtime_identity(&self) -> &str {
        &self.producing_runtime_identity
    }
}

// ── The source spec and the report shapes (T049 wrap list) ─────────────────

/// The contract-shaped source spec: how an embedder names a source to open.
#[derive(Debug)]
pub struct EmbeddedSourceSpec {
    root: std::path::PathBuf,
}

impl EmbeddedSourceSpec {
    /// The contract constructor: the current worktree rooted at `root`.
    pub fn current_worktree(root: std::path::PathBuf) -> Self {
        Self { root }
    }
}

/// The contract-shaped shutdown report: observed counts only.
#[derive(Debug)]
pub struct ShutdownReport {
    pub closed_sources: u64,
    pub joined_workers: u64,
}

/// The contract-shaped source-close report. Distinct from the internal
/// `embedded::SourceCloseReport` (T047's observation record): this is the
/// PUBLIC field-for-field contract record the boundary's `wait` returns.
#[derive(Debug)]
pub struct SourceCloseReport {
    pub already_terminal: bool,
    pub terminal_source_version: u64,
}

/// The contract-shaped shutdown receipt. The DARK runtime spawns nothing and
/// closes no holder's source, so the wait completes immediately and reports
/// the counts it observed — zeros, honestly, not a claim of teardown work
/// nothing performed. Slice 4 wires the real lifecycle behind this shape.
#[derive(Debug)]
pub struct EmbedShutdownReceipt {
    _not_unwind_safe: NotUnwindSafe,
}

impl EmbedShutdownReceipt {
    /// The contract wait. Nothing is spawned in the dark modules, so the
    /// deadline can never be reached; the parameter keeps its contract name
    /// and the dark lane records that it does not read it.
    pub fn wait(&self, deadline: std::time::Instant) -> Result<ShutdownReport, ReceiptWaitError> {
        let _ = deadline;
        Ok(ShutdownReport {
            closed_sources: 0,
            joined_workers: 0,
        })
    }
}

/// The public runtime phase, contract-verbatim, OWNED BY THE BOUNDARY (C7
/// ruling): `runtime::SourceRuntimePhase` is an internal path, and a public
/// field typed by it would be a D12 path-identity leak through the embed
/// surface. Same six variants; the mapping is total and explicit.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SourceRuntimePhase {
    Blocked,
    Current,
    Loading,
    Refreshing,
    Stopped,
    Stopping,
}

/// The public view of one source's runtime state, contract field-for-field.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SourceRuntimeView {
    pub binding_identity: String,
    pub current_publication_identity: Option<String>,
    pub observer_epoch: u64,
    pub phase: SourceRuntimePhase,
    pub source_version: u64,
}

/// The honest dark refusal every unbound operation returns: no generation is
/// bound, nothing was examined.
pub(crate) fn dark_unbound_refusal(kind: OperationKind) -> EmbedSourceRefusal {
    EmbedSourceRefusal::wrap(&SourceRefusal::for_runtime(
        SourceRefusalKind::SourceUnavailable,
        OperationReceipt::for_dark_refusal(kind),
        RetryAdvice::OnEvent,
        None,
    ))
}

// ── The runtime wrapper ────────────────────────────────────────────────────

/// The contract-shaped process runtime: zero-argument `acquire` delegating to
/// the explicit-budget `incarnate` with the named provisional constant.
///
/// `Clone` and `Drop` are CONTRACT-pinned direct impls, not conveniences: the
/// frozen trait_impls record both, so the dark stand-in carries them or the
/// activation cut would change the public trait surface.
#[derive(Debug, Clone)]
pub struct ProcessRuntimeApi {
    _inner: std::sync::Arc<ProcessIndexRuntime>,
    factory: std::sync::Arc<super::embedded::EmbeddedSourceFactory>,
    _not_unwind_safe: NotUnwindSafe,
}

impl Drop for ProcessRuntimeApi {
    fn drop(&mut self) {
        // The contract pins a literal `Drop` on this atom. The dark runtime
        // owns no workers, so there is nothing to join yet; Slice 4 gives
        // this body its real teardown.
    }
}

impl ProcessRuntimeApi {
    /// The atom's shape: no receiver, no arguments. Since C5 the embed
    /// surface ATTACHES: acquisition runs the process activation ceremony
    /// (idempotent) and joins the ONE process capacity runtime every other
    /// surface attaches to — the C4b daemon/stdio/serve pattern — instead
    /// of minting a private incarnation with a provisional budget. The
    /// `Result` shape is the contract's; the current admission cannot
    /// refuse, and the refusing evidence arrives with the measured budgets
    /// (C7/C8).
    pub fn acquire() -> Result<Self, EmbedSourceRefusal> {
        super::activation::activate_surface(super::process_runtime::SurfaceKind::Embed);
        Ok(Self {
            _inner: super::activation::process_index_runtime(),
            factory: super::embedded::EmbeddedSourceFactory::new(),
            _not_unwind_safe: std::marker::PhantomData,
        })
    }

    /// Open the sole handle for the source `spec` names (T049). Dark behavior
    /// is the registration-level truth: sole-handle admission works, and a
    /// second open of a source already held refuses — the selected source is
    /// unavailable until its holder closes, hence `SelectionUnavailable` with
    /// `OnEvent` retry. No authority is examined at registration level, so
    /// the evidence renders the closed sentinel.
    pub fn open_embedded_source(
        &self,
        spec: EmbeddedSourceSpec,
    ) -> Result<super::embedded::EmbeddedSourceHandle, EmbedSourceRefusal> {
        let key = super::registry::ProjectKey::new(spec.root.to_string_lossy());
        self.factory.open(key).map_err(|refusal| {
            // D18, ratified and NARROWED: open() refuses only SourceAlreadyOpen
            // (M14 pinned that), so the two arms that mapped refusals open()
            // cannot produce are deleted rather than given dead kind mappings.
            // The held_by identity is an EmbeddedIdentity, not an
            // AuthorityIdentity — surfacing it as refusal evidence would MINT,
            // so the sentinel stands.
            let super::embedded::EmbedRefusal::SourceAlreadyOpen { .. } = refusal else {
                unreachable!("EmbeddedSourceFactory::open refuses only SourceAlreadyOpen")
            };
            EmbedSourceRefusal::wrap(&SourceRefusal::for_runtime(
                SourceRefusalKind::SelectionUnavailable,
                OperationReceipt::for_dark_refusal(OperationKind::OpenEmbeddedSource),
                RetryAdvice::OnEvent,
                None,
            ))
        })
    }

    /// Begin process shutdown (T049). The dark runtime closes no holder's
    /// source and joins no workers — the receipt's wait reports the observed
    /// zeros rather than teardown work nothing performed.
    pub fn begin_shutdown(&self) -> EmbedShutdownReceipt {
        EmbedShutdownReceipt {
            _not_unwind_safe: std::marker::PhantomData,
        }
    }

    /// Fixture probe: the wrapper's honest dark refusal, for shape oracles.
    #[cfg(all(test, feature = "server"))]
    pub fn refusal_probe_for_test(&self) -> Result<(), EmbedSourceRefusal> {
        Err(dark_unbound_refusal(OperationKind::SearchSymbols))
    }
}

// ── The wrap table and the export delta ────────────────────────────────────

/// One top-level introduced atom and how its contract shape is satisfied.
/// Obligations are the module's own judgment — NEVER path identity:
///
/// * `"wrapped-here"` — a contract-shaped wrapper exists in this module or on
///   the SEAM-pinned handle, exercised by the shape oracle.
/// * `"verbatim-reexport"` — the C7 ruling's third word: the internal type
///   was MINTED contract-verbatim (the `lifecycle_identity` enums) and this
///   module makes it nameable by an actual `pub use` — which the delta
///   oracle verifies against the source, never trusting this self-report.
///   Distinct from the banned `"direct-reexport"`: nothing pre-existing
///   leaks; the type exists only because the contract named it.
/// * `"wrap-planned-t049"` — RETIRED vocabulary: T048 recorded the nine
///   shape-diverging types (the D13 list) under it so they could not be
///   forgotten; T049 discharged all nine into `"wrapped-here"` wrappers.
/// * `"keyword-flip"` — `server_api`: the `pub(crate)` module whose
///   activation was one keyword — executed at C5; the module is public and
///   wired to the crate dispatcher.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WrapEntry {
    pub atom: &'static str,
    pub obligation: &'static str,
}

/// The V11 public-API seam (the frozen postactivation anchor
/// `src/index_lifecycle/public_api.rs::V11PublicApi`): the boundary's own
/// record of how each introduced atom is satisfied. `embed.rs` re-exports
/// the wrappers; the export-delta oracle reads the table through this
/// seam, so the anchor is load-bearing, not a marker.
pub struct V11PublicApi;

impl V11PublicApi {
    /// The closed wrap table over exactly the top-level introduced atoms.
    pub fn wrap_table() -> &'static [WrapEntry] {
        wrap_table()
    }
}

/// The closed table over exactly the top-level introduced atoms.
pub fn wrap_table() -> &'static [WrapEntry] {
    const TABLE: &[WrapEntry] = &[
        WrapEntry {
            atom: "symforge::embed::AtomicAuthority",
            obligation: "wrapped-here",
        },
        WrapEntry {
            atom: "symforge::embed::Claim",
            obligation: "wrapped-here",
        },
        WrapEntry {
            atom: "symforge::embed::ClaimProvenance",
            obligation: "wrapped-here",
        },
        WrapEntry {
            atom: "symforge::embed::EmbeddedSourceHandle",
            obligation: "wrapped-here",
        },
        WrapEntry {
            atom: "symforge::embed::EmbeddedSourceSpec",
            obligation: "wrapped-here",
        },
        WrapEntry {
            atom: "symforge::embed::EvaluationProvenance",
            obligation: "wrapped-here",
        },
        WrapEntry {
            atom: "symforge::embed::OperationKind",
            obligation: "verbatim-reexport",
        },
        WrapEntry {
            atom: "symforge::embed::OperationReceipt",
            obligation: "wrapped-here",
        },
        WrapEntry {
            atom: "symforge::embed::ProcessIndexRuntime",
            obligation: "wrapped-here",
        },
        WrapEntry {
            atom: "symforge::embed::ReceiptWaitError",
            obligation: "wrapped-here",
        },
        WrapEntry {
            atom: "symforge::embed::RefreshTicket",
            obligation: "wrapped-here",
        },
        WrapEntry {
            atom: "symforge::embed::RetryAdvice",
            obligation: "verbatim-reexport",
        },
        WrapEntry {
            atom: "symforge::embed::ShutdownReceipt",
            obligation: "wrapped-here",
        },
        WrapEntry {
            atom: "symforge::embed::ShutdownReport",
            obligation: "wrapped-here",
        },
        WrapEntry {
            atom: "symforge::embed::SourceCloseReceipt",
            obligation: "wrapped-here",
        },
        WrapEntry {
            atom: "symforge::embed::SourceCloseReport",
            obligation: "wrapped-here",
        },
        WrapEntry {
            atom: "symforge::embed::SourceRefusal",
            obligation: "wrapped-here",
        },
        WrapEntry {
            atom: "symforge::embed::SourceRefusalKind",
            obligation: "verbatim-reexport",
        },
        WrapEntry {
            atom: "symforge::embed::SourceRuntimePhase",
            obligation: "wrapped-here",
        },
        WrapEntry {
            atom: "symforge::embed::SourceRuntimeView",
            obligation: "wrapped-here",
        },
        WrapEntry {
            atom: "symforge::embed::SymbolMatch",
            obligation: "wrapped-here",
        },
        WrapEntry {
            atom: "symforge::embed::SymbolSearchRequest",
            obligation: "wrapped-here",
        },
        WrapEntry {
            atom: "symforge::embed::SymbolSearchResult",
            obligation: "wrapped-here",
        },
        WrapEntry {
            atom: "symforge::embed::TextMatch",
            obligation: "wrapped-here",
        },
        WrapEntry {
            atom: "symforge::embed::TextSearchRequest",
            obligation: "wrapped-here",
        },
        WrapEntry {
            atom: "symforge::embed::TextSearchResult",
            obligation: "wrapped-here",
        },
        WrapEntry {
            atom: "symforge::server_api",
            obligation: "keyword-flip",
        },
        WrapEntry {
            atom: "symforge::server_api::ServerBootstrapError",
            obligation: "keyword-flip",
        },
        WrapEntry {
            atom: "symforge::server_api::ServerExit",
            obligation: "keyword-flip",
        },
        WrapEntry {
            atom: "symforge::server_api::run",
            obligation: "keyword-flip",
        },
    ];
    TABLE
}

/// Render the export delta as closed JSON: the frozen contract's SHA, every
/// introduced atom, the live census it is measured against, the per-atom
/// obligations, and the two forbidden citizens D12 records. Deterministic:
/// every collection is sorted.
pub fn render_export_delta(contract_text: &str, lib_text: &str) -> String {
    let contract_sha = crate::hash::digest_hex(contract_text.as_bytes());
    let contract: serde_json::Value =
        serde_json::from_str(contract_text).expect("frozen contract parses");
    let atoms: Vec<String> = contract["migration_v10"]["introduced_v11_atoms"]
        .as_array()
        .expect("atoms")
        .iter()
        .map(|a| a.as_str().expect("atom").to_string())
        .collect();
    let live_mods: BTreeSet<String> = lib_text
        .lines()
        .filter_map(parse_pub_mod)
        .map(|name| format!("symforge::{name}"))
        .collect();
    // C14: the subtraction the artifact CLAIMS is the subtraction the
    // renderer PERFORMS — exact-match only: an atom drops out when it
    // appears VERBATIM in the live pub-mod census. A first draft keyed this
    // on the top-level module and wrongly subtracted all embed item atoms
    // because V10's `pub mod embed` exists — module existence is not item
    // existence.
    let introduced_minus_live: Vec<&String> = atoms
        .iter()
        .filter(|atom| !live_mods.contains(*atom))
        .collect();

    let obligations: Vec<serde_json::Value> = wrap_table()
        .iter()
        .map(|entry| {
            serde_json::json!({
                "atom": entry.atom,
                "obligation": entry.obligation,
            })
        })
        .collect();

    let delta = serde_json::json!({
        "kind": "symforge-feature-020-export-delta",
        "schema_version": 1,
        "contract_sha256": contract_sha,
        "computed_as": "introduced_v11_atoms listed verbatim; introduced_minus_live is that list with every atom that already appears verbatim in the live pub-mod census of src/lib.rs subtracted",
        "live_census_pub_mods": live_mods.iter().collect::<Vec<_>>(),
        "introduced_atoms": atoms,
        "introduced_minus_live": introduced_minus_live,
        "obligations": obligations,
        "forbidden_at_activation": [
            {
                "citizen": "symforge::protocol::format::claim_provenance",
                "rule": "D12: the internal provenance mount must not surface as a public module path; the embed boundary wraps it"
            },
            {
                "citizen": "symforge::live_index::knowledge_bridge::LimitBreach through TruncationBreaches",
                "rule": "D12/D13: a deep internal type must not leak through the embed surface; the boundary wraps or unwinds it"
            }
        ],
        "server_api": {
            "form": "cfg feature=server gated pub mod server_api in src/lib.rs, wired to the crate dispatcher",
            "activation": "executed at C5: the pub(crate) keyword flipped behind the already-present server cfg gate, and the census carries the four server_api atoms in server graphs only - the embed-v11 projection excludes this module, so no embed cell may ever grow them"
        }
    });
    serde_json::to_string_pretty(&delta).expect("delta serializes")
}

/// Parse one `pub mod NAME;` census line, tolerant of interior whitespace —
/// aligned with the checker's regex rather than a stricter literal prefix.
fn parse_pub_mod(line: &str) -> Option<&str> {
    let mut words = line.split_whitespace();
    if words.next() != Some("pub") || words.next() != Some("mod") {
        return None;
    }
    let name = words.next()?.strip_suffix(';')?;
    if words.next().is_some() || name.is_empty() {
        return None;
    }
    Some(name)
}

/// Feature 020 V11, T048 — the dark wrapper contract-shape oracle.
///
/// Moved in-crate verbatim from `tests/public_api_delta_v11.rs` at the start
/// of the Slice 4 activation cut so `refusal_probe_for_test` could tighten to
/// `all(test, feature = "server")` and stop shipping in the release binary
/// (the same precondition discharge as `runtime.rs::dark_runtime_oracles`).
/// The export-delta oracle stays external — it consumes no fixture door.
#[cfg(all(test, feature = "server"))]
mod dark_wrapper_oracles {
    use super::super::embedded::EmbeddedSourceFactory;
    use super::super::registry::ProjectKey;
    use super::{
        EVIDENCE_ABSENT, EmbedOperationReceipt, EmbedSourceRefusal, ProcessRuntimeApi,
        SymbolSearchRequest, TextSearchRequest,
    };

    #[test]
    fn dark_wrappers_match_contract_shapes() {
        // acquire takes NO arguments per the atom. Since C5 it attaches to
        // the ONE process capacity runtime through the activation ceremony
        // (the provisional private-incarnation constant retired with it).
        let runtime = ProcessRuntimeApi::acquire().expect("the acquisition admits");

        // The refusal wrapper: kind-prefixed identity strings, stored at wrap
        // time; Display and Error implemented; the sentinel reserved for
        // refusals that examined nothing.
        let refusal: EmbedSourceRefusal = runtime
            .refusal_probe_for_test()
            .expect_err("the probe yields the wrapper's honest dark refusal");
        let evidence = refusal.evidence_identity();
        assert_eq!(
            evidence, EVIDENCE_ABSENT,
            "a refusal that examined no authority renders the closed sentinel"
        );
        assert!(
            !evidence.starts_with("auth-"),
            "the sentinel is a token the identity renderer cannot emit"
        );
        let operation: &EmbedOperationReceipt = refusal.operation();
        assert!(
            operation.identity().starts_with("op-"),
            "operation identities are kind-prefixed, got {}",
            operation.identity()
        );
        assert!(
            operation
                .identity()
                .trim_start_matches("op-")
                .parse::<u64>()
                .is_ok(),
            "the prefix is followed by the counter digits"
        );
        let first = operation.identity().to_string();
        assert_eq!(
            operation.identity(),
            first,
            "the rendered string is STORED at wrap time — stable across calls"
        );
        // Display + Error are contract trait impls, exercised not just
        // derived.
        let rendered = format!("{refusal}");
        assert!(
            rendered.contains("SourceUnavailable"),
            "Display names the refusal kind: {rendered}"
        );
        let _as_error: &dyn std::error::Error = &refusal;

        // The four V11 handle methods, under their contract shapes, refusing
        // honestly in the dark rather than fabricating empty results.
        let factory = EmbeddedSourceFactory::new();
        let handle = factory
            .open(ProjectKey::new("src-a"))
            .expect("open admits a fresh key");

        let view = handle.runtime_view();
        assert!(
            view.binding_identity.starts_with("source-"),
            "the view's binding identity is kind-prefixed: {}",
            view.binding_identity
        );
        assert!(
            view.current_publication_identity.is_none(),
            "a dark handle has NO publication; inventing one would be \
             fabricated completion"
        );
        assert_eq!(view.observer_epoch, 0, "no observer has been registered");

        let refusal = handle
            .search_symbols(&SymbolSearchRequest {
                query: Some("anchor".to_string()),
                path_prefix: None,
                limit: 10,
            })
            .expect_err("no generation is bound, so a symbol search refuses");
        assert_eq!(refusal.kind_name(), "SourceUnavailable");

        let refusal = handle
            .search_text(&TextSearchRequest {
                query: "anchor".to_string(),
                path_prefix: None,
                limit: 10,
                case_sensitive: false,
            })
            .expect_err("no generation is bound, so a text search refuses");
        assert_eq!(refusal.kind_name(), "SourceUnavailable");

        let refusal = handle
            .request_refresh()
            .expect_err("a dark refresh cannot run, so the ticket is refused honestly");
        assert_eq!(refusal.kind_name(), "SourceUnavailable");
    }
}