> ## Documentation Index
> Fetch the complete documentation index at: https://uom-demo.vercel.app/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# local_context

> [<span className="flex items-center gap-1"><Icon icon="external-link"/>View Source</span>](https://github.com/corovcam/Universal-Object-Mapping/tree/main/services/orchestrator/src/uom_deep_agent/local_context.py)

Middleware for injecting local context into system prompt.

Detects git state, project structure, package managers, runtimes, and
directory layout by running a bash script via the backend. Because the
script executes inside the backend (local shell or remote sandbox), the
same detection logic works regardless of where the agent runs.

#### uom\_deep\_agent.local\_context.build\_detect\_script

```python theme={null}
def build_detect_script() -> str
```

[<span className="flex items-center gap-1"><Icon icon="external-link" />View Source</span>](https://github.com/corovcam/Universal-Object-Mapping/blob/main/services/orchestrator/src/uom_deep_agent/local_context.py#L304)

Concatenate all section functions into the full detection script.

Independent sections run as parallel background jobs writing to temp
files, then results are concatenated in the original display order.
The header (CWD / IN\_GIT) and project section (sets ROOT) run first
because later sections depend on their variables.

**Returns**:

Complete bash heredoc ready for `backend.execute()`.

## uom\_deep\_agent.local\_context.LocalContextState Objects

```python theme={null}
class LocalContextState(AgentState)
```

[<span className="flex items-center gap-1"><Icon icon="external-link" />View Source</span>](https://github.com/corovcam/Universal-Object-Mapping/blob/main/services/orchestrator/src/uom_deep_agent/local_context.py#L352)

State for local context middleware.

#### LocalContextState.local\_context: `NotRequired[str]`

Formatted local context: cwd, project, package managers,
runtimes, git, test command, files, tree, Makefile.

## uom\_deep\_agent.local\_context.LocalContextMiddleware Objects

```python theme={null}
class LocalContextMiddleware(AgentMiddleware)
```

[<span className="flex items-center gap-1"><Icon icon="external-link" />View Source</span>](https://github.com/corovcam/Universal-Object-Mapping/blob/main/services/orchestrator/src/uom_deep_agent/local_context.py#L374)

Inject local context (git state, project structure, etc.) into the system prompt.

Runs a bash detection script via `backend.execute()` on first interaction
and again after each summarization event, stores the result in state, and
appends it to the system prompt on every model call.

Because the script runs inside the backend, it works for both local shells
and remote sandboxes.

#### LocalContextMiddleware.\_\_init\_\_

```python theme={null}
def __init__(backend: _ExecutableBackend) -> None
```

[<span className="flex items-center gap-1"><Icon icon="external-link" />View Source</span>](https://github.com/corovcam/Universal-Object-Mapping/blob/main/services/orchestrator/src/uom_deep_agent/local_context.py#L387)

Initialize with a backend that supports shell execution.

**Arguments**:

* `backend` - Backend instance that provides shell command execution.

#### LocalContextMiddleware.before\_agent

```python theme={null}
def before_agent(state: LocalContextState,
                 runtime: Runtime) -> dict[str, Any] | None
```

[<span className="flex items-center gap-1"><Icon icon="external-link" />View Source</span>](https://github.com/corovcam/Universal-Object-Mapping/blob/main/services/orchestrator/src/uom_deep_agent/local_context.py#L433)

Run context detection on first interaction and refresh after summarization.

On the first invocation, runs the detection script and stores the result.
After a summarization event (indicated by a new `_summarization_event`
in state), re-runs the script to capture any environment changes that
occurred during the session.

**Arguments**:

* `state` - Current agent state.
* `runtime` - Runtime context.

**Returns**:

State update with `local_context` populated on success. On a
post-summarization refresh failure, returns a state update
recording the cutoff (without `local_context`) to prevent
retry loops.

Returns `None` if context is already set and no refresh is
needed, or if initial detection fails.

#### LocalContextMiddleware.wrap\_model\_call

```python theme={null}
def wrap_model_call(
        request: ModelRequest,
        handler: Callable[[ModelRequest], ModelResponse]) -> ModelResponse
```

[<span className="flex items-center gap-1"><Icon icon="external-link" />View Source</span>](https://github.com/corovcam/Universal-Object-Mapping/blob/main/services/orchestrator/src/uom_deep_agent/local_context.py#L507)

Inject local context into system prompt.

**Arguments**:

* `request` - The model request being processed.
* `handler` - The handler function to call with the modified request.

**Returns**:

The model response from the handler.

#### LocalContextMiddleware.awrap\_model\_call

```python theme={null}
async def awrap_model_call(
    request: ModelRequest,
    handler: Callable[[ModelRequest],
                      Awaitable[ModelResponse]]) -> ModelResponse
```

[<span className="flex items-center gap-1"><Icon icon="external-link" />View Source</span>](https://github.com/corovcam/Universal-Object-Mapping/blob/main/services/orchestrator/src/uom_deep_agent/local_context.py#L524)

Inject local context into system prompt (async).

**Arguments**:

* `request` - The model request being processed.
* `handler` - The async handler function to call with the modified request.

**Returns**:

The model response from the handler.
