From c91202fb4e9318b13a9f1ad00ea587f79c37ec22 Mon Sep 17 00:00:00 2001 From: awork Date: Wed, 21 Oct 2026 11:11:31 +0800 Subject: [PATCH] fix(verif): anthropic round-trip harness off-machine fixes - KP: repo-relative kernel/src/wire/anthropic.ts (was a hardcoded author-machine tmp path -> ENOENT on every other machine/runner) - both solver tests: sibling-style no-solver t.skip guard (they would otherwise throw 'no solver available' once the path resolves) - drop leftover debug scaffolding (console.log DBG blocks + dead helpers) - engine: inlineCall bound default params against innerState declared after the binding loop (TDZ ReferenceError when a defaulted param is omitted); declare innerState before param binding - simplify tautological 'fn.body ? : fn.body' to fn.body --- tests/verif-wire-anthropic-roundtrip.test.ts | 60 ++++---------------- verif/symbolic.ts | 4 +- 2 files changed, 12 insertions(+), 52 deletions(-) diff --git a/tests/verif-wire-anthropic-roundtrip.test.ts b/tests/verif-wire-anthropic-roundtrip.test.ts index dbeedc6698..c96bb7ba30 100644 --- a/tests/verif-wire-anthropic-roundtrip.test.ts +++ b/tests/verif-wire-anthropic-roundtrip.test.ts @@ -1,5 +1,6 @@ import test from "node:test"; import assert from "node:assert"; +import { resolve } from "node:path"; import { translateFunction } from "../verif/symbolic.ts"; import { newCtx, smt2Of } from "../verif/terms.ts"; import { solveSmt2 } from "../verif/solve.ts"; @@ -25,7 +26,7 @@ import type { Term } from "../verif/terms.ts"; * negative controls via UNSAT. */ -const KP = "/home/dog/tmp/bili-verif2/kernel/src/wire/anthropic.ts"; +const KP = resolve(import.meta.dirname, "../kernel/src/wire/anthropic.ts"); function makeIdPort(): (role: SymVal, ct: SymVal, text: SymVal, opts?: SymVal) => SymVal { const memo = new Map(); @@ -64,7 +65,10 @@ function makeCounterPorts(): Record }; } -test("wire-anthropic round-trip: block order + payload preserved (same-role pairs)", { timeout: 240_000 }, () => { +test("wire-anthropic round-trip: block order + payload preserved (same-role pairs)", { timeout: 240_000 }, async (t) => { + const solver = await (async () => { try { return await solveSmt2("(set-logic ALL)(check-sat)", []); } catch { return null; } })(); + if (!solver) return t.skip("no SMT solver available (set VERIF_SOLVER_CMD / VERIF_PYTHON)"); + // Pin each run's block types to constants: the symbolic-type union across // switch arms merges payloads through the tool_use arm's safeStringify // degrade ({kind:"any"} has no term to query). ORDER+payload per pinned @@ -106,7 +110,10 @@ test("wire-anthropic round-trip: block order + payload preserved (same-role pair } }); -test("wire-anthropic round-trip: thinking keeps its own message after same-role group (cross-message order)", { timeout: 240_000 }, () => { +test("wire-anthropic round-trip: thinking keeps its own message after same-role group (cross-message order)", { timeout: 240_000 }, async (t) => { + const solver = await (async () => { try { return await solveSmt2("(set-logic ALL)(check-sat)", []); } catch { return null; } })(); + if (!solver) return t.skip("no SMT solver available (set VERIF_SOLVER_CMD / VERIF_PYTHON)"); + const ctx = newCtx(); const strv = (t: unknown) => ({ kind: "str", t }) as SymVal; const TYPE1 = ctx.String.val("text"); // block-1 type pinned: this test exercises @@ -121,62 +128,15 @@ test("wire-anthropic round-trip: thinking keeps its own message after same-role const content: SymVal = { kind: "arr", elems: [blk(TYPE1, T1, C1, uid1), blk(ctx.String.val("thinking"), T2, T2, uid1)], len: ctx.Real.val(2) } as never; const body: SymVal = { kind: "obj", fields: new Map([["messages", { kind: "arr", elems: [{ kind: "obj", fields: new Map([["role", strv(ctx.String.val("user"))], ["content", content]]) } as SymVal], len: ctx.Real.val(1) } as SymVal]]) } as never; const parse = translateFunction({ file: KP, function: "anthropicToCore", params: { body }, ctx, options: { maxArrayLength: 16, portImpls: { deriveCanonicalMessageId: makeIdPort() as never, ...makeCounterPorts() } } }).exec(); - if (parse.degradations.length) console.log("PARSE2 DEGR:", JSON.stringify(parse.degradations)); assert.deepEqual(parse.degradations, [], "parse must translate cleanly"); const msgsArr = (parse.ret as { fields: Map }).fields.get("msgs") as SymVal; const rebuild = translateFunction({ file: KP, function: "coreToAnthropic", params: { messages: msgsArr, cacheControls: { kind: "undef" } as SymVal }, ctx, options: { maxArrayLength: 32, portImpls: {} } }).exec(); - if (rebuild.degradations.length) console.log("REBUILD2 DEGR:", JSON.stringify(rebuild.degradations)); - { - const kk = (v: unknown): string => { const o = v as { kind?: string; fields?: Map }; if (o?.fields) return "obj{" + [...o.fields.entries()].map(([k2, v2]) => k2 + ":" + ((v2 as { kind?: string })?.kind ?? "?")).slice(0, 8).join(",") + "}"; return String(o?.kind); }; - const m1v = (msgsArr as { elems?: unknown[] }).elems?.[1] as { kind?: string; nullUnder?: unknown; fields?: Map } | undefined; - const m1t = m1v?.fields?.get("text") as { kind?: string; nullUnder?: unknown } | undefined; - const kk2 = (v: unknown): string => { const o = v as { kind?: string; fields?: Map; nullUnder?: unknown }; return o?.fields ? "obj{" + (o.nullUnder !== undefined ? "NU " : "") + [...o.fields.keys()].slice(0, 5).join(",") + "}" : String(o?.kind); }; - const ea = (msgsArr as { elems?: unknown[] }).elems ?? []; - console.log("DBG slots=", ea.length, [...ea.slice(0, 6).map((v, i2) => i2 + ":" + kk2(v))].join(" | ")); - { // walk slot1.text term leaves - const seen = new Set(); const leaves: string[] = []; - const walk = (t: unknown): void => { - if (!t || typeof t !== "object" || seen.has(t)) return; seen.add(t); - const tt = t as { c?: unknown; op?: string; kids?: unknown[]; kind?: string }; - if (tt.kids === undefined || tt.kids.length === 0) { leaves.push(`${tt.op ?? "?"}:${JSON.stringify(tt.c)}`); return; } - for (const k3 of tt.kids) walk(k3); - }; - const s1 = ea[1] as { kind?: string; fields?: Map } | undefined; - const txt = s1?.fields?.get("text") as { kind?: string; t?: unknown } | undefined; - console.log("DBG slot1.kind=", s1?.kind, "text.kind=", txt?.kind); - if (txt?.kind === "str") { walk(txt.t); console.log("DBG slot1.text leaves=", leaves.slice(0, 12).join(" ")); } - else if (txt) { walk(txt); console.log("DBG slot1 TEXTVAL leaves=", leaves.slice(0, 12).join(" ")); } - } - } assert.deepEqual(rebuild.degradations, [], "rebuild must translate cleanly"); const outArr = rebuild.ret as { elems: SymVal[]; len: unknown }; const blocksOf = (m: SymVal): { elems: SymVal[]; len: unknown } => (m as { fields: Map }).fields.get("content") as never; const fld = (o: SymVal, k: string): { t: unknown } => ((o as { fields: Map }).fields.get(k) ?? { kind: "undef" }) as never; // block 2 is thinking → rebuild splits: out[1] exists and holds the thinking block const out1 = (outArr.elems[1] ?? { kind: "undef" }) as SymVal; - const dbg = (v: unknown): string => { const o = v as { kind?: string; fields?: Map }; return o?.fields ? "obj[" + [...o.fields.keys()].join(",") + "]" : String(o?.kind); }; - const roleOf = (v: unknown): unknown => { const o = v as { fields?: Map }; const r = o?.fields?.get("role"); return (r as { t?: { c?: unknown } } | undefined)?.t?.c ?? String((r as { kind?: string } | undefined)?.kind); }; - const blocksOf2 = (v: unknown): unknown => (v as { fields?: Map })?.fields?.get("blocks") ?? (v as { fields?: Map })?.fields?.get("content"); - const bdbg = (v: unknown): string => { const a = v as { kind?: string; elems?: unknown[]; len?: { c?: unknown } }; return a?.kind === "arr" ? `arr[${a.elems?.length}]len=${a.len?.c}` : String(a?.kind); }; - const rolec = (v: unknown): unknown => { const r = (v as { fields?: Map })?.fields?.get("role"); return (r as { t?: { c?: unknown } } | undefined)?.t?.c ?? "?"; }; - const firstType = (v: unknown): unknown => { const bl = blocksOf2(v) as { elems?: unknown[] }; const b0 = bl?.elems?.[0] as { fields?: Map } | undefined; const ty = b0?.fields?.get("type"); return (ty as { t?: { c?: unknown } } | undefined)?.t?.c ?? String((ty as { kind?: string } | undefined)?.kind); }; - const secondType = (v: unknown): unknown => { const bl = blocksOf2(v) as { elems?: unknown[] }; const b1 = bl?.elems?.[1] as { fields?: Map } | undefined; const ty = b1?.fields?.get("type"); return (ty as { t?: { c?: unknown } } | undefined)?.t?.c ?? String((ty as { kind?: string } | undefined)?.kind); }; - const bl1 = blocksOf2(outArr.elems[1]) as { elems?: unknown[] }; - const th = (bl1?.elems?.[0] as { fields?: Map })?.fields?.get("thinking"); - const tx = (bl1?.elems?.[0] as { fields?: Map })?.fields?.get("text"); - console.log("DBG out1.slot0.thinking=", JSON.stringify((th as { kind?: string } | undefined)?.kind), "text=", JSON.stringify((tx as { kind?: string } | undefined)?.kind)); - { // rebuild out1 structure - console.log("DBG rb.out elems=", outArr.elems?.length, [...(outArr.elems ?? []).map((v: unknown, i3: number) => { const o = v as { kind?: string; fields?: Map }; return i3 + ":" + (o?.fields ? [...o.fields.keys()].join("/") : String(o?.kind)); })].join(" | ")); - const o1 = (outArr.elems[1] ?? { kind: "undef" }) as { kind?: string; fields?: Map }; - console.log("DBG rb.out1 kind=", o1?.kind, "fields=", o1?.fields ? [...o1.fields.keys()].join(",") : "-"); - const bl2 = o1?.fields?.get("blocks") ?? o1?.fields?.get("content"); - const blA = bl2 as { elems?: unknown[]; len?: { c?: unknown } }; - console.log("DBG rb.out1.blocks kind=", (bl2 as { kind?: string })?.kind, "elems=", blA?.elems?.length, "lenC=", blA?.len?.c); - const b0 = blA?.elems?.[0] as { kind?: string; fields?: Map } | undefined; - console.log("DBG rb.out1.bl0 kind=", b0?.kind, b0?.fields ? [...b0.fields.keys()].join(",") : "-"); - const thk = b0?.fields?.get("thinking") as { kind?: string; t?: { c?: unknown } } | undefined; - console.log("DBG rb.bl0.thinking kind=", thk?.kind, "c=", thk?.t?.c); - } const thinkT2 = blocksOf(out1).elems[0] !== undefined ? fld(blocksOf(out1).elems[0]!, "thinking").t : T2; const q = solveSmt2(smt2Of(ctx, [...parse.assumptions as never[], ...rebuild.assumptions as never[], TYPE1.eq(ctx.String.val("text")).or(TYPE1.eq(ctx.String.val("tool_result")) as never), diff --git a/verif/symbolic.ts b/verif/symbolic.ts index aa3f58aa31..f9330b7928 100644 --- a/verif/symbolic.ts +++ b/verif/symbolic.ts @@ -2186,6 +2186,7 @@ const mergeBranch = ( if (defInfo) moduleStack.push(defInfo); try { const inner: Frame = { vars: new Map(frame.vars) }; // lexical closure + const innerState: RunState = runState0(); // bind parameters (earlier params visible to later default exprs) fn.parameters.forEach((prm, pi) => { const pv: SymVal | undefined = args[pi] ?? (prm.initializer ? evalExpr(prm.initializer, inner, innerState, depth + 1) : undefined); @@ -2196,8 +2197,7 @@ const mergeBranch = ( inner.vars.set(el.name.text, elementAccess(pd, { kind: "num", t: num(ctx, j) }, el, st)); }); }); - const innerState: RunState = runState0(); - const body = ts.isFunctionDeclaration(fn) ? fn.body : fn.body; + const body = fn.body; if (!body) return { kind: "any" }; if (ts.isBlock(body)) { execStmt(body, inner, innerState, depth + 1);