diff --git a/changelog.d/1537-importance-range-every-type.changed.md b/changelog.d/1537-importance-range-every-type.changed.md new file mode 100644 index 000000000..d5cc6094c --- /dev/null +++ b/changelog.d/1537-importance-range-every-type.changed.md @@ -0,0 +1,4 @@ +- **A write no longer stores an `importance` outside 1 to 10, or one that is not a number, on any entry type.** Until now only `event` entries refused one; every other type stored it, and `pyrite qa validate` then warned about what Pyrite had just written. Now a create, an update that sets it, and `pyrite add` of a file that holds it are refused with `VALIDATION_FAILED` and an `errors` row naming `importance` (`rule: range`, `expected: 1-10`), and no file is written or changed, on the CLI (`-f importance=`) and on MCP `kb_create` / `kb_update`. An update that sets it and `pyrite add` check the number as given, so `10.000001`, `10.5`, `10.9`, `0.5`, `0.99` and `-0.5` are refused there too (they were accepted: a decimal was cut to a whole number before the comparison); a create still cuts a decimal first (see the limits below). What changes for a non-event entry: `0`, `11`, `-5`, `99` and 30-digit numbers are refused (they were stored); `update -f importance=high` or `=abc`, and an MCP `kb_update` with a non-number `importance` (including the string `"10.9"` or `"1e1"`), are refused (they wrote the text into the file as given); `true`, `false`, `.inf`, `-.inf`, `.nan`, a list, a mapping and a date count as non-numbers and are refused too on an update and by `pyrite add`, and `true` no longer passes as 1 there on an event; `pyrite add` of a file whose `importance` is `high`, `[5]`, `{a: 1}`, `0`, `true`, `.inf`, `10.9` or any other non-number or out-of-range value is refused (it was accepted). A numeric string (`"5"`, `" 5 "`, `"05"`) and a whole-valued float (`5.0`, `10.0`, `1e1`) are accepted, as before. A blank, `null` or `~` `importance:` in a file is absent, not a violation: `pyrite add` accepts it and an update of another field says nothing about it. A quoted empty string (`importance: ''` or `""`) is not blank: `pyrite add` refuses it (a note accepted it and stored `importance: ""`), and an update of another field on a file that holds it reports it as kept. An MCP `kb_update` with `"importance": null` is accepted on every type (an event used to refuse it) and leaves `importance: null` in the file, which reads as absent; on the command line `-f importance=` and `-f importance=null` are text, not null, and are refused. +- **A value already in a file is kept and reported.** An update of another field on a file whose `importance` is out of range or not a number succeeds and returns a "violation already on disk; kept" warning, and never touches that line. On an event, `.inf` and `-.inf` used to crash that update (`NO_OUTCOME` on the CLI, `INTERNAL` on MCP); they now answer as every other value does. One value still fails: with a 30-digit whole number already in the file, the update rewrites the file and then the index cannot store the number, so the command answers `NO_OUTCOME` (CLI) or `INTERNAL` (MCP) although the file has changed, and the index has no row for the entry. It does the same on 0.26.3 (#1584). +- **Limits, unchanged:** a create still turns its value into a whole number or the default before the check, without saying so. `create -f importance=10.7` exits 0 and stores `importance: 10`, and `3.9` stores `3` (`0.99` is refused only because it becomes 0); `create -f importance=high` (or `abc`, `.inf`, a blank) stores the default 5, and `create -f importance=true` stores 1, on a note and on an event. And `update -f importance=5.5` still writes `5.5` as given: a number inside 1 to 10 is accepted whether or not it is whole. +- In a `journalism-investigation` KB the refusal used to come from the plugin as `SCHEMA_VIOLATION` (`SchemaViolationError`, a row with no `rule`); it now comes from the model as `VALIDATION_FAILED` (`ValidationError`), the same answer as every other type (#1537). diff --git a/kb/backlog/done/a-write-refuses-importance-outside-1-10-on-every-type-1537.md b/kb/backlog/done/a-write-refuses-importance-outside-1-10-on-every-type-1537.md new file mode 100644 index 000000000..23fd83a94 --- /dev/null +++ b/kb/backlog/done/a-write-refuses-importance-outside-1-10-on-every-type-1537.md @@ -0,0 +1,16 @@ +--- +id: a-write-refuses-importance-outside-1-10-on-every-type-1537 +title: A write refuses importance outside 1-10 on every type (#1537) +type: backlog_item +tags: +- bug +importance: 5 +kind: bug +status: done +priority: medium +assignee: agent:cloud-worker +effort: S +rank: 0 +--- + +https://github.com/pyrite-wiki/pyrite/issues/1537: the range rule sits on EventEntry only; move it to Entry.validation_errors so the one write gate applies it to every type and surface; a value already on disk is reported, not refused. diff --git a/pyrite/models/base.py b/pyrite/models/base.py index 316f7da09..10bcdd9f4 100644 --- a/pyrite/models/base.py +++ b/pyrite/models/base.py @@ -15,7 +15,7 @@ from typing import Any, ClassVar from ..exceptions import EntryTypeError, json_safe_value -from ..schema import Link, Provenance, Source +from ..schema import Link, Provenance, Source, validate_importance from ..utils.frontmatter import require_frontmatter from ..utils.yaml import load_yaml @@ -760,6 +760,21 @@ def validation_errors(self) -> list[dict[str, Any]]: ) if problem := lifecycle_validation_error(self.lifecycle): errors.append(problem) + + # `importance` is a base field, 1-10 on every type (#1537). Check the + # file's value when loading fell back to a default (`importance: high`). + # A blank or null `importance:` is absent, not a violation: the file is + # a person's, and a blank key is common in a hand-made template. + importance = self.unrepresented_frontmatter().get("importance", self.importance) + if importance is not None and not validate_importance(importance): + errors.append( + { + "field": "importance", + "rule": "range", + "message": f"Importance must be 1-10, got: {importance}", + "expected": "1-10", + } + ) return errors def validate(self) -> list[str]: diff --git a/pyrite/models/core_types.py b/pyrite/models/core_types.py index f9c69d89f..5273a71e2 100644 --- a/pyrite/models/core_types.py +++ b/pyrite/models/core_types.py @@ -19,7 +19,6 @@ generate_entry_id, generate_event_id, validate_date, - validate_importance, ) from ..utils.frontmatter import load_frontmatter, yaml_start from ..utils.parse import safe_int @@ -198,9 +197,10 @@ def from_frontmatter(cls, meta: dict[str, Any], body: str) -> "EventEntry": def validation_errors(self) -> list[dict[str, Any]]: """Event rules as structured rows (#1284), each naming its field.""" errors = super().validation_errors() + date_rows: list[dict[str, Any]] = [] if not self.date: - errors.append( + date_rows.append( { "field": "date", "rule": "required", @@ -209,7 +209,7 @@ def validation_errors(self) -> list[dict[str, Any]]: } ) elif not validate_date(self.date): - errors.append( + date_rows.append( { "field": "date", "rule": "format", @@ -218,18 +218,10 @@ def validation_errors(self) -> list[dict[str, Any]]: } ) - # Validate the file's value when loading fell back to a default. - importance = self.unrepresented_frontmatter().get("importance", self.importance) - if not validate_importance(importance): - errors.append( - { - "field": "importance", - "rule": "range", - "message": f"Importance must be 1-10, got: {importance}", - "expected": "1-10", - } - ) - + # The date row sits before the base class's importance row (#1537), the + # order an event's refusal has always had. + at = next((i for i, e in enumerate(errors) if e.get("field") == "importance"), len(errors)) + errors[at:at] = date_rows return errors @classmethod diff --git a/pyrite/schema/validators.py b/pyrite/schema/validators.py index 51234d3a5..3843dd3f5 100644 --- a/pyrite/schema/validators.py +++ b/pyrite/schema/validators.py @@ -101,12 +101,31 @@ def _updated_at_instant(value: Any) -> datetime | None: def validate_importance(importance: Any) -> bool: - """Validate importance is 1-10.""" + """True when ``importance`` is a number in 1-10; never raises (#1537). + + The one place a value becomes "in range or not", and it is asked about + whatever a file, JSON or ``-f`` text puts there. A number is compared as + it is, not truncated first (``10.9`` is above 10 and ``0.99`` below 1; an + ``int()`` first would pass the one and refuse the other). A string is + numeric only as whole-number text (``"5"``, ``" 5 "``, ``"05"``). A value + nothing here can read (``.nan`` and ``high``, a list, a date) is out of + range, never an exception, and a bool is not a number: ``true`` would + otherwise pass as 1. A blank or null value is absent, which the caller + decides (``None`` is False here). ``5.5`` is accepted: the check is the + range, not whole-ness. + """ + if isinstance(importance, bool): + return False try: - val = int(importance) - return 1 <= val <= 10 - except (ValueError, TypeError): + if isinstance(importance, (int, float)): + value = importance + elif isinstance(importance, str): + value = int(importance) + else: + value = float(importance) + except (ValueError, TypeError, OverflowError): return False + return 1 <= value <= 10 def validate_event_id(event_id: str) -> bool: diff --git a/tests/test_importance_range_on_every_type.py b/tests/test_importance_range_on_every_type.py new file mode 100644 index 000000000..a494ead1c --- /dev/null +++ b/tests/test_importance_range_on_every_type.py @@ -0,0 +1,636 @@ +"""A write never stores an importance outside 1-10, on any type, by any surface (#1537). + +The property: ``importance`` is a base field, 1 to 10 on every entry. An update +that sets it outside that range, ``pyrite add`` of a file that holds it, and a +create of a whole number outside it are refused with ``VALIDATION_FAILED``, an +``errors`` row naming ``importance``, and no file written or changed -- on every +entry type and on every surface (the CLI's ``-f importance=`` and MCP +``kb_create`` / ``kb_update``). A create first turns a non-number into the +default (or ``true`` into 1) and cuts a decimal to a whole number in the model, +so the create rows assert "refused, or what is stored is in 1 to 10", not that +every bad value is refused. A value the file already holds is the file's: an +update of another field succeeds and reports it ("violation already on disk; +kept", design principle 2). + +Before #1537 only ``EventEntry`` carried the rule, so a ``person`` stored +``importance: 99`` and ``pyrite qa validate`` then reported what Pyrite had just +written. The rule now sits on ``Entry.validation_errors`` (one place, design +principle 8), and the one write gate (``KBService._validate_write``) applies it +to every type. + +The types are not listed by hand: the core registry (``ENTRY_TYPE_REGISTRY``), +every plugin-declared class (``get_registry().get_all_entry_types()``), and a +type a ``kb.yaml`` declares alone (``GenericEntry``). A type added to any of +them is covered by ``test_every_entry_class_carries_the_rule`` without a new +line here. + +Event rows. These are controls (``_EVENT_CONTROL``): they passed before #1537 +and must still pass -- the ``core-event`` rows of the create, update, on-disk +and add tests for 0, 11, -5, 99, the in-range values and an absent value, and +the ``EventEntry`` class rows (and its subclasses) for 0, 11, -5, 99. These are +not controls: the ``event`` cells of the value-kind block below (``KINDS``, run +for a note and an event) pin the answers on both types, and on an event some +changed with #1537 -- ``.inf`` / ``-.inf`` (they crashed an update and ``add``), +``true`` (it passed as 1), a number above 10 that is not whole such as 10.9 (an +update and ``add`` accepted it), blank and null (an update no longer warns about +them, and an MCP update that sets null was refused), and numeric text ``10.9`` +on a CLI update. +""" + +from __future__ import annotations + +import json +from pathlib import Path + +import pytest + +from pyrite.config import KBConfig +from pyrite.models.core_types import ENTRY_TYPE_REGISTRY + +KB = "imp" +YAML_KB = "imp-yaml" +OUT_OF_RANGE = [0, 11, -5, 99] +IN_RANGE = [1, 10, "7"] +KEPT_MESSAGE = "violation already on disk" +KB_YAML = """\ +name: imp-yaml +kb_type: generic +types: + widget: + description: A type only this kb.yaml declares + subdirectory: widgets + fields: + colour: + type: text +""" + +_EVENT_CONTROL = pytest.mark.control( + reason="EventEntry already carried the rule before #1537; this row must pass before and after" +) + + +_ACCEPTS = pytest.mark.control( + reason="over-refusal guard: in-range and absent values were accepted before #1537 and must stay accepted" +) + + +def _kb(etype: str) -> str: + """The KB a type is written to: the kb.yaml-only type lives in its own KB.""" + return YAML_KB if etype == "widget" else KB + + +def _plugin_types() -> dict[str, type]: + from pyrite.plugins import get_registry + + return get_registry().get_all_entry_types() + + +def _surface_types() -> list: + """Core types, one plugin type, one kb.yaml-only type; derived, not listed.""" + out: list = [] + for name in sorted(ENTRY_TYPE_REGISTRY): + marks = [_EVENT_CONTROL] if name == "event" else [] + out.append(pytest.param(name, id=f"core-{name}", marks=marks)) + plugin = sorted( + n for n, c in _plugin_types().items() if c.__module__.startswith("pyrite_zettelkasten") + ) + out.append(pytest.param(plugin[0], id=f"plugin-{plugin[0]}")) + out.append(pytest.param("widget", id="kbyaml-widget")) + return out + + +TYPES = _surface_types() + + +@pytest.fixture +def world(cli_world, tmp_path): + w = cli_world( + kbs=[ + KBConfig(name=KB, path=tmp_path / "imp-kb", kb_type="generic"), + KBConfig(name=YAML_KB, path=tmp_path / "imp-yaml-kb", kb_type="generic"), + ] + ) + (w.kb_path(YAML_KB) / "kb.yaml").write_text(KB_YAML) + return w + + +def _snapshot(world) -> dict[str, bytes]: + out = {} + for name in (KB, YAML_KB): + root = world.kb_path(name) + for p in sorted(root.rglob("*")): + if p.is_file(): + out[f"{name}/{p.relative_to(root)}"] = p.read_bytes() + return out + + +def _extra(etype: str) -> dict: + return {"date": "2026-01-02"} if etype == "event" else {} + + +def _file_text(etype: str, entry_id: str, importance=None, extra_lines: str = "") -> str: + lines = ["---", f"id: {entry_id}", f"type: {etype}", f"title: {entry_id}"] + if etype == "event": + lines.append("date: 2026-01-02") + if importance is not None: + lines.append(f"importance: {json.dumps(importance)}") + lines += [extra_lines] if extra_lines else [] + lines += ["---", "", "Body.", ""] + return "\n".join(lines) + + +# -- one result shape for both surfaces -------------------------------------- + + +def _json(output: str) -> dict: + start = output.index("{") + return json.loads(output[start:]) + + +def _cli_create(world, etype, importance): + args = ["create", "-k", _kb(etype), "-t", etype, "--title", "T1", "-b", "x"] + for k, v in _extra(etype).items(): + args += ["-f", f"{k}={v}"] + if importance is not None: + args += ["-f", f"importance={importance}"] + res = world.cli(*args, "--format", "json") + return res.exit_code, _json(res.output) + + +def _cli_update(world, kb, entry_id, **fields): + args = ["update", entry_id, "-k", kb] + for k, v in fields.items(): + args += ["-b", v] if k == "body" else ["-f", f"{k}={v}"] + res = world.cli(*args, "--format", "json") + return res.exit_code, _json(res.output) + + +def _mcp_create(world, etype, importance): + server = world.mcp() + payload = { + "kb_name": _kb(etype), + "entry_type": etype, + "title": "T1", + "body": "x", + **_extra(etype), + } + if importance is not None: + payload["importance"] = importance + res = server._dispatch_tool("kb_create", payload) + return (0 if res.get("created") else 1), res + + +def _mcp_update(world, kb, entry_id, **fields): + server = world.mcp() + res = server._dispatch_tool("kb_update", {"kb_name": kb, "entry_id": entry_id, **fields}) + return (0 if res.get("updated") else 1), res + + +SURFACES = { + "cli": (_cli_create, _cli_update), + "mcp": (_mcp_create, _mcp_update), +} +surface = pytest.mark.parametrize("surf", sorted(SURFACES)) +types = pytest.mark.parametrize("etype", TYPES) + + +def _assert_refused(code, res): + assert code != 0, res + assert res.get("error_code") == "VALIDATION_FAILED", res + rows = res.get("errors") or [] + assert any(r.get("field") == "importance" for r in rows), res + + +# -- create -------------------------------------------------------------------- + + +@types +@surface +@pytest.mark.parametrize("value", OUT_OF_RANGE) +def test_a_create_outside_1_to_10_is_refused_and_writes_nothing(world, etype, surf, value): + before = _snapshot(world) + code, res = SURFACES[surf][0](world, etype, value) + _assert_refused(code, res) + assert _snapshot(world) == before + + +@_ACCEPTS +@types +@surface +@pytest.mark.parametrize("value", [*IN_RANGE, None], ids=lambda v: f"value-{v}") +def test_a_create_in_range_or_absent_is_accepted(world, etype, surf, value): + code, res = SURFACES[surf][0](world, etype, value) + assert code == 0, res + assert res.get("created") is True, res + + +# -- update that sets it --------------------------------------------------------- + + +@types +@surface +@pytest.mark.parametrize("value", OUT_OF_RANGE) +def test_an_update_setting_it_outside_1_to_10_is_refused_and_changes_nothing( + world, etype, surf, value +): + world.add_entry(_kb(etype), _file_text(etype, "seed", importance=5)) + before = _snapshot(world) + code, res = SURFACES[surf][1](world, _kb(etype), "seed", importance=value) + _assert_refused(code, res) + assert _snapshot(world) == before + + +@_ACCEPTS +@types +@surface +@pytest.mark.parametrize("value", IN_RANGE) +def test_an_update_setting_it_in_range_is_accepted(world, etype, surf, value): + world.add_entry(_kb(etype), _file_text(etype, "seed", importance=5)) + code, res = SURFACES[surf][1](world, _kb(etype), "seed", importance=value) + assert code == 0, res + + +# -- a value already on disk is reported, not refused ------------------------------ + + +@types +@surface +@pytest.mark.parametrize("on_disk", [99, "high"], ids=["99", "high"]) +def test_an_update_of_another_field_keeps_and_reports_a_value_already_on_disk( + world, etype, surf, on_disk +): + path = world.add_entry(_kb(etype), _file_text(etype, "seed", importance=on_disk)) + code, res = SURFACES[surf][1](world, _kb(etype), "seed", body="changed body") + assert code == 0, res + warnings = res.get("warnings") or [] + kept = [w for w in warnings if KEPT_MESSAGE in json.dumps(w)] + assert kept, res + assert any("importance" in json.dumps(w) for w in kept), res + text = path.read_text() + assert f"importance: {json.dumps(on_disk)}" in text or f"importance: {on_disk}" in text + assert "changed body" in text + + +# -- every entry class carries the rule (a new type cannot go around it) ---------- + + +def _every_class() -> list: + from pyrite.models.generic import GenericEntry + + classes = {f"core-{n}": c for n, c in ENTRY_TYPE_REGISTRY.items()} + classes.update({f"plugin-{n}": c for n, c in _plugin_types().items()}) + classes["generic"] = GenericEntry + from pyrite.models.core_types import EventEntry + + return [ + pytest.param(c, id=i, marks=[_EVENT_CONTROL] if issubclass(c, EventEntry) else []) + for i, c in sorted(classes.items()) + ] + + +@pytest.mark.parametrize("cls", _every_class()) +@pytest.mark.parametrize("value", [0, 11, -5, 99]) +def test_every_entry_class_carries_the_rule(cls, value): + entry = cls(id="x", title="X", importance=value) + rows = [r for r in entry.validation_errors() if r.get("field") == "importance"] + assert rows, f"{cls.__name__} accepted importance={value}" + assert rows[0]["rule"] == "range" + assert rows[0]["message"] == f"Importance must be 1-10, got: {value}" + assert rows[0]["expected"] == "1-10" + + +@_ACCEPTS +@pytest.mark.parametrize("cls", _every_class()) +def test_every_entry_class_accepts_the_bounds_and_the_default(cls): + for value in (1, 10): + rows = [ + r + for r in cls(id="x", title="X", importance=value).validation_errors() + if r.get("field") == "importance" + ] + assert not rows + assert not [ + r for r in cls(id="x", title="X").validation_errors() if r.get("field") == "importance" + ] + + +# -- pyrite add of a hand-written file --------------------------------------------- + + +def test_add_of_a_file_holding_importance_99_is_refused_for_a_non_event(world, tmp_path): + src = tmp_path / "jane.md" + src.write_text(_file_text("person", "jane", importance=99)) + before = _snapshot(world) + res = world.cli("add", str(src), "-k", KB, "--format", "json") + assert res.exit_code != 0, res.output + body = _json(res.output) + assert body.get("error_code") == "VALIDATION_FAILED", body + assert any(r.get("field") == "importance" for r in body.get("errors", [])), body + assert _snapshot(world) == before + + +@_EVENT_CONTROL +def test_add_of_an_event_file_holding_importance_99_is_refused(world, tmp_path): + src = tmp_path / "e.md" + src.write_text(_file_text("event", "2026-01-02--e", importance=99)) + before = _snapshot(world) + res = world.cli("add", str(src), "-k", KB, "--format", "json") + assert res.exit_code != 0, res.output + assert _snapshot(world) == before + + +# -- a plugin-scoped KB answers as every other KB does --------------------------------- + + +@pytest.mark.parametrize("surf", sorted(SURFACES)) +def test_a_journalism_kb_refuses_importance_99_with_the_models_answer(cli_world, tmp_path, surf): + """The journalism plugin used to refuse this itself (`SCHEMA_VIOLATION`, a row + with no `rule`); the model answers first now: `VALIDATION_FAILED`, the same + row an event has.""" + world = cli_world( + kbs=[KBConfig(name="ji", path=tmp_path / "ji", kb_type="journalism-investigation")] + ) + before = {p: p.read_bytes() for p in world.kb_path("ji").rglob("*") if p.is_file()} + if surf == "cli": + res = world.cli( + "create", "-k", "ji", "-t", "person", "--title", "P", "-b", "x", + "-f", "importance=99", "--format", "json", + ) # fmt: skip + code, body = res.exit_code, _json(res.output) + else: + body = world.mcp()._dispatch_tool( + "kb_create", + {"kb_name": "ji", "entry_type": "person", "title": "P", "body": "x", "importance": 99}, + ) + code = 0 if body.get("created") else 1 + _assert_refused(code, body) + assert any(r.get("rule") == "range" for r in body["errors"]), body + assert {p: p.read_bytes() for p in world.kb_path("ji").rglob("*") if p.is_file()} == before + + +# -- every kind of value a file, JSON or `-f` text can put in `importance` --------------- +# +# The property, for any value: the check answers with a row or with nothing and +# never raises; a value this write did not set never stops the write; a write +# that is accepted never leaves an importance outside 1-10 in the file. One row +# per kind, each with the verdict the property gives it by hand (not read off the +# code), so a new kind of value cannot be added without saying what it must do. +# +# file: "good" (a number in 1-10, or numeric text), "absent" (blank or null: +# a guest in a person's file does not call a blank key a violation), +# "bad" (everything else: kept and reported when already on disk, +# refused by `pyrite add`) +# cli_set: the verdict of the `-f importance=` text as text ("null" and +# "" are strings on the command line) +# mcp_set: the verdict of the JSON value +# create_*: "refused" for a whole number out of range (and for a value that the +# model turns into 0), "safe" for anything else: a create turns a +# non-number into the default or into 1 (`true`), and cuts a decimal +# to a whole number (10.7 stores 10), in the model before the check (a +# known limit, not part of this fix), so the property is "refused, or +# what is stored is in 1-10" +_I = 123456789012345678901234567890 +KINDS = { + # kind: (yaml, cli text, json value, file, cli_set, mcp_set, create_cli, create_mcp) + "in_range_7": ("7", "7", 7, "good", "good", "good", "safe", "safe"), + "out_99": ("99", "99", 99, "bad", "bad", "bad", "refused", "refused"), + "zero": ("0", "0", 0, "bad", "bad", "bad", "refused", "refused"), + "big_30_digits": (str(_I), str(_I), _I, "bad", "bad", "bad", "refused", "refused"), + "float_5.0": ("5.0", "5.0", 5.0, "good", "good", "good", "safe", "safe"), + "float_10.0": ("10.0", "10.0", 10.0, "good", "good", "good", "safe", "safe"), + # 5.5 is "good" because the range is the rule and whole-ness is not checked: the + # stated limit of this change (a write of 5.5 is stored as given), not a verdict + # that 5.5 is a sensible importance. + "float_5.5": ("5.5", "5.5", 5.5, "good", "good", "good", "safe", "safe"), + # the edges of the range that are not whole: compared as numbers, not truncated first + "float_10.000001": ("10.000001", "10.000001", 10.000001, "bad", "bad", "bad", "safe", "safe"), + "float_10.5": ("10.5", "10.5", 10.5, "bad", "bad", "bad", "safe", "safe"), + "float_10.9": ("10.9", "10.9", 10.9, "bad", "bad", "bad", "safe", "safe"), + "float_0.5": ("0.5", "0.5", 0.5, "bad", "bad", "bad", "safe", "safe"), + "float_0.99": ("0.99", "0.99", 0.99, "bad", "bad", "bad", "safe", "safe"), + "float_-0.5": ("-0.5", "-0.5", -0.5, "bad", "bad", "bad", "safe", "safe"), + "float_1e1": ("1e1", "1e1", 1e1, "good", "good", "good", "safe", "safe"), + # a string is whole-number text only; the command line reads numeric text as a number + "str_10.9": ('"10.9"', "10.9", "10.9", "bad", "bad", "bad", "safe", "safe"), + "str_1e1": ('"1e1"', "1e1", "1e1", "bad", "good", "bad", "safe", "safe"), + "inf": (".inf", ".inf", float("inf"), "bad", "bad", "bad", "safe", "safe"), + "-inf": ("-.inf", "-.inf", float("-inf"), "bad", "bad", "bad", "safe", "safe"), + "nan": (".nan", ".nan", float("nan"), "bad", "bad", "bad", "safe", "safe"), + "true": ("true", "true", True, "bad", "bad", "bad", "safe", "safe"), + "false": ("false", "false", False, "bad", "bad", "bad", "refused", "refused"), + "null": ("null", "null", None, "absent", "bad", "absent", "safe", "safe"), + "blank": ("", "", None, "absent", "bad", "absent", "safe", "safe"), + "empty_string": ('""', "", "", "bad", "bad", "bad", "safe", "safe"), + "str_5": ('"5"', "5", "5", "good", "good", "good", "safe", "safe"), + "str_spaces_5": ('" 5 "', " 5 ", " 5 ", "good", "good", "good", "safe", "safe"), + "str_05": ('"05"', "05", "05", "good", "good", "good", "safe", "safe"), + "high": ("high", "high", "high", "bad", "bad", "bad", "safe", "safe"), + "abc": ('"abc"', "abc", "abc", "bad", "bad", "bad", "safe", "safe"), + "list": ("[5]", "[5]", [5], "bad", "bad", "bad", "safe", "safe"), + "mapping": ("{a: 1}", "{a: 1}", {"a": 1}, "bad", "bad", "bad", "safe", "safe"), + "date": ("2026-01-02", "2026-01-02", "2026-01-02", "bad", "bad", "bad", "safe", "safe"), +} +ETYPES = ["note", "event"] +kinds = pytest.mark.parametrize("kind", sorted(KINDS)) +etypes = pytest.mark.parametrize("etype", ETYPES) + + +# An update that leaves a 30-digit importance on disk writes the file, then the index +# upsert raises OverflowError (SQLite INTEGER), so the command answers NO_OUTCOME (CLI) or +# INTERNAL (MCP) and the index has no row for the entry. Same on dev; the storage defect is +# #1584 and not part of this change (the check answers with a row). The cell is expected +# to fail only that way: `_IndexOverflowError` is raised only for that answer, with the file +# rewritten, and `raises=` makes any other failure a real one. +class _IndexOverflowError(Exception): + pass + + +_INDEX_OVERFLOW = pytest.mark.xfail( + strict=True, + raises=_IndexOverflowError, + reason="#1584: the index upsert overflows SQLite INTEGER on a 30-digit value on disk", +) + + +def _raw_file(etype: str, entry_id: str, raw: str | None) -> str: + lines = ["---", f"id: {entry_id}", f"type: {etype}", f"title: {entry_id}"] + if etype == "event": + lines.append("date: 2026-01-02") + if raw is not None: + lines.append("importance:" + (f" {raw}" if raw else "")) + return "\n".join([*lines, "---", "", "Body.", ""]) + + +def _stored_is_in_range(path: Path) -> bool: + """What the file now holds is a number (or numeric text) in 1-10.""" + from pyrite.utils.yaml import load_yaml + + value = load_yaml(path.read_text().split("---", 2)[1]).get("importance") + if isinstance(value, bool) or value is None: + return False + try: + return 1 <= float(str(value).strip()) <= 10 + except ValueError: + return False + + +def _md_files(world) -> list[Path]: + return sorted(world.kb_path(KB).rglob("*.md")) + + +def _kept(res: dict) -> bool: + return any( + KEPT_MESSAGE in json.dumps(w) and "importance" in json.dumps(w) + for w in res.get("warnings") or [] + ) + + +def _surface_call(world, surf, op, etype, **kw): + """(exit, body) of one operation on one surface; never lets an exception escape.""" + if surf == "cli": + if op == "update": + args = ["update", "seed", "-k", KB] + args += ["-b", "changed"] if "body" in kw else ["-f", f"importance={kw['value']}"] + else: + args = ["create", "-k", KB, "-t", etype, "--title", "T1", "-b", "x"] + args += ["-f", "date=2026-01-02"] if etype == "event" else [] + args += ["-f", f"importance={kw['value']}"] + res = world.cli(*args, "--format", "json") + return res.exit_code, _json(res.output) + server = world.mcp() + if op == "update": + payload = {"kb_name": KB, "entry_id": "seed"} + payload.update({"body": "changed"} if "body" in kw else {"importance": kw["value"]}) + res = server._dispatch_tool("kb_update", payload) + else: + payload = {"kb_name": KB, "entry_type": etype, "title": "T1", "body": "x"} + payload.update({"date": "2026-01-02"} if etype == "event" else {}) + payload["importance"] = kw["value"] + res = server._dispatch_tool("kb_create", payload) + return (0 if res.get("created") or res.get("updated") else 1), res + + +def _no_crash(code, res): + assert res.get("error_code") not in ("NO_OUTCOME", "INTERNAL"), res + + +@etypes +@surface +@pytest.mark.parametrize( + "kind", + [ + pytest.param(k, marks=[_INDEX_OVERFLOW] if k == "big_30_digits" else []) + for k in sorted(KINDS) + ], +) +def test_a_value_already_on_disk_never_stops_an_update_of_another_field(world, etype, surf, kind): + yaml_text, _cli, _mcp, file, *_ = KINDS[kind] + path = world.add_entry(KB, _raw_file(etype, "seed", yaml_text)) + before_text = path.read_text() + code, res = _surface_call(world, surf, "update", etype, body="changed") + if kind == "big_30_digits" and res.get("error_code") in ("NO_OUTCOME", "INTERNAL"): + assert "changed" in path.read_text(), "the known failure comes after the file is written" + raise _IndexOverflowError(res) + _no_crash(code, res) + assert code == 0, res + assert (file == "bad") == _kept(res), res # kept and reported only when it is a violation + text = path.read_text() + assert "changed" in text + # the importance line itself is not touched by a write that did not set it + assert [ln for ln in text.splitlines() if ln.startswith("importance:")] == [ + ln for ln in before_text.splitlines() if ln.startswith("importance:") + ] + + +@etypes +@surface +@kinds +def test_a_set_importance_is_refused_or_in_1_to_10(world, etype, surf, kind): + _y, cli, mcp, _f, cli_set, mcp_set, *_ = KINDS[kind] + path = world.add_entry(KB, _raw_file(etype, "seed", "5")) + before = path.read_bytes() + verdict = cli_set if surf == "cli" else mcp_set + code, res = _surface_call(world, surf, "update", etype, value=cli if surf == "cli" else mcp) + _no_crash(code, res) + if verdict == "bad": + _assert_refused(code, res) + assert path.read_bytes() == before + else: + assert code == 0, res + if verdict == "good": + assert _stored_is_in_range(path) + + +@etypes +@surface +@kinds +def test_a_created_importance_is_refused_or_stored_in_1_to_10(world, etype, surf, kind): + _y, cli, mcp, _f, _cs, _ms, create_cli, create_mcp = KINDS[kind] + verdict = create_cli if surf == "cli" else create_mcp + before = _snapshot(world) + code, res = _surface_call(world, surf, "create", etype, value=cli if surf == "cli" else mcp) + _no_crash(code, res) + if verdict == "refused": + _assert_refused(code, res) + assert _snapshot(world) == before + elif code == 0: + (path,) = _md_files(world) + assert _stored_is_in_range(path), path.read_text() + + +@etypes +@kinds +def test_add_refuses_a_file_holding_a_bad_value_and_accepts_the_rest(world, tmp_path, etype, kind): + yaml_text, _cli, _mcp, file, *_ = KINDS[kind] + src = tmp_path / "src.md" + src.write_text(_raw_file(etype, "seed", yaml_text)) + before = _snapshot(world) + res = world.cli("add", str(src), "-k", KB, "--format", "json") + body = _json(res.output) + _no_crash(res.exit_code, body) + if file == "bad": + _assert_refused(res.exit_code, body) + assert _snapshot(world) == before + else: + assert res.exit_code == 0, body + + +@etypes +@kinds +def test_the_check_never_raises_and_says_row_or_nothing(etype, kind): + from pyrite.models.core_types import get_entry_class + from pyrite.schema import validate_importance + + yaml_text, _cli, mcp, file, *_ = KINDS[kind] + entry = get_entry_class(etype).from_markdown(_raw_file(etype, "seed", yaml_text)) + rows = [r for r in entry.validation_errors() if r.get("field") == "importance"] + assert bool(rows) == (file == "bad"), rows + assert validate_importance(mcp) is (file == "good") + + +# -- a write that sets null or blank: one answer on every type ----------------------------- +# +# Null is absent (no row), so over MCP an update with `"importance": null` is accepted +# on a note and on an event alike, and the file then holds no number (on 0.26.3 an event +# refused it). On the command line `-f importance=` and `-f importance=null` are text, +# not null: a string that is not a number, refused on every type. + + +@etypes +def test_an_mcp_update_setting_importance_null_is_accepted_on_every_type(world, etype): + from pyrite.utils.yaml import load_yaml + + path = world.add_entry(KB, _raw_file(etype, "seed", "5")) + code, res = _surface_call(world, "mcp", "update", etype, value=None) + assert code == 0, res + assert load_yaml(path.read_text().split("---", 2)[1]).get("importance") is None + + +@etypes +@pytest.mark.parametrize("text", ["", "null"], ids=["blank", "null-text"]) +def test_a_cli_update_setting_importance_to_blank_or_null_text_is_refused(world, etype, text): + path = world.add_entry(KB, _raw_file(etype, "seed", "5")) + before = path.read_bytes() + code, res = _surface_call(world, "cli", "update", etype, value=text) + _assert_refused(code, res) + assert path.read_bytes() == before diff --git a/tests/test_plugin_contract.py b/tests/test_plugin_contract.py index c9102859b..024d28c20 100644 --- a/tests/test_plugin_contract.py +++ b/tests/test_plugin_contract.py @@ -403,27 +403,45 @@ def get_hooks(self): class TestValidateWritePrefersMessage: """Coordinator should-fix 4: KBService._validate_write (~207) built its error string from field/rule/expected/got and ignored `message` when a - validator supplied one. A validator whose item has no `rule` (like - journalism-investigation's importance-range check, `{"field": "importance", - "message": "Importance must be 1-10, got: 99"}`) fell into the generic + validator supplied one. A validator whose item has no `rule` (a plugin + validator returning `{"field": "importance", "message": "..."}`, as + journalism-investigation's importance check did until #1537 moved that + range rule into the model) fell into the generic `f"{field}: {rule} (expected {expected}, got {got!r})"` branch, which with rule/expected/got all absent renders literally as "importance: (expected None, got None)" -- losing the validator's actual - message. `message`, when present, must be preferred.""" + message. `message`, when present, must be preferred. - def test_importance_error_message_is_not_lost(self, tmp_path): - if "journalism_investigation" not in _installed_plugin_names(): - pytest.skip("journalism-investigation extension not installed in this environment") + The test registers its own plugin whose validator returns exactly that + rule-less item: no installed plugin returns one any more, and the model + now answers first for importance.""" + def test_a_rule_less_validator_message_is_not_lost(self, tmp_path): from pyrite.config import KBConfig, PyriteConfig, Settings from pyrite.exceptions import SchemaViolationError + from pyrite.plugins import get_registry from pyrite.services.kb_service import KBService from pyrite.storage.database import PyriteDB - kb_path = tmp_path / "ji-kb" + class RuleLessPlugin: + name = "rule_less_test_plugin" + capabilities = {Capability.STORAGE} + + def get_kb_types(self): + return ["rule-less-test"] + + def get_validators(self): + return [ + lambda entry_type, fields, ctx: [ + {"field": "mood", "message": "Mood must be cheerful, got: grim"} + ] + ] + + reg = get_registry() + reg.register(RuleLessPlugin()) + kb_path = tmp_path / "rl-kb" kb_path.mkdir() - (kb_path / "entities").mkdir() - kb_config = KBConfig(name="test-ji", path=kb_path, kb_type="journalism-investigation") + kb_config = KBConfig(name="test-rl", path=kb_path, kb_type="rule-less-test") config = PyriteConfig( knowledge_bases=[kb_config], settings=Settings(index_path=tmp_path / "index.db") ) @@ -431,21 +449,16 @@ def test_importance_error_message_is_not_lost(self, tmp_path): svc = KBService(config, db) try: with pytest.raises(SchemaViolationError) as exc_info: - svc.create_entry( - "test-ji", - "bad-person", - "A Real Person", - "person", - allow_undeclared=True, - importance=99, - ) + svc.create_entry("test-rl", "bad-note", "A note", "note", allow_undeclared=True) message = str(exc_info.value) - assert "Importance must be 1-10, got: 99" in message, ( + assert "Mood must be cheerful, got: grim" in message, ( f"expected the validator's own message in the error, got: {message!r}" ) assert "expected None, got None" not in message finally: db.close() + reg._plugins.pop(RuleLessPlugin.name, None) + reg._conformance_cache.pop(RuleLessPlugin.name, None) class TestPluginKBTaskUpdateNoTracebackNoSilentSkip: @@ -512,12 +525,14 @@ def test_benign_task_update_no_traceback_log(self, tmp_path, caplog): db.close() def test_plugin_validator_error_is_non_zero_not_silent(self, tmp_path): - """A real plugin-specific validation error (importance out of the - 1-10 range -- a rule only the plugin validator enforces, isolating - it from create_entry's own generic title/date checks) raises - SchemaViolationError -- the exception the CLI's task commands catch - and turn into a non-zero exit -- rather than being silently - swallowed by a signature-mismatch fallback.""" + """A real plugin-specific validation error (a `payment` transaction + with no `amount` -- a rule only the journalism validator enforces; + the model has no opinion on it, so the error can only come from the + plugin) raises SchemaViolationError -- the exception the CLI's task + commands catch and turn into a non-zero exit -- rather than being + silently swallowed by a signature-mismatch fallback. (It was + `importance=99` until #1537 moved that range rule into the model, + which refuses before any plugin validator runs.)""" if "journalism_investigation" not in _installed_plugin_names(): pytest.skip("journalism-investigation extension not installed in this environment") @@ -528,15 +543,18 @@ def test_plugin_validator_error_is_non_zero_not_silent(self, tmp_path): with pytest.raises(SchemaViolationError) as exc_info: svc.kb_svc.create_entry( "test-ji", - "bad-person", - "A Real Person", - "person", + "bad-payment", + "A payment", + "transaction", allow_undeclared=True, - importance=99, + date="2026-01-02", + transaction_type="payment", + sender="a", + receiver="b", ) assert isinstance(exc_info.value, PyriteError) # what task_commands.py catches - assert any(e.get("field") == "importance" for e in exc_info.value.errors), ( - f"expected an importance-range error, got: {exc_info.value.errors}" + assert any(e.get("field") == "amount" for e in exc_info.value.errors), ( + f"expected an amount-required error, got: {exc_info.value.errors}" ) finally: db.close()