Implementation:0xSteph Pentest ai Exec Context
| Knowledge Sources | |
|---|---|
| Domains | Async, ContextVars, State_Management |
| Last Updated | 2026-10-23 00:00 GMT |
Overview
Concrete tool for threading engagement ID and FindingsDB through the async stack via contextvars, provided by the pentest-ai engine.
Description
The exec_context module uses Python contextvars to make the active engagement_id and FindingsDB available throughout the async call stack without explicit passing. The exec_context context manager sets both contextvars, and get_exec_context retrieves them from anywhere in the async task. This lets SecurityTool.execute() persist tool results to the database without every call site passing the DB explicitly.
Usage
Import this module when you need to access the current engagement ID or FindingsDB from within an async tool execution context. Used by the agent loop and tool wrappers.
Code Reference
Source Location
- Repository: 0xSteph_Pentest_ai
- File: engine/exec_context.py
- Lines: 1-41
Signature
current_engagement_id: ContextVar[str] = ContextVar("current_engagement_id", default="")
current_findings_db: ContextVar[Any] = ContextVar("current_findings_db", default=None)
@contextmanager
def exec_context(engagement_id: str, db: Any): ...
def get_exec_context() -> tuple[str, Any]: ...
Import
from engine.exec_context import exec_context, get_exec_context, current_engagement_id, current_findings_db
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| engagement_id | str | Yes | Active engagement UUID |
| db | Any | Yes | FindingsDB instance |
Outputs
| Name | Type | Description |
|---|---|---|
| (engagement_id, db) | tuple[str, Any] | Retrieved via get_exec_context() |
Usage Examples
Set and Get Context
from engine.exec_context import exec_context, get_exec_context
from engine.findings_db import FindingsDB
db = FindingsDB()
with exec_context("eng-abc-123", db):
# Any code in this block can retrieve context
eid, db_ref = get_exec_context()
print(f"Running engagement: {eid}")
# SecurityTool.execute() can persist without explicit db param