@@ -98,7 +98,7 @@ describe("transformRequestBody — cache_control (Anthropic)", () => {
9898 expect ( parsed . messages [ 0 ] . content [ 0 ] . cache_control ) . toEqual ( { type : "ephemeral" } ) ;
9999 } ) ;
100100
101- it ( "does NOT overwrite existing cache_control on last block " , ( ) => {
101+ it ( "clears a non-canonical existing cache_control and re-marks with the canonical ephemeral shape (zsi+Fsi) " , ( ) => {
102102 const existing = { type : "ephemeral" , ttl : "1h" } ;
103103 const body = JSON . stringify ( {
104104 messages : [
@@ -107,7 +107,32 @@ describe("transformRequestBody — cache_control (Anthropic)", () => {
107107 } ) ;
108108 const out = transformRequestBody ( body , { format : "anthropic" } ) ;
109109 const parsed = JSON . parse ( out as string ) ;
110- expect ( parsed . messages [ 0 ] . content [ 0 ] . cache_control ) . toEqual ( existing ) ;
110+ expect ( parsed . messages [ 0 ] . content [ 0 ] . cache_control ) . toEqual ( { type : "ephemeral" } ) ;
111+ } ) ;
112+
113+ it ( "strips client cache_control on earlier non-system messages, keeps only the last-message marker" , ( ) => {
114+ const body = JSON . stringify ( {
115+ messages : [
116+ { role : "user" , content : [ { type : "text" , text : "early" , cache_control : { type : "ephemeral" } } ] } ,
117+ { role : "assistant" , content : [ { type : "text" , text : "reply" } ] } ,
118+ ] ,
119+ } ) ;
120+ const out = transformRequestBody ( body , { format : "anthropic" } ) ;
121+ const parsed = JSON . parse ( out as string ) ;
122+ expect ( parsed . messages [ 0 ] . content [ 0 ] . cache_control ) . toBeUndefined ( ) ;
123+ expect ( parsed . messages [ 1 ] . content [ 0 ] . cache_control ) . toEqual ( { type : "ephemeral" } ) ;
124+ } ) ;
125+
126+ it ( "is idempotent: an already-marked body is returned without modification" , ( ) => {
127+ const body = JSON . stringify ( {
128+ messages : [
129+ { role : "user" , content : [ { type : "text" , text : "early" } ] } ,
130+ { role : "assistant" , content : [ { type : "text" , text : "reply" , cache_control : { type : "ephemeral" } } ] } ,
131+ ] ,
132+ } ) ;
133+ const out = transformRequestBody ( body , { format : "anthropic" } ) ;
134+ // Already our target shape → the original string comes back untouched
135+ expect ( out ) . toBe ( body ) ;
111136 } ) ;
112137
113138 it ( "skips system messages — finds last non-system" , ( ) => {
@@ -164,7 +189,7 @@ describe("transformRequestBody — combined behavior", () => {
164189} ) ;
165190
166191describe ( "transformRequestBody — start-plan system (Anthropic)" , ( ) => {
167- it ( "prepends current ZCode system messages as three cacheable blocks plus currentModel block" , ( ) => {
192+ it ( "prepends three official blocks with the currentModel line merged into the Environment block (T9o) " , ( ) => {
168193 const body = JSON . stringify ( {
169194 model : "glm-5.2" ,
170195 max_tokens : 1024 ,
@@ -174,7 +199,7 @@ describe("transformRequestBody — start-plan system (Anthropic)", () => {
174199 const out = transformRequestBody ( body , { format : "anthropic" , startPlan : true } ) ;
175200 const parsed = JSON . parse ( out as string ) ;
176201
177- expect ( parsed . system ) . toHaveLength ( 4 ) ;
202+ expect ( parsed . system ) . toHaveLength ( 3 ) ;
178203 expect ( parsed . system [ 0 ] ) . toEqual ( {
179204 type : "text" ,
180205 text : "You are ZCode, an interactive coding agent" ,
@@ -186,12 +211,10 @@ describe("transformRequestBody — start-plan system (Anthropic)", () => {
186211 expect ( parsed . system [ 2 ] . text ) . toContain ( "You have been invoked in the following environment:" ) ;
187212 expect ( parsed . system [ 2 ] . text ) . toContain ( "- Is a git repository: no" ) ;
188213 expect ( parsed . system [ 2 ] . text ) . not . toContain ( "- Is a git repository: unknown" ) ;
214+ // The powered-by line lives INSIDE the Environment text as its last line,
215+ // not as a separate 4th block (bundle 3.11.2 T9o shape).
216+ expect ( parsed . system [ 2 ] . text . endsWith ( "\n- You are powered by the model named glm-5.2." ) ) . toBe ( true ) ;
189217 expect ( parsed . system [ 2 ] . cache_control ) . toEqual ( { type : "ephemeral" } ) ;
190- expect ( parsed . system [ 3 ] ) . toEqual ( {
191- type : "text" ,
192- text : "- You are powered by the model named glm-5.2." ,
193- cache_control : { type : "ephemeral" } ,
194- } ) ;
195218 } ) ;
196219
197220 it ( "omits the currentModel block when body.model is missing" , ( ) => {
@@ -241,13 +264,13 @@ describe("transformRequestBody — start-plan system (Anthropic)", () => {
241264 const out = transformRequestBody ( body , { format : "anthropic" , startPlan : true } ) ;
242265 const parsed = JSON . parse ( out as string ) ;
243266
244- expect ( parsed . system ) . toHaveLength ( 5 ) ;
245- expect ( parsed . system [ 4 ] ) . toEqual ( { type : "text" , text : "User rule" } ) ;
267+ expect ( parsed . system ) . toHaveLength ( 4 ) ;
268+ expect ( parsed . system [ 3 ] ) . toEqual ( { type : "text" , text : "User rule" } ) ;
246269 } ) ;
247270} ) ;
248271
249272describe ( "transformRequestBody — start-plan system (OpenAI)" , ( ) => {
250- it ( "prepends current ZCode system messages (incl. currentModel) before OpenAI chat messages" , ( ) => {
273+ it ( "prepends current ZCode system messages (currentModel line inside the Environment message ) before OpenAI chat messages" , ( ) => {
251274 const body = JSON . stringify ( {
252275 model : "glm-5.2" ,
253276 messages : [ { role : "user" , content : "hi" } ] ,
@@ -265,11 +288,8 @@ describe("transformRequestBody — start-plan system (OpenAI)", () => {
265288 expect ( parsed . messages [ 2 ] . role ) . toBe ( "system" ) ;
266289 expect ( parsed . messages [ 2 ] . content ) . toContain ( "You have been invoked in the following environment:" ) ;
267290 expect ( parsed . messages [ 2 ] . content ) . toContain ( "- Is a git repository: no" ) ;
268- expect ( parsed . messages [ 3 ] ) . toEqual ( {
269- role : "system" ,
270- content : "- You are powered by the model named glm-5.2." ,
271- } ) ;
272- expect ( parsed . messages [ 4 ] ) . toEqual ( { role : "user" , content : "hi" } ) ;
291+ expect ( parsed . messages [ 2 ] . content ) . toContain ( "- You are powered by the model named glm-5.2." ) ;
292+ expect ( parsed . messages [ 3 ] ) . toEqual ( { role : "user" , content : "hi" } ) ;
273293 } ) ;
274294
275295 it ( "omits the currentModel system message when body.model is missing (OpenAI)" , ( ) => {
0 commit comments