65 lines
3.0 KiB
Python
Executable File
65 lines
3.0 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Recipe 6: compact API context of the touched area, sized for an LLM reviewer brief.
|
|
|
|
Usage: context.py <env-file> <defdiff.json> [--budget BYTES] [--all-files]
|
|
Per changed file (visible to orbit), one line per type/callable at tip with its declaration text
|
|
read from the source (more reliable than repo-map's extractor), change markers from defdiff
|
|
(+ added, S signature, ~ body, > moved), untouched items collapsed when over budget.
|
|
Compare: `orbit repo-map api <dir>` (directory prefix only; a file path returns nothing).
|
|
"""
|
|
import collections, json, re, sys
|
|
sys.path.insert(0, __file__.rsplit('/', 1)[0])
|
|
from ob import load_env, q
|
|
|
|
env = load_env(sys.argv[1]); dd = json.load(open(sys.argv[2]))
|
|
DB, TW = env['DB'], env['TIP_WT']
|
|
BUDGET = int(sys.argv[sys.argv.index('--budget') + 1]) if '--budget' in sys.argv else 8000
|
|
SHOW = {'Struct', 'Enum', 'Trait', 'TypeAlias', 'Function', 'Method', 'AssociatedFunction', 'Constant', 'Static', 'Macro'}
|
|
|
|
ops = {r.get('tid'): r['op'] for r in dd['rows'] if r.get('tid')}
|
|
changed = [f for f in dd['changed_files'] if f.endswith('.rs')]
|
|
defs = collections.defaultdict(list)
|
|
for d in q("SELECT id, file_path f, fqn, name, definition_type k, start_line s, end_line e FROM gl_definition WHERE file_path LIKE '%.rs'", repo=TW, db=DB):
|
|
if d['f'] in changed and d['k'] in SHOW: defs[d['f']].append(d)
|
|
|
|
def decl(lines, d):
|
|
out = []
|
|
for l in lines[d['s'] - 1:d['e']]:
|
|
t = l.strip()
|
|
if not out and (not t or t.startswith(('///', '//', '#[', '#!'))): continue
|
|
out.append(t)
|
|
if '{' in t or t.endswith(';') or len(out) > 8: break
|
|
s = re.sub(r'\s+', ' ', ' '.join(out)).split('{')[0].strip()
|
|
s = re.sub(r'\(\s+', '(', s); s = re.sub(r',\s*\)', ')', s)
|
|
return s[:160]
|
|
|
|
blocks = []
|
|
for f in changed:
|
|
ds = sorted(defs.get(f, []), key=lambda d: d['s'])
|
|
if not ds: continue
|
|
try: lines = open(f"{TW}/{f}", errors='replace').read().split('\n')
|
|
except FileNotFoundError: continue
|
|
body = []
|
|
nchg = 0
|
|
for d in ds:
|
|
op = ops.get(d['id'], ' ')
|
|
nchg += op != ' '
|
|
ind = ' ' if d['k'] in ('Method', 'AssociatedFunction') else ''
|
|
body.append((op, f"{op} {ind}{decl(lines, d)} L{d['s']}"))
|
|
blocks.append((f, nchg, body))
|
|
|
|
out = []; size = 0
|
|
is_test = lambda f: '/tests/' in f or f.endswith('_test.rs') or f.endswith('tests.rs')
|
|
blocks.sort(key=lambda b: (is_test(b[0]), -b[1]))
|
|
for f, nchg, body in blocks:
|
|
head = f"## {f} ({nchg} changed / {len(body)} items)"
|
|
full = '\n'.join([head] + [x for _, x in body])
|
|
unchanged = sum(1 for op, _ in body if op == ' ')
|
|
slim = '\n'.join([head] + [x for op, x in body if op != ' '] + ([f" … {unchanged} unchanged items (orbit grep --path {f})"] if unchanged else []))
|
|
txt = full if len(full) <= 2000 and size + len(full) <= BUDGET else slim
|
|
if size + len(txt) > BUDGET:
|
|
out.append(f"## {f} ({nchg} changed) — omitted (budget)"); continue
|
|
out.append(txt); size += len(txt)
|
|
print(f"# context: {len(blocks)} visible changed files, budget {BUDGET}B")
|
|
print('\n'.join(out))
|