symforge 10.1.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
pub mod governor;
pub mod handlers;
pub mod port_file;
pub mod router;
pub mod server;

pub use governor::RequestGovernor;
pub use server::spawn_sidecar;

use parking_lot::{Mutex, RwLock};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};

use serde::Serialize;

use crate::live_index::store::SharedIndex;

/// Handle returned by `spawn_sidecar`. Dropping this or sending on `shutdown_tx`
/// gracefully stops the background axum server and cleans up port/PID files.
///
/// Prefer `shutdown_and_join().await` over a bare `shutdown_tx.send(())`: the
/// helper awaits the server task's completion so the listener is fully dropped
/// before the caller proceeds. This matters in tests where rapid teardown +
/// rebind cycles must not race a still-open listener.
pub struct SidecarHandle {
    /// The ephemeral port the sidecar bound to.
    pub port: u16,
    /// Send `()` on this channel to initiate graceful shutdown.
    pub shutdown_tx: tokio::sync::oneshot::Sender<()>,
    /// Join handle for the spawned axum-serve task. Awaiting this after sending
    /// on `shutdown_tx` guarantees the listener has been dropped.
    pub server_join: tokio::task::JoinHandle<()>,
    /// Shared token stats for the sidecar session.
    /// Pass this `Arc` to `SymForgeServer::new()` so the health tool can report savings.
    pub token_stats: Arc<TokenStats>,
}

impl SidecarHandle {
    /// Signal graceful shutdown and await server-task completion.
    ///
    /// Consumes the handle; the listener and port/PID files are fully released
    /// by the time this returns. Tests should prefer this over a bare
    /// `shutdown_tx.send(())` followed by an arbitrary sleep.
    pub async fn shutdown_and_join(self) {
        let _ = self.shutdown_tx.send(());
        let _ = self.server_join.await;
    }
}

// ---------------------------------------------------------------------------
// TokenStats — per-session atomic counters for hook fire counts and token savings
// ---------------------------------------------------------------------------

/// Lightweight snapshot of all token stats counters. Returned by `/stats`.
#[derive(Debug, Clone, Serialize)]
pub struct StatsSnapshot {
    pub read_fires: usize,
    pub read_saved_tokens: u64,
    pub edit_fires: usize,
    pub edit_saved_tokens: u64,
    pub write_fires: usize,
    pub grep_fires: usize,
    pub grep_saved_tokens: u64,
}

/// In-memory atomic counters tracking hook fires and estimated token savings per hook type.
///
/// All counters start at zero; incremented by handlers on each successful response.
/// Token savings are estimated as `(file_bytes - output_bytes) / 4` (bytes-per-token heuristic).
pub struct TokenStats {
    pub read_fires: AtomicUsize,
    pub read_saved_tokens: AtomicU64,
    pub edit_fires: AtomicUsize,
    pub edit_saved_tokens: AtomicU64,
    /// Write fires track new-file indexing; no savings because it's additive context.
    pub write_fires: AtomicUsize,
    pub grep_fires: AtomicUsize,
    pub grep_saved_tokens: AtomicU64,
    /// Per-tool invocation counts since daemon start.
    pub tool_calls: Mutex<HashMap<String, usize>>,
    /// Per-tool token tracking: tool_name -> (tokens_served, tokens_saved).
    pub tool_token_details: Mutex<HashMap<String, (u64, u64)>>,
    /// Total tokens served across all tool calls this session.
    pub total_tokens_served: AtomicU64,
    /// Total estimated naive-equivalent tokens (what raw file reads would cost).
    pub total_tokens_naive: AtomicU64,
}

impl TokenStats {
    /// Create a new `Arc<TokenStats>` with all counters at zero.
    pub fn new() -> Arc<Self> {
        Arc::new(Self {
            read_fires: AtomicUsize::new(0),
            read_saved_tokens: AtomicU64::new(0),
            edit_fires: AtomicUsize::new(0),
            edit_saved_tokens: AtomicU64::new(0),
            write_fires: AtomicUsize::new(0),
            grep_fires: AtomicUsize::new(0),
            grep_saved_tokens: AtomicU64::new(0),
            tool_calls: Mutex::new(HashMap::new()),
            tool_token_details: Mutex::new(HashMap::new()),
            total_tokens_served: AtomicU64::new(0),
            total_tokens_naive: AtomicU64::new(0),
        })
    }

    /// Record a Read hook fire. Savings vs competent-manual windowed-read baseline.
    pub fn record_read(&self, file_bytes: u64, output_bytes: u64) {
        self.read_fires.fetch_add(1, Ordering::Relaxed);
        let saved = crate::protocol::format::saved_tokens_vs_competent_manual(
            output_bytes as usize,
            file_bytes as usize,
        );
        self.read_saved_tokens.fetch_add(saved, Ordering::Relaxed);
    }

