GeezerVison is showing github.com — fix(server): a commit, push or pull request finishes when its connect… · Lazurio/t3code@3e199b8
File treeExpand file treeCollapse file tree Open diff view settings packages/client-runtime/src/state
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +54Lines changed: 54 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { describe, expect, it } from "@effect/vitest"; | | 2 | +import * as Deferred from "effect/Deferred"; | | 3 | +import * as Effect from "effect/Effect"; | | 4 | +import * as Fiber from "effect/Fiber"; | | 5 | +import * as Stream from "effect/Stream"; | | 6 | + | | 7 | +import { detachedProgressStream } from "./detachedProgressStream.ts"; | | 8 | + | | 9 | +describe("detachedProgressStream", () => { | | 10 | + it.effect("finishes the work after the client's stream is interrupted", () => | | 11 | + Effect.gen(function* () { | | 12 | + const pushing = yield* Deferred.make<void>(); | | 13 | + const releasePush = yield* Deferred.make<void>(); | | 14 | + const outcome = yield* Deferred.make<"pull request created" | "interrupted">(); | | 15 | + | | 16 | + const consumed = yield* detachedProgressStream<string, never, never>((offer) => | | 17 | + offer("committed").pipe( | | 18 | + Effect.andThen(Deferred.succeed(pushing, undefined)), | | 19 | + Effect.andThen(Deferred.await(releasePush)), | | 20 | + Effect.andThen(offer("pushed")), | | 21 | + Effect.andThen(Deferred.succeed(outcome, "pull request created")), | | 22 | + Effect.onInterrupt(() => Deferred.succeed(outcome, "interrupted")), | | 23 | + ), | | 24 | + ).pipe(Stream.runDrain, Effect.forkChild); | | 25 | + | | 26 | + yield* Deferred.await(pushing); | | 27 | + // The socket closes: Effect RPC interrupts the client's request fiber. | | 28 | + yield* Fiber.interrupt(consumed); | | 29 | + yield* Deferred.succeed(releasePush, undefined); | | 30 | + | | 31 | + expect(yield* Deferred.await(outcome)).toBe("pull request created"); | | 32 | + }), | | 33 | + ); | | 34 | + | | 35 | + it.effect("streams progress and ends when the work succeeds", () => | | 36 | + Effect.gen(function* () { | | 37 | + const events = yield* detachedProgressStream<string, never, never>((offer) => | | 38 | + offer("committed").pipe(Effect.andThen(offer("pushed"))), | | 39 | + ).pipe(Stream.runCollect); | | 40 | + | | 41 | + expect(events).toEqual(["committed", "pushed"]); | | 42 | + }), | | 43 | + ); | | 44 | + | | 45 | + it.effect("fails the stream with the work's own error", () => | | 46 | + Effect.gen(function* () { | | 47 | + const error = yield* detachedProgressStream<string, string, never>((offer) => | | 48 | + offer("committed").pipe(Effect.andThen(Effect.fail("push rejected"))), | | 49 | + ).pipe(Stream.runDrain, Effect.flip); | | 50 | + | | 51 | + expect(error).toBe("push rejected"); | | 52 | + }), | | 53 | + ); | | 54 | +}); |
Collapse file Copy file name to clipboard +21Lines changed: 21 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import * as Effect from "effect/Effect"; | | 2 | +import * as Queue from "effect/Queue"; | | 3 | +import * as Stream from "effect/Stream"; | | 4 | + | | 5 | +// Effect RPC interrupts every fiber of a client whose socket closes, so work that ran in the | | 6 | +// stream's fiber stopped halfway: a commit without its push, a push without its pull request. | | 7 | +// The work runs detached instead. Progress offered after the stream ends is dropped; the work | | 8 | +// still finishes, and its result reaches the client through state it already resyncs. | | 9 | +export const detachedProgressStream = <A, E, R>( | | 10 | + run: (offer: (event: A) => Effect.Effect<void>) => Effect.Effect<unknown, E, R>, | | 11 | +): Stream.Stream<A, E, R> => | | 12 | + Stream.callback<A, E, R>((queue) => | | 13 | + run((event) => Queue.offer(queue, event).pipe(Effect.ignore)).pipe( | | 14 | + Effect.matchCauseEffect({ | | 15 | + onFailure: (cause) => Queue.failCause(queue, cause), | | 16 | + onSuccess: () => Queue.end(queue), | | 17 | + }), | | 18 | + Effect.ignore, | | 19 | + Effect.forkDetach, | | 20 | + ), | | 21 | + ); |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/ws.ts +25-32Lines changed: 25 additions & 32 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -218,6 +218,7 @@ import * as VcsProvisioningService from "./vcs/VcsProvisioningService.ts"; | 218 | 218 | import * as GitWorkflowService from "./git/GitWorkflowService.ts"; | 219 | 219 | import { refreshPushedPullRequests } from "./git/refreshPushedPullRequests.ts"; | 220 | 220 | import { linkCreatedPullRequest } from "./git/linkCreatedPullRequest.ts"; | | 221 | +import { detachedProgressStream } from "./git/detachedProgressStream.ts"; | 221 | 222 | import * as ReviewService from "./review/ReviewService.ts"; | 222 | 223 | import * as ProjectEnrichmentService from "./project/ProjectEnrichmentService.ts"; | 223 | 224 | import * as ProjectService from "./project/ProjectService.ts"; | @@ -3141,45 +3142,37 @@ export const layerWsRpc = ( | 3141 | 3142 | }), | 3142 | 3143 | ), | 3143 | 3144 | [WS_METHODS.gitRunStackedAction]: (input) => | 3144 | | - Stream.callback<GitActionProgressEvent, GitManagerServiceError>((queue) => | | 3145 | + detachedProgressStream<GitActionProgressEvent, GitManagerServiceError, never>((offer) => | 3145 | 3146 | gitWorkflow | 3146 | 3147 | .runStackedAction(input, { | 3147 | 3148 | actionId: input.actionId, | 3148 | | - progressReporter: { | 3149 | | - publish: (event) => Queue.offer(queue, event).pipe(Effect.asVoid), | 3150 | | - }, | | 3149 | + progressReporter: { publish: offer }, | 3151 | 3150 | }) | 3152 | 3151 | .pipe( | 3153 | | - Effect.matchCauseEffect({ | 3154 | | - // Earlier steps (branch, commit) may have landed before the failing one. | 3155 | | - onFailure: (cause) => | 3156 | | - refreshGitStatus(input.cwd).pipe(Effect.andThen(Queue.failCause(queue, cause))), | 3157 | | - onSuccess: (result) => | 3158 | | - (input.threadId === undefined | 3159 | | - ? Effect.void | 3160 | | - : linkCreatedPullRequest({ | 3161 | | - threadId: input.threadId, | 3162 | | - result, | 3163 | | - commandId: serverCommandId("pr-created-link"), | 3164 | | - }).pipe( | 3165 | | - Effect.provideService(Orchestrator.OrchestratorV2, orchestrationEngine), | 3166 | | - Effect.provideService(ProjectService.ProjectService, projectService), | 3167 | | - ) | 3168 | | - ).pipe( | 3169 | | - Effect.andThen( | 3170 | | - refreshPushedPullRequests(input, result).pipe( | 3171 | | - Effect.provideService(Orchestrator.OrchestratorV2, orchestrationEngine), | 3172 | | - Effect.provideService(ProjectStore.ProjectStoreV2, projectStore), | 3173 | | - Effect.provideService( | 3174 | | - PullRequestService.PullRequestService, | 3175 | | - pullRequests, | 3176 | | - ), | 3177 | | - ), | | 3152 | + // Earlier steps (branch, commit) may have landed before the failing one. | | 3153 | + Effect.tapCause(() => refreshGitStatus(input.cwd)), | | 3154 | + Effect.flatMap((result) => | | 3155 | + (input.threadId === undefined | | 3156 | + ? Effect.void | | 3157 | + : linkCreatedPullRequest({ | | 3158 | + threadId: input.threadId, | | 3159 | + result, | | 3160 | + commandId: serverCommandId("pr-created-link"), | | 3161 | + }).pipe( | | 3162 | + Effect.provideService(Orchestrator.OrchestratorV2, orchestrationEngine), | | 3163 | + Effect.provideService(ProjectService.ProjectService, projectService), | | 3164 | + ) | | 3165 | + ).pipe( | | 3166 | + Effect.andThen( | | 3167 | + refreshPushedPullRequests(input, result).pipe( | | 3168 | + Effect.provideService(Orchestrator.OrchestratorV2, orchestrationEngine), | | 3169 | + Effect.provideService(ProjectStore.ProjectStoreV2, projectStore), | | 3170 | + Effect.provideService(PullRequestService.PullRequestService, pullRequests), | 3178 | 3171 | ), | 3179 | | - Effect.andThen(refreshGitStatus(input.cwd)), | 3180 | | - Effect.andThen(Queue.end(queue).pipe(Effect.asVoid)), | 3181 | 3172 | ), | 3182 | | - }), | | 3173 | + Effect.andThen(refreshGitStatus(input.cwd)), | | 3174 | + ), | | 3175 | + ), | 3183 | 3176 | ), | 3184 | 3177 | ), | 3185 | 3178 | [WS_METHODS.gitResolvePullRequest]: (input) => gitWorkflow.resolvePullRequest(input), | |
Collapse file Copy file name to clipboardExpand all lines: packages/client-runtime/src/state/vcsAction.test.ts +40Lines changed: 40 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -17,6 +17,8 @@ import * as Layer from "effect/Layer"; | 17 | 17 | import * as Option from "effect/Option"; | 18 | 18 | import * as Stream from "effect/Stream"; | 19 | 19 | import * as SubscriptionRef from "effect/SubscriptionRef"; | | 20 | +import { RpcClientError } from "effect/rpc"; | | 21 | +import * as Socket from "effect/socket/Socket"; | 20 | 22 | import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity"; | 21 | 23 |
| 22 | 24 | import { | @@ -40,6 +42,7 @@ import { | 40 | 42 | EMPTY_VCS_ACTION_STATE, | 41 | 43 | normalizeVcsActionProgressEvent, | 42 | 44 | parseVcsActionTargetKey, | | 45 | + VcsActionConnectionLostError, | 43 | 46 | VcsActionMissingTerminalEventError, | 44 | 47 | VcsActionRemoteFailureError, | 45 | 48 | VcsActionTargetKeyParseError, | @@ -378,6 +381,43 @@ describe("vcsActionState", () => { | 378 | 381 | }), | 379 | 382 | ); | 380 | 383 |
| | 384 | + it.effect("reports a dropped connection as lost, not as a failed action", () => | | 385 | + Effect.gen(function* () { | | 386 | + const target = { environmentId, cwd }; | | 387 | + const transportActionId = createVcsActionTransportId(target, actionId); | | 388 | + const error = yield* consumeVcsActionProgress( | | 389 | + Stream.fromIterable<GitActionProgressEvent>([ | | 390 | + { | | 391 | + actionId: transportActionId, | | 392 | + action, | | 393 | + cwd, | | 394 | + kind: "phase_started", | | 395 | + phase: "push", | | 396 | + label: "Pushing...", | | 397 | + }, | | 398 | + ]).pipe( | | 399 | + Stream.concat( | | 400 | + Stream.fail( | | 401 | + new RpcClientError.RpcClientError({ | | 402 | + reason: new Socket.SocketCloseError({ code: 1006 }), | | 403 | + }), | | 404 | + ), | | 405 | + ), | | 406 | + ), | | 407 | + { | | 408 | + target, | | 409 | + transportActionId, | | 410 | + actionId, | | 411 | + action, | | 412 | + onProgress: () => Effect.void, | | 413 | + }, | | 414 | + ).pipe(Effect.flip); | | 415 | + | | 416 | + expect(error).toBeInstanceOf(VcsActionConnectionLostError); | | 417 | + expect(error).toMatchObject({ actionId, transportActionId, action, environmentId, cwd }); | | 418 | + }), | | 419 | + ); | | 420 | + | 381 | 421 | it("tracks finite mutations without letting an older completion clear newer state", async () => { | 382 | 422 | const runtime = Atom.runtime(Layer.empty) as unknown as Atom.AtomRuntime< | 383 | 423 | EnvironmentRegistry.EnvironmentRegistry | Persistence.EnvironmentCacheStore, | |
Collapse file Copy file name to clipboardExpand all lines: packages/client-runtime/src/state/vcsAction.ts +32-1Lines changed: 32 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,7 +19,7 @@ import { AsyncResult, Atom, type AtomRegistry } from "effect/reactivity"; | 19 | 19 |
| 20 | 20 | import type { EnvironmentRegistry } from "../connection/registry.ts"; | 21 | 21 | import * as Persistence from "../platform/persistence.ts"; | 22 | | -import { runStreamGuarded, RpcPermissionGuard } from "../rpc/client.ts"; | | 22 | +import { isRpcClientError, runStreamGuarded, RpcPermissionGuard } from "../rpc/client.ts"; | 23 | 23 | import { | 24 | 24 | createRuntimeCommand, | 25 | 25 | runStreamInEnvironment, | @@ -130,6 +130,21 @@ export class VcsActionMissingTerminalEventError extends Schema.TaggedError<VcsAc | 130 | 130 | } | 131 | 131 | } | 132 | 132 |
| | 133 | +export class VcsActionConnectionLostError extends Schema.TaggedError<VcsActionConnectionLostError>()( | | 134 | + "VcsActionConnectionLostError", | | 135 | + { | | 136 | + actionId: Schema.String, | | 137 | + transportActionId: Schema.String, | | 138 | + action: GitStackedAction, | | 139 | + environmentId: EnvironmentId, | | 140 | + cwd: Schema.String, | | 141 | + }, | | 142 | +) { | | 143 | + override get message(): string { | | 144 | + return `Lost the connection to the server during '${this.action}'. The action can still finish there; the status updates once the connection returns.`; | | 145 | + } | | 146 | +} | | 147 | + | 133 | 148 | export class VcsActionTargetKeyParseError extends Schema.TaggedError<VcsActionTargetKeyParseError>()( | 134 | 149 | "VcsActionTargetKeyParseError", | 135 | 150 | { | @@ -145,6 +160,7 @@ export class VcsActionTargetKeyParseError extends Schema.TaggedError<VcsActionTa | 145 | 160 | export const VcsActionExecutionError = Schema.Union([ | 146 | 161 | VcsActionRemoteFailureError, | 147 | 162 | VcsActionMissingTerminalEventError, | | 163 | + VcsActionConnectionLostError, | 148 | 164 | ]); | 149 | 165 | export type VcsActionExecutionError = typeof VcsActionExecutionError.Type; | 150 | 166 |
| @@ -313,6 +329,21 @@ export function consumeVcsActionProgress<E, R>( | 313 | 329 | }), | 314 | 330 | ); | 315 | 331 | }), | | 332 | + // The server runs the action detached from the socket, so losing the transport says | | 333 | + // nothing about whether the action failed. | | 334 | + Effect.catchIf( | | 335 | + (error) => isRpcClientError(error) && error.reason._tag !== "RpcClientDefect", | | 336 | + () => | | 337 | + Effect.fail<VcsActionExecutionError>( | | 338 | + new VcsActionConnectionLostError({ | | 339 | + actionId: input.actionId, | | 340 | + transportActionId: input.transportActionId, | | 341 | + action: input.action, | | 342 | + environmentId: input.target.environmentId, | | 343 | + cwd: input.target.cwd, | | 344 | + }), | | 345 | + ), | | 346 | + ), | 316 | 347 | ); | 317 | 348 | }); | 318 | 349 | } | |
|