Skip to content

fix: add second-chance developer->user compat learn (#583) - #585

Merged
ranxianglei merged 1 commit into
masterfrom
2026-09-06_compat-second-chance-developer-user
Sep 7, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-09-06_compat-second-chance-developer-user

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Summary

Adds a bounded second-chance hop to the #552 learn-on-failure compat auto-fix, closing the edge described in #583.

On the Responses path, if a backend rejects an unknown role (developer -> 400) and only accepts system at index 0 (#377-class placement), the existing single retry rewrites the offending role to system. When that developer sits mid-list, the rewritten system is also mid-list -> the retry 400s again with a placement error (not a role-name error) -> the original 400 passes through verbatim and the auto-learn never engages.

This change adds one more controlled retry for exactly that case:

  • New detectSystemPlacementError(status, bodyText) in src/compat-roles.ts — a conservative detector for system-placement 400s (Only one 'system' ... at the beginning, Multiple system messages, system ... at index N, ...). It requires a 400 plus the word "system" plus a strong placement/multiplicity marker, so quota / overflow / missing-system / auth 400s never trip it.
  • In src/server.ts the single-retry block becomes a fixed ladder: primary developer->system; if (and only if) that 400s with a placement error, one more developer->user. On success the session learns the winning mapping (developer->user wins over the generic ->system). Hard cap stays at original + 2 retries, never a loop; anything still failing passes the original 400 through verbatim.

Scope notes (triage finding)

  • Chat path unaffected — systemToUser(coreToOpenai(...)) already normalizes mid-list system/developer to user before the wire, so the rewrite is a no-op there.
  • One nuance worth flagging: through the normal compression pipeline a rejected developer is hoisted to index 0 by the kernel (injectResponsesDeveloperMessage), so the primary ->system retry already lands at index 0 and succeeds on placement-strict backends. A developer reaches the boundary mid-list only when it is non-normalized (e.g. missing type) or when the unchanged body is forwarded (transform-failure fallback). Both are covered by the new hop — so this is a defensive safety-net rather than a common-path fix, but it closes the class cleanly.

Tests

  • Unit test for detectSystemPlacementError (positive placement messages incl. quoted 'system'; negative role/quota/overflow/auth; non-400 status).
  • e2e G: mid-list developer -> primary system placement-400 -> second-chance user -> transparent 200; the next request on the same session pre-rewrites via the learned map (no round-trip). Asserts the exact 3-hit ladder + no idle-timer leak.
  • e2e H: every hop fails -> exactly 3 hits, original 400 passed through verbatim, no loop, no timer leak.
  • Existing billion-context: 400 Bad Request when codex sends role: "developer" messages — request a request-body compat transform hook #552 E/F regression tests still pass (primary hop unchanged; F's constant Invalid role: body does not match the placement detector, so it stays a single retry).

Pre-flight

  • tsc --noEmit clean
  • npm run build OK (self-contained dist/index.js)
  • Full suite green except a pre-existing, environment-dependent launcher.test.ts case (codex resolve to themselves) that fails identically on a clean tree whenever a real codex binary is present on PATH — unrelated to this change.
  • Responses e2e (tests/e2e/e2e-codex.test.ts, gated by ACP_TEST_E2E=1) not run here (needs a live upstream); CI manual-dispatch covers it.

On the Responses path, a mid-list `developer` that a backend rejects can be
rewritten to a mid-list `system` by the existing #552 auto-retry, which then
400s again on backends that only accept `system` at index 0 (#377 class).

Add a bounded second-chance hop: when the primary developer→system retry fails
with a system-placement error (new detectSystemPlacementError), do ONE more
controlled retry rewriting the offending role to `user`; on success learn
developer→user for the session (position-class learning wins over the generic
one). Hard cap stays at original + 2 retries, fixed sequence (never a loop);
anything still failing passes the original 400 through verbatim.

Notes:
- Chat path unaffected: systemToUser(coreToOpenai(...)) already normalizes
  mid-list system/developer to user before the wire, so the rewrite is a no-op.
- Through the normal compression pipeline a rejected developer is HOISTED to
  index 0 (injectResponsesDeveloperMessage), so the primary retry already
  suffices; the second-chance covers the cases where a developer survives
  mid-list to the boundary (non-normalized items / unchanged-body fallbacks).

Pre-flight: tsc --noEmit clean; npm run build OK; full suite green except a
pre-existing environment-dependent launcher.test.ts case (fails identically on
a clean tree when a real `codex` binary is on PATH). New unit test for
detectSystemPlacementError plus e2e G (ladder + learn) and H (hard cap /
verbatim). Existing #552 E/F regression tests still pass.
@github-actions

github-actions Bot commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-06_compat-second-chance-developer-user (4cc4d72)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-585

Each push to this PR publishes a new version under the pr-585 npm tag.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr585.tgz
npm install -g package

This comment is automatically updated on each push.

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