Add durable GitHub waitpoints to orchestration V2 #5003
ClosedJules-Astier(GitHub profile, opens in a new tab)wants to merge 226 commits into
t3code/codex-turn-mapping fromrefs/pull/5003/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:XXL
- apps/server/src/github/GitHubWaitpointService.ts line 232OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +212,21 @@212+ lastError: errorMessage(delivered.failure),213+ });214+ return;215+ }216+ const marked = yield* store.markDelivered({217+ id: waitpoint.id,218+ leaseToken,219+ completedAt: nowIso,220+ });221+ if (marked) {222+ yield* Effect.logInfo("github.waitpoint.delivered", {223+ waitpointId: waitpoint.id,224+ threadId: waitpoint.threadId,225+ repository: waitpoint.repository,226+ pullRequestNumber: waitpoint.pullRequestNumber,227+ condition: waitpoint.condition,228+ });229+ }230+ });231+232+ const claimAndDeliver = Effect.fn("GitHubWaitpointService.claimAndDeliver")(function* (
- apps/server/src/github/GitHubWaitpointStore.ts lines 99 to 104OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +80,21 @@80+ readonly continuation_prompt: unknown;81+ readonly delivery_prompt: unknown;82+ readonly state: unknown;83+ readonly next_poll_at: unknown;84+ readonly deadline_at: unknown;85+ readonly delivery_lease_token: unknown;86+ readonly delivery_lease_expires_at: unknown;87+ readonly attempt_count: unknown;88+ readonly last_error: unknown;89+ readonly created_at: unknown;90+ readonly updated_at: unknown;91+ readonly completed_at: unknown;92+}93+94+const decodeBaseline = Schema.decodeUnknownEffect(Schema.fromJsonString(GitHubPullRequestSnapshot));95+const encodeBaseline = Schema.encodeEffect(Schema.fromJsonString(GitHubPullRequestSnapshot));96+const decodeWaitpoint = Schema.decodeUnknownEffect(GitHubWaitpoint);97+98+function storeError(operation: string) {99+ return (cause: unknown) => new GitHubWaitpointStoreError({ operation, cause });100+}apps/server/src/github/GitHubWaitpointService.ts lines 74 to 97OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +77,21 @@77+ ) => Effect.Effect<GitHubWaitpointStore.GitHubWaitpoint, GitHubWaitpointMutationError>;78+ readonly get: (79+ id: GitHubWaitpointId,80+ ) => Effect.Effect<GitHubWaitpointStore.GitHubWaitpoint, GitHubWaitpointMutationError>;81+ readonly listForThread: (82+ threadId: ThreadId,83+ ) => Effect.Effect<84+ ReadonlyArray<GitHubWaitpointStore.GitHubWaitpoint>,85+ GitHubWaitpointStore.GitHubWaitpointStoreError86+ >;87+ readonly cancel: (input: {88+ readonly id: GitHubWaitpointId;89+ readonly threadId: ThreadId;90+ }) => Effect.Effect<GitHubWaitpointStore.GitHubWaitpoint, GitHubWaitpointMutationError>;91+ readonly processDue: Effect.Effect<void, GitHubWaitpointStore.GitHubWaitpointStoreError>;92+}93+94+export class GitHubWaitpointService extends Context.Service<95+ GitHubWaitpointService,96+ GitHubWaitpointServiceShape97+>()("t3/github/GitHubWaitpointService") {}apps/server/src/github/GitHubWaitpointStore.ts lines 151 to 210OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +190,21 @@190+ readonly expireClaim: (input: {191+ readonly id: GitHubWaitpointId;192+ readonly leaseToken: string;193+ readonly completedAt: string;194+ readonly lastError: string;195+ }) => Effect.Effect<boolean, GitHubWaitpointStoreError>;196+ readonly markDelivered: (input: {197+ readonly id: GitHubWaitpointId;198+ readonly leaseToken: string;199+ readonly completedAt: string;200+ }) => Effect.Effect<boolean, GitHubWaitpointStoreError>;201+ readonly cancel: (input: {202+ readonly id: GitHubWaitpointId;203+ readonly threadId: ThreadId;204+ readonly completedAt: string;205+ }) => Effect.Effect<Option.Option<GitHubWaitpoint>, GitHubWaitpointStoreError>;206+}207+208+export class GitHubWaitpointStore extends Context.Service<209+ GitHubWaitpointStore,210+ GitHubWaitpointStoreShape
- apps/server/src/github/GitHubWaitpointService.ts line 372OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +352,21 @@352+ yield* claimAndDeliver(353+ waitpoint,354+ waitpoint.deliveryPrompt ?? waitpoint.continuationPrompt,355+ now,356+ );357+ return;358+ }359+ if (waitpoint.state === "pending") {360+ yield* processPending(waitpoint, now);361+ }362+ });363+364+ const processDue = Effect.gen(function* () {365+ const now = yield* DateTime.now;366+ const due = yield* store.listDue({367+ now: DateTime.formatIso(now),368+ limit: DUE_BATCH_SIZE,369+ });370+ yield* Effect.forEach(371+ due,372+ (waitpoint) =>
- apps/server/src/github/GitHubWaitpointStore.ts lines 292 to 294OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +274,21 @@274+ ${baseline},275+ ${input.continuationPrompt},276+ NULL,277+ 'pending',278+ ${input.nextPollAt},279+ ${input.deadlineAt},280+ NULL,281+ NULL,282+ 0,283+ NULL,284+ ${input.createdAt},285+ ${input.createdAt},286+ NULL287+ )288+ ON CONFLICT (waitpoint_id) DO NOTHING289+ `;290+ const stored = yield* get(input.id);291+ if (Option.isNone(stored)) {292+ return yield* new GitHubWaitpointStoreError({293+ operation: "register:reload",294+ cause: new Error("Inserted waitpoint could not be reloaded."),
- apps/server/src/github/GitHubWaitpointStore.ts line 449OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +429,21 @@429+ markDelivered: Effect.fn("GitHubWaitpointStore.markDelivered")(430+ function* ({ id, leaseToken, completedAt }) {431+ const rows = yield* sql`432+ UPDATE github_waitpoints433+ SET434+ state = 'delivered',435+ delivery_lease_token = NULL,436+ delivery_lease_expires_at = NULL,437+ last_error = NULL,438+ updated_at = ${completedAt},439+ completed_at = ${completedAt}440+ WHERE waitpoint_id = ${id}441+ AND state = 'delivering'442+ AND delivery_lease_token = ${leaseToken}443+ RETURNING waitpoint_id444+ `;445+ return changed(rows);446+ },447+ Effect.mapError(storeError("markDelivered")),448+ ),449+ cancel: Effect.fn("GitHubWaitpointStore.cancel")(
- apps/server/src/github/GitHubWaitpointStore.ts line 215OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +195,21 @@195+ }) => Effect.Effect<boolean, GitHubWaitpointStoreError>;196+ readonly expireClaim: (input: {197+ readonly id: GitHubWaitpointId;198+ readonly leaseToken: string;199+ readonly completedAt: string;200+ readonly lastError: string;201+ }) => Effect.Effect<boolean, GitHubWaitpointStoreError>;202+ readonly markDelivered: (input: {203+ readonly id: GitHubWaitpointId;204+ readonly leaseToken: string;205+ readonly completedAt: string;206+ }) => Effect.Effect<boolean, GitHubWaitpointStoreError>;207+ readonly cancel: (input: {208+ readonly id: GitHubWaitpointId;209+ readonly threadId: ThreadId;210+ readonly completedAt: string;211+ }) => Effect.Effect<Option.Option<GitHubWaitpoint>, GitHubWaitpointStoreError>;212+ }213+>()("t3/github/GitHubWaitpointStore") {}214+215+export const layer = Layer.effect(apps/server/src/github/GitHubPullRequestProbe.ts line 196OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +176,21 @@176+ satisfied: added.length > 0,177+ summary:178+ added.length > 0179+ ? `${added.length} new review or comment event${added.length === 1 ? "" : "s"}.`180+ : "No new review or comment activity.",181+ };182+ }183+ case "pull_request_closed":184+ return {185+ satisfied: current.state !== "open",186+ summary:187+ current.state === "merged"188+ ? "Pull request merged."189+ : current.state === "closed"190+ ? "Pull request closed without merging."191+ : "Pull request remains open.",192+ };193+ }194+}195+196+export const layer = Layer.effect(apps/server/src/github/GitHubWaitpointService.ts line 132OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +112,21 @@112+ "Re-read the pull request and continue the task from the latest GitHub state.",113+ ...(reason ? [`Original reason for waiting: ${reason}`] : []),114+ ].join(" ");115+}116+117+function errorMessage(error: unknown): string {118+ return error instanceof Error ? error.message : String(error);119+}120+121+function continuationIds(waitpointId: GitHubWaitpointId): {122+ readonly commandId: CommandId;123+ readonly messageId: MessageId;124+} {125+ const value = `github-waitpoint:${encodeURIComponent(waitpointId)}`;126+ return {127+ commandId: CommandId.make(value),128+ messageId: MessageId.make(value),129+ };130+}131+132+export const layer = Layer.effect(
- apps/server/src/github/GitHubWaitpointService.ts lines 211 to 230Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +190,21 @@190+ threads.sendToThread({191+ projectId: waitpoint.projectId,192+ commandId: ids.commandId,193+ threadId: waitpoint.threadId,194+ messageId: ids.messageId,195+ text: prompt,196+ attachments: [],197+ mode: "queue",198+ createdBy: "system",199+ creationSource: "server",200+ }),201+ );202+ if (Result.isFailure(delivered)) {203+ yield* store.rescheduleClaim({204+ id: waitpoint.id,205+ leaseToken,206+ nextPollAt: DateTime.formatIso(DateTime.add(now, { seconds: DELIVERY_RETRY_SECONDS })),207+ updatedAt: nowIso,208+ lastError: errorMessage(delivered.failure),209+ });210+ return;
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:
dd78d8da-e8e4-4c09-9d51-bae69d6f2a1cYou 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