{"record":{"id":"e96475f34d4e490a","repo":"mvanhorn/last30days-skill","slug":"could-not-find-a-unique-discovery-output-filename","errorCode":null,"errorMessage":"Could not find a unique discovery output filename","messagePattern":"Could not find a unique discovery output filename","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"skills/last30days/scripts/last30days.py","lineNumber":1502,"sourceCode":") -> Path:\n    directory = Path(save_dir).expanduser().resolve()\n    directory.mkdir(parents=True, exist_ok=True)\n    extension = \"json\" if emit == \"json\" else \"md\"\n    suffix_part = f\"-{suffix}\" if suffix else \"\"\n    stem = f\"{slugify(domain)}-discover-raw{suffix_part}\"\n    date_str = datetime.datetime.now().strftime(\"%Y-%m-%d\")\n    candidates = [directory / f\"{stem}.{extension}\", directory / f\"{stem}-{date_str}.{extension}\"]\n    candidates.extend(directory / f\"{stem}-{date_str}-{index}.{extension}\" for index in range(1, 100))\n    encoded = rendered.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 output:\n            output.write(encoded)\n        return candidate\n    raise RuntimeError(\"Could not find a unique discovery output filename\")\n\n\ndef _pre_run_prior_state(\n    prior: dict[str, object] | None, run_ref: str\n) -> dict[str, object] | None:\n    \"\"\"Reconstruct the queue state a topic had BEFORE this run identity\n    recorded it.\n\n    A row whose last_run_ref equals THIS run's run_ref was stamped by this\n    very run's own earlier attempt (a finalize retry), so its surface_count\n    already includes this run's surfacing: subtract it and keep the prior's\n    covered state (covered_at intact) so the retry renders exactly like the\n    first attempt did. Only when nothing remains after the subtraction AND\n    the row was never covered is the topic genuinely first-ever (no prior).\n    \"\"\"\n    if not prior or prior.get(\"last_run_ref\") != run_ref:\n        return prior\n    previously = max(0, int(prior[\"surface_count\"]) - 1)","sourceCodeStart":1484,"sourceCodeEnd":1520,"githubUrl":"https://github.com/mvanhorn/last30days-skill/blob/c7460f6114449ddfe6ea3fc2f23c3d910c0e740c/skills/last30days/scripts/last30days.py#L1484-L1520","documentation":"The discovery output writer mirrors save_output(): it O_CREAT|O_EXCL-probes stem.ext, stem-date.ext, then stem-date-1..99.ext and raises RuntimeError if all 100+ candidates exist. Like [8] it means this exact discovery stem was written more than 100 times into the same directory on the same date.","triggerScenarios":"A discovery/monitor loop writing repeatedly to one directory: every candidate filename from the fixed list is taken, each FileExistsError is skipped, and the loop exhausts range(1, 100).","commonSituations":"High-frequency scheduled discovery runs; duplicated retry storms; multiple agents pointed at one shared output dir.","solutions":["Move or purge old discovery outputs for that stem in the directory.","Give each run a distinct directory or stem (timestamped) instead of relying on the -N suffix chain.","Reduce rerun frequency below ~100/day for the same stem."],"exampleFix":"# before\nOUT_DIR=discovery; loop writes stem.md every run\n\n# after\nOUT_DIR=discovery/$(date +%Y%m%d-%H%M); mkdir -p \"$OUT_DIR\"","handlingStrategy":"validation","validationCode":"if sum(1 for _ in directory.glob(f'{stem}*.ext')) >= 100:\n    directory = directory / datetime.datetime.now().strftime('%H%M%S')\n    directory.mkdir(parents=True, exist_ok=True)","typeGuard":null,"tryCatchPattern":"try:\n    out = write_discovery_output(directory, stem, rendered, extension)\nexcept RuntimeError:\n    stem = f'{stem}-{os.getpid()}'\n    out = write_discovery_output(directory, stem, rendered, extension)","preventionTips":["Rotate discovery output directories per run or per hour.","Cap scheduled reruns well below 100/day per stem."],"tags":["filesystem","discovery","save","monitoring"],"backgroundTag":null,"analyzedSha":"c7460f6114449ddfe6ea3fc2f23c3d910c0e740c","analyzedAt":"2026-08-15T03:34:49.540Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}