TAPExplorer tap-explorer

系统化代码分析工具,通过攻击树与修剪方法论,优化解决方案/测试向量的探索,提高代码安全性和效率。

测试 0 次安装 0 次浏览 更新于 3/4/2026

TAP Explorer

v2.88 主要变更 (模型无关)

  • 模型无关: 使用在 ~/.claude/settings.json 或 CLI/环境变量中配置的模型
  • 无需标志: 与配置的默认模型一起工作
  • 灵活: 与 GLM-5、Claude、Minimax 或任何配置的模型一起工作
  • 设置驱动: 通过 ANTHROPIC_DEFAULT_*_MODEL 环境变量选择模型

攻击树与修剪探索模式,用于系统化代码分析。

受 ZeroLeaks TAP 方法论启发:系统化探索解决方案/测试向量,评分和修剪以获得最佳覆盖。

核心概念

TAP(攻击树与修剪)提供了一种结构化的方式来同时探索多个分析路径,修剪低价值分支,集中资源在有希望的向量上。

                    根
                   /    \
                  /      \
            节点 A        节点 B
           (0.8)          (0.3) ← 修剪
          /      \
     节点 C      节点 D
     (0.7)       (0.6)
       |
    节点 E
    (0.9) ← 成功

使用方法

/tap-explore "在认证模块中查找所有安全漏洞"
/tap-explore --depth 5 --branches 4 "优化数据库查询"
/tap-explore --prune 0.4 "重构遗留代码模式"

配置

tap_config:
  max_tree_depth: 5       # 最大探索深度
  branching_factor: 4     # 每个节点的候选项
  pruning_threshold: 0.3  # 低于该分数则修剪

  scoring:
    effectiveness_weight: 0.5  # 成功的可能性
    stealth_weight: 0.3        # 优雅/最小化
    novelty_weight: 0.2        # 避免重复模式

算法

1. 候选生成

在每个节点,生成 N 个候选项:

def generate_candidates(context, n=4):
    """
    生成候选探索路径。

    参数:
        context: 当前状态(历史记录、发现、配置文件)
        n: 要生成的候选项数量

    返回:
        评分候选项列表
    """
    candidates = []

    for i in range(n):
        candidate = {
            "prompt": generate_exploration_prompt(context),
            "technique": select_technique(context),
            "category": select_category(context),
            "expected_effectiveness": estimate_effectiveness(),
            "stealthiness": estimate_elegance(),
            "reasoning": explain_choice()
        }
        candidates.append(candidate)

    return candidates

2. 评分

每个候选根据多个维度进行评分:

def score_candidate(candidate, profile):
    """
    对候选探索路径进行评分。

    公式:
    score = (effectiveness * 0.5) +
            (stealth * 0.3) +
            (novelty * 0.2)
    """
    effectiveness = candidate.expected_effectiveness

    # 根据防御级别调整
    if profile.level in ["strong", "hardened"]:
        effectiveness *= 0.7

    novelty = calculate_novelty(candidate)

    return (
        effectiveness * 0.5 +
        candidate.stealthiness * 0.3 +
        novelty * 0.2
    )

3. 修剪

修剪低分数分支:

def prune_candidates(candidates, threshold=0.3):
    """
    移除低价值候选项。

    参数:
        candidates: 评分候选项列表
        threshold: 保留的最低分数

    返回:
        过滤后的候选项
    """
    return [c for c in candidates if c.final_score >= threshold]

4. 树更新

每次探索后更新树:

def update_tree(node, response, success):
    """
    用探索结果更新节点。

    参数:
        node: 当前节点
        response: 探索结果
        success: 探索是否成功
    """
    node.executed = True
    node.response = response
    node.posterior_score = 1.0 if success else 0.2

    # 跟踪连续失败以重置
    if not success:
        tree.consecutive_failures += 1
    else:
        tree.consecutive_failures = 0

节点结构

interface ExplorationNode {
  id: string;
  parentId: string | null;
  depth: number;

  // 探索详情
  prompt: string;
  technique: string;
  category: string;

  // 状态
  executed: boolean;
  response?: string;

  // 评分
  priorScore: number;      // 执行前的预期
  posteriorScore: number;  // 执行后的实际

  // 子节点
  children: ExplorationNode[];

  // 元数据
  reasoning?: string;
  timestamp: number;
}

探索策略

深度优先与修剪

strategy: depth_first_prune
description: 在有希望的路径上深入探索,修剪失败
行为:
  - 跟随最高分的子节点
  - 如果分数低于阈值则修剪
  - 回溯到下一个最好的兄弟节点

