Skip to content

Commit cd376c4

Browse files
Merge remote-tracking branch 'origin/main' into takeover/pr-18465
2 parents 81e85c3 + 3ea253f commit cd376c4

250 files changed

Lines changed: 10388 additions & 767 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.

‎.agents/skills/test-t3-mobile/SKILL.md‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,15 @@ prints one JSON line; on failure it names the next command. Mobile verification
4343
includes that step unless the user prohibits it.
4444

4545
For a device you opened in an agent-device session elsewhere (a device pool,
46-
cloud, or remote host), run `vp run mobile:install <ios|android> --session <s>`
47-
from the directory you opened the session in. It installs the cached build into
48-
that session (`--abi arm64-v8a` for an ARM Android device; `--metro <url>` also
49-
opens it against Metro) and never builds.
46+
cloud, or remote host), run `vp run mobile:install <ios|android> --session <s>
47+
--config <file>` from the directory you opened the session in, passing the
48+
`--config` file from `device_open`'s `targetArgs`. Without it, a device pool's
49+
gateway refuses the install, wrongly saying agent-device is too old. It installs
50+
the cached build into that session (`--abi arm64-v8a` for an ARM Android device;
51+
pool Android emulators are x86_64, the default; `--metro <url>` also opens it
52+
against Metro). Without a cached build for this checkout it builds first, which can
53+
outlast the lease, so run `vp run mobile:install <ios|android> --prepare` (with
54+
the same `--abi`) before you open the device.
5055

5156
Start `vp run dev:client` from `apps/mobile`, or reuse a healthy Metro belonging
5257
to this checkout. Open its printed development-client URL with AgentDevice

‎apps/desktop/src/app/DesktopApp.test.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ it.effect.each([false, true])(
8686
const historyStarted = yield* Deferred.make<void>();
8787
const releaseHistory = yield* Deferred.make<void>();
8888
const quitting = yield* Ref.make(false);
89+
const tokenRequested = yield* Deferred.make<void>();
8990
const events: string[] = [];
9091
const record = (event: string) =>
9192
Effect.sync(() => {
@@ -144,7 +145,7 @@ it.effect.each([false, true])(
144145
configure: Effect.void,
145146
} as PreviewPasskeys.PreviewPasskeys["Service"]),
146147
Effect.provideService(DesktopApplicationMenu.DesktopApplicationMenu, {
147-
configure: Effect.void,
148+
configure: record("menu"),
148149
}),
149150
Effect.provideService(DesktopUpdates.DesktopUpdates, {
150151
configure: Effect.void,
@@ -222,7 +223,12 @@ it.effect.each([false, true])(
222223
Layer.mock(DesktopClientSettings.DesktopClientSettings, {}),
223224
Layer.mock(DesktopConnectionCatalogStore.DesktopConnectionCatalogStore, {}),
224225
Layer.mock(DesktopIpc.DesktopIpc, {}),
225-
Layer.mock(DesktopLocalEnvironmentAuth.DesktopLocalEnvironmentAuth, {}),
226+
Layer.mock(DesktopLocalEnvironmentAuth.DesktopLocalEnvironmentAuth, {
227+
getBearerToken: record("token").pipe(
228+
Effect.andThen(Deferred.succeed(tokenRequested, undefined)),
229+
Effect.as("token"),
230+
),
231+
}),
226232
Layer.mock(DesktopManagedPolicy.DesktopManagedPolicy, {}),
227233
Layer.succeed(DesktopPrimaryPaste.DesktopPrimaryPaste, { enabled: true }),
228234
Layer.mock(DesktopSshEnvironment.DesktopSshEnvironment, {}),
@@ -253,7 +259,13 @@ it.effect.each([false, true])(
253259
);
254260
yield* Deferred.await(registrationStarted);
255261
yield* Deferred.await(windowStarted);
256-
assert.deepEqual(events, ["identity", "ready", "identity", "backend", "window"]);
262+
yield* Deferred.await(tokenRequested);
263+
// The backend spawns before menus, and its token is minted without waiting for the renderer.
264+
assert.deepEqual(
265+
events.filter((event) => event !== "token"),
266+
["identity", "ready", "identity", "backend", "menu", "window"],
267+
);
268+
assert.isAbove(events.indexOf("token"), events.indexOf("backend"));
257269
assert.isFalse(yield* Deferred.isDone(registrationStopped));
258270
const shutdown = yield* DesktopShutdown.DesktopShutdown;
259271
yield* shutdown.request;

‎apps/desktop/src/app/DesktopApp.ts‎

Lines changed: 81 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import * as PreviewPasskeys from "../preview/Passkeys.ts";
2222
import * as DesktopBackendManager from "../backend/DesktopBackendManager.ts";
2323
import * as DesktopBackendPool from "../backend/DesktopBackendPool.ts";
2424
import * as DesktopHomeOwner from "../backend/DesktopHomeOwner.ts";
25+
import * as DesktopLocalEnvironmentAuth from "../backend/DesktopLocalEnvironmentAuth.ts";
2526
import * as DesktopEnvironment from "./DesktopEnvironment.ts";
2627
import * as DesktopLegacyLocalStorage from "./DesktopLegacyLocalStorage.ts";
2728
import * as DesktopLifecycle from "./DesktopLifecycle.ts";
@@ -168,13 +169,12 @@ const stopAllPoolInstances = Effect.fn("desktop.app.stopAllPoolInstances")(
168169
},
169170
);
170171

171-
const bootstrap = Effect.gen(function* () {
172+
// Spawns the local backend as soon as the protocol, IPC, and legacy storage that every window needs exist.
173+
const startLocalBackend = Effect.gen(function* () {
172174
const state = yield* DesktopState.DesktopState;
173175
const environment = yield* DesktopEnvironment.DesktopEnvironment;
174176
const desktopSettings = yield* DesktopAppSettings.DesktopAppSettings;
175177
const desktopWindow = yield* DesktopWindow.DesktopWindow;
176-
const snapShot = yield* DesktopSnapShot.DesktopSnapShot;
177-
const appActivation = yield* DesktopAppActivation.DesktopAppActivation;
178178
yield* logBootstrapInfo("bootstrap start");
179179

180180
const settings = yield* desktopSettings.get;
@@ -193,32 +193,13 @@ const bootstrap = Effect.gen(function* () {
193193
yield* (yield* DesktopAppIdentity.DesktopAppIdentity).resolveUserDataPath,
194194
);
195195

196-
yield* snapShot.initialize;
197-
// Only the user's own login shell PATH shows every t3 a new terminal runs; a fresh install has
198-
// no cached one. Without it, the command is left to Settings.
199-
const installCli = (yield* DesktopCliCommand.DesktopCliCommand).installOnFirstLaunch;
200-
yield* Effect.forkScoped(
201-
(yield* DesktopShellEnvironment.DesktopShellEnvironment).awaitUserShellDiscovery.pipe(
202-
Effect.flatMap((found) =>
203-
found || environment.platform === "win32"
204-
? installCli
205-
: logBootstrapInfo("skipped installing the t3 command without the shell PATH"),
206-
),
207-
),
208-
);
209-
210196
if (!settings.localEnvironmentEnabled) {
211197
yield* logBootstrapInfo("bootstrap skipping local environment (disabled in settings)");
212-
if (!(yield* Ref.get(state.quitting))) {
213-
yield* desktopWindow.createMainIfBackendReady;
214-
}
215-
return;
198+
return false;
216199
}
217200

218-
const pool = yield* DesktopBackendPool.DesktopBackendPool;
219-
const primaryBackend = yield* pool.primary;
201+
const primaryBackend = yield* (yield* DesktopBackendPool.DesktopBackendPool).primary;
220202
const serverExposure = yield* DesktopServerExposure.DesktopServerExposure;
221-
const wslBackend = yield* DesktopWslBackend.DesktopWslBackend;
222203

223204
if (environment.isDevelopment && Option.isNone(environment.configuredBackendPort)) {
224205
return yield* new DesktopDevelopmentBackendPortRequiredError();
@@ -237,7 +218,8 @@ const bootstrap = Effect.gen(function* () {
237218
"This T3 home is unavailable",
238219
DesktopHomeOwner.homeOwnedMessage(owner),
239220
);
240-
return yield* (yield* ElectronApp.ElectronApp).quit;
221+
yield* (yield* ElectronApp.ElectronApp).quit;
222+
return false;
241223
}
242224

243225
const backendPortSelection = yield* resolveDesktopBackendPort(environment.configuredBackendPort);
@@ -275,29 +257,73 @@ const bootstrap = Effect.gen(function* () {
275257
);
276258
}
277259

278-
if (!(yield* Ref.get(state.quitting))) {
279-
if (settings.wslOnly === true && settings.wslBackendEnabled === true) {
280-
yield* desktopWindow.showConnectingSplash;
281-
}
282-
yield* primaryBackend.start;
283-
yield* logBootstrapInfo("bootstrap backend start requested");
284-
// WSL-only fallback can change the primary endpoint before readiness.
285-
const config = yield* primaryBackend.currentConfig;
286-
if (
287-
!environment.isDevelopment &&
288-
!settings.wslOnly &&
289-
Option.isSome(config) &&
290-
Option.isNone(config.value.preflightFailure)
291-
) {
292-
yield* desktopWindow.ensureMain;
260+
if (yield* Ref.get(state.quitting)) {
261+
return false;
262+
}
263+
if (settings.wslOnly === true && settings.wslBackendEnabled === true) {
264+
yield* desktopWindow.showConnectingSplash;
265+
}
266+
yield* primaryBackend.start;
267+
yield* logBootstrapInfo("bootstrap backend start requested");
268+
// The renderer asks for this token first; minting it at readiness saves a round trip.
269+
yield* Effect.forkScoped(
270+
(yield* DesktopLocalEnvironmentAuth.DesktopLocalEnvironmentAuth).getBearerToken.pipe(
271+
Effect.ignore,
272+
),
273+
);
274+
return true;
275+
}).pipe(Effect.withSpan("desktop.startLocalBackend"));
276+
277+
const bootstrap = Effect.fn("desktop.bootstrap")(function* (backendStarted: boolean) {
278+
const state = yield* DesktopState.DesktopState;
279+
const environment = yield* DesktopEnvironment.DesktopEnvironment;
280+
const desktopSettings = yield* DesktopAppSettings.DesktopAppSettings;
281+
const desktopWindow = yield* DesktopWindow.DesktopWindow;
282+
const snapShot = yield* DesktopSnapShot.DesktopSnapShot;
283+
const appActivation = yield* DesktopAppActivation.DesktopAppActivation;
284+
const settings = yield* desktopSettings.get;
285+
if (settings.localEnvironmentEnabled && (!backendStarted || (yield* Ref.get(state.quitting)))) {
286+
return;
287+
}
288+
289+
yield* snapShot.initialize;
290+
// Only the user's own login shell PATH shows every t3 a new terminal runs; a fresh install has
291+
// no cached one. Without it, the command is left to Settings.
292+
const installCli = (yield* DesktopCliCommand.DesktopCliCommand).installOnFirstLaunch;
293+
yield* Effect.forkScoped(
294+
(yield* DesktopShellEnvironment.DesktopShellEnvironment).awaitUserShellDiscovery.pipe(
295+
Effect.flatMap((found) =>
296+
found || environment.platform === "win32"
297+
? installCli
298+
: logBootstrapInfo("skipped installing the t3 command without the shell PATH"),
299+
),
300+
),
301+
);
302+
303+
if (!settings.localEnvironmentEnabled) {
304+
if (!(yield* Ref.get(state.quitting))) {
305+
yield* desktopWindow.createMainIfBackendReady;
293306
}
294-
yield* appActivation.start.pipe(
295-
Effect.tap(() => logBootstrapInfo("desktop app control socket ready")),
296-
Effect.catch((error) => logStartupError("desktop app control socket unavailable", { error })),
297-
);
298-
yield* Effect.forkScoped(wslBackend.reconcile);
307+
return;
299308
}
300-
}).pipe(Effect.withSpan("desktop.bootstrap"));
309+
310+
const primaryBackend = yield* (yield* DesktopBackendPool.DesktopBackendPool).primary;
311+
// WSL-only fallback can change the primary endpoint before readiness.
312+
const config = yield* primaryBackend.currentConfig;
313+
if (
314+
!environment.isDevelopment &&
315+
!settings.wslOnly &&
316+
Option.isSome(config) &&
317+
Option.isNone(config.value.preflightFailure)
318+
) {
319+
yield* desktopWindow.ensureMain;
320+
}
321+
yield* appActivation.start.pipe(
322+
Effect.tap(() => logBootstrapInfo("desktop app control socket ready")),
323+
Effect.catch((error) => logStartupError("desktop app control socket unavailable", { error })),
324+
);
325+
yield* Effect.forkScoped((yield* DesktopWslBackend.DesktopWslBackend).reconcile);
326+
});
301327

302328
const startup = Effect.gen(function* () {
303329
const appIdentity = yield* DesktopAppIdentity.DesktopAppIdentity;
@@ -369,14 +395,20 @@ const startup = Effect.gen(function* () {
369395
});
370396
}
371397
yield* appIdentity.configure;
372-
yield* previewPasskeys.configure;
373-
yield* applicationMenu.configure;
398+
// Sets canInstallUpdates, which the backend bootstrap payload reads.
374399
yield* updates.configure;
375400
yield* DesktopRemoteUpdates.listen;
376401
yield* DesktopStartupRecovery.listen;
402+
const backendStarted = yield* startLocalBackend.pipe(
403+
Effect.catchCause((cause) => fatalStartupCause("bootstrap", cause)),
404+
);
405+
yield* previewPasskeys.configure;
406+
yield* applicationMenu.configure;
377407
// Portal identity is prepared pre-ready; MIME helpers must not gate the backend or window.
378408
yield* linuxUrlHandler.register.pipe(Effect.forkScoped);
379-
yield* bootstrap.pipe(Effect.catchCause((cause) => fatalStartupCause("bootstrap", cause)));
409+
yield* bootstrap(backendStarted).pipe(
410+
Effect.catchCause((cause) => fatalStartupCause("bootstrap", cause)),
411+
);
380412
}).pipe(Effect.withSpan("desktop.startup"));
381413

382414
const scopedProgram = Effect.scoped(

‎apps/desktop/src/backend/DesktopBackendConfiguration.test.ts‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,43 @@ describe("DesktopBackendConfiguration", () => {
683683
),
684684
);
685685

686+
it.effect("gives the Windows and WSL backends one host scope that survives a restart", () =>
687+
Effect.gen(function* () {
688+
const fileSystem = yield* FileSystem.FileSystem;
689+
const baseDir = yield* fileSystem.makeTempDirectoryScoped({
690+
prefix: "t3-desktop-backend-config-test-",
691+
});
692+
const launch = Effect.gen(function* () {
693+
const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
694+
const primary = yield* configuration.resolvePrimary;
695+
const wsl = yield* configuration.resolveWsl({ port: 5050, distro: null });
696+
assert.equal(wsl.env.T3CODE_DESKTOP_HOST_SCOPE, primary.env.T3CODE_DESKTOP_HOST_SCOPE);
697+
assert.include((wsl.env.WSLENV ?? "").split(":"), "T3CODE_DESKTOP_HOST_SCOPE");
698+
return primary.env.T3CODE_DESKTOP_HOST_SCOPE;
699+
}).pipe(
700+
Effect.provide(
701+
layerBackendConfiguration().pipe(
702+
Layer.provideMerge(layerServerExposure),
703+
Layer.provideMerge(DesktopAppSettings.layerTest()),
704+
Layer.provideMerge(DesktopWslServerTree.layerTest()),
705+
Layer.provideMerge(
706+
DesktopWslEnvironment.layerTest({
707+
isAvailable: true,
708+
windowsToWslPath: () => Option.some("/mnt/c/repo/apps/server/src/index.ts"),
709+
getDistroIp: () => Option.some("172.27.0.99"),
710+
}),
711+
),
712+
Layer.provideMerge(layerEnvironment(baseDir, { platform: "win32" })),
713+
),
714+
),
715+
);
716+
717+
const first = yield* launch;
718+
assert.match(first ?? "", /^[0-9a-f]{32}$/);
719+
assert.equal(yield* launch, first);
720+
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
721+
);
722+
686723
it.effect("resolveWsl carries the kill switch and resource attributes into the distro", () =>
687724
Effect.gen(function* () {
688725
const fileSystem = yield* FileSystem.FileSystem;
@@ -748,7 +785,7 @@ describe("DesktopBackendConfiguration", () => {
748785
assert.equal(config.env.T3CODE_OTLP_PROTOCOL, "http/protobuf");
749786
assert.equal(
750787
config.env.WSLENV,
751-
"GOPATH/p:OPENAI_API_KEY/u:EMPTY::AZURE_DEVOPS_EXT_PAT/u:ANTHROPIC_API_KEY:T3CODE_OTLP_HEADERS:T3CODE_OTLP_PROTOCOL",
788+
"GOPATH/p:OPENAI_API_KEY/u:EMPTY::AZURE_DEVOPS_EXT_PAT/u:T3CODE_DESKTOP_HOST_SCOPE:ANTHROPIC_API_KEY:T3CODE_OTLP_HEADERS:T3CODE_OTLP_PROTOCOL",
752789
);
753790
}).pipe(
754791
Effect.provide(

‎apps/desktop/src/backend/DesktopBackendConfiguration.ts‎

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,38 @@ interface SharedBootstrapInput {
243243
readonly bootstrapToken: string;
244244
readonly bootstrapSecret: string;
245245
readonly observabilitySettings: BackendObservabilitySettings;
246+
readonly hostScope: string;
246247
}
247248

249+
// Matches the server's DESKTOP_HOST_SCOPE_ENV.
250+
const DESKTOP_HOST_SCOPE_ENV = "T3CODE_DESKTOP_HOST_SCOPE";
251+
const HOST_SCOPE_FILE_NAME = "desktop-host-scope";
252+
const HOST_SCOPE_PATTERN = /^[0-9a-f]{32}$/;
253+
254+
/**
255+
* Not a credential: it only tells this install's Windows and WSL servers that
256+
* they run on one machine, so usage both read from one Windows directory counts
257+
* once. Persisted so summaries from earlier launches still match.
258+
*/
259+
const readOrCreateHostScope = Effect.gen(function* () {
260+
const fileSystem = yield* FileSystem.FileSystem;
261+
const environment = yield* DesktopEnvironment.DesktopEnvironment;
262+
const random = yield* SecureRandom.SecureRandom;
263+
const scopePath = environment.path.join(environment.stateDir, HOST_SCOPE_FILE_NAME);
264+
const existing = (yield* fileSystem
265+
.readFileString(scopePath)
266+
.pipe(Effect.orElseSucceed(() => ""))).trim();
267+
if (HOST_SCOPE_PATTERN.test(existing)) return existing;
268+
const scope = Hex.encode(yield* random.randomBytes(16).pipe(Effect.orDie));
269+
yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true }).pipe(
270+
Effect.andThen(fileSystem.writeFileString(scopePath, scope)),
271+
Effect.catch((cause) =>
272+
Effect.logWarning("Could not persist the desktop host scope.", { cause }),
273+
),
274+
);
275+
return scope;
276+
});
277+
248278
type WslPreflightRuntime =
249279
| {
250280
readonly kind: "executable";
@@ -540,6 +570,7 @@ const resolvePrimaryStartConfig = Effect.fn("desktop.backendConfiguration.resolv
540570
ELECTRON_RUN_AS_NODE: "1",
541571
T3CODE_DESKTOP_SHELL_DISCOVERY_PENDING: "1",
542572
T3CODE_CLI_PATH: Option.getOrUndefined(input.cliPath),
573+
[DESKTOP_HOST_SCOPE_ENV]: input.hostScope,
543574
},
544575
extendEnv: true,
545576
bootstrap,
@@ -636,8 +667,8 @@ const resolveWslStartConfig = Effect.fn("desktop.backendConfiguration.resolveWsl
636667
const httpBaseUrl = new URL(`http://${rendererHost}:${input.port}`);
637668

638669
const distroArgs = distroForConfig ? ["-d", distroForConfig] : [];
639-
const forwardedEnv: Record<string, string> = {};
640-
const forwardedEnvNames: string[] = [];
670+
const forwardedEnv: Record<string, string> = { [DESKTOP_HOST_SCOPE_ENV]: input.hostScope };
671+
const forwardedEnvNames: string[] = [DESKTOP_HOST_SCOPE_ENV];
641672
for (const name of WSL_FORWARDED_ENV_NAMES) {
642673
const value = env[name];
643674
if (value !== undefined && value.length > 0) {
@@ -735,6 +766,13 @@ export const make = Effect.gen(function* () {
735766
const random = yield* SecureRandom.SecureRandom;
736767
// Random generation yields; serialize it so concurrent backends cannot capture distinct bootstrap secrets.
737768
const secretRef = yield* SynchronizedRef.make(Option.none<string>());
769+
const hostScope = yield* Effect.cached(
770+
readOrCreateHostScope.pipe(
771+
Effect.provideService(FileSystem.FileSystem, fileSystem),
772+
Effect.provideService(DesktopEnvironment.DesktopEnvironment, environment),
773+
Effect.provideService(SecureRandom.SecureRandom, random),
774+
),
775+
);
738776
const getOrCreateBootstrapSecret = SynchronizedRef.modifyEffect(secretRef, (current) =>
739777
Option.match(current, {
740778
onSome: (secret) => Effect.succeed([secret, current] as const),
@@ -762,6 +800,7 @@ export const make = Effect.gen(function* () {
762800
bootstrapToken,
763801
bootstrapSecret,
764802
observabilitySettings,
803+
hostScope: yield* hostScope,
765804
} satisfies SharedBootstrapInput;
766805
});
767806

0 commit comments

Comments
 (0)