Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/junior/src/api/conversations/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { readRootConversationMetricsFromSql } from "./usage";
import { readConversationAuxiliaryCostsFromSql } from "./auxiliary-costs";
import { listConversationWork } from "@/chat/plugins/unfinished-work";
import { isConversationPriority } from "./priority";
import { readLastUserMessageAtByConversation } from "./user-message-activity";

const CONVERSATION_FEED_LIMIT = 50;

Expand Down Expand Up @@ -245,6 +246,7 @@ export async function readConversationFeedFromSql(
metricsByRoot,
teamDomainByTeamId,
conversationWork,
lastUserMessageAtByConversation,
] = await Promise.all([
readConversationAccessFromSql(db, conversationIds, options.viewer),
readConversationAuxiliaryCostsFromSql(db, conversationIds, {
Expand All @@ -259,6 +261,7 @@ export async function readConversationFeedFromSql(
),
),
listConversationWork(conversationIds),
readLastUserMessageAtByConversation(db, conversationIds),
]);
const assignedWork = new Set(conversationWork.assignedIds);
const unfinishedWork = new Set(conversationWork.unfinishedIds);
Expand Down Expand Up @@ -297,6 +300,9 @@ export async function readConversationFeedFromSql(
isPriority: isConversationPriority(
{
lastSeenAt: summary.lastSeenAt,
lastUserMessageAt: lastUserMessageAtByConversation.get(
conversation.conversationId,
),
...work,
},
nowMs,
Expand Down
10 changes: 8 additions & 2 deletions packages/junior/src/api/conversations/priority.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type ConversationPriorityInput = {
assignedWork?: boolean;
finishedWorkAt?: string;
lastSeenAt: string;
lastUserMessageAt?: string;
unfinishedWork?: boolean;
};

Expand All @@ -15,7 +16,7 @@ export type ConversationPriorityInput = {
*
* Priority includes only:
* - unfinished work last seen within 48 hours
* - finished assigned work with conversation activity after the finish time
* - finished assigned work with a user message after the finish time
* - no known work last seen within 3 hours
*
* Finished assigned work with no later activity stays out of Priority.
Expand All @@ -33,7 +34,12 @@ export function isConversationPriority(

if (conversation.assignedWork) {
const finishedAt = Date.parse(conversation.finishedWorkAt ?? "");
return Number.isFinite(finishedAt) && lastSeenAt > finishedAt;
const lastUserMessageAt = Date.parse(conversation.lastUserMessageAt ?? "");
return (
Number.isFinite(finishedAt) &&
Number.isFinite(lastUserMessageAt) &&
lastUserMessageAt > finishedAt
);
}

return nowMs - lastSeenAt <= UNASSIGNED_PRIORITY_WINDOW_MS;
Expand Down
46 changes: 46 additions & 0 deletions packages/junior/src/api/conversations/user-message-activity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { and, eq, inArray, isNotNull, or, sql } from "drizzle-orm";
import type { JuniorDatabase } from "@/db/db";
import { juniorConversationEvents } from "@/db/schema";

/** Read the latest true user message time for each selected conversation. */
export async function readLastUserMessageAtByConversation(
db: JuniorDatabase,
conversationIds: string[],
): Promise<Map<string, string>> {
if (conversationIds.length === 0) return new Map();

const authority = sql<string>`${juniorConversationEvents.payload}->'provenance'->>'authority'`;
const actorPlatform = sql<string>`${juniorConversationEvents.payload}->'provenance'->'actor'->>'platform'`;
const role = sql<string>`${juniorConversationEvents.payload}->>'role'`;
const rows = await db
.select({
conversationId: juniorConversationEvents.conversationId,
lastUserMessageAt: sql<Date>`max(${juniorConversationEvents.createdAt})`,
})
.from(juniorConversationEvents)
.where(
and(
inArray(juniorConversationEvents.conversationId, conversationIds),
or(
and(
eq(juniorConversationEvents.type, "user_message"),
eq(authority, "instruction"),
sql`${actorPlatform} in ('slack', 'local', 'web')`,
),
and(
eq(juniorConversationEvents.type, "message"),
eq(role, "user"),
isNotNull(juniorConversationEvents.actorIdentityId),
),
),
),
)
.groupBy(juniorConversationEvents.conversationId);

return new Map(
rows.map((row) => [
row.conversationId,
new Date(row.lastUserMessageAt).toISOString(),
]),
);
}
38 changes: 38 additions & 0 deletions packages/junior/tests/integration/api/conversations/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,44 @@ describe("conversation list API", () => {
conversationId: finishedId,
nowMs: nowMs - 120_000,
});
await fixture.sql.db().insert(juniorConversationEvents).values([
{
conversationId: finishedUpdatedId,
createdAt: new Date(nowMs - 30_000),
historyVersion: 0,
payload: {
content: "Please follow up.",
provenance: {
authority: "instruction",
actor: {
platform: "slack",
teamId: "T123",
userId: "U123456",
},
},
timestamp: nowMs - 30_000,
},
schemaVersion: 1,
seq: 0,
type: "user_message",
},
{
conversationId: finishedId,
createdAt: new Date(nowMs - 30_000),
historyVersion: 0,
payload: {
content: "Pull request merged.",
provenance: {
authority: "instruction",
actor: { name: "resource-event", platform: "system" },
},
timestamp: nowMs - 30_000,
},
schemaVersion: 1,
seq: 0,
type: "user_message",
},
]);
setPlugins([
defineJuniorPlugin({
manifest: {
Expand Down
16 changes: 14 additions & 2 deletions packages/junior/tests/unit/api/conversations/priority.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ describe("isConversationPriority", () => {
).toBe(false);
});

it("keeps finished assigned work only when activity is after the finish time", () => {
it("keeps finished assigned work only when a user message is after the finish time", () => {
expect(
isConversationPriority(
{
assignedWork: true,
finishedWorkAt: "2026-08-03T17:00:00.000Z",
lastSeenAt: "2026-08-03T18:00:00.000Z",
lastUserMessageAt: "2026-08-03T17:30:00.000Z",
},
NOW_MS,
),
Expand All @@ -41,7 +42,18 @@ describe("isConversationPriority", () => {
isConversationPriority(
{
assignedWork: true,
finishedWorkAt: "2026-08-03T18:00:00.000Z",
finishedWorkAt: "2026-08-03T17:00:00.000Z",
lastSeenAt: "2026-08-03T18:00:00.000Z",
lastUserMessageAt: "2026-08-03T16:30:00.000Z",
},
NOW_MS,
),
).toBe(false);
expect(
isConversationPriority(
{
assignedWork: true,
finishedWorkAt: "2026-08-03T17:00:00.000Z",
lastSeenAt: "2026-08-03T18:00:00.000Z",
},
NOW_MS,
Expand Down
Loading