Skip to content

test(mcp): drive the worker CPU-budget case from a stepping thread clock - #34853

Merged
objectstack-fleet[bot] merged 2 commits into
mainfrom
claude/issue-34758-mcp-worker-cpu-budget
Nov 17, 2026
Merged

objectstack-fleet[bot] merged 2 commits into
mainfrom
claude/issue-34758-mcp-worker-cpu-budget

Conversation

@objectstack-fleet

Copy link
Copy Markdown
Contributor

Fixes #34758
Clause-②: no
Metadata-protocol: none

维护者速读

  • 改了什么:只改一个测试用例(packages/mcp/src/mcp-file-tools.test.ts 里 worker 的 CPU 预算用例),让它用"每读一次前进固定毫秒"的假线程时钟,不再和真实 CPU 赛跑;没有改任何会发布的代码。
  • 为什么改:这个用例在不相关的 PR 上随机变红。实测红的原因不是 CPU 预算本身,而是 30 ms 预算带来的 150 ms 真实墙钟上限:机器一忙,worker 连第 1 页都没读完就被掐断,答出"after page 0 of 120"且没有文字。
  • 风险与回滚:只涉及测试文件,生产行为逐字节不变;回滚即 revert 本 PR,用例回到原先会随负载变红的状态。
  • 先看:packages/mcp/src/mcp-file-tools.test.ts 的 steppingThreadCpuWorker 辅助函数(注入点),以及紧随其后的用例里 3000 ms 预算与步长的取值说明。
  • 两分钟验证:pnpm --filter @objectstack/mcp exec vitest run --maxWorkers=2 src/mcp-file-tools.test.ts -t 'out of CPU budget on the worker',预期 1 passed,且无论机器多忙,PDF 恒停在第 3 页、Word 恒停在 12800 段。
  • 需要你做的:无,席位落地。

What changes

The worker CPU-budget case in packages/mcp/src/mcp-file-tools.test.ts raced real time. It is now deterministic at the root, by a test-only seam that crosses the thread boundary, with no shipped file touched.

  • What actually failed (measured). CI's red (content: "none" at the case's first assertion) reproduces locally under synthetic load, and an instrumented run printed the answer: Reading stopped at the CPU-time budget for one file (30 ms) after page 0 of 120, so the text is partial. That is the pool's wall-clock ceiling (5 budgets of real time = 150 ms) cutting the read before page 1, not the CPU budget: a read stopped by the budget always carries page 1. So making the CPU clock controllable alone, at 30 ms, would leave the failure in place.
  • The seam. steppingThreadCpuWorker(stepMs) starts the source worker with one more --import: a data: URL preload that replaces process.threadCpuUsage inside the worker thread with a clock that advances stepMs on every read. It is the worker's twin of the in-process steppingCpuClock, which spies the request thread's process.cpuUsage and cannot reach a worker. file-extraction-worker.ts is unchanged and reads the clock it always reads. The preload is set per case through the existing configureExtractionWorkerEntry, which restarts the pool, and afterEach restores the built entry.
  • The case now pins exact stop points, not a 1..119 range:
    • PDF: budget 3000 ms, step 1000 ms. The budget is spent at the third checkpoint, so the answer is pages 1-3 with the exact note (3000 ms) after page 3 of 120.
    • Word: the same budget, step 15 ms. The budget is spent at the 200th checkpoint (every 64 paragraphs), so the read stops after 12800 paragraphs. That is past the 100,000-character cap, so the length-cap pin is real now. Before, it was a no-op whenever the count was small.
  • Why 3000 ms and not 30. The budget value no longer races anything: the stepping clock decides where the read stops. The value sets the real-time ceiling (15 s), far beyond any runner's stall. This does not widen the budget to get green. Truncation is pinned exactly, and the ablation below shows the case goes red without the clock.
  • The warm read is gone. A 1-page PDF never reaches a budget checkpoint, and worker start is outside the ceiling, so with a stepping clock it had nothing left to protect.

No changeset: the only changed file is a *.test.ts. It is not a tsup entry and is excluded from the build tsconfig, and @objectstack/mcp ships files: ["dist", "README.md", "CHANGELOG.md"]. skip-changeset is declared on create.

Tests

Package suite and typecheck (head a5821d8)
  • pnpm --filter @objectstack/mcp test (under the verify lock): Test Files 68 passed (68), Tests 1237 passed (1237), VERDICT command-exit 0.
  • pnpm --filter @objectstack/mcp typecheck: exit 0, check:test-typecheck: OK — ... 6 file(s) / 53 error(s) / 8 pinned signature(s) held (the ledger is unchanged).
Stress proof: 32 busy loops on a 4-core box

The same harness ran both legs: 32 node -e 'for(;;){}' loops, then the case alone (-t 'out of CPU budget on the worker') N times.

  • origin/main (dfbc316), original case: pass=1 fail=5 of 6. Every failure was the CI shape: content: "none", where content: "text" was expected.
  • This branch (53d7c7f): pass=12 fail=0 of 12, each run Tests 1 passed | 46 skipped.
  • With 8 busy loops the original case passed 8/8, so CI's red needs heavier contention than a 2x oversubscription.
Ablation: remove the seam wiring, then restore it

node scripts/ablation-replace.mjs replaced the helper's execArgv line with one that drops the --import of the clock preload.

  • Mutation landed: anchor 1 -> 0, blob 58c2867c7929 -> 98321a40db56.
  • Case result: red. Tests 1 failed. The expected note: ...after page 3 of 120... and truncated: true were missing: on the real clock, a 3000 ms budget reads all 120 pages.
  • Restored: blob == HEAD (58c2867c7929) and git diff HEAD is empty.
  • No dist/ is involved: vitest runs the test from source, and the worker runs file-extraction-worker.ts from source through tsx.

Acceptance notes

  • Finding (reported to the seat, not fixed here). When the ceiling cuts a worker before its first page, read_file answers content: "none". Its reason is the budget sentence, which says "after page 0 of N, so the text is partial", but no text comes back. See stoppedExtractionAnswer, then pdfAnswer([], ...), in file-content.ts. Measured through read_file in this suite, under load.
  • Sibling worker cases, observed only. These still use real wall-clock time: the ceiling case (40 ms budget, so a 200 ms ceiling before page 1 must arrive), the request-thread tick count, and the default-budget full reads. None has failed in CI that I know of, and none was stress-run here. They are noted only, with no carrier.
  • Production budget read unchanged: git diff origin/main -- packages/mcp/src/file-extraction-worker.ts packages/mcp/src/file-content.ts packages/mcp/src/file-extraction-pool.ts is empty. The CONTROL is the rest of the worker describe block, which runs on the real thread clock.

Verification

Local gate subset (derived by dispatch-gates --commands at a5821d8)
  • 50 derived; 49 ran locally with exit 0 (exit captured before any pipe): node scripts/check-closing-keyword-parity.mjs, node scripts/check-closing-keyword-parity.mjs --self-test, node scripts/check-comment-mask-adoption.mjs, node scripts/check-comment-mask-adoption.mjs --self-test, node scripts/check-comment-mask-corpus.mjs, node scripts/check-dts-emitted.mjs --self-test, node scripts/check-issue-citations.mjs, node scripts/check-plugin-teardown-shape.mjs, node scripts/check-plugin-teardown-shape.mjs --self-test, node scripts/ci/assert-turbo-remote-cache.mjs --self-test, node scripts/ci/plan-test-core-shards.mjs --self-test, node scripts/ci/prune-turbo-cache.mjs --self-test, node scripts/docs-audit/check-affected-docs.mjs, node scripts/docs-audit/check-drift-comment.mjs, pnpm --filter @objectstack/spec run check:duration-unit-keys, pnpm check:adr-anchors, pnpm check:ci-filter-parity, pnpm check:closed-ceremony-text, pnpm check:cross-package-test-inputs, pnpm check:doc-authoring, pnpm check:driver-memory-census, pnpm check:dts-closure, pnpm check:engine-double-contract, pnpm check:gitlink-declared, pnpm check:issue-citations, pnpm check:keyed-text-bounds, pnpm check:lean-entry-closure, pnpm check:logger-receiver-detach, pnpm check:nul-bytes, pnpm check:objectql-double-limit, pnpm check:org-identifier, pnpm check:page-declaration-shape, pnpm check:platform-object-tenancy-census, pnpm check:published-files, pnpm check:query-options-erasure, pnpm check:refd-timer-probe, pnpm check:registry-log-declared, pnpm check:rest-log-spy-declared, pnpm check:slot-lookup, pnpm check:sourcemap-no-sources-content, pnpm check:system-context-census, pnpm check:test-source-alias, pnpm check:tier-file-adoption, pnpm check:type-check-coverage, pnpm check:type-check-debt, pnpm check:undeclared-dep-imports, pnpm check:vouch-for-reference-callers, pnpm check:watch-hint-literal, pnpm check:where-matcher.
  • pnpm check:dual-build-cjs-loads: NOT MEASURED. It exited 3 with PREREQUISITE NOT MET — this gate reads built output, and some package has no dist/ (78 packages unbuilt in this worktree). This is a declared narrowing: the gate reads built output of every package, and this diff changes no built file. CI's Lint & Repo Gates runs it.
  • dispatch-gates --ran reconciliation: 50 derived, 49 run, 0 NOT-MEASURED, 1 UNRUN (the one above).
  • node scripts/pm/overlap-check.mjs: VERDICT clean exit 0 (merges cleanly into origin/main e9f65a3).
  • Every family CI runs is read from this PR's check-runs; the seat reads CI convergence.

Generated by Claude Code

The worker case raced real time: a 30 ms budget makes the pool's wall-clock
ceiling 150 ms, and a loaded runner's worker was cut there before page 1,
answering "after page 0 of 120" with no text. The case now starts its worker
with a data-URL preload that replaces process.threadCpuUsage in the worker
thread with a stepping clock (the worker twin of steppingCpuClock), so the
read stops at an exact checkpoint: page 3 of 120, and 12,800 Word paragraphs
(past the length cap, which still cuts the text). The 3000 ms budget keeps the
real-time ceiling (15 s) out of reach. No shipped file changes.

