rsclaw 2026.5.1

AI Agent Engine Compatible with OpenClaw
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
//! Workspace file loading (AGENTS.md §14 + §20).
//!
//! Loads the standard workspace markdown files into a `WorkspaceContext`
//! struct that is merged into the system prompt.
//!
//! File loading rules:
//!   - Missing files inject a `[<FILE>.md not found — using defaults]` marker.
//!   - Per-file character limit: `bootstrap_max_chars` (default 20 000).
//!   - Total character limit:    `bootstrap_total_max_chars` (default 150 000).
//!   - Load order follows AGENTS.md §20 "System Prompt assembly".

use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::time::SystemTime;

use chrono::Local;
use tracing::debug;

/// Default per-file character limit.
pub const DEFAULT_MAX_CHARS_PER_FILE: usize = 10_000;
/// Default total character limit across all workspace files.
pub const DEFAULT_TOTAL_MAX_CHARS: usize = 50_000;

// ---------------------------------------------------------------------------
// SessionType
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SessionType {
    /// Regular user DM or group message.
    Normal,
    /// Scheduled heartbeat run.
    Heartbeat,
    /// First run after gateway restart.
    Boot,
    /// First ever run on a brand-new workspace (BOOTSTRAP.md present).
    Bootstrap,
}

// ---------------------------------------------------------------------------
// WorkspaceContext
// ---------------------------------------------------------------------------

/// Contents of the workspace markdown files, ready to be injected into
/// the system prompt.
#[derive(Debug, Clone, Default)]
pub struct WorkspaceContext {
    /// AGENTS.md — loaded every session.
    pub agents_md: Option<String>,
    /// SOUL.md — loaded every session.
    pub soul_md: Option<String>,
    /// USER.md — loaded every session.
    pub user_md: Option<String>,
    /// IDENTITY.md — loaded every session.
    pub identity_md: Option<String>,
    /// TOOLS.md — loaded every session.
    pub tools_md: Option<String>,
    /// HEARTBEAT.md — loaded only during heartbeat runs.
    pub heartbeat_md: Option<String>,
    /// BOOT.md — loaded only after gateway restart.
    pub boot_md: Option<String>,
    /// BOOTSTRAP.md — loaded only on a brand-new workspace.
    pub bootstrap_md: Option<String>,
    /// MEMORY.md — loaded only in private (DM) sessions.
    pub memory_long: Option<String>,
    /// `memory/YYYY-MM-DD.md` — today's daily memory log.
    pub memory_today: Option<String>,
    /// `memory/YYYY-MM-DD.md` — yesterday's daily memory log.
    pub memory_yesterday: Option<String>,
    /// Workspace root directory.
    pub workspace_dir: PathBuf,
}

impl WorkspaceContext {
    /// Load workspace files from `workspace`.
    ///
    /// `session_type` controls which optional files are loaded.
    /// `is_private` controls whether `MEMORY.md` is loaded.
    ///
    /// If a file is missing from the agent's workspace, it falls back to
    /// the default workspace (inheritance). Pass `None` for default agents.
    pub fn load(
        workspace: &Path,
        session_type: SessionType,
        is_private: bool,
        max_chars_per_file: usize,
        total_max_chars: usize,
    ) -> Self {
        // Determine fallback workspace: the default "workspace" directory.
        // Only used if `workspace` is an agent-specific directory (workspace-xxx).
        let fallback = {
            let default_ws = crate::config::loader::base_dir().join("workspace");
            if workspace != default_ws && default_ws.exists() {
                Some(default_ws)
            } else {
                None
            }
        };

        let mut ctx = WorkspaceContext {
            workspace_dir: workspace.to_path_buf(),
            ..Default::default()
        };

        let mut total_chars: usize = 0;

        // Helper macro: read a file, apply per-file limit, accumulate total.
        // Falls back to the default workspace if not found in the agent's workspace.
        macro_rules! load_file {
            ($field:ident, $filename:expr) => {{
                if total_chars < total_max_chars {
                    let content = read_workspace_file(
                        workspace,
                        $filename,
                        max_chars_per_file,
                        &mut total_chars,
                        total_max_chars,
                    );
                    let content = if content.is_empty() {
                        if let Some(ref fb) = fallback {
                            read_workspace_file(
                                fb,
                                $filename,
                                max_chars_per_file,
                                &mut total_chars,
                                total_max_chars,
                            )
                        } else {
                            content
                        }
                    } else {
                        content
                    };
                    if !content.is_empty() {
                        ctx.$field = Some(content);
                    }
                }
            }};
        }

        // Always-loaded files.
        load_file!(agents_md, "AGENTS.md");
        load_file!(soul_md, "SOUL.md");
        load_file!(user_md, "USER.md");
        load_file!(identity_md, "IDENTITY.md");
        load_file!(tools_md, "TOOLS.md");

        // Conditional files.
        if session_type == SessionType::Heartbeat {
            load_file!(heartbeat_md, "HEARTBEAT.md");
        }
        if session_type == SessionType::Boot {
            load_file!(boot_md, "BOOT.md");
        }
        if session_type == SessionType::Bootstrap {
            load_file!(bootstrap_md, "BOOTSTRAP.md");
        }
        if is_private {
            load_file!(memory_long, "MEMORY.md");
        }

        // Daily memory logs.
        let today = Local::now().format("%Y-%m-%d").to_string();
        let yesterday = (Local::now() - chrono::Duration::days(1))
            .format("%Y-%m-%d")
            .to_string();

        ctx.memory_today = read_optional_file(
            &workspace.join("memory").join(format!("{today}.md")),
            max_chars_per_file,
            &mut total_chars,
            total_max_chars,
        );
        ctx.memory_yesterday = read_optional_file(
            &workspace.join("memory").join(format!("{yesterday}.md")),
            max_chars_per_file,
            &mut total_chars,
            total_max_chars,
        );

        debug!(
            workspace = %workspace.display(),
            total_chars,
            "workspace context loaded"
        );

        ctx
    }

    /// Build the system-prompt segment from loaded workspace files.
    /// Each non-None file is wrapped with a header.
    pub fn to_prompt_segment(&self) -> String {
        let mut parts: Vec<String> = Vec::new();

        macro_rules! append {
            ($field:expr, $label:expr) => {
                if let Some(content) = &$field {
                    parts.push(format!("## {}\n\n{content}", $label));
                }
            };
        }

        append!(self.agents_md, "AGENTS.md");
        append!(self.soul_md, "SOUL.md");
        append!(self.identity_md, "IDENTITY.md");
        append!(self.user_md, "USER.md");
        append!(self.tools_md, "TOOLS.md");
        append!(self.heartbeat_md, "HEARTBEAT.md");
        append!(self.boot_md, "BOOT.md");
        append!(self.bootstrap_md, "BOOTSTRAP.md");
        append!(self.memory_long, "MEMORY.md");
        append!(self.memory_today, "Memory (today)");
        append!(self.memory_yesterday, "Memory (yesterday)");

        parts.join("\n\n---\n\n")
    }
}

// ---------------------------------------------------------------------------
// Workspace cache (P1 context optimisation)
// ---------------------------------------------------------------------------

/// In-memory cache for workspace files. Re-reads only files whose mtime
/// has changed since the last load, avoiding redundant disk IO every turn.
#[derive(Debug)]
pub struct WorkspaceCache {
    /// Cached file contents keyed by filename.
    entries: HashMap<String, CacheEntry>,
    /// Last fully-assembled `WorkspaceContext`.
    cached_ctx: Option<WorkspaceContext>,
    /// Workspace root.
    workspace: PathBuf,
}

#[derive(Debug, Clone)]
struct CacheEntry {
    content: String,
    mtime: Option<SystemTime>,
}

impl WorkspaceCache {
    pub fn new(workspace: &Path) -> Self {
        Self {
            entries: HashMap::new(),
            cached_ctx: None,
            workspace: workspace.to_path_buf(),
        }
    }

