forked from 1jehuang/jcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathambient.rs
More file actions
1115 lines (991 loc) · 36.2 KB
/
ambient.rs
File metadata and controls
1115 lines (991 loc) · 36.2 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
use anyhow::Result;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
pub mod runner;
pub mod scheduler;
use crate::config::config;
use crate::storage;
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
/// Context passed from the ambient runner to a visible TUI cycle.
/// Saved to `~/.jcode/ambient/visible_cycle.json`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VisibleCycleContext {
pub system_prompt: String,
pub initial_message: String,
}
impl VisibleCycleContext {
pub fn context_path() -> Result<PathBuf> {
Ok(storage::jcode_dir()?
.join("ambient")
.join("visible_cycle.json"))
}
pub fn save(&self) -> Result<()> {
let path = Self::context_path()?;
if let Some(parent) = path.parent() {
storage::ensure_dir(parent)?;
}
storage::write_json(&path, self)
}
pub fn load() -> Result<Self> {
let path = Self::context_path()?;
storage::read_json(&path)
}
pub fn result_path() -> Result<PathBuf> {
Ok(storage::jcode_dir()?
.join("ambient")
.join("cycle_result.json"))
}
}
/// Ambient mode status
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub enum AmbientStatus {
#[default]
Idle,
Running {
detail: String,
},
Scheduled {
next_wake: DateTime<Utc>,
},
Paused {
reason: String,
},
Disabled,
}
/// Priority for scheduled items
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub enum Priority {
Low,
Normal,
High,
}
/// Where a scheduled task should be delivered when it becomes due.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum ScheduleTarget {
/// Wake the ambient agent and hand it the queued task.
#[default]
Ambient,
/// Deliver the reminder back into a specific interactive session.
Session { session_id: String },
/// Spawn a single new session derived from the originating session.
Spawn { parent_session_id: String },
}
impl ScheduleTarget {
pub fn is_direct_delivery(&self) -> bool {
matches!(self, Self::Session { .. } | Self::Spawn { .. })
}
}
/// A scheduled ambient task
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScheduledItem {
pub id: String,
pub scheduled_for: DateTime<Utc>,
pub context: String,
pub priority: Priority,
#[serde(default)]
pub target: ScheduleTarget,
pub created_by_session: String,
pub created_at: DateTime<Utc>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub working_dir: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub task_description: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub relevant_files: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub git_branch: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub additional_context: Option<String>,
}
/// Persistent ambient state
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct AmbientState {
pub status: AmbientStatus,
pub last_run: Option<DateTime<Utc>>,
pub last_summary: Option<String>,
pub last_compactions: Option<u32>,
pub last_memories_modified: Option<u32>,
pub total_cycles: u64,
}
/// Result from an ambient cycle
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AmbientCycleResult {
pub summary: String,
pub memories_modified: u32,
pub compactions: u32,
pub proactive_work: Option<String>,
pub next_schedule: Option<ScheduleRequest>,
pub started_at: DateTime<Utc>,
pub ended_at: DateTime<Utc>,
pub status: CycleStatus,
/// Full conversation transcript (markdown) for email notifications
#[serde(default, skip_serializing_if = "Option::is_none")]
pub conversation: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CycleStatus {
Complete,
Interrupted,
Incomplete,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScheduleRequest {
pub wake_in_minutes: Option<u32>,
pub wake_at: Option<DateTime<Utc>>,
pub context: String,
pub priority: Priority,
#[serde(default)]
pub target: ScheduleTarget,
#[serde(default)]
pub created_by_session: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub working_dir: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub task_description: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub relevant_files: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub git_branch: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub additional_context: Option<String>,
}
// ---------------------------------------------------------------------------
// Storage paths
// ---------------------------------------------------------------------------
fn ambient_dir() -> Result<PathBuf> {
let dir = storage::jcode_dir()?.join("ambient");
storage::ensure_dir(&dir)?;
Ok(dir)
}
fn state_path() -> Result<PathBuf> {
Ok(ambient_dir()?.join("state.json"))
}
fn queue_path() -> Result<PathBuf> {
Ok(ambient_dir()?.join("queue.json"))
}
fn lock_path() -> Result<PathBuf> {
Ok(ambient_dir()?.join("ambient.lock"))
}
fn transcripts_dir() -> Result<PathBuf> {
let dir = ambient_dir()?.join("transcripts");
storage::ensure_dir(&dir)?;
Ok(dir)
}
// ---------------------------------------------------------------------------
// User Directives (from email replies)
// ---------------------------------------------------------------------------
/// A user directive received via email reply to an ambient cycle notification.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserDirective {
pub id: String,
pub text: String,
pub received_at: DateTime<Utc>,
pub in_reply_to_cycle: String,
pub consumed: bool,
}
fn directives_path() -> Result<PathBuf> {
Ok(ambient_dir()?.join("directives.json"))
}
pub fn load_directives() -> Vec<UserDirective> {
directives_path()
.ok()
.and_then(|p| {
if p.exists() {
storage::read_json(&p).ok()
} else {
None
}
})
.unwrap_or_default()
}
fn save_directives(directives: &[UserDirective]) -> Result<()> {
storage::write_json(&directives_path()?, directives)
}
/// Store a new directive from an email reply.
pub fn add_directive(text: String, in_reply_to: String) -> Result<()> {
let mut directives = load_directives();
directives.push(UserDirective {
id: format!("dir_{:08x}", rand::random::<u32>()),
text,
received_at: Utc::now(),
in_reply_to_cycle: in_reply_to,
consumed: false,
});
save_directives(&directives)
}
/// Take all unconsumed directives, marking them as consumed.
pub fn take_pending_directives() -> Vec<UserDirective> {
let mut all = load_directives();
let pending: Vec<_> = all.iter().filter(|d| !d.consumed).cloned().collect();
if pending.is_empty() {
return pending;
}
for d in &mut all {
if !d.consumed {
d.consumed = true;
}
}
let _ = save_directives(&all);
pending
}
/// Check if there are any unconsumed directives.
pub fn has_pending_directives() -> bool {
load_directives().iter().any(|d| !d.consumed)
}
// ---------------------------------------------------------------------------
// AmbientState persistence
// ---------------------------------------------------------------------------
impl AmbientState {
pub fn load() -> Result<Self> {
let path = state_path()?;
if path.exists() {
storage::read_json(&path)
} else {
Ok(Self::default())
}
}
pub fn save(&self) -> Result<()> {
storage::write_json(&state_path()?, self)
}
pub fn record_cycle(&mut self, result: &AmbientCycleResult) {
self.last_run = Some(result.ended_at);
self.last_summary = Some(result.summary.clone());
self.last_compactions = Some(result.compactions);
self.last_memories_modified = Some(result.memories_modified);
self.total_cycles += 1;
match result.status {
CycleStatus::Complete => {
if let Some(ref req) = result.next_schedule {
let next = req.wake_at.unwrap_or_else(|| {
Utc::now()
+ chrono::Duration::minutes(req.wake_in_minutes.unwrap_or(30) as i64)
});
self.status = AmbientStatus::Scheduled { next_wake: next };
} else {
self.status = AmbientStatus::Idle;
}
}
CycleStatus::Interrupted | CycleStatus::Incomplete => {
self.status = AmbientStatus::Idle;
}
}
}
}
// ---------------------------------------------------------------------------
// ScheduledQueue
// ---------------------------------------------------------------------------
pub struct ScheduledQueue {
items: Vec<ScheduledItem>,
path: PathBuf,
}
impl ScheduledQueue {
pub fn load(path: PathBuf) -> Self {
let items: Vec<ScheduledItem> = if path.exists() {
storage::read_json(&path).unwrap_or_default()
} else {
Vec::new()
};
Self { items, path }
}
pub fn save(&self) -> Result<()> {
storage::write_json(&self.path, &self.items)
}
pub fn push(&mut self, item: ScheduledItem) {
self.items.push(item);
let _ = self.save();
}
/// Pop items whose `scheduled_for` is in the past, sorted by priority
/// (highest first) then by time (earliest first).
pub fn pop_ready(&mut self) -> Vec<ScheduledItem> {
let now = Utc::now();
let (ready, remaining): (Vec<_>, Vec<_>) =
self.items.drain(..).partition(|i| i.scheduled_for <= now);
self.items = remaining;
let mut ready = ready;
// Sort: highest priority first, then earliest scheduled_for
ready.sort_by(|a, b| {
b.priority
.cmp(&a.priority)
.then_with(|| a.scheduled_for.cmp(&b.scheduled_for))
});
if !ready.is_empty() {
let _ = self.save();
}
ready
}
/// Remove and return ready items targeted at a specific direct-delivery session,
/// leaving ambient-targeted queue items intact for the ambient agent to process.
pub fn take_ready_direct_items(&mut self) -> Vec<ScheduledItem> {
let now = Utc::now();
let mut ready_direct = Vec::new();
let mut remaining = Vec::with_capacity(self.items.len());
for item in self.items.drain(..) {
let is_ready = item.scheduled_for <= now;
let is_direct_target = item.target.is_direct_delivery();
if is_ready && is_direct_target {
ready_direct.push(item);
} else {
remaining.push(item);
}
}
self.items = remaining;
if !ready_direct.is_empty() {
let _ = self.save();
}
ready_direct.sort_by(|a, b| {
b.priority
.cmp(&a.priority)
.then_with(|| a.scheduled_for.cmp(&b.scheduled_for))
});
ready_direct
}
pub fn peek_next(&self) -> Option<&ScheduledItem> {
self.items.iter().min_by_key(|i| i.scheduled_for)
}
pub fn len(&self) -> usize {
self.items.len()
}
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
pub fn items(&self) -> &[ScheduledItem] {
&self.items
}
}
// ---------------------------------------------------------------------------
// AmbientLock (single-instance guard)
// ---------------------------------------------------------------------------
pub struct AmbientLock {
lock_path: PathBuf,
}
impl AmbientLock {
/// Try to acquire the ambient lock.
/// Returns `Ok(Some(lock))` if acquired, `Ok(None)` if another instance
/// already holds it, or `Err` on I/O failure.
pub fn try_acquire() -> Result<Option<Self>> {
let path = lock_path()?;
// Check existing lock
if path.exists() {
if let Ok(contents) = std::fs::read_to_string(&path)
&& let Ok(pid) = contents.trim().parse::<u32>()
&& is_pid_alive(pid)
{
return Ok(None); // Another instance is running
}
let _ = std::fs::remove_file(&path);
}
// Write our PID
let pid = std::process::id();
if let Some(parent) = path.parent() {
storage::ensure_dir(parent)?;
}
std::fs::write(&path, pid.to_string())?;
Ok(Some(Self { lock_path: path }))
}
pub fn release(self) -> Result<()> {
let _ = std::fs::remove_file(&self.lock_path);
// Drop runs, but we already cleaned up
std::mem::forget(self);
Ok(())
}
}
impl Drop for AmbientLock {
fn drop(&mut self) {
let _ = std::fs::remove_file(&self.lock_path);
}
}
fn is_pid_alive(pid: u32) -> bool {
crate::platform::is_process_running(pid)
}
// ---------------------------------------------------------------------------
// AmbientManager
// ---------------------------------------------------------------------------
pub struct AmbientManager {
state: AmbientState,
queue: ScheduledQueue,
}
impl AmbientManager {
pub fn new() -> Result<Self> {
// Ensure storage layout exists
let _ = ambient_dir()?;
let _ = transcripts_dir()?;
let state = AmbientState::load()?;
let queue = ScheduledQueue::load(queue_path()?);
Ok(Self { state, queue })
}
pub fn is_enabled() -> bool {
config().ambient.enabled
}
/// Check whether it's time to run a cycle based on current state and queue.
pub fn should_run(&self) -> bool {
if !Self::is_enabled() {
return false;
}
match &self.state.status {
AmbientStatus::Disabled | AmbientStatus::Paused { .. } => false,
AmbientStatus::Running { .. } => false, // already running
AmbientStatus::Idle => true,
AmbientStatus::Scheduled { next_wake } => Utc::now() >= *next_wake,
}
}
pub fn record_cycle_result(&mut self, result: AmbientCycleResult) -> Result<()> {
self.state.record_cycle(&result);
self.state.save()?;
// If the cycle produced a schedule request, enqueue it
if let Some(ref req) = result.next_schedule {
self.schedule(req.clone())?;
}
Ok(())
}
/// Remove and return all ready scheduled items.
pub fn take_ready_items(&mut self) -> Vec<ScheduledItem> {
self.queue.pop_ready()
}
/// Remove and return only ready items targeted at direct delivery into a
/// specific resumed or spawned session.
pub fn take_ready_direct_items(&mut self) -> Vec<ScheduledItem> {
self.queue.take_ready_direct_items()
}
/// Add a schedule request to the queue. Returns the item ID.
pub fn schedule(&mut self, request: ScheduleRequest) -> Result<String> {
let id = format!("sched_{:08x}", rand::random::<u32>());
let scheduled_for = request.wake_at.unwrap_or_else(|| {
Utc::now() + chrono::Duration::minutes(request.wake_in_minutes.unwrap_or(30) as i64)
});
let item = ScheduledItem {
id: id.clone(),
scheduled_for,
context: request.context,
priority: request.priority,
target: request.target,
created_by_session: request.created_by_session,
created_at: Utc::now(),
working_dir: request.working_dir,
task_description: request.task_description,
relevant_files: request.relevant_files,
git_branch: request.git_branch,
additional_context: request.additional_context,
};
self.queue.push(item);
Ok(id)
}
pub fn state(&self) -> &AmbientState {
&self.state
}
pub fn queue(&self) -> &ScheduledQueue {
&self.queue
}
}
// ---------------------------------------------------------------------------
// Ambient System Prompt Builder
// ---------------------------------------------------------------------------
/// Health stats for the memory graph, used in the ambient system prompt.
#[derive(Debug, Clone, Default)]
pub struct MemoryGraphHealth {
pub total: usize,
pub active: usize,
pub inactive: usize,
pub low_confidence: usize,
pub contradictions: usize,
pub missing_embeddings: usize,
pub duplicate_candidates: usize,
pub last_consolidation: Option<DateTime<Utc>>,
}
/// Summary of a recent session for the ambient prompt.
#[derive(Debug, Clone)]
pub struct RecentSessionInfo {
pub id: String,
pub status: String,
pub topic: Option<String>,
pub duration_secs: i64,
pub extraction_status: String,
}
/// Resource budget info for the ambient prompt.
#[derive(Debug, Clone, Default)]
pub struct ResourceBudget {
pub provider: String,
pub tokens_remaining_desc: String,
pub window_resets_desc: String,
pub user_usage_rate_desc: String,
pub cycle_budget_desc: String,
}
/// Gather memory graph health stats from the MemoryManager.
pub fn gather_memory_graph_health(
memory_manager: &crate::memory::MemoryManager,
) -> MemoryGraphHealth {
let mut health = MemoryGraphHealth::default();
// Accumulate stats from project + global graphs
for graph in [
memory_manager.load_project_graph(),
memory_manager.load_global_graph(),
]
.into_iter()
.flatten()
{
let active_count = graph.memories.values().filter(|m| m.active).count();
let inactive_count = graph.memories.values().filter(|m| !m.active).count();
health.total += graph.memories.len();
health.active += active_count;
health.inactive += inactive_count;
// Low confidence: effective confidence < 0.1
health.low_confidence += graph
.memories
.values()
.filter(|m| m.active && m.effective_confidence() < 0.1)
.count();
// Missing embeddings
health.missing_embeddings += graph
.memories
.values()
.filter(|m| m.active && m.embedding.is_none())
.count();
// Count contradiction edges
for edges in graph.edges.values() {
for edge in edges {
if matches!(edge.kind, crate::memory_graph::EdgeKind::Contradicts) {
health.contradictions += 1;
}
}
}
// Use last_cluster_update as a proxy for last consolidation
if let Some(ts) = graph.metadata.last_cluster_update {
match health.last_consolidation {
Some(existing) if ts > existing => health.last_consolidation = Some(ts),
None => health.last_consolidation = Some(ts),
_ => {}
}
}
}
// Contradicts edges are bidirectional, so divide by 2
health.contradictions /= 2;
// Duplicate candidates would require embedding similarity scan;
// placeholder for now — ambient agent will discover them during its cycle.
health.duplicate_candidates = 0;
health
}
/// Gather feedback memories relevant to ambient mode.
///
/// Pulls from two sources:
/// 1. Recent ambient transcripts (summaries of past cycles)
/// 2. Memory graph entries tagged "ambient" or "system"
///
/// Returns formatted strings for inclusion in the ambient system prompt.
pub fn gather_feedback_memories(memory_manager: &crate::memory::MemoryManager) -> Vec<String> {
let mut feedback = Vec::new();
// --- Source 1: Recent ambient transcripts ---
let transcripts_dir = match crate::storage::jcode_dir() {
Ok(d) => d.join("ambient").join("transcripts"),
Err(_) => return feedback,
};
if transcripts_dir.exists()
&& let Ok(dir) = std::fs::read_dir(&transcripts_dir)
{
let mut files: Vec<_> = dir.flatten().collect();
// Sort by filename descending (most recent first)
files.sort_by_key(|entry| std::cmp::Reverse(entry.file_name()));
// Only look at the last 5 transcripts
files.truncate(5);
for entry in files {
if let Ok(content) = std::fs::read_to_string(entry.path())
&& let Ok(transcript) =
serde_json::from_str::<crate::safety::AmbientTranscript>(&content)
{
let status = format!("{:?}", transcript.status);
let summary = transcript.summary.as_deref().unwrap_or("no summary");
let age = format_duration_rough(Utc::now() - transcript.started_at);
feedback.push(format!(
"Past cycle ({} ago, {}): {} memories modified, {} compactions — {}",
age,
status.to_lowercase(),
transcript.memories_modified,
transcript.compactions,
summary,
));
}
}
}
// --- Source 2: Memory graph entries tagged "ambient" or "system" ---
for graph in [
memory_manager.load_project_graph(),
memory_manager.load_global_graph(),
]
.into_iter()
.flatten()
{
for memory in graph.memories.values() {
if !memory.active {
continue;
}
let has_ambient_tag = memory.tags.iter().any(|t| t == "ambient" || t == "system");
if has_ambient_tag {
feedback.push(format!("Memory [{}]: {}", memory.id, memory.content));
}
}
}
feedback
}
/// Gather recent sessions since a given timestamp.
pub fn gather_recent_sessions(since: Option<DateTime<Utc>>) -> Vec<RecentSessionInfo> {
let sessions_dir = match crate::storage::jcode_dir() {
Ok(d) => d.join("sessions"),
Err(_) => return Vec::new(),
};
if !sessions_dir.exists() {
return Vec::new();
}
let cutoff = since.unwrap_or_else(|| Utc::now() - chrono::Duration::hours(24));
let mut recent = Vec::new();
if let Ok(entries) = std::fs::read_dir(&sessions_dir) {
for entry in entries.flatten() {
let path = entry.path();
if path.extension().map(|e| e == "json").unwrap_or(false)
&& let Some(stem) = path.file_stem().and_then(|s| s.to_str())
&& let Ok(session) = crate::session::Session::load(stem)
{
// Skip debug sessions
if session.is_debug {
continue;
}
// Only include sessions updated after cutoff
if session.updated_at < cutoff {
continue;
}
let duration = (session.updated_at - session.created_at)
.num_seconds()
.max(0);
let extraction = if session.messages.is_empty() {
"no messages"
} else {
// Heuristic: if session closed normally, assume extracted
match &session.status {
crate::session::SessionStatus::Closed => "extracted",
crate::session::SessionStatus::Crashed { .. } => "missed",
crate::session::SessionStatus::Active => "in progress",
_ => "unknown",
}
};
recent.push(RecentSessionInfo {
id: session.id.clone(),
status: session.status.display().to_string(),
topic: session.title.clone(),
duration_secs: duration,
extraction_status: extraction.to_string(),
});
}
}
}
// Sort by most recent first (we don't have created_at easily, sort by id which embeds timestamp)
recent.sort_by(|a, b| b.id.cmp(&a.id));
recent.truncate(20); // Cap at 20 to keep prompt reasonable
recent
}
/// Build the dynamic system prompt for an ambient cycle.
///
/// Populates the template from AMBIENT_MODE.md with real data from the
/// current state, queue, memory graph, sessions, and resource budget.
pub fn build_ambient_system_prompt(
state: &AmbientState,
queue: &[ScheduledItem],
graph_health: &MemoryGraphHealth,
recent_sessions: &[RecentSessionInfo],
feedback_memories: &[String],
budget: &ResourceBudget,
active_user_sessions: usize,
) -> String {
let mut prompt = String::with_capacity(4096);
prompt.push_str(
"You are the ambient agent for jcode. You operate autonomously without \
user prompting. Your job is to maintain and improve the user's \
development environment.\n\n",
);
// --- Current State ---
prompt.push_str("## Current State\n");
if let Some(last_run) = state.last_run {
let ago = Utc::now() - last_run;
let ago_str = format_duration_rough(ago);
prompt.push_str(&format!(
"- Last ambient cycle: {} ({} ago)\n",
last_run.format("%Y-%m-%d %H:%M UTC"),
ago_str,
));
} else {
prompt.push_str("- Last ambient cycle: never (first run)\n");
}
if active_user_sessions > 0 {
prompt.push_str(&format!(
"- Active user sessions: {}\n",
active_user_sessions
));
} else {
prompt.push_str("- Active user sessions: none\n");
}
prompt.push_str(&format!(
"- Total cycles completed: {}\n",
state.total_cycles
));
prompt.push('\n');
// --- Scheduled Queue ---
prompt.push_str("## Scheduled Queue\n");
if queue.is_empty() {
prompt.push_str("Empty -- do general ambient work.\n");
} else {
for item in queue {
let age = Utc::now() - item.created_at;
let priority = match item.priority {
Priority::Low => "low",
Priority::Normal => "normal",
Priority::High => "HIGH",
};
prompt.push_str(&format!(
"- [{}] {} (scheduled {} ago, priority: {})\n",
item.id,
item.context,
format_duration_rough(age),
priority,
));
match &item.target {
ScheduleTarget::Ambient => {}
ScheduleTarget::Session { session_id } => {
prompt.push_str(&format!(" Target session: {}\n", session_id));
}
ScheduleTarget::Spawn { parent_session_id } => {
prompt.push_str(&format!(" Spawn from session: {}\n", parent_session_id));
}
}
if let Some(ref dir) = item.working_dir {
prompt.push_str(&format!(" Working dir: {}\n", dir));
}
if let Some(ref desc) = item.task_description {
prompt.push_str(&format!(" Details: {}\n", desc));
}
if !item.relevant_files.is_empty() {
prompt.push_str(&format!(" Files: {}\n", item.relevant_files.join(", ")));
}
if let Some(ref branch) = item.git_branch {
prompt.push_str(&format!(" Branch: {}\n", branch));
}
if let Some(ref ctx) = item.additional_context {
for line in ctx.lines() {
prompt.push_str(&format!(" {}\n", line));
}
}
}
}
prompt.push('\n');
// --- Recent Sessions ---
prompt.push_str("## Recent Sessions (since last cycle)\n");
if recent_sessions.is_empty() {
prompt.push_str("No sessions since last cycle.\n");
} else {
for s in recent_sessions {
let topic = s.topic.as_deref().unwrap_or("(no title)");
let dur = format_duration_rough(chrono::Duration::seconds(s.duration_secs));
prompt.push_str(&format!(
"- {} | {} | {} | {} | extraction: {}\n",
s.id, s.status, dur, topic, s.extraction_status,
));
}
}
prompt.push('\n');
// --- Memory Graph Health ---
prompt.push_str("## Memory Graph Health\n");
prompt.push_str(&format!(
"- Total memories: {} ({} active, {} inactive)\n",
graph_health.total, graph_health.active, graph_health.inactive,
));
prompt.push_str(&format!(
"- Memories with confidence < 0.1: {}\n",
graph_health.low_confidence,
));
prompt.push_str(&format!(
"- Unresolved contradictions: {}\n",
graph_health.contradictions,
));
prompt.push_str(&format!(
"- Memories without embeddings: {}\n",
graph_health.missing_embeddings,
));
if graph_health.duplicate_candidates > 0 {
prompt.push_str(&format!(
"- Duplicate candidates (similarity > 0.95): {}\n",
graph_health.duplicate_candidates,
));
} else {
prompt.push_str("- Duplicate candidates: run embedding scan to detect\n");
}
if let Some(ts) = graph_health.last_consolidation {
let ago = format_duration_rough(Utc::now() - ts);
prompt.push_str(&format!("- Last consolidation: {} ago\n", ago));
} else {
prompt.push_str("- Last consolidation: never\n");
}
prompt.push('\n');
// --- User Feedback History ---
prompt.push_str("## User Feedback History\n");
if feedback_memories.is_empty() {
prompt.push_str("No feedback memories found about ambient mode yet.\n");
} else {
for mem in feedback_memories {
prompt.push_str(&format!("- {}\n", mem));
}
}
prompt.push('\n');
// --- Resource Budget ---
prompt.push_str("## Resource Budget\n");
prompt.push_str(&format!("- Provider: {}\n", budget.provider));
prompt.push_str(&format!(
"- Tokens remaining in window: {}\n",
budget.tokens_remaining_desc,
));
prompt.push_str(&format!("- Window resets: {}\n", budget.window_resets_desc));
prompt.push_str(&format!(
"- User usage rate: {}\n",
budget.user_usage_rate_desc,
));
prompt.push_str(&format!(
"- Budget for this cycle: {}\n",
budget.cycle_budget_desc,
));
prompt.push('\n');
// --- User Directives (from email/Telegram replies) ---
let pending_directives = take_pending_directives();
if !pending_directives.is_empty() {
prompt.push_str("## User Directives (from replies)\n");
prompt.push_str(
"The user replied to ambient notifications with these instructions. \
Address them as your **top priority** this cycle.\n\n",
);
for dir in &pending_directives {
let ago = format_duration_rough(Utc::now() - dir.received_at);
prompt.push_str(&format!(
"- [reply to cycle {}] ({} ago): {}\n",
dir.in_reply_to_cycle, ago, dir.text,
));
}
prompt.push('\n');
}
// --- Instructions ---
prompt.push_str(
"## Instructions\n\n\
Start by using the todos tool to plan what you'll do this cycle.\n\n\
Priority order:\n\
1. Execute any scheduled queue items first.\n\
2. Garden the memory graph -- consolidate duplicates, resolve \
contradictions, prune dead memories, verify stale facts, \
extract from missed sessions.\n\
3. Scout for proactive work (only if enabled and past cold start) -- \
look at recent sessions and git history to identify useful work \
the user would appreciate.\n\n\
For gardening: focus on highest-value maintenance first. Duplicates \
and contradictions before pruning. Verify stale facts only if you \