chewcw-work-queue 0.3.0__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.
work_queue/verify.py ADDED
@@ -0,0 +1,242 @@
1
+ """The drift rules and their repairs.
2
+
3
+ `verify` names what is wrong; `reconcile` repairs what can be repaired without a
4
+ human deciding anything. A missing kind is deliberately not repairable: guessing what
5
+ the work is would be worse than reporting the gap.
6
+ """
7
+
8
+ import os
9
+
10
+ from . import artefacts, gates, gh, labels
11
+
12
+
13
+ def _issues():
14
+ return gh.list_json(state="all", fields="number,state,labels")
15
+
16
+
17
+ def _names(issue):
18
+ return [l["name"] for l in issue.get("labels", [])]
19
+
20
+
21
+ def findings():
22
+ """(rule, issue or '-', detail) for every drift, in a stable order."""
23
+ out = []
24
+ repo_labels = gh.out(["label", "list", "--limit", "500", "--json", "name", "--jq", "[.[].name] | .[]"])
25
+ present = set(repo_labels.split("\n")) if repo_labels else set()
26
+ for name in labels.vocab_names():
27
+ if name not in present:
28
+ out.append(("V7", "-", f"label '{name}' is missing from the repo"))
29
+
30
+ open_numbers = [i["number"] for i in gh.list_json(state="open", fields="number")]
31
+ at_plan = [i["number"] for i in gh.list_json(labels="agent:plan", fields="number")]
32
+ at_build = [i["number"] for i in gh.list_json(labels="agent:build", fields="number")]
33
+
34
+ for issue in _issues():
35
+ n = issue["number"]
36
+ state = issue.get("state")
37
+ names = _names(issue)
38
+ label_text = " ".join(names)
39
+ queue_count = sum(1 for x in names if labels.is_queue(x))
40
+ kind_count = sum(1 for x in names if x.startswith("kind:"))
41
+
42
+ if state == "OPEN":
43
+ if labels.has_label(label_text, "kind:intent"):
44
+ if queue_count:
45
+ out.append(("V1", n, f"epic carries a queue label ({label_text})"))
46
+ elif queue_count != 1:
47
+ out.append(("V1", n, f"open with {queue_count} queue labels ({label_text})"))
48
+ else:
49
+ if queue_count:
50
+ out.append(("V2", n, f"closed but still queued ({label_text})"))
51
+
52
+ if kind_count > 1:
53
+ out.append(("V6", n, f"{kind_count} kind labels ({label_text})"))
54
+
55
+ # Queued work needs a kind. A parked item waits for a human to decide what it is,
56
+ # and an epic is a container rather than a queue item.
57
+ if state == "OPEN" and any(x.startswith("agent:") for x in names):
58
+ if not labels.has_label(label_text, "kind:intent") and not labels.kind_in(label_text):
59
+ out.append(("V12", n, "queued with no kind label"))
60
+
61
+ for name in names:
62
+ if labels.is_vocab(name):
63
+ continue
64
+ if not labels.is_allowed(name):
65
+ out.append(("V8", n, f"label '{name}' is outside the vocabulary"))
66
+
67
+ if state == "OPEN" and labels.has_label(label_text, "agent:plan"):
68
+ if not artefacts.spec_path_for(n):
69
+ out.append(("V3", n, f"agent:plan but no spec file names issue {n}"))
70
+ if state == "OPEN" and labels.has_label(label_text, "agent:build"):
71
+ directory = artefacts.plan_dir_for(n)
72
+ if not directory:
73
+ out.append(("V4", n, f"agent:build but no plan directory names issue {n}"))
74
+ elif not artefacts.seal_ok(directory):
75
+ out.append(("V4", n, f"agent:build but the seal does not verify in {directory}"))
76
+ if state == "OPEN" and labels.has_label(label_text, "agent:spec"):
77
+ body = gh.out(["issue", "view", str(n), "--json", "body", "--jq", ".body"])
78
+ if not gates._has_heading_text(body, ["Outcome", "Boundary"]):
79
+ if gates._comment_count(n, gates.TRIAGE_MARK) == 0:
80
+ out.append(("V9", n, "agent:spec but no Outcome/Boundary and no triage marker"))
81
+
82
+ for issue, path, status in artefacts.spec_index():
83
+ if status != "ready":
84
+ continue
85
+ if int(issue) not in open_numbers:
86
+ continue
87
+ if int(issue) in at_plan or int(issue) in at_build:
88
+ continue
89
+ out.append(("V5", issue, f"spec {path} is ready but the issue is at neither agent:plan nor agent:build"))
90
+
91
+ for issue, path, status in artefacts.spec_index():
92
+ if status != "ready":
93
+ continue
94
+ if int(issue) not in open_numbers:
95
+ continue
96
+ if gates.gate_failed(path):
97
+ out.append(
98
+ (
99
+ "V11",
100
+ issue,
101
+ f"spec says status: ready but records 'gate: {artefacts.frontmatter_field(path, 'gate')}': {path}",
102
+ )
103
+ )
104
+
105
+ inbox = artefacts.inbox_dir()
106
+ if os.path.isdir(inbox):
107
+ for name in sorted(os.listdir(inbox)):
108
+ if not name.endswith(".md"):
109
+ continue
110
+ path = os.path.join(inbox, name)
111
+ if artefacts.frontmatter_field(path, "issue"):
112
+ continue
113
+ out.append(("V10", "-", f"captured but not promoted: {path}"))
114
+
115
+ return out
116
+
117
+
118
+ def cmd_verify(args, dry):
119
+ as_json = "--json" in args
120
+ gh.need_gh()
121
+ found = findings()
122
+ finding_count = len(found)
123
+ for rule, issue, detail in found:
124
+ if as_json:
125
+ print(
126
+ '{{"rule":"{}","issue":"{}","detail":"{}"}}'.format(rule, issue, detail)
127
+ )
128
+ else:
129
+ print(f"{rule} #{issue} {detail}")
130
+ if not finding_count:
131
+ if not as_json:
132
+ print("clean: no drift")
133
+ return 0
134
+ if not as_json:
135
+ print(f"\n{finding_count} finding(s). Run: work-queue reconcile")
136
+ return 3
137
+
138
+
139
+ def cmd_reconcile(args, dry):
140
+ gh.need_gh()
141
+ labels.ensure_labels(dry)
142
+
143
+ for issue in _issues():
144
+ n = issue["number"]
145
+ state = issue.get("state")
146
+ names = _names(issue)
147
+ label_text = " ".join(names)
148
+ queue_count = sum(1 for x in names if labels.is_queue(x))
149
+ kind_count = sum(1 for x in names if x.startswith("kind:"))
150
+
151
+ if state != "OPEN":
152
+ if queue_count:
153
+ for name in names:
154
+ if labels.is_queue(name):
155
+ labels.remove_label(n, name, dry)
156
+ print(f"#{n} closed: queue label removed")
157
+ continue
158
+
159
+ if labels.has_label(label_text, "kind:intent"):
160
+ if queue_count:
161
+ for name in names:
162
+ if labels.is_queue(name):
163
+ labels.remove_label(n, name, dry)
164
+ print(f"#{n} epic: queue label removed")
165
+ elif queue_count == 0:
166
+ gh.ghm(
167
+ ["issue", "comment", str(n), "--body", "Reconciled: no queue label found. Routed to human:triage."],
168
+ dry,
169
+ )
170
+ labels.apply_queue_label(n, "human:triage", dry)
171
+ elif queue_count > 1:
172
+ gh.ghm(
173
+ [
174
+ "issue",
175
+ "comment",
176
+ str(n),
177
+ "--body",
178
+ f"Reconciled: {queue_count} queue labels ({label_text}). A human decides which one is true.",
179
+ ],
180
+ dry,
181
+ )
182
+ labels.apply_queue_label(n, "human:triage", dry)
183
+ elif labels.has_label(label_text, "agent:plan") and not artefacts.spec_path_for(n):
184
+ gh.ghm(
185
+ ["issue", "comment", str(n), "--body", f"Reconciled: agent:plan but no spec file names issue {n}."],
186
+ dry,
187
+ )
188
+ labels.apply_queue_label(n, "human:triage", dry)
189
+ elif labels.has_label(label_text, "agent:spec"):
190
+ body = gh.out(["issue", "view", str(n), "--json", "body", "--jq", ".body"])
191
+ if not gates._has_heading_text(body, ["Outcome", "Boundary"]):
192
+ if gates._comment_count(n, gates.TRIAGE_MARK) == 0:
193
+ gh.ghm(
194
+ [
195
+ "issue",
196
+ "comment",
197
+ str(n),
198
+ "--body",
199
+ "Reconciled: agent:spec but no Outcome/Boundary section and no triage marker.",
200
+ ],
201
+ dry,
202
+ )
203
+ labels.apply_queue_label(n, "human:triage", dry)
204
+ elif labels.has_label(label_text, "agent:build"):
205
+ directory = artefacts.plan_dir_for(n)
206
+ if not directory or not artefacts.seal_ok(directory):
207
+ gh.ghm(
208
+ [
209
+ "issue",
210
+ "comment",
211
+ str(n),
212
+ "--body",
213
+ f"Reconciled: agent:build but no verified plan for issue {n}.",
214
+ ],
215
+ dry,
216
+ )
217
+ labels.apply_queue_label(n, "human:triage", dry)
218
+
219
+ if kind_count > 1:
220
+ for name in names:
221
+ if name.startswith("kind:") and name != "kind:intent":
222
+ labels.remove_label(n, name, dry)
223
+ print(f"#{n}: extra kind label removed")
224
+
225
+ open_numbers = {i["number"] for i in gh.list_json(state="open", fields="number")}
226
+ at_plan = {i["number"] for i in gh.list_json(labels="agent:plan", fields="number")}
227
+ at_build = {i["number"] for i in gh.list_json(labels="agent:build", fields="number")}
228
+ for issue, path, status in artefacts.spec_index():
229
+ if status != "ready":
230
+ continue
231
+ if int(issue) not in open_numbers:
232
+ continue
233
+ if int(issue) in at_plan or int(issue) in at_build:
234
+ continue
235
+ if gates.gate_failed(path):
236
+ print(f"#{issue}: spec records a failed gate, leaving it where it is ({path})")
237
+ continue
238
+ print(f"#{issue}: spec is ready, moving it to agent:plan")
239
+ labels.apply_queue_label(issue, "agent:plan", dry)
240
+
241
+ print("reconcile done")
242
+ return 0