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:AISecurityLab Hackagent StrategyLibrary

From Leeroopedia
Knowledge Sources
Domains Security_Testing, Information_Retrieval
Last Updated 2026-10-29 00:00 GMT

Overview

Concrete in-memory data structure holding named jailbreak strategies indexed by response embeddings, with nearest-neighbor retrieval to find strategies that beat similar refusals, provided by the HackAgent AutoDAN-Turbo attack.

Description

'StrategyLibrary is a dataclass storing named strategies as dicts with Strategy, Definition, Example (prompts that worked), Score (improvement deltas), and Embeddings (response vectors). add() records a new strategy or extends an existing one by name. retrieve() takes a query embedding (the target's last response) and returns a tuple (valid, strategies): effective strategies (score ≥ 5 returns immediately, ≥ 2 collected as "use", < 2 as "avoid"). Retrieval uses L2 distance via numpy. The library is model-free — it holds vectors, not text to embed; the attack embeds and hands vectors in.

Usage

  • AutoDAN-Turbo attack: The attack owns the library, adds strategies during warm-up and lifelong phases, and retrieves against the last response.
  • Research output: The library's all() method returns the accumulated strategies as a research artifact.

Code Reference

Source Location

Signature

@dataclass
class StrategyLibrary:
    strategies: dict[str, dict[str, Any]] = field(default_factory=dict)

    def add(
        self, strategy: dict[str, Any], example: str,
        score: float, vector: Optional[Sequence[float]],
    ) -> None: ...

    def size(self) -> int: ...

    def all(self) -> dict[str, dict[str, Any]]: ...

    def retrieve(
        self, query_vector: Optional[Sequence[float]], k: int = 5
    ) -> tuple[bool, list[dict[str, Any]]]: ...

Import

from hackagent.attacks.techniques.adaptive.autodan_turbo.library import StrategyLibrary

I/O Contract

Inputs

Name Type Required Description
strategy dict Yes {"Strategy": str, "Definition": str}
example str Yes Prompt that worked
score float Yes Improvement delta
vector Sequence[float] No Response embedding for retrieval
query_vector Sequence[float] No Query embedding for retrieve()

Outputs

Name Type Description
retrieve() tuple[bool, list[dict]] (valid, strategies) — use or avoid
all() dict[str, dict] All accumulated strategies
size() int Number of strategies

Usage Examples

Add and Retrieve Strategies

from hackagent.attacks.techniques.adaptive.autodan_turbo.library import StrategyLibrary

lib = StrategyLibrary()
lib.add(
    strategy={"Strategy": "Storytelling", "Definition": "Narrative engagement"},
    example="Once upon a time, a character needed to...",
    score=3.5,
    vector=[0.1, 0.2, 0.3],
)
valid, strategies = lib.retrieve([0.15, 0.25, 0.35])
print(f"Valid: {valid}, found {len(strategies)} strategies")

Inspect the Full Library

for name, info in lib.all().items():
    print(f"{name}: {info['Definition']}")

Related Pages

Implements Principle

Page Connections

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