Skip to content

FIX: Fix CodeChameleonConverter decryption functions in binary_tree and length modes - #3165

Open
RohithPariki wants to merge 1 commit into
microsoft:mainfrom
RohithPariki:fix/codechameleon-decryption-functions
Open

RohithPariki wants to merge 1 commit into
microsoft:mainfrom
RohithPariki:fix/codechameleon-decryption-functions

Conversation

@RohithPariki

Copy link
Copy Markdown
Contributor

Description

Fixes #3164.

In CodeChameleonConverter, the generated decryption functions for binary_tree and length modes contained bugs that caused runtime failures when executed:

  1. binary_tree mode (_decrypt_binary_tree):
    string_to_tree uses json.loads(problem_description), deserializing the full nested structure into Python dictionaries. The recursive helper then called json.loads(node_dict['left']) and json.loads(node_dict['right']). Passing a Python dict to json.loads() resulted in TypeError: the JSON object must be str, bytes or bytearray, not dict.

    • Fix: Changed recursion to pass node_dict['left'] and node_dict['right'] directly to helper() without calling json.loads.
  2. length mode (_decrypt_length):
    The _encrypt_length method serializes the list of single-entry dictionaries to a JSON string (json.dumps(...)), which is inserted into the prompt template. However, decryption(encrypted_problem) did not import json or parse encrypted_problem. Iterating directly over the JSON string caused AttributeError: 'str' object has no attribute 'items'.

    • Fix: Added import json and parsed encrypted_problem via json.loads if passed as a string.

Tests and Documentation

  • Updated existing docstring assertions in tests/unit/converter/test_code_chameleon_converter.py for binary_tree and length modes.
  • Added test_codechameleon_converter_decryption_execution parametrized over all built-in encryption types (reverse, odd_even, binary_tree, length) to verify that the generated decryption() function executes cleanly and reconstructs the original prompt.
  • Verified all unit tests pass with pytest tests/unit/converter/test_code_chameleon_converter.py (10/10 passed).
  • Linted and formatted with ruff check and ruff format.

@romanlutz

Copy link
Copy Markdown
Contributor

#3072 was technically here first but author didn't accept the CLA so happy to merge this.

@romanlutz Roman Lutz (romanlutz) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll make a couple of improvements and then merge. Thank you!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: CodeChameleonConverter binary_tree and length decryption functions fail during execution with TypeError and AttributeError

2 participants