# Collection versions — v0.1 (immutable releases, pinning, upgrades)

**Status:** descriptive. It describes `release/0.545.0`. This is the spec that the docstring in
`collector/store/entitlements.py` calls "immutable-versions". Every statement cites
`file:function`. Gaps are listed under §6 *Open*.

## 1. What a buyer gets

When you buy a collection, you are entitled to its **major line**:

- **minor and patch releases** of that major are **free** upgrades;
- a new **major** release is a **new purchase**.

A version you bought never changes under you. Published versions are immutable, and your pin
keeps working even if the seller later deprecates that version.

## 2. Versions and releases

- Every edit to a collection manifest creates a new **immutable** version, staged and patch-bumped
  (`routes/memory.py:_stage_collection`). The versions stay in the tenant's projection (a git odb).
  Unpublishing clears a channel. It never deletes a version.
- A metadata-only edit (`POST /memory/collections/{cid}/metadata`, #999) is such an edit too: it
  stages a patch version that differs from its base only in `tags`/`description`, and is released
  (level `patch`, free) when it is promoted. Its base is the live version (or, when nothing is live,
  the staged draft, whose member changes ride along) and its version is the next free patch on the base's own major.minor line, never derived
  from the highest version; with a newer draft staged on top of the live version it is refused
  (409 `newer_draft_staged`), and with a staged draft older than the live version too (409
  `older_draft_staged`). The promote-time storefront scrub re-stages on the promoted version's own
  line the same way, so a scrubbed metadata edit is still a `patch` release.
- Promoting to `latest` records a **release** (`routes/memory.py:promote_collection` emits
  `collection.released {id, tenant, version, prev_version, level, changelog, member_diff, by}`).
- **Level** (`routes/memory.py:_release_level_and_diff`) compares the new version with what `latest`
  pointed to before:
  - no previous version: `major` (the initial release);
  - the first `X` differs: `major`;
  - `Y` differs: `minor`;
  - `Z` differs: `patch`;
  - otherwise `metadata`.

  The version string is parsed as `v?X.Y.Z` (`_semver_key`). `member_diff` lists the members
  added, removed and changed.
- The ledger (`store/collection_releases.py:CollectionReleases`) folds `collection.released` and
  `collection.version.deprecated`. Replaying the same version replaces its record.

## 3. Pinning at purchase

- Checkout pins a collection sale to the version `latest` resolves to **at checkout**
  (`routes/billing.py:checkout`, `options.version`).
- Fulfilment emits `entitlement.granted{version}` (`routes/billing.py:_fulfil`). Free imports and
  acquires also record the version (`routes/memory.py:import_collection`, `library_acquire`).
- The grant covers the version's **major** (`store/entitlements.py:_Grant.add_version`, `_major` =
  the leading integer of `v?N.`). Several grants add their majors together. A grant **without** a version
  (admin grants, legacy grants, and purchases made before pinning existed) covers **every** major, so
  earlier buyers are never locked out.
- A buyer who already holds the current major gets 409 `already_owned`. A buyer holding only an
  older major may buy the new major (`routes/billing.py:_refuse_own_or_owned`).

## 4. Reading and upgrading

| Route | Behaviour |
|---|---|
| `GET /memory/collections/{cid}/bundle` (default `version=latest`) | An entitled buyer gets `_pinned_version`: the **highest** version in their major lines. So free minor and patch upgrades arrive automatically, and a new major does not. An explicit `?version=` is served if the entitlement covers its major; otherwise the answer is 402. (`collection_bundle`) |
| `GET /memory/collections/{cid}/releases` | The release history, newest first, each with `level`, `changelog`, `member_diff` and `deprecated`. It can be read anonymously for a public collection. Non-members never see the sign-off name. A collection with no ledger entries gets a synthesised `genesis` entry. (`collection_releases`) |
| `GET /memory/collections/{cid}/diff?to=` | The release record for `to` (default: latest). (`collection_diff`) |
| `GET /memory/collections/{cid}/upgrade` | For the caller: `pinned_version`, `latest_version`, `latest_level`, `changelog`, and `upgrade ∈ current \| free \| paid \| not_entitled`. `paid` means that latest's major is newer than every major the caller holds. Owners, admins and all-access callers always get `current`. (`collection_upgrade`) |
| `POST /library/acquire/{cid}` | Copies the pinned version's session into the buyer's private library (`specs/session-library`). |

The discovery row shows `latest_release {version, level, count}`, so a card shows whether the
listing's latest release was major, minor or patch (`store/discovery_index.py:_latest_release_of`).

## 5. Deprecation

`POST /memory/collections/{cid}/versions/{version}/deprecate` (tenant admin,
`deprecate_collection_version`) emits `collection.version.deprecated`. The ledger flags that
version `deprecated` in `/releases`. Existing pins keep resolving.

While the **listed** version (`latest`) is deprecated:

- the listing is left out of browse and search (`DiscoveryIndex.search_page`) and of the facet
  counts (`facets`), via `DiscoveryIndex.is_deprecated` (reads `CollectionReleases.is_deprecated`
  for the doc's version). The live fold drops the cached rankings at once
  (`app.py:_fold_collection_releases_live` → `DiscoveryIndex.invalidate`);
- its row (`item_row`, the listing page) carries `deprecated:true`, `sellable:false`,
  `unsellable_reason:"version_deprecated"`, and the UI explains it (`ui/src/lib/listing.ts:unsellableReason`);
- checkout refuses it with 400 `version_deprecated` (`routes/billing.py:checkout`, checked against
  the version the sale would pin);
- the doc **stays** in the index, so `get`, the listing page, access checks and disputes keep
  working for existing buyers. Entitlements and the bundle do not read deprecation.

Promoting a successor to `latest` lifts all of this. Deprecating an **older** version changes
nothing for new buyers (they are always sold `latest`).

## 6. Open

1. *Fixed (`fix/spec-review-gaps`):* a deprecated `latest` is hidden from discovery and refused at
   checkout; existing buyers keep access (§5). Free imports / `library_acquire` of a deprecated
   `latest` are still allowed (they are not sales).
2. **Level vs major key.** A release's `level` comes from the version string (`_semver_key`), and so
   does the entitlement's major (`_major`). The royalty invalidation key (`routes/billing.py:_major_seq`)
   counts `level == "major"` releases in the ledger instead. For a non-semver version string the two can
   disagree. `_semver_key` treats it as `0.0.0`, and `_major` returns None, which means "not
   restricted" in `EntitlementStore.has`.
3. **`diff` and `upgrade` use the host tenant** (`_tenant(request)`), not the item's tenant. A
   buyer on another host sees 404 or a misleading `not_entitled`. This is plan WP-13b, not done.
4. **No escrow.** Pinned versions live in the seller tenant's odb. A seller-tenant purge or erasure
   removes them, and nothing keeps a buyer's pinned version (WP-13b escrow, not built). Content
   erasure (`specs/erasure` §5) also removes versions on purpose. A pin cannot outlive an Art. 17
   erasure.
5. `_pinned_version` serves the highest version in the major, **including a deprecated one**.
