Files
skillshare/internal/plugin/pi_extensions.go
Rusty Shackleford da3383ed5f fix(plugins): separate shared intent from local installation state (#660)
* fix(plugins): separate shared intent from machine-local receipts

Shared configs currently carry native paths, reviewed snapshots and pending
failures, so one host can publish local progress over another host's target
selection. Persist versioned declarations separately from local applied and
pending receipts, and reconcile reviewed installs, imports and cleanup against
this machine's native inventory without rewriting intent during sync/update.

Keep snapshot names and adapter safeguards, move the operation lock locally,
validate legacy adoption with local evidence, and invalidate approval on local
state or native-context changes. Preserve source recipes after native adoption,
require local import for absent native-only targets, and document the shared
writer upgrade boundary in schemas and translated references.

Cover two-host behavior, generated legacy fixtures, failure/recovery, stale
intent, cleanup, npm recipes and the existing CLI/API/UI consumers with focused
resource-capped tests. Fixes #651.

* fix(plugins): preserve intent and ownership across host transitions

Repeat-add must retain shared target selection, and update previews must resolve
the requested ref before reviewing fresh or replacement installs. Keep disabled
targets on the uninstall path and reject refs unsupported by native-only/npm
intent before they can produce invalid shared declarations.

Block changed-context add/import before Pi registration preparation so a new
account cannot overwrite the old receipt. Preserve unresolved legacy bindings
as passive private proof candidates before rewriting shared YAML, retaining
original intent/store for later verified adoption and cleanup. Never compose
these hints as executable state, and persist their explicit resolution.

Cover the five review classes, native/private failure boundaries, requested-ref
retry, and original-context hint proofs with isolated focused regressions.
Keep local format v1 and baseline account validation unchanged. Refs #651.

* fix(plugins): allow receipt-only orphan cleanup to complete

An installation already removed on this host can retain a local receipt when
another host removes or replaces shared intent. Sync correctly plans forget,
but native command verification rejects that metadata-only action and blocks
all targets before the receipt can be cleared.

Exclude forget from native command verification while retaining host inventory
checks and OMP scoped cleanup preparation. The existing completion path then
persists receipt deletion, including pending work and passive legacy hints.

Cover the cross-host disable/re-enable/remove walk, five native adapters,
failed work with a legacy hint, and absent source replacement alongside a
healthy target. Assert Apply completion, unchanged shared bytes/mtime, and a
quiet second sync. Refs #651.

* fix(plugin): reconcile ref intent across inventory consumers

Validate explicit ref changes before disabled targets can save them and reuse
one reviewed discovery throughout a preview. Surface shared ref changes as local
reconciliation work while preserving the applied native identity and receipts.

Reuse scoped legacy proofs for Pi extension ownership and make full inventory
the dashboard's authoritative package/host pair. Cover invalid and replacement
refs, read-only ownership, and shared-state properties without new persisted
state or dependencies.

* test(plugin): repair and consolidate host-state fixtures

Create source intent and a pending imported Claude update through supported
operations so direct and batch updates exercise the intended fixture. Keep the
marketplace-loss case locally owned before testing its skipped reinstall.

Consolidate cross-host repeat-add and requested-ref replacement/orphan retries
at their existing owners, retaining failure recovery and shared-byte checks.
Trim duplicate HTTP and translated-text assertions from consumer tests.

* fix(plugin): reconcile legacy snapshot reads and native imports

Resolve legacy snapshot ownership through existing scoped proofs before listing,
reading or discovering cached source files. Keep adoption read-only and limit
proof work to the requested package and targets; current receipts need no CLI.

Only preserve reviewed source metadata on reimport when intent remains source
managed. Native-only intent must not resurrect an older Git source after a
same-ID local import. Retain identity mismatch and reviewed-source safeguards.

Remove the unused source-folding helper left by the old shared runtime writer.

* Preserve reviewed package versions locally

* Fix reviewed package version lifecycle

* fix(plugin): report missing local imports in doctor

Show the local-import instruction instead of claiming a skipped imported
plugin is healthy. Preserve the missing-CLI warning and shared config bytes.

Keep fixture native settings inside scratch XDG directories and serialize
the fake CLI calls used by concurrent inventories. Update assertions to
separate shared declarations from local versions and native IDs, and align
the raw-write audit with private plugin state and operation locks.

* test(plugin): use declarations for doctor source fixture

The missing-CLI case has no native ID, so encode it with the version 2
declaration schema. Keep the native-only import case in legacy syntax.

* fix(plugin): install shared sources from reviewed snapshots

When a source target is already declared, first sync must install its
reviewed local snapshot even if the native client has a package from the
same marketplace. Keep explicit add adoption and native-only import intact.

Extend the existing two-host adoption check to cover an independent native
installation on the second host and verify a quiet subsequent sync.

* fix(plugin): preserve reviewed retries and native collision boundaries

Retry a pending source update through the existing reviewed update path,
so a rolled-back native failure cannot clear progress at the old snapshot.
Record default-source versions for add, sync, and update while retaining
the recipe fingerprint and generic-version checks for target overrides.

Keep explicit native adoption, but block shared Claude sources when an
independent same-name native plugin could shadow their hooks. Extract the
existing adoption decision so the preview function becomes smaller.

* test(plugin): respect OMP update safety in version control

Check generic and target versions during review, then confirm the existing
OMP upgrade guard preserves the installed and default-source versions.

* fix(plugin): declare first local native imports explicitly

A first local import into a shared source target now changes that target
to native-only intent while preserving its sync selection. Verified source
reimports retain their reviewed source and the preview discloses conversion.

Cover the explicit import, local sync, and other-host cleanup flow, and
remove the unconsumed internal observation assertion from the OMP test.

* docs(plugin): explain first local import intent changes

Describe the shared native-only target conversion and its effect on other
machines, while distinguishing verified source reimports.

* fix(plugin): verify checked sources before native reinstalls

A checked update that becomes an install after native removal must still
match the source digest the user approved. Reject moved or unchecked
sources before native work, and allow a stable approved reinstall.

Clear completed reinstalls from dashboard update availability while keeping
failed targets available for retry. Cover both native presence states in
the existing checked-update tests and the dashboard outcome rule.

* fix(plugin): guard replacement before cleanup

* fix(plugin): refresh discovery for each manager action

A later Add action must review the current source even when the manager
previously observed that source. Keep discovery sharing across one action
while ending its cache before the next wizard invocation.

Exercise a fresh source version after an earlier Add preview without TTY
input or native writes, using an unavailable client to stop at review.

* test(plugin): cover desired checks and final update outcomes

Consolidate checked batch approval and dashboard completion tests while
retaining removed-native, digest, version and approval controls. Reproduce
stale shared recipes and failed or unfinished replacement installs.

* test(plugin): make stale source discovery observable

Remove the old repository before native-only and npm desired checks so
those existing cases cannot pass by reading the applied source.

* fix(plugin): check desired source and final outcome

* test(plugin): bind checked updates to the observed recipe

Reproduce a shared candidate change with equal version, commit and digest,
and preserve a freshly checked replacement whose native ID differs. Use
actual check observations in approval and native update controls, reject
missing source identity, and verify the UI carries the observation through.

* fix(plugin): bind checked update to source recipe

* fix(ui): classify npm recipes as Pi packages

* fix(plugin): retain legacy removal intent during migration

Omit pending removals from shared declarations while retaining original local ownership proof so sync retries cleanup.

Verification: focused regression passed in an isolated Linux Codex container, not the devcontainer (GOMAXPROCS=4, -p=4).

* fix(plugin): keep imports local to an existing shared recipe

Adopt a native installation into its local receipt without converting the shared source target or removing other machines installations.

Verification: focused import regressions passed in an isolated Linux Codex container, not the devcontainer (GOMAXPROCS=4, -p=4).

* fix(plugin): capture legacy evidence on first read

Persist passive hints before another machine writes v2 and save verified adoption on reads. Apply reuses preview receipts instead of querying every legacy binding again. Document the per-machine pre-migration read.

Verification: migration regressions passed in isolated Linux Codex container runs, not the devcontainer (GOMAXPROCS=4, -p=4).

* fix(plugin): target named declarations in interactive imports

Offer unresolved package names in dashboard and TUI imports and pass the selected name in single and batch requests. Drop display-only rendered assertions.

Verification: 47 UI tests passed with at most four workers in an isolated Linux Codex container, not the devcontainer.

* fix(plugin): localize shared intent plan guidance

Add message keys for local imports, account changes, cleanup and native collisions across all dashboard locales. Localized guidance contains no CLI flags.

Verification: failing localization regression now passes in an isolated Linux Codex container, not the devcontainer (GOMAXPROCS=4, -p=4).

* refactor(plugin): reuse preview inventory and guard apply boundaries

Capture hints on first load without persisting in-memory adoption during preview, keeping approval revisions stable. Apply reuses preview inventory. Reuse accountEnv and guard applyChange plus native write boundaries without inspecting argv.

Verification: migration, receipt, account and sync regressions passed in an isolated Linux Codex container, not the devcontainer (GOMAXPROCS=4, -p=4).

* test(plugin): organize host state coverage around lifecycle scenarios

Group migration, ref drift, account context and cleanup tests behind shared host fixtures and public Preview/Apply assertions. Cover Codex, Cursor, Antigravity and project scopes; add second-machine doctor coverage and the two-machine runbook. Consolidate package-version tests and remove the unused private-record fixture.

Verification: focused plugin regressions passed in an isolated Linux Codex container, not the devcontainer (GOMAXPROCS=4, -p=4).

* docs(plugin): explain local imports and per-machine migration

Document local receipts, per-machine review and commit pinning, config-path state identity and the required legacy read on every machine. Align sharing, doctor, troubleshooting, file-layout and four translated plugin references; restore schema editing guidance.

Verification: context-router check passed. Runtime verification uses an isolated Linux Codex container, not the devcontainer.

* refactor(plugin): separate unrelated review scope

Move project OMP configuration, plugin list ordering, and npm Pi classification to independent local branches based on upstream/main. Keep the shared intent change focused and cover named import selection in the pure flow helper.

The three local branches have passing builds in a Linux Codex container, not the devcontainer, with isolated test homes and capped parallelism.

* perf(plugin): avoid recapturing v2 state on reads

Only compare and persist passive migration hints when legacy bindings exist. Native v2 reads need no state serialization for migration.

Verification: pending removal and first-read/import regressions passed in a Linux Codex container, not the devcontainer, with isolated HOME/XDG/TMPDIR and GOMAXPROCS=4 GOFLAGS=-p=4.

* docs(plugin): clarify native marketplace repair after import

A local import receipt is not a reviewed reinstall source. Correct the remaining troubleshooting instruction and its four translations to require native-client repair, and align the nearby code comment.

Validation: checked against prepareChange, context-router check and git diff --check passed. Verification for this review ran in a Linux Codex container, not the devcontainer.

* fix(plugins): preserve local ownership across shared intent changes

Retain completed legacy migration markers so unchanged v1 YAML cannot
uninstall a later manual reinstall. Serialize read-side hint persistence
with native operations and keep local imports when shared recipes change.
Require explicit dashboard import names and validate recipe identities.

Correct schema descriptions, messages, CLI ref status, translated migration
guidance and runbooks. Add failing-first Preview/Apply regressions and a
real two-ssenv CLI lifecycle script.

* fix(plugins): preserve migration history and show retained import guidance

Mark existing legacy hints and receipts as captured before cleanup so state
from the previous serializer cannot re-adopt a later manual installation.
Keep recipe-change guidance in unchanged sync outcomes and visible dashboard
rows, and resolve nested OpenCode entries through their package manifest.

Cover public migration, ref, account, import and message contracts across
adapters. Correct the real two-host Cursor assertions, explicit dashboard
selection flow, and remaining localized guidance.

* fix(plugins): retain explicit OpenCode entry during import matching

Standalone OpenCode packages can rely on a reviewed explicit entry rather
than an SDK dependency. Preserve that entry when reading native package
identity so matching nested imports remain supported.

* fix(plugins): preserve migrated imports and verify native entry identity

Recognize older native-only receipts by their existing declaration
fingerprint so remote source recipe edits cannot replace migrated imports.
Keep managed npm receipts distinct through their source fingerprints.

Only borrow an ancestor OpenCode package identity when its resolved entry
is the actual registered file. Retain nested and explicit entry support.
Add public two-host migration and mismatching-entry regressions.

* fix(plugins): retain imported ownership through repeated adds

Persist inferred imported ownership when a matching add refreshes the
receipt intent, so later shared recipe changes still preserve local
native installations. Keep the legacy replacement regression aligned
with that ownership contract while retaining removed-intent cleanup.

Reject missing or unstatable absolute OpenCode entries before identity
matching rather than falling back to the file name. Cover both paths
through public Preview and Apply regressions.

* test(plugins): isolate OpenCode adapter fixtures from host config

Keep native registration fixtures inside their fake agent home by setting
XDG_CONFIG_HOME and clearing inherited OpenCode overrides. Ambient config
writes can otherwise leak into config-discovery tests in other packages.

Cover the XDG, file, directory and inline override cases with temporary
config sentinels, while retaining the existing MCP listing expectation.

* fix(plugin): let local imports return to the shared source

Add a local unimport action that removes only this machine’s installation and receipt while preserving the shared source declaration. Keep retained-import guidance visible across matching adds, list, doctor and dashboard consumers.

Avoid idle OMP guidance and unchanged legacy-read locks, preserve localized errors, keep import identity checks offline, and normalize OpenCode drive URLs. Cover the A/B transition and align translations, runbooks and environment cleanup.

* fix(plugin): complete unimport validation and guidance

Reject missing targets before planning local unimports and share per-target eligibility with the wizard. Make native uninstall effects explicit in help and translated docs, preserve structured update guidance, and cover error paths and shell completion.
2026-10-31 04:23:20 +08:00

854 lines
28 KiB
Go

package plugin
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"maps"
"os"
"path"
"path/filepath"
"runtime"
"slices"
"strconv"
"strings"
"unicode/utf8"
)
// The extensions of a Pi target (issue #342): which package extensions its settings
// select, read statically. Global, account and project targets can change the
// selection of one extension at a time when their own Pi is PiMinVersion or later. A
// project change is saved to the project's .pi/settings.json only; whether Pi
// trusts the project, and so reads that file, stays Pi's decision.
// PiMinVersion is the oldest Pi that scripts/pi/version-matrix.sh installed and passed
// (scripts/pi/version-evidence.json): the package contract against dist/core and the
// CLI's bundle, and the native lock test. Older or unparsable versions are read-only.
const PiMinVersion = "0.99.2"
// plainVersion splits a plain X.Y.Z version into its numbers, nil for anything else.
func plainVersion(v string) []int {
fields := strings.Split(v, ".")
if len(fields) != 3 {
return nil
}
out := make([]int, 3)
for i, field := range fields {
n, err := strconv.Atoi(field)
if err != nil || n < 0 {
return nil
}
out[i] = n
}
return out
}
// piVersionSupported reports a plain X.Y.Z version at or above PiMinVersion.
func piVersionSupported(version string) bool {
got := plainVersion(version)
return got != nil && slices.Compare(got, plainVersion(PiMinVersion)) >= 0
}
// Read-only reasons, translated by the dashboard as piExtensions.readOnly.<key>.
const (
piReadOnlyFork = "fork"
piReadOnlyNoCLI = "noCli"
piReadOnlyUnsupported = "unsupportedVersion"
piReadOnlySettings = "settings"
)
type PiExtensionsView struct {
Target string `json:"target"`
Scope string `json:"scope"` // global, account or project
SettingsPath string `json:"settingsPath"`
GlobalSettingsPath string `json:"globalSettingsPath,omitempty"`
Version string `json:"version"`
MinVersion string `json:"minVersion"`
Editable bool `json:"editable"`
ReadOnly string `json:"readOnly,omitempty"`
Problem string `json:"problem,omitempty"` // the settings file cannot be used
Revision string `json:"revision"`
Packages []PiExtensionPackage `json:"packages"`
Folders []PiExtensionFolder `json:"folders"`
Trust *PiTrustHints `json:"trust,omitempty"`
}
// PiExtensionPackage is one entry of settings.json "packages". Rules is its
// "extensions" list as written, null when the entry has none.
type PiExtensionPackage struct {
Index int `json:"index"`
Source string `json:"source"`
Identity string `json:"identity"`
Kind string `json:"kind"` // npm, git, local, unknown
Form string `json:"form"` // string or object
Scope string `json:"scope"` // global or project: which file the entry is in
Shape string `json:"shape,omitempty"`
Install string `json:"install"` // present, missing or unknown
Version string `json:"version,omitempty"`
Problem string `json:"problem,omitempty"`
// ReadOnly says why rows that Skillshare can read can't be switched here:
// singleFile (Pi ignores filters) or otherResources (a first rule turns the
// string entry into an object, and Skillshare can't show that this leaves the
// package's skills, prompts and themes as they are).
ReadOnly string `json:"readOnly,omitempty"`
Rules []string `json:"rules"`
GlobalRules []string `json:"globalRules,omitempty"`
OtherKeys []string `json:"otherKeys"`
ManagedBy string `json:"managedBy,omitempty"`
Rows []PiExtensionRow `json:"rows"`
}
// PiExtensionRow is one extension. Selection is what the settings select (for a
// project: configured, if Pi trusts it); whether Pi loaded it is never known here.
type PiExtensionRow struct {
Path string `json:"path"`
File string `json:"file"` // present or missing
Selection string `json:"selection"` // loads, skipped, unknown, or none for a missing file
Origin string `json:"origin"` // default, rule, glob, emptyList, file, inherited, project, unnamed
Rule string `json:"rule,omitempty"`
Globs []string `json:"globs,omitempty"`
Editable bool `json:"editable"`
// Unruled is the row's selection once its exact rule is removed, when Skillshare
// can make that change; a switch removes the rule when that is the state it wants.
Unruled string `json:"unruled,omitempty"`
}
// PiExtensionFolder is extensions Pi finds outside packages, always read-only.
type PiExtensionFolder struct {
Path string `json:"path"`
Kind string `json:"kind"` // folder, or settings for paths listed in settings
Scope string `json:"scope,omitempty"` // project view only: global or project
Problem string `json:"problem,omitempty"`
Rows []PiExtensionFolderRow `json:"rows"`
}
type PiExtensionFolderRow struct {
Path string `json:"path"`
File string `json:"file"`
Selection string `json:"selection"`
Provenance string `json:"provenance"` // native or extras
Extra string `json:"extra,omitempty"`
}
// PiTrustHints are what Pi saved, shown as hints only: an extension handler or a
// session-only choice can decide trust differently.
type PiTrustHints struct {
Saved string `json:"saved"` // trusted, untrusted, none, unknown
Default string `json:"default"` // always, never, ask, unset, unknown
}
// piEntry is one parsed "packages" entry.
type piEntry struct {
index int
source string
object bool
fields map[string]json.RawMessage
autoloadFalse bool
rules []string
hasRules bool
problem string
// badSource: the source can't be read as Pi reads it, so the entry may be any package.
badSource bool
}
type piSettings struct {
path string
raw []byte
exists bool
problem string
body map[string]json.RawMessage
entries []piEntry
}
// readPiSettings reads settings.json as Pi does, strictly: Pi refuses JSON with
// comments, so Skillshare never treats such a file as usable.
func readPiSettings(root, file string) *piSettings {
st := &piSettings{path: file, body: map[string]json.RawMessage{}}
if err := noSymlink(root, file); err != nil {
st.problem = "symlink"
return st
}
raw, err := os.ReadFile(file)
if errors.Is(err, os.ErrNotExist) {
return st
}
if err != nil {
st.problem = piProblemUnreadable
return st
}
st.raw, st.exists = raw, true
if len(raw) == 0 {
return st
}
switch {
case bytes.HasPrefix(raw, []byte{0xEF, 0xBB, 0xBF}):
st.problem = "bom"
case !json.Valid(raw):
st.problem = "invalidJson"
case jsonDuplicateKeys(raw):
st.problem = "duplicateKey"
case json.Unmarshal(raw, &st.body) != nil || st.body == nil:
st.problem = "notObject"
}
if st.problem != "" {
st.body = map[string]json.RawMessage{}
return st
}
var list []json.RawMessage
if field, ok := st.body["packages"]; ok && (json.Unmarshal(field, &list) != nil || list == nil) {
st.problem = "packagesInvalid"
return st
}
for i, raw := range list {
st.entries = append(st.entries, parsePiEntry(i, raw))
}
return st
}
func parsePiEntry(i int, raw json.RawMessage) piEntry {
e := piEntry{index: i}
if json.Unmarshal(raw, &e.source) == nil {
if !piSourceOK(raw, e.source) {
e.problem, e.badSource = "unsupportedEntry", true
}
return e
}
e.object = true
if json.Unmarshal(raw, &e.fields) != nil || e.fields == nil || json.Unmarshal(e.fields["source"], &e.source) != nil || !piSourceOK(e.fields["source"], e.source) {
e.problem, e.badSource = "unsupportedEntry", true
return e
}
// Native Pi uses strict === false; decoding null into bool also yields false.
if a, ok := e.fields["autoload"]; ok && bytes.Equal(bytes.TrimSpace(a), []byte("false")) {
e.autoloadFalse = true
}
if r, ok := e.fields["extensions"]; ok {
e.hasRules = true
if json.Unmarshal(r, &e.rules) != nil || e.rules == nil || !piLossless(r) {
e.rules, e.problem = nil, "unsupportedEntry"
}
}
return e
}
// piSourceOK is false for a source Pi can't use (encoding/json reads null as "")
// or one Go would not read as Pi does.
func piSourceOK(raw json.RawMessage, source string) bool {
return strings.TrimSpace(source) != "" && piLossless(raw)
}
// piLossless reports whether Go decodes the strings of a JSON value as Pi
// (JavaScript) does. Go turns an unpaired UTF-16 surrogate escape, and invalid
// UTF-8, into U+FFFD, while JavaScript keeps the surrogate, so a rule could read
// as naming a different file. raw must be valid JSON.
func piLossless(raw []byte) bool {
if !utf8.Valid(raw) {
return false
}
hex := func(i int) uint64 {
if i+4 > len(raw) {
return 0
}
n, err := strconv.ParseUint(string(raw[i:i+4]), 16, 32)
if err != nil {
return 0
}
return n
}
in := false
for i := 0; i < len(raw); i++ {
switch {
case raw[i] == '"':
in = !in
case in && raw[i] == '\\' && i+1 < len(raw) && raw[i+1] == 'u':
switch r := hex(i + 2); {
case r >= 0xD800 && r <= 0xDBFF:
if i+12 > len(raw) || raw[i+6] != '\\' || raw[i+7] != 'u' {
return false
}
if lo := hex(i + 8); lo < 0xDC00 || lo > 0xDFFF {
return false
}
i += 11
case r >= 0xDC00 && r <= 0xDFFF:
return false
default:
i += 5
}
case in && raw[i] == '\\':
i++ // the escaped character, which may be a backslash
}
}
return true
}
// otherKeys names the entry's keys besides source and extensions, never their values.
func (e piEntry) otherKeys() []string {
keys := []string{}
for k := range e.fields {
if k != "source" && k != "extensions" {
keys = append(keys, k)
}
}
slices.Sort(keys)
return keys
}
func jsonDuplicateKeys(data []byte) bool {
dec := json.NewDecoder(bytes.NewReader(data))
var walk func() bool
walk = func() bool {
tok, err := dec.Token()
if err != nil {
return false
}
switch tok {
case json.Delim('{'):
seen := map[string]bool{}
for dec.More() {
key, err := dec.Token()
if err != nil {
return false
}
k, _ := key.(string)
if seen[k] {
return true
}
seen[k] = true
if walk() {
return true
}
}
_, _ = dec.Token()
case json.Delim('['):
for dec.More() {
if walk() {
return true
}
}
_, _ = dec.Token()
}
return false
}
return walk()
}
// piPackage locates one entry's files and evaluates it against any rule list.
type piPackage struct {
src piSource
install string
version string
problem string
single bool // a local source that is one file: Pi ignores its filters
// convertLoadsOthers: a string entry can't be shown to take a rule without
// changing other resources (see piLayout.conversionLoadsOthers).
convertLoadsOthers bool
abs string // package root, slash separated, for Pi's absolute-path matching
// The file sets an evaluation draws from (see piLayout.defaults and base).
defaultsString, defaultsObject, base []string
}
func openPiPackage(source, agentDir, projectDir, scope string) *piPackage {
p := &piPackage{src: resolvePiSource(source, agentDir, projectDir, scope), install: "unknown"}
if p.src.kind == "unknown" {
p.problem = "sourceUnknown"
return p
}
info, err := os.Stat(p.src.install)
if err != nil {
if p.src.kind == "npm" && scope == "user" {
// Native Pi may fall back to a legacy global npm/pnpm root. Without
// resolving that root, absence here does not establish non-installation.
p.problem = "sourceUnknown"
return p
}
p.install, p.problem = "missing", "notInstalled"
return p
}
p.install, p.abs = "present", filepath.ToSlash(p.src.install)
if info.Mode().IsRegular() && p.src.kind == "local" {
p.single = true
return p
}
if !info.IsDir() {
p.install, p.problem = "unknown", piProblemUnreadable
return p
}
l, err := openPiLayout(p.src.install)
if err != nil {
p.problem = piProblemUnreadable
return p
}
defer l.Close()
p.defaultsString, p.defaultsObject, p.base = l.defaults(false), l.defaults(true), l.base()
p.convertLoadsOthers = l.conversionLoadsOthers()
p.problem, p.version = l.problem, l.version
return p
}
// readOnly is why an entry of this form can't be switched here, "" if it can.
func (p *piPackage) readOnly(object bool) string {
switch {
case p.single:
return "singleFile"
case p.convertLoadsOthers && !object:
return "otherResources"
}
return ""
}
type piRowState struct {
rel string
present bool
eval piEvaluation
origin string
// inherited: a project override leaves this path to the global entry.
inherited bool
}
// evaluate is the selection of an entry with this form and rule list, as Pi
// applies it in global scope.
func (p *piPackage) evaluate(object, hasRules bool, rules []string) []piRowState {
if p.single {
return []piRowState{{rel: path.Base(p.abs), present: true, eval: piEvaluation{state: piOn}, origin: "file"}}
}
rows := []piRowState{}
switch {
case !hasRules:
files := p.defaultsString
if object {
files = p.defaultsObject
}
for _, f := range files {
rows = append(rows, piRowState{rel: f, present: true, eval: piEvaluation{state: piOn}, origin: "default"})
}
case len(rules) == 0:
for _, f := range p.base {
rows = append(rows, piRowState{rel: f, present: true, eval: piEvaluation{state: piOff}, origin: "emptyList"})
}
default:
for _, f := range p.base {
e := piEvaluate(rules, f, path.Join(p.abs, f))
origin := "default"
if e.exact != "" {
origin = "rule"
} else if len(e.globs) > 0 {
origin = "glob"
}
rows = append(rows, piRowState{rel: f, present: true, eval: e, origin: origin})
}
}
// An exact rule for a file the package no longer has still sits in the list.
for _, rule := range rules {
if !strings.HasPrefix(rule, "+") && !strings.HasPrefix(rule, "-") {
continue
}
target := piExactPattern(rule[1:], runtime.GOOS)
if slices.ContainsFunc(rows, func(r piRowState) bool { return r.rel == target || path.Join(p.abs, r.rel) == target }) {
continue
}
rows = append(rows, piRowState{rel: target, eval: piEvaluation{state: piUnknown, exact: rule}, origin: "rule"})
}
return rows
}
func (r piRowState) row(editable bool) PiExtensionRow {
row := PiExtensionRow{Path: r.rel, File: "present", Selection: r.eval.state.String(), Origin: r.origin, Rule: r.eval.exact, Globs: r.eval.globs}
if !r.present {
row.File, row.Selection = "missing", "none"
}
// A switch needs a known state; a missing file can only lose its rule. Under []
// any added rule would change every extension of the package.
row.Editable = editable && r.origin != "emptyList" && (r.present && r.eval.state != piUnknown || !r.present && r.eval.exact != "")
return row
}
// piSetUnruled fills Unruled for each switchable row with an exact rule. try
// returns the selections after a change and whether the guard accepts it.
func piSetUnruled(rows []PiExtensionRow, try func([]PiExtensionChange) (map[string]piRowState, error)) {
for i, r := range rows {
if !r.Editable || r.File != "present" || r.Rule == "" {
continue
}
after, err := try([]PiExtensionChange{{Path: r.Path, Action: "default"}})
if a, ok := after[r.Path]; err == nil && ok && a.eval.state != piUnknown {
rows[i].Unruled = a.eval.state.String()
}
}
}
// piTryEntry is piSetUnruled's try for an entry Pi applies as written.
func piTryEntry(p *piPackage, e piEntry) func([]PiExtensionChange) (map[string]piRowState, error) {
return func(changes []PiExtensionChange) (map[string]piRowState, error) {
rules := piEditRules(e.rules, p.abs, changes)
hasRules := len(rules) > 0
return piEvaluateMap(p.evaluate(true, hasRules, rules)), piGuard(p, e, hasRules, rules, changes)
}
}
// piTryDelta is piSetUnruled's try for a project override.
func piTryDelta(rules []string, p *piPackage, inherited []piRowState, hasBase bool) func([]PiExtensionChange) (map[string]piRowState, error) {
return func(changes []PiExtensionChange) (map[string]piRowState, error) {
before := piEvaluateMap(piDeltaStates(rules, p, inherited, hasBase))
after := piEvaluateMap(piDeltaStates(piEditRules(rules, p.abs, changes), p, inherited, hasBase))
return after, piGuardStates(before, after, changes, true)
}
}
// piTargetState is everything a preview or apply of a global target needs.
type piTargetState struct {
view *PiExtensionsView
settings *piSettings
agentDir string
packages map[int]*piPackage
}
func piCLIIsNative(cli, goos string) bool {
if goos == "windows" {
// Windows paths and launcher names are case-insensitive. Only accept the
// official executable/shim names, not arbitrary or stacked suffixes.
name := strings.ToLower(path.Base(strings.ReplaceAll(cli, `\`, "/")))
return name == "pi" || name == "pi.cmd" || name == "pi.exe"
}
return filepath.Base(cli) == "pi"
}
func (s *Service) piGate(ctx context.Context, target string) (version, readOnly string) {
if a, ok := s.account(target); ok && a.CLI != "" && !piCLIIsNative(a.CLI, runtime.GOOS) {
// Known unsupported by name: never run it just to read its version.
return "", piReadOnlyFork
}
out, err := s.run(ctx, target, "--version")
version = strings.TrimSpace(string(out))
switch {
case err != nil:
return "", piReadOnlyNoCLI
case !piVersionSupported(version):
return version, piReadOnlyUnsupported
}
return version, ""
}
// PiExtensions reads the extensions of a Pi target: "pi" or a Pi account, in the
// project when ProjectRoot is set.
func (s *Service) PiExtensions(ctx context.Context, target string) (*PiExtensionsView, error) {
if s.agentOf(target) != "pi" {
return nil, errors.New("not a Pi target")
}
if s.ProjectRoot != "" {
st, err := s.piProjectState(ctx, target)
if err != nil {
return nil, err
}
return st.view, nil
}
st, err := s.piGlobalState(ctx, target)
if err != nil {
return nil, err
}
return st.view, nil
}
func (s *Service) piGlobalState(ctx context.Context, target string) (*piTargetState, error) {
agentDir, err := s.piAgentDir(target)
if err != nil {
return nil, err
}
settings := readPiSettings(agentDir, filepath.Join(agentDir, "settings.json"))
version, readOnly := s.piGate(ctx, target)
if readOnly == "" && settings.problem != "" {
readOnly = piReadOnlySettings
}
scope := "global"
if _, ok := s.account(target); ok {
scope = "account"
}
v := &PiExtensionsView{
Target: target, Scope: scope, SettingsPath: settings.path, Version: version, MinVersion: PiMinVersion,
Editable: readOnly == "", ReadOnly: readOnly, Problem: settings.problem, Revision: hash(settings.raw),
Packages: []PiExtensionPackage{}, Folders: []PiExtensionFolder{},
}
st := &piTargetState{view: v, settings: settings, agentDir: agentDir, packages: map[int]*piPackage{}}
managed := s.piManaged(ctx, target)
seen := map[string]bool{}
unresolved := false
for _, e := range settings.entries {
pkg := PiExtensionPackage{Index: e.index, Source: redactSource(e.source), Form: "string", Scope: "global", Install: "unknown", Rules: nil, OtherKeys: e.otherKeys(), Rows: []PiExtensionRow{}}
if e.object {
pkg.Form = "object"
}
if e.hasRules {
pkg.Rules = e.rules
}
if e.problem != "" {
pkg.Problem = e.problem
// Pi still reads the entry, so it is the one that counts for its package.
if !e.badSource {
src := resolvePiSource(e.source, agentDir, "", "user")
pkg.ManagedBy = piManagedBy(managed, e.source, src)
seen[src.identity] = true
unresolved = unresolved || src.identity == ""
} else {
unresolved = true
}
v.Packages = append(v.Packages, pkg)
continue
}
p := openPiPackage(e.source, agentDir, "", "user")
st.packages[e.index] = p
pkg.Kind, pkg.Identity, pkg.Install, pkg.Version, pkg.Problem = p.src.kind, redactSource(p.src.identity), p.install, p.version, p.problem
pkg.ManagedBy = piManagedBy(managed, e.source, p.src)
switch {
case p.src.identity != "" && seen[p.src.identity]:
// Pi keeps the first entry of a package and ignores the rest.
pkg.Problem = "duplicate"
case unresolved:
// An earlier unknown identity may be the first entry of this package.
pkg.Problem = "sourceUnknown"
case e.autoloadFalse:
pkg.Problem = "autoloadGlobal"
}
unresolved = unresolved || p.src.identity == ""
seen[p.src.identity] = true
pkg.ReadOnly = p.readOnly(e.object)
if pkg.Problem == "" || pkg.Problem == "notInstalled" && len(e.rules) > 0 {
editable := v.Editable && pkg.Problem == "" && pkg.ReadOnly == ""
for _, r := range p.evaluate(e.object, e.hasRules, e.rules) {
// Rules for an uninstalled package are listed so they can be read, not changed.
if pkg.Problem != "" && r.present {
continue
}
pkg.Rows = append(pkg.Rows, r.row(editable))
}
piSetUnruled(pkg.Rows, piTryEntry(p, e))
}
v.Packages = append(v.Packages, pkg)
}
v.Folders = append(v.Folders, s.piFolders(agentDir, settings)...)
return st, nil
}
// piManaged maps the source of each plugin Skillshare installs into target to its name.
func (s *Service) piManaged(ctx context.Context, target string) map[string]string {
result := map[string]string{}
d, err := s.load()
if err != nil {
return result
}
var h *Host
s.adoptLegacy(d, func(requested string) Host {
if requested != target {
return Host{Error: "outside requested target"}
}
if h == nil {
current := s.host(ctx, target)
h = &current
}
return *h
})
for name, p := range d.packages {
if b, ok := p.Bindings[target]; ok && b.ID != "" && !b.Cleanup && !b.ContextChanged {
result[b.ID] = name
}
}
return result
}
// piManagedBy is the plugin that owns a settings entry: Skillshare records a Pi
// binding by its source, or for a local package by the path it resolves to.
func piManagedBy(managed map[string]string, source string, src piSource) string {
if name := managed[strings.TrimSpace(source)]; name != "" || src.kind != "local" {
return name
}
return managed[src.install]
}
// piFolders lists the extensions Pi discovers in baseDir/extensions and the paths
// listed in the settings' own "extensions", which apply to them as overrides.
func (s *Service) piFolders(baseDir string, settings *piSettings) []PiExtensionFolder {
var top []string
if raw, ok := settings.body["extensions"]; ok && (json.Unmarshal(raw, &top) != nil || !piLossless(raw)) {
return []PiExtensionFolder{{Path: filepath.Join(baseDir, "extensions"), Kind: "folder", Problem: "unsupportedEntry", Rows: []PiExtensionFolderRow{}}}
}
overrides, plain := []string{}, []string{}
for _, r := range top {
switch {
case strings.HasPrefix(r, "+") || strings.HasPrefix(r, "-") || strings.HasPrefix(r, "!"):
overrides = append(overrides, r)
case !strings.ContainsAny(r, "*?"):
plain = append(plain, r)
}
}
selection := func(abs string, patterns []string) string {
rel, err := filepath.Rel(baseDir, abs)
if err != nil || len(patterns) == 0 {
return piOn.String()
}
return piEvaluate(patterns, filepath.ToSlash(rel), filepath.ToSlash(abs)).state.String()
}
folder := PiExtensionFolder{Path: filepath.Join(baseDir, "extensions"), Kind: "folder", Rows: []PiExtensionFolderRow{}}
if l, err := openPiLayout(baseDir); err == nil {
l.follow = true
for _, rel := range l.autoEntries("extensions") {
abs := filepath.Join(baseDir, filepath.FromSlash(rel))
row := PiExtensionFolderRow{Path: rel, File: "present", Selection: selection(abs, overrides), Provenance: "native"}
if name := s.extraOf(baseDir, rel); name != "" {
row.Provenance, row.Extra = "extras", name
}
folder.Rows = append(folder.Rows, row)
}
folder.Problem = l.problem
l.Close()
}
folders := []PiExtensionFolder{folder}
if len(plain) > 0 {
listed := PiExtensionFolder{Path: settings.path, Kind: "settings", Rows: []PiExtensionFolderRow{}}
patterns := []string{}
for _, r := range top {
if strings.HasPrefix(r, "+") || strings.HasPrefix(r, "-") || strings.HasPrefix(r, "!") || strings.ContainsAny(r, "*?") {
patterns = append(patterns, r)
}
}
for _, p := range plain {
src := resolvePiSource(p, baseDir, baseDir, "user")
row := PiExtensionFolderRow{Path: p, File: "missing", Selection: "none", Provenance: "native"}
if src.kind == "local" {
if _, err := os.Stat(src.install); err == nil {
row.File = "present"
row.Selection = selection(src.install, patterns)
if info, _ := os.Stat(src.install); info != nil && info.IsDir() {
row.Selection = piUnknown.String()
}
}
}
listed.Rows = append(listed.Rows, row)
}
folders = append(folders, listed)
}
return folders
}
// extraOf names the extra whose source a folder entry links into, "" for a native file.
func (s *Service) extraOf(baseDir, rel string) string {
first, _, _ := strings.Cut(rel, "/")
link := filepath.Join(baseDir, first)
if first == "extensions" {
next, _, _ := strings.Cut(strings.TrimPrefix(rel, "extensions/"), "/")
link = filepath.Join(baseDir, "extensions", next)
}
dest, err := os.Readlink(link)
if err != nil {
return ""
}
if !filepath.IsAbs(dest) {
dest = filepath.Join(filepath.Dir(link), dest)
}
for _, name := range slices.Sorted(maps.Keys(s.ExtrasSources)) {
if r, err := filepath.Rel(s.ExtrasSources[name], dest); err == nil && r != ".." && !strings.HasPrefix(r, ".."+string(filepath.Separator)) {
return name
}
}
return ""
}
// piDeltaStates is a project autoload:false entry: the paths its rules name are
// decided here, every other path keeps the global selection when there is one,
// and without one Pi doesn't load it at all.
func piDeltaStates(rules []string, p *piPackage, inherited []piRowState, hasBase bool) []piRowState {
rows := []piRowState{}
named := map[string]bool{}
for _, f := range p.base {
state, ok, rule := piEvaluateDelta(rules, f, path.Join(p.abs, f))
if !ok {
continue
}
named[f] = true
rows = append(rows, piRowState{rel: f, present: true, eval: piEvaluation{state: state, exact: rule}, origin: "project"})
}
// Missing exact paths still belong to the project and can lose their rules.
for _, rule := range rules {
if !strings.HasPrefix(rule, "+") && !strings.HasPrefix(rule, "-") {
continue
}
target := piExactPattern(rule[1:], runtime.GOOS)
if slices.ContainsFunc(rows, func(r piRowState) bool { return r.rel == target || path.Join(p.abs, r.rel) == target }) {
continue
}
named[target] = true
rows = append(rows, piRowState{rel: target, eval: piEvaluation{state: piUnknown, exact: rule}, origin: "project"})
}
if hasBase {
for _, r := range inherited {
if !named[r.rel] {
r.inherited = true
rows = append(rows, r)
}
}
return rows
}
for _, f := range p.base {
if !named[f] {
rows = append(rows, piRowState{rel: f, present: true, eval: piEvaluation{state: piOff}, origin: "unnamed"})
}
}
return rows
}
func piDeltaRows(states []piRowState, editable bool) []PiExtensionRow {
rows := []PiExtensionRow{}
for _, r := range states {
row := r.row(editable)
if r.inherited {
row.Origin = "inherited"
}
rows = append(rows, row)
}
return rows
}
// piTrust reads the decision Pi saved for root and its default, as hints.
func piTrust(agentDir, root string, global *piSettings) *PiTrustHints {
hints := &PiTrustHints{Saved: "none", Default: "unset"}
if raw, ok := global.body["defaultProjectTrust"]; ok {
var d string
hints.Default = "unknown"
if json.Unmarshal(raw, &d) == nil && slices.Contains([]string{"always", "never", "ask"}, d) {
hints.Default = d
}
}
if global.problem != "" {
hints.Default = "unknown"
}
file := filepath.Join(agentDir, "trust.json")
f, err := os.Open(file)
if errors.Is(err, os.ErrNotExist) {
return hints
}
if err != nil {
hints.Saved = "unknown"
return hints
}
defer f.Close()
data, err := io.ReadAll(io.LimitReader(f, maxPreview))
var decisions map[string]*bool
if err != nil || json.Unmarshal(trimBOM(data), &decisions) != nil {
hints.Saved = "unknown"
return hints
}
dir := root
if real, err := filepath.EvalSymlinks(root); err == nil {
dir = real
}
for dir = filepath.Clean(dir); ; dir = filepath.Dir(dir) {
if d := decisions[dir]; d != nil {
hints.Saved = map[bool]string{true: "trusted", false: "untrusted"}[*d]
return hints
}
if filepath.Dir(dir) == dir {
return hints
}
}
}