Fix the config reader for a timeout setting

Review the AI's codeMedium15 min

Your task

Your service reads an optional timeout from its config and falls back to a default. The first version wrapped the parse in catch (Exception e) {}, so a typo like 3O (letter O) silently became the default with nothing in the logs. The new version returns the value together with the reason, so the caller can log anything that is not fine.

An assistant wrote resolve_timeout_setting(config_pairs, key, default_seconds). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • config_pairs: config entries, each written key=value.
  • key: the setting to read, such as timeout.
  • default_seconds: the default timeout.
  • Return [value, reason]. reason is one of "ok", "missing", "empty", "malformed" and "out_of_range". For every reason except "ok", value is default_seconds written as text.

Rules the tests check:

  • Split each entry at its first = into a key and a value. Skip an entry with no =. Keys match exactly: nothing is trimmed and case matters.
  • When the key appears more than once, the last entry wins.
  • Check the value in this order and return the first reason that applies:
    1. The key never appears: "missing".
    2. The value is empty: "empty".
    3. The value is not an optional - followed by one or more digits from 0 to 9, with nothing else (no spaces, no +): "malformed".
    4. After dropping leading zeros, more than 9 digits are left: "malformed".
    5. The number is 0 or less: "out_of_range".
  • Otherwise return the number as text without leading zeros, and "ok". So 0030 gives ["30", "ok"].

Examples

resolve_timeout_setting(config_pairs: list[str], key: str, default_seconds: int) -> list[str]
Example 1
Inputresolve_timeout_setting(["timeout=45", "retries=3"], "timeout", 30)
Output["45", "ok"]
Example 2
Inputresolve_timeout_setting(["retries=3"], "timeout", 30)
Output["30", "missing"]
Example 3
Inputresolve_timeout_setting(["timeout=3O"], "timeout", 30)
Output["30", "malformed"]