Files
skillshare/internal/plugin/omp_extensions_selection.go
T
Willie 97446744ff feat(omp): add native Oh My Pi support (#423)
* feat(omp): add native Oh My Pi support

Support OMP skills, instructions, MCP, code hooks and guarded extension selection using its verified native contracts rather than Pi aliases.

Add reviewed marketplace plugin installation, cache-preserving scoped removal and fresh-cache reinstall. Align Pi/OMP extension cards and localize inventory guidance while keeping native ownership and read-only constraints.

Include regression coverage, isolated E2E runbooks, documentation and verification policies. Native plugin updates and unverified runtime platforms remain guarded.

Refs #409

* fix(omp): guard runtime ownership and localize inventory guidance

Native install recursively replaces its runtime destination, and a missing cache manifest could leave the real runtime link installed after removal. Bind and recheck an absent reviewed destination, and recover removal names only from verified scope-local links.

Localize fixed backend reasons and warnings across dashboard locales while preserving paths and external diagnostics. Restore the reviewed source in the existing reinstall fixture and record real native verification.

* fix(omp): allow forgetting natively removed plugins

A different plugin can retain node_modules after native uninstall deletes this installation. When its cache, registration and known runtime state are absent, forget only the stale Skillshare binding instead of demanding runtime-name proof for unrelated files.

Extend the existing removal recovery test with a failing-before-fix case that verifies the binding is dropped and unrelated native lock bytes and plugin files are preserved. make check passes.
2026-10-05 20:36:39 +08:00

201 lines
7.5 KiB
Go

package plugin
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"unicode/utf8"
)
// ompNativePackage identifies npm/Bun/source installs through the selected
// launcher, not arbitrary package.json files in HOME. Standalone binaries and
// wrappers without provable package ownership remain read-only; never run them.
func (s *Service) ompNativePackage(target string) (string, string) {
// Account CLI is plugin-command routing only, not an extension runtime.
bin := "omp"
path, err := exec.LookPath(bin)
if err != nil {
return "", "Cannot identify an installed OMP 18.6.1 package without executing the CLI."
}
path, err = filepath.EvalSymlinks(path)
if err != nil {
return "", "Cannot resolve the OMP launcher."
}
path, _ = filepath.Abs(path)
for dir := filepath.Dir(path); ; dir = filepath.Dir(dir) {
raw, err := ompRead(filepath.Join(dir, "package.json"))
if err == nil {
var pkg struct {
Name, Version string
Bin map[string]string
}
if json.Unmarshal(raw, &pkg) != nil || pkg.Name != "@oh-my-pi/pi-coding-agent" || pkg.Version != "18.6.1" {
return hash(raw), "Only the verified OMP 18.6.1 package is selectable."
}
entry, err := filepath.EvalSymlinks(filepath.Join(dir, pkg.Bin["omp"]))
if pkg.Bin["omp"] == "" || err != nil || entry != path {
return hash(raw), "OMP launcher ownership cannot be established; wrappers and standalone binaries are read-only."
}
return hash(raw) + "\x00" + path, ""
}
if !os.IsNotExist(err) {
return "", "OMP package metadata is unreadable."
}
if filepath.Dir(dir) == dir {
break
}
}
return "", "OMP launcher has no verified package metadata; standalone binaries are read-only."
}
// Reject links rather than following user-controlled configuration/source aliases.
// Writes also anchor the settings parent with os.OpenRoot and revalidate before rename.
func ompSafePath(path string) error {
if !filepath.IsAbs(path) || !utf8.ValidString(path) || strings.ContainsAny(path, "\x00\r\n") {
return fmt.Errorf("unsafe OMP path")
}
for p := filepath.Clean(path); ; p = filepath.Dir(p) {
st, err := os.Lstat(p)
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("unreadable OMP path")
}
if err == nil && (st.Mode()&os.ModeSymlink != 0 || p != path && !st.IsDir()) {
return fmt.Errorf("linked or non-directory OMP path component")
}
if filepath.Dir(p) == p {
break
}
}
return nil
}
func (inv *ompInventory) selection(s *Service, target string) {
v := inv.view
v.disabled = slices.Clone(inv.disabled)
gate, reason := s.ompNativePackage(target)
if reason == "" && !ompLockSupported() {
reason = "Native OMP locking is not implemented on this platform."
}
if reason == "" && (inv.uncertain || inv.selectionUncertain) {
reason = "Effective OMP settings are unknown; resolve the inventory warnings first."
}
if reason == "" {
if err := ompSafePath(v.SettingsPath); err != nil {
reason = err.Error()
}
}
if reason == "" {
if _, _, err := ompYAML(v.SettingsPath); err != nil {
reason = err.Error()
}
}
if reason == "" {
base := filepath.Join(inv.home, ".omp")
defaultAgent := filepath.Join(base, "agent")
agent := filepath.Dir(v.SettingsPath)
if s.ProjectRoot != "" {
agent = defaultAgent
if override := os.Getenv("PI_CODING_AGENT_DIR"); override != "" {
agent = override
}
}
if ompPluginRoot(base, agent) == "" {
root := ompPluginRoot(base, defaultAgent)
if _, err := os.Lstat(root); err == nil || !os.IsNotExist(err) {
reason = "The explicit agent directory does not establish ownership of the native user plugin root; resolve plugin name collisions in OMP first."
}
}
}
if reason == "" && s.ProjectRoot != "" {
home, _ := os.UserHomeDir()
if filepath.Clean(s.ProjectRoot) == filepath.Clean(home) {
reason = "OMP does not discover project settings at HOME."
}
}
// Constructing native settings when only legacy user JSON exists migrates it.
// Do not create YAML that prevents that migration from ever running.
if reason == "" {
_, err := os.Stat(v.SettingsPath)
if os.IsNotExist(err) {
if _, err := os.Lstat(filepath.Join(filepath.Dir(v.SettingsPath), "settings.json")); err == nil {
reason = "Legacy settings require native migration before YAML selection can be edited."
}
}
}
// Native startup loads .env inputs; this static editor cannot establish their overrides.
for _, root := range []string{filepath.Dir(v.SettingsPath), filepath.Dir(filepath.Dir(v.SettingsPath)), s.ProjectRoot} {
if root == "" {
continue
}
if _, err := os.Lstat(filepath.Join(root, ".env")); err == nil {
reason = "Native .env overrides are not evaluated; selection is read-only."
}
}
counts := map[string]int{}
bypasses := map[string]bool{}
for _, r := range v.Rows {
if r.Source == "hook" || slices.Contains(r.Notes, "Explicit file bypasses the extension-module disabled-name filter.") {
bypasses[r.Path] = true
}
if strings.HasPrefix(r.DerivedID, "extension-module:") {
counts[r.DerivedID]++
}
}
fingerprints := []string{gate}
selectable := false
for i := range v.Rows {
r := &v.Rows[i]
r.Key = hash([]byte(r.Source + "\x00" + r.Scope + "\x00" + r.Path + "\x00" + r.DerivedID))
if r.Selection != "unknown" {
enabled := !slices.Contains(inv.disabled, r.DerivedID)
r.Enabled = &enabled
}
switch {
case r.Owner == "hooks":
r.ReadOnlyReason = "Managed by Hooks; change it on the Hooks page."
case slices.Contains(r.Notes, "Hooks ownership record exists but the file has drifted; resolve it in Hooks before changing extension selection."):
r.ReadOnlyReason = "Hooks-owned file has drifted; resolve it on the Hooks page."
case r.Owner == "plugin" || r.Source == "plugin":
r.ReadOnlyReason = "Managed by its plugin; change it on the Plugins page."
case r.Source == "hook":
r.ReadOnlyReason = "Ambient hooks use hook IDs and bypass the extension-module filter."
case reason != "":
r.ReadOnlyReason = reason
case bypasses[r.Path]:
r.ReadOnlyReason = "This file also loads through an explicit file or ambient hook that bypasses extension-module filtering."
case counts[r.DerivedID] > 1:
r.ReadOnlyReason = "Duplicate derived-name group: changing this ID affects multiple entries; rename or resolve the collision first."
case r.Selection == "unknown" || r.Path == "":
r.ReadOnlyReason = "Effective selection could not be established."
case slices.Contains(r.Notes, "Explicit file bypasses the extension-module disabled-name filter."):
r.ReadOnlyReason = "Explicit configured files bypass disabledExtensions; edit their configuration in OMP."
case !ompScanFile(filepath.Base(r.Path), "plugin"):
r.ReadOnlyReason = "Unsupported extension entry-point type; selection is read-only."
case r.Source == "native" && ompHasGitignore(r.Path):
r.ReadOnlyReason = "Native gitignore filtering is not reproduced."
default:
if err := ompSafePath(r.Path); err != nil {
r.ReadOnlyReason = err.Error()
}
}
raw, err := ompRead(r.Path)
fingerprints = append(fingerprints, r.Path+"\x00"+hash(raw)+"\x00"+fmt.Sprint(err))
if r.ReadOnlyReason == "" && err != nil {
r.ReadOnlyReason = "Extension file is unreadable or exceeds the static inventory limit."
}
r.Selectable = r.ReadOnlyReason == ""
selectable = selectable || r.Selectable
}
v.ReadOnly = !selectable
if reason != "" {
v.Reasons = append(v.Reasons, reason)
}
slices.Sort(inv.metadata)
raw, _ := json.Marshal([]any{target, s.ProjectRoot, v.SettingsPath, v.Rows, inv.metadata, fingerprints})
v.Revision = hash(raw)
}