| 1 | package conversation |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 5 | "gorm.io/gorm" |
| 6 | ) |
| 7 | |
| 8 | // A published candidate still awaits its person. A delivered recommendation is |
| 9 | // terminal; a successful investigation may still await its next plan. |
| 10 | const contextOpenWork = `w.state NOT IN ('merged','cancelled','failed','completed') |
| 11 | AND COALESCE(j.state,'') NOT IN ('cancelled','failed') |
| 12 | AND NOT (w.kind='recommendation' AND COALESCE(j.state,'')='succeeded')` |
| 13 | |
| 14 | // Answers follow the work created from, or explicitly attached to, their request. |
| 15 | // Until that work exists, the answer's pending follow-up owns the decision. |
| 16 | const contextAnswerOpen = `COALESCE((SELECT MAX(CASE WHEN ` + contextOpenWork + ` THEN 1 ELSE 0 END) |
| 17 | FROM tasks w LEFT JOIN jobs j ON j.id=w.job_id |
| 18 | WHERE w.team_id=t.team_id AND w.id IN ( |
| 19 | SELECT task_id FROM conversation_routes WHERE source_id IN (json_extract(q.payload,'$.MessageID'),m.id) |
| 20 | UNION SELECT json_extract(cr.result,'$.resource_id') FROM conversation_requests cr |
| 21 | WHERE cr.principal=q.person AND cr.operation='coordinator.tool' |
| 22 | AND json_extract(cr.result,'$.value.message_id') IN (json_extract(q.payload,'$.MessageID'),m.id))), |
| 23 | EXISTS(SELECT 1 FROM coordinator_turns followup WHERE followup.id=json_extract(r.result,'$.TurnID') |
| 24 | AND followup.generation=? AND followup.state NOT IN ('delivered','superseded')) |
| 25 | OR EXISTS(SELECT 1 FROM coordinator_questions pending WHERE pending.binding_id=q.binding_id |
| 26 | AND pending.generation=? AND pending.state='pending' |
| 27 | AND json_extract(pending.payload,'$.MessageID')=json_extract(q.payload,'$.MessageID')))` |
| 28 | |
| 29 | type contextDecision struct { |
| 30 | ID, Body string |
| 31 | Revision, ProjectID int64 |
| 32 | } |
| 33 | |
| 34 | func (e *Engine) contextDecisions(tx *gorm.DB, c CoordinatorClaim, row TurnRecord) (required, optional []runnerproto.ContextRef, err error) { |
| 35 | answers := tx.Table("coordinator_questions q").Select("q.id,m.body,q.revision,t.project_id").Joins("JOIN coordinator_turns t ON t.id=q.turn_id").Joins("JOIN conversation_requests r ON r.resource=q.id AND r.operation='clarification.answer' AND r.principal=q.person").Joins("JOIN conversation_messages m ON m.id=json_extract(r.result,'$.MessageID')").Where("q.person=? AND t.conversation_id=? AND q.state='answered'", c.Binding.Person, row.ConversationID) |
| 36 | approvals := tx.Table("tasks w").Select("w.id,p.plan AS body,w.project_id,p.revision").Joins("LEFT JOIN jobs j ON j.id=w.job_id").Joins("JOIN plan_revisions p ON p.task_id=w.id AND p.revision=w.approval_revision").Where("w.requester=? AND w.team_id=? AND w.approval_revision=w.plan_revision AND EXISTS (SELECT 1 FROM plan_requests r WHERE r.resource=w.id AND r.operation='plan.approve' AND r.principal=? AND json_extract(r.result,'$.ApprovalRevision')=w.approval_revision AND json_extract(r.result,'$.PlanRevision')=w.plan_revision)", c.Binding.Person, row.TeamID, c.Binding.Person) |
| 37 | for _, open := range []bool{true, false} { |
| 38 | limit := 50 |
| 39 | if open { |
| 40 | limit = 1001 |
| 41 | } |
| 42 | for _, source := range []struct { |
| 43 | kind string |
| 44 | query *gorm.DB |
| 45 | }{ |
| 46 | {"answer", answers.Session(&gorm.Session{}).Where("("+contextAnswerOpen+") = ?", c.Binding.Generation, c.Binding.Generation, open).Order("m.sequence DESC,q.id")}, |
| 47 | {"plan_approval", approvals.Session(&gorm.Session{}).Where("("+contextOpenWork+") = ?", open).Order("p.created_at DESC,w.id")}, |
| 48 | } { |
| 49 | var rows []contextDecision |
| 50 | if err = source.query.Limit(limit).Scan(&rows).Error; err != nil { |
| 51 | return |
| 52 | } |
| 53 | if open && len(rows) > 1000 { |
| 54 | err = runnerproto.ErrPromptTooLarge |
| 55 | return |
| 56 | } |
| 57 | for _, a := range rows { |
| 58 | if a.ProjectID > 0 { |
| 59 | if _, checkErr := e.CheckProject(tx, c.Binding.Person, a.ProjectID, c.Brief.Selection); checkErr != nil { |
| 60 | _, err = e.contextDenied(checkErr) |
| 61 | if err != nil { |
| 62 | return |
| 63 | } |
| 64 | continue |
| 65 | } |
| 66 | } |
| 67 | ref := runnerproto.CoordinatorContextRef(source.kind, a.ID, a.Revision, a.Body) |
| 68 | if open { |
| 69 | required = append(required, ref) |
| 70 | } else { |
| 71 | optional = append(optional, ref) |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | return |
| 77 | } |
| 78 | |