Skip to content

K2GO-427 feat(cx-core): content-exchange core (read, export, import) - #675

Merged
luisguzman-adfa merged 7 commits into
mainfrom
feat/K2GO-427-cx-core-read
Oct 22, 2026
Merged

luisguzman-adfa merged 7 commits into
mainfrom
feat/K2GO-427-cx-core-read

Conversation

@luisguzman-adfa

Copy link
Copy Markdown
Collaborator

In-box REST service (cx-core) for granular content exchange between boxes, per
controller/docs/ADR-content-exchange.md (Pillar 0 contracts + Pillars 1-3). Python
stdlib-only, localhost, client-agnostic (dash-node / IIAB Console), portable across
the IIAB systemd and proot architectures.

What it adds (static/cx-core/):

  • Read: GET /v1/apps, /v1/content, /v1/content/{app}/{localRef}. Kiwix, Kolibri,
    Calibre adapters; identity = localRef (export/delete) + dedup (authoritative for
    Kiwix/Kolibri, soft/advisory for Calibre).
  • Export: POST /v1/export/plan (sizes only), GET /v1/export/item (streaming tar,
    bounded memory; staging on the content volume with a disk preflight).
  • Import: POST /v1/dedup/check, /v1/import/validate (partial), /v1/import/item.
    Untrusted-archive guard (member name/type + size/count bounds); delegates to each
    app's own import; Calibre via Calibre-Web upload (calibredb absent).

Verification: 102 unit tests pass, local and on-device (box Python 3.13). Each pillar
had a two-pass code-review; findings fixed.

Not in this PR: the front-side orchestration (dash-node/APK disk flow, phone
preflight, foreground, QR) and the live installed-apps re-read.

Add the in-box content-exchange core (lean Python, stdlib http.server, localhost
only) with the read side of Pillar 1 and the first adapter.

- app-agnostic model + manifest shapes, and the inner adapter port (read methods).
- Kiwix adapter: lists installed ZIMs; identity read not computed (ZIM header UUID
  + line/version from the filename convention).
- GET /v1/apps, GET /v1/content (filter by app), GET /v1/content/{app}/{localRef}.
- Registry loads an adapter only when its app is installed.
- stdlib-unittest for the pure logic (UUID parse, filename decomposition, soft-key
  normalization, manifest shape, route dispatch).

Contracts per controller/docs/ADR-content-exchange.md (Pillar 0, frozen).
- kiwix: an unreadable ZIM header degrades the item to a soft dedup (never a
  confident authoritative match on a missing UUID).
- kiwix: get() resolves the one requested file via a pure containment guard
  (rejects path traversal before any filesystem access), not a full list() scan.
- config: a bad CX_PORT falls back to the default instead of crashing at startup.
- registry: document that installed-state is a startup snapshot; reacting to the
  module lifecycle must consume the install system's single source of truth.
- tests for all of the above.
- kolibri: list installed channels from db.sqlite3 (readonly), identity
  channel_id + integer version (authoritative); size = on-disk available bytes,
  summed over DISTINCT local files (same aggregation as dash-node).
- calibre: list library records from metadata.db (readonly); localRef = books.uuid
  (authoritative-local); dedup = soft key (normalized title + authors + formats +
  byte-size, plus an external identifier when present).
- both wired into the registry; is_installed is a DB-present check; single-item get()
  uses a scoped query (no path, so no containment guard needed).
- unit tests for both (pure builders + temp-sqlite list/get).

Schemas confirmed against a real box; no divergence from the ADR per-app notes.
Pillar 2 export: the core stages one item, the front moves the bytes (D1c).
- port: add ExportStream + open_export (read-only on the source); export only.
- REST: POST /v1/export/plan (manifest entries + exact bytes, no payload moved) and
  GET /v1/export/item/{app}/{localRef} (streams one item, chunked).
- kiwix: the item is the .zim; stream it as-is.
- calibre: tar the book's own directory (its format files, cover, opf).
- kolibri: delegate to its own export (manage exportchannel + exportcontent) into a temp
  tree, then tar-stream it; the content store is content-addressed so import dedups.
- shared streaming helpers (file_stream, tar_stream, resolve_within containment guard).
- unit tests for plan/stream and each adapter's export.

Kolibri export tooling + the Calibre/Kolibri layouts confirmed on a real box.
- server: HTTP/1.1 so the chunked export stream is valid (HTTP/1.0 would leak the
  chunk-size lines into the body); the stream closes the connection explicitly.
- server: an oversized request body returns 413 and closes, never a truncated parse.
- export: tar_stream writes through a bounded queue drained by a producer thread, so a
  large member streams without being buffered whole in RAM.
- kolibri: stage the export under a configured dir on the content volume (not /tmp), and
  preflight free space against the channel size, failing cleanly instead of ENOSPC.
- tests: end-to-end HTTP test (http.client) that reassembles a streamed item and compares
  it to the source, plus the staging-short-on-space case.
… partial

Pillar 3 import: the front places one local payload, the core ingests one item.
- port: add check() (ABSENT/SAME_VERSION/OLDER_HERE/NEWER_HERE) and import_item();
  authoritative verdicts for Kiwix/Kolibri, advisory (soft) for Calibre.
- REST: POST /v1/dedup/check, POST /v1/import/validate (partial is first-class),
  POST /v1/import/item.
- kiwix: place the .zim and re-run the indexer. kolibri: delegate to importchannel disk
  + importcontent disk from the extracted tar. calibre: add through Calibre-Web /upload
  (calibredb absent); credentials from the shared store.
- security: imported archives are untrusted. safe_extract_tar validates every member
  against traversal (D11 escapes_root) and refuses links; payloadPath must be inside the
  import area; free-space preflight before extracting.
- tests: dedup verdicts, validate partial, tar traversal rejection, import_item per app
  via seams.

Box import paths confirmed: iiab-make-kiwix-lib, kolibri importchannel/importcontent disk,
Calibre-Web /upload.
- importing: safe_extract_tar bounds the member count and the summed ACTUAL member
  sizes against free disk (not the untrusted manifest bytes), so a decompression-bomb
  archive that declares a tiny size cannot fill the box.
- calibreweb: upload streams the file as the request body (known Content-Length) instead
  of reading it whole into memory, so a large media book does not OOM.
- calibreweb: the upload filename is sanitized (strip CR/LF and quotes, basename only), so
  an untrusted member name cannot inject multipart headers.
- calibreweb: login confirms the session actually authenticated (remember_token cookie, or
  redirected off the login form), since Calibre-Web re-serves /login with 200 on bad creds.
- stop tracking build artifacts: add static/cx-core/.gitignore and untrack the committed
  __pycache__/*.pyc.
- tests for the tar size/count guard and the filename sanitization.
@luisguzman-adfa
luisguzman-adfa merged commit 81e1242 into main Oct 22, 2026
1 check passed
@luisguzman-adfa
luisguzman-adfa deleted the feat/K2GO-427-cx-core-read branch October 22, 2026 18:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant