Skip to content

Commit 8b2cabb

Browse files
jayhackcodegen-bot
andauthored
docs: act via code blog [wip] (#101)
# Motivation <!-- Why is this change necessary? --> # Content <!-- Please include a summary of the change --> # Testing <!-- How was the change tested? --> # Please check the following before marking your PR as ready for review - [ ] I have added tests for my changes - [ ] I have updated the documentation or added new documentation as needed - [ ] I have read and agree to the [Contributor License Agreement](../CLA.md) --------- Co-authored-by: codegen-bot <[email protected]>
1 parent 6395457 commit 8b2cabb

13 files changed

+461
-11
lines changed

CONTRIBUTING.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# Contributing to Graph Sitter
1+
# Contributing to Codegen
22

3-
Thank you for your interest in contributing to Graph Sitter! This document outlines the process and guidelines for contributing.
3+
Thank you for your interest in contributing to Codegen! This document outlines the process and guidelines for contributing.
44

55
## Contributor License Agreement
66

7-
By contributing to Graph Sitter, you agree that:
7+
By contributing to Codegen, you agree that:
88

99
1. Your contributions will be licensed under the project's license.
1010
2. You have the right to license your contribution under the project's license.

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# Codegen
22

33
[![Documentation](https://img.shields.io/badge/docs-docs.codegen.com-blue)](https://docs.codegen.com)
4+
[![Slack Community](https://img.shields.io/badge/slack-community-4A154B?logo=slack)](https://community.codegen.com)
5+
[![Twitter Follow](https://img.shields.io/twitter/follow/codegen)](https://twitter.com/codegen)
46

57
[Codegen](https://docs.codegen.com) is a python library for manipulating codebases.
68

7-
Write code that transforms code. Codegen combines the parsing power of [Tree-sitter](https://tree-sitter.github.io/tree-sitter/) with the graph algorithms of [rustworkx](https://github.com/Qiskit/rustworkx) to enable scriptable, multi-language code manipulation at scale.
89

910
```python
1011
from codegen import Codebase
@@ -21,6 +22,8 @@ for function in codebase.functions:
2122
function.move_to_file('deprecated.py')
2223
```
2324

25+
Write code that transforms code. Codegen combines the parsing power of [Tree-sitter](https://tree-sitter.github.io/tree-sitter/) with the graph algorithms of [rustworkx](https://github.com/Qiskit/rustworkx) to enable scriptable, multi-language code manipulation at scale.
26+
2427
## Installation
2528
**This library requires Python 3.12 – 3.13.**
2629
```
@@ -31,6 +34,7 @@ uv pip install codegen
3134

3235
- [Docs](https://docs.codegen.com)
3336
- [Get Started](https://docs.codegen.com/introduction/getting-started)
37+
- [Contributing](CONTRIBUTING.md)
3438

3539

3640
## Why Codegen?

docs/blog/act-via-code.mdx

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
---
2+
title: "Act via Code"
3+
icon: "code"
4+
iconType: "solid"
5+
description: "The path to advanced code manipulation agents"
6+
---
7+
8+
<Frame caption="Voyager (Jim Fan)">
9+
<img src="/images/nether-portal.png" />
10+
</Frame>
11+
12+
13+
# Act via Code
14+
15+
Two and a half years since the launch of the GPT-3 API, code assistants have emerged as the most powerful and practically useful applications of LLMs. The rapid adoption of AI-powered IDEs and prototype builders isn't surprising — code is structured, deterministic, and rich with patterns, making it an ideal domain for machine learning. As model capabilities continue to scale, we're seeing compounding improvements in code understanding and generation.
16+
17+
Yet there's a striking gap between what AI agents can understand and what they can actually do. While they can reason about complex architectural changes, debug intricate issues, and propose sophisticated refactors, they often can't execute these ideas. The ceiling isn't intelligence or context—it's the ability to manipulate code at scale. Large-scale modifications remain unreliable or impossible, not because agents don't understand what to do, but because they lack the right interfaces to do it.
18+
19+
The bottleneck isn't intelligence — it's tooling. By giving AI models the ability to write and execute code that modifies code, we're about to unlock an entire class of tasks that agents already understand but can't yet perform. Code execution environments represent the most expressive tool we could offer an agent—enabling composition, abstraction, and systematic manipulation of complex systems. When paired with ever-improving language models, this will unlock another step function improvement in AI capabilities.
20+
21+
## Beating Minecraft with Code Execution
22+
23+
In mid-2023, a research project called [Voyager](https://voyager.minedojo.org) made waves: it effectively solved Minecraft, performing several multiples better than the prior SOTA on many important dimensions. This was a massive breakthrough — previous reinforcement learning systems had struggled for years with even basic Minecraft tasks.
24+
25+
While the AI community was focused on scaling intelligence, Voyager demonstrated something more fundamental: the right tools can unlock entirely new tiers of capability. The same GPT-4 model that struggled with Minecraft using traditional frameworks achieved remarkable results when allowed to write and execute code. This wasn't about raw intelligence—it was about giving the agent a more expressive way to act.
26+
27+
<Frame>
28+
<img src="/images/voyager-performance.png" />
29+
</Frame>
30+
31+
The breakthrough came from a simple yet powerful insight: let the AI write code. Instead of limiting the agent to primitive "tools," Voyager allowed GPT-4 to write and execute [JS programs](https://github.com/MineDojo/Voyager/tree/main/skill_library/trial2/skill/code) that controlled Minecraft actions through a clean API:
32+
33+
```javascript
34+
async function chopSpruceLogs(bot) {
35+
const spruceLogCount = bot.inventory.count(mcData.itemsByName.spruce_log.id);
36+
const logsToMine = 3 - spruceLogCount;
37+
if (logsToMine > 0) {
38+
bot.chat("Chopping down spruce logs...");
39+
await mineBlock(bot, "spruce_log", logsToMine);
40+
bot.chat("Chopped down 3 spruce logs.");
41+
} else {
42+
bot.chat("Already have 3 spruce logs in inventory.");
43+
}
44+
}
45+
```
46+
47+
This approach transformed the agent's capabilities. Rather than being constrained to atomic actions like `equipItem(...)`, it could create higher-level operations like [`craftShieldWithFurnace()`](https://github.com/MineDojo/Voyager/blob/main/skill_library/trial2/skill/code/craftShieldWithFurnace.js) through composing JS APIs. The system also implemented a memory mechanism, storing successful programs for reuse in similar situations—effectively building its own library of proven solutions it could later refer to and adapt to similar circumstances.
48+
49+
<Frame>
50+
<img src="/images/voyager-retrieval.png" />
51+
</Frame>
52+
53+
As the Voyager authors noted:
54+
55+
<Tip>*"We opt to use code as the action space instead of low-level motor commands because programs can naturally represent temporally extended and compositional actions, which are essential for many long-horizon tasks in Minecraft."*</Tip>
56+
57+
## Code is an Ideal Action Space
58+
59+
The implications of code as an action space extend far beyond gaming. Code provides a uniquely powerful interface between AI and real-world systems. When an agent writes code, it gains several critical advantages over traditional atomic tools.
60+
61+
### Code is Composable
62+
Code is the ultimate composable medium. Agents can build their own tools by combining simpler operations, wrapping any function as a building block for more complex behaviors. This aligns well with what is perhaps LLMs' premier capability: understanding and interpolating between examples to create new solutions.
63+
64+
### Code Constrains the Action Space
65+
APIs can enforce guardrails that keep agents on track. By designing interfaces that make invalid operations impossible to express, we can prevent entire classes of errors before they happen. The type system becomes a powerful tool for shaping agent behavior.
66+
67+
### Code Provides Objective Feedback
68+
Code execution gives immediate, unambiguous feedback. When something goes wrong, you get stack traces and error messages—not just a confidence score. This concrete error signal is invaluable for agents learning to navigate complex systems.
69+
70+
### Code is a Natural Medium for Collaboration
71+
Programs are a shared language between humans and agents. Code explicitly encodes reasoning in a reviewable format, making agent actions transparent and debuggable. There's no magic—just deterministic execution that can be understood, modified, and improved by both humans and AI.
72+
73+
## For Software Engineering
74+
75+
This brings us to software engineering, where we see a massive gap between AI's theoretical capabilities and practical achievements. Many code modification tasks are fundamentally programmatic—dependency analysis, refactors, control flow analysis—yet we lack the tools to express them properly.
76+
77+
Consider how a developer thinks about refactoring: it's rarely about direct text manipulation. Instead, we think in terms of high-level operations: "move this function," "rename this variable everywhere," "split this module." These operations can be encoded into a powerful Python API:
78+
79+
```python
80+
# simple access to high-level code constructs
81+
for component in codebase.jsx_components:
82+
# access detailed code structure and relations
83+
if len(component.usages) == 0:
84+
# powerful edit APIs that handle edge cases
85+
component.rename(component.name + 'Page')
86+
```
87+
88+
This isn't just another code manipulation library—it's a scriptable language server that builds on proven foundations like LSP and codemods, but designed specifically for programmatic analysis and refactoring.
89+
90+
## What does this look like?
91+
92+
At Codegen, we've built exactly this system. Our approach centers on four key principles:
93+
94+
The foundation must be Python, enabling easy composition with existing tools and workflows. Operations must be in-memory for performance, handling large-scale changes efficiently. The system must be open source, allowing developers and AI researchers to extend and enhance it. And perhaps most importantly, it must be thoroughly documented—not just for humans, but for the next generation of AI agents that will build upon it.
95+
96+
## What does this enable?
97+
98+
We've already used this approach to merge hundreds of thousands of lines of code in enterprise codebases. Our tools have automated complex tasks like feature flag deletion, test suite reorganization, import cycle elimination, and dead code removal. But more importantly, we've proven that code-as-action-space isn't just theoretical—it's a practical approach to scaling software engineering.
99+
100+
This is just the beginning. With Codegen, we're providing the foundation for the next generation of code manipulation tools—built for both human developers and AI agents. We believe this approach will fundamentally change how we think about and implement large-scale code changes, making previously impossible tasks not just possible, but routine.

docs/blog/codemod-frameworks.mdx

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
---
2+
title: "Comparing Codemod Frameworks"
3+
sidebarTitle: "Codemod Frameworks"
4+
icon: "code-compare"
5+
iconType: "solid"
6+
---
7+
8+
Code transformation tools have evolved significantly over the years, each offering unique approaches to programmatic code manipulation. Let's explore the strengths and limitations of major frameworks in this space.
9+
10+
## Python's AST Module
11+
12+
Python's built-in Abstract Syntax Tree (AST) module provides the foundation for Python code manipulation.
13+
14+
### Strengths
15+
16+
- Native Python implementation
17+
- No external dependencies
18+
- Full access to Python's syntax tree
19+
- Great for Python-specific transformations
20+
21+
### Limitations
22+
23+
- Python-only
24+
- Low-level API requiring deep AST knowledge
25+
- Manual handling of formatting and comments
26+
- No cross-file awareness
27+
28+
```python
29+
import ast
30+
31+
class NameTransformer(ast.NodeTransformer):
32+
def visit_Name(self, node):
33+
if node.id == 'old_name':
34+
return ast.Name(id='new_name', ctx=node.ctx)
35+
return node
36+
```
37+
38+
## LibCST
39+
40+
Meta's Concrete Syntax Tree library offers a more modern approach to Python code modification.
41+
42+
### Strengths
43+
44+
- Preserves formatting and comments
45+
- Type annotations support
46+
- Visitor pattern API
47+
- Excellent documentation
48+
- Supports codemods at scale
49+
50+
### Limitations
51+
52+
- Python-only
53+
- Steeper learning curve
54+
- Slower than raw AST manipulation
55+
- Memory-intensive for large codebases
56+
57+
```python
58+
import libcst as cst
59+
60+
class NameTransformer(cst.CSTTransformer):
61+
def leave_Name(self, original_node, updated_node):
62+
if original_node.value == "old_name":
63+
return updated_node.with_changes(value="new_name")
64+
return updated_node
65+
```
66+
67+
## jscodeshift
68+
69+
The pioneer of JavaScript codemods, jscodeshift remains a staple in the JS ecosystem.
70+
71+
### Strengths
72+
73+
- Robust JavaScript/TypeScript support
74+
- Rich ecosystem of transforms
75+
- Familiar jQuery-like API
76+
- Battle-tested at scale
77+
78+
### Limitations
79+
80+
- JavaScript/TypeScript only
81+
- Limited type information
82+
- Can be verbose for simple transforms
83+
- Documentation could be better
84+
85+
```javascript
86+
export default function transformer(file, api) {
87+
const j = api.jscodeshift;
88+
return j(file.source)
89+
.find(j.Identifier)
90+
.filter((path) => path.node.name === "old_name")
91+
.replaceWith((path) => j.identifier("new_name"))
92+
.toSource();
93+
}
94+
```
95+
96+
## ts-morph
97+
98+
A TypeScript-first transformation tool with rich type system integration.
99+
100+
### Strengths
101+
102+
- First-class TypeScript support
103+
- Excellent type information access
104+
- High-level, intuitive API
105+
- Great documentation
106+
- Project-wide analysis capabilities
107+
108+
### Limitations
109+
110+
- TypeScript/JavaScript only
111+
- Higher memory usage
112+
- Can be slower for large projects
113+
- More complex setup than alternatives
114+
115+
```typescript
116+
import { Project } from "ts-morph";
117+
118+
const project = new Project();
119+
project.addSourceFileAtPath("src/**/*.ts");
120+
project.getSourceFiles().forEach((sourceFile) => {
121+
sourceFile
122+
.getDescendantsOfKind(SyntaxKind.Identifier)
123+
.filter((node) => node.getText() === "old_name")
124+
.forEach((node) => node.replaceWithText("new_name"));
125+
});
126+
```
127+
128+
## ast-grep
129+
130+
A modern, language-agnostic code searching and rewriting tool.
131+
132+
### Strengths
133+
134+
- Multi-language support
135+
- Fast pattern matching
136+
- Simple YAML-based rules
137+
- Great for quick transformations
138+
- Excellent performance
139+
140+
### Limitations
141+
142+
- Limited complex transformation support
143+
- Newer, less battle-tested
144+
- Smaller ecosystem
145+
- Less granular control
146+
147+
```yaml
148+
rules:
149+
- pattern: old_name
150+
replace: new_name
151+
```
152+
153+
## tree-sitter
154+
155+
The foundation many modern tools build upon, offering lightning-fast parsing and analysis.
156+
157+
### Strengths
158+
159+
- Incredible performance
160+
- Multi-language support
161+
- Incremental parsing
162+
- Language-agnostic design
163+
- Growing ecosystem
164+
165+
### Limitations
166+
167+
- Lower-level API
168+
- Requires language-specific grammars
169+
- Manual handling of transformations
170+
- Steeper learning curve
171+
172+
```javascript
173+
const Parser = require("tree-sitter");
174+
const JavaScript = require("tree-sitter-javascript");
175+
176+
const parser = new Parser();
177+
parser.setLanguage(JavaScript);
178+
const tree = parser.parse('console.log("Hello")');
179+
```
180+
181+
## Choosing the Right Tool
182+
183+
The choice of codemod framework depends heavily on your specific needs:
184+
185+
- **Single Language Focus**: If you're working exclusively with one language, use its specialized tools:
186+
187+
- Python → LibCST
188+
- TypeScript → ts-morph
189+
- JavaScript → jscodeshift
190+
191+
- **Multi-Language Projects**: Consider:
192+
193+
- ast-grep for simple transformations
194+
- tree-sitter for building custom tools
195+
- A combination of specialized tools
196+
197+
- **Scale Considerations**:
198+
- Small projects → Any tool works
199+
- Medium scale → Language-specific tools
200+
- Large scale → Need proper tooling support (LibCST, ts-morph)
201+
202+
## The Future of Codemods
203+
204+
As codebases grow and AI becomes more prevalent, we're seeing a shift toward:
205+
206+
1. **More Intelligent Tools**
207+
208+
- Better type awareness
209+
- Improved cross-file analysis
210+
- AI-assisted transformations
211+
212+
2. **Universal Approaches**
213+
214+
- Language-agnostic frameworks
215+
- Unified transformation APIs
216+
- Better interoperability
217+
218+
3. **Enhanced Developer Experience**
219+
- Simpler APIs
220+
- Better debugging tools
221+
- Richer ecosystems
222+
223+
The ideal codemod framework of the future will likely combine the best aspects of current tools: the type awareness of ts-morph, the simplicity of ast-grep, the performance of tree-sitter, and the reliability of LibCST.

0 commit comments

Comments
 (0)