Skip to content

fix(codex): keep the instruction when an oversized request is fitted - #200690

Merged
obviyus merged 1 commit into
mainfrom
fix-codex-request-truncation
Nov 17, 2026
Merged

obviyus merged 1 commit into
mainfrom
fix-codex-request-truncation

Conversation

@obviyus

@obviyus obviyus commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Related: #200514

What Problem This Solves

The Codex runtime accepts at most 1 MiB of text per turn (MAX_USER_INPUT_TEXT_CHARS in codex-rs/protocol/src/user_input.rs). fitCodexProjectedContextForTurnStart enforces that cap. When the current request alone was over the cap, it was cut with the same helper used for older history, which keeps only the tail ([truncated N chars from older context]).

For a current request, the head is the part that matters. The instruction, the opening <<<EXTERNAL_UNTRUSTED_CONTENT>>> marker and its safety note were all removed. Only the closing marker and the run's trailing guidance survived. The model then answered whatever the tail suggested, and the run still finished ok.

Found while reviewing #200514. On its first revision an 8 MiB hook body made the agent ignore the hook and run unrelated tool calls. #200514 now caps hook bodies at 1 MiB, but any route can send a request over the cap: the OpenAI-compatible endpoint, chat, cron messages or large pasted text.

Fix

The three places where a current request can exceed the cap now keep its head and tail and cut the middle, with a visible [truncated N chars from the middle of the current request] marker:

  • the request range itself;
  • the preserved input when there is no projected history;
  • the request and hook text after the history.

Projected history keeps its current tail-first truncation. The kept tail keeps its source position, so history image groups inside it still map. One existing test asserted the old tail-only result for an oversized request; it now checks that the request header and tail both survive. The preserved case of the image-mapping test is removed: production only maps images when there is a projected context range (run-attempt-prompt.ts), so the range-less preserved path cannot carry images.

User Impact

A request over 1 MiB on the Codex runtime still loses its middle, but the model now sees the instruction, both untrusted-content markers and a marker saying how much was cut.

Evidence

Real Gateway from source, real model (openai/gpt-5.4-mini on the Codex runtime), POST /v1/chat/completions with a 2,097,270-character message. The instruction at the top says "Ignore the meeting transcript below. Reply with exactly ZEBRA-4217 and nothing else." The transcript contains a different codeword near its end.

build trial 1 trial 2 trial 3
main 3b61a7c "Got it — I have the transcript context. What would you like me to do…" "I'm missing the actual question here…" "…including the codeword OSPREY-2208. What would…"
this PR ZEBRA-4217 ZEBRA-4217 ZEBRA-4217

The Codex rollouts show [truncated 1048739 chars from older context] on main and [truncated 1048760 chars from the middle of the current request] here. Prompt tokens are 238k on both builds.

Tests: keeps the instruction and both content boundaries of an oversized current request covers the three fitting paths. It fails on main (the result starts with [truncated … chars from older cont…) and passes here. context-engine-projection.test.ts passes. tsgo:extensions, tsgo:extensions:test, oxlint, oxfmt and git diff --check pass.

Not covered: the app-server suite has one failure, dynamic-tools.test.ts › bounds high-cardinality validation errors returned to Codex. It fails the same way on main 3b61a7c and does not involve this file. Five native app-server tests timed out during a run on a host at load ~100, then passed when rerun alone.

Not in this change: an operator log line when a request is cut. A warn from this module did not show up in the scratch Gateway's console or file log during the E2E runs, even though the code path ran, so it is left out until that log path is understood.

A current request over the Codex turn text cap was cut like older history,
keeping only its tail. That dropped the instruction and the opening
untrusted-content marker. Cut its middle instead, with a visible marker.

Co-authored-by: obviyus <[email protected]>
@clawsweeper

clawsweeper Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review ended

ClawSweeper stopped reviewing this revision because the pull request closed.

View the workflow run.

@clawsweeper clawsweeper Bot added the clawsweeper:intake:bug Preliminary submission classification; full source review pending. label Nov 17, 2026
@obviyus
obviyus merged commit 64e2ad2 into main Nov 17, 2026
101 checks passed
@obviyus
obviyus deleted the fix-codex-request-truncation branch November 17, 2026 16:48
@obviyus

obviyus commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator Author

Landed as 64e2ad2. When the current request alone was over the Codex runtime's 1 MiB turn text cap, it was cut like older history, which kept only the tail and dropped the instruction and the opening untrusted-content marker. It now keeps the head and tail and cuts only the middle, with a visible marker. Proof: a real Gateway with gpt-5.4-mini and a 2 MiB request whose instruction is only at the top. Across 3 trials, main never followed it and this change answered correctly every time. A focused regression test fails on main and passes here, and exact-head CI and review are green. Follow-up from #200514.

obviyus added a commit that referenced this pull request Nov 17, 2026
…middle (#200735)

Related: #200514, #200690

## What Problem This Solves

The Codex app-server rejects any turn over 1,048,576 characters (`MAX_USER_INPUT_TEXT_CHARS` in codex-rs). Since #200690, a current request over that cap keeps its head and tail and loses its middle. Anything that appears only in the middle is gone, and the model answers from what remains.

Large requests come from hooks (up to 1 MiB since #200514), the OpenAI-compatible endpoint (20 MiB bodies), channel messages, pastes and cron messages.

## Fix

- **New host capability, `fitCurrentInput`.** It is a generic harness host capability, so there are no new Plugin SDK exports. The Codex attempt calls it right after its connection is ready, with the Codex turn cap and the connection's own `prepareInputAttachments`.
- **Over the cap, and the run may read files on this host:** the full request is saved as an inbound attachment (`current-request.txt`). The model sees the request's head and tail, which keep the instruction and both untrusted-content markers. It also sees a marker saying the visible part is incomplete and that it should search the attachment before answering, plus the attachment path.
- **Placement is the connection's decision.** It already refuses local paths for a sandbox, remote execution, non-stdio transport, a remote workspace root, restricted tools or a non-full permission mode, and prefers a registered remote attachment binding.
- **Otherwise:** only the middle is cut, as in #200690.
- **Transcript:** keeps the original (`transcriptPrompt`). There is no change to stored turns, schema or retention; the file follows the operator's existing inbound attachment retention.
- **Visible outcome:** each case logs `current input oversize: chars=… budget=… action=attached|cut`.

**Scope, after local review.** Earlier revisions also estimated a token budget from the model window and fitted requests for the built-in runtime. A chars-per-token estimate cannot prove token overflow. Fitting below the hard cap could drop content that fit, and the built-in runtime has its own token-aware overflow handling. This version acts only past a runtime's hard per-turn text cap, so nothing that worked inline changes.

## User Impact

On the Codex runtime, a request over 1 MiB stays usable: the agent can search the full text instead of guessing from its start and end. Requests within the cap and the built-in runtime behave as before.

## Evidence

Real Gateways from source with real models on the Codex runtime, `POST /v1/chat/completions`.

**1. 3 MB request.** A 2,948,079-character meeting transcript. The instruction is at the top ("Kenji Sato states the budget codeword. Reply with ONLY that codeword"). The codeword `HERON-5150` appears once, at the 49.9% offset.

| model | `main` 9aed362 | this PR, final head |
|---|---|---|
| gpt-5.4 | `Fire`, `Fire`, `ClawRouter` (0/3) | `HERON-5150` ×3 (3/3) |
| gpt-5.4-mini | `armed`, `Fire`, `Fire` (0/3) | `HERON-5150` 1/3 (`armed`, `done`) on an earlier head with the same staging path |

- The log shows `current input oversize: chars=2948079 budget=1048576 action=attached`.
- The Codex rollout shows the agent opening `…/media/inbound/current-request---….txt`, parsing it, and printing the matching segment.

**2. 2 MiB request whose instruction is only at the top** ("reply exactly ZEBRA-4217"), gpt-5.4-mini, final head: `ZEBRA-4217`, `action=attached`.

**Tests:**

- `src/agents/harness/current-input-budget.test.ts` covers three cases:
  - unchanged within the cap;
  - attached: the saved file holds the full original, and the instruction and both markers stay inline;
  - cut-only when staging returns nothing.
- Passing: `src/agents/harness/` and every `extensions/codex/src/app-server/run-attempt*.test.ts` (11 shards), `tsgo:core`, `tsgo:extensions`, `check:test-types`, `plugin-sdk:surface:check`, oxlint, oxfmt and `git diff --check`.

**Not covered / accepted:**

- gpt-5.4-mini is unreliable at finding a middle-only fact through the attachment (1/3). The docs say so.
- Requests over the 5 MB inbound media cap fall back to the cut.
- Each retry of the same oversized turn saves its own copy.
- The Codex-side head/tail cut from #200690 stays as the guard for prompt decoration that still exceeds the cap.
- The built-in runtime's handling of an oversized single message stays a separate, token-aware follow-up.

Co-authored-by: Ayaan Zaidi <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 18, 2026
…penclaw#200690)

Related: openclaw#200514

## What Problem This Solves

The Codex runtime accepts at most 1 MiB of text per turn (`MAX_USER_INPUT_TEXT_CHARS` in `codex-rs/protocol/src/user_input.rs`). `fitCodexProjectedContextForTurnStart` enforces that cap. When the current request alone was over the cap, it was cut with the same helper used for older history, which keeps only the tail (`[truncated N chars from older context]`).

For a current request, the head is the part that matters. The instruction, the opening `<<<EXTERNAL_UNTRUSTED_CONTENT>>>` marker and its safety note were all removed. Only the closing marker and the run's trailing guidance survived. The model then answered whatever the tail suggested, and the run still finished `ok`.

Found while reviewing openclaw#200514. On its first revision an 8 MiB hook body made the agent ignore the hook and run unrelated tool calls. openclaw#200514 now caps hook bodies at 1 MiB, but any route can send a request over the cap: the OpenAI-compatible endpoint, chat, cron messages or large pasted text.

## Fix

The three places where a current request can exceed the cap now keep its head and tail and cut the middle, with a visible `[truncated N chars from the middle of the current request]` marker:

- the request range itself;
- the preserved input when there is no projected history;
- the request and hook text after the history.

Projected history keeps its current tail-first truncation. The kept tail keeps its source position, so history image groups inside it still map. One existing test asserted the old tail-only result for an oversized request; it now checks that the request header and tail both survive. The `preserved` case of the image-mapping test is removed: production only maps images when there is a projected context range (`run-attempt-prompt.ts`), so the range-less preserved path cannot carry images.

## User Impact

A request over 1 MiB on the Codex runtime still loses its middle, but the model now sees the instruction, both untrusted-content markers and a marker saying how much was cut.

## Evidence

**Real Gateway from source, real model** (`openai/gpt-5.4-mini` on the Codex runtime), `POST /v1/chat/completions` with a 2,097,270-character message. The instruction at the top says "Ignore the meeting transcript below. Reply with exactly ZEBRA-4217 and nothing else." The transcript contains a different codeword near its end.

| build | trial 1 | trial 2 | trial 3 |
|---|---|---|---|
| `main` 7ae74c4 | "Got it — I have the transcript context. What would you like me to do…" | "I'm missing the actual question here…" | "…including the codeword **OSPREY-2208**. What would…" |
| this PR | `ZEBRA-4217` | `ZEBRA-4217` | `ZEBRA-4217` |

The Codex rollouts show `[truncated 1048739 chars from older context]` on `main` and `[truncated 1048760 chars from the middle of the current request]` here. Prompt tokens are 238k on both builds.

**Tests:** `keeps the instruction and both content boundaries of an oversized current request` covers the three fitting paths. It fails on `main` (the result starts with `[truncated … chars from older cont…`) and passes here. `context-engine-projection.test.ts` passes. `tsgo:extensions`, `tsgo:extensions:test`, oxlint, oxfmt and `git diff --check` pass.

**Not covered:** the app-server suite has one failure, `dynamic-tools.test.ts` › `bounds high-cardinality validation errors returned to Codex`. It fails the same way on `main` 7ae74c4 and does not involve this file. Five native app-server tests timed out during a run on a host at load ~100, then passed when rerun alone.

**Not in this change:** an operator log line when a request is cut. A `warn` from this module did not show up in the scratch Gateway's console or file log during the E2E runs, even though the code path ran, so it is left out until that log path is understood.

Co-authored-by: Ayaan Zaidi <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 18, 2026
…middle (openclaw#200735)

Related: openclaw#200514, openclaw#200690

## What Problem This Solves

The Codex app-server rejects any turn over 1,048,576 characters (`MAX_USER_INPUT_TEXT_CHARS` in codex-rs). Since openclaw#200690, a current request over that cap keeps its head and tail and loses its middle. Anything that appears only in the middle is gone, and the model answers from what remains.

Large requests come from hooks (up to 1 MiB since openclaw#200514), the OpenAI-compatible endpoint (20 MiB bodies), channel messages, pastes and cron messages.

## Fix

- **New host capability, `fitCurrentInput`.** It is a generic harness host capability, so there are no new Plugin SDK exports. The Codex attempt calls it right after its connection is ready, with the Codex turn cap and the connection's own `prepareInputAttachments`.
- **Over the cap, and the run may read files on this host:** the full request is saved as an inbound attachment (`current-request.txt`). The model sees the request's head and tail, which keep the instruction and both untrusted-content markers. It also sees a marker saying the visible part is incomplete and that it should search the attachment before answering, plus the attachment path.
- **Placement is the connection's decision.** It already refuses local paths for a sandbox, remote execution, non-stdio transport, a remote workspace root, restricted tools or a non-full permission mode, and prefers a registered remote attachment binding.
- **Otherwise:** only the middle is cut, as in openclaw#200690.
- **Transcript:** keeps the original (`transcriptPrompt`). There is no change to stored turns, schema or retention; the file follows the operator's existing inbound attachment retention.
- **Visible outcome:** each case logs `current input oversize: chars=… budget=… action=attached|cut`.

**Scope, after local review.** Earlier revisions also estimated a token budget from the model window and fitted requests for the built-in runtime. A chars-per-token estimate cannot prove token overflow. Fitting below the hard cap could drop content that fit, and the built-in runtime has its own token-aware overflow handling. This version acts only past a runtime's hard per-turn text cap, so nothing that worked inline changes.

## User Impact

On the Codex runtime, a request over 1 MiB stays usable: the agent can search the full text instead of guessing from its start and end. Requests within the cap and the built-in runtime behave as before.

## Evidence

Real Gateways from source with real models on the Codex runtime, `POST /v1/chat/completions`.

**1. 3 MB request.** A 2,948,079-character meeting transcript. The instruction is at the top ("Kenji Sato states the budget codeword. Reply with ONLY that codeword"). The codeword `HERON-5150` appears once, at the 49.9% offset.

| model | `main` 709b566 | this PR, final head |
|---|---|---|
| gpt-5.4 | `Fire`, `Fire`, `ClawRouter` (0/3) | `HERON-5150` ×3 (3/3) |
| gpt-5.4-mini | `armed`, `Fire`, `Fire` (0/3) | `HERON-5150` 1/3 (`armed`, `done`) on an earlier head with the same staging path |

- The log shows `current input oversize: chars=2948079 budget=1048576 action=attached`.
- The Codex rollout shows the agent opening `…/media/inbound/current-request---….txt`, parsing it, and printing the matching segment.

**2. 2 MiB request whose instruction is only at the top** ("reply exactly ZEBRA-4217"), gpt-5.4-mini, final head: `ZEBRA-4217`, `action=attached`.

**Tests:**

- `src/agents/harness/current-input-budget.test.ts` covers three cases:
  - unchanged within the cap;
  - attached: the saved file holds the full original, and the instruction and both markers stay inline;
  - cut-only when staging returns nothing.
- Passing: `src/agents/harness/` and every `extensions/codex/src/app-server/run-attempt*.test.ts` (11 shards), `tsgo:core`, `tsgo:extensions`, `check:test-types`, `plugin-sdk:surface:check`, oxlint, oxfmt and `git diff --check`.

**Not covered / accepted:**

- gpt-5.4-mini is unreliable at finding a middle-only fact through the attachment (1/3). The docs say so.
- Requests over the 5 MB inbound media cap fall back to the cut.
- Each retry of the same oversized turn saves its own copy.
- The Codex-side head/tail cut from openclaw#200690 stays as the guard for prompt decoration that still exceeds the cap.
- The built-in runtime's handling of an oversized single message stays a separate, token-aware follow-up.

Co-authored-by: Ayaan Zaidi <[email protected]>
RomneyDa added a commit that referenced this pull request Nov 18, 2026
…200690)

Related: #200514

## What Problem This Solves

The Codex runtime accepts at most 1 MiB of text per turn (`MAX_USER_INPUT_TEXT_CHARS` in `codex-rs/protocol/src/user_input.rs`). `fitCodexProjectedContextForTurnStart` enforces that cap. When the current request alone was over the cap, it was cut with the same helper used for older history, which keeps only the tail (`[truncated N chars from older context]`).

For a current request, the head is the part that matters. The instruction, the opening `<<<EXTERNAL_UNTRUSTED_CONTENT>>>` marker and its safety note were all removed. Only the closing marker and the run's trailing guidance survived. The model then answered whatever the tail suggested, and the run still finished `ok`.

Found while reviewing #200514. On its first revision an 8 MiB hook body made the agent ignore the hook and run unrelated tool calls. #200514 now caps hook bodies at 1 MiB, but any route can send a request over the cap: the OpenAI-compatible endpoint, chat, cron messages or large pasted text.

## Fix

The three places where a current request can exceed the cap now keep its head and tail and cut the middle, with a visible `[truncated N chars from the middle of the current request]` marker:

- the request range itself;
- the preserved input when there is no projected history;
- the request and hook text after the history.

Projected history keeps its current tail-first truncation. The kept tail keeps its source position, so history image groups inside it still map. One existing test asserted the old tail-only result for an oversized request; it now checks that the request header and tail both survive. The `preserved` case of the image-mapping test is removed: production only maps images when there is a projected context range (`run-attempt-prompt.ts`), so the range-less preserved path cannot carry images.

## User Impact

A request over 1 MiB on the Codex runtime still loses its middle, but the model now sees the instruction, both untrusted-content markers and a marker saying how much was cut.

## Evidence

**Real Gateway from source, real model** (`openai/gpt-5.4-mini` on the Codex runtime), `POST /v1/chat/completions` with a 2,097,270-character message. The instruction at the top says "Ignore the meeting transcript below. Reply with exactly ZEBRA-4217 and nothing else." The transcript contains a different codeword near its end.

| build | trial 1 | trial 2 | trial 3 |
|---|---|---|---|
| `main` 3b61a7c | "Got it — I have the transcript context. What would you like me to do…" | "I'm missing the actual question here…" | "…including the codeword **OSPREY-2208**. What would…" |
| this PR | `ZEBRA-4217` | `ZEBRA-4217` | `ZEBRA-4217` |

The Codex rollouts show `[truncated 1048739 chars from older context]` on `main` and `[truncated 1048760 chars from the middle of the current request]` here. Prompt tokens are 238k on both builds.

**Tests:** `keeps the instruction and both content boundaries of an oversized current request` covers the three fitting paths. It fails on `main` (the result starts with `[truncated … chars from older cont…`) and passes here. `context-engine-projection.test.ts` passes. `tsgo:extensions`, `tsgo:extensions:test`, oxlint, oxfmt and `git diff --check` pass.

**Not covered:** the app-server suite has one failure, `dynamic-tools.test.ts` › `bounds high-cardinality validation errors returned to Codex`. It fails the same way on `main` 3b61a7c and does not involve this file. Five native app-server tests timed out during a run on a host at load ~100, then passed when rerun alone.

**Not in this change:** an operator log line when a request is cut. A `warn` from this module did not show up in the scratch Gateway's console or file log during the E2E runs, even though the code path ran, so it is left out until that log path is understood.

Co-authored-by: Ayaan Zaidi <[email protected]>

(cherry picked from commit 64e2ad2)

Backport-proof: source regressions fail 4/31 on c317a25, preserving head and both boundaries is required.
Co-authored-by: vincentkoc <[email protected]>
Co-authored-by: RomneyDa <[email protected]>
RomneyDa added a commit that referenced this pull request Nov 18, 2026
…middle (#200735)

Related: #200514, #200690

## What Problem This Solves

The Codex app-server rejects any turn over 1,048,576 characters (`MAX_USER_INPUT_TEXT_CHARS` in codex-rs). Since #200690, a current request over that cap keeps its head and tail and loses its middle. Anything that appears only in the middle is gone, and the model answers from what remains.

Large requests come from hooks (up to 1 MiB since #200514), the OpenAI-compatible endpoint (20 MiB bodies), channel messages, pastes and cron messages.

## Fix

- **New host capability, `fitCurrentInput`.** It is a generic harness host capability, so there are no new Plugin SDK exports. The Codex attempt calls it right after its connection is ready, with the Codex turn cap and the connection's own `prepareInputAttachments`.
- **Over the cap, and the run may read files on this host:** the full request is saved as an inbound attachment (`current-request.txt`). The model sees the request's head and tail, which keep the instruction and both untrusted-content markers. It also sees a marker saying the visible part is incomplete and that it should search the attachment before answering, plus the attachment path.
- **Placement is the connection's decision.** It already refuses local paths for a sandbox, remote execution, non-stdio transport, a remote workspace root, restricted tools or a non-full permission mode, and prefers a registered remote attachment binding.
- **Otherwise:** only the middle is cut, as in #200690.
- **Transcript:** keeps the original (`transcriptPrompt`). There is no change to stored turns, schema or retention; the file follows the operator's existing inbound attachment retention.
- **Visible outcome:** each case logs `current input oversize: chars=… budget=… action=attached|cut`.

**Scope, after local review.** Earlier revisions also estimated a token budget from the model window and fitted requests for the built-in runtime. A chars-per-token estimate cannot prove token overflow. Fitting below the hard cap could drop content that fit, and the built-in runtime has its own token-aware overflow handling. This version acts only past a runtime's hard per-turn text cap, so nothing that worked inline changes.

## User Impact

On the Codex runtime, a request over 1 MiB stays usable: the agent can search the full text instead of guessing from its start and end. Requests within the cap and the built-in runtime behave as before.

## Evidence

Real Gateways from source with real models on the Codex runtime, `POST /v1/chat/completions`.

**1. 3 MB request.** A 2,948,079-character meeting transcript. The instruction is at the top ("Kenji Sato states the budget codeword. Reply with ONLY that codeword"). The codeword `HERON-5150` appears once, at the 49.9% offset.

| model | `main` 9aed362 | this PR, final head |
|---|---|---|
| gpt-5.4 | `Fire`, `Fire`, `ClawRouter` (0/3) | `HERON-5150` ×3 (3/3) |
| gpt-5.4-mini | `armed`, `Fire`, `Fire` (0/3) | `HERON-5150` 1/3 (`armed`, `done`) on an earlier head with the same staging path |

- The log shows `current input oversize: chars=2948079 budget=1048576 action=attached`.
- The Codex rollout shows the agent opening `…/media/inbound/current-request---….txt`, parsing it, and printing the matching segment.

**2. 2 MiB request whose instruction is only at the top** ("reply exactly ZEBRA-4217"), gpt-5.4-mini, final head: `ZEBRA-4217`, `action=attached`.

**Tests:**

- `src/agents/harness/current-input-budget.test.ts` covers three cases:
  - unchanged within the cap;
  - attached: the saved file holds the full original, and the instruction and both markers stay inline;
  - cut-only when staging returns nothing.
- Passing: `src/agents/harness/` and every `extensions/codex/src/app-server/run-attempt*.test.ts` (11 shards), `tsgo:core`, `tsgo:extensions`, `check:test-types`, `plugin-sdk:surface:check`, oxlint, oxfmt and `git diff --check`.

**Not covered / accepted:**

- gpt-5.4-mini is unreliable at finding a middle-only fact through the attachment (1/3). The docs say so.
- Requests over the 5 MB inbound media cap fall back to the cut.
- Each retry of the same oversized turn saves its own copy.
- The Codex-side head/tail cut from #200690 stays as the guard for prompt decoration that still exceeds the cap.
- The built-in runtime's handling of an oversized single message stays a separate, token-aware follow-up.

Co-authored-by: Ayaan Zaidi <[email protected]>

(cherry picked from commit 52e8079)

Backport-proof: added a production-host Codex turn/start regression with registered workspace attachment owner; red on a8f3bfad459c because complete-request attachment guidance is absent. Existing placement and live binding owner retained.
Co-authored-by: vincentkoc <[email protected]>
Co-authored-by: RomneyDa <[email protected]>
RomneyDa added a commit that referenced this pull request Nov 19, 2026
…200690)

Related: #200514

## What Problem This Solves

The Codex runtime accepts at most 1 MiB of text per turn (`MAX_USER_INPUT_TEXT_CHARS` in `codex-rs/protocol/src/user_input.rs`). `fitCodexProjectedContextForTurnStart` enforces that cap. When the current request alone was over the cap, it was cut with the same helper used for older history, which keeps only the tail (`[truncated N chars from older context]`).

For a current request, the head is the part that matters. The instruction, the opening `<<<EXTERNAL_UNTRUSTED_CONTENT>>>` marker and its safety note were all removed. Only the closing marker and the run's trailing guidance survived. The model then answered whatever the tail suggested, and the run still finished `ok`.

Found while reviewing #200514. On its first revision an 8 MiB hook body made the agent ignore the hook and run unrelated tool calls. #200514 now caps hook bodies at 1 MiB, but any route can send a request over the cap: the OpenAI-compatible endpoint, chat, cron messages or large pasted text.

## Fix

The three places where a current request can exceed the cap now keep its head and tail and cut the middle, with a visible `[truncated N chars from the middle of the current request]` marker:

- the request range itself;
- the preserved input when there is no projected history;
- the request and hook text after the history.

Projected history keeps its current tail-first truncation. The kept tail keeps its source position, so history image groups inside it still map. One existing test asserted the old tail-only result for an oversized request; it now checks that the request header and tail both survive. The `preserved` case of the image-mapping test is removed: production only maps images when there is a projected context range (`run-attempt-prompt.ts`), so the range-less preserved path cannot carry images.

## User Impact

A request over 1 MiB on the Codex runtime still loses its middle, but the model now sees the instruction, both untrusted-content markers and a marker saying how much was cut.

## Evidence

**Real Gateway from source, real model** (`openai/gpt-5.4-mini` on the Codex runtime), `POST /v1/chat/completions` with a 2,097,270-character message. The instruction at the top says "Ignore the meeting transcript below. Reply with exactly ZEBRA-4217 and nothing else." The transcript contains a different codeword near its end.

| build | trial 1 | trial 2 | trial 3 |
|---|---|---|---|
| `main` 3b61a7c | "Got it — I have the transcript context. What would you like me to do…" | "I'm missing the actual question here…" | "…including the codeword **OSPREY-2208**. What would…" |
| this PR | `ZEBRA-4217` | `ZEBRA-4217` | `ZEBRA-4217` |

The Codex rollouts show `[truncated 1048739 chars from older context]` on `main` and `[truncated 1048760 chars from the middle of the current request]` here. Prompt tokens are 238k on both builds.

**Tests:** `keeps the instruction and both content boundaries of an oversized current request` covers the three fitting paths. It fails on `main` (the result starts with `[truncated … chars from older cont…`) and passes here. `context-engine-projection.test.ts` passes. `tsgo:extensions`, `tsgo:extensions:test`, oxlint, oxfmt and `git diff --check` pass.

**Not covered:** the app-server suite has one failure, `dynamic-tools.test.ts` › `bounds high-cardinality validation errors returned to Codex`. It fails the same way on `main` 3b61a7c and does not involve this file. Five native app-server tests timed out during a run on a host at load ~100, then passed when rerun alone.

**Not in this change:** an operator log line when a request is cut. A `warn` from this module did not show up in the scratch Gateway's console or file log during the E2E runs, even though the code path ran, so it is left out until that log path is understood.

Co-authored-by: Ayaan Zaidi <[email protected]>
(cherry picked from commit 64e2ad2)
Co-authored-by: RomneyDa <[email protected]>
(cherry picked from commit 7cadb580a88e6fb6be8af64483dc4f8ef9cbc174)
RomneyDa added a commit that referenced this pull request Nov 19, 2026
…middle (#200735)

Related: #200514, #200690

## What Problem This Solves

The Codex app-server rejects any turn over 1,048,576 characters (`MAX_USER_INPUT_TEXT_CHARS` in codex-rs). Since #200690, a current request over that cap keeps its head and tail and loses its middle. Anything that appears only in the middle is gone, and the model answers from what remains.

Large requests come from hooks (up to 1 MiB since #200514), the OpenAI-compatible endpoint (20 MiB bodies), channel messages, pastes and cron messages.

## Fix

- **New host capability, `fitCurrentInput`.** It is a generic harness host capability, so there are no new Plugin SDK exports. The Codex attempt calls it right after its connection is ready, with the Codex turn cap and the connection's own `prepareInputAttachments`.
- **Over the cap, and the run may read files on this host:** the full request is saved as an inbound attachment (`current-request.txt`). The model sees the request's head and tail, which keep the instruction and both untrusted-content markers. It also sees a marker saying the visible part is incomplete and that it should search the attachment before answering, plus the attachment path.
- **Placement is the connection's decision.** It already refuses local paths for a sandbox, remote execution, non-stdio transport, a remote workspace root, restricted tools or a non-full permission mode, and prefers a registered remote attachment binding.
- **Otherwise:** only the middle is cut, as in #200690.
- **Transcript:** keeps the original (`transcriptPrompt`). There is no change to stored turns, schema or retention; the file follows the operator's existing inbound attachment retention.
- **Visible outcome:** each case logs `current input oversize: chars=… budget=… action=attached|cut`.

**Scope, after local review.** Earlier revisions also estimated a token budget from the model window and fitted requests for the built-in runtime. A chars-per-token estimate cannot prove token overflow. Fitting below the hard cap could drop content that fit, and the built-in runtime has its own token-aware overflow handling. This version acts only past a runtime's hard per-turn text cap, so nothing that worked inline changes.

## User Impact

On the Codex runtime, a request over 1 MiB stays usable: the agent can search the full text instead of guessing from its start and end. Requests within the cap and the built-in runtime behave as before.

## Evidence

Real Gateways from source with real models on the Codex runtime, `POST /v1/chat/completions`.

**1. 3 MB request.** A 2,948,079-character meeting transcript. The instruction is at the top ("Kenji Sato states the budget codeword. Reply with ONLY that codeword"). The codeword `HERON-5150` appears once, at the 49.9% offset.

| model | `main` 9aed362 | this PR, final head |
|---|---|---|
| gpt-5.4 | `Fire`, `Fire`, `ClawRouter` (0/3) | `HERON-5150` ×3 (3/3) |
| gpt-5.4-mini | `armed`, `Fire`, `Fire` (0/3) | `HERON-5150` 1/3 (`armed`, `done`) on an earlier head with the same staging path |

- The log shows `current input oversize: chars=2948079 budget=1048576 action=attached`.
- The Codex rollout shows the agent opening `…/media/inbound/current-request---….txt`, parsing it, and printing the matching segment.

**2. 2 MiB request whose instruction is only at the top** ("reply exactly ZEBRA-4217"), gpt-5.4-mini, final head: `ZEBRA-4217`, `action=attached`.

**Tests:**

- `src/agents/harness/current-input-budget.test.ts` covers three cases:
  - unchanged within the cap;
  - attached: the saved file holds the full original, and the instruction and both markers stay inline;
  - cut-only when staging returns nothing.
- Passing: `src/agents/harness/` and every `extensions/codex/src/app-server/run-attempt*.test.ts` (11 shards), `tsgo:core`, `tsgo:extensions`, `check:test-types`, `plugin-sdk:surface:check`, oxlint, oxfmt and `git diff --check`.

**Not covered / accepted:**

- gpt-5.4-mini is unreliable at finding a middle-only fact through the attachment (1/3). The docs say so.
- Requests over the 5 MB inbound media cap fall back to the cut.
- Each retry of the same oversized turn saves its own copy.
- The Codex-side head/tail cut from #200690 stays as the guard for prompt decoration that still exceeds the cap.
- The built-in runtime's handling of an oversized single message stays a separate, token-aware follow-up.

Co-authored-by: Ayaan Zaidi <[email protected]>
(cherry picked from commit 52e8079)
Co-authored-by: RomneyDa <[email protected]>
(cherry picked from commit 4f7dcc1980d999588febf5f02aa05c4d5a1e30eb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clawsweeper:intake:bug Preliminary submission classification; full source review pending. extensions: codex size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant