Skip to content

fix(proxy): support remote compaction v2 passthrough - #1404

Merged
ding113 merged 4 commits into
devfrom
fix/remote-compaction-v2
Aug 7, 2026
Merged

fix(proxy): support remote compaction v2 passthrough#1404
ding113 merged 4 commits into
devfrom
fix/remote-compaction-v2

Conversation

@ding113

@ding113 ding113 commented Aug 7, 2026

Copy link
Copy Markdown
Owner

Summary

  • detect exact compaction_trigger items on POST /v1/responses, including the repository-supported single-object input form
  • normalize object-form input before forwarding and keep request.message, raw wire buffer, and audit log synchronized
  • preserve the real /v1/responses path, query, headers, and native SSE while reusing the managed /v1/responses/compact endpoint policy
  • bypass fake streaming for all raw_passthrough endpoints so opaque compaction streams are never parsed as unary JSON
  • recognize completed compaction items with non-empty encrypted_content as valid Responses stream content
  • localize normalized-body serialization errors through the existing next-intl error-code path for all five supported locales

Root Cause

Remote Compaction v2 uses a normal Responses stream with a compaction_trigger input item. The initial implementation classified only array-form input. Because ProxySession chooses its immutable endpoint policy before input rectification, object-form compaction requests remained on the conversation policy. Raw compaction requests could also enter fake streaming, where the fake-streaming path mutated only the parsed message while the raw forwarder sent the original buffer, causing a successful upstream SSE response to be parsed as unary JSON.

Compatibility

  • classification scans only top-level input items whose exact type is compaction_trigger
  • both input: [{...}] and input: {...} are supported
  • replayed compaction, nested markers, strings, and future item names remain normal Responses requests
  • standard array-form bodies retain their original bytes
  • object-form triggers are serialized only after required normalization
  • raw passthrough applies the same non-conversation management, logging, retry, and billing semantics as the existing v1 compact endpoint

The implementation also reviews Wei-Shaw/sub2api#5297. We retain its transparent wire/SSE invariants, but intentionally do not add its DeepSeek lossy-summary bridge, model-name heuristics, Base64 envelope, or cross-provider semantic conversion. Compaction state remains opaque and is forwarded unchanged.

Validation

  • focused proxy suite: 7 files, 119 tests passed
  • focused i18n and localized-error behavior suite: 2 files, 27 tests passed
  • full Vitest suite: 860 files passed, 8,436 tests passed, 13 skipped
  • isolated readiness rerun after a concurrent build/test timeout: 2 tests passed
  • bun run build
  • bun run lint:fix
  • bun run lint
  • bun run typecheck
  • git diff --check
  • independent review: no findings

Acceptance

Official protocol reference: https://developers.openai.com/api/docs/guides/compaction

Closes #1398

Greptile Summary

The PR adds Remote Compaction v2 passthrough while retaining the public Responses wire endpoint and reusing compact-endpoint management semantics.

  • Detects exact top-level compaction_trigger items in array and object input forms.
  • Synchronizes normalized request messages with forwarded bytes and audit logs.
  • Bypasses fake streaming and response rectification for raw compaction passthrough.
  • Recognizes completed opaque compaction output as stream content.
  • Adds localized serialization errors and focused regression coverage.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
src/app/v1/_lib/proxy/session.ts Introduces immutable managed-endpoint classification and synchronized serialization of normalized request state.
src/app/v1/_lib/proxy/remote-compaction.ts Detects exact top-level Remote Compaction v2 triggers on the Responses endpoint.
src/app/v1/_lib/proxy-handler.ts Prevents raw-passthrough policies from entering fake streaming.
src/app/v1/_lib/proxy/response-input-rectifier.ts Synchronizes normalized input with the forwarded wire buffer and audit log.
src/app/v1/_lib/proxy/response-handler.ts Uses the managed endpoint when determining non-billing usage semantics.
src/app/v1/_lib/proxy/stream-gate/frame-classifier.ts Treats completed compaction items containing opaque encrypted state as stream content.

Sequence Diagram

sequenceDiagram
  participant Client
  participant Session as ProxySession
  participant Guards as Guard Pipeline
  participant Forwarder
  participant Upstream
  participant Handler as Response Handler

  Client->>Session: POST /v1/responses with compaction_trigger
  Session->>Session: Preserve wire endpoint
  Session->>Session: Select compact managed policy
  Session->>Session: Normalize object input and synchronize body
  Session->>Guards: Run raw-passthrough preset
  Guards->>Forwarder: Selected provider
  Forwarder->>Upstream: POST original /v1/responses path
  Upstream-->>Forwarder: Native SSE compaction stream
  Forwarder-->>Handler: Raw stream with deferred finalization
  Handler-->>Client: Native SSE bytes
Loading

Reviews (4): Last reviewed commit: "test(proxy): cover localized normalizati..." | Re-trigger Greptile

Context used:

@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

本次变更支持远程压缩 v2。代理识别 compaction_trigger 请求,将管理端点映射到 /v1/responses/compact,同时保留原始请求路径和内容。Responses 流分类器识别有效的压缩输出内容。请求体规范化失败时返回本地化错误。

Changes

远程压缩 v2 请求处理

Layer / File(s) Summary
请求识别与会话端点映射
src/app/v1/_lib/proxy/remote-compaction.ts, src/app/v1/_lib/proxy/session.ts, tests/unit/proxy/remote-compaction-v2.test.ts
新增 isRemoteCompactionV2RequestProxySession 将匹配的请求映射到 /v1/responses/compact,并保留 /v1/responses 原始端点。
管理端点集成、请求体同步与原始透传
src/app/v1/_lib/proxy/message-service.ts, src/app/v1/_lib/proxy/response-handler.ts, src/app/v1/_lib/proxy/response-input-rectifier.ts, src/app/v1/_lib/proxy/session.ts, src/app/v1/_lib/proxy-handler.ts, tests/unit/proxy/*
消息记录和非计费判断使用管理端点。请求体整流后同步更新转发缓冲区和审计日志。raw passthrough 端点跳过 fake streaming。回归测试验证原始路径、查询参数、请求头、请求体和上游 SSE 响应。
压缩流内容分类与提交
src/app/v1/_lib/proxy/stream-gate/frame-classifier.ts, tests/unit/proxy/stream-gate-*
带有非空 encrypted_contentcompaction 完成帧现在被分类为 content。测试覆盖提交行为和边界场景。
规范化请求体错误契约
src/lib/utils/error-messages.ts, messages/*/errors.json, tests/unit/i18n/session-request-errors.test.ts
新增 INVALID_NORMALIZED_BODY 错误码及多语言消息。测试覆盖各语言消息。

Estimated code review effort: 3 (Moderate) | ~30 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed 变更识别有效的 compaction 流内容,并增加相关请求透传和回归测试,满足问题 #1398 的主要目标。
Out of Scope Changes check ✅ Passed 代码和测试变更均服务于 Remote Compaction v2 透传、流识别及请求同步,没有发现明显的无关改动。
Title check ✅ Passed 标题准确概括了 PR 的主要变更,即修复代理对 Remote Compaction v2 passthrough 的支持。
Description check ✅ Passed 描述详细说明了 Remote Compaction v2 的检测、规范化、透传、流处理和测试变更,与代码变更直接相关。
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/remote-compaction-v2

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

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/app/v1/_lib/proxy/remote-compaction.ts`:
- Line 1: Replace the relative imports with the `@/` path alias: update
src/app/v1/_lib/proxy/remote-compaction.ts lines 1-1 to import endpoint-paths
from `@/app/v1/_lib/proxy/endpoint-paths`; update src/app/v1/_lib/proxy/session.ts
lines 38-38 similarly, and lines 51-51 to import remote-compaction from
`@/app/v1/_lib/proxy/remote-compaction`.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 05b4fb56-f2ac-4add-b791-43a60ecfb9e6

📥 Commits

Reviewing files that changed from the base of the PR and between 63ff4cf and 5a4f2df.

📒 Files selected for processing (12)
  • src/app/v1/_lib/proxy/message-service.test.ts
  • src/app/v1/_lib/proxy/message-service.ts
  • src/app/v1/_lib/proxy/remote-compaction.ts
  • src/app/v1/_lib/proxy/response-handler.ts
  • src/app/v1/_lib/proxy/session.ts
  • src/app/v1/_lib/proxy/stream-gate/frame-classifier.ts
  • tests/unit/proxy/proxy-forwarder-raw-passthrough-regression.test.ts
  • tests/unit/proxy/proxy-handler-public-success.test.ts
  • tests/unit/proxy/remote-compaction-v2.test.ts
  • tests/unit/proxy/response-handler-bill-non-success.test.ts
  • tests/unit/proxy/stream-gate-content-gate.test.ts
  • tests/unit/proxy/stream-gate-frame-classifier.test.ts

Comment thread src/app/v1/_lib/proxy/remote-compaction.ts Outdated
@github-actions

github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

🧪 测试结果

测试类型 状态
代码质量
单元测试
集成测试
API 测试

总体结果: ✅ 所有测试通过

@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: 5a4f2dff0d

ℹ️ 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 thread src/app/v1/_lib/proxy/remote-compaction.ts Outdated
Comment thread src/app/v1/_lib/proxy/session.ts
@github-actions github-actions Bot added the size/M Medium PR (< 500 lines) label Aug 7, 2026
Comment thread src/app/v1/_lib/proxy/remote-compaction.ts Outdated

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review Summary

I found one high-confidence defect in the new remote-compaction classification path. A supported /v1/responses input shape is still routed through the normal conversation policy, which means this PR does not fully preserve the /v1/responses/compact raw-passthrough and non-billing semantics it is introducing.

PR Size: M

  • Lines changed: 295
  • Files changed: 12

Issues Found

Category Critical High Medium Low
Logic/Bugs 0 1 0 0
Security 0 0 0 0
Error Handling 0 0 0 0
Types 0 0 0 0
Comments/Docs 0 0 0 0
Tests 0 0 0 0
Simplification 0 0 0 0

Critical Issues (Must Fix)

None.

High Priority Issues (Should Fix)

  • src/app/v1/_lib/proxy/remote-compaction.ts:12isRemoteCompactionV2Request() rejects single-object input payloads even though the proxy already supports that /v1/responses shape via normalizeResponseInput() / rectifyResponseInput(). As a result, { input: { type: "compaction_trigger" } } keeps the normal /v1/responses policy instead of the managed /v1/responses/compact raw-passthrough/non-billing behavior.

Review Coverage

  • Logic and correctness
  • Security (OWASP Top 10)
  • Error handling
  • Type safety
  • Documentation accuracy
  • Test coverage
  • Code clarity

Automated review by Codex AI

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

  • Reviewed PR #1404, applied the size/M label, and submitted the GitHub review summary.
  • Posted 1 inline high-priority comment on src/app/v1/_lib/proxy/remote-compaction.ts:12.
  • The defect: isRemoteCompactionV2Request() only recognizes array-shaped input, but the proxy already supports single-object /v1/responses inputs via the response-input rectifier, so { input: { type: "compaction_trigger" } } stays on the normal /v1/responses policy instead of the managed /v1/responses/compact raw-passthrough/non-billing path.
  • I also called out the needed regression test for that object-form request shape.

If you want, I can do a follow-up review pass after the author pushes a fix.

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/app/v1/_lib/proxy/session.ts`:
- Around line 1203-1211: Update syncRequestBodyFromMessage so the serialization
failure uses the existing server-side next-intl error-message mechanism instead
of a hardcoded English ProxyError message. Add the corresponding translation key
and values for zh-CN, zh-TW, en, ja, and ru, while preserving the 400 status and
existing error behavior.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: a7311991-ccf7-458e-bfc1-acdeb8088451

📥 Commits

Reviewing files that changed from the base of the PR and between 5a4f2df and b26de3a.

📒 Files selected for processing (8)
  • src/app/v1/_lib/proxy-handler.ts
  • src/app/v1/_lib/proxy/remote-compaction.ts
  • src/app/v1/_lib/proxy/response-input-rectifier.ts
  • src/app/v1/_lib/proxy/session.ts
  • tests/unit/proxy/proxy-forwarder-raw-passthrough-regression.test.ts
  • tests/unit/proxy/proxy-handler-public-success.test.ts
  • tests/unit/proxy/remote-compaction-v2.test.ts
  • tests/unit/proxy/response-input-rectifier.test.ts

Comment thread src/app/v1/_lib/proxy/session.ts Outdated
@github-actions

github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

🧪 测试结果

测试类型 状态
代码质量
单元测试
集成测试
API 测试

总体结果: ✅ 所有测试通过

@ding113

ding113 commented Aug 7, 2026

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@ding113

ding113 commented Aug 7, 2026

Copy link
Copy Markdown
Owner Author

@codex review

@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown
⚠️ Action not completed

Already reviewed.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: b26de3a336

ℹ️ 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".

@ding113

ding113 commented Aug 7, 2026

Copy link
Copy Markdown
Owner Author

@coderabbitai review\n\n@codex review

@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown

@ding113 I will review the current changes.

⚠️ Action not completed

Already reviewed.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 7b0181b5df

ℹ️ 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".

@github-actions

github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

🧪 测试结果

测试类型 状态
代码质量
单元测试
集成测试
API 测试

总体结果: ✅ 所有测试通过

@ding113
ding113 merged commit 191bb19 into dev Aug 7, 2026
14 checks passed
@github-project-automation github-project-automation Bot moved this from Backlog to Done in Claude Code Hub Roadmap Aug 7, 2026
@coderabbitai coderabbitai Bot mentioned this pull request Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/M Medium PR (< 500 lines)

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant