mirror of
https://github.com/lukaszraczylo/harness-sync.git
synced 2026-10-12 00:45:22 +00:00
Three robustness fixes for the apply pipeline:
1. Silent read errors on state/target files treated 'file absent' as
'file empty' and fast-forward-overwrote user edits. Add
common.ReadExistingFile that returns (nil,nil) only on
os.IsNotExist; other I/O errors propagate. Use it from apply.Run
and from every adapter's Render (claudecode, crush, kilo,
opencode, goose, zed).
2. apply.Run required callers to pre-construct *gitx.Repo; passing
nil silently skipped the commit. Run now auto-opens a repo from
Bundle.Root, gracefully no-ops when the root is not a git repo,
and uses a new gitx.HasStagedChanges() check to skip the no-op
'nothing to commit' that triggered the original silent-skip.
3. Each of 7 adapter packages re-implemented the same 25-line
Option/WithHome/New constructor. Hoist into adapter.Base +
adapter.BaseOption + NewBase. Each adapter embeds *Base and
reduces to a 9-line constructor. Public New(WithHome(h)) API
preserved; a.home becomes a.Home.
Tests:
- apply: 5 new (propagates-read-error, auto-opens-repo,
no-repo-skips-commit, skips-no-op-commit, commit-message-counts)
- gitx: 2 new (HasStagedChanges empty/after-add)
- adapter/common: 3 new (ReadExistingFile missing/content/perm)
- adapter: 4 new (NewBase default/WithHome/order/typed)
- existing adapter tests unchanged — verify a.Home still honoured.
131 lines
4.1 KiB
Go
131 lines
4.1 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,
|
|
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"),
|
|
})
|
|
|
|
instructions := b.Instructions.Global
|
|
if override, ok := b.Instructions.PerHarness[name]; ok && override != "" {
|
|
instructions = override
|
|
}
|
|
fs.Add(adapter.File{
|
|
Dest: filepath.Join(base, "CLAUDE.md"),
|
|
Kind: adapter.RenderedFile,
|
|
Content: []byte(instructions),
|
|
})
|
|
|
|
// 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)
|
|
}
|