Repository: All-Hands-AI/OpenHands
File: openhands/core/config/toml_writer.py (Python)

Code before:
    def __enter__(self):
        start = time.time()
        while True:
            try:
                fd = os.open(self.lock_path, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
                os.write(fd, str(os.getpid()).encode('utf-8'))
                os.close(fd)
                self._acquired = True
                return self
            except FileExistsError:
                if time.time() - start > self.timeout:
                    raise TimeoutError(
                        f'Could not acquire config lock at {self.lock_path}'
                    )
                time.sleep(self.poll_interval)

    def __exit__(self, exc_type, exc, tb):
        if self._acquired:
            try:
                os.unlink(self.lock_path)
            except FileNotFoundError:
                pass


class TOMLConfigWriter:
    """TOML writer for OpenHands config using tomlkit.

    - Preserves comments/formatting when updating
    - Merges keys without clobbering unrelated sections
    - Provides explicit APIs for base vs named subsections
    - Uses atomic write with a simple file lock
    """

    def __init__(self, toml_file: str = 'config.toml') -> None:
        self.toml_file = toml_file
        self._doc = tomlkit.document()
        self._load()

    def _load(self) -> None:
        try:
            with open(self.toml_file, 'r', encoding='utf-8') as f:
                self._doc = tomlkit.parse(f.read())
        except FileNotFoundError:
            self._doc = tomlkit.document()
        except Exception as e:
            logger.openhands_logger.warning(
                f'Cannot parse existing TOML at {self.toml_file}: {e}. Recreating file.'
            )
            self._doc = tomlkit.document()

    def _ensure_table(self, name: str):

Review comment (suggestion):
```suggestion
    """TOML writer for OpenHands config.
```

Code after:
    def __enter__(self):
        start = time.time()
        while True:
            try:
                fd = os.open(self.lock_path, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
                os.write(fd, str(os.getpid()).encode('utf-8'))
                os.close(fd)
                self._acquired = True
                return self
            except FileExistsError:
                if time.time() - start > self.timeout:
                    raise TimeoutError(
                        f'Could not acquire config lock at {self.lock_path}'
                    )
                time.sleep(self.poll_interval)

    def __exit__(self, exc_type, exc, tb):
        if self._acquired:
            try:
                os.unlink(self.lock_path)
            except FileNotFoundError:
                pass


class TOMLConfigWriter:
    """TOML writer for OpenHands config.

    - Preserves comments/formatting when updating
    - Merges keys without clobbering unrelated sections
    - Provides explicit APIs for base vs named subsections
    - Uses atomic write with a simple file lock
    """

    def __init__(self, toml_file: str = 'config.toml') -> None:
        self.toml_file = toml_file
        self._doc = tomlkit.document()
        self._load()

    def _load(self) -> None:
        try:
            with open(self.toml_file, 'r', encoding='utf-8') as f:
                self._doc = tomlkit.parse(f.read())
        except FileNotFoundError:
            self._doc = tomlkit.document()
        except Exception as e:
            logger.openhands_logger.warning(
                f'Cannot parse existing TOML at {self.toml_file}: {e}. Recreating file.'
            )
            self._doc = tomlkit.document()

    def _ensure_table(self, name: str):