File size: 5,325 Bytes
ba70497
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""Shared types for the Wacky Wheels control tower.

The coordinator owns the pipeline. Specialists accept these types and return
plans. Nothing in here calls a tool or reads an event id's expected answer.
"""

from __future__ import annotations

from dataclasses import asdict, dataclass, field
from typing import Any, Literal

# The five kinds of message the handbook defines. A compound message becomes several Incidents,
# one per section.
IncidentKind = Literal[
    "supplier_delay",
    "plant_outage",
    "quality_hold",
    "noise",
    "unconfirmed",
]

# Tools that change the world. Reads and escalate are not in this set.
WRITE_TOOLS = frozenset({
    "mes.reschedule",
    "mes.transfer",
    "dealer.notify",
    "dealer.stop_sale",
    "crm.assign_loaner",
    "crm.notify_customer",
    "quality.hold_lot",
})


# Produced by WorldGraph.resolve_mentions(). start/end are character offsets in the text.
@dataclass
class Mention:
    """One entity spotted in event text, resolved through aliases or an id."""

    alias: str
    entity_type: str
    entity_id: str
    start: int
    end: int


# Output of step 1 (understand.py). Each kind fills only the fields it needs; the rest stay None.
@dataclass
class Incident:
    """One incident inside an event. A compound message yields several."""

    kind: IncidentKind
    summary: str
    supplier_id: str | None = None
    part_id: str | None = None
    plant_id: str | None = None
    shipment_id: str | None = None
    lot_id: str | None = None
    # Supplier delay: builds are blocked on [blocked_from, blocked_until).
    blocked_from: str | None = None
    blocked_until: str | None = None
    # Plant outage, inclusive working days.
    outage_start: str | None = None
    outage_end: str | None = None
    # Quality window used to identify the lot, inclusive.
    build_start: str | None = None
    build_end: str | None = None
    evidence: list[str] = field(default_factory=list)
    # Who produced this reading: the model name when its output was verified,
    # or "rules" when the deterministic reader was used.
    reader: str = "rules"
    # The model's own explanation of this section, whether or not its reading was used.
    model_reason: str = ""
    # Set when the tower needs more information from a person before it can act: the question
    # that names exactly what is missing. The event is escalated with it, and a reply re-reads it.
    question: str = ""

    def to_dict(self) -> dict[str, Any]:
        """Plain dict for traces.json; every field is already JSON-safe."""
        return asdict(self)


# One planned enterprise tool call plus a plain-English reason. Specialists create these; only the
# coordinator executes them.
@dataclass
class ToolCall:
    """A planned call: tool name and args exactly as POST /act takes them, plus the reason
    shown in the trace and UI."""

    tool: str
    args: dict[str, Any]
    reason: str
    # The agent that planned the call (roster id). Set by the coordinator when it accepts the plan.
    agent: str = ""

    @property
    def writes(self) -> bool:
        """True for calls that change the world. The coordinator counts these to decide
        whether a confirmed incident did anything, or must be escalated."""
        return self.tool in WRITE_TOOLS

    def to_dict(self) -> dict[str, Any]:
        return {"tool": self.tool, "args": self.args, "reason": self.reason, "writes": self.writes,
                "agent": self.agent}


# What a specialist hands back to the coordinator: the affected records, the graph paths that
# prove it, and its planned calls.
@dataclass
class SpecialistResult:
    """One specialist's answer: affected ids, the graph paths that cite them, and planned calls."""

    agent: str
    affected_ids: list[str]
    graph_paths: list[list[str]]
    calls: list[ToolCall]
    notes: list[str] = field(default_factory=list)

    def to_dict(self) -> dict[str, Any]:
        return {
            "agent": self.agent,
            "affected_ids": self.affected_ids,
            "graph_paths": self.graph_paths,
            "calls": [c.to_dict() for c in self.calls],
            "notes": self.notes,
        }


# One per event. run.py writes the list to runs/run1/traces.json and the UI reads it.
@dataclass
class EventTrace:
    """What the control tower shows for one event, and what we keep for audit."""

    event_id: str
    source: str
    received_at: str
    text: str
    classification: str
    incidents: list[Incident]
    results: list[SpecialistResult]
    calls: list[ToolCall]
    summary: str
    guardrail: str
    # Coordinator <-> agent messages, in order: who was asked what, and what came back.
    handoffs: list[dict[str, Any]] = field(default_factory=list)

    def to_dict(self) -> dict[str, Any]:
        return {
            "event_id": self.event_id,
            "source": self.source,
            "received_at": self.received_at,
            "text": self.text,
            "classification": self.classification,
            "incidents": [i.to_dict() for i in self.incidents],
            "results": [r.to_dict() for r in self.results],
            "calls": [c.to_dict() for c in self.calls],
            "summary": self.summary,
            "guardrail": self.guardrail,
            "handoffs": self.handoffs,
        }