symforge 10.10.0

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
//! Feature 020 V11, T047 — the dark source runtime.
//!
//! The closed five-state machine from `data-model.md:1413-1449`, the strict
//! acquisition rule REFREEZE-pinned as F020-V11-A20 (`:1539-1554`), FR-043's
//! no-restore terminal paths, and the single publication root with sealed
//! per-source rebasing (`:1528-1537`) — all reachable ONLY through
//! [`DarkRuntimeFactory`]. Nothing in production constructs any of this;
//! Slice 4's activation cut is the only planned caller.
//!
//! **Payload simplifications are recorded, not hidden.** The frozen machine
//! carries observer phases, mutation epochs, revocation packages, and
//! `NonCurrentWork`; this dark slice implements the fields its ten oracles
//! exercise and the evidence document's D-ledger carries the rest as Slice 4
//! obligations. The five state NAMES are the frozen ones, the public phase has
//! the contract's six variants, and `Stopped` is derived from the registry
//! tombstone — never a sixth machine state.
//!
//! **Stand-in constructors.** Every `*_for_test` admission mints its evidence
//! unconditionally, the recorded fixture family: sealed shapes, `Ok` until the
//! real observer and candidate machinery of Slice 4 supplies refusing
//! evidence. Do not "complete" them with checks they cannot observe.
//!
//! **This module must not import from `protocol`.** Its refusal vocabulary
//! comes from `crate::lifecycle_identity`, the shared ungated home — the embed
//! gate on this module's first commit is what proved the relocation need.

use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};

use arc_swap::ArcSwap;

use crate::lifecycle_identity::{
    AuthorityIdentity, GenerationAuthority, ObserverToken, OperationKind, OperationReceipt,
    PublicationIdentity, RetryAdvice, SourceRefusal, SourceRefusalKind,
};
// Consumed only by the C3-gated probes; the plain embed build sheds it with
// them (the embed-gate unused-import class CLAUDE.md documents).
#[cfg(any(test, feature = "server"))]
use crate::lifecycle_identity::GenerationIdentity;

use super::registry::ProjectKey;

// ── The dark factory ───────────────────────────────────────────────────────

/// The ONLY door into the V11 runtime during Slice 3. Holds the physical root
/// every admitted source binds beneath.
#[derive(Debug)]
pub struct DarkRuntimeFactory {
    root: String,
}

impl DarkRuntimeFactory {
    /// Fixture constructor: the real root lease arrives with Slice 4.
    ///
    /// Every `*_for_test` probe carries
    /// `#[cfg(any(test, feature = "server"))]`. Per the Slice 0 predicate
    /// rule, `any(...)` is test-only ONLY when every disjunct is — with
    /// `server` in the default set these probes SHIP in the published server
    /// binary; only the embed build sheds them. Plain `#[cfg(test)]` is not
    /// compilable here — the Slice 3 oracles live in `tests/`, an external
    /// crate built without the lib's `test` cfg — which is the whole reason
    /// this leak exists. ACTIVATION PRECONDITION, not a trim-list optional:
    /// before the keyword flip these become `all(test, feature = "server")`
    /// by moving their oracles in-crate, or they sit behind a dedicated
    /// non-server test feature.
    #[cfg(any(test, feature = "server"))]
    pub fn for_test_root(root: &str) -> Self {
        Self {
            root: root.to_string(),
        }
    }

    /// Mint one project runtime beneath this factory's root.
    pub fn project_runtime(&self, key: ProjectKey) -> ProjectIndexRuntime {
        ProjectIndexRuntime {
            root: self.root.clone(),
            _key: key,
            next_source: AtomicU64::new(1),
            sources: Mutex::new(HashMap::new()),
            publication: ProjectPublicationRoot {
                inner: ArcSwap::new(Arc::new(ProjectRuntimePublication {
                    publication_identity: PublicationIdentity::fresh(),
                    sources: HashMap::new(),
                })),
            },
        }
    }
}

// ── Public phase and internal machine ──────────────────────────────────────

/// The PUBLIC phase vocabulary — the contract's six variants. The internal
/// machine has five states; `Stopped` exists only here, derived from the
/// registry tombstone after `Stopping` completes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SourceRuntimePhase {
    Loading,
    Current,
    Refreshing,
    Blocked,
    Stopping,
    Stopped,
}

