Skip to content

The platform for continuously improving AI agents.

1.2 KB
Raw Blame
import asyncio

from modal.call_graph import InputStatus
from modal.client import synchronizer
from modal_proto import api_pb2


@synchronizer.create_blocking
async def reported_graph(call):
    # Run the internal RPC on the SDK's loop; its public graph API drops truncation.
    async with asyncio.timeout(10):
        await call.hydrate()
        return await call.client.stub.FunctionGetCallGraph(
            api_pb2.FunctionGetCallGraphRequest(function_call_id=call.object_id),
            retry=None,
            timeout=5,
        )


def finished(call):
    terminal = {
        InputStatus.SUCCESS,
        InputStatus.FAILURE,
        InputStatus.INIT_FAILURE,
        InputStatus.TERMINATED,
        InputStatus.TIMEOUT,
    }
    try:
        # A provider-reported terminal snapshot cannot attest to unreported descendants.
        graph = reported_graph(call)
        reported = {node.function_call_id for node in graph.inputs}
        return (
            not graph.truncated
            and call.object_id in reported
            and all(node.function_call_id in reported for node in graph.function_calls)
            and all(node.status in terminal for node in graph.inputs)
        )
    except Exception:
        return False