跳到正文
DEV Community· Akshara A·· 2 小時前AI 評分52

Night Ghost:可在夜間自主編碼的 100% 本地 CLI 智能體

The Autonomous 100% local CLI Agent That Codes While You Sleep

AI 導讀

Night Ghost 是一款由 Ollama 驅動、100% 本地執行的開源 CLI 智能體,可根據任務備忘錄在夜間於獨立分支編碼及驗證改動。它在離線沙盒中執行 pytest,提交通過驗證的步驟,若測試回歸便自動回滾,並從執行日誌產生早晨報告。

正文

Akshara A

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

Night Ghost 👻: The Autonomous Agent That Codes While You Sleep

"I stopped halfway" → "I woke up further ahead."


What I Built

Every developer and indie hacker knows the evening exhaustion of leaving code half-finished. You hit a wall at 11 PM: functions lack edge-case tests, helper logic is duplicated, and your working tree is messy. By the time you wake up the next morning, context and momentum have vanished. The first hour of your day is spent re-orienting: "Where was I? What broke? What was I about to test?"

Night Ghost is an open-source, 100% local CLI agent powered by Ollama designed to bridge that evening-to-morning gap. It is quite literally a benevolent ghost in your machine:

  • 100% Local & Offline: All inference runs directly on your machine through Ollama. No source code, diffs, or credentials ever leave your device.
  • Air-Gapped Egress: The test runner actively blocks network access during test runs.
  • Deterministic Guardrails: Security and path boundaries are enforced in Python code, not by trusting system prompts.
  1. In the Evening: You drop a quick task note before closing your laptop:
   nightghost note "Add unit tests for charge() edge cases and clean up duplicated currency rounding"
   nightghost sleep
  1. Overnight (While You Sleep): Night Ghost wakes up (triggered via local cron/timer), reads your note and recent diffs, creates an isolated branch (nightghost/YYYY-MM-DD), and runs an autonomous, test-gated pipeline:
    • Plans small, verification-gated steps.
    • Proposes test additions and safe, behavior-preserving cleanups.
    • Evaluates every write against strict security guardrails in Python code.
    • Runs your test suite (pytest) in a sandboxed, offline environment.
    • Commits every verified step (ghost: <step>). If any test regresses, it automatically rolls back.
    • Compiles a transparent morning report from measured execution logs.
  2. In the Morning: You open your laptop, review the factual report and the clean diff, and decide whether to merge or discard:
   nightghost report
   git diff main..nightghost/2026-11-01

Demo

1. Terminal Workflow (Local CLI)

Watch Night Ghost wake up, analyze the task, run tests, and commit progress autonomously:

$ cd demo_project
$ nightghost status

┌────────────────────────────────┬──────────────────────┐
│ Property                       │ Value                │
├────────────────────────────────┼──────────────────────┤
│ Current Git Branch             │ main                 │
│ Uncommitted Changes            │ Clean                │
│ Scheduled Time                 │ 01:00                │
│ Configured Model               │ gemma4:e2b (ollama)  │
│ Test Command                   │ pytest -q            │
│ Ollama Server                  │ Online               │
│ Model Status                   │ Ready (Installed)    │
│ Kill Switch (.nightghost/STOP) │ Disengaged (Ready)   │
└────────────────────────────────┴──────────────────────┘

$ nightghost note "Add comprehensive unit tests for payment edge cases"
$ nightghost run

┌────────── Overnight Run ──────────┐
│ Night Ghost Active                │
│ Model: gemma4:e2b (ollama)        │
│ Mode: LIVE RUN                    │
└───────────────────────────────────┘

[OK] Pipeline completed!
Branch: nightghost/2026-10-31
Steps Planned: 2
Steps Executed: 2
Elapsed: 142.7s

$ nightghost report

2. Sample Morning Report Output

Every claim in the morning report is compiled directly from actions.jsonl and git diff stats—not model hallucinations:

# Night Ghost Report: 2026-10-31
Task: "Add comprehensive unit tests for charge() and refund() edge cases"
Branch: nightghost/2026-10-31 (2 commits, 2 files, +38 / -6)
Model: gemma4:e2b (local) | Runtime: 2m 22s

## Result at a glance
[PASS] Tests: 2 -> 6 passing (0 failing)

## What I did
1. Added unit tests for charge() negative amounts raising ValueError: commit a1f4d92
2. Added unit tests for refund() invalid transaction IDs: commit 8c2b1e4

## Needs your judgment
- None. All committed changes are self-contained and behavior-preserving.

## Skipped / failed
- None. All attempted steps passed validation.

## Blocked by guardrails
- None. All file and command requests adhered to security rules.

## How to review
git diff main..nightghost/2026-10-31
nightghost discard   # to throw it all away

3. Dry-Run Simulation Mode

Night Ghost also provides a simulation mode so you can preview planned steps and check baseline tests without writing files or making commits:

nightghost run --dry-run
nightghost report

Code

Architecture at a Glance

┌────────────┐   ┌───────────┐   ┌──────────────┐   ┌─────────────┐
│ Task note  │──▶│  Planner  │──▶│  Executor    │──▶│  Reporter   │
│ + config   │   │ (Open LLM)│   │ (LLM + tools)│   │ (log→MD)    │
└────────────┘   └───────────┘   └──────┬───────┘   └─────────────┘
                                        │
                  ┌─────────────────────┼─────────────────────┐
                  ▼                     ▼                     ▼
            ┌──────────┐         ┌────────────┐        ┌────────────┐
            │ Guardrail│         │ Test Runner│        │ Git Manager│
            │ (paths,  │         │ (sandboxed │        │ (branch,   │
            │  budgets)│         │  subproc)  │        │  commits)  │
            └──────────┘         └────────────┘        └────────────┘
                                        │
                                  ┌─────▼──────┐
                                  │ Local LLM  │
                                  │ (Ollama)   │
                                  └────────────┘

Key Engineering Components:

  • Guardrail Engine (guardrail.py): All writes are path-checked in deterministic code (resolving symlinks and ..). Denylist precedence protects .env*, migrations/**, *.pem, *.key, and lockfiles. Detects secrets and blocks command injection vectors (;, &&, |, backticks, $(), newlines).
  • Assertion Tampering Protection: Hard rule preventing models from "fixing" tests by weakening them. If an existing test assertion is altered or deleted, the write is instantly rejected with EXISTING_ASSERTION_MODIFIED.
  • Git Isolation (gitmanager.py): Never touches main or leaves your working tree dirty. Snapshots uncommitted work with git stash create, works on nightghost/YYYY-MM-DD, makes atomic ghost: <step> commits, and supports one-command rollback with nightghost discard.
  • Test Runner (testrunner.py): Runs pytest in a sandboxed subprocess with proxy poisoning (http_proxy=http://127.0.0.1:0, PIP_NO_INDEX=1) to enforce zero network egress.
  • Test Suite: 33 automated tests (pytest) covering every denial path, symlink escape, budget constraint, and pipeline execution.

How I Built It

100% Local Inference via Ollama

  • Zero Cloud Dependence: Night Ghost is built strictly for 100% local, offline execution using Ollama. It runs completely disconnected from the internet, communicating over localhost:11434.
  • Open-Weight Coder Models: Fully tested and optimized with open models including:
    • Gemma 4 (gemma4:e2b / gemma4:e4b): Ultra-efficient reasoning on modest consumer hardware.
    • Qwen 2.5 Coder (qwen2.5-coder:14b / 7b): Deep context understanding and accurate test generation.
    • Llama 3 / DeepSeek-Coder-V2: Advanced multi-file refactoring.

The Agent Harness

  • Python 3.11+, Typer, and Rich for a terminal UI.
  • Pydantic v2 and PyYAML for strict schema validation in nightghost.yaml.
  • Jinja2 for compiling transparent morning reports from JSONL action logs.

Why Does Open Innovation Matter?

  1. 100% Local Code Privacy via Ollama: Developers cannot and should not send their proprietary codebases, uncommitted diffs, or corporate repositories to third-party closed cloud APIs while they sleep. Open innovation makes running open-weight models 100% locally via Ollama viable on everyday laptops, ensuring not a single byte of code or credentials ever leaves your machine.
  2. Deterministic Security Over Model Promises: Closed-source agent frameworks often rely on "system prompt polite requests" not to touch sensitive files. Night Ghost enforces security in deterministic open-source code—every path check, symlink resolution, and assertion integrity check is auditable, testable, and mathematically verified.
  3. No Per-Token Metering: An autonomous multi-turn overnight loop executing dozens of test iterations would rack up unpredictable API bills on closed models. Running open-weight models 100% locally via Ollama empowers indie developers to run autonomous agents overnight completely free.

The Spirit of It

There is a distinct, quiet magic in closing your laptop with a half-solved problem, going to sleep, and waking up with your morning coffee to find:

[PASS] Tests: 2 -> 6 passing (0 failing)
1. Added unit tests for charge() negative amounts: commit a1f4d92

The dreaded edge cases you didn't have the energy to write at midnight are written, tested green, and neatly committed on an isolated branch. You didn't lose your morning momentum—you woke up further ahead. That is the spirit of a benevolent Ghost in the Machine.


Prize Categories

  • Hacktoberfest Open-Source AI Challenge Week 4: Ghost in the Machine
  • Best Use of Open-Weight Models (Ollama / Gemma / Llama / Qwen)
  • Developer Productivity & Autonomous Developer Tooling

來源:DEV Community · dev.to