From 85c97cc23a463c821ce650368c839a9572cb1c76 Mon Sep 17 00:00:00 2001 From: TejasLamba2006 Date: Wed, 11 Nov 2026 00:50:14 +0530 Subject: [PATCH 1/3] fix(microskills): guard parse_payload against wrongly-typed proposer fields - Handle extractors/facts as null (not just missing key) - Filter extractors with non-string pattern - Filter facts with non-dict detector - Guard _rx against non-string pattern input Fixes #173 --- tokeneconomizer/microskills/distiller.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tokeneconomizer/microskills/distiller.py b/tokeneconomizer/microskills/distiller.py index a1532ef8..953b189c 100644 --- a/tokeneconomizer/microskills/distiller.py +++ b/tokeneconomizer/microskills/distiller.py @@ -261,7 +261,7 @@ def proposer_view(dg): def _rx(pat): - if not pat: + if not pat or not isinstance(pat, str): return None try: return re.compile(pat) @@ -526,10 +526,10 @@ def parse_payload(raw): obj = json.loads(m.group(0)) except Exception: return [], [] - exts = [e for e in obj.get("extractors", []) - if isinstance(e, dict) and e.get("name") and e.get("pattern")] - facts = [f for f in obj.get("facts", []) - if isinstance(f, dict) and f.get("id") and f.get("detector")] + exts = [e for e in (obj.get("extractors") or []) + if isinstance(e, dict) and e.get("name") and isinstance(e.get("pattern"), str)] + facts = [f for f in (obj.get("facts") or []) + if isinstance(f, dict) and f.get("id") and isinstance(f.get("detector"), dict)] return exts, facts From 30e07e04553a59d4a366aab16ec91c44eadb3edf Mon Sep 17 00:00:00 2001 From: TejasLamba2006 Date: Sat, 14 Nov 2026 12:36:43 +0530 Subject: [PATCH 2/3] fix(microskills): reject wrongly-typed proposer name, id, detector and cite fields A malformed proposer reply with a list or dict name or id crashed the extractor and fact admission on an unhashable key, and non-string detector fields crashed the feature lookup. Drop such entries in parse_payload, and treat a non-list cite as no citations. --- tests/test_distiller_payload_types.py | 96 ++++++++++++++++++++++++ tokeneconomizer/microskills/distiller.py | 17 ++++- 2 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 tests/test_distiller_payload_types.py diff --git a/tests/test_distiller_payload_types.py b/tests/test_distiller_payload_types.py new file mode 100644 index 00000000..b365b43b --- /dev/null +++ b/tests/test_distiller_payload_types.py @@ -0,0 +1,96 @@ +"""Malformed proposer payloads: wrongly-typed fields are dropped, never crash the distiller.""" +import json + +import pytest + +import tokeneconomizer.microskills.distiller as dist +from tokeneconomizer.microskills import Distiller +from tokeneconomizer.microskills.distiller import parse_payload +from fixtures import write_claude_transcript + + +def _runner(payload): + return lambda prompt, model="agent": (payload, 1.0) + + +def _payload(**body): + return json.dumps(body) + + +MALFORMED = [ + '{"extractors": null, "facts": null}', + _payload(extractors=[{"name": ["a"], "pattern": "x"}], facts=[]), + _payload(extractors=[{"name": {"x": 1}, "pattern": "x"}], facts=[]), + _payload(extractors=[], facts=[{"id": ["a"], "detector": {}}]), + _payload(extractors=[], facts=[{"id": {"x": 1}, "detector": {}}]), + _payload(extractors=[], facts=[{"id": "f", "detector": {"scope": ["bad"]}}]), + _payload(extractors=[], facts=[{"id": "f", "detector": {"scope": {"a": 1}}}]), + _payload(extractors=[], facts=[{"id": "f", "detector": {"scope": "ran_tests", "fired": 5}}]), + _payload(extractors=[], facts=[{"id": "f", "detector": {"scope": "ran_tests", "confirm": ["x"]}}]), + _payload(extractors=[], facts=[{"id": "f", "detector": {"scope": "ran_tests", "contradict": {"x": 1}}}]), + _payload(extractors=[], facts=[{"id": "f", "detector": {"scope": "ran_tests"}, "cite": 3}]), +] + + +@pytest.mark.parametrize("raw", MALFORMED) +def test_parse_payload_drops_wrongly_typed_fields(raw): + exts, facts = parse_payload(raw) + assert exts == [] + assert facts == [] + + +def test_parse_payload_keeps_valid_entries_next_to_malformed(): + raw = _payload( + extractors=[{"name": ["a"], "pattern": "x"}, {"name": "ok", "pattern": "x"}], + facts=[{"id": {"x": 1}, "detector": {}}, + {"id": "good", "detector": {"scope": "ran_tests", "confirm": "passed"}}]) + exts, facts = parse_payload(raw) + assert [e["name"] for e in exts] == ["ok"] + assert [f["id"] for f in facts] == ["good"] + + +def test_ground_treats_non_list_cite_as_empty(tmp_path): + d = Distiller("acme/widget") + tr = str(tmp_path / "t.jsonl") + write_claude_transcript(tr, [("bash", {"command": "pytest"}, "1 passed")]) + d.update(tr, iid="w-0", resolved=True) + for cite in (3, "w-0", {}, {"w-0": 1}, [["w-0"], {"x": 1}, 7]): + spec = {"id": "f", "detector": {"scope": "ran_tests", "confirm": "passed"}, "cite": cite} + assert d._ground(spec) == 0 + + +@pytest.mark.parametrize("raw", MALFORMED) +def test_update_survives_malformed_proposer_reply(tmp_path, raw): + d = Distiller("acme/widget", runner=_runner(raw)) + d.last_propose = -1 + saved, dist.PROPOSE_EVERY = dist.PROPOSE_EVERY, 1 + try: + for i in range(3): + tr = str(tmp_path / f"t{i}.jsonl") + write_claude_transcript(tr, [("bash", {"command": "pytest"}, "1 passed")]) + d.update(tr, iid=f"w-{i}", resolved=True) + finally: + dist.PROPOSE_EVERY = saved + assert d.facts == {} + assert d.extractors == {} + + +def test_valid_fact_admitted_alongside_malformed_ones(tmp_path): + raw = _payload( + extractors=[{"name": ["bad"], "pattern": "x"}], + facts=[{"id": ["bad"], "detector": {}}, + {"id": "use-pytest", "claim": "pytest is canonical", "injection": "run pytest", + "detector": {"scope": "ran_tests", "confirm": "passed", "fired": "ran_tests"}, + "cite": ["w-0", "w-1"]}]) + d = Distiller("acme/widget", runner=_runner(raw)) + d.last_propose = -1 + saved, dist.PROPOSE_EVERY = dist.PROPOSE_EVERY, 1 + try: + for i in range(2): + tr = str(tmp_path / f"t{i}.jsonl") + write_claude_transcript(tr, [("bash", {"command": "pytest"}, "1 passed")]) + d.update(tr, iid=f"w-{i}", resolved=True) + finally: + dist.PROPOSE_EVERY = saved + assert "use-pytest" in d.facts + assert set(d.facts) == {"use-pytest"} diff --git a/tokeneconomizer/microskills/distiller.py b/tokeneconomizer/microskills/distiller.py index 953b189c..a3fe37b6 100644 --- a/tokeneconomizer/microskills/distiller.py +++ b/tokeneconomizer/microskills/distiller.py @@ -527,12 +527,20 @@ def parse_payload(raw): except Exception: return [], [] exts = [e for e in (obj.get("extractors") or []) - if isinstance(e, dict) and e.get("name") and isinstance(e.get("pattern"), str)] + if isinstance(e, dict) and isinstance(e.get("name"), str) and e.get("name") + and isinstance(e.get("pattern"), str)] facts = [f for f in (obj.get("facts") or []) - if isinstance(f, dict) and f.get("id") and isinstance(f.get("detector"), dict)] + if isinstance(f, dict) and isinstance(f.get("id"), str) and f.get("id") + and isinstance(f.get("detector"), dict) and _detector_fields_ok(f["detector"]) + and (f.get("cite") is None or isinstance(f.get("cite"), list))] return exts, facts +def _detector_fields_ok(det): + return all(det.get(k) is None or isinstance(det.get(k), str) + for k in ("scope", "fired", "confirm", "contradict")) + + def _reparse_cmds(dg): """Recover [(cmd,out)] from a digest's stashed blobs (for retro-featurize).""" cmds = dg.get("_cmd_blob", "").split("\n") @@ -654,7 +662,10 @@ def _ground(self, spec): """#window cites whose confirm regex fires under the CURRENT features.""" by_iid = {d["iid"]: d for d in self.window} hits = 0 - for iid in (spec.get("cite") or []): + cites = spec.get("cite") + for iid in (cites if isinstance(cites, list) else []): + if not isinstance(iid, str): + continue dg = by_iid.get(iid) if dg is None: continue From 1c4e0e215bcda07dd746c0b3152b342cd222518b Mon Sep 17 00:00:00 2001 From: Aaron Date: Sun, 15 Nov 2026 18:02:38 -0800 Subject: [PATCH 3/3] distiller: take extractors and facts only as lists, keep empty ones out `obj.get(...) or []` still iterated a truthy value that is not a list, so {"extractors": true} or {"facts": 7} raised TypeError out of parse_payload and aborted the fold (#173). The isinstance checks had also replaced the truthiness checks, so an empty pattern or detector got through, and an empty detector, which never grounds, took one of the MAX_CANDS slots. The id cases now carry a real detector, so they reproduce the unhashable-id crash in _admit on main instead of being dropped for their empty detector. A new test checks that empty detectors no longer crowd out a valid fact. Co-Authored-By: Claude Opus 5.5 --- tests/test_distiller_payload_types.py | 32 +++++++++++++++++++++--- tokeneconomizer/microskills/distiller.py | 10 +++++--- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/tests/test_distiller_payload_types.py b/tests/test_distiller_payload_types.py index b365b43b..f5ea9a5e 100644 --- a/tests/test_distiller_payload_types.py +++ b/tests/test_distiller_payload_types.py @@ -21,8 +21,11 @@ def _payload(**body): '{"extractors": null, "facts": null}', _payload(extractors=[{"name": ["a"], "pattern": "x"}], facts=[]), _payload(extractors=[{"name": {"x": 1}, "pattern": "x"}], facts=[]), - _payload(extractors=[], facts=[{"id": ["a"], "detector": {}}]), - _payload(extractors=[], facts=[{"id": {"x": 1}, "detector": {}}]), + '{"extractors": true, "facts": []}', + '{"extractors": [], "facts": 7}', + _payload(extractors=[{"name": "a", "pattern": ""}], facts=[{"id": "f", "detector": {}}]), + _payload(extractors=[], facts=[{"id": ["a"], "detector": {"scope": "ran_tests"}}]), + _payload(extractors=[], facts=[{"id": {"x": 1}, "detector": {"scope": "ran_tests"}}]), _payload(extractors=[], facts=[{"id": "f", "detector": {"scope": ["bad"]}}]), _payload(extractors=[], facts=[{"id": "f", "detector": {"scope": {"a": 1}}}]), _payload(extractors=[], facts=[{"id": "f", "detector": {"scope": "ran_tests", "fired": 5}}]), @@ -42,7 +45,7 @@ def test_parse_payload_drops_wrongly_typed_fields(raw): def test_parse_payload_keeps_valid_entries_next_to_malformed(): raw = _payload( extractors=[{"name": ["a"], "pattern": "x"}, {"name": "ok", "pattern": "x"}], - facts=[{"id": {"x": 1}, "detector": {}}, + facts=[{"id": {"x": 1}, "detector": {"scope": "ran_tests"}}, {"id": "good", "detector": {"scope": "ran_tests", "confirm": "passed"}}]) exts, facts = parse_payload(raw) assert [e["name"] for e in exts] == ["ok"] @@ -78,7 +81,7 @@ def test_update_survives_malformed_proposer_reply(tmp_path, raw): def test_valid_fact_admitted_alongside_malformed_ones(tmp_path): raw = _payload( extractors=[{"name": ["bad"], "pattern": "x"}], - facts=[{"id": ["bad"], "detector": {}}, + facts=[{"id": ["bad"], "detector": {"scope": "ran_tests"}}, {"id": "use-pytest", "claim": "pytest is canonical", "injection": "run pytest", "detector": {"scope": "ran_tests", "confirm": "passed", "fired": "ran_tests"}, "cite": ["w-0", "w-1"]}]) @@ -94,3 +97,24 @@ def test_valid_fact_admitted_alongside_malformed_ones(tmp_path): dist.PROPOSE_EVERY = saved assert "use-pytest" in d.facts assert set(d.facts) == {"use-pytest"} + + +def test_empty_detectors_do_not_crowd_out_a_valid_fact(tmp_path): + """An empty detector never grounds; it must not take one of the MAX_CANDS slots.""" + raw = _payload( + extractors=[], + facts=[*({"id": f"empty-{i}", "detector": {}} for i in range(4)), + {"id": "use-pytest", "claim": "pytest is canonical", "injection": "run pytest", + "detector": {"scope": "ran_tests", "confirm": "passed", "fired": "ran_tests"}, + "cite": ["w-0", "w-1"]}]) + d = Distiller("acme/widget", runner=_runner(raw)) + d.last_propose = -1 + saved, dist.PROPOSE_EVERY = dist.PROPOSE_EVERY, 1 + try: + for i in range(2): + tr = str(tmp_path / f"t{i}.jsonl") + write_claude_transcript(tr, [("bash", {"command": "pytest"}, "1 passed")]) + d.update(tr, iid=f"w-{i}", resolved=True) + finally: + dist.PROPOSE_EVERY = saved + assert set(d.facts) == {"use-pytest"} diff --git a/tokeneconomizer/microskills/distiller.py b/tokeneconomizer/microskills/distiller.py index a3fe37b6..8a696f72 100644 --- a/tokeneconomizer/microskills/distiller.py +++ b/tokeneconomizer/microskills/distiller.py @@ -526,12 +526,14 @@ def parse_payload(raw): obj = json.loads(m.group(0)) except Exception: return [], [] - exts = [e for e in (obj.get("extractors") or []) + exts, facts = obj.get("extractors"), obj.get("facts") + exts = [e for e in (exts if isinstance(exts, list) else []) if isinstance(e, dict) and isinstance(e.get("name"), str) and e.get("name") - and isinstance(e.get("pattern"), str)] - facts = [f for f in (obj.get("facts") or []) + and isinstance(e.get("pattern"), str) and e.get("pattern")] + facts = [f for f in (facts if isinstance(facts, list) else []) if isinstance(f, dict) and isinstance(f.get("id"), str) and f.get("id") - and isinstance(f.get("detector"), dict) and _detector_fields_ok(f["detector"]) + and isinstance(f.get("detector"), dict) and f.get("detector") + and _detector_fields_ok(f["detector"]) and (f.get("cite") is None or isinstance(f.get("cite"), list))] return exts, facts