Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(agents-api): Configure routes #986

Merged
merged 46 commits into from
Dec 24, 2024
Merged

Conversation

Ahmad-mtos
Copy link
Contributor

@Ahmad-mtos Ahmad-mtos commented Dec 24, 2024

PR Type

Enhancement


Description

Major refactoring and enhancement of the agents-api service:

  • Routing and Authentication:

    • Configured API routers with proper authentication using API key dependency
    • Added internal router without authentication requirement
    • Removed scalar router in favor of new routing structure
  • Database and Queries:

    • Migrated from models to queries directory structure
    • Made database query functions asynchronous
    • Improved database pool initialization and management
  • Request Validation:

    • Added content length validation middleware
    • Set max payload size configuration
    • Improved error handling for task validation
  • Testing:

    • Enabled and updated test cases for agent routes
    • Added proper test fixtures and assertions
    • Updated test client configuration
  • Code Organization:

    • Moved queries to dedicated directory
    • Updated import paths across codebase
    • Improved code structure and organization

Changes walkthrough 📝

Relevant files
Configuration changes
web.py
Configure API routers with authentication                               

agents-api/agents_api/web.py

  • Removed scalar_router and docs endpoint
  • Added routers for agents, sessions, users, jobs, files, docs, tasks
    with API key dependency
  • Added internal router without dependency
  • +21/-39 
    app.py
    Add app configuration and middleware                                         

    agents-api/agents_api/app.py

  • Added content length validation middleware
  • Added scalar docs router
  • Added DB pool initialization in lifespan
  • +53/-6   
    Enhancement
    create_task_execution.py
    Update task execution creation to use async queries           

    agents-api/agents_api/routers/tasks/create_task_execution.py

  • Updated imports to use queries instead of models
  • Made query functions async
  • Added error handling for task validation
  • +20/-20 
    Tests
    test_agent_routes.py
    Enable agent routes test cases                                                     

    agents-api/tests/test_agent_routes.py

  • Uncommented and enabled test cases for agent routes
  • Updated test assertions and response codes
  • +172/-172

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    Copy link
    Contributor

    qodo-merge-pro-for-open-source bot commented Dec 24, 2024

    CI Failure Feedback 🧐

    (Checks updated until commit 77903ef)

    Action: Typecheck

    Failed stage: Typecheck [❌]

    Failure summary:

    The pytype type checking failed due to multiple issues:

  • Missing dependencies: Several modules could not be imported, specifically:
    - pycozo and
    pycozo.client modules
    - models.tools module
    - router module
  • Type annotation mismatches:
    - In search_docs_hybrid.py: Type annotation for embedding does not
    match assignment
    - In get_execution.py and count_executions.py: Bad return type errors where
    actual return types did not match expected types
  • The most frequent errors were import-related issues, suggesting missing package installations or
    incorrect import paths

  • Relevant error logs:
    1:  ##[group]Operating System
    2:  Ubuntu
    ...
    
    1205:  [12/372] check agents_api.autogen.Executions
    1206:  [13/372] check agents_api.common.utils.datetime
    1207:  [14/372] check agents_api.autogen.Entries
    1208:  [15/372] check agents_api.autogen.Tasks
    1209:  [16/372] check agents_api.autogen.Agents
    1210:  [17/372] check agents_api.clients.__init__
    1211:  [18/372] check agents_api.common.exceptions.__init__
    1212:  [19/372] check agents_api.common.utils.cozo
    1213:  FAILED: /home/runner/work/julep/julep/agents-api/.pytype/pyi/agents_api/common/utils/cozo.pyi 
    1214:  /home/runner/work/julep/julep/agents-api/.venv/bin/python -m pytype.main --disable pyi-error --imports_info /home/runner/work/julep/julep/agents-api/.pytype/imports/agents_api.common.utils.cozo.imports --module-name agents_api.common.utils.cozo --platform linux -V 3.12 -o /home/runner/work/julep/julep/agents-api/.pytype/pyi/agents_api/common/utils/cozo.pyi --analyze-annotated --nofail --none-is-not-bool --quick --strict-none-binding /home/runner/work/julep/julep/agents-api/agents_api/common/utils/cozo.py
    1215:  /home/runner/work/julep/julep/agents-api/agents_api/common/utils/cozo.py:9:1: error: in <module>: Can't find module 'pycozo'. [import-error]
    1216:  from pycozo import Client
    1217:  ~~~~~~~~~~~~~~~~~~~~~~~~~
    1218:  For more details, see https://google.github.io/pytype/errors.html#import-error
    1219:  [20/372] check agents_api.app
    1220:  [21/372] check agents_api.exceptions
    1221:  [22/372] check agents_api.autogen.openapi_model
    1222:  [23/372] check agents_api.common.protocol.developers
    1223:  [24/372] check agents_api.clients.async_s3
    1224:  [25/372] check agents_api.worker.codec
    1225:  [26/372] check agents_api.common.protocol.agents
    1226:  [27/372] check agents_api.queries.utils
    1227:  ERROR:pytype.matcher Invalid type: <class 'pytype.abstract.function.ParamSpecMatch'>
    ...
    
    1237:  [37/372] check agents_api.common.utils.messages
    1238:  [38/372] check agents_api.common.exceptions.tasks
    1239:  [39/372] check agents_api.common.protocol.sessions
    1240:  [40/372] check agents_api.queries.sessions.get_session
    1241:  [41/372] check agents_api.queries.entries.get_history
    1242:  [42/372] check agents_api.common.retry_policies
    1243:  [43/372] check agents_api.routers.docs.router
    1244:  [44/372] check agents_api.queries.docs.search_docs_hybrid
    1245:  FAILED: /home/runner/work/julep/julep/agents-api/.pytype/pyi/agents_api/queries/docs/search_docs_hybrid.pyi 
    1246:  /home/runner/work/julep/julep/agents-api/.venv/bin/python -m pytype.main --disable pyi-error --imports_info /home/runner/work/julep/julep/agents-api/.pytype/imports/agents_api.queries.docs.search_docs_hybrid.imports --module-name agents_api.queries.docs.search_docs_hybrid --platform linux -V 3.12 -o /home/runner/work/julep/julep/agents-api/.pytype/pyi/agents_api/queries/docs/search_docs_hybrid.pyi --analyze-annotated --nofail --none-is-not-bool --quick --strict-none-binding /home/runner/work/julep/julep/agents-api/agents_api/queries/docs/search_docs_hybrid.py
    1247:  /home/runner/work/julep/julep/agents-api/agents_api/queries/docs/search_docs_hybrid.py:50:1: error: in <module>: Type annotation for embedding does not match type of assignment [annotation-type-mismatch]
    ...
    
    1344:  metadata_filter,
    1345:  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    1346:  search_language,
    1347:  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    1348:  ],
    1349:  ~~~~~~~~~~
    1350:  )
    1351:  ~~~~~
    1352:  For more details, see https://google.github.io/pytype/errors.html#annotation-type-mismatch
    ...
    
    1363:  [55/372] check agents_api.queries.sessions.count_sessions
    1364:  [56/372] check agents_api.queries.entries.create_entries
    1365:  [57/372] check agents_api.queries.chat.prepare_chat_context
    1366:  [58/372] check agents_api.queries.docs.create_doc
    1367:  [59/372] check agents_api.common.nlp
    1368:  [60/372] check agents_api.activities.task_steps.get_value_step
    1369:  [61/372] check agents_api.routers.users.router
    1370:  [62/372] check agents_api.activities.execute_integration
    1371:  FAILED: /home/runner/work/julep/julep/agents-api/.pytype/pyi/agents_api/activities/execute_integration.pyi 
    1372:  /home/runner/work/julep/julep/agents-api/.venv/bin/python -m pytype.main --disable pyi-error --imports_info /home/runner/work/julep/julep/agents-api/.pytype/imports/agents_api.activities.execute_integration.imports --module-name agents_api.activities.execute_integration --platform linux -V 3.12 -o /home/runner/work/julep/julep/agents-api/.pytype/pyi/agents_api/activities/execute_integration.pyi --analyze-annotated --nofail --none-is-not-bool --quick --strict-none-binding /home/runner/work/julep/julep/agents-api/agents_api/activities/execute_integration.py
    1373:  /home/runner/work/julep/julep/agents-api/agents_api/activities/execute_integration.py:11:1: error: in <module>: Can't find module 'models.tools'. [import-error]
    1374:  from ..models.tools import get_tool_args_from_metadata
    1375:  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    1376:  For more details, see https://google.github.io/pytype/errors.html#import-error
    ...
    
    1462:  [148/372] check agents_api.workflows.mem_rating
    1463:  [149/372] check agents_api.workflows.mem_mgmt
    1464:  [150/372] check agents_api.workflows.demo
    1465:  [151/372] check agents_api.queries.tools.delete_tool
    1466:  [152/372] check agents_api.queries.tools.create_tools
    1467:  [153/372] check agents_api.queries.developers.create_developer
    1468:  [154/372] check agents_api.common.utils.types
    1469:  [155/372] check agents_api.queries.executions.get_execution
    1470:  FAILED: /home/runner/work/julep/julep/agents-api/.pytype/pyi/agents_api/queries/executions/get_execution.pyi 
    1471:  /home/runner/work/julep/julep/agents-api/.venv/bin/python -m pytype.main --disable pyi-error --imports_info /home/runner/work/julep/julep/agents-api/.pytype/imports/agents_api.queries.executions.get_execution.imports --module-name agents_api.queries.executions.get_execution --platform linux -V 3.12 -o /home/runner/work/julep/julep/agents-api/.pytype/pyi/agents_api/queries/executions/get_execution.pyi --analyze-annotated --nofail --none-is-not-bool --quick --strict-none-binding /home/runner/work/julep/julep/agents-api/agents_api/queries/executions/get_execution.py
    1472:  /home/runner/work/julep/julep/agents-api/agents_api/queries/executions/get_execution.py:49:5: error: in get_execution: bad return type [bad-return-type]
    ...
    
    1475:  return (
    1476:  ~~~~~~~~
    1477:  sql_query.format(),
    1478:  ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    1479:  [execution_id],
    1480:  ~~~~~~~~~~~~~~~~~~~~~~~
    1481:  )
    1482:  ~~~~~
    1483:  For more details, see https://google.github.io/pytype/errors.html#bad-return-type
    1484:  [156/372] check agents_api.queries.tools.list_tools
    1485:  [157/372] check agents_api.queries.tasks.update_task
    1486:  [158/372] check agents_api.queries.tasks.patch_task
    1487:  [159/372] check agents_api.queries.tasks.delete_task
    1488:  [160/372] check agents_api.queries.executions.get_temporal_workflow_data
    1489:  FAILED: /home/runner/work/julep/julep/agents-api/.pytype/pyi/agents_api/queries/executions/get_temporal_workflow_data.pyi 
    1490:  /home/runner/work/julep/julep/agents-api/.venv/bin/python -m pytype.main --disable pyi-error --imports_info /home/runner/work/julep/julep/agents-api/.pytype/imports/agents_api.queries.executions.get_temporal_workflow_data.imports --module-name agents_api.queries.executions.get_temporal_workflow_data --platform linux -V 3.12 -o /home/runner/work/julep/julep/agents-api/.pytype/pyi/agents_api/queries/executions/get_temporal_workflow_data.pyi --analyze-annotated --nofail --none-is-not-bool --quick --strict-none-binding /home/runner/work/julep/julep/agents-api/agents_api/queries/executions/get_temporal_workflow_data.py
    1491:  /home/runner/work/julep/julep/agents-api/agents_api/queries/executions/get_temporal_workflow_data.py:6:1: error: in <module>: Can't find module 'pycozo.client'. [import-error]
    1492:  from pycozo.client import QueryException
    1493:  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    1494:  For more details, see https://google.github.io/pytype/errors.html#import-error
    1495:  [161/372] check agents_api.rec_sum.utils
    1496:  [162/372] check agents_api.queries.executions.get_paused_execution_token
    1497:  FAILED: /home/runner/work/julep/julep/agents-api/.pytype/pyi/agents_api/queries/executions/get_paused_execution_token.pyi 
    1498:  /home/runner/work/julep/julep/agents-api/.venv/bin/python -m pytype.main --disable pyi-error --imports_info /home/runner/work/julep/julep/agents-api/.pytype/imports/agents_api.queries.executions.get_paused_execution_token.imports --module-name agents_api.queries.executions.get_paused_execution_token --platform linux -V 3.12 -o /home/runner/work/julep/julep/agents-api/.pytype/pyi/agents_api/queries/executions/get_paused_execution_token.pyi --analyze-annotated --nofail --none-is-not-bool --quick --strict-none-binding /home/runner/work/julep/julep/agents-api/agents_api/queries/executions/get_paused_execution_token.py
    1499:  /home/runner/work/julep/julep/agents-api/agents_api/queries/executions/get_paused_execution_token.py:6:1: error: in <module>: Can't find module 'pycozo.client'. [import-error]
    1500:  from pycozo.client import QueryException
    1501:  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    1502:  For more details, see https://google.github.io/pytype/errors.html#import-error
    1503:  [163/372] check agents_api.queries.developers.update_developer
    1504:  [164/372] check agents_api.queries.developers.patch_developer
    1505:  [165/372] check agents_api.rec_sum.generate
    1506:  [166/372] check agents_api.rec_sum.data
    1507:  [167/372] check agents_api.queries.executions.create_execution
    1508:  FAILED: /home/runner/work/julep/julep/agents-api/.pytype/pyi/agents_api/queries/executions/create_execution.pyi 
    1509:  /home/runner/work/julep/julep/agents-api/.venv/bin/python -m pytype.main --disable pyi-error --imports_info /home/runner/work/julep/julep/agents-api/.pytype/imports/agents_api.queries.executions.create_execution.imports --module-name agents_api.queries.executions.create_execution --platform linux -V 3.12 -o /home/runner/work/julep/julep/agents-api/.pytype/pyi/agents_api/queries/executions/create_execution.pyi --analyze-annotated --nofail --none-is-not-bool --quick --strict-none-binding /home/runner/work/julep/julep/agents-api/agents_api/queries/executions/create_execution.py
    1510:  /home/runner/work/julep/julep/agents-api/agents_api/queries/executions/create_execution.py:36:5: error: in create_execution: bad return type [bad-return-type]
    1511:  Expected: tuple[list[str], dict]
    1512:  Actually returned: None
    1513:  execution_id = execution_id or uuid7()
    1514:  ~~~~~~~~~~~~
    1515:  For more details, see https://google.github.io/pytype/errors.html#bad-return-type
    1516:  [168/372] check agents_api.queries.executions.count_executions
    1517:  FAILED: /home/runner/work/julep/julep/agents-api/.pytype/pyi/agents_api/queries/executions/count_executions.pyi 
    1518:  /home/runner/work/julep/julep/agents-api/.venv/bin/python -m pytype.main --disable pyi-error --imports_info /home/runner/work/julep/julep/agents-api/.pytype/imports/agents_api.queries.executions.count_executions.imports --module-name agents_api.queries.executions.count_executions --platform linux -V 3.12 -o /home/runner/work/julep/julep/agents-api/.pytype/pyi/agents_api/queries/executions/count_executions.pyi --analyze-annotated --nofail --none-is-not-bool --quick --strict-none-binding /home/runner/work/julep/julep/agents-api/agents_api/queries/executions/count_executions.py
    1519:  /home/runner/work/julep/julep/agents-api/agents_api/queries/executions/count_executions.py:40:5: error: in count_executions: bad return type [bad-return-type]
    1520:  Expected: tuple[list[str], dict]
    1521:  Actually returned: tuple[Any, list[uuid.UUID]]
    1522:  return (sql_query.format(), [developer_id, task_id])
    1523:  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    1524:  For more details, see https://google.github.io/pytype/errors.html#bad-return-type
    ...
    
    1526:  [170/372] check agents_api.queries.tools.patch_tool
    1527:  [171/372] check agents_api.queries.tools.get_tool_args_from_metadata
    1528:  [172/372] check agents_api.queries.tools.get_tool
    1529:  [173/372] check agents_api.queries.entries.list_entries
    1530:  [174/372] check agents_api.queries.entries.delete_entries
    1531:  [175/372] check agents_api.clients.worker.types
    1532:  [176/372] check agents_api.dependencies.__init__
    1533:  [177/372] check agents_api.queries.executions.list_execution_transitions
    1534:  FAILED: /home/runner/work/julep/julep/agents-api/.pytype/pyi/agents_api/queries/executions/list_execution_transitions.pyi 
    1535:  /home/runner/work/julep/julep/agents-api/.venv/bin/python -m pytype.main --disable pyi-error --imports_info /home/runner/work/julep/julep/agents-api/.pytype/imports/agents_api.queries.executions.list_execution_transitions.imports --module-name agents_api.queries.executions.list_execution_transitions --platform linux -V 3.12 -o /home/runner/work/julep/julep/agents-api/.pytype/pyi/agents_api/queries/executions/list_execution_transitions.pyi --analyze-annotated --nofail --none-is-not-bool --quick --strict-none-binding /home/runner/work/julep/julep/agents-api/agents_api/queries/executions/list_execution_transitions.py
    1536:  /home/runner/work/julep/julep/agents-api/agents_api/queries/executions/list_execution_transitions.py:6:1: error: in <module>: Can't find module 'pycozo.client'. [import-error]
    1537:  from pycozo.client import QueryException
    1538:  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    1539:  For more details, see https://google.github.io/pytype/errors.html#import-error
    1540:  [178/372] check agents_api.queries.tasks.__init__
    1541:  [179/372] check agents_api.queries.executions.get_execution_transition
    1542:  FAILED: /home/runner/work/julep/julep/agents-api/.pytype/pyi/agents_api/queries/executions/get_execution_transition.pyi 
    1543:  /home/runner/work/julep/julep/agents-api/.venv/bin/python -m pytype.main --disable pyi-error --imports_info /home/runner/work/julep/julep/agents-api/.pytype/imports/agents_api.queries.executions.get_execution_transition.imports --module-name agents_api.queries.executions.get_execution_transition --platform linux -V 3.12 -o /home/runner/work/julep/julep/agents-api/.pytype/pyi/agents_api/queries/executions/get_execution_transition.pyi --analyze-annotated --nofail --none-is-not-bool --quick --strict-none-binding /home/runner/work/julep/julep/agents-api/agents_api/queries/executions/get_execution_transition.py
    1544:  /home/runner/work/julep/julep/agents-api/agents_api/queries/executions/get_execution_transition.py:6:1: error: in <module>: Can't find module 'pycozo.client'. [import-error]
    1545:  from pycozo.client import QueryException
    1546:  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    1547:  For more details, see https://google.github.io/pytype/errors.html#import-error
    1548:  [180/372] check agents_api.queries.developers.__init__
    1549:  [181/372] check agents_api.activities.logger
    1550:  [182/372] check agents_api.rec_sum.summarize
    1551:  [183/372] check agents_api.routers.healthz.check_health
    1552:  FAILED: /home/runner/work/julep/julep/agents-api/.pytype/pyi/agents_api/routers/healthz/check_health.pyi 
    1553:  /home/runner/work/julep/julep/agents-api/.venv/bin/python -m pytype.main --disable pyi-error --imports_info /home/runner/work/julep/julep/agents-api/.pytype/imports/agents_api.routers.healthz.check_health.imports --module-name agents_api.routers.healthz.check_health --platform linux -V 3.12 -o /home/runner/work/julep/julep/agents-api/.pytype/pyi/agents_api/routers/healthz/check_health.pyi --analyze-annotated --nofail --none-is-not-bool --quick --strict-none-binding /home/runner/work/julep/julep/agents-api/agents_api/routers/healthz/check_health.py
    1554:  /home/runner/work/julep/julep/agents-api/agents_api/routers/healthz/check_health.py:5:1: error: in <module>: Can't find module 'router'. [import-error]
    1555:  from .router import router
    1556:  ~~~~~~~~~~~~~~~~~~~~~~~~~~
    1557:  For more details, see https://google.github.io/pytype/errors.html#import-error
    1558:  [184/372] check agents_api.queries.users.__init__
    1559:  [185/372] check agents_api.routers.agents.list_agent_tools
    1560:  [186/372] check agents_api.common.protocol.__init__
    1561:  [187/372] check agents_api.queries.tools.__init__
    1562:  [188/372] check tests.test_messages_truncation
    1563:  [189/372] check agents_api.clients.worker.__init__
    1564:  [190/372] check tests.sample_tasks.test_find_selector
    1565:  [191/372] check tests.test_workflow_routes
    1566:  /home/runner/work/julep/julep/agents-api/tests/test_workflow_routes.py:65:1: error: : Stray type comment: object [ignored-type-comment]
    1567:  #   type: object~~~~~~~~~~~~~~~
    1568:  #   type: object
    1569:  /home/runner/work/julep/julep/agents-api/tests/test_workflow_routes.py:114:1: error: : Stray type comment: object [ignored-type-comment]
    1570:  #   type: object~~~~~~~~~~~~~~~
    1571:  #   type: object
    1572:  For more details, see https://google.github.io/pytype/errors.html#ignored-type-comment
    ...
    
    1581:  [200/372] check agents_api.clients.worker.worker
    1582:  [201/372] check agents_api.routers.agents.update_agent_tool
    1583:  [202/372] check tests.test_activities
    1584:  [203/372] check agents_api.routers.sessions.exceptions
    1585:  [204/372] check agents_api.queries.files.__init__
    1586:  [205/372] check agents_api.rec_sum.trim
    1587:  [206/372] check agents_api.workflows.__init__
    1588:  [207/372] check agents_api.routers.tasks.list_task_executions
    1589:  FAILED: /home/runner/work/julep/julep/agents-api/.pytype/pyi/agents_api/routers/tasks/list_task_executions.pyi 
    1590:  /home/runner/work/julep/julep/agents-api/.venv/bin/python -m pytype.main --disable pyi-error --imports_info /home/runner/work/julep/julep/agents-api/.pytype/imports/agents_api.routers.tasks.list_task_executions.imports --module-name agents_api.routers.tasks.list_task_executions --platform linux -V 3.12 -o /home/runner/work/julep/julep/agents-api/.pytype/pyi/agents_api/routers/tasks/list_task_executions.pyi --analyze-annotated --nofail --none-is-not-bool --quick --strict-none-binding /home/runner/work/julep/julep/agents-api/agents_api/routers/tasks/list_task_executions.py
    1591:  /home/runner/work/julep/julep/agents-api/agents_api/routers/tasks/list_task_executions.py:11:1: error: in <module>: Can't find module 'queries.executions.list_executions'. [import-error]
    1592:  from ...queries.executions.list_executions import (
    1593:  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    1594:  list_executions as list_task_executions_query,
    1595:  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    1596:  )
    1597:  ~
    1598:  For more details, see https://google.github.io/pytype/errors.html#import-error
    ...
    
    1610:  [219/372] check agents_api.routers.agents.create_agent_tool
    1611:  [220/372] check agents_api.metrics.__init__
    1612:  [221/372] check agents_api.routers.agents.patch_agent_tool
    1613:  [222/372] check tests.__init__
    1614:  [223/372] check agents_api.models.docs.mmr
    1615:  [224/372] check agents_api.activities.__init__
    1616:  [225/372] check agents_api.common.utils.debug
    1617:  [226/372] check agents_api.routers.agents.delete_agent_tool
    1618:  ninja: build stopped: cannot make progress due to previous errors.
    1619:  Computing dependencies
    1620:  Generated API key since not set in the environment: 00290349124473843217515823647802
    1621:  Sentry DSN not found. Sentry will not be enabled.
    1622:  Analyzing 347 sources with 0 local dependencies
    1623:  Leaving directory '.pytype'
    1624:  ##[error]Process completed with exit code 1.
    

    ✨ CI feedback usage guide:

    The CI feedback tool (/checks) automatically triggers when a PR has a failed check.
    The tool analyzes the failed checks and provides several feedbacks:

    • Failed stage
    • Failed test name
    • Failure summary
    • Relevant error logs

    In addition to being automatically triggered, the tool can also be invoked manually by commenting on a PR:

    /checks "https://github.com/{repo_name}/actions/runs/{run_number}/job/{job_number}"
    

    where {repo_name} is the name of the repository, {run_number} is the run number of the failed check, and {job_number} is the job number of the failed check.

    Configuration options

    • enable_auto_checks_feedback - if set to true, the tool will automatically provide feedback when a check is failed. Default is true.
    • excluded_checks_list - a list of checks to exclude from the feedback, for example: ["check1", "check2"]. Default is an empty list.
    • enable_help_text - if set to true, the tool will provide a help message with the feedback. Default is true.
    • persistent_comment - if set to true, the tool will overwrite a previous checks comment with the new feedback. Default is true.
    • final_update_message - if persistent_comment is true and updating a previous checks message, the tool will also create a new message: "Persistent checks updated to latest commit". Default is true.

    See more information about the checks tool in the docs.

    @creatorrr
    Copy link
    Contributor

    @ellipsis-dev remove references to cozo in agents-api/**/*.py files including comments

    Copy link
    Contributor

    ellipsis-dev bot commented Dec 24, 2024

    I encountered difficulties in removing certain import statements related to 'cozo', specifically from pycozo.client import QueryException, in several files within the 'agents-api/agents_api/routers/tasks/' directory. Despite multiple attempts, the tool did not make any changes.

    Here are the specific files where the removal attempts failed:

    • create_task_execution.py
    • get_task_details.py
    • list_execution_transitions.py
    • stream_transitions_events.py
    • update_execution.py

    I suspect there might be dependencies or other issues preventing the removal. Could you provide guidance on how to proceed with these specific cases? Additionally, if there are any other specific areas or files you would like me to focus on for removing 'cozo' references, please let me know.


    Responding to this comment by @creatorrr. For more information about Ellipsis, check the documentation.

    @Ahmad-mtos Ahmad-mtos marked this pull request as ready for review December 24, 2024 16:35
    @Ahmad-mtos Ahmad-mtos merged commit e015a51 into f/switch-to-pg Dec 24, 2024
    1 check passed
    @Ahmad-mtos Ahmad-mtos deleted the f/configure-routers branch December 24, 2024 16:36
    Copy link
    Contributor

    @ellipsis-dev ellipsis-dev bot left a comment

    Choose a reason for hiding this comment

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

    👍 Looks good to me! Reviewed everything up to 77903ef in 1 minute and 2 seconds

    More details
    • Looked at 4181 lines of code in 96 files
    • Skipped 0 files when reviewing.
    • Skipped posting 1 drafted comments based on config settings.
    1. memory-store/migrations/000015_entries.up.sql:109
    • Draft comment:
      Consider implementing a timescale background job to update the token count instead of using a trigger, as suggested in the comments. This can help improve the performance of the create_entries query.
    • Reason this comment was not posted:
      Confidence changes required: 50%
      The SQL migration script includes a commented-out trigger that is causing performance issues. The comment suggests using a timescale background job instead. This is a valid concern, as triggers can indeed slow down insert operations, especially if they perform complex calculations or updates. The suggestion to use a background job is a good alternative, as it can offload the processing to a separate task, improving the performance of the main transaction.

    Workflow ID: wflow_DeguziIgEXOvmU7x


    You can customize Ellipsis with 👍 / 👎 feedback, review rules, user-specific overrides, quiet mode, and more.

    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
    🧪 PR contains tests
    🔒 Security concerns

    Content Length Validation:
    The content length validation middleware relies on client-reported Content-Length header which could be spoofed. An attacker could potentially bypass size limits by manipulating this header. Consider implementing additional server-side validation of actual payload size.

    ⚡ Recommended focus areas for review

    Security Configuration

    Verify that the content length validation middleware is properly configured and cannot be bypassed. The current implementation relies on client-reported Content-Length header which could be spoofed.

    @app.middleware("http")
    async def validate_content_length(
        request: Request,
        call_next: Callable[[Request], Coroutine[Any, Any, Response]],
    ):
        content_length = request.headers.get("content-length")
    
        if not content_length:
            return Response(status_code=411, content="Content-Length header is required")
    
        if int(content_length) > max_payload_size:
            return Response(status_code=413, content="Payload too large")
    
        return await call_next(request)
    Input Validation

    The validate_transition_targets function has a commented out validation check for next target on finish/error/cancelled transitions. Verify if this validation should be enabled for security.

    case "finish" | "error" | "cancelled":
        pass
    
        ### FIXME: HACK: Fix this and uncomment
    
        ### assert (
        ###     data.next is None
        ### ), "Next target must be None for finish/finish_branch/error/cancelled"
    Incomplete Implementation

    Most of the file is commented out, including critical validation and error handling. This could lead to security issues if deployed without proper implementation.

    # @rewrap_exceptions(
    #     {
    #         QueryException: partialclass(HTTPException, status_code=400),
    #         ValidationError: partialclass(HTTPException, status_code=400),
    #         TypeError: partialclass(HTTPException, status_code=400),
    #     }
    # )
    # @wrap_in_class(
    #     Execution,
    #     one=True,
    #     transform=lambda d: {"id": d["execution_id"], **d},
    #     _kind="inserted",
    # )
    # @cozo_query
    # @increase_counter("create_execution")
    # @beartype
    async def create_execution(
        *,
        developer_id: UUID,
        task_id: UUID,
        execution_id: UUID | None = None,
        data: Annotated[CreateExecutionRequest | dict, dict_like(CreateExecutionRequest)],
    ) -> tuple[list[str], dict]:
        execution_id = execution_id or uuid7()
    
        # developer_id = str(developer_id)
        # task_id = str(task_id)
        # execution_id = str(execution_id)
    
        # if isinstance(data, CreateExecutionRequest):
        #     data.metadata = data.metadata or {}
        #     execution_data = data.model_dump()
        # else:
        #     data["metadata"] = data.get("metadata", {})
        #     execution_data = data
    
        # if execution_data["output"] is not None and not isinstance(
        #     execution_data["output"], dict
        # ):
        #     execution_data["output"] = {OUTPUT_UNNEST_KEY: execution_data["output"]}
    
        # columns, values = cozo_process_mutate_data(
        #     {
        #         **execution_data,
        #         "task_id": task_id,
        #         "execution_id": execution_id,
        #     }
        # )
    
        # insert_query = f"""
        # ?[{columns}] <- $values
    
        # :insert executions {{
        #     {columns}
        # }}
    
        # :returning
        # """
    
        # queries = [
        #     verify_developer_id_query(developer_id),
        #     verify_developer_owns_resource_query(
        #         developer_id,
        #         "tasks",
        #         task_id=task_id,
        #         parents=[("agents", "agent_id")],
        #     ),
        #     insert_query,
        # ]
    
        # return (queries, {"values": values})

    Copy link
    Contributor

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Restore critical validation check for transition targets to prevent invalid state transitions

    The assertion for finish/error/cancelled transitions is commented out with a FIXME
    note. This could allow invalid transitions where next targets are provided when they
    should be None. Uncomment and fix the assertion to ensure proper transition
    validation.

    agents-api/agents_api/queries/executions/create_execution_transition.py [180-188]

     case "finish" | "error" | "cancelled":
    -    pass
    +    assert (
    +        data.next is None
    +    ), "Next target must be None for finish/finish_branch/error/cancelled"
     
    -    ### FIXME: HACK: Fix this and uncomment
    -
    -    ### assert (
    -    ###     data.next is None
    -    ### ), "Next target must be None for finish/finish_branch/error/cancelled"
    -
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: The commented-out validation is critical for preventing invalid state transitions. Without this check, the system could accept invalid transitions where next targets are provided for finish/error/cancelled states, potentially leading to runtime errors or inconsistent state.

    9
    Restore error handling for missing resources to provide clear error messages to API consumers

    Re-enable the commented out error handling for task not found scenarios. The current
    code removes critical error handling that could lead to unclear errors when tasks
    don't exist.

    agents-api/agents_api/routers/tasks/create_task_execution.py [115-121]

    -# except QueryException as e:
    -#     if e.code == "transact::assertion_failure":
    -#         raise HTTPException(
    -#             status_code=status.HTTP_404_NOT_FOUND, detail="Task not found"
    -#         )
    +except QueryException as e:
    +    if e.code == "transact::assertion_failure":
    +        raise HTTPException(
    +            status_code=status.HTTP_404_NOT_FOUND, detail="Task not found"
    +        )
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: The suggestion restores critical error handling code that was commented out, which is essential for providing proper HTTP 404 responses when tasks don't exist. Without this, the API would return unclear errors.

    9
    Fix incorrect Content-Length calculation that could cause request validation issues

    The Content-Length header is being set using sys.getsizeof() which may not
    accurately reflect the actual payload size, especially for JSON data. Use
    len(json.dumps(data)) instead.

    agents-api/tests/fixtures.py [403]

    -headers["Content-Length"] = str(sys.getsizeof(kwargs.get("json", {})))
    +import json
    +headers["Content-Length"] = str(len(json.dumps(kwargs.get("json", {}))) if kwargs.get("json") else 0)
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: Using sys.getsizeof() incorrectly calculates JSON payload size which could cause request validation failures. This is a critical bug fix for request handling.

    9
    Add proper error handling for database connection initialization failures

    Add error handling around DB pool initialization to handle connection failures
    gracefully. Currently, if DB connection fails, the error is not properly handled.

    agents-api/agents_api/app.py [19-20]

    -if not getattr(app.state, "postgres_pool", None):
    -    app.state.postgres_pool = await create_db_pool(db_dsn)
    +try:
    +    if not getattr(app.state, "postgres_pool", None):
    +        app.state.postgres_pool = await create_db_pool(db_dsn)
    +except Exception as e:
    +    logger.error(f"Failed to initialize database pool: {e}")
    +    raise RuntimeError("Could not establish database connection") from e
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The suggestion adds crucial error handling for database connection failures during startup, which could prevent the application from silently failing or crashing when DB connection issues occur.

    8
    Implement streaming for large file downloads to prevent memory exhaustion

    The fetch_file_content function loads the entire file content into memory which
    could cause memory issues with large files. Consider implementing streaming or
    chunked responses.

    agents-api/agents_api/routers/files/list_files.py [14-19]

    -async def fetch_file_content(file_id: UUID) -> str:
    +async def fetch_file_content(file_id: UUID, chunk_size: int = 8192) -> AsyncGenerator[bytes, None]:
         await async_s3.setup()
         key = str(file_id)
    -    content = await async_s3.get_object(key)
    -    return base64.b64encode(content).decode("utf-8")
    +    async for chunk in async_s3.get_object_stream(key, chunk_size):
    +        yield base64.b64encode(chunk)
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Loading entire file contents into memory could cause OOM issues with large files. Implementing streaming is critical for handling large files efficiently.

    8
    Add error handling for file content retrieval failures

    Add error handling for file content retrieval to handle cases where the file doesn't
    exist in blob storage or is corrupted.

    agents-api/agents_api/routers/files/get_file.py [29-30]

    -# Fetch the file content from blob storage
    -file.content = await fetch_file_content(file.id)
    +try:
    +    # Fetch the file content from blob storage
    +    file.content = await fetch_file_content(file.id)
    +except Exception as e:
    +    raise HTTPException(
    +        status_code=status.HTTP_404_NOT_FOUND,
    +        detail="File content could not be retrieved"
    +    ) from e
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: The suggestion adds important error handling for file content retrieval failures, providing clear HTTP 404 responses when files cannot be retrieved from blob storage.

    7
    General
    Strengthen test assertion to explicitly verify successful response status

    The assertion for get_agent_exists test only checks that status code is not 404,
    which could pass with any other error code. Add explicit check for 200 success
    status.

    agents-api/tests/test_agent_routes.py [93-102]

     @test("route: get agent exists")
     def _(make_request=make_request, agent=test_agent):
         agent_id = str(agent.id)
     
         response = make_request(
             method="GET",
             url=f"/agents/{agent_id}",
         )
     
    -    assert response.status_code != 404
    +    assert response.status_code == 200
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: The current test assertion is too permissive as it only checks for non-404 status. The suggested change ensures the test properly verifies a successful response (200), making the test more precise and reliable.

    7
    Add pagination support to prevent performance degradation when listing large collections

    Consider implementing pagination for the list_files endpoint to prevent performance
    issues when dealing with large file collections. Add limit and offset parameters
    similar to other list endpoints in the codebase.

    agents-api/agents_api/routers/files/list_files.py [23-26]

     @router.get("/files", tags=["files"])
     async def list_files(
         x_developer_id: Annotated[UUID, Depends(get_developer_id)],
    +    limit: int = 100,
    +    offset: int = 0
     ) -> list[File]:
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Adding pagination is important for performance and scalability when dealing with large file collections. The suggestion follows consistent patterns used in other list endpoints in the codebase.

    7

    Copy link
    Contributor

    ellipsis-dev bot commented Dec 27, 2024

    I have removed several references to 'cozo' in the agents-api/**/*.py files, including import statements and comments. However, there are still some instances that need to be addressed, particularly in the activities and models directories. If there are any specific files or lines you would like me to prioritize, please let me know. Additionally, if there are any other specific instructions or clarifications needed, feel free to provide them.


    Responding to this comment by @creatorrr. For more information about Ellipsis, check the documentation.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    4 participants