-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat(responses): bounded canonical upstream WS reuse #3664
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
4 commits
Select commit
Hold shift + click to select a range
1f9de60
docs(codex): bind WS lifecycle plan to landed protocol guards
invalid-email-address 639ea4b
test(codex): specify same-turn upstream socket reuse
invalid-email-address 76fdacc
feat(responses): retain bounded canonical upstream WS sessions
invalid-email-address 4bf8d40
fix(responses): preserve multiline WS errors in SSE framing
invalid-email-address 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
7 changes: 7 additions & 0 deletions
7
devlog/_plan/260905_http_upstream_ws_parity/012_protocol_outcome.md
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,7 @@ | ||
| # Protocol foundation outcome | ||
|
|
||
| PR3643 merged at `87083e03422b6096d150232cbdf6066038f53383`, with source head `fded48f491809d781068bc410f70a6986f175355`. The actual merge tree matched the checked source tree and fetched `dev` ancestry was verified. The owner explicitly requested immediate admin merge without waiting for CI; pending/cancelled CI was not reported as green. | ||
|
|
||
| Source-bound remote checks passed:145 tests,1 pre-existing skip,0 failures,715 assertions; typecheck and real synthetic HTTP-to-WS QA passed, with process/listener/home cleanup. The earlier hosted CLI-test timeout remains unassigned to a cause; an auxiliary non-reproduction did not close it. No installation, service, home configuration or credential mutation was performed. | ||
|
|
||
| Next is020: bounded connection lifecycle/reuse. It must preserve the implemented metadata/stream contract and the subsequently landed `beforeDispatch` admission checks. No provider billing conclusion follows from either phase. |
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,65 @@ | ||
| export const CODEX_WS_ID_MAX_BYTES = 4096; | ||
| export const CODEX_WS_MAX_TRACKED_ITEMS = 10_000; | ||
| const MAX_TRACKED_ID_BYTES = 1024 * 1024; | ||
|
|
||
| function record(value: unknown): value is Record<string, unknown> { | ||
| return value !== null && typeof value === "object" && !Array.isArray(value); | ||
| } | ||
| function id(value: unknown): value is string { | ||
| return typeof value === "string" && value.length > 0 && !/[\u0000-\u001f\u007f]/.test(value) | ||
| && Buffer.byteLength(value) <= CODEX_WS_ID_MAX_BYTES; | ||
| } | ||
|
|
||
| /** A cold incompatible response may finish one-shot; a reused socket cannot mix owners. */ | ||
| export class CodexWsCorrelation { | ||
| private responseId: string | null = null; | ||
| private reusable = true; | ||
| private readonly items = new Set<string>(); | ||
| private itemBytes = 0; | ||
|
|
||
| constructor(private readonly strict: boolean, private readonly previouslyCompleted: (id: string) => boolean) {} | ||
|
|
||
| private mismatch(): void { | ||
| this.reusable = false; | ||
| if (this.strict) throw new Error("codex websocket response identity mismatch"); | ||
| } | ||
|
|
||
| accept(event: Record<string, unknown>): void { | ||
| if (event.stream_id !== undefined) { this.mismatch(); return; } | ||
| if (event.type === "error") return; | ||
| const response = record(event.response) ? event.response : undefined; | ||
| if (event.type === "response.created") { | ||
| const next = response?.id; | ||
| if (!id(next) || this.responseId !== null || this.previouslyCompleted(next)) { | ||
| this.mismatch(); | ||
| return; | ||
| } | ||
| this.responseId = next; | ||
| return; | ||
| } | ||
| if (!this.responseId) { this.mismatch(); return; } | ||
| if ((response && response.id !== this.responseId) | ||
| || (event.response_id !== undefined && event.response_id !== this.responseId)) this.mismatch(); | ||
| const item = record(event.item) ? event.item : undefined; | ||
| if (event.type === "response.output_item.added") { | ||
| if (!id(item?.id) || this.items.has(item.id)) { this.mismatch(); return; } | ||
| this.itemBytes += Buffer.byteLength(item.id); | ||
| if (this.items.size >= CODEX_WS_MAX_TRACKED_ITEMS || this.itemBytes > MAX_TRACKED_ID_BYTES) { | ||
| throw new Error("codex websocket correlation exceeds its bounded item budget"); | ||
| } | ||
| this.items.add(item.id); | ||
| return; | ||
| } | ||
| const itemId = event.item_id ?? item?.id; | ||
| if (itemId !== undefined && (!id(itemId) || !this.items.has(itemId))) this.mismatch(); | ||
| if (typeof event.type === "string" && event.type.endsWith(".delta") && itemId === undefined) this.mismatch(); | ||
| } | ||
|
|
||
| completed(event: Record<string, unknown>): string | null { | ||
| const response = record(event.response) ? event.response : undefined; | ||
| return this.reusable && event.type === "response.completed" && response?.status === "completed" | ||
| && response.id === this.responseId ? this.responseId : null; | ||
| } | ||
|
|
||
| finish(): void { this.items.clear(); this.itemBytes = 0; } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: lidge-jun/opencodex
Length of output: 21644
Security Misconfiguration (CWE-16)
Reachability: Internal · Exploitability: Difficult
Make independent review an explicit merge gate.
Lines 141–143 permit merging after “the remaining checks” but do not identify independent reviewer approval as a required check. This conflicts with the independent-review requirement in line 133 and the draft restriction in line 140. Keep the follow-up unmerged until the focused checks, exact-head CI, and independent reviewer approval are complete.
Proposed documentation fix
📝 Committable suggestion
🤖 Prompt for AI Agents