diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 2f27b224..9b4aa7ca 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -1,7 +1,7 @@ name: Dataset lifecycle tests on: push: - branches: [develop/dataset-workflows, feature/grounded-text-candidates, feature/image-caption-exports, feature/selected-release-preview-20261006, feature/saved-dataset-selections-20261006, fix/saved-selection-intent-20261006, feature/explicit-metadata-filters-20261006, feature/bulk-real-import-20261006, integration/dataset-candidate-20261006, feature/annotated-caption-import-20261006, integration/dataset-caption-stage-20261006, feature/curation-diagnostics-20261006, feature/rights-note-correction-20261006, feature/instruction-responses-20261007, feature/labeled-text-import-20261007, feature/text-classification-proposals, feature/local-postgres-metadata-20261011, integration/workspaces-postgres-20261011, feature/workspace-image-runs-20261011] + branches: [main, develop/dataset-workflows, feature/grounded-text-candidates, feature/image-caption-exports, feature/selected-release-preview-20261006, feature/saved-dataset-selections-20261006, fix/saved-selection-intent-20261006, feature/explicit-metadata-filters-20261006, feature/bulk-real-import-20261006, integration/dataset-candidate-20261006, feature/annotated-caption-import-20261006, integration/dataset-caption-stage-20261006, feature/curation-diagnostics-20261006, feature/rights-note-correction-20261006, feature/instruction-responses-20261007, feature/labeled-text-import-20261007, feature/text-classification-proposals, feature/local-postgres-metadata-20261011, integration/workspaces-postgres-20261011, feature/workspace-image-runs-20261011] pull_request: permissions: contents: read diff --git a/.github/workflows/workspaces-m1.yml b/.github/workflows/workspaces-m1.yml index 0550a288..f95ecf3b 100644 --- a/.github/workflows/workspaces-m1.yml +++ b/.github/workflows/workspaces-m1.yml @@ -1,7 +1,7 @@ name: M1 workspace interface on: push: - branches: [ui/dataset-workspaces-m1-20261011, integration/workspaces-postgres-20261011, feature/workspace-image-runs-20261011] + branches: [main, ui/dataset-workspaces-m1-20261011, integration/workspaces-postgres-20261011, feature/workspace-image-runs-20261011] pull_request: paths: - 'static/workspace*' diff --git a/backup_metadata.py b/backup_metadata.py index 0beba1be..82768308 100644 --- a/backup_metadata.py +++ b/backup_metadata.py @@ -12,6 +12,7 @@ from pathlib import Path import shutil import stat +import sys import tempfile import uuid @@ -95,10 +96,34 @@ def separate_paths(*paths): return paths +def tree_modes(path, expected): + """Transient permission evidence; the retained byte receipt stays unchanged.""" + path = Path(path) + result = {} + for name, kind in [('', 'directory')] + [(n, e['kind']) for n, e in expected.items()]: + relative = Path(name) + if name and (relative.is_absolute() or '..' in relative.parts or name != relative.as_posix()): + raise StorageTransferError('Unsafe artifact path in receipt.') + value = (path / name).lstat() + valid = stat.S_ISDIR(value.st_mode) if kind == 'directory' else stat.S_ISREG(value.st_mode) + if not valid: + raise StorageTransferError('Unsupported permission source: ' + name) + result[name] = stat.S_IMODE(value.st_mode) + return result + + def copy_tree(source, target, expected): - """Copy missing bytes into an owned staging directory; refuse changed bytes.""" + """Preserve bytes and permissions, refusing changed or contradictory staging.""" source, target = Path(source), Path(target) + modes = tree_modes(source, expected) target.mkdir(parents=True, exist_ok=True) + for name, mode in modes.items(): + directory = target / name + if (name == '' or expected[name]['kind'] == 'directory') and directory.exists(): + actual = directory.lstat().st_mode + if (not stat.S_ISDIR(actual) or + (not actual & stat.S_IWUSR and stat.S_IMODE(actual) != mode)): + raise StorageTransferError('Read-only staging directory permissions differ: ' + name) for name, entry in expected.items(): relative = Path(name) if relative.is_absolute() or '..' in relative.parts or name != relative.as_posix(): @@ -111,24 +136,52 @@ def copy_tree(source, target, expected): else: target_file.parent.mkdir(parents=True, exist_ok=True) if target_file.exists(): - if not target_file.is_file() or file_hash(target_file) != entry['sha256']: - raise StorageTransferError('Staged artifact differs from backup: ' + name) + if (not target_file.is_file() or file_hash(target_file) != entry['sha256'] + or stat.S_IMODE(target_file.stat().st_mode) != modes[name]): + raise StorageTransferError('Staged artifact bytes or permissions differ: ' + name) else: - # A partial copy is never given the final artifact name. + # Never widen read-only staging permissions to force a retry. + if not target_file.parent.stat().st_mode & stat.S_IWUSR: + raise StorageTransferError('Read-only staging cannot accept missing artifact: ' + name) temporary = target.parent / ('.tuldok-transfer-' + uuid.uuid4().hex) try: - with source_file.open('rb') as reader, temporary.open('xb') as writer: + before = source_file.lstat() + descriptor = os.open(source_file, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(descriptor, 'rb') as reader, temporary.open('xb') as writer: + opened = os.fstat(reader.fileno()) + if (not stat.S_ISREG(opened.st_mode) or before != opened + or stat.S_IMODE(opened.st_mode) != modes[name]): + raise StorageTransferError('Source identity or permissions changed: ' + name) shutil.copyfileobj(reader, writer) writer.flush() + os.fchmod(writer.fileno(), modes[name]) os.fsync(writer.fileno()) + after = os.fstat(reader.fileno()) + if (before.st_ino, before.st_size, before.st_mtime_ns, before.st_ctime_ns, before.st_mode) != ( + after.st_ino, after.st_size, after.st_mtime_ns, after.st_ctime_ns, after.st_mode): + raise StorageTransferError('Source changed during copy: ' + name) + current = source_file.lstat() + if (current.st_dev, current.st_ino, current.st_mode, current.st_ctime_ns) != ( + before.st_dev, before.st_ino, before.st_mode, before.st_ctime_ns): + raise StorageTransferError('Source changed during copy: ' + name) if temporary.stat().st_size != entry['bytes'] or file_hash(temporary) != entry['sha256']: raise StorageTransferError('Artifact changed during copy: ' + name) os.rename(temporary, target_file) finally: temporary.unlink(missing_ok=True) - for directory in sorted((p for p in target.rglob('*') if p.is_dir()), reverse=True): - sync_directory(directory) - sync_directory(target) + if tree_modes(source, expected) != modes: + raise StorageTransferError('Source permissions changed during copy.') + directories = [name for name, entry in expected.items() if entry['kind'] == 'directory'] + for name in sorted(directories, key=lambda n: len(Path(n).parts), reverse=True) + ['']: + directory = target / name + descriptor = os.open(directory, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW) + try: + os.fchmod(descriptor, modes[name]) + os.fsync(descriptor) + finally: + os.close(descriptor) + if tree_modes(target, expected) != modes: + raise StorageTransferError('Copied permissions differ from source.') def pack(value): @@ -226,6 +279,41 @@ def populate_catalog(metadata, tables): raise StorageTransferError('Restored PostgreSQL row/hash reconciliation failed.') +def _remove_partial_contents(descriptor): + """Delete only entries reached from the retained invocation-owned handle.""" + current = os.fstat(descriptor) + if not stat.S_ISDIR(current.st_mode) or current.st_uid != os.getuid(): + raise StorageTransferError('Private partial backup ownership changed.') + os.fchmod(descriptor, stat.S_IMODE(current.st_mode) | 0o700) + with os.scandir(descriptor) as entries: + for entry in entries: + before = entry.stat(follow_symlinks=False) + if stat.S_ISLNK(before.st_mode) or before.st_uid != os.getuid(): + raise StorageTransferError('Private partial backup custody changed; retained.') + if stat.S_ISDIR(before.st_mode): + child = os.open(entry.name, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, + dir_fd=descriptor) + try: + opened = os.fstat(child) + if (before.st_dev, before.st_ino, before.st_uid) != ( + opened.st_dev, opened.st_ino, os.getuid()): + raise StorageTransferError('Private partial backup identity changed; retained.') + _remove_partial_contents(child) + named = os.stat(entry.name, dir_fd=descriptor, follow_symlinks=False) + if (named.st_dev, named.st_ino) != (opened.st_dev, opened.st_ino): + raise StorageTransferError('Private partial backup identity changed; retained.') + os.rmdir(entry.name, dir_fd=descriptor) + finally: + os.close(child) + elif stat.S_ISREG(before.st_mode): + named = os.stat(entry.name, dir_fd=descriptor, follow_symlinks=False) + if (named.st_dev, named.st_ino) != (before.st_dev, before.st_ino): + raise StorageTransferError('Private partial backup identity changed; retained.') + os.unlink(entry.name, dir_fd=descriptor) + else: + raise StorageTransferError('Private partial backup contains special files; retained.') + + def backup_catalog(path, backup, *, dsn=None, quiesced=False): """Create a complete immutable backup after the caller stops every writer.""" from metadata import Metadata @@ -236,14 +324,22 @@ def backup_catalog(path, backup, *, dsn=None, quiesced=False): raise StorageTransferError('Backup destination already exists; never overwrite a backup.') backup.parent.mkdir(parents=True, exist_ok=True) temporary = Path(tempfile.mkdtemp(prefix=backup.name + '.partial-', dir=backup.parent)) - metadata = Metadata(path, dsn=dsn, create=False, schema_version=None) + parent_descriptor = partial_descriptor = None + partial_identity = None + metadata = None try: + parent_descriptor = os.open(backup.parent, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW) + partial_descriptor = os.open(temporary.name, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, + dir_fd=parent_descriptor) + partial_identity = os.fstat(partial_descriptor) + metadata = Metadata(path, dsn=dsn, create=False, schema_version=None) with metadata.lock, metadata.transaction(): before = tree_manifest(path) tables = read_catalog(metadata) sequences = read_sequences(metadata) copy_tree(path, temporary / 'catalog', before) - if before != tree_manifest(path) or before != tree_manifest(temporary / 'catalog'): + if (before != tree_manifest(path) or before != tree_manifest(temporary / 'catalog') + or tree_modes(path, before) != tree_modes(temporary / 'catalog', before)): raise StorageTransferError('Artifacts changed during backup. Keep all writers stopped.') write_json(temporary / 'metadata.json', {'format_version': FORMAT_VERSION, 'schema_version': metadata.schema_version, 'dataset_id': metadata.dataset_id, 'tables': tables, @@ -261,9 +357,39 @@ def backup_catalog(path, backup, *, dsn=None, quiesced=False): sync_directory(backup.parent) return receipt finally: - metadata.close() - if temporary.exists(): - shutil.rmtree(temporary) + original_error = sys.exc_info()[1] + try: + if metadata is not None: + metadata.close() + if partial_identity is None: + raise StorageTransferError('Private partial backup identity unavailable; retained.') + try: + named = os.stat(temporary.name, dir_fd=parent_descriptor, follow_symlinks=False) + except FileNotFoundError: + named = None + if named is not None: + if (named.st_dev, named.st_ino, named.st_uid) != ( + partial_identity.st_dev, partial_identity.st_ino, os.getuid()): + raise StorageTransferError('Private partial backup identity changed; retained.') + _remove_partial_contents(partial_descriptor) + named = os.stat(temporary.name, dir_fd=parent_descriptor, follow_symlinks=False) + if (named.st_dev, named.st_ino) != (partial_identity.st_dev, partial_identity.st_ino): + raise StorageTransferError('Private partial backup identity changed; retained.') + os.rmdir(temporary.name, dir_fd=parent_descriptor) + except (OSError, StorageTransferError) as cleanup_error: + if original_error is None: + raise + diagnostic = ('Partial backup cleanup refused; preserve for diagnosis: ' + + str(temporary) + ': ' + str(cleanup_error)) + original_error.backup_cleanup_diagnostic = diagnostic + add_note = getattr(original_error, 'add_note', None) + if callable(add_note): + add_note(diagnostic) + finally: + if partial_descriptor is not None: + os.close(partial_descriptor) + if parent_descriptor is not None: + os.close(parent_descriptor) def restore_catalog(backup, destination, *, dsn=None): @@ -294,22 +420,53 @@ def restore_catalog(backup, destination, *, dsn=None): metadata = Metadata(destination, dsn=dsn, create=False, dataset_id=identity, schema_version=version) try: with metadata.lock, metadata.transaction(): - if summaries(read_catalog(metadata)) != receipt['tables'] or read_sequences(metadata) != receipt['sequences'] or tree_manifest(destination, (marker_name,)) != receipt['files']: + if (summaries(read_catalog(metadata)) != receipt['tables'] or read_sequences(metadata) != receipt['sequences'] or tree_manifest(destination, (marker_name,)) != receipt['files'] + or tree_modes(destination, receipt['files']) != tree_modes(backup / 'catalog', receipt['files'])): raise StorageTransferError('Restored catalog has changed; refusing to overwrite later writes.') return marker finally: metadata.close() + staging_descriptor = None if staging.exists(): if json.loads((staging / marker_name).read_bytes()) != marker: raise StorageTransferError('Restore staging directory belongs to another transfer.') else: staging.mkdir(parents=True, mode=0o700) write_json(staging / marker_name, marker) + staging_descriptor = os.open(staging, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW) # The source manifest names an already existing schema. Create this catalog # before copying that manifest, so an existing schema is never reused. - existed = (staging / '.tuldok.json').exists() - metadata = Metadata(staging, dsn=dsn, create=not existed, dataset_id=identity, schema_version=version) + metadata = None try: + existed = (staging / '.tuldok.json').exists() + initialization = {} + if staging_descriptor is not None: + if os.path.lexists(staging / '.tuldok.json'): + raise StorageTransferError('New restore catalog manifest already exists.') + held = os.fstat(staging_descriptor) + named = staging.lstat() + if (held.st_dev, held.st_ino, held.st_uid) != (named.st_dev, named.st_ino, os.getuid()): + raise StorageTransferError('New restore staging identity changed.') + modes = tree_modes(backup / 'catalog', receipt['files']) + initialization['initial_file_modes'] = {name: modes[name] + for name in ('.tuldok.json', '.metadata-init.lock')} + elif not os.path.lexists(staging / '.tuldok.json') and os.path.lexists(staging / '.tuldok-initializing.json'): + modes = tree_modes(backup / 'catalog', receipt['files']) + lock = staging / '.metadata-init.lock' + current = lock.lstat() + if (not stat.S_ISREG(current.st_mode) or + stat.S_IMODE(current.st_mode) != modes['.metadata-init.lock'] or + file_hash(lock) != receipt['files']['.metadata-init.lock']['sha256']): + raise StorageTransferError('Pending restore lock bytes or permissions differ.') + # Metadata validates the pending identity/schema before publishing a + # newly created marker. The existing lock receives no permission change. + initialization['initial_file_modes'] = {'.tuldok.json': modes['.tuldok.json']} + metadata = Metadata(staging, dsn=dsn, create=not existed, dataset_id=identity, + schema_version=version, **initialization) + if staging_descriptor is not None: + named = staging.lstat() + if (held.st_dev, held.st_ino) != (named.st_dev, named.st_ino): + raise StorageTransferError('New restore staging identity changed.') with metadata.lock, metadata.transaction(): current = read_catalog(metadata) if any(state['count'] for state in current.values()): @@ -325,7 +482,10 @@ def restore_catalog(backup, destination, *, dsn=None): sync_directory(destination.parent) return marker finally: - metadata.close() + if metadata is not None: + metadata.close() + if staging_descriptor is not None: + os.close(staging_descriptor) def main(): @@ -341,7 +501,11 @@ def main(): result = (backup_catalog(args.catalog, args.backup, quiesced=args.quiesced) if args.command == 'backup' else restore_catalog(args.backup, args.destination)) except (StorageTransferError, OSError, ValueError) as error: - parser.exit(1, str(error) + '\n') + message = str(error) + diagnostic = getattr(error, 'backup_cleanup_diagnostic', None) + if diagnostic: + message += '\n' + diagnostic + parser.exit(1, message + '\n') print(json.dumps(result, indent=2)) diff --git a/docs/contracts/captured-cpu-runtime.md b/docs/contracts/captured-cpu-runtime.md index b5d3ecd0..1caaa10d 100644 --- a/docs/contracts/captured-cpu-runtime.md +++ b/docs/contracts/captured-cpu-runtime.md @@ -122,3 +122,12 @@ This extension does not enlarge captured input/source or diagnostic streams, alter cancellation/drain deadlines, enable network isolation, or qualify GPU/JIT. Actual recipes retain explicit smaller output budgets and require measured capacity admission. See the external recipe contract for custody and downloads. + + +## Backup permission preservation + +Captured executable (`0500`) and data (`0400`) permissions survive new backups +and restores. Historical byte receipts do not authenticate modes: qualify the +actual snapshot at its restored root before claiming runtime acceptance. +The canonical transfer, retry and partial-cleanup policy is in +[local storage](local-storage.md#backup-permission-preservation). diff --git a/docs/contracts/local-storage.md b/docs/contracts/local-storage.md index 24ed2bf2..3559bc4f 100644 --- a/docs/contracts/local-storage.md +++ b/docs/contracts/local-storage.md @@ -227,3 +227,30 @@ complete backup, never automatically deleted. Deletion staging is reconciled against committed metadata on reopen. Nested deletion defers physical removal until the outermost commit. Release publication and worker dispatch refuse an enclosing transaction so they cannot publish or execute uncommitted intent. + + +## Backup permission preservation + +The existing PostgreSQL backup and restore owner preserves ordinary file +permission bits, including captured runtime executables (`0500`) and data +(`0400`). It applies directory permissions after copying children, with the +catalog root last. Source identity and permissions must remain stable during +copying. Resumed files must match both bytes and permissions; a missing file +inside read-only staging is refused without widening directory permissions. +Writable staging directories are intermediate construction state; existing +read-only directories must already match source permissions. A completed restore +retry compares all permissions against the retained backup. On backup failure, +only the invocation-owned private partial backup directories are made removable +for cleanup through retained directory descriptors and no-follow child opens. +Source and resumable restore directories are never widened. If private-partial +identity or traversal is uncertain, cleanup stops, retains the partial and adds +a diagnostic note to the original error rather than masking that failure. +The operator must preserve refused staging for diagnosis; the transfer never +repairs source files or rewrites runtime manifests. + +The version-1 byte manifest and receipt format are unchanged. Permissions are +copied from the ordinary backup files, not authenticated by the historical byte +receipt. Older backups made without permission preservation cannot establish +original permissions or usable captured runtimes. Cold runtime verification is +required before claiming runtime restore acceptance; synthetic executable +fixtures do not qualify a real model or runtime installation. diff --git a/docs/contracts/workspace-storage.md b/docs/contracts/workspace-storage.md index a8f069d7..91a2443f 100644 --- a/docs/contracts/workspace-storage.md +++ b/docs/contracts/workspace-storage.md @@ -229,3 +229,12 @@ does not interpret executable recipes or synthesize approval. Integer/text columns use the existing backup format; retained schema-7 backups restore exactly and must be explicitly upgraded. Complete schema-8 backups preserve approval state and identities; recovery does not implicitly refund approvals. + + +## Backup permission preservation + +Complete PostgreSQL/artifact transfers preserve file and directory permissions +without changing the retained byte receipt format or historical Run identities. +Resumed and completed restores refuse contradictory permissions. +The canonical transfer, retry and partial-cleanup policy is in +[local storage](local-storage.md#backup-permission-preservation). diff --git a/docs/plans/generic-scientific-projects/execution-ledger.md b/docs/plans/generic-scientific-projects/execution-ledger.md index 31d7bf60..317fbf73 100644 --- a/docs/plans/generic-scientific-projects/execution-ledger.md +++ b/docs/plans/generic-scientific-projects/execution-ledger.md @@ -228,3 +228,37 @@ verified durable publication; all89 published counterparts rehashed afterwards. Preserved old cleanup evidence under its original names and recorded this new cleanup separately. No failed staging, snapshot, source/input or custody removed. P6 source checkpoint b02004a is draftPR53; no merge/deployment. + +## P7 isolated source checkpoint and merge authority + +P7 permission-preservation fix and explicit staged restore/browser procedures +are implemented in an isolated feature branch. Focused tests and independent +review passed; actual transfer and acceptance remain pending. See +[verification](reports/p7-verification.md) and [admission](reports/p7-admission.md). +The user's subsequent explicit instruction admits merging qualified Tuldok PRs, +assessing owner32/33 and verifying integrated main. PRs38–42 were merged in +dependency order with exact qualified trees retained. No incomplete/failing +work or deployment is admitted by this new authority. + +## P6 hosted acceptance and P7 actual qualification + +PR52 `fb3a5a0` and PR53 `5c0e7f9` passed both final hosted checks. P6's +patched full discovery passed 900 tests with 36 explicit skips; dedicated +local optional qualification remains separately recorded. G5/P6 is accepted. + +P7's actual separate-database restore passed raw table/schema/sequence/tree +parity, cold runtime and completed-artifact/release reads, and public +Data/Pipeline/Run inspection at desktop and narrow widths. All 89 evaluation +artifacts matched full authenticated HTTP hashes. The original database and +verified backup remain unchanged; only the admitted task-created source tree +was removed after recovery proofs. The first logout failure, exact session +cleanup and fresh successful browser attempt remain recorded. + +Final review additionally admitted exclusive fresh-catalog permission +initialization for private manifests/cross-umask restores. This refinement passed independent review and all 38 affected tests with +zero skips and frozen source. P7 final hosted regression remains pending. PR32 owner screenshots +and independent main CI trigger PR54 were qualified and merged, preserving +owner image bytes. Integrated main is `f2751c3`; its UI check passed and its +lifecycle check is queued. PR33 requires separate deliberate reconciliation. +PR43 and its dependent stack remain unmerged while the actual original v2 +packet gate is blocked. A8 lacks a live supported model/runtime. No deployment. diff --git a/docs/plans/generic-scientific-projects/plan.md b/docs/plans/generic-scientific-projects/plan.md index ad97abad..d985aa15 100644 --- a/docs/plans/generic-scientific-projects/plan.md +++ b/docs/plans/generic-scientific-projects/plan.md @@ -2,9 +2,9 @@ **Plan status:** `Active` -**Current phase:** P5 qualified; P6 actual scientific MCP read passed and final test/CI qualification is active. See [P5c](reports/p5c-verification.md) and [P6](reports/p6-verification.md). +**Current phase:** P5 and P6 are qualified. P7 actual scientific backup, separate-database restore, cold reads and public UI/artifact checks passed. The narrowly scoped catalog creation-permission refinement passed final affected tests and independent review. Final hosted regression and original legacy-v2 acceptance remain under verification. See [P5c](reports/p5c-verification.md), [P6](reports/p6-verification.md) and [P7](reports/p7-verification.md). -**Next slice:** Finish P6 regression/CI and P7 complete PostgreSQL/artifact restore; acceptance remains partial. +**Next slice:** Finish P7 hosted regression; verify integrated main. Original v2 packet access and A8 real image/text inference remain external blockers to the overall task. **Acceptance status:** `partial` @@ -36,10 +36,10 @@ The user approved publishing this plan and staged implementation on 2026-10-13. | G2 | Scientific fields preserve exact float bytes, dimensions, staggered placement, units, time and trajectory identity through import, freeze, download and cold read | contract | representative | automated | satisfied | [Actual native and neutral custody workflows](reports/p2-verification.md) | | G3 | An optional Rheon adapter exposes bounded generation controls and produces real generic scientific data; removing it leaves generic project/data use intact | integration | required-real | automated | satisfied | [P4 actual generation and no-adapter startup](reports/p4-verification.md) | | G4 | Saved graph executes existing lab fitting and autonomous evaluation scripts; verified checkpoint, full-field comparisons and physics diagnostics survive restart with complete provenance | system | required-real | automated | satisfied | [P5b training](reports/p5b-verification.md) and [P5c real evaluation/restart](reports/p5c-verification.md) | -| G5 | Agent and UI clients reach the same domain owners for the admitted bounded matrix and equivalent authority; owner-only review/grants remain separate. Stale requests, forged human review, unauthorized execution, path escape and approval reuse fail before side effects | contract | representative | automated | pending | P3/P6 public-interface denial and parity tests | +| G5 | Agent and UI clients reach the same domain owners for the admitted bounded matrix and equivalent authority; owner-only review/grants remain separate. Stale requests, forged human review, unauthorized execution, path escape and approval reuse fail before side effects | contract | representative | automated | satisfied | [P6 real SDK parity, denial and final hosted regression](reports/p6-verification.md) | | G6 | Admission is idempotent; cancel, crash and restart never duplicate work, publish partial success or silently leave descendants running | system | representative | automated | satisfied | [P3 custody and fault tests](reports/p3b-verification.md), retained by final P5 regression | -| G7 | Existing imports/releases/runs retain identities and bytes, including Rheon v1/v2; explicit migration and complete PostgreSQL/artifact restore preserve old and new records | integration | representative | automated | pending | P2/P7 retained-history and restore workflow | -| G8 | Desktop/narrow/keyboard UI supports open → generate → review/freeze → edit graph → train/evaluate → inspect results without explanatory clutter or false qualification labels | user-workflow | required-real | either | pending | P7 reviewed screenshots and actual-run browser receipt | +| G7 | Existing imports/releases/runs retain identities and bytes, including Rheon v1/v2; explicit migration and complete PostgreSQL/artifact restore preserve old and new records | integration | representative | automated | pending | [P7 actual scientific restore and retained fixture matrix](reports/p7-verification.md) passed; original legacy-v2 packet remains blocked | +| G8 | Desktop/narrow/keyboard UI supports open → generate → review/freeze → edit graph → train/evaluate → inspect results without explanatory clutter or false qualification labels | user-workflow | required-real | either | satisfied | [P1 desktop/narrow/keyboard](reports/p1-verification.md), [P4 real generation/review](reports/p4-verification.md), [P5 real train/evaluate](reports/p5c-verification.md), [P7 restored UI](reports/p7-verification.md) | ## Scope @@ -236,21 +236,21 @@ Statuses below track the approved staged implementation. Before starting each mi **Acceptance gate:** Complete G5 against the admitted matrix through a real workflow with separate owner review/grants. Compare domain evidence; exercise revisions, lost acknowledgements, revoked credentials, human-review spoofing, grant reuse, fresh external attempts, scoped test exposure and exact artifact reassembly. Excluded capabilities remain absent from MCP; existing HTTP authority is unchanged. No new actor-specific privilege bypass. -**Status:** `Planned` +**Status:** `Accepted` -**Preparation admission:** [P6 exact contract and sequencing](reports/p6-admission.md). Source preparation is active; objective acceptance remains pending. +**Preparation admission:** [P6 exact contract and sequencing](reports/p6-admission.md). The admitted matrix passed actual SDK scientific parity and final local/hosted gates. ### P7: Complete workflow and compatibility qualification **Goal:** Deliver an installable local workflow with preserved old data, explicit limitations and meaningful real scientific acceptance. -**Allowed write set:** existing CI configuration, deployment/dependency documentation, existing backup/upgrade and browser acceptance tests, scoped packaging needed by P1–P6, README and this plan's reports. Runtime fixes remain in the owning milestone's boundary and receive affected re-review. +**Allowed write set:** existing CI configuration, deployment/dependency documentation, existing backup/upgrade, metadata initialization and browser acceptance tests, scoped packaging needed by P1–P6, README and this plan's reports. Runtime fixes remain in the owning milestone's boundary and receive affected re-review. **Tasks:** Fresh setup/reopen, full PostgreSQL/artifact restore, old+new results, offline operation after installation, full actual browser workflow, all relevant lint/static/type/unit/integration/UI checks supported by chosen technologies; independent architecture/security/scientific review and applicable hosted CI once publication is authorized. **Acceptance gate:** G1–G8 satisfied with linked evidence; no unqualified hardware/performance/model claims; all material review findings resolved. Separate outstanding dataset-workflows A8 and actual legacy-v2 packet qualification remain visible rather than hidden by this plan's completion. -**Status:** `Planned` +**Status:** `Verifying` ## Blockers diff --git a/docs/plans/generic-scientific-projects/reports/p6-verification.md b/docs/plans/generic-scientific-projects/reports/p6-verification.md index 687c3e2a..b071fc78 100644 --- a/docs/plans/generic-scientific-projects/reports/p6-verification.md +++ b/docs/plans/generic-scientific-projects/reports/p6-verification.md @@ -138,3 +138,22 @@ API-test patch the only change from the original full discovery inventory. `7b1de6a33f76b3cf08ad45a91af346bfbb5ff13840a459937d73b5f4120e979a`. The failed original900-test discovery remains recorded; fresh hosted full discovery on the patched source is the final regression acceptance gate. + +## Final hosted acceptance + +Both final checks passed at PR53 head +`5c0e7f92914e5aeac6ec939e3a667fc85b22aa90`: +[lifecycle](https://github.com/MrScripty/Tuldok/actions/runs/38478255351) and +[workspace UI](https://github.com/MrScripty/Tuldok/actions/runs/38478255410). +Hosted full discovery passed 900 tests in 672.693 seconds with 36 explicit +skips. The separately recorded local optional environments cover the admitted +reader, training, evaluation and SDK paths with zero skips. Hosted agent +22-test and MCP 13-test modules also passed. The broader suite is not claimed +to have zero skips. Independent review and final JavaScript/browser checks +passed. This supersedes the pending full-regression status above and satisfies +G5 for the explicitly admitted operation matrix. + +The later caption-browser change only waits for the loaded page before DOM +reads; it changes no scientific or MCP production behavior. Actual scientific +SDK evidence remains tied to source `b02004a`. Original v2 packet and A8 +real-model acceptance remain separate blockers to overall completion. diff --git a/docs/plans/generic-scientific-projects/reports/p7-admission.md b/docs/plans/generic-scientific-projects/reports/p7-admission.md new file mode 100644 index 00000000..4d3f779f --- /dev/null +++ b/docs/plans/generic-scientific-projects/reports/p7-admission.md @@ -0,0 +1,161 @@ +# P7 admission: preserve runtime permissions and qualify restore + +Root admits isolated source preparation on 2026-10-13 in branch +`feature/scientific-restore-qualification-20261013`, based on P6 checkpoint +`b02004a` (draft PR53). P5c actual evaluation has passed; its remaining final +checks and P6 full acceptance are separately gated. This sequencing amendment +allows only the bounded existing backup-owner fix and focused tests in this +isolated worktree. Do not alter frozen P5c/P6 sources or the qualified catalog. +The later resume instruction admits P7 inventory, complete backup and separate-DB +SQL probe preparation while the isolated P6 test-only correction finishes. +P6 actual scientific MCP read already passed; P7 product acceptance remains +gated on final P6 checks and measured transfer evidence. No deletion is admitted +by this document. Restore waits for a concrete verified recovery plan. + +The observed defect is that copy_tree drops file permissions: a byte-exact +restore can make captured 0500 executables and 0400 data fail runtime verification. +Admit mode preservation in existing backup_metadata.copy_tree, source identity +and mode consistency checks, matching modes for resumed/completed restore, +and directories applied after children. Preserve byte manifest/receipt format; +old byte-only backups do not prove original modes. Refuse changed evidence; +never repair source or historical snapshot manifests. Safe retry behavior for +read-only staging must be explicit and tested, with no generic chmod bypass. + +Owner write set: backup_metadata.py; tests/test_workspace_metadata.py; +tests/test_runtime_snapshots.py; docs/contracts/local-storage.md, +docs/contracts/captured-cpu-runtime.md and docs/contracts/workspace-storage.md. +A focused tests/qualify_scientific_restore.py may compose existing owners. +Root owns plan/ledger/reports. Independent review and focused/full tests precede +acceptance and publication. No new backup framework, schema migration or model +execution is admitted. + +# P7 proposed backup/restore qualification + +2026-10-13. Planning only during active P5c evaluation. No source changes, deletion, backup, restore, model execution or heavy tests. Actual P5c numerical/lifecycle gates must finish first. QA owner review/auth flows are already authorized; this plan adds no generic approval ceremony. Destructive removal, if needed, still requires the parent to approve exact task-owned paths and verified recovery evidence. No deletion is authorized here. + +## Existing owners and prerequisite fix + +`backup_metadata.py backup CATALOG BACKUP --quiesced` creates a new separate/nonnested backup. It packs every SQL table losslessly, retains opaque JSON text, per-row/table hashes and identity sequence high-water marks, copies the complete ordinary-file tree, compares source before/after and backup bytes, then publishes metadata.json/backup-receipt.json/catalog. Symlinks and special files are refused. On failure only the newly owned partial backup is cleaned. Quiesced is an assertion, not a process stopper. + +`backup_metadata.py restore BACKUP DESTINATION` preserves the dataset UUID and original schema version. The target directory must be new and the target PostgreSQL catalog schema absent. It validates backup bytes/metadata, creates marker-bound staging, reconciles/inserts rows, copies files and publishes by rename. Failure can leave DB rows and staging; same-target retry reconciles the exact marker/rows rather than merging/replaying. A repeated completed restore accepts only an unchanged destination and refuses later writes. + +`upgrade_metadata.py CATALOG --quiesced` explicitly upgrades schemas1–7 to8, retaining the known TABLES_V1..V7 identities. Real current schema8 restore needs no upgrade. Older small fixture restores prove old-state parity before explicitly upgrading. + +**Concrete prerequisite:** `backup_metadata.tree_manifest` records bytes/hash, not modes; `copy_tree` currently writes temporary files with default umask and never restores mode. CPU snapshot verification requires exact0500 ELF/0400 data modes (`recipe_process._cpu_stream_file`). Byte-perfect backup/restore alone cannot qualify a usable runtime. + +Parent-selected fix: preserve source file modes in existing `copy_tree`, applying directory modes last after children are copied, with cold runtime verification. Keep existing backup manifest/receipt byte format and supported old readers unchanged. Source permissions must be checked consistently while copying, before final publication; do not repair them later with an undocumented chmod script or rewrite a runtime manifest. Existing old byte-only backups cannot guarantee runtime usability; disclose this, do not claim they were previously qualified. New backups copy canonical source permissions into their own files and restore preserves those permissions. Test restrictive writable/read-only modes, directories, copied file ownership/link count, and exact runtime manifest expectations without model execution. + +Exact proposed owner/write set: storage owner `backup_metadata.py`, `tests/test_workspace_metadata.py`, `tests/test_runtime_snapshots.py`; actual restore evidence may add one focused `tests/qualify_scientific_restore.py` procedure reusing these owners. Documentation owner updates `docs/contracts/local-storage.md`, `docs/contracts/captured-cpu-runtime.md`, `docs/contracts/workspace-storage.md` and parent plan/ledger/reports. No current source edits during evaluation. Root admits and independently reviews this bounded prerequisite first; no alternative backup framework or schema migration. + +## Identity and database constraints + +.tuldok.json fixes dataset_id and PostgreSQL schema `catalog_`. Removing its filesystem tree does NOT remove that schema. Prefer an explicitly configured separate restore database, leaving the stopped original DB/schema intact. Provision it through the authorized fixture/service owner. Do not print DSNs/passwords. If a separate DB cannot be provisioned, report the blocker; do not improvise DROP SCHEMA to force restore to pass. + +Runtime manifest, saved graph/Run/material/spec/request/grant and custody receipt bytes remain exact. Historical spec.cwd/output_dir and cpu_snapshot.root are evidence, not current destinations. Completed Run artifact reads use the restored catalog's owned paths. Dispatch refuses a historical staging-root mismatch; no historical attempt is resumed. `RuntimeSnapshots.get(sha,verify=True)` creates a fresh inspection descriptor pointing to the current snapshot root from its unchanged manifest. New preflight would create new material/approval identity; never rewrite old paths/hashes. + +External project files are outside full catalog backup. Project rows preserve immutable manifest snapshots/root aliases, but restored app inspection requires explicit authorized root bindings. Missing external files remain missing. Captured per-Run inputs/sources and snapshot bytes inside the catalog must be backed up. Backup credentials are digests, but live sessions/grants may remain effective until expiry: protect full backups like the source and do not publish their rows/tokens. + +## Phase 1: finish, inventory, quiesce + +1. Finish actual evaluation and independent all-output/full-field verification. Large-profile `receipt_verified/payloads:not_checked` list/get is NOT all-payload verification. Preserve exact checkpoint/freeze/training receipts, numerical results, original NPZ/viewport, scientific chunks and native-wire evidence. Record failures honestly. +2. Obtain exact catalog path, UUID, source commit, Python executable, actual Run IDs and private source/restore DB aliases from parent. The shared qualified scientific catalog is the intended source; do not guess a scratch path for destructive work. +3. Stop app and workers via lifecycle owners and confirm drained terminal states across Runs/generation/proposals. No queued/running/cancelling/unknown work for this snapshot. Unknown cessation is a blocker; directory deletion or a second Dataset cannot resolve it. Never signal remembered PIDs. +4. Keep all writers stopped. Capture raw `Metadata(create=False,schema_version=None)` table summaries via `read_catalog`, sequence high-water marks via `read_sequences`, manifest bytes, file digests/sizes/modes and actual free space. Do this BEFORE Dataset/login/runtime CLI; those can append audit/recovery rows. Record checks outside the catalog to avoid perturbing its tree. + +## Phase 2: prefer narrowly approved staging duplicate cleanup + +After confirmed successful publication, `.external-/output` contains a duplicate of durable `workspace-runs//`. Propose cleanup only if each exact target is parent-approved after evidence: + +- Completed Run, drained successful custody; every declared source output matches published size+SHA; published manifest and receipt independently verify. +- Target is the task-owned canonical directory beneath the confirmed catalog, with no symlinks/undeclared files. No active reader/writer depends on it. Capture path/count/byte/digest inventory and retained counterpart in a cleanup receipt. +- Current artifact/capture/scientific-import owners use durable published paths; historical spec output paths are not operational dependencies. + +If approved, delete ONLY that output subtree, keeping `.external-/custody` logs/receipt, captured sources/inputs/bootstrap, runtime snapshots and durable artifacts. Do not remove the entire staging tree or any failed/interrupted/unknown attempt evidence. Do not rewrite historical spec.output_dir. Verify all published artifacts, progress custody and typed input capture again. Record the intentional before/after tree delta. No such deletion occurred in this planning task. + +## Phase 3: measured space and two-copy fallback + +Let C be measured source file bytes after any approved cleanup, M metadata/receipt overhead, and R at least512MiB operational reserve plus actual DB/WAL growth. Parent forecast is approximately2.2GB before evaluation +3.3GB canonical evaluation ≈5.5GB; unremoved staging duplicates add another~3.3GB. These are estimates, not permission to start transfer. Recompute on the actual filesystem after evaluation. + +`copy_tree` copies a file into an adjacent temporary and renames it into its final staging location. That temporary becomes part of the final destination C: it does not add a whole extra largest file beyond the complete C budget. Account separately for filesystem overhead, metadata, DB/WAL and concurrent user writes. No compression/dedup/reflink is promised. Measure /tmp separately; do not assume it shares /workspace's capacity or persistence. + +Preferred no-source-deletion protocol: source+backup+restore coexist, approximately3C+M plus databases/reserve. If this fits after approved duplicate cleanup, use it and retain original DB/source stopped. + +If only two copies fit: source→complete verified backup; independently verify backup and SQL readability; parent approves removal of ONLY the exact task-owned original catalog tree (resolved path, UUID, full digest, backup path and receipt hash); remove original filesystem tree while preserving original stopped DB; restore to another path and a separate DB. Peak backup+restore≈2C+M plus databases/reserve. No DB schema, source checkout, backup or other catalog deletion. Full backup must exist and be independently verified BEFORE original removal. No completed duplicate cleanup or whole-catalog removal is assumed done. + +Before source deletion, prove SQL backup readability without a second full artifact copy: validate metadata.json SHA, every packed value/table columns/count/row digest/sequence and dataset/schema definition; exercise existing `populate_catalog` in a separate disposable probe DB and rollback transaction if needed. This proves types/permissions, not complete restore. Keep actual restore target schema absent. No ad hoc DB cleanup outside exact disposable ownership. + +## Phase 4: existing commands (planned, not executed) + +Use frozen source and task-specific reviewed absolute paths, with new separate/nonnested backup and restore destinations. DSNs are supplied through private environment files; never echo them. + +``` +source "$TULDOK_QA_SOURCE_ENV" +"$TULDOK_QA_PYTHON" backup_metadata.py backup \ + "$TULDOK_QA_CATALOG" "$TULDOK_QA_BACKUP" --quiesced +# Independently verify full backup; parent owns any exact approved removal. +source "$TULDOK_QA_RESTORE_ENV" +"$TULDOK_QA_PYTHON" backup_metadata.py restore \ + "$TULDOK_QA_BACKUP" "$TULDOK_QA_RESTORED" +``` + +Only genuinely old small fixture catalogs then use: +``` +"$TULDOK_QA_PYTHON" upgrade_metadata.py "$TULDOK_QA_OLD_RESTORED" --quiesced +``` + +## Phase 5: raw parity, then restored workflows + +Before auth/app startup, compare actual restored schema/version/definition, UUID, all table columns/counts/row hashes/table digests and sequences to backup. Compare full restored tree excluding only `.restore-receipt-.json`. Verify the prerequisite's permission invariants and unchanged full backup tree. Historical graph/Run documents, IDs, material/spec/receipt bytes stay exact. No path normalization in opaque JSON. + +Then open Dataset using explicit restore DSN/root bindings. Record expected new auth/audit/reconciliation events separately. Verify completed actual Runs remain completed with no replay. Use production local owner login into an exclusive0600 file only after raw parity; no token output. Runtime verification also creates/logout credentials and audit entries: +``` +"$TULDOK_QA_PYTHON" runtime_snapshots.py verify \ + --data "$TULDOK_QA_RESTORED" --manifest-sha256 "$TULDOK_QA_RUNTIME_SHA" +``` + +This must cold-verify the ACTUAL qualified snapshot at its new root without loading a model. Public owners must read exact completed checkpoint/results/summary and all scientific artifacts from restored paths; stream/hash full evaluation output once, preserving roles and evidence. Inspect representative scientific slice geometry, review/history and immutable release bytes. In the two-copy case the old source is absent, proving no fallback to historical paths. New preflight may prove current runtime root with a newly computed material digest; no model submission is needed. Any later neutral dispatch uses a fresh approval, never a replay of a historical attempt. + +## Actual-history and retained-fixture matrix + +Inventory exact real IDs; do not claim the scientific QA catalog contains every old modality. Required real history if present: P5a checkpoint comparison, P4/native generation, P5b reviewed training records/six releases/fit-validation-checkpoint, P5c evaluated checkpoint/full fields/custody, editable graphs/projects/runtime, actors/audit/admissions/grants/consumptions and imported/reviewed evaluation chunk. Mark any absent item explicitly. + +Qualify all legacy modalities using separate small fixtures rather than merging or multiplying multi-GB catalogs: `tests/test_workspace_metadata.py` (retained schemas/upgrade/rollback/restore), `tests/test_spatial_storage.py`, `tests/test_scientific_release.py` (neutral project+Run provenance restore), sequence v1/v2 `tests/test_sequences_v2.py`, `tests/test_sequence_runs.py`, `tests/test_sequence_v2_runs.py`, plus owned image/text/audio regressions. Existing `tests/qualify_spatial_retained_runs.py --legacy-root --report ` proves actual retained spatial Runs; its checker decides the required historical source, not a guessed current checkout. Retain release bytes, source bytes, reviews/revisions/history/families/splits and classification test-exposure refusal. Synthetic compatibility fixtures never qualify actual model acceptance. + +Schedule tests after evaluation under root ownership; no concurrent heavy suite now. Capture source hashes before/after authoritative verification. + +## Failure/recovery and authority switch + +- Backup failure: source DB/tree stay stopped and authoritative. Never delete source based on a partial transfer. Existing owner cleans only its private partial backup. +- No approval to remove original: retain it and report exact space requirement. +- Restore failure after approved source removal: preserve full backup, original DB and marker-bound staging/target DB. Same-target restore reconciles; no new import/merge/replay or ad hoc DROP SCHEMA. Unsupported crash reconciliation is a blocker, not apparent success. +- Commit acknowledgement unknown: preserve rows/files and inspect exact receipt/marker. No blind retry mutation or deletion. Restored destination with later writes cannot be overwritten. +- Unknown custody: restore does not stop processes. Do not edit state/boot IDs, clear fences or signal old PIDs. Same-boot active/unknown source blocks full quiesced qualification. Authored interrupted/queued restore cases belong to small lifecycle tests and must prove no spawned replay, not fictional successful cancellation. +- Runtime mode/hash mismatch: fail runtime acceptance; byte-readable completed artifacts are narrower evidence only. No posthoc chmod/recapture changing identities to force green. +- Finish with complete verified backup and restored catalog/evidence. Parent explicitly selects live DB/catalog and keeps old writers stopped. No further deletion follows automatically. + +## Final evidence + +Bounded report: source commit/tool versions, safe absolute paths and UUID (no secrets), actual disk phases, permission-preservation fix revision, table/sequence/file digest summaries, backup receipt SHA, optional cleanup receipt, cold actual runtime verification, completed Run/release/checkpoint/full-field digests, restored UI/read results and exact compatibility receipts/blockers. Store long inventories privately with hashes/counts; do not dump credentials or raw DB rows into chat. This plan writes only this /tmp document. + +## Fresh catalog permission initialization refinement + +Final review observed that Metadata creates `.tuldok.json` and +`.metadata-init.lock` before transfer. A private source manifest or a different +creation umask can therefore contradict the exact-mode transfer guard in a +valid new restore. Root admits a minimal owner change in `metadata.py`, +`backup_metadata.py` and `tests/test_workspace_metadata.py` to apply validated +source modes only to files exclusively created by this invocation. A resumed +marker-bound staging directory may initialize an absent manifest after Metadata +validates its existing pending reservation, identity and schema. Its existing +lock and all existing file modes remain untouched. +Creation identity must come from the creating owner; inspecting an existing +name after construction does not prove ownership. Refuse preexisting fresh marker/lock files and never chmod a substituted +file, hardlink or symlink. Keep the existing atomic manifest publication +behavior; this refinement adds no stronger same-owner publication guarantee. +Resumed staging and completed +restores retain strict mode contradiction refusal. A leftover exclusive +manifest temporary is refused; no general cleanup or overwrite is admitted +to turn ambiguous partial state into apparent success. No receipt, schema or +historical mode claim changes, and no actual restored catalog edits are +admitted. Qualify private 0400/0600 manifests, 077-to-022 umask changes, +interruption/retry and replaced-file refusal, then independent review. diff --git a/docs/plans/generic-scientific-projects/reports/p7-verification.md b/docs/plans/generic-scientific-projects/reports/p7-verification.md new file mode 100644 index 00000000..ba1d5d70 --- /dev/null +++ b/docs/plans/generic-scientific-projects/reports/p7-verification.md @@ -0,0 +1,150 @@ +# P7 actual backup and restore qualification + +Actual scientific catalog backup/restore passed on Python3.12.14 with the +permission-preserving transfer owner633b816. Final local compatibility refinement +and hosted regression remain separately recorded below. No deployment. + +## Exact retained state + +Source catalog UUID `d4856fe9eec74aea8b007cca029b3248`, schema8:31 SQL tables, +462 rows,21,537 files/5,281,350,158 logical bytes and23,392 tree entries. +All21 external attempts retain verified drained custody:17completed,3failed, +1cancelled. Table/schema/sequence/file/mode parity was established before any +Dataset/auth construction at the restored destination. + +[Final report](p7/final-report.json), SHA256 +`9f0cd5949539520b168a3087095c002b965540fc7e4c8cde3a7b942c8d69e85a`. +[Raw restore proof](p7/restore-raw.json), [independent backup oracle](p7/independent-backup-proof.json), +[committed separate-DB SQL probe](p7/probe-sql.json). + +The full backup remains at +`/tmp/tuldok-p7-scientific-restore-qualification/full-backup`; receipt SHA256 +`cea2a452a4c1478ab54ffd3866a85b7111da8b08ecdb9f603c0eb96483a1389c`. +Restored catalog: +`/workspace/scratch/tuldok-cpu-qualification/restored-catalog-20261013`. +Its private connection configuration is +`/tmp/tuldok-p7-scientific-restore-qualification/restore.env`; credentials are not +published. The original PostgreSQL database/schema remains unchanged. + +## Measured two-copy procedure + +Source+backup+restore coexistence was short2,723,254,272 bytes including512MiB +operational and128MiB additional DB/WAL reserves. After complete backup, +independent all-byte/mode proof and committed SQL probe, root independently +verified exact evidence and admitted disposition of only the task-created +source filesystem tree (device27/inode1210551). Fresh full source parity and +quiescence passed under the metadata lock before its exact removal. Original +database, verified backup, other catalogs, toolchains, owner worktrees and +failed/cancelled evidence retained in the backup were preserved. +[Exact admission](p7/source-disposition-admission.json), +[disposition result](p7/source-disposition-result.json), +[original DB proof](p7/final-original-database.json). + +The old source path is absent, so restored reads cannot fall back to it. +Historical opaque Run/spec/material/custody paths and hashes were not rewritten; +no historical attempt was resumed. Current inspection generates a descriptor +for the unchanged CPU manifest at the new root. + +## Cold owners and public UI + +The actual captured CPU snapshot +`cc54ef199fc192b70f702e85dda70bfc8697bf3220373ae9f031afd8b237f0a7` +verified at its restored root. All156 artifacts from17 completed Runs +(3,471,725,258 bytes) and all six immutable releases matched full hashes. +Typed scientific view/history remained intact; no Runs scheduler thread started. +[Cold qualification](p7/qualify.json). + +The production restored app was proven to use the distinct restore database +before owner login. Loaded Data pressure slices, saved Pipeline and completed +Run views passed at1440 and390 pixels. All89 evaluation artifacts +(3,086,837,711 bytes) matched the prior authenticated HTTP inventory. Graphs, +Run/record IDs and the reviewed record remained unchanged. Root visually +reviewed the loaded narrow slice and desktop results. +[Browser receipt](p7/browser-receipt.json), +[server DB scope](p7/browser-server-database-scope.json), +[loaded slice](p7/inspection-loaded-data-390.jpg), +[results](p7/inspection-summary-1440.jpg). + +The first browser attempt passed reads/HTTP but failed logout with400 because +the new QA request omitted its required JSON body. That attempt is retained. +The exact newly issued session was revoked through authenticated Access, and +the fresh cleanup session logged out. The corrected request passed in a new +browser attempt with verified200/logout and guaranteed child cleanup. All new +QA sessions are revoked; source DB and all non-auth state remain exact. +[Session cleanup](p7/failed-browser-session-cleanup.json), +[successful logout](p7/browser-logout.json). + +## Implementation and final code gates + +The existing owner preserves ordinary file modes, applies directories last and +refuses contradictory resumed/completed permissions. Private partial cleanup +uses retained descriptors, guards acquisition and refuses uncertain identity. +Version1 byte receipts remain unchanged and do not authenticate historical +modes; cold runtime verification is required. Canonical +[storage policy](../../../contracts/local-storage.md#backup-permission-preservation). + +The owned metadata/runtime modules passed37 tests before final cleanup +refinements; eight final affected checks passed9.081s. Final review found +a Python3.10 compatibility gap in exception diagnostics. It now uses +`sys.exc_info()[1]`, optional notes and an explicit CLI diagnostic preserving +original exception identity/arguments. Nine affected tests passed8.431s, +including legacy note capability and a tiny cold-runtime backup/restore. +At that compatibility-only checkpoint, copy/mode/receipt/restore functions +remained byte-identical. The later creation-mode refinement below changes +restore initialization separately. The API availability +is documented in [Python sys](https://docs.python.org/3.11/library/sys.html#sys.exception) +and [exceptions](https://docs.python.org/3.11/library/exceptions.html#BaseException.add_note). +[Compatibility receipt](p7/python310-compat-receipt.json). Tests ran on3.12; +3.10 grammar checks passed, but no3.10 interpreter execution is claimed. +Counts overlap. Independent final review found no concrete blocker. + +Fresh hosted lifecycle/UI on the final published branch remains the full +regression acceptance gate. Both main push triggers are enabled here and in +independent PR54 to verify integrated main directly. No Standards Engine +certificate or unconfigured type checker is claimed. + +This closes the actual scientific transfer/read gates within their stated +environment. Dataset-workflows A8 real positive model assistance and the +original legacy-v2 packet remain separate external blockers; authored +compatibility fixtures do not close their real-data/model acceptance. + +## Private manifest and creation-umask refinement + +A tiny independent reproduction found that Metadata creates its manifest and +initialization lock before file transfer. With a 0400/0600 source manifest or +a 077-to-022 creation-umask change, a valid restore could reach the strict copy +guard with newly generated modes that differed from the source. + +The refinement applies validated source modes while Metadata still holds the +file descriptors it exclusively created. It changes no existing file mode. +Restore no longer performs permission changes on names inspected after +Metadata construction. A fresh staging directory refuses preexisting +initialization files. A validated interrupted initialization may assign the +source mode to its newly created absent manifest; its existing lock is left +untouched. Resumed and completed mode contradictions still fail. Default +Metadata callers and atomic manifest publication retain their prior behavior. + +The first reviewed candidate applied modes after construction; review rejected +its replacement-file race before publication. The final owner design removes +that permission authority. Mid-publication ambiguity with a leftover exclusive +manifest temporary remains a safe refusal, requiring explicit owner recovery; +no temporary-file sweep or arbitrary overwrite is introduced. + +These tiny cases extend the transfer proof to private initialization modes. +The actual multi-gigabyte scientific restore remains evidence for its stated +owner revision and ordinary catalog initialization modes, with final affected +checks and hosted regression recorded separately. + +Final affected regression passed 38 tests in 228.276 seconds with zero skips: +the complete 37-test metadata module plus cold captured-runtime backup/restore. +All three source/test hashes remained unchanged. Independent final review, +Python 3.10 grammar/current-interpreter compilation, plan validation and +`git diff --check` passed. Actual tests used Python 3.12.14. +[Final mode receipt](p7/final-mode-receipt.json), SHA256 +`9de3696ddc47af73475cae2b3aea4f894c7cc719bf476716e78c404b7d7f50eb`. +Only `restore_catalog`, `_publish_manifest`, `Metadata.__init__` and +`Metadata._initialize` changed from the prior P7 head; backup/copy/receipt +functions remained unchanged. No actual catalog, backup or installed package +was modified during these final checks. Fresh hosted lifecycle/UI checks on +the published refinement remain pending. The original v2 packet remains +required for the plan's remaining G7 acceptance. diff --git a/docs/plans/generic-scientific-projects/reports/p7/backup.json b/docs/plans/generic-scientific-projects/reports/p7/backup.json new file mode 100644 index 00000000..6dcbdf9b --- /dev/null +++ b/docs/plans/generic-scientific-projects/reports/p7/backup.json @@ -0,0 +1 @@ +{"backup":{"files_sha256":"3bfd920ba06272d7d91393f16559cc85533c059cbc1e1ba9734de3fcf72861ce","metadata_sha256":"30de2587989cf9d4fa7530f67b1d898a0e132096ca2fd020635a31d8b476c5dd","receipt_digest":"4a42e5b0b6ff4d1323c9563caaa3331dd01d84f58d6b85d4027bd80baeab144b","receipt_sha256":"cea2a452a4c1478ab54ffd3866a85b7111da8b08ecdb9f603c0eb96483a1389c"},"baseline_sha256":"9ddac55275f6022e1d6c138822b3f3f27eb8cbf37a3df0eee294a2b3777cbddb","completed_unix":1791930356.9585092,"format":"tuldok-scientific-restore-qa-v1","free_after":3298738176,"free_before":3312553984,"phase":"backup","python":"3.12.14","required_transfer_free_bytes":5828496637,"source":"/workspace/scratch/tuldok-cpu-qualification/qualified-catalog","started_unix":1791930296.8165038,"status":"passed"} diff --git a/docs/plans/generic-scientific-projects/reports/p7/browser-logout.json b/docs/plans/generic-scientific-projects/reports/p7/browser-logout.json new file mode 100644 index 00000000..238ca9f4 --- /dev/null +++ b/docs/plans/generic-scientific-projects/reports/p7/browser-logout.json @@ -0,0 +1,5 @@ +{ + "attempted": true, + "verified": true, + "http_status": 200 +} \ No newline at end of file diff --git a/docs/plans/generic-scientific-projects/reports/p7/browser-receipt.json b/docs/plans/generic-scientific-projects/reports/p7/browser-receipt.json new file mode 100644 index 00000000..0e57b73c --- /dev/null +++ b/docs/plans/generic-scientific-projects/reports/p7/browser-receipt.json @@ -0,0 +1,95 @@ +{ + "qualification": "Actual relocated restored catalog: read-only Data/Pipeline/Run UI and all89 authenticated HTTP artifact hashes; no model/import/review/graph mutation", + "source_commit": "53bdce1bd2f7875928104717b4a4579843f91457", + "restored_catalog": "/workspace/scratch/tuldok-cpu-qualification/restored-catalog-20261013", + "prior_evidence": "/workspace/Tuldok-p5c/build/qa/workspace-rheon-evaluation/run-hWVrH7", + "graphs_exactly_unchanged": true, + "http_artifacts": 89, + "http_bytes": 3086837711, + "prior_http_inventory_exact_match": true, + "run_id": "e731c9569b7d41f9a8f6cde438dcf9ac", + "record_id": "9df813a605d746709eb0c245f16f4033", + "run_ids_unchanged": [ + "0cc3ef6b5f9e460f873c8eda1ba3d525", + "11fc2be1aada490f9b67c11c190728f6", + "28a3dd42f3e64fab89136da1d7e5d8a0", + "3a0e69f8e71c4c7995d21c8157edc230", + "44272474e32544898feb39f651d997b9", + "48dbf653056a4d45ae53c6842e7d3aa3", + "4a9a129e8405405ab12b866edd766e48", + "80ad7f8474ae4e9384483b9463206ab2", + "8a22050ad7fc496db53e67d091de062a", + "8d2985a122b644fa96f3bafd3146edf5", + "a8298e4e255a46d4beef0ec9f73e38d0", + "ab46bb3b6e2944a0a0794776da0352f7", + "c3f7594d311843ea8487427556c84810", + "cea0a01e08c4488cbdba92f1662c7020", + "cf94711d8a2047c08a64c0219f474017", + "d1700cfb1fb249e3b627a9cb1daadcec", + "df53534ae6154ec8a4788f8378a8f004", + "e46bc5a79331428580dba01bb671d25b", + "e731c9569b7d41f9a8f6cde438dcf9ac", + "ecd6685cbd034a2e9c4c870b68ddd3a4", + "ece8abe0a4024d1483048d91b7980cf0" + ], + "record_ids_unchanged": [ + "10248a96cd884c97aba789c46f5b30e8", + "2965e02ece8e46b3b841dc078abaef77", + "3033b32664e64095ab011f44bcc730b6", + "3bd7a890512e4a678e6989829deb366e", + "68a4b8417a8e4334b97ba309f9fd7c69", + "6a0ff276b3464a9d81c2c7c791f5179f", + "9189ed47d88a4bee8dd4f65b7e767b60", + "9a12007586654e9190df16a9a6e0c3b2", + "9df813a605d746709eb0c245f16f4033", + "a361b032e4474d5bac7f28201ab6a2bf", + "a6d37093fda84b86a4416e17337103e5", + "b49013cfdbc7467bb2f87cabb67e5d1a", + "d7485bc75f45470c84a2fab66e2788df" + ], + "record_exactly_unchanged": true, + "captures": [ + { + "file": "restored-pipeline-1440.jpg", + "width": 1440, + "height": 900, + "qualification": "Read-only restored catalog; no new execution or scientific acceptance" + }, + { + "file": "restored-pipeline-390.jpg", + "width": 390, + "height": 900, + "qualification": "Read-only restored catalog; no new execution or scientific acceptance" + }, + { + "file": "inspection-summary-1440.jpg", + "width": 1440, + "height": 900, + "qualification": "Read-only restored catalog; no new execution or scientific acceptance" + }, + { + "file": "inspection-summary-390.jpg", + "width": 390, + "height": 900, + "qualification": "Read-only restored catalog; no new execution or scientific acceptance" + }, + { + "file": "inspection-loaded-data-1440.jpg", + "width": 1440, + "height": 900, + "status": "Frame 0 · time 0.016666666666666666 native time units (unspecified) · cell · = 512*1024*1024, 'Keep at least 512 MiB operational reserve plus separately measured DB/WAL headroom.') + require(args.quiesced, 'Every phase requires the explicit stopped-writers assertion --quiesced.') + paths = [source, args.report.absolute(), args.baseline.absolute()] + if args.backup: paths.append(args.backup.resolve()) + if args.destination: paths.append(args.destination.resolve()) + if args.probe_path: paths.append(args.probe_path.resolve()) + transfer.separate_paths(*paths) + require(not args.report.exists(), 'Use a new report path for every attempt.') + report_authorized = True + report['free_before'] = shutil.disk_usage(source.parent).free + if args.phase == 'inventory': + state = snapshot(source, dsn(args.source_dsn_env)) + state.update(format='tuldok-scientific-restore-baseline-v1', source=str(source)) + save_private(args.baseline, state) + report['raw'] = compact(state) + else: + state = baseline(args.baseline) + require(state['source'] == str(source), 'Explicit source differs from baseline.') + require(args.backup is not None, 'This phase requires --backup.') + if args.phase == 'backup': + compare(snapshot(source, dsn(args.source_dsn_env)), state) + require(args.backup.parent.exists(), 'Create the approved backup parent before transfer.') + required = sum(value.get('bytes',0) for value in state['files'].values()) + state['packed_table_bytes'] + args.reserve_bytes + require(shutil.disk_usage(args.backup.parent).free >= required, 'Measured free space cannot hold backup plus reserve.') + report['required_transfer_free_bytes'] = required + transfer.backup_catalog(source, args.backup, dsn=dsn(args.source_dsn_env), quiesced=True) + compare(snapshot(source, dsn(args.source_dsn_env)), state) + report['backup'] = verify_backup(args.backup, state) + elif args.phase == 'verify-backup': + report['backup'] = verify_backup(args.backup, state) + elif args.phase == 'probe-sql': + require(args.probe_path is not None, 'SQL probe requires --probe-path.') + report['backup'] = verify_backup(args.backup, state) + report['sql_probe'] = sql_probe(args.backup, args.probe_path, state, + dsn(args.probe_dsn_env), dsn(args.restore_dsn_env)) + else: + require(args.destination is not None, 'This phase requires --destination.') + require(source.exists() or args.allow_absent_source, 'Source is absent; exact separately approved fallback must be explicit.') + report['source_filesystem_present'] = source.exists() + report['backup'] = verify_backup(args.backup, state) + connection = dsn(args.restore_dsn_env) + # Connection-only identity check before restore can create schema. + import psycopg + with psycopg.connect(connection, autocommit=True) as probe: + class ConnectionOnly: pass + holder = ConnectionOnly(); holder.connection = probe + require(database_identity(holder) != state['database_identity_sha256'], 'Restore database must be separate from the original.') + if args.phase == 'restore-raw': + require(args.destination.parent.exists(), 'Create the approved restore parent before transfer.') + required = sum(value.get('bytes',0) for value in state['files'].values()) + args.reserve_bytes + require(shutil.disk_usage(args.destination.parent).free >= required, 'Measured free space cannot hold restore plus reserve.') + report['required_transfer_free_bytes'] = required + transfer.restore_catalog(args.backup, args.destination, dsn=connection) + proof, restored = raw_restored(args.backup, args.destination, state, connection) + report.update(backup=proof, destination=str(args.destination.resolve()), raw=compact(restored), raw_parity_before_dataset_and_auth=True) + if args.phase == 'qualify': + roots = {} + for binding in args.project_root: + alias, separator, location = binding.partition('=') + require(separator and alias and location and alias not in roots, 'Supply unique explicit project root aliases.') + roots[alias] = location + report['reads'] = qualified_reads(args.destination, connection, state, roots) + marker = '.restore-receipt-' + proof['receipt_digest'] + '.json' + after = snapshot(args.destination, connection, exclude=(marker,)) + changed = [name for name in state['tables'] if state['tables'][name] != after['tables'][name]] + require(set(changed) <= {'access_audit', 'access_credentials'}, 'Read qualification changed non-auth historical tables.') + require(after['run_states'] == state['run_states'] and after['sequences'] == state['sequences'], 'Qualification changed historical Runs or identity sequences.') + require(after['files'] == state['files'] and after['modes'] == state['modes'], 'Qualification changed retained artifact bytes/modes.') + report['post_auth_changed_tables'] = changed + report['backup_after'] = verify_backup(args.backup, state) + report.update(status='passed', free_after=shutil.disk_usage(source.parent).free, completed_unix=time.time(), + baseline_sha256=transfer.file_hash(args.baseline)) + except Exception as error: + # Metadata errors can include SQL values/connection details. Do not leak + # them into stdout or a public report; preserve only a safe failure class. + report['failure_type'] = type(error).__name__ + if isinstance(error, transfer.StorageTransferError): report['failure'] = str(error) + if report_authorized: + save_private(args.report, report) + print(json.dumps({'status':'failed','phase':args.phase,'report':str(args.report),'failure_type':type(error).__name__})) + return 1 + save_private(args.report, report) + print(json.dumps({'status':'passed','phase':args.phase,'report':str(args.report), + 'report_sha256':transfer.file_hash(args.report),'dataset_id':state['dataset_id']})) + return 0 + + +if __name__ == '__main__': + raise SystemExit(main()) diff --git a/tests/test_runtime_snapshots.py b/tests/test_runtime_snapshots.py index 7af5f1d9..adfc8c9c 100644 --- a/tests/test_runtime_snapshots.py +++ b/tests/test_runtime_snapshots.py @@ -53,6 +53,35 @@ def test_exact_capture_survives_source_removal_and_reuses_without_overwrite(self self.assertEqual((root/'packages/torch/__init__.py').stat().st_mode&0o777,0o400) self.assertFalse(list(owner.path.glob('.capture-*'))) + def test_backup_restore_keeps_cold_runtime_modes_and_refuses_changed_completed_modes(self): + from backup_metadata import backup_catalog, restore_catalog, StorageTransferError + db, access, owner, config = self.fixture() + identity = owner.inspect(config)['inventory_sha256'] + value = owner.create(config, identity, quiesced=True, reserve_bytes=0) + manifest = (Path(value['root']) / 'manifest.json').read_bytes() + backup = self.root / 'backup-runtime' + backup_catalog(db.path, backup, quiesced=True) + metadata_tests.WorkspaceMetadata.drop(self, db) + restored = self.root / 'restored-runtime' + restore_catalog(backup, restored) + restore_catalog(backup, restored) + restored_executable = restored / Path(value['root']).relative_to(db.path) / 'python/bin/python3.12' + restored_executable.chmod(0o400) + with self.assertRaises(StorageTransferError): + restore_catalog(backup, restored) + restored_executable.chmod(0o500) + current = Metadata(restored, create=False); self.addCleanup(current.close) + restored_access = Access(current, restored) + token = restored_access.exchange(restored_access._local_owner_token())['token'] + with restored_access.bind(token): + actual = RuntimeSnapshots(current, restored_access, restored).get(identity) + self.assertEqual((Path(actual['root']) / 'manifest.json').read_bytes(), manifest) + executable = Path(actual['root']) / 'python/bin/python3.12' + self.assertEqual(executable.stat().st_mode & 0o777, 0o500) + executable.chmod(0o400) + with self.assertRaises(StorageTransferError): + restore_catalog(backup, restored) + def test_unicode_nested_path_uses_shared_canonical_bytes(self): db,access,owner,config=self.fixture() (Path(config['site_packages'])/'torch'/'méta.txt').write_text('unicode entry') diff --git a/tests/test_workspace_metadata.py b/tests/test_workspace_metadata.py index f380330c..993de68d 100644 --- a/tests/test_workspace_metadata.py +++ b/tests/test_workspace_metadata.py @@ -1,5 +1,5 @@ """Real PostgreSQL version evolution and lossless retained backup contracts.""" -from contextlib import contextmanager +from contextlib import contextmanager, redirect_stderr import io import json import os @@ -379,6 +379,284 @@ def transaction(*inner_args, **inner_kwargs): self.assertEqual(reopened.schema_version, 8) self.assertEqual(upgrade_catalog(metadata.path, quiesced=True)['status'], 'already_current') + def test_failed_backup_cleans_its_read_only_partial_without_changing_source(self): + from backup_metadata import tree_modes + metadata = self.catalog(version=8) + nested = metadata.path / 'readonly'; nested.mkdir() + (nested / 'data').write_bytes(b'fixture'); (nested / 'data').chmod(0o400) + nested.chmod(0o500) + before = tree_manifest(metadata.path); modes = tree_modes(metadata.path, before) + backup = self.root / 'failed-backup' + with patch('backup_metadata.write_json', side_effect=OSError('post-copy injected failure')): + with self.assertRaisesRegex(OSError, 'post-copy injected failure'): + backup_catalog(metadata.path, backup, quiesced=True) + self.assertFalse(backup.exists()) + self.assertFalse(list(self.root.glob('failed-backup.partial-*'))) + self.assertEqual(tree_manifest(metadata.path), before) + self.assertEqual(tree_modes(metadata.path, before), modes) + nested.chmod(0o700) + + def test_uncertain_partial_cleanup_preserves_original_error_and_external_modes(self): + metadata = self.catalog(version=8) + external = self.root / 'external'; external.mkdir(); external.chmod(0o500) + backup = self.root / 'uncertain-backup' + def replace(path, value): + partial = Path(path).parent + (partial / 'catalog').rename(partial / 'retained-catalog') + (partial / 'catalog').symlink_to(external, target_is_directory=True) + raise OSError('original post-copy failure') + with patch('backup_metadata.write_json', side_effect=replace): + with self.assertRaisesRegex(OSError, 'original post-copy failure') as caught: + backup_catalog(metadata.path, backup, quiesced=True) + self.assertIn('cleanup refused', caught.exception.backup_cleanup_diagnostic) + self.assertEqual(external.stat().st_mode & 0o777, 0o500) + self.assertEqual(len(list(self.root.glob('uncertain-backup.partial-*'))), 1) + external.chmod(0o700) + + def test_backup_cleanup_never_deletes_replacement_root_contents(self): + import backup_metadata + metadata = self.catalog(version=8) + backup = self.root / 'replaced-backup' + original = backup_metadata._remove_partial_contents + replacement = [] + def replace(descriptor): + partial = next(self.root.glob('replaced-backup.partial-*')) + partial.rename(self.root / 'retained-owned-partial') + partial.mkdir(); (partial / 'unrelated').write_bytes(b'preserve') + replacement.append(partial) + # Only intercept the root call; recursion uses the genuine implementation. + with patch('backup_metadata._remove_partial_contents', original): + original(descriptor) + with patch('backup_metadata.write_json', side_effect=OSError('original failure')): + with patch('backup_metadata._remove_partial_contents', side_effect=replace): + with self.assertRaisesRegex(OSError, 'original failure') as caught: + backup_catalog(metadata.path, backup, quiesced=True) + self.assertEqual((replacement[0] / 'unrelated').read_bytes(), b'preserve') + self.assertIn('identity changed', caught.exception.backup_cleanup_diagnostic) + + def test_cleanup_diagnostic_preserves_original_error_without_note_capability_and_reaches_cli(self): + import backup_metadata + class LegacyOSError(OSError): + add_note = None # Python 3.10 exceptions have no note capability. + metadata = self.catalog(version=8) + before = tree_manifest(metadata.path) + for error_type in (OSError, LegacyOSError): + with self.subTest(error_type=error_type.__name__): + backup = self.root / ('diagnostic-' + error_type.__name__) + original_error = error_type('original post-copy failure') + original_args = original_error.args + with patch('backup_metadata.write_json', side_effect=original_error), \ + patch('backup_metadata._remove_partial_contents', + side_effect=backup_metadata.StorageTransferError('injected custody refusal')): + with self.assertRaises(error_type) as caught: + backup_catalog(metadata.path, backup, quiesced=True) + self.assertIs(caught.exception, original_error) + self.assertEqual(original_error.args, original_args) + self.assertEqual(str(original_error), 'original post-copy failure') + partials = list(self.root.glob(backup.name + '.partial-*')) + self.assertEqual(len(partials), 1) + diagnostic = original_error.backup_cleanup_diagnostic + self.assertIn(str(partials[0]), diagnostic) + self.assertIn('injected custody refusal', diagnostic) + if callable(getattr(original_error, 'add_note', None)): + self.assertIn(diagnostic, original_error.__notes__) + else: + self.assertFalse(hasattr(original_error, '__notes__')) + stderr = io.StringIO() + with patch.object(sys, 'argv', ['backup_metadata.py', 'backup', + str(metadata.path), str(backup), '--quiesced']), \ + patch('backup_metadata.backup_catalog', side_effect=original_error), \ + redirect_stderr(stderr): + with self.assertRaises(SystemExit) as exited: + backup_metadata.main() + self.assertEqual(exited.exception.code, 1) + self.assertEqual(stderr.getvalue(), str(original_error) + '\n' + diagnostic + '\n') + self.assertEqual(original_error.args, original_args) + self.assertEqual(tree_manifest(metadata.path), before) + + def test_backup_partial_descriptor_acquisition_failure_closes_parent(self): + metadata = self.catalog(version=8) + original = os.open; parents = [] + def fail(name, flags, *args, **kwargs): + if isinstance(name, str) and name.startswith('open-failure.partial-'): + parents.append(kwargs['dir_fd']) + raise OSError('injected descriptor acquisition failure') + return original(name, flags, *args, **kwargs) + with patch('backup_metadata.os.open', side_effect=fail): + with self.assertRaisesRegex(OSError, 'descriptor acquisition failure'): + backup_catalog(metadata.path, self.root / 'open-failure', quiesced=True) + self.assertEqual(len(parents), 1) + with self.assertRaises(OSError): + os.fstat(parents[0]) + + def test_private_manifest_restore_preserves_mode_and_refuses_changed_completed_mode(self): + from backup_metadata import StorageTransferError, tree_modes + previous_umask = os.umask(0o022) + try: + for mode in (0o400, 0o600): + with self.subTest(mode=oct(mode)): + metadata = self.catalog(name='private-' + oct(mode), version=8) + (metadata.path / '.tuldok.json').chmod(mode) + backup = self.root / ('backup-' + oct(mode)) + receipt = backup_catalog(metadata.path, backup, quiesced=True) + before = tree_manifest(backup) + self.drop(metadata) + destination = self.root / ('restored-' + oct(mode)) + result = restore_catalog(backup, destination) + self.assertEqual(restore_catalog(backup, destination), result) + self.assertEqual(tree_modes(destination, receipt['files']), + tree_modes(backup / 'catalog', receipt['files'])) + manifest = destination / '.tuldok.json' + manifest.chmod(0o644) + with self.assertRaisesRegex(StorageTransferError, 'later writes'): + restore_catalog(backup, destination) + self.assertEqual(manifest.stat().st_mode & 0o777, 0o644) + self.assertEqual(tree_manifest(backup), before) + finally: + os.umask(previous_umask) + + def test_private_manifest_restore_retry_preserves_identity_and_refuses_changed_staging_mode(self): + import backup_metadata + previous_umask = os.umask(0o077) + try: + metadata = self.catalog(version=8) + (metadata.path / '.tuldok.json').chmod(0o400) + backup = self.root / 'private-retry-backup' + backup_catalog(metadata.path, backup, quiesced=True) + before = tree_manifest(backup) + self.drop(metadata) + os.umask(0o022) + destination = self.root / 'private-retry-restored' + with patch('backup_metadata.copy_tree', side_effect=OSError('injected pre-copy interruption')): + with self.assertRaisesRegex(OSError, 'pre-copy interruption'): + restore_catalog(backup, destination) + staging = next(self.root.glob(destination.name + '.restore-*')) + manifest = staging / '.tuldok.json' + marker_bytes = manifest.read_bytes() + self.assertEqual(manifest.stat().st_mode & 0o777, 0o400) + lock = staging / '.metadata-init.lock' + self.assertEqual(lock.stat().st_mode & 0o777, 0o600) + lock.chmod(0o644) + with self.assertRaisesRegex(backup_metadata.StorageTransferError, 'permissions differ'): + restore_catalog(backup, destination) + self.assertEqual(lock.stat().st_mode & 0o777, 0o644) + lock.chmod(0o600) # Fixture owner explicitly restores the admitted mode. + manifest.chmod(0o600) + with self.assertRaisesRegex(backup_metadata.StorageTransferError, 'permissions differ'): + restore_catalog(backup, destination) + self.assertEqual(manifest.stat().st_mode & 0o777, 0o600) + self.assertEqual(manifest.read_bytes(), marker_bytes) + manifest.chmod(0o400) # Fixture owner explicitly restores the admitted mode. + result = restore_catalog(backup, destination) + self.assertEqual(restore_catalog(backup, destination), result) + self.assertEqual((destination / '.tuldok.json').read_bytes(), marker_bytes) + self.assertEqual((destination / '.tuldok.json').stat().st_mode & 0o777, 0o400) + self.assertEqual((destination / '.metadata-init.lock').stat().st_mode & 0o777, 0o600) + self.assertEqual(tree_manifest(backup), before) + finally: + os.umask(previous_umask) + + def test_new_restore_never_repairs_same_owner_files_replaced_after_metadata_creation(self): + from backup_metadata import StorageTransferError + for name in ('.tuldok.json', '.metadata-init.lock'): + with self.subTest(name=name): + metadata = self.catalog(name='replace-' + name, version=8) + backup = self.root / ('replace-backup-' + name) + backup_catalog(metadata.path, backup, quiesced=True) + self.drop(metadata) + replaced = [] + def replace(path, *args, **kwargs): + current = Metadata(path, *args, **kwargs) + target = Path(path) / name + value = target.read_bytes() + target.unlink(); target.write_bytes(value); target.chmod(0o400) + replaced.append((target, value, target.stat().st_ino)) + return current + with patch('metadata.Metadata', side_effect=replace): + with self.assertRaisesRegex(StorageTransferError, 'permissions differ'): + restore_catalog(backup, self.root / ('replace-restored-' + name)) + target, value, inode = replaced[0] + self.assertEqual(target.stat().st_mode & 0o777, 0o400) + self.assertEqual(target.stat().st_ino, inode) + self.assertEqual(target.read_bytes(), value) + + def test_fresh_restore_refuses_preexisting_catalog_initialization_files(self): + import backup_metadata + for name in ('.tuldok.json', '.metadata-init.lock'): + with self.subTest(name=name): + metadata = self.catalog(name='preexisting-' + name, version=8) + backup = self.root / ('preexisting-backup-' + name) + backup_catalog(metadata.path, backup, quiesced=True) + self.drop(metadata) + original = backup_metadata.write_json + foreign = [] + def create(path, value): + original(path, value) + target = Path(path).parent / name + target.write_bytes(b'preserve preexisting file'); target.chmod(0o400) + foreign.append(target) + with patch('backup_metadata.write_json', side_effect=create): + with self.assertRaisesRegex((StorageError, backup_metadata.StorageTransferError), 'already exist'): + restore_catalog(backup, self.root / ('preexisting-restored-' + name)) + self.assertEqual(foreign[0].stat().st_mode & 0o777, 0o400) + self.assertEqual(foreign[0].read_bytes(), b'preserve preexisting file') + foreign[0].unlink() # The fixture's intentionally invalid manifest is not a catalog. + + def test_private_pending_restore_creates_missing_manifest_without_changing_existing_lock(self): + from backup_metadata import StorageTransferError, digest + previous_umask = os.umask(0o077) + try: + metadata = self.catalog(version=8) + (metadata.path / '.tuldok.json').chmod(0o400) + backup = self.root / 'pending-private-backup' + receipt = backup_catalog(metadata.path, backup, quiesced=True) + self.drop(metadata); os.umask(0o022) + destination = self.root / 'pending-private-restored' + with patch('metadata._publish_manifest', side_effect=OSError('injected early publication interruption')): + with self.assertRaisesRegex(OSError, 'early publication interruption'): + restore_catalog(backup, destination) + staging = next(self.root.glob(destination.name + '.restore-*')) + self.assertFalse(os.path.lexists(staging / '.tuldok.json')) + lock = staging / '.metadata-init.lock' + identity = lock.stat().st_ino + lock.chmod(0o644) + with self.assertRaisesRegex(StorageTransferError, 'lock bytes or permissions differ'): + restore_catalog(backup, destination) + self.assertEqual(lock.stat().st_mode & 0o777, 0o644) + lock.chmod(0o600) # Fixture owner restores the mode; the tool never repairs it. + result = restore_catalog(backup, destination) + self.assertEqual(restore_catalog(backup, destination), result) + self.assertEqual((destination / '.metadata-init.lock').stat().st_ino, identity) + self.assertEqual((destination / '.metadata-init.lock').stat().st_mode & 0o777, 0o600) + self.assertEqual((destination / '.tuldok.json').stat().st_mode & 0o777, 0o400) + self.assertEqual(tree_manifest(destination, ('.restore-receipt-' + digest(receipt) + '.json',)), + receipt['files']) + finally: + os.umask(previous_umask) + + def test_private_pending_restore_preserves_stale_exclusive_manifest_temporary(self): + metadata = self.catalog(version=8) + (metadata.path / '.tuldok.json').chmod(0o400) + backup = self.root / 'stale-temporary-backup' + backup_catalog(metadata.path, backup, quiesced=True) + self.drop(metadata) + destination = self.root / 'stale-temporary-restored' + original = os.replace + def interrupt(source, target): + if Path(target).name == '.tuldok.json': + raise OSError('injected pre-rename interruption') + return original(source, target) + with patch('metadata.os.replace', side_effect=interrupt): + with self.assertRaisesRegex(OSError, 'pre-rename interruption'): + restore_catalog(backup, destination) + staging = next(self.root.glob(destination.name + '.restore-*')) + temporary = staging / '.tuldok.json.tmp' + before = (temporary.read_bytes(), temporary.stat().st_mode, temporary.stat().st_ino) + with self.assertRaises(FileExistsError): + restore_catalog(backup, destination) + self.assertEqual((temporary.read_bytes(), temporary.stat().st_mode, temporary.stat().st_ino), before) + self.assertFalse(os.path.lexists(staging / '.tuldok.json')) + def test_v1_backup_restores_exact_v1_and_can_explicitly_upgrade_with_backup_unchanged(self): metadata = self.historical_catalog() rows = summaries(read_catalog(metadata)) @@ -619,3 +897,71 @@ def test_upgrade_cli_reopens_catalog_and_never_runs_on_ordinary_startup(self): if __name__ == '__main__': unittest.main() + + +class TransferPermissions(unittest.TestCase): + def test_read_only_tree_and_retry_preserve_modes_and_byte_manifest(self): + from backup_metadata import copy_tree, tree_modes, StorageTransferError + with tempfile.TemporaryDirectory() as temporary: + source = Path(temporary) / 'source'; target = Path(temporary) / 'target' + (source / 'nested').mkdir(parents=True) + (source / 'nested/program').write_bytes(b'fixture executable') + (source / 'data').write_bytes(b'fixture data') + (source / 'nested/program').chmod(0o500); (source / 'data').chmod(0o400) + (source / 'nested').chmod(0o500); source.chmod(0o500) + expected = tree_manifest(source) + copy_tree(source, target, expected) + self.assertEqual(tree_manifest(target), expected) + self.assertEqual(tree_modes(target, expected), tree_modes(source, expected)) + self.assertEqual((target / 'data').stat().st_nlink, 1) + copy_tree(source, target, expected) + (target / 'nested').chmod(0o550) + with self.assertRaisesRegex(StorageTransferError, 'directory permissions differ'): + copy_tree(source, target, expected) + (target / 'nested').chmod(0o500) + (target / 'data').chmod(0o600) + with self.assertRaisesRegex(StorageTransferError, 'permissions differ'): + copy_tree(source, target, expected) + target.chmod(0o700) + (target / 'data').unlink() + target.chmod(0o500) + with self.assertRaisesRegex(StorageTransferError, 'Read-only staging'): + copy_tree(source, target, expected) + for root in (source, target): + root.chmod(0o700); (root / 'nested').chmod(0o700) + + def test_partial_cleanup_cannot_follow_replaced_intermediate_directory(self): + from backup_metadata import _remove_partial_contents + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) / 'partial'; root.mkdir() + nested = root / 'catalog'; nested.mkdir() + external = Path(temporary) / 'external'; external.mkdir(); external.chmod(0o500) + descriptor = os.open(root, os.O_RDONLY | os.O_DIRECTORY) + original = os.open + def replace(name, flags, *args, **kwargs): + if name == 'catalog' and kwargs.get('dir_fd') == descriptor: + nested.rename(root / 'retained-catalog') + nested.symlink_to(external, target_is_directory=True) + return original(name, flags, *args, **kwargs) + try: + with patch('backup_metadata.os.open', side_effect=replace): + with self.assertRaises(OSError): + _remove_partial_contents(descriptor) + self.assertEqual(external.stat().st_mode & 0o777, 0o500) + self.assertTrue((root / 'retained-catalog').is_dir()) + finally: + os.close(descriptor); external.chmod(0o700) + + def test_source_mode_change_during_copy_refused(self): + from backup_metadata import copy_tree, StorageTransferError + with tempfile.TemporaryDirectory() as temporary: + source = Path(temporary) / 'source'; source.mkdir() + payload = source / 'payload'; payload.write_bytes(b'fixture'); payload.chmod(0o400) + expected = tree_manifest(source) + original = shutil.copyfileobj + def change(reader, writer): + original(reader, writer); payload.chmod(0o600) + with patch('backup_metadata.shutil.copyfileobj', side_effect=change): + with self.assertRaisesRegex(StorageTransferError, 'Source changed'): + copy_tree(source, Path(temporary) / 'target', expected) + self.assertFalse((Path(temporary) / 'target/payload').exists()) diff --git a/tests/workspace_scientific_restore.cjs b/tests/workspace_scientific_restore.cjs new file mode 100644 index 00000000..4493a8b8 --- /dev/null +++ b/tests/workspace_scientific_restore.cjs @@ -0,0 +1,133 @@ +'use strict'; +// Read-only restored-catalog UI and authenticated artifact-byte qualification. +// Opens existing records/graphs/Runs; never imports, reviews, edits, or executes models. +// Run only after raw restore parity is recorded; owner login appends access audit rows. +const fixtureAuth=require('./auth_helpers.cjs').authenticatedFixture(),fetch=fixtureAuth.fetch; +const assert=require('node:assert/strict'),fs=require('node:fs'),os=require('node:os'),path=require('node:path'),crypto=require('node:crypto'); +const {spawn,execFileSync}=require('node:child_process'); +const {waitForDebugger}=require('./browser_startup.cjs'); +const {qaDirectory,screenshotOptions}=require('./qa_artifacts.cjs'); +const root=path.resolve(__dirname,'..'); +function option(name){const index=process.argv.indexOf(name);return index<0?undefined:process.argv[index+1];} +const catalog=option('--catalog'),projectRoot=option('--project'),evidence=option('--evidence'); +assert.ok(catalog&&projectRoot&&evidence,'Explicit --catalog restored path, --project root and --evidence prior qualification directory required'); +assert.ok(process.argv.includes('--restore-qualified'),'Run only after root confirms completed raw restore verification and private restore DSN'); +const prior=JSON.parse(fs.readFileSync(path.join(evidence,'receipt.json'))); +const expectedInventory=JSON.parse(fs.readFileSync(path.join(evidence,'completed-http-inventory.json'))); +assert.match(prior.run_id||'',/^[a-f0-9]{32}$/);assert.match(prior.record_id||'',/^[a-f0-9]{32}$/); +assert.equal(expectedInventory.length,89); +const explicitReport=option('--report'); +if(explicitReport)fs.mkdirSync(explicitReport,{recursive:false}); +const report=explicitReport||qaDirectory(root,'workspace-scientific-restore'); +const temporary=fs.mkdtempSync(path.join(os.tmpdir(),'tuldok-restored-scientific-browser-')); +const captures=[],errors=[],children=[];const pause=ms=>new Promise(resolve=>setTimeout(resolve,ms));let ws,send,evaluate,base,authRegistered=false,serverErrors=''; +async function until(fn,message='Timed out',seconds=60){for(let i=0;i['-u','app.py','--port',String(port),'--data',catalog,'--project-root','sample='+projectRoot]; +(async()=>{ + const serverEnv={...process.env,PGAPPNAME:'tuldok-p7-restore-browser-'+process.pid}; + const server=launch('python3',serverArgs(0),{cwd:root,env:serverEnv,stdio:['ignore','pipe','pipe']}); + let output='';server.stdout.on('data',chunk=>output+=chunk);server.stderr.on('data',chunk=>serverErrors+=chunk); + const port=await until(()=>{if(server.exitCode!==null)throw Error('Project server exited: '+serverErrors);return output.match(/127\.0\.0\.1:(\d+)/)?.[1];}); + base='http://127.0.0.1:'+port; + const scopeCode=`import os,sys,json +from pathlib import Path +import psycopg +from metadata import Metadata +from backup_metadata import digest +def identity(connection): + info=connection.info + return digest({'host':info.host,'port':info.port,'dbname':info.dbname}) +metadata=Metadata(sys.argv[1],dsn=os.environ['TULDOK_DATABASE_URL'],create=False,schema_version=None) +try: + with psycopg.connect(os.environ['TULDOK_RESTORE_DATABASE_URL'],autocommit=True) as expected: + assert identity(metadata.connection)==identity(expected), 'Effective browser DSN differs from admitted restore DSN' + sessions=expected.execute('SELECT datname,count(*) FROM pg_stat_activity WHERE application_name=%s AND pid<>pg_backend_pid() AND pid<>%s GROUP BY datname',(os.environ['PGAPPNAME'],metadata.connection.info.backend_pid)).fetchall() + assert sessions and all(row[0]==expected.info.dbname for row in sessions), 'Actual browser app sessions differ from admitted restore database' + print(json.dumps({'catalog':str(Path(sys.argv[1]).resolve()),'dataset_id':metadata.dataset_id,'schema_version':metadata.schema_version,'database_identity_sha256':identity(expected),'actual_app_sessions':sum(row[1] for row in sessions),'actual_app_sessions_exclusively_restore_database':True,'verified_before_owner_login':True})) +finally: + metadata.close() +`; + const scope=JSON.parse(execFileSync('python3',['-c',scopeCode,catalog],{cwd:root,env:serverEnv,encoding:'utf8'})); + assert.match(scope.dataset_id,/^[a-f0-9]{32}$/);assert.equal(scope.catalog,path.resolve(catalog)); + fs.writeFileSync(path.join(report,'server-database-scope.json'),JSON.stringify(scope,null,2)); + await fixtureAuth.register('http://127.0.0.1:'+port,catalog,root); + authRegistered=true; + const browser=launch(process.env.BROWSER||'/usr/bin/chromium',['--headless','--no-sandbox','--disable-gpu','--no-first-run','--no-default-browser-check','--remote-debugging-port=0','--window-size=1440,900','--user-data-dir='+path.join(temporary,'browser'),'about:blank'],{stdio:['ignore','ignore','ignore'],env:{...process.env,XDG_CONFIG_HOME:temporary,XDG_CACHE_HOME:temporary}}); + const debugPort=await waitForDebugger(browser,path.join(temporary,'browser','DevToolsActivePort')); + const tabs=await(await fetch('http://127.0.0.1:'+debugPort+'/json')).json(),target=tabs.find(tab=>tab.type==='page'&&tab.url==='about:blank');assert.ok(target); + ws=new WebSocket(target.webSocketDebuggerUrl);await new Promise((resolve,reject)=>{ws.onopen=resolve;ws.onerror=reject;}); + let next=0;const pending=new Map(); + send=(method,params={})=>new Promise((resolve,reject)=>{const id=++next,timer=setTimeout(()=>{pending.delete(id);reject(Error('CDP timeout '+method));},20000);pending.set(id,{resolve:value=>{clearTimeout(timer);resolve(value);},reject:error=>{clearTimeout(timer);reject(error);}});ws.send(JSON.stringify({id,method,params}));}); + ws.onmessage=event=>{const message=JSON.parse(event.data);if(message.id){const task=pending.get(message.id);pending.delete(message.id);if(task)message.error?task.reject(message.error):task.resolve(message.result);}if(message.method==='Runtime.exceptionThrown')errors.push(message.params.exceptionDetails);}; + evaluate=async expression=>{const result=await send('Runtime.evaluate',{expression,awaitPromise:true,returnByValue:true});if(result.exceptionDetails)throw Error(JSON.stringify(result.exceptionDetails));return result.result.value;}; + const doc=frame=>frame?'document.getElementById("catalog-frame").contentDocument':'document'; + const element=(selector,frame=false)=>`${doc(frame)}.querySelector(${JSON.stringify(selector)})`,text=(selector,frame=false)=>evaluate(`${element(selector,frame)}?.textContent`); + async function click(selector,frame=false){ + const box=await until(()=>evaluate(`(async()=>{const e=${element(selector,frame)};if(!e||e.disabled||!e.getClientRects().length)return null;${frame?'document.getElementById("catalog-frame").scrollIntoView({block:"start"});':''}e.scrollIntoView({block:'center'});await new Promise(r=>requestAnimationFrame(()=>requestAnimationFrame(r)));const b=e.getBoundingClientRect(),x=b.x+b.width/2,y=b.y+b.height/2,hit=${doc(frame)}.elementFromPoint(x,y),f=${frame?'document.getElementById("catalog-frame").getBoundingClientRect()':'{x:0,y:0}'};return hit&&(hit===e||e.contains(hit))?{x:x+f.x,y:y+f.y}:null})()`),'Click target unavailable '+selector); + for(const type of ['mousePressed','mouseReleased'])await send('Input.dispatchMouseEvent',{type,button:'left',clickCount:1,...box}); + } + async function key(key,code=key,modifiers=0){for(const type of ['keyDown','keyUp'])await send('Input.dispatchKeyEvent',{type,key,code,modifiers,windowsVirtualKeyCode:{Enter:13,Escape:27,Tab:9,a:65}[key]||0,...(type==='keyDown'&&key==='Enter'?{text:'\r',unmodifiedText:'\r'}:{})});} + const select=(selector,value,frame=false)=>evaluate(`(()=>{const e=${element(selector,frame)};e.value=${JSON.stringify(value)};e.dispatchEvent(new Event('change',{bubbles:true}));})()`); + await send('Page.enable');await send('Page.setLifecycleEventsEnabled',{enabled:true});await send('Runtime.enable');await send('Network.enable'); + const offlineCanary=process.env.LD_PRELOAD?JSON.parse(execFileSync('python3',['-c',"import sys;sys.path.insert(0,'tests');from test_workspace_offline import CANARY;exec(CANARY)"],{cwd:root,encoding:'utf8'})):null; + await fixtureAuth.browser(send,'http://127.0.0.1:'+port); + await send('Page.navigate',{url:base+'/workspaces'});await until(()=>evaluate('!!document.getElementById("open-project")')); + async function captureWorkspace(name,width,selector){await send('Emulation.setDeviceMetricsOverride',{width,height:900,deviceScaleFactor:1,mobile:false});await evaluate(`${element(selector)}.scrollIntoView({block:'start'})`);await pause(100);assert.ok(await evaluate('document.documentElement.scrollWidth<=innerWidth+1'));const file=name+'.jpg';fs.writeFileSync(path.join(report,file),Buffer.from((await send('Page.captureScreenshot',screenshotOptions)).data,'base64'));captures.push({file,width,height:900,qualification:'Read-only restored catalog; no new execution or scientific acceptance'});} + async function api(route){const response=await fetch(base+route);const value=await response.json();assert.ok(response.ok,JSON.stringify({route,status:response.status,value}));return value;} + + const runId=prior.run_id,recordId=prior.record_id; + const beforeRuns=(await api('/api/workspace/runs')).map(x=>x.id).sort(),beforeRecords=(await api('/api/workbench/records')).items.map(x=>x.id).sort(); + const beforeGraphs=await api('/api/workspace/graphs'); + const beforeRecord=await api('/api/workbench/records/'+recordId),run=await api('/api/workspace/runs/'+runId);assert.equal(run.state,'completed');assert.equal(beforeRecord.review,'human_reviewed'); + await until(()=>evaluate('typeof document.getElementById("dataset")?.onchange==="function"'));await select('#dataset','catalog'); + await evaluate('document.getElementById("nav-pipeline").focus()');await key('Enter'); + await until(()=>evaluate('!!document.getElementById("real-graph-select")')); + await select('#real-graph-select',run.graph.id); + await until(()=>evaluate('!!document.getElementById("external-preflight")')); + await captureWorkspace('restored-pipeline-1440',1440,'#real-pipeline-root');await captureWorkspace('restored-pipeline-390',390,'#real-pipeline-root'); + await evaluate('document.getElementById("nav-runs").focus()');await key('Enter'); + await click('#real-run-'+runId);await until(()=>evaluate('!!document.getElementById("external-result-summary")')); + await captureWorkspace('inspection-summary-1440',1440,'#external-result-summary');await captureWorkspace('inspection-summary-390',390,'#external-result-summary'); + await evaluate('document.getElementById("nav-data").focus()');await key('Enter'); + await until(()=>evaluate('!!document.getElementById("catalog-frame")?.contentWindow.TuldokWorkspaceData')); + await until(async()=>{const value=await evaluate(`document.getElementById('catalog-frame').contentWindow.TuldokWorkspaceData.openCurrentRecord('${recordId}')`);if(value.kind!=='available')console.log('Read-only initial record navigation deferred:',value.message);return value.kind==='available';},'Existing reviewed record could not be opened'); + await select('#scientific-field','pressure',true);await select('#scientific-axis','z',true); + const ready=()=>evaluate(`(()=>{const d=${doc(true)},c=d.getElementById('scientific-canvas');return c&&!c.hidden&&c.width===32&&c.height===32&&d.getElementById('scientific-status').textContent.includes('Producer-declared stage: solved pressure')&&d.getElementById('record-review').value==='human_reviewed'})()`); + await until(ready,'Reviewed exact pressure slice never became ready'); + for(const width of [1440,390]){ + await send('Emulation.setDeviceMetricsOverride',{width,height:900,deviceScaleFactor:1,mobile:false});await until(ready); + await evaluate(`(async()=>{const f=document.getElementById('catalog-frame');f.scrollIntoView({block:'start'});f.contentDocument.getElementById('scientific-canvas').scrollIntoView({block:'center'});await new Promise(r=>requestAnimationFrame(()=>requestAnimationFrame(r)));window.scrollBy(0,f.getBoundingClientRect().top)})()`); + assert.ok(await ready());assert.ok(!(await text('#scientific-status',true)).includes('Loading')); + const file='inspection-loaded-data-'+width+'.jpg';fs.writeFileSync(path.join(report,file),Buffer.from((await send('Page.captureScreenshot',screenshotOptions)).data,'base64'));captures.push({file,width,height:900,status:await text('#scientific-status',true),coordinates:await text('#scientific-coordinates',true),review:'human_reviewed'}); + } + + const inventory=[];assert.equal(run.artifacts.length,89); + for(const item of run.artifacts){ + const response=await fetch(base+item.url);assert.equal(response.status,200);assert.equal(Number(response.headers.get('content-length')),item.bytes); + let bytes=0;const hash=crypto.createHash('sha256');for await(const chunk of response.body){bytes+=chunk.length;hash.update(chunk);} + const digest=hash.digest('hex');assert.equal(bytes,item.bytes);assert.equal(digest,item.sha256);inventory.push({name:item.name,bytes,sha256:digest}); + if(inventory.length%8===0)console.log('Restored verified HTTP artifacts',inventory.length,'/89'); + } + const sorted=value=>[...value].sort((a,b)=>a.name.localeCompare(b.name));assert.deepEqual(sorted(inventory),sorted(expectedInventory)); + fs.writeFileSync(path.join(report,'restored-http-inventory.json'),JSON.stringify(inventory,null,2)); + assert.deepEqual(await api('/api/workspace/graphs'),beforeGraphs); + assert.deepEqual((await api('/api/workspace/runs')).map(x=>x.id).sort(),beforeRuns);assert.deepEqual((await api('/api/workbench/records')).items.map(x=>x.id).sort(),beforeRecords);assert.deepEqual(await api('/api/workbench/records/'+recordId),beforeRecord);assert.deepEqual(errors,[]); + fs.writeFileSync(path.join(report,'receipt.json'),JSON.stringify({qualification:'Actual relocated restored catalog: read-only Data/Pipeline/Run UI and all89 authenticated HTTP artifact hashes; no model/import/review/graph mutation',source_commit:execFileSync('git',['rev-parse','HEAD'],{cwd:root,encoding:'utf8'}).trim(),restored_catalog:path.resolve(catalog),prior_evidence:path.resolve(evidence),graphs_exactly_unchanged:true,http_artifacts:inventory.length,http_bytes:inventory.reduce((sum,item)=>sum+item.bytes,0),prior_http_inventory_exact_match:true,run_id:runId,record_id:recordId,run_ids_unchanged:beforeRuns,record_ids_unchanged:beforeRecords,record_exactly_unchanged:true,captures,offline_canary:offlineCanary},null,2));console.log('PASS restored scientific UI and HTTP qualification:',report); +})().catch(async error=>{console.error(error);console.error('Application stderr:',serverErrors);process.exitCode=1;}).finally(async()=>{ + const logout={attempted:authRegistered,verified:false}; + if(authRegistered){ + try{ + const response=await fetch(base+'/api/access/logout',{method:'POST',headers:{'Content-Type':'application/json'},body:'{}',signal:AbortSignal.timeout(10000)}); + logout.http_status=response.status;await response.arrayBuffer();assert.ok(response.ok,'Restored owner session logout refused'); + logout.verified=true;console.log('Restored owner session logout verified'); + }catch(error){logout.failure_type=error.name;console.error('Restored owner session logout failed:',error.name);process.exitCode=1;} + } + try{ + fs.writeFileSync(path.join(report,'logout.json'),JSON.stringify(logout,null,2)); + const receiptPath=path.join(report,'receipt.json'); + if(fs.existsSync(receiptPath)){const receipt=JSON.parse(fs.readFileSync(receiptPath));receipt.owner_session_logout_verified=logout.verified;fs.writeFileSync(receiptPath,JSON.stringify(receipt,null,2));} + }finally{ + try{ws?.close();}finally{for(const child of children.reverse())child.kill('SIGTERM');} + } +});