// Package kilo is the kilo harness adapter. package kilo import ( "encoding/json" "fmt" "maps" "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 = "kilo" // Adapter implements adapter.Adapter for the kilo 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 kilo harness-sync manages. func (a *Adapter) Capabilities() adapter.HarnessCapabilities { return adapter.HarnessCapabilities{ ManagesProviders: true, ManagesModels: true, ManagesMCP: true, ManagesSkills: true, ManagesAgents: true, ManagesRules: true, ManagesInstructions: true, HasBuiltInSub: false, } } // Detect returns true when ~/.config/kilo exists. func (a *Adapter) Detect() bool { _, err := os.Stat(filepath.Join(a.Home, ".config", "kilo")) return err == nil } // Render produces the FileSet that applies the canonical bundle to kilo. // kilo.json is MERGED to preserve user-managed keys ($schema, small_model, // instructions, permission, compaction, watcher, formatter, skills). // Key "model" is a plain string; MCP uses "mcp" with local/remote type entries. // 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") // Kilo CLI reads global markdown subagents from ~/.config/kilo/agents/.md // (PLURAL — the singular "agent/" used by older OpenCode-fork builds is a // silent no-op on current Kilo). fs.Add(adapter.File{ Dest: filepath.Join(base, "agents"), Kind: adapter.SymlinkDir, SymlinkTarget: filepath.Join(b.Root, "agents"), }) // kilo reads skills from ~/.kilo/skills//SKILL.md fs.Add(adapter.File{ Dest: filepath.Join(a.Home, ".kilo", "skills"), Kind: adapter.SymlinkDir, SymlinkTarget: filepath.Join(b.Root, "skills"), }) // kilo has no global rules directory; ~/.config/kilo/AGENTS.md is the // always-on global instructions file, so fold rules into it. fs.Add(adapter.File{ Dest: filepath.Join(base, "AGENTS.md"), Kind: adapter.RenderedFile, Content: []byte(b.InstructionTextWithRules(name)), }) cfgPath := filepath.Join(base, "kilo.json") 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 { overlay["provider"] = providers } if modelStr != "" { overlay["model"] = modelStr overlay["small_model"] = modelStr } if mcp := common.BuildMCPMapStyled(&b.MCP, common.MCPOpencodeStyle); len(mcp) > 0 { overlay["mcp"] = mcp } merged, err := common.MergeJSONKeys(existing, overlay) if err != nil { return nil, err } fs.Add(adapter.File{ Dest: cfgPath, Kind: adapter.RenderedFile, Content: merged, }) // Also merge into opencode.jsonc when present — kilo reads it as its // primary config. Delete the enabled_providers filter so our provider is // visible alongside user-defined providers. ocPath := filepath.Join(base, "opencode.jsonc") if ocContent, mergeErr := mergeOpenCodeJSONC(ocPath, overlay, b.Profile.Gateway.URL); mergeErr != nil { return nil, mergeErr } else if ocContent != nil { fs.Add(adapter.File{ Dest: ocPath, Kind: adapter.RenderedFile, Content: ocContent, NoMerge: true, }) } return fs, nil } // mergeOpenCodeJSONC deep-merges our overlay into the existing opencode.jsonc // file, deleting the enabled_providers filter and deduplicating gateway providers. // Returns nil content (no error) when the file does not exist. func mergeOpenCodeJSONC(path string, overlay map[string]any, gatewayURL string) ([]byte, error) { ocRaw, err := os.ReadFile(path) if err != nil { return nil, nil //nolint:nilerr // file absent is not an error } var ocBase map[string]any clean := common.StripJSONComments(string(ocRaw)) _ = json.Unmarshal([]byte(clean), &ocBase) if ocBase == nil { ocBase = map[string]any{} } ocOverlay := map[string]any{"enabled_providers": nil} if newProv, ok := overlay["provider"].(map[string]any); ok && len(newProv) > 0 { existingProv, _ := ocBase["provider"].(map[string]any) if existingProv == nil { existingProv = map[string]any{} } maps.Copy(existingProv, newProv) ocOverlay["provider"] = common.AbsorbDuplicateProviders(existingProv, common.GatewayProviderKey(gatewayURL), gatewayURL) } for _, k := range []string{"model", "small_model", "mcp"} { if v, ok := overlay[k]; ok { ocOverlay[k] = v } } return common.MergeJSONKeys([]byte(clean), ocOverlay) } // Import reads kilo config from home and returns a canonical ImportResult. func (a *Adapter) Import(home string) (*adapter.ImportResult, error) { return importFrom(home) }