Repository navigation
Expand file tree
/
Copy pathmain.rs
More file actions
1070 lines (1003 loc) · 47.4 KB
/
Copy pathmain.rs
File metadata and controls
1070 lines (1003 loc) · 47.4 KB
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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
mod agent;
mod api;
mod checkpoint;
mod command_registry;
mod compaction;
mod context_assembler;
mod file_index;
mod model_profile;
mod permissions;
mod prompt;
mod renderer;
mod repo_context;
mod rewind;
mod session;
mod tools;
mod util;
use agent::Agent;
use anyhow::{Context, Result};
use api::{GrokClient, ReasoningEffort};
use chrono::Utc;
use clap::Parser;
use colored::Colorize;
use context_assembler::ContextAssembler;
use permissions::{PermissionMode, SessionPermissions};
use command_registry::{Command, CommandRegistry, GroxHelper, ThinkToggleHandler};
use rustyline::{Editor, EventHandler, KeyCode, KeyEvent, Modifiers};
use serde_json::json;
use session::{SessionIndex, SessionMeta, Transcript, TranscriptEntry};
/// A model choice for the interactive picker.
struct ModelPickerChoice {
name: String,
display: String,
}
impl std::fmt::Display for ModelPickerChoice {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.display)
}
}
#[derive(Parser)]
#[command(name = "grox", about = "Agentic coding with Grok")]
struct Cli {
/// Model name (overrides GROX_MODEL env var)
#[arg(long)]
model: Option<String>,
/// Print raw SSE events to stderr
#[arg(long)]
verbose: bool,
/// Auto-approve writes inside the project root
#[arg(long, conflicts_with_all = ["read_only", "yolo"])]
auto_approve_writes: bool,
/// Read-only mode: deny all writes and shell execution
#[arg(long, conflicts_with_all = ["auto_approve_writes", "yolo"])]
read_only: bool,
/// YOLO mode: auto-approve everything, including destructive commands
#[arg(long, conflicts_with_all = ["auto_approve_writes", "read_only"])]
yolo: bool,
/// Resume a previous session (optionally specify session ID; defaults to most recent for this project)
#[arg(long, num_args = 0..=1, default_missing_value = "")]
resume: Option<String>,
/// Set initial reasoning effort (low or high). Only effective on models that support it.
#[arg(long, num_args = 0..=1, default_missing_value = "low")]
think: Option<String>,
/// Do not store conversations on the provider side
#[arg(long)]
no_store: bool,
}
#[tokio::main]
async fn main() -> Result<()> {
let _ = dotenvy::dotenv();
let cli = Cli::parse();
let api_key = match std::env::var("XAI_API_KEY") {
Ok(key) if !key.is_empty() => key,
_ => {
eprintln!("{}", "XAI_API_KEY is not set.".red().bold());
eprintln!();
eprintln!("Get your API key at: {}", "https://console.x.ai/".cyan());
eprintln!("Then export it in your shell:");
eprintln!();
eprintln!(" {}", "export XAI_API_KEY=your-key-here".dimmed());
std::process::exit(1);
}
};
const DEFAULT_MODEL: &str = "grok-4-1-fast-reasoning";
let user_specified_model = cli.model.is_some() || std::env::var("GROX_MODEL").is_ok();
let model = cli.model.unwrap_or_else(|| {
std::env::var("GROX_MODEL").unwrap_or_else(|_| DEFAULT_MODEL.to_string())
});
if cli.verbose {
// SAFETY: called once at startup before any threads are spawned
unsafe { std::env::set_var("GROX_VERBOSE", "1") };
}
let permission_mode = if cli.yolo {
PermissionMode::Yolo
} else if cli.read_only {
PermissionMode::ReadOnly
} else if cli.auto_approve_writes {
PermissionMode::Trust
} else {
PermissionMode::Default
};
let cwd = std::env::current_dir().context("Failed to get current directory")?;
let project_root = util::detect_project_root(&cwd);
// --- Session setup ---
let sessions_dir = SessionIndex::default_sessions_dir()?;
let (mut session_meta, mut transcript, mut history, resumed) = if let Some(ref resume_arg) =
cli.resume
{
// Resume an existing session
let resume_id = resume_arg.trim();
let meta = if resume_id.is_empty() {
// No ID specified — resume most recent session for this project
let project_sessions =
SessionIndex::list_for_project(&sessions_dir, &project_root.display().to_string())?;
match project_sessions.into_iter().next() {
Some(m) => m,
None => {
eprintln!(
"{}",
"no previous sessions found for this project".red().bold()
);
std::process::exit(1);
}
}
} else {
// ID specified — find by prefix match
let all = SessionIndex::list(&sessions_dir)?;
let matches: Vec<_> = all
.into_iter()
.filter(|s| s.session_id.starts_with(resume_id))
.collect();
match matches.len() {
0 => {
eprintln!(
"{}",
format!("no session found matching '{resume_id}'")
.red()
.bold()
);
std::process::exit(1);
}
1 => matches.into_iter().next().unwrap(),
n => {
eprintln!(
"{}",
format!("'{resume_id}' matches {n} sessions — be more specific:")
.red()
.bold()
);
for m in &matches {
eprintln!(
" {} ({})",
&m.session_id[..8],
m.last_active.format("%Y-%m-%d %H:%M")
);
}
std::process::exit(1);
}
}
};
// Warn if resuming a session from a different project
let current_project = project_root.display().to_string();
if meta.project_root != current_project {
eprintln!(
"{}",
format!(
" warning: session {} was created in '{}', but you are in '{}'",
&meta.session_id[..8],
meta.project_root,
current_project
)
.yellow()
);
eprintln!(
"{}",
" tools will operate on the current directory, not the original project".yellow()
);
}
let transcript_path = SessionMeta::transcript_path(&sessions_dir, &meta.session_id);
let t = Transcript::new(&transcript_path);
if !transcript_path.exists() {
eprintln!(
"{}",
format!(
" warning: transcript file missing for session {} — starting with empty history",
&meta.session_id[..8]
).yellow()
);
t.create()?;
}
let entries = t.read_all().with_context(|| {
format!(
"failed to read transcript for session {}",
&meta.session_id[..8]
)
})?;
if transcript_path.exists()
&& entries.is_empty()
&& std::fs::metadata(&transcript_path)
.map(|m| m.len() > 0)
.unwrap_or(false)
{
eprintln!(
"{}",
format!(
" warning: transcript for session {} appears corrupt — starting with empty history",
&meta.session_id[..8]
).yellow()
);
}
(meta, t, entries, true)
} else {
// New session
let meta = SessionMeta::new(&model, project_root.display().to_string());
let t = Transcript::new(SessionMeta::transcript_path(
&sessions_dir,
&meta.session_id,
));
t.create()?;
meta.save(&sessions_dir)?;
(meta, t, Vec::new(), false)
};
// Resolve model for this session: on resume, prefer saved model unless user explicitly overrode
let model = if resumed && !user_specified_model {
session_meta.model.clone()
} else {
if resumed && session_meta.model != model {
session_meta.model = model.clone();
}
model
};
println!("{}", "grox — agentic coding with Grok".bold());
println!(
"model: {} | project: {} | mode: {} | type {} to exit",
model.cyan(),
project_root.display().to_string().cyan(),
format!("{permission_mode}").cyan(),
"/quit".dimmed()
);
let no_store = cli.no_store || std::env::var("GROX_NO_STORE").as_deref() == Ok("1");
if no_store {
println!(
"{}",
"note: grox can read any file on your system. File contents are sent to xAI (store: false — provider asked not to retain)."
.dimmed()
);
} else {
println!(
"{}",
"note: grox can read any file on your system. File contents are sent to xAI and stored for 30 days (use --no-store to opt out)."
.dimmed()
);
}
if resumed {
println!(
"{}",
format!("session: {} (resumed)", &session_meta.session_id[..8]).dimmed()
);
// Show brief summary of previous state
let user_turns = history
.iter()
.filter(|e| matches!(e, TranscriptEntry::UserMessage { .. }))
.count();
let assistant_turns = history
.iter()
.filter(|e| matches!(e, TranscriptEntry::AssistantMessage { .. }))
.count();
println!(
"{}",
format!(
" {} user turns, {} assistant responses — last active {}",
user_turns,
assistant_turns,
session_meta.last_active.format("%Y-%m-%d %H:%M UTC")
)
.dimmed()
);
} else {
println!(
"{}",
format!("session: {}", &session_meta.session_id[..8]).dimmed()
);
}
println!();
let mut session_perms = SessionPermissions::new(permission_mode, project_root.clone());
let mut client = GrokClient::new(api_key, model, session_meta.session_id.clone());
if no_store {
client.set_no_store(true);
}
// Set initial reasoning effort from --think flag
if let Some(ref think_arg) = cli.think {
let effort = match think_arg.as_str() {
"low" => Some(ReasoningEffort::Low),
"high" => Some(ReasoningEffort::High),
other => {
eprintln!(
"{}",
format!(" warning: unknown --think value '{other}', using 'low'").yellow()
);
Some(ReasoningEffort::Low)
}
};
let profile = model_profile::ModelProfile::for_model(client.model());
if profile.supports_reasoning_effort_control {
client.set_reasoning_effort(effort);
} else {
eprintln!(
"{}",
format!(
" warning: model {} does not support reasoning effort control, --think ignored",
client.model()
)
.yellow()
);
}
}
// Gather repo context
let repo_ctx = repo_context::RepoContext::gather(&project_root);
if repo_ctx.truncated {
eprintln!(
"{}",
" warning: repo context exceeds 10K characters and was truncated".yellow()
);
}
// Load GROX.md custom instructions if present
let grox_md = util::load_grox_md(&project_root);
if let Some(ref content) = grox_md
&& content.contains("truncated")
{
eprintln!(
"{}",
" warning: GROX.md exceeds 10K characters and was truncated".yellow()
);
}
let repo_ctx_text = if repo_ctx.text.is_empty() {
None
} else {
Some(repo_ctx.text.as_str())
};
let system_content =
prompt::build_system_prompt(&project_root, repo_ctx_text, grox_md.as_deref());
let system_prompt = json!({
"role": "system",
"content": system_content
});
let mut assembler = ContextAssembler::new(system_prompt);
let file_idx = file_index::FileIndex::build(&project_root);
let config = rustyline::Config::builder()
.completion_type(rustyline::config::CompletionType::List)
.build();
let mut rl = Editor::with_config(config)?;
rl.set_helper(Some(GroxHelper::with_file_index(file_idx.clone())));
let mut term_renderer = renderer::TerminalRenderer::new();
// Seed cumulative stats from persisted session data (matters for --resume)
term_renderer.seed_cumulative(
session_meta.cumulative_input_tokens,
session_meta.cumulative_output_tokens,
session_meta.cumulative_cached_input_tokens,
session_meta.cumulative_cost,
);
// Bind Ctrl+T to toggle thinking display
rl.bind_sequence(
KeyEvent(KeyCode::Char('t'), Modifiers::CTRL),
EventHandler::Conditional(Box::new(ThinkToggleHandler::new(
term_renderer.thinking_expanded_handle(),
))),
);
loop {
let input = match rl.readline(&format!("{} ", ">>".green().bold())) {
Ok(line) => line,
Err(
rustyline::error::ReadlineError::Interrupted | rustyline::error::ReadlineError::Eof,
) => break,
Err(e) => return Err(e.into()),
};
let input = input.trim().to_string();
if input.is_empty() {
continue;
}
// --- Slash command dispatch via registry ---
if input.starts_with('/') {
match CommandRegistry::find(&input) {
Some((spec, args)) => match spec.command {
Command::Quit => break,
Command::Model => {
let new_model = args.to_string();
if new_model.is_empty() {
// Interactive model picker
let models = model_profile::ModelProfile::known_models();
let current = client.model().to_string();
let current_profile =
model_profile::ModelProfile::for_model(¤t);
let choices: Vec<ModelPickerChoice> = models
.iter()
.map(|(_, profile)| ModelPickerChoice {
name: profile.name.clone(),
display: profile.format_for_picker(¤t_profile),
})
.collect();
let starting_cursor = models
.iter()
.position(|(_, p)| p.same_family(¤t_profile))
.unwrap_or(0);
match inquire::Select::new("select a model:", choices)
.with_starting_cursor(starting_cursor)
.with_page_size(10)
.prompt()
{
Ok(choice) => {
let choice_profile =
model_profile::ModelProfile::for_model(&choice.name);
if choice_profile.same_family(¤t_profile) {
println!(
"{}",
format!(" already using {}", current).dimmed()
);
} else {
let profile =
model_profile::ModelProfile::for_model(&choice.name);
if !profile.supports_tools {
println!(
"{}",
format!(
" model {} does not support tool use",
choice.name
)
.red()
);
} else {
client.set_model(choice.name.clone());
session_meta.model = choice.name.clone();
println!(
" model switched to {}",
choice.name.cyan()
);
}
}
}
Err(
inquire::InquireError::OperationCanceled
| inquire::InquireError::OperationInterrupted,
) => {
// User pressed Escape or Ctrl+C — cancel silently
}
Err(e) => {
println!(
"{}",
format!(" model picker error: {e}").red()
);
}
}
} else {
let profile = model_profile::ModelProfile::for_model(&new_model);
if !profile.supports_tools {
println!(
"{}",
format!(" model {new_model} does not support tool use").red()
);
} else {
client.set_model(new_model.clone());
session_meta.model = new_model.clone();
println!(" model switched to {}", new_model.cyan());
}
}
}
Command::Think => {
if args == "display" {
let expanded = term_renderer.toggle_thinking_display();
let label = if expanded { "expanded" } else { "collapsed" };
println!(" thinking display: {}", label.cyan());
} else if !args.is_empty() {
println!(
"{}",
format!(" unknown argument: {args}. usage: /think [display]")
.dimmed()
);
} else {
let profile =
model_profile::ModelProfile::for_model(client.model());
if !profile.supports_reasoning_effort_control {
println!(
"{}",
format!(
" model {} does not support reasoning effort control",
client.model()
)
.dimmed()
);
if profile.supports_reasoning() {
println!(
"{}",
" (this model has built-in reasoning that is always active)"
.dimmed()
);
}
} else {
let next = match client.reasoning_effort() {
None => Some(ReasoningEffort::Low),
Some(ReasoningEffort::Low) => Some(ReasoningEffort::High),
Some(ReasoningEffort::High) => None,
};
client.set_reasoning_effort(next);
match next {
Some(effort) => {
println!(
" reasoning effort: {}",
effort.to_string().cyan()
)
}
None => {
println!(" reasoning effort: {}", "off".dimmed())
}
}
}
}
}
Command::Status => {
let profile = model_profile::ModelProfile::for_model(client.model());
let estimated = assembler.estimate_tokens(&history);
println!(" model: {}", client.model().cyan());
let mut caps = Vec::new();
if profile.returns_plaintext_reasoning {
caps.push("plaintext-reasoning");
}
if profile.returns_encrypted_reasoning {
caps.push("encrypted-reasoning");
}
if profile.supports_reasoning_effort_control {
caps.push("effort-control");
}
if !caps.is_empty() {
println!(" caps: {}", caps.join(", ").cyan());
}
if profile.supports_reasoning_effort_control {
let effort_str = match client.reasoning_effort() {
Some(e) => e.to_string(),
None => "off".to_string(),
};
println!(" think: {}", effort_str.cyan());
}
println!(" project: {}", project_root.display().to_string().cyan());
println!(" mode: {}", format!("{permission_mode}").cyan());
println!(" session: {}", &session_meta.session_id[..8].dimmed());
println!(
" context: ~{} tokens (threshold: {})",
estimated.to_string().cyan(),
profile.compaction_threshold().to_string().cyan()
);
println!(
" tools: {}",
tools::Tool::all()
.iter()
.map(|t| format!("{t:?}"))
.collect::<Vec<_>>()
.join(", ")
.dimmed()
);
}
Command::Undo => {
let mut mode = rewind::RewindMode::Both;
let mut turn_number: Option<usize> = None;
let mut parse_error = false;
for arg in args.split_whitespace() {
match arg {
"--code" => mode = rewind::RewindMode::CodeOnly,
"--conversation" => mode = rewind::RewindMode::ConversationOnly,
"--both" => mode = rewind::RewindMode::Both,
_ => {
if let Ok(n) = arg.parse::<usize>() {
turn_number = Some(n);
} else {
println!(
"{}",
format!(" unknown argument: {arg}. usage: /undo [N] [--code|--conversation|--both]")
.dimmed()
);
parse_error = true;
break;
}
}
}
}
if !parse_error {
let result = if let Some(n) = turn_number {
rewind::rewind_to_turn(&history, n, &project_root, mode)
} else {
rewind::undo_last_turn(&history, &project_root, mode)
};
match result {
Ok(rr) => {
println!("{}", rewind::format_rewind_result(&rr).yellow());
history = rr.entries;
transcript.atomic_rewrite(&history)?;
}
Err(e) => {
println!("{}", format!(" undo failed: {e}").red());
}
}
}
}
Command::Sessions => {
let project_str = project_root.display().to_string();
match SessionIndex::list_for_project(&sessions_dir, &project_str) {
Ok(sessions) if sessions.is_empty() => {
println!("{}", " no sessions found for this project".dimmed());
}
Ok(sessions) => {
println!(" {}", "recent sessions:".bold());
for (i, s) in sessions.iter().take(10).enumerate() {
let active = if s.session_id == session_meta.session_id {
" (current)"
} else {
""
};
let summary_part = if s.summary.is_empty() {
String::new()
} else {
format!(" — {}", s.summary)
};
println!(
" {}. {} — {} — {}{}{}",
i + 1,
&s.session_id[..8].cyan(),
s.last_active
.format("%Y-%m-%d %H:%M")
.to_string()
.dimmed(),
s.model.dimmed(),
summary_part.dimmed(),
active.green(),
);
}
println!(
"{}",
" use /resume <id> to resume a session".dimmed()
);
}
Err(e) => {
println!("{}", format!(" failed to list sessions: {e}").red());
}
}
}
Command::Resume => {
if args.is_empty() {
println!("{}", " usage: /resume <session-id-prefix>".dimmed());
println!(
"{}",
" use /sessions to see available sessions".dimmed()
);
} else {
let all = match SessionIndex::list(&sessions_dir) {
Ok(s) => s,
Err(e) => {
println!(
"{}",
format!(" failed to list sessions: {e}").red()
);
continue;
}
};
let matches: Vec<_> = all
.into_iter()
.filter(|s| s.session_id.starts_with(args))
.collect();
match matches.len() {
0 => {
println!(
"{}",
format!(" no session found matching '{args}'").dimmed()
);
}
1 => {
let target = &matches[0];
if target.session_id == session_meta.session_id {
println!("{}", " already in this session".dimmed());
} else {
let current_project =
project_root.display().to_string();
if target.project_root != current_project {
println!(
"{}",
format!(
" warning: session was created in '{}', tools will operate on '{}'",
target.project_root, current_project
).yellow()
);
}
let t = Transcript::new(SessionMeta::transcript_path(
&sessions_dir,
&target.session_id,
));
match t.read_all() {
Ok(entries) => {
let user_turns = entries
.iter()
.filter(|e| {
matches!(
e,
TranscriptEntry::UserMessage { .. }
)
})
.count();
let assistant_turns = entries
.iter()
.filter(|e| {
matches!(
e,
TranscriptEntry::AssistantMessage {
..
}
)
})
.count();
session_meta = target.clone();
term_renderer.seed_cumulative(
session_meta.cumulative_input_tokens,
session_meta.cumulative_output_tokens,
session_meta.cumulative_cached_input_tokens,
session_meta.cumulative_cost,
);
history = entries;
transcript =
Transcript::new(SessionMeta::transcript_path(
&sessions_dir,
&session_meta.session_id,
));
client.set_session_id(
session_meta.session_id.clone(),
);
if session_meta.model != client.model() {
client
.set_model(session_meta.model.clone());
println!(
" model switched to {} (from resumed session)",
session_meta.model.cyan()
);
}
println!(
" {} session {} — {} user turns, {} assistant responses",
"resumed".green(),
&session_meta.session_id[..8].cyan(),
user_turns,
assistant_turns,
);
}
Err(e) => {
println!(
"{}",
format!(" failed to read transcript: {e}")
.red()
);
}
}
}
}
n => {
println!(
"{}",
format!(
" '{args}' matches {n} sessions — be more specific:"
)
.dimmed()
);
for m in &matches {
println!(
" {} ({})",
&m.session_id[..8],
m.last_active.format("%Y-%m-%d %H:%M")
);
}
}
}
}
}
Command::Compact => {
let old_estimate = assembler.estimate_tokens(&history);
let old_count = history.len();
let heuristic = compaction::heuristic_compact(&history, &project_root);
let after_heuristic = assembler.estimate_tokens(&heuristic.entries);
let threshold = model_profile::ModelProfile::for_model(client.model())
.compaction_threshold();
let result = if after_heuristic > threshold {
match compaction::llm_compact(&heuristic.entries, client.model(), &client)
.await
{
Ok(llm_result) if llm_result.compacted => llm_result,
Ok(llm_result) => compaction::CompactionResult {
llm_usage: llm_result.llm_usage,
..heuristic
},
Err(e) => {
eprintln!(
"{}",
format!(" warning: LLM compaction failed: {e}").yellow()
);
heuristic
}
}
} else {
heuristic
};
if let Some(u) = &result.llm_usage {
term_renderer.record_usage(client.model(), u);
session_meta.cumulative_input_tokens += u.input_tokens;
session_meta.cumulative_output_tokens += u.output_tokens;
if let Some(cached) = u.cached_input_tokens {
session_meta.cumulative_cached_input_tokens += cached;
}
if let Some(cost) = model_profile::ModelProfile::for_model(client.model()).estimate_cost_from_usage(u) {
session_meta.cumulative_cost += cost;
}
let _ = session_meta.save(&sessions_dir);
}
if result.compacted {
let new_estimate = assembler.estimate_tokens(&result.entries);
let new_count = result.entries.len();
let llm_cost = result.llm_usage.as_ref().and_then(|u| {
let profile =
model_profile::ModelProfile::for_model(client.model());
profile.estimate_cost_from_usage(u)
});
history = result.entries;
transcript.atomic_rewrite(&history)?;
let mut msg = format!(
" compacted: {} entries → {} entries, ~{} → ~{} tokens",
old_count, new_count, old_estimate, new_estimate
);
if let Some(cost) = llm_cost {
msg.push_str(&format!(" (LLM summarization: ~${cost:.4})"));
}
println!("{}", msg.yellow());
} else {
let llm_cost = result.llm_usage.as_ref().and_then(|u| {
let profile =
model_profile::ModelProfile::for_model(client.model());
profile.estimate_cost_from_usage(u)
});
if let Some(cost) = llm_cost {
println!(
"{}",
format!(
" nothing to compact (LLM summarization attempt: ~${cost:.4})"
)
.yellow()
);
} else {
println!("{}", " nothing to compact".dimmed());
}
}
}
},
None => {
let cmd = input.split_whitespace().next().unwrap_or(&input);
println!("{}", format!(" unknown command: {cmd}").dimmed());
println!(
"{}",
" type / and press Tab for available commands".dimmed()
);
}
}
continue;
}
rl.add_history_entry(&input)?;
// Append user message to history and transcript
let user_entry = TranscriptEntry::user_message(&input);
history.push(user_entry.clone());
transcript.append(&user_entry)?;
// Update last_active on every turn (not just successful API calls)
session_meta.last_active = Utc::now();
let _ = session_meta.save(&sessions_dir);
// Preflight budget check: compact if estimated tokens exceed threshold
if let Some(result) =
compaction::maybe_compact(&history, &assembler, client.model(), &project_root, &client)
.await
{
// Always account for LLM usage even if compaction didn't reduce size
if let Some(u) = &result.llm_usage {
term_renderer.record_usage(client.model(), u);
session_meta.cumulative_input_tokens += u.input_tokens;
session_meta.cumulative_output_tokens += u.output_tokens;
if let Some(cached) = u.cached_input_tokens {
session_meta.cumulative_cached_input_tokens += cached;
}
if let Some(cost) = model_profile::ModelProfile::for_model(client.model()).estimate_cost_from_usage(u) {
session_meta.cumulative_cost += cost;
}
let _ = session_meta.save(&sessions_dir);
}
if result.compacted {
let old_estimate = assembler.estimate_tokens(&history);
let new_estimate = assembler.estimate_tokens(&result.entries);
let threshold =
model_profile::ModelProfile::for_model(client.model()).compaction_threshold();
let llm_cost = result.llm_usage.as_ref().and_then(|u| {
let profile = model_profile::ModelProfile::for_model(client.model());
profile.estimate_cost_from_usage(u)
});
history = result.entries;
transcript.atomic_rewrite(&history)?;
let mut msg = format!(
" auto-compacted: ~{old_estimate} → ~{new_estimate} tokens (threshold: {threshold})",
);
if let Some(cost) = llm_cost {
msg.push_str(&format!(" [LLM summarization: ~${cost:.4}]"));
}
eprintln!("{}", msg.yellow());
}
}
// Build full message array from history
let api_input = assembler.build_messages(&history);
println!();
term_renderer.begin_turn();
term_renderer.print_streaming_indicator();
let agent = Agent::new(&client, &project_root);
let run_result = agent
.run(
api_input,
&mut term_renderer,
&mut |name: &str, args: &str| session_perms.authorize(name, args),
&mut || {
// Refresh repo context and file index after mutating tools
file_idx.refresh();
let fresh_ctx = repo_context::RepoContext::gather(&project_root);
let fresh_ctx_text = if fresh_ctx.text.is_empty() {
None
} else {
Some(fresh_ctx.text.as_str())
};
let fresh_system_content = prompt::build_system_prompt(
&project_root,
fresh_ctx_text,
grox_md.as_deref(),
);
let fresh_prompt = json!({
"role": "system",
"content": fresh_system_content
});
assembler.set_system_prompt(fresh_prompt.clone());
fresh_prompt