Claude-Session: https://claude.ai/code/session_012MmDsS4GoHCgYwA2uv3n3C
Co-authored-by: Claude <noreply@anthropic.com>
@objectstack-fleet objectstack-fleet Bot added the skip-changeset PR has no user-facing published change; bypasses the changeset gate label Nov 17, 2026
@github-actions

Copy link
Copy Markdown
Contributor

📓 Docs Drift Check

Nothing in this diff resolved to a documentable surface (no symbol, route or SDK anchor derived from 0 changed package(s)), so this run has no opinion about the docs.

What this run could not see

Coarse fallback — 0 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 7752cac7b002c6beb7b32dcc4ad2db450884151a → packageMentionDocs.

@github-actions github-actions Bot added the tests label Nov 17, 2026
@objectstack-fleet

Copy link
Copy Markdown
Contributor Author

repo:objectstack#4 · os-lizheng · session_012MmDsS4GoHCgYwA2uv3n3C

席位核对 · L1 ACCEPT · Head-sha: a5821d8 · 2026-11-17T14:00Z

Card #34758, claim 6948552137, dev report 6950149326. The seat read the diff itself. Self-review of record: one test file, +46 −17, Clause-②: no, no contract face.

Item Reading
Shape Draft, base main, first line Fixes #34758. Clause-②: no and Metadata-protocol: none are line-leading. One file: packages/mcp/src/mcp-file-tools.test.ts. No shipped file changes, so skip-changeset is right.
Premise measured The card's mechanism was partly falsified, with evidence in the PR body. The red was the pool's wall-clock ceiling (5 budgets of real time, 150 ms at a 30 ms budget) cutting the read before page 1, not the CPU-time race. An instrumented run under load printed the "after page 0 of 120" answer.
Fix A --import data-URL preload replaces process.threadCpuUsage inside the worker thread with a stepping clock (the worker twin of steppingCpuClock). The worker's code is unchanged.
Assertions Stricter than before. The PDF pins the exact note (3000 ms) after page 3 of 120 and the text of pages 1–3 (it was a 1..119 range). Word pins after 12800 paragraphs, and the case now proves 12,800 paragraphs exceed the 100,000-character cap, so the cap pin bites. The ablation (drop the preload) turns it red: the real clock reads all 120 pages. Stress: 12 of 12 green with 32 busy loops, against 1 of 6 on main.
Budget 30 → 3000 (open question) Seat ruling: accepted. The card's fence ("Never widen the budget or skip the case to get green", triage 6944733782; filer: "a looser assertion that stops pinning the truncation") guards against loosening the pin. With the stepping clock, the budget value no longer races anything: it only sets the real-time ceiling (15 s). Truncation is pinned more tightly than before. This follows the L1 rule that a criterion overturned by measurement and replaced with a stronger invariant is accepted when the PR body carries the measurement, and this one does. Route B (fake the request thread's timers, or a seam in shipped pool code) adds risk for the letter of the fence only.
Gates Local: 49 of 50 derived commands exit 0. check:dual-build-cjs-loads is NOT MEASURED (it needs every package's dist) and is CI-owned. CI on a5821d8 is read again at landing.
Out of scope The ceiling cut before page 1 answers content: none with a note claiming partial text: filed #34855. The sibling worker cases still on real wall time are noted, with no carrier (no CI failure known).
Landing Not governed (one packages/mcp test file). The seat lands it once every check on this head is green.

@objectstack-fleet

Copy link
Copy Markdown
Contributor Author

repo:objectstack#4 · os-lizheng · session_012MmDsS4GoHCgYwA2uv3n3C

Landing · Head-sha: a5821d8 · 2026-11-17T14:20Z

Probe Reading
Check-runs on a5821d8 success 37 · skipped 21 · in progress 0 · failure 0. Lint & Repo Gates success, TypeScript Type Check success, Test Core 10 of 10 shards success (the changed case runs in one of them). The skips are path- or label-gated (UI and docs families, Check Changeset under skip-changeset, merge-group-only queue verdicts, opt-in harnesses).
Mergeable · governed mergeable_state clean. check-governed-merges.mjs --pr 34853: not governed (one packages/mcp test file).

State: ready + auto-merge now (L1 ACCEPT 6950219903). #34758 closes on merge.

@objectstack-fleet
objectstack-fleet Bot marked this pull request as ready for review November 17, 2026 14:21
@objectstack-fleet
objectstack-fleet Bot added this pull request to the merge queue Nov 17, 2026
Merged via the queue into main with commit 73609d0 Nov 17, 2026
59 checks passed
@objectstack-fleet
objectstack-fleet Bot deleted the claude/issue-34758-mcp-worker-cpu-budget branch November 17, 2026 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/s skip-changeset PR has no user-facing published change; bypasses the changeset gate tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mcp: the worker CPU-budget case in mcp-file-tools.test.ts depends on runner load and fails on unrelated PRs

2 participants