---
name: update-opencode
description: Check for and apply OpenCode version updates in Nix-based dotfiles. Use when asked to update OpenCode, check OpenCode version, or upgrade OpenCode.
---

# Update OpenCode Skill

This skill automates checking for and applying OpenCode version updates in a Nix-based dotfiles setup.

## When to Use

Use this skill when the user requests:
- Check OpenCode version or check for updates
- Update/upgrade OpenCode to latest version
- Install specific OpenCode version
- "Is there a newer version of OpenCode?"

## Process

### 1. Check Current vs Latest Version

Run the version check script:
```bash
cd ~/.claude/skills/update-opencode/scripts
./check-version.sh
```

This outputs:
```
current=X.Y.Z
latest=X.Y.Z
update_available=yes|no
```

**Report findings to user** with version numbers and update availability.

### 2. Apply Update (if user confirms)

If `update_available=yes`, **ask user for confirmation** before proceeding:
- Explain that this will modify Nix configuration and rebuild the system
- Mention rebuild may take a few minutes
- Ask: "Proceed with update to version X.Y.Z?"

If user confirms, execute the following steps:

**Step 2a: Fetch SHA256 hash**
```bash
./fetch-sha256.sh <latest-version>
```

This downloads the release and computes the SRI hash (output: `sha256-...`).

**Step 2b: Update Nix package file**
```bash
./update-nix-file.sh <latest-version> <sri-hash>
```

For safety, can use `--dry-run` first to preview changes:
```bash
./update-nix-file.sh <latest-version> <sri-hash> --dry-run
```

**Step 2c: Trigger system rebuild**
```bash
cd ~/proj/dotfiles
sudo nixos-rebuild switch --flake .#delpad
```

This will rebuild the NixOS configuration with the new OpenCode version.

**Step 2d: Verify installation**
```bash
cd ~/.claude/skills/update-opencode/scripts
./verify-update.sh <latest-version>
```

This confirms OpenCode reports the expected version.

### 3. Install Specific Version

For version pinning or downgrades:

```bash
# Fetch hash for specific version
./fetch-sha256.sh 1.0.44

# Update and rebuild as above
./update-nix-file.sh 1.0.44 <sri-hash>
cd ~/proj/dotfiles && sudo nixos-rebuild switch --flake .#delpad
./verify-update.sh 1.0.44
```

## Requirements

**Tools:**
- `jq` - JSON parsing for GitHub API
- `curl` - HTTP requests for GitHub API
- `nix-prefetch-url` - Download and hash verification
- `sed` - File modification
- `grep` - Pattern matching

**Permissions:**
- Read access to `~/proj/dotfiles/pkgs/opencode/default.nix`
- Write access to dotfiles repository
- `sudo` for `nixos-rebuild switch`

**Network:**
- GitHub API access: `https://api.github.com/repos/sst/opencode/releases`
- GitHub releases: `https://github.com/sst/opencode/releases/download/`

## Helper Scripts

**check-version.sh**
- Reads current version from Nix file
- Queries GitHub API for latest release
- Compares versions
- Output: `key=value` pairs

**fetch-sha256.sh <version>**
- Downloads OpenCode release for specified version
- Computes SRI hash using `nix-prefetch-url`
- Converts to SRI format (sha256-...)
- Output: SRI hash string

**update-nix-file.sh <version> <sha256> [--dry-run]**
- Updates version and sha256 fields in Nix file
- Validates patterns before modifying
- Supports dry-run mode
- Verifies changes after update

**verify-update.sh <version>**
- Runs `opencode --version`
- Compares output to expected version
- Exit code 0 on success, 1 on mismatch

## Error Handling

**Network failures:** Report clear error, suggest manual GitHub check

**Missing Nix file:** Report path error, verify dotfiles location

**Invalid version:** Report format error (expected X.Y.Z)

**SHA256 fetch failure:** Do not modify files, report download error

**Rebuild failure:** Report error with logs, suggest rollback or manual intervention

**Verification failure:** Report version mismatch, suggest re-running rebuild

## Guidelines

1. **Always ask confirmation** before triggering system rebuild
2. **Report progress** at each step (fetching hash, updating file, rebuilding)
3. **Handle errors gracefully** - explain what went wrong and suggest fixes
4. **Verify atomicity** - if any step fails, do not proceed to next step
5. **Check prerequisites** - ensure all required tools are installed before starting

## Examples

**Example 1: Check for updates**
```
User: "Check if there's a new OpenCode version"
Agent: *runs check-version.sh*
Agent: "Current version: 1.0.44, Latest: 1.0.51. Update available."
```

**Example 2: Apply update**
```
User: "Update OpenCode to latest"
Agent: *runs check-version.sh*
Agent: "Update available: 1.0.44 → 1.0.51. This will rebuild your system. Proceed?"
User: "Yes"
Agent: *runs fetch-sha256.sh, update-nix-file.sh, rebuild, verify-update.sh*
Agent: "✓ Updated to OpenCode 1.0.51"
```

**Example 3: Specific version**
```
User: "Install OpenCode 1.0.44"
Agent: *fetches hash, updates file, rebuilds, verifies*
Agent: "✓ Installed OpenCode 1.0.44"
```
