ai-agent 0.88.0

Idiomatic agent sdk inspired by the claude code source leak
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
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
// Source: ~/claudecode/openclaudecode/src/services/SessionMemory/sessionMemory.ts
//! Session memory extraction via post-sampling hook.
//!
//! Automatically maintains a markdown file with notes about the current conversation.
//! Runs periodically in the background using a forked subagent to extract key information
//! without interrupting the main conversation flow.

use std::collections::HashMap;
use std::sync::{Arc, Mutex};

use crate::constants::tools::FILE_EDIT_TOOL_NAME;
use crate::services::compact::auto_compact::is_auto_compact_enabled;
use crate::types::Message;
use crate::utils::file_state_cache::{
    DEFAULT_MAX_CACHE_SIZE_BYTES, FileStateCache, READ_FILE_STATE_CACHE_SIZE,
};
use crate::utils::forked_agent::{
    CanUseToolFn as ForkCanUseToolFn, PermissionDecision, QuerySource,
    create_cache_safe_params, run_forked_agent, SubagentContextOverrides,
};
use crate::utils::hooks::post_sampling_hooks::{
    PostSamplingHook, ReplHookContext, register_post_sampling_hook,
};

// Re-export utility functions for external callers
use super::session_memory_utils::*;
use super::prompts::*;

/// Module-level state for tracking the last memory extraction boundary.
static LAST_MEMORY_MESSAGE_UUID: Mutex<Option<String>> = Mutex::new(None);

/// Reset the last memory message UUID (for testing)
pub fn reset_last_memory_message_uuid() {
    *LAST_MEMORY_MESSAGE_UUID.lock().unwrap() = None;
}

// ============================================================================
// Token estimation
// ============================================================================

/// Estimate total token count from API [Message] array.
///
/// Uses a rough character-based estimation (4 chars per token).
/// This works with `crate::types::Message` (the API message struct).
fn estimate_message_token_count(messages: &[Message]) -> u64 {
    messages.iter().map(rough_token_estimate).sum()
}

/// Rough token estimate for a single API [Message].
fn rough_token_estimate(msg: &Message) -> u64 {
    let content_len = msg.content.len() as u64;
    let tool_calls_len = msg
        .tool_calls
        .as_ref()
        .map(|tc| tc.len() as u64 * 20)
        .unwrap_or(0);
    (content_len / 4 + tool_calls_len).max(1)
}

// ============================================================================
// Tool call counting
// ============================================================================

/// Count tool calls in assistant messages since a given index.
pub fn count_tool_calls_since(messages: &[Message], since_index: Option<usize>) -> usize {
    let start = since_index.map(|i| i + 1).unwrap_or(0);
    messages[start..]
        .iter()
        .filter(|m| m.role == crate::types::MessageRole::Assistant)
        .flat_map(|m| m.tool_calls.as_ref())
        .map(|tc| tc.len())
        .sum()
}

/// Count tool calls in assistant messages after the given UUID.
/// If `since_uuid` is None, counts all assistant tool calls.
fn count_tool_calls_since_uuid(messages: &[Message], since_uuid: Option<&str>) -> usize {
    let mut tool_call_count = 0;
    let mut found_start = since_uuid.is_none();

    for message in messages {
        if !found_start {
            if message.uuid.as_deref() == since_uuid {
                found_start = true;
            }
            continue;
        }
        if message.role == crate::types::MessageRole::Assistant {
            if let Some(ref tc) = message.tool_calls {
                tool_call_count += tc.len();
            }
        }
    }
    tool_call_count
}

/// Find the index of the last assistant message.
fn find_last_assistant_index(messages: &[Message]) -> Option<usize> {
    messages
        .iter()
        .rposition(|m| m.role == crate::types::MessageRole::Assistant)
}

// ============================================================================
// shouldExtractMemory
// ============================================================================

/// Check if session memory should be extracted based on current conversation state.
pub fn should_extract_memory(messages: &[Message]) -> bool {
    let current_token_count = estimate_message_token_count(messages);

    if !is_session_memory_initialized() {
        if !has_met_initialization_threshold(current_token_count) {
            return false;
        }
        mark_session_memory_initialized();
    }

    let has_met_token_threshold = has_met_update_threshold(current_token_count);

    // Count tool calls since last extraction (matches TS: countToolCallsSince(messages, lastMemoryMessageUuid))
    let last_uuid = LAST_MEMORY_MESSAGE_UUID.lock().unwrap().clone();
    let tool_calls_since = count_tool_calls_since_uuid(messages, last_uuid.as_deref());
    let has_met_tool_call_threshold =
        tool_calls_since >= get_tool_calls_between_updates() as usize;

    // Check if last assistant turn has tool calls
    let has_tool_calls_in_last_turn = messages.iter().rev().any(|m| {
        if m.role == crate::types::MessageRole::Assistant {
            return m.tool_calls.as_ref().map(|tc| !tc.is_empty()).unwrap_or(false);
        }
        false
    });

    // Trigger extraction when:
    // 1. Both thresholds are met (tokens AND tool calls), OR
    // 2. No tool calls in last turn AND token threshold is met
    (has_met_token_threshold && has_met_tool_call_threshold)
        || (has_met_token_threshold && !has_tool_calls_in_last_turn)
}

// ============================================================================
// setupSessionMemoryFile
// ============================================================================

/// Set up the session memory file (create if needed) and read current contents.
async fn setup_session_memory_file() -> Result<(String, String), String> {
    let memory_dir =
        crate::utils::permissions::filesystem::get_session_memory_dir();
    let memory_path =
        crate::utils::permissions::filesystem::get_session_memory_path();

    // Create directory
    std::fs::create_dir_all(&memory_dir)
        .map_err(|e| format!("Failed to create session memory directory: {e}"))?;

    // Create file if it doesn't exist
    let file_path = std::path::Path::new(&memory_path);
    if !file_path.exists() {
        let template = load_session_memory_template();
        std::fs::write(file_path, template.as_bytes())
            .map_err(|e| format!("Failed to create session memory file: {e}"))?;
    }

    // Read current contents
    let current_memory =
        std::fs::read_to_string(file_path).unwrap_or_default();

    log::debug!(
        "Session memory file read: {} characters",
        current_memory.len()
    );

    Ok((memory_path, current_memory))
}

// ============================================================================
// createMemoryFileCanUseTool
// ============================================================================

/// Creates a can_use_tool function that only allows Edit for the exact memory file.
pub fn create_memory_file_can_use_tool(
    memory_path: String,
) -> Arc<ForkCanUseToolFn> {
    Arc::new(move |tool_def: &serde_json::Value,
              input: &serde_json::Value,
              _context: Arc<crate::tool::ToolUseContext>,
              _assistant: Arc<crate::types::message::AssistantMessage>,
              _query_source: &str,
              _is_explicit: bool| {
            let memory_path = memory_path.clone();
            let tool_name = tool_def
                .get("name")
                .and_then(|n| n.as_str())
                .unwrap_or("")
                .to_string();
            let is_edit = tool_name == FILE_EDIT_TOOL_NAME;
            let input_file_path = input
                .get("file_path")
                .and_then(|p| p.as_str())
                .map(|s| s.to_string());
            Box::pin(async move {
                if is_edit {
                    if let Some(ref fp) = input_file_path {
                        if fp == memory_path.as_str() {
                            return Ok(PermissionDecision::Allow);
                        }
                    }
                }

                Ok(PermissionDecision::Deny {
                    reason: Some(format!(
                        "only {FILE_EDIT_TOOL_NAME} on {memory_path} is allowed"
                    )),
                })
            })
        },
    )
}

// ============================================================================
// updateLastSummarizedMessageIdIfSafe
// ============================================================================

