Skip to content

config

config

Configuration, tree location and the closed failure taxonomy of the files backend.

Nothing in this module touches the filesystem: the factory's validation is cheap and pure, and :func:resolve_files_location only computes paths.

FILES_SUBTYPES is the closed set of reasons a files-backend call can fail, each mapped onto its errors/1 code. The spec's fourteen subtypes are kept; wave A adds five, each named here:

  • cursor_invalid (E7): a recall cursor that was forged, tampered with, or minted against a tree that has changed since.
  • fact_invalid (E7): fact text the tier format cannot store (empty, or a line that looks like a fact marker), or a tier name that is not a plain *.md file name.
  • state_corrupt (E5): the backend's own state.json cannot be parsed.
  • argument_invalid (E7): a call argument out of range (fetch offset inside a UTF-8 character, a non-positive limit...).
  • storage_error (E5): the operating system refused a read or write.

Classes

FilesBackendConfig

Bases: BaseModel

One files-backend attachment, as the gateway hands it to the factory.

FilesLocation dataclass

FilesLocation(memory_tree, attachment_dir, state_root)

Where one scope's files live: the tier tree, its attachment dir, the state root.

Functions:

files_error

files_error(subtype, detail, *, remedy='')

The typed errors/1 exception for a files-backend subtype.

Source code in src/marianne/memory/backends/files/config.py
def files_error(subtype: str, detail: str, *, remedy: str = "") -> MemoryBackendError:
    """The typed ``errors/1`` exception for a files-backend ``subtype``."""
    code = FILES_SUBTYPES[subtype]
    return ERROR_CLASSES[code](detail, subtype=subtype, remedy=remedy)

default_files_root

default_files_root()

~/.marianne/memory/files for the current user.

Source code in src/marianne/memory/backends/files/config.py
def default_files_root() -> Path:
    """``~/.marianne/memory/files`` for the current user."""
    return Path.home() / ".marianne" / "memory" / "files"

resolve_files_location

resolve_files_location(scope, root=None)

Map a scope onto its scope-family tree under root.

project/demo -> <root>/project/demo; local:default -> <root>/local/default; organization/x -> <root>/org/x. Writer state for every tree lives under <root>/trees. A scope that would resolve outside root is refused (E3 scope_unknown). No I/O.

Source code in src/marianne/memory/backends/files/config.py
def resolve_files_location(scope: str, root: Path | str | None = None) -> FilesLocation:
    """Map a scope onto its scope-family tree under ``root``.

    ``project/demo`` -> ``<root>/project/demo``; ``local:default`` ->
    ``<root>/local/default``; ``organization/x`` -> ``<root>/org/x``. Writer
    state for every tree lives under ``<root>/trees``. A scope that would
    resolve outside ``root`` is refused (E3 ``scope_unknown``). No I/O.
    """
    try:
        key = parse_scope(scope)
    except ScopeRefused as exc:
        raise files_error("scope_unknown", exc.detail, remedy=exc.remedy) from None
    chosen = root if root is not None else default_files_root()
    base = os.path.normpath(os.path.abspath(os.fspath(chosen)))
    tree = os.path.normpath(os.path.join(base, key.family, key.ident))
    if os.path.dirname(os.path.dirname(tree)) != base:
        raise files_error(
            "scope_unknown",
            f"scope {key.canonical!r} resolves outside the memory root",
            remedy="use a scope id made of a-z 0-9 _ . - that is not a path",
        )
    return FilesLocation(
        memory_tree=tree,
        attachment_dir=tree,
        state_root=os.path.join(base, "trees"),
    )