| 1 | # defer.py DOX |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | - Own the `defer.py` helper module. |
| 6 | - This module runs deferred or child async tasks on managed event-loop threads. |
| 7 | - Keep this file-level DOX profile synchronized with `defer.py` because this directory is intentionally flat. |
| 8 | |
| 9 | ## Ownership |
| 10 | |
| 11 | - `defer.py` owns the runtime implementation. |
| 12 | - `defer.py.dox.md` owns durable notes about responsibilities, contracts, side effects, and verification for that implementation. |
| 13 | - Classes: |
| 14 | - `EventLoopThread` (no explicit base class) |
| 15 | - `terminate(self)` |
| 16 | - `run_coroutine(self, coro)` |
| 17 | - `ChildTask` (no explicit base class) |
| 18 | - `DeferredTask` (no explicit base class) |
| 19 | - `start_task(self, func: Callable[..., Coroutine[Any, Any, Any]], *args, **kwargs)` |
| 20 | - `add_done_callback(self, callback: Callable[[Future], Any]) -> None` |
| 21 | - `is_ready(self) -> bool` |
| 22 | - `result_sync(self, timeout: Optional[float]=...) -> Any` |
| 23 | - `async result(self, timeout: Optional[float]=...) -> Any` |
| 24 | - `kill(self, terminate_thread: bool=...) -> None` |
| 25 | - `kill_children(self) -> None` |
| 26 | - `is_alive(self) -> bool` |
| 27 | - `restart(self, terminate_thread: bool=...) -> None` |
| 28 | - Notable constants/configuration names: `T`, `THREAD_BACKGROUND`. |
| 29 | |
| 30 | ## Runtime Contracts |
| 31 | |
| 32 | - Helper modules own reusable framework APIs and must preserve public callers unless all callers, tests, and docs are updated together. |
| 33 | - `DeferredTask` retains its callable and arguments only while an invocation is active; completion and `kill()` clear those references after the running coroutine has taken its own snapshot. |
| 34 | - `add_done_callback()` forwards to the current invocation's concurrent future and rejects calls before `start_task()`; callbacks observe `is_alive() == False` and must remain lightweight. |
| 35 | - Task results remain available after completion. `restart()` can restart an active invocation, but a completed invocation has no retained call recipe and must be started again explicitly. |
| 36 | - Update this file whenever public functions, classes, persistence behavior, path/security assumptions, side effects, or cross-module contracts change. |
| 37 | - Observed side-effect areas: scheduler state. |
| 38 | - Imported dependency areas include: `asyncio`, `concurrent.futures`, `dataclasses`, `threading`, `typing`. |
| 39 | |
| 40 | ## Key Concepts |
| 41 | |
| 42 | - Important called helpers/classes observed in the source: `TypeVar`, `threading.Lock`, `self._start`, `asyncio.set_event_loop`, `self.loop.run_forever`, `loop.is_running`, `asyncio.run_coroutine_threadsafe`, `EventLoopThread`, `self._start_task`, `self.kill`, `self.event_loop_thread.run_coroutine`, `self.kill_children`, `asyncio.get_running_loop`, `func`, `asyncio.iscoroutine`, `Future`, `asyncio.wrap_future`, `asyncio.current_task`, `asyncio.new_event_loop`, `threading.Thread`. |
| 43 | - Keep request/response, tool, or helper semantics documented here at the same time as source changes. |
| 44 | |
| 45 | ## Work Guidance |
| 46 | |
| 47 | - Preserve public helper APIs used by core code and plugins unless every caller is updated. |
| 48 | - Keep path, auth, secret, persistence, network, and subprocess behavior explicit and bounded. |
| 49 | - Prefer adding cohesive helper functions here only when behavior is reused across modules. |
| 50 | |
| 51 | ## Verification |
| 52 | |
| 53 | - Run targeted tests for changed helper behavior; run security regressions for auth, filesystem, WebSocket, tunnel, upload, or secret-handling helpers. |
| 54 | - Related tests observed by source search: |
| 55 | - `tests/test_office_document_store.py` |
| 56 | |
| 57 | ## Child DOX Index |
| 58 | |
| 59 | No child DOX files. |