| 1 | package memoria |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "strings" |
| 9 | |
| 10 | access "amadan.net/oficina/agent-access.git" |
| 11 | "github.com/google/uuid" |
| 12 | ) |
| 13 | |
| 14 | func (a *app) agentCreate(ctx context.Context, p access.Principal, raw json.RawMessage, address string) (any, error) { |
| 15 | var in struct { |
| 16 | Team, Title, Body string |
| 17 | ClientID string `json:"client_id"` |
| 18 | } |
| 19 | if json.Unmarshal(raw, &in) != nil { |
| 20 | return nil, access.Fail("invalid", "Invalid note.") |
| 21 | } |
| 22 | uid, e := uuid.Parse(in.ClientID) |
| 23 | if e != nil || uid == uuid.Nil || uid.String() != in.ClientID || strings.TrimSpace(in.Title) == "" || strings.TrimSpace(in.Body) == "" || len(in.Title) > 512 || len(in.Body) > 1<<20 { |
| 24 | return nil, access.Fail("invalid", "Supply a title, a note up to 1 MiB, and a canonical nonzero UUID client_id.") |
| 25 | } |
| 26 | coordinate, _ := json.Marshal([]string{a.home.issuer, a.agentIssuer, p.Subject, p.Client, in.Team, uid.String()}) |
| 27 | key, e := a.store.sealer.Fingerprint("agent.note.coordinate.v1", coordinate) |
| 28 | if e != nil { |
| 29 | return nil, e |
| 30 | } |
| 31 | content, _ := json.Marshal([]string{in.Title, in.Body}) |
| 32 | fingerprint, e := a.store.sealer.Fingerprint("agent.note.content.v1", content) |
| 33 | if e != nil { |
| 34 | return nil, e |
| 35 | } |
| 36 | a.agentNotes.Lock() |
| 37 | defer a.agentNotes.Unlock() |
| 38 | replayed, indexed := false, false |
| 39 | var noteExternal string |
| 40 | guard := &ingestBoundary{ |
| 41 | before: func(tx *gormTx, external string) (int64, bool, error) { |
| 42 | noteExternal = external |
| 43 | if e := a.bindAgent(tx); e != nil { |
| 44 | return 0, false, e |
| 45 | } |
| 46 | if e := a.checkAgentWriter(ctx, tx, p, address); e != nil { |
| 47 | return 0, false, e |
| 48 | } |
| 49 | if e := tx.Exec("INSERT INTO memoria_agent_notes(coordinate,content) VALUES(?,?) ON CONFLICT(coordinate) DO NOTHING", key, fingerprint).Error; e != nil { |
| 50 | return 0, false, e |
| 51 | } |
| 52 | var saved string |
| 53 | var id sql.NullInt64 |
| 54 | if e := tx.Raw("SELECT content,document_id,indexed FROM memoria_agent_notes WHERE coordinate=?", key).Row().Scan(&saved, &id, &indexed); e != nil { |
| 55 | return 0, false, e |
| 56 | } |
| 57 | if saved != fingerprint { |
| 58 | return 0, false, access.Fail("conflict", "This client_id was already used with different note content.") |
| 59 | } |
| 60 | if id.Valid { |
| 61 | var count int64 |
| 62 | if e := tx.Model(&Document{}).Where("id=? AND source='paste' AND external_id=?", id.Int64, external).Count(&count).Error; e != nil { |
| 63 | return 0, false, e |
| 64 | } |
| 65 | if count != 1 { |
| 66 | return 0, false, access.Fail("not_found", "This note is unavailable.") |
| 67 | } |
| 68 | replayed = true |
| 69 | return id.Int64, true, nil |
| 70 | } |
| 71 | var count int64 |
| 72 | if e := tx.Model(&Document{}).Where("source='paste' AND external_id=?", external).Count(&count).Error; e != nil { |
| 73 | return 0, false, e |
| 74 | } |
| 75 | if count != 0 { |
| 76 | return 0, false, access.Fail("unavailable", "The note receipt requires explicit repair.") |
| 77 | } |
| 78 | return 0, false, nil |
| 79 | }, |
| 80 | after: func(tx *gormTx, id int64) error { |
| 81 | if !replayed { |
| 82 | result := tx.Exec("UPDATE memoria_agent_notes SET document_id=?,indexed=0 WHERE coordinate=? AND document_id IS NULL", id, key) |
| 83 | if result.Error != nil { |
| 84 | return result.Error |
| 85 | } |
| 86 | if result.RowsAffected != 1 { |
| 87 | return errors.New("memoria: note receipt changed") |
| 88 | } |
| 89 | } |
| 90 | return a.checkAgentWriter(ctx, tx, p, address) |
| 91 | }, |
| 92 | } |
| 93 | noteID, e := a.store.ingestAgent(ctx, IngestSpec{Source: "paste", Key: "agent-" + key, Title: in.Title, Body: in.Body}, guard) |
| 94 | if e != nil && !errors.Is(e, ErrIndexLag) { |
| 95 | return nil, e |
| 96 | } |
| 97 | if state, ok := ctx.Value(agentBoundaryKey{}).(*agentRequest); ok { |
| 98 | state.noteID = noteID |
| 99 | state.noteKey = key |
| 100 | state.noteExternal = noteExternal |
| 101 | } |
| 102 | if replayed { |
| 103 | return agentNoteResult{noteID, indexed}, nil |
| 104 | } |
| 105 | indexed = e == nil |
| 106 | if _, e = a.db.Writer().ExecContext(context.WithoutCancel(ctx), "UPDATE memoria_agent_notes SET indexed=? WHERE coordinate=? AND document_id=?", indexed, key, noteID); e != nil { |
| 107 | // Content and durable retry identity already committed. Conservatively report |
| 108 | // index state unknown rather than turn this into a duplicate-producing error. |
| 109 | return agentNoteResult{noteID, false}, nil |
| 110 | } |
| 111 | return agentNoteResult{noteID, indexed}, nil |
| 112 | } |
| 113 | |