From cbf79d6e2d4e3529e51e480c450b7b8cf55428c1 Mon Sep 17 00:00:00 2001 From: TejasLamba2006 Date: Tue, 24 Nov 2026 22:14:07 +0530 Subject: [PATCH] fix(ts): preserve literal __proto__ fields and normalize nested arrays in stripNulls Emit a computed property name ["__proto__"] in createModuleTextFromProgram so own __proto__ data properties are preserved instead of acting as prototype setters, with behavioral tests executing generated modules for string, object, and null values alongside interpreter parity checks. Keep value synchronized with the filtered array in stripNulls before recursing so nulls inside nested arrays are removed in the returned structure, with regression tests using createTypeScriptJsonValidator (attemptRepair = false, closed in finally). --- typescript/src/ts/program.ts | 2 +- typescript/src/typechat.ts | 4 +-- typescript/test/failure.test.ts | 47 +++++++++++++++++++++++++++++++++ typescript/test/program.test.ts | 30 +++++++++++++++++++++ 4 files changed, 80 insertions(+), 3 deletions(-) diff --git a/typescript/src/ts/program.ts b/typescript/src/ts/program.ts index b771c653..ea1a70d9 100644 --- a/typescript/src/ts/program.ts +++ b/typescript/src/ts/program.ts @@ -137,7 +137,7 @@ export function createModuleTextFromProgram(jsonObject: object): Result return `[${arrayToString(obj)}]`; } else { - return `{ ${Object.keys(obj).map(key => `${JSON.stringify(key)}: ${exprToString(obj[key])}`).join(", ")} }`; + return `{ ${Object.keys(obj).map(key => `${key === "__proto__" ? '["__proto__"]' : JSON.stringify(key)}: ${exprToString(obj[key])}`).join(", ")} }`; } hasError = true; return ""; diff --git a/typescript/src/typechat.ts b/typescript/src/typechat.ts index 66b082f1..9b3f191b 100644 --- a/typescript/src/typechat.ts +++ b/typescript/src/typechat.ts @@ -206,14 +206,14 @@ export function createJsonTranslator(model: TypeChatLanguageMo function stripNulls(obj: any) { let keysToDelete: string[] | undefined; for (const k in obj) { - const value = obj[k]; + let value = obj[k]; if (value === null) { (keysToDelete ??= []).push(k); } else { if (Array.isArray(value)) { if (value.some(x => x === null)) { - obj[k] = value.filter(x => x !== null); + value = obj[k] = value.filter(x => x !== null); } } if (typeof value === "object") { diff --git a/typescript/test/failure.test.ts b/typescript/test/failure.test.ts index 5a5666d1..ebda2b7f 100644 --- a/typescript/test/failure.test.ts +++ b/typescript/test/failure.test.ts @@ -250,6 +250,53 @@ describe("translation failure context", () => { }); }); +describe("stripNulls array normalization", () => { + for (const { groups, expected } of [ + { groups: [null, [null, 1]], expected: [[1]] }, + { groups: [[null, 1], null, [null, null]], expected: [[1], []] } + ]) { + it(`strips nulls from nested arrays ${JSON.stringify(groups)}`, async () => { + const validator = createTypeScriptJsonValidator<{ groups: number[][] }>( + "export interface Groups { groups: number[][]; }", "Groups" + ); + try { + const model: TypeChatLanguageModel = { + complete: async () => success(JSON.stringify({ groups })) + }; + const translator = createJsonTranslator(model, validator); + translator.stripNulls = true; + translator.attemptRepair = false; + const result = await translator.translate("request"); + assert.ok(result.success); + assert.deepEqual(result.data, { groups: expected }); + } + finally { + validator.close(); + } + }); + } + + it("strips nulls from flat arrays", async () => { + const validator = createTypeScriptJsonValidator<{ items: number[] }>( + "export interface Items { items: number[]; }", "Items" + ); + try { + const model: TypeChatLanguageModel = { + complete: async () => success('{"items":[null,1,null,2]}') + }; + const translator = createJsonTranslator(model, validator); + translator.stripNulls = true; + translator.attemptRepair = false; + const result = await translator.translate("request"); + assert.ok(result.success); + assert.deepEqual(result.data, { items: [1, 2] }); + } + finally { + validator.close(); + } + }); +}); + describe("repair failure context", () => { it("preserves a successful repair and the existing repair prompt", async () => { const first = '{"value":"invalid"}'; diff --git a/typescript/test/program.test.ts b/typescript/test/program.test.ts index 5eaa8183..39af2095 100644 --- a/typescript/test/program.test.ts +++ b/typescript/test/program.test.ts @@ -1,5 +1,7 @@ import { describe, it } from "node:test"; import assert from "node:assert/strict"; +import { stripTypeScriptTypes } from "node:module"; +import { runInNewContext } from "node:vm"; import { createModuleTextFromProgram, createProgramTranslator, @@ -186,6 +188,34 @@ describe("hasOwnProperty shadowing in parsed programs", () => { }); }); +describe("literal __proto__ property preservation in generated modules", () => { + for (const value of ["kept", { label: "kept" }, null]) { + it(`preserves an own __proto__ data property with value ${JSON.stringify(value)}`, async () => { + const input = JSON.parse(`{"__proto__": ${JSON.stringify(value)}, "value": 7}`); + const program: Program = { "@steps": [{ "@func": "identity", "@args": [input] }] }; + const moduleResult = createModuleTextFromProgram(program); + assert.ok(moduleResult.success); + assert.match(moduleResult.data, /\["__proto__"\]: /); + assert.doesNotMatch(moduleResult.data, /(? unknown }) => unknown; + const generatedResult = generatedProgram({ identity: arg => arg }); + + assert.ok(typeof generatedResult === "object" && generatedResult !== null); + assert.ok(Object.prototype.hasOwnProperty.call(generatedResult, "__proto__")); + assert.equal(JSON.stringify(generatedResult), JSON.stringify(input)); + + const interpretedResult = await evaluateJsonProgram(program, async (_func, args) => args[0]); + assert.ok(typeof interpretedResult === "object" && interpretedResult !== null); + assert.ok(Object.prototype.hasOwnProperty.call(interpretedResult, "__proto__")); + assert.equal(JSON.stringify(generatedResult), JSON.stringify(interpretedResult)); + }); + } +}); + describe("@func name validation", () => { const maliciousProgram: Program = { "@steps": [