perf(ai-harness, ai-harness-cli): cheaper plugin, log, media, view, client, and host-event paths, no idle store calls, and context prompts after the history #1823
feat/harness-integrations fromrefs/pull/1823/head·merged by AlemTuzlak View on GitHubThe harness now does less work per event, per turn, and per idle session. The hot paths in plugins, the log tail, media, the view, the client, and host events are cheaper, and an idle session makes no store calls. A plugin prompt that changes every turn no longer breaks the provider's prompt cache. This is PR 13 of the harness redesign stack for #1755. It targets #1822 (feat/harness-integrations), as part of the chain 12 → 13 → 14.
🎯 Changes#
- Plugins (D12).
ctx.collectkeeps a list per extension point. After setup it returns that same array, with no Proxy rebuilt on each access.- Permission patterns compile once. Instruction files are read async, and checked once per turn.
- Prompt cache (D11). A plugin prompt with
placement: 'context'goes after the history, so the cached prefix stays valid. Todos, goal status, and plan mode use it. A plugin that a thread removes adds no context prompt. - The log tail (B6). The shared log keeps its tail in a byte-capped
EventRing, with O(1) push and drop and a per-thread index. - Media (C2).
- Media streams from the store, and base64 parts are cached across turns in a byte-capped LRU.
- An open-ended range (
bytes=N-) sends at most 8 MB.
- The view and the CLI (C12, E10).
- The view redoes only the messages that changed, and the reducer folds usage, so it does not fetch again.
- Children load on demand with
view.loadAgent({ id }). - The CLI printer prints only what is new.
- The client (C13).
- An incremental SSE parser that takes CRLF and multi-line
data:. - One abort listener per stream, and a capped, jittered backoff.
- An
inputIdon each control input, with a fallback for plain HTTP. events()with nofromstarts at the snapshot cursor.
- An incremental SSE parser that takes CRLF and multi-line
- Joins (C1). A join on another host reads from the run start, not from the start of the log. A rebuilt operation streams from its first input.
- Log and events.
commonPrefixchecks identity first (B7), and the turn skips a second compare when nothing changed.MESSAGES_SNAPSHOTno longer goes into the log (B9).host.events()andGET host-eventsread a per-tenant host event bus. The bus filters before the queue and caps it (C4).
- Idle. An idle session makes no store calls, and a test pins it.
Docs: plugins (prompt placement), custom-ui (loadAgent), connect (the snapshot cursor), sessions (a slow host.events() reader stops), and lifecycle. The changesets are edited in place.
Decisions made while building:
placementdefaults to'system', so existing plugin prompts keep their place. A'context'prompt is resolved at every model call, so a tool that changes the todo list shows on the next call.- The base64 cache is keyed by media id. The bytes of an id never change, and one store serves one thread of one tenant.
- An open-ended range is capped only on HTTP. A programmatic
loadMediais still limited bymaxBytes. client.events()with nofromstarts at the snapshot cursor. Passfrom: '0'to read from the start.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with
pnpm run test:pr, or these tests do not apply to this pull request. - I fully understand the code in this pull request, including any code generated with AI assistance.
- Docs: I updated
docs/for this change, or this change is not user-facing. - Changeset: I added a changeset (
pnpm changeset), or this PR does not change a published package.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Testing#
Commands run:
- Builds of the E2E app's dependencies and the CLI.
test:typesofai,ai-harness,ai-mcp,ai-acp,ai-harness-cli,ai-code-mode,ai-skills,ai-sandbox, and the E2E app. All pass. vitest relatedon every changedai-harnesssource file, with--maxWorkers=2: 148 files, 1,939 tests, all pass. The CLIprinterandchild-viewtests pass too.test:oxlintofai-harness,test:knip,pnpm test:docs, andoxfmt --checkon every changed file.
Not run locally: E2E and coverage. CI runs them.
Manual test:
- Run
pnpm --dir packages/ai-harness exec vitest run tests/plugin-hotpaths.test.ts tests/ring.test.ts tests/idle-budget.test.ts tests/prompt-placement.test.ts. - Read
tests/prompt-placement.test.ts: a'context'prompt changes between two model calls, and the system prompts and the history before it stay byte-identical.
How this PR makes testing easy: the new tests pin each part: plugin-hotpaths, client-parse, ring, host-event-bus, idle-budget, and prompt-placement, plus new cases in the media, view, and printer tests. Each new test failed on the old code.
Risk / rollback#
Medium. The log tail, the view, the client parser, and the host event fan-out changed. The behavior stays the same, but the code paths are new. To undo, revert this PR.
Public API change#
Before
definePlugin({ name: 'acme/todos', setup: () => ({ prompts: [{ id: 'todos', text: () => todoText() }] }) })
for await (const event of client.events({ threadId })) {} // from the start
After
definePlugin({ name: 'acme/todos', setup: () => ({ prompts: [{ id: 'todos', text: () => todoText(), placement: 'context' }] }) })
for await (const event of client.events({ threadId })) {} // from the snapshot cursor; pass from: '0' for all
await view.loadAgent({ id })
Review fixes#
These fixes come from a stack review. #1824 and #1825 are already squash-merged into this branch. This PR is squash-merged into #1822 next.
- Context prompts (plan mode, goal, todos) are now the last system prompt, not a user message. They keep system authority. A change to them now misses the provider cache for the history, because Anthropic and Gemini have no system message inside the history.
- New plugin hook
onTurnStart(turn).projectInstructionsandworkspaceToolsuse it instead of adiscoverToolsthat returns[]. MESSAGES_SNAPSHOTgoes into the log again (core sends it only at an interrupt), so a rejoin from the log gets it.client.events()checks the snapshot shape (no unchecked cast), and the docs state its head-first default.- The view keeps the usage
source. The first host-event statuses do not count toward the queue cap.host.events()listens from the call. - The TUI example loads saved child agents. The
view.loadAgentpattern is documented.
Checks: the unit tests for the changed files, plus test:types and test:oxlint for ai-harness and ai-harness-cli. CI runs the rest, including E2E.
- 1
Important
Review skipped#
Auto reviews are disabled on base/target branches other than the default branch.
Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
-
- Configuration used: Repository: TanStack/ai/.coderabbit.yaml
- Review profile: CHILL
- Plan: Advanced
- Run ID:
09b333bc-68cf-46f4-9232-96e65edb04a9
You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
- 🔍 Trigger review
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment `@coderabbitai help` to get the list of available commands.-
View your CI Pipeline Execution ↗ for commit bc6c749dd8d27e27649eb4061e95933cff566be4
Command Status Duration Result nx run-many --targets=build --exclude=examples/...✅ Succeeded 48s View ↗
☁️ Nx Cloud last updated this comment at
2026-10-19 09:29:20UTC@tanstack/ai
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai@1823@tanstack/ai-acp
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-acp@1823@tanstack/ai-angular
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-angular@1823@tanstack/ai-anthropic
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-anthropic@1823@tanstack/ai-bedrock
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-bedrock@1823@tanstack/ai-byteplus
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-byteplus@1823@tanstack/ai-claude-code
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-claude-code@1823@tanstack/ai-client
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-client@1823@tanstack/ai-cloudflare
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-cloudflare@1823@tanstack/ai-code-mode
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-code-mode@1823@tanstack/ai-code-mode-snippets
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-code-mode-snippets@1823@tanstack/ai-codex
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-codex@1823@tanstack/ai-cohere
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-cohere@1823@tanstack/ai-compaction
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-compaction@1823@tanstack/ai-devtools-core
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-devtools-core@1823@tanstack/ai-durable-stream
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-durable-stream@1823@tanstack/ai-elevenlabs
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-elevenlabs@1823@tanstack/ai-event-client
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-event-client@1823@tanstack/ai-fal
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-fal@1823@tanstack/ai-gemini
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-gemini@1823@tanstack/ai-grok
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-grok@1823@tanstack/ai-grok-build
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-grok-build@1823@tanstack/ai-groq
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-groq@1823@tanstack/ai-harness
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-harness@1823@tanstack/ai-harness-cli
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-harness-cli@1823@tanstack/ai-isolate-cloudflare
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-cloudflare@1823@tanstack/ai-isolate-daytona
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-daytona@1823@tanstack/ai-isolate-e2b
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-e2b@1823@tanstack/ai-isolate-node
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-node@1823@tanstack/ai-isolate-quickjs
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-quickjs@1823@tanstack/ai-isolate-quickjs-bun
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-quickjs-bun@1823@tanstack/ai-llmgateway
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-llmgateway@1823@tanstack/ai-lovable
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-lovable@1823@tanstack/ai-mcp
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-mcp@1823@tanstack/ai-memory
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-memory@1823@tanstack/ai-mistral
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-mistral@1823@tanstack/ai-octane
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-octane@1823@tanstack/ai-ollama
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-ollama@1823@tanstack/ai-ollaya
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-ollaya@1823@tanstack/ai-openai
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-openai@1823@tanstack/ai-opencode
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-opencode@1823@tanstack/ai-openrouter
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-openrouter@1823@tanstack/ai-perplexity
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-perplexity@1823@tanstack/ai-persistence
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-persistence@1823@tanstack/ai-preact
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-preact@1823@tanstack/ai-react
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-react@1823@tanstack/ai-react-ui
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-react-ui@1823@tanstack/ai-reactor
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-reactor@1823@tanstack/ai-remix
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-remix@1823@tanstack/ai-sandbox
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox@1823@tanstack/ai-sandbox-blaxel
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-blaxel@1823@tanstack/ai-sandbox-boxd
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-boxd@1823@tanstack/ai-sandbox-cloudflare
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-cloudflare@1823@tanstack/ai-sandbox-daytona
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-daytona@1823@tanstack/ai-sandbox-docker
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-docker@1823@tanstack/ai-sandbox-e2b
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-e2b@1823@tanstack/ai-sandbox-local-process
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-local-process@1823@tanstack/ai-sandbox-railway
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-railway@1823@tanstack/ai-sandbox-sprites
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-sprites@1823@tanstack/ai-sandbox-upstash-box
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-upstash-box@1823@tanstack/ai-sandbox-vercel
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-vercel@1823@tanstack/ai-skills
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-skills@1823@tanstack/ai-solid
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-solid@1823@tanstack/ai-solid-ui
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-solid-ui@1823@tanstack/ai-svelte
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-svelte@1823@tanstack/ai-typesafe
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-typesafe@1823@tanstack/ai-utils
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-utils@1823@tanstack/ai-vercel-gateway
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vercel-gateway@1823@tanstack/ai-vertex
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vertex@1823@tanstack/ai-vue
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vue@1823@tanstack/ai-vue-ui
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vue-ui@1823@tanstack/ai-worldlabs
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-worldlabs@1823@tanstack/openai-base
npm i https://pkg.pr.new/TanStack/ai/@tanstack/openai-base@1823@tanstack/preact-ai-devtools
npm i https://pkg.pr.new/TanStack/ai/@tanstack/preact-ai-devtools@1823@tanstack/react-ai-devtools
npm i https://pkg.pr.new/TanStack/ai/@tanstack/react-ai-devtools@1823@tanstack/solid-ai-devtools
npm i https://pkg.pr.new/TanStack/ai/@tanstack/solid-ai-devtools@1823@tanstack/svelte-ai-devtools
npm i https://pkg.pr.new/TanStack/ai/@tanstack/svelte-ai-devtools@1823commit:
bc6c749Coverage#
✅ Coverage held across 30 compared package(s).
Each package is measured twice in this job — on this PR and on its merge-base with
main— and compared. A metric fails only when it is at or above 60% on the merge-base and under 60% on this PR. Other drops are listed but do not fail. Packages your PR doesn't affect are not measured and not listed.package statements branches functions lines ai-acp 82.69% (0.00) 71.04% (0.00) 78.63% (0.00) 84.72% (0.00) ok ai-claude-code 88.93% (0.00) 76.88% (0.00) 86.88% (0.00) 90.40% (0.00) ok ai-code-mode 93.04% (0.00) 86.07% (0.00) 89.55% (0.00) 93.76% (0.00) ok ai-code-mode-snippets 87.13% (0.00) 75.67% (0.00) 91.11% (0.00) 88.39% (0.00) ok ai-codex 94.55% (0.00) 85.10% (0.00) 89.28% (0.00) 95.56% (0.00) ok ai-durable-stream 86.70% (0.00) 82.25% (0.00) 92.30% (0.00) 88.72% (0.00) ok ai-grok-build 79.19% (0.00) 63.56% (0.00) 77.39% (0.00) 81.02% (0.00) ok ai-harness 93.30% (+0.02) 87.18% (+0.01) 93.34% (+0.04) 95.12% (+0.07) ok ai-harness-cli 92.01% (+0.11) 82.89% (+0.12) 88.00% (0.00) 93.02% (+0.08) ok ai-isolate-cloudflare 94.11% (0.00) 70.66% (0.00) 100.00% (0.00) 93.96% (0.00) ok ai-isolate-daytona 90.25% (0.00) 80.59% (0.00) 100.00% (0.00) 90.15% (0.00) ok ai-isolate-e2b 92.11% (0.00) 81.48% (0.00) 97.36% (0.00) 95.05% (0.00) ok ai-isolate-node 84.00% (0.00) 43.39% (0.00) 100.00% (0.00) 83.78% (0.00) ok ai-isolate-quickjs 89.07% (0.00) 76.41% (0.00) 90.90% (0.00) 90.30% (0.00) ok ai-isolate-quickjs-bun 7.76% (0.00) 8.08% (0.00) 5.00% (0.00) 8.33% (0.00) ok ai-mcp 96.36% (0.00) 91.24% (0.00) 96.67% (0.00) 98.28% (0.00) ok ai-opencode 69.04% (0.00) 60.82% (0.00) 65.26% (0.00) 70.87% (0.00) ok ai-sandbox 78.54% (+0.06) 83.04% (+0.04) 75.74% (+0.11) 79.09% (+0.04) ok ai-sandbox-blaxel 87.64% (0.00) 77.18% (0.00) 82.26% (0.00) 89.71% (0.00) ok ai-sandbox-boxd 95.86% (0.00) 87.12% (0.00) 95.31% (0.00) 98.17% (0.00) ok ai-sandbox-cloudflare 67.91% (0.00) 57.47% (0.00) 71.79% (0.00) 69.62% (0.00) ok ai-sandbox-daytona 82.70% (0.00) 81.89% (0.00) 67.14% (0.00) 85.18% (0.00) ok ai-sandbox-docker 80.15% (0.00) 71.49% (0.00) 77.38% (0.00) 82.13% (0.00) ok ai-sandbox-e2b 94.42% (0.00) 88.97% (0.00) 93.24% (0.00) 98.63% (0.00) ok ai-sandbox-local-process 69.43% (0.00) 54.01% (0.00) 77.27% (0.00) 70.27% (0.00) ok ai-sandbox-railway 94.75% (0.00) 92.68% (0.00) 90.90% (0.00) 97.59% (0.00) ok ai-sandbox-sprites 73.23% (0.00) 59.70% (0.00) 73.83% (0.00) 76.17% (0.00) ok ai-sandbox-upstash-box 94.82% (0.00) 91.52% (0.00) 91.04% (0.00) 97.76% (0.00) ok ai-sandbox-vercel 44.67% (0.00) 38.18% (0.00) 35.18% (0.00) 45.76% (0.00) ok ai-skills 83.98% (0.00) 68.19% (0.00) 88.51% (0.00) 86.02% (0.00) ok github-actions[bot](GitHub profile, opens in a new tab) assignedtombeckenham
github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer
AlemTuzlak merged this7ab3b63
AlemTuzlak closed this
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: 5fbb4eab8c7d3a24c938a9ed45ea8ca7d11b5337
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 69 packages
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR