| 1 | package memoria |
| 2 | |
| 3 | import ( |
| 4 | access "amadan.net/oficina/agent-access.git" |
| 5 | "amadan.net/oficina/memoria/internal/memoria/seal" |
| 6 | "amadan.net/oficina/memoria/internal/memoria/sibling" |
| 7 | "amadan.net/oficina/suite" |
| 8 | "context" |
| 9 | "encoding/json" |
| 10 | "errors" |
| 11 | "gorm.io/gorm" |
| 12 | ) |
| 13 | |
| 14 | func (a *app) agentResponse(ctx context.Context, p access.Principal, name string, _ json.RawMessage, result any) error { |
| 15 | state, ok := ctx.Value(agentBoundaryKey{}).(*agentRequest) |
| 16 | if !ok || state.personID == 0 || name != "" && !state.selected { |
| 17 | return access.Fail("forbidden", "Response authority is unavailable.") |
| 18 | } |
| 19 | address, err := a.agentAddress(ctx, p, a.agentTeam) |
| 20 | if err != nil { |
| 21 | return err |
| 22 | } |
| 23 | if name == "notes_search" { |
| 24 | value, ok := result.(map[string]any) |
| 25 | if !ok { |
| 26 | return access.Fail("forbidden", "Passage provenance is unavailable.") |
| 27 | } |
| 28 | rows, ok := value["results"].([]any) |
| 29 | if !ok || len(rows) != len(state.passages) { |
| 30 | return access.Fail("forbidden", "Passage provenance is unavailable.") |
| 31 | } |
| 32 | if err := a.store.guardAgentPassages(ctx, a.agentSubject(p, address), state.passages); err != nil { |
| 33 | return err |
| 34 | } |
| 35 | _, err = a.agentAddress(ctx, p, a.agentTeam) |
| 36 | return err |
| 37 | } |
| 38 | if name == "notes_get" || name == "notes_update" { |
| 39 | if state.noteID <= 0 || state.noteExternal == "" { |
| 40 | return access.Fail("forbidden", "Note provenance is unavailable.") |
| 41 | } |
| 42 | d, err := localAgentNote(a.db.G.WithContext(ctx), state.noteID) |
| 43 | if err != nil { |
| 44 | return err |
| 45 | } |
| 46 | if d.ExternalID != state.noteExternal || name == "notes_get" && d.Revision != state.noteRevision { |
| 47 | return access.Fail("conflict", "This note changed while reading.") |
| 48 | } |
| 49 | } |
| 50 | if name == "notes_create" { |
| 51 | if state.noteID <= 0 || state.noteKey == "" || state.noteExternal == "" { |
| 52 | return access.Fail("forbidden", "Note provenance is unavailable.") |
| 53 | } |
| 54 | var count int64 |
| 55 | err := a.db.G.WithContext(ctx).Table("documents").Joins("JOIN memoria_agent_notes ON memoria_agent_notes.document_id=documents.id").Where("documents.id=? AND documents.source='paste' AND documents.external_id=? AND memoria_agent_notes.coordinate=?", state.noteID, state.noteExternal, state.noteKey).Count(&count).Error |
| 56 | if err != nil { |
| 57 | return access.Fail("unavailable", "Note storage is unavailable.") |
| 58 | } |
| 59 | if count != 1 { |
| 60 | return access.Fail("not_found", "This note is unavailable.") |
| 61 | } |
| 62 | } |
| 63 | return nil |
| 64 | } |
| 65 | |
| 66 | // guardAgentPassages asks only about the already selected passage coordinates; |
| 67 | // it neither searches again nor changes the original search's breaker counters. |
| 68 | func (s *Store) guardAgentPassages(ctx context.Context, subject suite.Subject, passages []Result) error { |
| 69 | var candidates []sibling.Candidate |
| 70 | for _, r := range passages { |
| 71 | if r.ChunkID <= 0 || r.DocumentID <= 0 { |
| 72 | return access.Fail("forbidden", "Passage provenance is unavailable.") |
| 73 | } |
| 74 | if r.item.Item.ItemID == "" { |
| 75 | continue |
| 76 | } |
| 77 | if s.custody == nil { |
| 78 | return access.Fail("unavailable", "Passage custody is unavailable.") |
| 79 | } |
| 80 | sib, ok := s.custody.Sibling(r.Source) |
| 81 | if !ok { |
| 82 | return access.Fail("forbidden", "Passage custody is unavailable.") |
| 83 | } |
| 84 | candidate := r.item |
| 85 | candidate.Sibling = sib |
| 86 | candidates = append(candidates, candidate) |
| 87 | } |
| 88 | if len(candidates) > 0 { |
| 89 | dispositions := s.custody.Rule(ctx, subject, candidates) |
| 90 | if len(dispositions) != len(candidates) { |
| 91 | return access.Fail("forbidden", "Passage custody is incomplete.") |
| 92 | } |
| 93 | for _, d := range dispositions { |
| 94 | if !d.Renders() { |
| 95 | return access.Fail("forbidden", "Passage access changed.") |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | // Custody may take network time; verify local provenance after it returns. |
| 100 | for _, r := range passages { |
| 101 | var d Document |
| 102 | err := s.db.G.WithContext(ctx).Table("documents").Select("documents.*").Joins("JOIN chunks ON chunks.document_id=documents.id").Where("documents.id=? AND chunks.id=?", r.DocumentID, r.ChunkID).Take(&d).Error |
| 103 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 104 | return access.Fail("not_found", "This passage is unavailable.") |
| 105 | } |
| 106 | if err != nil { |
| 107 | return access.Fail("unavailable", "Passage storage is unavailable.") |
| 108 | } |
| 109 | if d.Source != r.Source { |
| 110 | return access.Fail("not_found", "This passage is unavailable.") |
| 111 | } |
| 112 | if r.item.Item.ItemID == "" { |
| 113 | if len(d.SiblingItemID) != 0 || d.SiblingRevision != 0 { |
| 114 | return access.Fail("forbidden", "Passage custody changed.") |
| 115 | } |
| 116 | continue |
| 117 | } |
| 118 | id, err := s.sealer.Open(seal.PurposeItemID, d.SiblingItemID) |
| 119 | if err != nil || string(id) != r.item.Item.ItemID || d.SiblingRevision != r.item.Item.Revision { |
| 120 | return access.Fail("forbidden", "Passage custody changed.") |
| 121 | } |
| 122 | } |
| 123 | return nil |
| 124 | } |
| 125 | |