    /// Record an Edit hook fire. Savings vs competent-manual windowed-read baseline.
    pub fn record_edit(&self, file_bytes: u64, output_bytes: u64) {
        self.edit_fires.fetch_add(1, Ordering::Relaxed);
        let saved = crate::protocol::format::saved_tokens_vs_competent_manual(
            output_bytes as usize,
            file_bytes as usize,
        );
        self.edit_saved_tokens.fetch_add(saved, Ordering::Relaxed);
    }

    /// Record a Write hook fire (new-file indexing). No savings — additive context.
    pub fn record_write(&self) {
        self.write_fires.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a Grep hook fire. Savings vs competent-manual grep+window baseline.
    pub fn record_grep(&self, _file_bytes: u64, output_bytes: u64) {
        self.grep_fires.fetch_add(1, Ordering::Relaxed);
        let baseline_chars =
            crate::protocol::format::estimate_listing_baseline_chars(output_bytes as usize)
                .max(output_bytes as usize);
        let saved = crate::protocol::format::saved_tokens_vs_competent_manual(
            output_bytes as usize,
            baseline_chars,
        );
        self.grep_saved_tokens.fetch_add(saved, Ordering::Relaxed);
    }

    /// Record a single MCP tool invocation by name.
    pub fn record_tool_call(&self, name: &str) {
        let mut map = self.tool_calls.lock();
        *map.entry(name.to_string()).or_insert(0) += 1;
    }

    /// Record per-tool token details: tokens served and tokens saved.
    pub fn record_tool_tokens(&self, tool_name: &str, tokens_served: u64, tokens_saved: u64) {
        let mut map = self.tool_token_details.lock();
        let entry = map.entry(tool_name.to_string()).or_insert((0, 0));
        entry.0 += tokens_served;
        entry.1 += tokens_saved;
        self.total_tokens_served
            .fetch_add(tokens_served, Ordering::Relaxed);
        self.total_tokens_naive
            .fetch_add(tokens_served + tokens_saved, Ordering::Relaxed);
    }

    /// Return per-tool token details sorted by tokens saved descending.
    pub fn tool_token_details(&self) -> Vec<(String, u64, u64)> {
        let map = self.tool_token_details.lock();
        let mut details: Vec<(String, u64, u64)> = map
            .iter()
            .map(|(k, (served, saved))| (k.clone(), *served, *saved))
            .collect();
        details.sort_by(|a, b| b.2.cmp(&a.2).then_with(|| a.0.cmp(&b.0)));
        details
    }

    /// Return per-tool invocation counts sorted by count descending, then name ascending.
    pub fn tool_call_counts(&self) -> Vec<(String, usize)> {
        let map = self.tool_calls.lock();
        let mut counts: Vec<(String, usize)> = map.iter().map(|(k, v)| (k.clone(), *v)).collect();
        counts.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(&b.0)));
        counts
    }

    /// Read all counter values atomically (Relaxed ordering — display only).
    pub fn summary(&self) -> StatsSnapshot {
        StatsSnapshot {
            read_fires: self.read_fires.load(Ordering::Relaxed),
            read_saved_tokens: self.read_saved_tokens.load(Ordering::Relaxed),
            edit_fires: self.edit_fires.load(Ordering::Relaxed),
            edit_saved_tokens: self.edit_saved_tokens.load(Ordering::Relaxed),
            write_fires: self.write_fires.load(Ordering::Relaxed),
            grep_fires: self.grep_fires.load(Ordering::Relaxed),
            grep_saved_tokens: self.grep_saved_tokens.load(Ordering::Relaxed),
        }
    }
}

// ---------------------------------------------------------------------------
// SymbolSnapshot — lightweight copy of a symbol for pre/post diff in impact handler
// ---------------------------------------------------------------------------

/// Lightweight snapshot of a symbol used to detect pre/post-edit changes.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SymbolSnapshot {
    pub name: String,
    pub kind: String,
    pub line_range: (u32, u32),
    pub byte_range: (u32, u32),
}

/// Public cache shape retained for sidecar embedders.
pub type SymbolSnapshotCache = HashMap<String, Vec<SymbolSnapshot>>;

// ---------------------------------------------------------------------------
// SidecarState — bundles index + stats + symbol cache for all handlers
// ---------------------------------------------------------------------------

/// Axum state type bundling the shared index, token statistics, and pre-edit symbol cache.
///
/// Passed to every handler via `State<SidecarState>`. Replaces bare `SharedIndex` as the
/// axum state type in Plan 06-01.
#[derive(Clone)]
pub struct SidecarState {
    pub index: SharedIndex,
    pub token_stats: Arc<TokenStats>,
    /// Canonical project root for file-system reads during impact analysis.
    /// `None` falls back to process cwd for local test setups.
    pub repo_root: Option<PathBuf>,
    /// Per-file symbol snapshot cache for impact diff.
    /// Key: relative file path. Value: symbol list captured after the last impact.
    pub symbol_cache: Arc<RwLock<SymbolSnapshotCache>>,
}

// ---------------------------------------------------------------------------
// build_with_budget — truncate a list of lines at a byte boundary
// ---------------------------------------------------------------------------

/// Join `items` with newlines, stopping before exceeding `max_bytes`.
///
/// If `max_bytes` is 0, all items are returned without truncation.
///
/// Returns `(text, remaining_count)` where `remaining_count` is 0 when no
/// truncation occurred. A canonical truncation suffix is appended when
/// items were dropped.
pub fn build_with_budget(items: &[String], max_bytes: u64) -> (String, usize) {
    if max_bytes == 0 || items.is_empty() {
        return (items.join("\n"), 0);
    }

    let mut included = Vec::new();
    let mut used_bytes: u64 = 0;

    for (i, item) in items.iter().enumerate() {
        // Each item costs: len + 1 newline (except the last).
        let item_cost = item.len() as u64 + if i + 1 < items.len() { 1 } else { 0 };
        if used_bytes + item_cost > max_bytes && !included.is_empty() {
            // Would exceed budget — stop here.
            let remaining = items.len() - included.len();
            let mut text = included.join("\n");
            text.push_str(&budget_truncation_suffix(max_bytes, remaining));
            return (text, remaining);
        }
        used_bytes += item_cost;
        included.push(item.as_str());
    }

    // After the loop: if fewer items were included than available (e.g. because
    // the very first item exceeded max_bytes and forced inclusion while the rest
    // were silently dropped), always append the truncation suffix so callers
    // know output was cut short.
    if included.len() < items.len() {
        let remaining = items.len() - included.len();
        let mut text = included.join("\n");
        text.push_str(&budget_truncation_suffix(max_bytes, remaining));
        return (text, remaining);
    }

    (included.join("\n"), 0)
}

const CANONICAL_TRUNCATION_MARKER: &str = "[truncated]";
const APPROX_BYTES_PER_TOKEN: u64 = 4;

fn approx_tokens_from_bytes(bytes: u64) -> u64 {
    bytes.saturating_add(APPROX_BYTES_PER_TOKEN - 1) / APPROX_BYTES_PER_TOKEN
}

fn budget_truncation_suffix(max_bytes: u64, remaining: usize) -> String {
    let max_tokens = approx_tokens_from_bytes(max_bytes);
    format!(
        "\n{CANONICAL_TRUNCATION_MARKER} Truncated at ~{max_tokens} tokens. {remaining} additional output line(s) not shown."
    )
}

// ---------------------------------------------------------------------------
// Unit tests for Task 1
// ---------------------------------------------------------------------------

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

    // --- TokenStats ---

    #[test]
    fn test_token_stats_new_all_zeros() {
        let stats = TokenStats::new();
        let snap = stats.summary();
        assert_eq!(snap.read_fires, 0);
        assert_eq!(snap.read_saved_tokens, 0);
        assert_eq!(snap.edit_fires, 0);
        assert_eq!(snap.edit_saved_tokens, 0);
        assert_eq!(snap.write_fires, 0);
        assert_eq!(snap.grep_fires, 0);
        assert_eq!(snap.grep_saved_tokens, 0);
    }

    #[test]
    fn test_token_stats_record_read() {
        let stats = TokenStats::new();
        stats.record_read(1000, 200);
        let snap = stats.summary();
        assert_eq!(snap.read_fires, 1);
        // (1000 - 200) / 4 = 200
        assert_eq!(snap.read_saved_tokens, 200);
    }

    #[test]
    fn test_token_stats_record_edit() {
        let stats = TokenStats::new();
        stats.record_edit(800, 300);
        let snap = stats.summary();
        assert_eq!(snap.edit_fires, 1);
        // (800 - 300) / 4 = 125
        assert_eq!(snap.edit_saved_tokens, 125);
    }

    #[test]
    fn test_token_stats_record_write_no_savings() {
        let stats = TokenStats::new();
        stats.record_write();
        let snap = stats.summary();
        assert_eq!(snap.write_fires, 1);
        // No savings fields for write
    }

    #[test]
    fn test_token_stats_record_grep() {
        let stats = TokenStats::new();
        stats.record_grep(2000, 100);
        let snap = stats.summary();
        assert_eq!(snap.grep_fires, 1);
        let expected = crate::protocol::format::saved_tokens_vs_competent_manual(
            100,
            crate::protocol::format::estimate_listing_baseline_chars(100),
        );
        assert_eq!(snap.grep_saved_tokens, expected);
    }

    #[test]
    fn test_token_stats_records_tool_calls() {
        let stats = TokenStats::new();

        stats.record_tool_call("search_text");
        stats.record_tool_call("search_text");
        stats.record_tool_call("get_file_context");

        let counts = stats.tool_call_counts();

        // search_text has the highest count and should appear first.
        assert_eq!(counts[0], ("search_text".to_string(), 2));
        assert_eq!(counts[1], ("get_file_context".to_string(), 1));
        assert_eq!(counts.len(), 2);
    }

    #[test]
    fn test_token_stats_saturating_sub_no_underflow() {
        // output_bytes > file_bytes — should not underflow
        let stats = TokenStats::new();
        stats.record_read(100, 500);
        let snap = stats.summary();
        assert_eq!(
            snap.read_saved_tokens, 0,
            "saturating_sub prevents underflow"
        );
    }

    #[test]
    fn test_token_stats_accumulates_multiple_fires() {
        let stats = TokenStats::new();
        stats.record_read(1000, 200);
        stats.record_read(800, 400);
        let snap = stats.summary();
        assert_eq!(snap.read_fires, 2);
        // 200 + 100 = 300
        assert_eq!(snap.read_saved_tokens, 300);
    }

    // --- build_with_budget ---

    #[test]
    fn test_build_with_budget_no_truncation_when_fits() {
        let items: Vec<String> = vec![
            "line1".to_string(),
            "line2".to_string(),
            "line3".to_string(),
        ];
        let (text, remaining) = build_with_budget(&items, 1000);
        assert_eq!(text, "line1\nline2\nline3");
        assert_eq!(remaining, 0, "no truncation when all items fit");
    }

    #[test]
    fn test_build_with_budget_truncates_at_logical_boundary() {
        // "line1\nline2\nline3" = 5+1+5+1+5 = 17 bytes
        // max_bytes=12 means line1(5+1=6) + line2(5+1=6) = 12 bytes fits,
        // but adding line3(5) would be 12+5=17 > 12
        let items: Vec<String> = vec![
            "line1".to_string(),
            "line2".to_string(),
            "line3".to_string(),
        ];
        let (text, remaining) = build_with_budget(&items, 12);
        assert!(text.contains("line1"), "line1 should be included");
        assert!(text.contains("line2"), "line2 should be included");
        assert!(text.contains("truncated"), "should have truncation suffix");
        assert_eq!(remaining, 1, "1 item was truncated");
    }

    #[test]
    fn test_build_with_budget_zero_means_unlimited() {
        let items: Vec<String> = vec!["a".to_string(), "b".to_string(), "c".to_string()];
        let (text, remaining) = build_with_budget(&items, 0);
        assert_eq!(text, "a\nb\nc");
        assert_eq!(remaining, 0);
    }

    #[test]
    fn test_build_with_budget_empty_items() {
        let items: Vec<String> = vec![];
        let (text, remaining) = build_with_budget(&items, 100);
        assert_eq!(text, "");
        assert_eq!(remaining, 0);
    }

    #[test]
    fn test_build_with_budget_truncation_suffix_format() {
        let items: Vec<String> = vec![
            "line1".to_string(),
            "line2".to_string(),
            "line3".to_string(),
            "line4".to_string(),
        ];
        let (text, remaining) = build_with_budget(&items, 12);
        assert!(
            text.ends_with(
                "[truncated] Truncated at ~3 tokens. 2 additional output line(s) not shown."
            ),
            "suffix should use canonical truncation marker and mention 2 remaining items, got: {text}"
        );
        assert_eq!(remaining, 2);
    }

    // --- SidecarState construction ---

    #[test]
    fn test_sidecar_state_constructs() {
        use crate::live_index::store::{CircuitBreakerState, LiveIndex};
        use parking_lot::RwLock;
        use std::collections::HashMap;
        use std::sync::Arc;
        use std::time::{Duration, Instant, SystemTime};

        let index = crate::live_index::SharedIndexHandle::shared(LiveIndex {
            files: HashMap::new(),
            loaded_at: Instant::now(),
            loaded_at_system: SystemTime::now(),
            load_duration: Duration::ZERO,
            cb_state: CircuitBreakerState::new(0.20),
            is_empty: true,
            load_source: crate::live_index::store::IndexLoadSource::EmptyBootstrap,
            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,
        });

        let state = SidecarState {
            index,
            token_stats: TokenStats::new(),
            repo_root: None,
            symbol_cache: Arc::new(RwLock::new(HashMap::new())),
        };

        // Clone must work (derived Clone)
        let _cloned = state.clone();
    }
}