{"record":{"id":"f9a110d82ef76ea9","repo":"mvanhorn/last30days-skill","slug":"save-output-could-not-find-a-unique-filename-aft","errorCode":null,"errorMessage":"_save_output: could not find a unique filename after 101 attempts in {path}","messagePattern":"_save_output: could not find a unique filename after 101 attempts in (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"skills/last30days/scripts/lib/hosted.py","lineNumber":232,"sourceCode":"    extension = \"json\" if emit == \"json\" else \"md\"\n    suffix_part = f\"-{suffix}\" if suffix else \"\"\n    base = path / f\"{slug}-raw{suffix_part}.{extension}\"\n    date_str = datetime.now().strftime('%Y-%m-%d')\n    candidates = [base]\n    candidates.append(path / f\"{slug}-raw{suffix_part}-{date_str}.{extension}\")\n    for i in range(1, 100):\n        candidates.append(path / f\"{slug}-raw{suffix_part}-{date_str}-{i}.{extension}\")\n    encoded = content.encode(\"utf-8\")\n    for candidate in candidates:\n        try:\n            fd = os.open(candidate, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o644)\n        except FileExistsError:\n            continue\n        with os.fdopen(fd, \"wb\") as f:\n            f.write(encoded)\n        return candidate\n    # Fallback: all 101 candidates existed (extremely unlikely).\n    raise RuntimeError(\n        f\"_save_output: could not find a unique filename after 101 attempts in {path}\"\n    )\n\n\ndef _render_complete(row: dict, topic: str, emit: str, save_dir, save_suffix: str) -> int:\n    synthesis = row.get(\"synthesis_text\") or \"\"\n    raw_markdown = row.get(\"raw_markdown\") or \"\"\n    if emit == \"json\":\n        payload = {\n            key: row.get(key)\n            for key in (\"id\", \"status\", \"synthesis_text\", \"raw_markdown\")\n            if key in row\n        }\n        rendered = json.dumps(payload, indent=2, sort_keys=True)\n        save_content = rendered\n    else:\n        # The server report is the content source; it already synthesized.\n        # All markdown-ish emit modes print the synthesis text as-is.","sourceCodeStart":214,"sourceCodeEnd":250,"githubUrl":"https://github.com/mvanhorn/last30days-skill/blob/c7460f6114449ddfe6ea3fc2f23c3d910c0e740c/skills/last30days/scripts/lib/hosted.py#L214-L250","documentation":"Raised by _save_output in hosted.py when all 101 candidate filenames (the base name plus suffixes -1..-100) already exist in the save directory. The writer uses os.open with O_CREAT|O_EXCL to guarantee atomic no-clobber creation; if every candidate collides it gives up rather than overwrite. In practice this means the same slug/date/extension combination has been saved ~100 times.","triggerScenarios":"A loop or cron job re-publishing the identical topic slug with the same save_suffix and date more than 100 times in one day; an automation bug calling _save_output in a tight loop; a hostile/accidental pre-creation of all candidate names.","commonSituations":"A retry storm in a hosted batch renderer; scheduled runs that regenerate the same topic hourly; someone seeded the directory with blocking filenames. The code comment itself notes it is 'extremely unlikely' in normal use.","solutions":["Delete or archive the existing 100+ collision files for that slug in the save directory.","Vary the inputs: use a distinct --save-suffix, topic slug, or output directory for repeated runs.","Fix the caller that re-saves the identical artifact in a loop (the collision count is the symptom, the loop is the bug)."],"exampleFix":"# before: repeated identical invocations\nfor i in range(150):\n    run(slug=\"my-topic\", suffix=\"\", out_dir=same_dir)  # exhausts -1..-99\n\n# after: unique suffix per run\nfor i in range(150):\n    run(slug=\"my-topic\", suffix=f\"v{i}\", out_dir=same_dir)","handlingStrategy":"fallback","validationCode":"from pathlib import Path\n\ndef can_save(path: Path, base_candidates: list[str]) -> bool:\n    return not all((path / c).exists() for c in base_candidates)","typeGuard":null,"tryCatchPattern":"try:\n    out = _save_output(...)\nexcept RuntimeError as e:\n    if 'unique filename' in str(e):\n        # switch save_dir or make the slug/suffix unique, then retry once\n        out = _save_output(..., suffix=f\"{suffix}-{run_token}\")","preventionTips":["Make repeated runs unique by construction: include a run token or timestamp in save_suffix.","Alert when a single slug/date accumulates dozens of files — that indicates a retry loop bug.","Never rely on the 100-deep collision suffix; treat >3 collisions as a caller bug."],"tags":["filesystem","atomic-write","collision","automation"],"backgroundTag":null,"analyzedSha":"c7460f6114449ddfe6ea3fc2f23c3d910c0e740c","analyzedAt":"2026-08-15T03:34:49.540Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}