@bbcsoftwarefactory/skills 0.1.1 → 1.1.2

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.
Files changed (71) hide show
  1. package/dist/index.json +2 -3
  2. package/dist/skills/code-quality-gate/SKILL.md +4 -5
  3. package/dist/skills/code-quality-gate/references/gate-bigo.md +1 -1
  4. package/dist/skills/delivery-coordinator/SKILL.md +7 -8
  5. package/dist/skills/delivery-coordinator/references/hud-output.md +1 -1
  6. package/dist/skills/delivery-coordinator/references/issue-claim.md +5 -5
  7. package/dist/skills/delivery-coordinator/references/issue-dedup.md +1 -1
  8. package/dist/skills/delivery-coordinator/references/worktree-isolation.md +2 -2
  9. package/dist/skills/greptile-review-loop/SKILL.md +3 -4
  10. package/dist/skills/greptile-review-loop/references/review-protocol.md +1 -1
  11. package/dist/skills/issue-reviewer/SKILL.md +10 -13
  12. package/dist/skills/issue-reviewer/references/review-format.md +1 -1
  13. package/dist/skills/issue-reviewer/references/triage.md +6 -6
  14. package/dist/skills/question-gate/SKILL.md +6 -8
  15. package/dist/skills/question-gate/references/client-hooks.md +10 -10
  16. package/dist/skills/question-gate/references/diagram-routing.md +3 -3
  17. package/dist/skills/skill-clean-architecture/SKILL.md +2 -3
  18. package/dist/skills/skill-clean-architecture/references/source-headers.md +2 -3
  19. package/dist/skills/tool-depot/references/readonly-access.md +1 -1
  20. package/dist/skills/tool-depot/references/tool-catalog.json +2 -2
  21. package/dist/skills/watcher-observability/SKILL.md +3 -4
  22. package/dist/skills/watcher-observability/references/batch-protocol.md +6 -6
  23. package/dist/skills/watcher-observability/references/configuration.md +1 -1
  24. package/dist/skills/watcher-observability/references/error-scan.md +1 -1
  25. package/dist/skills/watcher-observability/references/live-protocol.md +8 -8
  26. package/dist/skills/watcher-observability/references/server-account.md +8 -8
  27. package/dist/skills/watcher-observability/server/watcher-gate.sh +1 -1
  28. package/dist/skills/watcher-observability/tests/watcher-gate.test.sh +10 -7
  29. package/dist/skills/workflow-auditor/SKILL.md +3 -5
  30. package/dist/skills/workflow-auditor/references/audit-protocol.md +3 -3
  31. package/dist/skills/workflow-auditor/references/ledger.md +1 -1
  32. package/package.json +26 -27
  33. package/dist/skills/code-quality-gate/scripts/extract-diff-scope.py +0 -225
  34. package/dist/skills/code-quality-gate/tests/extract-diff-scope.test.sh +0 -200
  35. package/dist/skills/delivery-coordinator/scripts/check-issue-claim.py +0 -363
  36. package/dist/skills/delivery-coordinator/scripts/check-worktree-guard.py +0 -220
  37. package/dist/skills/delivery-coordinator/scripts/classify-change-lane.py +0 -263
  38. package/dist/skills/delivery-coordinator/tests/check-issue-claim.test.sh +0 -221
  39. package/dist/skills/delivery-coordinator/tests/check-worktree-guard.test.sh +0 -123
  40. package/dist/skills/delivery-coordinator/tests/classify-change-lane.test.sh +0 -124
  41. package/dist/skills/greptile-review-loop/scripts/review-loop.py +0 -435
  42. package/dist/skills/greptile-review-loop/tests/review-loop.test.sh +0 -331
  43. package/dist/skills/issue-reviewer/scripts/fetch-issue-attachments.py +0 -239
  44. package/dist/skills/issue-reviewer/scripts/plan-issue-triage.py +0 -311
  45. package/dist/skills/issue-reviewer/tests/fetch-issue-attachments.test.sh +0 -90
  46. package/dist/skills/issue-reviewer/tests/plan-issue-triage.test.sh +0 -168
  47. package/dist/skills/question-gate/scripts/triage-question.py +0 -713
  48. package/dist/skills/question-gate/tests/question-corpus.test.sh +0 -32
  49. package/dist/skills/question-gate/tests/triage-question.test.sh +0 -441
  50. package/dist/skills/skill-clean-architecture/scripts/measure-bigo.py +0 -427
  51. package/dist/skills/skill-clean-architecture/tests/measure-bigo.test.sh +0 -222
  52. package/dist/skills/watcher-observability/scripts/error-scan.py +0 -318
  53. package/dist/skills/watcher-observability/scripts/resolve-containers.py +0 -155
  54. package/dist/skills/watcher-observability/scripts/save-counts.py +0 -381
  55. package/dist/skills/watcher-observability/scripts/summarize-logs.py +0 -113
  56. package/dist/skills/watcher-observability/scripts/watch-live.py +0 -606
  57. package/dist/skills/watcher-observability/tests/error-scan.test.sh +0 -257
  58. package/dist/skills/watcher-observability/tests/live-windows.test.sh +0 -177
  59. package/dist/skills/watcher-observability/tests/resolve-containers.test.sh +0 -87
  60. package/dist/skills/watcher-observability/tests/save-counts.test.sh +0 -338
  61. package/dist/skills/watcher-observability/tests/sleuth.test.sh +0 -176
  62. package/dist/skills/watcher-observability/tests/summarize-logs.test.sh +0 -105
  63. package/dist/skills/watcher-observability/tests/watcher-config.test.sh +0 -70
  64. package/dist/skills/workflow-auditor/scripts/audit-ledger.py +0 -624
  65. package/dist/skills/workflow-auditor/scripts/list-zed-commands.py +0 -183
  66. package/dist/skills/workflow-auditor/scripts/render-workflow.py +0 -201
  67. package/dist/skills/workflow-auditor/scripts/validate-skills.py +0 -444
  68. package/dist/skills/workflow-auditor/tests/audit-ledger.test.sh +0 -660
  69. package/dist/skills/workflow-auditor/tests/list-zed-commands.test.sh +0 -93
  70. package/dist/skills/workflow-auditor/tests/render-workflow.test.sh +0 -135
  71. package/dist/skills/workflow-auditor/tests/validate-skills.test.sh +0 -190
