What changed: fetch still downloads a repository as one codeload
tarball first. When its files pass the 200 MB limit (untar's errTooBig),
it now falls back to fetchSkillFiles: one request to GitHub's API for
the repository's file list (git/trees?recursive=1), then every file in
a folder with a SKILL.md, from raw.githubusercontent.com, each checked
against the git hash the list gives. The repository's folders are made
empty, so candidates, origin, a probe of a subfolder and an update check
read the result as they read a whole download. The 200 MB limit now
applies to what is actually fetched: skills that are themselves over it
still say so. A private repository's files come from the API's
git/blobs with the Settings token (it never goes to raw). A repository
found too big is remembered for the run, so the install after its probe,
and update checks, don't download 150 MB again before falling back.
gitTree's entries gained mode, sha and size (named gitEntry).
Why: vectorize-io/hindsight keeps its skills (a few hundred KB) beside
an app; its tarball is 188 MB gzipped, 253 MB unpacked, so any skill
in it failed with "the repository is too big to install skills from".
What was run:
- Real repo, sandbox HOME, no token: on the old code ProbeSkills
("https://github.com/vectorize-io/hindsight") fails with "too big"
after 34s. With the fix it lists all 15 skills (skills/*, the
integrations' skills; .claude/skills hidden as before) in 32s, and
installing skills/hindsight-docs writes its 160 files, the same count
GitHub's tree lists.
- New TestSkillsFromATooBigRepository and
TestTooBigRepositoryChecksWhatArrives (httptest stand-ins for codeload,
the API and raw, laid out as hindsight is, paths and tree entries
from its real listing). With the fallback taken out both fail with
"the repository is too big to install skills from (over 200 MB)".
They check raw is never asked for a file outside a skill, codeload
is asked once across two probes, a file that doesn't match its hash
is refused, and skills over the limit themselves still error.
- go test -tags nogui ./internal/library (ok, 141s); -race -count=10
-run TooBig (ok); gofmt -l clean; go vet, GOOS=linux and
GOOS=windows go vet -tags nogui ./internal/library clean.
Not tried: a private repository over the limit against real GitHub
(covered only by reading: raw 404 -> git/blobs with the token).
192 lines
6.9 KiB
Go
192 lines
6.9 KiB
Go
package library
|
|
|
|
import (
|
|
"crypto/sha1"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
slash "path"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
// fakeBigRepo stands in for codeload, GitHub's API and
|
|
// raw.githubusercontent.com for a repository laid out as
|
|
// vectorize-io/hindsight is (#1941): an app whose whole download is over
|
|
// the limit, with its skills in a few small folders beside it. It notes
|
|
// the paths raw was asked for and how often codeload was.
|
|
type fakeBigRepo struct {
|
|
mu sync.Mutex
|
|
raw []string
|
|
tarballs int
|
|
files map[string]string
|
|
serve map[string]string // what raw hands out, where it differs
|
|
}
|
|
|
|
func gitSHA(body string) string {
|
|
h := sha1.New()
|
|
fmt.Fprintf(h, "blob %d\x00%s", len(body), body)
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
}
|
|
|
|
func newFakeBigRepo(t *testing.T) *fakeBigRepo {
|
|
t.Helper()
|
|
big := strings.Repeat("PK\x03\x04 jar ", 20<<10) // 240 KB, past the test's limit
|
|
g := &fakeBigRepo{serve: map[string]string{}, files: map[string]string{
|
|
"README.md": "# Hindsight",
|
|
"hindsight-clients/go/openapi-generator-cli.jar": big,
|
|
"hindsight-clients/go/openapi-generator-config.yaml": "packageName: hindsight\n",
|
|
"skills/hindsight-docs/SKILL.md": "---\nname: hindsight-docs\ndescription: Hindsight's docs\n---\n# Docs\n",
|
|
"skills/hindsight-docs/references/best-practices.md": "# Best practices\n",
|
|
"skills/hindsight-docs/references/changelog/index.md": "# Changelog\n",
|
|
"skills/hindsight-local/SKILL.md": "---\nname: hindsight-local\ndescription: Run Hindsight locally\n---\n",
|
|
"skills/hindsight-local/scripts/start.sh": "#!/bin/sh\necho start\n",
|
|
".claude/skills/ship-it/SKILL.md": "---\nname: ship-it\n---\n",
|
|
"hindsight-integrations/cursor/skills/hindsight-recall/SKILL.md": "---\nname: hindsight-recall\ndescription: Recall\n---\n",
|
|
}}
|
|
codeload := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
g.mu.Lock()
|
|
g.tarballs++
|
|
g.mu.Unlock()
|
|
w.Write(tarball(t, g.files))
|
|
}))
|
|
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if !strings.HasPrefix(r.URL.Path, "/repos/vectorize-io/hindsight/git/trees/") {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
// GitHub's answer, as it lists hindsight: folders, then files with
|
|
// their mode, git hash and size
|
|
var tree []map[string]any
|
|
dirs := map[string]bool{}
|
|
paths := []string{}
|
|
for p := range g.files {
|
|
paths = append(paths, p)
|
|
for d := slash.Dir(p); d != "."; d = slash.Dir(d) {
|
|
dirs[d] = true
|
|
}
|
|
}
|
|
for d := range dirs {
|
|
paths = append(paths, d)
|
|
}
|
|
sort.Strings(paths)
|
|
for _, p := range paths {
|
|
if dirs[p] {
|
|
tree = append(tree, map[string]any{"mode": "040000", "path": p, "sha": "e488653ba64c6cfc3551ec46454b965421d50176", "type": "tree"})
|
|
continue
|
|
}
|
|
mode := "100644"
|
|
if strings.HasSuffix(p, ".sh") {
|
|
mode = "100755"
|
|
}
|
|
tree = append(tree, map[string]any{"mode": mode, "path": p, "sha": gitSHA(g.files[p]), "size": len(g.files[p]), "type": "blob"})
|
|
}
|
|
json.NewEncoder(w).Encode(map[string]any{"sha": "52ef60fad332184c3b8a1f9fff4756247fc07146", "tree": tree, "truncated": false})
|
|
}))
|
|
raw := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
p, _ := strings.CutPrefix(r.URL.Path, "/vectorize-io/hindsight/HEAD/")
|
|
g.mu.Lock()
|
|
g.raw = append(g.raw, p)
|
|
body, ok := g.serve[p]
|
|
g.mu.Unlock()
|
|
if !ok {
|
|
if body, ok = g.files[p]; !ok {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
}
|
|
fmt.Fprint(w, body)
|
|
}))
|
|
oldT, oldA, oldR, oldM := tarballURL, githubAPI, rawURL, maxFetch
|
|
tarballURL = func(repo, ref string) string { return codeload.URL + "/tar/" + repo }
|
|
githubAPI = api.URL
|
|
rawURL = func(repo, ref, path string) string {
|
|
if ref == "" {
|
|
ref = "HEAD"
|
|
}
|
|
return raw.URL + "/" + repo + "/" + ref + "/" + path
|
|
}
|
|
maxFetch = 100 << 10
|
|
t.Cleanup(func() {
|
|
tarballURL, githubAPI, rawURL, maxFetch = oldT, oldA, oldR, oldM
|
|
codeload.Close()
|
|
api.Close()
|
|
raw.Close()
|
|
tooBig.Lock()
|
|
clear(tooBig.m)
|
|
tooBig.Unlock()
|
|
})
|
|
return g
|
|
}
|
|
|
|
// #1941 (Goalonez): a repository too big to download whole, its skills in
|
|
// small folders beside an app, installs them: only the skills' folders are
|
|
// fetched, every file of them, and a second fetch goes straight to them.
|
|
func TestSkillsFromATooBigRepository(t *testing.T) {
|
|
sandbox(t)
|
|
t.Setenv("GITHUB_TOKEN", "")
|
|
t.Setenv("GH_TOKEN", "")
|
|
g := newFakeBigRepo(t)
|
|
|
|
p, err := ProbeSkills("https://github.com/vectorize-io/hindsight")
|
|
if err != nil {
|
|
t.Fatalf("probe: %v", err)
|
|
}
|
|
var names []string
|
|
for _, c := range p.Candidates {
|
|
names = append(names, c.Path)
|
|
}
|
|
if want := []string{"skills/hindsight-docs", "skills/hindsight-local", "hindsight-integrations/cursor/skills/hindsight-recall"}; strings.Join(names, " ") != strings.Join(want, " ") {
|
|
t.Fatalf("candidates %v, want %v", names, want)
|
|
}
|
|
ok(t)(InstallSkills("https://github.com/vectorize-io/hindsight", []string{"skills/hindsight-docs", "skills/hindsight-local"}, []string{"claude"}))
|
|
for _, f := range []string{"hindsight-docs/SKILL.md", "hindsight-docs/references/changelog/index.md", "hindsight-local/scripts/start.sh"} {
|
|
b, err := os.ReadFile(filepath.Join(skillDir("."), f))
|
|
if err != nil || string(b) != g.files["skills/"+f] {
|
|
t.Errorf("%s: %q, %v", f, b, err)
|
|
}
|
|
}
|
|
g.mu.Lock()
|
|
for _, p := range g.raw {
|
|
if strings.HasPrefix(p, "hindsight-clients/") || p == "README.md" {
|
|
t.Errorf("raw was asked for %s, which no skill has", p)
|
|
}
|
|
}
|
|
g.mu.Unlock()
|
|
|
|
// a subfolder deep in the repository, as a link to it gives
|
|
p, err = ProbeSkills("https://github.com/vectorize-io/hindsight/tree/HEAD/hindsight-integrations/cursor")
|
|
if err != nil || len(p.Candidates) != 1 || p.Candidates[0].Name != "hindsight-recall" {
|
|
t.Fatalf("subfolder probe: %+v, %v", p, err)
|
|
}
|
|
g.mu.Lock()
|
|
defer g.mu.Unlock()
|
|
if g.tarballs != 1 {
|
|
t.Errorf("codeload was asked %d times; once found too big, the repository's skills are fetched alone", g.tarballs)
|
|
}
|
|
}
|
|
|
|
// What raw hands out must be the file GitHub's list names; skills too big
|
|
// themselves still say so.
|
|
func TestTooBigRepositoryChecksWhatArrives(t *testing.T) {
|
|
sandbox(t)
|
|
t.Setenv("GITHUB_TOKEN", "")
|
|
t.Setenv("GH_TOKEN", "")
|
|
g := newFakeBigRepo(t)
|
|
g.serve["skills/hindsight-docs/references/best-practices.md"] = "something else"
|
|
if _, err := ProbeSkills("vectorize-io/hindsight"); err == nil || !strings.Contains(err.Error(), "changed while it was being fetched") {
|
|
t.Fatalf("a file that isn't the listed one: %v", err)
|
|
}
|
|
delete(g.serve, "skills/hindsight-docs/references/best-practices.md")
|
|
g.files["skills/hindsight-local/model.bin"] = strings.Repeat("x", 200<<10)
|
|
if _, err := ProbeSkills("vectorize-io/hindsight"); err == nil || !strings.Contains(err.Error(), "skills in vectorize-io/hindsight are too big") {
|
|
t.Fatalf("skills too big themselves: %v", err)
|
|
}
|
|
}
|