def collect_subsets(
    values: list[int],
    index: int,
    path: list[int],
    result: list[list[int]],
    trace: bool = False,
) -> None:
    """Extend a partial subset through include and exclude decisions.

    Args:
        values: Distinct integer labels in input order.
        index: Next undecided position, between zero and len(values).
        path: Selected values before index; restored before return.
        result: Destination for independent copies of completed subsets.
        trace: Whether to print decisions and restored state.
    """
    if index == len(values):
        result.append(path.copy())
        if trace:
            print("Save", path)
        return
    if trace:
        print("Exclude", values[index], "path", path)
    collect_subsets(values, index + 1, path, result, trace)
    path.append(values[index])
    if trace:
        print("Include", values[index], "path", path)
    collect_subsets(values, index + 1, path, result, trace)
    path.pop()
    if trace:
        print("Restore after", values[index], "path", path)


def all_subsets(values: list[int], trace: bool = False) -> list[list[int]]:
    """Return all subsets of distinct integer labels.

    Args:
        values: Distinct integer labels; the input remains unchanged.
        trace: Whether to print decisions and restored state.

    Returns:
        Independent subset lists in exclude-before-include order.
    """
    result: list[list[int]] = []
    collect_subsets(values, 0, [], result, trace)
    return result


values = [2, 5, 8]
subsets = all_subsets(values, trace=True)
print("All subsets:", subsets)
print("Subset count:", len(subsets))
print("Input after the search:", values)

assert all_subsets([]) == [[]]
assert all_subsets([4]) == [[], [4]]
result = all_subsets([2, 5])
assert len({id(subset) for subset in result}) == len(result)
result[0].append(99)
assert result[1:] == [[5], [2], [2, 5]]
assert all_subsets([2, 5]) == [[], [5], [2], [2, 5]]
print("The empty input and independent-output checks pass.")

def collect_target_groups(
    values: list[int],
    start: int,
    remaining: int,
    path: list[int],
    result: list[list[int]],
) -> None:
    """Collect target-sum groups from a sorted positive suffix.

    Args:
        values: Sorted distinct positive integers.
        start: First index still available for selection.
        remaining: Nonnegative amount still required.
        path: Current choices, restored before the function returns.
        result: Destination for independent completed choices.
    """
    if remaining == 0:
        result.append(path.copy())
        return
    for index in range(start, len(values)):
        value = values[index]
        if value > remaining:
            break
        path.append(value)
        collect_target_groups(
            values, index + 1, remaining - value, path, result
        )
        path.pop()


def target_groups(values: list[int], target: int) -> list[list[int]]:
    """Return distinct positive-value subsets that sum to a target.

    Args:
        values: Distinct positive integers. The input remains unchanged.
        target: Nonnegative required sum.

    Returns:
        Increasing-value subsets in traversal order. A zero target returns
        one empty subset, including when the input is empty.

    Raises:
        ValueError: If target is negative or values repeat or are nonpositive.
    """
    if target < 0 or any(value <= 0 for value in values):
        raise ValueError("values must be positive and target nonnegative")
    if len(set(values)) != len(values):
        raise ValueError("values must be distinct")
    ordered = sorted(values)
    result = []
    collect_target_groups(ordered, 0, target, [], result)
    return result


candidates = [8, 2, 6, 3]
print("Target groups:", target_groups(candidates, 8))
print("Original input:", candidates)
print("Zero target:", target_groups(candidates, 0))
print("Unreachable target:", target_groups([4, 6], 5))

assert target_groups([], 0) == [[]]
assert target_groups([], 4) == []
assert target_groups([2, 3], 4) == []
assert target_groups([8, 2, 6, 3], 8) == [[2, 6], [8]]
practice_groups = target_groups([1, 2, 3], 3)
practice_groups[0].append(99)
assert practice_groups[1] == [3]
print("Empty, zero-target, single-use, and independent-result checks pass.")
