fix: address latest review feedback

This commit is contained in:
Theo Browne committed 2026-08-04 22:10:50 -07:00
1 parent d58dba29c7
commit 28966761e1
8 files changed
+95 -13

No files matched your search

+17
View File
@@ -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 },
+5
View File
@@ -119,6 +119,7 @@ export class TailscaleServeFailedError extends Schema.TaggedErrorClass<Tailscale
"TailscaleServeFailedError",
{
servePort: Schema.Number,
executable: Schema.optional(Schema.Literals(["tailscale", "tailscale.exe"])),
exitCode: Schema.optional(Schema.Number),
stderrDiagnostic: Schema.optional(TailscaleStderrDiagnostic),
cause: Schema.Defect(),
@@ -127,6 +128,9 @@ export class TailscaleServeFailedError extends Schema.TaggedErrorClass<Tailscale
override get message(): string {
if (this.stderrDiagnostic === "permission-denied") {
const exitDetail = this.exitCode === undefined ? "" : ` with code ${String(this.exitCode)}`;
if (this.executable === "tailscale.exe") {
return `tailscale serve failed${exitDetail} for HTTPS port ${String(this.servePort)}: access denied. Grant this Windows user permission to manage Tailscale, then retry.`;
}
return `tailscale serve failed${exitDetail} for HTTPS port ${String(this.servePort)}: access denied. Run \`sudo tailscale set --operator=$USER\`, then retry.`;
}
return `tailscale serve failed for HTTPS port ${String(this.servePort)}. Run \`tailscale serve --https=${String(this.servePort)} --bg <local-url>\` 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
? {}
+11
View File
@@ -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);
});
});
+18 -3
View File
@@ -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);
}
}
}
+24 -4
View File
@@ -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<string> = [];
let ownerId = "";
let insertedExpiresAt: SQL | undefined;
let updatedExpiresAt: SQL | undefined;
let leaseExpiryCondition: SQL | undefined;
const db = {
$client: {
withTransaction: <A, E, R>(effect: Effect.Effect<A, E, R>) =>
@@ -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));
});
+3 -6
View File
@@ -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<A, E, R>(
const acquire = (): Effect.Effect<void, EnvironmentLifecycleLeasePersistenceError> =>
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<string>`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<A, E, R>(
relayEnvironmentLifecycleLeases.environmentId,
],
set: { ownerId, expiresAt },
setWhere: lte(relayEnvironmentLifecycleLeases.expiresAt, nowIso),
setWhere: sql`${relayEnvironmentLifecycleLeases.expiresAt}::timestamptz <= now()`,
})
.returning({ ownerId: relayEnvironmentLifecycleLeases.ownerId })
.pipe(
+14
View File
@@ -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;
+3
View File
@@ -95,6 +95,9 @@ export class TailscaleCommandExitError extends Schema.TaggedErrorClass<Tailscale
) {
override get message(): string {
if (this.stderrDiagnostic === "permission-denied" && this.subcommand === "serve") {
if (this.executable === "tailscale.exe") {
return `tailscale serve exited with code ${this.exitCode}: access denied. Grant this Windows user permission to manage Tailscale, then retry.`;
}
return `tailscale serve exited with code ${this.exitCode}: access denied. Run \`sudo tailscale set --operator=$USER\`, then retry.`;
}
return `tailscale ${this.subcommand} exited with code ${this.exitCode}.`;