symforge 11.22.1

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
use rmcp::handler::server::wrapper::Parameters;
use rmcp::model::{GetPromptResult, PromptMessage, Role};
use rmcp::{prompt, prompt_router};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use super::SymForgeServer;
use crate::protocol::resources::{
    file_context_resource, repo_changes_resource, repo_health_resource, repo_map_resource,
    repo_outline_resource, tools_catalog_resource,
};

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
pub struct CodeReviewPromptInput {
    pub path: Option<String>,
    pub focus: Option<String>,
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
pub struct ArchitectureMapPromptInput {
    pub area: Option<String>,
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
pub struct FailureTriagePromptInput {
    pub symptom: String,
    pub path: Option<String>,
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
pub struct OnboardPromptInput {
    /// Optional area or module to focus on first.
    pub area: Option<String>,
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
pub struct RefactorPromptInput {
    /// What you want to refactor (e.g., "rename ErrorKind to AppError", "extract validation logic").
    pub goal: String,
    /// Optional file or symbol to start from.
    pub target: Option<String>,
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
pub struct DebugPromptInput {
    /// The error message, stack trace, or unexpected behavior.
    pub error: String,
    /// Optional file path where the error occurs.
    pub path: Option<String>,
}

#[derive(Debug, Clone, Default, Deserialize, Serialize, JsonSchema)]
pub struct AdminPromptInput {}

#[derive(Debug, Clone, Default, Deserialize, Serialize, JsonSchema)]
pub struct KnowledgeHygienePromptInput {
    /// Optional repository-relative path prefix to constrain the review.
    pub path_prefix: Option<String>,
}

#[prompt_router(vis = "pub(crate)")]
impl SymForgeServer {
    #[prompt(
        name = "symforge-review",
        description = "Generate a code review plan using SymForge context surfaces."
    )]
    pub(crate) async fn code_review_prompt(
        &self,
        params: Parameters<CodeReviewPromptInput>,
    ) -> GetPromptResult {
        let mut messages = vec![
            PromptMessage::new_text(
                Role::User,
                build_code_review_instructions(&self.project_name, &params.0),
            ),
            PromptMessage::new_resource_link(Role::User, repo_health_resource()),
            PromptMessage::new_resource_link(Role::User, repo_map_resource()),
        ];

        if let Some(path) = params.0.path.as_deref() {
            messages.push(PromptMessage::new_resource_link(
                Role::User,
                file_context_resource(path, Some(200)),
            ));
        }

        GetPromptResult::new(messages)
            .with_description("Review code using SymForge resources and targeted tools.")
    }

    #[prompt(
        name = "symforge-architecture",
        description = "Generate an architecture mapping plan using SymForge repo context."
    )]
    pub(crate) async fn architecture_map_prompt(
        &self,
        params: Parameters<ArchitectureMapPromptInput>,
    ) -> GetPromptResult {
        let mut messages = vec![
            PromptMessage::new_text(
                Role::User,
                build_architecture_map_instructions(&self.project_name, params.0.area.as_deref()),
            ),
            PromptMessage::new_resource_link(Role::User, repo_map_resource()),
            PromptMessage::new_resource_link(Role::User, repo_outline_resource()),
            PromptMessage::new_resource_link(Role::User, repo_health_resource()),
        ];

        if let Some(area) = params.0.area.as_deref() {
            messages.push(PromptMessage::new_text(
                Role::User,
                format!("Prioritize the area or subsystem named '{area}' if it exists."),
            ));
        }

        GetPromptResult::new(messages).with_description(
            "Map repository architecture using SymForge resources and cross-reference tools.",
        )
    }

    #[prompt(
        name = "symforge-triage",
        description = "Generate a debugging and failure-triage plan using SymForge state."
    )]
    pub(crate) async fn failure_triage_prompt(
        &self,
        params: Parameters<FailureTriagePromptInput>,
    ) -> GetPromptResult {
        let mut messages = vec![
            PromptMessage::new_text(
                Role::User,
                build_failure_triage_instructions(&self.project_name, &params.0),
            ),
            PromptMessage::new_resource_link(Role::User, repo_health_resource()),
            PromptMessage::new_resource_link(Role::User, repo_changes_resource()),
            PromptMessage::new_resource_link(Role::User, repo_map_resource()),
        ];

        if let Some(path) = params.0.path.as_deref() {
            messages.push(PromptMessage::new_resource_link(
                Role::User,
                file_context_resource(path, Some(200)),
            ));
        }

        GetPromptResult::new(messages).with_description(
            "Triage failures using SymForge runtime health, changed files, and local context.",
        )
    }

    #[prompt(
        name = "symforge-onboard",
        description = "Generate a codebase onboarding plan using SymForge for guided exploration."
    )]
    pub(crate) async fn onboard_prompt(
        &self,
        params: Parameters<OnboardPromptInput>,
    ) -> GetPromptResult {
        let mut messages = vec![
            PromptMessage::new_text(
                Role::User,
                build_onboard_instructions(&self.project_name, params.0.area.as_deref()),
            ),
            PromptMessage::new_resource_link(Role::User, repo_map_resource()),
            PromptMessage::new_resource_link(Role::User, repo_outline_resource()),
            PromptMessage::new_resource_link(Role::User, repo_health_resource()),
            PromptMessage::new_resource_link(Role::User, tools_catalog_resource()),
        ];

        if let Some(area) = params.0.area.as_deref() {
            messages.push(PromptMessage::new_text(
                Role::User,
                format!("Focus onboarding on the '{area}' area first."),
            ));
        }

        GetPromptResult::new(messages)
            .with_description("Onboard to a codebase using layered SymForge exploration.")
    }

    #[prompt(
        name = "symforge-refactor",
        description = "Generate a refactoring plan with impact analysis using SymForge."
    )]
    pub(crate) async fn refactor_prompt(
        &self,
        params: Parameters<RefactorPromptInput>,
    ) -> GetPromptResult {
        let mut messages = vec![
            PromptMessage::new_text(
                Role::User,
                build_refactor_instructions(&self.project_name, &params.0),
            ),
            PromptMessage::new_resource_link(Role::User, repo_map_resource()),
            PromptMessage::new_resource_link(Role::User, repo_health_resource()),
        ];

        if let Some(target) = params.0.target.as_deref() {
            messages.push(PromptMessage::new_resource_link(
                Role::User,
                file_context_resource(target, Some(200)),
            ));
        }

        GetPromptResult::new(messages)
            .with_description("Plan a refactoring with full impact analysis using SymForge.")
    }

    #[prompt(
        name = "symforge-debug",
        description = "Generate a detailed debugging plan using SymForge call tracing and change analysis."
    )]
    pub(crate) async fn debug_prompt(
        &self,
        params: Parameters<DebugPromptInput>,
    ) -> GetPromptResult {
        let mut messages = vec![
            PromptMessage::new_text(
                Role::User,
                build_debug_instructions(&self.project_name, &params.0),
            ),
            PromptMessage::new_resource_link(Role::User, repo_health_resource()),
            PromptMessage::new_resource_link(Role::User, repo_changes_resource()),
        ];

        if let Some(path) = params.0.path.as_deref() {
            messages.push(PromptMessage::new_resource_link(
                Role::User,
                file_context_resource(path, Some(200)),
            ));
        }

        GetPromptResult::new(messages)
            .with_description("Debug a problem using SymForge call tracing and impact analysis.")
    }

    #[prompt(
        name = "symforge-knowledge-hygiene",
        description = "Review repository knowledge evidence and prepare advisory remediation proposals without mutating files."
    )]
    pub(crate) async fn knowledge_hygiene_prompt(
        &self,
        params: Parameters<KnowledgeHygienePromptInput>,
    ) -> GetPromptResult {
        let messages = vec![
            PromptMessage::new_text(
                Role::User,
                build_knowledge_hygiene_instructions(
                    &self.project_name,
                    params.0.path_prefix.as_deref(),
                ),
            ),
            PromptMessage::new_resource_link(Role::User, repo_health_resource()),
            PromptMessage::new_resource_link(Role::User, repo_map_resource()),
        ];
        GetPromptResult::new(messages).with_description(
            "Review exact knowledge evidence, propose the smallest safe remediation, and stop for explicit approval before any preview or mutation.",
        )
    }

    #[prompt(
        name = "symforge-admin",
        description = "Return the running SymForge operator dashboard URL, or guidance to start it."
    )]
    pub(crate) async fn admin_prompt(
        &self,
        _params: Parameters<AdminPromptInput>,
    ) -> GetPromptResult {
        // The universal in-harness affordance (FR-016, Constitution II): every
        // MCP-speaking harness gets this, with or without a command file.
        //
        // Execution-model decision (009 US3): a prompt handler is a pure,
        // read-only fetch. It does NOT start a server — starting one is a
        // blocking, side-effectful process action that belongs to the
        // `symforge admin` CLI verb, and `operator_server_reachable` /
        // `start_operator_server` build their own current-thread tokio runtime and
        // `block_on`, which would panic if invoked directly on this async worker.
        // Instead the prompt does the read-only reachability check off the async
        // runtime (via `spawn_blocking`) and returns the reachable dashboard URL,
        // or guidance to run `symforge admin` when nothing is up yet.
        let repo_root = self.capture_repo_root();
        let dashboard_url =
            tokio::task::spawn_blocking(move || resolve_running_dashboard_url(repo_root))
                .await
                .unwrap_or(None);

        let body = build_admin_instructions(&self.project_name, dashboard_url.as_deref());
        GetPromptResult::new(vec![PromptMessage::new_text(Role::User, body)])
            .with_description(
                "Open the SymForge operator dashboard (reuse the running server, or start it via `symforge admin`).",
            )
    }
}

