Skip to content

Commit 611132c

Browse files
t3dotggclaude
andauthored
feat(server,web,mobile): agents see snooze state and link to threads (pingdotgg#16782)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1 parent cd41c4a commit 611132c

18 files changed

Lines changed: 381 additions & 19 deletions

File tree

‎apps/mobile/modules/t3-markdown-text/src/markdownLinks.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
normalizeMarkdownLinkDestination,
66
parseMarkdownFileLink,
77
} from "@t3tools/client-runtime/markdown-links";
8+
import { parseThreadLinkHref } from "@t3tools/shared/threadLinks";
89
import { videoMimeType } from "@t3tools/shared/video";
910

1011
import type { MARKDOWN_FILE_ICON_SOURCES } from "./markdownFileIcons.generated";
@@ -293,7 +294,11 @@ export function resolveMarkdownLinkPresentation(href: string): MarkdownLinkPrese
293294

294295
return {
295296
kind: "link",
296-
href: /^(?:mailto|tel):/i.test(normalized) ? normalized : null,
297+
// A thread link keeps its href so a press reaches the feed, which opens the thread.
298+
href:
299+
/^(?:mailto|tel):/i.test(normalized) || parseThreadLinkHref(normalized) !== null
300+
? normalized
301+
: null,
297302
};
298303
}
299304

‎apps/mobile/src/features/threads/ThreadFeed.tsx‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
} from "@t3tools/contracts";
2626
import { renderAssistantCitationsAsText } from "@t3tools/shared/assistantCitations";
2727
import { encodeComposerContextFragment } from "@t3tools/shared/composerContextClipboard";
28+
import { parseThreadLinkHref } from "@t3tools/shared/threadLinks";
2829
import {
2930
parseComposerContextHref,
3031
collectComposerContextReferences,
@@ -1242,16 +1243,20 @@ function useMarkdownStyles(
12421243
);
12431244
}
12441245
const linkHref = presentation.href;
1246+
// A thread link opens the thread in the app, through the feed's link handler.
1247+
const isThreadLink = parseThreadLinkHref(href) !== null;
12451248
return (
12461249
<MarkdownLinkLabelContext.Provider value="other">
12471250
<NativeText
12481251
className="underline"
12491252
onPress={
1250-
linkHref
1251-
? () => {
1252-
void tryOpenExternalUrl(linkHref, "markdown-link");
1253-
}
1254-
: undefined
1253+
isThreadLink
1254+
? () => onLinkPress(href)
1255+
: linkHref
1256+
? () => {
1257+
void tryOpenExternalUrl(linkHref, "markdown-link");
1258+
}
1259+
: undefined
12551260
}
12561261
style={{ color: markdownLinkColor }}
12571262
>
@@ -2271,6 +2276,14 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
22712276
const userBubbleColor = theme["--color-user-bubble"];
22722277
const onMarkdownLinkPress = useCallback(
22732278
(href: string) => {
2279+
const threadLink = parseThreadLinkHref(href);
2280+
if (threadLink) {
2281+
navigation.navigate("Thread", {
2282+
environmentId: String(threadLink.environmentId),
2283+
threadId: String(threadLink.threadId),
2284+
});
2285+
return;
2286+
}
22742287
const presentation = resolveMarkdownLinkPresentation(href);
22752288
if (presentation.kind === "file") {
22762289
const relativePath = resolveWorkspaceRelativeFilePath(

‎apps/mobile/src/lib/nativeMarkdownText.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,21 @@ describe("nativeMarkdownTextRuns", () => {
8484
]);
8585
});
8686

87+
it("keeps a thread link's href so a press can open the thread", () => {
88+
expect(
89+
nativeMarkdownTextRuns({
90+
type: "paragraph",
91+
children: [
92+
{
93+
type: "link",
94+
href: "t3-thread://v1/env/thread-1",
95+
children: [{ type: "text", content: "Fix the build" }],
96+
},
97+
],
98+
}),
99+
).toEqual([{ text: "Fix the build", href: "t3-thread://v1/env/thread-1" }]);
100+
});
101+
87102
it("preserves the destination of a link with a code-formatted label", () => {
88103
expect(
89104
nativeMarkdownTextRuns({

‎apps/server/src/mcp/OrchestratorMcpService.ts‎

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
type EnvironmentId,
23
CommandId,
34
type RunId,
45
isProviderAvailable,
@@ -58,6 +59,8 @@ import {
5859
ThreadId,
5960
} from "@t3tools/contracts";
6061
import { runRanAfter } from "@t3tools/shared/orchestrationV2ThreadError";
62+
import { formatThreadLink } from "@t3tools/shared/threadLinks";
63+
import * as Clock from "effect/Clock";
6164
import * as Context from "effect/Context";
6265
import * as Crypto from "effect/Crypto";
6366
import * as DateTime from "effect/DateTime";
@@ -80,6 +83,7 @@ import {
8083
type DispatchModeRefusal,
8184
} from "../orchestration-v2/DispatchModeLimit.ts";
8285
import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
86+
import { isSnoozed } from "../orchestration-v2/ThreadSettlementService.ts";
8387
import * as ProjectService from "../project/ProjectService.ts";
8488
import * as ProviderRegistry from "../provider/ProviderRegistry.ts";
8589
import * as ScheduledTaskService from "../scheduledTasks/ScheduledTaskService.ts";
@@ -617,9 +621,35 @@ function threadSettlement(
617621
};
618622
}
619623

620-
function listItemFromShell(shell: OrchestrationV2ThreadShell): OrchestratorMcpThreadListItem {
624+
function threadSnooze(
625+
thread: Parameters<typeof isSnoozed>[0],
626+
nowMs: number,
627+
): Pick<OrchestratorMcpThreadListItem, "snoozed" | "snoozedUntil"> {
628+
const snoozed = isSnoozed(thread, nowMs);
629+
return {
630+
snoozed,
631+
snoozedUntil:
632+
snoozed && thread.snoozedUntil != null ? DateTime.formatIso(thread.snoozedUntil) : null,
633+
};
634+
}
635+
636+
/** Where and when a thread is shown: the environment for its link, and the time for snooze state. */
637+
interface ThreadViewContext {
638+
readonly environmentId: EnvironmentId;
639+
readonly nowMs: number;
640+
}
641+
642+
function listItemFromShell(
643+
shell: OrchestrationV2ThreadShell,
644+
context: ThreadViewContext,
645+
): OrchestratorMcpThreadListItem {
621646
return {
622647
threadId: shell.id,
648+
link: formatThreadLink({
649+
environmentId: context.environmentId,
650+
threadId: shell.id,
651+
title: shell.title,
652+
}),
623653
title: shell.title,
624654
createdBy: shell.createdBy,
625655
creationSource: shell.creationSource,
@@ -631,6 +661,7 @@ function listItemFromShell(shell: OrchestrationV2ThreadShell): OrchestratorMcpTh
631661
interactionMode: shell.interactionMode,
632662
linkedPullRequest: shell.linkedPullRequest ?? null,
633663
...threadSettlement(shell),
664+
...threadSnooze(shell, context.nowMs),
634665
parentThreadId: shell.lineage.parentThreadId,
635666
relationshipToParent: shell.lineage.relationshipToParent,
636667
itemCount: shell.visibleItemCount,
@@ -642,11 +673,18 @@ function listItemFromShell(shell: OrchestrationV2ThreadShell): OrchestratorMcpTh
642673
function threadDetail(
643674
projection: Pick<OrchestrationV2ThreadProjection, "thread" | "runs" | "runtimeRequests">,
644675
itemCount: number,
676+
shell: OrchestrationV2ThreadShell,
677+
context: ThreadViewContext,
645678
): OrchestratorMcpThreadDetail {
646679
const latest = ThreadManagementService.latestRun(projection);
647680
const active = ThreadManagementService.latestActiveRun(projection);
648681
return {
649682
threadId: projection.thread.id,
683+
link: formatThreadLink({
684+
environmentId: context.environmentId,
685+
threadId: projection.thread.id,
686+
title: projection.thread.title,
687+
}),
650688
projectId: projection.thread.projectId,
651689
title: projection.thread.title,
652690
createdBy: projection.thread.createdBy,
@@ -678,6 +716,8 @@ function threadDetail(
678716
).length,
679717
archived: projection.thread.archivedAt !== null,
680718
...threadSettlement(projection.thread),
719+
// From the shell, like the list, so read and list agree on snooze state.
720+
...threadSnooze(shell, context.nowMs),
681721
createdAt: DateTime.formatIso(projection.thread.createdAt),
682722
updatedAt: DateTime.formatIso(projection.thread.updatedAt),
683723
};
@@ -1024,7 +1064,7 @@ const make = Effect.gen(function* () {
10241064
"contextTransfers",
10251065
])
10261066
.pipe(Effect.mapError(threadManagementFailure));
1027-
return { parent, target } as const;
1067+
return { parent, target, shell } as const;
10281068
});
10291069

10301070
const loadProviders = providerRegistry.getProviders;
@@ -2226,6 +2266,7 @@ const make = Effect.gen(function* () {
22262266
failure("orchestration_error", `Unable to list threads: ${errorMessage(error)}`),
22272267
),
22282268
);
2269+
const nowMs = yield* Clock.currentTimeMillis;
22292270
const statuses = input.statuses === undefined ? null : new Set(input.statuses);
22302271
const titleContains = input.titleContains?.toLocaleLowerCase();
22312272
const filtered = projectThreads
@@ -2237,6 +2278,9 @@ const make = Effect.gen(function* () {
22372278
(thread) =>
22382279
input.settled === undefined || threadSettlement(thread).settled === input.settled,
22392280
)
2281+
.filter(
2282+
(thread) => input.snoozed === undefined || isSnoozed(thread, nowMs) === input.snoozed,
2283+
)
22402284
.filter(
22412285
(thread) =>
22422286
titleContains === undefined ||
@@ -2249,14 +2293,16 @@ const make = Effect.gen(function* () {
22492293
return {
22502294
projectId,
22512295
currentThreadId: parent?.thread.id ?? null,
2252-
threads: page.map(listItemFromShell),
2296+
threads: page.map((shell) =>
2297+
listItemFromShell(shell, { environmentId: scope.environmentId, nowMs }),
2298+
),
22532299
nextCursor,
22542300
total: filtered.length,
22552301
} satisfies OrchestratorMcpThreadListResult;
22562302
}),
22572303
readThread: (scope, input) =>
22582304
Effect.gen(function* () {
2259-
const { parent, target } = yield* loadReadableThread(scope, input.threadId);
2305+
const { parent, target, shell } = yield* loadReadableThread(scope, input.threadId);
22602306
const view = input.view ?? "messages";
22612307
const afterPosition = input.afterPosition ?? -1;
22622308
const limit = input.limit ?? DEFAULT_THREAD_READ_LIMIT;
@@ -2329,7 +2375,10 @@ const make = Effect.gen(function* () {
23292375
}
23302376
}
23312377
return {
2332-
thread: threadDetail(target, timeline.totalItems),
2378+
thread: threadDetail(target, timeline.totalItems, shell, {
2379+
environmentId: scope.environmentId,
2380+
nowMs: yield* Clock.currentTimeMillis,
2381+
}),
23332382
recentRuns: target.runs
23342383
.toSorted((left, right) => right.ordinal - left.ordinal)
23352384
.slice(0, input.runLimit ?? DEFAULT_THREAD_RUN_LIMIT)

‎apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2232,6 +2232,44 @@ describe("orchestrator MCP toolkit", () => {
22322232
reason: "user",
22332233
});
22342234

2235+
expect(metadataRead.thread).toMatchObject({ snoozed: false, snoozedUntil: null });
2236+
expect(metadataRead.thread.link).toBe(
2237+
`[Metadata-managed thread](t3-thread://v1/environment%3Amcp-orchestrator/${encodeURIComponent(emptyThread.threadId)})`,
2238+
);
2239+
yield* orchestrator.dispatch({
2240+
type: "thread.snooze",
2241+
commandId: CommandId.make("command:mcp-empty:snooze"),
2242+
threadId: emptyThread.threadId,
2243+
snoozedUntil: "2099-01-01T00:00:00.000Z",
2244+
});
2245+
const snoozedListCall = yield* invoke("t3_thread_list", { snoozed: true, limit: 100 });
2246+
const snoozedList = yield* decodeThreadListResult(
2247+
snoozedListCall.structuredContent,
2248+
).pipe(Effect.orDie);
2249+
expect(snoozedList.threads.map((thread) => thread.threadId)).toEqual([
2250+
emptyThread.threadId,
2251+
]);
2252+
expect(snoozedList.threads[0]).toMatchObject({
2253+
snoozed: true,
2254+
snoozedUntil: "2099-01-01T00:00:00.000Z",
2255+
});
2256+
const snoozedReadCall = yield* invoke("t3_thread_read", {
2257+
threadId: emptyThread.threadId,
2258+
});
2259+
const snoozedRead = yield* decodeThreadReadResult(
2260+
snoozedReadCall.structuredContent,
2261+
).pipe(Effect.orDie);
2262+
expect(snoozedRead.thread).toMatchObject({
2263+
snoozed: true,
2264+
snoozedUntil: "2099-01-01T00:00:00.000Z",
2265+
});
2266+
yield* orchestrator.dispatch({
2267+
type: "thread.unsnooze",
2268+
commandId: CommandId.make("command:mcp-empty:unsnooze"),
2269+
threadId: emptyThread.threadId,
2270+
reason: "user",
2271+
});
2272+
22352273
const unlinkedCall = yield* invoke("t3_thread_update", {
22362274
threadId: emptyThread.threadId,
22372275
action: "unlink_pull_request",

‎apps/server/src/mcp/toolkits/orchestrator/tools.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ export const CreateThreadsTool = Tool.make("create_threads", {
178178

179179
const ThreadListTool = Tool.make("t3_thread_list", {
180180
description:
181-
"List T3 threads in a project, newest first. Omit projectId for the calling thread's project. Filter by durable run status, title, or settled state (settled=true lists threads the user or auto-settlement moved out of the active list) and paginate with the returned cursor.",
181+
"List T3 threads in a project, newest first. Omit projectId for the calling thread's project. Filter by durable run status, title, or settled state (settled=true lists threads the user or auto-settlement moved out of the active list), or snoozed state, and paginate with the returned cursor. A snoozed thread wakes early when it asks for something, fails, or completes. Each thread has a link: paste it when you mention the thread so the user can open it.",
182182
parameters: OrchestratorMcpThreadListInput,
183183
success: OrchestratorMcpThreadListResult,
184184
failure: OrchestratorMcpFailure,
@@ -192,7 +192,7 @@ const ThreadListTool = Tool.make("t3_thread_list", {
192192

193193
const ThreadReadTool = Tool.make("t3_thread_read", {
194194
description:
195-
"Read durable state and a paginated timeline from any T3 thread in this environment. The default messages view returns user messages, assistant messages, and proposed plans; activity returns all summarized timeline items. Reading an untruncated terminal assistant result from this parent thread's direct app-owned child acknowledges that child's automatic completion delivery. Continue with afterPosition=nextPosition. Recover long item text with itemId and textOffset=nextTextOffset until nextTextOffset is null; offsets count UTF-16 code units.",
195+
"Read durable state and a paginated timeline from any T3 thread in this environment. The default messages view returns user messages, assistant messages, and proposed plans; activity returns all summarized timeline items. Reading an untruncated terminal assistant result from this parent thread's direct app-owned child acknowledges that child's automatic completion delivery. Continue with afterPosition=nextPosition. Recover long item text with itemId and textOffset=nextTextOffset until nextTextOffset is null; offsets count UTF-16 code units. The thread has a link and snooze state: paste the link when you mention the thread so the user can open it.",
196196
parameters: OrchestratorMcpThreadReadInput,
197197
success: OrchestratorMcpThreadReadResult,
198198
failure: OrchestratorMcpFailure,

‎apps/server/src/mcp/toolkits/project/handlers.test.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ it.effect("attributes a launched thread's first message to the calling thread",
6969
return Effect.succeed({
7070
threadId: input.threadId,
7171
projection: {
72-
thread: { id: input.threadId, projectId, modelSelection },
72+
thread: { id: input.threadId, projectId, modelSelection, title: input.title },
7373
runs: [],
7474
},
7575
resumed: false,
@@ -94,7 +94,11 @@ it.effect("attributes a launched thread's first message to the calling thread",
9494
const result = yield* toolkit
9595
.handle("t3_thread_launch", { title: "Audit", message: "Review the change" })
9696
.pipe(Stream.unwrap, Stream.runCollect, Effect.provide(layerDependencies));
97-
expect(result.at(-1)?.result).toMatchObject({ projectId, modelSelection });
97+
expect(result.at(-1)?.result).toMatchObject({
98+
projectId,
99+
modelSelection,
100+
link: expect.stringMatching(/^\[Audit\]\(t3-thread:\/\/v1\/environment\//),
101+
});
98102
expect(launchedSender).toBe(sourceThreadId);
99103
}),
100104
);
@@ -140,7 +144,12 @@ it.effect("launches a scratch thread into the Scratch project", () =>
140144
return Effect.succeed({
141145
threadId: input.threadId,
142146
projection: {
143-
thread: { id: input.threadId, projectId: input.projectId, modelSelection },
147+
thread: {
148+
id: input.threadId,
149+
projectId: input.projectId,
150+
modelSelection,
151+
title: input.title,
152+
},
144153
runs: [],
145154
},
146155
resumed: false,
@@ -329,6 +338,7 @@ const clientLaunchHarness = (input: {
329338
id: launch.threadId,
330339
projectId: launch.projectId,
331340
modelSelection: launch.modelSelection,
341+
title: launch.title,
332342
},
333343
runs: [],
334344
},

‎apps/server/src/mcp/toolkits/project/handlers.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { MessageId, ThreadId, OrchestratorMcpFailure, ProjectId } from "@t3tools/contracts";
2+
import { formatThreadLink } from "@t3tools/shared/threadLinks";
23
import * as Effect from "effect/Effect";
34
import * as FileSystem from "effect/FileSystem";
45
import * as Option from "effect/Option";
@@ -57,7 +58,7 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, {
5758
(input, { runtimeMode, interactionMode }) =>
5859
Effect.gen(function* () {
5960
const context = yield* readCaller();
60-
const { caller } = context;
61+
const { caller, scope } = context;
6162
const commandId = yield* newCommandId();
6263
const threadId = ThreadId.make(commandId);
6364
const messageId = MessageId.make(commandId);
@@ -146,6 +147,11 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, {
146147
const run = result.projection.runs.find((run) => run.userMessageId === messageId);
147148
return {
148149
threadId: thread.id,
150+
link: formatThreadLink({
151+
environmentId: scope.environmentId,
152+
threadId: thread.id,
153+
title: thread.title,
154+
}),
149155
projectId: thread.projectId,
150156
modelSelection: thread.modelSelection,
151157
runId: run?.id ?? null,

0 commit comments

Comments
 (0)