From 3d2ad29e34261ea57f1bbeea0c47008d2ee9192a Mon Sep 17 00:00:00 2001 From: MrScripty Date: Thu, 12 Nov 2026 07:20:56 -0700 Subject: [PATCH 1/2] test(engine): align stale tests with interface 48 and checkout reconciliation Six engine tests have failed on main since 22 October, when two changes landed without the tests that depend on them being updated: - Interface 48 (ad564433). Four publication tests asserted the literal "interface 47;" in the server instructions, and two contract examples still carried interface_version 47. The assertion now reads the number from the interface the server under test serves, so it cannot go stale again, and the examples say 48. - Publication checkout reconciliation (8c5a4ad4). The create-and-retire apply test still asserted that apply leaves the working tree untouched. It now asserts the documented behaviour: HEAD, index and worktree present the published candidate and no other local difference exists. No production code changes. The suite-input manifest is regenerated; it was already stale on main because 5bce3110 added files without refreshing it. Co-Authored-By: Claude Fable 5.2 --- .../generated/suite-inputs.json | 6 +-- .../contracts/examples/a1-examples.json | 4 +- tools/standards_engine/tests/test_analysis.py | 42 ++++++++++++++++++- .../tests/test_consumer_publication.py | 13 +++++- 4 files changed, 57 insertions(+), 8 deletions(-) diff --git a/evaluation/standards-effectiveness/generated/suite-inputs.json b/evaluation/standards-effectiveness/generated/suite-inputs.json index d12e6834..58420f4d 100644 --- a/evaluation/standards-effectiveness/generated/suite-inputs.json +++ b/evaluation/standards-effectiveness/generated/suite-inputs.json @@ -7405,7 +7405,7 @@ ] }, { - "digest": "sha256:489fea805dbfd6f84bcb28374b467da192d56243100c0a58d568d9acf6180e08", + "digest": "sha256:146231132e8fbff19b5d917c1268bb75a0195cff4478d0e44df6bf04b8ba6aa2", "path": "tools/standards_engine/contracts/examples/a1-examples.json", "state": "present", "uses": [ @@ -7875,7 +7875,7 @@ ] }, { - "digest": "sha256:7f51e403f03f5c57be8b1ca5736a79bcf139c87dd9eb4703ce3f7e59f1ba3d01", + "digest": "sha256:30eec6a6c472938048821b5669d17e054aa7cdee637647961b4012a380245e62", "path": "tools/standards_engine/tests/test_analysis.py", "state": "present", "uses": [ @@ -14116,7 +14116,7 @@ "path": "evaluation/standards-effectiveness/suite-registry.toml" }, "repository_index": { - "digest": "sha256:25f5d13e9650e10c5bcc198f50ea81aa00f90799ca3f9ba2a5a433a658262229", + "digest": "sha256:96dd89639c5b1a45d6c6692e969cea93338efdd3e5604fb2c2d30e6cfe254fd7", "uses": [ { "check": "python-package-contract", diff --git a/tools/standards_engine/contracts/examples/a1-examples.json b/tools/standards_engine/contracts/examples/a1-examples.json index a697ac1f..1f256f60 100644 --- a/tools/standards_engine/contracts/examples/a1-examples.json +++ b/tools/standards_engine/contracts/examples/a1-examples.json @@ -1274,7 +1274,7 @@ "value": { "kind": "input-contract-result", "purpose": "application", - "interface_version": 47, + "interface_version": 48, "catalog_digest": "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "operation": "runtime_info", "root": "RuntimeInfoCall", @@ -1420,7 +1420,7 @@ "value": { "kind": "output-contract-result", "purpose": "authoring", - "interface_version": 47, + "interface_version": 48, "catalog_digest": "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "operation": "review", "dialect": "https://json-schema.org/draft/2020-12/schema", diff --git a/tools/standards_engine/tests/test_analysis.py b/tools/standards_engine/tests/test_analysis.py index 0eeaf032..0ae8a9e6 100644 --- a/tools/standards_engine/tests/test_analysis.py +++ b/tools/standards_engine/tests/test_analysis.py @@ -687,8 +687,39 @@ def apply_ready(readiness_id: str) -> ProposalApplication: } ) ) + # Apply advances this unchanged main checkout to its publication. + self.assertEqual(applied.checkout.status, "current") return engine._authoring.read_application(applied.application.id) + def assert_checkout_presents( + application: ProposalApplication, + present: tuple[str, ...], + absent: tuple[str, ...] = (), + ) -> None: + # Interface 47: HEAD, index and worktree all present the exact + # published candidate, and no other local difference appears. + self.assertEqual( + git_output(repository, ("rev-parse", "HEAD")).strip(), + application.candidate.oid.encode("ascii"), + ) + for path in present: + self.assertEqual( + (repository / path).read_bytes(), + engine._repository.read_file( + application.candidate, RepositoryPath.parse(path) + ), + path, + ) + for path in absent: + self.assertFalse((repository / path).exists(), path) + self.assertEqual( + git_output( + repository, + ("status", "--porcelain=v1", "--untracked-files=all"), + ), + b"", + ) + def assert_commit( application: ProposalApplication, revision: ProposalRevision, @@ -798,7 +829,10 @@ def assert_commit( impact_path: "100644", }, ) - self.assertFalse((repository / standard_path).exists()) + assert_checkout_presents( + create_application, + (standard_path, policy_path, impact_path), + ) facade.close() engine = StandardsEngine.open_repository( @@ -933,7 +967,11 @@ def assert_commit( ) self.assertIn(b"[[tombstone]]", retired_policy) self.assertNotIn(b"[[policy_unit]]", retired_policy) - self.assertFalse((repository / standard_path).exists()) + assert_checkout_presents( + retire_application, + (policy_path,), + absent=(standard_path, impact_path), + ) facade.close() engine = StandardsEngine.open_repository( diff --git a/tools/standards_engine/tests/test_consumer_publication.py b/tools/standards_engine/tests/test_consumer_publication.py index 43021335..b5d05cc0 100644 --- a/tools/standards_engine/tests/test_consumer_publication.py +++ b/tools/standards_engine/tests/test_consumer_publication.py @@ -11,13 +11,23 @@ import subprocess import sys import tempfile +import tomllib import unittest +from tools.standards_engine.standards_engine.tools import INTERFACE_CONTRACT from tools.standards_engine.tests.test_agent_workflow import ROOT, decisions, evidence, prepare_repository from tools.standards_engine.tests import test_proposal_consumers as fixtures from tools.standards_engine.tests.test_proposal_consumers import registration +def installed_interface_version(root): + # The server announces the interface declared by the repository it serves. + # Read that declaration instead of repeating a number that each interface + # edition would otherwise leave stale here. + with (root / INTERFACE_CONTRACT).open('rb') as source: + return tomllib.load(source)['interface_schema_version'] + + class ConsumerPublicationTest(unittest.TestCase): def setUp(self): self.temp = tempfile.TemporaryDirectory(prefix='consumer-publication-') @@ -43,7 +53,8 @@ def rpc(self, method, params, purpose='authoring'): self.assertEqual(process.returncode,0,process.stderr) self.assertEqual(process.stderr,'') responses={row['id']:row for row in map(json.loads,process.stdout.splitlines())} - self.assertIn('interface 47;',responses[1]['result']['instructions']) + self.assertIn(f'interface {installed_interface_version(self.root)};', + responses[1]['result']['instructions']) return responses[2] def call(self,name,arguments,purpose='authoring',error=False): From f2984a3665efa7128427f380ada550b436f3c470 Mon Sep 17 00:00:00 2001 From: MrScripty Date: Thu, 12 Nov 2026 07:21:22 -0700 Subject: [PATCH 2/2] perf(snapshots): audit the store once per process, not on every open The MCP server opens the snapshot store for each tool call, and every open of an existing store ran PRAGMA integrity_check and foreign_key_check over the whole file. On a 282 MB store that is about 0.6 s added to every route, read and related call, growing with the file. The full audit now runs at most once per process for each store file, identified by device and inode. It still runs on the first open in every process, throughout a version 1 migration, and for a file put in place under a different inode. Application id, schema version and exact schema checks still run on every open. Measured on a copy of that store, five consecutive opens in one process go from about 0.6 s each to 0.6 s for the first and under 1 ms for the rest. Known limits, documented in the snapshots README: a one-shot CLI call is a new process and still audits; a store deleted and recreated in place can reuse its inode on ext4 and is then not audited again by a server that is already running. Tests cover one audit for two opens, a replaced file, a corrupt replacement, the checks that stay on every open, a fresh process rejecting a corrupted file, migration keeping all of its audits, and the server auditing once across several tool calls. Co-Authored-By: Claude Fable 5.2 --- .../generated/suite-inputs.json | 6 +- tools/standards_engine/PURPOSE-SEPARATION.md | 16 +- tools/standards_engine/README.md | 4 + .../tests/test_storage_lifecycle_transport.py | 116 ++++++++--- tools/standards_snapshots/README.md | 33 +++- .../standards_snapshots/store.py | 54 ++++- .../tests/test_storage_lifecycle.py | 184 ++++++++++++++++-- 7 files changed, 354 insertions(+), 59 deletions(-) diff --git a/evaluation/standards-effectiveness/generated/suite-inputs.json b/evaluation/standards-effectiveness/generated/suite-inputs.json index 58420f4d..9dbde56a 100644 --- a/evaluation/standards-effectiveness/generated/suite-inputs.json +++ b/evaluation/standards-effectiveness/generated/suite-inputs.json @@ -7347,7 +7347,7 @@ ] }, { - "digest": "sha256:8d8859b36d951bbf7a747da81d597df19b07b6653f1e36acc48b4b131b90c0a8", + "digest": "sha256:c85b4e2d525e7d644261658b19e35b815e86f6386754d327f56de5bc8be52caa", "path": "tools/standards_engine/README.md", "state": "present", "uses": [ @@ -8344,7 +8344,7 @@ ] }, { - "digest": "sha256:828be47647e4332067893b79a508e85ed65ff1d524c41daf8f09fa41f6b7d2a2", + "digest": "sha256:e6f3adf5543fad7cbd24b31b68b21d08029e542e11d80d2899d30b2ced682452", "path": "tools/standards_snapshots/README.md", "state": "present", "uses": [ @@ -8441,7 +8441,7 @@ ] }, { - "digest": "sha256:bda178630b8226af0de5c42a7da6290ace2ed1745e3d2a99057deb0bae6ac617", + "digest": "sha256:6853039f09ef90b9d4ba9f54e4df7050b13b0a1941764af95250b44f7237ecae", "path": "tools/standards_snapshots/standards_snapshots/store.py", "state": "present", "uses": [ diff --git a/tools/standards_engine/PURPOSE-SEPARATION.md b/tools/standards_engine/PURPOSE-SEPARATION.md index b112edf0..02e66413 100644 --- a/tools/standards_engine/PURPOSE-SEPARATION.md +++ b/tools/standards_engine/PURPOSE-SEPARATION.md @@ -124,9 +124,14 @@ two-entry and 32 MiB accounted-retention limit shared by snapshot and draft projection entries. Snapshot keys contain the exact verified captured paths, bytes and source revision, plus the installed compiler function. Python dictionary equality distinguishes hash collisions. Each new operation -opens and validates its store, checks snapshot lifecycle and verifies complete -content identity before lookup. Missing, quarantined, purged, replaced or corrupt -storage therefore remains observable. Equal verified inputs may share pure +opens its store, checks its application, version and exact schema, checks snapshot +lifecycle and verifies complete content identity before lookup. Missing, +quarantined, purged or replaced storage, and content that no longer matches its +identity, therefore remain observable. The store's complete SQLite +integrity/foreign-key audit is not repeated by every operation: a process runs it +on its first open of a store file, as described under +[Snapshot admission](../standards_snapshots/README.md#admission-and-maintenance-work). +Equal verified inputs may share pure compilation even when obtained from distinct store roots; store-specific handles, access checks and result envelopes remain fresh. No store facts enter the cache. @@ -185,8 +190,9 @@ Review, candidate verification, application and recovery retain their existing fresh replay paths. New snapshot capture retains both independent compiler passes. Main advancement affects new captures, never the base of an existing draft. Old explicit revisions retain historical meaning and can have live stale status. -Corruption, quarantine, purge or missing/replaced stores reject before projection -reuse. Server restart reconstructs cold, and close releases both entry kinds. +Stored content that fails validation, quarantine, purge or missing/replaced stores +reject before projection reuse. Server restart reconstructs cold and repeats the +store's complete SQLite audit; close releases both entry kinds. ### Incremental successor construction diff --git a/tools/standards_engine/README.md b/tools/standards_engine/README.md index 3e0b3fd7..85f52a8d 100644 --- a/tools/standards_engine/README.md +++ b/tools/standards_engine/README.md @@ -336,6 +336,10 @@ The process-owned compilation cache also retains exact captured-content identity proofs. Identity and compilation for one capture share one LRU entry and the existing byte budget. The Snapshot codec still defines identity, and every read reloads actual bytes, verifies file integrity and checks the stored identity and current lifecycle. +These per-read checks are separate from the store's complete SQLite +integrity/foreign-key audit, which a process runs on its first open of a store file +and not on each store-backed call; see +[Snapshot admission](../standards_snapshots/README.md#admission-and-maintenance-work). Focused authoring operations reuse canonical revision decoding through `ProposalMaterials`; the single record is released on success or exceptional exit. diff --git a/tools/standards_engine/tests/test_storage_lifecycle_transport.py b/tools/standards_engine/tests/test_storage_lifecycle_transport.py index a9b5eb47..0ff707b7 100644 --- a/tools/standards_engine/tests/test_storage_lifecycle_transport.py +++ b/tools/standards_engine/tests/test_storage_lifecycle_transport.py @@ -1,5 +1,8 @@ """Cold MCP reads coexist with a reserved writer only when no purge is due. +One server process audits its store once, on its first call; a newly started +process audits again and still rejects a corrupt store. + The fixture owns its database, evidence and processes. Publication, authorization and interpreter timing are not simulated; no production store is accessed. """ @@ -7,19 +10,27 @@ import json import os +import shutil import sqlite3 import subprocess import sys import time import unittest from contextlib import closing +from pathlib import Path +from unittest import mock +from tools.standards_engine.standards_engine.mcp import MCPServer from tools.standards_engine.tests import test_agent_interaction_transport as transport +from tools.standards_engine.tests.test_mcp import initialize, request from tools.standards_engine.tests.test_route_content import known_assertions from tools.standards_snapshots.standards_snapshots import ( AggregateChild, AggregateRecord, AggregateRoot, ChildHandle, SnapshotError, SnapshotId, SnapshotModule, ) +from tools.standards_snapshots.standards_snapshots import store as snapshot_store + +AUDIT_STATEMENTS = ('PRAGMA integrity_check', 'PRAGMA foreign_key_check') class StorageLifecycleTransportTest(unittest.TestCase): @@ -39,6 +50,85 @@ def call(self, purpose, operation, arguments, *, rejected=False): self, purpose, operation, arguments, rejected=rejected, delivery='on-demand', ) + def cold_open_failure(self): + """Run one cold authoring read whose store open fails; return its private stderr.""" + # Open-time failure retains the existing MCP private-stderr + # boundary (no structured result), not an invented tool outcome. + messages = [ + {'jsonrpc': '2.0', 'id': 1, 'method': 'initialize', 'params': { + 'protocolVersion': '2025-11-25', 'capabilities': {}, + 'clientInfo': {'name': 'storage-open-fixture', 'version': '1'}}}, + {'jsonrpc': '2.0', 'method': 'notifications/initialized'}, + {'jsonrpc': '2.0', 'id': 2, 'method': 'tools/call', 'params': { + 'name': 'read', 'arguments': {'snapshot': self.snapshot, 'target': 'core'}}}, + ] + process = subprocess.run( + [sys.executable, '-P', '-m', 'tools.standards_engine.standards_engine.mcp', + '--repo-root', str(self.root), '--purpose', 'authoring', + '--output-schemas', 'on-demand'], + env={**os.environ, 'PYTHONPATH': str(transport.fixture.ROOT)}, + input=''.join(json.dumps(message) + '\n' for message in messages), + text=True, capture_output=True, timeout=90, + ) + self.assertEqual(process.returncode, 0, process.stderr) + failed = next(json.loads(line)['result'] for line in process.stdout.splitlines() + if json.loads(line).get('id') == 2) + self.assertTrue(failed['isError']) + self.assertNotIn('structuredContent', failed) + self.assertIn('Engine invocation failed', failed['content'][0]['text']) + return process.stderr + + def test_one_server_process_audits_its_store_once_across_calls(self): + # The fixture built this store inside the test process. Discard that + # record so the server below starts as a newly launched process does. + with snapshot_store._AUDITED_FILES_LOCK: + snapshot_store._AUDITED_FILES.clear() + opens = [] + connect = sqlite3.connect + + def traced_connect(database, *arguments, **options): + connection = connect(database, *arguments, **options) + if Path(database).resolve() == self.path.resolve(): + opens.append([]) + connection.set_trace_callback(opens[-1].append) + return connection + + server = MCPServer(self.root, purpose='application') + self.addCleanup(server.close) + initialize(server) + arguments = {'snapshot': self.snapshot, 'target': 'core'} + with mock.patch.object(snapshot_store.sqlite3, 'connect', traced_connect): + results = [ + server.dispatch(request('tools/call', {'name': 'read', 'arguments': arguments}, + identifier))['result'] + for identifier in (2, 3, 4) + ] + for result in results: + self.assertFalse(result['isError'], result) + self.assertEqual(result['structuredContent']['content'], + transport.fixture.PurposeTransportTest.expected) + # Every call still opens and closes its own store connection. + self.assertEqual(len(opens), 3) + self.assertEqual([[statement for statement in statements if statement in AUDIT_STATEMENTS] + for statements in opens], [list(AUDIT_STATEMENTS), [], []]) + for statements in opens: + self.assertIn('PRAGMA application_id', statements) + self.assertIn('PRAGMA user_version', statements) + self.assertTrue(any('FROM sqlite_schema' in statement for statement in statements)) + + def test_cold_server_process_rejects_a_corrupt_store_on_its_first_call(self): + retained = self.path.with_name('retained-valid-store.sqlite3') + shutil.copyfile(self.path, retained) + try: + with closing(sqlite3.connect(self.path, isolation_level=None)) as corruptor: + corruptor.execute( + "INSERT INTO content_files VALUES ('missing', 'a', X'', 0, 'digest')") + self.assertIn('SNAPSHOT_STORE.FOREIGN_KEY_FAILURE', self.cold_open_failure()) + finally: + os.replace(retained, self.path) + recovered = self.call('authoring', 'read', {'snapshot': self.snapshot, 'target': 'core'}) + self.assertEqual(recovered['content'], transport.fixture.PurposeTransportTest.expected) + def test_cold_agent_observations_match_while_another_process_holds_writer(self): cases = [ ('read', {'snapshot': self.snapshot, 'target': 'core'}), @@ -82,31 +172,7 @@ def test_cold_due_maintenance_keeps_busy_then_expires_all_dependents_atomically( with closing(sqlite3.connect(self.path, isolation_level=None)) as writer: writer.execute('BEGIN IMMEDIATE') try: - # Open-time failure retains the existing MCP private-stderr - # boundary (no structured result), not an invented tool outcome. - messages = [ - {'jsonrpc': '2.0', 'id': 1, 'method': 'initialize', 'params': { - 'protocolVersion': '2025-11-25', 'capabilities': {}, - 'clientInfo': {'name': 'storage-busy-fixture', 'version': '1'}}}, - {'jsonrpc': '2.0', 'method': 'notifications/initialized'}, - {'jsonrpc': '2.0', 'id': 2, 'method': 'tools/call', 'params': { - 'name': 'read', 'arguments': {'snapshot': self.snapshot, 'target': 'core'}}}, - ] - process = subprocess.run( - [sys.executable, '-P', '-m', 'tools.standards_engine.standards_engine.mcp', - '--repo-root', str(self.root), '--purpose', 'authoring', - '--output-schemas', 'on-demand'], - env={**os.environ, 'PYTHONPATH': str(transport.fixture.ROOT)}, - input=''.join(json.dumps(message) + '\n' for message in messages), - text=True, capture_output=True, timeout=90, - ) - self.assertEqual(process.returncode, 0, process.stderr) - self.assertIn('SNAPSHOT_STORE.BUSY', process.stderr) - failed = next(json.loads(line)['result'] for line in process.stdout.splitlines() - if json.loads(line).get('id') == 2) - self.assertTrue(failed['isError']) - self.assertNotIn('structuredContent', failed) - self.assertIn('Engine invocation failed', failed['content'][0]['text']) + self.assertIn('SNAPSHOT_STORE.BUSY', self.cold_open_failure()) self.assertIsNotNone(writer.execute( 'SELECT 1 FROM snapshot_roots WHERE snapshot_id = ?', (str(expired),) ).fetchone()) diff --git a/tools/standards_snapshots/README.md b/tools/standards_snapshots/README.md index a1809843..0d56ee42 100644 --- a/tools/standards_snapshots/README.md +++ b/tools/standards_snapshots/README.md @@ -53,18 +53,33 @@ No identity domain, store format or persisted representation changes. ## Admission and maintenance work -Each open of an existing current-schema database authenticates the application, -exact schema, and complete SQLite integrity/foreign-key state **before** persistent -configuration. Configuration is followed by the existing exact schema check. The -same full audit is not repeated for a store admitted at the current schema version. -This is a point-in-time audit on every open, not cached proof that the database can -never change. Selected content and aggregate reads retain their ordinary checks. +Each open of an existing current-schema database authenticates the application +and exact schema **before** persistent configuration. Configuration is followed by +the existing exact schema check. Those checks run on every open. -New initialization keeps its final integrity audit and owned-file cleanup on failure. +The complete SQLite integrity/foreign-key audit runs once per process for each +store file. A process's first open of a file audits it before persistent +configuration. Later opens of the same file by that process omit only this audit. +The process remembers a passed audit in memory, by the file's device and inode, and +only after that open succeeded; nothing is persisted. A new process audits again, +and so does a file moved into place under a different inode. To audit a store +again, open it from a new process. `maintain` purges expired roots; it does not run +this audit. + +The audit is a point-in-time observation, not proof that the database stays valid. +Damage that appears after a process's audit is not reported by that process's later +opens. A file deleted and then recreated outside this package can receive the +audited file's inode number, and that process does not audit it; replace a store by +renaming a complete file into place, or restart the process. Selected content and +aggregate reads retain their ordinary checks on every call. + +New initialization keeps its final integrity audit and owned-file cleanup on failure; +that audit is the creating process's audit of the new file. A store admitted as v1 keeps the pre-configuration audit, transaction-guarded -pre/post-migration audits, and final destination audit. The final audit also applies +pre/post-migration audits, and final destination audit, even when the process +audited the same file earlier. The final audit also applies when another opener finishes migration before this opener prepares the schema. -No integrity policy, SQLite profile, supported migration or stored format changes. +No SQLite profile, supported migration or stored format changes. Maintenance remains synchronous at the same module call sites. It first performs a short-lived existence read for quarantined roots whose deadline is at or before the diff --git a/tools/standards_snapshots/standards_snapshots/store.py b/tools/standards_snapshots/standards_snapshots/store.py index 9b42f2ac..8c1ec836 100644 --- a/tools/standards_snapshots/standards_snapshots/store.py +++ b/tools/standards_snapshots/standards_snapshots/store.py @@ -5,6 +5,7 @@ import os import sqlite3 import stat +import threading from collections.abc import Iterator from contextlib import closing, contextmanager from functools import lru_cache @@ -35,6 +36,25 @@ USER_VERSION = 2 BUSY_TIMEOUT_MS = 5_000 +# Device and inode of every store file whose complete SQLite integrity and +# foreign-key audit has passed in this process. A later open of the same file +# keeps every other admission check and omits only that audit pair. The record +# is never persisted: each new process audits again, and a file put in place +# under a different inode has no record. It is a point-in-time observation of +# one file, not proof that another process has left the file unchanged. +_AUDITED_FILES: set[tuple[int, int]] = set() +_AUDITED_FILES_LOCK = threading.Lock() + + +def _audit_recorded(identity: tuple[int, int]) -> bool: + with _AUDITED_FILES_LOCK: + return identity in _AUDITED_FILES + + +def _record_audit(identity: tuple[int, int]) -> None: + with _AUDITED_FILES_LOCK: + _AUDITED_FILES.add(identity) + def _root_reference(value: object, description: str) -> None: if type(value) is not str or not value: @@ -274,6 +294,7 @@ def __init__(self, path: Path) -> None: "SNAPSHOT_STORE.UNSUPPORTED_PATH", "SQLite path must be one regular non-symlink file", ) + identity = (observed.st_dev, observed.st_ino) else: path.parent.mkdir(mode=0o700, parents=True, exist_ok=True) flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL @@ -281,7 +302,7 @@ def __init__(self, path: Path) -> None: try: descriptor = os.open(path, flags, 0o600) observed = os.fstat(descriptor) - created_identity = (observed.st_dev, observed.st_ino) + identity = created_identity = (observed.st_dev, observed.st_ino) os.close(descriptor) except OSError as error: raise unavailable("SNAPSHOT_STORE.UNAVAILABLE", str(error)) from error @@ -292,11 +313,18 @@ def __init__(self, path: Path) -> None: ) self._connection = connection self._connection.enable_load_extension(False) + # SQLite has opened whichever file the path named at that moment. + # An audit is reused, and afterwards recorded, only for a path that + # still names the file observed before the connection was made. + same_file = self._path_identity() == identity if existed: - admitted_version = self._verify_existing_authority() + admitted_version = self._verify_existing_authority( + audited=same_file and _audit_recorded(identity) + ) self._configure() self._prepare_existing_schema() - # Current stores were fully audited before configuration. A v1 + # Current stores were fully audited before configuration, in + # this open or an earlier one in this process. A v1 # admission still needs the final destination audit, including # when another opener completed migration in the meantime. if admitted_version != USER_VERSION: @@ -305,6 +333,8 @@ def __init__(self, path: Path) -> None: self._configure() self._initialize_schema() self._verify_integrity() + if same_file: + _record_audit(identity) except Exception as error: self._cleanup_failed_open(connection, created_identity) if isinstance(error, SnapshotError): @@ -318,6 +348,13 @@ def __init__(self, path: Path) -> None: def close(self) -> None: self._connection.close() + def _path_identity(self) -> tuple[int, int] | None: + try: + observed = self.path.lstat() + except OSError: + return None + return (observed.st_dev, observed.st_ino) + def _cleanup_failed_open( self, connection: sqlite3.Connection | None, @@ -1047,8 +1084,12 @@ def _prepare_existing_schema(self) -> None: self._migrate_version_one() self._verify_schema() - def _verify_existing_authority(self) -> int: - """Authenticate before configuration and return the admitted schema version.""" + def _verify_existing_authority(self, *, audited: bool) -> int: + """Authenticate before configuration and return the admitted schema version. + + ``audited`` reports that this process already completed the full audit + of this exact current-schema file. It never shortens a v1 admission. + """ application_id = self._connection.execute("PRAGMA application_id").fetchone()[0] user_version = self._connection.execute("PRAGMA user_version").fetchone()[0] if application_id != APPLICATION_ID: @@ -1071,7 +1112,8 @@ def _verify_existing_authority(self) -> int: f"expected schema version 1 or {USER_VERSION}, observed {user_version}", ) self._verify_schema() - self._verify_integrity() + if not audited: + self._verify_integrity() return user_version def _migrate_version_one(self) -> None: diff --git a/tools/standards_snapshots/tests/test_storage_lifecycle.py b/tools/standards_snapshots/tests/test_storage_lifecycle.py index 66afbafd..ce7edb5e 100644 --- a/tools/standards_snapshots/tests/test_storage_lifecycle.py +++ b/tools/standards_snapshots/tests/test_storage_lifecycle.py @@ -2,26 +2,67 @@ No-work observations must not request a writer. A positive probe is only a hint: transactions still select the current due set and own all deletion effects. + +One process runs the complete integrity/foreign-key audit of a store file once. +Later opens of that file keep the application, version and exact schema checks. """ from __future__ import annotations +import os +import shutil import sqlite3 +import subprocess +import sys import tempfile import unittest from contextlib import closing from pathlib import Path from typing import Callable +from unittest import mock from tools.standards_snapshots.standards_snapshots import ( AggregateChild, AggregateRecord, AggregateRoot, ChildHandle, FindAggregateRootsRequest, FindSnapshotsRequest, SnapshotError, SnapshotModule, ) +from tools.standards_snapshots.standards_snapshots import store as store_module from tools.standards_snapshots.standards_snapshots.store import SQLiteSnapshotStore from tools.standards_snapshots.tests.test_module import capture from tools.standards_snapshots.tests.test_store import ( _populated_version_one_store, _version_one_rows, ) +ROOT = Path(__file__).resolve().parents[3] + +# A separate interpreter has no audit record. It reports the admission outcome +# of the ordinary module open path for one existing store file. +_COLD_OPEN = """ +import sys +from pathlib import Path +from tools.standards_snapshots.standards_snapshots import SnapshotError, SnapshotModule +try: + SnapshotModule.open(Path(sys.argv[1])).close() +except SnapshotError as error: + print(error.failure.code) +else: + print('admitted') +""" + + +def _begin_as_new_process() -> None: + """Discard this process's audit record, as a newly started process has none.""" + with store_module._AUDITED_FILES_LOCK: + store_module._AUDITED_FILES.clear() + + +def _file_identity(path: Path) -> tuple[int, int]: + observed = path.lstat() + return observed.st_dev, observed.st_ino + + +def _violate_foreign_key(path: Path) -> None: + with closing(sqlite3.connect(path, isolation_level=None)) as corruptor: + corruptor.execute("INSERT INTO content_files VALUES ('missing', 'a', X'', 0, 'digest')") + class _ObservedAdmissionStore(SQLiteSnapshotStore): """Record proof phases while running the unchanged real proof implementations.""" @@ -74,6 +115,25 @@ def setUp(self): self.addCleanup(directory.cleanup) self.path = Path(directory.name) / 'snapshots.sqlite3' self.now = 2_000_000_000 + # Audit expectations must not depend on stores opened by earlier tests, + # whose removed files can lend their inode numbers to these fixtures. + _begin_as_new_process() + + def events(self, path=None): + opened = _ObservedAdmissionStore(path or self.path) + try: + return opened.admission_events + finally: + opened.close() + + def cold_open(self, path=None): + completed = subprocess.run( + [sys.executable, '-P', '-c', _COLD_OPEN, str(path or self.path)], + env={**os.environ, 'PYTHONPATH': str(ROOT)}, + text=True, capture_output=True, timeout=60, + ) + self.assertEqual(completed.returncode, 0, completed.stderr) + return completed.stdout.strip() def module(self): module = SnapshotModule.open(self.path, now=lambda: self.now, quarantine_seconds=10) @@ -311,22 +371,110 @@ def test_probe_sql_error_retains_typed_failure_without_transaction(self): self.assertNotIn('BEGIN IMMEDIATE', statements) self.assertFalse(module._store._connection.in_transaction) - def test_new_and_current_admission_have_one_full_integrity_audit(self): - created = _ObservedAdmissionStore(self.path) - try: - self.assertEqual(created.admission_events, [('configure', 0, False), ('integrity', 2, False)]) - finally: - created.close() + def test_new_and_current_admission_have_one_full_integrity_audit_per_process(self): + # The creating process audits its new file, and does not audit it again. + self.assertEqual(self.events(), [('configure', 0, False), ('integrity', 2, False)]) + for _ in range(2): + self.assertEqual(self.events(), [('configure', 2, False)]) + # A new process audits the existing file once, before configuration. + _begin_as_new_process() + self.assertEqual(self.events(), [('integrity', 2, False), ('configure', 2, False)]) for _ in range(2): - opened = _ObservedAdmissionStore(self.path) - try: - self.assertEqual(opened.admission_events, [('integrity', 2, False), ('configure', 2, False)]) - finally: - opened.close() + self.assertEqual(self.events(), [('configure', 2, False)]) + + def test_two_module_opens_in_one_process_audit_the_file_once(self): + with SnapshotModule.open(self.path, now=lambda: self.now) as initial: + snapshot = initial.create_snapshot(capture()).snapshot + _begin_as_new_process() + opens = [] + connect = sqlite3.connect + + def traced_connect(database, *arguments, **options): + # Observe each store connection from before its first statement. + connection = connect(database, *arguments, **options) + if Path(database).resolve() == self.path.resolve(): + opens.append([]) + connection.set_trace_callback(opens[-1].append) + return connection + + with mock.patch.object(store_module.sqlite3, 'connect', traced_connect): + for _ in range(2): + with SnapshotModule.open(self.path, now=lambda: self.now) as reopened: + self.assertEqual(reopened.load_content(snapshot), capture()) + first, second = opens + audit = ['PRAGMA integrity_check', 'PRAGMA foreign_key_check'] + self.assertEqual([statement for statement in first if statement in audit], audit) + # The later open omits exactly that pair and repeats every other statement. + self.assertEqual([statement for statement in first if statement not in audit], second) + self.assertIn('PRAGMA application_id', second) + self.assertIn('PRAGMA user_version', second) + self.assertTrue(any('FROM sqlite_schema' in statement for statement in second), second) + + def test_replaced_file_is_audited_again(self): + self.assertEqual(self.events(), [('configure', 0, False), ('integrity', 2, False)]) + self.assertEqual(self.events(), [('configure', 2, False)]) + audited = _file_identity(self.path) + # The replacement exists beside the audited file, so it has another inode. + replacement = self.path.with_name('replacement.sqlite3') + shutil.copyfile(self.path, replacement) + self.assertNotEqual(_file_identity(replacement), audited) + os.replace(replacement, self.path) + self.assertEqual(self.events(), [('integrity', 2, False), ('configure', 2, False)]) + self.assertEqual(self.events(), [('configure', 2, False)]) + + def test_corrupt_replacement_of_an_audited_file_is_rejected(self): + SQLiteSnapshotStore(self.path).close() + SQLiteSnapshotStore(self.path).close() + replacement = self.path.with_name('replacement.sqlite3') + shutil.copyfile(self.path, replacement) + _violate_foreign_key(replacement) + os.replace(replacement, self.path) + self.assert_code('SNAPSHOT_STORE.FOREIGN_KEY_FAILURE', lambda: SQLiteSnapshotStore(self.path)) + # A rejected file gains no audit record: the next open audits it again. + self.assert_code('SNAPSHOT_STORE.FOREIGN_KEY_FAILURE', lambda: SQLiteSnapshotStore(self.path)) + self.assertTrue(self.path.is_file()) + + def test_later_opens_keep_application_version_and_schema_checks(self): + for change, code in ( + ('PRAGMA application_id=1', 'SNAPSHOT_STORE.INVALID_APPLICATION'), + ('PRAGMA user_version=3', 'SNAPSHOT_STORE.UNSUPPORTED_VERSION'), + ('PRAGMA user_version=0', 'SNAPSHOT_STORE.INVALID_VERSION'), + ('CREATE TABLE unexpected (value TEXT)', 'SNAPSHOT_STORE.INVALID_SCHEMA'), + ('DROP TRIGGER child_index_no_update', 'SNAPSHOT_STORE.INVALID_SCHEMA'), + ): + with self.subTest(change=change), tempfile.TemporaryDirectory() as temporary: + path = Path(temporary) / 'snapshots.sqlite3' + SQLiteSnapshotStore(path).close() + self.assertEqual(self.events(path), [('configure', 2, False)]) + with closing(sqlite3.connect(path, isolation_level=None)) as changer: + changer.execute(change) + # Same file, already audited by this process: only the audit is omitted. + self.assert_code(code, lambda: _ObservedAdmissionStore(path)) + self.assertTrue(path.is_file()) + + def test_new_process_rejects_corruption_in_a_file_audited_elsewhere(self): + for corruption, code in (('foreign-key', 'SNAPSHOT_STORE.FOREIGN_KEY_FAILURE'), + ('check', 'SNAPSHOT_STORE.INTEGRITY_FAILURE')): + with self.subTest(corruption=corruption), tempfile.TemporaryDirectory() as temporary: + path = Path(temporary) / 'snapshots.sqlite3' + # This process audits the file; that record stays in this process. + SQLiteSnapshotStore(path).close() + self.assertEqual(self.cold_open(path), 'admitted') + with closing(sqlite3.connect(path, isolation_level=None)) as corruptor: + if corruption == 'foreign-key': + corruptor.execute("INSERT INTO content_files VALUES ('missing', 'a', X'', 0, 'digest')") + else: + corruptor.execute('PRAGMA ignore_check_constraints=ON') + corruptor.execute("INSERT INTO content_sets VALUES ('bad', 0)") + self.assertEqual(self.cold_open(path), code) + self.assertTrue(path.is_file()) def test_version_one_keeps_preconfiguration_guarded_and_final_audits(self): _, record = _populated_version_one_store(self.path) before = _version_one_rows(self.path) + # Building the fixture audited this same file as a current store. That + # record must not shorten any audit of its version-one admission. + self.assertTrue(store_module._audit_recorded(_file_identity(self.path))) opened = _ObservedAdmissionStore(self.path) try: self.assertEqual(opened.admission_events, [ @@ -338,6 +486,18 @@ def test_version_one_keeps_preconfiguration_guarded_and_final_audits(self): opened.close() self.assertEqual(_version_one_rows(self.path), before) + def test_version_one_migration_in_a_new_process_keeps_every_audit(self): + _populated_version_one_store(self.path) + before = _version_one_rows(self.path) + _begin_as_new_process() + self.assertEqual(self.events(), [ + ('integrity', 1, False), ('configure', 1, False), + ('integrity', 1, True), ('integrity', 2, True), ('integrity', 2, False), + ]) + self.assertEqual(_version_one_rows(self.path), before) + # The final destination audit is this process's audit of the migrated file. + self.assertEqual(self.events(), [('configure', 2, False)]) + def test_other_opener_migration_keeps_destination_audit(self): _populated_version_one_store(self.path) before = _version_one_rows(self.path) @@ -404,6 +564,8 @@ def test_current_corruption_is_rejected_before_persistent_configuration(self): else: corruptor.execute('PRAGMA ignore_check_constraints=ON') corruptor.execute("INSERT INTO content_sets VALUES ('bad', 0)") + # The first open by a process is the one that audits. + _begin_as_new_process() self.assert_code(code, lambda: _ObservedAdmissionStore(path)) self.assertTrue(path.is_file()) with closing(sqlite3.connect(path)) as observed: