Skip to content

Commit 5baf761

Browse files
committed
chore: merge current main into Keychain fix
2 parents 6a69236 + 9482773 commit 5baf761

53 files changed

Lines changed: 2608 additions & 135 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/mobile/src/App.tsx‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import {
2020
} from "./features/settings/appearance/AppearancePreferencesProvider";
2121
import { RootStack } from "./Stack";
2222
import { appAtomRegistry } from "./state/atom-registry";
23+
import { subscribeComposerDraftModelReconciliation } from "./state/use-composer-drafts";
24+
import { environmentThreadShells } from "./state/threads";
25+
import { environmentShell } from "./state/shell";
2326
import { OverlayPortalHost } from "./components/ui/OverlayPortal";
2427
import { shouldHandleAppLink } from "./lib/appLinking";
2528
import { useMobileNavigationTheme } from "./lib/useMobileNavigationTheme";
@@ -57,6 +60,16 @@ function SplashScreenCoordinator() {
5760

5861
export default function App() {
5962
useUiRuntimeMemoryWarningGc();
63+
useEffect(
64+
() =>
65+
subscribeComposerDraftModelReconciliation({
66+
registry: appAtomRegistry,
67+
threadShellAtom: environmentThreadShells.threadShellAtom,
68+
threadRefsAtom: environmentThreadShells.threadRefsAtom,
69+
shellStateAtom: environmentShell.stateAtom,
70+
}),
71+
[],
72+
);
6073

6174
return (
6275
<RegistryContext.Provider value={appAtomRegistry}>

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

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
import { PrimaryConnectionTarget } from "@t3tools/client-runtime/connection";
2+
import { createEnvironmentThreadShellAtoms } from "@t3tools/client-runtime/state/threads";
3+
import {
4+
applyShellStreamEvent,
5+
createEnvironmentSnapshotAtom,
6+
type EnvironmentShellState,
7+
type EnvironmentShellStatus,
8+
} from "@t3tools/client-runtime/state/shell";
9+
import {
10+
v2ShellSnapshot,
11+
v2ThreadShell,
12+
} from "../../../../packages/client-runtime/src/state/orchestrationV2TestFixtures";
13+
import * as Option from "effect/Option";
14+
import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
115
import { afterEach, describe, expect, it } from "@effect/vitest";
216
import {
317
CommandId,
@@ -7,6 +21,9 @@ import {
721
ProjectId,
822
ProviderInstanceId,
923
ThreadId,
24+
type OrchestrationV2ThreadShell,
25+
type OrchestrationV2ShellSnapshot,
26+
type ModelSelection,
1027
} from "@t3tools/contracts";
1128
import { onTestFinished, vi } from "vite-plus/test";
1229

@@ -162,6 +179,7 @@ import {
162179
archiveCloudComposerDrafts,
163180
clearComposerDraftContent,
164181
clearComposerDraftContentState,
182+
subscribeComposerDraftModelReconciliation,
165183
clearComposerDraftsEnvironment,
166184
ComposerDraftPersistenceError,
167185
composerDraftsAtom,
@@ -196,6 +214,7 @@ import {
196214
stickyComposerModelSelectionAtom,
197215
undoComposerDraftMerge,
198216
undoComposerDraftMergeState,
217+
updateComposerDraftSettings,
199218
} from "./use-composer-drafts";
200219
import { retainComposerAttachmentFileForPreview } from "../lib/composerAttachmentPreviewRetention";
201220

@@ -242,6 +261,98 @@ function contextDraft(start: number, count: number): ComposerDraft {
242261
};
243262
}
244263

264+
function modelReconciliationHarness(
265+
environmentId: EnvironmentId,
266+
threadId: ThreadId,
267+
initialStatus: EnvironmentShellStatus = "live",
268+
) {
269+
const tasks: Array<() => void> = [];
270+
const registry = AtomRegistry.make({
271+
scheduleTask: (task) => {
272+
tasks.push(task);
273+
return () => {};
274+
},
275+
});
276+
onTestFinished(() => registry.dispose());
277+
const flush = () => {
278+
for (let task = tasks.shift(); task !== undefined; task = tasks.shift()) task();
279+
};
280+
const selectedThreadId = ThreadId.make("another-selected-thread");
281+
const initialSnapshot: OrchestrationV2ShellSnapshot = {
282+
...v2ShellSnapshot,
283+
threads: [
284+
{ ...v2ThreadShell, id: threadId },
285+
{ ...v2ThreadShell, id: selectedThreadId },
286+
],
287+
};
288+
const shellStateAtom = Atom.make<EnvironmentShellState>({
289+
snapshot: Option.some(initialSnapshot),
290+
status: initialStatus,
291+
error: Option.none(),
292+
});
293+
const shellResultAtom = Atom.map(shellStateAtom, AsyncResult.success);
294+
const snapshotAtom = createEnvironmentSnapshotAtom(() => shellResultAtom)(environmentId);
295+
const catalogValueAtom = Atom.make({
296+
isReady: true,
297+
entries: new Map([
298+
[
299+
environmentId,
300+
{
301+
target: new PrimaryConnectionTarget({
302+
environmentId,
303+
label: "Environment",
304+
httpBaseUrl: "https://example.test",
305+
wsBaseUrl: "wss://example.test",
306+
}),
307+
profile: Option.none(),
308+
enabled: true,
309+
},
310+
],
311+
]),
312+
});
313+
const shells = createEnvironmentThreadShellAtoms({
314+
catalogValueAtom,
315+
snapshotAtom: () => snapshotAtom,
316+
});
317+
const selectedShell = shells.threadShellAtom({ environmentId, threadId: selectedThreadId });
318+
onTestFinished(registry.mount(selectedShell));
319+
onTestFinished(
320+
subscribeComposerDraftModelReconciliation({
321+
registry,
322+
threadShellAtom: shells.threadShellAtom,
323+
threadRefsAtom: shells.threadRefsAtom,
324+
shellStateAtom: () => shellResultAtom,
325+
}),
326+
);
327+
flush();
328+
return {
329+
flush,
330+
setStatus(status: EnvironmentShellStatus) {
331+
registry.set(shellStateAtom, { ...registry.get(shellStateAtom), status });
332+
flush();
333+
},
334+
update(changes: Partial<OrchestrationV2ThreadShell>, status?: EnvironmentShellStatus) {
335+
const state = registry.get(shellStateAtom);
336+
const snapshot = Option.getOrThrow(state.snapshot);
337+
registry.set(shellStateAtom, {
338+
...state,
339+
status: status ?? state.status,
340+
snapshot: Option.some(
341+
applyShellStreamEvent(snapshot, {
342+
kind: "thread.updated",
343+
location: "active",
344+
sequence: snapshot.snapshotSequence + 1,
345+
thread: { ...snapshot.threads[0]!, ...changes },
346+
}),
347+
),
348+
});
349+
flush();
350+
expect(registry.get(selectedShell)?.id).toBe(selectedThreadId);
351+
},
352+
model: () => registry.get(shells.threadShellAtom({ environmentId, threadId }))!.modelSelection,
353+
};
354+
}
355+
245356
describe("mobile composer drafts", () => {
246357
it("restores a PR checkout source after restarting the composer", () => {
247358
const draft: ComposerDraft = {
@@ -428,6 +539,114 @@ describe("mobile composer drafts", () => {
428539
expect(draft.text).toContain(String(record?.contextId));
429540
});
430541

542+
it("retains a pick matching cached state until the live shell confirms it", () => {
543+
const environmentId = EnvironmentId.make("environment-1");
544+
const threadId = ThreadId.make("thread-1");
545+
const key = `${environmentId}:${threadId}`;
546+
const h = modelReconciliationHarness(environmentId, threadId, "cached");
547+
const pick = h.model();
548+
updateComposerDraftSettings(key, { modelSelection: pick });
549+
h.flush();
550+
const before = getComposerDraftSnapshot(key);
551+
expect(before.modelSelection).toEqual(pick);
552+
553+
for (const status of ["empty", "synchronizing", "cached"] as const) {
554+
h.setStatus(status);
555+
expect(getComposerDraftSnapshot(key)).toEqual(before);
556+
}
557+
h.update({ modelSelection: { ...pick, model: "remote-model" } }, "live");
558+
expect(getComposerDraftSnapshot(key)).toEqual(before);
559+
h.update({ modelSelection: pick }, "synchronizing");
560+
expect(getComposerDraftSnapshot(key)).toEqual(before);
561+
h.setStatus("live");
562+
expect(appAtomRegistry.get(composerDraftsAtom)[key]).toBeUndefined();
563+
});
564+
565+
it("releases an inactive thread pick only after its queued model becomes durable", () => {
566+
const environmentId = EnvironmentId.make("environment-1");
567+
const threadId = ThreadId.make("thread-1");
568+
const key = `${environmentId}:${threadId}`;
569+
const pick = { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.5" };
570+
setComposerDraftText(key, "keep this unsent follow-up");
571+
appendComposerDraftAttachments(
572+
key,
573+
[
574+
{
575+
type: "file",
576+
id: "file",
577+
name: "notes.txt",
578+
mimeType: "text/plain",
579+
sizeBytes: 4,
580+
fileUri: "file:///notes.txt",
581+
},
582+
],
583+
{ appendReference: true },
584+
);
585+
updateComposerDraftSettings(key, {
586+
modelSelection: pick,
587+
runtimeMode: "approval-required",
588+
interactionMode: "plan",
589+
});
590+
const before = getComposerDraftSnapshot(key);
591+
const remoteKey = `environment-remote:${threadId}`;
592+
updateComposerDraftSettings(remoteKey, { modelSelection: pick });
593+
const remoteBefore = getComposerDraftSnapshot(remoteKey);
594+
const h = modelReconciliationHarness(environmentId, threadId);
595+
596+
h.update({ status: "queued" });
597+
expect(getComposerDraftSnapshot(key)).toEqual(before);
598+
h.update({
599+
status: "running",
600+
modelSelection: { ...pick, options: [{ id: "fastMode", value: false }] },
601+
});
602+
expect(getComposerDraftSnapshot(key)).toEqual({ ...before, modelSelection: undefined });
603+
expect(getComposerDraftSnapshot(remoteKey)).toEqual(remoteBefore);
604+
605+
h.update({
606+
modelSelection: {
607+
instanceId: ProviderInstanceId.make("claudeAgent"),
608+
model: "claude-opus-5-5",
609+
},
610+
});
611+
expect(getComposerDraftSnapshot(key).modelSelection ?? h.model()).toEqual(h.model());
612+
expect(getComposerDraftSnapshot(key)).toEqual({ ...before, modelSelection: undefined });
613+
});
614+
615+
it("retains an unavailable provider pick through differing durable model updates", () => {
616+
const environmentId = EnvironmentId.make("environment-1");
617+
const threadId = ThreadId.make("thread-1");
618+
const key = `${environmentId}:${threadId}`;
619+
const pick: ModelSelection = {
620+
instanceId: ProviderInstanceId.make("codex-secondary"),
621+
model: "gpt-5.5",
622+
options: [
623+
{ id: "reasoningEffort", value: "high" },
624+
{ id: "fastMode", value: true },
625+
],
626+
};
627+
updateComposerDraftSettings(key, { modelSelection: pick });
628+
const before = getComposerDraftSnapshot(key);
629+
const h = modelReconciliationHarness(environmentId, threadId);
630+
expect(getComposerDraftSnapshot(key)).toEqual(before);
631+
for (const modelSelection of [
632+
{ ...pick, instanceId: ProviderInstanceId.make("codex") },
633+
{ ...pick, model: "gpt-5.4" },
634+
{ ...pick, options: [{ id: "reasoningEffort", value: "high" }] },
635+
{
636+
...pick,
637+
options: [
638+
{ id: "reasoningEffort", value: "low" },
639+
{ id: "fastMode", value: true },
640+
],
641+
},
642+
]) {
643+
h.update({ modelSelection });
644+
expect(getComposerDraftSnapshot(key)).toEqual(before);
645+
}
646+
h.update({ modelSelection: { ...pick, options: pick.options!.toReversed() } });
647+
expect(appAtomRegistry.get(composerDraftsAtom)[key]).toBeUndefined();
648+
});
649+
431650
it("drops chips and records for attachments a replace no longer keeps", () => {
432651
const key = "new-task:draft-1";
433652
const kept = {

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

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,18 @@ import {
1818
type ProviderInteractionMode,
1919
type ProviderOptionSelection,
2020
type RuntimeMode,
21+
type ScopedThreadRef,
2122
} from "@t3tools/contracts";
2223
import * as Schema from "effect/Schema";
2324
import { useEffect } from "react";
24-
import { Atom } from "effect/reactivity";
25+
import { AsyncResult, Atom, type AtomRegistry } from "effect/reactivity";
26+
import * as Option from "effect/Option";
27+
import { parseScopedThreadKey, scopedThreadKey } from "@t3tools/client-runtime/environment";
28+
import type {
29+
EnvironmentShellState,
30+
EnvironmentThreadShell,
31+
} from "@t3tools/client-runtime/state/shell";
32+
import { draftModelPickMatchesThread } from "@t3tools/shared/model";
2533

2634
import { writeFileAtomically } from "../lib/atomic-file";
2735
import { createComposerContextHistory, referencedComposerContext } from "../lib/composerContext";
@@ -1460,6 +1468,71 @@ export function updateComposerDraftSettings(
14601468
});
14611469
}
14621470

1471+
export function subscribeComposerDraftModelReconciliation(input: {
1472+
readonly registry: AtomRegistry.AtomRegistry;
1473+
readonly threadRefsAtom: Atom.Atom<ReadonlyArray<ScopedThreadRef>>;
1474+
readonly threadShellAtom: (ref: ScopedThreadRef) => Atom.Atom<EnvironmentThreadShell | null>;
1475+
readonly shellStateAtom: (
1476+
environmentId: EnvironmentId,
1477+
) => Atom.Atom<AsyncResult.AsyncResult<EnvironmentShellState, unknown>>;
1478+
}): () => void {
1479+
const draftsAtom = Atom.make(appAtomRegistry.get(composerDraftsAtom));
1480+
const unsubscribeDrafts = appAtomRegistry.subscribe(composerDraftsAtom, (drafts) =>
1481+
input.registry.set(draftsAtom, drafts),
1482+
);
1483+
const threadKeysAtom = Atom.map(
1484+
input.threadRefsAtom,
1485+
(refs) => new Set(refs.map(scopedThreadKey)),
1486+
);
1487+
const matchesAtom = Atom.make((get) => {
1488+
const threadKeys = get(threadKeysAtom);
1489+
const matches: Array<readonly [string, ComposerDraft]> = [];
1490+
for (const [key, draft] of Object.entries(get(draftsAtom))) {
1491+
if (!draft.modelSelection || isNewTaskDraftKey(key) || isQueuedEditDraftKey(key)) continue;
1492+
const ref = parseScopedThreadKey(key);
1493+
if (!ref || !threadKeys.has(key)) continue;
1494+
const thread = get(input.threadShellAtom(ref));
1495+
if (!thread || !draftModelPickMatchesThread(draft.modelSelection, thread.modelSelection))
1496+
continue;
1497+
// Status and selection must come from the same emission; derived atoms can update apart.
1498+
const shell = Option.getOrNull(
1499+
AsyncResult.value(get(input.shellStateAtom(ref.environmentId))),
1500+
);
1501+
if (shell?.status !== "live") continue;
1502+
const authoritativeThread = Option.getOrNull(shell.snapshot)?.threads.find(
1503+
(candidate) => candidate.id === ref.threadId,
1504+
);
1505+
if (
1506+
authoritativeThread &&
1507+
draftModelPickMatchesThread(draft.modelSelection, authoritativeThread.modelSelection)
1508+
) {
1509+
matches.push([key, draft]);
1510+
}
1511+
}
1512+
return matches;
1513+
});
1514+
const unsubscribeMatches = input.registry.subscribe(
1515+
matchesAtom,
1516+
(matches) => {
1517+
if (matches.length === 0) return;
1518+
updateComposerDrafts((current) => {
1519+
let next = current;
1520+
for (const [key, matchedDraft] of matches) {
1521+
if (next[key] !== matchedDraft) continue;
1522+
const { modelSelection: _modelSelection, ...draft } = matchedDraft;
1523+
next = withComposerDraft(next, key, draft);
1524+
}
1525+
return next;
1526+
});
1527+
},
1528+
{ immediate: true },
1529+
);
1530+
return () => {
1531+
unsubscribeMatches();
1532+
unsubscribeDrafts();
1533+
};
1534+
}
1535+
14631536
export function clearComposerDraftContentState(
14641537
current: Record<string, ComposerDraft>,
14651538
draftKey: string,

0 commit comments

Comments
 (0)