Skip to content

Commit 2fae83e

Browse files
Merge remote-tracking branch 'origin/main' into v2/delegated-steer-settle-race
2 parents 4397659 + 9f52dc2 commit 2fae83e

64 files changed

Lines changed: 1913 additions & 366 deletions

File tree

Some content is hidden

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

‎apps/desktop/src/preview/Manager.test.ts‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,8 @@ describe("PreviewManager", () => {
18861886
const sendCommand = vi.fn<(method: string, params?: unknown) => Promise<unknown>>(
18871887
async () => undefined,
18881888
);
1889+
const detachListeners = new Set<() => void>();
1890+
let chromiumDetaches = 0;
18891891
let destroyed = false;
18901892
const wc = {
18911893
id,
@@ -1914,19 +1916,30 @@ describe("PreviewManager", () => {
19141916
setIgnoreMenuShortcuts: vi.fn(),
19151917
setWindowOpenHandler: vi.fn(),
19161918
debugger: {
1917-
isAttached: () => attach.mock.calls.length > detach.mock.calls.length,
1919+
isAttached: () => attach.mock.calls.length > detach.mock.calls.length + chromiumDetaches,
19181920
attach,
19191921
detach,
19201922
sendCommand,
1921-
on: vi.fn(),
1922-
off: vi.fn(),
1923+
on: (event: string, listener: () => void) => {
1924+
if (event === "detach") detachListeners.add(listener);
1925+
},
1926+
off: (event: string, listener: () => void) => {
1927+
if (event === "detach") detachListeners.delete(listener);
1928+
},
19231929
},
19241930
};
1931+
/** Chromium dropping the debugger without a detach() call. */
1932+
const chromiumDetach = () => {
1933+
chromiumDetaches += 1;
1934+
// Snapshot like an EventEmitter, so a listener added during emit waits for the next one.
1935+
for (const listener of Array.from(detachListeners)) listener();
1936+
};
19251937
return {
19261938
wc: wc as unknown as Electron.WebContents,
19271939
attach,
19281940
detach,
19291941
sendCommand,
1942+
chromiumDetach,
19301943
destroy: () => {
19311944
destroyed = true;
19321945
listeners.get("destroyed")?.();
@@ -1965,6 +1978,24 @@ describe("PreviewManager", () => {
19651978
),
19661979
);
19671980

1981+
effectIt.effect("reattaches a debugger that Chromium dropped on its own", () =>
1982+
withManager((manager) =>
1983+
Effect.gen(function* () {
1984+
const guest = makeAttachingGuest(48);
1985+
fromId.mockReturnValue(guest.wc);
1986+
yield* manager.createTab("tab_detached");
1987+
yield* manager.registerWebview("tab_detached", 48);
1988+
yield* Effect.yieldNow;
1989+
expect(guest.attach).toHaveBeenCalledTimes(1);
1990+
1991+
guest.chromiumDetach();
1992+
yield* Effect.yieldNow;
1993+
yield* Effect.yieldNow;
1994+
expect(guest.attach).toHaveBeenCalledTimes(2);
1995+
}),
1996+
),
1997+
);
1998+
19681999
effectIt.effect("skips a guest destroyed while another guest's session opens", () =>
19692000
withManager((manager) =>
19702001
Effect.gen(function* () {

‎apps/desktop/src/preview/Manager.ts‎

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,13 +1023,22 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
10231023
copy.delete(webContentsId);
10241024
}),
10251025
]);
1026-
if (control) {
1027-
if (closedServerTab) browserHost.detach(closedServerTab);
1028-
for (const tab of (yield* SynchronizedRef.get(tabsRef)).values()) {
1029-
if (tab.webContentsId === webContentsId && tab.serverTab) browserHost.detach(tab.serverTab);
1026+
if (control) yield* releaseControlSession(control, closedServerTab);
1027+
});
1028+
1029+
// Closes a session already removed from `controlSessionsRef`. The server can
1030+
// only drive a tab while the desktop holds its debugger.
1031+
const releaseControlSession = Effect.fnUntraced(function* (
1032+
control: BrowserControlSession,
1033+
closedServerTab?: PreviewTabState["serverTab"],
1034+
) {
1035+
if (closedServerTab) browserHost.detach(closedServerTab);
1036+
for (const tab of (yield* SynchronizedRef.get(tabsRef)).values()) {
1037+
if (tab.webContentsId === control.webContentsId && tab.serverTab) {
1038+
browserHost.detach(tab.serverTab);
10301039
}
1031-
yield* Scope.close(control.scope, Exit.void).pipe(Effect.ignore);
10321040
}
1041+
yield* Scope.close(control.scope, Exit.void).pipe(Effect.ignore);
10331042
});
10341043

10351044
const ensureControlSession = Effect.fn("PreviewManager.ensureControlSession")(function* (
@@ -1141,23 +1150,36 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
11411150
const onMessage: BrowserControlSession["onMessage"] = (_event, method, params) => {
11421151
runFork(handleDebuggerMessage(method, params));
11431152
};
1144-
yield* Scope.addFinalizer(
1145-
scope,
1146-
attempt({ operation: "detachControlSession", webContentsId: wc.id }, () => {
1147-
wcDebugger.off("message", onMessage);
1148-
if (wcDebugger.isAttached()) wcDebugger.detach();
1149-
}).pipe(Effect.ignore),
1150-
);
11511153
const control: BrowserControlSession = {
11521154
webContentsId: wc.id,
11531155
debugger: wcDebugger,
11541156
semaphore,
11551157
scope,
11561158
onMessage,
11571159
};
1160+
// Electron emits `detach` when Chromium closes the debugger session on its
1161+
// own. The cached session would then fail every command until the webview
1162+
// is replaced.
1163+
let dropped = false;
1164+
const onDetach = () => {
1165+
dropped = true;
1166+
runFork(recoverDetachedControlSession(wc, control));
1167+
};
1168+
// The listener comes off before detach() so a detach this manager requested
1169+
// never looks like a dropped one. A dropped session has nothing left to
1170+
// detach: `wc.debugger` is shared, so detaching here would hit a successor.
1171+
yield* Scope.addFinalizer(
1172+
scope,
1173+
attempt({ operation: "detachControlSession", webContentsId: wc.id }, () => {
1174+
wcDebugger.off("message", onMessage);
1175+
wcDebugger.off("detach", onDetach);
1176+
if (!dropped && wcDebugger.isAttached()) wcDebugger.detach();
1177+
}).pipe(Effect.ignore),
1178+
);
11581179
const initialize = Effect.fn("PreviewManager.initializeControlSession")(function* () {
11591180
yield* attempt({ operation: "attachDebuggerListeners", webContentsId: wc.id }, () => {
11601181
wcDebugger.on("message", onMessage);
1182+
wcDebugger.on("detach", onDetach);
11611183
wcDebugger.attach("1.3");
11621184
});
11631185
// Without this, dark-scheme pages with no background of their own (text/plain, e.g.
@@ -2356,6 +2378,30 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
23562378
}
23572379
}).pipe(Effect.ignore);
23582380

2381+
// Only the session that saw the detach is replaced: by the time the event is
2382+
// handled the guest may already hold a newer one, which must stay.
2383+
const recoverDetachedControlSession = Effect.fn("PreviewManager.recoverDetachedControlSession")(
2384+
function* (wc: Electron.WebContents, detached: BrowserControlSession) {
2385+
const removed = yield* SynchronizedRef.modify(controlSessionsRef, (sessions) =>
2386+
sessions.get(wc.id) === detached
2387+
? ([
2388+
true,
2389+
replaceMap(sessions, (copy) => {
2390+
copy.delete(wc.id);
2391+
}),
2392+
] as const)
2393+
: ([false, sessions] as const),
2394+
);
2395+
if (!removed) return;
2396+
yield* releaseControlSession(detached);
2397+
if (wc.isDestroyed()) return;
2398+
const tabId = yield* tabIdForWebContents(wc.id);
2399+
if (tabId === null) return;
2400+
yield* restoreControlSession(tabId, wc);
2401+
},
2402+
(recover) => Effect.ignore(recover),
2403+
);
2404+
23592405
const setColorScheme = Effect.fn("PreviewManager.setColorScheme")(function* (
23602406
tabId: string,
23612407
colorScheme: DesktopPreviewColorScheme,

‎apps/mobile/src/connection/background-activity.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ export const layerReporter = Layer.effectDiscard(
8181
observedAt: DateTime.makeUnsafe(observedAtMs),
8282
}),
8383
)
84-
.pipe(Effect.ignore),
84+
// Not `ignore`: tearing down an environment mid-request interrupts its report,
85+
// and an interrupt leaking out here would end the reporting loop for good.
86+
.pipe(Effect.ignoreCause),
8587
{ concurrency: "unbounded", discard: true },
8688
);
8789
}).pipe(Effect.withSpan("mobile.backgroundActivity.report"));

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,9 +1302,7 @@ function renderFeedEntry(
13021302
accessibilityLabel={intentBadge.accessibilityLabel}
13031303
className="flex-row items-center gap-1"
13041304
>
1305-
{intentBadge.icon ? (
1306-
<SymbolView name={intentBadge.icon} size={12} tintColor={iconSubtleColor} />
1307-
) : null}
1305+
<SymbolView name="arrow.uturn.forward" size={12} tintColor={iconSubtleColor} />
13081306
<Text className="font-t3-medium text-xs text-foreground-secondary">
13091307
{intentBadge.label}
13101308
</Text>

‎apps/mobile/src/features/threads/userMessageIntentBadge.test.ts‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,23 @@ import { describe, expect, it } from "@effect/vitest";
33
import { resolveUserMessageIntentBadge } from "./userMessageIntentBadge";
44

55
describe("user message intent badge", () => {
6-
it("does not label ordinary turn-start messages", () => {
6+
it("does not label turn-start or queued messages", () => {
77
expect(resolveUserMessageIntentBadge(undefined)).toBeNull();
88
expect(resolveUserMessageIntentBadge("turn_start")).toBeNull();
9-
});
10-
11-
it("explains the queued history without claiming delivery", () => {
12-
expect(resolveUserMessageIntentBadge("queued_turn")).toEqual({
13-
label: "From queue",
14-
accessibilityLabel: "Queued while the agent was busy",
15-
icon: null,
16-
});
9+
expect(resolveUserMessageIntentBadge("queued_turn")).toBeNull();
1710
});
1811

1912
it("explains the intent to steer without claiming delivery", () => {
2013
expect(resolveUserMessageIntentBadge("steer")).toEqual({
2114
label: "Steer",
2215
accessibilityLabel: "Steered the active turn",
23-
icon: "arrow.uturn.forward",
2416
});
2517
});
2618

2719
it("preserves the queued origin after promotion to steer", () => {
2820
expect(resolveUserMessageIntentBadge("promoted_queued_to_steer")).toEqual({
2921
label: "Steer",
3022
accessibilityLabel: "Originally queued, then promoted to steer the active turn",
31-
icon: "arrow.uturn.forward",
3223
});
3324
});
3425
});

‎apps/mobile/src/features/threads/userMessageIntentBadge.ts‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,25 @@
11
import type { OrchestrationV2UserMessageInputIntent } from "@t3tools/contracts";
22

3-
import type { AppSymbolName } from "../../components/ui/AppSymbol";
4-
53
export interface UserMessageIntentBadgePresentation {
64
readonly label: string;
75
readonly accessibilityLabel: string;
8-
readonly icon: AppSymbolName | null;
96
}
107

118
export function resolveUserMessageIntentBadge(
129
intent: OrchestrationV2UserMessageInputIntent | undefined,
1310
): UserMessageIntentBadgePresentation | null {
1411
switch (intent) {
15-
case "queued_turn":
16-
return {
17-
label: "From queue",
18-
accessibilityLabel: "Queued while the agent was busy",
19-
icon: null,
20-
};
2112
case "steer":
2213
return {
2314
label: "Steer",
2415
accessibilityLabel: "Steered the active turn",
25-
icon: "arrow.uturn.forward",
2616
};
2717
case "promoted_queued_to_steer":
2818
return {
2919
label: "Steer",
3020
accessibilityLabel: "Originally queued, then promoted to steer the active turn",
31-
icon: "arrow.uturn.forward",
3221
};
22+
case "queued_turn":
3323
case "turn_start":
3424
case undefined:
3525
return null;

‎apps/server/src/binCli.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { browserCommand } from "./cli/browser.ts";
1515
import { connectCommand } from "./cli/connect.ts";
1616
import { pairCommand } from "./cli/pair.ts";
1717
import { hasCloudPublicConfig } from "./cloud/publicConfig.ts";
18-
import { keepOutboundFetchOnHttp1 } from "./outboundHttp.ts";
18+
import { extendConnectionAttemptTimeout, keepOutboundFetchOnHttp1 } from "./outboundHttp.ts";
1919
import { sharedServerCommandFlags } from "./cli/config.ts";
2020
import { projectCommand } from "./cli/project.ts";
2121
import { runDefaultServerCommand, serveCommand, startCommand } from "./cli/server.ts";
@@ -95,6 +95,7 @@ export const makeCli = ({ cloudEnabled = hasCloudPublicConfig } = {}) =>
9595
export const cli = makeCli();
9696

9797
export function runCli() {
98+
extendConnectionAttemptTimeout();
9899
keepOutboundFetchOnHttp1();
99100
Command.run(cli, { version: packageJson.version }).pipe(
100101
Effect.scoped,

0 commit comments

Comments
 (0)