-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathengine.mjs
More file actions
514 lines (451 loc) · 18.3 KB
/
engine.mjs
File metadata and controls
514 lines (451 loc) · 18.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
// Rule engine for regulation YAML files.
//
// Scope: evaluates regulations/eu/customs/anti_dumping_china_v1.yaml end-to-end.
// Throws UnsupportedConstructError for syntax it doesn't yet handle, so callers
// can fall back to a weaker check on regulations that haven't been tightened yet.
//
// The condition language follows docs/SCHEMA.md plus the dialect used in
// anti_dumping_china_v1: AND/OR blocks, starts_with("..."), and a small set of
// built-in virtual predicates.
//
// Phase 3 addition: a rule's `then` may carry an `invoke:` block that delegates
// to a sub-model lookup (see lookup_by_cn_origin_consigned). The engine loads
// the named submodel and runs the lookup synchronously.
import path from "node:path";
import yaml from "js-yaml";
import { readFileSync } from "node:fs";
export class UnsupportedConstructError extends Error {
constructor(msg) { super(msg); this.name = "UnsupportedConstructError"; }
}
// ============================================================
// Clock & date helpers
// ============================================================
const DEFAULT_CLOCK = "2026-04-28"; // matches consolidated_version of the file
function parseDate(s) {
if (!s) return null;
if (s instanceof Date) return s;
const m = String(s).match(/^(\d{4})(?:-(\d{2}))?(?:-(\d{2}))?/);
if (!m) return null;
const yr = +m[1];
const mo = m[2] ? +m[2] - 1 : 0;
const dy = m[3] ? +m[3] : 1;
return new Date(Date.UTC(yr, mo, dy));
}
function addMonths(date, months) {
const d = new Date(date.getTime());
d.setUTCMonth(d.getUTCMonth() + months);
return d;
}
// ============================================================
// Context: a flat dotted-path map of facts the rules can read
// ============================================================
class Ctx {
constructor() { this.map = new Map(); }
set(path, value) { this.map.set(path, value); return this; }
get(path) { return this.map.get(path); }
has(path) { return this.map.has(path); }
setMany(obj) {
for (const [k, v] of Object.entries(obj || {})) this.map.set(k, v);
return this;
}
}
// ============================================================
// Predicate evaluator
// ============================================================
const RX_COMPARE = /^(==|!=|<=|>=|<|>)\s*(.+)$/;
const RX_RANGE = /^([\[\(])\s*([-\d.]+)\s*\.\.\s*([-\d.]+)\s*([\]\)])$/;
const RX_STARTS_WITH = /^starts_with\(\s*"([^"]*)"\s*\)$/;
const RX_CONTAINS = /^contains\s+'([^']*)'$/;
const RX_IN_LIST = /^in\s+\[(.+)\]$/;
const RX_NUMBER = /^-?\d+(\.\d+)?$/;
const UNRESOLVED_PRODUCER_TARIC = Symbol("unresolved-producer-taric");
function lookupInObject(obj, dottedPath) {
return dottedPath.split(".").reduce((acc, k) => (acc == null ? acc : acc[k]), obj);
}
function readField(field, ctx, scope) {
// measure.* in a per-measure scope reads from the bound record.
if (scope?.measure && field.startsWith("measure.")) {
return lookupInObject(scope.measure, field.slice("measure.".length));
}
return ctx.get(field);
}
function resolveRhs(rhs, ctx, scope) {
const s = rhs.trim();
if ((s.startsWith('"') && s.endsWith('"')) || (s.startsWith("'") && s.endsWith("'"))) {
return s.slice(1, -1);
}
if (s === "true") return true;
if (s === "false") return false;
if (s === "null") return null;
if (RX_NUMBER.test(s)) return Number(s);
if (s === "producer_taric") return UNRESOLVED_PRODUCER_TARIC;
return s; // bare token — treat as enum string
}
function compare(actual, op, rhs) {
if (rhs === UNRESOLVED_PRODUCER_TARIC) {
// Phase 1: no producer→TARIC lookup yet. Assume the declared code matches
// the producer's expected code so the conforming path is reachable.
// Phase 3 replaces this with a real lookup against the register.
if (op === "==") return true;
if (op === "!=") return false;
return false;
}
if (op === "==") return String(actual) === String(rhs);
if (op === "!=") return String(actual) !== String(rhs);
const a = Number(actual), b = Number(rhs);
if (op === "<=") return a <= b;
if (op === ">=") return a >= b;
if (op === "<") return a < b;
if (op === ">") return a > b;
return false;
}
function evalAtomic(field, predicate, ctx, scope, virtuals) {
const actual = readField(field, ctx, scope);
if (typeof predicate === "boolean" || typeof predicate === "number" || predicate === null) {
return actual === predicate;
}
if (Array.isArray(predicate)) return predicate.includes(actual);
if (typeof predicate !== "string") {
throw new UnsupportedConstructError(
`Predicate for "${field}" is a ${typeof predicate}; expected scalar/string/array`
);
}
const s = predicate.trim();
if (s === "is null") return actual === null || actual === undefined;
if (s === "is not null") return actual !== null && actual !== undefined;
if (s.startsWith("not(") && s.endsWith(")")) {
const inner = s.slice(4, -1).trim();
if (inner === "true") return actual !== true;
if (inner === "false") return actual !== false;
return String(actual) !== inner;
}
let m = s.match(RX_STARTS_WITH);
if (m) return typeof actual === "string" && actual.startsWith(m[1]);
m = s.match(RX_CONTAINS);
if (m) {
if (typeof actual === "string") return actual.includes(m[1]);
if (Array.isArray(actual)) return actual.includes(m[1]);
return false;
}
m = s.match(RX_IN_LIST);
if (m) {
const items = m[1].split(",").map(x => x.trim().replace(/^['"]|['"]$/g, ""));
return items.includes(String(actual));
}
m = s.match(RX_COMPARE);
if (m) return compare(actual, m[1], resolveRhs(m[2], ctx, scope));
m = s.match(RX_RANGE);
if (m) {
const [, lb, lo, hi, ub] = m;
const x = Number(actual);
const okLo = lb === "[" ? x >= +lo : x > +lo;
const okHi = ub === "]" ? x <= +hi : x < +hi;
return okLo && okHi;
}
// Virtual predicate (built-in) or bare-enum equality.
if (virtuals.has(s)) return virtuals.get(s)(actual, ctx, scope);
return String(actual) === s;
}
function evalWhen(when, ctx, scope, virtuals) {
if (!when || (typeof when === "object" && Object.keys(when).length === 0)) return true;
if (typeof when !== "object") {
throw new UnsupportedConstructError(`when: must be an object, got ${typeof when}`);
}
return Object.entries(when).every(([k, v]) => {
if (k === "AND") return v.every(child => evalWhen(child, ctx, scope, virtuals));
if (k === "OR") return v.some(child => evalWhen(child, ctx, scope, virtuals));
if (k === "NOT") return !evalWhen(v, ctx, scope, virtuals);
return evalAtomic(k, v, ctx, scope, virtuals);
});
}
// ============================================================
// Sub-model loading + register lookups
// ============================================================
const SUBMODELS_ROOT = path.resolve(
path.dirname(new URL(import.meta.url).pathname), "..", "submodels"
);
const submodelCache = new Map();
function loadSubmodel(slug) {
if (submodelCache.has(slug)) return submodelCache.get(slug);
const p = path.join(SUBMODELS_ROOT, `${slug}.yaml`);
const doc = yaml.load(readFileSync(p, "utf8"), { filename: p });
submodelCache.set(slug, doc);
return doc;
}
function normalizeCn(s) {
if (s == null) return "";
let v = String(s).trim();
if (v.startsWith("ex ")) v = v.slice(3).trim();
return v.replace(/\s+/g, "");
}
function cnMatches(measureCnCodes, queryCn) {
if (!Array.isArray(measureCnCodes)) return false;
const q = normalizeCn(queryCn);
if (!q) return false;
for (const c of measureCnCodes) {
const norm = normalizeCn(c);
if (!norm) continue;
// Match if either side is a prefix of the other.
// Register entries are typically 8- or 10-digit; queries are 8-digit.
// Wider entries like "7318" match any 7318* query.
if (q.startsWith(norm) || norm.startsWith(q)) return true;
}
return false;
}
function lookupByCnOriginConsigned(submodel, { cn_code, declared_origin, consigned_from }) {
const measures = submodel?.data?.measures ?? [];
const matches = [];
for (const m of measures) {
const origins = m.origins || [];
const cns = m.cn_codes || [];
const directOriginMatch = origins.includes(declared_origin) && cnMatches(cns, cn_code);
let extensionMatch = false;
for (const ext of m.anti_circumvention_extensions || []) {
if (ext.country === consigned_from && cnMatches(cns, cn_code)) {
extensionMatch = true; break;
}
}
if (directOriginMatch || extensionMatch) {
// Normalise to the shape the parent's risk decision reads (`measure.type`).
const view = {
...m,
type: m.instrument === "anti_dumping" ? "anti_dumping"
: m.instrument === "countervailing" ? "countervailing"
: m.instrument,
residual_rate_pct: m.rates?.residual_rate_pct,
individual_rate_min_pct: m.rates?.individual_rate_min_pct,
individual_rate_max_pct: m.rates?.individual_rate_max_pct,
cooperating_non_sampled_rate_pct: m.rates?.cooperating_non_sampled_rate_pct,
applied_via: extensionMatch ? "anti_circumvention_extension" : "direct_origin",
};
matches.push(view);
}
}
let lookup_status;
if (matches.length === 0) lookup_status = "not_found";
else if (matches.length === 1) lookup_status = "found";
else lookup_status = "found"; // multiple matches are normal (AD+CVD parallel)
return { measures: matches, lookup_status };
}
const LOOKUP_DISPATCH = {
lookup_by_cn_origin_consigned: lookupByCnOriginConsigned,
};
function dispatchInvoke(invoke, ctx) {
const submodel = loadSubmodel(invoke.submodel);
const fn = LOOKUP_DISPATCH[invoke.lookup];
if (!fn) throw new UnsupportedConstructError(`unknown submodel lookup: ${invoke.lookup}`);
// Resolve `with:` arguments: each value is either a literal or a context path.
const args = {};
for (const [k, v] of Object.entries(invoke.with || {})) {
args[k] = typeof v === "string" && ctx.has(v) ? ctx.get(v) : v;
}
return fn(submodel, args);
}
function collectRegisterExtensionCountries() {
const submodel = loadSubmodel("eu/eu_trade_defence_measures_register_v1");
const set = new Set();
for (const m of submodel?.data?.measures || []) {
for (const ext of m.anti_circumvention_extensions || []) {
if (ext?.country) set.add(ext.country);
}
}
return set;
}
// ============================================================
// Virtual-predicate construction
// ============================================================
function collectExtensionCountries(regulation) {
// Prefer the register submodel when the parent file has switched to delegation.
// Falls back to scanning the parent's inline rules if the submodel isn't available.
let set = new Set();
try { set = collectRegisterExtensionCountries(); } catch { /* ignore */ }
for (const decision of regulation.decisions || []) {
if (decision.id !== "active_measures_lookup") continue;
for (const rule of decision.rules || []) {
const ext = rule.then?.measure?.extended_to_consignments_from;
if (!Array.isArray(ext)) continue;
for (const e of ext) {
if (typeof e === "string") set.add(e);
else if (e?.country) set.add(e.country);
}
}
}
return set;
}
function buildVirtuals(extensionCountries) {
const v = new Map();
v.set("in_circumvention_extension_list",
(actual) => typeof actual === "string" && extensionCountries.has(actual));
v.set("not_in_circumvention_extension_list",
(actual) => !(typeof actual === "string" && extensionCountries.has(actual)));
return v;
}
// ============================================================
// Decision evaluator
// ============================================================
function fireRule(rule, ctx, scope, virtuals) {
try {
return evalWhen(rule.when, ctx, scope, virtuals) ? rule.then : null;
} catch (e) {
if (e instanceof UnsupportedConstructError) {
throw new UnsupportedConstructError(`rule "${rule.id}": ${e.message}`);
}
throw e;
}
}
function evalDecisionFirst(decision, ctx, virtuals) {
for (const rule of decision.rules || []) {
const out = fireRule(rule, ctx, {}, virtuals);
if (out) return { rule_id: rule.id, output: out };
}
return null;
}
function evalDecisionCollect(decision, ctx, virtuals, perMeasure = null) {
const out = [];
for (const rule of decision.rules || []) {
if (perMeasure && referencesMeasure(rule.when)) {
// Iterate over each measure record; if any iteration matches, fire once.
let fired = null;
for (const m of perMeasure) {
const r = fireRule(rule, ctx, { measure: m }, virtuals);
if (r) { fired = r; break; }
}
if (fired) out.push({ rule_id: rule.id, output: fired });
} else {
const r = fireRule(rule, ctx, {}, virtuals);
if (r) out.push({ rule_id: rule.id, output: r });
}
}
return out;
}
function referencesMeasure(when) {
if (!when || typeof when !== "object") return false;
for (const [k, v] of Object.entries(when)) {
if (k === "AND" || k === "OR") {
if (v.some(referencesMeasure)) return true;
} else if (k === "NOT") {
if (referencesMeasure(v)) return true;
} else if (k.startsWith("measure.")) {
return true;
}
}
return false;
}
// ============================================================
// Top-level orchestration for anti_dumping_china_v1 specifically
// ============================================================
function topLevelVerdict({ applicable, drcVerdict, ccVerdict }) {
if (!applicable && ccVerdict === "NOT_APPLICABLE") return "NOT_APPLICABLE";
// Cross-decision aggregation: any sub-verdict of INSUFFICIENT_DATA propagates
// even when applicability is false (the dossier still needs the evidence).
const verdicts = [drcVerdict, ccVerdict].filter(Boolean);
if (verdicts.includes("NON_CONFORMING")) return "NON_CONFORMING";
if (verdicts.includes("INSUFFICIENT_DATA")) return "INSUFFICIENT_DATA";
if (verdicts.every(v => v === "NOT_APPLICABLE")) return "NOT_APPLICABLE";
return "CONFORMING";
}
export function evaluateRegulation(regulation, fixtureInput, options = {}) {
const clock = parseDate(options.clock || DEFAULT_CLOCK);
const extensionCountries = collectExtensionCountries(regulation);
const virtuals = buildVirtuals(extensionCountries);
const ctx = new Ctx().setMany(fixtureInput);
const rationale = [];
let applicable = false;
let measures = [];
let drcVerdict = null;
let ccVerdict = null;
let evidence = [];
let risks = [];
for (const decision of regulation.decisions || []) {
if (decision.id === "applicability") {
const fired = evalDecisionFirst(decision, ctx, virtuals);
if (fired) {
applicable = !!fired.output.applicable;
ctx.set("applicable", applicable);
rationale.push({ decision: decision.id, rule_id: fired.rule_id });
}
continue;
}
if (decision.id === "active_measures_lookup") {
// Two shapes supported:
// (a) inline rules — each rule's then.measure is one record (legacy).
// (b) delegation — a single rule with then.invoke calling a submodel lookup.
const delegationRule = (decision.rules || []).find(r => r.then?.invoke);
let collected = [];
let lookupStatus = null;
if (delegationRule && evalWhen(delegationRule.when, ctx, {}, virtuals)) {
const result = dispatchInvoke(delegationRule.then.invoke, ctx);
measures = result.measures || [];
lookupStatus = result.lookup_status;
rationale.push({ decision: decision.id, rule_id: delegationRule.id });
} else {
collected = evalDecisionCollect(decision, ctx, virtuals);
measures = collected.map(c => c.output.measure);
for (const c of collected) rationale.push({ decision: decision.id, rule_id: c.rule_id });
lookupStatus = measures.length === 0 ? "not_found" : "found";
}
ctx.set("measures", measures);
// Fixture override wins so tests can exercise lookup_failed without
// having to corrupt the register itself.
if (!ctx.has("measure.lookup_status")) {
ctx.set("measure.lookup_status", lookupStatus);
}
// Derived facts after lookup.
const hasAd = measures.some(m => m?.type === "anti_dumping");
const hasCvd = measures.some(m => m?.type === "countervailing");
ctx.set("parallel_cvd_in_force", hasAd && hasCvd);
// Annotate each measure with derived per-measure flags.
for (const m of measures) {
if (m?.expiry) {
const exp = parseDate(m.expiry);
m.expiry_within_24_months = exp ? (exp <= addMonths(clock, 24)) : false;
}
if (m?.in_force_from) {
const inf = parseDate(m.in_force_from);
m.in_force_from_within_12_months = inf ? (inf >= addMonths(clock, -12)) : false;
}
}
continue;
}
if (decision.id === "duty_rate_compliance") {
const fired = evalDecisionFirst(decision, ctx, virtuals);
if (fired) {
drcVerdict = fired.output.verdict;
ctx.set("duty_rate_compliance.verdict", drcVerdict);
rationale.push({ decision: decision.id, rule_id: fired.rule_id });
}
continue;
}
if (decision.id === "circumvention_compliance") {
const fired = evalDecisionFirst(decision, ctx, virtuals);
if (fired) {
ccVerdict = fired.output.verdict;
ctx.set("circumvention_compliance.verdict", ccVerdict);
rationale.push({ decision: decision.id, rule_id: fired.rule_id });
}
continue;
}
if (decision.id === "evidence_requirements") {
const collected = evalDecisionCollect(decision, ctx, virtuals);
evidence = collected.map(c => c.output.evidence);
for (const c of collected) rationale.push({ decision: decision.id, rule_id: c.rule_id });
continue;
}
if (decision.id === "risks") {
const collected = evalDecisionCollect(decision, ctx, virtuals, measures);
risks = collected.map(c => c.output.risk);
for (const c of collected) rationale.push({ decision: decision.id, rule_id: c.rule_id });
continue;
}
throw new UnsupportedConstructError(`unknown decision id: ${decision.id}`);
}
return {
applicable,
verdict: topLevelVerdict({ applicable, drcVerdict, ccVerdict }),
measures,
evidence_required: evidence,
risks,
rationale,
sub_verdicts: { duty_rate_compliance: drcVerdict, circumvention_compliance: ccVerdict },
};
}