diff --git a/tests/verif-conflict-watch.test.ts b/tests/verif-conflict-watch.test.ts new file mode 100644 index 0000000000..64a2c07338 --- /dev/null +++ b/tests/verif-conflict-watch.test.ts @@ -0,0 +1,252 @@ +// Conflict-watch discipline judge (#3806 family / #2709 / #3815). +// +// Two properties, solver-checked over the REAL translated code of +// src/conflict-watch.ts: +// +// J1 — witness gate (dshNativeCompactionWitness): the #2709 attribution +// ("this rewrite is the client's own native compaction") may fire with a +// non-zero refusal counter ONLY when the rewrite evidence shows a +// SHRINKING history (incoming < 0.75 × knownBefore). A restart re-key +// (#3806: 2793/2814 = 99%, 395/389 = 101% GREW) must stay in the quiet +// unannounced-rewrite tier even with a stale refusal counter. This is +// "attribution must accompany shrinkage" as a solver-certified absence: +// NO session shape fires the witness while the fingerprint is flat. +// Non-vacuity is pinned too: real #2709 landings (320/771, 427/875) DO +// fire, boundary 749/1000 fires / 750/1000 does not, and without a +// fingerprint (the fold-reconcile call site) the counter alone decides. +// +// J2 — banner tier discipline (formatConflictSection): the one-compressor +// command ("Keep exactly ONE compressor ...") may appear ONLY when a +// CONFIRMED foreign third-party event exists (non-suspected, +// non-sibling, non-display-only) AND it is within the active window. +// Arbitrary symbolic kinds/timestamps over concrete detail shapes; the +// #3806 regression shape (only unannounced-rewrite + inferred landing +// absent) must produce the softer "cause not identified"/host-side +// framing, never the removal command. +// +// No-cheat rules per verif/README.md: real entry functions only, general +// invariants only, replay mandatory, ports only at public seams, negative +// controls mandatory. + +import test from "node:test"; +import assert from "node:assert/strict"; +import { translateFunction, type SymVal } from "../verif/symbolic.ts"; +import { newCtx, smt2Of, type Context, type Term } from "../verif/terms.ts"; +import { solveSmt2 } from "../verif/solve.ts"; +import { dshNativeCompactionWitness } from "../src/conflict-watch.ts"; + +const CW = new URL("../src/conflict-watch.ts", import.meta.url).pathname; + +// ---------------------------------------------------------------- helpers -- +function haveSolver(): boolean { + try { + const r = solveSmt2("(check-sat)", []); + return r.status === "sat" || r.status === "unsat"; + } catch { + return false; + } +} + +const numv = (t: Term): SymVal => ({ kind: "num", t }) as SymVal; +const strv = (t: Term): SymVal => ({ kind: "str", t }) as SymVal; +const objv = (f: [string, SymVal][]): SymVal => + ({ kind: "obj", fields: new Map(f) }) as SymVal; + +type ExecOut = { ret: SymVal | null; thrown: { kind: string } | null; assumptions: unknown[]; degradations: string[]; ctx: Context }; + +function runWitness(ctx: Context, session: SymVal, fingerprint: SymVal): ExecOut { + const out = translateFunction({ + file: CW, function: "dshNativeCompactionWitness", + params: { session, fingerprint }, ctx: ctx as never, options: {}, + }).exec(); + return out as unknown as ExecOut; +} + +// ret may be undefined on guard paths — undefUnder carries the union. +function firedGT(ctx: Context, ret: SymVal | null, x: number): Term { + if (!ret || ret.kind !== "num") return ctx.Bool.val(false) as never; + const uu = (ret as { undefUnder?: Term }).undefUnder; + const gt = (ret as unknown as { t: Term }).t.gt(ctx.Real.val(x)) as unknown as Term; + return (uu ? (uu.not() as unknown as Term).and(gt) : gt) as never; +} + +// session shape: { metadata: { pluginAgent, dshCompactionRefusals } } +function mkSession(ctx: Context, pa: Term, ref: Term): SymVal { + return objv([["metadata", objv([ + ["pluginAgent", strv(pa)], + ["dshCompactionRefusals", numv(ref)], + ])]]); +} + +test("conflict-watch J1: witness fires only on shrinking history (arbitrary sessions)", { timeout: 240_000 }, (t) => { + if (!haveSolver()) return t.skip("no solver (set VERIF_PYTHON)"); + + const ctx = newCtx(); + const PA = ctx.String.const("PA"), REF = ctx.Real.const("REF"); + const INC = ctx.Real.const("INC"), KN = ctx.Real.const("KN"); + const out = runWitness(ctx, mkSession(ctx, PA, REF), objv([ + ["incomingTotal", numv(INC)], ["knownBefore", numv(KN)], + ])); + assert.deepEqual(out.degradations, []); + assert.equal(out.ret!.kind, "num", "witness returns a counter (symbolic ite of num/undef merged)"); + + const fired = firedGT(ctx, out.ret, 0); + const flat = INC.ge(KN.mul(ctx.Real.val(0.75))); // incoming >= 75% of known + const known = KN.gt(ctx.Real.val(0)); + + // Q1 discipline: firing with a flat-or-grown fingerprint is unreachable. + const q1 = solveSmt2(smt2Of(ctx, [...(out.assumptions as Term[]), fired, known, flat] as never[], [] as never), []); + assert.equal(q1.status, "unsat", "witness must not attribute a flat resend to native compaction (#3806 restart re-key)"); + + // Q2 non-vacuity: a real landing shape fires (and returns the counter). + const q2 = solveSmt2(smt2Of(ctx, [...(out.assumptions as Term[]), fired, + PA.eq(ctx.String.val("dsh")), REF.eq(ctx.Real.val(17)), KN.eq(ctx.Real.val(771)), INC.eq(ctx.Real.val(320))] as never[], [] as never), []); + assert.equal(q2.status, "sat", "#2709 landing 320/771 must fire"); + + // Q3 gate: never fires for non-dsh agents or zero/negative refusals. + const q3 = solveSmt2(smt2Of(ctx, [...(out.assumptions as Term[]), fired, + PA.neq(ctx.String.val("dsh")).or(REF.le(ctx.Real.val(0)))] as never[], [] as never), []); + assert.equal(q3.status, "unsat", "witness gated on pluginAgent=dsh AND refusals>0"); + + // Q4 no-fingerprint call site (fold-reconcile): counter alone decides. + const outNF = runWitness(ctx, mkSession(ctx, PA, REF), { kind: "undef" } as SymVal); + const firedNF = firedGT(ctx, outNF.ret, 0); + const q4 = solveSmt2(smt2Of(ctx, [...(outNF.assumptions as Term[]), firedNF, + PA.eq(ctx.String.val("dsh")), REF.eq(ctx.Real.val(3))] as never[], [] as never), []); + assert.equal(q4.status, "sat", "without fingerprint the counter alone decides (documented call-site contract)"); +}); + +test("conflict-watch J1 replay: real code on the #2709/#3806 calibration shapes", () => { + + const md = (agent: string, refusals: number) => ({ pluginAgent: agent, dshCompactionRefusals: refusals }); + const fp = (i: number, k: number) => ({ incomingTotal: i, knownBefore: k }); + + // #2709 real landings — shrinking, MUST attribute (returns the counter). + assert.equal(dshNativeCompactionWitness({ metadata: md("dsh", 17) } as never, fp(320, 771)), 17); + assert.equal(dshNativeCompactionWitness({ metadata: md("dsh", 17) } as never, fp(427, 875)), 17); + // boundary: 74.9% fires, 75.0% does not. + assert.equal(dshNativeCompactionWitness({ metadata: md("dsh", 5) } as never, fp(749, 1000)), 5); + assert.equal(dshNativeCompactionWitness({ metadata: md("dsh", 5) } as never, fp(750, 1000)), undefined); + // #3806 restart re-keys — flat/grown, MUST NOT attribute. + assert.equal(dshNativeCompactionWitness({ metadata: md("dsh", 375) } as never, fp(2793, 2814)), undefined); + assert.equal(dshNativeCompactionWitness({ metadata: md("dsh", 31) } as never, fp(395, 389)), undefined); + // gates: wrong agent / zero counter / missing fingerprint. + assert.equal(dshNativeCompactionWitness({ metadata: md("pi", 9) } as never, fp(100, 800)), undefined); + assert.equal(dshNativeCompactionWitness({ metadata: md("dsh", 0) } as never, fp(100, 800)), undefined); + assert.equal(dshNativeCompactionWitness({ metadata: md("dsh", 4) } as never, undefined), 4); +}); + +// ------------------------------------------------------------------- J2 ---- +import { isSiblingConflictDetail, isDisplayOnlyConflictDetail } from "../src/thirdparty-scan.ts"; +import { formatConflictSection, CONFLICT_ACTIVE_WINDOW_MS } from "../src/conflict-watch.ts"; + +const D_SIBLING = "dsh: billion-context-pi (native)"; // neutral: bili's own sibling +const D_SUSPECT = "x: acme-context (mcp) [suspected]"; // name-only guess +const D_FOREIGN = "m: real-comp (mcp)"; // plain foreign compressor + +function mkEvent(ctx2: Context, at: Term, kind: Term, detail: string): SymVal { + return objv([["at", numv(at)], ["kind", strv(kind)], ["detail", strv(ctx2.String.val(detail))]]); +} + +test("conflict-watch J2: one-compressor command only with confirmed ACTIVE foreign evidence (arbitrary kinds/ages)", { timeout: 240_000 }, (t) => { + if (!haveSolver()) return t.skip("no solver (set VERIF_PYTHON)"); + + const ctx = newCtx(); + const K1 = ctx.String.const("K1"), K2 = ctx.String.const("K2"), K3 = ctx.String.const("K3"); + const A1 = ctx.Real.const("A1"), A2 = ctx.Real.const("A2"), A3 = ctx.Real.const("A3"); + const NOW = ctx.Real.val(1_000_000_000_000); + const events = { kind: "arr", elems: [ + mkEvent(ctx, A1, K1, D_SIBLING), mkEvent(ctx, A2, K2, D_SUSPECT), mkEvent(ctx, A3, K3, D_FOREIGN), + ], len: ctx.Real.val(3) } as SymVal; + + const out = translateFunction({ + file: CW, function: "formatConflictSection", + params: { events, now: numv(NOW) }, ctx: ctx as never, + options: { + portImpls: { + // concrete-string display-tier classifiers (regex exec on symbolic + // strings is the documented weak spot; details are concrete here) + isSiblingConflictDetail: (sv) => ({ kind: "bool", t: ctx.Bool.val(isSiblingConflictDetail(String((sv as { t: { c?: string } }).t.c))) }), + isDisplayOnlyConflictDetail: (sv) => ({ kind: "bool", t: ctx.Bool.val(isDisplayOnlyConflictDetail(String((sv as { t: { c?: string } }).t.c))) }), + // timestamps are display-only — opaque strings suffice + Date: () => ({ kind: "obj", fields: new Map() }), + __method_toISOString: () => ({ kind: "str", t: ctx.String.val("1970-01-01T00:00:00.000Z") }), + }, + }, + }).exec() as unknown as ExecOut; + assert.deepEqual(out.degradations, []); + assert.equal(out.ret!.kind, "arr"); + + const lines = (out.ret as unknown as { elems: SymVal[]; len: Term }).elems; + const lenT = (out.ret as unknown as { len: Term }).len; + const CMD = "Keep exactly ONE compressor"; + let hasCmd = ctx.Bool.val(false) as unknown as Term; + for (let i = 0; i < lines.length; i++) { + const li = lines[i]!; + if (li.kind !== "str") continue; + const contain = (li.t as Term).contains(ctx.String.val(CMD)) as unknown as Term; + hasCmd = hasCmd.or(ctx.If(lenT.gt(ctx.Real.val(i)), contain, ctx.Bool.val(false))) as unknown as Term; + } + // domain: kind is the ConflictKind union — a free String could otherwise + // model as containing the command phrase itself. + const KINDS = ["third-party-plugin", "unannounced-rewrite", "orphan-reap", "native-compaction", "native-compaction-inferred", "identity-orphan"]; + const kindDomain = (k: Term): Term => KINDS.reduce((acc: Term, v: string) => acc.or(k.eq(ctx.String.val(v))) as unknown as Term, ctx.Bool.val(false) as unknown as Term); + const dom = [kindDomain(K1), kindDomain(K2), kindDomain(K3)]; + + // only the plain-detail event can be foreign-confirmed (sibling and + // [suspected] details classify neutral/unverified by construction) + const foreignConfirmed3 = K3.eq(ctx.String.val("third-party-plugin")); + const active3 = NOW.sub(A3).le(ctx.Real.val(CONFLICT_ACTIVE_WINDOW_MS)); + + // Q1 discipline: command without a confirmable foreign event is unreachable. + const noForeign = K1.neq(ctx.String.val("third-party-plugin")).and(K2.neq(ctx.String.val("third-party-plugin"))).and(K3.neq(ctx.String.val("third-party-plugin"))); + const q1 = solveSmt2(smt2Of(ctx, [...(out.assumptions as Term[]), ...dom, hasCmd, noForeign] as never[], [] as never), []); + assert.equal(q1.status, "unsat", "the removal command requires confirmed foreign evidence (#2545 tiering)"); + + // Q2 active gate: a confirmed-but-historical (older than the window) event + // must produce the softer verify-then-clear wording, not the command. + // activeConfirmed ranges over ALL confirmed kinds (#2709 nci counts), so + // staleness must pin every event, not just the foreign one. + const allStale = NOW.sub(A1).gt(ctx.Real.val(CONFLICT_ACTIVE_WINDOW_MS)).and(NOW.sub(A2).gt(ctx.Real.val(CONFLICT_ACTIVE_WINDOW_MS))).and(active3.not()); + const q2 = solveSmt2(smt2Of(ctx, [...(out.assumptions as Term[]), ...dom, hasCmd, foreignConfirmed3, allStale] as never[], [] as never), []); + assert.equal(q2.status, "unsat", "stale confirmed evidence must not command removal (#2102 active window)"); + + // Q3 non-vacuity: the command IS reachable with fresh confirmed evidence. + const q3 = solveSmt2(smt2Of(ctx, [...(out.assumptions as Term[]), ...dom, hasCmd] as never[], ["K3", "A3"] as never), ["K3", "A3"]); + assert.equal(q3.status, "sat"); + const w: Record = {}; + ["K3", "A3"].forEach((nm, i) => { w[nm] = q3.values[i] ?? ""; }); + assert.equal(w.K3, '"third-party-plugin"', "witness: the plain-detail event is the confirmed foreign one"); +}); + +test("conflict-watch J2 replay: banner tiering on the #3806/#2709/#2545 ledger shapes", () => { + const NOW = 1_000_000_000_000; + const ev = (kind: string, detail: string, at = NOW): { at: number; kind: "third-party-plugin"; detail: string } => ({ at, kind, detail } as { at: number; kind: "third-party-plugin"; detail: string }); + const adviceOf = (events: { at: number; kind: string; detail: string }[]): string => + formatConflictSection(events as never, NOW).join("\n"); + + // #3806 regression shape: ONLY unannounced rewrites (restart re-key) — + // "cause not identified", never the removal command. + const unannounced = adviceOf([ev("unannounced-rewrite", "restart re-keyed 2793/2814 ids")]); + assert.ok(!unannounced.includes("Keep exactly ONE compressor"), "unconfirmed signals must not command removal"); + assert.ok(unannounced.includes("cause is not confirmed"), "unannounced-rewrite tier wording"); + + // #2709 inferred landing: host-side explanation, no plugin hunt. + const inferred = adviceOf([ev("native-compaction-inferred", "witness: refusals=17, 320/771")]); + assert.ok(!inferred.includes("Keep exactly ONE compressor")); + assert.ok(inferred.includes("do not go hunting"), "inferred landing gets the host-side branch"); + + // confirmed foreign, fresh: the command IS given. + const foreignFresh = adviceOf([ev("third-party-plugin", D_FOREIGN, NOW - 1000)]); + assert.ok(foreignFresh.includes("Keep exactly ONE compressor"), "fresh confirmed foreign evidence commands removal"); + assert.ok(foreignFresh.includes(D_FOREIGN), "the confirmed event is shown"); + + // confirmed foreign but older than the window: softer wording. + const foreignStale = adviceOf([ev("third-party-plugin", D_FOREIGN, NOW - CONFLICT_ACTIVE_WINDOW_MS - 1)]); + assert.ok(!foreignStale.includes("Keep exactly ONE compressor"), "stale confirmed evidence softens"); + + // neutral-only ledger: stand-down wording, no command. + const neutral = adviceOf([ev("third-party-plugin", D_SIBLING, NOW)]); + assert.ok(!neutral.includes("Keep exactly ONE compressor")); + assert.ok(neutral.includes("no second compressor is active"), "sibling ledger stands down"); +}); diff --git a/tests/verif-snapshot-render.test.ts b/tests/verif-snapshot-render.test.ts new file mode 100644 index 0000000000..ad54f16410 --- /dev/null +++ b/tests/verif-snapshot-render.test.ts @@ -0,0 +1,244 @@ +/** + * verif judge J3 — snapshot-render buildView discipline (#3756 / #3812). + * + * No-cheat rules (verif/README.md): the translated entry function is the REAL + * module-internal buildView from src/server/snapshot-render.ts (resolved via + * the module graph, never re-typed); the judged properties are GENERAL + * contracts of the view, not statements about specific bug lines; every + * symbolic result is answered by the solver over the full formula; the replay + * test runs the real exported maybeSnapshotView against real JS objects. + * + * Contracts judged (symbolic, arbitrary ids/texts/seqs in the input model): + * C1 tail preservation — when buildView returns ok, every client tail + * message (the suffix after the join point the algorithm itself picked) + * must appear in the rendered view. Violation SAT would mean a silent + * drop of live history. + * C2 anchor presence — under ok with an active block whose fold-time ids + * resolve against the transcript, the view contains the synthesized + * acp_summary_ carrier. Violation SAT would mean the summary + * carrier disappears while its covered rows are suppressed. + * C3 non-vacuity — ok=true must be reachable in the model (the success + * path exists; C1/C2 are not proven on an empty set). + * C4 anchor-id collision sanity — the summary carrier id must equal + * acp_summary_ exactly (no free string leaks into it). + * + * The input model keeps rows/client symbolic (ids, texts, roles, contentType) + * with the minimal domain constraints the real system guarantees: non-empty + * distinct row ids for the chainable prefix, block ids drawn from the rows + * (anchored shape), ≥2 client prefix ids shared with rows (seam length ≥ + * MIN_SEAM_RUN), numeric seqs. + * + * Replay (real JS, exported maybeSnapshotView): a fake store seeded with + * concrete rows exercises (a) the success shape end-to-end, (b) the #3812 + * lone-duplicate degrade (seam-too-short → legacy fallback, reason recorded), + * (c) block-unanchored degrade. + */ +import test from "node:test"; +import assert from "node:assert/strict"; +import { translateFunction, type SymVal } from "../verif/symbolic.ts"; +import { newCtx, smt2Of, type Term } from "../verif/terms.ts"; +import { solveSmt2 } from "../verif/solve.ts"; +import { maybeSnapshotView } from "../src/server/snapshot-render.ts"; +import { _setStoreForTest, type SessionStore } from "../src/persist.ts"; +import type { BiliMessage } from "acp-kernel/wire"; + +const SR_FILE = new URL("../src/server/snapshot-render.ts", import.meta.url).pathname; + +function haveSolver(): boolean { + try { + solveSmt2("(set-logic ALL)(declare-const x Int)(assert (> x 1))(check-sat)", []); + return true; + } catch { + return false; + } +} + +test("bug3756 judge: buildView keeps the client tail and the summary anchor (symbolic)", { timeout: 120_000 }, async (t) => { + if (!haveSolver()) return t.skip("no SMT solver on PATH (set VERIF_PYTHON)"); + + const ctx = newCtx(); + const numv = (x: number): SymVal => ({ kind: "num", t: ctx.Real.val(x) }); + const strv = (t: Term): SymVal => ({ kind: "str", t }); + const arrv = (elems: SymVal[]): SymVal => ({ kind: "arr", elems, len: ctx.Real.val(elems.length) }); + const objv = (fields: [string, SymVal][]): SymVal => ({ kind: "obj", fields: new Map(fields) }); + + // Transcript rows: 3 fully-symbolic messages with distinct free ids. + const r1 = ctx.String.const("r1"), r2 = ctx.String.const("r2"), r3 = ctx.String.const("r3"); + const t1 = ctx.String.const("t1"), t2 = ctx.String.const("t2"), t3 = ctx.String.const("t3"); + const msg = (id: Term, text: Term): SymVal => + objv([["id", strv(id)], ["role", strv(ctx.String.val("user"))], ["contentType", strv(ctx.String.val("text"))], ["text", strv(text)]]); + // JSON template pieces EXACTLY matching the engine's jsonParseTemplate skeleton + // so JSON.parse(content) stays symbolic-parseable (no degradation). + const S0 = ctx.String.val('{"id":"'), S1 = ctx.String.val('","role":"'), S2 = ctx.String.val('","contentType":"'), S3 = ctx.String.val('","text":"'), S4 = ctx.String.val('"}'); + const ROLE = ctx.String.val("user"), CT = ctx.String.val("text"); + const row = (seq: number, id: Term, text: Term): SymVal => + objv([ + ["seq", numv(seq)], + ["content", strv(S0.concat(id).concat(S1).concat(ROLE).concat(S2).concat(CT).concat(S3).concat(text).concat(S4))], + ]); + const rows = arrv([row(1, r1, t1), row(2, r2, t2), row(3, r3, t3)]); + + // Client history: 2 prefix ids shared with rows (seam ≥ MIN_SEAM_RUN=2), free tail. + const iF = ctx.String.const("iF"), tF = ctx.String.const("tF"); + const client = arrv([msg(r1, t1), msg(r2, t2), msg(iF, tF)]); + + // Active block whose fold-time ids resolve against the rows (anchored). + const block = objv([ + ["active", { kind: "bool", t: ctx.Bool.val(true) } as SymVal], + ["blockId", strv(ctx.String.val("b1"))], + ["effectiveMessageIds", arrv([strv(r2)])], + ]); + const session = objv([ + ["id", strv(ctx.String.val("s1"))], + ["state", objv([["blocks", arrv([block])]])], + ["metadata", objv([])], + ]); + + const io = objv([ + ["messages", client], + ["effects", objv([["preTransformMessages", client]])], + ]); + const out = translateFunction({ + file: SR_FILE, function: "buildView", params: { session, clientMsgs: client }, + ctx, + options: { + portImpls: { + getStore: (): SymVal => objv([]), + __method_unifiedTailViewRows: (): SymVal => rows, + __method_unifiedTailSeqs: (): SymVal => arrv([numv(1), numv(2), numv(3)]), + }, + }, + }).exec(); + assert.deepEqual(out.degradations, [], "buildView must translate without degradations"); + const ret = out.ret; + assert.equal(ret?.kind, "obj", "buildView must return an object"); + const retObj = ret as { fields: Map }; + const okB = retObj.fields.get("ok"); + assert.equal(okB?.kind, "bool"); + const view = retObj.fields.get("view"); + assert.equal(view?.kind, "arr", "view must be present (guarded) on the merged result"); + + const base = [...(out.assumptions as unknown as Term[])]; + // Domain: distinct row ids; distinct tail id (a duplicated tail id is the + // #3945 collision family — its own judge covers that shape). + const dom: Term[] = [ + r1.neq(r2), r1.neq(r3), r2.neq(r3), + iF.neq(r1), iF.neq(r2), iF.neq(r3), + ]; + const okIsTrue = (okB as { t: Term }).t.eq(ctx.Bool.val(true)); + + // --- C1 tail preservation: the free tail message must appear in the view. + const viewArr = view as { elems: SymVal[]; len: Term }; + const viewIdsContain = (idTerm: Term): Term => { + let acc: Term | null = null; + viewArr.elems.forEach((e, k) => { + if (e.kind !== "obj") return; + const idv = (e as { fields: Map }).fields.get("id"); + if (!idv || idv.kind !== "str") return; + const live = viewArr.len.gt(ctx.Real.val(k)); + acc = acc === null ? live.and((idv as { t: Term }).t.eq(idTerm)) : acc!.or(live.and((idv as { t: Term }).t.eq(idTerm))); + }); + return acc ?? ctx.Bool.val(false); + }; + // C1 is BYTES-based, not id-based: the chain may legitimately bind a fresh + // tail message to an upgrade twin by signature (identical role/ct/text) — + // the tail is then rendered as the twin's row, re-keyed but byte-equal. + // What must never happen is the tail's CONTENT disappearing from the view. + const viewTextsContain = (txt: Term): Term => { + let acc: Term | null = null; + (view as { elems: SymVal[] }).elems.forEach((e, k) => { + if (e.kind !== "obj") return; + const tv = (e as { fields: Map }).fields.get("text"); + if (!tv || tv.kind !== "str") return; + const g = (view as { len: Term }).len.gt(ctx.ToReal(ctx.Int.val(k) as never) as never); + const piece = g.and((tv as { t: Term }).t.eq(txt)); + acc = acc === null ? piece : (acc as Term).or(piece); + }); + return (acc ?? ctx.Bool.val(false)) as Term; + }; + // C1 (collision-free tail): under ok, when the tail's signature does NOT + // collide with the covered row, its content must appear in the view. + const qc1 = solveSmt2(smt2Of(ctx, [...base, ...dom, okIsTrue as Term, tF.neq(t2) as Term, viewTextsContain(tF).not() as Term] as never[], [] as never[]), []); + assert.equal(qc1.status, "unsat", "C1: under ok, the client tail message content must never be dropped from the view"); + // C1b (#4052 regression pin): the tail's (role|ct|text) EQUALS the covered + // row's — the sig fallback binds the fresh message to a covered row and the + // anchor swallows it. SAT today; a fix (covered rows excluded from sig + // candidates, or stronger sig) must flip this to unsat. + const qc1b = solveSmt2(smt2Of(ctx, [...base, ...dom, okIsTrue as Term, tF.eq(t2) as Term, viewTextsContain(tF).not() as Term] as never[], [] as never[]), []); + assert.equal(qc1b.status, "sat", "C1b: #4052 shape (tail sig-collides with covered row) currently loses the tail — flip to unsat when fixed"); + + // --- C2 anchor presence: the acp_summary_b1 carrier must be in the view. + const qc2 = solveSmt2(smt2Of(ctx, [...base, ...dom, okIsTrue as Term, viewIdsContain(ctx.String.val("acp_summary_b1")).not() as Term] as never[], [] as never[]), []); + assert.equal(qc2.status, "unsat", "C2: under ok with an anchored active block, the summary carrier must be present"); + + // --- C3 non-vacuity: the success path is reachable. + const qc3 = solveSmt2(smt2Of(ctx, [...base, ...dom, okIsTrue as Term] as never[], [] as never[]), []); + assert.equal(qc3.status, "sat", "C3: ok=true must be reachable (judge not vacuous)"); +}); + +test("bug3756 replay: maybeSnapshotView end-to-end on real JS (success / #3812 lone-dup / unanchored)", () => { + const rowsOf = (ids: string[]): unknown[] => + ids.map((id, i) => ({ + seq: i + 1, + msgId: id, + ref: null, refNum: null, role: "user", kind: "text", + content: JSON.stringify({ id, role: "user", contentType: "text", text: `body-${id}` }), + contentHash: `h${i}`, tokens: 3, + })); + const fakeStore = (rows: unknown[] | null): SessionStore => + ({ unifiedTailViewRows: () => rows, unifiedTailSeqs: () => (rows ? rows.map((_, i) => i + 1) : null) }) as unknown as SessionStore; + const mkSession = (blockIds: string[]): { session: never; blocks: never } => { + const session = { + id: "s1", + state: { blocks: blockIds.length ? [{ blockId: "b1", runId: "r", tier: 1, summary: "S", directMessageIds: blockIds, effectiveMessageIds: blockIds, directBlockIds: [], compressedTokens: 9, createdAt: 0, survivedCount: 0, generation: 1, active: true }] : [] }, + metadata: {}, + }; + return { session: session as never, blocks: undefined as never }; + }; + const prev = process.env.BILI_ROLLOUT_SNAPSHOT_RENDER; + process.env.BILI_ROLLOUT_SNAPSHOT_RENDER = "1"; + const deps = { sessionId: "s1" }; + try { + // (a) success shape: 3 rows, block anchored at row 2, client = [r1, r2, NEW]. + _setStoreForTest(fakeStore(rowsOf(["r1", "r2", "r3"]))); + const { session: sA } = mkSession(["r2"]); + const clientA: BiliMessage[] = [ + { id: "r1", role: "user", contentType: "text", text: "body-r1" }, + { id: "r2", role: "user", contentType: "text", text: "body-r2" }, + { id: "NEW", role: "user", contentType: "text", text: "tail-msg" }, + ]; + const outA = maybeSnapshotView(sA as never, clientA, deps as never); + const idsA = outA.map((m) => m.id); + assert.ok(idsA.includes("NEW"), "tail message must survive the snapshot view"); + assert.ok(idsA.includes("acp_summary_b1"), "summary carrier must be present"); + assert.ok(!idsA.includes("r2"), "covered row must be suppressed in favor of the anchor"); + + // (b) #3812 lone-duplicate: only ONE client id resolves (seam < MIN_SEAM_RUN). + _setStoreForTest(fakeStore(rowsOf(["r1", "r2", "r3"]))); + const { session: sB } = mkSession(["r2"]); + const clientB: BiliMessage[] = [ + { id: "X1", role: "user", contentType: "text", text: "a" }, + { id: "r2", role: "user", contentType: "text", text: "body-r2" }, + { id: "NEW", role: "user", contentType: "text", text: "t" }, + ]; + const outB = maybeSnapshotView(sB as never, clientB, deps as never); + assert.equal(outB, clientB, "seam-too-short must degrade to the legacy view (client bytes untouched)"); + assert.equal((sB as unknown as { metadata: Record }).metadata["snapshotRenderDegradeReason"], "seam-too-short"); + + // (c) block-unanchored: fold-time ids resolve against nothing. + _setStoreForTest(fakeStore(rowsOf(["r1", "r2", "r3"]))); + const { session: sC } = mkSession(["GHOST"]); + const clientC: BiliMessage[] = [ + { id: "r1", role: "user", contentType: "text", text: "body-r1" }, + { id: "r2", role: "user", contentType: "text", text: "body-r2" }, + { id: "NEW", role: "user", contentType: "text", text: "t" }, + ]; + const outC = maybeSnapshotView(sC as never, clientC, deps as never); + assert.equal(outC, clientC, "unanchored block must degrade to the legacy view"); + assert.equal((sC as unknown as { metadata: Record }).metadata["snapshotRenderDegradeReason"], "block-unanchored"); + } finally { + if (prev === undefined) delete process.env.BILI_ROLLOUT_SNAPSHOT_RENDER; + else process.env.BILI_ROLLOUT_SNAPSHOT_RENDER = prev; + _setStoreForTest(null as never); + } +}); diff --git a/tests/verif-view-latch-discipline.test.ts b/tests/verif-view-latch-discipline.test.ts new file mode 100644 index 0000000000..09c73aabd1 --- /dev/null +++ b/tests/verif-view-latch-discipline.test.ts @@ -0,0 +1,190 @@ +/** + * Latch-discipline induction judge (#3989 phase 1). + * + * The #3956 fold-gate contract: on a QUIET turn (no fold, no config change, + * no fresh arrival) the view latch must replay latched bytes VERBATIM — + * whatever the transform layer just produced for an already-latched ref is + * deferred to the next boundary. This is the general invariant that makes + * quiet turns cache-neutral; it must hold for arbitrary (pre, post, latch) + * triples, which is exactly a solver question, not a scenario question. + * + * No-cheat contract (#3848): + * - we execute the REAL applyViewLatch (translateFunction on view-latch.ts); + * - the invariant is a GENERAL discipline ("replay == latch entry, always, + * on quiet turns"), never a statement about any specific bug; + * - UNSAT answers are bounded-absence claims within the constructed bounds + * (bounded message count — the quiet-turn discipline is per-message + * local, so the bounded claim transfers; see the induction note below); + * - the SAT side (config-boundary release) and the full behavior matrix are + * replayed on real code. + */ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { resolve } from "node:path"; +import { translateFunction } from "../verif/symbolic.js"; +import { newCtx, smt2Of } from "../verif/terms.js"; +import { solveSmt2 } from "../verif/solve.js"; +import { applyViewLatch } from "../kernel/src/view-latch.ts"; +import type { CompressionState, CoreMessage, ViewLatchEntry } from "../kernel/src/types.ts"; +import type { NodeIO } from "../kernel/src/pipeline.ts"; +import type { SymVal } from "../verif/symbolic.ts"; + +const file = resolve(import.meta.dirname, "../kernel/src/view-latch.ts"); + +test("view-latch discipline: quiet turns replay latched bytes verbatim (arbitrary texts)", { timeout: 240_000 }, () => { + const ctx = newCtx(); + // One already-latched message (id i1 → ref r1, latched text L1 ≠ anything + // fixed) + one fresh message (id i2 → ref r2, no latch entry). Both the + // resent (pre) and transformed (post) texts are FREE strings — the judge + // must not be able to find any triple where a quiet turn leaks transform + // output for the latched ref. + const id1 = ctx.String.const("i1"), id2 = ctx.String.const("i2"); + const r1 = ctx.String.const("r1"), r2 = ctx.String.const("r2"); + const L1 = ctx.String.const("L1"); + const Tpre1 = ctx.String.const("Tpre1"), Tpre2 = ctx.String.const("Tpre2"); + const Tpost1 = ctx.String.const("Tpost1"), Tpost2 = ctx.String.const("Tpost2"); + const strv = (t: unknown) => ({ kind: "str", t }) as SymVal; + const msg = (id: unknown, text: unknown): SymVal => ({ kind: "obj", fields: new Map([ + ["id", strv(id)], ["text", strv(text)], + ]) } as never); + const byRaw: SymVal = { kind: "obj", fields: new Map(), symFields: new Map([ + [id1, { g: ctx.Bool.val(true), v: strv(r1) }], + [id2, { g: ctx.Bool.val(true), v: strv(r2) }], + ]) } as never; + const latchEntry: SymVal = { kind: "obj", fields: new Map([ + ["text", strv(L1)], ["hidden", { kind: "bool", t: ctx.Bool.val(false) }], + ]) } as never; + const viewLatches: SymVal = { kind: "obj", fields: new Map(), symFields: new Map([ + [r1, { g: ctx.Bool.val(true), v: latchEntry }], + ]) } as never; + const state: SymVal = { kind: "obj", fields: new Map([ + ["messageRefs", { kind: "obj", fields: new Map([["byRaw", byRaw]]) } as SymVal], + ["viewLatches", viewLatches], + ]) } as never; + const io: SymVal = { kind: "obj", fields: new Map([ + ["messages", { kind: "arr", elems: [msg(id1, Tpost1), msg(id2, Tpost2)], len: ctx.Real.val(2) } as SymVal], + ["state", state], + ["effects", { kind: "obj", fields: new Map([["preTransformMessages", + { kind: "arr", elems: [msg(id1, Tpre1), msg(id2, Tpre2)], len: ctx.Real.val(2) } as SymVal]]) } as SymVal], + ]) } as never; + const run = translateFunction({ + file, function: "applyViewLatch", + params: { io, ctx: { kind: "undef" } as SymVal }, + ctx, options: {}, + }).exec(); + assert.deepEqual(run.degradations, [], "view-latch translation must be faithful (no degradations)"); + const outArr = (run.ret as { fields: Map }).fields.get("messages") as { elems: SymVal[]; len: unknown }; + assert.ok(outArr, "applyViewLatch must return io with messages"); + const out1 = outArr.elems[0] as { fields: Map }; + const out1Text = (out1.fields.get("text") as { t: unknown }).t as { eq: (x: unknown) => { not: () => unknown } }; + // Domain constraints, each pinning a DELIBERATE unlatchable class: + // - ids/refs are non-empty (assignRefs never mints ""; the !m.id guard + // passes unidentified messages through post view); + // - ids are not CCR retrieved placeholders (RETRIEVED_ID_PREFIX guard); + // - the latched ref is not BLOCKED (BLOCKED carriers render verbatim and + // never carry latch entries in reachable states). + const INT1 = (ctx as unknown as { Int: { val: (n: number) => unknown } }).Int.val(1); + const lenGE1 = (v: unknown) => (v as { length: () => { ge: (x: unknown) => unknown } }).length().ge(INT1); + // Term.prefixOf(a) has JS startsWith semantics: receiver.startsWith(arg). + // "id must not start with p" ⇒ NOT (id.prefixOf(val(p))). + const notPrefix = (p: string, v: unknown) => ((v as { prefixOf: (x: unknown) => unknown }).prefixOf((ctx as unknown as { String: { val: (s: string) => unknown } }).String.val(p)) as { not: () => unknown }).not(); + const RETP = "acp_retrieved_"; + const dom: unknown[] = [ + lenGE1(id1), lenGE1(id2), lenGE1(r1), lenGE1(r2), + notPrefix(RETP, id1), notPrefix(RETP, id2), + // distinct ids: duplicate raw ids collapsing one byRaw slot is the + // #3945 collision family (pinned by its own generic harness); for the + // latch-discipline judge we pin the identified, non-colliding domain. + (id1 as { neq: (x: unknown) => unknown }).neq(id2), + (r1 as { neq: (x: unknown) => unknown }).neq((ctx as unknown as { String: { val: (s: string) => unknown } }).String.val("BLOCKED")), + // distinct raw ids hold distinct numeric refs in real byRaw states + // (assignRefs allocates one ref per id; BLOCKED shared carriers are + // excluded above) — same-ref-different-id is unreachable. + (r1 as { neq: (x: unknown) => unknown }).neq(r2), + ]; + // Q1: can the FIRST output message's text differ from the latched L1? + // Guard the slot read by the array length: elems[N] beyond len is ghost + // data (branch-merged slots), not a real message. + const lenGE = (v: unknown, k: number) => (v as { ge: (x: unknown) => unknown }).ge((ctx as unknown as { Real: { val: (n: number) => unknown } }).Real.val(k)); + const q1 = solveSmt2(smt2Of(ctx, [ + ...(run.assumptions as never[]), + ...dom as never[], + lenGE(outArr.len, 1), + out1Text.eq(L1).not() as never, + ] as never[], [] as never), []); + if (q1.status !== "unsat") { + const wnames = ["L1", "Tpre1", "Tpre2", "Tpost1", "Tpost2", "i1", "i2", "r1", "r2"]; + const dbgs = smt2Of(ctx, [...(run.assumptions as never[]), ...dom as never[], lenGE(outArr.len, 1) as never, out1Text.eq(L1).not() as never] as never[], wnames as never[]); + const dbg = solveSmt2(typeof dbgs === "string" ? dbgs : (dbgs as { smt2: string }).smt2 as never, wnames); + console.log("Q1 witness:", dbg.status, JSON.stringify(dbg.values), String(dbg.raw).slice(0, 300)); + } + assert.equal(q1.status, "unsat", + "quiet turn must replay the latched bytes verbatim — no transform leak, for ANY pre/post/latch texts"); + // Q2: can the FRESH message's output differ from its own transform output? + const out2 = outArr.elems[1] as { fields: Map }; + const out2Text = (out2.fields.get("text") as { t: unknown }).t as { eq: (x: unknown) => { not: () => unknown } }; + const q2 = solveSmt2(smt2Of(ctx, [ + ...(run.assumptions as never[]), + ...dom as never[], + lenGE(outArr.len, 2), + out2Text.eq(Tpost2).not() as never, + ] as never[], [] as never), []); + assert.equal(q2.status, "unsat", + "fresh (unlatched) messages must pass the transform output through untouched on quiet turns"); + // Q2: fresh (unlatched) messages pass their own transform output through — + // guardedNeutral on one-arm array slots (mergeVal arr) removed the ghost + // arms that previously made this query a false SAT. + // Sanity: the two output slots are actually distinct messages. + const out1Id = (out1.fields.get("id") as { t: unknown }).t as { eq: (x: unknown) => { not: () => unknown } }; + const sane = solveSmt2(smt2Of(ctx, [...(run.assumptions as never[]), ...dom as never[], (out1Id.eq(id1) as unknown as { not: () => unknown }).not() as never] as never[], [] as never), []); + assert.equal(sane.status, "unsat", "output order must be preserved (slot 0 is still i1)"); +}); + +test("view-latch behavior matrix on real code (quiet replay / arrival / fold reset / steady-state no-churn)", () => { + const baseState = (): CompressionState => ({ + messageRefs: { byRaw: { a: "r1", b: "r2" }, byRef: {} }, + viewLatches: { r1: { text: "LATCHED_A", hidden: false } }, + } as unknown as CompressionState); + const pre: CoreMessage[] = [ + { id: "a", role: "user", contentType: "text", text: "resent A" }, + { id: "b", role: "user", contentType: "text", text: "resent B" }, + ] as CoreMessage[]; + const ioOf = (post: CoreMessage[], state: CompressionState): NodeIO => ({ + messages: post, state, effects: { preTransformMessages: pre }, + }) as NodeIO; + // Quiet turn: transform layer produced NEW bytes for the latched ref — + // the latch must replay the old bytes, not the fresh transform output. + const q1 = applyViewLatch(ioOf([ + { ...pre[0]!, text: "TRANSFORM_NEW_A" }, pre[1]!, + ], baseState())); + assert.equal((q1.messages[0] as CoreMessage).text, "LATCHED_A", "quiet turn replays latched bytes"); + assert.equal((q1.messages[1] as CoreMessage).text, "resent B", "fresh ref passes through"); + // Second quiet turn with yet another transform output: byte-stable. + const q2 = applyViewLatch(ioOf([ + { ...pre[0]!, text: "TRANSFORM_NEWER_A" }, pre[1]!, + ], q1.state)); + assert.equal((q2.messages[0] as CoreMessage).text, "LATCHED_A", "second quiet turn identical"); + assert.strictEqual(q1.state, baseState() === q1.state ? undefined : q1.state); // state replaced, not mutated + // Steady state: identical transform outputs → the SAME state object (no churn). + const steady = applyViewLatch(ioOf([ + { ...pre[0]!, text: "TRANSFORM_NEW_A" }, pre[1]!, + ], q2.state)); + assert.strictEqual(steady.state, q2.state, "steady-state latch map must not churn the state object"); + // Arrival: a fresh message whose transform rewrote it latches AT arrival + // (the flip rides the history change, not a quiet turn). + const arr = applyViewLatch(ioOf([ + { ...pre[0]!, text: "TRANSFORM_NEW_A" }, { ...pre[1]!, text: "CRUSHED_B" }, + ], baseState())); + assert.equal((arr.messages[1] as CoreMessage).text, "CRUSHED_B", "arrival shows the transform output"); + const latches = (arr.state.viewLatches ?? {}) as Record; + assert.equal(latches.r2?.text, "CRUSHED_B", "arrival latches what the transform produced"); + // Arrival of a transform-HIDDEN fresh message stays hidden and latches it. + const hid = applyViewLatch(ioOf([pre[0]!], baseState())); + assert.equal(hid.messages.length, 1, "transform-hidden message stays hidden"); + assert.equal(((hid.state.viewLatches ?? {}) as Record).r2?.hidden, true, "hidden latched"); + // Fold reset: the next pass re-latches fresh (the deferred transform lands). + const afterFold = applyViewLatch(ioOf([ + { ...pre[0]!, text: "TRANSFORM_NEW_A" }, pre[1]!, + ], { ...baseState(), viewLatches: undefined })); + assert.equal((afterFold.messages[0] as CoreMessage).text, "TRANSFORM_NEW_A", "fold reset releases the deferred transform"); +}); diff --git a/tests/verif-wire-anthropic-roundtrip.test.ts b/tests/verif-wire-anthropic-roundtrip.test.ts new file mode 100644 index 0000000000..dbeedc6698 --- /dev/null +++ b/tests/verif-wire-anthropic-roundtrip.test.ts @@ -0,0 +1,204 @@ +import test from "node:test"; +import assert from "node:assert"; +import { translateFunction } from "../verif/symbolic.ts"; +import { newCtx, smt2Of } from "../verif/terms.ts"; +import { solveSmt2 } from "../verif/solve.ts"; +import type { SymVal } from "../verif/symbolic.ts"; +import { anthropicToCore, coreToAnthropic } from "../kernel/src/wire/anthropic.ts"; +import type { Term } from "../verif/terms.ts"; + +/** + * Wire round-trip discipline judge (phase 2: adapters beyond openai). + * + * Property (#3676 family, adapter-generic): parse→rebuild of a wire + * conversation must preserve block ORDER and per-block payload. The openai + * adapter flips [file,text]→[text,file] (#3676); this judge asks the same + * question of the anthropic codec under ARBITRARY text payloads and + * arbitrary type draws from its same-role block universe + * (text | tool_result — both rebuild into user-role groups). + * + * No-cheat rules (verif/README.md): REAL translated entry fns + * (anthropicToCore + coreToAnthropic), general invariant (first/second + * rebuilt block equals the original block's type and payload), ports only + * for the declared seams (deriveCanonicalMessageId — sha256 is outside the + * guarded subset; ClusterCounter instance state), replay on real code, + * negative controls via UNSAT. + */ + +const KP = "/home/dog/tmp/bili-verif2/kernel/src/wire/anthropic.ts"; + +function makeIdPort(): (role: SymVal, ct: SymVal, text: SymVal, opts?: SymVal) => SymVal { + const memo = new Map(); + let n = 0; + return (role: SymVal, ct: SymVal, text: SymVal, _opts?: SymVal): SymVal => { + const key = [role, ct, text].map((x) => (x.kind === "str" ? `s:${String((x as { t: Term }).t)}` : x.kind)).join("\u0000"); + const hit = memo.get(key); + if (hit) return hit; + void n++; + const ctx = newCtx(); + const out: SymVal = { kind: "str", t: ctx.String.val(`__id_${memo.size}`) }; + memo.set(key, out); + return out; + }; +} + +function makeCounterPorts(): Record SymVal> { + const counters = new WeakMap(); + return { + ClusterCounter: (recv: SymVal): SymVal => { + void recv; + const inst = { marker: true }; + counters.set(inst, { n: 0 }); + return { kind: "obj", fields: new Map([["__inst", { kind: "obj", fields: new Map() } as SymVal]]) } as never as SymVal; + }, + __method_next: (recv: SymVal, base: SymVal): SymVal => { + const st = counters.get((recv as { fields: Map }).fields.get("__inst") as never as object) ?? { n: 0 }; + const n2 = st.n + 1; + if (Object.isFrozen(st) || !(recv as { fields: Map }).fields) return base; + st.n = n2; + counters.set((recv as { fields: Map }).fields.get("__inst") as never as object, st); + if (base.kind !== "str") return { kind: "any" }; + const ctx = newCtx(); + return n2 === 1 ? base : { kind: "str", t: ctx.String.val("__dup") }; + }, + }; +} + +test("wire-anthropic round-trip: block order + payload preserved (same-role pairs)", { timeout: 240_000 }, () => { + // 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 + // pair covers the same-role contract; thinking-cross-message is test 2. + const pairs: [string, string][] = [["text", "text"], ["text", "tool_result"], ["tool_result", "text"], ["tool_result", "tool_result"]]; + for (const [ty1, ty2] of pairs) { + const ctx = newCtx(); + const strv = (t: unknown) => ({ kind: "str", t }) as SymVal; + const T1 = ctx.String.const("T1"), T2 = ctx.String.const("T2"); + const C1 = ctx.String.const("C1"), C2 = ctx.String.const("C2"); + const uid1 = ctx.String.const("uid1"), uid2 = ctx.String.const("uid2"); + const blk = (ty: string, t: unknown, c: unknown, uid: unknown): SymVal => + ({ kind: "obj", fields: new Map([["type", strv(ctx.String.val(ty))], ["text", strv(t)], ["content", strv(c)], ["tool_use_id", strv(uid)], ["thinking", strv(t)], ["id", strv(uid)], ["name", strv(ctx.String.val("tool"))], ["input", strv(ctx.String.val("{}"))]]) } as never); + const content: SymVal = { kind: "arr", elems: [blk(ty1, T1, C1, uid1), blk(ty2, T2, C2, uid2)], 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(); + assert.deepEqual(parse.degradations, [], `parse must translate cleanly (${ty1},${ty2})`); + 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(); + assert.deepEqual(rebuild.degradations, [], `rebuild must translate cleanly (${ty1},${ty2})`); + 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; + const dom = [uid1.neq(uid2) as never, T1.neq(T2) as never, C1.neq(C2) as never, T1.neq(C1) as never, T2.neq(C2) as never] as never[]; + const blocks0 = blocksOf(outArr.elems[0]!); + const b0 = blocks0.elems[0]!, b1 = blocks0.elems[1]!; + const pay = (b: SymVal, ty: string, t: unknown, c: unknown): unknown => + ty === "tool_result" ? fld(b, "content").t : fld(b, "text").t; + const q1 = solveSmt2(smt2Of(ctx, [...parse.assumptions as never[], ...rebuild.assumptions as never[], ...dom, + blocks0.len ? (blocks0.len as never as { lt: (x: unknown) => unknown }).lt(ctx.Real.val(2)) as never : (ctx.Real.val(0) as never), + (fld(b0, "type").t as never as { eq: (x: unknown) => { not: () => unknown } }).eq(ctx.String.val(ty1)).not() as never, + (pay(b0, ty1, T1, C1) as never as { eq: (x: unknown) => { not: () => unknown } }).eq(ty1 === "tool_result" ? C1 : T1).not() as never] as never[], ["T1", "T2", "C1", "C2"] as never[]), ["T1", "T2", "C1", "C2"]); + assert.equal(q1.status, "unsat", `first rebuilt block must preserve block 1's type and payload (${ty1},${ty2})`); + const q2 = solveSmt2(smt2Of(ctx, [...parse.assumptions as never[], ...rebuild.assumptions as never[], ...dom, + blocks0.len ? (blocks0.len as never as { lt: (x: unknown) => unknown }).lt(ctx.Real.val(2)) as never : (ctx.Real.val(0) as never), + (fld(b1, "type").t as never as { eq: (x: unknown) => { not: () => unknown } }).eq(ctx.String.val(ty2)).not() as never, + (pay(b1, ty2, T2, C2) as never as { eq: (x: unknown) => { not: () => unknown } }).eq(ty2 === "tool_result" ? C2 : T2).not() as never] as never[], ["T1", "T2", "C1", "C2"] as never[]), ["T1", "T2", "C1", "C2"]); + assert.equal(q2.status, "unsat", `second rebuilt block must preserve block 2's type and payload (${ty1},${ty2})`); + } +}); + +test("wire-anthropic round-trip: thinking keeps its own message after same-role group (cross-message order)", { timeout: 240_000 }, () => { + 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 + // the thinking cross-message split, not the block-1 dispatch (test 1 owns + // the symbolic TYPE1 order property; an arm-union would make the payload + // fields unqueryable via the safeStringify degrade in the tool_use arm). + const T1 = ctx.String.const("T1"), T2 = ctx.String.const("T2"); + const C1 = ctx.String.const("C1"); + const uid1 = ctx.String.const("uid1"); + const blk = (ty: unknown, t: unknown, c: unknown, uid: unknown): SymVal => + ({ kind: "obj", fields: new Map([["type", strv(ty)], ["text", strv(t)], ["content", strv(c)], ["tool_use_id", strv(uid)], ["thinking", strv(t)], ["id", strv(uid)], ["name", strv(ctx.String.val("tool"))], ["input", strv(ctx.String.val("{}"))]]) } as never); + 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), + (fld(out1, "role").t as never as { eq: (x: unknown) => { not: () => unknown } }).eq(ctx.String.val("assistant")).not() as never, + (thinkT2 as never as { eq: (x: unknown) => { not: () => unknown } }).eq(T2).not() as never] as never[], ["TYPE1", "T1", "T2", "C1"] as never[]), ["TYPE1", "T1", "T2", "C1"]); + assert.equal(q.status, "unsat", "thinking block must land in its own assistant message, payload intact"); +}); + +test("wire-anthropic round-trip: real-code replay baseline", () => { + const run = (blocks: unknown[]): unknown[] => { + const core = anthropicToCore({ messages: [{ role: "user", content: blocks as never }] } as never); + return coreToAnthropic(core.msgs); + }; + const r1 = run([{ type: "text", text: "alpha" }, { type: "tool_result", tool_use_id: "t9", content: "beta" }]); + assert.equal((r1[0] as { content: unknown[] }).content.length, 2); + assert.deepEqual((r1[0] as { content: { type: string }[] }).content.map((b) => b.type), ["text", "tool_result"]); + assert.equal(((r1[0] as { content: { type: string; text?: string }[] }).content[0]!).text, "alpha"); + assert.equal(((r1[0] as { content: { type: string; content?: string }[] }).content[1]!).content, "beta"); + const r2 = run([{ type: "tool_result", tool_use_id: "t9", content: "beta" }, { type: "text", text: "alpha" }]); + assert.deepEqual((r2[0] as { content: { type: string }[] }).content.map((b) => b.type), ["tool_result", "text"], "order preserved both directions"); + const r3 = run([{ type: "text", text: "alpha" }, { type: "thinking", thinking: "secret", signature: "sig" }]); + assert.equal(r3.length, 2); + assert.equal((r3[1] as { role: string }).role, "assistant"); + assert.equal(((r3[1] as { content: { type: string; thinking?: string }[] }).content[0]!).thinking, "secret"); +}); diff --git a/tests/verif-wire-google-rebuild-stability.test.ts b/tests/verif-wire-google-rebuild-stability.test.ts new file mode 100644 index 0000000000..afa3342464 --- /dev/null +++ b/tests/verif-wire-google-rebuild-stability.test.ts @@ -0,0 +1,138 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { translateFunction, type SymVal } from "../verif/symbolic.ts"; +import { newCtx, smt2Of, type Context, type Term, type Seq } from "../verif/terms.ts"; +import { solveSmt2 } from "../verif/solve.ts"; + +/** + * #3848 phase 2 — google adapter rebuild stability under append. + * + * Property (byte-stability contract in kernel/src/wire/google.ts:353): + * coreToGoogle is PURE in the core array — same shape, same bytes, every + * request. Appending an ALTERNATING-role core opens a new content and must + * leave every earlier content byte-identical; appending a same-side + * tool-result core merges into the trailing user content with the plain + * text part still leading (#3159 canonical order) — the prefix text part + * itself must not drift. + * + * No-cheat: the entry is the REAL coreToGoogle; the appended cores are + * fully symbolic payloads (texts/toolName/toolCallId); no knowledge of + * google.ts internals enters the queries. Ports only for identity seams + * this direction does not exercise (none needed — rebuild helpers are + * same-file). + */ + +const haveSolver = (() => { + try { void solveSmt2("(check-sat)", []); return true; } catch { return false; } +})(); + +type KCtx = Context; +const newCtx0 = (n: number): Term => newCtx().Real.val(n); +const strv = (ctx: KCtx, t: Term): SymVal => ({ kind: "str", t: t as Seq }); +const objv = (fields: [string, SymVal][]): SymVal => + ({ kind: "obj", fields: new Map(fields) }) as SymVal; +const arrv = (elems: SymVal[]): SymVal => + ({ kind: "arr", elems, len: newCtx0(elems.length) }) as SymVal; + +function coreMsg(fields: [string, SymVal][], rawParts: SymVal[]): SymVal { + return objv([...fields, ["rawGoogleParts", arrv(rawParts)]]); +} + +/** REAL coreToGoogle over a symbolic core array. */ +function runCoreToGoogle(ctx: KCtx, msgs: SymVal): { ret: SymVal; assumptions: unknown[]; degradations: string[] } { + const out = translateFunction({ + file: "/home/dog/tmp/bili-verif2/kernel/src/wire/google.ts", + function: "coreToGoogle", + params: { messages: msgs }, + ctx: ctx as never, + options: {}, + }).exec() as unknown as { ret: SymVal; assumptions: unknown[]; degradations: string[] }; + return out; +} + +const contentsOf = (ret: SymVal): { elems: SymVal[] } => { + assert.equal(ret.kind, "arr", "coreToGoogle returns the contents array"); + return ret as { elems: SymVal[] }; +}; +const partTextOf = (content: SymVal): SymVal => { + const parts = (content as { fields: Map }).fields.get("parts") as { elems: SymVal[] }; + const p0 = parts.elems[0] as { fields: Map }; + return p0.fields.get("text") ?? { kind: "undef" }; +}; + +test("google rebuild: alternating append leaves earlier contents byte-identical (symbolic payloads)", { skip: !haveSolver }, () => { + const ctx = newCtx(); + const T1 = ctx.String.const("T1"), T2 = ctx.String.const("T2"); + // len(T1) >= 1: empty text cores render no text part (contentParts drops + // them); the domain here is real text turns. + const base = coreMsg( + [["id", strv(ctx, ctx.String.val("u1"))], ["role", strv(ctx, ctx.String.val("user"))], ["contentType", strv(ctx, ctx.String.val("text"))], ["text", strv(ctx, T1)]], + [objv([["text", strv(ctx, T1)]])], + ); + const appended = coreMsg( + [["id", strv(ctx, ctx.String.val("a1"))], ["role", strv(ctx, ctx.String.val("assistant"))], ["contentType", strv(ctx, ctx.String.val("text"))], ["text", strv(ctx, T2)]], + [objv([["text", strv(ctx, T2)]])], + ); + const run1 = runCoreToGoogle(ctx, arrv([base])); + const run2 = runCoreToGoogle(ctx, arrv([base, appended])); + assert.deepEqual(run1.degradations, [], "rebuild turn 1 must translate cleanly"); + assert.deepEqual(run2.degradations, [], "rebuild turn 2 must translate cleanly"); + + const c1 = contentsOf(run1.ret), c2 = contentsOf(run2.ret); + // sanity: 1 content before, 2 after (alternating roles never merge) + { + const q = solveSmt2(smt2Of(ctx as never, [...run1.assumptions as never[], (c1.elems.length as unknown as number) !== 1 ? ctx.Bool.val(false) : ctx.Bool.val(false)] as never[], [] as never[]), []); + assert.equal(q.status, "unsat", "one content in turn 1"); + } + // Q1: the FIRST content is byte-stable across the append (role + first + // part's text). UNSAT for any drift. + const t1a = partTextOf(c1.elems[0]!), t1b = partTextOf(c2.elems[0]!); + const roleA = (c1.elems[0] as { fields: Map }).fields.get("role"); + const roleB = (c2.elems[0] as { fields: Map }).fields.get("role"); + const q1 = solveSmt2(smt2Of(ctx as never, [ + ...run1.assumptions as never[], ...run2.assumptions as never[], + (t1a as { t: { neq: (x: unknown) => unknown } }).t.neq((t1b as { t: unknown }).t) as never, + (roleA as { t: { neq: (x: unknown) => unknown } }).t.neq((roleB as { t: unknown }).t) as never, + ] as never[], [] as never[]), []); + assert.equal(q1.status, "unsat", "appending an alternating turn must not change any earlier content byte (prefix cache anchor)"); +}); + +test("google rebuild: same-side tool-result append merges with the text part unchanged (symbolic payloads)", { skip: !haveSolver }, () => { + const ctx = newCtx(); + const T1 = ctx.String.const("T1"), RT = ctx.String.const("RT"), N = ctx.String.const("N"), CID = ctx.String.const("CID"); + const base = coreMsg( + [["id", strv(ctx, ctx.String.val("u1"))], ["role", strv(ctx, ctx.String.val("user"))], ["contentType", strv(ctx, ctx.String.val("text"))], ["text", strv(ctx, T1)]], + [objv([["text", strv(ctx, T1)]])], + ); + const toolRes = coreMsg( + [["id", strv(ctx, ctx.String.val("r1"))], ["role", strv(ctx, ctx.String.val("tool"))], ["contentType", strv(ctx, ctx.String.val("tool-result"))], ["text", strv(ctx, RT)], ["toolName", strv(ctx, N)], ["toolCallId", strv(ctx, CID)]], + [objv([["functionResponse", objv([["name", strv(ctx, N)], ["id", strv(ctx, CID)]])]])], + ); + const run1 = runCoreToGoogle(ctx, arrv([base])); + const run2 = runCoreToGoogle(ctx, arrv([base, toolRes])); + assert.deepEqual(run2.degradations, [], "merge rebuild must translate cleanly"); + + const c1 = contentsOf(run1.ret), c2 = contentsOf(run2.ret); + // Q2: after the same-side append the merged content still leads with the + // ORIGINAL text part, byte-identical (#3159 canonical order; the fr trails). + const t1a = partTextOf(c1.elems[0]!), t1b = partTextOf(c2.elems[0]!); + const q2 = solveSmt2(smt2Of(ctx as never, [ + ...run1.assumptions as never[], ...run2.assumptions as never[], + (t1a as { t: { neq: (x: unknown) => unknown } }).t.neq((t1b as { t: unknown }).t) as never, + ] as never[], [] as never[]), []); + assert.equal(q2.status, "unsat", "same-side tool-result append must keep the leading text part byte-identical"); +}); + +test("google rebuild: real-code replay baseline", async () => { + const { coreToGoogle } = await import("../kernel/src/wire/google.ts"); + const base = { id: "u1", role: "user", contentType: "text", text: "hello there", rawGoogleParts: [{ text: "hello there" }] }; + const c1 = coreToGoogle([base] as never); + assert.deepEqual(c1, [{ role: "user", parts: [{ text: "hello there" }] }]); + const c2 = coreToGoogle([base, { id: "r1", role: "tool", contentType: "tool-result", text: "42", toolName: "calc", toolCallId: "call_9", rawGoogleParts: [{ functionResponse: { name: "calc", id: "call_9" } }] }] as never); + // merged single user content: text leads, fr trails (#3159) + assert.equal(c2.length, 1); + assert.deepEqual(c2[0]!.parts[0], { text: "hello there" }); + assert.deepEqual(c2[0]!.parts[1], { functionResponse: { name: "calc", id: "call_9" } }); + const c3 = coreToGoogle([base, { id: "a1", role: "assistant", contentType: "text", text: "hi", rawGoogleParts: [{ text: "hi" }] }] as never); + assert.deepEqual(c3[0], { role: "user", parts: [{ text: "hello there" }] }, "alternating append keeps earlier content identical"); +}); diff --git a/verif/README.md b/verif/README.md index 943bb9014e..548a56cc46 100644 --- a/verif/README.md +++ b/verif/README.md @@ -39,6 +39,7 @@ VERIF_PYTHON=/path/to/python-with-cvc5 node --import tsx --test tests/verif-bug* | `verif-bug3805-durable-ref-migration` | 定向不变量 | 保护解除时 BLOCKED→数字引用迁移给已发送字节中途加标签;非 durable 追加稳定(UNSAT) | | `verif-generic-append-stability` | **任意输入** | 消息全符号化、唯一不变量"只增不删的字节稳定":Q1 SAT(#3805 形状泛化重现)、Q2 SAT(**求解器自己发现的新 bug #3945**:id 撞车重打标,零保护解除)、Q3 UNSAT(排除两个已知形状后界内无第三种) | | `verif-induction-append-stability` | **归纳判官** | 任意消息数的 append-only 稳定性:前缀抽象成一个符号绑定,归纳步 UNSAT(id 不重复时任何前缀状态都动不了);撤掉约束 SAT = #3945;真代码随机抽查 60 轮 | +| `verif-view-latch-discipline` | **锁存纪律判官**(#3989) | 安静轮 latch 字节逐字回放(Q1 UNSAT)+ 新鲜消息直通不受污染(Q2 UNSAT,任意 pre/post/latch 文本)+ 真代码行为矩阵(quiet 回放 / 到达锁存 / 折叠重置 / 稳态零churn) | ## 无作弊规则(写新 harness 前必读) @@ -67,6 +68,11 @@ VERIF_PYTHON=/path/to/python-with-cvc5 node --import tsx --test tests/verif-bug* - 循环展开 K(默认 8)、数组槽位 N(默认 8):**有界模型检测**。要"任意 消息数"不用加 N —— 用归纳判官(`verif-induction-*`)。 - 模块级常量从源文件真实收集,不手抄。 +- 幻影臂已修(历史教训,保留记录):①符号键读扇出第一层曾用 + iteVal——obj/arr 与 null 中性值合并时 nullUnder 丢失,`o?.[k]` 键未命中 + 也会交出条目值(假真值+字段幻读),现改 iteStrict+member 读nullUnder门; + ②mergeVal 数组单臂槽位曾走 undef-loses 折叠,分支值无条件可达,现改 + guardedNeutral 携带分支守卫。 ## 已发现的 bug(求解器自动 + 回放确认) diff --git a/verif/guarded-files.ts b/verif/guarded-files.ts index 7d4830201c..70839070ac 100644 --- a/verif/guarded-files.ts +++ b/verif/guarded-files.ts @@ -20,4 +20,6 @@ export const GUARDED_FILES: readonly string[] = [ "kernel/src/rebuild.ts", "kernel/src/wire/openai.ts", "kernel/src/wire/responses.ts", + // #3989 phase 1 — the #3956 fold-gate state machines + "kernel/src/view-latch.ts", ]; diff --git a/verif/symbolic.ts b/verif/symbolic.ts index 5d6cf66fbe..6592bd587f 100644 --- a/verif/symbolic.ts +++ b/verif/symbolic.ts @@ -43,13 +43,13 @@ export type Num = Arith; type Str = Seq; export type SymVal = - | { kind: "num"; t: Num } - | { kind: "bool"; t: Bool<"main"> } - | { kind: "str"; t: Str } + | { kind: "num"; t: Num; undefUnder?: Bool<"main"> } + | { kind: "bool"; t: Bool<"main">; undefUnder?: Bool<"main"> } + | { kind: "str"; t: Str; undefUnder?: Bool<"main"> } // nullUnder: set on obj/arr values that came out of a STRICT branch merge // with null/undefined on the other arm — the value is the obj/arr only // when nullUnder is FALSE (JS truthiness/field-access must respect it). - | { kind: "arr"; elems: SymVal[]; len: Num; nullUnder?: Bool<"main"> } + | { kind: "arr"; elems: SymVal[]; len: Num; nullUnder?: Bool<"main">; aliasEntries?: { entry: { fields: Map }; match: Bool<"main"> }[] } | { kind: "obj"; fields: Map; symFields?: Map; v: SymVal }>; nullUnder?: Bool<"main"> } | { kind: "fn"; node: ts.ArrowFunction | ts.FunctionExpression | ts.FunctionDeclaration; env: Frame } | { kind: "null" } @@ -139,6 +139,26 @@ function isStr(v: SymVal | undefined): v is { kind: "str"; t: Str } { function isArr(v: SymVal | undefined): v is Extract { return v !== undefined && v.kind === "arr"; } +function isNumConstV(v: SymVal): boolean { return v.kind === "num" && typeof v.t.c === "number"; } +// Upper bound on a numeric Term from its constant leaves (ite merges of +// concrete lengths, +/- of constants). undefined = unbounded / undecidable. +function maxConcreteLeaf(t: { c?: number | string | boolean; op?: string; kids?: readonly unknown[] }, seen = new Set()): number | undefined { + if (seen.has(t)) return undefined; + seen.add(t); + if (typeof t.c === "number") return t.c; + const kids = (t.kids ?? []) as { c?: number | string | boolean; op?: string; kids?: readonly unknown[] }[]; + if (kids.length === 0) return undefined; // free variable + const op = t.op ?? ""; + if (op === "ite") { + const a = maxConcreteLeaf(kids[1]!, seen), b = maxConcreteLeaf(kids[2]!, seen); + return a === undefined || b === undefined ? undefined : Math.max(a, b); + } + if (kids.every((x) => typeof x.c === "number")) { + if (op === "+") return kids.reduce((acc, x) => acc + (x.c as number), 0); + if (op === "-") return (kids[0]!.c as number) - (kids[1]!.c as number); + } + return undefined; +} function isObj(v: SymVal | undefined): v is Extract { return v !== undefined && v.kind === "obj"; } @@ -163,6 +183,25 @@ function constBool(t: Bool<"main"> | null | undefined): boolean | null { function truthy(ctx: Z3, v: SymVal): Bool<"main"> | null { + // undefUnder: a scalar that is `return undefined` on some path is falsy + // on that path — the guard condition must survive truthiness reads. + const uuNum = (v.kind === "num" || v.kind === "str" || v.kind === "bool") ? (v as { undefUnder?: Bool<"main"> }).undefUnder : undefined; + if (uuNum) { + const core = truthyCore(ctx, v); + if (core === null) return null; + const tc = boolConst.get(uuNum); + const uuOk = tc === undefined ? uuNum.not() : bool(ctx, !tc); + // inline and-fold (module scope has no andFold) + const okC = boolConst.get(uuOk), coreC = boolConst.get(core); + if (okC === true) return core; + if (okC === false || coreC === false) return bool(ctx, false); + if (coreC === true) return uuOk; + return uuOk.and(core); + } + return truthyCore(ctx, v); +} + +function truthyCore(ctx: Z3, v: SymVal): Bool<"main"> | null { switch (v.kind) { case "bool": return v.t; case "num": { @@ -344,15 +383,18 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut * `(or false false)` to `false`, and control-flow checks compare the * printed form against "true"/"false" — so every control Bool must be * built through these to keep literal chains folded. */ - const orFold = (a: Bool<"main"> | null, b: Bool<"main">): Bool<"main"> => { - if (a === null) return b; + const orFold = (a: Bool<"main"> | null | undefined, b: Bool<"main"> | null | undefined): Bool<"main"> | undefined => { + if (a === null || a === undefined) return b ?? undefined; + if (b === null || b === undefined) return a; const ab = boolConst.get(a), bb = boolConst.get(b); if (ab === true || bb === true) return bool(ctx, true); if (ab === false) return b; if (bb === false) return a; return a.or(b); }; - const andFold = (a: Bool<"main">, b: Bool<"main">): Bool<"main"> => { + const andFold = (a: Bool<"main"> | undefined, b: Bool<"main"> | undefined): Bool<"main"> | undefined => { + if (a === undefined) return b; + if (b === undefined) return a; const ab = boolConst.get(a), bb = boolConst.get(b); if (ab === false || bb === false) return bool(ctx, false); if (ab === true) return b; @@ -402,17 +444,28 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut // stability properties across turns need the function property, a fresh // hole per call would fake instability). const numStrMemo = new Map(); + const digitsOnly = (hole: Str): void => { + // String(number) holes must be digit strings (JS prints decimal digits; + // sign/point excluded for the integer-dominant counts we model). Without + // this, an uninterpreted __numstr could model as containing ANY substring + // and fake a contains() violation on every line embedding a count. + const zero = ctx.String.val("0"), nine = ctx.String.val("9"); + const digits = ctx.app1("re.+", ctx.app2("re.range", zero, nine, "Bool"), "Bool"); + ctx.pendingAsums.push(ctx.app2("str.in_re", hole, digits, "Bool")); + }; const numToStringStrFor = (x: Num): { kind: "str"; t: Str } => { const key = x.hash(); const hit = numStrMemo.get(key); if (hit) return hit; const hole = ctx.String.const("__numstr_" + numStrCounter++); + digitsOnly(hole); const v: { kind: "str"; t: Str } = { kind: "str", t: hole }; numStrMemo.set(key, v); return v; }; const numToStringStr = (st: RunState): { kind: "str"; t: Str } => { const hole = ctx.String.const("__numstr_" + numStrCounter++); + digitsOnly(hole); pushAsum(st, (ctx.ToReal(hole.length() as never) as Num).ge(num(ctx, 1)).and((ctx.ToReal(hole.length() as never) as Num).le(num(ctx, 19)))); return { kind: "str", t: hole }; }; @@ -431,6 +484,49 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut ({ kind: "arr", elems, len: len ?? num(ctx, elems.length) }); /** Convert a REAL JSON value (from concrete JSON.parse) into a SymVal. */ + // JSON.parse over a symbolic concat template '{"id":"' X '","role":"' Y + // ',"contentType":"' Z '","text":"' W '"}': walk the concat DAG into a + // linear piece list, match the literal skeleton, return the parsed obj with + // the holes as free fields. Domain: holes contain no '"' / '\\' (asserted), + // so field extraction is exact for the plain-text message domain. + const collectConcats = (t: Str, out: Str[]): void => { + const tt = t as unknown as { op: string; kids?: Str[] }; + const kids = tt.kids; + if (!kids || kids.length === 0) { out.push(t); return; } + const op = tt.op; + if (op === "str.++" || op === "concat") { for (let i = 0; i < kids.length; i++) collectConcats(kids[i]!, out); return; } + if (op === "app2" && kids.length === 3 && (kids[0] as unknown as { op: string }).op === "str.++") { collectConcats(kids[1]!, out); collectConcats(kids[2]!, out); return; } + out.push(t); + }; + const jsonParseTemplate = (a0: { kind: "str"; t: Str }): SymVal | null => { + const pieces: Str[] = []; + collectConcats(a0.t, pieces); + const isLit = (t: Str, v: string): boolean => t.c === v; + if (pieces.length !== 9) return null; + const SKELETON = ['{"id":"', '","role":"', '","contentType":"', '","text":"', '"}']; + for (let i = 0; i < 5; i++) { + const lit = pieces[i * 2]!; + const want = SKELETON[i]!; + if (!isLit(lit, want)) return null; + // value pieces may be free holes OR concrete literals (role/contentType + // are constants in the input model; ids/texts vary) + } + const mk = (hole: Str): { kind: "str"; t: Str } => { + if (hole.c !== undefined) return { kind: "str", t: hole }; // concrete value: no constraint needed + // domain: plain-text fields (no quote/backslash) keep extraction exact + const q = ctx.String.val('"'); + const bs = ctx.String.val("\\"); + ctx.pendingAsums.push(hole.contains(q).not()); + ctx.pendingAsums.push(hole.contains(bs).not()); + return { kind: "str", t: hole }; + }; + const fields = new Map(); + fields.set("id", mk(pieces[1]!)); + fields.set("role", mk(pieces[3]!)); + fields.set("contentType", mk(pieces[5]!)); + fields.set("text", mk(pieces[7]!)); + return { kind: "obj", fields }; + }; const jsonToSymVal = (v: unknown, depth: number): SymVal => { if (v === null) return { kind: "null" }; if (typeof v === "string") return { kind: "str", t: strc(ctx, v) }; @@ -477,6 +573,23 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut // element-access chains (guarded by length checks in real code) must not // poison the merged object. Real out-of-range access throws and is caught // by the replay lock. + // EXCEPTION — scalar returns: a literal {kind:"undef"} arm from a + // `return undefined` guard is a REAL JS return value, not an unwritten + // slot. Merging it away would delete the guard entirely (the witness + // gate bug: ite(cond, undef, n) collapsed to bare n, so every session + // shape "passed" the gate). Track it as undefUnder on the scalar. + if (a.kind === "undef" && (b.kind === "num" || b.kind === "str" || b.kind === "bool")) { + // a (undef) is the THEN arm — live under cond. + const bu = (b as { undefUnder?: Bool<"main"> }).undefUnder; + const uu = bu ? orFold(cond, bu) : cond; + return { ...(b as object), undefUnder: uu } as SymVal; + } + if (b.kind === "undef" && (a.kind === "num" || a.kind === "str" || a.kind === "bool")) { + // b (undef) is the ELSE arm — live under ¬cond. + const au = (a as { undefUnder?: Bool<"main"> }).undefUnder; + const uu = au ? orFold(notFold(cond), au) : notFold(cond); + return { ...(a as object), undefUnder: uu } as SymVal; + } if (a.kind === "undef" && b.kind !== "undef") return mergeVal(cond.not(), b, { kind: "undef" }) === null ? { kind: "any" } : b; if (b.kind === "undef" && a.kind !== "undef") return a; if (a.kind === "null" && b.kind !== "null") return b; @@ -488,19 +601,41 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut case "num": { const bb = b as Extract; const ac = numC(a.t), bc = numC(bb.t); - if (ac !== undefined && ac === bc) return a; // same constant: no ITE + const au = (a as { undefUnder?: Bool<"main"> }).undefUnder, bu = (bb as { undefUnder?: Bool<"main"> }).undefUnder; + // the maybe-undefined tag must SURVIVE value-vs-value merges: the + // miss arm of a fan chain carries undefUnder, and merging it with + // the next slot's real value dropped the tag — `map.get(k) === + // undefined` then folded to const-false and the PHANTOM hit value + // leaked through every subsequent comparison. + // Tagged maybe-undefined scalars merging value-vs-value: the tag + // must NOT be re-attached as a plain or-chain here. Empirically this + // polluted downstream guards with write-path conditions (the #3805 + // render guard folded to write-guard soup). The fan/get sites attach + // exact nullness themselves (acc-chain); tagged-eq consults it. + if (au === undefined && bu === undefined) { + if (ac !== undefined && ac === bc) return a; // same constant: no ITE + return { kind: "num", t: ctx.If(cond, a.t, bb.t) }; + } return { kind: "num", t: ctx.If(cond, a.t, bb.t) }; } case "bool": { const bb = b as Extract; - const ac = boolConst.get(a.t), bc = boolConst.get(bb.t); - if (ac !== undefined && ac === bc) return a; + const auB = (a as { undefUnder?: Bool<"main"> }).undefUnder, buB = (bb as { undefUnder?: Bool<"main"> }).undefUnder; + if (auB === undefined && buB === undefined) { + const ac = boolConst.get(a.t), bc = boolConst.get(bb.t); + if (ac !== undefined && ac === bc) return a; + return { kind: "bool", t: ctx.If(cond, a.t, bb.t) }; + } return { kind: "bool", t: ctx.If(cond, a.t, bb.t) }; } case "str": { const bb = b as Extract; - const ac = strConst.get(a.t), bc = strConst.get(bb.t); - if (ac !== undefined && ac === bc) return a; + const auS = (a as { undefUnder?: Bool<"main"> }).undefUnder, buS = (bb as { undefUnder?: Bool<"main"> }).undefUnder; + if (auS === undefined && buS === undefined) { + const ac = strConst.get(a.t), bc = strConst.get(bb.t); + if (ac !== undefined && ac === bc) return a; + return { kind: "str", t: ctx.If(cond, a.t, bb.t) }; + } return { kind: "str", t: ctx.If(cond, a.t, bb.t) }; } case "null": case "undef": return a; @@ -510,9 +645,17 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut const n = Math.max(a.elems.length, bb.elems.length); const elems: SymVal[] = []; for (let i = 0; i < n; i++) { - // missing slots merge as undef (undef loses to concrete values in - // mergeVal) so growing arrays keep their element structure - elems.push(mergeVal(cond, a.elems[i] ?? { kind: "undef" }, bb.elems[i] ?? { kind: "undef" })); + const va = a.elems[i], vb = bb.elems[i]; + if (va === undefined && vb === undefined) { elems.push({ kind: "undef" }); continue; } + // ONE-ARM slots must carry the branch guard inside the value + // (guardedNeutral): the old undef-loses collapse made the arm's + // value reachable unconditionally — a ghost arm the solver could + // pick even when that branch never executed (false SAT, e.g. the + // view-latch pass-through property). Reads are len-guarded AND + // the value now carries ¬cond → its neutral. + if (vb === undefined) { elems.push(guardedNeutral(cond, va)); continue; } + if (va === undefined) { elems.push(guardedNeutral(cond.not()!, vb)); continue; } + elems.push(mergeVal(cond, va, vb)); } const la = numC(a.len), lb = numC(bb.len); if (la !== undefined && la === lb) return { kind: "arr", elems, len: a.len }; // same length: keep const @@ -540,13 +683,23 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut symFields = new Map; v: SymVal }>(); const seen = new Set(); const bothArm = (ga: Bool<"main">, va: SymVal, gb: Bool<"main">, vb: SymVal): { g: Bool<"main">; v: SymVal } => { - const gT = andFold(cond, ga), gE = andFold(cond.not(), gb); - const gLive = orFold(gT, gE); - if (gLive === null) return { g: bool(ctx, false), v: va }; + // An entry present on BOTH arms keeps the disjunction of the + // arm-guarded write guards, and the merge-level condition goes + // into the VALUE only. Entries often exist on both sides with + // the SAME write guard (created in an earlier iteration): the + // old form (g=true, v=ite(gT,va,ite(gE,vb,neutral))) erased that + // guard and degraded the else-arm to a neutral value, losing + // the pre-write binding (3-message refs.ts repro). + const gT = andFold(cond, ga) ?? bool(ctx, false), gE = andFold(cond.not(), gb) ?? bool(ctx, false); + const gLive = orFold(gT, gE) ?? bool(ctx, false); + if (constBool(gLive) === false) return { g: bool(ctx, false), v: va }; let v: SymVal; - if (constBool(gT) === true) v = va; - else if (constBool(gE) === true) v = vb; - else v = iteVal(gT, va, iteVal(gE, vb, neutralFor(vb))); + if (constBool(cond) === true) v = va; + else if (constBool(cond) === false) v = vb; + else v = iteVal(cond, va, vb); + // No double-count: the fan tests `key ∧ g` (g already includes + // cond via gT/gE) and then reads v = ite(cond, va, vb) — the + // truthiness channel and the value channel agree. return { g: gLive, v }; }; if (sa) for (const [kt, ea] of sa) { @@ -559,9 +712,24 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut symFields.set(kt, { g: andFold(cond.not(), eb.g)!, v: eb.v }); } } - return { kind: "obj", fields, ...(symFields ? { symFields } : {}) }; + // nullUnder merges across arms: the merged entry is null-ish exactly + // when the LIVE arm is null-ish (a guarded tail entry merged with a + // real entry must keep "maybe absent" on its own side — dropping it + // resurrected cross-epoch phantom keys in map-entry slot merges). + const tTrue = bool(ctx, true); + const au = (a as { nullUnder?: Bool<"main"> }).nullUnder; + const bu = (bb as { nullUnder?: Bool<"main"> }).nullUnder; + let nullUnder: Bool<"main"> | undefined; + if (au || bu) { + const auT = andFold(cond, au ?? tTrue); + const buT = andFold(cond.not(), bu ?? tTrue); + nullUnder = orFold(auT, buT) ?? undefined; + } + return { kind: "obj", fields, ...(symFields ? { symFields } : {}), ...(nullUnder ? { nullUnder } : {}) }; + } + default: { + return { kind: "any" }; } - default: return { kind: "any" }; } }; @@ -626,6 +794,10 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut const jsOr = (a: SymVal, b: () => SymVal): SymVal => { const t = truthy(ctx, a); if (t === null) return { kind: "any" }; + // JS || short-circuits: provably-true left → a (right never evaluated). + const tf = constBool(t); + if (tf === true) return a.kind === "bool" ? { kind: "bool", t } : a; + if (tf === false) return b(); const av = a; const bv = b(); // (a || b): if a truthy → a else b @@ -633,6 +805,12 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut }; const jsAnd = (a: SymVal, b: () => SymVal): SymVal => { const t = truthy(ctx, a); + // JS && short-circuits: a provably-false left operand must NOT evaluate + // the right (the whole reason dead guards like Array.isArray(x) && x.some(...) + // stay translatable without touching the right side's degradations). + const tf = t === null ? null : constBool(t); + if (tf === false) return a.kind === "bool" ? a : iteVal(t as never, a, a); + if (tf === true) return b(); if (t !== null) { const bb = b(); // Prefer exact truthiness when both sides coerce: `(a && b)` in a @@ -640,10 +818,11 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut // (`a && b` evaluating to `a` when falsy) is only observable via // value-equality/aliasing patterns absent from the guarded subset. const tbv = truthy(ctx, bb); - if (tbv !== null) return { kind: "bool", t: andFold(t, tbv) }; + if (tbv !== null) return { kind: "bool", t: andFold(t, tbv) ?? bool(ctx, false) }; + if (process.env.VERIF_AND_DEBUG) console.log(`[and-any] a=${a.kind} b=${bb.kind} tnull=${String(t === null)} tbnull=${String(tbv === null)}`); return iteVal(t, bb, a.kind === "bool" ? { kind: "bool", t: t } : a); } - if (t === null) return { kind: "any" }; + if (t === null) { if (process.env.VERIF_AND_DEBUG) console.log(`[and-any-left] a=${a.kind}`); return { kind: "any" }; } return iteVal(t, b(), a.kind === "bool" ? { kind: "bool", t: t } : a); }; @@ -669,13 +848,15 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut * continue-if in the same block) guard correctly. */ const execSeq = (stmts: readonly ts.Statement[], frame: Frame, st: RunState, depth: number): void => { for (let i = 0; i < stmts.length; i++) { + if (process.env.VERIF_SEQ_DEBUG) console.log(`[seq] line=${sf.getLineAndCharacterOfPosition(stmts[i]!.getStart(sf)).line + 1} kind=${ts.SyntaxKind[stmts[i]!.kind]} pr=${st.pendingRets.length} hard=${st.hardRet}`); execStmt(stmts[i]!, frame, st, depth); const bc = st.breakCond; const cc = st.continueCond; const bcT = bc !== null && constBool(bc) !== false; const ccT = cc !== null && constBool(cc) !== false; if (bcT || ccT) { - const stopCond = bc && cc ? orFold(bc as Bool<"main">, cc as Bool<"main">) : ((bc ?? cc) as Bool<"main">); + const stopCond = (bc && cc ? orFold(bc as Bool<"main">, cc as Bool<"main">) : ((bc ?? cc) as Bool<"main">)) as Bool<"main">; + if (process.env.VERIF_SEQ_DEBUG) console.log(`[seq-stop] after line=${sf.getLineAndCharacterOfPosition(stmts[i]!.getStart(sf)).line + 1} stopConst=${constBool(stopCond)} rest=${stmts.length - i - 1} hard=${st.hardRet} thrownTrue=${constBool(st.thrown) === true} bcTerm=${(stopCond as unknown as { c?: unknown }).c ?? (stopCond as unknown as { op?: unknown }).op} ccT=${ccT}`); if (i + 1 < stmts.length && constBool(stopCond) !== true) { const f2: Frame = { vars: new Map(frame.vars) }; const s2: RunState = { ...st, ret: null, pendingRets: [], hardRet: false, thrown: null, breakCond: null, continueCond: null, assumptions: st.assumptions, degradations: st.degradations }; @@ -700,7 +881,9 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut if (ts.isTypeAliasDeclaration(stmt) || ts.isInterfaceDeclaration(stmt) || ts.isEnumDeclaration(stmt)) return; // type-level only — erased if (ts.isVariableStatement(stmt)) { for (const d of stmt.declarationList.declarations) { - const init = d.initializer ? evalExpr(d.initializer, frame, st, depth) : { kind: "undef" as const }; + const init0 = d.initializer ? evalExpr(d.initializer, frame, st, depth) : { kind: "undef" as const }; + if (init0 === undefined) console.log("[INIT-UNDEFINED] line", lineOf(d), ts.isIdentifier(d.name) ? d.name.text : "pattern"); + const init: SymVal = init0 === undefined ? { kind: "any" } : init0; if (ts.isIdentifier(d.name)) { frame.vars.set(d.name.text, init); } else if (ts.isObjectBindingPattern(d.name)) { @@ -728,6 +911,7 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut if (ts.isReturnStatement(stmt)) { if (st.ret === null) { st.ret = stmt.expression ? evalExpr(stmt.expression, frame, st, depth) : { kind: "undef" }; + st.hardRet = true; } else { // a pending conditional return already captured the tail; a second @@ -745,12 +929,19 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut if (ts.isIfStatement(stmt)) { const c = evalExpr(stmt.expression, frame, st, depth); const tb = truthy(ctx, c); - if (tb === null) { degrade(st, stmt, "if condition degraded"); return; } + if (tb === null) { + if (process.env.VERIF_IF_DEBUG) { + const cond = stmt.expression as unknown as { left?: unknown; operatorToken?: { kind: number }; kind: number }; + const sub = cond.left ? `leftKind=${(cond.left as { kind?: number }).kind}` : "n/a"; + console.log(`[if-degr] c.kind=${c.kind} ${sub}`); + } + degrade(st, stmt, "if condition degraded"); return; + } // Dead-branch pruning: a constant condition executes only the live // arm, in-frame. Eager both-arm execution would force provably-dead // code (e.g. a regex in a never-taken branch) to be translatable. const cb = constBool(tb); - if (cb === true) { execStmt(stmt.thenStatement, frame, st, depth); return; } + if (cb === true) { if (process.env.VERIF_SEQ_DEBUG) console.log(`[if-live] line=${sf.getLineAndCharacterOfPosition(stmt.getStart(sf)).line + 1}`); execStmt(stmt.thenStatement, frame, st, depth); return; } if (cb === false) { if (stmt.elseStatement) execStmt(stmt.elseStatement, frame, st, depth); return; } const thenFrame: Frame = { vars: new Map(frame.vars) }; const thenState: RunState = { ...st, ret: null, pendingRets: [], hardRet: false, thrown: null, breakCond: null, continueCond: null, assumptions: st.assumptions, degradations: st.degradations }; @@ -781,7 +972,14 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut const lenC = numC(iter.len); const slotCeil = lenC !== undefined ? Math.min(iter.elems.length, Math.ceil(lenC)) : iter.elems.length; for (let k = 0; k < slotCeil; k++) { - const guard = andFold(iter.len.gt(num(ctx, k)), st.breakCond ?? bool(ctx, true)); + // Iteration k runs only if the array is long enough AND the loop + // has not already exited via break — `breakCond` accumulates + // "exited by an earlier iteration", so the guard CONJOINS ITS + // NEGATION. Conjoining the bare flag let iteration k+1 overwrite + // loop-carried writes from a broken iteration k (snapshot-render + // chain bySig candidates: the `first idx < cursor` pick loop + // returned the SECOND candidate after breaking on the first). + const guard = andFold(iter.len.gt(num(ctx, k)), st.breakCond ? notFold(st.breakCond) : bool(ctx, true)) ?? bool(ctx, true); const prBefore = st.pendingRets.length; const elemFrame: Frame = { vars: new Map(frame.vars) }; const elemState: RunState = { ...st, ret: null, pendingRets: [], hardRet: false, thrown: null, breakCond: null, continueCond: null, assumptions: st.assumptions, degradations: st.degradations }; @@ -789,16 +987,36 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut execStmt(stmt.statement, elemFrame, elemState, depth); mergeBranch(frame, st, guard, elemFrame, elemState, null, null); st.continueCond = null; // continue ends only this iteration - if (st.hardRet || constBool(st.thrown) === true || constBool(st.breakCond) === true) break; + if (st.hardRet || constBool(st.thrown) === true || constBool(st.breakCond) === true) { + // break/continue terminate only THIS loop — see the tb-site note. + if (process.env.VERIF_SEQ_DEBUG) console.log(`[forof-hard-exit] line=${sf.getLineAndCharacterOfPosition(stmt.getStart(sf)).line + 1} hardRet=${st.hardRet} thrown=${String(st.thrown)} bc=${String(st.breakCond)}`); + st.breakCond = null; + st.continueCond = null; + break; + } // A conditional return/throw fired under this iteration's guard: a // real execution would have left the whole function — treat it as a // loop exit (post-loop statements run under ¬exit via rest-on-copy). + // The exit condition is the RETURN'S OWN condition (guard ∧ inner), + // NOT the bare guard: conflating them made guard-folded-true loops + // (len const > 0) drop every post-loop statement unconditionally — + // the fall-through path (return not taken) became unreachable and + // the function's final return vanished (snapshot-render buildView + // lost its success arm entirely, zero degradations). if (st.pendingRets.length > prBefore || (st.thrown !== null && constBool(st.thrown) === undefined)) { - st.breakCond = orFold(st.breakCond, guard); + let exitCond: Bool<"main"> | null = null; + for (let pi = prBefore; pi < st.pendingRets.length; pi++) exitCond = orFold(exitCond, st.pendingRets[pi]!.cond) ?? null; + if (st.thrown !== null && constBool(st.thrown) === undefined) exitCond = orFold(exitCond, guard) ?? null; + st.breakCond = orFold(st.breakCond, exitCond) ?? null; break; } } - // Elements beyond the declared length never execute: nothing needed. + // Elements beyond the declared length never execute. The loop is the + // target of any break/continue inside it — consume them here so the + // enclosing block's rest-on-copy does not guard post-loop statements + // with a stale inner-break condition. + st.breakCond = null; + st.continueCond = null; return; } if (ts.isForStatement(stmt)) { @@ -821,20 +1039,54 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut // execute the body (dead iterations pollute live state). if (constBool(tb) === false) { pushAsum(st, bool(ctx, true)); + // the loop is the target of any break/continue inside it — + // consumed here, never propagated to the enclosing block + st.breakCond = null; + st.continueCond = null; return; } - const iterFrame: Frame = { vars: new Map(frame.vars) }; + // Ghost-iteration pruning: k concrete + bounded length term ⇒ + // iterations past the max reachable length never execute. + if (stmt.condition && ts.isBinaryExpression(stmt.condition)) { + const cond = stmt.condition; + const rhsTerm = evalExpr(cond.right, frame, st, depth); + if (rhsTerm.kind === "num") { + const maxL = maxConcreteLeaf(rhsTerm.t); + const lhsV = ts.isIdentifier(cond.left) ? frame.vars.get(cond.left.text) : undefined; + const kC = lhsV && lhsV.kind === "num" ? numC(lhsV.t) : undefined; + if (maxL !== undefined && kC !== undefined) { + const ops = cond.operatorToken.kind; + const dead = (ops === ts.SyntaxKind.LessThanToken && kC >= maxL) || (ops === ts.SyntaxKind.LessThanEqualsToken && kC > maxL); + if (dead) { pushAsum(st, bool(ctx, true)); st.breakCond = null; st.continueCond = null; return; } + } + } + } const prBefore = st.pendingRets.length; + const iterFrame: Frame = { vars: new Map(frame.vars) }; const iterState: RunState = { ...st, ret: null, pendingRets: [], hardRet: false, thrown: null, breakCond: null, continueCond: null, assumptions: st.assumptions, degradations: st.degradations }; execStmt(stmt.statement, iterFrame, iterState, depth); // continue: still run the increment — handled by running increment // on iterFrame after body (continue skips rest of body only). if (stmt.incrementor) evalExpr(stmt.incrementor, iterFrame, iterState, depth); - mergeBranch(frame, st, tb, iterFrame, iterState, null, null); + // post-break iterations never run: gate the merge by ¬break-so-far + mergeBranch(frame, st, andFold(tb, st.breakCond ? notFold(st.breakCond) : bool(ctx, true)) as Bool<"main">, iterFrame, iterState, null, null); st.continueCond = null; // continue ends only this iteration - if (st.hardRet || constBool(st.thrown) === true || constBool(st.breakCond) === true) return; + if (st.hardRet || constBool(st.thrown) === true || constBool(st.breakCond) === true) { + // break/continue terminate only THIS loop — the loop construct + // consumes them, and post-loop statements run unconditionally. + // Leaving the flag set made execSeq treat the rest of the block + // as unreachable (stopCond === "true") and drop the function's + // final return entirely (ret null with zero degradations). + st.breakCond = null; + st.continueCond = null; + return; + } if (st.pendingRets.length > prBefore || (st.thrown !== null && constBool(st.thrown) === undefined)) { - st.breakCond = orFold(st.breakCond, tb as Bool<"main">); + // exit condition = the return's own condition (tb ∧ inner), not bare tb — see the for-of note. + let exitCond: Bool<"main"> | null = null; + for (let pi = prBefore; pi < st.pendingRets.length; pi++) exitCond = orFold(exitCond, st.pendingRets[pi]!.cond) ?? null; + if (st.thrown !== null && constBool(st.thrown) === undefined) exitCond = orFold(exitCond, tb as Bool<"main">) ?? null; + st.breakCond = orFold(st.breakCond, exitCond) ?? null; return; } } @@ -842,6 +1094,8 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut const c = stmt.condition ? evalExpr(stmt.condition, frame, st, depth) : null; const tb = c ? truthy(ctx, c) : bool(ctx, true); if (tb) pushAsum(st, notFold(tb as Bool<"main">)); + st.breakCond = null; + st.continueCond = null; return; } if (ts.isWhileStatement(stmt)) { @@ -849,21 +1103,36 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut const c = evalExpr(stmt.expression, frame, st, depth); const tb = truthy(ctx, c); if (tb === null) { degrade(st, stmt, "while condition degraded"); return; } - const iterFrame: Frame = { vars: new Map(frame.vars) }; const prBefore = st.pendingRets.length; + const iterFrame: Frame = { vars: new Map(frame.vars) }; const iterState: RunState = { ...st, ret: null, pendingRets: [], hardRet: false, thrown: null, breakCond: null, continueCond: null, assumptions: st.assumptions, degradations: st.degradations }; execStmt(stmt.statement, iterFrame, iterState, depth); - mergeBranch(frame, st, tb, iterFrame, iterState, null, null); + mergeBranch(frame, st, andFold(tb, st.breakCond ? notFold(st.breakCond) : bool(ctx, true)) as Bool<"main">, iterFrame, iterState, null, null); st.continueCond = null; // continue ends only this iteration - if (st.hardRet || constBool(st.thrown) === true || constBool(st.breakCond) === true) return; + if (st.hardRet || constBool(st.thrown) === true || constBool(st.breakCond) === true) { + // break/continue terminate only THIS loop — the loop construct + // consumes them, and post-loop statements run unconditionally. + // Leaving the flag set made execSeq treat the rest of the block + // as unreachable (stopCond === "true") and drop the function's + // final return entirely (ret null with zero degradations). + st.breakCond = null; + st.continueCond = null; + return; + } if (st.pendingRets.length > prBefore || (st.thrown !== null && constBool(st.thrown) === undefined)) { - st.breakCond = orFold(st.breakCond, tb as Bool<"main">); + // exit condition = the return's own condition (tb ∧ inner), not bare tb — see the for-of note. + let exitCond: Bool<"main"> | null = null; + for (let pi = prBefore; pi < st.pendingRets.length; pi++) exitCond = orFold(exitCond, st.pendingRets[pi]!.cond) ?? null; + if (st.thrown !== null && constBool(st.thrown) === undefined) exitCond = orFold(exitCond, tb as Bool<"main">) ?? null; + st.breakCond = orFold(st.breakCond, exitCond) ?? null; return; } } const c = evalExpr(stmt.expression, frame, st, depth); const tb = truthy(ctx, c); if (tb) pushAsum(st, notFold(tb as Bool<"main">)); + st.breakCond = null; + st.continueCond = null; return; } if (ts.isBreakStatement(stmt)) { st.breakCond = bool(ctx, true); return; } @@ -876,6 +1145,18 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut // false) are never executed — eager execution of provably-dead bodies // wasted terms and forced untranslatable code into the model. const disc = evalExpr(stmt.expression, frame, st, depth); + // Clauses must start from the SAME pre-switch state. Copying from the + // accumulated (already-merged) frame let each later arm see earlier + // arms' pushes — arm N's msgs.push landed at a DIFFERENT slot than + // arm N-1's, and the iteration merge then fused two arms' objects + // into one field-union ghost (anthropic parse: thinking msg merged + // with tool_use msg, text degraded to any). + const flat = (xs: readonly ts.Statement[]): ts.Statement[] => xs.flatMap(x => ts.isBlock(x) ? flat(x.statements) : [x]); + // Empty labels (`case "a": case "b": {…}`) are grouping, not fallthrough. + const hasFallthrough = stmt.caseBlock.clauses.some((c, ci) => + ci < stmt.caseBlock.clauses.length - 1 && c.statements.length > 0 && !flat(c.statements).some(x => ts.isBreakStatement(x) || ts.isReturnStatement(x) || ts.isThrowStatement(x))); + if (hasFallthrough) degrade(st, stmt, "switch fallthrough (no break) not modeled"); + const base: Frame = { vars: new Map(frame.vars) }; let matched = bool(ctx, false); // some earlier case compared equal let stopped: Bool<"main"> | null = null; // an entered clause broke out for (const clause of stmt.caseBlock.clauses) { @@ -884,18 +1165,18 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut const cv = evalExpr(clause.expression, frame, st, depth); const eq = valEq(disc, cv); if (eq === null) { - bad(clause.expression, "case comparison degraded"); + degrade(st, clause.expression, "case comparison degraded"); matched = bool(ctx, true); // unconstrained — conservative over-approx enter = bool(ctx, true); } else { - matched = orFold(matched, eq); - enter = eq; + matched = orFold(matched, eq) ?? bool(ctx, false); + enter = eq ?? bool(ctx, true); } } else { enter = notFold(matched); } - if (stopped) enter = andFold(enter, notFold(stopped)); - const cf: Frame = { vars: new Map(frame.vars) }; + if (stopped) enter = andFold(enter, notFold(stopped)) ?? enter; + const cf: Frame = { vars: new Map(base.vars) }; const cs: RunState = { ...st, ret: null, pendingRets: [], hardRet: false, thrown: null, breakCond: null, continueCond: null, assumptions: st.assumptions, degradations: st.degradations }; const enterC = constBool(enter); if (enterC === false) continue; // dead clause: never executed @@ -908,16 +1189,16 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut if (enterC === true) { // Live clause: apply in-frame (no ITE wrapper needed). for (const [k2, v] of cf.vars) frame.vars.set(k2, v); - st.ret = cs.ret ?? st.ret; st.hardRet = st.hardRet || cs.hardRet; + const __cs_ret = cs.ret ?? st.ret; st.ret = __cs_ret as SymVal | null; st.hardRet = st.hardRet || cs.hardRet; st.thrown = cs.thrown ?? st.thrown; - st.breakCond = st.breakCond ? orFold(st.breakCond, cs.breakCond ?? bool(ctx, false)) : cs.breakCond; - st.continueCond = st.continueCond ? orFold(st.continueCond, cs.continueCond ?? bool(ctx, false)) : cs.continueCond; + st.breakCond = st.breakCond ? (orFold(st.breakCond, cs.breakCond ?? bool(ctx, false)) ?? null) : cs.breakCond; + st.continueCond = st.continueCond ? (orFold(st.continueCond, cs.continueCond ?? bool(ctx, false)) ?? null) : cs.continueCond; st.pendingRets.push(...cs.pendingRets); if (constBool(broke) === true) stopped = bool(ctx, true); if (st.hardRet || constBool(st.thrown) === true) return; continue; } - if (broke) stopped = orFold(stopped ?? bool(ctx, false), andFold(enter, broke)); + if (broke) stopped = orFold(stopped ?? bool(ctx, false), andFold(enter, broke) ?? bool(ctx, false)) ?? bool(ctx, false); mergeBranch(frame, st, enter, cf, cs, null, null); if (st.hardRet || constBool(st.thrown) === true) return; } @@ -937,9 +1218,9 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut if (stmt.finallyBlock) execStmt(stmt.finallyBlock, tf, ts_, depth); // merge try-path back unconditionally for (const [k2, v] of tf.vars) frame.vars.set(k2, v); - st.ret = ts_.ret ?? st.ret; st.hardRet = st.hardRet || ts_.hardRet; st.thrown = ts_.thrown ?? st.thrown; - st.breakCond = st.breakCond ? orFold(st.breakCond, ts_.breakCond ?? bool(ctx, false)) : ts_.breakCond; - st.continueCond = st.continueCond ? orFold(st.continueCond, ts_.continueCond ?? bool(ctx, false)) : ts_.continueCond; + const __try_ret = ts_.ret ?? st.ret; st.ret = __try_ret as SymVal | null; st.hardRet = st.hardRet || ts_.hardRet; st.thrown = ts_.thrown ?? st.thrown; + st.breakCond = st.breakCond ? (orFold(st.breakCond, ts_.breakCond ?? bool(ctx, false)) ?? null) : ts_.breakCond; + st.continueCond = st.continueCond ? (orFold(st.continueCond, ts_.continueCond ?? bool(ctx, false)) ?? null) : ts_.continueCond; st.pendingRets.push(...ts_.pendingRets); // shared accumulators — no merge needed return; @@ -981,8 +1262,7 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut st.seenDeg.add(key); st.degradations.push(key); }; - - const mergeBranch = ( +const mergeBranch = ( frame: Frame, st: RunState, cond: Bool<"main">, @@ -1024,12 +1304,12 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut // them under the same if would DROP them: the while-loop early-return // detection compares pendingRets counts and never fires; the loop then // unrolls to exhaustion and the post-loop `throw` poisons the caller. - for (const pr of thenState.pendingRets) st.pendingRets.push({ cond: andFold(cond, pr.cond), val: pr.val }); + for (const pr of thenState.pendingRets) st.pendingRets.push({ cond: andFold(cond, pr.cond) ?? bool(ctx, false), val: pr.val }); if (elseState && (elseState.hardRet || elseState.ret !== null)) { const ev = elseState.ret ?? { kind: "undef" }; st.pendingRets.push({ cond: notFold(cond), val: ev }); } - for (const pr of elseState?.pendingRets ?? []) st.pendingRets.push({ cond: andFold(notFold(cond), pr.cond), val: pr.val }); + for (const pr of elseState?.pendingRets ?? []) st.pendingRets.push({ cond: andFold(notFold(cond), pr.cond) ?? bool(ctx, false), val: pr.val }); // Flags merge — normalize provably-false flags back to null: a // const-false TERM is not nullish, and every consumer does // `st.flag ?? default`; a lingering false term flips loop guards @@ -1039,17 +1319,17 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut const tB = tThrown ?? bool(ctx, false); const eB = eThrown ?? bool(ctx, false); const mergedThrow = st.thrown ?? bool(ctx, false); - const nt = orFold(orFold(mergedThrow, andFold(cond, tB)), andFold(notFold(cond), eB)); + const nt = orFold(orFold(mergedThrow, andFold(cond, tB) ?? bool(ctx, false)) ?? bool(ctx, false), andFold(notFold(cond), eB) ?? bool(ctx, false)) ?? bool(ctx, false); st.thrown = constBool(nt) === false ? null : nt; const tBrk = thenState.breakCond ?? null; const eBrk = elseState?.breakCond ?? null; const mergedBrk = st.breakCond ?? bool(ctx, false); - const nb = orFold(orFold(mergedBrk, tBrk ? andFold(cond, tBrk) : bool(ctx, false)), eBrk ? andFold(notFold(cond), eBrk) : bool(ctx, false)); + const nb = orFold(orFold(mergedBrk, tBrk ? (andFold(cond, tBrk) ?? bool(ctx, false)) : bool(ctx, false)) ?? bool(ctx, false), eBrk ? (andFold(notFold(cond), eBrk) ?? bool(ctx, false)) : bool(ctx, false)) ?? bool(ctx, false); st.breakCond = constBool(nb) === false ? null : nb; const tCnt = thenState.continueCond ?? null; const eCnt = elseState?.continueCond ?? null; const mergedCnt = st.continueCond ?? bool(ctx, false); - const nc = orFold(orFold(mergedCnt, tCnt ? andFold(cond, tCnt) : bool(ctx, false)), eCnt ? andFold(notFold(cond), eCnt) : bool(ctx, false)); + const nc = orFold(orFold(mergedCnt, tCnt ? (andFold(cond, tCnt) ?? bool(ctx, false)) : bool(ctx, false)) ?? bool(ctx, false), eCnt ? (andFold(notFold(cond), eCnt) ?? bool(ctx, false)) : bool(ctx, false)) ?? bool(ctx, false); st.continueCond = constBool(nc) === false ? null : nc; const pushAll = (dst: T[], src: T[]): void => { if (src !== dst) for (const x of src) dst.push(x); }; pushAll(st.assumptions, thenState.assumptions); if (elseState) pushAll(st.assumptions, elseState.assumptions); @@ -1057,6 +1337,13 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut }; const valEq = (a: SymVal, b: SymVal): Bool<"main"> | null => { + // undefined / null never equal anything: a switch over an out-of-range + // slot read (phantom tail of a symbolic-length array) must simply not + // match its cases — NOT degrade the whole statement. + const ka: string = (a as { kind: string }).kind, kb: string = (b as { kind: string }).kind; + if (ka === "undef" || ka === "null" || kb === "undef" || kb === "null") { + return bool(ctx, ka === kb); + } if (isNum(a) && isNum(b)) { const ac = numC(a.t), bc = numC(b.t); if (ac !== undefined && bc !== undefined) return bool(ctx, ac === bc); @@ -1120,6 +1407,10 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut if (s !== undefined) return { kind: "str", t: strc(ctx, s) }; const arrow = mi.constArrowFns.get(expr.text) ?? info.constArrowFns.get(expr.text); if (arrow) return evalExpr(arrow, frame, st, depth); + // module-level function declaration used as a VALUE (filter(fnRef)): + // hand out a closure; curInfo() during its body resolves its module. + const mdFn = mi.functions.get(expr.text) ?? info.functions.get(expr.text); + if (mdFn && mdFn.body) return { kind: "fn", node: mdFn, env: { vars: new Map() } }; const binding = mi.constBindings.get(expr.text) ?? info.constBindings.get(expr.text); if (binding) return evalExpr(binding, frame, st, depth); const imported = importedSymbols.get(expr.text); @@ -1142,16 +1433,12 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut } if (ts.isTypeOfExpression(expr)) { const v = evalExpr(expr.expression, frame, st, depth); - switch (v.kind) { - case "str": return { kind: "str", t: ctx.String.val("string") }; - case "num": return { kind: "str", t: ctx.String.val("number") }; - case "bool": return { kind: "str", t: ctx.String.val("boolean") }; - case "obj": case "null": return { kind: "str", t: ctx.String.val("object") }; - case "arr": return { kind: "str", t: ctx.String.val("object") }; - case "fn": return { kind: "str", t: ctx.String.val("function") }; - case "undef": return { kind: "str", t: ctx.String.val("undefined") }; - default: degrade(st, expr, "typeof on degraded value"); return { kind: "any" }; - } + const uu = (v.kind === "num" || v.kind === "str" || v.kind === "bool") ? (v as { undefUnder?: Bool<"main"> }).undefUnder : undefined; + const base = v.kind === "str" ? "string" : v.kind === "num" ? "number" : v.kind === "bool" ? "boolean" + : v.kind === "obj" || v.kind === "null" || v.kind === "arr" ? "object" + : v.kind === "fn" ? "function" : "undefined"; + if (uu) return { kind: "str", t: ctx.If(uu, ctx.String.val("undefined"), ctx.String.val(base)) } as SymVal; + return { kind: "str", t: ctx.String.val(base) }; } if (ts.isPrefixUnaryExpression(expr)) { if (expr.operator === ts.SyntaxKind.ExclamationToken) { @@ -1186,6 +1473,7 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut // Content-irrelevant for structural properties; replay lock still // validates any witness end-to-end. const hole = ctx.String.const("__numstr_" + numHoleCounter++); + digitsOnly(hole); pushAsum(st, (ctx.ToReal(hole.length() as never) as Num).ge(num(ctx, 1)).and((ctx.ToReal(hole.length() as never) as Num).le(num(ctx, 8)))); v = { kind: "str", t: hole }; } @@ -1228,18 +1516,20 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut for (const [k2, v2] of v.fields) fields.set(k2, v2); if (v.symFields) symFields = new Map(v.symFields); } - else degrade(st, prop, "object spread of non-object"); } else if (ts.isPropertyAssignment(prop) && ts.isIdentifier(prop.name)) { - fields.set(prop.name.text, evalExpr(prop.initializer, frame, st, depth)); + const pv = evalExpr(prop.initializer, frame, st, depth); + fields.set(prop.name.text, pv); } else if (ts.isShorthandPropertyAssignment(prop)) { - fields.set(prop.name.text, evalExpr(prop.name, frame, st, depth)); + const sv0 = evalExpr(prop.name, frame, st, depth); + const sv = sv0 === undefined || sv0.kind === undefined ? (() => { console.log(`[AT-UNDEF] shorthand ${prop.name.text}:`, JSON.stringify(frame.vars.get(prop.name.text), (k, v) => (k === "kids" ? "…" : v))?.slice(0, 220)); return { kind: "any" as const }; })() : sv0; + fields.set(prop.name.text, sv); } else { degrade(st, prop, "object literal member"); } } return { kind: "obj", fields, ...(symFields ? { symFields } : {}) }; } - if (ts.isPropertyAccessExpression(expr)) { + if (ts.isPropertyAccessExpression(expr)) { const obj = evalExpr(expr.expression, frame, st, depth); return member(obj, expr.name.text, expr, st); } @@ -1359,6 +1649,14 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut return { kind: "any" }; } if (isObj(obj)) { + // A merged-away object (nullUnder) must not hand out its surviving + // side's fields unconditionally — gate every read by ¬nullUnder. + const nu = (obj as { nullUnder?: Bool<"main"> }).nullUnder; + if (nu) { + const raw = member({ ...obj, nullUnder: undefined } as SymVal, name, at, st); + if (raw.kind === "any") return raw; + return iteVal(nu.not()!, raw, { kind: "undef" }); + } // Map/Set .size (property, not method) if (name === "size" && (obj.fields.has("__map_entries") || obj.fields.has("__set_values"))) { const e = obj.fields.get("__map_entries") ?? obj.fields.get("__set_values"); @@ -1371,7 +1669,7 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut return v ?? { kind: "undef" }; } if (obj.kind === "null" || obj.kind === "undef") return { kind: "undef" }; - return { kind: "any" }; + return bad(at, `member on ${obj.kind}`); }; @@ -1398,7 +1696,7 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut // hand out values for keys that never matched (phantom reads). let acc: SymVal = { kind: "undef" }; const fanLayer = (c: Bool<"main">, v: SymVal, accv: SymVal): SymVal => { - if (accv.kind === "undef" || accv.kind === "null") return iteVal(c, v, neutralFor(v)); + if (accv.kind === "undef" || accv.kind === "null") return iteStrict(c, v, neutralFor(v)); return iteVal(c, v, accv); }; for (const [key, v] of obj.fields) { @@ -1425,8 +1723,33 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut * — so chained stores must rewrite every container on the path. */ const writeLValue = (target: ts.Expression, value: SymVal, frame: Frame, st: RunState, depth: number): boolean => { if (ts.isNonNullExpression(target)) return writeLValue(target.expression, value, frame, st, depth); + // (x as T).field = v / ((x)).field = v — unwrap casts and parens + if (ts.isParenthesizedExpression(target) || ts.isAsExpression(target)) return writeLValue(target.expression, value, frame, st, depth); if (ts.isIdentifier(target)) { frame.vars.set(target.text, value); return true; } if (ts.isPropertyAccessExpression(target)) { + // A[i].f = v with symbolic i: the read A[i] produces a fan over all + // slots; mutating the fan and writing it back into every slot fuses + // the slot identities into one field-union ghost AND (for plain + // A[i] = v chains) grows len unboundedly under loop unrolling. + // Correct semantics: distribute the field write per slot, guarded by + // i == k, leaving len untouched (a field write never grows an array). + let eaInner: ts.Expression = target.expression; + while (ts.isAsExpression(eaInner) || ts.isParenthesizedExpression(eaInner) || ts.isNonNullExpression(eaInner)) eaInner = eaInner.expression; + if (ts.isElementAccessExpression(eaInner) && !(eaInner.questionDotToken)) { + const rootArr = evalExpr(eaInner.expression, frame, st, depth); + const idxV = evalExpr(eaInner.argumentExpression, frame, st, depth); + if (isArr(rootArr) && isNum(idxV) && !isNumConstV(idxV)) { + const fname = target.name.text; + const elems = rootArr.elems.map((e, k) => { + const withField: SymVal = isObj(e) + ? { kind: "obj", fields: new Map([...e.fields, [fname, value]]), ...(e.symFields ? { symFields: new Map(e.symFields) } : {}) } as SymVal + : e; + return iteVal(numEq(ctx, idxV.t, num(ctx, k)), withField, e); + }); + const newRoot: SymVal = { kind: "arr", elems, len: rootArr.len } as SymVal; + return writeLValue(eaInner.expression, newRoot, frame, st, depth); + } + } const base = evalExpr(target.expression, frame, st, depth); if (isObj(base)) { const fields = new Map(base.fields); @@ -1524,6 +1847,25 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut }; const applyBinop = (op: ts.SyntaxKind, l: SymVal, r: SymVal, at: ts.Node, st: RunState): SymVal => { + // JS equality with undefined/null: a phantom-slot read (out-of-range + // index of a symbolic-length array) compares FALSE against any concrete + // value — it must not degrade the surrounding condition. + { + const lu = (l as { kind?: string }).kind, ru = (r as { kind?: string }).kind; + // TAGGED maybe-undefined/null values fall through to the tagged + // handler below — this kind-only fold would otherwise force + // `taggedX === undefined` to const-false and delete the guard. + const lTag = (l as { undefUnder?: unknown; nullUnder?: unknown }).undefUnder ?? (l as { nullUnder?: unknown }).nullUnder; + const rTag = (r as { undefUnder?: unknown; nullUnder?: unknown }).undefUnder ?? (r as { nullUnder?: unknown }).nullUnder; + if (!lTag && !rTag && (lu === "undef" || lu === "null" || ru === "undef" || ru === "null")) { + const isEq = op === ts.SyntaxKind.EqualsEqualsToken || op === ts.SyntaxKind.EqualsEqualsEqualsToken; + const isNe = op === ts.SyntaxKind.ExclamationEqualsToken || op === ts.SyntaxKind.ExclamationEqualsEqualsToken; + if (isEq || isNe) { + const same = lu === ru; + return { kind: "bool", t: bool(ctx, isEq ? same : !same) }; + } + } + } if (isNum(l) && isNum(r)) { const a = l.t, b = r.t; const ac = numC(a), bc = numC(b); @@ -1579,11 +1921,11 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut if (op === ts.SyntaxKind.EqualsEqualsToken || op === ts.SyntaxKind.EqualsEqualsEqualsToken) return { kind: "bool", t: bool(ctx, lc === rc) }; if (op === ts.SyntaxKind.ExclamationEqualsToken || op === ts.SyntaxKind.ExclamationEqualsEqualsToken) return { kind: "bool", t: bool(ctx, lc !== rc) }; } - if (op === ts.SyntaxKind.EqualsEqualsToken || op === ts.SyntaxKind.EqualsEqualsEqualsToken) return { kind: "bool", t: l.t.eq(r.t) }; + if (op === ts.SyntaxKind.EqualsEqualsToken || op === ts.SyntaxKind.EqualsEqualsEqualsToken) { if (typeof (l.t as { eq?: unknown }).eq !== "function" || typeof (r.t as { eq?: unknown }).eq !== "function") { if (process.env.VERIF_BIN_DEBUG) console.log(`[str-eq-bad] l=${JSON.stringify(l.t)} r=${JSON.stringify(r.t)}`); return { kind: "any" }; } return { kind: "bool", t: l.t.eq(r.t) }; } if (op === ts.SyntaxKind.ExclamationEqualsToken || op === ts.SyntaxKind.ExclamationEqualsEqualsToken) return { kind: "bool", t: l.t.neq(r.t) }; } if (isBool(l) && isBool(r)) { - if (op === ts.SyntaxKind.EqualsEqualsToken || op === ts.SyntaxKind.EqualsEqualsEqualsToken) return { kind: "bool", t: l.t.eq(r.t) }; + if (op === ts.SyntaxKind.EqualsEqualsToken || op === ts.SyntaxKind.EqualsEqualsEqualsToken) { if (typeof (l.t as { eq?: unknown }).eq !== "function" || typeof (r.t as { eq?: unknown }).eq !== "function") { if (process.env.VERIF_BIN_DEBUG) console.log(`[str-eq-bad] l=${JSON.stringify(l.t)} r=${JSON.stringify(r.t)}`); return { kind: "any" }; } return { kind: "bool", t: l.t.eq(r.t) }; } if (op === ts.SyntaxKind.ExclamationEqualsToken || op === ts.SyntaxKind.ExclamationEqualsEqualsToken) return { kind: "bool", t: l.t.neq(r.t) }; } // Mixed num/str `+` is JS string concatenation — route through the @@ -1596,15 +1938,43 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut const rs = numToStringStrFor(r.t); return { kind: "str", t: l.t.concat(rs.t) }; } - // Mixed equality against null/undefined - if ((l.kind === "null" || l.kind === "undef") || (r.kind === "null" || r.kind === "undef")) { - if (op === ts.SyntaxKind.EqualsEqualsEqualsToken || op === ts.SyntaxKind.EqualsEqualsToken) { - return { kind: "bool", t: bool(ctx, l.kind === r.kind) }; + // Mixed equality against null/undefined — TAGGED scalars must consult + // their undefUnder/nullUnder tag first: the plain kind comparison below + // folds `taggedX === undefined` to const-false BEFORE the tagged handler + // runs, silently deleting the maybe-undefined guard (phantom Map.get hits). + { + const tl = (l as { undefUnder?: unknown; nullUnder?: unknown }).undefUnder ?? (l as { nullUnder?: unknown }).nullUnder; + const tr = (r as { undefUnder?: unknown; nullUnder?: unknown }).undefUnder ?? (r as { nullUnder?: unknown }).nullUnder; + const eitherTagged = (l.kind === "null" || l.kind === "undef" || r.kind === "null" || r.kind === "undef") && (tl || tr); + if (!eitherTagged && ((l.kind === "null" || l.kind === "undef") || (r.kind === "null" || r.kind === "undef"))) { + if (op === ts.SyntaxKind.EqualsEqualsEqualsToken || op === ts.SyntaxKind.EqualsEqualsToken) { + return { kind: "bool", t: bool(ctx, l.kind === r.kind) }; + } + if (op === ts.SyntaxKind.ExclamationEqualsToken || op === ts.SyntaxKind.ExclamationEqualsEqualsToken) { + return { kind: "bool", t: bool(ctx, l.kind !== r.kind) }; + } } - if (op === ts.SyntaxKind.ExclamationEqualsToken || op === ts.SyntaxKind.ExclamationEqualsEqualsToken) { - return { kind: "bool", t: bool(ctx, l.kind !== r.kind) }; + } + // Tagged maybe-undefined/null scalars vs the literal: `x !== undefined` + // must consult the undefUnder/nullUnder tag (Map.get fan results). + { + const scalar = isNum(l) || isStr(l) || isBool(l) ? l : isNum(r) || isStr(r) || isBool(r) ? r : null; + const other = scalar === l ? r : l; + if (scalar && (other.kind === "undef" || other.kind === "null")) { + const uu = (scalar as { undefUnder?: Bool<"main">; nullUnder?: Bool<"main"> }).undefUnder + ?? (scalar as { nullUnder?: Bool<"main"> }).nullUnder; + if (uu) { + const kindMatch = other.kind === "undef" ? (scalar as { undefUnder?: unknown }).undefUnder !== undefined : (scalar as { nullUnder?: unknown }).nullUnder !== undefined; + if (op === ts.SyntaxKind.EqualsEqualsEqualsToken || op === ts.SyntaxKind.EqualsEqualsToken) { + return { kind: "bool", t: kindMatch ? uu : bool(ctx, false) }; + } + return { kind: "bool", t: kindMatch ? notFold(uu) : bool(ctx, true) }; + } } } + if (process.env.VERIF_BIN_DEBUG) { + console.log(`[bin-any] l=${l.kind}${(l as { undefUnder?: unknown }).undefUnder !== undefined ? "+uu" : ""} r=${r.kind} op=${op}`); + } return { kind: "any" }; }; @@ -1620,7 +1990,9 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut if (isArr(v)) argVals.push(...v.elems); else degrade(st, a, "spread argument"); } else { - argVals.push(evalExpr(a, frame, st, depth)); + const av = evalExpr(a, frame, st, depth); + if (av === undefined && process.env.VERIF_ARG_DEBUG) console.log(`[arg-undef] expr=${a.getText().slice(0, 60)}`); + argVals.push(av); } } const calleeExpr = call.expression; @@ -1659,19 +2031,48 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut if (lit !== undefined) { try { return jsonToSymVal(JSON.parse(lit), depth); } catch { return { kind: "null" }; } } + // symbolic template: content built as literal-skeleton ++ free + // holes (the transcript-row input model). The skeleton must be + // the exact parseRow template; holes are constrained to contain + // no quote/backslash so the extracted fields are faithful. + const tmpl = jsonParseTemplate(a0); + if (tmpl) return tmpl; } } degrade(st, call, `JSON.${meth}`); return { kind: "any" }; } - if (ns === "Object" || ns === "Boolean") { degrade(st, call, `${ns}.${meth}`); return { kind: "any" }; } + if (ns === "Object") { + const a0 = argVals[0]; + if ((meth === "keys" || meth === "entries" || meth === "values") && isObj(a0)) { + // concrete fields first, then symbolic-key bindings (a key may be + // bound on either side of a branch merge — read via the fan) + const ckeys: Str[] = []; + for (const k of a0.fields.keys()) if (!k.startsWith("__")) ckeys.push(ctx.String.val(k)); + const sf = a0.symFields; + if (sf && sf.size > 0) for (const kt of sf.keys()) ckeys.push(kt); + if (meth === "keys") return arrOf(ckeys.map((t) => ({ kind: "str", t }) as SymVal)); + if (meth === "values") return arrOf(ckeys.map((t) => elementAccess(a0, { kind: "str", t }, call, st) as SymVal)); + // entries: [key, value] pairs (positional destructure reads them) + return arrOf(ckeys.map((t) => arrOf([{ kind: "str", t } as SymVal, elementAccess(a0, { kind: "str", t }, call, st) as SymVal]))); + } + degrade(st, call, `Object.${meth}`); return { kind: "any" }; + } + if (ns === "Boolean") { degrade(st, call, `${ns}.${meth}`); return { kind: "any" }; } } // Method calls on values ------------------------------------------- if (ts.isPropertyAccessExpression(calleeExpr)) { - // optional call `x.m?.(…)`: absent method reads as undefined (no throw) - if (call.questionDotToken !== undefined && !(input.options?.portImpls && ("__method_" + calleeExpr.name.text) in input.options.portImpls)) { + // optional call `x.m?.(…)` (dot on the CALL) or `x?.m(…)` (dot on the + // property access — TS puts questionDotToken on the member chain node): + // absent receiver/method reads as undefined (no throw) + const optChain = call.questionDotToken !== undefined || (calleeExpr as unknown as { questionDotToken?: unknown }).questionDotToken !== undefined; + if (optChain && !(input.options?.portImpls && ("__method_" + calleeExpr.name.text) in input.options.portImpls)) { const r0 = evalExpr(calleeExpr.expression, frame, st, depth); + // the receiver itself absent (undef/null) or merged-away (nullUnder): + // the whole optional chain short-circuits to undefined + if (r0.kind === "undef" || r0.kind === "null") return { kind: "undef" }; if (isObj(r0)) { + if ((r0 as { nullUnder?: unknown }).nullUnder) return { kind: "undef" }; const mv = r0.fields.get(calleeExpr.name.text); if (mv === undefined || mv.kind === "undef" || mv.kind === "null") return { kind: "undef" }; } @@ -1721,7 +2122,7 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut // Local closure value call (const arrow / function expression stored in a var) const fnVal = frame.vars.get(name); if (fnVal && fnVal.kind === "fn") { - return invokeCallback(fnVal.node, argVals, st, depth, fnVal.env); + return invokeCallback(fnVal.node, argVals, st, depth, fnVal.env, frame); } // Port override (public injection seam); memoized on the argument's @@ -1776,18 +2177,15 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut if (defInfo) moduleStack.push(defInfo); try { const inner: Frame = { vars: new Map(frame.vars) }; // lexical closure - const params = ts.isFunctionDeclaration(fn) ? fn.parameters : fn.parameters; - params.forEach((p, i) => { - if (ts.isIdentifier(p.name)) { - inner.vars.set(p.name.text, args[i] ?? (p.initializer ? evalExpr(p.initializer, inner, st, depth + 1) : { kind: "undef" })); - } else if (ts.isArrayBindingPattern(p.name)) { - // destructure array param: bind pattern elements to slots - p.name.elements.forEach((el, j) => { - if (ts.isBindingElement(el) && ts.isIdentifier(el.name)) { - inner.vars.set(el.name.text, args[i] && isArr(args[i]) ? args[i].elems[j] : { kind: "undef" }); - } - }); - } + // 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); + const pd: SymVal = pv === undefined ? { kind: "undef" } : pv; + if (ts.isIdentifier(prm.name)) inner.vars.set(prm.name.text, pd); + else if (ts.isArrayBindingPattern(prm.name)) prm.name.elements.forEach((el, j) => { + if (!ts.isBindingElement(el) || !ts.isIdentifier(el.name)) return; + 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; @@ -1801,7 +2199,7 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut const pushAll2 = (dst: T[], src: T[]): void => { if (src !== dst) for (const x of src) dst.push(x); }; pushAll2(st.assumptions, innerState.assumptions); // degradations shared - if (innerState.thrown) st.thrown = orFold(st.thrown ?? bool(ctx, false), innerState.thrown); + if (innerState.thrown) st.thrown = orFold(st.thrown ?? bool(ctx, false), innerState.thrown) ?? null; // fold conditional returns (pendingRets) LIFO over the unconditional ret let out = innerState.ret; for (let i = innerState.pendingRets.length - 1; i >= 0; i--) { @@ -1819,6 +2217,40 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut recv: SymVal, m: string, args: SymVal[], at: ts.CallExpression, st: RunState, frame: Frame, depth: number, ): SymVal => { + // Map-entry array aliasing: `const l = m.get(k); l.push(...)` must be + // visible through the map (JS shares the array object). mapSetOp "get" + // records the entries the fan could match with their key-match guards; + // a push writes the rebuilt array back into each entry under the same + // guard (entry objects are shared by reference, so this is visible + // through every alias of the map). + const writeArrayAlias = (recvAlias: SymVal, next: SymVal, retv: SymVal): SymVal => { + const aelist = isArr(recvAlias) ? recvAlias.aliasEntries : undefined; + if (!aelist || aelist.length === 0) return retv; + for (const ae of aelist) { + const cur = ae.entry.fields.get("value"); + ae.entry.fields.set("value", iteVal(ae.match, next, cur ?? { kind: "undef" })); + } + return retv; + }; + // named function references as callbacks (filter(isSuspectedEvent)): + // resolve Identifier -> fn SymVal (frame binding or module fn decl) + const cbNode = (cb: ts.Expression | undefined): ts.ArrowFunction | ts.FunctionExpression | ts.FunctionDeclaration | null => { + if (!cb) return null; + if (ts.isArrowFunction(cb) || ts.isFunctionExpression(cb)) return cb; + if (ts.isIdentifier(cb)) { + const fv = frame.vars.get(cb.text); + if (fv && fv.kind === "fn") return fv.node; + const mi2 = curInfo(); + const md = mi2.functions.get(cb.text) ?? info.functions.get(cb.text); + if (md && md.body) return md; + } + return null; + }; + const callCb = (cb: ts.Expression | undefined, args: SymVal[]): SymVal | null => { + const node = cbNode(cb); + if (!node) return null; + return invokeCallback(node, args, st, depth); + }; // ---- namespace objects ---- if (isObj(recv) && m === "call" ) { degrade(st, at, "fn.call"); return { kind: "any" }; } @@ -1888,17 +2320,84 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut if (isArr(recv)) { switch (m) { case "length": return { kind: "num", t: recv.len }; + case "push": { const v = args[0]; + // `(cond ? a : b).push(x)` — a call on a TERNARY of identifiers. + // JS mutates whichever array the condition selects; model BOTH + // bindings with the push guarded by the leaf condition (a plain + // evaluated receiver would be a merged array whose rebind target + // does not exist — the write would be lost). + if (ts.isPropertyAccessExpression(at.expression)) { + let rootExpr: ts.Expression = at.expression.expression; + while (ts.isParenthesizedExpression(rootExpr) || ts.isAsExpression(rootExpr) || ts.isNonNullExpression(rootExpr)) rootExpr = rootExpr.expression; + if (ts.isConditionalExpression(rootExpr)) { + const leaves: Array<{ cond: Bool<"main">; name: string }> = []; + const collect = (e: ts.Expression, c: Bool<"main">): void => { + let x: ts.Expression = e; + while (ts.isParenthesizedExpression(x) || ts.isAsExpression(x) || ts.isNonNullExpression(x)) x = x.expression; + if (ts.isConditionalExpression(x)) { + const xc = truthy(ctx, evalExpr(x.condition, frame, st, depth)); + if (xc === null) return; + collect(x.whenTrue, andFold(c, xc) ?? bool(ctx, true)); + collect(x.whenFalse, andFold(c, notFold(xc)) ?? bool(ctx, true)); + return; + } + if (ts.isIdentifier(x)) leaves.push({ cond: c, name: x.text }); + }; + const rootC = truthy(ctx, evalExpr(rootExpr.condition, frame, st, depth)); + if (rootC !== null) { + collect(rootExpr.whenTrue, rootC); + collect(rootExpr.whenFalse, notFold(rootC)); + if (leaves.length > 0) { + for (const leaf of leaves) { + const cur = frame.vars.get(leaf.name); + if (isArr(cur) && cur.elems.length < N) { + const pushSlotLeaf = (e: SymVal, k: number): SymVal => { + const g = numEq(ctx, cur.len, num(ctx, k)); + if (e.kind === "undef" || e.kind === "null") return guardedNeutral(g, v); + return iteVal(g, v, e); + }; + const elems = cur.elems.map((e, k) => pushSlotLeaf(e, k)); + elems.push(guardedNeutral(numEq(ctx, cur.len, num(ctx, cur.elems.length)), v)); + const pushed: SymVal = { kind: "arr", elems, len: cur.len.add(num(ctx, 1)) }; + frame.vars.set(leaf.name, iteVal(leaf.cond, pushed, cur)); + } + } + return v; + } + } + } + } + // append at the symbolic tail position (arrays built by push). + // SLOT GUARDS: writing into an undef phantom-tail slot via plain + // iteVal hits the undef-loses merge rule and drops the len==k + // guard — the pushed value becomes unconditionally reachable (a + // ghost push the solver can pick on paths where the push never + // ran). guardedNeutral keeps the guard inside the slot value. + const pushSlot = (e: SymVal, k: number, lenT: Num, arr: { elems: SymVal[] } | SymVal[]): SymVal => { + const g = numEq(ctx, lenT, num(ctx, k)); + if (e.kind === "undef" || e.kind === "null") return guardedNeutral(g, v); + return iteVal(g, v, e); + }; if (recv.elems.length < N) { - // append at the symbolic tail position (arrays built by push) - const elems = recv.elems.map((e, k) => iteVal(numEq(ctx, recv.len, num(ctx, k)), v, e)); - elems.push(iteVal(numEq(ctx, recv.len, num(ctx, recv.elems.length)), v, { kind: "undef" })); + const elems = recv.elems.map((e, k) => pushSlot(e, k, recv.len, recv.elems)); + elems.push(guardedNeutral(numEq(ctx, recv.len, num(ctx, recv.elems.length)), v)); const newLen = recv.len.add(num(ctx, 1)); - return rebindArray(recv, { kind: "arr", elems, len: newLen }, at, st, frame); + return writeArrayAlias(recv, { kind: "arr", elems, len: newLen }, rebindArray(recv, { kind: "arr", elems, len: newLen }, at, st, frame)); } - degrade(st, at, "push beyond bounded slots"); - return { kind: "any" }; + // Bounded-slot wrap: the slot budget N is full (phantom tail slots + // from ghost iterations may have consumed them), but the SYMBOLIC + // length can still be far below N. A push at len == k must write + // slot k — broadcasting the ite(len==k, v, e_k) write across ALL + // slots (writing only the LAST slot dropped pushes whose len was + // below N-1 entirely: real content vanished from the model — + // buildView's row pushes died exactly there). We assume len ≤ N-1 + // before the push (post-push len ≤ N, the model's array bound). + pushAsum(st, recv.len.le(num(ctx, N - 1))); + const elems = recv.elems.map((e, k) => pushSlot(e, k, recv.len, recv.elems)); + const newLen = recv.len.add(num(ctx, 1)); + return writeArrayAlias(recv, { kind: "arr", elems, len: newLen }, rebindArray(recv, { kind: "arr", elems, len: newLen }, at, st, frame)); } case "includes": { const target = args[0]; @@ -1922,22 +2421,22 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut } case "map": { const cb = at.arguments[0]; - if (!cb || (!ts.isArrowFunction(cb) && !ts.isFunctionExpression(cb))) { degrade(st, at, "map without function literal"); return { kind: "any" }; } + if (!cbNode(cb)) { degrade(st, at, "map without function literal"); return { kind: "any" }; } const elems = recv.elems.map((e, k) => { const guarded = iteVal(recv.len.gt(num(ctx, k)), e, { kind: "undef" }); - const r = invokeCallback(cb, [guarded, { kind: "num", t: num(ctx, k) }, recv], st, depth); + const r = invokeCallback(cbNode(cb)!, [guarded, { kind: "num", t: num(ctx, k) }, recv], st, depth); return r; }); return { kind: "arr", elems, len: recv.len }; } case "filter": { const cb = at.arguments[0]; - if (!cb || (!ts.isArrowFunction(cb) && !ts.isFunctionExpression(cb))) { degrade(st, at, "filter without function literal"); return { kind: "any" }; } + if (!cbNode(cb)) { degrade(st, at, "filter without function literal"); return { kind: "any" }; } // Unrolled two-pass: keep-slot[j] via counting. const keep: Bool<"main">[] = recv.elems.map((e, k) => { const g = recv.len.gt(num(ctx, k)); const guarded = iteVal(g, e, { kind: "undef" }); - const r = invokeCallback(cb, [guarded, { kind: "num", t: num(ctx, k) }, recv], st, depth); + const r = invokeCallback(cbNode(cb)!, [guarded, { kind: "num", t: num(ctx, k) }, recv], st, depth); const t = truthy(ctx, r); if (!t) return g.not(); // out-of-length slots never keep return g.and(t); @@ -1964,7 +2463,7 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut for (let k = 0; k < recv.elems.length; k++) { const g = recv.len.gt(num(ctx, k)); const guarded = iteVal(g, recv.elems[k], { kind: "undef" }); - const r = truthy(ctx, invokeCallback(cb, [guarded, { kind: "num", t: num(ctx, k) }, recv], st, depth)); + const r = truthy(ctx, invokeCallback(cbNode(cb)!, [guarded, { kind: "num", t: num(ctx, k) }, recv], st, depth)); if (!r) { degrade(st, at, `${m} callback degraded`); return { kind: "any" }; } const effective = g.and(r); acc = m === "every" ? acc.and(effective) : acc.or(effective); @@ -1978,12 +2477,12 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut } case "find": { const cb = at.arguments[0]; - if (!cb || (!ts.isArrowFunction(cb) && !ts.isFunctionExpression(cb))) { degrade(st, at, "find without function literal"); return { kind: "any" }; } + if (!cbNode(cb)) { degrade(st, at, "find without function literal"); return { kind: "any" }; } let acc: SymVal = { kind: "undef" }; for (let k = recv.elems.length - 1; k >= 0; k--) { const g = recv.len.gt(num(ctx, k)); const guarded = iteVal(g, recv.elems[k], { kind: "undef" }); - const r = truthy(ctx, invokeCallback(cb, [guarded, { kind: "num", t: num(ctx, k) }, recv], st, depth)); + const r = truthy(ctx, invokeCallback(cbNode(cb)!, [guarded, { kind: "num", t: num(ctx, k) }, recv], st, depth)); if (!r) { degrade(st, at, "find callback degraded"); return { kind: "any" }; } acc = iteVal(g.and(r), recv.elems[k], acc); } @@ -1991,12 +2490,12 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut } case "findIndex": { const cb = at.arguments[0]; - if (!cb || (!ts.isArrowFunction(cb) && !ts.isFunctionExpression(cb))) { degrade(st, at, "findIndex without function literal"); return { kind: "any" }; } + if (!cbNode(cb)) { degrade(st, at, "findIndex without function literal"); return { kind: "any" }; } let acc: Num = num(ctx, -1); for (let k = recv.elems.length - 1; k >= 0; k--) { const g = recv.len.gt(num(ctx, k)); const guarded = iteVal(g, recv.elems[k], { kind: "undef" }); - const r = truthy(ctx, invokeCallback(cb, [guarded, { kind: "num", t: num(ctx, k) }, recv], st, depth)); + const r = truthy(ctx, invokeCallback(cbNode(cb)!, [guarded, { kind: "num", t: num(ctx, k) }, recv], st, depth)); if (!r) { degrade(st, at, "findIndex callback degraded"); return { kind: "any" }; } acc = ctx.If(g.and(r), num(ctx, k), acc); } @@ -2047,11 +2546,11 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut } case "forEach": { const cb = at.arguments[0]; - if (!cb || (!ts.isArrowFunction(cb) && !ts.isFunctionExpression(cb))) { degrade(st, at, "forEach without function literal"); return { kind: "any" }; } + if (!cbNode(cb)) { degrade(st, at, "forEach without function literal"); return { kind: "any" }; } for (let k = 0; k < recv.elems.length; k++) { const g = recv.len.gt(num(ctx, k)); const guarded = iteVal(g, recv.elems[k], { kind: "undef" }); - invokeCallback(cb, [guarded, { kind: "num", t: num(ctx, k) }, recv], st, depth); + invokeCallback(cbNode(cb)!, [guarded, { kind: "num", t: num(ctx, k) }, recv], st, depth); } return { kind: "undef" }; } @@ -2131,6 +2630,14 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut } case "slice": case "substring": { + // known string + full-range slice is the identity (keeps JSON.parse + // of `raw.slice(raw.indexOf("{"))` concrete) + const rcFold = strConst.get(recv.t); + if (rcFold !== undefined && args.length === 1 && isNum(args[0])) { + const s0 = numC(args[0].t); + if (s0 === 0) return recv; + if (s0 !== undefined && s0 > 0) return { kind: "str", t: ctx.String.val(rcFold.slice(s0)) }; + } const a0 = isNum(args[0]) ? args[0].t : num(ctx, 0); const rawEnd = isNum(args[1]) ? args[1].t : lenR(recv.t); const start = ctx.If(a0.lt(num(ctx, 0)), ctx.If(a0.add(lenR(recv.t)).ge(num(ctx, 0)), a0.add(lenR(recv.t)), num(ctx, 0)), a0); @@ -2273,17 +2780,24 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut chain.unshift(node); node = node.expression; } + // The LAST chain element is the METHOD ACCESS itself (a.b.push → the + // `push` hop). It is not a container link: without dropping it the + // rebuild writes the new array under the field "push" and the real + // receiver (`current.blocks`) keeps the stale array. + if (chain.length > 0 && at.expression === chain[chain.length - 1] && ts.isPropertyAccessExpression(chain[chain.length - 1]!)) { + chain.pop(); + } if (ts.isIdentifier(node)) { const root = node.text; let rebuilt: SymVal = newArr; - for (let i = chain.length - 1; i >= 1; i--) { + for (let i = chain.length - 1; i >= 0; i--) { const hop = chain[i]!; const parentVal = evalExpr(hop.expression, frame, st, 0); if (parentVal.kind !== "obj") return newArr; // degrade silently: root rebind only const fields = new Map(parentVal.fields); if (!ts.isPropertyAccessExpression(hop)) return newArr; // element-middle chains: root rebind only fields.set(hop.name.text, rebuilt); - rebuilt = { kind: "obj", fields }; + rebuilt = { kind: "obj", fields, symFields: parentVal.symFields ? new Map(parentVal.symFields) : undefined } as Extract; } frame.vars.set(root, rebuilt); return rebuilt; @@ -2298,17 +2812,18 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut st: RunState, depth: number, env?: Frame, + caller?: Frame, ): SymVal => { const inner: Frame = { vars: new Map((env ?? frame).vars) }; // lexical closure - cb.parameters.forEach((p, i) => { - if (ts.isIdentifier(p.name)) inner.vars.set(p.name.text, args[i] ?? { kind: "undef" }); - else if (ts.isArrayBindingPattern(p.name)) { - p.name.elements.forEach((el, j) => { - if (ts.isBindingElement(el) && ts.isIdentifier(el.name) && isArr(args[i])) { - inner.vars.set(el.name.text, args[i].elems[j] ?? { kind: "undef" }); - } - }); - } + // bind parameters (earlier params visible to later default exprs) + cb.parameters.forEach((prm, pi) => { + const pv: SymVal | undefined = args[pi] ?? (prm.initializer ? evalExpr(prm.initializer, inner, innerState, depth + 1) : undefined); + const pd: SymVal = pv === undefined ? { kind: "undef" } : pv; + if (ts.isIdentifier(prm.name)) inner.vars.set(prm.name.text, pd); + else if (ts.isArrayBindingPattern(prm.name)) prm.name.elements.forEach((el, j) => { + if (!ts.isBindingElement(el) || !ts.isIdentifier(el.name)) return; + inner.vars.set(el.name.text, elementAccess(pd, { kind: "num", t: num(ctx, j) }, el, st)); + }); }); const innerState = runState0(); if (cb.body && ts.isBlock(cb.body)) execStmt(cb.body, inner, innerState, depth + 1); @@ -2316,7 +2831,7 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut const pushAll2 = (dst: T[], src: T[]): void => { if (src !== dst) for (const x of src) dst.push(x); }; pushAll2(st.assumptions, innerState.assumptions); // degradations shared - if (innerState.thrown) st.thrown = orFold(st.thrown ?? bool(ctx, false), innerState.thrown); + if (innerState.thrown) st.thrown = orFold(st.thrown ?? bool(ctx, false), innerState.thrown) ?? null; // fold inner conditional returns (arrow bodies with early returns) let ret: SymVal | null = innerState.ret; for (let i = innerState.pendingRets.length - 1; i >= 0; i--) { @@ -2328,16 +2843,40 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut // call site's live frame is the declaration frame (top-level helper calls // like applyTo(...)); loop-body callbacks in the guarded subset never // write captured vars. - if (env) { - for (const [k, v] of inner.vars) { - const old = env.vars.get(k); + // Write back to BOTH the declaring (closure) frame and the CALLING + // frame. They are the same Map for top-level helper calls, but diverge + // when the call happens inside a branch COPY: mergeBranch rebuilds + // values from the copies, so a closure write that only landed in the + // live declaring frame would be overwritten by the stale-copy merge + // (the flush()-inside-if pattern lost every push this way). + // caller = the frame the call site runs in (may be a branch COPY of the + // declaring frame); without it, closure writes inside `if (...) flush()` + // land only in the live frame and the branch merge resurrects stale state. + const callerFrame = caller ?? frame; + const targets = env && env.vars !== callerFrame.vars ? [env.vars, callerFrame.vars] : [env ? env.vars : callerFrame.vars]; + // Only keys the closure body actually MUTATED propagate (inner value + // differs from the declaring frame's snapshot). Writing untouched + // bindings would push the ENV's stale values into branch copies of the + // caller — `...cc(m.id)` as a call ARGUMENT used to clobber + // `current` (assigned in the caller's frame) with the top-level `null` + // snapshot, degrading current.blocks.push to "method .push on undef". + const envSnapshot: Frame["vars"] | null = env ? env.vars : null; + for (const [k, v] of inner.vars) { + // Read the PRE-CALL env value BEFORE any target write: when target0 IS + // env.vars itself, its own set(k, v) would make later targets' skip + // check see the updated map and skip the caller frame — the caller's + // stale copy then wins the next merge and the closure's write is lost. + const preEnv = envSnapshot !== null ? envSnapshot.get(k) : undefined; + if (envSnapshot !== null && preEnv === v) continue; // untouched by the closure + for (const tv of targets) { + const old = tv.get(k); if (old === undefined || old === v) continue; const same = (isNum(old) && isNum(v) && old.t.hash() === v.t.hash()) || (isStr(old) && isStr(v) && old.t.hash() === v.t.hash()) || (isBool(old) && isBool(v) && old.t.hash() === v.t.hash()) || (isArr(old) && isArr(v) && old.len.hash() === v.len.hash()); - if (!same) env.vars.set(k, v); + if (!same) tv.set(k, v); } } return ret ?? { kind: "undef" }; @@ -2369,13 +2908,61 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut return { kind: "bool", t: acc }; } case "get": { - const eqs = entriesArr.elems.map((e) => (isObj(e) ? valEq(e.fields.get("key") ?? { kind: "any" }, args[0]) : null)); + const keyOf = (e: SymVal): SymVal => { + if (!isObj(e)) return { kind: "undef" }; + const nu = e.nullUnder; + if (!nu) return e.fields.get("key") ?? { kind: "undef" }; + return iteVal(nu.not(), e.fields.get("key") ?? { kind: "undef" }, { kind: "undef" }); + }; + const eqs = entriesArr.elems.map((e) => (isObj(e) ? valEq(keyOf(e), args[0]) : null)); if (eqs.some((e) => e === null)) return { kind: "any" }; + // miss arm keeps an undefUnder tag (like the obj symFields fan): + // `map.get(k) !== undefined` must stay symbolic when a guarded slot + // may or may not match — a plain undef loses the tag through merges + // and the lookup folds to "always found" (phantom reads). let acc: SymVal = { kind: "undef" }; + // JS reference semantics: a Map entry's array value can be mutated + // through the alias `const l = m.get(k); l.push(...)`. Track every + // entry the fan could have matched (with its key-match guard) so a + // later push can write the new array back into each entry under + // exactly the same guard. + const aliasEntries: { entry: { fields: Map }; match: Bool<"main"> }[] = []; + const accNull = (v: SymVal): Bool<"main"> | undefined => (v.kind === "arr" || v.kind === "obj") + ? (v as { nullUnder?: Bool<"main"> }).nullUnder + : (v.kind === "num" || v.kind === "str" || v.kind === "bool") + ? (v as { undefUnder?: Bool<"main"> }).undefUnder + : undefined; for (let k = eqs.length - 1; k >= 0; k--) { const slotVal = entriesArr.elems[k]; - const got: SymVal = isObj(slotVal) ? slotVal.fields.get("value") ?? { kind: "undef" } : { kind: "undef" }; - acc = iteVal(entriesArr.len.gt(num(ctx, k)).and(eqs[k]!), got, acc); + const gotRaw: SymVal = isObj(slotVal) ? slotVal.fields.get("value") ?? { kind: "undef" } : { kind: "undef" }; + const got: SymVal = isObj(slotVal) && slotVal.nullUnder ? iteVal(slotVal.nullUnder.not(), gotRaw, { kind: "undef" }) : gotRaw; + const live = entriesArr.len.gt(num(ctx, k)).and(eqs[k]!); + if (isArr(got)) aliasEntries.push({ entry: { fields: (slotVal as { fields: Map }).fields }, match: live }); + // exact acc-chain nullness: result is nullish iff THIS slot did + // not match AND the rest of the chain is nullish. mergeVal's + // arr-arr path drops nullUnder (real arrays on other paths rely + // on that), so compute it here: nullUnder = and(¬live, accNull). + const gotNull = accNull(got), accN = accNull(acc); + if ((gotNull !== undefined) || (accN !== undefined)) { + const chainNull = andFold(live.not(), accN ?? bool(ctx, true)) ?? bool(ctx, true); + // VALUE channel keeps BOTH arms: when the matching slot was + // already consumed into acc (higher k processed first), the + // bare `got` of the current slot is the WRONG object — the + // post.get(i2)-returns-msg1 phantom (view-latch Q2). + const mergedV = iteStrict(live, got, acc); + acc = isArr(mergedV) && isArr(got) + ? { kind: "arr", elems: (mergedV as { elems: SymVal[] }).elems, len: (mergedV as { len: Term }).len, nullUnder: chainNull } + : isObj(mergedV) && isObj(got) + ? { kind: "obj", fields: (mergedV as { fields: Map }).fields, nullUnder: chainNull, ...((mergedV as { symFields?: unknown }).symFields ? { symFields: (mergedV as { symFields: Map; v: SymVal }> }).symFields } : {}) } + : (mergedV.kind === "num" || mergedV.kind === "str" || mergedV.kind === "bool") + ? { ...(mergedV as Extract), undefUnder: chainNull } + : mergedV; + } else { + acc = iteStrict(live, got, acc); + } + } + if (isArr(acc) && aliasEntries.length > 0) { + return { ...acc, aliasEntries }; } return acc; } @@ -2411,7 +2998,16 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut // length is never observed by length-guarded reads). const slotElems = entriesArr.elems.map((e, k) => iteVal(numEq(ctx, entriesArr.len, num(ctx, k)), entry, e)); if (entriesArr.elems.length < N - 1) { - slotElems.push(iteVal(numEq(ctx, entriesArr.len, num(ctx, entriesArr.elems.length)), entry, { kind: "undef" })); + // The phantom tail entry must keep its own guard: collapsing + // ite(guard, entry, undef) via the undef-loses rule made the tail + // entry look REAL in every model — later exists-checks and fan + // reads matched keys from length epochs that never happened + // (cross-epoch guards: solver picked branch-consistent-but- + // unreachable keys). Tag with nullUnder instead: reads through + // member()/truthy() are gated, exists-checks see undef -> false. + const tailGuard = numEq(ctx, entriesArr.len, num(ctx, entriesArr.elems.length)); + if (isObj(entry)) slotElems.push({ kind: "obj", fields: entry.fields, nullUnder: notFold(tailGuard) }); + else slotElems.push(iteVal(tailGuard, entry, { kind: "undef" })); } else { degrade(st, at, "map/set add beyond bounded slots"); } @@ -2435,12 +3031,31 @@ export function translateFunction(input: TranslateInput): { exec: () => ExecOut // new Map()/new Set() const evalNew = (call: ts.NewExpression, frame: Frame, st: RunState, depth: number): SymVal => { const name = ts.isIdentifier(call.expression) ? call.expression.text : ""; - if (name === "Map") { - return { kind: "obj", fields: new Map([["__map_entries", arrOf([])]]) }; - } if (name === "Set") { + // `new Set(iterable)` — seed the model from an array argument. Entries + // are copied slot-for-slot with the argument's (possibly symbolic) + // length; reads are len-guarded on the call side, so phantom slots + // behind a shorter concrete length never surface. + const a0 = call.arguments?.[0]; + const init = a0 ? evalExpr(a0, frame, st, depth) : undefined; + if (init && isArr(init)) { + return { kind: "obj", fields: new Map([["__set_values", { kind: "arr", elems: [...init.elems], len: init.len }]]) }; + } return { kind: "obj", fields: new Map([["__set_values", arrOf([])]]) }; } + if (name === "Map") { + // `new Map(entries)` — seed from a [[k, v], ...] array argument. + const a0 = call.arguments?.[0]; + const init = a0 ? evalExpr(a0, frame, st, depth) : undefined; + if (init && isArr(init)) { + const entries: SymVal[] = init.elems.map((p) => { + if (!isArr(p) || p.elems.length < 2) return { kind: "obj", fields: new Map([["key", { kind: "undef" }], ["value", { kind: "undef" }]]) }; + return { kind: "obj", fields: new Map([["key", p.elems[0]!], ["value", p.elems[1]!]]) }; + }); + return { kind: "obj", fields: new Map([["__map_entries", { kind: "arr", elems: entries, len: init.len }]]) }; + } + return { kind: "obj", fields: new Map([["__map_entries", arrOf([])]]) }; + } if (name === "RegExp") { // regex carried as an object; .test/.replace on it use the anchored- // prefix over-approximation at the call sites (see callMethod). diff --git a/verif/terms.ts b/verif/terms.ts index 060f435534..bc5a526e63 100644 --- a/verif/terms.ts +++ b/verif/terms.ts @@ -100,7 +100,10 @@ function smtStr(v: string): string { for (const ch of v) { const cp = ch.codePointAt(0)!; if (ch === '"') out += '""'; - else if (cp < 0x20 || cp === 0x7f) out += `\\u{${cp.toString(16).padStart(4, "0")}}`; + // SMT-LIB string literals: only printable ASCII may appear raw; + // cvc5 rejects non-ASCII bytes as "Non-printable character" even when + // they are printable Unicode (advice strings carry em-dashes, ·, etc). + else if (cp < 0x20 || cp === 0x7f || cp > 0x7e) out += `\\u{${cp.toString(16).padStart(4, "0")}}`; else out += ch; } return out + '"'; @@ -119,6 +122,8 @@ export class Context { readonly pendingFns = new Set(); readonly pendingFnsR1 = new Set(); readonly pendingFnsS1 = new Set(); + /** global side assumptions (digit-only __numstr, etc.) — asserted in smt2Of */ + readonly pendingAsums: Term[] = []; /** (String, String) → String, deterministic in both args. */ readonly pendingFnsS2 = new Set(); private readonly varCache = new Map(); @@ -382,14 +387,17 @@ function bindMethods(ctx: Context): void { const av = typeof this.c === "string" ? this.c : undefined; const bv = typeof s.c === "string" ? s.c : undefined; if (av !== undefined && bv !== undefined) return lit(c, String(bv.startsWith(av)), "Bool", bv.startsWith(av)); - return c.app2("str.prefixof", this, s, "Bool"); + // SMT-LIB (str.prefixof s t) = "s is a prefix of t" — the RECEIVER (this) + // is the string being tested, the ARG is the prefix, so arg goes FIRST. + return c.app2("str.prefixof", s, this, "Bool"); }, suffixOf(s) { const c = this.ctx ?? ctx; const av = typeof this.c === "string" ? this.c : undefined; const bv = typeof s.c === "string" ? s.c : undefined; if (av !== undefined && bv !== undefined) return lit(c, String(bv.endsWith(av)), "Bool", bv.endsWith(av)); - return c.app2("str.suffixof", this, s, "Bool"); + // (str.suffixof s t) = "s is a suffix of t" — arg (the suffix) goes FIRST. + return c.app2("str.suffixof", s, this, "Bool"); }, replace(a, b) { const c = this.ctx ?? ctx; @@ -504,6 +512,7 @@ export function smt2Of( for (const fn of ctx.pendingFnsS1) lines.push(`(declare-fun ${fn} (String) String)`); for (const fn of (ctx as unknown as { pendingFnsS2: Set }).pendingFnsS2) lines.push(`(declare-fun ${fn} (String String) String)`); for (const d of ctx.decls) lines.push(`(declare-fun ${d} () ${ctx.declSort.get(d) ?? "Real"})`); + for (const a of ctx.pendingAsums) lines.push(`(assert ${render(a, false)})`); if (names.size) { // Shared subterms become top-level define-funs — sequential commands, // so later definitions may reference earlier ones. (SMT-LIB `let`