Skip to content

perf(api): Parse the conversation feed once per response - #3808

Merged
BYK merged 3 commits into
mainfrom
fix/conversation-feed-single-parse
Nov 19, 2026
Merged

BYK merged 3 commits into
mainfrom
fix/conversation-feed-single-parse

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

GET /api/conversations parsed the feed twice against conversationFeedSchema: once in readConversationFeed, then again in jsonResponse. 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 inside readConversationFeed. The value inside the route comes from internal SQL code with the ConversationFeed type, so it is not an edge.

The other production caller, the MCP searchConversations tool, still validates its output. It maps rows through mcpConversation, and the MCP SDK checks structuredContent against the tool's strict mcpConversationSchema.

The feed schemas have only idempotent trim() checks on annotation fields, which jsonResponse still applies, so the response does not change.

via @BYK.

--

View Session [Sentry]

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]>
@vercel

vercel Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
junior-docs Ready Ready Preview Nov 19, 2026 6:51pm UTC

Request Review

@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 19, 2026
@BYK

BYK commented Nov 19, 2026

Copy link
Copy Markdown
Member

Can we detect and prevent this everywhere using an oxlint rule?

@BYK
BYK enabled auto-merge (squash) November 19, 2026 17:52
@sentry-junior

sentry-junior Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Mostly yes, with a narrower rule than "detect the double parse". It is not limited to this route: 14 other api/ readers return xReportSchema.parse(...), and a route then passes the result to jsonResponse with the same schema. Examples: readConversationEventPage (via event-list.ts), child-agents.ts, plan.ts, watches.ts, pending-messages.ts, code/overview.ts, locations/*, people/*, stats.ts.

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 readX(), imported from another module, already returns a parsed value.

A per-file rule can enforce the policy instead: response schemas are parsed only at the response edge.

  • junior/no-response-schema-parse: in packages/junior/src/api/**, flag .parse() / .safeParse() on a schema imported from api/schema/*, outside api/http.ts.
  • Real edges keep their parse with a disable comment that states the edge. These are request bodies and query params (already parsed via validateRequest), durable or stored reads, and plugin input. Schemas like acpInboundMessageSchema or userSchema don't come from api/schema/, so the rule doesn't fire on them.
  • MCP callers stay covered, because the SDK validates structuredContent against each tool's outputSchema.

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?

BYK added a commit that referenced this pull request Nov 19, 2026
`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]>
@BYK
BYK merged commit d7c1242 into main Nov 19, 2026
52 checks passed
@BYK
BYK deleted the fix/conversation-feed-single-parse branch November 19, 2026 19:05
sentry-junior Bot added a commit that referenced this pull request Nov 19, 2026
Co-Authored-By: BYK <[email protected]>

#3808 removed the feed parse, so list.ts no longer needs the override.
BYK added a commit that referenced this pull request Nov 19, 2026
`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]>

This branch was successfully deployed

1 active deployment
Preview – junior-docs — 6d5a320a Deployed Nov 19, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant