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
5 changes: 4 additions & 1 deletion docs/knowledge-contribution-workflow.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ that regenerates every record; do not hand-edit the generated files.
the same fields and field order as before: the `sources` entry with the full commit
URL, and `source_files` including the registry hash. Search, `knowledge show`,
Collect and installed wheels keep reading schema 1. The generator refuses to write a
record that does not resolve back to the record it rendered.
record that does not resolve back to the record it rendered. A pack with
`_provenance.json` holds only schema-2 records: the catalog rejects any other JSON
file in it, including a nested `_provenance.json`. Packs without a snapshot keep
hand-written schema-1 records.

The one-time migration regenerated all 80 records at the previous pin
`d648f25732dee1c974da62a94c46a642ad52cef8`; each resolved record was byte-identical
Expand Down
6 changes: 6 additions & 0 deletions src/inferrsi/knowledge/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,18 @@ def knowledge_records() -> list[dict[str, Any]]:
continue
record = json.loads(item.read_text(encoding="utf-8"))
# Generated skill records store schema v2; readers always receive schema v1.
# A pack with a snapshot holds only generated records.
if isinstance(record, dict) and record.get("schema_version") == (
_source_snapshot.STORED_SCHEMA
):
if snapshot is None:
raise ValueError(f"Knowledge record lacks its source snapshot: {repo}")
record = _source_snapshot.resolve(record, snapshot)
elif snapshot is not None:
raise ValueError(
f"Source-snapshot pack holds a record that is not schema "
f"{_source_snapshot.STORED_SCHEMA}: {repo}/{'/'.join(parts)}"
)
_validate_record(record, repo, parts, tier)
if record["id"] in seen:
raise ValueError(f"Duplicate knowledge ID: {record['id']}")
Expand Down
24 changes: 24 additions & 0 deletions tests/test_source_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest

from inferrsi.knowledge import _source_snapshot as snapshot
from inferrsi.knowledge import catalog
from inferrsi.knowledge._source_snapshot import source_entry
from inferrsi.knowledge.catalog import search_knowledge
from tools import build_skill_index as index_builder
Expand Down Expand Up @@ -96,6 +97,29 @@ def test_invalid_stored_record_never_resolves(mutation):
snapshot.resolve(stored, manifest)


def test_snapshot_pack_reads_only_the_root_manifest_and_stored_records(tmp_path, monkeypatch):
pack = tmp_path / "knowledge/data/repo_packs/inferrsi/knowledge"
source = record_files()[0]
target = pack / source.relative_to(GENERATED)
target.parent.mkdir(parents=True)
target.write_bytes(source.read_bytes())
(pack / snapshot.MANIFEST).write_bytes((GENERATED / snapshot.MANIFEST).read_bytes())
monkeypatch.setattr(catalog, "files", lambda _: tmp_path)
assert len(catalog.knowledge_records()) == 1
# Only the root manifest is the snapshot; a nested copy is an invalid record.
nested = target.parent / snapshot.MANIFEST
nested.write_bytes((pack / snapshot.MANIFEST).read_bytes())
with pytest.raises(ValueError, match="not schema 2"):
catalog.knowledge_records()
nested.unlink()
# Schema-1 and schema-2 records never mix in one snapshot pack.
manifest = json.loads((pack / snapshot.MANIFEST).read_text(encoding="utf-8"))
public = snapshot.resolve(json.loads(target.read_text(encoding="utf-8")), manifest)
target.write_text(json.dumps(public), encoding="utf-8")
with pytest.raises(ValueError, match="not schema 2"):
catalog.knowledge_records()


# Fixture identity and settings; hosted runners have no global Git identity.
GIT = ["git", "-c", "user.name=Snapshot fixture", "-c", "user.email=fixture@example.invalid",
"-c", "commit.gpgsign=false", "-c", "core.autocrlf=false"]
Expand Down
Loading