fn update_last_summarized_message_id_if_safe(messages: &[Message]) {
    let has_tool_calls = messages.iter().rev().any(|m| {
        if m.role == crate::types::MessageRole::Assistant {
            return m.tool_calls.as_ref().map(|tc| !tc.is_empty()).unwrap_or(false);
        }
        false
    });

    // Only set last summarized message ID if last assistant turn has no tool calls
    // (avoids orphaned tool_results)
    if !has_tool_calls {
        if let Some(last_msg) = messages.last() {
            if let Some(ref uuid) = last_msg.uuid {
                set_last_summarized_message_id(Some(uuid.as_str()));
            }
        }
    }
}

// ============================================================================
// Core extraction
// ============================================================================

async fn do_session_memory_extraction(
    context: &ReplHookContext,
) -> Result<(), String> {
    mark_extraction_started();

    // Set up file system and read current state
    let (memory_path, current_memory) = setup_session_memory_file().await?;

    // Create extraction message
    let user_prompt = build_session_memory_update_prompt(
        &current_memory,
        &memory_path,
    );

    // Store memory path for future reference
    set_session_memory_path(memory_path.clone());

    // Create a ToolUseContext for cache-safe params.
    // The hook context only carries a lightweight ToolUseContext,
    // so we use a stub. The forked agent is currently a stub itself,
    // so passing empty messages is sufficient.
    let tool_use_context = crate::tool::ToolUseContext::stub();

    let cache_safe_params = create_cache_safe_params(
        context.system_prompt.join("\n"),
        context.user_context.clone(),
        context.system_context.clone(),
        Arc::new(tool_use_context),
        Vec::<crate::types::message::Message>::new(),
    );

    // Run session memory extraction using runForkedAgent
    let prompt_user_msg = crate::types::message::UserMessage {
        base: crate::types::message::MessageBase {
            uuid: Some(uuid::Uuid::new_v4().to_string()),
            ..Default::default()
        },
        message_type: "user".to_string(),
        message: crate::types::message::UserMessageContent {
            content: crate::types::message::UserContent::Text(user_prompt),
            extra: std::collections::HashMap::new(),
        },
    };
    let prompt_messages =
        vec![crate::types::message::Message::User(prompt_user_msg)];

    // Create fresh file state cache for the subagent
    let fresh_cache = Arc::new(FileStateCache::new(
        READ_FILE_STATE_CACHE_SIZE,
        DEFAULT_MAX_CACHE_SIZE_BYTES,
    ));

    run_forked_agent(crate::utils::forked_agent::ForkedAgentConfig {
        prompt_messages,
        cache_safe_params,
        can_use_tool: create_memory_file_can_use_tool(memory_path.clone()),
        query_source: QuerySource("session_memory".to_string()),
        fork_label: "session_memory".to_string(),
        overrides: Some(SubagentContextOverrides {
            read_file_state: Some(fresh_cache),
            ..SubagentContextOverrides::default()
        }),
        max_output_tokens: None,
        max_turns: None,
        on_message: None,
        skip_transcript: true,
        skip_cache_write: true,
    })
    .await?;

    // Record the context size at extraction
    let token_count = estimate_message_token_count(&context.messages);
    record_extraction_token_count(token_count);

    update_last_summarized_message_id_if_safe(&context.messages);

    mark_extraction_completed();

    Ok(())
}

// ============================================================================
// Post-sampling hook (sequential wrapper)
// ============================================================================

/// Mutex to prevent concurrent session memory extractions.
static EXTRACTION_LOCK: Mutex<bool> = Mutex::new(false);

fn create_extraction_hook() -> PostSamplingHook {
    fn extract_session_memory_hook(
        ctx: ReplHookContext,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send>> {
        Box::pin(async move {
            // Only run on main REPL thread
            if ctx.query_source.as_deref() != Some("repl_main_thread") {
                return;
            }

            // Prevent concurrent extractions - acquire lock
            let locked = {
                let mut lock = match EXTRACTION_LOCK.lock() {
                    Ok(l) => l,
                    Err(poisoned) => poisoned.into_inner(),
                };
                if *lock {
                    return;
                }
                *lock = true;
                true
            };

            if !locked {
                return;
            }

            let result = do_session_memory_extraction(&ctx).await;

            // Release lock
            if let Ok(mut l) = EXTRACTION_LOCK.lock() {
                *l = false;
            }

            if let Err(e) = result {
                log::warn!("Session memory extraction failed: {e}");
            }
        })
    }

    Arc::new(extract_session_memory_hook)
}

// ============================================================================
// initSessionMemory
// ============================================================================

/// Initialize session memory by registering the post-sampling hook.
pub fn init_session_memory() {
    if !is_auto_compact_enabled() {
        return;
    }

    register_post_sampling_hook(create_extraction_hook());
}

// ============================================================================
// get_session_memory
// ============================================================================

/// Read the current session memory notes from disk (sync).
pub fn get_session_memory() -> Option<String> {
    let memory_path = get_session_memory_path()?;
    let content = std::fs::read_to_string(&memory_path).ok()?;
    if content.is_empty() {
        return None;
    }
    Some(content)
}

/// Async version for callers that need an async Result.
pub async fn get_session_memory_content() -> Result<Option<String>, crate::AgentError> {
    match get_session_memory() {
        Some(content) => Ok(Some(content)),
        None => Ok(None),
    }
}

/// Initialize session memory file with template on disk.
pub async fn init_session_memory_file() -> Result<String, crate::AgentError> {
    let memory_dir =
        crate::utils::permissions::filesystem::get_session_memory_dir();
    let memory_path =
        crate::utils::permissions::filesystem::get_session_memory_path();

    tokio::fs::create_dir_all(&memory_dir)
        .await
        .map_err(crate::AgentError::Io)?;

    let file_path = std::path::Path::new(&memory_path);
    if !file_path.exists() {
        let template = load_session_memory_template();
        tokio::fs::write(&file_path, template)
            .await
            .map_err(crate::AgentError::Io)?;
    }

    set_session_memory_path(memory_path.clone());

    tokio::fs::read_to_string(&file_path)
        .await
        .map_err(crate::AgentError::Io)
}

// ============================================================================
// manuallyExtractSessionMemory
// ============================================================================

/// Result of a manual session memory extraction.
#[derive(Debug, Clone)]
pub struct ManualExtractionResult {
    pub success: bool,
    pub memory_path: Option<String>,
    pub error: Option<String>,
}

/// Manually trigger session memory extraction, bypassing threshold checks.
pub async fn manually_extract_session_memory(
    messages: &[Message],
    _tool_use_context: Arc<crate::tool::ToolUseContext>,
) -> ManualExtractionResult {
    if messages.is_empty() {
        return ManualExtractionResult {
            success: false,
            memory_path: None,
            error: Some("No messages to summarize".to_string()),
        };
    }

    mark_extraction_started();

    let context = ReplHookContext {
        messages: messages.to_vec(),
        system_prompt: vec![],
        user_context: HashMap::new(),
        system_context: HashMap::new(),
        tool_use_context: Arc::new(
            crate::utils::hooks::can_use_tool::ToolUseContext {
                session_id: "manual_extraction".to_string(),
                cwd: None,
                is_non_interactive_session: false,
                options: None,
            },
        ),
        query_source: Some("manual_extraction".to_string()),
        query_message_count: Some(messages.len()),
    };

    match do_session_memory_extraction(&context).await {
        Ok(()) => ManualExtractionResult {
            success: true,
            memory_path: get_session_memory_path(),
            error: None,
        },
        Err(e) => {
            mark_extraction_completed();
            ManualExtractionResult {
                success: false,
                memory_path: None,
                error: Some(e),
            }
        }
    }
}

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

    fn make_assistant_message(
        tool_calls: Option<Vec<crate::types::ToolCall>>,
    ) -> Message {
        Message {
            role: crate::types::MessageRole::Assistant,
            content: "thinking...".to_string(),
            attachments: None,
            tool_call_id: None,
            tool_calls,
            is_error: None,
            is_meta: None,
            is_api_error_message: None,
            error_details: None,
            uuid: None,
        }
    }

    fn make_user_message(text: &str) -> Message {
        Message {
            role: crate::types::MessageRole::User,
            content: text.to_string(),
            attachments: None,
            tool_call_id: None,
            tool_calls: None,
            is_error: None,
            is_meta: None,
            is_api_error_message: None,
            error_details: None,
            uuid: None,
        }
    }

    #[test]
    fn test_rough_token_estimate() {
        let msg = make_user_message("hello world");
        let est = rough_token_estimate(&msg);
        assert!(est > 0);
    }

    #[test]
    fn test_token_count_no_tool_calls() {
        let messages = vec![
            make_user_message("hello"),
            make_assistant_message(None),
        ];
        let count = estimate_message_token_count(&messages);
        assert!(count > 0);
    }

    #[test]
    fn test_token_count_with_tool_calls() {
        let tool_call = crate::types::ToolCall {
            id: "tc_1".to_string(),
            r#type: "function".to_string(),
            name: "Edit".to_string(),
            arguments: serde_json::json!({"file_path": "/tmp/test.rs"}),
        };
        let messages = vec![
            make_user_message("hello"),
            make_assistant_message(Some(vec![tool_call])),
        ];
        let count = estimate_message_token_count(&messages);
        assert!(count > 0);
    }

    #[test]
    fn test_count_tool_calls_since() {
        let tool_call = crate::types::ToolCall {
            id: "tc_1".to_string(),
            r#type: "function".to_string(),
            name: "Edit".to_string(),
            arguments: serde_json::json!({}),
        };
        let messages = vec![
            make_user_message("hello"),
            make_assistant_message(Some(vec![tool_call.clone(), tool_call.clone()])),
            make_user_message("result"),
            make_assistant_message(Some(vec![tool_call])),
        ];
        assert_eq!(count_tool_calls_since(&messages, None), 3);
        assert_eq!(count_tool_calls_since(&messages, Some(2)), 1);
    }

    #[test]
    fn test_should_extract_memory_initial_threshold() {
        super::reset_session_memory_state();
        reset_last_memory_message_uuid();

        let small_messages = vec![make_user_message("hi")];
        assert!(!should_extract_memory(&small_messages));
    }

    #[test]
    fn test_has_tool_calls_in_last_turn() {
        let tool_call = crate::types::ToolCall {
            id: "tc_1".to_string(),
            r#type: "function".to_string(),
            name: "Edit".to_string(),
            arguments: serde_json::json!({}),
        };

        let messages = vec![
            make_user_message("hello"),
            make_assistant_message(Some(vec![tool_call])),
        ];
        let has_calls = messages.iter().rev().any(|m| {
            if m.role == crate::types::MessageRole::Assistant {
                return m.tool_calls.as_ref().map(|tc| !tc.is_empty()).unwrap_or(false);
            }
            false
        });
        assert!(has_calls);

        let messages = vec![
            make_user_message("hello"),
            make_assistant_message(None),
        ];
        let has_calls = messages.iter().rev().any(|m| {
            if m.role == crate::types::MessageRole::Assistant {
                return m.tool_calls.as_ref().map(|tc| !tc.is_empty()).unwrap_or(false);
            }
            false
        });
        assert!(!has_calls);
    }

    #[test]
    fn test_reset_last_memory_message_uuid() {
        *LAST_MEMORY_MESSAGE_UUID.lock().unwrap() =
            Some("test-uuid".to_string());
        reset_last_memory_message_uuid();
        assert!(LAST_MEMORY_MESSAGE_UUID.lock().unwrap().is_none());
    }
}