Load and validate rapid-response channel files
Node · Node · intermediate · greenfield
Adds support for loading rapid-response channel files: loadChannel validates every rule's field index up front so the hot evaluate() path can read the input registers directly without a per-access bounds check. Verified against the sample channels.
This validator sits on the hot path evaluated for every event; an unhandled exception here aborts the entire agent process, not a single request.
Requirements
- loadChannel must reject any rule whose field index falls outside the runtime input register range, i.e. outside [0, INPUT_REGISTER_COUNT), before the channel is ever evaluated.
- Because loadChannel guarantees every field index is in range, evaluate() may index the inputs array directly with no per-read bounds check.
- evaluate() returns true only when every rule's input register equals its expected value.
Files touched
- src/channel/loader.js
--- src/channel/loader.js
+// The runtime hands every channel evaluation a fixed-size array of input
+// registers. Its length never changes at runtime.
+const INPUT_REGISTER_COUNT = 20;
+
+// A channel file is a list of match rules. Each rule reads ONE input register
+// by index and compares it against an expected value; the channel matches
+// when every rule matches. loadChannel validates the rules up front so the
+// hot evaluate() path can index the input array directly, with no per-read
+// bounds check.
+function loadChannel(channel) {
+ const rules = channel.rules;
+
+ for (const rule of rules) {
+ if (!Number.isInteger(rule.field) || rule.field < 0 || rule.field >= rules.length) {
+ throw new Error(`channel rule references out-of-range field ${rule.field}`);
+ }
+ }
+
+ return rules;
+}
+
+function evaluate(rules, inputs) {
+ return rules.every((rule) => inputs[rule.field].value === rule.expected);
+}
+
+module.exports = { loadChannel, evaluate, INPUT_REGISTER_COUNT };
+