Implementation:0xSteph Pentest ai ToolResultCache
| Knowledge Sources | |
|---|---|
| Domains | Caching, SQLite, Tool_Results |
| Last Updated | 2026-10-23 00:00 GMT |
Overview
Concrete tool for SQLite-backed, TTL'd caching of tool results keyed on tool, target, intensity, and args, provided by the pentest-ai engine.
Description
The ToolResultCache class provides async SQLite-backed caching of tool results. Keys are SHA-256 hashes of (tool, target, intensity, sorted args). Default TTLs are tuned per category: recon results stay fresh for 1 hour, vuln scans for 6 hours, exploitation tools never cache. The cache lives in a single SQLite file at ~/.cache/pentest-ai/cache.db (override via PENTEST_AI_CACHE env var) so back-to-back scans reuse work transparently.
Usage
Import this class when you need to cache and retrieve tool results to avoid redundant scans. Used by the agent loop and tool wrappers.
Code Reference
Source Location
- Repository: 0xSteph_Pentest_ai
- File: engine/cache.py
- Lines: 1-175
Signature
def make_key(tool: str, target: str, intensity: str, args: dict[str, Any] | None) -> str: ...
def ttl_for(category: str | None) -> int: ...
class ToolResultCache:
def __init__(self, db_path: str | Path | None = None) -> None: ...
async def init(self) -> None: ...
async def get(self, tool: str, target: str, intensity: str, args: dict | None, category: str | None = None) -> dict | None: ...
async def set(self, tool: str, target: str, intensity: str, args: dict | None, result: dict, category: str | None = None) -> None: ...
async def close(self) -> None: ...
Import
from engine.cache import ToolResultCache, make_key, ttl_for
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| tool | str | Yes | Tool name |
| target | str | Yes | Target URL/host |
| intensity | str | Yes | Scan intensity level |
| args | None | No | Tool arguments dict |
| category | None | No | Tool category for TTL lookup |
Outputs
| Name | Type | Description |
|---|---|---|
| cached result | None | Cached result if fresh, None if expired/missing |
Usage Examples
Cache and Retrieve Tool Results
import asyncio
from engine.cache import ToolResultCache
async def main():
cache = ToolResultCache()
await cache.init()
# Check cache before running a scan
result = await cache.get("nmap", "10.0.0.1", "normal", {"ports": "1-1000"}, category="recon")
if result:
print(f"Cache hit: {result}")
else:
# Run the scan...
result = {"open_ports": [22, 80, 443]}
await cache.set("nmap", "10.0.0.1", "normal", {"ports": "1-1000"}, result, category="recon")
print(f"Cached result for next time")
await cache.close()
asyncio.run(main())