From e0b3cfe666b4f902b290b585081dbc9168152aa2 Mon Sep 17 00:00:00 2001 From: oliver <97427849+flamboh@users.noreply.github.com> Date: Tue, 27 Oct 2026 19:48:16 -0700 Subject: [PATCH 01/46] fix(web): image and file chips underline their name on hover again (#26252) --- apps/web/src/components/contextChipParts.tsx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/apps/web/src/components/contextChipParts.tsx b/apps/web/src/components/contextChipParts.tsx index b510c1275ee84..a1112d037d908 100644 --- a/apps/web/src/components/contextChipParts.tsx +++ b/apps/web/src/components/contextChipParts.tsx @@ -167,7 +167,9 @@ export function ImageChipButton({ )} - {middleTruncateAttachmentName(name)} + + {middleTruncateAttachmentName(name)} + {size} {suffix ? {suffix} : null} @@ -314,7 +316,9 @@ function FileChipContent(props: { )} - {middleTruncateAttachmentName(props.name)} + + {middleTruncateAttachmentName(props.name)} + {props.size} {props.suffix ? {props.suffix} : null} From 2de4762e4b59a6ce5bde5efe0ca081ed600bb760 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 27 Oct 2026 19:49:57 -0700 Subject: [PATCH 02/46] fix(mobile): tab bar hides with the push, logo fits the iPad sidebar, Android rail holds still (#26241) Co-authored-by: Claude Opus 5.5 --- .../t3nativecontrols/T3WorkspaceNavigationModule.kt | 3 +++ apps/mobile/src/components/CompactBrandTitle.tsx | 5 +++++ .../layout/createWorkspaceStackNavigator.ios.tsx | 8 ++++++++ .../src/features/sharing/IncomingShareProvider.tsx | 5 +++++ .../src/features/workspace/WorkspaceRootScreen.tsx | 9 +++++---- 5 files changed, 26 insertions(+), 4 deletions(-) diff --git a/apps/mobile/modules/t3-native-controls/android/src/main/java/expo/modules/t3nativecontrols/T3WorkspaceNavigationModule.kt b/apps/mobile/modules/t3-native-controls/android/src/main/java/expo/modules/t3nativecontrols/T3WorkspaceNavigationModule.kt index 32969b1b71537..736f7cd3019f0 100644 --- a/apps/mobile/modules/t3-native-controls/android/src/main/java/expo/modules/t3nativecontrols/T3WorkspaceNavigationModule.kt +++ b/apps/mobile/modules/t3-native-controls/android/src/main/java/expo/modules/t3nativecontrols/T3WorkspaceNavigationModule.kt @@ -320,11 +320,14 @@ class T3WorkspaceNavigationView( ) } } + // Other tabs keep the button's slot so the destinations below never move between tabs. if (selectedTab == "threads") { newThreadButton(fraction) { expanded = false onNewThread(emptyMap()) } + } else { + Spacer(Modifier.padding(top = 12.dp).height(FAB_SIZE)) } Spacer(Modifier.weight(1f)) tabs.forEach { tab -> diff --git a/apps/mobile/src/components/CompactBrandTitle.tsx b/apps/mobile/src/components/CompactBrandTitle.tsx index e19bbaf209c50..7a251a2e57c16 100644 --- a/apps/mobile/src/components/CompactBrandTitle.tsx +++ b/apps/mobile/src/components/CompactBrandTitle.tsx @@ -10,6 +10,8 @@ import { IPAD_HOME_TITLE_OFFSET } from "../lib/layoutMetrics"; import { resolveMobileStageLabel } from "../lib/mobileBranding"; import { useAndroidControlSizing } from "./ui/useAndroidControlSizing"; +const BRAND_MAX_FONT_SCALE = 1.4; + export function brandTitleOffset(): number { if (Platform.OS !== "ios") return 0; return Platform.isPad && !NATIVE_WORKSPACE_COLUMNS_SUPPORTED ? IPAD_HOME_TITLE_OFFSET : 0; @@ -49,8 +51,10 @@ export function CompactBrandTitle( ]} > + {/* A logo beside a fixed-size wordmark: it may grow with Dynamic Type, not crowd the bar. */} @@ -67,6 +71,7 @@ export function CompactBrandTitle( > diff --git a/apps/mobile/src/features/layout/createWorkspaceStackNavigator.ios.tsx b/apps/mobile/src/features/layout/createWorkspaceStackNavigator.ios.tsx index 8103e34923154..e2697f4e4d238 100644 --- a/apps/mobile/src/features/layout/createWorkspaceStackNavigator.ios.tsx +++ b/apps/mobile/src/features/layout/createWorkspaceStackNavigator.ios.tsx @@ -490,6 +490,14 @@ function WorkspaceStackView(props: ViewProps & { readonly nativeColumns: boolean const presentation = props.descriptors[route.key]?.options.presentation; return presentation !== undefined && presentation !== "card"; }); + // The root's willDisappear reaches JS frames into the push, so hide the dock in the commit that + // pushes; the native events still own every later change, including back gestures. + const topDetailKey = projection.detail.at(-1)?.key; + const [pushedDetailKey, setPushedDetailKey] = useState(topDetailKey); + if (pushedDetailKey !== topDetailKey) { + setPushedDetailKey(topDetailKey); + if (topDetailKey !== undefined && primaryVisible) setPrimaryVisible(false); + } // Detail-only histories must stay single-stack: placeholder descriptors cannot own sidebar actions or setOptions. const primaryRoute = projection.primary; if (!primaryRoute) return ; diff --git a/apps/mobile/src/features/sharing/IncomingShareProvider.tsx b/apps/mobile/src/features/sharing/IncomingShareProvider.tsx index 6c03a4110da5b..b9c48bd68a1f7 100644 --- a/apps/mobile/src/features/sharing/IncomingShareProvider.tsx +++ b/apps/mobile/src/features/sharing/IncomingShareProvider.tsx @@ -229,6 +229,11 @@ function getIncomingShareSnapshot() { } function reportRefreshFailure(cause: unknown) { + console.warn( + "[incoming-share] refresh failed", + cause, + cause instanceof Error ? cause.cause : null, + ); const error = cause instanceof Error ? cause : new Error("Could not import shared content."); Dialog.alert("Could not import shared content", error.message, [ { text: "Dismiss", style: "cancel" }, diff --git a/apps/mobile/src/features/workspace/WorkspaceRootScreen.tsx b/apps/mobile/src/features/workspace/WorkspaceRootScreen.tsx index 171c6960ff7fa..fd382d1de0aec 100644 --- a/apps/mobile/src/features/workspace/WorkspaceRootScreen.tsx +++ b/apps/mobile/src/features/workspace/WorkspaceRootScreen.tsx @@ -62,7 +62,7 @@ function WorkspaceRootTitle(props: { return ( } + brand={} maxWidth={props.maxWidth} onPress={props.onOpenEnvironments} /> @@ -95,14 +95,15 @@ function WorkspaceRootContent() { const keyboardHeight = useKeyboardState((keyboard) => keyboard.height); const workspace = useWorkspaceTabs()!; const { state } = workspace; + // Settings is a workspace tab too; its list takes the place of the selected tab's. + const settingsOpen = workspace.settingsOpen; const renderHeaderTitle = () => ( openSettings(navigation, "SettingsEnvironments")} /> ); - // Settings is a workspace tab too; its list takes the place of the selected tab's. - const settingsOpen = workspace.settingsOpen; const tab = state.tabs[settingsOpen ? "settings" : state.selectedTab]; const bottomClearance = layout.primarySidebarOverlaysContent && state.selectedTab === "usage" From 91992bd5d464d6a01b2a5c1e7926e68ad09ea40e Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Tue, 27 Oct 2026 19:51:14 -0700 Subject: [PATCH 03/46] fix(web): update dialog shows one row per machine (#26210) Co-authored-by: Claude Opus 5.5 --- .../SidebarUpdatePill.browser.test.tsx | 5 +- .../components/sidebar/SidebarUpdatePill.tsx | 6 +- .../sidebar/UpdateEverythingDialog.tsx | 150 +++++++++++++++--- .../sidebar/updateEverything.logic.test.ts | 41 +++++ .../sidebar/updateEverything.logic.ts | 69 +++++--- .../components/sidebar/useUpdateEverything.ts | 8 +- 6 files changed, 229 insertions(+), 50 deletions(-) diff --git a/apps/web/src/components/sidebar/SidebarUpdatePill.browser.test.tsx b/apps/web/src/components/sidebar/SidebarUpdatePill.browser.test.tsx index c7b1ca448775c..714d1564bac0a 100644 --- a/apps/web/src/components/sidebar/SidebarUpdatePill.browser.test.tsx +++ b/apps/web/src/components/sidebar/SidebarUpdatePill.browser.test.tsx @@ -37,9 +37,10 @@ vi.mock("./useUpdateEverything", () => ({ useUpdateEverything: () => ({ hasUpdates: mocks.hasUpdates, summary: "Update everything", - lines: [], + version: null, + providerCount: 0, + rows: [], restarts: [], - skipped: [], }), })); diff --git a/apps/web/src/components/sidebar/SidebarUpdatePill.tsx b/apps/web/src/components/sidebar/SidebarUpdatePill.tsx index 89c992f8cc9ed..ecfeb4d664a3b 100644 --- a/apps/web/src/components/sidebar/SidebarUpdatePill.tsx +++ b/apps/web/src/components/sidebar/SidebarUpdatePill.tsx @@ -396,10 +396,10 @@ function SidebarUpdateControl() { void runEverything()} /> ); diff --git a/apps/web/src/components/sidebar/UpdateEverythingDialog.tsx b/apps/web/src/components/sidebar/UpdateEverythingDialog.tsx index e235047d36e1e..8cd5c5ca777d5 100644 --- a/apps/web/src/components/sidebar/UpdateEverythingDialog.tsx +++ b/apps/web/src/components/sidebar/UpdateEverythingDialog.tsx @@ -1,6 +1,8 @@ import { useAtomValue } from "@effect/atom-react"; import { EnvironmentId } from "@t3tools/contracts"; import { Atom } from "effect/reactivity"; +import { ChevronRightIcon } from "lucide-react"; +import { Fragment, type RefObject, useRef } from "react"; import { environmentThreadShells } from "~/state/threads"; import { @@ -13,7 +15,10 @@ import { AlertDialogTitle, } from "../ui/alert-dialog"; import { Button } from "../ui/button"; -import type { RestartingMachine, SkippedMachine } from "./updateEverything.logic"; +import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible"; +import { MiddleTruncate } from "../ui/middle-truncate"; +import { ScrollArea } from "../ui/scroll-area"; +import type { RestartingMachine, UpdateEverythingRow } from "./updateEverything.logic"; const isRunning = (status: string) => status === "preparing" || status === "starting" || status === "running"; @@ -37,35 +42,54 @@ const runningThreadCountAtom = Atom.family((environmentIds: string) => interface UpdateEverythingDialogProps { readonly open: boolean; readonly onOpenChange: (open: boolean) => void; - readonly summary: string; - readonly lines: ReadonlyArray; + readonly version: string | null; + readonly providerCount: number; + readonly rows: ReadonlyArray; readonly restarts: ReadonlyArray; - readonly skipped: ReadonlyArray; readonly onConfirm: () => void; } +type UpdateEverythingDialogContentProps = Omit< + UpdateEverythingDialogProps, + "open" | "onOpenChange" +> & { + readonly cancelRef: RefObject; +}; + export function UpdateEverythingDialog({ open, onOpenChange, ...content }: UpdateEverythingDialogProps) { + const cancelRef = useRef(null); return ( {/* Refocusing the trigger after a pointer close shows its focus ring and opens its tooltip. */} - closeType === "keyboard"}> - + closeType === "keyboard"} + > + ); } +const APP_LABELS = { + "this-app": "This app", + server: "Server", + "desktop-app": "Desktop app", +} as const; + function UpdateEverythingDialogContent({ - summary, - lines, + version, + providerCount, + rows, restarts, - skipped, onConfirm, -}: Omit) { + cancelRef, +}: UpdateEverythingDialogContentProps) { const stopped = useAtomValue( runningThreadCountAtom( restarts @@ -84,16 +108,96 @@ function UpdateEverythingDialogContent({ ); const running = stopped + continuing; const threads = `${running} running ${running === 1 ? "thread" : "threads"}`; + const restarting = new Set(restarts.map((machine) => machine.environmentId)); + const updating = rows.filter((row) => row.app !== null || row.providers.length > 0); + const skipped = rows.filter((row) => row.skipped !== null); return ( <> - - {summary}? - - {lines.join("\n")} + + + Update {updating.length} {updating.length === 1 ? "machine" : "machines"}? + + + {version !== null ? ( + <> + T3 Code {version} + + ) : null} + {version !== null && providerCount > 0 ? " · " : null} + {providerCount > 0 + ? `${providerCount} provider ${providerCount === 1 ? "update" : "updates"}` + : null} + + +
+ {/* Machine names truncate first. Provider lists wrap between names, never inside one. */} +
+
+ Machine + T3 Code + Providers + +
+ {updating.map((row) => ( +
+ + + + + {row.app !== null ? APP_LABELS[row.app] : null} + {row.app === null && row.skipped !== null ? ( + Skipped + ) : null} + {row.app === "this-app" ? ( + restarts last + ) : null} + + + {row.providers.map((provider, index) => ( + + {provider} + {index < row.providers.length - 1 ? ", " : null} + + ))} + + + {row.environmentId !== null && restarting.has(row.environmentId) ? ( + + ) : null} + +
+ ))} +
+ {skipped.length > 0 ? ( + + + + {skipped.length} skipped + + +
+ {skipped.map((row) => ( + + + + + {row.skipped} + + ))} +
+
+
+ ) : null} +
+
+ {running > 0 ? ( -

+

{stopped === 0 ? `${threads} will be interrupted and can resume after the restart.` : continuing === 0 @@ -101,16 +205,16 @@ function UpdateEverythingDialogContent({ : `${threads} will be interrupted. ${continuing} can resume after the restart.`}

) : null} - {skipped.length > 0 ? ( -

- {skipped.map((machine) => `Skips ${machine.label}: ${machine.reason}`).join("\n")} -

- ) : null} -
- - }>Cancel + }> + Cancel + ); } + +function RunningThreads({ environmentId }: { readonly environmentId: EnvironmentId }) { + const count = useAtomValue(runningThreadCountAtom(environmentId)); + return count > 0 ? {count} running : null; +} diff --git a/apps/web/src/components/sidebar/updateEverything.logic.test.ts b/apps/web/src/components/sidebar/updateEverything.logic.test.ts index 3b13cd30df86c..3739308f7b1c7 100644 --- a/apps/web/src/components/sidebar/updateEverything.logic.test.ts +++ b/apps/web/src/components/sidebar/updateEverything.logic.test.ts @@ -4,6 +4,7 @@ import { describe, expect, it } from "vite-plus/test"; import { collectRestartingMachines, collectServerUpdateTargets, + describeUpdateEverything, runUpdateEverything, type UpdateEverythingMachine, } from "./updateEverything.logic"; @@ -136,3 +137,43 @@ describe("runUpdateEverything", () => { expect(result.localReady).toBe(false); }); }); + +describe("describeUpdateEverything", () => { + it("gives each machine one row with its T3 Code and provider updates", () => { + const id = EnvironmentId.make; + const { rows } = describeUpdateEverything({ + localVersion: "1.1.0", + localMachines: [ + { environmentId: id("local"), label: "Mainbook" }, + { environmentId: id("wsl"), label: "Mainbook WSL" }, + ], + servers: [ + { + environmentId: id("npm"), + serverLabel: "bb-1", + selfUpdate: "respawn", + targetVersion: "1.1.0", + }, + ], + providerMachines: [ + { environmentId: id("npm"), label: "bb-1", providers: ["Codex", "Claude"] }, + { environmentId: id("local"), label: "Mainbook", providers: ["Codex"] }, + { environmentId: id("manual"), label: "alvin", providers: ["Claude"] }, + ], + skipped: [ + { environmentId: id("manual"), label: "alvin", reason: "needs a manual update" }, + { environmentId: id("offline"), label: "TheoRoom", reason: "not connected" }, + ], + }); + + expect( + rows.map(({ label, app, skipped, providers }) => [label, app, skipped, providers]), + ).toEqual([ + ["Mainbook", "this-app", null, ["Codex"]], + ["Mainbook WSL", "this-app", null, []], + ["bb-1", "server", null, ["Codex", "Claude"]], + ["alvin", null, "needs a manual update", ["Claude"]], + ["TheoRoom", null, "not connected", []], + ]); + }); +}); diff --git a/apps/web/src/components/sidebar/updateEverything.logic.ts b/apps/web/src/components/sidebar/updateEverything.logic.ts index ad103ff936f63..469e5a0faf674 100644 --- a/apps/web/src/components/sidebar/updateEverything.logic.ts +++ b/apps/web/src/components/sidebar/updateEverything.logic.ts @@ -351,6 +351,11 @@ export function getUpdateEverythingToastView( export interface UpdateEverythingPlan { readonly localVersion: string | null; + /** Machines whose servers this app hosts; this app's update restarts them. */ + readonly localMachines: ReadonlyArray<{ + readonly environmentId: EnvironmentId; + readonly label: string; + }>; readonly servers: ReadonlyArray< Pick >; @@ -359,6 +364,15 @@ export interface UpdateEverythingPlan { readonly label: string; readonly providers: ReadonlyArray; }>; + readonly skipped: ReadonlyArray; +} + +export interface UpdateEverythingRow { + readonly environmentId: EnvironmentId | null; + readonly label: string; + readonly app: "this-app" | "server" | "desktop-app" | null; + readonly skipped: string | null; + readonly providers: ReadonlyArray; } function countLabel(count: number, noun: string): string | null { @@ -370,9 +384,12 @@ function joinList(parts: ReadonlyArray): string { return `${parts.slice(0, -1).join(", ")}, and ${parts.at(-1)}`; } +/** One row per machine: this app's machines first, then servers, provider-only, and skipped. */ export function describeUpdateEverything(plan: UpdateEverythingPlan): { readonly summary: string; - readonly lines: ReadonlyArray; + readonly version: string | null; + readonly providerCount: number; + readonly rows: ReadonlyArray; } { const providerCount = plan.providerMachines.reduce( (count, machine) => count + machine.providers.length, @@ -386,30 +403,46 @@ export function describeUpdateEverything(plan: UpdateEverythingPlan): { ].filter((part) => part !== null), )}`; - const machines = new Map(); - const itemsFor = (environmentId: EnvironmentId, label: string) => { - const existing = machines.get(environmentId); - if (existing) return existing.items; - const items: string[] = []; - machines.set(environmentId, { label, items }); - return items; + const rows = new Map(); + const update = ( + environmentId: EnvironmentId | null, + label: string, + change: Partial, + ) => { + const row = rows.get(environmentId) ?? { + environmentId, + label, + app: null, + skipped: null, + providers: [], + }; + rows.set(environmentId, { ...row, ...change }); }; + if (plan.localVersion) { + if (plan.localMachines.length === 0) update(null, "This app", { app: "this-app" }); + for (const machine of plan.localMachines) { + update(machine.environmentId, machine.label, { app: "this-app" }); + } + } for (const server of plan.servers) { - itemsFor(server.environmentId, server.serverLabel).push( - server.selfUpdate === "desktop-managed" - ? "desktop app update" - : `server to ${server.targetVersion}`, - ); + update(server.environmentId, server.serverLabel, { + app: server.selfUpdate === "desktop-managed" ? "desktop-app" : "server", + }); } for (const machine of plan.providerMachines) { - itemsFor(machine.environmentId, machine.label).push(...machine.providers); + update(machine.environmentId, machine.label, { providers: machine.providers }); + } + for (const machine of plan.skipped) { + update(machine.environmentId, machine.label, { skipped: machine.reason }); } return { summary, - lines: [ - ...(plan.localVersion ? [`This app: ${plan.localVersion}, restarts last`] : []), - ...Array.from(machines.values(), ({ label, items }) => `${label}: ${items.join(", ")}`), - ], + version: + plan.localVersion ?? + plan.servers.find((server) => server.selfUpdate !== "desktop-managed")?.targetVersion ?? + null, + providerCount, + rows: Array.from(rows.values()), }; } diff --git a/apps/web/src/components/sidebar/useUpdateEverything.ts b/apps/web/src/components/sidebar/useUpdateEverything.ts index e9e5a9b78efc6..af38bbf5d4bc4 100644 --- a/apps/web/src/components/sidebar/useUpdateEverything.ts +++ b/apps/web/src/components/sidebar/useUpdateEverything.ts @@ -78,9 +78,11 @@ export function useUpdateEverything(localUpdateVersion: string | null) { const providers = useProviderUpdates(); const updateServer = useServerUpdate(); - const { summary, lines } = describeUpdateEverything({ + const view = describeUpdateEverything({ localVersion: localUpdateVersion, + localMachines: machines.filter((machine) => machine.hostedByThisApp), servers, + skipped, providerMachines: providers.machines.map((machine) => ({ environmentId: machine.environmentId, label: machine.label, @@ -92,10 +94,8 @@ export function useUpdateEverything(localUpdateVersion: string | null) { return { hasUpdates: servers.length > 0 || providers.machines.length > 0, - summary, - lines, + ...view, restarts, - skipped, run: async ({ downloadLocal, installLocal, From 7c6446b81a18afdd3898e1359b401d6f9763fffa Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 27 Oct 2026 19:57:51 -0700 Subject: [PATCH 04/46] chore(mobile): e2e review skips behaviour confirmed as intended The scroll-edge header blur, rows under the floating tab bar, Settings' own search and filter, unlabelled Settings groups, edge-clipped scrolling rows, and wrapping at the largest text size were reported on every run. Co-Authored-By: Claude Opus 5.5 --- scripts/mobile-e2e/review.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/mobile-e2e/review.ts b/scripts/mobile-e2e/review.ts index 148e8ef0b9588..9f134e767c279 100644 --- a/scripts/mobile-e2e/review.ts +++ b/scripts/mobile-e2e/review.ts @@ -41,6 +41,8 @@ export async function reviewPose(result: PoseResult, outputDirectory: string): P ``, `Report only real defects a user would notice: text clipped, truncated mid-word where it should wrap, or overlapping other content; controls overlapping, cut off, or under the status bar, notch, home indicator, or keyboard; content hidden behind navigation chrome; colours wrong for the ${pose.appearance} appearance or unreadable contrast; large unexplained empty areas or misaligned layout; error banners or red error screens.`, `Ignore: the fixture content itself, the status bar clock, a development-build badge, and anything that is plainly a deliberate design choice. Prefer no finding to a speculative one.`, + // Each of these has been confirmed as intended; the review kept reporting them. + `These are intended, never report them: content fading or showing through the header as it scrolls under it (the iOS scroll-edge blur); list rows scrolling under the floating glass tab bar; the search and filter buttons on the Settings header (search searches settings, the filter picks which environments settings apply to); Settings groups without a section header; chips or rows cut off at the edge of a list that scrolls; labels and rows wrapping onto more lines at the largest accessibility text size.`, `Use the exact step names above. Severity: high = broken or unusable, medium = clearly wrong, low = polish.`, ].join("\n"); const { stdout } = await run( From 36aea583bb65a04a62dea94074bb0ad137811df1 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 27 Oct 2026 20:10:02 -0700 Subject: [PATCH 05/46] fix(mobile): trust user-installed Android CA certificates (#25730) Co-authored-by: Claude Opus 5.5 --- apps/mobile/app.config.ts | 2 +- .../plugins/withAndroidCleartextTraffic.cjs | 18 ------ .../withAndroidNetworkSecurityConfig.cjs | 56 +++++++++++++++++++ docs/user/remote-access.md | 4 ++ 4 files changed, 61 insertions(+), 19 deletions(-) delete mode 100644 apps/mobile/plugins/withAndroidCleartextTraffic.cjs create mode 100644 apps/mobile/plugins/withAndroidNetworkSecurityConfig.cjs diff --git a/apps/mobile/app.config.ts b/apps/mobile/app.config.ts index 980da9c83c33a..6ea260eae4d9e 100644 --- a/apps/mobile/app.config.ts +++ b/apps/mobile/app.config.ts @@ -425,7 +425,7 @@ const config: ExpoConfig = { ["expo-sensors", { motionPermission: false }], // Mods run in reverse order; expo-widgets must create and wipe the target before assets are added. ...(!isIosPersonalTeamBuild ? ["./plugins/withWidgetLogoAsset.cjs", widgetsPlugin] : []), - "./plugins/withAndroidCleartextTraffic.cjs", + "./plugins/withAndroidNetworkSecurityConfig.cjs", "./plugins/withAndroidGradleHeap.cjs", "./plugins/withAndroidInputBackground.cjs", "./plugins/withAndroidModernPopupMenu.cjs", diff --git a/apps/mobile/plugins/withAndroidCleartextTraffic.cjs b/apps/mobile/plugins/withAndroidCleartextTraffic.cjs deleted file mode 100644 index f612f751e9151..0000000000000 --- a/apps/mobile/plugins/withAndroidCleartextTraffic.cjs +++ /dev/null @@ -1,18 +0,0 @@ -const { withAndroidManifest } = require("expo/config-plugins"); - -module.exports = function withAndroidCleartextTraffic(config) { - return withAndroidManifest(config, (nextConfig) => { - const application = nextConfig.modResults.manifest.application?.[0]; - - if (application == null) { - throw new Error( - "AndroidManifest.xml is missing the application element required for cleartext traffic configuration.", - ); - } - - application.$ ??= {}; - application.$["android:usesCleartextTraffic"] = "true"; - - return nextConfig; - }); -}; diff --git a/apps/mobile/plugins/withAndroidNetworkSecurityConfig.cjs b/apps/mobile/plugins/withAndroidNetworkSecurityConfig.cjs new file mode 100644 index 0000000000000..90fea189e8d01 --- /dev/null +++ b/apps/mobile/plugins/withAndroidNetworkSecurityConfig.cjs @@ -0,0 +1,56 @@ +const fs = require("node:fs"); +const path = require("node:path"); +const { withAndroidManifest, withDangerousMod } = require("expo/config-plugins"); + +// Environments behind a private CA installed in the user store must work like they do in Chrome. +// Android ignores usesCleartextTraffic once a network security config exists, so permit it here. +const NETWORK_SECURITY_CONFIG = ` + + + + + + + + +`; + +function withNetworkSecurityConfigManifest(config) { + return withAndroidManifest(config, (nextConfig) => { + const application = nextConfig.modResults.manifest.application?.[0]; + + if (application == null) { + throw new Error( + "AndroidManifest.xml is missing the application element required for network security configuration.", + ); + } + + application.$ ??= {}; + application.$["android:networkSecurityConfig"] = "@xml/network_security_config"; + + return nextConfig; + }); +} + +function withNetworkSecurityConfigResource(config) { + return withDangerousMod(config, [ + "android", + async (config) => { + const xmlDir = path.join( + config.modRequest.platformProjectRoot, + "app", + "src", + "main", + "res", + "xml", + ); + fs.mkdirSync(xmlDir, { recursive: true }); + fs.writeFileSync(path.join(xmlDir, "network_security_config.xml"), NETWORK_SECURITY_CONFIG); + return config; + }, + ]); +} + +module.exports = function withAndroidNetworkSecurityConfig(config) { + return withNetworkSecurityConfigResource(withNetworkSecurityConfigManifest(config)); +}; diff --git a/docs/user/remote-access.md b/docs/user/remote-access.md index 2f7a80187668d..d1bfe3710d276 100644 --- a/docs/user/remote-access.md +++ b/docs/user/remote-access.md @@ -62,6 +62,10 @@ On a computer with the desktop app, you can open the printed `t3code://pair` app link instead. The desktop app asks you to confirm the server address, then adds it. +If your HTTPS endpoint uses a certificate from your own certificate authority, +install that CA on Android as a **CA certificate** in its security settings; the +Android app then trusts it. + Pairing authorizes that device for future connections. Use a fresh one-time link for each new device; you do not need the original token to reconnect. Links created in Settings can only be copied from the client that created them while From 8947f3e3533d97cd91e4a02e9ae08ff1b2b07408 Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Tue, 27 Oct 2026 20:17:17 -0700 Subject: [PATCH 06/46] fix(web): server rows keep their source visible, cmd-click opens your browser (#26267) Co-authored-by: Claude Opus 5.5 --- .../components/chat/ThreadServersPanel.tsx | 53 ++++++++++++------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/apps/web/src/components/chat/ThreadServersPanel.tsx b/apps/web/src/components/chat/ThreadServersPanel.tsx index 9b7be42eccef7..353f6032b6f87 100644 --- a/apps/web/src/components/chat/ThreadServersPanel.tsx +++ b/apps/web/src/components/chat/ThreadServersPanel.tsx @@ -11,9 +11,11 @@ import { squashAtomCommandFailure, } from "@t3tools/client-runtime/state/runtime"; import { GlobeIcon, LoaderCircleIcon, SquareIcon } from "lucide-react"; -import { useState } from "react"; +import { type MouseEvent, useState } from "react"; -import { cn } from "../../lib/utils"; +import { resolveDiscoveredServerUrl } from "../../browser/browserTargetResolver"; +import { readLocalApi } from "../../localApi"; +import { cn, isMacPlatform } from "../../lib/utils"; import { useThreadDiscoveredPorts } from "../../portDiscoveryState"; import { useEnvironment } from "../../state/environments"; import { previewEnvironment } from "../../state/preview"; @@ -48,6 +50,7 @@ export function ThreadServersPanel(props: { const [stoppingKey, setStoppingKey] = useState(null); if (servers.length === 0) return null; + const externalClickLabel = isMacPlatform(navigator.platform) ? "⌘-click" : "Ctrl-click"; const threadRef = scopeThreadRef(props.environmentId, props.threadId); const reportFailure = (title: string, error: unknown) => { @@ -60,7 +63,17 @@ export function ThreadServersPanel(props: { ); }; - const open = async (server: DiscoveredLocalServer) => { + const open = async (server: DiscoveredLocalServer, event: MouseEvent) => { + if (event.button > 1) return; + // Like links: modified and middle clicks go to the system browser. + if (event.metaKey || event.ctrlKey || event.button === 1) { + event.preventDefault(); + const url = resolveDiscoveredServerUrl(props.environmentId, server.url); + await readLocalApi() + ?.shell.openExternal(url) + .catch((error: unknown) => reportFailure("Unable to open in browser", error)); + return; + } const result = await openDiscoveredPort({ threadRef, port: server, openPreview }); if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) { reportFailure("Unable to open preview", squashAtomCommandFailure(result)); @@ -87,7 +100,7 @@ export function ThreadServersPanel(props: { const key = serverKey(server); const stoppable = canStop && supportsStop && server.pid !== null; return ( -
  • +
  • void open(server)} + onClick={(event) => void open(server, event)} + onAuxClick={(event) => void open(server, event)} /> } > - - localhost:{server.port} - - {/* The stop button covers this while it shows. */} - - {[server.terminal === null ? "Agent" : "Terminal", server.processName] - .filter(Boolean) - .join(" · ")} + {/* The end padding keeps text clear of the stop button. */} + + + localhost:{server.port} + + + {[server.terminal === null ? "Agent" : "Terminal", server.processName] + .filter(Boolean) + .join(" · ")} + - Open in the browser panel + + Open in the browser panel. {externalClickLabel} for your browser. + {stoppable ? (
    From f120c51134c6b5f20b42f51cda84e3e6da681a5f Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 27 Oct 2026 20:19:34 -0700 Subject: [PATCH 07/46] fix(mobile): list bullets keep a gap before their text at every text size (#26268) Co-authored-by: Claude Opus 5.5 --- .../src/native/EnrichedMarkdownText.tsx | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/apps/mobile/src/native/EnrichedMarkdownText.tsx b/apps/mobile/src/native/EnrichedMarkdownText.tsx index 142fee0e61a75..13739f0f24a1a 100644 --- a/apps/mobile/src/native/EnrichedMarkdownText.tsx +++ b/apps/mobile/src/native/EnrichedMarkdownText.tsx @@ -12,7 +12,7 @@ import { markdownWithTableBreaks, } from "../lib/markdownDocument"; import { isValidElement, useMemo, useState, type ReactNode } from "react"; -import { Platform, Share, StyleSheet, View } from "react-native"; +import { Platform, Share, StyleSheet, useWindowDimensions, View } from "react-native"; import { EnrichedMarkdownText, type MarkdownImageBlock, @@ -35,6 +35,7 @@ import { function enrichedStyle( style: SelectableMarkdownTextProps["textStyle"], dark: boolean, + fontScale: number, ): MarkdownStyle { const monospace = Platform.OS === "ios" ? "Menlo" : "monospace"; // Keep the radius modest: iOS clips square table-cell borders without stroking the corner arcs. @@ -132,7 +133,13 @@ function enrichedStyle( borderWidth: 2, gapWidth: 12, }, - list: { ...body, bulletColor: style.mutedColor, markerColor: style.mutedColor, gapWidth: 6 }, + list: { + ...body, + bulletColor: style.mutedColor, + markerColor: style.mutedColor, + // The bullet is centred on the gap's edge and scales with the system text size; the gap must too. + gapWidth: 12 * fontScale, + }, thematicBreak: { color: style.dividerColor, height: 1, marginTop: 8, marginBottom: 8 }, table: { ...body, @@ -256,9 +263,13 @@ export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) { mimeTypes: { "app.t3.context-fragment": props.contextClipboardFragment! }, }; }, [props.contextClipboardFragment, props.markdown]); + const { fontScale } = useWindowDimensions(); const markdownStyle = useMemo( - () => ({ ...enrichedStyle(props.textStyle, themeAppearance === "dark"), linkVariants }), - [props.textStyle, themeAppearance, linkVariants], + () => ({ + ...enrichedStyle(props.textStyle, themeAppearance === "dark", fontScale), + linkVariants, + }), + [props.textStyle, themeAppearance, fontScale, linkVariants], ); const renderers = renderImage ? { From 949929dda5df97bc2084290fbe761c41b23665f7 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 27 Oct 2026 20:31:58 -0700 Subject: [PATCH 08/46] fix(mobile): Android split content keeps a gap at the screen edge (#26276) Co-authored-by: Claude Opus 5.5 --- apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx b/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx index a2f880ba724c7..c3b46cf8e24c2 100644 --- a/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx +++ b/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx @@ -626,8 +626,10 @@ function AdaptiveWorkspaceLayoutContent( workspaceInspector !== null && workspaceInspector.active && panes.auxiliaryPaneVisible ? (panes.auxiliaryPaneWidth ?? 0) : 0; + // Android's split content is a rounded card: keep it off the screen edge like the list card. + const contentEndGap = Platform.OS === "android" && layout.usesSplitView ? 4 : 0; const contentTargetWidth = layout.usesSplitView - ? Math.max(0, panes.contentPaneWidth - inspectorColumnTargetWidth) + ? Math.max(0, panes.contentPaneWidth - inspectorColumnTargetWidth - contentEndGap) : null; const [listPaneSizing, setListPaneSizing] = useState({ viewportWidth, @@ -936,6 +938,7 @@ function AdaptiveWorkspaceLayoutContent( flex: 1, width: contentTargetWidth, alignSelf: contentAnchor.end ? "flex-end" : "flex-start", + marginRight: contentAnchor.end ? contentEndGap : 0, } : { flex: 1 } } From c2fbf0a52454f2816012eae1fe7230cc69d69f2a Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 27 Oct 2026 20:39:25 -0700 Subject: [PATCH 09/46] feat(server,web,mobile): thread links name their environment and never a title (#26239) Co-authored-by: Claude Opus 5.5 --- .../features/threads/PendingUserInputCard.tsx | 11 +- .../features/threads/ThreadDetailScreen.tsx | 1 + .../src/features/threads/ThreadFeed.tsx | 32 ++-- .../src/lib/enrichedLinkPresentation.test.ts | 18 ++- .../src/lib/enrichedLinkPresentation.ts | 13 +- apps/mobile/src/state/entities.ts | 78 ++++++---- apps/server/src/mcp/OrchestratorMcpService.ts | 30 +++- .../src/mcp/toolkits/project/handlers.test.ts | 2 +- .../src/mcp/toolkits/project/handlers.ts | 2 +- .../src/mcp/toolkits/thread/handlers.ts | 6 +- apps/server/src/mcp/toolkits/thread/tools.ts | 1 + apps/server/src/peer/RemoteDelegation.test.ts | 5 + .../src/provider/agentContextBudget.json | 4 +- apps/web/src/components/ChatMarkdown.tsx | 12 +- .../components/chat/MarkdownThreadLink.tsx | 42 +++--- .../chat/timelineMinimapItems.test.ts | 16 ++ .../components/chat/timelineMinimapItems.ts | 5 +- apps/web/src/pendingUserInput.test.ts | 2 +- apps/web/src/pendingUserInput.ts | 2 +- .../orchestrator-mcp-server.md | 15 +- evals/mcp-tool-use/cases/orchestration.ndjson | 9 +- evals/mcp-tool-use/run.test.ts | 6 +- evals/mcp-tool-use/run.ts | 17 ++- .../src/server/orchestrationInstructions.ts | 2 +- packages/shared/src/markdownPipeline.test.ts | 14 +- packages/shared/src/threadLinks.test.ts | 137 ++++++++++++------ packages/shared/src/threadLinks.ts | 130 +++++++++++++---- 27 files changed, 427 insertions(+), 185 deletions(-) create mode 100644 apps/web/src/components/chat/timelineMinimapItems.test.ts diff --git a/apps/mobile/src/features/threads/PendingUserInputCard.tsx b/apps/mobile/src/features/threads/PendingUserInputCard.tsx index 9b6371813d207..d097416b2ef20 100644 --- a/apps/mobile/src/features/threads/PendingUserInputCard.tsx +++ b/apps/mobile/src/features/threads/PendingUserInputCard.tsx @@ -1,6 +1,6 @@ import { Button } from "../../components/ui/Button"; import { QuestionAttachments } from "./QuestionAttachments"; -import type { RuntimeRequestId } from "@t3tools/contracts"; +import type { EnvironmentId, RuntimeRequestId } from "@t3tools/contracts"; import type { ThreadUserInputQuestion } from "@t3tools/client-runtime/state/thread-requests"; import { useRef } from "react"; import { Platform, Pressable, ScrollView, View, type LayoutChangeEvent } from "react-native"; @@ -23,6 +23,7 @@ import { cn } from "../../lib/cn"; import { SelectableMarkdownText } from "../../native/SelectableMarkdownText"; import { useOpenPullRequest } from "../../native/useOpenPullRequest"; import { useMarkdownPreviewStyles } from "../files/FileMarkdownPreview"; +import { useLiveThreadLinkLabels } from "../../state/entities"; import { isPendingUserInputOptionSelected, type PendingUserInput, @@ -30,6 +31,7 @@ import { } from "../../lib/threadActivity"; export interface PendingUserInputCardProps { + readonly environmentId: EnvironmentId; readonly canOperateThread: boolean; readonly pendingUserInput: PendingUserInput; readonly maxHeight: DerivedValue; @@ -59,12 +61,13 @@ export interface PendingUserInputCardProps { /** Android cannot hit-test overlays beyond their parent's bounds; render its expanded card in-flow. */ const EXPANDED_CARD_IS_OVERLAY = Platform.OS === "ios"; -function QuestionText(props: { readonly markdown: string }) { +function QuestionText(props: { readonly markdown: string; readonly environmentId: EnvironmentId }) { const styles = useMarkdownPreviewStyles(); + const markdown = useLiveThreadLinkLabels(props.markdown, props.environmentId); const openLink = useOpenPullRequest(); return ( void openLink(href)} /> @@ -217,7 +220,7 @@ export function PendingUserInputCard(props: PendingUserInputCardProps) { {question.header} - + {question.options.map((option) => { const optionValue = option.value ?? option.label.trim(); diff --git a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx b/apps/mobile/src/features/threads/ThreadDetailScreen.tsx index 3500e6bb35ad9..a23f06abef24b 100644 --- a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx +++ b/apps/mobile/src/features/threads/ThreadDetailScreen.tsx @@ -1513,6 +1513,7 @@ function ThreadDetailBody(props: ThreadDetailScreenProps) { ) : null} {props.activePendingUserInput ? ( { - const linkedThreadId = parseThreadLinkHref(href); - if (!linkedThreadId) return null; - const source = scopeThreadRef(props.environmentId, linkedThreadId); - return ( - resolveThreadLinkRef(source, appAtomRegistry.get(environmentThreadShells.threadRefsAtom)) ?? - source + const link = parseThreadLinkHref(href); + if (!link) return null; + const resolved = resolveThreadLink( + (atom) => appAtomRegistry.get(atom), + link, + props.environmentId, ); + if (resolved !== null || link.environmentId !== null) return resolved; + // A v1 link names no environment, so it falls back to this message's. + return scopeThreadRef(props.environmentId, link.threadId); }; const onMarkdownLinkPress = (href: string, imageEmbed = false) => { markdownLinkRequest.current?.abort(); @@ -2273,12 +2277,14 @@ export const ThreadFeed = memo(function ThreadFeed({ if (instructionsPath) openSkillInstructions(instructionsPath); return; } - const target = resolveLinkedThread(href); - if (target) { - openLinkedThread({ - environmentId: String(target.environmentId), - threadId: String(target.threadId), - }); + if (parseThreadLinkHref(href)) { + const target = resolveLinkedThread(href); + if (target) { + openLinkedThread({ + environmentId: String(target.environmentId), + threadId: String(target.threadId), + }); + } return; } const media = resolveMarkdownMediaPreview(href, { diff --git a/apps/mobile/src/lib/enrichedLinkPresentation.test.ts b/apps/mobile/src/lib/enrichedLinkPresentation.test.ts index 89155b11a3cc3..4a3b0174ff67d 100644 --- a/apps/mobile/src/lib/enrichedLinkPresentation.test.ts +++ b/apps/mobile/src/lib/enrichedLinkPresentation.test.ts @@ -51,10 +51,12 @@ describe("Enriched link presentation", () => { }); it("presents thread links as thread chips and leaves other links alone", () => { - expect(enrichedThreadLinkPresentation("t3-thread://v1/thread%3Aproject%3A1")).toEqual({ - accent: "#009c96", - icon: { kind: "chip", name: "messages-square" }, - }); + for (const href of ["t3-thread://v1/thread%3Aproject%3A1", "t3-thread://v2/env/thread_1"]) { + expect(enrichedThreadLinkPresentation(href), href).toEqual({ + accent: "#009c96", + icon: { kind: "chip", name: "messages-square" }, + }); + } for (const href of [ "https://github.com/example/repo/pull/7", "https://example.com", @@ -70,7 +72,7 @@ describe("Enriched link presentation", () => { expect( threadChipTargets( here, - "See [A](t3-thread://v1/thread_a), [A again](t3-thread://v1/thread_a) and [B](t3-thread://v1/thread_b).", + "See [A](t3-thread://v1/thread_a), [A again](t3-thread://v1/thread_a), [B](t3-thread://v1/thread_b) and [D](t3-thread://v2/env-there/thread_d).", [ { version: 1, @@ -86,6 +88,12 @@ describe("Enriched link presentation", () => { ).toEqual([ { href: "t3-thread://v1/thread_a", environmentId: here, threadId: "thread_a", link: true }, { href: "t3-thread://v1/thread_b", environmentId: here, threadId: "thread_b", link: true }, + { + href: "t3-thread://v2/env-there/thread_d", + environmentId: there, + threadId: "thread_d", + link: true, + }, { href: "t3-context://v1/thread/thr1", environmentId: there, diff --git a/apps/mobile/src/lib/enrichedLinkPresentation.ts b/apps/mobile/src/lib/enrichedLinkPresentation.ts index b93841ad8f8e1..12859d2dc8bd8 100644 --- a/apps/mobile/src/lib/enrichedLinkPresentation.ts +++ b/apps/mobile/src/lib/enrichedLinkPresentation.ts @@ -58,9 +58,16 @@ export function threadChipTargets( ): ReadonlyArray { const targets = new Map(); if (hasThreadLinks(markdown)) { - for (const [, href = ""] of markdown.matchAll(/\]\((t3-thread:\/\/v1\/[^\s)]+)\)/g)) { - const threadId = parseThreadLinkHref(href); - if (threadId) targets.set(href, { href, environmentId, threadId, link: true }); + for (const [, href = ""] of markdown.matchAll(/\]\((t3-thread:\/\/v[12]\/[^\s)]+)\)/g)) { + const link = parseThreadLinkHref(href); + if (link) { + targets.set(href, { + href, + environmentId: link.environmentId ?? environmentId, + threadId: link.threadId, + link: true, + }); + } } } for (const record of records ?? []) { diff --git a/apps/mobile/src/state/entities.ts b/apps/mobile/src/state/entities.ts index 43910e0f14abc..c98c8a11488cf 100644 --- a/apps/mobile/src/state/entities.ts +++ b/apps/mobile/src/state/entities.ts @@ -17,9 +17,13 @@ import { } from "@t3tools/contracts"; import { scopeThreadRef } from "@t3tools/client-runtime/environment"; import { + guessThreadLinkRef, hasThreadLinks, + parseThreadLinkHref, relabelThreadLinks, - resolveThreadLinkRef, + threadLinkCandidates, + threadLinkRefs, + type ThreadLink, } from "@t3tools/shared/threadLinks"; import * as Option from "effect/Option"; import * as Schema from "effect/Schema"; @@ -45,26 +49,35 @@ const EMPTY_SERVER_CONFIG_ATOM = Atom.make(null).pipe( const EMPTY_THREAD_ATOM = Atom.make(null).pipe( Atom.withLabel("mobile-thread:empty"), ); -const EMPTY_THREAD_TITLES: ReadonlyMap = new Map(); -const EMPTY_THREAD_TITLES_ATOM = Atom.make(EMPTY_THREAD_TITLES).pipe( - Atom.withLabel("mobile-thread-titles:empty"), +const EMPTY_THREAD_LINK_TITLES_ATOM = Atom.make>(new Map()).pipe( + Atom.withLabel("mobile-thread-link-titles:empty"), ); -const threadTitlesAtom = Atom.family((environmentId: EnvironmentId) => { - let previous = EMPTY_THREAD_TITLES; +const decodeThreadKey = Schema.decodeUnknownSync(Schema.Tuple([EnvironmentId, ThreadId])); +const threadKey = (ref: ScopedThreadRef) => JSON.stringify([ref.environmentId, ref.threadId]); + +// Null when this client doesn't know the thread. Keyed, so a link never walks an environment's threads. +const threadTitleAtom = Atom.family((key: string) => { + const ref = scopeThreadRef(...decodeThreadKey(JSON.parse(key))); + return Atom.make((get) => get(environmentThreadShells.threadShellAtom(ref))?.title ?? null).pipe( + Atom.withLabel(`mobile-thread-title:${key}`), + ); +}); + +const decodeThreadKeys = Schema.decodeUnknownSync(Schema.Array(Schema.String)); + +const threadLinkTitlesAtom = Atom.family((key: string) => { + const refKeys = decodeThreadKeys(JSON.parse(key)); return Atom.make((get) => { - const index = get(environmentThreadShells.environmentThreadIndexAtom(environmentId)); - const unchanged = - index.size === previous.size && - Array.from(index).every(([threadId, shell]) => previous.get(threadId) === shell.title); - if (!unchanged) { - previous = new Map(Array.from(index, ([threadId, shell]) => [threadId, shell.title])); + const titles = new Map(); + for (const refKey of refKeys) { + const title = get(threadTitleAtom(refKey)); + if (title !== null) titles.set(refKey, title); } - return previous; - }).pipe(Atom.withLabel(`mobile-thread-titles:${environmentId}`)); + return titles; + }).pipe(Atom.withLabel(`mobile-thread-link-titles:${key}`)); }); -const decodeThreadKey = Schema.decodeUnknownSync(Schema.Tuple([EnvironmentId, ThreadId])); const decodeThreadChipTargets = Schema.decodeUnknownSync( Schema.Array( Schema.Struct({ @@ -118,13 +131,11 @@ const threadChipIconsAtom = Atom.family((key: string) => { return Atom.make((get) => { const icons = new Map(); for (const target of targets) { - const source = scopeThreadRef(target.environmentId, target.threadId); - const ref = target.link - ? (resolveThreadLinkRef(source, get(environmentThreadShells.threadRefsAtom)) ?? source) - : source; - const icon = get( - threadProjectChipIconAtom(JSON.stringify([ref.environmentId, ref.threadId])), - ); + const link = target.link ? parseThreadLinkHref(target.href) : null; + const ref = + (link ? resolveThreadLink(get, link, target.environmentId) : null) ?? + scopeThreadRef(target.environmentId, target.threadId); + const icon = get(threadProjectChipIconAtom(threadKey(ref))); if (icon) icons.set(target.href, icon); } return icons; @@ -190,11 +201,26 @@ export function useChildThreadInputs(ref: ScopedThreadRef) { export function useLiveThreadLinkLabels(markdown: string, environmentId: EnvironmentId): string { const titles = useAtomValue( - hasThreadLinks(markdown) ? threadTitlesAtom(environmentId) : EMPTY_THREAD_TITLES_ATOM, + hasThreadLinks(markdown) + ? threadLinkTitlesAtom(JSON.stringify(threadLinkRefs(markdown, environmentId).map(threadKey))) + : EMPTY_THREAD_LINK_TITLES_ATOM, + ); + return relabelThreadLinks(markdown, environmentId, (ref) => titles.get(threadKey(ref))); +} + +/** Keyed reads; every thread is read only to place a v1 link its message's environment lacks. */ +export function resolveThreadLink( + read: (atom: Atom.Atom) => A, + link: ThreadLink, + messageEnvironmentId: EnvironmentId, +): ScopedThreadRef | null { + const known = threadLinkCandidates(link, messageEnvironmentId).find( + (ref) => read(threadTitleAtom(threadKey(ref))) !== null, ); - return titles.size === 0 - ? markdown - : relabelThreadLinks(markdown, (threadId) => titles.get(threadId)); + if (known) return known; + return link.environmentId === null + ? guessThreadLinkRef(link, read(environmentThreadShells.threadRefsAtom)) + : null; } export function useThreadChipIcons( diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.ts index bb66e208c971e..3681c45789093 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.ts @@ -1,5 +1,6 @@ import { CommandId, + type EnvironmentId, type RunId, isProviderAvailable, MessageId, @@ -748,13 +749,14 @@ function threadPullRequests( } function listItemFromShell( + environmentId: EnvironmentId, shell: OrchestrationV2ThreadShell, nowMs: number, ): OrchestratorMcpThreadListItem { return { threadId: shell.id, title: shell.title, - link: formatThreadLink(shell.id, shell.title), + link: formatThreadLink({ environmentId, threadId: shell.id }), createdBy: shell.createdBy, creationSource: shell.creationSource, status: shell.activityRunStatus ?? shell.status, @@ -776,6 +778,7 @@ function listItemFromShell( } function threadDetail( + environmentId: EnvironmentId, projection: Pick, itemCount: number, shell: OrchestrationV2ThreadShell, @@ -787,7 +790,7 @@ function threadDetail( threadId: projection.thread.id, projectId: projection.thread.projectId, title: projection.thread.title, - link: formatThreadLink(projection.thread.id, projection.thread.title), + link: formatThreadLink({ environmentId, threadId: projection.thread.id }), createdBy: projection.thread.createdBy, creationSource: projection.thread.creationSource, status: active?.status ?? latest?.status ?? "idle", @@ -1518,7 +1521,10 @@ const make = Effect.gen(function* () { const result = { taskId: task.id, childThreadId: task.childThreadId, - link: formatThreadLink(task.childThreadId, childControls.thread.title), + link: formatThreadLink({ + environmentId: scope.environmentId, + threadId: task.childThreadId, + }), childRunId: childRun?.id ?? null, childNodeId: task.id, status, @@ -1674,7 +1680,9 @@ const make = Effect.gen(function* () { return { taskId: task.id, childThreadId: null, - ...(task.remoteChild === undefined ? {} : { remoteChild: task.remoteChild }), + ...(task.remoteChild === undefined + ? {} + : { remoteChild: task.remoteChild, link: formatThreadLink(task.remoteChild) }), childRunId: null, childNodeId: task.id, status, @@ -2739,7 +2747,7 @@ const make = Effect.gen(function* () { ); return { threadId, - link: formatThreadLink(threadId, projection.thread.title), + link: formatThreadLink({ environmentId: scope.environmentId, threadId }), runId: run?.id ?? null, status: run?.status ?? "idle", title: projection.thread.title, @@ -2792,7 +2800,7 @@ const make = Effect.gen(function* () { return { projectId, currentThreadId: parent?.thread.id ?? null, - threads: page.map((shell) => listItemFromShell(shell, nowMs)), + threads: page.map((shell) => listItemFromShell(scope.environmentId, shell, nowMs)), nextCursor, total: filtered.length, } satisfies OrchestratorMcpThreadListResult; @@ -2872,7 +2880,13 @@ const make = Effect.gen(function* () { } } return { - thread: threadDetail(target, timeline.totalItems, shell, yield* Clock.currentTimeMillis), + thread: threadDetail( + scope.environmentId, + target, + timeline.totalItems, + shell, + yield* Clock.currentTimeMillis, + ), recentRuns: target.runs .toSorted((left, right) => right.ordinal - left.ordinal) .slice(0, input.runLimit ?? DEFAULT_THREAD_RUN_LIMIT) @@ -2931,7 +2945,7 @@ const make = Effect.gen(function* () { ); return { threadId: input.threadId, - link: formatThreadLink(input.threadId, target.thread.title), + link: formatThreadLink({ environmentId: scope.environmentId, threadId: input.threadId }), messageId, runId: result.run.id, ...(result.run.delegatedTaskId === undefined ? {} : { taskId: result.run.delegatedTaskId }), diff --git a/apps/server/src/mcp/toolkits/project/handlers.test.ts b/apps/server/src/mcp/toolkits/project/handlers.test.ts index 3e9fe6a21f93c..4031654a8c826 100644 --- a/apps/server/src/mcp/toolkits/project/handlers.test.ts +++ b/apps/server/src/mcp/toolkits/project/handlers.test.ts @@ -185,7 +185,7 @@ it.effect( expect(belowCeiling.result).toMatchObject({ projectId: belowCeiling.projectId, modelSelection: belowCeiling.modelSelection, - link: `[Fix](t3-thread://v1/${belowCeiling.launched[0]?.threadId})`, + link: ``, }); expect(belowCeiling.launched[0]?.runtimeMode).toBe("approval-required"); expect(belowCeiling.launched[0]?.initialMessage?.senderThreadId).toBeUndefined(); diff --git a/apps/server/src/mcp/toolkits/project/handlers.ts b/apps/server/src/mcp/toolkits/project/handlers.ts index f5a67ae025a85..950cb38c6c4fd 100644 --- a/apps/server/src/mcp/toolkits/project/handlers.ts +++ b/apps/server/src/mcp/toolkits/project/handlers.ts @@ -240,7 +240,7 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, { const run = result.projection.runs.find((run) => run.userMessageId === messageId); return { threadId: thread.id, - link: formatThreadLink(thread.id, thread.title), + link: formatThreadLink({ environmentId: scope.environmentId, threadId: thread.id }), projectId: thread.projectId, modelSelection: thread.modelSelection, runId: run?.id ?? null, diff --git a/apps/server/src/mcp/toolkits/thread/handlers.ts b/apps/server/src/mcp/toolkits/thread/handlers.ts index 8d7d1359e8c9e..8ad9afa435193 100644 --- a/apps/server/src/mcp/toolkits/thread/handlers.ts +++ b/apps/server/src/mcp/toolkits/thread/handlers.ts @@ -11,6 +11,7 @@ import * as Effect from "effect/Effect"; import { modelSelectionCommandType } from "@t3tools/shared/model"; import { formatThreadLink } from "@t3tools/shared/threadLinks"; +import * as McpInvocationContext from "../../McpInvocationContext.ts"; import * as McpToolAccess from "../../McpToolAccess.ts"; import { dispatchFailure, @@ -103,9 +104,11 @@ export const layer = McpToolAccess.toLayer(ThreadToolkit, { const { task, threadId } = yield* scheduler .runNow({ id: input.taskId }) .pipe(Effect.mapError(unavailable)); + const { environmentId } = yield* McpInvocationContext.McpInvocationContext; return { taskId: task.id, threadId, + ...(threadId === null ? {} : { link: formatThreadLink({ environmentId, threadId }) }), lastRunStatus: task.lastRunStatus, runCount: task.runCount, nextRunAt: task.nextRunAt, @@ -115,6 +118,7 @@ export const layer = McpToolAccess.toLayer(ThreadToolkit, { t3_thread_search: McpToolAccess.reads((input) => Effect.gen(function* () { const { caller } = yield* readCaller(); + const { environmentId } = yield* McpInvocationContext.McpInvocationContext; const threadSearch = yield* ThreadSearch.ThreadSearch; const { matches } = yield* threadSearch .searchThreads({ ...input, projectId: input.projectId ?? caller?.projectId }) @@ -122,7 +126,7 @@ export const layer = McpToolAccess.toLayer(ThreadToolkit, { return { matches: matches.map((match) => ({ ...match, - link: formatThreadLink(match.threadId, match.title), + link: formatThreadLink({ environmentId, threadId: match.threadId }), })), }; }), diff --git a/apps/server/src/mcp/toolkits/thread/tools.ts b/apps/server/src/mcp/toolkits/thread/tools.ts index b14b1bd387f1a..4b566c44a69d1 100644 --- a/apps/server/src/mcp/toolkits/thread/tools.ts +++ b/apps/server/src/mcp/toolkits/thread/tools.ts @@ -226,6 +226,7 @@ const ScheduledTaskRunTool = Tool.make("run_scheduled_task_now", { success: Schema.Struct({ taskId: ScheduledTaskId, threadId: ScheduledTask.fields.threadId, + link: Schema.optional(Schema.String), lastRunStatus: ScheduledTask.fields.lastRunStatus, runCount: NonNegativeInt, nextRunAt: ScheduledTask.fields.nextRunAt, diff --git a/apps/server/src/peer/RemoteDelegation.test.ts b/apps/server/src/peer/RemoteDelegation.test.ts index 73c547546d1d6..a344f63c523ab 100644 --- a/apps/server/src/peer/RemoteDelegation.test.ts +++ b/apps/server/src/peer/RemoteDelegation.test.ts @@ -759,6 +759,7 @@ const delegateToBox = (laptopEnv: Effect.Success>) status: string; childThreadId: null; remoteChild: { environmentId: string; threadId: ThreadId; label: string }; + link?: string; }; }), ); @@ -807,6 +808,10 @@ it.effect("a task delegated to the box wakes the laptop's parent when it ends th }); // The box launched it in the project with the same repository. expect(yield* Ref.get(thread.launched)).toBe(delegated.remoteChild.threadId); + // Its link opens the thread there, so the parent can mention it like any other. + expect(delegated.link).toBe( + ``, + ); // ...naming the parent here, so the thread there links back to it. expect(yield* Ref.get(thread.delegatedFrom)).toEqual({ environmentId: laptop.environmentId, diff --git a/apps/server/src/provider/agentContextBudget.json b/apps/server/src/provider/agentContextBudget.json index beec65c80d123..fdc39bc78bc0a 100644 --- a/apps/server/src/provider/agentContextBudget.json +++ b/apps/server/src/provider/agentContextBudget.json @@ -1,5 +1,5 @@ { - "claudeSystemAppendBytes": 5258, - "codexAdditionalContextBytes": 5810, + "claudeSystemAppendBytes": 5135, + "codexAdditionalContextBytes": 5687, "mcpToolListJsonBytes": 45861 } diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx index fc0489c016516..2ae3e8d9f971a 100644 --- a/apps/web/src/components/ChatMarkdown.tsx +++ b/apps/web/src/components/ChatMarkdown.tsx @@ -1060,13 +1060,15 @@ const CHAT_MARKDOWN_COMPONENTS = { const text = use(ChatMarkdownTextContext); const citation = href ? parseAssistantCitationHref(href) : null; if (citation) return ; - const linkedThreadId = href ? parseThreadLinkHref(href) : null; - if (linkedThreadId) { - const label = hastPlainTextDeep(node) || linkedThreadId; + const threadLink = href ? parseThreadLinkHref(href) : null; + if (threadLink) { + const written = hastPlainTextDeep(node); + // An autolink's text is its own URL, which is no label. + const label = written && parseThreadLinkHref(written) === null ? written : null; return environmentId ? ( - + ) : ( - {label} + {label ?? "Thread"} ); } const contextReference = href ? parseComposerContextHref(href) : null; diff --git a/apps/web/src/components/chat/MarkdownThreadLink.tsx b/apps/web/src/components/chat/MarkdownThreadLink.tsx index 8b32a541223d2..68ab4cd569e0d 100644 --- a/apps/web/src/components/chat/MarkdownThreadLink.tsx +++ b/apps/web/src/components/chat/MarkdownThreadLink.tsx @@ -1,39 +1,41 @@ import { scopeThreadRef } from "@t3tools/client-runtime/environment"; -import type { EnvironmentId, ThreadId } from "@t3tools/contracts"; +import type { EnvironmentId } from "@t3tools/contracts"; import { formatThreadLink, + guessThreadLinkRef, percentDecodedThreadLinkId, - resolveThreadLinkRef, + type ThreadLink, + unknownThreadLinkLabel, } from "@t3tools/shared/threadLinks"; import { useThreadRefs, useThreadShell } from "../../state/entities"; +import { UnresolvedChip } from "../contextChipParts"; import { ThreadContextChip } from "../ThreadContextChip"; +/** `label` is the link's written text; an autolink has none. */ export function MarkdownThreadLink(props: { readonly environmentId: EnvironmentId; - readonly threadId: ThreadId; - readonly label: string; + readonly link: ThreadLink; + readonly label: string | null; }) { - const decodedId = percentDecodedThreadLinkId(props.threadId); - const written = useThreadShell(scopeThreadRef(props.environmentId, props.threadId)); + const environmentId = props.link.environmentId ?? props.environmentId; + const decodedId = percentDecodedThreadLinkId(props.link.threadId); + const written = useThreadShell(scopeThreadRef(environmentId, props.link.threadId)); const decoded = useThreadShell( - written === null && decodedId !== null ? scopeThreadRef(props.environmentId, decodedId) : null, + written === null && decodedId !== null ? scopeThreadRef(environmentId, decodedId) : null, ); const local = written ?? decoded; - const refs = useThreadRefs(local === null); - const remote = useThreadShell( - local === null - ? resolveThreadLinkRef(scopeThreadRef(props.environmentId, props.threadId), refs) - : null, - ); + const guess = local === null && props.link.environmentId === null; + const refs = useThreadRefs(guess); + const remote = useThreadShell(guess ? guessThreadLinkRef(props.link, refs) : null); const thread = local ?? remote; - const environmentId = thread?.environmentId ?? props.environmentId; - const threadId = thread?.id ?? props.threadId; - const title = thread?.title.trim() || props.label; + if (thread === null) { + const unknown = unknownThreadLinkLabel(props.link, props.environmentId); + return ; + } + const ref = { environmentId: thread.environmentId, threadId: thread.id }; + const title = thread.title.trim() || props.label || "Untitled thread"; return ( - + ); } diff --git a/apps/web/src/components/chat/timelineMinimapItems.test.ts b/apps/web/src/components/chat/timelineMinimapItems.test.ts new file mode 100644 index 0000000000000..20d4c75997b1d --- /dev/null +++ b/apps/web/src/components/chat/timelineMinimapItems.test.ts @@ -0,0 +1,16 @@ +import { describe, expect, it } from "vite-plus/test"; + +import { resolveTimelineMinimapPreview } from "./timelineMinimapItems"; + +describe("resolveTimelineMinimapPreview", () => { + it("previews a thread autolink as prose, not its URL", () => { + const preview = resolveTimelineMinimapPreview({ + id: "turn-1", + rowIndex: 0, + userText: null, + assistantText: + "Started and [Fix CI](t3-thread://v2/env-1/t2).", + }); + expect(preview?.assistantText).toBe("Started a thread and Fix CI."); + }); +}); diff --git a/apps/web/src/components/chat/timelineMinimapItems.ts b/apps/web/src/components/chat/timelineMinimapItems.ts index faf6af9415405..099c58379ccf6 100644 --- a/apps/web/src/components/chat/timelineMinimapItems.ts +++ b/apps/web/src/components/chat/timelineMinimapItems.ts @@ -1,3 +1,4 @@ +import { parseThreadLinkHref } from "@t3tools/shared/threadLinks"; import type { Nodes } from "mdast"; import remarkGfm from "remark-gfm"; import remarkParse from "remark-parse"; @@ -82,7 +83,9 @@ function markdownNodeText(node: Nodes): string { if (node.type === "image" || node.type === "imageReference") return node.alt ?? ""; if (!("children" in node)) return ""; const separator = INLINE_PARENT_TYPES.has(node.type) ? "" : " "; - return node.children.map(markdownNodeText).join(separator); + const text = node.children.map(markdownNodeText).join(separator); + // A thread autolink's only text is its URL. + return node.type === "link" && parseThreadLinkHref(text) !== null ? "a thread" : text; } function compactMinimapPreview(text: string | null | undefined) { diff --git a/apps/web/src/pendingUserInput.test.ts b/apps/web/src/pendingUserInput.test.ts index 521a249b00048..69800be4b7574 100644 --- a/apps/web/src/pendingUserInput.test.ts +++ b/apps/web/src/pendingUserInput.test.ts @@ -27,7 +27,7 @@ describe("projectPendingUserInputAnswer", () => { it("sends thread chips as readable links with the actual thread ID", () => { const answer = `Compare ${formatInlineContextReference(threadContextReference(record))}`; expect(projectPendingUserInputAnswer(answer, [record])).toBe( - "Compare [Reference work](t3-thread://v1/reference-thread)", + "Compare [Reference work](t3-thread://v2/env-answer/reference-thread)", ); }); diff --git a/apps/web/src/pendingUserInput.ts b/apps/web/src/pendingUserInput.ts index 55817c3378bda..f8f9d6d52b5ba 100644 --- a/apps/web/src/pendingUserInput.ts +++ b/apps/web/src/pendingUserInput.ts @@ -12,7 +12,7 @@ export function projectPendingUserInputAnswer( const recordsById = new Map(threadContexts.map((record) => [record.contextId, record])); return replaceComposerContextReferences(answer, (reference) => { const record = reference.kind === "thread" ? recordsById.get(reference.contextId) : undefined; - return record ? formatThreadLink(record.threadId, reference.label) : reference.source; + return record ? formatThreadLink(record, reference.label) : reference.source; }); } diff --git a/docs/orchestration-v2/orchestrator-mcp-server.md b/docs/orchestration-v2/orchestrator-mcp-server.md index e41f42c8248a3..befaca4c077c8 100644 --- a/docs/orchestration-v2/orchestrator-mcp-server.md +++ b/docs/orchestration-v2/orchestrator-mcp-server.md @@ -390,12 +390,15 @@ and `creationSource: "mcp"`; provider output uses `creationSource: "provider"`. Actor and ingress are separate so agent-authored user-role messages remain distinguishable from human-authored messages. -Agents mention another thread as `[title](t3-thread://v1/)`. Thread -results carry this ready-made as `link`, so agents never format the id. The -link carries only the id. Clients first resolve it in the message's environment, -then use a unique match in another enabled environment. Ambiguous matches stay -unresolved. Web and desktop show the resolved thread’s current title. Mobile -relabels local matches; cross-environment links can retain the written label. +Agents mention another thread by the `link` its result carries, +`/>`, so they never format an id or +write a title, which goes stale when the user renames the thread. The +environment that owns the thread builds the link, so forwarded results and tasks +delegated to a linked environment name that environment. Clients open only that +environment's thread and show its current title; a link to a thread they do not +know renders as a label that opens nothing. Older `t3-thread://v1/` +links name no environment: clients resolve them in the message's environment, +then by a unique match in another enabled environment. List and read results report `snoozed` and `snoozedUntil`, and `t3_thread_list` filters on `snoozed`. The server's `isSnoozed` follows the diff --git a/evals/mcp-tool-use/cases/orchestration.ndjson b/evals/mcp-tool-use/cases/orchestration.ndjson index fae66d64431ee..dc79eb774df7e 100644 --- a/evals/mcp-tool-use/cases/orchestration.ndjson +++ b/evals/mcp-tool-use/cases/orchestration.ndjson @@ -13,9 +13,10 @@ {"id": "debate-drops-second-model", "area": "orchestration", "kind": "bad", "pattern": "missed requested delegate; polling", "experience": "Asked to argue with GPT-6.1 Sol and Muse Wonder Full, the agent only delegated to Sol (twice), never mentioned Muse, and blocked the turn with 7 task_status and 3 t3_thread_wait calls.", "rule": "Use orchestrator_capabilities to discover the current provider/model IDs ... (intended: one delegate per requested model; end the turn after async delegates)", "privacy": "commit-safe", "setup": {"role": "top", "mocks": {"orchestrator_capabilities": {"parentThreadId": "33333333-3333-4333-8333-333333333333", "inheritedProviderInstanceId": "claudeAgent", "inheritedModel": "claude-sonnet-5-5", "inheritedOptions": [{"id": "effort", "value": "high"}], "runtimeMode": "full-access", "interactionMode": "default", "features": {"appOwnedSubagents": true, "asyncPolling": true, "cancellation": true, "batchThreadCreation": true, "threadManagement": true, "incrementalThreadRead": true, "scheduledTasks": true}, "providers": [{"providerInstanceId": "claudeAgent", "driverKind": "claudeAgent", "displayName": "Claude", "models": [{"id": "claude-opus-5-5", "label": "Claude Opus 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}, {"id": "fastMode", "type": "boolean", "default": false}]}, {"id": "claude-sonnet-5-5", "label": "Claude Sonnet 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}]}, {"id": "claude-haiku-5-5", "label": "Claude Haiku 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}]}]}, {"providerInstanceId": "codex", "driverKind": "codex", "displayName": "Codex", "models": [{"id": "gpt-6.1-sol", "label": "GPT-6.1 Sol", "options": [{"id": "reasoningEffort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}, {"id": "serviceTier", "values": ["default", "priority"], "default": "default"}]}]}, {"providerInstanceId": "muse", "driverKind": "muse", "displayName": "Muse Code", "runtimeModes": ["approval-required", "full-access"], "models": [{"id": "muse-wonder-full", "label": "Muse Wonder Full", "options": [{"id": "reasoningEffort", "values": ["minimal", "low", "medium", "high", "xhigh", "max"], "default": "high"}]}]}, {"providerInstanceId": "grok", "driverKind": "grok", "displayName": "Grok", "models": [{"id": "grok-4.7", "label": "Grok 4.7", "options": []}]}]}, "delegate_task": "default", "task_status": "default", "t3_thread_wait": {"threadId": "44444444-4444-4444-8444-000000000001", "status": "running", "timedOut": true}, "WebSearch": "Hostco status page: Major outage. Deploys and new builds are unavailable in all regions. Started 17 hours ago. Running apps and databases are not affected. Next update in 60 minutes."}, "world": {"files": {"infra/README.md": "# Hosting\n\nProduction runs on Hostco: the web service, a worker, and managed Postgres, all in us-east. The app needs the database in the same region for latency.\n", "infra/hostco.yaml": "services:\n - name: web\n plan: standard\n - name: worker\n plan: starter\ndatabases:\n - name: main\n plan: pro-4gb\n"}}}, "prompt": "Our hosting provider has been down for SEVENTEEN HOURS and I can't deploy. The DB has to stay colocated with compute, probably on the same platform. Rethink our options and research more. Argue with GPT-6.1 Sol and Muse Wonder Full about it. Don't bother me until you actually understand what is going on and have a real plan.", "expect": {"calls": [{"tool": "delegate_task", "args": {"target.model": "gpt-6.1-sol"}}, {"tool": "delegate_task", "args": {"target.model": "muse-wonder-full"}}], "order": ["orchestrator_capabilities", "delegate_task"], "maxCalls": {"t3_thread_wait": 0, "task_status": 1}}, "notes": "'Don't bother me until...' is the pressure toward blocking waits. Real model id replaced with the fixture 'muse-wonder-full'.", "maxSteps": 12} {"id": "file-watcher-after-async-delegates", "area": "orchestration", "kind": "bad", "pattern": "polling after async delegate_task (shell watcher)", "experience": "After async delegates, the agent ran background 'until [ -f file ]; do sleep 15; done' watchers. They caused empty wake turns ('That alert was the old plan-file watcher closing, so nothing changed').", "rule": "Keep polling/wait loops bounded, do not duplicate active work (intended: end the turn; completion wakes this thread)", "privacy": "commit-safe", "setup": {"role": "top", "mocks": {"orchestrator_capabilities": {"parentThreadId": "33333333-3333-4333-8333-333333333333", "inheritedProviderInstanceId": "claudeAgent", "inheritedModel": "claude-sonnet-5-5", "inheritedOptions": [{"id": "effort", "value": "high"}], "runtimeMode": "full-access", "interactionMode": "default", "features": {"appOwnedSubagents": true, "asyncPolling": true, "cancellation": true, "batchThreadCreation": true, "threadManagement": true, "incrementalThreadRead": true, "scheduledTasks": true}, "providers": [{"providerInstanceId": "claudeAgent", "driverKind": "claudeAgent", "displayName": "Claude", "models": [{"id": "claude-opus-5-5", "label": "Claude Opus 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}, {"id": "fastMode", "type": "boolean", "default": false}]}, {"id": "claude-sonnet-5-5", "label": "Claude Sonnet 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}]}, {"id": "claude-haiku-5-5", "label": "Claude Haiku 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}]}]}, {"providerInstanceId": "codex", "driverKind": "codex", "displayName": "Codex", "models": [{"id": "gpt-6.1-sol", "label": "GPT-6.1 Sol", "options": [{"id": "reasoningEffort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}, {"id": "serviceTier", "values": ["default", "priority"], "default": "default"}]}]}, {"providerInstanceId": "muse", "driverKind": "muse", "displayName": "Muse Code", "runtimeModes": ["approval-required", "full-access"], "models": [{"id": "muse-wonder-full", "label": "Muse Wonder Full", "options": [{"id": "reasoningEffort", "values": ["minimal", "low", "medium", "high", "xhigh", "max"], "default": "high"}]}]}, {"providerInstanceId": "grok", "driverKind": "grok", "displayName": "Grok", "models": [{"id": "grok-4.7", "label": "Grok 4.7", "options": []}]}]}, "delegate_task": "default", "task_status": "default"}, "world": {"files": {"infra/README.md": "# Services\n\nweb (Node), worker (queue consumer), Postgres, Redis. All on Hostco in one region.\n", "infra/web.yaml": "name: web\nenv:\n DATABASE_URL: from-db\n REDIS_URL: from-redis\n", "infra/worker.yaml": "name: worker\nenv:\n DATABASE_URL: from-db\n REDIS_URL: from-redis\n"}}}, "prompt": "We're starting a new repo that helps people move off our current host after today's outages. Do these, all with subagents:\n1. Inventory the services in infra/ and how they connect. Write .research/01-inventory.md.\n2. Survey the other hosting options. Write .research/02-alternatives.md.\n3. When 1 and 2 are done, propose a migration plan for the best two options in .research/03-plan.md.", "expect": {"notCalls": ["Monitor", "ScheduleWakeup"], "callsMustNot": [{"tool": "Agent", "args": {"run_in_background": true}}], "shellMustNot": ["\\bsleep\\b", "until \\[", "while \\[ ! -f", "inotifywait|fswatch"], "maxCalls": {"task_status": 1, "t3_thread_wait": 0}}, "notes": "Step 3 depends on 1 and 2, which is what pushed the agent to watch for files. Expected: start 1 and 2 as delegates, end the turn, start 3 on the wake."} {"id": "worktree-add-for-parallel-pr-delegates", "area": "orchestration", "kind": "bad", "pattern": "worktree via git worktree add instead of isolation", "experience": "Asked for delegates that each make a separate PR, the agent ran git worktree add 12 times in the shell and told each delegate to work in its path.", "rule": "For a child task that needs a separate checkout, use delegate_task with isolation:\"worktree\" ... Asking an agent to run git worktree add or cd in its prompt does not update T3's thread binding.", "privacy": "commit-safe", "setup": {"role": "top", "priorTurns": [{"role": "user", "text": "What's left before we can ship 1.0? Give me a short list of blockers."}, {"role": "assistant", "text": "Three release blockers:\n1. A deploy can drop a table without confirmation.\n2. Auth tokens are sent in WebSocket URLs.\n3. Admin sessions never expire.", "toolCalls": [{"tool": "Bash", "input": {"command": "rg -n \"dropTable\" src/deploy"}, "result": "src/deploy/migrate.ts:3: await db.dropTable(name); // no confirmation"}, {"tool": "Bash", "input": {"command": "rg -n \"token=\" src/ws"}, "result": "src/ws/client.ts:2: return new WebSocket(`${base}?token=${token}`);"}, {"tool": "Bash", "input": {"command": "rg -n \"expires\" src/admin"}, "result": "Exit code 1\n"}, {"tool": "Read", "input": {"file_path": "/home/user/.t3/worktrees/t3code/t3-f8cdd940/src/admin/session.ts"}, "result": " 1\texport function createAdminSession(userId: string) {\n 2\t return { userId, createdAt: Date.now() }; // no expiresAt\n 3\t}"}]}], "mocks": {"orchestrator_capabilities": {"parentThreadId": "33333333-3333-4333-8333-333333333333", "inheritedProviderInstanceId": "claudeAgent", "inheritedModel": "claude-sonnet-5-5", "inheritedOptions": [{"id": "effort", "value": "high"}], "runtimeMode": "full-access", "interactionMode": "default", "features": {"appOwnedSubagents": true, "asyncPolling": true, "cancellation": true, "batchThreadCreation": true, "threadManagement": true, "incrementalThreadRead": true, "scheduledTasks": true}, "providers": [{"providerInstanceId": "claudeAgent", "driverKind": "claudeAgent", "displayName": "Claude", "models": [{"id": "claude-opus-5-5", "label": "Claude Opus 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}, {"id": "fastMode", "type": "boolean", "default": false}]}, {"id": "claude-sonnet-5-5", "label": "Claude Sonnet 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}]}, {"id": "claude-haiku-5-5", "label": "Claude Haiku 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}]}]}, {"providerInstanceId": "codex", "driverKind": "codex", "displayName": "Codex", "models": [{"id": "gpt-6.1-sol", "label": "GPT-6.1 Sol", "options": [{"id": "reasoningEffort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}, {"id": "serviceTier", "values": ["default", "priority"], "default": "default"}]}]}, {"providerInstanceId": "muse", "driverKind": "muse", "displayName": "Muse Code", "runtimeModes": ["approval-required", "full-access"], "models": [{"id": "muse-wonder-full", "label": "Muse Wonder Full", "options": [{"id": "reasoningEffort", "values": ["minimal", "low", "medium", "high", "xhigh", "max"], "default": "high"}]}]}, {"providerInstanceId": "grok", "driverKind": "grok", "displayName": "Grok", "models": [{"id": "grok-4.7", "label": "Grok 4.7", "options": []}]}]}, "delegate_task": {"taskId": "task-1", "childThreadId": "44444444-4444-4444-8444-000000000001", "childRunId": "run-child-1", "status": "running", "workState": "working", "providerInstanceId": "claudeAgent", "model": "claude-sonnet-5-5"}}, "world": {"files": {"src/deploy/migrate.ts": "export async function applyMigration(m: Migration) {\n for (const name of m.dropTables) {\n await db.dropTable(name); // no confirmation\n }\n}\n", "src/ws/client.ts": "export function connect(base: string, token: string) {\n return new WebSocket(`${base}?token=${token}`);\n}\n", "src/admin/session.ts": "export function createAdminSession(userId: string) {\n return { userId, createdAt: Date.now() }; // no expiresAt\n}\n"}}}, "prompt": "Sounds reasonable. Start the work. Spin up delegates to work on each of these as separate PRs.", "expect": {"calls": [{"tool": "delegate_task", "args": {"isolation": "worktree"}}], "notCalls": ["t3_thread_launch", "create_threads"], "shellMustNot": ["git worktree add"], "callsMustNot": [{"tool": "delegate_task", "args": {"task": "~git worktree add"}}]}, "notes": "Also guards pattern 1: the 'independent implementation in its own worktree' launch section can pull toward t3_thread_launch, which the user did not ask for."} -{"id": "thread-id-not-read", "area": "orchestration", "kind": "bad", "pattern": "did not read a given thread id", "experience": "The user pasted a thread id and asked why 'this t3 code thread' kept mentioning pi. The agent answered from its own context. The user: 'Use the t3 code mcp to get that thread, that's the id lol'.", "rule": "Intended: read a given thread id with t3_thread_read, with a bounded size.", "privacy": "commit-safe", "setup": {"role": "top", "priorTurns": [{"role": "user", "text": "Is the Muse CLI just a fork or wrapper of the Pi agent?"}, {"role": "assistant", "text": "No. The installed Muse CLI is a native Rust binary built from its own source tree. I searched the provider code for 'pi' to check, and found only T3 Code's separate Pi provider docs.", "toolCalls": [{"tool": "Bash", "input": {"command": "rg -il '\\bpi\\b' src/providers"}, "result": "src/providers/pi/README.md"}, {"tool": "Bash", "input": {"command": "file \"$(which muse)\""}, "result": "/usr/local/bin/muse: ELF 64-bit LSB pie executable, x86-64, dynamically linked, Rust toolchain 1.91, stripped"}]}], "mocks": {"t3_thread_read": {"thread": {"threadId": "11111111-1111-4111-8111-111111111111", "title": "Muse CLI integration and ACP support", "link": "[Muse CLI integration and ACP support](t3-thread://v1/11111111-1111-4111-8111-111111111111)", "status": "idle"}, "items": [{"position": 494, "createdBy": "assistant", "text": "Pi already replays recorded transcripts in tests, so the Muse adapter copies that pattern."}], "nextPosition": 495, "hasMore": false}}, "world": {"files": {"src/providers/pi/README.md": "# Pi provider\n\nRuns the Pi agent through its RPC mode.\n", "src/providers/muse/adapter.ts": "export const museAdapter = makeAdapter({ binary: 'muse' });\n"}}}, "prompt": "11111111-1111-4111-8111-111111111111 why does this t3 code thread keep mentioning pi?", "expect": {"calls": [{"tool": "t3_thread_read", "args": {"threadId": "11111111-1111-4111-8111-111111111111"}}]}, "notes": "The prior turn mentions pi, which is what let the agent think 'this thread' meant itself. The same thread then read with limit 100 and got an 85k-char result (see gaps)."} -{"id": "thread-mention-without-link-after-read", "area": "orchestration", "kind": "bad", "pattern": "thread mentioned without t3-thread link", "experience": "Asked to explain what went wrong in a given thread, the agent named it by id or title with no t3-thread:// link.", "rule": "To mention another thread to the user, link it as [title](t3-thread://v1/) with its exact threadId.", "privacy": "commit-safe", "setup": {"role": "top", "mocks": {"t3_thread_read": {"thread": {"threadId": "11111111-1111-4111-8111-111111111111", "title": "Fix ultrafast pricing", "link": "[Fix ultrafast pricing](t3-thread://v1/11111111-1111-4111-8111-111111111111)", "status": "idle"}, "items": [{"position": 210, "createdBy": "assistant", "toolName": "t3_thread_send", "text": "t3_thread_send threadId=44444444-4444-4444-8444-000000000001 message='Round 2. Read only, as before...'"}, {"position": 214, "createdBy": "user", "text": "Use delegate_task please, this should not be a separate thread."}], "nextPosition": 215, "hasMore": false}}}, "prompt": "11111111-1111-4111-8111-111111111111 This thread used the wrong tool. Figure out why.", "expect": {"calls": [{"tool": "t3_thread_read", "args": {"threadId": "11111111-1111-4111-8111-111111111111"}}], "textMust": ["t3-thread://v1/[^)\\s]*11111111-1111-4111-8111-111111111111"]}, "notes": "Real agent was Codex before the 10-21 untruncation fix, when the link rule often did not reach Codex. May pass on Claude today; keep as a regression check for the rewrite.", "maxSteps": 10} -{"id": "found-thread-mentioned-without-link", "area": "orchestration", "kind": "bad", "pattern": "thread mentioned without t3-thread link", "experience": "The agent found yesterday's thread by search and told the user where they left off, but did not link the thread.", "rule": "To mention another thread to the user, link it as [title](t3-thread://v1/) with its exact threadId.", "privacy": "commit-safe", "setup": {"role": "top", "mocks": {"t3_thread_search": {"matches": [{"threadId": "11111111-1111-4111-8111-111111111111", "projectId": "22222222-2222-4222-8222-222222222222", "title": "Fix macOS camera permission prompt", "snippet": "...the app never asks for camera permission on mac...", "messageCreatedAt": "2026-10-19T21:04:00Z"}]}, "t3_thread_list": {"threads": [{"threadId": "11111111-1111-4111-8111-111111111111", "title": "Fix macOS camera permission prompt", "status": "idle"}]}, "t3_thread_read": {"thread": {"threadId": "11111111-1111-4111-8111-111111111111", "title": "Fix macOS camera permission prompt", "link": "[Fix macOS camera permission prompt](t3-thread://v1/11111111-1111-4111-8111-111111111111)", "status": "idle"}, "items": [{"position": 88, "createdBy": "assistant", "text": "The fix is committed locally on branch fix/camera-entitlement in another clone. No PR is open yet."}], "nextPosition": 89, "hasMore": false}}, "shellMocks": [{"match": "gh pr list", "output": ""}]}, "prompt": "Did I ever make a PR to fix the issue where the app couldn't request camera permission on mac? I had a thread about it yesterday and forgot where we left off.", "expect": {"textMust": ["t3-thread://v1/[^)\\s]*11111111-1111-4111-8111-111111111111"]}, "notes": "Also seen in another thread ('Found the prior audits (Jul 21, Sep 1, Oct 3 threads ...)' with no links). Original prompt also said 'get it fixed full stop'; dropped to keep the case small."} +{"id": "thread-id-not-read", "area": "orchestration", "kind": "bad", "pattern": "did not read a given thread id", "experience": "The user pasted a thread id and asked why 'this t3 code thread' kept mentioning pi. The agent answered from its own context. The user: 'Use the t3 code mcp to get that thread, that's the id lol'.", "rule": "Intended: read a given thread id with t3_thread_read, with a bounded size.", "privacy": "commit-safe", "setup": {"role": "top", "priorTurns": [{"role": "user", "text": "Is the Muse CLI just a fork or wrapper of the Pi agent?"}, {"role": "assistant", "text": "No. The installed Muse CLI is a native Rust binary built from its own source tree. I searched the provider code for 'pi' to check, and found only T3 Code's separate Pi provider docs.", "toolCalls": [{"tool": "Bash", "input": {"command": "rg -il '\\bpi\\b' src/providers"}, "result": "src/providers/pi/README.md"}, {"tool": "Bash", "input": {"command": "file \"$(which muse)\""}, "result": "/usr/local/bin/muse: ELF 64-bit LSB pie executable, x86-64, dynamically linked, Rust toolchain 1.91, stripped"}]}], "mocks": {"t3_thread_read": {"thread": {"threadId": "11111111-1111-4111-8111-111111111111", "title": "Muse CLI integration and ACP support", "link": "", "status": "idle"}, "items": [{"position": 494, "createdBy": "assistant", "text": "Pi already replays recorded transcripts in tests, so the Muse adapter copies that pattern."}], "nextPosition": 495, "hasMore": false}}, "world": {"files": {"src/providers/pi/README.md": "# Pi provider\n\nRuns the Pi agent through its RPC mode.\n", "src/providers/muse/adapter.ts": "export const museAdapter = makeAdapter({ binary: 'muse' });\n"}}}, "prompt": "11111111-1111-4111-8111-111111111111 why does this t3 code thread keep mentioning pi?", "expect": {"calls": [{"tool": "t3_thread_read", "args": {"threadId": "11111111-1111-4111-8111-111111111111"}}]}, "notes": "The prior turn mentions pi, which is what let the agent think 'this thread' meant itself. The same thread then read with limit 100 and got an 85k-char result (see gaps)."} +{"id": "thread-mention-without-link-after-read", "area": "orchestration", "kind": "bad", "pattern": "thread mentioned without t3-thread link", "experience": "Asked to explain what went wrong in a given thread, the agent named it by id or title with no t3-thread:// link.", "rule": "To mention another thread to the user, write its `link` verbatim: />, with no title.", "privacy": "commit-safe", "setup": {"role": "top", "mocks": {"t3_thread_read": {"thread": {"threadId": "11111111-1111-4111-8111-111111111111", "title": "Fix ultrafast pricing", "link": "", "status": "idle"}, "items": [{"position": 210, "createdBy": "assistant", "toolName": "t3_thread_send", "text": "t3_thread_send threadId=44444444-4444-4444-8444-000000000001 message='Round 2. Read only, as before...'"}, {"position": 214, "createdBy": "user", "text": "Use delegate_task please, this should not be a separate thread."}], "nextPosition": 215, "hasMore": false}}}, "prompt": "11111111-1111-4111-8111-111111111111 This thread used the wrong tool. Figure out why.", "expect": {"calls": [{"tool": "t3_thread_read", "args": {"threadId": "11111111-1111-4111-8111-111111111111"}}], "textMust": ["t3-thread://v2/eeeeeeee-eeee-4eee-8eee-eeeeeeeeeeee/11111111-1111-4111-8111-111111111111"], "textMustNot": ["t3-thread://v1/"]}, "notes": "Real agent was Codex before the 10-21 untruncation fix, when the link rule often did not reach Codex. May pass on Claude today; keep as a regression check for the rewrite.", "maxSteps": 10} +{"id": "found-thread-mentioned-without-link", "area": "orchestration", "kind": "bad", "pattern": "thread mentioned without t3-thread link", "experience": "The agent found yesterday's thread by search and told the user where they left off, but did not link the thread.", "rule": "To mention another thread to the user, write its `link` verbatim: />, with no title.", "privacy": "commit-safe", "setup": {"role": "top", "mocks": {"t3_thread_search": {"matches": [{"threadId": "11111111-1111-4111-8111-111111111111", "projectId": "22222222-2222-4222-8222-222222222222", "title": "Fix macOS camera permission prompt", "snippet": "...the app never asks for camera permission on mac...", "messageCreatedAt": "2026-10-19T21:04:00Z"}]}, "t3_thread_list": {"threads": [{"threadId": "11111111-1111-4111-8111-111111111111", "title": "Fix macOS camera permission prompt", "status": "idle"}]}, "t3_thread_read": {"thread": {"threadId": "11111111-1111-4111-8111-111111111111", "title": "Fix macOS camera permission prompt", "link": "", "status": "idle"}, "items": [{"position": 88, "createdBy": "assistant", "text": "The fix is committed locally on branch fix/camera-entitlement in another clone. No PR is open yet."}], "nextPosition": 89, "hasMore": false}}, "shellMocks": [{"match": "gh pr list", "output": ""}]}, "prompt": "Did I ever make a PR to fix the issue where the app couldn't request camera permission on mac? I had a thread about it yesterday and forgot where we left off.", "expect": {"textMust": ["t3-thread://v2/eeeeeeee-eeee-4eee-8eee-eeeeeeeeeeee/11111111-1111-4111-8111-111111111111"], "textMustNot": ["t3-thread://v1/"]}, "notes": "Also seen in another thread ('Found the prior audits (Jul 21, Sep 1, Oct 3 threads ...)' with no links). Original prompt also said 'get it fixed full stop'; dropped to keep the case small."} +{"id": "thread-link-rewritten-with-title", "area": "orchestration", "kind": "bad", "pattern": "thread link rewritten with a title", "experience": "Thread links carried the title the agent wrote, which went stale once the user renamed the thread. The app already shows each linked thread's current title, so maintainers asked that agents write only the stable link.", "rule": "To mention another thread to the user, write its `link` verbatim: />, with no title.", "privacy": "commit-safe", "setup": {"role": "top", "mocks": {"t3_thread_list": {"threads": [{"threadId": "11111111-1111-4111-8111-111111111111", "title": "Fix flaky checkout test", "status": "running"}, {"threadId": "44444444-4444-4444-8444-000000000002", "title": "Upgrade Vite", "status": "idle"}]}}}, "prompt": "Which of my threads in this project are still running?", "expect": {"calls": [{"tool": "t3_thread_list"}], "textMust": [""], "textMustNot": ["\\]\\(t3-thread://", "t3-thread://v1/"]}, "notes": "Source: maintainer request with the v2 thread link change. The old instructions carried a titled [title](t3-thread://v1/) template for threads without a link."} {"id": "cross-provider-delegates-with-options", "area": "orchestration", "kind": "guard", "pattern": "capabilities then cross-provider delegate_task with options", "experience": "Asked for Sol 'high' and Muse Wonder Full 'xhigh' delegates, the agent read capabilities and put effort in target.options.", "rule": "Read the selected model's descriptors from orchestrator_capabilities and put settings in target.options, not only in the prompt.", "privacy": "commit-safe", "setup": {"role": "top", "mocks": {"orchestrator_capabilities": {"parentThreadId": "33333333-3333-4333-8333-333333333333", "inheritedProviderInstanceId": "claudeAgent", "inheritedModel": "claude-sonnet-5-5", "inheritedOptions": [{"id": "effort", "value": "high"}], "runtimeMode": "full-access", "interactionMode": "default", "features": {"appOwnedSubagents": true, "asyncPolling": true, "cancellation": true, "batchThreadCreation": true, "threadManagement": true, "incrementalThreadRead": true, "scheduledTasks": true}, "providers": [{"providerInstanceId": "claudeAgent", "driverKind": "claudeAgent", "displayName": "Claude", "models": [{"id": "claude-opus-5-5", "label": "Claude Opus 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}, {"id": "fastMode", "type": "boolean", "default": false}]}, {"id": "claude-sonnet-5-5", "label": "Claude Sonnet 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}]}, {"id": "claude-haiku-5-5", "label": "Claude Haiku 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}]}]}, {"providerInstanceId": "codex", "driverKind": "codex", "displayName": "Codex", "models": [{"id": "gpt-6.1-sol", "label": "GPT-6.1 Sol", "options": [{"id": "reasoningEffort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}, {"id": "serviceTier", "values": ["default", "priority"], "default": "default"}]}]}, {"providerInstanceId": "muse", "driverKind": "muse", "displayName": "Muse Code", "runtimeModes": ["approval-required", "full-access"], "models": [{"id": "muse-wonder-full", "label": "Muse Wonder Full", "options": [{"id": "reasoningEffort", "values": ["minimal", "low", "medium", "high", "xhigh", "max"], "default": "high"}]}]}, {"providerInstanceId": "grok", "driverKind": "grok", "displayName": "Grok", "models": [{"id": "grok-4.7", "label": "Grok 4.7", "options": []}]}]}, "delegate_task": "default"}}, "prompt": "I want this codebase reviewed by two different models. Spin up a delegate for GPT-6.1 Sol high in Codex and one for Muse Wonder Full at xhigh. Tell them both to explore the codebase and find interesting things, plus some easy wins.", "expect": {"order": ["orchestrator_capabilities", "delegate_task"], "calls": [{"tool": "delegate_task", "args": {"target.providerInstanceId": "codex", "target.model": "gpt-6.1-sol", "target.options.reasoningEffort": "high"}}, {"tool": "delegate_task", "args": {"target.providerInstanceId": "muse", "target.model": "muse-wonder-full", "target.options.reasoningEffort": "xhigh"}}], "notCalls": ["t3_thread_launch", "create_threads", "Agent"]}, "notes": "target.options may be an object or an [{id,value}] array. The runner must normalize arrays to an object before matching 'target.options.'."} {"id": "same-provider-delegate-with-effort", "area": "orchestration", "kind": "guard", "pattern": "model options force delegate_task over native", "experience": "Asked for an Opus 5.5 (xhigh) delegate from a Claude parent, the agent used delegate_task with target.options effort xhigh.", "rule": "When model options are requested ... use delegate_task even for native-supported models. Native subagents cannot reliably apply T3 model options.", "privacy": "commit-safe", "setup": {"role": "top", "mocks": {"orchestrator_capabilities": {"parentThreadId": "33333333-3333-4333-8333-333333333333", "inheritedProviderInstanceId": "claudeAgent", "inheritedModel": "claude-sonnet-5-5", "inheritedOptions": [{"id": "effort", "value": "high"}], "runtimeMode": "full-access", "interactionMode": "default", "features": {"appOwnedSubagents": true, "asyncPolling": true, "cancellation": true, "batchThreadCreation": true, "threadManagement": true, "incrementalThreadRead": true, "scheduledTasks": true}, "providers": [{"providerInstanceId": "claudeAgent", "driverKind": "claudeAgent", "displayName": "Claude", "models": [{"id": "claude-opus-5-5", "label": "Claude Opus 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}, {"id": "fastMode", "type": "boolean", "default": false}]}, {"id": "claude-sonnet-5-5", "label": "Claude Sonnet 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}]}, {"id": "claude-haiku-5-5", "label": "Claude Haiku 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}]}]}, {"providerInstanceId": "codex", "driverKind": "codex", "displayName": "Codex", "models": [{"id": "gpt-6.1-sol", "label": "GPT-6.1 Sol", "options": [{"id": "reasoningEffort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}, {"id": "serviceTier", "values": ["default", "priority"], "default": "default"}]}]}, {"providerInstanceId": "muse", "driverKind": "muse", "displayName": "Muse Code", "runtimeModes": ["approval-required", "full-access"], "models": [{"id": "muse-wonder-full", "label": "Muse Wonder Full", "options": [{"id": "reasoningEffort", "values": ["minimal", "low", "medium", "high", "xhigh", "max"], "default": "high"}]}]}, {"providerInstanceId": "grok", "driverKind": "grok", "displayName": "Grok", "models": [{"id": "grok-4.7", "label": "Grok 4.7", "options": []}]}]}, "delegate_task": "default"}}, "prompt": "Spin up an Opus 5.5 delegate in Claude Code on xhigh to research why the update flow spams 'connected successfully' toasts. Have it write up the causes in priority order.", "expect": {"calls": [{"tool": "delegate_task", "args": {"target.providerInstanceId": "claudeAgent", "target.model": "claude-opus-5-5", "target.options.effort": "xhigh"}}], "notCalls": ["Agent", "Workflow"]}, "notes": "A shorter text that drops the options rule could send this to the native Agent tool with model 'opus' and lose xhigh."} {"id": "async-delegate-then-end-turn", "area": "orchestration", "kind": "guard", "pattern": "async delegate then end turn", "experience": "After starting an async Sol review the agent said its completion would wake the thread and ended the turn.", "rule": "Prefer mode='async' for long work ... the task's completion still wakes this thread. (intended: end the turn; no sleep or task_status loops)", "privacy": "commit-safe", "setup": {"role": "top", "mocks": {"orchestrator_capabilities": {"parentThreadId": "33333333-3333-4333-8333-333333333333", "inheritedProviderInstanceId": "claudeAgent", "inheritedModel": "claude-sonnet-5-5", "inheritedOptions": [{"id": "effort", "value": "high"}], "runtimeMode": "full-access", "interactionMode": "default", "features": {"appOwnedSubagents": true, "asyncPolling": true, "cancellation": true, "batchThreadCreation": true, "threadManagement": true, "incrementalThreadRead": true, "scheduledTasks": true}, "providers": [{"providerInstanceId": "claudeAgent", "driverKind": "claudeAgent", "displayName": "Claude", "models": [{"id": "claude-opus-5-5", "label": "Claude Opus 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}, {"id": "fastMode", "type": "boolean", "default": false}]}, {"id": "claude-sonnet-5-5", "label": "Claude Sonnet 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}]}, {"id": "claude-haiku-5-5", "label": "Claude Haiku 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}]}]}, {"providerInstanceId": "codex", "driverKind": "codex", "displayName": "Codex", "models": [{"id": "gpt-6.1-sol", "label": "GPT-6.1 Sol", "options": [{"id": "reasoningEffort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}, {"id": "serviceTier", "values": ["default", "priority"], "default": "default"}]}]}, {"providerInstanceId": "muse", "driverKind": "muse", "displayName": "Muse Code", "runtimeModes": ["approval-required", "full-access"], "models": [{"id": "muse-wonder-full", "label": "Muse Wonder Full", "options": [{"id": "reasoningEffort", "values": ["minimal", "low", "medium", "high", "xhigh", "max"], "default": "high"}]}]}, {"providerInstanceId": "grok", "driverKind": "grok", "displayName": "Grok", "models": [{"id": "grok-4.7", "label": "Grok 4.7", "options": []}]}]}, "delegate_task": "default", "task_status": "default"}, "world": {"branch": "fix/session-stop", "commits": [{"sha": "3e8d1f0", "subject": "fix(server): stop clears an orphaned task row"}], "diff": "diff --git a/src/session.ts b/src/session.ts\n--- a/src/session.ts\n+++ b/src/session.ts\n@@ -10,1 +10,1 @@\n- if (task.status !== \"running\") return;\n+ if (task.status === \"cancelled\") return emitTaskRow(task);\n", "prs": [{"number": 123, "title": "fix(server): stop clears an orphaned task row", "headRefName": "fix/session-stop"}]}}, "prompt": "Have a GPT-6.1 Sol delegate review the diff on this branch (https://github.com/acme/app/pull/123) before I merge it.", "expect": {"calls": [{"tool": "delegate_task", "args": {"target.model": "gpt-6.1-sol"}}], "maxCalls": {"task_status": 0, "t3_thread_wait": 0}, "shellMustNot": ["\\bsleep\\b"]}, "notes": ""} @@ -24,7 +25,7 @@ {"id": "opus-review-too-via-delegate", "area": "orchestration", "kind": "guard", "pattern": "same-provider review via delegate_task", "experience": "'Have Opus review the things too' from a Claude parent with no extra settings runs a native Opus subagent the parent waits for.", "rule": "Use your native tool when the child uses your provider, a model it offers, and no settings beyond yours, and you need it only in this turn.", "privacy": "commit-safe", "setup": {"role": "top", "priorTurns": [{"role": "user", "text": "Get a Sol review on https://github.com/acme/app/pull/123 before we file anything else."}, {"role": "assistant", "text": "Sol approved PR 123 after one fix to its description.", "toolCalls": [{"tool": "delegate_task", "input": {"title": "Sol review PR 123", "mode": "async", "clientRequestId": "sol-123-r1", "target": {"providerInstanceId": "codex", "model": "gpt-6.1-sol", "options": {"reasoningEffort": "high"}}, "task": "Review https://github.com/acme/app/pull/123. Read only."}, "result": {"taskId": "task-1", "childThreadId": "44444444-4444-4444-8444-000000000001", "childRunId": "run-child-1", "status": "running", "workState": "working", "providerInstanceId": "codex", "model": "gpt-6.1-sol"}}, {"tool": "task_status", "input": {"taskId": "task-1"}, "result": {"taskId": "task-1", "childThreadId": "44444444-4444-4444-8444-000000000001", "status": "completed", "workState": "result_available", "summary": "APPROVE after S1 (description wording)."}}]}], "mocks": {"orchestrator_capabilities": {"parentThreadId": "33333333-3333-4333-8333-333333333333", "inheritedProviderInstanceId": "claudeAgent", "inheritedModel": "claude-sonnet-5-5", "inheritedOptions": [{"id": "effort", "value": "high"}], "runtimeMode": "full-access", "interactionMode": "default", "features": {"appOwnedSubagents": true, "asyncPolling": true, "cancellation": true, "batchThreadCreation": true, "threadManagement": true, "incrementalThreadRead": true, "scheduledTasks": true}, "providers": [{"providerInstanceId": "claudeAgent", "driverKind": "claudeAgent", "displayName": "Claude", "models": [{"id": "claude-opus-5-5", "label": "Claude Opus 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}, {"id": "fastMode", "type": "boolean", "default": false}]}, {"id": "claude-sonnet-5-5", "label": "Claude Sonnet 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}]}, {"id": "claude-haiku-5-5", "label": "Claude Haiku 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}]}]}, {"providerInstanceId": "codex", "driverKind": "codex", "displayName": "Codex", "models": [{"id": "gpt-6.1-sol", "label": "GPT-6.1 Sol", "options": [{"id": "reasoningEffort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}, {"id": "serviceTier", "values": ["default", "priority"], "default": "default"}]}]}, {"providerInstanceId": "muse", "driverKind": "muse", "displayName": "Muse Code", "runtimeModes": ["approval-required", "full-access"], "models": [{"id": "muse-wonder-full", "label": "Muse Wonder Full", "options": [{"id": "reasoningEffort", "values": ["minimal", "low", "medium", "high", "xhigh", "max"], "default": "high"}]}]}, {"providerInstanceId": "grok", "driverKind": "grok", "displayName": "Grok", "models": [{"id": "grok-4.7", "label": "Grok 4.7", "options": []}]}]}, "delegate_task": "default"}, "world": {"prs": [{"number": 123, "title": "fix(web): keep scroll position on thread switch", "headRefName": "fix/scroll"}]}, "harness": "claude"}, "prompt": "Have Opus review the things too, not just Sol", "expect": {"calls": [{"tool": "Agent", "args": {"model": "~opus"}}], "notCalls": ["delegate_task", "t3_thread_send"], "callsMustNot": [{"tool": "Agent", "args": {"run_in_background": true}}]}, "notes": "T3's 'prefer native for same-provider' line pulls toward the native Agent tool here; this stayed correct in the real thread."} {"id": "delegates-then-new-thread-with-model", "area": "orchestration", "kind": "guard", "pattern": "new thread asked: t3_thread_launch with model", "experience": "'Spin up 2 Opus delegates ... then start a new thread using Grok 4.7 to implement' gave 2 delegate_task calls and 1 t3_thread_launch on Grok.", "rule": "t3_thread_launch and create_threads ... Use them only when the user explicitly asks for separate/new/top-level threads.", "privacy": "commit-safe", "setup": {"role": "top", "priorTurns": [{"role": "user", "text": "Spin up 2 Opus 5.5 delegates to explore performance wins in this app."}, {"role": "assistant", "text": "Both Opus delegates are done.\n- Client: the thread list re-renders every row on each streamed token.\n- Server: each event write runs a full projection rebuild.", "toolCalls": [{"tool": "delegate_task", "input": {"title": "Opus: client performance", "mode": "wait", "clientRequestId": "perf-client", "target": {"providerInstanceId": "claudeAgent", "model": "claude-opus-5-5"}, "task": "Find concrete client performance wins. Read only."}, "result": {"taskId": "task-1", "childThreadId": "44444444-4444-4444-8444-000000000001", "childRunId": "run-child-1", "status": "completed", "workState": "result_available", "providerInstanceId": "claudeAgent", "model": "claude-opus-5-5", "summary": "Client: the thread list re-renders every row on each streamed token. Memoize rows by thread id and select only the active thread's stream."}}, {"tool": "delegate_task", "input": {"title": "Opus: server performance", "mode": "wait", "clientRequestId": "perf-server", "target": {"providerInstanceId": "claudeAgent", "model": "claude-opus-5-5"}, "task": "Find concrete server performance wins. Read only."}, "result": {"taskId": "task-2", "childThreadId": "44444444-4444-4444-8444-000000000002", "childRunId": "run-child-2", "status": "completed", "workState": "result_available", "providerInstanceId": "claudeAgent", "model": "claude-opus-5-5", "summary": "Server: each event write runs a full projection rebuild. Apply events to the projection incrementally."}}]}], "mocks": {"orchestrator_capabilities": {"parentThreadId": "33333333-3333-4333-8333-333333333333", "inheritedProviderInstanceId": "claudeAgent", "inheritedModel": "claude-sonnet-5-5", "inheritedOptions": [{"id": "effort", "value": "high"}], "runtimeMode": "full-access", "interactionMode": "default", "features": {"appOwnedSubagents": true, "asyncPolling": true, "cancellation": true, "batchThreadCreation": true, "threadManagement": true, "incrementalThreadRead": true, "scheduledTasks": true}, "providers": [{"providerInstanceId": "claudeAgent", "driverKind": "claudeAgent", "displayName": "Claude", "models": [{"id": "claude-opus-5-5", "label": "Claude Opus 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}, {"id": "fastMode", "type": "boolean", "default": false}]}, {"id": "claude-sonnet-5-5", "label": "Claude Sonnet 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}]}, {"id": "claude-haiku-5-5", "label": "Claude Haiku 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}]}]}, {"providerInstanceId": "codex", "driverKind": "codex", "displayName": "Codex", "models": [{"id": "gpt-6.1-sol", "label": "GPT-6.1 Sol", "options": [{"id": "reasoningEffort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}, {"id": "serviceTier", "values": ["default", "priority"], "default": "default"}]}]}, {"providerInstanceId": "muse", "driverKind": "muse", "displayName": "Muse Code", "runtimeModes": ["approval-required", "full-access"], "models": [{"id": "muse-wonder-full", "label": "Muse Wonder Full", "options": [{"id": "reasoningEffort", "values": ["minimal", "low", "medium", "high", "xhigh", "max"], "default": "high"}]}]}, {"providerInstanceId": "grok", "driverKind": "grok", "displayName": "Grok", "models": [{"id": "grok-4.7", "label": "Grok 4.7", "options": []}]}]}, "t3_thread_launch": {"threadId": "11111111-1111-4111-8111-111111111111", "projectId": "22222222-2222-4222-8222-222222222222", "modelSelection": {"instanceId": "grok", "model": "grok-4.7"}, "status": "starting"}}}, "prompt": "Summarize their thoughts, then start a new thread using Grok 4.7 to implement your findings.", "expect": {"calls": [{"tool": "t3_thread_launch", "args": {"modelSelection.model": "~grok"}}], "notCalls": ["delegate_task", "create_threads"]}, "notes": "Pattern 2 (new thread asked but delegate used) had only 1-2 real cases in 40 asks, so this is a guard. Check the arg path against the current t3_thread_launch schema."} {"id": "subagent-ask-no-new-threads", "area": "orchestration", "kind": "guard", "pattern": "subagent asked: no t3_thread_launch/create_threads", "experience": "'Have Haiku 5.5 subagents do the exploration' must give child tasks, not top-level threads. 0 of 98 subagent asks went wrong, and the user still warns about it.", "rule": "Never use them merely because the user said \"subagent\" or requested parallel delegated work.", "privacy": "commit-safe", "setup": {"role": "top", "mocks": {"orchestrator_capabilities": {"parentThreadId": "33333333-3333-4333-8333-333333333333", "inheritedProviderInstanceId": "claudeAgent", "inheritedModel": "claude-sonnet-5-5", "inheritedOptions": [{"id": "effort", "value": "high"}], "runtimeMode": "full-access", "interactionMode": "default", "features": {"appOwnedSubagents": true, "asyncPolling": true, "cancellation": true, "batchThreadCreation": true, "threadManagement": true, "incrementalThreadRead": true, "scheduledTasks": true}, "providers": [{"providerInstanceId": "claudeAgent", "driverKind": "claudeAgent", "displayName": "Claude", "models": [{"id": "claude-opus-5-5", "label": "Claude Opus 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}, {"id": "fastMode", "type": "boolean", "default": false}]}, {"id": "claude-sonnet-5-5", "label": "Claude Sonnet 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}]}, {"id": "claude-haiku-5-5", "label": "Claude Haiku 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}]}]}, {"providerInstanceId": "codex", "driverKind": "codex", "displayName": "Codex", "models": [{"id": "gpt-6.1-sol", "label": "GPT-6.1 Sol", "options": [{"id": "reasoningEffort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}, {"id": "serviceTier", "values": ["default", "priority"], "default": "default"}]}]}, {"providerInstanceId": "muse", "driverKind": "muse", "displayName": "Muse Code", "runtimeModes": ["approval-required", "full-access"], "models": [{"id": "muse-wonder-full", "label": "Muse Wonder Full", "options": [{"id": "reasoningEffort", "values": ["minimal", "low", "medium", "high", "xhigh", "max"], "default": "high"}]}]}, {"providerInstanceId": "grok", "driverKind": "grok", "displayName": "Grok", "models": [{"id": "grok-4.7", "label": "Grok 4.7", "options": []}]}]}, "delegate_task": "default"}, "world": {"files": {"package.json": "{\n \"name\": \"app\",\n \"private\": true,\n \"packageManager\": \"pnpm@10.4.0\",\n \"scripts\": {\n \"dev\": \"turbo run dev\",\n \"build\": \"turbo run build\",\n \"test\": \"vitest run\",\n \"typecheck\": \"turbo run typecheck\"\n }\n}\n", "pnpm-workspace.yaml": "packages:\n - apps/*\n - packages/*\n", "turbo.json": "{\n \"tasks\": {\n \"build\": { \"outputs\": [\"dist/**\"] },\n \"typecheck\": {},\n \"dev\": { \"cache\": false }\n }\n}\n", "apps/web/package.json": "{ \"name\": \"@app/web\", \"scripts\": { \"dev\": \"vite\", \"build\": \"vite build\" } }\n", "apps/server/package.json": "{ \"name\": \"@app/server\", \"scripts\": { \"dev\": \"tsx watch src/index.ts\", \"build\": \"tsc\" } }\n", ".github/workflows/ci.yml": "name: ci\non: [push, pull_request]\njobs:\n test:\n runs-on: ubuntu-latest\n steps:\n - uses: actions/checkout@v4\n - run: pnpm install --frozen-lockfile\n - run: pnpm typecheck\n - run: pnpm test\n", "scripts/seed-dev-db.ts": "import { db } from '../apps/server/src/db';\n\n// Seeds the dev database with 20 fake users.\nfor (let i = 0; i < 20; i++) await db.insert('users', { name: `user${i}` });\n", "apps/web/src/main.tsx": "import { createRoot } from 'react-dom/client';\nimport { App } from './App';\n\ncreateRoot(document.getElementById('root')!).render();\n", "apps/web/src/App.tsx": "export function App() {\n return ;\n}\n", "apps/server/src/index.ts": "import { createServer } from './server';\n\ncreateServer().listen(Number(process.env.PORT ?? 3773));\n", "apps/server/src/db.ts": "import { Database } from 'better-sqlite3';\n\nexport const db = new Database(process.env.DB_PATH ?? 'dev.sqlite');\n"}}}, "prompt": "I want your honest thoughts on this project. Do a deep dive and really read through it so you know it well. Have Haiku 5.5 subagents do the exploration.", "expect": {"notCalls": ["t3_thread_launch", "create_threads"]}, "notes": "Narrow on purpose: native vs delegate is graded in the bad cases. The user's preemptive guard: 'Please don't make new threads with the T3 Code MCP, only create delegates, workflows, subagents etc.'."} -{"id": "thread-send-to-named-top-level-thread", "area": "orchestration", "kind": "guard", "pattern": "t3_thread_send to a top-level thread the user names", "experience": "Given another top-level thread id to talk to, the agent used t3_thread_send on it and linked it in the reply.", "rule": "To mention another thread to the user, link it as [title](t3-thread://v1/).", "privacy": "commit-safe", "setup": {"role": "top", "priorTurns": [{"role": "user", "text": "The other agent disagreed:\n\nTheirs is the better plan to execute. Mine is the wider survey. Start from theirs and graft three items from mine.\n\nTheir 5 points: 1) cache tsc output per package, 2) split the web typecheck into two projects, 3) drop the duplicate lint step in CI, 4) run typecheck only for changed packages in CI (memo), 5) measure before and after on main."}, {"role": "assistant", "text": "We now agree on almost everything. I accept points 1, 2, 3, and 5. I disagree with one detail of the memo in point 4: CI must also typecheck the packages that depend on a changed package, not only the changed ones. From my plan I would graft: project references, a warm tsc cache restored in CI, and a per-package timing report."}], "mocks": {"t3_thread_send": {"threadId": "11111111-1111-4111-8111-111111111111", "messageId": "msg-1", "runId": "run-1", "queued": true}, "t3_thread_read": {"thread": {"threadId": "11111111-1111-4111-8111-111111111111", "title": "Faster type checking", "link": "[Faster type checking](t3-thread://v1/11111111-1111-4111-8111-111111111111)", "status": "idle"}, "items": [{"position": 41, "createdBy": "assistant", "text": "Plan: 1) cache tsc output per package, 2) split the web typecheck, 3) drop the duplicate lint step, 4) memo: typecheck only changed packages in CI, 5) measure on main."}, {"position": 42, "createdBy": "user", "text": "Another agent has a wider survey. I'll bring its answer back here."}], "nextPosition": 43, "hasMore": false}}}, "prompt": "That thread is on this machine, you can talk to it directly with t3 code: 11111111-1111-4111-8111-111111111111", "expect": {"calls": [{"tool": "t3_thread_send", "args": {"threadId": "11111111-1111-4111-8111-111111111111"}}], "notCalls": ["delegate_task", "t3_thread_launch"], "textMust": ["t3-thread://v1/[^)\\s]*11111111-1111-4111-8111-111111111111"]}, "notes": "Keeps a rewrite from over-banning t3_thread_send. The ban is only for delegated childThreadIds."} +{"id": "thread-send-to-named-top-level-thread", "area": "orchestration", "kind": "guard", "pattern": "t3_thread_send to a top-level thread the user names", "experience": "Given another top-level thread id to talk to, the agent used t3_thread_send on it and linked it in the reply.", "rule": "To mention another thread to the user, write its `link` verbatim: />, with no title.", "privacy": "commit-safe", "setup": {"role": "top", "priorTurns": [{"role": "user", "text": "The other agent disagreed:\n\nTheirs is the better plan to execute. Mine is the wider survey. Start from theirs and graft three items from mine.\n\nTheir 5 points: 1) cache tsc output per package, 2) split the web typecheck into two projects, 3) drop the duplicate lint step in CI, 4) run typecheck only for changed packages in CI (memo), 5) measure before and after on main."}, {"role": "assistant", "text": "We now agree on almost everything. I accept points 1, 2, 3, and 5. I disagree with one detail of the memo in point 4: CI must also typecheck the packages that depend on a changed package, not only the changed ones. From my plan I would graft: project references, a warm tsc cache restored in CI, and a per-package timing report."}], "mocks": {"t3_thread_send": {"threadId": "11111111-1111-4111-8111-111111111111", "messageId": "msg-1", "runId": "run-1", "queued": true}, "t3_thread_read": {"thread": {"threadId": "11111111-1111-4111-8111-111111111111", "title": "Faster type checking", "link": "", "status": "idle"}, "items": [{"position": 41, "createdBy": "assistant", "text": "Plan: 1) cache tsc output per package, 2) split the web typecheck, 3) drop the duplicate lint step, 4) memo: typecheck only changed packages in CI, 5) measure on main."}, {"position": 42, "createdBy": "user", "text": "Another agent has a wider survey. I'll bring its answer back here."}], "nextPosition": 43, "hasMore": false}}}, "prompt": "That thread is on this machine, you can talk to it directly with t3 code: 11111111-1111-4111-8111-111111111111", "expect": {"calls": [{"tool": "t3_thread_send", "args": {"threadId": "11111111-1111-4111-8111-111111111111"}}], "notCalls": ["delegate_task", "t3_thread_launch"], "textMust": [""]}, "notes": "Keeps a rewrite from over-banning t3_thread_send. The ban is only for delegated childThreadIds."} {"id": "too-many-delegates-cancel-extras", "area": "orchestration", "kind": "guard", "pattern": "volume complaint: cancel, do not add", "experience": "After 'You're doing too much man, we have like 6+ delegates going', the agent stopped the stale review tasks and made the next fix without a new round.", "rule": "none (task_cancel description covers the action)", "privacy": "commit-safe", "setup": {"role": "top", "priorTurns": [{"role": "user", "text": "Take over https://github.com/acme/app/pull/123. Have Sol and Opus delegates audit it hard."}, {"role": "assistant", "text": "Five audits are running on the PR: three Sol (error handling, concurrency, API surface) and two Opus (tests, migration safety).", "toolCalls": [{"tool": "delegate_task", "input": {"title": "Sol audit: error handling", "mode": "async", "clientRequestId": "pr123-sol-error", "target": {"providerInstanceId": "codex", "model": "gpt-6.1-sol", "options": {"reasoningEffort": "high"}}, "task": "Audit https://github.com/acme/app/pull/123 for error handling problems. Report each with file:line. Read only."}, "result": {"taskId": "task-1", "childThreadId": "44444444-4444-4444-8444-000000000001", "childRunId": "run-child-1", "status": "running", "workState": "working", "providerInstanceId": "codex", "model": "gpt-6.1-sol"}}, {"tool": "delegate_task", "input": {"title": "Sol audit: concurrency", "mode": "async", "clientRequestId": "pr123-sol-concurrency", "target": {"providerInstanceId": "codex", "model": "gpt-6.1-sol", "options": {"reasoningEffort": "high"}}, "task": "Audit https://github.com/acme/app/pull/123 for concurrency problems. Report each with file:line. Read only."}, "result": {"taskId": "task-2", "childThreadId": "44444444-4444-4444-8444-000000000002", "childRunId": "run-child-2", "status": "running", "workState": "working", "providerInstanceId": "codex", "model": "gpt-6.1-sol"}}, {"tool": "delegate_task", "input": {"title": "Sol audit: API surface", "mode": "async", "clientRequestId": "pr123-sol-api", "target": {"providerInstanceId": "codex", "model": "gpt-6.1-sol", "options": {"reasoningEffort": "high"}}, "task": "Audit https://github.com/acme/app/pull/123 for API surface problems. Report each with file:line. Read only."}, "result": {"taskId": "task-3", "childThreadId": "44444444-4444-4444-8444-000000000003", "childRunId": "run-child-3", "status": "running", "workState": "working", "providerInstanceId": "codex", "model": "gpt-6.1-sol"}}, {"tool": "delegate_task", "input": {"title": "Opus audit: tests", "mode": "async", "clientRequestId": "pr123-opus-tests", "target": {"providerInstanceId": "claudeAgent", "model": "claude-opus-5-5"}, "task": "Audit https://github.com/acme/app/pull/123 for tests problems. Report each with file:line. Read only."}, "result": {"taskId": "task-4", "childThreadId": "44444444-4444-4444-8444-000000000004", "childRunId": "run-child-4", "status": "running", "workState": "working", "providerInstanceId": "claudeAgent", "model": "claude-opus-5-5"}}, {"tool": "delegate_task", "input": {"title": "Opus audit: migration safety", "mode": "async", "clientRequestId": "pr123-opus-migration", "target": {"providerInstanceId": "claudeAgent", "model": "claude-opus-5-5"}, "task": "Audit https://github.com/acme/app/pull/123 for migration safety problems. Report each with file:line. Read only."}, "result": {"taskId": "task-5", "childThreadId": "44444444-4444-4444-8444-000000000005", "childRunId": "run-child-5", "status": "running", "workState": "working", "providerInstanceId": "claudeAgent", "model": "claude-opus-5-5"}}]}], "mocks": {"task_status": {"status": "running", "workState": "working"}, "task_cancel": {"status": "cancel_requested"}}, "world": {"prs": [{"number": 123, "title": "feat(server): resumable uploads", "headRefName": "feat/resumable-uploads"}]}}, "prompt": "You're doing too much man, we have like 6+ delegates going. We don't need to go that hard.", "expect": {"calls": [{"tool": "task_cancel"}], "maxCalls": {"delegate_task": 0}, "notCalls": ["t3_thread_interrupt"]}, "notes": "Pairs with review-round-per-bot-nit."} {"id": "control-new-thread-view-wording", "area": "orchestration", "kind": "control", "pattern": "'new thread' as product text", "experience": "A UI bug report that says 'new thread view' needs a code fix, not a new T3 thread or delegate.", "rule": "t3_thread_launch and create_threads ... only when the user explicitly asks for separate/new/top-level threads.", "privacy": "commit-safe", "setup": {"role": "top", "world": {"files": {"apps/mobile/src/Sidebar.tsx": "export function Sidebar() {\n return (\n \n \n \n );\n}\n", "apps/mobile/src/NewThreadView.tsx": "export function NewThreadView() {\n return {/* composer */};\n}\n"}}}, "prompt": "On Android foldables the sidebar is visible but tapping threads does nothing in some places, like the new thread view or settings. It makes the sidebar feel broken. Fix it.", "expect": {"notCalls": ["t3_thread_launch", "create_threads", "delegate_task", "orchestrator_capabilities"]}, "notes": ""} {"id": "control-subagent-label-bug-wording", "area": "orchestration", "kind": "control", "pattern": "'subagent' as product text", "experience": "A bug report about how subagent models are labeled is a code task. It needs no delegation.", "rule": "none", "privacy": "commit-safe", "setup": {"role": "top", "world": {"commits": [{"sha": "61b0e9d", "subject": "fix(web): label subagent messages with the subagent's model"}], "files": {"apps/web/src/subagentLabel.ts": "export function subagentModelLabel(run: Run, parent: Thread) {\n return run.model ?? parent.model;\n}\n"}}}, "prompt": "I recently fixed a bug where models were labeled wrong inside subagents that run through Claude Code. I think it came back in a different form. A Sol subagent shows the parent's model name in the UI. Find the cause and fix it.", "expect": {"notCalls": ["delegate_task", "orchestrator_capabilities", "t3_thread_launch", "create_threads", "Workflow"]}, "notes": ""} diff --git a/evals/mcp-tool-use/run.test.ts b/evals/mcp-tool-use/run.test.ts index 9cf9a6db484de..b0a5ed0d51e6c 100644 --- a/evals/mcp-tool-use/run.test.ts +++ b/evals/mcp-tool-use/run.test.ts @@ -104,7 +104,7 @@ it("passes a good run and names every rule a bad run breaks", () => { maxCalls: { task_status: 0 }, endsTurnAfter: "delegate_task", shellMustNot: ["\\bsleep\\b"], - textMust: ["t3-thread://v1/"], + textMust: ["t3-thread://v2/"], textMustNot: ["Paste .*token"], linked: ["~/pull/124"], }; @@ -117,7 +117,7 @@ it("passes a good run and names every rule a bad run breaks", () => { input: { task: "Prior findings: ...", target: { options: { reasoningEffort: "max" } } }, }, ], - text: "Started [Review](t3-thread://v1/11111111-1111-4111-8111-111111111111). Do not paste a token.", + text: "Started . Do not paste a token.", stopReason: "end_turn", linked: ["https://github.com/acme/app/pull/124"], }; @@ -146,7 +146,7 @@ it("passes a good run and names every rule a bad run breaks", () => { "maxCalls task_status <= 0", "endsTurnAfter delegate_task", "shellMustNot /\\bsleep\\b/", - "textMust /t3-thread://v1//", + "textMust /t3-thread://v2//", "textMustNot /Paste .*token/", "linked ~/pull/124", ]); diff --git a/evals/mcp-tool-use/run.ts b/evals/mcp-tool-use/run.ts index c1970086f7767..af5a6455f5022 100644 --- a/evals/mcp-tool-use/run.ts +++ b/evals/mcp-tool-use/run.ts @@ -33,7 +33,7 @@ import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/http"; import { taskPrompt } from "../../apps/server/src/mcp/delegatedTaskModes.ts"; import * as McpInvocationContext from "../../apps/server/src/mcp/McpInvocationContext.ts"; import { advertisedJsonSchema } from "../../apps/server/src/mcp/advertisedJsonSchema.ts"; -import { ORCHESTRATOR_MCP_RUNNING_TASK_NEXT } from "@t3tools/contracts"; +import { EnvironmentId, ORCHESTRATOR_MCP_RUNNING_TASK_NEXT, ThreadId } from "@t3tools/contracts"; import { projectComposerContextForProvider } from "@t3tools/shared/composerContextReferences"; import { formatThreadLink } from "@t3tools/shared/threadLinks"; import { buildCodexAdditionalContext } from "../../apps/server/src/provider/CodexDeveloperInstructions.ts"; @@ -893,14 +893,24 @@ const t3Default = (sample: Sample, name: string, input: Readonly { if (!isRecord(value)) return value; const linked = (thread: unknown, idKey = "threadId") => { if (!isRecord(thread) || thread.link !== undefined) return thread; const id = thread[idKey] ?? thread.id; - const title = valueText(thread.title) || "Thread"; - return typeof id === "string" ? { ...thread, link: formatThreadLink(id, title) } : thread; + return typeof id === "string" + ? { + ...thread, + link: formatThreadLink({ + environmentId: EVAL_ENVIRONMENT_ID, + threadId: ThreadId.make(id), + }), + } + : thread; }; const listed = (key: string) => Array.isArray(value[key]) ? { ...value, [key]: value[key].map((item) => linked(item)) } : value; @@ -915,6 +925,7 @@ const withProductFields = (name: string, value: unknown, child: boolean): unknow return { ...value, thread: linked(value.thread) }; case "t3_thread_launch": case "t3_thread_send": + case "run_scheduled_task_now": return linked(value); case "t3_thread_list": case "create_threads": diff --git a/packages/provider-core/src/server/orchestrationInstructions.ts b/packages/provider-core/src/server/orchestrationInstructions.ts index 5ce74766c4018..6737fdea693ef 100644 --- a/packages/provider-core/src/server/orchestrationInstructions.ts +++ b/packages/provider-core/src/server/orchestrationInstructions.ts @@ -15,7 +15,7 @@ Ordinary coding tasks need no T3 tools. - Given a thread id, \`t3_thread_read\` it before you answer about it. - Another machine: \`t3_environment_links\` lists linked ones. Pass its \`environmentId\` to a T3 tool to act there. Ids are then that machine's, so pass \`threadId\`. - A machine the user names that is not linked ("delegate this to my vps"): call \`t3_environment_link\` with a \`hint\` from their words. Never ask for a pairing code in chat. -- In your reply, a thread's first mention is its \`link\` verbatim. No \`link\`: \`[title](t3-thread://v1/)\`. Never a plain title or bare id. A thread on a linked machine: its title and machine, not its link. +- In your reply, mention a thread by its \`link\`, verbatim. Never a plain title or bare id. - A secret: call \`request_secret\` yourself. Pass its \`secretRef\` to a T3 tool, or use \`delivery: "file"\` for a shell command. Never ask in chat or invent one. When asked only to schedule, \`schedule_task\` without running its prompt. - T3 tools may load late. Try the name once before you call it absent.`, `### Visuals and preview diff --git a/packages/shared/src/markdownPipeline.test.ts b/packages/shared/src/markdownPipeline.test.ts index e23195f693afe..aa753e6303fc0 100644 --- a/packages/shared/src/markdownPipeline.test.ts +++ b/packages/shared/src/markdownPipeline.test.ts @@ -12,7 +12,7 @@ it("preserves in-app thread links through the shared Markdown sanitizer", () => .use(CHAT_MARKDOWN_REHYPE_PLUGINS); const tree = processor.runSync( processor.parse( - '[Open thread](t3-thread://v1/environment/thread)\n\nUnsafe', + '[Open thread](t3-thread://v1/thread)\n\n\n\nUnsafe', ), ); expect(tree.children[0]).toMatchObject({ @@ -20,11 +20,21 @@ it("preserves in-app thread links through the shared Markdown sanitizer", () => children: [ { tagName: "a", - properties: { href: "t3-thread://v1/environment/thread" }, + properties: { href: "t3-thread://v1/thread" }, children: [{ type: "text", value: "Open thread" }], }, ], }); + expect(tree.children[2]).toMatchObject({ + tagName: "p", + children: [ + { + tagName: "a", + properties: { href: "t3-thread://v2/environment/thread:a%3A1" }, + children: [{ type: "text", value: "t3-thread://v2/environment/thread:a%3A1" }], + }, + ], + }); expect(JSON.stringify(tree)).not.toContain("javascript:"); }); diff --git a/packages/shared/src/threadLinks.test.ts b/packages/shared/src/threadLinks.test.ts index a6c8eebc7e65d..82d981f59827c 100644 --- a/packages/shared/src/threadLinks.test.ts +++ b/packages/shared/src/threadLinks.test.ts @@ -1,53 +1,69 @@ import { describe, expect, it } from "vite-plus/test"; -import { EnvironmentId, ThreadId } from "@t3tools/contracts"; +import { EnvironmentId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts"; import { formatThreadLink, + guessThreadLinkRef, parseThreadLinkHref, relabelThreadLinks, - resolveThreadLinkRef, + threadLinkCandidates, + threadLinkRefs, } from "./threadLinks.ts"; const ref = (environmentId: string, threadId = "chat-1") => ({ environmentId: EnvironmentId.make(environmentId), threadId: ThreadId.make(threadId), }); +const v1 = (threadId: string) => ({ environmentId: null, threadId: ThreadId.make(threadId) }); +const local = EnvironmentId.make("local"); +const titlesIn = + (titles: Record>) => + ({ environmentId, threadId }: ScopedThreadRef) => + titles[environmentId]?.[threadId]; describe("thread link destinations", () => { - it("prefers the local literal id, then the local decoded id, over remote matches", () => { - const source = ref("local", "provider%3A1"); - const decoded = ref("local", "provider:1"); - const remote = ref("remote", "provider%3A1"); - expect(resolveThreadLinkRef(source, [remote, decoded, source])).toEqual(source); - expect(resolveThreadLinkRef(source, [remote, decoded])).toEqual(decoded); + it("names a v1 link's thread in the message's environment, as written and then decoded", () => { + expect(threadLinkCandidates(v1("provider%3A1"), local)).toEqual([ + ref("local", "provider%3A1"), + ref("local", "provider:1"), + ]); }); - it("opens the unique matching chat in another environment", () => { + it("opens the unique matching chat in another environment for a v1 link", () => { const destination = ref("remote"); - expect(resolveThreadLinkRef(ref("local"), [ref("local", "other-chat"), destination])).toEqual( + expect(guessThreadLinkRef(v1("chat-1"), [ref("local", "other-chat"), destination])).toEqual( destination, ); }); + it("opens a v2 link only in its own environment", () => { + const there = ref("there", "a%3A1"); + expect(threadLinkCandidates(there, local)).toEqual([there, ref("there", "a:1")]); + expect(guessThreadLinkRef(there, [ref("elsewhere", "a%3A1"), ref("elsewhere", "a:1")])).toBe( + null, + ); + }); + it("leaves ambiguous or missing destinations unresolved", () => { - const source = ref("local", "provider%3A1"); + const link = v1("provider%3A1"); const literal = ref("remote", "provider%3A1"); const decoded = ref("remote", "provider:1"); expect( - resolveThreadLinkRef(source, [literal, ref("other-remote", literal.threadId), decoded]), - ).toBeNull(); - expect( - resolveThreadLinkRef(source, [decoded, ref("other-remote", decoded.threadId)]), + guessThreadLinkRef(link, [literal, ref("other-remote", literal.threadId), decoded]), ).toBeNull(); - expect(resolveThreadLinkRef(source, [ref("remote", "other-chat")])).toBeNull(); + expect(guessThreadLinkRef(link, [decoded, ref("other-remote", decoded.threadId)])).toBeNull(); + expect(guessThreadLinkRef(link, [ref("remote", "other-chat")])).toBeNull(); }); }); describe("thread links", () => { - it("takes the thread id verbatim, percent escapes included", () => { - expect(parseThreadLinkHref("t3-thread://v1/mcp:1234")).toBe("mcp:1234"); - expect(parseThreadLinkHref("t3-thread://v1/thread:delegated-task:mcp%3A1")).toBe( - "thread:delegated-task:mcp%3A1", + it("takes the ids verbatim, percent escapes included", () => { + expect(parseThreadLinkHref("t3-thread://v1/mcp:1234")).toEqual(v1("mcp:1234")); + expect(parseThreadLinkHref("t3-thread://v1/thread:delegated-task:mcp%3A1")).toEqual( + v1("thread:delegated-task:mcp%3A1"), + ); + expect(parseThreadLinkHref("t3-thread://v2/env-1/thread:a/b%3A1")).toEqual( + ref("env-1", "thread:a/b%3A1"), ); }); @@ -55,59 +71,90 @@ describe("thread links", () => { expect(parseThreadLinkHref("https://t3.codes")).toBeNull(); expect(parseThreadLinkHref("t3-thread://v1/")).toBeNull(); expect(parseThreadLinkHref("t3-thread://v1/ ")).toBeNull(); + expect(parseThreadLinkHref("t3-thread://v2/env-1")).toBeNull(); + expect(parseThreadLinkHref("t3-thread://v2/env-1/")).toBeNull(); + expect(parseThreadLinkHref("t3-thread://v2//chat-1")).toBeNull(); }); it("resolves a percent-encoded id when the id as written names no thread", () => { - const titles = new Map([ - ["thread:project:1", "Decoded"], - ["provider%3A1", "Literal escape"], - ]); + const title = titlesIn({ local: { "thread:project:1": "Decoded", "provider%3A1": "Literal" } }); expect( relabelThreadLinks( "[a](t3-thread://v1/thread%3Aproject%3A1) [b](t3-thread://v1/provider%3A1)", - (threadId) => titles.get(threadId), + local, + title, ), - ).toBe( - "[Decoded](t3-thread://v1/thread:project:1) [Literal escape](t3-thread://v1/provider%3A1)", + ).toBe("[Decoded](t3-thread://v1/thread:project:1) [Literal](t3-thread://v1/provider%3A1)"); + const untitled = titlesIn({ local: { "a%3A1": "", "a:1": "Other" } }); + expect(relabelThreadLinks("[Kept](t3-thread://v1/a%3A1)", local, untitled)).toBe( + "[Kept](t3-thread://v1/a%3A1)", ); - const untitled = new Map([ - ["a%3A1", ""], - ["a:1", "Other"], - ]); + }); + + it("titles a v2 link from its own environment and writes it with that title", () => { + const title = titlesIn({ local: { t1: "Here" }, there: { t1: "There" } }); + expect( + relabelThreadLinks( + " and [old](t3-thread://v2/local/t1)", + local, + title, + ), + ).toBe("[There](t3-thread://v2/there/t1) and [Here](t3-thread://v2/local/t1)"); + }); + + it("turns a v2 link it cannot resolve into plain text, never the raw link", () => { expect( - relabelThreadLinks("[Kept](t3-thread://v1/a%3A1)", (threadId) => untitled.get(threadId)), - ).toBe("[Kept](t3-thread://v1/a%3A1)"); + relabelThreadLinks( + ", , [Old title](t3-thread://v2/there/t1)", + local, + () => undefined, + ), + ).toBe("Thread on another machine, Thread no longer available, Old title"); + expect(relabelThreadLinks("[Old](t3-thread://v1/t1)", local, () => undefined)).toBe( + "[Old](t3-thread://v1/t1)", + ); }); it("leaves links inside code spans and fences as written", () => { const markdown = [ "Live [old](t3-thread://v1/t1), literal `[old](t3-thread://v1/t1)`.", "```md", - "[old](t3-thread://v1/t1)", + "", "```", - "After [old](t3-thread://v1/t1)", + "After ", ].join("\n"); - expect(relabelThreadLinks(markdown, () => "New")).toBe( + expect(relabelThreadLinks(markdown, local, () => "New")).toBe( [ "Live [New](t3-thread://v1/t1), literal `[old](t3-thread://v1/t1)`.", "```md", - "[old](t3-thread://v1/t1)", + "", "```", - "After [New](t3-thread://v1/t1)", + "After [New](t3-thread://v2/local/t1)", ].join("\n"), ); }); - it("writes the thread id verbatim so the link opens that exact thread", () => { - const link = formatThreadLink("thread:delegated-task:mcp%3A1", "Fix"); - expect(link).toBe("[Fix](t3-thread://v1/thread:delegated-task:mcp%3A1)"); - expect(parseThreadLinkHref(link.slice(link.indexOf("](") + 2, -1))).toBe( - "thread:delegated-task:mcp%3A1", + it("lists the threads a message's links can name, outside code", () => { + expect( + threadLinkRefs( + " [a](t3-thread://v1/t%3A2) ``", + local, + ), + ).toEqual([ref("there", "t1"), ref("local", "t%3A2"), ref("local", "t:2")]); + }); + + it("writes a titleless autolink that opens that exact thread", () => { + const link = formatThreadLink(ref("env-1", "thread:delegated-task:mcp%3A1")); + expect(link).toBe(""); + expect(parseThreadLinkHref(link.slice(1, -1))).toEqual( + ref("env-1", "thread:delegated-task:mcp%3A1"), ); }); it("formats a label that would otherwise break the Markdown link", () => { - expect(formatThreadLink("t1", "Fix [ci] \\ build")).toBe("[Fix ci build](t3-thread://v1/t1)"); - expect(formatThreadLink("t1", " ] ")).toBe("[t1](t3-thread://v1/t1)"); + expect(formatThreadLink(ref("e", "t1"), "Fix [ci] \\ build")).toBe( + "[Fix ci build](t3-thread://v2/e/t1)", + ); + expect(formatThreadLink(ref("e", "t1"), " ] ")).toBe(""); }); }); diff --git a/packages/shared/src/threadLinks.ts b/packages/shared/src/threadLinks.ts index 20c64e1ec1a39..a71fe4489d65e 100644 --- a/packages/shared/src/threadLinks.ts +++ b/packages/shared/src/threadLinks.ts @@ -1,19 +1,36 @@ -import { ThreadId, type ScopedThreadRef } from "@t3tools/contracts"; +import { EnvironmentId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts"; import * as Option from "effect/Option"; import * as Schema from "effect/Schema"; export const THREAD_LINK_PROTOCOL = "t3-thread"; -const THREAD_LINK_HREF_PREFIX = `${THREAD_LINK_PROTOCOL}://v1/`; +const V1_PREFIX = `${THREAD_LINK_PROTOCOL}://v1/`; +const V2_PREFIX = `${THREAD_LINK_PROTOCOL}://v2/`; // Code comes first in the alternation so a link written inside a code span or fence is skipped. const THREAD_LINK_OUTSIDE_CODE = - /(?(`{3,}|~{3,})[\s\S]*?(?:\2|$))|(?(`+)[^\n]*?\4)|\[[^\]\n]*\]\((?t3-thread:\/\/v1\/[^\s)]+)\)/g; + /(?(`{3,}|~{3,})[\s\S]*?(?:\2|$))|(?(`+)[^\n]*?\4)|\[(?
    +
    { diff --git a/apps/web/src/components/settings/SourceControlAppSignInSettings.tsx b/apps/web/src/components/settings/SourceControlAppSignInSettings.tsx new file mode 100644 index 0000000000000..5662954ef7cd6 --- /dev/null +++ b/apps/web/src/components/settings/SourceControlAppSignInSettings.tsx @@ -0,0 +1,277 @@ +import type { EnvironmentId, SourceControlSignInPrompt } from "@t3tools/contracts"; +import { + readHostCredentialSettings, + type SourceControlAppSignIn, + type SourceControlDefinition, +} from "@t3tools/client-runtime/source-control-clients"; +import { useAtomValue } from "@effect/atom-react"; +import * as Cause from "effect/Cause"; +import { Check, Copy } from "lucide"; +import { CheckIcon, ExternalLinkIcon } from "lucide-react"; +import { useState, type ReactNode } from "react"; + +import { MorphIcon } from "~/components/MorphIcon"; +import { T3AppIcon } from "~/components/T3AppIcon"; +import { useCopyToClipboard } from "~/hooks/useCopyToClipboard"; +import { useEnvironmentSettings } from "../../hooks/useSettings"; +import { cn } from "../../lib/utils"; +import { serverEnvironment } from "../../state/server"; +import { sourceControlEnvironment } from "../../state/sourceControl"; +import { useAtomCommand } from "../../state/use-atom-command"; +import { Button, InlineButton } from "../ui/button"; +import { Spinner } from "../ui/spinner"; +import { RedactedSensitiveText } from "./RedactedSensitiveText"; + +type SignInState = + | { readonly status: "idle" } + | { readonly status: "starting" } + | { readonly status: "waiting"; readonly prompt: SourceControlSignInPrompt } + | { readonly status: "failed"; readonly message: string }; + +const failureMessage = (cause: Cause.Cause) => { + const error = Cause.squash(cause); + return error instanceof Error ? error.message : String(error); +}; + +const expiryTime = (expiresAt: number) => + new Date(expiresAt).toLocaleTimeString(undefined, { hour: "numeric", minute: "2-digit" }); + +function ExternalLink(props: { readonly href: string; readonly children: ReactNode }) { + return ( + }> + {props.children} + + + ); +} + +function AppMark(props: { readonly signedIn: boolean }) { + return ( +
    + + {props.signedIn ? ( + + + + ) : null} +
    + ); +} + +/** The device code, one cell per character, as GitHub prints it. */ +function DeviceCode(props: { readonly code: string }) { + return ( +
    + {props.code} + {Array.from(props.code, (char, index) => + char === "-" ? ( + + – + + ) : ( + + {char} + + ), + )} +
    + ); +} + +export function SourceControlAppSignInSettings({ + environmentId, + definition, + appSignIn, + account, + onChanged, +}: { + readonly environmentId: EnvironmentId; + readonly definition: SourceControlDefinition; + readonly appSignIn: SourceControlAppSignIn; + /** The login discovery reports, which is the signed-in account when no token overrides it. */ + readonly account: string | null; + readonly onChanged: () => void; +}) { + const host = appSignIn.hosts[0] ?? ""; + const { signIns, tokens } = useEnvironmentSettings(environmentId, (settings) => + readHostCredentialSettings(settings, definition.kind), + ); + const signedIn = (signIns[host] ?? "").length > 0; + const tokenOverrides = (tokens[host] ?? "").length > 0; + const canSignIn = useAtomValue( + sourceControlEnvironment.startSignIn.permissionAtom(environmentId), + ); + const start = useAtomCommand(sourceControlEnvironment.startSignIn, { reportFailure: false }); + const wait = useAtomCommand(sourceControlEnvironment.waitForSignIn, { reportFailure: false }); + const cancel = useAtomCommand(sourceControlEnvironment.cancelSignIn, { reportFailure: false }); + const updateSettings = useAtomCommand(serverEnvironment.updateSettings, { + label: `sign out of ${appSignIn.appName}`, + }); + const { copyToClipboard, isCopied } = useCopyToClipboard({ + timeout: 1500, + target: "sign-in code", + }); + const [state, setState] = useState({ status: "idle" }); + + const signIn = async () => { + setState({ status: "starting" }); + const input = { provider: definition.kind, host }; + const started = await start({ environmentId, input }); + if (started._tag !== "Success") { + setState({ status: "failed", message: failureMessage(started.cause) }); + return; + } + const prompt = started.value; + setState({ status: "waiting", prompt }); + const finished = await wait({ environmentId, input }); + // A newer attempt, a cancel, or a sign-out owns the state by now. + setState((current) => { + if (current.status !== "waiting" || current.prompt !== prompt) return current; + return finished._tag === "Success" + ? { status: "idle" } + : { status: "failed", message: failureMessage(finished.cause) }; + }); + if (finished._tag === "Success") onChanged(); + }; + + const signOut = async () => { + setState({ status: "idle" }); + const result = await updateSettings({ + environmentId, + // Send the whole map; other hosts' redaction markers preserve their sessions. + input: { + patch: { + sourceControlHosts: { [definition.kind]: { signIns: { ...signIns, [host]: "" } } }, + }, + }, + }); + if (result._tag === "Success") onChanged(); + }; + + const waiting = !signedIn && state.status === "waiting" ? state.prompt : null; + + let subtitle: ReactNode; + let action: ReactNode; + if (signedIn) { + subtitle = + account !== null && !tokenOverrides ? ( + + Signed in as + + + ) : tokenOverrides ? ( + "Signed in, but the token saved below is used until you remove it." + ) : ( + "Signed in" + ); + action = ( + + ); + } else if (waiting !== null) { + subtitle = "Approve the app on GitHub to finish signing in."; + action = ( + + ); + } else { + subtitle = "A far higher GitHub API rate limit than a personal token or gh login."; + action = ( + + ); + } + + return ( +
    +
    + +
    +

    {appSignIn.appName}

    +
    {subtitle}
    +
    +
    {action}
    +
    + + {waiting !== null ? ( +
    +
    + + +
    +
    + +

    + + Waiting for approval · code expires at {expiryTime(waiting.expiresAt)} +

    +
    +
    + ) : null} + + {signedIn ? ( +
    + + Reaches repositories in accounts and organizations that installed the app. + + Install on an organization + Manage on GitHub +
    + ) : null} + + {state.status === "failed" && !signedIn ? ( +

    + {state.message} +

    + ) : null} +
    + ); +} diff --git a/apps/web/src/components/settings/SourceControlSettings.tsx b/apps/web/src/components/settings/SourceControlSettings.tsx index cdf31a835ecd0..7f7b0a90ebcc5 100644 --- a/apps/web/src/components/settings/SourceControlSettings.tsx +++ b/apps/web/src/components/settings/SourceControlSettings.tsx @@ -32,6 +32,7 @@ import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; import { GitIcon, JujutsuIcon, type Icon } from "../Icons"; import { SourceControlHostSettings } from "./SourceControlHostSettings"; import { SourceControlAccountSettings } from "./SourceControlAccountSettings"; +import { SourceControlAppSignInSettings } from "./SourceControlAppSignInSettings"; import { SourceControlTokenSettings } from "./SourceControlTokenSettings"; import { RedactedSensitiveText } from "./RedactedSensitiveText"; import { SourceControlWritingSettingsSection } from "./SourceControlWritingSettings"; @@ -391,6 +392,23 @@ export function SourceControlSettingsPanel() { return (
    + {credentials.appSignIn && item.appSignIn === true ? ( + + ) : null} + {credentials.appSignIn && item.appSignIn === true ? ( +
    + + Or use your own credentials + +
    + ) : null} { + const target = appSignInTargetForRemote(remoteUrl); + if (target === null) return; + const result = await runAtomCommand( + appAtomRegistry, + sourceControlEnvironment.checkAppAccess, + { environmentId, input: target.input }, + { label: "check app access", reportFailure: false, reportDefect: false }, + ); + if (result._tag !== "Success" || result.value.status !== "not-covered") return; + const owner = target.input.repository.split("/")[0] ?? target.input.repository; + const id = toastManager.add({ + type: "info", + title: `Give ${target.appSignIn.appName} access to ${owner}`, + description: `It isn't installed there, so GitHub requests for ${target.input.repository} use your own login and its lower rate limit.`, + actionProps: { + children: "Install", + onClick: () => { + toastManager.close(id); + window.open(target.appSignIn.installUrl, "_blank", "noopener,noreferrer"); + }, + }, + }); +} diff --git a/apps/web/src/hooks/useNewProject.ts b/apps/web/src/hooks/useNewProject.ts index 496991daeeb93..989ac9f757d1a 100644 --- a/apps/web/src/hooks/useNewProject.ts +++ b/apps/web/src/hooks/useNewProject.ts @@ -15,6 +15,7 @@ import { projectEnvironment } from "~/state/projects"; import { sourceControlEnvironment } from "~/state/sourceControl"; import { useAtomCommand } from "~/state/use-atom-command"; import { useNewThreadHandler } from "./useHandleNewThread"; +import { suggestAppAccessForRemote } from "../components/sourceControlAppAccessToast"; function errorMessage(error: unknown): string { return error instanceof Error && error.message.trim().length > 0 @@ -63,6 +64,7 @@ export function useNewProject() { description: result.value.repository.nameWithOwner, }), ); + void suggestAppAccessForRemote(input.environmentId, result.value.remoteUrl); }; return async (input: { diff --git a/apps/web/src/lib/addProjectToMachine.ts b/apps/web/src/lib/addProjectToMachine.ts index bccfc9d84910e..0ef0467b934ea 100644 --- a/apps/web/src/lib/addProjectToMachine.ts +++ b/apps/web/src/lib/addProjectToMachine.ts @@ -13,6 +13,7 @@ import type { import { create } from "zustand"; import { appAtomRegistry } from "../rpc/atomRegistry"; +import { suggestAppAccessForRemote } from "../components/sourceControlAppAccessToast"; import { environmentProjectClonesAtom, silenceProjectCloneToasts } from "../state/projectClones"; import { projectEnvironment } from "../state/projects"; import { sourceControlEnvironment } from "../state/sourceControl"; @@ -143,6 +144,7 @@ export async function addProjectToMachine(input: AddProjectToMachineInput): Prom setFileCopy(copyKey, null); throw new Error(failureMessage(started)); } + void suggestAppAccessForRemote(input.target.environmentId, input.remoteUrl); if (reads.length === 0) return; void (async () => { diff --git a/apps/web/src/rpc/requestLatencyState.ts b/apps/web/src/rpc/requestLatencyState.ts index 88990a4521339..c95ddae2afcde 100644 --- a/apps/web/src/rpc/requestLatencyState.ts +++ b/apps/web/src/rpc/requestLatencyState.ts @@ -27,10 +27,12 @@ interface PendingRpcAckRequest { const pendingRpcAckRequests = new Map(); // Background reports nobody waits on. A busy host delays them first, and warning -// about a request the user never made does not say what is wrong. +// about a request the user never made does not say what is wrong. A sign-in wait +// lasts as long as the user takes on the forge. const untrackedRpcAckMethods = new Set([ WS_METHODS.serverGetUsageSummary, WS_METHODS.serverReportClientActivity, + WS_METHODS.sourceControlSignInWait, ]); const longRunningRpcAckMethods = new Set([ WS_METHODS.serverUpdateProvider, diff --git a/apps/web/src/state/sourceControlActions.test.ts b/apps/web/src/state/sourceControlActions.test.ts index be47b4b9eb6fa..e4a05b8c3da40 100644 --- a/apps/web/src/state/sourceControlActions.test.ts +++ b/apps/web/src/state/sourceControlActions.test.ts @@ -19,6 +19,9 @@ vi.mock("@effect/atom-react", () => ({ useAtomValue: () => ({ operation: null, error: null, isRunning: false }), })); vi.mock("../rpc/atomRegistry", () => ({ appAtomRegistry: {} })); +vi.mock("../components/sourceControlAppAccessToast", () => ({ + suggestAppAccessForRemote: () => Promise.resolve(), +})); vi.mock("./session", () => ({ useEnvironmentScope: (environmentId: string, scope: string) => state.scopes.get(environmentId)?.has(scope) === true, diff --git a/apps/web/src/state/sourceControlActions.ts b/apps/web/src/state/sourceControlActions.ts index 12b9aff67159b..dc21df5782468 100644 --- a/apps/web/src/state/sourceControlActions.ts +++ b/apps/web/src/state/sourceControlActions.ts @@ -31,6 +31,7 @@ import { readEnvironmentScope, useEnvironmentScope } from "./session"; import { sourceControlEnvironment } from "./sourceControl"; import { useAtomCommand } from "./use-atom-command"; import { vcsActionManager, vcsEnvironment } from "./vcs"; +import { suggestAppAccessForRemote } from "../components/sourceControlAppAccessToast"; export type SourceControlActionKind = | "init" @@ -286,13 +287,17 @@ export function useSourceControlPublishRepositoryAction(scope: SourceControlActi ), ); } - return publishRepository({ + const result = await publishRepository({ environmentId: target.environmentId, input: { cwd: target.cwd, ...input, }, }); + if (result._tag === "Success") { + void suggestAppAccessForRemote(target.environmentId, result.value.remoteUrl); + } + return result; }; return useAction({ kind: "publishRepository", diff --git a/packages/client-runtime/src/sourceControlClients.test.ts b/packages/client-runtime/src/sourceControlClients.test.ts index 6954dd979cc8d..359da39b33b04 100644 --- a/packages/client-runtime/src/sourceControlClients.test.ts +++ b/packages/client-runtime/src/sourceControlClients.test.ts @@ -1,7 +1,7 @@ import { ProjectId, SourceControlProviderKind } from "@t3tools/contracts"; import { describe, expect, it } from "vite-plus/test"; -import { sourceControlClients } from "./sourceControlClients.ts"; +import { appSignInTargetForRemote, sourceControlClients } from "./sourceControlClients.ts"; import { normalizeThreadPullRequestKey } from "./threadPullRequests.ts"; const parse = (url: string) => { @@ -366,3 +366,23 @@ describe("sourceControlClients.findByPublicDomain", () => { expect(sourceControlClients.findByPublicHost("gist.github.com")).toBeUndefined(); }); }); + +describe("appSignInTargetForRemote", () => { + it("names the repository a GitHub remote points at, in the remote's casing", () => { + for (const remote of [ + "https://github.com/octocat/Hello-World.git", + "git@github.com:octocat/Hello-World.git", + ]) { + expect(appSignInTargetForRemote(remote)?.input).toEqual({ + provider: "github", + host: "github.com", + repository: "octocat/Hello-World", + }); + } + }); + + it("skips hosts the app is not registered on", () => { + expect(appSignInTargetForRemote("https://ghe.acme.test/acme/web.git")).toBeNull(); + expect(appSignInTargetForRemote("https://gitlab.com/acme/web.git")).toBeNull(); + }); +}); diff --git a/packages/client-runtime/src/sourceControlClients.ts b/packages/client-runtime/src/sourceControlClients.ts index c7db3db4afc56..21cb1530aae30 100644 --- a/packages/client-runtime/src/sourceControlClients.ts +++ b/packages/client-runtime/src/sourceControlClients.ts @@ -2,8 +2,11 @@ import { builtInSourceControlDefinitions } from "@t3tools/source-control-definit import { HostCredentialSettings, readSourceControlHostSettings, + type SourceControlAppSignIn, + type SourceControlDefinition, } from "@t3tools/source-control-core/shared/definition"; -import type { ServerSettings } from "@t3tools/contracts"; +import type { ServerSettings, SourceControlAppAccessInput } from "@t3tools/contracts"; +import { parseGitRemoteRepository } from "@t3tools/shared/git"; export { isMonochromeSourceControlIcon, @@ -14,6 +17,7 @@ export { type HostCredentialSettings, type ParsedChangeRequestUrl, type ReferenceAutolink, + type SourceControlAppSignIn, type SourceControlDefinition, type SourceControlHostCredentials, type SourceControlIcon, @@ -33,3 +37,30 @@ export function readHostCredentialSettings( export function capitalizeTerm(term: string): string { return term.charAt(0).toUpperCase() + term.slice(1); } + +/** The repository a remote names, when its host has an app sign-in that could reach it. */ +export function appSignInTargetForRemote(remoteUrl: string): { + readonly definition: SourceControlDefinition; + readonly appSignIn: SourceControlAppSignIn; + readonly input: SourceControlAppAccessInput; +} | null { + const parsed = parseGitRemoteRepository(remoteUrl); + const provider = sourceControlClients.recognizeRemote(remoteUrl); + const definition = provider ? sourceControlClients.find(provider.kind) : undefined; + const appSignIn = definition?.credentials?.appSignIn; + if (parsed === null || definition === undefined || appSignIn === undefined) return null; + if (!appSignIn.hosts.includes(parsed.host)) return null; + // The parser lowercases; keep the remote's own casing for display when it names the same path. + const asWritten = remoteUrl + .trim() + .replace(/\.git\/?$/i, "") + .split(/[/:]/) + .slice(-2) + .join("/"); + const repository = asWritten.toLowerCase() === parsed.path ? asWritten : parsed.path; + return { + definition, + appSignIn, + input: { provider: definition.kind, host: parsed.host, repository }, + }; +} diff --git a/packages/client-runtime/src/state/sourceControl.ts b/packages/client-runtime/src/state/sourceControl.ts index d63fae293a54c..dad3ad6a1c247 100644 --- a/packages/client-runtime/src/state/sourceControl.ts +++ b/packages/client-runtime/src/state/sourceControl.ts @@ -21,6 +21,15 @@ export function createSourceControlEnvironmentAtoms( label: "environment-data:server:source-control-discovery", tag: WS_METHODS.serverDiscoverSourceControl, }), + appAccess: createEnvironmentRpcQueryAtomFamily(runtime, { + label: "environment-data:source-control:app-access", + tag: WS_METHODS.sourceControlAppAccess, + }), + // One check, as after cloning, where no view keeps the query mounted. + checkAppAccess: createEnvironmentRpcCommand(runtime, { + label: "environment-data:source-control:check-app-access", + tag: WS_METHODS.sourceControlAppAccess, + }), repository: createEnvironmentRpcQueryAtomFamily(runtime, { label: "environment-data:source-control:repository", tag: WS_METHODS.sourceControlLookupRepository, @@ -56,6 +65,19 @@ export function createSourceControlEnvironmentAtoms( key: ({ environmentId }) => environmentId, }, }), + // Unscheduled: a pending sign-in waits on the user for minutes and must not hold up clones. + startSignIn: createEnvironmentRpcCommand(runtime, { + label: "environment-data:source-control:sign-in-start", + tag: WS_METHODS.sourceControlSignInStart, + }), + waitForSignIn: createEnvironmentRpcCommand(runtime, { + label: "environment-data:source-control:sign-in-wait", + tag: WS_METHODS.sourceControlSignInWait, + }), + cancelSignIn: createEnvironmentRpcCommand(runtime, { + label: "environment-data:source-control:sign-in-cancel", + tag: WS_METHODS.sourceControlSignInCancel, + }), projectClones: createEnvironmentRpcSubscriptionAtomFamily(runtime, { label: "environment-data:source-control:project-clones", tag: WS_METHODS.subscribeProjectClones, diff --git a/packages/contracts/src/clientRpcPermissions.ts b/packages/contracts/src/clientRpcPermissions.ts index af9407760046e..3cd80feea6b73 100644 --- a/packages/contracts/src/clientRpcPermissions.ts +++ b/packages/contracts/src/clientRpcPermissions.ts @@ -35,6 +35,10 @@ export const CLIENT_GUARDED_RPC_SCOPES = { [WS_METHODS.pullRequestsGenerateReviewGuide]: AuthOrchestrationOperateScope, [WS_METHODS.sourceControlCloneRepository]: AuthSourceControlWriteScope, [WS_METHODS.sourceControlPublishRepository]: AuthSourceControlWriteScope, + // Signing in replaces the server's GitHub credential, as saving a token in Settings does. + [WS_METHODS.sourceControlSignInStart]: AuthSettingsWriteScope, + [WS_METHODS.sourceControlSignInWait]: AuthSettingsWriteScope, + [WS_METHODS.sourceControlSignInCancel]: AuthSettingsWriteScope, [WS_METHODS.projectCloneStart]: AuthSourceControlWriteScope, [WS_METHODS.projectCloneCancel]: AuthSourceControlWriteScope, [WS_METHODS.projectCloneRetry]: AuthSourceControlWriteScope, diff --git a/packages/contracts/src/rpc.ts b/packages/contracts/src/rpc.ts index 2122869c09715..e4823f30272bd 100644 --- a/packages/contracts/src/rpc.ts +++ b/packages/contracts/src/rpc.ts @@ -382,6 +382,12 @@ import { SourceControlRepositoryError, SourceControlRepositoryInfo, SourceControlRepositoryLookupInput, + SourceControlAppAccess, + SourceControlAppAccessInput, + SourceControlSignInError, + SourceControlSignInInput, + SourceControlSignInPrompt, + SourceControlSignInResult, } from "./sourceControl.ts"; import { VcsError } from "./vcs.ts"; import { Project, ProjectMutation, ProjectMutationError } from "./project.ts"; @@ -567,6 +573,10 @@ export const WS_METHODS = { sourceControlLookupRepository: "sourceControl.lookupRepository", sourceControlCloneRepository: "sourceControl.cloneRepository", sourceControlPublishRepository: "sourceControl.publishRepository", + sourceControlSignInStart: "sourceControl.signInStart", + sourceControlSignInWait: "sourceControl.signInWait", + sourceControlSignInCancel: "sourceControl.signInCancel", + sourceControlAppAccess: "sourceControl.appAccess", projectCloneStart: "projectClone.start", projectCloneCancel: "projectClone.cancel", projectCloneRetry: "projectClone.retry", @@ -1182,6 +1192,33 @@ const WsSourceControlCloneRepositoryRpc = Rpc.make(WS_METHODS.sourceControlClone error: Schema.Union([SourceControlRepositoryError, EnvironmentAuthorizationError]), }); +// Starts a device-flow sign-in the server finishes on its own, so a client can leave meanwhile. +const WsSourceControlSignInStartRpc = Rpc.make(WS_METHODS.sourceControlSignInStart, { + payload: SourceControlSignInInput, + success: SourceControlSignInPrompt, + error: Schema.Union([SourceControlSignInError, EnvironmentAuthorizationError]), +}); + +// Resolves when the sign-in `signInStart` began ends, however long the user takes. +const WsSourceControlSignInWaitRpc = Rpc.make(WS_METHODS.sourceControlSignInWait, { + payload: SourceControlSignInInput, + success: SourceControlSignInResult, + error: Schema.Union([SourceControlSignInError, EnvironmentAuthorizationError]), +}); + +// Ends the sign-in `signInStart` began, so approving the code afterwards signs nothing in. +const WsSourceControlSignInCancelRpc = Rpc.make(WS_METHODS.sourceControlSignInCancel, { + payload: SourceControlSignInInput, + success: Schema.Void, + error: Schema.Union([SourceControlSignInError, EnvironmentAuthorizationError]), +}); + +const WsSourceControlAppAccessRpc = Rpc.make(WS_METHODS.sourceControlAppAccess, { + payload: SourceControlAppAccessInput, + success: SourceControlAppAccess, + error: EnvironmentAuthorizationError, +}); + // Returns when the project exists and cloning starts; progress uses the subscription. const WsProjectCloneStartRpc = Rpc.make(WS_METHODS.projectCloneStart, { payload: ProjectCloneStartInput, @@ -1981,6 +2018,10 @@ export const WsRpcGroup = RpcGroup.make( WsSourceControlLookupRepositoryRpc, WsSourceControlCloneRepositoryRpc, WsSourceControlPublishRepositoryRpc, + WsSourceControlSignInStartRpc, + WsSourceControlSignInWaitRpc, + WsSourceControlSignInCancelRpc, + WsSourceControlAppAccessRpc, WsProjectCloneStartRpc, WsProjectCloneCancelRpc, WsProjectCloneRetryRpc, diff --git a/packages/contracts/src/sourceControl.ts b/packages/contracts/src/sourceControl.ts index e0713b2425a29..6ffb4b97066f2 100644 --- a/packages/contracts/src/sourceControl.ts +++ b/packages/contracts/src/sourceControl.ts @@ -155,6 +155,8 @@ export const SourceControlProviderDiscoveryItem = Schema.Struct({ kind: SourceControlProviderKind, ...SourceControlDiscoverySharedFields, auth: SourceControlProviderAuth, + /** The server is configured to sign in through the host's app; clients offer it only then. */ + appSignIn: Schema.optionalKey(Schema.Boolean), }); export type SourceControlProviderDiscoveryItem = typeof SourceControlProviderDiscoveryItem.Type; @@ -182,6 +184,63 @@ export class SourceControlProviderError extends Schema.TaggedError()( + "SourceControlSignInError", + { + provider: SourceControlProviderKind, + host: Schema.String, + reason: SourceControlSignInFailure, + detail: Schema.String, + cause: Schema.optional(Schema.Defect()), + }, +) { + override get message(): string { + return this.detail; + } +} + export class SourceControlRepositoryError extends Schema.TaggedError()( "SourceControlRepositoryError", { diff --git a/packages/source-control-core/src/server/PullRequestProvider.ts b/packages/source-control-core/src/server/PullRequestProvider.ts index ce1a72c1aedf6..b300f14afb0cb 100644 --- a/packages/source-control-core/src/server/PullRequestProvider.ts +++ b/packages/source-control-core/src/server/PullRequestProvider.ts @@ -268,7 +268,8 @@ export interface ProviderRepositoryRef { export interface PullRequestProviderApi { readonly withVerifiedCredential?: ( - input: { readonly cwd: string; readonly host: string }, + /** `repository` lets a host pick the credential that can reach it. */ + input: { readonly cwd: string; readonly host: string; readonly repository?: string }, use: (identity: { readonly accountId: string; readonly viewer: string; diff --git a/packages/source-control-core/src/server/SourceControlHost.ts b/packages/source-control-core/src/server/SourceControlHost.ts index eb68cc4730c80..79efb54b26a5d 100644 --- a/packages/source-control-core/src/server/SourceControlHost.ts +++ b/packages/source-control-core/src/server/SourceControlHost.ts @@ -2,6 +2,7 @@ import type { GitCommandError, ServerSettings, ServerSettingsError, + ServerSettingsPatch, VcsError, VcsListRemotesResult, VcsProcessExitFailureKind, @@ -67,6 +68,10 @@ export class SourceControlHost extends Context.Service< readonly settings: { /** Read fresh on each call to pick up credential changes. */ readonly get: Effect.Effect; + /** For credentials the host obtains itself, such as a sign-in it refreshes. */ + readonly update: ( + patch: ServerSettingsPatch, + ) => Effect.Effect; }; readonly process: { readonly run: ( diff --git a/packages/source-control-core/src/server/driver.ts b/packages/source-control-core/src/server/driver.ts index 81d3c03538885..c25179fa2d440 100644 --- a/packages/source-control-core/src/server/driver.ts +++ b/packages/source-control-core/src/server/driver.ts @@ -1,4 +1,10 @@ -import type { SourceControlProviderKind } from "@t3tools/contracts"; +import type { + SourceControlAppAccess, + SourceControlProviderKind, + SourceControlSignInError, + SourceControlSignInPrompt, + SourceControlSignInResult, +} from "@t3tools/contracts"; import type * as Crypto from "effect/Crypto"; import * as Effect from "effect/Effect"; import type * as FileSystem from "effect/FileSystem"; @@ -21,6 +27,25 @@ export interface SourceControlMediaRequest { readonly headers: Readonly>; } +/** The server side of a definition's `credentials.appSignIn`. */ +export interface SourceControlSignIn { + /** Replaces any sign-in already pending for `host`; the driver finishes it on its own. */ + readonly start: ( + host: string, + ) => Effect.Effect; + /** The outcome of the latest sign-in `start` began for `host`, waiting for it if pending. */ + readonly wait: ( + host: string, + ) => Effect.Effect; + /** Ends the sign-in pending for `host`; its `wait` fails as cancelled. */ + readonly cancel: (host: string) => Effect.Effect; + /** Whether the sign-in reaches `owner/name`, so clients can suggest installing the app there. */ + readonly access: ( + host: string, + repository: string, + ) => Effect.Effect; +} + export interface SourceControlDriverInstance { readonly sourceControl: SourceControlProvider.SourceControlProvider["Service"]; readonly discovery: SourceControlProviderDiscoverySpec; @@ -37,6 +62,7 @@ export interface SourceControlDriverInstance { HttpClientError.HttpClientError, Scope.Scope >; + readonly signIn?: SourceControlSignIn; } /** What the server gives every driver. A driver builds anything else it needs itself. */ diff --git a/packages/source-control-core/src/shared/definition.ts b/packages/source-control-core/src/shared/definition.ts index 6a9f3d23bc6f6..6f99525260129 100644 --- a/packages/source-control-core/src/shared/definition.ts +++ b/packages/source-control-core/src/shared/definition.ts @@ -166,6 +166,23 @@ export interface SourceControlHostCredentials { /** What a token must be granted, completing "Give it …". */ readonly tokenAccess: string; readonly newTokenUrl: (host: string) => string; + /** Signing in through the forge's own app instead of a token or CLI login. */ + readonly appSignIn?: SourceControlAppSignIn; +} + +/** + * A forge app the server signs in to with a device code, on the hosts it is registered on. The + * session it keeps is the `signIns` setting, which takes precedence over everything but a saved + * token. + */ +export interface SourceControlAppSignIn { + /** The app's name on the forge, as the consent screen shows it. */ + readonly appName: string; + readonly hosts: ReadonlyArray; + /** Where an account or organization installs the app so it can reach its repositories. */ + readonly installUrl: string; + /** Where the user revokes the app's access entirely. */ + readonly revokeUrl: string; } export const SourceControlHostName = TrimmedNonEmptyString.pipe( @@ -189,6 +206,12 @@ export const hostCredentialSettingsFields = { secretSetting, Schema.annotateKey({ providerSettingsForm: { hidden: true } }), ), + /** The serialized session of an `appSignIn`, owned by the server's driver. */ + signIns: Schema.Record(SourceControlHostName, TrimmedString).pipe( + Schema.withDecodingDefault(Effect.succeed({})), + secretSetting, + Schema.annotateKey({ providerSettingsForm: { hidden: true } }), + ), }; export const HostCredentialSettings = Schema.Struct(hostCredentialSettingsFields); diff --git a/packages/source-control-github/scripts/measure-pr-preview.ts b/packages/source-control-github/scripts/measure-pr-preview.ts index d1c97074e16a2..6f03586958cca 100644 --- a/packages/source-control-github/scripts/measure-pr-preview.ts +++ b/packages/source-control-github/scripts/measure-pr-preview.ts @@ -13,6 +13,7 @@ import * as Stream from "effect/Stream"; import { FetchHttpClient, HttpClient } from "effect/http"; import { ChildProcess, ChildProcessSpawner } from "effect/process"; +import * as GitHubAppAccess from "../src/server/GitHubAppAccess.ts"; import * as GitHubPullRequestApi from "../src/server/GitHubPullRequestApi.ts"; import * as GitHubPullRequestProvider from "../src/server/GitHubPullRequestProvider.ts"; import * as GitHubRest from "../src/server/GitHubRest.ts"; @@ -99,6 +100,7 @@ const sourceControlHost = Layer.unwrap( ).pipe(Layer.provideMerge(NodeServices.layer)); const services = GitHubPullRequestApi.layer.pipe( + Layer.provideMerge(GitHubAppAccess.layer), Layer.provideMerge(GitHubRest.layerWithDependencies), Layer.provideMerge(SourceControlRateLimit.layer), Layer.provideMerge(measuredHttp), diff --git a/packages/source-control-github/src/server/GitHubAppAccess.ts b/packages/source-control-github/src/server/GitHubAppAccess.ts new file mode 100644 index 0000000000000..39aa08c5e9ccf --- /dev/null +++ b/packages/source-control-github/src/server/GitHubAppAccess.ts @@ -0,0 +1,216 @@ +import * as Cache from "effect/Cache"; +import * as Context from "effect/Context"; +import * as Duration from "effect/Duration"; +import * as Effect from "effect/Effect"; +import * as Exit from "effect/Exit"; +import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; +import * as Schema from "effect/Schema"; + +import * as GitHubAppSignIn from "./GitHubAppSignIn.ts"; +import * as GitHubRequestPolicy from "./GitHubRequestPolicy.ts"; +import * as GitHubRest from "./GitHubRest.ts"; + +const Installations = Schema.fromJsonString( + Schema.Struct({ + installations: Schema.Array( + Schema.Struct({ + id: Schema.Number, + repository_selection: Schema.String, + account: Schema.NullOr(Schema.Struct({ login: Schema.String })), + }), + ), + }), +); + +const InstallationRepositories = Schema.fromJsonString( + Schema.Struct({ repositories: Schema.Array(Schema.Struct({ full_name: Schema.String })) }), +); + +const Viewer = Schema.fromJsonString(Schema.Struct({ login: Schema.String })); + +const PAGE_SIZE = 100; +const MAX_PAGES = 10; +// Installing the app on another account should show up soon after the user comes back. +const COVERAGE_TTL = Duration.minutes(2); +// A failed read answers `covered`; keep that briefly so an outage is not re-asked per request. +const COVERAGE_FAILURE_TTL = Duration.seconds(30); + +/** Whether the app sign-in reaches a repository; `signed-out` when the server is not signed in. */ +export type GitHubAppReach = "covered" | "not-covered" | "signed-out"; + +/** Accounts that installed the app for all repositories, and repositories listed one by one. */ +interface AppCoverage { + readonly accounts: ReadonlySet; + readonly repositories: ReadonlySet; +} + +export class GitHubAppAccess extends Context.Service< + GitHubAppAccess, + { + /** + * Whether an installation covers `owner/name`. A check that fails answers `covered`, so a + * GitHub hiccup does not nag the user. + */ + readonly reach: (host: string, repository: string) => Effect.Effect; + /** The login the app sign-in acts as. */ + readonly viewer: (host: string) => Effect.Effect; + /** + * The credential for work pinned to one repository: the app's only where an installation is + * confirmed to cover it, the user's own otherwise when there is one. + */ + readonly credentialFor: ( + host: string, + repository: string, + ) => Effect.Effect< + GitHubRequestPolicy.GitHubHeldCredential, + GitHubRequestPolicy.GitHubApiError + >; + } +>()("@t3tools/source-control-github/server/GitHubAppAccess") {} + +/** Callers may name a repository as a URL, `host/owner/name` or `owner/name.git`. */ +function ownerAndName(repository: string): string { + return repository + .trim() + .toLowerCase() + .replace(/\.git\/?$/, "") + .split(/[/:]/) + .filter((segment) => segment.length > 0) + .slice(-2) + .join("/"); +} + +/** @public */ +export const make = Effect.gen(function* () { + const api = yield* GitHubRest.GitHubRest; + const appSignIn = yield* GitHubAppSignIn.GitHubAppSignIn; + + /** Reads `path` as the app sign-in, whatever credential outranks it elsewhere. */ + const readAsApp = Effect.fn("GitHubAppAccess.readAsApp")(function* ( + host: string, + operation: string, + path: string, + schema: Schema.Codec, + ) { + const app = yield* api.appCredential(host); + if (Option.isNone(app)) return Option.none(); + const response = yield* api.rest({ host, operation, path, allowReserve: true }).pipe( + Effect.provideService(GitHubRequestPolicy.PinnedGitHubCredential, { + host, + token: app.value.token, + credentialFingerprint: app.value.fingerprint, + }), + ); + return Option.some(yield* Schema.decodeEffect(schema)(response.body)); + }); + + const coverageOf = Effect.fn("GitHubAppAccess.coverageOf")(function* (host: string) { + const installations: Array<{ + readonly id: number; + readonly repository_selection: string; + readonly account: { readonly login: string } | null; + }> = []; + for (let page = 1; page <= MAX_PAGES; page++) { + const listed = yield* readAsApp( + host, + "appInstallations", + `user/installations?per_page=${PAGE_SIZE}&page=${page}`, + Installations, + ); + if (Option.isNone(listed)) return null; + installations.push(...listed.value.installations); + if (listed.value.installations.length < PAGE_SIZE) break; + } + const accounts = new Set(); + const repositories = new Set(); + for (const installation of installations) { + const login = installation.account?.login.toLowerCase(); + if (login === undefined) continue; + if (installation.repository_selection === "all") { + accounts.add(login); + continue; + } + for (let page = 1; ; page++) { + // Past the cap the list is cut short; count the whole account as reached rather than nag. + if (page > MAX_PAGES) { + accounts.add(login); + break; + } + const listed = yield* readAsApp( + host, + "appInstallationRepositories", + `user/installations/${installation.id}/repositories?per_page=${PAGE_SIZE}&page=${page}`, + InstallationRepositories, + ); + if (Option.isNone(listed)) return null; + for (const repository of listed.value.repositories) { + repositories.add(repository.full_name.toLowerCase()); + } + if (listed.value.repositories.length < PAGE_SIZE) break; + } + } + return { accounts, repositories } satisfies AppCoverage; + }); + + const coverage = yield* Cache.makeWith(coverageOf, { + capacity: 8, + timeToLive: (exit) => (Exit.isSuccess(exit) ? COVERAGE_TTL : COVERAGE_FAILURE_TTL), + }); + + /** `unknown` when GitHub could not list the installations just now. */ + const coverageFor = Effect.fn("GitHubAppAccess.coverageFor")(function* ( + rawHost: string, + rawRepository: string, + ) { + const host = rawHost.trim().toLowerCase(); + if (!appSignIn.enabled) return "signed-out" as const; + const known = yield* Cache.get(coverage, host).pipe(Effect.option); + if (Option.isNone(known)) return "unknown" as const; + if (known.value === null) return "signed-out" as const; + const repository = ownerAndName(rawRepository); + const owner = repository.split("/")[0] ?? ""; + return known.value.accounts.has(owner) || known.value.repositories.has(repository) + ? ("covered" as const) + : ("not-covered" as const); + }); + + const reach: GitHubAppAccess["Service"]["reach"] = (host, repository) => + coverageFor(host, repository).pipe( + Effect.map((answer) => (answer === "unknown" ? "covered" : answer)), + ); + + const viewer: GitHubAppAccess["Service"]["viewer"] = Effect.fn("GitHubAppAccess.viewer")( + function* (rawHost) { + const host = rawHost.trim().toLowerCase(); + // A new sign-in can reach different installations than the cached answer. + yield* Cache.invalidate(coverage, host); + const login = yield* readAsApp(host, "appViewer", "user", Viewer).pipe( + Effect.catchTags({ + SchemaError: () => Effect.succeed(Option.none<{ readonly login: string }>()), + }), + ); + if (Option.isNone(login)) { + return yield* new GitHubRequestPolicy.GitHubApiAuthenticationError({ + host, + operation: "appViewer", + }); + } + return login.value.login; + }, + ); + + const credentialFor: GitHubAppAccess["Service"]["credentialFor"] = Effect.fn( + "GitHubAppAccess.credentialFor", + )(function* (host, repository) { + // Pinned work cannot be handed on, so only confirmed app access keeps the app credential. + if ((yield* coverageFor(host, repository)) === "covered") return yield* api.credential(host); + return yield* api + .credential(host, { personal: true }) + .pipe(Effect.catch(() => api.credential(host))); + }); + + return GitHubAppAccess.of({ reach, viewer, credentialFor }); +}); + +export const layer = Layer.effect(GitHubAppAccess, make); diff --git a/packages/source-control-github/src/server/GitHubAppSignIn.test.ts b/packages/source-control-github/src/server/GitHubAppSignIn.test.ts new file mode 100644 index 0000000000000..2f78a3aab83e9 --- /dev/null +++ b/packages/source-control-github/src/server/GitHubAppSignIn.test.ts @@ -0,0 +1,593 @@ +import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; +import { afterEach, beforeEach, describe, expect, it, vi } from "@effect/vitest"; +import * as Deferred from "effect/Deferred"; +import * as Effect from "effect/Effect"; +import * as Fiber from "effect/Fiber"; +import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; +import * as Redacted from "effect/Redacted"; +import { HttpClient, HttpClientResponse } from "effect/http"; +import { ChildProcessSpawner } from "effect/process"; +import * as TestClock from "effect/testing/TestClock"; + +import { DEFAULT_SERVER_SETTINGS, ServerSettingsError } from "@t3tools/contracts"; +import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; +import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit"; +import { readSourceControlHostSettings } from "@t3tools/source-control-core/shared/definition"; +import { applyServerSettingsPatch } from "@t3tools/shared/serverSettings"; +import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost"; + +import * as GitHubClient from "../shared/definition.ts"; +import * as GitHubAppAccess from "./GitHubAppAccess.ts"; +import * as GitHubAppSignIn from "./GitHubAppSignIn.ts"; +import * as GitHubRest from "./GitHubRest.ts"; + +type Reply = { readonly status?: number; readonly body: unknown }; +interface Sent { + readonly host: string; + readonly path: string; + readonly form: URLSearchParams; + /** The bearer token, if any. */ + readonly token: string | undefined; +} + +const HOUR = 3_600_000; + +const session = (overrides: Record = {}) => + JSON.stringify({ + accessToken: "ghu_current", + accessTokenExpiresAt: 8 * HOUR, + refreshToken: "ghr_current", + refreshTokenExpiresAt: 4_000 * HOUR, + ...overrides, + }); + +/** The whole GitHub request stack over a fake GitHub that `handle` answers. */ +function harness(options: { + readonly handle: (sent: Sent) => Reply; + readonly signIns?: Record; + readonly tokens?: Record; + /** Without one, the server has no `gh` login to fall back to. */ + readonly ghSignedIn?: boolean; + /** Makes every settings write fail, as a full disk would. */ + readonly settingsWritesFail?: boolean; + /** Holds a request open until it completes, to interleave other work with it. */ + readonly gate?: (sent: Sent) => Effect.Effect; +}) { + const requests: Array = []; + const http = HttpClient.make((request) => { + const url = new URL(request.url); + const sent: Sent = { + host: url.host, + path: url.pathname, + form: new URLSearchParams( + request.body._tag === "Uint8Array" ? new TextDecoder().decode(request.body.body) : "", + ), + token: request.headers["authorization"]?.replace(/^Bearer /, ""), + }; + return Effect.suspend(() => options.gate?.(sent) ?? Effect.void).pipe( + Effect.map(() => { + requests.push(sent); + const reply = options.handle(sent); + return HttpClientResponse.fromWeb( + request, + new Response(JSON.stringify(reply.body), { + status: reply.status ?? 200, + headers: { "content-type": "application/json" }, + }), + ); + }), + ); + }); + const run: SourceControlHost.SourceControlHost["Service"]["process"]["run"] = () => + Effect.succeed({ + exitCode: ChildProcessSpawner.ExitCode(0), + stdout: options.ghSignedIn === false ? "" : "gh-token\n", + stderr: "", + stdoutTruncated: false, + stderrTruncated: false, + }); + const host = TestSourceControlHost.layer({ + settings: applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, { + sourceControlHosts: { + github: { signIns: options.signIns ?? {}, tokens: options.tokens ?? {} }, + }, + }), + process: { run }, + }); + const failingWrites = Layer.effect( + SourceControlHost.SourceControlHost, + Effect.map(Effect.service(SourceControlHost.SourceControlHost), (service) => ({ + ...service, + settings: { + ...service.settings, + update: () => + Effect.fail( + new ServerSettingsError({ settingsPath: "settings.json", operation: "write-file" }), + ), + }, + })), + ).pipe(Layer.provideMerge(host)); + const layer = GitHubAppAccess.layer.pipe( + Layer.provideMerge(GitHubRest.layerWithDependencies), + Layer.provideMerge(SourceControlRateLimit.layer), + Layer.provideMerge(options.settingsWritesFail === true ? failingWrites : host), + Layer.provide(NodeCrypto.layer), + Layer.provide(Layer.succeed(HttpClient.HttpClient, http)), + ); + return { layer, requests }; +} + +const savedSignIns = Effect.gen(function* () { + const host = yield* SourceControlHost.SourceControlHost; + const settings = yield* host.settings.get; + return readSourceControlHostSettings(GitHubClient.settings, settings.sourceControlHosts.github) + .signIns; +}); + +/** A REST read through the request policy, answering with the token it went out with. */ +const readUser = Effect.gen(function* () { + const api = yield* GitHubRest.GitHubRest; + return yield* api.rest({ host: "github.com", operation: "test", path: "user" }); +}); + +const deviceCode = { + device_code: "device-1", + user_code: "ABCD-1234", + verification_uri: "https://github.com/login/device", + expires_in: 900, + interval: 5, +}; + +const installedOn = (sent: Sent): Reply => + sent.path === "/user/installations" + ? { + body: { + installations: [ + { id: 1, repository_selection: "all", account: { login: "pingdotgg" } }, + { id: 2, repository_selection: "selected", account: { login: "acme" } }, + ], + }, + } + : sent.path === "/user/installations/2/repositories" + ? { body: { repositories: [{ full_name: "acme/one" }] } } + : { status: 404, body: {} }; + +beforeEach(() => { + for (const name of ["GH_TOKEN", "GITHUB_TOKEN"]) vi.stubEnv(name, ""); + vi.stubEnv(GitHubAppSignIn.CLIENT_ID_VARIABLE, "Iv-test-client"); +}); +afterEach(() => { + vi.unstubAllEnvs(); +}); + +describe("GitHubAppSignIn", () => { + it.effect("finishes a device-flow sign-in and saves the session", () => { + const answers = [ + { error: "authorization_pending" }, + { error: "slow_down", interval: 10 }, + { + access_token: "ghu_new", + expires_in: 28_800, + refresh_token: "ghr_new", + refresh_token_expires_in: 15_897_600, + }, + ]; + const { layer, requests } = harness({ + handle: (sent) => + sent.path === "/login/device/code" ? { body: deviceCode } : { body: answers.shift() }, + }); + return Effect.gen(function* () { + const signIn = yield* GitHubAppSignIn.GitHubAppSignIn; + expect(yield* signIn.start("github.com")).toEqual({ + userCode: "ABCD-1234", + verificationUri: "https://github.com/login/device", + expiresAt: 900_000, + }); + const waiting = yield* Effect.forkChild(signIn.wait("github.com")); + yield* TestClock.adjust("20 seconds"); + yield* Fiber.join(waiting); + + // `slow_down` stretches the interval: polls at 5s, 10s, then 20s. + const polls = requests.filter((sent) => sent.path === "/login/oauth/access_token"); + expect(polls).toHaveLength(3); + expect(polls[0]?.form.get("client_id")).toBe("Iv-test-client"); + expect(polls[0]?.form.get("device_code")).toBe("device-1"); + expect(JSON.parse((yield* savedSignIns)["github.com"] ?? "")).toEqual({ + accessToken: "ghu_new", + accessTokenExpiresAt: 20_000 + 28_800_000, + refreshToken: "ghr_new", + refreshTokenExpiresAt: 20_000 + 15_897_600_000, + }); + }).pipe(Effect.provide(layer)); + }); + + it.effect("reports a sign-in cancelled on GitHub and saves nothing", () => { + const { layer } = harness({ + handle: (sent) => + sent.path === "/login/device/code" + ? { body: deviceCode } + : { body: { error: "access_denied" } }, + }); + return Effect.gen(function* () { + const signIn = yield* GitHubAppSignIn.GitHubAppSignIn; + yield* signIn.start("github.com"); + const waiting = yield* Effect.forkChild(Effect.flip(signIn.wait("github.com"))); + yield* TestClock.adjust("5 seconds"); + expect((yield* Fiber.join(waiting)).reason).toBe("denied"); + expect(yield* savedSignIns).toEqual({}); + }).pipe(Effect.provide(layer)); + }); + + it.effect("expires a code nobody entered", () => { + const { layer } = harness({ + handle: (sent) => + sent.path === "/login/device/code" + ? { body: { ...deviceCode, expires_in: 12 } } + : { body: { error: "authorization_pending" } }, + }); + return Effect.gen(function* () { + const signIn = yield* GitHubAppSignIn.GitHubAppSignIn; + yield* signIn.start("github.com"); + const waiting = yield* Effect.forkChild(Effect.flip(signIn.wait("github.com"))); + yield* TestClock.adjust("15 seconds"); + expect((yield* Fiber.join(waiting)).reason).toBe("expired"); + }).pipe(Effect.provide(layer)); + }); + + it.effect("cancels a sign-in so approving the code afterwards saves nothing", () => { + const { layer, requests } = harness({ + handle: (sent) => + sent.path === "/login/device/code" + ? { body: deviceCode } + : { body: { access_token: "ghu_new", expires_in: 28_800 } }, + }); + return Effect.gen(function* () { + const signIn = yield* GitHubAppSignIn.GitHubAppSignIn; + yield* signIn.start("github.com"); + const waiting = yield* Effect.forkChild(Effect.flip(signIn.wait("github.com"))); + yield* signIn.cancel("github.com"); + yield* TestClock.adjust("10 seconds"); + expect((yield* Fiber.join(waiting)).reason).toBe("cancelled"); + expect(requests.map((sent) => sent.path)).toEqual(["/login/device/code"]); + expect(yield* savedSignIns).toEqual({}); + }).pipe(Effect.provide(layer)); + }); + + it.effect("refuses hosts the app is not registered on", () => { + const { layer, requests } = harness({ handle: () => ({ body: {} }) }); + return Effect.gen(function* () { + const signIn = yield* GitHubAppSignIn.GitHubAppSignIn; + expect((yield* Effect.flip(signIn.start("ghe.acme.test"))).reason).toBe("unsupported"); + expect((yield* Effect.flip(signIn.wait("github.com"))).reason).toBe("not-started"); + expect(requests).toEqual([]); + }).pipe(Effect.provide(layer)); + }); + + it.effect("does nothing with the app when the server has no client ID", () => { + vi.stubEnv(GitHubAppSignIn.CLIENT_ID_VARIABLE, ""); + const { layer, requests } = harness({ + signIns: { "github.com": session() }, + handle: () => ({ body: { login: "octocat" } }), + }); + return Effect.gen(function* () { + const signIn = yield* GitHubAppSignIn.GitHubAppSignIn; + expect(signIn.enabled).toBe(false); + expect((yield* Effect.flip(signIn.start("github.com"))).reason).toBe("unsupported"); + expect(yield* (yield* GitHubAppAccess.GitHubAppAccess).reach("github.com", "acme/one")).toBe( + "signed-out", + ); + // A session saved while it was on stays put but is not used. + yield* readUser; + expect(requests.map((sent) => sent.token)).toEqual(["gh-token"]); + expect(Object.keys(yield* savedSignIns)).toEqual(["github.com"]); + }).pipe(Effect.provide(layer)); + }); + + describe("refresh", () => { + const refreshed = { + access_token: "ghu_refreshed", + expires_in: 28_800, + refresh_token: "ghr_refreshed", + refresh_token_expires_in: 15_897_600, + }; + + it.effect("refreshes an expiring token once for concurrent requests", () => { + const { layer, requests } = harness({ + signIns: { "github.com": session({ accessTokenExpiresAt: 3 * 60_000 }) }, + handle: () => ({ body: refreshed }), + }); + return Effect.gen(function* () { + const signIn = yield* GitHubAppSignIn.GitHubAppSignIn; + const tokens = yield* Effect.all([signIn.token("github.com"), signIn.token("github.com")], { + concurrency: "unbounded", + }); + expect(tokens).toEqual([Option.some("ghu_refreshed"), Option.some("ghu_refreshed")]); + expect(requests).toHaveLength(1); + expect(requests[0]?.form.get("grant_type")).toBe("refresh_token"); + expect(requests[0]?.form.get("refresh_token")).toBe("ghr_current"); + }).pipe(Effect.provide(layer)); + }); + + it.effect("refreshes a token GitHub refused, and only that one", () => { + const { layer, requests } = harness({ + signIns: { "github.com": session() }, + handle: () => ({ body: refreshed }), + }); + return Effect.gen(function* () { + const signIn = yield* GitHubAppSignIn.GitHubAppSignIn; + yield* signIn.invalidate("github.com", "gh-token"); + expect(yield* signIn.token("github.com")).toEqual(Option.some("ghu_current")); + yield* signIn.invalidate("github.com", "ghu_current"); + expect(yield* signIn.token("github.com")).toEqual(Option.some("ghu_refreshed")); + expect(requests).toHaveLength(1); + }).pipe(Effect.provide(layer)); + }); + + it.effect("signs out when GitHub refuses the refresh token", () => { + const { layer } = harness({ + signIns: { "github.com": session({ accessTokenExpiresAt: 0 }) }, + handle: () => ({ body: { error: "bad_refresh_token" } }), + }); + return Effect.gen(function* () { + const signIn = yield* GitHubAppSignIn.GitHubAppSignIn; + expect(yield* signIn.token("github.com")).toEqual(Option.none()); + expect(yield* savedSignIns).toEqual({}); + }).pipe(Effect.provide(layer)); + }); + + it.effect("keeps the session when a refresh fails for another reason", () => { + const { layer } = harness({ + signIns: { "github.com": session({ accessTokenExpiresAt: 0 }) }, + handle: () => ({ body: { error: "unauthorized_client" } }), + }); + return Effect.gen(function* () { + const signIn = yield* GitHubAppSignIn.GitHubAppSignIn; + expect((yield* Effect.flip(signIn.token("github.com")))._tag).toBe("GitHubAppSessionError"); + expect(Object.keys(yield* savedSignIns)).toEqual(["github.com"]); + }).pipe(Effect.provide(layer)); + }); + + it.effect("serves the still-valid token when a refresh ahead of expiry fails", () => { + const { layer } = harness({ + signIns: { "github.com": session({ accessTokenExpiresAt: 3 * 60_000 }) }, + handle: () => ({ status: 502, body: {} }), + }); + return Effect.gen(function* () { + const signIn = yield* GitHubAppSignIn.GitHubAppSignIn; + expect(yield* signIn.token("github.com")).toEqual(Option.some("ghu_current")); + }).pipe(Effect.provide(layer)); + }); + + it.effect("serves a refreshed token GitHub issued even when saving it fails", () => { + const { layer } = harness({ + signIns: { "github.com": session({ accessTokenExpiresAt: 3 * 60_000 }) }, + settingsWritesFail: true, + handle: () => ({ body: refreshed }), + }); + return Effect.gen(function* () { + const signIn = yield* GitHubAppSignIn.GitHubAppSignIn; + const pending = yield* Effect.forkChild(signIn.token("github.com")); + yield* TestClock.adjust("1 second"); + expect(yield* Fiber.join(pending)).toEqual(Option.some("ghu_refreshed")); + }).pipe(Effect.provide(layer)); + }); + + it.effect("does not bring a session back that was signed out while it refreshed", () => { + const released = Deferred.makeUnsafe(); + const requested = Deferred.makeUnsafe(); + const { layer } = harness({ + signIns: { "github.com": session({ accessTokenExpiresAt: 0 }) }, + gate: () => + Deferred.succeed(requested, undefined).pipe(Effect.andThen(Deferred.await(released))), + handle: () => ({ body: refreshed }), + }); + return Effect.gen(function* () { + const signIn = yield* GitHubAppSignIn.GitHubAppSignIn; + const settings = yield* TestSourceControlHost.TestSourceControlHostSettings; + const refreshing = yield* Effect.forkChild(signIn.token("github.com")); + yield* Deferred.await(requested); + yield* settings.update({ sourceControlHosts: { github: { signIns: {} } } }); + yield* Deferred.succeed(released, undefined); + expect(yield* Fiber.join(refreshing)).toEqual(Option.none()); + expect(yield* savedSignIns).toEqual({}); + }).pipe(Effect.provide(layer)); + }); + }); +}); + +describe("GitHub requests with the app sign-in", () => { + const answerUser = (sent: Sent): Reply => ({ body: { login: sent.token ?? "anonymous" } }); + + it.effect("uses the app before GH_TOKEN and gh, and a saved token before the app", () => { + vi.stubEnv("GH_TOKEN", "from-env"); + const signedIn = harness({ signIns: { "github.com": session() }, handle: answerUser }); + const withToken = harness({ + signIns: { "github.com": session() }, + tokens: { "github.com": "saved-pat" }, + handle: answerUser, + }); + return Effect.gen(function* () { + yield* readUser.pipe(Effect.provide(signedIn.layer)); + yield* readUser.pipe(Effect.provide(withToken.layer)); + expect(signedIn.requests.map((sent) => sent.token)).toEqual(["ghu_current"]); + expect(withToken.requests.map((sent) => sent.token)).toEqual(["saved-pat"]); + }); + }); + + it.effect("asks again with the user's own credential when the app gets not found", () => { + const { layer, requests } = harness({ + signIns: { "github.com": session() }, + handle: (sent) => + sent.token === "ghu_current" ? { status: 404, body: {} } : answerUser(sent), + }); + return Effect.gen(function* () { + const response = yield* readUser; + expect(JSON.parse(response.body)).toEqual({ login: "gh-token" }); + expect(requests.map((sent) => sent.token)).toEqual(["ghu_current", "gh-token"]); + }).pipe(Effect.provide(layer)); + }); + + it.effect("asks again when GitHub says the resource is not accessible by integration", () => { + const { layer, requests } = harness({ + signIns: { "github.com": session() }, + handle: (sent) => + sent.token === "ghu_current" + ? { status: 403, body: { message: "Resource not accessible by integration" } } + : answerUser(sent), + }); + return Effect.gen(function* () { + yield* readUser; + expect(requests.map((sent) => sent.token)).toEqual(["ghu_current", "gh-token"]); + }).pipe(Effect.provide(layer)); + }); + + it.effect("keeps the app's answer when there is no other credential", () => { + const { layer, requests } = harness({ + signIns: { "github.com": session() }, + ghSignedIn: false, + handle: () => ({ status: 404, body: {} }), + }); + return Effect.gen(function* () { + expect((yield* Effect.flip(readUser))._tag).toBe("GitHubApiNotFoundError"); + expect(requests.map((sent) => sent.token)).toEqual(["ghu_current"]); + }).pipe(Effect.provide(layer)); + }); + + it.effect("does not ask again after the user's own credential is refused", () => { + const { layer, requests } = harness({ handle: () => ({ status: 404, body: {} }) }); + return Effect.gen(function* () { + expect((yield* Effect.flip(readUser))._tag).toBe("GitHubApiNotFoundError"); + expect(requests.map((sent) => sent.token)).toEqual(["gh-token"]); + }).pipe(Effect.provide(layer)); + }); + + it.effect("uses the gh login while the app session cannot refresh, and keeps the session", () => { + const { layer, requests } = harness({ + signIns: { "github.com": session({ accessTokenExpiresAt: 0 }) }, + handle: (sent) => (sent.host === "github.com" ? { status: 502, body: {} } : answerUser(sent)), + }); + return Effect.gen(function* () { + yield* readUser; + expect(requests.filter((sent) => sent.path === "/user").map((sent) => sent.token)).toEqual([ + "gh-token", + ]); + expect(Object.keys(yield* savedSignIns)).toEqual(["github.com"]); + }).pipe(Effect.provide(layer)); + }); + + it.effect("refreshes the app token after GitHub rejects it", () => { + let rejected = false; + const { layer, requests } = harness({ + signIns: { "github.com": session() }, + handle: (sent) => { + if (sent.path === "/login/oauth/access_token") { + return { body: { access_token: "ghu_refreshed", expires_in: 28_800 } }; + } + if (sent.token === "ghu_current" && !rejected) { + rejected = true; + return { status: 401, body: {} }; + } + return answerUser(sent); + }, + }); + return Effect.gen(function* () { + expect((yield* Effect.flip(readUser))._tag).toBe("GitHubApiAuthenticationError"); + yield* readUser; + expect(requests.map((sent) => sent.token ?? sent.path)).toEqual([ + "ghu_current", + "/login/oauth/access_token", + "ghu_refreshed", + ]); + }).pipe(Effect.provide(layer)); + }); +}); + +describe("GitHubAppAccess", () => { + it.effect("reads the installations as the app, even with a saved token", () => { + const { layer, requests } = harness({ + signIns: { "github.com": session() }, + tokens: { "github.com": "saved-pat" }, + handle: installedOn, + }); + return Effect.gen(function* () { + const access = yield* GitHubAppAccess.GitHubAppAccess; + expect(yield* access.reach("github.com", "pingdotgg/t3code")).toBe("covered"); + expect(yield* access.reach("github.com", "acme/one")).toBe("covered"); + expect(yield* access.reach("github.com", "acme/two")).toBe("not-covered"); + expect(yield* access.reach("github.com", "https://github.com/Acme/One.git")).toBe("covered"); + expect(new Set(requests.map((sent) => sent.token))).toEqual(new Set(["ghu_current"])); + // One read of the installations serves every repository until it goes stale. + expect(requests.filter((sent) => sent.path === "/user/installations")).toHaveLength(1); + }).pipe(Effect.provide(layer)); + }); + + it.effect("answers signed out without a session, and covered while GitHub fails", () => { + const signedOut = harness({ handle: installedOn }); + const failing = harness({ + signIns: { "github.com": session() }, + handle: () => ({ status: 502, body: {} }), + }); + return Effect.gen(function* () { + expect( + yield* Effect.flatMap(Effect.service(GitHubAppAccess.GitHubAppAccess), (access) => + access.reach("github.com", "acme/one"), + ).pipe(Effect.provide(signedOut.layer)), + ).toBe("signed-out"); + const answers = yield* Effect.gen(function* () { + const access = yield* GitHubAppAccess.GitHubAppAccess; + return [ + yield* access.reach("github.com", "acme/one"), + yield* access.reach("github.com", "acme/two"), + ]; + }).pipe(Effect.provide(failing.layer)); + expect(answers).toEqual(["covered", "covered"]); + // A failed read is kept briefly instead of asked again per request. + expect(failing.requests).toHaveLength(1); + }); + }); + + it.effect("gives work on a repository the app cannot reach the user's credential", () => { + const { layer } = harness({ signIns: { "github.com": session() }, handle: installedOn }); + return Effect.gen(function* () { + const access = yield* GitHubAppAccess.GitHubAppAccess; + const tokenFor = (repository: string) => + access + .credentialFor("github.com", repository) + .pipe(Effect.map((held) => Redacted.value(held.token))); + expect(yield* tokenFor("pingdotgg/t3code")).toBe("ghu_current"); + expect(yield* tokenFor("someone/else")).toBe("gh-token"); + }).pipe(Effect.provide(layer)); + }); + + it.effect("pins the user's credential while the app's reach is unknown", () => { + const failing = (sent: Sent): Reply => + sent.path.startsWith("/user/installations") ? { status: 502, body: {} } : { body: {} }; + const withGh = harness({ signIns: { "github.com": session() }, handle: failing }); + const appOnly = harness({ + signIns: { "github.com": session() }, + ghSignedIn: false, + handle: failing, + }); + const tokenFor = Effect.flatMap(Effect.service(GitHubAppAccess.GitHubAppAccess), (access) => + access + .credentialFor("github.com", "pingdotgg/t3code") + .pipe(Effect.map((held) => Redacted.value(held.token))), + ); + return Effect.gen(function* () { + expect(yield* tokenFor.pipe(Effect.provide(withGh.layer))).toBe("gh-token"); + expect(yield* tokenFor.pipe(Effect.provide(appOnly.layer))).toBe("ghu_current"); + }); + }); + + it.effect("names the account the app sign-in acts as", () => { + const { layer } = harness({ + signIns: { "github.com": session() }, + tokens: { "github.com": "saved-pat" }, + handle: (sent) => ({ body: { login: sent.token === "ghu_current" ? "octocat" : "other" } }), + }); + return Effect.gen(function* () { + expect(yield* (yield* GitHubAppAccess.GitHubAppAccess).viewer("github.com")).toBe("octocat"); + }).pipe(Effect.provide(layer)); + }); +}); diff --git a/packages/source-control-github/src/server/GitHubAppSignIn.ts b/packages/source-control-github/src/server/GitHubAppSignIn.ts new file mode 100644 index 0000000000000..e7e792068617e --- /dev/null +++ b/packages/source-control-github/src/server/GitHubAppSignIn.ts @@ -0,0 +1,467 @@ +import { + SourceControlProviderKind, + SourceControlSignInError, + type SourceControlSignInFailure, + type SourceControlSignInPrompt, +} from "@t3tools/contracts"; +import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; +import { readSourceControlHostSettings } from "@t3tools/source-control-core/shared/definition"; +import * as Clock from "effect/Clock"; +import * as Context from "effect/Context"; +import * as Deferred from "effect/Deferred"; +import * as Duration from "effect/Duration"; +import * as Effect from "effect/Effect"; +import * as Fiber from "effect/Fiber"; +import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; +import * as Ref from "effect/Ref"; +import * as Schema from "effect/Schema"; +import * as Schedule from "effect/Schedule"; +import * as Semaphore from "effect/Semaphore"; +import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http"; +import * as HostProcess from "@t3tools/shared/HostProcess"; + +import * as GitHubClient from "../shared/definition.ts"; + +/** + * The public client ID of the T3 Code for GitHub app, which the device flow needs (no secret). + * Unset turns the app sign-in off: no app tokens, no sign-in, nothing for clients to show. + */ +export const CLIENT_ID_VARIABLE = "T3CODE_GITHUB_APP_CLIENT_ID"; + +const KIND = SourceControlProviderKind.make("github"); +const REQUEST_TIMEOUT = Duration.seconds(15); +// Refresh ahead of expiry so a token never lapses between the check and a long request. +const REFRESH_MARGIN_MS = Duration.toMillis(Duration.minutes(5)); + +const GitHubAppSession = Schema.Struct({ + accessToken: Schema.String, + /** Null when the app hands out tokens that do not expire. */ + accessTokenExpiresAt: Schema.NullOr(Schema.Number), + refreshToken: Schema.NullOr(Schema.String), + refreshTokenExpiresAt: Schema.NullOr(Schema.Number), +}); +type GitHubAppSession = typeof GitHubAppSession.Type; + +const GitHubAppSessionJson = Schema.fromJsonString(GitHubAppSession); +const decodeSession = Schema.decodeUnknownOption(GitHubAppSessionJson); +const encodeSession = Schema.encodeSync(GitHubAppSessionJson); + +// GitHub answers both token grants with HTTP 200 and puts refusals in `error`. +const TokenResponse = Schema.Struct({ + access_token: Schema.optional(Schema.String), + expires_in: Schema.optional(Schema.Number), + refresh_token: Schema.optional(Schema.String), + refresh_token_expires_in: Schema.optional(Schema.Number), + interval: Schema.optional(Schema.Number), + error: Schema.optional(Schema.String), + error_description: Schema.optional(Schema.String), +}); + +const DeviceCodeResponse = Schema.Struct({ + device_code: Schema.optional(Schema.String), + user_code: Schema.optional(Schema.String), + verification_uri: Schema.optional(Schema.String), + expires_in: Schema.optional(Schema.Number), + interval: Schema.optional(Schema.Number), + error: Schema.optional(Schema.String), + error_description: Schema.optional(Schema.String), +}); + +export class GitHubAppSessionError extends Schema.TaggedError()( + "GitHubAppSessionError", + { host: Schema.String, cause: Schema.Defect() }, +) { + override get message(): string { + return `Could not refresh the ${GitHubClient.APP_SIGN_IN.appName} sign-in for ${this.host}. Check the server's connection to GitHub, or sign in again in Settings → Source Control.`; + } +} + +export class GitHubAppSignIn extends Context.Service< + GitHubAppSignIn, + { + /** Whether the server is configured with the app's client ID. */ + readonly enabled: boolean; + /** The signed-in access token for `host`, refreshed when due; none when not signed in. */ + readonly token: (host: string) => Effect.Effect, GitHubAppSessionError>; + /** Refreshes on the next `token` when `rejected` is the token GitHub just refused. */ + readonly invalidate: (host: string, rejected: string) => Effect.Effect; + readonly start: ( + host: string, + ) => Effect.Effect; + /** Resolves once the sign-in `start` began has saved a session. */ + readonly wait: (host: string) => Effect.Effect; + readonly cancel: (host: string) => Effect.Effect; + } +>()("@t3tools/source-control-github/server/GitHubAppSignIn") {} + +interface PendingSignIn { + readonly deferred: Deferred.Deferred; + readonly fiber: Fiber.Fiber; +} + +const signInError = (host: string, reason: SourceControlSignInFailure, detail: string) => + new SourceControlSignInError({ provider: KIND, host, reason, detail }); + +/** @public */ +export const make = Effect.gen(function* () { + const sourceControlHost = yield* SourceControlHost.SourceControlHost; + const httpClient = yield* HttpClient.HttpClient; + const clientId = (yield* HostProcess.Environment)[CLIENT_ID_VARIABLE]?.trim() ?? ""; + const enabled = clientId !== ""; + const scope = yield* Effect.scope; + const refreshLock = yield* Semaphore.make(1); + const refused = yield* Ref.make>(new Set()); + const pending = yield* Ref.make>(new Map()); + + const isAppHost = (host: string) => + enabled && GitHubClient.APP_SIGN_IN.hosts.includes(host.trim().toLowerCase()); + + const readSignIns = sourceControlHost.settings.get.pipe( + Effect.map( + (settings) => + readSourceControlHostSettings( + GitHubClient.settings, + settings.sourceControlHosts[GitHubClient.definition.kind], + ).signIns, + ), + Effect.orElseSucceed((): Readonly> => ({})), + ); + + const readSession = (host: string) => + readSignIns.pipe(Effect.map((signIns) => decodeSession(signIns[host] ?? ""))); + + const writeSession = Effect.fn("GitHubAppSignIn.writeSession")(function* ( + host: string, + session: GitHubAppSession | null, + ) { + const signIns = { ...(yield* readSignIns) }; + if (session === null) delete signIns[host]; + else signIns[host] = encodeSession(session); + yield* sourceControlHost.settings.update({ + sourceControlHosts: { [GitHubClient.definition.kind]: { signIns } }, + }); + }); + + /** + * Writes `next` only while the stored session is still the one a refresh started from, so a + * sign-out or a newer sign-in during the request is not undone. + */ + const replaceSession = Effect.fn("GitHubAppSignIn.replaceSession")(function* ( + host: string, + from: GitHubAppSession, + next: GitHubAppSession | null, + ) { + const current = yield* readSession(host); + if (Option.isNone(current) || current.value.accessToken !== from.accessToken) return false; + yield* writeSession(host, next); + return true; + }); + + const postForm = ( + host: string, + path: string, + body: Record, + schema: S, + ) => + httpClient + .execute( + HttpClientRequest.post(`https://${host}${path}`).pipe( + HttpClientRequest.setHeaders({ accept: "application/json", "user-agent": "t3code" }), + HttpClientRequest.bodyUrlParams(body), + ), + ) + .pipe( + Effect.flatMap(HttpClientResponse.filterStatusOk), + Effect.flatMap(HttpClientResponse.schemaBodyJson(schema)), + Effect.timeout(REQUEST_TIMEOUT), + ); + + const sessionFrom = ( + now: number, + response: typeof TokenResponse.Type & { readonly access_token: string }, + ): GitHubAppSession => ({ + accessToken: response.access_token, + accessTokenExpiresAt: + response.expires_in === undefined ? null : now + response.expires_in * 1_000, + refreshToken: response.refresh_token ?? null, + refreshTokenExpiresAt: + response.refresh_token_expires_in === undefined + ? null + : now + response.refresh_token_expires_in * 1_000, + }); + + const refresh = Effect.fn("GitHubAppSignIn.refresh")(function* ( + host: string, + session: GitHubAppSession, + ) { + const now = yield* Clock.currentTimeMillis; + if ( + session.refreshToken === null || + (session.refreshTokenExpiresAt !== null && session.refreshTokenExpiresAt <= now) + ) { + yield* replaceSession(host, session, null).pipe(Effect.ignore); + return Option.none(); + } + const response = yield* postForm( + host, + "/login/oauth/access_token", + { + client_id: clientId, + grant_type: "refresh_token", + refresh_token: session.refreshToken, + }, + TokenResponse, + ).pipe(Effect.mapError((cause) => new GitHubAppSessionError({ host, cause }))); + if (response.error === "bad_refresh_token") { + // Final: the user revoked the app or the session lapsed. + yield* Effect.logWarning("GitHub refused the app sign-in refresh; signing out.", { host }); + yield* replaceSession(host, session, null).pipe(Effect.ignore); + return Option.none(); + } + if (response.access_token === undefined) { + return yield* new GitHubAppSessionError({ + host, + cause: new Error(`GitHub did not refresh the sign-in (${response.error ?? "no token"}).`), + }); + } + const next = sessionFrom(now, { + ...response, + access_token: response.access_token, + }); + // GitHub has retired the old pair, so an unsaved new one is still the only usable token. + const saved = yield* replaceSession(host, session, next).pipe( + Effect.retry({ times: 2, schedule: Schedule.spaced(Duration.millis(250)) }), + Effect.catch((cause) => + Effect.logError("Could not save the refreshed app sign-in; using it unsaved.", { + host, + cause, + }).pipe(Effect.as(true)), + ), + ); + if (saved) return Option.some(next.accessToken); + // Signed out or signed in again meanwhile: serve whatever is stored now. + return Option.map(yield* readSession(host), (current) => current.accessToken); + }); + + const isFresh = (session: GitHubAppSession, now: number) => + session.accessTokenExpiresAt === null || now < session.accessTokenExpiresAt - REFRESH_MARGIN_MS; + + const token: GitHubAppSignIn["Service"]["token"] = Effect.fn("GitHubAppSignIn.token")( + function* (rawHost) { + const host = rawHost.trim().toLowerCase(); + if (!isAppHost(host)) return Option.none(); + const session = yield* readSession(host); + if (Option.isNone(session)) return Option.none(); + const now = yield* Clock.currentTimeMillis; + if ( + isFresh(session.value, now) && + !(yield* Ref.get(refused)).has(session.value.accessToken) + ) { + return Option.some(session.value.accessToken); + } + return yield* refreshLock.withPermits(1)( + Effect.gen(function* () { + // Another request may have refreshed while this one queued. + const current = yield* readSession(host); + if (Option.isNone(current)) return Option.none(); + const refusedTokens = yield* Ref.get(refused); + const now = yield* Clock.currentTimeMillis; + if (isFresh(current.value, now) && !refusedTokens.has(current.value.accessToken)) { + return Option.some(current.value.accessToken); + } + const usable = Option.some(current.value.accessToken).pipe( + Option.filter( + (accessToken) => + !refusedTokens.has(accessToken) && + (current.value.accessTokenExpiresAt === null || + now < current.value.accessTokenExpiresAt), + ), + ); + // A refresh ahead of expiry that fails leaves the still-valid token in use. + return yield* refresh(host, current.value).pipe( + Effect.catchTags({ + GitHubAppSessionError: (error) => + Option.isSome(usable) ? Effect.succeed(usable) : Effect.fail(error), + }), + ); + }), + ); + }, + ); + + const invalidate: GitHubAppSignIn["Service"]["invalidate"] = Effect.fn( + "GitHubAppSignIn.invalidate", + )(function* (rawHost, rejected) { + const session = yield* readSession(rawHost.trim().toLowerCase()); + // A refused fallback token says nothing about the app session. + if (Option.isNone(session) || session.value.accessToken !== rejected) return; + yield* Ref.update(refused, (current) => new Set([...current, rejected])); + }); + + const poll = Effect.fn("GitHubAppSignIn.poll")(function* ( + host: string, + deviceCode: string, + expiresAt: number, + initialInterval: number, + ) { + let interval = initialInterval; + while (true) { + yield* Effect.sleep(Duration.seconds(interval)); + const now = yield* Clock.currentTimeMillis; + if (now >= expiresAt) { + return yield* signInError(host, "expired", "The sign-in code expired. Start again."); + } + const answer = yield* postForm( + host, + "/login/oauth/access_token", + { + client_id: clientId, + device_code: deviceCode, + grant_type: "urn:ietf:params:oauth:grant-type:device_code", + }, + TokenResponse, + ).pipe(Effect.option); + // A dropped poll is retried on the next tick; the code stays valid until it expires. + if (Option.isNone(answer)) continue; + const response = answer.value; + if (response.access_token !== undefined) { + const accessToken = response.access_token; + return yield* writeSession( + host, + sessionFrom(yield* Clock.currentTimeMillis, { ...response, access_token: accessToken }), + ).pipe( + Effect.mapError( + (cause) => + new SourceControlSignInError({ + provider: KIND, + host, + reason: "failed", + detail: "The server could not save the sign-in.", + cause, + }), + ), + ); + } + switch (response.error) { + case "authorization_pending": + continue; + case "slow_down": + interval = response.interval ?? interval + 5; + continue; + case "expired_token": + return yield* signInError(host, "expired", "The sign-in code expired. Start again."); + case "access_denied": + return yield* signInError(host, "denied", "The sign-in was cancelled on GitHub."); + default: + return yield* signInError( + host, + "failed", + response.error_description ?? + `GitHub refused the sign-in (${response.error ?? "no reason given"}).`, + ); + } + } + }); + + const start: GitHubAppSignIn["Service"]["start"] = Effect.fn("GitHubAppSignIn.start")( + function* (rawHost) { + const host = rawHost.trim().toLowerCase(); + if (!enabled) { + return yield* signInError( + host, + "unsupported", + `This server is not set up to sign in with ${GitHubClient.APP_SIGN_IN.appName}.`, + ); + } + if (!isAppHost(host)) { + return yield* signInError( + host, + "unsupported", + `${GitHubClient.APP_SIGN_IN.appName} only signs in to ${GitHubClient.APP_SIGN_IN.hosts.join(", ")}. Use a token or the GitHub CLI for ${host}.`, + ); + } + const code = yield* postForm( + host, + "/login/device/code", + { client_id: clientId }, + DeviceCodeResponse, + ).pipe( + Effect.mapError( + (cause) => + new SourceControlSignInError({ + provider: KIND, + host, + reason: "failed", + detail: "The server could not reach GitHub to start the sign-in.", + cause, + }), + ), + ); + if ( + code.device_code === undefined || + code.user_code === undefined || + code.verification_uri === undefined || + code.expires_in === undefined + ) { + return yield* signInError( + host, + "failed", + code.error_description ?? + `GitHub would not start the sign-in (${code.error ?? "no reason given"}).`, + ); + } + const expiresAt = (yield* Clock.currentTimeMillis) + code.expires_in * 1_000; + const deferred = yield* Deferred.make(); + const fiber = yield* poll(host, code.device_code, expiresAt, code.interval ?? 5).pipe( + Effect.tap(() => Ref.set(refused, new Set())), + Deferred.into(deferred), + Effect.forkIn(scope), + ); + const previous = yield* Ref.modify(pending, (current) => [ + current.get(host), + new Map(current).set(host, { deferred, fiber }), + ]); + if (previous !== undefined) { + yield* Deferred.fail( + previous.deferred, + signInError(host, "failed", "A newer sign-in replaced this one."), + ); + yield* Fiber.interrupt(previous.fiber); + } + return { + userCode: code.user_code, + verificationUri: code.verification_uri, + expiresAt, + } satisfies SourceControlSignInPrompt; + }, + ); + + const wait: GitHubAppSignIn["Service"]["wait"] = Effect.fn("GitHubAppSignIn.wait")( + function* (rawHost) { + const host = rawHost.trim().toLowerCase(); + const current = (yield* Ref.get(pending)).get(host); + if (current === undefined) { + return yield* signInError(host, "not-started", "No sign-in is in progress. Start again."); + } + return yield* Deferred.await(current.deferred); + }, + ); + + const cancel: GitHubAppSignIn["Service"]["cancel"] = Effect.fn("GitHubAppSignIn.cancel")( + function* (rawHost) { + const host = rawHost.trim().toLowerCase(); + const current = (yield* Ref.get(pending)).get(host); + if (current === undefined) return; + yield* Deferred.fail( + current.deferred, + signInError(host, "cancelled", "The sign-in was cancelled."), + ); + yield* Fiber.interrupt(current.fiber); + }, + ); + + return GitHubAppSignIn.of({ enabled, token, invalidate, start, wait, cancel }); +}); + +export const layer = Layer.effect(GitHubAppSignIn, make); diff --git a/packages/source-control-github/src/server/GitHubCredentials.test.ts b/packages/source-control-github/src/server/GitHubCredentials.test.ts index a17be69501055..35d0eb23bf4ed 100644 --- a/packages/source-control-github/src/server/GitHubCredentials.test.ts +++ b/packages/source-control-github/src/server/GitHubCredentials.test.ts @@ -3,6 +3,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "@effect/vitest" import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Redacted from "effect/Redacted"; +import { HttpClient } from "effect/http"; import { ChildProcessSpawner } from "effect/process"; import { DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts"; @@ -62,6 +63,12 @@ function harness( }), ), Layer.provide(NodeCrypto.layer), + Layer.provide( + Layer.succeed( + HttpClient.HttpClient, + HttpClient.make((request) => Effect.die(`Unexpected request to ${request.url}`)), + ), + ), ); return { layer, calls }; } diff --git a/packages/source-control-github/src/server/GitHubCredentials.ts b/packages/source-control-github/src/server/GitHubCredentials.ts index b958750e860ad..41641790aac52 100644 --- a/packages/source-control-github/src/server/GitHubCredentials.ts +++ b/packages/source-control-github/src/server/GitHubCredentials.ts @@ -29,7 +29,7 @@ export const GH_PROCESS_CONCURRENCY = { key: "gh", limit: 4 } as const; const TOKEN_TTL = Duration.minutes(5); const MISSING_TTL = Duration.seconds(10); -export const GitHubCredentialSource = Schema.Literals(["settings", "env", "gh"]); +export const GitHubCredentialSource = Schema.Literals(["settings", "app", "env", "gh"]); export type GitHubCredentialSource = typeof GitHubCredentialSource.Type; export interface GitHubCredential { @@ -89,6 +89,14 @@ export class GitHubCredentials extends Context.Service< readonly get: ( host: string, ) => Effect.Effect; + /** The token saved in Settings for `host`, which outranks every other source. */ + readonly saved: ( + host: string, + ) => Effect.Effect< + Option.Option, + GitHubHostDisabledError | GitHubNotSignedInError + >; + readonly fingerprint: (host: string, token: string) => Effect.Effect; readonly invalidate: (host: string) => Effect.Effect; } >()("@t3tools/source-control-github/server/GitHubCredentials") {} @@ -225,25 +233,31 @@ export const make = Effect.gen(function* () { : MISSING_TTL, }); + const saved = Effect.fn("GitHubCredentials.saved")(function* (rawHost: string) { + const host = normalizeHost(rawHost); + if (!isGitHubHost(host)) return yield* new GitHubNotSignedInError({ host }); + if ((yield* hostChoice(host))?.enabled === false) { + return yield* new GitHubHostDisabledError({ host }); + } + const token = yield* savedToken(host); + if (token === null) return Option.none(); + return Option.some({ + host, + token: Redacted.make(token), + source: "settings", + fingerprint: yield* fingerprintOf(host, token), + }); + }); + return GitHubCredentials.of({ get: Effect.fn("GitHubCredentials.get")(function* (rawHost) { const host = normalizeHost(rawHost); - if (!isGitHubHost(host)) return yield* new GitHubNotSignedInError({ host }); - const choice = yield* hostChoice(host); - if (choice?.enabled === false) { - return yield* new GitHubHostDisabledError({ host }); - } - const saved = yield* savedToken(host); - if (saved !== null) { - return { - host, - token: Redacted.make(saved), - source: "settings", - fingerprint: yield* fingerprintOf(host, saved), - } satisfies GitHubCredential; - } - return yield* Cache.get(cache, cacheKey(host, choice?.account)); + const fromSettings = yield* saved(host); + if (Option.isSome(fromSettings)) return fromSettings.value; + return yield* Cache.get(cache, cacheKey(host, (yield* hostChoice(host))?.account)); }), + saved, + fingerprint: fingerprintOf, invalidate: Effect.fn("GitHubCredentials.invalidate")(function (rawHost) { const host = normalizeHost(rawHost); return hostChoice(host).pipe( diff --git a/packages/source-control-github/src/server/GitHubPullRequestApi.test.ts b/packages/source-control-github/src/server/GitHubPullRequestApi.test.ts index 61005227a168c..3fd74e08130ec 100644 --- a/packages/source-control-github/src/server/GitHubPullRequestApi.test.ts +++ b/packages/source-control-github/src/server/GitHubPullRequestApi.test.ts @@ -21,6 +21,14 @@ import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSour import * as GitHubPullRequestApi from "./GitHubPullRequestApi.ts"; import * as GitHubPullRequestProvider from "./GitHubPullRequestProvider.ts"; import { KnownWorkflowRuns } from "./gitHubConditionalChecks.ts"; +import * as GitHubAppAccess from "./GitHubAppAccess.ts"; +import * as GitHubAppSignIn from "./GitHubAppSignIn.ts"; + +const appSignedOut = Layer.mock(GitHubAppSignIn.GitHubAppSignIn)({ + enabled: false, + token: () => Effect.succeedNone, + invalidate: () => Effect.void, +}); const encodeJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown)); @@ -112,6 +120,7 @@ const mockApi = Layer.succeed( GitHubRest.GitHubRest.of({ rest: (input) => mockedExecute({ kind: "rest", ...input }), credential: (host) => mockedCredential(host), + appCredential: () => Effect.succeedNone, }), ); @@ -167,8 +176,10 @@ const mockHttp = Layer.succeed( const layer = it.layer( GitHubPullRequestApi.layer.pipe( + Layer.provideMerge(GitHubAppAccess.layer), Layer.provideMerge(mockApi), Layer.provideMerge(GitHubRequestPolicy.layer), + Layer.provideMerge(appSignedOut), Layer.provideMerge(GitHubQuota.layer), Layer.provideMerge(SourceControlRateLimit.layer), Layer.provideMerge(mockHttp), @@ -176,6 +187,8 @@ const layer = it.layer( Layer.succeed(GitHubCredentials.GitHubCredentials, { get: (host) => Effect.map(mockedCredential(host), (held) => ({ ...held, host, source: "gh" as const })), + saved: () => Effect.succeedNone, + fingerprint: (host, token) => Effect.succeed(`${host}:${token}`), invalidate: () => Effect.void, }), ), @@ -623,6 +636,8 @@ it.effect( source: "gh" as const, fingerprint: `${host}:${activeToken.length}${activeToken.at(-1)}`, })), + saved: () => Effect.succeedNone, + fingerprint: (host, token) => Effect.succeed(`${host}:${token}`), invalidate: () => Effect.void, }), ); @@ -648,8 +663,9 @@ it.effect( ); const cli = yield* GitHubPullRequestApi.make.pipe( Effect.provide( - GitHubRest.layer.pipe( - Layer.provideMerge(Layer.mergeAll(credentials, http)), + GitHubAppAccess.layer.pipe( + Layer.provideMerge(GitHubRest.layer), + Layer.provideMerge(Layer.mergeAll(credentials, http, appSignedOut)), Layer.provideMerge(GitHubQuota.layer), Layer.provideMerge(SourceControlRateLimit.layer), Layer.merge(TestSourceControlHost.layer()), diff --git a/packages/source-control-github/src/server/GitHubPullRequestApi.ts b/packages/source-control-github/src/server/GitHubPullRequestApi.ts index 12e38f40a05ba..4b7014a890744 100644 --- a/packages/source-control-github/src/server/GitHubPullRequestApi.ts +++ b/packages/source-control-github/src/server/GitHubPullRequestApi.ts @@ -45,6 +45,7 @@ import { type PullRequestPreview, } from "@t3tools/contracts"; +import * as GitHubAppAccess from "./GitHubAppAccess.ts"; import * as GitHubRest from "./GitHubRest.ts"; import * as GitHubRequestPolicy from "./GitHubRequestPolicy.ts"; import * as GitHubGraphQlClient from "./gitHubGraphQlClient.ts"; @@ -405,7 +406,7 @@ export class GitHubPullRequestApi extends Context.Service< GitHubPullRequestApi, { readonly withVerifiedCredential: ( - input: { readonly cwd: string; readonly host: string }, + input: { readonly cwd: string; readonly host: string; readonly repository?: string }, use: (identity: { readonly accountId: string; readonly viewer: string; @@ -886,6 +887,7 @@ interface RestReadInput { /** @public */ export const make = Effect.gen(function* () { const api = yield* GitHubRest.GitHubRest; + const appAccess = yield* GitHubAppAccess.GitHubAppAccess; const policy = yield* GitHubRequestPolicy.GitHubRequestPolicy; const httpClient = yield* HttpClient.HttpClient; const sourceControlHost = yield* SourceControlHost.SourceControlHost; @@ -909,10 +911,18 @@ export const make = Effect.gen(function* () { const decodeRoutingIdentity = Schema.decodeUnknownEffect(Schema.fromJsonString(RoutingIdentity)); const decodeViewerIdentity = Schema.decodeUnknownEffect(RoutingIdentity); const captureVerifiedCredential = Effect.fn("GitHubPullRequestApi.captureVerifiedCredential")( - function* (input: { readonly cwd: string; readonly host: string }) { + function* (input: { + readonly cwd: string; + readonly host: string; + readonly repository?: string; + }) { const unavailable = () => new GitHubViewerLoginUnavailableError({ cwd: input.cwd }); const host = input.host.toLowerCase(); - const { token, fingerprint: key } = yield* api.credential(host); + // A pinned credential is never handed on to another, so pick one that reaches the repository. + const { token, fingerprint: key } = + input.repository === undefined + ? yield* api.credential(host) + : yield* appAccess.credentialFor(host, input.repository); const credential = { host, token, credentialFingerprint: key }; return yield* Effect.acquireUseRelease( Effect.sync(() => { @@ -2391,6 +2401,7 @@ export const make = Effect.gen(function* () { if (input.stackNumber !== undefined) return runGitHubStackAction({ ...input, stackNumber: input.stackNumber }).pipe( Effect.provideService(GitHubRest.GitHubRest, api), + Effect.provideService(GitHubAppAccess.GitHubAppAccess, appAccess), Effect.provideService(GitHubRequestPolicy.GitHubRequestPolicy, policy), Effect.provideService(HttpClient.HttpClient, httpClient), Effect.provideService(SourceControlHost.SourceControlHost, sourceControlHost), diff --git a/packages/source-control-github/src/server/GitHubPullRequestProvider.credential.test.ts b/packages/source-control-github/src/server/GitHubPullRequestProvider.credential.test.ts index 8bde631903ca8..231aec1f12aa6 100644 --- a/packages/source-control-github/src/server/GitHubPullRequestProvider.credential.test.ts +++ b/packages/source-control-github/src/server/GitHubPullRequestProvider.credential.test.ts @@ -12,6 +12,14 @@ import * as GitHubPullRequestApi from "./GitHubPullRequestApi.ts"; import * as GitHubPullRequestProvider from "./GitHubPullRequestProvider.ts"; import * as GitHubQuota from "./GitHubQuota.ts"; import * as GitHubRest from "./GitHubRest.ts"; +import * as GitHubAppAccess from "./GitHubAppAccess.ts"; +import * as GitHubAppSignIn from "./GitHubAppSignIn.ts"; + +const appSignedOut = Layer.mock(GitHubAppSignIn.GitHubAppSignIn)({ + enabled: false, + token: () => Effect.succeedNone, + invalidate: () => Effect.void, +}); it.effect("keeps branch deletion on the credential that passed source repository permissions", () => Effect.gen(function* () { @@ -27,6 +35,8 @@ it.effect("keeps branch deletion on the credential that passed source repository source: "gh" as const, fingerprint: `${host}:${activeToken}`, })), + saved: () => Effect.succeedNone, + fingerprint: (host, token) => Effect.succeed(`${host}:${token}`), invalidate: () => Effect.void, }), ); @@ -101,10 +111,11 @@ it.effect("keeps branch deletion on the credential that passed source repository const provider = yield* GitHubPullRequestProvider.make.pipe( Effect.provide( GitHubPullRequestApi.layer.pipe( + Layer.provideMerge(GitHubAppAccess.layer), // GraphQL reads go over `http` too, through the same credential and quota as REST. Layer.provide( GitHubRest.layer.pipe( - Layer.provideMerge(Layer.mergeAll(credentials, http)), + Layer.provideMerge(Layer.mergeAll(credentials, http, appSignedOut)), Layer.provideMerge(GitHubQuota.layer), Layer.provideMerge(SourceControlRateLimit.layer), ), diff --git a/packages/source-control-github/src/server/GitHubRequestPolicy.ts b/packages/source-control-github/src/server/GitHubRequestPolicy.ts index 47b145a5f1388..2c2935bd1fbcf 100644 --- a/packages/source-control-github/src/server/GitHubRequestPolicy.ts +++ b/packages/source-control-github/src/server/GitHubRequestPolicy.ts @@ -11,6 +11,10 @@ import * as Schema from "effect/Schema"; import * as Semaphore from "effect/Semaphore"; import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit"; +import * as Option from "effect/Option"; +import * as Result from "effect/Result"; + +import * as GitHubAppSignIn from "./GitHubAppSignIn.ts"; import * as GitHubCredentials from "./GitHubCredentials.ts"; import * as GitHubQuota from "./GitHubQuota.ts"; @@ -86,6 +90,7 @@ export class GitHubApiResponseError extends Schema.TaggedError = { readonly status: number | undefined; readonly headers: Readonly>; } & ( - | { readonly _tag: "Ok"; readonly value: A } + | { + readonly _tag: "Ok"; + readonly value: A; + /** GitHub left part of the answer out as not found or forbidden for this credential. */ + readonly withheld?: boolean; + } | { readonly _tag: "RateLimited" | "Unauthorized" | "NotFound" } | { readonly _tag: "Failed"; readonly messages?: ReadonlyArray | undefined } ); @@ -188,40 +198,109 @@ export class GitHubRequestPolicy extends Context.Service< input: GitHubRequest, send: (headers: Readonly>) => Effect.Effect, E, R>, ) => Effect.Effect; + /** + * The credential requests to `host` use: a token saved in Settings, else the app sign-in, + * else `GH_TOKEN` or `gh`. `personal` skips the app, for repositories it cannot reach. + */ readonly credential: ( host: string, - ) => Effect.Effect< - { readonly token: Redacted.Redacted; readonly fingerprint: string }, - GitHubApiError - >; + options?: { readonly personal?: boolean }, + ) => Effect.Effect; + /** The app sign-in's own credential, whatever else outranks it; none when not signed in. */ + readonly appCredential: ( + host: string, + ) => Effect.Effect, GitHubAppSignIn.GitHubAppSessionError>; } >()("@t3tools/source-control-github/server/GitHubRequestPolicy") {} +export interface GitHubHeldCredential { + readonly token: Redacted.Redacted; + readonly fingerprint: string; +} + +interface ChosenCredential extends GitHubHeldCredential { + readonly fromApp: boolean; +} + +/** + * The app reaches only repositories where it is installed. A not-found or "not accessible by + * integration" answer to its token is worth one more try with the user's own credential. + */ +function refusedToApp(answer: GitHubAnswer): boolean { + switch (answer._tag) { + case "NotFound": + return true; + case "Ok": + return answer.withheld === true; + case "Failed": + return ( + answer.status === 403 && + (answer.messages ?? []).some((message) => /not accessible by integration/i.test(message)) + ); + default: + return false; + } +} + /** @public */ export const make = Effect.gen(function* () { const credentials = yield* GitHubCredentials.GitHubCredentials; + const appSignIn = yield* GitHubAppSignIn.GitHubAppSignIn; const quota = yield* GitHubQuota.GitHubQuota; const limits = yield* SourceControlRateLimit.SourceControlRateLimit; const slots = yield* Semaphore.make(CONCURRENCY); - const credential: GitHubRequestPolicy["Service"]["credential"] = Effect.fn( - "GitHubRest.credential", + const personalCredential = (host: string) => + credentials.get(host).pipe( + Effect.map((held): ChosenCredential => ({ + token: held.token, + fingerprint: held.fingerprint, + fromApp: false, + })), + ); + + const appCredential: GitHubRequestPolicy["Service"]["appCredential"] = Effect.fn( + "GitHubRest.appCredential", )(function* (host) { - const normalized = normalizeHost(host); + const token = yield* appSignIn.token(normalizeHost(host)); + if (Option.isNone(token)) return Option.none(); + return Option.some({ + token: Redacted.make(token.value), + fingerprint: yield* credentials.fingerprint(normalizeHost(host), token.value), + }); + }); + + const choose = Effect.fn("GitHubRest.credential")(function* (host: string, personal: boolean) { const pinned = yield* PinnedGitHubCredential; if (pinned !== null) { - if (pinned.host !== normalized) { - return yield* new GitHubApiAuthenticationError({ - host: normalized, - operation: "credential", - }); + if (pinned.host !== host) { + return yield* new GitHubApiAuthenticationError({ host, operation: "credential" }); } - return { token: pinned.token, fingerprint: pinned.credentialFingerprint }; + return { + token: pinned.token, + fingerprint: pinned.credentialFingerprint, + fromApp: false, + } satisfies ChosenCredential; + } + const saved = yield* credentials.saved(host); + if (Option.isSome(saved)) { + return { token: saved.value.token, fingerprint: saved.value.fingerprint, fromApp: false }; + } + if (personal) return yield* personalCredential(host); + const app = yield* Effect.result(appCredential(host)); + // An app session that cannot refresh right now steps aside for the user's own credential. + if (Result.isFailure(app)) { + return yield* personalCredential(host).pipe(Effect.mapError(() => app.failure)); } - const held = yield* credentials.get(normalized); - return { token: held.token, fingerprint: held.fingerprint }; + if (Option.isNone(app.success)) return yield* personalCredential(host); + return { ...app.success.value, fromApp: true } satisfies ChosenCredential; }); + const credential: GitHubRequestPolicy["Service"]["credential"] = (host, options) => + choose(normalizeHost(host), options?.personal === true).pipe( + Effect.map(({ token, fingerprint }) => ({ token, fingerprint })), + ); + // The REST/GraphQL transport span owns request and quota metadata. const request: GitHubRequestPolicy["Service"]["request"] = Effect.fnUntraced(function* ( input: GitHubRequest, @@ -233,14 +312,17 @@ export const make = Effect.gen(function* () { "github.host": host, "github.operation": input.operation, }); - const { token, fingerprint } = yield* credential(host); const scope = yield* SourceControlRateLimit.CredentialScope; // A refusal pauses the whole host, whichever quota it spent: the key PullRequestService // records its own backoff under. const key = { provider: SourceControlProviderKind.make("github"), host }; // Settlement annotates the request span with status and rate-limit attributes. - const settle = Effect.fnUntraced(function* (lease: number, answer: GitHubAnswer) { + const settle = Effect.fnUntraced(function* ( + lease: number, + answer: GitHubAnswer, + token: Redacted.Redacted, + ) { yield* quota.observe(host, answer.headers); yield* Effect.annotateCurrentSpan({ ...(answer.status === undefined ? {} : { "http.response.status_code": answer.status }), @@ -264,6 +346,7 @@ export const make = Effect.gen(function* () { } case "Unauthorized": // The source may hold a newer token than the one that was refused. + yield* appSignIn.invalidate(host, Redacted.value(token)); yield* credentials.invalidate(host); return yield* new GitHubApiAuthenticationError(context); case "NotFound": @@ -277,39 +360,65 @@ export const make = Effect.gen(function* () { } }); - const run = Effect.gen(function* () { - const lease = yield* quota - .admit(host, input.resource, { allowReserve: input.allowReserve }) + /** One send under `held`; `Retry` hands an app-refused request to the user's credential. */ + const attempt = (held: ChosenCredential) => + slots + .withPermit( + Effect.gen(function* () { + const lease = yield* quota + .admit(host, input.resource, { allowReserve: input.allowReserve }) + .pipe( + Effect.andThen( + limits.check(key, input.allowReserve ? { allowPaused: true } : undefined), + ), + Effect.tapError((paused) => + Effect.annotateCurrentSpan({ + "github.paused": true, + "github.retry_at": paused.retryAt, + }), + ), + ); + // One deadline covers the headers and the body: a host that answers headers and then + // stalls must not hold a shared slot for undici's own five-minute body timeout. + const answer = yield* send({ + ...REQUEST_HEADERS, + authorization: `Bearer ${Redacted.value(held.token)}`, + }).pipe( + Effect.timeoutOrElse({ + duration: input.timeout ?? DEFAULT_TIMEOUT, + orElse: () => + Effect.fail( + new GitHubApiRequestError({ ...context, cause: new Cause.TimeoutError() }), + ), + }), + ); + if (held.fromApp && refusedToApp(answer)) { + const personal = yield* Effect.option(personalCredential(host)); + if (Option.isSome(personal)) { + yield* quota.observe(host, answer.headers); + yield* limits.recordSuccess({ ...key, lease }); + yield* Effect.annotateCurrentSpan({ "github.app_refused": true }); + return { _tag: "Retry", with: personal.value } as const; + } + } + return { _tag: "Done", value: yield* settle(lease, answer, held.token) } as const; + }), + ) + // The rate-limit scope follows the credential, so a pause on one account never blocks another. .pipe( - Effect.andThen(limits.check(key, input.allowReserve ? { allowPaused: true } : undefined)), - Effect.tapError((paused) => - Effect.annotateCurrentSpan({ - "github.paused": true, - "github.retry_at": paused.retryAt, - }), - ), + Effect.provideService(SourceControlRateLimit.CredentialScope, scope || held.fingerprint), ); - // One deadline covers the headers and the body: a host that answers headers and then - // stalls must not hold a shared slot for undici's own five-minute body timeout. - const answer = yield* send({ - ...REQUEST_HEADERS, - authorization: `Bearer ${Redacted.value(token)}`, - }).pipe( - Effect.timeoutOrElse({ - duration: input.timeout ?? DEFAULT_TIMEOUT, - orElse: () => - Effect.fail(new GitHubApiRequestError({ ...context, cause: new Cause.TimeoutError() })), - }), - ); - return yield* settle(lease, answer); - }); - // The rate-limit scope follows the credential, so a pause on one account never blocks another. - return yield* slots - .withPermit(run) - .pipe(Effect.provideService(SourceControlRateLimit.CredentialScope, scope || fingerprint)); + + // Only an app credential is handed on, so this runs at most twice. + let held = yield* choose(host, false); + while (true) { + const outcome = yield* attempt(held); + if (outcome._tag === "Done") return outcome.value; + held = outcome.with; + } }); - return GitHubRequestPolicy.of({ request, credential }); + return GitHubRequestPolicy.of({ request, credential, appCredential }); }); export const layer = Layer.effect(GitHubRequestPolicy, make); diff --git a/packages/source-control-github/src/server/GitHubRest.test.ts b/packages/source-control-github/src/server/GitHubRest.test.ts index a4ca7a14c77ab..fec5ab9242605 100644 --- a/packages/source-control-github/src/server/GitHubRest.test.ts +++ b/packages/source-control-github/src/server/GitHubRest.test.ts @@ -16,6 +16,13 @@ import * as GitHubQuota from "./GitHubQuota.ts"; import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit"; import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost"; +import * as GitHubAppSignIn from "./GitHubAppSignIn.ts"; + +const appSignedOut = Layer.mock(GitHubAppSignIn.GitHubAppSignIn)({ + enabled: false, + token: () => Effect.succeedNone, + invalidate: () => Effect.void, +}); const NOW = Date.parse("2026-10-05T12:00:00.000Z"); @@ -37,6 +44,8 @@ function harness(respond: (request: HttpClientRequest.HttpClientRequest) => Resp fingerprint: `${host}:${tokens[0]}`, }; }), + saved: () => Effect.succeedNone, + fingerprint: (host, token) => Effect.succeed(`${host}:${token}`), invalidate: () => Effect.sync(() => { invalidations++; @@ -52,7 +61,7 @@ function harness(respond: (request: HttpClientRequest.HttpClientRequest) => Resp }), ); const layer = GitHubRest.layer.pipe( - Layer.provide(Layer.mergeAll(credentials, http)), + Layer.provide(Layer.mergeAll(credentials, http, appSignedOut)), Layer.provideMerge(GitHubQuota.layer), Layer.provideMerge(SourceControlRateLimit.layer), ); @@ -231,10 +240,14 @@ describe("GitHubRest", () => { }); describe("GitHubCredentials", () => { + const unexpectedHttp = HttpClient.make((request) => + Effect.die(`Unexpected request to ${request.url}`), + ); const credentialsWith = (run: SourceControlHost.SourceControlHost["Service"]["process"]["run"]) => GitHubCredentials.layer.pipe( Layer.provide(TestSourceControlHost.layer({ process: { run } })), Layer.provide(NodeCrypto.layer), + Layer.provide(Layer.succeed(HttpClient.HttpClient, unexpectedHttp)), ); it.effect("does not cache a gh failure that is not about signing in", () => { diff --git a/packages/source-control-github/src/server/GitHubRest.ts b/packages/source-control-github/src/server/GitHubRest.ts index 6d7dd29049753..35bdfb39cc5c2 100644 --- a/packages/source-control-github/src/server/GitHubRest.ts +++ b/packages/source-control-github/src/server/GitHubRest.ts @@ -4,11 +4,11 @@ import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; -import * as Redacted from "effect/Redacted"; import * as Schema from "effect/Schema"; import { HttpClient, HttpClientRequest, type HttpClientResponse } from "effect/http"; import { collectUint8StreamText } from "@t3tools/provider-core/server/collectStreamText"; +import * as GitHubAppSignIn from "./GitHubAppSignIn.ts"; import * as GitHubCredentials from "./GitHubCredentials.ts"; import * as GitHubQuota from "./GitHubQuota.ts"; import * as GitHubRequestPolicy from "./GitHubRequestPolicy.ts"; @@ -49,12 +49,8 @@ export class GitHubRest extends Context.Service< GitHubRest, { readonly rest: (input: GitHubRestInput) => Effect.Effect; - readonly credential: ( - host: string, - ) => Effect.Effect< - { readonly token: Redacted.Redacted; readonly fingerprint: string }, - GitHubApiError - >; + readonly credential: GitHubRequestPolicy.GitHubRequestPolicy["Service"]["credential"]; + readonly appCredential: GitHubRequestPolicy.GitHubRequestPolicy["Service"]["appCredential"]; } >()("@t3tools/source-control-github/server/GitHubRest") {} @@ -212,7 +208,11 @@ export const make = Effect.gen(function* () { ); }; - return GitHubRest.of({ rest, credential: policy.credential }); + return GitHubRest.of({ + rest, + credential: policy.credential, + appCredential: policy.appCredential, + }); }); export const layer = Layer.effect(GitHubRest, make).pipe( @@ -226,5 +226,6 @@ export const layer = Layer.effect(GitHubRest, make).pipe( */ export const layerWithDependencies = layer.pipe( Layer.provideMerge(GitHubCredentials.layer), + Layer.provideMerge(GitHubAppSignIn.layer), Layer.provideMerge(GitHubQuota.layer), ); diff --git a/packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts b/packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts index 0bf7773aa0ea5..0fb270156ceaa 100644 --- a/packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts +++ b/packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts @@ -22,6 +22,13 @@ import * as GitHubCredentials from "./GitHubCredentials.ts"; import * as GitHubQuota from "./GitHubQuota.ts"; import { parseGitHubAuthStatus } from "./gitHubAuthStatus.ts"; import * as GitHubSourceControlProvider from "./GitHubSourceControlProvider.ts"; +import * as GitHubAppSignIn from "./GitHubAppSignIn.ts"; + +const appSignedOut = Layer.mock(GitHubAppSignIn.GitHubAppSignIn)({ + enabled: false, + token: () => Effect.succeedNone, + invalidate: () => Effect.void, +}); const encodeJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown)); @@ -220,15 +227,19 @@ function harness(input: { source: "gh" as const, fingerprint: `${host}:token`, })), + saved: () => Effect.succeedNone, + fingerprint: (host, token) => Effect.succeed(`${host}:${token}`), invalidate: () => Effect.void, }), GitHubQuota.layer, SourceControlRateLimit.layer, + appSignedOut, ), ), ); const layer = Layer.mergeAll( host, + appSignedOut, Layer.mock(GitHubRest.GitHubRest)(input.api ?? {}), policy, Layer.succeed(HttpClient.HttpClient, graphQlHttp(graphql, graphqlCalls)), @@ -831,6 +842,7 @@ it("reports every gh login and leads with the account Settings pin", () => { const auth = GitHubSourceControlProvider.parseGitHubAuth(multiAccountStatus(), { hosts: { "github.com": { account: "work", enabled: true } }, tokens: {}, + signIns: {}, }); assert.deepStrictEqual(auth.account, Option.some("work")); assert.deepStrictEqual(auth.accounts, [ @@ -844,6 +856,7 @@ it("reports unauthenticated when Settings turn off every signed-in host", () => const auth = GitHubSourceControlProvider.parseGitHubAuth(multiAccountStatus(), { hosts: { "github.com": { enabled: false } }, tokens: {}, + signIns: {}, }); assert.strictEqual(auth.status, "unauthenticated"); assert.deepStrictEqual( @@ -863,7 +876,7 @@ it("names the environment token that overrides the Settings choice", () => { tokenSource: "GH_TOKEN", }, ]), - { hosts: { "github.com": { account: "work", enabled: true } }, tokens: {} }, + { hosts: { "github.com": { account: "work", enabled: true } }, tokens: {}, signIns: {} }, ); assert.deepStrictEqual(auth.account, Option.some("bot")); assert.deepStrictEqual( diff --git a/packages/source-control-github/src/server/GitHubSourceControlProvider.ts b/packages/source-control-github/src/server/GitHubSourceControlProvider.ts index dda1c7c752777..bfc5fc29041ea 100644 --- a/packages/source-control-github/src/server/GitHubSourceControlProvider.ts +++ b/packages/source-control-github/src/server/GitHubSourceControlProvider.ts @@ -27,6 +27,7 @@ import { decodeJsonResult } from "@t3tools/shared/schemaJson"; import { isSshRemoteUrl } from "@t3tools/shared/sourceControl"; import { GH_PROCESS_CONCURRENCY } from "./GitHubCredentials.ts"; +import * as GitHubAppSignIn from "./GitHubAppSignIn.ts"; import * as GitHubRest from "./GitHubRest.ts"; import * as GitHubRequestPolicy from "./GitHubRequestPolicy.ts"; import * as GitHubChangeRequestTemplate from "./gitHubChangeRequestTemplate.ts"; @@ -212,6 +213,9 @@ export const makeDiscovery = Effect.gen(function* () { const sourceControlHost = yield* SourceControlHost.SourceControlHost; const process = sourceControlHost.process; const environment = yield* HostProcess.Environment; + const appSignIn = yield* GitHubAppSignIn.GitHubAppSignIn; + const offerAppSignIn = (item: A): A => + appSignIn.enabled ? { ...item, appSignIn: true } : item; return { type: "managed-cli", @@ -234,9 +238,18 @@ export const makeDiscovery = Effect.gen(function* () { spec: { ...discovery, parseAuth: (input) => parseGitHubAuth(input, settings) }, }); const savedToken = (settings.tokens["github.com"] ?? "").trim() !== ""; + const signedIn = + appSignIn.enabled && !savedToken && (settings.signIns["github.com"] ?? "").trim() !== ""; const variable = environmentTokenVariable(environment); - const tokenSource = savedToken ? "the token saved in Settings" : variable; - if (tokenSource === null || settings.hosts["github.com"]?.enabled === false) return cli; + const appName = GitHubClient.APP_SIGN_IN.appName; + const tokenSource = savedToken + ? "the token saved in Settings" + : signedIn + ? `the ${appName} sign-in` + : variable; + if (tokenSource === null || settings.hosts["github.com"]?.enabled === false) { + return offerAppSignIn(cli); + } const viewer = yield* api .rest({ host: "github.com", operation: "discovery", path: "user" }) .pipe(Effect.result); @@ -244,7 +257,7 @@ export const makeDiscovery = Effect.gen(function* () { ? Option.getOrUndefined(decodeViewer(viewer.success.body))?.login : undefined; const accounts = cli.auth.accounts === undefined ? {} : { accounts: cli.auth.accounts }; - return { + return offerAppSignIn({ ...cli, status: "available" as const, auth: { @@ -255,24 +268,28 @@ export const makeDiscovery = Effect.gen(function* () { host: "github.com", detail: savedToken ? "Using the token saved in Settings; it overrides GH_TOKEN and the gh login." - : `Using ${variable} from the server environment; it overrides the account chosen in Settings.`, + : signedIn + ? `Signed in with ${appName}.` + : `Using ${variable} from the server environment; it overrides the account chosen in Settings.`, }) : Result.isFailure(viewer) && viewer.failure._tag !== "GitHubApiAuthenticationError" ? providerAuth({ status: "unknown", host: "github.com", - detail: `Could not check ${savedToken ? tokenSource : `the token in ${tokenSource}`}: ${viewer.failure.message}`, + detail: `Could not check ${savedToken || signedIn ? tokenSource : `the token in ${tokenSource}`}: ${viewer.failure.message}`, }) : providerAuth({ status: "unauthenticated", host: "github.com", detail: savedToken ? "GitHub refused the token saved in Settings. Replace or remove it in Settings → Source Control." - : `GitHub refused the token in ${tokenSource}. Replace it, or unset it to use \`gh auth login\`.`, + : signedIn + ? `GitHub refused the ${appName} sign-in. Sign in again in Settings → Source Control.` + : `GitHub refused the token in ${tokenSource}. Replace it, or unset it to use \`gh auth login\`.`, })), ...accounts, }, - } satisfies SourceControlProviderDiscoveryItem; + } satisfies SourceControlProviderDiscoveryItem); }), // Claim disabled hosts so errors identify the setting. refineUnknownRemote: ({ context }) => { @@ -337,6 +354,7 @@ function fromGitHubApiError( case "GitHubApiNotFoundError": return failure(notFound, error); case "GitHubCliFailedError": + case "GitHubAppSessionError": case "GitHubApiResponseError": case "GitHubApiRequestError": return failure(error.message.trim() || "GitHub request failed.", error); @@ -983,7 +1001,11 @@ export const make = Effect.gen(function* () { const locator = parseGitHubRepositorySelector(input.repository, fallbackHost) ?? (/^[^/\s]+$/.test(bareName) - ? { host: fallbackHost, owner: yield* readViewerLogin(fallbackHost), name: bareName } + ? { + host: fallbackHost, + owner: yield* readViewerLogin(fallbackHost), + name: bareName, + } : null); if (locator === null) return yield* failure("Repositories are named owner/name."); return repositoryCloneUrls(yield* readRepository(locator)); @@ -991,7 +1013,9 @@ export const make = Effect.gen(function* () { (effect, input) => effect.pipe( Effect.mapError( - providerError("getRepositoryCloneUrls", input.cwd, { repository: input.repository }), + providerError("getRepositoryCloneUrls", input.cwd, { + repository: input.repository, + }), ), ), ), diff --git a/packages/source-control-github/src/server/driver.ts b/packages/source-control-github/src/server/driver.ts index a767cbbef8553..023098b0b63f5 100644 --- a/packages/source-control-github/src/server/driver.ts +++ b/packages/source-control-github/src/server/driver.ts @@ -1,12 +1,16 @@ +import { SourceControlSignInError, type SourceControlProviderKind } from "@t3tools/contracts"; import { defineSourceControlDriver, withOwnServices, + type SourceControlSignIn, } from "@t3tools/source-control-core/server/driver"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import { definition } from "../shared/definition.ts"; +import * as GitHubAppAccess from "./GitHubAppAccess.ts"; +import * as GitHubAppSignIn from "./GitHubAppSignIn.ts"; import * as GitHubMediaFetch from "./gitHubMediaFetch.ts"; import * as GitHubPullRequestApi from "./GitHubPullRequestApi.ts"; import * as GitHubPullRequestProvider from "./GitHubPullRequestProvider.ts"; @@ -16,14 +20,45 @@ import * as GitHubSourceControlProvider from "./GitHubSourceControlProvider.ts"; // `GitHubRest.layerWithDependencies` carries the quota reserve every GitHub reader shares, so it // must be built once; the driver is. const services = GitHubPullRequestApi.layer.pipe( + Layer.provideMerge(GitHubAppAccess.layer), Layer.provideMerge(GitHubRest.layerWithDependencies), ); +const makeSignIn = Effect.gen(function* () { + const appSignIn = yield* GitHubAppSignIn.GitHubAppSignIn; + const appAccess = yield* GitHubAppAccess.GitHubAppAccess; + const kind: SourceControlProviderKind = definition.kind; + return { + start: appSignIn.start, + wait: (host) => + appSignIn.wait(host).pipe( + Effect.andThen( + appAccess.viewer(host).pipe( + Effect.mapError( + (cause) => + new SourceControlSignInError({ + provider: kind, + host, + reason: "failed", + detail: "Signed in, but GitHub would not say which account it belongs to.", + cause, + }), + ), + ), + ), + Effect.map((account) => ({ account })), + ), + cancel: appSignIn.cancel, + access: appAccess.reach, + } satisfies SourceControlSignIn; +}); + const makeInstance = Effect.all({ sourceControl: GitHubSourceControlProvider.make, discovery: GitHubSourceControlProvider.makeDiscovery, pullRequests: GitHubPullRequestProvider.make, fetchMedia: GitHubMediaFetch.make, + signIn: makeSignIn, }); export const driver = defineSourceControlDriver({ diff --git a/packages/source-control-github/src/server/gitHubDiscovery.test.ts b/packages/source-control-github/src/server/gitHubDiscovery.test.ts index 85fecc967ac6b..cd8a838b23723 100644 --- a/packages/source-control-github/src/server/gitHubDiscovery.test.ts +++ b/packages/source-control-github/src/server/gitHubDiscovery.test.ts @@ -7,6 +7,7 @@ import * as Redacted from "effect/Redacted"; import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost"; import * as GitHubCredentials from "./GitHubCredentials.ts"; +import * as GitHubAppSignIn from "./GitHubAppSignIn.ts"; import * as GitHubRest from "./GitHubRest.ts"; import * as GitHubSourceControlProvider from "./GitHubSourceControlProvider.ts"; @@ -22,6 +23,7 @@ const makeDiscovery = (credential: GitHubRest.GitHubRest["Service"]["credential" Effect.provide( Layer.mergeAll( Layer.mock(GitHubRest.GitHubRest)({ credential }), + Layer.mock(GitHubAppSignIn.GitHubAppSignIn)({ enabled: false }), TestSourceControlHost.layer(), ), ), diff --git a/packages/source-control-github/src/server/gitHubGraphQlClient.test.ts b/packages/source-control-github/src/server/gitHubGraphQlClient.test.ts index 5027e618f9e93..667dc4f8f4c22 100644 --- a/packages/source-control-github/src/server/gitHubGraphQlClient.test.ts +++ b/packages/source-control-github/src/server/gitHubGraphQlClient.test.ts @@ -13,10 +13,20 @@ import * as GitHubCredentials from "./GitHubCredentials.ts"; import * as GitHubGraphQlClient from "./gitHubGraphQlClient.ts"; import * as GitHubQuota from "./GitHubQuota.ts"; import { PullRequestBatchesGraphQLGroup } from "./pullRequestBatches.graphql.ts"; +import * as GitHubAppSignIn from "./GitHubAppSignIn.ts"; + +const appSignedOut = Layer.mock(GitHubAppSignIn.GitHubAppSignIn)({ + enabled: false, + token: () => Effect.succeedNone, + invalidate: () => Effect.void, +}); const NOW = Date.parse("2026-10-05T12:00:00.000Z"); -function harness(respond: (request: HttpClientRequest.HttpClientRequest) => Response) { +function harness( + respond: (request: HttpClientRequest.HttpClientRequest) => Response, + options?: { readonly appToken?: string }, +) { const requests: Array = []; let invalidations = 0; const services = Layer.mergeAll( @@ -28,6 +38,8 @@ function harness(respond: (request: HttpClientRequest.HttpClientRequest) => Resp source: "gh" as const, fingerprint: `${host}:secret`, }), + saved: () => Effect.succeedNone, + fingerprint: (host, token) => Effect.succeed(`${host}:${token}`), invalidate: () => Effect.sync(() => { invalidations++; @@ -42,6 +54,13 @@ function harness(respond: (request: HttpClientRequest.HttpClientRequest) => Resp ), GitHubQuota.layer, SourceControlRateLimit.layer, + options?.appToken === undefined + ? appSignedOut + : Layer.mock(GitHubAppSignIn.GitHubAppSignIn)({ + enabled: true, + token: () => Effect.succeedSome(options.appToken ?? ""), + invalidate: () => Effect.void, + }), ); const layer = GitHubRest.layer.pipe(Layer.provideMerge(services)); return { layer, requests, invalidations: () => invalidations }; @@ -74,6 +93,26 @@ const readStats = (host: string, number = 7) => const stats = { additions: 3, deletions: 1 }; describe("GitHubGraphQlAuth", () => { + it.effect("asks again with the user's own credential when the app token gets NOT_FOUND", () => { + const { layer, requests } = harness( + (request) => + request.headers["authorization"] === "Bearer app-token" + ? json({ + data: { _0_repository: null }, + errors: [{ type: "NOT_FOUND", message: "Could not resolve to a Repository" }], + }) + : json({ data: { _0_repository: { pullRequest: stats } } }), + { appToken: "app-token" }, + ); + return Effect.gen(function* () { + expect(yield* readStats("github.com")).toEqual({ repository: { pullRequest: stats } }); + expect(requests.map((request) => request.headers["authorization"])).toEqual([ + "Bearer app-token", + "Bearer secret", + ]); + }).pipe(Effect.provide(layer)); + }); + it.effect( "sends each host's lookups to its own endpoint with its token, never in one batch", () => { diff --git a/packages/source-control-github/src/server/gitHubGraphQlClient.ts b/packages/source-control-github/src/server/gitHubGraphQlClient.ts index 36ea0765505a5..32198ad0b0236 100644 --- a/packages/source-control-github/src/server/gitHubGraphQlClient.ts +++ b/packages/source-control-github/src/server/gitHubGraphQlClient.ts @@ -77,9 +77,18 @@ export class GitHubGraphQlAuth extends GraphQLMiddleware.Service< headers: responseHeaders, ...(answer.errors === undefined ? {} : { graphqlErrors: answer.errors }), }); + const withheld = (answer.errors ?? []).some( + (error) => error["type"] === "NOT_FOUND" || error["type"] === "FORBIDDEN", + ); return rateLimited ? { _tag: "RateLimited" as const, status, headers: responseHeaders } - : { _tag: "Ok" as const, value: answer, status, headers: responseHeaders }; + : { + _tag: "Ok" as const, + value: answer, + withheld, + status, + headers: responseHeaders, + }; }), Effect.catchIf(GraphQLClientError.isGraphQLClientError, (error) => refused(error, { host, operation: target.operation }), diff --git a/packages/source-control-github/src/server/githubStackActions.test.ts b/packages/source-control-github/src/server/githubStackActions.test.ts index 5f9b96a60e1cd..3d95638bc48e9 100644 --- a/packages/source-control-github/src/server/githubStackActions.test.ts +++ b/packages/source-control-github/src/server/githubStackActions.test.ts @@ -13,6 +13,14 @@ import * as GitHubQuota from "./GitHubQuota.ts"; import * as GitHubRequestPolicy from "./GitHubRequestPolicy.ts"; import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost"; import { runGitHubStackAction as runStackAction } from "./githubStackActions.ts"; +import * as GitHubAppAccess from "./GitHubAppAccess.ts"; +import * as GitHubAppSignIn from "./GitHubAppSignIn.ts"; + +const appSignedOut = Layer.mock(GitHubAppSignIn.GitHubAppSignIn)({ + enabled: false, + token: () => Effect.succeedNone, + invalidate: () => Effect.void, +}); type Call = ReadonlyArray; type Send = (call: Call, kind: "graphql" | "rest") => Effect.Effect; @@ -86,13 +94,19 @@ const runGitHubStackAction = (send: Send, input: Parameters Effect.succeedNone, + fingerprint: (host, token) => Effect.succeed(`${host}:${token}`), invalidate: () => Effect.void, }), GitHubQuota.layer, SourceControlRateLimit.layer, + appSignedOut, ), ), ), + Layer.mock(GitHubAppAccess.GitHubAppAccess)({ + credentialFor: () => Effect.succeed({ token: Redacted.make("token"), fingerprint: "fp" }), + }), Layer.succeed(HttpClient.HttpClient, graphQlHttp(send)), ), ), diff --git a/packages/source-control-github/src/server/githubStackRebase.test.ts b/packages/source-control-github/src/server/githubStackRebase.test.ts index 725abeed42ca0..aa04c61744753 100644 --- a/packages/source-control-github/src/server/githubStackRebase.test.ts +++ b/packages/source-control-github/src/server/githubStackRebase.test.ts @@ -8,12 +8,12 @@ import * as Redacted from "effect/Redacted"; import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost"; -import * as GitHubRest from "./GitHubRest.ts"; +import * as GitHubAppAccess from "./GitHubAppAccess.ts"; import { cascadeRebaseStack } from "./githubStackRebase.ts"; const layer = Layer.mergeAll( - Layer.mock(GitHubRest.GitHubRest)({ - credential: () => Effect.succeed({ token: Redacted.make("token"), fingerprint: "fp" }), + Layer.mock(GitHubAppAccess.GitHubAppAccess)({ + credentialFor: () => Effect.succeed({ token: Redacted.make("token"), fingerprint: "fp" }), }), TestSourceControlHost.layerWithRealGit(), ).pipe(Layer.provideMerge(NodeServices.layer)); diff --git a/packages/source-control-github/src/server/githubStackRebase.ts b/packages/source-control-github/src/server/githubStackRebase.ts index cf795ab6e1f65..27595e7e7706d 100644 --- a/packages/source-control-github/src/server/githubStackRebase.ts +++ b/packages/source-control-github/src/server/githubStackRebase.ts @@ -4,7 +4,7 @@ import * as Redacted from "effect/Redacted"; import * as Schema from "effect/Schema"; import * as Base64 from "effect/encoding/Base64"; -import * as GitHubRest from "./GitHubRest.ts"; +import * as GitHubAppAccess from "./GitHubAppAccess.ts"; import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; export interface CascadeLayer { @@ -42,10 +42,11 @@ export const cascadeRebaseStack = Effect.fn("cascadeRebaseStack")(function* (inp readonly layers: ReadonlyArray; readonly remote?: string; }) { - const api = yield* GitHubRest.GitHubRest; + const appAccess = yield* GitHubAppAccess.GitHubAppAccess; const { process } = yield* SourceControlHost.SourceControlHost; const fileSystem = yield* FileSystem.FileSystem; - const { token } = yield* api.credential(input.host); + // A push takes one credential up front, so it must be one that reaches the repository. + const { token } = yield* appAccess.credentialFor(input.host, input.repository); const first = input.layers[0]; if (first === undefined) return 0; const directory = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-stack-rebase-" }).pipe( diff --git a/packages/source-control-github/src/shared/definition.ts b/packages/source-control-github/src/shared/definition.ts index 170395bb7eef5..965ed42814c19 100644 --- a/packages/source-control-github/src/shared/definition.ts +++ b/packages/source-control-github/src/shared/definition.ts @@ -8,6 +8,7 @@ import { parseChangeRequestPath, projectChangeRequestHost, remoteWebOrigin, + type SourceControlAppSignIn, } from "@t3tools/source-control-core/shared/definition"; import { gitHubMediaUrl } from "./gitHubMedia.ts"; @@ -42,6 +43,14 @@ function isRepositoryPath(repository: string): boolean { ); } +/** Signing in through the T3 Code for GitHub app lifts the per-user API rate limit to the app's. */ +export const APP_SIGN_IN: SourceControlAppSignIn = { + appName: "T3 Code for GitHub", + hosts: ["github.com"], + installUrl: "https://github.com/apps/t3-code-for-github/installations/new", + revokeUrl: "https://github.com/settings/apps/authorizations", +}; + export const settings = makeProviderSettingsSchema(hostCredentialSettingsFields); export type GitHubSettings = typeof settings.Type; @@ -58,6 +67,7 @@ export const definition = defineSourceControl({ tokenEnvVar: "GH_TOKEN", tokenAccess: "read and write access to pull requests and contents", newTokenUrl: (host) => `https://${host}/settings/personal-access-tokens/new`, + appSignIn: APP_SIGN_IN, }, changeRequest: { shortLabel: "PR", diff --git a/packages/source-control-testing/src/TestSourceControlHost.ts b/packages/source-control-testing/src/TestSourceControlHost.ts index d241f9f737fac..3f1ef6c2b4c2b 100644 --- a/packages/source-control-testing/src/TestSourceControlHost.ts +++ b/packages/source-control-testing/src/TestSourceControlHost.ts @@ -55,8 +55,10 @@ export const layer = ( Layer.effectContext( Effect.gen(function* () { const settings = yield* Ref.make(options.settings ?? DEFAULT_SERVER_SETTINGS); + const update = (patch: ServerSettingsPatch) => + Ref.updateAndGet(settings, (current) => applyServerSettingsPatch(current, patch)); const host = SourceControlHost.SourceControlHost.of({ - settings: { get: Ref.get(settings) }, + settings: { get: Ref.get(settings), update }, process: { run: options.process?.run ?? @@ -66,8 +68,7 @@ export const layer = ( }); return Context.make(SourceControlHost.SourceControlHost, host).pipe( Context.add(TestSourceControlHostSettings, { - update: (patch) => - Ref.update(settings, (current) => applyServerSettingsPatch(current, patch)), + update: (patch) => Effect.asVoid(update(patch)), }), ); }), From ff71186669c863aa976cee0fdb00c20c64ea92b4 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 27 Oct 2026 21:31:27 -0700 Subject: [PATCH 13/46] test(mobile): vp run mobile:behavior grades app behavior on pool devices Brings Theo's mobile behavior checks (#25876) into the local test suite beside mobile:e2e. A dev-only probe reads the app's own state through Metro's Hermes inspector, and a scripted Fx provider drives replies, so 25 cases grade scroll, frame drops, drafts, focus, status, and links without a model. The command leases an iPhone 18 Pro and an iPad Pro 13 from the Simlock pool, runs each with its own server and Metro, keeps the leases renewed, and writes one scoreboard to artifacts/mobile-behavior/. Pool errors retry, Metro serves no lazy chunks, and a run fails only on guard or control cases; bad cases reproduce known bugs. Co-Authored-By: Theo Browne Co-Authored-By: Claude Opus 5.5 --- .gitignore | 1 + apps/mobile/index.ts | 3 +- .../development/behavior-probe-entry.ts | 4 + .../features/development/behavior-probe.ts | 333 ++++++++++ knip.jsonc | 6 +- package.json | 1 + pnpm-lock.yaml | 3 + scripts/mobile-behavior.ts | 229 +++++++ scripts/mobile-behavior/README.md | 32 + scripts/mobile-behavior/cases.ts | 338 ++++++++++ scripts/mobile-behavior/device.ts | 307 +++++++++ scripts/mobile-behavior/environment.ts | 420 ++++++++++++ scripts/mobile-behavior/fake-fx.ts | 262 ++++++++ scripts/mobile-behavior/grade.ts | 233 +++++++ scripts/mobile-behavior/run.test.ts | 94 +++ scripts/mobile-behavior/run.ts | 298 +++++++++ scripts/mobile-behavior/scenarios.ts | 597 ++++++++++++++++++ scripts/package.json | 1 + 18 files changed, 3159 insertions(+), 3 deletions(-) create mode 100644 apps/mobile/src/features/development/behavior-probe-entry.ts create mode 100644 apps/mobile/src/features/development/behavior-probe.ts create mode 100644 scripts/mobile-behavior.ts create mode 100644 scripts/mobile-behavior/README.md create mode 100644 scripts/mobile-behavior/cases.ts create mode 100644 scripts/mobile-behavior/device.ts create mode 100644 scripts/mobile-behavior/environment.ts create mode 100644 scripts/mobile-behavior/fake-fx.ts create mode 100644 scripts/mobile-behavior/grade.ts create mode 100644 scripts/mobile-behavior/run.test.ts create mode 100644 scripts/mobile-behavior/run.ts create mode 100644 scripts/mobile-behavior/scenarios.ts diff --git a/.gitignore b/.gitignore index b33f3469c5345..b6b793e020ef1 100644 --- a/.gitignore +++ b/.gitignore @@ -37,6 +37,7 @@ apps/mobile/.android-cache/ /.generated/ artifacts/app-store/screenshots/ artifacts/mobile-e2e/ +artifacts/mobile-behavior/ .github/pr-assets/ native/**/target/ node_modules/ diff --git a/apps/mobile/index.ts b/apps/mobile/index.ts index 169d5cc4048ba..9367ade408e95 100644 --- a/apps/mobile/index.ts +++ b/apps/mobile/index.ts @@ -1,5 +1,6 @@ -// Must stay first: the render counter wraps the DevTools hook before any React commit. +// Must stay first: the render counter and behavior probe wrap the DevTools hook before any React commit. import "./src/features/development/render-counter-entry"; +import "./src/features/development/behavior-probe-entry"; // Next, so errors thrown while the rest of the app loads are recorded too. import "./src/features/observability/observe"; import { registerRootComponent } from "expo"; diff --git a/apps/mobile/src/features/development/behavior-probe-entry.ts b/apps/mobile/src/features/development/behavior-probe-entry.ts new file mode 100644 index 0000000000000..0771a35dd1c62 --- /dev/null +++ b/apps/mobile/src/features/development/behavior-probe-entry.ts @@ -0,0 +1,4 @@ +// Metro folds this condition before collecting dependencies, excluding the probe from release bundles. +if (__DEV__ && process.env.EXPO_PUBLIC_T3_BEHAVIOR_PROBE === "1") { + require("./behavior-probe"); +} diff --git a/apps/mobile/src/features/development/behavior-probe.ts b/apps/mobile/src/features/development/behavior-probe.ts new file mode 100644 index 0000000000000..03f9727602e51 --- /dev/null +++ b/apps/mobile/src/features/development/behavior-probe.ts @@ -0,0 +1,333 @@ +// Read by evals/mobile-behavior through the Hermes inspector; product modules load lazily so import order is unchanged. + +interface Fiber { + readonly tag: number; + readonly type: unknown; + readonly child: Fiber | null; + readonly sibling: Fiber | null; + readonly memoizedProps: Record | null; +} +interface DevToolsHook { + onCommitFiberRoot?: (id: unknown, root: { current: Fiber }, ...rest: unknown[]) => unknown; + __t3BehaviorProbe?: true; +} +interface ListState { + readonly scroll: number; + readonly scrollLength: number; + readonly contentLength: number; + readonly isAtEnd: boolean; + readonly start: number; + readonly end: number; + readonly data: ReadonlyArray<{ readonly id?: string; readonly key?: string }>; + readonly positionAtIndex: (index: number) => number; +} +interface Feed { + readonly threadId: string; + readonly environmentId: string; + readonly list: { readonly current: { getState?: () => ListState } | null } | undefined; + following: boolean | null; +} +interface Atom { + readonly _atom: true; +} + +// Every root React has committed; LogBox and other overlays commit their own. +const roots = new Set<{ current: Fiber }>(); + +function installCommitHook() { + const hook = (globalThis as { __REACT_DEVTOOLS_GLOBAL_HOOK__?: DevToolsHook }) + .__REACT_DEVTOOLS_GLOBAL_HOOK__; + if (!hook || typeof hook.onCommitFiberRoot !== "function") { + setTimeout(installCommitHook, 50); + return; + } + if (hook.__t3BehaviorProbe) return; + hook.__t3BehaviorProbe = true; + const original = hook.onCommitFiberRoot.bind(hook); + hook.onCommitFiberRoot = (id, next, ...rest) => { + roots.add(next); + return original(id, next, ...rest); + }; +} + +/** Visits every mounted fiber; `enter` returns the context passed to the fiber's children. */ +function walk(fiber: Fiber | null, context: C, enter: (fiber: Fiber, context: C) => C) { + let node = fiber; + while (node) { + const next = enter(node, context); + if (node.child) walk(node.child, next, enter); + node = node.sibling; + } +} + +const readAtom = (atom: Atom): T => { + const { appAtomRegistry } = require("../../state/atom-registry") as { + appAtomRegistry: { get: (atom: Atom) => unknown }; + }; + return appAtomRegistry.get(atom) as T; +}; + +/** Mounted thread feeds, the composer text the native editor was given, and the status pill. */ +function scanTree() { + const feeds = new Map(); + const editors: Array<{ value: string; expanded: boolean }> = []; + let floatingStatus: string | null = null; + let homeList: Array<{ + type: string; + threadId?: string; + status?: string; + pinned?: boolean; + count?: number; + expanded?: boolean; + }> | null = null; + const enter = (fiber: Fiber, feed: Feed | null): Feed | null => { + const props = fiber.memoizedProps; + if (!props || typeof props !== "object") return feed; + if ("listRef" in props && typeof props.threadId === "string" && "detailRef" in props) { + const existing = feeds.get(props.threadId); + if (existing) return existing; + const created: Feed = { + threadId: props.threadId, + environmentId: String(props.environmentId), + list: props.listRef as Feed["list"], + following: null, + }; + feeds.set(props.threadId, created); + return created; + } + if (feed && "maintainScrollAtEnd" in props && "maintainVisibleContentPosition" in props) + feed.following = props.maintainScrollAtEnd !== false; + if (typeof props.value === "string" && "chipsInert" in props) + editors.push({ value: props.value, expanded: props.chipsInert === false }); + if ("status" in props && "showScrollToEnd" in props) { + const status = props.status as { kind?: string } | null; + floatingStatus = status?.kind ?? null; + } + if (homeList === null && Array.isArray(props.data) && "renderItem" in props) { + const data = props.data as ReadonlyArray<{ + type?: string; + item?: { thread?: { id?: string }; pinned?: boolean }; + count?: number; + expanded?: boolean; + }>; + if (data.some((entry) => entry?.type?.startsWith("v2-"))) { + const { resolveThreadListV2Status } = require("../threads/threadListV2") as { + resolveThreadListV2Status: (thread: unknown) => string; + }; + homeList = data.map((entry) => ({ + type: String(entry.type), + ...(entry.item?.thread?.id + ? { + threadId: entry.item.thread.id, + status: resolveThreadListV2Status(entry.item.thread), + pinned: entry.item.pinned === true, + } + : {}), + ...(typeof entry.count === "number" ? { count: entry.count } : {}), + ...(typeof entry.expanded === "boolean" ? { expanded: entry.expanded } : {}), + })); + } + } + return feed; + }; + for (const root of roots) walk(root.current, null, enter); + return { feeds: [...feeds.values()], editors, floatingStatus, homeList }; +} + +function feedState(feed: Feed) { + const state = feed.list?.current?.getState?.(); + if (!state) return { threadId: feed.threadId, ready: false as const }; + const start = Math.max(0, state.start); + const end = Math.min(state.data.length - 1, state.end); + const rows: Record = {}; + for (let index = start; index <= end; index++) { + const row = state.data[index]; + const key = row?.id ?? row?.key; + if (key) rows[key] = Math.round(state.positionAtIndex(index) - state.scroll); + } + return { + threadId: feed.threadId, + ready: true as const, + scroll: Math.round(state.scroll), + viewport: Math.round(state.scrollLength), + content: Math.round(state.contentLength), + fromEnd: Math.round(state.contentLength - state.scroll - state.scrollLength), + atEnd: state.isAtEnd, + following: feed.following, + count: state.data.length, + /** Screen y of each visible row, keyed by row id. */ + rows, + }; +} + +function route() { + const { rootNavigationRef } = + require("../../lib/rootNavigation") as typeof import("../../lib/rootNavigation"); + if (!rootNavigationRef.isReady()) return null; + const current = rootNavigationRef.getCurrentRoute(); + const { workspaceHasColumns } = require("../workspace/workspace-selection-store") as { + workspaceHasColumns: () => boolean; + }; + return { + name: current?.name ?? null, + params: (current?.params ?? {}) as Record, + columns: workspaceHasColumns(), + }; +} + +function snapshot() { + const scan = scanTree(); + const at = route(); + const threadId = typeof at?.params.threadId === "string" ? at.params.threadId : null; + const environmentId = + typeof at?.params.environmentId === "string" ? at.params.environmentId : null; + const threadKey = threadId && environmentId ? `${environmentId}:${threadId}` : null; + const { composerDraftsAtom } = require("../../state/use-composer-drafts") as { + composerDraftsAtom: Atom; + }; + const { threadComposerErrorsAtom } = require("../../state/thread-composer-error") as { + threadComposerErrorsAtom: Atom; + }; + const drafts = readAtom>(composerDraftsAtom); + const errors = readAtom>(threadComposerErrorsAtom); + const { KeyboardController } = require("react-native-keyboard-controller") as { + KeyboardController: { isVisible: () => boolean }; + }; + const feed = visibleFeed(scan.feeds); + const { environmentThreadShells } = require("../../state/threads") as { + environmentThreadShells: { threadShellsAtom: Atom }; + }; + const { resolveThreadListV2Status } = require("../threads/threadListV2") as { + resolveThreadListV2Status: (thread: unknown) => string; + }; + const shells = readAtom>( + environmentThreadShells.threadShellsAtom, + ); + return { + /** Saved environments the app has threads from. */ + environments: [...new Set(shells.map((shell) => shell.environmentId))], + /** The app's own status for every thread it knows, including rows folded into a shelf. */ + threads: Object.fromEntries( + shells.map((shell) => [shell.id, resolveThreadListV2Status(shell)]), + ), + route: at, + feed: feed ? feedState(feed) : null, + composer: { + key: threadKey, + draft: threadKey ? (drafts[threadKey]?.text ?? "") : null, + drafts: Object.fromEntries( + Object.entries(drafts).map(([key, draft]) => [key, draft.text ?? ""]), + ), + editors: scan.editors, + error: threadKey ? (errors[threadKey]?.message ?? null) : null, + keyboard: KeyboardController.isVisible(), + }, + floatingStatus: scan.floatingStatus, + homeList: scan.homeList, + }; +} + +const frames = { js: [] as number[], on: false }; + +function jsFrame(time: number) { + if (!frames.on) return; + frames.js.push(time); + requestAnimationFrame(jsFrame); +} + +function uiFramesStart() { + "worklet"; + const ui = globalThis as { __t3UiFrames?: number[]; __t3UiFramesOn?: boolean }; + ui.__t3UiFrames = []; + ui.__t3UiFramesOn = true; + const tick = (time: number) => { + if (!ui.__t3UiFramesOn) return; + ui.__t3UiFrames!.push(time); + requestAnimationFrame(tick); + }; + requestAnimationFrame(tick); +} + +function uiFramesStop() { + "worklet"; + const ui = globalThis as { __t3UiFrames?: number[]; __t3UiFramesOn?: boolean }; + ui.__t3UiFramesOn = false; + return ui.__t3UiFrames ?? []; +} + +let samples: Array> = []; +let sampled: Feed | null = null; + +// The feed is found once per recording; a tree walk on every frame would itself cause jank. +function sampleFeed(time: number) { + if (!sampled) return; + const { following: _following, ...state } = feedState(sampled); + samples.push({ at: time, ...state }); + requestAnimationFrame(sampleFeed); +} + +function visibleFeed(feeds: ReadonlyArray) { + const threadId = route()?.params.threadId; + return feeds.find((feed) => feed.threadId === threadId) ?? feeds.at(-1) ?? null; +} + +installCommitHook(); + +Object.assign(globalThis, { + __t3Behavior: { + open(url: string) { + const { Linking } = require("react-native") as typeof import("react-native"); + void Linking.openURL(url); + return true; + }, + /** Restarts the JS app; deferred so the inspector call can reply first. */ + reload() { + const { DevSettings } = require("react-native") as typeof import("react-native"); + setTimeout(() => DevSettings.reload(), 100); + return true; + }, + state: () => JSON.stringify(snapshot()), + /** Removes these saved environments (and their cached threads) through the app's own command. */ + async forgetEnvironments(ids: ReadonlyArray) { + const { runAtomCommand } = require("@t3tools/client-runtime/state/runtime") as { + runAtomCommand: ( + registry: unknown, + command: unknown, + value: unknown, + options: object, + ) => Promise; + }; + const { appAtomRegistry } = require("../../state/atom-registry") as { + appAtomRegistry: unknown; + }; + const { environmentCatalog } = require("../../connection/catalog") as { + environmentCatalog: { remove: unknown }; + }; + for (const id of ids) + await runAtomCommand(appAtomRegistry, environmentCatalog.remove, id, { + label: "behavior probe", + }); + return true; + }, + /** Records JS and UI frame times, and the visible feed on every JS frame. */ + recordStart(withFeed: boolean) { + const { scheduleOnUI } = + require("react-native-worklets") as typeof import("react-native-worklets"); + frames.js = []; + frames.on = true; + requestAnimationFrame(jsFrame); + scheduleOnUI(uiFramesStart); + samples = []; + sampled = withFeed ? visibleFeed(scanTree().feeds) : null; + if (sampled) requestAnimationFrame(sampleFeed); + return true; + }, + recordStop() { + const { runOnUISync } = + require("react-native-worklets") as typeof import("react-native-worklets"); + frames.on = false; + sampled = null; + return JSON.stringify({ js: frames.js, ui: runOnUISync(uiFramesStop), feed: samples }); + }, + }, +}); diff --git a/knip.jsonc b/knip.jsonc index 4ff0f92ecfdd2..7efe20acfb080 100644 --- a/knip.jsonc +++ b/knip.jsonc @@ -21,12 +21,14 @@ // worktree setup (from t3.json) run directly. release-desktop.yml bundles the // Windows verify script with esbuild through a path knip does not trace, // electron-builder loads the macOS signer by path, and mobile-e2e.ts runs - // agent-device's CLI by path. release-desktop.yml runs the desktop launch - // check through xvfb-run, which knip does not trace. + // agent-device's CLI by path, and mobile-behavior.ts spawns its runner by path. + // release-desktop.yml runs the desktop launch check through xvfb-run, which knip + // does not trace. "entry": [ // These fixture scripts are injected into a browser page by path. "fixtures/*/*.js", "knip-schemas.ts", + "mobile-behavior/run.ts", "mobile-native-client.ts", "setup-worktree.ts", "sign-macos.ts", diff --git a/package.json b/package.json index d0808c9d60a8e..1c4ffa679029c 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "knip:production": "knip --production --preprocessor ./scripts/knip-schemas.ts", "lint:mobile": "node scripts/mobile-native-static-check.ts", "mobile:install": "node scripts/mobile-native-client.ts ensure", + "mobile:behavior": "node scripts/mobile-behavior.ts", "mobile:e2e": "node scripts/mobile-e2e.ts", "test": "vp run -r test", "test:cached": "node scripts/test-cached.ts", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cd4d38370135a..0adcb65c50062 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2281,6 +2281,9 @@ importers: '@electron/osx-sign': specifier: 2.7.0 version: 2.7.0 + '@t3tools/contracts': + specifier: workspace:* + version: link:../packages/contracts '@t3tools/shared': specifier: workspace:* version: link:../packages/shared diff --git a/scripts/mobile-behavior.ts b/scripts/mobile-behavior.ts new file mode 100644 index 0000000000000..aa5f28f7483f3 --- /dev/null +++ b/scripts/mobile-behavior.ts @@ -0,0 +1,229 @@ +#!/usr/bin/env node +// @effect-diagnostics nodeBuiltinImport:off globalDate:off globalTimers:off globalFetch:off - Host-side device automation uses Node subprocess and filesystem APIs directly. + +import * as NodeChildProcess from "node:child_process"; +import * as NodeFS from "node:fs"; +import * as NodeFSP from "node:fs/promises"; +import * as NodeOS from "node:os"; +import * as NodePath from "node:path"; +import * as NodeProcess from "node:process"; +import * as NodeReadline from "node:readline"; +import * as NodeURL from "node:url"; + +import { CASES } from "./mobile-behavior/cases.ts"; +import { blockingResults, resultsFor, summarize, type SampleRow } from "./mobile-behavior/grade.ts"; +import { Device, type Pool, poolError, run } from "./mobile-e2e/device.ts"; +import type { Pose } from "./mobile-e2e/poses.ts"; + +const REPO_ROOT = NodePath.resolve(NodePath.dirname(NodeURL.fileURLToPath(import.meta.url)), ".."); + +/** The devices the behavior cases run on; each runs in parallel with its own server and Metro. */ +const TARGETS = [ + { id: "iphone-18-pro", device: "iPhone 18 Pro", form: "phone", behaviorPose: "phone" }, + { id: "ipad-pro-13", device: "iPad Pro 13-inch (M5)", form: "tablet", behaviorPose: "ipad" }, +] as const; +type Target = (typeof TARGETS)[number]; + +const log = (scope: string, message: string) => NodeProcess.stderr.write(`[${scope}] ${message}\n`); + +interface TargetRun { + readonly target: Target; + readonly directory: string; + readonly error?: string; +} + +/** Keeps a lease alive while the cases run: they go minutes without an agent-device call. */ +function renewWhileRunning(pool: Pool, stateDir: string, session: string, token: string) { + const gateway = pool.agentDeviceUrl.replace(/\/v1\/agent-device\/?$/, ""); + const timer = setInterval(() => { + const record = NodePath.join(stateDir, "remote-connections", `${session}.json`); + void NodeFSP.readFile(record, "utf8") + .then((text) => (JSON.parse(text) as { leaseId: string }).leaseId) + .then((leaseId) => + fetch(`${gateway}/v1/leases/${encodeURIComponent(leaseId)}/renew`, { + method: "POST", + headers: { authorization: `Bearer ${token}` }, + }), + ) + .catch(() => undefined); + }, 90_000); + return () => clearInterval(timer); +} + +async function runTarget( + target: Target, + pool: Pool, + runId: string, + outputDirectory: string, + config: string, + stateDir: string, + token: string, + passThrough: ReadonlyArray, +): Promise { + const directory = NodePath.join(outputDirectory, target.id); + await NodeFSP.mkdir(directory, { recursive: true }); + const pose: Pose = { + id: target.id, + platform: "ios", + device: target.device, + form: target.form, + appearance: "light", + }; + let device: Device | null = null; + for (let attempt = 1; !device; attempt++) { + try { + log(target.id, `leasing ${target.device}`); + device = await Device.lease(pose, pool, attempt === 1 ? runId : `${runId}-retry`); + log(target.id, "installing the dev client"); + await device.install(); + } catch (error) { + const message = error instanceof Error ? error.message : String(error); + await device?.release(); + device = null; + if (attempt >= 2 || !poolError(message)) return { target, directory, error: message }; + log(target.id, `retrying after: ${message.split("\n")[0]}`); + } + } + const stopRenewing = renewWhileRunning(pool, stateDir, device.session, token); + try { + log(target.id, "running the behavior cases"); + const child = NodeChildProcess.spawn( + NodeProcess.execPath, + [ + "scripts/mobile-behavior/run.ts", + "--out", + directory, + "--label", + target.id, + "--pose", + target.behaviorPose, + "--device-config", + config, + "--device-session", + device.session, + "--agent-device", + pool.agentDevice, + ...passThrough, + ], + { cwd: REPO_ROOT, env: NodeProcess.env, stdio: ["ignore", "pipe", "pipe"] }, + ); + const transcript = NodeFS.createWriteStream(NodePath.join(directory, "run.log")); + // The scoreboard and any fatal error go to stdout, progress to stderr; the transcript keeps both. + NodeReadline.createInterface({ input: child.stdout }).on("line", (line) => + transcript.write(`${line}\n`), + ); + NodeReadline.createInterface({ input: child.stderr }).on("line", (line) => { + transcript.write(`${line}\n`); + if (/#\d+: /.test(line)) log(target.id, line); + }); + const code = await new Promise((resolve) => + child.on("close", (exit) => resolve(exit ?? 1)), + ); + transcript.end(); + const hasResults = NodeFS.existsSync(NodePath.join(directory, "results.jsonl")); + return hasResults || code === 0 + ? { target, directory } + : { target, directory, error: `the cases stopped (exit ${code}); see ${directory}/run.log` }; + } finally { + stopRenewing(); + await device.release(); + } +} + +async function main() { + const args = NodeProcess.argv.slice(2); + const values = (flag: string) => + args.flatMap((arg, index) => (args[index - 1] === flag ? [arg] : [])); + if (args.includes("--list")) { + for (const target of TARGETS) NodeProcess.stdout.write(`${target.id}\t${target.device}\n`); + for (const testCase of CASES) + NodeProcess.stdout.write(`${testCase.id}\t${testCase.kind}\t${testCase.source}\n`); + return; + } + const selected = values("--device"); + const unknown = selected.filter((id) => !TARGETS.some((target) => target.id === id)); + if (unknown.length) throw new Error(`Unknown device ${unknown.join(", ")}. Run with --list.`); + const targets = TARGETS.filter((target) => selected.length === 0 || selected.includes(target.id)); + const passThrough = [ + ...values("--samples").flatMap((value) => ["--samples", value]), + ...values("--case").flatMap((value) => ["--case", value]), + ]; + + const agentDeviceUrl = NodeProcess.env.SIMLOCK_AGENT_DEVICE_URL; + const token = NodeProcess.env.AGENT_DEVICE_DAEMON_AUTH_TOKEN; + if (!agentDeviceUrl || !token) + throw new Error( + "Set SIMLOCK_AGENT_DEVICE_URL (/v1/agent-device) and AGENT_DEVICE_DAEMON_AUTH_TOKEN (a Simlock agent token) to run on the device pool.", + ); + + const runId = new Date().toISOString().replaceAll(/[:.]/g, "-"); + const outputDirectory = NodePath.join(REPO_ROOT, "artifacts/mobile-behavior", runId); + await NodeFSP.mkdir(outputDirectory, { recursive: true }); + const rootDir = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-mobile-behavior-")); + const workspace = await NodeFSP.realpath(outputDirectory); + const config = NodePath.join(rootDir, "agent-device.json"); + const stateDir = NodePath.join(rootDir, "agent-device"); + await NodeFSP.writeFile( + config, + JSON.stringify({ daemonBaseUrl: agentDeviceUrl, daemonAuthToken: token, stateDir }), + { mode: 0o600 }, + ); + const pool: Pool = { + repoRoot: REPO_ROOT, + agentDevice: NodePath.join(REPO_ROOT, "scripts/node_modules/agent-device/bin/agent-device.mjs"), + agentDeviceUrl, + workspace, + env: { ...NodeProcess.env, AGENT_DEVICE_CONFIG: config, PWD: workspace }, + }; + try { + // A cache miss builds the dev client, which can outlast a lease, so it is ready first. + log("behavior", "preparing the ios dev client"); + await run( + NodeProcess.execPath, + ["scripts/mobile-native-client.ts", "ensure", "ios", "--prepare"], + { cwd: REPO_ROOT, timeoutMs: 1_800_000 }, + ); + const runs = await Promise.all( + targets.map((target) => + runTarget(target, pool, runId, outputDirectory, config, stateDir, token, passThrough), + ), + ); + + const rows = runs.flatMap((entry) => { + const file = NodePath.join(entry.directory, "results.jsonl"); + if (!NodeFS.existsSync(file)) return []; + return NodeFS.readFileSync(file, "utf8") + .split("\n") + .filter(Boolean) + .map((line) => JSON.parse(line) as SampleRow); + }); + const results = resultsFor(rows, CASES); + const stopped = runs.filter((entry) => entry.error); + const summary = summarize({ + cases: CASES, + results, + header: [ + `# Mobile behavior · ${runId}`, + "", + "Cells show pass or FAIL, passed/samples, and the key metric's median. `bad` cases reproduce known bugs and are expected to fail until they are fixed.", + ...stopped.map((entry) => `\n**${entry.target.id} did not finish:** ${entry.error}`), + ].join("\n"), + }); + const summaryFile = NodePath.join(outputDirectory, "summary.md"); + await NodeFSP.writeFile(summaryFile, summary); + const blocking = blockingResults(results, CASES); + for (const result of blocking) + log(result.device, `${result.id}: ${result.pass === null ? "incomplete" : "FAIL"}`); + for (const entry of stopped) log(entry.target.id, entry.error!); + NodeProcess.stdout.write( + `${JSON.stringify({ summary: summaryFile, cases: results.length, blocking: blocking.length, stopped: stopped.length })}\n`, + ); + process.exitCode = blocking.length || stopped.length ? 1 : 0; + } finally { + await NodeFSP.rm(rootDir, { recursive: true, force: true }); + } +} + +if (import.meta.main) { + await main(); +} diff --git a/scripts/mobile-behavior/README.md b/scripts/mobile-behavior/README.md new file mode 100644 index 0000000000000..d2a5e0b7407a6 --- /dev/null +++ b/scripts/mobile-behavior/README.md @@ -0,0 +1,32 @@ +# Mobile behavior + +`vp run mobile:behavior` checks what the iOS app does, from its own state rather than pixels: scroll position, dropped frames, composer drafts and focus, status labels, and which thread opens. It leases an iPhone 18 Pro and an iPad Pro 13 from the Simlock pool and runs the cases on both in parallel. It is the companion to `vp run mobile:e2e`, which checks layout. + +No model runs. Each device gets its own server whose only provider, `evalbot`, is an Fx instance backed by [`fake-fx.ts`](fake-fx.ts). A `[[eval:…]]` marker in a message picks its scripted reply: a paced stream, a held turn, a question, two subagents, or a failure. Each device also gets its own Metro with the dev-only probe ([`behavior-probe.ts`](../../apps/mobile/src/features/development/behavior-probe.ts)) compiled in. The probe reads app state through Metro's Hermes inspector, and agent-device drives the touches. + +## Run + +Needs `SIMLOCK_AGENT_DEVICE_URL` and `AGENT_DEVICE_DAEMON_AUTH_TOKEN`, like `mobile:e2e`. + +```sh +vp run mobile:behavior # both devices, 3 samples per case +vp run mobile:behavior --device iphone-18-pro --samples 1 --case scroll-up-never-teleports +vp run mobile:behavior --list # devices and cases +``` + +Output lands in `artifacts/mobile-behavior//`: `summary.md` is the scoreboard. Each device's folder holds `results.jsonl` (one row per case sample), `run.log`, `metro.log`, and the server under `env/`. A full run takes about 15 minutes. + +A case fails when half or more of its graded samples fail, and is `incomplete` when errors outnumber graded samples. The run fails on a `guard` or `control` case that fails or is incomplete. `bad` cases reproduce known bugs and fail until those are fixed. + +## Metrics + +- **Scroll.** The probe reads the thread list's state on every JS frame and records each visible row's y on screen. `backwardPx` is the largest move against the drag. `teleports` counts one-frame moves over 400 pt, faster than a real fling. `maxFromEndPx` is the distance from the latest message. +- **Jank.** JS-thread frames from `requestAnimationFrame` in the probe, UI-thread frames from a worklet loop. Simulators render at 60 Hz, and injected swipes load the simulator's UI thread, so swipe cases grade JS frames only. +- **Composer.** The draft store, the editor's text from the accessibility tree, whether it expanded, and whether the keyboard is up. +- **Status.** Each Home row's status from the app's own `resolveThreadListV2Status`, and the Working shelf count, against server state. + +## Add a case + +Cases are data in [`cases.ts`](cases.ts): an `id`, a `kind` (`bad` fails on `main` today, `guard` must keep passing, `control` is ordinary use), its `source` issue, the `scenario` that produces its metrics, a `key` metric, and `checks`. A new flow goes in [`scenarios.ts`](scenarios.ts). Add a case before its fix, so it fails on `main` while the bug exists; flip it from `bad` to `guard` when the fix lands. + +Not covered: Live Activities and notification taps (they need a T3 Connect relay and delivered pushes), native editor scroll, the background-work pill and async questions (Fx has no background tasks), and 120 Hz pacing. diff --git a/scripts/mobile-behavior/cases.ts b/scripts/mobile-behavior/cases.ts new file mode 100644 index 0000000000000..962c423e3377b --- /dev/null +++ b/scripts/mobile-behavior/cases.ts @@ -0,0 +1,338 @@ +/** + * Cases for the mobile behavior eval. `bad` fails on main today and must pass once fixed; + * `guard` passes on main and must keep passing (its issue is fixed, or does not show on iOS); + * `control` is ordinary use that must keep passing. Each names the scenario that produces its metrics and the checks on them. + */ +import type { Check } from "./grade.ts"; +import type { Pose, ScenarioName } from "./scenarios.ts"; + +export interface MobileBehaviorCase { + readonly id: string; + readonly area: "scroll" | "jank" | "composer" | "status" | "wrong-thread"; + readonly kind: "bad" | "guard" | "control"; + /** The issue or PR it came from. */ + readonly source: string; + /** The real bad experience, or the good one a guard protects. */ + readonly experience: string; + readonly scenario: ScenarioName; + /** The metric the scoreboard shows. */ + readonly key: string; + readonly checks: ReadonlyArray; + /** Omit to run on every pose. */ + readonly poses?: ReadonlyArray; +} + +/** + * Frame budget checks for jank cases: at most 5% of UI frames and 10% of JS frames dropped. + * Swipe-driven cases check JS frames only: the injected swipes cost the simulator's UI thread. + */ +const smooth: ReadonlyArray = [ + { metric: "uiDroppedPct", max: 5 }, + { metric: "jsDroppedPct", max: 10 }, +]; + +export const CASES: ReadonlyArray = [ + { + id: "scroll-up-never-moves-backward", + area: "scroll", + kind: "guard", + source: "#22350", + experience: "Flicking up a long thread, content jerks back down toward the latest message.", + scenario: "scrollUpLongThread", + key: "backwardPx", + checks: [ + { metric: "movedPx", min: 300 }, + { metric: "backwardPx", max: 24 }, + ], + }, + { + id: "scroll-up-never-teleports", + area: "scroll", + kind: "guard", + source: "#12072", + experience: "Dragging up a little after scroll-to-end, the viewport jumps far away.", + scenario: "scrollUpLongThread", + key: "largestStepPx", + checks: [ + { metric: "movedPx", min: 300 }, + { metric: "teleports", equals: 0 }, + ], + }, + { + id: "streaming-stays-at-latest", + area: "scroll", + kind: "bad", + source: "#15469", + experience: + "While a reply streams at the bottom, the thread jumps far above the latest message.", + scenario: "streamInView", + key: "settledFromEndPx", + checks: [ + { metric: "settledFromEndPx", max: 64 }, + { metric: "followingAtEnd", equals: true }, + ], + }, + { + id: "send-in-long-thread-stays-at-end", + area: "scroll", + kind: "guard", + source: "#13706", + experience: + "Sending in a long thread, the timeline jumps far up instead of showing the new message.", + scenario: "sendInLongThread", + key: "maxFromEndPx", + checks: [ + { metric: "maxFromEndPx", max: 1500 }, + { metric: "finalFromEndPx", max: 24 }, + ], + }, + { + id: "background-finish-keeps-scroll", + area: "scroll", + kind: "guard", + source: "#15165", + experience: "Another thread finishing resets the open thread's scroll position.", + scenario: "backgroundFinish", + key: "largestStepPx", + checks: [{ metric: "largestStepPx", max: 4 }], + }, + { + id: "jank-streaming-long-reply", + area: "jank", + kind: "bad", + source: "#15641, #14010, #19120", + experience: "A long reply streaming into a long thread freezes or stutters the app.", + scenario: "streamInView", + key: "jsDroppedPct", + checks: smooth, + }, + { + id: "jank-scrolling-settled-thread", + area: "jank", + kind: "control", + source: "#13925 (Android)", + experience: "Scrolling a long settled thread drops frames.", + scenario: "scrollUpLongThread", + key: "jsP99Ms", + checks: [ + { metric: "movedPx", min: 300 }, + { metric: "jsDroppedPct", max: 10 }, + ], + }, + { + id: "jank-scrolling-subagent-thread", + area: "jank", + kind: "control", + source: "#18376 (Android)", + experience: "Scrolling a subagent's thread is far less smooth than its parent.", + scenario: "openSubagentThread", + key: "jsP99Ms", + checks: [ + { metric: "movedPx", min: 300 }, + { metric: "jsDroppedPct", max: 10 }, + ], + }, + { + id: "draft-survives-thread-switch", + area: "composer", + kind: "guard", + source: "#17422", + experience: "A typed draft is gone after switching threads and coming back.", + scenario: "draftThreadSwitch", + key: "draftKept", + checks: [ + { metric: "draftKept", equals: true }, + { metric: "editorShowsDraft", equals: true }, + ], + }, + { + id: "draft-never-leaks-to-other-thread", + area: "composer", + kind: "guard", + source: "#7390", + experience: "Text typed in a streaming thread shows up in another thread's composer.", + scenario: "draftThreadSwitch", + key: "leakedIntoOtherEditor", + checks: [ + { metric: "leakedIntoOtherDraft", equals: false }, + { metric: "leakedIntoOtherEditor", equals: false }, + ], + }, + { + id: "draft-survives-background", + area: "composer", + kind: "guard", + source: "#17422, #7390", + experience: "A typed draft is gone after the app goes to the background and back.", + scenario: "draftBackground", + key: "draftKept", + checks: [ + { metric: "draftKept", equals: true }, + { metric: "editorShowsDraft", equals: true }, + ], + }, + { + id: "composer-clears-after-send", + area: "composer", + kind: "guard", + source: "#20982", + experience: "After sending, the message text stays in the composer.", + scenario: "sendInLongThread", + key: "editorCleared", + checks: [ + { metric: "draftCleared", equals: true }, + { metric: "editorCleared", equals: true }, + ], + }, + { + id: "composer-tap-focuses-text", + area: "composer", + kind: "guard", + source: "#24303", + experience: "Tapping the composer's text area opens the model picker instead of the keyboard.", + scenario: "tapComposer", + key: "editorFocused", + checks: [ + { metric: "stayedOnThread", equals: true }, + { metric: "editorFocused", equals: true }, + ], + }, + { + id: "composer-keeps-focus-after-command-pick", + area: "composer", + kind: "guard", + source: "#10956", + experience: "Picking a skill or command from the / menu closes the keyboard.", + scenario: "skillPick", + key: "editorFocused", + checks: [{ metric: "editorFocused", equals: true }], + }, + { + id: "failed-send-is-never-silent", + area: "composer", + kind: "guard", + source: "#15801", + experience: "A send the server will reject looks sent, with no error.", + scenario: "failedSend", + // The cell shows which path ran: Send blocked, or a rejected send with an error. + key: "sendBlocked", + checks: [{ metric: "silentFailure", equals: false }], + }, + { + id: "working-shelf-clears-when-done", + area: "status", + kind: "bad", + source: "#24901", + experience: "A finished thread stays in the Working shelf.", + scenario: "workingShelfFinish", + key: "shelfCountAfter", + checks: [ + { metric: "shelfShownWhileWorking", equals: true }, + { metric: "shelfCountAfter", equals: 0 }, + { metric: "statusAfter", equals: "ready" }, + ], + }, + { + id: "pending-question-shows-input", + area: "status", + kind: "guard", + source: "#24901", + experience: "A thread waiting on a question shows as Working instead of needing input.", + scenario: "pendingQuestion", + key: "status", + checks: [ + { metric: "status", equals: "input" }, + { metric: "shelfCount", equals: 0 }, + ], + }, + { + id: "running-subagents-not-idle", + area: "status", + kind: "guard", + source: "#16464, #4962", + experience: "A thread with subagents still running looks idle in the list and the thread.", + scenario: "subagentsRunning", + key: "status", + checks: [ + { metric: "status", equals: "working" }, + { metric: "threadShowsStatus", equals: true }, + ], + }, + { + id: "long-press-pin-pins-that-row", + area: "wrong-thread", + kind: "guard", + source: "#20428", + experience: "Pin from a row's long-press menu pins a different thread.", + scenario: "pinFromLongPress", + key: "targetPinned", + checks: [ + { metric: "targetPinned", equals: true }, + { metric: "otherPinned", equals: 0 }, + ], + }, + { + id: "link-opens-new-thread", + area: "wrong-thread", + kind: "guard", + source: "#14689 (web)", + experience: "A link to a just-created thread redirects to another thread.", + scenario: "linkToNewThread", + key: "opensTarget", + checks: [{ metric: "opensTarget", equals: true }], + }, + { + id: "link-opens-archived-thread", + area: "wrong-thread", + kind: "guard", + source: "#16443", + experience: "A link to an archived thread sends you Home.", + scenario: "linkToArchivedThread", + key: "opensTarget", + checks: [{ metric: "opensTarget", equals: true }], + }, + { + id: "folded-duo-back-opens-right-thread", + area: "wrong-thread", + kind: "guard", + source: "#20633", + experience: "On a folded iPhone Duo, after going back, the next thread opens off screen.", + scenario: "duoBackThenOpen", + key: "titleOnScreen", + poses: ["duo-closed"], + checks: [ + { metric: "opensTarget", equals: true }, + { metric: "titleOnScreen", equals: true }, + ], + }, + { + id: "control-open-thread-from-list", + area: "wrong-thread", + kind: "control", + source: "control", + experience: "Tapping a row opens that thread.", + scenario: "openFromList", + key: "opensTarget", + checks: [{ metric: "opensTarget", equals: true }], + }, + { + id: "control-send-reaches-server", + area: "composer", + kind: "control", + source: "control", + experience: "A message sent from the composer reaches the server once.", + scenario: "sendInLongThread", + key: "serverMessages", + checks: [{ metric: "serverMessages", equals: 1 }], + }, + { + id: "control-streaming-reply-renders", + area: "scroll", + kind: "control", + source: "control", + experience: "A streaming reply grows in the open thread.", + scenario: "streamInView", + key: "contentGrewPx", + checks: [{ metric: "contentGrewPx", min: 500 }], + }, +]; diff --git a/scripts/mobile-behavior/device.ts b/scripts/mobile-behavior/device.ts new file mode 100644 index 0000000000000..916e3adb21245 --- /dev/null +++ b/scripts/mobile-behavior/device.ts @@ -0,0 +1,307 @@ +// @effect-diagnostics nodeBuiltinImport:off preferSchemaOverJson:off - drives agent-device and the Hermes inspector; the probe types its own JSON. +/** + * One simulator, driven two ways: T3's `agent-device` CLI for real touches and system actions, + * and the dev-only behavior probe (`apps/mobile/src/features/development/behavior-probe.ts`) + * through Metro's Hermes inspector for exact app state. + */ +import * as NodeChildProcess from "node:child_process"; +import * as NodeOS from "node:os"; +import * as NodePath from "node:path"; +import * as NodeSocket from "@effect/platform-node/NodeSocket"; +import * as Clock from "effect/Clock"; +import * as Effect from "effect/Effect"; +import * as Schedule from "effect/Schedule"; +import * as Schema from "effect/Schema"; +import { FetchHttpClient, HttpClient, HttpClientResponse } from "effect/http"; +import { Socket } from "effect/socket"; + +import { poolError } from "../mobile-e2e/device.ts"; + +const InspectorTargets = Schema.Array( + Schema.Struct({ + appId: Schema.String, + deviceName: Schema.optional(Schema.String), + webSocketDebuggerUrl: Schema.String, + }), +); + +export class DeviceError extends Schema.TaggedError()("DeviceError", { + message: Schema.String, +}) {} + +export const APP_ID = "com.t3tools.t3code.dev"; +export const SCHEME = "t3code-dev"; + +export interface ProbeState { + readonly route: { + readonly name: string | null; + readonly params: Record; + readonly columns: boolean; + } | null; + readonly feed: FeedSample | null; + readonly composer: { + readonly key: string | null; + readonly draft: string | null; + readonly drafts: Record; + readonly editors: ReadonlyArray<{ readonly value: string; readonly expanded: boolean }>; + readonly error: string | null; + readonly keyboard: boolean; + }; + readonly floatingStatus: string | null; + readonly environments: ReadonlyArray; + /** The app's status for every thread it knows, by thread id. */ + readonly threads: Record; + readonly homeList: ReadonlyArray<{ + readonly type: string; + readonly threadId?: string; + readonly status?: string; + readonly pinned?: boolean; + readonly count?: number; + readonly expanded?: boolean; + }> | null; +} + +export interface FeedSample { + readonly at?: number; + readonly threadId: string; + readonly ready: boolean; + readonly scroll?: number; + readonly viewport?: number; + readonly content?: number; + readonly fromEnd?: number; + readonly atEnd?: boolean; + readonly following?: boolean | null; + readonly count?: number; + readonly rows?: Record; +} + +export interface Recording { + readonly js: ReadonlyArray; + readonly ui: ReadonlyArray; + readonly feed: ReadonlyArray; +} + +export interface AxNode { + readonly type?: string; + readonly label?: string; + readonly value?: string; + readonly kind?: string; + readonly editable?: boolean; + readonly enabled?: boolean; + readonly rect?: { + readonly x: number; + readonly y: number; + readonly width: number; + readonly height: number; + }; +} + +export interface DeviceOptions { + /** `agent-device` executable and the target args `device_open` returned. */ + readonly agentDevice: string; + readonly targetArgs: ReadonlyArray; + readonly metro: string; + /** Simulator name as Metro's inspector lists it; empty for the only app on this run's Metro. */ + readonly deviceName: string; +} + +export function makeDevice(options: DeviceOptions) { + const run = (args: ReadonlyArray, timeoutMs = 90_000) => + Effect.try({ + try: () => + NodeChildProcess.execFileSync(options.agentDevice, [...args, ...options.targetArgs], { + encoding: "utf8", + timeout: timeoutMs, + stdio: ["ignore", "pipe", "pipe"], + }), + catch: (cause) => { + // The thrown message starts with the full command line; the reason is in its output. + const output = cause as { readonly stdout?: string; readonly stderr?: string }; + const reason = `${output.stderr ?? ""}${output.stdout ?? ""}`.trim() || String(cause); + return new DeviceError({ + message: `agent-device ${args.join(" ")} failed: ${reason.slice(-400)}`, + }); + }, + }).pipe( + // A shared pool's workers drop off the gateway for seconds at a time. + Effect.retry({ + times: 4, + while: (error) => poolError(error.message), + schedule: Schedule.spaced("5 seconds"), + }), + Effect.withSpan("device.agentDevice", { attributes: { command: args[0] } }), + ); + + const evaluate = Effect.fn("device.evaluate")( + function* (expression: string) { + const targets = yield* HttpClient.get(`${options.metro}/json/list`).pipe( + Effect.flatMap(HttpClientResponse.schemaBodyJson(InspectorTargets)), + Effect.provide(FetchHttpClient.layer), + Effect.mapError( + () => new DeviceError({ message: `Metro at ${options.metro} did not answer.` }), + ), + ); + // Pool simulators carry generated names; a run's own Metro then serves only its device. + const target = targets.findLast( + (entry) => + entry.appId === APP_ID && + (!options.deviceName || entry.deviceName === options.deviceName), + ); + if (!target) + return yield* new DeviceError({ + message: `No app on ${options.deviceName} is attached to Metro.`, + }); + // Expo drops inspector sockets whose Origin is not the URL Metro advertises. + const socket = yield* Socket.makeWebSocket(target.webSocketDebuggerUrl, { + headers: { origin: new URL(options.metro).origin }, + openTimeout: "10 seconds", + }); + const writer = yield* socket.writer; + const reader = yield* socket.reader; + yield* writer.write( + JSON.stringify({ + id: 1, + method: "Runtime.evaluate", + params: { expression, returnByValue: true, awaitPromise: true }, + }), + ); + while (true) { + for (const frame of yield* reader.pull) { + const reply = JSON.parse( + typeof frame === "string" ? frame : new TextDecoder().decode(frame), + ) as { + id?: number; + result?: { result?: { value?: unknown }; exceptionDetails?: unknown }; + }; + if (reply.id !== 1) continue; + if (reply.result?.exceptionDetails) + return yield* new DeviceError({ + message: `${expression} threw ${JSON.stringify(reply.result.exceptionDetails).slice(0, 400)}`, + }); + return reply.result?.result?.value; + } + } + }, + Effect.scoped, + Effect.provide(NodeSocket.layerWebSocketConstructorWS), + Effect.timeoutOrElse({ + duration: "20 seconds", + orElse: () => Effect.fail(new DeviceError({ message: "The inspector did not answer." })), + }), + Effect.mapError((error) => + error._tag === "DeviceError" + ? error + : new DeviceError({ message: `Inspector: ${error.message}` }), + ), + ); + + const state = evaluate("__t3Behavior.state()").pipe( + Effect.map((value) => JSON.parse(String(value)) as ProbeState), + ); + + /** Polls the probe until `predicate` holds; fails with the last state after `timeoutMs`. */ + const waitFor = Effect.fn("device.waitFor")(function* ( + description: string, + predicate: (state: ProbeState) => boolean, + timeoutMs = 15_000, + ) { + const deadline = (yield* Clock.currentTimeMillis) + timeoutMs; + let last: ProbeState | null = null; + while ((yield* Clock.currentTimeMillis) < deadline) { + last = yield* state.pipe(Effect.orElseSucceed(() => null)); + if (last && predicate(last)) return last; + yield* Effect.sleep("250 millis"); + } + return yield* new DeviceError({ + message: `Timed out waiting for ${description}. Last route: ${JSON.stringify(last?.route ?? null)}`, + }); + }); + + const nodes = run(["snapshot", "--json"]).pipe( + Effect.map((out) => (JSON.parse(out) as { data: { nodes: ReadonlyArray } }).data.nodes), + ); + + const center = (node: AxNode) => ({ + x: Math.round(node.rect!.x + node.rect!.width / 2), + y: Math.round(node.rect!.y + node.rect!.height / 2), + }); + + const screenSize = run([ + "screenshot", + NodePath.join(NodeOS.tmpdir(), "t3-mobile-behavior-screen.png"), + "--json", + ]).pipe( + Effect.map((out) => { + const { data } = JSON.parse(out) as { data: { logicalWidth: number; logicalHeight: number } }; + return { width: data.logicalWidth, height: data.logicalHeight }; + }), + ); + + return { + options, + run, + evaluate, + state, + waitFor, + nodes, + center, + open: (path: string) => + evaluate(`__t3Behavior.open(${JSON.stringify(`${SCHEME}://${path.replace(/^\//, "")}`)})`), + openUrl: (url: string) => evaluate(`__t3Behavior.open(${JSON.stringify(url)})`), + /** Reloads JS, then opens Home; the app restores the last thread, and split layouts keep one open. */ + reload: Effect.fn("device.reload")(function* (ready: (state: ProbeState) => boolean) { + yield* evaluate("__t3Behavior.reload()").pipe(Effect.ignore); + yield* Effect.sleep("2 seconds"); + yield* waitFor("the thread list after reload", ready, 60_000); + yield* evaluate(`__t3Behavior.open(${JSON.stringify(`${SCHEME}://`)})`); + // Split layouts may keep a thread beside Home, so they get a short wait for the Home route. + const home = (value: ProbeState) => ready(value) && value.route?.name === "Home"; + const state = yield* waitFor("Home", home, 5_000).pipe(Effect.option); + if (state._tag === "Some") return state.value; + return yield* waitFor( + "the split layout", + (value) => ready(value) && value.route?.columns === true, + ); + }), + recordStart: (withFeed: boolean) => evaluate(`__t3Behavior.recordStart(${withFeed})`), + recordStop: evaluate("__t3Behavior.recordStop()").pipe( + Effect.map((value) => JSON.parse(String(value)) as Recording), + ), + press: (selector: string) => run(["press", selector]), + pressAt: (x: number, y: number) => run(["press", String(x), String(y)]), + longPress: (selector: string) => run(["longpress", selector, "800"]), + type: (text: string) => run(["type", text]), + /** Drags from y1 to y2 at x; a downward drag scrolls the content up. */ + swipe: (x: number, y1: number, y2: number) => + run(["swipe", String(x), String(y1), String(x), String(y2)]), + /** + * Answers iOS's "Open in" prompt. XCTest cannot see the iOS 27.1 prompt, so when `alert` fails + * this taps where its Open button sits, just right of and below the screen's center. + */ + acceptOpenPrompt: run(["alert", "accept"], 60_000).pipe( + Effect.catch(() => + screenSize.pipe( + Effect.flatMap(({ width, height }) => + run(["press", String(Math.round(width / 2 + 76)), String(Math.round(height / 2 + 8))]), + ), + ), + ), + ), + home: run(["home"]), + /** The Duo simulator often follows a fold without a hinge reading, so the screen's shape confirms it. */ + fold: (pose: "closed" | "open") => + run(["fold", pose]).pipe( + Effect.catch((error) => + screenSize.pipe( + Effect.filterOrFail( + ({ width, height }) => (pose === "closed") === width < height, + () => error, + ), + ), + ), + ), + back: run(["back"]), + }; +} + +export type Device = ReturnType; diff --git a/scripts/mobile-behavior/environment.ts b/scripts/mobile-behavior/environment.ts new file mode 100644 index 0000000000000..aa526a1d943e0 --- /dev/null +++ b/scripts/mobile-behavior/environment.ts @@ -0,0 +1,420 @@ +// @effect-diagnostics nodeBuiltinImport:off globalTimers:off - the eval owns a server subprocess and a scratch workspace. +/** + * An isolated T3 server for one device run. Its only provider is `evalbot`, an Fx instance + * backed by `fake-fx.ts`, so every reply is scripted. State lives under the run's `--out`. + */ +import * as NodeChildProcess from "node:child_process"; +import * as NodeFS from "node:fs"; +import * as NodeNet from "node:net"; +import * as NodePath from "node:path"; +import * as NodeSocket from "@effect/platform-node/NodeSocket"; +import { + CommandId, + EnvironmentId, + ExecutionEnvironmentDescriptor, + MessageId, + ORCHESTRATION_PROTOCOL_HEADER, + ORCHESTRATION_PROTOCOL_VERSION, + OrchestrationV2ShellSnapshot, + ProjectId, + ProviderInstanceId, + ThreadId, + WsRpcGroup, + type OrchestrationV2ThreadProjection, +} from "@t3tools/contracts"; +import * as Crypto from "effect/Crypto"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as Schema from "effect/Schema"; +import { FetchHttpClient, HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http"; +import { RpcClient, RpcSerialization } from "effect/rpc"; +import { Socket } from "effect/socket"; + +const REPO_ROOT = NodePath.resolve(import.meta.dirname, "../.."); +export const EVAL_INSTANCE = ProviderInstanceId.make("evalbot"); +export const EVAL_MODEL = "eval/scripted"; + +export const uuid = Effect.flatMap(Crypto.Crypto, (crypto) => crypto.randomUUIDv4()); + +export class EnvironmentError extends Schema.TaggedError()("EnvironmentError", { + message: Schema.String, +}) {} + +export const freePort = () => + Effect.callback((resume) => { + const server = NodeNet.createServer(); + server.listen(0, () => { + const address = server.address(); + server.close(() => + resume(Effect.succeed(typeof address === "object" && address ? address.port : 0)), + ); + }); + }); + +const waitForPort = Effect.fn("environment.waitForPort")(function* (port: number) { + for (let attempt = 0; attempt < 120; attempt++) { + const open = yield* Effect.callback((resume) => { + const socket = NodeNet.connect(port, "127.0.0.1"); + socket.once("connect", () => (socket.destroy(), resume(Effect.succeed(true)))); + socket.once("error", () => resume(Effect.succeed(false))); + }); + if (open) return; + yield* Effect.sleep("500 millis"); + } + return yield* new EnvironmentError({ message: `The server never listened on ${port}.` }); +}); + +/** Runs a server CLI command and returns both output streams. */ +const runCli = (args: ReadonlyArray) => + Effect.try({ + try: () => { + const result = NodeChildProcess.spawnSync("node", ["apps/server/src/bin.ts", ...args], { + cwd: REPO_ROOT, + encoding: "utf8", + }); + if (result.status !== 0) throw new Error(result.stderr); + return { stdout: result.stdout, stderr: result.stderr }; + }, + catch: (cause) => + new EnvironmentError({ message: `bin.ts ${args.join(" ")} failed: ${cause}` }), + }); + +function writeScratch(baseDir: string) { + const workspace = NodePath.join(baseDir, "workspace/acme-app"); + const control = NodePath.join(baseDir, "control"); + NodeFS.mkdirSync(NodePath.join(baseDir, "userdata"), { recursive: true }); + NodeFS.mkdirSync(control, { recursive: true }); + NodeFS.mkdirSync(workspace, { recursive: true }); + NodeFS.writeFileSync(NodePath.join(workspace, "README.md"), "# acme app\n"); + // The / menu's pickable row; without it, fx finds only skills in folders above `--out`. + const skill = NodePath.join(workspace, ".fx/skills/eval-notes"); + NodeFS.mkdirSync(skill, { recursive: true }); + NodeFS.writeFileSync( + NodePath.join(skill, "SKILL.md"), + "---\nname: eval-notes\ndescription: Takes notes for the eval.\n---\nTake notes.\n", + ); + const git = (...args: string[]) => + NodeChildProcess.execFileSync("git", ["-C", workspace, ...args], { stdio: "ignore" }); + git("init", "-q"); + git("add", "-A"); + git("-c", "user.name=eval", "-c", "user.email=eval@example.com", "commit", "-qm", "init"); + const launcher = NodePath.join(baseDir, "fx"); + NodeFS.writeFileSync( + launcher, + `#!/bin/sh\nexec node ${JSON.stringify(NodePath.join(import.meta.dirname, "fake-fx.ts"))} "$@"\n`, + { mode: 0o755 }, + ); + NodeFS.writeFileSync( + NodePath.join(baseDir, "userdata/settings.json"), + JSON.stringify({ + providerInstances: { + [EVAL_INSTANCE]: { + driver: "fx", + displayName: "Eval Bot", + environment: [{ name: "T3_EVAL_FX_CONTROL_DIR", value: control }], + config: { enabled: true, binaryPath: launcher }, + }, + }, + }), + ); + return { workspace, control }; +} + +/** Starts the server in the caller's scope; the scope's end stops it. */ +export const startEnvironment = Effect.fn("environment.start")(function* (input: { + readonly baseDir: string; + /** Origin the device uses to reach this machine, without a port. */ + readonly deviceHost: string; +}) { + NodeFS.rmSync(input.baseDir, { recursive: true, force: true }); + const { workspace, control } = writeScratch(input.baseDir); + const home = NodePath.join(input.baseDir, "home"); + NodeFS.mkdirSync(home, { recursive: true }); + const port = yield* freePort(); + const log = NodeFS.openSync(NodePath.join(input.baseDir, "server.log"), "w"); + yield* Effect.acquireRelease( + Effect.sync(() => + NodeChildProcess.spawn( + "node", + [ + "apps/server/src/bin.ts", + "serve", + "--host", + "0.0.0.0", + "--port", + String(port), + "--base-dir", + input.baseDir, + "--no-browser", + "--log-level", + "warn", + workspace, + ], + // A scratch HOME keeps the user's own skills and config out of the app under test. + { cwd: REPO_ROOT, stdio: ["ignore", log, log], env: { ...process.env, HOME: home } }, + ), + ), + (child) => Effect.sync(() => child.kill("SIGTERM")), + ); + yield* waitForPort(port); + const origin = `http://127.0.0.1:${port}`; + const issued = yield* runCli([ + "auth", + "session", + "issue", + "--base-dir", + input.baseDir, + "--token-only", + ]); + // The token is the last line of stdout; stderr is only a fallback. + const lastLine = (text: string) => + text + .split("\n") + .filter((line) => line.trim()) + .at(-1) + ?.trim(); + const token = lastLine(issued.stdout) ?? lastLine(issued.stderr) ?? ""; + const http = (yield* HttpClient.HttpClient).pipe( + HttpClient.mapRequest((request) => + request.pipe( + HttpClientRequest.bearerToken(token), + HttpClientRequest.setHeader( + ORCHESTRATION_PROTOCOL_HEADER, + String(ORCHESTRATION_PROTOCOL_VERSION), + ), + ), + ), + HttpClient.filterStatusOk, + ); + const descriptor = yield* http + .get(`${origin}/.well-known/t3/environment`) + .pipe(Effect.flatMap(HttpClientResponse.schemaBodyJson(ExecutionEnvironmentDescriptor))); + const shell = () => + http + .get(`${origin}/api/orchestration/shell`) + .pipe(Effect.flatMap(HttpClientResponse.schemaBodyJson(OrchestrationV2ShellSnapshot))); + const createProject = Effect.fn("environment.createProject")(function* ( + title: string, + workspaceRoot: string, + ) { + const projectId = ProjectId.make(yield* uuid); + yield* http.execute( + HttpClientRequest.post(`${origin}/api/projects/mutate`).pipe( + HttpClientRequest.bodyJsonUnsafe({ + type: "project.create", + commandId: yield* uuid, + projectId, + title, + workspaceRoot, + }), + ), + ); + return projectId; + }); + const projectId = yield* createProject("acme-app", workspace); + const ticket = yield* http + .execute(HttpClientRequest.post(`${origin}/api/auth/websocket-ticket`)) + .pipe( + Effect.flatMap(HttpClientResponse.schemaBodyJson(Schema.Struct({ ticket: Schema.String }))), + ); + // Built into the caller's scope: `Effect.provide` would close the socket once `make` returns. + const protocol = yield* Layer.build( + RpcClient.layerProtocolSocket().pipe( + Layer.provide( + Socket.layerWebSocket( + `${origin.replace(/^http/, "ws")}/ws?orchestrationProtocol=${ORCHESTRATION_PROTOCOL_VERSION}&wsTicket=${encodeURIComponent(ticket.ticket)}`, + ), + ), + Layer.provide(RpcSerialization.layerJson), + Layer.provide(NodeSocket.layerWebSocketConstructorWS), + ), + ); + const client = yield* RpcClient.make(WsRpcGroup).pipe(Effect.provideContext(protocol)); + const environmentId = EnvironmentId.make(descriptor.environmentId); + + const command = Effect.map(uuid, CommandId.make); + const projection = (threadId: ThreadId) => + client["orchestration.getThreadProjection"]({ threadId }); + + const create = Effect.fn("environment.create")(function* ( + title: string, + prompt?: string, + project: ProjectId = projectId, + ) { + const threadId = ThreadId.make(yield* uuid); + yield* client["orchestration.launchThread"]({ + commandId: yield* command, + threadId, + projectId: project, + title, + modelSelection: { instanceId: EVAL_INSTANCE, model: EVAL_MODEL }, + runtimeMode: "full-access", + interactionMode: "default", + workspaceStrategy: { type: "root" }, + ...(prompt === undefined ? {} : { initialMessage: { text: prompt, attachments: [] } }), + }); + return threadId; + }); + + /** Starts a thread whose first reply follows `prompt`'s `[[eval:…]]` script. */ + const launch = (title: string, prompt: string) => create(title, prompt); + + const until = Effect.fn("environment.until")(function* ( + description: string, + threadId: ThreadId, + pick: (value: OrchestrationV2ThreadProjection) => A | undefined, + ) { + for (let attempt = 0; attempt < 150; attempt++) { + const found = pick(yield* projection(threadId)); + if (found !== undefined) return found; + yield* Effect.sleep("200 millis"); + } + return yield* new EnvironmentError({ message: `Thread ${threadId} never had ${description}.` }); + }); + + const send = Effect.fn("environment.send")(function* (threadId: ThreadId, text: string) { + yield* client["orchestration.dispatchCommand"]({ + type: "message.dispatch", + commandId: yield* command, + threadId, + messageId: MessageId.make(yield* uuid), + text, + attachments: [], + dispatchMode: { type: "queue_after_active" }, + createdBy: "user", + creationSource: "server", + }); + }); + + const isSettled = (value: OrchestrationV2ThreadProjection) => + value.runs.length > 0 && + value.runs.every((run) => + ["completed", "failed", "interrupted", "cancelled"].includes(run.status), + ); + + /** Waits until every run of the thread has ended. */ + const settled = Effect.fn("environment.settled")(function* (threadId: ThreadId) { + for (let attempt = 0; attempt < 600; attempt++) { + const value = yield* projection(threadId); + if (isSettled(value)) return value; + yield* Effect.sleep("200 millis"); + } + return yield* new EnvironmentError({ message: `Thread ${threadId} never settled.` }); + }); + + /** A settled thread of `turns` request and reply pairs, for scroll and jank cases. */ + const longThread = Effect.fn("environment.longThread")(function* (title: string, turns: number) { + const threadId = yield* launch(title, "Summarize the module. [[eval:stream:400:0]]"); + yield* settled(threadId); + for (let turn = 1; turn < turns; turn++) { + const text = `Next part ${turn}. [[eval:stream:${turn % 3 === 0 ? 900 : 300}:0]]`; + yield* send(threadId, text); + // The message and its run land in one command, so once it shows, `settled` sees the run. + yield* until("its next message", threadId, (value) => + value.messages.some((message) => message.text === text) ? true : undefined, + ); + yield* settled(threadId); + } + return threadId; + }); + + return { + /** An empty thread in its own project; `lock` then disables `evalbot` there, so sends are rejected. */ + launchLockable: Effect.fn("environment.launchLockable")(function* (title: string) { + const root = NodePath.join(input.baseDir, `workspace/locked-${(yield* uuid).slice(0, 6)}`); + NodeFS.mkdirSync(root, { recursive: true }); + const project = yield* createProject("locked", root); + const threadId = yield* create(title, undefined, project); + const lock = client["server.updateSettings"]({ + patch: { + projectSettingsOverrides: { + [project]: { providerInstances: { [EVAL_INSTANCE]: { enabled: false } } }, + }, + }, + }); + return { threadId, lock }; + }), + /** Archives every thread but `keep`, so earlier samples' rows never crowd Home. */ + archiveAllExcept: Effect.fn("environment.archiveAllExcept")(function* (keep: ThreadId) { + for (const thread of (yield* shell()).threads) { + if (thread.id === keep) continue; + yield* client["orchestration.dispatchCommand"]({ + type: "thread.archive", + commandId: yield* command, + threadId: thread.id, + }); + } + }), + /** Stops every unfinished run, so one sample's held turns never leak into the next. */ + interruptAll: Effect.fn("environment.interruptAll")(function* () { + const threads = (yield* shell()).threads; + for (const thread of threads) + for (const run of (yield* projection(thread.id)).runs) { + if (["completed", "failed", "interrupted", "cancelled"].includes(run.status)) continue; + yield* client["orchestration.dispatchCommand"]({ + type: "run.interrupt", + commandId: yield* command, + threadId: thread.id, + runId: run.id, + }); + } + for (const thread of threads) + if ((yield* projection(thread.id)).runs.length) yield* settled(thread.id); + }), + waitForPendingInput: (threadId: ThreadId) => + until("a pending question", threadId, (value) => + value.runtimeRequests.find((request) => request.status === "pending"), + ), + waitForSubagents: (threadId: ThreadId) => + until("running subagents", threadId, (value) => + value.subagents.length >= 2 ? value.subagents : undefined, + ), + origin, + environmentId, + archive: (threadId: ThreadId) => + command.pipe( + Effect.flatMap((commandId) => + client["orchestration.dispatchCommand"]({ type: "thread.archive", commandId, threadId }), + ), + Effect.asVoid, + ), + unpin: (threadId: ThreadId) => + command.pipe( + Effect.flatMap((commandId) => + client["orchestration.dispatchCommand"]({ type: "thread.unpin", commandId, threadId }), + ), + Effect.asVoid, + ), + projection, + launch, + send, + settled, + longThread, + shell, + release: (key: string) => + Effect.sync(() => NodeFS.writeFileSync(NodePath.join(control, `release-${key}`), "")), + pairingUrl: () => + runCli([ + "auth", + "pairing", + "create", + "--base-dir", + input.baseDir, + "--base-url", + `http://${input.deviceHost}:${port}`, + "--ttl", + "15m", + "--label", + "mobile-behavior-eval", + ]).pipe( + Effect.flatMap((out) => { + const url = /Pair URL:\s*(\S+)/.exec(`${out.stdout}\n${out.stderr}`)?.[1]; + return url + ? Effect.succeed(url) + : Effect.fail(new EnvironmentError({ message: "No pairing URL in CLI output." })); + }), + ), + }; +}, Effect.provide(FetchHttpClient.layer)); + +export type EvalEnvironment = Effect.Success>; diff --git a/scripts/mobile-behavior/fake-fx.ts b/scripts/mobile-behavior/fake-fx.ts new file mode 100644 index 0000000000000..ebdbb5820509d --- /dev/null +++ b/scripts/mobile-behavior/fake-fx.ts @@ -0,0 +1,262 @@ +// @effect-diagnostics nodeBuiltinImport:off globalTimers:off preferSchemaOverJson:off globalConsole:off - a tiny stdio ACP agent spawned by the server; plain Node keeps it dependency-free. +/** + * A scripted stand-in for the `fx` CLI, registered as an Fx provider instance so the + * server streams deterministic replies with no model. A `[[eval:"); + const eventSink = yield* EventSink.EventSinkV2; + const now = yield* DateTime.now; + const run = (yield* orchestrator.getThreadRecords(threadId, ["runs"])).runs[0]; + const item = ( + id: string, + ordinal: number, + fields: + | { readonly type: "assistant_message"; readonly text: string } + | { readonly type: "user_message"; readonly text: string }, + target: { readonly threadId: ThreadId; readonly runId: RunId } = { threadId, runId }, + ) => ({ + id: EventId.make(`event:${id}`), + type: "turn-item.updated" as const, + threadId: target.threadId, + driver: ProviderDriverKind.make("codex"), + providerInstanceId: instanceId, + occurredAt: now, + payload: { + id: TurnItemId.make(id), + threadId: target.threadId, + runId: target.runId, + nodeId: null, + providerThreadId: null, + providerTurnId: null, + nativeItemRef: null, + parentItemId: null, + ordinal, + title: null, + status: "completed" as const, + startedAt: now, + completedAt: now, + updatedAt: now, + messageId: MessageId.make(id), + ...(fields.type === "assistant_message" + ? { ...fields, streaming: false } + : { + ...fields, + createdBy: "user" as const, + creationSource: "web" as const, + inputIntent: "turn_start" as const, + attachments: [], + }), + }, + }); + yield* eventSink.write({ + events: [ + item("item:agent-images", 1, { + type: "assistant_message", + text: `Done: ![Shot](<${shot}>) ![Other](file://${encodeURI(fileUrl)}) ![Page](${page})`, + }), + // A guest's sent message is a user message; it must not open files on the host. + item("item:guest-image", 2, { type: "user_message", text: `![Mine](${fromUser})` }), + { + id: EventId.make("event:run-done"), + type: "run.updated" as const, + threadId, + runId, + providerInstanceId: instanceId, + occurredAt: now, + payload: { ...run!, status: "completed" as const, completedAt: now }, + }, + ], + }); + // The guest watches a fork, which shows these replies as inherited history. + const forkId = ThreadId.make("thread:guest-share-fork"); + yield* orchestrator.dispatch({ + type: "thread.fork", + commandId: CommandId.make("fork"), + sourceThreadId: threadId, + targetThreadId: forkId, + sourcePoint: { type: "run", runId }, + createdBy: "user", + creationSource: "web", + }); + const { url } = yield* guestShares.create({ threadId: forkId }); + const { token: session } = yield* guestShares.redeem({ + credential: url.slice(url.indexOf("#") + 1), + name: "Sam", + }); + + // The guest's page opens, and its stream sends the replies the fork inherits. + const sent = yield* Queue.unbounded(); + yield* Stream.runForEach(yield* guestShares.stream(session), (event) => + Queue.offer(sent, event), + ).pipe(Effect.forkScoped); + const sentItem = Effect.fn(function* (id: string) { + for (;;) { + const event = yield* Queue.take(sent); + if ( + event.type === "thread" && + event.item.kind === "event" && + event.item.event.type === "turn-item.updated" && + event.item.event.payload.id === id + ) { + return; + } + } + }); + + // The agent shows another screenshot live, then enough rows to push the first reply out of + // the window, before the page asks for any image. + yield* orchestrator.dispatch({ + type: "message.dispatch", + commandId: CommandId.make("fork-message"), + threadId: forkId, + messageId: MessageId.make("message:fork"), + text: "One more", + attachments: [], + dispatchMode: { type: "defer_start" }, + createdBy: "user", + creationSource: "web", + }); + const forkRun = (yield* orchestrator.getThreadRecords(forkId, ["runs"])).runs.at(-1)!; + const inFork = { threadId: forkId, runId: forkRun.id }; + yield* eventSink.write({ + events: [ + item( + "item:later-image", + 1, + { type: "assistant_message", text: `![Later](${later})` }, + inFork, + ), + ...Array.from({ length: 600 }, (_, index) => + item( + `item:filler-${index}`, + index + 2, + { type: "assistant_message", text: "Working." }, + inFork, + ), + ), + ], + }); + yield* sentItem("item:later-image"); + + // The page still shows every reply it was sent, so each image loads. + for (const path of [shot, fileUrl, later]) { + assert.deepStrictEqual(yield* guestShares.media(session, path), { + path, + mimeType: "image/png", + }); + } + for (const path of [page, fromUser, unnamed, `${directory}/missing.png`]) { + assert.equal((yield* Effect.flip(guestShares.media(session, path))).reason, "unavailable"); + } + + // A reply edited to drop its image stops allowing it. + yield* eventSink.write({ + events: [ + { + ...item("item:later-image", 1, { type: "assistant_message", text: "Gone." }, inFork), + id: EventId.make("event:later-image-edited"), + }, + ], + }); + yield* sentItem("item:later-image"); + assert.equal((yield* Effect.flip(guestShares.media(session, later))).reason, "unavailable"); + }).pipe(Effect.scoped, Effect.provide(layerTest)), +); + +it.effect("keeps a connected guest's images while other shares come and go", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const directory = yield* fileSystem.makeTempDirectoryScoped(); + const shot = `${directory}/shot.png`; + yield* fileSystem.writeFile(shot, PNG); + const eventSink = yield* EventSink.EventSinkV2; + const now = yield* DateTime.now; + /** A shared thread whose agent reply shows the screenshot, and its guest's session. */ + const sharedWithImage = Effect.fn(function* (id: ThreadId) { + const { orchestrator, guestShares, credential } = yield* shareThreadFor(id); + yield* orchestrator.dispatch({ + type: "message.dispatch", + commandId: CommandId.make(`message:${id}`), + threadId: id, + messageId: MessageId.make(`message:${id}`), + text: "Show me", + attachments: [], + dispatchMode: { type: "defer_start" }, + createdBy: "user", + creationSource: "web", + }); + const runId = (yield* orchestrator.getThreadRecords(id, ["runs"])).runs[0]!.id; + yield* eventSink.write({ + events: [ + { + id: EventId.make(`event:reply:${id}`), + type: "turn-item.updated", + threadId: id, + driver: ProviderDriverKind.make("codex"), + providerInstanceId: instanceId, + occurredAt: now, + payload: { + id: TurnItemId.make(`item:reply:${id}`), + threadId: id, + runId, + nodeId: null, + providerThreadId: null, + providerTurnId: null, + nativeItemRef: null, + parentItemId: null, + ordinal: 1, + title: null, + status: "completed", + startedAt: now, + completedAt: now, + updatedAt: now, + type: "assistant_message", + messageId: MessageId.make(`item:reply:${id}`), + text: `![Shot](${shot})`, + streaming: false, + }, + }, + ], + }); + const { token } = yield* guestShares.redeem({ credential, name: "Sam" }); + return { guestShares, token }; + }); + + // This guest's page stays open while more guests than the record keeps open and close theirs. + const { guestShares, token } = yield* sharedWithImage(threadId); + const opened = yield* Deferred.make(); + yield* Stream.runForEach(yield* guestShares.stream(token), () => + Deferred.succeed(opened, undefined), + ).pipe(Effect.forkScoped); + yield* Deferred.await(opened); + const closedTokens: Array = []; + for (let index = 0; index < 65; index++) { + const other = yield* sharedWithImage(ThreadId.make(`thread:other-${index}`)); + const events = yield* guestShares.stream(other.token); + // The first is built but never read, as for a HEAD request; the rest open and close. + if (index > 0) yield* Stream.runDrain(Stream.take(events, 2)); + closedTokens.push(other.token); + } + assert.equal((yield* guestShares.media(token, shot)).mimeType, "image/png"); + // Records of guests with no open page were dropped to make room. + for (const closed of closedTokens.slice(0, 2)) { + assert.equal((yield* Effect.flip(guestShares.media(closed, shot))).reason, "unavailable"); + } + + // A new link on the thread starts empty: its guest gets nothing the last guest was sent. + const { url } = yield* guestShares.create({ threadId }); + const next = yield* guestShares.redeem({ + credential: url.slice(url.indexOf("#") + 1), + name: "Kim", + }); + assert.equal((yield* Effect.flip(guestShares.media(next.token, shot))).reason, "unavailable"); + yield* Stream.runDrain(Stream.take(yield* guestShares.stream(next.token), 2)); + assert.equal((yield* guestShares.media(next.token, shot)).mimeType, "image/png"); + }).pipe(Effect.scoped, Effect.provide(layerTest)), +); diff --git a/apps/server/src/guest/GuestShareService.ts b/apps/server/src/guest/GuestShareService.ts index 1f65217e45212..0346068bc546f 100644 --- a/apps/server/src/guest/GuestShareService.ts +++ b/apps/server/src/guest/GuestShareService.ts @@ -15,9 +15,11 @@ import { type OrchestrationV2CommandOutputFrame, type OrchestrationV2DomainEvent, type OrchestrationV2GetTurnItemResult, + type OrchestrationV2TurnItem, ThreadId, TurnItemId, } from "@t3tools/contracts"; +import { chatMarkdownImageSources } from "@t3tools/shared/markdownPipeline"; import { htmlRenderFromToolItem } from "@t3tools/shared/toolOutput"; import * as Context from "effect/Context"; import * as Crypto from "effect/Crypto"; @@ -63,7 +65,10 @@ import { guestShareView, guestThreadModel } from "./guestView.ts"; import { classifyGuestUploads, GUEST_UPLOAD_HEADER_BYTES, + guestHostImageType, type GuestUpload, + matchesImageSignature, + repliesShowImage, } from "./guestAttachments.ts"; const SIGNING_SECRET_NAME = "guest-share-signing-key"; @@ -71,6 +76,8 @@ const SHARE_ID_BYTES = 16; const INVITE_SIGNATURE_BYTES = 16; const SNAPSHOT_ROW_LIMIT = 600; const SNAPSHOT_USER_TURN_LIMIT = 20; +// Shares with no open guest stream whose sent image replies are still kept. +const SHARED_THREAD_REPLIES_LIMIT = 64; // How soon an open read notices that its link stopped working, such as Connect no longer serving // the host. Closing or replacing the link ends the main stream at once. const ACCESS_RECHECK_INTERVAL = Duration.seconds(30); @@ -190,9 +197,20 @@ export class GuestShareService extends Context.Service< { readonly path: string; readonly attachment: ChatAttachment }, GuestShareAccessError >; + /** An image on the host that an agent reply the guest sees shows, and its type. */ + readonly media: ( + session: string, + path: string, + ) => Effect.Effect<{ readonly path: string; readonly mimeType: string }, GuestShareAccessError>; } >()("t3/guest/GuestShareService") {} +/** A reply a guest's stream sent, with its image sources once a media request parsed them. */ +interface SentReply { + readonly text: string; + sources?: ReadonlyArray; +} + /** The thread record an event carries, for the share and the view. */ const threadOf = (event: OrchestrationV2DomainEvent): OrchestrationV2AppThread | null => { switch (event.type) { @@ -464,6 +482,40 @@ const make = Effect.gen(function* () { Effect.ignore, ); + // The image replies each share's guest stream has sent, in its opening snapshot or live, so an + // image loads while the guest's page can show its reply, inherited ones included. Keyed by + // share, so a new link on the thread starts empty. A share with an open stream keeps its + // record; past the limit, closed ones are dropped. + const sentImageReplies = new Map< + GuestShareId, + { openStreams: number; readonly replies: Map } + >(); + const sentRecord = (shareId: GuestShareId) => { + let record = sentImageReplies.get(shareId); + if (record === undefined) { + record = { openStreams: 0, replies: new Map() }; + sentImageReplies.set(shareId, record); + for (const [id, other] of sentImageReplies) { + if (sentImageReplies.size <= SHARED_THREAD_REPLIES_LIMIT) break; + if (other.openStreams === 0 && id !== shareId) sentImageReplies.delete(id); + } + } + return record; + }; + const rememberSent = (shareId: GuestShareId, items: Iterable) => { + for (const item of items) { + if (item.type !== "assistant_message") continue; + const key = `${item.threadId}\u0000${item.id}`; + // Only replies with image syntax can show an image. One edited to drop its images stops + // allowing them. + if (item.text.includes("![") || / => { if (item.kind !== "event") return []; if (endsShare(item.event)) return [ended]; + if (item.event.type === "turn-item.updated") rememberSent(share.id, [item.event.payload]); const updated = threadOf(item.event); const view = updated === null ? null : viewOf(updated); return view === null @@ -528,17 +581,15 @@ const make = Effect.gen(function* () { const untilExpiry = Effect.sleep( Math.max(0, Date.parse(share.expiresAt) - DateTime.toEpochMillis(yield* DateTime.now)), ); + const openingSnapshot = buildBoundedThreadStreamSnapshot({ + snapshotSequence: snapshot.snapshotSequence, + projection: snapshot.projection, + }); const opening: ReadonlyArray = [ { type: "view", view: initialView }, - { - type: "thread", - item: buildBoundedThreadStreamSnapshot({ - snapshotSequence: snapshot.snapshotSequence, - projection: snapshot.projection, - }), - }, + { type: "thread", item: openingSnapshot }, ]; - return Stream.fromIterable(opening).pipe( + const events = Stream.fromIterable(opening).pipe( Stream.concat( Stream.merge(live, Stream.fromEffect(Effect.as(untilExpiry, ended)), { haltStrategy: "either", @@ -557,6 +608,25 @@ const make = Effect.gen(function* () { ), ), ); + // The record is opened and counted when the stream runs, paired with its release, so a + // response whose body never runs (HEAD) touches nothing. + return Stream.unwrap( + Effect.acquireRelease( + Effect.sync(() => { + const record = sentRecord(share.id); + record.openStreams += 1; + rememberSent( + share.id, + openingSnapshot.projection.visibleTurnItems.map((row) => row.item), + ); + return record; + }), + (record) => + Effect.sync(() => { + record.openStreams -= 1; + }), + ).pipe(Effect.as(events)), + ); }); const unavailable = new GuestShareAccessError({ reason: "unavailable" }); @@ -630,6 +700,33 @@ const make = Effect.gen(function* () { return exists ? { path, attachment: found } : yield* unavailable; }); + const media = Effect.fn("GuestShareService.media")(function* (session: string, path: string) { + const { share } = yield* authorize(session, "session"); + const mimeType = guestHostImageType(path); + if (mimeType === null) return yield* unavailable; + const sent = sentImageReplies.get(share.id)?.replies.values() ?? []; + // Each sent reply is parsed once, on the first request that needs it. + if ( + !repliesShowImage( + path, + [...sent], + (reply) => (reply.sources ??= chatMarkdownImageSources(reply.text)), + ) + ) { + return yield* unavailable; + } + // The bytes must be the image the name says, as for a guest's own upload. + const header = yield* fileSystem.open(path, { flag: "r" }).pipe( + Effect.flatMap((file) => file.readAlloc(GUEST_UPLOAD_HEADER_BYTES)), + Effect.scoped, + Effect.mapError(() => unavailable), + ); + if (Option.isNone(header) || !matchesImageSignature(mimeType, header.value)) { + return yield* unavailable; + } + return { path, mimeType }; + }); + return GuestShareService.of({ create, redeem, @@ -640,6 +737,7 @@ const make = Effect.gen(function* () { item, commandOutput, attachment, + media, }); }); diff --git a/apps/server/src/guest/guestAttachments.test.ts b/apps/server/src/guest/guestAttachments.test.ts new file mode 100644 index 0000000000000..c65dd1364c2b0 --- /dev/null +++ b/apps/server/src/guest/guestAttachments.test.ts @@ -0,0 +1,33 @@ +import { describe, expect, it } from "vite-plus/test"; + +import { guestHostImageType, repliesShowImage } from "./guestAttachments.ts"; + +describe("guest host images", () => { + it.each([ + ["![a](/w/a.png)", "/w/a.png", "image/png"], + ["![a]()", "/w/my shot.jpg", "image/jpeg"], + ["![a](/w/shot(1).png)", "/w/shot(1).png", "image/png"], + ["![a](/w/shot\\_1.png) ![b](/w/a&b.png)", "/w/shot_1.png", "image/png"], + ["![a](/w/shot\\_1.png) ![b](/w/a&b.png)", "/w/a&b.png", "image/png"], + ["![a](file:///w/caf%c3%a9.png)", "/w/café.png", "image/png"], + ["![a](file:///w/%73hot.png) ![b](/w/shot2.png)", "/w/shot.png", "image/png"], + ["![a](file:///w/%73hot.png) ![b](/w/shot2.png)", "/w/shot2.png", "image/png"], + ["![a][r]\n\n[r]: /w/ref.png", "/w/ref.png", "image/png"], + ["![a](file:///w/url%20form.png)", "/w/url form.png", "image/png"], + ["![a](C:\\w\\win.png)", "C:\\w\\win.png", "image/png"], + ["![a][r]\n\n[r]: C:\\w\\ref.png", "C:/w/ref.png", "image/png"], + // Shown as a link, in code, or not at all: the guest sees no image, so none loads. + ["[a](/w/link.png)", "/w/link.png", null], + ["```\n![a](/w/code.png)\n```", "/w/code.png", null], + ["`![a](/w/span.png)`", "/w/span.png", null], + ["", "/w/comment.png", null], + ["![a](/w/a.png)", "/a.png", null], + ["![a](//host/s/x.png)", "//host/s/x.png", null], + ["![a](\\\\host\\s\\x.png)", "\\\\host\\s\\x.png", null], + ["![a](/w/a.svg)", "/w/a.svg", null], + ["![a](/w/a.constructor)", "/w/a.constructor", null], + ])("%j serves %j as %j", (text, path, type) => { + const served = guestHostImageType(path) !== null && repliesShowImage(path, [{ text }]); + expect(served ? guestHostImageType(path) : null).toBe(type); + }); +}); diff --git a/apps/server/src/guest/guestAttachments.ts b/apps/server/src/guest/guestAttachments.ts index ab7bd1a78b8e5..5118899620d64 100644 --- a/apps/server/src/guest/guestAttachments.ts +++ b/apps/server/src/guest/guestAttachments.ts @@ -3,6 +3,9 @@ import { GUEST_MESSAGE_MAX_ATTACHMENTS, isProviderSendTurnSupportedImageMimeType, } from "@t3tools/contracts"; +import { markdownDestinationHostPath } from "@t3tools/shared/markdownLinks"; +import { chatMarkdownImageSources } from "@t3tools/shared/markdownPipeline"; +import { isAbsolutePath, isNetworkPath } from "@t3tools/shared/path"; /** A file from a guest's message request, saved by the multipart parser to a temporary path. */ export interface GuestUpload { @@ -46,6 +49,44 @@ const IMAGE_SIGNATURES: Readonly boolean> startsWith(header, ascii("RIFF")) && startsWith(header, ascii("WEBP"), 8), }; +export const matchesImageSignature = (mimeType: string, header: Uint8Array) => + IMAGE_SIGNATURES[mimeType]?.(header) === true; + +const IMAGE_TYPE_BY_EXTENSION: ReadonlyMap = new Map([ + ["png", "image/png"], + ["jpg", "image/jpeg"], + ["jpeg", "image/jpeg"], + ["gif", "image/gif"], + ["webp", "image/webp"], +]); + +/** + * The type of a host image a guest may ask for by path, or null. A network path never qualifies: + * it would make the host reach out to another machine. + */ +export function guestHostImageType(path: string): string | null { + if (!isAbsolutePath(path) || isNetworkPath(path)) return null; + return IMAGE_TYPE_BY_EXTENSION.get(path.split(".").at(-1)!.toLowerCase()) ?? null; +} + +/** + * Whether an agent reply shows the image at `path`, so the guest already sees it. `imageSources` + * lets the caller reuse a reply's parse across requests. + */ +export function repliesShowImage( + path: string, + replies: ReadonlyArray, + imageSources: (reply: Reply) => ReadonlyArray = (reply) => + chatMarkdownImageSources(reply.text), +) { + // Parsing is the slow part; a reply with no image syntax shows no image. + return replies.some( + (reply) => + (reply.text.includes("![") || / markdownDestinationHostPath(source) === path), + ); +} + /** * Control characters and separators would break the name in headers and on disk, and direction * overrides would let `invoice\u202Efdp.exe` show as a PDF. @@ -76,11 +117,9 @@ export function classifyGuestUploads( upload.contentType.split(";", 1)[0]!.trim().toLowerCase() || "application/octet-stream"; if (upload.sizeBytes === 0) return { ok: false, reason: "unsupported_file" }; if (mimeType.startsWith("image/")) { - const signatureMatches = IMAGE_SIGNATURES[mimeType]; if ( !isProviderSendTurnSupportedImageMimeType(mimeType) || - signatureMatches === undefined || - !signatureMatches(upload.header) + !matchesImageSignature(mimeType, upload.header) ) { return { ok: false, reason: "unsupported_file" }; } diff --git a/apps/server/src/guest/http.ts b/apps/server/src/guest/http.ts index 7c8356119deb5..bf368ac884d92 100644 --- a/apps/server/src/guest/http.ts +++ b/apps/server/src/guest/http.ts @@ -2,6 +2,7 @@ import { GUEST_ATTACHMENT_ROUTE, GUEST_COMMAND_OUTPUT_ROUTE, GUEST_ITEM_ROUTE, + GUEST_MEDIA_ROUTE, GUEST_RENDER_ROUTE, GUEST_ROUTE_PREFIX, GUEST_SHARE_PARAM, @@ -34,6 +35,12 @@ import { GuestShareAccessError, GuestShareService } from "./GuestShareService.ts // Cloudflare closes idle streams after about 100 seconds. const HEARTBEAT_INTERVAL = Duration.seconds(20); const NO_STORE = { "cache-control": "no-store" }; +// A file never runs as a page on this origin, whatever its bytes. +const FILE_HEADERS = { + ...NO_STORE, + "x-content-type-options": "nosniff", + "content-security-policy": "default-src 'none'; sandbox", +}; const decodeRedeem = Schema.decodeUnknownEffect(GuestRedeemRequest); const decodeSubmit = Schema.decodeUnknownEffect(GuestSubmitRequest); @@ -315,9 +322,7 @@ const attachmentRoute = HttpRouter.add( Effect.flatMap(({ path, attachment }) => HttpServerResponse.file(path, { headers: { - ...NO_STORE, - "x-content-type-options": "nosniff", - "content-security-policy": "default-src 'none'; sandbox", + ...FILE_HEADERS, ...(attachment.type === "image" && isProviderSendTurnSupportedImageMimeType(attachment.mimeType) ? { "content-type": attachment.mimeType } @@ -334,6 +339,25 @@ const attachmentRoute = HttpRouter.add( }), ); +// Only an image an agent reply shows loads; anything else on the host is refused. +const mediaRoute = HttpRouter.add( + "GET", + GUEST_MEDIA_ROUTE, + Effect.gen(function* () { + const request = yield* HttpServerRequest.HttpServerRequest; + const path = new URLSearchParams(request.url.split("?", 2)[1] ?? "").get("path"); + if (!path) return yield* badRequest; + const guestShares = yield* GuestShareService; + return yield* guestShares.media(yield* sessionToken, path).pipe( + Effect.flatMap(({ path, mimeType }) => + HttpServerResponse.file(path, { headers: { ...FILE_HEADERS, "content-type": mimeType } }), + ), + Effect.catchTags({ GuestShareAccessError: errorResponse }), + Effect.orElseSucceed(() => HttpServerResponse.empty({ status: 500, headers: NO_STORE })), + ); + }), +); + export const layer = Layer.mergeAll( redeemRoute, sessionRoute, @@ -343,4 +367,5 @@ export const layer = Layer.mergeAll( itemRoute, commandOutputRoute, attachmentRoute, + mediaRoute, ); diff --git a/apps/web/src/components/ChatMarkdown.browser.test.tsx b/apps/web/src/components/ChatMarkdown.browser.test.tsx index d67803a6399ce..345bde84976c3 100644 --- a/apps/web/src/components/ChatMarkdown.browser.test.tsx +++ b/apps/web/src/components/ChatMarkdown.browser.test.tsx @@ -10,6 +10,7 @@ import "../index.css"; import { filesystemEnvironment } from "../state/filesystem"; import { useRightPanelStore } from "../rightPanelStore"; import { setMarkdownTaskChecked } from "./files/filePreviewMode"; +import { HostImageUrlContext } from "./chat/hostImageUrl"; const { workspaceSearch, filesystemMetadata } = vi.hoisted(() => ({ workspaceSearch: vi.fn(), @@ -122,6 +123,22 @@ it("shows unavailable audio when a workspace embed has no thread", async () => { expect(screen.container.querySelector("audio")).toBeNull(); }); +it("loads a host image through the page's own route when there is no environment", async () => { + setup(); + const pixel = + "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII="; + const hostImageUrl = vi.fn((_path: string) => pixel); + await using screen = await render( + + + , + ); + await expect.element(screen.getByAltText("Shot")).toHaveAttribute("src", pixel); + expect(new Set(hostImageUrl.mock.calls.map(([path]) => path))).toEqual( + new Set(["/work/my shot.png"]), + ); +}); + describe("ChatMarkdown favicon privacy", () => { it("suppresses private link images while preserving public links across updates", async () => { setup(); diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx index 2ae3e8d9f971a..32f15bcf542d7 100644 --- a/apps/web/src/components/ChatMarkdown.tsx +++ b/apps/web/src/components/ChatMarkdown.tsx @@ -65,6 +65,7 @@ import { import { mediaUrlReference } from "@t3tools/client-runtime/media-reference"; import { mediaKindFromPath, mediaMimeTypeFromExtension } from "@t3tools/shared/mediaPreview"; import { InlineAudioPreview } from "./files/InlineAudioPreview"; +import { HostImageUrlContext } from "./chat/hostImageUrl"; import * as Cause from "effect/Cause"; import * as Option from "effect/Option"; import * as Schema from "effect/Schema"; @@ -1483,6 +1484,24 @@ const CHAT_MARKDOWN_COMPONENTS = { /> ); } + const hostImageUrl = use(HostImageUrlContext); + if (imageSource._tag === "WorkspaceFile" && kind === "image" && hostImageUrl !== null) { + const hostSrc = hostImageUrl(imageSource.path); + return ( + + ); + } if (imageSource._tag === "WorkspaceFile" && threadRef) { return ( string) | null>(null); diff --git a/apps/web/src/components/guest/GuestPage.tsx b/apps/web/src/components/guest/GuestPage.tsx index 3ec7b2d542335..6d72741691f92 100644 --- a/apps/web/src/components/guest/GuestPage.tsx +++ b/apps/web/src/components/guest/GuestPage.tsx @@ -45,6 +45,7 @@ import type { ChatComposerHandle } from "../chat/ChatComposer"; import { ComposerControl, ComposerControlIcon } from "../chat/ComposerControl"; import type { ExpandedImagePreview } from "../chat/ExpandedImagePreview"; import { GuestMessageAttachments } from "../chat/GuestMessageAttachments"; +import { HostImageUrlContext } from "../chat/hostImageUrl"; import { PierreEntryIcon } from "../chat/PierreEntryIcon"; import { GuestMessageName, guestBubbleClassName } from "../chat/guestMessageStyle"; import { MessagesTimeline } from "../chat/MessagesTimeline"; @@ -60,6 +61,7 @@ import { applyGuestStreamEvent, formatGuestExpiry, guestAttachmentUrl, + guestMediaUrl, createGuestAttachmentUrls, guestFilesProblem, initialGuestStreamState, @@ -387,6 +389,7 @@ function GuestThread({ const openFile = useStableCallback((file: { readonly id: string }) => openInNewTab(guestAttachmentUrl(shareId, file.id)), ); + const hostImageUrl = useStableCallback((path: string) => guestMediaUrl(shareId, path)); const activityRun = deriveThreadActivityRun(projection); const runtime = deriveThreadStructureRuntime(projection); const runlessWorkStartedAt = deriveRunlessWorkStartedAt(projection); @@ -448,50 +451,52 @@ function GuestThread({
    - - {unplaced.map((message) => ( -
  • - -
  • - ))} - - ) - } - /> + + + {unplaced.map((message) => ( +
  • + +
  • + ))} + + ) + } + /> +
    {follow.showScrollToBottom ? ( `${GUEST_ATTACHMENT_ROUTE}?${GUEST_SHARE_PARAM}=${encodeURIComponent(shareId)}&id=${encodeURIComponent(attachmentId)}`; +/** Where the guest page loads an image file on the host that an agent message embeds. */ +export const guestMediaUrl = (shareId: string, path: string) => + `${GUEST_MEDIA_ROUTE}?${GUEST_SHARE_PARAM}=${encodeURIComponent(shareId)}&path=${encodeURIComponent(path)}`; + /** * Why these files cannot go with one message, or null. The server checks the same limits; this * only says so before the upload. diff --git a/docs/user/guest-links.md b/docs/user/guest-links.md index b97e400c65146..a8631a6c93b28 100644 --- a/docs/user/guest-links.md +++ b/docs/user/guest-links.md @@ -26,9 +26,9 @@ unlinking the machine from T3 Connect also ends it. You can send or dismiss gues ## What the guest sees The guest sees the thread's recent history and everything after, in the same timeline you see, as -it streams: messages and their files, the agent's replies and reasoning, plans, questions, and its -work, with command output when they expand a command. Their composer shows the thread's model, but -they cannot change it. They do not see diffs, and they cannot act on the thread: no forking, +it streams: messages and their files, the agent's replies with the images they show from your +machine, its reasoning, plans, questions, and its work, with command output when they expand a +command. Their composer shows the thread's model, but they cannot change it. They do not see diffs, and they cannot act on the thread: no forking, reverting, opening files on your machine, or answering the agent for you. Anything the agent read or printed can show, such as a value from a file. Share only threads you diff --git a/packages/client-runtime/src/markdownImages.ts b/packages/client-runtime/src/markdownImages.ts index 924220ec27ce4..94cba5c9f842b 100644 --- a/packages/client-runtime/src/markdownImages.ts +++ b/packages/client-runtime/src/markdownImages.ts @@ -1,16 +1,14 @@ import { isNetworkPath, isWindowsAbsolutePath, - stripSlashPrefixedWindowsDrive, workspaceRelativeFilePath, } from "@t3tools/shared/path"; import { + markdownDestinationHostPath, normalizeMarkdownLinkDestination, - safeDecodeURIComponent, splitMarkdownLinkSearchAndHash, } from "@t3tools/shared/markdownLinks"; -import { parseFileUrlHref } from "@t3tools/shared/fileLinks"; const DIRECT_IMAGE_SOURCE_PATTERN = /^(?:https?:|data:|blob:|\/\/)/i; const URI_SCHEME_PATTERN = /^[A-Za-z][A-Za-z0-9+.-]*:/; @@ -59,19 +57,9 @@ export function classifyMarkdownImageSource( return { _tag: "Direct", uri: source }; } - if (/^file:/i.test(source)) { - const target = parseFileUrlHref(source); - return target === null - ? { _tag: "Blocked" } - : hostFileSource( - stripSlashPrefixedWindowsDrive(safeDecodeURIComponent(target.path)), - workspaceRoot, - ); - } - - const path = stripSlashPrefixedWindowsDrive( - safeDecodeURIComponent(splitMarkdownLinkSearchAndHash(source).path), - ); + const path = markdownDestinationHostPath(source); + if (path === null) return { _tag: "Blocked" }; + if (/^file:/i.test(source)) return hostFileSource(path, workspaceRoot); if (path.length === 0) return { _tag: "Blocked" }; if (path.startsWith("/") || isWindowsAbsolutePath(path)) { return hostFileSource(path, workspaceRoot); diff --git a/packages/contracts/src/guestShare.ts b/packages/contracts/src/guestShare.ts index fa4b3a256f013..fd991fe1e4c3b 100644 --- a/packages/contracts/src/guestShare.ts +++ b/packages/contracts/src/guestShare.ts @@ -136,3 +136,8 @@ export const GUEST_COMMAND_OUTPUT_ROUTE = `${GUEST_ROUTE_PREFIX}/command-output` * Images load inline; other files download. */ export const GUEST_ATTACHMENT_ROUTE = `${GUEST_ROUTE_PREFIX}/attachment`; +/** + * An image file on the host that an agent message in the shared thread embeds: + * `?share=&path=`. + */ +export const GUEST_MEDIA_ROUTE = `${GUEST_ROUTE_PREFIX}/media`; diff --git a/packages/shared/src/markdownLinks.ts b/packages/shared/src/markdownLinks.ts index 0445fa42d8a0a..177e6fcbf5008 100644 --- a/packages/shared/src/markdownLinks.ts +++ b/packages/shared/src/markdownLinks.ts @@ -227,6 +227,22 @@ export function splitMarkdownLinkSearchAndHash(value: string): { }; } +/** + * The decoded path a markdown destination names on the host, before any workspace resolution, or + * null for a `file:` URL that does not parse. Clients and the guest media route both read it here. + */ +export function markdownDestinationHostPath(source: string): string | null { + if (/^file:/i.test(source)) { + const target = parseFileUrlHref(source); + return target === null + ? null + : stripSlashPrefixedWindowsDrive(safeDecodeURIComponent(target.path)); + } + return stripSlashPrefixedWindowsDrive( + safeDecodeURIComponent(splitMarkdownLinkSearchAndHash(source).path), + ); +} + export function isMarkdownFileLinkLabel(label: string, href: string): boolean { const destination = parseMarkdownFileLink(href); if (!destination) return false; diff --git a/packages/shared/src/markdownPipeline.ts b/packages/shared/src/markdownPipeline.ts index 275b93b562f6b..fe3d73ddac65c 100644 --- a/packages/shared/src/markdownPipeline.ts +++ b/packages/shared/src/markdownPipeline.ts @@ -1,6 +1,8 @@ import type { Paragraph, PhrasingContent, Root, RootContent } from "mdast"; import { remarkKeepWindowsPaths } from "./markdownWindowsPaths.ts"; -import type { PluggableList } from "unified"; +import { type PluggableList, unified } from "unified"; +import remarkParse from "remark-parse"; +import remarkRehype from "remark-rehype"; import rehypeRaw from "rehype-raw"; import rehypeSanitize, { defaultSchema } from "rehype-sanitize"; import remarkBreaks from "remark-breaks"; @@ -11,7 +13,7 @@ import { CODEX_ARTIFACT_TEMPLATE_HAST_PROPERTIES, remarkCodexDirectives, } from "./codexMarkdownDirectives.ts"; -import { isWindowsDrivePathHref } from "./markdownLinks.ts"; +import { isWindowsDrivePathHref, normalizeMarkdownLinkDestination } from "./markdownLinks.ts"; import { THREAD_LINK_PROTOCOL } from "./threadLinks.ts"; type MarkdownImageHastNode = { @@ -304,3 +306,38 @@ function remarkNormalizeLinksAndTagInlineCode() { export function shouldPreserveAssistantLineBreaks(text: string): boolean { return /^★ Insight(?:\s|─)/mu.test(text); } + +const imageSourceProcessor = unified() + .use(remarkParse) + .use(CHAT_MARKDOWN_REMARK_PLUGINS) + .use(remarkRehype, { allowDangerousHtml: true }) + .use(CHAT_MARKDOWN_REHYPE_PLUGINS); + +interface ImageSourceNode { + readonly type: string; + readonly tagName?: string; + readonly properties?: { readonly src?: unknown; readonly dataLocalSrc?: unknown }; + readonly children?: ReadonlyArray; +} + +/** + * The sources of the images chat markdown shows, read as ChatMarkdown's `img` renderer reads + * them. Code, comments, and plain links show no image, so they add none. + */ +export function chatMarkdownImageSources(markdown: string): Array { + const sources: Array = []; + const visit = (node: ImageSourceNode) => { + if (node.type === "element" && node.tagName === "img") { + const { src, dataLocalSrc } = node.properties ?? {}; + // The sanitizer drops a Windows drive path from `src`; the renderer reads it from here. + if (typeof dataLocalSrc === "string") { + sources.push(normalizeMarkdownLinkDestination(dataLocalSrc).replaceAll("\\", "/")); + } else if (typeof src === "string") { + sources.push(normalizeMarkdownLinkDestination(src)); + } + } + node.children?.forEach(visit); + }; + visit(imageSourceProcessor.runSync(imageSourceProcessor.parse(markdown)) as ImageSourceNode); + return sources; +} From a9418a1206f9f092d6d5769f6a62fb8da2ecc6ec Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Tue, 27 Oct 2026 23:53:18 -0700 Subject: [PATCH 35/46] ci(release): releases no longer reinstall Vite+ in every job (#26305) Co-authored-by: Claude Opus 5.5 --- .github/actions/setup-ci-deps/action.yml | 61 ++++++++++++--- .github/workflows/release-desktop.yml | 13 +--- .github/workflows/release.yml | 98 +++++++----------------- 3 files changed, 83 insertions(+), 89 deletions(-) diff --git a/.github/actions/setup-ci-deps/action.yml b/.github/actions/setup-ci-deps/action.yml index e0ef427bd1d36..afaef17e1c310 100644 --- a/.github/actions/setup-ci-deps/action.yml +++ b/.github/actions/setup-ci-deps/action.yml @@ -5,6 +5,14 @@ inputs: description: Read-only credential for the private maintainer task cache required: false default: "" + install: + description: Set to "false" to set up Vite+ only, with no pnpm store or install + required: false + default: "true" + install-args: + description: Arguments for `vp install`, such as filters. Empty installs the whole workspace. + required: false + default: "" runs: using: composite steps: @@ -19,10 +27,11 @@ runs: echo "node=$(jq -r '.engines.node' package.json)" >> "$GITHUB_OUTPUT" echo "pnpm=$(jq -r '.packageManager' package.json)" >> "$GITHUB_OUTPUT" + # Pinned because release-desktop.yml signs builds with this action in the job. - name: Cache Vite+ id: vp_cache if: steps.vp.outputs.version != '' - uses: actions/cache@v6 + uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6 with: path: ~/.vite-plus key: vite-plus-home-v2-${{ runner.os }}-${{ runner.arch }}-${{ steps.vp.outputs.version }}-node${{ steps.vp.outputs.node }}-${{ steps.vp.outputs.pnpm }} @@ -40,36 +49,66 @@ runs: - name: Setup Vite+ if: steps.vp_cache.outputs.cache-hit != 'true' - uses: voidzero-dev/setup-vp@v1 + uses: voidzero-dev/setup-vp@250f29ce396baf5e8f24498e17c0dfdebabc26eb # v1 with: node-version-file: package.json cache: false run-install: false - - name: Resolve pnpm store path + # A shared key lets whichever install saves first, often a small filtered + # one, become every job's store. Each set of install args gets its own. + - name: Resolve pnpm store id: pnpm_store + if: inputs.install == 'true' shell: bash + env: + INSTALL_ARGS: ${{ inputs.install-args }} run: | - store_path="$(vp pm cache dir)" - echo "path=$store_path" >> "$GITHUB_OUTPUT" + echo "path=$(vp pm cache dir)" >> "$GITHUB_OUTPUT" + if [[ -z "$INSTALL_ARGS" ]]; then + echo "namespace=full" >> "$GITHUB_OUTPUT" + else + echo "namespace=filtered-$(printf '%s' "$INSTALL_ARGS" | cksum | cut -d' ' -f1)" >> "$GITHUB_OUTPUT" + fi - # setup-vp's shared cache key ignores install filters. Keep full installs in a - # separate namespace, including fallback restores. + # Filtered installs prefer the current full store, which holds every package, + # over an older store of their own. # The pnpm store stays a cache tarball on the runner's own disk. A store on a # Blacksmith sticky disk mounts in under a second, but pnpm then copies every # package across filesystems instead of hard-linking it: install took 28-49s # against 9s plus an 11s restore. - - name: Cache full pnpm store - uses: actions/cache@v6 + - name: Restore pnpm store + id: pnpm_cache + if: inputs.install == 'true' + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6 with: path: ${{ steps.pnpm_store.outputs.path }} - key: pnpm-full-v1-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('pnpm-lock.yaml', 'pnpm-workspace.yaml', 'package.json') }} + key: pnpm-${{ steps.pnpm_store.outputs.namespace }}-v1-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('pnpm-lock.yaml', 'pnpm-workspace.yaml', 'package.json') }} restore-keys: | + pnpm-full-v1-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('pnpm-lock.yaml', 'pnpm-workspace.yaml', 'package.json') }} + pnpm-${{ steps.pnpm_store.outputs.namespace }}-v1-${{ runner.os }}-${{ runner.arch }}- pnpm-full-v1-${{ runner.os }}-${{ runner.arch }}- - name: Install dependencies + if: inputs.install == 'true' shell: bash - run: vp install + env: + INSTALL_ARGS: ${{ inputs.install-args }} + # Unquoted so each filter is its own argument. + run: vp install $INSTALL_ARGS + + # A filtered install that started from the current full store adds nothing + # worth a second copy of it. + - name: Save pnpm store + if: >- + inputs.install == 'true' && + steps.pnpm_cache.outputs.cache-hit != 'true' && + (steps.pnpm_store.outputs.namespace == 'full' || + steps.pnpm_cache.outputs.cache-matched-key != format('pnpm-full-v1-{0}-{1}-{2}', runner.os, runner.arch, hashFiles('pnpm-lock.yaml', 'pnpm-workspace.yaml', 'package.json'))) + uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6 + with: + path: ${{ steps.pnpm_store.outputs.path }} + key: ${{ steps.pnpm_cache.outputs.cache-primary-key }} - name: Enable shared task cache if: >- diff --git a/.github/workflows/release-desktop.yml b/.github/workflows/release-desktop.yml index bff1b125be44b..ca418467f6b9c 100644 --- a/.github/workflows/release-desktop.yml +++ b/.github/workflows/release-desktop.yml @@ -147,13 +147,6 @@ jobs: git sparse-checkout set --no-cone '/*' '!/.repos/' git -c checkout.workers=0 checkout --detach FETCH_HEAD - - name: Setup Vite+ - uses: voidzero-dev/setup-vp@250f29ce396baf5e8f24498e17c0dfdebabc26eb # v1 - with: - node-version-file: package.json - cache: true - run-install: false - # pnpm checks the lockfile and policy before reusing this result. A missing # artifact leaves the cache empty, so installation runs the checks again. - name: Download dependency verification @@ -163,10 +156,12 @@ jobs: name: release-dependency-verification path: ${{ runner.temp }}/pnpm-metadata - - name: Install desktop dependencies + - name: Setup dependencies + uses: ./.github/actions/setup-ci-deps + with: + install-args: --filter=@t3tools/desktop... --filter=t3... --filter=@t3tools/scripts... env: pnpm_config_cache_dir: ${{ runner.temp }}/pnpm-metadata - run: vp install --filter=@t3tools/desktop... --filter=t3... --filter=@t3tools/scripts... - name: Cache resource monitor id: resource_monitor_cache diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 33076d208f3a4..e8f300242f054 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -420,14 +420,10 @@ jobs: !/.repos/ sparse-checkout-cone-mode: false - - name: Setup Vite+ - uses: voidzero-dev/setup-vp@v1 + - name: Setup dependencies + uses: ./.github/actions/setup-ci-deps with: - node-version-file: package.json - cache: true - run-install: | - args: - - --filter=t3code-relay... + install-args: --filter=t3code-relay... # The deployed stack's outputs, read from Alchemy's state store without # planning or applying. Select the backend explicitly to avoid loading @@ -522,13 +518,6 @@ jobs: !/.repos/ sparse-checkout-cone-mode: false - - name: Setup Vite+ - uses: voidzero-dev/setup-vp@v1 - with: - node-version-file: package.json - cache: true - run-install: false - # pnpm checks the lockfile and policy before reusing this result. A missing # artifact leaves the cache empty, so installation runs the checks again. - name: Download dependency verification @@ -538,10 +527,12 @@ jobs: name: release-dependency-verification path: ${{ runner.temp }}/pnpm-metadata - - name: Install bundle dependencies + - name: Setup dependencies + uses: ./.github/actions/setup-ci-deps + with: + install-args: --filter=t3... --filter=@t3tools/web... --filter=@t3tools/desktop... --filter=@t3tools/scripts... env: pnpm_config_cache_dir: ${{ runner.temp }}/pnpm-metadata - run: vp install --filter=t3... --filter=@t3tools/web... --filter=@t3tools/desktop... --filter=@t3tools/scripts... - name: Download relay client tracing config uses: actions/download-artifact@v8 @@ -781,14 +772,10 @@ jobs: !/.repos/ sparse-checkout-cone-mode: false - - name: Setup Vite+ - uses: voidzero-dev/setup-vp@v1 + - name: Setup dependencies + uses: ./.github/actions/setup-ci-deps with: - node-version-file: package.json - cache: true - run-install: | - args: - - --filter=@t3tools/scripts... + install-args: --filter=@t3tools/scripts... - name: Download all desktop artifacts uses: actions/download-artifact@v8 @@ -977,15 +964,10 @@ jobs: !/.repos/ sparse-checkout-cone-mode: false - - name: Setup Vite+ - uses: voidzero-dev/setup-vp@v1 + - name: Setup dependencies + uses: ./.github/actions/setup-ci-deps with: - node-version-file: package.json - cache: true - run-install: | - args: - - --filter=t3... - - --filter=@t3tools/scripts... + install-args: --filter=t3... --filter=@t3tools/scripts... - name: Build the npm launcher run: node scripts/build-npm-launcher.ts --version "${{ needs.preflight.outputs.version }}" --output-dir npm-packages @@ -1041,15 +1023,10 @@ jobs: !/.repos/ sparse-checkout-cone-mode: false - - name: Setup Vite+ - uses: voidzero-dev/setup-vp@v1 + - name: Setup dependencies + uses: ./.github/actions/setup-ci-deps with: - node-version-file: package.json - cache: true - run-install: | - args: - - --filter=@t3tools/scripts... - - --filter=@t3tools/web... + install-args: --filter=@t3tools/scripts... --filter=@t3tools/web... - name: Download relay client tracing config uses: actions/download-artifact@v8 @@ -1145,11 +1122,9 @@ jobs: sparse-checkout-cone-mode: false - name: Setup Vite+ - uses: voidzero-dev/setup-vp@v1 + uses: ./.github/actions/setup-ci-deps with: - node-version-file: package.json - cache: false - run-install: false + install: false - name: Alias channel shell: bash @@ -1216,14 +1191,10 @@ jobs: !/.repos/ sparse-checkout-cone-mode: false - - name: Setup Vite+ - uses: voidzero-dev/setup-vp@v1 + - name: Setup dependencies + uses: ./.github/actions/setup-ci-deps with: - node-version-file: package.json - cache: true - run-install: | - args: - - --filter=@t3tools/marketing... + install-args: --filter=@t3tools/marketing... - id: deploy name: Build staged deployment @@ -1287,11 +1258,9 @@ jobs: sparse-checkout-cone-mode: false - name: Setup Vite+ - uses: voidzero-dev/setup-vp@v1 + uses: ./.github/actions/setup-ci-deps with: - node-version-file: package.json - cache: false - run-install: false + install: false - name: Promote marketing site shell: bash @@ -1346,15 +1315,10 @@ jobs: echo "name=${APP_SLUG}[bot]" >> "$GITHUB_OUTPUT" echo "email=${user_id}+${APP_SLUG}[bot]@users.noreply.github.com" >> "$GITHUB_OUTPUT" - - name: Setup Vite+ - uses: voidzero-dev/setup-vp@v1 + - name: Setup dependencies + uses: ./.github/actions/setup-ci-deps with: - node-version-file: package.json - cache: true - run-install: | - args: - - --filter=@t3tools/scripts... - - --filter=@t3tools/oxlint-plugin-t3code... + install-args: --filter=@t3tools/scripts... --filter=@t3tools/oxlint-plugin-t3code... - id: update_versions name: Update version strings @@ -1411,14 +1375,10 @@ jobs: !/.repos/ sparse-checkout-cone-mode: false - - name: Setup Vite+ - uses: voidzero-dev/setup-vp@v1 + - name: Setup dependencies + uses: ./.github/actions/setup-ci-deps with: - node-version-file: package.json - cache: true - run-install: | - args: - - --filter=@t3tools/scripts... + install-args: --filter=@t3tools/scripts... - name: Read published release notes if: needs.preflight.outputs.is_prerelease == 'true' From f6d3700ac3c506250260fcb143c5e795cbbaeb9f Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Tue, 27 Oct 2026 23:55:40 -0700 Subject: [PATCH 36/46] ci: server test shards survive a throttled task cache (#26332) Co-authored-by: Claude Opus 5.5 --- .github/workflows/ci.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ca294dd4a701a..e4c45728c2342 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -354,7 +354,9 @@ jobs: id-token: write name: Test Server ${{ matrix.shard }} runs-on: blacksmith-4vcpu-ubuntu-2404 - timeout-minutes: 10 + # Shards take 2-5 minutes. When the shared task cache throttles reads, each + # failed fetch stalls vp for 15-90s, which pushed shards past 10 minutes. + timeout-minutes: 20 strategy: fail-fast: false matrix: From 4e4d4ba3959cae17db7bba1915e6a25885844484 Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Wed, 28 Oct 2026 00:05:49 -0700 Subject: [PATCH 37/46] feat(web): boring sidebar groups threads into project folders (#21039) Co-authored-by: Claude Opus 5.5 --- .../settings/DesktopClientSettings.test.ts | 2 +- apps/web/package.json | 1 - apps/web/src/components/AppSidebarLayout.tsx | 10 +- apps/web/src/components/LegacySidebar.tsx | 3743 ----------------- apps/web/src/components/Sidebar.logic.test.ts | 98 + apps/web/src/components/Sidebar.logic.ts | 125 +- apps/web/src/components/Sidebar.tsx | 144 +- .../src/components/ThreadStatusIndicators.tsx | 4 +- .../components/settings/LabsSettingsPanel.tsx | 23 + .../components/settings/SettingsPanels.tsx | 19 +- .../settings/settingsSearch.test.ts | 2 +- .../src/components/settings/settingsSearch.ts | 12 +- .../sidebar/SidebarProjectFolderHeader.tsx | 115 + .../components/sidebar/SidebarThreadRow.tsx | 84 +- apps/web/src/components/ui/sidebar.tsx | 2 +- apps/web/src/connection/desktopLocal.ts | 4 - apps/web/src/hooks/useMediaQuery.ts | 4 - apps/web/src/hooks/useSettings.ts | 10 +- apps/web/src/shortcuts/chatRouteCommands.ts | 6 +- .../src/state/use-orchestration-command.ts | 3 +- docs/user/thread-sidebar.md | 9 + .../src/state/projectGrouping.ts | 2 +- packages/contracts/src/settings.ts | 18 +- pnpm-lock.yaml | 8 - 24 files changed, 549 insertions(+), 3899 deletions(-) delete mode 100644 apps/web/src/components/LegacySidebar.tsx create mode 100644 apps/web/src/components/sidebar/SidebarProjectFolderHeader.tsx diff --git a/apps/desktop/src/settings/DesktopClientSettings.test.ts b/apps/desktop/src/settings/DesktopClientSettings.test.ts index 9f76003c19c8d..ac4c6e61378e3 100644 --- a/apps/desktop/src/settings/DesktopClientSettings.test.ts +++ b/apps/desktop/src/settings/DesktopClientSettings.test.ts @@ -63,7 +63,7 @@ const clientSettings: ClientSettings = { }, sidebarProjectSortOrder: "manual", sidebarThreadSortOrder: "created_at", - sidebarThreadPreviewCount: 6, + boringSidebarEnabled: false, legacySidebarEnabled: false, sidebarWorkingShelfEnabled: false, loadBalancingEnabled: false, diff --git a/apps/web/package.json b/apps/web/package.json index de75da24e380b..729d44f8ea6cd 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -27,7 +27,6 @@ "@dnd-kit/utilities": "^3.2.2", "@effect/atom-react": "catalog:", "@fontsource-variable/inter": "^5.3.0", - "@formkit/auto-animate": "^0.9.0", "@legendapp/list": "catalog:", "@noble/hashes": "catalog:", "@pierre/diffs": "catalog:", diff --git a/apps/web/src/components/AppSidebarLayout.tsx b/apps/web/src/components/AppSidebarLayout.tsx index 20abcf1e57ade..6c8837e63f40a 100644 --- a/apps/web/src/components/AppSidebarLayout.tsx +++ b/apps/web/src/components/AppSidebarLayout.tsx @@ -31,14 +31,13 @@ import { selectActiveRightPanel, useRightPanelStore } from "../rightPanelStore"; import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore"; import { cn, isMacPlatform } from "../lib/utils"; import { primaryServerKeybindingsAtom } from "../state/server"; -import { useEnvironmentIdentificationMode, useLegacySidebarEnabled } from "../hooks/useSettings"; +import { useEnvironmentIdentificationMode } from "../hooks/useSettings"; import { PanelAnimationSuppressionProvider, usePanelAnimationSettings, usePanelNavigationSuppression, } from "../panelAnimations"; import { BackgroundThreadAnnouncer } from "./BackgroundThreadAnnouncer"; -import LegacyThreadSidebar from "./LegacySidebar"; import ThreadSidebar from "./Sidebar"; import { SidebarBrandWidthProbe } from "./sidebar/SidebarChrome"; import { isAppModalPage } from "./sidebar/mainAppLocation"; @@ -403,7 +402,6 @@ function SidebarControl() { export function AppSidebarLayout({ children }: { children: ReactNode }) { const sidebarArea = workspaceAreaProps("sidebar", 0, "Threads"); - const legacySidebarEnabled = useLegacySidebarEnabled(); const [sidebarRouteReveal] = useState(() => ({ observedKey: null, pendingKey: null, @@ -470,11 +468,7 @@ export function AppSidebarLayout({ children }: { children: ReactNode }) { }} style={windowControlsInsetStyle} > - {legacySidebarEnabled ? ( - - ) : ( - - )} + {children} diff --git a/apps/web/src/components/LegacySidebar.tsx b/apps/web/src/components/LegacySidebar.tsx deleted file mode 100644 index 27ea62b61511a..0000000000000 --- a/apps/web/src/components/LegacySidebar.tsx +++ /dev/null @@ -1,3743 +0,0 @@ -import { attachSidebarTree, SIDEBAR_TREE_PROPS } from "../lib/sidebarTree"; -import { useSupportsMultiplePullRequests } from "~/hooks/useSupportsMultiplePullRequests"; -import { resolveThreadCurrentPullRequestLink } from "@t3tools/client-runtime/thread-pull-requests"; -import { Spinner } from "~/components/ui/spinner"; -import { - ArchiveIcon, - ArrowUpDownIcon, - ChevronRightIcon, - FolderPlusIcon, - Globe2Icon, - SearchIcon, - SquarePenIcon, - TriangleAlertIcon, -} from "lucide-react"; -import { - ChangeRequestStatusIcon, - prStatusIndicator, - PrStatusTooltipContent, - ThreadTerminalStatusButton, - ThreadStatusLabel, - ThreadWorktreeIndicator, - useLinkedThreadPullRequest, -} from "./ThreadStatusIndicators"; -import { EnvironmentMachineIcon } from "./EnvironmentMachineIcon"; -import { ProjectFavicon } from "./ProjectFavicon"; -import { useAtomValue } from "@effect/atom-react"; -import { autoAnimate } from "@formkit/auto-animate"; -import React, { - createContext, - useEffect, - useEffectEvent, - memo, - useContext, - useRef, - useState, -} from "react"; -import { cn } from "~/lib/utils"; -import { useShallow } from "zustand/react/shallow"; -import { - DndContext, - type DragCancelEvent, - type CollisionDetection, - PointerSensor, - type DragStartEvent, - closestCorners, - pointerWithin, - useSensor, - useSensors, - type DragEndEvent, -} from "@dnd-kit/core"; -import { SortableContext, useSortable, verticalListSortingStrategy } from "@dnd-kit/sortable"; -import { restrictToFirstScrollableAncestor, restrictToVerticalAxis } from "@dnd-kit/modifiers"; -import { CSS } from "@dnd-kit/utilities"; -import { - AuthOrchestrationOperateScope, - AuthPreviewOperateScope, - type ContextMenuItem, - type EnvironmentId, - ProjectId, - type ScopedThreadRef, - type ResolvedKeybindingsConfig, - type SidebarProjectGroupingMode, - resolveEnvironmentMachineKind, - ThreadId, -} from "@t3tools/contracts"; -import { - parseScopedThreadKey, - scopedProjectKey, - scopedThreadKey, - scopeProjectRef, - scopeThreadRef, -} from "@t3tools/client-runtime/environment"; -import { safeErrorLogAttributes } from "@t3tools/client-runtime/errors"; -import { - isAtomCommandInterrupted, - settlePromise, - squashAtomCommandFailure, -} from "@t3tools/client-runtime/state/runtime"; -import { threadRuntimeCanArchive } from "@t3tools/client-runtime/state/models"; -import { useNavigate, useRouter } from "@tanstack/react-router"; -import { - MAX_SIDEBAR_THREAD_PREVIEW_COUNT, - MIN_SIDEBAR_THREAD_PREVIEW_COUNT, - type SidebarProjectSortOrder, - type SidebarThreadPreviewCount, - type SidebarThreadSortOrder, -} from "@t3tools/contracts/settings"; -import { isDesktopLocalConnectionTarget, isWslConnectionTarget } from "../connection/desktopLocal"; -import { useDesktopLocalBootstraps } from "../connection/useDesktopLocalBootstraps"; -import { isElectron } from "../env"; -import { useStableCallback } from "../hooks/useStableCallback"; -import { useTerminalFocus } from "../hooks/useTerminalFocus"; -import { useOpenPrLink } from "../lib/openPullRequestLink"; -import { releaseProjectDraftUploads } from "../lib/composerDraftUploads"; -import { isTerminalFocused } from "../lib/terminalFocus"; -import { isMacPlatform } from "../lib/utils"; -import { useSidebarPendingFileDropStore } from "../sidebarPendingFileDropStore"; -import { makeWorkspaceFileDropHandlers } from "./chat/workspaceFileDrop"; -import { - readThreadShell, - useProjects, - useDelegatedUnderParent, - useThreadShells, - useThreadShellsForProjectRefs, - waitForProjectRemoval, -} from "../state/entities"; -import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore"; -import { useThreadRunningTerminalIds } from "../state/terminalSessions"; -import { useThreadDiscoveredPorts } from "../portDiscoveryState"; -import { openDiscoveredPort } from "./preview/openDiscoveredPort"; -import { useAtomCommand } from "../state/use-atom-command"; -import { - ORCHESTRATION_OPERATE_DENIED_MESSAGE, - useOrchestrationCommand, -} from "../state/use-orchestration-command"; -import { previewEnvironment } from "../state/preview"; -import { readEnvironmentScope, useEnvironmentScope } from "../state/session"; -import { - legacyProjectCwdPreferenceKey, - resolveProjectExpanded, - useUiStateStore, -} from "../uiStateStore"; -import { - resolveShortcutCommand, - shortcutLabelForCommand, - shouldShowThreadJumpHintsForModifiers, - threadJumpCommandForIndex, - threadJumpIndexFromCommand, - threadTraversalDirectionFromCommand, -} from "../keybindings"; -import { isModelPickerOpen } from "../modelPickerVisibility"; -import { useShortcutModifierState } from "../shortcutModifierState"; -import { ensureLocalApi, readLocalApi } from "../localApi"; -import { useComposerDraftStore } from "../composerDraftStore"; -import { useNewThreadHandler } from "../hooks/useHandleNewThread"; -import { useDesktopUpdateState } from "../state/desktopUpdate"; - -import { useThreadActions } from "../hooks/useThreadActions"; -import { projectEnvironment } from "../state/projects"; -import { threadEnvironment, useEnvironmentThread } from "../state/threads"; -import { readUnloadedThreadCount } from "../state/olderSettledShelf"; -import { useEnvironment, useEnvironments, usePrimaryEnvironmentId } from "../state/environments"; -import { buildThreadRouteParams, resolveActiveThreadRouteRef } from "../threadRoutes"; -import { stackedThreadToast, toastManager } from "./ui/toast"; -import { copyThreadReference } from "../lib/copyThreadReference"; -import { threadChipCopyTarget } from "@t3tools/client-runtime/thread-reference"; -import { RelativeTime } from "./RelativeTime"; -import { Kbd } from "./ui/kbd"; -import { - getArm64IntelBuildWarningDescription, - getDesktopUpdateActionError, - getDesktopUpdateInstallConfirmationMessage, - isDesktopUpdateButtonDisabled, - resolveDesktopUpdateButtonAction, - shouldShowArm64IntelBuildWarning, - shouldToastDesktopUpdateActionResult, -} from "./desktopUpdate.logic"; -import { showDesktopUpdateDownloadedToast } from "./desktopUpdate.toast"; -import { Alert, AlertAction, AlertDescription, AlertTitle } from "./ui/alert"; -import { Button } from "./ui/button"; -import { - Dialog, - DialogDescription, - DialogFooter, - DialogHeader, - DialogPanel, - DialogPopup, - DialogTitle, -} from "./ui/dialog"; -import { Input } from "./ui/input"; -import { Menu, MenuGroup, MenuPopup, MenuRadioGroup, MenuRadioItem, MenuTrigger } from "./ui/menu"; -import { - NumberField, - NumberFieldDecrement, - NumberFieldGroup, - NumberFieldIncrement, - NumberFieldInput, -} from "./ui/number-field"; -import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "./ui/select"; -import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip"; -import { - SidebarContent, - SidebarGroup, - SidebarMenu, - SidebarMenuButton, - SidebarMenuItem, - SidebarMenuSub, - SidebarMenuSubButton, - SidebarMenuSubItem, - useSidebar, -} from "./ui/sidebar"; -import { - getThreadKeysToDeselectAfterDelete, - useThreadSelectionStore, -} from "../threadSelectionStore"; -import { isCommandPaletteOpen, openCommandPalette } from "../commandPaletteBus"; -import { - archiveSelectedThreadEntries, - buildMultiSelectThreadContextMenuItems, - deleteSelectedThreadEntries, - filterSidebarV2VisibleThreads, - getSidebarThreadIdsToPrewarm, - resolveAdjacentThreadId, - isContextMenuPointerDown, - isSidebarNestedLinkClick, - isTrailingDoubleClick, - resolveProjectStatusIndicator, - resolveThreadLastVisitedAt, - resolveThreadStatusPill, - orderItemsByPreferredIds, - shouldClearThreadSelectionOnMouseDown, - sortProjectsForSidebar, - useSidebarRowSubscriptionLease, - setFileDragActive, - useThreadJumpHintsVisible, - ThreadStatusPill, -} from "./Sidebar.logic"; -import { sortThreads } from "@t3tools/client-runtime/state/thread-sort"; -import { SidebarChromeFooter, SidebarChromeHeader } from "./sidebar/SidebarChrome"; -import { useCopyToClipboard } from "~/hooks/useCopyToClipboard"; -import { useIsMobile } from "~/hooks/useMediaQuery"; -import { CommandDialogTrigger } from "./ui/command"; -import { useClientSettings, updateClientSettings } from "~/hooks/useSettings"; -import { primaryServerKeybindingsAtom } from "../state/server"; -import { appAtomRegistry } from "../rpc/atomRegistry"; -import { canShareWithGuestAtom } from "./chat/guestShareAvailability"; -import { openGuestShareDialog } from "./chat/guestShareDialogState"; -import { - derivePhysicalProjectKey, - deriveProjectGroupingOverrideKey, - getProjectOrderKey, - selectProjectGroupingSettings, -} from "@t3tools/client-runtime/state/project-grouping"; -import type { SidebarThreadSummary } from "../types"; -import { - buildPhysicalToLogicalProjectKeyMap, - buildSidebarProjectSnapshots, - type SidebarProjectGroupMember, - type SidebarProjectSnapshot, -} from "../sidebarProjectGrouping"; -import { PullRequestGlyph } from "~/components/pullRequest/pullRequestIcons"; -import { useThreadRouteTarget } from "../hooks/useThreadRouteTarget"; -import { type SidebarRouteReveal, useActiveRowReveal } from "./sidebar/routeReveal"; -const SIDEBAR_SORT_LABELS: Record = { - updated_at: "Last user message", - created_at: "Created at", - manual: "Manual", -}; -const SIDEBAR_THREAD_SORT_LABELS: Record = { - updated_at: "Last user message", - created_at: "Created at", -}; -const SIDEBAR_LIST_ANIMATION_OPTIONS = { - duration: 180, - easing: "ease-out", -} as const; -const EMPTY_THREAD_JUMP_LABELS = new Map(); -const PROJECT_GROUPING_MODE_LABELS: Record = { - repository: "Group by repository", - repository_path: "Group by repository path", - separate: "Keep separate", -}; -const SIDEBAR_ICON_ACTION_BUTTON_CLASS = - "inline-flex h-6 min-w-6 cursor-pointer items-center justify-center rounded-md px-0.75 text-icon-muted hover:text-foreground focus-visible:outline-hidden focus-visible:ring-1 focus-visible:ring-inset focus-visible:ring-ring"; - -function SidebarThreadDetailPrewarmer({ threadRef }: { readonly threadRef: ScopedThreadRef }) { - useEnvironmentThread(threadRef.environmentId, threadRef.threadId); - return null; -} - -function clampSidebarThreadPreviewCount(value: number): SidebarThreadPreviewCount { - return Math.min( - MAX_SIDEBAR_THREAD_PREVIEW_COUNT, - Math.max(MIN_SIDEBAR_THREAD_PREVIEW_COUNT, value), - ) as SidebarThreadPreviewCount; -} - -function formatProjectMemberActionLabel( - member: SidebarProjectGroupMember, - groupedProjectCount: number, -): string { - if (groupedProjectCount <= 1) { - return member.title; - } - - return member.environmentLabel - ? `${member.environmentLabel}: ${member.workspaceRoot}` - : member.workspaceRoot; -} - -function projectExpansionPreferenceKeys(project: SidebarProjectSnapshot): string[] { - return [ - project.projectKey, - ...project.memberProjects.map((member) => member.physicalProjectKey), - ...project.memberProjects.map((member) => legacyProjectCwdPreferenceKey(member.workspaceRoot)), - ]; -} - -function projectGroupingModeDescription(mode: SidebarProjectGroupingMode): string { - switch (mode) { - case "repository": - return "Projects from the same repository share one sidebar row."; - case "repository_path": - return "Projects group only when both the repository and repo-relative path match."; - case "separate": - return "Every project path gets its own sidebar row."; - } -} - -function buildThreadJumpLabelMap(input: { - keybindings: ResolvedKeybindingsConfig; - platform: string; - terminalOpen: boolean; - threadJumpCommandByKey: ReadonlyMap< - string, - NonNullable> - >; -}): ReadonlyMap { - if (input.threadJumpCommandByKey.size === 0) { - return EMPTY_THREAD_JUMP_LABELS; - } - - const shortcutLabelOptions = { - platform: input.platform, - context: { - terminalFocus: false, - terminalOpen: input.terminalOpen, - }, - } as const; - const mapping = new Map(); - for (const [threadKey, command] of input.threadJumpCommandByKey) { - const label = shortcutLabelForCommand(input.keybindings, command, shortcutLabelOptions); - if (label) { - mapping.set(threadKey, label); - } - } - return mapping.size > 0 ? mapping : EMPTY_THREAD_JUMP_LABELS; -} - -interface SidebarThreadRowProps { - navigationParent?: string | undefined; - thread: SidebarThreadSummary; - orderedProjectThreadKeys: readonly string[]; - isActive: boolean; - openPullRequestsInRightPanel: boolean; - jumpLabel: string | null; - appSettingsConfirmThreadArchive: boolean; - renamingThreadKey: string | null; - renamingTitle: string; - setRenamingTitle: (title: string) => void; - startThreadRename: (threadKey: string, title: string) => void; - renamingInputRef: React.RefObject; - renamingCommittedRef: React.RefObject; - confirmingArchiveThreadKey: string | null; - setConfirmingArchiveThreadKey: React.Dispatch>; - confirmArchiveButtonRefs: React.RefObject>; - handleThreadClick: ( - event: React.MouseEvent, - threadRef: ScopedThreadRef, - orderedProjectThreadKeys: readonly string[], - ) => void; - navigateToThread: (threadRef: ScopedThreadRef) => Promise; - handleMultiSelectContextMenu: (position: { x: number; y: number }) => Promise; - handleThreadContextMenu: ( - threadRef: ScopedThreadRef, - position: { x: number; y: number }, - ) => Promise; - clearSelection: () => void; - commitRename: ( - threadRef: ScopedThreadRef, - newTitle: string, - originalTitle: string, - ) => Promise; - cancelRename: () => void; - attemptArchiveThread: (threadRef: ScopedThreadRef) => Promise; - openPrLink: ( - event: React.MouseEvent, - prUrl: string, - threadRef?: ScopedThreadRef, - ) => boolean; - onFileDropThreads: (threadRef: ScopedThreadRef, files: File[]) => void; -} - -function checkTaskPermission(environmentId: EnvironmentId): boolean { - if (readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) return true; - toastManager.add( - stackedThreadToast({ - type: "error", - title: "Action unavailable", - description: ORCHESTRATION_OPERATE_DENIED_MESSAGE, - }), - ); - return false; -} - -// The active row reports itself, so a route change scrolls to it once it mounts. -const RevealActiveRowContext = createContext<(row: HTMLElement | null, threadKey: string) => void>( - () => {}, -); - -const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowProps) { - const { - orderedProjectThreadKeys, - isActive, - openPullRequestsInRightPanel, - jumpLabel, - appSettingsConfirmThreadArchive, - renamingThreadKey, - renamingTitle, - setRenamingTitle, - startThreadRename, - renamingInputRef, - renamingCommittedRef, - confirmingArchiveThreadKey, - setConfirmingArchiveThreadKey, - confirmArchiveButtonRefs, - handleThreadClick, - navigateToThread, - handleMultiSelectContextMenu, - handleThreadContextMenu, - clearSelection, - commitRename, - cancelRename, - attemptArchiveThread, - openPrLink, - onFileDropThreads, - thread, - } = props; - const canOperateThread = useEnvironmentScope(thread.environmentId, AuthOrchestrationOperateScope); - const canOperatePreview = useEnvironmentScope(thread.environmentId, AuthPreviewOperateScope); - const threadKey = scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)); - const [isFileDragOver, setIsFileDragOver] = useState(false); - const revealActiveRow = useContext(RevealActiveRowContext); - const { leaseLiveStatus, rowRef } = useSidebarRowSubscriptionLease(isActive); - const localLastVisitedAt = useUiStateStore((state) => state.threadLastVisitedAtById[threadKey]); - const lastVisitedAt = resolveThreadLastVisitedAt(thread.lastVisitedAt, localLastVisitedAt); - const isSelected = useThreadSelectionStore((state) => state.selectedThreadKeys.has(threadKey)); - const runningTerminalIds = useThreadRunningTerminalIds({ - environmentId: thread.environmentId, - threadId: thread.id, - }); - const isMobile = useIsMobile(); - const discoveredPorts = useThreadDiscoveredPorts({ - environmentId: thread.environmentId, - threadId: thread.id, - }); - const openPreview = useAtomCommand(previewEnvironment.open, { - reportFailure: false, - }); - const environment = useEnvironment(thread.environmentId); - const primaryEnvironmentId = usePrimaryEnvironmentId(); - const isRemoteThread = thread.environmentId !== primaryEnvironmentId; - const remoteEnvLabel = environment?.label ?? null; - const remoteMachine = resolveEnvironmentMachineKind(environment?.serverConfig ?? null); - const isDesktopLocalThread = - environment !== null && isDesktopLocalConnectionTarget(environment.entry.target); - const threadEnvironmentLabel = isRemoteThread - ? (remoteEnvLabel ?? (isDesktopLocalThread ? "Local" : "Remote")) - : null; - const isThreadRunning = !threadRuntimeCanArchive(thread.runtime); - const threadStatus = resolveThreadStatusPill({ - thread: { - ...thread, - lastVisitedAt, - }, - }); - const linkedPullRequestStatus = useLinkedThreadPullRequest( - thread.environmentId, - thread.linkedPullRequest, - leaseLiveStatus, - thread.pullRequests, - thread.branchPullRequest, - ); - const pr = linkedPullRequestStatus?.pr ?? null; - const supportsMultiplePullRequests = useSupportsMultiplePullRequests(thread.environmentId); - // Hooks must precede this ref's plain uses for the compiler to cache it. - const threadRef = scopeThreadRef(thread.environmentId, thread.id); - const fileDropHandlers = makeWorkspaceFileDropHandlers({ - setDragActive: (active) => setFileDragActive(setIsFileDragOver, active), - addFiles: (files) => { - onFileDropThreads(threadRef, files); - }, - addFolders: () => {}, - }); - const isHighlighted = isActive || isSelected; - const handleOpenDiscoveredPort = (event: React.MouseEvent) => { - const port = discoveredPorts[0]; - if (!port || !canOperatePreview) return; - event.preventDefault(); - event.stopPropagation(); - navigateToThread(threadRef); - void (async () => { - const result = await openDiscoveredPort({ threadRef, port, openPreview }); - if (result._tag === "Success" || isAtomCommandInterrupted(result)) { - return; - } - const error = squashAtomCommandFailure(result); - toastManager.add( - stackedThreadToast({ - type: "error", - title: "Unable to open preview", - description: error instanceof Error ? error.message : "The preview could not be opened.", - }), - ); - })(); - }; - const currentLinkedPr = supportsMultiplePullRequests - ? resolveThreadCurrentPullRequestLink(thread.pullRequests) - : null; - const prStatus = prStatusIndicator(pr, linkedPullRequestStatus?.sourceControlProvider); - const isConfirmingArchive = - canOperateThread && confirmingArchiveThreadKey === threadKey && !isThreadRunning; - const threadMetaClassName = isConfirmingArchive - ? "pointer-events-none opacity-0" - : canOperateThread && !isThreadRunning - ? "pointer-events-none transition-opacity duration-150 max-sm:pr-6 group-hover/menu-sub-item:opacity-0 group-focus-within/menu-sub-item:opacity-0" - : "pointer-events-none"; - const clearConfirmingArchive = () => { - setConfirmingArchiveThreadKey((current) => (current === threadKey ? null : current)); - }; - const handleMouseLeave = () => { - clearConfirmingArchive(); - }; - const handleBlurCapture = (event: React.FocusEvent) => { - const currentTarget = event.currentTarget; - requestAnimationFrame(() => { - if (currentTarget.contains(document.activeElement)) { - return; - } - clearConfirmingArchive(); - }); - }; - const handleRowClick = (event: React.MouseEvent) => { - handleThreadClick(event, threadRef, orderedProjectThreadKeys); - }; - const handleRowDoubleClick = (event: React.MouseEvent) => { - if (!readEnvironmentScope(thread.environmentId, AuthOrchestrationOperateScope)) return; - if (renamingThreadKey === threadKey) return; - // Mobile navigation closes the sidebar on the first tap, so rename stays in the context menu. - if (isMobile) return; - if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return; - if ((event.target as HTMLElement).closest("button, a")) return; - event.preventDefault(); - startThreadRename(threadKey, thread.title); - }; - const handleRowKeyDown = (event: React.KeyboardEvent) => { - if (event.target !== event.currentTarget) return; - if ( - event.key === "F2" && - renamingThreadKey !== threadKey && - readEnvironmentScope(thread.environmentId, AuthOrchestrationOperateScope) - ) { - event.preventDefault(); - startThreadRename(threadKey, thread.title); - return; - } - if (event.key !== "Enter" && event.key !== " ") return; - event.preventDefault(); - navigateToThread(threadRef); - }; - const handleRowContextMenu = (event: React.MouseEvent) => { - event.preventDefault(); - const hasSelection = useThreadSelectionStore.getState().hasSelection(); - if (hasSelection && isSelected) { - void (async () => { - const result = await settlePromise(() => - handleMultiSelectContextMenu({ - x: event.clientX, - y: event.clientY, - }), - ); - if (result._tag === "Failure") { - const error = squashAtomCommandFailure(result); - toastManager.add( - stackedThreadToast({ - type: "error", - title: "Thread action failed", - description: error instanceof Error ? error.message : "An error occurred.", - }), - ); - } - })(); - return; - } - - if (hasSelection) { - clearSelection(); - } - void (async () => { - const result = await settlePromise(() => - handleThreadContextMenu(threadRef, { - x: event.clientX, - y: event.clientY, - }), - ); - if (result._tag === "Failure") { - const error = squashAtomCommandFailure(result); - toastManager.add( - stackedThreadToast({ - type: "error", - title: "Thread action failed", - description: error instanceof Error ? error.message : "An error occurred.", - }), - ); - } - })(); - }; - const handlePrClick = (event: React.MouseEvent) => { - const url = prStatus?.url ?? currentLinkedPr?.url; - if (!url) return; - const openedInRightPanel = openPrLink( - event, - url, - openPullRequestsInRightPanel ? threadRef : undefined, - ); - if (openedInRightPanel && openPullRequestsInRightPanel && !isActive) { - navigateToThread(threadRef); - } - }; - const handleRenameInputRef = (element: HTMLInputElement | null) => { - if (element && renamingInputRef.current !== element) { - renamingInputRef.current = element; - element.focus(); - element.select(); - } - }; - const handleRenameInputChange = (event: React.ChangeEvent) => { - setRenamingTitle(event.target.value); - }; - const handleRenameInputKeyDown = (event: React.KeyboardEvent) => { - event.stopPropagation(); - if (event.key === "Enter") { - event.preventDefault(); - renamingCommittedRef.current = true; - void commitRename(threadRef, renamingTitle, thread.title); - } else if (event.key === "Escape") { - event.preventDefault(); - renamingCommittedRef.current = true; - cancelRename(); - } - }; - const handleRenameInputBlur = () => { - if (!renamingCommittedRef.current) { - void commitRename(threadRef, renamingTitle, thread.title); - } - }; - // Stop dblclick propagation so word selection cannot restart rename and discard edits. - const handleRenameInputClick = (event: React.MouseEvent) => { - event.stopPropagation(); - }; - const handleConfirmArchiveRef = (element: HTMLButtonElement | null) => { - if (element) { - confirmArchiveButtonRefs.current.set(threadKey, element); - } else { - confirmArchiveButtonRefs.current.delete(threadKey); - } - }; - const stopPropagationOnPointerDown = (event: React.PointerEvent) => { - event.stopPropagation(); - }; - const handleConfirmArchiveClick = (event: React.MouseEvent) => { - event.preventDefault(); - event.stopPropagation(); - clearConfirmingArchive(); - void attemptArchiveThread(threadRef); - }; - const handleStartArchiveConfirmation = (event: React.MouseEvent) => { - event.preventDefault(); - event.stopPropagation(); - if (!checkTaskPermission(thread.environmentId)) return; - setConfirmingArchiveThreadKey(threadKey); - requestAnimationFrame(() => { - confirmArchiveButtonRefs.current.get(threadKey)?.focus(); - }); - }; - const handleArchiveImmediateClick = (event: React.MouseEvent) => { - event.preventDefault(); - event.stopPropagation(); - void attemptArchiveThread(threadRef); - }; - - return ( - -
    revealActiveRow(node, threadKey) : undefined} - role="treeitem" - tabIndex={-1} - data-sidebar-row={threadKey} - data-sidebar-parent={props.navigationParent} - aria-current={isActive ? "page" : undefined} - data-active={isActive} - data-slot="sidebar-menu-sub-button" - data-sidebar="menu-sub-button" - data-size="sm" - data-testid={`thread-row-${thread.id}`} - className={cn( - "relative isolate flex h-8 w-full min-w-0 cursor-pointer select-none items-center gap-2 overflow-hidden rounded-md px-2 text-left text-xs outline-hidden focus-visible:ring-1 focus-visible:ring-inset focus-visible:ring-ring group-data-[collapsible=icon]:hidden [&>span:last-child]:truncate [&>svg:not([class*='size-'])]:size-4 [&>svg]:shrink-0 [&>svg]:text-sidebar-muted-foreground", - isActive - ? "bg-sidebar-row-active font-medium text-sidebar-foreground hover:bg-sidebar-row-active" - : isSelected - ? "bg-sidebar-row-selected text-sidebar-foreground hover:bg-sidebar-row-active" - : "text-sidebar-muted-foreground/80 hover:bg-sidebar-row-hover hover:text-sidebar-foreground", - isFileDragOver && "ring-1 ring-inset ring-primary/70", - )} - onClick={handleRowClick} - onDoubleClick={handleRowDoubleClick} - onKeyDown={handleRowKeyDown} - onContextMenu={handleRowContextMenu} - > -
    - {prStatus && pr && ( - - event.stopPropagation()} - onClick={handlePrClick} - > - - - } - /> - - - - - )} - {!pr && currentLinkedPr ? ( - event.stopPropagation()} - onClick={handlePrClick} - className="text-muted-foreground" - aria-label={`PR #${currentLinkedPr.number}, status pending`} - > - - - ) : null} - {threadStatus && } - {canOperateThread && renamingThreadKey === threadKey ? ( - - ) : ( - - - {thread.title} - - } - /> - {thread.title} - - )} -
    -
    - {canOperatePreview && discoveredPorts.length > 0 && ( - - - } - > - - - - Open localhost:{discoveredPorts[0]?.port} - {discoveredPorts.length > 1 ? ` (+${discoveredPorts.length - 1})` : ""} - - - )} - - -
    - {isConfirmingArchive ? ( - - ) : canOperateThread && !isThreadRunning ? ( - appSettingsConfirmThreadArchive ? ( -
    - -
    - ) : ( - - - -
    - } - /> - Archive - - ) - ) : null} - - - {isRemoteThread && !isDesktopLocalThread && ( - - - } - > - - - {threadEnvironmentLabel} - - )} - {jumpLabel ? ( - - - } - > - {jumpLabel} - - {jumpLabel} - - ) : ( - - - - )} - - -
    -
    - - - ); -}); - -interface SidebarProjectThreadListProps { - projectKey: string; - projectExpanded: boolean; - hasOverflowingThreads: boolean; - hiddenThreadStatus: ThreadStatusPill | null; - orderedProjectThreadKeys: readonly string[]; - renderedThreads: readonly SidebarThreadSummary[]; - showEmptyThreadState: boolean; - shouldShowThreadPanel: boolean; - isThreadListExpanded: boolean; - activeRouteThreadKey: string | null; - openPullRequestsInRightPanel: boolean; - threadJumpLabelByKey: ReadonlyMap; - appSettingsConfirmThreadArchive: boolean; - renamingThreadKey: string | null; - renamingTitle: string; - setRenamingTitle: (title: string) => void; - startThreadRename: (threadKey: string, title: string) => void; - renamingInputRef: React.RefObject; - renamingCommittedRef: React.RefObject; - confirmingArchiveThreadKey: string | null; - setConfirmingArchiveThreadKey: React.Dispatch>; - confirmArchiveButtonRefs: React.RefObject>; - attachThreadListAutoAnimateRef: (node: HTMLElement | null) => void; - handleThreadClick: ( - event: React.MouseEvent, - threadRef: ScopedThreadRef, - orderedProjectThreadKeys: readonly string[], - ) => void; - navigateToThread: (threadRef: ScopedThreadRef) => Promise; - onFileDropThreads: (threadRef: ScopedThreadRef, files: File[]) => void; - handleMultiSelectContextMenu: (position: { x: number; y: number }) => Promise; - handleThreadContextMenu: ( - threadRef: ScopedThreadRef, - position: { x: number; y: number }, - ) => Promise; - clearSelection: () => void; - commitRename: ( - threadRef: ScopedThreadRef, - newTitle: string, - originalTitle: string, - ) => Promise; - cancelRename: () => void; - attemptArchiveThread: (threadRef: ScopedThreadRef) => Promise; - openPrLink: ( - event: React.MouseEvent, - prUrl: string, - threadRef?: ScopedThreadRef, - ) => boolean; - expandThreadListForProject: (projectKey: string) => void; - collapseThreadListForProject: (projectKey: string) => void; -} - -const SidebarProjectThreadList = memo(function SidebarProjectThreadList( - props: SidebarProjectThreadListProps, -) { - const { - projectKey, - projectExpanded, - hasOverflowingThreads, - hiddenThreadStatus, - orderedProjectThreadKeys, - renderedThreads, - showEmptyThreadState, - shouldShowThreadPanel, - isThreadListExpanded, - activeRouteThreadKey, - openPullRequestsInRightPanel, - threadJumpLabelByKey, - appSettingsConfirmThreadArchive, - renamingThreadKey, - renamingTitle, - setRenamingTitle, - startThreadRename, - renamingInputRef, - renamingCommittedRef, - confirmingArchiveThreadKey, - setConfirmingArchiveThreadKey, - confirmArchiveButtonRefs, - attachThreadListAutoAnimateRef, - handleThreadClick, - navigateToThread, - onFileDropThreads, - handleMultiSelectContextMenu, - handleThreadContextMenu, - clearSelection, - commitRename, - cancelRename, - attemptArchiveThread, - openPrLink, - expandThreadListForProject, - collapseThreadListForProject, - } = props; - const showMoreButtonRender = - - } - /> - - {newThreadShortcutLabel ? `New thread (${newThreadShortcutLabel})` : "New thread"} - - - - - - - { - if (!open) { - closeProjectRenameDialog(); - } - }} - > - - - Rename project - - {projectRenameTarget - ? `Update the title for ${projectRenameTarget.workspaceRoot}.` - : "Update the project title."} - - - -
    - Project title - setProjectRenameTitle(event.target.value)} - onKeyDown={(event) => { - if (event.key === "Enter") { - event.preventDefault(); - void submitProjectRename(); - } - }} - /> -
    - {projectRenameTarget?.environmentLabel ? ( -

    - Environment: {projectRenameTarget.environmentLabel} -

    - ) : null} - {!canRenameProject ? ( -

    - This connection cannot change projects. -

    - ) : null} -
    - - - - -
    -
    - - { - if (!open) { - closeProjectGroupingDialog(); - } - }} - > - - - Project grouping - - {projectGroupingTarget - ? `Choose how ${projectGroupingTarget.workspaceRoot} should be grouped in the sidebar.` - : "Choose how this project should be grouped in the sidebar."} - - - -
    - Grouping rule - -
    -

    - {projectGroupingSelection === "inherit" - ? projectGroupingModeDescription(projectGroupingSettings.sidebarProjectGroupingMode) - : projectGroupingModeDescription(projectGroupingSelection)} -

    -
    - - - - -
    -
    - - ); -}); - -const SidebarProjectListRow = memo(function SidebarProjectListRow(props: SidebarProjectItemProps) { - return ( - - - - ); -}); - -function LocalSecondaryStatus() { - const { environments } = useEnvironments(); - // Poll desktop-local backends because the bridge has no change event. - const secondaries = useDesktopLocalBootstraps(); - - const localEnvByUrl = (() => { - const map = new Map(); - for (const environment of environments) { - if ( - isDesktopLocalConnectionTarget(environment.entry.target) && - environment.displayUrl !== null - ) { - map.set(environment.displayUrl, { - phase: environment.connection.phase, - error: environment.connection.error, - }); - } - } - return map; - })(); - - const connecting: string[] = []; - const failed: Array<{ label: string; error: string | null }> = []; - for (const bootstrap of secondaries) { - const env = - bootstrap.httpBaseUrl !== null ? localEnvByUrl.get(bootstrap.httpBaseUrl) : undefined; - if (env?.phase === "connected") { - continue; - } - if (env?.phase === "error") { - failed.push({ label: bootstrap.label, error: env.error }); - continue; - } - connecting.push(bootstrap.label); - } - - if (connecting.length === 0 && failed.length === 0) { - return null; - } - - return ( - - {connecting.length > 0 ? ( - - - Connecting {connecting.join(", ")} - - ) : null} - {failed.length > 0 ? ( - - - Couldn't connect {failed.map((entry) => entry.label).join(", ")} - - {failed - .map((entry) => entry.error) - .filter(Boolean) - .join("; ") || "The backend didn't respond."} - - - ) : null} - - ); -} - -type SortableProjectHandleProps = Pick< - ReturnType, - "attributes" | "listeners" | "setActivatorNodeRef" ->; - -function ProjectSortMenu({ - projectSortOrder, - threadSortOrder, - threadPreviewCount, - onProjectSortOrderChange, - onThreadSortOrderChange, - onThreadPreviewCountChange, -}: { - projectSortOrder: SidebarProjectSortOrder; - threadSortOrder: SidebarThreadSortOrder; - threadPreviewCount: SidebarThreadPreviewCount; - onProjectSortOrderChange: (sortOrder: SidebarProjectSortOrder) => void; - onThreadSortOrderChange: (sortOrder: SidebarThreadSortOrder) => void; - onThreadPreviewCountChange: (count: SidebarThreadPreviewCount) => void; -}) { - const handleThreadPreviewCountChange = (nextValue: number | null) => { - if (nextValue === null) { - return; - } - - const clampedValue = clampSidebarThreadPreviewCount(nextValue); - if (clampedValue !== threadPreviewCount) { - onThreadPreviewCountChange(clampedValue); - } - }; - - return ( - - - } - /> - } - > - - - Sidebar options - - - -
    - Sort projects -
    - { - onProjectSortOrderChange(value as SidebarProjectSortOrder); - }} - > - {(Object.entries(SIDEBAR_SORT_LABELS) as Array<[SidebarProjectSortOrder, string]>).map( - ([value, label]) => ( - - {label} - - ), - )} - -
    - -
    - Sort threads -
    - { - onThreadSortOrderChange(value as SidebarThreadSortOrder); - }} - > - {( - Object.entries(SIDEBAR_THREAD_SORT_LABELS) as Array<[SidebarThreadSortOrder, string]> - ).map(([value, label]) => ( - - {label} - - ))} - -
    - -
    - Visible threads -
    -
    - - - - { - event.stopPropagation(); - }} - /> - - - -
    -
    -
    -
    - ); -} - -function SortableProjectItem({ - projectId, - disabled = false, - children, -}: { - projectId: string; - disabled?: boolean; - children: (handleProps: SortableProjectHandleProps) => React.ReactNode; -}) { - const { - attributes, - listeners, - setActivatorNodeRef, - setNodeRef, - transform, - transition, - isDragging, - isOver, - } = useSortable({ id: projectId, disabled }); - return ( -
  • - {children({ attributes, listeners, setActivatorNodeRef })} -
  • - ); -} - -interface SidebarProjectsContentProps { - showArm64IntelBuildWarning: boolean; - arm64IntelBuildWarningDescription: string | null; - desktopUpdateButtonAction: "download" | "install" | "none"; - desktopUpdateButtonDisabled: boolean; - desktopUpdateActionPending: boolean; - handleDesktopUpdateButtonClick: () => void; - projectSortOrder: SidebarProjectSortOrder; - threadSortOrder: SidebarThreadSortOrder; - threadPreviewCount: SidebarThreadPreviewCount; - openAddProject: () => void; - isManualProjectSorting: boolean; - projectDnDSensors: ReturnType; - projectCollisionDetection: CollisionDetection; - handleProjectDragStart: (event: DragStartEvent) => void; - handleProjectDragEnd: (event: DragEndEvent) => void; - handleProjectDragCancel: (event: DragCancelEvent) => void; - handleNewThread: ReturnType; - archiveThread: ReturnType["archiveThread"]; - deleteThread: ReturnType["deleteThread"]; - markThreadUnread: ReturnType["markThreadUnread"]; - sortedProjects: readonly SidebarProjectSnapshot[]; - sortedProjectKeys: string[]; - expandedThreadListsByProject: ReadonlySet; - activeRouteProjectKey: string | null; - routeThreadKey: string | null; - openPullRequestsInRightPanel: boolean; - newThreadShortcutLabel: string | null; - commandPaletteShortcutLabel: string | null; - threadJumpLabelByKey: ReadonlyMap; - attachThreadListAutoAnimateRef: (node: HTMLElement | null) => void; - expandThreadListForProject: (projectKey: string) => void; - collapseThreadListForProject: (projectKey: string) => void; - dragInProgressRef: React.RefObject; - suppressProjectClickAfterDragRef: React.RefObject; - suppressProjectClickForContextMenuRef: React.RefObject; - attachProjectListAutoAnimateRef: (node: HTMLElement | null) => void; - projectsLength: number; -} - -const SidebarProjectsContent = memo(function SidebarProjectsContent( - props: SidebarProjectsContentProps, -) { - const { - showArm64IntelBuildWarning, - arm64IntelBuildWarningDescription, - desktopUpdateButtonAction, - desktopUpdateButtonDisabled, - desktopUpdateActionPending, - handleDesktopUpdateButtonClick, - projectSortOrder, - threadSortOrder, - threadPreviewCount, - openAddProject, - isManualProjectSorting, - projectDnDSensors, - projectCollisionDetection, - handleProjectDragStart, - handleProjectDragEnd, - handleProjectDragCancel, - handleNewThread, - archiveThread, - deleteThread, - markThreadUnread, - sortedProjects, - sortedProjectKeys, - expandedThreadListsByProject, - activeRouteProjectKey, - routeThreadKey, - openPullRequestsInRightPanel, - newThreadShortcutLabel, - commandPaletteShortcutLabel, - threadJumpLabelByKey, - attachThreadListAutoAnimateRef, - expandThreadListForProject, - collapseThreadListForProject, - dragInProgressRef, - suppressProjectClickAfterDragRef, - suppressProjectClickForContextMenuRef, - attachProjectListAutoAnimateRef, - projectsLength, - } = props; - - const attachTreeRef = (node: HTMLUListElement | null) => { - attachProjectListAutoAnimateRef(node); - const dispose = attachSidebarTree(node); - return () => { - dispose?.(); - attachProjectListAutoAnimateRef(null); - }; - }; - const handleProjectSortOrderChange = (sortOrder: SidebarProjectSortOrder) => { - updateClientSettings({ sidebarProjectSortOrder: sortOrder }); - }; - const handleThreadSortOrderChange = (sortOrder: SidebarThreadSortOrder) => { - updateClientSettings({ sidebarThreadSortOrder: sortOrder }); - }; - const handleThreadPreviewCountChange = (count: SidebarThreadPreviewCount) => { - updateClientSettings({ sidebarThreadPreviewCount: count }); - }; - - return ( - - - - } - > - - Search - {commandPaletteShortcutLabel ? {commandPaletteShortcutLabel} : null} - - - - - } - > - {showArm64IntelBuildWarning && arm64IntelBuildWarningDescription ? ( - - - - Intel build on Apple Silicon - {arm64IntelBuildWarningDescription} - {desktopUpdateButtonAction !== "none" ? ( - - - - ) : null} - - - ) : null} - - -
    - Projects -
    - - - - } - > - - - Add project - -
    -
    - - {isManualProjectSorting ? ( - - - - {sortedProjects.map((project) => ( - - {(dragHandleProps) => ( - - )} - - ))} - - - - ) : ( - - {sortedProjects.map((project) => ( - - ))} - - )} - - {projectsLength === 0 && ( -
    No projects yet
    - )} -
    -
    - ); -}); - -const PROJECT_POINTER_SENSOR_OPTIONS = { activationConstraint: { distance: 6 } }; -const PROJECT_DND_MODIFIERS = [restrictToVerticalAxis, restrictToFirstScrollableAncestor]; - -const projectCollisionDetection: CollisionDetection = (args) => { - const pointerCollisions = pointerWithin(args); - if (pointerCollisions.length > 0) { - return pointerCollisions; - } - - return closestCorners(args); -}; - -function openAddProjectCommandPalette() { - openCommandPalette({ open: "add-project" }); -} - -// Separate hooks cache real inputs; later sidebar hooks would invalidate every row's props. -function useLegacySidebarProjects() { - const projects = useProjects(); - const projectOrder = useUiStateStore((store) => store.projectOrder); - const projectGroupingSettings = useClientSettings(selectProjectGroupingSettings); - const { environments } = useEnvironments(); - const primaryEnvironmentId = usePrimaryEnvironmentId(); - const environmentLabelById = new Map( - environments.map((environment) => [environment.environmentId, environment.label] as const), - ); - const desktopLocalEnvironmentIds = new Set( - environments - .filter((environment) => isDesktopLocalConnectionTarget(environment.entry.target)) - .map((environment) => environment.environmentId), - ); - const wslEnvironmentIds = new Set( - environments - .filter((environment) => isWslConnectionTarget(environment.entry.target)) - .map((environment) => environment.environmentId), - ); - const orderedProjects = orderItemsByPreferredIds({ - items: projects, - preferredIds: projectOrder, - getId: getProjectOrderKey, - getPreferenceIds: (project) => [ - getProjectOrderKey(project), - legacyProjectCwdPreferenceKey(project.workspaceRoot), - ], - }); - const physicalToLogicalKey = buildPhysicalToLogicalProjectKeyMap({ - projects: orderedProjects, - settings: projectGroupingSettings, - primaryEnvironmentId, - }); - const projectPhysicalKeyByScopedRef = new Map( - orderedProjects.map((project) => [ - scopedProjectKey(scopeProjectRef(project.environmentId, project.id)), - derivePhysicalProjectKey(project), - ]), - ); - const sidebarProjects: SidebarProjectSnapshot[] = buildSidebarProjectSnapshots({ - projects: orderedProjects, - settings: projectGroupingSettings, - primaryEnvironmentId, - resolveEnvironmentLabel: (environmentId) => environmentLabelById.get(environmentId) ?? null, - isDesktopLocalEnvironment: (environmentId) => desktopLocalEnvironmentIds.has(environmentId), - isWslEnvironment: (environmentId) => wslEnvironmentIds.has(environmentId), - }); - return { - projectsLength: projects.length, - orderedProjects, - physicalToLogicalKey, - projectPhysicalKeyByScopedRef, - sidebarProjects, - }; -} - -// Re-sort on thread changes without rebuilding project snapshots used as row keys. -function useSortedLegacySidebarProjects(input: { - sidebarProjects: readonly SidebarProjectSnapshot[]; - physicalToLogicalKey: ReadonlyMap; - projectPhysicalKeyByScopedRef: ReadonlyMap; - sidebarProjectSortOrder: SidebarProjectSortOrder; -}) { - const { - sidebarProjects, - physicalToLogicalKey, - projectPhysicalKeyByScopedRef, - sidebarProjectSortOrder, - } = input; - const sidebarThreads = useThreadShells(); - const delegatedUnderParent = useDelegatedUnderParent(); - const sidebarProjectByKey = new Map( - sidebarProjects.map((project) => [project.projectKey, project] as const), - ); - const sortableProjects = sidebarProjects.map((project) => ({ - ...project, - id: project.projectKey, - })); - const sortableThreads = filterSidebarV2VisibleThreads( - sidebarThreads, - null, - delegatedUnderParent, - ).map((thread) => { - const physicalKey = - projectPhysicalKeyByScopedRef.get( - scopedProjectKey(scopeProjectRef(thread.environmentId, thread.projectId)), - ) ?? scopedProjectKey(scopeProjectRef(thread.environmentId, thread.projectId)); - return { - ...thread, - projectId: (physicalToLogicalKey.get(physicalKey) ?? physicalKey) as ProjectId, - }; - }); - const sortedProjects = sortProjectsForSidebar( - sortableProjects, - sortableThreads, - sidebarProjectSortOrder, - ).flatMap((project) => { - const resolvedProject = sidebarProjectByKey.get(project.id); - return resolvedProject ? [resolvedProject] : []; - }); - return { - sortedProjects, - sortedProjectKeys: sortedProjects.map((project) => project.projectKey), - }; -} - -function useLegacySidebarThreads(input: { - sortedProjects: readonly SidebarProjectSnapshot[]; - physicalToLogicalKey: ReadonlyMap; - projectPhysicalKeyByScopedRef: ReadonlyMap; - routeThreadKey: string | null; - sidebarThreadSortOrder: SidebarThreadSortOrder; - sidebarThreadPreviewCount: number; - expandedThreadListsByProject: ReadonlySet; -}) { - const { - sortedProjects, - physicalToLogicalKey, - projectPhysicalKeyByScopedRef, - routeThreadKey, - sidebarThreadSortOrder, - sidebarThreadPreviewCount, - expandedThreadListsByProject, - } = input; - const sidebarThreads = useThreadShells(); - const delegatedUnderParent = useDelegatedUnderParent(); - const projectExpandedById = useUiStateStore((store) => store.projectExpandedById); - const sidebarThreadByKey = new Map( - sidebarThreads.map( - (thread) => - [scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)), thread] as const, - ), - ); - const logicalProjectKeyForThread = (thread: SidebarThreadSummary) => { - const physicalKey = - projectPhysicalKeyByScopedRef.get( - scopedProjectKey(scopeProjectRef(thread.environmentId, thread.projectId)), - ) ?? scopedProjectKey(scopeProjectRef(thread.environmentId, thread.projectId)); - return physicalToLogicalKey.get(physicalKey) ?? physicalKey; - }; - const activeRouteThread = routeThreadKey ? sidebarThreadByKey.get(routeThreadKey) : undefined; - const activeRouteProjectKey = activeRouteThread - ? logicalProjectKeyForThread(activeRouteThread) - : null; - const threadsByProjectKey = new Map(); - for (const thread of sidebarThreads) { - const logicalKey = logicalProjectKeyForThread(thread); - const existing = threadsByProjectKey.get(logicalKey); - if (existing) { - existing.push(thread); - } else { - threadsByProjectKey.set(logicalKey, [thread]); - } - } - const visibleSidebarThreadKeys = sortedProjects.flatMap((project) => { - const projectThreads = sortThreads( - filterSidebarV2VisibleThreads( - threadsByProjectKey.get(project.projectKey) ?? [], - null, - delegatedUnderParent, - ), - sidebarThreadSortOrder, - ); - const projectExpanded = resolveProjectExpanded( - projectExpandedById, - projectExpansionPreferenceKeys(project), - ); - const activeThreadKey = routeThreadKey ?? undefined; - const pinnedCollapsedThread = - !projectExpanded && activeThreadKey - ? (projectThreads.find( - (thread) => - scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)) === activeThreadKey, - ) ?? null) - : null; - const shouldShowThreadPanel = projectExpanded || pinnedCollapsedThread !== null; - if (!shouldShowThreadPanel) { - return []; - } - const isThreadListExpanded = expandedThreadListsByProject.has(project.projectKey); - const hasOverflowingThreads = projectThreads.length > sidebarThreadPreviewCount; - const previewThreads = - isThreadListExpanded || !hasOverflowingThreads - ? projectThreads - : projectThreads.slice(0, sidebarThreadPreviewCount); - if (pinnedCollapsedThread) { - return [ - scopedThreadKey( - scopeThreadRef(pinnedCollapsedThread.environmentId, pinnedCollapsedThread.id), - ), - ]; - } - return projectThreads.flatMap((thread, index) => { - const threadKey = scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)); - return index < previewThreads.length || threadKey === activeThreadKey ? [threadKey] : []; - }); - }); - const threadJumpCommandByKey = new Map< - string, - NonNullable> - >(); - for (const [visibleThreadIndex, threadKey] of visibleSidebarThreadKeys.entries()) { - const jumpCommand = threadJumpCommandForIndex(visibleThreadIndex); - if (!jumpCommand) { - break; - } - threadJumpCommandByKey.set(threadKey, jumpCommand); - } - const prewarmedSidebarThreadRefs = getSidebarThreadIdsToPrewarm(visibleSidebarThreadKeys).flatMap( - (threadKey) => { - const ref = parseScopedThreadKey(threadKey); - return ref ? [ref] : []; - }, - ); - return { - sidebarThreadByKey, - activeRouteProjectKey, - visibleSidebarThreadKeys, - threadJumpCommandByKey, - threadJumpThreadKeys: [...threadJumpCommandByKey.keys()], - prewarmedSidebarThreadRefs, - }; -} - -function clearThreadSelectionOnOutsideMouseDown(event: globalThis.MouseEvent) { - const selection = useThreadSelectionStore.getState(); - if (!selection.hasSelection()) return; - const target = event.target instanceof HTMLElement ? event.target : null; - if (shouldClearThreadSelectionOnMouseDown(target)) selection.clearSelection(); -} - -export default function LegacySidebar({ routeReveal }: { routeReveal: SidebarRouteReveal }) { - const reorderProjects = useUiStateStore((store) => store.reorderProjects); - const navigate = useNavigate(); - const sidebarThreadSortOrder = useClientSettings((s) => s.sidebarThreadSortOrder); - const sidebarProjectSortOrder = useClientSettings((s) => s.sidebarProjectSortOrder); - const sidebarThreadPreviewCount = useClientSettings((s) => s.sidebarThreadPreviewCount); - const handleNewThread = useNewThreadHandler(); - const { archiveThread, deleteThread, markThreadUnread } = useThreadActions(); - const { isMobile, setOpenMobile } = useSidebar(); - const routeTarget = useThreadRouteTarget(); - const routeDraftThread = useComposerDraftStore((store) => - routeTarget?.kind === "draft" ? store.getDraftSession(routeTarget.draftId) : null, - ); - const routeThreadRef = resolveActiveThreadRouteRef(routeTarget, routeDraftThread); - const routeThreadKey = routeThreadRef ? scopedThreadKey(routeThreadRef) : null; - const routeTerminalOpen = useTerminalUiStateStore((state) => - routeThreadRef - ? selectThreadTerminalUiState(state.terminalUiStateByThreadKey, routeThreadRef).terminalOpen - : false, - ); - const keybindings = useAtomValue(primaryServerKeybindingsAtom); - const [expandedThreadListsByProject, setExpandedThreadListsByProject] = useState< - ReadonlySet - >(() => new Set()); - const dragInProgressRef = useRef(false); - const suppressProjectClickAfterDragRef = useRef(false); - const suppressProjectClickForContextMenuRef = useRef(false); - const desktopUpdateState = useDesktopUpdateState(); - const [desktopUpdateActionPending, setDesktopUpdateActionPending] = useState(false); - const clearSelection = useThreadSelectionStore((s) => s.clearSelection); - const setSelectionAnchor = useThreadSelectionStore((s) => s.setAnchor); - const platform = navigator.platform; - const shortcutModifiers = useShortcutModifierState(); - const terminalFocused = useTerminalFocus(); - const { - projectsLength, - orderedProjects, - physicalToLogicalKey, - projectPhysicalKeyByScopedRef, - sidebarProjects, - } = useLegacySidebarProjects(); - const { sortedProjects, sortedProjectKeys } = useSortedLegacySidebarProjects({ - sidebarProjects, - physicalToLogicalKey, - projectPhysicalKeyByScopedRef, - sidebarProjectSortOrder, - }); - const { - sidebarThreadByKey, - activeRouteProjectKey, - visibleSidebarThreadKeys, - threadJumpCommandByKey, - threadJumpThreadKeys, - prewarmedSidebarThreadRefs, - } = useLegacySidebarThreads({ - sortedProjects, - physicalToLogicalKey, - projectPhysicalKeyByScopedRef, - routeThreadKey, - sidebarThreadSortOrder, - sidebarThreadPreviewCount, - expandedThreadListsByProject, - }); - const revealActiveRow = useActiveRowReveal(routeReveal, routeThreadKey); - const newThreadShortcutLabelOptions = { - platform, - context: { - terminalFocus: false, - terminalOpen: false, - }, - }; - const newThreadShortcutLabel = - shortcutLabelForCommand(keybindings, "chat.newLocal", newThreadShortcutLabelOptions) ?? - shortcutLabelForCommand(keybindings, "chat.new", newThreadShortcutLabelOptions); - - const navigateToThread = (threadRef: ScopedThreadRef) => { - if (useThreadSelectionStore.getState().selectedThreadKeys.size > 0) { - clearSelection(); - } - setSelectionAnchor(scopedThreadKey(threadRef)); - if (isMobile) { - setOpenMobile(false); - } - void navigate({ - to: "/$environmentId/$threadId", - params: buildThreadRouteParams(threadRef), - }); - }; - - const projectDnDSensors = useSensors(useSensor(PointerSensor, PROJECT_POINTER_SENSOR_OPTIONS)); - - const handleProjectDragEnd = useStableCallback((event: DragEndEvent) => { - if (sidebarProjectSortOrder !== "manual") { - dragInProgressRef.current = false; - return; - } - dragInProgressRef.current = false; - const { active, over } = event; - if (!over || active.id === over.id) return; - const activeProject = sidebarProjects.find((project) => project.projectKey === active.id); - const overProject = sidebarProjects.find((project) => project.projectKey === over.id); - if (!activeProject || !overProject) return; - const activeMemberKeys = activeProject.memberProjects.map( - (member) => member.physicalProjectKey, - ); - const overMemberKeys = overProject.memberProjects.map((member) => member.physicalProjectKey); - reorderProjects(orderedProjects.map(getProjectOrderKey), activeMemberKeys, overMemberKeys); - }); - - const handleProjectDragStart = useStableCallback((_event: DragStartEvent) => { - if (sidebarProjectSortOrder !== "manual") { - return; - } - dragInProgressRef.current = true; - suppressProjectClickAfterDragRef.current = true; - }); - - const handleProjectDragCancel = (_event: DragCancelEvent) => { - dragInProgressRef.current = false; - }; - - const animatedProjectListsRef = useRef(new WeakSet()); - const attachProjectListAutoAnimateRef = (node: HTMLElement | null) => { - if (!node || animatedProjectListsRef.current.has(node)) { - return; - } - autoAnimate(node, SIDEBAR_LIST_ANIMATION_OPTIONS); - animatedProjectListsRef.current.add(node); - }; - - const animatedThreadListsRef = useRef(new WeakSet()); - const attachThreadListAutoAnimateRef = (node: HTMLElement | null) => { - if (!node || animatedThreadListsRef.current.has(node)) { - return; - } - autoAnimate(node, SIDEBAR_LIST_ANIMATION_OPTIONS); - animatedThreadListsRef.current.add(node); - }; - - const isManualProjectSorting = sidebarProjectSortOrder === "manual"; - // Build before modifier-dependent context to give the compiler a separate cache used by every row. - const threadJumpLabelByKey = buildThreadJumpLabelMap({ - keybindings, - platform, - terminalOpen: routeTerminalOpen, - threadJumpCommandByKey, - }); - const sidebarShortcutContext = { - terminalFocus: terminalFocused, - terminalOpen: routeTerminalOpen, - modelPickerOpen: isModelPickerOpen(), - }; - const shouldShowThreadJumpHintsNow = shouldShowThreadJumpHintsForModifiers( - shortcutModifiers, - keybindings, - { - platform, - context: sidebarShortcutContext, - }, - ); - const showThreadJumpHints = useThreadJumpHintsVisible(shouldShowThreadJumpHintsNow); - const visibleThreadJumpLabelByKey = showThreadJumpHints - ? threadJumpLabelByKey - : EMPTY_THREAD_JUMP_LABELS; - const orderedSidebarThreadKeys = visibleSidebarThreadKeys; - - const onWindowKeyDown = useEffectEvent((event: globalThis.KeyboardEvent) => { - const shortcutContext = { - terminalFocus: isTerminalFocused(), - terminalOpen: routeTerminalOpen, - modelPickerOpen: isModelPickerOpen(), - }; - - if (event.defaultPrevented || event.repeat || isCommandPaletteOpen() || isModelPickerOpen()) { - return; - } - - const command = resolveShortcutCommand(event, keybindings, { - platform, - context: shortcutContext, - }); - const traversalDirection = threadTraversalDirectionFromCommand(command); - if (traversalDirection !== null) { - const targetThreadKey = resolveAdjacentThreadId({ - threadIds: orderedSidebarThreadKeys, - currentThreadId: routeThreadKey, - direction: traversalDirection, - }); - if (!targetThreadKey) { - return; - } - const targetThread = sidebarThreadByKey.get(targetThreadKey); - if (!targetThread) { - return; - } - - event.preventDefault(); - event.stopPropagation(); - navigateToThread(scopeThreadRef(targetThread.environmentId, targetThread.id)); - return; - } - - const jumpIndex = threadJumpIndexFromCommand(command ?? ""); - if (jumpIndex === null) { - return; - } - - const targetThreadKey = threadJumpThreadKeys[jumpIndex]; - if (!targetThreadKey) { - return; - } - const targetThread = sidebarThreadByKey.get(targetThreadKey); - if (!targetThread) { - return; - } - - event.preventDefault(); - event.stopPropagation(); - navigateToThread(scopeThreadRef(targetThread.environmentId, targetThread.id)); - }); - // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to window keydown for thread navigation shortcuts for the sidebar's lifetime - useEffect(() => { - const handleWindowKeyDown = (event: globalThis.KeyboardEvent) => onWindowKeyDown(event); - window.addEventListener("keydown", handleWindowKeyDown); - return () => { - window.removeEventListener("keydown", handleWindowKeyDown); - }; - }, []); - // A window listener whose lifetime is the sidebar's: no element owns outside clicks. - // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- window mousedown listener that clears thread selection on outside clicks for the sidebar's lifetime - useEffect(() => { - window.addEventListener("mousedown", clearThreadSelectionOnOutsideMouseDown); - return () => window.removeEventListener("mousedown", clearThreadSelectionOnOutsideMouseDown); - }, []); - - const desktopUpdateButtonDisabled = isDesktopUpdateButtonDisabled(desktopUpdateState); - const desktopUpdateButtonAction = desktopUpdateState - ? resolveDesktopUpdateButtonAction(desktopUpdateState) - : "none"; - const showArm64IntelBuildWarning = - isElectron && shouldShowArm64IntelBuildWarning(desktopUpdateState); - const arm64IntelBuildWarningDescription = - desktopUpdateState && showArm64IntelBuildWarning - ? getArm64IntelBuildWarningDescription(desktopUpdateState) - : null; - const commandPaletteShortcutLabel = isMobile - ? null - : shortcutLabelForCommand(keybindings, "commandPalette.toggle", newThreadShortcutLabelOptions); - const handleDesktopUpdateButtonClick = async () => { - const bridge = window.desktopBridge; - if (!bridge || !desktopUpdateState) return; - if ( - desktopUpdateButtonDisabled || - desktopUpdateButtonAction === "none" || - desktopUpdateActionPending - ) { - return; - } - - setDesktopUpdateActionPending(true); - - if (desktopUpdateButtonAction === "download") { - void bridge - .downloadUpdate() - .then((result) => { - if (result.completed) { - showDesktopUpdateDownloadedToast(bridge, result.state); - } - if (!shouldToastDesktopUpdateActionResult(result)) return; - const actionError = getDesktopUpdateActionError(result); - if (!actionError) return; - toastManager.add( - stackedThreadToast({ - type: "error", - title: "Could not download update", - description: actionError, - }), - ); - }) - .catch((error) => { - toastManager.add( - stackedThreadToast({ - type: "error", - title: "Could not start update download", - description: error instanceof Error ? error.message : "An unexpected error occurred.", - }), - ); - }) - .finally(() => setDesktopUpdateActionPending(false)); - return; - } - - if (desktopUpdateButtonAction === "install") { - let confirmed = false; - try { - confirmed = await ensureLocalApi().dialogs.confirm( - getDesktopUpdateInstallConfirmationMessage(desktopUpdateState), - ); - } catch (error) { - setDesktopUpdateActionPending(false); - toastManager.add( - stackedThreadToast({ - type: "error", - title: "Could not confirm update", - description: error instanceof Error ? error.message : "Update confirmation failed.", - }), - ); - return; - } - if (!confirmed) { - setDesktopUpdateActionPending(false); - return; - } - void bridge - .installUpdate() - .then((result) => { - if (!shouldToastDesktopUpdateActionResult(result)) return; - const actionError = getDesktopUpdateActionError(result); - if (!actionError) return; - toastManager.add( - stackedThreadToast({ - type: "error", - title: "Could not install update", - description: actionError, - }), - ); - }) - .catch((error) => { - toastManager.add( - stackedThreadToast({ - type: "error", - title: "Could not install update", - description: error instanceof Error ? error.message : "An unexpected error occurred.", - }), - ); - }) - .finally(() => setDesktopUpdateActionPending(false)); - } - }; - - const expandThreadListForProject = (projectKey: string) => { - setExpandedThreadListsByProject((current) => { - if (current.has(projectKey)) return current; - const next = new Set(current); - next.add(projectKey); - return next; - }); - }; - - const collapseThreadListForProject = (projectKey: string) => { - setExpandedThreadListsByProject((current) => { - if (!current.has(projectKey)) return current; - const next = new Set(current); - next.delete(projectKey); - return next; - }); - }; - - return ( - - {prewarmedSidebarThreadRefs.map((threadRef) => ( - - ))} - - - - - - ); -} diff --git a/apps/web/src/components/Sidebar.logic.test.ts b/apps/web/src/components/Sidebar.logic.test.ts index 1bf7a930c1f24..93f3fb342fb9e 100644 --- a/apps/web/src/components/Sidebar.logic.test.ts +++ b/apps/web/src/components/Sidebar.logic.test.ts @@ -32,7 +32,11 @@ import { type SidebarListMarker, type SidebarSection, resolveSidebarDropVerb, + groupSidebarThreadsByProject, + resolveSidebarFolderDot, + sidebarFolderExpansionKeys, } from "./Sidebar.logic"; +import { resolveProjectExpanded } from "../uiStateStore"; import { threadSearchMatchKey } from "@t3tools/client-runtime/state/thread-search"; import { EnvironmentId, ProjectId, ProviderInstanceId, RunId, ThreadId } from "@t3tools/contracts"; @@ -1348,3 +1352,97 @@ describe("resolveSidebarRouteReveal", () => { ).toEqual({ pendingKey: hiddenKey, widensScope: false }); }); }); + +describe("sidebarFolderExpansionKeys", () => { + it("honors a collapse the legacy sidebar saved under a member's cwd", () => { + const keys = sidebarFolderExpansionKeys({ + projectKey: "github.com/pingdotgg/t3code", + memberProjects: [{ physicalProjectKey: "local:/code/t3code", workspaceRoot: "/code/t3code" }], + }); + const cwdKey = keys.at(-1)!; + + expect(resolveProjectExpanded({}, keys)).toBe(true); + expect(resolveProjectExpanded({ [cwdKey]: false }, keys)).toBe(false); + expect(resolveProjectExpanded({ [keys[0]!]: true, [cwdKey]: false }, keys)).toBe(true); + }); +}); + +describe("groupSidebarThreadsByProject", () => { + it("puts each thread in its project's folder and keeps empty projects", () => { + const remoteEnvironmentId = EnvironmentId.make("environment-remote"); + const t3code = ProjectId.make("t3code"); + const t3codeRemote = ProjectId.make("t3code-remote"); + const fleet = ProjectId.make("fleet"); + const thread = (id: string, environmentId: EnvironmentId, projectId: ProjectId) => ({ + id, + environmentId, + projectId, + }); + const groups = [ + { + projectKey: "t3code", + memberProjectRefs: [ + { environmentId: localEnvironmentId, projectId: t3code }, + { environmentId: remoteEnvironmentId, projectId: t3codeRemote }, + ], + }, + { + projectKey: "fleet", + memberProjectRefs: [{ environmentId: localEnvironmentId, projectId: fleet }], + }, + ]; + + const folders = groupSidebarThreadsByProject(groups, [ + thread("a", localEnvironmentId, t3code), + thread("orphan", localEnvironmentId, ProjectId.make("deleted")), + thread("b", remoteEnvironmentId, t3codeRemote), + ]); + + expect( + folders.map((folder) => ({ + projectKey: folder.group.projectKey, + threads: folder.threads.map((entry) => entry.id), + })), + ).toEqual([ + { projectKey: "t3code", threads: ["a", "b"] }, + { projectKey: "fleet", threads: [] }, + ]); + }); +}); + +describe("resolveSidebarFolderDot", () => { + it("flags the most urgent hidden thread and stays quiet otherwise", () => { + const idle = { + hasActionableProposedPlan: false, + hasPendingApprovals: false, + hasPendingUserInput: false, + interactionMode: "default" as const, + latestRun: makeLatestRun(), + lastVisitedAt: "2026-03-09T10:06:00.000Z", + runtime: null, + }; + const runtime = { + status: "running" as const, + activeRunId: null, + providerInstanceId: ProviderInstanceId.make("codex"), + providerName: "Codex", + lastError: null, + updatedAt: "2026-03-09T10:00:00.000Z", + }; + const working = { ...idle, runtime }; + const unread = { ...idle, lastVisitedAt: "2026-03-09T10:04:00.000Z" }; + const limited = { + ...idle, + runtime: { ...runtime, status: "failed" as const, lastErrorClass: "usage_limit" as const }, + }; + + expect(resolveSidebarFolderDot([idle])).toBeNull(); + // A waiting thread stays quiet even with an unread completion, like its row. + expect( + resolveSidebarFolderDot([{ ...unread, runtime: { ...runtime, status: "idle" as const } }]), + ).toBeNull(); + expect(resolveSidebarFolderDot([idle, working])).toBe("working"); + expect(resolveSidebarFolderDot([working, unread])).toBe("unread"); + expect(resolveSidebarFolderDot([unread, limited])).toBe("attention"); + }); +}); diff --git a/apps/web/src/components/Sidebar.logic.ts b/apps/web/src/components/Sidebar.logic.ts index aa4903db93e00..7f7705573673c 100644 --- a/apps/web/src/components/Sidebar.logic.ts +++ b/apps/web/src/components/Sidebar.logic.ts @@ -34,6 +34,7 @@ import { isLatestRunSettled } from "../session-logic"; import { observeIntersection } from "../lib/observeIntersection"; import { deadlineAfter, deadlineStore } from "@t3tools/client-runtime/clock"; import { useDeadlinePassed } from "../hooks/useDeadline"; +import { legacyProjectCwdPreferenceKey } from "../uiStateStore"; export function shouldNavigateAfterThreadPark(input: { readonly threadKey: string; @@ -51,7 +52,6 @@ export function shouldNavigateAfterThreadPark(input: { ); } -const THREAD_SELECTION_SAFE_SELECTOR = "[data-thread-item], [data-thread-selection-safe]"; const THREAD_JUMP_HINT_SHOW_DELAY_MS = 200; export function resolveSidebarRowAccessibility(input: { @@ -67,7 +67,6 @@ export function resolveSidebarRowAccessibility(input: { } // Each prewarm retains growing thread detail and a live subscription, multiplying heap and server load. -const SIDEBAR_THREAD_PREWARM_LIMIT = 3; const SIDEBAR_ROW_SUBSCRIPTION_OVERSCAN_PX = 160; export function useSidebarRowSubscriptionLease(isActive: boolean): { @@ -542,21 +541,6 @@ export async function archiveSelectedThreadEntries< return { archivedThreadKeys, mutationFailure: null, followupFailures }; } -export function buildMultiSelectThreadContextMenuItems(input: { - count: number; - hasRunningThread: boolean; -}): readonly ContextMenuItem<"mark-unread" | "archive" | "delete">[] { - return [ - { id: "mark-unread", label: `Mark unread (${input.count})` }, - { - id: "archive", - label: `Archive (${input.count})`, - disabled: input.hasRunningThread, - }, - { id: "delete", label: `Delete (${input.count})`, destructive: true }, - ]; -} - export function isSidebarSubagentThread(thread: Pick): boolean { return thread.lineage.relationshipToParent === "subagent"; } @@ -722,11 +706,6 @@ function hasUnseenFailure(thread: ThreadStatusInput): boolean { return completedAt > lastVisitedAt; } -export function shouldClearThreadSelectionOnMouseDown(target: HTMLElement | null): boolean { - if (target === null) return true; - return !target.closest(THREAD_SELECTION_SAFE_SELECTOR); -} - // Suppress trailing double-click navigation; keyboard and synthetic clicks use detail 0. export function isTrailingDoubleClick(detail: number): boolean { return detail > 1; @@ -795,13 +774,6 @@ export function orderItemsByPreferredIds(input: { return [...ordered, ...remaining]; } -export function getSidebarThreadIdsToPrewarm( - visibleThreadIds: readonly TThreadId[], - limit = SIDEBAR_THREAD_PREWARM_LIMIT, -): TThreadId[] { - return visibleThreadIds.slice(0, Math.max(0, limit)); -} - export function resolveAdjacentThreadId(input: { threadIds: readonly T[]; currentThreadId: T | null; @@ -829,15 +801,6 @@ export function resolveAdjacentThreadId(input: { return currentIndex < threadIds.length - 1 ? (threadIds[currentIndex + 1] ?? null) : null; } -export function isContextMenuPointerDown(input: { - button: number; - ctrlKey: boolean; - isMac: boolean; -}): boolean { - if (input.button === 2) return true; - return input.isMac && input.button === 0 && input.ctrlKey; -} - export type SidebarThreadStatus = | "approval" | "input" @@ -909,6 +872,92 @@ export function resolveSidebarThreadStatus(thread: SidebarThreadStatusInput): Si return "ready"; } +/** + * The dot on a closed boring-sidebar folder: the most urgent state among its + * hidden threads. Waiting threads stay quiet, like their rows. + */ +export function resolveSidebarFolderDot( + threads: ReadonlyArray, +): "attention" | "failed" | "unread" | "working" | null { + const statuses = new Set(threads.map(resolveSidebarThreadStatus)); + if ( + statuses.has("input") || + statuses.has("approval") || + statuses.has("request") || + statuses.has("limited") + ) { + return "attention"; + } + if (statuses.has("failed")) return "failed"; + // Rows show Done only for ready threads, so the dot follows the same rule. + const hasUnread = threads.some( + (thread) => resolveSidebarThreadStatus(thread) === "ready" && hasUnseenCompletion(thread), + ); + if (hasUnread) return "unread"; + if (statuses.has("working")) return "working"; + return null; +} + +/** + * Keys the old per-project sidebar saved folder open state under, most + * specific first, for `resolveProjectExpanded`. Boring folders read and write + * the same keys, so a legacy user's closed folders stay closed after regrouping. + */ +export function sidebarFolderExpansionKeys(group: { + readonly projectKey: string; + readonly memberProjects: ReadonlyArray<{ + readonly physicalProjectKey: string; + readonly workspaceRoot: string; + }>; +}): string[] { + return [ + group.projectKey, + ...group.memberProjects.map((member) => member.physicalProjectKey), + ...group.memberProjects.map((member) => legacyProjectCwdPreferenceKey(member.workspaceRoot)), + ]; +} + +/** + * Boring sidebar folders. Folders follow `groups` order and include empty + * projects so each one can start a thread. Threads keep their input order. + * A thread whose project is in no group has no folder and is left out. + */ +export function groupSidebarThreadsByProject< + TGroup extends { + readonly memberProjectRefs: ReadonlyArray<{ + readonly environmentId: EnvironmentId; + readonly projectId: string; + }>; + }, + TThread extends Pick, +>( + groups: readonly TGroup[], + threads: readonly TThread[], +): Array<{ group: TGroup; threads: TThread[] }> { + const folders = groups.map((group) => ({ group, threads: [] as TThread[] })); + const folderByProject = new Map(); + for (const folder of folders) { + for (const ref of folder.group.memberProjectRefs) { + folderByProject.set(`${ref.environmentId}:${ref.projectId}`, folder); + } + } + for (const thread of threads) { + folderByProject.get(`${thread.environmentId}:${thread.projectId}`)?.threads.push(thread); + } + return folders; +} + +export type SidebarV2TopStatusKind = + | "approval" + | "done" + | "failed" + | "limited" + | "input" + | "request" + | "waiting" + | "woke" + | "working"; + export function firstValidTimestampMs( ...candidates: ReadonlyArray ): number { diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsx index 280e23dad4060..6bda36fc85919 100644 --- a/apps/web/src/components/Sidebar.tsx +++ b/apps/web/src/components/Sidebar.tsx @@ -110,7 +110,7 @@ import { getSidebarProjectSettingsKey, type SidebarProjectSnapshot, } from "../sidebarProjectGrouping"; -import { useUiStateStore } from "../uiStateStore"; +import { resolveProjectExpanded, useUiStateStore } from "../uiStateStore"; import { getThreadKeysToDeselectAfterDelete, useThreadSelectionStore, @@ -122,7 +122,7 @@ import { useTerminalFocus } from "../hooks/useTerminalFocus"; import { isCommandPaletteOpen, openCommandPalette } from "../commandPaletteBus"; import { registerSettleAndAdvance } from "../settleAndAdvance"; import { startNewThreadFromContext } from "../lib/chatThreadActions"; -import { useClientSettings } from "../hooks/useSettings"; +import { useBoringSidebarEnabled, useClientSettings } from "../hooks/useSettings"; import { useCopyToClipboard } from "../hooks/useCopyToClipboard"; import { useLocalStorage } from "../hooks/useLocalStorage"; import { useNowMinute } from "../hooks/useNowMinute"; @@ -168,6 +168,8 @@ import { import { applySidebarThreadDrop, filterSidebarV2VisibleThreads, + groupSidebarThreadsByProject, + sidebarFolderExpansionKeys, buildBulkTitleRegenerationContextMenuItem, buildBulkUnpinContextMenuItem, deleteSelectedThreadEntries, @@ -246,6 +248,10 @@ import { type SidebarSweepAction, } from "./sidebar/SidebarThreadRow"; import { SidebarDraftBlock } from "./sidebar/SidebarDraftBlock"; +import { + SidebarProjectFolderHeader, + sidebarFolderRowId, +} from "./sidebar/SidebarProjectFolderHeader"; import { SidebarDragBoundary, SidebarSectionHeader, @@ -391,6 +397,8 @@ function useSidebarThreads(input: { readonly threads: readonly EnvironmentThreadShell[]; readonly delegatedUnderParent: ReadonlySet; readonly routeRowThreadKey: string | null; + // Set in boring mode: the project groups to fold active threads into, in order. + readonly folderGroups: readonly SidebarProjectSnapshot[] | null; }) { const { projectScopeKey, @@ -400,8 +408,11 @@ function useSidebarThreads(input: { threads, delegatedUnderParent, routeRowThreadKey, + folderGroups, } = input; - const workingShelfEnabled = useClientSettings((s) => s.sidebarWorkingShelfEnabled); + // Folders already separate the work, so boring mode has no Working shelf. + const workingShelfEnabled = + useClientSettings((s) => s.sidebarWorkingShelfEnabled) && folderGroups === null; const inboxReturnsVersion = useSyncExternalStore( inboxReturns.subscribe, inboxReturns.getSnapshot, @@ -424,6 +435,9 @@ function useSidebarThreads(input: { false, Schema.Boolean, ); + // Shared with the old per-project sidebar, so folders keep their open state. + const projectExpandedById = useUiStateStore((store) => store.projectExpandedById); + const setProjectExpanded = useUiStateStore((store) => store.setProjectExpanded); const [settledTail, setSettledTail] = useState({ scopeKey: projectScopeKey, count: SETTLED_TAIL_INITIAL_COUNT, @@ -478,9 +492,31 @@ function useSidebarThreads(input: { const visibleWorkingThreads = workingShelfExpanded ? workingThreads : routeThreadOnly(workingThreads, routeRowThreadKey); + const projectFolders = + folderGroups === null + ? null + : groupSidebarThreadsByProject(folderGroups, activeThreads).map((folder) => { + const collapsed = !resolveProjectExpanded( + projectExpandedById, + sidebarFolderExpansionKeys(folder.group), + ); + return { + group: folder.group, + collapsed, + // A closed folder still shows the open thread, and hints at the rest. + threads: collapsed + ? routeThreadOnly(folder.threads, routeRowThreadKey) + : folder.threads, + hiddenThreads: collapsed + ? folder.threads.filter((thread) => threadShellKey(thread) !== routeRowThreadKey) + : EMPTY_THREADS, + }; + }); const orderedThreads = [ ...pinnedThreads, - ...activeThreads, + ...(projectFolders === null + ? activeThreads + : projectFolders.flatMap((folder) => folder.threads)), ...visibleWorkingThreads, ...visibleSnoozedThreads, ...renderedSettledThreads, @@ -521,6 +557,11 @@ function useSidebarThreads(input: { toggleSnoozedShelf: () => setSnoozedShelfExpanded((value) => !value), workingShelfExpanded, toggleWorkingShelf: () => setWorkingShelfExpanded((value) => !value), + projectFolders, + toggleProjectFolder: (group: SidebarProjectSnapshot) => { + const keys = sidebarFolderExpansionKeys(group); + setProjectExpanded(keys, !resolveProjectExpanded(projectExpandedById, keys)); + }, renderedSettledThreads, visibleSnoozedThreads, visibleWorkingThreads, @@ -911,6 +952,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve ); }, }); + const boringSidebarEnabled = useBoringSidebarEnabled(); const newThreadContext = useHandleNewThread(); // Row callbacks capture only the handler; the rest of the context changes with the route. const { handleNewThread } = newThreadContext; @@ -1145,6 +1187,40 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve openProjectSettings(projectGroup); }, ); + const startThreadInProjectFolder = useStableCallback((group: SidebarProjectSnapshot) => { + if (isMobile) setOpenMobile(false); + void handleNewThread(scopeProjectRef(group.environmentId, group.id)); + }); + const handleProjectFolderContextMenu = useStableCallback( + (group: SidebarProjectSnapshot, position: { x: number; y: number }) => { + const api = readLocalApi(); + if (!api) return; + void (async () => { + const clicked = await settlePromise(() => + api.contextMenu.show( + [ + { id: "new-thread", label: "New thread" }, + { id: "copy-path", label: "Copy path" }, + { id: "project-settings", label: "Project settings", icon: "settings" }, + ], + position, + ), + ); + if (clicked._tag === "Failure") return; + switch (clicked.value) { + case "new-thread": + startThreadInProjectFolder(group); + return; + case "copy-path": + copyPathToClipboard(group.workspaceRoot, { path: group.workspaceRoot }); + return; + case "project-settings": + openProjectSettings(group); + return; + } + })(); + }, + ); const { workingShelfEnabled, @@ -1168,6 +1244,8 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve toggleSnoozedShelf, workingShelfExpanded, toggleWorkingShelf, + projectFolders, + toggleProjectFolder, renderedSettledThreads, visibleSnoozedThreads, visibleWorkingThreads, @@ -1188,6 +1266,11 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve threads, delegatedUnderParent, routeRowThreadKey, + folderGroups: boringSidebarEnabled + ? scopedProjectGroups.length === 0 + ? projectGroups + : scopedProjectGroups + : null, }); // Ranked across environments as the palette ranks them, so one @@ -1748,6 +1831,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve event.activatorEvent instanceof PointerEvent ? event.activatorEvent.clientY : null, }); }; + const hasProjectFolders = projectFolders !== null && projectFolders.length > 0; const sidebarListItemsFresh = ((): readonly SidebarListItem[] => { const rowsOf = ( list: readonly EnvironmentThreadShell[], @@ -1757,13 +1841,15 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve const key = scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)); return { kind: "thread", key, section }; }); + // Boring mode keeps empty project folders, so each can start a thread. if ( + !hasProjectFolders && pinnedThreads.length + activeThreads.length + workingThreads.length + snoozedThreads.length + settledCount === - 0 + 0 ) { return []; } @@ -1813,6 +1899,9 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve // Only order, section, or draft changes need layout reads and motion. const listMotionLayoutKey = [ sidebarListItemsKey, + projectFolders + ?.map((folder) => `${folder.group.projectKey}:${folder.threads.length}`) + .join("\n") ?? "", visibleDraftSessionCount, routeDraftIdForRows ?? "", ].join("\0"); @@ -2742,7 +2831,10 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve const showJumpHints = useThreadJumpHintsVisible(shouldShowJumpHintsNow); const handleNewThreadClick = (event?: ReactMouseEvent) => { - if (shouldCreateNewThreadInCurrentProject(event?.shiftKey ?? false, projectGroups.length)) { + if ( + boringSidebarEnabled || + shouldCreateNewThreadInCurrentProject(event?.shiftKey ?? false, projectGroups.length) + ) { if (isMobile) setOpenMobile(false); void startNewThreadFromContext({ activeDraftThread: newThreadContext.activeDraftThread, @@ -2942,7 +3034,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve newThreadDisabled={projects.length === 0} newThreadShortcutLabel={newThreadShortcutLabel} newThreadInProjectShortcutLabel={newThreadInProjectShortcutLabel} - showNewThreadInProjectHint={projectGroups.length > 1} + showNewThreadInProjectHint={!boringSidebarEnabled && projectGroups.length > 1} searchInputRef={threadSearchInputRef} searchQuery={threadSearchQuery} onSearchQueryChange={(value) => { @@ -3058,19 +3150,22 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve const renderThreadRow = ( thread: EnvironmentThreadShell, section: SidebarSection, + // The tree row of the project folder this row sits in. + folderRow?: string, ) => { const threadKey = scopedThreadKey( scopeThreadRef(thread.environmentId, thread.id), ); const isCard = section === "active" || section === "pinned" || section === "working"; - const rowVariant = isCard ? "card" : "slim"; + const rowVariant = folderRow ? "folder" : isCard ? "card" : "slim"; return ( , + ); + for (const thread of folder.threads) { + items.push( + renderThreadRow( + thread, + "active", + sidebarFolderRowId(folder.group.projectKey), + ), + ); + } + } + continue; + } switch (item.marker) { case "pinned-header": items.push( @@ -3330,6 +3453,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve ) : null} {!isSearchingThreads && + !hasProjectFolders && visibleDraftSessionCount === 0 && pinnedThreads.length + activeThreads.length + diff --git a/apps/web/src/components/ThreadStatusIndicators.tsx b/apps/web/src/components/ThreadStatusIndicators.tsx index 45eb01782c763..682172f1ff308 100644 --- a/apps/web/src/components/ThreadStatusIndicators.tsx +++ b/apps/web/src/components/ThreadStatusIndicators.tsx @@ -440,7 +440,7 @@ export function ChangeRequestStatusIcon({ return ; } -export function PrStatusTooltipContent({ status }: { status: PrStatusIndicator }) { +function PrStatusTooltipContent({ status }: { status: PrStatusIndicator }) { return ( {status.tooltipLead} @@ -600,7 +600,7 @@ export function ThreadWorktreeIndicator({ ); } -export function ThreadStatusLabel({ +function ThreadStatusLabel({ status, compact = false, }: { diff --git a/apps/web/src/components/settings/LabsSettingsPanel.tsx b/apps/web/src/components/settings/LabsSettingsPanel.tsx index b06f7b04c4fb8..5f944c867db68 100644 --- a/apps/web/src/components/settings/LabsSettingsPanel.tsx +++ b/apps/web/src/components/settings/LabsSettingsPanel.tsx @@ -4,6 +4,7 @@ import { Link } from "@tanstack/react-router"; import { hasCloudPublicConfig } from "~/cloud/publicConfig"; import { useT3ConnectAuthPrompt } from "../clerk/useT3ConnectAuthPrompt"; import { Button } from "../ui/button"; +import { Switch } from "../ui/switch"; import { ScopedSwitch } from "./ScopedSwitch"; import { SettingsPageContainer, SettingsRow, SettingsSection } from "./settingsLayout"; import { searchableSetting } from "./settingsSearch"; @@ -15,6 +16,7 @@ export function LabsSettingsPanel() { return ( + {/* Clerk is only mounted when this build has T3 Connect. */} {hasCloudPublicConfig() ? : } @@ -24,6 +26,27 @@ export function LabsSettingsPanel() { ); } +function BoringSidebarRow() { + const settings = useScopedSettings(); + const updateSettings = useUpdateScopedSettings(); + return ( + + updateSettings({ boringSidebarEnabled: Boolean(checked), legacySidebarEnabled: false }) + } + aria-label="Boring sidebar (group by project)" + /> + } + /> + ); +} + /** Signed out of T3 Connect, the row offers to sign in instead of its switch. */ function GuestLinksRowWithSignIn() { const { isSignedIn } = useAuth(); diff --git a/apps/web/src/components/settings/SettingsPanels.tsx b/apps/web/src/components/settings/SettingsPanels.tsx index 25e297470502f..a4aadac731081 100644 --- a/apps/web/src/components/settings/SettingsPanels.tsx +++ b/apps/web/src/components/settings/SettingsPanels.tsx @@ -425,9 +425,6 @@ export function useSettingsRestore(onRestored?: () => void) { ...(settings.inAppNotificationsEnabled !== DEFAULT_UNIFIED_SETTINGS.inAppNotificationsEnabled ? ["In-app notifications"] : []), - ...(settings.sidebarThreadPreviewCount !== DEFAULT_UNIFIED_SETTINGS.sidebarThreadPreviewCount - ? ["Visible threads"] - : []), ...(settings.sidebarProjectGroupingMode !== DEFAULT_UNIFIED_SETTINGS.sidebarProjectGroupingMode ? ["Project Grouping"] : []), @@ -603,7 +600,6 @@ export function useSettingsRestore(onRestored?: () => void) { environmentIdentificationMode: DEFAULT_UNIFIED_SETTINGS.environmentIdentificationMode, glassOpacity: DEFAULT_UNIFIED_SETTINGS.glassOpacity, panelAnimationDurationMs: DEFAULT_UNIFIED_SETTINGS.panelAnimationDurationMs, - sidebarThreadPreviewCount: DEFAULT_UNIFIED_SETTINGS.sidebarThreadPreviewCount, sidebarProjectGroupingMode: DEFAULT_UNIFIED_SETTINGS.sidebarProjectGroupingMode, sidebarProjectSortOrder: DEFAULT_UNIFIED_SETTINGS.sidebarProjectSortOrder, sidebarWorkingShelfEnabled: DEFAULT_UNIFIED_SETTINGS.sidebarWorkingShelfEnabled, @@ -732,19 +728,6 @@ export function LegacyFeaturesSection() { /> } /> - - updateSettings({ legacySidebarEnabled: Boolean(checked) }) - } - aria-label="Sidebar (legacy)" - /> - } - /> ); } @@ -851,7 +834,7 @@ export function GeneralSettingsPanel() { /> { const streaming = getSettingsSearchTargetScope("response-streaming")!; expect(streaming.scope).toBe("project-defaults"); expect(isSettingsSearchScopeAvailable(streaming.scope, "project")).toBe(true); - for (const id of ["legacy-plan-mode", "legacy-context-window-indicator", "legacy-sidebar"]) { + for (const id of ["legacy-plan-mode", "legacy-context-window-indicator", "boring-sidebar"]) { expect(getSettingsSearchTargetScope(id)!.scope).toBeNull(); } }); diff --git a/apps/web/src/components/settings/settingsSearch.ts b/apps/web/src/components/settings/settingsSearch.ts index 460a6ef650b61..25b5b7cc6d24e 100644 --- a/apps/web/src/components/settings/settingsSearch.ts +++ b/apps/web/src/components/settings/settingsSearch.ts @@ -419,6 +419,12 @@ export const SETTINGS_SEARCH_ITEMS = [ to: "/settings/general", searchTerms: ["hide fold running monitoring threads inbox sidebar shelf"], }, + { + id: "boring-sidebar", + title: "Boring sidebar (group by project)", + to: "/settings/labs", + searchTerms: ["beta folders project tree legacy old classic sidebar"], + }, { id: "guest-links", title: "Guest links", @@ -708,12 +714,6 @@ export const SETTINGS_SEARCH_ITEMS = [ to: "/settings/labs", searchTerms: ["composer meter usage tokens circle old"], }, - { - id: "legacy-sidebar", - title: "Sidebar (legacy)", - to: "/settings/labs", - searchTerms: ["project thread tree old flat list"], - }, { id: "keybindings", title: "Keybindings", diff --git a/apps/web/src/components/sidebar/SidebarProjectFolderHeader.tsx b/apps/web/src/components/sidebar/SidebarProjectFolderHeader.tsx new file mode 100644 index 0000000000000..1ee152533a75b --- /dev/null +++ b/apps/web/src/components/sidebar/SidebarProjectFolderHeader.tsx @@ -0,0 +1,115 @@ +import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/models"; +import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment"; +import { ChevronDownIcon, SquarePenIcon } from "lucide-react"; +import { memo } from "react"; + +import { cn } from "~/lib/utils"; +import type { SidebarProjectSnapshot } from "../../sidebarProjectGrouping"; +import { useUiStateStore } from "../../uiStateStore"; +import { resolveSidebarFolderDot, resolveThreadLastVisitedAt } from "../Sidebar.logic"; +import { ProjectFavicon } from "../ProjectFavicon"; +import { Button } from "../ui/button"; + +const FOLDER_DOT_CLASS_NAME = { + attention: "bg-warning", + failed: "bg-error", + unread: "bg-success", + working: "bg-info", +} as const; + +const FOLDER_DOT_LABEL = { + attention: ", needs you", + failed: ", failed", + unread: ", done", + working: ", working", +} as const; + +/** The folder's sidebar tree row; its thread rows name it as their parent. */ +export function sidebarFolderRowId(projectKey: string) { + return `folder:${projectKey}`; +} + +/** + * A project folder in the boring sidebar. It reads like a shelf header: a trailing + * count and chevron, with the count and a status dot only while it is closed, so a + * closed folder still says when a hidden thread needs you. + */ +export const SidebarProjectFolderHeader = memo(function SidebarProjectFolderHeader(props: { + group: SidebarProjectSnapshot; + collapsed: boolean; + hiddenThreads: readonly EnvironmentThreadShell[]; + onToggle: (group: SidebarProjectSnapshot) => void; + onNewThread: (group: SidebarProjectSnapshot) => void; + onContextMenu: (group: SidebarProjectSnapshot, position: { x: number; y: number }) => void; +}) { + const { group, collapsed, hiddenThreads } = props; + const rowId = sidebarFolderRowId(group.projectKey); + const localLastVisitedAtById = useUiStateStore((state) => state.threadLastVisitedAtById); + const dot = collapsed + ? resolveSidebarFolderDot( + hiddenThreads.map((thread) => ({ + ...thread, + lastVisitedAt: resolveThreadLastVisitedAt( + thread.lastVisitedAt, + localLastVisitedAtById[ + scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)) + ], + ), + })), + ) + : null; + return ( +
  • + + {/* Sits over the count, which hides on hover, and left of the chevron. */} + + + +
  • + ); +}); diff --git a/apps/web/src/components/sidebar/SidebarThreadRow.tsx b/apps/web/src/components/sidebar/SidebarThreadRow.tsx index dff97ef21057d..eac7c8591fa1c 100644 --- a/apps/web/src/components/sidebar/SidebarThreadRow.tsx +++ b/apps/web/src/components/sidebar/SidebarThreadRow.tsx @@ -515,9 +515,26 @@ const dropVerbBadge: Record = { ), }; +const TOP_STATUS_ICONS = { + working: CircleDashedIcon, + waiting: CircleDotDashedIcon, + input: MessageCircleQuestionIcon, + request: UserRoundIcon, + approval: ShieldQuestionIcon, + failed: CircleAlertIcon, + done: CircleCheckIcon, + woke: AlarmClockIcon, +} as const; + +function TopStatusIcon(props: { icon: keyof typeof TOP_STATUS_ICONS; className: string }) { + const Icon = TOP_STATUS_ICONS[props.icon]; + return ; +} + type SidebarThreadRowProps = { thread: SidebarThreadSummary; - variant: "card" | "slim"; + // "folder" is a one-line row inside a boring sidebar project folder. + variant: "card" | "slim" | "folder"; navigationParent?: string | undefined; variantAction: SidebarSweepAction; settlementSupported: boolean; @@ -997,7 +1014,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowP className={cn( "min-w-0 flex-1 text-start text-sm transition-opacity motion-reduce:transition-none", shouldRecede ? "font-normal" : "font-medium", - variant === "card" + variant !== "slim" ? cn( "truncate", shouldRecede @@ -1104,7 +1121,8 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowP ) ) : null; - if (variant === "slim") { + if (variant !== "card") { + const inFolder = variant === "folder"; return (
  • {accessibleTitle} - - {props.project ? : null} - + {inFolder ? null : ( + + {props.project ? ( + + ) : null} + + )} {draftIndicator} {title} {pinIndicator} @@ -1176,7 +1203,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowP {props.snoozeWakeLabelText} - ) : isWoke ? ( + ) : (inFolder ? isWokeStatus : isWoke) ? ( Dismiss Woke notification + ) : inFolder && topStatus ? ( + // Folder rows have no status line, so the status takes the time slot. + + + {topStatus.label} + ) : ( - {topStatus.icon === "working" ? ( - - ) : topStatus.icon === "waiting" ? ( - - ) : topStatus.icon === "input" ? ( - - ) : topStatus.icon === "request" ? ( - - ) : topStatus.icon === "approval" ? ( - - ) : topStatus.icon === "failed" ? ( - - ) : topStatus.icon === "done" ? ( - - ) : null} + {/* Not a live region: a collapsed sidebar is inert, so BackgroundThreadAnnouncer and the open thread's timeline speak status changes instead. */} diff --git a/apps/web/src/components/ui/sidebar.tsx b/apps/web/src/components/ui/sidebar.tsx index b4246f2cfabf0..6060a867fed7a 100644 --- a/apps/web/src/components/ui/sidebar.tsx +++ b/apps/web/src/components/ui/sidebar.tsx @@ -97,7 +97,7 @@ function useSidebarVisibility() { return isMobile ? openMobile : open; } -// Same breakpoint as useIsMobile, also read when a shortcut fires. +// The `max-md` breakpoint, also read when a shortcut fires. const SIDEBAR_MOBILE_QUERY = "(max-width: 767px)"; function isMobileViewport(): boolean { diff --git a/apps/web/src/connection/desktopLocal.ts b/apps/web/src/connection/desktopLocal.ts index 746344759a76e..eb46a09ab3ac6 100644 --- a/apps/web/src/connection/desktopLocal.ts +++ b/apps/web/src/connection/desktopLocal.ts @@ -26,10 +26,6 @@ export function desktopLocalBackendId(target: ConnectionTarget): string | null { : null; } -export function isWslConnectionTarget(target: ConnectionTarget): boolean { - return desktopLocalBackendId(target)?.startsWith("wsl:") === true; -} - export type DesktopSecondaryBootstrapsRead = | { readonly _tag: "Success"; diff --git a/apps/web/src/hooks/useMediaQuery.ts b/apps/web/src/hooks/useMediaQuery.ts index ec6ee4fa51160..f3df1b3a4b1de 100644 --- a/apps/web/src/hooks/useMediaQuery.ts +++ b/apps/web/src/hooks/useMediaQuery.ts @@ -77,7 +77,3 @@ export function useMediaQuery(query: BreakpointQuery | MediaQueryInput | (string return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot); } - -export function useIsMobile(): boolean { - return useMediaQuery("max-md"); -} diff --git a/apps/web/src/hooks/useSettings.ts b/apps/web/src/hooks/useSettings.ts index 70f8ac321f54f..87733457a417b 100644 --- a/apps/web/src/hooks/useSettings.ts +++ b/apps/web/src/hooks/useSettings.ts @@ -415,11 +415,11 @@ export function useEnvironmentIdentificationMode(): EnvironmentIdentificationMod }); } -/** Wait for hydration: swapping sidebar implementations after mount remounts the whole tree. */ -export function useLegacySidebarEnabled(): boolean { - const settingsHydrated = useClientSettingsHydrated(); - const legacySidebarEnabled = useClientSettings((settings) => settings.legacySidebarEnabled); - return settingsHydrated && legacySidebarEnabled; +/** Legacy sidebar opt-ins still count, so those users land in the boring sidebar. */ +export function useBoringSidebarEnabled(): boolean { + return useClientSettings( + (settings) => settings.boringSidebarEnabled || settings.legacySidebarEnabled, + ); } export async function prepareModelPreferencesPatch( diff --git a/apps/web/src/shortcuts/chatRouteCommands.ts b/apps/web/src/shortcuts/chatRouteCommands.ts index e832ca295284e..f0f80dfed8830 100644 --- a/apps/web/src/shortcuts/chatRouteCommands.ts +++ b/apps/web/src/shortcuts/chatRouteCommands.ts @@ -117,8 +117,10 @@ export function installChatRouteCommands(router: AnyRouter): () => void { void scratch.startScratchThread(environmentId); }, "chat.new": (event) => { - // Pre-hydration settings are the defaults, where the legacy sidebar is off. - if (!getClientSettings().legacySidebarEnabled && readProjectGroupCount() > 1) { + // The boring sidebar's folders already show the context, so it starts in + // the current project like the legacy sidebar did. + const { boringSidebarEnabled, legacySidebarEnabled } = getClientSettings(); + if (!boringSidebarEnabled && !legacySidebarEnabled && readProjectGroupCount() > 1) { claim(event); openCommandPalette({ open: "new-thread-in" }); return; diff --git a/apps/web/src/state/use-orchestration-command.ts b/apps/web/src/state/use-orchestration-command.ts index 1c801abc3c6b7..98f7efeecbfe6 100644 --- a/apps/web/src/state/use-orchestration-command.ts +++ b/apps/web/src/state/use-orchestration-command.ts @@ -14,8 +14,7 @@ import { AsyncResult } from "effect/reactivity"; import { readEnvironmentScope } from "./session"; import { useAtomCommand } from "./use-atom-command"; -export const ORCHESTRATION_OPERATE_DENIED_MESSAGE = - "This connection cannot change threads or projects."; +const ORCHESTRATION_OPERATE_DENIED_MESSAGE = "This connection cannot change threads or projects."; export function useOrchestrationCommand( command: AtomCommand, diff --git a/docs/user/thread-sidebar.md b/docs/user/thread-sidebar.md index ebab04125a3bf..b5373749a4956 100644 --- a/docs/user/thread-sidebar.md +++ b/docs/user/thread-sidebar.md @@ -139,6 +139,15 @@ choice. While this is on, the active list is ordered by when each thread last came back to you, so you cannot drag or move threads within it. Your saved order returns when you turn it off. +### Group threads by project (beta) + +Turn on **Settings → Labs → Boring sidebar (group by project)** on web and desktop to put active +threads in a folder for each project. Click a folder to open or close it. A closed folder shows how +many threads it holds, and a dot when one of them needs you. Use the pen button on a folder, or +right-click it, to start a thread in that project. Pinned threads stay above the folders, and +Snoozed and Settled stay below them. In this mode you cannot drag threads, and the Working section +is off. Each device keeps its own choice. + ## Settle finished work Choose **Settle thread** from its menu to move finished work out of the active list diff --git a/packages/client-runtime/src/state/projectGrouping.ts b/packages/client-runtime/src/state/projectGrouping.ts index 6866c02b08cfd..a23fd4a79e0bc 100644 --- a/packages/client-runtime/src/state/projectGrouping.ts +++ b/packages/client-runtime/src/state/projectGrouping.ts @@ -78,7 +78,7 @@ export function derivePhysicalProjectKey( return derivePhysicalProjectKeyFromPath(project.environmentId, project.workspaceRoot); } -export function deriveProjectGroupingOverrideKey( +function deriveProjectGroupingOverrideKey( project: Pick, ): string { return derivePhysicalProjectKey(project); diff --git a/packages/contracts/src/settings.ts b/packages/contracts/src/settings.ts index a21013004c6cb..8fc9a46116e78 100644 --- a/packages/contracts/src/settings.ts +++ b/packages/contracts/src/settings.ts @@ -72,16 +72,6 @@ export const SidebarProjectGroupingMode = Schema.Literals([ ]); export type SidebarProjectGroupingMode = typeof SidebarProjectGroupingMode.Type; const DEFAULT_SIDEBAR_PROJECT_GROUPING_MODE: SidebarProjectGroupingMode = "repository"; -export const MIN_SIDEBAR_THREAD_PREVIEW_COUNT = 1; -export const MAX_SIDEBAR_THREAD_PREVIEW_COUNT = 15; -export const SidebarThreadPreviewCount = Schema.Int.check( - Schema.isBetween({ - minimum: MIN_SIDEBAR_THREAD_PREVIEW_COUNT, - maximum: MAX_SIDEBAR_THREAD_PREVIEW_COUNT, - }), -); -export type SidebarThreadPreviewCount = typeof SidebarThreadPreviewCount.Type; -const DEFAULT_SIDEBAR_THREAD_PREVIEW_COUNT: SidebarThreadPreviewCount = 6; export const MIN_SIDEBAR_AUTO_SETTLE_AFTER_DAYS = 1; export const MAX_SIDEBAR_AUTO_SETTLE_AFTER_DAYS = 90; export const SidebarAutoSettleAfterDays = Schema.Number.check( @@ -479,6 +469,9 @@ export const ClientSettingsSchema = Schema.Struct({ ), proactivePanelsEnabled: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))), showSkillsInSlashMenu: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(true))), + boringSidebarEnabled: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))), + // Opt-in for the removed legacy sidebar. Still read so those users land in + // the boring sidebar; the boring sidebar toggle clears it. legacySidebarEnabled: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))), sidebarWorkingShelfEnabled: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(true))), sidebarProjectGroupingMode: SidebarProjectGroupingMode.pipe( @@ -494,9 +487,6 @@ export const ClientSettingsSchema = Schema.Struct({ sidebarThreadSortOrder: SidebarThreadSortOrder.pipe( Schema.withDecodingDefault(Effect.succeed(DEFAULT_SIDEBAR_THREAD_SORT_ORDER)), ), - sidebarThreadPreviewCount: SidebarThreadPreviewCount.pipe( - Schema.withDecodingDefault(Effect.succeed(DEFAULT_SIDEBAR_THREAD_PREVIEW_COUNT)), - ), timestampFormat: TimestampFormat.pipe( Schema.withDecodingDefault(Effect.succeed(DEFAULT_TIMESTAMP_FORMAT)), ), @@ -1712,6 +1702,7 @@ export const ClientSettingsPatch = Schema.Struct({ followUpBehavior: Schema.optionalKey(Schema.Literals(["queue", "steer"])), proactivePanelsEnabled: Schema.optionalKey(Schema.Boolean), showSkillsInSlashMenu: Schema.optionalKey(Schema.Boolean), + boringSidebarEnabled: Schema.optionalKey(Schema.Boolean), legacySidebarEnabled: Schema.optionalKey(Schema.Boolean), sidebarWorkingShelfEnabled: Schema.optionalKey(Schema.Boolean), sidebarProjectGroupingMode: Schema.optionalKey(SidebarProjectGroupingMode), @@ -1720,7 +1711,6 @@ export const ClientSettingsPatch = Schema.Struct({ ), sidebarProjectSortOrder: Schema.optionalKey(SidebarProjectSortOrder), sidebarThreadSortOrder: Schema.optionalKey(SidebarThreadSortOrder), - sidebarThreadPreviewCount: Schema.optionalKey(SidebarThreadPreviewCount), timestampFormat: Schema.optionalKey(TimestampFormat), snapShotEnabled: Schema.optionalKey(Schema.Boolean), snapShotIncludeAccessibility: Schema.optionalKey(Schema.Boolean), diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0adcb65c50062..5889ab5607c79 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1033,9 +1033,6 @@ importers: '@fontsource-variable/inter': specifier: ^5.3.0 version: 5.3.0 - '@formkit/auto-animate': - specifier: ^0.9.0 - version: 0.9.0 '@legendapp/list': specifier: 3.3.5 version: 3.3.5(patch_hash=6cbdacd8c380d9a5afb9f73d4198dc2cc44be6ef28a955b1183c781469307896)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0) @@ -4311,9 +4308,6 @@ packages: '@fontsource-variable/inter@5.3.0': resolution: {integrity: sha512-OupL48va4JNofb97w6NYeF9S7W/kHNKM0Er8Dem5nqi4jeOLrVJDoE8tZEpnMJmtkvNbB1EIPPwHcdkF6b1oUA==} - '@formkit/auto-animate@0.9.0': - resolution: {integrity: sha512-VhP4zEAacXS3dfTpJpJ88QdLqMTcabMg0jwpOSxZ/VzfQVfl3GkZSCZThhGC5uhq/TxPHPzW0dzr4H9Bb1OgKA==} - '@hono/node-server@1.19.17': resolution: {integrity: sha512-dSneS5qhiauZWGDCeK4o695Xd9nUNjviSZCMQrj10eetr8Uln1ucn6bbphOM6UynAMMtNIzZNSpL9vnASJwrPQ==} engines: {node: '>=18.14.1'} @@ -14427,8 +14421,6 @@ snapshots: '@fontsource-variable/inter@5.3.0': {} - '@formkit/auto-animate@0.9.0': {} - '@hono/node-server@1.19.17(hono@4.13.7)': dependencies: hono: 4.13.7 From 9ebca5f71cbcf050e94732f0a1da041073223c84 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 28 Oct 2026 00:13:53 -0700 Subject: [PATCH 38/46] fix(relay): tunnel limit counts abandoned reprovisions and frees failed revokes (#26320) Co-authored-by: Claude Opus 5.5 --- .../migration.sql | 12 + .../snapshot.json | 1571 +++++++++++++++++ infra/relay/package.json | 1 + .../ManagedEndpointAllocations.test.ts | 27 + .../ManagedEndpointAllocations.ts | 9 +- .../ManagedEndpointProvider.test.ts | 46 + .../environments/ManagedEndpointProvider.ts | 26 + .../environments/ManagedTunnelLimits.test.ts | 204 ++- .../src/environments/ManagedTunnelLimits.ts | 96 +- pnpm-lock.yaml | 6 +- 10 files changed, 1931 insertions(+), 67 deletions(-) create mode 100644 infra/relay/migrations/postgres/20261028062605_backfill_withdrawn_tunnel_allocations/migration.sql create mode 100644 infra/relay/migrations/postgres/20261028062605_backfill_withdrawn_tunnel_allocations/snapshot.json diff --git a/infra/relay/migrations/postgres/20261028062605_backfill_withdrawn_tunnel_allocations/migration.sql b/infra/relay/migrations/postgres/20261028062605_backfill_withdrawn_tunnel_allocations/migration.sql new file mode 100644 index 0000000000000..4c822889bd98e --- /dev/null +++ b/infra/relay/migrations/postgres/20261028062605_backfill_withdrawn_tunnel_allocations/migration.sql @@ -0,0 +1,12 @@ +-- Teardown claims used to bump updated_at past the link's revoke or publish-only write, so a +-- failed deprovision now reads as an unfinished relink. Give those rows the updated_at the +-- current write rule would have left. Touches only withdrawn links' live tunnels; idempotent. +UPDATE "relay_managed_endpoint_allocations" AS a +SET "updated_at" = l."updated_at" +FROM "relay_environment_links" AS l +WHERE l."user_id" = a."user_id" + AND l."environment_id" = a."environment_id" + AND a."tunnel_id" IS NOT NULL + AND a."tunnel_released_at" IS NULL + AND (l."revoked_at" IS NOT NULL OR l."managed_tunnels_enabled" = false) + AND l."updated_at" < a."updated_at"; diff --git a/infra/relay/migrations/postgres/20261028062605_backfill_withdrawn_tunnel_allocations/snapshot.json b/infra/relay/migrations/postgres/20261028062605_backfill_withdrawn_tunnel_allocations/snapshot.json new file mode 100644 index 0000000000000..e02f3a0c6f2e7 --- /dev/null +++ b/infra/relay/migrations/postgres/20261028062605_backfill_withdrawn_tunnel_allocations/snapshot.json @@ -0,0 +1,1571 @@ +{ + "id": "4413755a-63f1-4f93-bdc9-d1357283c23e", + "prevIds": ["7d8c1702-6f47-4453-a8ee-66ce5e3bfb41"], + "version": "8", + "dialect": "postgres", + "ddl": [ + { + "isRlsEnabled": false, + "name": "relay_agent_activity_rows", + "entityType": "tables", + "schema": "public" + }, + { + "isRlsEnabled": false, + "name": "relay_delivery_attempts", + "entityType": "tables", + "schema": "public" + }, + { + "isRlsEnabled": false, + "name": "relay_dpop_proofs", + "entityType": "tables", + "schema": "public" + }, + { + "isRlsEnabled": false, + "name": "relay_environment_credentials", + "entityType": "tables", + "schema": "public" + }, + { + "isRlsEnabled": false, + "name": "relay_environment_links", + "entityType": "tables", + "schema": "public" + }, + { + "isRlsEnabled": false, + "name": "relay_live_activities", + "entityType": "tables", + "schema": "public" + }, + { + "isRlsEnabled": false, + "name": "relay_managed_endpoint_allocations", + "entityType": "tables", + "schema": "public" + }, + { + "isRlsEnabled": false, + "name": "relay_managed_tunnel_limits", + "entityType": "tables", + "schema": "public" + }, + { + "isRlsEnabled": false, + "name": "relay_mobile_devices", + "entityType": "tables", + "schema": "public" + }, + { + "isRlsEnabled": false, + "name": "relay_upstream_api_pauses", + "entityType": "tables", + "schema": "public" + }, + { + "type": "varchar(191)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "environment_id", + "entityType": "columns", + "schema": "public", + "table": "relay_agent_activity_rows" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "environment_public_key", + "entityType": "columns", + "schema": "public", + "table": "relay_agent_activity_rows" + }, + { + "type": "varchar(512)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "thread_id", + "entityType": "columns", + "schema": "public", + "table": "relay_agent_activity_rows" + }, + { + "type": "jsonb", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "state_json", + "entityType": "columns", + "schema": "public", + "table": "relay_agent_activity_rows" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "updated_at", + "entityType": "columns", + "schema": "public", + "table": "relay_agent_activity_rows" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "created_at", + "entityType": "columns", + "schema": "public", + "table": "relay_agent_activity_rows" + }, + { + "type": "varchar(36)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "id", + "entityType": "columns", + "schema": "public", + "table": "relay_delivery_attempts" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "created_at", + "entityType": "columns", + "schema": "public", + "table": "relay_delivery_attempts" + }, + { + "type": "varchar(255)", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "user_id", + "entityType": "columns", + "schema": "public", + "table": "relay_delivery_attempts" + }, + { + "type": "varchar(191)", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "environment_id", + "entityType": "columns", + "schema": "public", + "table": "relay_delivery_attempts" + }, + { + "type": "varchar(512)", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "thread_id", + "entityType": "columns", + "schema": "public", + "table": "relay_delivery_attempts" + }, + { + "type": "varchar(255)", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "device_id", + "entityType": "columns", + "schema": "public", + "table": "relay_delivery_attempts" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "kind", + "entityType": "columns", + "schema": "public", + "table": "relay_delivery_attempts" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "source_job_id", + "entityType": "columns", + "schema": "public", + "table": "relay_delivery_attempts" + }, + { + "type": "varchar(16)", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "token_suffix", + "entityType": "columns", + "schema": "public", + "table": "relay_delivery_attempts" + }, + { + "type": "integer", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "apns_status", + "entityType": "columns", + "schema": "public", + "table": "relay_delivery_attempts" + }, + { + "type": "text", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "apns_reason", + "entityType": "columns", + "schema": "public", + "table": "relay_delivery_attempts" + }, + { + "type": "varchar(128)", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "apns_id", + "entityType": "columns", + "schema": "public", + "table": "relay_delivery_attempts" + }, + { + "type": "text", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "transport_error", + "entityType": "columns", + "schema": "public", + "table": "relay_delivery_attempts" + }, + { + "type": "varchar(128)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "thumbprint", + "entityType": "columns", + "schema": "public", + "table": "relay_dpop_proofs" + }, + { + "type": "varchar(255)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "jti", + "entityType": "columns", + "schema": "public", + "table": "relay_dpop_proofs" + }, + { + "type": "integer", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "iat", + "entityType": "columns", + "schema": "public", + "table": "relay_dpop_proofs" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "expires_at", + "entityType": "columns", + "schema": "public", + "table": "relay_dpop_proofs" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "created_at", + "entityType": "columns", + "schema": "public", + "table": "relay_dpop_proofs" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "credential_id", + "entityType": "columns", + "schema": "public", + "table": "relay_environment_credentials" + }, + { + "type": "varchar(191)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "environment_id", + "entityType": "columns", + "schema": "public", + "table": "relay_environment_credentials" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "environment_public_key", + "entityType": "columns", + "schema": "public", + "table": "relay_environment_credentials" + }, + { + "type": "varchar(191)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "credential_hash", + "entityType": "columns", + "schema": "public", + "table": "relay_environment_credentials" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "revoked_at", + "entityType": "columns", + "schema": "public", + "table": "relay_environment_credentials" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "created_at", + "entityType": "columns", + "schema": "public", + "table": "relay_environment_credentials" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "updated_at", + "entityType": "columns", + "schema": "public", + "table": "relay_environment_credentials" + }, + { + "type": "varchar(191)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "user_id", + "entityType": "columns", + "schema": "public", + "table": "relay_environment_links" + }, + { + "type": "varchar(191)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "environment_id", + "entityType": "columns", + "schema": "public", + "table": "relay_environment_links" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "'T3 Environment'", + "generated": null, + "identity": null, + "name": "environment_label", + "entityType": "columns", + "schema": "public", + "table": "relay_environment_links" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "environment_public_key", + "entityType": "columns", + "schema": "public", + "table": "relay_environment_links" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "endpoint_http_base_url", + "entityType": "columns", + "schema": "public", + "table": "relay_environment_links" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "endpoint_ws_base_url", + "entityType": "columns", + "schema": "public", + "table": "relay_environment_links" + }, + { + "type": "varchar(32)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "endpoint_provider_kind", + "entityType": "columns", + "schema": "public", + "table": "relay_environment_links" + }, + { + "type": "boolean", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "true", + "generated": null, + "identity": null, + "name": "notifications_enabled", + "entityType": "columns", + "schema": "public", + "table": "relay_environment_links" + }, + { + "type": "boolean", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "true", + "generated": null, + "identity": null, + "name": "live_activities_enabled", + "entityType": "columns", + "schema": "public", + "table": "relay_environment_links" + }, + { + "type": "boolean", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "false", + "generated": null, + "identity": null, + "name": "managed_tunnels_enabled", + "entityType": "columns", + "schema": "public", + "table": "relay_environment_links" + }, + { + "type": "boolean", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "false", + "generated": null, + "identity": null, + "name": "hold_webhooks_while_offline", + "entityType": "columns", + "schema": "public", + "table": "relay_environment_links" + }, + { + "type": "varchar(191)", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "created_by_device_id", + "entityType": "columns", + "schema": "public", + "table": "relay_environment_links" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "revoked_at", + "entityType": "columns", + "schema": "public", + "table": "relay_environment_links" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "created_at", + "entityType": "columns", + "schema": "public", + "table": "relay_environment_links" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "updated_at", + "entityType": "columns", + "schema": "public", + "table": "relay_environment_links" + }, + { + "type": "varchar(255)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "user_id", + "entityType": "columns", + "schema": "public", + "table": "relay_live_activities" + }, + { + "type": "varchar(255)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "device_id", + "entityType": "columns", + "schema": "public", + "table": "relay_live_activities" + }, + { + "type": "text", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "activity_push_token", + "entityType": "columns", + "schema": "public", + "table": "relay_live_activities" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "remote_start_queued_at", + "entityType": "columns", + "schema": "public", + "table": "relay_live_activities" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "remote_started_at", + "entityType": "columns", + "schema": "public", + "table": "relay_live_activities" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "ended_at", + "entityType": "columns", + "schema": "public", + "table": "relay_live_activities" + }, + { + "type": "jsonb", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "last_aggregate_json", + "entityType": "columns", + "schema": "public", + "table": "relay_live_activities" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "last_live_activity_delivery_at", + "entityType": "columns", + "schema": "public", + "table": "relay_live_activities" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "created_at", + "entityType": "columns", + "schema": "public", + "table": "relay_live_activities" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "updated_at", + "entityType": "columns", + "schema": "public", + "table": "relay_live_activities" + }, + { + "type": "varchar(191)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "user_id", + "entityType": "columns", + "schema": "public", + "table": "relay_managed_endpoint_allocations" + }, + { + "type": "varchar(191)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "environment_id", + "entityType": "columns", + "schema": "public", + "table": "relay_managed_endpoint_allocations" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "hostname", + "entityType": "columns", + "schema": "public", + "table": "relay_managed_endpoint_allocations" + }, + { + "type": "varchar(191)", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "tunnel_id", + "entityType": "columns", + "schema": "public", + "table": "relay_managed_endpoint_allocations" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "tunnel_name", + "entityType": "columns", + "schema": "public", + "table": "relay_managed_endpoint_allocations" + }, + { + "type": "varchar(191)", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "dns_record_id", + "entityType": "columns", + "schema": "public", + "table": "relay_managed_endpoint_allocations" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "ready_at", + "entityType": "columns", + "schema": "public", + "table": "relay_managed_endpoint_allocations" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "recovery_enabled_at", + "entityType": "columns", + "schema": "public", + "table": "relay_managed_endpoint_allocations" + }, + { + "type": "text", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "recovery_environment_public_key", + "entityType": "columns", + "schema": "public", + "table": "relay_managed_endpoint_allocations" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "tunnel_released_at", + "entityType": "columns", + "schema": "public", + "table": "relay_managed_endpoint_allocations" + }, + { + "type": "jsonb", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "origin", + "entityType": "columns", + "schema": "public", + "table": "relay_managed_endpoint_allocations" + }, + { + "type": "integer", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "0", + "generated": null, + "identity": null, + "name": "generation", + "entityType": "columns", + "schema": "public", + "table": "relay_managed_endpoint_allocations" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "created_at", + "entityType": "columns", + "schema": "public", + "table": "relay_managed_endpoint_allocations" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "updated_at", + "entityType": "columns", + "schema": "public", + "table": "relay_managed_endpoint_allocations" + }, + { + "type": "varchar(191)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "user_id", + "entityType": "columns", + "schema": "public", + "table": "relay_managed_tunnel_limits" + }, + { + "type": "integer", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "max_tunnels", + "entityType": "columns", + "schema": "public", + "table": "relay_managed_tunnel_limits" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "created_at", + "entityType": "columns", + "schema": "public", + "table": "relay_managed_tunnel_limits" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "updated_at", + "entityType": "columns", + "schema": "public", + "table": "relay_managed_tunnel_limits" + }, + { + "type": "varchar(255)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "user_id", + "entityType": "columns", + "schema": "public", + "table": "relay_mobile_devices" + }, + { + "type": "varchar(255)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "device_id", + "entityType": "columns", + "schema": "public", + "table": "relay_mobile_devices" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "'iOS device'", + "generated": null, + "identity": null, + "name": "label", + "entityType": "columns", + "schema": "public", + "table": "relay_mobile_devices" + }, + { + "type": "varchar(16)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "platform", + "entityType": "columns", + "schema": "public", + "table": "relay_mobile_devices" + }, + { + "type": "integer", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "ios_major_version", + "entityType": "columns", + "schema": "public", + "table": "relay_mobile_devices" + }, + { + "type": "integer", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "android_api_level", + "entityType": "columns", + "schema": "public", + "table": "relay_mobile_devices" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "app_version", + "entityType": "columns", + "schema": "public", + "table": "relay_mobile_devices" + }, + { + "type": "varchar(255)", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "bundle_id", + "entityType": "columns", + "schema": "public", + "table": "relay_mobile_devices" + }, + { + "type": "varchar(16)", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "aps_environment", + "entityType": "columns", + "schema": "public", + "table": "relay_mobile_devices" + }, + { + "type": "text", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "push_token", + "entityType": "columns", + "schema": "public", + "table": "relay_mobile_devices" + }, + { + "type": "text", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "push_to_start_token", + "entityType": "columns", + "schema": "public", + "table": "relay_mobile_devices" + }, + { + "type": "jsonb", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "preferences_json", + "entityType": "columns", + "schema": "public", + "table": "relay_mobile_devices" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "created_at", + "entityType": "columns", + "schema": "public", + "table": "relay_mobile_devices" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "updated_at", + "entityType": "columns", + "schema": "public", + "table": "relay_mobile_devices" + }, + { + "type": "varchar(64)", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "api", + "entityType": "columns", + "schema": "public", + "table": "relay_upstream_api_pauses" + }, + { + "type": "bigint", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "paused_until_ms", + "entityType": "columns", + "schema": "public", + "table": "relay_upstream_api_pauses" + }, + { + "nameExplicit": true, + "columns": [ + { + "value": "created_at", + "isExpression": false, + "asc": true, + "nullsFirst": false, + "opclass": null + } + ], + "isUnique": false, + "where": null, + "with": "", + "method": "btree", + "concurrently": false, + "name": "idx_relay_delivery_attempts_created", + "entityType": "indexes", + "schema": "public", + "table": "relay_delivery_attempts" + }, + { + "nameExplicit": true, + "columns": [ + { + "value": "source_job_id", + "isExpression": false, + "asc": true, + "nullsFirst": false, + "opclass": null + } + ], + "isUnique": true, + "where": null, + "with": "", + "method": "btree", + "concurrently": false, + "name": "idx_relay_delivery_attempts_source_job", + "entityType": "indexes", + "schema": "public", + "table": "relay_delivery_attempts" + }, + { + "nameExplicit": true, + "columns": [ + { + "value": "expires_at", + "isExpression": false, + "asc": true, + "nullsFirst": false, + "opclass": null + } + ], + "isUnique": false, + "where": null, + "with": "", + "method": "btree", + "concurrently": false, + "name": "idx_relay_dpop_proofs_expires_at", + "entityType": "indexes", + "schema": "public", + "table": "relay_dpop_proofs" + }, + { + "nameExplicit": true, + "columns": [ + { + "value": "credential_hash", + "isExpression": false, + "asc": true, + "nullsFirst": false, + "opclass": null + } + ], + "isUnique": true, + "where": null, + "with": "", + "method": "btree", + "concurrently": false, + "name": "idx_relay_environment_credentials_hash", + "entityType": "indexes", + "schema": "public", + "table": "relay_environment_credentials" + }, + { + "nameExplicit": true, + "columns": [ + { + "value": "environment_id", + "isExpression": false, + "asc": true, + "nullsFirst": false, + "opclass": null + }, + { + "value": "environment_public_key", + "isExpression": false, + "asc": true, + "nullsFirst": false, + "opclass": null + }, + { + "value": "revoked_at", + "isExpression": false, + "asc": true, + "nullsFirst": false, + "opclass": null + } + ], + "isUnique": false, + "where": null, + "with": "", + "method": "btree", + "concurrently": false, + "name": "idx_relay_environment_credentials_environment_key", + "entityType": "indexes", + "schema": "public", + "table": "relay_environment_credentials" + }, + { + "nameExplicit": true, + "columns": [ + { + "value": "environment_id", + "isExpression": false, + "asc": true, + "nullsFirst": false, + "opclass": null + }, + { + "value": "revoked_at", + "isExpression": false, + "asc": true, + "nullsFirst": false, + "opclass": null + } + ], + "isUnique": false, + "where": null, + "with": "", + "method": "btree", + "concurrently": false, + "name": "idx_relay_environment_links_environment", + "entityType": "indexes", + "schema": "public", + "table": "relay_environment_links" + }, + { + "nameExplicit": true, + "columns": [ + { + "value": "activity_push_token", + "isExpression": false, + "asc": true, + "nullsFirst": false, + "opclass": null + } + ], + "isUnique": true, + "where": null, + "with": "", + "method": "btree", + "concurrently": false, + "name": "idx_relay_live_activities_activity_push_token", + "entityType": "indexes", + "schema": "public", + "table": "relay_live_activities" + }, + { + "nameExplicit": true, + "columns": [ + { + "value": "hostname", + "isExpression": false, + "asc": true, + "nullsFirst": false, + "opclass": null + } + ], + "isUnique": true, + "where": null, + "with": "", + "method": "btree", + "concurrently": false, + "name": "idx_relay_managed_endpoint_allocations_hostname", + "entityType": "indexes", + "schema": "public", + "table": "relay_managed_endpoint_allocations" + }, + { + "nameExplicit": true, + "columns": [ + { + "value": "tunnel_name", + "isExpression": false, + "asc": true, + "nullsFirst": false, + "opclass": null + } + ], + "isUnique": true, + "where": null, + "with": "", + "method": "btree", + "concurrently": false, + "name": "idx_relay_managed_endpoint_allocations_tunnel_name", + "entityType": "indexes", + "schema": "public", + "table": "relay_managed_endpoint_allocations" + }, + { + "nameExplicit": true, + "columns": [ + { + "value": "push_token", + "isExpression": false, + "asc": true, + "nullsFirst": false, + "opclass": null + } + ], + "isUnique": true, + "where": null, + "with": "", + "method": "btree", + "concurrently": false, + "name": "idx_relay_mobile_devices_push_token", + "entityType": "indexes", + "schema": "public", + "table": "relay_mobile_devices" + }, + { + "nameExplicit": true, + "columns": [ + { + "value": "push_to_start_token", + "isExpression": false, + "asc": true, + "nullsFirst": false, + "opclass": null + } + ], + "isUnique": true, + "where": null, + "with": "", + "method": "btree", + "concurrently": false, + "name": "idx_relay_mobile_devices_push_to_start_token", + "entityType": "indexes", + "schema": "public", + "table": "relay_mobile_devices" + }, + { + "columns": ["environment_id", "environment_public_key", "thread_id"], + "nameExplicit": false, + "name": "relay_agent_activity_rows_pkey", + "entityType": "pks", + "schema": "public", + "table": "relay_agent_activity_rows" + }, + { + "columns": ["thumbprint", "jti"], + "nameExplicit": false, + "name": "relay_dpop_proofs_pkey", + "entityType": "pks", + "schema": "public", + "table": "relay_dpop_proofs" + }, + { + "columns": ["user_id", "environment_id"], + "nameExplicit": false, + "name": "relay_environment_links_pkey", + "entityType": "pks", + "schema": "public", + "table": "relay_environment_links" + }, + { + "columns": ["user_id", "device_id"], + "nameExplicit": false, + "name": "relay_live_activities_pkey", + "entityType": "pks", + "schema": "public", + "table": "relay_live_activities" + }, + { + "columns": ["user_id", "environment_id"], + "nameExplicit": false, + "name": "relay_managed_endpoint_allocations_pkey", + "entityType": "pks", + "schema": "public", + "table": "relay_managed_endpoint_allocations" + }, + { + "columns": ["user_id", "device_id"], + "nameExplicit": false, + "name": "relay_mobile_devices_pkey", + "entityType": "pks", + "schema": "public", + "table": "relay_mobile_devices" + }, + { + "columns": ["id"], + "nameExplicit": false, + "name": "relay_delivery_attempts_pkey", + "schema": "public", + "table": "relay_delivery_attempts", + "entityType": "pks" + }, + { + "columns": ["credential_id"], + "nameExplicit": false, + "name": "relay_environment_credentials_pkey", + "schema": "public", + "table": "relay_environment_credentials", + "entityType": "pks" + }, + { + "columns": ["user_id"], + "nameExplicit": false, + "name": "relay_managed_tunnel_limits_pkey", + "schema": "public", + "table": "relay_managed_tunnel_limits", + "entityType": "pks" + }, + { + "columns": ["api"], + "nameExplicit": false, + "name": "relay_upstream_api_pauses_pkey", + "schema": "public", + "table": "relay_upstream_api_pauses", + "entityType": "pks" + } + ], + "renames": [] +} diff --git a/infra/relay/package.json b/infra/relay/package.json index c75e4b70648d2..197c37819981e 100644 --- a/infra/relay/package.json +++ b/infra/relay/package.json @@ -33,6 +33,7 @@ "@cloudflare/workers-types": "^4.20260601.1", "@effect/platform-node": "catalog:", "@effect/vitest": "catalog:", + "@electric-sql/pglite": "0.3.15", "@types/node": "catalog:", "drizzle-kit": "1.0.0-rc.5-ab785fc", "typescript": "catalog:", diff --git a/infra/relay/src/environments/ManagedEndpointAllocations.test.ts b/infra/relay/src/environments/ManagedEndpointAllocations.test.ts index 931e8a2b8893d..f0cd86bef9669 100644 --- a/infra/relay/src/environments/ManagedEndpointAllocations.test.ts +++ b/infra/relay/src/environments/ManagedEndpointAllocations.test.ts @@ -74,4 +74,31 @@ describe("ManagedEndpointAllocations", () => { ]); }).pipe(Effect.provide(layerWithDb(fakeDb))); }); + + it.effect("teardown claims leave updatedAt to provisioning writes", () => { + // The tunnel limit compares link and allocation updatedAt; a claim that bumps it makes a + // failed revoke cleanup look like a reprovision and keeps holding the user's slot. + const writes: Array> = []; + const fakeDb = { + update: () => ({ + set: (values: Record) => { + writes.push(values); + return { + where: () => ({ returning: () => Effect.succeed([{ generation: 2 }]) }), + }; + }, + }), + } as unknown as RelayDb.RelayDb["Service"]; + + return Effect.gen(function* () { + const allocations = yield* ManagedEndpointAllocations.ManagedEndpointAllocations; + const key = { userId: "user-1", environmentId: "environment-1", generation: 1 }; + yield* allocations.claimDeprovision(key); + yield* allocations.claimRelease({ ...key, tunnelId: "tunnel-1" }); + yield* allocations.claimRelease({ ...key, tunnelId: "tunnel-1", markReleased: true }); + + expect(writes.map((values) => "updatedAt" in values)).toEqual([false, false, true]); + expect(writes[2]?.["updatedAt"]).toBe(writes[2]?.["tunnelReleasedAt"]); + }).pipe(Effect.provide(layerWithDb(fakeDb))); + }); }); diff --git a/infra/relay/src/environments/ManagedEndpointAllocations.ts b/infra/relay/src/environments/ManagedEndpointAllocations.ts index d432f45c61763..918c410e35176 100644 --- a/infra/relay/src/environments/ManagedEndpointAllocations.ts +++ b/infra/relay/src/environments/ManagedEndpointAllocations.ts @@ -512,9 +512,8 @@ const make = Effect.gen(function* () { const claimed = yield* db .update(relayManagedEndpointAllocations) .set({ - updatedAt: now, generation: sql`${relayManagedEndpointAllocations.generation} + 1`, - ...(input.markReleased === true ? { tunnelReleasedAt: now } : {}), + ...(input.markReleased === true ? { tunnelReleasedAt: now, updatedAt: now } : {}), }) .where( and( @@ -585,10 +584,8 @@ const make = Effect.gen(function* () { ) { const claimed = yield* db .update(relayManagedEndpointAllocations) - .set({ - updatedAt: DateTime.formatIso(yield* DateTime.now), - generation: sql`${relayManagedEndpointAllocations.generation} + 1`, - }) + // Claims move only the generation: the tunnel limit reads updatedAt as the last provisioning write. + .set({ generation: sql`${relayManagedEndpointAllocations.generation} + 1` }) .where( and( whereAllocation(input), diff --git a/infra/relay/src/environments/ManagedEndpointProvider.test.ts b/infra/relay/src/environments/ManagedEndpointProvider.test.ts index 36ea69d5a2694..01230171be9ba 100644 --- a/infra/relay/src/environments/ManagedEndpointProvider.test.ts +++ b/infra/relay/src/environments/ManagedEndpointProvider.test.ts @@ -1406,6 +1406,52 @@ describe("ManagedEndpointProvider", () => { }).pipe(Effect.provide(layer)); }); + it.effect("marks the tunnel released when removing the allocation fails after deletion", () => { + const allocationCalls: AllocationCall[] = []; + const allocations = makeAllocations(allocationCalls); + const layer = layerProvider(makePersistentTunnelClient(), makeDnsClient(), { + ...allocations, + removeClaimed: (input) => + Effect.suspend(() => { + allocationCalls.push({ operation: "removeClaimed", input }); + return Effect.fail( + new ManagedEndpointAllocations.ManagedEndpointAllocationPersistenceError({ + operation: "remove-claimed", + stage: "database-request", + userId: input.userId, + environmentId: input.environmentId, + }), + ); + }), + }); + + return Effect.gen(function* () { + const provider = yield* ManagedEndpointProvider.ManagedEndpointProvider; + const key = { userId: "user_ABC", environmentId: "env_ABC" } as const; + yield* provider.provision({ + ...key, + origin: { localHttpHost: "127.0.0.1", localHttpPort: 3773 }, + }); + const result = yield* Effect.result(provider.deprovision(key)); + + expect(result._tag).toBe("Failure"); + if (result._tag === "Failure") { + expect(result.failure).toMatchObject({ stage: "remove-allocation" }); + } + // The tunnel is gone; without the mark the tunnel limit would keep counting it. + const claim = allocationCalls.find((call) => call.operation === "claimDeprovision"); + if (claim === undefined) return expect.unreachable("deprovision never claimed the row"); + expect(allocationCalls.at(-1)).toMatchObject({ + operation: "claimRelease", + input: { + tunnelId: "tunnel-id", + generation: (claim.input as { generation: number }).generation + 1, + markReleased: true, + }, + }); + }).pipe(Effect.provide(layer)); + }); + it.effect("treats already deleted remote resources as successfully deprovisioned", () => { const allocationCalls: AllocationCall[] = []; const notFound = { _tag: "NotFound" } as const; diff --git a/infra/relay/src/environments/ManagedEndpointProvider.ts b/infra/relay/src/environments/ManagedEndpointProvider.ts index 5524b5117faaf..9a76b9c09981a 100644 --- a/infra/relay/src/environments/ManagedEndpointProvider.ts +++ b/infra/relay/src/environments/ManagedEndpointProvider.ts @@ -882,6 +882,7 @@ const make = Effect.gen(function* () { return false; } const tunnelId = allocation.tunnelId; + let tunnelDeleted = false; const deprovision = Effect.gen(function* () { const dnsRecordId = allocation.dnsRecordId; if (dnsRecordId !== null) { @@ -909,6 +910,7 @@ const make = Effect.gen(function* () { }), ), ); + tunnelDeleted = true; } return yield* allocations .removeClaimed({ @@ -956,6 +958,30 @@ const make = Effect.gen(function* () { }), ), }), + // The rollback keeps the deleted tunnel's id; mark it released so the tunnel limit stops + // counting it. onError also covers an interrupted unlink, which rolls back the same way. + Effect.onError(() => + tunnelDeleted + ? allocations + .claimRelease({ + userId: input.userId, + environmentId: input.environmentId, + tunnelId, + generation: claimedGeneration, + markReleased: true, + }) + .pipe( + Effect.catch((error) => + Effect.logWarning("Failed to mark a deleted managed tunnel released", { + userId: input.userId, + environmentId: input.environmentId, + tunnelId, + error: error.message, + }), + ), + ) + : Effect.void, + ), ); return Option.getOrElse(removed, () => false); }), diff --git a/infra/relay/src/environments/ManagedTunnelLimits.test.ts b/infra/relay/src/environments/ManagedTunnelLimits.test.ts index d45dfc0fde8fd..5837ae3f0eb52 100644 --- a/infra/relay/src/environments/ManagedTunnelLimits.test.ts +++ b/infra/relay/src/environments/ManagedTunnelLimits.test.ts @@ -1,8 +1,13 @@ import { describe, expect, it } from "@effect/vitest"; +import * as NodeServices from "@effect/platform-node/NodeServices"; import * as Context from "effect/Context"; import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Semaphore from "effect/Semaphore"; +import { PGlite } from "@electric-sql/pglite"; +import { drizzle } from "drizzle-orm/pglite"; +import { ne } from "drizzle-orm"; import { PgDialect } from "drizzle-orm/pg-core"; import type { RelayT3ChatPlan } from "@t3tools/contracts/relay"; @@ -11,6 +16,8 @@ import { relayManagedEndpointAllocations } from "../persistence/schema.ts"; import * as T3ChatAccounts from "../t3chat/T3ChatAccounts.ts"; import * as ManagedTunnelLimits from "./ManagedTunnelLimits.ts"; +const BACKFILL_MIGRATION = "20261028062605_backfill_withdrawn_tunnel_allocations"; + class FakeTransaction extends Context.Service()( "t3code-relay/environments/ManagedTunnelLimits.test/FakeTransaction", ) {} @@ -19,7 +26,6 @@ interface FakeDbInput { readonly otherSlots: () => number; readonly ownSlot?: boolean; readonly override?: number; - readonly countConditions?: Array; } // Models Postgres just enough: the advisory lock only works inside a transaction and is held until it ends. @@ -59,7 +65,6 @@ function makeFakeDb(input: FakeDbInput) { ), }; } - input.countConditions?.push(condition); const countsOthers = dialect .sqlToQuery(condition as never) .sql.includes('"relay_managed_endpoint_allocations"."environment_id" <>'); @@ -154,24 +159,191 @@ describe("ManagedTunnelLimits", () => { ); }); - it.effect("pending reservations stop counting once released or withdrawn", () => { - const countConditions: Array = []; + it.effect("counts each allocation by its tunnel and link state", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const pg = yield* Effect.acquireRelease( + Effect.promise(() => PGlite.create()), + (client) => Effect.promise(() => client.close()), + ); + const migrationsDir = new URL("../../migrations/postgres/", import.meta.url).pathname; + const migrations = (yield* fs.readDirectory(migrationsDir)).toSorted(); + const applyMigrations = (names: ReadonlyArray) => + Effect.forEach(names, (name) => + fs + .readFileString(`${migrationsDir}${name}/migration.sql`) + .pipe(Effect.flatMap((migration) => Effect.promise(() => pg.exec(migration)))), + ); + const backfill = migrations.indexOf(BACKFILL_MIGRATION); + expect(backfill).toBeGreaterThan(-1); + yield* applyMigrations(migrations.slice(0, backfill)); - return Effect.gen(function* () { - yield* reserve({ activeTunnels: 0, plan: "free", countConditions }); + const at = (minutes: number) => `2026-10-27T12:${String(minutes).padStart(2, "0")}:00.000Z`; + const cases: ReadonlyArray<{ + readonly environmentId: string; + readonly counted: boolean; + readonly allocation: { + readonly tunnelId?: string; + readonly releasedAt?: number; + readonly updatedAt: number; + }; + readonly link?: { + readonly revoked?: boolean; + readonly publishOnly?: boolean; + readonly updatedAt: number; + }; + }> = [ + { environmentId: "pending", counted: true, allocation: { updatedAt: 50 } }, + { environmentId: "abandoned-reservation", counted: false, allocation: { updatedAt: 10 } }, + { + environmentId: "live", + counted: true, + allocation: { tunnelId: "t", updatedAt: 10 }, + link: { updatedAt: 12 }, + }, + { + environmentId: "released", + counted: false, + allocation: { tunnelId: "t", releasedAt: 20, updatedAt: 20 }, + }, + { + environmentId: "re-reserved-after-release", + counted: true, + allocation: { tunnelId: "t", releasedAt: 20, updatedAt: 50 }, + }, + { + environmentId: "revoked", + counted: false, + allocation: { tunnelId: "t", updatedAt: 10 }, + link: { revoked: true, updatedAt: 20 }, + }, + { + environmentId: "revoked-pending", + counted: false, + allocation: { updatedAt: 50 }, + link: { revoked: true, updatedAt: 55 }, + }, + { + environmentId: "publish-only", + counted: false, + allocation: { tunnelId: "t", updatedAt: 10 }, + link: { publishOnly: true, updatedAt: 20 }, + }, + // Reprovisioned after a revoke, but the Worker died before rewriting the link. + { + environmentId: "reprovision-before-link", + counted: true, + allocation: { tunnelId: "t", updatedAt: 20 }, + link: { revoked: true, updatedAt: 10 }, + }, + { + environmentId: "reserved-before-link-rewrite", + counted: true, + allocation: { updatedAt: 50 }, + link: { revoked: true, updatedAt: 48 }, + }, + // Tunnel deleted but the row survived; deprovision marked it released, then a relink stalled. + { + environmentId: "released-after-failed-removal-then-reserved", + counted: false, + allocation: { tunnelId: "t", releasedAt: 20, updatedAt: 30 }, + link: { revoked: true, updatedAt: 15 }, + }, + { + environmentId: "withdrawn-at-provisioning-write", + counted: false, + allocation: { tunnelId: "t", updatedAt: 20 }, + link: { revoked: true, updatedAt: 20 }, + }, + { environmentId: "self", counted: false, allocation: { tunnelId: "t", updatedAt: 10 } }, + ]; + // Written before the deploy: claimDeprovision bumped updatedAt past the revoke, then cleanup failed. + const legacyCases: typeof cases = [ + { + environmentId: "legacy-failed-deprovision", + counted: false, + allocation: { tunnelId: "t", updatedAt: 30 }, + link: { revoked: true, updatedAt: 20 }, + }, + { + environmentId: "legacy-failed-publish-only-deprovision", + counted: false, + allocation: { tunnelId: "t", updatedAt: 30 }, + link: { publishOnly: true, updatedAt: 20 }, + }, + { + environmentId: "legacy-live", + counted: true, + allocation: { tunnelId: "t", updatedAt: 30 }, + link: { updatedAt: 20 }, + }, + ]; + const insert = (entries: typeof cases) => + Effect.forEach(entries, (entry) => + Effect.promise(async () => { + await pg.query( + `insert into relay_managed_endpoint_allocations + (user_id, environment_id, hostname, tunnel_id, tunnel_name, tunnel_released_at, created_at, updated_at) + values ('user-1', $1::text, $1::text, $2, $1::text, $3, $4, $4)`, + [ + entry.environmentId, + entry.allocation.tunnelId ?? null, + entry.allocation.releasedAt === undefined ? null : at(entry.allocation.releasedAt), + at(entry.allocation.updatedAt), + ], + ); + const link = entry.link; + if (link !== undefined) { + await pg.query( + `insert into relay_environment_links + (user_id, environment_id, environment_public_key, endpoint_http_base_url, + endpoint_ws_base_url, endpoint_provider_kind, revoked_at, managed_tunnels_enabled, + created_at, updated_at) + values ('user-1', $1, 'key', 'https://e', 'wss://e', 'cloudflare_tunnel', $2, $3, $4, $4)`, + [ + entry.environmentId, + link.revoked === true ? at(link.updatedAt) : null, + link.publishOnly !== true, + at(link.updatedAt), + ], + ); + } + }), + ); + yield* insert(legacyCases); + yield* applyMigrations(migrations.slice(backfill)); + yield* insert(cases); + yield* Effect.promise(() => + pg.query( + `insert into relay_managed_endpoint_allocations + (user_id, environment_id, hostname, tunnel_id, tunnel_name, created_at, updated_at) + values ('user-2', 'other-user', 'other-user', 't', 'other-user', $1, $1)`, + [at(10)], + ), + ); - expect(countConditions).toHaveLength(1); - const query = new PgDialect().sqlToQuery(countConditions[0] as never); - // claimRelease writes updatedAt = tunnelReleasedAt; a later reserve moves updatedAt past it. - expect(query.sql).toContain( - '"relay_managed_endpoint_allocations"."updated_at" > "relay_managed_endpoint_allocations"."tunnel_released_at"', + const counted = yield* Effect.promise(() => + drizzle({ client: pg }) + .select({ environmentId: relayManagedEndpointAllocations.environmentId }) + .from(relayManagedEndpointAllocations) + .where( + ManagedTunnelLimits.countedAllocations({ + userId: "user-1", + environmentId: "self", + pendingCutoff: at(45), + environment: ne, + }), + ), ); - // A link revoked or made publish-only after the last reservation no longer holds a slot. - expect(query.sql).toContain( - '"relay_environment_links"."updated_at" >= "relay_managed_endpoint_allocations"."updated_at"', + + expect(counted.map((row) => row.environmentId).toSorted()).toEqual( + [...legacyCases, ...cases] + .filter((entry) => entry.counted) + .map((entry) => entry.environmentId) + .toSorted(), ); - }); - }); + }).pipe(Effect.provide(NodeServices.layer)), + ); it.effect("does not ask T3 Chat while the account is under its cap", () => Effect.gen(function* () { diff --git a/infra/relay/src/environments/ManagedTunnelLimits.ts b/infra/relay/src/environments/ManagedTunnelLimits.ts index e06bd883a2351..93aea073c71d3 100644 --- a/infra/relay/src/environments/ManagedTunnelLimits.ts +++ b/infra/relay/src/environments/ManagedTunnelLimits.ts @@ -47,6 +47,58 @@ export class ManagedTunnelLimitExceeded extends Schema.TaggedError + exists( + new QueryBuilder() + .select({ userId: relayEnvironmentLinks.userId }) + .from(relayEnvironmentLinks) + .where( + and( + eq(relayEnvironmentLinks.userId, allocation.userId), + eq(relayEnvironmentLinks.environmentId, allocation.environmentId), + or( + isNotNull(relayEnvironmentLinks.revokedAt), + eq(relayEnvironmentLinks.managedTunnelsEnabled, false), + ), + gte(relayEnvironmentLinks.updatedAt, since), + ), + ), + ); + +// Counts allocations, not links: a link is written only after its tunnel exists. +// `environment` is `ne` for the user's other environments or `eq` for this one, which relinks without counting itself. +// updatedAt moves only on provisioning writes and release, never on teardown claims, so a link +// withdrawn at or after it means cleanup is owed; an older withdrawal means a relink that never finished. +export const countedAllocations = (input: { + readonly userId: string; + readonly environmentId: string; + readonly pendingCutoff: string; + readonly environment: typeof eq; +}) => + and( + eq(allocation.userId, input.userId), + input.environment(allocation.environmentId, input.environmentId), + or( + // Reserved but not yet holding a tunnel. Release writes updatedAt = tunnelReleasedAt and + // a relink reserves before rewriting its link, so only a newer reservation counts here. + and( + gt(allocation.updatedAt, input.pendingCutoff), + or( + isNull(allocation.tunnelReleasedAt), + gt(allocation.updatedAt, allocation.tunnelReleasedAt), + ), + not(withdrawnLink(allocation.updatedAt)), + ), + and( + isNotNull(allocation.tunnelId), + isNull(allocation.tunnelReleasedAt), + not(withdrawnLink(allocation.updatedAt)), + ), + ), + ); + export class ManagedTunnelLimits extends Context.Service< ManagedTunnelLimits, { @@ -119,53 +171,11 @@ const make = Effect.gen(function* () { minutes: MANAGED_TUNNEL_PENDING_WINDOW_MINUTES, }), ); - // Counts allocations, not links: a link is written only after its tunnel exists. - // Excludes this environment so relinking at the limit stays idempotent. - const allocation = relayManagedEndpointAllocations; - const withdrawnLink = (since?: typeof allocation.updatedAt) => - exists( - new QueryBuilder() - .select({ userId: relayEnvironmentLinks.userId }) - .from(relayEnvironmentLinks) - .where( - and( - eq(relayEnvironmentLinks.userId, allocation.userId), - eq(relayEnvironmentLinks.environmentId, allocation.environmentId), - or( - isNotNull(relayEnvironmentLinks.revokedAt), - eq(relayEnvironmentLinks.managedTunnelsEnabled, false), - ), - since === undefined ? undefined : gte(relayEnvironmentLinks.updatedAt, since), - ), - ), - ); const countSlots = (environment: typeof eq) => db .select({ slots: count() }) .from(allocation) - .where( - and( - eq(allocation.userId, input.userId), - environment(allocation.environmentId, input.environmentId), - or( - // Reserved but not yet holding a tunnel. Release writes updatedAt = tunnelReleasedAt and - // a relink reserves before rewriting its link, so only a newer reservation counts here. - and( - gt(allocation.updatedAt, pendingCutoff), - or( - isNull(allocation.tunnelReleasedAt), - gt(allocation.updatedAt, allocation.tunnelReleasedAt), - ), - not(withdrawnLink(allocation.updatedAt)), - ), - and( - isNotNull(allocation.tunnelId), - isNull(allocation.tunnelReleasedAt), - not(withdrawnLink()), - ), - ), - ), - ) + .where(countedAllocations({ ...input, pendingCutoff, environment })) .pipe( Effect.map((rows) => rows[0]?.slots ?? 0), Effect.mapError( diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5889ab5607c79..6bdece6bd1839 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1351,6 +1351,9 @@ importers: '@effect/vitest': specifier: 4.0.5 version: 4.0.5(patch_hash=2731c9c3761c595f25ddc625a7d61ca376b389d5b3049ecb837c8940f21cfa02)(@astrojs/compiler-rs@0.5.1)(@tsc-rs/typescript@0.8.0)(@types/node@24.12.4)(@vitest/browser-playwright@5.0.5)(@vitest/ui@5.0.5)(@volar/typescript@2.4.28(@tsc-rs/typescript@0.8.0))(bufferutil@4.1.0)(effect@4.0.5)(esbuild@0.28.2)(jiti@2.7.0)(jsdom@30.1.0(@noble/hashes@1.8.0))(playwright@1.60.0)(terser@5.48.0)(unrun@0.2.39)(utf-8-validate@6.0.6)(yaml@2.9.0) + '@electric-sql/pglite': + specifier: 0.3.15 + version: 0.3.15 '@types/node': specifier: 24.12.4 version: 24.12.4 @@ -13567,8 +13570,7 @@ snapshots: - vue-tsc - yaml - '@electric-sql/pglite@0.3.15': - optional: true + '@electric-sql/pglite@0.3.15': {} '@electron-internal/extract-zip@1.0.5': {} From cadbdafc063e562626843aaea64553f791c890b4 Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Wed, 28 Oct 2026 00:38:33 -0700 Subject: [PATCH 39/46] test(web): attachment preview tests wait long enough for cold workers and fonts (#26342) Co-authored-by: Claude Opus 5.5 --- .../AttachmentFilePreview.browser.test.tsx | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/apps/web/src/components/files/AttachmentFilePreview.browser.test.tsx b/apps/web/src/components/files/AttachmentFilePreview.browser.test.tsx index 5fd8b05505282..9879edfeedbd8 100644 --- a/apps/web/src/components/files/AttachmentFilePreview.browser.test.tsx +++ b/apps/web/src/components/files/AttachmentFilePreview.browser.test.tsx @@ -2154,8 +2154,10 @@ it("refreshes authorization and retries a failed streamed attachment", async () asset={{ environmentId: EnvironmentId.make("test"), attachmentId: "retry" }} />, ); - await vi.waitFor(() => - expect(screen.container.textContent).toContain("This file contains binary data."), + // A cold large-text worker can take longer than waitFor's default to boot on CI. + await vi.waitFor( + () => expect(screen.container.textContent).toContain("This file contains binary data."), + { timeout: 10_000 }, ); await act(() => screen.container.querySelector('[role="alert"] button')!.click(), @@ -2217,7 +2219,10 @@ it("copies every indexed line rather than the mounted preview rows", async () => , ); await vi.waitFor(() => expect(screen.container.textContent).toContain("line-0")); - await vi.waitFor(async () => expect(await copyContents.current!()).toBe(text)); + // Copy rejects until the worker finishes indexing, which a cold worker can delay past waitFor's default on CI. + await vi.waitFor(async () => expect(await copyContents.current!()).toBe(text), { + timeout: 10_000, + }); const first = copyContents.current!(); await expect(copyContents.current!()).rejects.toThrow("Another copy is in progress."); expect(await first).toBe(text); @@ -2284,8 +2289,6 @@ it("loads a font attachment through the lazy preview", async () => { const { default: fontUrl } = await import("../../../node_modules/@betteroffice/fonts/assets/Carlito-Regular.ttf?url"); const file = await (await fetch(fontUrl)).blob(); - // Both lazy chunks can take longer than waitFor allows to load cold on CI. - await Promise.all([import("./FontFilePreview"), import("./FontViewer")]); await using screen = await render( { file={file} />, ); - await vi.waitFor(() => { - const frame = screen.container.querySelector("iframe"); - expect(frame?.title).toBe("Carlito.ttf"); - expect(frame?.srcdoc).toContain("@font-face"); - expect(frame?.srcdoc).toContain("The quick brown fox"); - }); + // Two cold lazy chunks plus decoding a 600 KB font can take longer than waitFor's default on CI. + await vi.waitFor( + () => { + const frame = screen.container.querySelector("iframe"); + expect(frame?.title).toBe("Carlito.ttf"); + expect(frame?.srcdoc).toContain("@font-face"); + expect(frame?.srcdoc).toContain("The quick brown fox"); + }, + { timeout: 10_000 }, + ); expect(screen.container.querySelector('[role="alert"]')).toBeNull(); }); From 2349c81afc1ce5de71fdb50f488f395f0307e4a2 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 28 Oct 2026 00:39:19 -0700 Subject: [PATCH 40/46] refactor(mobile): app link parsing reads top to bottom with pipe (#26330) Co-authored-by: Claude Opus 5.5 --- apps/mobile/src/App.tsx | 37 ++++++++++-- apps/mobile/src/lib/appLinkState.test.ts | 72 ------------------------ apps/mobile/src/lib/appLinkState.ts | 27 --------- 3 files changed, 32 insertions(+), 104 deletions(-) delete mode 100644 apps/mobile/src/lib/appLinkState.test.ts delete mode 100644 apps/mobile/src/lib/appLinkState.ts diff --git a/apps/mobile/src/App.tsx b/apps/mobile/src/App.tsx index 78299a2aa480c..225496bb7a407 100644 --- a/apps/mobile/src/App.tsx +++ b/apps/mobile/src/App.tsx @@ -13,9 +13,11 @@ import { createPathConfigForStaticNavigation, createStaticNavigation, getActionFromState, + getStateFromPath, type NavigationAction, type NavigationState, } from "@react-navigation/native"; +import { pipe } from "effect/Function"; import { RegistryContext } from "@effect/atom-react"; import { ProjectChipIconRasterizer } from "./native/ProjectChipIconImages"; @@ -48,12 +50,22 @@ import { subscribeComposerDraftModelReconciliation } from "./state/use-composer- import { environmentThreadShells } from "./state/threads"; import { environmentShell } from "./state/shell"; import { OverlayPortalHost } from "./components/ui/OverlayPortal"; -import { shouldHandleAppLink } from "./lib/appLinking"; +import { shouldHandleAppLink, restoreDraftLinkState } from "./lib/appLinking"; import { hasCloudPublicConfig } from "./features/cloud/publicConfig"; -import { pairingFormLinkAction } from "./features/connection/devAutoConnect"; -import { resolvePendingNewTaskPromptLink } from "./features/prompt-links/newTaskPromptLink"; -import { resolvePendingThreadPromptLink } from "./features/prompt-links/threadPromptLink"; -import { getAppStateFromPath } from "./lib/appLinkState"; +import { takeScheduledTaskNewLink } from "./features/settings/scheduled-task-editor-state"; +import { + pairingFormLinkAction, + startDevAutoConnect, + withoutPairingTarget, +} from "./features/connection/devAutoConnect"; +import { + resolvePendingNewTaskPromptLink, + takeNewTaskPromptLink, +} from "./features/prompt-links/newTaskPromptLink"; +import { + resolvePendingThreadPromptLink, + takeThreadPromptLink, +} from "./features/prompt-links/threadPromptLink"; import { disposeOnFoundationReplace, type FoundationHotModule, @@ -63,10 +75,12 @@ import { installUiRuntimeMemoryWarningGc } from "./lib/useUiRuntimeMemoryWarning import { SubscriptionUsageCoordinator } from "./widgets/SubscriptionUsageCoordinator"; import { installSubscriptionUsageRefresh } from "./widgets/useSubscriptionUsage"; import { GlobalVoiceInputControl } from "./features/voice-input/GlobalVoiceInputControl"; +import { restoreSettingsLinkState } from "./features/settings/settings-navigation"; import { hydrateWorkspaceSelections, launchWorkspaceURL, recordWorkspaceSelections, + settingsLinkPage, } from "./features/workspace/workspace-selection-store"; import { workspaceDetailLinkAction } from "./features/workspace/workspace-detail-selection"; import { NativeLayoutMetricsProvider } from "./native/native-layout-metrics"; @@ -94,6 +108,19 @@ const linkingConfig = { screens: createPathConfigForStaticNavigation(RootStack, { initialRouteName: "Home" }) ?? {}, } as const; +function getAppStateFromPath(path: string, options: Parameters[1]) { + return pipe( + getStateFromPath(path, options), + (state) => restoreDraftLinkState(state, path), + (state) => restoreSettingsLinkState(state, settingsLinkPage()), + takeThreadPromptLink, + takeNewTaskPromptLink, + withoutPairingTarget, + startDevAutoConnect, + takeScheduledTaskNewLink, + ); +} + const appLinking = { prefixes: [Linking.createURL("/"), "t3code://", "t3code-dev://", "t3code-preview://"], config: { initialRouteName: "Home" }, diff --git a/apps/mobile/src/lib/appLinkState.test.ts b/apps/mobile/src/lib/appLinkState.test.ts deleted file mode 100644 index 9b907df800597..0000000000000 --- a/apps/mobile/src/lib/appLinkState.test.ts +++ /dev/null @@ -1,72 +0,0 @@ -import * as NodeModule from "node:module"; -import { expect, it, vi } from "vite-plus/test"; - -const harness = vi.hoisted(() => { - vi.stubGlobal("__DEV__", true); - return { pair: vi.fn() }; -}); - -// The native package pulls in React Native; its link parsing and actions live in core and routers. -vi.mock("@react-navigation/native", () => { - const requireFromNative = NodeModule.createRequire( - NodeModule.createRequire(import.meta.url).resolve("@react-navigation/native/package.json"), - ); - const requireFromCore = NodeModule.createRequire( - requireFromNative.resolve("@react-navigation/core/package.json"), - ); - const { getStateFromPath } = requireFromNative("@react-navigation/core"); - const { CommonActions, StackActions } = requireFromCore("@react-navigation/routers"); - return { getStateFromPath, CommonActions, StackActions }; -}); - -vi.mock("./rootNavigation", () => ({ - rootNavigationRef: { isReady: () => false }, - whenRootNavigationReady: () => {}, -})); -vi.mock("../components/ui/Dialog", () => ({ alert: () => {} })); -vi.mock("../features/prompt-links/prompt-link-actions", () => ({})); -// Stores the prompt-link steps read only when a link carries a prompt; they load the native runtime. -vi.mock("../state/projects", () => ({})); -vi.mock("../state/pull-requests", () => ({})); -vi.mock("../state/server", () => ({})); -vi.mock("../state/threads", () => ({})); -vi.mock("../state/use-composer-drafts", () => ({})); -vi.mock("../state/workspace", () => ({})); -vi.mock("../state/use-remote-environment-registry", () => ({ pairEnvironment: harness.pair })); -vi.mock("../features/workspace/workspace-selection-store", () => ({ - settingsLinkPage: () => undefined, - workspaceHasColumns: () => false, -})); - -import { findLinkedRoute } from "./appLinking"; -import { getAppStateFromPath } from "./appLinkState"; - -const PAIRING_URL = "http://192.168.1.10:3773/pair#token=abc"; - -// RootStack imports every screen, which Node cannot load; these are its paths for the pairing form. -const linking = { - initialRouteName: "Home", - screens: { - Home: "", - Settings: "settings", - SettingsEnvironmentNew: "settings/environment-new", - ConnectionsNew: "connections/new", - }, -}; - -it("a link cannot aim a pairing at a saved environment", () => { - harness.pair.mockResolvedValue({ ok: false, error: null }); - for (const path of ["connections/new", "settings/environment-new"]) { - harness.pair.mockClear(); - const state = getAppStateFromPath( - `${path}?routeFor=victim&pairingUrl=${encodeURIComponent(PAIRING_URL)}&autoConnect=1`, - linking, - ); - - expect(findLinkedRoute(state, ["ConnectionsNew", "SettingsEnvironmentNew"])?.params).toEqual({ - pairingUrl: PAIRING_URL, - autoConnect: "1", - }); - expect(harness.pair).toHaveBeenCalledExactlyOnceWith(PAIRING_URL); - } -}); diff --git a/apps/mobile/src/lib/appLinkState.ts b/apps/mobile/src/lib/appLinkState.ts deleted file mode 100644 index f404819f68f89..0000000000000 --- a/apps/mobile/src/lib/appLinkState.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { getStateFromPath } from "@react-navigation/native"; - -import { startDevAutoConnect, withoutPairingTarget } from "../features/connection/devAutoConnect"; -import { takeNewTaskPromptLink } from "../features/prompt-links/newTaskPromptLink"; -import { takeThreadPromptLink } from "../features/prompt-links/threadPromptLink"; -import { takeScheduledTaskNewLink } from "../features/settings/scheduled-task-editor-state"; -import { restoreSettingsLinkState } from "../features/settings/settings-navigation"; -import { settingsLinkPage } from "../features/workspace/workspace-selection-store"; -import { restoreDraftLinkState } from "./appLinking"; - -/** Parses an app link into navigation state, taking each link-owned side effect on the way. */ -export function getAppStateFromPath(path: string, options: Parameters[1]) { - return takeScheduledTaskNewLink( - startDevAutoConnect( - withoutPairingTarget( - takeNewTaskPromptLink( - takeThreadPromptLink( - restoreSettingsLinkState( - restoreDraftLinkState(getStateFromPath(path, options), path), - settingsLinkPage(), - ), - ), - ), - ), - ), - ); -} From ce66aab13fabde01b82deac4b4b3eaae53641988 Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Wed, 28 Oct 2026 00:40:10 -0700 Subject: [PATCH 41/46] ci(release): Windows CLI builds retry the Node.js download (#26345) Co-authored-by: Claude Opus 5.5 --- .github/workflows/release-desktop.yml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/.github/workflows/release-desktop.yml b/.github/workflows/release-desktop.yml index ca418467f6b9c..3383bb0e80bb8 100644 --- a/.github/workflows/release-desktop.yml +++ b/.github/workflows/release-desktop.yml @@ -254,6 +254,24 @@ jobs: if [[ "${{ inputs.version }}" =~ -nightly\.[0-9]{8}\.[0-9]+$ ]]; then brand=nightly; fi node scripts/apply-web-brand-assets.ts "$brand" apps/server/dist/client + # tsdown downloads the target Node with one fetch and no retry, and + # nodejs.org drops that download often enough to fail releases. Seed + # tsdown's cache with a retried, checksum-verified copy instead. + - name: Download target Node.js for the CLI executable + if: inputs.cli_archive && inputs.platform == 'win' + shell: bash + run: | + set -euo pipefail + version="$(node -p 'require("./package.json").engines.node')" + name="node-v$version-win-${{ inputs.arch }}" + dest="${XDG_CACHE_HOME:-$HOME/.cache}/tsdown/node/v$version/win-${{ inputs.arch }}" + dir="$RUNNER_TEMP/node-download" + mkdir -p "$dir" "$dest" + curl -fsSL --retry 5 --retry-all-errors -o "$dir/$name.zip" "https://nodejs.org/dist/v$version/$name.zip" + curl -fsSL --retry 5 --retry-all-errors -o "$dir/SHASUMS256.txt" "https://nodejs.org/dist/v$version/SHASUMS256.txt" + (cd "$dir" && grep " $name.zip\$" SHASUMS256.txt | sha256sum -c -) + unzip -p "$dir/$name.zip" "$name/node.exe" > "$dest/node.exe" + # Setup Vite+ selects the exact engines.node version from package.json. # Native builds embed that executable; cross-target builds download the # same version for their target. From 990e9e06e59c9b06317670a08ff0dca5859cb7d8 Mon Sep 17 00:00:00 2001 From: Yash Singh Date: Wed, 28 Oct 2026 02:41:07 -0500 Subject: [PATCH 42/46] fix(web): preserve handoffs before superseded attempt folds (#26333) --- .../components/chat/MessagesTimeline.logic.test.ts | 11 +++++++++++ .../web/src/components/chat/MessagesTimeline.logic.ts | 1 + 2 files changed, 12 insertions(+) diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.test.ts b/apps/web/src/components/chat/MessagesTimeline.logic.test.ts index dfc9c31fb3a7d..aa082a268667d 100644 --- a/apps/web/src/components/chat/MessagesTimeline.logic.test.ts +++ b/apps/web/src/components/chat/MessagesTimeline.logic.test.ts @@ -2642,6 +2642,15 @@ describe("v2 run and attempt history", () => { completedAt: null, }; const timelineEntries = [ + { + id: "context-handoff-entry", + kind: "event" as const, + createdAt: "2026-01-01T00:00:00Z", + attempt: supersededAttempt, + projectedItem: { + item: { type: "handoff", runId }, + } as never, + }, { id: "initial-user-entry", kind: "message" as const, @@ -2744,6 +2753,7 @@ describe("v2 run and attempt history", () => { const collapsedRows = deriveMessagesTimelineRows(common); expect(collapsedRows.map((row) => row.id)).toEqual([ + "context-handoff-entry", "initial-user-entry", `attempt-fold:${supersededAttemptId}`, "superseded-thread-created-entry", @@ -2762,6 +2772,7 @@ describe("v2 run and attempt history", () => { expandedAttemptIds: new Set([supersededAttemptId]), }); expect(expandedRows.map((row) => row.id)).toEqual([ + "context-handoff-entry", "initial-user-entry", `attempt-fold:${supersededAttemptId}`, "superseded-assistant-entry", diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.ts b/apps/web/src/components/chat/MessagesTimeline.logic.ts index f771c1ed0d072..ac9db2f7dd794 100644 --- a/apps/web/src/components/chat/MessagesTimeline.logic.ts +++ b/apps/web/src/components/chat/MessagesTimeline.logic.ts @@ -770,6 +770,7 @@ function deriveSupersededAttemptFolds( completeAttemptIds.has(entry.attempt.id) || unfoldedRunIds.has(entry.attempt.runId) || (entry.kind === "message" && entry.message.role === "user") || + (entry.kind === "event" && entry.projectedItem.item.type === "handoff") || entry.kind === "html-render" || entry.kind === "mcp-app" || timelineEntryIsPersistentResourceCard(entry) || From 441a4ebd65543fa414797253eb31cc02f8811e32 Mon Sep 17 00:00:00 2001 From: Yash Singh Date: Wed, 28 Oct 2026 02:42:27 -0500 Subject: [PATCH 43/46] feat(web): open tool call diffs in the diff panel (#26304) --- .../Adapters/AcpAdapterV2.test.ts | 20 ++- .../orchestration-v2/WireProjection.test.ts | 9 + .../src/orchestration-v2/WireProjection.ts | 14 +- apps/web/src/components/ChatView.tsx | 2 + apps/web/src/components/DiffPanel.tsx | 157 +++++++++++++----- .../src/components/chat/MessagesTimeline.tsx | 4 + .../components/chat/TimelineWorkEntryRow.tsx | 2 +- .../src/components/chat/V2ItemInspector.tsx | 15 +- .../src/components/chat/timelineRowContext.ts | 2 + .../components/chat/useRightPanelActions.ts | 11 ++ apps/web/src/diffPanelStore.test.ts | 76 ++++++++- apps/web/src/diffPanelStore.ts | 31 +++- apps/web/src/lib/toolCallDiff.test.ts | 132 +++++++++++++++ apps/web/src/lib/toolCallDiff.ts | 77 +++++++++ packages/provider-acp/src/server/adapter.ts | 19 ++- 15 files changed, 520 insertions(+), 51 deletions(-) create mode 100644 apps/web/src/lib/toolCallDiff.test.ts create mode 100644 apps/web/src/lib/toolCallDiff.ts diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts index 92eb5b1047ff8..f05ace7ff750d 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts @@ -111,7 +111,25 @@ const ACP_TEST_DRIVER = ProviderDriverKind.make("acp-test"); const decodeUnknownJson = Schema.decodeUnknownOption(Schema.fromJsonString(Schema.Unknown)); describe("acpToolCallDiffPatch", () => { - it("builds a patch per file from ACP v1 oldText/newText, with /dev/null for a new file", () => { + it("builds per-file patches from ACP hunks and recorded old/new text", () => { + for (const operation of ["add", "delete"]) { + const text = operation === "add" ? "@@ -0,0 +1 @@\n+content\n" : "@@ -1 +0,0 @@\n-content\n"; + assert.equal( + acpToolCallDiffPatch([ + { + type: "diff", + changes: [{ operation, path: "/repo/a.ts" }], + patch: { format: "git_patch", text }, + }, + ]), + [ + 'diff --git "a//repo/a.ts" "b//repo/a.ts"', + operation === "add" ? "--- /dev/null" : '--- "a//repo/a.ts"', + operation === "delete" ? "+++ /dev/null" : '+++ "b//repo/a.ts"', + text, + ].join("\n"), + ); + } assert.equal( acpToolCallDiffPatch([ { type: "diff", path: "/repo/new.txt", oldText: null, newText: "hello\n" }, diff --git a/apps/server/src/orchestration-v2/WireProjection.test.ts b/apps/server/src/orchestration-v2/WireProjection.test.ts index baf1ea0a3cb77..d449aeb933dbf 100644 --- a/apps/server/src/orchestration-v2/WireProjection.test.ts +++ b/apps/server/src/orchestration-v2/WireProjection.test.ts @@ -275,6 +275,15 @@ describe("orchestration V2 wire projection", () => { expect(projected).not.toHaveProperty("newStr"); expect(projected).toMatchObject({ fileName: "src/main.ts", additions: 3, deletions: 1 }); expect(item.diffStr).toBe("+new code"); + expect(projectTurnItemForDetail(item)).toMatchObject({ diffStr: "+new code" }); + expect(projectTurnItemForDetail(item)).not.toHaveProperty("oldStr"); + expect(projectTurnItemForDetail(item)).not.toHaveProperty("newStr"); + const { diffStr: _diff, ...contentOnly } = item; + expect(projectTurnItemForDetail(contentOnly)).toMatchObject({ + oldStr: "old code", + newStr: "new code", + }); + expect(projectTurnItemForWire(contentOnly)).not.toHaveProperty("newStr"); const failed = projectTurnItemForWire({ ...item, status: "failed", diff --git a/apps/server/src/orchestration-v2/WireProjection.ts b/apps/server/src/orchestration-v2/WireProjection.ts index a84f8cd466859..6b66f1b91f204 100644 --- a/apps/server/src/orchestration-v2/WireProjection.ts +++ b/apps/server/src/orchestration-v2/WireProjection.ts @@ -200,8 +200,20 @@ export function projectTurnItemForDetail(item: OrchestrationV2TurnItem): Orchest result: item.result === null ? null : (truncateDetail(item.result, MAX_ON_DEMAND_BYTES) ?? null), }; - case "handoff": case "file_change": + return { + ...projectTurnItemForWire(item), + ...(item.diffStr === undefined + ? {} + : { diffStr: truncateDetail(item.diffStr, MAX_ON_DEMAND_BYTES) }), + ...(!item.diffStr?.trim() && item.oldStr !== undefined + ? { oldStr: truncateDetail(item.oldStr, MAX_ON_DEMAND_BYTES) } + : {}), + ...(!item.diffStr?.trim() && item.newStr !== undefined + ? { newStr: truncateDetail(item.newStr, MAX_ON_DEMAND_BYTES) } + : {}), + }; + case "handoff": return projectTurnItemForWire(item); default: return item; diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 4796a8465e244..c6d9cfd534b86 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -2323,6 +2323,7 @@ export default function ChatView(props: ChatViewProps) { createBrowserSurface, createBrowserSurfaceStable, moveRightPanelSurfaceStable, + onOpenToolDiffStable, onOpenTurnDiffStable, openChangesFromThreadPanelStable, openFileSurfaceStable, @@ -3309,6 +3310,7 @@ export default function ChatView(props: ChatViewProps) { routeThreadKey={displayedTimelineKey} displayThreadKey={displayedTimelineKey} announceTurnStatus={!isProviderSubagent} + onOpenToolDiff={onOpenToolDiffStable} onOpenTurnDiff={onOpenTurnDiffStable} onOpenThread={onOpenRelatedThreadStable} parentThreadLink={parentThreadLink} diff --git a/apps/web/src/components/DiffPanel.tsx b/apps/web/src/components/DiffPanel.tsx index 53af7b6e92e44..c257d0ea52d34 100644 --- a/apps/web/src/components/DiffPanel.tsx +++ b/apps/web/src/components/DiffPanel.tsx @@ -26,10 +26,12 @@ import { useOpenInPreferredEditor } from "../editorPreferences"; import { useFileContextMenuHandler } from "../fileContextMenu"; import { type DraftId } from "../composerDraftStore"; import { openDiffFilePrimaryAction } from "../diffFileActions"; -import { useCheckpointDiff } from "~/state/queries"; +import { toolCallDiff } from "../lib/toolCallDiff"; +import { useCheckpointDiff, useTurnItemDetail } from "~/state/queries"; import { revisionedQuery } from "~/state/revisionedQuery"; import { cn } from "~/lib/utils"; import { + resolveToolCallDiffTarget, selectThreadDiffPanelSelection, useDiffPanelSessionStore, useDiffPanelStore, @@ -48,7 +50,12 @@ import { import { PREFERRED_HIGHLIGHTER } from "../lib/syntaxHighlighting"; import { areAllDiffFilesCollapsed, toggleAllDiffFiles } from "../lib/diffCollapse"; import { LIVE_REFRESH_MIN_INTERVAL_MS, useLiveRefresh } from "../hooks/useLiveRefresh"; -import { useProject, useThreadCheckpointSummaries, useQuietThreadShell } from "../state/entities"; +import { + useProject, + useThreadCheckpointSummaries, + useQuietThreadShell, + useQuietThreadVisibleTurnItems, +} from "../state/entities"; import { useClientSettings, updateClientSettings } from "../hooks/useSettings"; import { formatShortTimestamp } from "../timestampFormat"; import { DiffFilePathCopyButton } from "./DiffFilePathCopyButton"; @@ -252,6 +259,18 @@ export default function DiffPanel({ const diffSelection = useDiffPanelStore((state) => selectThreadDiffPanelSelection(state.byThreadKey, routeThreadRef), ); + const toolCallSelection = diffSelection.kind === "tool-call" ? diffSelection : null; + const isGitSelection = diffSelection.kind === "branch" || diffSelection.kind === "unstaged"; + const toolCallItems = useQuietThreadVisibleTurnItems(toolCallSelection ? routeThreadRef : null); + const toolCallDetail = useTurnItemDetail( + toolCallSelection && routeThreadRef + ? { + ...resolveToolCallDiffTarget(toolCallSelection, toolCallItems), + environmentId: routeThreadRef.environmentId, + } + : null, + ); + const toolCallItem = toolCallDetail.data?.item; const isGitRepo = gitStatusQuery.data?.isRepo ?? true; const turnDiffSummaries = useThreadCheckpointSummaries(routeThreadRef); const orderedTurnDiffSummaries = turnDiffSummaries.toSorted((left, right) => { @@ -283,24 +302,31 @@ export default function DiffPanel({ orderedTurnDiffSummaries[0]); const selectedCheckpointTurnCount = selectedTurn?.checkpointTurnCount; const latestTurn = orderedTurnDiffSummaries[0]; - const selectedScopeLabel = - selectedRunId === null + const selectedScopeLabel = toolCallSelection + ? "Tool call" + : selectedRunId === null ? selectedGitScope === "unstaged" ? "Uncommitted" : "Changes" : selectedTurn?.runId === latestTurn?.runId ? "Latest turn" : `Turn ${selectedCheckpointTurnCount ?? "?"}`; - const reviewSectionId = selectedTurn ? `turn:${selectedTurn.runId}` : selectedGitScope; + const reviewSectionId = toolCallSelection + ? `tool-call:${toolCallSelection.threadId}:${toolCallSelection.itemId}` + : selectedTurn + ? `turn:${selectedTurn.runId}` + : selectedGitScope; const collapseScopeKey = routeThreadRef ? `${routeThreadRef.environmentId}:${routeThreadRef.threadId}:${reviewSectionId}` : null; const codeViewMountKey = `${collapseScopeKey ?? reviewSectionId}:${codeViewRevision}`; - const reviewSectionTitle = selectedTurn - ? `Turn ${selectedCheckpointTurnCount ?? "?"}` - : selectedGitScope === "unstaged" - ? "Uncommitted" - : "Changes"; + const reviewSectionTitle = toolCallSelection + ? "Tool call" + : selectedTurn + ? `Turn ${selectedCheckpointTurnCount ?? "?"}` + : selectedGitScope === "unstaged" + ? "Uncommitted" + : "Changes"; const selectedCheckpointRange = typeof selectedCheckpointTurnCount === "number" ? { @@ -320,7 +346,7 @@ export default function DiffPanel({ { enabled: isGitRepo && selectedTurn !== undefined }, ); const branchDiffPreviewInput = - canReadFiles && selectedRunId === null && activeThread && activeCwd + canReadFiles && isGitSelection && activeThread && activeCwd ? { cwd: activeCwd, ...(selectedBaseRef ? { baseRef: selectedBaseRef } : {}), @@ -328,7 +354,7 @@ export default function DiffPanel({ } : undefined; const canRefreshGitDiff = - isGitRepo && selectedRunId === null && activeThread != null && activeCwd != null; + isGitRepo && isGitSelection && activeThread != null && activeCwd != null; // Read under the workspace mutation id, so each mutation refetches the diff; the panel stays // mounted while hidden, so a mutation made then is already reflected when it shows. const branchDiffPreview = useEnvironmentQuery( @@ -355,25 +381,52 @@ export default function DiffPanel({ const preview = branchDiffPreview.data; const gitDiff = selectedGitSource?.diff; - const selectedPatch = selectedTurn ? activeCheckpointDiff.data?.diff : gitDiff; - const isSelectedPatchTruncated = !selectedTurn && selectedGitSource?.truncated === true; - const selectedPatchQuery = selectedTurn ? activeCheckpointDiff : branchDiffPreview; + const toolCallPreview = + toolCallSelection && toolCallItem?.type === "file_change" ? toolCallDiff(toolCallItem) : null; + const selectedPatch = toolCallSelection + ? toolCallPreview?.patch + : selectedTurn + ? activeCheckpointDiff.data?.diff + : gitDiff; + const isSelectedPatchTruncated = + toolCallPreview?.truncated === true || + (isGitSelection && selectedGitSource?.truncated === true); + const selectedPatchQuery = toolCallSelection + ? toolCallDetail + : selectedTurn + ? activeCheckpointDiff + : branchDiffPreview; const isLoadingSelectedPatch = selectedPatchQuery.isPending && selectedPatchQuery.data === null; - const selectedPatchError = selectedTurn ? activeCheckpointDiff.error : branchDiffPreview.error; + const selectedPatchError = toolCallSelection + ? toolCallDetail.error + : selectedTurn + ? activeCheckpointDiff.error + : branchDiffPreview.error; const hasResolvedPatch = typeof selectedPatch === "string"; const hasNoNetChanges = hasResolvedPatch && selectedPatch.trim().length === 0; const lazySource = - !selectedTurn && selectedGitSource?.truncated && selectedGitSource.files + isGitSelection && selectedGitSource?.truncated && selectedGitSource.files ? selectedGitSource : null; const renderablePatch = getRenderablePatch( lazySource ? undefined : selectedPatch, `diff-panel:${resolvedTheme}`, - { compactPartialHunkOffsets: selectedRunId === null }, + { + compactPartialHunkOffsets: isGitSelection, + ...(toolCallSelection ? { ignoreWhitespace: diffIgnoreWhitespace } : {}), + }, ); + const recordedToolCallFileCount = + toolCallSelection && toolCallItem?.type === "file_change" + ? (toolCallItem.changes?.length ?? 0) + : 0; + const omittedToolCallFiles = + renderablePatch?.kind === "files" + ? Math.max(0, recordedToolCallFileCount - renderablePatch.files.length) + : 0; const fileStats = new Map(lazySource?.files?.map((file) => [file.path, file])); const sourceLineStat = - !selectedTurn && selectedGitSource?.files + isGitSelection && selectedGitSource?.files ? selectedGitSource.files.reduce( (total, file) => ({ additions: total.additions + file.additions, @@ -383,7 +436,7 @@ export default function DiffPanel({ ) : null; const currentLoadDiffFiles = - selectedRunId === null && activeThread && preview && selectedGitSource && canReadFiles + isGitSelection && activeThread && preview && selectedGitSource && canReadFiles ? createGitDiffFileContentsLoader(getDiffFileContents, { environmentId: activeThread.environmentId, cwd: preview.cwd, @@ -400,10 +453,7 @@ export default function DiffPanel({ return currentLoadDiffFiles(fileDiff); }); const localBranchRefs = useEnvironmentQuery( - selectedRunId === null && - selectedGitScope === "branch" && - activeThread && - branchDiffPreview.data?.cwd + isGitSelection && selectedGitScope === "branch" && activeThread && branchDiffPreview.data?.cwd ? vcsEnvironment.listRefs({ environmentId: activeThread.environmentId, input: { @@ -417,10 +467,7 @@ export default function DiffPanel({ : null, ); const remoteBranchRefs = useEnvironmentQuery( - selectedRunId === null && - selectedGitScope === "branch" && - activeThread && - branchDiffPreview.data?.cwd + isGitSelection && selectedGitScope === "branch" && activeThread && branchDiffPreview.data?.cwd ? vcsEnvironment.listRefs({ environmentId: activeThread.environmentId, input: { @@ -630,8 +677,9 @@ export default function DiffPanel({ useDiffPanelStore.getState().selectBranchBaseRef(routeThreadRef, baseRef); }; const selectedTurnValue = selectedTurn ? `turn:${selectedTurn.runId}` : ""; - const selectedScopeValue = - selectedRunId === null + const selectedScopeValue = toolCallSelection + ? "" + : selectedRunId === null ? selectedGitScope : selectedTurn?.runId === latestTurn?.runId ? "latest" @@ -698,7 +746,7 @@ export default function DiffPanel({ - {selectedRunId === null && selectedGitScope === "branch" && selectedGitSource?.baseRef && ( + {isGitSelection && selectedGitScope === "branch" && selectedGitSource?.baseRef && (
    - {codeViewFiles.length > 0 || (!selectedTurn && selectedGitSource?.files?.length) ? ( + {codeViewFiles.length > 0 || (isGitSelection && selectedGitSource?.files?.length) ? ( Select a thread to inspect turn diffs.
    - ) : !isGitRepo ? ( + ) : !isGitRepo && !toolCallSelection ? (
    Turn diffs are unavailable because this project is not a git repository.
    @@ -981,7 +1029,7 @@ export default function DiffPanel({
    No completed turns yet.
    - ) : selectedRunId === null && !canReadFiles ? ( + ) : isGitSelection && !canReadFiles ? ( fileAccess.isPending ? ( ) : ( @@ -995,7 +1043,13 @@ export default function DiffPanel({ {isSelectedPatchTruncated && !lazySource && (

    This preview exceeds the size limit. Changes shown are incomplete. - {selectedGitSource?.files ? " Totals include all changes." : ""} + {isGitSelection && selectedGitSource?.files ? " Totals include all changes." : ""} +

    + )} + {omittedToolCallFiles > 0 && ( +

    + This tool call changed {recordedToolCallFileCount} files. The recorded patch omits{" "} + {omittedToolCallFiles} of them.

    )} {selectedPatchError && !renderablePatch && ( @@ -1003,15 +1057,36 @@ export default function DiffPanel({

    {selectedPatchError}

    )} - {!renderablePatch && !lazySource ? ( + {!renderablePatch && toolCallPreview && toolCallPreview.content.length > 0 ? ( +
    +

    + The provider recorded file content without a diff. +

    + {toolCallPreview.content.map(({ label, text }) => ( +
    +

    {label}

    +
    +                      {text === "" ? "Empty content." : text}
    +                    
    +
    + ))} +
    + ) : !renderablePatch && !lazySource ? ( isLoadingSelectedPatch ? ( ) : ( diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsx index a166a25e47766..c7a38de8fbed6 100644 --- a/apps/web/src/components/chat/MessagesTimeline.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.tsx @@ -1,3 +1,4 @@ +import type { ToolCallDiffTarget } from "../../diffPanelStore"; import { AudioPreviewUnmountContext } from "../files/AudioPreview"; import { ThreadFindTimelineContext } from "./ThreadFindProvider"; import { MarkdownFindContext } from "./markdownFindContext"; @@ -241,6 +242,7 @@ interface MessagesTimelineProps { displayThreadKey?: string; /** Off where another region already speaks the thread's status, like a subagent's bar. */ announceTurnStatus?: boolean; + onOpenToolDiff?: (target: ToolCallDiffTarget) => void; onOpenTurnDiff?: (runId: RunId, filePath?: string) => void; onOpenThread: (threadId: OrchestrationV2TurnItem["threadId"]) => void; renderHtmlRender?: TimelineRowSharedState["renderHtmlRender"]; @@ -350,6 +352,7 @@ const ConversationTimeline = memo(function ConversationTimeline({ routeThreadKey, displayThreadKey, announceTurnStatus = true, + onOpenToolDiff, onOpenTurnDiff, onOpenThread, renderHtmlRender, @@ -1053,6 +1056,7 @@ const ConversationTimeline = memo(function ConversationTimeline({ onAppFullscreenChange, onFileDownload, openPullRequest, + onOpenToolDiff, onOpenTurnDiff, onOpenThread, renderHtmlRender, diff --git a/apps/web/src/components/chat/TimelineWorkEntryRow.tsx b/apps/web/src/components/chat/TimelineWorkEntryRow.tsx index 94d001be7e551..a8d5e3e68de77 100644 --- a/apps/web/src/components/chat/TimelineWorkEntryRow.tsx +++ b/apps/web/src/components/chat/TimelineWorkEntryRow.tsx @@ -536,7 +536,7 @@ function WorkEntryLogRow(props: WorkEntryRowProps) { environmentId={ctx.activeThreadEnvironmentId} cwd={ctx.markdownCwd} workspaceRoot={workspaceRoot} - onOpenTurnDiff={ctx.onOpenTurnDiff} + onOpenToolDiff={ctx.onOpenToolDiff} onImageExpand={ctx.onImageExpand} source={ctx.turnItemSource} /> diff --git a/apps/web/src/components/chat/V2ItemInspector.tsx b/apps/web/src/components/chat/V2ItemInspector.tsx index 958a56077e51a..ee6072b926b7e 100644 --- a/apps/web/src/components/chat/V2ItemInspector.tsx +++ b/apps/web/src/components/chat/V2ItemInspector.tsx @@ -1,4 +1,5 @@ -import type { EnvironmentId, OrchestrationV2ProjectedTurnItem, RunId } from "@t3tools/contracts"; +import type { ToolCallDiffTarget } from "../../diffPanelStore"; +import type { EnvironmentId, OrchestrationV2ProjectedTurnItem } from "@t3tools/contracts"; import { toolCallLines, turnItemDetailRevision, @@ -32,7 +33,7 @@ interface V2ItemInspectorProps { readonly environmentId: EnvironmentId; readonly cwd?: string | undefined; readonly workspaceRoot?: string | undefined; - readonly onOpenTurnDiff?: ((runId: RunId, filePath?: string) => void) | undefined; + readonly onOpenToolDiff?: ((target: ToolCallDiffTarget) => void) | undefined; readonly onImageExpand?: ((preview: ExpandedImagePreview) => void) | undefined; readonly source?: TurnItemSource | undefined; } @@ -289,11 +290,17 @@ export const V2ItemInspector = memo(function V2ItemInspector(props: V2ItemInspec -{formatDiffCount(item.deletions ?? 0)}
    ) : null} - {item.runId !== null && props.onOpenTurnDiff ? ( + {props.onOpenToolDiff ? ( diff --git a/apps/web/src/components/chat/timelineRowContext.ts b/apps/web/src/components/chat/timelineRowContext.ts index b3677b33de76c..6334ef53bd050 100644 --- a/apps/web/src/components/chat/timelineRowContext.ts +++ b/apps/web/src/components/chat/timelineRowContext.ts @@ -1,3 +1,4 @@ +import type { ToolCallDiffTarget } from "../../diffPanelStore"; import type { ProviderApprovalDecision, RuntimeRequestId, @@ -67,6 +68,7 @@ export interface TimelineRowSharedState { findActive: boolean; onImageExpand: (preview: ExpandedImagePreview) => void; displayThreadKey?: string; + onOpenToolDiff: ((target: ToolCallDiffTarget) => void) | undefined; /** Unset where there is no diff to open, so the timeline shows no "Open diff" button. */ onOpenTurnDiff: ((runId: RunId, filePath?: string) => void) | undefined; /** diff --git a/apps/web/src/components/chat/useRightPanelActions.ts b/apps/web/src/components/chat/useRightPanelActions.ts index ac79b0853bf1f..3919df03b6af3 100644 --- a/apps/web/src/components/chat/useRightPanelActions.ts +++ b/apps/web/src/components/chat/useRightPanelActions.ts @@ -1,3 +1,4 @@ +import type { ToolCallDiffTarget } from "../../diffPanelStore"; import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; import type { ThreadPreviewState } from "../../previewStateStore"; import { formatFilePathForClipboard } from "@t3tools/client-runtime/media-actions"; @@ -302,6 +303,15 @@ export function useRightPanelActions(input: RightPanelActionsInput) { useRightPanelStore.getState().open(activeThreadRef, "diff"); onDiffPanelOpen?.(); }; + const onOpenToolDiffStable = useStableCallback((target: ToolCallDiffTarget) => { + if (!isServerThread || !activeThreadRef) return; + const diffOpen = + selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, activeThreadRef) === "diff"; + explicitDiffOpenRef.current = diffOpen ? null : activeThreadRef; + useDiffPanelStore.getState().selectToolCall(activeThreadRef, target); + useRightPanelStore.getState().open(activeThreadRef, "diff"); + onDiffPanelOpen?.(); + }); const onOpenTurnDiffStable = useStableCallback(onOpenTurnDiff); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to the preview action bus to toggle the preview panel useEffect( @@ -338,6 +348,7 @@ export function useRightPanelActions(input: RightPanelActionsInput) { createBrowserSurface, createBrowserSurfaceStable, moveRightPanelSurfaceStable, + onOpenToolDiffStable, onOpenTurnDiffStable, onToggleDiffStable, openChangesFromThreadPanelStable, diff --git a/apps/web/src/diffPanelStore.test.ts b/apps/web/src/diffPanelStore.test.ts index b7238e8edf906..82065c86fe3aa 100644 --- a/apps/web/src/diffPanelStore.test.ts +++ b/apps/web/src/diffPanelStore.test.ts @@ -1,6 +1,13 @@ +import { EnvironmentId, ThreadId, TurnItemId } from "@t3tools/contracts"; +import * as DateTime from "effect/DateTime"; +import { scopedThreadKey } from "@t3tools/client-runtime/environment"; import { beforeEach, describe, expect, it } from "vite-plus/test"; -import { useDiffPanelSessionStore, useDiffPanelStore } from "./diffPanelStore"; +import { + resolveToolCallDiffTarget, + useDiffPanelSessionStore, + useDiffPanelStore, +} from "./diffPanelStore"; describe("diffPanelStore", () => { beforeEach(() => { @@ -11,6 +18,73 @@ describe("diffPanelStore", () => { useDiffPanelSessionStore.setState({ collapsedFileKeysByScopeKey: {} }); }); + it("keeps a tool call selection independent of checkpoints and allows returning to Git changes", () => { + const ref = { environmentId: EnvironmentId.make("env"), threadId: ThreadId.make("thread") }; + const target = { + threadId: ThreadId.make("child"), + itemId: TurnItemId.make("edit"), + revision: "1", + }; + const store = useDiffPanelStore.getState(); + store.selectToolCall(ref, target); + store.reconcileTurnSelection(ref, []); + expect(useDiffPanelStore.getState().byThreadKey[scopedThreadKey(ref)]).toEqual({ + kind: "tool-call", + ...target, + }); + store.selectGitScope(ref, "branch"); + expect(useDiffPanelStore.getState().byThreadKey[scopedThreadKey(ref)]).toEqual({ + kind: "branch", + baseRef: null, + }); + }); + + it("refreshes a live tool-call target when its source item completes", () => { + const target = { + threadId: ThreadId.make("child"), + itemId: TurnItemId.make("edit"), + revision: "live", + }; + const now = DateTime.makeUnsafe("2026-10-28T00:00:00Z"); + const row = { + position: 0, + visibility: "local" as const, + sourceThreadId: target.threadId, + sourceItemId: target.itemId, + item: { + id: target.itemId, + threadId: target.threadId, + runId: null, + nodeId: null, + providerThreadId: null, + providerTurnId: null, + nativeItemRef: null, + parentItemId: null, + ordinal: 1, + status: "running" as const, + title: null, + startedAt: now, + completedAt: null, + updatedAt: now, + type: "file_change" as const, + fileName: "a.ts", + }, + }; + expect(resolveToolCallDiffTarget(target, [row])).toBe(target); + const completed = { + ...row, + item: { ...row.item, status: "completed" as const, completedAt: now }, + }; + expect(resolveToolCallDiffTarget(target, [completed])).toEqual({ + ...target, + revision: DateTime.formatIso(now), + }); + expect( + resolveToolCallDiffTarget(target, [{ ...completed, sourceThreadId: ThreadId.make("other") }]), + ).toBe(target); + expect(resolveToolCallDiffTarget(target, [])).toBe(target); + }); + it("retains fold choices when a panel consumer returns with fresh defaults", () => { const scopeKey = "environment-1:thread-1:branch"; const defaults = new Set(["src/app.ts", "src/index.ts"]); diff --git a/apps/web/src/diffPanelStore.ts b/apps/web/src/diffPanelStore.ts index eb6f0e9d455c8..11d96a3909ce7 100644 --- a/apps/web/src/diffPanelStore.ts +++ b/apps/web/src/diffPanelStore.ts @@ -1,11 +1,20 @@ +import type { ThreadStructureVisibleTurnItem } from "@t3tools/client-runtime/state/shell"; +import { turnItemDetailRevision } from "@t3tools/client-runtime/work-log/item-detail"; import { scopedThreadKey } from "@t3tools/client-runtime/environment"; -import type { RunId, ScopedThreadRef } from "@t3tools/contracts"; +import type { RunId, ScopedThreadRef, ThreadId, TurnItemId } from "@t3tools/contracts"; import { create } from "zustand"; import { createJSONStorage, persist } from "zustand/middleware"; import { resolveStorage } from "./lib/storage"; +export interface ToolCallDiffTarget { + readonly threadId: ThreadId; + readonly itemId: TurnItemId; + readonly revision: string; +} + export type DiffPanelSelection = + | ({ kind: "tool-call" } & ToolCallDiffTarget) | { kind: "branch"; baseRef: string | null } | { kind: "unstaged" } | { kind: "turn"; turnId: RunId; filePath: string | null; revealRequestId: number }; @@ -17,6 +26,7 @@ interface DiffPanelStoreState { branchBaseRefByThreadKey: Record; selectGitScope: (ref: ScopedThreadRef, scope: "branch" | "unstaged") => void; selectBranchBaseRef: (ref: ScopedThreadRef, baseRef: string | null) => void; + selectToolCall: (ref: ScopedThreadRef, target: ToolCallDiffTarget) => void; selectTurn: (ref: ScopedThreadRef, turnId: RunId, filePath?: string) => void; reconcileTurnSelection: (ref: ScopedThreadRef, availableTurnIds: ReadonlyArray) => void; removeThread: (ref: ScopedThreadRef) => void; @@ -69,6 +79,13 @@ export const useDiffPanelStore = create()( }, }; }), + selectToolCall: (ref, target) => + set((state) => ({ + byThreadKey: { + ...state.byThreadKey, + [scopedThreadKey(ref)]: { kind: "tool-call", ...target }, + }, + })), selectTurn: (ref, turnId, filePath) => set((state) => { const threadKey = scopedThreadKey(ref); @@ -160,3 +177,15 @@ export function selectThreadDiffPanelSelection( if (!ref) return DEFAULT_SELECTION; return byThreadKey[scopedThreadKey(ref)] ?? DEFAULT_SELECTION; } + +export function resolveToolCallDiffTarget( + target: ToolCallDiffTarget, + items: ReadonlyArray, +): ToolCallDiffTarget { + const row = items.find( + (row) => row.sourceThreadId === target.threadId && row.sourceItemId === target.itemId, + ); + if (row?.item.type !== "file_change") return target; + const revision = turnItemDetailRevision(row.item); + return revision === target.revision ? target : { ...target, revision }; +} diff --git a/apps/web/src/lib/toolCallDiff.test.ts b/apps/web/src/lib/toolCallDiff.test.ts new file mode 100644 index 0000000000000..cb33dd45cfb5a --- /dev/null +++ b/apps/web/src/lib/toolCallDiff.test.ts @@ -0,0 +1,132 @@ +import { describe, expect, it } from "vite-plus/test"; +import { getRenderablePatch, resolveFileDiffPath } from "./diffRendering"; +import { toolCallDiff } from "./toolCallDiff"; + +describe("toolCallDiff", () => { + it("renders recorded hunks without reading a repository, including renamed paths", () => { + for (const changes of [ + undefined, + [{ operation: "update", oldPath: "old.ts", path: "new.ts" }], + ]) { + const { patch } = toolCallDiff({ + fileName: "old.ts", + diffStr: "@@ -1 +1 @@\n-before\n+after\n", + ...(changes ? { changes } : {}), + }); + const rendered = getRenderablePatch(patch); + expect(rendered?.kind).toBe("files"); + if (rendered?.kind !== "files") throw new Error("Expected a file diff"); + expect(rendered.files).toHaveLength(1); + expect(resolveFileDiffPath(rendered.files[0]!)).toBe(changes ? "new.ts" : "old.ts"); + expect(rendered.files[0]!.additionLines.join("")).toContain("after"); + expect(rendered.files[0]!.deletionLines.join("")).toContain("before"); + } + }); + + it("renders added and deleted file contents as a file diff", () => { + for (const operation of ["add", "delete"]) { + for (const [content, ending] of [ + ["first\nsecond", ""], + ["@@ -0,0 +1 @@\n+content", "\n"], + ["@@ -1 +0,0 @@\n-content", "\n"], + ["first\nsecond", "\n"], + ["--- notes\nsecond", "\n"], + ["diff --git notes\nsecond", "\n"], + ["--- a/embedded.ts\n+++ b/embedded.ts\n@@ -1 +1 @@\n-before\n+after", "\n"], + [ + "diff --git a/embedded.ts b/embedded.ts\n--- a/embedded.ts\n+++ b/embedded.ts\n@@ -1 +1 @@\n-before\n+after", + "\n", + ], + ] as const) { + const recordedContent = `${content}${ending}`; + const rendered = getRenderablePatch( + toolCallDiff({ + fileName: "file with spaces.ts", + diffStr: recordedContent, + changes: [{ operation, path: "file with spaces.ts" }], + }).patch, + ); + expect(rendered?.kind).toBe("files"); + if (rendered?.kind !== "files") throw new Error("Expected a file diff"); + expect(resolveFileDiffPath(rendered.files[0]!)).toBe("file with spaces.ts"); + const file = rendered.files[0]!; + const lines = content.split("\n").length; + expect(file.additionLines).toHaveLength(operation === "add" ? lines : 0); + expect(file.deletionLines).toHaveLength(operation === "delete" ? lines : 0); + expect((operation === "add" ? file.additionLines : file.deletionLines).join("")).toBe( + recordedContent.replace(/\n$/, ""), + ); + } + } + }); + + it("keeps content-only writes out of the patch and preserves empty recorded content", () => { + for (const operation of ["add", "delete"]) { + expect( + toolCallDiff({ + fileName: "empty.ts", + diffStr: "", + changes: [{ operation, path: "empty.ts" }], + }).content, + ).toEqual([ + { + label: operation === "add" ? "Recorded content after" : "Recorded content before", + text: "", + }, + ]); + } + expect(toolCallDiff({ fileName: "a.ts", oldStr: "before", newStr: "" })).toEqual({ + patch: undefined, + content: [ + { label: "Recorded content before", text: "before" }, + { label: "Recorded content after", text: "" }, + ], + truncated: false, + }); + expect(toolCallDiff({ fileName: "a.ts", newStr: "after" }).content).toEqual([ + { label: "Recorded content after", text: "after" }, + ]); + }); + + it("removes transport truncation markers before converting file contents", () => { + const suffix = "\n… output truncated for transport"; + const diff = toolCallDiff({ + fileName: "a.ts", + diffStr: `first\nsecond${suffix}`, + changes: [{ operation: "add", path: "a.ts" }], + }); + expect(diff.truncated).toBe(true); + expect(diff.patch).not.toContain("output truncated"); + expect(getRenderablePatch(diff.patch)?.kind).toBe("files"); + expect(toolCallDiff({ fileName: "a.ts", newStr: `content${suffix}` })).toMatchObject({ + content: [{ label: "Recorded content after", text: "content" }], + truncated: true, + }); + }); + + it("preserves complete patches and unsupported provider output", () => { + for (const operation of ["add", "delete"]) { + const diffStr = [ + "diff --git a/a.ts b/a.ts", + `--- ${operation === "add" ? "/dev/null" : "a/a.ts"}`, + `+++ ${operation === "add" ? "b/a.ts" : "/dev/null"}`, + operation === "add" ? "@@ -0,0 +1 @@" : "@@ -1 +0,0 @@", + operation === "add" ? "+content" : "-content", + "", + ].join("\n"); + expect( + toolCallDiff({ fileName: "a.ts", diffStr, changes: [{ operation, path: "a.ts" }] }).patch, + ).toBe(diffStr); + } + for (const diffStr of [ + undefined, + "", + "provider output", + "--- a/a.ts\n+++ b/a.ts\n@@ -1 +1 @@\n-a\n+b\n", + ]) { + expect( + toolCallDiff({ fileName: "a.ts", ...(diffStr === undefined ? {} : { diffStr }) }).patch, + ).toBe(diffStr); + } + }); +}); diff --git a/apps/web/src/lib/toolCallDiff.ts b/apps/web/src/lib/toolCallDiff.ts new file mode 100644 index 0000000000000..2224702601d74 --- /dev/null +++ b/apps/web/src/lib/toolCallDiff.ts @@ -0,0 +1,77 @@ +import type { OrchestrationV2TurnItem } from "@t3tools/contracts"; +import { getRenderablePatch, resolveFileDiffPath } from "./diffRendering"; + +type FileChange = Extract; + +function toolCallPatch(item: Pick) { + let patch = item.diffStr; + if (!patch) return patch; + const change = item.changes?.[0]; + const rawFileOperation = change?.operation === "add" || change?.operation === "delete"; + if (patch.startsWith("diff --git ") || patch.startsWith("--- ")) { + if (!rawFileOperation) return patch; + const parsed = getRenderablePatch(patch, "tool-call-format"); + if ( + parsed?.kind === "files" && + parsed.files.every((file) => { + const path = resolveFileDiffPath(file).replaceAll("\\", "/"); + return ( + file.hunks.length > 0 && + item.changes?.some((recorded) => { + const recordedPath = recorded.path.replaceAll("\\", "/"); + return ( + (recorded.operation === "add" + ? file.deletionLines.length === 0 + : recorded.operation === "delete" + ? file.additionLines.length === 0 + : true) && + (recordedPath === path || recordedPath.endsWith(`/${path}`)) + ); + }) + ); + }) + ) + return patch; + } + // Codex can record added/deleted file contents or bare hunks instead of a complete patch. + if (rawFileOperation || !patch.trimStart().startsWith("@@ ")) { + if (!rawFileOperation) return patch; + const lines = patch.split("\n"); + const hasFinalNewline = patch.endsWith("\n"); + if (hasFinalNewline) lines.pop(); + const added = change.operation === "add"; + const range = `1,${lines.length}`; + patch = `@@ -${added ? "0,0" : range} +${added ? range : "0,0"} @@\n${lines.map((line) => `${added ? "+" : "-"}${line}`).join("\n")}\n${hasFinalNewline ? "" : "\\ No newline at end of file\n"}`; + } + const oldPath = change?.oldPath ?? item.fileName; + const newPath = change?.path ?? item.fileName; + const before = change?.operation === "add" ? "/dev/null" : JSON.stringify(`a/${oldPath}`); + const after = change?.operation === "delete" ? "/dev/null" : JSON.stringify(`b/${newPath}`); + return `diff --git ${JSON.stringify(`a/${oldPath}`)} ${JSON.stringify(`b/${newPath}`)}\n--- ${before}\n+++ ${after}\n${patch.trimStart()}`; +} + +function recordedText(value: string | undefined) { + const suffix = "\n… output truncated for transport"; + const truncated = value !== undefined && value.endsWith(suffix); + return { text: truncated ? value.slice(0, -suffix.length) : value, truncated }; +} + +export function toolCallDiff( + item: Pick, +) { + const diff = recordedText(item.diffStr); + const operation = item.changes?.[0]?.operation; + const emptyContent = diff.text === "" ? "" : undefined; + const before = recordedText(item.oldStr ?? (operation === "delete" ? emptyContent : undefined)); + const after = recordedText(item.newStr ?? (operation === "add" ? emptyContent : undefined)); + return { + patch: toolCallPatch({ ...item, ...(diff.text === undefined ? {} : { diffStr: diff.text }) }), + content: diff.text?.trim() + ? [] + : [ + { label: "Recorded content before", text: before.text }, + { label: "Recorded content after", text: after.text }, + ].filter((entry): entry is { label: string; text: string } => entry.text !== undefined), + truncated: diff.truncated || before.truncated || after.truncated, + }; +} diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index ead4f553fc43d..7e7f43fa632cc 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -838,7 +838,24 @@ export function acpToolCallDiffPatch(content: unknown): string | undefined { }); for (const diff of diffs) { const patch = unknownRecord(diff.patch); - if (typeof patch?.text === "string") return patch.text; + if (typeof patch?.text === "string") { + const change = + Array.isArray(diff.changes) && diff.changes.length === 1 + ? unknownRecord(diff.changes[0]) + : undefined; + if ( + patch.format === "git_patch" && + patch.text.trimStart().startsWith("@@ ") && + typeof change?.path === "string" + ) { + const oldPath = typeof change.oldPath === "string" ? change.oldPath : change.path; + const before = change.operation === "add" ? "/dev/null" : JSON.stringify(`a/${oldPath}`); + const after = + change.operation === "delete" ? "/dev/null" : JSON.stringify(`b/${change.path}`); + return `diff --git ${JSON.stringify(`a/${oldPath}`)} ${JSON.stringify(`b/${change.path}`)}\n--- ${before}\n+++ ${after}\n${patch.text.trimStart()}`; + } + return patch.text; + } } const v1Patches = diffs.flatMap((diff) => { if (typeof diff.path !== "string" || typeof diff.newText !== "string") return []; From cfc8be97d733caa089ecd26198d1be9d56669126 Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Wed, 28 Oct 2026 00:51:34 -0700 Subject: [PATCH 44/46] ci: a stalled Node.js download retries after 2 minutes, not 10 (#26343) Co-authored-by: Claude Opus 5.5 --- .github/actions/setup-ci-deps/action.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/actions/setup-ci-deps/action.yml b/.github/actions/setup-ci-deps/action.yml index afaef17e1c310..e97365e2f3ec5 100644 --- a/.github/actions/setup-ci-deps/action.yml +++ b/.github/actions/setup-ci-deps/action.yml @@ -47,9 +47,14 @@ runs: echo "$HOME/.vite-plus/bin" >> "$GITHUB_PATH" "$HOME/.vite-plus/bin/vp" env use "${{ steps.vp.outputs.node }}" + # vp gives each Node.js download attempt 10 minutes by default, so one + # stalled stream holds the job that long. It retries 3 times and resumes + # partial bytes, so a shorter limit costs a slow download only a retry. - name: Setup Vite+ if: steps.vp_cache.outputs.cache-hit != 'true' uses: voidzero-dev/setup-vp@250f29ce396baf5e8f24498e17c0dfdebabc26eb # v1 + env: + VP_DOWNLOAD_TIMEOUT: "120" with: node-version-file: package.json cache: false From c3f5e363e59371d292f4a7e27008d2c5d1260725 Mon Sep 17 00:00:00 2001 From: Yash Singh Date: Wed, 28 Oct 2026 02:52:00 -0500 Subject: [PATCH 45/46] fix(server): avoid replaying stale Codex usage limits (#26331) --- .../Adapters/CodexAdapterV2.ts | 4 ++-- .../src/provider/codexUsageLimits.test.ts | 23 +++++++++++++++++++ apps/server/src/provider/codexUsageLimits.ts | 14 ++++++++++- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts index 36702689be9e8..e372b1b9466b0 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts @@ -4304,7 +4304,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( yield* client.handleServerNotification("account/rateLimits/updated", (payload) => Effect.gen(function* () { - yield* Ref.update(rateLimitSnapshot, (previous) => + const previous = yield* Ref.getAndUpdate(rateLimitSnapshot, (previous) => mergeCodexRateLimits(previous, payload.rateLimits), ); const resetAt = codexUsageLimitResetAt(yield* Ref.get(rateLimitSnapshot)); @@ -4324,7 +4324,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( turnItem: updated, }); } - const update = codexRateLimitsToUpdate(payload.rateLimits); + const update = codexRateLimitsToUpdate(payload.rateLimits, previous); if (update && adapterOptions.onUsageLimits) { const checkedAt = DateTime.formatIso(yield* DateTime.now); yield* adapterOptions.onUsageLimits({ ...update, checkedAt }); diff --git a/apps/server/src/provider/codexUsageLimits.test.ts b/apps/server/src/provider/codexUsageLimits.test.ts index 934412bd3bb41..9c863f54c0867 100644 --- a/apps/server/src/provider/codexUsageLimits.test.ts +++ b/apps/server/src/provider/codexUsageLimits.test.ts @@ -128,6 +128,29 @@ describe("codexRateLimitsToLimits", () => { }); describe("codexRateLimitsToUpdate", () => { + it("does not republish cached windows over a newer account probe", () => { + const cached = { + primary: { usedPercent: 0, resetsAt: 1_784_000_000, windowDurationMins: 300 }, + secondary: { usedPercent: 20, resetsAt: 1_784_500_000, windowDurationMins: 10080 }, + }; + expect(codexRateLimitsToUpdate(cached)?.windows).toHaveLength(2); + expect(codexRateLimitsToUpdate(structuredClone(cached), cached)).toBeUndefined(); + + const changed = { ...cached, secondary: { ...cached.secondary, usedPercent: 21 } }; + expect(codexRateLimitsToUpdate(changed, cached)?.windows.map((window) => window.id)).toEqual([ + "secondary", + ]); + expect(codexRateLimitsToUpdate({ primary: cached.primary }, cached)).toBeUndefined(); + expect( + codexRateLimitsToUpdate({ primary: { ...cached.primary, resetsAt: 1_784_001_000 } }, cached) + ?.windows, + ).toHaveLength(1); + expect( + codexRateLimitsToUpdate({ primary: { ...cached.primary, usedPercent: 76 } }, cached) + ?.windows[0]?.usedPercent, + ).toBe(76); + }); + it("carries only the windows the notification names", () => { expect( codexRateLimitsToUpdate({ diff --git a/apps/server/src/provider/codexUsageLimits.ts b/apps/server/src/provider/codexUsageLimits.ts index f8718cdda03d9..d4f9c621119df 100644 --- a/apps/server/src/provider/codexUsageLimits.ts +++ b/apps/server/src/provider/codexUsageLimits.ts @@ -135,8 +135,20 @@ export function codexRateLimitsToLimits(input: { export function codexRateLimitsToUpdate( snapshot: CodexRateLimitSnapshot, + previous?: CodexRateLimitSnapshot, ): ProviderUsageLimitsUpdate | undefined { - const windows = codexRateLimitsToWindows(snapshot); + const previousWindows = previous ? codexRateLimitsToWindows(previous) : []; + // Token ticks repeat session-cached limits that may predate an account probe or reset. + const windows = codexRateLimitsToWindows(snapshot).filter((window) => { + const existing = previousWindows.find((candidate) => candidate.id === window.id); + return ( + !existing || + existing.kind !== window.kind || + existing.usedPercent !== window.usedPercent || + existing.resetsAt !== window.resetsAt || + existing.windowDurationMins !== window.windowDurationMins + ); + }); return windows.length > 0 ? { windows } : undefined; } From ff424802edd1560fd99fe163e678b67ec09f69b3 Mon Sep 17 00:00:00 2001 From: Yash Singh Date: Wed, 28 Oct 2026 02:56:27 -0500 Subject: [PATCH 46/46] feat(web): mark PR files viewed with shift-click (#26349) --- .../pullRequest/PullRequestCodeTab.tsx | 29 +++++++++++++++---- .../pullRequest/pullRequestDiff.logic.test.ts | 17 +++++++++++ .../pullRequest/pullRequestDiff.logic.ts | 12 ++++++++ 3 files changed, 53 insertions(+), 5 deletions(-) diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx index f8d58cfaa020d..d1b165c62147b 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx @@ -106,6 +106,7 @@ import { toViewerSide, } from "./PullRequestReviewAnnotation"; import { + getFileViewedRange, isFileDiffCollapsed, isLineInFileDiff, toggleFileDiffFoldForViewed, @@ -175,7 +176,7 @@ interface FileViewedToggleState { readonly enabled: boolean; readonly isViewed: (path: string) => boolean; readonly isStale: (path: string) => boolean; - readonly setFileViewed: (fileKey: string, path: string, viewed: boolean) => void; + readonly setFileViewed: (fileKey: string, viewed: boolean, extendRange: boolean) => void; } // Context updates viewed toggles without invalidating memoized header portals. @@ -205,7 +206,13 @@ function FileViewedToggle({ viewedFiles.setFileViewed(fileKey, path, next === true)} + onCheckedChange={(next, details) => + viewedFiles.setFileViewed( + fileKey, + next === true, + "shiftKey" in details.event && details.event.shiftKey === true, + ) + } /> {stale ? ( @@ -287,6 +294,7 @@ function PullRequestCodeTab({ wordWrap: settings.wordWrap, })); const [toggledFiles, setToggledFiles] = useState>(() => new Set()); + const [lastViewedFileKey, setLastViewedFileKey] = useState(null); const [visibleCommitCount, setVisibleCommitCount] = useState(COMMIT_PAGE_SIZE); const [foldOverride, setFoldOverride] = useState(null); const effectiveFoldOverride = @@ -326,6 +334,7 @@ function PullRequestCodeTab({ const [stateScopeKey, setStateScopeKey] = useState(scopeKey); if (stateScopeKey !== scopeKey) { setStateScopeKey(scopeKey); + setLastViewedFileKey(null); setDraft(null); setSelectedLines(null); setToggledFiles(new Set()); @@ -689,10 +698,20 @@ function PullRequestCodeTab({ return next; }); - const setFileViewed = (fileKey: string, path: string, viewed: boolean) => { - setViewed(path, viewed); + const setFileViewed = (fileKey: string, viewed: boolean, extendRange: boolean) => { + const range = getFileViewedRange( + annotatedFiles, + fileKey, + extendRange ? lastViewedFileKey : null, + ); + setLastViewedFileKey(fileKey); + for (const file of range) setViewed(file.path, viewed); setToggledFiles((current) => - toggleFileDiffFoldForViewed(fileKey, viewed, effectiveFoldOverride, current), + range.reduce( + (next, file) => + toggleFileDiffFoldForViewed(file.fileKey, viewed, effectiveFoldOverride, next), + current, + ), ); }; const fileViewedToggle = { diff --git a/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts b/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts index 4e11e04e1d784..552fa748f07f6 100644 --- a/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts +++ b/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts @@ -2,6 +2,7 @@ import type { FileDiffMetadata } from "@pierre/diffs"; import { describe, expect, it } from "vite-plus/test"; import { + getFileViewedRange, isFileDiffCollapsed, isLineInFileDiff, toggleFileDiffFoldForViewed, @@ -54,3 +55,19 @@ describe("toggleFileDiffFoldForViewed", () => { expect(toggleFileDiffFoldForViewed("a.ts", false, "expanded", new Set()).size).toBe(0); }); }); + +describe("getFileViewedRange", () => { + const files = ["c.ts", "a.ts", "b.ts", "d.ts"].map((fileKey) => ({ fileKey })); + + it("includes both endpoints in displayed order in either direction", () => { + expect(getFileViewedRange(files, "b.ts", "c.ts")).toEqual(files.slice(0, 3)); + expect(getFileViewedRange(files, "c.ts", "b.ts")).toEqual(files.slice(0, 3)); + expect(getFileViewedRange(files, "a.ts", "a.ts")).toEqual([files[1]]); + }); + + it("changes only the clicked file without a current anchor", () => { + expect(getFileViewedRange(files, "a.ts", null)).toEqual([files[1]]); + expect(getFileViewedRange(files, "a.ts", "removed.ts")).toEqual([files[1]]); + expect(getFileViewedRange(files, "removed.ts", "a.ts")).toEqual([]); + }); +}); diff --git a/apps/web/src/components/pullRequest/pullRequestDiff.logic.ts b/apps/web/src/components/pullRequest/pullRequestDiff.logic.ts index 37fbfe30e9ab4..f175c0151431a 100644 --- a/apps/web/src/components/pullRequest/pullRequestDiff.logic.ts +++ b/apps/web/src/components/pullRequest/pullRequestDiff.logic.ts @@ -39,3 +39,15 @@ export function toggleFileDiffFoldForViewed( else next.add(fileKey); return next; } + +export function getFileViewedRange( + files: ReadonlyArray, + fileKey: string, + anchorFileKey: string | null, +): ReadonlyArray { + const index = files.findIndex((file) => file.fileKey === fileKey); + if (index === -1) return []; + const anchorIndex = files.findIndex((file) => file.fileKey === anchorFileKey); + if (anchorIndex === -1) return files.slice(index, index + 1); + return files.slice(Math.min(index, anchorIndex), Math.max(index, anchorIndex) + 1); +}