fix(storage): return the latest snapshot from JSONLStorage.get (#4675)

## Description

`JSONLStorage.save` appends a row per call, but `get(request_id)`
returned the first matching row, so a request saved again with updated
metrics kept returning the original snapshot. `SQLiteStorage` returns
the replacement. `get` now returns the last matching row in a single
pass. The journal is not rewritten and `iter_all`/`query` behavior is
unchanged (this is lookup parity, not deduplication).

Closes #4254

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)

## Changes Made

- `headroom/storage/jsonl.py`: `get` keeps the last match instead of
returning the first.
- `headroom/storage/jsonl.py`: `iter_all` skips malformed metric
objects, so a later `{}` does not raise and `get` returns the last valid
snapshot.
- `tests/test_storage_backends.py`: save/update/reopen test comparing
JSONL with SQLite. Fails on `main`, passes now.

## Testing

- [x] Unit tests pass: `tests/test_storage_backends.py` and
`tests/test_storage`, 63 passed (the new test failed on `main`)
- [x] Linting passes (`ruff check` / `ruff format --check` on touched
files)
- [ ] Type checking (`mypy headroom`): not run
- [x] New tests added

## Real Behavior Proof

- Environment: macOS, Python 3.12, real JSONL and SQLite files in a temp
dir.
- Steps: saved the same request ID twice (`tokens_input_after` 80 then
40) to both backends, reopened the JSONL file, called `get`.
- Result: before, JSONL returned 80 while SQLite returned 40; after,
both return 40.
- Not tested: very large journals, concurrent writers, full suite, mypy,
pre-push hooks.

## Runtime Rollout Safety

- Rollout-managed feature(s): none. `JSONLStorage.get` is not gated by a
rollout flag.
- Minimum rollout channel: n/a.
- Stable/default behavior changed: yes. `get` still returns the last
matching snapshot. The iterator now skips malformed metric rows
(syntactically valid JSON that is not a metric, such as `{}`) the same
way it already skipped invalid JSON, so lookup returns the last valid
snapshot instead of raising `KeyError`. The journal is not rewritten.
`iter_all` and `query` still yield the valid rows in the existing order.
- Kill switch / disable path: none for this lookup. Deployments that do
not use the JSONL journal are unaffected.
- Unsafe override required: no.
- Qualification impact: none. No schema change and no migration.
- Rollback path: revert this change. Existing JSONL files remain valid
as-is.

## Review Readiness

- [x] I have performed a self-review
- [x] This PR is ready for human review

## Checklist

- [x] I did **not** edit `CHANGELOG.md`

This change was prepared with AI assistance (Claude Code).

<!-- devin-review-badge-begin -->

---

<a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4675"
target="_blank"><picture><source media="(prefers-color-scheme: dark)"
srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img
src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4"
alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->

---------

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Rudy Mizrahi Celekli
2026-11-09 12:21:37 -06:00
committed by GitHub
co-authored by Claude Sonnet 5.5 Cursor
parent 364c59544c
commit b25c45b08f
2 changed files with 83 additions and 5 deletions
+18 -5
View File
@@ -108,11 +108,16 @@ class JSONLStorage(Storage):
f.write(json.dumps(data) + "\n")
def get(self, request_id: str) -> RequestMetrics | None:
"""Get metrics by request ID."""
"""Get metrics by request ID.
``save`` appends, so a request saved twice has two rows; return the last
one, matching SQLiteStorage where a later save replaces the earlier row.
"""
latest: RequestMetrics | None = None
for metrics in self.iter_all():
if metrics.request_id == request_id:
return metrics
return None
latest = metrics
return latest
def query(
self,
@@ -211,9 +216,17 @@ class JSONLStorage(Storage):
continue
try:
data = json.loads(line)
yield self._dict_to_metrics(data)
except json.JSONDecodeError:
# Skip malformed lines
# Skip malformed lines. The journal is not rewritten.
continue
if not isinstance(data, dict):
continue
try:
yield self._dict_to_metrics(data)
except (KeyError, TypeError, ValueError, AttributeError):
# Skip syntactically valid rows that are not metric objects,
# such as `{}` (no `id`). A later bad row must not hide an
# earlier valid snapshot from get(), query(), or count().
continue
def get_summary_stats(
+65
View File
@@ -279,6 +279,71 @@ def test_storage_plugin_class_name_cannot_leak_or_forge_log_lines(
assert "\n" not in rendered
def test_jsonl_storage_get_returns_latest_snapshot_for_a_repeated_request_id(
tmp_path: Path,
) -> None:
"""Saving a request ID again must be visible to get(), as in SQLite (#4254)."""
now = datetime(2026, 4, 23, 12, 0, 0)
initial = _metrics("req", now, after=80)
other = _metrics("other", now + timedelta(seconds=1))
updated = _metrics("req", now, after=40)
jsonl_storage = JSONLStorage(str(tmp_path / "metrics.jsonl"))
sqlite_storage = SQLiteStorage(str(tmp_path / "metrics.db"))
try:
for storage in (jsonl_storage, sqlite_storage):
storage.save(initial)
storage.save(other)
storage.save(updated)
reopened = JSONLStorage(str(tmp_path / "metrics.jsonl"))
assert reopened.get("req") == updated
assert jsonl_storage.get("req") == sqlite_storage.get("req") == updated
finally:
sqlite_storage.close()
def test_jsonl_storage_get_skips_malformed_metric_rows_and_returns_last_valid_snapshot(
tmp_path: Path,
) -> None:
"""A later invalid JSON object must not hide the last valid snapshot.
``{}`` parses but has no metric fields. ``get`` scans the whole journal,
so that row used to raise ``KeyError`` and drop the valid match. The file
stays as written, and query/iteration still yield only the valid rows.
"""
now = datetime(2026, 4, 23, 12, 0, 0)
first = _metrics("req", now, after=80)
latest = _metrics("req", now + timedelta(seconds=5), after=40)
no_later = tmp_path / "no-later.jsonl"
no_later_storage = JSONLStorage(str(no_later))
no_later_storage.save(first)
with no_later.open("a", encoding="utf-8") as handle:
handle.write("{}\n")
no_later_journal = no_later.read_bytes()
assert no_later_storage.get("req") == first
assert list(no_later_storage.iter_all()) == [first]
assert no_later_storage.query() == [first]
assert no_later_storage.count() == 1
assert no_later.read_bytes() == no_later_journal
with_later = tmp_path / "with-later.jsonl"
with_later_storage = JSONLStorage(str(with_later))
with_later_storage.save(first)
with with_later.open("a", encoding="utf-8") as handle:
handle.write("{}\n")
with_later_storage.save(latest)
with_later_journal = with_later.read_bytes()
assert with_later_storage.get("req") == latest
assert [row.tokens_input_after for row in with_later_storage.iter_all()] == [80, 40]
assert [row.tokens_input_after for row in with_later_storage.query()] == [40, 80]
assert with_later_storage.count() == 2
assert with_later.read_bytes() == with_later_journal
def test_jsonl_storage_round_trip_query_count_and_summary(tmp_path: Path) -> None:
storage = JSONLStorage(str(tmp_path / "metrics.jsonl"))
now = datetime(2026, 4, 23, 12, 0, 0)