Repository navigation
Commit 270d160
fix(runtime): bound list_drafts verdict work by a count cap and a time budget (#39012)
<!-- claude-projects-attribution: {"github_login":"os-support-ai"} -->
_Requested by **Yibo Wang** · [project
thread](https://claude.ai/code/project/chan_01BVUyyPie8gHwoPM18iABwB?thread=cmsg_01BVUyyPie8gHwoPM18iABwBG4YD4RMAt72Ga5PdYYZDAQ)_
Fixes #38965
Claim for this branch: #38965 comment 7324088602
Clause-②: no
Metadata-protocol: none
## 维护者速读
- 用户能看到什么变化:环境里草稿很多时,助手调用「列出草稿」会在几十秒内答复。草稿仍然全部列出,但一次只给最近保存的最多 50
个草稿做发布检查,超过 20 秒就不再开始新的检查,其余草稿注明「未检查」,并提示按应用包缩小范围。
- 为什么做:云端搭建能力上线前的第二阶段修复,替积累了大量草稿的客户解决「一问就卡死、数据库被持续占用」的问题。
- 出错会怎样:最坏情况是部分草稿没有检查结论,助手会如实说明,不会误称可发布;改动只在服务端,回滚这个提交即可恢复原样。
- 要你做什么:无。
## What changes
`list_drafts` (the MCP draft door, and the in-process authoring tools
built from it) judged every pending draft one after another: one strict
draft read plus one publish judgement each, with no count bound and no
deadline. Measured on the real protocol over SQLite, one verdict costs
about 25 + P queries, where P is the number of drafts in its package (P
= 10 / 50 / 100 gave 35 / 75 / 125 queries per verdict). So a whole list
of verdicts costs P × (P + 25) queries. At 500 drafts that is about 262k
queries, or about 44 minutes at the roughly 100 queries a second the
card measured on sqld. The card saw the call still running after 29
minutes.
Now, in `packages/runtime/src/domains/mcp-authoring.ts`:
- **Every draft is still listed**, in the protocol's order, with the
same fields. The list is one read.
- **Count cap:** at most `LIST_DRAFTS_VERDICT_CAP` = 50 drafts are
judged per call. This matches the console draft review card's per-turn
maximum (`DRAFT_REVIEW_MAX`), so the drafts one turn saved fit under the
cap. On a local database 50 verdicts at P = 50 cost about 3,750 queries
(about 5 s measured).
- **Time budget:** no verdict is started once
`LIST_DRAFTS_VERDICT_BUDGET_MS` = 20,000 ms have passed since the call
began (the list read counts). The cap alone is no time bound on a remote
database. The verdict that is running when the time runs out finishes;
at P = 500 on sqld that is about 5 s. So the answer lands well inside
the MCP SDK's default 60 s request timeout. The budget reads
`Date.now()`, so the test spends it with a spy and no sleep.
- **Sequential:** verdicts still run one at a time. They share the
per-package closure map, and each one reads the database O(P) times, so
running them side by side would multiply the load on the database that
the card saw saturated.
- **Order:** the most recently saved drafts are judged first
(`updatedAt` descending). Drafts without a timestamp come last, and ties
keep the list's order. The drafts a caller most likely wants verdicts
for are the ones it just saved. The protocol's list has no defined order
(no `ORDER BY`), so judging in list order would pick an arbitrary set.
- **A draft past the bound** answers `verdict: null` with
`verdictWithheld: { code: 'MAX_RECORDS_EXCEEDED', message, status: 400
}`. This is the shape a withheld verdict already has, and the console's
draft review row (`DraftReviewVerdictRefusalSchema`) accepts it
(pinned). The code is the registered standard caller-side limit (the
request asked for more verdicts than one call reads), and its remedy is
the request's own: a narrower list. One code covers both bounds, and
each row's message says which one stopped it.
- **The answer** gains `judged` (always) and, when some drafts were not
judged, `notJudged`, one sentence that says how many and to pass
`package`. Both come ahead of `drafts`, so a reply cut short still
carries them.
- **The tool description** gains one sentence: verdicts are bounded,
`judged` counts them, the rest answer `verdict: null` with
`verdictWithheld`, and `package` narrows the list.
No input parameter is added (no paging, no `offset`), so the input
schema is unchanged. That keeps the claim's `Clause-②: no`. A package
with more than 50 drafts still gets only its 50 most recent judged;
paging verdicts would widen the input schema and is left out.
## Tests
`packages/runtime/src/domains/mcp-authoring-38965-list-drafts-bound.test.ts`
(new). The stand-in protocol lists 500 flow drafts and counts
`getMetaItem` and `draftPublishJudgement`:
1. At 500 drafts: all 500 rows are listed in order; reads = judgements =
`judged` = the cap. The judged set is exactly the 50 most recently saved
drafts. Every other row has `verdict: null` with `verdictWithheld`
`MAX_RECORDS_EXCEEDED` / 400, and that object parses under
`DraftReviewVerdictRefusalSchema`.
2. Time budget: a `Date.now` spy advances 3 s on each strict read.
Exactly `ceil(20000 / 3000)` = 7 verdicts start, which is below the cap;
the rest are withheld.
3. Under the bound (3 drafts): every draft is judged, `judged` = 3, and
there is no `notJudged`.
<details><summary>Red on today's code, ablations, restore</summary>
- Today's `mcp-authoring.ts` (base `38ab473c3e`, blob `3d9009bbfa`)
against the new test: **3 failed** (`expected 500 to be undefined` on
the read count; `expected { Object (count, drafts) } to match object {
count: 3, judged: 3 }`).
- Ablation 1, cap guard deleted (`node scripts/ablation-replace.mjs`,
anchor 1 to 0, blob `01bb9c7693` to `49d566da17`): case 1 red, `expected
500 to be 50`. Restored: blob equals HEAD and `git diff HEAD` is empty.
- Ablation 2, budget guard replaced by `if (false)` (blob `01bb9c7693`
to `03662ccd29`): case 2 red, `expected 50 to be 7`. Restored: blob
equals HEAD and `git diff HEAD` is empty.
- Restore after the base run: `git checkout HEAD --
packages/runtime/src/domains/mcp-authoring.ts`, `git hash-object` = HEAD
blob `01bb9c7693`, `git diff HEAD` empty.
</details>
<details><summary>Local runs at 55d173f</summary>
- `pnpm --filter @objectstack/runtime exec vitest run --maxWorkers=2
src/domains/mcp-authoring-38965-list-drafts-bound.test.ts
src/domains/mcp-authoring.test.ts` (under the verify lock): `Test Files
2 passed (2)`, `Tests 80 passed (80)`, VERDICT command-exit 0.
- `pnpm --filter @objectstack/runtime typecheck`: VERDICT command-exit 0
(`tsc --noEmit`, then check:test-typecheck OK).
- The whole `@objectstack/runtime` suite did not finish within the
container's foreground limit and was stopped: NOT MEASURED locally. CI's
`Test Core` owns it.
- The baseline cost measurement (P = 10 / 50 / 100: 352 / 3,752 / 12,502
queries, 565 / 4,928 / 15,004 ms) came from a scratch test that was
never committed.
</details>
## Acceptance notes
- The per-verdict cost that makes the whole list quadratic is the strict
draft read: it folds its own publish verdict into `_diagnostics`,
collecting the package closure again without the door's shared map. That
cost lives in `metadata-protocol`, outside this card's fence, and is
filed as #39001. With this bound, about 3 verdicts fit the budget at P =
500 on sqld. That is honest but thin, and #39001 widens it.
- The cloud agent loop's turn deadline does not interrupt a running tool
(`maxExecutionTimeSec` 600 did not cut the hung call). That is cloud
`service-ai`'s, out of scope here.
- `skills/objectstack-mcp-build/SKILL.md` says `list_drafts` lists
"every pending draft in the environment, each with its verdict". That is
now true only up to the bound. This is a follow-up for the skill (not
edited here: `skills/**` is outside the fence).
- The spec prose of `DraftReviewItemSchema.verdictWithheld` reads "why
the person may not read a verdict". A not-judged row uses the same
field, as the claim prescribes; the closed schema accepts it, but the
prose is permission-only. Spec is not changed here.
- The module's header docblock still says each `list_drafts` entry
"carries its publish verdict". The bound is documented at the branch and
in the tool description. The header lies outside this card's file fence,
so it is not edited.
## Verification
Files to read first, by risk:
1. `packages/runtime/src/domains/mcp-authoring.ts`, the `case
'list_drafts'` branch: the order, the two guards, and the withheld row.
2.
`packages/runtime/src/domains/mcp-authoring-38965-list-drafts-bound.test.ts`.
3. `.changeset/38965-list-drafts-bound.md`.
Two-minute check: `pnpm --filter @objectstack/runtime exec vitest run
src/domains/mcp-authoring-38965-list-drafts-bound.test.ts` should report
3 passed.
<details><summary>Local gate subset at 55d173f</summary>
`node scripts/pm/dispatch-gates.mjs --commands` derived 68 families. 66
exited 0, among them `check:nul-bytes`, `check:engine-double-contract`,
`check:cross-package-test-inputs`, `check:doc-authoring`,
`check:issue-citations`, `check:durability-log-level`,
`check:test-source-alias`, `check-adr-0087-registration --base
origin/main` and `check-changeset-no-major --base origin/main`. Two
exited 3 with PREREQUISITE NOT MET and are NOT MEASURED locally, so CI
owns them: `check-plugin-teardown-shape.mjs --self-test` (its positive
control is pinned to a commit this shallow clone cannot reach) and
`check:dual-build-cjs-loads` (it reads dist output for packages this
worktree did not build). `--ran` reconciliation: `68 derived, 66 run, 2
NOT-MEASURED, 0 UNRUN`. `overlap-check.mjs` was clean before each push
(origin/main, and #39006, which shares `mcp-authoring.ts`: merges
cleanly).
</details>
---
_Generated by [Claude
Code](https://claude.ai/code/session_01WGufzeatqKfTRUzTJVa9jQ)_
<!-- signed: repo:objectstack@os-support-ai · os-support-ai ·
session_01WGufzeatqKfTRUzTJVa9jQ -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent ce0d0da commit 270d160
3 files changed
Lines changed: 251 additions & 4 deletions
File tree
- .changeset
- packages/runtime/src/domains
| 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 | + | |
| 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 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
197 | 197 | | |
198 | 198 | | |
199 | 199 | | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
200 | 226 | | |
201 | 227 | | |
202 | 228 | | |
| |||
329 | 355 | | |
330 | 356 | | |
331 | 357 | | |
332 | | - | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
333 | 362 | | |
334 | 363 | | |
335 | 364 | | |
| |||
899 | 928 | | |
900 | 929 | | |
901 | 930 | | |
| 931 | + | |
| 932 | + | |
902 | 933 | | |
903 | 934 | | |
904 | 935 | | |
| |||
913 | 944 | | |
914 | 945 | | |
915 | 946 | | |
916 | | - | |
917 | | - | |
918 | | - | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
919 | 1000 | | |
920 | 1001 | | |
921 | 1002 | | |
| |||
0 commit comments