Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ The editor retains AI generation/modification, saved models, JSON/XMILE import a

Native engine improvements add bounded [arrays and reusable modules](docs/STRUCTURED-MODELS.md), [dimensional checks](docs/DIMENSIONAL-ANALYSIS.md), conservative [MDL import](docs/MDL-IMPORT.md), and [local feedback analysis](docs/FEEDBACK-ANALYSIS.md). Use **Source** to edit structured JSON without flattening the saved project. Inspect displays units evidence and can analyze a completed run. The [measured comparison](docs/ENGINE-COMPARISON.md) records a 32–35% improvement over our earlier scalar execution, while Simlin remains faster on those workloads. No Simlin runtime dependency was introduced.

The three previously recorded identifier/import failures are now repaired, with exact names preserved through import, simulation, rename and export. See the [identifier repair and compatibility evidence](docs/IDENTIFIER-IMPORT-FIX.md).

## Run locally

```bash
Expand Down Expand Up @@ -67,4 +69,4 @@ PYTHONPATH=. python scripts/check_mcp.py --stdio
PYTHONPATH=. python scripts/audit_library.py --full --tracked-only --output operations/library-full.json
```

Run the application before the MCP client check. Node.js is needed for JavaScript checks. API tests use disposable storage. Every active library model must pass; retired invalid inputs have separate causal-failure regression tests. Three existing XMILE identifier limitations remain expected failures. Offline pytest collection is restricted to tests/; root-level provider scripts are manual probes. Browser acceptance and alternate-engine/device limitations are recorded separately. Never synchronize model directories, databases, credentials or configuration over production. Reviewed per-file repairs and retirements require exact hashes and backups; preserve all other model data.
Run the application before the MCP client check. Node.js is needed for JavaScript checks. API tests use disposable storage. Every active library model must pass; retired invalid inputs have separate causal-failure regression tests. [Literal punctuation, EN SPACE and internal-dollar identifiers](docs/IDENTIFIER-IMPORT-FIX.md) now have passing identity/import/export regressions while unique legacy aliases remain compatible. Offline pytest collection is restricted to tests/; root-level provider scripts are manual probes. Browser acceptance and alternate-engine/device limitations are recorded separately. Never synchronize model directories, databases, credentials or configuration over production. Reviewed per-file repairs and retirements require exact hashes and backups; preserve all other model data.
40 changes: 24 additions & 16 deletions app/core/adaptive.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import ast
from fractions import Fraction
from scipy.integrate import solve_ivp
from app.core.equations import (EquationError, SafeEvaluator, canonical_name,
from app.core.equations import (EquationError, SafeEvaluator, canonical_name, symbol_identity, resolve_name,
decode_identifier, preprocess_equation)
from app.core.failure_diagnostics import capacity_error
from app.core.model import SimulationResult
Expand All @@ -19,7 +19,7 @@ class _UncertifiedBoundary(Exception):
pass


def _matching_branch_limits(condition, yes, no, aliases):
def _matching_branch_limits(condition, yes, no, aliases, resolve_key=canonical_name):
"""A bounded algebraic proof, never a numerical sampling heuristic.

Expand algebraic aliases, impose each comparison's equality boundary, and
Expand All @@ -35,7 +35,7 @@ def expand(node, active=()):
if budget[0] < 0 or len(active) > 80:
raise _UncertifiedBoundary
if isinstance(node, ast.Name):
name = canonical_name(decode_identifier(node.id))
name = resolve_key(decode_identifier(node.id))
if name in aliases:
if name in active:
raise _UncertifiedBoundary
Expand Down Expand Up @@ -229,23 +229,27 @@ def state_discontinuities(model):
continuous; IF needs matching branch limits. This is a conservative supported
syntax contract, not a general proof of existence, uniqueness or accuracy.
"""
elements = {canonical_name(e.name): e for e in [*model.variables, *model.flows]}
declared = tuple(e.name for e in (*model.stocks, *model.flows, *model.variables))
table_declarations = tuple(t.name for t in model.tables)
key = lambda name: symbol_identity(resolve_name(name, declared) or name)
table_key = lambda name: symbol_identity(resolve_name(name, table_declarations) or name)
elements = {symbol_identity(e.name): e for e in [*model.variables, *model.flows]}
trees = {name: ast.parse(preprocess_equation(e.equation), mode="eval").body
for name,e in elements.items() if e.equation.strip()}
stocks = {canonical_name(s.name) for s in model.stocks}
tables = {canonical_name(t.name): t.interpolation for t in model.tables}
stocks = {symbol_identity(s.name) for s in model.stocks}
tables = {symbol_identity(t.name): t.interpolation for t in model.tables}

def references(node):
functions = {id(n.func) for n in ast.walk(node) if isinstance(n, ast.Call)}
return {canonical_name(decode_identifier(n.id)) for n in ast.walk(node)
return {key(decode_identifier(n.id)) for n in ast.walk(node)
if isinstance(n, ast.Name) and id(n) not in functions}

state_names = set(stocks)
for _ in range(len(trees)+1):
added = {name for name,tree in trees.items() if references(tree) & state_names} - state_names
if not added: break
state_names.update(added)
needed = {canonical_name(f.name) for f in model.flows if f.from_stock is not None or f.to_stock is not None}
needed = {symbol_identity(f.name) for f in model.flows if f.from_stock is not None or f.to_stock is not None}
for _ in range(len(trees)+1):
added = {ref for name in needed if name in trees for ref in references(trees[name]) if ref in trees} - needed
if not added: break
Expand All @@ -259,7 +263,7 @@ def references(node):
condition, yes, no = (node.test, node.body, node.orelse) if isinstance(node, ast.IfExp) else (None,None,None)
if isinstance(node, ast.Call) and isinstance(node.func, ast.Name):
function = decode_identifier(node.func.id).upper()
if (canonical_name(function) in tables or function in {"LOOKUP", "WITH_LOOKUP"}) and references(node) & state_names:
if (table_key(function) in tables or function in {"LOOKUP", "WITH_LOOKUP"}) and references(node) & state_names:
# Even continuous interpolation can hide an entire narrow
# forcing pulse between internal steps. Table knots need
# event localization before this RHS contract can expand.
Expand All @@ -270,7 +274,7 @@ def references(node):
findings.append({"element":elements[name].name,"operation":function})
if condition is not None:
condition_nodes.update(id(n) for n in ast.walk(condition))
if references(condition) & state_names and not _matching_branch_limits(condition,yes,no,trees):
if references(condition) & state_names and not _matching_branch_limits(condition,yes,no,trees,key):
findings.append({"element":elements[name].name,"operation":"uncertified state-dependent conditional"})
for node in ast.walk(tree):
if isinstance(node, (ast.Compare, ast.BoolOp)) and id(node) not in condition_nodes and references(node) & state_names:
Expand All @@ -287,22 +291,26 @@ def time_discontinuities(model):
feedback conditions retain their ordinary ODE meaning. This is a conservative
input contract, not a proof that all accepted RHS functions are smooth.
"""
declared = tuple(e.name for e in (*model.stocks, *model.flows, *model.variables))
table_declarations = tuple(t.name for t in model.tables)
key = lambda name: symbol_identity(resolve_name(name, declared) or name)
table_key = lambda name: symbol_identity(resolve_name(name, table_declarations) or name)
trees = {}
table_names = {canonical_name(t.name) for t in model.tables}
table_names = {symbol_identity(t.name) for t in model.tables}
for element in [*model.variables, *model.flows]:
if element.equation.strip():
trees[element.name] = ast.parse(preprocess_equation(element.equation), mode="eval")
def references(node):
function_nodes = {id(n.func) for n in ast.walk(node) if isinstance(n, ast.Call)}
return {canonical_name(decode_identifier(n.id)) for n in ast.walk(node)
return {key(decode_identifier(n.id)) for n in ast.walk(node)
if isinstance(n, ast.Name) and id(n) not in function_nodes}
time_names = {"time"}
changed = True
while changed:
changed = False
for name, tree in trees.items():
if canonical_name(name) not in time_names and references(tree) & time_names:
time_names.add(canonical_name(name))
if key(name) not in time_names and references(tree) & time_names:
time_names.add(key(name))
changed = True
findings = []
for name, tree in trees.items():
Expand All @@ -315,7 +323,7 @@ def references(node):
# do not constrain internal steps. Until table-knot events are
# supported, reject explicit TIME-driven lookups rather than
# returning a successful but silently missed forcing pulse.
if canonical_name(function) in table_names:
if table_key(function) in table_names:
lookup_arguments = node.args
elif function == "LOOKUP":
lookup_arguments = node.args[1:] # table name is not an input
Expand All @@ -325,7 +333,7 @@ def references(node):
lookup_arguments = []
if any(references(argument) & time_names for argument in lookup_arguments):
findings.append({"element": name, "operation": "time-dependent lookup", "function": function})
if canonical_name(function) not in table_names:
if table_key(function) not in table_names:
if function in {"IF", "IF_THEN_ELSE"} and node.args:
condition = node.args[0]
elif function in {"FLOOR", "CEIL", "ROUND", "SIGN", "MOD"} and references(node) & time_names:
Expand Down
11 changes: 7 additions & 4 deletions app/core/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from collections import Counter
from pathlib import Path

from app.core.equations import (SAFE_CONSTANTS, canonical_name, decode_identifier,
from app.core.equations import (SAFE_CONSTANTS, canonical_name, symbol_identity, resolve_name, decode_identifier,
equation_features, get_equation_variables, preprocess_equation)
from app.core.model import Model, TimeSettings
from app.core.limits import MAX_INTERVALS, MAX_RETAINED_VALUES, MAX_WORK_UNITS
Expand Down Expand Up @@ -41,7 +41,7 @@ def _finite(value):
def equation_evidence(model):
"""Find unresolved names, syntax and sensitive operations without guessing fixes."""
elements = [*model.stocks, *model.flows, *model.variables]
originals = {canonical_name(e.name): e.name for e in elements}
originals = {symbol_identity(e.name): e.name for e in elements}
defined = set(originals) | {canonical_name(n) for n in SAFE_CONSTANTS} | {"time", "dt", "time_step"}
table_names = {canonical_name(t.name) for t in model.tables}
missing, domains, syntax, functions = [], [], [], set()
Expand Down Expand Up @@ -69,7 +69,10 @@ def equation_evidence(model):
stochastic = stochastic or bool(equation_features(equation, [t.name for t in model.tables])["random_calls"])
except Exception as error:
syntax.append({"element": element.name, "equation": equation, "error": str(error), "stage": "engine_feature_check"})
absent = sorted(n for n in get_equation_variables(equation) if canonical_name(n) not in defined and canonical_name(n) not in table_names)
absent = sorted(n for n in get_equation_variables(equation)
if resolve_name(n, [e.name for e in elements]) is None
and symbol_identity(n) not in {*SAFE_CONSTANTS, "time", "dt", "time_step"}
and resolve_name(n, [t.name for t in model.tables]) is None)
if absent:
missing.append({"element": element.name, "equation": equation, "undefined_symbols": absent})
operations = sorted(calls & {"LOG", "LN", "LOG10", "SQRT", "ASIN", "ACOS", "POWER", "EXP"})
Expand All @@ -79,7 +82,7 @@ def equation_evidence(model):
operations.append("power")
if operations:
domains.append({"element": element.name, "equation": equation, "operations_to_review": operations})
duplicates = sorted(n for n, count in Counter(canonical_name(e.name) for e in elements).items() if count > 1)
duplicates = sorted(n for n, count in Counter(symbol_identity(e.name) for e in elements).items() if count > 1)
return {"undefined_symbols": missing, "syntax": syntax, "ambiguous_names": duplicates,
"domain_sensitive_equations": domains, "called_functions": sorted(functions),
"stochastic": stochastic,
Expand Down
Loading
Loading