context-bar-core 0.6.1

Engine for context-bar: AI coding-agent usage, rolling quota windows, and API-equivalent cost estimation from local transcripts.
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
use std::{
    collections::BTreeMap,
    fs,
    path::{Path, PathBuf},
    time::SystemTime,
};

use serde::Serialize;
use time::{OffsetDateTime, format_description::well_known::Rfc3339};
#[cfg(target_arch = "wasm32")]
use zed_extension_api as zed;

use crate::{
    git_signal::{ChangeSummary, CommitSummary, GitSignals},
    time_windows::{NOW_WINDOW, SESSION_WINDOW, WEEK_WINDOW},
    usage_signal::UsageSnapshot,
};
#[cfg(target_arch = "wasm32")]
use crate::{git_signal, usage_signal};

#[derive(Clone, Debug, Serialize)]
pub struct TouchedFile {
    pub path: String,
    pub modified_at: String,
}

#[derive(Clone, Debug, Serialize)]
pub struct WindowSummary {
    pub label: String,
    pub top_files: Vec<String>,
    pub focus_areas: Vec<String>,
    pub themes: Vec<String>,
    pub resume_hint: String,
    pub commit_refs: Vec<CommitSummary>,
    pub change_summary: Vec<ChangeSummary>,
}

#[derive(Clone, Debug, Serialize)]
pub struct AssistantMemory {
    pub latest_summary: String,
    pub thread_refs: Vec<String>,
}

#[derive(Clone, Debug, Serialize)]
pub struct ContextSnapshot {
    pub worktree_root: String,
    pub branch: String,
    pub updated_at: String,
    pub touched_files: Vec<TouchedFile>,
    pub now: WindowSummary,
    pub session: WindowSummary,
    pub week: WindowSummary,
    pub assistant_memory: AssistantMemory,
    #[serde(default)]
    pub usage: UsageSnapshot,
}

#[derive(Clone, Debug)]
pub struct FileObservation {
    pub relative_path: String,
    pub modified_at: SystemTime,
}

pub struct ContextEngine;

impl ContextEngine {
    /// Convenience entry point bound to Zed's `Worktree`. The actual snapshot
    /// assembly is delegated to [`assemble`] so non-Zed integration layers
    /// (e.g. a future MCP server or ACP bridge) can drive the engine with
    /// their own signal sources without redesign.
    #[cfg(target_arch = "wasm32")]
    pub fn generate(worktree: &zed::Worktree) -> Result<ContextSnapshot, String> {
        let root = PathBuf::from(worktree.root_path());
        let git = git_signal::collect(worktree)?;
        let touched_files = collect_file_observations(&root)?;
        let usage = usage_signal::collect(worktree);
        let mut snapshot = assemble(root, git, touched_files)?;
        snapshot.usage = usage;
        Ok(snapshot)
    }
}

/// Assemble a [`ContextSnapshot`] from already-collected signals. This is the
/// stable seam any future integration layer (HUD, ACP, MCP) plugs into.
pub fn assemble(
    root: PathBuf,
    git: GitSignals,
    touched_files: Vec<FileObservation>,
) -> Result<ContextSnapshot, String> {
    {
        let updated_at = timestamp(SystemTime::now())?;

        let now_files = filter_files_for_window(&touched_files, NOW_WINDOW.duration);
        let session_files = filter_files_for_window(&touched_files, SESSION_WINDOW.duration);
        let week_files = filter_files_for_window(&touched_files, WEEK_WINDOW.duration);

        let snapshot = ContextSnapshot {
            worktree_root: root.display().to_string(),
            branch: git.branch.clone(),
            updated_at,
            touched_files: touched_files
                .iter()
                .take(25)
                .map(|file| {
                    Ok(TouchedFile {
                        path: file.relative_path.clone(),
                        modified_at: timestamp(file.modified_at)?,
                    })
                })
                .collect::<Result<Vec<_>, String>>()?,
            now: summarize_window(NOW_WINDOW.label, &git, &now_files, NOW_WINDOW.duration, true),
            session: summarize_window(SESSION_WINDOW.label, &git, &session_files, SESSION_WINDOW.duration, false),
            week: summarize_window(WEEK_WINDOW.label, &git, &week_files, WEEK_WINDOW.duration, false),
            assistant_memory: AssistantMemory {
                latest_summary: build_assistant_memory(&git, &session_files),
                thread_refs: Vec::new(),
            },
            usage: UsageSnapshot::default(),
        };

        Ok(snapshot)
    }
}

pub fn collect_files(root: &Path) -> Result<Vec<FileObservation>, String> {
    collect_file_observations(root)
}

pub fn render_window_markdown(snapshot: &ContextSnapshot, window: &str) -> String {
    let summary = match window {
        "now" => &snapshot.now,
        "session" => &snapshot.session,
        "week" => &snapshot.week,
        _ => &snapshot.now,
    };

    let mut lines = vec![
        format!("# {} brief", summary.label),
        String::new(),
        format!("- worktree: {}", snapshot.worktree_root),
        format!("- branch: {}", snapshot.branch),
        format!("- updated_at: {}", snapshot.updated_at),
        String::new(),
        "## Resume hint".to_string(),
        summary.resume_hint.clone(),
        String::new(),
    ];

    if !summary.top_files.is_empty() {
        lines.push("## Top files".to_string());
        lines.extend(summary.top_files.iter().map(|path| format!("- {path}")));
        lines.push(String::new());
    }

    if !summary.focus_areas.is_empty() {
        lines.push("## Focus areas".to_string());
        lines.extend(summary.focus_areas.iter().map(|path| format!("- {path}")));
        lines.push(String::new());
    }

    if !summary.themes.is_empty() {
        lines.push("## Themes".to_string());
        lines.extend(summary.themes.iter().map(|theme| format!("- {theme}")));
        lines.push(String::new());
    }

    if !summary.commit_refs.is_empty() {
        lines.push("## Recent commits".to_string());
        lines.extend(
            summary
                .commit_refs
                .iter()
                .map(|commit| format!("- {} {}", commit.sha, commit.subject)),
        );
        lines.push(String::new());
    }

    if !summary.change_summary.is_empty() {
        lines.push("## Local changes".to_string());
        lines.extend(
            summary
                .change_summary
                .iter()
                .map(|change| format!("- {} {}", change.code, change.path)),
        );
    }

    lines.join("\n").trim().to_string() + "\n"
}

fn summarize_window(
    label: &str,
    git: &GitSignals,
    files: &[FileObservation],
    window: std::time::Duration,
    include_local_changes: bool,
) -> WindowSummary {
    let top_files = files
        .iter()
        .take(6)
        .map(|file| file.relative_path.clone())
        .collect::<Vec<_>>();

    let windowed_commits = filter_commits_for_window(&git.recent_commits, window);
    let focus_areas = top_directories(files, 4);
    let themes = derive_themes(files, &windowed_commits, 4);
    let change_summary = if include_local_changes {
        combine_change_summary(git, 8)
    } else {
        Vec::new()
    };

    let resume_hint = match label {
        "now" => {
            if top_files.is_empty() {
                format!("No files changed in the last {} minutes.", NOW_WINDOW.duration.as_secs() / 60)
            } else {
                format!(
                    "Continue in {} on branch {}. Local changes are concentrated in {}.",
                    top_files[0],
                    git.branch,
                    focus_areas
                        .first()
                        .cloned()
                        .unwrap_or_else(|| "the worktree root".to_string())
                )
            }
        }
        "session" => format!(
            "Session focus is {} with {} recent file touches.",
            focus_areas
                .first()
                .cloned()
                .unwrap_or_else(|| "mixed project areas".to_string()),
            files.len()
        ),
        _ => format!(
            "Weekly pattern points to {} and {} recent commits on {}.",
            focus_areas
                .first()
                .cloned()
                .unwrap_or_else(|| "mixed project areas".to_string()),
            windowed_commits.len(),
            git.branch
        ),
    };

    WindowSummary {
        label: label.to_string(),
        top_files,
        focus_areas,
        themes,
        resume_hint,
        commit_refs: windowed_commits.into_iter().take(6).collect(),
        change_summary,
    }
}

fn filter_commits_for_window(
    commits: &[CommitSummary],
    window: std::time::Duration,
) -> Vec<CommitSummary> {
    let now = SystemTime::now();
    commits
        .iter()
        .filter(|commit| match commit.committed_at_system {
            Some(time) => now
                .duration_since(time)
                .map(|age| age <= window)
                .unwrap_or(true),
            // If timestamp is missing, fall back to inclusion only for the
            // widest window so older parsing paths still produce output.
            None => window >= WEEK_WINDOW.duration,
        })
        .cloned()
        .collect()
}

fn build_assistant_memory(git: &GitSignals, session_files: &[FileObservation]) -> String {
    let top_file = session_files
        .first()
        .map(|file| file.relative_path.as_str())
        .unwrap_or("no recent files");
    let commit = git
        .recent_commits
        .first()
        .map(|commit| commit.subject.as_str())
        .unwrap_or("no recent commits");

    format!(
        "Current branch is {}. Session focus is {}. Latest visible commit theme: {}.",
        git.branch, top_file, commit
    )
}

fn combine_change_summary(git: &GitSignals, limit: usize) -> Vec<ChangeSummary> {
    git.staged_changes
        .iter()
        .chain(git.unstaged_changes.iter())
        .take(limit)
        .cloned()
        .collect()
}

fn derive_themes(
    files: &[FileObservation],
    commits: &[CommitSummary],
    limit: usize,
) -> Vec<String> {
    let mut themes = Vec::new();

    for area in top_directories(files, limit) {
        if area != "." {
            themes.push(format!("focus on {area}"));
        }
    }

    for commit in commits.iter().take(limit) {
        if themes.len() >= limit {
            break;
        }
        themes.push(commit.subject.clone());
    }

    themes
}

