| 1 | // Package pi is the Pi harness (connectors spec 2026-11-12, §3 and §4.1, |
| 2 | // slice S2): the pinned Pi package tree and Node, Equipo's own Pi |
| 3 | // extension, the isolated configuration, the RPC client and the decoder |
| 4 | // that turns Pi's events into Equipo's kinds. |
| 5 | package pi |
| 6 | |
| 7 | import ( |
| 8 | "embed" |
| 9 | "encoding/json" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 12 | ) |
| 13 | |
| 14 | // The pinned Pi (connectors spec §4.1): npm's @earendil-works package |
| 15 | // (the @mariozechner one is deprecated), with the integrity npm |
| 16 | // published for it. The lockfile pins every package it brings. |
| 17 | const ( |
| 18 | PackageName = "@earendil-works/pi-coding-agent" |
| 19 | Version = "1.5.2" |
| 20 | Integrity = "sha512-mUWI04rqF1Vd/m1q+Ps1XeWsTXEVvRUnsmRa83I3Dzua0Tx5bYS93imeqtIfrXf7PkkaR6ERz+AuQhIJlom5yA==" |
| 21 | // NodeVersion is the pinned Node Pi runs on (Pi needs 22.19 or |
| 22 | // later); the Mac runner's srt runs on the same release. |
| 23 | NodeVersion = "v22.23.4" |
| 24 | // CLI is Pi's entry point inside the tree (package.json bin "pi"). |
| 25 | CLI = "@earendil-works/pi-coding-agent/dist/bundle/cli.js" |
| 26 | ) |
| 27 | |
| 28 | // NodeArchive is the official Node release archive for one platform: |
| 29 | // the archive's SHA-256 as nodejs.org publishes it (SHASUMS256.txt), and |
| 30 | // the node program's own SHA-256 inside it. |
| 31 | type NodeArchive struct { |
| 32 | Version, URL, SHA256, Root, BinarySHA256 string |
| 33 | } |
| 34 | |
| 35 | // treeSHA256 is the committed digest of the installed node_modules tree |
| 36 | // (TreeSHA256 after Seal), per platform. Pi 1.5.2's tree has no |
| 37 | // platform-specific package (its native terminal modules ship every |
| 38 | // platform's build), so linux/amd64 (the runner) and linux/arm64 (CI) |
| 39 | // install the same tree. |
| 40 | var treeSHA256 = map[string]string{ |
| 41 | "amd64": "d0ce14faa1c7442ee2cc5289b350f7a56b2bddbdf7d7c1675331de37c6cbc534", |
| 42 | "arm64": "d0ce14faa1c7442ee2cc5289b350f7a56b2bddbdf7d7c1675331de37c6cbc534", |
| 43 | } |
| 44 | |
| 45 | // nodeArchives are Node v22.23.4 for Linux. |
| 46 | var nodeArchives = map[string]NodeArchive{ |
| 47 | "amd64": {Version: NodeVersion, URL: "https://nodejs.org/dist/v22.23.4/node-v22.23.4-linux-x64.tar.gz", |
| 48 | SHA256: "b8492284c76a54baf58d61668dd0e41ee0fc98adabf9503d30f05d08f55b4406", Root: "node-v22.23.4-linux-x64", |
| 49 | BinarySHA256: "94efd062ed066c1a0b332e18249859c905e920eeada5df55d24c99b919629a90"}, |
| 50 | "arm64": {Version: NodeVersion, URL: "https://nodejs.org/dist/v22.23.4/node-v22.23.4-linux-arm64.tar.gz", |
| 51 | SHA256: "f7a1e486dbc8b286847815b477d4fb5e9fb963ed4489c3e1588853d685f589f9", Root: "node-v22.23.4-linux-arm64", |
| 52 | BinarySHA256: "7e48183361053f039294fbbc3ba586722fe756bf296d6cb8195356d10819cd16"}, |
| 53 | } |
| 54 | |
| 55 | // Pins are the committed tree digest and the Node archive for goarch. |
| 56 | // ok is false where Pi is not supported. |
| 57 | func Pins(goarch string) (tree string, node NodeArchive, ok bool) { |
| 58 | tree, ok = treeSHA256[goarch] |
| 59 | if !ok { |
| 60 | return "", NodeArchive{}, false |
| 61 | } |
| 62 | return tree, nodeArchives[goarch], true |
| 63 | } |
| 64 | |
| 65 | //go:embed npm/package.json npm/package-lock.json |
| 66 | var npmFiles embed.FS |
| 67 | |
| 68 | // Package is the package.json and package-lock.json an install writes |
| 69 | // into the Pi prefix before `npm ci --ignore-scripts`. |
| 70 | func Package() (pkg, lock []byte) { |
| 71 | pkg, _ = npmFiles.ReadFile("npm/package.json") |
| 72 | lock, _ = npmFiles.ReadFile("npm/package-lock.json") |
| 73 | return pkg, lock |
| 74 | } |
| 75 | |
| 76 | // Pinned reports whether the lockfile pins Pi at Version with Integrity. |
| 77 | func Pinned() bool { |
| 78 | _, lock := Package() |
| 79 | var l struct { |
| 80 | Packages map[string]struct{ Version, Integrity string } `json:"packages"` |
| 81 | } |
| 82 | if json.Unmarshal(lock, &l) != nil { |
| 83 | return false |
| 84 | } |
| 85 | p := l.Packages["node_modules/"+PackageName] |
| 86 | return p.Version == Version && p.Integrity == Integrity |
| 87 | } |
| 88 | |
| 89 | // ErrUnpinned: the Pi tree or Node is not where the install put it, is |
| 90 | // writable, or does not match its pin. |
| 91 | var ErrUnpinned = provider.ErrTree |
| 92 | |
| 93 | // Seal makes an installed tree read-only (provider.SealTree). |
| 94 | func Seal(dir string) error { return provider.SealTree(dir) } |
| 95 | |
| 96 | // TreeSHA256 names an installed tree (provider.TreeSHA256). |
| 97 | func TreeSHA256(dir string) (string, error) { return provider.TreeSHA256(dir) } |
| 98 | |