Skip to content

Commit 7b22a0c

Browse files
Merge remote-tracking branch 'origin/main' into fix/issue-33278
2 parents a4c644e + 402d062 commit 7b22a0c

31 files changed

Lines changed: 437 additions & 66 deletions

‎apps/mobile/src/App.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import {
4949
import { RootStack, rootPathnameFromState, workspaceLocationFromState } from "./Stack";
5050
import { appAtomRegistry } from "./state/atom-registry";
5151
import { subscribeComposerDraftModelReconciliation } from "./state/use-composer-drafts";
52-
import { environmentThreadShells } from "./state/threads";
52+
import { environmentThreadShells, threadEnvironment } from "./state/threads";
5353
import { environmentShell } from "./state/shell";
5454
import { OverlayPortalHost } from "./components/ui/OverlayPortal";
5555
import { shouldHandleAppLink, restoreDraftLinkState } from "./lib/appLinking";
@@ -187,6 +187,7 @@ const appServiceDisposers = [
187187
threadShellAtom: environmentThreadShells.threadShellAtom,
188188
threadRefsAtom: environmentThreadShells.threadRefsAtom,
189189
shellStateAtom: environmentShell.stateAtom,
190+
pendingThreadIdsAtom: threadEnvironment.pendingThreadIdsAtom,
190191
}),
191192
installAppearanceRuntime(),
192193
installSubscriptionUsageRefresh(),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ import {
8888
} from "../../state/use-composer-drafts";
8989
import { useQuietThreadStructure } from "../../state/use-thread-detail";
9090
import { activeComposerDraftKey, useQueuedRunEdit } from "../../state/queued-run-edit";
91-
import { updateThreadDraftModelSelection } from "../../state/use-thread-composer-state";
91+
import { selectThreadModel } from "../../state/thread-model-selection";
9292
import { useThreadSelection } from "../../state/use-thread-selection";
9393
import { threadAllowsProviderSwitch } from "../../state/thread-provider-switching";
9494
import { environmentServerConfigsAtom, serverEnvironment } from "../../state/server";
@@ -1338,7 +1338,7 @@ export function ExistingThreadSettingsRouteScreen({
13381338
option.selection.model === modelSelection.model,
13391339
) ?? null;
13401340
const updateModelSelection = (selection: ModelSelection) =>
1341-
updateThreadDraftModelSelection(threadKey, selection);
1341+
selectThreadModel(route.params, selection);
13421342

13431343
return (
13441344
<ThreadSettingsPickerScreen
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { runAtomCommand, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
2+
import {
3+
AuthOrchestrationOperateScope,
4+
type EnvironmentId,
5+
type ModelSelection,
6+
type ThreadId,
7+
} from "@t3tools/contracts";
8+
9+
import { scopedThreadKey } from "../lib/scopedEntities";
10+
import * as Dialog from "../components/ui/Dialog";
11+
import { appAtomRegistry } from "./atom-registry";
12+
import { readEnvironmentScope } from "./session";
13+
import { environmentThreadShells, threadEnvironment } from "./threads";
14+
import {
15+
releaseComposerDraftModelSelection,
16+
updateComposerDraftSettings,
17+
} from "./use-composer-drafts";
18+
19+
/** Sets an existing thread's pick on the thread, so any next turn runs on it. */
20+
export function selectThreadModel(
21+
ref: { readonly environmentId: EnvironmentId; readonly threadId: ThreadId },
22+
value: ModelSelection,
23+
): void {
24+
const threadKey = scopedThreadKey(ref.environmentId, ref.threadId);
25+
// Its own object, so an older reply for an equal pick cannot release this one.
26+
const modelSelection = { ...value };
27+
updateComposerDraftSettings(threadKey, { modelSelection });
28+
const thread = appAtomRegistry.get(environmentThreadShells.threadShellAtom(ref));
29+
// Another instance replaces the live session, which may hold an open question; that pick
30+
// still waits for a sent message.
31+
if (
32+
thread?.modelSelection.instanceId !== modelSelection.instanceId ||
33+
!readEnvironmentScope(ref.environmentId, AuthOrchestrationOperateScope)
34+
) {
35+
return;
36+
}
37+
void runAtomCommand(
38+
appAtomRegistry,
39+
threadEnvironment.setModelSelection,
40+
{ environmentId: ref.environmentId, input: { threadId: ref.threadId, modelSelection } },
41+
{ label: "change model", reportFailure: true, reportDefect: true },
42+
).then((result) => {
43+
// Released on the reply, so a later change from another client shows.
44+
if (result._tag === "Success") {
45+
releaseComposerDraftModelSelection(threadKey, modelSelection);
46+
} else {
47+
const error = squashAtomCommandFailure(result);
48+
Dialog.alert(
49+
"Could not apply model selection",
50+
`This model pick was not saved. ${error instanceof Error ? error.message : "Try again."}`,
51+
);
52+
}
53+
});
54+
}

‎apps/mobile/src/state/use-composer-drafts.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ function modelReconciliationHarness(
310310
],
311311
]),
312312
});
313+
const noPendingThreadIdsAtom = Atom.make((): ReadonlySet<ThreadId> => new Set());
313314
const shells = createEnvironmentThreadShellAtoms({
314315
catalogValueAtom,
315316
snapshotAtom: () => snapshotAtom,
@@ -323,6 +324,7 @@ function modelReconciliationHarness(
323324
threadShellAtom: shells.threadShellAtom,
324325
threadRefsAtom: shells.threadRefsAtom,
325326
shellStateAtom: () => shellResultAtom,
327+
pendingThreadIdsAtom: () => noPendingThreadIdsAtom,
326328
}),
327329
);
328330
flush();

‎apps/mobile/src/state/use-composer-drafts.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
type ProviderOptionSelection,
2020
type RuntimeMode,
2121
type ScopedThreadRef,
22+
type ThreadId,
2223
} from "@t3tools/contracts";
2324
import * as Schema from "effect/Schema";
2425
import { useEffect, useState } from "react";
@@ -1541,6 +1542,20 @@ export function updateComposerDraftSettings(
15411542
});
15421543
}
15431544

1545+
/** Lets the thread's model show again once the server accepted this exact pick. */
1546+
export function releaseComposerDraftModelSelection(
1547+
draftKey: string,
1548+
modelSelection: ModelSelection,
1549+
): void {
1550+
updateComposerDrafts((current) => {
1551+
const existing = current[draftKey];
1552+
// A newer pick, even of the same model, is still waiting for its own reply.
1553+
if (existing?.modelSelection !== modelSelection) return current;
1554+
const { modelSelection: _modelSelection, ...draft } = existing;
1555+
return withComposerDraft(current, draftKey, draft);
1556+
});
1557+
}
1558+
15441559
export function setFreshSessionPending(draftKey: string, pending: boolean): void {
15451560
if ((getComposerDraftSnapshot(draftKey).freshContext === true) === pending) return;
15461561
updateComposerDraftSettings(draftKey, { freshContext: pending ? true : undefined });
@@ -1557,6 +1572,7 @@ export function subscribeComposerDraftModelReconciliation(input: {
15571572
readonly shellStateAtom: (
15581573
environmentId: EnvironmentId,
15591574
) => Atom.Atom<AsyncResult.AsyncResult<EnvironmentShellState, unknown>>;
1575+
readonly pendingThreadIdsAtom: (environmentId: EnvironmentId) => Atom.Atom<ReadonlySet<ThreadId>>;
15601576
}): () => void {
15611577
const draftsAtom = Atom.make(appAtomRegistry.get(composerDraftsAtom));
15621578
const unsubscribeDrafts = appAtomRegistry.subscribe(composerDraftsAtom, (drafts) =>
@@ -1576,6 +1592,8 @@ export function subscribeComposerDraftModelReconciliation(input: {
15761592
const thread = get(input.threadShellAtom(ref));
15771593
if (!thread || !draftModelPickMatchesThread(draft.modelSelection, thread.modelSelection))
15781594
continue;
1595+
// The pick can match an old model while an earlier pick in flight still replaces it.
1596+
if (get(input.pendingThreadIdsAtom(ref.environmentId)).has(ref.threadId)) continue;
15791597
// Status and selection must come from the same emission; derived atoms can update apart.
15801598
const shell = AsyncResult.getOrElse(get(input.shellStateAtom(ref.environmentId)), () => null);
15811599
if (shell?.status !== "live") continue;

‎apps/mobile/src/state/use-thread-composer-state.ts‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ import {
7979
scheduleUnusedComposerAttachmentCleanup,
8080
setComposerDraftText,
8181
setFreshSessionPending,
82-
updateComposerDraftSettings,
8382
useComposerDraft,
8483
useComposerDraftSettings,
8584
} from "./use-composer-drafts";
@@ -178,10 +177,6 @@ export function useThreadDraftForThread(input: {
178177
};
179178
}
180179

181-
export function updateThreadDraftModelSelection(threadKey: string, value: ModelSelection): void {
182-
updateComposerDraftSettings(threadKey, { modelSelection: value });
183-
}
184-
185180
export function useThreadComposerState(input: {
186181
/** Forks the thread into a side chat that gets `prompt`; resolves false when it did not start. */
187182
readonly startSideChat: (

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,18 @@ export interface McpInvocationScope {
136136
/**
137137
* T3 Code renders `t3-thread://` links as thread chips. Other MCP clients cannot open them, so
138138
* they get the hosted link Copy thread copies, which opens the thread in either app or a browser.
139+
* A known `title` labels the chip for clients that cannot load this environment's threads.
139140
*/
140141
export const threadLinkFor = (
141142
scope: Pick<McpInvocationScope, "environmentId" | "client">,
142143
threadId: ThreadId,
144+
title?: string,
143145
): string => {
144146
const hostedAppUrl = scope.client?.hostedAppUrl;
145147
const ref = { environmentId: scope.environmentId, threadId };
146-
return hostedAppUrl === undefined ? formatThreadLink(ref) : hostedThreadLink(ref, hostedAppUrl);
148+
return hostedAppUrl === undefined
149+
? formatThreadLink(ref, title)
150+
: hostedThreadLink(ref, hostedAppUrl);
147151
};
148152

149153
export class McpInvocationContext extends Context.Service<

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ function listItemFromShell(
805805
threadId: shell.id,
806806
projectId: shell.projectId,
807807
title: shell.title,
808-
link: threadLinkFor(scope, shell.id),
808+
link: threadLinkFor(scope, shell.id, shell.title),
809809
createdBy: shell.createdBy,
810810
creationSource: shell.creationSource,
811811
status: shell.activityRunStatus ?? shell.status,
@@ -839,7 +839,7 @@ function threadDetail(
839839
threadId: projection.thread.id,
840840
projectId: projection.thread.projectId,
841841
title: projection.thread.title,
842-
link: threadLinkFor(scope, projection.thread.id),
842+
link: threadLinkFor(scope, projection.thread.id, projection.thread.title),
843843
createdBy: projection.thread.createdBy,
844844
creationSource: projection.thread.creationSource,
845845
status: active?.status ?? latest?.status ?? "idle",
@@ -1630,10 +1630,10 @@ const make = Effect.gen(function* () {
16301630
const result = {
16311631
taskId: task.id,
16321632
childThreadId: task.childThreadId,
1633-
link: formatThreadLink({
1634-
environmentId: scope.environmentId,
1635-
threadId: task.childThreadId,
1636-
}),
1633+
link: formatThreadLink(
1634+
{ environmentId: scope.environmentId, threadId: task.childThreadId },
1635+
childControls.thread.title,
1636+
),
16371637
childRunId: childRun?.id ?? null,
16381638
childNodeId: task.id,
16391639
status,
@@ -1804,7 +1804,10 @@ const make = Effect.gen(function* () {
18041804
childThreadId: null,
18051805
...(task.remoteChild === undefined
18061806
? {}
1807-
: { remoteChild: task.remoteChild, link: formatThreadLink(task.remoteChild) }),
1807+
: {
1808+
remoteChild: task.remoteChild,
1809+
link: formatThreadLink(task.remoteChild, task.remoteChild.title),
1810+
}),
18081811
childRunId: null,
18091812
childNodeId: task.id,
18101813
status,
@@ -2992,7 +2995,10 @@ const make = Effect.gen(function* () {
29922995
);
29932996
return {
29942997
threadId,
2995-
link: formatThreadLink({ environmentId: scope.environmentId, threadId }),
2998+
link: formatThreadLink(
2999+
{ environmentId: scope.environmentId, threadId },
3000+
projection.thread.title,
3001+
),
29963002
runId: run?.id ?? null,
29973003
status: run?.status ?? "idle",
29983004
title: projection.thread.title,
@@ -3209,7 +3215,7 @@ const make = Effect.gen(function* () {
32093215
);
32103216
return {
32113217
threadId: input.threadId,
3212-
link: threadLinkFor(scope, input.threadId),
3218+
link: threadLinkFor(scope, input.threadId, target.thread.title),
32133219
messageId,
32143220
runId: result.run.id,
32153221
...(result.run.delegatedTaskId === undefined ? {} : { taskId: result.run.delegatedTaskId }),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ it.effect(
221221
expect(belowCeiling.result).toMatchObject({
222222
projectId: belowCeiling.projectId,
223223
modelSelection: belowCeiling.modelSelection,
224-
link: `<t3-thread://v2/environment/${belowCeiling.launched[0]?.threadId}>`,
224+
link: `[Fix](t3-thread://v2/environment/${belowCeiling.launched[0]?.threadId})`,
225225
});
226226
expect(belowCeiling.launched[0]?.runtimeMode).toBe("approval-required");
227227
expect(belowCeiling.launched[0]?.initialMessage?.senderThreadId).toBeUndefined();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, {
277277
const run = result.projection.runs.find((run) => run.userMessageId === messageId);
278278
return {
279279
threadId: thread.id,
280-
link: McpInvocationContext.threadLinkFor(scope, thread.id),
280+
link: McpInvocationContext.threadLinkFor(scope, thread.id, thread.title),
281281
projectId: thread.projectId,
282282
modelSelection: thread.modelSelection,
283283
runId: run?.id ?? null,

0 commit comments

Comments
 (0)