55 normalizeApplyPatchDelimiters ,
66 repairFreeformToolInput ,
77 stableCanonicalPatchPrefix ,
8+ unwrapFreeformToolInput ,
89} from "../../src/responses/apply-patch-envelope" ;
910import { compileCodeModeHelperInput , resolveCodeModeHelperName } from "../../src/responses/code-mode-helper-compat" ;
1011
@@ -23,6 +24,48 @@ const CANONICAL_PATCH = `*** Begin Patch
2324*** End Patch` ;
2425
2526describe ( "apply_patch envelope repair" , ( ) => {
27+ test . each ( [
28+ [ "exec" , 'text("nested wrapper");' ] ,
29+ [ "apply_patch" , CANONICAL_PATCH ] ,
30+ ] ) ( "unwraps one extra canonical string body for %s" , ( tool , body ) => {
31+ const nested = JSON . stringify ( { input : body } ) ;
32+ const args = JSON . stringify ( { input : nested } ) ;
33+ expect ( repairFreeformToolInput ( args , tool ) ) . toBe ( body ) ;
34+ expect ( repairFreeformToolInput ( args , tool , "functions" ) ) . toBe ( body ) ;
35+ expect ( repairFreeformToolInput ( body , tool ) ) . toBe ( body ) ;
36+ expect ( repairFreeformToolInput ( nested , tool ) ) . toBe ( body ) ;
37+ // A third layer is deliberately left in place.
38+ expect ( repairFreeformToolInput ( JSON . stringify ( { input : args } ) , tool ) ) . toBe ( nested ) ;
39+ expect ( repairFreeformToolInput ( args , tool , "mcp" ) ) . toBe ( nested ) ;
40+ expect ( repairFreeformToolInput ( args , "render_diagram" ) ) . toBe ( nested ) ;
41+ } ) ;
42+
43+ test ( "the reported exec body parses only after the extra wrapper is removed" , ( ) => {
44+ const body = 'text("nested wrapper");' ;
45+ const nested = JSON . stringify ( { input : body } ) ;
46+ expect ( ( ) => new Function ( "text" , nested ) ) . toThrow ( SyntaxError ) ;
47+ const values : string [ ] = [ ] ;
48+ new Function ( "text" , repairFreeformToolInput ( JSON . stringify ( { input : nested } ) , "exec" ) ) (
49+ ( value : string ) => values . push ( value ) ,
50+ ) ;
51+ expect ( values ) . toEqual ( [ "nested wrapper" ] ) ;
52+ } ) ;
53+
54+ test . each ( [ "exec" , "apply_patch" ] ) ( "preserves noncanonical nested %s bodies" , tool => {
55+ for ( const nested of [
56+ '{"input":"text(1)","extra":true}' ,
57+ '{"input":42}' , '{"input":null}' , '{"input":{}}' ,
58+ '{"code":"text(1)"}' , '["text(1)"]' , '{"input":"incomplete' ,
59+ ] ) {
60+ expect ( repairFreeformToolInput ( JSON . stringify ( { input : nested } ) , tool ) ) . toBe ( nested ) ;
61+ }
62+ for ( const input of [ null , 42 , true , { } , [ ] ] ) {
63+ const args = JSON . stringify ( { input } ) ;
64+ expect ( repairFreeformToolInput ( args , tool ) ) . toBe ( args ) ;
65+ }
66+ expect ( unwrapFreeformToolInput ( undefined , tool ) ) . toBe ( "" ) ;
67+ } ) ;
68+
2669 test ( "repairs only the outer lines of a complete top-level apply_patch payload" , ( ) => {
2770 expect ( repairFreeformToolInput ( DECORATED_PATCH , "apply_patch" ) ) . toBe ( CANONICAL_PATCH ) ;
2871 expect ( normalizeApplyPatchDelimiters ( DECORATED_PATCH ) ) . toBe ( CANONICAL_PATCH ) ;
@@ -176,6 +219,12 @@ describe("raw exec patch envelope recognition", () => {
176219 expect ( source ) . not . toContain ( "*** Begin Patch ***" ) ;
177220 } ) ;
178221
222+ test ( "recognizes a double-wrapped patch submitted as exec" , ( ) => {
223+ const args = JSON . stringify ( { input : JSON . stringify ( { input : DECORATED_PATCH } ) } ) ;
224+ expect ( resolveCodeModeHelperName ( undefined , "exec" , args , undefined , CODE_MODE ) ) . toBe ( "apply_patch" ) ;
225+ expect ( compileCodeModeHelperInput ( args , "apply_patch" , "exec" ) ) . toContain ( JSON . stringify ( CANONICAL_PATCH ) ) ;
226+ } ) ;
227+
179228 test ( "refuses JavaScript that merely mentions an envelope" , ( ) => {
180229 const cases = [
181230 "const sample = \"tools.apply_patch({ input: patchText })\";" ,
0 commit comments