| 1 | // Package chat is the thin v1 conversation core on the instance: person API |
| 2 | // v1 messages, turns, the person event log and stream, and the runner |
| 3 | // WebSocket with its durable command mailbox. Every state change and its |
| 4 | // event are written in one transaction. |
| 5 | package chat |
| 6 | |
| 7 | import ( |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/equipo/push" |
| 11 | ) |
| 12 | |
| 13 | // Models are the tables this package owns, for the migration check. |
| 14 | var Models = append([]any{&changeDiffRecord{}, ¬ificationReceipt{}, &projectNameRecord{}, &cardVisibility{}, &cardThreadRecord{}, ©AttemptRecord{}, ©ReviewRecord{}, ©FeedbackRecord{}, &turnRecord{}, &messageRecord{}, &eventRecord{}, &commandRecord{}, &inboxRecord{}, &runnerSessionRecord{}, &projectRecord{}, &cardRecord{}, &toolCallRecord{}, &settingRecord{}, &coordinatorRecord{}, ¬ebookRecord{}, &taskRecord{}, &stepRecord{}, &entryRecord{}, &attachmentRecord{}, &starRecord{}, &questionRecord{}, &connectionRecord{}, &usageRecord{}, &distinctRecord{}, &hkRecord{}, &hkFailureRecord{}, &hkSettingsRecord{}, &fileRecord{}, &fileDataRecord{}, &transferRecord{}, &mergePolicyRecord{}, &mergeAuditRecord{}, &specialistRecord{}, &grantRecord{}, &specialistAuditRecord{}, &researchSourceRecord{}, &researchAddressRecord{}, &researchPrefixRecord{}, &researchInventoryRecord{}, &instancePrefixRecord{}, &instanceAddressRecord{}, &instanceInventoryCardRecord{}, &webSearchUsageRecord{}, &lookupRecord{}, &researchRecord{}, &researchFetchRecord{}, &taskFileRecord{}, &taskFileDataRecord{}, &stepRenderRecord{}, &releaseRecord{}, &adviceRecord{}, &assistantHostRecord{}, &abilitiesRecord{}, &handoffRecord{}, &handoffTurnRecord{}, &handoffTaskRecord{}, &handoffActionRecord{}, &connectTurnRecord{}, &specialistTurnRecord{}, &carriedRecord{}, &zoneRecord{}, &placeRecord{}, &projectPlaceRecord{}}, push.Models...) |
| 15 | |
| 16 | type turnRecord struct { |
| 17 | CreateRepo string |
| 18 | ID string `gorm:"primaryKey"` |
| 19 | Person string `gorm:"index:chat_turn_person,priority:1"` |
| 20 | State string `gorm:"index:chat_turn_person,priority:2"` |
| 21 | Reason string |
| 22 | Attempt int64 |
| 23 | Dispatched bool |
| 24 | // DispatchedAt is when the message command was committed; Timing is |
| 25 | // the wire.TurnTiming JSON once known. |
| 26 | DispatchedAt *time.Time |
| 27 | Timing string |
| 28 | ReplyID string |
| 29 | // Kind is wire.TurnPerson, TurnMoveIn or TurnExplore; Yield marks a |
| 30 | // move-in or explore turn asked to give way to the person. |
| 31 | Kind string |
| 32 | Yield bool |
| 33 | // Connection is the runner's connection that answered (its LocalID). |
| 34 | Connection string |
| 35 | Revision int64 |
| 36 | CreatedAt time.Time |
| 37 | UpdatedAt time.Time |
| 38 | // Lane is wire.LaneCoordinator (empty), wire.LaneAssistant or a job |
| 39 | // thread (wire.JobLane). |
| 40 | Lane string |
| 41 | // Note is a team_update turn's text: the summary it tells. |
| 42 | Note string |
| 43 | // RetryOf is the failed turn this turn tried again (Try again): its |
| 44 | // exchange moved here (Retry). It is never read to dispatch. |
| 45 | RetryOf string |
| 46 | } |
| 47 | |
| 48 | func (turnRecord) TableName() string { return "chat_turns" } |
| 49 | |
| 50 | // carriedRecord is one person's message a turn carried, unanswered, from |
| 51 | // a turn that failed before it (dispatch, unanswered). |
| 52 | type carriedRecord struct { |
| 53 | TurnID string `gorm:"primaryKey"` |
| 54 | MessageID string `gorm:"primaryKey"` |
| 55 | Person string |
| 56 | } |
| 57 | |
| 58 | func (carriedRecord) TableName() string { return "chat_turn_carried" } |
| 59 | |
| 60 | // messageRecord's integer key is the SQLite rowid, so the person index |
| 61 | // orders by it. Coordinator replies use the key "reply:<turn>". A key is |
| 62 | // unique per person and lane. |
| 63 | type messageRecord struct { |
| 64 | ProjectID string |
| 65 | Origin string |
| 66 | Seq int64 `gorm:"primaryKey"` |
| 67 | ID string `gorm:"uniqueIndex:chat_message_id"` |
| 68 | Person string `gorm:"index:chat_message_person;uniqueIndex:chat_message_key,priority:1"` |
| 69 | IdempotencyKey string `gorm:"uniqueIndex:chat_message_key,priority:3"` |
| 70 | BodyHash string |
| 71 | Role string |
| 72 | Text string |
| 73 | TurnID string `gorm:"index:chat_message_turn"` |
| 74 | Revision int64 |
| 75 | CreatedAt time.Time |
| 76 | // Switched is the reply's wire.Switched JSON, or empty. |
| 77 | Switched string |
| 78 | // Lane is its turn's lane. |
| 79 | Lane string `gorm:"uniqueIndex:chat_message_key,priority:2"` |
| 80 | } |
| 81 | |
| 82 | func (messageRecord) TableName() string { return "chat_messages" } |
| 83 | |
| 84 | // eventRecord's id is the person's stream cursor. Retention never deletes |
| 85 | // the newest row, so ids are never reused. |
| 86 | type eventRecord struct { |
| 87 | ID int64 `gorm:"primaryKey"` |
| 88 | Person string `gorm:"index:chat_event_person"` |
| 89 | Type string |
| 90 | Body string |
| 91 | CreatedAt time.Time |
| 92 | // Lane is the lane whose stream carries it: each lane's events go to |
| 93 | // its own routes only. |
| 94 | Lane string |
| 95 | } |
| 96 | |
| 97 | func (eventRecord) TableName() string { return "chat_events" } |
| 98 | |
| 99 | // commandRecord is the instance side of the runner mailbox. A command is |
| 100 | // delivered to its runner (RunnerID) until that runner acks it: the |
| 101 | // person's primary for conversation, merge and publication commands, the |
| 102 | // step's runner for its dispatch and cancel. |
| 103 | type commandRecord struct { |
| 104 | Seq int64 `gorm:"primaryKey"` |
| 105 | Person string `gorm:"index:runner_command_person"` |
| 106 | Kind string |
| 107 | Body string |
| 108 | CreatedAt time.Time |
| 109 | AckedAt *time.Time |
| 110 | RunnerID string `gorm:"index:runner_command_runner"` |
| 111 | } |
| 112 | |
| 113 | func (commandRecord) TableName() string { return "runner_commands" } |
| 114 | |
| 115 | // inboxRecord makes runner frames idempotent: the frame id is recorded in |
| 116 | // the same transaction as its effect. |
| 117 | type inboxRecord struct { |
| 118 | RunnerID string `gorm:"primaryKey"` |
| 119 | FrameID string `gorm:"primaryKey"` |
| 120 | SeenAt time.Time |
| 121 | } |
| 122 | |
| 123 | func (inboxRecord) TableName() string { return "runner_inbox" } |
| 124 | |
| 125 | // runnerSessionRecord is the execution lease: one generation per runner. |
| 126 | type runnerSessionRecord struct { |
| 127 | RunnerID string `gorm:"primaryKey"` |
| 128 | Generation int64 |
| 129 | Connected bool |
| 130 | Process string |
| 131 | Revision int64 |
| 132 | Hello string |
| 133 | SeenAt time.Time |
| 134 | // Protocol is what this connection speaks (wire.Spoken). |
| 135 | Protocol int |
| 136 | } |
| 137 | |
| 138 | func (runnerSessionRecord) TableName() string { return "runner_sessions" } |
| 139 | |
| 140 | // coordinatorRecord is whether a person's coordinator has moved in, and |
| 141 | // the move-in turn while one runs. |
| 142 | type coordinatorRecord struct { |
| 143 | Person string `gorm:"primaryKey"` |
| 144 | State string |
| 145 | TurnID string |
| 146 | Revision int64 |
| 147 | MovedInAt *time.Time |
| 148 | UpdatedAt time.Time |
| 149 | } |
| 150 | |
| 151 | func (coordinatorRecord) TableName() string { return "chat_coordinators" } |
| 152 | |
| 153 | // transferRecord authorises the chunks of one pack between two runners of |
| 154 | // a person, for one attempt of one step (Mac runners spec §2). Chunks |
| 155 | // travel as blob commands to ToRunner; Received and Bytes bound them. |
| 156 | type transferRecord struct { |
| 157 | ID string `gorm:"primaryKey"` |
| 158 | Person string |
| 159 | FromRunner string |
| 160 | ToRunner string |
| 161 | Repo string |
| 162 | TaskID string |
| 163 | StepID string `gorm:"index:runner_transfer_step"` |
| 164 | Attempt int64 |
| 165 | BaseSHA string |
| 166 | CandidateSHA string |
| 167 | // Kind is "" (checks, or a worker's outbound commit) or |
| 168 | // wire.TransferKindEngineering; OldBaseSHA is an Engineering rebase |
| 169 | // round's old base (migration 0120). |
| 170 | Kind string |
| 171 | OldBaseSHA string |
| 172 | Direction string |
| 173 | State string |
| 174 | SHA256 string |
| 175 | Total int |
| 176 | Received int |
| 177 | Bytes int64 |
| 178 | ExpiresAt time.Time |
| 179 | CreatedAt time.Time |
| 180 | } |
| 181 | |
| 182 | func (transferRecord) TableName() string { return "runner_transfers" } |
| 183 | |