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.
- chewcw_work_queue-0.3.0.dist-info/METADATA +138 -0
- chewcw_work_queue-0.3.0.dist-info/RECORD +16 -0
- chewcw_work_queue-0.3.0.dist-info/WHEEL +5 -0
- chewcw_work_queue-0.3.0.dist-info/entry_points.txt +2 -0
- chewcw_work_queue-0.3.0.dist-info/licenses/LICENSE +21 -0
- chewcw_work_queue-0.3.0.dist-info/top_level.txt +1 -0
- work_queue/__init__.py +1 -0
- work_queue/__main__.py +5 -0
- work_queue/artefacts.py +192 -0
- work_queue/cli.py +100 -0
- work_queue/commands.py +623 -0
- work_queue/docs/pipeline.md +183 -0
- work_queue/gates.py +128 -0
- work_queue/gh.py +111 -0
- work_queue/labels.py +139 -0
- work_queue/verify.py +242 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
# The pipeline
|
|
2
|
+
|
|
3
|
+
The only place the label vocabulary is described. A skill prints this with `work-queue docs`
|
|
4
|
+
instead of restating it: duplicated label rules are how the old drift started.
|
|
5
|
+
|
|
6
|
+
## The chain
|
|
7
|
+
|
|
8
|
+
```
|
|
9
|
+
intent → scope → spec → plan → build
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Five artefacts, each in a file:
|
|
13
|
+
|
|
14
|
+
| Rung | Artefact | Path |
|
|
15
|
+
|---|---|---|
|
|
16
|
+
| intent | product intent, one copy | `.ai/intent/<slug>.md` |
|
|
17
|
+
| scope | a slice, on the issue | issue body: `## Outcome`, `## Boundary`, `## Depends on` |
|
|
18
|
+
| spec | the change, in full | `.ai/specs/<date>-<topic>-design.md` |
|
|
19
|
+
| plan | the run, split into tasks | `.ai/plans/<date>-<name>/00-index.md` + `SEAL.sha256` |
|
|
20
|
+
| build | code and a PR | the repository |
|
|
21
|
+
|
|
22
|
+
Other artefacts live outside the queue and never move a label:
|
|
23
|
+
|
|
24
|
+
| Artefact | Path | Owner |
|
|
25
|
+
|---|---|---|
|
|
26
|
+
| retro lesson | `.ai/retros/<date>-<slug>.md` | `retrospective` writes, `intent-to-scopes` Phase D reads |
|
|
27
|
+
| un-tracked capture, waiting for a tracker | `.ai/inbox/<date>-<slug>.md` | `capture` writes, `work-queue promote` drains |
|
|
28
|
+
| rejected idea, one file per concept | `.ai/out-of-scope/<concept>.md` | `work-queue close --reason wontfix` writes |
|
|
29
|
+
| shaped idea, no commitment | `.ai/ideas/<name>.md` | `idea-refine` |
|
|
30
|
+
| handoff pack for another team | `docs/plan/<date>-<initiative>/` + `pack.json` + `SEAL.sha256` | `master-plan`, sealed and read-only |
|
|
31
|
+
| standalone spec outside the queue | `SPEC-<module>.md` or project docs folder | `spec-driven-development` |
|
|
32
|
+
| ADR or decision note | `docs/decisions/` or project ADR folder | `documentation-and-adrs` |
|
|
33
|
+
|
|
34
|
+
This file owns the rules. Entry routing lives in each skill description plus Route Instead blocks.
|
|
35
|
+
|
|
36
|
+
## Labels
|
|
37
|
+
|
|
38
|
+
| Label | Meaning | Who acts next |
|
|
39
|
+
|---|---|---|
|
|
40
|
+
| `agent:spec` | Scope settled; write the spec | agent |
|
|
41
|
+
| `agent:plan` | Spec passed the readiness gate; write the plan | agent |
|
|
42
|
+
| `agent:build` | Plan exists and is sealed; implement it | agent |
|
|
43
|
+
| `human:triage` | A human decides where this enters | human |
|
|
44
|
+
| `human:info` | Waiting on the reporter to answer | human |
|
|
45
|
+
| `kind:intent` | Product intent; the epic indexes it | — (type axis) |
|
|
46
|
+
| `kind:bug` | A defect: something is wrong | — (type axis) |
|
|
47
|
+
| `kind:feature` | A wanted change: new behaviour or capability | — (type axis) |
|
|
48
|
+
| `kind:task` | A chore: upkeep with no new behaviour | — (type axis) |
|
|
49
|
+
|
|
50
|
+
Rules:
|
|
51
|
+
|
|
52
|
+
- **Exactly one queue label** on every open work item.
|
|
53
|
+
- **The epic is exempt.** An issue carrying `kind:intent` is a container, not a queue item: it has no queue label, and it never appears in `list --stage`. Its thread is the intent's archive: one frozen comment per revision.
|
|
54
|
+
- **At most one `kind:` label**, independent of the queue axis.
|
|
55
|
+
- **Exactly one kind on queued work.** An issue carrying an `agent:` label carries one of
|
|
56
|
+
`kind:bug`, `kind:feature`, `kind:task`. A `human:triage` or `human:info` issue may carry
|
|
57
|
+
none until a human decides what the work is.
|
|
58
|
+
- **The kind is set once, by `work-queue kind`.** It is not a scratch note: `move` refuses a
|
|
59
|
+
rung without one, so a missing kind stops the item instead of travelling with it.
|
|
60
|
+
- **A missing kind is drift.** `verify` reports it as V12 for any open issue carrying an
|
|
61
|
+
`agent:` label. `reconcile` leaves it alone: deciding what the work is needs a human, and
|
|
62
|
+
a guessed kind is worse than a reported gap.
|
|
63
|
+
- **The inbox keeps the kind.** A capture written with no tracker reachable records `kind:`
|
|
64
|
+
in its frontmatter. `work-queue promote` applies it at creation, so the answer survives the
|
|
65
|
+
wait. A capture without the key still promotes, untyped, at `human:triage`.
|
|
66
|
+
- **No queue label on a closed issue.**
|
|
67
|
+
- `deferred` is not a queue label. It marks a closed issue as "later" rather than
|
|
68
|
+
"never", because GitHub records both as `not planned`. `work-queue icebox` lists them.
|
|
69
|
+
Reaching it never requires `init`, because the close path creates the label on first
|
|
70
|
+
use.
|
|
71
|
+
- No `spec` label: reaching `agent:plan` already proves a spec exists.
|
|
72
|
+
|
|
73
|
+
## Edges
|
|
74
|
+
|
|
75
|
+
| From | To | Owner | The gate |
|
|
76
|
+
|---|---|---|---|
|
|
77
|
+
| ticket | `agent:spec` | `intent-to-scopes`, triage router | body has `## Outcome` + `## Boundary`, or a triage marker; one `kind:` label; no **ready** spec file names the issue yet (a draft may return here) |
|
|
78
|
+
| `agent:spec` | `agent:plan` | `spec-from-issue` (AFK), `grill-to-spec` (human-started) | spec file exists, has `## Problem Statement`, frontmatter `topic` + `status: ready`, `gate:` is not `failed`, and a `<!-- spec: … -->` marker comment is on the issue |
|
|
79
|
+
| `agent:plan` | `agent:build` | `writing-plans` | `--plan` is `<dir>/00-index.md`, the file exists, `SEAL.sha256` verifies, and its `issue:` matches |
|
|
80
|
+
| `agent:build` | closed | `executing-plans` | `close --reason done` |
|
|
81
|
+
| any | `human:triage` | any | a non-empty `--reason` |
|
|
82
|
+
| `human:triage` | `agent:spec`, `agent:plan` | triage router | a `<!-- triage: … -->` marker comment on the issue |
|
|
83
|
+
|
|
84
|
+
`agent:build` is unreachable without a sealed plan. That is the requirement "an implementation plan must exist for each spec", expressed as a check rather than a promise.
|
|
85
|
+
|
|
86
|
+
## Frontmatter contract
|
|
87
|
+
|
|
88
|
+
The label is a cache of the artefacts. `verify` reads these fields, so both skills that write them must keep them:
|
|
89
|
+
|
|
90
|
+
| File | Fields |
|
|
91
|
+
|---|---|
|
|
92
|
+
| `.ai/specs/*.md` | `topic`, `status: ready`, `gate: passed`, `issue: <n>` |
|
|
93
|
+
| `.ai/plans/*/00-index.md` | `issue: <n>` |
|
|
94
|
+
|
|
95
|
+
Missing or wrong frontmatter is not a style issue: `verify` reports V3/V4 and `reconcile` demotes the issue to `human:triage`, naming what it could not find.
|
|
96
|
+
|
|
97
|
+
One pair is drift on its own: `status: ready` under `gate: failed`. A spec file exists there, so the label cache agrees with an artefact, and the artefact says the run was refused. `verify` reports it as V11, `move --to agent:plan` refuses it, and `reconcile` will not promote it. That is the state a failed draft reaches when someone flips the status to get past a gate; the fix is to answer the recorded `gate:` line, not to edit the field.
|
|
98
|
+
|
|
99
|
+
A spec that is not `ready` is a draft wherever it sits. `agent:spec` is the rung that finishes it: `gate_scope` lets the issue back there when the spec naming it is not ready, and refuses only when it is.
|
|
100
|
+
|
|
101
|
+
## One page
|
|
102
|
+
|
|
103
|
+
| Entry | Who acts | The move |
|
|
104
|
+
|---|---|---|
|
|
105
|
+
| Inbound issue you did not create | human | fail-closed Action → `human:triage`; `/skill:triage` → `move --to agent:spec` or `agent:plan` |
|
|
106
|
+
| Inbound request too big for one spec | human | stays `human:triage`, `route=intent`; runs `/skill:intent-to-scopes` |
|
|
107
|
+
| Inbound report too thin to verify | human | `park --to human:info --reason …`; reporter replies → `move --to human:triage` |
|
|
108
|
+
| An idea you had | human | `/skill:intent-to-scopes` → epic `kind:intent` + tickets `agent:spec` |
|
|
109
|
+
| A finding outside the current task | agent | `/skill:capture` → issue parked at `human:triage`, nothing is decided. Without that skill: `gh issue create`, then `kind --issue N --to kind:bug`, then `park --to human:triage`, same shape |
|
|
110
|
+
| Spec rung | agent, or human + agent | `/skill:spec-from-issue` (AFK) or `/skill:grill-to-spec` (interview), then `move --to agent:plan --spec F` |
|
|
111
|
+
| Plan rung | agent | `/skill:writing-plans` → `move --to agent:build --plan F` |
|
|
112
|
+
| Build rung | agent | `/skill:executing-plans` → `close --reason done` |
|
|
113
|
+
| Later, or reconsidered | human | `close --reason deferred` → `icebox` lists it → `reopen --reason …` brings it back to `human:triage` |
|
|
114
|
+
| Any drift | anyone | `verify`, then `reconcile` |
|
|
115
|
+
|
|
116
|
+
## Which skills start themselves
|
|
117
|
+
|
|
118
|
+
A skill is `disable-model-invocation: true` when a **human decision** starts it. It stays model-invocable when it is a step inside a flow another skill already began.
|
|
119
|
+
|
|
120
|
+
| Disabled (human types the command) | Invocable (an agent may reach for it) |
|
|
121
|
+
|---|---|
|
|
122
|
+
| `intent-to-scopes`, `grill-to-spec`, `triage`, `commit`, `finishing-a-development-branch`, `judgment-day`, `master-plan` | `work-queue`, `capture`, `spec-from-issue`, `writing-plans`, `executing-plans`, `systematic-debugging`, `subagent-driven-development`, `test-driven-development`, `verification-before-completion`, `code-review`, `ship`, `retrospective`, `idea-refine`, `spec-driven-development`, `documentation-and-adrs` |
|
|
123
|
+
|
|
124
|
+
The back-half skills (`test-driven-development`, `verification-before-completion`, `code-review`) are steps
|
|
125
|
+
inside build flows: `executing-plans` and `subagent-driven-development` exit through
|
|
126
|
+
verification and review. `capture` is model-invocable because it makes no decision. It
|
|
127
|
+
files the finding and parks it at `human:triage`, where a human still has to route it. `ship` writes PR text and changelog only. It never runs tag, publish, or deploy without an explicit human request in the session. `retrospective` runs once after a merge or after a hard bug fix, called from `executing-plans`, `subagent-driven-development`, or `finishing-a-development-branch`. `master-plan` is a side exit: it takes a requirement and returns a sealed pack in `docs/plan/`. It never writes to `.ai/plans` and never moves a queue label.
|
|
128
|
+
|
|
129
|
+
Side exits that never touch the queue: `master-plan` handoff, `idea-refine` shaping, `spec-driven-development` standalone spec, `documentation-and-adrs` notes..
|
|
130
|
+
|
|
131
|
+
Finding the tool: the queue is the command `work-queue`, resolved from `PATH`. Install it once
|
|
132
|
+
(`uv tool install`, `pipx install`, or a symlink to `bin/work-queue` from a checkout), then call it
|
|
133
|
+
by name. It acts on the repo your working directory belongs to, or set `GH_REPO=owner/name`.
|
|
134
|
+
|
|
135
|
+
Do not hardcode a path to the tool in a skill, and do not vendor a copy: a path breaks when skills
|
|
136
|
+
are copied or the tool moves, and a copy drifts from the single place that writes labels.
|
|
137
|
+
|
|
138
|
+
```bash
|
|
139
|
+
work-queue claim --stage agent:build --issue <n>
|
|
140
|
+
work-queue docs # the labels, the rules, the gates
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
If the command is missing, stop and say how to install it. Do not fall back to `gh issue edit`: a
|
|
144
|
+
label applied by hand is the drift this tool exists to prevent.
|
|
145
|
+
|
|
146
|
+
`grill-to-spec` ships `publish_spec.sh` beside its own SKILL.md; locate it the same way, relative to
|
|
147
|
+
the skill you are running.
|
|
148
|
+
|
|
149
|
+
Consequence: **a disabled skill cannot be called by another skill.** A handoff to one says "ask the human to run `/skill:X`", never "invoke X".
|
|
150
|
+
|
|
151
|
+
## Claiming
|
|
152
|
+
|
|
153
|
+
`claim` assigns the issue, posts a comment whose first line is `claim: <login>` and whose list is
|
|
154
|
+
`work`, `artefact`, `harness`, `session`, `started`, `host`, then re-reads. It parses only the first
|
|
155
|
+
line. If a newer foreign claim appeared, it releases the assignee and exits 1. There is no atomic
|
|
156
|
+
compare-and-set in the GitHub API, so the window is real but tiny, and the loser backs off before
|
|
157
|
+
touching any code.
|
|
158
|
+
|
|
159
|
+
The `- artefact:` bullet names the file a reader should open, and `claim` writes it when the file
|
|
160
|
+
already exists: the plan index at `agent:build`, the spec at every other rung. When the rung's file
|
|
161
|
+
does not exist yet, because the claim is about to write it, `claim` leaves the bullet out and the
|
|
162
|
+
caller fills it in after the write:
|
|
163
|
+
|
|
164
|
+
work-queue record --issue N --artefact PATH
|
|
165
|
+
|
|
166
|
+
`record` finds the newest claim comment by the same login and replaces the one `artefact` line, or
|
|
167
|
+
inserts it under `work`, then reads the comment back. It refuses when the file does not name the
|
|
168
|
+
issue, when the claim was made at another rung, or when no claim comment exists. The label and the
|
|
169
|
+
file frontmatter stay the machine contract; the comment is the human record.
|
|
170
|
+
|
|
171
|
+
`list` hides any issue whose `## Depends on` names still-open work. That is the frontier, not a suggestion.
|
|
172
|
+
|
|
173
|
+
## No tracker
|
|
174
|
+
|
|
175
|
+
Exit 2 from `work-queue` or `publish_spec.sh` means there is no tracker: `gh` is missing, not authenticated, the repo is not on GitHub, or the token cannot write. It is one outcome, not four, and every skill handles it the same way.
|
|
176
|
+
|
|
177
|
+
1. **Write the artefact file.** Set `status: local-only` in its frontmatter.
|
|
178
|
+
2. **Make no tracker write.** No `gh issue create`, no `gh issue edit`, no `work-queue` command, no label by hand.
|
|
179
|
+
3. **Say it in one line and stop.** The rung stays where it is; the file is the whole record.
|
|
180
|
+
|
|
181
|
+
There is no file-based queue, and none should be built. `list`, `claim`, `move`, `verify`, `reconcile`, and `triage` all read or write the tracker, so none of them can stand in. The read-back after every label write is the reason this queue exists, and a file has no read-back.
|
|
182
|
+
|
|
183
|
+
Because the rung cannot move, the next skill never starts: nothing claims the item, so nothing picks it up. A `local-only` spec is not `status: ready`, and `move --to agent:plan` refuses it. That is the design, not a bug to route around.
|
work_queue/gates.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"""The five gates a rung transition has to pass.
|
|
2
|
+
|
|
3
|
+
Each one refuses with the reason, and the reason names the fix, because an agent
|
|
4
|
+
reads the refusal and nothing else.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import re
|
|
8
|
+
import shutil
|
|
9
|
+
import subprocess
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
from . import artefacts, gh, labels
|
|
13
|
+
|
|
14
|
+
SPEC_MARK = "<!-- spec:"
|
|
15
|
+
TRIAGE_MARK = "<!-- triage:"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def gate_kind(n):
|
|
19
|
+
"""Every rung needs a recorded kind: an untyped issue cannot enter the queue."""
|
|
20
|
+
if not labels.kind_in(labels.labels_of(n)):
|
|
21
|
+
gh.die(
|
|
22
|
+
f"#{n} has no kind label. Record what the work is first: "
|
|
23
|
+
f"work-queue kind --issue {n} --to kind:bug"
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _has_heading_text(text, words):
|
|
28
|
+
"""A heading such as `## Outcome`, matched on a case-insensitive line start."""
|
|
29
|
+
return bool(re.search(rf"^#+ *({'|'.join(words)})", text or "", re.I | re.M))
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _has_heading(path, words):
|
|
33
|
+
try:
|
|
34
|
+
return _has_heading_text(Path(path).read_text(), words)
|
|
35
|
+
except OSError:
|
|
36
|
+
return False
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _comment_count(n, prefix):
|
|
40
|
+
value = gh.out(
|
|
41
|
+
[
|
|
42
|
+
"issue",
|
|
43
|
+
"view",
|
|
44
|
+
str(n),
|
|
45
|
+
"--json",
|
|
46
|
+
"comments",
|
|
47
|
+
"--jq",
|
|
48
|
+
'[.comments[].body | select(startswith("%s"))] | length' % prefix,
|
|
49
|
+
]
|
|
50
|
+
)
|
|
51
|
+
try:
|
|
52
|
+
return int(value or 0)
|
|
53
|
+
except ValueError:
|
|
54
|
+
return 0
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def gate_scope(n):
|
|
58
|
+
"""A scope ticket needs material to write a spec from, or a triage marker."""
|
|
59
|
+
body = gh.out(["issue", "view", str(n), "--json", "body", "--jq", ".body"])
|
|
60
|
+
if not re.search(r"^#+ *(Outcome|Boundary)", body, re.I | re.M):
|
|
61
|
+
if _comment_count(n, TRIAGE_MARK) == 0:
|
|
62
|
+
gh.die(
|
|
63
|
+
f"#{n} has neither Outcome/Boundary headings nor a triage marker; "
|
|
64
|
+
"nothing to write a spec from"
|
|
65
|
+
)
|
|
66
|
+
path = artefacts.spec_path_for(n)
|
|
67
|
+
if path:
|
|
68
|
+
status = artefacts.frontmatter_field(path, "status")
|
|
69
|
+
if status == "ready":
|
|
70
|
+
gh.die(f"#{n} already has a ready spec at {path}; move it to agent:plan instead")
|
|
71
|
+
print(f"#{n} returns to agent:spec to finish the draft at {path} (status: {status or 'missing'})")
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def gate_failed(path):
|
|
75
|
+
return artefacts.frontmatter_field(path, "gate").startswith("failed")
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def gate_spec(path, n):
|
|
79
|
+
if not path:
|
|
80
|
+
gh.die("--spec FILE is required to move to agent:plan")
|
|
81
|
+
if not Path(path).is_file():
|
|
82
|
+
gh.die(f"no such spec file: {path}")
|
|
83
|
+
if not _has_heading(path, ["Problem Statement"]):
|
|
84
|
+
gh.die(f"spec has no '## Problem Statement': {path}")
|
|
85
|
+
topic = artefacts.frontmatter_field(path, "topic")
|
|
86
|
+
status = artefacts.frontmatter_field(path, "status")
|
|
87
|
+
if not topic:
|
|
88
|
+
gh.die(f"spec frontmatter has no 'topic': {path}")
|
|
89
|
+
if status != "ready":
|
|
90
|
+
gh.die(f"spec status is '{status or 'missing'}', not 'ready': {path}")
|
|
91
|
+
if gate_failed(path):
|
|
92
|
+
gh.die(
|
|
93
|
+
f"spec records 'gate: {artefacts.frontmatter_field(path, 'gate')}'; answer that line "
|
|
94
|
+
f"and re-run the gate, or keep status: needs-triage and park it: {path}"
|
|
95
|
+
)
|
|
96
|
+
owned = artefacts.frontmatter_field(path, "issue")
|
|
97
|
+
if owned and owned != str(n):
|
|
98
|
+
if owned == "0":
|
|
99
|
+
gh.die(f"spec frontmatter still says 'issue: 0'; write 'issue: {n}', then re-run")
|
|
100
|
+
gh.die(f"spec is owned by issue {owned}, not #{n}")
|
|
101
|
+
if _comment_count(n, SPEC_MARK) == 0:
|
|
102
|
+
gh.die(f"#{n} carries no '{SPEC_MARK}' marker comment; publish the spec first")
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def gate_plan(path, n):
|
|
106
|
+
if not path:
|
|
107
|
+
gh.die("--plan PATH is required to move to agent:build")
|
|
108
|
+
index_name = artefacts.plan_index_name()
|
|
109
|
+
if not path.endswith("/" + index_name):
|
|
110
|
+
gh.die(f"--plan must be <plan dir>/{index_name}, got: {path}")
|
|
111
|
+
if not Path(path).is_file():
|
|
112
|
+
gh.die(f"no such plan file: {path}")
|
|
113
|
+
directory = str(Path(path).parent)
|
|
114
|
+
seal = artefacts.seal_name()
|
|
115
|
+
if not (Path(directory) / seal).is_file():
|
|
116
|
+
gh.die(f"no {seal} in {directory}; seal the plan before it can be built")
|
|
117
|
+
tool = artefacts.seal_tool()
|
|
118
|
+
if not tool:
|
|
119
|
+
gh.die(f"neither sha256sum nor shasum is installed; cannot verify the seal in {directory}")
|
|
120
|
+
if not shutil.which(tool[0]):
|
|
121
|
+
gh.die(f"seal tool '{tool[0]}' is not installed; cannot verify the seal in {directory}")
|
|
122
|
+
if not artefacts.seal_ok(directory):
|
|
123
|
+
gh.die(f"seal does not verify in {directory}; re-seal before it can be built")
|
|
124
|
+
owned = artefacts.frontmatter_field(path, "issue")
|
|
125
|
+
if not owned:
|
|
126
|
+
gh.warn("plan has no 'issue:' frontmatter; verify will not be able to trace it")
|
|
127
|
+
elif owned != str(n):
|
|
128
|
+
gh.die(f"plan is owned by issue {owned}, not #{n}")
|
work_queue/gh.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"""Every call to `gh` goes through this module.
|
|
2
|
+
|
|
3
|
+
The transport is the `gh` CLI, not the REST API: authentication is already solved
|
|
4
|
+
there, and the test suite proves the tool by putting a fake `gh` on PATH.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import json
|
|
8
|
+
import os
|
|
9
|
+
import shutil
|
|
10
|
+
import subprocess
|
|
11
|
+
import sys
|
|
12
|
+
|
|
13
|
+
QUEUE_REPO = os.environ.get("GH_REPO", "")
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def die(msg):
|
|
17
|
+
print(f"error: {msg}", file=sys.stderr)
|
|
18
|
+
raise SystemExit(1)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def envfail(msg):
|
|
22
|
+
print(f"error: {msg}", file=sys.stderr)
|
|
23
|
+
raise SystemExit(2)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def drift(msg):
|
|
27
|
+
print(f"drift: {msg}", file=sys.stderr)
|
|
28
|
+
raise SystemExit(3)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def warn(msg):
|
|
32
|
+
print(f"warn: {msg}", file=sys.stderr)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def run(args):
|
|
36
|
+
"""Run gh and capture it. Never raises on a non-zero exit; the caller decides."""
|
|
37
|
+
return subprocess.run(["gh"] + [str(a) for a in args], capture_output=True, text=True)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def out(args):
|
|
41
|
+
"""Stdout of a successful call, without the trailing newline. Empty on failure."""
|
|
42
|
+
r = run(args)
|
|
43
|
+
if r.returncode != 0:
|
|
44
|
+
return ""
|
|
45
|
+
return r.stdout.rstrip("\n")
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def ghm(args, dry):
|
|
49
|
+
"""The only path a mutating call takes, so --dry-run cannot be forgotten."""
|
|
50
|
+
if dry:
|
|
51
|
+
print("would: gh " + " ".join(str(a) for a in args), file=sys.stderr)
|
|
52
|
+
return None
|
|
53
|
+
return run(args)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def need_gh():
|
|
57
|
+
global QUEUE_REPO
|
|
58
|
+
if shutil.which("gh") is None:
|
|
59
|
+
envfail("gh is not installed. Write the artefacts only.")
|
|
60
|
+
if run(["auth", "status"]).returncode != 0:
|
|
61
|
+
envfail("gh is not authenticated. Write the artefacts only.")
|
|
62
|
+
if not QUEUE_REPO:
|
|
63
|
+
QUEUE_REPO = out(["repo", "view", "--json", "nameWithOwner", "--jq", ".nameWithOwner"])
|
|
64
|
+
if not QUEUE_REPO:
|
|
65
|
+
envfail("no GitHub repo resolved. Write the artefacts only.")
|
|
66
|
+
return QUEUE_REPO
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def json_of(args):
|
|
70
|
+
"""Parse a gh call that returns JSON. An empty list on failure, never a traceback."""
|
|
71
|
+
r = run(args)
|
|
72
|
+
if r.returncode != 0:
|
|
73
|
+
return []
|
|
74
|
+
try:
|
|
75
|
+
return json.loads(r.stdout or "[]")
|
|
76
|
+
except ValueError:
|
|
77
|
+
return []
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def list_limit():
|
|
81
|
+
try:
|
|
82
|
+
return int(os.environ.get("QUEUE_MAX_ISSUES") or 800)
|
|
83
|
+
except ValueError:
|
|
84
|
+
return 800
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def list_json(state="open", labels=None, fields="number"):
|
|
88
|
+
"""Every `gh issue list` goes through here, so the page size is one knob."""
|
|
89
|
+
args = ["issue", "list", "--limit", str(list_limit()), "--state", state, "--json", fields]
|
|
90
|
+
if labels:
|
|
91
|
+
args += ["--label", labels]
|
|
92
|
+
items = json_of(args)
|
|
93
|
+
if len(items) >= list_limit():
|
|
94
|
+
warn(
|
|
95
|
+
f"hit the page limit ({list_limit()}); results are incomplete. "
|
|
96
|
+
"Raise QUEUE_MAX_ISSUES."
|
|
97
|
+
)
|
|
98
|
+
return items
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def detect_harness():
|
|
102
|
+
"""Coding-agent markers, first match wins."""
|
|
103
|
+
if os.environ.get("PI_CODING_AGENT") or os.environ.get("AI_AGENT") == "pi":
|
|
104
|
+
return "pi"
|
|
105
|
+
if os.environ.get("CLAUDECODE"):
|
|
106
|
+
return "claude-code"
|
|
107
|
+
if os.environ.get("OPENCODE"):
|
|
108
|
+
return "opencode"
|
|
109
|
+
if os.environ.get("COPILOT_CLI"):
|
|
110
|
+
return "copilot-cli"
|
|
111
|
+
return "unknown"
|
work_queue/labels.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"""The label vocabulary and every label write.
|
|
2
|
+
|
|
3
|
+
This is the only module that changes a label on an issue, which is what makes the
|
|
4
|
+
read-back checks possible: a write that silently missed is reported, not assumed.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from . import gh
|
|
8
|
+
|
|
9
|
+
import os
|
|
10
|
+
import sys
|
|
11
|
+
|
|
12
|
+
# Name, colour, description. Order matters: `init` creates them in this order.
|
|
13
|
+
VOCAB = [
|
|
14
|
+
("agent:spec", "FBCA04", "Scope settled; an agent writes the spec"),
|
|
15
|
+
("agent:plan", "1D76DB", "Spec passed the readiness gate; an agent writes the plan"),
|
|
16
|
+
("agent:build", "0E8A16", "Plan exists and is sealed; an agent implements it"),
|
|
17
|
+
("human:triage", "D93F0B", "A human decides where this enters the flow"),
|
|
18
|
+
("human:info", "BFD4F2", "Waiting on the reporter to answer"),
|
|
19
|
+
("kind:intent", "5319E7", "Product intent; the epic indexes it"),
|
|
20
|
+
("kind:bug", "D73A4A", "A defect: something is wrong"),
|
|
21
|
+
("kind:feature", "A2EEEF", "A wanted change: new behaviour or capability"),
|
|
22
|
+
("kind:task", "C5DEF5", "A chore: upkeep with no new behaviour"),
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
# Closed-and-later is a state, not a queue position, so it never appears on an open issue.
|
|
26
|
+
STATE_VOCAB = [("deferred", "6E7781", "Not now; closed with a reason")]
|
|
27
|
+
|
|
28
|
+
# The kinds a queue item can carry. kind:intent is excluded: it marks an epic, which is a
|
|
29
|
+
# container and never sits in the queue.
|
|
30
|
+
ITEM_KINDS = ("kind:bug", "kind:feature", "kind:task")
|
|
31
|
+
|
|
32
|
+
# Labels the tool does not own but must not flag as foreign.
|
|
33
|
+
GITHUB_DEFAULTS = (
|
|
34
|
+
"bug",
|
|
35
|
+
"enhancement",
|
|
36
|
+
"documentation",
|
|
37
|
+
"duplicate",
|
|
38
|
+
"invalid",
|
|
39
|
+
"question",
|
|
40
|
+
"wontfix",
|
|
41
|
+
"dependencies",
|
|
42
|
+
"stale",
|
|
43
|
+
"deferred",
|
|
44
|
+
"good first issue",
|
|
45
|
+
"help wanted",
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def vocab_names():
|
|
50
|
+
return [name for name, _color, _desc in VOCAB]
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def is_vocab(name):
|
|
54
|
+
return name in vocab_names()
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def is_queue(name):
|
|
58
|
+
return name.startswith("agent:") or name.startswith("human:")
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def has_label(labels, name):
|
|
62
|
+
"""Whole-label match only: `agent:spec` must never satisfy a test for `agent:`."""
|
|
63
|
+
return f" {name} " in f" {labels} "
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def is_item_kind(name):
|
|
67
|
+
return name in ITEM_KINDS
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def kind_in(labels):
|
|
71
|
+
for name in ITEM_KINDS:
|
|
72
|
+
if has_label(labels, name):
|
|
73
|
+
return name
|
|
74
|
+
return ""
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def is_allowed(name):
|
|
78
|
+
if name in GITHUB_DEFAULTS:
|
|
79
|
+
return True
|
|
80
|
+
extra = (os.environ.get("QUEUE_EXTRA_LABELS", "") or "").split(",")
|
|
81
|
+
return name in [e for e in extra if e]
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def labels_of(n):
|
|
85
|
+
return gh.out(["issue", "view", str(n), "--json", "labels", "--jq", '[.labels[].name] | join(" ")'])
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def ensure_labels(dry):
|
|
89
|
+
for name, color, desc in list(VOCAB) + list(STATE_VOCAB):
|
|
90
|
+
if dry:
|
|
91
|
+
print(f"would: gh label create {name}", file=sys.stderr)
|
|
92
|
+
continue
|
|
93
|
+
r = gh.run(["label", "create", name, "--color", color, "--description", desc, "--force"])
|
|
94
|
+
if r.returncode != 0:
|
|
95
|
+
gh.envfail(f"could not create or update label '{name}'")
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def add_label(n, label, dry):
|
|
99
|
+
gh.ghm(["issue", "edit", str(n), "--add-label", label], dry)
|
|
100
|
+
if dry:
|
|
101
|
+
return
|
|
102
|
+
current = labels_of(n)
|
|
103
|
+
if not has_label(current, label):
|
|
104
|
+
gh.drift(f"gh refused to add '{label}' to #{n}")
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def remove_label(n, label, dry):
|
|
108
|
+
current = labels_of(n)
|
|
109
|
+
if not has_label(current, label):
|
|
110
|
+
return
|
|
111
|
+
gh.ghm(["issue", "edit", str(n), "--remove-label", label], dry)
|
|
112
|
+
if dry:
|
|
113
|
+
return
|
|
114
|
+
if has_label(labels_of(n), label):
|
|
115
|
+
gh.drift(f"'{label}' is still on #{n}")
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def clear_assignees(n, dry):
|
|
119
|
+
"""The assignee is the claim token, so a transition has to release it."""
|
|
120
|
+
logins = gh.out(["issue", "view", str(n), "--json", "assignees", "--jq", ".assignees[].login"])
|
|
121
|
+
for login in logins.split():
|
|
122
|
+
r = gh.ghm(["issue", "edit", str(n), "--remove-assignee", login], dry)
|
|
123
|
+
if r is not None and r.returncode != 0:
|
|
124
|
+
gh.drift(f"could not release #{n} from {login}")
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def apply_queue_label(n, target, dry):
|
|
128
|
+
ensure_labels(dry)
|
|
129
|
+
add_label(n, target, dry)
|
|
130
|
+
if dry:
|
|
131
|
+
print(f"#{n} → {target} (dry run)")
|
|
132
|
+
return
|
|
133
|
+
current = labels_of(n)
|
|
134
|
+
for name in vocab_names():
|
|
135
|
+
if not is_queue(name) or name == target:
|
|
136
|
+
continue
|
|
137
|
+
if has_label(current, name):
|
|
138
|
+
remove_label(n, name, dry)
|
|
139
|
+
print(f"#{n} → {target}")
|