Repository navigation
Commit 85f7a0d
fix(server): budget counters hold their limit per key, count in a registered cache, and keep the in-process fallback bounded (#29345)
<!-- claude-projects-attribution: {"github_login":"os-steve"} -->
_Requested by **Steve** · [project
thread](https://claude.ai/code/project/chan_01FdL1XeUyvVNxLreh3F4Qi9?thread=cmsg_01FdL1XeUyvVNxLreh3F4Qi9DDnuBdW9LnD9CpSuCNuHyX)_
Refs #29053
Clause-②: no
Metadata-protocol: none
## 维护者速读
- 改了什么:把 #29216 给 plugin-auth 定下的计数规则推广到 server 车道的其余预算计数器:runtime
的入站限流与端点限流(令牌桶)同一个键的更新在进程内串行;runtime、短信每日配额、notify 节点按地址的邮件限额都按同一规则解析
`cache` 服务(值注册、工厂注册都能拿到,内核自带的 degraded 内存替身不算共享缓存);core 的内存 cache
兜底在写入时清掉已过期的键,不必等再读一次。
- 为什么改:按维护者裁决「A+C 同意」的 C 半,规则是:每个预算计数器在并发请求下守住限额(同一个键的更新进程内串行),注册了共享
cache 时计在其中(ADR-0069 D2),进程内兜底保持有界。
- 风险与回滚:同一个键的更新会排队,不同键互不影响;没有 cache 插件时 runtime
的限流桶改走有界的进程内兜底,并照旧告警一次。跨节点仍没有原子自增(契约在 spec,本 PR 不动)。回滚即撤销本 PR。
-
先看:`packages/runtime/src/security/inbound-rate-limit.ts`(`SharedTokenBucketLimiter.consume`)、`packages/runtime/src/security/kernel-cache.ts`、`packages/core/src/fallbacks/memory-cache.ts`
的写入清扫。
- 两分钟验证:`pnpm --filter @objectstack/runtime exec vitest run
src/security/inbound-rate-limit-concurrency.test.ts
src/security/kernel-cache.test.ts
src/dispatcher-plugin.rate-limit-cache.integration.test.ts`,预期 3
个测试文件、15 个用例全部通过。
- 需要你做的:无,席位落地。
## What changes
The rule, for every budget counter in the server lane: **every budget
counter holds its limit under concurrent requests (updates to one key
are serialised in-process), counts in the registered shared cache when
one is registered (ADR-0069 D2), and its in-process fallback stays
bounded.** PR #29216 applied it to `@objectstack/plugin-auth`; this
applies it to the rest.
- **`@objectstack/runtime` token buckets.**
`SharedTokenBucketLimiter.consume` runs its read, refill, spend and
write for one key through `serializePerKey` (new, package-local
`src/security/per-key-serial.ts`). That covers the server-level inbound
limiter (`server.security.rateLimit`) and every declared endpoint
`rateLimit`, which share the class. Other keys run concurrently.
- **Cache resolution.** The dispatcher's inbound and endpoint limiters,
the daily SMS send quota (`@objectstack/service-sms`) and the `notify`
node's per-address email-field limit (`@objectstack/service-automation`)
resolve the `cache` service through a package-local `resolveKernelCache`
(`src/security/kernel-cache.ts`, `src/kernel-cache.ts`,
`src/builtin/kernel-cache.ts`): a value registration and a factory
registration both answer; the kernel's own in-memory stand-in, which
self-describes `degraded` (ADR-0076 D12), does not, so those counters
keep the bounded per-process fallback and its one-shot warning.
- **`@objectstack/core` in-memory cache fallback.** Expired entries are
evicted on write once the map reaches a sweep mark (twice the entries
left after the previous sweep, at least 1024), so the map holds at most
about twice its live entries without a key having to be read again. No
live entry (no TTL, or a TTL still running) is evicted; the
`ICacheService` contract is unchanged.
- **Changeset**: `.changeset/29053-server-budget-counters.md`, patch for
`@objectstack/runtime`, `@objectstack/service-sms`,
`@objectstack/service-automation`, `@objectstack/core`.
**Local copies, by the seat's decision.** `per-key-serial.ts` and
`kernel-cache.ts` are copies of the two internal plugin-auth helpers
#29216 added (same rule, same code), one per package that needs them.
Re-exporting them from plugin-auth's published `./rate-limit-storage`
entry would widen that entry, so they stay internal and are not
re-exported from any package.
## Why
ADR-0069 D2 promises a budget counted once across nodes when a shared
cache is registered, and a counter's limit is only a limit if it holds
when requests for one key arrive together. #29216 measured both halves
for the auth counters; the server lane's other counters follow the same
rule.
## Tests
- `packages/runtime/src/security/inbound-rate-limit-concurrency.test.ts`
(new, 6 cases): concurrent consumes of one key never exceed the budget,
in a cache whose calls each cross a macrotask, in the per-process
fallback through the middleware, and for a declared endpoint budget;
serialisation is per key; a failing update releases the next; nothing is
left in flight once the concurrent updates settle.
- `packages/runtime/src/security/kernel-cache.test.ts` (new, 7 cases),
against real `ObjectKernel` and `LiteKernel` contexts: factory, value,
none, the `degraded` stand-in, a non-store value, and a bucket counted
through it lands in the factory-registered cache.
-
`packages/runtime/src/dispatcher-plugin.rate-limit-cache.integration.test.ts`
(new, 2 cases), the composed dispatcher on a real `ObjectKernel` over a
socket: with a factory-registered cache the caller's bucket lives in it
and the budget holds; with only the stand-in, the stand-in stays empty
and the budget still holds.
- `packages/services/service-sms/src/sms-daily-quota-cache.test.ts`
(new, 2 cases) and
`packages/services/service-automation/src/builtin/notify-email-field-cache.integration.test.ts`
(new, 2 cases), on a real `ObjectKernel`: with a factory-registered
cache the counter lives in it and the limit holds; with only the
stand-in, the stand-in stays empty and the limit still holds.
- `packages/core/src/fallbacks/memory-cache-bound.test.ts` (new, 3
cases): the fallback stays bounded after many distinct keys expire
unread; live entries survive every sweep; an expired entry still reads
as absent.
- **Forced test edits**: none.
`scripts/engine-double-contract.pinned.json` gains the new notify test's
data double (written by `check-engine-double-contract.mjs --write`).
- Each pin goes red with its check removed (eight ablations, each
restored to the HEAD blob).
## Acceptance notes
- **Across nodes, unchanged.** `ICacheService` has no atomic increment,
so two nodes sharing one cache still read-modify-write against each
other. Adding one is a `packages/spec` contract change, outside this
lane.
- **What the rule costs.** Updates of one key wait for each other; other
keys are unaffected. The fallback cache's sweep costs O(1) per write
amortised: on this shared box, one million writes of distinct keys took
the same time as before within noise when all stay live, and less when
they expire (the map stays small).
- **The stand-in rule** matches #29216's choice: a cache-less
`ObjectKernel` deployment's runtime buckets move from the stand-in to
the bounded per-process fallback, which is just as process-local, and
the "no shared cache" warning now fires there.
## Verification
Final head `0c0c6185` (origin/main `39951d2a` merged in `edc9e043`; the
two commits after it touch only test harness files: the notify pin's
data double and its ledger row, and `service-sms`'s `vitest.config.ts`).
Each reading names the head it was taken on.
<details><summary>Tests and typecheck</summary>
- On `edc9e043`, under the verify lock: `turbo run build` of the four
packages and their dependencies, exit 0; `pnpm --filter PKG test`:
`@objectstack/core` 130 files / 2912 tests passed;
`@objectstack/service-sms` 8 / 109; `@objectstack/service-automation`
260 / 3337; `@objectstack/runtime` 518 files, 8346 passed, 19 skipped.
`pnpm --filter PKG typecheck` exit 0 for core, service-sms and
service-automation; runtime's first read was a missing optional-driver
build (no `dist` for `@objectstack/driver-turso`), and after `turbo run
build --filter='./packages/**'` it read exit 0 (`check:test-typecheck`
OK, debt ledger held).
- On `a7dd43d1`: the notify pin, 2 passed.
- On `0c0c6185`: `@objectstack/service-sms` test 8 files / 109 tests
passed, typecheck exit 0.
</details>
Each pin goes red with its check removed (eight ablations, each restored
to the HEAD blob).
<details><summary>Dispatch gates and lint</summary>
- `node scripts/pm/dispatch-gates.mjs --commands --repo
objectstack-ai/objectstack` on `0c0c6185` derived 70 commands (18 paths,
1005 changed lines, under the 3000-line threshold). All 70 run with
stdin from /dev/null and the exit captured before any pipe: 70 exit 0.
`--ran`: 70 derived, 70 run, 0 NOT-MEASURED, 0 UNRUN (a derived zero).
- Two families read red on the earlier head and were fixed in the two
commits after it: `check:engine-double-contract` (the notify pin's data
double now routes `findOne` through `assertEngineFindOnePredicate`, and
the pinned ledger gained its row via `--write`) and
`check:test-source-alias` (`service-sms` aliases `@objectstack/core` and
`@objectstack/spec/api` to source rather than widening the shrink-only
registry).
- `eslint --no-inline-config` over `packages/runtime`, `packages/core`,
`packages/services/service-sms`, `packages/services/service-automation`
on `edc9e043`: exit 0; over `packages/services/service-sms` on
`0c0c6185`: exit 0. The repo-wide run is CI's.
- `test:repo` is not run locally (no file of that project changed); it
is CI's `Test Core`.
- CI check-runs: not read yet at the time of writing.
</details>
## Human Minimum
- [x] 1. Schemas start as Zod; `packages/spec` holds no runtime logic.
(No schema touched.)
- [x] 2. Every name I added uses the casing for its kind.
- [x] 3. No formatter ran; `pnpm lint` passes. (eslint over the four
touched packages; the repo-wide run is CI's.)
- [x] 4. One issue, one branch off an up-to-date `main`, one pull
request.
- [x] 5. A change that publishes carries a changeset, and both
declaration lines are filled in here and in the changeset.
- [x] 6. `packages/spec` touched: its generated artifacts are
regenerated and committed. (Not touched.)
- [x] 7. No hand edits to `content/docs/references/`,
`packages/*/CHANGELOG.md` or `content/docs/releases/`.
- [x] 8. Title, body and comments are in English. (The 维护者速读 section is
Chinese by the repo's maintainer rule.)
- [x] The *Before Submitting* list in CONTRIBUTING.md is done.
<!-- signed: domain:server#5 · os-steve ·
session_01ADHkmPcNztYJ49AQY8UaeN -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent f2f6bf0 commit 85f7a0d
18 files changed
Lines changed: 965 additions & 43 deletions
File tree
- .changeset
- packages
- core/src/fallbacks
- runtime/src
- security
- services
- service-automation/src/builtin
- service-sms
- src
- scripts
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
3 | 6 | | |
4 | 7 | | |
5 | 8 | | |
| |||
15 | 18 | | |
16 | 19 | | |
17 | 20 | | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
18 | 31 | | |
19 | 32 | | |
20 | 33 | | |
21 | 34 | | |
22 | 35 | | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
23 | 43 | | |
24 | 44 | | |
25 | 45 | | |
| |||
38 | 58 | | |
39 | 59 | | |
40 | 60 | | |
41 | | - | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
42 | 64 | | |
43 | 65 | | |
44 | 66 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
8 | 7 | | |
9 | 8 | | |
10 | 9 | | |
| |||
21 | 20 | | |
22 | 21 | | |
23 | 22 | | |
| 23 | + | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| |||
911 | 911 | | |
912 | 912 | | |
913 | 913 | | |
914 | | - | |
| 914 | + | |
915 | 915 | | |
916 | 916 | | |
917 | 917 | | |
| |||
2190 | 2190 | | |
2191 | 2191 | | |
2192 | 2192 | | |
2193 | | - | |
| 2193 | + | |
2194 | 2194 | | |
2195 | 2195 | | |
2196 | 2196 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
0 commit comments