Playground
gnomod.toml
module = "gno.land/r/g1csu73nv8c38a8xru5vk6wgq0ltnpvzd79a6udn/gnosage" gno = "0.9" [addpkg] creator = "g1csu73nv8c38a8xru5vk6wgq0ltnpvzd79a6udn" height = 1344772 max_deposit = "100000000ugnot"
gnosage.gno
// Package gnosage is the on-chain review board for GnoSage, an autonomous
// open-source AI agent that reviews Gno realms and posts scored verdicts
// on-chain.
//
// Anyone can post a review. Reviews from registered agent keys are marked
// as agent reviews and are the ones the board surfaces as the headline
// verdict for each realm. Human reviews are kept alongside them.
//
// A review scores a realm 0-100 overall and in four categories (docs,
// tests, safety, code quality), carries a short verdict and a compact
// machine-readable check list, and is versioned per realm path so a
// re-review after a redeploy never overwrites history.
package gnosage
import (
"chain"
"errors"
"strings"
"time"
"gno.land/p/nt/avl/v0"
"gno.land/p/nt/ownable/v0"
"gno.land/p/nt/ufmt/v0"
)
// Limits keep review payloads small: storage on Gno.land costs a
// refundable deposit, and a review board must stay cheap to write.
const (
maxPathLength = 256
maxVerdictLength = 1000
maxChecksLength = 2000
)
const (
// ReviewPostedEvent is emitted on every accepted review.
ReviewPostedEvent = "ReviewPosted"
// ReviewerRegisteredEvent is emitted when the owner registers a reviewer.
ReviewerRegisteredEvent = "ReviewerRegistered"
// ReviewerRemovedEvent is emitted when the owner removes a reviewer.
ReviewerRemovedEvent = "ReviewerRemoved"
)
var (
ErrEmptyPath = errors.New("pkgPath must not be empty")
ErrPathTooLong = errors.New("pkgPath is too long")
ErrBadPath = errors.New("pkgPath must start with gno.land/")
ErrScoreOutOfRange = errors.New("scores must be between 0 and 100")
ErrEmptyVerdict = errors.New("verdict must not be empty")
ErrVerdictTooLong = errors.New("verdict is too long")
ErrChecksTooLong = errors.New("checks list is too long")
ErrNotOwner = errors.New("caller is not the owner")
ErrReviewNotFound = errors.New("review not found")
)
// Review is one scored verdict for one realm path at one version.
type Review struct {
ID uint64 // global sequence number across all reviews
PkgPath string // reviewed realm path, e.g. gno.land/r/<addr>/foo
Version uint64 // review version for this path, starting at 1
Reviewer address // who posted it
Agent bool // true when the reviewer is a registered agent key
Score int // 0-100 overall
Docs int // 0-100 documentation
Tests int // 0-100 tests
Safety int // 0-100 safety
Quality int // 0-100 code quality
Verdict string // short human-readable summary
Checks string // compact check list, "name:result;name:result"
CreatedAt time.Time // block time of the review
}
var (
// Ownable gates reviewer registration to the deployer.
Ownable *ownable.Ownable
// reviews holds every review, keyed by "pkgPath#%020d" (version).
reviews *avl.Tree
// versions holds the review count per path, keyed by pkgPath.
versions *avl.Tree
// latestAgent holds the latest agent review version per path.
latestAgent *avl.Tree
// reviewers holds registered agent reviewer addresses, keyed by address.
reviewers *avl.Tree
// reviewCount is the total number of reviews posted.
reviewCount uint64
)
func init(cur realm) {
reviews = avl.NewTree()
versions = avl.NewTree()
latestAgent = avl.NewTree()
reviewers = avl.NewTree()
reviewCount = 0
// At real deploy time the previous realm is the deploying user, who
// becomes the owner. Under `gno test` there is no user caller, so the
// owner stays unset and tests set it explicitly.
if cur.Previous().IsUserCall() {
Ownable = ownable.NewWithAddress(cur.Previous().Address())
}
}
// reviewKey builds the storage key for one version of one path. The
// zero-padded version keeps lexicographic order equal to version order,
// so range scans over a path return reviews oldest-first.
func reviewKey(pkgPath string, version uint64) string {
return pkgPath + "#" + ufmt.Sprintf("%020d", version)
}
// assertOwner panics unless the caller is the realm owner.
func assertOwner(cur realm) {
if !Ownable.OwnedBy(cur.Previous().Address()) {
panic(ErrNotOwner)
}
}
// RegisterReviewer marks an address as a trusted agent reviewer. Only
// the owner can register reviewers. Reviews from registered reviewers
// are the board's headline verdicts.
func RegisterReviewer(cur realm, reviewer address) {
assertOwner(cur)
reviewers.Set(reviewer.String(), true)
chain.Emit(ReviewerRegisteredEvent,
"reviewer", reviewer.String(),
)
}
// UnregisterReviewer removes a registered agent reviewer. Only the owner.
func UnregisterReviewer(cur realm, reviewer address) {
assertOwner(cur)
reviewers.Remove(reviewer.String())
chain.Emit(ReviewerRemovedEvent,
"reviewer", reviewer.String(),
)
}
// IsReviewer reports whether an address is a registered agent reviewer.
func IsReviewer(reviewer address) bool {
v, ok := reviewers.Get(reviewer.String()).(bool)
return ok && v
}
// PostReview records a scored verdict for a realm path. Any caller may
// post; the review is marked as an agent review only when the caller is
// a registered agent reviewer. Returns the new version for the path.
func PostReview(cur realm, pkgPath string, score, docs, tests, safety, quality int, verdict, checks string) (uint64, error) {
if err := validateReview(pkgPath, score, docs, tests, safety, quality, verdict, checks); err != nil {
return 0, err
}
caller := cur.Previous().Address()
agent := IsReviewer(caller)
version := uint64(1)
if v, ok := versions.Get(pkgPath).(int); ok {
version = uint64(v) + 1
}
versions.Set(pkgPath, int(version))
reviewCount++
review := &Review{
ID: reviewCount,
PkgPath: pkgPath,
Version: version,
Reviewer: caller,
Agent: agent,
Score: score,
Docs: docs,
Tests: tests,
Safety: safety,
Quality: quality,
Verdict: verdict,
Checks: checks,
CreatedAt: time.Now(),
}
reviews.Set(reviewKey(pkgPath, version), review)
if agent {
latestAgent.Set(pkgPath, int(version))
}
chain.Emit(ReviewPostedEvent,
"pkg", pkgPath,
"version", ufmt.Sprintf("%d", version),
"reviewer", caller.String(),
"agent", ufmt.Sprintf("%t", agent),
"score", ufmt.Sprintf("%d", score),
)
return version, nil
}
// validateReview checks every field of a review before it is stored.
func validateReview(pkgPath string, score, docs, tests, safety, quality int, verdict, checks string) error {
if strings.TrimSpace(pkgPath) == "" {
return ErrEmptyPath
}
if len(pkgPath) > maxPathLength {
return ErrPathTooLong
}
if !strings.HasPrefix(pkgPath, "gno.land/") {
return ErrBadPath
}
for _, s := range []int{score, docs, tests, safety, quality} {
if s < 0 || s > 100 {
return ErrScoreOutOfRange
}
}
if strings.TrimSpace(verdict) == "" {
return ErrEmptyVerdict
}
if len(verdict) > maxVerdictLength {
return ErrVerdictTooLong
}
if len(checks) > maxChecksLength {
return ErrChecksTooLong
}
return nil
}
// GetReview returns one review by path and version, or nil.
func GetReview(pkgPath string, version uint64) *Review {
v, ok := reviews.Get(reviewKey(pkgPath, version)).(*Review)
if !ok {
return nil
}
return v
}
// LatestAgentReview returns the newest agent review for a path, or nil
// when no agent has reviewed it yet.
func LatestAgentReview(pkgPath string) *Review {
v, ok := latestAgent.Get(pkgPath).(int)
if !ok {
return nil
}
return GetReview(pkgPath, uint64(v))
}
// VersionCount returns how many reviews a path has received.
func VersionCount(pkgPath string) int {
v, ok := versions.Get(pkgPath).(int)
if !ok {
return 0
}
return v
}
// ReviewCount returns the total number of reviews on the board.
func ReviewCount() uint64 {
return reviewCount
}
// ReviewedPaths returns every path that has at least one review, in
// lexicographic order.
func ReviewedPaths() []string {
paths := []string{}
versions.Iterate("", "", func(key string, _ any) bool {
paths = append(paths, key)
return false
})
return paths
}
render.gno
package gnosage
import (
"strings"
"gno.land/p/nt/ufmt/v0"
)
// Render serves the review board through gnoweb.
//
// - Render("") lists every reviewed realm with its latest agent score.
// - Render("<pkgPath>") shows the review history of one realm, e.g.
// Render("gno.land/r/<addr>/foo").
//
// Caller-controlled text (verdicts) is flattened before rendering so a
// review can never break the board's markdown layout or smuggle links.
func Render(path string) string {
if path != "" && VersionCount(path) > 0 {
return renderDetail(path)
}
return renderIndex()
}
// renderIndex lists reviewed realms with their latest agent verdict.
func renderIndex() string {
var b strings.Builder
b.WriteString("# GnoSage review board\n\n")
b.WriteString("Autonomous AI reviews of Gno realms. Scores are 0-100; the headline score is the latest review by a registered agent reviewer.\n\n")
paths := ReviewedPaths()
if len(paths) == 0 {
b.WriteString("No reviews yet. The agent posts its first verdicts shortly after deploy.\n")
return b.String()
}
b.WriteString("| Realm | Score | Docs | Tests | Safety | Quality | Reviews |\n")
b.WriteString("|---|---|---|---|---|---|---|\n")
for _, p := range paths {
latest := LatestAgentReview(p)
if latest == nil {
b.WriteString(ufmt.Sprintf("| %s | — | — | — | — | — | %d |\n", p, VersionCount(p)))
continue
}
b.WriteString(ufmt.Sprintf("| [%s](%s) | %d | %d | %d | %d | %d | %d |\n",
p, p, latest.Score, latest.Docs, latest.Tests, latest.Safety, latest.Quality, VersionCount(p)))
}
b.WriteString(ufmt.Sprintf("\n%d review(s) on the board.\n", ReviewCount()))
return b.String()
}
// renderDetail shows every review of one realm, newest first.
func renderDetail(pkgPath string) string {
var b strings.Builder
b.WriteString(ufmt.Sprintf("# %s\n\n", pkgPath))
latest := LatestAgentReview(pkgPath)
if latest != nil {
b.WriteString(ufmt.Sprintf("Latest agent verdict (v%d, score %d/100): %s\n\n",
latest.Version, latest.Score, flatten(latest.Verdict)))
}
b.WriteString("## Reviews\n\n")
count := VersionCount(pkgPath)
for v := count; v >= 1; v-- {
r := GetReview(pkgPath, uint64(v))
if r == nil {
continue
}
kind := "human"
if r.Agent {
kind = "agent"
}
b.WriteString(ufmt.Sprintf("### v%d — %d/100 (%s, %s)\n\n", r.Version, r.Score, kind, r.Reviewer.String()))
b.WriteString(ufmt.Sprintf("Docs %d · Tests %d · Safety %d · Quality %d\n\n", r.Docs, r.Tests, r.Safety, r.Quality))
b.WriteString(flatten(r.Verdict) + "\n\n")
if r.Checks != "" {
b.WriteString("Checks: `" + flatten(r.Checks) + "`\n\n")
}
}
return b.String()
}
// flatten removes markdown-breaking characters from caller text.
func flatten(s string) string {
s = strings.ReplaceAll(s, "\n", " ")
s = strings.ReplaceAll(s, "\r", " ")
s = strings.ReplaceAll(s, "|", "/")
s = strings.ReplaceAll(s, "[", "(")
s = strings.ReplaceAll(s, "]", ")")
return s
}