Semantic Code Transcoder for LLMs
YCG (YAML Code Graph) is a high-performance Rust tool that transforms source code into semantic knowledge graphs optimized for Large Language Models. By combining SCIP (Stack Graph Code Indexing Protocol) for global symbol resolution with Tree-sitter for local enrichment, YCG generates "Pseudo-RDF" YAML representations that maximize semantic density while minimizing token consumption.
New to YCG? Check out the Quick Start Guide for a 5-minute tutorial!
For complete CLI documentation, see the CLI Reference.
# Automatic language detection and indexing
ycg index
# Generate optimized YAML graph
ycg generate -i index.scip -o graph.yaml --compactThat's it! YCG automatically detects your project language (Rust or TypeScript) and generates the semantic graph.
| Language | Status | SCIP Indexer | Tree-sitter Grammar | Notes |
|---|---|---|---|---|
| TypeScript | β Stable | @sourcegraph/scip-typescript |
β Full support | Recommended for production use |
| JavaScript | β Stable | @sourcegraph/scip-typescript |
β Full support | Same tooling as TypeScript |
| Rust | π§ Beta | rust-analyzer |
β Full support | Core functionality works, some edge cases |
| Python | π Planned | scip-python |
π In progress | Roadmap for v2.0 |
| Java | π Planned | scip-java |
π In progress | Roadmap for v2.0 |
Recommendation: For production use, TypeScript/JavaScript projects have the most mature support and extensive testing. Rust support is functional but may have edge cases in complex macro-heavy codebases.
Traditional code context (raw files) is inefficient and noisy for LLMs. YCG addresses this by:
- π Token Compression: Achieves >1.5x compression ratio through YAML anchors/aliases and semantic deduplication
- π§ Logic Lifting: Extracts guard clauses and preconditions from imperative code into declarative predicates
- π Semantic Topology: Explicitly maps references, scopes, and dependencies using deterministic identifiers
- β‘ Blazing Fast: Processes 10,000+ files in <60s using Rust + SCIP + Tree-sitter
- π― LOD Control: Adjustable Level of Detail (Low/Medium/High) to fit token budgets
YCG implements a hybrid two-pass pipeline:
- Deserializes SCIP Protobuf indexes
- Generates deterministic xxHash-64 identifiers for all symbols
- Builds anchor registry for YAML reference resolution
- Definitions: Extracts symbol nodes with Tree-sitter enrichment (signatures, docs, logic)
- References: Resolves caller/callee relationships using scope analysis
- Optimization: Applies LOD filtering and optional adjacency list compression
- SCIP Indexer: Global symbol resolution without reimplementing static analysis
- Tree-sitter Parser: Local enrichment for TypeScript, JavaScript, Rust
- Logic Lifter: Detects guard clauses (
if-throw,if-return) and transforms to preconditions - YAML Emitter: Serializes with anchors (&) and aliases (*) for minimal token usage
git clone https://github.com/yourusername/ycg.git
cd ycg
cargo build --release
sudo ./install.shThis installs the ycg command globally. See INSTALL.md for detailed installation options.
- Rust 1.75+ with Cargo
- SCIP Indexer for your target language:
npm install -g @sourcegraph/scip-typescript # For TypeScript/JavaScript # Or: scip-python, scip-java, etc.
cargo install --path crates/ycg_cliYCG can automatically detect your project language and generate the SCIP index:
cd my-project
# Automatically detect language and generate index + graph
ycg index # Creates index.scip
ycg generate -i index.scip -o graph.yaml --compactYCG provides powerful optimization strategies to reduce token consumption by 40-70% while maintaining semantic accuracy. All optimizations are opt-in and can be combined for maximum efficiency.
Create a ycg.config.json in your project root to define optimization settings:
{
"output": {
"format": "adhoc",
"compact": true,
"ignoreFrameworkNoise": true
},
"ignore": {
"useGitignore": true,
"customPatterns": [
"**/node_modules/**",
"**/dist/**"
]
},
"include": [
"src/**/*.ts"
]
}Configuration Precedence: CLI flags override config file settings.
Filters out low-significance symbols (local variables, anonymous blocks) while preserving architectural information.
# Enable graph compaction
ycg generate -i index.scip -o graph.yaml --compact
# Expected reduction: ~50% in graph section sizeBefore (default):
graph:
- from: validateUser_a3f2
to: local_11_6d84
type: defines
- from: local_11_6d84
to: String_b2c3
type: referencesAfter (compact):
graph:
validateUser_a3f2:
calls: [Error_b8c1]
references: [String_b2c3]Token Reduction: ~50% in graph section
Removes boilerplate patterns from framework-heavy codebases (NestJS, TypeORM).
# Enable framework noise reduction
ycg generate -i index.scip -o graph.yaml --ignore-framework-noiseWhat gets filtered:
- Dependency injection constructors (only assignment statements)
- Decorator metadata (
@ApiProperty,@IsString, etc.) - DTO boilerplate while preserving property names and types
Before:
- id: UserDto_a1b2
sig: |
class UserDto {
@ApiProperty()
@IsString()
name: string;
@ApiProperty()
@IsEmail()
email: string;
}After:
- id: UserDto_a1b2
sig: |
class UserDto {
name: string;
email: string;
}Token Reduction: ~30% in framework-heavy projects
Position-based format that eliminates verbose YAML key-value pairs.
# Use ad-hoc format
ycg generate -i index.scip -o graph.yaml --output-format adhocYAML Format:
_defs:
- id: validateUser_a3f2
n: validateUser
t: functionAd-Hoc Format:
_defs:
- "validateUser_a3f2|validateUser|function"Token Reduction: ~20-30% in definitions section
Control which files are processed using glob patterns.
# Include only specific files
ycg generate -i index.scip -o graph.yaml --include "src/**/*.ts"
# Exclude test files
ycg generate -i index.scip -o graph.yaml --exclude "**/*.test.ts"
# Disable gitignore (process all files)
ycg generate -i index.scip -o graph.yaml --no-gitignorePattern Precedence: Include first, then exclude. If a file matches both, it's excluded.
Maximize token reduction by combining all strategies:
ycg generate -i index.scip -o graph.yaml \
--compact \
--ignore-framework-noise \
--output-format adhoc \
--include "src/**/*.ts" \
--exclude "**/*.test.ts"Expected Results:
- Token Reduction: 60-70% compared to default output
- Processing Time: <5% overhead
- Semantic Accuracy: 100% preserved for significant symbols
| Strategy | Token Reduction | Use Case |
|---|---|---|
| Default (no flags) | 0% (baseline) | Full detail needed |
--compact |
~50% | Focus on architecture |
--ignore-framework-noise |
~30% | NestJS, TypeORM projects |
--output-format adhoc |
~25% | Minimize syntax overhead |
| All combined | 60-70% | Maximum efficiency |
See the examples/ directory for ready-to-use configurations:
ycg.config.minimal.json- Minimal configurationycg.config.full.json- All optimizations enabledycg.config.typescript.json- TypeScript project optimized
For detailed guidance on choosing optimizations, see OPTIMIZATION_GUIDE.md.
YCG's ad-hoc format supports three granularity levels that control the amount of detail included in each symbol definition. This allows you to optimize token usage based on your specific analysis needs.
| Level | Format | Use Case | Token Savings |
|---|---|---|---|
| Level 0: Default | ID|Name|Type |
Maximum token efficiency, structural information only | Baseline |
| Level 1: Signatures | ID|Signature|Type |
API contracts and data flow analysis | +15-20% tokens |
| Level 2: Logic | ID|Signature|Type|logic:steps |
Security analysis, business logic review | +30-40% tokens |
# Level 0: Default (structural only)
ycg generate -i index.scip -o graph.yaml --output-format adhoc
# Level 1: Include inline signatures
ycg generate -i index.scip -o graph.yaml --output-format adhoc --adhoc-inline-signatures
# Level 2: Include inline signatures + logic
ycg generate -i index.scip -o graph.yaml --output-format adhoc --adhoc-inline-logicNote: The --adhoc-inline-logic flag automatically enables signatures, so you don't need both flags.
You can also set the granularity level in your ycg.config.json:
{
"output": {
"format": "adhoc",
"adhocGranularity": "logic"
}
}Valid values: "default", "signatures", "logic"
Precedence: CLI flags override config file settings.
_defs:
- UserDto_698e|UserDto|class
- StoreService_4844|StoreService|class
- StoreService_checkStock_7fed|checkStock|method
- StoreService_purchase_99a1|purchase|methodUse when: You only need to understand the codebase structure and relationships.
_defs:
- UserDto_698e|UserDto|class
- StoreService_4844|StoreService|class
- StoreService_checkStock_7fed|checkStock(itemId:str):bool|method
- StoreService_purchase_99a1|purchase(user:User,itemId:str):Promise<Order>|methodUse when: You need to understand API contracts, parameter types, and return values.
Type Abbreviations:
stringβstrnumberβnumbooleanβbool- Custom types preserved (e.g.,
User,Order)
_defs:
- UserDto_698e|UserDto|class
- StoreService_4844|StoreService|class
- StoreService_checkStock_7fed|checkStock(itemId:str):bool|method|logic:return(item.qty > 0)
- StoreService_purchase_99a1|purchase(user,itemId)|method|logic:check(stock>0);check(user.balance>=price);action(deduct_balance);action(save_order)
- AuthService_validate_dd39|validate(user)|method|logic:check(user.isActive && (user.isAdmin || user.isSuper))
- RolesGuard_canActivate_c9aa|canActivate(ctx)|method|logic:get(user_roles);match(required_roles)?allow:denyUse when: You need to understand business logic, security checks, and control flow.
Logic Keywords:
check(condition)- Conditional checks, guard clausesaction(operation)- Side effects, state mutationsreturn(expression)- Return valuesmatch(pattern)?true:false- Pattern matching, ternary operatorsget(source)- Data retrieval operations
Logic steps are chained with semicolons (;) to represent execution sequence.
See the examples/ directory for ready-to-use configurations:
ycg.config.granularity-default.json- Level 0 (structural only)ycg.config.granularity-signatures.json- Level 1 (with signatures)ycg.config.granularity-logic.json- Level 2 (with logic)
Before/After Comparison (NestJS API example):
| Level | Definitions Tokens | Total Tokens | vs. Default |
|---|---|---|---|
| Level 0 (Default) | 2,450 | 8,120 | Baseline |
| Level 1 (Signatures) | 2,890 | 8,560 | +5.4% |
| Level 2 (Logic) | 3,380 | 9,050 | +11.5% |
Recommendation: Start with Level 0 for initial exploration, then use Level 1 for API analysis, and Level 2 only when you need to understand business logic or security-critical code.
For detailed information about logic keywords and extraction rules, see LOGIC_KEYWORDS.md and GRANULARITY_GUIDE.md.
Option A: Automatic (Recommended)
cd my-project
ycg index # Auto-detects Rust or TypeScript
ycg index -d ./src -o custom.scip # Custom directory and outputOption B: Manual
Navigate to your project and create the index manually:
cd my-project
# For TypeScript/JavaScript projects
npm install # Install dependencies first
scip-typescript index .
# For Rust projects
rust-analyzer scip-export --output index.scipConvert the SCIP index to optimized YAML:
# Standard mode (flat list of edges)
ycg generate -i index.scip -o graph.yaml
# Compact mode (adjacency list - recommended)
ycg generate -i index.scip -o graph.yaml --compact
# High detail mode (includes locals and externals)
ycg generate -i index.scip -o graph.yaml --lod 2
# Specify project root explicitly
ycg generate -i index.scip -o graph.yaml --root /path/to/projectAutomatically detect project language and generate SCIP index.
| Flag | Description | Default |
|---|---|---|
-d, --directory |
Project directory to index | . (current) |
-o, --output |
Output path for SCIP index | index.scip |
Supported Languages:
- TypeScript/JavaScript (β
Stable): Detects
package.jsonortsconfig.json, usesscip-typescript - Rust (π§ Beta): Detects
Cargo.toml, usesrust-analyzer
See the Language Support section for detailed status and recommendations.
Generate YAML graph from existing SCIP index.
| Flag | Description | Default |
|---|---|---|
-i, --input |
Path to SCIP index file | Required |
-o, --output |
Path to output YAML file | stdout |
-r, --root |
Project root directory | Parent of input file |
-l, --lod |
Level of Detail (0=Low, 1=Medium, 2=High) | 1 |
-c, --compact |
Enable adjacency list optimization | false |
--ignore-framework-noise |
Remove framework boilerplate patterns | false |
--output-format <FORMAT> |
Output format: yaml or adhoc |
yaml |
--include <PATTERN> |
Include files matching glob pattern (repeatable) | All files |
--exclude <PATTERN> |
Exclude files matching glob pattern (repeatable) | None |
--no-gitignore |
Disable automatic gitignore processing | false |
- Low (0): Classes and functions only, excludes variables/interfaces
- Medium (1): Includes public methods, filters local variables and parameters
- High (2): Full detail including private methods, locals, and external references
function validateUser(name: string) {
if (name.length === 0) throw new Error("Empty name");
return { valid: true, name };
}_meta:
name: ycg-v1.3
version: 1.3.0
_defs:
- id: validateUser_a3f2
n: validateUser
t: function
sig: 'function validateUser(name: string)'
logic:
pre:
- 'must avoid: name.length === 0'
graph:
validateUser_a3f2:
calls:
- Error_b8c1When using --compact, edges are grouped by source node and type:
graph:
source_node_id:
calls: [target1, target2]
references: [target3]ycg/
βββ crates/
β βββ ycg_core/ # Core library
β β βββ src/
β β β βββ lib.rs # Main conversion logic
β β β βββ model.rs # Data structures (SymbolNode, ReferenceEdge)
β β β βββ enricher.rs # Tree-sitter integration
β β βββ build.rs # Protobuf code generation
β β βββ Cargo.toml
β βββ ycg_cli/ # CLI application
β βββ src/main.rs
β βββ Cargo.toml
βββ examples/
β βββ nestjs-api-ts/ # Example NestJS project
β βββ simple-ts/ # Minimal TypeScript example
βββ proto/
β βββ scip.proto # SCIP protocol definition
βββ Cargo.toml # Workspace configuration
The examples/ directory contains sample projects:
cd examples/nestjs-api-ts
npm install
ycg index # Auto-detects TypeScript
ycg generate -i index.scip -o context_map.yaml --compactcd examples/nestjs-api-ts
npm install
scip-typescript index .
../../target/release/ycg_cli generate -i index.scip -o context_map.yaml --compactcd examples/simple-ts
ycg index # Auto-detects TypeScript
ycg generate -i index.scip -o output.yamlcd /path/to/ycg
ycg index # Auto-detects Rust
ycg generate -i index.scip -o ycg_graph.yaml --compact --lod 2YCG automatically reports compression metrics:
--- MΓ©trica de Densidade ---
Input Total Tokens (CΓ³digo Bruto): 45230
Output Total Tokens (Grafo YAML): 28145
Taxa de CompressΓ£o: 1.61x
--------------------------
- SCIP Protobuf deserialization
- Tree-sitter enrichment (TypeScript β , JavaScript β , Rust π§)
- Logic lifting (guard clauses)
- Deterministic xxHash-64 identifiers
- LOD filtering
- Adjacency list optimization
- Token density metrics
- Configuration file support
- Streaming mode for >100MB indexes
- Python language support (grammar + indexer integration)
- Java language support (grammar + indexer integration)
- Rust macro expansion improvements
- Property-based testing suite
- CLI Reference - Complete command-line interface documentation
- Quick Start Guide - 5-minute tutorial
- Language Support - Detailed language support status and limitations
- Granularity Guide - Ad-hoc granularity levels explained
- Optimization Guide - Token optimization strategies
- Troubleshooting Guide - Common issues and solutions
- Installation Guide - Detailed installation instructions
- Changelog - Version history and release notes
Having issues? Check the Troubleshooting Guide for common problems and solutions:
- Language detection issues
- Missing indexer tools (rust-analyzer, scip-typescript)
- SCIP export failures
- Performance optimization tips
Contributions are welcome! This project follows the SPARC methodology for development. See .kiro/specs/ycg-core/requirements.md for detailed requirements.
cargo build
cargo test
cargo clippy -- -D warningsLicensed under the Apache License, Version 2.0. See LICENSE for details.
- SCIP Protocol by Sourcegraph
- Tree-sitter parsing library
- xxHash non-cryptographic hash algorithm