This commit is contained in:
Mateusz Gruszczyński
2026-09-02 22:41:27 +02:00
parent 8004be0841
commit db6d2f09db
20 changed files with 945 additions and 59 deletions
+1
View File
@@ -10,3 +10,4 @@ e00d211e3885e30d7fed1e43b44e6fdad40a67019060156c0641816a93e3365f ./network-debu
81345b6a0b51736bdbc98fd23199b62e4c721b4e7437e02dab7ea79b97dff29a ./service.sh
fec8b0362e763bbc115b5cc5c56f9ebe815730cee161632d81c5dc0ee1475f80 ./smoke.sh
b50782b3742dfbf8a319c60571c968e93fdf8547db747c759edcffae68cb98bf ./update.sh
9abc30f32476ffbab8d8073c7f2167a58732a1de9aeb30ceffb6708857a7f1f2 ./verify_flow_logic.py
+394
View File
@@ -0,0 +1,394 @@
#!/usr/bin/env python3
from __future__ import annotations
import json
import math
import re
from dataclasses import dataclass, field
from datetime import datetime, timedelta
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
CONDITIONS = {
'weekday','time_range','date_range','cron_trigger','stable_for','delay','rolling_stat','oscillates',
'outdoor_temperature','device_temperature','zone_temperature','ha_state','ha_numeric','ha_attribute',
'ha_available','house_mode','device_state','zone_state','group_state','night_mode','constant','shared_input'
}
LOGIC = {'logic_and','logic_or','logic_not'}
ACTIONS = {'zone_thermostat','device_action','group_action','ha_service_action'}
ALL = CONDITIONS | LOGIC | ACTIONS
passed = 0
def check(value, message):
global passed
if not value:
raise AssertionError(message)
passed += 1
def cron_field_valid(field: str, lo: int, hi: int, allow_seven=False):
upper = 7 if allow_seven else hi
if not field.strip(): return False
for part in field.split(','):
part = part.strip()
if part == '*': continue
if part.startswith('*/'):
try: step = int(part[2:])
except ValueError: return False
if not (0 < step <= hi - lo + 1): return False
continue
if '-' in part:
try: a,b = map(int, part.split('-',1))
except ValueError: return False
if not (lo <= a <= b <= upper): return False
continue
try: v = int(part)
except ValueError: return False
if not (lo <= v <= upper): return False
return True
def cron_valid(expr: str):
f=expr.split()
return len(f)==5 and cron_field_valid(f[0],0,59) and cron_field_valid(f[1],0,23) and cron_field_valid(f[2],1,31) and cron_field_valid(f[3],1,12) and cron_field_valid(f[4],0,6,True)
def cron_part_match(part, value, lo, hi):
if part == '*': return True
if part.startswith('*/'):
step=int(part[2:]); return lo <= value <= hi and (value-lo)%step==0
if '-' in part:
a,b=map(int,part.split('-',1)); return a <= value <= b and a >= lo and b <= hi
v=int(part); return v==value and lo <= v <= hi
def cron_field_match(field,value,lo,hi):
return any(cron_part_match(p.strip(),value,lo,hi) for p in field.split(','))
def cron_matches(expr: str, dt: datetime):
if not cron_valid(expr): return False
f=expr.split(); # Python Monday=0; backend Sunday=0
weekday=(dt.weekday()+1)%7
return (cron_field_match(f[0],dt.minute,0,59) and cron_field_match(f[1],dt.hour,0,23)
and cron_field_match(f[2],dt.day,1,31) and cron_field_match(f[3],dt.month,1,12)
and (cron_field_match(f[4],weekday,0,7) or (weekday==0 and cron_field_match(f[4],7,0,7))))
@dataclass
class Runtime:
since: datetime|None=None
samples: list[tuple[datetime,float]]=field(default_factory=list)
def timed_gate(rt: Runtime, input_value: bool, seconds: int, now: datetime):
if not input_value:
rt.since=None; return False
if rt.since is None: rt.since=now
return (now-rt.since).total_seconds() >= seconds
def rolling(rt: Runtime, sample: float, window: int, statistic: str, now: datetime):
cutoff=now-timedelta(seconds=window)
rt.samples=[x for x in rt.samples if x[0] >= cutoff]
rt.samples.append((now,sample))
vals=[v for _,v in rt.samples]
if statistic == 'median':
vals=sorted(vals); mid=len(vals)//2
return (vals[mid-1]+vals[mid])/2 if len(vals)%2==0 else vals[mid]
return sum(vals)/len(vals)
def oscillation_metrics(values):
if len(values)<3: return None
span=max(values)-min(values)
prev=0; changes=0
for a,b in zip(values,values[1:]):
d=b-a; sign=1 if d>1e-6 else -1 if d<-1e-6 else 0
if not sign: continue
if prev and sign != prev: changes += 1
prev=sign
return span,changes
def oscillates(rt: Runtime, sample: float, window: int, min_span: float, min_changes: int, now: datetime):
cutoff=now-timedelta(seconds=window)
rt.samples=[x for x in rt.samples if x[0] >= cutoff]
rt.samples.append((now,sample))
metrics=oscillation_metrics([v for _,v in rt.samples])
return bool(metrics and metrics[0] >= min_span and metrics[1] >= min_changes)
NUMERIC_OPS = {'lt','lte','gt','gte','eq','neq'}
TEXT_OPS = {'eq','neq'}
DEVICE_STATE_FIELDS = {'enabled','online','power','mode','fan_speed','swing_vertical','swing_horizontal','quiet','turbo','light','air','xfan','health','sleep'}
ZONE_STATE_FIELDS = {'enabled','mode','active_preset','demand','control_owner','device_manual_override','local_thermostat_power'}
THERMOSTAT_PRESETS = {'auto','comfort','sleep','away','custom'}
THERMOSTAT_MODES = {'auto','heat','cool'}
GROUP_PRESETS = {'auto','comfort','sleep','away','custom'}
GROUP_MODES = {'auto','house','cool','heat'}
ROLLING_SOURCES = {'outdoor_temperature','device_temperature','zone_temperature','ha_numeric'}
def nonempty(value):
return isinstance(value,str) and bool(value.strip())
def finite_number(value):
return isinstance(value,(int,float)) and not isinstance(value,bool) and math.isfinite(float(value))
def optional_text(value):
return value.strip() if isinstance(value,str) and value.strip() else None
def validate_numeric_comparison(config, name):
check(config.get('operator','lt') in NUMERIC_OPS, f'{name}: unsupported numeric operator')
check(finite_number(config.get('value')), f'{name}: numeric comparison needs finite value')
def validate_text_comparison_config(config, name):
check(config.get('operator','eq') in TEXT_OPS, f'{name}: unsupported text operator')
check('value' in config, f'{name}: state comparison needs value')
def validate_source_config(config, name):
source=config.get('source')
check(source in ROLLING_SOURCES, f'{name}: unsupported statistic source')
if source == 'device_temperature': check(nonempty(config.get('device_id')), f'{name}: device source needs device_id')
if source == 'zone_temperature': check(nonempty(config.get('zone_id')), f'{name}: zone source needs zone_id')
if source == 'ha_numeric': check(nonempty(config.get('entity_id')), f'{name}: HA source needs entity_id')
def validate_node_config(node, name):
kind=node['kind']; c=node.get('config',{})
check(isinstance(c,dict), f'{name}/{node.get("id")}: config is not an object')
tag=f'{name}/{node.get("id")}:{kind}'
if kind == 'weekday':
days=c.get('days'); check(isinstance(days,list) and bool(days), f'{tag}: weekdays missing')
check(all(isinstance(d,int) and not isinstance(d,bool) and 1 <= d <= 7 for d in days), f'{tag}: invalid weekday')
elif kind == 'time_range':
for key in ('start','end'):
try: datetime.strptime(c.get(key,''),'%H:%M')
except (TypeError,ValueError): check(False, f'{tag}: invalid {key} time')
else: check(True, f'{tag}: valid {key} time')
elif kind == 'date_range':
try:
start=datetime.strptime(c.get('start',''),'%Y-%m-%d').date(); end=datetime.strptime(c.get('end',''),'%Y-%m-%d').date()
except (TypeError,ValueError):
check(False, f'{tag}: invalid date range')
else:
check(start <= end, f'{tag}: reversed date range')
elif kind == 'cron_trigger':
check(nonempty(c.get('expression')) and cron_valid(c['expression']), f'{tag}: invalid cron')
elif kind in {'stable_for','delay'}:
seconds=c.get('seconds'); check(isinstance(seconds,int) and not isinstance(seconds,bool) and 1 <= seconds <= 604800, f'{tag}: invalid duration')
elif kind == 'rolling_stat':
validate_source_config(c,tag)
window=c.get('window_seconds'); check(isinstance(window,int) and not isinstance(window,bool) and 10 <= window <= 604800, f'{tag}: invalid window')
check(c.get('statistic') in {'mean','median'}, f'{tag}: invalid statistic')
validate_numeric_comparison(c,tag)
elif kind == 'oscillates':
validate_source_config(c,tag)
window=c.get('window_seconds'); check(isinstance(window,int) and not isinstance(window,bool) and 10 <= window <= 604800, f'{tag}: invalid window')
check(finite_number(c.get('min_span')) and float(c['min_span']) > 0, f'{tag}: min_span must be positive')
changes=c.get('min_direction_changes'); check(isinstance(changes,int) and not isinstance(changes,bool) and 1 <= changes <= 1000, f'{tag}: invalid direction-change count')
elif kind == 'outdoor_temperature':
validate_numeric_comparison(c,tag)
elif kind == 'device_temperature':
check(nonempty(c.get('device_id')), f'{tag}: device_id missing'); validate_numeric_comparison(c,tag)
elif kind == 'zone_temperature':
check(nonempty(c.get('zone_id')), f'{tag}: zone_id missing'); validate_numeric_comparison(c,tag)
elif kind == 'ha_state':
check(nonempty(c.get('entity_id')), f'{tag}: entity_id missing'); validate_text_comparison_config(c,tag)
elif kind == 'ha_numeric':
check(nonempty(c.get('entity_id')), f'{tag}: entity_id missing'); validate_numeric_comparison(c,tag)
elif kind == 'ha_attribute':
check(nonempty(c.get('entity_id')), f'{tag}: entity_id missing')
check(nonempty(c.get('attribute')), f'{tag}: attribute missing')
check(c.get('operator','eq') in NUMERIC_OPS, f'{tag}: invalid attribute operator')
check('value' in c, f'{tag}: attribute value missing')
elif kind == 'ha_available':
check(nonempty(c.get('entity_id')), f'{tag}: entity_id missing')
elif kind == 'house_mode':
check(c.get('value') in {'cool','heat','off'}, f'{tag}: invalid house mode'); validate_text_comparison_config(c,tag)
elif kind == 'device_state':
check(nonempty(c.get('device_id')), f'{tag}: device_id missing')
check(c.get('field') in DEVICE_STATE_FIELDS, f'{tag}: invalid device field'); validate_text_comparison_config(c,tag)
elif kind == 'zone_state':
check(nonempty(c.get('zone_id')), f'{tag}: zone_id missing')
check(c.get('field') in ZONE_STATE_FIELDS, f'{tag}: invalid zone field'); validate_text_comparison_config(c,tag)
elif kind == 'group_state':
check(nonempty(c.get('group_id')), f'{tag}: group_id missing')
check(c.get('field') == 'power_enabled', f'{tag}: invalid group field'); validate_text_comparison_config(c,tag)
elif kind == 'constant':
check(isinstance(c.get('value'),bool), f'{tag}: constant needs boolean')
elif kind == 'shared_input':
check(nonempty(c.get('input_id')), f'{tag}: input_id missing')
elif kind == 'zone_thermostat':
check(nonempty(c.get('zone_id')), f'{tag}: zone_id missing')
preset=optional_text(c.get('preset')) or 'comfort'; check(preset in THERMOSTAT_PRESETS, f'{tag}: invalid preset')
mode=optional_text(c.get('mode')); check(mode is None or mode in THERMOSTAT_MODES, f'{tag}: invalid mode')
if preset == 'custom': check(finite_number(c.get('setpoint')) and 8 <= float(c['setpoint']) <= 30, f'{tag}: invalid custom target')
if 'power' in c and c['power'] is not None: check(isinstance(c['power'],bool), f'{tag}: power is not boolean')
if 'cooldown_seconds' in c: check(isinstance(c['cooldown_seconds'],int) and c['cooldown_seconds'] >= 0, f'{tag}: invalid cooldown')
elif kind == 'device_action':
check(nonempty(c.get('device_id')), f'{tag}: device_id missing')
fields=('power','mode','target_temperature','fan_speed','swing_vertical','swing_horizontal','quiet','turbo','light','air','xfan','health','sleep')
check(any(c.get(field) is not None for field in fields), f'{tag}: empty device action')
elif kind == 'group_action':
check(nonempty(c.get('group_id')), f'{tag}: group_id missing')
mode=optional_text(c.get('mode')); preset=optional_text(c.get('preset'))
check(mode is None or mode in GROUP_MODES, f'{tag}: invalid group mode')
check(preset is None or preset in GROUP_PRESETS, f'{tag}: invalid group preset')
check(c.get('power') is not None or mode is not None or preset is not None, f'{tag}: empty group action')
if preset == 'custom': check(finite_number(c.get('setpoint')) and 8 <= float(c['setpoint']) <= 30, f'{tag}: invalid custom target')
elif kind == 'ha_service_action':
check(nonempty(c.get('domain')), f'{tag}: domain missing'); check(nonempty(c.get('service')), f'{tag}: service missing')
# Empty entity_id is intentionally treated as omitted by backend flow_string().
check(isinstance(c.get('data',{}),dict), f'{tag}: HA service data must be object')
elif kind in LOGIC or kind == 'night_mode':
pass
def flow_action_truth_table(flow, action_id):
by={n['id']:n for n in flow.get('nodes',[])}
incoming={}
for edge in flow.get('edges',[]): incoming.setdefault(edge['to'],[]).append(edge['from'])
ancestors=set(); stack=list(incoming.get(action_id,[]))
while stack:
node_id=stack.pop()
if node_id in ancestors: continue
ancestors.add(node_id); stack.extend(incoming.get(node_id,[]))
primitive=[node_id for node_id in ancestors if by[node_id]['kind'] not in LOGIC and by[node_id]['kind'] not in {'stable_for','delay'}]
check(len(primitive) <= 16, f'action {action_id}: truth table unexpectedly large')
def eval_node(node_id, assigned, memo):
if node_id in memo: return memo[node_id]
node=by[node_id]; kind=node['kind']; ins=incoming.get(node_id,[])
if kind == 'logic_and': value=bool(ins) and all(eval_node(x,assigned,memo) for x in ins)
elif kind == 'logic_or': value=bool(ins) and any(eval_node(x,assigned,memo) for x in ins)
elif kind == 'logic_not': value=len(ins)==1 and not eval_node(ins[0],assigned,memo)
elif kind in {'stable_for','delay'}: value=len(ins)==1 and eval_node(ins[0],assigned,memo)
else:
predecessors=all(eval_node(x,assigned,memo) for x in ins)
value=predecessors and assigned.get(node_id,False)
memo[node_id]=value; return value
outcomes=[]
for mask in range(1 << len(primitive)):
assigned={node_id: bool(mask & (1 << index)) for index,node_id in enumerate(primitive)}
memo={}
outcomes.append(bool(incoming.get(action_id)) and all(eval_node(x,assigned,memo) for x in incoming.get(action_id,[])))
return outcomes
def validate_graph(flow, name):
nodes=flow.get('nodes',[]); edges=flow.get('edges',[])
check(bool(nodes), f'{name}: no nodes')
ids=[n.get('id') for n in nodes]
check(all(ids) and len(ids)==len(set(ids)), f'{name}: duplicate/empty node ids')
by={n['id']:n for n in nodes}
check(all(n.get('kind') in ALL for n in nodes), f'{name}: unsupported node kind')
check(any(n['kind'] in ACTIONS for n in nodes), f'{name}: no action')
edge_ids=[e.get('id') for e in edges]
check(all(edge_ids) and len(edge_ids)==len(set(edge_ids)), f'{name}: duplicate/empty edge ids')
pairs=[]
incoming={}
outgoing={}
for e in edges:
a,b=e.get('from'),e.get('to')
check(a in by and b in by and a != b, f'{name}: invalid edge endpoint')
check(by[a]['kind'] not in ACTIONS, f'{name}: action has outgoing edge')
pairs.append((a,b)); incoming.setdefault(b,[]).append(a); outgoing.setdefault(a,[]).append(b)
check(len(pairs)==len(set(pairs)), f'{name}: duplicate connections')
# DAG
temp=set(); done=set()
def visit(x):
if x in done: return
check(x not in temp, f'{name}: cycle')
temp.add(x)
for y in outgoing.get(x,[]): visit(y)
temp.remove(x); done.add(x)
for x in ids: visit(x)
for n in nodes:
validate_node_config(n, name)
ins=incoming.get(n['id'],[])
if n['kind']=='logic_not': check(len(ins)==1, f'{name}: NOT arity')
if n['kind'] in {'logic_and','logic_or'}: check(len(ins)>=1, f'{name}: logic arity')
if n['kind'] in {'stable_for','delay'}: check(len(ins)==1, f'{name}: timed block arity')
if n['kind'] in ACTIONS: check(len(ins)>=1, f'{name}: action without condition')
for action in (n for n in nodes if n['kind'] in ACTIONS):
outcomes=flow_action_truth_table(flow, action['id'])
check(any(outcomes), f'{name}/{action["id"]}: action condition graph can never become true')
return len(nodes),len(edges)
def main():
# Deterministic backend semantics.
check(cron_valid('*/5 * * * *'), 'cron */5 invalid')
check(cron_valid('0,15,30,45 6-18 * * 1-5'), 'cron list/range invalid')
for bad in ('* * * *','*/0 * * * *','61 * * * *','* 24 * * *','* * 0 * *','* * * 13 *','* * * * 8','*/100 * * * *'):
check(not cron_valid(bad), f'cron accepted invalid: {bad}')
monday=datetime(2026,9,7,10,15)
check(cron_matches('15 10 * * 1', monday), 'cron exact weekday mismatch')
check(cron_matches('15 10 * * 1-7', monday), 'cron weekday range containing 7 mismatch')
check(not cron_matches('16 10 * * 1', monday), 'cron false positive')
sunday=datetime(2026,9,6,8,0)
check(cron_matches('0 8 * * 0', sunday) and cron_matches('0 8 * * 7', sunday) and cron_matches('0 8 * * 1-7', sunday), 'cron Sunday 0/7/range mismatch')
base=datetime(2026,9,2,12,0,0)
rt=Runtime(); check(not timed_gate(rt,True,30,base),'stable fired immediately'); check(not timed_gate(rt,True,30,base+timedelta(seconds=29)),'stable fired early'); check(timed_gate(rt,True,30,base+timedelta(seconds=30)),'stable did not fire'); check(not timed_gate(rt,False,30,base+timedelta(seconds=31)),'stable did not reset'); check(not timed_gate(rt,True,30,base+timedelta(seconds=32)),'stable restart should wait')
rt=Runtime(); check(not timed_gate(rt,True,3,base),'delay immediate'); check(timed_gate(rt,True,3,base+timedelta(seconds=3)),'delay did not pass after wait')
rt=Runtime(); check(math.isclose(rolling(rt,10,60,'mean',base),10),'mean one'); check(math.isclose(rolling(rt,20,60,'mean',base+timedelta(seconds=10)),15),'mean two'); check(math.isclose(rolling(rt,30,60,'median',base+timedelta(seconds=20)),20),'median odd'); check(math.isclose(rolling(rt,40,60,'median',base+timedelta(seconds=30)),25),'median even'); check(math.isclose(rolling(rt,100,60,'mean',base+timedelta(seconds=100)),100),'window prune')
rt=Runtime();
for i,v in enumerate((20,22,19,23,20)):
result=oscillates(rt,v,300,3,2,base+timedelta(seconds=i*10))
check(result,'oscillation not detected')
rt=Runtime();
for i,v in enumerate((20,21,22,23,24)):
result=oscillates(rt,v,300,3,2,base+timedelta(seconds=i*10))
check(not result,'monotonic trend detected as oscillation')
# Static backend/frontend wiring checks.
engine=(ROOT/'src/engine/automations.rs').read_text()
api=(ROOT/'src/api/flows.rs').read_text()
js=(ROOT/'web/js/flows.js').read_text()
html=(ROOT/'web/index.html').read_text()
for kind in ('cron_trigger','stable_for','delay','rolling_stat','oscillates'):
check(f'"{kind}"' in api and f'"{kind}"' in engine, f'{kind}: backend wiring missing')
check(kind in js and f'data-flow-add="{kind}"' in html, f'{kind}: UI wiring missing')
meta_block=js.split('const FLOW_NODE_META = Object.freeze({',1)[1].split('});',1)[0]
meta_kinds=set(re.findall(r'^\s*([a-z_]+):\s*\{', meta_block, re.M))
palette_kinds=set(re.findall(r'data-flow-add="([a-z_]+)"', html))
check(meta_kinds == ALL, f'UI metadata/backend kind mismatch: missing={ALL-meta_kinds}, extra={meta_kinds-ALL}')
check(palette_kinds == ALL, f'palette/backend kind mismatch: missing={ALL-palette_kinds}, extra={palette_kinds-ALL}')
check('"ha_service_action" => Ok(None)' in api, 'HA action missing dry-run support')
check('home_assistant::call_service' in engine and 'action_ha_domain' in engine, 'HA action runtime missing')
check('format!("ha:{entity_id}")' in engine, 'HA target conflict claim missing')
check('Some(&mut runtime)' in api, 'dry-run does not use stateful runtime')
check('conditions.iter().any(|condition| condition.kind == "cron_trigger")' in engine and 'last.minute() == now.minute()' in engine, 'CRON same-minute duplicate guard missing')
check('previous.config == current.config' in api and 'runtime.retain' in api, 'runtime reset-on-edit protection missing')
runtime_leaf=engine.split('async fn flow_leaf_observation',1)[1].split('pub async fn evaluate_flow_conditions_trace',1)[0]
for kind in CONDITIONS - {'stable_for','delay','rolling_stat','oscillates'}:
check(f'\"{kind}\" =>' in runtime_leaf or (kind == 'shared_input' and 'condition.kind == \"shared_input\"' in runtime_leaf), f'{kind}: runtime leaf implementation missing')
evaluator=engine.split('pub async fn evaluate_flow_conditions_trace',1)[1].split('async fn flow_conditions_match',1)[0]
for kind in ('stable_for','delay','rolling_stat','oscillates','logic_and','logic_or','logic_not'):
check(f'\"{kind}\"' in evaluator, f'{kind}: evaluator implementation missing')
for category in ('trigger','time','timeop','sensor','logic','action','haaction'):
check(f'flow-palette-{category}' in html, f'palette category {category} missing')
css=(ROOT/'web/styles.css').read_text()
for marker in ('.flow-palette-group button', 'min-height:30px', 'padding:6px 8px', '.flow-palette-timeop', '.flow-palette-haaction'):
check(marker in css, f'compact/color Flow CSS missing: {marker}')
# Translation pack correctness: all new keys must be inside translations, not root.
for lang in ('pl','en'):
pack=json.loads((ROOT/f'lang/{lang}.json').read_text())
check(not any(k.startswith('flow.') for k in pack), f'{lang}: flow translations leaked to root')
for key in ('flow.triggers','flow.timeOps','flow.haActions','flow.node.cronTrigger','flow.node.stableFor','flow.node.delay','flow.node.rollingStat','flow.node.oscillates','flow.node.haServiceAction'):
check(bool(pack.get('translations',{}).get(key)), f'{lang}: missing {key}')
title_keys=set(re.findall(r"titleKey: '([^']+)'", meta_block))
missing_titles=sorted(key for key in title_keys if not pack.get('translations',{}).get(key))
check(not missing_titles, f'{lang}: missing Flow node title translations: {missing_titles}')
referenced_flow_keys=set(re.findall(r'''(?:data-i18n(?:-[a-z]+)?=|t\()\s*[\"'](flow\.[A-Za-z0-9_.]+)[\"']''', js + '\n' + html))
missing_referenced=sorted(key for key in referenced_flow_keys if key not in pack.get('translations',{}))
check(not missing_referenced, f'{lang}: missing referenced Flow translations: {missing_referenced}')
# Regression pass over every bundled Flow preset.
preset_files=sorted((ROOT/'presets').glob('*.json'))
check(len(preset_files) >= 30, 'unexpectedly small preset library')
nodes=edges=0
for f in preset_files:
data=json.loads(f.read_text())
n,e=validate_graph(data['flow'],f.name); nodes+=n; edges+=e
print(f'PASS: {passed} logical assertions; {len(preset_files)} presets; {nodes} nodes; {edges} edges')
if __name__=='__main__': main()