Inspiration

I use Codex and Copilot constantly at my day job — investigation prompts for bugs, architecture sketches, sometimes whole PRs. It's genuinely fast. It's also easy to approve a diff that looks right without being able to explain why it's right if someone asks. My team lead has said more than once that we need to review AI-generated code more carefully before committing it — for example i once vibecoded huge chunk of project (a few modules) and literally couldn`t progress further after critical bug, i had to investigate previous commits deeply to firstly understand logic/patterns and only after that i could implement new buisness logic.

That's the gap CodeDefense targets. Regular code review checks whether the code looks correct. CodeDefense checks whether the person about to commit it can actually explain it — the design decision behind it, a boundary case it should handle, and what a good test for it would look like. If you can't answer, maybe you shouldn't be shipping it yet.

What it does

CodeDefense runs a short, evidence-grounded technical defense of an exact Git change.

You stage a change and start a defense from the CLI, JetBrains IDE, or Codex plugin. CodeDefense:

  1. captures a bounded snapshot of the staged Git hunks;
  2. separates trusted app instructions from untrusted repo content;
  3. uses GPT-5.6 through the locally authenticated Codex CLI to generate exactly three questions — the design decision, a boundary/counterfactual case, and a test prediction;
  4. evaluates each answer, with at most one adaptive follow-up;
  5. calculates the final score locally in Java, not by the model;
  6. issues a repository-local Change Passport bound to the exact Git fingerprint;
  7. shows which changed hunks were actually referenced, via an Evidence Coverage Map;
  8. flags the Passport as expired if the staged change is edited afterward;
  9. verifies Passport continuity in GitHub Actions without calling a model.

The Passport is bounded metadata plus the assessment result — not the source snapshot. It's evidence that a defense happened, not proof of correctness, security, authorship, or a green light to merge.

How I built it

The core is a Java 21 CLI (Maven, Picocli) with a deliberately locked-down filesystem/Git layer: excluded directories pruned before traversal, symlink boundaries enforced, reads byte-limited, secrets redacted, whole snapshot capped at 30 files / 120 KiB.

GPT-5.6 comes in through the user's existing local Codex CLI login — no OpenAI API key, no SDK dependency in the project (in future possible to add Ollama/OpenAI integration). A dedicated adapter launches Codex with explicit args, bounded stdin/stdout, timeouts, process cleanup, and validated JSON schemas on the way back.

Three surfaces share the same core: a standalone CLI, a JetBrains plugin with an interactive "defense cockpit," and a Codex plugin (skill + advisory Stop hook). A GitHub Actions workflow checks Passport continuity without touching source or calling a model.

I paired with Codex throughout — architecture decisions, privacy-boundary testing, staged-diff edge cases, a nasty Windows launcher bug, and iterating on the JetBrains/Codex integrations. GPT-5.6 also does the actual question generation and answer evaluation at runtime.

Challenges

Binding the conversation to the exact code being defended was the hard part. Reading only the start of a large file wasn't enough — the changed line is often way past that. I switched to bounded, hunk-oriented context instead of a file prefix, so the model actually sees the changed lines and their surrounding context.

Cross-platform process launching was its own fight. Windows npm installs expose Codex through a PowerShell shim; native installs use a different stdin contract. The adapter has to handle both without building shell strings and without leaking prompts into process arguments.

I also wanted the tool to be honest about privacy and trust rather than oversell itself. Repo content is treated as untrusted input, snapshots are bounded and redacted, generated artifacts never include the source snapshot, and CI checks fingerprint continuity — it doesn't claim the AI score proves the code is correct.

IDE integration needed a responsive async bridge: no duplicate sessions from double-clicks, long model calls stay cancellable, and the UI has to clearly distinguish CURRENT / EXPIRED / UNDEFENDED / no-staged-change states.

What I learned

Trust in AI-written code needs to be attached to something concrete — a Passport, a fingerprint — not to a chat transcript you'd have to scroll back through months later to reconstruct.

It also needs both halves working together: the model is good at generating context-aware questions and spotting gaps in an explanation, but it shouldn't be the one deciding the score. That has to be deterministic code, because deterministic code is the part you can actually audit and trust not to move on you.

And the plumbing mattered more than I expected going in. The same defense logic is a lot more useful sitting inside JetBrains and Codex, at commit time and in CI, than living behind a CLI command nobody remembers to run.

What's next

Next up is Delta Defense — comparing consecutive Passports so a re-defense only asks about what actually changed since the last successful one, instead of starting from scratch. After that, deeper Codex and CI integration.

Built With

Share this project:

Updates