History

feat(executor): promote reasoning content as summary and sanitize inputs

- Introduce `promoteOpenAIResponsesReasoningTextToSummary` to move `reasoning_text` parts from content to summary when the summary is empty. - Clear `reasoning.content` to comply with Codex schema constraints (`maxItems: 0`). - Implement safeguards for handling cleartext reasoning, preserving valid encrypted content, and stripping invalid `encrypted_content`. - Add comprehensive tests to validate behavior for promoting reasoning texts, preserving existing summaries, and ensuring sanitization rules.

Luis Pater committed 2 months ago

Commit 7bbfeaf1 parent 8c984672 files · +169 −10Browse files

Unified · Split

2 files
  1. internal/runtime/executor/openai_responses_signature.go +87 −10
  2. internal/runtime/executor/openai_responses_signature_test.go +82 −0
internal/runtime/executor/openai_responses_signature.go+87−10
@@ -12,6 +12,42 @@ import ( · 11 unmodified lines
12 "github.com/tidwall/sjson"12 "github.com/tidwall/sjson"
13)13)
14 14
15func openaiResponsesReasoningSummaryIsEmpty(summary gjson.Result) bool {
16 if !summary.Exists() || summary.Type == gjson.Null {
17 return true
18 }
19 return summary.IsArray() && len(summary.Array()) == 0
20}
21
22func promoteOpenAIResponsesReasoningTextToSummary(itemRaw string, content gjson.Result) (string, error) {
23 var b strings.Builder
24 b.WriteByte('[')
25 n := 0
26 for _, part := range content.Array() {
27 if strings.TrimSpace(part.Get("type").String()) != "reasoning_text" {
28 continue
29 }
30 text := part.Get("text").String()
31 if text == "" {
32 continue
33 }
34 partJSON, err := sjson.Set(`{"type":"summary_text"}`, "text", text)
35 if err != nil {
36 return itemRaw, err
37 }
38 if n > 0 {
39 b.WriteByte(',')
40 }
41 b.WriteString(partJSON)
42 n++
43 }
44 b.WriteByte(']')
45 if n == 0 {
46 return itemRaw, nil
47 }
48 return sjson.SetRaw(itemRaw, "summary", b.String())
49}
50
15func sanitizeOpenAIResponsesReasoningEncryptedContent(ctx context.Context, provider string, body []byte) []byte {51func sanitizeOpenAIResponsesReasoningEncryptedContent(ctx context.Context, provider string, body []byte) []byte {
16 inputResult := util.GetGJSONBytesNoCopy(body, "input")52 inputResult := util.GetGJSONBytesNoCopy(body, "input")
17 if !inputResult.Exists() || !inputResult.IsArray() {53 if !inputResult.Exists() || !inputResult.IsArray() {
@@ -73,20 +109,49 @@ func sanitizeOpenAIResponsesReasoningEncryptedContent(ctx context.Context, provi · 55 unmodified lines
73 itemID = fmt.Sprintf("input[%d]", index)109 itemID = fmt.Sprintf("input[%d]", index)
74 }110 }
75 111
112 nextItem := item.Raw
113 changed := false
114
115 // Official Codex schema sets maxItems: 0 on reasoning.content. Third-party
116 // channels replay cleartext thinking there; promote it into summary when
117 // summary is empty, then force content to [].
118 content := item.Get("content")
119 if content.IsArray() && len(content.Array()) > 0 {
120 if openaiResponsesReasoningSummaryIsEmpty(item.Get("summary")) {
121 promoted, errPromote := promoteOpenAIResponsesReasoningTextToSummary(nextItem, content)
122 if errPromote != nil {
123 helps.LogWithRequestID(ctx).Debugf("%s: failed to promote reasoning_text into summary at input[%d]: %v", provider, index, errPromote)
124 } else {
125 nextItem = promoted
126 }
127 }
128 cleared, errClear := sjson.SetRaw(nextItem, "content", "[]")
129 if errClear != nil {
130 helps.LogWithRequestID(ctx).Debugf("%s: failed to clear reasoning content at input[%d]: %v", provider, index, errClear)
131 } else {
132 nextItem = cleared
133 changed = true
134 helps.LogWithRequestID(ctx).Debugf("%s: cleared reasoning content at input[%d] item_id=%q", provider, index, itemID)
135 }
136 }
137
76 if !encryptedContent.Exists() {138 if !encryptedContent.Exists() {
77 if stripOrphanReasoningIDs && item.Get("id").Exists() {139 if stripOrphanReasoningIDs && item.Get("id").Exists() {
78 nextItem, err := sjson.Delete(item.Raw, "id")140 dropped, err := sjson.Delete(nextItem, "id")
79 if err != nil {141 if err != nil {
80 helps.LogWithRequestID(ctx).Debugf("%s: failed to drop orphan reasoning id at input[%d]: %v", provider, index, err)142 helps.LogWithRequestID(ctx).Debugf("%s: failed to drop orphan reasoning id at input[%d]: %v", provider, index, err)
81 keep(item.Raw)143 } else {
82 continue144 nextItem = dropped
145 changed = true
146 helps.LogWithRequestID(ctx).Debugf("%s: dropped orphan reasoning id at input[%d] item_id=%q reason=missing encrypted_content with store disabled", provider, index, itemID)
83 }147 }
84 startRebuild(index)148 }
85 keep(nextItem)149 if !changed {
86 helps.LogWithRequestID(ctx).Debugf("%s: dropped orphan reasoning id at input[%d] item_id=%q reason=missing encrypted_content with store disabled", provider, index, itemID)150 keep(item.Raw)
87 continue151 continue
88 }152 }
89 keep(item.Raw)153 startRebuild(index)
154 keep(nextItem)
90 continue155 continue
91 }156 }
92 157
@@ -105,16 +170,28 @@ func sanitizeOpenAIResponsesReasoningEncryptedContent(ctx context.Context, provi · 12 unmodified lines
105 reason = fmt.Sprintf("encrypted_content must be a string, got %s", encryptedContent.Type.String())170 reason = fmt.Sprintf("encrypted_content must be a string, got %s", encryptedContent.Type.String())
106 }171 }
107 if reason == "" {172 if reason == "" {
108 keep(item.Raw)173 if !changed {
174 keep(item.Raw)
175 continue
176 }
177 startRebuild(index)
178 keep(nextItem)
109 continue179 continue
110 }180 }
111 181
112 nextItem, err := sjson.Delete(item.Raw, "encrypted_content")182 dropped, err := sjson.Delete(nextItem, "encrypted_content")
113 if err != nil {183 if err != nil {
114 helps.LogWithRequestID(ctx).Debugf("%s: failed to drop invalid reasoning encrypted_content at input[%d]: %v", provider, index, err)184 helps.LogWithRequestID(ctx).Debugf("%s: failed to drop invalid reasoning encrypted_content at input[%d]: %v", provider, index, err)
115 keep(item.Raw)185 if !changed {
186 keep(item.Raw)
187 continue
188 }
189 startRebuild(index)
190 keep(nextItem)
116 continue191 continue
117 }192 }
193 nextItem = dropped
194 changed = true
118 if stripOrphanReasoningIDs && item.Get("id").Exists() {195 if stripOrphanReasoningIDs && item.Get("id").Exists() {
119 if nextID, errID := sjson.Delete(nextItem, "id"); errID != nil {196 if nextID, errID := sjson.Delete(nextItem, "id"); errID != nil {
120 helps.LogWithRequestID(ctx).Debugf("%s: failed to drop reasoning id after invalid encrypted_content at input[%d]: %v", provider, index, errID)197 helps.LogWithRequestID(ctx).Debugf("%s: failed to drop reasoning id after invalid encrypted_content at input[%d]: %v", provider, index, errID)
internal/runtime/executor/openai_responses_signature_test.go+82−0
@@ -11,6 +11,14 @@ import ( · 10 unmodified lines
11 11
12var benchmarkSanitizeOpenAIResponsesReasoningOutput []byte12var benchmarkSanitizeOpenAIResponsesReasoningOutput []byte
13 13
14func assertEmptyReasoningContent(t *testing.T, body []byte, path string) {
15 t.Helper()
16 content := gjson.GetBytes(body, path)
17 if !content.Exists() || !content.IsArray() || len(content.Array()) != 0 {
18 t.Fatalf("%s should be an empty array for Codex maxItems:0, got %s body=%s", path, content.Raw, body)
19 }
20}
21
14func validOpenAIResponsesReasoningEncryptedContentForTest() string {22func validOpenAIResponsesReasoningEncryptedContentForTest() string {
15 payload := make([]byte, 1+8+16+16+32)23 payload := make([]byte, 1+8+16+16+32)
16 payload[0] = 0x8024 payload[0] = 0x80
@@ -70,6 +78,80 @@ func TestSanitizeOpenAIResponsesReasoningEncryptedContent_KeepsIDsWhenStoreEnabl · 53 unmodified lines
70 }78 }
71}79}
72 80
81func TestSanitizeOpenAIResponsesReasoningEncryptedContent_MovesCleartextContentToSummary(t *testing.T) {
82 // Codex Desktop replays third-party thinking as reasoning.content[reasoning_text].
83 // Official Codex upstream rejects that shape with array_above_max_length (maxItems: 0).
84 body := []byte(`{"store":false,"input":[` +
85 `{"type":"reasoning","summary":[],"content":[{"type":"reasoning_text","text":"The model thinking process from a previous turn with a third-party provider..."}],"encrypted_content":null},` +
86 `{"id":"msg_1","type":"message","role":"user","content":[{"type":"input_text","text":"hi"}]}` +
87 `]}`)
88
89 got := sanitizeOpenAIResponsesReasoningEncryptedContent(context.Background(), "test", body)
90
91 assertEmptyReasoningContent(t, got, "input.0.content")
92 if gotType := gjson.GetBytes(got, "input.0.summary.0.type").String(); gotType != "summary_text" {
93 t.Fatalf("summary.0.type = %q, want summary_text; body=%s", gotType, got)
94 }
95 if gotText := gjson.GetBytes(got, "input.0.summary.0.text").String(); gotText != "The model thinking process from a previous turn with a third-party provider..." {
96 t.Fatalf("summary.0.text = %q, want promoted reasoning_text; body=%s", gotText, got)
97 }
98 if gjson.GetBytes(got, "input.0.encrypted_content").Exists() {
99 t.Fatalf("null encrypted_content should still be stripped: %s", got)
100 }
101 if gotType := gjson.GetBytes(got, "input.1.content.0.type").String(); gotType != "input_text" {
102 t.Fatalf("non-reasoning content should stay: %s", got)
103 }
104}
105
106func TestSanitizeOpenAIResponsesReasoningEncryptedContent_DoesNotDuplicateExistingSummary(t *testing.T) {
107 body := []byte(`{"store":false,"input":[{"type":"reasoning","summary":[{"type":"summary_text","text":"already summarized"}],"content":[{"type":"reasoning_text","text":"duplicate thinking"}]}]}`)
108
109 got := sanitizeOpenAIResponsesReasoningEncryptedContent(context.Background(), "test", body)
110
111 assertEmptyReasoningContent(t, got, "input.0.content")
112 if gotLen := len(gjson.GetBytes(got, "input.0.summary").Array()); gotLen != 1 {
113 t.Fatalf("summary length = %d, want 1 (no duplicate); body=%s", gotLen, got)
114 }
115 if gotText := gjson.GetBytes(got, "input.0.summary.0.text").String(); gotText != "already summarized" {
116 t.Fatalf("existing summary overwritten: %s", got)
117 }
118}
119
120func TestSanitizeOpenAIResponsesReasoningEncryptedContent_PromotesMultipleReasoningTextParts(t *testing.T) {
121 body := []byte(`{"store":false,"input":[{"type":"reasoning","summary":[],"content":[{"type":"reasoning_text","text":"step one"},{"type":"reasoning_text","text":"step two"}]}]}`)
122
123 got := sanitizeOpenAIResponsesReasoningEncryptedContent(context.Background(), "test", body)
124
125 assertEmptyReasoningContent(t, got, "input.0.content")
126 if gotLen := len(gjson.GetBytes(got, "input.0.summary").Array()); gotLen != 2 {
127 t.Fatalf("summary length = %d, want 2; body=%s", gotLen, got)
128 }
129 if gotText := gjson.GetBytes(got, "input.0.summary.0.text").String(); gotText != "step one" {
130 t.Fatalf("summary.0.text = %q, want step one; body=%s", gotText, got)
131 }
132 if gotText := gjson.GetBytes(got, "input.0.summary.1.text").String(); gotText != "step two" {
133 t.Fatalf("summary.1.text = %q, want step two; body=%s", gotText, got)
134 }
135}
136
137func TestSanitizeOpenAIResponsesReasoningEncryptedContent_KeepsValidEncryptedContentWhenStrippingContent(t *testing.T) {
138 valid := validOpenAIResponsesReasoningEncryptedContentForTest()
139 body := []byte(`{"store":false,"input":[{"id":"rs_good","type":"reasoning","encrypted_content":"` + valid + `","summary":[],"content":[{"type":"reasoning_text","text":"cleartext thinking"}]}]}`)
140
141 got := sanitizeOpenAIResponsesReasoningEncryptedContent(context.Background(), "test", body)
142
143 assertEmptyReasoningContent(t, got, "input.0.content")
144 if gotID := gjson.GetBytes(got, "input.0.id").String(); gotID != "rs_good" {
145 t.Fatalf("valid reasoning id = %q, want rs_good; body=%s", gotID, got)
146 }
147 if gotEC := gjson.GetBytes(got, "input.0.encrypted_content").String(); gotEC != valid {
148 t.Fatalf("valid encrypted_content not preserved: %s", got)
149 }
150 if gotText := gjson.GetBytes(got, "input.0.summary.0.text").String(); gotText != "cleartext thinking" {
151 t.Fatalf("summary.0.text = %q, want promoted reasoning_text; body=%s", gotText, got)
152 }
153}
154
73func TestSanitizeOpenAIResponsesReasoningEncryptedContent_NoopReturnsOriginalBody(t *testing.T) {155func TestSanitizeOpenAIResponsesReasoningEncryptedContent_NoopReturnsOriginalBody(t *testing.T) {
74 valid := validOpenAIResponsesReasoningEncryptedContentForTest()156 valid := validOpenAIResponsesReasoningEncryptedContentForTest()
75 body := []byte(`{"store":false,"input":[{"id":"rs_good","type":"reasoning","encrypted_content":"` + valid + `","summary":[]},{"role":"user","content":"hi"}]}`)157 body := []byte(`{"store":false,"input":[{"id":"rs_good","type":"reasoning","encrypted_content":"` + valid + `","summary":[]},{"role":"user","content":"hi"}]}`)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t