-
-
Notifications
You must be signed in to change notification settings - Fork 380
feat(proxy): add bounded streaming discovery #1348
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
ding113
merged 31 commits into
ding113:integration/discovery-stack-20260723
from
Brisbanehuang:codex/discovery-pr2
Jul 23, 2026
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
a5aeccf
feat(proxy): add bounded streaming discovery
Brisbanehuang 31c4324
fix(proxy): keep discovery disabled path compatible
Brisbanehuang d4cba72
fix(proxy): close discovery coordinator lifecycle gaps
Brisbanehuang 9c7793c
fix(proxy): guard optional discovery request message
Brisbanehuang ee5557e
fix(proxy): handle replacement launch failures
Brisbanehuang 43c6733
fix(proxy): preserve complete discovery candidates
Brisbanehuang 7e6dadf
fix(proxy): pause held discovery readers
Brisbanehuang 8e0cedb
fix(proxy): close discovery attempt cleanup gaps
Brisbanehuang 0f7e170
fix(proxy): execute discovery normal winner actions
Brisbanehuang 0690278
fix(proxy): keep discovery timers and candidates consistent
Brisbanehuang 7d49cde
fix(discovery): clear binding against snapshot provider
Brisbanehuang a643ff8
fix(discovery): honor effective group priority
Brisbanehuang db5d68e
merge: synchronize versioned binding fixes
Brisbanehuang 2be4a4e
merge: synchronize session binding safety fixes
Brisbanehuang ca12624
fix(discovery): harden bounded streaming lifecycle
Brisbanehuang 0537a6e
fix(discovery): retain blocked fallback readiness
Brisbanehuang 42ffcf4
merge: synchronize latest binding safety fixes
Brisbanehuang 12887e4
fix(discovery): reserve sticky timeout wave
Brisbanehuang 08337d8
fix(discovery): stop parsing after validity errors
Brisbanehuang 41ecb53
merge: synchronize binding touch and scoped termination fixes
Brisbanehuang ef6b833
fix(discovery): close final lifecycle gaps
Brisbanehuang d915e08
fix(discovery): refill setup failures within current round
Brisbanehuang 7e24438
merge: synchronize final binding lifecycle fixes
Brisbanehuang 74bb97e
fix(discovery): fence readiness and sticky cooldown
Brisbanehuang 3f9cca1
merge: synchronize tenant-scoped content hash fixes
Brisbanehuang c1b291c
Merge branch 'codex/versioned-session-binding' into codex/discovery-pr2
Brisbanehuang 1d23d6c
fix(discovery): release non-SSE winner resources
Brisbanehuang 4c1f166
fix(discovery): refresh cleared binding authority
Brisbanehuang 1c82d52
fix(discovery): parse SSE events by complete data frames
Brisbanehuang 3dd24e7
fix(discovery): validate fallback stream completion
Brisbanehuang a885e0c
Merge remote-tracking branch 'origin/integration/discovery-stack-2026…
ding113 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
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,347 @@ | ||
| /** | ||
| * Pure state machine for bounded provider discovery. | ||
| * | ||
| * The coordinator deliberately has no network or timer dependencies. The | ||
| * forwarder owns attempts and calls these methods at event boundaries. This | ||
| * keeps cancellation and stale-event handling deterministic and testable. | ||
| */ | ||
|
|
||
| export type DiscoveryAttemptKind = "normal" | "fallback"; | ||
| export type DiscoveryState = | ||
| | "STICKY_PROBING" | ||
| | "DISCOVERY_RACING" | ||
| | "FALLBACK_READY_HELD" | ||
| | "FALLBACK_ACTIVE" | ||
| | "WINNER_COMMITTED" | ||
| | "TERMINAL_FAILED"; | ||
|
|
||
| export type DiscoveryAttempt = { | ||
| id: string; | ||
| providerId: number; | ||
| priority: number; | ||
| kind: DiscoveryAttemptKind; | ||
| ready: boolean; | ||
| pending: boolean; | ||
| round: number; | ||
| launchOrder: number; | ||
| }; | ||
|
|
||
| export type DiscoveryAction = | ||
| | { type: "commit_normal"; attemptId: string } | ||
| | { type: "promote_fallback"; attemptId: string } | ||
| | { type: "cancel"; attemptIds: string[]; promoteAttemptId?: string } | ||
| | { | ||
| type: "launch"; | ||
| slots: number; | ||
| cancelAttemptIds?: string[]; | ||
| promoteAttemptId?: string; | ||
| } | ||
| | { type: "none" } | ||
| | { type: "terminal_failure" }; | ||
|
|
||
| export type DiscoveryCoordinatorOptions = { | ||
| concurrency: number; | ||
| maxRounds: number; | ||
| }; | ||
|
|
||
| function compareAttempts(a: DiscoveryAttempt, b: DiscoveryAttempt): number { | ||
| return a.priority - b.priority || a.launchOrder - b.launchOrder; | ||
| } | ||
|
|
||
| export class DiscoveryCoordinator { | ||
| readonly concurrency: number; | ||
| readonly maxRounds: number; | ||
| state: DiscoveryState = "DISCOVERY_RACING"; | ||
| round = 1; | ||
| private attempts = new Map<string, DiscoveryAttempt>(); | ||
| private requestEpoch = 0; | ||
| private roundEpoch = 0; | ||
|
|
||
| constructor(options: DiscoveryCoordinatorOptions) { | ||
| this.concurrency = Math.max(1, Math.floor(options.concurrency)); | ||
| this.maxRounds = Math.max(1, Math.floor(options.maxRounds)); | ||
| } | ||
|
|
||
| get epochs(): { requestEpoch: number; roundEpoch: number } { | ||
| return { requestEpoch: this.requestEpoch, roundEpoch: this.roundEpoch }; | ||
| } | ||
|
|
||
| startStickyProbe(): void { | ||
| if (!this.isTerminal) this.state = "STICKY_PROBING"; | ||
| } | ||
|
|
||
| startDiscoveryAfterSticky(): void { | ||
| if (this.state === "STICKY_PROBING" || this.state === "FALLBACK_READY_HELD") { | ||
| this.state = "DISCOVERY_RACING"; | ||
| } | ||
| } | ||
|
|
||
| beginRound(): { requestEpoch: number; roundEpoch: number; round: number } { | ||
| if (this.isTerminal || this.round >= this.maxRounds) { | ||
| return { ...this.epochs, round: this.round }; | ||
| } | ||
| this.round += 1; | ||
| this.roundEpoch += 1; | ||
| if (!this.isTerminal) this.state = "DISCOVERY_RACING"; | ||
| return { ...this.epochs, round: this.round }; | ||
| } | ||
|
|
||
| addAttempt(attempt: DiscoveryAttempt): boolean { | ||
| if (this.isTerminal || this.attempts.has(attempt.id)) return false; | ||
| this.attempts.set(attempt.id, { ...attempt, round: this.round }); | ||
| return true; | ||
| } | ||
|
|
||
| removeAttempt(id: string): void { | ||
| this.attempts.delete(id); | ||
| } | ||
|
|
||
| /** Mark an already-running attempt as the sole fallback for this request. */ | ||
| promoteToFallback(id: string): boolean { | ||
| if (this.isTerminal) return false; | ||
| const attempt = this.attempts.get(id); | ||
| if (!attempt?.pending) return false; | ||
| attempt.kind = "fallback"; | ||
| this.state = "FALLBACK_READY_HELD"; | ||
| return true; | ||
| } | ||
|
|
||
| get isTerminal(): boolean { | ||
| return ( | ||
| this.state === "WINNER_COMMITTED" || | ||
| this.state === "FALLBACK_ACTIVE" || | ||
| this.state === "TERMINAL_FAILED" | ||
| ); | ||
| } | ||
|
|
||
| get activeAttempts(): DiscoveryAttempt[] { | ||
| return Array.from(this.attempts.values()).filter((attempt) => attempt.pending); | ||
| } | ||
|
|
||
| get snapshot(): DiscoveryAttempt[] { | ||
| return Array.from(this.attempts.values()).map((attempt) => ({ ...attempt })); | ||
| } | ||
|
|
||
| /** Ignore events from a cancelled request or an old round. */ | ||
| acceptsEpoch(requestEpoch: number, roundEpoch: number): boolean { | ||
| return requestEpoch === this.requestEpoch && roundEpoch === this.roundEpoch; | ||
| } | ||
|
|
||
| markReady( | ||
| id: string, | ||
| requestEpoch = this.requestEpoch, | ||
| roundEpoch = this.roundEpoch | ||
| ): DiscoveryAction { | ||
| if (!this.acceptsEpoch(requestEpoch, roundEpoch) || this.isTerminal) return { type: "none" }; | ||
| const attempt = this.attempts.get(id); | ||
| if (!attempt?.pending) return { type: "none" }; | ||
| attempt.ready = true; | ||
| if (attempt.kind === "fallback") { | ||
| const pendingNormal = Array.from(this.attempts.values()).some( | ||
| (candidate) => candidate.pending && candidate.kind === "normal" | ||
| ); | ||
| if (!pendingNormal) { | ||
| attempt.pending = false; | ||
| this.state = "FALLBACK_ACTIVE"; | ||
| return { type: "promote_fallback", attemptId: attempt.id }; | ||
| } | ||
| return { type: "none" }; | ||
| } | ||
| return this.chooseReadyNormal(); | ||
| } | ||
|
|
||
| /** Record a ready fallback without allowing it to preempt a reserved normal wave. */ | ||
| recordReadyHeld( | ||
| id: string, | ||
| requestEpoch = this.requestEpoch, | ||
| roundEpoch = this.roundEpoch | ||
| ): boolean { | ||
| if (!this.acceptsEpoch(requestEpoch, roundEpoch) || this.isTerminal) return false; | ||
| const attempt = this.attempts.get(id); | ||
| if (!attempt?.pending || attempt.kind !== "fallback") return false; | ||
| attempt.ready = true; | ||
| this.state = "FALLBACK_READY_HELD"; | ||
| return true; | ||
| } | ||
|
|
||
| /** Convert a timed-out Sticky attempt into the request's fallback lane. */ | ||
| demoteToFallback( | ||
| id: string, | ||
| requestEpoch = this.requestEpoch, | ||
| roundEpoch = this.roundEpoch | ||
| ): boolean { | ||
| if (!this.acceptsEpoch(requestEpoch, roundEpoch) || this.isTerminal) return false; | ||
| const attempt = this.attempts.get(id); | ||
| if (!attempt?.pending) return false; | ||
| attempt.kind = "fallback"; | ||
| this.state = "FALLBACK_READY_HELD"; | ||
| return true; | ||
| } | ||
|
|
||
| markFailed( | ||
| id: string, | ||
| requestEpoch = this.requestEpoch, | ||
| roundEpoch = this.roundEpoch | ||
| ): DiscoveryAction { | ||
| if (!this.acceptsEpoch(requestEpoch, roundEpoch) || this.isTerminal) return { type: "none" }; | ||
| const attempt = this.attempts.get(id); | ||
| if (!attempt?.pending) return { type: "none" }; | ||
| attempt.pending = false; | ||
| attempt.ready = false; | ||
| const readyAction = this.chooseReadyNormal(); | ||
| if (readyAction.type !== "none") return readyAction; | ||
| return this.afterAttemptState(); | ||
| } | ||
|
|
||
| /** A normal ready result may win only after priority gating is satisfied. */ | ||
| private chooseReadyNormal(ignorePriorityGate = false): DiscoveryAction { | ||
| const readyNormal = Array.from(this.attempts.values()) | ||
| .filter((attempt) => attempt.pending && attempt.ready && attempt.kind === "normal") | ||
| .sort(compareAttempts); | ||
| if (readyNormal.length === 0) return { type: "none" }; | ||
| const bestPriority = readyNormal[0].priority; | ||
| if (!ignorePriorityGate) { | ||
| const higherTierPending = Array.from(this.attempts.values()).some( | ||
| (attempt) => | ||
| attempt.pending && | ||
| attempt.kind === "normal" && | ||
| !attempt.ready && | ||
| attempt.priority < bestPriority | ||
| ); | ||
| if (higherTierPending) return { type: "none" }; | ||
| } | ||
| const winner = readyNormal[0]; | ||
| this.state = "WINNER_COMMITTED"; | ||
| winner.pending = false; | ||
| return { | ||
| type: "commit_normal", | ||
| attemptId: winner.id, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Close the current SLA window. At a boundary a ready normal always wins; | ||
| * otherwise the best still-pending normal becomes the sole fallback. A | ||
| * fallback that is merely ready is held until no normal can still win. | ||
| */ | ||
| onRoundBoundary(requestEpoch = this.requestEpoch, roundEpoch = this.roundEpoch): DiscoveryAction { | ||
| if (!this.acceptsEpoch(requestEpoch, roundEpoch) || this.isTerminal) return { type: "none" }; | ||
| const readyAction = this.chooseReadyNormal(true); | ||
| if (readyAction.type === "commit_normal") return readyAction; | ||
|
|
||
| const currentFallback = Array.from(this.attempts.values()).find( | ||
| (attempt) => attempt.pending && attempt.kind === "fallback" | ||
| ); | ||
| if (currentFallback?.ready) { | ||
| currentFallback.pending = false; | ||
| this.state = "FALLBACK_ACTIVE"; | ||
| return { type: "promote_fallback", attemptId: currentFallback.id }; | ||
| } | ||
|
|
||
| const pendingNormal = Array.from(this.attempts.values()) | ||
| .filter((attempt) => attempt.pending && attempt.kind === "normal") | ||
| .sort(compareAttempts); | ||
| if (currentFallback && pendingNormal.length > 0) { | ||
| const cancelAttemptIds = pendingNormal.map((attempt) => attempt.id); | ||
| for (const attempt of pendingNormal) attempt.pending = false; | ||
| if (this.round < this.maxRounds) { | ||
| this.beginRound(); | ||
| this.state = "DISCOVERY_RACING"; | ||
| return { | ||
| type: "launch", | ||
| slots: Math.max(0, this.concurrency - 1), | ||
| cancelAttemptIds, | ||
| }; | ||
| } | ||
| return { type: "cancel", attemptIds: cancelAttemptIds }; | ||
| } | ||
| if (pendingNormal.length === 0) { | ||
| if (currentFallback) { | ||
| this.state = "FALLBACK_READY_HELD"; | ||
| return { type: "none" }; | ||
| } | ||
| return this.finishOrLaunch(); | ||
| } | ||
|
|
||
| const fallback = pendingNormal[0]; | ||
| fallback.kind = "fallback"; | ||
| this.state = "FALLBACK_READY_HELD"; | ||
| const losers = pendingNormal.slice(1).map((attempt) => attempt.id); | ||
| for (const id of losers) this.attempts.get(id)!.pending = false; | ||
|
|
||
| if (this.round < this.maxRounds) { | ||
| this.beginRound(); | ||
| this.state = "DISCOVERY_RACING"; | ||
| return { | ||
| type: "launch", | ||
| slots: Math.max(0, this.concurrency - 1), | ||
| cancelAttemptIds: losers, | ||
| promoteAttemptId: fallback.id, | ||
| }; | ||
| } | ||
| return { type: "cancel", attemptIds: losers, promoteAttemptId: fallback.id }; | ||
| } | ||
|
|
||
| onDeadline(): DiscoveryAction { | ||
| if (this.isTerminal) return { type: "none" }; | ||
| const readyNormal = this.chooseReadyNormal(true); | ||
| if (readyNormal.type === "commit_normal") return readyNormal; | ||
| const fallback = Array.from(this.attempts.values()).find( | ||
| (attempt) => attempt.pending && attempt.kind === "fallback" && attempt.ready | ||
| ); | ||
| if (fallback) { | ||
| fallback.pending = false; | ||
| this.state = "FALLBACK_ACTIVE"; | ||
| return { type: "promote_fallback", attemptId: fallback.id }; | ||
| } | ||
| this.state = "TERMINAL_FAILED"; | ||
| return { type: "terminal_failure" }; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| commitWinner(id: string): DiscoveryAction { | ||
| const attempt = this.attempts.get(id); | ||
| if (!attempt || this.isTerminal) return { type: "none" }; | ||
| attempt.pending = false; | ||
| this.state = attempt.kind === "fallback" ? "FALLBACK_ACTIVE" : "WINNER_COMMITTED"; | ||
| return { | ||
| type: attempt.kind === "fallback" ? "promote_fallback" : "commit_normal", | ||
| attemptId: id, | ||
| }; | ||
| } | ||
|
|
||
| cancelRequest(): DiscoveryAction { | ||
| this.requestEpoch += 1; | ||
| this.roundEpoch += 1; | ||
| const ids = this.activeAttempts.map((attempt) => attempt.id); | ||
| for (const attempt of this.attempts.values()) attempt.pending = false; | ||
| this.state = "TERMINAL_FAILED"; | ||
| return { type: "cancel", attemptIds: ids }; | ||
| } | ||
|
|
||
| private afterAttemptState(): DiscoveryAction { | ||
| const pending = this.activeAttempts; | ||
| if (pending.length === 0) return this.finishOrLaunch(); | ||
|
|
||
| // A higher-priority attempt may have been the only gate preventing a | ||
| // ready lower-priority candidate from winning. Once that attempt fails, | ||
| // re-run the normal winner selection before waiting for another boundary. | ||
| const readyNormal = this.chooseReadyNormal(); | ||
| if (readyNormal.type === "commit_normal") return readyNormal; | ||
|
|
||
| const fallback = pending.find((attempt) => attempt.kind === "fallback"); | ||
| if (fallback?.ready && pending.every((attempt) => attempt.kind === "fallback")) { | ||
| return this.commitWinner(fallback.id); | ||
| } | ||
| return { type: "none" }; | ||
| } | ||
|
|
||
| private finishOrLaunch(): DiscoveryAction { | ||
| if (this.round >= this.maxRounds) { | ||
| this.state = "TERMINAL_FAILED"; | ||
| return { type: "terminal_failure" }; | ||
| } | ||
| this.beginRound(); | ||
| this.state = "DISCOVERY_RACING"; | ||
| return { type: "launch", slots: this.concurrency }; | ||
| } | ||
| } | ||
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.