Notes / AI / CSS migration using AI

The Warhammer 40k Adeptus Mechanicus symbol

Table of contents

    00%
    Oregon Trail

    CSS migration using AI

    Planted: 

    Status: seed

    Intended Audience: Software engineers

    An investigation into using AI to migrate CSS from CSS Modules to StyleX.

    Situation

    My team received new requirements, a business decision was made to migrate from CSS modules to StyleX. We own multiple projects so we picked one as a test case to assess how much effort a migration will require. It's a 2-page microsite with a form, made in React.

    The codebase has three versions of this site: v1, v2, and v3. Each builds on the previous. v3 has new components and components imported from v1, v2 and a shared directory. v3 is live and the only version we want to migrate. Before AI, I would use a codemodAn automated script that refactors your codebase by programmatically parsing, changing, and rewriting your source code. but this as an opportunity to assess the latest AI capabilities and see how well it could handle this task.

    Agent:

    • ▪ Harness: Cursor
    • ▪ Model: Fable 5.1

    Prepare the patient

    Before starting the migration I made the codebase agent-friendly by adding any missing AGENT.mdA standard Markdown file placed in a code repository to give AI coding agents project-specific context, rules, and instructions. files to key directories. This is a best practice for any codebase using AI.

    The Spec

    Next, I prompted the agent to write a specA clear, structured description of what software should do, its constraints, and how it behaves. to narrow the gap between my intention and the AI's actions. The AI landscape changes fast making it difficult to know how much information I need to provide. Therefore, instead of guessing, I prompt the simplest, high-level intent: Write a spec to migrate v3 from CSS modules to StyleX. The generated draft reveals what information I need to provide to steer the AI towards what I want. The following are the notable parts.

    Scope

    The spec detailed v1, v2 and a shared directory will be affected by the migration because of imported components. This information is useful because I want to restrict the scope as much as possible. Reprompting added the following to the spec:

    • ▪ duplicate imported v1 and v2 components and relocate to v3's directory.
    • ▪ duplicate imported shared components, append _v2 to the component's name and add a deprecation comment to the original component.
    • ▪ list duplicated shared components in spec so we known which components, outside of the v3 directory, need to be migrated.

    Bad communicator

    A consistent weakness with each generation of AI is communicating technical information. This is apparent with the spec, being overly complex and the way it communicates can be a poor fit for what it's communicating. For example, the spec has a hard cases section. A list of CSS rules that don't easily translate to StyleX and details about how the migration will handle each one. These are communicated as prose but would be easier to understand in a before / after table with examples of code.

    Prose: Desktop menu sibling/state selectors. For the divider between items, compute “show divider” in the loop and apply a variant.

    Table:

    CSSStyleX
    .item + .item // menu dividerDecide in JS whether this item needs a divider; add a divider style when it does

    Reprompting specifying how to display information improves results. Results can be expanded beyond text to diagrams, sandboxes and 3D models.

    Theming

    The plan for migrating theming was bad. It detailed creating some complex solution combining the current solution (CSS variables) with StyleX style objects. This surprised me because:

    • ▪ using CSS variables for themes is a common pattern and
    • ▪ StyleX has a well-documented theme feature.

    Reprompting and providing a link to the StyleX theme documentation improved the plan.

    Animation

    The plan to migrate some animations was also bad. Some animations are linked to scroll position. The animation moving forward or backward depending on the scroll position. To avoid performance problems caused by React component re-rendering, the current code uses CSS variables manipulated by JavaScript within a useEffect. This spec's initial plan would introduce this problem.

    Verification

    Because AI is probabilistic, not deterministic, the most important thing when working with it is verification, confirming what the AI did matches my intention. The spec did a good job at this, listing four checks that run for every PR:

    • ▪ VisregsVisual regression tests - automated check that takes screenshots of your application's user interface (UI) before and after your code changes, then compares them pixel-by-pixel to ensure you didn't accidentally break the visual layout or styling. on storybooks expecting zero diffs.
    • ▪ A git diff to confirm only changes to the v3 and shared directories are made.
    • ▪ Formatting, linting, jest and type checks.
    • ▪ Human verification. I would be prompted to check the latest version of the page.

    Agent architecture

    I was surprised the spec didn't detail agent architecture. When queried, it stated it would use a single-agent working sequentially, producing a chain of 26 PRs. This is inefficient considering part of the migration involves migrating multiple react components. Work that can be done in parallel by multiple agents. Architecture patterns:

    Agent architecture diagram

    Re-prompting added an agent architecture section changing 26 sequential PRs to 4 PR waves.

    • ▪ Wave A: 3 sequential foundational PRs, 1 agent working sequentially.
      • ▪ duplicating components from v1 and shared components
      • ▪ adding missing storybooks
      • ▪ ensure all storybooks have visreg baselines
      • ▪ freezing them so there is no re-baselining
      • ▪ migrating parent files all components rely like themes.
    • ▪ Wave B and C: 9 PRs by 9 agents working in parallel to migrate components.
    • ▪ Wave D: clean up. Doing things like folding the spec into a AGENTS.md.

    Implementation

    The following is issues that came up during implementation.

    Verification works

    A failing visreg exposed a failed migration. StyleX flattens the cascade to atomic classes so a rule that used to win by CSS specificity stops winning.

    Original CSS:

    :root { --padding: 48px; }
    @media (max-width: 900px) { :root { --padding: 32px; } }
    @media (max-width: 600px) { :root { --padding: 24px; } }

    Migrated CSS (StyleX):

    paddingXSection: {
    default: '48px',
    belowMedium: '32px', // under 900
    belowSmall: '24px', // under 600
    }

    Problem (StyleX compiled): Media blocks sorted alphabetically (600 before 900), so now padding: 24px will never be applied.

    :root { --padding: 48px; }
    @media (max-width: 600px) { :root { --padding: 24px; } }
    @media (max-width: 900px) { :root { --padding: 32px; } }

    Solution (StyleX):

    paddingXSection: {
    default: '48px',
    belowMedium: {
    default: '32px',
    belowSmall: '24px',
    },
    }

    Context failure

    When a wave completed, the chat would prompt me for human review and raise issues that need a decision. This thread started to get long and I became concerned about context failure. When you prompt AI, the chat history gets sent along with your latest prompt. All history is context. Context can fail, causing degraded AI results in four ways:

    • ▪ Context Poisoning: a hallucination or other error makes it into the context and is repeatedly referenced.
    • ▪ Context Distraction: context grows so long that the model over-focuses on it, neglecting what it learned during training.
    • ▪ Context Confusion: superfluous information in the context is used by the model to generate a low-quality response.
    • ▪ Context Clash: when you accrue new information and tools in your context that conflicts with other information in the prompt.

    To avoid this, after wave B I modified the spec: When a wave completes, add handoff notes to the spec so a new chat can continue the work. When starting a new wave, I would open a new chat and prompt the AI to continue based on the handoff notes.

    Verification doesn't work

    There was one instance where verification failed. The error was caught by human review. During a wave, changes were made to CSS in v1's directory even though the spec explicitly stated not to modify files outside of the v3 and shared directory. There was also a verification step to check this (see above). AI is probabilistic, not deterministic. The mistake I made was allowing AI to manage verification. Handing verification steps to human control would have caught the error.

    Stats

    • ▪ Changes: +22,164 / - 8,151 over 794 files
    • ▪ Time taken: 8 days from first spec message to last PR merged. Required ~ 20min focus time twice a day once implementation started.
    • ▪ Cost: 552 M tokens, $612.62 (https://cursor.com/dashboard/usage)

    Feedback

    Have any feedback about this note or just want to comment on the state of the economy?

    Where to next?

    AI
    Arrow pointing downYOU ARE HERE
    A Johnny Cab pilot