Door A (manifest -> connector -> deterministic materialization -> index) is no longer implemented here. src/portfolio_optimiser/ingest.py becomes a thin consumer seam over the shared library, git-pinned to v0.3.1 on the same Forgejo channel portfolio-optimiser-claude uses. Net -626/+385; ingest.py 599 -> 145 lines. shared/ingest-spec.md remains the normative spec: the library implements it, it does not replace it. Spec changes continue to go via commons. Acceptance criterion met and proven: all three golden bundles (file/sql/http) are byte-exact before and after, including the idempotence re-run. examples/ and shared/ carry ZERO modifications -- the fasit was not adjusted to fit. The rejection set was verified equivalent, not assumed: all 22 malformations the repo's pydantic models refused are refused by the library, with typed codes (okf_type_reserved, credential_embedded, extraction_id_duplicate, ...). Test rebinding (invariants preserved, vehicle changed): the library has zero runtime dependencies by design, so pydantic is unavailable to it. ManifestV1.model_validate(dict) -> load_manifest_bytes(bytes); ValidationError -> ManifestError; model_fields -> dataclasses.fields; PathSecurityError -> SourceError(path_escape); ValueError -> MaterializationError(ingested_at_invalid). Tests now also pin the refusal `code`, the library's documented stability contract -- a sharper assertion than "some validation error was raised". Two accepted behavioural deltas, recorded rather than silently dropped: - Title whitespace is stored verbatim instead of collapsed at validation, so the frontmatter title and the index label are no longer guaranteed identical for irregular whitespace. Both behaviours are spec-conformant (the spec is SILENT; the old one was a repo-local pinned decision). Queued as a commons-amendment candidate so both stacks pin the same answer. Goldens unaffected. - The section 8 audit log moves to logger llm_ingestion_okf.materialize. Nothing in the repo consumed the old channel. Also: the `type` discriminator is no longer a dataclass field, so the spec cross-check asserts it explicitly -- without that line the swap would have silently narrowed the test. New tests/test_ingest_library_seam.py pins the seam itself: the restated section 5 stamp formula against the stamp the library actually writes (the one place the adapter does not purely delegate, since v0.3.1 exposes no stamp helper), the local-only allow_network default, the list[Path] unwrapping, and a guard that the adapter never regrows local Door A machinery. All four verified RED when detached, as were both golden regressions under a byte-level render mutation. Door A is UNGATED: it calls no guard before writing to disk. Gating untrusted content remains the caller's responsibility (guard wiring still planned). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B4jNN186eVqfe1x5DnTU6r
153 lines
6.4 KiB
Python
153 lines
6.4 KiB
Python
"""Door A ingest — the consumer seam over the shared ``llm-ingestion-okf`` library.
|
|
|
|
An addition IN FRONT of the loop (``shared/ingest-spec.md`` §1): a deterministic step that
|
|
couples the framework to a real data source and materializes the extract as an OKF knowledge
|
|
bundle, which the existing 8-step loop then consumes UNCHANGED. Zero model calls; no network
|
|
for the ``file``/``sql`` source types.
|
|
|
|
Since the library adoption (2026-07-20) the implementation IS the shared
|
|
``llm-ingestion-okf`` library (git-pinned to ``v0.3.1``). The spec in ``shared/ingest-spec.md``
|
|
remains the normative source — the library implements it, it does not replace it — and the
|
|
repo-local goldens under ``examples/`` remain the fasit (verified byte-exact across all three
|
|
source types, before and after the swap).
|
|
|
|
This module is the ONE place the repo touches the library for Door A. It is deliberately thin:
|
|
it re-exports the library's typed surface and keeps the historical ``materialize`` signature
|
|
(keyword-only ``ingested_at``, ``list[Path]`` return) so callers and tests bind to a stable
|
|
repo-local name rather than to the library's evolving one.
|
|
|
|
**Gating is the CALL SITE's responsibility (library README, "What is gated today: nothing").**
|
|
Door A calls no guard function before writing to disk — ``materialize_bundle`` writes what it
|
|
is given. The repo's own local-only posture still holds at this seam via ``allow_network``
|
|
(default ``False``): an ``http`` source is refused fail-fast at the library's network gate
|
|
unless a run explicitly opts in — the manifest can never grant itself network (§8, no silent
|
|
egress). Untrusted-content scanning remains the separate, still-planned ``llm-ingestion-guard``
|
|
wiring (see ``docs/plan/2026-07-16-llm-ingestion-guard-inclusion.md``); adopting this library
|
|
does NOT provide it.
|
|
|
|
MAF-free (D7-portable), like the rest of the context seam: the library has zero runtime
|
|
dependencies and imports no ``agent_framework`` / ``mcp``. Guarded by
|
|
``tests/test_ingest_loadbearing.py::test_ingest_module_is_maf_free_and_context_layer_pure``.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
from pathlib import Path
|
|
|
|
from llm_ingestion_okf import (
|
|
Extraction,
|
|
FileSource,
|
|
HttpSource,
|
|
IngestError,
|
|
IngestResult,
|
|
Manifest,
|
|
ManifestError,
|
|
MaterializationError,
|
|
NetworkGateError,
|
|
RenderError,
|
|
SourceError,
|
|
SqlSource,
|
|
materialize_bundle,
|
|
)
|
|
from llm_ingestion_okf import (
|
|
load_manifest as _load_manifest,
|
|
)
|
|
from llm_ingestion_okf.connectors import (
|
|
HttpGet,
|
|
read_csv,
|
|
read_http,
|
|
read_sql,
|
|
)
|
|
from llm_ingestion_okf.manifest import generated_filename
|
|
from llm_ingestion_okf.render import render_fenced_block, render_table
|
|
|
|
# Two historical PRIVATE names the repo's existing tests bind to, re-exported so those bindings
|
|
# survive the adoption unchanged. `_urllib_get` backs an identity assertion that the default http
|
|
# transport is the real socket path rather than a stub (tests/test_ingest_http.py);
|
|
# `_sql_value_to_text` is exercised directly for the §5 bool/BLOB refusals
|
|
# (tests/test_ingest_sql.py). Not in `__all__` — they are not part of this module's contract.
|
|
from llm_ingestion_okf.connectors import urllib_get as _urllib_get # noqa: F401
|
|
from llm_ingestion_okf.render import sql_value_to_text as _sql_value_to_text # noqa: F401
|
|
|
|
#: The repo's historical name for the §4 top-level model. The library calls it ``Manifest`` and
|
|
#: carries the version in the ``manifest_version`` field (still ``1``); the alias keeps the
|
|
#: repo-local name stable for callers that bound to it before the library adoption.
|
|
ManifestV1 = Manifest
|
|
|
|
__all__ = [
|
|
"Extraction",
|
|
"FileSource",
|
|
"HttpGet",
|
|
"HttpSource",
|
|
"IngestError",
|
|
"IngestResult",
|
|
"Manifest",
|
|
"ManifestError",
|
|
"ManifestV1",
|
|
"MaterializationError",
|
|
"NetworkGateError",
|
|
"RenderError",
|
|
"SourceError",
|
|
"SqlSource",
|
|
"generated_filename",
|
|
"load_manifest",
|
|
"materialize",
|
|
"materialize_bundle",
|
|
"read_csv",
|
|
"read_http",
|
|
"read_sql",
|
|
"render_fenced_block",
|
|
"render_table",
|
|
]
|
|
|
|
|
|
def load_manifest(path: str | Path) -> tuple[Manifest, str]:
|
|
"""Validate a manifest fail-fast, BEFORE any source access (§4, §9).
|
|
|
|
Returns the validated manifest and the §5 provenance stamp ``{stem}@{hash16}`` — the first
|
|
16 hex chars of SHA-256 over the manifest file's RAW bytes, so every generated file points
|
|
at the exact manifest version that produced it. Raises ``ManifestError`` without touching
|
|
any source.
|
|
|
|
The two-value return is the repo-local shape: the library returns the manifest alone and
|
|
mints the stamp INSIDE ``materialize_bundle``, exposing no stamp helper (v0.3.1). The
|
|
formula is therefore restated here, which is the one place this adapter is not purely
|
|
delegating — so it is pinned by
|
|
``tests/test_ingest_library_seam.py::test_adapter_stamp_equals_library_stamp``, which
|
|
compares this value against the ``ingest_manifest`` the library actually writes. That test
|
|
goes RED if either side's formula drifts."""
|
|
manifest_path = Path(path)
|
|
manifest = _load_manifest(manifest_path)
|
|
stamp = f"{manifest_path.stem}@{hashlib.sha256(manifest_path.read_bytes()).hexdigest()[:16]}"
|
|
return manifest, stamp
|
|
|
|
|
|
def materialize(
|
|
manifest_path: str | Path,
|
|
bundle_dir: str | Path,
|
|
*,
|
|
ingested_at: str,
|
|
allow_network: bool = False,
|
|
http_get: HttpGet | None = None,
|
|
) -> list[Path]:
|
|
"""Materialize a manifest's extractions into an OKF bundle (§5), returning the generated
|
|
concept-file paths in extraction order.
|
|
|
|
The historical repo signature over the library's ``materialize_bundle``: ``ingested_at`` is
|
|
a REQUIRED keyword (no wall-clock default, stamped verbatim; mirrors the promotion gate's
|
|
timestamp rule), and the return is the ``written`` paths as a list rather than the
|
|
library's ``IngestResult``. ``allow_network`` (§8) is the per-run network opt-in — an
|
|
``http`` source is refused fail-fast unless it is set, so the manifest itself can never
|
|
grant network access. ``http_get`` optionally injects the transport seam; both are ignored
|
|
for ``file``/``sql`` sources. Call ``materialize_bundle`` directly when the stamp is
|
|
wanted alongside the paths."""
|
|
return list(
|
|
materialize_bundle(
|
|
Path(manifest_path),
|
|
Path(bundle_dir),
|
|
ingested_at,
|
|
allow_network=allow_network,
|
|
http_get=http_get,
|
|
).written
|
|
)
|