Skip to content

API Reference parrot_formdesigner.services.form_version

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot_formdesigner.services.form_version

View source

FormVersionService — immutable semver publishing for FormSchema objects.

Implements the form publishing lifecycle described in FEAT-300 §2 (RF-06):

  • Publishing promotes the current live version to published, IN PLACE (FEAT-433 §8 Q5, closed 2026-08-19) — it does not bump to a new tag.
  • Published snapshots are immutable — re-promoting raises ValueError.
  • In-flight responses resolve against the version they started with.
  • Deletion of a form/version with associated responses is blocked (caller provides a has_responses hook).

FEAT-300 — Module 4. FEAT-433 — Modules 3, 5, 6.

Alias FormRegistry

Re-exported from parrot_formdesigner.services.registry.FormRegistry.

Alias FormSchema

Re-exported from parrot_formdesigner.core.schema.FormSchema.

Alias FormStorage

Re-exported from parrot_formdesigner.services.registry.FormStorage.

Class FormVersionService

Immutable semver publishing service for FormSchema objects.

Each call to :meth:publish promotes the CURRENT live version to published, IN PLACE (FEAT-433 §8 Q5, closed 2026-08-19) — it no longer bumps to a new tag. The next editor SAVE is what bumps to a new draft (_bump / api/_utils._bump_version). The snapshot is stored via storage.promote() when available; an in-memory fallback (dict) is used otherwise (suitable for tests and development).

Deletion is guarded by the optional has_responses async hook: if supplied, the service calls it before any delete to confirm no response data exists. If the hook returns True, deletion raises ValueError and the form is only deactivated (not deleted).

Example::

svc = FormVersionService(registry, storage)
# form.version == "1.5" (the editor's current draft)
tag = await svc.publish(form.form_uid, tenant="navigator")  # → "1.5"
snap = await svc.get_published(form.form_uid, version="1.5", tenant="navigator")
Parameter Type Default Description
registry FormRegistry FormRegistry used to look up the live form state and register snapshots when a storage backend is not available.
storage FormStorage | None None FormStorage used to persist snapshots. When None, the service stores snapshots in an in-memory dict.
has_responses Callable[[str, str], Awaitable[bool]] | None None Optional async callback (form_uid, tenant) -> bool that returns True when the form/version has associated responses. When True is returned, deletion is blocked.

Method FormVersionService.publish

Promote the current live version to published, in place.

FEAT-433 Module 6 (spec §8 Q5, closed 2026-08-19): no longer bumps to a new tag. Publishing draft "1.5" produces published "1.5" (not "1.6") — the version the user is looking at is the version that gets published. The next editor SAVE is what bumps to a new draft. This produces one row per actual change, instead of a content-identical draft/published twin pair for every publish.

Steps:

  1. Load the live form from the registry.
  2. Raise ValueError if the live version is already published (immutability).
  3. Set published_version (= the unchanged version) on a deep copy.
  4. Promote the existing row in place (storage) or record it (in-memory).

publish(form_uid: str, *, tenant: str, bump: str = 'minor') -> str

Parameter Type Default Description
form_uid str The form's immutable UUID (FEAT-389).
tenant str Tenant slug.
bump str 'minor' Accepted for backward API compatibility. UNUSED — publish() no longer bumps (see above); version bumping now happens only on the editor's save path (_bump / api/_utils._bump_version).

Returns: str — The published version string — the SAME tag the live form was already at.

Raises:

  • KeyError — If the form is not found in the registry.
  • ValueError — If the live version is already published (immutable).

Method FormVersionService.get_published

Retrieve the stored snapshot for a version — draft or published.

FEAT-433 Module 5: despite the name (kept for API compatibility — renaming is optional per the spec, and this is public API), this no longer requires version to be the published tag. Before this change the snap.published_version == version filter meant GET .../versions/{version} 404d for every version the editor ever saved directly (i.e. every draft) — the exact rows Module 1-3 now lists. Every listed version must resolve here, or the history list is a list of dead links (the anti-regression this task exists for). The returned FormSchema still carries its own published_version, so a caller can tell draft from published without a second call. Immutability for an actually-published snapshot (RF-06 — untouched by subsequent publishes) is unaffected: dropping the filter changes what is returned, not whether a published row can be overwritten (that guarantee is Module 6 / TASK-2269's job).

get_published(form_uid: str, *, version: str, tenant: str) -> FormSchema | None

Parameter Type Default Description
form_uid str The form's immutable UUID (FEAT-389).
version str The semver tag to retrieve (e.g. "1.1").
tenant str Tenant slug.

Returns: FormSchema | None — The stored FormSchema snapshot at version, or None if no such version was ever stored.

Method FormVersionService.list_versions

List every stored version of a form, draft and published (D1/D3).

Merges the in-process VersionMeta cache with a single ordered query against storage (:meth:FormStorage.list_versions, FEAT-433 Module 2), so history survives a process restart (the snapshots live in Postgres under UNIQUE(form_uid, version); published_at is recovered from the stamp written into snapshot.meta by :meth:publish). Replaces the old per-candidate-version probing loop, which cost one round-trip per version and silently truncated history across any two-version gap.

FEAT-433 Module 3 (D1/D3): published_version == version no longer decides whether a row is listed — every stored row is, including rows the editor saved directly via storage.save() (no publish() call). It decides how the row is labelled (:func:_is_published_label): is_published (and is_frozen, which mirrors it).

On conflict between the in-process cache and a storage row for the same version, the storage row wins — a _meta entry is only an in-process echo of a publish that already wrote a row, so a restart never changes the answer.

list_versions(form_uid: str, *, tenant: str) -> list[VersionMeta]

Parameter Type Default Description
form_uid str The form's immutable UUID (FEAT-389).
tenant str Tenant slug.

Returns: list[VersionMeta] — List of VersionMeta objects ordered by (major, minor).

Method FormVersionService.can_delete

Return True if deletion is safe (no responses associated).

If no has_responses hook was provided, deletion is always considered safe (returns True).

can_delete(form_uid: str, *, tenant: str) -> bool

Parameter Type Default Description
form_uid str The form's immutable UUID (FEAT-389).
tenant str Tenant slug.

Returns: bool — True if deletion is permitted.

Method FormVersionService.safe_delete

Delete a form only if it has no responses.

safe_delete(form_uid: str, *, tenant: str) -> None

Parameter Type Default Description
form_uid str
tenant str

Raises:

  • ValueError — If has_responses returns True for this form.

Method FormVersionService.backfill_published

Backfill pre-existing forms as published v1.0 snapshots.

For every form whose published_version is None, marks it as published at its current version value (default "1.0"). Already-backfilled forms (published_version is not None) are skipped — the operation is idempotent.

This resolves decision C3 (spec §8): forms created before FEAT-300 had no version history. Running this migration once enables FormVersionService on tenants with pre-existing forms.

backfill_published(*, tenant: str, dry_run: bool = False) -> int

Parameter Type Default Description
tenant str Tenant slug to backfill.
dry_run bool False If True, logs what would change but persists nothing.

Returns: int — Number of forms that were (or would be) backfilled.

Class VersionMeta

Metadata record for one stored form version (FEAT-433 D1/Module 3).

Fields:

Field Type Default Description
form_id str The form's human-readable slug (FEAT-389 — display / search only; NOT the canonical identifier). The canonical, immutable identifier is FormSchema.form_uid, used as the lookup key everywhere in this service; VersionMeta keeps form_id purely as a friendlier label for callers.
version str The semver-style major.minor tag (e.g. "1.0").
published_at datetime UTC timestamp. For a published row, when it was published; for a draft row, its own created_at (never the wall-clock "now" of the request — see :meth:_published_at_from_row).
tenant str Tenant slug.
is_published bool Whether this row IS the published snapshot for its own version — derived, never stored (D1/D2): published_version == version. The draft/published distinction is a per-row LABEL (D3), not a visibility gate — every stored row is listed regardless of this value.
is_frozen bool Equal to is_published — only a published snapshot is immutable; a draft row is rewritable in place by the next editor save.

Alias is_unique_violation

Re-exported from parrot_formdesigner.services._db_utils.is_unique_violation.

Attribute logger

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally