checkStartWrite runs under the workspace write lock through the item write hooks: every create door (createItemChecked), item update, and bulk assign. Item restore calls it through checkStartRestore before the restore, and a collection move applies the owner rule itself. Only a workspace import or b
(q store.Queryer, r *http.Request, coll *models.Collection, before, after *models.Item, demoted bool)
| 44 | // collection move applies the owner rule itself. Only a workspace import or |
| 45 | // backup restore skips it; bootstrap reports any clash as start.conflicts. |
| 46 | func (s *Server) checkStartWrite(q store.Queryer, r *http.Request, coll *models.Collection, before, after *models.Item, demoted bool) error { |
| 47 | if !isStartSlotCollection(coll) { |
| 48 | return nil |
| 49 | } |
| 50 | oldMode, newMode := startMode(before), startMode(after) |
| 51 | oldRole := "" |
| 52 | if before != nil && before.AgentRoleID != nil { |
| 53 | oldRole = *before.AgentRoleID |
| 54 | } |
| 55 | newRole := "" |
| 56 | if after.AgentRoleID != nil { |
| 57 | newRole = *after.AgentRoleID |
| 58 | } |
| 59 | if demoted && oldMode == "run" { |
| 60 | oldMode = "offer" |
| 61 | } |
| 62 | changed := oldMode != newMode || ((oldMode != "" || newMode != "") && oldRole != newRole) |
| 63 | activated := before != nil && startMode(after) != "" && startFields(before)["status"] != "active" && startFields(after)["status"] == "active" |
| 64 | // TASK-4926: setting, changing or clearing the start condition of an |
| 65 | // item that has or gets a start mode is owner-only too. It is not part |
| 66 | // of changed: a condition-only change takes no slot. |
| 67 | conditioned := (oldMode != "" || newMode != "") && collections.SchemaHasStartWhen(coll.Schema) && |
| 68 | !reflect.DeepEqual(startWhenValue(before), startWhenValue(after)) |
| 69 | if (changed || activated || conditioned) && !requireRole(r, "owner") { |
| 70 | reason := "" |
| 71 | if activated { |
| 72 | reason = "activation" |
| 73 | } else if conditioned && !changed { |
| 74 | reason = "condition" |
| 75 | } |
| 76 | return &startWriteError{code: "start_owner_only", reason: reason} |
| 77 | } |
| 78 | if !changed || (newMode != "run" && newMode != "offer") { |
| 79 | return nil |
| 80 | } |
| 81 | holder, err := s.store.StartSlotHolderQ(q, after.WorkspaceID, coll.ID, after.ID, newRole) |
| 82 | if err != nil { |
| 83 | return err |
| 84 | } |
| 85 | if holder != "" { |
| 86 | return &startWriteError{code: "start_playbook_taken", holder: holder} |
| 87 | } |
| 88 | return nil |
| 89 | } |
| 90 | |
| 91 | // startWhenValue is an item's start_when, with absent, null and "" all the |
| 92 | // same: no condition. |
no test coverage detected