Skip to content

Commit 158cd33

Browse files
fix(spaces): Give the feed's nested Threads their tree state
The sidebar orders a root's Threads with attentionOrder, but nested feed Threads had no tree state, so a Thread that waits on the viewer sorted under Done and could hide past the collapsed cap. The Thread read now reads each Thread's tree state with its first messages, so the feed takes one more SQL round. Co-Authored-By: David Cramer <[email protected]>
1 parent 76cfd71 commit 158cd33

5 files changed

Lines changed: 64 additions & 12 deletions

File tree

‎packages/junior-dashboard/src/client/conversations/ConversationStateIcon.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export type ConversationDisplayState =
1414
/**
1515
* Pick one state for a Conversation row. When more than one applies:
1616
* `needs_you` > `working` > `watching` > `pinned`. A `done` tree shows
17-
* nothing. A row without a tree state, such as a Thread row, works while
17+
* nothing. A row without a tree state works while
1818
* its own Turn is queued or running.
1919
*/
2020
export function conversationDisplayState(

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,7 @@ export async function readConversationFeedFromSql(
717717
conversations
718718
.filter((conversation) => conversation.archivedAtMs === undefined)
719719
.map(({ conversationId }) => conversationId),
720+
options.viewer?.id,
720721
),
721722
readSpaceRefs(
722723
db,

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type { ConversationAccess } from "./access";
1919
import { readFirstHumanMessageTexts } from "./first-message";
2020
import { conversationSummaryFromStoredConversation } from "./projection";
2121
import { conversationFromRecordRow, selectConversationRecords } from "./record";
22+
import { readConversationTreeStates } from "./tree-state";
2223

2324
/**
2425
* Find where a Thread nests: its nearest ancestor that is a listed row or a
@@ -50,13 +51,14 @@ type FeedThreadRecords = Awaited<ReturnType<typeof readFeedThreadRecords>>;
5051

5152
/**
5253
* Read the stored Threads under the listed rows for the feed, with their
53-
* read-time titles. It needs no access, so it can start with the other
54-
* feed reads. The caller passes only unarchived roots, so a root archive
54+
* read-time titles and each Thread's tree state for the viewer. It needs
55+
* no access, so it can start with the other feed reads. The caller passes only unarchived roots, so a root archive
5556
* hides its tree with no writes to the children.
5657
*/
5758
export async function readFeedThreadRecords(
5859
db: JuniorDatabase,
5960
rootIds: string[],
61+
viewerUserId: string | undefined,
6062
) {
6163
if (rootIds.length === 0) return undefined;
6264
const rows = await selectConversationRecords(db)
@@ -86,11 +88,23 @@ export async function readFeedThreadRecords(
8688
conversation: conversationFromRecordRow(row),
8789
row,
8890
}));
89-
const firstHumanMessageTexts = await readFirstHumanMessageTexts(
90-
db,
91-
threads.map(({ conversation }) => conversation),
92-
);
93-
return { firstHumanMessageTexts, rootIds: new Set(rootIds), threads };
91+
const [firstHumanMessageTexts, treeStates] = await Promise.all([
92+
readFirstHumanMessageTexts(
93+
db,
94+
threads.map(({ conversation }) => conversation),
95+
),
96+
readConversationTreeStates(
97+
db,
98+
threads.map(({ conversation }) => conversation.conversationId),
99+
viewerUserId,
100+
),
101+
]);
102+
return {
103+
firstHumanMessageTexts,
104+
rootIds: new Set(rootIds),
105+
threads,
106+
treeStates,
107+
};
94108
}
95109

96110
/** The Threads that a feed row lists, and how many the cap left out. */
@@ -113,7 +127,7 @@ export function nestFeedThreads(
113127
accessByRoot: ReadonlyMap<string, ConversationAccess | undefined>,
114128
): Map<string, FeedRowThreads> {
115129
if (!records) return new Map();
116-
const { firstHumanMessageTexts, rootIds, threads } = records;
130+
const { firstHumanMessageTexts, rootIds, threads, treeStates } = records;
117131
const childrenByParent = new Map<string, ConversationFeedItem[]>();
118132
const placedByRoot = new Map<string, number>();
119133
for (const { conversation, row } of threads) {
@@ -139,7 +153,10 @@ export function nestFeedThreads(
139153
: undefined),
140154
});
141155
const siblings = childrenByParent.get(parentId) ?? [];
142-
siblings.push(summary);
156+
siblings.push({
157+
...summary,
158+
...treeStates.get(conversation.conversationId),
159+
});
143160
childrenByParent.set(parentId, siblings);
144161
}
145162
// Parent edges are set once at create, so placements form a forest.

‎packages/junior/tests/integration/api/conversations/list-budget.test.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ describe("conversation list API response budget", () => {
165165
() =>
166166
expectSqlBudget(
167167
() => fixture.request("/api/conversations?include=links,media"),
168-
{ statements: 18, serialRounds: 3 },
168+
// Threads' tree states wait for the Thread read: one more round.
169+
{ statements: 18, serialRounds: 4 },
169170
),
170171
// Pins are not paged, so the body has no budget.
171172
{
@@ -176,7 +177,8 @@ describe("conversation list API response budget", () => {
176177
// row leaves them out.
177178
"conversations.participants": 52,
178179
"conversations.sidebarAnnotations": 674,
179-
"conversations.threads": 637,
180+
// A Thread's own fields, plus its tree state and Watch flag.
181+
"conversations.threads": 673,
180182
},
181183
);
182184
expect(response.status).toBe(200);

‎packages/junior/tests/integration/api/conversations/tree-state.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,38 @@ describe("conversation tree state", () => {
223223
await expect(fixture.feedTree(otherRootId)).resolves.toEqual(DONE);
224224
});
225225

226+
it("gives each Thread in the feed its own tree state", async () => {
227+
const fixture = await createStatusFixture();
228+
const rootId = await recordSlackRoot(
229+
fixture.conversationStore,
230+
"1700000000.000470",
231+
);
232+
for (const childConversationId of ["agent:feed-ask", "agent:feed-done"]) {
233+
await fixture.conversationStore.createChild({
234+
childConversationId,
235+
kind: "thread",
236+
parentConversationId: rootId,
237+
});
238+
}
239+
240+
await addItem("agent:feed-ask", ACTION_ITEM);
241+
242+
const row = await fixture.feedRow(rootId);
243+
// The sidebar orders a root's Threads by these, as the Threads panel
244+
// orders a Conversation's children.
245+
expect(
246+
Object.fromEntries(
247+
(row?.threads ?? []).map((thread) => [
248+
thread.conversationId,
249+
{ treeState: thread.treeState, watching: thread.watching },
250+
]),
251+
),
252+
).toEqual({
253+
"agent:feed-ask": { treeState: "needs_you", watching: false },
254+
"agent:feed-done": DONE,
255+
});
256+
});
257+
226258
it("needs the viewer for their own sign-in waits anywhere in the tree", async () => {
227259
const fixture = await createStatusFixture();
228260
const conversationId = await recordSlackRoot(

0 commit comments

Comments
 (0)