def merge_diffs(d1, d2):
    """
    Merge diffs `d1` and `d2`, returning a new diff which is
    equivalent to applying both diffs in sequence. Do not modify `d1`
    or `d2`.
    """
    if not isinstance(d1, dict) or not isinstance(d2, dict):
        return d2
    diff = d1.copy()
    for key, val in d2.items():
        diff[key] = merge_diffs(diff[key], val) if key in diff else val
    return diff