Metadata-Version: 2.5
Name: mojiblame
Version: 0.1.0
Summary: Git-aware encoding forensics: find which commit corrupted your text, prove every fix byte-exact, recover what other tools call unrecoverable.
Project-URL: Homepage, https://github.com/Golemozan/mojiblame
Project-URL: Repository, https://github.com/Golemozan/mojiblame
Project-URL: Issues, https://github.com/Golemozan/mojiblame/issues
Author: Ozan
License: MIT
License-File: LICENSE
Keywords: cp1252,encoding,forensics,git,mojibake,pre-commit,utf-8,windows
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Quality Assurance
Classifier: Topic :: Text Processing :: General
Requires-Python: >=3.10
Requires-Dist: charset-normalizer>=3.3
Requires-Dist: ftfy>=6.2
Requires-Dist: rich>=13.7
Requires-Dist: typer>=0.12
Description-Content-Type: text/markdown

# mojiblame

<!-- mojiblame:ignore-file — this README intentionally contains damaged encoding examples. -->

**Git-aware encoding forensics.** Find the commit that corrupted your text, prove every fix
byte-exact, and recover characters other tools call unrecoverable.

[![test](https://github.com/Golemozan/mojiblame/actions/workflows/test.yml/badge.svg)](https://github.com/Golemozan/mojiblame/actions/workflows/test.yml)
[![PyPI](https://img.shields.io/pypi/v/mojiblame)](https://pypi.org/project/mojiblame/)
[![Python](https://img.shields.io/pypi/pyversions/mojiblame)](https://pypi.org/project/mojiblame/)
[![License: MIT](https://img.shields.io/badge/license-MIT-green)](LICENSE)

![mojiblame — git-aware encoding forensics](docs/assets/hero.png)

```console
$ mojiblame scan .

damage    proof       file             detail
lossy     unprovable  docs/kurulum.md  19 character(s) destroyed (U+FFFD) - try: mojiblame blame
mojibake  byte-exact  src/tr.json      latin-1 -> utf-8, 1 layer(s), reversal verified byte-exact

  2 scanned  ·  2 damaged  ·  1 fixable now  ·  1 needs history (mojiblame blame <file>)
```

---

## The problem

AI coding agents read and write your files through shells and subprocesses. On Windows —
and anywhere the default codepage is not UTF-8 — that pipeline silently mangles non-ASCII
text. Nobody notices, because nothing fails: the agent writes `Ã¶` instead of `ö`, the patch
context stops matching, a hook prints garbage, and the damage sits in the repo for weeks.

This is not hypothetical. It has been reported against
[Codex on Windows](https://github.com/openai/codex/issues/4131) — Turkish characters coming
back wrong from `Get-Content`/`Set-Content` — and again as
[mojibake breaking patch application](https://community.openai.com/t/codex-on-windows-utf-8-mojibake-and-patch-context-mismatch-issues/1384116),
where the agent can no longer edit a file because the bytes it read are not the bytes on disk.

Existing tools look at a file **as it is now** and **guess**. `ftfy` is an excellent library
for repairing a string, but it does not scan a repository, does not know your git history,
and cannot tell you whether its repair is correct or merely plausible.

![How silent codepage corruption enters a repository](docs/assets/pipeline.svg)

## What mojiblame does differently

### 1. It separates three kinds of damage that other tools blur together

| | What happened | Fix | Standard of proof |
|---|---|---|---|
| **wrong-codec** | The bytes are not UTF-8 at all (cp1254, cp1251, latin-1…). The text is intact; only the codec is wrong. | transcode | **reversible** — re-encoding restores the original bytes. The codec itself is inferred, and we say so. |
| **mojibake** | The file *is* valid UTF-8, but the characters are wrong: UTF-8 was decoded as a legacy codepage and re-encoded. `ö` → `Ã¶`. Stacks to several layers. | repair | **byte-exact** — see below |
| **lossy** | `U+FFFD` replacement characters. The bytes were destroyed at decode time. They are not in the file any more. | *none* | **unprovable from the file** — only history can help |

The distinction is not academic. A tool that treats the third case like the first will happily
"fix" a file and hand you plausible fiction.

### 2. Every fix is proven, or it is not applied

For mojibake we take the repair plan, **invert it**, and re-apply the inversion to the repaired
text. If that reproduces the bytes currently on disk exactly, the repair is not a guess: the
recovered text is a verified preimage of the file as it stands, not the most plausible-looking
candidate.

If the inversion does not reproduce them, mojiblame reports the finding and **refuses to write**:

```console
mojibake  unprovable  README.md   looks like partially decoded UTF-8 mojibake,
                                  but the reversal is not exact - not fixing
```

No proof, no write. That rule has no exceptions.

### 3. It recovers what the file alone cannot

`U+FFFD` means the bytes are gone, and no amount of cleverness gets them back out of the file.
But git kept every earlier version. `mojiblame blame` walks that history, finds the commit where
the damage appeared, works out which decode step caused it, and restores the destroyed
characters from the last clean version:

```console
$ mojiblame blame docs/kurulum.md

kurulum.md

  19 character(s) destroyed (U+FFFD)
  corruption entered   03bbd41  2026-09-02  chore: toplu yeniden bicimlendirme
  last clean version   c6455a1  2026-09-02  docs: kurulum notlari eklendi
  transform            cp1254 -> utf-8
  recoverable          19 / 19  (proof: re-applying the transform reproduces this file exactly)
```

That last line is the point. We reconstruct the corruption, apply it to the recovered text, and
check it reproduces today's file byte for byte. If it does, the recovery is a deduction, not a
best guess — and mojiblame says which one it is either way.

**It splices, it does not revert.** `git checkout <old-sha> -- file` would throw away every
legitimate edit made since the damage. mojiblame keeps today's file and replaces only the runs
of destroyed characters.

![mojiblame scan, blame, and fix terminal demo](docs/assets/blame-demo.gif)

## Install

```console
uv tool install mojiblame     # or: pipx install mojiblame
```

## Use

```console
mojiblame scan [PATH]     read-only report, with the standard of proof for each finding
mojiblame blame <FILE>    which commit broke it, which step, what history can restore
mojiblame fix [PATH]      apply only the provable fixes; --dry-run, --yes
mojiblame undo            restore the originals from the last fix
mojiblame guard [PATH]    exit non-zero if anything is corrupted (CI, hooks)
mojiblame init            wire it into pre-commit, GitHub Actions, or your agent
```

`fix` journals the original bytes before it changes anything, so `undo` is byte-identical —
not "close enough". Inside a git repo, scanning honours `.gitignore`.

## Wire it in

**pre-commit** — the version most people should use, because it runs without anyone remembering it:

```yaml
repos:
  - repo: https://github.com/Golemozan/mojiblame
    rev: v0.1.0
    hooks:
      - id: mojiblame
```

**GitHub Actions** — `mojiblame init --github`, or:

```yaml
- run: pip install mojiblame
- run: mojiblame guard .
```

**Coding agents** — `mojiblame init --claude` installs a `PreToolUse` hook that inspects writes
*before they reach the disk* and blocks the ones that would introduce corruption. Cleaning up
afterwards is the losing half of this problem; the agent is told what it was about to corrupt
and can retry correctly:

```
mojiblame blocked this write to notes.md: mojibake (cp1254 -> utf-8, 1 layer(s)).
The text was decoded with the wrong codec somewhere upstream.
Intended text starts: 'yönetici İŞ şğüöç'
```

## Suppression

Some files contain broken characters on purpose — documentation about encoding bugs, test
fixtures, golden files. Two ways to say so:

```gitignore
# .mojiblameignore
fixtures/
*.snapshot
```

or put `mojiblame:ignore-file` anywhere in the file. The marker is matched against raw bytes,
so it still works in files that do not decode cleanly — which is exactly where you need it.

## Limitations

Stated plainly, because a forensics tool that oversells itself is worse than none:

- **Codec identification is statistical.** `latin-1` decodes every possible byte sequence, so a
  clean round-trip proves the transcode is reversible, not that the codec was right. mojiblame
  reports those as two separate facts and never merges them into one confident claim.
- **The codec *name* on a mojibake finding may be one of several.** Where two codepages map the
  characters in a file identically, they are indistinguishable from the bytes — corruption caused
  by cp1254 is reported as `latin-1 -> utf-8` when the text contains nothing that separates them.
  The recovered text is the same either way, and it is the part that gets verified; only the label
  is ambiguous.
- **Destroyed characters with no clean ancestor stay destroyed.** If a line was first written
  already broken, no earlier version exists and mojiblame will tell you it recovered `0 / n`
  rather than inventing text.
- **History walking assumes damage, once introduced, persists.** That assumption is verified,
  and the search falls back to a linear scan when a file was broken, fixed, and broken again.
- Files over 5 MB are skipped by default.

## How it works

```
detect/facts.py       BOM, line endings, strict UTF-8 decode - facts, no inference
detect/codecs.py      D1: which legacy codec, and is the transcode reversible
detect/mojibake.py    D2: repair plan, layer depth, and the inverse-plan proof
gitfx/repo.py         git, always via bytes - never text=True, never the console codepage
gitfx/forensics.py    binary-searches history for the commit that introduced the damage
gitfx/recover.py      splices destroyed runs from the clean version, keeping later edits
fixing/journal.py     gzipped originals so undo is byte-identical
hooks/                pre-commit entry point and the agent PreToolUse guard
```

One detail worth calling out: `gitfx/repo.py` never uses `subprocess(text=True)`. That decodes
with the console codepage — cp1254 on a Turkish Windows box — which is precisely the bug class
this tool hunts. A forensics tool that mangles its own evidence is worthless, so every git call
returns bytes and decoding is always an explicit, local decision.

## Why this exists

A memory system I run injected context into every session for months. It was writing `y���n�`
where it meant `yönetici`, and nothing ever failed — the sessions just quietly got a little
worse. The cause was a PowerShell 5.1 hook writing stdout in the console codepage.

Finding it took an afternoon. Knowing *when* it started took git archaeology by hand, and by
then the answer to "can any of this be recovered?" was pure guesswork. mojiblame is that
afternoon, made repeatable.

Its first real run, against that same 437-file vault, found four damaged files and named the
commit responsible for each.

## License

MIT
