devkb 0.2.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.
Files changed (61) hide show
  1. devkb/__init__.py +11 -0
  2. devkb/audit.py +173 -0
  3. devkb/bench/add_packs.py +52 -0
  4. devkb/bench/cli_tasks.py +105 -0
  5. devkb/bench/confirm_tasks.py +149 -0
  6. devkb/bench/module_truth.py +249 -0
  7. devkb/bench/oco_tasks.py +216 -0
  8. devkb/bench/pipeline.py +313 -0
  9. devkb/bench/process_tasks.py +135 -0
  10. devkb/bench/report.py +74 -0
  11. devkb/bench/run.py +174 -0
  12. devkb/bench/truth_check.py +148 -0
  13. devkb/bench/workflow_report.py +64 -0
  14. devkb/bench/workflow_run.py +206 -0
  15. devkb/bench/workflow_tasks.py +227 -0
  16. devkb/build.py +258 -0
  17. devkb/build_symbols.py +1394 -0
  18. devkb/call_recall.py +159 -0
  19. devkb/cli.py +53 -0
  20. devkb/coverage.py +155 -0
  21. devkb/edge_truth.py +272 -0
  22. devkb/extractors/backlog.py +113 -0
  23. devkb/extractors/component_subsystem.py +116 -0
  24. devkb/extractors/configuration.py +267 -0
  25. devkb/extractors/files_tests.py +220 -0
  26. devkb/extractors/flows.py +117 -0
  27. devkb/extractors/lifecycle.py +258 -0
  28. devkb/extractors/members.py +160 -0
  29. devkb/extractors/pull_requests.py +271 -0
  30. devkb/extractors/releases.py +49 -0
  31. devkb/extractors/source_baseline.py +66 -0
  32. devkb/extractors/spec_docs.py +220 -0
  33. devkb/extractors/symbol_tests.py +131 -0
  34. devkb/extractors/test_inventory.py +38 -0
  35. devkb/extractors/ticket_similarity.py +193 -0
  36. devkb/extractors/tickets_git.py +151 -0
  37. devkb/extractors/warnings.py +230 -0
  38. devkb/extractors/work_units.py +90 -0
  39. devkb/github.py +515 -0
  40. devkb/gitutil.py +16 -0
  41. devkb/index.py +51 -0
  42. devkb/init.py +178 -0
  43. devkb/mcp_server.py +216 -0
  44. devkb/model.py +221 -0
  45. devkb/module_file_bridge.py +57 -0
  46. devkb/ontology.md +178 -0
  47. devkb/packs.py +462 -0
  48. devkb/process_api.py +1601 -0
  49. devkb/project_context.py +191 -0
  50. devkb/query.py +251 -0
  51. devkb/service.py +324 -0
  52. devkb/startup.py +103 -0
  53. devkb/test_discovery.py +76 -0
  54. devkb/ts_symbols.py +291 -0
  55. devkb-0.2.0.dist-info/METADATA +162 -0
  56. devkb-0.2.0.dist-info/RECORD +61 -0
  57. devkb-0.2.0.dist-info/WHEEL +5 -0
  58. devkb-0.2.0.dist-info/entry_points.txt +2 -0
  59. devkb-0.2.0.dist-info/licenses/LICENSE +202 -0
  60. devkb-0.2.0.dist-info/licenses/NOTICE +11 -0
  61. devkb-0.2.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,249 @@
1
+ """Import-path call truth for the confirmatory study (ODSF-315, PREREGISTRATION.md deviation D1).
2
+
3
+ The exploratory truth (workflow_tasks.callers_of) matches a call by the called NAME and keeps only names defined once
4
+ in the repository. Public FastAPI projects define `get`, `create` and `update` in dozens of `service.py` modules, so
5
+ on Netflix Dispatch that rule left one usable task. This truth resolves a call through the caller file's imports to
6
+ the MODULE that defines the function, so equal names in different modules never meet:
7
+
8
+ from dispatch.incident import service -> service.get(...) -> dispatch/incident/service.py::get
9
+ from dispatch.incident.service import get -> get(...) -> dispatch/incident/service.py::get
10
+ import dispatch.incident.service as svc -> svc.get(...) -> dispatch/incident/service.py::get
11
+ a call to a function defined in the same file, by its bare name
12
+
13
+ Targets are module-level functions, and methods called as `self.x()` / `cls.x()` from their own class in the same
14
+ file. A call made through any other object (`store.x()`), or reaching a method through a base class, is not
15
+ attributed: the truth is conservative, so a task is built only on functions whose callers it can see completely (no
16
+ method on the path except a route handler, whose own callers in its class are followed).
17
+
18
+ A name imported from a package that re-exports it (`from lnbits.core.crud import get_wallet`, with
19
+ `from .wallets import get_wallet` in `crud/__init__.py`) is followed to the module that defines it. A call made outside
20
+ every function (`app_dirs = get_app_dirs()` at import time, or in a class body) has the caller `<module>`: it counts
21
+ for the files a signature change breaks, and is not a function on a call chain.
22
+
23
+ A raised message is the first positional string argument, or a `detail=` / `message=` / `msg=` / `description=`
24
+ keyword (`HTTPException(status_code=422, detail="...")`).
25
+ """
26
+ from __future__ import annotations
27
+
28
+ import ast
29
+ import collections
30
+ import re
31
+ import subprocess
32
+ from pathlib import Path
33
+
34
+ ROUTE_VERBS = {"get", "post", "put", "delete", "patch"}
35
+ RAISE_KEYWORDS = ("detail", "message", "msg", "description")
36
+ MODULE = "<module>"
37
+ _TEST = re.compile(r"(^|/)(tests?/|test_[^/]*\.py$|[^/]*_test\.py$|conftest\.py$)")
38
+
39
+
40
+ def is_test(path: str) -> bool:
41
+ return bool(_TEST.search(path))
42
+
43
+
44
+ class _File(ast.NodeVisitor):
45
+ def __init__(self, rel: str) -> None:
46
+ self.rel = rel
47
+ self.stack: list[str] = []
48
+ self.fn: list[str] = []
49
+ self.functions: set[str] = set() # module-level function names
50
+ self.defs: dict[str, tuple[int, bool]] = {} # qualname -> (line, is_route)
51
+ self.calls: dict[str, set[tuple[str, str]]] = collections.defaultdict(set) # caller qual -> {(base, name)}
52
+ self.bind_mod: dict[str, str] = {} # local -> dotted module
53
+ self.bind_obj: dict[str, tuple[str, str]] = {} # local -> (dotted module, name)
54
+ self.raises: list[tuple[str, str]] = []
55
+ self.classes: list[str] = [] # qualnames of the enclosing classes, innermost last
56
+
57
+ def visit_Import(self, node):
58
+ for a in node.names:
59
+ if a.asname:
60
+ self.bind_mod[a.asname] = a.name
61
+ else:
62
+ self.bind_mod[a.name.split(".")[0]] = a.name.split(".")[0]
63
+
64
+ def visit_ImportFrom(self, node):
65
+ if node.level:
66
+ parts = self.rel[:-3].split("/")[:-1]
67
+ base = parts[: len(parts) - (node.level - 1)] if node.level - 1 <= len(parts) else []
68
+ mod = ".".join(base + ([node.module] if node.module else []))
69
+ else:
70
+ mod = node.module or ""
71
+ for a in node.names:
72
+ if a.name != "*":
73
+ self.bind_obj[a.asname or a.name] = (mod, a.name)
74
+
75
+ def visit_ClassDef(self, node):
76
+ self.stack.append(node.name)
77
+ self.classes.append(".".join(self.stack))
78
+ self.generic_visit(node)
79
+ self.classes.pop()
80
+ self.stack.pop()
81
+
82
+ def _fn(self, node):
83
+ qual = ".".join(self.stack + [node.name])
84
+ if not self.stack:
85
+ self.functions.add(node.name)
86
+ is_route = any(isinstance(d, ast.Call) and isinstance(d.func, ast.Attribute) and d.func.attr in ROUTE_VERBS
87
+ for d in node.decorator_list)
88
+ self.defs[qual] = (node.lineno, is_route)
89
+ self.stack.append(node.name)
90
+ self.fn.append(qual)
91
+ self.generic_visit(node)
92
+ self.fn.pop()
93
+ self.stack.pop()
94
+
95
+ visit_FunctionDef = visit_AsyncFunctionDef = _fn
96
+
97
+ def visit_Call(self, node):
98
+ caller = self.fn[-1] if self.fn else MODULE
99
+ f = node.func
100
+ if isinstance(f, ast.Name):
101
+ self.calls[caller].add(("", f.id))
102
+ elif isinstance(f, ast.Attribute) and isinstance(f.value, ast.Name):
103
+ if f.value.id in ("self", "cls") and self.classes:
104
+ if self.fn:
105
+ self.calls[caller].add((f"self:{self.classes[-1]}", f.attr)) # the innermost enclosing class
106
+ else:
107
+ self.calls[caller].add((f.value.id, f.attr))
108
+ elif isinstance(f, ast.Attribute): # `prefect.context.get_run_context()` after `import prefect.context`
109
+ parts, v = [], f.value
110
+ while isinstance(v, ast.Attribute):
111
+ parts.insert(0, v.attr)
112
+ v = v.value
113
+ if isinstance(v, ast.Name) and v.id not in ("self", "cls"):
114
+ self.calls[caller].add((".".join([v.id, *parts]), f.attr))
115
+ self.generic_visit(node)
116
+
117
+ def visit_Lambda(self, node): # a lambda's body runs when it is called, not at import: no module-level call
118
+ if self.fn:
119
+ self.generic_visit(node)
120
+
121
+ def visit_Raise(self, node):
122
+ if self.fn and isinstance(node.exc, ast.Call):
123
+ cands = node.exc.args[:1] + [k.value for k in node.exc.keywords if k.arg in RAISE_KEYWORDS]
124
+ a = next((c for c in cands if isinstance(c, ast.Constant) and isinstance(c.value, str)), None)
125
+ if a is not None:
126
+ self.raises.append((self.fn[-1], a.value))
127
+ self.generic_visit(node)
128
+
129
+
130
+ class Truth:
131
+ def __init__(self, root: Path) -> None:
132
+ self.root = root
133
+ self.files: dict[str, _File] = {}
134
+ listed = subprocess.run(["git", "ls-files", "--recurse-submodules", "--", "*.py"], cwd=root,
135
+ capture_output=True, text=True).stdout.split()
136
+ for rel in listed:
137
+ # migrations and alembic versions are callers too: a signature change breaks them like any other file
138
+ if re.search(r"(^|/)(\.impl-worktrees|node_modules|\.claude|_bmad)/", rel):
139
+ continue
140
+ try:
141
+ v = _File(rel)
142
+ v.visit(ast.parse((root / rel).read_text(errors="replace")))
143
+ except (SyntaxError, ValueError, OSError, RecursionError):
144
+ continue
145
+ self.files[rel] = v
146
+ self.suffix: dict[str, list[str]] = collections.defaultdict(list)
147
+ for rel in self.files:
148
+ mod = rel[:-3].replace("/", ".")
149
+ if mod.endswith(".__init__"):
150
+ mod = mod[: -len(".__init__")]
151
+ parts = mod.split(".")
152
+ for i in range(len(parts)):
153
+ self.suffix[".".join(parts[i:])].append(rel)
154
+ self._callers: dict[tuple[str, str], set[tuple[str, str]]] | None = None
155
+
156
+ def module_file(self, dotted: str) -> str | None:
157
+ """The one file a dotted module names, by its longest unique path suffix."""
158
+ hits = self.suffix.get(dotted, [])
159
+ return hits[0] if len(hits) == 1 else None
160
+
161
+ def _defined(self, target: str | None, name: str, depth: int = 0) -> tuple[str, str] | None:
162
+ """(file, name) where `name`, looked up in module file `target`, is defined: there, or through the import that
163
+ makes it importable from there (a package's `__init__.py` re-export), a bounded number of hops."""
164
+ if target is None:
165
+ return None
166
+ if name in self.files[target].functions:
167
+ return (target, name)
168
+ via = self.files[target].bind_obj.get(name)
169
+ if via and depth < 6:
170
+ return self._defined(self.module_file(via[0]), via[1], depth + 1)
171
+ return None
172
+
173
+ def _target(self, rel: str, base: str, name: str) -> tuple[str, str] | None:
174
+ f = self.files[rel]
175
+ if base.startswith("self:"):
176
+ qual = f"{base[5:]}.{name}"
177
+ return (rel, qual) if qual in f.defs else None
178
+ if not base:
179
+ if name in f.bind_obj:
180
+ mod, orig = f.bind_obj[name]
181
+ return self._defined(self.module_file(mod), orig)
182
+ return (rel, name) if name in f.functions else None
183
+ if "." in base: # a module path: its root must be an imported module
184
+ root, rest = base.split(".", 1)
185
+ if root in f.bind_mod:
186
+ return self._defined(self.module_file(f"{f.bind_mod[root]}.{rest}"), name)
187
+ if root in f.bind_obj:
188
+ mod, orig = f.bind_obj[root]
189
+ return self._defined(self.module_file(".".join(x for x in (mod, orig, rest) if x)), name)
190
+ return None
191
+ dotted = f.bind_mod.get(base)
192
+ if dotted is None and base in f.bind_obj:
193
+ mod, orig = f.bind_obj[base]
194
+ dotted = f"{mod}.{orig}" if mod else orig
195
+ if dotted is None:
196
+ return None
197
+ return self._defined(self.module_file(dotted), name)
198
+
199
+ def callers(self) -> dict[tuple[str, str], set[tuple[str, str]]]:
200
+ if self._callers is None:
201
+ out: dict[tuple[str, str], set[tuple[str, str]]] = collections.defaultdict(set)
202
+ for rel, f in self.files.items():
203
+ for caller, calls in f.calls.items():
204
+ for base, name in calls:
205
+ t = self._target(rel, base, name)
206
+ if t and t != (rel, caller):
207
+ out[t].add((rel, caller))
208
+ self._callers = out
209
+ return self._callers
210
+
211
+ def is_route(self, key: tuple[str, str]) -> bool:
212
+ return self.files[key[0]].defs.get(key[1], (0, False))[1]
213
+
214
+ def is_function(self, key: tuple[str, str]) -> bool:
215
+ return "." not in key[1] and key[1] in self.files[key[0]].functions
216
+
217
+ def up(self, start: tuple[str, str], max_depth: int = 6) -> tuple[set, set, bool]:
218
+ """(production callers, test callers, clean). Clean when every production caller on the way is a module-level
219
+ function whose own callers the truth sees, or a route handler, where a path ends."""
220
+ callers = self.callers()
221
+ prod, tests, clean = set(), set(), True
222
+ frontier = [start]
223
+ for _ in range(max_depth):
224
+ nxt = []
225
+ for k in frontier:
226
+ for c in callers.get(k, ()):
227
+ if c in prod or c in tests or c[1] == MODULE:
228
+ continue # an import-time call is no function on a chain
229
+ if is_test(c[0]):
230
+ tests.add(c)
231
+ continue
232
+ prod.add(c)
233
+ if self.is_route(c):
234
+ nxt.append(c) # a handler may itself be called by another handler of its class
235
+ continue
236
+ if not self.is_function(c):
237
+ clean = False
238
+ continue
239
+ nxt.append(c)
240
+ frontier = nxt
241
+ if not frontier:
242
+ break
243
+ else:
244
+ clean = clean and not frontier
245
+ return prod, tests, clean
246
+
247
+
248
+ def short(key: tuple[str, str]) -> str:
249
+ return f"{key[0]}::{key[1].split('.')[-1]}"
@@ -0,0 +1,216 @@
1
+ #!/usr/bin/env python3
2
+ """Multi-hop workflow tasks on a large repository (ODSF-310, run on OCO): ground truth from source, never from devkb.
3
+
4
+ The ODSF workflow benchmark (docs/workflow-benchmark-odsf310.md) asked one-hop questions that `git grep` answers in a
5
+ few turns. These ask what a developer needs before a change in a large codebase, where the answer is a closure:
6
+
7
+ err_to_handler an error message raised once, in non-route code: the raiser, every HTTP route handler that can
8
+ reach it through calls at any depth, and the test files that call any function on those paths
9
+ impact a function reachable from three or more route handlers: those handlers, and the tests to run
10
+ wide_refactor a function called from 8 to 25 files: the caller files, the test files, and the files the role
11
+ may not edit
12
+
13
+ Calls are resolved by workflow_tasks.callers_of (through imports; a name a library type shares is never matched by
14
+ name alone). A task is kept only when every function on its paths has a name defined once in the repository, so no
15
+ step in the closure depends on a guess.
16
+
17
+ python3 devkb/bench/oco_tasks.py CHECKOUT OUT.json
18
+ """
19
+ from __future__ import annotations
20
+
21
+ import ast
22
+ import collections
23
+ import json
24
+ import random
25
+ import sys
26
+ from pathlib import Path
27
+
28
+ sys.path.insert(0, str(Path(__file__).resolve().parent))
29
+ import workflow_tasks as wt # noqa: E402
30
+
31
+ ROUTE_VERBS = {"get", "post", "put", "delete", "patch"}
32
+ MAX_DEPTH = 6
33
+
34
+
35
+ def handlers(root: Path, s: dict) -> set[tuple[str, str]]:
36
+ """Functions decorated `@<something>.get(...)` and the like: FastAPI route handlers."""
37
+ out = set()
38
+ for rel in {k[0] for k in s["calls"]}:
39
+ try:
40
+ tree = ast.parse((root / rel).read_text(errors="replace"))
41
+ except (SyntaxError, ValueError, OSError):
42
+ continue
43
+ for node in ast.walk(tree):
44
+ if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
45
+ for d in node.decorator_list:
46
+ if isinstance(d, ast.Call) and isinstance(d.func, ast.Attribute) and d.func.attr in ROUTE_VERBS:
47
+ out.add((rel, node.name))
48
+ return out
49
+
50
+
51
+ def up_closure(s: dict, unique: dict, start: str) -> tuple[set, set, bool]:
52
+ """Every function that reaches `start` through calls, and whether the walk stayed unambiguous."""
53
+ seen: set = set()
54
+ frontier, clean = [start], True
55
+ for _ in range(MAX_DEPTH):
56
+ nxt = []
57
+ for name in frontier:
58
+ for caller in wt.callers_of(s, name):
59
+ if caller in seen:
60
+ continue
61
+ seen.add(caller)
62
+ if wt.is_test_file(caller[0]):
63
+ continue # a test ends a path; it is not a route to anything
64
+ cname = caller[1].split(".")[-1]
65
+ if unique.get(cname) != caller:
66
+ clean = False # its own callers cannot be found by name without guessing
67
+ continue
68
+ nxt.append(cname)
69
+ frontier = nxt
70
+ if not frontier:
71
+ break
72
+ else:
73
+ clean = clean and not frontier
74
+ tests = {k for k in seen if wt.is_test_file(k[0])}
75
+ return seen - tests, tests, clean
76
+
77
+
78
+ def build(root: Path, seed: int = 311, per_kind: int = 2, exclude: set[str] | None = None,
79
+ impact_prefixes: tuple[str, ...] | None = ("webapp/backend/app/services", "webapp/backend/app/", "scripts/"),
80
+ ) -> list[dict]:
81
+ """The exploratory set used the defaults. The confirmatory study (ODSF-315, PREREGISTRATION.md) passes a new seed,
82
+ four tasks per kind, the exploratory names as `exclude`, and `impact_prefixes=None` (any non-test file)."""
83
+ exclude = set(exclude or ())
84
+ s = wt.scan(root)
85
+ unique = {n: ks[0] for n, ks in s["defs"].items() if len(ks) == 1 and not n.startswith("test_") and not n.startswith("__")}
86
+ routes = handlers(root, s)
87
+ route_names = {(r, q.split(".")[-1]) for r, q in routes}
88
+ rnd = random.Random(seed)
89
+ tasks: list[dict] = []
90
+
91
+ def reach(name):
92
+ prod, tests, clean = up_closure(s, unique, name)
93
+ hit = sorted(wt.short(k) for k in prod if (k[0], k[1].split(".")[-1]) in route_names)
94
+ test_files = sorted({k[0] for k in tests} | {c[0] for c in wt.callers_of(s, name) if wt.is_test_file(c[0])})
95
+ return prod, hit, test_files, clean
96
+
97
+ used: set[str] = set(exclude)
98
+ counts = collections.Counter(msg for _, msg in s["raises"])
99
+ errs = []
100
+ for key, msg in s["raises"]:
101
+ name = key[1].split(".")[-1]
102
+ if name in used or counts[msg] != 1 or len(msg.split()) < 4 or "{" in msg or unique.get(name) != key or wt.is_test_file(key[0]):
103
+ continue
104
+ if (key[0], name) in route_names:
105
+ continue # raised in the handler itself is one hop, not a trace
106
+ prod, hit, tests, clean = reach(name)
107
+ if clean and 1 <= len(hit) <= 12 and tests and len(prod) >= 2:
108
+ errs.append((key, msg, hit, tests, len(prod)))
109
+ # every clean chain in OCO reaches one handler; the deepest chains are the hardest traces
110
+ errs.sort(key=lambda e: (-e[4], -len(e[3]), e[0]))
111
+ for key, msg, hit, tests, n in errs[:per_kind]:
112
+ name = key[1].split(".")[-1]
113
+ used.add(name)
114
+ tasks.append({"id": f"ERR-{name}", "stage": "RCA", "kind": "err_to_handler", "skill": "devkb-rca",
115
+ "prompt": (f'Users of the web API get an error whose message is: "{msg}". Trace it: name the function that raises it, '
116
+ "every HTTP route handler function (a function decorated as a FastAPI route) that can reach that function "
117
+ "through calls at any depth, and every test file that calls the raising function or any function on a path "
118
+ "between it and those handlers. Answer with one JSON object: "
119
+ '{"raiser": "path::function", "handlers": ["path::function", ...], "test_files": ["path", ...]}.'),
120
+ "truth": {"raiser": wt.short(key), "handlers": hit, "test_files": tests, "closure_size": n}})
121
+
122
+ imp = []
123
+ for name, key in unique.items():
124
+ if name in used or wt.is_test_file(key[0]) or (key[0], name) in route_names or len(name) < 8:
125
+ continue
126
+ if impact_prefixes and not key[0].startswith(impact_prefixes):
127
+ continue
128
+ prod, hit, tests, clean = reach(name)
129
+ if clean and 3 <= len(hit) <= 15 and tests and len(prod) >= 4:
130
+ imp.append((name, key, hit, tests, len(prod)))
131
+ rnd.shuffle(imp)
132
+ for name, key, hit, tests, n in imp[:per_kind]:
133
+ used.add(name)
134
+ tasks.append({"id": f"IMP-{name}", "stage": "IMPACT", "kind": "impact", "skill": "devkb-feature",
135
+ "prompt": (f"You are about to change the behaviour of `{name}` (defined in {key[0]}). Before editing, find which HTTP route "
136
+ "handler functions (functions decorated as FastAPI routes) can reach it through calls at any depth, and which "
137
+ "test files you must run: every test file that calls it or any function on a path between it and those handlers. "
138
+ 'Answer with one JSON object: {"handlers": ["path::function", ...], "test_files": ["path", ...]}.'),
139
+ "truth": {"handlers": hit, "test_files": tests, "closure_size": n}})
140
+
141
+ roles = wt.dnt(root)
142
+ wide = []
143
+ for name, key in unique.items():
144
+ if name in used or wt.is_test_file(key[0]) or len(name) < 8:
145
+ continue
146
+ files = sorted({c[0] for c in wt.callers_of(s, name)})
147
+ if not (8 <= len(files) <= 25 and any(wt.is_test_file(f) for f in files)):
148
+ continue
149
+ for role, prefixes in sorted(roles.items()):
150
+ blocked = sorted(f for f in files if any(f.startswith(p) for p in prefixes))
151
+ if 1 <= len(blocked) < len(files) and not any(key[0].startswith(p) for p in prefixes):
152
+ wide.append((name, key, files, role, blocked))
153
+ break
154
+ if len(wide) < per_kind and roles:
155
+ # In OCO no role is barred from part of a wide function's callers without also being barred from the function
156
+ # itself, so the widest functions are scoped by a role that may edit them: the right boundary answer is [].
157
+ extra = []
158
+ for name, key in unique.items():
159
+ if name in used or name in {w[0] for w in wide} or wt.is_test_file(key[0]) or len(name) < 8:
160
+ continue
161
+ files = sorted({c[0] for c in wt.callers_of(s, name)})
162
+ if 12 <= len(files) <= 25 and 0 < sum(wt.is_test_file(f) for f in files) < len(files):
163
+ role = next((r for r, p in sorted(roles.items(), key=lambda rp: -len(rp[1]))
164
+ if p and not any(key[0].startswith(x) for x in p)), "TL")
165
+ extra.append((len(files), name, key, files, role,
166
+ sorted(f for f in files if any(f.startswith(x) for x in roles.get(role, [])))))
167
+ extra.sort(key=lambda e: -e[0])
168
+ wide += [e[1:] for e in extra[: per_kind - len(wide)]]
169
+ if not roles:
170
+ # A repository with no role profiles (a public project): the same refactor, without the boundary question.
171
+ cand = []
172
+ for name, key in unique.items():
173
+ if name in used or wt.is_test_file(key[0]) or len(name) < 8:
174
+ continue
175
+ files = sorted({c[0] for c in wt.callers_of(s, name)})
176
+ if 8 <= len(files) <= 25 and 0 < sum(wt.is_test_file(f) for f in files) < len(files):
177
+ cand.append((name, key, files))
178
+ rnd.shuffle(cand)
179
+ for name, key, files in cand[:per_kind]:
180
+ tasks.append({"id": f"WIDE-{name}", "stage": "REFACTOR", "kind": "refactor", "skill": "devkb-refactor",
181
+ "prompt": (f"You will change the signature of `{name}` (defined in {key[0]}). Before editing, scope the change: "
182
+ "list every file that contains a direct caller (tests included), and the test files among them. "
183
+ 'Answer with one JSON object: {"caller_files": [...], "test_files": [...]}.'),
184
+ "truth": {"caller_files": files, "test_files": [f for f in files if wt.is_test_file(f)]}})
185
+ return tasks
186
+ rnd.shuffle(wide)
187
+ for name, key, files, role, blocked in wide[:per_kind]:
188
+ tasks.append({"id": f"WIDE-{name}", "stage": "REFACTOR", "kind": "refactor", "skill": "devkb-refactor",
189
+ "prompt": (f"You work as role {role} and will change the signature of `{name}` (defined in {key[0]}). Before editing, scope the "
190
+ "change: list every file that contains a direct caller (tests included), the test files among them, and which of "
191
+ f"those files role {role} must not edit according to the DO NOT TOUCH block of its profile in sdp/process/roles. "
192
+ 'Answer with one JSON object: {"caller_files": [...], "test_files": [...], "blocked_files": [...]}.'),
193
+ "truth": {"caller_files": files, "test_files": [f for f in files if wt.is_test_file(f)], "blocked_files": blocked}})
194
+ return tasks
195
+
196
+
197
+ if __name__ == "__main__":
198
+ import argparse
199
+ ap = argparse.ArgumentParser()
200
+ ap.add_argument("checkout")
201
+ ap.add_argument("out")
202
+ ap.add_argument("--seed", type=int, default=311)
203
+ ap.add_argument("--per-kind", type=int, default=2)
204
+ ap.add_argument("--exclude", help="a JSON list of function names already used")
205
+ ap.add_argument("--any-path", action="store_true", help="draw impact tasks from any non-test file")
206
+ ap.add_argument("--prefix", default="", help="prefix task ids, e.g. the repository id")
207
+ a = ap.parse_args()
208
+ t = build(Path(a.checkout), seed=a.seed, per_kind=a.per_kind,
209
+ exclude=set(json.load(open(a.exclude))) if a.exclude else None,
210
+ impact_prefixes=None if a.any_path else ("webapp/backend/app/services", "webapp/backend/app/", "scripts/"))
211
+ for x in t:
212
+ x["id"] = a.prefix + x["id"]
213
+ sys.argv = [sys.argv[0], a.checkout, a.out]
214
+ json.dump(t, open(sys.argv[2], "w"), indent=1)
215
+ for x in t:
216
+ print(x["id"], {k: (len(v) if isinstance(v, (list, dict)) else v) for k, v in x["truth"].items()})