From ee9bfe733b5002f5279c6643e7a4ad2a6a1eae2b Mon Sep 17 00:00:00 2001 From: Vorflux AI Date: Wed, 15 Apr 2026 19:32:48 +0000 Subject: [PATCH] fix: use hasOwnProperty in Proxy get trap to avoid shadowing Object.prototype props The 'in' operator checks the entire prototype chain, so 'constructor' in overrides would match Object.prototype.constructor and return undefined from the overrides map. Use hasOwnProperty.call() to only match own keys (doGenerate, doStream). --- packages/tools/src/vercel/index.ts | 7 ++++++- .../tools/test/with-supermemory/unit.test.ts | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/tools/src/vercel/index.ts b/packages/tools/src/vercel/index.ts index 54bc5dcf..e87f31b6 100644 --- a/packages/tools/src/vercel/index.ts +++ b/packages/tools/src/vercel/index.ts @@ -216,7 +216,12 @@ const wrapVercelLanguageModel = ( const wrappedModel = new Proxy(model, { get(target, prop, _receiver) { - if (typeof prop === "string" && prop in overrides) { + // Use hasOwnProperty to avoid matching inherited Object.prototype props + // (e.g. 'constructor', 'toString') which would shadow them on the proxy. + if ( + typeof prop === "string" && + Object.prototype.hasOwnProperty.call(overrides, prop) + ) { return overrides[prop] } // Delegate to the original model so that `this` inside prototype diff --git a/packages/tools/test/with-supermemory/unit.test.ts b/packages/tools/test/with-supermemory/unit.test.ts index 4a7695f4..f625a95b 100644 --- a/packages/tools/test/with-supermemory/unit.test.ts +++ b/packages/tools/test/with-supermemory/unit.test.ts @@ -216,6 +216,24 @@ describe("Unit: withSupermemory", () => { }) }) + describe("Proxy override correctness", () => { + it("should not shadow Object.prototype inherited props (e.g. constructor) with undefined overrides", () => { + process.env.SUPERMEMORY_API_KEY = "test-key" + + // 'constructor', 'toString', 'hasOwnProperty' etc are in Object.prototype. + // Using `prop in overrides` (instead of hasOwnProperty) would match these + // inherited props and return undefined from the overrides map, shadowing + // the real implementations on the model/target. + const mockModel = createMockV3LanguageModel() + const wrappedModel = withSupermemory(mockModel, TEST_CONFIG.containerTag) + + // constructor should point to the model's class, not Object + expect((wrappedModel as object).constructor).toBe(MockLanguageModelV3) + // toString should be callable and not undefined + expect(typeof (wrappedModel as object).toString).toBe("function") + }) + }) + describe("Memory caching", () => { let fetchMock: ReturnType