GeezerVison is showing github.com — fix(pr): action replies are opt-in for old clients and keep post-muta… · pingdotgg/t3code@25ff493
File treeExpand file treeCollapse file tree Open diff view settings apps/server/src/pullRequest
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/pullRequest/PullRequestService.test.ts +7-1Lines changed: 7 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1248,7 +1248,13 @@ it.effect("returns the host's merged reply as a summary and trusts it without an | 1248 | 1248 | const observedAt = Date.parse("2026-09-03T02:00:01.000Z"); | 1249 | 1249 | yield* TestClock.setTime(observedAt); | 1250 | 1250 |
| 1251 | | - const result = yield* service.runAction({ ...reference, action: "merge" }); | | 1251 | + // Clients that did not ask decode the success as void, so they must get nothing back. | | 1252 | + assert.isUndefined(yield* service.runAction({ ...reference, action: "merge" })); | | 1253 | + const result = yield* service.runAction({ | | 1254 | + ...reference, | | 1255 | + action: "merge", | | 1256 | + replyWithSummary: true, | | 1257 | + }); | 1252 | 1258 |
| 1253 | 1259 | assert.deepStrictEqual(result, { | 1254 | 1260 | ...merged, | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/pullRequest/PullRequestService.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -3621,7 +3621,7 @@ export const make = Effect.gen(function* () { | 3621 | 3621 | }); | 3622 | 3622 | } | 3623 | 3623 | } | 3624 | | - return summary; | | 3624 | + return input.replyWithSummary === true ? summary : undefined; | 3625 | 3625 | }); | 3626 | 3626 |
| 3627 | 3627 | const credentialCached = | |
Collapse file Copy file name to clipboardExpand all lines: packages/client-runtime/src/state/pullRequests.test.ts +65Lines changed: 65 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1256,6 +1256,71 @@ it.effect("keeps an action's reply when a refresh started before the action answ | 1256 | 1256 | ), | 1257 | 1257 | ); | 1258 | 1258 |
| | 1259 | +it.effect("keeps a list read the server's refresh started before the action replied", () => | | 1260 | + Effect.scoped( | | 1261 | + Effect.gen(function* () { | | 1262 | + const refreshEvents = yield* PubSub.unbounded<number>(); | | 1263 | + let state: "open" | "merged" = "open"; | | 1264 | + let reads = 0; | | 1265 | + const actionInputs: unknown[] = []; | | 1266 | + const listWaiting = Latch.makeUnsafe(); | | 1267 | + const release = yield* Deferred.make<void>(); | | 1268 | + const client = actionReplyClient({ | | 1269 | + [WS_METHODS.pullRequestsSubscribeRefreshes]: () => Stream.fromPubSub(refreshEvents), | | 1270 | + [WS_METHODS.pullRequestsList]: () => | | 1271 | + Effect.gen(function* () { | | 1272 | + reads++; | | 1273 | + const entries = [ | | 1274 | + { ...actionReference, state, isDraft: false, updatedAt: actionReply.updatedAt }, | | 1275 | + ]; | | 1276 | + if (reads === 2) yield* Deferred.await(release); | | 1277 | + return { | | 1278 | + entries, | | 1279 | + viewers: {}, | | 1280 | + providers: [], | | 1281 | + errors: [], | | 1282 | + truncated: false, | | 1283 | + nextCursors: {}, | | 1284 | + }; | | 1285 | + }), | | 1286 | + [WS_METHODS.pullRequestsRunAction]: (input: unknown) => | | 1287 | + Effect.gen(function* () { | | 1288 | + actionInputs.push(input); | | 1289 | + state = "merged"; | | 1290 | + // The server announces its refresh before it replies. | | 1291 | + yield* PubSub.publish(refreshEvents, 1); | | 1292 | + yield* listWaiting.await; | | 1293 | + return actionReply; | | 1294 | + }), | | 1295 | + }); | | 1296 | + const { atoms, registry } = yield* makeTestRuntime(client); | | 1297 | + const target = { environmentId: TARGET.environmentId, input: actionReference }; | | 1298 | + const list = atoms.list({ environmentId: target.environmentId, input: { state: "all" } }); | | 1299 | + const unmount = registry.mount(list); | | 1300 | + yield* Effect.addFinalizer(() => Effect.sync(unmount)); | | 1301 | + yield* Effect.promise(() => executeAtomQuery(registry, list)); | | 1302 | + const stop = registry.subscribe(list, (result) => { | | 1303 | + if (result.waiting) listWaiting.openUnsafe(); | | 1304 | + }); | | 1305 | + yield* Effect.addFinalizer(() => Effect.sync(stop)); | | 1306 | + | | 1307 | + yield* Effect.promise(() => | | 1308 | + atoms.runAction.run(registry, { | | 1309 | + ...target, | | 1310 | + input: { ...actionReference, action: "merge" }, | | 1311 | + }), | | 1312 | + ); | | 1313 | + yield* Deferred.succeed(release, undefined); | | 1314 | + | | 1315 | + expect( | | 1316 | + (yield* AtomRegistry.getResult(registry, list, { suspendOnWaiting: true })).entries, | | 1317 | + ).toMatchObject([{ state: "merged" }]); | | 1318 | + expect(reads).toBe(2); | | 1319 | + expect(actionInputs).toEqual([expect.objectContaining({ replyWithSummary: true })]); | | 1320 | + }), | | 1321 | + ), | | 1322 | +); | | 1323 | + | 1259 | 1324 | it.effect("shows a reopen reply at once and re-reads the fields it lacks", () => | 1260 | 1325 | Effect.scoped( | 1261 | 1326 | Effect.gen(function* () { | |
Collapse file Copy file name to clipboardExpand all lines: packages/client-runtime/src/state/pullRequests.ts +74-41Lines changed: 74 additions & 41 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -127,12 +127,17 @@ function updateCached<A, E>( | 127 | 127 | registry.update(atom, AsyncResult.map(update)); | 128 | 128 | } | 129 | 129 |
| 130 | | -type ActionReplyPatch = ( | 131 | | - registry: AtomRegistry.AtomRegistry, | 132 | | - environmentId: EnvironmentId, | 133 | | - reference: PullRequestRef, | 134 | | - reply: PullRequestSummary | undefined, | 135 | | -) => Effect.Effect<void>; | | 130 | +interface ActionReplyPatch { | | 131 | + /** The read this reader is waiting on, if any. */ | | 132 | + readonly pendingRead: (registry: AtomRegistry.AtomRegistry) => object | undefined; | | 133 | + readonly apply: ( | | 134 | + registry: AtomRegistry.AtomRegistry, | | 135 | + environmentId: EnvironmentId, | | 136 | + reference: PullRequestRef, | | 137 | + reply: PullRequestSummary | undefined, | | 138 | + readsBeforeSend: ReadonlySet<object>, | | 139 | + ) => Effect.Effect<void>; | | 140 | +} | 136 | 141 |
| 137 | 142 | // Module-wide so readers built by separate factories, such as linked summaries, are patched too. | 138 | 143 | const actionReplyPatches = new WeakMap<object, ActionReplyPatch>(); | @@ -252,38 +257,54 @@ function actionPatchedQuery<Input, A, E>( | 252 | 257 | writableQuery(atom, options.idleTtlMs), | 253 | 258 | ); | 254 | 259 | return (target: { readonly environmentId: EnvironmentId; readonly input: Input }) => { | 255 | | - const atom = writable(source(target)); | | 260 | + const query = source(target); | | 261 | + const atom = writable(query); | 256 | 262 | if (actionReplyPatches.has(atom)) return atom; | 257 | | - actionReplyPatches.set( | 258 | | - atom, | 259 | | - Effect.fnUntraced(function* (registry, environmentId, reference, reply) { | 260 | | - if (environmentId !== target.environmentId || !options.concerns(target.input, reference)) | 261 | | - return; | 262 | | - if (reply === undefined) { | 263 | | - return options.rereadWithoutReply === true ? registry.refresh(atom) : undefined; | 264 | | - } | 265 | | - const shown = Option.flatMap( | 266 | | - registry.getNodes().get(atom)?.valueOption() ?? Option.none(), | 267 | | - (current: AsyncResult.AsyncResult<A, E>) => AsyncResult.value(current), | 268 | | - ); | 269 | | - // A read with nothing to show began before the action; restart it rather than guess. | 270 | | - if (Option.isNone(shown)) return registry.refresh(atom); | 271 | | - const patched = options.patch(target.input, shown.value, reference, reply); | 272 | | - if (patched === "refresh") return registry.refresh(atom); | 273 | | - const reread = options.rereads?.(shown.value, reply) === true; | 274 | | - const next = reuseUnchanged(shown.value, patched); | 275 | | - if (next === shown.value) return reread ? registry.refresh(atom) : undefined; | 276 | | - yield* options.onAnswer?.(target, next, registry) ?? Effect.void; | 277 | | - updateCached(registry, atom, () => next, reread); | 278 | | - if (options.persistence !== undefined) { | 279 | | - yield* options.persistence.save( | 280 | | - environmentId, | 281 | | - queryPersistenceKey(options.label, target.input), | 282 | | - next, | | 263 | + const pendingRead = (registry: AtomRegistry.AtomRegistry) => { | | 264 | + const result = Option.getOrUndefined( | | 265 | + registry.getNodes().get(query)?.valueOption() ?? Option.none(), | | 266 | + ) as AsyncResult.AsyncResult<A, E> | undefined; | | 267 | + return result?.waiting === true ? result : undefined; | | 268 | + }; | | 269 | + actionReplyPatches.set(atom, { | | 270 | + pendingRead, | | 271 | + apply: Effect.fnUntraced( | | 272 | + function* (registry, environmentId, reference, reply, readsBeforeSend) { | | 273 | + if (environmentId !== target.environmentId || !options.concerns(target.input, reference)) | | 274 | + return; | | 275 | + const pending = pendingRead(registry); | | 276 | + // A read sent after the action already sees its result; one sent before may not. | | 277 | + const freshRead = pending !== undefined && !readsBeforeSend.has(pending); | | 278 | + const reread = () => (freshRead ? undefined : registry.refresh(atom)); | | 279 | + if (reply === undefined) { | | 280 | + return options.rereadWithoutReply === true ? reread() : undefined; | | 281 | + } | | 282 | + const shown = Option.flatMap( | | 283 | + registry.getNodes().get(atom)?.valueOption() ?? Option.none(), | | 284 | + (current: AsyncResult.AsyncResult<A, E>) => AsyncResult.value(current), | 283 | 285 | ); | 284 | | - } | 285 | | - }), | 286 | | - ); | | 286 | + if (Option.isNone(shown)) return reread(); | | 287 | + const patched = options.patch(target.input, shown.value, reference, reply); | | 288 | + if (patched === "refresh") return reread(); | | 289 | + const lacking = options.rereads?.(shown.value, reply) === true; | | 290 | + const next = reuseUnchanged(shown.value, patched); | | 291 | + if (next === shown.value) return lacking ? reread() : undefined; | | 292 | + yield* options.onAnswer?.(target, next, registry) ?? Effect.void; | | 293 | + if (lacking || (pending !== undefined && !freshRead)) registry.refresh(atom); | | 294 | + registry.update( | | 295 | + atom, | | 296 | + AsyncResult.map(() => next), | | 297 | + ); | | 298 | + if (options.persistence !== undefined) { | | 299 | + yield* options.persistence.save( | | 300 | + environmentId, | | 301 | + queryPersistenceKey(options.label, target.input), | | 302 | + next, | | 303 | + ); | | 304 | + } | | 305 | + }, | | 306 | + ), | | 307 | + }); | 287 | 308 | return atom; | 288 | 309 | }; | 289 | 310 | } | @@ -590,17 +611,27 @@ export function createPullRequestEnvironmentAtoms<R, E>( | 590 | 611 | }); | 591 | 612 | preparedInput = { ...actionInput, mergeMethod }; | 592 | 613 | } | 593 | | - return yield* routedRequest(WS_METHODS.pullRequestsRunAction, preparedInput); | | 614 | + return yield* routedRequest(WS_METHODS.pullRequestsRunAction, { | | 615 | + ...preparedInput, | | 616 | + replyWithSummary: true, | | 617 | + }); | 594 | 618 | }); | | 619 | + const actionReplyReaders = (registry: AtomRegistry.AtomRegistry) => | | 620 | + Array.from(registry.getNodes().keys()).flatMap((atom) => { | | 621 | + const patch = typeof atom === "string" ? undefined : actionReplyPatches.get(atom); | | 622 | + return patch === undefined ? [] : [patch]; | | 623 | + }); | | 624 | + const readsInFlight = (registry: AtomRegistry.AtomRegistry): ReadonlySet<object> => | | 625 | + new Set(actionReplyReaders(registry).flatMap((reader) => reader.pendingRead(registry) ?? [])); | 595 | 626 | const applyActionReply = Effect.fn("PullRequests.applyActionReply")(function* ( | 596 | 627 | registry: AtomRegistry.AtomRegistry, | 597 | 628 | environmentId: EnvironmentId, | 598 | 629 | reference: PullRequestRef, | 599 | 630 | reply: PullRequestSummary | undefined, | | 631 | + readsBeforeSend: ReadonlySet<object>, | 600 | 632 | ) { | 601 | | - for (const atom of registry.getNodes().keys()) { | 602 | | - const patch = typeof atom === "string" ? undefined : actionReplyPatches.get(atom); | 603 | | - if (patch !== undefined) yield* patch(registry, environmentId, reference, reply); | | 633 | + for (const reader of actionReplyReaders(registry)) { | | 634 | + yield* reader.apply(registry, environmentId, reference, reply, readsBeforeSend); | 604 | 635 | } | 605 | 636 | if (reply !== undefined && reference.expectedAccountId === undefined) { | 606 | 637 | yield* ( | @@ -761,8 +792,9 @@ export function createPullRequestEnvironmentAtoms<R, E>( | 761 | 792 | // Preparation belongs to the write's lane. Refreshable queries would restart it after | 762 | 793 | // every preceding action, and preparing outside the lane could reorder the clicks. | 763 | 794 | execute: runAction, | | 795 | + onStart: (_target, registry) => readsInFlight(registry), | 764 | 796 | // Loaded readers show the reply, or re-read when the host's reply lacked the pull request. | 765 | | - onSuccess: ({ environmentId, input }, registry, reply) => { | | 797 | + onSuccess: ({ environmentId, input }, registry, reply, readsBeforeSend) => { | 766 | 798 | const { projectId, host, repository, number, expectedAccountId } = input; | 767 | 799 | return applyActionReply( | 768 | 800 | registry, | @@ -775,6 +807,7 @@ export function createPullRequestEnvironmentAtoms<R, E>( | 775 | 807 | ...(expectedAccountId === undefined ? {} : { expectedAccountId }), | 776 | 808 | }, | 777 | 809 | reply, | | 810 | + readsBeforeSend ?? new Set(), | 778 | 811 | ); | 779 | 812 | }, | 780 | 813 | scheduler: commandScheduler, | |
Collapse file Copy file name to clipboardExpand all lines: packages/client-runtime/src/state/runtime.ts +18-2Lines changed: 18 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -950,11 +950,20 @@ export function createEnvironmentRpcCommand< | 950 | 950 | ER, | 951 | 951 | TTag extends EnvironmentUnaryRpcTag, | 952 | 952 | Input extends EnvironmentRpcInput<TTag> = EnvironmentRpcInput<TTag>, | | 953 | + Started = undefined, | 953 | 954 | >( | 954 | 955 | runtime: Atom.AtomRuntime<EnvironmentRegistry.EnvironmentRegistry | R, ER>, | 955 | 956 | options: { | 956 | 957 | readonly label: string; | 957 | 958 | readonly tag: TTag; | | 959 | + /** Runs once authorized, as the request starts; what it returns reaches `onSuccess`. */ | | 960 | + readonly onStart?: ( | | 961 | + target: { | | 962 | + readonly environmentId: EnvironmentIdType; | | 963 | + readonly input: NoInfer<Input>; | | 964 | + }, | | 965 | + registry: AtomRegistry.AtomRegistry, | | 966 | + ) => Started; | 958 | 967 | readonly execute?: ( | 959 | 968 | input: Input, | 960 | 969 | ) => Effect.Effect< | @@ -974,6 +983,7 @@ export function createEnvironmentRpcCommand< | 974 | 983 | }, | 975 | 984 | registry: AtomRegistry.AtomRegistry, | 976 | 985 | result: EnvironmentRpcSuccess<TTag>, | | 986 | + started: Started | undefined, | 977 | 987 | ) => Effect.Effect<void, never, R>; | 978 | 988 | readonly onSettled?: ( | 979 | 989 | target: { | @@ -994,15 +1004,21 @@ export function createEnvironmentRpcCommand< | 994 | 1004 | environmentId, | 995 | 1005 | input, | 996 | 1006 | }; | | 1007 | + let started: Started | undefined; | 997 | 1008 | // Routing requires consent on the origin as well as the actual destination. | 998 | 1009 | // The transport check below deliberately checks the destination again. | 999 | 1010 | return permissions.authorize(registry, environmentId, input).pipe( | 1000 | | - Effect.andThen(() => options.execute?.(input) ?? requestGuarded(options.tag, input)), | | 1011 | + Effect.andThen(() => { | | 1012 | + started = options.onStart?.(target, registry); | | 1013 | + return options.execute?.(input) ?? requestGuarded(options.tag, input); | | 1014 | + }), | 1001 | 1015 | Effect.provideService(RpcPermissionGuard, { | 1002 | 1016 | authorize: (id, method, payload) => | 1003 | 1017 | createCommandPermissions(runtime, method).authorize(registry, id, payload), | 1004 | 1018 | }), | 1005 | | - Effect.tap((result) => options.onSuccess?.(target, registry, result) ?? Effect.void), | | 1019 | + Effect.tap( | | 1020 | + (result) => options.onSuccess?.(target, registry, result, started) ?? Effect.void, | | 1021 | + ), | 1006 | 1022 | Effect.ensuring(options.onSettled?.(target, registry) ?? Effect.void), | 1007 | 1023 | ); | 1008 | 1024 | }, | |
Collapse file Copy file name to clipboardExpand all lines: packages/contracts/src/pullRequest.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -739,6 +739,8 @@ export const PullRequestActionInput = Schema.Struct({ | 739 | 739 | action: PullRequestAction, | 740 | 740 | mergeMethod: Schema.optional(PullRequestMergeMethod), | 741 | 741 | updateMethod: Schema.optional(PullRequestUpdateMethod), | | 742 | + /** Opt-in, because older clients decode the action's success as void and reject a summary. */ | | 743 | + replyWithSummary: Schema.optional(Schema.Literal(true)), | 742 | 744 | }); | 743 | 745 | export type PullRequestActionInput = typeof PullRequestActionInput.Type; | 744 | 746 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/contracts/src/rpc.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1116,7 +1116,7 @@ const WsPullRequestsSetFilesViewedRpc = Rpc.make(WS_METHODS.pullRequestsSetFiles | 1116 | 1116 |
| 1117 | 1117 | const WsPullRequestsRunActionRpc = Rpc.make(WS_METHODS.pullRequestsRunAction, { | 1118 | 1118 | payload: PullRequestActionInput, | 1119 | | - // Undefined when the host's reply did not carry the change request; clients then re-read. | | 1119 | + // Undefined unless asked for with replyWithSummary, or when the host's reply lacked it. | 1120 | 1120 | success: Schema.UndefinedOr(PullRequestSummary), | 1121 | 1121 | error: PullRequestRpcError, | 1122 | 1122 | }); | |
|