| 1 | // Package opencode is the OpenCode harness (connectors spec 2026-11-12, |
| 2 | // §3 and §4.2, slice S5): the pinned OpenCode v2 package and its |
| 3 | // platform binary, the isolated configuration, the Agent Client Protocol |
| 4 | // client that drives `opencode acp` over the namespace's stdio, and the |
| 5 | // decoder that turns its updates into Equipo's kinds. |
| 6 | package opencode |
| 7 | |
| 8 | import ( |
| 9 | "embed" |
| 10 | "encoding/json" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 13 | ) |
| 14 | |
| 15 | // The pinned OpenCode (connectors spec §4.2): npm's @opencode/cli v2 with |
| 16 | // the integrity npm published for it. Its binary ships in a per-platform |
| 17 | // package (an optional dependency its postinstall script would copy into |
| 18 | // place); the installer fetches this platform's package itself, checked |
| 19 | // against the committed lockfile's integrity, and runs no script. |
| 20 | const ( |
| 21 | PackageName = "@opencode/cli" |
| 22 | Version = "2.2.7" |
| 23 | Integrity = "sha512-qsSExDfpUCqhGXBqZ8xhjg2OG9DxnsnfWl1/7vGINRWw7sxZvK9cXeW6MI8j9e+GItaYtg94zH3Eo2ng86VLCg==" |
| 24 | ) |
| 25 | |
| 26 | // Platform is one supported platform's binary package: its name in the |
| 27 | // lockfile and the SHA-256 of its bin/opencode. |
| 28 | type Platform struct { |
| 29 | Package, BinarySHA256 string |
| 30 | } |
| 31 | |
| 32 | // platforms are the glibc builds for Linux (the runner is amd64, CI's |
| 33 | // boxes arm64). The x64 build needs AVX2; the baseline and musl builds |
| 34 | // are not supported. |
| 35 | var platforms = map[string]Platform{ |
| 36 | "amd64": {Package: "@opencode/cli-linux-x64", BinarySHA256: "589253d36810f21217edd51545cd435b328cdc3cf4f451fbe0cce6f88afcf5e1"}, |
| 37 | "arm64": {Package: "@opencode/cli-linux-arm64", BinarySHA256: "c577783eacf7d1e66661fc7fd904b7646761497b687ff8f8f82ebe4f2f3ea425"}, |
| 38 | } |
| 39 | |
| 40 | // treeSHA256 is the committed digest of the installed node_modules tree |
| 41 | // (provider.TreeSHA256 after the seal), per platform: @opencode/cli and |
| 42 | // this platform's binary package, nothing else. |
| 43 | var treeSHA256 = map[string]string{ |
| 44 | "amd64": "0a36384b35ce78bda86ee1cf5e5e8d67e46639a2dc610d316648bf0fc6f5ba08", |
| 45 | "arm64": "921289af48f7e6a5b6764a20ae35d6a3d42a39a70c2bd0755089088b6bbc1c03", |
| 46 | } |
| 47 | |
| 48 | // Pins are the committed tree digest and the binary package for goarch. |
| 49 | // ok is false where OpenCode is not supported. |
| 50 | func Pins(goarch string) (tree string, platform Platform, ok bool) { |
| 51 | platform, ok = platforms[goarch] |
| 52 | if !ok || treeSHA256[goarch] == "" { |
| 53 | return "", Platform{}, false |
| 54 | } |
| 55 | return treeSHA256[goarch], platform, true |
| 56 | } |
| 57 | |
| 58 | //go:embed npm/package.json npm/package-lock.json |
| 59 | var npmFiles embed.FS |
| 60 | |
| 61 | // Package is the committed package.json and package-lock.json. |
| 62 | func Package() (pkg, lock []byte) { |
| 63 | pkg, _ = npmFiles.ReadFile("npm/package.json") |
| 64 | lock, _ = npmFiles.ReadFile("npm/package-lock.json") |
| 65 | return pkg, lock |
| 66 | } |
| 67 | |
| 68 | // Locked is one lockfile entry: where npm resolved it and its integrity. |
| 69 | type Locked struct { |
| 70 | Version, Resolved, Integrity string |
| 71 | } |
| 72 | |
| 73 | // lockfile reads the committed lockfile's packages. |
| 74 | func lockfile() map[string]Locked { |
| 75 | _, lock := Package() |
| 76 | var l struct { |
| 77 | Packages map[string]Locked `json:"packages"` |
| 78 | } |
| 79 | if json.Unmarshal(lock, &l) != nil { |
| 80 | return nil |
| 81 | } |
| 82 | return l.Packages |
| 83 | } |
| 84 | |
| 85 | // Lock is a package's entry in the committed lockfile. |
| 86 | func Lock(name string) (Locked, bool) { |
| 87 | p, ok := lockfile()["node_modules/"+name] |
| 88 | return p, ok && p.Version == Version && p.Resolved != "" && len(p.Integrity) > len("sha512-") |
| 89 | } |
| 90 | |
| 91 | // Pinned reports whether the lockfile pins OpenCode at Version with |
| 92 | // Integrity. |
| 93 | func Pinned() bool { |
| 94 | p, ok := Lock(PackageName) |
| 95 | return ok && p.Integrity == Integrity |
| 96 | } |
| 97 | |
| 98 | // ErrUnpinned: the tree or the binary is not where the install put it, |
| 99 | // is writable, or does not match its pin. |
| 100 | var ErrUnpinned = provider.ErrTree |
| 101 | |