Files
harness-sync/internal/adapters/cagent/cagent.go
T
lukaszraczyloandGitHub e01f3419ea fix(apply): propagate read errors, auto-open repo, dedupe adapter base (#6)
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.
2026-06-01 13:11:07 +01:00

124 lines
3.1 KiB
Go

// Package cagent is the cagent harness adapter.
//
// cagent is per-run (no persistent agent state). v1 renders a starter
// ~/.config/cagent/default.yaml with a single default agent pointing at the
// gateway. Import always returns an empty result.
package cagent
import (
"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"
"gopkg.in/yaml.v3"
)
const name = "cagent"
// Adapter implements adapter.Adapter for the cagent 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 cagent harness-sync manages.
func (a *Adapter) Capabilities() adapter.HarnessCapabilities {
return adapter.HarnessCapabilities{
ManagesProviders: true,
ManagesModels: true,
ManagesMCP: true,
ManagesSkills: false,
ManagesInstructions: true,
HasBuiltInSub: false,
}
}
// Detect returns true when ~/.config/cagent/ exists.
func (a *Adapter) Detect() bool {
_, err := os.Stat(filepath.Join(a.Home, ".config", "cagent"))
return err == nil
}
// Render produces a starter default.yaml for cagent.
func (a *Adapter) Render(b *canonical.Bundle) (*adapter.FileSet, error) {
fs := adapter.NewFileSet()
cfgPath := filepath.Join(a.Home, ".config", "cagent", "default.yaml")
instructions := b.Instructions.Global
if override, ok := b.Instructions.PerHarness[name]; ok && override != "" {
instructions = override
}
mcps := map[string]any{}
for _, s := range b.MCP.Servers {
e := map[string]any{}
if s.Command != "" {
e["command"] = s.Command
}
args := s.Args
if args == nil {
args = []string{}
}
e["args"] = args
mcps[s.Name] = e
}
provKey := common.GatewayProviderKey(b.Profile.Gateway.URL)
// inline model shorthand: "providerID/modelID"
modelRef := common.KiloModelString(&b.Profile)
cfg := map[string]any{
"version": 8,
"metadata": map[string]any{
"name": "harness-sync",
},
"agents": map[string]any{
"default": map[string]any{
"model": modelRef,
"instruction": instructions,
},
},
"providers": common.ProvidersAsCagentMap(&b.Profile),
"models": map[string]any{
provKey: map[string]any{
"provider": provKey,
"model": b.Profile.Gateway.DefaultModel,
},
},
}
if len(mcps) > 0 {
cfg["mcps"] = mcps
}
body, err := yaml.Marshal(cfg)
if err != nil {
return nil, err
}
fs.Add(adapter.File{
Dest: cfgPath,
Kind: adapter.RenderedFile,
Content: body,
})
return fs, nil
}
// Import returns an empty result — cagent has no persistent state to import.
func (a *Adapter) Import(home string) (*adapter.ImportResult, error) {
return &adapter.ImportResult{}, nil
}