Validated wire representation of a session filesystem write scope.
A prefix listed in markdown_only_prefixes carries a nature on top
of its path: it grants Markdown sidecars only. Without it, a documentation
prefix would have to be narrowed by whoever produces the contract, and
every producer would reinvent that filter — the divergence this field
exists to prevent. Such a prefix must also appear in allowed_prefixes;
one that does not is dropped rather than silently granting a wider path.
Source code in packages/axm/src/axm/tools/write_scope.py
| Python |
|---|
| @dataclass(frozen=True)
class WriteContract:
"""Validated wire representation of a session filesystem write scope.
A prefix listed in ``markdown_only_prefixes`` carries a **nature** on top
of its path: it grants Markdown sidecars only. Without it, a documentation
prefix would have to be narrowed by whoever *produces* the contract, and
every producer would reinvent that filter — the divergence this field
exists to prevent. Such a prefix must also appear in ``allowed_prefixes``;
one that does not is dropped rather than silently granting a wider path.
"""
execution_root: str
allowed_prefixes: tuple[str, ...] = ()
markdown_only_prefixes: tuple[str, ...] = ()
@classmethod
def from_mapping(cls, raw: Mapping[str, object]) -> WriteContract:
"""Validate and normalize a transported contract mapping."""
raw_root = raw.get("execution_root")
if not isinstance(raw_root, str) or not raw_root.strip():
raise ValueError("execution_root must be a non-empty string")
root = os.path.realpath(raw_root.strip())
prefixes = _normalized_prefixes(raw, "allowed_prefixes", root)
markdown_only = _normalized_prefixes(raw, "markdown_only_prefixes", root)
return cls(
execution_root=root,
allowed_prefixes=prefixes,
markdown_only_prefixes=tuple(
prefix for prefix in markdown_only if prefix in prefixes
),
)
@classmethod
def from_json(cls, raw: str) -> WriteContract:
"""Decode, validate and normalize a JSON transport payload."""
decoded = json.loads(raw)
if not isinstance(decoded, Mapping):
raise ValueError("write contract must be a JSON object")
return cls.from_mapping(decoded)
def resolve(self, base: str, candidate: str) -> str:
"""Resolve a candidate location relative to an explicit base."""
return _absolute_location(base, candidate)
def permits(self, location: str) -> bool:
"""Return whether a resolved location is under an allowed prefix.
A location granted *only* by Markdown-restricted prefixes must itself
be Markdown: those prefixes carry a nature, not merely a path. A
location also covered by an unrestricted prefix keeps that grant.
"""
granting = [
prefix
for prefix in self.allowed_prefixes
if location == prefix or location.startswith(prefix + os.sep)
]
if not granting:
return False
if any(prefix not in self.markdown_only_prefixes for prefix in granting):
return True
return location.casefold().endswith(_MARKDOWN_SUFFIXES)
def contains(self, location: str) -> bool:
"""Return whether a resolved location is under the execution root."""
root = self.execution_root
return location == root or location.startswith(root + os.sep)
|
Return whether a resolved location is under the execution root.
Source code in packages/axm/src/axm/tools/write_scope.py
| Python |
|---|
| def contains(self, location: str) -> bool:
"""Return whether a resolved location is under the execution root."""
root = self.execution_root
return location == root or location.startswith(root + os.sep)
|
Decode, validate and normalize a JSON transport payload.
Source code in packages/axm/src/axm/tools/write_scope.py
| Python |
|---|
| @classmethod
def from_json(cls, raw: str) -> WriteContract:
"""Decode, validate and normalize a JSON transport payload."""
decoded = json.loads(raw)
if not isinstance(decoded, Mapping):
raise ValueError("write contract must be a JSON object")
return cls.from_mapping(decoded)
|
Validate and normalize a transported contract mapping.
Source code in packages/axm/src/axm/tools/write_scope.py
| Python |
|---|
| @classmethod
def from_mapping(cls, raw: Mapping[str, object]) -> WriteContract:
"""Validate and normalize a transported contract mapping."""
raw_root = raw.get("execution_root")
if not isinstance(raw_root, str) or not raw_root.strip():
raise ValueError("execution_root must be a non-empty string")
root = os.path.realpath(raw_root.strip())
prefixes = _normalized_prefixes(raw, "allowed_prefixes", root)
markdown_only = _normalized_prefixes(raw, "markdown_only_prefixes", root)
return cls(
execution_root=root,
allowed_prefixes=prefixes,
markdown_only_prefixes=tuple(
prefix for prefix in markdown_only if prefix in prefixes
),
)
|
Return whether a resolved location is under an allowed prefix.
A location granted only by Markdown-restricted prefixes must itself
be Markdown: those prefixes carry a nature, not merely a path. A
location also covered by an unrestricted prefix keeps that grant.
Source code in packages/axm/src/axm/tools/write_scope.py
| Python |
|---|
| def permits(self, location: str) -> bool:
"""Return whether a resolved location is under an allowed prefix.
A location granted *only* by Markdown-restricted prefixes must itself
be Markdown: those prefixes carry a nature, not merely a path. A
location also covered by an unrestricted prefix keeps that grant.
"""
granting = [
prefix
for prefix in self.allowed_prefixes
if location == prefix or location.startswith(prefix + os.sep)
]
if not granting:
return False
if any(prefix not in self.markdown_only_prefixes for prefix in granting):
return True
return location.casefold().endswith(_MARKDOWN_SUFFIXES)
|
Resolve a candidate location relative to an explicit base.
Source code in packages/axm/src/axm/tools/write_scope.py
| Python |
|---|
| def resolve(self, base: str, candidate: str) -> str:
"""Resolve a candidate location relative to an explicit base."""
return _absolute_location(base, candidate)
|