Tier 2 for the beta: the lifecycle stream gets a contract, plus a P0 found on the way #81

Merged
chrnx merged 7 commits from beta/tier-2 into develop 2026-09-01 16:11:16 +00:00
Owner

One branch for the tier, one commit per issue, same as #78. Six issues, five planned and one that turned up mid-tier and jumped the queue.

Issue Commit What it was
#73 5d8363d request:end could arrive with no request:start
#64 3930804 three events per failed request, nothing saying which to count
#80 5294e62 a custom onError that throws took the process down
#65 c00f714 route:unmatched offered only the caller-controlled path to label on
#67 1e76921 the bus was reachable from a plugin but not the graph, and nothing said so
#74 1c4f3fb a close listener per request for a budget most apps never set

Four of these are the same defect wearing different clothes: the stream is silent when it is wrong. Nothing throws, nothing logs, the numbers just come out incorrect. That is what shaped the fixes — each one tries to make the obvious consumer code the correct code, rather than documenting a rule and hoping it is read.

#73 — the pair is a guarantee

Both options were live and the alternative was seriously considered. Documenting request:end as standalone-terminal loses on one concrete case: the in-flight gauge everyone writes.

bus.on('request:start', () => inFlight++);
bus.on('request:end',   () => inFlight--);

Without the guarantee that drifts negative the moment a server sheds — under load, which is when an operator is reading it. Emitting a start for a shed request is not a fabrication either: the request did arrive, the gate refused it before the router, and request:start says arrived, not routed.

The cost is that it binds every future emitter, and that is real. It is paid down by test/lifecycle-pairing.test.ts, which enumerates every response shape — 200, 404, 500, 413, 204 preflight, and 503 on both adapters — and asserts the pair and the shared requestId. Adding a shape means adding a row. Verified non-vacuous: dropping the emission fails the two shed rows and nothing else.

#64 — what to count

request:end is terminal and universal and is the request counter; request:failed is handler code threw, which no status expresses since a rendered 422 is also a throw; route:unmatched is no route ran. The last two are additional. All of it now sits on EventPayload, where a consumer reads it.

request:failed carries status too. Getting it required moving the emission after the render — onError may turn any throw into any status, so deriving one from the error would be a guess a custom renderer makes wrong.

#80 — the P0, found by asking what happens when the renderer throws

Moving that emission meant asking what renderError does when onError throws. It exited the process, code 1.

Same shape as the CORS predicate crash in #78 and easier to reach: not a cross-origin request but any request that errors. The stack goes through unmatchedOutcome, so the renderer produces the 404 too — an app with a custom renderer and no matching route was one request away from exiting. And it is the worst-timed crash there was: the renderer only runs once something has already gone wrong, so an error occurred, the code written to report it failed, and the server ended instead of degrading.

All six call sites funnel through renderError, so the guard goes there once. A throwing renderer falls back to errorToResponse — what the option overrides, and therefore the honest fallback. The original error still gets its response; the renderer's failure is logged separately naming both errors.

viewEngine was checked in the same pass, since it is the other user callback on a render path. It has a boundary — the pipeline's catch — so a broken engine is a 500. Asserted now rather than left to inference.

#65 — something bounded to label on

route:unmatched and the request:end that follows carry route: '<unmatched>', exported as UNMATCHED_ROUTE. A matched path is bounded by the route table; an unmatched one is bounded by nothing, and a scanner walking /aaa, /aab, /aac is a memory leak with a metrics backend attached.

Found while reading the emission order: route:unmatched fires before unmatchedOutcome, which can serve a static file and answer 200. So it is not a synonym for failure, and a consumer counting it as 404s over-counts every asset served. Documented.

#67 — three things that were one decision

@needs('events') reaches { on } — the same narrowing plugins get, exported as Events. The Bus itself stays out of the graph, because a node that could reach it could also emit, and an observation channel anything can write to is not one. @needs('bus') now fails saying that and pointing at what does work, because documenting it reaches a reader while the boot error reaches the person typing it.

Behaviour change: logger, rooms, events and bus are reserved names now, enforced in setRunner — the single point modules, plugins and mesh exports all register through. Built-ins used to be registered only if the name was free, so a provider called logger silently replaced the framework's own and every @needs('logger') in the app got something that was not the logger core writes to. This replaces a test that asserted that shadowing as a feature, and it can fail an app that boots today. test/bus-access.test.ts proves the three legitimate doors all still work and that only emit stayed put.

#74 — the trivial one

The gate exposes limited rather than each adapter re-deriving it from opts, because the gate is what knows: acquire() returns true for both "admitted" and "not counting", which is why a caller reading only the result paid either way. Unchanged where a budget is configured — the listener is what releases a slot on a mid-handler disconnect — and the test asserts both halves so a fix for one cannot become a regression in the other.

Gates on 0adab69

lint               0 errors (48 pre-existing warnings)
complexity:check   every function <= 25
typecheck          clean
format:check       clean
build              clean
npm test           495 passed / 72 files   (466 / 69 at branch point)
npm test (node 18) 494 passed              (CI's build floor)
test:deno          17 passed
test:bun           7 passed
test:edge          PASS
deno publish       dry run clean

Not here

The documentation. guides/observability.md already has the bus-access and what-to-count sections on the docs branch; the rest of this tier follows before that branch goes up as one pull request, so the site moves once rather than per tier. The version bump and the dated changelog section still belong to the cut.

One branch for the tier, one commit per issue, same as #78. Six issues, five planned and one that turned up mid-tier and jumped the queue. | Issue | Commit | What it was | |---|---|---| | #73 | `5d8363d` | `request:end` could arrive with no `request:start` | | #64 | `3930804` | three events per failed request, nothing saying which to count | | **#80** | `5294e62` | **a custom `onError` that throws took the process down** | | #65 | `c00f714` | `route:unmatched` offered only the caller-controlled path to label on | | #67 | `1e76921` | the bus was reachable from a plugin but not the graph, and nothing said so | | #74 | `1c4f3fb` | a `close` listener per request for a budget most apps never set | Four of these are the same defect wearing different clothes: **the stream is silent when it is wrong**. Nothing throws, nothing logs, the numbers just come out incorrect. That is what shaped the fixes — each one tries to make the obvious consumer code the correct code, rather than documenting a rule and hoping it is read. ## #73 — the pair is a guarantee Both options were live and the alternative was seriously considered. Documenting `request:end` as standalone-terminal loses on one concrete case: the in-flight gauge everyone writes. bus.on('request:start', () => inFlight++); bus.on('request:end', () => inFlight--); Without the guarantee that drifts negative the moment a server sheds — under load, which is when an operator is reading it. Emitting a `start` for a shed request is not a fabrication either: the request *did* arrive, the gate refused it before the router, and `request:start` says arrived, not routed. The cost is that it binds every future emitter, and that is real. It is paid down by `test/lifecycle-pairing.test.ts`, which enumerates every response shape — 200, 404, 500, 413, 204 preflight, and 503 on both adapters — and asserts the pair and the shared `requestId`. Adding a shape means adding a row. Verified non-vacuous: dropping the emission fails the two shed rows and nothing else. ## #64 — what to count `request:end` is terminal and universal and is the request counter; `request:failed` is *handler code threw*, which no status expresses since a rendered 422 is also a throw; `route:unmatched` is *no route ran*. The last two are additional. All of it now sits on `EventPayload`, where a consumer reads it. `request:failed` carries `status` too. Getting it required moving the emission after the render — `onError` may turn any throw into any status, so deriving one from the error would be a guess a custom renderer makes wrong. ## #80 — the P0, found by asking what happens when the renderer throws Moving that emission meant asking what `renderError` does when `onError` throws. It exited the process, code 1. Same shape as the CORS predicate crash in #78 and easier to reach: not a cross-origin request but *any* request that errors. The stack goes through `unmatchedOutcome`, so **the renderer produces the 404 too** — an app with a custom renderer and no matching route was one request away from exiting. And it is the worst-timed crash there was: the renderer only runs once something has already gone wrong, so an error occurred, the code written to report it failed, and the server ended instead of degrading. All six call sites funnel through `renderError`, so the guard goes there once. A throwing renderer falls back to `errorToResponse` — what the option overrides, and therefore the honest fallback. The original error still gets its response; the renderer's failure is logged separately naming both errors. `viewEngine` was checked in the same pass, since it is the other user callback on a render path. It has a boundary — the pipeline's catch — so a broken engine is a 500. Asserted now rather than left to inference. ## #65 — something bounded to label on `route:unmatched` and the `request:end` that follows carry `route: '<unmatched>'`, exported as `UNMATCHED_ROUTE`. A matched path is bounded by the route table; an unmatched one is bounded by nothing, and a scanner walking `/aaa`, `/aab`, `/aac` is a memory leak with a metrics backend attached. Found while reading the emission order: `route:unmatched` fires *before* `unmatchedOutcome`, which can serve a static file and answer 200. So it is not a synonym for failure, and a consumer counting it as 404s over-counts every asset served. Documented. ## #67 — three things that were one decision `@needs('events')` reaches `{ on }` — the same narrowing plugins get, exported as `Events`. The `Bus` itself stays out of the graph, because a node that could reach it could also `emit`, and an observation channel anything can write to is not one. `@needs('bus')` now fails saying that and pointing at what does work, because documenting it reaches a reader while the boot error reaches the person typing it. **Behaviour change:** `logger`, `rooms`, `events` and `bus` are reserved names now, enforced in `setRunner` — the single point modules, plugins and mesh exports all register through. Built-ins used to be registered only if the name was free, so a provider called `logger` silently replaced the framework's own and every `@needs('logger')` in the app got something that was not the logger core writes to. This replaces a test that asserted that shadowing as a feature, and it can fail an app that boots today. `test/bus-access.test.ts` proves the three legitimate doors all still work and that only `emit` stayed put. ## #74 — the trivial one The gate exposes `limited` rather than each adapter re-deriving it from `opts`, because the gate is what knows: `acquire()` returns `true` for both "admitted" and "not counting", which is why a caller reading only the result paid either way. Unchanged where a budget *is* configured — the listener is what releases a slot on a mid-handler disconnect — and the test asserts both halves so a fix for one cannot become a regression in the other. ## Gates on `0adab69` lint 0 errors (48 pre-existing warnings) complexity:check every function <= 25 typecheck clean format:check clean build clean npm test 495 passed / 72 files (466 / 69 at branch point) npm test (node 18) 494 passed (CI's build floor) test:deno 17 passed test:bun 7 passed test:edge PASS deno publish dry run clean ## Not here The documentation. `guides/observability.md` already has the bus-access and what-to-count sections on the docs branch; the rest of this tier follows before that branch goes up as one pull request, so the site moves once rather than per tier. The version bump and the dated changelog section still belong to the cut.
chrnx added 7 commits 2026-09-01 16:09:31 +00:00
`request:start` had one emitter and `request:end` had three, so the pairing every
consumer would reasonably assume held by accident rather than by promise. The
`maxConcurrentRequests` merge added the first terminal path that breaks it: a shed
request emits `request:end` and nothing before it, because it never reaches `handle()`.

Both shed paths now emit the pair, through one shared `emitShedPair` rather than two
copies — the guarantee lives in one place, and the two adapters already had a duplicate
of this block between them.

## Why the pair rather than documenting `request:end` as standalone

Both were defensible and the alternative was seriously considered. It loses on one
concrete case: the obvious in-flight gauge.

    bus.on('request:start', () => inFlight++);
    bus.on('request:end',   () => inFlight--);

Without the guarantee that drifts negative the moment a server sheds — under load, which
is exactly when an operator is reading it, and it fails by producing a plausible wrong
number rather than an error. Documenting "do not do that" asks every consumer to read
the warning; guaranteeing the pair makes the obvious code correct.

Emitting a `start` for a shed request is not a fabrication, which was the objection worth
answering: the request *did* arrive. The gate refused it before the router, and
`request:start` says a request arrived, not that it routed.

The cost of the guarantee is that it binds every future emitter, and that is real. It is
paid down by `test/lifecycle-pairing.test.ts`, which enumerates every response shape —
200, 404, 500, 413, 204 preflight, and 503 on both adapters — and asserts the pair and
the shared `requestId`. The constraint is now something CI enforces rather than something
a contributor has to remember. Verified non-vacuous: dropping the `request:start`
emission fails the two shed rows and nothing else.

Also writes down, next to `LifecycleEvent` where a consumer reads it, the half of this
that is not new: `request:end` is terminal and universal and is the one to count;
`request:failed` and `route:unmatched` describe the same request and are additional.

Closes #73.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
One request that throws emits three events, and nothing said which of them to count. The
obvious first implementation counts the failure twice — once from `request:failed` and
once from the `request:end` that follows it, under whatever status it invents for the one
that had none. `route:unmatched` overlaps the same way, so a 404 is two events and one
outcome. Both mistakes are silent: the metrics simply lie.

The division of labour is now stated on `EventPayload`, where a consumer reads it:
`request:end` is terminal, universal and carries the status the client received, so it is
the request counter; `request:failed` means *handler code threw*, which no status can
express on its own since a rendered 422 is also a throw; `route:unmatched` means no route
ran. The last two are additional, not alternatives.

`request:failed` also carries a `status` now, so an error counter can break down by
status without joining back through `requestId` for something the emitter already had.

Getting it required moving the emission after the render rather than before, and the two
reasons are worth keeping: `onError` may turn any throw into any status, so deriving one
from the error would be a guess a custom renderer makes wrong — and the emission sits in
`finally`, so a renderer that *throws* cannot swallow the event. That case is the only one
where the event has no status, and the field's doc says so rather than leaving a reader to
find out.

Closes #64.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
`createApp({ onError })` is the advertised way to render errors, it runs on the request
path, and it ran with no boundary around it. A renderer that threw did not produce a 500
— it took the process with it, exit code 1.

This is #72 in a different callback, and easier to reach: not a cross-origin request, but
*any* request that produces an error. The stack from the reproduction goes through
`unmatchedOutcome`, so the renderer produces the 404 too — an app with a custom renderer
and no matching route was one request away from exiting.

It is also the worst-timed crash there was. The renderer only runs once something has
already gone wrong, so the failure mode read: an error occurs, the code written to report
it fails, and instead of a degraded report the server ends.

All six call sites funnel through `renderError`, so the guard goes there once. A renderer
that throws falls back to `errorToResponse` — the built-in rendering, which is exactly
what the option overrides and therefore the honest thing to fall back to. The original
error still gets its response; that is the whole point. The renderer's own failure is
logged separately and names both errors, because they are different errors and a reader
chasing the wrong one loses an afternoon.

Verified against the built `dist/` with the issue's own reproduction: `404`, process
alive, one error line naming the renderer.

`viewEngine` was checked in the same pass, since it is the other user callback on a render
path and its having a boundary was not something to assume. It has one — the pipeline's
catch covers it, so a broken engine is a 500. Asserted now rather than left to inference.

Amends the `request:failed` test added a commit ago, which asserted the crashing behaviour
as its baseline: a throwing renderer now yields the fallback's status rather than none.
The `finally` that carries the emission stays, demoted from first line of defence to
second — a failure whose reporting is what broke is the worst one to lose.

Closes #80.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
`route:unmatched` was the one terminal request event carrying no `route`, so the only
subject a consumer could reach was `name` — and on that event `name` is the concrete,
caller-controlled path.

The cardinality doctrine is already written into the codebase and it is right: a metrics
consumer that labels a counter with a concrete path gets one label per distinct URL, and
unbounded label cardinality takes down the metrics backend rather than the application.
That reasoning applies hardest to exactly this event. A matched route's path is bounded by
the route table; an unmatched one is bounded by nothing at all, and a scanner walking
/aaa, /aab, /aac is a memory leak with a metrics backend attached.

The framework did not create the trap knowingly. It just left the one field that avoids
it empty, precisely where it mattered most.

`route:unmatched` and the `request:end` that follows it now both carry
`route: '<unmatched>'`, exported as `UNMATCHED_ROUTE` so nobody has to hardcode the
string to compare against it. A single series for every path that was never a route,
rather than a fallback each consumer invents and some get wrong. It cannot collide with a
pattern a user can declare, since those start with `/`.

Written down alongside it, because a field alone does not teach the rule: `name` is
caller-controlled and must not be a metric label — true of `request:start` too, which is
not terminal and so was never the one anyone counted. And `route:unmatched` is not a
synonym for failure: a static file served by `createApp({ static })` matched no route
either and answers 200, so the status comes from `request:end`, like everything else.

Closes #65.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
Three things that turned out to be one decision.

## `@needs('events')`

`app.bus` is public and `Bus` is an exported type, but the bus was not a graph token, so
`@needs('bus')` failed at boot. There is a good reason for the difference — putting the
`Bus` in the graph hands `emit` to every node and turns a one-way observation channel into
something anything can forge events on — and it was written down nowhere, inferable only
by reading `provideBuiltins` and noticing what is absent.

The read-only half is a token now: `events`, bound to `{ on }`, the same narrowing plugins
already get. `@needs('events')` works the way `@needs('logger')` does, which is the
symmetry a reader expects, and `emit` still belongs to nobody.

A plugin remains the right home for observation and the docs say so, because it gets
`on` and `onShutdown` *together*. The token gives the subscribe half alone: a `@Provider`
that subscribes should release in `dispose()` — `on()` returns its own unsubscribe — and a
`@Step` should not subscribe at all, since it runs per request and would add a listener
each time.

## The boot error teaches it

Documenting it prevents the mistake for a reader; it does nothing for the person typing
`@needs('bus')`, which is who the issue was about. That need now fails with what it should
have used and why, rather than the generic "nothing provides it" plus a nearest-match that
has nothing near enough to suggest.

## Framework names are reserved

**Behaviour change.** Builtins were registered with `if (!runners.has(name))`, so a
provider called `logger` silently replaced the framework's own and every `@needs('logger')`
in the app got something that was not the logger core writes to — a divergence you
discover from a log line that never appeared. `logger`, `rooms`, `events` and `bus` are
reserved now, enforced in `setRunner`, which is the single point modules, plugins and mesh
exports all register through. Taking one is a boot error naming it.

`bus` is reserved without being provided, so `@needs('bus')` cannot resolve to whatever a
user happened to call `bus` and be wrong in silence.

This replaces a test that asserted the old shadowing as a feature.

Closes #67.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
Every request registered a `close` listener and set an acquire flag, including in the
apps — most of them — that never set `maxConcurrentRequests`. `acquire()` returns `true`
unconditionally when the limit is unset and `release()` early-returns for the same reason,
so both the closure and the EventEmitter registration were pure overhead: a per-request
cost, on the hot path, for a feature that is off.

The gate exposes `limited` rather than each adapter re-deriving it from `opts`, because the
gate is what knows. `acquire()` cannot say it — its `true` means "admitted" and "not
counting" alike, which is exactly why a caller reading only the result pays either way.

With a budget configured nothing changes: the listener is what releases a slot when a
client disconnects mid-handler, and it comes back. The guard is about the unset case, not
about dropping the feature, and the test asserts both halves so a fix for one cannot
quietly become a regression in the other.

Verified non-vacuous: reverting `src/` fails the assertion with the listener it counts.

Closes #74.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
docs(changelog): record the tier-2 work in Unreleased
CI / dco (pull_request) Successful in 5s
CI / build (pull_request) Successful in 28s
CI / runtimes (bun) (pull_request) Successful in 14s
CI / runtimes (deno) (pull_request) Successful in 18s
CI / runtimes (edge) (pull_request) Successful in 12s
0adab69466
The event stream's contract, the read-only `events` token, the reserved framework names,
the unmatched-route label, the budget bookkeeping, and the `onError` crash.

Grouped by what a reader is looking for rather than one bullet per commit: the three
observability issues describe one contract and read better as one entry plus the two
fields it needed.

The reserved names get their own paragraph saying plainly that they **can fail an app
that boots today**, because a changelog that buries a breaking change under a feature
heading is how someone finds it from a stack trace instead.

Heading still `[Unreleased]`. The version and the date belong to the cut.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
chrnx merged commit b75158eddc into develop 2026-09-01 16:11:16 +00:00
Sign in to join this conversation.