fn top_directories(files: &[FileObservation], limit: usize) -> Vec<String> {
    let mut counts: BTreeMap<String, usize> = BTreeMap::new();

    for file in files {
        let area = Path::new(&file.relative_path)
            .parent()
            .map(|path| {
                let value = path.to_string_lossy().to_string();
                if value.is_empty() { ".".to_string() } else { value }
            })
            .unwrap_or_else(|| ".".to_string());
        if is_noise_area(&area) {
            continue;
        }
        *counts.entry(area).or_default() += 1;
    }

    let mut entries = counts.into_iter().collect::<Vec<_>>();
    entries.sort_by(|left, right| right.1.cmp(&left.1).then_with(|| left.0.cmp(&right.0)));

    entries.into_iter().take(limit).map(|(path, _)| path).collect()
}

fn is_noise_area(area: &str) -> bool {
    matches!(
        area,
        "." | ".git" | ".tmp" | ".context-bar" | "target" | "node_modules"
    )
}

fn filter_files_for_window(
    files: &[FileObservation],
    duration: std::time::Duration,
) -> Vec<FileObservation> {
    let now = SystemTime::now();
    files.iter()
        .filter(|file| {
            now.duration_since(file.modified_at)
                .map(|age| age <= duration)
                .unwrap_or(false)
        })
        .cloned()
        .collect()
}

/// Maximum directory recursion depth for `collect_dir`. Prevents stack
/// overflow / runaway walks on pathological worktrees (deep symlink chains,
/// generated monorepos, etc.). 12 is generous for real projects.
const MAX_COLLECT_DEPTH: usize = 12;

fn collect_file_observations(root: &Path) -> Result<Vec<FileObservation>, String> {
    let mut observations = Vec::new();
    collect_dir(root, root, &mut observations, 0)?;
    observations.sort_by(|left, right| right.modified_at.cmp(&left.modified_at));
    Ok(observations)
}

fn collect_dir(
    root: &Path,
    current: &Path,
    observations: &mut Vec<FileObservation>,
    depth: usize,
) -> Result<(), String> {
    if depth >= MAX_COLLECT_DEPTH {
        return Ok(());
    }
    for entry in fs::read_dir(current)
        .map_err(|error| format!("failed to read directory {}: {error}", current.display()))?
    {
        let entry = entry.map_err(|error| format!("failed to inspect directory entry: {error}"))?;
        let path = entry.path();
        let file_type = entry
            .file_type()
            .map_err(|error| format!("failed to read file type for {}: {error}", path.display()))?;

        if file_type.is_symlink() {
            continue;
        }

        if file_type.is_dir() {
            if should_skip_dir(&path) {
                continue;
            }
            collect_dir(root, &path, observations, depth + 1)?;
            continue;
        }

        if !file_type.is_file() {
            continue;
        }

        if should_skip_file(&path) {
            continue;
        }

        let metadata = entry
            .metadata()
            .map_err(|error| format!("failed to read metadata for {}: {error}", path.display()))?;
        let modified_at = match metadata.modified() {
            Ok(modified_at) => modified_at,
            Err(_) => continue,
        };

        let relative_path = path
            .strip_prefix(root)
            .map_err(|error| format!("failed to compute relative path for {}: {error}", path.display()))?
            .to_string_lossy()
            .to_string();

        observations.push(FileObservation {
            relative_path,
            modified_at,
        });
    }

    Ok(())
}

fn should_skip_dir(path: &Path) -> bool {
    matches!(
        path.file_name().and_then(|value| value.to_str()),
        Some(".git" | "target" | ".context-bar" | "node_modules" | ".tmp")
    )
}

fn should_skip_file(path: &Path) -> bool {
    matches!(
        path.file_name().and_then(|value| value.to_str()),
        Some("extension.wasm" | "Cargo.lock")
    )
}

fn timestamp(time: SystemTime) -> Result<String, String> {
    let datetime = OffsetDateTime::from(time);
    datetime
        .format(&Rfc3339)
        .map_err(|error| format!("failed to format timestamp: {error}"))
}

#[cfg(test)]
mod tests {
    use super::{CommitSummary, FileObservation, derive_themes, top_directories};
    use std::time::SystemTime;

    #[test]
    fn picks_top_directories_from_recent_files() {
        let files = vec![
            FileObservation {
                relative_path: "src/lib.rs".to_string(),
                modified_at: SystemTime::now(),
            },
            FileObservation {
                relative_path: "src/context_engine.rs".to_string(),
                modified_at: SystemTime::now(),
            },
            FileObservation {
                relative_path: "docs/02-architecture.md".to_string(),
                modified_at: SystemTime::now(),
            },
        ];

        let areas = top_directories(&files, 2);
        assert_eq!(areas, vec!["src".to_string(), "docs".to_string()]);
    }

    #[test]
    fn derives_themes_from_files_and_commits() {
        let files = vec![FileObservation {
            relative_path: "src/lib.rs".to_string(),
            modified_at: SystemTime::now(),
        }];
        let commits = vec![CommitSummary {
            sha: "abc1234".to_string(),
            subject: "Add context engine".to_string(),
            committed_at: None,
            committed_at_system: None,
        }];

        let themes = derive_themes(&files, &commits, 4);
        assert!(themes.iter().any(|theme| theme.contains("src")));
        assert!(themes.iter().any(|theme| theme.contains("Add context engine")));
    }
}