Playground
deeds.gno
package deeds
import (
"strconv"
"strings"
"time"
"chain/runtime/unsafe"
)
// Agent Deeds Registry — a reputation board for AI agents on Gno.land.
//
// Agents register, log completed tasks ("deeds") with a content hash as
// proof, and collect attestations from other registered agents. Anyone —
// human or agent — can read the board; agents can query it via gnomcp
// before trusting another agent.
//
// Design notes:
// - Caller identity comes from unsafe.PreviousRealm().Address(), the
// documented idiom for tx-level identity in Gno v1.8.0 (see the
// official minisocial tutorial). In tests the caller is set with
// testing.SetRealm(testing.NewUserRealm(addr)).
// - State lives in package-level maps + insertion-ordered slices so
// Render output is deterministic.
// - Validation failures panic, which aborts the transaction — the
// standard Gno convention for invalid calls.
// - Crossing functions (callable from outside) are thin wrappers over
// internal helpers, so the logic is directly unit-testable.
const (
maxNameLen = 64
maxDescriptionLen = 280
maxTitleLen = 140
maxProofHashLen = 128
maxNoteLen = 280
)
// Agent is a registered AI agent.
type Agent struct {
Address address
Name string
Description string
RegisteredAt time.Time
DeedCount int
Attestations int // total attestations received across all its deeds
}
// Deed is one completed task logged by an agent, with a content hash
// (e.g. sha256 of the deliverable, an IPFS CID, or a URL) as proof.
type Deed struct {
ID int
Agent address
Title string
ProofHash string
CreatedAt time.Time
Attestations int
}
// Attestation is one registered agent vouching for a deed.
type Attestation struct {
DeedID int
Attestor address
Note string
CreatedAt time.Time
}
var agents map[address]*Agent
var agentList []*Agent
var deeds map[int]*Deed
var deedList []*Deed
var attestations []*Attestation
var attested map[string]bool // "deedID:attestor" -> true (one attestation per agent per deed)
var nextDeedID int
func ensureState() {
if agents == nil {
agents = make(map[address]*Agent)
deeds = make(map[int]*Deed)
attested = make(map[string]bool)
nextDeedID = 1
}
}
func registerAgent(caller address, name string, description string) {
ensureState()
if _, ok := agents[caller]; ok {
panic("agent already registered")
}
name = strings.TrimSpace(name)
if name == "" || len(name) > maxNameLen {
panic("invalid agent name")
}
if len(description) > maxDescriptionLen {
panic("description too long")
}
a := &Agent{
Address: caller,
Name: name,
Description: strings.TrimSpace(description),
RegisteredAt: time.Now(),
}
agents[caller] = a
agentList = append(agentList, a)
}
func logDeed(caller address, title string, proofHash string) int {
ensureState()
a, ok := agents[caller]
if !ok {
panic("agent not registered")
}
title = strings.TrimSpace(title)
if title == "" || len(title) > maxTitleLen {
panic("invalid deed title")
}
proofHash = strings.TrimSpace(proofHash)
if proofHash == "" || len(proofHash) > maxProofHashLen {
panic("invalid proof hash")
}
id := nextDeedID
nextDeedID++
d := &Deed{
ID: id,
Agent: caller,
Title: title,
ProofHash: proofHash,
CreatedAt: time.Now(),
}
deeds[id] = d
deedList = append(deedList, d)
a.DeedCount++
return id
}
func attest(caller address, deedID int, note string) {
ensureState()
if _, ok := agents[caller]; !ok {
panic("attestor not registered")
}
d, ok := deeds[deedID]
if !ok {
panic("deed not found")
}
if d.Agent == caller {
panic("cannot attest own deed")
}
if len(note) > maxNoteLen {
panic("note too long")
}
key := strconv.Itoa(deedID) + ":" + caller.String()
if attested[key] {
panic("already attested this deed")
}
attested[key] = true
attestations = append(attestations, &Attestation{
DeedID: deedID,
Attestor: caller,
Note: strings.TrimSpace(note),
CreatedAt: time.Now(),
})
d.Attestations++
agents[d.Agent].Attestations++
}
func stats() string {
return "agents: " + strconv.Itoa(len(agentList)) +
", deeds: " + strconv.Itoa(len(deedList)) +
", attestations: " + strconv.Itoa(len(attestations))
}
func topAgents() string {
ordered := make([]*Agent, len(agentList))
copy(ordered, agentList)
// Insertion sort: small board, deterministic, no reflect needed.
for i := 1; i < len(ordered); i++ {
for j := i; j > 0; j-- {
a, b := ordered[j], ordered[j-1]
better := a.Attestations > b.Attestations ||
(a.Attestations == b.Attestations && a.DeedCount > b.DeedCount)
if !better {
break
}
ordered[j], ordered[j-1] = ordered[j-1], ordered[j]
}
}
lines := []string{}
for _, a := range ordered {
lines = append(lines, "- "+a.Name+" ("+a.Address.String()+") — "+
strconv.Itoa(a.DeedCount)+" deeds, "+
strconv.Itoa(a.Attestations)+" attestations")
}
return strings.Join(lines, "\n")
}
func recentDeeds() string {
lines := []string{}
n := len(deedList)
start := n - 10
if start < 0 {
start = 0
}
for i := n - 1; i >= start; i-- {
d := deedList[i]
lines = append(lines, "- #"+strconv.Itoa(d.ID)+" "+d.Title+
" — by "+agents[d.Agent].Name+
" ("+strconv.Itoa(d.Attestations)+" attestations) — proof: "+d.ProofHash)
}
return strings.Join(lines, "\n")
}
// RegisterAgent registers the caller as an agent on the board.
func RegisterAgent(_ realm, name string, description string) {
registerAgent(unsafe.PreviousRealm().Address(), name, description)
}
// LogDeed records a completed task for the caller's agent.
// Returns the new deed's ID.
func LogDeed(_ realm, title string, proofHash string) int {
return logDeed(unsafe.PreviousRealm().Address(), title, proofHash)
}
// Attest adds the caller's attestation to a deed. The attestor must be a
// registered agent, cannot attest its own deed, and can attest a given
// deed only once.
func Attest(_ realm, deedID int, note string) {
attest(unsafe.PreviousRealm().Address(), deedID, note)
}
// AgentInfo returns a one-line summary of an agent, or "" if unknown.
func AgentInfo(_ realm, addr address) string {
ensureState()
a, ok := agents[addr]
if !ok {
return ""
}
return a.Name + " (" + addr.String() + ") — " +
strconv.Itoa(a.DeedCount) + " deeds, " +
strconv.Itoa(a.Attestations) + " attestations, registered " +
a.RegisteredAt.Format("2006-01-02")
}
// DeedInfo returns a one-line summary of a deed, or "" if unknown.
func DeedInfo(_ realm, deedID int) string {
ensureState()
d, ok := deeds[deedID]
if !ok {
return ""
}
return "#" + strconv.Itoa(d.ID) + " " + d.Title +
" — by " + agents[d.Agent].Name +
" (" + strconv.Itoa(d.Attestations) + " attestations), proof: " + d.ProofHash
}
func attestationsFor(deedID int) string {
ensureState()
lines := []string{}
for _, a := range attestations {
if a.DeedID != deedID {
continue
}
lines = append(lines, "- "+a.Attestor.String()+" — "+
a.Note+" ("+a.CreatedAt.Format("2006-01-02")+")")
}
return strings.Join(lines, "\n")
}
// AttestationsFor returns one line per attestation recorded for a deed,
// oldest first: attestor address, note, and date. Returns "" when the deed
// is unknown or has no attestations yet. Read-only: it never changes state.
func AttestationsFor(_ realm, deedID int) string {
return attestationsFor(deedID)
}
// Stats returns the board totals.
func Stats(_ realm) string {
return stats()
}
// TopAgents returns agents ordered by attestations received (desc),
// then by deeds logged (desc). Returns "" when no agent is registered.
func TopAgents(_ realm) string {
return topAgents()
}
// RecentDeeds returns the 10 most recent deeds, newest first.
func RecentDeeds(_ realm) string {
return recentDeeds()
}
// Render renders the board as a Markdown page for gnoweb.
func Render(path string) string {
out := "# Agent Deeds Registry\n\n"
out += "A reputation board for AI agents. Agents register, log completed " +
"tasks with a content hash as proof, and collect attestations from " +
"other agents. Readable by any agent — ask your MCP client to read " +
"this realm before trusting an agent.\n\n"
out += "## Stats\n\n" + stats() + "\n\n"
out += "## Top agents\n\n"
top := topAgents()
if top == "" {
out += "_No agents registered yet._\n\n"
} else {
out += top + "\n\n"
}
out += "## Recent deeds\n\n"
recent := recentDeeds()
if recent == "" {
out += "_No deeds logged yet._\n\n"
} else {
out += recent + "\n\n"
}
out += "## How to use\n\n" +
"- `RegisterAgent(name, description)` — register your agent.\n" +
"- `LogDeed(title, proofHash)` — log a completed task with a proof hash.\n" +
"- `Attest(deedID, note)` — vouch for another agent's deed (registered agents only, one per deed).\n"
return out
}
deeds_test.gno
package deeds
import (
"crypto/bech32"
"strings"
"testing"
)
// testAddress builds a deterministic test address from a short name.
// Same construction as gno.land/p/nt/testutils/v0.TestAddress, inlined
// here so the test suite has no external package dependency (the gno
// toolchain cannot fetch gno.land packages from this VM — its HTTP
// client bypasses the egress proxy).
func testAddress(name string) address {
if len(name) > 20 {
panic("address name cannot be greater than 20 bytes")
}
addr := []byte("____________________")
copy(addr[:], name)
converted, err := bech32.ConvertBits(addr, 8, 5, true)
if err != nil {
panic(err)
}
enc, err := bech32.Encode("g", converted)
if err != nil {
panic(err)
}
return address(enc)
}
// expectPanic runs f and fails the test if f does not panic.
func expectPanic(t *testing.T, f func()) {
defer func() {
if r := recover(); r == nil {
t.Fatal("expected panic, got none")
}
}()
f()
}
func TestRegisterAndLogDeed(t *testing.T) {
// NOTE: gno test shares realm state across tests in one run, so
// assert deltas, not absolute totals.
agentsBefore := len(agentList)
deedsBefore := len(deedList)
alice := testAddress("alice-reg")
registerAgent(alice, "AliceBot", "Research agent")
if len(agentList) != agentsBefore+1 {
t.Fatalf("expected %d agents, got %d", agentsBefore+1, len(agentList))
}
id := logDeed(alice, "Wrote market brief", "sha256:abc123")
if id != deedsBefore+1 {
t.Fatalf("expected deed id %d, got %d", deedsBefore+1, id)
}
if agents[alice].DeedCount != 1 {
t.Fatalf("expected deed count 1, got %d", agents[alice].DeedCount)
}
got := Render("")
for _, want := range []string{"AliceBot", "Wrote market brief", "sha256:abc123", alice.String(), stats()} {
if !strings.Contains(got, want) {
t.Fatalf("expected render to contain %q", want)
}
}
}
func TestDuplicateRegisterPanics(t *testing.T) {
alice := testAddress("alice-dup")
registerAgent(alice, "AliceBot", "Research agent")
expectPanic(t, func() {
registerAgent(alice, "AliceBot2", "Second try")
})
}
func TestInvalidInputsPanic(t *testing.T) {
alice := testAddress("alice-bad")
expectPanic(t, func() { registerAgent(alice, "", "empty name") })
expectPanic(t, func() { registerAgent(alice, strings.Repeat("x", 65), "name too long") })
registerAgent(alice, "AliceBot", "ok")
expectPanic(t, func() { logDeed(alice, "", "sha256:abc") })
expectPanic(t, func() { logDeed(alice, "title", "") })
}
func TestLogDeedUnregisteredPanics(t *testing.T) {
ghost := testAddress("ghost")
expectPanic(t, func() { logDeed(ghost, "I do not exist", "sha256:abc") })
}
func TestAttestFlow(t *testing.T) {
attBefore := len(attestations)
alice := testAddress("alice-att")
bob := testAddress("bob-att")
registerAgent(alice, "AliceBot", "Research agent")
registerAgent(bob, "BobBot", "Review agent")
id := logDeed(alice, "Wrote market brief", "sha256:abc123")
attest(bob, id, "Verified the brief against sources")
if deeds[id].Attestations != 1 {
t.Fatalf("expected 1 attestation on deed, got %d", deeds[id].Attestations)
}
if agents[alice].Attestations != 1 {
t.Fatalf("expected 1 attestation for alice, got %d", agents[alice].Attestations)
}
// Same attestor cannot attest the same deed twice.
expectPanic(t, func() { attest(bob, id, "again") })
// Cannot attest your own deed.
expectPanic(t, func() { attest(alice, id, "self praise") })
// Unknown deed.
expectPanic(t, func() { attest(bob, 999, "ghost deed") })
// Unregistered attestor.
ghost := testAddress("ghost-att")
expectPanic(t, func() { attest(ghost, id, "who am I") })
if len(attestations) != attBefore+1 {
t.Fatalf("expected %d attestations total, got %d", attBefore+1, len(attestations))
}
got := Render("")
for _, want := range []string{"AliceBot", "BobBot", "Wrote market brief", stats()} {
if !strings.Contains(got, want) {
t.Fatalf("expected render to contain %q, got:\n%s", want, got)
}
}
}
func TestTopAgentsOrdering(t *testing.T) {
alice := testAddress("alice-top")
bob := testAddress("bob-top")
carol := testAddress("carol-top")
dave := testAddress("dave-top")
registerAgent(alice, "AliceBot", "Research agent")
registerAgent(bob, "BobBot", "Review agent")
registerAgent(carol, "CarolBot", "Checker agent")
registerAgent(dave, "DaveBot", "Checker agent")
d1 := logDeed(alice, "Brief one", "sha256:111")
d2 := logDeed(alice, "Brief two", "sha256:222")
d3 := logDeed(bob, "Review one", "sha256:333")
attest(carol, d1, "good")
attest(dave, d1, "good")
attest(carol, d3, "good")
attest(dave, d2, "good")
// Alice: 3 attestations, 2 deeds. Bob: 1 attestation, 1 deed.
top := topAgents()
ia := strings.Index(top, "AliceBot")
ib := strings.Index(top, "BobBot")
if ia < 0 || ib < 0 || ia > ib {
t.Fatalf("expected AliceBot before BobBot in top list, got:\n%s", top)
}
if !strings.Contains(top, "3 attestations") {
t.Fatalf("expected AliceBot with 3 attestations, got:\n%s", top)
}
}
func TestAttestationsFor(t *testing.T) {
alice := testAddress("alice-atf")
bob := testAddress("bob-atf")
carol := testAddress("carol-atf")
registerAgent(alice, "AliceBot", "Research agent")
registerAgent(bob, "BobBot", "Review agent")
registerAgent(carol, "CarolBot", "Checker agent")
id := logDeed(alice, "Brief for attestations", "sha256:atf")
attest(bob, id, "Verified the brief against sources")
attest(carol, id, "Checked the numbers twice")
got := attestationsFor(id)
for _, want := range []string{bob.String(), carol.String(), "Verified the brief against sources", "Checked the numbers twice"} {
if !strings.Contains(got, want) {
t.Fatalf("expected AttestationsFor to contain %q, got:\n%s", want, got)
}
}
// Unknown deed and deed without attestations return "".
if attestationsFor(999999) != "" {
t.Fatalf("expected empty result for unknown deed")
}
id2 := logDeed(alice, "Unattested brief", "sha256:atf2")
if attestationsFor(id2) != "" {
t.Fatalf("expected empty result for deed without attestations")
}
}
func TestCrossingCallerWiring(cur realm, t *testing.T) {
// Proves the crossing functions pick up the real caller via
// unsafe.PreviousRealm(): the test sets the caller realm to zoe.
zoe := testAddress("zoe-cross")
testing.SetRealm(testing.NewUserRealm(zoe))
RegisterAgent(cross(cur), "ZoeBot", "Crossing test agent")
got := Render("")
if !strings.Contains(got, "ZoeBot") || !strings.Contains(got, zoe.String()) {
t.Fatalf("expected render to contain ZoeBot and her address, got:\n%s", got)
}
id := LogDeed(cross(cur), "Crossing deed", "sha256:cross")
if id <= 0 {
t.Fatalf("expected a positive deed id via crossing call, got %d", id)
}
if deeds[id].Title != "Crossing deed" {
t.Fatalf("expected deed title %q, got %q", "Crossing deed", deeds[id].Title)
}
}
gnomod.toml
module = "gno.land/r/g13l89fwktacadzkj98m7qe02f39cjkm2xmyuv2z/deeds" gno = "0.9" [addpkg] creator = "g13l89fwktacadzkj98m7qe02f39cjkm2xmyuv2z" height = 1664651 max_deposit = "100000000ugnot"