HKUDS/Vibe-Trading · error · ValueError

unsupported operator: {type(node.op).__name__}

Error message

unsupported operator: {type(node.op).__name__}

What it means

Raised by the sandboxed arithmetic evaluator in financial_rigor_tool when an ast.BinOp uses an operator not present in _AST_BINOPS (only the basic numeric operators are whitelisted). This is a deliberate security restriction: arbitrary Python operators (e.g. bitwise |, &, <<) are rejected so the exact-Decimal calculator cannot be abused. The error names the offending operator class so you can see exactly what was disallowed.

Source

Thrown at agent/src/tools/financial_rigor_tool.py:104

    Args:
        node: An AST node from a parsed expression.

    Returns:
        The exact Decimal value of the node.

    Raises:
        ValueError: If the node is not a supported numeric/arithmetic form.
    """
    if isinstance(node, ast.Constant):
        # bool is a subclass of int — reject it explicitly.
        if isinstance(node.value, bool) or not isinstance(node.value, (int, float)):
            raise ValueError("only numeric constants are allowed")
        return _exact(node.value)
    if isinstance(node, ast.BinOp):
        op_fn = _AST_BINOPS.get(type(node.op))
        if op_fn is None:
            raise ValueError(f"unsupported operator: {type(node.op).__name__}")
        return op_fn(_eval_arith_node(node.left), _eval_arith_node(node.right))
    if isinstance(node, ast.UnaryOp):
        op_fn = _AST_UNARYOPS.get(type(node.op))
        if op_fn is None:
            raise ValueError(f"unsupported unary operator: {type(node.op).__name__}")
        return op_fn(_eval_arith_node(node.operand))
    raise ValueError(f"disallowed element in expression: {type(node).__name__}")


def _safe_arith(expr: str) -> Decimal:
    """Evaluate a numeric arithmetic expression in the exact-Decimal domain.

    The expression is parsed and evaluated recursively with Decimal arithmetic,
    so ``0.1 + 0.2`` is exactly ``0.3`` — no IEEE-754 drift, and no ``eval``.
    Only numbers and the operators ``+ - * /`` (with optional unary sign) are
    permitted; any other AST node raises ``ValueError``.

    Args:

View on GitHub (pinned to 80ffdda44c)

Solutions

  1. Rewrite the expression using only whitelisted arithmetic operators (+ - * / // % **)
  2. If a legitimate operator is missing, extend the _AST_BINOPS mapping in agent/src/tools/financial_rigor_tool.py with a Decimal-safe implementation
  3. Validate/normalize expressions upstream before calling exact_calc

Example fix

// before
exact_calc("shares = 1024 | 512")
// after
exact_calc("shares = 1024 + 512")
Defensive patterns

Strategy: validation

Validate before calling

import ast
ALLOWED_BINOPS = {ast.Add, ast.Sub, ast.Mult, ast.Div, ast.FloorDiv, ast.Mod, ast.Pow}
expr = "1 | 2"
tree = ast.parse(expr, mode="eval")
bad = [n.op for n in ast.walk(tree) if isinstance(n, ast.BinOp) and type(n.op) not in ALLOWED_BINOPS]
assert not bad, f"disallowed operators: {bad}"

Type guard

def uses_only_whitelisted_ops(expr: str) -> bool:
    import ast
    try:
        tree = ast.parse(expr, mode="eval")
    except SyntaxError:
        return False
    ok = {ast.Add, ast.Sub, ast.Mult, ast.Div, ast.FloorDiv, ast.Mod, ast.Pow}
    return all(type(n.op) in ok for n in ast.walk(tree) if isinstance(n, ast.BinOp))

Try / catch

try:
    result = exact_calc(expr)
except ValueError as e:
    if "unsupported operator" in str(e):
        expr = sanitize(expr)  # strip/replace disallowed ops
        result = exact_calc(expr)
    else:
        raise

Prevention

When it happens

Trigger: Calling exact_calc (or _safe_arith) with an expression containing a binary operator outside the whitelist, e.g. '1 | 2', '5 << 1', or '3 ^ 2'. Also triggered when a caller attempts Python syntax features that parse as BinOp with exotic ops.

Common situations: Agents or users passing bitwise/boolean expressions into a financial rigor tool that only supports +, -, *, /, //, %, **; pasting raw Python snippets into the calculator.

Related errors


AI-assisted analysis of HKUDS/Vibe-Trading@80ffdda44c (2026-08-28). Data as JSON: /api/errors/6b3251bc37b67fc3. Report an issue: GitHub.