cellix-agent 0.6.4

Local socket and CLI adapters for Cellix
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
//! What one Claude Code session worked from, from its transcript: the instruction files it
//! loaded as it says it loaded them, the listings it was given, what it read and searched, and
//! how full its context was at its last reply. The transcript is data; nothing in it is run.

use std::collections::HashMap;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};

use serde_json::Value;

use super::{Scope, claude_project_dir, estimate_tokens};

/// A transcript line longer than this is skipped unread: a pasted image or a huge tool output.
const LONGEST_LINE: usize = 8 * 1024 * 1024;

/// Where Claude Code keeps session `session`'s transcript for a project.
pub fn transcript_path(claude: &Path, project: &Path, session: &str) -> PathBuf {
    claude_project_dir(claude, project).join(format!("{session}.jsonl"))
}

/// An instruction file as the agent got it.
#[derive(Clone, Debug, PartialEq)]
pub struct Loaded {
    pub path: PathBuf,
    pub scope: Scope,
    pub text: String,
}

/// A file the session read, searched in or changed.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct FileRead {
    pub path: PathBuf,
    pub reads: usize,
    pub searches: usize,
    pub edits: usize,
    /// About how many tokens what it read of this file came to.
    pub tokens: usize,
    /// The turns it read the file in, 1-based, each once.
    pub turns: Vec<usize>,
    /// When it last read the file, in seconds since 1970, to tell whether it changed since.
    pub last_read: Option<u64>,
    /// When it last changed the file itself.
    pub last_edit: Option<u64>,
}

/// A search the session ran: Grep or Glob.
#[derive(Clone, Debug, PartialEq)]
pub struct Search {
    pub tool: String,
    pub pattern: String,
    pub path: Option<String>,
    pub tokens: usize,
    pub turn: usize,
}

/// What fills the context, in tokens. Everything but `total` is estimated from the text.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Breakdown {
    pub system: usize,
    pub tools: usize,
    pub instructions: usize,
    /// The skills and subagents it was told it could use.
    pub listings: usize,
    pub mcp: usize,
    pub files: usize,
    pub output: usize,
    pub messages: usize,
    /// The last reply's real count: input, cache written and cache read.
    pub total: usize,
}

impl Breakdown {
    /// Everything there before the first message.
    pub fn before(&self) -> usize {
        self.system + self.tools + self.instructions + self.listings + self.mcp
    }

    pub fn conversation(&self) -> usize {
        self.files + self.output + self.messages
    }
}

#[derive(Clone, Debug, Default)]
pub struct Session {
    pub loaded: Vec<Loaded>,
    /// The skills listing exactly as the agent was given it.
    pub skills: String,
    /// One line per subagent it was told of.
    pub subagents: Vec<String>,
    /// MCP servers that gave it instructions, and those instructions.
    pub mcp: Vec<(String, String)>,
    /// Skills it called on, by name, each once.
    pub invoked: Vec<String>,
    pub files: Vec<FileRead>,
    pub searches: Vec<Search>,
    pub turns: usize,
    pub model: Option<String>,
    pub breakdown: Breakdown,
    /// Times the context was compacted.
    pub compactions: usize,
}

impl Session {
    /// The model's context window, as `claude_window` gives it.
    pub fn window(&self) -> usize {
        let model = self.model.as_deref().unwrap_or("");
        claude_window(model, self.breakdown.total as u64) as usize
    }

    /// Files it read that have changed on disk since, most recent read first.
    pub fn stale(&self) -> Vec<&FileRead> {
        self.files
            .iter()
            .filter(|f| {
                let Some(read) = f.last_read else {
                    return false;
                };
                let own = f.last_edit.is_some_and(|e| e >= read);
                let modified = std::fs::metadata(&f.path).and_then(|m| m.modified()).ok();
                let modified = modified.and_then(|m| m.duration_since(std::time::UNIX_EPOCH).ok());
                !own && modified.is_some_and(|m| m.as_secs() > read + 1)
            })
            .collect()
    }
}

/// The context window of a Claude model, by the id its transcript logs (`claude-opus-5-5`, or
/// with a provider's prefix as `us.anthropic.claude-…`), and how much of it is `used`. Every
/// Claude model since Opus and Sonnet 4.6 has a million, and so does a model not known here,
/// since a new one is likelier to have the long window than an old one. Those up to 4.5 have
/// 200k, unless the name says otherwise (`[1m]`, `-1m`) or the context has gone past it.
pub fn claude_window(model: &str, used: u64) -> u64 {
    const SHORT: u64 = 200_000;
    const LONG: u64 = 1_000_000;
    let long_named = model.contains("[1m]") || model.ends_with("-1m");
    if short_windowed(model) && !long_named && used <= SHORT {
        SHORT
    } else {
        LONG
    }
}

/// Whether the model is a Claude from before the long window: `claude-3…`, or a
/// `claude-<family>-4[-<minor>][-<date>]` up to 4.5.
fn short_windowed(model: &str) -> bool {
    let Some(at) = model.find("claude-") else {
        return false;
    };
    let mut parts = model[at + "claude-".len()..].split(['-', '[', '@']);
    let first = parts.next().unwrap_or("");
    if first.parse::<u32>().is_ok_and(|major| major < 4) {
        return true;
    }
    let Some(Ok(major)) = parts.next().map(str::parse::<u32>) else {
        return false;
    };
    // A minor is a number or two; eight digits are a date, as in `claude-opus-4-20250514`.
    let minor = parts
        .next()
        .filter(|p| (1..=2).contains(&p.len()))
        .and_then(|p| p.parse::<u32>().ok())
        .unwrap_or(0);
    major < 4 || (major == 4 && minor <= 5)
}

/// Seconds since 1970 for an RFC 3339 time in UTC, as transcripts write them.
pub(crate) fn unix_seconds(stamp: &str) -> Option<u64> {
    let (date, time) = stamp.split_once('T')?;
    let mut d = date.split('-').map(|p| p.parse::<i64>().ok());
    let (y, m, day) = (d.next()??, d.next()??, d.next()??);
    let time = time.trim_end_matches('Z');
    let time = time.split(['+', '-']).next()?;
    let mut t = time.split(':');
    let h: i64 = t.next()?.parse().ok()?;
    let min: i64 = t.next()?.parse().ok()?;
    let s: f64 = t.next().unwrap_or("0").parse().ok()?;
    // Days from the civil date, after Howard Hinnant.
    let y = if m <= 2 { y - 1 } else { y };
    let era = y.div_euclid(400);
    let yoe = y - era * 400;
    let mp = (m + 9) % 12;
    let doy = (153 * mp + 2) / 5 + day - 1;
    let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
    let days = era * 146_097 + doe - 719_468;
    u64::try_from(days * 86_400 + h * 3600 + min * 60 + s as i64).ok()
}

fn text_of(content: &Value) -> String {
    match content {
        Value::String(s) => s.clone(),
        Value::Array(parts) => parts
            .iter()
            .filter_map(|p| p["text"].as_str().or_else(|| p.as_str()))
            .collect::<Vec<_>>()
            .join("\n"),
        _ => String::new(),
    }
}

