-
Notifications
You must be signed in to change notification settings - Fork 0
Queue accepted review requests before backend dispatch #386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ea5bcce
Document the approved deferred Codex update design
lynnswap a0977e3
Queue accepted reviews before backend dispatch
lynnswap db182b3
Clear unpersisted review start timestamps
lynnswap 1b03667
Update history E2E acceptance-time checks
lynnswap 5f274fa
Publish diagnostics when queued reviews start
lynnswap File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| import Foundation | ||
|
|
||
| @MainActor | ||
| package final class QueuedReviewStart { | ||
| package let ordinal: UInt64 | ||
| package let request: CodexReviewAPI.Start.Request | ||
| package let model: String? | ||
| package let admission: ReviewStartAdmission | ||
| private enum Dispatch { | ||
| case pending | ||
| case finished(Task<Void, Never>?) | ||
| } | ||
| private var dispatch = Dispatch.pending | ||
| private var dispatchWaiters: [CheckedContinuation<Void, Never>] = [] | ||
|
|
||
| package init(ordinal: UInt64, request: CodexReviewAPI.Start.Request, model: String?, admission: ReviewStartAdmission) { | ||
| self.ordinal = ordinal | ||
| self.request = request | ||
| self.model = model | ||
| self.admission = admission | ||
| } | ||
|
|
||
| package func finishDispatch(worker: Task<Void, Never>? = nil) { | ||
| dispatch = .finished(worker) | ||
| let waiters = dispatchWaiters | ||
| dispatchWaiters.removeAll() | ||
| for waiter in waiters { waiter.resume() } | ||
| } | ||
|
|
||
| package func waitUntilFinished() async { | ||
| if case .pending = dispatch { | ||
| await withCheckedContinuation { dispatchWaiters.append($0) } | ||
| } | ||
| if case .finished(let worker) = dispatch { | ||
| await worker?.value | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| extension CodexReviewStore { | ||
| package func suspendReviewStarts() { | ||
| reviewStartsAreSuspended = true | ||
| } | ||
|
|
||
| package func resumeReviewStarts() { | ||
| reviewStartsAreSuspended = false | ||
| scheduleQueuedReviewStarts() | ||
| } | ||
|
|
||
| private var nextQueuedReviewStart: (id: String, start: QueuedReviewStart)? { | ||
| guard let next = queuedReviewStarts.min(by: { $0.value.ordinal < $1.value.ordinal }), | ||
| historyStartReceipts.values.contains(where: { $0.ordinal < next.value.ordinal }) == false | ||
| else { return nil } | ||
| return (next.key, next.value) | ||
| } | ||
|
|
||
| package func scheduleQueuedReviewStarts() { | ||
| guard reviewStartsAreSuspended == false, | ||
| applicationShutdownRequested == false, | ||
| queuedReviewDispatchTask == nil, | ||
| nextQueuedReviewStart != nil else { return } | ||
| queuedReviewDispatchTask = startRegisteredStoreWork( | ||
| kind: .reviewMutation("dispatch-queued"), | ||
| cancelledBeforeEntry: .runFinalizer { store in | ||
| store.queuedReviewDispatchTask = nil | ||
| } | ||
| ) { store in | ||
| await store.dispatchQueuedReviews() | ||
| } | ||
| } | ||
|
|
||
| private func dispatchQueuedReviews() async { | ||
| defer { | ||
| queuedReviewDispatchTask = nil | ||
| if Task.isCancelled == false { scheduleQueuedReviewStarts() } | ||
| } | ||
| while reviewStartsAreSuspended == false, applicationShutdownRequested == false, | ||
| Task.isCancelled == false, let next = nextQueuedReviewStart { | ||
| queuedReviewStarts.removeValue(forKey: next.id) | ||
| var dispatchedWorker: Task<Void, Never>? | ||
| defer { next.start.finishDispatch(worker: dispatchedWorker) } | ||
| guard let job = job(id: next.id), job.isTerminal == false else { continue } | ||
| let startedAt = clock.now() | ||
| job.core.lifecycle.status = .running | ||
| job.core.lifecycle.startedAt = startedAt | ||
| job.core.output.summary = "Review started." | ||
|
lynnswap marked this conversation as resolved.
|
||
| do { | ||
| do { | ||
| try await persistReviewExecutionStart(id: next.id, at: startedAt) | ||
| } catch { | ||
| // The durable job is still queued when its execution-start write fails. | ||
| job.core.lifecycle.startedAt = nil | ||
| throw error | ||
| } | ||
| try Task.checkCancellation() | ||
| guard job.isTerminal == false else { | ||
| removeStartingReviewOwnership(for: next.id, ifOwnedBy: next.start.admission) | ||
| continue | ||
| } | ||
| guard let worker = makeReviewWorker( | ||
| jobID: job.id, | ||
| sessionID: job.sessionID, | ||
| request: next.start.request, | ||
| effectiveModel: next.start.model, | ||
| admission: next.start.admission | ||
| ) else { throw CancellationError() } | ||
| reviewWorkerTasks[job.id] = worker | ||
| dispatchedWorker = worker | ||
| writeDiagnosticsIfNeeded() | ||
| } catch { | ||
| if job.isTerminal == false { | ||
| if error is CancellationError || Task.isCancelled { | ||
| try? completeCancellationLocally( | ||
| jobID: job.id, | ||
| sessionID: job.sessionID, | ||
| cancellation: job.pendingCancellationRequest?.cancellation ?? .system() | ||
| ) | ||
| } else { | ||
| markReviewFailed(job, message: error.localizedDescription) | ||
| } | ||
| } | ||
| removeStartingReviewOwnership(for: next.id, ifOwnedBy: next.start.admission) | ||
| await waitForHistoryTerminalCommitIfNeeded(jobID: job.id) | ||
| resumeReviewWaiters(for: job.id) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private func persistReviewExecutionStart(id: String, at date: Date) async throws { | ||
| let persistence = historyPersistence | ||
| guard let receipt = historyMutationCoordinator.enqueue( | ||
| intent: id, | ||
| prepare: { $0 }, | ||
| operation: { id in try await persistence.recordExecutionStarted(id: id, at: date) }, | ||
| apply: { [weak self] _, result in | ||
| if case .failure(let error) = result { | ||
| self?.publishReviewHistoryFailure(error) | ||
| } | ||
| } | ||
| ) else { | ||
| throw CodexReviewAPI.Error.io("Review history mutation admission is closed.") | ||
| } | ||
| switch await receipt.wait() { | ||
| case .success: return | ||
| case .failure(let error): throw error | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.