/// The closed machine, frozen at exactly these five states
/// (`data-model.md:1413-1449`). Private: the names are not public
/// constructors, exactly as the corpus stamps them.
#[derive(Debug)]
enum SourceRuntimeState {
    Loading,
    Current {
        generation: Arc<VerifiedGeneration>,
    },
    Refreshing {
        retained: Arc<VerifiedGeneration>,
        /// F020-V11-R20A: the retention is a queryable lane ONLY while this
        /// refresh has issued no mutation permit.
        permit_issued: bool,
    },
    Blocked {
        /// Recovery evidence, never a lane (F020-V11-R20B).
        _retained: Option<Arc<VerifiedGeneration>>,
    },
    Stopping {
        /// Accounting, never a lane (F020-V11-R20B).
        _retained: Option<Arc<VerifiedGeneration>>,
    },
}

#[derive(Debug)]
struct SourceEntry {
    state: SourceRuntimeState,
    /// The registry tombstone. `Stopped` is THIS, not a machine state.
    tombstoned: bool,
}

/// Opaque handle naming one admitted source within its runtime.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct DarkSourceHandle(u64);

// ── VerifiedGeneration and the strict lease ────────────────────────────────

/// A COMPLETE verified generation. The frozen skeleton carries manifests,
/// certificates, ledgers, and charged roots (`data-model.md:1382-1400`); the
/// dark slice retains the exact authority Arc and the completeness verdict its
/// oracles pin, with the remaining fields recorded as Slice 4 obligations.
#[derive(Debug)]
pub struct VerifiedGeneration {
    generation_authority: Arc<GenerationAuthority>,
    complete: bool,
}

impl VerifiedGeneration {
    /// The generation's own identity is its AUTHORITY's identity — one exact
    /// Arc, retained, never re-minted (`data-model.md:1523-1526`).
    pub fn identity(&self) -> AuthorityIdentity {
        self.generation_authority.identity()
    }

    pub fn scope_certificate_is_complete(&self) -> bool {
        self.complete
    }

    pub fn generation_authority(&self) -> Arc<GenerationAuthority> {
        Arc::clone(&self.generation_authority)
    }
}

/// A strict acquisition: proof that a COMPLETE verified generation was
/// leasable at acquisition time.
#[derive(Debug)]
pub struct StrictLease {
    generation: Arc<VerifiedGeneration>,
}

impl StrictLease {
    pub fn generation(&self) -> Arc<VerifiedGeneration> {
        Arc::clone(&self.generation)
    }
}

// ── The publication root ───────────────────────────────────────────────────

/// The SOLE publication root for a project's runtime state
/// (`data-model.md:1528-1537`): one `ArcSwap` whose every swap carries a
/// never-reused publication identity.
#[derive(Debug)]
pub struct ProjectPublicationRoot {
    inner: ArcSwap<ProjectRuntimePublication>,
}

impl ProjectPublicationRoot {
    pub fn load(&self) -> Arc<ProjectRuntimePublication> {
        self.inner.load_full()
    }
}

/// One published snapshot of every source's public state.
#[derive(Debug)]
pub struct ProjectRuntimePublication {
    publication_identity: PublicationIdentity,
    sources: HashMap<u64, Arc<SourceStatePublication>>,
}

impl ProjectRuntimePublication {
    pub fn publication_identity(&self) -> PublicationIdentity {
        self.publication_identity
    }

    pub fn source_publication(
        &self,
        source: &DarkSourceHandle,
    ) -> Option<Arc<SourceStatePublication>> {
        self.sources.get(&source.0).cloned()
    }
}

/// The published per-source record. A sealed transition replaces exactly ONE
/// of these; every sibling's Arc survives identical.
#[derive(Debug)]
pub struct SourceStatePublication {
    phase: SourceRuntimePhase,
}

impl SourceStatePublication {
    pub fn phase(&self) -> SourceRuntimePhase {
        self.phase
    }
}

/// A validated capture of one source's published view
/// (`data-model.md:1556-1564`): loaded, revalidated against the root, and
/// carrying an observer token only when one is actually registered.
#[derive(Debug)]
pub struct SourceView {
    publication_identity: PublicationIdentity,
    observer_token: Option<ObserverToken>,
}

impl SourceView {
    pub fn publication_identity(&self) -> PublicationIdentity {
        self.publication_identity
    }

    pub fn observer_token(&self) -> Option<ObserverToken> {
        self.observer_token
    }
}

// ── Permits and shutdown receipts ──────────────────────────────────────────

/// The dark mutation permit. FR-043 is a property of the MACHINE, not of this
/// value: once granted, no terminal path of the permit — commit, rollback with
/// a no-side-effect proof, or drop — restores the prior `Current`; return
/// happens only through fresh candidate publication, which is Slice 4.
#[derive(Debug)]
pub struct DarkMutationPermit {
    _sealed: (),
}

impl DarkMutationPermit {
    /// A proven no-op rollback. Consumes the permit; the source stays
    /// non-current, because FR-043 forbids restoring the prior publication
    /// even when nothing was written.
    #[cfg(any(test, feature = "server"))]
    pub fn rollback_with_no_side_effect_proof_for_test(self) {}
}

/// Receipt for a dark shutdown. Nothing was spawned, so the wait completes
/// immediately and the report can only honestly count zero joins.
#[derive(Debug)]
pub struct DarkShutdownReceipt {
    _sealed: (),
}

impl DarkShutdownReceipt {
    #[cfg(any(test, feature = "server"))]
    pub fn wait_for_test(self) -> Result<DarkShutdownReport, SourceRefusal> {
        Ok(DarkShutdownReport { joined_workers: 0 })
    }
}

/// What the shutdown OBSERVED. `joined_workers` is zero because the dark
/// modules spawn no threads, timers, or tasks — any other number would report
/// a completion nothing witnessed.
#[derive(Debug)]
pub struct DarkShutdownReport {
    joined_workers: u64,
}

impl DarkShutdownReport {
    pub fn joined_workers(&self) -> u64 {
        self.joined_workers
    }
}

// ── The project runtime ────────────────────────────────────────────────────

/// The per-project V11 runtime: owns the source machines and the one
/// publication root. SEAM-pinned name.
#[derive(Debug)]
pub struct ProjectIndexRuntime {
    root: String,
    _key: ProjectKey,
    next_source: AtomicU64,
    sources: Mutex<HashMap<u64, SourceEntry>>,
    publication: ProjectPublicationRoot,
}

impl ProjectIndexRuntime {
    pub fn publication_root(&self) -> &ProjectPublicationRoot {
        &self.publication
    }

    /// Admit a source that is still loading: it retains NOTHING and is
    /// therefore not queryable.
    #[cfg(any(test, feature = "server"))]
    pub fn admit_loading_source_for_test(&self, _name: &str) -> DarkSourceHandle {
        self.insert_source(SourceRuntimeState::Loading)
    }

    /// Admit a source straight at `Current` with a complete verified
    /// generation — the fixture family's unconditional evidence.
    #[cfg(any(test, feature = "server"))]
    pub fn admit_current_source_for_test(&self, _name: &str) -> DarkSourceHandle {
        let generation = Arc::new(VerifiedGeneration {
            generation_authority: Arc::new(GenerationAuthority::captured(
                AuthorityIdentity::fresh(),
                GenerationIdentity::fresh(),
                self.root.clone(),
            )),
            complete: true,
        });
        self.insert_source(SourceRuntimeState::Current { generation })
    }

    /// Enter a reload-shaped refresh: the current generation becomes the
    /// retention, untouched, and stays a queryable lane until a permit exists.
    #[cfg(any(test, feature = "server"))]
    pub fn begin_reload_refresh_for_test(&self, source: &DarkSourceHandle) {
        self.transition(source, |state| match state {
            SourceRuntimeState::Current { generation } => SourceRuntimeState::Refreshing {
                retained: generation,
                permit_issued: false,
            },
            other => other,
        });
    }

    /// Grant the refresh its mutation permit. From this instant the retention
    /// stops being a lane (R20A), BEFORE any side effect can run.
    #[cfg(any(test, feature = "server"))]
    pub fn grant_mutation_permit_for_test(
        &self,
        source: &DarkSourceHandle,
    ) -> Result<DarkMutationPermit, SourceRefusal> {
        let mut granted = false;
        self.transition(source, |state| match state {
            SourceRuntimeState::Refreshing { retained, .. } => {
                granted = true;
                SourceRuntimeState::Refreshing {
                    retained,
                    permit_issued: true,
                }
            }
            other => other,
        });
        if granted {
            Ok(DarkMutationPermit { _sealed: () })
        } else {
            Err(self.refusal(OperationKind::RefreshSource, None))
        }
    }

    #[cfg(any(test, feature = "server"))]
    pub fn block_source_for_test(&self, source: &DarkSourceHandle) {
        self.transition(source, |state| {
            let retained = retained_of(state);
            SourceRuntimeState::Blocked {
                _retained: retained,
            }
        });
    }

    #[cfg(any(test, feature = "server"))]
    pub fn stop_source_for_test(&self, source: &DarkSourceHandle) {
        self.transition(source, |state| {
            let retained = retained_of(state);
            SourceRuntimeState::Stopping {
                _retained: retained,
            }
        });
    }

    /// Complete the stop: the REGISTRY tombstones the source. The machine
    /// itself never holds a `Stopped` state — the public phase derives it.
    #[cfg(any(test, feature = "server"))]
    pub fn complete_stop_for_test(&self, source: &DarkSourceHandle) {
        let mut sources = self.sources.lock().expect("runtime lock");
        if let Some(entry) = sources.get_mut(&source.0) {
            entry.tombstoned = true;
        }
        drop(sources);
        self.republish(Some(source.0));
    }

    /// The strict acquisition, closed on COMPLETENESS (F020-V11-A20):
    /// `Current` leases; a permit-free `Refreshing` leases its retention;
    /// everything else refuses.
    pub fn acquire_strict(&self, source: &DarkSourceHandle) -> Result<StrictLease, SourceRefusal> {
        let sources = self.sources.lock().expect("runtime lock");
        let Some(entry) = sources.get(&source.0) else {
            return Err(self.refusal(OperationKind::AcquireRuntime, None));
        };
        if entry.tombstoned {
            return Err(self.refusal(OperationKind::AcquireRuntime, None));
        }
        match &entry.state {
            SourceRuntimeState::Current { generation } => Ok(StrictLease {
                generation: Arc::clone(generation),
            }),
            SourceRuntimeState::Refreshing {
                retained,
                permit_issued: false,
            } => Ok(StrictLease {
                generation: Arc::clone(retained),
            }),
            SourceRuntimeState::Refreshing {
                retained,
                permit_issued: true,
            } => Err(self.refusal(OperationKind::AcquireRuntime, Some(retained.identity()))),
            SourceRuntimeState::Loading
            | SourceRuntimeState::Blocked { .. }
            | SourceRuntimeState::Stopping { .. } => {
                Err(self.refusal(OperationKind::AcquireRuntime, None))
            }
        }
    }

    /// The public phase: the tombstone wins, then the machine state maps.
    pub fn source_phase(&self, source: &DarkSourceHandle) -> SourceRuntimePhase {
        let sources = self.sources.lock().expect("runtime lock");
        let Some(entry) = sources.get(&source.0) else {
            return SourceRuntimePhase::Stopped;
        };
        if entry.tombstoned {
            return SourceRuntimePhase::Stopped;
        }
        match &entry.state {
            SourceRuntimeState::Loading => SourceRuntimePhase::Loading,
            SourceRuntimeState::Current { .. } => SourceRuntimePhase::Current,
            SourceRuntimeState::Refreshing { .. } => SourceRuntimePhase::Refreshing,
            SourceRuntimeState::Blocked { .. } => SourceRuntimePhase::Blocked,
            SourceRuntimeState::Stopping { .. } => SourceRuntimePhase::Stopping,
        }
    }

    /// Load, revalidate against the root, and return a view that carries an
    /// observer token only when one is registered — the capture never invents
    /// (`data-model.md:1556-1564`). The dark runtime registers no observers,
    /// and single-threaded revalidation cannot drift, so one validated pass
    /// suffices; the retry loop arrives with the real observer machinery.
    pub fn capture_source_view(
        &self,
        source: &DarkSourceHandle,
    ) -> Result<SourceView, SourceRefusal> {
        let first = self.publication.load();
        if first.source_publication(source).is_none() {
            return Err(self.refusal(OperationKind::AcquireRuntime, None));
        }
        let revalidated = self.publication.load();
        if revalidated.publication_identity() != first.publication_identity() {
            // Drift between load and revalidate: refuse rather than compose
            // two publications; the retrying capture is Slice 4 work.
            return Err(self.refusal(OperationKind::AcquireRuntime, None));
        }
        Ok(SourceView {
            publication_identity: revalidated.publication_identity(),
            observer_token: None,
        })
    }

    /// Begin the dark shutdown. Nothing was spawned, so there is nothing to
    /// signal; the receipt's wait reports what was observed: zero joins.
    #[cfg(any(test, feature = "server"))]
    pub fn begin_shutdown_for_test(&self) -> DarkShutdownReceipt {
        DarkShutdownReceipt { _sealed: () }
    }

    // ── internals ──────────────────────────────────────────────────────────

    fn insert_source(&self, state: SourceRuntimeState) -> DarkSourceHandle {
        let id = self.next_source.fetch_add(1, Ordering::Relaxed);
        self.sources.lock().expect("runtime lock").insert(
            id,
            SourceEntry {
                state,
                tombstoned: false,
            },
        );
        self.republish(Some(id));
        DarkSourceHandle(id)
    }

    fn transition(
        &self,
        source: &DarkSourceHandle,
        apply: impl FnOnce(SourceRuntimeState) -> SourceRuntimeState,
    ) {
        let mut sources = self.sources.lock().expect("runtime lock");
        if let Some(entry) = sources.remove(&source.0) {
            let next = apply(entry.state);
            sources.insert(
                source.0,
                SourceEntry {
                    state: next,
                    tombstoned: entry.tombstoned,
                },
            );
        }
        drop(sources);
        self.republish(Some(source.0));
    }

    /// Publish a successor: a NEW never-reused publication identity, the
    /// affected source's record replaced, and every sibling's Arc carried
    /// over IDENTICAL — the sealed per-source rebase of `data-model.md:1528`.
    fn republish(&self, changed: Option<u64>) {
        let sources = self.sources.lock().expect("runtime lock");
        let previous = self.publication.load();
        let mut published: HashMap<u64, Arc<SourceStatePublication>> = HashMap::new();
        for (id, entry) in sources.iter() {
            let phase = if entry.tombstoned {
                SourceRuntimePhase::Stopped
            } else {
                match &entry.state {
                    SourceRuntimeState::Loading => SourceRuntimePhase::Loading,
                    SourceRuntimeState::Current { .. } => SourceRuntimePhase::Current,
                    SourceRuntimeState::Refreshing { .. } => SourceRuntimePhase::Refreshing,
                    SourceRuntimeState::Blocked { .. } => SourceRuntimePhase::Blocked,
                    SourceRuntimeState::Stopping { .. } => SourceRuntimePhase::Stopping,
                }
            };
            let record = match (changed, previous.sources.get(id)) {
                (Some(changed_id), Some(existing)) if *id != changed_id => Arc::clone(existing),
                (_, Some(existing)) if existing.phase == phase => Arc::clone(existing),
                _ => Arc::new(SourceStatePublication { phase }),
            };
            published.insert(*id, record);
        }
        self.publication
            .inner
            .store(Arc::new(ProjectRuntimePublication {
                publication_identity: PublicationIdentity::fresh(),
                sources: published,
            }));
    }

    /// A strict-acquisition refusal, naming the evidence that was examined
    /// when there was any: a permit-holding refresh names its retention; the
    /// evidence-free states name nothing rather than minting.
    /// Mint the runtime's honest dark refusal. The receipt is the C5-ruled
    /// dark constructor — argument identity is NOT claimed on these lanes —
    /// and the kind names the operation that actually refused, per call site.
    fn refusal(&self, kind: OperationKind, evidence: Option<AuthorityIdentity>) -> SourceRefusal {
        SourceRefusal::for_runtime(
            SourceRefusalKind::SourceUnavailable,
            OperationReceipt::for_dark_refusal(kind),
            RetryAdvice::OnEvent,
            evidence,
        )
    }
}

fn retained_of(state: SourceRuntimeState) -> Option<Arc<VerifiedGeneration>> {
    match state {
        SourceRuntimeState::Current { generation } => Some(generation),
        SourceRuntimeState::Refreshing { retained, .. } => Some(retained),
        SourceRuntimeState::Blocked { _retained } => _retained,
        SourceRuntimeState::Stopping { _retained } => _retained,
        SourceRuntimeState::Loading => None,
    }
}