Skip to content

perf(search): the prefilter was matching JSON field names, not commands - #26

Merged
matinsaurralde merged 1 commit into
masterfrom
fix/20-prefilter-scans-the-command
Aug 4, 2026
Merged

matinsaurralde merged 1 commit into
masterfrom
fix/20-prefilter-scans-the-command

Conversation

@matinsaurralde

Copy link
Copy Markdown
Owner

The raw-line prefilter scanned the whole encoded record, and every record contains the field names command, dir, exit_code, duration_ms, session, imported.

So a search for exit — or dir, command, port, code, ms, ts — matched every line in the store. The filter admitted all of them and saved nothing, precisely for the queries most likely to be slow. Measured over 20,000 records:

exit      -> 20000/20000 lines matched   (no filtering whatsoever)
dir       -> 20000/20000
command   -> 20000/20000
git       ->  2000/20000

It was never a correctness bug — _matches re-checks the parsed command, and the command field is the only thing it looks at — which is exactly why nobody noticed. Narrowing the scan to the same field the real filter uses makes the two agree.

Found while building the concept map, where port queries hit it through "imported":false.

The first attempt was worse than the bug

Walking the line character by character in Python to locate the field cost 165 ms per 100k records — against 137 ms for simply parsing all of them. A filter slower than the work it skips is worse than no filter, and the benchmark caught it before it shipped.

The version here is str.find, which runs in C, with a fast path for the shape mem itself writes and a Python loop only for the rare escaped quote:

query parsed before after
exit 0 (was 100,000) 137 ms 62 ms
git 10,096 — 76 ms
no filter 100,000 140 ms 140 ms

Safety

A record it cannot recognise — hand-edited, truncated, written by another tool — is scanned whole. Admitting too much is the safe direction; excluding a real match is not. The span is the encoded text, which is what the needles are built to match: decoding is the expensive step this path exists to skip.

Tests

Both failure modes are pinned, because each of them is silent:

  • a field name no longer matches every record;
  • a perf test that a field-name query is not slower than no filter at all — the regression that nearly shipped.

Plus the escaped-quote, non-ASCII, tab, unterminated, missing-field and wrong-type cases, each asserting the span equals json.dumps(command)[1:-1] rather than a hand-written expectation.

1309 passed, 90% branch coverage.

The raw-line prefilter scanned the whole encoded record, and every record
contains the field *names* `command`, `dir`, `exit_code`, `duration_ms`,
`session` and `imported`. So searching for `exit` — or `dir`, `command`,
`port`, `code`, `ms`, `ts` — matched **every line in the store**, the filter
admitted all of them, and the queries most likely to be slow were the ones
that got no speedup at all.

Measured over 20,000 records before the fix:

    exit     -> 20000/20000 lines matched   (no filtering whatsoever)
    dir      -> 20000/20000
    command  -> 20000/20000
    git      ->  2000/20000

Never a correctness bug — `_matches` re-checks the parsed command, and the
command field is the only thing it looks at — which is exactly why nobody
noticed. Narrowing the scan to the same field the real filter uses makes the
two agree. Found while building the concept map, where `port` queries hit it
through `"imported":false`.

The first attempt was worse than the bug. Walking the line character by
character in Python to find the field cost **165ms per 100k records against
137ms for simply parsing all of them** — a filter slower than the work it
skips. The version here is `str.find` in C, with a fast path for the shape
mem itself writes and a Python loop only for the rare escaped quote:

    exit     ->      0 parsed    62ms    (was: 100000 parsed, 137ms)
    git      ->  10096 parsed    76ms
    no filter -> 100000 parsed  140ms

A record it cannot recognise — hand-edited, truncated, written by another
tool — is scanned whole. Admitting too much is the safe direction; excluding
a real match is not.

Both failure modes are now pinned: a test that a field name no longer matches
every record, and a `perf` test that a field-name query is not slower than no
filter at all.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5b8443b297

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/mem/storage.py
produced by another tool — so it tolerates whitespace the compact
encoder never emits.
"""
start = line.find(_COMMAND_KEY)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Restrict extraction to the top-level command key

For hand-edited records or records produced by another tool, _locate_value selects the first "command" key at any nesting level rather than the top-level field that Pydantic will deserialize. For example, {"metadata":{"command":"git"},"command":"docker ps","ts":1,"dir":"/x"} yields a span of git while CapturedCommand.from_jsonl yields docker ps, so a search for docker silently skips the valid record. Locate the top-level key or fall back to scanning the entire line when that cannot be guaranteed.

Useful? React with 👍 / 👎.

Comment thread src/mem/storage.py
continue
if needles:
lowered = line.lower()
lowered = command_span(line).lower()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Apply command-only filtering to the interactive finder

This changes only storage.read_commands, but the inspected Ctrl+R path calls tui.rank, whose candidate_lines still executes lowered = line.lower() over the complete JSON record (src/mem/tui.py:187-202). Consequently queries such as exit, command, or port still admit and parse every history entry on every keystroke in the latency-sensitive interactive finder, leaving that user-facing search path unaffected by the optimization.

Useful? React with 👍 / 👎.

@matinsaurralde
matinsaurralde merged commit 97ced8e into master Aug 4, 2026
5 checks passed
@matinsaurralde
matinsaurralde deleted the fix/20-prefilter-scans-the-command branch August 4, 2026 05:29
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