Skip to content

Add Claude Code JSONL session import#2

Closed
Siddhant-K-code wants to merge 3 commits into
mainfrom
feature/jsonl-import
Closed

Add Claude Code JSONL session import#2
Siddhant-K-code wants to merge 3 commits into
mainfrom
feature/jsonl-import

Conversation

@Siddhant-K-code
Copy link
Copy Markdown
Owner

Summary

Imports Claude Code's native JSONL session logs (~/.claude/projects/) into agent-trace format, enabling replay, export, and stats on existing sessions without hooks.

Originally contributed by @ywatanabe1989 in #1. This version addresses all review feedback from that PR.

Changes

  • src/agent_trace/jsonl_import.py — JSONL parser, importer, CLI handler, session discovery
  • src/agent_trace/cli.pyimport subcommand and --discover flag
  • tests/test_jsonl_import.py — 5 unit tests

Parses: tool_use/tool_result blocks, token usage (input/output/cache), subagent detection (isSidechain, subagent_type, caller.type), session metadata.

Fixes over #1

Issue Fix
--discover wrote listing to stderr Switched to stdout so output can be piped/grepped
Session ID truncated to 16 chars Preserve full session UUID from JSONL for cross-referencing
content_preview truncated to 200 chars Consistent 2000-char limit with ... indicator, matching other events
Duplicate TOOL_RESULT events Guard toolUseResult path with not tool_results to avoid emitting twice for the same call
Assistant text lost when tool calls present Emit ASSISTANT_RESPONSE whenever text is non-empty, regardless of accompanying tool calls
discover opened every JSONL file to read session ID Use jsonl_file.stem directly — Claude Code names files <session-uuid>.jsonl
_decode_project_path was O(n²) and filesystem-dependent Replaced with encoded.replace("-", "/")

Note on path decoding: The simple replace is correct for display in --discover but is ambiguous for project names containing hyphens (e.g. /home/user/my-project). This is an inherent limitation of Claude Code's encoding scheme.

Test Plan

  • All 135 tests pass (130 existing + 5 new)

ywatanabe1989 and others added 2 commits March 22, 2026 07:20
## Summary
Import Claude Code's native JSONL session logs (~/.claude/projects/) into agent-trace format, enabling replay, export, and stats on existing sessions without hooks.

## Changes
- `src/agent_trace/jsonl_import.py` — JSONL parser, importer, CLI handler, session discovery
- `src/agent_trace/cli.py` — added `import` subcommand and `--discover` flag (3 lines)
- `tests/test_jsonl_import.py` — 5 unit tests

Parses: tool_use/tool_result blocks, token usage (input/output/cache), subagent detection (isSidechain, subagent_type, caller.type), session metadata.

## Test Plan
- All 135 tests pass (130 existing + 5 new)
- Tested with real Claude Code session (315-line JSONL, 38 tool calls, 4M tokens)

## Checklist
- [x] Follows existing code style
- [x] Tests pass
- [x] Documentation updated (if applicable)

Co-authored-by: Ona <no-reply@ona.com>
- --discover output goes to stdout (was stderr), enabling pipe/grep
- Preserve full session ID from JSONL instead of truncating to 16 chars
- Consistent 2000-char truncation with '...' indicator for tool result previews
- Guard toolUseResult path with 'not tool_results' to prevent duplicate TOOL_RESULT events
- Emit ASSISTANT_RESPONSE even when tool calls are present in the same message
- discover_claude_sessions uses jsonl_file.stem directly, no per-file open
- Replace O(n²) filesystem-probing _decode_project_path with simple str.replace

Co-authored-by: Ona <no-reply@ona.com>
Siddhant-K-code added a commit that referenced this pull request Mar 22, 2026
Imports Claude Code native JSONL session logs (~/.claude/projects/) into
agent-trace format, enabling replay, export, and stats on existing sessions
without hooks.

- New `import` subcommand and `--discover` flag in CLI
- Parses tool_use/tool_result blocks, token usage, subagent detection
- 5 new unit tests (135 total passing)

Co-authored-by: Ona <no-reply@ona.com>
@Siddhant-K-code
Copy link
Copy Markdown
Owner Author

@Siddhant-K-code Siddhant-K-code deleted the feature/jsonl-import branch March 22, 2026 07:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants