Skip to content

Commit 884313b

Browse files
Merge remote-tracking branch 'origin/main' into factory/issue-11985-a1794473966
2 parents aeac2bf + 3ec8347 commit 884313b

63 files changed

Lines changed: 2728 additions & 284 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/BrowserImport/BrowserImport.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const layerRejectedBeforeSession = Layer.succeed(
3333
isPartition: () => false,
3434
getSession: () => Effect.die("getSession must not be reached"),
3535
clearCookies: () => Effect.die("clearCookies must not be reached"),
36+
cookiesCleared: () => Effect.die("cookiesCleared must not be reached"),
3637
clearCache: () => Effect.die("clearCache must not be reached"),
3738
}),
3839
);

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

Lines changed: 325 additions & 5 deletions
Large diffs are not rendered by default.

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

Lines changed: 337 additions & 15 deletions
Large diffs are not rendered by default.

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export interface DesktopBrowserTabKey {
4040
export interface DesktopBrowserTabDebugger {
4141
readonly webContents: Electron.WebContents;
4242
readonly debugger: Electron.Debugger;
43+
/** Runs after the server clears the tab's cookies, so state kept beside them goes too. */
44+
readonly cookiesCleared?: () => Promise<void>;
4345
}
4446

4547
const keyOf = ({ threadId, tabId }: DesktopBrowserTabKey) => `${threadId}\u0000${tabId}`;
@@ -315,9 +317,12 @@ export const make = Effect.gen(function* () {
315317

316318
const relayFor = (tab: AttachedTab) => {
317319
if (tab.relay) return tab.relay;
318-
const { webContents, debugger: debuggee } = tab.debuggee;
319-
const send = (method: string, params: Record<string, unknown>, sessionId?: string) =>
320-
sendPageCommand(webContents, debuggee, method, params, sessionId);
320+
const { webContents, debugger: debuggee, cookiesCleared } = tab.debuggee;
321+
const send = async (method: string, params: Record<string, unknown>, sessionId?: string) => {
322+
const result = await sendPageCommand(webContents, debuggee, method, params, sessionId);
323+
if (method === "Network.clearBrowserCookies") await cookiesCleared?.();
324+
return result;
325+
};
321326
const detachedOwners = new Set<string>();
322327
/** Viewers and recordings stream; the agent's own sessions never do. */
323328
const streamingOwners = new Set<string>();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ const layerBrowserSession = Layer.succeed(
205205
isPartition: (partition) => partition.startsWith("persist:t3code-preview-"),
206206
getSession: () => Effect.succeed(previewSession as unknown as Electron.Session),
207207
clearCookies: () => Effect.void,
208+
cookiesCleared: () => Effect.void,
208209
clearCache: () => Effect.void,
209210
}),
210211
);

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,9 +563,11 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
563563
const random = yield* SecureRandom.SecureRandom;
564564
const parentScope = yield* Scope.Scope;
565565
const browserHost = yield* DesktopBrowserHost.DesktopBrowserHost;
566+
const browserSession = yield* BrowserSession.BrowserSession;
566567
const passkeys = yield* PreviewPasskeys.PreviewPasskeys;
567568
const context = yield* Effect.context<never>();
568569
const runFork = Effect.runForkWith(context);
570+
const runPromise = Effect.runPromiseWith(context);
569571
const resolvedArtifactDirectory = path.resolve(artifactDirectory);
570572

571573
let forwardedShortcuts: ReadonlyArray<PreviewForwardedShortcut> = [];
@@ -2472,7 +2474,11 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
24722474
return;
24732475
}
24742476
if (afterAttach.serverTab) {
2475-
browserHost.attach(afterAttach.serverTab, { webContents: wc, debugger: control.debugger });
2477+
browserHost.attach(afterAttach.serverTab, {
2478+
webContents: wc,
2479+
debugger: control.debugger,
2480+
cookiesCleared: () => runPromise(browserSession.cookiesCleared(wc.session)),
2481+
});
24762482
// CDP cannot recover a failure that happened before it attached.
24772483
const { navStatus } = afterAttach;
24782484
if (navStatus.kind === "LoadFailed") {

‎apps/desktop/src/shell/DesktopShellEnvironment.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,52 @@ describe("DesktopShellEnvironment", () => {
637637
}),
638638
);
639639

640+
it.effect("recovers the login-shell PATH when the first probe outlasts the timeout", () =>
641+
Effect.gen(function* () {
642+
const env: NodeJS.ProcessEnv = { SHELL: "/bin/zsh", PATH: "/usr/bin:/bin:/usr/sbin:/sbin" };
643+
const output = envOutput({ PATH: "/opt/homebrew/bin:/usr/bin" });
644+
let shellProbes = 0;
645+
yield* runShellEnvironment({
646+
env,
647+
platform: "darwin",
648+
handler: () => "",
649+
processSpawner: ChildProcessSpawner.make((command) => {
650+
if (command._tag !== "StandardCommand" || command.command !== "/bin/zsh") {
651+
return Effect.succeed(makeProcess(""));
652+
}
653+
shellProbes += 1;
654+
if (shellProbes > 1) return Effect.succeed(makeProcess(output));
655+
// A cold start: the first login shell answers, but only after 7s.
656+
const slow = Effect.sleep("7 seconds");
657+
const stdout = Stream.fromEffect(slow.pipe(Effect.as(textEncoder.encode(output))));
658+
return Effect.succeed(
659+
ChildProcessSpawner.makeHandle({
660+
pid: ChildProcessSpawner.ProcessId(125),
661+
stdout,
662+
stderr: Stream.empty,
663+
all: stdout,
664+
exitCode: slow.pipe(Effect.as(ChildProcessSpawner.ExitCode(0))),
665+
isRunning: Effect.succeed(true),
666+
kill: () => Effect.void,
667+
stdin: Sink.drain,
668+
getInputFd: () => Sink.drain,
669+
getOutputFd: () => Stream.empty,
670+
unref: Effect.succeed(Effect.void),
671+
}),
672+
);
673+
}),
674+
use: (service) =>
675+
Effect.gen(function* () {
676+
yield* service.installIntoProcess;
677+
yield* TestClock.adjust("10 minutes");
678+
return true;
679+
}),
680+
}).pipe(Effect.provide(Logger.layer([])));
681+
682+
assert.equal(env.PATH, "/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin");
683+
}),
684+
);
685+
640686
it.effect("loads PowerShell profile environment on Windows", () =>
641687
Effect.gen(function* () {
642688
const env: NodeJS.ProcessEnv = {

‎apps/desktop/src/shell/DesktopShellEnvironment.ts‎

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ const LOCALE_ENV_NAMES = ["LANG", "LC_ALL", "LC_CTYPE"] as const;
117117
const FALLBACK_LC_CTYPE = "en_US.UTF-8";
118118
const WINDOWS_SHELL_CANDIDATES = ["pwsh.exe", "powershell.exe"] as const;
119119
const LOGIN_SHELL_TIMEOUT = Duration.seconds(5);
120+
// A profile can be slow only while the machine boots; one longer wait covers it.
121+
const SLOW_LOGIN_SHELL_TIMEOUT = Duration.seconds(20);
120122
const LAUNCHCTL_TIMEOUT = Duration.seconds(2);
121123
const PROCESS_TERMINATE_GRACE = Duration.seconds(1);
122124

@@ -261,7 +263,11 @@ const runCommandOutput = Effect.fn("desktop.shellEnvironment.runCommandOutput")(
261263
readonly env: NodeJS.ProcessEnv;
262264
readonly timeout: Duration.Duration;
263265
readonly shell?: boolean;
264-
}): Effect.fn.Return<string, never, ChildProcessSpawner.ChildProcessSpawner> {
266+
}): Effect.fn.Return<
267+
string,
268+
DesktopShellEnvironmentCommandTimeoutError,
269+
ChildProcessSpawner.ChildProcessSpawner
270+
> {
265271
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
266272
const fiber = yield* Effect.scoped(
267273
Effect.gen(function* () {
@@ -317,29 +323,42 @@ const runCommandOutput = Effect.fn("desktop.shellEnvironment.runCommandOutput")(
317323
return output.value;
318324
}
319325

320-
const error = new DesktopShellEnvironmentCommandTimeoutError({
326+
return yield* new DesktopShellEnvironmentCommandTimeoutError({
321327
probe: input.probe,
322328
executable: executableName(input.command),
323329
argumentCount: input.args.length,
324330
timeoutMs: Duration.toMillis(input.timeout),
325331
});
326-
yield* logShellEnvironmentCommandError(error);
327-
return "";
328332
});
329333

334+
const orEmptyOnTimeout = <R>(
335+
effect: Effect.Effect<string, DesktopShellEnvironmentCommandTimeoutError, R>,
336+
): Effect.Effect<string, never, R> =>
337+
effect.pipe(
338+
Effect.catchTags({
339+
DesktopShellEnvironmentCommandTimeoutError: (error) =>
340+
logShellEnvironmentCommandError(error).pipe(Effect.as("")),
341+
}),
342+
);
343+
330344
const readLoginShellEnvironment = (
331345
shell: string,
332346
names: ReadonlyArray<string>,
333347
env: NodeJS.ProcessEnv,
334-
): Effect.Effect<EnvironmentPatch, never, ChildProcessSpawner.ChildProcessSpawner> =>
348+
timeout: Duration.Duration,
349+
): Effect.Effect<
350+
EnvironmentPatch,
351+
DesktopShellEnvironmentCommandTimeoutError,
352+
ChildProcessSpawner.ChildProcessSpawner
353+
> =>
335354
names.length === 0
336355
? Effect.succeed({})
337356
: runCommandOutput({
338357
probe: "login-shell",
339358
command: shell,
340359
args: ["-ilc", buildEnvironmentCaptureCommand(shell, names)],
341360
env,
342-
timeout: LOGIN_SHELL_TIMEOUT,
361+
timeout,
343362
}).pipe(Effect.map((output) => extractEnvironment(output, names)));
344363

345364
const readLaunchctlPath = (env: NodeJS.ProcessEnv) =>
@@ -349,7 +368,7 @@ const readLaunchctlPath = (env: NodeJS.ProcessEnv) =>
349368
command: "/bin/launchctl",
350369
args: ["getenv", "PATH"],
351370
timeout: LAUNCHCTL_TIMEOUT,
352-
}).pipe(Effect.map(trimNonEmpty));
371+
}).pipe(orEmptyOnTimeout, Effect.map(trimNonEmpty));
353372

354373
const readWindowsEnvironment = Effect.fn("desktop.shellEnvironment.readWindowsEnvironment")(
355374
function* (
@@ -374,7 +393,7 @@ const readWindowsEnvironment = Effect.fn("desktop.shellEnvironment.readWindowsEn
374393
args,
375394
env,
376395
timeout: LOGIN_SHELL_TIMEOUT,
377-
});
396+
}).pipe(orEmptyOnTimeout);
378397
const environment = extractEnvironment(output, names);
379398
if (Object.keys(environment).length > 0) {
380399
return environment;
@@ -429,10 +448,27 @@ const installPosixEnvironment = Effect.fn("desktop.shellEnvironment.installPosix
429448
const userShells = [config.env.SHELL?.trim(), readUserLoginShell()];
430449

431450
let answeredBy: string | undefined;
451+
let waitedForSlowShell = false;
432452
for (const shell of listLoginShellCandidates(config.platform, config.env.SHELL)) {
453+
const read = (timeout: Duration.Duration) =>
454+
readLoginShellEnvironment(shell, LOGIN_SHELL_ENV_NAMES, config.env, timeout);
433455
Object.assign(
434456
shellEnvironment,
435-
yield* readLoginShellEnvironment(shell, LOGIN_SHELL_ENV_NAMES, config.env),
457+
yield* read(LOGIN_SHELL_TIMEOUT).pipe(
458+
Effect.catchTags({
459+
DesktopShellEnvironmentCommandTimeoutError: (error) => {
460+
const retry = !waitedForSlowShell;
461+
waitedForSlowShell = true;
462+
return logShellEnvironmentCommandError(error).pipe(
463+
Effect.andThen(retry ? read(SLOW_LOGIN_SHELL_TIMEOUT) : Effect.succeed({})),
464+
);
465+
},
466+
}),
467+
Effect.catchTags({
468+
DesktopShellEnvironmentCommandTimeoutError: (error) =>
469+
logShellEnvironmentCommandError(error).pipe(Effect.as({})),
470+
}),
471+
),
436472
);
437473
if (shellEnvironment.PATH) {
438474
answeredBy = shell;

‎apps/server/src/attachmentStore.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,21 @@ describe("threadHtmlRenderAttachmentIds", () => {
176176
).toEqual([own]);
177177
});
178178

179+
it("includes pages an OpenCode Code Mode execute script returned", () => {
180+
const own = createAttachmentId("thread-a", "html")!;
181+
expect(
182+
threadHtmlRenderAttachmentIds("thread-a", [
183+
{
184+
toolName: "execute",
185+
output: JSON.stringify({
186+
htmlRender: { attachmentId: own, title: "x", height: 300 },
187+
message: "Shown to the reader above your reply.",
188+
}),
189+
},
190+
]),
191+
).toEqual([own]);
192+
});
193+
179194
it("includes captured MCP App documents from any tool", () => {
180195
const app = createAttachmentId("thread-a", "html")!;
181196
expect(

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,17 @@ import {
3030
import { checkpointWorkspace } from "@t3tools/provider-testing/replayWorkspace";
3131
import * as DateTime from "effect/DateTime";
3232
import * as Deferred from "effect/Deferred";
33+
import * as Duration from "effect/Duration";
3334
import * as Effect from "effect/Effect";
3435
import * as Fiber from "effect/Fiber";
3536
import * as Layer from "effect/Layer";
37+
import * as Option from "effect/Option";
3638
import * as Queue from "effect/Queue";
3739
import * as Ref from "effect/Ref";
3840
import * as Result from "effect/Result";
3941
import * as Schema from "effect/Schema";
4042
import * as Stream from "effect/Stream";
43+
import * as TestClock from "effect/testing/TestClock";
4144

4245
import { CodexProviderCapabilitiesV2 } from "../orchestration-v2/Adapters/CodexAdapterV2.ts";
4346
import * as CommandReceiptStore from "../orchestration-v2/CommandReceiptStore.ts";
@@ -1274,6 +1277,7 @@ describe("OrchestratorMcpService provider resolution", () => {
12741277
"child run completion",
12751278
"child question",
12761279
"child approval",
1280+
"Node fetch's 300 s idle timeout",
12771281
] as const)("a task wait still returns on %s", (wake) =>
12781282
Effect.gen(function* () {
12791283
const events = yield* Queue.unbounded<OrchestrationV2StoredEvent>();
@@ -1379,6 +1383,24 @@ describe("OrchestratorMcpService provider resolution", () => {
13791383
.delegateTask(scope, { task: "Review the change.", mode: "wait" })
13801384
.pipe(Effect.forkChild);
13811385
yield* Deferred.await(subscribed);
1386+
if (wake === "Node fetch's 300 s idle timeout") {
1387+
// Nothing is written during the wait, so undici drops the call at 300 s, before any handle.
1388+
const beforeClientTimeout = yield* Fiber.join(waiting).pipe(
1389+
Effect.timeoutOption(Duration.seconds(299)),
1390+
Effect.forkChild,
1391+
);
1392+
yield* TestClock.adjust(Duration.seconds(299));
1393+
const result = yield* Fiber.join(beforeClientTimeout);
1394+
assert.isTrue(
1395+
Option.isSome(result),
1396+
"a wait without timeoutMs was still silent at 299 s, so a Node MCP client loses the taskId",
1397+
);
1398+
if (Option.isSome(result)) {
1399+
assert.equal(result.value.taskId, taskId);
1400+
assert.isTrue(result.value.waitTimedOut);
1401+
}
1402+
return;
1403+
}
13821404
ready = true;
13831405
if (wake === "parent completion") {
13841406
yield* Queue.offer(events, {

0 commit comments

Comments
 (0)