"""Phase 3 — Act: accept reasoning output, execute and produce feedback.

This is the command phase. It receives the reasoning result from Phase 2
and takes action. In the stub form, it passes the payload through with a
phase marker and produces feedback for the next cycle.
"""

from __future__ import annotations

import logging

from loop.schema import ContextPayload

logger = logging.getLogger(__name__)


def act(payload: ContextPayload) -> ContextPayload:
    """Accept reasoning result and return action output + feedback.

    Stub: tags the payload with phase=act and logs transit.
    Timmy will flesh this out with tool execution, delegation,
    response generation, and feedback construction.
    """
    logger.info(
        "Phase 3 (Act) received: source=%s reasoned=%s",
        payload.source,
        payload.metadata.get("reasoned", False),
    )

    result = payload.with_metadata(phase="act", acted=True)

    logger.info(
        "Phase 3 (Act) produced: metadata_keys=%s",
        sorted(result.metadata.keys()),
    )
    return result
