"""Determinism-replay for operator tool calls.Captures golden tool-call records (the tool name, its arguments, and the resolvedinvocation) and replays them, asserting the resolution is byte-identical. Anon-deterministic mapping is an assurance failure in regulated work: the sameoperator action must always project onto the same deterministic CLI invocation.Pure: the resolver is injected, so this module stays free of the entrypointslayer it replays."""from__future__importannotationsfromcollections.abcimportCallable,IterablefrompydanticimportBaseModel,ConfigDict,Field_STRICT_FROZEN=ConfigDict(frozen=True,strict=True,validate_assignment=True,extra="forbid")#: A resolver projects a (tool_name, args) call onto its deterministic invocation.ToolCallResolver=Callable[[str,tuple[str,...]],tuple[str,...]]
[docs]classGoldenToolCall(BaseModel):"""A captured tool call and its resolved invocation."""model_config=_STRICT_FROZENtool_name:str=Field(min_length=1)args:tuple[str,...]invocation:tuple[str,...]
[docs]defrecord_tool_call(tool_name:str,args:tuple[str,...],*,resolve:ToolCallResolver)->GoldenToolCall:"""Capture a golden tool call by resolving it once. Returns: :class:`GoldenToolCall` containing the original call and invocation. """returnGoldenToolCall(tool_name=tool_name,args=args,invocation=resolve(tool_name,args))
[docs]defreplay_tool_call(record:GoldenToolCall,*,resolve:ToolCallResolver)->bool:"""Return True when re-resolving the record yields its captured invocation."""returnresolve(record.tool_name,record.args)==record.invocation
[docs]defdivergent_replays(records:Iterable[GoldenToolCall],*,resolve:ToolCallResolver,)->tuple[GoldenToolCall,...]:"""Return the records whose replay diverges from their captured invocation. Returns: Tuple of divergent :class:`GoldenToolCall` records. """returntuple(recordforrecordinrecordsifnotreplay_tool_call(record,resolve=resolve))