# Upgrade application files

Reconcile the files your application explicitly manages between a predecessor
and a candidate snapshot. Use `planAppUpgrade` and `applyAppUpgrade` from
`@openlup/platform/lifecycle`, and keep application-specific brand, content,
policy and integration composition outside that managed inventory.

For a starter application, first follow the owned-file migration in the applicable
[per-release notes](README.md#per-release-notes).

## Prepare snapshots

Keep immutable `from` and `to` values of type `AppVersionSnapshot`. Each names
a version, an inclusive schema range (`schema.min` and `schema.max`) and a
`files` record mapping relative paths to exact UTF-8 contents. Include a file
only in `to` to create it, or only in `from` to remove it.

Pass both snapshots, `root` and `schemaVersion` as `AppUpgradeInput`. Obtain
`root` with `realpath` from `node:fs/promises`: it must be the absolute canonical
path of an existing ordinary application directory. For `schemaVersion` or
`--schema-version`, use `(await runner.status()).applied.length` from the
migration runner for the actual database and the same installation profile.
Use the predecessor observation when planning against that state. Package
versions and the migration manifest's format version are separate from the
applied ledger count. If `status()` refuses, resolve catalogue or ledger
admission for that database and profile before using a count.

The count must be a nonnegative safe integer inside both inclusive ranges.
If the ranges do not overlap, stop the hop and use a documented intermediate
hop only if one exists. If the database needs a forward migration, follow the
[application migration chain](database-migrations.md) and observe again.
The file commands trust the supplied ordinal and perform no database probe.

## Plan and reconcile conflicts

Call `planAppUpgrade(input)` and inspect its `files` actions and `conflicts`.
With `openlup upgrade plan --json`, read the top-level `conflicts` array and
the findings for each refusal.
Planning only reads the application tree. The upgrade preserves local edits to
files that did not change between the snapshots, and
refuses divergent changed files before writes: a managed file whose local edits
match neither snapshot is a conflict. Reconciliation follows these rules:

| Snapshot and current contents | Result |
| --- | --- |
| The file is identical in both snapshots | Keep local contents or local deletion; report `unchanged` or `preserved`. |
| A changed path already has exact candidate bytes, or an intended removal is already absent | Report `already-applied`. |
| A changed path matches the predecessor, including absence for a creation | Plan `create`, `replace` or `remove`. |
| A changed path matches neither snapshot | Add a conflict: `current bytes match neither predecessor nor candidate`. |
| A path is unlisted in both snapshots | Leave it application-owned. |

Comparison uses exact bytes, without normalizing text or line endings. Reconcile
a divergent managed file before applying; the primitive does not merge local
edits. Replan after reconciliation and retain the final snapshot pair for
recovery.

Unsafe paths, case or Unicode-normalization aliases, file-parent collisions,
symbolic links and nonregular managed files refuse. Directory prefixes must
have one exact spelling across both snapshots, including directories that do
not yet exist. Existing managed components must match their directory-entry
spelling. An alias does not make an unlisted entry managed.
Preserve and move a structural obstruction without following it. Restore the
exact predecessor or candidate state with the expected spelling, including
absence for a target-only creation, then rerun the plan.

## Apply in a maintenance window

Hold an exclusive maintenance window over the application tree before calling
`applyAppUpgrade(input)`. Prevent other application writers and editors from
changing it until the operation and any recovery finish. The primitive does
not acquire a filesystem lock or defend against a hostile concurrent writer.

Apply inspects the whole input again before writing. A previously clean plan
does not bypass this preflight. If conflicts remain, apply throws
`AppUpgradeConflictError` before any file or directory mutation. Invalid
snapshot or path structure can throw an ordinary error instead.

Each create or replacement writes a temporary file in the destination's
directory and renames it into place. Removal unlinks the managed file.
Individual replacements are atomic on a local filesystem; the complete upgrade
is not a transaction. Replacement preserves ordinary permission bits, but not
ownership, access-control lists, extended attributes or special permission
bits. These operations do not establish power-loss durability.

## Resume interrupted work

After an I/O failure or process interruption, keep the tree exclusive and call
apply again with the same immutable predecessor and candidate snapshots. Exact
completed creates, replacements and removals are recognized as `already-applied`;
remaining actions continue. A third byte value still conflicts. Reconstructing
the predecessor from the partly upgraded tree would lose the original comparison.

Abrupt termination before rename can leave an unreferenced temporary file. It
is not adopted as a managed file or used as resume state.

## Verify the rest of the upgrade

File application performs no SQL, dependency installation, version-marker
write, deployment or user hook. Schema-range admission only compares
caller-supplied numbers. It certifies neither a real database upgrade or
backfill nor dependency provenance, runtime behavior or a rollback path.
Perform [database migration](./database-migrations.md) and application checks
separately before resuming service. After reconciling application files, run
`./node_modules/.bin/openlup settings check --app ./owned/settings-host.ts`
(or pass `check` to your starter's `settings` script)
to validate owned business settings offline before running full tests and
starting the application.

The set-release preflight exercises this same transition before publication
against retained digest-verified local candidate packs; a failure blocks the
dispatch. Its receipt labels the target `candidate, local digest-verified`.
Published provenance and post-publication qualification remain separate.

The published-set qualification also exercises the neutral starter from the
previous complete published set through the target release's public upgrade
CLI. Managed snapshots come from each starter's ownership inventory. The check
first follows the applicable starter migration in the
[per-release notes](README.md#per-release-notes); before release preparation
publishes it there, a pending migration is in the source repository's
unreleased Core change notes. This includes the
[0.43.0 transition](v1-0-0.md#starter-applications-from-0430) and the
[1.1.0 to 1.2.0 migration](v1-2-0.md#starter-applications-from-110).
The notes identify pristine owned files that take
the target extraction's bytes and any owned paths created via target-only
snapshot entries. The check then customizes owned
theme and fragment files, refuses a divergent managed file,
interrupts apply with a process kill, resumes with identical snapshots and
requires replay to make no changes. A harness check refuses incomplete file
or declared-versus-installed dependency state; this is not an automatic serving
guard added to the application by the CLI.

For a starter with adopter catalogue packs, follow the applicable owned-module
migration in the [per-release notes](README.md#per-release-notes), then run
`./lifecycle up --json` to rebuild the application and restore the verified
builds. Compare the reported pack and type versions with the saved draft and
repeat the [operator action](../how-to/extend/catalogue-extension.md#install-a-pack-in-the-starter).

The harness then installs the target's exact dependencies using a regenerated,
verified lock and checks typechecking, build, application tests and the local
development loop. Owned customization bytes must survive. The receipt names
both versions, extraction identities, the plan digest and phase results. The
previous set is the greatest stable core release tag below the target visible
in every package's npm version history. Historical starters are explicitly
labelled reconstructed. This proves only the checked neutral-starter file and
dependency transition; it provides no SQL migration, browser/provider coverage,
rollback guarantee or stable/support claim.
