Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:0xSteph Pentest ai agents Persistent Engagement Storage

From Leeroopedia
Knowledge Sources
Domains Security, Data_Storage, Persistence
Last Updated 2026-10-22 12:00 GMT

Overview

Design pattern for zero-token-cost persistent storage of engagement data using SQLite with a dual-backend abstraction layer.

Description

Persistent Engagement Storage is the principle of storing all engagement findings (hosts, services, vulnerabilities, credentials, attack chains, session logs) in a local SQLite database that survives across Claude Code sessions. It addresses the critical problem of context loss: AI agent sessions have limited context windows, and without persistent storage, all findings are lost when a session closes.

The principle uses a dual-backend design: native `sqlite3` CLI when available, with an automatic `python3` stdlib sqlite3 fallback. This ensures the database works on any system with either sqlite3 or python3 installed. The database is the single source of truth — agents read from it to avoid duplicate work and write to it after each discovery.

Usage

Use this principle when:

  • Designing a data persistence layer for AI-driven security tools
  • Ensuring engagement continuity across multiple sessions
  • Providing a queryable store for findings that agents can read and write
  • Enabling cross-session handoff with structured reports

Theoretical Basis

The storage model uses a normalized relational schema with engagement-scoped tables:

Failed to parse (syntax error): {\displaystyle \text{DB} = \{\text{engagements}, \text{hosts}, \text{services}, \text{vulns}, \text{credentials}, \text{chains}, \text{session\_log}\} }

All tables are scoped by `engagement_id`, enabling multiple concurrent engagements. The dual-backend abstraction routes queries:

Failed to parse (syntax error): {\displaystyle \text{db\_exec}(sql) = \begin{cases} \text{sqlite3}(sql) & \text{if } \text{sqlite3} \in \text{PATH} \\ \text{python3\_sqlite3}(sql) & \text{elif } \text{python3} \text{ has } \text{sqlite3} \end{cases} }

Pseudo-code Logic:

# Abstract storage pattern (NOT real implementation)
class EngagementStore:
    def __init__(self, db_path, engagement_id):
        self.backend = detect_backend()  # sqlite3 or python3
        self.engagement_id = engagement_id

    def add_host(self, ip, hostname=None, os=None, role=None):
        sql = f"INSERT OR REPLACE INTO hosts (engagement_id, ip, ...) VALUES (...)"
        self.exec(sql)

    def list_vulns(self, severity=None, status=None):
        where = f"engagement_id='{self.engagement_id}'"
        if severity: where += f" AND severity='{severity}'"
        if status: where += f" AND status='{status}'"
        return self.exec_csv(f"SELECT ... FROM vulns WHERE {where}")

    def require_engagement(self):
        count = self.exec(f"SELECT COUNT(*) FROM engagements WHERE id='{self.engagement_id}'")
        if count == 0:
            raise Error(f"Engagement not found. Run: findings.sh init {self.engagement_id}")

Practical Guide

  1. Initialize early: Create the engagement record at the start of every engagement (`findings.sh init`).
  2. Set env var: Export `PENTEST_AI_ENGAGEMENT` so all scripts know the active engagement.
  3. Write after each discovery:*** Persist hosts, services, vulns, and creds immediately — don't batch.
  4. Read before scanning:*** Check existing records (`findings.sh list hosts`) to avoid duplicate work.
  5. Use SQL escaping:*** Wrap all user-provided values with `escape_sql()` before SQL interpolation.
  6. Dual-backend awareness:*** The library auto-detects sqlite3 or python3 — no manual configuration needed.
  7. Cross-session handoff:*** Use `handoff.sh` at session end to produce a Markdown summary for the next operator.
  8. JSON export:*** Use `findings.sh export` for full data export when generating reports.

Related Pages

Implemented By

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment