zyonix-cli 0.1.13__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- zyonix_cli/__init__.py +2 -0
- zyonix_cli/agent_init.py +228 -0
- zyonix_cli/auth_cmd.py +499 -0
- zyonix_cli/auth_store.py +124 -0
- zyonix_cli/brief.py +271 -0
- zyonix_cli/checks_cmd.py +160 -0
- zyonix_cli/client.py +130 -0
- zyonix_cli/cmdkit.py +142 -0
- zyonix_cli/common.py +94 -0
- zyonix_cli/context_view.py +116 -0
- zyonix_cli/deploy_cmd.py +52 -0
- zyonix_cli/drafts_cmd.py +260 -0
- zyonix_cli/explain.py +283 -0
- zyonix_cli/fix_cmd.py +284 -0
- zyonix_cli/guard_cmd.py +125 -0
- zyonix_cli/helpview.py +171 -0
- zyonix_cli/importer.py +274 -0
- zyonix_cli/issues_cmd.py +597 -0
- zyonix_cli/jobs_cmd.py +71 -0
- zyonix_cli/junit.py +49 -0
- zyonix_cli/mcp_cmd.py +96 -0
- zyonix_cli/mcp_server.py +375 -0
- zyonix_cli/monitor_cmd.py +120 -0
- zyonix_cli/perf_view.py +51 -0
- zyonix_cli/progress.py +57 -0
- zyonix_cli/prompts_cmd.py +166 -0
- zyonix_cli/regression_cmd.py +216 -0
- zyonix_cli/render.py +313 -0
- zyonix_cli/replay_cmd.py +215 -0
- zyonix_cli/review_cmd.py +169 -0
- zyonix_cli/runner.py +213 -0
- zyonix_cli/runs_cmd.py +638 -0
- zyonix_cli/spec_cmd.py +74 -0
- zyonix_cli/status_cmd.py +488 -0
- zyonix_cli/term.py +206 -0
- zyonix_cli/test_cmd.py +248 -0
- zyonix_cli/triage.py +215 -0
- zyonix_cli/usage.py +172 -0
- zyonix_cli/verify.py +223 -0
- zyonix_cli/verify_view.py +87 -0
- zyonix_cli-0.1.13.dist-info/METADATA +58 -0
- zyonix_cli-0.1.13.dist-info/RECORD +45 -0
- zyonix_cli-0.1.13.dist-info/WHEEL +4 -0
- zyonix_cli-0.1.13.dist-info/entry_points.txt +21 -0
- zyonix_cli-0.1.13.dist-info/licenses/LICENSE +202 -0
zyonix_cli/explain.py
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
"""Plain words for what the server recorded: checks, tiers, lifecycle stages, and which key an
|
|
2
|
+
action needs. Shared by `zyonix status`, `zyonix issues show` and the help.
|
|
3
|
+
|
|
4
|
+
Nothing here decides anything. A check's sentence describes the check id (``required:total`` is
|
|
5
|
+
"a required field is missing"), not the run; the verdict, the root cause and its confidence are
|
|
6
|
+
the server's; the lifecycle text describes the transitions the Loop Controller allows
|
|
7
|
+
(``server/.../loop/controller.py``); the key table only words the server's own rule
|
|
8
|
+
(``server/.../auth.py`` SCOPES), which still enforces every request."""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import textwrap
|
|
13
|
+
from typing import Any
|
|
14
|
+
|
|
15
|
+
from zyonix_cli import term
|
|
16
|
+
|
|
17
|
+
# ------------------------------------------------------------------------------ checks
|
|
18
|
+
|
|
19
|
+
TIERS = {
|
|
20
|
+
"T0": "execution check",
|
|
21
|
+
"T1": "schema check",
|
|
22
|
+
"T2": "business-rule check",
|
|
23
|
+
"T3": "grounding check",
|
|
24
|
+
"T4": "ground-truth check",
|
|
25
|
+
"T7": "human feedback",
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
_FIELD_CHECKS = {
|
|
29
|
+
"required": "The output is missing a required field: {f}.",
|
|
30
|
+
"type": "The field {f} has the wrong type.",
|
|
31
|
+
"format": "The field {f} is not in the expected format.",
|
|
32
|
+
"enum": "The field {f} has a value outside the allowed list.",
|
|
33
|
+
"grounding": "The value of {f} could not be verified against the available source evidence.",
|
|
34
|
+
"ground_truth": "The value of {f} differs from the known correct value.",
|
|
35
|
+
"unstable": "The value of {f} differs between runs of the same document.",
|
|
36
|
+
"placeholder": "The field {f} holds a placeholder instead of a value.",
|
|
37
|
+
"label": "The value of {f} was taken from a different label in the document.",
|
|
38
|
+
"plausible": "The value of {f} does not look like the kind of value the field holds.",
|
|
39
|
+
"math": "The line items' arithmetic does not add up at {f}.",
|
|
40
|
+
}
|
|
41
|
+
_NAMED = {
|
|
42
|
+
"exec:llm_error": "A call to the model failed (an error, a timeout or a rate limit).",
|
|
43
|
+
"exec:finish_reason": "The model's output was cut off before it was complete.",
|
|
44
|
+
"exec:json_parse": "The model's output was not valid JSON.",
|
|
45
|
+
"exec:step_error": "A pipeline step raised an error.",
|
|
46
|
+
"exec:missing_step": "A pipeline step that should have run did not.",
|
|
47
|
+
"exec:no_output": "The run produced no output.",
|
|
48
|
+
"exec:output_object": "The output is not the object the schema describes.",
|
|
49
|
+
"exec:empty_input": "The input given to the pipeline was empty.",
|
|
50
|
+
"exec:rejected_by_app": "The application's own validation rejected the output.",
|
|
51
|
+
"exec:tool_error": "A tool the agent called failed.",
|
|
52
|
+
"generic:refusal": "The model refused to answer.",
|
|
53
|
+
"generic:empty_output": "The model returned an empty answer.",
|
|
54
|
+
"generic:prompt_leak": "The model's answer repeats its instructions.",
|
|
55
|
+
"generic:token_repetition": "The model's answer repeats the same text over and over.",
|
|
56
|
+
"generic:control_chars": "The model's answer holds control characters.",
|
|
57
|
+
"human:marked_wrong": "A person marked this output as wrong.",
|
|
58
|
+
"scope:document": "The document is outside what the pipeline is meant to handle.",
|
|
59
|
+
"grounding:source": "The values could not be compared with the document.",
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def check_sentence(check: str | None, field: str | None = None) -> str:
|
|
64
|
+
"""One sentence for a check id; an invariant (a spec rule id) is a business rule."""
|
|
65
|
+
if not check:
|
|
66
|
+
return "A check failed."
|
|
67
|
+
if check in _NAMED:
|
|
68
|
+
return _NAMED[check]
|
|
69
|
+
prefix, sep, rest = check.partition(":")
|
|
70
|
+
if prefix == "check" and rest:
|
|
71
|
+
return f"Your agent reported its check {rest!r} as failed."
|
|
72
|
+
if sep and prefix in _FIELD_CHECKS:
|
|
73
|
+
return _FIELD_CHECKS[prefix].format(f=field or rest or "?")
|
|
74
|
+
if sep:
|
|
75
|
+
return f"The check {check} failed."
|
|
76
|
+
return f"A business rule failed: {check}."
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def tier_name(tier: str | None) -> str:
|
|
80
|
+
return TIERS.get(tier or "", f"{tier} check" if tier else "check")
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
# ------------------------------------------------------------------------------ lifecycle
|
|
84
|
+
|
|
85
|
+
PATH = (
|
|
86
|
+
"DETECTED",
|
|
87
|
+
"DIAGNOSED",
|
|
88
|
+
"FIX_PENDING",
|
|
89
|
+
"VERIFYING",
|
|
90
|
+
"AWAITING_APPROVAL",
|
|
91
|
+
"AWAITING_DEPLOY",
|
|
92
|
+
"MONITORING",
|
|
93
|
+
"RESOLVED",
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
# what moves an issue on from each state (the Loop Controller's gates, in words)
|
|
97
|
+
ADVANCE = {
|
|
98
|
+
"DETECTED": "DIAGNOSED once a root cause is attributed with evidence and a regression case "
|
|
99
|
+
"exists (an uncertain finding first needs a review decision)",
|
|
100
|
+
"DIAGNOSED": "FIX_PENDING when a person proposes a fix",
|
|
101
|
+
"FIX_PENDING": "VERIFYING when `zyonix replay --fix` starts on your machine",
|
|
102
|
+
"VERIFYING": "AWAITING_APPROVAL if the replay passes the gate, back to FIX_PENDING if not",
|
|
103
|
+
"AWAITING_APPROVAL": "AWAITING_DEPLOY when a person approves the fix, FIX_PENDING if it is "
|
|
104
|
+
"rejected (rule and runtime_policy fixes are applied at approval and "
|
|
105
|
+
"go straight on to MONITORING)",
|
|
106
|
+
"AWAITING_DEPLOY": "MONITORING when the deploy is recorded (`zyonix deploy --fix`) or seen "
|
|
107
|
+
"as a release, prompt or config change",
|
|
108
|
+
"MONITORING": "RESOLVED automatically with no recurrence over the run and day thresholds; "
|
|
109
|
+
"REOPENED on any recurrence",
|
|
110
|
+
"REOPENED": "DIAGNOSED again with the new evidence (automatic)",
|
|
111
|
+
"RESOLVED": "stays closed; REOPENED if the failure recurs",
|
|
112
|
+
}
|
|
113
|
+
# who or what the issue is waiting for
|
|
114
|
+
WAITING = {
|
|
115
|
+
"DETECTED": "Zyonix: the diagnosis (a review decision or a regression case may be missing)",
|
|
116
|
+
"DIAGNOSED": "a person with an admin key to propose a fix",
|
|
117
|
+
"FIX_PENDING": "you: verify the fix with a replay on your machine",
|
|
118
|
+
"VERIFYING": "the replay that is running",
|
|
119
|
+
"AWAITING_APPROVAL": "a person with an admin key to approve or reject the fix",
|
|
120
|
+
"AWAITING_DEPLOY": "you: ship the approved fix, then record the deploy",
|
|
121
|
+
"MONITORING": "nobody: Zyonix is watching for recurrences",
|
|
122
|
+
"REOPENED": "Zyonix: a new diagnosis with the evidence of the recurrence",
|
|
123
|
+
"RESOLVED": "nobody: the issue is closed",
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def progression(state: str | None, *, indent: int = 2, width: int | None = None) -> str:
|
|
128
|
+
"""DETECTED > [DIAGNOSED] > FIX_PENDING > ... with the current state in brackets.
|
|
129
|
+
|
|
130
|
+
REOPENED is not a stage on the path (an issue enters it from MONITORING or RESOLVED and
|
|
131
|
+
leaves it for DIAGNOSED), so it is named on a line of its own."""
|
|
132
|
+
tokens = []
|
|
133
|
+
for s in PATH:
|
|
134
|
+
if s == state:
|
|
135
|
+
tokens.append(term.paint(f"[{s}]", "bold"))
|
|
136
|
+
else:
|
|
137
|
+
tokens.append(term.paint(s, "muted"))
|
|
138
|
+
avail = width or min(100, term.width() or 100)
|
|
139
|
+
lines = _fold(tokens, " > ", avail - indent)
|
|
140
|
+
pad = " " * indent
|
|
141
|
+
out = [pad + line for line in lines]
|
|
142
|
+
if state == "REOPENED":
|
|
143
|
+
out.append(
|
|
144
|
+
pad + term.paint("[REOPENED]", "bad") + " the failure recurred after its fix;"
|
|
145
|
+
" it goes back to DIAGNOSED with the new evidence"
|
|
146
|
+
)
|
|
147
|
+
return "\n".join(out)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def _fold(tokens: list[str], sep: str, avail: int) -> list[str]:
|
|
151
|
+
"""Join coloured tokens with ``sep``, breaking lines by visible width."""
|
|
152
|
+
lines: list[str] = []
|
|
153
|
+
line = ""
|
|
154
|
+
for tok in tokens:
|
|
155
|
+
candidate = tok if not line else line + sep + tok
|
|
156
|
+
if line and term.visible_len(candidate) > avail:
|
|
157
|
+
lines.append(line + sep.rstrip())
|
|
158
|
+
line = tok
|
|
159
|
+
else:
|
|
160
|
+
line = candidate
|
|
161
|
+
if line:
|
|
162
|
+
lines.append(line)
|
|
163
|
+
return lines
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
# ------------------------------------------------------------------------------ keys
|
|
167
|
+
|
|
168
|
+
# Which key kind holds which scope: the words of server/.../auth.py SCOPES (the server enforces
|
|
169
|
+
# them on every request; tests/test_cli_role_hints.py keeps this copy equal to it).
|
|
170
|
+
KIND_SCOPES = {
|
|
171
|
+
"sdk": {"ingest", "feedback", "run_exists", "policy"},
|
|
172
|
+
"cli": {"ingest", "feedback", "run_exists", "read", "results", "policy", "preview"},
|
|
173
|
+
"admin": {
|
|
174
|
+
"ingest",
|
|
175
|
+
"feedback",
|
|
176
|
+
"run_exists",
|
|
177
|
+
"read",
|
|
178
|
+
"results",
|
|
179
|
+
"admin",
|
|
180
|
+
"policy",
|
|
181
|
+
"preview",
|
|
182
|
+
"draft",
|
|
183
|
+
},
|
|
184
|
+
"agent": {"read", "preview", "draft"},
|
|
185
|
+
}
|
|
186
|
+
KIND_TEXT = {
|
|
187
|
+
"cli": "cli: reads, runs tests and replays; decisions need an admin key",
|
|
188
|
+
"admin": "admin: reads and makes decisions (approve, review, Guard, spec)",
|
|
189
|
+
"agent": "agent: reads, previews fixes, leaves drafts; cannot approve or deploy",
|
|
190
|
+
"sdk": "sdk: sends traces only; reading needs a cli or admin key",
|
|
191
|
+
}
|
|
192
|
+
# commands a Next line may name, by the scope their request needs (most specific first)
|
|
193
|
+
_NEEDS = (
|
|
194
|
+
("zyonix fix propose", "admin"),
|
|
195
|
+
("zyonix fix approve", "admin"),
|
|
196
|
+
("zyonix fix reject", "admin"),
|
|
197
|
+
("zyonix issues reopen", "admin"),
|
|
198
|
+
("zyonix review confirm", "admin"),
|
|
199
|
+
("zyonix review reject", "admin"),
|
|
200
|
+
("zyonix review correct", "admin"),
|
|
201
|
+
("zyonix spec push", "admin"),
|
|
202
|
+
("zyonix checks add", "admin"),
|
|
203
|
+
("zyonix checks test", "admin"),
|
|
204
|
+
("zyonix checks remove", "admin"),
|
|
205
|
+
("zyonix spec redetect", "admin"),
|
|
206
|
+
("zyonix guard mode", "admin"),
|
|
207
|
+
("zyonix guard kill", "admin"),
|
|
208
|
+
("zyonix guard resume", "admin"),
|
|
209
|
+
("zyonix monitor run", "admin"),
|
|
210
|
+
("zyonix jobs retry", "admin"),
|
|
211
|
+
("zyonix golden add", "admin"),
|
|
212
|
+
("zyonix golden import", "admin"),
|
|
213
|
+
("zyonix golden remove", "admin"),
|
|
214
|
+
("zyonix replay --issue", "preview"),
|
|
215
|
+
("zyonix replay --fix", "results"),
|
|
216
|
+
("zyonix test", "results"),
|
|
217
|
+
("zyonix deploy", "results"),
|
|
218
|
+
("zyonix drafts create", "draft"),
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def needs(command: str) -> str:
|
|
223
|
+
"""The scope a command's request needs (``read`` for every inspection command)."""
|
|
224
|
+
for prefix, scope in _NEEDS:
|
|
225
|
+
if command == prefix or command.startswith(prefix + " "):
|
|
226
|
+
return scope
|
|
227
|
+
return "read"
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def key_note(command: str, kind: str | None) -> str:
|
|
231
|
+
""" "" when the current key may run the command, else which key it needs."""
|
|
232
|
+
scope = needs(command)
|
|
233
|
+
if kind in KIND_SCOPES and scope in KIND_SCOPES[kind]:
|
|
234
|
+
return ""
|
|
235
|
+
if kind is None and scope == "read": # any key that can show this screen can read
|
|
236
|
+
return ""
|
|
237
|
+
return f"needs {keys_with(scope)}" + (f", yours is {kind}" if kind else "")
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def keys_with(scope: str) -> str:
|
|
241
|
+
""" "an admin key", "a cli or admin key", ...: the kinds of key that hold a scope."""
|
|
242
|
+
kinds = [k for k in ("sdk", "cli", "admin", "agent") if scope in KIND_SCOPES[k]]
|
|
243
|
+
if not kinds:
|
|
244
|
+
return f"a key with the {scope} scope"
|
|
245
|
+
names = kinds[0] if len(kinds) == 1 else ", ".join(kinds[:-1]) + " or " + kinds[-1]
|
|
246
|
+
return ("a " if kinds[0] == "cli" else "an ") + names + " key"
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def annotate(steps: list[tuple[str, str]], kind: str | None) -> list[tuple[str, str]]:
|
|
250
|
+
"""Next steps with the key each one needs, when the current key does not hold it."""
|
|
251
|
+
out = []
|
|
252
|
+
for cmd, why in steps:
|
|
253
|
+
note = key_note(cmd, kind)
|
|
254
|
+
out.append((cmd, f"{why}; {note}" if note else why))
|
|
255
|
+
return out
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def key_kind(client: Any) -> str | None:
|
|
259
|
+
"""The kind of the key in use, from the server (``GET /v1/auth/check``); None if unknown."""
|
|
260
|
+
from zyonix_cli.client import CliError
|
|
261
|
+
|
|
262
|
+
try:
|
|
263
|
+
doc = client.get("/v1/auth/check")
|
|
264
|
+
if doc.get("kind") == "user":
|
|
265
|
+
return {"admin": "admin", "member": "cli"}.get(doc.get("role"))
|
|
266
|
+
kind = doc.get("kind")
|
|
267
|
+
except (CliError, AttributeError):
|
|
268
|
+
return None
|
|
269
|
+
return kind if isinstance(kind, str) else None
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
def fold_text(text: str, *, indent: int, hang: int | None = None) -> str:
|
|
273
|
+
"""Wrap one paragraph to the terminal (or 100 columns), with a hanging indent."""
|
|
274
|
+
avail = min(100, term.width() or 100)
|
|
275
|
+
pad = " " * indent
|
|
276
|
+
return textwrap.fill(
|
|
277
|
+
text,
|
|
278
|
+
width=avail,
|
|
279
|
+
initial_indent=pad,
|
|
280
|
+
subsequent_indent=" " * (hang if hang is not None else indent + 2),
|
|
281
|
+
break_on_hyphens=False,
|
|
282
|
+
break_long_words=False,
|
|
283
|
+
)
|
zyonix_cli/fix_cmd.py
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
"""`zyonix fix`: propose, inspect, approve or reject a fix (the human steps of the loop).
|
|
2
|
+
|
|
3
|
+
zyonix fix propose --issue 3 --kind prompt --title "Restore tax" --candidate-file fix.json
|
|
4
|
+
zyonix fix propose --issue 3 --kind runtime_policy --strategy R2 --title "Enable R2"
|
|
5
|
+
zyonix fix propose --issue 3 --kind rule --spec-file new_spec.yaml --title "Tighten total"
|
|
6
|
+
zyonix replay --fix 4 # verify on your machine: sets A/B/C, k-of-N (existing command)
|
|
7
|
+
zyonix fix show 4 # candidate + verification report + next step
|
|
8
|
+
zyonix fix approve 4 [--reason ..] # human approval (admin key); rule/runtime_policy deploy
|
|
9
|
+
zyonix fix reject 4 --reason ".." # send it back (admin key)
|
|
10
|
+
zyonix deploy --fix 4 --release <sha> # record that an approved prompt/config/code fix is live
|
|
11
|
+
zyonix fix promote 7 # look at an agent's draft D-7 and what blocks promotion
|
|
12
|
+
zyonix fix promote 7 --revision 2 # promote exactly what you inspected (human, admin key)
|
|
13
|
+
zyonix fix suggest --issue 3 [--out dir] # fixes Zyonix derives from the evidence, as files
|
|
14
|
+
zyonix fix suggest --run 4be0910c269c # ... for one run's findings (uncertain ones too)
|
|
15
|
+
|
|
16
|
+
Promotion turns a verified draft (`zyonix drafts`) into a *proposed* fix. It approves nothing:
|
|
17
|
+
the fix still goes through `zyonix replay --fix` (the real verification, on your machine) and
|
|
18
|
+
`zyonix fix approve`.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
from __future__ import annotations
|
|
22
|
+
|
|
23
|
+
import argparse
|
|
24
|
+
import re
|
|
25
|
+
import shlex
|
|
26
|
+
from pathlib import Path
|
|
27
|
+
from typing import Any
|
|
28
|
+
|
|
29
|
+
from zyonix_cli import explain, verify_view
|
|
30
|
+
from zyonix_cli.client import ApiClient, CliError
|
|
31
|
+
from zyonix_cli.cmdkit import emit, group, leaf, read_json_arg
|
|
32
|
+
from zyonix_cli.render import badge, heading, jdump, kv, next_steps, wrap
|
|
33
|
+
|
|
34
|
+
KINDS = ("prompt", "config", "rule", "code_note", "runtime_policy")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def register(sub: Any) -> None:
|
|
38
|
+
_, actions = group(sub, "fix", "propose, inspect, approve or reject a fix")
|
|
39
|
+
pp = leaf(actions, "propose", "attach a fix proposal to a DIAGNOSED issue (admin key)",
|
|
40
|
+
cmd_propose, label="fix propose")
|
|
41
|
+
pp.add_argument("--issue", type=int, required=True)
|
|
42
|
+
pp.add_argument("--kind", choices=KINDS, required=True)
|
|
43
|
+
pp.add_argument("--title", required=True)
|
|
44
|
+
pp.add_argument("--description", default="")
|
|
45
|
+
pp.add_argument("--candidate", metavar="JSON", help="the candidate as inline JSON")
|
|
46
|
+
pp.add_argument("--candidate-file", metavar="FILE", help="the candidate as a JSON file")
|
|
47
|
+
pp.add_argument("--strategy", choices=["R1", "R2", "R3", "R4"],
|
|
48
|
+
help="runtime_policy: the strategy to enable")
|
|
49
|
+
pp.add_argument("--spec-file", metavar="FILE", help="rule: the new reliability spec (YAML)")
|
|
50
|
+
pp.add_argument("--note", help="code_note: the change you will make in your code")
|
|
51
|
+
sp = leaf(actions, "show", "a fix with its verification report", cmd_show, label="fix show")
|
|
52
|
+
sp.add_argument("fix", type=int)
|
|
53
|
+
ap = leaf(actions, "approve", "approve a verified fix (human decision, admin key)",
|
|
54
|
+
cmd_approve, label="fix approve")
|
|
55
|
+
ap.add_argument("fix", type=int)
|
|
56
|
+
ap.add_argument("--reason", default="")
|
|
57
|
+
rp = leaf(actions, "reject", "reject a verified fix (admin key)", cmd_reject,
|
|
58
|
+
label="fix reject")
|
|
59
|
+
rp.add_argument("fix", type=int)
|
|
60
|
+
rp.add_argument("--reason", required=True)
|
|
61
|
+
mp = leaf(actions, "promote", "turn an agent's verified draft into a fix proposal (human "
|
|
62
|
+
"decision, admin key); it is not approved by this", cmd_promote,
|
|
63
|
+
label="fix promote")
|
|
64
|
+
mp.add_argument("draft", type=int, help="draft id (D-<n>, see `zyonix drafts`)")
|
|
65
|
+
mp.add_argument("--revision", type=int,
|
|
66
|
+
help="the revision you inspected; without it the draft is only shown")
|
|
67
|
+
mp.add_argument("--replace", action="store_true",
|
|
68
|
+
help="supersede a fix that is already proposed (never one that is being "
|
|
69
|
+
"verified, approved or deployed)")
|
|
70
|
+
mp.add_argument("--reason", default="")
|
|
71
|
+
gp = leaf(actions, "suggest", "fix candidates derived from the evidence of an issue or a run "
|
|
72
|
+
"(read key; nothing is applied)", cmd_suggest, label="fix suggest")
|
|
73
|
+
target = gp.add_mutually_exclusive_group(required=True)
|
|
74
|
+
target.add_argument("--issue", type=int)
|
|
75
|
+
target.add_argument("--run", help="run id (or a unique prefix)")
|
|
76
|
+
gp.add_argument("--out", metavar="DIR", help="write each suggestion as a fix file (for "
|
|
77
|
+
"`zyonix replay --local`) and a candidate file (for `zyonix fix propose`)")
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _candidate(args: argparse.Namespace) -> dict[str, Any]:
|
|
81
|
+
if args.kind == "runtime_policy" and args.strategy:
|
|
82
|
+
return {"strategy": args.strategy}
|
|
83
|
+
if args.kind == "rule" and args.spec_file:
|
|
84
|
+
try:
|
|
85
|
+
return {"spec_yaml": Path(args.spec_file).read_text(encoding="utf-8-sig")}
|
|
86
|
+
except OSError as exc:
|
|
87
|
+
raise CliError(f"cannot read {args.spec_file}: {exc}") from None
|
|
88
|
+
if args.kind == "code_note" and args.note:
|
|
89
|
+
return {"note": args.note}
|
|
90
|
+
return read_json_arg(args.candidate, args.candidate_file, "the fix candidate")
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
_CONTROL = re.compile("[\x00-\x08\x0b-\x1f\x7f-\x9f--]")
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _printable(value: Any) -> str:
|
|
97
|
+
"""Server text that quotes a document, without terminal control or bidi characters."""
|
|
98
|
+
return _CONTROL.sub("", "" if value is None else str(value))
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def cmd_suggest(client: ApiClient, args: argparse.Namespace) -> int:
|
|
102
|
+
path = (f"/v1/issues/{args.issue}/suggestions" if args.issue is not None
|
|
103
|
+
else f"/v1/runs/{args.run}/suggestions")
|
|
104
|
+
out = client.get(path)
|
|
105
|
+
sugg = out["suggestions"]
|
|
106
|
+
name = (f"issue-{args.issue}" if args.issue is not None
|
|
107
|
+
else "run-" + re.sub(r"[^A-Za-z0-9_-]", "_", str(args.run))[:12])
|
|
108
|
+
if getattr(args, "json", False): # --out still writes the files; stdout is the JSON only
|
|
109
|
+
if args.out:
|
|
110
|
+
for n, s in enumerate(sugg, 1):
|
|
111
|
+
_write_suggestion(Path(args.out), name, n, s)
|
|
112
|
+
emit(args, out)
|
|
113
|
+
return 0
|
|
114
|
+
if not sugg:
|
|
115
|
+
print(f"no fix suggestions for {name}: none of its findings names its own fix")
|
|
116
|
+
return 0
|
|
117
|
+
written: list[tuple[str, str]] = []
|
|
118
|
+
for n, s in enumerate(sugg, 1):
|
|
119
|
+
code = s.get("requires_app_code_change", s.get("needs_code_change"))
|
|
120
|
+
how = ("needs a code change in the application (not replay-verifiable)"
|
|
121
|
+
if code else "replay-verifiable")
|
|
122
|
+
print(heading(f"{n}. {_printable(s['title'])}"))
|
|
123
|
+
print(kv([("kind", s["kind"]), ("for", s["check_key"]), ("verification", how),
|
|
124
|
+
("why", _printable(s.get("why")))]))
|
|
125
|
+
print(wrap(f"patch: {_printable(s['patch'])}", indent=2))
|
|
126
|
+
for line in [x for x in s.get("evidence") or [] if x][:5]:
|
|
127
|
+
print(wrap(f"evidence: {_printable(line)}", indent=2))
|
|
128
|
+
if s.get("untrusted"):
|
|
129
|
+
print(wrap(f"note: {_printable(s['untrusted'])}", indent=2))
|
|
130
|
+
if args.out:
|
|
131
|
+
fix_file, cand_file = _write_suggestion(Path(args.out), name, n, s)
|
|
132
|
+
written.append((str(fix_file), str(cand_file)))
|
|
133
|
+
print(f" written: {fix_file}")
|
|
134
|
+
steps: list[tuple[str, str]] = []
|
|
135
|
+
issue = args.issue if args.issue is not None else "<n>"
|
|
136
|
+
if written:
|
|
137
|
+
fix_file, cand_file = written[0]
|
|
138
|
+
first = sugg[0]
|
|
139
|
+
if not first.get("requires_app_code_change", first.get("needs_code_change")):
|
|
140
|
+
steps.append((f"zyonix replay --issue {issue} --candidate-file "
|
|
141
|
+
f"{shlex.quote(fix_file)} --local",
|
|
142
|
+
"preview it on the affected cases (changes nothing)"))
|
|
143
|
+
steps.append((f"zyonix fix propose --issue {issue} --kind {first['kind']} --title "
|
|
144
|
+
f"{shlex.quote(_printable(first['title']))} --candidate-file "
|
|
145
|
+
f"{shlex.quote(cand_file)}", "attach it as a fix proposal (admin key)"))
|
|
146
|
+
else:
|
|
147
|
+
which = (f"--issue {args.issue}" if args.issue is not None
|
|
148
|
+
else f"--run {shlex.quote(str(args.run))}")
|
|
149
|
+
steps.append((f"zyonix fix suggest {which} --out <dir>", "write them as files"))
|
|
150
|
+
print(next_steps(steps))
|
|
151
|
+
return 0
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _write_suggestion(folder: Path, name: str, n: int, s: dict[str, Any]) -> tuple[Path, Path]:
|
|
155
|
+
"""A fix file (for `zyonix replay --local`) and a candidate file (for `zyonix fix propose`)."""
|
|
156
|
+
try:
|
|
157
|
+
folder.mkdir(parents=True, exist_ok=True)
|
|
158
|
+
fix_file = folder / f"zyonix-fix-{name}-{n}.json"
|
|
159
|
+
cand_file = folder / f"zyonix-fix-{name}-{n}.candidate.json"
|
|
160
|
+
fix_file.write_text(jdump({"kind": s["kind"], "title": s["title"],
|
|
161
|
+
"candidate": s["candidate"],
|
|
162
|
+
"description": s.get("why") or ""}) + "\n", encoding="utf-8")
|
|
163
|
+
cand_file.write_text(jdump(s["candidate"]) + "\n", encoding="utf-8")
|
|
164
|
+
except OSError as exc:
|
|
165
|
+
raise CliError(f"cannot write the suggestion files in {folder}: {exc}") from None
|
|
166
|
+
return fix_file, cand_file
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def cmd_propose(client: ApiClient, args: argparse.Namespace) -> int:
|
|
170
|
+
out = client.post("/v1/fixes", {"issue_id": args.issue, "kind": args.kind,
|
|
171
|
+
"title": args.title, "description": args.description,
|
|
172
|
+
"candidate": _candidate(args)})
|
|
173
|
+
if emit(args, out):
|
|
174
|
+
return 0
|
|
175
|
+
print(f"fix #{out['fix_id']} attached to ISS-{out['issue_id']} ({out['status']})")
|
|
176
|
+
if args.kind == "code_note":
|
|
177
|
+
print("a code note is not replay-verifiable in the pilot; make the change, then "
|
|
178
|
+
"`zyonix deploy --fix ...` after approval")
|
|
179
|
+
return 0
|
|
180
|
+
print(next_steps([(f"zyonix replay --fix {out['fix_id']}",
|
|
181
|
+
"verify it on your machine (k-of-N on sets A/B/C)")]))
|
|
182
|
+
return 0
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def cmd_show(client: ApiClient, args: argparse.Namespace) -> int:
|
|
186
|
+
f = client.get(f"/v1/fixes/{args.fix}")
|
|
187
|
+
rep = client.get(f"/v1/replay-runs/{f['replay_run_id']}") if f["replay_run_id"] else None
|
|
188
|
+
if emit(args, {"fix": f, "replay": rep}):
|
|
189
|
+
return 0
|
|
190
|
+
print(f"Fix #{f['id']} {f['kind']} [{badge(f['status'])}] for ISS-{f['issue_id']}")
|
|
191
|
+
print(kv([("title", f["title"]), ("author", f["author"]), ("approved/rejected by",
|
|
192
|
+
f["approved_by"]),
|
|
193
|
+
("deploy marker", f["deploy_marker_id"])]))
|
|
194
|
+
if f["description"]:
|
|
195
|
+
print(wrap(f["description"]))
|
|
196
|
+
print(heading("Candidate"))
|
|
197
|
+
print(f" {jdump(f['candidate'])}")
|
|
198
|
+
print(heading("Verification"))
|
|
199
|
+
if rep and rep["report"].get("sets"):
|
|
200
|
+
r = rep["report"]
|
|
201
|
+
print(f" replay run #{rep['replay_run_id']}, k={rep['k']} (the server decides the gate)")
|
|
202
|
+
verify_view.print_scorecard({**r, "passed": rep["passed"]}, title=None,
|
|
203
|
+
overall=verify_view.gate_word({"passed": rep["passed"]}))
|
|
204
|
+
elif f["status"] == "verifying":
|
|
205
|
+
print(" running: a replay was started and its results have not been uploaded yet")
|
|
206
|
+
elif f["kind"] == "code_note":
|
|
207
|
+
print(" a code note cannot be verified by replay")
|
|
208
|
+
else:
|
|
209
|
+
print(" not verified yet")
|
|
210
|
+
hints: list[tuple[str, str]] = []
|
|
211
|
+
if f["status"] == "proposed" and f["kind"] != "code_note":
|
|
212
|
+
hints.append((f"zyonix replay --fix {f['id']}", "verify it on your machine"))
|
|
213
|
+
elif f["status"] == "verified":
|
|
214
|
+
hints += [(f"zyonix fix approve {f['id']}", "approve (human decision)"),
|
|
215
|
+
(f"zyonix fix reject {f['id']} --reason \"...\"", "or send it back")]
|
|
216
|
+
elif f["status"] == "approved":
|
|
217
|
+
hints.append((f"zyonix deploy --fix {f['id']} --release <git-sha>",
|
|
218
|
+
"record that it is live"))
|
|
219
|
+
hints.append((f"zyonix issues show {f['issue_id']}", "the whole issue"))
|
|
220
|
+
print(next_steps(explain.annotate(hints, explain.key_kind(client))))
|
|
221
|
+
return 0
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def cmd_approve(client: ApiClient, args: argparse.Namespace) -> int:
|
|
225
|
+
out = client.post(f"/v1/fixes/{args.fix}/approve", {"reason": args.reason})
|
|
226
|
+
if emit(args, out):
|
|
227
|
+
return 0
|
|
228
|
+
print(f"fix #{out['fix_id']} approved for ISS-{out['issue_id']} ({out['status']})")
|
|
229
|
+
if out["status"] == "deployed":
|
|
230
|
+
extra = (f"policy v{out['policy_version']}" if "policy_version" in out
|
|
231
|
+
else f"spec v{out['spec_version']}")
|
|
232
|
+
print(f"applied on the server ({extra}); the issue is now MONITORING")
|
|
233
|
+
print(next_steps([("zyonix monitor status", "progress toward auto-resolve")]))
|
|
234
|
+
else:
|
|
235
|
+
print(next_steps([(f"zyonix deploy --fix {out['fix_id']} --release <git-sha>",
|
|
236
|
+
"after you ship it (a release/prompt/config change is also detected)")]))
|
|
237
|
+
return 0
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def cmd_reject(client: ApiClient, args: argparse.Namespace) -> int:
|
|
241
|
+
out = client.post(f"/v1/fixes/{args.fix}/reject", {"reason": args.reason})
|
|
242
|
+
if emit(args, out):
|
|
243
|
+
return 0
|
|
244
|
+
print(f"fix #{out['fix_id']} rejected; ISS-{out['issue_id']} is back to FIX_PENDING")
|
|
245
|
+
print(next_steps([(f"zyonix fix propose --issue {out['issue_id']} --kind ...",
|
|
246
|
+
"attach a different fix")]))
|
|
247
|
+
return 0
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def cmd_promote(client: ApiClient, args: argparse.Namespace) -> int:
|
|
251
|
+
"""Two steps on purpose: look, then promote the revision that was looked at. A draft that
|
|
252
|
+
changed in between is refused by the backend."""
|
|
253
|
+
from zyonix_cli.drafts_cmd import print_draft
|
|
254
|
+
|
|
255
|
+
if args.revision is None:
|
|
256
|
+
d = client.get(f"/v1/fix-drafts/{args.draft}")
|
|
257
|
+
if emit(args, d):
|
|
258
|
+
return 0
|
|
259
|
+
print_draft(d)
|
|
260
|
+
print("\nnothing was promoted")
|
|
261
|
+
if d["status"] == "draft" and d["promotion"]["possible"]:
|
|
262
|
+
print(next_steps([(f"zyonix fix promote {d['id']} --revision {d['revision']}",
|
|
263
|
+
"promote what you see above (it becomes a proposed fix)"),
|
|
264
|
+
(f"zyonix drafts dismiss {d['id']} --reason \"...\"",
|
|
265
|
+
"or close it")]))
|
|
266
|
+
elif d["status"] == "draft":
|
|
267
|
+
only_replace = {b["code"] for b in d["promotion"]["blockers"]} == {"fix_proposed"}
|
|
268
|
+
print(next_steps(
|
|
269
|
+
[(f"zyonix fix promote {d['id']} --revision {d['revision']} --replace",
|
|
270
|
+
"supersede the fix that is already proposed")] if only_replace else
|
|
271
|
+
[(f"zyonix drafts dismiss {d['id']} --reason \"...\"",
|
|
272
|
+
"it cannot be promoted as it is (see Promotion above)")]))
|
|
273
|
+
return 0
|
|
274
|
+
out = client.post(f"/v1/fix-drafts/{args.draft}/promote", {
|
|
275
|
+
"revision": args.revision, "replace_proposed": args.replace, "reason": args.reason})
|
|
276
|
+
if emit(args, out):
|
|
277
|
+
return 0
|
|
278
|
+
print(f"draft D-{out['draft_id']} promoted: fix #{out['fix_id']} is proposed for "
|
|
279
|
+
f"ISS-{out['issue_id']} (not verified, not approved)")
|
|
280
|
+
print(next_steps([(f"zyonix replay --fix {out['fix_id']}",
|
|
281
|
+
"verify it on your machine (the real replay: k-of-N on sets A/B/C)"),
|
|
282
|
+
(f"zyonix fix approve {out['fix_id']}",
|
|
283
|
+
"after it is verified: the human approval")]))
|
|
284
|
+
return 0
|