topmark.config.policy¶
Policy model for TopMark (global and per-file-type).
This module defines the policy layer used to control run intent and safety rules, both globally and with per-file-type overrides.
Design
MutablePolicyuses tri-state options (bool | None/Enum | None) to represent explicit values versus unset. This enables non-destructive merges and inheritance when composing multiple sources (defaults -> user -> project -> CLI).FrozenPolicyis the fully resolved, immutable runtime view with plain values, so pipeline steps do not branch onNone.MutablePolicy.resolve(base)fills unset fields frombaseand returns an immutableFrozenPolicy; use it atMutableConfig.freeze()time.
TOML mapping:
```toml
[policy]
header_mutation_mode = "all"
allow_header_in_empty_files = false
empty_insert_mode = "logical_empty"
allow_content_probe = true
[policy_by_type.python]
allow_header_in_empty_files = true
```
HeaderMutationMode ¶
Bases: str, Enum
Defines how headers may be mutated.
Attributes:
| Name | Type | Description |
|---|---|---|
ALL |
Allow both inserting missing headers and updating existing headers (default). |
|
ADD_ONLY |
Only add headers when no header present. |
|
UPDATE_ONLY |
Only update existing headers. |
EmptyInsertMode ¶
Bases: str, Enum
How TopMark classifies "empty" inputs for header insertion.
The selected mode determines which files count as "empty" when evaluating
Policy.allow_header_in_empty_files.
Attributes:
| Name | Type | Description |
|---|---|---|
BYTES_EMPTY |
Only true 0-byte files ( |
|
LOGICAL_EMPTY |
0-byte files plus logically-empty placeholders (optional BOM, optional horizontal whitespace, and at most one trailing newline). |
|
WHITESPACE_EMPTY |
0-byte files plus any effectively-empty decoded image (no non-whitespace characters; whitespace/newlines allowed). |
FrozenPolicy
dataclass
¶
FrozenPolicy(
*,
header_mutation_mode=HeaderMutationMode.ALL,
allow_header_in_empty_files=False,
empty_insert_mode=EmptyInsertMode.LOGICAL_EMPTY,
render_empty_header_when_no_fields=False,
allow_reflow=False,
allow_content_probe=True,
)
Immutable, runtime policy used by processing steps.
Attributes:
| Name | Type | Description |
|---|---|---|
header_mutation_mode |
HeaderMutationMode
|
Defines how headers may be mutated: process all files if ( |
allow_header_in_empty_files |
bool
|
Allow inserting headers into files that are
classified as empty under |
empty_insert_mode |
EmptyInsertMode
|
Defines which files are considered "empty" for
insertion policy ( |
render_empty_header_when_no_fields |
bool
|
Allow inserting an otherwise empty header when no fields are configured. |
allow_reflow |
bool
|
Allow reflowing file content when inserting a header. Enabling this can break check/strip idempotence. |
allow_content_probe |
bool
|
Whether the resolver may consult file contents
during file-type detection. |
thaw ¶
Return a mutable builder initialized from this immutable policy.
Returns:
| Type | Description |
|---|---|
MutablePolicy
|
A tri-state mutable policy. |
Source code in src/topmark/config/policy.py
to_dict ¶
Serialize this resolved policy to a TOML-friendly dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
Dictionary containing primitive TOML-serializable values. |
Notes
This is an export helper for documentation, machine payloads, and config rendering.
Resolved FrozenPolicy instances always emit
all fields.
Source code in src/topmark/config/policy.py
MutablePolicy
dataclass
¶
MutablePolicy(
*,
header_mutation_mode=None,
allow_header_in_empty_files=None,
empty_insert_mode=None,
render_empty_header_when_no_fields=None,
allow_reflow=None,
allow_content_probe=None,
)
Mutable policy builder suitable for config loading/merging.
This class is merged in a last-wins manner when reading multiple config files.
Attributes:
| Name | Type | Description |
|---|---|---|
header_mutation_mode |
HeaderMutationMode | None
|
See |
allow_header_in_empty_files |
bool | None
|
See |
empty_insert_mode |
EmptyInsertMode | None
|
See |
render_empty_header_when_no_fields |
bool | None
|
See |
allow_reflow |
bool | None
|
See |
allow_content_probe |
bool | None
|
See |
merge_with ¶
Return a new MutablePolicy by applying other over self (last-wins).
None fields in other do not override explicit values in self.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
MutablePolicy
|
The policy whose values override current ones. |
required |
Returns:
| Type | Description |
|---|---|
MutablePolicy
|
Merged policy. |
Source code in src/topmark/config/policy.py
resolve ¶
Resolve tri-state fields against a base immutable policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base
|
FrozenPolicy
|
Base policy that provides defaults for unset fields. |
required |
Returns:
| Type | Description |
|---|---|
FrozenPolicy
|
A fully-resolved immutable policy with plain booleans. |
Source code in src/topmark/config/policy.py
freeze ¶
Freeze to a concrete FrozenPolicy using the built-in policy defaults.
Returns:
| Type | Description |
|---|---|
FrozenPolicy
|
Fully resolved immutable policy. |
Notes
This is equivalent to resolving against
FrozenPolicy().
Source code in src/topmark/config/policy.py
from_toml_table
classmethod
¶
Create a MutablePolicy from a TOML table.
Unspecified keys remain None so they can inherit from the base policy during resolve().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tbl
|
Mapping[str, object] | None
|
TOML table mapping for |
required |
Returns:
| Type | Description |
|---|---|
MutablePolicy
|
Parsed mutable policy. |
Source code in src/topmark/config/policy.py
HasPolicyConfig ¶
Bases: Protocol
Read-only view of resolved policy configuration.
This protocol captures the minimum surface required by helpers like
make_policy_registry
and effective_frozen_policy.
It intentionally avoids importing the concrete
FrozenConfig /
MutableConfig
classes to prevent type-check-time import cycles.
Implementations are expected to expose resolved runtime policies:
- policy is a fully resolved FrozenPolicy
- policy_by_type maps file-type identifiers to resolved per-type
FrozenPolicy
Attributes are defined as read-only properties so both immutable
FrozenConfig and mutable
MutableConfig builders
can satisfy the protocol.
PolicyRegistry
dataclass
¶
Immutable registry of effective policies per file type.
Instances of this class are derived from a resolved
FrozenConfig and provide constant-time
lookup of the effective FrozenPolicy
to apply for a given file type.
for_type ¶
Return the effective policy for the given file-type name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
File type name, or None for the global/default case. |
required |
Returns:
| Type | Description |
|---|---|
FrozenPolicy
|
The resolved per-type policy if present; otherwise the global policy. |
Source code in src/topmark/config/policy.py
policy_to_dict ¶
Serialize a resolved or tri-state policy to a TOML-friendly dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
policy
|
FrozenPolicy
|
Frozen |
required |
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
Dictionary containing all policy fields as TOML-friendly primitives. Enum values are |
dict[str, object]
|
serialized via |
Source code in src/topmark/config/policy.py
make_policy_registry ¶
Build an immutable PolicyRegistry from a resolved config-like object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
HasPolicyConfig
|
Resolved configuration object exposing global and per-type policy
mappings through the |
required |
Returns:
| Type | Description |
|---|---|
PolicyRegistry
|
Immutable effective policy registry. |
Source code in src/topmark/config/policy.py
effective_frozen_policy ¶
Return the effective policy for a given file type.
Per-type overrides take precedence over the global policy. If file_type_id
is None or no per-type policy exists for that identifier, the global policy
is returned.
This helper assumes that both cfg.policy and entries in cfg.policy_by_type
are already fully resolved FrozenPolicy
instances (that is, no tri-state inheritance remains at runtime).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
HasPolicyConfig
|
Resolved runtime configuration. |
required |
file_type_id
|
str | None
|
File type identifier (for example, |
required |
Returns:
| Type | Description |
|---|---|
FrozenPolicy
|
Effective policy to use for processing. |