mvanhorn/last30days-skill · error · RuntimeError

Could not find a unique discovery output filename

Error message

Could not find a unique discovery output filename

What it means

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.

Source

Thrown at skills/last30days/scripts/last30days.py:1502

) -> Path:
    directory = Path(save_dir).expanduser().resolve()
    directory.mkdir(parents=True, exist_ok=True)
    extension = "json" if emit == "json" else "md"
    suffix_part = f"-{suffix}" if suffix else ""
    stem = f"{slugify(domain)}-discover-raw{suffix_part}"
    date_str = datetime.datetime.now().strftime("%Y-%m-%d")
    candidates = [directory / f"{stem}.{extension}", directory / f"{stem}-{date_str}.{extension}"]
    candidates.extend(directory / f"{stem}-{date_str}-{index}.{extension}" for index in range(1, 100))
    encoded = rendered.encode("utf-8")
    for candidate in candidates:
        try:
            fd = os.open(candidate, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o644)
        except FileExistsError:
            continue
        with os.fdopen(fd, "wb") as output:
            output.write(encoded)
        return candidate
    raise RuntimeError("Could not find a unique discovery output filename")


def _pre_run_prior_state(
    prior: dict[str, object] | None, run_ref: str
) -> dict[str, object] | None:
    """Reconstruct the queue state a topic had BEFORE this run identity
    recorded it.

    A row whose last_run_ref equals THIS run's run_ref was stamped by this
    very run's own earlier attempt (a finalize retry), so its surface_count
    already includes this run's surfacing: subtract it and keep the prior's
    covered state (covered_at intact) so the retry renders exactly like the
    first attempt did. Only when nothing remains after the subtraction AND
    the row was never covered is the topic genuinely first-ever (no prior).
    """
    if not prior or prior.get("last_run_ref") != run_ref:
        return prior
    previously = max(0, int(prior["surface_count"]) - 1)

View on GitHub (pinned to c7460f6114)

Solutions

  1. Move or purge old discovery outputs for that stem in the directory.
  2. Give each run a distinct directory or stem (timestamped) instead of relying on the -N suffix chain.
  3. Reduce rerun frequency below ~100/day for the same stem.

Example fix

# before
OUT_DIR=discovery; loop writes stem.md every run

# after
OUT_DIR=discovery/$(date +%Y%m%d-%H%M); mkdir -p "$OUT_DIR"
Defensive patterns

Strategy: validation

Validate before calling

if sum(1 for _ in directory.glob(f'{stem}*.ext')) >= 100:
    directory = directory / datetime.datetime.now().strftime('%H%M%S')
    directory.mkdir(parents=True, exist_ok=True)

Try / catch

try:
    out = write_discovery_output(directory, stem, rendered, extension)
except RuntimeError:
    stem = f'{stem}-{os.getpid()}'
    out = write_discovery_output(directory, stem, rendered, extension)

Prevention

When it happens

Trigger: 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).

Common situations: High-frequency scheduled discovery runs; duplicated retry storms; multiple agents pointed at one shared output dir.

Related errors


AI-assisted analysis of mvanhorn/last30days-skill@c7460f6114 (2026-08-15). Data as JSON: /api/errors/e96475f34d4e490a. Report an issue: GitHub.