# How many outcome tags does loan restoration need?

A library returns handles to a caller. If one handle borrows from another,
the caller must close the child first so its final value reaches the parent
before the parent writes back. How much must the library tell the caller about
its private borrowing decisions?

For a family of batches with `n` independent decisions, an encoder that sees
all the decisions needs exactly `n + 1` public outcomes. An encoder that sees
only the optimal local cleanup tags needs all `2^n` outcomes. At seven
decisions, that is **three bits versus seven bits** of fixed-width tag
information for the same handles, feasible routes and cleanup guarantee. The
separation concerns where a library chooses its public representation. A
second result locates a complexity threshold for inferring that representation.
One dependency per private route always fits in one bit. With two disjoint
dependencies per route, deciding whether one bit suffices is NP-complete.

## The contract

Fix a finite, duplicate-free inventory of public handles and a nonempty family
of feasible private routes. A route is an acyclic graph on the inventory.
An edge `x → y` requires closing `x` before `y`. Initially all handles are
open. Closing an eligible handle succeeds and leaves it closed. A complete
cleanup closes every handle exactly once. The producer may choose any tag as
a function of its private route. The only route-dependent observation available
to the caller is that tag. Its decoder supplies one permutation per tag.

The [restoration semantics](../loan-analysis/#opaque-loan-interfaces) already
check a script against all routes assigned to an outcome. A complete script
for a tag exists exactly when the union of the edges of those routes is acyclic.
Reading handle positions in a successful script gives strictly increasing ranks
along every edge. Conversely, sorting the public inventory by any such ranks
gives a complete script. Ties between unrelated handles are harmless.
[Operational equivalence](https://github.com/symbiogenesis/HoldFast/blob/main/tools/rocq/loan_disclosure.v#ranked_tag_schedules_exact)

The three handles `x, y, z` illustrate the distinction between the callee's
private routes and the caller's public outcomes.

| Private dependency | Public tag | Caller closes |
| --- | --- | --- |
| `x → y` | 0 | `z, x, y` |
| `y → z` | 1 | `x, y, z` |
| `z → x` | 0 | `z, x, y` |

Every pair of rows admits a common order. Grouping all three would require
`x` before `y` before `z` before `x`. The table therefore achieves the
minimum of two tags. [Input](https://github.com/symbiogenesis/HoldFast/blob/main/tools/tests/fixtures/cleanup-ring.json) ·
[Executable three-route proof](https://github.com/symbiogenesis/HoldFast/blob/main/tools/rocq/loan_disclosure_obstruction.v#three_cycle_has_no_common_cleanup)

## Choosing the tag before or after local information is erased

**Independent rings.** Give each of `n` components its own `h` public handles
numbered from zero, where `1 ≤ n < h`. The hidden route in a component chooses
one child `j` and requires `j` to close before `(j + 1) mod h`. Every tuple
of choices is feasible. Handles in different components have no dependencies
on one another. A public script can interleave their closes in any order.

**Joint upper bound.** Look for a number `t` among `0, …, n` that none of the
components chose. There are at most `n` chosen numbers. Return `t`, then close
each component cyclically from `(t + 1) mod h` through `t`. This order satisfies
every possible edge except the one starting at `t`, which the producer knows
is absent. For example, choices `0, 2, 0` at width four can return tag one.
All three components then use the order `2, 3, 0, 1`. Selecting the tag takes
linear work in the number of components with the trial's set implementation
and requires no enumeration of the `h^n` private tuples.

**Joint lower bound.** Suppose the caller has at most `n` scripts. Assign
each script a different component. In that component any permutation violates
at least one of the ring edges. Choose that edge as the private route.
Independence makes all these choices feasible together. Every offered script
fails somewhere on that tuple. This rules out even arbitrary interleaved
scripts and proves the exact minimum of `n + 1`.
[Ring encoding and lower bound](https://github.com/symbiogenesis/HoldFast/blob/main/tools/rocq/loan_disclosure_batch.v#joint_tag_alphabet_exact)

**Loss at a package boundary.** Now let each component first publish an
individually minimal local tag. Each ring needs exactly two tags. The two
classes together contain the whole ring, so one cleanup order cannot serve
both entire classes. Suppose a downstream encoder merges two different
local-tag tuples. Pick a component where they differ and project the proposed
joint script onto its handles. Holding the other private choices fixed within
each tuple, independence allows every route of the first class and every route
of the second. The projected order would serve both classes, a contradiction.
Thus even an arbitrary downstream re-encoding must distinguish all `2^n`
tuples. Concatenating the tags attains that bound.

The argument extends to independent components with different finite local
alphabets. In any minimum local partition, merging two used classes must
destroy the existence of a common schedule, since a successful merge would
give a smaller partition. The downstream encoder must therefore be injective
on the product of the used alphabets. The joint upper bound requires a
producer that still has the private choices when it builds the batch.
[Projection of joint scripts](https://github.com/symbiogenesis/HoldFast/blob/main/tools/rocq/loan_disclosure_factorization.v#project_joint_cleanup) ·
[Injectivity after local tagging](https://github.com/symbiogenesis/HoldFast/blob/main/tools/rocq/loan_disclosure_factorization.v#postencoding_injective_on_realizable_tuples)

## Inferring an interface from explicit routes

Call an inclusion-minimal set of routes with a cyclic union an *obstruction*.
Use routes as vertices and obstructions as hyperedges. A class admits a
cleanup order exactly when it contains no whole hyperedge. The minimum
number of tags is therefore the weak chromatic number, the minimum number of
colors that avoids a monochromatic hyperedge. With `h` public handles, every
obstruction has at most `h` routes. A simple cycle has at most `h` edges, and
one route supplying each edge suffices. One route per edge of an `h`-cycle
attains the bound for every `h ≥ 2`.
[Constructive obstruction bound](https://github.com/symbiogenesis/HoldFast/blob/main/tools/rocq/loan_disclosure_obstruction.v#bounded_cleanup_obstruction)

Conversely, take any finite hypergraph whose edges have at least two distinct
vertices. Allocate a separate ring of handles for each edge and give one arc
of the ring to each incident vertex's route. A selected set of routes admits
a common order exactly when it omits at least one vertex of every hyperedge.
Cut each ring at an omitted arc to construct the order. On an individual
route, all child–parent pairs are disjoint. This construction uses one handle
and one arc per incidence. Redundant hyperedges disappear when taking minimal
obstructions. Applying the construction to a 3-uniform, 4-regular instance
shows that deciding whether one bit suffices is NP-hard even when **every
pair of routes shares an order** and each route contributes just four disjoint
dependencies. Two permutations certify a positive answer in polynomial time.
[Ring realization](https://github.com/symbiogenesis/HoldFast/blob/main/tools/rocq/loan_disclosure_cycles.v#pooled_schedule_iff_no_whole_edge) ·
[Matching property](https://github.com/symbiogenesis/HoldFast/blob/main/tools/rocq/loan_disclosure_cycles.v#single_route_arcs_form_matching) ·
[Restricted hypergraph hardness, Darmann and Döcker](https://arxiv.org/abs/1908.04198)

**Threshold in dependencies per route.** With at most one edge per route,
any order and its reverse cover all routes. For exactly two disjoint edges,
reduce from Betweenness. Each input triple `(a,b,c)` asks for `b` between
`a` and `c` in one common order. Allocate fresh handles `p,q` per triple.
For each of the two comparisons `(u,v) = (a,b), (b,c)`, introduce these
two routes.

| Route | First dependency | Second dependency |
| --- | --- | --- |
| Forward | `u → v` | `p → q` |
| Reverse | `v → u` | `q → p` |

Given two covering orders, each must cover one route of each opposing pair.
In the first order, both original comparisons therefore have the orientation
of `p,q`. Restricting that order to the original handles satisfies every
triple. Conversely, extend a satisfying order by arranging each fresh pair
to agree with its triple. The extension and its reverse cover all four routes.
The reduction adds two handles and four matching routes per triple. Together
with certificate checking this proves NP-completeness. This reduction and
the pairwise-compatible construction impose different restrictions.
[Reduction equivalence](https://github.com/symbiogenesis/HoldFast/blob/main/tools/rocq/loan_disclosure_betweenness.v#two_order_matching_reduction) ·
[Betweenness, Opatrný](https://doi.org/10.1137/0208008)

## Artifact and evidence

Run the finite solver on the three-handle interface, or encode a batch
directly from its width and private child positions.

```text
python tools/run.py holdfast-borrow-compare --cleanup-tags tools/tests/fixtures/cleanup-ring.json
python tools/run.py holdfast-borrow-compare --cleanup-batch 4 0 2 0
```

The general solver accepts one to twelve explicit private routes. If their
union admits a close order, that one schedule serves every route. Otherwise,
the solver computes all minimal obstructions and finds a minimum partition by
dynamic programming over route subsets. A separate replay checks each public
permutation against its assigned routes. One valid schedule already proves
every subset feasible and the one-tag alphabet minimal. For larger alphabets,
replay compares the claimed obstructions with stepwise close eligibility and
searches for a coloring with one fewer tag. The bounded solver
has exponential worst-case cost. The independent oracle enumerates permutations
and their covers for every family of one to three distinct DAGs on three
handles. All 2,625 families agree on the optimum and the obstruction family.
A seven-route construction from the Fano plane needs three tags even though
all 21 pairs are compatible. [Solver and replay](https://github.com/symbiogenesis/HoldFast/blob/main/tools/toolchain/loan_disclosure.py) ·
[Oracle and mutations](https://github.com/symbiogenesis/HoldFast/blob/main/tools/tests/test_holdfast_loan_disclosure.py)

A separate C11 caller receives opaque handles and a tag. Each close copies a
staged counter value to its destination. Closing a parent early commits a stale
value even though all backing storage stays live. The computed schedules
restore the expected owner values on all three single-ring routes. A wrong
producer tag loses one nested write.

For three independent four-handle rings, the C11 provider selects the joint
cut from the same private choices that wired the twelve handles. A second
build packs the two local cuts for each ring into one public tag. The
separately compiled callers restore all twelve owner values on all 64 private
tuples. The joint caller uses four schedules and the local caller uses eight.
Both use the same one-byte tag field. On Linux ARM64 the descriptor is 104
bytes, with the tag at offset 96. The dispatch tables of byte indices occupy
48 and 96 bytes respectively. Omitting the final private choice in the joint
encoder causes sixteen observed losses when linked against the original
joint caller. [Native replay](https://github.com/symbiogenesis/HoldFast/blob/main/tools/tests/test_holdfast_loan_disclosure_native.py)

Use `python3 tools/run.py test --only loan_disclosure --require-toolchain` in
Linux or WSL to run the oracle, native mutation controls and Rocq audits.

## Relation to prior work and scope

Felsner and Trotter characterize poset dimension by coloring a hypergraph of
incompatible order requirements. Van Iersel and colleagues already prove
hardness for covering ordered triples with two linear orders. The matching
restriction above uses four distinct endpoints. The joint ring count also
has a graph-theoretic interpretation. A tuple of cut positions covers a
route tuple precisely when they differ at every coordinate, the open-neighborhood
relation in a direct product of complete graphs. Total domination of such
products has an established literature. The research claim here ties these
ordering and covering phenomena to operational loan restoration and to the
information lost at a separately compiled interface.
[Felsner and Trotter](https://trotter.math.gatech.edu/papers/112.pdf) ·
[Van Iersel et al., Theorem 1](https://arxiv.org/abs/1410.2371) ·
[Mekiš](https://www.sciencedirect.com/science/article/pii/S0012365X10002852)

Plural already checks resource protocols whose permissions depend on a
method's result, including JDBC close obligations. Place Capability Graphs
recover borrowing information from Rust compiler analyses. In optimization,
`K`-adaptability preselects policies and chooses one after learning the
scenario. The specific question here is the minimum alphabet for returning
a static cleanup schedule under a fixed set of public observations.
[Plural](https://www.cs.cmu.edu/~kbierhof/papers/permission-practice-tr.pdf) ·
[Place Capability Graphs](https://arxiv.org/abs/2503.21691v5) ·
[K-adaptability](https://doi.org/10.1007/s12532-019-00174-2)

The proof assumes an exact family of feasible routes, monotone close
operations and a caller with no additional way to inspect the topology.
A library-owned destructor, readiness queries, route-dependent close results,
or shared backing between batch components changes the contract. The native
fixture constructs the ring routes from counter wiring. Inferring routes
from HoldFast source, checking separate package evolution and measuring the
representation on an independently authored client remain open in the
[package trial](../../plan/#q25fg).
