MCPcopy Create free account
hub / github.com/PerpetualSoftware/pad / checkStartWrite

Method checkStartWrite

internal/server/start_write.go:46–89  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
46func (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.

Callers 5

checkStartRestoreMethod · 0.95
applyBulkOpMethod · 0.95
bulkFieldUpdateMethod · 0.95
createItemCheckedMethod · 0.95
updateItemMethod · 0.95

Calls 7

SchemaHasStartWhenFunction · 0.92
isStartSlotCollectionFunction · 0.85
startModeFunction · 0.85
startFieldsFunction · 0.85
startWhenValueFunction · 0.85
requireRoleFunction · 0.85
StartSlotHolderQMethod · 0.80

Tested by

no test coverage detected