From 548ee20b3ba576891f9090f5da472e092a970a12 Mon Sep 17 00:00:00 2001 From: Ronald Tse Date: Sat, 14 Nov 2026 11:41:17 +0800 Subject: [PATCH] fix(isc): a ruleset declared twice is a load error (G7, py canon) keep-first silently hid map bugs, and first-`find` lookup made the executed body declaration-order dependent. expandDocument now rejects duplicate ruleset names with a named IscLoadError, matching rs #19's builder check and py's parse rejection; ruby keeps first (its half is separate). Every document passes through expandDocument at load - deps included - so a dependent's duplicate cannot slip through. TDD: wf1 test red (loaded, executed the second body) -> green; isc suite 135/135. --- src/isc/expand.ts | 8 ++++++++ test/isc/wf1.test.ts | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/src/isc/expand.ts b/src/isc/expand.ts index c3c71c8..f6fb071 100644 --- a/src/isc/expand.ts +++ b/src/isc/expand.ts @@ -43,6 +43,14 @@ export interface ExpandOptions { /** Expand every `use` in the document's stages; rulesets themselves never execute. */ export function expandDocument(doc: IscDocument, opts: ExpandOptions = {}): IscDocument { + // WO18 G7 (py canon): a ruleset declared twice is a load error — + // keep-first silently hides map bugs and first-`.find` lookup makes + // the executed body declaration-order dependent. + const seenRs = new Set() + for (const rs of doc.rulesets ?? []) { + if (!seenRs.has(rs.name)) seenRs.add(rs.name) + else throw new IscLoadError(`ruleset "${rs.name}" declared more than once in ${doc.systemCode}`) + } const scope: ExpandScope = { doc, docKey: doc.systemCode, diff --git a/test/isc/wf1.test.ts b/test/isc/wf1.test.ts index 124bafa..6d82caf 100644 --- a/test/isc/wf1.test.ts +++ b/test/isc/wf1.test.ts @@ -288,6 +288,14 @@ describe("expandDocument — cycle and resolution errors (§3.2, §6.4)", () => expect(() => expandDocument(doc)).toThrow(/use cycle/) }) + it("rejects a duplicate ruleset definition (py canon - keep-first hides map bugs)", () => { + const doc = parseIsc( + minimalWrap(`ruleset a { sub "x" "1" }\nruleset a { sub "x" "2" }\nstage main { use a }`), + ) + expect(() => expandDocument(doc)).toThrow(IscLoadError) + expect(() => expandDocument(doc)).toThrow(/declared more than once/) + }) + it("rejects a mutual cycle", () => { const doc = parseIsc( minimalWrap(`ruleset a { use b }\nruleset b { use a }\nstage main { use a }`),