Skip to content

Add configurable Responses context projection - #42

Open
maiphucgiang wants to merge 2 commits into
mainfrom
feat/responses-projection-modes
Open

maiphucgiang wants to merge 2 commits into
mainfrom
feat/responses-projection-modes

Conversation

@maiphucgiang

@maiphucgiang maiphucgiang commented Sep 25, 2026 •

Copy link
Copy Markdown
Owner

Changes

  • Replace the previous fixed Responses projection with responses_projection_mode=balanced|passthrough (default balanced), exposed through --responses-projection-mode, CODEBUDDY2API_RESPONSES_PROJECTION_MODE, .env, the WebUI and SQLite settings. Mode and limits are frozen per request, responses carry X-CodeBuddy-Responses-Projection, and audits record only non-sensitive counters. Client paths, models and payloads are unchanged.
  • Keep tool schemas, tool-call identity, argument shape, conversation history and images intact; only recognized harness blocks with fixed summaries are rewritten. Oversized assistant text, tool results and JSON tool arguments are clipped with Codex-style head/tail retention plus original byte, estimated token and line counts. apply_patch arguments are no longer omitted wholesale, and arguments already within responses_projection_max_bytes are never truncated.
  • Add responses_projection_max_bytes (default 40000; 0 disables clipping, otherwise 256..33554432) with the existing precedence, source locking and hot updates. passthrough disables Responses-specific projection while keeping protocol conversion, image policy, the 64 MiB inbound / 32 MiB processed-request budgets and output collection limits.
  • Remove the old 1600/900/1800-character trimming, schema-depth pruning and history summaries; no legacy compatibility mode remains. Reject non-string function_call.arguments and text that cannot be encoded as UTF-8 with 400, keep projected arguments valid strict JSON, and run projection off the event loop.
  • Synchronize CLI, WebUI settings, Compose passthrough, .env.example, concise English/Chinese advanced, client and deployment documentation, and the README FAQ. No dependency, database-schema or version changes (1.2.10).

Verification

  • All 63 backend regression scripts pass with the CI loop (for script in tests/test_*.py; do python -B "$script"; done).
  • WebUI: vp check reports 52 formatted files and no lint/type errors in 47 files, 133 unit tests pass, vp build succeeds, and Playwright passes 14 isolated plus 2 real-backend integration tests.
  • Focused projection regressions cover both modes, UTF-8 head/tail retention and metadata, statistics, invalid mode/limit, arguments larger than 1 MiB that already fit the configured limit, strict JSON constants, non-string arguments and invalid Unicode.
  • A local Docker image builds; compileall, check_version.py, docker compose config and git diff --check pass. Independent code reviews closed every reported P1/P2 finding.
  • Resource spot-checks: a 6 MB numeric tool argument peaks at about 36 MB RSS, and a 10 MB single-string argument is returned unchanged in about 0.12 s at about 37 MB RSS. Synthetic fixtures only; no live-account or upstream-latency claim.
  • Version remains 1.2.10. Reverting the two commits restores the previous behaviour; no configuration or data migration is required.

Summary by Sourcery

Add configurable, loss-conscious Responses context projection with balanced truncation or verbatim passthrough while preserving protocol compatibility.

New Features:

  • Add configurable Responses projection modes and per-item byte limits across CLI, environment, WebUI, SQLite settings, Compose, and response headers.

Bug Fixes:

  • Preserve tool schemas, conversation history, images, tool-call identity, and real user text while preventing invalid or non-string function-call arguments from reaching conversion.
  • Reject invalid UTF-8 text and ensure projected tool arguments remain strict valid JSON.

Enhancements:

  • Replace fixed context compaction with balanced harness-only rewriting and Codex-style UTF-8 head/tail truncation for generated text, tool results, and oversized JSON arguments.
  • Run projection off the event loop and record only non-sensitive projection counters in audits.

Deployment:

  • Expose Responses projection settings through Compose and deployment configuration without requiring migration or changing client endpoints.

Documentation:

  • Document Responses projection modes, limits, configuration precedence, and client behavior in English and Chinese README and advanced, client, and deployment guides.

Tests:

  • Update and expand backend, endpoint, configuration, observability, truncation, and tool-schema regression coverage for both projection modes and edge cases.

Chores:

  • Remove legacy Responses history summarization, schema pruning, fixed character limits, and compatibility mode.

@sourcery-ai

sourcery-ai Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

This PR replaces fixed Responses context compaction with request-scoped balanced or passthrough projection, preserving protocol payload structure while selectively rewriting recognized harness blocks and safely clipping oversized generated content and JSON arguments. It adds configurable limits across CLI, environment, SQLite, WebUI, and Compose, exposes the selected mode in responses, records non-sensitive counters, validates malformed inputs, offloads projection work from the event loop, and updates regression coverage and documentation.

Sequence diagram for configurable Responses projection

sequenceDiagram
    participant Client
    participant API as Responses API
    participant Config as Request config snapshot
    participant Projection as Responses projection
    participant Upstream
    participant Audit

    Client->>API: POST /v1/responses
    API->>Config: Freeze mode and max bytes
    API->>Projection: project_responses_chat_body(body, mode, max_item_bytes)
    alt mode is balanced
        Projection->>Projection: parse_harness_text(text)
        Projection->>Projection: truncate_middle_bytes(text, max_item_bytes)
        Projection->>Projection: JSON-validate and clip tool arguments
    else mode is passthrough
        Projection-->>API: Preserve messages and tools
    end
    Projection-->>API: Projected body and counters
    API->>Audit: observe_responses_projection(stats)
    API->>Upstream: Forward converted Chat request
    Upstream-->>API: Response stream or JSON
    API-->>Client: Response with X-CodeBuddy-Responses-Projection
Loading

Flow diagram for Responses projection modes and limits

flowchart TD
    A[Responses request] --> B[Freeze mode and max bytes per request]
    B --> C{Projection mode}
    C -->|passthrough| D[Keep Responses-derived payload unchanged]
    C -->|balanced| E[Preserve schemas identity history and images]
    E --> F[Rewrite recognized harness blocks]
    E --> G[Clip oversized assistant text and tool results]
    E --> H[Clip oversized JSON tool arguments]
    G --> I[Keep UTF-8 head and tail with byte token and line metadata]
    H --> J[Return strict valid JSON with head tail metadata]
    D --> K[Protocol conversion and global limits]
    I --> K
    J --> K
    K --> L[Forward upstream and expose selected mode]
Loading

File-Level Changes

Change Details Files
Replaced the legacy Responses compaction algorithm with configurable balanced and passthrough projection modes.
  • Added balanced-mode rewriting for recognized harness blocks while preserving user text, history, images, tool-call identity, arguments, and complete tool schemas.
  • Added passthrough mode and per-item byte-limit validation, including zero-limit behavior and request-scoped projection settings.
  • Removed legacy history summarization, schema pruning, fixed character limits, and compatibility modes.
app/adapters/responses_projection.py
tests/test_harness_projection.py
tests/test_request_limits.py
tests/test_responses_adapter.py
tests/test_tool_metadata.py
tests/test_workbuddy_filter.py
Implemented byte-safe truncation for generated text, tool results, and JSON tool arguments.
  • Added UTF-8-aware head/tail retention with byte, estimated-token, and line metadata.
  • Preserved strict JSON for projected tool arguments, including apply_patch payloads and nonstandard JSON-constant handling.
  • Added large-argument fast paths to avoid full JSON materialization when arguments already fit or require bounded textual projection.
  • Moved projection execution to the thread pool and rejected invalid Unicode or non-string function arguments with HTTP 400.
app/output_truncation.py
app/adapters/responses_projection.py
app/adapters/responses_adapter.py
converter.py
tests/test_output_truncation.py
tests/test_harness_projection.py
Exposed projection configuration across runtime configuration and request observability.
  • Added mode and max-byte settings with balanced/ passthrough choices, 0 or 256..33554432 byte limits, precedence, source locking, SQLite persistence, hot updates, CLI flags, environment variables, Compose forwarding, and WebUI metadata.
  • Frozen effective mode and limit per request and added the X-CodeBuddy-Responses-Projection response header.
  • Recorded only projection mode, limit, and truncation counters in audits.
app/settings.py
converter.py
app/observability.py
app/audit_store.py
docker-compose.yml
tests/test_environment_config.py
tests/test_deployment.py
tests/test_runtime_endpoints.py
tests/test_observability.py
Updated user-facing documentation and deployment configuration for the new projection behavior.
  • Documented configuration, precedence, truncation semantics, passthrough behavior, unchanged client addresses, and preserved protocol/request limits in English and Chinese.
  • Updated README FAQs, client and deployment guidance, Compose passthrough, and environment examples.
README.md
README.zh-CN.md
docs/advanced.md
docs/advanced.zh-CN.md
docs/clients.md
docs/clients.zh-CN.md
docs/deployment.md
docs/deployment.zh-CN.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-25T15:38:57.769882Z fc35170 PR opened
🔒 Security Review ✅ Completed 2026-09-25T15:39:53.737395Z fc35170 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@sourcery-ai sourcery-ai 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.

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Needs a human reviewer. The default projection changes the context and tool arguments sent upstream, including truncating tool results and JSON values, so an incorrect transformation could cause an agent to make a wrong downstream decision or tool call before the change is reverted. Reverting restores the prior behavior, but it cannot undo actions or external effects that already occurred.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant