-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
fix(codex): strip intermediary updates from prompts #4131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
fichas
wants to merge
1
commit into
router-for-me:dev
Choose a base branch
from
fichas:agent/codex-strip-intermediary-updates
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+180
−1
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package executor | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "strings" | ||
| ) | ||
|
|
||
| const codexIntermediaryUpdatesHeading = "## Intermediary updates" | ||
|
|
||
| func stripCodexIntermediaryUpdatesFromPayload(body []byte) []byte { | ||
| if !bytes.Contains(body, []byte(codexIntermediaryUpdatesHeading)) { | ||
| return body | ||
| } | ||
|
|
||
| var payload map[string]any | ||
| decoder := json.NewDecoder(bytes.NewReader(body)) | ||
| decoder.UseNumber() | ||
| if err := decoder.Decode(&payload); err != nil { | ||
| return body | ||
| } | ||
|
|
||
| instructions, ok := payload["instructions"].(string) | ||
| if !ok { | ||
| return body | ||
| } | ||
| stripped, changed := stripCodexIntermediaryUpdatesText(instructions) | ||
| if !changed { | ||
| return body | ||
| } | ||
| payload["instructions"] = stripped | ||
|
|
||
| var out bytes.Buffer | ||
| encoder := json.NewEncoder(&out) | ||
| encoder.SetEscapeHTML(false) | ||
| if err := encoder.Encode(payload); err != nil { | ||
| return body | ||
| } | ||
| return bytes.TrimSpace(out.Bytes()) | ||
| } | ||
|
|
||
| func stripCodexIntermediaryUpdatesText(text string) (string, bool) { | ||
| if !strings.Contains(text, codexIntermediaryUpdatesHeading) { | ||
| return text, false | ||
| } | ||
|
|
||
| lines := strings.SplitAfter(text, "\n") | ||
| out := make([]string, 0, len(lines)) | ||
| skipping := false | ||
| changed := false | ||
|
|
||
| for _, line := range lines { | ||
| trimmed := strings.TrimSpace(strings.TrimSuffix(strings.TrimSuffix(line, "\n"), "\r")) | ||
| if !skipping && trimmed == codexIntermediaryUpdatesHeading { | ||
| skipping = true | ||
| changed = true | ||
| continue | ||
| } | ||
|
|
||
| if skipping { | ||
| if isCodexIntermediaryUpdatesStopHeading(trimmed) { | ||
| skipping = false | ||
| out = append(out, line) | ||
| } | ||
| continue | ||
| } | ||
|
|
||
| out = append(out, line) | ||
| } | ||
|
|
||
| if !changed { | ||
| return text, false | ||
| } | ||
| return strings.Join(out, ""), true | ||
| } | ||
|
|
||
| func isCodexIntermediaryUpdatesStopHeading(trimmedLine string) bool { | ||
| if trimmedLine == codexIntermediaryUpdatesHeading { | ||
| return false | ||
| } | ||
|
|
||
| headingLevel := 0 | ||
| for headingLevel < len(trimmedLine) && headingLevel < 6 && trimmedLine[headingLevel] == '#' { | ||
| headingLevel++ | ||
| } | ||
| return headingLevel > 0 && len(trimmedLine) > headingLevel && trimmedLine[headingLevel] == ' ' | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package executor | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/tidwall/gjson" | ||
| ) | ||
|
|
||
| func TestStripCodexIntermediaryUpdatesFromPayloadTopLevelInstructions(t *testing.T) { | ||
| payload := []byte(`{"instructions":"before\n\n## Intermediary updates \n- noisy updates\n- more noise\n## Final answer instructions\nkeep this","input":[{"role":"user","content":"hello"}]}`) | ||
|
|
||
| out := stripCodexIntermediaryUpdatesFromPayload(payload) | ||
| instructions := gjson.GetBytes(out, "instructions").String() | ||
|
|
||
| if strings.Contains(instructions, "Intermediary updates") || strings.Contains(instructions, "noisy updates") { | ||
| t.Fatalf("intermediary section was not stripped: %q", instructions) | ||
| } | ||
| if !strings.Contains(instructions, "before") || !strings.Contains(instructions, "## Final answer instructions") { | ||
| t.Fatalf("surrounding instructions were not preserved: %q", instructions) | ||
| } | ||
| } | ||
|
|
||
| func TestStripCodexIntermediaryUpdatesFromPayloadLeavesNestedInputText(t *testing.T) { | ||
| payload := []byte(`{"instructions":"keep","input":[{"content":[{"type":"input_text","text":"alpha\n## Intermediary updates\nremove me"}]}],"reasoning":{"effort":"high"}}`) | ||
|
|
||
| out := stripCodexIntermediaryUpdatesFromPayload(payload) | ||
| text := gjson.GetBytes(out, "input.0.content.0.text").String() | ||
|
|
||
| if !strings.Contains(text, "Intermediary updates") || !strings.Contains(text, "remove me") { | ||
| t.Fatalf("nested user text was stripped unexpectedly: %q", text) | ||
| } | ||
| if !strings.Contains(text, "alpha") { | ||
| t.Fatalf("nested prompt prefix was not preserved: %q", text) | ||
| } | ||
| if got := gjson.GetBytes(out, "reasoning.effort").String(); got != "high" { | ||
| t.Fatalf("unrelated fields changed: reasoning.effort=%q", got) | ||
| } | ||
| } | ||
|
|
||
| func TestStripCodexIntermediaryUpdatesFromPayloadOnlyTouchesInstructions(t *testing.T) { | ||
| payload := []byte(`{"instructions":"keep\n## Intermediary updates\nremove me\n## Final answer instructions\nanswer","input":[{"content":[{"type":"input_text","text":"alpha\n## Intermediary updates\nkeep this user text"}]}]}`) | ||
|
|
||
| out := stripCodexIntermediaryUpdatesFromPayload(payload) | ||
| instructions := gjson.GetBytes(out, "instructions").String() | ||
| text := gjson.GetBytes(out, "input.0.content.0.text").String() | ||
|
|
||
| if strings.Contains(instructions, "Intermediary updates") || strings.Contains(instructions, "remove me") { | ||
| t.Fatalf("instructions intermediary section was not stripped: %q", instructions) | ||
| } | ||
| if !strings.Contains(instructions, "## Final answer instructions") { | ||
| t.Fatalf("following instruction section was not preserved: %q", instructions) | ||
| } | ||
| if !strings.Contains(text, "Intermediary updates") || !strings.Contains(text, "keep this user text") { | ||
| t.Fatalf("nested user text was stripped unexpectedly: %q", text) | ||
| } | ||
| } | ||
|
|
||
| func TestStripCodexIntermediaryUpdatesFromPayloadLeavesUnrelatedPayload(t *testing.T) { | ||
| payload := []byte(`{"instructions":"inline mention of ## Intermediary updates should stay","input":[]}`) | ||
|
|
||
| out := stripCodexIntermediaryUpdatesFromPayload(payload) | ||
| if string(out) != string(payload) { | ||
| t.Fatalf("payload changed unexpectedly:\ngot %s\nwant %s", string(out), string(payload)) | ||
| } | ||
| } | ||
|
|
||
| func TestStripCodexIntermediaryUpdatesTextRemovesLastSection(t *testing.T) { | ||
| out, changed := stripCodexIntermediaryUpdatesText("before\n## Intermediary updates\n- remove me") | ||
| if !changed { | ||
| t.Fatal("expected text to change") | ||
| } | ||
| if strings.Contains(out, "Intermediary updates") || strings.Contains(out, "remove me") { | ||
| t.Fatalf("last section was not stripped: %q", out) | ||
| } | ||
| if !strings.Contains(out, "before") { | ||
| t.Fatalf("prefix was not preserved: %q", out) | ||
| } | ||
| } | ||
|
|
||
| func TestStripCodexIntermediaryUpdatesTextStopsAtMarkdownHeading(t *testing.T) { | ||
| out, changed := stripCodexIntermediaryUpdatesText("before\n## Intermediary updates\n- remove me\n## Intermediary updates\n- remove me too\n### Keep this\nkeep") | ||
| if !changed { | ||
| t.Fatal("expected text to change") | ||
| } | ||
| if strings.Contains(out, "Intermediary updates") || strings.Contains(out, "remove me") { | ||
| t.Fatalf("intermediary sections were not stripped: %q", out) | ||
| } | ||
| if !strings.Contains(out, "before") || !strings.Contains(out, "### Keep this\nkeep") { | ||
| t.Fatalf("surrounding markdown was not preserved: %q", out) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation of
isCodexIntermediaryUpdatesStopHeadingonly stops skipping when it encounters#or##headings. If the prompt contains subheadings of other levels (such as###,####, etc.) after the## Intermediary updatessection, they will be skipped and incorrectly stripped from the payload.\n\nAdditionally, if another## Intermediary updatesheading is encountered while skipping, it will be treated as a stop heading and stop the skipping process, which is also unintended.\n\nWe should update this function to:\n1. Avoid stopping if the heading is exactly## Intermediary updates.\n2. Support all standard Markdown heading levels (H1 to H6, i.e., 1 to 6#characters followed by a space).