Skip to content

fix(capture-prior): extract tool calls via adapter protocol method#455

Merged
rsnodgrass merged 3 commits intomainfrom
ryan/capture-prior-bug
Apr 8, 2026
Merged

fix(capture-prior): extract tool calls via adapter protocol method#455
rsnodgrass merged 3 commits intomainfrom
ryan/capture-prior-bug

Conversation

@rsnodgrass
Copy link
Copy Markdown
Contributor

@rsnodgrass rsnodgrass commented Apr 7, 2026

Summary

  • Bug: capture-prior silently dropped tool calls (Read, Edit, Write, Bash) and their results from Claude Code sessions — only high-level user/assistant text was captured
  • Fix: Added capture-prior as a first-class adapter protocol method so each adapter (Claude Code, Codex, Gemini, etc.) owns its own session parsing logic
  • Claude Code adapter now extracts tool_use blocks (with call IDs) from assistant messages and tool_result blocks from user messages
  • CLI auto-detects the active adapter and delegates — no --from-claude flag needed; falls back to JSONL stdin/file when no adapter is available

Changes

graph LR
    CLI["ox capture-prior"] -->|DetectAdapter| A["Active Adapter"]
    A -->|capture-prior protocol| CP["handleCapturePrior"]
    CP --> Parse["Parse native JSONL"]
    Parse --> Entries["[]RawEntry with tools"]
    Entries -->|ConvertProtocolEntriesToHistory| H["CapturedHistory"]
    H --> Redact["Secret redaction"]
    Redact --> Store["Ledger storage"]
    
    CLI -->|stdin/--file fallback| V["ValidateCapturePriorInput"]
    V --> Redact
Loading

Protocol layer (pkg/adapterprotocol, pkg/adapterruntime):

  • Added CapturePriorParams/CapturePriorResult types and CapCapturePrior capability
  • Added capture-prior subcommand dispatch in adapter runtime

Claude Code adapter (cmd/ox-adapter-claude-code):

  • handleCapturePrior — finds and parses the native session file
  • extractToolResults — extracts tool_result content blocks from user messages
  • tool_use blocks now preserve callID for call/result correlation

Session internals (internal/session):

  • ConvertProtocolEntriesToHistory — bridges []RawEntryCapturedHistory
  • CapturePriorFromHistory — stores pre-parsed history (skips JSONL validation)
  • Relaxed content validation for tool entries (they use tool_name/tool_input/tool_output, not content)

Test plan

  • 5 new adapter tests: tool_use with callID, tool_result extraction, mixed content, nested content, missing callID
  • 4 new converter tests: basic conversion, time range, empty input, tool error preservation
  • CLI arg parsing tests for --session-id, --file
  • make lint — 0 issues
  • make test — 12321 tests pass

Co-Authored-By: SageOx

Summary by CodeRabbit

  • New Features

    • Capture prior conversation history from active coding agents using session identifiers.
    • Enhanced tool interaction tracking with improved result parsing and identification.
    • Added session ID parameter support for capture operations.
  • Enhancements

    • Improved worker ID generation with extended random identifier suffix.
    • Refined history validation to better support tool-based interactions.
  • Tests

    • Added comprehensive tests for tool extraction, session ID parsing, and protocol conversion.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 7, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: dd9fdacc-3024-4b9c-a2f1-a713221a5bc2

📥 Commits

Reviewing files that changed from the base of the PR and between 00df611 and d1a0489.

📒 Files selected for processing (14)
  • cmd/ox-adapter-claude-code/main.go
  • cmd/ox-adapter-claude-code/session.go
  • cmd/ox-adapter-claude-code/session_test.go
  • cmd/ox/agent_capture_prior_coverage_test.go
  • cmd/ox/agent_session_capture_prior.go
  • internal/daemon/worker_tracker.go
  • internal/daemon/worker_tracker_test.go
  • internal/session/adapters/external.go
  • internal/session/capture.go
  • internal/session/convert.go
  • internal/session/convert_test.go
  • internal/session/history_schema.go
  • pkg/adapterprotocol/types.go
  • pkg/adapterruntime/runtime.go

📝 Walkthrough

Walkthrough

This PR introduces a new "capture-prior" adapter capability that enables session history retrieval. It defines the protocol types, extends the adapter runtime with capture-prior dispatch, implements the handler in the Claude adapter with enhanced session parsing for tool correlation, adds session history conversion utilities, and updates the CLI to delegate capture operations to adapters with stdin/file fallback.

Changes

Cohort / File(s) Summary
Adapter Protocol Definition
pkg/adapterprotocol/types.go
Added CapCapturePrior capability constant and new wire types: CapturePriorParams (input) and CapturePriorResult (output with entries, metadata, agent type, and resolved session ID).
Adapter Runtime Dispatch
pkg/adapterruntime/runtime.go
Extended Config struct with CapturePrior handler field; added capture-prior subcommand dispatch with parameter parsing from CLI flags.
Claude Adapter Implementation
cmd/ox-adapter-claude-code/main.go
Registered and implemented handleCapturePrior to locate session files, read entries/metadata, and return results with resolved session ID.
Claude Session Parsing Enhancements
cmd/ox-adapter-claude-code/session.go, session_test.go
Updated parseUserEntry and parseAssistantEntry to extract tool results and tool use blocks with call IDs; added helpers extractToolResults and extractToolResultContent; added five comprehensive tests covering tool extraction scenarios.
Session History Conversion
internal/session/convert.go, convert_test.go
Added ConvertProtocolEntriesToHistory function to transform adapter protocol entries to internal history format with metadata and time range computation; added HistorySourceAdapterImport constant and mapRoleToHistoryType helper; included four conversion test cases.
Session Capture and Storage
internal/session/capture.go, adapters/external.go, history_schema.go
Added CapturePriorFromHistory for processing captured history with secret redaction and storage; added CapturePrior method to ExternalAdapter for one-shot subcommand invocation; relaxed history validation to permit empty content for tool entries.
CLI Capture-Prior Command
cmd/ox/agent_session_capture_prior.go
Refactored to attempt adapter-based capture when available (with capability check) before falling back to stdin/file; added helpers: capturePriorViaAdapter, findCapturePriorAdapter, isStdinPiped, parseSessionID; documented --session-id flag.
Supporting Changes
cmd/ox/agent_capture_prior_coverage_test.go, internal/daemon/worker_tracker.go, worker_tracker_test.go
Added TestParseSessionID unit test; increased worker ID random suffix from 4 to 6 characters with corresponding test update.

Sequence Diagram(s)

sequenceDiagram
    participant CLI as CLI<br/>(capture-prior)
    participant AdapterMgr as Adapter<br/>Discovery
    participant Adapter as External<br/>Adapter
    participant SessionMgr as Session<br/>Manager
    participant History as History<br/>Converter

    CLI->>CLI: Parse --session-id, detect project root
    CLI->>AdapterMgr: Find adapter with capture-prior capability
    AdapterMgr-->>CLI: Return selected adapter
    
    CLI->>Adapter: Call CapturePrior(session_id, repo_root, agent_id, ...)
    Adapter->>Adapter: Construct CLI args and invoke<br/>adapter capture-prior subcommand
    Adapter-->>CLI: Return CapturePriorResult<br/>(entries, metadata, agent_type)
    
    CLI->>History: ConvertProtocolEntriesToHistory(entries, agent_id, agent_type)
    History-->>CLI: Return CapturedHistory<br/>(with metadata, time range)
    
    CLI->>SessionMgr: CapturePriorFromHistory(history, opts)
    SessionMgr->>SessionMgr: Validate, redact secrets,<br/>compute metadata
    SessionMgr->>SessionMgr: StoreCapturedHistory(...)
    SessionMgr-->>CLI: Return CaptureResult<br/>(path, entry count, agent_id)
    
    CLI-->>CLI: Output result
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

🐰 A whisper through sessions past,
Tool calls bound, their IDs vast,
Prior captured, history flows,
From adapters now our wisdom grows! 🌟

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ryan/capture-prior-bug

Comment @coderabbitai help to get the list of available commands and usage tips.

@rsnodgrass rsnodgrass force-pushed the ryan/capture-prior-bug branch from 1079795 to 6437127 Compare April 7, 2026 22:05
@rsnodgrass rsnodgrass marked this pull request as ready for review April 8, 2026 01:39
@rsnodgrass rsnodgrass merged commit fb158ea into main Apr 8, 2026
1 of 3 checks passed
@rsnodgrass rsnodgrass deleted the ryan/capture-prior-bug branch April 8, 2026 01:39
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.

1 participant