git-meta-cli 0.1.0

Command-line tool for structured Git metadata (get/set, serialize, materialize, push/pull). Installs the `git-meta` binary.
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
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::io::{BufRead, BufReader, Seek, SeekFrom};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::mpsc;
use std::time::{Duration, Instant};

use anyhow::{bail, Context, Result};
use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher};
use time::OffsetDateTime;

use crate::context::CommandContext;
use git_meta_lib::types::{Target, TargetType, ValueType};
use git_meta_lib::Session;

// ANSI colors
const BOLD: &str = "\x1b[1m";
const DIM: &str = "\x1b[2m";
const GREEN: &str = "\x1b[32m";
const YELLOW: &str = "\x1b[33m";
const MAGENTA: &str = "\x1b[35m";
const CYAN: &str = "\x1b[36m";
const RED: &str = "\x1b[31m";
const RESET: &str = "\x1b[0m";

pub fn run(agent: &str, debounce_secs: u64) -> Result<()> {
    let ctx = CommandContext::open(None)?;
    let repo = ctx.session.repo();
    let workdir = repo
        .workdir()
        .context("bare repo not supported")?
        .to_path_buf();
    let workdir = workdir.canonicalize()?;

    let transcripts_dir = resolve_transcripts_dir(agent, &workdir)?;
    let git_dir = repo.path().to_path_buf();

    eprintln!(
        "{}{}[watch]{} Watching {} transcripts: {}",
        BOLD,
        CYAN,
        RESET,
        agent,
        transcripts_dir.display()
    );
    eprintln!(
        "{}{}[watch]{} Watching git refs: {}",
        BOLD,
        CYAN,
        RESET,
        git_dir.join("refs").display()
    );
    eprintln!("{BOLD}{CYAN}[watch]{RESET} Debounce: {debounce_secs}s -- press Ctrl+C to stop\n");

    // Set up file watcher
    let (tx, rx) = mpsc::channel();
    let tx2 = tx.clone();
    let mut watcher = RecommendedWatcher::new(
        move |res| {
            let _ = tx2.send(res);
        },
        Config::default(),
    )
    .context("failed to create file watcher")?;

    // Watch transcripts directory
    if transcripts_dir.exists() {
        watcher.watch(&transcripts_dir, RecursiveMode::Recursive)?;
    } else {
        eprintln!("  {YELLOW}[warn]{RESET} Transcripts dir does not exist yet, watching parent...");
        // Watch parent so we catch when the directory is created
        if let Some(parent) = transcripts_dir.parent() {
            if parent.exists() {
                watcher.watch(parent, RecursiveMode::NonRecursive)?;
            }
        }
    }

    // Watch .git/HEAD and refs for commit detection
    let head_path = git_dir.join("HEAD");
    if head_path.exists() {
        watcher.watch(&head_path, RecursiveMode::NonRecursive)?;
    }
    let refs_dir = git_dir.join("refs");
    if refs_dir.exists() {
        watcher.watch(&refs_dir, RecursiveMode::Recursive)?;
    }

    // Initialize state
    let mut state = WatchState::new(&workdir, &transcripts_dir)?;

    // Scan existing transcript files so we only process new content
    state.init_file_positions()?;

    // Take initial snapshot of gitbutler status
    state.refresh_gitbutler_status()?;

    // Event loop
    loop {
        match rx.recv_timeout(Duration::from_secs(1)) {
            Ok(Ok(event)) => {
                for path in &event.paths {
                    if path.extension().is_some_and(|e| e == "jsonl")
                        && path.starts_with(&transcripts_dir)
                    {
                        state.handle_transcript_change(path)?;
                    } else if path.starts_with(&git_dir) {
                        state.mark_git_dirty();
                    }
                }
            }
            Ok(Err(e)) => {
                eprintln!("{RED}[error]{RESET} Watch error: {e}");
            }
            Err(mpsc::RecvTimeoutError::Timeout) => {
                state.tick(debounce_secs)?;
            }
            Err(mpsc::RecvTimeoutError::Disconnected) => break,
        }
    }

    Ok(())
}

fn resolve_transcripts_dir(agent: &str, workdir: &Path) -> Result<PathBuf> {
    match agent {
        "claude" => {
            let home = std::env::var("HOME").context("HOME not set")?;
            // Claude uses the absolute path with / replaced by -
            let dir_name = workdir.to_string_lossy().replace('/', "-");
            Ok(PathBuf::from(format!("{home}/.claude/projects/{dir_name}")))
        }
        _ => bail!("unsupported agent: {agent} (supported: claude)"),
    }
}

struct WatchState {
    workdir: PathBuf,
    transcripts_dir: PathBuf,
    // Transcript tracking
    file_positions: HashMap<PathBuf, u64>,
    session_lines: HashMap<String, Vec<String>>,
    last_transcript_activity: Option<Instant>,
    active_session_id: Option<String>,
    agent_idle: bool,
    prompts_attached_up_to: HashMap<String, usize>, // session_id -> index into session_lines
    // Git tracking
    known_commits: BTreeSet<String>,
    branch_for_commit: BTreeMap<String, String>, // commit_id -> branch_name
    branch_first_seen: HashMap<String, i64>,     // branch_name -> first-seen epoch ms
    last_committed_branch: Option<String>,
    git_dirty: bool,
    last_git_event: Option<Instant>,
}

impl WatchState {
    fn new(workdir: &Path, transcripts_dir: &Path) -> Result<Self> {
        Ok(Self {
            workdir: workdir.to_path_buf(),
            transcripts_dir: transcripts_dir.to_path_buf(),
            file_positions: HashMap::new(),
            session_lines: HashMap::new(),
            last_transcript_activity: None,
            active_session_id: None,
            agent_idle: true,
            prompts_attached_up_to: HashMap::new(),
            known_commits: BTreeSet::new(),
            branch_for_commit: BTreeMap::new(),
            branch_first_seen: HashMap::new(),
            last_committed_branch: None,
            git_dirty: false,
            last_git_event: None,
        })
    }

    fn init_file_positions(&mut self) -> Result<()> {
        if !self.transcripts_dir.exists() {
            return Ok(());
        }
        for entry in std::fs::read_dir(&self.transcripts_dir)? {
            let entry = entry?;
            let path = entry.path();
            if path.extension().is_some_and(|e| e == "jsonl") && path.is_file() {
                let size = entry.metadata()?.len();
                self.file_positions.insert(path, size);
            }
        }
        if !self.file_positions.is_empty() {
            eprintln!(
                "  {}[init]{} Tracking {} existing transcript files",
                DIM,
                RESET,
                self.file_positions.len()
            );
        }
        Ok(())
    }

    fn handle_transcript_change(&mut self, path: &Path) -> Result<()> {
        let last_pos = self.file_positions.get(path).copied().unwrap_or(0);

        let Ok(file) = std::fs::File::open(path) else {
            return Ok(()); // File might have been deleted
        };
        let file_size = file.metadata()?.len();

        if file_size <= last_pos {
            return Ok(());
        }

        let mut reader = BufReader::new(file);
        reader.seek(SeekFrom::Start(last_pos))?;

        let mut new_lines = Vec::new();
        let mut buf = String::new();
        while reader.read_line(&mut buf)? > 0 {
            let line = buf.trim().to_string();
            if !line.is_empty() {
                new_lines.push(line);
            }
            buf.clear();
        }

        self.file_positions.insert(path.to_path_buf(), file_size);

        for line in &new_lines {
            self.process_transcript_line(line);
        }

        if !new_lines.is_empty() {
            self.last_transcript_activity = Some(Instant::now());
            self.agent_idle = false;
        }

        Ok(())
    }

    fn process_transcript_line(&mut self, line: &str) {
        let parsed: serde_json::Value = match serde_json::from_str(line) {
            Ok(v) => v,
            Err(_) => return,
        };

        let msg_type = parsed["type"].as_str().unwrap_or("");
        let session_id = parsed["sessionId"].as_str().unwrap_or("").to_string();

        if !session_id.is_empty() {
            // Detect new session
            if self.active_session_id.as_deref() != Some(&session_id) {
                let short_id = &session_id[..8.min(session_id.len())];
                let ts = parsed["timestamp"].as_str().unwrap_or("");
                eprintln!("\n{BOLD}{YELLOW}[session]{RESET} {short_id} {DIM}{ts}{RESET}");
                self.active_session_id = Some(session_id.clone());
            }
            self.session_lines
                .entry(session_id)
                .or_default()
                .push(line.to_string());
        }

        match msg_type {
            "assistant" => {
                if let Some(content) = parsed["message"]["content"].as_array() {
                    for block in content {
                        match block["type"].as_str() {
                            Some("text") => {
                                if let Some(text) = block["text"].as_str() {
                                    let text = text.trim();
                                    if text.is_empty() {
                                        continue;
                                    }
                                    let preview = truncate(text, 100).replace('\n', " ");
                                    eprintln!("  {DIM}[text]{RESET} {preview}");
                                }
                            }
                            Some("tool_use") => {
                                // Skip tool calls -- too noisy
                            }
                            _ => {}
                        }
                    }
                }
            }
            "user" => {
                let is_meta = parsed["isMeta"].as_bool().unwrap_or(false);
                if !is_meta {
                    for text in extract_content_texts(&parsed["message"]["content"]) {
                        let preview = truncate(&text, 100).replace('\n', " ");
                        eprintln!("  {BOLD}{MAGENTA}[user]{RESET} {preview}");
                    }
                }
            }
            "last-prompt" => {
                eprintln!("  {DIM}[turn complete]{RESET}");
            }
            _ => {}
        }
    }

    fn mark_git_dirty(&mut self) {
        self.git_dirty = true;
        self.last_git_event = Some(Instant::now());
    }

    fn tick(&mut self, debounce_secs: u64) -> Result<()> {
        // Check git ref changes (2s debounce to batch rapid ref updates)
        if self.git_dirty {
            if let Some(last) = self.last_git_event {
                if last.elapsed() > Duration::from_secs(2) {
                    self.git_dirty = false;
                    self.refresh_gitbutler_status()?;
                }
            }
        }

        // Check agent idle debounce
        if !self.agent_idle {
            if let Some(last) = self.last_transcript_activity {
                if last.elapsed() > Duration::from_secs(debounce_secs) {
                    self.on_agent_stop()?;
                }
            }
        }

        Ok(())
    }

    fn on_agent_stop(&mut self) -> Result<()> {
        self.agent_idle = true;
        let idle_secs = self
            .last_transcript_activity
            .map_or(0, |t| t.elapsed().as_secs());

        eprintln!("\n{BOLD}{YELLOW}[idle]{RESET} Agent stopped ({idle_secs}s) -- committing...");

        // Run but commit --ai --json
        let output = Command::new("but")
            .args(["commit", "--ai", "--json"])
            .current_dir(&self.workdir)
            .output()
            .context("failed to run 'but commit --ai --json'")?;

        let stdout = String::from_utf8_lossy(&output.stdout);
        let stderr = String::from_utf8_lossy(&output.stderr);

        if !output.status.success() {
            let msg = if !stderr.is_empty() {
                stderr.trim().to_string()
            } else {
                stdout.trim().to_string()
            };
            eprintln!("  {}[commit]{} Failed: {}", RED, RESET, truncate(&msg, 120));
            return Ok(());
        }

        // Try to parse JSON output
        if let Ok(json) = serde_json::from_str::<serde_json::Value>(&stdout) {
            // Use embedded status if available
            if !json["status"].is_null() {
                self.process_status_update(&json["status"])?;
            } else {
                self.refresh_gitbutler_status()?;
            }
        } else {
            // Non-JSON output, just refresh status
            eprintln!(
                "  {}[commit]{} {}",
                GREEN,
                RESET,
                truncate(stdout.trim(), 120)
            );
            self.refresh_gitbutler_status()?;
        }

        // Attach transcript to the branch
        self.attach_transcript()?;

        Ok(())
    }

    fn refresh_gitbutler_status(&mut self) -> Result<()> {
        let output = Command::new("but")
            .args(["status", "--json"])
            .current_dir(&self.workdir)
            .output()
            .context("failed to run 'but status --json'")?;

        if !output.status.success() {
            return Ok(());
        }

        let stdout = String::from_utf8_lossy(&output.stdout);
        let status: serde_json::Value = match serde_json::from_str(&stdout) {
            Ok(v) => v,
            Err(_) => return Ok(()),
        };

        self.process_status_update(&status)
    }

    fn process_status_update(&mut self, status: &serde_json::Value) -> Result<()> {
        let Some(stacks) = status["stacks"].as_array() else {
            return Ok(());
        };

        let session = Session::discover()?;
        let db = session.store();
        let email = session.email();

        for stack in stacks {
            let Some(branches) = stack["branches"].as_array() else {
                continue;
            };

            for branch in branches {
                let branch_name = match branch["name"].as_str() {
                    Some(n) => n.to_string(),
                    None => continue,
                };

                let Some(commits) = branch["commits"].as_array() else {
                    continue;
                };

                for commit in commits {
                    let commit_id = match commit["commitId"].as_str() {
                        Some(id) => id.to_string(),
                        None => continue,
                    };

                    if self.known_commits.contains(&commit_id) {
                        continue;
                    }

                    self.known_commits.insert(commit_id.clone());
                    self.branch_for_commit
                        .insert(commit_id.clone(), branch_name.clone());
                    self.last_committed_branch = Some(branch_name.clone());

                    // Get change-id via but show
                    let cli_id = commit["cliId"].as_str().unwrap_or("");
                    let show_id = if !cli_id.is_empty() {
                        cli_id
                    } else {
                        &commit_id
                    };

                    let change_id = get_change_id(&self.workdir, show_id);

                    let short_sha = &commit_id[..8.min(commit_id.len())];
                    let msg = commit["message"].as_str().unwrap_or("");
                    let msg_preview = truncate(msg, 60);

                    eprintln!(
                        "  {BOLD}{GREEN}[commit]{RESET} {BOLD}{short_sha}{RESET} {branch_name} \"{msg_preview}\""
                    );

                    if let Some(ref cid) = change_id {
                        let ts =
                            OffsetDateTime::now_utc().unix_timestamp_nanos() as i64 / 1_000_000;
                        let first_seen = *self
                            .branch_first_seen
                            .entry(branch_name.clone())
                            .or_insert(ts);
                        let branch_id = format!("{branch_name}@{first_seen}");
                        let value = serde_json::to_string(&branch_id)?;
                        let cid_target =
                            Target::from_parts(TargetType::ChangeId, Some(cid.clone()));
                        db.set(
                            &cid_target,
                            "branch:id",
                            &value,
                            &ValueType::String,
                            email,
                            ts,
                        )?;

                        let short_cid = &cid[..16.min(cid.len())];
                        eprintln!(
                            "  {CYAN}[meta]{RESET} change-id:{short_cid}... branch:id = {branch_id}"
                        );

                        // Attach new user prompts to the change-id
                        let prompts = self.extract_new_user_prompts();
                        if !prompts.is_empty() {
                            let prompt_count = prompts.len();
                            for prompt in prompts {
                                db.list_push_with_repo(
                                    Some(session.repo()),
                                    &cid_target,
                                    "agent:prompts",
                                    &prompt,
                                    email,
                                    ts,
                                )?;
                            }
                            eprintln!(
                                "  {CYAN}[meta]{RESET} change-id:{short_cid}... agent:prompts += {prompt_count} prompt(s)"
                            );
                        }
                    }
                }
            }
        }

        Ok(())
    }

    /// Extract user prompt texts from session lines that haven't been attached yet.
    fn extract_new_user_prompts(&mut self) -> Vec<String> {
        let Some(session_id) = &self.active_session_id else {
            return Vec::new();
        };
        let session_id = session_id.clone();

        let Some(lines) = self.session_lines.get(&session_id) else {
            return Vec::new();
        };

        let start = self
            .prompts_attached_up_to
            .get(&session_id)
            .copied()
            .unwrap_or(0);

        let mut prompts = Vec::new();
        for line in lines.iter().skip(start) {
            let parsed: serde_json::Value = match serde_json::from_str(line) {
                Ok(v) => v,
                Err(_) => continue,
            };

            if parsed["type"].as_str() != Some("user") {
                continue;
            }
            if parsed["isMeta"].as_bool().unwrap_or(false) {
                continue;
            }

            for text in extract_content_texts(&parsed["message"]["content"]) {
                prompts.push(text);
            }
        }

        // Mark all current lines as processed
        self.prompts_attached_up_to.insert(session_id, lines.len());

        prompts
    }

    fn attach_transcript(&mut self) -> Result<()> {
        let session_id = match &self.active_session_id {
            Some(id) => id.clone(),
            None => return Ok(()),
        };

        let lines = match self.session_lines.get(&session_id) {
            Some(l) if !l.is_empty() => l.clone(),
            _ => return Ok(()),
        };

        let branch_name = match &self.last_committed_branch {
            Some(b) => b.clone(),
            None => {
                eprintln!("  {YELLOW}[warn]{RESET} No branch found to attach transcript to");
                return Ok(());
            }
        };

        let transcript_content = lines.join("\n");
        let ts = OffsetDateTime::now_utc().unix_timestamp_nanos() as i64 / 1_000_000;

        let first_seen = *self
            .branch_first_seen
            .entry(branch_name.clone())
            .or_insert(ts);
        let branch_id = format!("{branch_name}@{first_seen}");

        let session = Session::discover()?;
        let db = session.store();
        let email = session.email();

        let branch_target = Target::from_parts(TargetType::Branch, Some(branch_id.clone()));
        db.list_push_with_repo(
            Some(session.repo()),
            &branch_target,
            "agent:transcripts",
            &transcript_content,
            email,
            ts,
        )?;

        eprintln!(
            "  {}{}[meta]{} Stored {} transcript lines -> branch:{} agent:transcripts",
            BOLD,
            GREEN,
            RESET,
            lines.len(),
            branch_id
        );

        // Clear stored lines -- they've been persisted
        self.session_lines.remove(&session_id);

        Ok(())
    }
}

fn get_change_id(workdir: &Path, show_id: &str) -> Option<String> {
    let output = Command::new("but")
        .args(["show", show_id, "--json"])
        .current_dir(workdir)
        .output()
        .ok()?;

    if !output.status.success() {
        return None;
    }

    let stdout = String::from_utf8_lossy(&output.stdout);
    let json: serde_json::Value = serde_json::from_str(&stdout).ok()?;
    json["changeId"]
        .as_str()
        .map(std::string::ToString::to_string)
}

/// Extract text strings from a message content value.
/// Handles both the array-of-blocks format (`[{"type":"text","text":"..."}]`)
/// and the plain-string format (`"the prompt text"`) used by Claude Code transcripts.
fn extract_content_texts(content: &serde_json::Value) -> Vec<String> {
    let mut texts = Vec::new();
    if let Some(arr) = content.as_array() {
        for block in arr {
            if block["type"].as_str() == Some("text") {
                if let Some(text) = block["text"].as_str() {
                    let text = text.trim();
                    if !text.is_empty() {
                        texts.push(text.to_string());
                    }
                }
            }
        }
    } else if let Some(s) = content.as_str() {
        let s = s.trim();
        if !s.is_empty() {
            texts.push(s.to_string());
        }
    }
    texts
}

fn truncate(s: &str, max: usize) -> String {
    if s.len() > max {
        format!("{}...", &s[..max])
    } else {
        s.to_string()
    }
}