Repository navigation
perf(api): Parse the conversation feed once per response - #3808
Conversation
readConversationFeed parsed the feed, then jsonResponse parsed it again against the same schema. Keep the parse at the response edge. The MCP SDK already checks searchConversations output against its own schema. Co-Authored-By: BYK <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Can we detect and prevent this everywhere using an oxlint rule? |
|
Mostly yes, with a narrower rule than "detect the double parse". It is not limited to this route: 14 other A rule cannot see the double parse directly. oxlint JS plugins have no type info and check one file at a time, so a rule can't tell that A per-file rule can enforce the policy instead: response schemas are parsed only at the response edge.
I'd ship it as one follow-up PR: the rule, its test, and the removal of the ~14 inner parses. This PR stays minimal next to the sibling route PRs. Want me to open it? |
`GET /api/conversations` responses can reach millions of chars, and no
telemetry shows which row fields fill them.
`serializeConversationFeed` (`api/conversations/feed-response.ts`)
replaces the plain `JSON.stringify` for the feed. It serializes
`activityPreview`, `annotations`, `participants` and `threads` on their
own and splices them into each row, so the response is still serialized
only once.
**Key order:** values do not change, but those four keys now come last
in each top-level row object. JSON clients that read by key are not
affected.
`jsonResponse` takes an optional serializer that returns the body and
extra span attributes. The default is unchanged.
New attributes on the `serialize` span of the feed response:
-
`app.api.response.field_chars.{activity_preview,annotations,participants,threads}`:
total chars of each field across the page's top-level rows. `threads`
includes the nested Threads with all their fields.
- `app.conversation_feed.rows`: top-level rows.
- `app.conversation_feed.threads`: nested Threads, counted recursively.
- `app.conversation_feed.waiting_rows`: waiting rows added from outside
the page. The feed does not mark them, so `readConversationFeed` reports
the count to the route through `onWaitingRows`.
`TELEMETRY.md` gets a Domains row for these attributes.
**Conflict risk:** #3808 also edits
`readConversationFeed` in `list.ts`, in lines next to these. Whichever
PR merges second needs `main` merged in.
<!-- junior-request-attribution:start -->
via @BYK.
<!-- junior-request-attribution:end -->
<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3A9657c284764ed5856a5b9710a83ad2c6 -->
--
[View
Session](https://junior-prod.sentry.dev/conversations/agent%3A9657c284764ed5856a5b9710a83ad2c6)
[[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3A9657c284764ed5856a5b9710a83ad2c6/?project=4510944073809921)
<!-- junior-session-footer:end -->
---------
Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: BYK <[email protected]>
Co-Authored-By: BYK <[email protected]> #3808 removed the feed parse, so list.ts no longer needs the override.
`jsonResponse` parses every REST response against its schema. Many
`api/` readers parsed the same value first, so each of those responses
was validated twice.
## Rule
`junior/no-response-schema-parse` (`tools/oxlint/junior/rules/`, with a
RuleTester test):
- It reports `.parse` / `.safeParse` / `.parseAsync` / `.safeParseAsync`
on a value import from `@/api/schema` or `@/api/schema/*`, or from a
relative path that resolves there.
- It is on for `packages/junior/src/api/**/*.ts`.
- `api/http.ts` does not trigger it, because it parses a schema
parameter, not an imported schema.
- Schemas from other modules, such as `acpInboundMessageSchema` or
`userSchema`, are not checked.
## Removed parses (16)
- `code/overview.ts` (overview and person), `code/repository.ts`,
`cache-warming/savings.ts`, `stats.ts`, `notifications/routes.ts`,
`people/code.ts`.
-
`conversations/{child-agents,plan,watches,pending-messages,event-list,detail}.ts`,
`locations/{detail,list}.ts`, `people/profile.ts`.
Each removed parse was followed by a `jsonResponse` parse with the same
or a wider schema. None of these schemas has a transform, pipe, default
or catch. The detail, event page, location detail and actor profile
schemas have only idempotent `trim()` checks, which `jsonResponse` still
applies, so the response does not change.
Other callers:
- **MCP `getConversation`** reads `readConversationDetail`.
- **MCP `readMcpPendingWork`** reads `readConversationPendingMessages`.
- Both tools declare an `outputSchema`, and the MCP SDK checks
`structuredContent` against it. They do not read annotations. Each
detail event is still parsed in `events.ts`.
The detail parse had its own `Validate conversation detail` span. That
span is gone with it.
Readers that had no declared return type now declare their report type.
After the change, `locations/detail.ts`, `locations/list.ts` and
`people/profile.ts` were only pass-throughs, so they are deleted and the
routes call `read*FromSql` directly.
## Edges kept behind a disable comment
- `conversations/events.ts`: stored read. It checks each stored event
payload, and MCP readers use the result with no other parse.
- `conversations/feed-brief.ts`: stored read of the Brief JSON.
- `mcp/tools/search-automations.ts`: request input. These are MCP tool
arguments, and the schema sets defaults.
## list.ts
`readConversationFeed` in `conversations/list.ts` is left as is, because
#3808 removes that parse. A file-level `.oxlintrc.json`
override turns the rule off for `list.ts`. Whichever of this PR and
#3808 merges second removes the override.
<!-- junior-request-attribution:start -->
via @BYK.
<!-- junior-request-attribution:end -->
<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3A9657c284764ed5856a5b9710a83ad2c6 -->
--
[View
Session](https://junior-prod.sentry.dev/conversations/agent%3A9657c284764ed5856a5b9710a83ad2c6)
[[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3A9657c284764ed5856a5b9710a83ad2c6/?project=4510944073809921)
<!-- junior-session-footer:end -->
---------
Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: BYK <[email protected]>
GET /api/conversationsparsed the feed twice againstconversationFeedSchema: once inreadConversationFeed, then again injsonResponse. On large feeds the second parse costs real time in the serialize span.This keeps the parse at the response edge, in
jsonResponse, and removes the one insidereadConversationFeed. The value inside the route comes from internal SQL code with theConversationFeedtype, so it is not an edge.The other production caller, the MCP
searchConversationstool, still validates its output. It maps rows throughmcpConversation, and the MCP SDK checksstructuredContentagainst the tool's strictmcpConversationSchema.The feed schemas have only idempotent
trim()checks on annotation fields, whichjsonResponsestill applies, so the response does not change.via @BYK.
--
View Session [Sentry]