forked from coredipper/operon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path26_wiring_diagram_guarded_toolchain.py
More file actions
153 lines (129 loc) · 4.7 KB
/
26_wiring_diagram_guarded_toolchain.py
File metadata and controls
153 lines (129 loc) · 4.7 KB
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
#!/usr/bin/env python3
"""
Example 26: Wiring Diagram - Guarded Toolchain
==============================================
Shows rigorous WAgent wiring constraints:
- Integrity upgrades via explicit validator/attestor modules
- Type adapters for JSON <-> TEXT
- Approval-gated tool execution
- Capability aggregation across modules
This does not execute anything; it only validates wiring constraints.
Mermaid diagram:
examples/wiring_diagrams/example26_guarded_toolchain.md
"""
from operon_ai import (
Capability,
DataType,
IntegrityLabel,
ModuleSpec,
PortType,
WiringDiagram,
WiringError,
)
def main() -> None:
diagram = WiringDiagram()
diagram.add_module(
ModuleSpec(
name="user",
outputs={"request": PortType(DataType.TEXT, IntegrityLabel.UNTRUSTED)},
)
)
diagram.add_module(
ModuleSpec(
name="membrane",
inputs={"in": PortType(DataType.TEXT, IntegrityLabel.UNTRUSTED)},
outputs={"raw": PortType(DataType.TEXT, IntegrityLabel.UNTRUSTED)},
)
)
diagram.add_module(
ModuleSpec(
name="validator",
inputs={"raw": PortType(DataType.TEXT, IntegrityLabel.UNTRUSTED)},
outputs={"clean": PortType(DataType.TEXT, IntegrityLabel.VALIDATED)},
)
)
diagram.add_module(
ModuleSpec(
name="parser",
inputs={"text": PortType(DataType.TEXT, IntegrityLabel.VALIDATED)},
outputs={"plan": PortType(DataType.JSON, IntegrityLabel.VALIDATED)},
)
)
diagram.add_module(
ModuleSpec(
name="planner",
inputs={"plan": PortType(DataType.JSON, IntegrityLabel.VALIDATED)},
outputs={"refined": PortType(DataType.JSON, IntegrityLabel.VALIDATED)},
)
)
diagram.add_module(
ModuleSpec(
name="serializer",
inputs={"data": PortType(DataType.JSON, IntegrityLabel.VALIDATED)},
outputs={"text": PortType(DataType.TEXT, IntegrityLabel.VALIDATED)},
)
)
diagram.add_module(
ModuleSpec(
name="policy",
inputs={"summary": PortType(DataType.TEXT, IntegrityLabel.VALIDATED)},
outputs={"approval": PortType(DataType.APPROVAL, IntegrityLabel.TRUSTED)},
)
)
diagram.add_module(
ModuleSpec(
name="tool_builder",
inputs={"plan": PortType(DataType.JSON, IntegrityLabel.VALIDATED)},
outputs={"action": PortType(DataType.TOOL_CALL, IntegrityLabel.VALIDATED)},
capabilities={Capability.WRITE_FS},
)
)
diagram.add_module(
ModuleSpec(
name="attestor",
inputs={"clean": PortType(DataType.TEXT, IntegrityLabel.VALIDATED)},
outputs={"trusted": PortType(DataType.TEXT, IntegrityLabel.TRUSTED)},
)
)
diagram.add_module(
ModuleSpec(
name="operator_console",
inputs={"message": PortType(DataType.TEXT, IntegrityLabel.TRUSTED)},
)
)
diagram.add_module(
ModuleSpec(
name="sink",
inputs={
"action": PortType(DataType.TOOL_CALL, IntegrityLabel.VALIDATED),
"approval": PortType(DataType.APPROVAL, IntegrityLabel.TRUSTED),
},
)
)
diagram.connect("user", "request", "membrane", "in")
diagram.connect("membrane", "raw", "validator", "raw")
diagram.connect("validator", "clean", "parser", "text")
diagram.connect("parser", "plan", "planner", "plan")
# Demonstrate a type mismatch (JSON -> TEXT) and fix via serializer.
try:
diagram.connect("planner", "refined", "policy", "summary")
except WiringError as exc:
print(f"Expected wiring error: {exc}")
diagram.connect("planner", "refined", "serializer", "data")
diagram.connect("serializer", "text", "policy", "summary")
# Demonstrate an integrity violation (VALIDATED -> TRUSTED).
try:
diagram.connect("validator", "clean", "operator_console", "message")
except WiringError as exc:
print(f"Expected wiring error: {exc}")
diagram.connect("validator", "clean", "attestor", "clean")
diagram.connect("attestor", "trusted", "operator_console", "message")
# Guarded tool execution requires approval + action.
diagram.connect("planner", "refined", "tool_builder", "plan")
diagram.connect("tool_builder", "action", "sink", "action")
diagram.connect("policy", "approval", "sink", "approval")
print("✅ Wiring accepted")
required = sorted(diagram.required_capabilities(), key=lambda c: c.value)
print("Required capabilities:", [cap.value for cap in required])
if __name__ == "__main__":
main()