Skip to content

Commit bab9cfd

Browse files
committed
chore: merge main into managed provider proxy
2 parents 9fb6ebd + d897d3f commit bab9cfd

300 files changed

Lines changed: 14080 additions & 1306 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/scripts/main-process-bundle.test.mjs‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as NodeFSP from "node:fs/promises";
33
import * as NodeOS from "node:os";
44
import * as NodePath from "node:path";
55
import * as NodeVM from "node:vm";
6+
import * as NodeURL from "node:url";
67
import { build } from "vite-plus/pack";
78
import { assert, it } from "vite-plus/test";
89

@@ -114,18 +115,16 @@ it("loads the emitted packaged boot entry and backend cache preload", async () =
114115
const directory = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-desktop-boot-"));
115116
try {
116117
const entries = ["src/boot.ts", "src/compileCache.ts"];
117-
await NodeFSP.mkdir(NodePath.join(directory, "src"));
118-
await Promise.all(
119-
entries.map((entry) =>
120-
NodeFSP.copyFile(new URL(`../${entry}`, import.meta.url), NodePath.join(directory, entry)),
121-
),
122-
);
123118
assert.ok(Array.isArray(desktopConfig.pack));
124119
for (const packConfig of desktopConfig.pack) {
125120
if (!Array.isArray(packConfig.entry)) continue;
126121
if (!packConfig.entry.some((entry) => entries.includes(entry))) continue;
127122
await build({
128123
...packConfig,
124+
entry: packConfig.entry.map((entry) =>
125+
NodeURL.fileURLToPath(new URL(`../${entry}`, import.meta.url)),
126+
),
127+
outDir: NodePath.join(directory, "dist-electron"),
129128
config: false,
130129
cwd: directory,
131130
tsconfig: false,
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* DesktopManagedPolicy - the administrator's managed policy as the desktop app reads it, at most
3+
* once: every desktop check needs the same answer.
4+
*
5+
* @module DesktopManagedPolicy
6+
*/
7+
import type { ManagedPolicyDocument } from "@t3tools/contracts";
8+
import {
9+
layerWindowsRegistry,
10+
ManagedPolicyFileError,
11+
readManagedPolicy,
12+
WindowsRegistry,
13+
} from "@t3tools/shared/managedPolicyFile";
14+
import * as Context from "effect/Context";
15+
import * as Effect from "effect/Effect";
16+
import * as FileSystem from "effect/FileSystem";
17+
import * as Layer from "effect/Layer";
18+
import * as Schema from "effect/Schema";
19+
import { ChildProcessSpawner } from "effect/process";
20+
21+
import * as DesktopEnvironment from "./DesktopEnvironment.ts";
22+
23+
type FoundPolicy = Effect.Success<ReturnType<typeof readManagedPolicy>>;
24+
const isManagedPolicyFileError = Schema.is(ManagedPolicyFileError);
25+
26+
export class DesktopManagedPolicy extends Context.Service<
27+
DesktopManagedPolicy,
28+
{
29+
/** The policy on this machine, undefined without one; fails when it cannot be used. */
30+
readonly read: Effect.Effect<FoundPolicy, ManagedPolicyFileError>;
31+
}
32+
>()("@t3tools/desktop/app/DesktopManagedPolicy") {}
33+
34+
export const layer = Layer.effect(
35+
DesktopManagedPolicy,
36+
Effect.gen(function* () {
37+
const { platform } = yield* DesktopEnvironment.DesktopEnvironment;
38+
const read = yield* Effect.cached(
39+
readManagedPolicy(platform).pipe(
40+
Effect.provideService(FileSystem.FileSystem, yield* FileSystem.FileSystem),
41+
Effect.provideService(
42+
ChildProcessSpawner.ChildProcessSpawner,
43+
yield* ChildProcessSpawner.ChildProcessSpawner,
44+
),
45+
Effect.provideService(WindowsRegistry, yield* WindowsRegistry),
46+
),
47+
);
48+
return DesktopManagedPolicy.of({ read });
49+
}),
50+
).pipe(Layer.provide(layerWindowsRegistry(() => import("ffi-rs"))));
51+
52+
/** A machine with `policy` deployed, one whose policy cannot be used, or none. */
53+
export const layerTest = (policy?: ManagedPolicyDocument | ManagedPolicyFileError) =>
54+
Layer.succeed(
55+
DesktopManagedPolicy,
56+
DesktopManagedPolicy.of({
57+
read:
58+
policy === undefined
59+
? // @effect-diagnostics-next-line effectSucceedWithVoid:off - No policy is undefined, not void.
60+
Effect.succeed(undefined)
61+
: isManagedPolicyFileError(policy)
62+
? Effect.fail(policy)
63+
: Effect.succeed({ source: "/test/policy.json", document: policy }),
64+
}),
65+
);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import * as Layer from "effect/Layer";
88
import * as ManagedRuntime from "effect/ManagedRuntime";
99
import * as Option from "effect/Option";
1010
import * as Path from "effect/Path";
11+
import * as Stream from "effect/Stream";
1112
import { ChildProcessSpawner } from "effect/process";
1213

1314
import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
15+
import * as DesktopManagedPolicy from "../app/DesktopManagedPolicy.ts";
1416
import * as DesktopBackendConfiguration from "./DesktopBackendConfiguration.ts";
1517
import * as DesktopShellEnvironment from "../shell/DesktopShellEnvironment.ts";
1618
import * as DesktopConfig from "../app/DesktopConfig.ts";
@@ -22,6 +24,8 @@ import * as DesktopWslServerTree from "../wsl/DesktopWslServerTree.ts";
2224
const layerShellEnvironment = (prepared = true) =>
2325
Layer.succeed(DesktopShellEnvironment.DesktopShellEnvironment, {
2426
installIntoProcess: Effect.succeed(prepared),
27+
awaitDiscovery: Effect.succeed(prepared),
28+
updates: Stream.empty,
2529
});
2630

2731
const layerBackendConfiguration = (prepared = true) =>
@@ -237,6 +241,7 @@ describe("DesktopBackendConfiguration", () => {
237241
assert.equal(first.cwd, environment.backendCwd);
238242
assert.equal(first.captureOutput, true);
239243
assert.equal(first.env.ELECTRON_RUN_AS_NODE, "1");
244+
assert.equal(first.env.T3CODE_DESKTOP_SHELL_DISCOVERY_PENDING, "1");
240245
assert.isUndefined(first.env.T3CODE_PORT);
241246
assert.isUndefined(first.env.T3CODE_MODE);
242247
assert.isUndefined(first.env.T3CODE_DESKTOP_LAN_HOST);
@@ -968,6 +973,7 @@ describe("DesktopBackendConfiguration", () => {
968973
Layer.provideMerge(DesktopAppSettings.layerTest()),
969974
Layer.provideMerge(DesktopWslServerTree.layerTest()),
970975
Layer.provideMerge(DesktopWslEnvironment.layer),
976+
Layer.provideMerge(DesktopManagedPolicy.layerTest()),
971977
Layer.provideMerge(
972978
Layer.succeed(
973979
ChildProcessSpawner.ChildProcessSpawner,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ const resolvePrimaryStartConfig = Effect.fn("desktop.backendConfiguration.resolv
532532
env: {
533533
...backendChildEnvPatch(),
534534
ELECTRON_RUN_AS_NODE: "1",
535+
T3CODE_DESKTOP_SHELL_DISCOVERY_PENDING: "1",
535536
T3CODE_CLI_PATH: Option.getOrUndefined(input.cliPath),
536537
},
537538
extendEnv: true,

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import * as Effect from "effect/Effect";
77
import * as Fiber from "effect/Fiber";
88
import * as FileSystem from "effect/FileSystem";
99
import * as Layer from "effect/Layer";
10+
import * as Option from "effect/Option";
1011
import * as Sink from "effect/Sink";
1112
import * as Stream from "effect/Stream";
1213
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
1314

1415
import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
1516
import * as DesktopConfig from "../app/DesktopConfig.ts";
17+
import * as DesktopManagedPolicy from "../app/DesktopManagedPolicy.ts";
1618
import * as DesktopNetworkInterfaces from "./DesktopNetworkInterfaces.ts";
1719
import * as DesktopServerExposure from "./DesktopServerExposure.ts";
1820
import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts";
@@ -90,6 +92,7 @@ function layer(input: {
9092
readonly env?: Record<string, string | undefined>;
9193
readonly spawnerLayer?: Layer.Layer<ChildProcessSpawner.ChildProcessSpawner>;
9294
readonly desktopSettingsLayer?: Layer.Layer<DesktopAppSettings.DesktopAppSettings>;
95+
readonly managedPolicyLayer?: Layer.Layer<DesktopManagedPolicy.DesktopManagedPolicy>;
9396
}) {
9497
const env = { T3CODE_HOME: input.baseDir, ...input.env };
9598
const layerEnvironment = layerEnvironmentFor(input.baseDir, env);
@@ -99,6 +102,7 @@ function layer(input: {
99102

100103
return DesktopServerExposure.layer.pipe(
101104
Layer.provideMerge(input.desktopSettingsLayer ?? DesktopAppSettings.layer),
105+
Layer.provideMerge(input.managedPolicyLayer ?? DesktopManagedPolicy.layerTest()),
102106
Layer.provideMerge(NodeFileSystem.layer),
103107
Layer.provideMerge(NodeHttpClient.layerUndici),
104108
Layer.provideMerge(input.spawnerLayer ?? layerMockSpawner()),
@@ -122,6 +126,7 @@ const withHarness = <A, E, R>(
122126
env: Record<string, string | undefined> = {},
123127
spawnerLayer?: Layer.Layer<ChildProcessSpawner.ChildProcessSpawner>,
124128
desktopSettingsLayer?: Layer.Layer<DesktopAppSettings.DesktopAppSettings>,
129+
managedPolicyLayer?: Layer.Layer<DesktopManagedPolicy.DesktopManagedPolicy>,
125130
) =>
126131
Effect.gen(function* () {
127132
const fileSystem = yield* FileSystem.FileSystem;
@@ -136,6 +141,7 @@ const withHarness = <A, E, R>(
136141
env,
137142
...(spawnerLayer ? { spawnerLayer } : {}),
138143
...(desktopSettingsLayer ? { desktopSettingsLayer } : {}),
144+
...(managedPolicyLayer ? { managedPolicyLayer } : {}),
139145
}),
140146
),
141147
);
@@ -191,6 +197,41 @@ describe("DesktopServerExposure", () => {
191197
),
192198
);
193199

200+
it.effect("starts local-only, Serve off, when a managed policy keeps clients local", () =>
201+
withHarness(
202+
lanNetworkInterfaces,
203+
Effect.gen(function* () {
204+
const serverExposure = yield* DesktopServerExposure.DesktopServerExposure;
205+
const settings = yield* DesktopAppSettings.DesktopAppSettings;
206+
yield* settings.setServerExposureMode("network-accessible");
207+
yield* settings.setTailscaleServe({ enabled: true, port: Option.none() });
208+
209+
const state = yield* serverExposure.configureFromSettings({ port: 4173 });
210+
assert.equal(state.mode, "local-only");
211+
const backendConfig = yield* serverExposure.backendConfig;
212+
assert.equal(backendConfig.bindHost, "127.0.0.1");
213+
assert.isFalse(backendConfig.tailscaleServeEnabled);
214+
// Choosing them again while the policy applies does not widen the backend either.
215+
yield* serverExposure.setMode("network-accessible");
216+
yield* serverExposure.setTailscaleServeEnabled({ enabled: true });
217+
assert.equal((yield* serverExposure.backendConfig).bindHost, "127.0.0.1");
218+
assert.isFalse((yield* serverExposure.backendConfig).tailscaleServeEnabled);
219+
// The saved choice returns once the policy allows it.
220+
const saved = yield* settings.get;
221+
assert.equal(saved.serverExposureMode, "network-accessible");
222+
assert.isTrue(saved.tailscaleServeEnabled);
223+
}),
224+
{},
225+
undefined,
226+
undefined,
227+
DesktopManagedPolicy.layerTest({
228+
schemaVersion: 1,
229+
organization: "Example Corp",
230+
limits: { clients: ["desktop"] },
231+
}),
232+
),
233+
);
234+
194235
it.effect("persists network-accessible mode and updates backend binding state", () =>
195236
withHarness(
196237
lanNetworkInterfaces,

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import * as Semaphore from "effect/Semaphore";
2222
import * as HttpClient from "effect/http/HttpClient";
2323
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
2424

25+
import * as DesktopManagedPolicy from "../app/DesktopManagedPolicy.ts";
2526
import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts";
2627
import * as DesktopConfig from "../app/DesktopConfig.ts";
2728
import * as DesktopNetworkInterfaces from "./DesktopNetworkInterfaces.ts";
@@ -482,6 +483,18 @@ export const make = Effect.gen(function* () {
482483

483484
const readNetworkInterfaces = networkInterfaces.read;
484485

486+
// The server refuses to bind wider than the policy's clients allow, so network access or Serve
487+
// saved before the policy would keep the backend from starting. The backend runs without them;
488+
// the saved choice stays for when the policy allows it.
489+
const remoteAllowed = yield* (yield* DesktopManagedPolicy.DesktopManagedPolicy).read.pipe(
490+
Effect.map((found) => found?.document.limits?.clients?.includes("remote") ?? true),
491+
Effect.orElseSucceed(() => false),
492+
);
493+
const withinPolicy = (settings: DesktopAppSettings.DesktopSettings) =>
494+
remoteAllowed
495+
? settings
496+
: { ...settings, serverExposureMode: "local-only" as const, tailscaleServeEnabled: false };
497+
485498
const readCurrentState = (currentNetworkInterfaces: DesktopNetworkInterfaces.NetworkInterfaces) =>
486499
Ref.get(stateRef).pipe(
487500
Effect.map((state) =>
@@ -501,8 +514,8 @@ export const make = Effect.gen(function* () {
501514
const settings = yield* desktopSettings.get;
502515
const currentNetworkInterfaces = yield* readNetworkInterfaces;
503516
const resolved = resolveRuntimeState({
504-
requestedMode: settings.serverExposureMode,
505-
settings,
517+
requestedMode: withinPolicy(settings).serverExposureMode,
518+
settings: withinPolicy(settings),
506519
port,
507520
networkInterfaces: currentNetworkInterfaces,
508521
advertisedHostOverride: config.desktopLanHostOverride,
@@ -524,8 +537,8 @@ export const make = Effect.gen(function* () {
524537
};
525538
const currentNetworkInterfaces = yield* readNetworkInterfaces;
526539
const resolved = resolveRuntimeState({
527-
requestedMode: mode,
528-
settings: nextSettings,
540+
requestedMode: withinPolicy(nextSettings).serverExposureMode,
541+
settings: withinPolicy(nextSettings),
529542
port: previous.port,
530543
networkInterfaces: currentNetworkInterfaces,
531544
advertisedHostOverride: config.desktopLanHostOverride,
@@ -576,7 +589,7 @@ export const make = Effect.gen(function* () {
576589

577590
const nextState = yield* Ref.updateAndGet(stateRef, (current) => ({
578591
...current,
579-
tailscaleServeEnabled: result.settings.tailscaleServeEnabled,
592+
tailscaleServeEnabled: withinPolicy(result.settings).tailscaleServeEnabled,
580593
tailscaleServePort: result.settings.tailscaleServePort,
581594
}));
582595

‎apps/desktop/src/compileCache.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,23 @@
22
import * as NodeModule from "node:module";
33
import * as NodeOS from "node:os";
44
import * as NodePath from "node:path";
5+
import { enablePreparedServerCache } from "../../../scripts/lib/server-compile-cache.ts";
56

67
// Linux /tmp is shared; macOS and Windows temp dirs are per user.
78
// AppImage mounts change paths each launch, so Node's path-keyed cache would miss and accumulate copies.
89
try {
9-
if (!process.env.APPIMAGE) {
10+
if (!process.env.APPIMAGE || process.env.ELECTRON_RUN_AS_NODE === "1") {
1011
const cacheRoot =
1112
// oxlint-disable-next-line t3code/no-global-process-runtime -- Loads before any Effect runtime.
1213
process.platform === "linux"
1314
? process.env.XDG_CACHE_HOME || NodePath.join(NodeOS.homedir(), ".cache")
1415
: NodeOS.tmpdir();
15-
NodeModule.enableCompileCache(NodePath.join(cacheRoot, "t3code", "compile-cache"));
16+
const directory = NodePath.join(cacheRoot, "t3code", "compile-cache");
17+
const prepared =
18+
process.env.ELECTRON_RUN_AS_NODE === "1" &&
19+
enablePreparedServerCache(process.argv[1] ?? "", directory);
20+
if (!prepared && !process.env.APPIMAGE) {
21+
NodeModule.enableCompileCache(directory);
22+
}
1623
}
1724
} catch {}

‎apps/desktop/src/ipc/DesktopIpcHandlers.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {
3636
getAppBranding,
3737
getLocalEnvironmentBootstraps,
3838
getLocalEnvironmentBearerToken,
39-
getRelayTelemetryEnabled,
39+
installRelayTelemetryEnabled,
4040
getSystemLocale,
4141
getWindowFullscreenState,
4242
openExternal,
@@ -91,7 +91,7 @@ export const installDesktopIpcHandlers = Effect.fn("desktop.ipc.installHandlers"
9191
yield* ipc.handleSync(takeLegacyLocalStorage);
9292
yield* ipc.handle(completeLegacyLocalStorage);
9393
yield* ipc.handleSync(getAppBranding);
94-
yield* ipc.handleSync(getRelayTelemetryEnabled);
94+
yield* installRelayTelemetryEnabled();
9595
yield* ipc.handleSync(getSystemLocale);
9696
yield* ipc.handleSync(getWindowFullscreenState);
9797
yield* ipc.handleSync(getLocalEnvironmentBootstraps);

0 commit comments

Comments
 (0)