Skip to content

latent.serve.knowledge_fetch

Fetch the knowledge bundle an agent version pins, before the pod serves.

A version that pins a bundle sets AGENT_KNOWLEDGE_DIGEST (with AGENT_KNOWLEDGE_BUCKET, AGENT_KNOWLEDGE_PREFIX and AGENT_KNOWLEDGE_DIR). latent serve then, before the HTTP server listens and before it registers:

  1. GETs s3://<bucket>/<prefix>/<hex>.tar.zst with the node's credentials (60 s per attempt, 3 attempts with backoff);
  2. hashes the bytes as they stream in and refuses a digest mismatch;
  3. unpacks into a staging directory inside the knowledge directory, which must be empty or missing, refusing absolute paths, .., symlinks, hard links and special files;
  4. reads manifest.json and refuses an unknown format_version or a writer_contract the image does not read ([knowledge] contracts_read, see :func:latent.config.read_knowledge_contracts);
  5. moves the files into place and writes the marker .knowledge-complete (the digest and format_version) last.

A container restarted inside the same pod finds its emptyDir already holding the bundle. When the marker names the pinned digest, the files are reused without a download, after the manifest checks run again.

Every refusal raises, so the process exits non-zero and never serves with an empty, partial or image-baked knowledge directory. With the digest unset the pod starts as before, on the image's own knowledge.

Needs latent[knowledge] (boto3, zstandard) only when a digest is set.

Classes

BundleManifest

BundleManifest()

manifest.json at the bundle root. Fields beyond these are the writer's.

DigestMismatch

DigestMismatch(expected: str, actual: str)

FetchedKnowledge

FetchedKnowledge(digest: str, manifest: BundleManifest)

The bundle a pod fetched at startup: what it reports and stamps on turns.

KnowledgeDirNotEmpty

KnowledgeDirNotEmpty(directory: Path, holds: str = 'files this pod did not write')

KnowledgeFetchError

KnowledgeFetchError()

A pinned bundle the pod must not serve with.

UnknownFormat

UnknownFormat(format_version: object)

UnsafeArchive

UnsafeArchive(member: str, reason: str)

WriterContractMismatch

WriterContractMismatch(writer_contract: int, contracts_read: frozenset[int])

Functions

fetch_bundle

fetch_bundle(digest: str, bucket: str, prefix: str, target: Path, contracts_read: frozenset[int]) -> BundleManifest

Fetch, verify and unpack the bundle digest into target.

target is created when missing, and reused without a download when its marker names digest (see the module docstring). Raises :class:KnowledgeDirNotEmpty, :class:DigestMismatch, :class:UnsafeArchive, :class:UnknownFormat or :class:WriterContractMismatch; S3 errors propagate once the attempts are spent.

fetch_knowledge_from_env

fetch_knowledge_from_env(environ: Mapping[str, str] = os.environ) -> FetchedKnowledge | None

Fetch the bundle the environment pins, or None when no digest is set.

Attributes

ATTEMPTS

ATTEMPT_TIMEOUT_S

BACKOFF_BASE_S

BUCKET_ENV

DIGEST_ENV

DIR_ENV

KNOWN_FORMAT_VERSIONS

MARKER

PREFIX_ENV

STAGING_PREFIX