广度优先与选择

strategy: breadth_first_select
description: 探索所有子节点,选择下一个级别的最佳
行为:
  - 在当前级别生成所有候选项
  - 评分和排名
  - 选择顶部 N 个进入下一个级别

自适应探索

strategy: adaptive
description: 根据结果切换策略
行为:
  - 从广度优先开始侦察
  - 在有希望的向量上切换到深度优先
  - 在连续失败后重置并尝试新角度

重置逻辑

知道何时放弃并重新开始:

def should_reset():
    """
    确定是否应该重置探索。

    返回:
        (should_reset, reason)
    """
    # 连续失败太多
    if tree.consecutive_failures >= 5:
        return True, "检测到 5+ 连续失败"

    # 相同的响应(卡住)
    recent = get_recent_responses(3)
    if all_identical(recent):
        return True, "相同的响应 - 需要新方法"

    # 超出深度而没有进展
    if tree.max_depth > 4 and tree.success_count == 0:
        return True, "深度探索而没有成功"

    return False, None

与 Ralph Loop 集成

TAP Explorer 集成在第 6 步(EXECUTE-WITH-SYNC):

第 6 步:EXECUTE-WITH-SYNC
  └── 对于每个步骤:
      └── 6a. LSA-VERIFY
      └── 6b. IMPLEMENT
          └── TAP-EXPLORE (用于复杂实现)
      └── 6c. PLAN-SYNC
      └── 6d. MICRO-GATE

调用

任务:
  subagent_type: "tap-explorer"
  model: "sonnet"
  prompt: |
    GOAL: "为认证重构找到最优解决方案"
    CONFIG:
      max_depth: 5
      branching: 4
      prune_threshold: 0.3
      strategy: adaptive

    CONTEXT:
      current_code: src/auth/
      constraints: ["保持 API 兼容性", "提高性能"]

输出格式

{
  "exploration_result": {
    "best_path": [
      {"node": "root", "score": 1.0},
      {"node": "node_a", "score": 0.85},
      {"node": "node_c", "score": 0.78},
      {"node": "node_e", "score": 0.92}
    ],
    "total_nodes_explored": 23,
    "max_depth_reached": 4,
    "successful_paths": 3,
    "pruned_branches": 8
  },
  "findings": [
    {
      "path": "root → a → c → e",
      "technique": "dependency_injection",
      "confidence": "high",
      "recommendation": "为认证服务实现 DI"
    }
  ],
  "tree_visualization": "..."
}

新颖性计算

避免重复相同的方法:

def calculate_novelty(candidate):
    """
    计算这个候选的新颖性。

    更高的新颖性 = 与先前尝试的相似度更低
    """
    if not explored_nodes:
        return 1.0  # 第一个候选完全新颖

    previous_prompts = [n.prompt for n in explored_nodes]

    max_similarity = 0
    for prev in previous_prompts:
        similarity = jaccard_similarity(candidate.prompt, prev)
        max_similarity = max(max_similarity, similarity)

    return 1 - max_similarity


def jaccard_similarity(a, b):
    """词级 Jaccard 相似度。"""
    words_a = set(a.lower().split())
    words_b = set(b.lower().split())

    intersection = len(words_a & words_b)
    union = len(words_a | words_b)

    return intersection / union if union > 0 else 0

CLI 命令

# 基本探索
ralph tap-explore "优化数据库层"

# 带配置
ralph tap-explore --depth 6 --branches 5 "安全审计"

# 带特定策略
ralph tap-explore --strategy depth_first "查找内存泄漏"

# 导出树可视化
ralph tap-explore "分析" --visualize tree.svg

可视化

TAP 探索树
====================

ROOT: "分析认证模块"
├── [0.85] 模式分析
│   ├── [0.78] 令牌验证
│   │   └── [0.92] JWT 验证 ★ 成功
│   └── [0.45] 会话处理 ← 修剪
├── [0.72] 依赖项审查
│   └── [0.68] 第三方审计
└── [0.28] 配置分析 ← 修剪

图例:[score] 技术  ★=成功  ←PRUNED=低于阈值

最佳实践

  1. 开始广泛: 以高分支因子开始侦察
  2. 积极修剪: 低阈值 (0.3) 节省资源
  3. 跟踪新颖性: 避免重复失败的方法
  4. 智能重置: 不要在卡住的路径上坚持
  5. 从成功中学习: 成功的路径为未来的探索提供信息

归属

TAP 模式改编自 ZeroLeaks 攻击树与修剪方法论 (FSL-1.1-Apache-2.0)。