Files
Rusty Shackleford 89ae7068d5 fix(config): honor OMP agent directory for default skills path (#652)
* fix(config): honor OMP skills directory overrides

OMP reads global skills below PI_CODING_AGENT_DIR, but unpinned built-in
targets still selected the default home. Resolve that runtime default
without changing the canonical template used to derive named accounts.

Keep account scan paths independent of the override and document explicit
path precedence in target guidance and its existing translations.

* fix(config): preserve inferred OMP paths across saves

Saving an unpinned OMP target serialized its resolved skills path, so a
later agent-directory override no longer selected the native location.
Track this fallback with the existing derived-path marker so config saves
preserve its inferred status while explicit paths remain pinned.

Cover save/reload after environment changes and filter edits, and fold
redundant account and project-alias checks into their existing cases.

* docs(config): clarify when OMP skills paths stay fixed

Initialization and target addition save their selected destination, while
an OMP entry with no configured skills path follows later environment
changes. Clarify that distinction in public and built-in target guidance
and all four existing translations.

Keep project alias coverage in its existing owner test and remove
redundant OMP matcher and generic save assertions.

* test(config): normalize explicit OMP account paths

The explicit-path fixture combines a native temporary root with a forward
slash suffix. Clean the actual path for comparison so mixed separators
match the native expected path on Windows, retaining the original path
in failure output.

* test(config): use portable OMP whitespace override fixture

Windows normalizes a directory component ending in a space during native
absolute-path resolution. Use a leading-space component to preserve the
check that override values are not trimmed without depending on trailing
space filename behavior.

* fix(config): preserve default OMP home in account shells

Keep plain OMP skills on the canonical home when its environment override
matches a declared account, so the unfiltered default target cannot bypass
account filters. Share account-aware defaults with discovery and dashboard
selection, and keep canonical account templates separate from runtime homes.

Require an OMP install before automatic detection because Pi shares the same
override. Trim surrounding whitespace, ignore relative overrides, and retain
explicit saved paths. Cover account filtering, aliases, symlinks, saves,
discovery, and existing environment-selected file homes with regressions.
2026-10-28 21:06:52 +08:00

204 lines
6.8 KiB
Go

package config
import (
"fmt"
"path/filepath"
"sort"
"strings"
)
// globalSpec finds a built-in target by name or alias.
func globalSpec(name string) (targetSpec, bool) {
specs, _ := loadTargetTemplates()
for _, spec := range specs {
if spec.Name != "" && targetSpecMatchesName(spec, name) {
return spec, true
}
}
return targetSpec{}, false
}
// AgentConfigDirAgents lists the built-in Agents a target can be another config directory of.
func AgentConfigDirAgents() []string {
specs, _ := loadTargetSpecs()
var names []string
for _, spec := range specs {
if spec.ConfigDir != "" {
names = append(names, spec.Name)
}
}
return names
}
// AgentConfigDir is the default config directory of a built-in Agent, or "" when a
// target cannot be another config directory of it.
func AgentConfigDir(agent string) string {
spec, ok := globalSpec(agent)
if !ok || spec.ConfigDir == "" {
return ""
}
return normalizeTargetPath(spec.ConfigDir)
}
// AgentConfigDirTargets names the targets that are another config directory of an Agent.
func (c *Config) AgentConfigDirTargets() []string {
var names []string
for name, target := range c.Targets {
if target.Agent != "" {
names = append(names, name)
}
}
sort.Strings(names)
return names
}
// AgentConfigDirTargetAgents maps each target that is another config directory of an Agent
// to that Agent.
func (c *Config) AgentConfigDirTargetAgents() map[string]string {
agents := map[string]string{}
for _, name := range c.AgentConfigDirTargets() {
agents[name] = c.Targets[name].Agent
}
return agents
}
// AgentConfigDirPaths returns where an Agent keeps skills and agents when its config
// directory is dir instead of the default one. agents is empty for an Agent without any.
func AgentConfigDirPaths(agent, dir string) (skills, agents string, err error) {
spec, ok := globalSpec(agent)
if !ok {
return "", "", fmt.Errorf("unknown Agent %q", agent)
}
if spec.ConfigDir == "" {
return "", "", fmt.Errorf("%s has no config directory of its own to move; supported: %s", spec.Name, strings.Join(AgentConfigDirAgents(), ", "))
}
dir = expandPath(dir)
if !filepath.IsAbs(dir) {
return "", "", fmt.Errorf("config_dir %q must be an absolute path or start with ~", dir)
}
base := normalizeTargetPath(spec.ConfigDir)
if filepath.Clean(dir) == filepath.Clean(base) {
return "", "", fmt.Errorf("config_dir %s is where %s already keeps its files; use the %s target", dir, spec.Name, spec.Name)
}
move := func(path string) string {
rel, err := filepath.Rel(base, normalizeTargetPath(path))
if path == "" || err != nil || strings.HasPrefix(rel, "..") {
return ""
}
return filepath.Join(dir, rel)
}
// An Agent may keep its primary skills directory outside its own config directory
// (Codex reads the shared ~/.agents/skills), while also reading one inside it. An
// account owns only what lies in its directory, so take the first path that does.
skills = ""
for _, path := range append([]string{spec.Skills.Global}, spec.AlsoScans.Global...) {
if skills = move(path); skills != "" {
break
}
}
return skills, move(spec.Agents.Global), nil
}
// agentCLI checks the executable an account runs instead of its Agent's. A name is looked
// up on PATH when a plugin command runs; a path must be absolute, since a relative one would
// depend on where Skillshare runs. Nothing goes through a shell, so it is one executable.
func agentCLI(cli string) (string, error) {
if !strings.ContainsAny(cli, `/\`) {
if strings.ContainsAny(cli, " \t") {
return "", fmt.Errorf("cli %q must be one executable, without arguments", cli)
}
return cli, nil
}
if path := expandPath(cli); filepath.IsAbs(path) {
return path, nil
}
return "", fmt.Errorf("cli %q must be a name found on PATH or an absolute path, which may start with ~", cli)
}
// expandAgentConfigDirs fills in the paths of targets that are another config directory
// of a built-in Agent, such as a second account. A path written in the config wins.
func (c *Config) expandAgentConfigDirs() error {
names := make([]string, 0, len(c.Targets))
for name := range c.Targets {
names = append(names, name)
}
sort.Strings(names)
dirs := map[string]string{}
for _, name := range names {
target := c.Targets[name]
if target.Agent == "" && target.ConfigDir == "" && target.CLI == "" {
continue
}
if target.Agent == "" || target.ConfigDir == "" {
return fmt.Errorf("targets: %s: agent and config_dir go together, and cli needs both", name)
}
if target.CLI != "" {
cli, err := agentCLI(target.CLI)
if err != nil {
return fmt.Errorf("targets: %s: %w", name, err)
}
target.CLI = cli
}
if _, builtin := globalSpec(name); builtin || name == "none" {
return fmt.Errorf("targets: %s: that name is taken by a built-in target; choose another, such as %s-work", name, target.Agent)
}
target.ConfigDir = expandPath(target.ConfigDir)
skills, agents, err := AgentConfigDirPaths(target.Agent, target.ConfigDir)
if err != nil {
return fmt.Errorf("targets: %s: %w", name, err)
}
key := filepath.Clean(target.ConfigDir)
if other, taken := dirs[key]; taken {
return fmt.Errorf("targets: %s and %s share config_dir %s", other, name, target.ConfigDir)
}
dirs[key] = name
if skills != "" && target.SkillsConfig().Path == "" {
target.EnsureSkills().Path, target.derivedSkills = skills, skills
}
if agents != "" && target.AgentsConfig().Path == "" {
target.EnsureAgents().Path, target.derivedAgents = agents, agents
}
c.Targets[name] = target
}
return nil
}
// AddAgentConfigDirTarget adds a target that is another config directory of a built-in
// Agent and returns the skills path it syncs to. cli is optional; see TargetConfig.CLI.
func (c *Config) AddAgentConfigDirTarget(name, agent, dir, cli string) (string, error) {
if _, exists := c.Targets[name]; exists {
return "", fmt.Errorf("target '%s' already exists", name)
}
if c.Targets == nil {
c.Targets = map[string]TargetConfig{}
}
tc := TargetConfig{Agent: agent, ConfigDir: dir, CLI: cli, defaultTargetNaming: c.TargetNaming}
if mode := NewTargetSkillsMode(c.TargetNaming, c.Mode); mode != "" {
tc.Skills = &ResourceTargetConfig{Mode: mode}
}
c.Targets[name] = tc
if err := c.expandAgentConfigDirs(); err != nil {
delete(c.Targets, name)
return "", err
}
target := c.Targets[name]
return target.SkillsConfig().Path, nil
}
// withoutDerivedPaths drops the paths filled in from config_dir or the environment, so
// they keep following it instead of being saved as written ones.
func (tc TargetConfig) withoutDerivedPaths() TargetConfig {
strip := func(rc *ResourceTargetConfig, derived string) *ResourceTargetConfig {
if rc == nil || derived == "" || rc.Path != derived {
return rc
}
out := *rc
if out.Path = ""; out.IsEmpty() {
return nil
}
return &out
}
tc.Skills, tc.Agents = strip(tc.Skills, tc.derivedSkills), strip(tc.Agents, tc.derivedAgents)
return tc
}