From 166f4fb7a1b14c4d0f919bb0bd3a7e54b1600492 Mon Sep 17 00:00:00 2001 From: diffwireauto <330573775+diffwireauto@users.noreply.github.com> Date: Thu, 22 Oct 2026 23:49:31 -0400 Subject: [PATCH] fix(#770): manifest check refuses conditional or doubled codes From the Gemini adversarial review of Epic A, both checked against the firmware before fixing: - The generator reads source with regexes, so a protocol code inside #if would be taken whichever branch a build uses. It now refuses one (none exist at the pinned tags; FIRMWARE_VERSION's #ifndef default is not a protocol code), and refuses a name defined twice with different values. - The contract test compares names after dropping case and underscores; it now fails if two names collapse to the same key. Both pinned manifests regenerate unchanged. Co-Authored-By: Claude Opus 5.5 --- test/fake_radio/manifest_contract_test.dart | 20 ++++++++++++ tool/fake_radio/gen_protocol_manifest.py | 36 ++++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/test/fake_radio/manifest_contract_test.dart b/test/fake_radio/manifest_contract_test.dart index c513c0ed..a68c69fa 100644 --- a/test/fake_radio/manifest_contract_test.dart +++ b/test/fake_radio/manifest_contract_test.dart @@ -102,6 +102,26 @@ void main() { expect(_compare(client, offband).unknown, isEmpty); }); + test('no two names reduce to the same comparison key', () { + // `_key` drops case and underscores. If two names collapsed together, + // one of them would never be compared. + List collisions(Iterable names) { + final seen = {}; + return [ + for (final n in names) + if (seen.putIfAbsent(_key(n), () => n) != n) + '$n and ${seen[_key(n)]}', + ]; + } + + expect(collisions(client.keys), isEmpty); + expect( + collisions([for (final s in _sections) ...offband.section(s).keys]), + isEmpty, + ); + expect(collisions(['CMD_FOO_BAR', 'cmdFooBar']), hasLength(1)); + }); + test('a deliberately wrong constant is caught', () { final doctored = Map.of(client) ..['offbandCapCaplog'] = 0x08 diff --git a/tool/fake_radio/gen_protocol_manifest.py b/tool/fake_radio/gen_protocol_manifest.py index 5449b343..2a9a1a52 100644 --- a/tool/fake_radio/gen_protocol_manifest.py +++ b/tool/fake_radio/gen_protocol_manifest.py @@ -39,8 +39,36 @@ def show(repo, ref, path): return out.stdout +PROTOCOL = re.compile(r"(CMD_|RESP_CODE_|PUSH_CODE_|ERR_CODE_|OFFBAND_)") + + +def conditional_definitions(text): + """Protocol definitions inside #if/#ifdef/#ifndef. Reading source with + regexes can't tell which branch a build takes, so the manifest would be + wrong for some build; refuse instead of guessing.""" + depth, found = 0, [] + for n, line in enumerate(text.splitlines(), 1): + s = line.strip() + if re.match(r"#\s*if", s): + depth += 1 + elif re.match(r"#\s*endif", s): + depth -= 1 + elif depth > 0: + m = DEFINE.match(s) or CONSTEXPR.match(s) + if m and PROTOCOL.match(m.group(1)): + found.append(f"line {n}: {m.group(1)}") + return found + + def pick(pairs, prefix): - return {name: int(value, 0) for name, value in pairs if name.startswith(prefix)} + out = {} + for name, value in pairs: + if not name.startswith(prefix): + continue + if name in out and out[name] != int(value, 0): + sys.exit(f"{name} is defined twice with different values") + out[name] = int(value, 0) + return out def main(): @@ -67,6 +95,12 @@ def main(): if offband is None: sys.exit("offband flavor but OffbandConfigProtocol.h is missing") pairs += CONSTEXPR.findall(offband) + DEFINE.findall(offband) + for name, text in [("MyMesh.cpp", source)] + ( + [("OffbandConfigProtocol.h", offband)] if args.flavor == "offband" else [] + ): + found = conditional_definitions(text) + if found: + sys.exit(f"{name} defines protocol codes inside #if: {', '.join(found)}") commands = pick(pairs, "CMD_") responses = pick(pairs, "RESP_CODE_")