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.
This commit is contained in:
2026-06-01 13:11:07 +01:00
committed by GitHub
parent 5704ede5c7
commit e01f3419ea
15 changed files with 482 additions and 118 deletions
+13 -16
View File
@@ -3,6 +3,7 @@ package kilo
import (
"encoding/json"
"fmt"
"maps"
"os"
"path/filepath"
@@ -16,22 +17,15 @@ const name = "kilo"
// Adapter implements adapter.Adapter for the kilo harness.
type Adapter struct {
home string
*adapter.Base
}
// Option configures an Adapter.
type Option func(*Adapter)
// WithHome overrides the home directory (defaults to os.UserHomeDir).
func WithHome(h string) Option { return func(a *Adapter) { a.home = h } }
// 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 ...Option) *Adapter {
a := &Adapter{home: common.DefaultHome()}
for _, o := range opts {
o(a)
}
return a
func New(opts ...adapter.BaseOption) *Adapter {
return &Adapter{Base: adapter.NewBase(opts...)}
}
// Name returns the harness identifier.
@@ -51,7 +45,7 @@ func (a *Adapter) Capabilities() adapter.HarnessCapabilities {
// Detect returns true when ~/.config/kilo exists.
func (a *Adapter) Detect() bool {
_, err := os.Stat(filepath.Join(a.home, ".config", "kilo"))
_, err := os.Stat(filepath.Join(a.Home, ".config", "kilo"))
return err == nil
}
@@ -62,7 +56,7 @@ func (a *Adapter) Detect() bool {
// No "providers" key — kilo has no such concept.
func (a *Adapter) Render(b *canonical.Bundle) (*adapter.FileSet, error) {
fs := adapter.NewFileSet()
base := filepath.Join(a.home, ".config", "kilo")
base := filepath.Join(a.Home, ".config", "kilo")
fs.Add(adapter.File{
Dest: filepath.Join(base, "agent"),
@@ -71,13 +65,16 @@ func (a *Adapter) Render(b *canonical.Bundle) (*adapter.FileSet, error) {
})
// kilo reads skills from ~/.kilo/skills/<name>/SKILL.md
fs.Add(adapter.File{
Dest: filepath.Join(a.home, ".kilo", "skills"),
Dest: filepath.Join(a.Home, ".kilo", "skills"),
Kind: adapter.SymlinkDir,
SymlinkTarget: filepath.Join(b.Root, "skills"),
})
cfgPath := filepath.Join(base, "kilo.json")
existing, _ := os.ReadFile(cfgPath)
existing, err := common.ReadExistingFile(cfgPath)
if err != nil {
return nil, fmt.Errorf("read %s: %w", cfgPath, err)
}
modelStr := common.KiloModelString(&b.Profile)
overlay := map[string]any{}
if providers := common.ProvidersAsMap(&b.Profile); len(providers) > 0 {