From 9ce122749cb37fbe9b19fbe2cbc7ed13d6b3670d Mon Sep 17 00:00:00 2001 From: Dong1017 <55723162+Dong1017@users.noreply.github.com> Date: Mon, 16 Nov 2026 14:20:54 +0800 Subject: [PATCH] knowledge: reject foreign records in a source-snapshot pack A pack with _provenance.json holds only generated schema-2 records. The catalog now rejects any other JSON record there, including a schema-1 record mixed into the pack and a _provenance.json copied into a subdirectory, which was previously read as an incomplete record. Packs without a snapshot keep their hand-written schema-1 records. Follows the review questions on #359. Refs #355. Agent-assisted-by: Claude Code / claude-opus-5-5 [effort=xhigh] (author) Signed-off-by: Dong1017 <55723162+Dong1017@users.noreply.github.com> --- docs/knowledge-contribution-workflow.md | 5 ++++- src/inferrsi/knowledge/catalog.py | 6 ++++++ tests/test_source_snapshot.py | 24 ++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/docs/knowledge-contribution-workflow.md b/docs/knowledge-contribution-workflow.md index 5dd07ae4..3671d528 100644 --- a/docs/knowledge-contribution-workflow.md +++ b/docs/knowledge-contribution-workflow.md @@ -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 diff --git a/src/inferrsi/knowledge/catalog.py b/src/inferrsi/knowledge/catalog.py index fffd2d66..a8c1fc4a 100644 --- a/src/inferrsi/knowledge/catalog.py +++ b/src/inferrsi/knowledge/catalog.py @@ -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']}") diff --git a/tests/test_source_snapshot.py b/tests/test_source_snapshot.py index f7554654..619d810e 100644 --- a/tests/test_source_snapshot.py +++ b/tests/test_source_snapshot.py @@ -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 @@ -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"]