Skip to content

fix(proxy): preserve bridge restart continuity anchors - #993

Closed
Komzpa wants to merge 9 commits into
Soju06:mainfrom
Komzpa:fix/bridge-restart-continuity
Closed

Komzpa wants to merge 9 commits into
Soju06:mainfrom
Komzpa:fix/bridge-restart-continuity

Conversation

@Komzpa

@Komzpa Komzpa commented Jun 13, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • preserve durable HTTP bridge previous-response anchors when the replacement instance takes over a closed same-account session without a new response id yet
  • avoid treating an owner endpoint that resolves to the current instance as forwardable, so local restart recovery and anchor injection can run
  • document the local restart-orphan invariant in OpenSpec and cover it with regression tests

Live deploy

  • deployed locally as codex-lb:live-stack-3d63876 from aggregate commit 3d638765f
  • previous rollback container: codex-lb-before-live-stack-3d63876-20260613T114136Z
  • /health and /health/ready are OK; bridge ring settled to one live member

Tests

  • uv run pytest tests/unit/test_durable_bridge_sessions.py tests/unit/test_proxy_http_bridge.py -q
  • uv run ruff check app/modules/proxy/durable_bridge_repository.py app/modules/proxy/_service/http_bridge/owner_forwarding.py tests/unit/test_durable_bridge_sessions.py tests/unit/test_proxy_http_bridge.py
  • openspec validate --specs
  • git diff --check origin/main..HEAD

@Soju06

Soju06 commented Jun 13, 2026

Copy link
Copy Markdown
Owner

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9de07fd7af

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

- **THEN** the service MUST fail the request with a generic 5xx bridge-forward error
- **AND** it MUST NOT attempt another owner handoff

#### Scenario: local restart orphan is recovered by the replacement instance

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Add the required OpenSpec change folder

