{"record":{"id":"f736c6df4c3af9eb","repo":"JuliusBrussee/caveman","slug":"invalid-configuration","errorCode":"invalid_configuration","errorMessage":"invalid_configuration","messagePattern":"invalid_configuration","errorType":"error_code","errorClass":"MiddlewareError","httpStatus":null,"severity":"error","filePath":"packages/sdk/python/caveman_cloud/middleware/runtime.py","lineNumber":71,"sourceCode":"\ndef _json(value: Any) -> str:\n    return json.dumps(value, ensure_ascii=False, separators=(\",\", \":\"), allow_nan=False)\n\n\nclass MiddlewareRuntime:\n    def __init__(self, *, endpoint: str = \"http://127.0.0.1:8787\", token: str | None = None,\n                 allow_remote_content: bool = False, mode: str = \"compress\", deadline_ms: int = 100,\n                 retrieve_deadline_ms: int = 5000,\n                 strict: bool = False, on_diagnostic: Callable[[dict], None] | None = None,\n                 on_report: Callable[[CallReport], None] | None = None):\n        url = urlsplit(endpoint)\n        local = url.hostname in (\"127.0.0.1\", \"::1\", \"localhost\")\n        if url.scheme not in (\"http\", \"https\") or not url.hostname or url.username or url.password or url.query or url.fragment or url.path not in (\"\", \"/\"):\n            raise MiddlewareError(\"invalid_endpoint\")\n        if not local and (not allow_remote_content or url.scheme != \"https\"):\n            raise MiddlewareError(\"remote_content_not_enabled\")\n        if type(deadline_ms) is not int or deadline_ms <= 0 or mode not in (\"off\", \"record\", \"compress\"):\n            raise MiddlewareError(\"invalid_configuration\")\n        # A model asking to see an original is waiting on a page of stored text,\n        # not on the optimizer in front of a provider call. Separate budget.\n        if type(retrieve_deadline_ms) is not int or retrieve_deadline_ms <= 0:\n            raise MiddlewareError(\"invalid_configuration\")\n        self.endpoint = f\"{url.scheme}://{url.netloc}\"\n        self.mode, self.deadline_ms, self.strict = mode, deadline_ms, strict\n        self.retrieve_deadline_ms = retrieve_deadline_ms\n        self._token, self._diagnostic = token, on_diagnostic\n        self._report_sink, self._last_report = on_report, None\n        self._url = url\n        self._connections: set[http.client.HTTPConnection] = set()\n        self._caps: dict | None = None\n        self._bindings: weakref.WeakKeyDictionary[RecoveryBinding, tuple] = weakref.WeakKeyDictionary()\n        self._lock = threading.RLock()\n        self._slots = threading.BoundedSemaphore(16)\n        self._closed = False\n        self._failures = 0\n        self._open_until = 0.0","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/sdk/python/caveman_cloud/middleware/runtime.py#L53-L89","documentation":"The constructor enforces typed, sane configuration: deadline_ms must be a positive int and mode must be exactly one of \"off\", \"record\", \"compress\". Anything else — floats, strings for deadline, unknown mode names — raises MiddlewareError(\"invalid_configuration\").","triggerScenarios":"Passing deadline_ms=5000.0 (float), deadline_ms=0 or negative, mode=\" Off\", mode=\"compression\", or mode=None.","commonSituations":"Reading the deadline from an env var or YAML without int conversion (getting str/float); renaming modes based on guessed spelling (\"compressed\", \"off mode\"); typos after a config refactor.","solutions":["Pass deadline_ms as a strict positive int (e.g. int(os.environ[\"DEADLINE_MS\"]))","Use exactly \"off\", \"record\", or \"compress\" for mode","Validate config at load time: type(x) is int and x > 0 and mode in {...}","Catch MiddlewareError(\"invalid_configuration\") during startup to fail fast with context"],"exampleFix":"// before\nRuntime(endpoint=ep, deadline_ms=\"5000\", mode=\"compress-mode\")\n// after\nRuntime(endpoint=ep, deadline_ms=int(\"5000\"), mode=\"compress\")","handlingStrategy":"validation","validationCode":"assert type(deadline_ms) is int and deadline_ms > 0, 'deadline_ms must be positive int'\nassert mode in ('off', 'record', 'compress'), f'invalid mode: {mode!r}'","typeGuard":"def valid_runtime_config(deadline_ms: object, mode: object) -> bool:\n    return (type(deadline_ms) is int and deadline_ms > 0\n            and mode in ('off', 'record', 'compress'))","tryCatchPattern":"try:\n    runtime = Runtime(endpoint=ep, deadline_ms=deadline_ms, mode=mode)\nexcept MiddlewareError as e:\n    if str(e) == 'invalid_configuration':\n        raise ConfigError(f'deadline_ms={deadline_ms!r} must be positive int; mode={mode!r} in off|record|compress') from e\n    raise","preventionTips":["Coerce env/config values with int() before passing (floats and strings fail the strict type check)","Copy mode names exactly: off, record, compress — no variants","Validate the whole config object once at startup, not at each call site","Add schema validation (pydantic/dataclass with StrictInt/Literal) at the config boundary"],"tags":["python","configuration","validation","middleware"],"backgroundTag":"invalid-config-value","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}