Skip to content

Speed up cwd-filtered listing and tidy up with clippy - #65

Merged
nihen merged 2 commits into
mainfrom
feature/refactor-perf-tuning
Nov 26, 2026
Merged

nihen merged 2 commits into
mainfrom
feature/refactor-perf-tuning

Conversation

@nihen

@nihen nihen commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Summary

ah log in a project directory (the default, cwd-filtered listing) spent most of its time on work the result did not need. This PR removes that work, and tidies the code with clippy and a duplicate-code scan.

Performance

  • Sessions are mapped after the cwd/agent filters. Every session file used to be mapped before the filters ran, although most are rejected. Plugins that read the cwd from those bytes say so through the new AgentPlugin::cwd_in_session_bytes (pi).
  • The Claude cwd is read from the first 32 KiB instead of mapping the whole file. When those bytes do not decide it (a longer file whose complete lines name no cwd, or not a regular file), the previous path is used.
  • Running sessions are looked up while the files are scanned, one thread per plugin. This step used to run serially after the parallel stage and took about 30 ms.
  • Model lines are found by searching for the needle (LinesWith) instead of splitting the file into lines and searching each line.

Refactoring

  • ah project: projects that tie on the sort field are ordered by name. They used to follow hash map order, so the output changed between runs. This is the only intended change in output.
  • remote.rs: the two functions that forward filters to the remote ah share push_value / push_flag / push_limit. Order and conditions are unchanged.
  • search.rs: search_prompts and search_texts share match_context.
  • The remaining byte-at-a-time line splits over session data use jsonl_lines.
  • A selected set of clippy pedantic/nursery fixes (map_or, let … else, redundant clones, Eq derives, raw string hashes). Lints that only produce churn (use_self, inlined format args) were left out.

Performance

About 9,800 session files, 32 threads, warm page cache, hyperfine:

command before after
ah log 118 ms 48 ms
ah -q tokio log 116 ms 48 ms
ah -a project 66 ms 45 ms
ah -a log 694 ms 642 ms
ah -a -q tokio log 577 ms 557 ms

With RAYON_NUM_THREADS=4, ah log went from 122 ms to 80 ms (measured before the last two fixes of this branch).

Known limitations

  • -a commands gain little: they are bound by reading about 10 GB of JSONL on every run. Going further needs a per-file result cache, which is not part of this PR.
  • A Claude session whose cwd is not within the first 32 KiB (about 3% on the test host) is read twice: the head, then the mapping.
  • pi has no sessions on the test host, so its path was checked by reading the code and its unit tests only.

Verification

  • cargo test (486 tests), cargo clippy --all-targets -- -D warnings, cargo fmt --check.
  • Release builds of main and this branch give identical output on real data for log (with and without -a, --json, -q, -p -q, --raw-search, --running, --subagents, -o id,cwd,project, -o id,model,models, -o id,turns,first_prompt), memory, agent and show. project differs only in the order of tied rows.
  • New unit tests: LinesWith against a line-by-line scan, the Claude cwd from the head against the whole file (including the 32 KiB boundary), and a FIFO as a session path.

https://claude.ai/code/session_013M2MLyLFJxdYU9rz7M7Utt

- Map session files only after the cwd/agent filters, unless the plugin
  reads the cwd from those bytes.
- Read the Claude cwd from the first 32 KiB instead of mapping the file.
- Look up running sessions while the files are scanned, one thread per
  plugin.
- Find model lines by searching for the needle instead of splitting the
  file into lines first.
- Sort projects by name before the stable sort, so ties no longer follow
  hash map order.
- Share the option forwarding of the remote commands and the match
  context of the searches; use `jsonl_lines` for every line split.
- Apply a selected set of clippy pedantic/nursery fixes.

Claude-Session: https://claude.ai/code/session_013M2MLyLFJxdYU9rz7M7Utt
@nihen
nihen requested a balanced review from Copilot November 26, 2026 04:22
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T04:23:58.966270Z 21f64fa PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A critical issue in src/agents/claude.rs can cause cwd resolution to reopen a FIFO and block indefinitely; non-regular files must return Some(None) to avoid the mapping fallback.

1 open issue
What changed in this PR

This PR speeds up directory-filtered listing in the cross-agent history CLI by avoiding unnecessary session reads and overlapping running-session detection with scanning. It also makes tied project ordering deterministic and applies targeted cleanup.

Changes:

  • Delay session mapping until after early filters, read Claude cwd from a bounded prefix, and accelerate model-line searches.
  • Run plugin running-session lookups concurrently.
  • Share filtering and search helpers, standardize JSONL iteration, and simplify code with clippy fixes.
File Description
tests/​search.rs Simplify a raw-string fixture.
tests/​cloud.rs Add a closure statement terminator.
tests/​cli.rs Clean up fixture syntax.
src/​subcmd.rs Simplify agent lookup defaults.
src/​show.rs Simplify field access and statement syntax.
src/​search.rs Share match-context extraction.
src/​resume.rs Simplify resume-command lookup.
src/​resolver.rs Simplify defaults and remove a prompt clone.
src/​remote.rs Share remote argument helpers.
src/​projects.rs Order tied projects by name.
src/​pipeline.rs Delay mapping and overlap running detection.
src/​pager.rs Simplify stdin extraction control flow.
src/​output.rs Simplify output field defaults.
src/​memory.rs Simplify metadata and parsing expressions.
src/​mcp/​tools.rs Remove a clone and simplify body sizing.
src/​fuzzy.rs Simplify field access and patterns.
src/​config.rs Simplify configuration handling.
src/​cloud/​providers.rs Simplify coverage fallback and fixture ownership.
src/​cli.rs Add equality derives and simplify expressions.
src/​agents/​pi.rs Declare byte-backed cwd resolution.
src/​agents/​opencode.rs Simplify a JSONC matching pattern.
src/​agents/​mod.rs Add cwd capability and concurrent running lookups.
src/​agents/​gemini.rs Standardize JSONL iteration and simplify replay.
src/​agents/​dsh.rs Use shared JSONL iteration.
src/​agents/​copilot.rs Simplify workspace fallback and test syntax.
src/​agents/​common.rs Add needle-based line lookup and tests.
src/​agents/​codex.rs Accelerate model scanning and standardize iteration.
src/​agents/​claude.rs Add bounded cwd reads and regression fixtures.
src/​agents/​agy.rs Simplify a raw-string fixture.
src/​agent_stats.rs Simplify plugin and field defaults.

Review effort: Balanced

Comment thread src/agents/claude.rs Resolved
A FIFO whose writer has gone would block the open of the mapping
fallback, so such a path now resolves to no cwd right away.

Claude-Session: https://claude.ai/code/session_013M2MLyLFJxdYU9rz7M7Utt

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Agent-only filters still unnecessarily map Pi sessions before rejecting them because the cwd plugin opt-in is not gated on a cwd filter being present.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Low severity Avoid mapping sessions for agent-only filters

src/​pipeline.rs:195

With an agent-only filter such as ah -a --agent claude log, Pi's cwd_in_session_bytes() still makes load_early true for every pi session. Resolving Field::Agent needs no cwd or session bytes, so these rejected sessions are unnecessarily mapped before filtering. Gate the plugin opt-in on a cwd filter actually being present so agent-only listings also benefit from deferred mapping.

1 resolved since last review

Review effort: Balanced

@nihen
nihen merged commit 9bb6911 into main Nov 26, 2026
4 checks passed
@nihen
nihen deleted the feature/refactor-perf-tuning branch November 26, 2026 05:08
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