claude_storage 1.0.0

CLI tool for exploring Claude Code filesystem storage
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
//! Bug fix tests for `.count` command issues (#003a, #003b, issue-017)
//!
//! ## Bug #003a: Default Behavior Mismatch
//!
//! - Location: `claude_storage/src/cli/mod.rs`
//! - The `.count` command defaults to `target::projects` when called without parameters,
//!   counting all projects globally instead of being context-aware like `.show`.
//! - Fix: When NO parameters and CWD is a project, count total entries in that project.
//!
//! ## Bug #003b: Hardcoded UUID Parsing
//!
//! - Location: `claude_storage/src/cli/mod.rs`
//! - The `count_routine` hardcodes `ProjectId::uuid()` instead of `parse_project_parameter()`.
//! - Fix: Use `parse_project_parameter()` for both UUID and path-based project parameters.
//!
//! ## issue-017: IO Error in Session Crashes `.count`
//!
//! - Location: `claude_storage/src/cli/mod.rs` — context-aware entry counting loop
//! - The context-aware `.count` loop used `?` to propagate `count_entries()` errors.
//!   Any session that raises an IO error (e.g., permission denied, filesystem error)
//!   caused the entire command to fail rather than skipping that session.
//! - Fix: Changed `?` to `match` + `eprintln!` warning, matching `project_stats()` behavior.
//!
//! ## Pitfall
//!
//! Never use `?` in a loop over user data files. Always skip erroring items with a warning.
//! Note: truncated JSONL does NOT trigger this — `count_entries()` uses byte-level search
//! and succeeds on partial lines. Only true IO errors (permission denied, unreadable file)
//! cause `count_entries()` to return `Err`.

mod common;

use std::fs;
use tempfile::TempDir;

/// Test Bug #003a: .count should be context-aware like .show
///
/// When called with NO parameters from within a project directory,
/// .count should count entries in that project, not count all projects globally.
// test_kind: bug_reproducer(issue-003a)
#[ test ]
fn test_count_default_behavior_context_aware()
{
  let storage = TempDir::new().unwrap();
  let project_cwd = TempDir::new().unwrap();

  // Write 5 conversation entries for the project whose path == project_cwd
  common::write_path_project_session(
    storage.path(),
    project_cwd.path(),
    "session-context-aware",
    5,
  );

  let output = common::clg_cmd()
    .args( [ ".count" ] )
    .current_dir( project_cwd.path() )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "failed to execute .count" );

  let stdout = String::from_utf8_lossy( &output.stdout );
  let stderr = String::from_utf8_lossy( &output.stderr );

  assert!(
    output.status.success(),
    ".count must succeed. stderr: {stderr}"
  );

  let count_str = stdout.trim();

  // Bug #003a: Previously returned number of projects (e.g., 85)
  // Fixed: Now returns number of entries in current project (5)
  assert_eq!(
    count_str, "5",
    "Expected .count (no params) to return 5 entries in current project, got: {count_str}"
  );
}

/// Test Bug #003b: .count should handle path-based project parameters
///
/// The .count command should use `parse_project_parameter()` to handle
/// both UUID and path-based project parameters, not hardcode `ProjectId::uuid()`.
// test_kind: bug_reproducer(issue-003b)
#[ test ]
fn test_count_with_path_project_parameter()
{
  let storage = TempDir::new().unwrap();
  let project_path = TempDir::new().unwrap();

  // Write 3 sessions (1 entry each)
  common::write_path_project_session(
    storage.path(),
    project_path.path(),
    "session-path-001",
    1,
  );
  common::write_path_project_session(
    storage.path(),
    project_path.path(),
    "session-path-002",
    1,
  );
  common::write_path_project_session(
    storage.path(),
    project_path.path(),
    "session-path-003",
    1,
  );

  let output = common::clg_cmd()
    .args( [
      ".count",
      "target::sessions",
      &format!( "project::{}", project_path.path().display() ),
    ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "failed to execute .count" );

  let stdout = String::from_utf8_lossy( &output.stdout );
  let stderr = String::from_utf8_lossy( &output.stderr );

  assert!(
    output.status.success(),
    ".count should succeed with path parameter. stderr: {stderr}"
  );

  assert_eq!(
    stdout.trim(), "3",
    "Expected 3 sessions. stdout: {stdout}"
  );
}

/// Test `.count` skips unreadable sessions instead of failing (Bug Reproducer: issue-017)
///
/// ## Root Cause
///
/// The context-aware `.count` loop used `?` to propagate `count_entries()` errors, so
/// a session that returns `Err` (e.g., IO permission denied) caused the entire command
/// to fail. This is inconsistent with `project_stats()` which skips erroring sessions
/// with `eprintln!` and continues.
///
/// ## Why Not Caught
///
/// No tests covered the scenario where a CWD project contains a session that raises
/// an IO error. All existing tests used clean synthetic data. Real production storage
/// can have permission issues or filesystem errors during `read_to_string()`.
///
/// ## Fix Applied
///
/// Changed the session loop in `count_routine()` from `?` propagation to `match`:
/// sessions that return `Err` are skipped with `eprintln!("Warning: Skipping corrupted
/// session ...")`, matching `project.project_stats()` behavior (src/project.rs:342-355).
///
/// ## Prevention
///
/// Never use `?` inside a loop over user data files. Always use `match` + warning
/// for per-item errors so one bad file doesn't kill the whole command.
///
/// ## Pitfall
///
/// Truncated JSONL (e.g., mid-write crash) does NOT trigger this path: `count_entries()`
/// uses byte-level string search and succeeds on partial lines. Only true IO errors
/// (permission denied, read failure) cause `count_entries()` to return `Err`. Use
/// `chmod 000` in tests to simulate an IO failure cleanly.
// test_kind: bug_reproducer(issue-017)
#[ test ]
#[ cfg( unix ) ]
fn test_count_skips_unreadable_sessions()
{
  use std::os::unix::fs::PermissionsExt;

  let storage = TempDir::new().unwrap();
  let project_cwd = TempDir::new().unwrap();

  // Write the clean session (2 entries) first
  let encoded = common::write_path_project_session(
    storage.path(),
    project_cwd.path(),
    "aaaaaaaa-clean-ccccccccccc",
    2,
  );

  // Create a second session file then make it unreadable
  let unreadable_session = storage.path()
    .join( "projects" )
    .join( &encoded )
    .join( "bbbbbbbb-unreadable-dddddddddddd.jsonl" );
  fs::write( &unreadable_session, "" ).unwrap();
  fs::set_permissions(
    &unreadable_session,
    fs::Permissions::from_mode( 0o000 ),
  ).unwrap();

  let output = common::clg_cmd()
    .args( [ ".count" ] )
    .current_dir( project_cwd.path() )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "failed to execute .count" );

  // Restore permissions so TempDir cleanup succeeds
  fs::set_permissions(
    &unreadable_session,
    fs::Permissions::from_mode( 0o644 ),
  ).ok();

  let stdout = String::from_utf8_lossy( &output.stdout );
  let stderr = String::from_utf8_lossy( &output.stderr );

  assert!(
    output.status.success(),
    ".count must succeed even with unreadable sessions. stderr: {stderr}"
  );

  assert_eq!(
    stdout.trim(), "2",
    "Expected 2 entries from clean session only. stdout: {stdout}, stderr: {stderr}"
  );

  assert!(
    stderr.contains( "Warning" ) && stderr.contains( "corrupted" ),
    "Expected 'Warning: Skipping corrupted session ...' in stderr. stderr: {stderr}"
  );
}