/// Reads a transcript. A line that isn't JSON, or a kind it doesn't know, is passed over, so a
/// newer Claude Code's transcript still reads as far as it can.
pub fn read_session(path: &Path) -> std::io::Result<Session> {
    let reader = BufReader::new(File::open(path)?);
    let mut session = Session::default();
    let mut index: HashMap<PathBuf, usize> = HashMap::new();
    // Tool calls waiting for their result: id to (tool, file or search).
    let mut pending: HashMap<String, (String, Option<usize>, Option<usize>)> = HashMap::new();
    let mut since_compact_files = 0;
    let mut since_compact_output = 0;
    let mut usage_total = 0;
    for line in reader.split(b'\n') {
        let Ok(line) = line else { break };
        if line.len() > LONGEST_LINE {
            continue;
        }
        let Ok(record) = serde_json::from_slice::<Value>(&line) else {
            continue;
        };
        if record["isSidechain"].as_bool() == Some(true) {
            continue;
        }
        let when = record["timestamp"].as_str().and_then(unix_seconds);
        match record["type"].as_str() {
            Some("attachment") => attachment(&record["attachment"], &mut session),
            Some("api-request-shape") => {
                let shape = &record["shape"];
                session.breakdown.system = estimate_tokens(&text_of(&shape["system"]));
                session.breakdown.tools = estimate_tokens(&shape["tools"].to_string());
            }
            Some("system") if record["subtype"] == "compact_boundary" => {
                session.compactions += 1;
                since_compact_files = 0;
                since_compact_output = 0;
            }
            Some("assistant") => {
                let message = &record["message"];
                if let Some(model) = message["model"].as_str().filter(|m| !m.starts_with('<')) {
                    session.model = Some(model.to_string());
                }
                let usage = &message["usage"];
                let total: u64 = [
                    "input_tokens",
                    "cache_creation_input_tokens",
                    "cache_read_input_tokens",
                ]
                .iter()
                .filter_map(|k| usage[*k].as_u64())
                .sum();
                if total > 0 {
                    usage_total = total as usize;
                }
                for block in message["content"].as_array().into_iter().flatten() {
                    if block["type"] == "tool_use" {
                        let id = block["id"].as_str().unwrap_or("").to_string();
                        let call = tool_call(block, &mut session, &mut index, when);
                        pending.insert(id, call);
                    }
                }
            }
            Some("user") => {
                let content = &record["message"]["content"];
                let prompt = match content {
                    Value::String(_) => true,
                    Value::Array(parts) => parts.iter().any(|p| p["type"] == "text"),
                    _ => false,
                };
                let own = ["isMeta", "isCompactSummary", "isVisibleInTranscriptOnly"]
                    .iter()
                    .any(|flag| record[*flag].as_bool() == Some(true));
                if prompt && !own {
                    session.turns += 1;
                }
                for part in content.as_array().into_iter().flatten() {
                    if part["type"] != "tool_result" {
                        continue;
                    }
                    let tokens = estimate_tokens(&text_of(&part["content"]));
                    let id = part["tool_use_id"].as_str().unwrap_or("");
                    match pending.remove(id) {
                        Some((tool, Some(file), _)) if tool == "Read" => {
                            session.files[file].tokens += tokens;
                            since_compact_files += tokens;
                        }
                        Some((_, _, Some(search))) => {
                            session.searches[search].tokens += tokens;
                            since_compact_output += tokens;
                        }
                        _ => since_compact_output += tokens,
                    }
                }
            }
            _ => {}
        }
    }
    let b = &mut session.breakdown;
    b.instructions = session
        .loaded
        .iter()
        .map(|l| estimate_tokens(&l.text))
        .sum();
    b.listings = estimate_tokens(&session.skills)
        + session
            .subagents
            .iter()
            .map(|s| estimate_tokens(s))
            .sum::<usize>();
    b.mcp = session.mcp.iter().map(|(_, t)| estimate_tokens(t)).sum();
    b.total = usage_total;
    b.files = since_compact_files;
    b.output = since_compact_output;
    let known = b.before() + b.files + b.output;
    b.messages = usage_total.saturating_sub(known);
    session
        .files
        .sort_by(|a, b| b.tokens.cmp(&a.tokens).then(a.path.cmp(&b.path)));
    Ok(session)
}

