Add resumable corpus inventory verification
This commit is contained in:
1 parent
0e276ae652
commit
4b2e5578cd
6 files changed
+1372
-49
No files matched your search
@@ -0,0 +1,91 @@
|
||||
# Corpus inventory and production handoff
|
||||
|
||||
The repository contains a read-only verifier for the ignored local corpus at
|
||||
`data/minjust-cbd/` and `data/minjust-normalized/`. It does not download,
|
||||
normalize, copy, index, or modify source files. Keep inventory artifacts outside
|
||||
the repository and both source trees.
|
||||
|
||||
## Run the inventory
|
||||
|
||||
From the repository root, create a private artifact directory and run:
|
||||
|
||||
```bash
|
||||
artifact_dir=$(mktemp -d /tmp/akyldash-corpus-audit.XXXXXXXX)
|
||||
python3 infra/deploy/verify_corpus.py \
|
||||
--raw-root data/minjust-cbd \
|
||||
--normalized-root data/minjust-normalized \
|
||||
--output-dir "$artifact_dir"
|
||||
```
|
||||
|
||||
The tool writes `result.json` and `inventory.sqlite3` into that directory. Keep
|
||||
the directory private: the inventory contains source paths and checksums. Exit
|
||||
status `0` means the scan and structural checks completed without errors; `1`
|
||||
means findings were recorded or the run is incomplete; `2` means the arguments
|
||||
or artifact path were invalid. Read `result.json` to distinguish those states.
|
||||
Warnings, including documents with zero text fragments, are reported
|
||||
separately and do not turn the scan into a failure.
|
||||
|
||||
For an interrupted run, use the same artifact directory and add `--resume`.
|
||||
Resume rehashes every source file instead of trusting cached timestamps, then
|
||||
reports added, changed, and removed paths relative to entries already stored
|
||||
in the inventory. After resuming an interrupted first pass, those counters are
|
||||
relative to the partial saved inventory; the full rehash and final checks still
|
||||
cover every path. A successful result therefore describes the latest complete
|
||||
pass. Run against a quiescent corpus; do not
|
||||
normalize, fetch, or copy files into either source tree during the scan. Do not
|
||||
point `--output-dir` inside either
|
||||
source tree or through a symlink. The verifier rejects such paths before
|
||||
creating or changing them; it also flags symlinks, reparse points, hard links,
|
||||
unsupported file types, and files that change while being read.
|
||||
|
||||
The audit checks SQLite integrity, manifest/document directory agreement,
|
||||
normalized schema and normalizer versions, source inventory checksums,
|
||||
fragment identities and text hashes, required output files, and text assembled
|
||||
from fragments. It does not establish the legal authority, completeness, or
|
||||
semantic correctness of MinJust content.
|
||||
|
||||
## Current data disposition
|
||||
|
||||
As measured on 2026-10-18, both local manifests contain 209,958 documents and
|
||||
pass SQLite `quick_check`. All normalized manifest entries are `success` at
|
||||
schema `1` and normalizer `1.0.0`. The raw manifest records one fetch error for
|
||||
code `6`, which is absent from its `documents` table; this does not prove that
|
||||
the source document is absent. On 2026-10-18, bounded calls through the
|
||||
repository's rate-limited MinJust client received HTTP `404` for
|
||||
`GetDocumentListByQuery`, `GetDocument(6)`, control `GetDocument(1096)`, and
|
||||
`CheckAvailable`. The listing call failed before returning a code list, and
|
||||
both document calls failed identically. The repeated `404` is consistent with
|
||||
the OpenData endpoint being unavailable or its route having changed, but does
|
||||
not identify which. It is not evidence that code `6` is absent. The [public
|
||||
document listing page](https://cbd.minjust.gov.kg/list-docs/kg) was reachable,
|
||||
but it does not confirm this numeric API code. Do not describe the corpus as
|
||||
fully verified or silently waive this error. Recheck the official listing and
|
||||
API when the endpoint responds, then record an explicit owner disposition.
|
||||
From the VPS, a metadata-only HTTPS check to `CheckAvailable` failed at the
|
||||
HTTP/2 transport layer (`curl` exit 92, `INTERNAL_ERROR`, HTTP status `000`,
|
||||
TLS verification `0`). An HTTP/1.1 retry was not completed.
|
||||
|
||||
The full local file inventory and normalized cross-check are still in progress
|
||||
until a `result.json` reports `status: complete` or `status: failed` and
|
||||
`scan_status: complete`. Do not treat progress output as a completed audit.
|
||||
|
||||
## Production transfer and indexing gates
|
||||
|
||||
Production deployment is not enabled. Before any transfer, the owner must
|
||||
review the root-only host audit, confirm the target data location is empty or
|
||||
backed up, resolve the raw code `6` finding, and approve the exact normalized
|
||||
corpus to use. Transfer only `minjust-normalized/`; keep the raw source archive
|
||||
offline. Do not preserve the source's Windows/WSL file permission bits. Copy
|
||||
to a new staging location with owner-approved permissions, verify the copied
|
||||
manifest and files there, and only then consider placing it under `DATA_ROOT`.
|
||||
Never overwrite an existing corpus or OpenSearch volume.
|
||||
|
||||
The current bulk importer checks individual Bulk API item results, but it can
|
||||
switch an alias after a zero-fragment input and does not enforce a refresh,
|
||||
count comparison, or representative-query check before that switch. Until a
|
||||
separate change adds those gates, do not pass `--alias` during production
|
||||
loading. Load into a new versioned index only after the corpus transfer is
|
||||
verified; require a nonzero fragment count, successful item results, an
|
||||
explicit refresh, a matching OpenSearch document count, and representative
|
||||
Russian/Kyrgyz queries before an owner performs an alias change. Take and
|
||||
verify the required backup before any production index or service change.
|
||||
@@ -1,25 +1,26 @@
|
||||
# Infrastructure current state
|
||||
|
||||
Last host audit: 2026-10-12. Gitea Actions status checked 2026-10-18. Source: read-only SSH session as `codex`, owner-reported
|
||||
UFW state, local repository inspection, and Gitea API. Root-only Docker state,
|
||||
UFW rule details, and protected file metadata remain unverified.
|
||||
Read-only host recheck: 2026-10-18. Source: SSH as `codex`, local repository
|
||||
inspection, and Gitea API. Root-only Docker state, UFW rule details, and
|
||||
protected file metadata remain unverified.
|
||||
|
||||
## VPS facts
|
||||
|
||||
- Host: `avprogs.hlab.kz`, `206.223.249.118`, SSH port `22`, user `codex`.
|
||||
- OS: Ubuntu Server `24.04.5 LTS`; kernel `6.8.0-142-generic` from the prior
|
||||
inventory audit.
|
||||
- Resources: 4 vCPU, 7.7 GiB RAM, 3.8 GiB swap, 99 GiB disk with 92 GiB free
|
||||
at this audit.
|
||||
- Resources: 4 vCPU, 7.7 GiB RAM, 3.8 GiB swap, 99 GiB root filesystem with
|
||||
84 GiB free on 2026-10-18. Runner storage is a separate 15 GiB ext4 loop
|
||||
mount with 12 GiB free at that check.
|
||||
- Network: interface `ens3`, IPv4 `206.223.249.118/32`.
|
||||
- `codex` is UID/GID 1000, groups `codex` and `users`; `sudo -n true` requires
|
||||
a password. Docker CLI cannot access `/var/run/docker.sock` as `codex`.
|
||||
- SSH service is running. The inspected sshd configuration has public-key and
|
||||
password authentication enabled and `PermitRootLogin yes`; no SSH changes
|
||||
were made.
|
||||
- Bootstrap completed. Docker CE/CLI `29.9.0`, Compose `5.6.0`, containerd
|
||||
`2.4.1`, and runc `1.5.2` are installed; package versions and active Docker
|
||||
service were independently checked. The Docker daemon API remains root-only.
|
||||
- Bootstrap completed. Docker CLI `29.9.0` and Compose `5.6.0` were rechecked
|
||||
over SSH on 2026-10-18; containerd `2.4.1` and runc `1.5.2` are from the
|
||||
prior package audit. The Docker daemon API remains root-only.
|
||||
- `/etc/docker/daemon.json` has the expected `local` log driver and `10m` / `3`
|
||||
rotation limits. Bootstrap paths exist with root ownership: `/opt/akyldash`
|
||||
mode `0755`, `/etc/akyldash` and `/var/backups/akyldash` mode `0700`, and
|
||||
@@ -27,7 +28,10 @@ UFW rule details, and protected file metadata remain unverified.
|
||||
- `/opt/akyldash/current`, verifier/rollback binaries, and the sudoers file are
|
||||
absent. `codex` has not received sudo or Docker-group access. Metadata for
|
||||
`/etc/akyldash/production.env` and `/etc/akyldash/review_secret` could not be
|
||||
checked because `/etc/akyldash` is root-only.
|
||||
checked because access is denied. The target
|
||||
`/var/lib/akyldash/data/minjust-normalized` and its manifest metadata were
|
||||
also denied; their presence, ownership, and contents are unknown. No sudo was
|
||||
used.
|
||||
- TCP listeners observed: SSH `22` and loopback DNS only; no listener on `1500`,
|
||||
`80`, or `443`. No ISPmanager package or service unit was found through the
|
||||
unprivileged checks; root can confirm with the audit script below.
|
||||
@@ -39,8 +43,10 @@ UFW rule details, and protected file metadata remain unverified.
|
||||
`https://git.vakanaut.ru/api/v1/version` returned Gitea `1.25.5`; runner
|
||||
registration and Gitea Actions protocol connectivity are not confirmed.
|
||||
- `akyldash.kg` resolves to Cloudflare IPv4/IPv6 edge addresses. From the VPS,
|
||||
HTTP, HTTPS, and `/review` still return Cloudflare `521`; origin TLS and
|
||||
Cloudflare Full (strict) are not verified.
|
||||
HTTPS returned Cloudflare `521` with TLS verification result `0` on
|
||||
2026-10-18. TCP listeners were SSH `22` and local DNS only, so no origin
|
||||
listener on `80`/`443` was observed. Origin TLS and Cloudflare Full (strict)
|
||||
are not verified.
|
||||
|
||||
## Application and Gitea facts
|
||||
|
||||
@@ -49,13 +55,12 @@ UFW rule details, and protected file metadata remain unverified.
|
||||
PRs enabled. `main` is protected and requires one approval; the current API
|
||||
principal cannot push or merge directly to `main`.
|
||||
- The repository runner-list API returns `403` because this API principal is
|
||||
not the repository owner. Gitea status checked 2026-10-18 showed smoke run 62
|
||||
successful and CI run 61 pending on PR head `69c62466`. That workflow used
|
||||
`ubuntu-latest`, while the smoke workflow used `akyldash-ci`; this label
|
||||
difference is a plausible explanation, not a confirmed scheduler diagnosis.
|
||||
The owner reports that the runner is registered and Idle and rootless Docker
|
||||
works (report date not recorded). Runner inventory and package publishing
|
||||
permissions remain unverified.
|
||||
not the repository owner. The latest verified CI run 75 and smoke run 76 both
|
||||
succeeded on `main` head `0e276ae6`. The Actions run-list endpoint returned
|
||||
`403` for this API principal during this recheck, so no newer run state was
|
||||
independently retrieved. The owner reports the runner is registered and
|
||||
Idle; repository runner inventory and package publishing permissions remain
|
||||
unverified.
|
||||
- No public application listener is observed on TCP `80`/`443`. Docker
|
||||
container/volume state still requires the root audit. Repository Compose describes
|
||||
Caddy `2.11.6` pinned by digest, Python Search API `0.11.3`, SQLite
|
||||
@@ -89,11 +94,15 @@ UFW rule details, and protected file metadata remain unverified.
|
||||
ext4 loop-backed image/workspace storage, capacity 1, per-job limits (2 CPU,
|
||||
3 GiB RAM, 512 processes), account slice limits (2.5 CPU, 4 GiB RAM, 2048
|
||||
tasks), rotated file logs, and a systemd health timer. Jobs target the
|
||||
`akyldash-ci` label and do not receive either Docker socket. The owner reports
|
||||
that runner registration and rootless Docker work (report date not recorded).
|
||||
Gitea status checked 2026-10-18 showed smoke run 62 successful and CI run 61
|
||||
pending on PR head `69c62466`. The workflow/label difference may explain that
|
||||
pending status, but the scheduler reason was not independently verified. On
|
||||
`akyldash-ci` label and do not receive either Docker socket. On 2026-10-18,
|
||||
SSH showed `akyldash-act-runner.service` active under `akyldash-runner`, with
|
||||
zero restarts and about 115 MiB current memory. Its rootless Docker store is
|
||||
mounted from `/dev/loop0` as ext4 with `rw,nosuid,nodev`; the 15 GiB mount had
|
||||
12 GiB free. The separate `akyldash-runner-restart.service` remains failed
|
||||
with exit status 1 since 2026-10-14; its journal had no entries, so the cause
|
||||
is unknown. The latest verified main CI run 75 and smoke run 76 succeeded on
|
||||
head `0e276ae6`. The Actions API returned 403 during this recheck, so no newer
|
||||
run state was independently retrieved. On
|
||||
head `3a89dc21`, CI run 63 failed at the host audit root guard and smoke run 64
|
||||
succeeded. On head `436806a8`, CI run 65 failed in the Caddy container action
|
||||
with `exec: "validate": executable file not found`; Kaniko was skipped and
|
||||
@@ -113,6 +122,37 @@ UFW rule details, and protected file metadata remain unverified.
|
||||
backend `0.11.3`, frontend `0.1.2`, Telegram bot `0.2.2` (the bot is not
|
||||
deployed on this VPS).
|
||||
|
||||
## Corpus readiness (2026-10-18)
|
||||
|
||||
- The local raw archive and normalized corpus are ignored under `data/`; no
|
||||
corpus files were changed. Their read-only SQLite manifests both pass
|
||||
`quick_check` and each report 209,958 documents. The raw manifest is
|
||||
28,295,168 bytes with SHA-256
|
||||
`f8e3dd2fdec607583ca09cfdb5b76171bb7bfa1e41a0e07ee5aca8481e18e17e`; it has
|
||||
one fetch error, code `6`, absent from `documents`. The normalized manifest
|
||||
is 31,571,968 bytes with SHA-256
|
||||
`1679c20e6d0f3e5a9b50737c006c4417a28ebcaf7467d194d3bf8243538f83bf`; all
|
||||
entries are `success`, schema `1`, normalizer `1.0.0`.
|
||||
- A streaming full-file inventory and cross-check is running read-only with
|
||||
artifacts only in `/tmp/akyldash-corpus-audit.Mjr6aa`. Latest progress was
|
||||
run `b4a4556e30cc464d90bc8d75ef1673e9`; live counters are in its private
|
||||
`result.json`, updated every 1,000 entries. The normalized tree has not yet
|
||||
started. Progress is not a completed audit; record the final result and full
|
||||
manifest comparison when the run finishes.
|
||||
- Through the repository client, bounded MinJust OpenData requests for
|
||||
`GetDocumentListByQuery`, `GetDocument(6)`, control `GetDocument(1096)`, and
|
||||
`CheckAvailable` each returned HTTP `404`. The list API returned no code list;
|
||||
the direct requests do not establish whether code `6` exists. The public
|
||||
listing page is reachable, but it does not map the numeric API code. A
|
||||
metadata-only `CheckAvailable` request from the VPS failed in HTTP/2 with
|
||||
`curl` exit 92 (`INTERNAL_ERROR`), HTTP status `000`, and TLS verification 0;
|
||||
the HTTP/1.1 retry was not completed. Treat the code `6` disposition and
|
||||
full corpus readiness as unresolved.
|
||||
- On the VPS, read-only `stat` of `/var/lib/akyldash/data/minjust-normalized`
|
||||
and its manifest was denied. Production corpus presence and metadata remain
|
||||
unknown. No corpus was transferred and no production service, Docker socket,
|
||||
runner policy, or VPS configuration was changed.
|
||||
|
||||
## Checks not completed
|
||||
|
||||
- Docker Engine version from the server API, container and volume inventory,
|
||||
@@ -149,17 +189,11 @@ UFW rule details, and protected file metadata remain unverified.
|
||||
5. Test backup/restore and rollback only with synthetic data in an isolated
|
||||
environment before any production deployment.
|
||||
|
||||
## CI workflow change in this PR
|
||||
## Change in this PR
|
||||
|
||||
The branch targets `akyldash-ci` and uses digest-pinned isolated actions for
|
||||
Caddy validation and the two production Dockerfile builds. Kaniko image
|
||||
publishing and cache publishing are disabled. This repository change does not
|
||||
modify the VPS or grant workflow containers a Docker socket. CI run 65 failed
|
||||
in the Caddy action because the runner tried to execute `validate` directly;
|
||||
Kaniko was skipped. CI run 67 then found that the action's fixed
|
||||
`/github/workspace` path did not contain the repository; Kaniko was skipped.
|
||||
The branch now passes Gitea's `${{ github.workspace }}` value into all three
|
||||
container action invocations. CI run 69 validated Caddy and completed both
|
||||
image builds; smoke run 70 passed. Actual per-action resource-limit enforcement
|
||||
and peak usage were not independently measured or verified. Host-wide runner
|
||||
metrics, image publishing, and production deployment remain unverified.
|
||||
Adds a read-only, resumable full-file corpus inventory and normalized-data
|
||||
cross-check, synthetic regression tests, and an operator handoff runbook. CI's
|
||||
existing `infra/deploy` unittest discovery includes the new tests. No workflow,
|
||||
runner, VPS, production data, or Docker settings are changed. The real corpus
|
||||
scan is still in progress and the source API's code `6` outcome remains
|
||||
unconfirmed.
|
||||
@@ -34,8 +34,9 @@ backup/restore have been tested.
|
||||
It creates `/etc/akyldash/production.env` from the tracked example and
|
||||
generates `/etc/akyldash/review_secret`; both are root-owned mode `0600`.
|
||||
It refuses to overwrite either file and does not start services or create
|
||||
volumes. Review `DATA_ROOT` and place the approved normalized corpus there
|
||||
before deployment.
|
||||
volumes. Review `DATA_ROOT` and follow the
|
||||
[corpus inventory and handoff gates](CORPUS_HANDOFF.md) before placing the
|
||||
approved normalized corpus there.
|
||||
3. After reviewing the root-owned checkout and audit output, run
|
||||
`sudo bash /opt/akyldash/current/infra/deploy/install-helper.sh`. It installs
|
||||
the backup reader, read-only verifier, `akyldash-compose config|status`, and
|
||||
@@ -65,13 +66,13 @@ backup/restore have been tested.
|
||||
sudo chmod 0600 /opt/akyldash/deployed-image.txt
|
||||
```
|
||||
|
||||
Application deployment remains owner-run. A repository-level Actions runner
|
||||
installation is prepared in [CI_RUNNER.md](CI_RUNNER.md), but it is not yet
|
||||
installed or registered; its Online state and smoke workflow are unverified.
|
||||
The runner's job containers have no Docker socket or production secrets. CI has
|
||||
no image publishing or production deployment step. The installed sudo helpers
|
||||
support fixed verification, read-only Compose `config`/`status`, and guarded
|
||||
rollback. They do not start or stop services.
|
||||
Application deployment remains owner-run. The repository-scoped `act_runner`
|
||||
`0.2.13` service is active under `akyldash-runner` and uses rootless Docker on a
|
||||
separate loop-backed filesystem. CI run 75 and smoke run 76 succeeded on main
|
||||
head `0e276ae6`. Runner job containers have neither Docker socket nor production
|
||||
secrets. CI has no image publishing or production deployment step. The
|
||||
installed sudo helpers support fixed verification, read-only Compose
|
||||
`config`/`status`, and guarded rollback. They do not start or stop services.
|
||||
|
||||
## CI
|
||||
|
||||
@@ -79,5 +80,7 @@ rollback. They do not start or stop services.
|
||||
production images on pull requests and pushes to `main`. It does not publish an
|
||||
image or deploy production. `.gitea/workflows/runner-smoke.yaml` exercises the
|
||||
`akyldash-ci` runner on pull requests, pushes to `main`, or manual dispatch.
|
||||
Runner registration is incomplete, so neither workflow execution on that
|
||||
runner nor current `ubuntu-latest` runner availability has been confirmed.
|
||||
Both workflows passed on main head `0e276ae6` in runs 75 and 76 respectively.
|
||||
The repository runner-list endpoint is not available to the current API
|
||||
principal, so its registration details cannot be independently read through
|
||||
Gitea API.
|
||||
@@ -11,8 +11,13 @@
|
||||
|
||||
- загрузчик находится в `backend/ingestion/minjust_cbd.py`;
|
||||
- сырой архив хранится в `data/minjust-cbd` и исключён из Git;
|
||||
- в манифесте 209 811 успешно загруженных документов;
|
||||
- 13 кодов остаются в таблице `errors` и отсутствуют в таблице `documents`;
|
||||
- в read-only проверенном манифесте 209 958 документов;
|
||||
- один код, `6`, остаётся в таблице `errors` и отсутствует в `documents`;
|
||||
это не подтверждает отсутствие документа в источнике: при последней
|
||||
проверке OpenData API возвращал HTTP `404` для списка, `GetDocument(6)`,
|
||||
контрольного `GetDocument(1096)` и `CheckAvailable`;
|
||||
- в нормализованном манифесте 209 958 успешных записей, schema `1`,
|
||||
normalizer `1.0.0`; полная потоковая проверка всех файлов ещё выполняется.
|
||||
- документ содержит `metadata.json` и каталог `editions`;
|
||||
- редакция содержит `metadata.json`, `ru.html` и/или `ky.html`, иногда
|
||||
изображения;
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import sqlite3
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from verify_corpus import (
|
||||
InventoryError,
|
||||
database,
|
||||
safe_relative_path,
|
||||
validate_fragment_file,
|
||||
verify_corpus,
|
||||
)
|
||||
|
||||
|
||||
class CorpusInventoryTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.temp = tempfile.TemporaryDirectory()
|
||||
self.base = Path(self.temp.name)
|
||||
self.raw = self.base / "raw"
|
||||
self.raw.mkdir()
|
||||
self._manifest()
|
||||
|
||||
def tearDown(self):
|
||||
self.temp.cleanup()
|
||||
|
||||
def _manifest(self):
|
||||
connection = sqlite3.connect(self.raw / "manifest.sqlite3")
|
||||
connection.executescript(
|
||||
"CREATE TABLE documents (code INTEGER PRIMARY KEY, source_sha256 TEXT, fetched_at TEXT);"
|
||||
"CREATE TABLE errors (code INTEGER PRIMARY KEY, message TEXT, failed_at TEXT);"
|
||||
)
|
||||
connection.close()
|
||||
|
||||
def test_output_inside_source_is_rejected_without_creating_it(self):
|
||||
output = self.raw / "artifacts"
|
||||
before_mode = self.raw.stat().st_mode
|
||||
with self.assertRaisesRegex(InventoryError, "separate"):
|
||||
verify_corpus(self.raw, None, output)
|
||||
self.assertFalse(output.exists())
|
||||
self.assertEqual(self.raw.stat().st_mode, before_mode)
|
||||
self.assertEqual({path.name for path in self.raw.iterdir()}, {"manifest.sqlite3"})
|
||||
|
||||
def test_symlink_output_is_rejected_without_changing_target(self):
|
||||
target = self.base / "target"
|
||||
target.mkdir(mode=0o755)
|
||||
link = self.base / "output-link"
|
||||
try:
|
||||
link.symlink_to(target, target_is_directory=True)
|
||||
except (NotImplementedError, OSError):
|
||||
self.skipTest("directory symlinks are unavailable")
|
||||
before_mode = target.stat().st_mode
|
||||
with self.assertRaisesRegex(InventoryError, "link or file"):
|
||||
verify_corpus(self.raw, None, link)
|
||||
self.assertEqual(target.stat().st_mode, before_mode)
|
||||
self.assertEqual(list(target.iterdir()), [])
|
||||
|
||||
def test_public_existing_output_is_rejected_without_chmod(self):
|
||||
output = self.base / "public-output"
|
||||
output.mkdir(mode=0o755)
|
||||
output.chmod(0o755)
|
||||
before_mode = output.stat().st_mode
|
||||
with self.assertRaisesRegex(InventoryError, "must be private"):
|
||||
verify_corpus(self.raw, None, output)
|
||||
self.assertEqual(output.stat().st_mode, before_mode)
|
||||
self.assertEqual(list(output.iterdir()), [])
|
||||
|
||||
def test_resume_rejects_symlink_inventory_without_touching_target(self):
|
||||
output = self.base / "artifacts"
|
||||
output.mkdir(mode=0o700)
|
||||
target = self.base / "do-not-touch.sqlite3"
|
||||
target.write_bytes(b"sentinel")
|
||||
link = output / "inventory.sqlite3"
|
||||
try:
|
||||
link.symlink_to(target)
|
||||
except (NotImplementedError, OSError):
|
||||
self.skipTest("file symlinks are unavailable")
|
||||
with self.assertRaisesRegex(InventoryError, "private regular file"):
|
||||
verify_corpus(self.raw, None, output, resume=True)
|
||||
self.assertEqual(target.read_bytes(), b"sentinel")
|
||||
|
||||
def test_rejects_unsafe_cross_platform_relative_paths(self):
|
||||
for value in ("../outside", "C:\\outside", "folder\\file", "//server/share"):
|
||||
with self.subTest(value=value), self.assertRaises(InventoryError):
|
||||
safe_relative_path(value)
|
||||
|
||||
def test_interrupted_inventory_resumes_and_detects_changed_input(self):
|
||||
payload = self.raw / "payload.txt"
|
||||
payload.write_text("before", encoding="utf-8")
|
||||
output = self.base / "artifacts"
|
||||
stopped = verify_corpus(self.raw, None, output, stop_after_entries=1)
|
||||
self.assertEqual(stopped["status"], "incomplete")
|
||||
|
||||
resumed = verify_corpus(self.raw, None, output, resume=True)
|
||||
self.assertEqual(resumed["status"], "complete")
|
||||
self.assertEqual(resumed["input_changes"]["raw"]["added"], 1)
|
||||
|
||||
payload.write_text("after", encoding="utf-8")
|
||||
changed = verify_corpus(self.raw, None, output, resume=True)
|
||||
self.assertEqual(changed["status"], "complete")
|
||||
self.assertEqual(changed["input_changes"]["raw"]["changed"], 1)
|
||||
rows = sqlite3.connect(output / "inventory.sqlite3").execute(
|
||||
"SELECT sha256 FROM files WHERE relative_path='payload.txt'"
|
||||
).fetchone()
|
||||
self.assertEqual(rows[0], hashlib.sha256(b"after").hexdigest())
|
||||
|
||||
def test_manifest_error_missing_document_and_extra_directory_are_reported(self):
|
||||
connection = sqlite3.connect(self.raw / "manifest.sqlite3")
|
||||
connection.execute("INSERT INTO documents VALUES (1, ?, 'now')", ("a" * 64,))
|
||||
connection.execute("INSERT INTO errors VALUES (6, 'fetch failed', 'now')")
|
||||
connection.commit()
|
||||
connection.close()
|
||||
(self.raw / "documents" / "999").mkdir(parents=True)
|
||||
|
||||
result = verify_corpus(self.raw, None, self.base / "artifacts")
|
||||
kinds = {finding["kind"] for finding in result["finding_examples"]}
|
||||
self.assertEqual(result["status"], "failed")
|
||||
self.assertIn("raw_manifest_error", kinds)
|
||||
self.assertIn("missing_raw_document_directory", kinds)
|
||||
self.assertIn("extra_raw_document_directory", kinds)
|
||||
|
||||
def test_fragment_text_hash_mismatch_is_reported(self):
|
||||
normalized = self.base / "normalized"
|
||||
fragment_rel = "documents/1/editions/2/ru/fragments.json"
|
||||
fragment_path = normalized / fragment_rel
|
||||
fragment_path.parent.mkdir(parents=True)
|
||||
fragment = {
|
||||
"id": "document:1:edition:2:lang:ru:fragment:1",
|
||||
"document_code": "1",
|
||||
"edition_code": "2",
|
||||
"language": "ru",
|
||||
"position": 1,
|
||||
"text": "text",
|
||||
"text_sha256": "0" * 64,
|
||||
"source_path": "documents/1/source.txt",
|
||||
"source_sha256": "1" * 64,
|
||||
}
|
||||
fragment_path.write_text(json.dumps([fragment]), encoding="utf-8")
|
||||
content_path = fragment_path.with_name("content.txt")
|
||||
content_path.write_text("text\n", encoding="utf-8")
|
||||
connection = database(self.base / "test.sqlite3")
|
||||
content_digest = hashlib.sha256(b"text\n").hexdigest()
|
||||
connection.execute(
|
||||
"INSERT INTO files(root_id,relative_path,sha256,status,last_seen_run) VALUES('normalized',?,?, 'hashed','run')",
|
||||
(content_path.relative_to(normalized).as_posix(), content_digest),
|
||||
)
|
||||
validate_fragment_file(
|
||||
connection,
|
||||
"run",
|
||||
normalized,
|
||||
fragment_path,
|
||||
fragment_rel,
|
||||
hashlib.sha256(fragment_path.read_bytes()).hexdigest(),
|
||||
{"documents/1/source.txt": "1" * 64},
|
||||
)
|
||||
kinds = {row[0] for row in connection.execute("SELECT kind FROM findings WHERE run_id='run'")}
|
||||
connection.close()
|
||||
self.assertIn("fragment_text_checksum_mismatch", kinds)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user