# Publish ## Purpose `internal/publish` plans and executes publication for one validated source bundle and one destination. ## Inputs and outputs Inputs are a source bundle, source backend, destination backend, pipeline id, destination id, publish policy, transform policy, transformer resolver, transfer policy, destination bundle path, and existing destination state. Output is a plan with an action, reason, and selected source or generated outputs. Execution writes selected source files, generated files, and `.distributor.json` for publish or replacement actions. ## Actions Supported actions are `publish_new`, `replace_older`, `skip_same`, `skip_destination_newer`, `fail_conflict`, and `fail_unmanaged`. ## Failure behavior Planning fails when request fields are incomplete, publish and transform policies are invalid, selected outputs collide, HTML output is requested without Markdown inputs, destination state is invalid, destination content is unmanaged, or transfer policy maps the comparison outcome to failure. Execution fails if a write, delete, state serialization, or context check fails. Outputs written during a failed publish attempt are cleaned up through managed deletion where possible. ## Boundaries The current implementation publishes source files and Markdown-to-HTML sidecar outputs. Backend behavior is supplied through `internal/storage`; app runtime currently supplies local backends. The package uses `internal/state` for destination comparison, `internal/storage` for IO, and the shared `internal/config` publish/transform policy helper for request validation. It resolves transforms through a narrow resolver supplied by the caller; concrete transform registration is owned by the app layer. It does not parse CLI flags or load config files. ## Safety Replacement deletes only outputs recorded in existing destination state plus `.distributor.json`. Failed local writes trigger cleanup of outputs written during the failed attempt. ## Tests Before changing publish behavior, inspect tests under `internal/publish` and local run tests under `internal/app`. ## Invariants - Publish planning is deterministic for the same source, destination state, policies, and transform outputs. - Replacement deletes only managed paths recorded in existing state plus `.distributor.json`. - Publish execution writes destination state after selected outputs are written. - Transform implementations are resolved through an interface supplied by the caller. - Unmanaged destination content is never overwritten.