/// Verification test: .count with explicit `target::projects` should still work
///
/// After fixing Bug #003a to make default context-aware, the explicit
/// `target::projects` should still count all projects globally.
// test_kind: bug_reproducer(issue-003a)
#[ test ]
fn test_count_explicit_target_projects()
{
  let storage = TempDir::new().unwrap();

  // Create 2 projects
  common::write_test_session( storage.path(), "proj-alpha", "s001", 1 );
  common::write_test_session( storage.path(), "proj-beta", "s001", 1 );

  let output = common::clg_cmd()
    .args( [ ".count", "target::projects" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "failed to execute .count" );

  let stdout = String::from_utf8_lossy( &output.stdout );
  let stderr = String::from_utf8_lossy( &output.stderr );

  assert!(
    output.status.success(),
    ".count target::projects must succeed. stderr: {stderr}"
  );

  assert_eq!(
    stdout.trim(), "2",
    "Expected 2 projects. stdout: {stdout}"
  );
}

/// Test `.count target::entries session::existing-id` succeeds
///
/// Verifies that providing a valid `session` filter with `target::entries`
/// counts only entries in that specific session.
#[ test ]
fn test_count_entries_with_session_filter()
{
  let storage = TempDir::new().unwrap();

  // Create 2 sessions: one with 3 entries, one with 5
  common::write_test_session( storage.path(), "count-sess-proj", "target-session-aaa", 3 );
  common::write_test_session( storage.path(), "count-sess-proj", "other-session-bbb", 5 );

  let output = common::clg_cmd()
    .args( [
      ".count",
      "target::entries",
      "session::target-session-aaa",
      "project::count-sess-proj",
    ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "failed to execute .count" );

  let stdout = String::from_utf8_lossy( &output.stdout );
  let stderr = String::from_utf8_lossy( &output.stderr );

  assert!(
    output.status.success(),
    ".count with session filter should succeed. stderr: {stderr}"
  );

  assert_eq!(
    stdout.trim(), "3",
    "Expected 3 entries from target session only. stdout: {stdout}"
  );
}

/// Test `.count target::entries session::` (empty) fails — framework parse error
///
/// The framework rejects `session::` (empty String parameter value) with a
/// parse error before the command reaches application code.
#[ test ]
fn test_count_entries_session_empty()
{
  let storage = TempDir::new().unwrap();

  let output = common::clg_cmd()
    .args( [ ".count", "target::entries", "session::", "project::some-proj" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "failed to execute .count" );

  let stderr = String::from_utf8_lossy( &output.stderr );
  let stdout = String::from_utf8_lossy( &output.stdout );
  let combined = format!( "{stderr}{stdout}" );

  assert!(
    !output.status.success(),
    "Should fail with empty session filter. Got: {combined}"
  );

  assert!(
    combined.to_lowercase().contains( "error" ) ||
    combined.contains( "session" ),
    "Error should be a parse or validation error. Got: {combined}"
  );
}

/// Test `.count target::entries` with partial UUID session ID (issue-019)
///
/// ## Root Cause
///
/// `count_routine` ("entries" arm) used exact string equality (`s.id() == sess_id`)
/// to locate the session, while `.show` and `.export` use prefix matching
/// (`s.id() == sid || s.id().starts_with(sid)`). A user who copies the first 8
/// characters of a UUID from `.list` output can count entries with `.show` and
/// `.export` but not with `.count target::entries`, making the UX inconsistent.
///
/// ## Why Not Caught
///
/// The partial-UUID-matching fix (issue-011) was applied to `format_session_output`
/// (used by `.show`) and to `export_routine`, but the same `find()` call in
/// `count_routine` was not updated at the same time. There was no test exercising
/// partial IDs in `target::entries` mode; all existing count tests used
/// full session IDs written by `write_test_session`.
///
/// ## Fix Applied
///
/// Changed the session lookup in `count_routine` entries arm from exact match to
/// prefix match: `s.id() == sess_id || s.id().starts_with(sess_id)`, consistent
/// with `show_routine` and `export_routine`.
///
/// ## Prevention
///
/// When applying a partial-ID-matching fix to one session lookup, grep for all
/// other `sessions.iter*().find(|s| s.id() == ...)` patterns in the codebase and
/// apply the same fix. Inconsistent ID matching across commands causes confusing
/// UX. Add tests that exercise prefix IDs in every command that accepts a
/// `session` parameter.
///
/// ## Pitfall
///
/// Partial UUID support must be applied uniformly. If `.show` accepts 8-char
/// prefixes but `.count` requires the full UUID, users who discover the shortcut
/// in one command waste time debugging why the same shortcut fails elsewhere.
// test_kind: bug_reproducer(issue-019)
#[ test ]
fn test_count_entries_partial_uuid_match()
{
  let storage = TempDir::new().unwrap();
  let session_uuid = "79f86582-1435-442c-935a-13f8d874918a";
  let session_prefix = "79f86582";

  // Write 3 entries using the full session UUID
  common::write_test_session( storage.path(), "count-partial-proj", session_uuid, 3 );

  // Bug: using 8-char prefix returns "Session not found: 79f86582"
  // Fixed: returns "3" (prefix match, consistent with .show and .export)
  let output = common::clg_cmd()
    .args( [
      ".count",
      "target::entries",
      "project::count-partial-proj",
      &format!( "session::{session_prefix}" ),
    ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "failed to execute .count" );

  let stdout = String::from_utf8_lossy( &output.stdout );
  let stderr = String::from_utf8_lossy( &output.stderr );

  assert!(
    output.status.success(),
    ".count target::entries with partial UUID must succeed. stderr: {stderr}"
  );

  assert_eq!(
    stdout.trim(), "3",
    "Expected 3 entries via partial UUID match. stdout: {stdout}"
  );
}