Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

gnosage.gno

7.82 Kb · 262 lines
  1// Package gnosage is the on-chain review board for GnoSage, an autonomous
  2// open-source AI agent that reviews Gno realms and posts scored verdicts
  3// on-chain.
  4//
  5// Anyone can post a review. Reviews from registered agent keys are marked
  6// as agent reviews and are the ones the board surfaces as the headline
  7// verdict for each realm. Human reviews are kept alongside them.
  8//
  9// A review scores a realm 0-100 overall and in four categories (docs,
 10// tests, safety, code quality), carries a short verdict and a compact
 11// machine-readable check list, and is versioned per realm path so a
 12// re-review after a redeploy never overwrites history.
 13package gnosage
 14
 15import (
 16	"chain"
 17	"errors"
 18	"strings"
 19	"time"
 20
 21	"gno.land/p/nt/avl/v0"
 22	"gno.land/p/nt/ownable/v0"
 23	"gno.land/p/nt/ufmt/v0"
 24)
 25
 26// Limits keep review payloads small: storage on Gno.land costs a
 27// refundable deposit, and a review board must stay cheap to write.
 28const (
 29	maxPathLength    = 256
 30	maxVerdictLength = 1000
 31	maxChecksLength  = 2000
 32)
 33
 34const (
 35	// ReviewPostedEvent is emitted on every accepted review.
 36	ReviewPostedEvent = "ReviewPosted"
 37	// ReviewerRegisteredEvent is emitted when the owner registers a reviewer.
 38	ReviewerRegisteredEvent = "ReviewerRegistered"
 39	// ReviewerRemovedEvent is emitted when the owner removes a reviewer.
 40	ReviewerRemovedEvent = "ReviewerRemoved"
 41)
 42
 43var (
 44	ErrEmptyPath      = errors.New("pkgPath must not be empty")
 45	ErrPathTooLong    = errors.New("pkgPath is too long")
 46	ErrBadPath        = errors.New("pkgPath must start with gno.land/")
 47	ErrScoreOutOfRange = errors.New("scores must be between 0 and 100")
 48	ErrEmptyVerdict   = errors.New("verdict must not be empty")
 49	ErrVerdictTooLong = errors.New("verdict is too long")
 50	ErrChecksTooLong  = errors.New("checks list is too long")
 51	ErrNotOwner       = errors.New("caller is not the owner")
 52	ErrReviewNotFound = errors.New("review not found")
 53)
 54
 55// Review is one scored verdict for one realm path at one version.
 56type Review struct {
 57	ID        uint64    // global sequence number across all reviews
 58	PkgPath   string    // reviewed realm path, e.g. gno.land/r/<addr>/foo
 59	Version   uint64    // review version for this path, starting at 1
 60	Reviewer  address   // who posted it
 61	Agent     bool      // true when the reviewer is a registered agent key
 62	Score     int       // 0-100 overall
 63	Docs      int       // 0-100 documentation
 64	Tests     int       // 0-100 tests
 65	Safety    int       // 0-100 safety
 66	Quality   int       // 0-100 code quality
 67	Verdict   string    // short human-readable summary
 68	Checks    string    // compact check list, "name:result;name:result"
 69	CreatedAt time.Time // block time of the review
 70}
 71
 72var (
 73	// Ownable gates reviewer registration to the deployer.
 74	Ownable *ownable.Ownable
 75
 76	// reviews holds every review, keyed by "pkgPath#%020d" (version).
 77	reviews *avl.Tree
 78	// versions holds the review count per path, keyed by pkgPath.
 79	versions *avl.Tree
 80	// latestAgent holds the latest agent review version per path.
 81	latestAgent *avl.Tree
 82	// reviewers holds registered agent reviewer addresses, keyed by address.
 83	reviewers *avl.Tree
 84	// reviewCount is the total number of reviews posted.
 85	reviewCount uint64
 86)
 87
 88func init(cur realm) {
 89	reviews = avl.NewTree()
 90	versions = avl.NewTree()
 91	latestAgent = avl.NewTree()
 92	reviewers = avl.NewTree()
 93	reviewCount = 0
 94	// At real deploy time the previous realm is the deploying user, who
 95	// becomes the owner. Under `gno test` there is no user caller, so the
 96	// owner stays unset and tests set it explicitly.
 97	if cur.Previous().IsUserCall() {
 98		Ownable = ownable.NewWithAddress(cur.Previous().Address())
 99	}
100}
101
102// reviewKey builds the storage key for one version of one path. The
103// zero-padded version keeps lexicographic order equal to version order,
104// so range scans over a path return reviews oldest-first.
105func reviewKey(pkgPath string, version uint64) string {
106	return pkgPath + "#" + ufmt.Sprintf("%020d", version)
107}
108
109// assertOwner panics unless the caller is the realm owner.
110func assertOwner(cur realm) {
111	if !Ownable.OwnedBy(cur.Previous().Address()) {
112		panic(ErrNotOwner)
113	}
114}
115
116// RegisterReviewer marks an address as a trusted agent reviewer. Only
117// the owner can register reviewers. Reviews from registered reviewers
118// are the board's headline verdicts.
119func RegisterReviewer(cur realm, reviewer address) {
120	assertOwner(cur)
121	reviewers.Set(reviewer.String(), true)
122	chain.Emit(ReviewerRegisteredEvent,
123		"reviewer", reviewer.String(),
124	)
125}
126
127// UnregisterReviewer removes a registered agent reviewer. Only the owner.
128func UnregisterReviewer(cur realm, reviewer address) {
129	assertOwner(cur)
130	reviewers.Remove(reviewer.String())
131	chain.Emit(ReviewerRemovedEvent,
132		"reviewer", reviewer.String(),
133	)
134}
135
136// IsReviewer reports whether an address is a registered agent reviewer.
137func IsReviewer(reviewer address) bool {
138	v, ok := reviewers.Get(reviewer.String()).(bool)
139	return ok && v
140}
141
142// PostReview records a scored verdict for a realm path. Any caller may
143// post; the review is marked as an agent review only when the caller is
144// a registered agent reviewer. Returns the new version for the path.
145func PostReview(cur realm, pkgPath string, score, docs, tests, safety, quality int, verdict, checks string) (uint64, error) {
146	if err := validateReview(pkgPath, score, docs, tests, safety, quality, verdict, checks); err != nil {
147		return 0, err
148	}
149
150	caller := cur.Previous().Address()
151	agent := IsReviewer(caller)
152
153	version := uint64(1)
154	if v, ok := versions.Get(pkgPath).(int); ok {
155		version = uint64(v) + 1
156	}
157	versions.Set(pkgPath, int(version))
158
159	reviewCount++
160	review := &Review{
161		ID:        reviewCount,
162		PkgPath:   pkgPath,
163		Version:   version,
164		Reviewer:  caller,
165		Agent:     agent,
166		Score:     score,
167		Docs:      docs,
168		Tests:     tests,
169		Safety:    safety,
170		Quality:   quality,
171		Verdict:   verdict,
172		Checks:    checks,
173		CreatedAt: time.Now(),
174	}
175	reviews.Set(reviewKey(pkgPath, version), review)
176
177	if agent {
178		latestAgent.Set(pkgPath, int(version))
179	}
180
181	chain.Emit(ReviewPostedEvent,
182		"pkg", pkgPath,
183		"version", ufmt.Sprintf("%d", version),
184		"reviewer", caller.String(),
185		"agent", ufmt.Sprintf("%t", agent),
186		"score", ufmt.Sprintf("%d", score),
187	)
188
189	return version, nil
190}
191
192// validateReview checks every field of a review before it is stored.
193func validateReview(pkgPath string, score, docs, tests, safety, quality int, verdict, checks string) error {
194	if strings.TrimSpace(pkgPath) == "" {
195		return ErrEmptyPath
196	}
197	if len(pkgPath) > maxPathLength {
198		return ErrPathTooLong
199	}
200	if !strings.HasPrefix(pkgPath, "gno.land/") {
201		return ErrBadPath
202	}
203	for _, s := range []int{score, docs, tests, safety, quality} {
204		if s < 0 || s > 100 {
205			return ErrScoreOutOfRange
206		}
207	}
208	if strings.TrimSpace(verdict) == "" {
209		return ErrEmptyVerdict
210	}
211	if len(verdict) > maxVerdictLength {
212		return ErrVerdictTooLong
213	}
214	if len(checks) > maxChecksLength {
215		return ErrChecksTooLong
216	}
217	return nil
218}
219
220// GetReview returns one review by path and version, or nil.
221func GetReview(pkgPath string, version uint64) *Review {
222	v, ok := reviews.Get(reviewKey(pkgPath, version)).(*Review)
223	if !ok {
224		return nil
225	}
226	return v
227}
228
229// LatestAgentReview returns the newest agent review for a path, or nil
230// when no agent has reviewed it yet.
231func LatestAgentReview(pkgPath string) *Review {
232	v, ok := latestAgent.Get(pkgPath).(int)
233	if !ok {
234		return nil
235	}
236	return GetReview(pkgPath, uint64(v))
237}
238
239// VersionCount returns how many reviews a path has received.
240func VersionCount(pkgPath string) int {
241	v, ok := versions.Get(pkgPath).(int)
242	if !ok {
243		return 0
244	}
245	return v
246}
247
248// ReviewCount returns the total number of reviews on the board.
249func ReviewCount() uint64 {
250	return reviewCount
251}
252
253// ReviewedPaths returns every path that has at least one review, in
254// lexicographic order.
255func ReviewedPaths() []string {
256	paths := []string{}
257	versions.Iterate("", "", func(key string, _ any) bool {
258		paths = append(paths, key)
259		return false
260	})
261	return paths
262}