ProbeSkills took only a GitHub source or a folder. It now also takes any
git repository's address: https/http on gitlab.com or a self-hosted
server (any depth of groups), a GitLab or Gitea page of a folder
(/-/tree/<ref>/<path>, /-/blob/..., /src/branch/...), ssh://,
git@host:group/repo.git, a bare host.name/group/repo, and file://.
Such a source is kind "git" (gitSource, skillgit.go).
It is fetched with the user's own git (proc.FindTool), so their
credential helpers and SSH keys reach an intranet host: git init, then
fetch --depth 1 --no-tags -- <url> <ref|HEAD>, checkout FETCH_HEAD into
a magpie-skill-* folder, .git removed; the same keepProbe/maxFetch path
GitHub's tarball takes.
What the user typed is data: an address starting with - and any
transport:: address (ext::, fd::, file::) are refused before git runs;
the address follows --; git runs with protocol.ext.allow=never and
core.symlinks=false, no shell, GIT_TERMINAL_PROMPT=0, and ssh -o
BatchMode=yes unless the user set their own SSH command (GIT_SSH,
GIT_SSH_COMMAND, core.sshCommand), so a sign-in it can't make fails
instead of waiting. An address with a password or token in it is
refused: library.json is shown and backed up. Git missing, a sign-in
refused, an unknown host key, a missing branch and an unreachable host
each say so plainly (gitError).
Updates: Commit keeps the repository head the skill was fetched at. A
check runs git ls-remote; another head is fetched (once per repository
and ref) and the skill's folder hashed, so a commit elsewhere in the
repository leaves it current and moves Commit on. UpdateSkill and the
edited-skill refusal work as for GitHub; EditedError and the page's
Changed here / Update / replace question name the repository instead of
GitHub. findNew, Group again, icons and the market stay GitHub's only
(documented).
Page, TUI and docs: the add box's placeholder in en/zh/zh-TW/ja/de
(zh: owner/repo、GitHub 或 GitLab 链接、任意 git 仓库地址,或本机上的文件夹),
a git skill's source shown as a link (https) or text (SSH), its own
Update and Check for updates; the TUI's prompt; library.md's Skills and
Skill updates rows. GIT_SSH/GIT_SSH_COMMAND are listed in agentenv's
notAnAgent (TestFolderVarsAreListed).
Tests (fail on the old code: gitSource/gitPath don't exist there, and
skill-git-source.test.cjs fails on the old assets' placeholder, checked
by stashing client.js/i18n.js): TestGitSourceReads,
TestInstallFromGitRepo (a local bare repo over file://: probe, install,
check finds an update, update, a commit elsewhere stays current, an
edit here is refused naming the repo), TestGitTreeURLInstallsFolder,
TestGitMissingSaysSo, TestGitSignInRefusedSaysSo (a local httptest 401,
GIT_CONFIG_NOSYSTEM so no Keychain), TestGitRefusesOptionsAndTransports,
TestGitErrorsSaid. No test reaches the network.
Run (worktree at origin/main 224ea7d5b, machine load average ~120-145
from other sessions):
- gofmt -l: clean. go vet darwin, GOOS=linux -tags nogui, GOOS=windows:
clean.
- set -o pipefail; go test -timeout 45m ./...: 63 packages ok; one red,
internal/agent TestFolderVarsAreListed (GIT_SSH vars), fixed above
and rerun alone: ok.
- After later edits, rerun ./internal/library ./internal/cli
./internal/tui ok; internal/gui TestMainWindowCorners red once at
load 144 (76.5s, AppKit window test this change doesn't touch),
passed alone (28.6s); internal/agent timed out at 45m in that
combined rerun at load ~140 (it ran 962s in the full run) and passed
alone (1808s, load ~100).
- GUI: the 88 test files that load the library page, chromium and
webkit: 1420/1420 pass; skill-git-source + skill-update-edited rerun
after the last client.js edit: 20/20.
- Real use in a sandbox HOME (GIT_CONFIG_NOSYSTEM=1): a local bare repo
over file:// probed, installed, checked current, a new commit found as
update, updated to v2; ext:: and --upload-pack refused with nothing
run; https://gitlab.com/gitlab-org/ai/skills probed (52 skills), its
/-/tree/main/skills/glab installed and checked current; a private
gitlab.com address said "git couldn't sign in to gitlab.com ...".
- Not tried: a real self-hosted GitLab with credentials, ssh:// against
a real host, Windows.
124 lines
5.2 KiB
Go
124 lines
5.2 KiB
Go
package agent
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/agentenv"
|
|
)
|
|
|
|
// readsEnv matches the two ways these packages name a variable they read to
|
|
// find an agent's folder: as the argument of a Getenv, a LookupEnv or a
|
|
// place's getenv, and as the env of a table entry, which is how Qoder's two
|
|
// builds are named (read through at.getenv(b.env)).
|
|
var readsEnv = regexp.MustCompile(`(?:Getenv|LookupEnv|getenv)\("([A-Z][A-Z_0-9]*)"\)|env:\s*"([A-Z][A-Z_0-9]*)"`)
|
|
|
|
// notAnAgent are the variables the same sources read that are not an agent's
|
|
// folder, so no sandbox clears them through agentenv.Vars.
|
|
var notAnAgent = map[string]string{
|
|
"APPDATA": "Windows' own folder, which a sandbox sets to one of its own rather than clears",
|
|
"LOCALAPPDATA": "the same",
|
|
"PATH": "the process' own",
|
|
"USER": "the process' own",
|
|
"XDG_CONFIG_HOME": "magpie's own folder's, which appdir decides",
|
|
"XDG_DATA_HOME": "the same",
|
|
"XDG_CACHE_HOME": "Go's build cache's too, which a sandbox keeps shared; Air's ACP OpenCode under it is found by path in a test's own home (#2423)",
|
|
"MAGPIE_ADDR": "magpie's own",
|
|
"MAGPIE_DEBUG": "magpie's own debug flag",
|
|
"MAGPIE_PUBLIC_URL": "magpie's own public URL",
|
|
"MAGPIE_TRUST_PROXY": "magpie's own: a proxy here signs its clients in (#1022)",
|
|
"MAGPIE_EFFORT_UPDATES": "magpie's own switch for effort updates (#617)",
|
|
"MAGPIE_SITE_ORIGIN": "a site origin magpie's Omarchy theme answers, to try the site locally",
|
|
"MAGPIE_PARTNERS": "where magpie's own partner list is read from (a URL, or off), not a folder",
|
|
"CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "Claude Code's behaviour flag, not an agent's folder",
|
|
"GIT_SSH": "the user's own SSH program for git, which a skill fetched with their git keeps",
|
|
"GIT_SSH_COMMAND": "the same, as a command",
|
|
}
|
|
|
|
// TestFolderVarsAreListed says every variable the packages that find an
|
|
// agent's folder read is one a sandbox clears. Nothing said so before, so a
|
|
// new agent could take a variable of its own and every sandbox would go on
|
|
// meeting the agent a developer really has installed — which is how #522 came
|
|
// about, four sandboxes clearing lists of their own that had each drifted.
|
|
//
|
|
// The five packages are this one, sessions, provider, library and gateway,
|
|
// with appdir's codex.go, where CODEX_HOME is read for all of them:
|
|
// every agent's folder variable magpie reads is read in one of them, and the
|
|
// rest of the repository reads only magpie's own variables, the process' and
|
|
// the desktop's. Their sources are read as files rather than imported, since a
|
|
// test in sessions or provider cannot import this package, which imports
|
|
// them; what the walk looks at is what they say, not what they do.
|
|
//
|
|
// What this cannot see is a variable read through a name the sources build at
|
|
// runtime, as omoDir reads OMO_CODING_AGENT_DIR and SENPI_CODING_AGENT_DIR
|
|
// out of a slice, or through a constant (const fooEnv = "FOO_HOME" and then
|
|
// os.Getenv(fooEnv)). Both are in agentenv.Vars; a third taken either way
|
|
// would not be caught here.
|
|
func TestFolderVarsAreListed(t *testing.T) {
|
|
listed := make(map[string]bool, len(agentenv.Vars))
|
|
for _, v := range agentenv.Vars {
|
|
listed[v] = true
|
|
}
|
|
dirs := []string{".", filepath.Join("..", "sessions"), filepath.Join("..", "provider"), filepath.Join("..", "library"), filepath.Join("..", "gateway")}
|
|
found := map[string]string{}
|
|
var files []string
|
|
for _, dir := range dirs {
|
|
entries, err := os.ReadDir(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, e := range entries {
|
|
n := e.Name()
|
|
if e.IsDir() || !strings.HasSuffix(n, ".go") || strings.HasSuffix(n, "_test.go") {
|
|
continue
|
|
}
|
|
files = append(files, filepath.Join(dir, n))
|
|
}
|
|
}
|
|
files = append(files, filepath.Join("..", "appdir", "codex.go"))
|
|
for _, n := range files {
|
|
b, err := os.ReadFile(n)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, m := range readsEnv.FindAllStringSubmatch(string(b), -1) {
|
|
name := m[1]
|
|
if name == "" {
|
|
name = m[2]
|
|
}
|
|
if _, ok := found[name]; !ok {
|
|
found[name] = n
|
|
}
|
|
}
|
|
}
|
|
// One name read each way the pattern matches, so that a change in how a
|
|
// variable is read empties the scan loudly instead of leaving the test
|
|
// nothing to complain of.
|
|
for _, canary := range []string{"CODEX_HOME", "QODER_CONFIG_DIR"} {
|
|
if _, ok := found[canary]; !ok {
|
|
t.Errorf("%s was not found in these sources, so readsEnv no longer matches the way a variable is read", canary)
|
|
}
|
|
}
|
|
// And no excuse outlives the variable it was for.
|
|
for n := range notAnAgent {
|
|
if _, ok := found[n]; !ok {
|
|
t.Errorf("%s is in notAnAgent, but nothing in these sources reads it any more", n)
|
|
}
|
|
}
|
|
names := make([]string, 0, len(found))
|
|
for n := range found {
|
|
names = append(names, n)
|
|
}
|
|
slices.Sort(names)
|
|
for _, n := range names {
|
|
if listed[n] || notAnAgent[n] != "" {
|
|
continue
|
|
}
|
|
t.Errorf("%s, read by %s, is not in agentenv.Vars, so no sandbox clears it and a test meets the agent a developer really has installed: add it to Vars, or to notAnAgent here with a reason if it is not an agent's folder", n, found[n])
|
|
}
|
|
}
|