feat(connection): support multiple routes per environment #7921
Closedburnmandont(GitHub profile, opens in a new tab)wants to merge 9 commits into
main fromrefs/pull/7921/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/web/src/components/settings/ConnectionsSettings.tsx line 1516OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1478,3 +1503,14 @@14781503 ) : null}14791504 </div>14801505 <div className="flex w-full shrink-0 items-center gap-2 sm:w-auto sm:justify-end">1506+ {routes.length > 1 ? (1507+ <Select1508+ value={connectionRouteId(environment.entry.target)}1509+ onValueChange={(value) => {1510+ if (typeof value === "string") {1511+ onSelectRoute(environmentId, value);1512+ }1513+ }}1514+ >1515+ <SelectTrigger1516+ size="sm"
- packages/client-runtime/src/connection/registry.ts lines 769 to 769OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -616,0 +749,21 @@749+ return yield* new PlatformEnvironmentRemovalError({ environmentId });750+ }751+ const selected = yield* getEntry(environmentId);752+ yield* registrations.removeRoute(relay.target, fallback.target);753+ yield* SubscriptionRef.update(routes, (current) => {754+ const next = new Map(current);755+ const remaining = (next.get(environmentId) ?? []).filter(756+ (entry) => connectionRouteId(entry.target) !== connectionRouteId(relay.target),757+ );758+ next.set(environmentId, remaining);759+ return next;760+ });761+ if (selected.target._tag !== "RelayConnectionTarget") return;762+ yield* Ref.update(persistedTargetsByEnvironment, (current) => {763+ const next = new Map(current);764+ next.set(environmentId, fallback.target);765+ return next;766+ });767+ yield* installEntryLocked(fallback);768+ }),769+ ).pipe(Effect.catchTag("EnvironmentNotRegisteredError", () => Effect.void));packages/client-runtime/src/connection/registry.ts lines 488 to 500Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -405,0 +468,21 @@468+469+ const selectRoute = Effect.fn("EnvironmentRegistry.selectRoute")(function* (470+ environmentId: EnvironmentId,471+ routeId: string,472+ ) {473+ yield* withLeaseLock(474+ environmentId,475+ Effect.gen(function* () {476+ if ((yield* Ref.get(platformEnvironmentIds)).has(environmentId)) {477+ return yield* new PlatformEnvironmentRemovalError({ environmentId });478+ }479+ yield* getEntry(environmentId);480+ const entry = (yield* SubscriptionRef.get(routes))481+ .get(environmentId)482+ ?.find((candidate) => connectionRouteId(candidate.target) === routeId);483+ if (entry === undefined) {484+ return yield* new ConnectionRouteNotRegisteredError({ environmentId, routeId });485+ }486+ if (entry.target._tag === "PrimaryConnectionTarget") {487+ return yield* new PlatformEnvironmentRemovalError({ environmentId });488+ }packages/client-runtime/src/connection/resolver.ts lines 263 to 270Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -237,11 +260,10 @@237- wsBaseUrl: prepared.bootstrap.wsBaseUrl,238- bearerToken: prepared.bearerToken,239- });240- return {241- environmentId: authorized.environmentId,242- label: authorized.label,243- httpBaseUrl: authorized.httpBaseUrl,244- socketUrl: authorized.socketUrl,245- httpAuthorization: authorized.httpAuthorization,246- target,247- } satisfies PreparedConnection;260+261+ const credential = yield* credentials.get(target.connectionId);262+ if (Option.isSome(credential) && isBearerCredential(credential.value)) {263+ return yield* prepareAndAuthorize(credential.value.token).pipe(264+ Effect.catch((error) =>265+ error._tag === "ConnectionBlockedError" && error.reason === "authentication"266+ ? credentials.remove(target.connectionId).pipe(Effect.andThen(prepareAndAuthorize()))267+ : Effect.fail(error),268+ ),269+ );
- packages/ssh/src/tunnel.ts line 462Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -452,3 +453,9 @@452453 exit 1453454 `;454455456+const REMOTE_STATE_LOCK_SCRIPT = `acquire_state_lock() {457+ STATE_LOCK_FILE="$STATE_DIR/operation.lock"458+ STATE_LOCK_DIR="$STATE_DIR/operation.lock.d"459+ STATE_LOCK_MODE=""460+ if command -v flock >/dev/null 2>&1; then461+ exec 9>"$STATE_LOCK_FILE"packages/ssh/src/tunnel.ts line 545Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -468,4 +509,21 @@509+@@T3_STATE_LOCK_SCRIPT@@510+LAUNCHED_PID=""468511 cleanup_runner_next() {469512 rm -f "$RUNNER_NEXT"513+ release_state_lock514+}515+abort_remote_launch() {516+ if [ -n "$LAUNCHED_PID" ] && kill -0 "$LAUNCHED_PID" 2>/dev/null; then517+ kill "$LAUNCHED_PID" 2>/dev/null || true518+ ABORT_WAIT_COUNT=0519+ while kill -0 "$LAUNCHED_PID" 2>/dev/null && [ "$ABORT_WAIT_COUNT" -lt 20 ]; do520+ ABORT_WAIT_COUNT=$((ABORT_WAIT_COUNT + 1))521+ sleep 0.1522+ done523+ fi524+ rm -f "$PID_FILE" "$PORT_FILE" "$MANAGED_FILE" "$BASE_DIR_FILE"525+ exit 1470526 }471527 trap cleanup_runner_next EXIT528+trap abort_remote_launch HUP INT TERM529+acquire_state_lockpackages/client-runtime/src/platform/storageDocument.ts line 93Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -80,3 +91,3 @@8091 connectionId === null8192 ? document.credentials8293 : removeCatalogValue(document.credentials, (value) => value.connectionId, connectionId),packages/ssh/src/tunnel.ts line 462OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -452,3 +453,10 @@452453 exit 1453454 `;454455456+const REMOTE_STATE_LOCK_SCRIPT = `acquire_state_lock() {457+ STATE_LOCK_FILE="$STATE_DIR/operation.lock"458+ STATE_LOCK_DIR="$STATE_DIR/operation.lock.d"459+ STATE_LOCK_MODE=""460+ if command -v flock >/dev/null 2>&1; then461+ exec 9>"$STATE_LOCK_FILE"462+ if ! flock -w 120 9; then
- packages/ssh/src/tunnel.ts line 612OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -502,0 +592,21 @@592+const candidates = [];593+const seen = new Set();594+const addBaseDir = (value) => {595+ const baseDir = value.trim();596+ if (baseDir.length === 0 || seen.has(baseDir)) return;597+ seen.add(baseDir);598+ candidates.push(baseDir);599+};600+const addBaseDirFromPid = (pid) => {601+ if (!Number.isInteger(pid) || pid <= 0) return;602+ try {603+ const environment = fs.readFileSync(\`/proc/\${pid}/environ\`, "utf8").split("\\0");604+ const value = environment.find((entry) => entry.startsWith("T3CODE_HOME="));605+ if (value) addBaseDir(value.slice("T3CODE_HOME=".length));606+ } catch {}607+};608+609+addBaseDirFromPid(servicePid);610+addBaseDir(environmentBaseDir);611+try {612+ for (const entry of fs.readdirSync("/proc")) {packages/ssh/src/tunnel.ts line 483OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -454,0 +463,21 @@463+ printf 'Timed out waiting for another T3 SSH operation to finish.\n' >&2464+ return 1465+ fi466+ STATE_LOCK_MODE="flock"467+ return 0468+ fi469+470+ STATE_LOCK_WAIT_COUNT=0471+ STATE_LOCK_MISSING_PID_COUNT=0472+ while ! mkdir "$STATE_LOCK_DIR" 2>/dev/null; do473+ STATE_LOCK_OWNER_PID="$(cat "$STATE_LOCK_DIR/pid" 2>/dev/null || true)"474+ if [ -n "$STATE_LOCK_OWNER_PID" ]; then475+ STATE_LOCK_MISSING_PID_COUNT=0476+ if ! kill -0 "$STATE_LOCK_OWNER_PID" 2>/dev/null; then477+ rm -f "$STATE_LOCK_DIR/pid"478+ rmdir "$STATE_LOCK_DIR" 2>/dev/null || true479+ continue480+ fi481+ else482+ STATE_LOCK_MISSING_PID_COUNT=$((STATE_LOCK_MISSING_PID_COUNT + 1))483+ if [ "$STATE_LOCK_MISSING_PID_COUNT" -ge 10 ]; then
- apps/web/src/components/settings/ConnectionsSettings.tsx lines 1517 to 1517OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1478,3 +1503,15 @@14781503 ) : null}14791504 </div>14801505 <div className="flex w-full shrink-0 items-center gap-2 sm:w-auto sm:justify-end">1506+ {routes.length > 1 ? (1507+ <Select1508+ value={connectionRouteId(environment.entry.target)}1509+ onValueChange={(value) => {1510+ if (typeof value === "string") {1511+ onSelectRoute(environmentId, value);1512+ }1513+ }}1514+ >1515+ <SelectTrigger1516+ size="xs"1517+ className="w-full sm:w-48"
- apps/web/src/components/settings/ConnectionsSettings.tsx line 1520OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1478,3 +1503,18 @@14781503 ) : null}14791504 </div>14801505 <div className="flex w-full shrink-0 items-center gap-2 sm:w-auto sm:justify-end">1506+ {routes.length > 1 ? (1507+ <Select1508+ value={connectionRouteId(environment.entry.target)}1509+ onValueChange={(value) => {1510+ if (typeof value === "string") {1511+ onSelectRoute(environmentId, value);1512+ }1513+ }}1514+ >1515+ <SelectTrigger1516+ size="xs"1517+ className="w-full min-w-0 sm:w-48"1518+ aria-label={`Connection method for ${environment.label}`}1519+ disabled={1520+ switchingRouteEnvironmentId === environmentId ||
- apps/web/src/components/settings/ConnectionsSettings.tsx lines 1520 to 1520OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1478,3 +1503,18 @@14781503 ) : null}14791504 </div>14801505 <div className="flex w-full shrink-0 items-center gap-2 sm:w-auto sm:justify-end">1506+ {routes.length > 1 ? (1507+ <Select1508+ value={connectionRouteId(environment.entry.target)}1509+ onValueChange={(value) => {1510+ if (typeof value === "string") {1511+ onSelectRoute(environmentId, value);1512+ }1513+ }}1514+ >1515+ <SelectTrigger1516+ size="xs"1517+ className="w-full min-w-0 sm:w-48"1518+ aria-label={`Connection method for ${environment.label}`}1519+ disabled={1520+ switchingRouteEnvironmentId !== null || removingEnvironmentId === environmentId
- @@ -554,6 +708,8 @@554708 REMOTE_MANAGED="external"555709 fi556710 else557- REMOTE_PID="$(cat "$PID_FILE" 2>/dev/null || true)"558- REMOTE_PORT="$(cat "$PORT_FILE" 2>/dev/null || true)"559- REMOTE_MANAGED="$(cat "$MANAGED_FILE" 2>/dev/null || true)"711+ printf 'Existing remote T3 server did not become ready on 127.0.0.1:%s.\n' "$REMOTE_PORT" >&2712+ REMOTE_PORT="$PREVIOUS_REMOTE_PORT"713+ DEFAULT_REMOTE_PORT=""714+ DEFAULT_RUNTIME_PID=""715+ rm -f "$DISCOVERED_BASE_DIR_FILE"
- @@ -1804,3 +1870,4 @@18041870 return keys;18051871 }, [savedEnvironments]);18061872 const [sshConnectionError, setSshConnectionError] = useState<string | null>(null);1873+ const [switchingRouteEnvironmentIds, setSwitchingRouteEnvironmentIds] = useState<
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
ad01a8e4-6d2b-4374-aaf9-5333f62280bfYou can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share