-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriarch_cli.py
More file actions
293 lines (240 loc) · 8.21 KB
/
triarch_cli.py
File metadata and controls
293 lines (240 loc) · 8.21 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/env python3
"""
TRIARCH PROTOCOL - CLI Vote Runner (LM Studio edition, Python)
This is a terminal interface that reuses triarch_core.TriarchEngine,
similar to the Gradio UI, but without any web UI.
Usage:
python triarch_cli.py
python triarch_cli.py --config triarch_vote_config.json
python triarch_cli.py --single-round
python triarch_cli.py --two-round
python triarch_cli.py --no-color
Copyright (c) 2025 DaragonTech
License: 3-clause BSD
See https://github.com/daragontech/Triarch for details
"""
import argparse
import sys
from typing import Optional
from triarch_core import TriarchEngine
# ---------------------------------------------------------------------------
# Small helpers
# ---------------------------------------------------------------------------
def ask(prompt: str) -> str:
try:
return input(prompt)
except EOFError:
return ""
def yes_no_abstain_prompt(label: str, default: Optional[str] = None) -> str:
"""
Loop until user enters Yes / No / Abstain (case-insensitive).
If default is provided and user hits Enter, returns default.
"""
while True:
raw = ask(label).strip()
if not raw and default:
return default
v = TriarchEngine.normalize_vote(raw)
if v:
return v
print("Please answer with Yes, No, or Abstain.\n")
def colorize(text: str, code: str, use_color: bool) -> str:
if not use_color:
return text
return "\033[{}m{}\033[0m".format(code, text)
def vote_badge(vote: Optional[str], use_color: bool) -> str:
v = (vote or "").upper()
if v == "YES":
return colorize("[YES]", "32;1", use_color) # bright green
if v == "NO":
return colorize("[NO]", "31;1", use_color) # bright red
if v == "ABSTAIN":
return colorize("[ABSTAIN]", "90;1", use_color) # gray
return "[?]"
def format_round_summary(payload: dict, round_num: int, use_color: bool) -> str:
key = "round{}".format(round_num)
block = payload.get(key)
if not block:
return "No data for round {}.\n".format(round_num)
decision = payload.get("decision", "")
lines = []
lines.append("=== ROUND {} SUMMARY ===".format(round_num))
lines.append("")
lines.append("Decision: {}".format(decision))
lines.append("")
human = block.get("human") or {}
h_vote = human.get("vote")
h_just = human.get("justification")
lines.append(
"Human {}: {}".format(
human.get("name"),
vote_badge(h_vote, use_color),
)
)
if h_just:
lines.append(" Justification: {}".format(h_just))
lines.append("")
lines.append("AI votes:")
for a in block.get("ais") or []:
lines.append(
"- {} ({}) -> {}".format(
a.get("id"),
a.get("role"),
vote_badge(a.get("vote"), use_color),
)
)
if a.get("justification"):
lines.append(" {}".format(a["justification"]))
lines.append("")
return "\n".join(lines)
def format_outcome(payload: dict, use_color: bool) -> str:
outcome = payload.get("outcome")
if not outcome:
return "No final outcome yet.\n"
passed = bool(outcome.get("passed"))
status = "APPROVED" if passed else "BLOCKED"
if passed:
status_colored = colorize(status, "32;1", use_color)
else:
status_colored = colorize(status, "31;1", use_color)
lines = []
lines.append("=== FINAL OUTCOME ===")
lines.append("")
lines.append("Decision: {}".format(status_colored))
lines.append("Resolution mode: {}".format(outcome.get("mode")))
lines.append("Reason: {}".format(outcome.get("reason")))
lines.append("Accountability: {}".format(outcome.get("responsibility")))
rf = (payload.get("meta") or {}).get("resultFile")
if rf:
lines.append("")
lines.append("Results saved to: {}".format(rf))
lines.append("")
return "\n".join(lines)
# ---------------------------------------------------------------------------
# Main CLI
# ---------------------------------------------------------------------------
def main() -> int:
parser = argparse.ArgumentParser(
description="TRIARCH Protocol - CLI Vote Runner (LM Studio, Python)"
)
parser.add_argument(
"--config",
default="triarch_vote_config.json",
help="Config JSON file (default: triarch_vote_config.json)",
)
parser.add_argument(
"--single-round",
action="store_true",
help="Force single-round mode (final decision after Round 1).",
)
parser.add_argument(
"--two-round",
action="store_true",
help="Force two-round mode (ignore TRIARCH_SINGLE_ROUND env).",
)
parser.add_argument(
"--no-color",
action="store_true",
help="Disable ANSI colors in output.",
)
args = parser.parse_args()
use_color = not args.no_color
engine = TriarchEngine(config_file=args.config)
# Determine single_round flag to pass to engine
single_round: Optional[bool]
if args.single_round and args.two_round:
print("Cannot use --single-round and --two-round together.")
return 1
elif args.single_round:
single_round = True
elif args.two_round:
single_round = False
else:
single_round = None # let engine use its default
print("TRIARCH Protocol - CLI Vote Runner")
print("Endpoint: {}".format(engine.endpoint))
print("Config: {}".format(args.config))
print("Human: {}".format(engine.human_name))
print(
"AIs: {}".format(
", ".join("{}={}".format(ai["id"], ai["model"]) for ai in engine.ais)
)
)
print(
"Mode: {}".format(
"Single-round (env or default)"
if engine.single_round_default
else "Two-round (env or default)"
)
)
if single_round is not None:
print("Override: {}".format("Single-round" if single_round else "Two-round"))
print("")
# Decision
decision_text = ask("Decision under vote: ").strip()
if not decision_text:
print("No decision provided, aborting.")
return 1
print("")
# Round 1 - Human
hv1 = yes_no_abstain_prompt(
"Round 1 - {} vote [Yes / No / Abstain]: ".format(engine.human_name)
)
human_just_r1 = ask(
"Optional human justification (Round 1) [press Enter to skip]: "
).strip()
print("")
print("Running Round 1 with the AIs...")
print("")
payload_r1 = engine.run_round1(
decision_text=decision_text,
human_vote_r1=hv1,
human_just_r1=human_just_r1,
single_round=single_round,
log_verbose=None,
)
# Show Round 1 summary
print(format_round_summary(payload_r1, 1, use_color))
# If single-round mode, we already have final outcome
if payload_r1.get("outcome"):
print(format_outcome(payload_r1, use_color))
return 0
# Otherwise, Round 2
print("=== ROUND 2 - Confirm or revise ===")
print("You can keep your previous vote by pressing Enter.")
print("")
prev_vote_label = hv1.capitalize()
prompt_r2 = (
"{} - Round 2 vote [Enter = keep {} / Yes / No / Abstain]: ".format(
engine.human_name, prev_vote_label
)
)
raw_r2 = ask(prompt_r2)
raw_r2 = raw_r2.strip()
if not raw_r2:
hv2_raw: Optional[str] = None # reuse Round 1 vote inside the engine
else:
hv2_norm = TriarchEngine.normalize_vote(raw_r2)
if not hv2_norm:
print("Could not parse Round 2 vote, keeping Round 1 vote.")
hv2_raw = None
else:
hv2_raw = raw_r2
human_just_r2 = ask(
"Optional human justification (Round 2) [press Enter to skip]: "
).strip()
print("")
print("Running Round 2 with the AIs...")
print("")
payload_r2 = engine.run_round2(
round1_payload=payload_r1,
human_vote_r2=hv2_raw,
human_just_r2=human_just_r2,
log_verbose=None,
)
# Show Round 2 summary and final outcome
print(format_round_summary(payload_r2, 2, use_color))
print(format_outcome(payload_r2, use_color))
return 0
if __name__ == "__main__":
sys.exit(main())