This new scenario changes the HTTP bridge restart-orphan behavior/proxy-routing contract, but the commit only edits the main spec and code/tests; AGENTS.md requires behavior/contract changes to create an OpenSpec change in openspec/changes/** first and states that for proxy-routing/compatibility changes, “Code/tests alone are not enough when OpenSpec is required.” Without the proposal/tasks change folder, the repository’s documented PR readiness gate cannot be satisfied for this behavior change.

Useful? React with 👍 / 👎.

@Soju06 Soju06 added the 🤖 codex: needs work [@codex review] raised an issue label Jun 13, 2026
@Soju06

Soju06 commented Jun 13, 2026

Copy link
Copy Markdown
Owner

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 404ae89b6e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

request_service_tier=request_service_tier,
)
raise
if code == "upstream_request_timeout":

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Clear affinity when local compact attempts time out

When the per-attempt timeout added here fires, the compact transport wraps both body-read and connect aiohttp/asyncio timeouts as openai_error("upstream_unavailable", ...) in app/core/clients/proxy.py (around lines 3034 and 3115), so this upstream_request_timeout-only branch is skipped. In that scenario the request may still fail over via the generic path, but the stale compact affinity mapping is not cleared, leaving subsequent prompt/session-affinity compactions pinned back to the account that just timed out.

Useful? React with 👍 / 👎.

response = await _call_compact(
account,
account_response_create_lease,
account_attempts_remaining=_compact_max_account_attempts() - _account_attempt,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid splitting compact budget without another candidate

When only one account is eligible, such as a single-account deployment or an API-key/file-pinned compact request, this still passes 2 on the first attempt because it uses the configured max attempts rather than actual remaining candidates. _compact_attempt_timeout_seconds then halves the remaining request budget and installs that as the total upstream timeout, so a valid compact call that would complete within compact_request_budget_seconds can be cut off early even though there is no account to fail over to.

Useful? React with 👍 / 👎.

@Soju06

Soju06 commented Jun 13, 2026

Copy link
Copy Markdown
Owner

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ae4e58e168

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +476 to +480
raise ProxyResponseError(
503,
openai_error(
"compact_upstream_unavailable",
"Compact upstream is cooling down after repeated timeouts; retry shortly.",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Move compact cooldown into accounting path

When an internal bridge/forwarded request hits this cooldown while carrying an api_key_reservation_override, this early raise happens before the compact method's normal try/finally accounting path, while _stream_responses only releases reservations it owns. That leaves the forwarded reservation unsettled/released only by neither side and also skips compact request logging for these 503s; settle/log through the existing compact error path before returning the cooldown error.

Useful? React with 👍 / 👎.

request_service_tier=request_service_tier,
)
raise
if code == "upstream_request_timeout":

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Handle timeout codes before same-account retries

When the compact upstream returns a retryable HTTP status such as 502/503/504 with an OpenAI error code of upstream_request_timeout, the core compact client marks the ProxyResponseError as retryable_same_contract, so the guard just above this parsing block retries the same account first. That bypasses the new timeout failover behavior for the first timeout, delaying the required stale-affinity clear/exclusion and letting a dead account consume the retry slice intended for another eligible account; parse and handle upstream_request_timeout before the same-contract retry guard.

Useful? React with 👍 / 👎.

@Komzpa
Komzpa force-pushed the fix/bridge-restart-continuity branch from 077a10e to 96487ae Compare June 13, 2026 13:10
@Soju06

Soju06 commented Jun 13, 2026

Copy link
Copy Markdown
Owner

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 96487aee51

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

return CompactResponsePayload.model_validate(
{
"object": "response.compaction",
"output": build_local_compact_fallback_output(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Return a compaction item from the local fallback

When this fallback is used via the terminal compaction_trigger path in _stream_responses, api._compact_response_output_item() only accepts an output item of type compaction/compaction_summary with encrypted_content; these message items make it return None, so _stream_responses turns the timeout fallback into a 502 “Compact response did not include a compaction output item” instead of letting Codex continue. Please wrap or emit a valid compaction output for this path.

Useful? React with 👍 / 👎.

assert marker_content[0]["text"].startswith("[local compact fallback]")
assert "Remote compaction was unavailable" in marker_content[0]["text"]
assert "test outage" in marker_content[0]["text"]
assert "codex-lb" not in marker_content[0]["text"]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Fix the contradictory compact fallback assertion

This new test asserts that the fallback marker does not contain codex-lb, but _local_compact_fallback_marker() constructs the marker text with codex-lb in it, so tests/unit/test_openai_requests.py fails as soon as this test is run. Update the expected text or remove the assertion so CI can pass.

Useful? React with 👍 / 👎.

@Soju06

Soju06 commented Jun 13, 2026

Copy link
Copy Markdown
Owner

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c1c49cf92f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +1012 to +1017
if _local_compact_fallback_enabled(settings):
response = _local_compact_fallback_response(
payload,
reason="all compact upstream account attempts timed out",
max_estimated_tokens=_local_compact_fallback_token_limit(settings),
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Return a compaction item for local fallback

When a /backend-api/codex/responses request ends with compaction_trigger and all compact attempts time out, this path returns a "successful" fallback whose output is made of message items from build_local_compact_fallback_output. The streaming compaction-trigger path immediately calls _compact_response_output_item, which only accepts compaction/compaction_summary items with encrypted_content, so the fallback is converted into a 502 "Compact response did not include a compaction output item" instead of preserving continuation during the exact timeout scenario this branch is meant to handle.

Useful? React with 👍 / 👎.

@Soju06

Soju06 commented Jun 13, 2026

Copy link
Copy Markdown
Owner

@codex review

@Soju06

Soju06 commented Jun 13, 2026

Copy link
Copy Markdown
Owner

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 97c4ec9045

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +231 to +235
"output": build_local_compact_fallback_output(
payload.input,
reason=reason,
max_estimated_tokens=max_estimated_tokens,
),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Emit a compaction item for local fallback

When the normal /responses flow hits a terminal compaction_trigger, api.py calls _compact_response_output_item() and only accepts compaction/compaction_summary items that carry encrypted_content before it can synthesize the continuation stream. This fallback response instead fills output with plain message/trim-marker items, so any upstream timeout or cooldown that selects local fallback in that flow will be converted into Compact response did not include a compaction output item (502) rather than preserving the session. The fallback needs to produce or be adapted into the encrypted compaction item shape consumed by that path.

Useful? React with 👍 / 👎.

@Komzpa

Komzpa commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator Author

#993 has been partially folded into #875.

Carried scope: the useful bridge-restart continuity anchor behavior is now part of the #875 account-bound proxy routing and continuity branch.

Deliberately not carried: the older local compact fallback tail is superseded by #991's newer compact-timeout/budget path, which is also folded into #875.

Survivor: #875
Tracker: #554

@Komzpa Komzpa closed this Jun 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🤖 codex: needs work [@codex review] raised an issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants