diff options
| author | Morgan Lunt <morganl@anthropic.com> | 2026-09-24 21:22:06 -0700 |
|---|---|---|
| committer | Morgan Lunt <morganl@anthropic.com> | 2026-09-24 21:22:06 -0700 |
| commit | ff833bd7231262a8521ab3728c5625dc6c4f4a1d (patch) | |
| tree | d12e70b6407f666e787fae7217362011f892f459 /plugins/code-modernization/scripts | |
| parent | f45794fa4831636e7ae55f8081769c8ebf3f58c6 (diff) | |
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.
Diffstat (limited to 'plugins/code-modernization/scripts')
| -rw-r--r-- | plugins/code-modernization/scripts/render_rules.py | 15 | ||||
| -rw-r--r-- | plugins/code-modernization/scripts/tests/test_rules_tools.py | 13 |
2 files changed, 26 insertions, 2 deletions
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')), diff --git a/plugins/code-modernization/scripts/tests/test_rules_tools.py b/plugins/code-modernization/scripts/tests/test_rules_tools.py index 59d9a30b..33cfd279 100644 --- a/plugins/code-modernization/scripts/tests/test_rules_tools.py +++ b/plugins/code-modernization/scripts/tests/test_rules_tools.py @@ -174,6 +174,19 @@ class RenderTests(unittest.TestCase): self.assertIn('**Also cited:** (aggregation); family table at lib/b.pm:31-40', text) self.assertEqual(text.count('**Also cited:**'), 2) + def test_folded_rules_are_listed_so_nothing_disappears_untraceably(self): + result = {'confirmedRules': [{'name': 'Twiddle table', 'category': 'Calculation', 'priority': 'P0', 'source': 'a.c:1-2 ; also b.c:3-4', + 'plainEnglish': 'x', 'given': 'g', 'when': 'w', 'then': 't', 'confidence': 'Low'}], + 'foldedRules': [{'name': 'Twiddle factors', 'source': 'b.c:3-4', 'into': 'Twiddle table'}, 'junk', {'name': 'X\n# fake heading', 'source': 's', 'into': 'i'}]} + with tempfile.TemporaryDirectory() as ws: + write(ws, 'analysis/s/rules_result.json', json.dumps(result)) + self.assertEqual(run('render_rules.py', 's', '--workspace', ws).returncode, 0) + text = open(os.path.join(ws, 'analysis/s/BUSINESS_RULES.md'), encoding='utf-8').read() + self.assertIn('## Rules folded into another', text) + self.assertIn('- Twiddle factors (b.c:3-4) into Twiddle table', text) + self.assertIn('**Also cited:** b.c:3-4', text) + self.assertNotIn('\n# fake heading', text) + def test_unreadable_result_is_an_error(self): with tempfile.TemporaryDirectory() as ws: self.assertEqual(run('render_rules.py', 's', '--workspace', ws).returncode, 1) |
