| 1 | # message_queue.py DOX |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | - Own the `message_queue.py` helper module. |
| 6 | - This module stores and drains queued user messages for a context. |
| 7 | - Keep this file-level DOX profile synchronized with `message_queue.py` because this directory is intentionally flat. |
| 8 | |
| 9 | ## Ownership |
| 10 | |
| 11 | - `message_queue.py` owns the runtime implementation. |
| 12 | - `message_queue.py.dox.md` owns durable notes about responsibilities, contracts, side effects, and verification for that implementation. |
| 13 | - Top-level functions: |
| 14 | - `get_queue(context: 'AgentContext') -> list`: Get current queue from context.data. |
| 15 | - `_get_next_seq(context: 'AgentContext') -> int`: Get next sequence number. |
| 16 | - `_sync_output(context: 'AgentContext')`: Sync queue to output_data for frontend polling. |
| 17 | - `add(context: 'AgentContext', text: str, attachments: list[str] | None=..., item_id: str | None=...) -> dict`: Add message to queue. Attachments should be filenames, will be converted to full paths. |
| 18 | - `remove(context: 'AgentContext', item_id: str | None=...) -> int`: Remove item(s). If item_id is None, clears all. Returns remaining count. |
| 19 | - `pop_first(context: 'AgentContext') -> dict | None`: Remove and return first item. |
| 20 | - `pop_item(context: 'AgentContext', item_id: str) -> dict | None`: Remove and return specific item. |
| 21 | - `has_queue(context: 'AgentContext') -> bool`: Check if queue has items. |
| 22 | - `log_user_message(context: 'AgentContext', message: str, attachment_paths: list[str], message_id: str | None=..., source: str=...)`: Log user message to console and UI. Used by message API and queue processing. |
| 23 | - `send_message(context: 'AgentContext', item: dict, source: str=...)`: Send a single queued message (log + communicate). |
| 24 | - `send_next(context: 'AgentContext') -> bool`: Send next queued message. Returns True if sent, False if queue empty. |
| 25 | - `send_all_aggregated(context: 'AgentContext') -> int`: Aggregate and send all queued messages as one. Returns count of items sent. |
| 26 | - Notable constants/configuration names: `QUEUE_KEY`, `QUEUE_SEQ_KEY`, `UPLOAD_FOLDER`. |
| 27 | |
| 28 | ## Runtime Contracts |
| 29 | |
| 30 | - Helper modules own reusable framework APIs and must preserve public callers unless all callers, tests, and docs are updated together. |
| 31 | - Update this file whenever public functions, classes, persistence behavior, path/security assumptions, side effects, or cross-module contracts change. |
| 32 | - Observed side-effect areas: filesystem reads, filesystem deletion, settings/state persistence. |
| 33 | - Imported dependency areas include: `helpers`, `helpers.print_style`, `os`, `typing`, `uuid`. |
| 34 | |
| 35 | ## Key Concepts |
| 36 | |
| 37 | - Important called helpers/classes observed in the source: `context.set_data`, `get_queue`, `context.set_output_data`, `_sync_output`, `queue.pop`, `context.log.log`, `log_user_message`, `context.communicate`, `pop_first`, `has_queue`, `join`, `context.get_data`, `att.startswith`, `_get_next_seq`, `uuid.uuid4`, `UserMessage`, `send_message`, `guids.generate_id`, `os.path.basename`, `PrintStyle`. |
| 38 | - Keep request/response, tool, or helper semantics documented here at the same time as source changes. |
| 39 | |
| 40 | ## Work Guidance |
| 41 | |
| 42 | - Preserve public helper APIs used by core code and plugins unless every caller is updated. |
| 43 | - Keep path, auth, secret, persistence, network, and subprocess behavior explicit and bounded. |
| 44 | - Prefer adding cohesive helper functions here only when behavior is reused across modules. |
| 45 | |
| 46 | ## Verification |
| 47 | |
| 48 | - Run targeted tests for changed helper behavior; run security regressions for auth, filesystem, WebSocket, tunnel, upload, or secret-handling helpers. |
| 49 | - No direct test reference was found by name search; choose the nearest behavioral test or perform a focused smoke check. |
| 50 | |
| 51 | ## Child DOX Index |
| 52 | |
| 53 | No child DOX files. |