Return an `AsyncCodeModeContext` for the running kernel. Use as an async context manager:: async with cm.get_context() as ctx: ctx.create_cell("x = 1") Parameters ---------- skip_validation : bool, default False When False (the default), a dry-run compi
(
*,
skip_validation: bool = False,
skip_staleness_check: bool = False,
)
| 199 | |
| 200 | |
| 201 | def get_context( |
| 202 | *, |
| 203 | skip_validation: bool = False, |
| 204 | skip_staleness_check: bool = False, |
| 205 | ) -> AsyncCodeModeContext: |
| 206 | """Return an `AsyncCodeModeContext` for the running kernel. |
| 207 | |
| 208 | Use as an async context manager:: |
| 209 | |
| 210 | async with cm.get_context() as ctx: |
| 211 | ctx.create_cell("x = 1") |
| 212 | |
| 213 | Parameters |
| 214 | ---------- |
| 215 | skip_validation : bool, default False |
| 216 | When False (the default), a dry-run compile check runs on exit |
| 217 | to catch syntax errors, multiply-defined names, and cycles |
| 218 | *before* any graph mutations are applied. The check is cheap |
| 219 | and should almost never be disabled. Only set to True when you |
| 220 | intentionally need to insert code that would fail validation |
| 221 | (e.g. incomplete stubs the user plans to fix by hand). |
| 222 | skip_staleness_check : bool, default False |
| 223 | When False (the default), `edit_cell` raises |
| 224 | :class:`StaleCellError` if the agent tries to overwrite a cell |
| 225 | whose code has changed since the agent last read it (e.g. via |
| 226 | `ctx.cells[cell_id].code`). Set to True to overwrite blindly — |
| 227 | useful when the agent intentionally discards prior content. |
| 228 | """ |
| 229 | runtime_ctx = _get_runtime_context() |
| 230 | if not isinstance(runtime_ctx, KernelRuntimeContext): |
| 231 | raise RuntimeError("code mode requires a running kernel context") |
| 232 | cell_manager = runtime_ctx._app.cell_manager if runtime_ctx._app else None |
| 233 | return AsyncCodeModeContext( |
| 234 | runtime_ctx._kernel, |
| 235 | cell_manager=cell_manager, |
| 236 | skip_validation=skip_validation, |
| 237 | skip_staleness_check=skip_staleness_check, |
| 238 | ) |
| 239 | |
| 240 | |
| 241 | class StaleCellError(RuntimeError): |
searching dependent graphs…