mirror of
https://github.com/lukaszraczylo/harness-sync.git
synced 2026-10-18 01:17:50 +00:00
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).
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
package crush
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -36,7 +37,8 @@ func (a *Adapter) Capabilities() adapter.HarnessCapabilities {
|
||||
ManagesModels: true,
|
||||
ManagesMCP: true,
|
||||
ManagesSkills: true,
|
||||
ManagesInstructions: false,
|
||||
ManagesRules: true,
|
||||
ManagesInstructions: true,
|
||||
HasBuiltInSub: false,
|
||||
}
|
||||
}
|
||||
@@ -60,12 +62,25 @@ func (a *Adapter) Render(b *canonical.Bundle) (*adapter.FileSet, error) {
|
||||
SymlinkTarget: filepath.Join(b.Root, "skills"),
|
||||
})
|
||||
|
||||
// crush has no global instructions file and no rules directory: it loads
|
||||
// context from the files listed in options.context_paths (defaults are always
|
||||
// prepended; absolute paths load verbatim; the global config applies to every
|
||||
// project). Write a global AGENTS.md with rules folded in, and register its
|
||||
// absolute path so crush loads it in every session.
|
||||
agentsMD := filepath.Join(base, "AGENTS.md")
|
||||
fs.Add(adapter.File{
|
||||
Dest: agentsMD,
|
||||
Kind: adapter.RenderedFile,
|
||||
Content: []byte(b.InstructionTextWithRules(name)),
|
||||
})
|
||||
|
||||
cfgPath := filepath.Join(base, "crush.json")
|
||||
existing, err := common.ReadExistingFile(cfgPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read %s: %w", cfgPath, err)
|
||||
}
|
||||
overlay := map[string]any{}
|
||||
overlay["options"] = crushOptionsWithContextPath(existing, agentsMD)
|
||||
if providers := common.ProvidersAsCrushMap(&b.Profile); len(providers) > 0 {
|
||||
overlay["providers"] = providers
|
||||
}
|
||||
@@ -89,6 +104,34 @@ func (a *Adapter) Render(b *canonical.Bundle) (*adapter.FileSet, error) {
|
||||
return fs, nil
|
||||
}
|
||||
|
||||
// crushOptionsWithContextPath returns the crush "options" object with contextFile
|
||||
// added to options.context_paths, preserving every other existing option key
|
||||
// (lsp, permissions, …). crush always prepends its built-in default context
|
||||
// paths at load time, so only the extra path is set here. Idempotent.
|
||||
func crushOptionsWithContextPath(existing []byte, contextFile string) map[string]any {
|
||||
opts := map[string]any{}
|
||||
if len(existing) > 0 {
|
||||
var raw map[string]any
|
||||
clean := common.StripJSONComments(string(existing))
|
||||
if json.Unmarshal([]byte(clean), &raw) == nil {
|
||||
if o, ok := raw["options"].(map[string]any); ok {
|
||||
opts = o
|
||||
}
|
||||
}
|
||||
}
|
||||
var paths []any
|
||||
if existingPaths, ok := opts["context_paths"].([]any); ok {
|
||||
paths = existingPaths
|
||||
}
|
||||
for _, p := range paths {
|
||||
if s, ok := p.(string); ok && s == contextFile {
|
||||
return opts // already registered
|
||||
}
|
||||
}
|
||||
opts["context_paths"] = append(paths, contextFile)
|
||||
return opts
|
||||
}
|
||||
|
||||
// Import reads crush config from home and returns a canonical ImportResult.
|
||||
func (a *Adapter) Import(home string) (*adapter.ImportResult, error) {
|
||||
return importFrom(home)
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
package crush
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/lukaszraczylo/harness-sync/internal/adapter"
|
||||
"github.com/lukaszraczylo/harness-sync/internal/canonical"
|
||||
)
|
||||
|
||||
func renderCrush(t *testing.T, home string, b *canonical.Bundle) map[string]adapter.File {
|
||||
t.Helper()
|
||||
fs, err := New(WithHome(home)).Render(b)
|
||||
require.NoError(t, err)
|
||||
seen := map[string]adapter.File{}
|
||||
fs.ForEach(func(f adapter.File) { seen[f.Dest] = f })
|
||||
return seen
|
||||
}
|
||||
|
||||
func TestCrushWritesAGENTSAndRegistersContextPath(t *testing.T) {
|
||||
home := t.TempDir()
|
||||
b := &canonical.Bundle{
|
||||
Root: "/canon",
|
||||
Instructions: canonical.Instructions{Global: "# global"},
|
||||
Rules: []canonical.Rule{{Name: "sec", Body: "# Security\n\nno secrets in code"}},
|
||||
}
|
||||
seen := renderCrush(t, home, b)
|
||||
|
||||
base := filepath.Join(home, ".config", "crush")
|
||||
agentsMD := filepath.Join(base, "AGENTS.md")
|
||||
assert.Equal(t, adapter.RenderedFile, seen[agentsMD].Kind)
|
||||
body := string(seen[agentsMD].Content)
|
||||
assert.Contains(t, body, "# global")
|
||||
assert.Contains(t, body, "no secrets in code")
|
||||
|
||||
var cfg map[string]any
|
||||
require.NoError(t, json.Unmarshal(seen[filepath.Join(base, "crush.json")].Content, &cfg))
|
||||
opts := cfg["options"].(map[string]any)
|
||||
paths := opts["context_paths"].([]any)
|
||||
assert.Contains(t, paths, agentsMD, "absolute AGENTS.md path must be registered for global load")
|
||||
}
|
||||
|
||||
func TestCrushPreservesExistingOptionsAndIsIdempotent(t *testing.T) {
|
||||
home := t.TempDir()
|
||||
base := filepath.Join(home, ".config", "crush")
|
||||
require.NoError(t, os.MkdirAll(base, 0o750))
|
||||
agentsMD := filepath.Join(base, "AGENTS.md")
|
||||
|
||||
// Existing crush.json with other options keys and a pre-existing context path
|
||||
// (including our own — second apply must not duplicate it).
|
||||
existing := map[string]any{
|
||||
"options": map[string]any{
|
||||
"context_paths": []any{"PROJECT.md", agentsMD},
|
||||
"lsp": map[string]any{"go": true},
|
||||
},
|
||||
}
|
||||
body, _ := json.Marshal(existing)
|
||||
require.NoError(t, os.WriteFile(filepath.Join(base, "crush.json"), body, 0o600))
|
||||
|
||||
seen := renderCrush(t, home, &canonical.Bundle{Root: "/canon"})
|
||||
|
||||
var cfg map[string]any
|
||||
require.NoError(t, json.Unmarshal(seen[filepath.Join(base, "crush.json")].Content, &cfg))
|
||||
opts := cfg["options"].(map[string]any)
|
||||
|
||||
// Other option keys survive.
|
||||
assert.Contains(t, opts, "lsp")
|
||||
// Pre-existing user path survives; ours is present exactly once (idempotent).
|
||||
paths := opts["context_paths"].([]any)
|
||||
count := 0
|
||||
for _, p := range paths {
|
||||
if p == agentsMD {
|
||||
count++
|
||||
}
|
||||
}
|
||||
assert.Equal(t, 1, count, "our context path must not be duplicated on re-apply")
|
||||
assert.Contains(t, paths, "PROJECT.md")
|
||||
}
|
||||
Reference in New Issue
Block a user