Skip to content

Commit 0eb3feb

Browse files
refactor(agents): Read the parent edge for top-level checks (#4292)
Part of #4275. Stacked on #4278. `design/child-agents.md` lists a gap: some child-agent behavior branches on `kind` outside `kind-behavior.ts`. Several of those branches only ask whether a Conversation has a parent. This PR answers that from the parent edge instead of the kind. The `thread` kind stays. - `isTopLevelKind(kind)` call sites now use `isTopLevel(conversation)`, which reads `parentConversationId`. This covers pins, Space moves, delivery pause, work routing, Turn wake, web input, and Turn visibility. - `canMoveToSpace` takes the Conversation: top-level and not a Lead. - The store's kind check still uses `isTopLevelKind`, because it checks that kind and parent agree. - The `kind-behavior` baseline goes down in 5 files. - `design/child-agents.md`: the rule that `kind` is the only switch is replaced with the text David confirmed. Behavior does not change. Refs #4275 <!-- junior-request-attribution:start --> via @dcramer. <!-- junior-request-attribution:end --> <!-- junior-session-footer:start --> <!-- junior-conversation-id:agent%3A8d107731066590a2dd1a7ea144ad5fef --> -- [View Session](https://junior-prod.sentry.dev/conversations/agent%3A8d107731066590a2dd1a7ea144ad5fef) [[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3A8d107731066590a2dd1a7ea144ad5fef/?project=4510944073809921) <!-- junior-session-footer:end --> --------- Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com> Co-authored-by: David Cramer <[email protected]>
1 parent 7e1b81c commit 0eb3feb

10 files changed

Lines changed: 43 additions & 31 deletions

File tree

‎design/child-agents.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ relation adds: authority, kinds, limits, stops, and lifecycle.
2020

2121
## Rules
2222

23-
- A child agent is an ordinary Conversation. Its `kind`, `thread` or
24-
`task`, is the only switch for its behavior, and it never changes.
23+
- A child agent is an ordinary Conversation. Its behavior depends only on
24+
whether it is a `task` and on who sent the Turn. A child that is not a
25+
`task` behaves like any Conversation that has a parent. Its kind and its
26+
parent never change.
2527
- Each Turn of a child acts for exactly one Actor, with no more authority
2628
than the parent Turn that gave the work. `startAgent` and `messageAgent`
2729
pass the current Turn's Actor to the child, and the child's result

‎packages/junior/src/api/conversations/pin.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { User } from "@sentry/junior-plugin-api";
22
import { and, count, eq, isNotNull, sql } from "drizzle-orm";
33
import { getDb, getSqlExecutor } from "@/chat/db";
44
import { setSpanAttributes } from "@/chat/logging";
5-
import { conversationKindFromRow } from "@/chat/conversations/sql/conversation-row";
65
import { setArchivedAt } from "@/chat/conversations/sql/conversation-tree";
76
import type { JuniorDatabase } from "@/db/db";
87
import {
@@ -17,7 +16,6 @@ import type {
1716
ReorderPinnedConversationsResponse,
1817
} from "../schema/conversation-pin";
1918
import { readConversationAccessFromSql } from "./access";
20-
import { isTopLevelKind } from "@/chat/conversations/kind-behavior";
2119

2220
/**
2321
* Pin or unpin one conversation for the signed-in viewer.
@@ -107,7 +105,7 @@ async function setPin(
107105
if (!conversation) throwApiError(404, "Conversation not found.");
108106
// The viewer passed the access check above, so the root id is safe to name.
109107
// MCP output has no root field, and the caller needs it to act.
110-
if (!isTopLevelKind(conversationKindFromRow(conversation))) {
108+
if (conversation.parentConversationId) {
111109
throwApiError(
112110
409,
113111
`A child conversation cannot be pinned or unpinned. It uses its root's pin: ${conversation.rootConversationId ?? conversation.parentConversationId}.`,

‎packages/junior/src/api/conversations/space.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export async function setConversationSpaceForViewer(
4949
if (!conversation || !access?.isParticipant) {
5050
throwApiError(404, "Conversation not found.");
5151
}
52-
if (!canMoveToSpace(conversation.kind)) {
52+
if (!canMoveToSpace(conversation)) {
5353
throwApiError(
5454
409,
5555
"Only a top-level conversation moves. A child is in the Space of its root.",

‎packages/junior/src/chat/app/conversation-work.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { withAgentInvocationResultActors } from "@/chat/agent-invocations/result
2222
import { stopChildAgentsOf } from "@/chat/agent-invocations/stop";
2323
import { logException, withLogContext } from "@/chat/logging";
2424
import { resumeForPersonInput } from "@/chat/conversations/session-stop";
25-
import { isTopLevelKind } from "@/chat/conversations/kind-behavior";
25+
import { isTopLevel } from "@/chat/conversations/kind-behavior";
2626
import { createConversationTurnWorker } from "@/chat/task-execution/conversation-turn";
2727
import { resolveMailboxTurnWork } from "@/chat/task-execution/mailbox-turn";
2828
import {
@@ -210,7 +210,7 @@ export function createConversationWork(
210210
const conversation = await options.conversationStore.get({
211211
conversationId: context.conversationId,
212212
});
213-
const topLevel = isTopLevelKind(conversation?.kind ?? "root");
213+
const topLevel = !conversation || isTopLevel(conversation);
214214
// Bind the tree root once, so spans of child agents count by root.
215215
return await withLogContext(
216216
{

‎packages/junior/src/chat/conversations/delivery-pause.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ import { getSqlExecutor } from "@/chat/db";
44
import { nextDetailVersion } from "@/chat/conversations/sql/detail-version";
55
import type { Conversation } from "@/chat/conversations/store";
66
import { juniorConversations } from "@/db/schema";
7-
import { isTopLevelKind } from "@/chat/conversations/kind-behavior";
7+
import { isTopLevel } from "@/chat/conversations/kind-behavior";
88

99
/** True when the Conversation is a root that delivers to a Location. */
1010
export function canPauseConversationDelivery(
11-
conversation: Pick<Conversation, "kind" | "location">,
11+
conversation: Pick<Conversation, "parentConversationId" | "location">,
1212
): boolean {
13-
return (
14-
isTopLevelKind(conversation.kind) && conversation.location !== undefined
15-
);
13+
return isTopLevel(conversation) && conversation.location !== undefined;
1614
}
1715

1816
const rootConversation = (conversationId: string) =>

‎packages/junior/src/chat/conversations/kind-behavior.ts‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import type { Source } from "@sentry/junior-plugin-api";
22
import type { RunDispatch } from "@/chat/automation-result";
3-
import type { ConversationKind } from "@/chat/conversations/store";
3+
import type {
4+
Conversation,
5+
ConversationKind,
6+
} from "@/chat/conversations/store";
47
import type { PromptMode, TurnInputSender } from "@/chat/system-prompt-rules";
58
import type { StaticToolName } from "@/chat/tool-support/registry";
69

@@ -328,11 +331,13 @@ export function hasConversationMetadata(kind: ConversationKind): boolean {
328331
}
329332

330333
/**
331-
* Return whether a person can put a Conversation of this kind in a Space. A
332-
* child is in the Space of its root, and a Lead never moves.
334+
* Return whether a person can put a Conversation in a Space. A child is in
335+
* the Space of its root, and a Lead never moves.
333336
*/
334-
export function canMoveToSpace(kind: ConversationKind): boolean {
335-
return kind === "root" || kind === "automation";
337+
export function canMoveToSpace(
338+
conversation: Pick<Conversation, "kind" | "parentConversationId">,
339+
): boolean {
340+
return isTopLevel(conversation) && conversation.kind !== "lead";
336341
}
337342

338343
/**
@@ -343,6 +348,13 @@ export function messagesNeedSpaceMembership(kind: ConversationKind): boolean {
343348
return kind === "lead";
344349
}
345350

351+
/** Return whether a Conversation has no parent. */
352+
export function isTopLevel(
353+
conversation: Pick<Conversation, "parentConversationId">,
354+
): boolean {
355+
return !conversation.parentConversationId;
356+
}
357+
346358
/** Return whether a Conversation of this kind has no parent. */
347359
export function isTopLevelKind(kind: ConversationKind): boolean {
348360
return kind === "root" || kind === "automation" || kind === "lead";

‎packages/junior/src/chat/conversations/web-input.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import type { ConversationWorkQueue } from "@/chat/task-execution/queue";
3535
import { resolveConversationDestination } from "@/chat/conversations/destination";
3636
import { sameDestination } from "@/chat/destination";
3737
import { webMessageId } from "./web-message-id";
38-
import { isTopLevelKind } from "@/chat/conversations/kind-behavior";
38+
import { isTopLevel } from "@/chat/conversations/kind-behavior";
3939

4040
type EnqueueOptions = {
4141
attachmentStorage?: AttachmentStorage;
@@ -143,9 +143,12 @@ function storedActorFromWeb(actor: WebActor): StoredSlackActor {
143143
* Conversation: a dashboard Conversation, a Slack thread, or a Thread.
144144
*/
145145
export function acceptsWebMessages(
146-
conversation: Pick<Conversation, "conversationId" | "destination" | "kind">,
146+
conversation: Pick<
147+
Conversation,
148+
"conversationId" | "destination" | "kind" | "parentConversationId"
149+
>,
147150
): boolean {
148-
if (!isTopLevelKind(conversation.kind)) return conversation.kind === "thread";
151+
if (!isTopLevel(conversation)) return conversation.kind === "thread";
149152
const { conversationId, destination } = conversation;
150153
if (destination?.platform === "slack") return true;
151154
return (
@@ -285,7 +288,7 @@ export async function recordWebConversationActivity(args: {
285288
? { spaceId: args.rootSpaceId }
286289
: undefined),
287290
});
288-
return existing && !isTopLevelKind(existing.kind) ? undefined : destination;
291+
return existing && !isTopLevel(existing) ? undefined : destination;
289292
}
290293

291294
/** Create a Conversation from web input and enqueue its first Message. */

‎packages/junior/src/chat/task-execution/conversation-turn.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { blockRunAutomation } from "@/chat/automations/block-run";
33
import {
44
isPersonInput,
5-
isTopLevelKind,
5+
isTopLevel,
66
turnBehavior,
77
} from "@/chat/conversations/kind-behavior";
88
import {
@@ -240,7 +240,9 @@ export function createConversationTurnWorker(
240240
const kind = storedConversation?.kind ?? "root";
241241
// The parent's task can carry the root's private content, so a child
242242
// with missing visibility runs as private.
243-
const visibility = isTopLevelKind(kind)
243+
const visibility = isTopLevel({
244+
parentConversationId: storedConversation?.parentConversationId,
245+
})
244246
? storedConversation?.visibility
245247
: (storedConversation?.visibility ?? "private");
246248
const mailboxDestination =

‎packages/junior/src/chat/task-execution/turn-wake.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
requestConversationWork,
3131
} from "@/chat/task-execution/store";
3232
import { getVercelConversationWorkQueue } from "@/chat/task-execution/vercel-queue";
33-
import { isTopLevelKind } from "@/chat/conversations/kind-behavior";
33+
import { isTopLevel } from "@/chat/conversations/kind-behavior";
3434

3535
export interface PausedTurnRequest {
3636
conversationId: string;
@@ -153,7 +153,7 @@ export async function wakePausedTurn(
153153
conversationId: request.conversationId,
154154
// A child's work keeps no Destination. Its workers read the Conversation.
155155
destination:
156-
conversation && !isTopLevelKind(conversation.kind)
156+
conversation && !isTopLevel(conversation)
157157
? undefined
158158
: request.destination,
159159
nowMs,

‎scripts/test-architecture-baseline.json‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,30 +183,27 @@
183183
"packages/junior/src/chat/agent/index.ts": 8,
184184
"packages/junior/src/chat/agent/tools.ts": 2,
185185
"packages/junior/src/chat/agent/types.ts": 5,
186-
"packages/junior/src/chat/app/conversation-work.ts": 1,
187186
"packages/junior/src/chat/automation-result.ts": 1,
188187
"packages/junior/src/chat/automations/tool-access.ts": 1,
189188
"packages/junior/src/chat/cache-warming/backfill.ts": 1,
190-
"packages/junior/src/chat/conversations/delivery-pause.ts": 1,
191189
"packages/junior/src/chat/conversations/fork.ts": 1,
192190
"packages/junior/src/chat/conversations/lineage.ts": 1,
193191
"packages/junior/src/chat/conversations/sql/conversation-row.ts": 1,
194192
"packages/junior/src/chat/conversations/sql/conversation-tree.ts": 2,
195193
"packages/junior/src/chat/conversations/sql/purge.ts": 2,
196194
"packages/junior/src/chat/conversations/sql/store.ts": 9,
197-
"packages/junior/src/chat/conversations/web-input.ts": 3,
195+
"packages/junior/src/chat/conversations/web-input.ts": 1,
198196
"packages/junior/src/chat/ingress/slack-conversation-unfurl.ts": 1,
199197
"packages/junior/src/chat/plugins/agent-hooks.ts": 2,
200198
"packages/junior/src/chat/prompt.ts": 2,
201199
"packages/junior/src/chat/services/conversation-title.ts": 1,
202200
"packages/junior/src/chat/slack/automated-turn-limit-notice.ts": 1,
203201
"packages/junior/src/chat/slack/continue-in-slack.ts": 1,
204202
"packages/junior/src/chat/task-execution/attempt-selection.ts": 1,
205-
"packages/junior/src/chat/task-execution/conversation-turn.ts": 2,
203+
"packages/junior/src/chat/task-execution/conversation-turn.ts": 1,
206204
"packages/junior/src/chat/task-execution/mailbox-turn.ts": 1,
207205
"packages/junior/src/chat/task-execution/slack-work.ts": 3,
208206
"packages/junior/src/chat/task-execution/state.ts": 2,
209-
"packages/junior/src/chat/task-execution/turn-wake.ts": 1,
210207
"packages/junior/src/chat/task-execution/web-steering.ts": 1,
211208
"packages/junior/src/chat/task-execution/worker.ts": 1,
212209
"packages/junior/src/chat/tools/runtime/archive-agent.ts": 1

0 commit comments

Comments
 (0)