DEV Community

Waleed Shamhan
Waleed Shamhan

Posted on Edited on

Ghost in the Machine: Necromancing Dead Codebases Locally with Gemma 3 & Ollama

Hacktoberfest Open-Source AI Challenge: Ghost in the Machine Submission 👻🌼

This is a submission for the Hacktoberfest Open-Source AI Challenge Week 4: Ghost in the Machine

Disclosure: this project was built with AI assistance — a local Gemma 3 model performed the code autopsy and drafted rewrites through Ollama, and a human verified every finding, rejected the invented ones, and wrote the tests. No cloud AI service was used.

Ghost in the Machine: Necromancing Dead Codebases Locally with Gemma 3 & Ollama

Introduction

Every developer’s GitHub profile is a graveyard. We've all got legacy repositories—abandoned scripts, forgotten utilities, and stale experiments written years ago that now sit gathering digital dust. For this year’s Hacktoberfest (Ghost in the Machine challenge), instead of building a trivial app, I decided to build something that resurrects the dead: The Legacy Code Necromancer.

It’s a local-first, zero-dependency CLI tool built in pure Python that uses Gemma 3 via Ollama to perform an autopsy on dead code, refactor it safely, and generate a haunted markdown audit report.

The Architecture: Privacy-First Code Necromancy

When you're dealing with legacy code, the biggest fear is leaking hardcoded API keys, database credentials, or sensitive secrets to cloud LLMs. That's why The Legacy Code Necromancer was built with strict constraints:

  1. Local Loopback Only: It communicates strictly with 127.0.0.1 where Ollama runs. Any remote server connection is strictly rejected unless explicitly authorized.
  2. Secret Scrubber: Before a single line of code is sent to Gemma 3, a built-in regex scrubber purges any potential keys, tokens, or sensitive strings, leaving the structural vulnerabilities (eval, pickle, etc.) exposed for analysis.

The Three Engines of the Necromancer

The tool runs three distinct phases against any legacy directory:

  • Phase 1: Code Autopsy: Scans the file tree (safely ignoring .git, node_modules, and binaries) and produces structured findings as JSON — severity, category, file path, and line — rendered into the Markdown report.
  • Phase 2: Revival & Patching: Refactors the haunted code into a clean, modern, testable structure (with syntax verification for Python targets).
  • Phase 3: The Haunted Audit Report: Generates a chilling, Halloween-themed Markdown report detailing the cause of death and the resurrection steps, complete with a persistent JSON session manifest (.necromancer-manifest.json) to resume work without duplication.

The Recorded Séance — what actually happened

No claims without evidence. On 30 October 2026 I ran the tool over the included haunted sample (two Python files, 57 lines, fake credentials planted deliberately) with gemma3:1b running locally on CPU through Ollama. The run took 404 seconds, 5 model calls, 16,388 characters sent, 18,208 received — and every raw reply is preserved.

The autopsy returned 15 findings. Human review confirmed 8 — including every planted critical flaw: the hardcoded credentials, eval, pickle deserialization, shell injection, SQL string interpolation, MD5 password hashing, and the removed time.clock / collections.Mapping APIs. The other 7 were invented or mislabeled — a "race condition" in single-threaded code, a "hardcoded credential" in a file that has none, "missing retries/backoff" for local file code — and the model's line numbers were guesses, not real line numbers. I rejected those, and the report says so.

The rewrite pass was the honest disappointment of the run: the small model's drafts echoed the redacted input verbatim — eval, pickle, SQL interpolation, MD5 and time.clock all survived. "Verified" in the report means only "compiles". Those drafts are not safe to run, and the report's "Cause of Death" prose (which invents a 2023 date and fixes that never happened) is preserved as evidence of what a small local model produces — not as documentation. The full annotated report, with a human curator's note on top, is in the repository at docs/example-haunted-audit-report.md.

That is the real story: the autopsy is strong, the curation is the product, and the audit trail is what makes it trustworthy.

Why Gemma 3?

The recorded run used gemma3:1b (806 MB) because this machine is RAM-constrained — and it delivered exactly the balance I needed for the autopsy: fast enough on CPU, with enough structural reasoning to understand chaotic, unmaintained legacy code, without sacrificing a single byte of code privacy. The tool accepts any local Gemma 3 tag; the 4B gemma3 is supported and is the recommended next run for stronger rewrites (untested on this machine).

Try the Haunted Sample Yourself

Clone the repository, feed it the included haunted sample directory containing fake secrets and old bugs, and watch the necromancy unfold locally:

ollama serve
ollama pull gemma3:1b

python3 necromancer.py fixtures/haunted-legacy --model gemma3:1b
Enter fullscreen mode Exit fullscreen mode

Demo — the recorded séance

This is the real console output of the run recorded on 30 October 2026, on CPU, against the included haunted sample:

Exhumed 2 file(s), 15 finding(s), 2 resurrected, 0 failed.
Report: necromancer-out/haunted-audit-report.md
Revived code: necromancer-out/revived
Enter fullscreen mode Exit fullscreen mode

What the generated report recorded (verbatim vitals):

Measure Value
Source files examined 2
Total lines 57
Secret lines redacted before prompting 2
Findings 15 (4 Critical · 4 High · 4 Medium · 3 Low)
Files resurrected 2 (drafts compile; curator review found they echo the input — see below)

A sample of the confirmed findings: hardcoded credential (Critical), eval() code execution, pickle deserialization, SQL string interpolation, MD5 password hashing, and the removed time.clock API. The full annotated report, with the human curator's note on what the model invented and what the rewrite drafts failed to fix, is in the repository at docs/example-haunted-audit-report.md.

Code

GitHub logo waleedshamhan2050-sys / legacy-code-necromancer

Local-first CLI that autopsies abandoned code, drafts safe rewrites, and writes a haunted audit report — powered by Gemma 3 via Ollama. No cloud, no secrets leave localhost.

👻 The Legacy Code Necromancer

Hacktoberfest 2026 — "Ghost in the Machine" challenge entry, Best Use of Gemma category. A local-first CLI that dissects abandoned code, resurrects it with Gemma 3 running entirely on your own machine through Ollama, and writes a haunted audit report you can verify line by line No cloud. No API keys. No code ever leaves localhost.

Every codebase has a graveyard: the utility written in 2019 that everyone is afraid to touch, the intern's bot, the script with the password still in it. The Necromancer does not delete the dead — it performs an autopsy, determines the cause of death, and brings the code back in a form that is safe to run. The ghost in the machine is not something to fear. It is something to understand.

The pipeline — three engines, one séance

abandoned project ──► 1. CODE AUTOPSY ──► 2. AUTO-REFACTORING
…

Why Does Open Innovation Matter?

Because a closed API would have made this project impossible in the way that mattered. Abandoned code is the most personal code a developer owns — half-finished, half-secret, full of old credentials. Open-weight Gemma 3 running locally meant the autopsy never depended on a service that can disappear, change its prices, or read my code. Anyone can rerun the identical séance offline and audit every finding against the preserved raw output. A closed model gives you an answer; an open model gives you a process you can inspect. For a student on a metered connection in Yemen, that is the difference between a ghost story and a resurrection.

The Spirit of It

Día de los Muertos is about honouring the dead without fearing them. This tool does the same for old code: you do not delete your past work because it is flawed. You sit with it, understand it, and bring it back better — with a local model that never judges, never leaks, and never leaves the room.

Prize Categories

  • Best Use of Gemma — Gemma 3 (via Ollama, local inference) is the engine of the entire project: autopsy, rewrite drafts, and report prose all run through the open-weight model on localhost.

Top comments (0)