From 50b4b34d33654d84b19b42a2cbdcc6ce6c98fa1f Mon Sep 17 00:00:00 2001 From: TejasLamba2006 Date: Wed, 11 Nov 2026 01:41:37 +0530 Subject: [PATCH 1/2] fix(plugin): guard against non-dict ledger records in accounting and repos store.read_lines can return non-dict JSON values (null, arrays, strings) if a ledger file was corrupted or written incorrectly. Code calling .get() on these records crashes with AttributeError. Added isinstance(r, dict) checks before accessing record fields in: - accounting.summarize (filters at entry) - repos._infer_repo_from_state (inline check) - repos._merge_state (filters ledger lists, inline check for events) Same pattern as the distiller fix in #199. --- tokeneconomizer/plugin/accounting.py | 1 + tokeneconomizer/plugin/repos.py | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/tokeneconomizer/plugin/accounting.py b/tokeneconomizer/plugin/accounting.py index cab043ac..36e7d682 100644 --- a/tokeneconomizer/plugin/accounting.py +++ b/tokeneconomizer/plugin/accounting.py @@ -35,6 +35,7 @@ def summarize(records): A session imported from the agent's history (`imported: true`) ran before the plugin: it is counted on its own (`imported_sessions`) and left out of every other number.""" + records = [r for r in records if isinstance(r, dict)] imported = [r for r in records if r.get("kind") == "session" and r.get("imported")] records = [r for r in records if not r.get("imported")] sessions = [r for r in records if r.get("kind") == "session"] diff --git a/tokeneconomizer/plugin/repos.py b/tokeneconomizer/plugin/repos.py index e6a05ffc..aa8b46cb 100644 --- a/tokeneconomizer/plugin/repos.py +++ b/tokeneconomizer/plugin/repos.py @@ -235,7 +235,7 @@ def dir_label(repo_dir): if names: return names[0][:-5].replace("__", "/") for rec in reversed(st.read_lines("ledger.jsonl")): - if rec.get("repo"): + if isinstance(rec, dict) and rec.get("repo"): return rec["repo"] return "" @@ -386,7 +386,8 @@ def _merge_host(src, dst, sids, whole, label="", timeout=LOCK_WAIT_S): refold.add(sid) incoming = set(sids) - s_ledger, d_ledger = src.read_lines("ledger.jsonl"), dst.read_lines("ledger.jsonl") + s_ledger = [r for r in src.read_lines("ledger.jsonl") if isinstance(r, dict)] + d_ledger = [r for r in dst.read_lines("ledger.jsonl") if isinstance(r, dict)] # One session record per session: 0.1.0 could fold a session it had split over two keys # under both, and its cost counts once -- the later fold's, which read the longer log. # A refolded session's record is written again by its fold. @@ -421,7 +422,7 @@ def _merge_host(src, dst, sids, whole, label="", timeout=LOCK_WAIT_S): rec["folded"] = False dst.write(os.path.join("sessions", f"{sid}.json"), rec) for r in src.read_lines("events.jsonl"): - if r.get("session_id") in incoming: + if isinstance(r, dict) and r.get("session_id") in incoming: dst.append("events.jsonl", r) if whole: _merge_envelope(src, dst) From 25ae385a2a60c1de088b23ded5c2f5ffe744857f Mon Sep 17 00:00:00 2001 From: TejasLamba2006 Date: Sat, 14 Nov 2026 12:38:58 +0530 Subject: [PATCH 2/2] fix(plugin): read_lines returns only object records A non-object JSONL line (null, a list, a string) came back as a record, so a trailing null with limit=1 hid the last valid record and callers crashed on it. Filter to dict records before the limit slice, so every reader gets dicts and the inline checks in repos.py are not needed. accounting.summarize keeps its own filter for direct lists. --- tests/test_store_read_lines.py | 32 ++++++++++++++++++++++++++++++++ tokeneconomizer/plugin/repos.py | 7 +++---- tokeneconomizer/plugin/store.py | 6 ++++-- 3 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 tests/test_store_read_lines.py diff --git a/tests/test_store_read_lines.py b/tests/test_store_read_lines.py new file mode 100644 index 00000000..26f7d842 --- /dev/null +++ b/tests/test_store_read_lines.py @@ -0,0 +1,32 @@ +"""Store.read_lines: only JSON-object records are returned, and `limit` counts those.""" +import os + +from tokeneconomizer.plugin import accounting +from tokeneconomizer.plugin.store import Store + + +def _write(store, name, lines): + os.makedirs(store.base, exist_ok=True) + with open(store.path(name), "w", encoding="utf-8") as fh: + fh.write("\n".join(lines) + "\n") + + +def test_read_lines_skips_non_object_json(tmp_path): + s = Store(str(tmp_path / "st")) + _write(s, "ledger.jsonl", ['{"n": 1}', "null", "[]", '"str"', "3", '{"n": 2}', "null"]) + assert s.read_lines("ledger.jsonl") == [{"n": 1}, {"n": 2}] + + +def test_read_lines_limit_counts_only_object_records(tmp_path): + s = Store(str(tmp_path / "st")) + _write(s, "ledger.jsonl", ['{"n": 1}', '{"n": 2}', "null"]) + assert s.read_lines("ledger.jsonl", limit=1) == [{"n": 2}] + + +def test_summarize_tolerates_non_object_records(tmp_path): + s = Store(str(tmp_path / "st")) + valid = {"kind": "session", "session_id": "a", "imported": False} + _write(s, "ledger.jsonl", ["null", "[]", '"str"', '{"kind": "session", "session_id": "a", ' + '"imported": false}']) + assert accounting.summarize(s.read_lines("ledger.jsonl")) == accounting.summarize([valid]) + assert accounting.summarize([None, [], "str", valid]) == accounting.summarize([valid]) diff --git a/tokeneconomizer/plugin/repos.py b/tokeneconomizer/plugin/repos.py index aa8b46cb..e6a05ffc 100644 --- a/tokeneconomizer/plugin/repos.py +++ b/tokeneconomizer/plugin/repos.py @@ -235,7 +235,7 @@ def dir_label(repo_dir): if names: return names[0][:-5].replace("__", "/") for rec in reversed(st.read_lines("ledger.jsonl")): - if isinstance(rec, dict) and rec.get("repo"): + if rec.get("repo"): return rec["repo"] return "" @@ -386,8 +386,7 @@ def _merge_host(src, dst, sids, whole, label="", timeout=LOCK_WAIT_S): refold.add(sid) incoming = set(sids) - s_ledger = [r for r in src.read_lines("ledger.jsonl") if isinstance(r, dict)] - d_ledger = [r for r in dst.read_lines("ledger.jsonl") if isinstance(r, dict)] + s_ledger, d_ledger = src.read_lines("ledger.jsonl"), dst.read_lines("ledger.jsonl") # One session record per session: 0.1.0 could fold a session it had split over two keys # under both, and its cost counts once -- the later fold's, which read the longer log. # A refolded session's record is written again by its fold. @@ -422,7 +421,7 @@ def _merge_host(src, dst, sids, whole, label="", timeout=LOCK_WAIT_S): rec["folded"] = False dst.write(os.path.join("sessions", f"{sid}.json"), rec) for r in src.read_lines("events.jsonl"): - if isinstance(r, dict) and r.get("session_id") in incoming: + if r.get("session_id") in incoming: dst.append("events.jsonl", r) if whole: _merge_envelope(src, dst) diff --git a/tokeneconomizer/plugin/store.py b/tokeneconomizer/plugin/store.py index 9c3accbd..2c00e21c 100644 --- a/tokeneconomizer/plugin/store.py +++ b/tokeneconomizer/plugin/store.py @@ -103,15 +103,17 @@ def append(self, name, record): fh.write(json.dumps(record, sort_keys=True) + "\n") def read_lines(self, name, limit=None): - """Records of a JSONL log, oldest first; the last `limit` when given.""" + """Object records of a JSONL log, oldest first; the last `limit` when given.""" out = [] try: with open(self.path(name), encoding="utf-8") as fh: for line in fh: try: - out.append(json.loads(line)) + rec = json.loads(line) except ValueError: continue + if isinstance(rec, dict): + out.append(rec) except OSError: return [] return out[-limit:] if limit else out