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

Implementation:0xSteph Pentest ai WorkingMemory summary for llm

From Leeroopedia
Knowledge Sources
Domains Security_Testing, LLM_Agents
Last Updated 2026-10-23 12:00 GMT

Overview

Concrete per-engagement in-flight state object that maintains findings, endpoints, coverage matrix, captured auth, and produces compact LLM-readable state snapshots for the autonomous agent loop.

Description

The WorkingMemory dataclass is the in-memory state the LLM agent reasons over while the engagement is live. The findings DB stays the authoritative store; working memory is what the agent sees.

Key components:

  • findings: In-memory FindingRef list (full records in DB)
  • endpoints_seen: Set of EndpointKey(method, path) discovered
  • coverage: CoverageMatrix tracking (endpoint, bug_class) pairs tried and hit
  • captured_auth: Credentials/tokens captured during engagement (never persisted across engagements)
  • iterations: Loop iteration count
  • consecutive_bad_responses: Counter for non-cooperative LLM detection
  • exit_reason: Typed exit reason set by run_agent_loop

summary_for_llm() produces a compact state snapshot with:

  • Findings by severity, recent findings (last 10)
  • Endpoints sample (first 30)
  • Untried (endpoint, bug_class) pairs (first 20) so the LLM picks what's most likely to find a bug
  • Coverage stats, auth kinds, last action/observation

Privacy rules: Per-engagement isolation by default; cross-engagement sharing is opt-in and exports only pattern-level (hashed) data, never raw findings or credentials.

Usage

Import this when you need to:

  • Initialize working memory for an agent loop engagement
  • Produce compact state snapshots for the LLM
  • Track coverage and findings during an autonomous engagement

Code Reference

Source Location

  • Repository: pentest-ai
  • File: engine/working_memory.py

Signature

BUG_CLASSES: tuple[str, ...] = (
    "sqli", "xss_reflected", "xss_stored", "ssrf", "ssti", "xxe",
    "idor", "auth_bypass", "jwt_alg_none", ...
    # 34 bug classes total
)

@dataclass
class WorkingMemory:
    engagement_id: str
    target: str
    findings: list[FindingRef] = field(default_factory=list)
    endpoints_seen: set[EndpointKey] = field(default_factory=set)
    coverage: CoverageMatrix = field(default_factory=CoverageMatrix)
    captured_auth: list[CapturedAuth] = field(default_factory=list)
    last_action: dict[str, Any] | None = None
    last_observation: dict[str, Any] | None = None
    cross_engagement_share: bool = False
    iterations: int = 0
    consecutive_bad_responses: int = 0
    exit_reason: str | None = None

    def summary_for_llm(self) -> dict[str, Any]:
        """Compact state snapshot for LLM reasoning."""

    def add_finding(self, ref: FindingRef) -> None:
    def add_endpoint(self, ep: EndpointKey) -> None:
    def add_auth(self, auth: CapturedAuth) -> None:
    def export_patterns(self) -> list[EngagementPattern]:
    def to_redacted_json(self) -> str:

def make_working_memory(engagement_id: str, target: str) -> WorkingMemory:
    """Factory. Validates engagement_id and target are non-empty."""

Import

from engine.working_memory import WorkingMemory, make_working_memory, BUG_CLASSES, CoverageMatrix, EndpointKey, FindingRef, CapturedAuth

I/O Contract

Inputs (summary_for_llm)

Name Type Required Description
(none) — — Reads WorkingMemory fields

Outputs (summary_for_llm)

Name Type Description
returns engagement_id, target, iteration, findings_by_severity, recent_findings, endpoints_seen_sample, untried_pairs_sample, coverage, auth_kinds, last_action, last_observation

Usage Examples

Creating Working Memory and Getting a Snapshot

from engine.working_memory import make_working_memory, FindingRef, EndpointKey

# 1. Initialize
memory = make_working_memory("eng-123", "https://app.example.com")

# 2. Record findings and endpoints during the loop
memory.add_finding(FindingRef(
    id="f1", severity="high", category="web",
    bug_class="sqli", target="https://app.example.com",
))
memory.add_endpoint(EndpointKey(method="GET", path="/api/users"))

# 3. Get compact state for the LLM
state = memory.summary_for_llm()
print(f"Iteration: {state['iteration']}")
print(f"Findings: {state['findings_total']}")
print(f"Untried pairs: {len(state['untried_pairs_sample'])}")

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

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