UAT: ContextManager.import_context() only handles JSON — YAML export/import round-trip fails silently #2594

Open
opened 2026-04-03 19:04:02 +00:00 by freemo · 1 comment
Owner

Metadata

  • Branch: fix/context-manager-yaml-import
  • Commit Message: fix(reactive): support YAML files in ContextManager.import_context()
  • Milestone: v3.4.0
  • Parent Epic: #396

Bug Description

ContextManager.import_context() in src/cleveragents/reactive/context_manager.py only reads JSON files. However, agents actor context export can write YAML files (when --output has a .yaml or .yml extension). This creates a broken export/import round-trip for YAML files.

What Was Tested

Code-level analysis of src/cleveragents/reactive/context_manager.py and src/cleveragents/cli/commands/actor_context.py.

Expected Behavior (from spec)

The spec (§ agents actor context import) states:

The exported file contains messages, metadata, state, and global_context and can later be re-imported with agents actor context import.

The agents actor context export command already supports YAML output:

# actor_context.py, context_export command
suffix = output.suffix.lower()
if suffix in (".yaml", ".yml"):
    with open(output, "w", encoding="utf-8") as fh:
        yaml.dump(export_data, fh, default_flow_style=False, sort_keys=False)
else:
    with open(output, "w", encoding="utf-8") as fh:
        json.dump(export_data, fh, indent=2)

The agents actor context import command also handles YAML at the CLI level:

# actor_context.py, context_import command
if suffix in (".yaml", ".yml"):
    file_data: dict[str, Any] = yaml.safe_load(text) or {}
else:
    try:
        file_data = json.loads(text)
    except json.JSONDecodeError:
        file_data = yaml.safe_load(text) or {}

Actual Behavior

ContextManager.import_context() only reads JSON:

# context_manager.py, line ~130
def import_context(self, context_file: Path) -> None:
    with open(context_file, encoding="utf-8") as f:
        data = json.load(f)  # ← Only JSON! Will raise json.JSONDecodeError for YAML files
    self.messages = data.get("messages", [])
    self.metadata = data.get("metadata", self._load_metadata())
    self.state = data.get("state", {})
    self.global_context = data.get("global_context", {})
    self.save()

When context_import in actor_context.py calls ctx_mgr.import_context(input_file) with a YAML file, the ContextManager.import_context() method will raise json.JSONDecodeError because it only calls json.load().

Note: The CLI-level context_import command does parse the YAML correctly to get the context name and validate the file structure, but then calls ctx_mgr.import_context(input_file) which re-reads the file using only json.load(). This means YAML imports fail at the persistence step even though the CLI-level parsing succeeded.

Code Location

  • src/cleveragents/reactive/context_manager.pyimport_context() method (uses json.load() only)
  • src/cleveragents/cli/commands/actor_context.pycontext_import command (correctly handles YAML at CLI level but delegates to ContextManager.import_context() which doesn't)

Steps to Reproduce

# Export to YAML
agents actor context export docs --output /tmp/docs-context.yaml

# Import from YAML — fails with json.JSONDecodeError
agents actor context import --input /tmp/docs-context.yaml

Impact

Medium. The export/import round-trip for YAML files is broken. Users who export to YAML (a documented and supported format) cannot re-import the file. The actor_context_cmds.feature test for YAML import (Scenario: Import a context from a YAML file) in features/actor_context_coverage_r3.feature uses a mock that bypasses ContextManager.import_context(), so this bug is not caught by existing tests.

Fix Required

Update ContextManager.import_context() to detect the file extension and use yaml.safe_load() for .yaml/.yml files:

def import_context(self, context_file: Path) -> None:
    text = context_file.read_text(encoding="utf-8")
    suffix = context_file.suffix.lower()
    if suffix in (".yaml", ".yml"):
        import yaml
        data = yaml.safe_load(text) or {}
    else:
        try:
            data = json.loads(text)
        except json.JSONDecodeError:
            import yaml
            data = yaml.safe_load(text) or {}
    self.messages = data.get("messages", [])
    self.metadata = data.get("metadata", self._load_metadata())
    self.state = data.get("state", {})
    self.global_context = data.get("global_context", {})
    self.save()

Subtasks

  • Update ContextManager.import_context() to support YAML files (detect by extension, fall back to YAML on JSON parse failure)
  • Add BDD scenario for YAML export → YAML import round-trip in features/actor_context_cmds.feature
  • Verify coverage >= 97% via nox -s coverage_report
  • Run nox (all default sessions) and fix any errors

Definition of Done

  • ContextManager.import_context() correctly reads both JSON and YAML files
  • YAML export → YAML import round-trip preserves all context data
  • BDD scenario covers the YAML round-trip
  • All nox sessions pass with coverage >= 97%

Automated by CleverAgents Bot
Supervisor: UAT Testing | Agent: ca-uat-tester

## Metadata - **Branch**: `fix/context-manager-yaml-import` - **Commit Message**: `fix(reactive): support YAML files in ContextManager.import_context()` - **Milestone**: v3.4.0 - **Parent Epic**: #396 ## Bug Description `ContextManager.import_context()` in `src/cleveragents/reactive/context_manager.py` only reads JSON files. However, `agents actor context export` can write YAML files (when `--output` has a `.yaml` or `.yml` extension). This creates a broken export/import round-trip for YAML files. ### What Was Tested Code-level analysis of `src/cleveragents/reactive/context_manager.py` and `src/cleveragents/cli/commands/actor_context.py`. ### Expected Behavior (from spec) The spec (§ `agents actor context import`) states: > The exported file contains messages, metadata, state, and global_context and can later be re-imported with `agents actor context import`. The `agents actor context export` command already supports YAML output: ```python # actor_context.py, context_export command suffix = output.suffix.lower() if suffix in (".yaml", ".yml"): with open(output, "w", encoding="utf-8") as fh: yaml.dump(export_data, fh, default_flow_style=False, sort_keys=False) else: with open(output, "w", encoding="utf-8") as fh: json.dump(export_data, fh, indent=2) ``` The `agents actor context import` command also handles YAML at the CLI level: ```python # actor_context.py, context_import command if suffix in (".yaml", ".yml"): file_data: dict[str, Any] = yaml.safe_load(text) or {} else: try: file_data = json.loads(text) except json.JSONDecodeError: file_data = yaml.safe_load(text) or {} ``` ### Actual Behavior `ContextManager.import_context()` only reads JSON: ```python # context_manager.py, line ~130 def import_context(self, context_file: Path) -> None: with open(context_file, encoding="utf-8") as f: data = json.load(f) # ← Only JSON! Will raise json.JSONDecodeError for YAML files self.messages = data.get("messages", []) self.metadata = data.get("metadata", self._load_metadata()) self.state = data.get("state", {}) self.global_context = data.get("global_context", {}) self.save() ``` When `context_import` in `actor_context.py` calls `ctx_mgr.import_context(input_file)` with a YAML file, the `ContextManager.import_context()` method will raise `json.JSONDecodeError` because it only calls `json.load()`. **Note**: The CLI-level `context_import` command does parse the YAML correctly to get the context name and validate the file structure, but then calls `ctx_mgr.import_context(input_file)` which re-reads the file using only `json.load()`. This means YAML imports fail at the persistence step even though the CLI-level parsing succeeded. ### Code Location - `src/cleveragents/reactive/context_manager.py` — `import_context()` method (uses `json.load()` only) - `src/cleveragents/cli/commands/actor_context.py` — `context_import` command (correctly handles YAML at CLI level but delegates to `ContextManager.import_context()` which doesn't) ### Steps to Reproduce ```bash # Export to YAML agents actor context export docs --output /tmp/docs-context.yaml # Import from YAML — fails with json.JSONDecodeError agents actor context import --input /tmp/docs-context.yaml ``` ### Impact **Medium.** The export/import round-trip for YAML files is broken. Users who export to YAML (a documented and supported format) cannot re-import the file. The `actor_context_cmds.feature` test for YAML import (`Scenario: Import a context from a YAML file`) in `features/actor_context_coverage_r3.feature` uses a mock that bypasses `ContextManager.import_context()`, so this bug is not caught by existing tests. ### Fix Required Update `ContextManager.import_context()` to detect the file extension and use `yaml.safe_load()` for `.yaml`/`.yml` files: ```python def import_context(self, context_file: Path) -> None: text = context_file.read_text(encoding="utf-8") suffix = context_file.suffix.lower() if suffix in (".yaml", ".yml"): import yaml data = yaml.safe_load(text) or {} else: try: data = json.loads(text) except json.JSONDecodeError: import yaml data = yaml.safe_load(text) or {} self.messages = data.get("messages", []) self.metadata = data.get("metadata", self._load_metadata()) self.state = data.get("state", {}) self.global_context = data.get("global_context", {}) self.save() ``` ## Subtasks - [ ] Update `ContextManager.import_context()` to support YAML files (detect by extension, fall back to YAML on JSON parse failure) - [ ] Add BDD scenario for YAML export → YAML import round-trip in `features/actor_context_cmds.feature` - [ ] Verify coverage >= 97% via `nox -s coverage_report` - [ ] Run `nox` (all default sessions) and fix any errors ## Definition of Done - `ContextManager.import_context()` correctly reads both JSON and YAML files - YAML export → YAML import round-trip preserves all context data - BDD scenario covers the YAML round-trip - All nox sessions pass with coverage >= 97% --- **Automated by CleverAgents Bot** Supervisor: UAT Testing | Agent: ca-uat-tester
freemo added this to the v3.4.0 milestone 2026-04-03 19:12:46 +00:00
Author
Owner

Issue triaged by project owner:

  • State: Verified
  • MoSCoW: Could Have

Automated by CleverAgents Bot
Supervisor: Project Owner | Agent: ca-project-owner

Issue triaged by project owner: - **State**: Verified - **MoSCoW**: Could Have --- **Automated by CleverAgents Bot** Supervisor: Project Owner | Agent: ca-project-owner
freemo removed this from the v3.4.0 milestone 2026-04-06 21:01:38 +00:00
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.

Blocks
#396 Epic: ACMS Context Pipeline
cleveragents/cleveragents-core
Reference
cleveragents/cleveragents-core#2594
No description provided.