ggbr on Discord (#help): easyeda/easyeda-api-skill says to run
`npm install` in its own folder. Once that ran, the Library tagged the
skill Changed here (本地已改动). The check for updates still ran on it
(it compares commits, not the folder), but Update all left it out as
changed, and its own Update asked to replace "your changes" and threw
node_modules away with the old version, so the user had to install again.
Cause: editState compared the hash of the whole library folder (only
.DS_Store left out) with Hash, the hash of the files as fetched. The
install's node_modules and its rewrite of package-lock.json made them
differ.
Now what installing dependencies or running the skill writes is not a
change (fromInstall): node_modules, .venv, venv, __pycache__,
.pytest_cache, .mypy_cache, .ruff_cache, *.pyc/*.pyo, and the lock files
package-lock.json, yarn.lock, pnpm-lock.yaml, bun.lock(b), uv.lock.
- New Skill.Own: the hash of the files as fetched without those, set on
install, update and when skill_from binds a skill to its repo.
onlyInstalled compares the folder with it first.
- An agent's copy (copy mode) counts as edited only when its ownHash
differs from the library's.
- An update carries those entries from the version replaced into the new
one wherever the new one has no entry of its own (carryInstalled). The
new version's own lock file wins.
A file the user really edited (run.py, SKILL.md, package.json, ...) still
marks the skill changed and is never overwritten without asking. An edit
to a lock file alone is no longer protected; an update takes upstream's.
Skills already marked changed: they clear on their own on the next page
read when only installed folders and bytecode differ, or the lock file the
skill came with was left byte-for-byte (compared with Hash, leaving
installed folders aside). A skill fetched before this commit whose lock
file the install did rewrite has no Own, so it stays marked until its
next update (Update on its row asks once, keeps the old folder in the
backups, carries node_modules over and records Own). From then on, a
rewritten lock file isn't counted.
Run (mote, worktree at e863f520c plus this change):
- go test -timeout 45m -count=1 ./internal/library: ok (107s)
- go vet ./internal/library, GOOS=windows and GOOS=linux go vet: clean
- gofmt -l internal/library: clean
- Windows box (win-run): TestInstalledDependencies* and TestUpdateKeeps*
pass (the first run found the test comparing a link target with "/";
it now uses filepath.ToSlash)
Broken by hand, each red on mote:
- editState without onlyInstalled: TestInstalledDependenciesAreNoChange
and TestInstalledDependenciesBeforeOwnWasKept red ("installing eda's
dependencies made it changed here")
- apply without carryInstalled: node_modules, .venv, __pycache__ and the
.bin link didn't go on with the update
- copy branch on sameTree alone: TestInstalledDependenciesInACopy red
- onlyInstalled ignoring Own: the lock-file rewrite after an update reads
as changed
Not tried: a real `npm install` of easyeda-api-skill in the GUI. No GUI
assets changed. Not changed: takeEdits still takes node_modules
installed in an agent's copy into the library (as before), and folders
like build/ or dist/ still count, since a skill can ship them.
118 lines
3.7 KiB
Go
118 lines
3.7 KiB
Go
package library
|
|
|
|
// Reading the Library page compares skill folders whole: an agent's own
|
|
// skill against the same name in another agent (foundSkills), and each
|
|
// agent's copy against the library's skill (behind). Each compare read
|
|
// every byte of both folders on every read of the page, so a few dozen
|
|
// skills with node_modules or templates in them, given to several agents,
|
|
// read hundreds of megabytes each time the page opened; on Windows, with
|
|
// every file read scanned, that is minutes (0day404, #541). What a compare
|
|
// or a hash found is kept here against what a walk of the folders sees
|
|
// without opening a file: each entry's place, size and time. While those
|
|
// are the same, the answer is too. A folder that couldn't be walked or
|
|
// read whole is unknown: it is read again next time, never taken as
|
|
// unchanged.
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
stdhash "hash"
|
|
"io/fs"
|
|
"path/filepath"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// settleFor is how long ago the newest file of a folder must have been
|
|
// written for what was found of it to be kept: a file written again within
|
|
// the time a file system can tell apart could keep its time and size.
|
|
const settleFor = 3 * time.Second
|
|
|
|
// stamper sums what a walk sees of a folder's files and links without
|
|
// opening one: each one's place, type, size and time. The walk that reads
|
|
// a folder stamps it as it goes, before opening a file, so a file written
|
|
// while it is read has a stamp that says so next time.
|
|
type stamper struct {
|
|
h stdhash.Hash
|
|
newest time.Time
|
|
}
|
|
|
|
func newStamper() *stamper { return &stamper{h: sha256.New()} }
|
|
|
|
func (s *stamper) add(rel string, fi fs.FileInfo) {
|
|
fmt.Fprintf(s.h, "%s\x00%d\x00%d\x00%d\n", filepath.ToSlash(rel), fi.Mode().Type(), fi.Size(), fi.ModTime().UnixNano())
|
|
if fi.ModTime().After(s.newest) {
|
|
s.newest = fi.ModTime()
|
|
}
|
|
}
|
|
|
|
// done is the stamp, and whether nothing stamped was written in the last
|
|
// settleFor.
|
|
func (s *stamper) done() (stamp string, settled bool) {
|
|
return hex.EncodeToString(s.h.Sum(nil)), time.Since(s.newest) > settleFor
|
|
}
|
|
|
|
type hashMemo struct{ stamp, hash string }
|
|
|
|
type treeMemo struct {
|
|
a, b string
|
|
same bool
|
|
}
|
|
|
|
var memo = struct {
|
|
sync.Mutex
|
|
hashes map[string]hashMemo // dir and what is left aside: the folder's hash
|
|
trees map[[2]string]treeMemo // two folders: whether they hold the same
|
|
}{hashes: map[string]hashMemo{}, trees: map[[2]string]treeMemo{}}
|
|
|
|
// rememberedHash is the hash of dir leaving lv aside: the hash found
|
|
// before, while a walk finds dir as it was then, else read. A folder
|
|
// hashed for the first time is walked once, as before the memo.
|
|
func rememberedHash(dir string, lv leave) string {
|
|
key := fmt.Sprintf("%d\x00%s", lv, dir)
|
|
memo.Lock()
|
|
m, hit := memo.hashes[key]
|
|
memo.Unlock()
|
|
if hit {
|
|
if _, stamp, _, ok := hashWalk(dir, lv, false); ok && stamp == m.stamp {
|
|
return m.hash
|
|
}
|
|
memo.Lock()
|
|
delete(memo.hashes, key)
|
|
memo.Unlock()
|
|
}
|
|
h, stamp, settled, ok := hashWalk(dir, lv, true)
|
|
if !ok {
|
|
return ""
|
|
}
|
|
if settled {
|
|
memo.Lock()
|
|
memo.hashes[key] = hashMemo{stamp: stamp, hash: h}
|
|
memo.Unlock()
|
|
}
|
|
return h
|
|
}
|
|
|
|
// rememberedSame is whether trees a and b hold the same, as found before
|
|
// while both are as they were then, else compare. Only an answer from
|
|
// reading both whole (sure) is kept.
|
|
func rememberedSame(a, b string, ta, tb tree, compare func() (same, sure bool)) bool {
|
|
key := [2]string{a, b}
|
|
memo.Lock()
|
|
m, hit := memo.trees[key]
|
|
memo.Unlock()
|
|
if hit && m.a == ta.stamp && m.b == tb.stamp {
|
|
return m.same
|
|
}
|
|
same, sure := compare()
|
|
memo.Lock()
|
|
if sure && ta.settled && tb.settled {
|
|
memo.trees[key] = treeMemo{a: ta.stamp, b: tb.stamp, same: same}
|
|
} else {
|
|
delete(memo.trees, key)
|
|
}
|
|
memo.Unlock()
|
|
return same
|
|
}
|