Skip to content

bug: Unbound execution_id causes 503 on chat endpoint #335

Closed

Description

@vybe

Summary

Chat endpoint returns 503 with error: cannot access local variable 'execution_id' where it is not associated with a value

Reproduction

Run the test suite:

pytest tests/test_activities.py::TestActivityCreation::test_chat_creates_activity -v

Error Details

AssertionError: Expected status in [200, 202], got 503.
Response: {"detail":"Failed to communicate with agent: Execution error: cannot access local variable 'execution_id' where it is not associated with a value"}

Root Cause

In src/backend/services/task_execution_service.py, execution_id is referenced before assignment. The variable is likely initialized inside a conditional block (e.g., try or if) but accessed in an except/finally handler unconditionally.

Fix

Ensure execution_id = None is assigned before any conditional logic that may set it:

execution_id = None  # Initialize before try block
try:
    execution_id = create_execution(...)
    # ...
except Exception as e:
    if execution_id:  # Safe to check now
        mark_failed(execution_id, e)

Affected Tests (4)

  • test_activities.py::TestActivityCreation::test_chat_creates_activity
  • test_dynamic_thinking_status.py::TestAsyncModeSubmission::test_async_mode_completes_eventually
  • test_dynamic_thinking_status.py::TestAsyncModeSessionPersistence::test_async_mode_with_save_to_session
  • test_dynamic_thinking_status.py::TestAsyncModeSessionPersistence::test_async_mode_session_contains_messages

Labels

  • priority-p0
  • type-bug

Activity

  1. added
    priority-p0
    type-bug
    on Apr 14, 2026
  2. added
    status-in-progress
    on Apr 15, 2026
  3. vybe commented on Apr 15, 2026

    @vybe
    ContributorAuthor

    Root Cause Analysis

    The UnboundLocalError was already fixed in commit 1644b4e (March 22, 2026):

    fix: Pass execution_id to execute_claude_code to fix UnboundLocalError
    
    execute_claude_code() referenced execution_id on line 516 without it
    being a parameter. The assignment made it a local variable for the
    entire function scope, causing UnboundLocalError when the `or`
    expression tried to read it before assignment.
    

    Why Tests Still Fail

    The affected agent containers were created before the fix and haven't been rebuilt:

    Container Created Fix Date
    agent-trinity-system Feb 9, 2026 March 22, 2026

    The container has old code without the execution_id parameter:

    # In running container (OLD):
    async def execute_claude_code(prompt: str, stream: bool = False, model: Optional[str] = None)
    
    # In source code (FIXED):
    async def execute_claude_code(prompt: str, ..., execution_id: Optional[str] = None)

    Resolution

    The fix is already in the source code and base image. Affected containers need to be recreated to pick up the new base image.

    Closing as already fixed - the tests should pass once containers are rebuilt.

  4. vybe commented on Apr 15, 2026

    @vybe
    ContributorAuthor

    Closing as already fixed. The code fix is in commit 1644b4e. The tests fail because old containers (created before the fix) need to be recreated to use the updated base image.

  5. removed
    status-in-progress
    on Apr 15, 2026
  6. added
    complexity-low
    on Jun 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    complexity-lowpriority-p0type-bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions