Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog.d/1537-importance-range-every-type.changed.md
Original file line number Diff line number Diff line change
@@ -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).
Original file line number Diff line number Diff line change
@@ -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.
17 changes: 16 additions & 1 deletion pyrite/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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]:
Expand Down
22 changes: 7 additions & 15 deletions pyrite/models/core_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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
Expand Down
27 changes: 23 additions & 4 deletions pyrite/schema/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading
Loading