state
napt.state
State persistence for NAPT.
Deployment state (napt.state.deployment) is the authoritative
per-app record (state/deployment/<recipe-id>.json) of what NAPT has
published to Intune and what is awaiting publication. Not regenerable.
Serialized deterministically so unchanged state produces byte-identical
files and clean diffs.
The provenance stamp (napt.state.stamp) is the join between deployment state and Intune: a machine-parseable line in each published app's notes field that marks the app as NAPT-managed and ties it to the publish instance recorded here.
napt discover --stateless neither reads nor writes deployment state.
napt.state.deployment
Deployment state persistence for NAPT.
This module implements per-app deployment state: authoritative records of what NAPT has published to Intune and what is awaiting publication. Unlike the downloads folder, deployment state is not regenerable.
Each app gets its own file, state/deployment/<recipe-id>.json, so that
concurrent changes to different apps never conflict and each file's diff
is scoped to one app. A file names its app once at the top (app_id
matching the filename, plus the recipe's display name, refreshed on
every save) and holds five sections:
published: The release currently in Intune, with its SHA-256 hash and Intune app IDs. Null until the first upload. Publishing uploads the release without assigning it;napt promotedeploys it through the rings afterwards.install_assigned: The release the install entry is currently assigned to (the result of apromoteplan'sassignaction).pending: The discovered release awaiting publication, with version, download URL, and SHA-256 hash. A single slot: a newer discovery replaces an unpublished candidate (newest wins). Null when nothing is awaiting publication.rings: Which version currently holds each deployment ring. Written bynapt promote.retained: Displaced versions kept in Intune for rollback.
Serialization is deterministic (fixed reading-order keys, fixed
indentation, no timestamps), so re-running a command that produces no
logical change produces a byte-identical file and a clean git diff.
Keys follow reading order (lifecycle order at the top level, version
first and hashes last inside blocks) because these files are what a
publish PR diff shows its reviewer.
deployment_state_path
Returns the deployment state file path for a recipe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_dir
|
Path
|
Directory holding per-app deployment state files
(typically |
required |
recipe_id
|
str
|
Recipe identifier (from recipe's 'id' field). |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path to the app's deployment state file. |
Source code in napt/state/deployment.py
working_release
Reads the release the pipeline commands act on for an app.
That is the pending release (awaiting publication), or the published
release when nothing is pending, which is the case when rebuilding,
re-packaging, or re-uploading the current version. napt build,
napt package, and napt upload all resolve their input from this
entry and verify it by hash, so none of them has to guess which folder
on disk is the right one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_dir
|
Path
|
State root ( |
required |
recipe_id
|
str
|
Recipe identifier (from recipe's 'id' field). |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
The release entry ( |
Raises:
| Type | Description |
|---|---|
StateError
|
If the deployment state file exists but is corrupted or has an unsupported schema version. |
Source code in napt/state/deployment.py
working_release_of
Reads the release the pipeline commands act on from a loaded state.
The same rule as working_release, for a caller that already holds the state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
dict[str, Any]
|
A loaded deployment state. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
The pending release, else the published one, else None. |
Source code in napt/state/deployment.py
create_default_deployment_state
Creates an empty deployment state structure.
The identity fields (app_id, name) are stamped at save time:
app_id from the filename, name by whichever writer holds the
recipe configuration.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Deployment state with no published release, no pending release, |
dict[str, Any]
|
no ring assignments, and no retained versions. |
Source code in napt/state/deployment.py
load_deployment_state
Loads deployment state for one app.
Returns a default empty structure when the file does not exist. Does not create the file; deployment state is only written when there is something to record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_path
|
Path
|
Path to the app's deployment state file. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Deployment state dictionary. |
Raises:
| Type | Description |
|---|---|
StateError
|
If the file exists but contains invalid JSON, its schemaVersion is missing or unsupported, its declared app_id disagrees with the filename, or a section does not have the shape NAPT writes (a hand edit). Deployment state is authoritative, so a corrupted file is never silently replaced. |
Source code in napt/state/deployment.py
load_deployment_states
Loads the deployment state of every app in a run, once.
A promote run reads each app's state in several steps (reconciling publications, planning, applying, checking drift). Loading the files here and handing the same dicts to each step means one read per app, and a step that records a change (reconciliation, apply) updates the dict the later steps see.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
deployment_dir
|
Path
|
Directory holding per-app deployment state files. |
required |
app_ids
|
Iterable[str]
|
The recipe ids in the run, in the order to keep. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, Any]]
|
Each app's deployment state keyed by app id; an app without a state file gets the default empty state. |
Raises:
| Type | Description |
|---|---|
StateError
|
If a state file is corrupted (see load_deployment_state). |
Source code in napt/state/deployment.py
save_deployment_state
Saves deployment state for one app deterministically.
Creates parent directories if needed. Stamps the schema version and
the app_id (from the filename, which is the identity). Output is
byte-identical for logically identical state: keys follow reading
order, indentation is fixed at 2 spaces, rings are sorted by name,
and no timestamps or run-specific values are written. The file lands
in one rename, so a failed write leaves the previous file in place.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
dict[str, Any]
|
Deployment state dictionary to save. |
required |
state_path
|
Path
|
Path to the app's deployment state file. |
required |
Raises:
| Type | Description |
|---|---|
OSError
|
If the file cannot be written due to permissions. |
Source code in napt/state/deployment.py
record_pending
Records a discovered release as the pending publication candidate.
The pending slot holds exactly one candidate and the newest discovery wins: a release that differs from both the published release and the current pending candidate replaces the pending candidate. Identity is the SHA-256 hash, not the version string, so a vendor re-release of the same version with a different binary is treated as new.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
dict[str, Any]
|
Deployment state dictionary to update in place. |
required |
version
|
str
|
Discovered version string. |
required |
sha256
|
str
|
SHA-256 hash of the discovered installer. |
required |
url
|
str
|
Download URL of the discovered installer. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
A string naming the change made ("recorded" for a first candidate, "replaced" when a candidate was overwritten, "cleared" when the vendor serves the already-published release), or None when the state did not change. |
Source code in napt/state/deployment.py
record_published
record_published(
state: dict[str, Any],
version: str,
sha256: str,
intune_app_id: str | None,
intune_update_app_id: str | None,
) -> None
Records a successful publication as the published release.
Replaces the published section and clears the pending slot when
the pending candidate is the release that was just published. A
pending candidate with a different hash (a newer discovery) is left
in place. A release rolled back to is removed from the retained
list: the published release is not a rollback candidate, and leaving
it there would let retention count it and mis-order the list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
dict[str, Any]
|
Deployment state dictionary to update in place. |
required |
version
|
str
|
Published version string. |
required |
sha256
|
str
|
SHA-256 hash of the published release's installer. |
required |
intune_app_id
|
str | None
|
Graph API object ID of the install entry, or None when build_types is "update_only". |
required |
intune_update_app_id
|
str | None
|
Graph API object ID of the update entry, or None when build_types is "app_only". |
required |
Source code in napt/state/deployment.py
summarize_deployment_states
Summarizes all per-app deployment state files in a directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
deployment_dir
|
Path
|
Directory holding per-app deployment state files. |
required |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
One summary dict per app, sorted by app id, each with the app id,
published version, pending version, whether the pending
version is lower than the published one
( |
Raises:
| Type | Description |
|---|---|
StateError
|
On a corrupted deployment state file. |
Source code in napt/state/deployment.py
napt.state.stamp
Provenance stamp for NAPT-managed Intune apps.
The stamp is a single machine-parseable line written to the Intune notes field of every app NAPT publishes:
napt/v1 id=<recipe-id> entry=<install|update> sha256=<installer-hash>
It serves two purposes: ownership (presence of the stamp marks an app as NAPT-managed; unstamped apps are never touched) and identity (the recipe id, entry type, and installer hash tie the Intune object to a specific publish instance recorded in deployment state). The notes field is reserved for NAPT and is not recipe-configurable.
build_stamp
Builds the provenance stamp for one Intune app entry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
recipe_id
|
str
|
Recipe identifier (from recipe's 'id' field). |
required |
entry
|
str
|
Entry type, either "install" or "update". |
required |
sha256
|
str
|
SHA-256 hex digest of the source installer. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The stamp line to write to the Intune notes field. |
Raises:
| Type | Description |
|---|---|
ConfigError
|
If the stamp would exceed Intune's notes field length limit (only possible with an extremely long recipe id). |
Source code in napt/state/stamp.py
find_stamped_app
Finds the app whose provenance stamp matches a publish instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
apps
|
list[dict]
|
Mobile app dicts (with "notes") from list_mobile_apps. |
required |
recipe_id
|
str
|
Recipe identifier to match. |
required |
entry
|
str
|
Entry type to match ("install" or "update"). |
required |
sha256
|
str
|
Installer hash to match. |
required |
Returns:
| Type | Description |
|---|---|
dict | None
|
The matching app dict, or None when no stamped app matches. |
Source code in napt/state/stamp.py
parse_stamp
Parses a provenance stamp from an Intune notes field value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
notes
|
str | None
|
The notes field content, or None. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str] | None
|
A dict with "id", "entry", and "sha256" keys, or None when the notes do not carry a complete NAPT stamp. |