fix(actor): Handle YAML parse failures in template engine fallback #10556

Open
opened 2026-04-18 17:13:08 +00:00 by HAL9000 · 0 comments
Owner

Metadata

Commit Message: fix(actor): Handle YAML parse failures in template engine fallback

Branch Name: fix/yaml-parse-error-handling

Background and Context

The yaml_template_engine.py module contains an unhandled exception vulnerability in the _render_and_parse() method. When YAML parsing fails on the first attempt, the code attempts to fix common YAML issues and parse again. However, the second yaml.safe_load() call is not wrapped in a try-except block. If the fixed YAML still fails to parse, an unhandled yaml.YAMLError will propagate to the caller, violating the fail-fast principle and making debugging difficult.

Code Evidence:

Lines 106-112 in src/cleveragents/actor/yaml_template_engine.py:

except yaml.YAMLError as exc:
    logger.error("Failed to parse rendered YAML: %s", exc)
    logger.debug("Rendered YAML:\n%s", fixed_yaml)
    fixed_yaml = self._fix_common_yaml_issues(fixed_yaml)
    parsed = yaml.safe_load(fixed_yaml)  # <-- Can still raise yaml.YAMLError!
    if not isinstance(parsed, dict):
        raise ValueError(...)
    return cast(dict[str, Any], parsed)

Reproducibility:

This is reproducible by providing YAML content that:

  1. Fails initial parsing (triggers first except block)
  2. Still fails after _fix_common_yaml_issues() is applied

Example: YAML with unmatched quotes or invalid nesting that the fix function cannot resolve.

Codebase Freshness:

This code is current as of April 6, 2026 (file modification date).

Expected Behavior

When YAML parsing fails on both the initial attempt and the fallback attempt after applying fixes, the application should:

  1. Catch the exception from the second yaml.safe_load() call
  2. Provide a descriptive error message that includes context about both parse attempts
  3. Include the original and fixed YAML in the error message for debugging purposes
  4. Raise an appropriate exception (either re-raised with context or a descriptive ValueError)

Acceptance Criteria

  • Second yaml.safe_load() call is wrapped in try-except
  • Exception includes context about both parse attempts
  • Error message includes the original and fixed YAML for debugging
  • Tests verify the fallback behavior with unparseable YAML
  • All existing tests pass
  • Code coverage remains >=97%

Subtasks

  • Add try-except around second yaml.safe_load() call
  • Create test case with YAML that fails both parse attempts
  • Verify error message includes helpful debugging context
  • Run full test suite to ensure no regressions
  • Verify code coverage metrics

Definition of Done

This issue is complete when:

  • The second yaml.safe_load() call is properly wrapped in exception handling
  • A test case demonstrates the fix handles unparseable YAML gracefully
  • All existing tests pass
  • Code coverage remains >=97%
  • The fix is merged to the main branch

Automated by CleverAgents Bot
Agent: new-issue-creator

## Metadata **Commit Message:** `fix(actor): Handle YAML parse failures in template engine fallback` **Branch Name:** `fix/yaml-parse-error-handling` ## Background and Context The `yaml_template_engine.py` module contains an unhandled exception vulnerability in the `_render_and_parse()` method. When YAML parsing fails on the first attempt, the code attempts to fix common YAML issues and parse again. However, the second `yaml.safe_load()` call is not wrapped in a try-except block. If the fixed YAML still fails to parse, an unhandled `yaml.YAMLError` will propagate to the caller, violating the fail-fast principle and making debugging difficult. **Code Evidence:** Lines 106-112 in `src/cleveragents/actor/yaml_template_engine.py`: ```python except yaml.YAMLError as exc: logger.error("Failed to parse rendered YAML: %s", exc) logger.debug("Rendered YAML:\n%s", fixed_yaml) fixed_yaml = self._fix_common_yaml_issues(fixed_yaml) parsed = yaml.safe_load(fixed_yaml) # <-- Can still raise yaml.YAMLError! if not isinstance(parsed, dict): raise ValueError(...) return cast(dict[str, Any], parsed) ``` **Reproducibility:** This is reproducible by providing YAML content that: 1. Fails initial parsing (triggers first except block) 2. Still fails after `_fix_common_yaml_issues()` is applied Example: YAML with unmatched quotes or invalid nesting that the fix function cannot resolve. **Codebase Freshness:** This code is current as of April 6, 2026 (file modification date). ## Expected Behavior When YAML parsing fails on both the initial attempt and the fallback attempt after applying fixes, the application should: 1. Catch the exception from the second `yaml.safe_load()` call 2. Provide a descriptive error message that includes context about both parse attempts 3. Include the original and fixed YAML in the error message for debugging purposes 4. Raise an appropriate exception (either re-raised with context or a descriptive ValueError) ## Acceptance Criteria - [ ] Second `yaml.safe_load()` call is wrapped in try-except - [ ] Exception includes context about both parse attempts - [ ] Error message includes the original and fixed YAML for debugging - [ ] Tests verify the fallback behavior with unparseable YAML - [ ] All existing tests pass - [ ] Code coverage remains >=97% ## Subtasks - [ ] Add try-except around second `yaml.safe_load()` call - [ ] Create test case with YAML that fails both parse attempts - [ ] Verify error message includes helpful debugging context - [ ] Run full test suite to ensure no regressions - [ ] Verify code coverage metrics ## Definition of Done This issue is complete when: - The second `yaml.safe_load()` call is properly wrapped in exception handling - A test case demonstrates the fix handles unparseable YAML gracefully - All existing tests pass - Code coverage remains >=97% - The fix is merged to the main branch --- **Automated by CleverAgents Bot** Agent: new-issue-creator
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cleveragents/cleveragents-core#10556
No description provided.