Skip to content
VDAI with VD

Phase 19 · Capstone Projects

Capstone 09 — Code Migration Agent (Repo-Level Language / Runtime Upgrade)

Amazon's MigrationBench (Java 8 to 17) and Google's App Engine Py2-to-Py3 migrator set the 2026 bar. Moderne's OpenRewrite does deterministic AST rewrites at scale. Grit targets the same problem with codemod-style DSL. The production pattern combines both: a deterministic substrate for safe rewrites plus an agent layer for the ambiguous cases, a sandbox for per-branch builds, and a test harness that flips green before the PR opens. The capstone is to migrate 50 real repos and publish a pass rate with a failure taxonomy.

  • Capstone
  • Python (agent), Java / Python (targets), TypeScript (dashboard)
  • 30 hours
  • Before this: Phase 5 (NLP), Phase 7 (transformers), Phase 11 (LLM engineering), Phase 13 (tools), Phase 14 (agents), Phase 15 (autonomous), Phase 17 (infrastructure)

Problem

Large-scale code migration is one of the cleanest production applications of 2026 coding agents. The ground truth is obvious (does the test suite pass after the migration?), the rewards are real (a Java-8 fleet migration is a headcount-scale project), and the benchmarks are public (MigrationBench 50-repo subset). Moderne's OpenRewrite handles the deterministic side. The agent layer handles everything OpenRewrite recipes cannot: ambiguous rewrites, build-system drift, long-tail syntax, transitive dependency breakage.

You will build an agent that takes a Java 8 repo (or Python 2 repo) and produces a green-CI migrated branch. You will measure pass rate, test-coverage preservation, cost per repo, and build a failure taxonomy. The side-by-side against a deterministic-only baseline tells you where the agent's value actually lives.

Concept

The pipeline has two layers. The deterministic substrate (OpenRewrite for Java, libcst for Python) runs the bulk of mechanical rewrites safely: imports, method signatures, null-safety edits, try-with-resources, deprecated API replacements. It is fast and produces auditable diffs. The agent layer (OpenAI Agents SDK or LangGraph over Claude Opus 4.7 and GPT-5.4-Codex) handles cases the recipes cannot: build-file upgrades (Maven/Gradle/pyproject), transitive dependency conflicts, test flakes, custom annotations.

Each repo gets a Daytona sandbox with the target runtime preinstalled. The agent iterates: run build, classify failures, apply fix, rerun. Hard limits: 30 minutes per repo, $8 per repo, 20 agent turns. If all tests pass and the coverage delta is not negative, the branch opens a PR. If not, the repo gets filed under a failure class with evidence.

The failure taxonomy is the deliverable. Across 50 repos, what broke? Transitive deps? Custom annotations? Build tool version? Test flakes unrelated to migration? Each class gets a count and an exemplar diff. Future recipe authors can target the top three.

Architecture

target repo
      |
      v
OpenRewrite / libcst deterministic recipes
   (safe, fast, auditable, ~70-80% of fixes)
      |
      v
Daytona sandbox per branch
      |
      v
agent loop (Claude Opus 4.7 / GPT-5.4-Codex):
   - run build -> capture failures
   - classify failures (build, test, lint)
   - apply fix (patch or retry recipe)
   - rerun
   - budget: 30 min, $8, 20 turns
      |
      v
test + coverage delta gate
      |
      v (passed)
open PR
      |
      v (failed)
file under failure class + attach repro

Stack

  • Deterministic substrate: OpenRewrite (Java) or libcst (Python)
  • Agent: OpenAI Agents SDK or LangGraph over Claude Opus 4.7 + GPT-5.4-Codex
  • Sandbox: Daytona devcontainers per branch, pre-installed target runtime (Java 17 / Python 3.12)
  • Build systems: Maven, Gradle, uv (Python)
  • Benchmarks: Amazon MigrationBench 50-repo subset (Java 8 to 17), Google App Engine Py2-to-Py3 repos
  • Test harness: parallel runner, coverage via Jacoco (Java) or coverage.py (Python)
  • Observability: Langfuse + trace bundle per repo with every diff chunk
  • Dashboard: failure-taxonomy dashboard with per-class counts and exemplar diffs

Interactive figure coming later.

Build It

  1. Recipe pass. Run OpenRewrite (Java) or libcst (Python) recipes first. Catch the 70-80% of migrations that are mechanical. Commit as "recipe" commit.

  2. Build trial. Daytona sandbox: install target runtime, run the build. If green, skip to tests. If red, hand off to agent.

  3. Agent loop. LangGraph with tools: run_build, read_file, edit_file, run_test, git_diff. Agent classifies the failure (dep, syntax, test, build-tool) and applies a targeted fix. Rerun.

  4. Budget caps. 30 minutes wall-clock per repo, $8 cost, 20 agent turns. Any breach halts and files under "budget_exhausted" with the current diff.

  5. Test + coverage gate. After the build goes green, run the test suite. Compare coverage to the base repo. If coverage dropped more than 2%, file under "coverage_regression".

  6. PR open. On success, push the branch, open the PR with the diff and a summary of which recipes applied and which commits the agent authored.

  7. Failure taxonomy. For each failed repo, tag with a class: dep_upgrade_required, build_tool_drift, custom_annotation, test_flake, syntax_edge_case, budget_exhausted. Build a dashboard.

  8. 50-repo run. Execute across the MigrationBench subset. Report per-class pass rate, cost-per-repo, coverage-preservation, and a compare-vs-deterministic-only baseline.

Use It

$ migrate legacy-java-service --target java17
[recipe]   27 rewrites applied (JUnit 4->5, HashMap initializer, try-with-resources)
[build]    FAIL: cannot find symbol sun.misc.BASE64Encoder
[agent]    turn 1 classify: removed_jdk_api
[agent]    turn 2 apply: sun.misc.BASE64Encoder -> java.util.Base64
[build]    OK
[tests]    412/412 passing; coverage 84.1% -> 84.3%
[pr]       opened #1841  cost=$3.20  turns=4

Ship It

outputs/skill-migration-agent.md is the deliverable. Given a repo, it executes deterministic recipes then an agent loop to produce a green migrated branch, or files the repo under a taxonomy class.

WeightCriterionHow it is measured
25MigrationBench pass rate50-repo subset pass@1
20Test-coverage preservationMean coverage delta vs base
20Cost per migrated repo$/repo on passing runs
20Agent / deterministic-tool integrationFraction of fixes that OpenRewrite handled vs agent authored
15Failure analysis write-upTaxonomy completeness with exemplars
100

Exercises

  1. Run the migrate pipeline with OpenRewrite only (no agent). Compare pass rate to the full pipeline. Identify the cases where the agent alone is the difference.

  2. Implement a "lint-clean" check: after migration, run a style linter (spotless for Java, ruff for Python). Fail the PR if new lint errors appear. Measure the coverage-preserved-but-style-regressed rate.

  3. Add a "minimal-diff" optimizer: after the agent's branch passes tests, trim unnecessary changes with a second pass. Report diff-size reduction.

  4. Extend to a third migration: Node 18 to Node 22. Reuse the sandbox wrapping; swap the recipe layer for a custom codemod.

  5. Measure time-to-first-green-build (TTFGB) as a UX metric. Target: p50 under 10 minutes.

Key Terms

TermWhat people sayWhat it actually means
Deterministic substrate"Recipe engine"OpenRewrite / libcst: declarative AST rewrites with safety guarantees
Codemod"Code-modifying program"A rewrite rule that changes source code mechanically
Build drift"Tool version skew"Subtle Maven / Gradle / uv behavior changes between major versions
Failure class"Taxonomy bucket"A labeled reason a repo did not migrate: dep, syntax, test, build-tool, budget
Coverage delta"Coverage preservation"Change in test coverage % from base to migrated branch
Agent turn"Tool-call round"One plan -> act -> observe cycle in the agent loop
Budget exhaustion"Hit the ceiling"The repo consumed its 30-min / $8 / 20-turn limit without passing

Further Reading

Code

  • main.py

    python phases/19-capstone-projects/09-code-migration-agent/code/main.pyOpen on GitHub ↗
  • ts/package.json

    Open on GitHub ↗
  • ts/README.md

    Open on GitHub ↗
  • ts/src/cost.ts

    npx tsx phases/19-capstone-projects/09-code-migration-agent/code/ts/src/cost.tsOpen on GitHub ↗
  • ts/src/index.ts

    npx tsx phases/19-capstone-projects/09-code-migration-agent/code/ts/src/index.tsOpen on GitHub ↗
  • ts/src/migrations.ts

    npx tsx phases/19-capstone-projects/09-code-migration-agent/code/ts/src/migrations.tsOpen on GitHub ↗
  • ts/src/server.ts

    npx tsx phases/19-capstone-projects/09-code-migration-agent/code/ts/src/server.tsOpen on GitHub ↗
  • ts/src/types.ts

    npx tsx phases/19-capstone-projects/09-code-migration-agent/code/ts/src/types.tsOpen on GitHub ↗
  • ts/tests/cost.test.ts

    npx tsx phases/19-capstone-projects/09-code-migration-agent/code/ts/tests/cost.test.tsOpen on GitHub ↗
  • ts/tests/migrations.test.ts

    npx tsx phases/19-capstone-projects/09-code-migration-agent/code/ts/tests/migrations.test.tsOpen on GitHub ↗
  • ts/tsconfig.json

    Open on GitHub ↗

Take-away files

skill-migration-agent.md

---
name: migration-agent
description: Build a repo-level code migration agent that combines deterministic recipes with an agent fallback loop, passes MigrationBench, and publishes a failure taxonomy.
version: 1.0.0
phase: 19
lesson: 09
tags: [capstone, code-migration, openrewrite, libcst, migrationbench, agent, sandbox]
---

Given a Java 8 or Python 2 repo, produce a migrated branch (to Java 17 or Python 3.12) with a green test suite and minimal coverage regression. Evaluate across the 50-repo MigrationBench subset.

Build plan:

1. Deterministic pass: OpenRewrite (Java) or libcst (Python) runs mechanical rewrites first. Commit as the "recipe" commit with a clean diff.
2. Daytona sandbox: target runtime preinstalled; per-branch build; read-only source mount.
3. Agent loop: LangGraph or OpenAI Agents SDK over Claude Opus 4.7 + GPT-5.4-Codex. Tools: `run_build`, `read_file`, `edit_file`, `run_test`, `git_diff`. Classify failure (dep, syntax, test, build-tool), apply targeted fix, rerun.
4. Budget caps: 30 min, $8, 20 turns. Breaching any halts and files under `budget_exhausted` with the current diff.
5. Test + coverage gate: build green then tests green; coverage must not drop more than 2%.
6. PR open with recipe-commit + agent commits + summary comment.
7. Failure taxonomy: per-repo tag from `{dep_upgrade_required, build_tool_drift, custom_annotation, test_flake, syntax_edge_case, budget_exhausted, coverage_regression}`.
8. 50-repo run across MigrationBench; publish per-class pass rate, cost-per-repo, and coverage-preservation; compare vs deterministic-only baseline.

Assessment rubric:

| Weight | Criterion | Measurement |
|:-:|---|---|
| 25 | MigrationBench pass rate | 50-repo subset pass@1 |
| 20 | Test-coverage preservation | Mean coverage delta vs base branch |
| 20 | Cost per migrated repo | Mean $/repo on passing runs |
| 20 | Agent / deterministic-tool integration | Fraction of fixes handled by OpenRewrite vs agent |
| 15 | Failure analysis write-up | Taxonomy completeness with exemplars |

Hard rejects:

- Pipelines that skip the deterministic pass. OpenRewrite handles the mechanical 70-80% cheaper and more reliably than any agent.
- Coverage regressions above 2% treated as passing.
- PRs that bundle mechanical and agent-authored changes into one commit. Must separate.
- Reporting pass rate without a matched deterministic-only baseline on the same 50 repos.

Refusal rules:

- Refuse to force-push a migrated branch over the base. Always a new branch + PR.
- Refuse to open a PR whose CI has not flipped green in the sandbox.
- Refuse to run on corporate repos without explicit license to modify.

Output: a repo containing the two-layer migration pipeline, the 50-repo MigrationBench run logs, the failure taxonomy dashboard, a matched deterministic-only baseline run, and a write-up on the three most common failure classes and the recipe change that would eliminate each.