Inspiration
Most "green code" tools just ask an LLM to eyeball a score. Teams ship performance and energy regressions constantly because nothing measures them automatically and nothing stops them. Software's carbon cost is real at scale — and invisible in ordinary code review. We wanted proof, not vibes, wired directly into GitLab's post-code lifecycle.
What it does
Perf Guardian is a carbon-aware guardian for every merge request, in two cooperating halves:
- Carbon gate (CI/CD): runs the project's workload, measures real CPU → energy → CO₂, computes the Software Carbon Intensity (SCI, Green Software Foundation), compares the change against the target branch apples-to-apples in the same process, fails the pipeline if carbon regresses beyond budget, and posts the before/after SCI numbers on the MR.
- Carbon Guardian (GitLab Duo agent): a custom Duo flow reviews the diff, identifies the carbon-costly patterns (nested loops, string concat in loops, redundant work), applies a behavior-preserving fix and commits it to the MR branch, and explains the change — treating all MR content as untrusted to resist prompt injection. With "Pipelines must succeed", a carbon regression can't be merged until it's fixed. Supervised autonomy: the agent does the work, you approve the merge.
How we built it
A custom GitLab Duo flow (flow registry v1) using native tools (get_merge_request, list_merge_request_diffs, create_commit, create_merge_request_note), triggered on merge-request creation. A dependency-free Node measurement engine (real process.cpuUsage, warm-up + median to resist noise) converts CPU time to energy and SCI; the CI carbon gate reconstructs the target-branch baseline with git show and compares in-process. The same engine is exposed as an MCP server, deployed on Google Cloud Run (scales to zero). MIT licensed, DCO signed-off.
Challenges we ran into
Measurement noise made a naive gate fail on variance → we switched to in-process A/B with warm-up + median. The CI baseline needed the target branch's code → our worktree approach broke on an image without git, so we reconstruct it with git show on a full image. We hardened the agent to treat MR content as untrusted.
Accomplishments that we're proud of
A real end-to-end loop on GitLab: the gate measures and blocks, the agent reviews and commits the fix, the gate confirms it green — with the sustainability gain quantified via SCI, not guessed. The pipeline itself is dependency-free to stay low-carbon.
What we learned
Grounding AI on real measurement beats a black-box score, and transparency (a published SCI formula) builds trust. Splitting "measure & enforce" (CI) from "review & fix" (Duo agent) made each half simple and reliable.
What's next for Perf Guardian
A fully hands-off mode, regional grid-intensity data, language support beyond Node, and a greenest-MR leaderboard for teams.
Built With
- gitlab
- gitlab-c
- gitlab-duo
- javascript
- mcp
- node.js
- software-carbon-intensity