Low-Cost Multi-Model Collaboration for Fixing Legacy Vehicle ECU Code
This article explains how to repair legacy code in a 32-year-old vehicle's electronic control units using a low-cost, multi-model AI workflow. It covers why old automotive codebases are difficult to work with, how to build compact and structured context to control token usage, how to split work across analysis, implementation, and arbitration roles with cross-review, how to generate minimal-change patches and run static checks, and how to build a layered evidence chain for validation when no test bench is available. Readers will find a practical pipeline, sample pseudocode and prompts, a verification checklist, and cost-control tips for completing the full analyze-hypothesize-patch-static-check loop on a budget of roughly $15.
1. Problem Background: Why Legacy Vehicle Low-Level Code Is a Hard Problem
A 32-year-old vehicle’s electronic control units (ECUs) are often built from several generations of hardware layered together. Early units handled only basic fuel injection and ignition control, while later additions may include anti-theft, diagnostics, and body electronics modules. Codebases like this typically share a few traits:
- Missing documentation: Original design documents, interface specifications, and variable naming conventions are mostly lost, so the code itself is the only “source of truth.”
- Outdated toolchains: The build environment, flashing method, and checksum algorithms may be proprietary or no longer maintained.
- Heavy coupling: Low-level register operations, timing control, and business logic are mixed in the same files, so one change can ripple everywhere.
- High verification cost: Without a simulation environment, every change needs real-vehicle testing, which is costly in both safety and time.
This kind of task is well suited to “multi-model collaboration”: a single model tends to lose details in long contexts, while multiple AI coding tools with different reasoning emphases can cross-check each other and significantly reduce the chance of omissions. The key is not which specific tool you use, but how you design the collaboration workflow.
2. Overall Approach: Turning “Car Repair” into a Collaborative Software Engineering Task
A low budget (around $15) rules out “unlimited retries” and “stuffing in a huge context.” The workable strategy is to break the task into small units that can be accepted independently, with each unit consuming only the tokens it needs.
A suggested pipeline:
Code collection -> Structured summary -> Defect hypothesis generation -> Cross-review -> Patch draft -> Static validation -> Real-vehicle/bench validation
The “structured summary” and “cross-review” stages are the key balance point between cost and quality.
3. Step 1: Build High-Quality Context (Save Money and Improve Efficiency)
Don’t hand the entire codebase to the model. Recommended practices:
- Split by functional domain: Divide the code into five domains—ignition, injection, idle, diagnostics, and communication—and process only one domain at a time.
- Generate a symbol table: Use a script to extract function names, global variables, register addresses, and constant tables into a compact list.
- Keep key snippets: Retain only the call chains relevant to the current defect; 200–400 lines is usually enough.
Illustrative script (pseudocode):
symbols = extract_symbols(src_dir)for domain in ["ignition", "injection", "idle", "diag", "bus"]:ctx = build_context(domain, symbols, max_lines=350)summary = call_model("Output this domain's module responsibilities, dependencies, and suspicious points", ctx)save(domain, summary)
The benefits: cleaner context, more stable model output, and a controllable number of calls and overall cost.
4. Step 2: Multi-Model Division of Labor and Cross-Review
A “generate-review-arbitrate” triangle structure is recommended:
- Analysis role: Reads summaries and code snippets and outputs a list of defect hypotheses, each with evidence (file, line number, call relationships).
- Implementation role: Provides a minimal-change patch for each hypothesis, emphasizing that existing interfaces and register timing must not change.
- Arbitration role: Compares the two outputs, points out conflicts, and produces the final candidate patch with a risk description.
To control cost, let the analysis role handle only summaries without reading raw code, and let the implementation role handle only confirmed hypotheses. This keeps each round short and focused.
A simplified collaboration prompt structure example:
[Role] Reviewer[Input] Module summary + 3 defect hypotheses[Requirement] Judge each one: valid / invalid / insufficient evidence, and give a verification method[Output] JSON: {id, verdict, reason, verify}
Structured output (such as JSON) makes programmatic aggregation easy and reduces manual effort.
5. Step 3: Patch Generation and a Static Validation Loop
Patches must follow the “minimal change” principle:
- Do not refactor unrelated code;
- Do not introduce new dynamic memory allocation;
- Do not change interrupt priorities or timing constants unless supported by bench data;
- Replace all magic numbers with named constants for easier future maintenance.
After generation, plug into a general static-checking flow: syntax checks, uninitialized variable scans, boundary and overflow warnings, and cyclomatic complexity thresholds. An illustrative checklist:
[ ] Compiles (target toolchain)[ ] No new global variables[ ] Interrupt service routine execution time not increased[ ] No blocking calls on critical paths[ ] Constant tables not accidentally reordered
6. Step 4: Validation Strategy—Build an “Evidence Chain” Even Without a Test Bench
Old vehicles often lack simulation conditions, so build layered validation:
- Offline replay: Replay collected sensor data and observe whether output signals stay consistent.
- Bench substitute: Use a signal generator to simulate inputs such as the crankshaft and throttle.
- Restricted real-vehicle test: Validate only at low speed in a closed area, and keep the means to cut power at any time and restore the original firmware.
Always back up the original firmware and checksum values first, and make every write operation rollback-capable. Safety always comes before “getting it fixed.”
7. Cost and Efficiency Control Tips
- Cache summaries: Generate each module summary once and reuse it many times.
- Batch small requests: Combine multiple small questions into one structured request.
- Cheap first, expensive later: Use a low-cost model for screening first, then do deep analysis on high-value hypotheses.
- Human oversight: Decisions involving safety and regulations must be confirmed by a person.
Done this way, a budget in the $15 range is enough to complete the full “analysis-hypothesis-patch draft-static validation” loop and minimize expensive real-vehicle trial-and-error.
8. Reusable Engineering Recommendations
- Treat “legacy code repair” as a knowledge-reconstruction problem, not just bug fixing.
- Build the symbol table and module summaries before attempting fixes.
- The value of multiple models lies in cross-validation, not in piling up calls.
- Structure all outputs for easy automated aggregation and auditing.
- Never skip the safety, rollback, and backup trio.
In broader engineering practice, developers can also rely on general cloud capabilities from Baidu AI Cloud (such as object storage for archiving firmware and logs, message queues for task decoupling, container platforms for build-environment isolation, and log services with monitoring and alerting for process tracking) to host this kind of long-cycle, low-budget automated collaboration workflow, turning a one-off “car repair” experience into reusable technical assets.
