From b10dab2f5ad83f642a03b5f800dea71651830beb Mon Sep 17 00:00:00 2001 From: Morgan Lunt Date: Thu, 24 Sep 2026 18:26:30 -0700 Subject: code-modernization: extract-rules uses a shard builder and a rules renderer that ship with the plugin The command used to carry a 35-line shard-building script inline and asked the model to write a rules renderer on every run, so the shard list and the BUSINESS_RULES.md layout varied from run to run and could not be tested. scripts/make_shards.py builds the shard list from topology.json when the map has run, and from the directory tree when it has not (skipping vendored, generated and test-fixture directories, one shard per directory, split at 25 files or about 5,000 lines). scripts/render_rules.py turns the workflow's result into BUSINESS_RULES.md and DATA_OBJECTS.md with RULE-NNN headings, the SME section and the coverage gaps, and writes every value from the analysis as plain text. The workflow takes an optional sourceDir, so the code can live outside the workspace, and shard files are kept relative to it. The command drops from 305 to about 150 lines. --- plugins/code-modernization/scripts/render_rules.py | 168 +++++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 plugins/code-modernization/scripts/render_rules.py (limited to 'plugins/code-modernization/scripts/render_rules.py') diff --git a/plugins/code-modernization/scripts/render_rules.py b/plugins/code-modernization/scripts/render_rules.py new file mode 100644 index 00000000..da375f55 --- /dev/null +++ b/plugins/code-modernization/scripts/render_rules.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +"""Render the extract-rules workflow's result into BUSINESS_RULES.md and DATA_OBJECTS.md. + + python3 render_rules.py [--workspace DIR] [--result FILE] + +Reads analysis//rules_result.json (the object the workflow returned, saved by the +command) and writes analysis//BUSINESS_RULES.md and DATA_OBJECTS.md. Rules are +numbered RULE-001, RULE-002, ... in priority then category order and the heading is always +`### RULE-NNN: `, the pattern later commands and the pane look for. Every value comes +from analysis of untrusted code, so it is written as plain text on one line where it is +a heading or a table cell. Standard library only. +""" +import json +import os +import sys + +CATEGORIES = ['Calculation', 'Validation', 'Lifecycle', 'Policy'] +PRIORITY = {'P0': 0, 'P1': 1, 'P2': 2} + + +def one_line(value, limit=300): + text = ' '.join(str(value if value is not None else '').split()) + text = text.replace('|', '/').replace('`', "'") + return text if len(text) <= limit else text[: limit - 1].rstrip() + '…' + + +def block(value): + """Multi-line text: keep the lines, but never let a value open a heading or a fence.""" + lines = [] + for raw in str(value if value is not None else '').splitlines(): + line = raw.rstrip() + if line.lstrip().startswith(('#', '```', '~~~')): + line = '\\' + line.lstrip() + lines.append(line) + return '\n'.join(lines).strip() + + +def render_rule(number, rule): + rid = f'RULE-{number:03d}' + out = [f"### {rid}: {one_line(rule.get('name'), 120)}", + f"**Category:** {one_line(rule.get('category'), 20)}", + f"**Priority:** {one_line(rule.get('priority'), 4)}", + f"**Source:** `{one_line(rule.get('source'), 200).replace(chr(39), '')}`", + f"**Plain English:** {one_line(rule.get('plainEnglish'), 400)}", + '**Specification:**', + f" Given {block(rule.get('given'))}", + f" When {block(rule.get('when'))}", + f" Then {block(rule.get('then'))}"] + if rule.get('and'): + out.append(f" And {block(rule['and'])}") + if rule.get('parameters'): + out.append(f"**Parameters:** {block(rule['parameters'])}") + cases = rule.get('edgeCases') or [] + if cases: + out.append('**Edge cases handled:** ' + '; '.join(one_line(c, 200) for c in cases)) + if rule.get('suspectedDefect'): + out.append(f"**Suspected defect:** {block(rule['suspectedDefect'])}") + confidence = one_line(rule.get('confidence'), 10) + question = rule.get('smeQuestion') + out.append(f"**Confidence:** {confidence}" + (f" — {one_line(question, 400)}" if question and confidence != 'High' else '')) + return rid, '\n'.join(out) + + +def main(argv): + args = [a for a in argv[1:] if not a.startswith('--')] + workspace, result_path = '.', None + if '--workspace' in argv: + workspace = argv[argv.index('--workspace') + 1] + args = [a for a in args if a != workspace] + if '--result' in argv: + result_path = argv[argv.index('--result') + 1] + args = [a for a in args if a != result_path] + if not args: + print('usage: render_rules.py [--workspace DIR] [--result FILE]', file=sys.stderr) + return 2 + system = args[0] + base = os.path.join(workspace, 'analysis', system) + result_path = result_path or os.path.join(base, 'rules_result.json') + try: + with open(result_path, encoding='utf-8') as handle: + result = json.load(handle) + except (OSError, ValueError) as error: + print(f'Cannot read {result_path}: {error}', file=sys.stderr) + return 1 + + rules = sorted(result.get('confirmedRules') or [], + key=lambda r: (PRIORITY.get(r.get('priority'), 1), + CATEGORIES.index(r['category']) if r.get('category') in CATEGORIES else 9, + str(r.get('source')))) + numbered = [(i + 1, r) for i, r in enumerate(rules)] + rendered = {n: render_rule(n, r) for n, r in numbered} + stats = result.get('stats') or {} + + doc = [f'# Business Rules — {one_line(system, 80)}', '', + f"{len(rules)} confirmed rules ({sum(1 for r in rules if r.get('priority') == 'P0')} P0). " + f"Each citation was checked by a second agent that read the cited lines; " + f"{len(result.get('rejectedRules') or [])} candidate rules were refuted and left out.", '', + '| ID | Name | Category | Priority | Source | Confidence |', '|---|---|---|---|---|---|'] + for n, r in numbered: + doc.append(f"| RULE-{n:03d} | {one_line(r.get('name'), 80)} | {one_line(r.get('category'), 20)} | " + f"{one_line(r.get('priority'), 4)} | `{one_line(r.get('source'), 80).replace(chr(39), '')}` | {one_line(r.get('confidence'), 10)} |") + doc.append('') + for category in CATEGORIES + [None]: + group = [n for n, r in numbered if (r.get('category') if r.get('category') in CATEGORIES else None) == category] + if not group: + continue + doc += [f"## {category or 'Other'}", ''] + for n in group: + doc += [rendered[n][1], ''] + sme = [(n, r) for n, r in numbered if r.get('confidence') != 'High'] + doc += ['## Rules requiring SME confirmation', ''] + if sme: + for n, r in sme: + doc.append(f"- **RULE-{n:03d}** ({one_line(r.get('confidence'), 10)}): " + f"{one_line(r.get('smeQuestion') or 'Confirm this rule with someone who knows the system.', 400)}") + else: + doc.append('None: every confirmed rule has High confidence.') + doc.append('') + + flags = result.get('injectionFlags') or [] + if flags: + doc += ['## ⚠ Instruction-shaped content found in source', '', + 'These lines of the source tried to steer automated analysis. A person should look at them.', ''] + doc += [f'- {one_line(f, 300)}' for f in flags] + doc.append('') + + gaps = [('never attempted (token budget or agent cap)', stats.get('skippedModules')), + ('extractor returned nothing', stats.get('failedModules')), + ('malformed entries, fix by hand', stats.get('droppedModules')), + ('phases cut short', stats.get('skippedPhases')), + ('candidate rules no referee judged (not in the catalog)', + [f"{one_line(u.get('name'), 80)} ({one_line(u.get('source'), 80)})" for u in (result.get('unverifiedRules') or [])])] + gaps = [(label, items) for label, items in gaps if items] + if gaps: + doc += ['## Coverage gaps', '', 'These parts were NOT fully mined:', ''] + for label, items in gaps: + doc.append(f"- **{len(items)} {label}:** " + ', '.join(one_line(i, 100) for i in items[:30]) + (' …' if len(items) > 30 else '')) + if result.get('rerunModules'): + doc.append(f"\nA follow-up run for the {len(result['rerunModules'])} affected shard(s) covers them.") + doc.append('') + + with open(os.path.join(base, 'BUSINESS_RULES.md'), 'w', encoding='utf-8') as handle: + handle.write('\n'.join(doc)) + + objects = result.get('dataObjects') or [] + dto = [f'# Data Objects — {one_line(system, 80)}', ''] + if not objects: + dto.append('No data objects were cataloged.') + for obj in objects: + dto += [f"## {one_line(obj.get('name'), 100)}", f"Source: `{one_line(obj.get('source'), 160).replace(chr(39), '')}`", '', + '| Field | Type | Note |', '|---|---|---|'] + for field in obj.get('fields') or []: + dto.append(f"| {one_line(field.get('name'), 80)} | {one_line(field.get('type'), 60)} | {one_line(field.get('note'), 160)} |") + used = obj.get('consumedBy') or [] + if used: + dto += ['', 'Used by: ' + ', '.join(one_line(u, 80) for u in used)] + dto.append('') + with open(os.path.join(base, 'DATA_OBJECTS.md'), 'w', encoding='utf-8') as handle: + handle.write('\n'.join(dto)) + + p0 = sum(1 for r in rules if r.get('priority') == 'P0') + print(f"BUSINESS_RULES.md: {len(rules)} rules ({p0} P0, {len(sme)} need SME review); DATA_OBJECTS.md: {len(objects)} objects; " + f"rejected {len(result.get('rejectedRules') or [])}, unverified {len(result.get('unverifiedRules') or [])}") + return 0 + + +if __name__ == '__main__': + sys.exit(main(sys.argv)) -- cgit From 696904602b2bdf3e0ddd07ae1221f9eb3e3b8e62 Mon Sep 17 00:00:00 2001 From: Morgan Lunt Date: Thu, 24 Sep 2026 20:15:45 -0700 Subject: code-modernization: rate rules by what the system is for, and make live runs finish Two problems from running the commands on real repositories: - The P0 definition was money- and regulation-shaped, so a music-library tool got no P0 rules and its behavior contract came out empty. P0 now means the system's core purpose depends on the rule, or a wrong result is costly or irreversible (money, legal, data integrity, security or safety, or the central calculation or decision the system exists to perform). - Citations came back as lists of ranges, and the rule renderer numbered RULE-NNN in a different order from the document. Each rule now cites one range and the numbers read in sequence. Commands that start subagents now say to run them in the foreground and wait, because a headless session ends when the model stops talking even if it said it was waiting for a background agent. --- plugins/code-modernization/scripts/render_rules.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'plugins/code-modernization/scripts/render_rules.py') diff --git a/plugins/code-modernization/scripts/render_rules.py b/plugins/code-modernization/scripts/render_rules.py index da375f55..88d75625 100644 --- a/plugins/code-modernization/scripts/render_rules.py +++ b/plugins/code-modernization/scripts/render_rules.py @@ -5,7 +5,7 @@ Reads analysis//rules_result.json (the object the workflow returned, saved by the command) and writes analysis//BUSINESS_RULES.md and DATA_OBJECTS.md. Rules are -numbered RULE-001, RULE-002, ... in priority then category order and the heading is always +numbered RULE-001, RULE-002, ... in the order they appear (by category, then priority) and the heading is always `### RULE-NNN: `, the pattern later commands and the pane look for. Every value comes from analysis of untrusted code, so it is written as plain text on one line where it is a heading or a table cell. Standard library only. @@ -83,9 +83,10 @@ def main(argv): print(f'Cannot read {result_path}: {error}', file=sys.stderr) return 1 + # Numbered in the order they appear in the document (by category, then priority), so RULE-001.. read in sequence. rules = sorted(result.get('confirmedRules') or [], - key=lambda r: (PRIORITY.get(r.get('priority'), 1), - CATEGORIES.index(r['category']) if r.get('category') in CATEGORIES else 9, + key=lambda r: (CATEGORIES.index(r['category']) if r.get('category') in CATEGORIES else 9, + PRIORITY.get(r.get('priority'), 1), str(r.get('source')))) numbered = [(i + 1, r) for i, r in enumerate(rules)] rendered = {n: render_rule(n, r) for n, r in numbered} -- cgit From e662d6d338a7abb1a03ba97e1a47fb98d669c81f Mon Sep 17 00:00:00 2001 From: Morgan Lunt Date: Thu, 24 Sep 2026 20:29:13 -0700 Subject: code-modernization: keep one citation per rule when rendering business rules The extractor is told to cite a single file range per rule, but on a large Perl codebase it kept returning two or three files and a sentence of explanation in the source field, which the report and the pane cannot resolve to a line. The renderer now takes the first path:line-range as the rule's citation and puts anything after it on an "Also cited" line. --- plugins/code-modernization/scripts/render_rules.py | 25 +++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'plugins/code-modernization/scripts/render_rules.py') diff --git a/plugins/code-modernization/scripts/render_rules.py b/plugins/code-modernization/scripts/render_rules.py index 88d75625..b815a2d7 100644 --- a/plugins/code-modernization/scripts/render_rules.py +++ b/plugins/code-modernization/scripts/render_rules.py @@ -12,6 +12,7 @@ a heading or a table cell. Standard library only. """ import json import os +import re import sys CATEGORIES = ['Calculation', 'Validation', 'Lifecycle', 'Policy'] @@ -24,6 +25,21 @@ def one_line(value, limit=300): return text if len(text) <= limit else text[: limit - 1].rstrip() + '…' +CITATION = re.compile(r'^\s*`?([^\s`:,;()]+):(\d+)(?:\s*[-\u2013]\s*(\d+))?') + + +def split_citation(value): + """One citation per rule: the first path:line[-line] in the text is it, and whatever follows + (a second file, a note) comes back separately so later tools can rely on the citation alone.""" + text = ' '.join(str(value if value is not None else '').split()) + m = CITATION.match(text) + if not m: + return one_line(text, 200), '' + primary = f'{m.group(1)}:{m.group(2)}' + (f'-{m.group(3)}' if m.group(3) else '') + rest = text[m.end():].strip(' \t;,.-\u2013\u2014`') + return one_line(primary, 200), one_line(rest, 300) + + def block(value): """Multi-line text: keep the lines, but never let a value open a heading or a fence.""" lines = [] @@ -37,11 +53,14 @@ def block(value): def render_rule(number, rule): rid = f'RULE-{number:03d}' + source, also = split_citation(rule.get('source')) out = [f"### {rid}: {one_line(rule.get('name'), 120)}", f"**Category:** {one_line(rule.get('category'), 20)}", f"**Priority:** {one_line(rule.get('priority'), 4)}", - f"**Source:** `{one_line(rule.get('source'), 200).replace(chr(39), '')}`", - f"**Plain English:** {one_line(rule.get('plainEnglish'), 400)}", + f"**Source:** `{source.replace(chr(39), '')}`"] + if also: + out.append(f'**Also cited:** {also}') + out += [f"**Plain English:** {one_line(rule.get('plainEnglish'), 400)}", '**Specification:**', f" Given {block(rule.get('given'))}", f" When {block(rule.get('when'))}", @@ -99,7 +118,7 @@ def main(argv): '| ID | Name | Category | Priority | Source | Confidence |', '|---|---|---|---|---|---|'] for n, r in numbered: doc.append(f"| RULE-{n:03d} | {one_line(r.get('name'), 80)} | {one_line(r.get('category'), 20)} | " - f"{one_line(r.get('priority'), 4)} | `{one_line(r.get('source'), 80).replace(chr(39), '')}` | {one_line(r.get('confidence'), 10)} |") + f"{one_line(r.get('priority'), 4)} | `{split_citation(r.get('source'))[0][:80].replace(chr(39), '')}` | {one_line(r.get('confidence'), 10)} |") doc.append('') for category in CATEGORIES + [None]: group = [n for n, r in numbered if (r.get('category') if r.get('category') in CATEGORIES else None) == category] -- cgit From 5b8ec4b30f45b5e8b4c8a6efd63719730cf1a5ad Mon Sep 17 00:00:00 2001 From: Morgan Lunt Date: Thu, 24 Sep 2026 20:34:21 -0700 Subject: code-modernization: harden the numeric tolerance and say what Rehost and Replace mean Follow-ups from review of the tolerance check: an absolute tolerance above 1e-6 is refused like a relative one above 1%, numbers are compared as exact decimals rather than doubles, dotted runs such as 1.2.3 or 10.0.0.1 are text that must match exactly, and the self-check now changes the leading digit of every sampled number to prove the tolerance cannot hide a real change. The business-rules header says how many rules there were at extraction, since later steps add rules. The assessment says what to do when it recommends Rehost or Replace, which change no code and so have no build command. --- plugins/code-modernization/scripts/render_rules.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'plugins/code-modernization/scripts/render_rules.py') diff --git a/plugins/code-modernization/scripts/render_rules.py b/plugins/code-modernization/scripts/render_rules.py index b815a2d7..f098a5cf 100644 --- a/plugins/code-modernization/scripts/render_rules.py +++ b/plugins/code-modernization/scripts/render_rules.py @@ -112,7 +112,8 @@ def main(argv): stats = result.get('stats') or {} doc = [f'# Business Rules — {one_line(system, 80)}', '', - f"{len(rules)} confirmed rules ({sum(1 for r in rules if r.get('priority') == 'P0')} P0). " + f"At extraction: {len(rules)} confirmed rules ({sum(1 for r in rules if r.get('priority') == 'P0')} P0); " + f"later steps may add or correct rules below. " f"Each citation was checked by a second agent that read the cited lines; " f"{len(result.get('rejectedRules') or [])} candidate rules were refuted and left out.", '', '| ID | Name | Category | Priority | Source | Confidence |', '|---|---|---|---|---|---|'] -- cgit From ff833bd7231262a8521ab3728c5625dc6c4f4a1d Mon Sep 17 00:00:00 2001 From: Morgan Lunt Date: Thu, 24 Sep 2026 21:22:06 -0700 Subject: code-modernization: make rule consolidation auditable and injection-safe Follow-ups from review of the consolidation pass: - The agent is a restricted type with the same read-only wording as its siblings, and every field on its numbered list is a single bounded line, so a rule name containing a newline cannot fake another entry. - A group with any number out of range or not an integer is rejected whole instead of being coerced into a different merge. - A merge keeps the most conservative review state: the highest priority, the lowest confidence, and every suspected defect and SME question of the folded rules. - Every folded rule is returned with the rule it went into, and the rules document lists them under "Rules folded into another", so nothing disappears untraceably. Extra citations show as "Also cited" without the joining word. --- plugins/code-modernization/scripts/render_rules.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'plugins/code-modernization/scripts/render_rules.py') diff --git a/plugins/code-modernization/scripts/render_rules.py b/plugins/code-modernization/scripts/render_rules.py index f098a5cf..fa609332 100644 --- a/plugins/code-modernization/scripts/render_rules.py +++ b/plugins/code-modernization/scripts/render_rules.py @@ -36,8 +36,9 @@ def split_citation(value): if not m: return one_line(text, 200), '' primary = f'{m.group(1)}:{m.group(2)}' + (f'-{m.group(3)}' if m.group(3) else '') - rest = text[m.end():].strip(' \t;,.-\u2013\u2014`') - return one_line(primary, 200), one_line(rest, 300) + rest = re.sub(r'\s*;\s*also\s+', '; ', text[m.end():], flags=re.I) # citations a consolidation appended + rest = re.sub(r'^[\s;,.\-\u2013\u2014`]*(?:also\s+)?', '', rest, flags=re.I) + return one_line(primary, 200), one_line(rest.strip(' \t;,.-\u2013\u2014`'), 300) def block(value): @@ -145,6 +146,16 @@ def main(argv): doc += [f'- {one_line(f, 300)}' for f in flags] doc.append('') + folded = [f for f in (result.get('foldedRules') or []) if isinstance(f, dict)] + if folded: + doc += ['## Rules folded into another', '', + 'These rules described the same behavior as another rule in a different place, so they were merged into it ' + '(the kept rule lists their locations under "Also cited"):', ''] + doc += [f"- {one_line(f.get('name'), 100)} ({one_line(f.get('source'), 100)}) into {one_line(f.get('into'), 100)}" for f in folded[:100]] + if len(folded) > 100: + doc.append(f'- … and {len(folded) - 100} more') + doc.append('') + gaps = [('never attempted (token budget or agent cap)', stats.get('skippedModules')), ('extractor returned nothing', stats.get('failedModules')), ('malformed entries, fix by hand', stats.get('droppedModules')), -- cgit