# The failure ledger

Every row Machyon couldn't complete is written down, with the reason and the real error. When a run ends, the ledger is the complete list of what's missing.

## Where it is

Two files in the save folder, written only when something failed:

**`failed_downloads.jsonl`**

One JSON object per line. This is the ledger.

**`failed_downloads.txt`**

The same URLs, one per line, for tools that want a plain list.

When a later run or retry resolves every failure, both files are deleted.

## Format

```json
{"url": "https://cdn.example.com/p/131.jpg", "names": ["131.jpg"], "reason": "http_404", "error": "HTTP 404 Not Found"}
{"url": "https://cdn.example.com/p/337.jpg", "names": ["337.jpg", "copy/337.jpg"], "reason": "not_image", "error": "Content-Type text/html, body starts b'<html>not found</html>'"}
```

| Field | Type | Meaning |
| --- | --- | --- |
| `url` | string | The URL as given |
| `names` | list of strings | Every filename (relative to the save folder) that this URL should have produced. Usually one; several when the same URL appeared on several rows |
| `reason` | string | Why: one of the [failure reasons](<https://divyanshusingh96.github.io/machyon/reference/failure-reasons/index.md>) |
| `error` | string, optional | The underlying error behind this reason: the first one seen in the run, such as the HTTP status line or what the body started with. It's an example, so rows sharing a reason share it |
| `attempts` | integer, optional | Requests Machyon has sent for this row so far, added up across runs and retries until it succeeds. Missing for rows that never reached a request (an unusable URL or name) and in ledgers written before 1.2.2 |
| `hedges` | integer, optional | How many of those requests were duplicates sent to a slow URL ([hedging](<https://divyanshusingh96.github.io/machyon/reliability/retries-and-rate-limits/index.md#hedging-no-straggler-tax>)). Never more than 3 for one row, however many times it is retried or resumed. Missing when there were none, and in ledgers written before 1.2.3 |

The ledger is keyed by filename, not URL, so two rows with the same URL but different names are tracked separately.

## Reading it

ledger\_read.py

```python
import json
from collections import Counter

import machyon

machyon.download("urls.csv", "images/")

# One JSON object per line: {"url", "names", "reason", "error"?}
with open("images/failed_downloads.jsonl", encoding="utf-8") as f:
    failures = [json.loads(line) for line in f]

print(Counter(entry["reason"] for entry in failures))
dead = [entry["url"] for entry in failures if entry["reason"] == "http_404"]
print(len(dead), "links are gone for good")
```

From the shell, with [`jq`](<https://jqlang.org/>):

```bash
jq -r .reason images/failed_downloads.jsonl | sort | uniq -c        # count by reason
jq -r 'select(.reason == "http_404") | .url' images/failed_downloads.jsonl > dead.txt
```

Inside a running program you don't need the file at all: [`Downloader.failures`](<https://divyanshusingh96.github.io/machyon/reference/python-api/index.md#machyon.Downloader.failures> "<code class=\"doc-symbol doc-symbol-heading doc-symbol-attribute\"></code>            <span class=\"doc doc-object-name doc-attribute-name\">failures</span>     <span class=\"doc doc-labels\">       <small class=\"doc doc-label doc-label-property\"><code>property</code></small>   </span>") lists the current failures as dicts, [`failure_reasons()`](<https://divyanshusingh96.github.io/machyon/reference/python-api/index.md#machyon.Downloader.failure_reasons> "<code class=\"doc-symbol doc-symbol-heading doc-symbol-method\"></code>            <span class=\"doc doc-object-name doc-function-name\">failure_reasons</span>") counts them, and [`Report.failure_reasons`](<https://divyanshusingh96.github.io/machyon/reference/python-api/index.md#machyon.Report> "<code class=\"doc-symbol doc-symbol-heading doc-symbol-class\"></code>            <span class=\"doc doc-object-name doc-class-name\">Report</span>     <span class=\"doc doc-labels\">       <small class=\"doc doc-label doc-label-dataclass\"><code>dataclass</code></small>   </span>") and `Report.failure_examples` describe the failures of one call. `failure_examples` gives one URL, its error and a hint for every reason, including reasons whose rows were only carried over from an earlier session; those take their example from this file, the lowest URL first. Example URLs never show a `user:password`, and query values that look like secrets (tokens, signatures, keys) appear as `…`.

## How it stays correct

- **Merged, never overwritten.** Each write reads what's on disk, adds this process's failures, and removes only the ones this process resolved. Slices, separate processes and later retries never erase each other's records.
- **Locked while writing.** Writers take a lock file (`failed_downloads.jsonl.lock`). A lock left behind by a killed process is considered stale after a minute and removed.
- **Atomic.** The ledger itself is written to a temporary file and renamed into place, like your images.
- **Self-cleaning.** A row that later succeeds, by a retry or a re-run, is removed from the ledger.

## Acting on it

| Reason | Usually means | Try |
| --- | --- | --- |
| `http_404`, `http_410` | The image is gone | Nothing will fix it. Drop the rows or find a new source |
| `http_400`, `http_401` | The URL or its parameters were rejected, or a login is needed | Check the URL; pass cookies or an `Authorization` header with `headers=` |
| `http_403`, `not_image` | Hotlink protection, a login wall or a bot check | `--referer` with the page the image appears on; [troubleshooting](<https://divyanshusingh96.github.io/machyon/troubleshooting/index.md#403-or-not_image-on-every-url>) |
| `http_429`, `http_5xx`, `timeout` | The server was overloaded or rate limiting | `machyon retry -o images/` later, perhaps with a lower `--max-concurrency` |
| `ssl_certificate` | The certificate couldn't be verified | `machyon doctor <url>`; [troubleshooting](<https://divyanshusingh96.github.io/machyon/troubleshooting/index.md#ssl_certificate-errors>) |
| `interrupted` | The run stopped before this row finished | Run the same command again |
