From 28966761e1230787d2a5bb3dc0fc95153a804047 Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Tue, 4 Aug 2026 11:25:02 -0700 Subject: [PATCH] fix: address latest review feedback --- apps/server/src/cli/pair.test.ts | 17 ++++++++++++++ apps/server/src/cli/pair.ts | 5 +++++ apps/web/src/connection/platform.test.ts | 11 ++++++++++ apps/web/src/connection/platform.ts | 21 +++++++++++++++--- infra/relay/src/db.test.ts | 28 ++++++++++++++++++++---- infra/relay/src/db.ts | 9 +++----- packages/tailscale/src/tailscale.test.ts | 14 ++++++++++++ packages/tailscale/src/tailscale.ts | 3 +++ 8 files changed, 95 insertions(+), 13 deletions(-) diff --git a/apps/server/src/cli/pair.test.ts b/apps/server/src/cli/pair.test.ts index 514ddef723..1ff5f8a95f 100644 --- a/apps/server/src/cli/pair.test.ts +++ b/apps/server/src/cli/pair.test.ts @@ -74,6 +74,23 @@ describe("pair tailscale local target", () => { ); }); + it("keeps Windows permission guidance free of Unix-only commands", () => { + const cause = new TailscaleCommandExitError({ + executable: "tailscale.exe", + subcommand: "serve", + argumentCount: 4, + exitCode: 1, + stderrLength: 24, + stderrDiagnostic: "permission-denied", + }); + + const failure = tailscaleServeFailedError(443, cause); + + expect(failure).toMatchObject({ executable: "tailscale.exe", cause }); + expect(failure.message).toContain("Grant this Windows user permission"); + expect(failure.message).not.toContain("sudo"); + }); + it("proxies the dev web port for dev servers", () => { expect(resolveTailscaleLocalTarget({ ...baseState, devUrl: "http://localhost:5733/" })).toEqual( { localPort: 5_733 }, diff --git a/apps/server/src/cli/pair.ts b/apps/server/src/cli/pair.ts index 2c864a8728..eef5168e36 100644 --- a/apps/server/src/cli/pair.ts +++ b/apps/server/src/cli/pair.ts @@ -119,6 +119,7 @@ export class TailscaleServeFailedError extends Schema.TaggedErrorClass\` by hand to see why.`; @@ -141,6 +145,7 @@ export function tailscaleServeFailedError( ): TailscaleServeFailedError { const exitDetails = isTailscaleCommandExitError(cause) ? { + executable: cause.executable, exitCode: cause.exitCode, ...(cause.stderrDiagnostic === undefined ? {} diff --git a/apps/web/src/connection/platform.test.ts b/apps/web/src/connection/platform.test.ts index b316347053..c5457f2ff0 100644 --- a/apps/web/src/connection/platform.test.ts +++ b/apps/web/src/connection/platform.test.ts @@ -12,6 +12,7 @@ import { canRetainCachedPlatformRegistrationAfterRefreshFailure, canReuseCachedPlatformRegistration, primaryRegistrationToRetainAfterTopologyRead, + primaryRegistrationRefreshAtEpochMs, provisionDesktopSshEnvironment, readPrimaryEnvironmentTargetResult, secondaryRegistrationsToRetainAfterTopologyRead, @@ -222,4 +223,14 @@ describe("primary topology cache", () => { }), ).toBeUndefined(); }); + + it("periodically refreshes primary descriptor metadata", () => { + const discoveredAtEpochMs = 10_000; + const refreshAtEpochMs = primaryRegistrationRefreshAtEpochMs(discoveredAtEpochMs); + const refreshable = { ...cached, refreshAtEpochMs }; + + expect(refreshAtEpochMs).toBe(40_000); + expect(canReuseCachedPlatformRegistration(refreshable, cached.signature, 39_999)).toBe(true); + expect(canReuseCachedPlatformRegistration(refreshable, cached.signature, 40_000)).toBe(false); + }); }); diff --git a/apps/web/src/connection/platform.ts b/apps/web/src/connection/platform.ts index 3019cb7128..4b74bcb9d5 100644 --- a/apps/web/src/connection/platform.ts +++ b/apps/web/src/connection/platform.ts @@ -359,11 +359,17 @@ const loadSecondaryConnectionRegistration = Effect.fn( }); // Poll cadence for the desktop bootstrap topology. There is no change event on -// the bridge, so the renderer polls; successful registrations are cached by a -// signature of their endpoint + token until bearer credentials approach expiry. +// the bridge, so the renderer polls. Primary descriptor metadata refreshes +// periodically; secondary registrations refresh before bearer credentials +// expire. const PLATFORM_POLL_INTERVAL = "3 seconds"; +const PRIMARY_REGISTRATION_REFRESH_INTERVAL_MS = 30_000; const SECONDARY_BEARER_REFRESH_SKEW_MS = 5_000; +export function primaryRegistrationRefreshAtEpochMs(discoveredAtEpochMs: number): number { + return discoveredAtEpochMs + PRIMARY_REGISTRATION_REFRESH_INTERVAL_MS; +} + export function secondaryBearerExpiresAtEpochMs( issuedAtEpochMs: number, expiresInSeconds: number, @@ -506,9 +512,18 @@ const platformConnectionSourceLayer = Layer.effect( Effect.option, ); if (Option.isSome(built)) { - const cacheEntry = { signature, registration: built.value }; + const cacheEntry = { + signature, + registration: built.value, + refreshAtEpochMs: primaryRegistrationRefreshAtEpochMs(nowEpochMs), + }; next.set(PRIMARY_LOCAL_ENVIRONMENT_ID, cacheEntry); registrations.push(built.value); + } else if (cached?.signature === signature) { + // A descriptor refresh should not remove a live primary. Keep the + // old registration and retry on the next topology poll. + next.set(PRIMARY_LOCAL_ENVIRONMENT_ID, cached); + registrations.push(cached.registration); } } } diff --git a/infra/relay/src/db.test.ts b/infra/relay/src/db.test.ts index 65be3f0b92..881e7ae1f9 100644 --- a/infra/relay/src/db.test.ts +++ b/infra/relay/src/db.test.ts @@ -1,4 +1,6 @@ import { expect, it } from "@effect/vitest"; +import type { SQL } from "drizzle-orm"; +import { PgDialect } from "drizzle-orm/pg-core"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Result from "effect/Result"; @@ -9,6 +11,9 @@ import { relayEnvironmentLifecycleLeases } from "./persistence/schema.ts"; it.effect("leases lifecycle work without changing its transaction boundary", () => { const calls: Array = []; let ownerId = ""; + let insertedExpiresAt: SQL | undefined; + let updatedExpiresAt: SQL | undefined; + let leaseExpiryCondition: SQL | undefined; const db = { $client: { withTransaction: (effect: Effect.Effect) => @@ -19,13 +24,21 @@ it.effect("leases lifecycle work without changing its transaction boundary", () insert: (table: unknown) => { expect(table).toBe(relayEnvironmentLifecycleLeases); return { - values: (values: { readonly ownerId: string }) => { + values: (values: { readonly ownerId: string; readonly expiresAt: SQL }) => { ownerId = values.ownerId; + insertedExpiresAt = values.expiresAt; calls.push("acquire"); return { - onConflictDoUpdate: () => ({ - returning: () => Effect.succeed([{ ownerId }]), - }), + onConflictDoUpdate: (config: { + readonly set: { readonly expiresAt: SQL }; + readonly setWhere: SQL; + }) => { + updatedExpiresAt = config.set.expiresAt; + leaseExpiryCondition = config.setWhere; + return { + returning: () => Effect.succeed([{ ownerId }]), + }; + }, }; }, }; @@ -58,5 +71,12 @@ it.effect("leases lifecycle work without changing its transaction boundary", () expect(Result.isFailure(result)).toBe(true); expect(ownerId).not.toBe(""); expect(calls).toEqual(["acquire", "work", "release"]); + const dialect = new PgDialect(); + expect(dialect.sqlToQuery(insertedExpiresAt!).sql).toContain("now() + interval '30 seconds'"); + expect(dialect.sqlToQuery(updatedExpiresAt!).sql).toContain("now() + interval '30 seconds'"); + expect(dialect.sqlToQuery(leaseExpiryCondition!)).toEqual({ + sql: '"relay_environment_lifecycle_leases"."expires_at"::timestamptz <= now()', + params: [], + }); }).pipe(Effect.provide(layer)); }); diff --git a/infra/relay/src/db.ts b/infra/relay/src/db.ts index 5e17286e30..754d63aea8 100644 --- a/infra/relay/src/db.ts +++ b/infra/relay/src/db.ts @@ -6,12 +6,11 @@ import * as Alchemy from "alchemy"; import * as RemovalPolicy from "alchemy/RemovalPolicy"; import type { EffectPgDatabase } from "drizzle-orm/effect-postgres"; import * as Context from "effect/Context"; -import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Random from "effect/Random"; import * as Schema from "effect/Schema"; -import { and, eq, lte } from "drizzle-orm"; +import { and, eq, sql } from "drizzle-orm"; import { relayDatabaseMode } from "./dbConfig.ts"; import { relayEnvironmentLifecycleLeases } from "./persistence/schema.ts"; @@ -46,9 +45,7 @@ function withEnvironmentLock( const acquire = (): Effect.Effect => Effect.suspend(() => Effect.gen(function* () { - const now = yield* DateTime.now; - const nowIso = DateTime.formatIso(now); - const expiresAt = DateTime.formatIso(DateTime.add(now, { seconds: 30 })); + const expiresAt = sql`to_char((now() + interval '30 seconds') at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"')`; const rows = yield* db .insert(relayEnvironmentLifecycleLeases) .values({ ...input, ownerId, expiresAt }) @@ -58,7 +55,7 @@ function withEnvironmentLock( relayEnvironmentLifecycleLeases.environmentId, ], set: { ownerId, expiresAt }, - setWhere: lte(relayEnvironmentLifecycleLeases.expiresAt, nowIso), + setWhere: sql`${relayEnvironmentLifecycleLeases.expiresAt}::timestamptz <= now()`, }) .returning({ ownerId: relayEnvironmentLifecycleLeases.ownerId }) .pipe( diff --git a/packages/tailscale/src/tailscale.test.ts b/packages/tailscale/src/tailscale.test.ts index 5eb227d0d2..1d3a751118 100644 --- a/packages/tailscale/src/tailscale.test.ts +++ b/packages/tailscale/src/tailscale.test.ts @@ -320,6 +320,20 @@ describe("tailscale", () => { }); }); + it("does not suggest a Unix permission command on Windows", () => { + const error = new TailscaleCommandExitError({ + executable: "tailscale.exe", + subcommand: "serve", + argumentCount: 4, + exitCode: 1, + stderrLength: 24, + stderrDiagnostic: "permission-denied", + }); + + assert.include(error.message, "Grant this Windows user permission"); + assert.notInclude(error.message, "sudo"); + }); + it.effect("disables tailscale serve through the process spawner service", () => { const commands: { readonly command: string; diff --git a/packages/tailscale/src/tailscale.ts b/packages/tailscale/src/tailscale.ts index 0680080d27..7747d37a01 100644 --- a/packages/tailscale/src/tailscale.ts +++ b/packages/tailscale/src/tailscale.ts @@ -95,6 +95,9 @@ export class TailscaleCommandExitError extends Schema.TaggedErrorClass