Skip to content

Commit 2896676

Browse files
committed
fix: address latest review feedback
1 parent d58dba2 commit 2896676

8 files changed

Lines changed: 95 additions & 13 deletions

File tree

‎apps/server/src/cli/pair.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@ describe("pair tailscale local target", () => {
7474
);
7575
});
7676

77+
it("keeps Windows permission guidance free of Unix-only commands", () => {
78+
const cause = new TailscaleCommandExitError({
79+
executable: "tailscale.exe",
80+
subcommand: "serve",
81+
argumentCount: 4,
82+
exitCode: 1,
83+
stderrLength: 24,
84+
stderrDiagnostic: "permission-denied",
85+
});
86+
87+
const failure = tailscaleServeFailedError(443, cause);
88+
89+
expect(failure).toMatchObject({ executable: "tailscale.exe", cause });
90+
expect(failure.message).toContain("Grant this Windows user permission");
91+
expect(failure.message).not.toContain("sudo");
92+
});
93+
7794
it("proxies the dev web port for dev servers", () => {
7895
expect(resolveTailscaleLocalTarget({ ...baseState, devUrl: "http://localhost:5733/" })).toEqual(
7996
{ localPort: 5_733 },

‎apps/server/src/cli/pair.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ export class TailscaleServeFailedError extends Schema.TaggedErrorClass<Tailscale
119119
"TailscaleServeFailedError",
120120
{
121121
servePort: Schema.Number,
122+
executable: Schema.optional(Schema.Literals(["tailscale", "tailscale.exe"])),
122123
exitCode: Schema.optional(Schema.Number),
123124
stderrDiagnostic: Schema.optional(TailscaleStderrDiagnostic),
124125
cause: Schema.Defect(),
@@ -127,6 +128,9 @@ export class TailscaleServeFailedError extends Schema.TaggedErrorClass<Tailscale
127128
override get message(): string {
128129
if (this.stderrDiagnostic === "permission-denied") {
129130
const exitDetail = this.exitCode === undefined ? "" : ` with code ${String(this.exitCode)}`;
131+
if (this.executable === "tailscale.exe") {
132+
return `tailscale serve failed${exitDetail} for HTTPS port ${String(this.servePort)}: access denied. Grant this Windows user permission to manage Tailscale, then retry.`;
133+
}
130134
return `tailscale serve failed${exitDetail} for HTTPS port ${String(this.servePort)}: access denied. Run \`sudo tailscale set --operator=$USER\`, then retry.`;
131135
}
132136
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(
141145
): TailscaleServeFailedError {
142146
const exitDetails = isTailscaleCommandExitError(cause)
143147
? {
148+
executable: cause.executable,
144149
exitCode: cause.exitCode,
145150
...(cause.stderrDiagnostic === undefined
146151
? {}

‎apps/web/src/connection/platform.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
canRetainCachedPlatformRegistrationAfterRefreshFailure,
1313
canReuseCachedPlatformRegistration,
1414
primaryRegistrationToRetainAfterTopologyRead,
15+
primaryRegistrationRefreshAtEpochMs,
1516
provisionDesktopSshEnvironment,
1617
readPrimaryEnvironmentTargetResult,
1718
secondaryRegistrationsToRetainAfterTopologyRead,
@@ -222,4 +223,14 @@ describe("primary topology cache", () => {
222223
}),
223224
).toBeUndefined();
224225
});
226+
227+
it("periodically refreshes primary descriptor metadata", () => {
228+
const discoveredAtEpochMs = 10_000;
229+
const refreshAtEpochMs = primaryRegistrationRefreshAtEpochMs(discoveredAtEpochMs);
230+
const refreshable = { ...cached, refreshAtEpochMs };
231+
232+
expect(refreshAtEpochMs).toBe(40_000);
233+
expect(canReuseCachedPlatformRegistration(refreshable, cached.signature, 39_999)).toBe(true);
234+
expect(canReuseCachedPlatformRegistration(refreshable, cached.signature, 40_000)).toBe(false);
235+
});
225236
});

‎apps/web/src/connection/platform.ts‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,17 @@ const loadSecondaryConnectionRegistration = Effect.fn(
359359
});
360360

361361
// Poll cadence for the desktop bootstrap topology. There is no change event on
362-
// the bridge, so the renderer polls; successful registrations are cached by a
363-
// signature of their endpoint + token until bearer credentials approach expiry.
362+
// the bridge, so the renderer polls. Primary descriptor metadata refreshes
363+
// periodically; secondary registrations refresh before bearer credentials
364+
// expire.
364365
const PLATFORM_POLL_INTERVAL = "3 seconds";
366+
const PRIMARY_REGISTRATION_REFRESH_INTERVAL_MS = 30_000;
365367
const SECONDARY_BEARER_REFRESH_SKEW_MS = 5_000;
366368

369+
export function primaryRegistrationRefreshAtEpochMs(discoveredAtEpochMs: number): number {
370+
return discoveredAtEpochMs + PRIMARY_REGISTRATION_REFRESH_INTERVAL_MS;
371+
}
372+
367373
export function secondaryBearerExpiresAtEpochMs(
368374
issuedAtEpochMs: number,
369375
expiresInSeconds: number,
@@ -506,9 +512,18 @@ const platformConnectionSourceLayer = Layer.effect(
506512
Effect.option,
507513
);
508514
if (Option.isSome(built)) {
509-
const cacheEntry = { signature, registration: built.value };
515+
const cacheEntry = {
516+
signature,
517+
registration: built.value,
518+
refreshAtEpochMs: primaryRegistrationRefreshAtEpochMs(nowEpochMs),
519+
};
510520
next.set(PRIMARY_LOCAL_ENVIRONMENT_ID, cacheEntry);
511521
registrations.push(built.value);
522+
} else if (cached?.signature === signature) {
523+
// A descriptor refresh should not remove a live primary. Keep the
524+
// old registration and retry on the next topology poll.
525+
next.set(PRIMARY_LOCAL_ENVIRONMENT_ID, cached);
526+
registrations.push(cached.registration);
512527
}
513528
}
514529
}

‎infra/relay/src/db.test.ts‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { expect, it } from "@effect/vitest";
2+
import type { SQL } from "drizzle-orm";
3+
import { PgDialect } from "drizzle-orm/pg-core";
24
import * as Effect from "effect/Effect";
35
import * as Layer from "effect/Layer";
46
import * as Result from "effect/Result";
@@ -9,6 +11,9 @@ import { relayEnvironmentLifecycleLeases } from "./persistence/schema.ts";
911
it.effect("leases lifecycle work without changing its transaction boundary", () => {
1012
const calls: Array<string> = [];
1113
let ownerId = "";
14+
let insertedExpiresAt: SQL | undefined;
15+
let updatedExpiresAt: SQL | undefined;
16+
let leaseExpiryCondition: SQL | undefined;
1217
const db = {
1318
$client: {
1419
withTransaction: <A, E, R>(effect: Effect.Effect<A, E, R>) =>
@@ -19,13 +24,21 @@ it.effect("leases lifecycle work without changing its transaction boundary", ()
1924
insert: (table: unknown) => {
2025
expect(table).toBe(relayEnvironmentLifecycleLeases);
2126
return {
22-
values: (values: { readonly ownerId: string }) => {
27+
values: (values: { readonly ownerId: string; readonly expiresAt: SQL }) => {
2328
ownerId = values.ownerId;
29+
insertedExpiresAt = values.expiresAt;
2430
calls.push("acquire");
2531
return {
26-
onConflictDoUpdate: () => ({
27-
returning: () => Effect.succeed([{ ownerId }]),
28-
}),
32+
onConflictDoUpdate: (config: {
33+
readonly set: { readonly expiresAt: SQL };
34+
readonly setWhere: SQL;
35+
}) => {
36+
updatedExpiresAt = config.set.expiresAt;
37+
leaseExpiryCondition = config.setWhere;
38+
return {
39+
returning: () => Effect.succeed([{ ownerId }]),
40+
};
41+
},
2942
};
3043
},
3144
};
@@ -58,5 +71,12 @@ it.effect("leases lifecycle work without changing its transaction boundary", ()
5871
expect(Result.isFailure(result)).toBe(true);
5972
expect(ownerId).not.toBe("");
6073
expect(calls).toEqual(["acquire", "work", "release"]);
74+
const dialect = new PgDialect();
75+
expect(dialect.sqlToQuery(insertedExpiresAt!).sql).toContain("now() + interval '30 seconds'");
76+
expect(dialect.sqlToQuery(updatedExpiresAt!).sql).toContain("now() + interval '30 seconds'");
77+
expect(dialect.sqlToQuery(leaseExpiryCondition!)).toEqual({
78+
sql: '"relay_environment_lifecycle_leases"."expires_at"::timestamptz <= now()',
79+
params: [],
80+
});
6181
}).pipe(Effect.provide(layer));
6282
});

‎infra/relay/src/db.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ import * as Alchemy from "alchemy";
66
import * as RemovalPolicy from "alchemy/RemovalPolicy";
77
import type { EffectPgDatabase } from "drizzle-orm/effect-postgres";
88
import * as Context from "effect/Context";
9-
import * as DateTime from "effect/DateTime";
109
import * as Effect from "effect/Effect";
1110
import * as Layer from "effect/Layer";
1211
import * as Random from "effect/Random";
1312
import * as Schema from "effect/Schema";
14-
import { and, eq, lte } from "drizzle-orm";
13+
import { and, eq, sql } from "drizzle-orm";
1514

1615
import { relayDatabaseMode } from "./dbConfig.ts";
1716
import { relayEnvironmentLifecycleLeases } from "./persistence/schema.ts";
@@ -46,9 +45,7 @@ function withEnvironmentLock<A, E, R>(
4645
const acquire = (): Effect.Effect<void, EnvironmentLifecycleLeasePersistenceError> =>
4746
Effect.suspend(() =>
4847
Effect.gen(function* () {
49-
const now = yield* DateTime.now;
50-
const nowIso = DateTime.formatIso(now);
51-
const expiresAt = DateTime.formatIso(DateTime.add(now, { seconds: 30 }));
48+
const expiresAt = sql<string>`to_char((now() + interval '30 seconds') at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"')`;
5249
const rows = yield* db
5350
.insert(relayEnvironmentLifecycleLeases)
5451
.values({ ...input, ownerId, expiresAt })
@@ -58,7 +55,7 @@ function withEnvironmentLock<A, E, R>(
5855
relayEnvironmentLifecycleLeases.environmentId,
5956
],
6057
set: { ownerId, expiresAt },
61-
setWhere: lte(relayEnvironmentLifecycleLeases.expiresAt, nowIso),
58+
setWhere: sql`${relayEnvironmentLifecycleLeases.expiresAt}::timestamptz <= now()`,
6259
})
6360
.returning({ ownerId: relayEnvironmentLifecycleLeases.ownerId })
6461
.pipe(

‎packages/tailscale/src/tailscale.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,20 @@ describe("tailscale", () => {
320320
});
321321
});
322322

323+
it("does not suggest a Unix permission command on Windows", () => {
324+
const error = new TailscaleCommandExitError({
325+
executable: "tailscale.exe",
326+
subcommand: "serve",
327+
argumentCount: 4,
328+
exitCode: 1,
329+
stderrLength: 24,
330+
stderrDiagnostic: "permission-denied",
331+
});
332+
333+
assert.include(error.message, "Grant this Windows user permission");
334+
assert.notInclude(error.message, "sudo");
335+
});
336+
323337
it.effect("disables tailscale serve through the process spawner service", () => {
324338
const commands: {
325339
readonly command: string;

‎packages/tailscale/src/tailscale.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ export class TailscaleCommandExitError extends Schema.TaggedErrorClass<Tailscale
9595
) {
9696
override get message(): string {
9797
if (this.stderrDiagnostic === "permission-denied" && this.subcommand === "serve") {
98+
if (this.executable === "tailscale.exe") {
99+
return `tailscale serve exited with code ${this.exitCode}: access denied. Grant this Windows user permission to manage Tailscale, then retry.`;
100+
}
98101
return `tailscale serve exited with code ${this.exitCode}: access denied. Run \`sudo tailscale set --operator=$USER\`, then retry.`;
99102
}
100103
return `tailscale ${this.subcommand} exited with code ${this.exitCode}.`;

0 commit comments

Comments
 (0)