| 1 | package memoria |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "net/http" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | access "amadan.net/oficina/agent-access.git" |
| 11 | "amadan.net/oficina/home/teammode" |
| 12 | "amadan.net/oficina/memoria/internal/memoria/sibling" |
| 13 | "amadan.net/oficina/suite" |
| 14 | ) |
| 15 | |
| 16 | func linkedTeamAgentFixture(t *testing.T) (*teamFixture, access.Principal, http.Handler, string) { |
| 17 | t.Helper() |
| 18 | f := newTeamFixture(t, "alice@example.test") |
| 19 | subject := teamSubject('a') |
| 20 | f.dir.Set(subject, "member") |
| 21 | f.refreshed() |
| 22 | f.local(t) |
| 23 | if w := f.linkFromMenu(t, subject); w.Code != http.StatusSeeOther { |
| 24 | t.Fatal("link", w.Code, w.Body.String()) |
| 25 | } |
| 26 | team := f.app.team.Bindings().Team |
| 27 | p := access.Principal{Subject: subject, Client: "test-agent", Grant: "grant-1", Teams: []string{team}, Scopes: []string{"notes:read", "notes:write"}} |
| 28 | mux := http.NewServeMux() |
| 29 | if err := f.app.mountAgentAccess(mux, "https://connect.example.test", team, access.VerifyFunc(func(context.Context, string) (access.Principal, error) { return p, nil })); err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | input := strings.Replace(agentNoteInput, `"acme"`, `"`+team+`"`, 1) |
| 33 | return f, p, mux, input |
| 34 | } |
| 35 | |
| 36 | func TestAgentTeamMembershipSupersedesLocalAllowlist(t *testing.T) { |
| 37 | for _, mode := range []string{"member-without-local-admission", "removed-but-still-locally-admitted", "unknown"} { |
| 38 | t.Run(mode, func(t *testing.T) { |
| 39 | f, p, h, input := linkedTeamAgentFixture(t) |
| 40 | if w := agentCall(h, "notes_create", input); w.Code != 200 { |
| 41 | t.Fatal("positive create", w.Code, w.Body.String()) |
| 42 | } |
| 43 | want := 200 |
| 44 | switch mode { |
| 45 | case "member-without-local-admission": |
| 46 | f.app.SetOwners(nil) |
| 47 | case "removed-but-still-locally-admitted": |
| 48 | f.dir.Set(p.Subject, "") |
| 49 | f.refreshed() |
| 50 | want = 403 |
| 51 | case "unknown": |
| 52 | f.expired() |
| 53 | want = 503 |
| 54 | } |
| 55 | for op, raw := range map[string]string{"notes_create": input, "notes_search": `{"team":"` + p.Teams[0] + `","query":"nebula"}`} { |
| 56 | if w := agentCall(h, op, raw); w.Code != want { |
| 57 | t.Fatal(op, w.Code, w.Body.String(), "want", want) |
| 58 | } |
| 59 | } |
| 60 | }) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func TestAgentTeamConfigRejectsForeignBindingAndMissingRuntime(t *testing.T) { |
| 65 | for _, mode := range []string{"foreign", "missing-runtime"} { |
| 66 | t.Run(mode, func(t *testing.T) { |
| 67 | f := newTeamFixture(t, "alice@example.test") |
| 68 | team := f.app.team.Bindings().Team |
| 69 | if mode == "foreign" { |
| 70 | team = "different" |
| 71 | } else { |
| 72 | f.app.team = nil |
| 73 | } |
| 74 | if err := f.app.mountAgentAccess(http.NewServeMux(), "https://connect.example.test", team, access.VerifyFunc(func(context.Context, string) (access.Principal, error) { return access.Principal{}, nil })); err == nil { |
| 75 | t.Fatal("invalid team binding accepted") |
| 76 | } |
| 77 | var tables int |
| 78 | if err := f.app.db.Writer().QueryRow("SELECT COUNT(*) FROM sqlite_master WHERE name IN ('memoria_agent_state','memoria_agent_notes')").Scan(&tables); err != nil || tables != 0 { |
| 79 | t.Fatal("invalid binding reached agent migrations", tables, err) |
| 80 | } |
| 81 | }) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | type teamAgentCustody struct { |
| 86 | *fixedCustody |
| 87 | subjects []suite.Subject |
| 88 | after func(int) |
| 89 | } |
| 90 | |
| 91 | func (c *teamAgentCustody) Rule(ctx context.Context, subject suite.Subject, items []sibling.Candidate) []sibling.Disposition { |
| 92 | c.subjects = append(c.subjects, subject) |
| 93 | result := c.fixedCustody.Rule(ctx, subject, items) |
| 94 | if c.after != nil { |
| 95 | c.after(len(c.subjects)) |
| 96 | } |
| 97 | return result |
| 98 | } |
| 99 | |
| 100 | func TestAgentTeamCustodyUsesPersonRefAndRechecksStanding(t *testing.T) { |
| 101 | for _, phase := range []string{"valid", "after-search", "during-emission"} { |
| 102 | t.Run(phase, func(t *testing.T) { |
| 103 | f, p, h, _ := linkedTeamAgentFixture(t) |
| 104 | custody := &teamAgentCustody{fixedCustody: &fixedCustody{apps: docsRoster(), give: suite.VISIBLE}} |
| 105 | f.app.store.custody = custody |
| 106 | if _, err := f.app.store.Ingest(systemWrite(t.Context()), IngestSpec{Source: "Docs", Key: "team-sibling", Title: "Sibling nebula", Body: "nebula sibling secret", ItemID: "docs-one", SiblingRevision: 1}); err != nil { |
| 107 | t.Fatal(err) |
| 108 | } |
| 109 | custody.after = func(n int) { |
| 110 | if phase == "after-search" && n == 1 || phase == "during-emission" && n == 2 { |
| 111 | f.expired() |
| 112 | } |
| 113 | } |
| 114 | w := agentCall(h, "notes_search", `{"team":"`+p.Teams[0]+`","query":"nebula"}`) |
| 115 | if phase == "valid" { |
| 116 | if w.Code != 200 || !strings.Contains(w.Body.String(), "sibling secret") || len(custody.subjects) < 2 { |
| 117 | t.Fatal("positive search", w.Code, w.Body.String(), custody.subjects) |
| 118 | } |
| 119 | } else if w.Code != 503 || strings.Contains(w.Body.String(), "sibling secret") { |
| 120 | t.Fatal("expired standing disclosed passage", w.Code, w.Body.String()) |
| 121 | } |
| 122 | for _, subject := range custody.subjects { |
| 123 | if subject != (suite.Subject{Kind: suite.SubjectPersonRef, Value: p.Subject}) { |
| 124 | t.Fatal("wrong sibling identity", subject) |
| 125 | } |
| 126 | } |
| 127 | }) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | func TestAgentTeamWriterExpiryRollsBackAndRefusesRetry(t *testing.T) { |
| 132 | for _, retry := range []bool{false, true} { |
| 133 | t.Run(map[bool]string{false: "new", true: "completed-retry"}[retry], func(t *testing.T) { |
| 134 | f, p, h, input := linkedTeamAgentFixture(t) |
| 135 | if retry { |
| 136 | if w := agentCall(h, "notes_create", input); w.Code != 200 { |
| 137 | t.Fatal(w.Code, w.Body.String()) |
| 138 | } |
| 139 | } |
| 140 | beforeDocs, beforeChunks, beforeReceipts := f.count("documents"), f.count("chunks"), f.count("memoria_agent_notes") |
| 141 | checks := 0 |
| 142 | state := &agentRequest{verify: func(context.Context, access.Principal, string) error { |
| 143 | checks++ |
| 144 | if checks == 2 { |
| 145 | f.expired() |
| 146 | } |
| 147 | return nil |
| 148 | }} |
| 149 | ctx := context.WithValue(t.Context(), agentBoundaryKey{}, state) |
| 150 | address, err := f.app.agentAddress(ctx, p, p.Teams[0]) |
| 151 | if err != nil { |
| 152 | t.Fatal(err) |
| 153 | } |
| 154 | ctx = teammode.WithAccess(ctx, state.standing) |
| 155 | // Domain call has no HTTP response guard to conceal a missed commit check. |
| 156 | result, err := f.app.agentCreate(ctx, p, json.RawMessage(input), address) |
| 157 | if err == nil || result != nil || checks != 2 { |
| 158 | t.Fatal("expired write/retry succeeded", result, err, checks) |
| 159 | } |
| 160 | if f.count("documents") != beforeDocs || f.count("chunks") != beforeChunks || f.count("memoria_agent_notes") != beforeReceipts { |
| 161 | t.Fatal("expired write retained effects") |
| 162 | } |
| 163 | }) |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | func TestAgentTeamHomeOnlyReadRefused(t *testing.T) { |
| 168 | f := newTeamFixture(t) |
| 169 | subject := teamSubject('a') |
| 170 | f.member(subject, "member") |
| 171 | team := f.app.team.Bindings().Team |
| 172 | p := access.Principal{Subject: subject, Client: "test", Grant: "grant", Teams: []string{team}, Scopes: []string{"notes:read", "notes:write"}} |
| 173 | mux := http.NewServeMux() |
| 174 | if err := f.app.mountAgentAccess(mux, "https://connect.example.test", team, access.VerifyFunc(func(context.Context, string) (access.Principal, error) { return p, nil })); err != nil { |
| 175 | t.Fatal(err) |
| 176 | } |
| 177 | if w := agentCall(mux, "notes_search", `{"team":"`+team+`","query":"nebula"}`); w.Code != 403 { |
| 178 | t.Fatal("Home-only agent read accepted", w.Code, w.Body.String()) |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | func TestAgentTeamWriterStandingIsIndependent(t *testing.T) { |
| 183 | f, p, _, _ := linkedTeamAgentFixture(t) |
| 184 | state := &agentRequest{verify: func(context.Context, access.Principal, string) error { f.expired(); return nil }} |
| 185 | ctx := context.WithValue(t.Context(), agentBoundaryKey{}, state) |
| 186 | address, err := f.app.agentAddress(ctx, p, p.Teams[0]) |
| 187 | if err != nil { |
| 188 | t.Fatal(err) |
| 189 | } |
| 190 | tx := f.app.db.G.WithContext(ctx).Begin() |
| 191 | if tx.Error != nil { |
| 192 | t.Fatal(tx.Error) |
| 193 | } |
| 194 | defer tx.Rollback() |
| 195 | if err := f.app.checkAgentWriter(ctx, tx, p, address); err == nil { |
| 196 | t.Fatal("writer ignored expired standing") |
| 197 | } |
| 198 | } |
| 199 | |