fn build_knowledge_hygiene_instructions(project_name: &str, path_prefix: Option<&str>) -> String {
    crate::prompt_engine::build_knowledge_hygiene_instructions(project_name, path_prefix)
}

/// Read-only resolution of the running operator dashboard URL for the
/// `symforge-admin` prompt: load the project's `OperatorSetupProfile` for the
/// remembered port and, if a server is reachable there, return its `/admin` URL.
/// Returns `None` when there is no profile or nothing serves the remembered port.
///
/// This performs an HTTP reachability probe on its own current-thread runtime, so
/// it MUST run off the async worker (the caller wraps it in `spawn_blocking`). It
/// never starts a server — that is the `symforge admin` CLI verb's job (D3).
fn resolve_running_dashboard_url(repo_root: Option<std::path::PathBuf>) -> Option<String> {
    let control_state_dir = crate::paths::process_control_state_placement().directory()?;
    resolve_running_dashboard_url_in(repo_root, control_state_dir)
}

/// Reachability check against an EXPLICIT control-state dir.
///
/// `resolve_running_dashboard_url` reached straight for the process-global
/// placement, which is a `OnceLock` and therefore not overridable. That left the
/// no-server guidance test unable to isolate itself: any sibling test that
/// started a real operator server and wrote a profile made this probe succeed,
/// so the prompt correctly reported a running dashboard and the test failed by
/// execution order. Taking the dir as an argument makes the dependency
/// injectable without changing which dir production resolves.
fn resolve_running_dashboard_url_in(
    _repo_root: Option<std::path::PathBuf>,
    control_state_dir: &crate::domain::ControlStateDir,
) -> Option<String> {
    use std::time::Duration;
    let profile = crate::cli::operator_profile::OperatorSetupProfile::load(control_state_dir)?;
    let addr = std::net::SocketAddr::new(
        std::net::IpAddr::V4(std::net::Ipv4Addr::LOCALHOST),
        profile.port,
    );
    if crate::cli::admin::operator_server_reachable(addr, Duration::from_millis(500)) {
        Some(format!("http://{addr}/admin"))
    } else {
        None
    }
}

