relate-candidates computes the available candidate SLUGS (slug-sorted, no per-candidate read), then reads titles only for the page it returns. On the unfiltered path (q="" — the initial picker load AND every editor server-fill, the common case) that's ~limit durable reads instead of one-per-post, cutting the http-listen suspend/resume churn. A filter (q≠"") still resolves titles across the pool since it matches on the title. (A boot slug→title cache would make the filter O(1)-perform too, but it's blocked: no bulk KV read, and a per-post host/blog-get loop at boot hits the JIT 'durable read in a boot loop drops all-but-first' bug — see plans/relations-as-posts.md.) conformance 291/291, run-picker-check 3/3 (incl. the title filter + paging). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
7.4 KiB
Relations as posts — declared, inherited, and eventually algebraic
Principle
Everything is a post in one graph: content-posts, type-posts, relation-posts, and
(later) constraint-posts. Nothing about typing is hardcoded — a type-post declares
which relations it anchors, declarations are inherited down the type closure, and
every candidate set / validation is a transitive graph query (lib/relations). This
closes the meta-circular loop the typing plan gestured at: the type system describes
itself in its own graph.
Supersedes the hardcoded :candidates "types"/"tags"/"all" field of host/blog-rel-kinds.
Why (the wrinkle that started this)
Candidates for is-a/subtype-of were instances-of("type") — the instances that are
types, but NOT the type-defining posts themselves (type, tag, article are wired with
subtype-of, no is-a edge, so they're not instances of type). So the picker offered
tutorial (is-a tag) but never tag/article/type — the things you most want to say a
post is-a. The fix is to ask the right question: a candidate is anything that inherited
the relation's object-end declaration from the anchor, which includes the roots.
Model
- A declaration is an edge
T --declares--> R: type-postTanchors relationRat its object end ("you may point atTwithR"). Seed:type declares is-a,type declares subtype-of,tag declares tagged.relatedhas no declaration. - Candidate set for relating under
R= the down-closure ofR's anchors throughinverse(is-a) ∪ inverse(subtype-of)(a post is a candidate iff it is, transitively, an instance-or-subtype of an anchor — or IS one). No anchors ⇒ every post (related).is-a/subtype-of: anchors{type}⇒ the whole type closure (roots + subtypes + instances). Wrinkle fixed.tagged: anchors{tag}⇒ the tags.related: no anchor ⇒ all posts.
Roadmap
Slice 1 — declarations + candidate-by-inheritance — DONE
- Seed
declaresedges; addhost/blog--reach-down(down-closure) and rewirehost/blog--candidate-poolto be declaration-driven.:candidatesbecomes vestigial. - Wrinkle fixed: the type roots now appear as
is-acandidates.
Slice 2 — relations as first-class posts — DONE
relationroot +is-a/subtype-of/tagged/relatedseeded as posts (each is-a relation) owning their metadata in a:relslot (:symmetric :label :inverse-label).host/blog-rel-kinds/kind-spec/kind-symmetric?now read it; the static registry is gone.host/blog--rel-slugs=host/blog-in "relation" "is-a"(cheap, flat).- Perform budget under http-listen (the hard lesson): a durable read inside the
render VM raises
VmSuspended, and too many per request 500s the page. Two fixes: (1) relation metadata is loaded into an in-memory cache at boot (host/blog-load-rel-kinds!, likeload-edges!) sokind-specis pure; (2) the initial edit page renders its pickers EMPTY (the load trigger fills each) — only the relate/unrelate FRAGMENT server-renders candidates (with-candsflag), so one page render doesn't docandidate-get × every picker. Benign single-perform suspend/resume still logsVmSuspendedbut returns 200. - Live JIT gotcha (cost real time): the serving-mode JIT drops all-but-first when
map/for-each-ing a function-produced list — buildingrel-kindsthat way rendered only 1 of 4 editors live, while conformance + the ephemeral server passed. Sohost/blog-rel-kindsis a VALUE the boot populates and the cache loads are UNROLLED. Conformance green ≠ correct live — verify the rendered edit page. (Re-fold the enumeration once plans/jit-bytecode-correctness.md lands.)
Slice 2.5 — picker title reads are O(page), not O(pool) — DONE
relate-candidatescomputes the available candidate SLUGS (slug-sorted, no per-candidate read), then reads titles ONLY for the page it returns. On the unfiltered path (q="" — the initial picker load AND every editor server-fill, the common case) that's ~limitreads instead of one-per-post — killing the durable-read churn under http-listen. A filter (q≠"") still resolves titles across the pool (it matches on the title), but that's the interactive path.- A boot-time slug→title cache would make even the filter O(1)-perform, BUT it's blocked
for now: there's no bulk KV read, and a per-post
host/blog-getloop at boot hits the JIT bug (a durable read inside a boot loop drops all-but-first —load-edges!only works because its loop body is perform-free). Revisit with a bulk read or once the JIT lands.
Remaining follow-ups: subject-end declarations (who may be the source); a proper relation-subtype closure when relations get subtyped; the boot title cache above.
Slice 3 — typed relations (target-type constraints) — DONE
- The declaration's
declares-anchor IS the target-type constraint:is-a/subtype-of(anchored bytype) require a type object;tagged(anchored bytag) a tag. A newwroterelation needs only aWork declares wroteedge — fully data-driven. host/blog--valid-object?(kind, other)=other ∈ candidate-pool(kind)— the SAME set the picker offers, so picker and validation agree by construction.relate-submitnow enforces it (an invalid target is a silent no-op, like the other guards);related(no anchor) accepts any post. The picker never offers an invalid target, so this guards crafted/API requests — the jump from "candidate set" to an enforced relation schema.- NOTE:
host/blog-relate!(direct/seed) stays UNVALIDATED — the seed needs to writeX is-a relationwhererelationisn't undertype. Validation is a handler boundary.
Slice 4 — type algebra
Types are posts + subtype-of is a partial order ⇒ a lattice, and is-a? is transitive
set-membership ⇒ extents have set semantics. So algebra is expressible as posts:
- Intersection
A ∧ B— a type-post whose membership predicate isis-a? A ∧ is-a? B(meet / GLB in the lattice). UnionA ∨ B—is-a? A ∨ is-a? B(join / LUB). - Refinement
{x : T | φ(x)}— a type-post with a:constraintpredicate over a post (generalises today'sarticleschema "must have a heading"). Gradual: declaring the type adds the obligation; the next save must satisfy it. - Algebraic types are themselves posts with edges to their operands —
is-a?recurses on the expression. Meta-circular: the algebra lives in the graph it describes.
Slice 5 — constraints as posts + validation
- Promote the schema/
:constraintslot to constraint-posts (a predicate expr + message), attachable to any type. Save-time validation evaluates the constraints of a post's full (transitive) type set. Relation cardinality (is-asingle-valued?taggedmany?) becomes a declared constraint too.
Open design questions (track as we go)
- Subject-end declarations — who may be the source of a relation (a root
Thing?). - Inheritance path — through
is-aANDsubtype-ofdownward (current choice); revisit if instances-of-instances as candidates surprises. - Bootstrap / meta-circularity —
is-aneedsis-a; seed relation-posts +Type is-a Type(?) idempotently, as the type seed already is. - Cost —
reach-downis a BFS of direct-edge scans; fine for a small blog, revisit with alib/relationstransitive query if the graph grows.