106 lines
3.8 KiB
Python
106 lines
3.8 KiB
Python
"""Native Candidate execution lifecycle with mandatory cleanup evidence."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from typing import Any, Mapping
|
|
|
|
from .evidence import EvidenceChain
|
|
from .native_adapters import CandidateNativeAdapter, NativeSession
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class NativeCanaryResult:
|
|
provider: str
|
|
broker_session_id: str
|
|
provider_session_id: str
|
|
message_id: str
|
|
receipt: str
|
|
broker_session_verified: bool
|
|
openclaw_native_child_session_verified: bool
|
|
codex_child_session_verified: bool
|
|
simulation: bool
|
|
session_closed: bool
|
|
secrets_included: bool = False
|
|
|
|
|
|
class ExecutionManager:
|
|
def __init__(self, evidence: EvidenceChain) -> None:
|
|
self.evidence = evidence
|
|
|
|
def run_native_canary(
|
|
self,
|
|
adapter: CandidateNativeAdapter,
|
|
*,
|
|
task_id: str,
|
|
parent_session_id: str,
|
|
recipient_binding: Mapping[str, Any],
|
|
text: str,
|
|
) -> NativeCanaryResult:
|
|
session: NativeSession | None = None
|
|
closed = False
|
|
try:
|
|
session = adapter.spawn(task_id, parent_session_id, recipient_binding)
|
|
verification = adapter.verify(session)
|
|
message = adapter.send_text(session, text)
|
|
adapter.interrupt(session)
|
|
adapter.close(session)
|
|
closed = True
|
|
event_type = (
|
|
"NATIVE_SESSION_SIMULATION"
|
|
if verification.simulation
|
|
else "NATIVE_SESSION_VERIFIED"
|
|
)
|
|
self.evidence.append(
|
|
event_type,
|
|
task_id,
|
|
{
|
|
"provider": session.provider,
|
|
"broker_session_id": session.broker_session_id,
|
|
"provider_session_id": session.provider_session_id,
|
|
"message_id": message.message_id,
|
|
"receipt": message.receipt,
|
|
"broker_session_verified": verification.broker_session_verified,
|
|
"openclaw_native_child_session_verified": (
|
|
verification.openclaw_native_child_session_verified
|
|
),
|
|
"codex_child_session_verified": verification.codex_child_session_verified,
|
|
"simulation": verification.simulation,
|
|
"session_closed": True,
|
|
"secrets_included": False,
|
|
},
|
|
)
|
|
return NativeCanaryResult(
|
|
provider=session.provider,
|
|
broker_session_id=session.broker_session_id,
|
|
provider_session_id=session.provider_session_id,
|
|
message_id=message.message_id,
|
|
receipt=message.receipt,
|
|
broker_session_verified=verification.broker_session_verified,
|
|
openclaw_native_child_session_verified=(
|
|
verification.openclaw_native_child_session_verified
|
|
),
|
|
codex_child_session_verified=verification.codex_child_session_verified,
|
|
simulation=verification.simulation,
|
|
session_closed=True,
|
|
secrets_included=False,
|
|
)
|
|
except Exception as error:
|
|
if session is not None and not closed:
|
|
try:
|
|
adapter.close(session)
|
|
closed = True
|
|
except Exception:
|
|
closed = False
|
|
self.evidence.append(
|
|
"NATIVE_SESSION_FAILED",
|
|
task_id,
|
|
{
|
|
"provider": adapter.provider,
|
|
"error_code": type(error).__name__,
|
|
"session_closed": closed,
|
|
"secrets_included": False,
|
|
},
|
|
)
|
|
raise
|