> Agent-ready spec from Diff of the Day (2026-11-29, idea 2). Give this file to your coding agent (Claude Code, Codex, Cursor) and ask it to build the P0 requirements first.

# Spec: Local GGUF model scanner

## Problem Statement
On 26 Nov 2026 a scan of 3,273,389 public Hugging Face model repos found five malicious GGUF models with 2,428 downloads. One campaign hid a Windows trojan after the last tensor and rewrote the chat template so the model asks the user to run the unpacking command. Anyone who downloads local models has no quick way to check the files they already have.

## Goals
- Scan every .gguf file under a folder and report suspicious chat templates and trailing bytes.
- Flag 100% of the patterns described in the Malspark write-up in test fixtures.
- Scan a 10 GB folder in under a minute without loading weights into memory.
- Never execute or render anything from the scanned file.

## Non-Goals
- Replacing antivirus: this checks model structure only.
- Judging model quality or licence: out of scope.
- Rebuilding clean files in v1: report first, repair later.

## User Stories
- As a developer running local models, I want one command that lists risky files so that I can delete them.
- As a team lead, I want a CI check on a shared model cache so that a poisoned file never reaches laptops.
- As a developer, I want to see the exact template lines that triggered a warning so that I can judge them myself.
- Edge case: as a developer, I want files with unusual but harmless padding reported as info, not alerts, so that I am not flooded.

## Requirements
### P0 (must have)
1. **Header parser**: read the GGUF header, metadata table and tensor table, and compute where the last tensor ends.
   - [ ] Given a file with bytes after the expected end, when scanned, then the report shows the extra byte count.
2. **Template checks** on tokenizer.chat_template: instructions to call shell or PowerShell tools, hidden-window flags, download commands, and text telling the model never to mention something.
   - [ ] Each rule has a unit test with a fixture string.
3. **Report**: one line per file with status ok, info or alert, and a JSON output option.
4. **Read-only**: open files read-only and never render the Jinja template.

### P1 (nice to have)
- Compare a template against the base model's known template and show a diff.
- Also check tokenizer_config.json and chat_template.jinja next to safetensors models.

### P2 (future)
- Rebuild a clean file with the same weights.

## Success Metrics
- Leading: all fixtures for the described patterns are flagged, with no alerts on five clean popular models.
- Lagging: the scanner runs weekly on your model folder.

## Open Questions
- Which runtimes' model folders to scan by default (llama.cpp, LM Studio, Ollama)? (you)

## Timeline
One evening for P0.

## Agent Instructions
- **Suggested stack**: Python 3.12 with the gguf package or a small hand-written header parser.
- **Build order**: header parser with tests on a tiny generated GGUF → trailing-bytes check → template rules → report.
- **Safety**: never execute, import or render content from scanned files; do not upload files anywhere.
- **Done when**: the scanner flags every fixture and passes clean models.

## Sources
- GGUF LLM Models with Hidden Malware on Hugging Face: https://malspark.com/blog/malicious-gguf-models-hugging-face/
