Skip to content

Latest commit

 

History

History
244 lines (212 loc) · 14.9 KB

application-projection.md

File metadata and controls

244 lines (212 loc) · 14.9 KB

Materialized application projection

The server maintains one current, normalized application-inventory projection per server-derived tenant/principal scope with an admitted application stream, so catch-up stays bounded: a client returning after a long absence receives one current checkpoint instead of a long historical replay, and without forcing another cross-cutting authoritative capture. The projection holds normalized inventory only; transcripts, provider-feature envelopes, blocking interactions and provider-native identifiers stay with their owners, as described in Provider-owned conversation state. Nothing is persisted: restart discards every projection and stream generation.

Ownership and lifetime

The materialization key is exactly the tenant and principal of the request scope. No replay cursor, workspace ID, thread ID or backend instance selects or widens it. The scoped registry creates the projection, its event hub, replay buffer, timers and byte accounting as one aggregate and destroys them together.

The first subscriber needing a ready projection drives one single-flight authoritative capture; concurrent handshakes join it. Capture reads inventory repositories, the execution-target catalog, installation advisories, terminal summaries and loaded-runtime state, and never attaches a dormant provider conversation. A producer publication is a state-change hint, not a request to instantiate a projection: publication looks the aggregate up without creating it, and a hint for a scope with no retained aggregate, or an unseeded one with no subscriber, is discarded because the next seed reads authoritative state. A failed seed or recapture discards the aggregate and closes its streams; the server does not retry, and the next reconnect seeds a new generation.

GET /api/threads/:threadId/summary resolves one normalized thread summary independently of the bounded bootstrap and descendant pages. It uses the authenticated request's tenant/principal scope and returns 404 for an unknown or inaccessible thread. The read combines durable inventory, terminal counts, and runtime state already loaded in memory; it never attaches a dormant provider or reads its transcript. Responses use Cache-Control: no-store. Clients use this read to resolve a saved default thread that is absent from their currently loaded inventory.

Application snapshots and thread deltas do not carry global Active, Snoozed, Parked or Archived totals. Shelves and archive views retain their own scoped counts, and thread summaries retain their descendant, group and automation counts. A single-thread publication never runs a global inventory aggregate.

Thread-summary readers reuse a bounded set of prepared SQL statements on the owning database connection. Scope and thread IDs are bound on every execution; rows are read afresh, including inside transactions. This caches query preparation only, not query results or a replacement for authoritative reads.

Fold and epoch model

An aggregate is unseeded, seeding, ready, recapturing or closed. The application publication boundary is its only writer and serializes every turn for a scope in one per-principal lane. Preparing a seed or replacement may await repositories and runtime readers in that lane; committing an already validated delta is synchronous.

Fold-and-publish is atomic. Validation, resolving the prior value, checking affected invariants, computing the byte delta and encoding the SSE frame all complete before installed state changes. The hub then assigns watermark + 1, installs the replay record, and a pre-fanout hook applies the prepared fold and installs the projection before any subscriber callback runs, so a subscriber never observes an event ahead of the checkpoint that includes it. A failure before the commit changes nothing; a failure inside it closes the aggregate.

Environment, workspace, thread and Task upserts and removals fold incrementally; a Workpad invalidation advances the position without changing snapshot values. An ordinary Group creation, assignment, or removal of a membership publishes the changed thread assignments and affected Group totals in one thread_groups_changed event. The server captures these facts in the membership transaction and publishes them in commit order. When the base assignment, Group totals, or fork selection prevent applying that exact change, the server publishes a full replacement. Group renames and deletions also use replacements.

Projects, execution targets, advisories, fork origins and lineage change through a full replacement, as do cross-collection changes one delta cannot carry, such as a grouped thread crossing the archived boundary, a thread moving workspaces, a location moving projects, or a fork whose bounded selection membership can change. A Task's associatedProjectId (null for a global Task) must name a listed project, and a thread Task whose thread is listed must carry the project of that thread's location, so a workspace changing project also revalidates its threads' Tasks. Folding is bounded work: ID, reverse-reference and lineage reference-count indexes and a per-entity byte ledger let a delta serialize only the incoming entity and reject a projected total above the 32 MiB snapshot limit. Regressed thread or Task revisions, dangling references and inserts beyond 256 environments or 10,000 other entries are rejected, and at most 64 dependent entities are inspected before recapturing rather than scanning a collection.

Captures coalesce by epoch. A structural hint or explicit client recovery advances the requested epoch; a capture records that epoch when it starts and covers it once installed, so waiters at or below the covered epoch are satisfied by it and later requests need at most one follow-up. Ordinary deltas admitted during a capture do not advance the epoch: they wait outside the lane, reread their source, and fold against the installed replacement.

Full validation is periodic. The next complete materialization after a value-changing fold re-parses the strict snapshot schema, recomputes the byte total, rebuilds and compares the indexes, and confirms no stored entity was mutated in place. It is due after 1,024 folds, 8 MiB of examined entity bytes, or 60 seconds from the first unaudited fold, and is scheduled only while the aggregate has subscribers, so an idle projection defers it. A due audit blocks serving a checkpoint but not an otherwise valid small replay.

Replay window and compaction

Application replay retains at most 32 events and 64 KiB of encoded SSE frames; thread hubs keep their own larger policy. Each retained frame is cached with its byte length, so accounting, replay and live fanout never encode an event twice. Snapshots are never retained: installing a seed or replacement clears the suffix and makes its sequence the replay floor, and an individually oversized delta likewise folds, advances the position and clears the suffix. Prefix eviction stays safe because the only cursor that loses history is older than the floor, and it receives a checkpoint that already includes every evicted event.

Catch-up and checkpoints

A handshake registers its live listener and chooses its baseline synchronously, before any capture or socket wait; events published after registration enter the connection's bounded pending queue and are filtered against the baseline sequence. Last-Event-ID takes precedence over the explicit query cursor. The retained suffix is served only when the aggregate is ready, no uncovered replacement is pending, and the cursor sits inside the contiguous retained suffix within both the 32-event and 64 KiB limits; retention uses the same bounds, so a cursor inside the window is within budget by construction. A cursor at the watermark receives only the live marker.

Every other case — an absent cursor, a foreign-generation, future or evicted cursor, or an over-budget suffix — receives the current checkpoint: an ordinary snapshot envelope whose event ID is the current stream position. It is synthesized for that subscriber, so it is not appended to replay, does not advance the watermark, and is not broadcast; the next reconnect asks only for events after that position. One materialized checkpoint and its encoded frame are cached per position and shared by concurrent handshakes until the next publication drops that cache, and this clean ready path performs no capture and no repository or backend read. The explicit authoritative-replacement handshake, honored only when the client sent no Last-Event-ID, bypasses the checkpoint and publishes a fresh capture as a replacement.

Idle eviction and accounting

Admitting a subscriber cancels idle expiration. Losing the last subscriber starts the one-hour idle interval and sets the aggregate's recency; producer traffic neither extends the deadline nor refreshes recency, although cheap valid deltas still fold into a retained idle aggregate. An aggregate that loses its last subscriber while seeding, recapturing or carrying an uncovered replacement request is discarded rather than captured for nobody. The registry keeps at most 128 idle scopes and 256 MiB of accounted bytes, evicting least-recently-idle first and never evicting a subscribed aggregate for the idle budget. Accounted bytes cover the projection's serialized size, retained replay frames, and any cached checkpoint frame plus the materialized snapshot behind it; they bound serialized size, not JavaScript heap use.

Consistency and fail-closed recovery

A checkpoint is served only when the aggregate is the one the registry holds for the authenticated scope, its generation equals the hub generation, its position equals the watermark at the atomic read, every position since the seed folded exactly once in order, retained replay is a contiguous suffix ending at that watermark, the last full validation established the complete normalized invariants and byte limits, every later fold passed its bounded checks, and no failed producer handoff, rejected fold or lifecycle race was observed. Ownership, generation and state are rechecked after every await.

Any failed statement moves the aggregate to recapturing, drops the cached checkpoint, and stops serving checkpoints or replay from that baseline. Without subscribers it is retired instead of recaptured. With subscribers, one coalesced recovery capture starts at least one second after the preceding capture, doubling up to 30 seconds per further recovery cycle without a clean interval and resetting after 60 seconds of uninterrupted ready state; structural replacements use epoch coverage rather than this failure spacing. A failed recovery retires the aggregate and closes its streams rather than serving a projection the server can no longer prove current, and invalidation reasons stay server-private: the browser sees a replacement snapshot or a disconnected stream. Current means current through the published stream position, so a change committing while its publication is pending follows the checkpoint as a later event.

Thread automation summary

Every thread summary carries a non-null automations aggregate. It reads only principal-scoped application definitions and runs and is shared by every backend. It contains totalCount, enabledCount, pausedCount, exhaustedCount, inFlightCount, attentionCount, revision, and optional nextRunAt. An empty source has zero counts. A thread with multiple definitions still appears once in inventory: joins must aggregate per source before producing thread rows.

The aggregate is bounded independently of the number of attached automations. Full definitions and histories are fetched from the paginated /api/automations collection and /api/automations/:automationId resource routes. Global collection membership never depends on which source threads or fork families the browser has loaded. Each resource carries its own identity, name, source metadata, revisions, latest run, and independent attention. Source IDs select collections, not a single implicit automation.

nextRunAt describes the earliest actionable candidate, accounting for source suppression and deferred delivery. An indefinitely blocked run has no invented deadline. In-flight counts include pending delivery and the latest accepted run whose agent turn has not settled. This does not change the scheduling deadline. In-flight and attention counts remain separate: a sibling's activity or success must not hide another automation's unresolved problem. Finished-only aggregates produce no sidebar or header schedule glyph.

The latest run follows history order, created_at DESC, id DESC. Attention is computed independently: an unresolved uncertain delivery takes priority; otherwise the latest decisive delivery failure needs attention until acknowledged or superseded by a later success from that same automation. Skips, cancellation, and manual uncertainty resolution do not count as successful delivery. A failed agent turn also needs attention without changing delivery state. An interrupted turn, whatever stopped it, is neutral: it never needs attention, marks the automation failed, or returns a parked anchor to Active.

Thread detail includes bounded attention.automationContexts, each with explicit automation, run, and source identity. Dismissal names the run and observed outcome, never a mutable singleton slot. Its outcome is triggered, interrupted, failed, or uncertain; an interrupted turn has a neutral notice on its result thread, like a triggered one. A stale neutral-notice dismissal cannot clear a later failure. First settlement as failed clears an earlier delivery acknowledgement atomically, while an interruption keeps it; settlement replay preserves subsequent failure acknowledgement. A result thread's provenance names its actual source even when the source's other automations have run since.

A date/time definition is retired with its existing deletion marker once its current scheduled occurrence has a successful or interrupted agent-turn settlement and nothing about the definition remains to see or dismiss: no undismissed result notice from any of its runs, no unresolved problem, and no in-flight work under the same latest-turn rule as the in-flight count. The anchor's notice follows the latest decisive run, so a later manual run supersedes the scheduled notice there; Fork result threads keep one notice per run. Old occurrences cannot retire a rearmed definition. Retirement removes normal collection/detail/history access while retaining the definition, runs, receipts, queued-input provenance, and thread lineage. Startup reconciliation handles already-settled eligible history and completion observations committed before a crash.

Definition and run changes publish scoped automation_changed invalidations on the existing application stream and refresh affected thread summaries. Source metadata changes also invalidate matching collection views. Reconnect must re-fetch authoritative collections; revision equality between sibling resources never makes their detail or history caches interchangeable.