Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186package entrypoint
import ( "crypto/sha256" "encoding/hex" "errors" "io" "io/fs" "os" "path/filepath" "sort" "strings" "syscall"
"albedo/cli/internal/daemon")
// selectedBuild describes the daemon executable this CLI would launch. The// label comes from ALBEDO_BUILD, or from the resolved ALBEDO_DAEMON path; the// digest hashes the candidate's OTP application directory when its layout is// recognizable (see appDir). A part that cannot be determined stays empty.func selectedBuild(projectRoot string) daemon.BuildIdentity { identity := daemon.BuildIdentity{Build: os.Getenv("ALBEDO_BUILD")} candidate := os.Getenv("ALBEDO_DAEMON") if identity.Build == "" && candidate != "" { identity.Build, _ = filepath.EvalSymlinks(candidate) } if candidate == "" && projectRoot != "" { candidate = filepath.Join(projectRoot, "apps", "albedo", "priv", "bin", "albedo-daemon") } if resolved, err := filepath.EvalSymlinks(candidate); err == nil { if appDir := appDir(resolved); appDir != "" { identity.Digest = digestTree(appDir) } } return identity}
// appDir derives the OTP application directory (holding ebin and priv) that a// daemon executable runs from. Three layouts are recognized: a packaged// wrapper at <package>/bin/albedo-daemon with its code at// <package>/lib/albedo/albedo, a bootstrap inside an application directory at// <app>/priv/bin/albedo-daemon, and a source checkout bootstrap at// <root>/apps/albedo/priv/bin/albedo-daemon whose code gleam builds under// <root>/apps/albedo/build/dev/erlang/albedo. Anything else, such as a test snapshot// launcher, stays unrecognized and compares by label alone.func appDir(executable string) string { packaged := filepath.Join(filepath.Dir(executable), "..", "lib", "albedo", "albedo") if hasEbin(packaged) { return packaged } if filepath.Base(executable) != "albedo-daemon" { return "" } bin := filepath.Dir(executable) if filepath.Base(bin) != "bin" || filepath.Base(filepath.Dir(bin)) != "priv" { return "" } root := filepath.Dir(filepath.Dir(bin)) if hasEbin(root) { return root } dev := filepath.Join(root, "build", "dev", "erlang", "albedo") if hasEbin(dev) { return dev } return ""}
func hasEbin(dir string) bool { info, err := os.Stat(filepath.Join(dir, "ebin")) return err == nil && info.IsDir()}
// digestTree reads a sealed build identity or hashes a mutable tree the way// the daemon hashes its own running tree (build_digest in// apps/albedo/src/albedo/lifecycle/build_identity.gleam): sha256 over the sorted relative// paths and bytes of every regular file under each sibling app's ebin/ and priv/, symlinks// followed. An unreadable path fails the whole digest, the call the daemon's// walker makes too, and the shared fixture in apps/albedo/test/fixtures/build-digest pins// both implementations to one constant.func digestTree(appDir string) string { if file, err := os.Open(filepath.Join(appDir, ".build-digest-v2")); err == nil { data, err := io.ReadAll(io.LimitReader(file, 66)) file.Close() if err == nil && len(data) == 65 && data[64] == '\n' { digest := string(data[:64]) if decoded, err := hex.DecodeString(digest); err == nil && len(decoded) == sha256.Size && digest == strings.ToLower(digest) { return digest } } } if !hasEbin(appDir) { return "" } output := filepath.Dir(appDir) entries, err := os.ReadDir(output) if err != nil { return "" } var paths []string applications := 0 for _, entry := range entries { app := filepath.Join(output, entry.Name()) info, err := os.Stat(filepath.Join(app, "ebin")) if errors.Is(err, fs.ErrNotExist) || errors.Is(err, syscall.ENOTDIR) { continue } if err != nil { return "" } if !info.IsDir() { continue } applications++ for _, sub := range []string{"ebin", "priv"} { root := filepath.Join(app, sub) info, err := os.Stat(root) if errors.Is(err, fs.ErrNotExist) { continue } if err != nil { return "" } if !info.IsDir() { continue } paths, err = appendTreePaths(paths, root, entry.Name()+"/"+sub, 0) if err != nil { return "" } } } if applications == 0 { return "" } sort.Strings(paths) hasher := sha256.New() // One buffer for every file: io.Copy from an *os.File allocates its own // for each, and a build tree has hundreds. buf := make([]byte, 32*1024) for _, rel := range paths { hasher.Write([]byte(rel)) file, err := os.Open(filepath.Join(output, rel)) if err != nil { return "" } _, err = io.CopyBuffer(hasher, struct{ io.Reader }{file}, buf) file.Close() if err != nil { return "" } } return hex.EncodeToString(hasher.Sum(nil))}
func appendTreePaths(paths []string, dir, rel string, depth int) ([]string, error) { if depth > 256 { return nil, errors.New("build tree exceeds 256 directory levels") } entries, err := os.ReadDir(dir) if err != nil { return nil, err } for _, entry := range entries { childRel := rel + "/" + entry.Name() if childRel == "albedo/priv/python/.venv" { continue } child := filepath.Join(dir, entry.Name()) info, err := os.Stat(child) // follows symlinks, like the daemon's walker if err != nil { return nil, err } switch { case info.IsDir(): if paths, err = appendTreePaths(paths, child, childRel, depth+1); err != nil { return nil, err } case info.Mode().IsRegular(): paths = append(paths, childRel) } } return paths, nil}