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
1 change: 1 addition & 0 deletions changelog.d/1639-file-pattern-private-fields.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- A `file_pattern` in `kb.yaml` that names a private entry field (such as `{_schema_version}`, `{_create_fields}`, `{_entry_type}`) is now refused with `VALIDATION_FAILED`; `_`-prefixed metadata keys on declared types continue to resolve and absent `_` placeholders fall back to the default filename (#1639).
13 changes: 12 additions & 1 deletion pyrite/schema/field_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,13 @@ def resolve_filename(self, entry: Entry) -> str | None:
import dataclasses
from string import Formatter

data_fields = (
dataclass_fields = (
{f.name for f in dataclasses.fields(entry)}
if dataclasses.is_dataclass(entry)
else set()
)
private_fields = {name for name in dataclass_fields if name.startswith("_")}
data_fields = dataclass_fields - private_fields
meta = getattr(entry, "metadata", None) or {}

def _is_missing(value: Any) -> bool:
Expand Down Expand Up @@ -334,6 +336,15 @@ def _is_missing(value: Any) -> bool:
"attribute/index access, no positional placeholders); "
"refusing to resolve it"
)
if field_name in private_fields:
# Private bookkeeping fields (_schema_version, _create_fields,
# _entry_type, etc.) are internal entry state, not data fields,
# and must not be resolved into file names (#1639).
raise ValidationError(
f"file_pattern {self.file_pattern!r} names {{{field_name}}}, "
f"which is a private field of entry {entry.id!r} (type "
f"{entry.entry_type!r}); refusing to resolve it"
)
if field_name in data_fields:
value = getattr(entry, field_name, None)
elif field_name in meta:
Expand Down
173 changes: 173 additions & 0 deletions tests/test_file_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,176 @@ def test_create_writes_the_dated_name(self, tmp_path):
p.name for p in kb_path.iterdir()
)
assert not (kb_path / "--test-d.md").exists(), "the empty-date name must not exist"

def test_create_refuses_private_field_placeholder(self, tmp_path):
"""#1639: an entry write under a pattern naming a private field raises
ValidationError (VALIDATION_FAILED)."""
from pyrite.config import KBConfig, KBType, PyriteConfig, Settings
from pyrite.exceptions import ValidationError
from pyrite.services.kb_service import KBService
from pyrite.storage.database import PyriteDB

kb_yaml = """\
name: tl
types:
timeline_event:
subdirectory: ""
file_pattern: "{_schema_version}.md"
"""
kb_path = tmp_path / "tl_priv"
kb_path.mkdir()
(kb_path / "kb.yaml").write_text(kb_yaml, encoding="utf-8")
db_path = tmp_path / "index_priv.db"
PyriteDB(db_path).close()

kb_config = KBConfig(name="tl", path=kb_path, kb_type=KBType.GENERIC)
config = PyriteConfig(
knowledge_bases=[kb_config],
settings=Settings(index_path=db_path, auto_embed=False),
)
db = PyriteDB(config.settings.index_path)
svc = KBService(config, db)
try:
with pytest.raises(ValidationError) as exc_info:
svc.create_entry("tl", "test-priv", "Test Priv", "timeline_event", "")
assert exc_info.value.error_code == "VALIDATION_FAILED"
assert "private field" in str(exc_info.value)
finally:
db.close()


def _collect_entry_private_field_pairs():
import dataclasses

from pyrite.models.base import Entry
import pyrite.models.core_types # noqa: F401
import pyrite.models.generic # noqa: F401
import pyrite.models.task # noqa: F401

try:
import pyrite_software_kb.entry_types # noqa: F401
except ImportError:
pass

def _subclasses(c):
subs = set(c.__subclasses__())
for s in list(subs):
subs.update(_subclasses(s))
return subs

all_classes = {Entry} | _subclasses(Entry)
pairs = []
for cls in sorted(all_classes, key=lambda c: (c.__module__, c.__qualname__)):
if not cls.__module__.startswith(("pyrite.", "pyrite_")):
continue
if not dataclasses.is_dataclass(cls):
continue
for f in dataclasses.fields(cls):
if f.name.startswith("_"):
pairs.append((cls, f.name))
return pairs


def _build_test_entry(cls):
import dataclasses
import inspect

target_cls = cls
if inspect.isabstract(cls):

class Concrete(cls):
@property
def entry_type(self):
return "entry"

@classmethod
def from_frontmatter(cls, *a, **k):
pass

def to_frontmatter(self):
return ""

Concrete.__name__ = f"Concrete{cls.__name__}"
target_cls = Concrete

kwargs = {}
for f in dataclasses.fields(cls):
if f.default is not dataclasses.MISSING or f.default_factory is not dataclasses.MISSING:
continue
if f.name == "id":
kwargs["id"] = "test-id"
elif f.name == "title":
kwargs["title"] = "Test Title"
elif f.name == "_entry_type":
kwargs["_entry_type"] = "note"
elif f.name == "entry_type":
kwargs["entry_type"] = "note"
else:
kwargs[f.name] = "dummy"
return target_cls(**kwargs)


_ENTRY_PRIVATE_FIELD_PAIRS = _collect_entry_private_field_pairs()
assert len(_ENTRY_PRIVATE_FIELD_PAIRS) > 0, "collected entry private field pairs must not be empty"


@pytest.mark.parametrize(
("cls", "field_name"),
_ENTRY_PRIVATE_FIELD_PAIRS,
ids=[f"{cls.__qualname__}-{field_name}" for cls, field_name in _ENTRY_PRIVATE_FIELD_PAIRS],
)
def test_private_field_placeholder_is_refused(cls, field_name):
"""#1639: placeholders naming private fields of the entry (e.g.
_schema_version, _create_fields, _entry_type) must be refused with
ValidationError rather than resolved into file names."""
from pyrite.exceptions import ValidationError

inst = _build_test_entry(cls)
schema = TypeSchema(name="t", file_pattern=f"{{{field_name}}}.md")
with pytest.raises(ValidationError, match="which is a private field of entry"):
schema.resolve_filename(inst)


@pytest.mark.control(reason="public data fields continue to resolve into filenames")
def test_public_field_placeholder_still_resolves():
from pyrite_software_kb.entry_types import ADREntry

entry = ADREntry(id="adr-0007", title="Use PostgreSQL", adr_number=7)
schema = TypeSchema(name="adr", file_pattern="adr-{adr_number}.md")
assert schema.resolve_filename(entry) == "adr-7.md"


@pytest.mark.control(
reason="kb.yaml-declared types resolve _-prefixed metadata keys like _draft from metadata"
)
def test_private_prefixed_metadata_key_on_generic_entry_still_resolves():
from pyrite.models.generic import GenericEntry

generic = GenericEntry(
id="draft-1",
title="Draft Note",
_entry_type="note",
metadata={"_draft": "true"},
)
schema = TypeSchema(name="note", file_pattern="{_draft}--{slug}.md")
assert schema.resolve_filename(generic) == "true--draft-1.md"


@pytest.mark.control(
reason="absent _-prefixed names fall back to default filename rather than refusing"
)
def test_absent_private_prefixed_placeholder_falls_back_to_none():
from pyrite_software_kb.entry_types import ADREntry

entry = ADREntry(id="adr-0007", title="Use PostgreSQL")
schema = TypeSchema(name="adr", file_pattern="{_absent_name}.md")
assert schema.resolve_filename(entry) is None


@pytest.mark.control(reason="{date}--{slug}.md pattern resolution is unchanged")
def test_date_slug_pattern_is_unchanged():
from pyrite.models.factory import build_entry

event = build_entry("event", title="ICE Raid Downtown", body="", date="2026-03-30")
schema = TypeSchema(name="event", file_pattern="{date}--{slug}.md")
assert schema.resolve_filename(event) == "2026-03-30--ice-raid-downtown.md"
Loading