Files
harness-sync/internal/adapters/claudecode/claudecode.go
T
lukaszraczylo f30c0f65a6 feat: sync rules across all harnesses + agents to opencode/kilo
Add Rule as a first-class canonical kind (rules/<name>.md, loaded like
agents) and propagate it the way each harness actually loads it:

- claude-code: native ~/.claude/rules directory symlink (preserves rule
  path-scoping frontmatter); rules NOT folded into CLAUDE.md.
- opencode/kilo/zed/crush: fold rule bodies into the global AGENTS.md
  always-on instructions file (frontmatter stripped). crush has no global
  instructions file, so register the absolute AGENTS.md path in
  options.context_paths (defaults are re-prepended at load, so nothing is
  clobbered).
- goose: fold into ~/.config/goose/.goosehints.
- cagent: fold into the per-agent instruction.

Also:
- Wire markdown agents to opencode (~/.config/opencode/agents) and fix the
  kilo agents symlink: plural ~/.config/kilo/agents (singular "agent" is a
  silent no-op on current Kilo CLI).
- Add ManagesAgents/ManagesRules to HarnessCapabilities + ImportResult.Rules;
  claude-code/opencode import rules/agents round-trip.
- Centralize instruction selection in Bundle.InstructionText /
  InstructionTextWithRules (replaces per-adapter override duplication).
- init scaffolds + imports rules; README capability matrix gains
  Agents/Rules columns with native-vs-folded footnotes.

Grounded each harness's sink against official docs + config source this
session. Gate: gofmt, go vet, go test -race ./..., golangci-lint (0),
govulncheck (clean).
2026-06-16 21:32:12 +01:00

137 lines
4.4 KiB
Go

// Package claudecode is the claude-code harness adapter.
package claudecode
import (
"fmt"
"os"
"path/filepath"
"github.com/lukaszraczylo/harness-sync/internal/adapter"
"github.com/lukaszraczylo/harness-sync/internal/adapter/common"
"github.com/lukaszraczylo/harness-sync/internal/canonical"
)
const name = "claude-code"
// Adapter implements adapter.Adapter for the claude-code harness.
type Adapter struct {
*adapter.Base
}
// WithHome overrides the home directory used to resolve target paths.
func WithHome(h string) adapter.BaseOption { return func(b *adapter.Base) { b.Home = h } }
// New returns a new Adapter with the given options applied.
func New(opts ...adapter.BaseOption) *Adapter {
return &Adapter{Base: adapter.NewBase(opts...)}
}
// Name returns the harness identifier.
func (a *Adapter) Name() string { return name }
// Capabilities declares what claude-code harness-sync manages.
// Claude Code has its own Anthropic Max subscription — provider/model config is skipped.
func (a *Adapter) Capabilities() adapter.HarnessCapabilities {
return adapter.HarnessCapabilities{
ManagesProviders: false,
ManagesModels: false,
ManagesMCP: true,
ManagesSkills: true,
ManagesAgents: true,
ManagesRules: true,
ManagesInstructions: true,
HasBuiltInSub: true,
}
}
// Detect returns true when ~/.claude exists.
func (a *Adapter) Detect() bool {
_, err := os.Stat(filepath.Join(a.Home, ".claude"))
return err == nil
}
// Render produces the FileSet that applies the canonical bundle to claude-code.
// Claude Code manages its own subscription and settings.json — harness-sync
// only manages skills/agents symlinks, CLAUDE.md, and MCP servers.
func (a *Adapter) Render(b *canonical.Bundle) (*adapter.FileSet, error) {
fs := adapter.NewFileSet()
base := filepath.Join(a.Home, ".claude")
fs.Add(adapter.File{
Dest: filepath.Join(base, "skills"),
Kind: adapter.SymlinkDir,
SymlinkTarget: filepath.Join(b.Root, "skills"),
})
fs.Add(adapter.File{
Dest: filepath.Join(base, "agents"),
Kind: adapter.SymlinkDir,
SymlinkTarget: filepath.Join(b.Root, "agents"),
})
// Claude Code natively auto-loads ~/.claude/rules/*.md (memory feature), so
// rules are delivered as their own directory symlink — NOT folded into
// CLAUDE.md — preserving each rule's optional path-scoping frontmatter.
fs.Add(adapter.File{
Dest: filepath.Join(base, "rules"),
Kind: adapter.SymlinkDir,
SymlinkTarget: filepath.Join(b.Root, "rules"),
})
fs.Add(adapter.File{
Dest: filepath.Join(base, "CLAUDE.md"),
Kind: adapter.RenderedFile,
Content: []byte(b.InstructionText(name)),
})
// Claude Code reads MCP servers from two files on disk:
// * ~/.claude.json (live, written by `claude mcp add`)
// * ~/.claude/mcp_servers.json (older / fallback location)
// Write the dedicated file as the canonical destination, AND merge the
// same map into ~/.claude.json (preserving every other key in that
// large state file) so both stay in sync.
mcpMap := common.BuildMCPMapStyled(&b.MCP, common.MCPClaudeStyle)
overlay := map[string]any{"mcpServers": mcpMap}
dedicatedPath := filepath.Join(base, "mcp_servers.json")
dedicatedExisting, err := common.ReadExistingFile(dedicatedPath)
if err != nil {
return nil, fmt.Errorf("read %s: %w", dedicatedPath, err)
}
dedicatedMerged, err := common.MergeJSONKeys(dedicatedExisting, overlay)
if err != nil {
return nil, err
}
fs.Add(adapter.File{
Dest: dedicatedPath,
Kind: adapter.RenderedFile,
Content: dedicatedMerged,
})
livePath := filepath.Join(a.Home, ".claude.json")
if _, err := os.Stat(livePath); err == nil {
liveExisting, err := common.ReadExistingFile(livePath)
if err != nil {
return nil, fmt.Errorf("read %s: %w", livePath, err)
}
liveMerged, err := common.MergeJSONKeys(liveExisting, overlay)
if err != nil {
return nil, err
}
fs.Add(adapter.File{
Dest: livePath,
Kind: adapter.RenderedFile,
Content: liveMerged,
// NoMerge: .claude.json is a live state file (Claude Code rewrites it
// constantly). The JSON merge above already reconciles at the key level;
// a git 3-way merge on top causes perpetual conflicts.
NoMerge: true,
})
}
return fs, nil
}
// Import reads claude-code config from home and returns a canonical ImportResult.
func (a *Adapter) Import(home string) (*adapter.ImportResult, error) {
return importFrom(home)
}