@@ -10,6 +10,7 @@ import {
1010 type JevCandidate ,
1111 type ResolveJevDecisionOptions ,
1212} from "../../src/combos/jev" ;
13+ import { anthropicToResponsesBody } from "../../src/claude/inbound" ;
1314import type { OcxConfig } from "../../src/types" ;
1415
1516const candidates : JevCandidate [ ] = [
@@ -138,6 +139,133 @@ describe("JEV bounded decision state", () => {
138139 expect ( JSON . stringify ( state ) ) . not . toContain ( "TOOL_MACHINE_SECRET" ) ;
139140 } ) ;
140141
142+ test . each ( [ "string" , "blocks" ] ) ( "extracts the real first Claude Code task from %s content" , shape => {
143+ const task = "Fix the parser and add a regression test." ;
144+ const reminders = [
145+ `<system-reminder>Project CLAUDE.md guidance\n${ "Follow project conventions.\n" . repeat ( 200 ) } </system-reminder>` ,
146+ "<system-reminder>User context and attribution guidance.</system-reminder>" ,
147+ ] ;
148+ const content = shape === "string"
149+ ? [ ...reminders , task ] . join ( "\n\n" )
150+ : [ ...reminders , task ] . map ( text => ( { type : "text" , text } ) ) ;
151+ const body = anthropicToResponsesBody ( {
152+ model : "claude-sonnet-4-6" ,
153+ messages : [ { role : "user" , content } ] ,
154+ } ) ;
155+ const original = JSON . stringify ( body ) ;
156+
157+ expect ( original ) . toContain ( "<system-reminder>" ) ;
158+ expect ( buildJevState ( body ) ) . toEqual ( {
159+ task,
160+ signals : { has_image : false , tool_history : false } ,
161+ step : { type : "user_turn" } ,
162+ } ) ;
163+ expect ( JSON . stringify ( body ) ) . toBe ( original ) ;
164+ } ) ;
165+
166+ test ( "strips reminders from the latest task and preserves head/tail clipping" , ( ) => {
167+ const task = `${ "h" . repeat ( 380 ) } ${ "t" . repeat ( 380 ) } ` ;
168+ const body = anthropicToResponsesBody ( {
169+ model : "claude-sonnet-4-6" ,
170+ messages : [
171+ { role : "user" , content : "An older request." } ,
172+ { role : "assistant" , content : "Ready for the next request." } ,
173+ {
174+ role : "user" ,
175+ content : `<system-reminder>${ "context" . repeat ( 40_000 ) } </system-reminder>\n${ task } ` ,
176+ } ,
177+ ] ,
178+ } ) ;
179+
180+ expect ( buildJevState ( body ) ) . toEqual ( {
181+ task : `${ "h" . repeat ( 320 ) } \n[...]\n${ "t" . repeat ( 173 ) } ` ,
182+ signals : { has_image : false , tool_history : false } ,
183+ step : { type : "user_turn" } ,
184+ previous_assistant : "Ready for the next request." ,
185+ } ) ;
186+ } ) ;
187+
188+ test ( "uses protected nesting semantics without salvaging reminder contents as a Codex goal" , ( ) => {
189+ const reminder = '<system-reminder><codex_internal_context source="goal">Quoted goal</codex_internal_context></system-reminder>' ;
190+ expect ( buildJevState ( { input : reminder } ) ) . toMatchObject ( { task : "" } ) ;
191+ expect ( buildJevState ( {
192+ input : `<codex_internal_context source="goal">Active goal${ reminder } </codex_internal_context>` ,
193+ } ) ) . toMatchObject ( { task : "Active goal" } ) ;
194+ expect ( buildJevState ( {
195+ input : "<system-reminder>Outer<system-reminder>Inner</system-reminder>Outer</system-reminder>Real task" ,
196+ } ) ) . toMatchObject ( { task : "Real task" } ) ;
197+ expect ( buildJevState ( {
198+ input : "Visible task<system-reminder>Unclosed context" ,
199+ } ) ) . toMatchObject ( { task : "Visible task" } ) ;
200+ expect ( buildJevState ( { input : "<system-reminder>Unclosed context" } ) ) . toMatchObject ( { task : "" } ) ;
201+ expect ( buildJevState ( { input : "Before<system-reminder>Context</system-reminder>after" } ) )
202+ . toMatchObject ( { task : "Before\nafter" } ) ;
203+ } ) ;
204+
205+ test ( "removes Claude Code reminders from converted assistant and tool-result tails" , ( ) => {
206+ const reminder = `<system-reminder>${ "Context only. " . repeat ( 200 ) } </system-reminder>` ;
207+ const body = anthropicToResponsesBody ( {
208+ model : "claude-sonnet-4-6" ,
209+ messages : [
210+ { role : "user" , content : "Continue the investigation." } ,
211+ {
212+ role : "assistant" ,
213+ content : [
214+ { type : "text" , text : `Visible plan${ reminder } visible follow-up${ reminder } ` } ,
215+ { type : "tool_use" , id : "call-1" , name : "Read" , input : { file : "example.ts" } } ,
216+ ] ,
217+ } ,
218+ {
219+ role : "user" ,
220+ content : [ { type : "tool_result" , tool_use_id : "call-1" , content : `Visible result${ reminder } ` } ] ,
221+ } ,
222+ ] ,
223+ } ) ;
224+
225+ expect ( buildJevState ( body ) ) . toEqual ( {
226+ task : "Continue the investigation." ,
227+ signals : { has_image : false , tool_history : true } ,
228+ step : { type : "tool_step" , last_tool_output_tail : "Visible result" , tool_call : { name : "Read" } } ,
229+ previous_assistant : "Visible plan\nvisible follow-up" ,
230+ } ) ;
231+ } ) ;
232+
233+ test ( "keeps reminder-free states byte-identical to the pre-reminder behavior" , ( ) => {
234+ const task = ` ${ "h" . repeat ( 380 ) } <example>ordinary markup</example>${ "t" . repeat ( 380 ) } ` ;
235+ const body = anthropicToResponsesBody ( {
236+ model : "claude-sonnet-4-6" ,
237+ messages : [
238+ { role : "user" , content : task } ,
239+ {
240+ role : "assistant" ,
241+ content : [
242+ { type : "text" , text : ` ${ "a" . repeat ( 300 ) } <example>plan</example> ` } ,
243+ { type : "tool_use" , id : "call-1" , name : "Read" , input : { file : "example.ts" } } ,
244+ ] ,
245+ } ,
246+ {
247+ role : "user" ,
248+ content : [ { type : "tool_result" , tool_use_id : "call-1" , content : ` ${ "z" . repeat ( 600 ) } ` } ] ,
249+ } ,
250+ ] ,
251+ } ) ;
252+ const expected = {
253+ task : `${ "h" . repeat ( 320 ) } \n[...]\n${ "t" . repeat ( 173 ) } ` ,
254+ signals : { has_image : false , tool_history : true } ,
255+ step : { type : "tool_step" , last_tool_output_tail : "z" . repeat ( 520 ) , tool_call : { name : "Read" } } ,
256+ previous_assistant : `${ "a" . repeat ( 217 ) } <example>plan</example>` ,
257+ } ;
258+
259+ expect ( JSON . stringify ( buildJevState ( body ) ) ) . toBe ( JSON . stringify ( expected ) ) ;
260+ expect ( JSON . stringify ( buildJevState ( { input : task } ) ) ) . toBe ( JSON . stringify ( {
261+ task : expected . task ,
262+ signals : { has_image : false , tool_history : false } ,
263+ step : { type : "user_turn" } ,
264+ } ) ) ;
265+ expect ( buildJevState ( { input : "<environment_context>Context</environment_context>Codex task" } ) )
266+ . toMatchObject ( { task : "Codex task" } ) ;
267+ } ) ;
268+
141269 test ( "salvages an envelope-only active goal but drops catalog-only envelopes" , ( ) => {
142270 expect ( buildJevState ( {
143271 input : '<codex_internal_context source="goal">Keep implementing JEV.</codex_internal_context>' ,
0 commit comments