Skip to content

fix: close the defects an eighth audit found - #9

Merged
dinhdev666 merged 2 commits into
mainfrom
fix/eighth-audit-findings
Nov 24, 2026
Merged

dinhdev666 merged 2 commits into
mainfrom
fix/eighth-audit-findings

Conversation

@dinhdev666

Copy link
Copy Markdown
Owner

What

The eighth audit was two reviews: one of shutdown, one of the test suite. Its findings are fixed here.

Shutdown

  • A stop signal before the router is ready. Nothing was listening during the first ~120 ms of module instantiation, so SIGTERM on a starting container killed the process by signal (-15). src/boot-signals.js is the first import of index.js and answers SIGINT/SIGTERM with a logged, clean exit 0. A unit case spawns a child that arms the handler and signals it.
  • A hijacked response was invisible to the drain. A reply.hijack() route never reaches onResponse, so shutdown could not tell that an SSE stream or a CSV export was still being written. src/http/hijacked.js tracks those sockets; the drain counts them, and only after the window are they destroyed — truncation stays a last resort.
  • One unbounded step could outlive the ceiling. upstream.close(), the session sweeper and db.close() are now bounded (1 000 / 400 / 900 ms) and log which one did not finish. Close and drain share a single 20 s window; the hard ceiling is 23 s, inside the 25 s stop grace that docker/README.md and docker-compose.yml both publish.

The gap that remains, stated rather than papered over: a signal arriving during ESM instantiation, before any statement of ours runs, still dies by signal. That is inherent to static imports and is not claimed as covered.

Tests that could not fail, now can

  • The keep-alive case searched for : keep-alive, which the router writes itself, so it matched with or without a forwarded comment. The fixture now writes : stub keep-alive and the case also asserts the comment arrived during the silence.
  • The log status filter passed on [].every(...). It now creates its own row, asserts inclusion of that row and exclusion of it under a different status.
  • injectedBytes was only type-checked; it is now asserted to be 0 for a stream that carried its own [DONE] and exactly data: [DONE]\n\n for one that did not.
  • The overview KPIs accepted null; they now require a measured success rate and p50.
  • "A 400 is never retried" never contacted a provider. The fixture gained a STUB400 marker, and the case asserts the client sees the provider's own message and that the provider was contacted exactly once.
  • waitForHttp promised "below 500" and returned true for anything; it now honours that.
  • refused.status >= 400 became an exact 404 with the documented message.
  • The fixture now models the provider's max_concurrency, with inFlight/maxInFlight/saturated counters, and the capacity case asserts the peak never exceeded one.

Documentation drift

  • docker/README.md: the --entrypoint stat /app/config.json verification cannot work on a fresh container (the entrypoint creates that file), so it uses a started one; the claim about which tables test/unit-docs.mjs checks is now accurate; the shutdown paragraph describes truncation and the 23 s ceiling.
  • AGENTS.md: five first-party plugins, and src/server/schemas.js, src/http/hijacked.js, src/boot-signals.js on the repository map.
  • web/src/pages/Keys.tsx: the public route list now matches PUBLIC_PATHS — it was missing /status and the preflight — and a new documentation case compares the two so it cannot drift again.
  • README.md: the error table lists every code the product emits (invalid_credentials, unauthorized, metrics_unauthorized, internal_error, no_account_configured, metrics_token_unset), enforced by a lint.

Verification

  • pnpm run check — 62 files, imports resolved, 14 action references pinned.
  • pnpm run typecheck — clean.
  • pnpm run test:web — 17 passed.
  • pnpm run test:unit — 1 231 checks across six files (core 498, docs 158).
  • pnpm run build — clean.
  • SMOKE_PORT=8399 SMOKE_STUB_PORT=4499 node test/smoke.mjs — 405 of 405, 4 skipped.
  • SMOKE_SLOW=1 SMOKE_PORT=8403 SMOKE_STUB_PORT=4503 node test/smoke.mjs — 426 of 426.

Docker: no image was built for this change; nothing under docker/ changed except its documentation, which was verified against the running container commands already used in CI.

A shutdown review and a test-quality review of the suite turned up one
robustness gap and a set of assertions that could not fail.

Shutdown. A stop signal arriving before the router was ready killed the
process by signal, because nothing was listening yet; `SIGTERM` on a
starting container exited -15 instead of 0. `src/boot-signals.js` is now
the first import of `index.js` and answers both signals with a clean exit.
A response a route hijacked never reaches `onResponse`, so the drain had no
idea it was still streaming: `src/http/hijacked.js` tracks those sockets,
the drain counts them, and shutdown destroys what is left only after the
window. Every unguarded step - the upstream agents, the session sweeper,
`db.close()` - is now bounded, so a stuck one can no longer hold
`process.exit` past the ceiling. The budget is one 20 s window plus a 3 s
margin, which fits the 25 s stop grace both the image documentation and
`docker-compose.yml` publish.

Tests. Several assertions could not fail: the keep-alive case searched for
a string the router writes itself, the log filter passed on an empty list,
the byte counter was only type-checked, `waitForHttp` promised "below 500"
and accepted anything, and the 400-not-retried case never contacted a
provider. The fixture gained `STUB400`, a distinguishable keep-alive, the
switches it documented but did not implement, and a modelled provider
concurrency limit whose peak the capacity case now asserts. A new
documentation case keeps the fixture's switch table and the dashboard's
public-route list honest, and the error-code table in the README is
complete.

Documentation. The Docker verification command for `config.json` cannot
work on a fresh container and now uses a started one; the claim about which
tables the documentation lint checks is accurate; AGENTS.md carries the new
files, `src/server/schemas.js` and the correct plugin count.
The shutdown case for a streamed export slept half a second and then sent
SIGTERM, which is a guess: on a slower runner the signal arrived while the
request was still being dispatched, the drain correctly found nothing in
flight, and the case failed while the shutdown behaviour it was written for
was intact. It now waits for the CSV header on the socket, so the export is
provably streaming before the signal, and pauses only then.

The fixture grew to 50 000 rows because 5 000 is about half a megabyte and
loopback socket buffers can hold that much: the route finished writing into
the kernel and ended before the signal arrived, which is the same failure
from the other direction.

Both slow runs pass locally: 427 of 427, twice.
@dinhdev666
dinhdev666 merged commit 15e84c9 into main Nov 24, 2026
6 checks passed
@dinhdev666
dinhdev666 deleted the fix/eighth-audit-findings branch November 24, 2026 07:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant