|
1
|
+ |
//! Foreign coding-agent session discovery, as a `packet-v0` guest plugin.
|
|
2
|
+ |
//!
|
|
3
|
+ |
//! The scanner half of OpenAgentsInc/openagents.com#198: given read-only
|
|
4
|
+ |
//! mounts over `~/.claude` (mount 0) and `~/.codex` (mount 1), report
|
|
5
|
+ |
//! recent session *metadata* — source, session id, working directory,
|
|
6
|
+ |
//! mtime, size, record count — and nothing else. Resuming a session is
|
|
7
|
+ |
//! deliberately not here; this plugin only says what exists.
|
|
8
|
+ |
//!
|
|
9
|
+ |
//! Foreign state is untrusted input, so the posture is the issue's:
|
|
10
|
+ |
//! read-only through the host's confined capability imports, bounded
|
|
11
|
+ |
//! everywhere (listing entries, per-file bytes, file reads per run,
|
|
12
|
+ |
//! directory listings per run, candidates, results), and fail-soft — a
|
|
13
|
+ |
//! missing directory contributes nothing, a malformed or unreadable file
|
|
14
|
+ |
//! is skipped and counted, a file over the host's per-file read bound is
|
|
15
|
+ |
//! reported from listing metadata alone and marked `metadata_truncated`.
|
|
16
|
+ |
//!
|
|
17
|
+ |
//! The guest has no clock on `wasm32-unknown-unknown`, so the age cutoff
|
|
18
|
+ |
//! runs against `now_ms` when the caller provides it, and otherwise
|
|
19
|
+ |
//! against the newest mtime the scan observed.
|
|
20
|
+ |
|
|
21
|
+ |
use openagents_pdk::{
|
|
22
|
+ |
list_mounted_dir, plugin_entry, read_mounted_file, MountDirListing, Refusal, RefusalCode,
|
|
23
|
+ |
};
|
|
24
|
+ |
use serde::{Deserialize, Serialize};
|
|
25
|
+ |
|
|
26
|
+ |
/// Mount indices, fixed by the order `manifest.json` declares the mounts.
|
|
27
|
+ |
const CLAUDE_MOUNT: u32 = 0;
|
|
28
|
+ |
const CODEX_MOUNT: u32 = 1;
|
|
29
|
+ |
|
|
30
|
+ |
const DEFAULT_MAX_AGE_DAYS: f64 = 30.0;
|
|
31
|
+ |
const DEFAULT_LIMIT: usize = 50;
|
|
32
|
+ |
/// Hard cap on `limit`; asking for more is answered with this many.
|
|
33
|
+ |
const LIMIT_CAP: usize = 50;
|
|
34
|
+ |
/// How many leading JSONL lines may be inspected for session metadata.
|
|
35
|
+ |
const META_SCAN_LINES: usize = 20;
|
|
36
|
+ |
/// File reads per invocation, across both sources.
|
|
37
|
+ |
const MAX_FILE_READS: usize = 200;
|
|
38
|
+ |
/// Directory listings per invocation, across both sources.
|
|
39
|
+ |
const MAX_DIR_LISTS: usize = 1500;
|
|
40
|
+ |
/// Candidate files held before sorting; beyond this the scan reports itself
|
|
41
|
+ |
/// truncated rather than growing without bound.
|
|
42
|
+ |
const MAX_CANDIDATES: usize = 5000;
|
|
43
|
+ |
const MS_PER_DAY: f64 = 86_400_000.0;
|
|
44
|
+ |
|
|
45
|
+ |
#[derive(Deserialize)]
|
|
46
|
+ |
pub struct Input {
|
|
47
|
+ |
/// Which stores to scan; both when absent.
|
|
48
|
+ |
#[serde(default)]
|
|
49
|
+ |
pub sources: Option<Vec<String>>,
|
|
50
|
+ |
/// Substring the session's working directory must contain.
|
|
51
|
+ |
#[serde(default)]
|
|
52
|
+ |
pub cwd_filter: Option<String>,
|
|
53
|
+ |
/// Sessions older than this are not reported. Default 30.
|
|
54
|
+ |
#[serde(default)]
|
|
55
|
+ |
pub max_age_days: Option<f64>,
|
|
56
|
+ |
/// Most sessions to report, newest first. Default 50, capped at 50.
|
|
57
|
+ |
#[serde(default)]
|
|
58
|
+ |
pub limit: Option<usize>,
|
|
59
|
+ |
/// Milliseconds since the Unix epoch, for the age cutoff. The sandbox
|
|
60
|
+ |
/// has no clock; when absent, the newest observed mtime stands in.
|
|
61
|
+ |
#[serde(default)]
|
|
62
|
+ |
pub now_ms: Option<i64>,
|
|
63
|
+ |
}
|
|
64
|
+ |
|
|
65
|
+ |
#[derive(Debug, Serialize, PartialEq)]
|
|
66
|
+ |
pub struct Session {
|
|
67
|
+ |
pub source: &'static str,
|
|
68
|
+ |
pub session_id: String,
|
|
69
|
+ |
/// Path relative to the source's mount root (`~/.claude` or `~/.codex`).
|
|
70
|
+ |
pub path: String,
|
|
71
|
+ |
#[serde(skip_serializing_if = "Option::is_none")]
|
|
72
|
+ |
pub cwd: Option<String>,
|
|
73
|
+ |
/// Claude only: the encoded project directory the session file sits in.
|
|
74
|
+ |
#[serde(skip_serializing_if = "Option::is_none")]
|
|
75
|
+ |
pub project_dir: Option<String>,
|
|
76
|
+ |
pub mtime_ms: i64,
|
|
77
|
+ |
pub size_bytes: u64,
|
|
78
|
+ |
/// JSONL records in the file, when the file was small enough to read.
|
|
79
|
+ |
#[serde(skip_serializing_if = "Option::is_none")]
|
|
80
|
+ |
pub record_count: Option<usize>,
|
|
81
|
+ |
/// True when the file exceeds the host's per-file read bound, so only
|
|
82
|
+ |
/// the directory listing's metadata is known.
|
|
83
|
+ |
#[serde(skip_serializing_if = "std::ops::Not::not")]
|
|
84
|
+ |
pub metadata_truncated: bool,
|
|
85
|
+ |
}
|
|
86
|
+ |
|
|
87
|
+ |
#[derive(Debug, Default, Serialize, PartialEq, Eq)]
|
|
88
|
+ |
pub struct Skipped {
|
|
89
|
+ |
/// Readable files whose leading records held no usable metadata.
|
|
90
|
+ |
pub malformed: usize,
|
|
91
|
+ |
/// Files the host refused to read for any reason but size.
|
|
92
|
+ |
pub unreadable: usize,
|
|
93
|
+ |
/// Symlinked entries, which the host would refuse to follow.
|
|
94
|
+ |
pub symlinked: usize,
|
|
95
|
+ |
}
|
|
96
|
+ |
|
|
97
|
+ |
#[derive(Debug, Serialize)]
|
|
98
|
+ |
pub struct Output {
|
|
99
|
+ |
pub sessions: Vec<Session>,
|
|
100
|
+ |
pub scanned_dirs: usize,
|
|
101
|
+ |
pub scanned_files: usize,
|
|
102
|
+ |
pub skipped: Skipped,
|
|
103
|
+ |
/// Files reported from listing metadata alone (over the read bound).
|
|
104
|
+ |
pub oversized: usize,
|
|
105
|
+ |
/// Sources whose store was not present under its mount.
|
|
106
|
+ |
#[serde(skip_serializing_if = "Vec::is_empty")]
|
|
107
|
+ |
pub missing_sources: Vec<&'static str>,
|
|
108
|
+ |
/// True when any directory listing hit the host's entry bound, or the
|
|
109
|
+ |
/// scan hit its own listing/candidate bounds; the picture may be partial.
|
|
110
|
+ |
pub scan_truncated: bool,
|
|
111
|
+ |
/// True when the per-invocation file-read budget ran out before every
|
|
112
|
+ |
/// surviving candidate could be inspected.
|
|
113
|
+ |
pub read_budget_exhausted: bool,
|
|
114
|
+ |
}
|
|
115
|
+ |
|
|
116
|
+ |
/// The two host capabilities the scanner uses, as a seam so the scan logic
|
|
117
|
+ |
/// runs under `cargo test` against a fake host as well as inside the WASM
|
|
118
|
+ |
/// sandbox against the real one.
|
|
119
|
+ |
pub trait Host {
|
|
120
|
+ |
fn list(&self, mount_index: u32, path: &str) -> Result<MountDirListing, Refusal>;
|
|
121
|
+ |
fn read(&self, path: &str) -> Result<Vec<u8>, Refusal>;
|
|
122
|
+ |
}
|
|
123
|
+ |
|
|
124
|
+ |
struct RealHost;
|
|
125
|
+ |
|
|
126
|
+ |
impl Host for RealHost {
|
|
127
|
+ |
fn list(&self, mount_index: u32, path: &str) -> Result<MountDirListing, Refusal> {
|
|
128
|
+ |
list_mounted_dir(mount_index, path)
|
|
129
|
+ |
}
|
|
130
|
+ |
fn read(&self, path: &str) -> Result<Vec<u8>, Refusal> {
|
|
131
|
+ |
read_mounted_file(path)
|
|
132
|
+ |
}
|
|
133
|
+ |
}
|
|
134
|
+ |
|
|
135
|
+ |
/// One file the listing pass found, before its bytes are inspected.
|
|
136
|
+ |
struct Candidate {
|
|
137
|
+ |
source: &'static str,
|
|
138
|
+ |
mount: u32,
|
|
139
|
+ |
path: String,
|
|
140
|
+ |
file_name: String,
|
|
141
|
+ |
project_dir: Option<String>,
|
|
142
|
+ |
mtime_ms: i64,
|
|
143
|
+ |
size_bytes: u64,
|
|
144
|
+ |
}
|
|
145
|
+ |
|
|
146
|
+ |
/// Encode a string the way Claude Code encodes a cwd into a project
|
|
147
|
+ |
/// directory name: every character outside `[A-Za-z0-9]` becomes `-`.
|
|
148
|
+ |
pub fn dashed(text: &str) -> String {
|
|
149
|
+ |
text.chars()
|
|
150
|
+ |
.map(|c| if c.is_ascii_alphanumeric() { c } else { '-' })
|
|
151
|
+ |
.collect()
|
|
152
|
+ |
}
|
|
153
|
+ |
|
|
154
|
+ |
/// Claude session metadata from the file's leading records: the first
|
|
155
|
+ |
/// `cwd` and `sessionId` seen in the first [`META_SCAN_LINES`] lines, plus
|
|
156
|
+ |
/// the record count. `None` when no line yields a cwd.
|
|
157
|
+ |
pub fn claude_meta(bytes: &[u8]) -> Option<(String, Option<String>, usize)> {
|
|
158
|
+ |
let text = String::from_utf8_lossy(bytes);
|
|
159
|
+ |
let record_count = text.lines().count();
|
|
160
|
+ |
let mut cwd = None;
|
|
161
|
+ |
let mut session_id = None;
|
|
162
|
+ |
for line in text.lines().take(META_SCAN_LINES) {
|
|
163
|
+ |
let Ok(value) = serde_json::from_str::<serde_json::Value>(line) else {
|
|
164
|
+ |
continue;
|
|
165
|
+ |
};
|
|
166
|
+ |
if cwd.is_none() {
|
|
167
|
+ |
if let Some(dir) = value.get("cwd").and_then(|v| v.as_str()) {
|
|
168
|
+ |
cwd = Some(dir.to_string());
|
|
169
|
+ |
}
|
|
170
|
+ |
}
|
|
171
|
+ |
if session_id.is_none() {
|
|
172
|
+ |
if let Some(id) = value.get("sessionId").and_then(|v| v.as_str()) {
|
|
173
|
+ |
session_id = Some(id.to_string());
|
|
174
|
+ |
}
|
|
175
|
+ |
}
|
|
176
|
+ |
if cwd.is_some() && session_id.is_some() {
|
|
177
|
+ |
break;
|
|
178
|
+ |
}
|
|
179
|
+ |
}
|
|
180
|
+ |
cwd.map(|dir| (dir, session_id, record_count))
|
|
181
|
+ |
}
|
|
182
|
+ |
|
|
183
|
+ |
/// Codex rollout metadata from the first line's `session_meta` record:
|
|
184
|
+ |
/// `(cwd, session id, record count)`. `None` when the first line is not a
|
|
185
|
+ |
/// well-formed `session_meta` with a `cwd`.
|
|
186
|
+ |
pub fn codex_meta(bytes: &[u8]) -> Option<(String, Option<String>, usize)> {
|
|
187
|
+ |
let text = String::from_utf8_lossy(bytes);
|
|
188
|
+ |
let record_count = text.lines().count();
|
|
189
|
+ |
let first = text.lines().next()?;
|
|
190
|
+ |
let value = serde_json::from_str::<serde_json::Value>(first).ok()?;
|
|
191
|
+ |
if value.get("type").and_then(|v| v.as_str()) != Some("session_meta") {
|
|
192
|
+ |
return None;
|
|
193
|
+ |
}
|
|
194
|
+ |
let payload = value.get("payload")?;
|
|
195
|
+ |
let cwd = payload.get("cwd").and_then(|v| v.as_str())?.to_string();
|
|
196
|
+ |
let id = payload
|
|
197
|
+ |
.get("id")
|
|
198
|
+ |
.and_then(|v| v.as_str())
|
|
199
|
+ |
.map(str::to_string);
|
|
200
|
+ |
Some((cwd, id, record_count))
|
|
201
|
+ |
}
|
|
202
|
+ |
|
|
203
|
+ |
/// A session file's stem: the name without its `.jsonl` suffix.
|
|
204
|
+ |
fn stem(name: &str) -> String {
|
|
205
|
+ |
name.strip_suffix(".jsonl").unwrap_or(name).to_string()
|
|
206
|
+ |
}
|
|
207
|
+ |
|
|
208
|
+ |
/// The whole scan, over any [`Host`]. Total: every path returns an output.
|
|
209
|
+ |
pub fn scan(host: &dyn Host, input: &Input) -> Result<Output, Refusal> {
|
|
210
|
+ |
let sources = match &input.sources {
|
|
211
|
+ |
None => vec!["claude", "codex"],
|
|
212
|
+ |
Some(named) => {
|
|
213
|
+ |
let mut sources = Vec::new();
|
|
214
|
+ |
for name in named {
|
|
215
|
+ |
match name.as_str() {
|
|
216
|
+ |
"claude" => sources.push("claude"),
|
|
217
|
+ |
"codex" => sources.push("codex"),
|
|
218
|
+ |
other => {
|
|
219
|
+ |
return Err(Refusal::unsupported(format!(
|
|
220
|
+ |
"unknown source `{other}`; this scanner knows `claude` and `codex`"
|
|
221
|
+ |
)))
|
|
222
|
+ |
}
|
|
223
|
+ |
}
|
|
224
|
+ |
}
|
|
225
|
+ |
sources
|
|
226
|
+ |
}
|
|
227
|
+ |
};
|
|
228
|
+ |
let max_age_days = input
|
|
229
|
+ |
.max_age_days
|
|
230
|
+ |
.filter(|days| days.is_finite() && *days > 0.0)
|
|
231
|
+ |
.unwrap_or(DEFAULT_MAX_AGE_DAYS);
|
|
232
|
+ |
let limit = input.limit.unwrap_or(DEFAULT_LIMIT).clamp(1, LIMIT_CAP);
|
|
233
|
+ |
|
|
234
|
+ |
let mut out = Output {
|
|
235
|
+ |
sessions: Vec::new(),
|
|
236
|
+ |
scanned_dirs: 0,
|
|
237
|
+ |
scanned_files: 0,
|
|
238
|
+ |
skipped: Skipped::default(),
|
|
239
|
+ |
oversized: 0,
|
|
240
|
+ |
missing_sources: Vec::new(),
|
|
241
|
+ |
scan_truncated: false,
|
|
242
|
+ |
read_budget_exhausted: false,
|
|
243
|
+ |
};
|
|
244
|
+ |
let mut candidates: Vec<Candidate> = Vec::new();
|
|
245
|
+ |
let mut dir_lists = 0usize;
|
|
246
|
+ |
|
|
247
|
+ |
// A listing whose store directory is absent means the source is not on
|
|
248
|
+ |
// this machine; any other listing failure also fails soft.
|
|
249
|
+ |
let mut list = |out: &mut Output,
|
|
250
|
+ |
mount: u32,
|
|
251
|
+ |
path: &str|
|
|
252
|
+ |
-> Option<MountDirListing> {
|
|
253
|
+ |
if dir_lists >= MAX_DIR_LISTS {
|
|
254
|
+ |
out.scan_truncated = true;
|
|
255
|
+ |
return None;
|
|
256
|
+ |
}
|
|
257
|
+ |
dir_lists += 1;
|
|
258
|
+ |
match host.list(mount, path) {
|
|
259
|
+ |
Ok(listing) => {
|
|
260
|
+ |
out.scanned_dirs += 1;
|
|
261
|
+ |
if listing.truncated {
|
|
262
|
+ |
out.scan_truncated = true;
|
|
263
|
+ |
}
|
|
264
|
+ |
Some(listing)
|
|
265
|
+ |
}
|
|
266
|
+ |
Err(_) => None,
|
|
267
|
+ |
}
|
|
268
|
+ |
};
|
|
269
|
+ |
|
|
270
|
+ |
let push = |out: &mut Output, candidates: &mut Vec<Candidate>, candidate: Candidate| {
|
|
271
|
+ |
out.scanned_files += 1;
|
|
272
|
+ |
if candidates.len() < MAX_CANDIDATES {
|
|
273
|
+ |
candidates.push(candidate);
|
|
274
|
+ |
} else {
|
|
275
|
+ |
out.scan_truncated = true;
|
|
276
|
+ |
}
|
|
277
|
+ |
};
|
|
278
|
+ |
|
|
279
|
+ |
for source in &sources {
|
|
280
|
+ |
match *source {
|
|
281
|
+ |
"claude" => {
|
|
282
|
+ |
// ~/.claude/projects/<encoded-cwd>/<session-uuid>.jsonl
|
|
283
|
+ |
let Some(projects) = list(&mut out, CLAUDE_MOUNT, "projects") else {
|
|
284
|
+ |
out.missing_sources.push("claude");
|
|
285
|
+ |
continue;
|
|
286
|
+ |
};
|
|
287
|
+ |
for project in &projects.entries {
|
|
288
|
+ |
match project.kind.as_str() {
|
|
289
|
+ |
"dir" => {}
|
|
290
|
+ |
"symlink" => {
|
|
291
|
+ |
out.skipped.symlinked += 1;
|
|
292
|
+ |
continue;
|
|
293
|
+ |
}
|
|
294
|
+ |
_ => continue,
|
|
295
|
+ |
}
|
|
296
|
+ |
let dir_path = format!("projects/{}", project.name);
|
|
297
|
+ |
let Some(files) = list(&mut out, CLAUDE_MOUNT, &dir_path) else {
|
|
298
|
+ |
continue;
|
|
299
|
+ |
};
|
|
300
|
+ |
for file in &files.entries {
|
|
301
|
+ |
if file.kind == "symlink" {
|
|
302
|
+ |
out.skipped.symlinked += 1;
|
|
303
|
+ |
continue;
|
|
304
|
+ |
}
|
|
305
|
+ |
if file.kind != "file" || !file.name.ends_with(".jsonl") {
|
|
306
|
+ |
continue;
|
|
307
|
+ |
}
|
|
308
|
+ |
push(
|
|
309
|
+ |
&mut out,
|
|
310
|
+ |
&mut candidates,
|
|
311
|
+ |
Candidate {
|
|
312
|
+ |
source: "claude",
|
|
313
|
+ |
mount: CLAUDE_MOUNT,
|
|
314
|
+ |
path: format!("{dir_path}/{}", file.name),
|
|
315
|
+ |
file_name: file.name.clone(),
|
|
316
|
+ |
project_dir: Some(project.name.clone()),
|
|
317
|
+ |
mtime_ms: file.mtime_ms,
|
|
318
|
+ |
size_bytes: file.size,
|
|
319
|
+ |
},
|
|
320
|
+ |
);
|
|
321
|
+ |
}
|
|
322
|
+ |
}
|
|
323
|
+ |
}
|
|
324
|
+ |
"codex" => {
|
|
325
|
+ |
// ~/.codex/sessions/YYYY/MM/DD/rollout-*.jsonl. The
|
|
326
|
+ |
// `state_*.sqlite` index beside it is out of scope for this
|
|
327
|
+ |
// slice; see the plugin README.
|
|
328
|
+ |
let Some(years) = list(&mut out, CODEX_MOUNT, "sessions") else {
|
|
329
|
+ |
out.missing_sources.push("codex");
|
|
330
|
+ |
continue;
|
|
331
|
+ |
};
|
|
332
|
+ |
for year in dirs_of(&years, &mut out.skipped) {
|
|
333
|
+ |
let year_path = format!("sessions/{year}");
|
|
334
|
+ |
let Some(months) = list(&mut out, CODEX_MOUNT, &year_path) else {
|
|
335
|
+ |
continue;
|
|
336
|
+ |
};
|
|
337
|
+ |
for month in dirs_of(&months, &mut out.skipped) {
|
|
338
|
+ |
let month_path = format!("{year_path}/{month}");
|
|
339
|
+ |
let Some(days) = list(&mut out, CODEX_MOUNT, &month_path) else {
|
|
340
|
+ |
continue;
|
|
341
|
+ |
};
|
|
342
|
+ |
for day in dirs_of(&days, &mut out.skipped) {
|
|
343
|
+ |
let day_path = format!("{month_path}/{day}");
|
|
344
|
+ |
let Some(files) = list(&mut out, CODEX_MOUNT, &day_path) else {
|
|
345
|
+ |
continue;
|
|
346
|
+ |
};
|
|
347
|
+ |
for file in &files.entries {
|
|
348
|
+ |
if file.kind == "symlink" {
|
|
349
|
+ |
out.skipped.symlinked += 1;
|
|
350
|
+ |
continue;
|
|
351
|
+ |
}
|
|
352
|
+ |
if file.kind != "file"
|
|
353
|
+ |
|| !file.name.starts_with("rollout-")
|
|
354
|
+ |
|| !file.name.ends_with(".jsonl")
|
|
355
|
+ |
{
|
|
356
|
+ |
continue;
|
|
357
|
+ |
}
|
|
358
|
+ |
push(
|
|
359
|
+ |
&mut out,
|
|
360
|
+ |
&mut candidates,
|
|
361
|
+ |
Candidate {
|
|
362
|
+ |
source: "codex",
|
|
363
|
+ |
mount: CODEX_MOUNT,
|
|
364
|
+ |
path: format!("{day_path}/{}", file.name),
|
|
365
|
+ |
file_name: file.name.clone(),
|
|
366
|
+ |
project_dir: None,
|
|
367
|
+ |
mtime_ms: file.mtime_ms,
|
|
368
|
+ |
size_bytes: file.size,
|
|
369
|
+ |
},
|
|
370
|
+ |
);
|
|
371
|
+ |
}
|
|
372
|
+ |
}
|
|
373
|
+ |
}
|
|
374
|
+ |
}
|
|
375
|
+ |
}
|
|
376
|
+ |
_ => unreachable!("sources were validated above"),
|
|
377
|
+ |
}
|
|
378
|
+ |
}
|
|
379
|
+ |
|
|
380
|
+ |
// The age cutoff: `now` is the caller's clock, or the newest thing seen.
|
|
381
|
+ |
let now_ms = input
|
|
382
|
+ |
.now_ms
|
|
383
|
+ |
.or_else(|| candidates.iter().map(|c| c.mtime_ms).max())
|
|
384
|
+ |
.unwrap_or(0);
|
|
385
|
+ |
let cutoff_ms = now_ms - (max_age_days * MS_PER_DAY) as i64;
|
|
386
|
+ |
candidates.retain(|c| c.mtime_ms >= cutoff_ms);
|
|
387
|
+ |
candidates.sort_by(|a, b| b.mtime_ms.cmp(&a.mtime_ms).then(a.path.cmp(&b.path)));
|
|
388
|
+ |
|
|
389
|
+ |
let dashed_filter = input.cwd_filter.as_deref().map(dashed);
|
|
390
|
+ |
let mut reads = 0usize;
|
|
391
|
+ |
|
|
392
|
+ |
for candidate in &candidates {
|
|
393
|
+ |
if out.sessions.len() >= limit {
|
|
394
|
+ |
break;
|
|
395
|
+ |
}
|
|
396
|
+ |
// Claude's project directory name encodes the cwd, so a filter can
|
|
397
|
+ |
// rule a candidate out before spending a read on it.
|
|
398
|
+ |
if let (Some(filter), Some(project_dir)) = (&dashed_filter, &candidate.project_dir) {
|
|
399
|
+ |
if !dashed(project_dir).contains(filter.as_str()) {
|
|
400
|
+ |
continue;
|
|
401
|
+ |
}
|
|
402
|
+ |
}
|
|
403
|
+ |
if reads >= MAX_FILE_READS {
|
|
404
|
+ |
out.read_budget_exhausted = true;
|
|
405
|
+ |
break;
|
|
406
|
+ |
}
|
|
407
|
+ |
reads += 1;
|
|
408
|
+ |
// `read` addresses the mounts in declaration order; the full
|
|
409
|
+ |
// relative path (projects/... vs sessions/...) exists in exactly
|
|
410
|
+ |
// one of them. `candidate.mount` records intent for the reader.
|
|
411
|
+ |
let _ = candidate.mount;
|
|
412
|
+ |
match host.read(&candidate.path) {
|
|
413
|
+ |
Ok(bytes) => {
|
|
414
|
+ |
let meta = match candidate.source {
|
|
415
|
+ |
"claude" => claude_meta(&bytes),
|
|
416
|
+ |
_ => codex_meta(&bytes),
|
|
417
|
+ |
};
|
|
418
|
+ |
let Some((cwd, session_id, record_count)) = meta else {
|
|
419
|
+ |
out.skipped.malformed += 1;
|
|
420
|
+ |
continue;
|
|
421
|
+ |
};
|
|
422
|
+ |
if let Some(filter) = input.cwd_filter.as_deref() {
|
|
423
|
+ |
if !cwd.contains(filter) && !dashed(&cwd).contains(&dashed(filter)) {
|
|
424
|
+ |
continue;
|
|
425
|
+ |
}
|
|
426
|
+ |
}
|
|
427
|
+ |
out.sessions.push(Session {
|
|
428
|
+ |
source: candidate.source,
|
|
429
|
+ |
session_id: session_id.unwrap_or_else(|| stem(&candidate.file_name)),
|
|
430
|
+ |
path: candidate.path.clone(),
|
|
431
|
+ |
cwd: Some(cwd),
|
|
432
|
+ |
project_dir: candidate.project_dir.clone(),
|
|
433
|
+ |
mtime_ms: candidate.mtime_ms,
|
|
434
|
+ |
size_bytes: candidate.size_bytes,
|
|
435
|
+ |
record_count: Some(record_count),
|
|
436
|
+ |
metadata_truncated: false,
|
|
437
|
+ |
});
|
|
438
|
+ |
}
|
|
439
|
+ |
Err(refusal) if refusal.code == RefusalCode::FileTooLarge => {
|
|
440
|
+ |
out.oversized += 1;
|
|
441
|
+ |
// Only the listing's metadata is known. With a cwd filter, a
|
|
442
|
+ |
// Claude candidate already passed the project-name prefilter;
|
|
443
|
+ |
// a Codex candidate's cwd is unknowable here, so the filter
|
|
444
|
+ |
// excludes it rather than guessing.
|
|
445
|
+ |
if dashed_filter.is_some() && candidate.project_dir.is_none() {
|
|
446
|
+ |
continue;
|
|
447
|
+ |
}
|
|
448
|
+ |
out.sessions.push(Session {
|
|
449
|
+ |
source: candidate.source,
|
|
450
|
+ |
session_id: stem(&candidate.file_name),
|
|
451
|
+ |
path: candidate.path.clone(),
|
|
452
|
+ |
cwd: None,
|
|
453
|
+ |
project_dir: candidate.project_dir.clone(),
|
|
454
|
+ |
mtime_ms: candidate.mtime_ms,
|
|
455
|
+ |
size_bytes: candidate.size_bytes,
|
|
456
|
+ |
record_count: None,
|
|
457
|
+ |
metadata_truncated: true,
|
|
458
|
+ |
});
|
|
459
|
+ |
}
|
|
460
|
+ |
Err(_) => {
|
|
461
|
+ |
out.skipped.unreadable += 1;
|
|
462
|
+ |
}
|
|
463
|
+ |
}
|
|
464
|
+ |
}
|
|
465
|
+ |
|
|
466
|
+ |
Ok(out)
|
|
467
|
+ |
}
|
|
468
|
+ |
|
|
469
|
+ |
/// Directory names in a listing, counting symlinks as skipped.
|
|
470
|
+ |
fn dirs_of<'l>(listing: &'l MountDirListing, skipped: &mut Skipped) -> Vec<&'l str> {
|
|
471
|
+ |
let mut dirs = Vec::new();
|
|
472
|
+ |
for entry in &listing.entries {
|
|
473
|
+ |
match entry.kind.as_str() {
|
|
474
|
+ |
"dir" => dirs.push(entry.name.as_str()),
|
|
475
|
+ |
"symlink" => skipped.symlinked += 1,
|
|
476
|
+ |
_ => {}
|
|
477
|
+ |
}
|
|
478
|
+ |
}
|
|
479
|
+ |
dirs
|
|
480
|
+ |
}
|
|
481
|
+ |
|
|
482
|
+ |
fn handle(input: Input) -> Result<Output, Refusal> {
|
|
483
|
+ |
scan(&RealHost, &input)
|
|
484
|
+ |
}
|
|
485
|
+ |
|
|
486
|
+ |
plugin_entry!(handle);
|
|
487
|
+ |
|
|
488
|
+ |
#[cfg(test)]
|
|
489
|
+ |
mod tests;
|