@@ -1,660 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -eu
3
- cd -- "${BASH_SOURCE[0]%/*}/.."
4
- PYTHON_BIN=$(command -v python3 || command -v python)
5
- "$PYTHON_BIN" -I -B - <<'PY'
6
- import contextlib
7
- import copy
8
- import io
9
- import json
10
- import os
11
- import shutil
12
- from pathlib import Path
13
- import runpy
14
- import subprocess
15
- import sys
16
- import tempfile
17
-
18
- entry = Path('scripts/audit-ledger.py').resolve()
19
- if not entry.is_file():
20
- sys.exit('Audit entry point is missing')
21
- main = runpy.run_path(str(entry))['main']
22
- catalog = Path('../tool-depot/references/tool-catalog.json').resolve()
23
- if not catalog.is_file():
24
- sys.exit('Tool Depot catalog is missing')
25
- scratch = Path(tempfile.mkdtemp())
26
- checks = 0
27
- failures = []
28
-
29
- HEAD = 'a1b2c3d4e5f6a7b8c9d0a1b2c3d4e5f6a7b8c9d0'
30
- BASE = '0da5c62aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
31
- CONFORMING = {
32
- 'version': 1,
33
- 'task': 'C1: status filter shows only failed jobs',
34
- 'kind': 'change',
35
- 'worktree': {'path': '/work/example-status-filter', 'branch': 'feat/status-filter', 'base_sha': BASE},
36
- 'authorizations': [
37
- {'id': 'AUTH-1', 'by': 'person', 'role': 'Tech Lead', 'covers': ['implement', 'commit_pr']},
38
- ],
39
- 'stages': [
40
- {'stage': 'specify', 'status': 'reused', 'actor': 'requirements-spec', 'reason': 'Issue example#12 lists C1.'},
41
- {'stage': 'reproduce', 'status': 'done', 'actor': 'regression-testing',
42
- 'evidence': {'command': 'npm test -- status-filter', 'result': 'fail'}},
43
- {'stage': 'implement', 'status': 'done', 'actor': 'skill-clean-architecture', 'authorization': 'AUTH-1'},
44
- {'stage': 'verify', 'status': 'done', 'actor': 'regression-testing',
45
- 'evidence': {'command': 'npm test -- status-filter', 'result': 'pass'}},
46
- {'stage': 'quality', 'status': 'done', 'actor': 'code-quality-gate',
47
- 'evidence': {'typesafe': 'pass', 'earlyreturn': 'pass', 'bigo': 'pass'}},
48
- {'stage': 'commit_pr', 'status': 'done', 'actor': 'host', 'authorization': 'AUTH-1',
49
- 'evidence': {'pr': 'example/repo#13', 'head_sha': HEAD}},
50
- {'stage': 'review', 'status': 'done', 'actor': 'greptile-review-loop',
51
- 'evidence': {'head_sha': HEAD, 'score': '5/5'}},
52
- ],
53
- 'tool_requests': [
54
- {'id': 'TD-1', 'requester': 'delivery-coordinator', 'tool': 'github-read',
55
- 'purpose': 'read-approved-issue-or-pr', 'target': 'example/repo#12', 'operation': 'read issue',
56
- 'disposition': 'eligible'},
57
- {'id': 'TD-2', 'requester': 'greptile-review-loop', 'tool': 'github-write',
58
- 'purpose': 'trigger-approved-greptile-review', 'target': 'example/repo#13', 'operation': 'request review',
59
- 'disposition': 'eligible', 'authorization': 'AUTH-1'},
60
- ],
61
- 'tool_uses': [
62
- {'request': 'TD-1', 'tool': 'github-read', 'target': 'example/repo#12'},
63
- {'request': 'TD-2', 'tool': 'github-write', 'target': 'example/repo#13'},
64
- ],
65
- }
66
-
67
-
68
- def run(ledger, *extra):
69
- path = scratch / 'ledger.json'
70
- path.write_text(ledger if isinstance(ledger, str) else json.dumps(ledger), encoding='utf-8')
71
- out = io.StringIO()
72
- with contextlib.redirect_stdout(out):
73
- status = main([str(path), '--catalog', str(catalog), *extra])
74
- return status, json.loads(out.getvalue())
75
-
76
-
77
- def rules(report, key='violations'):
78
- return sorted({finding['rule'] for finding in report.get(key, [])})
79
-
80
-
81
- def expect(label, ledger, status, verdict, violations=(), gaps=()):
82
- global checks
83
- checks += 1
84
- observed_status, report = run(ledger)
85
- observed = (observed_status, report['verdict'], rules(report), rules(report, 'gaps'))
86
- wanted = (status, verdict, sorted(violations), sorted(gaps))
87
- if observed != wanted:
88
- failures.append(label + ': expected ' + repr(wanted) + ', observed ' + repr(observed))
89
-
90
-
91
- def mutate(change):
92
- ledger = copy.deepcopy(CONFORMING)
93
- change(ledger)
94
- return ledger
95
-
96
-
97
- def stage(ledger, name):
98
- return [item for item in ledger['stages'] if item['stage'] == name][-1]
99
-
100
-
101
- expect('conforming change', CONFORMING, 0, 'conforms')
102
-
103
- expect('greptile unavailable is a gap, not a violation', mutate(lambda l: stage(l, 'review').update(
104
- {'status': 'gap', 'reason': 'Greptile is not installed on the organization.', 'evidence': {}})),
105
- 0, 'gaps', gaps=['G1'])
106
-
107
- expect('worktree must be a feat branch', mutate(lambda l: l['worktree'].update({'branch': 'main'})),
108
- 1, 'violations', ['W1'])
109
- expect('worktree is required', mutate(lambda l: l.pop('worktree')), 1, 'violations', ['W1'])
110
-
111
- expect('missing quality gate', mutate(lambda l: l['stages'].pop(4)), 1, 'violations', ['S1', 'S4'])
112
-
113
- def code_before_test(ledger):
114
- stages = ledger['stages']
115
- stages[1], stages[2] = stages[2], stages[1]
116
- expect('implementation before RED', mutate(code_before_test), 1, 'violations', ['S3'])
117
-
118
- def fix_without_rerun(ledger):
119
- ledger['stages'].append({'stage': 'implement', 'status': 'done', 'actor': 'skill-clean-architecture',
120
- 'authorization': 'AUTH-1'})
121
- expect('review fix not rerun', mutate(fix_without_rerun), 1, 'violations', ['S4'])
122
-
123
- def fix_loop(ledger):
124
- tail = copy.deepcopy(ledger['stages'][2:])
125
- new_head = 'f' * 40
126
- tail[3]['evidence']['head_sha'] = new_head
127
- tail[4]['evidence']['head_sha'] = new_head
128
- ledger['stages'].extend(tail)
129
- expect('review findings loop back to implementation', mutate(fix_loop), 0, 'conforms')
130
-
131
- def backwards(ledger):
132
- ledger['stages'].append({'stage': 'specify', 'status': 'done', 'actor': 'requirements-spec'})
133
- expect('loop back to specify', mutate(backwards), 1, 'violations', ['S2'])
134
-
135
- expect('RED without a failing result', mutate(lambda l: stage(l, 'reproduce')['evidence'].update({'result': 'pass'})),
136
- 1, 'violations', ['E1'])
137
-
138
- def unreproduced(ledger):
139
- stage(ledger, 'reproduce').update({'status': 'gap', 'gap_type': 'not-reproducible',
140
- 'reason': 'No safe reproduction.', 'evidence': {}})
141
- expect('implemented an unreproduced bug', mutate(unreproduced), 1, 'violations', ['E1'], ['G1'])
142
-
143
- def no_runnable_test(ledger):
144
- stage(ledger, 'reproduce').update({'status': 'gap', 'gap_type': 'no-runnable-test',
145
- 'reason': 'Copy-only change.', 'evidence': {}})
146
- stage(ledger, 'verify').update({'status': 'gap', 'reason': 'Checked proportionately.', 'evidence': {}})
147
- expect('documented no-runnable-test gap', mutate(no_runnable_test), 0, 'gaps', gaps=['G1'])
148
-
149
- expect('failing verification', mutate(lambda l: stage(l, 'verify')['evidence'].update({'result': 'fail'})),
150
- 1, 'violations', ['E2'])
151
- expect('blocked quality gate', mutate(lambda l: stage(l, 'quality')['evidence'].update({'bigo': 'block'})),
152
- 1, 'violations', ['Q1'])
153
- expect('quality gate cannot be a gap', mutate(lambda l: stage(l, 'quality').update(
154
- {'status': 'gap', 'reason': 'Skipped.'})), 1, 'violations', ['Q1', 'S5'])
155
-
156
- expect('stale Greptile head', mutate(lambda l: stage(l, 'review')['evidence'].update({'head_sha': 'b' * 40})),
157
- 1, 'violations', ['R1'])
158
-
159
- def with_claim(ledger, check='free', verify='claimed-by-you', write=True, issue='example/repo#12'):
160
- ledger['worktree']['branch'] = 'feat/12-status-filter'
161
- evidence = {'issue': issue, 'claimant': 'ana', 'check': check, 'verify': verify}
162
- ledger['stages'].insert(1, {'stage': 'claim', 'status': 'done', 'actor': 'delivery-coordinator',
163
- 'authorization': 'AUTH-1', 'evidence': evidence})
164
- ledger['tool_requests'].append({
165
- 'id': 'TD-CL1', 'requester': 'delivery-coordinator', 'tool': 'github-read', 'purpose': 'check-issue-claim',
166
- 'target': issue, 'operation': 'read assignees, labels, open PRs and feat/12-* branches',
167
- 'disposition': 'eligible'})
168
- ledger['tool_uses'].append({'request': 'TD-CL1', 'tool': 'github-read', 'target': issue})
169
- if not write:
170
- return
171
- ledger['tool_requests'].append({
172
- 'id': 'TD-CL2', 'requester': 'delivery-coordinator', 'tool': 'github-write', 'purpose': 'claim-approved-issue',
173
- 'target': issue, 'operation': 'assign ana and add status:in-progress', 'disposition': 'eligible',
174
- 'authorization': 'AUTH-1'})
175
- ledger['tool_uses'].append({'request': 'TD-CL2', 'tool': 'github-write', 'target': issue})
176
-
177
-
178
- def linked_issue_conforming(ledger):
179
- with_claim(ledger)
180
- stage(ledger, 'commit_pr')['evidence'].update({'issue': 'example/repo#12', 'issue_linked': True})
181
- ledger['tool_requests'].append({
182
- 'id': 'TD-3', 'requester': 'delivery-coordinator', 'tool': 'github-write',
183
- 'purpose': 'comment-approved-issue', 'target': 'example/repo#12', 'operation': 'link PR on issue',
184
- 'disposition': 'eligible', 'authorization': 'AUTH-1',
185
- })
186
- ledger['tool_uses'].append({'request': 'TD-3', 'tool': 'github-write', 'target': 'example/repo#12',
187
- 'visual': 'https://gist.githubusercontent.com/example/abc/raw/def/change.png'})
188
- expect('PR linked back onto canonical issue', mutate(linked_issue_conforming), 0, 'conforms')
189
-
190
-
191
- def project_board_move_conforming(ledger):
192
- linked_issue_conforming(ledger)
193
- ledger['tool_requests'].extend([
194
- {'id': 'TD-PROJ-R', 'requester': 'delivery-coordinator', 'tool': 'github-read',
195
- 'purpose': 'read-approved-project-board', 'target': 'example/repo#12',
196
- 'operation': 'read project board status options', 'disposition': 'eligible'},
197
- {'id': 'TD-PROJ-W', 'requester': 'delivery-coordinator', 'tool': 'github-write',
198
- 'purpose': 'update-approved-project-status', 'target': 'example/repo#12',
199
- 'operation': 'move issue to Trabajando', 'disposition': 'eligible', 'authorization': 'AUTH-1'},
200
- ])
201
- ledger['tool_uses'].extend([
202
- {'request': 'TD-PROJ-R', 'tool': 'github-read', 'target': 'example/repo#12'},
203
- {'request': 'TD-PROJ-W', 'tool': 'github-write', 'target': 'example/repo#12'},
204
- ])
205
- expect('GitHub Project board move to active status conforms', mutate(project_board_move_conforming), 0, 'conforms')
206
-
207
-
208
- def project_board_move_without_auth(ledger):
209
- project_board_move_conforming(ledger)
210
- ledger['tool_requests'][-1].pop('authorization')
211
- expect('GitHub Project board move without authorization fails T5',
212
- mutate(project_board_move_without_auth), 1, 'violations', ['T5'])
213
-
214
- def issue_comment_without_visual(ledger):
215
- linked_issue_conforming(ledger)
216
- ledger['tool_uses'][-1].pop('visual')
217
- expect('issue comment without a visual fails V1', mutate(issue_comment_without_visual), 1, 'violations', ['V1'])
218
-
219
- def issue_comment_with_plain_visual(ledger):
220
- linked_issue_conforming(ledger)
221
- ledger['tool_uses'][-1]['visual'] = 'screenshot attached'
222
- expect('visual must be an https URL', mutate(issue_comment_with_plain_visual), 1, 'violations', ['V1'])
223
-
224
- def issue_comment_with_visual_gap(ledger):
225
- linked_issue_conforming(ledger)
226
- ledger['tool_uses'][-1]['visual'] = {'gap': 'Backend-only change; redaction left nothing useful.'}
227
- expect('stated visual gap is a gap', mutate(issue_comment_with_visual_gap), 0, 'gaps', gaps=['G2'])
228
-
229
-
230
- def missing_issue_link_flag(ledger):
231
- with_claim(ledger)
232
- stage(ledger, 'commit_pr')['evidence'].update({'issue': 'example/repo#12'})
233
- expect('issue PR without issue_linked fails R2', mutate(missing_issue_link_flag), 1, 'violations', ['R2'])
234
-
235
- def missing_issue_link_tool_use(ledger):
236
- with_claim(ledger)
237
- stage(ledger, 'commit_pr')['evidence'].update({'issue': 'example/repo#12', 'issue_linked': True})
238
- expect('issue PR without comment-approved-issue tool use fails R2', mutate(missing_issue_link_tool_use),
239
- 1, 'violations', ['R2'])
240
-
241
- # Issue claim (C1-C4): one person per canonical issue, see delivery-coordinator/references/issue-claim.md
242
- def unclaimed_issue(ledger):
243
- linked_issue_conforming(ledger)
244
- ledger['stages'] = [item for item in ledger['stages'] if item['stage'] != 'claim']
245
- expect('issue delivery without a claim fails C1', mutate(unclaimed_issue), 1, 'violations', ['C1'])
246
-
247
- def declared_issue_unclaimed(ledger):
248
- ledger['issue'] = 'example/repo#12'
249
- expect('a declared canonical issue needs a claim before the PR exists', mutate(declared_issue_unclaimed),
250
- 1, 'violations', ['C1'])
251
-
252
- def claim_gap(ledger):
253
- linked_issue_conforming(ledger)
254
- stage(ledger, 'claim').update({'status': 'gap', 'reason': 'No triage permission on the repository.',
255
- 'evidence': {}})
256
- expect('a stated claim gap travels to the Code Owner', mutate(claim_gap), 0, 'gaps', gaps=['G1'])
257
-
258
- def claimed_by_other(ledger):
259
- linked_issue_conforming(ledger)
260
- stage(ledger, 'claim')['evidence']['check'] = 'claimed-by-other'
261
- expect('working on an issue someone else holds fails C2', mutate(claimed_by_other), 1, 'violations', ['C2'])
262
-
263
- def handed_over(ledger):
264
- claimed_by_other(ledger)
265
- ledger['authorizations'].append({'id': 'AUTH-H', 'by': 'person', 'role': 'Tech Lead', 'covers': ['claim']})
266
- stage(ledger, 'claim')['evidence']['handover'] = 'AUTH-H'
267
- expect('a handover approved by a person conforms', mutate(handed_over), 0, 'conforms')
268
-
269
- def handover_without_cover(ledger):
270
- claimed_by_other(ledger)
271
- stage(ledger, 'claim')['evidence']['handover'] = 'AUTH-1'
272
- expect('a handover authorization must cover claim', mutate(handover_without_cover), 1, 'violations', ['C2'])
273
-
274
- def lost_race(ledger):
275
- linked_issue_conforming(ledger)
276
- stage(ledger, 'claim')['evidence']['verify'] = 'contended-lost'
277
- expect('a lost claim race fails C2', mutate(lost_race), 1, 'violations', ['C2'])
278
-
279
- def claim_other_issue(ledger):
280
- linked_issue_conforming(ledger)
281
- stage(ledger, 'claim')['evidence']['issue'] = 'example/repo#99'
282
- expect('claim for another issue fails C2', mutate(claim_other_issue), 1, 'violations', ['C2'])
283
-
284
- def claim_without_read(ledger):
285
- linked_issue_conforming(ledger)
286
- ledger['tool_uses'] = [use for use in ledger['tool_uses'] if use['request'] != 'TD-CL1']
287
- expect('claim without check-issue-claim read fails C3', mutate(claim_without_read), 1, 'violations', ['C3'])
288
-
289
- def write_before_read(ledger):
290
- linked_issue_conforming(ledger)
291
- uses = ledger['tool_uses']
292
- read = next(use for use in uses if use['request'] == 'TD-CL1')
293
- uses.remove(read)
294
- uses.append(read)
295
- expect('claim written before it was checked fails C3', mutate(write_before_read), 1, 'violations', ['C3'])
296
-
297
- def free_but_not_written(ledger):
298
- linked_issue_conforming(ledger)
299
- ledger['tool_uses'] = [use for use in ledger['tool_uses'] if use['request'] != 'TD-CL2']
300
- expect('a free issue that was never claimed on GitHub fails C3', mutate(free_but_not_written),
301
- 1, 'violations', ['C3'])
302
-
303
- def already_mine(ledger):
304
- linked_issue_conforming(ledger)
305
- stage(ledger, 'claim')['evidence']['check'] = 'claimed-by-you'
306
- ledger['tool_uses'] = [use for use in ledger['tool_uses'] if use['request'] != 'TD-CL2']
307
- expect('an issue already assigned to the claimant needs no new write', mutate(already_mine), 0, 'conforms')
308
-
309
- def unnumbered_branch(ledger):
310
- linked_issue_conforming(ledger)
311
- ledger['worktree']['branch'] = 'feat/status-filter'
312
- expect('branch without the issue number fails C4', mutate(unnumbered_branch), 1, 'violations', ['C4'])
313
-
314
- def claim_after_red(ledger):
315
- linked_issue_conforming(ledger)
316
- stages = ledger['stages']
317
- stages[1], stages[2] = stages[2], stages[1]
318
- expect('claim after RED is out of order', mutate(claim_after_red), 1, 'violations', ['S2'])
319
-
320
- def claim_without_authorization(ledger):
321
- linked_issue_conforming(ledger)
322
- stage(ledger, 'claim').pop('authorization')
323
- expect('claim without a person authorization fails A1', mutate(claim_without_authorization),
324
- 1, 'violations', ['A1'])
325
-
326
- expect('implementation without authorization', mutate(lambda l: stage(l, 'implement').pop('authorization')),
327
- 1, 'violations', ['A1'])
328
- expect('authorization that does not cover the stage',
329
- mutate(lambda l: l['authorizations'][0].update({'covers': ['implement']})), 1, 'violations', ['A1'])
330
-
331
- def agent_approves(ledger):
332
- ledger['authorizations'][0]['by'] = 'delivery-coordinator'
333
- expect('an agent cannot approve', mutate(agent_approves), 1, 'violations', ['A1', 'A2', 'T5'])
334
-
335
- def agent_merges(ledger):
336
- ledger['stages'].append({'stage': 'code_owner', 'status': 'done', 'actor': 'delivery-coordinator'})
337
- expect('an agent cannot merge', mutate(agent_merges), 1, 'violations', ['H1'])
338
-
339
- def deploy_without_review(ledger):
340
- ledger['stages'].append({'stage': 'release_owner', 'status': 'done', 'actor': 'person'})
341
- expect('deploy decision without Code Owner', mutate(deploy_without_review), 1, 'violations', ['H2'])
342
-
343
- expect('tool used without the Depot', mutate(lambda l: l['tool_uses'].append(
344
- {'tool': 'ssh', 'target': 'bb-example'})), 1, 'violations', ['T1'])
345
- expect('tool used after a denial', mutate(lambda l: l['tool_requests'][0].update({'disposition': 'denied'})),
346
- 1, 'violations', ['T2'])
347
- expect('purpose outside the catalog', mutate(lambda l: l['tool_requests'][0].update({'purpose': 'open-shell'})),
348
- 1, 'violations', ['T3'])
349
- expect('use differs from the request', mutate(lambda l: l['tool_uses'][0].update({'target': 'example/other#1'})),
350
- 1, 'violations', ['T4'])
351
- expect('GitHub write without authorization', mutate(lambda l: l['tool_requests'][1].pop('authorization')),
352
- 1, 'violations', ['T5'])
353
- expect('incomplete Depot request', mutate(lambda l: l['tool_requests'][0].pop('requester')),
354
- 1, 'violations', ['T6'])
355
-
356
- def issue_only(ledger):
357
- ledger['kind'] = 'issue-only'
358
- expect('issue-only work cannot write', mutate(issue_only), 1, 'violations', ['K1'])
359
-
360
- def issue_only_clean(ledger):
361
- ledger['kind'] = 'issue-only'
362
- ledger.pop('worktree')
363
- ledger['stages'] = ledger['stages'][:1]
364
- expect('issue-only work', mutate(issue_only_clean), 0, 'conforms')
365
-
366
- def issue_only_claim(ledger):
367
- issue_only_clean(ledger)
368
- ledger['stages'].append({'stage': 'claim', 'status': 'done', 'actor': 'delivery-coordinator',
369
- 'authorization': 'AUTH-1'})
370
- expect('issue-only work cannot claim', mutate(issue_only_claim), 1, 'violations', ['K1'])
371
-
372
- VISUAL = 'https://gist.githubusercontent.com/example/abc/raw/def/issue.png'
373
-
374
-
375
- def dedup_search(ledger, target='example/repo'):
376
- ledger['tool_requests'].append({
377
- 'id': 'TD-DUP', 'requester': 'delivery-coordinator', 'tool': 'github-read',
378
- 'purpose': 'check-duplicate-issues', 'target': target,
379
- 'operation': 'search open, closed and draft issues', 'disposition': 'eligible',
380
- })
381
- ledger['tool_uses'].append({'request': 'TD-DUP', 'tool': 'github-read', 'target': target})
382
-
383
-
384
- def issue_write(ledger, purpose, target, visual=None):
385
- if not any(auth['id'] == 'AUTH-2' for auth in ledger['authorizations']):
386
- ledger['authorizations'].append({'id': 'AUTH-2', 'by': 'person', 'role': 'Product Manager', 'covers': []})
387
- request_id = 'TD-' + purpose
388
- ledger['tool_requests'].append({
389
- 'id': request_id, 'requester': 'delivery-coordinator', 'tool': 'github-write', 'purpose': purpose,
390
- 'target': target, 'operation': purpose, 'disposition': 'eligible', 'authorization': 'AUTH-2',
391
- })
392
- use = {'request': request_id, 'tool': 'github-write', 'target': target}
393
- if visual is not None:
394
- use['visual'] = visual
395
- ledger['tool_uses'].append(use)
396
-
397
-
398
- def published_issue_without_visual(ledger):
399
- issue_only_clean(ledger)
400
- dedup_search(ledger)
401
- issue_write(ledger, 'publish-approved-issue', 'example/repo')
402
- expect('published issue without a visual fails V1', mutate(published_issue_without_visual),
403
- 1, 'violations', ['V1'])
404
-
405
- def published_after_dedup(ledger):
406
- issue_only_clean(ledger)
407
- dedup_search(ledger)
408
- issue_write(ledger, 'publish-approved-issue', 'example/repo', VISUAL)
409
- expect('issue filed after a duplicate search', mutate(published_after_dedup), 0, 'conforms')
410
-
411
- def published_without_dedup(ledger):
412
- issue_only_clean(ledger)
413
- issue_write(ledger, 'publish-approved-issue', 'example/repo', VISUAL)
414
- expect('issue filed without a duplicate search fails D1', mutate(published_without_dedup), 1, 'violations', ['D1'])
415
-
416
- def dedup_after_publish(ledger):
417
- issue_only_clean(ledger)
418
- issue_write(ledger, 'publish-approved-issue', 'example/repo', VISUAL)
419
- dedup_search(ledger)
420
- expect('duplicate search after filing fails D1', mutate(dedup_after_publish), 1, 'violations', ['D1'])
421
-
422
- def dedup_on_other_repo(ledger):
423
- issue_only_clean(ledger)
424
- dedup_search(ledger, 'example/other')
425
- issue_write(ledger, 'publish-approved-issue', 'example/repo', VISUAL)
426
- expect('duplicate search on another repository fails D1', mutate(dedup_on_other_repo), 1, 'violations', ['D1'])
427
-
428
- def reopened_with_history(ledger):
429
- issue_only_clean(ledger)
430
- dedup_search(ledger)
431
- issue_write(ledger, 'reopen-approved-issue', 'example/repo#7')
432
- issue_write(ledger, 'comment-approved-issue', 'example/repo#7', VISUAL)
433
- expect('reopen after a duplicate search with a history comment', mutate(reopened_with_history), 0, 'conforms')
434
-
435
- def reopened_without_dedup(ledger):
436
- issue_only_clean(ledger)
437
- issue_write(ledger, 'reopen-approved-issue', 'example/repo#7')
438
- expect('reopen without a duplicate search fails D1', mutate(reopened_without_dedup), 1, 'violations', ['D1'])
439
-
440
- def denied_dedup(ledger):
441
- published_after_dedup(ledger)
442
- next(req for req in ledger['tool_requests'] if req['id'] == 'TD-DUP')['disposition'] = 'unavailable'
443
- expect('a non-eligible duplicate search does not count', mutate(denied_dedup), 1, 'violations', ['D1', 'T2'])
444
-
445
- # Issue triage (I1-I2): issue-reviewer/references/triage.md
446
- def triage_read(ledger, target='example/repo#7'):
447
- ledger['tool_requests'].append({
448
- 'id': 'TD-TRI', 'requester': 'issue-reviewer', 'tool': 'github-read', 'purpose': 'check-issue-triage',
449
- 'target': target, 'operation': 'read issue, repository labels, author assignability', 'disposition': 'eligible',
450
- })
451
- ledger['tool_uses'].append({'request': 'TD-TRI', 'tool': 'github-read', 'target': target})
452
-
453
-
454
- def triaged_and_announced(ledger):
455
- issue_only_clean(ledger)
456
- triage_read(ledger)
457
- issue_write(ledger, 'triage-approved-issue', 'example/repo#7')
458
- issue_write(ledger, 'comment-approved-issue', 'example/repo#7', VISUAL)
459
- expect('triage read, applied and announced', mutate(triaged_and_announced), 0, 'conforms')
460
-
461
- def triage_without_read(ledger):
462
- issue_only_clean(ledger)
463
- issue_write(ledger, 'triage-approved-issue', 'example/repo#7')
464
- issue_write(ledger, 'comment-approved-issue', 'example/repo#7', VISUAL)
465
- expect('triage without check-issue-triage fails I1', mutate(triage_without_read), 1, 'violations', ['I1'])
466
-
467
- def triage_read_other_issue(ledger):
468
- issue_only_clean(ledger)
469
- triage_read(ledger, 'example/repo#8')
470
- issue_write(ledger, 'triage-approved-issue', 'example/repo#7')
471
- issue_write(ledger, 'comment-approved-issue', 'example/repo#7', VISUAL)
472
- expect('triage read of another issue fails I1', mutate(triage_read_other_issue), 1, 'violations', ['I1'])
473
-
474
- def triage_not_announced(ledger):
475
- issue_only_clean(ledger)
476
- triage_read(ledger)
477
- issue_write(ledger, 'triage-approved-issue', 'example/repo#7')
478
- expect('triage without a comment that states it fails I2', mutate(triage_not_announced), 1, 'violations', ['I2'])
479
-
480
- def triage_announced_before(ledger):
481
- issue_only_clean(ledger)
482
- triage_read(ledger)
483
- issue_write(ledger, 'comment-approved-issue', 'example/repo#7', VISUAL)
484
- issue_write(ledger, 'triage-approved-issue', 'example/repo#7')
485
- expect('a comment before the triage does not state it (I2)', mutate(triage_announced_before),
486
- 1, 'violations', ['I2'])
487
-
488
- for label, ledger in (('not JSON', '{'), ('unknown version', {'version': 2, 'kind': 'change'}),
489
- ('unknown stage', mutate(lambda l: l['stages'].append({'stage': 'deploy', 'status': 'done'})))):
490
- checks += 1
491
- status, report = run(ledger)
492
- if (status, report['verdict']) != (2, 'unusable'):
493
- failures.append('unusable input ' + label + ': observed ' + repr((status, report['verdict'])))
494
-
495
- checks += 1
496
- secret = 'token-should-not-leak'
497
- status, report = run(mutate(lambda l: l['tool_uses'].append({'tool': 'ssh', 'target': secret})))
498
- if secret in json.dumps(report):
499
- failures.append('findings echoed a ledger value')
500
-
501
- checks += 1
502
- out = io.StringIO()
503
- with contextlib.redirect_stdout(out):
504
- status = main([str(scratch / 'ledger.json'), '--catalog', str(scratch / 'missing.json')])
505
- if status != 2 or json.loads(out.getvalue())['verdict'] != 'unusable':
506
- failures.append('missing catalog must be unusable')
507
-
508
- checks += 1
509
- repo = scratch / 'repo'
510
- subprocess.run(['git', 'init', '-q', str(repo)], check=True)
511
- ledger_path = subprocess.run(['git', 'rev-parse', '--git-path', 'bbc-delivery-ledger.json'], cwd=repo,
512
- capture_output=True, text=True, check=True).stdout.strip()
513
- (repo / ledger_path).write_text(json.dumps(CONFORMING), encoding='utf-8')
514
- result = subprocess.run([sys.executable, '-I', '-B', str(entry), '--catalog', str(catalog)], cwd=repo,
515
- capture_output=True, text=True)
516
- if result.returncode != 0 or json.loads(result.stdout)['verdict'] != 'conforms':
517
- failures.append('default ledger path inside the Git directory was not read')
518
- untracked = subprocess.run(['git', 'status', '--porcelain'], cwd=repo, capture_output=True, text=True).stdout
519
- if untracked.strip():
520
- failures.append('ledger must not appear as a repository change')
521
-
522
- # Uppercase hex SHAs should be accepted
523
- checks += 1
524
- def with_upper_sha(l):
525
- l['worktree']['base_sha'] = 'A1B2C3D'
526
- l['stages'][5]['evidence']['head_sha'] = 'F0E1D2C'
527
- l['stages'][6]['evidence']['head_sha'] = 'F0E1D2C'
528
- status, report = run(mutate(with_upper_sha))
529
- if status != 0 or report['verdict'] != 'conforms':
530
- failures.append('uppercase hex SHA should conform: ' + json.dumps(report))
531
-
532
- # #45: a support command's ClickHouse and database reads are catalog tools, so recording them
533
- # honestly in the ledger must conform instead of raising T3.
534
- def ticket_reads(ledger):
535
- ledger['tool_requests'] += [
536
- {'id': 'TD-CH', 'requester': 'ticket', 'tool': 'clickhouse-read', 'purpose': 'query-approved-project-logs',
537
- 'target': 'log table, project <uuid>', 'operation': 'aggregate query, readonly=1, 2h window',
538
- 'disposition': 'eligible'},
539
- {'id': 'TD-DB', 'requester': 'ticket', 'tool': 'database-read', 'purpose': 'read-approved-project-records',
540
- 'target': 'project <uuid>', 'operation': 'select with read-scoped credential', 'disposition': 'eligible'},
541
- ]
542
- ledger['tool_uses'] += [
543
- {'request': 'TD-CH', 'tool': 'clickhouse-read', 'target': 'log table, project <uuid>'},
544
- {'request': 'TD-DB', 'tool': 'database-read', 'target': 'project <uuid>'},
545
- ]
546
- expect('ticket clickhouse and database reads are catalog tools', mutate(ticket_reads), 0, 'conforms')
547
-
548
- # #40: --server-log compares what the server actually ran (watcher-gate syslog,
549
- # `journalctl -t watcher-gate -o json`) with the ssh uses the agent recorded. A
550
- # command the server allowed with no matching ssh tool_use is a bypass (T7).
551
- def journal(*messages):
552
- path = scratch / 'gate.json'
553
- path.write_text(''.join(json.dumps({'SYSLOG_IDENTIFIER': 'watcher-gate', 'MESSAGE': m,
554
- '__REALTIME_TIMESTAMP': '1732870000000000'}) + '\n'
555
- for m in messages), encoding='utf-8')
556
- return path
557
-
558
-
559
- def with_ssh(*targets):
560
- def change(ledger):
561
- for i, target in enumerate(targets):
562
- ledger['tool_requests'].append({'id': f'SSH-{i}', 'requester': 'watcher-observability', 'tool': 'ssh',
563
- 'purpose': 'read-approved-container-history', 'target': target,
564
- 'operation': 'one historical batch', 'disposition': 'eligible'})
565
- ledger['tool_uses'].append({'request': f'SSH-{i}', 'tool': 'ssh', 'target': target})
566
- return mutate(change)
567
-
568
-
569
- def expect_server(label, ledger, log, status, verdict, violations=()):
570
- global checks
571
- checks += 1
572
- observed_status, report = run(ledger, '--server-log', str(log))
573
- observed = (observed_status, report['verdict'], rules(report))
574
- wanted = (status, verdict, sorted(violations))
575
- if observed != wanted:
576
- failures.append(f'{label}: expected {wanted!r}, observed {observed!r} {report.get("violations")}')
577
-
578
-
579
- allowed = 'allowed: docker logs -f --tail=1000 -- jobs-api'
580
- expect_server('recorded ssh use matches the server', with_ssh('bb-example jobs-api'), journal(allowed), 0, 'conforms')
581
- expect_server('unrecorded ssh is a bypass', CONFORMING, journal(allowed), 1, 'violations', ['T7'])
582
- expect_server('one use covers one command only', with_ssh('bb-example jobs-api'),
583
- journal(allowed, allowed), 1, 'violations', ['T7'])
584
- expect_server('denied commands did not run', CONFORMING,
585
- journal('denied: docker rm -f jobs-api', 'denied: bash'), 0, 'conforms')
586
- expect_server('discovery prefix matches its use', with_ssh('bb-example saas-app.v2-*'),
587
- journal("allowed: docker ps --filter 'name=^/?saas-app\\.v2-.*$' --format '{{.Names}}'"), 0, 'conforms')
588
- expect_server('a different container does not match', with_ssh('bb-example billing'),
589
- journal(allowed), 1, 'violations', ['T7'])
590
- checks += 1
591
- plain = scratch / 'gate.log'
592
- plain.write_text('Nov 29 10:00:01 host watcher-gate: ' + allowed + '\n', encoding='utf-8')
593
- status, report = run(CONFORMING, '--server-log', str(plain))
594
- if (status, rules(report)) != (1, ['T7']):
595
- failures.append(f'plain syslog line: {status} {report}')
596
- checks += 1
597
- broken = scratch / 'broken.log'
598
- broken.write_text('no gate lines here\n', encoding='utf-8')
599
- status, report = run(CONFORMING, '--server-log', str(broken))
600
- if status != 2 or report.get('verdict') != 'unusable':
601
- failures.append(f'log without watcher-gate entries must be unusable: {status} {report}')
602
-
603
- # End to end: the real server gate writes the log this audit reads. A fake
604
- # `logger` records exactly what watcher-gate.sh sends to syslog.
605
- gate = Path('../watcher-observability/server/watcher-gate.sh').resolve()
606
- if gate.is_file() and shutil.which('bash'):
607
- bin_dir = scratch / 'gate-bin'
608
- bin_dir.mkdir(exist_ok=True)
609
- syslog = scratch / 'syslog.txt'
610
- (bin_dir / 'logger').write_text('#!/usr/bin/env bash\nshift 3\nprintf "Nov 29 10:00:01 host watcher-gate[42]: %s\\n" "$*" >>"$SYSLOG_FILE"\n')
611
- (bin_dir / 'docker').write_text('#!/usr/bin/env bash\nexit 0\n')
612
- for tool in ('logger', 'docker'):
613
- (bin_dir / tool).chmod(0o755)
614
- env = dict(os.environ, PATH=f"{bin_dir}{os.pathsep}{os.environ['PATH']}", SYSLOG_FILE=str(syslog))
615
- for command in ('docker logs -f --tail=1000 -- jobs-api', 'docker rm -f jobs-api'):
616
- subprocess.run(['bash', str(gate)], env=dict(env, SSH_ORIGINAL_COMMAND=command), capture_output=True)
617
- checks += 1
618
- status, report = run(CONFORMING, '--server-log', str(syslog))
619
- if (status, rules(report)) != (1, ['T7']) or 'jobs-api' not in json.dumps(report):
620
- failures.append(f'gate end to end, unrecorded: {status} {report} log={syslog.read_text()!r}')
621
- checks += 1
622
- status, report = run(with_ssh('bb-example jobs-api'), '--server-log', str(syslog))
623
- if status != 0:
624
- failures.append(f'gate end to end, recorded (the denied rm must not count): {status} {report}')
625
-
626
- # Lane classification support (C1): trivial, standard, high-risk
627
- expect('standard lane conforms identically', mutate(lambda l: l.update({'lane': 'standard'})), 0, 'conforms')
628
-
629
- def trivial_lane(ledger):
630
- ledger['lane'] = 'trivial'
631
- stage(ledger, 'reproduce').update({'status': 'gap', 'gap_type': 'no-runnable-test',
632
- 'reason': 'Docs-only trivial lane.', 'evidence': {}})
633
- stage(ledger, 'verify').update({'status': 'gap', 'reason': 'Validated with validate-skills.py.', 'evidence': {}})
634
- expect('trivial lane with proportional verification produces gaps only', mutate(trivial_lane), 0, 'gaps', gaps=['G1'])
635
-
636
- expect('high-risk lane without residual_risk fails L1',
637
- mutate(lambda l: l.update({'lane': 'high-risk'})), 1, 'violations', ['L1'])
638
-
639
- def high_risk_with_scout(ledger):
640
- ledger['lane'] = 'high-risk'
641
- ledger['stages'].append({'stage': 'residual_risk', 'status': 'done', 'actor': 'risk-scout'})
642
- expect('high-risk lane with residual_risk conforms', mutate(high_risk_with_scout), 0, 'conforms')
643
-
644
- def high_risk_with_gap(ledger):
645
- ledger['lane'] = 'high-risk'
646
- ledger['stages'].append({'stage': 'residual_risk', 'status': 'gap', 'reason': 'Skipped.'})
647
- expect('high-risk lane with residual_risk gap still fails L1',
648
- mutate(high_risk_with_gap), 1, 'violations', ['L1'], ['G1'])
649
-
650
- checks += 1
651
- status, report = run(mutate(lambda l: l.update({'lane': 'unknown-lane'})))
652
- if (status, report.get('verdict')) != (2, 'unusable'):
653
- failures.append('invalid lane must be unusable: ' + repr((status, report)))
654
-
655
- for failure in failures:
656
- print('FAIL ' + failure, file=sys.stderr)
657
- if failures:
658
- sys.exit(1)
659
- print('PASS ' + str(checks) + ' checks')
660
- PY