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 }`),