@bbcsoftwarefactory/skills 0.1.1 → 1.1.3
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.
- package/dist/index.json +2 -3
- package/dist/skills/code-quality-gate/SKILL.md +4 -5
- package/dist/skills/code-quality-gate/references/gate-bigo.md +1 -1
- package/dist/skills/delivery-coordinator/SKILL.md +7 -8
- package/dist/skills/delivery-coordinator/references/hud-output.md +1 -1
- package/dist/skills/delivery-coordinator/references/issue-claim.md +5 -5
- package/dist/skills/delivery-coordinator/references/issue-dedup.md +1 -1
- package/dist/skills/delivery-coordinator/references/worktree-isolation.md +2 -2
- package/dist/skills/greptile-review-loop/SKILL.md +3 -4
- package/dist/skills/greptile-review-loop/references/review-protocol.md +1 -1
- package/dist/skills/issue-reviewer/SKILL.md +10 -13
- package/dist/skills/issue-reviewer/references/review-format.md +1 -1
- package/dist/skills/issue-reviewer/references/triage.md +6 -6
- package/dist/skills/question-gate/SKILL.md +6 -8
- package/dist/skills/question-gate/references/client-hooks.md +10 -10
- package/dist/skills/question-gate/references/diagram-routing.md +3 -3
- package/dist/skills/skill-clean-architecture/SKILL.md +2 -3
- package/dist/skills/skill-clean-architecture/references/source-headers.md +2 -3
- package/dist/skills/tool-depot/references/readonly-access.md +1 -1
- package/dist/skills/tool-depot/references/tool-catalog.json +2 -2
- package/dist/skills/watcher-observability/SKILL.md +3 -4
- package/dist/skills/watcher-observability/references/batch-protocol.md +6 -6
- package/dist/skills/watcher-observability/references/configuration.md +1 -1
- package/dist/skills/watcher-observability/references/error-scan.md +1 -1
- package/dist/skills/watcher-observability/references/live-protocol.md +8 -8
- package/dist/skills/watcher-observability/references/server-account.md +8 -8
- package/dist/skills/watcher-observability/server/watcher-gate.sh +1 -1
- package/dist/skills/watcher-observability/tests/watcher-gate.test.sh +10 -7
- package/dist/skills/workflow-auditor/SKILL.md +3 -5
- package/dist/skills/workflow-auditor/references/audit-protocol.md +3 -3
- package/dist/skills/workflow-auditor/references/ledger.md +1 -1
- package/package.json +26 -27
- package/dist/skills/code-quality-gate/scripts/extract-diff-scope.py +0 -225
- package/dist/skills/code-quality-gate/tests/extract-diff-scope.test.sh +0 -200
- package/dist/skills/delivery-coordinator/scripts/check-issue-claim.py +0 -363
- package/dist/skills/delivery-coordinator/scripts/check-worktree-guard.py +0 -220
- package/dist/skills/delivery-coordinator/scripts/classify-change-lane.py +0 -263
- package/dist/skills/delivery-coordinator/tests/check-issue-claim.test.sh +0 -221
- package/dist/skills/delivery-coordinator/tests/check-worktree-guard.test.sh +0 -123
- package/dist/skills/delivery-coordinator/tests/classify-change-lane.test.sh +0 -124
- package/dist/skills/greptile-review-loop/scripts/review-loop.py +0 -435
- package/dist/skills/greptile-review-loop/tests/review-loop.test.sh +0 -331
- package/dist/skills/issue-reviewer/scripts/fetch-issue-attachments.py +0 -239
- package/dist/skills/issue-reviewer/scripts/plan-issue-triage.py +0 -311
- package/dist/skills/issue-reviewer/tests/fetch-issue-attachments.test.sh +0 -90
- package/dist/skills/issue-reviewer/tests/plan-issue-triage.test.sh +0 -168
- package/dist/skills/question-gate/scripts/triage-question.py +0 -713
- package/dist/skills/question-gate/tests/question-corpus.test.sh +0 -32
- package/dist/skills/question-gate/tests/triage-question.test.sh +0 -441
- package/dist/skills/skill-clean-architecture/scripts/measure-bigo.py +0 -427
- package/dist/skills/skill-clean-architecture/tests/measure-bigo.test.sh +0 -222
- package/dist/skills/watcher-observability/scripts/error-scan.py +0 -318
- package/dist/skills/watcher-observability/scripts/resolve-containers.py +0 -155
- package/dist/skills/watcher-observability/scripts/save-counts.py +0 -381
- package/dist/skills/watcher-observability/scripts/summarize-logs.py +0 -113
- package/dist/skills/watcher-observability/scripts/watch-live.py +0 -606
- package/dist/skills/watcher-observability/tests/error-scan.test.sh +0 -257
- package/dist/skills/watcher-observability/tests/live-windows.test.sh +0 -177
- package/dist/skills/watcher-observability/tests/resolve-containers.test.sh +0 -87
- package/dist/skills/watcher-observability/tests/save-counts.test.sh +0 -338
- package/dist/skills/watcher-observability/tests/sleuth.test.sh +0 -176
- package/dist/skills/watcher-observability/tests/summarize-logs.test.sh +0 -105
- package/dist/skills/watcher-observability/tests/watcher-config.test.sh +0 -70
- package/dist/skills/workflow-auditor/scripts/audit-ledger.py +0 -624
- package/dist/skills/workflow-auditor/scripts/list-zed-commands.py +0 -183
- package/dist/skills/workflow-auditor/scripts/render-workflow.py +0 -201
- package/dist/skills/workflow-auditor/scripts/validate-skills.py +0 -444
- package/dist/skills/workflow-auditor/tests/audit-ledger.test.sh +0 -660
- package/dist/skills/workflow-auditor/tests/list-zed-commands.test.sh +0 -93
- package/dist/skills/workflow-auditor/tests/render-workflow.test.sh +0 -135
- package/dist/skills/workflow-auditor/tests/validate-skills.test.sh +0 -190
|
@@ -1,427 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
"""measure-bigo.py: count lines and estimate a BigO score for the file-header contract.
|
|
3
|
-
|
|
4
|
-
BigO: O(n) score:3
|
|
5
|
-
keywords: [CreateUserUseCase, IUserRepository]
|
|
6
|
-
GOAL: <written by the agent, not by this script>
|
|
7
|
-
|
|
8
|
-
Score (5 is best). Do not rewrite a correct single pass just to raise it.
|
|
9
|
-
5 O(1) no loop over unbounded data
|
|
10
|
-
4 O(log n) logarithmic search (mid + low/high inside one loop)
|
|
11
|
-
3 O(n) one pass, including several sequential passes
|
|
12
|
-
2 O(n log n) sort
|
|
13
|
-
1 O(n^2) nested loops, lookup inside a loop, or object-spread inside reduce
|
|
14
|
-
1 O(n^3) three or more nested loops (same score, worse notation)
|
|
15
|
-
|
|
16
|
-
Usage (from the repo root):
|
|
17
|
-
python3 -I -B skills/skill-clean-architecture/scripts/measure-bigo.py <file-or-dir> [more...]
|
|
18
|
-
python3 -I -B .agents/skills/skill-clean-architecture/scripts/measure-bigo.py <file-or-dir> [more...]
|
|
19
|
-
|
|
20
|
-
Pure standard library, one process for every file: no awk, find or sort, so it
|
|
21
|
-
behaves the same on Linux, macOS and Windows.
|
|
22
|
-
"""
|
|
23
|
-
|
|
24
|
-
from bisect import bisect_left
|
|
25
|
-
import os
|
|
26
|
-
import re
|
|
27
|
-
import sys
|
|
28
|
-
|
|
29
|
-
EXTENSIONS = ('.ts', '.tsx', '.js', '.mjs', '.cjs', '.py', '.java', '.go', '.php')
|
|
30
|
-
EXCLUDED = ('/node_modules/', '/dist/', '/.git/', '/coverage/')
|
|
31
|
-
|
|
32
|
-
ITER = r'forEach|map|filter|reduce|reduceRight|flatMap|some|every|find|findIndex'
|
|
33
|
-
# One alternation instead of six searches per line: minified bundles have lines of
|
|
34
|
-
# 100k+ characters, and this test runs on every line.
|
|
35
|
-
LOOP = re.compile('|'.join('(?:' + expr + ')' for expr in (
|
|
36
|
-
r'(^|[^A-Za-z0-9_.$])for\s*(await\s*)?\(',
|
|
37
|
-
r'(^|[^A-Za-z0-9_.$])for\s+',
|
|
38
|
-
r'(^|[^A-Za-z0-9_.$])while\s*\(',
|
|
39
|
-
r'(^|[^A-Za-z0-9_.$])while\s+.*:\s*$',
|
|
40
|
-
r'(^|[^A-Za-z0-9_.$])do\s*\{',
|
|
41
|
-
r'\.(' + ITER + r'|sort)\s*\(',
|
|
42
|
-
)), re.ASCII)
|
|
43
|
-
# The stripper jumps between these tokens instead of visiting every character.
|
|
44
|
-
SPECIAL = re.compile(r'//|/\*|["\'`]')
|
|
45
|
-
# Body of a string opened by the quote: escaped pairs or any other character, then
|
|
46
|
-
# the closing quote. A trailing lone backslash cannot match, which leaves the string
|
|
47
|
-
# unterminated to the end of the line, as the character loop did.
|
|
48
|
-
STRING_END = {q: re.compile(r'(?:[^\\' + q + r']|\\.)*' + q, re.S) for q in '"\'`'}
|
|
49
|
-
REDUCE = re.compile(r'\.reduce\s*\(', re.ASCII)
|
|
50
|
-
SORT = re.compile(r'\.sort\s*\(', re.ASCII)
|
|
51
|
-
LOOKUP = re.compile(r'\.(includes|indexOf)\s*\(', re.ASCII)
|
|
52
|
-
CALL = re.compile(r'\.(' + ITER + r')\s*\(', re.ASCII)
|
|
53
|
-
INNER = re.compile(r'\.(' + ITER + r'|includes|indexOf)\s*\(', re.ASCII)
|
|
54
|
-
SPREAD = re.compile(r'\.\.\.[A-Za-z_]')
|
|
55
|
-
MID = re.compile(r'(^|[^A-Za-z0-9_])mid([^A-Za-z0-9_]|$)')
|
|
56
|
-
BOUND = re.compile(r'(^|[^A-Za-z0-9_])(low|high|left|right)([^A-Za-z0-9_]|$)')
|
|
57
|
-
BLANK = re.compile(r'^[ \t]*$')
|
|
58
|
-
LONE_BRACE = re.compile(r'^[ \t]*\{[ \t]*$')
|
|
59
|
-
|
|
60
|
-
IMPORT_LINE = re.compile(r'^\s*import\s', re.ASCII)
|
|
61
|
-
EXPORT_LINE = re.compile(r'^\s*export\s', re.ASCII)
|
|
62
|
-
BRACES = re.compile(r'\{[^}]*\}')
|
|
63
|
-
EXPORT_DECL = re.compile(r'(class|interface|type|function|const|enum)\s+([A-Za-z_][A-Za-z0-9_]*)', re.ASCII)
|
|
64
|
-
KEYWORD_LIMIT = 12
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
class Stripper:
|
|
68
|
-
"""Drop comments and string contents, carrying /* ... */ state across lines."""
|
|
69
|
-
|
|
70
|
-
def __init__(self):
|
|
71
|
-
self.in_block = False
|
|
72
|
-
|
|
73
|
-
def strip(self, line):
|
|
74
|
-
if self.in_block:
|
|
75
|
-
end = line.find('*/')
|
|
76
|
-
if end < 0:
|
|
77
|
-
return ''
|
|
78
|
-
self.in_block = False
|
|
79
|
-
line = line[end + 2:]
|
|
80
|
-
out = []
|
|
81
|
-
i, n = 0, len(line)
|
|
82
|
-
while i < n:
|
|
83
|
-
token = SPECIAL.search(line, i)
|
|
84
|
-
if not token:
|
|
85
|
-
out.append(line[i:])
|
|
86
|
-
break
|
|
87
|
-
out.append(line[i:token.start()])
|
|
88
|
-
mark = token.group(0)
|
|
89
|
-
if mark == '//':
|
|
90
|
-
break
|
|
91
|
-
if mark == '/*':
|
|
92
|
-
end = line.find('*/', token.end())
|
|
93
|
-
if end < 0:
|
|
94
|
-
self.in_block = True
|
|
95
|
-
break
|
|
96
|
-
i = end + 2
|
|
97
|
-
continue
|
|
98
|
-
body = STRING_END[mark].match(line, token.end())
|
|
99
|
-
i = body.end() if body else n
|
|
100
|
-
out.append(' ')
|
|
101
|
-
return ''.join(out)
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
def line_indent(text):
|
|
105
|
-
width = 0
|
|
106
|
-
for char in text:
|
|
107
|
-
if char == ' ':
|
|
108
|
-
width += 1
|
|
109
|
-
elif char == '\t':
|
|
110
|
-
width += 2
|
|
111
|
-
else:
|
|
112
|
-
break
|
|
113
|
-
return width
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
def loop_kind(text):
|
|
117
|
-
if REDUCE.search(text):
|
|
118
|
-
return 'reduce'
|
|
119
|
-
if SORT.search(text):
|
|
120
|
-
return 'sort'
|
|
121
|
-
if LOOP.search(text):
|
|
122
|
-
return 'loop'
|
|
123
|
-
return ''
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
def inline_lookup(text):
|
|
127
|
-
"""#43: ids.map(id => items.find(...)) nests a pass inside one line.
|
|
128
|
-
|
|
129
|
-
A call is nested only when the second iteration sits inside the first call's
|
|
130
|
-
parentheses; a chain like a.filter(...).map(...) stays sequential.
|
|
131
|
-
"""
|
|
132
|
-
rest = text
|
|
133
|
-
while True:
|
|
134
|
-
match = CALL.search(rest)
|
|
135
|
-
if not match:
|
|
136
|
-
return False
|
|
137
|
-
rest = rest[match.end():]
|
|
138
|
-
depth, inner = 1, []
|
|
139
|
-
for char in rest:
|
|
140
|
-
if char == '(':
|
|
141
|
-
depth += 1
|
|
142
|
-
elif char == ')':
|
|
143
|
-
depth -= 1
|
|
144
|
-
if depth == 0:
|
|
145
|
-
break
|
|
146
|
-
inner.append(char)
|
|
147
|
-
if INNER.search(''.join(inner)):
|
|
148
|
-
return True
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
def records(text):
|
|
152
|
-
"""Split like awk's default RS: on \\n only, keeping a final unterminated line."""
|
|
153
|
-
lines = text.split('\n')
|
|
154
|
-
if lines and lines[-1] == '':
|
|
155
|
-
lines.pop()
|
|
156
|
-
return lines
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
def analyze(text):
|
|
160
|
-
counts = dict.fromkeys(('loc', 'code_lines', 'loops', 'max_nest', 'lookup', 'spread', 'sorts', 'binary'), 0)
|
|
161
|
-
stripper = Stripper()
|
|
162
|
-
stack = [] # (indent, kind) of the open loops
|
|
163
|
-
saw_mid = saw_bound = False
|
|
164
|
-
for raw in records(text):
|
|
165
|
-
if raw.endswith('\r'):
|
|
166
|
-
raw = raw[:-1]
|
|
167
|
-
counts['loc'] += 1
|
|
168
|
-
stripped = stripper.strip(raw)
|
|
169
|
-
if BLANK.match(stripped):
|
|
170
|
-
continue
|
|
171
|
-
counts['code_lines'] += 1
|
|
172
|
-
if LONE_BRACE.match(stripped):
|
|
173
|
-
continue
|
|
174
|
-
indent = line_indent(stripped)
|
|
175
|
-
while stack and stack[-1][0] >= indent:
|
|
176
|
-
stack.pop()
|
|
177
|
-
kind = loop_kind(stripped)
|
|
178
|
-
if kind:
|
|
179
|
-
counts['loops'] += 1
|
|
180
|
-
stack.append((indent, kind))
|
|
181
|
-
counts['max_nest'] = max(counts['max_nest'], len(stack))
|
|
182
|
-
# #55: count a sort even when the same line is classed as reduce
|
|
183
|
-
# (`xs.sort(...).reduce(...)`); the kind stays reduce for spread detection.
|
|
184
|
-
if SORT.search(stripped):
|
|
185
|
-
counts['sorts'] += 1
|
|
186
|
-
if stack and (LOOKUP.search(stripped) or inline_lookup(stripped)):
|
|
187
|
-
counts['lookup'] += 1
|
|
188
|
-
if any(k == 'reduce' for _, k in stack) and SPREAD.search(stripped):
|
|
189
|
-
counts['spread'] += 1
|
|
190
|
-
if stack:
|
|
191
|
-
saw_mid = saw_mid or bool(MID.search(stripped))
|
|
192
|
-
saw_bound = saw_bound or bool(BOUND.search(stripped))
|
|
193
|
-
if saw_mid and saw_bound:
|
|
194
|
-
counts['binary'] = 1
|
|
195
|
-
return counts
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
# #43: per-function breakdown, standard library only (ADR 0001). Named functions
|
|
199
|
-
# are found on comment- and string-free lines; a brace language's body is the
|
|
200
|
-
# matching {...} block, a Python body is the indented block under `def`. Each body
|
|
201
|
-
# is scored with the same analyze() as the file. Anonymous callbacks are part of
|
|
202
|
-
# the function that contains them.
|
|
203
|
-
DECLARATIONS = (
|
|
204
|
-
re.compile(r'^\s*(?:export\s+)?(?:default\s+)?(?:async\s+)?function\s*\*?\s*([A-Za-z_$][\w$]*)\s*\('),
|
|
205
|
-
re.compile(r'^\s*(?:export\s+)?(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*(?::[^=]+)?=\s*(?:async\s+)?'
|
|
206
|
-
r'(?:function\b|\([^)]*\)\s*(?::[^=]+)?=>|[A-Za-z_$][\w$]*\s*=>)'),
|
|
207
|
-
re.compile(r'^\s*func\s+(?:\([^)]*\)\s*)?([A-Za-z_]\w*)\s*\('),
|
|
208
|
-
re.compile(r'^\s*(?:(?:public|private|protected|static|final|abstract|async|override|readonly|'
|
|
209
|
-
r'synchronized|function)\s+)*(?:[\w$<>\[\],.?]+\s+)?([A-Za-z_$][\w$]*)\s*\([^;]*\)\s*'
|
|
210
|
-
r'(?::\s*[^{;=]+)?(?:throws\s+[\w.,\s]+)?\{\s*$'),
|
|
211
|
-
)
|
|
212
|
-
NOT_FUNCTIONS = {'if', 'for', 'while', 'switch', 'catch', 'return', 'function', 'do', 'else', 'with',
|
|
213
|
-
'typeof', 'new', 'await', 'super', 'constructor_call'}
|
|
214
|
-
PY_DEF = re.compile(r'^([ \t]*)(?:async\s+)?def\s+([A-Za-z_]\w*)\s*\(')
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
def brace_pairs(stripped):
|
|
218
|
-
"""One pass: (open line, close line) for every {...} in order of its opening."""
|
|
219
|
-
pairs, stack = [], []
|
|
220
|
-
for index, line in enumerate(stripped):
|
|
221
|
-
for char in line:
|
|
222
|
-
if char == '{':
|
|
223
|
-
stack.append(len(pairs))
|
|
224
|
-
pairs.append([index, None])
|
|
225
|
-
elif char == '}' and stack:
|
|
226
|
-
pairs[stack.pop()][1] = index
|
|
227
|
-
return [(opened, closed) for opened, closed in pairs if closed is not None]
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
def find_functions(text, python):
|
|
231
|
-
lines = [raw[:-1] if raw.endswith('\r') else raw for raw in records(text)]
|
|
232
|
-
found = []
|
|
233
|
-
if python:
|
|
234
|
-
for index, line in enumerate(lines):
|
|
235
|
-
match = PY_DEF.match(line)
|
|
236
|
-
if not match:
|
|
237
|
-
continue
|
|
238
|
-
indent, end = len(match.group(1).expandtabs()), index
|
|
239
|
-
for later in range(index + 1, len(lines)):
|
|
240
|
-
if lines[later].strip() and len(lines[later]) - len(lines[later].lstrip()) <= indent \
|
|
241
|
-
and not lines[later].lstrip().startswith('#'):
|
|
242
|
-
break
|
|
243
|
-
if lines[later].strip():
|
|
244
|
-
end = later
|
|
245
|
-
found.append((match.group(2), index, end))
|
|
246
|
-
return lines, found
|
|
247
|
-
stripper = Stripper()
|
|
248
|
-
stripped = [stripper.strip(line) for line in lines]
|
|
249
|
-
pairs = brace_pairs(stripped)
|
|
250
|
-
opens = [opened for opened, _ in pairs]
|
|
251
|
-
for index, line in enumerate(stripped):
|
|
252
|
-
for pattern in DECLARATIONS:
|
|
253
|
-
match = pattern.match(line)
|
|
254
|
-
if not match or match.group(1) in NOT_FUNCTIONS:
|
|
255
|
-
continue
|
|
256
|
-
arrow = pattern is DECLARATIONS[1]
|
|
257
|
-
if arrow and '{' not in line:
|
|
258
|
-
found.append((match.group(1), index, index)) # expression-bodied arrow
|
|
259
|
-
break
|
|
260
|
-
# The body is the first {...} opening on this line or within the next two.
|
|
261
|
-
first = bisect_left(opens, index)
|
|
262
|
-
if first < len(pairs) and pairs[first][0] <= index + 2:
|
|
263
|
-
found.append((match.group(1), index, pairs[first][1]))
|
|
264
|
-
break
|
|
265
|
-
return lines, found
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
def function_reports(text, python):
|
|
269
|
-
"""Score each function on the lines it owns: a line belongs to the innermost
|
|
270
|
-
named function around it, so nested named functions are measured on their own
|
|
271
|
-
and the total work stays linear in the file size."""
|
|
272
|
-
lines, found = find_functions(text, python)
|
|
273
|
-
if not found:
|
|
274
|
-
return []
|
|
275
|
-
owner = [None] * len(lines)
|
|
276
|
-
for number, (_, start, end) in sorted(enumerate(found), key=lambda item: (item[1][1], -item[1][2])):
|
|
277
|
-
owner[start:end + 1] = [number] * (end + 1 - start)
|
|
278
|
-
bodies = [[] for _ in found]
|
|
279
|
-
for index, number in enumerate(owner):
|
|
280
|
-
if number is not None:
|
|
281
|
-
bodies[number].append(lines[index])
|
|
282
|
-
return [(name, start + 1, *decide_bigo(analyze('\n'.join(bodies[number]) + '\n')))
|
|
283
|
-
for number, (name, start, _) in enumerate(found)]
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
def suggest_keywords(text):
|
|
287
|
-
exports, imports, seen = [], [], set()
|
|
288
|
-
|
|
289
|
-
def emit(bucket, name):
|
|
290
|
-
name = name.strip(' \t')
|
|
291
|
-
if not name or name in ('type', 'typeof', 'as', 'from') or re.search(r'[^A-Za-z0-9_]', name):
|
|
292
|
-
return
|
|
293
|
-
if (bucket, name) in seen:
|
|
294
|
-
return
|
|
295
|
-
seen.add((bucket, name))
|
|
296
|
-
(exports if bucket == 'export' else imports).append(name)
|
|
297
|
-
|
|
298
|
-
for line in records(text):
|
|
299
|
-
if IMPORT_LINE.match(line):
|
|
300
|
-
braces = BRACES.search(line)
|
|
301
|
-
if braces:
|
|
302
|
-
inner = re.sub(r'type\s+', '', braces.group(0)[1:-1], flags=re.ASCII)
|
|
303
|
-
for item in inner.split(','):
|
|
304
|
-
# #55: `B as C` binds the local name C, which is what the file uses.
|
|
305
|
-
alias = re.search(r'\sas\s+([A-Za-z_][A-Za-z0-9_]*)', item, re.ASCII)
|
|
306
|
-
item = alias.group(1) if alias else item
|
|
307
|
-
emit('import', re.sub(r'[^A-Za-z0-9_]', '', item))
|
|
308
|
-
if EXPORT_LINE.match(line):
|
|
309
|
-
decl = EXPORT_DECL.search(line)
|
|
310
|
-
if decl:
|
|
311
|
-
emit('export', decl.group(2))
|
|
312
|
-
return (exports + imports)[:KEYWORD_LIMIT]
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
def decide_bigo(c):
|
|
316
|
-
if c['max_nest'] >= 3:
|
|
317
|
-
return 'O(n^3)', 1
|
|
318
|
-
if c['max_nest'] >= 2 or c['lookup'] > 0 or c['spread'] > 0:
|
|
319
|
-
return 'O(n^2)', 1
|
|
320
|
-
if c['sorts'] > 0:
|
|
321
|
-
return 'O(n log n)', 2
|
|
322
|
-
if c['binary'] > 0 and c['loops'] > 0:
|
|
323
|
-
return 'O(log n)', 4
|
|
324
|
-
if c['loops'] > 0:
|
|
325
|
-
return 'O(n)', 3
|
|
326
|
-
return 'O(1)', 5
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
def signals(c):
|
|
330
|
-
out = []
|
|
331
|
-
clean = c['max_nest'] < 2 and c['lookup'] == 0 and c['spread'] == 0
|
|
332
|
-
if c['max_nest'] >= 3:
|
|
333
|
-
out.append('deep_nesting')
|
|
334
|
-
if c['max_nest'] >= 2:
|
|
335
|
-
out.append('nested_loop')
|
|
336
|
-
if c['lookup'] > 0:
|
|
337
|
-
out.append('lookup_in_loop')
|
|
338
|
-
if c['spread'] > 0:
|
|
339
|
-
out.append('spread_in_reduce')
|
|
340
|
-
if c['sorts'] > 0:
|
|
341
|
-
out.append('sort')
|
|
342
|
-
if c['binary'] > 0 and clean:
|
|
343
|
-
out.append('logarithmic')
|
|
344
|
-
if c['loops'] > 0 and clean and c['sorts'] == 0 and c['binary'] == 0:
|
|
345
|
-
out.append('single_pass')
|
|
346
|
-
if c['loops'] == 0 or not out:
|
|
347
|
-
out.append('constant')
|
|
348
|
-
return out
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
def quote(value):
|
|
352
|
-
return '"' + value.replace('\\', '\\\\').replace('"', '\\"') + '"'
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
def collect_files(target):
|
|
356
|
-
if os.path.isfile(target):
|
|
357
|
-
return [target]
|
|
358
|
-
if not os.path.isdir(target):
|
|
359
|
-
print(f'Not a file or directory: {target}', file=sys.stderr)
|
|
360
|
-
return []
|
|
361
|
-
found = []
|
|
362
|
-
for root, _dirs, files in os.walk(target):
|
|
363
|
-
for name in files:
|
|
364
|
-
path = os.path.join(root, name)
|
|
365
|
-
probe = path.replace(os.sep, '/')
|
|
366
|
-
if (name.endswith(EXTENSIONS) and not os.path.islink(path)
|
|
367
|
-
and not any(part in probe for part in EXCLUDED)):
|
|
368
|
-
found.append(path)
|
|
369
|
-
return sorted(found)
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
def report(path):
|
|
373
|
-
with open(path, encoding='utf-8', errors='replace', newline='') as handle:
|
|
374
|
-
text = handle.read()
|
|
375
|
-
c = analyze(text)
|
|
376
|
-
bigo, score = decide_bigo(c)
|
|
377
|
-
lines = [
|
|
378
|
-
f' - file: {quote(path)}',
|
|
379
|
-
f' loc: {c["loc"]}',
|
|
380
|
-
f' code_lines: {c["code_lines"]}',
|
|
381
|
-
f' bigo: {quote(bigo)}',
|
|
382
|
-
f' bigo_score: {score}',
|
|
383
|
-
f' header_bigo: {quote(f"{bigo} score:{score}")}',
|
|
384
|
-
f' loops: {c["loops"]}',
|
|
385
|
-
f' max_loop_nesting: {c["max_nest"]}',
|
|
386
|
-
f' lookup_in_loop: {c["lookup"]}',
|
|
387
|
-
f' spread_in_reduce: {c["spread"]}',
|
|
388
|
-
f' sorts: {c["sorts"]}',
|
|
389
|
-
' signals:',
|
|
390
|
-
]
|
|
391
|
-
lines += [f' - {name}' for name in signals(c)]
|
|
392
|
-
keywords = suggest_keywords(text)
|
|
393
|
-
if keywords:
|
|
394
|
-
lines.append(' suggested_keywords:')
|
|
395
|
-
lines += [f' - {quote(word)}' for word in keywords]
|
|
396
|
-
else:
|
|
397
|
-
lines.append(' suggested_keywords: []')
|
|
398
|
-
functions = function_reports(text, path.endswith('.py'))
|
|
399
|
-
if functions:
|
|
400
|
-
lines.append(' functions:')
|
|
401
|
-
for name, line, f_bigo, f_score in functions:
|
|
402
|
-
lines += [f' - name: {quote(name)}', f' line: {line}',
|
|
403
|
-
f' bigo: {quote(f_bigo)}', f' bigo_score: {f_score}']
|
|
404
|
-
else:
|
|
405
|
-
lines.append(' functions: []')
|
|
406
|
-
return '\n'.join(lines)
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
def main(argv):
|
|
410
|
-
if not argv:
|
|
411
|
-
print(f'Usage: {sys.argv[0]} <file-or-directory> [more...]', file=sys.stderr)
|
|
412
|
-
return 1
|
|
413
|
-
out = sys.stdout
|
|
414
|
-
out.write('results:\n')
|
|
415
|
-
found = False
|
|
416
|
-
for target in argv:
|
|
417
|
-
for path in collect_files(target):
|
|
418
|
-
out.write(report(path) + '\n')
|
|
419
|
-
found = True
|
|
420
|
-
if not found:
|
|
421
|
-
print('No source files found.', file=sys.stderr)
|
|
422
|
-
return 1
|
|
423
|
-
return 0
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
if __name__ == '__main__':
|
|
427
|
-
sys.exit(main(sys.argv[1:]))
|
|
@@ -1,222 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bash
|
|
2
|
-
set -euo pipefail
|
|
3
|
-
cd -- "${BASH_SOURCE[0]%/*}/.."
|
|
4
|
-
|
|
5
|
-
SCRIPT="$PWD/scripts/measure-bigo.py"
|
|
6
|
-
PYTHON_BIN=$(command -v python3 || command -v python)
|
|
7
|
-
run() { "$PYTHON_BIN" -I -B "$SCRIPT" "$@"; }
|
|
8
|
-
scratch=$(mktemp -d)
|
|
9
|
-
trap 'rm -rf -- "$scratch"' EXIT
|
|
10
|
-
|
|
11
|
-
checks=0
|
|
12
|
-
|
|
13
|
-
cat >"$scratch/sample.ts" <<'EOF'
|
|
14
|
-
export function constantFn(x: number): number {
|
|
15
|
-
return x + 1
|
|
16
|
-
}
|
|
17
|
-
EOF
|
|
18
|
-
|
|
19
|
-
out_const=$(run "$scratch/sample.ts")
|
|
20
|
-
checks=$((checks + 1))
|
|
21
|
-
printf '%s' "$out_const" | grep -Fq 'header_bigo: "O(1) score:5"'
|
|
22
|
-
|
|
23
|
-
cat >"$scratch/nested.ts" <<'EOF'
|
|
24
|
-
export function nestedFn(items: number[]): number {
|
|
25
|
-
let total = 0
|
|
26
|
-
for (let i = 0; i < items.length; i++) {
|
|
27
|
-
for (let j = 0; j < items.length; j++) {
|
|
28
|
-
total += items[i] + items[j]
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
return total
|
|
32
|
-
}
|
|
33
|
-
EOF
|
|
34
|
-
|
|
35
|
-
out_nested=$(run "$scratch/nested.ts")
|
|
36
|
-
checks=$((checks + 1))
|
|
37
|
-
printf '%s' "$out_nested" | grep -Fq 'header_bigo: "O(n^2) score:1"'
|
|
38
|
-
|
|
39
|
-
# #43: a lookup inside a callback on the same line is O(n*m), not O(n).
|
|
40
|
-
cat >"$scratch/inline.ts" <<'EOF'
|
|
41
|
-
export function lookup(items: Item[], ids: string[]) {
|
|
42
|
-
return ids.map(id => items.find(i => i.id === id))
|
|
43
|
-
}
|
|
44
|
-
EOF
|
|
45
|
-
out_inline=$(run "$scratch/inline.ts")
|
|
46
|
-
checks=$((checks + 1))
|
|
47
|
-
printf '%s' "$out_inline" | grep -Fq 'header_bigo: "O(n^2) score:1"' \
|
|
48
|
-
|| { echo "FAIL: map+find on one line must be O(n^2): $out_inline" >&2; exit 1; }
|
|
49
|
-
printf '%s' "$out_inline" | grep -Fq 'lookup_in_loop: 1' \
|
|
50
|
-
|| { echo "FAIL: map+find must count as lookup_in_loop: $out_inline" >&2; exit 1; }
|
|
51
|
-
|
|
52
|
-
# A sequential chain is still one pass per step: O(n), not nested.
|
|
53
|
-
cat >"$scratch/chain.ts" <<'EOF'
|
|
54
|
-
export function active(users: User[]) {
|
|
55
|
-
return users.filter(u => u.active).map(u => u.id)
|
|
56
|
-
}
|
|
57
|
-
EOF
|
|
58
|
-
out_chain=$(run "$scratch/chain.ts")
|
|
59
|
-
checks=$((checks + 1))
|
|
60
|
-
printf '%s' "$out_chain" | grep -Fq 'header_bigo: "O(n) score:3"' \
|
|
61
|
-
|| { echo "FAIL: filter().map() chain must stay O(n): $out_chain" >&2; exit 1; }
|
|
62
|
-
|
|
63
|
-
# A linear search method on its own line inside a loop is also a lookup.
|
|
64
|
-
cat >"$scratch/someinloop.ts" <<'EOF'
|
|
65
|
-
export function anyMatch(a: number[], b: number[]) {
|
|
66
|
-
for (const x of a) {
|
|
67
|
-
if (b.some(y => y === x)) return true
|
|
68
|
-
}
|
|
69
|
-
return false
|
|
70
|
-
}
|
|
71
|
-
EOF
|
|
72
|
-
out_some=$(run "$scratch/someinloop.ts")
|
|
73
|
-
checks=$((checks + 1))
|
|
74
|
-
printf '%s' "$out_some" | grep -Fq 'header_bigo: "O(n^2) score:1"' \
|
|
75
|
-
|| { echo "FAIL: some() inside for must be O(n^2): $out_some" >&2; exit 1; }
|
|
76
|
-
|
|
77
|
-
# #55: a sort chained with reduce on one line is still a sort: O(n log n).
|
|
78
|
-
cat >"$scratch/sortreduce.ts" <<'EOF'
|
|
79
|
-
export function total(xs: number[]) {
|
|
80
|
-
return xs.sort((a, b) => a - b).reduce((s, x) => s + x, 0)
|
|
81
|
-
}
|
|
82
|
-
EOF
|
|
83
|
-
out_sr=$(run "$scratch/sortreduce.ts")
|
|
84
|
-
checks=$((checks + 1))
|
|
85
|
-
printf '%s' "$out_sr" | grep -Fq 'header_bigo: "O(n log n) score:2"' \
|
|
86
|
-
|| { echo "FAIL: sort().reduce() must be O(n log n): $out_sr" >&2; exit 1; }
|
|
87
|
-
printf '%s' "$out_sr" | grep -Fq 'sorts: 1' \
|
|
88
|
-
|| { echo "FAIL: sort().reduce() must count the sort: $out_sr" >&2; exit 1; }
|
|
89
|
-
|
|
90
|
-
# #55: an aliased import suggests the local name (the alias), not the source name.
|
|
91
|
-
printf 'import { Repo as UserRepository, type Id } from "./x"\n' >"$scratch/alias.ts"
|
|
92
|
-
out_alias=$(run "$scratch/alias.ts")
|
|
93
|
-
checks=$((checks + 1))
|
|
94
|
-
printf '%s' "$out_alias" | grep -Fq ' - "UserRepository"' \
|
|
95
|
-
|| { echo "FAIL: aliased import must suggest the alias: $out_alias" >&2; exit 1; }
|
|
96
|
-
if printf '%s' "$out_alias" | grep -Fq ' - "Repo"'; then
|
|
97
|
-
echo "FAIL: aliased import must not suggest the source name: $out_alias" >&2; exit 1
|
|
98
|
-
fi
|
|
99
|
-
|
|
100
|
-
# #43: per-function breakdown. The file keeps its worst value for the header;
|
|
101
|
-
# each named function reports its own estimate.
|
|
102
|
-
function_rows() { # prints "name line bigo" per function
|
|
103
|
-
"$PYTHON_BIN" -I -B -c '
|
|
104
|
-
import re, sys
|
|
105
|
-
text = sys.stdin.read()
|
|
106
|
-
for m in re.finditer(r"- name: \"([^\"]+)\"\n\s+line: (\d+)\n\s+bigo: \"([^\"]+)\"", text):
|
|
107
|
-
print(*m.groups())'
|
|
108
|
-
}
|
|
109
|
-
cat >"$scratch/two.ts" <<'EOF'
|
|
110
|
-
export function constant(x: number[]) {
|
|
111
|
-
const label = "for (;;) { while(true) }"
|
|
112
|
-
return x.length
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export function quadratic(a: number[], b: number[]) {
|
|
116
|
-
for (const i of a) {
|
|
117
|
-
for (const j of b) {
|
|
118
|
-
if (i === j) return true
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
return false
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export const linear = (xs: number[]) => {
|
|
125
|
-
return xs.map(x => x * 2)
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
class Repo {
|
|
129
|
-
sorted(items: Item[]) {
|
|
130
|
-
return items.sort((p, q) => p.id - q.id)
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
EOF
|
|
134
|
-
out_two=$(run "$scratch/two.ts")
|
|
135
|
-
checks=$((checks + 1))
|
|
136
|
-
got=$(printf '%s' "$out_two" | function_rows | tr '\n' ';')
|
|
137
|
-
want='constant 1 O(1);quadratic 6 O(n^2);linear 15 O(n);sorted 20 O(n log n);'
|
|
138
|
-
[[ $got == "$want" ]] || { echo "FAIL: TS functions: got '$got' want '$want'" >&2; exit 1; }
|
|
139
|
-
printf '%s' "$out_two" | grep -Fq 'header_bigo: "O(n^2) score:1"' \
|
|
140
|
-
|| { echo "FAIL: the file keeps its worst value: $out_two" >&2; exit 1; }
|
|
141
|
-
|
|
142
|
-
cat >"$scratch/two.py" <<'EOF'
|
|
143
|
-
def constant(x):
|
|
144
|
-
return len(x)
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
def quadratic(a, b):
|
|
148
|
-
for i in a:
|
|
149
|
-
for j in b:
|
|
150
|
-
pass
|
|
151
|
-
return None
|
|
152
|
-
EOF
|
|
153
|
-
checks=$((checks + 1))
|
|
154
|
-
got=$(run "$scratch/two.py" | function_rows | tr '\n' ';')
|
|
155
|
-
want='constant 1 O(1);quadratic 5 O(n^2);'
|
|
156
|
-
[[ $got == "$want" ]] || { echo "FAIL: Python functions: got '$got' want '$want'" >&2; exit 1; }
|
|
157
|
-
|
|
158
|
-
printf 'package main\n\nfunc (s *Store) Find(ids []int) int {\n\tfor _, id := range ids {\n\t\t_ = id\n\t}\n\treturn 0\n}\n' >"$scratch/store.go"
|
|
159
|
-
checks=$((checks + 1))
|
|
160
|
-
got=$(run "$scratch/store.go" | function_rows | tr '\n' ';')
|
|
161
|
-
[[ $got == 'Find 3 O(n);' ]] || { echo "FAIL: Go method: got '$got'" >&2; exit 1; }
|
|
162
|
-
|
|
163
|
-
# A named function nested in another is measured on its own and not added to the
|
|
164
|
-
# outer one (anonymous callbacks still belong to the function that holds them).
|
|
165
|
-
cat >"$scratch/nested.js" <<'EOF'
|
|
166
|
-
function outer(groups) {
|
|
167
|
-
function inner(ys) {
|
|
168
|
-
for (const y of ys) {
|
|
169
|
-
for (const z of y) {}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
return groups.map(g => g.id)
|
|
173
|
-
}
|
|
174
|
-
EOF
|
|
175
|
-
checks=$((checks + 1))
|
|
176
|
-
got=$(run "$scratch/nested.js" | function_rows | tr '\n' ';')
|
|
177
|
-
[[ $got == 'outer 1 O(n);inner 2 O(n^2);' ]] || { echo "FAIL: nested functions: got '$got'" >&2; exit 1; }
|
|
178
|
-
|
|
179
|
-
printf 'export const noFunctions = 1\n' >"$scratch/none.ts"
|
|
180
|
-
checks=$((checks + 1))
|
|
181
|
-
run "$scratch/none.ts" | grep -Fq ' functions: []' \
|
|
182
|
-
|| { echo "FAIL: a file without functions must print functions: []" >&2; exit 1; }
|
|
183
|
-
|
|
184
|
-
# Ensure usage text references scripts/measure-bigo.py, not .augment/
|
|
185
|
-
checks=$((checks + 1))
|
|
186
|
-
if grep -Fq '.augment/' "$SCRIPT"; then
|
|
187
|
-
echo 'FAIL: measure-bigo.py still references .augment/' >&2
|
|
188
|
-
exit 1
|
|
189
|
-
fi
|
|
190
|
-
|
|
191
|
-
# ADR 0001 equivalence contract: the Python port must reproduce, byte for byte,
|
|
192
|
-
# what the retired measure-bigo.sh printed for these edge cases (CRLF, tabs,
|
|
193
|
-
# multi-line block comments, unterminated strings, invalid UTF-8, empty file,
|
|
194
|
-
# no final newline). Regenerate only for a deliberate behaviour change.
|
|
195
|
-
checks=$((checks + 1))
|
|
196
|
-
golden=$(run tests/fixtures/bigo)
|
|
197
|
-
if [[ "$golden"$'\n' != "$(cat tests/fixtures/bigo.expected.yml)"$'\n' ]]; then
|
|
198
|
-
echo 'FAIL: output differs from tests/fixtures/bigo.expected.yml' >&2
|
|
199
|
-
diff <(printf '%s\n' "$golden") tests/fixtures/bigo.expected.yml >&2 || true
|
|
200
|
-
exit 1
|
|
201
|
-
fi
|
|
202
|
-
|
|
203
|
-
# ADR 0001 portability: no awk, find, sort or bash is needed at run time.
|
|
204
|
-
checks=$((checks + 1))
|
|
205
|
-
py_abs=$("$PYTHON_BIN" -c 'import sys; print(sys.executable)')
|
|
206
|
-
if ! out_bare=$(env -i PATH="$scratch/empty-path" "$py_abs" -I -B "$SCRIPT" "$scratch/nested.ts" 2>&1); then
|
|
207
|
-
echo "FAIL: measure-bigo.py needs external tools: $out_bare" >&2
|
|
208
|
-
exit 1
|
|
209
|
-
fi
|
|
210
|
-
printf '%s' "$out_bare" | grep -Fq 'header_bigo: "O(n^2) score:1"' \
|
|
211
|
-
|| { echo "FAIL: empty PATH run: $out_bare" >&2; exit 1; }
|
|
212
|
-
|
|
213
|
-
# Directory scans skip node_modules/dist/.git/coverage and symlinks, sorted.
|
|
214
|
-
checks=$((checks + 1))
|
|
215
|
-
mkdir -p "$scratch/tree/node_modules" "$scratch/tree/dist" "$scratch/tree/b" "$scratch/tree/a"
|
|
216
|
-
for f in tree/node_modules/x.ts tree/dist/y.js tree/b/z.ts tree/a/w.py tree/a/readme.md; do echo 'x = 1' > "$scratch/$f"; done
|
|
217
|
-
ln -s ../b/z.ts "$scratch/tree/a/link.ts"
|
|
218
|
-
files=$(run "$scratch/tree" | grep '^ - file: ' | grep -o '"[^"]*"$' | tr '\n' ' ')
|
|
219
|
-
[[ $files == "\"$scratch/tree/a/w.py\" \"$scratch/tree/b/z.ts\" " ]] \
|
|
220
|
-
|| { echo "FAIL: directory scan: $files" >&2; exit 1; }
|
|
221
|
-
|
|
222
|
-
printf 'measure-bigo.test.sh: PASS %d checks\n' "$checks"
|