Skip to content

ref(conversations): Read Conversation lineage with one query - #2831

Merged
dcramer merged 4 commits into
mainfrom
ref/conversation-lineage
Nov 7, 2026
Merged

dcramer merged 4 commits into
mainfrom
ref/conversation-lineage

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Prep for #2792. No behavior change.

The follow-up PRs for #2792 ask more questions about a Conversation's parent chain (root Location for a Thread Turn, nearest Thread-or-root for Guardian authority, deepest linked Conversation for review desk delivery). Today four places walk the chain on their own, one get per hop. This PR adds readConversationLineage in chat/conversations/lineage.ts. It reads the chain in one recursive query, root first, and the four walkers now use it:

  • readRunConversationVisibility (chat/agent/index.ts): nearest visibility.
  • readChildDepthAndRootLocation (chat/agent-invocations/work.ts): depth and root Location.
  • findRootConversation (chat/agent-invocations/root-conversation.ts): only the walk changed. The 32-link bound stays there.
  • readConversationAgentTree (api/conversations/agent-tree.ts): the 16-entry bound stays there.

The query keeps a path of visited ids and stops at a repeated id, so a bad parent link cannot loop. This replaces the includes check in the agent tree. It also stops the two unbounded walkers from hanging on a cycle. A chain that stops early still has a parentConversationId on its first entry, so each caller keeps its old handling for broken chains.

One edge case can differ: when a parent row is missing, the old agent tree showed the missing id as a "Subagent" node, and the new one leaves it out. The parent_conversation_id foreign key means this cannot happen.

via @dcramer.

--

View Session [Sentry]

@vercel

vercel Bot commented Nov 6, 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 7, 2026 12:11am UTC

Request Review

@blacksmith-sh

This comment has been minimized.

@sentry-junior sentry-junior Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. I found no behavior regressions in the four callers.

I checked each caller against the old walk for these cases: a missing Conversation, a root, a normal chain, a chain whose parent is missing, a cycle, and the bounds. Results:

  • readChildDepthAndRootLocation gives the same depth and Location in each case, including the extra link it counts when a chain is broken. A cycle now ends with a larger depth and no Location, so canSpawn fails closed.
  • findRootConversation still allows exactly 32 parent links. A broken chain and a cycle both still return nothing.
  • The agent tree keeps the 16-entry window, the root-title rule, and its cycle handling. The only difference is the missing-parent node. The parent_conversation_id FK has been in place since 0000_initial, so I agree that data cannot reach it.
  • Visibility still returns the nearest one. Child rows normally have no Destination, so reading the whole chain costs only a few PK lookups.

I seeded 50k roots, a 40-link chain, and a 3-node cycle on pg17, then ran EXPLAIN ANALYZE. Every step is a PK index scan, and the 40-link chain reads in about 0.5 ms. The cycle returns [y, z, x], and y still has a parentConversationId. The touched integration and component suites pass on Postgres and PGlite. pnpm typecheck and pnpm lint also pass.

I agree with leaving event-access.ts resolveRootConversationId and turn-session-routing.ts alone. The first is an access boundary that fails closed by returning the Conversation's own id, so its semantics differ, and changing it should come with access tests. The second needs full Conversation records and is already marked for deletion.

The new Location-decode throw (on every entry, not just the root) seems low risk. Child rows are written without a Location, and the detail request already fully decodes the root through store.get.

One thing for the follow-ups, not this PR: see the inline note about root_conversation_id.

*
* The chain stops at a parent that is missing or that is already in the
* chain. Then the first entry still has a `parentConversationId`, so callers
* can tell a broken chain from one that ends at a root.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For PR 4 (Guardian authority) and PR 5 (review desk delivery), this becomes authority- and destination-bearing. It follows parent_conversation_id only. readConversationAccessFromSql treats a root as valid only when the stored root_conversation_id agrees with it (hasValidRoot). Those two can disagree on bad data. When those PRs land, consider also selecting root_conversation_id here and treating a mismatch like a broken chain. Then authority, delivery, and access all fail closed on the same rows. Adding kind later is one more column on the outer select, so the shape otherwise looks right: root first, with the Conversation itself last.

@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

12 screenshot changes — 12 changed · 0 added · 0 removed

Review screenshots in Frameshift

Conversation Attachment · Desktop
Conversation Attachment · Desktop
Changed
Conversation Attachment · Mobile
Conversation Attachment · Mobile
Changed
Conversation Attachment Modal · Desktop
Conversation Attachment Modal · Desktop
Changed
Conversation Attachment Zoom · Desktop
Conversation Attachment Zoom · Desktop
Changed
Conversation Captured Memories Popover · Desktop
Conversation Captured Memories Popover · Desktop
Changed
Conversation Captured Memories Popover · Mobile
Conversation Captured Memories Popover · Mobile
Changed
Show 6 more changed screenshots
Conversation Catch Up · Mobile
Conversation Catch Up · Mobile
Changed
Conversation Details Watched Linked Work · Desktop
Conversation Details Watched Linked Work · Desktop
Changed
Conversation Details Watched Linked Work · Mobile
Conversation Details Watched Linked Work · Mobile
Changed
Conversation Long Transcript Touch · Mobile
Conversation Long Transcript Touch · Mobile
Changed
Conversation Share Details · Desktop
Conversation Share Details · Desktop
Changed
Settings Mcp · Desktop
Settings Mcp · Desktop
Changed

@dcramer
dcramer merged commit bd48cc4 into main Nov 7, 2026
44 checks passed
@dcramer
dcramer deleted the ref/conversation-lineage branch November 7, 2026 00:22

This branch was successfully deployed

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

Labels

risk: high PR risk score: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant