"""Pure post-trade reconciliation: compare intended vs actual signed positions; return the drift (actual -
intended) for any symbol off by more than tol_frac of the intended size (or any wholly-unexpected position).
A non-empty result means HALT — never trade past unexplained drift."""
from __future__ import annotations


def diff_positions(intended: dict[str, float], actual: dict[str, float], *, tol_frac: float) -> dict[str, float]:
    drift: dict[str, float] = {}
    for sym in set(intended) | set(actual):
        want = intended.get(sym, 0.0)
        have = actual.get(sym, 0.0)
        tol = max(abs(want) * tol_frac, 1e-9)
        if abs(have - want) > tol:
            drift[sym] = have - want
    return drift