    /// Return a `WorkspaceContext`, reloading only files whose mtime changed.
    pub fn load(
        &mut self,
        session_type: SessionType,
        is_private: bool,
        max_chars_per_file: usize,
        total_max_chars: usize,
    ) -> WorkspaceContext {
        let mut total_chars: usize = 0;
        let mut ctx = WorkspaceContext {
            workspace_dir: self.workspace.clone(),
            ..Default::default()
        };

        macro_rules! load_cached {
            ($field:ident, $filename:expr) => {{
                if total_chars < total_max_chars {
                    let content = self.read_cached(
                        $filename,
                        max_chars_per_file,
                        &mut total_chars,
                        total_max_chars,
                    );
                    if !content.is_empty() {
                        ctx.$field = Some(content);
                    }
                }
            }};
        }

        load_cached!(agents_md, "AGENTS.md");
        load_cached!(soul_md, "SOUL.md");
        load_cached!(user_md, "USER.md");
        load_cached!(identity_md, "IDENTITY.md");
        load_cached!(tools_md, "TOOLS.md");

        if session_type == SessionType::Heartbeat {
            load_cached!(heartbeat_md, "HEARTBEAT.md");
        }
        if session_type == SessionType::Boot {
            load_cached!(boot_md, "BOOT.md");
        }
        if session_type == SessionType::Bootstrap {
            load_cached!(bootstrap_md, "BOOTSTRAP.md");
        }
        if is_private {
            load_cached!(memory_long, "MEMORY.md");
        }

        // Daily memory logs (always re-read since date changes).
        let today = Local::now().format("%Y-%m-%d").to_string();
        let yesterday = (Local::now() - chrono::Duration::days(1))
            .format("%Y-%m-%d")
            .to_string();

        ctx.memory_today = read_optional_file(
            &self.workspace.join("memory").join(format!("{today}.md")),
            max_chars_per_file,
            &mut total_chars,
            total_max_chars,
        );
        ctx.memory_yesterday = read_optional_file(
            &self
                .workspace
                .join("memory")
                .join(format!("{yesterday}.md")),
            max_chars_per_file,
            &mut total_chars,
            total_max_chars,
        );

        debug!(
            workspace = %self.workspace.display(),
            total_chars,
            "workspace context loaded (cached)"
        );

        self.cached_ctx = Some(ctx.clone());
        ctx
    }

    /// Read a file, returning cached content if mtime hasn't changed.
    fn read_cached(
        &mut self,
        filename: &str,
        max_chars: usize,
        total_chars: &mut usize,
        total_max: usize,
    ) -> String {
        let path = self.workspace.join(filename);
        let current_mtime = std::fs::metadata(&path)
            .ok()
            .and_then(|m| m.modified().ok());

        // Check if cache is still valid.
        if let Some(entry) = self.entries.get(filename) {
            if entry.mtime == current_mtime && current_mtime.is_some() {
                let remaining = total_max.saturating_sub(*total_chars);
                let limit = max_chars.min(remaining);
                let truncated = truncate_chars(&entry.content, limit);
                *total_chars += truncated.len();
                return truncated.to_owned();
            }
        }

        // Cache miss or mtime changed -- re-read from disk.
        let content = match std::fs::read_to_string(&path) {
            Ok(c) => c,
            Err(_) => String::new(),
        };

        self.entries.insert(
            filename.to_owned(),
            CacheEntry {
                content: content.clone(),
                mtime: current_mtime,
            },
        );

        let remaining = total_max.saturating_sub(*total_chars);
        let limit = max_chars.min(remaining);
        let truncated = truncate_chars(&content, limit);
        *total_chars += truncated.len();
        truncated.to_owned()
    }
}

// ---------------------------------------------------------------------------
// File reading helpers
// ---------------------------------------------------------------------------

/// Read a workspace file, truncating to `max_chars`. If missing, return
/// the standard placeholder string.
fn read_workspace_file(
    workspace: &Path,
    filename: &str,
    max_chars: usize,
    total_chars: &mut usize,
    total_max: usize,
) -> String {
    let path = workspace.join(filename);
    match std::fs::read_to_string(&path) {
        Ok(content) => {
            let remaining = total_max.saturating_sub(*total_chars);
            let limit = max_chars.min(remaining);
            let truncated = truncate_chars(&content, limit);
            *total_chars += truncated.len();
            truncated.to_owned()
        }
        Err(_) => String::new(),
    }
}

/// Read an optional file (daily memory). Returns `None` if the file does
/// not exist or the total budget is exhausted.
fn read_optional_file(
    path: &Path,
    max_chars: usize,
    total_chars: &mut usize,
    total_max: usize,
) -> Option<String> {
    if !path.exists() || *total_chars >= total_max {
        return None;
    }
    let content = std::fs::read_to_string(path).ok()?;
    let remaining = total_max.saturating_sub(*total_chars);
    let limit = max_chars.min(remaining);
    let truncated = truncate_chars(&content, limit).to_owned();
    *total_chars += truncated.len();
    Some(truncated)
}

/// Truncate a string to at most `max_chars` Unicode characters.
fn truncate_chars(s: &str, max_chars: usize) -> &str {
    if s.len() <= max_chars {
        return s;
    }
    // Find the byte offset of the `max_chars`-th char boundary.
    match s.char_indices().nth(max_chars) {
        Some((idx, _)) => &s[..idx],
        None => s,
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use std::fs;

    use super::*;

    fn setup_workspace(dir: &Path) {
        fs::write(dir.join("AGENTS.md"), "# Agents\nDo stuff.").expect("agents");
        fs::write(dir.join("SOUL.md"), "# Soul\nBe helpful.").expect("soul");
        fs::write(dir.join("USER.md"), "# User\nJane.").expect("user");
        fs::write(dir.join("IDENTITY.md"), "# Identity\nBot.").expect("identity");
        fs::write(dir.join("TOOLS.md"), "# Tools\nNone.").expect("tools");
    }

    #[test]
    fn loads_standard_files() {
        let tmp = tempfile::tempdir().expect("tempdir");
        setup_workspace(tmp.path());

        let ctx = WorkspaceContext::load(
            tmp.path(),
            SessionType::Normal,
            false,
            DEFAULT_MAX_CHARS_PER_FILE,
            DEFAULT_TOTAL_MAX_CHARS,
        );

        assert!(ctx.agents_md.as_deref().unwrap_or("").contains("Agents"));
        assert!(ctx.soul_md.as_deref().unwrap_or("").contains("Soul"));
        assert!(
            ctx.heartbeat_md.is_none(),
            "heartbeat not loaded in Normal mode"
        );
    }

    #[test]
    fn missing_file_gets_placeholder() {
        // Use a "workspace" sub-dir inside tempdir so that base_dir()
        // fallback won't find the real ~/.rsclaw/workspace/SOUL.md.
        let tmp = tempfile::tempdir().expect("tempdir");
        let ws = tmp.path().join("workspace");
        fs::create_dir_all(&ws).expect("mkdir");
        // Write only AGENTS.md; others should get placeholders.
        fs::write(ws.join("AGENTS.md"), "# Agents").expect("agents");

        // Temporarily override RSCLAW_BASE_DIR so base_dir() resolves inside
        // the temp directory, preventing fallback to the real workspace.
        let orig = std::env::var("RSCLAW_BASE_DIR").ok();
        // SAFETY: test is single-threaded for this env var access.
        unsafe { std::env::set_var("RSCLAW_BASE_DIR", tmp.path()); }

        let ctx = WorkspaceContext::load(
            &ws,
            SessionType::Normal,
            false,
            DEFAULT_MAX_CHARS_PER_FILE,
            DEFAULT_TOTAL_MAX_CHARS,
        );

        // Restore env.
        // SAFETY: test is single-threaded for this env var access.
        unsafe {
            match orig {
                Some(v) => std::env::set_var("RSCLAW_BASE_DIR", v),
                None => std::env::remove_var("RSCLAW_BASE_DIR"),
            }
        }

        // Missing file → field is None (no fallback available).
        assert!(
            ctx.soul_md.is_none(),
            "expected None for missing SOUL.md, got: {:?}",
            ctx.soul_md
        );
    }

    #[test]
    fn heartbeat_loads_heartbeat_md() {
        let tmp = tempfile::tempdir().expect("tempdir");
        setup_workspace(tmp.path());
        fs::write(tmp.path().join("HEARTBEAT.md"), "# Heartbeat\nchecklist").expect("hb");

        let ctx = WorkspaceContext::load(
            tmp.path(),
            SessionType::Heartbeat,
            false,
            DEFAULT_MAX_CHARS_PER_FILE,
            DEFAULT_TOTAL_MAX_CHARS,
        );

        assert!(
            ctx.heartbeat_md
                .as_deref()
                .unwrap_or("")
                .contains("checklist")
        );
    }

    #[test]
    fn per_file_char_limit_applied() {
        let tmp = tempfile::tempdir().expect("tempdir");
        // Write a 100-char AGENTS.md but limit to 50.
        fs::write(tmp.path().join("AGENTS.md"), "x".repeat(100)).expect("agents");

        let ctx = WorkspaceContext::load(
            tmp.path(),
            SessionType::Normal,
            false,
            50,
            DEFAULT_TOTAL_MAX_CHARS,
        );

        let content = ctx.agents_md.as_deref().unwrap_or("");
        assert!(content.len() <= 50, "got {} chars", content.len());
    }
}