fix(plugin): ask npm which registry it uses, and keep updates that never ran

Codex review on #400:

- The private-registry check read npm's config by hand, and each round
  found another place npm reads it from: the lowercase environment, the
  global npmrc, and now a globalconfig or prefix set in the user .npmrc.
  Rather than chase npm's rules, the check now asks npm itself:
  `npm config get registry @scope:registry` in the folder Pi runs npm in.
  Any registry other than npmjs, or npm not answering, leaves the package
  to Pi. The hand-written .npmrc reading is gone.

- When an apply stopped before any Agent ran, such as on a stale preview,
  the result had no outcomes, and the page took every planned update off
  its list, hiding the row's Update action. Only an Agent the result
  reports done comes off now.
This commit is contained in:
Willie
2026-10-05 00:32:56 +08:00
parent be661c593f
commit 116feb6d16
4 changed files with 100 additions and 103 deletions
+30 -56
View File
@@ -1,18 +1,15 @@
package plugin
import (
"cmp"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"slices"
"strings"
"time"
)
@@ -161,7 +158,7 @@ func (s *Service) npmVersions(ctx context.Context, target, id string, h Host, la
return nil, nil
}
settings, err := s.piSettingsPath(target)
if err != nil || !npmUsesPublicRegistry(filepath.Join(filepath.Dir(settings), "npm"), name) {
if err != nil || !npmUsesPublicRegistry(ctx, filepath.Join(filepath.Dir(settings), "npm"), name) {
return nil, nil
}
for _, item := range h.Installed {
@@ -180,68 +177,45 @@ func (s *Service) npmVersions(ctx context.Context, target, id string, h Host, la
return installed, latest
}
// npmGlobalConfig is the global npmrc npm reads: the one the environment names, else
// $PREFIX/etc/npmrc, with the prefix from the environment or the npm on PATH; "" when neither
// tells.
func npmGlobalConfig() string {
if path := cmp.Or(os.Getenv("npm_config_globalconfig"), os.Getenv("NPM_CONFIG_GLOBALCONFIG")); path != "" {
return path
}
prefix := cmp.Or(os.Getenv("npm_config_prefix"), os.Getenv("NPM_CONFIG_PREFIX"))
if prefix == "" {
npm, err := exec.LookPath("npm")
if err != nil {
return ""
}
// npm sits in $PREFIX/bin, or in $PREFIX itself on Windows.
prefix = filepath.Dir(npm)
if runtime.GOOS != "windows" {
prefix = filepath.Dir(prefix)
}
}
return filepath.Join(prefix, "etc", "npmrc")
// npmConfig runs `npm config get` with keys in dir, where Pi runs npm; tests stand in for it.
var npmConfig = func(ctx context.Context, dir string, keys ...string) (string, error) {
cmd := exec.CommandContext(ctx, "npm", append([]string{"config", "get"}, keys...)...)
cmd.Dir = dir
out, err := cmd.Output()
return string(out), err
}
// npmUsesPublicRegistry reports whether npm, run in dir as Pi runs it, fetches name from the
// public registry: no registry setting for the package's scope, or for everything, names
// another one in the environment or in the .npmrc files npm reads. When the global one
// can't be found, the answer is no, so a private name never goes to npmjs.
// ponytail: npm's builtin config, inside its own install, is not read; ask `npm config get`
// if a registry set only there must be seen.
func npmUsesPublicRegistry(dir, name string) bool {
// npmUsesPublicRegistry asks npm, in the folder Pi runs it in, which registry it fetches name
// from, for everything and for the package's scope. npm resolves its environment, every .npmrc
// and the overrides they make itself. Another registry, or npm not answering, is a no, so a
// private name never goes to npmjs.
func npmUsesPublicRegistry(ctx context.Context, dir, name string) bool {
keys := []string{"registry"}
if scope, _, ok := strings.Cut(name, "/"); ok && strings.HasPrefix(scope, "@") {
keys = append(keys, scope+":registry")
}
public := func(value string) bool {
value = strings.TrimSuffix(strings.Trim(strings.TrimSpace(value), `"'`), "/")
return value == "" || strings.TrimSuffix(npmRegistry, "/") == value || value == "https://registry.npmjs.org"
}
for _, key := range keys {
if !public(os.Getenv("npm_config_"+key)) || !public(os.Getenv("NPM_CONFIG_"+strings.ToUpper(key))) {
return false
}
}
user := cmp.Or(os.Getenv("npm_config_userconfig"), os.Getenv("NPM_CONFIG_USERCONFIG"))
if user == "" {
if home, err := os.UserHomeDir(); err == nil {
user = filepath.Join(home, ".npmrc")
}
}
global := npmGlobalConfig()
if global == "" {
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
out, err := npmConfig(ctx, dir, keys...)
if err != nil {
return false
}
for _, path := range []string{filepath.Join(dir, ".npmrc"), user, global} {
data, err := os.ReadFile(path)
if err != nil {
// One key prints its value; several print key=value lines, "undefined" for one not set.
values := map[string]string{"registry": strings.TrimSpace(out)}
if len(keys) > 1 {
for line := range strings.SplitSeq(out, "\n") {
if key, value, ok := strings.Cut(strings.TrimSpace(line), "="); ok {
values[key] = value
}
}
}
for _, key := range keys {
value := strings.TrimSuffix(strings.TrimSpace(values[key]), "/")
if key != "registry" && (value == "" || value == "undefined") {
continue
}
for line := range strings.SplitSeq(string(data), "\n") {
key, value, ok := strings.Cut(line, "=")
if ok && slices.Contains(keys, strings.TrimSpace(key)) && !public(value) {
return false
}
if value != "https://registry.npmjs.org" {
return false
}
}
return true
+50 -45
View File
@@ -3,6 +3,7 @@ package plugin
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"os"
@@ -273,8 +274,40 @@ func fakeNpmRegistry(t *testing.T, version string) {
old := npmRegistry
npmRegistry = server.URL
t.Cleanup(func() { npmRegistry = old })
// The machine's own npm config stays out of the test.
t.Setenv("npm_config_globalconfig", filepath.Join(t.TempDir(), "none"))
fakeNpmConfig(t, nil)
}
// fakeNpmConfig is `npm config get` with these settings, the public registry for anything else
// it is asked about; asked records each call's folder and keys. A nil error from answer means npm ran.
func fakeNpmConfig(t *testing.T, settings map[string]string) (asked *[]string) {
t.Helper()
calls := []string{}
old := npmConfig
npmConfig = func(_ context.Context, dir string, keys ...string) (string, error) {
calls = append(calls, dir+" "+strings.Join(keys, " "))
if settings["error"] != "" {
return "", errors.New(settings["error"])
}
value := func(key string) string {
if v, ok := settings[key]; ok {
return v
}
if key == "registry" {
return "https://registry.npmjs.org/"
}
return "undefined"
}
if len(keys) == 1 {
return value(keys[0]) + "\n", nil
}
lines := []string{}
for _, key := range keys {
lines = append(lines, key+"="+value(key))
}
return strings.Join(lines, "\n") + "\n", nil
}
t.Cleanup(func() { npmConfig = old })
return &calls
}
// installedNpmDemo adds npm:demo through Pi, with the package Pi installed at version.
@@ -324,31 +357,34 @@ func TestNpmPackageCheckFallsBackToPiWhenNpmDoesNotAnswer(t *testing.T) {
}
// A package npm fetches from another registry may share its name with a public one; npmjs's
// latest says nothing about it.
// latest says nothing about it. npm is asked in the folder Pi runs it in, scope included.
func TestNpmPackageCheckLeavesAPrivateRegistryToPi(t *testing.T) {
for name, npmrc := range map[string]struct{ dir, body string }{
"user registry": {"", "registry=https://npm.example.com/\n"},
"scope in Pi's folder": {".pi/agent/npm", "@acme:registry = https://npm.example.com/\n"},
for name, c := range map[string]struct {
id string
settings map[string]string
}{
"registry": {"npm:demo", map[string]string{"registry": "https://npm.example.com/"}},
"scope": {"npm:@acme/demo", map[string]string{"@acme:registry": "https://npm.example.com/"}},
"npm missing": {"npm:demo", map[string]string{"error": "exec: npm: not found"}},
} {
t.Run(name, func(t *testing.T) {
fakeNpmRegistry(t, "1.2.0")
var commands []string
s := fakePiNpm(t, &commands)
id := "npm:demo"
if npmrc.dir != "" {
id = "npm:@acme/demo"
}
applyPluginRequest(t, s, Request{Action: "add", Source: id, Name: "demo", Targets: []string{"pi"}})
pkg, _ := npmSpec(id)
applyPluginRequest(t, s, Request{Action: "add", Source: c.id, Name: "demo", Targets: []string{"pi"}})
pkg, _ := npmSpec(c.id)
writePluginFile(t, os.Getenv("HOME"), ".pi/agent/npm/node_modules/"+pkg+"/package.json", `{"version":"1.0.0"}`)
writePluginFile(t, filepath.Join(os.Getenv("HOME"), npmrc.dir), ".npmrc", npmrc.body)
asked := fakeNpmConfig(t, c.settings)
p, err := s.Preview(context.Background(), Request{Action: "check", Name: "demo"})
if err != nil {
t.Fatal(err)
}
if c := p.Changes[0]; c.Action != "native-check" {
if ch := p.Changes[0]; ch.Action != "native-check" {
t.Fatalf("changes: %+v", p.Changes)
}
if name == "scope" && (len(*asked) != 1 || (*asked)[0] != filepath.Join(os.Getenv("HOME"), ".pi/agent/npm")+" registry @acme:registry") {
t.Fatalf("asked npm: %q", *asked)
}
})
}
}
@@ -369,37 +405,6 @@ func TestNpmPackageCheckLeavesAVersionRangeToPi(t *testing.T) {
}
}
func TestNpmPackageCheckReadsTheGlobalNpmConfig(t *testing.T) {
fakeNpmRegistry(t, "1.2.0")
s := installedNpmDemo(t, "1.0.0")
npmrc := filepath.Join(t.TempDir(), "npmrc")
writePluginFile(t, filepath.Dir(npmrc), filepath.Base(npmrc), "registry=https://npm.example.com/\n")
t.Setenv("npm_config_globalconfig", npmrc)
p, err := s.Preview(context.Background(), Request{Action: "check", Name: "demo"})
if err != nil {
t.Fatal(err)
}
if c := p.Changes[0]; c.Action != "native-check" {
t.Fatalf("changes: %+v", p.Changes)
}
}
// npm reads its environment case-insensitively, so a lowercase userconfig names the .npmrc too.
func TestNpmPackageCheckReadsALowercaseUserconfig(t *testing.T) {
fakeNpmRegistry(t, "1.2.0")
s := installedNpmDemo(t, "1.0.0")
npmrc := filepath.Join(t.TempDir(), "npmrc")
writePluginFile(t, filepath.Dir(npmrc), filepath.Base(npmrc), "registry=https://npm.example.com/\n")
t.Setenv("npm_config_userconfig", npmrc)
p, err := s.Preview(context.Background(), Request{Action: "check", Name: "demo"})
if err != nil {
t.Fatal(err)
}
if c := p.Changes[0]; c.Action != "native-check" {
t.Fatalf("changes: %+v", p.Changes)
}
}
func TestDiscoverExplainsNpmPackagesCannotBePreviewed(t *testing.T) {
_, err := Discover(context.Background(), "npm:demo")
if err == nil || !strings.Contains(err.Error(), "plugin add npm:") {
+16
View File
@@ -154,6 +154,7 @@ describe('PluginsPage', () => {
vi.mocked(pluginsApi.preview)
.mockResolvedValueOnce({ revision: 'r', blocked: false, changes: [{ name: 'demo', target: 'codex', id: 'demo@market', action: 'update-available', binding: { id: 'demo@market', version: '1.0.0' } }] })
.mockResolvedValueOnce({ revision: 'r2', blocked: false, changes: [{ name: 'demo', target: 'codex', id: 'demo@market', action: 'update', binding: { id: 'demo@market', version: '1.0.0' } }] });
vi.mocked(pluginsApi.apply).mockResolvedValue({ result: { results: [{ name: 'demo', target: 'codex', status: 'installed' }] }, failure: '' });
mount();
fireEvent.click(await screen.findByRole('button', { name: 'plugins.check' }));
fireEvent.click(within(await screen.findByRole('dialog')).getByRole('button', { name: 'common.cancel' }));
@@ -163,6 +164,21 @@ describe('PluginsPage', () => {
await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument());
expect(screen.queryByRole('button', { name: 'plugins.update' })).not.toBeInTheDocument();
});
it('keeps the row update when the update stops before any Agent ran', async () => {
vi.mocked(pluginsApi.list).mockResolvedValue({ targetDefinitions: [{ target: 'codex', label: 'Codex', project: false, operations: ['add', 'check', 'update'] }], packages: { demo: { bindings: { codex: { id: 'demo@market', source: 'https://example.com/demo.git', version: '1.0.0' } } } }, hosts: [] });
vi.mocked(pluginsApi.preview)
.mockResolvedValueOnce({ revision: 'r', blocked: false, changes: [{ name: 'demo', target: 'codex', id: 'demo@market', action: 'update-available', binding: { id: 'demo@market', version: '1.1.0' } }] })
.mockResolvedValueOnce({ revision: 'r2', blocked: false, changes: [{ name: 'demo', target: 'codex', id: 'demo@market', action: 'update', binding: { id: 'demo@market', version: '1.1.0' } }] });
vi.mocked(pluginsApi.apply).mockResolvedValue({ result: null, failure: 'preview is stale' });
mount();
fireEvent.click(await screen.findByRole('button', { name: 'plugins.check' }));
fireEvent.click(within(await screen.findByRole('dialog')).getByRole('button', { name: 'common.cancel' }));
await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument());
fireEvent.click(screen.getByRole('button', { name: 'plugins.update' }));
fireEvent.click(within(await screen.findByRole('dialog')).getByRole('button', { name: 'plugins.apply' }));
expect(await screen.findByRole('alert')).toHaveTextContent('preview is stale');
expect(screen.getByRole('button', { name: 'plugins.update' })).toBeInTheDocument();
});
it('keeps the row update for an Agent still behind when another Agent already has the new version', async () => {
const pi = { label: 'Pi', project: false, operations: ['add', 'check', 'update'], npm: true };
vi.mocked(pluginsApi.list).mockResolvedValue({
+4 -2
View File
@@ -95,11 +95,13 @@ export default function PluginsPage() {
const response = await pluginsApi.apply(review.request, review.plan.revision);
setResult(response); setFailure(failureText(response)); setReview(null); refresh();
if (review.request.action === 'update') {
const failed = new Set((response.result?.results ?? []).filter((r) => r.status === 'failed').map((r) => `${r.name}:${r.target}`));
// Only an Agent the update reports done comes off; one that failed, or never ran because the
// apply stopped first, keeps its Update action.
const done = new Set((response.result?.results ?? []).filter((r) => r.status !== 'failed').map((r) => `${r.name}:${r.target}`));
setUpdates((prev) => {
const next = { ...prev };
for (const c of review.plan.changes) {
if (c.action !== 'update' || failed.has(`${c.name}:${c.target}`) || !next[c.name]) continue;
if (c.action !== 'update' || !done.has(`${c.name}:${c.target}`) || !next[c.name]) continue;
next[c.name] = { ...next[c.name] };
delete next[c.name][c.target];
}