Architecture
The problem this shape solves
An agent writes HTML; a human reads it. The interesting part is not storage — it is that the reader's needs (versions, sharing, comments) and the writer's needs (publish, iterate, edit one paragraph) diverge, and that the page itself must be treated as hostile.
Hexagonal architecture is not decoration here. It is the answer to two questions that come up on day one of any self-hosted build:
- "We use S3, not whatever you used." → the store is a port.
- "Our pages must not reach the network, and we need to prove it in a test." → the policy is domain logic, and the HTTP adapter derives its header from the same object.
Layers
┌───────────────────────────────────────────────────────────┐
│ driving adapters: http handler · MCP tools · CLI │
└───────────────────────────┬───────────────────────────────┘
│ calls
┌───────────────────────────▼───────────────────────────────┐
│ application: publish · update · share · serve · comments │
└───────┬───────────────────────────────────┬───────────────┘
│ calls │ calls
┌───────▼─────────────┐ ┌────────▼───────────────┐
│ domain │ │ ports │
│ Artifact │ │ ArtifactStore │
│ CommentThread │ never │ BodyStore │
│ bindAnchor │ imports │ CommentStore │
│ validatePage / csp │ outward │ Clock · IdSource │
└─────────────────────┘ │ TokenSigner · EventSink│
│ Lock │
└────────┬───────────────┘
│ implemented by
┌───────────────────────────────────────────▼───────────────┐
│ driven adapters: memory · fs · KV · Postgres · Supabase │
└───────────────────────────────────────────────────────────┘
Dependency rule: arrows only point inward. domain/ has zero imports from src/ outside itself —
no node:crypto, no fetch, no config reading. That is what makes the aggregates testable in
milliseconds and portable to a Worker.
Aggregates and what they own
Artifact — the root. Owns its version list, its owner, its share token. Invariants:
- versions are contiguous and append-only (
versions.length === version); - an update names the version it was based on and is refused otherwise;
- only the owner writes or shares;
- every body passes
validatePagebefore it can become a version.
The aggregate holds bodies in memory because an update may need the previous version (ordered literal
edits). snapshot() returns metadata without bodies, which is exactly what ArtifactStore takes and
what BodyStore keys separately.
CommentThread — a thread is anchored to (artifact version, anchor id | null) and carries an
ordered comment list. Reply on a resolved thread is refused; reopening is explicit.
bindAnchor — not a service, a policy function: pure input, pure output, easy to reason about and to test at the boundary (exact → similarity → orphan). The threshold lives in the caller's options, not in a constant buried in a query.
The comment protocol, in one page
The artifact runs on an opaque origin with connect-src 'none', so it cannot post a comment. It
therefore contributes exactly two things over postMessage:
// artifact → host
{ source: 'lh-artifact', type: 'anchors',
anchors: [{ id, kind, label, top, left, width, height, visible }] }
// host → artifact
{ source: 'lh-host', type: 'set-mode' | 'focus' | 'rescan', id?, mode? }
The host validates event.source === iframe.contentWindow and nothing else. Anchor ids are
content-derived (tag:hash(first 80 chars of visible text):ordinal), so they survive re-publishes and
do not shift when a block is inserted above. Pixel geometry is ephemeral: recomputed on every scroll,
never stored.
Binding on read (see domain/anchor/binding.ts):
| Situation | Result |
|---|---|
| same anchor id in the new version | bound |
| block edited, same tag, text similarity ≥ 0.6 | rebound (thread re-pointed) |
| block gone, or below threshold | orphaned — shown with its original quote, never hidden |
Use cases
publishArtifact: validate → write bodies → write metadata conditionally on "absent" → publish
events. Bodies first, because a metadata row pointing at a missing body is unreadable while an
orphan body is garbage the next publish overwrites.
updateArtifact: load → owner check → domain update (version guard, edits, validate) → write the new
body → conditional metadata write → events. If the store has no compare-and-set, the whole
read-modify-write runs inside the optional Lock port.
serveArtifact: resolve the route (view token or share token) → load → return body plus the CSP
built from the same Limits the validator used. The HTTP adapter adds no-store for staff views and
a 60-second private cache for share links, so revocation takes effect within a minute.
Postgres adapter
The Postgres adapter is implemented in src/adapters/postgres/. The SQL below is a
conceptual excerpt; use the adapter’s exported SCHEMA_SQL or returned schema
for its complete current schema, including state and activity tables.
create table artifacts (
id text primary key,
owner text not null,
title text not null,
description text not null default '',
version int not null default 1,
share_token text unique,
created_at timestamptz not null default now(),
updated_at timestamptz not null default now()
);
create table artifact_versions (
artifact_id text not null references artifacts(id) on delete cascade,
version int not null,
body text not null,
bytes int not null,
created_by text not null,
created_at timestamptz not null default now(),
primary key (artifact_id, version)
);
-- conditional write without a race:
-- update artifacts set version = $2, updated_at = now()
-- where id = $1 and version = $3 -- 0 rows ⇒ conflict
Capabilities: atomicConditionalWrite: true (the where version = $n), consistentListings: true,
transactionalBodies: true (one transaction, bodies and metadata together).
Where the extension points are, and which are safe
| Extension | Risk |
|---|---|
BodyStore (S3, R2, GCS) | low — pure key/value, no invariants |
ArtifactStore | medium — must declare capabilities honestly |
TokenSigner (KMS, DB tokens) | low — payload shape is fixed, not the algorithm |
EventSink (Slack, webhooks, queue) | low — events are already drained after a successful write |
CommentStore (Postgres, Durable Object) | medium — must preserve comment ordering |
| domain policies | high — changing them changes what every adapter promises |
Anything in the last row needs an ADR, because it changes the contract rather than its vendor.