HKUDS/Vibe-Trading · critical · ValueError

invalid hypotheses storage JSON: {self.path}

Error message

invalid hypotheses storage JSON: {self.path}

What it means

The hypotheses storage file exists but its content is not parseable JSON, so HypothesisRegistry.list cannot load records. The original JSONDecodeError is chained as the cause, and the offending file path is included in the message.

Source

Thrown at agent/src/hypotheses/registry.py:374

            haystack = json.dumps(hyp.to_dict(), ensure_ascii=False, sort_keys=True)
            if not query_tokens:
                score = 1
            else:
                hay_tokens = _tokenize(haystack)
                score = len(query_tokens & hay_tokens)
            if score > 0:
                scored.append((score, hyp))
        scored.sort(key=lambda item: (item[0], item[1].updated_at), reverse=True)
        return [hyp for _, hyp in scored[: max(1, min(int(limit), 100))]]

    def list(self) -> list[Hypothesis]:
        """Load all hypotheses from storage."""
        if not self.path.exists():
            return []
        try:
            raw = json.loads(self.path.read_text(encoding="utf-8"))
        except json.JSONDecodeError as exc:
            raise ValueError(f"invalid hypotheses storage JSON: {self.path}") from exc
        if not isinstance(raw, list):
            raise ValueError("hypotheses storage must contain a JSON list")
        return [Hypothesis.from_dict(item) for item in raw if isinstance(item, dict)]

    def _save(self, records: list[Hypothesis]) -> None:
        payload = [hyp.to_dict() for hyp in sorted(records, key=lambda h: h.created_at)]
        tmp_path = self.path.with_suffix(self.path.suffix + ".tmp")
        tmp_path.write_text(
            json.dumps(payload, ensure_ascii=False, indent=2, sort_keys=True),
            encoding="utf-8",
        )
        tmp_path.replace(self.path)

    @staticmethod
    def _find_required(records: list[Hypothesis], hypothesis_id: str) -> Hypothesis:
        for hyp in records:
            if hyp.hypothesis_id == hypothesis_id:
                return hyp

View on GitHub (pinned to 80ffdda44c)

Solutions

  1. Inspect the file at the path in the message and fix the JSON syntax (json.tool or an editor with JSON validation)
  2. Restore from backup or an earlier version of the file if content is truncated
  3. If unrecoverable, archive the broken file to start fresh (data loss — last resort)
  4. Prevent recurrence: always write via registry methods, never echo/redirect into the file

Example fix

python -m json.tool hypotheses.json   # locate the syntax error, fix it manually
Defensive patterns

Strategy: try-catch

Validate before calling

import json
try:
    json.loads(path.read_text(encoding='utf-8'))
except json.JSONDecodeError:
    alert('hypotheses storage JSON is corrupt; restore from backup')

Type guard

def storage_json_ok(path) -> bool:
    try:
        return isinstance(json.loads(path.read_text(encoding='utf-8')), list)
    except (OSError, json.JSONDecodeError):
        return False

Try / catch

try:
    records = registry.list()
except ValueError as exc:
    if 'invalid hypotheses storage JSON' in str(exc):
        restore_from_backup(); records = registry.list()
    else:
        raise

Prevention

When it happens

Trigger: The JSON file at self.path contains trailing commas, truncated output from a crashed concurrent write, or hand-edited syntax errors; any subsequent list/create/update/link_backtest call then fails.

Common situations: Manual editing of the storage file, a previous interrupted save that left partial content, or two processes writing the file simultaneously despite the tmp-file rename scheme.

Related errors


AI-assisted analysis of HKUDS/Vibe-Trading@80ffdda44c (2026-08-28). Data as JSON: /api/errors/b1f568b209f6c766. Report an issue: GitHub.