AgentSkillsCN

project-knowledge-graph

项目知识提取与图结构规则。借助 Serena MCP,明确如何分析、记录并维护项目架构知识。

SKILL.md
--- frontmatter
description: Project knowledge extraction and graph structure rules. Defines how to analyze, document, and maintain project architecture knowledge using Serena MCP.
name: project-knowledge-graph
allowed-tools: ["Read", "Grep", "Glob", "Task"]

Project Knowledge Graph

Purpose

Define rules and patterns for extracting, structuring, and maintaining comprehensive project knowledge using Serena MCP's symbol-level analysis capabilities.


Knowledge Graph Structure

Core Entities

Entity TypeDescriptionSerena Tool
ModuleFile or directory that groups related codelist_dir, get_symbols_overview
SymbolClass, function, method, variablefind_symbol
DependencyImport or reference relationshipfind_referencing_symbols
PatternRepeated design pattern or conventionPattern analysis
ConventionNaming, structure, or style rulesConvention extraction

Graph Schema

yaml
KnowledgeGraph:
  project:
    name: string
    root_path: string
    language: string[]

  modules:
    - path: string
      type: "file" | "directory"
      symbols: Symbol[]
      dependencies: Dependency[]

  symbols:
    - name: string
      type: "class" | "function" | "method" | "variable" | "interface"
      file: string
      line: number
      visibility: "public" | "private" | "protected"
      dependencies: string[]
      dependents: string[]

  patterns:
    - name: string
      type: "structural" | "behavioral" | "creational"
      instances: SymbolRef[]

  conventions:
    - category: "naming" | "structure" | "import" | "export"
      rule: string
      examples: string[]

Extraction Rules

Phase 1: Structure Discovery

  1. Directory Scan

    code
    Serena: list_dir with recursive=true
    Output: File tree with types
    
  2. Entry Points Identification

    • Look for: main, index, app, __init__
    • Configuration files: *.config.*, *.json, *.yaml

Phase 2: Symbol Extraction

  1. Per-File Symbol Overview

    code
    Serena: get_symbols_overview for each file
    Extract: Classes, functions, methods, variables
    
  2. Symbol Details

    code
    Serena: find_symbol with include_body=true
    Extract: Full definition, parameters, return types
    

Phase 3: Dependency Mapping

  1. Outgoing Dependencies

    • Import statements analysis
    • Reference tracking within symbols
  2. Incoming Dependencies (Dependents)

    code
    Serena: find_referencing_symbols for key symbols
    Map: Which symbols depend on this one
    

Phase 4: Pattern Recognition

PatternDetection Method
FactoryClasses ending with Factory, methods named create*
RepositoryClasses ending with Repository, implementing CRUD
ServiceClasses ending with Service, with business logic
ControllerClasses ending with Controller, handling requests
StrategyInterface + multiple implementations
Observersubscribe, notify, emit patterns

Phase 5: Convention Extraction

  1. Naming Conventions

    • Class naming: PascalCase, camelCase
    • Function naming: verb prefixes (get, set, is, has)
    • File naming: kebab-case, snake_case
  2. Structure Conventions

    • Directory organization patterns
    • Import grouping rules
    • Export patterns

Memory Storage

Using Serena Memory for Persistence

Store extracted knowledge in Serena memories for future sessions:

code
Serena: write_memory
Key: "project-knowledge-graph.md"
Content: Serialized knowledge graph

Memory Structure

markdown
# Project Knowledge Graph
Generated: [timestamp]

## Project Overview
- Name: [project_name]
- Languages: [languages]
- Entry Points: [entry_points]

## Module Map
[Hierarchical module structure]

## Key Symbols
[Top-level classes and functions with roles]

## Dependency Graph
[Mermaid diagram or text representation]

## Detected Patterns
[Pattern instances with locations]

## Conventions
[Extracted naming and structure rules]

Query Interface

Common Queries

QueryImplementation
"Find all services"search_for_pattern: "class.*Service"
"Show dependencies of X"find_referencing_symbols + find_symbol
"List all entry points"Search for main/index patterns
"Get module structure"list_dir + get_symbols_overview

Query Response Format

yaml
QueryResult:
  query: string
  matches:
    - symbol: string
      file: string
      line: number
      relevance: number
      context: string

Update Strategy

Incremental Updates

When files change, update only affected portions:

  1. Detect changed files (git diff or file watcher)
  2. Re-extract symbols for changed files
  3. Update dependency edges
  4. Revalidate patterns

Full Refresh Triggers

  • Major refactoring
  • New module addition
  • Framework upgrade
  • User request

Integration with Feature Injection

The knowledge graph enables intelligent feature injection by providing:

  1. Insertion Points: Know where to add new code based on patterns
  2. Style Matching: Follow existing conventions automatically
  3. Dependency Awareness: Understand import requirements
  4. Impact Analysis: Predict what will be affected by changes