File size: 2,193 Bytes
b29893e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.

"""PostMortem Environment Client."""

from typing import Dict

from openenv.core import EnvClient
from openenv.core.client_types import StepResult
from openenv.core.env_server.types import State

from .models import PostmortemAction, PostmortemObservation


class PostmortemEnv(EnvClient[PostmortemAction, PostmortemObservation, State]):
    """
    Client for the PostMortem incident-triage environment.

    Example:
        >>> client = PostmortemEnv.from_docker_image("postmortem_env-env:latest")
        >>> try:
        ...     r = client.reset()
        ...     print(r.observation.task_description)
        ...     r = client.step(PostmortemAction(tool="ack"))
        ... finally:
        ...     client.close()
    """

    def _step_payload(self, action: PostmortemAction) -> Dict:
        return {"tool": action.tool, "args": action.args}

    def _parse_result(self, payload: Dict) -> StepResult[PostmortemObservation]:
        obs_data = payload.get("observation", {})
        observation = PostmortemObservation(
            task_id=obs_data.get("task_id", ""),
            task_description=obs_data.get("task_description", ""),
            available_services=obs_data.get("available_services", []) or [],
            available_trace_ids=obs_data.get("available_trace_ids", []) or [],
            tool_result=obs_data.get("tool_result", ""),
            subgoals=obs_data.get("subgoals", {}) or {},
            reward_so_far=obs_data.get("reward_so_far", 0.0) or 0.0,
            steps_remaining=obs_data.get("steps_remaining", 0) or 0,
            last_error=obs_data.get("last_error", "") or "",
            done=payload.get("done", False),
            reward=payload.get("reward"),
            metadata=obs_data.get("metadata", {}) or {},
        )
        return StepResult(
            observation=observation,
            reward=payload.get("reward"),
            done=payload.get("done", False),
        )

    def _parse_state(self, payload: Dict) -> State:
        return State(
            episode_id=payload.get("episode_id"),
            step_count=payload.get("step_count", 0),
        )