fn build_admin_instructions(project_name: &str, dashboard_url: Option<&str>) -> String {
    crate::prompt_engine::build_admin_instructions(project_name, dashboard_url)
}

fn build_code_review_instructions(project_name: &str, input: &CodeReviewPromptInput) -> String {
    crate::prompt_engine::build_code_review_instructions(
        project_name,
        input.path.as_deref(),
        input.focus.as_deref(),
    )
}

fn build_architecture_map_instructions(project_name: &str, area: Option<&str>) -> String {
    crate::prompt_engine::build_architecture_map_instructions(project_name, area)
}

fn build_failure_triage_instructions(
    project_name: &str,
    input: &FailureTriagePromptInput,
) -> String {
    crate::prompt_engine::build_failure_triage_instructions(
        project_name,
        &input.symptom,
        input.path.as_deref(),
    )
}

fn build_onboard_instructions(project_name: &str, area: Option<&str>) -> String {
    crate::prompt_engine::build_onboard_instructions(project_name, area)
}

fn build_refactor_instructions(project_name: &str, input: &RefactorPromptInput) -> String {
    crate::prompt_engine::build_refactor_instructions(
        project_name,
        &input.goal,
        input.target.as_deref(),
    )
}

fn build_debug_instructions(project_name: &str, input: &DebugPromptInput) -> String {
    crate::prompt_engine::build_debug_instructions(
        project_name,
        &input.error,
        input.path.as_deref(),
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use parking_lot::Mutex;
    use std::collections::HashMap;
    use std::sync::Arc;
    use std::time::{Duration, Instant};

    use crate::live_index::store::{CircuitBreakerState, LiveIndex};
    use crate::watcher::WatcherInfo;

    use crate::protocol::resources::REPO_HEALTH_URI;

    fn make_server() -> SymForgeServer {
        let index = LiveIndex {
            files: HashMap::new(),
            loaded_at: Instant::now(),
            loaded_at_system: std::time::SystemTime::now(),
            load_duration: Duration::from_millis(1),
            cb_state: CircuitBreakerState::new(0.20),
            is_empty: false,
            load_source: crate::live_index::store::IndexLoadSource::FreshLoad,
            snapshot_verify_state: crate::live_index::store::SnapshotVerifyState::NotNeeded,
            reverse_index: HashMap::new(),
            files_by_basename: HashMap::new(),
            files_by_dir_component: HashMap::new(),
            trigram_index: crate::live_index::trigram::TrigramIndex::new(),
            gitignore: None,
            manifest_entries: Vec::new(),
            coupling_store: None,
            local_empty_reason: std::sync::Arc::new(parking_lot::RwLock::new(None)),
            indexed_root: None,
        };

        SymForgeServer::new(
            crate::live_index::SharedIndexHandle::shared(index),
            "prompt_project".to_string(),
            Arc::new(Mutex::new(WatcherInfo::default())),
            None,
            None,
        )
    }

    #[test]
    fn test_prompt_router_lists_expected_prompts() {
        let server = make_server();
        let prompts = server.prompt_router.list_all();
        let by_name: HashMap<&str, &rmcp::model::Prompt> =
            prompts.iter().map(|p| (p.name.as_str(), p)).collect();

        // All eight prompts are advertised. 009 US3 (T024): symforge-admin is the
        // universal in-harness affordance.
        for name in [
            "symforge-review",
            "symforge-architecture",
            "symforge-triage",
            "symforge-onboard",
            "symforge-refactor",
            "symforge-debug",
            "symforge-knowledge-hygiene",
            "symforge-admin",
        ] {
            assert!(by_name.contains_key(name), "missing prompt: {name}");
        }

        // Regression guard for the prompts/list `description` each client renders
        // in its slash-command menu. A client once showed `symforge-architecture`
        // mangled ("using Synts: area)"); the SOURCE string was intact, so the
        // mangling was client-side truncation. This pins every source description
        // exactly, so a genuine source-side corruption would fail here.
        let expect_desc = |name: &str, desc: &str| {
            assert_eq!(
                by_name[name].description.as_deref(),
                Some(desc),
                "list description drift for {name}"
            );
        };
        expect_desc(
            "symforge-review",
            "Generate a code review plan using SymForge context surfaces.",
        );
        expect_desc(
            "symforge-architecture",
            "Generate an architecture mapping plan using SymForge repo context.",
        );
        expect_desc(
            "symforge-triage",
            "Generate a debugging and failure-triage plan using SymForge state.",
        );
        expect_desc(
            "symforge-onboard",
            "Generate a codebase onboarding plan using SymForge for guided exploration.",
        );
        expect_desc(
            "symforge-refactor",
            "Generate a refactoring plan with impact analysis using SymForge.",
        );
        expect_desc(
            "symforge-debug",
            "Generate a detailed debugging plan using SymForge call tracing and change analysis.",
        );
        expect_desc(
            "symforge-knowledge-hygiene",
            "Review repository knowledge evidence and prepare advisory remediation proposals without mutating files.",
        );
        expect_desc(
            "symforge-admin",
            "Return the running SymForge operator dashboard URL, or guidance to start it.",
        );

        // Declared arguments (prompts/list) must match the input structs,
        // including which are optional. rmcp derives `required` from the schema:
        // `Option<_>` fields are not required.
        let args = |name: &str| -> HashMap<String, bool> {
            by_name[name]
                .arguments
                .as_ref()
                .map(|list| {
                    list.iter()
                        .map(|a| (a.name.clone(), a.required.unwrap_or(false)))
                        .collect()
                })
                .unwrap_or_default()
        };
        assert_eq!(
            args("symforge-review"),
            HashMap::from([("path".into(), false), ("focus".into(), false)]),
        );
        assert_eq!(
            args("symforge-architecture"),
            HashMap::from([("area".into(), false)]),
        );
        assert_eq!(
            args("symforge-triage"),
            HashMap::from([("symptom".into(), true), ("path".into(), false)]),
        );
        assert_eq!(
            args("symforge-onboard"),
            HashMap::from([("area".into(), false)]),
        );
        assert_eq!(
            args("symforge-refactor"),
            HashMap::from([("goal".into(), true), ("target".into(), false)]),
        );
        assert_eq!(
            args("symforge-debug"),
            HashMap::from([("error".into(), true), ("path".into(), false)]),
        );
        assert_eq!(
            args("symforge-knowledge-hygiene"),
            HashMap::from([("path_prefix".into(), false)]),
        );
        // AdminPromptInput has no fields -> no declared arguments.
        assert!(args("symforge-admin").is_empty());
    }

    #[test]
    fn knowledge_hygiene_prompt_pins_advisory_flow_and_rejects_sensitive_scope_without_echo() {
        let instructions =
            build_knowledge_hygiene_instructions("prompt_project", Some("docs/design"));
        for required in [
            "review_knowledge(mode=\"summary\"",
            "review_knowledge(mode=\"remediation\")",
            "mode=\"document\"",
            "STOP for explicit user approval",
            "curate_knowledge preview",
            "prompt cannot approve",
            "never moves, edits, or deletes",
        ] {
            assert!(
                instructions.contains(required),
                "missing hygiene workflow contract {required}: {instructions}"
            );
        }

        let canary = ["runtime", "-", "prompt", "-", "canary"].concat();
        let sensitive_scope = format!("docs/token={canary}");
        let rejected =
            build_knowledge_hygiene_instructions("prompt_project", Some(&sensitive_scope));
        assert!(rejected.contains("rejected by repository safety policy"));
        assert!(!rejected.contains(&canary), "sensitive scope must not echo");
        assert!(
            !rejected.contains("review_knowledge") && !rejected.contains("curate_knowledge"),
            "a rejected prompt must not create a review or proposal workflow"
        );
    }

    #[test]
    fn resolve_running_dashboard_url_in_is_none_without_a_profile() {
        // Isolation contract: resolution reads the profile from the control-state
        // dir it is GIVEN. Previously it reached for the process-global
        // placement (a OnceLock), so a sibling test that started a real operator
        // server made this path resolve a URL and the no-server guidance test
        // below failed purely by execution order.
        let dir = tempfile::tempdir().expect("temp control state");
        let control = crate::domain::ControlStateDir::new(dir.path().join("control"));
        assert_eq!(
            resolve_running_dashboard_url_in(None, &control),
            None,
            "an empty control-state dir has no profile, so no dashboard resolves"
        );
    }

    #[test]
    fn test_admin_prompt_returns_guidance_when_no_server() {
        // No profile in this isolated control-state dir, so nothing resolves: the
        // prompt body must be the honest "run `symforge admin`" guidance rather
        // than a fabricated URL (URL-or-guidance, D2).
        let dir = tempfile::tempdir().expect("temp control state");
        let control = crate::domain::ControlStateDir::new(dir.path().join("control"));
        let dashboard_url = resolve_running_dashboard_url_in(None, &control);
        assert_eq!(dashboard_url, None, "no profile must resolve no dashboard");
        let text = build_admin_instructions("prompt_project", dashboard_url.as_deref());

        // Guidance points at the CLI verb that actually starts the server.
        assert!(
            text.contains("symforge admin"),
            "no-server guidance must name the `symforge admin` verb: {text}"
        );
        assert!(
            text.contains("No operator dashboard is currently running"),
            "must honestly report nothing is running: {text}"
        );
        // D21: a fresh `symforge admin` stays in the foreground (does not fire-and-
        // return); the guidance must describe the foreground-serving behavior, while
        // the reuse path returns immediately.
        assert!(
            text.contains("stays in the foreground"),
            "guidance must describe foreground serving on fresh start (D21): {text}"
        );
        assert!(
            text.contains("returns immediately"),
            "guidance must state the reuse path returns immediately: {text}"
        );
    }

    #[tokio::test]
    async fn test_admin_prompt_returns_a_text_message() {
        // End-to-end wiring: the handler resolves off the async runtime and always
        // returns a body. Whether THIS machine has an operator server running is
        // ambient state, so assert only what holds either way — the URL-vs-guidance
        // split is covered against an isolated control-state dir above.
        let server = make_server();
        let result = server
            .admin_prompt(Parameters(AdminPromptInput::default()))
            .await;

        let text = result
            .messages
            .iter()
            .find_map(|message| match &message.content {
                rmcp::model::ContentBlock::Text(content) => Some(content.text.clone()),
                _ => None,
            })
            .expect("admin prompt returns a text message");
        assert!(
            text.contains("SymForge Operator Dashboard"),
            "every admin prompt body carries the dashboard heading: {text}"
        );
    }

    #[test]
    fn test_admin_instructions_report_running_url_when_present() {
        // When a dashboard URL is resolved, the prompt body surfaces exactly that
        // URL (the reuse path) and does not fall back to guidance.
        let url = "http://127.0.0.1:8787/admin";
        let body = build_admin_instructions("prompt_project", Some(url));
        assert!(
            body.contains(url),
            "running body must include the URL: {body}"
        );
        assert!(
            body.contains("dashboard is running"),
            "running body must say the dashboard is up: {body}"
        );
    }

    #[tokio::test]
    async fn test_code_review_prompt_includes_resource_links() {
        let server = make_server();
        let result = server
            .code_review_prompt(Parameters(CodeReviewPromptInput {
                path: Some("src/lib.rs".to_string()),
                focus: Some("dependency risks".to_string()),
            }))
            .await;

        assert!(
            result.messages.iter().any(|message| matches!(
                &message.content,
                rmcp::model::ContentBlock::ResourceLink(link)
                    if link.uri == REPO_HEALTH_URI
            )),
            "symforge-review prompt should link repo health"
        );
        assert!(
            result.messages.iter().any(|message| matches!(
                &message.content,
                rmcp::model::ContentBlock::ResourceLink(link)
                    if link.uri.contains("symforge://file/context")
            )),
            "symforge-review prompt should link file context"
        );
    }

    #[test]
    fn test_onboard_instructions_embed_orientation_doctrine() {
        let body = build_onboard_instructions("prompt_project", None);
        // Statement 1: the map orients; the tools prove.
        assert!(
            body.contains("map orients"),
            "onboarding instructions must embed the 'map orients' doctrine: {body}"
        );
        // Statement 2: absence from the map is not absence from the repo.
        assert!(
            body.contains("not absence from the repo"),
            "onboarding instructions must embed the 'not absence' doctrine: {body}"
        );
        assert!(
            body.contains("search_symbols") && body.contains("search_text"),
            "onboarding doctrine must point at search_symbols / search_text: {body}"
        );
    }

    #[test]
    fn test_architecture_map_instructions_embed_orientation_doctrine() {
        let body = build_architecture_map_instructions("prompt_project", None);
        // Statement 1: the map orients; the tools prove.
        assert!(
            body.contains("map orients"),
            "architecture instructions must embed the 'map orients' doctrine: {body}"
        );
        // Statement 2: absence from the map is not absence from the repo.
        assert!(
            body.contains("not absence from the repo"),
            "architecture instructions must embed the 'not absence' doctrine: {body}"
        );
        assert!(
            body.contains("search_symbols") && body.contains("search_text"),
            "architecture doctrine must point at search_symbols / search_text: {body}"
        );
    }

    #[test]
    fn test_tool_driven_instructions_are_surface_aware_and_current() {
        let review = build_code_review_instructions(
            "p",
            &CodeReviewPromptInput {
                path: None,
                focus: None,
            },
        );
        let architecture = build_architecture_map_instructions("p", None);
        let triage = build_failure_triage_instructions(
            "p",
            &FailureTriagePromptInput {
                symptom: "boom".to_string(),
                path: None,
            },
        );
        let onboard = build_onboard_instructions("p", None);
        let refactor = build_refactor_instructions(
            "p",
            &RefactorPromptInput {
                goal: "extract validation".to_string(),
                target: None,
            },
        );
        let debug = build_debug_instructions(
            "p",
            &DebugPromptInput {
                error: "panic".to_string(),
                path: None,
            },
        );

        // Every tool-driven body carries the single upfront surface-mapping note so a
        // compact-surface agent knows the step tool names are full-surface spellings
        // and how to route them through `symforge`/`symforge_edit`.
        for (label, body) in [
            ("review", &review),
            ("architecture", &architecture),
            ("triage", &triage),
            ("onboard", &onboard),
            ("refactor", &refactor),
            ("debug", &debug),
        ] {
            assert!(
                body.contains("Surface note:") && body.contains("`symforge_edit`"),
                "{label} body must embed the surface-mapping note: {body}"
            );
        }

        // Current-reality capability claims land only where they help the plan.
        assert!(
            review.contains("detect_impact") && review.contains("caps at 200"),
            "review must mention detect_impact + the what_changed 200 cap: {review}"
        );
        assert!(
            triage.contains("detect_impact") && triage.contains("caps at 200"),
            "triage must mention detect_impact + the what_changed 200 cap: {triage}"
        );
        assert!(
            refactor.contains("detect_impact"),
            "refactor verify step must mention detect_impact: {refactor}"
        );
        assert!(
            debug.contains("get_symbol(name=")
                && debug.contains("detect_impact")
                && debug.contains("caps at 200"),
            "debug must use name-only get_symbol + detect_impact + the 200 cap: {debug}"
        );
    }
}