Repository navigation
ref(conversations): Read Conversation lineage with one query - #2831
Conversation
Co-Authored-By: David Cramer <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
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:
readChildDepthAndRootLocationgives 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, socanSpawnfails closed.findRootConversationstill 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_idFK has been in place since0000_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. |
There was a problem hiding this comment.
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.
|
12 screenshot changes — 12 changed · 0 added · 0 removed Review screenshots in Frameshift Show 6 more changed screenshots |
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
getper hop. This PR addsreadConversationLineageinchat/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
includescheck in the agent tree. It also stops the two unbounded walkers from hanging on a cycle. A chain that stops early still has aparentConversationIdon 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_idforeign key means this cannot happen.via @dcramer.
--
View Session [Sentry]