Upstream Merge Automation
Scripts for automating the merge of upstream opencode changes into Kilo.
Quick Start
# Install dependencies (from script/upstream directory)
cd script/upstream
bun install
# List available upstream versions
bun run list-versions.ts
# Analyze changes for a specific version (without merging)
bun run analyze.ts --version v1.1.49
# Run the full merge process
bun run merge.ts --version v1.1.49
# Dry-run to preview what would happen
bun run merge.ts --version v1.1.49 --dry-run
Scripts
Main Scripts
| Script | Description |
|---|---|
merge.ts |
Main orchestration script for upstream merges |
list-versions.ts |
List available upstream versions |
analyze.ts |
Analyze changes without merging |
Transform Scripts
| Script | Description |
|---|---|
transforms/package-names.ts |
Transform opencode package names to kilo |
transforms/preserve-versions.ts |
Preserve Kilo's package versions |
transforms/keep-ours.ts |
Keep Kilo's version of specific files |
Codemods (AST-based)
| Script | Description |
|---|---|
codemods/transform-imports.ts |
Transform import statements using ts-morph |
codemods/transform-strings.ts |
Transform string literals |
Merge Process
The merge automation follows this process:
-
Validate environment
- Check for upstream remote
- Ensure working directory is clean
-
Fetch upstream and determine target version
-
Generate conflict report analyzing which files will conflict
-
Create branches
backup/<branch>-<timestamp>- Backup of current state<author>/kilo-opencode-<version>- Merge target branch<author>/opencode-<version>- Transformed upstream branch
-
Apply transformations to upstream branch:
- Transform package names (opencode-ai -> @kilocode/cli)
- Preserve Kilo's versions
- Reset Kilo-specific files
-
Merge transformed upstream into Kilo branch
-
Auto-resolve known conflicts (markdown, Kilo-specific files)
-
Push and generate final report
Configuration
Configuration is defined in utils/config.ts:
{
// Package name mappings
packageMappings: [
{ from: "opencode-ai", to: "@kilocode/cli" },
{ from: "@opencode-ai/cli", to: "@kilocode/cli" },
// ...
],
// Files to always keep Kilo's version
keepOurs: [
"README.md",
"CONTRIBUTING.md",
"AGENTS.md",
// ...
],
// Kilo-specific directories
kiloDirectories: [
"packages/opencode/src/kilocode",
"packages/kilo-gateway",
// ...
],
}
CLI Options
merge.ts
Options:
--version <version> Target upstream version (e.g., v1.1.49)
--commit <hash> Target upstream commit hash
--dry-run Preview changes without applying them
--no-push Don't push branches to remote
--report-only Only generate conflict report
--verbose Enable verbose logging
--author <name> Author name for branch prefix
analyze.ts
Options:
--version <version> Target upstream version
--commit <hash> Target commit hash
--output <file> Output file for report
Manual Conflict Resolution
After running the merge script, you may have remaining conflicts. To resolve:
- Open each conflicted file
- Look for
kilocode_changemarkers to identify Kilo-specific code - Resolve conflicts, keeping Kilo-specific changes
- Stage and commit:
git add -A git commit -m "resolve merge conflicts"
Rollback
If something goes wrong:
# Find your backup branch
git branch | grep backup
# Reset to backup
git checkout dev
git reset --hard backup/dev-<timestamp>
Adding New Transformations
String-based (simple)
Edit transforms/package-names.ts and add patterns to PACKAGE_PATTERNS.
AST-based (robust)
- Create a new file in
codemods/ - Use ts-morph for TypeScript AST manipulation
- Export transform functions
- Add to the merge orchestration if needed
Troubleshooting
"No upstream remote found"
git remote add upstream git@github.com:anomalyco/opencode.git
"Working directory has uncommitted changes"
git stash
# or
git commit -am "WIP"
Merge conflicts after auto-resolution
Some files require manual review. Check the generated report for guidance.