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
36 changes: 36 additions & 0 deletions apps/server/src/orchestration-v2/ThreadTitleRefreshWorker.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { assert, describe, it } from "@effect/vitest";
import {
CommandId,
EnvironmentId,
EventId,
MessageId,
ProjectId,
Expand Down Expand Up @@ -43,9 +44,44 @@ describe("threadsDueForTitleRefresh", () => {
latestRunRequestedAt: null,
latestRunStartedAt: null,
latestRunCompletedAt: null,
lineage: {
parentThreadId: null,
relationshipToParent: null,
rootThreadId: ThreadId.make(id),
},
...overrides,
});

it("leaves subagents, side chats and delegated tasks alone but renames forks", () => {
const active = { latestUserMessageAt: hoursAgo(1) };
const child = (relationshipToParent: "fork" | "subagent" | null, sideChat?: boolean) => ({
parentThreadId: ThreadId.make("parent"),
relationshipToParent,
rootThreadId: ThreadId.make("parent"),
...(sideChat === undefined ? {} : { sideChat }),
});
assert.deepEqual(
ThreadTitleRefreshWorker.threadsDueForTitleRefresh(
[
shell("subagent", { ...active, lineage: child("subagent") }),
shell("side-chat", { ...active, lineage: child(null, true) }),
shell("delegated", {
...active,
delegatedFrom: {
environmentId: EnvironmentId.make("remote"),
threadId: ThreadId.make("remote-parent"),
title: "Remote parent",
},
}),
shell("fork", { ...active, lineage: child("fork") }),
shell("top-level", active),
],
nowMs,
).map(({ threadId }) => threadId),
["fork", "top-level"].map((id) => ThreadId.make(id)),
);
});

it("selects threads with a message or turn in the last three hours, newest first", () => {
assert.deepEqual(
ThreadTitleRefreshWorker.threadsDueForTitleRefresh(
Expand Down
25 changes: 19 additions & 6 deletions apps/server/src/orchestration-v2/ThreadTitleRefreshWorker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { CommandId, type OrchestrationV2ThreadShell, type ThreadId } from "@t3tools/contracts";
import {
CommandId,
isDelegatedAppThread,
isListedThreadLineage,
type OrchestrationV2ThreadShell,
type ThreadId,
} from "@t3tools/contracts";
import * as Cause from "effect/Cause";
import * as DateTime from "effect/DateTime";
import * as Duration from "effect/Duration";
Expand All @@ -18,10 +24,9 @@ const TITLE_REFRESH_CONCURRENCY = 2;
const TITLE_REFRESH_SETTLE_TIMEOUT = Duration.minutes(4);

/**
* Threads whose latest user message or agent turn falls inside the activity
* window, newest first, each once, with that activity time. A thread with a
* title generation already in flight is left to that request, and one with no
* activity after the time recorded in `refreshedActivityMs` is skipped.
* Listed threads with a user message or agent turn inside the activity window, newest first,
* each once. Threads with a title generation in flight, or no activity since
* `refreshedActivityMs`, are skipped; subagents, delegated tasks and side chats are never renamed.
*/
export function threadsDueForTitleRefresh(
threads: ReadonlyArray<
Expand All @@ -35,6 +40,8 @@ export function threadsDueForTitleRefresh(
| "latestRunRequestedAt"
| "latestRunStartedAt"
| "latestRunCompletedAt"
| "lineage"
| "delegatedFrom"
>
>,
nowMs: number,
Expand All @@ -43,7 +50,13 @@ export function threadsDueForTitleRefresh(
const windowStartMs = nowMs - Duration.toMillis(TITLE_REFRESH_ACTIVITY_WINDOW);
const due = new Map<ThreadId, number>();
for (const thread of threads) {
if (thread.archivedAt !== null || thread.deletedAt !== null || thread.titleRegeneration) {
if (
thread.archivedAt !== null ||
thread.deletedAt !== null ||
thread.titleRegeneration ||
!isListedThreadLineage(thread.lineage) ||
isDelegatedAppThread(thread)
) {
continue;
}
const activityMs = Math.max(
Expand Down
Loading