fn attachment(a: &Value, session: &mut Session) {
    match a["type"].as_str() {
        Some("instructions") => {
            for file in a["files"].as_array().into_iter().flatten() {
                let Some(path) = file["path"].as_str() else {
                    continue;
                };
                let loaded = Loaded {
                    path: PathBuf::from(path),
                    scope: Scope::from_claude(file["type"].as_str().unwrap_or("")),
                    text: file["content"].as_str().unwrap_or("").to_string(),
                };
                match session.loaded.iter_mut().find(|l| l.path == loaded.path) {
                    Some(known) => *known = loaded,
                    None => session.loaded.push(loaded),
                }
            }
        }
        Some("skill_listing") => session.skills = a["content"].as_str().unwrap_or("").to_string(),
        Some("agent_listing_delta") => {
            for line in a["addedLines"]
                .as_array()
                .into_iter()
                .flatten()
                .filter_map(Value::as_str)
            {
                if !session.subagents.iter().any(|s| s == line) {
                    session.subagents.push(line.to_string());
                }
            }
        }
        Some("mcp_instructions_delta") => {
            let names = a["addedNames"]
                .as_array()
                .into_iter()
                .flatten()
                .filter_map(Value::as_str);
            let blocks = a["addedBlocks"]
                .as_array()
                .into_iter()
                .flatten()
                .filter_map(Value::as_str);
            for (name, block) in names.zip(blocks) {
                session.mcp.retain(|(n, _)| n != name);
                session.mcp.push((name.to_string(), block.to_string()));
            }
        }
        Some("invoked_skills") => {
            for skill in a["skills"].as_array().into_iter().flatten() {
                if let Some(name) = skill["name"].as_str()
                    && !session.invoked.iter().any(|n| n == name)
                {
                    session.invoked.push(name.to_string());
                }
            }
        }
        _ => {}
    }
}

/// Notes a tool call: the file it reads or changes, or the search it runs.
fn tool_call(
    block: &Value,
    session: &mut Session,
    index: &mut HashMap<PathBuf, usize>,
    when: Option<u64>,
) -> (String, Option<usize>, Option<usize>) {
    let tool = block["name"].as_str().unwrap_or("").to_string();
    let input = &block["input"];
    let turn = session.turns.max(1);
    let mut file_at = |path: &str, session: &mut Session| -> usize {
        let path = PathBuf::from(path);
        *index.entry(path.clone()).or_insert_with(|| {
            session.files.push(FileRead {
                path,
                ..FileRead::default()
            });
            session.files.len() - 1
        })
    };
    match tool.as_str() {
        "Read" | "NotebookRead" => {
            let path = input["file_path"]
                .as_str()
                .or_else(|| input["notebook_path"].as_str());
            let Some(path) = path else {
                return (tool, None, None);
            };
            let i = file_at(path, session);
            let file = &mut session.files[i];
            file.reads += 1;
            file.last_read = when.or(file.last_read);
            if !file.turns.contains(&turn) {
                file.turns.push(turn);
            }
            (tool, Some(i), None)
        }
        "Edit" | "Write" | "MultiEdit" | "NotebookEdit" => {
            let path = input["file_path"]
                .as_str()
                .or_else(|| input["notebook_path"].as_str());
            if let Some(path) = path {
                let i = file_at(path, session);
                session.files[i].edits += 1;
                session.files[i].last_edit = when.or(session.files[i].last_edit);
            }
            (tool, None, None)
        }
        "Grep" | "Glob" => {
            let pattern = input["pattern"].as_str().unwrap_or("").to_string();
            let path = input["path"].as_str().map(str::to_string);
            if let Some(p) = &path
                && tool == "Grep"
                && Path::new(p).extension().is_some()
            {
                let i = file_at(p, session);
                session.files[i].searches += 1;
            }
            session.searches.push(Search {
                tool: tool.clone(),
                pattern,
                path,
                tokens: 0,
                turn,
            });
            let at = session.searches.len() - 1;
            (tool, None, Some(at))
        }
        _ => (tool, None, None),
    }
}