GeezerVison is showing github.com — refactor(server): repoint launchers inside the trial commit · pingdotgg/t3code@c04f95f
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/cloud/selfUpdate.test.ts +1-3Lines changed: 1 addition & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -122,16 +122,14 @@ const makeHarness = Effect.fn("test.make_self_update_harness")(function* ( | 122 | 122 | }); | 123 | 123 | const launcher = ServiceLauncherClient.ServiceLauncherClient.of({ | 124 | 124 | managed: options.managed ?? true, | 125 | | - trialPending: false, | 126 | | - repointLaunchers: () => Effect.void, | 127 | 125 | requestUpdate: | 128 | 126 | options.requestUpdate ?? | 129 | 127 | (() => | 130 | 128 | Effect.sync(() => { | 131 | 129 | order.push("accept"); | 132 | 130 | return "launcher-id"; | 133 | 131 | })), | 134 | | - prepareTrial: Effect.undefined, | | 132 | + prepareTrial: () => Effect.undefined, | 135 | 133 | }); | 136 | 134 | const config = yield* ServerConfig.ServerConfig.pipe( | 137 | 135 | Effect.provide(ServerConfig.layerTest(process.cwd(), baseDir)), | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/cloud/serviceLauncherClient.test.ts +63-48Lines changed: 63 additions & 48 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -55,34 +55,67 @@ const makeClient = (host: FakeLauncherProcess, currentVersion: string) => | 55 | 55 | Effect.provide(NodeServices.layer), | 56 | 56 | ); | 57 | 57 |
| 58 | | -it.effect("waits for the launcher to durably commit the trial update ID", () => | 59 | | - Effect.gen(function* () { | 60 | | - const pending = { | 61 | | - id: "update-1", | 62 | | - fromVersion: "1.0.0", | 63 | | - targetVersion: "1.1.0", | 64 | | - dbPath: "/tmp/state.sqlite", | 65 | | - status: "pending" as const, | 66 | | - }; | 67 | | - const host = new FakeLauncherProcess({ | 68 | | - protocol: SERVICE_LAUNCHER_PROTOCOL, | 69 | | - childVersion: "1.1.0", | 70 | | - update: pending, | 71 | | - }); | 72 | | - const client = yield* makeClient(host, "1.1.0"); | 73 | | - const prepared = yield* Effect.forkChild(client.prepareTrial, { startImmediately: true }); | 74 | | - yield* Effect.yieldNow; | 75 | | - expect(host.sent).toEqual([{ type: "prepared", updateId: "update-1" }]); | | 58 | +it.effect( | | 59 | + "waits for the launcher to durably commit the trial update ID, then moves owned launchers", | | 60 | + () => | | 61 | + Effect.gen(function* () { | | 62 | + const fs = yield* FileSystem.FileSystem; | | 63 | + const path = yield* Path.Path; | | 64 | + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-trial-" }); | | 65 | + const baseDir = path.join(root, ".t3"); | | 66 | + const exe = (version: string) => path.join(baseDir, "runtime/versions", version, "t3"); | | 67 | + const installed = path.join(root, ".local/bin/t3"); | | 68 | + for (const file of [exe("1.0.0"), exe("1.1.0")]) { | | 69 | + yield* fs.makeDirectory(path.dirname(file), { recursive: true }); | | 70 | + yield* fs.writeFileString(file, ""); | | 71 | + } | | 72 | + yield* fs.makeDirectory(path.dirname(installed), { recursive: true }); | | 73 | + yield* fs.symlink(exe("1.0.0"), installed); | 76 | 74 |
| 77 | | - const committed = { | 78 | | - id: pending.id, | 79 | | - fromVersion: pending.fromVersion, | 80 | | - targetVersion: pending.targetVersion, | 81 | | - status: "committed" as const, | 82 | | - }; | 83 | | - host.emit({ type: "committed", updateId: committed.id }); | 84 | | - expect(yield* Fiber.join(prepared)).toEqual(committed); | 85 | | - }), | | 75 | + const pending = { | | 76 | + id: "update-1", | | 77 | + fromVersion: "1.0.0", | | 78 | + targetVersion: "1.1.0", | | 79 | + dbPath: "/tmp/state.sqlite", | | 80 | + status: "pending" as const, | | 81 | + }; | | 82 | + const host = new FakeLauncherProcess({ | | 83 | + protocol: SERVICE_LAUNCHER_PROTOCOL, | | 84 | + childVersion: "1.1.0", | | 85 | + update: pending, | | 86 | + }); | | 87 | + host.env["HOME"] = root; | | 88 | + const client = yield* makeClient(host, "1.1.0"); | | 89 | + const prepared = yield* Effect.forkChild(client.prepareTrial({ baseDir }), { | | 90 | + startImmediately: true, | | 91 | + }); | | 92 | + yield* Effect.yieldNow; | | 93 | + expect(host.sent).toEqual([{ type: "prepared", updateId: "update-1" }]); | | 94 | + assert.equal(yield* fs.readLink(installed), exe("1.0.0")); | | 95 | + | | 96 | + const committed = { | | 97 | + id: pending.id, | | 98 | + fromVersion: pending.fromVersion, | | 99 | + targetVersion: pending.targetVersion, | | 100 | + status: "committed" as const, | | 101 | + }; | | 102 | + host.emit({ type: "committed", updateId: committed.id }); | | 103 | + expect(yield* Fiber.join(prepared)).toEqual(committed); | | 104 | + assert.equal(yield* fs.readLink(installed), exe("1.1.0")); | | 105 | + | | 106 | + // A later start replays the stored outcome; user pins made since then stay put. | | 107 | + yield* fs.remove(installed); | | 108 | + yield* fs.symlink(exe("1.0.0"), installed); | | 109 | + const restarted = new FakeLauncherProcess({ | | 110 | + protocol: SERVICE_LAUNCHER_PROTOCOL, | | 111 | + childVersion: "1.1.0", | | 112 | + update: committed, | | 113 | + }); | | 114 | + restarted.env["HOME"] = root; | | 115 | + const restartedClient = yield* makeClient(restarted, "1.1.0"); | | 116 | + expect(yield* restartedClient.prepareTrial({ baseDir })).toEqual(committed); | | 117 | + assert.equal(yield* fs.readLink(installed), exe("1.0.0")); | | 118 | + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), | 86 | 119 | ); | 87 | 120 |
| 88 | 121 | it.effect("returns the launcher-generated ID only after update acceptance", () => | @@ -180,13 +213,7 @@ it.layer(NodeServices.layer)("self-update launchers", (it) => { | 180 | 213 | yield* fs.makeDirectory(path.dirname(onPath), { recursive: true }); | 181 | 214 | yield* fs.symlink(exe("1.0.0"), onPath); | 182 | 215 |
| 183 | | - yield* Effect.gen(function* () { | 184 | | - const client = yield* ServiceLauncherClient.make(); | 185 | | - yield* client.repointLaunchers({ | 186 | | - baseDir, | 187 | | - targetVersion: "2.0.0", | 188 | | - }); | 189 | | - }).pipe( | | 216 | + yield* ServiceLauncherClient.repointLaunchers({ baseDir, targetVersion: "2.0.0" }).pipe( | 190 | 217 | Effect.provideService(HostProcess.Environment, { | 191 | 218 | HOME: root, | 192 | 219 | T3CODE_INSTALL_BIN_DIR: path.dirname(installed), | @@ -237,13 +264,7 @@ it.layer(NodeServices.layer)("self-update launchers", (it) => { | 237 | 264 | : fs.rename(source, destination), | 238 | 265 | }); | 239 | 266 |
| 240 | | - yield* Effect.gen(function* () { | 241 | | - const client = yield* ServiceLauncherClient.make(); | 242 | | - yield* client.repointLaunchers({ | 243 | | - baseDir, | 244 | | - targetVersion: "2.0.0", | 245 | | - }); | 246 | | - }).pipe( | | 267 | + yield* ServiceLauncherClient.repointLaunchers({ baseDir, targetVersion: "2.0.0" }).pipe( | 247 | 268 | Effect.provideService(HostProcess.Environment, { | 248 | 269 | HOME: root, | 249 | 270 | PATH: path.dirname(onPath), | @@ -276,13 +297,7 @@ it.layer(NodeServices.layer)("self-update launchers", (it) => { | 276 | 297 | yield* fs.makeDirectory(path.dirname(onPath), { recursive: true }); | 277 | 298 | yield* fs.symlink(foreign, onPath); | 278 | 299 |
| 279 | | - yield* Effect.gen(function* () { | 280 | | - const client = yield* ServiceLauncherClient.make(); | 281 | | - yield* client.repointLaunchers({ | 282 | | - baseDir, | 283 | | - targetVersion: "2.0.0", | 284 | | - }); | 285 | | - }).pipe( | | 300 | + yield* ServiceLauncherClient.repointLaunchers({ baseDir, targetVersion: "2.0.0" }).pipe( | 286 | 301 | Effect.provideService(HostProcess.Environment, { | 287 | 302 | HOME: root, | 288 | 303 | PATH: path.dirname(onPath), | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/cloud/serviceLauncherClient.ts +102-96Lines changed: 102 additions & 96 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -102,19 +102,14 @@ export class ServiceLauncherClient extends Context.Service< | 102 | 102 | ServiceLauncherClient, | 103 | 103 | { | 104 | 104 | readonly managed: boolean; | 105 | | - readonly trialPending: boolean; | 106 | | - readonly repointLaunchers: (input: { | 107 | | - readonly baseDir: string; | 108 | | - readonly targetVersion: string; | 109 | | - }) => Effect.Effect<void>; | 110 | 105 | readonly requestUpdate: (input: { | 111 | 106 | readonly targetVersion: string; | 112 | 107 | readonly dbPath: string; | 113 | 108 | }) => Effect.Effect<string, ServiceLauncherClientError | ServiceLauncherRejectedError>; | 114 | | - readonly prepareTrial: Effect.Effect< | 115 | | - ServerSelfUpdateOutcome | undefined, | 116 | | - ServiceLauncherClientError | 117 | | - >; | | 109 | + /** Owned `t3` symlinks move to the new runtime only when this start commits the trial. */ | | 110 | + readonly prepareTrial: (input: { | | 111 | + readonly baseDir: string; | | 112 | + }) => Effect.Effect<ServerSelfUpdateOutcome | undefined, ServiceLauncherClientError>; | 118 | 113 | } | 119 | 114 | >()("t3/cloud/serviceLauncherClient") {} | 120 | 115 |
| @@ -147,10 +142,88 @@ export const resolveServiceLauncherMode = Effect.fn("cloud.service_launcher_clie | 147 | 142 | }, | 148 | 143 | ); | 149 | 144 |
| | 145 | +export const repointLaunchers = Effect.fn("cloud.service_launcher_client.repoint_launchers")( | | 146 | + function* (input: { readonly baseDir: string; readonly targetVersion: string }) { | | 147 | + const fs = yield* FileSystem.FileSystem; | | 148 | + const path = yield* Path.Path; | | 149 | + const environment = yield* HostProcess.Environment; | | 150 | + const platform = yield* HostProcess.Platform; | | 151 | + if (platform !== "linux" && platform !== "darwin") return; | | 152 | + const installBinDir = | | 153 | + environment["T3CODE_INSTALL_BIN_DIR"] || | | 154 | + (environment["HOME"] ? path.join(environment["HOME"], ".local", "bin") : undefined); | | 155 | + const candidates = new Set<string>(); | | 156 | + if (installBinDir !== undefined) candidates.add(path.join(installBinDir, "t3")); | | 157 | + for (const directory of (environment["PATH"] ?? "").split(":")) { | | 158 | + if (directory.length === 0) continue; | | 159 | + const candidate = path.join(directory, "t3"); | | 160 | + if ( | | 161 | + yield* fs.exists(candidate).pipe( | | 162 | + Effect.catch((cause) => | | 163 | + Effect.logWarning("Could not check the t3 command on PATH", { | | 164 | + launchedAs: candidate, | | 165 | + cause, | | 166 | + }).pipe(Effect.as(false)), | | 167 | + ), | | 168 | + ) | | 169 | + ) { | | 170 | + candidates.add(candidate); | | 171 | + break; | | 172 | + } | | 173 | + } | | 174 | + const versionsDir = pinnedRuntimeVersionsDir(path, input.baseDir); | | 175 | + const target = pinnedRuntimePaths(path, input.baseDir, input.targetVersion, platform); | | 176 | + for (const launchedAs of candidates) { | | 177 | + yield* Effect.scoped( | | 178 | + Effect.gen(function* () { | | 179 | + const linkTarget = yield* fs.readLink(launchedAs).pipe( | | 180 | + Effect.asSome, | | 181 | + Effect.catchIf( | | 182 | + (error) => | | 183 | + error.reason._tag === "NotFound" || | | 184 | + (error.reason.cause as NodeJS.ErrnoException | undefined)?.code === "EINVAL", | | 185 | + () => Effect.succeedNone, | | 186 | + ), | | 187 | + ); | | 188 | + if (Option.isNone(linkTarget)) return; | | 189 | + const relative = path.relative( | | 190 | + versionsDir, | | 191 | + path.resolve(path.dirname(launchedAs), linkTarget.value), | | 192 | + ); | | 193 | + if ( | | 194 | + relative.length === 0 || | | 195 | + relative === ".." || | | 196 | + relative.startsWith(`..${path.sep}`) || | | 197 | + path.isAbsolute(relative) | | 198 | + ) | | 199 | + return; | | 200 | + const temporary = yield* fs.makeTempDirectoryScoped({ | | 201 | + directory: path.dirname(launchedAs), | | 202 | + prefix: ".t3-launcher-", | | 203 | + }); | | 204 | + const link = path.join(temporary, "t3"); | | 205 | + yield* fs.symlink(target.entryPath, link); | | 206 | + yield* fs.rename(link, launchedAs); | | 207 | + }), | | 208 | + ).pipe( | | 209 | + Effect.catchCause((cause) => | | 210 | + Cause.hasInterrupts(cause) | | 211 | + ? Effect.interrupt | | 212 | + : Effect.logWarning("Could not move the t3 command to the updated version", { | | 213 | + launchedAs, | | 214 | + cause, | | 215 | + }), | | 216 | + ), | | 217 | + ); | | 218 | + } | | 219 | + }, | | 220 | +); | | 221 | + | 150 | 222 | export const make = Effect.fn("cloud.service_launcher_client.make")(function* (options?: { | 151 | 223 | readonly currentVersion?: string; | 152 | 224 | }) { | 153 | 225 | const { host, context, managed } = yield* resolveStartup(options); | | 226 | + // prepareTrial runs later, outside whatever context built the client. | 154 | 227 | const fs = yield* FileSystem.FileSystem; | 155 | 228 | const path = yield* Path.Path; | 156 | 229 | const environment = yield* HostProcess.Environment; | @@ -232,103 +305,36 @@ export const make = Effect.fn("cloud.service_launcher_client.make")(function* (o | 232 | 305 | context?.update === undefined || context.update.status === "pending" | 233 | 306 | ? undefined | 234 | 307 | : context.update; | 235 | | - const prepareTrial = | | 308 | + const prepareTrial = (input: { readonly baseDir: string }) => | 236 | 309 | pending !== undefined | 237 | 310 | ? exchange( | 238 | 311 | { type: "prepared", updateId: pending.id }, | 239 | 312 | (reply) => reply.type === "committed" && reply.updateId === pending.id, | 240 | 313 | ).pipe( | 241 | | - Effect.flatMap((reply) => { | 242 | | - if (reply.type !== "committed") { | 243 | | - return Effect.die("service launcher returned an impossible prepared response"); | 244 | | - } | 245 | | - return Effect.succeed({ | 246 | | - id: pending.id, | 247 | | - fromVersion: pending.fromVersion, | 248 | | - targetVersion: pending.targetVersion, | 249 | | - status: "committed" as const, | 250 | | - }); | 251 | | - }), | | 314 | + Effect.flatMap((reply) => | | 315 | + reply.type !== "committed" | | 316 | + ? Effect.die("service launcher returned an impossible prepared response") | | 317 | + : repointLaunchers({ | | 318 | + baseDir: input.baseDir, | | 319 | + targetVersion: pending.targetVersion, | | 320 | + }).pipe( | | 321 | + Effect.provideService(FileSystem.FileSystem, fs), | | 322 | + Effect.provideService(Path.Path, path), | | 323 | + Effect.provideService(HostProcess.Environment, environment), | | 324 | + Effect.provideService(HostProcess.Platform, platform), | | 325 | + Effect.as({ | | 326 | + id: pending.id, | | 327 | + fromVersion: pending.fromVersion, | | 328 | + targetVersion: pending.targetVersion, | | 329 | + status: "committed" as const, | | 330 | + }), | | 331 | + ), | | 332 | + ), | 252 | 333 | ) | 253 | 334 | : Effect.succeed(outcome); | 254 | 335 |
| 255 | | - const repointLaunchers = Effect.fn("cloud.service_launcher_client.repoint_launchers")( | 256 | | - function* (input: { readonly baseDir: string; readonly targetVersion: string }) { | 257 | | - if (platform !== "linux" && platform !== "darwin") return; | 258 | | - const installBinDir = | 259 | | - environment["T3CODE_INSTALL_BIN_DIR"] || | 260 | | - (environment["HOME"] ? path.join(environment["HOME"], ".local", "bin") : undefined); | 261 | | - const candidates = new Set<string>(); | 262 | | - if (installBinDir !== undefined) candidates.add(path.join(installBinDir, "t3")); | 263 | | - for (const directory of (environment["PATH"] ?? "").split(":")) { | 264 | | - if (directory.length === 0) continue; | 265 | | - const candidate = path.join(directory, "t3"); | 266 | | - if ( | 267 | | - yield* fs.exists(candidate).pipe( | 268 | | - Effect.catch((cause) => | 269 | | - Effect.logWarning("Could not check the t3 command on PATH", { | 270 | | - launchedAs: candidate, | 271 | | - cause, | 272 | | - }).pipe(Effect.as(false)), | 273 | | - ), | 274 | | - ) | 275 | | - ) { | 276 | | - candidates.add(candidate); | 277 | | - break; | 278 | | - } | 279 | | - } | 280 | | - const versionsDir = pinnedRuntimeVersionsDir(path, input.baseDir); | 281 | | - const target = pinnedRuntimePaths(path, input.baseDir, input.targetVersion, platform); | 282 | | - for (const launchedAs of candidates) { | 283 | | - yield* Effect.scoped( | 284 | | - Effect.gen(function* () { | 285 | | - const linkTarget = yield* fs.readLink(launchedAs).pipe( | 286 | | - Effect.asSome, | 287 | | - Effect.catchIf( | 288 | | - (error) => | 289 | | - error.reason._tag === "NotFound" || | 290 | | - (error.reason.cause as NodeJS.ErrnoException | undefined)?.code === "EINVAL", | 291 | | - () => Effect.succeedNone, | 292 | | - ), | 293 | | - ); | 294 | | - if (Option.isNone(linkTarget)) return; | 295 | | - const relative = path.relative( | 296 | | - versionsDir, | 297 | | - path.resolve(path.dirname(launchedAs), linkTarget.value), | 298 | | - ); | 299 | | - if ( | 300 | | - relative.length === 0 || | 301 | | - relative === ".." || | 302 | | - relative.startsWith(`..${path.sep}`) || | 303 | | - path.isAbsolute(relative) | 304 | | - ) | 305 | | - return; | 306 | | - const temporary = yield* fs.makeTempDirectoryScoped({ | 307 | | - directory: path.dirname(launchedAs), | 308 | | - prefix: ".t3-launcher-", | 309 | | - }); | 310 | | - const link = path.join(temporary, "t3"); | 311 | | - yield* fs.symlink(target.entryPath, link); | 312 | | - yield* fs.rename(link, launchedAs); | 313 | | - }), | 314 | | - ).pipe( | 315 | | - Effect.catchCause((cause) => | 316 | | - Cause.hasInterrupts(cause) | 317 | | - ? Effect.interrupt | 318 | | - : Effect.logWarning("Could not move the t3 command to the updated version", { | 319 | | - launchedAs, | 320 | | - cause, | 321 | | - }), | 322 | | - ), | 323 | | - ); | 324 | | - } | 325 | | - }, | 326 | | - ); | 327 | | - | 328 | 336 | return ServiceLauncherClient.of({ | 329 | 337 | managed, | 330 | | - trialPending: pending !== undefined, | 331 | | - repointLaunchers, | 332 | 338 | requestUpdate, | 333 | 339 | prepareTrial, | 334 | 340 | }); | |
|