@@ -94,7 +94,7 @@ describe("the openai entry in a pure-ESM consumer", () => {
9494} ) ;
9595
9696describe ( "the anthropic entry in a pure-ESM consumer" , ( ) => {
97- it ( "patches the ESM build, and the flag leaves a later CJS require unpatched " , async ( ) => {
97+ it ( "patches the ESM build, and a later CJS require is patched too " , async ( ) => {
9898 const { provider } = recordingProvider ( ) ;
9999 const enabled = await enableInstrumentations ( makeSink ( ) , provider , [ "anthropic" ] ) ;
100100 expect ( enabled ) . toContain ( "anthropic" ) ;
@@ -106,20 +106,24 @@ describe("the anthropic entry in a pure-ESM consumer", () => {
106106 } ;
107107 expect ( isWrapped ( EsmAnthropic . Messages . prototype . create ) ) . toBe ( true ) ;
108108
109- // Documents the residual gap, not an aspiration: OpenInference's global
110- // patched flag (Arize-ai/openinference#3557) means the CJS build of the
111- // same package can no longer be patched in this process. A CJS require
112- // that only happens after init() therefore stays uninstrumented. If this
113- // assertion starts failing after an OpenInference upgrade, the upstream
114- // fix landed: patch BOTH builds unconditionally and drop the
115- // cache-checking heuristic.
109+ // This used to assert `false`, documenting the gap left by OpenInference's
110+ // module-global patched flag (Arize-ai/openinference#3557): whichever build
111+ // was patched first blocked the other, so a CJS require arriving after
112+ // init() stayed uninstrumented. The guard is now scoped to the patched
113+ // class (instrumentation-anthropic >= 0.2.1, -openai >= 4.2.1), so the
114+ // require hook is free to patch the CJS build even though we already
115+ // patched the ESM one. Two DIFFERENT class objects, both wrapped.
116+ //
117+ // Keep asserting it: this is what proves the floor in package.json is
118+ // doing its job. Against an older instrumentation it fails, which is the
119+ // signal we want if the range is ever loosened.
116120 const cjs = createRequire ( import . meta. url ) ( "@anthropic-ai/sdk" ) as {
117121 default ?: { Messages : { prototype : { create : unknown } } } ;
118122 Anthropic ?: { Messages : { prototype : { create : unknown } } } ;
119123 } ;
120124 const CjsAnthropic = cjs . default ?? cjs . Anthropic ;
121125 expect ( CjsAnthropic ) . toBeDefined ( ) ;
122126 expect ( EsmAnthropic ) . not . toBe ( CjsAnthropic ) ;
123- expect ( isWrapped ( CjsAnthropic ?. Messages . prototype . create ) ) . toBe ( false ) ;
127+ expect ( isWrapped ( CjsAnthropic ?. Messages . prototype . create ) ) . toBe ( true ) ;
124128 } ) ;
125129} ) ;
0 commit comments