Skip to content

fix(core): a signed-in request reads its session once with the rate limiter armed or a gate active on REST - #35703

Merged
objectstack-fleet[bot] merged 7 commits into
mainfrom
claude/issue-35606-request-session-read-memo
Nov 20, 2026
Merged

objectstack-fleet[bot] merged 7 commits into
mainfrom
claude/issue-35606-request-session-read-memo

Conversation

@objectstack-fleet

@objectstack-fleet objectstack-fleet Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Requested by Bill · project thread

repo:objectstack#5 · os-bill · session_01Utp6CZiaBg6rk7dpy1s9Me
Fixes #35606
Clause-②: no
Metadata-protocol: none

维护者速读

  • 用户能看到什么:开启了入站限流、或在数据接口上开启了登录策略门禁的部署里,一个已登录的请求只查一次会话(原来要查两到三次),响应更快;谁能访问、返回什么状态都不变。
  • 为什么做:托管数据库上每查一次会话就是两次远程往返,同一个请求重复查纯属浪费;这是此前「门禁不再重复读会话」那次修复的后半段。
  • 出错会怎样:如果共享出了错,一个请求可能用到稍早的会话判断;为此只在同一请求内共享,期间有任何写入、会话过期或查询失败都重新查,登录相关接口(组织的访问地址白名单在那里判断)一律自己重查;可以直接撤回,不涉及数据。
  • 要你做什么:无

What changes

One request-scoped session-read carrier in core, in the shape of the request-grants memo, that the inbound rate limiter, the identity step of every door and REST's reread-when-active gate read through, so one signed-in request reads its session once.

  • packages/core/src/security/request-session-read.ts (new). An AsyncLocalStorage scope (openRequestSessionRead / withRequestSessionRead / withoutRequestSessionRead / readRequestSession, exported from @objectstack/core). A stored read is served to a later reader of the same request only when: same auth session API object and every header equal (the read's input is decided by those headers); no engine write has landed or is in flight since the read opened (engine write epoch + the grants memo's write observer: engineWriteMark / engineWritesQuietSince, added to request-grants-memo.ts); the session has not expired. A read that throws is never stored, so each reader keeps its own outage answer. Served values are clones. A scope closes when its request settles; nothing survives a request.
  • request-execution-context.ts. resolveRequestExecutionContext joins the scope open where it runs (or opens one for its own call), and its session arm (readSession) and decideAuthGate (reread-when-active) both read through the carrier. readWriteEpoch is now exported from its module for the carrier.
  • Runtime limiter. resolveSessionPrincipalId / resolveSessionData read through the carrier with the engine the auth service reads through (dispatcher-plugin.ts passes objectql).
  • Hono transport (adapter.ts). The middleware seam runs its chain in a scope of its own (the limiter's read) and hands that scope to the route it serves only when the route was registered through IHttpServer and every handler Hono ran between the seam and the route is a whole-server middleware (matchedRoutes, /*). A route behind a path-scoped middleware or a yielding route, and every raw-app route, read for themselves. Every request starts outside any scope (withoutRequestSessionRead), so a request served in process inside another never reaches the outer one's reads. The seam and the route now share one header reading (readRequestHeaders, host backfilled from the URL), so their keys agree.
  • Why an async scope and not a request-attached carrier: the seam and the route each get their own request object built from the same request, and core's readers receive only Headers; nothing attached to either object reaches the other or core. The Hono context is used only to hand the seam's scope handle to the route of the same request (a WeakMap keyed by the context, deleted when the route answers).
  • Why the path-scoped rule: plugin-auth's door under the auth base path is a middleware scoped to that path; it carries the request's address to the session reads behind it, and the organization's IP list is judged on those reads only (ADR-0069 D5). The limiter's read is made in front of that door, so it must never answer a reader behind it. The rule keeps it from crossing; pinned below on the real door.

Per door and posture, warm request, getSession / sys_session by token / sys_user by id, real kernel:

Door · posture origin/main 03865ae995 this branch
(a) limiter armed · dispatcher GET /api/v1/packages 2 / 2 / 2 1 / 1 / 1
(a) limiter armed · REST GET /api/v1/data/pin_note 2 / 2 / 2 1 / 1 / 1
(b) gate active · REST 2 / 2 / 2 1 / 1 / 1
(b) gate active · dispatcher 1 / 1 / 1 1 / 1 / 1
(c) both · dispatcher 2 / 2 / 2 1 / 1 / 1
(c) both · REST 3 / 3 / 3 1 / 1 / 1
(d) default · dispatcher and REST 1 / 1 / 1 1 / 1 / 1
limiter armed · auth mount GET /api/v1/auth/client-address (control) 2 2 (reads for itself behind the door)

Who is admitted or refused, and with which status, does not change.

Tests

  • packages/runtime/src/http-dispatcher.session-read-once.pin.test.ts (rewritten, real kernel: ObjectQL + sqlite-wasm + Hono + AuthPlugin + SecurityPlugin + REST + dispatcher; three kernels: gate, limiter, both). ⭐ one read on (a)(b)(c) for each door. CONTROLS: held session 403 with the posture code (both doors), revoked 401, anonymous 401 with no sys_session statement, concurrent requests with two tokens each get their own answer on one read each, limiter still keys by principal and refuses over budget (30 x 403 then 429 with retryAfterSeconds, decided on one read; another principal untouched), a failing read keeps its outage answer (REST 401 / dispatcher 503 SERVICE_UNAVAILABLE, never served), a failing limiter read is not served (route reads afresh), a revocation landing between the limiter's read and the route's is seen (401 both doors), and the auth-mount control above. 26/26 green at 655c4e2399.
  • packages/plugins/plugin-hono-server/src/session-read-scope.pin.test.ts (new, real Hono app): route past whole-server middleware is served the seam's read; a route behind a path-scoped middleware reads for itself inside that middleware's ambient state; a route behind a yielding route reads for itself; a raw-app route reads for itself; a request served in process inside a route reads for itself. 5/5.
  • packages/core/src/security/request-session-read.test.ts (new): served/clone, isolation (no scope, other headers/API, interleaved scopes, continuation after close, join, handle across stretches, withoutRequestSessionRead), freshness (write landed, write in flight, non-write epoch bump, expired, throw not stored, unwatchable engine). 15/15.
Reverse verification and ablations (one-off; nothing left in the tree)
  • At origin/main 03865ae995 (runtime/core/hono sources reverted, pin file kept) the final runtime pin file (26 tests) went 10 red / 16 green: every ⭐ and every one-read control red, e.g. expected { getSession: 3, sys_session: 3 } to deeply equal { getSession: 1, sys_session: 1 } on (c) REST.
  • Admission check ablated (seamReadsReachRoute answering true), via scripts/ablation-replace.mjs (anchor 1 to 0, blob 0da067a3ecff to 29dc9cac98bb): the path-scoped-middleware and yielding-route pins go red (2 failed / 3 passed); restored, blob equals HEAD, git diff HEAD empty.
  • Previous request-wide scope layout (adapter and carrier at 9244357e1a, rebuilt, ablation-dist-preflight confirming the marker absent from plugin-hono-server/dist): the auth-mount control goes red, expected 1 to be 2 (the limiter's read answered the reader behind the door). Restored, hashes equal HEAD, rebuilt, preflight confirms the marker back in dist/.

Acceptance notes

  • The carrier's guard against ambient read state is structural: a whole-server middleware (use('*')) registered after the seam that changed a session read's ambient state would defeat it. None does in the tree (the only such state is plugin-auth's door, path-scoped). Stated in the carrier's docblock.
  • Conservative fallbacks, unchanged from origin/main: a dispatcher or REST route behind a path-scoped middleware or a yielding route, and every raw-app route (plugin-auth's mounts, the /auth/me/* endpoints), with the limiter armed, still read twice (limiter + route).
  • A write from another process lands unobserved by the write observer: a later reader in the same request is served the session as of the request's first read, milliseconds earlier.
  • New public exports from @objectstack/core: openRequestSessionRead, withRequestSessionRead, withoutRequestSessionRead, readRequestSession, types RequestSessionRead, RequestSessionReadHandle. Module-level exports (not re-exported from the index): readWriteEpoch, engineWriteMark, engineWritesQuietSince, EngineWriteMark.
  • (d) default posture measured 1 / 1 / 1 at BASE and on this branch (both doors and the auth mount); the issue's premise does not name it as doubled.

Verification

Read first, by risk:

  1. packages/plugins/plugin-hono-server/src/adapter.ts: seamReadsReachRoute, the middleware seam's scope (open, run chain, hand off, close after next()), and sessionReadScopeForRoute in wrap.
  2. packages/core/src/security/request-session-read.ts: readRequestSession (serve rule, store rule) and the residuals in the docblock.
  3. packages/core/src/security/request-execution-context.ts: the joinRequestSessionRead wrapper and the two consult points.

Two-minute check: pnpm --filter @objectstack/runtime exec vitest run --maxWorkers=2 src/http-dispatcher.session-read-once.pin.test.ts → Tests 26 passed (26) (needs @objectstack/plugin-hono-server built).

Local runs at 655c4e2
  • Typecheck: @objectstack/core exit 0, @objectstack/plugin-hono-server exit 0, @objectstack/runtime exit 0 (check:test-typecheck: OK).
  • @objectstack/core vitest run --project local: 154 files, 3670 tests passed.
  • @objectstack/plugin-hono-server vitest run: 43 files, 581 tests passed.
  • @objectstack/runtime vitest run --project local: 631 files, 11649 passed, 25 skipped.
  • Derived gate families (node scripts/pm/dispatch-gates.mjs --commands, 76 commands): all 76 exit 0; node scripts/pm/dispatch-gates.mjs --ran with recorded exit codes: "76 derived, 76 run, 0 NOT-MEASURED, 0 UNRUN" (a derived zero). check:nul-bytes OK.
  • node scripts/pm/check-governed-merges.mjs --branch HEAD: 0 of 12 paths hit the register, NOT governed; +1383 / -137.
  • node scripts/pm/overlap-check.mjs --branch=claude/issue-35606-request-session-read-memo: merges cleanly with origin/main c03a8a3428, 0 PR heads; VERDICT clean exit 0.
  • test:repo, pnpm lint and the full farm: CI.

Generated by Claude Code

… middleware

The Hono transport now runs its middleware chain in a session-read scope of
its own and hands that scope to a route it registered only when every
handler between the two is a whole-server middleware. A route behind a
path-scoped middleware (the auth mount's door, which carries the request's
address to the session reads behind it) and a raw-app route read for
themselves, and every request starts outside any scope, so a request served
in process inside another never reaches the outer one's reads.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Utp6CZiaBg6rk7dpy1s9Me
@github-actions github-actions Bot added size/xl documentation Improvements or additions to documentation tests tooling Fix lands in tooling, not a product package — enters pm:queue only with Unblocks: or a named surface labels Nov 20, 2026
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

📓 Docs Drift Check

This PR changes 3 package(s): @objectstack/core, @objectstack/plugin-hono-server, @objectstack/runtime, touching 35 documentable anchor(s). ⚠️ 1 changed file(s) yielded no anchor (packages/plugins/plugin-hono-server/README.md), so the pages documenting them are NOT COVERED by this run — this is not a clean bill of health for those files.

5 hand-written doc(s) NAME something this change touched and may need an implementation-accuracy re-verification:

  • content/docs/api/index.mdx (via HonoHttpServer (symbol, a top-level class))
  • content/docs/api/wire-format.mdx (via HonoHttpServer (symbol, a top-level class))
  • content/docs/deployment/production-readiness.mdx (via createDispatcherPlugin (symbol, a top-level function))
  • content/docs/plugins/packages.mdx (via createDispatcherPlugin (symbol, a top-level function))
  • content/docs/protocol/kernel/error-handling.mdx (via HonoHttpServer (symbol, a top-level class))

⛔ 1 release-owned page(s) also name something this change touched. These are read-only:

  • content/docs/releases/v18/18-5.mdx (via HonoHttpServer (symbol, a top-level class))

content/docs/releases/ is RELEASE-OWNED (AGENTS.md "Documentation Guardrails"): release
notes are written centrally at release time, and a code PR that edits them is the exact PR
that guardrail exists to stop. They are still audited — read-only. If one of them is actually
wrong, file an issue or open a dedicated docs-only PR; do not edit it here.

What this run could not see
  • 1 changed file(s) yielded no anchor (packages/plugins/plugin-hono-server/README.md) — pages documenting those are invisible to this run
  • 8 name(s) were too generic to anchor anything (single lowercase words)
  • the SDK route bridge reached 65 of 247 client-bound route-ledger rows — the other 182 have no registrar path: tail to select them, so pages documenting THEIR client methods cannot appear above, on this or any run. Of those 182: 0 are remediable by widening that discovery convention (an in-repo file declares the path; the convention did not scan it); 59 are structural — on a ledger where NOT ONE row is declared in-repo, so no discovery change reaches them at any price; 123 are undecided (no in-repo declaration, on a ledger that has other in-repo registrars — absence and an unreadable spelling are not distinguishable here). The rows themselves: node scripts/docs-audit/affected-docs.mjs --bridge-coverage
  • a page that states a rule by its inputs shares no identifier with the emitter that implements the rule, so an emitter-only diff cannot list it — not on this run and not on any run. Measured on fix(driver-sql): emit varchar(maxLength) for a text field a declared index keys on #11430: content/docs/protocol/objectql/types.mdx documents the text-family column mapping by the ObjectQL type names it maps FROM (text / textarea / html) while the diff changed createColumn; it went unlisted, and it was the page that diff falsified, in four places. No shared token exists to detect this on, so a rule your change carries has to be re-read by hand in the pages that restate it.
  • a key NAME is not a key, so the hand re-read the line above prescribes can land on the wrong schema. The same spelling is authorable on one governed type and a [REMOVED] tombstone on another for each of active, aria, joins, objects, template, tools and version (censused on [finding] tools is a key on BOTH AgentSchema (tombstoned, dead) and SkillSchema (live, cloud-attested), so a name-based search attributes skill examples to the agent key — it produced a false stop-the-line alarm on PR #19059 #19093 over the liveness ledger's governed types, top-level keys); nothing in a search result distinguishes the two, so a grep hit on a LIVE example reads as evidence about the DEAD key. Measured on fix(spec): the agent.tools liveness row says dead — it claimed live on a key the schema tombstoned #19059: content/docs/ai/agents.mdx was reported as contradicting the agent.tools tombstone over its tools: example at :161, which is inside the defineSkill({ block opened at :155 — the page was already correct. Settle ownership by PARSING the value against both schemas, never by the name: that literal PASSES SkillSchema, and as an AgentSchema it FAILS at tools with the tombstone prescription. ⛔ These names are not the whole class — a key retired through a .strict() guidance map leaves no tombstone in the walked shape and none of them here (tool.category, live as AIToolDefinition.category).

Coarse fallback — 59 page(s) merely mention a changed package (the pre-#9192 predicate, kept for the deliberately-wide backstop): node scripts/docs-audit/affected-docs.mjs --json 238661f47f3937c0cd79cebade410141763775ec → packageMentionDocs.

Which tree this was computed on

This run read content/docs from 4a3c0099629fcd81d91caf72f6f37438c0ca885c — the merge of head a78bdd775da709fbcd12a901c75d59b686965103 into base 238661f47f3937c0cd79cebade410141763775ec, which is what actions/checkout gives a pull_request run. Not the PR head.

A worktree cut from an older main holds a different content/docs, so re-deriving there can legitimately return a different list — that is a different tree, not a wrong row. To answer on the same tree:

# while this PR is open — GitHub drops the merge commit once it closes
git fetch origin 4a3c0099629fcd81d91caf72f6f37438c0ca885c && git checkout 4a3c0099629fcd81d91caf72f6f37438c0ca885c
# afterwards, rebuild it from the two parents, which stay fetchable
git fetch origin 238661f47f3937c0cd79cebade410141763775ec a78bdd775da709fbcd12a901c75d59b686965103 && git checkout -B drift-repro 238661f47f3937c0cd79cebade410141763775ec && git merge --no-ff a78bdd775da709fbcd12a901c75d59b686965103

node scripts/docs-audit/affected-docs.mjs --json 238661f47f3937c0cd79cebade410141763775ec

⚠️ That checkout carried uncommitted changes, so the commit above does not fully identify what was read.

Advisory only, and a precision-first one (#9192): a page is listed because it names a
symbol, wire route or SDK method this diff touched — not because it mentions a changed
package. Each row says which anchor put it there, so a wrong row is reportable rather than
merely annoying. To re-verify, run the docs-accuracy-audit workflow scoped to these files:
node scripts/docs-audit/affected-docs.mjs 238661f47f3937c0cd79cebade410141763775ec → pass the list as
args.docs, on the commit named under Which tree this was computed on.

@objectstack-fleet

Copy link
Copy Markdown
Contributor Author

repo:objectstack#5 · os-bill · session_01Utp6CXsZsKmgG1EcKp5bY4

维护者速读

  • 用户能看到什么变化:用户无感;开启限流或安全门的部署里,每个已登录请求查"你是谁"从两到三次降到一次,托管数据库上请求更快。
  • 为什么做:同一个请求被限流器、身份识别、安全门各查一次会话,白白多跑数据库往返。
  • 出错会怎样:最坏是把一个请求里已查过的会话再用一次;只在同一请求内、期间没有任何写入、会话没过期时才复用,谁能进谁被拒都不变。
  • 要你做什么:无。
  • 席位意见:通过;还差检查跑完全绿、补登记三个文件、更正 PR 说明里的一组数字(都是文字或等待),另有一个设计选择(A/B)请席位定。

席位核对

L1-a: ACCEPT, three items before queueing · head 655c4e239946fc89663b8687b77e1a44314e1c66 · Fixes #35606 (p1) · K=6 · merge-tree clean · not governed (0 of 12) · 1,520 lines · 2026-11-20T00:54Z

What it is. A request-scoped AsyncLocalStorage carrier (packages/core/src/security/request-session-read.ts) that lets the inbound rate limiter, the identity step and REST's ADR-0069 gate share one api.getSession read per request.

Safety read. I read the module, the grants-memo helpers and the adapter seam.

  • Never a cache. A scope is a fresh WeakMap per request and close() drops it. OUTSIDE never stores (open: false). The Hono adapter starts every request in withoutRequestSessionRead, installed from the constructor, so an in-process inner request never reaches an outer request's reads.
  • Served only when a fresh read would agree.
    • The key is the auth API object plus every header, names and values, JSON-encoded so no value can alias a separator.
    • The entry carries the engine's write epoch and observer counters, taken before the read. It is served only if no write started, landed or is still in flight since.
    • An expired expiresAt is not served. A throw is never stored. Values are structuredCloned both ways.
    • A reader with no observable engine stores nothing.
  • The IP-list door (ADR-0069 D5). seamReadsReachRoute hands the seam's scope to a route only when every handler Hono matched between the two is ALL on /* or *. A path-scoped middleware, such as plugin-auth's mount door, gives the route a fresh scope. Raw-app routes run outside every scope. Anything unreadable answers no.
  • Behaviour change, disclosed. The middleware seam now gets the same headers as the route, with host backfilled from the URL. The changeset says so.
  • Residual, disclosed. A revocation written by another process is unobserved for the rest of the request. That is at most milliseconds, and the same answer a read just before that write would have given.

Nothing here is a security finding.

Pins. request-session-read.test.ts (unit), session-read-scope.pin.test.ts (adapter) and http-dispatcher.session-read-once.pin.test.ts (real kernel, every door and posture). The CONTROLs cover held/403, revoked/401, anonymous with no read, the limiter still refusing over budget, concurrent tokens, kept outage codes, and the auth-mount door reading for itself. The relay reports BASE reverse 10 red of 26 and both ablations red; I did not re-run them (no install here).

R1: CI green on this head. At this reading: 22 success, 11 skipped, 32 in progress. REST's full suite last ran green locally on 9244357e1a. packages/rest is untouched since, and CI's Test Core shards cover it on this head.

R2 (text only): claim amendment. Claim 7024841348 names the carrier "beside" request-grants-memo.ts, plus request-execution-context.ts, the adapter and the runtime limiter. Three edited files are not named:

  • packages/core/src/security/request-grants-memo.ts (+44: EngineWriteMark, engineWriteMark, engineWritesQuietSince);
  • packages/core/src/security/resolve-authz-context.ts (readWriteEpoch exported);
  • packages/core/src/security/index.ts (four functions and two types exported from @objectstack/core).

One amendment line on #35606 closes it.

R3 (text only): correct the PR body's BASE reverse figures to the final run: 10 red / 16 green of 26, with (d) at BASE 1/1/1. The body still quotes the earlier 11 / 14 of 25.

For the seat (no L1 item).

  • Design A vs B. In A, which shipped, a whole-server use('*') middleware placed after the seam may sit between the seam's read and the route, and the route still reuses the read. That is safe only while no whole-server middleware changes a session read's ambient state, and the module note says "none does". B would refuse reuse past any later middleware. A is the measured saving. The invariant is prose plus seamReadsReachRoute's path test, and nothing stops a future use('*') that opens ambient state. If the seat picks A, a one-line note in the hono plugin's middleware docs is the cheap guard.
  • Semver. @objectstack/core gains public exports at patch. The seat has accepted patch for additive surfaces before (ruling 7023403692); flagged for consistency only.

Routing. Clause-② no, no packages/spec face, not governed: ux-3 marks it ready and lands it after R1–R3, once seat 5 has answered A/B. This is the auth path, so I recommend the seat's answer be on the PR before landing.

@objectstack-fleet

Copy link
Copy Markdown
Contributor Author

repo:objectstack#5 · os-bill · session_01Utp6CbUb7TcZK1XUAddUmQ

Seat ruling on the A/B choice (#35606), ahead of landing.

A stands (the shipped behaviour): a route may reuse the seam's session read across whole-server use('*') middleware, so OS_ROOT_DOMAIN hosts keep one read per request.

Why A: no whole-server middleware changes the session read's ambient state today, and B would give back the one-read win on root-domain hosts to guard against a middleware that does not exist.

Condition, in this PR: add the one-line note to the hono middleware docs saying that whole-server use('*') middleware must not change the state the session read depends on, because the seam's read is reused across it. If a later change needs such middleware, it moves the seam to B in the same PR.

Semver: @objectstack/core gaining public exports at patch is accepted, as on the calendar work.

Recorded at 2026-11-20T00:55Z.

…sion-read state

The middleware seam's session read is reused by routes past whole-server
middleware, so such middleware must leave the read's inputs alone.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Utp6CZiaBg6rk7dpy1s9Me
@objectstack-fleet

Copy link
Copy Markdown
Contributor Author

repo:objectstack#5 · os-bill · session_01Utp6CZiaBg6rk7dpy1s9Me

Landing · head a78bdd775d · after L1-a ACCEPT 7026892789 (R2 claim amendment on #35606, R3 body figures corrected) and the seat's A ruling 7026922808 (its condition, the whole-server middleware note in the hono plugin README, is commit a78bdd775d) · 2026-11-20T01:32Z

Probe Reading
Checks at head a78bdd775d 57 success, 11 skipped, 0 failed
check-governed-merges.mjs --pr 35703 NOT governed
Contract record not owed (no packages/spec/src change, Clause-② no)
Dogfood battery not owed (no !, no narrowing arm); CI Dogfood Regression Gate green

Status: pr_ready + automerge_enable. Fixes #35606: on MERGED the area posts the close-out.

@objectstack-fleet
objectstack-fleet Bot marked this pull request as ready for review November 20, 2026 01:32
@objectstack-fleet
objectstack-fleet Bot added this pull request to the merge queue Nov 20, 2026
Merged via the queue into main with commit 7c122ff Nov 20, 2026
69 checks passed
@objectstack-fleet
objectstack-fleet Bot deleted the claude/issue-35606-request-session-read-memo branch November 20, 2026 02:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation size/xl tests tooling Fix lands in tooling, not a product package — enters pm:queue only with Unblocks: or a named surface

Projects

None yet

2 participants