An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Pause database automations with unavailable references https://github.com/siyuan-note/siyuan/issues/20415
Danielba2a683a0ad1
diff --git a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20240208172514-9dsv6na.sy b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20240208172514-9dsv6na.syindex 95882e2508..cf16fd07d5 100644--- a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20240208172514-9dsv6na.sy+++ b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20240208172514-9dsv6na.sy@@ -7,7 +7,7 @@"id": "20240208172514-9dsv6na","title": "Database","type": "doc",- "updated": "20261010104905"+ "updated": "20261010222552"},"Children": [{@@ -3786,7 +3786,7 @@"HeadingLevel": 2,"Properties": {"id": "20261002224707-n5wvv5s",- "updated": "20261003000448"+ "updated": "20261010222552"},"Children": [{@@ -3800,7 +3800,7 @@"Type": "NodeParagraph","Properties": {"id": "20261002224707-5by05dc",- "updated": "20261003000448"+ "updated": "20261010222552"},"Children": [{@@ -3824,6 +3824,10 @@{"Type": "NodeText","Data": " to add a rule. Changes are saved automatically; text is saved when the field loses focus or Enter is pressed. New rules are disabled by default. Incomplete rules are automatically disabled and show what is missing; enable them manually after completing the configuration. Rules are shared by the entire database and can be triggered by item changes in any view or mirror"+ },+ {+ "Type": "NodeText",+ "Data": ". If a referenced field is deleted or its type changes, or a target database is deleted, the next item write automatically pauses the affected rule and identifies the rule and missing reference. The source edit is saved and the rule configuration is retained; repair it and enable the rule manually. Locked databases, authentication failures, corrupt data, and write failures still roll back the edit and automatic actions"}]},diff --git a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20240208113259-nykkvaq.sy b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20240208113259-nykkvaq.syindex 92fc467735..c08e58410c 100644--- a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20240208113259-nykkvaq.sy+++ b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20240208113259-nykkvaq.sy@@ -7,7 +7,7 @@"id": "20240208113259-nykkvaq","title": "数据库","type": "doc",- "updated": "20261010104905"+ "updated": "20261010222552"},"Children": [{@@ -3786,7 +3786,7 @@"HeadingLevel": 2,"Properties": {"id": "20261002224707-l8jclpi",- "updated": "20261003000448"+ "updated": "20261010222552"},"Children": [{@@ -3800,7 +3800,7 @@"Type": "NodeParagraph","Properties": {"id": "20261002224707-5gkk78g",- "updated": "20261003000448"+ "updated": "20261010222552"},"Children": [{@@ -3824,6 +3824,10 @@{"Type": "NodeText","Data": ",添加规则。修改会自动保存,文本在失焦或回车时保存。新规则默认停用;配置不完整时会自动停用并提示缺项,完善后手动启用。规则由整个数据库共享,在任一视图或镜像中的条目修改都可触发"+ },+ {+ "Type": "NodeText",+ "Data": "。引用的字段被删除或改型、目标数据库被删除后,后续条目写入会自动停用受影响规则,并提示规则名和失效引用。用户修改正常保存,规则配置保留;修复配置后需手动启用。数据库锁定、认证失败、数据损坏或写入失败时,用户修改和自动操作仍会一起回滚"}]},diff --git a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20240208171522-y7dxcno.sy b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20240208171522-y7dxcno.syindex 736c1912f4..4763f821e1 100644--- a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20240208171522-y7dxcno.sy+++ b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20240208171522-y7dxcno.sy@@ -7,7 +7,7 @@"id": "20240208171522-y7dxcno","title": "資料庫","type": "doc",- "updated": "20261010104905"+ "updated": "20261010222552"},"Children": [{@@ -3786,7 +3786,7 @@"HeadingLevel": 2,"Properties": {"id": "20261002224707-f9wjtlr",- "updated": "20261003000448"+ "updated": "20261010222552"},"Children": [{@@ -3800,7 +3800,7 @@"Type": "NodeParagraph","Properties": {"id": "20261002224707-zvqappp",- "updated": "20261003000448"+ "updated": "20261010222552"},"Children": [{@@ -3824,6 +3824,10 @@{"Type": "NodeText","Data": ",新增規則。修改會自動儲存,文字在失焦或按下 Enter 時儲存。新規則預設停用;設定不完整時會自動停用並提示缺少的項目,完善後手動啟用。規則由整個資料庫共用,在任一檢視或鏡像中的條目修改都可觸發"+ },+ {+ "Type": "NodeText",+ "Data": "。引用的欄位被刪除或變更類型、目標資料庫被刪除後,後續條目寫入會自動停用受影響規則,並提示規則名稱及失效引用。使用者修改正常儲存,規則設定保留;修復設定後需手動啟用。資料庫鎖定、驗證失敗、資料損毀或寫入失敗時,使用者修改和自動操作仍會一起回復"}]},diff --git a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-3eaevtp.sy b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-3eaevtp.syindex cae17e9416..d2b80d16f7 100644--- a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-3eaevtp.sy+++ b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-3eaevtp.sy@@ -8,7 +8,7 @@"id": "20240530101000-3eaevtp","title": "データベース","type": "doc",- "updated": "20261010104905"+ "updated": "20261010222552"},"Children": [{@@ -3796,7 +3796,7 @@"HeadingLevel": 2,"Properties": {"id": "20261002224707-2ctdjhm",- "updated": "20261003000448"+ "updated": "20261010222552"},"Children": [{@@ -3810,7 +3810,7 @@"Type": "NodeParagraph","Properties": {"id": "20261002224707-ffzbedy",- "updated": "20261003000448"+ "updated": "20261010222552"},"Children": [{@@ -3834,6 +3834,10 @@{"Type": "NodeText","Data": " からルールを追加します。変更は自動保存され、テキストはフォーカスを外すか Enter を押すと保存されます。新しいルールは既定で無効です。設定が不完全な場合は自動的に無効になり、不足項目が表示されます。設定を完了してから手動で有効にしてください。ルールはデータベース全体で共有され、どのビューやミラーでの項目変更でも実行できます"+ },+ {+ "Type": "NodeText",+ "Data": "。参照するフィールドが削除されたり型が変更されたり、対象データベースが削除された場合、次の項目の書き込み時に該当ルールが自動的に無効になり、ルール名と無効な参照が表示されます。ユーザーの編集は保存され、ルール設定も保持されます。設定を修復した後、手動で有効にしてください。データベースのロック、認証失敗、データ破損、書き込み失敗の場合は、編集と自動操作が引き続き一緒にロールバックされます"}]},diff --git a/app/src/types/api/index.d.ts b/app/src/types/api/index.d.tsindex f278222241..ceddb0bcd0 100644--- a/app/src/types/api/index.d.ts+++ b/app/src/types/api/index.d.ts@@ -7650,6 +7650,8 @@ export interface APIPOSTRoutes {* addAttributeViewBlocks、setAttributeViewBlockAttr、batchSetAttributeViewBlockAttrs 触发启用的新增或字段变化规则。* 自动操作与原修改一同提交,失败一起回滚;普通 API 写入不生成编辑器撤销记录。* 自动化不串联,导入、同步、历史恢复和撤销重放不重新触发;重做保留原条目 ID 和触发时间。+ * 字段失效或目标数据库被删除时,后续条目写入停用规则并保留配置,源修改仍提交;提交后提示失效引用。+ * 修复后需手动启用,撤销源修改不重新启用规则;锁定、认证失败、损坏及实际写入失败仍一起回滚。* 跨库动作限于同一加密边界,要求目标可访问;单笔事务最多执行 1000 个自动操作。*/"/api/transactions": {diff --git a/kernel/api/contract_av_automation_test.go b/kernel/api/contract_av_automation_test.goindex cf369c10fa..fedda51c8d 100644--- a/kernel/api/contract_av_automation_test.go+++ b/kernel/api/contract_av_automation_test.go@@ -122,4 +122,23 @@ func TestAVContractAutomationWrites(t *testing.T) {t.Fatalf("cell API did not trigger automation (batch=%v)", batch)}}+ if err = model.PerformAttributeViewOperations([]*model.Operation{{Action: "removeAttrViewCol", AvID: database.ID,+ BlockID: fixture.databaseID, ID: statusID}}); err != nil {+ t.Fatal(err)+ }+ call("/api/av/setAttributeViewBlockAttr", map[string]any{"avID": database.ID,+ "keyID": fixture.textKeyID, "itemID": itemID, "value": map[string]any{"text": map[string]any{"content": "after deletion"}},+ }, setAttributeViewBlockAttr)+ stored, err = av.ParseAttributeView(database.ID)+ if err != nil {+ t.Fatal(err)+ }+ if stored.Automations.Rules[1].Enabled || stored.Automations.Rules[1].Actions[0].Fields[statusID] == nil ||+ stored.GetValue(fixture.textKeyID, itemID).Text.Content != "after deletion" {+ t.Fatal("deleted action field blocked the API edit or discarded the rule configuration")+ }+ call("/api/av/batchSetAttributeViewBlockAttrs", map[string]any{"avID": database.ID,+ "values": []map[string]any{{"keyID": fixture.textKeyID, "itemID": itemID,+ "value": map[string]any{"text": map[string]any{"content": "later batch"}}}},+ }, batchSetAttributeViewBlockAttrs)}diff --git a/kernel/apicontract/av_automation.go b/kernel/apicontract/av_automation.goindex dfd87d6ec8..95c18e3b6b 100644--- a/kernel/apicontract/av_automation.go+++ b/kernel/apicontract/av_automation.go@@ -4,7 +4,9 @@ package apicontract// spec 为 1;缺省配置不会触发自动化。普通条目新增、字段编辑及对应 API 会触发启用的规则,// 导入、同步、历史恢复和撤销重放不会再次触发,自动化之间不串联。// 自动操作与源修改一同提交、失败回滚及撤销;单笔事务最多产生 1000 个自动操作。-// 跨库操作仅限同一加密边界,锁定、缺失或损坏的目标会使触发事务失败。+// 字段失效或目标数据库被删除时,后续条目写入会停用规则并保留配置,源修改仍可提交。+// 停用提示在提交后发送,包含规则名及失效引用;修复后需手动启用,撤销源修改不会重新启用规则。+// 跨库操作仅限同一加密边界,锁定、认证失败、损坏或实际写入失败仍使触发事务回滚。type AVAutomationConfig struct {Spec int `json:"spec" api:"const=1"`Rules []*AVAutomationRule `json:"rules"`diff --git a/kernel/apicontract/contracts.go b/kernel/apicontract/contracts.goindex ff7f6b94c3..a32e227a2d 100644--- a/kernel/apicontract/contracts.go+++ b/kernel/apicontract/contracts.go@@ -1333,6 +1333,8 @@ var SystemAddUIProcess = define[EmptyRequest, Null]("addUIProcess", "/api/system// addAttributeViewBlocks、setAttributeViewBlockAttr、batchSetAttributeViewBlockAttrs 触发启用的新增或字段变化规则。// 自动操作与原修改一同提交,失败一起回滚;普通 API 写入不生成编辑器撤销记录。// 自动化不串联,导入、同步、历史恢复和撤销重放不重新触发;重做保留原条目 ID 和触发时间。+// 字段失效或目标数据库被删除时,后续条目写入停用规则并保留配置,源修改仍提交;提交后提示失效引用。+// 修复后需手动启用,撤销源修改不重新启用规则;锁定、认证失败、损坏及实际写入失败仍一起回滚。// 跨库动作限于同一加密边界,要求目标可访问;单笔事务最多执行 1000 个自动操作。var PerformTransactions = define[PerformTransactionsRequest, []*Transaction]("performTransactions", "/api/transactions", AuthenticatedAccess|AdminAccess|WritableAccess, JSONBody, ResponseOptions{}, "POST")var UndoState = define[TransactionUndoStateRequest, TransactionUndoState]("undoState", "/api/transactions/undoState", AuthenticatedAccess, JSONBody, ResponseOptions{}, "POST")diff --git a/kernel/model/attribute_view_automation.go b/kernel/model/attribute_view_automation.goindex 5acdfa1390..4392b24b56 100644--- a/kernel/model/attribute_view_automation.go+++ b/kernel/model/attribute_view_automation.go@@ -2,7 +2,10 @@ package modelimport ("encoding/json"+ "errors""fmt"+ "os"+ "path/filepath""reflect""slices""sort"@@ -13,6 +16,7 @@ import ("github.com/88250/lute/parse""github.com/siyuan-note/siyuan/kernel/av""github.com/siyuan-note/siyuan/kernel/filesys"+ "github.com/siyuan-note/siyuan/kernel/util")const maxAutomationOperations = 1000@@ -29,6 +33,33 @@ type attributeViewAutomationState struct {time int64count intrecordUndo bool+ paused []automationPausedRule+}++type automationUnavailableReference struct {+ kind, id string+ err error+}++func (e *automationUnavailableReference) Error() string { return e.err.Error() }+func (e *automationUnavailableReference) Unwrap() error { return e.err }++type automationPausedRule struct {+ name string+ reference *automationUnavailableReference+}++func unavailableAutomationReference(kind, id, message string) error {+ return &automationUnavailableReference{kind: kind, id: id, err: errors.New(message)}+}++// 提示仅在事务提交后发送,不把内部错误或认证信息作为界面文案。+func (paused automationPausedRule) message() string {+ detail := paused.name + " - " + util.I18nTerm(Conf.Lang, paused.reference.kind)+ if paused.reference.id != "" {+ detail += " [" + paused.reference.id + "]"+ }+ return strings.ReplaceAll(util.I18nTerm(Conf.Lang, "automationIncomplete"), "${1}", util.EscapeHTML(detail))}// 仅用户及 API 的条目写入产生触发事件,撤销重放只执行已记录的操作。@@ -119,6 +150,14 @@ func (tx *Transaction) rememberAutomationView(view *av.AttributeView, blockID stcontinue}original, err := tx.readAttributeViewForMutation(id, "", boxID)+ if id != view.ID && errors.Is(err, av.ErrViewNotFound) {+ original, err = tx.readAutomationTarget(id, boxID)+ }+ var unavailable *automationUnavailableReference+ if id != view.ID && errors.As(err, &unavailable) {+ // 已删除的关联目标没有可补偿的快照,失效规则由执行前的引用校验停用。+ continue+ }if err != nil {return err}@@ -174,10 +213,19 @@ func (tx *Transaction) runAttributeViewAutomations() error {current := state.current[source.before.ID]added, changed := automationChangedItems(source.before, current)relationValues := tx.automationRelationValues(source.boxID, state.current)- for _, rule := range source.before.Automations.Rules {+ paused := false+ for _, rule := range current.Automations.Rules {if rule == nil || !rule.Enabled {continue}+ validationErr := tx.validateAutomationRule(current, rule, source.boxID)+ var unavailable *automationUnavailableReference+ if errors.As(validationErr, &unavailable) {+ rule.Enabled = false+ paused = true+ state.paused = append(state.paused, automationPausedRule{rule.Name, unavailable})+ continue+ }for _, row := range current.GetBlockKeyValues().Values {matchesTrigger := rule.Trigger == "added" && added[row.BlockID] || rule.Trigger == "changed" && changed[rule.KeyID][row.BlockID]if !matchesTrigger {@@ -193,15 +241,22 @@ func (tx *Transaction) runAttributeViewAutomations() error {if !matches {continue}+ if validationErr != nil {+ return fmt.Errorf("automation [%s]: %w", rule.ID, validationErr)+ }triggered = append(triggered, triggeredRule{source, current, rule, row.BlockID})}}+ if paused {+ // 仅改变启用状态,保留失效引用供用户修复;失败时随源数据库快照一起恢复。+ if err := avSaveView(current, source.blockID); err != nil {+ return err+ }+ ReloadAttrView(current.ID)+ }}// 先确定全部触发条件,关联关键词也不会读到前一自动动作修改后的标题。for _, entry := range triggered {- if err := tx.validateAutomationRule(entry.current, entry.rule, entry.source.boxID); err != nil {- return fmt.Errorf("automation [%s]: %w", entry.rule.ID, err)- }for _, action := range entry.rule.Actions {if err := tx.runAutomationAction(entry.source, entry.current, entry.itemID, action); err != nil {return fmt.Errorf("automation [%s]: %w", entry.rule.ID, err)@@ -339,14 +394,58 @@ func (tx *Transaction) automationTarget(source *av.AttributeView, action *av.Aut} else if action.Target == "related" {key, err := source.GetKey(action.RelationKeyID)if err != nil || key.Relation == nil || key.Relation.AvID == "" {- return nil, fmt.Errorf("automation relation field [%s] is unavailable", action.RelationKeyID)+ return nil, unavailableAutomationReference("fields", action.RelationKeyID,+ fmt.Sprintf("automation relation field [%s] is unavailable", action.RelationKeyID))}id = key.Relation.AvID}if !ast.IsNodeIDPattern(id) {return nil, fmt.Errorf("invalid automation target database")}- return tx.readAttributeViewForMutation(id, "", boxID)+ return tx.readAutomationTarget(id, boxID)+}++// 仅确认文件不存在时将目标视为失效引用,访问失败和读取竞态仍交由事务处理。+func (tx *Transaction) readAutomationTarget(id, boxID string) (*av.AttributeView, error) {+ target, err := tx.readAttributeViewForMutation(id, "", boxID)+ if errors.Is(err, av.ErrViewNotFound) {+ _, statErr := os.Stat(filepath.Join(util.DataDir, boxID, "storage", "av", id+".json"))+ if os.IsNotExist(statErr) {+ err = &automationUnavailableReference{kind: "database", id: id, err: err}+ } else if statErr != nil {+ err = statErr+ }+ }+ return target, err+}++// 在已认证的数据库定义上识别失效字段及关联目标;其他读取和配置错误仍返回给事务。+func (tx *Transaction) validateAutomationFilters(view *av.AttributeView, filters []*av.ViewFilter, boxID string) error {+ if err := av.ValidateFilterDepth(filters); err != nil {+ return err+ }+ for _, filter := range filters {+ if filter == nil {+ continue+ }+ if filter.IsGroup() {+ if err := tx.validateAutomationFilters(view, filter.Filters, boxID); err != nil {+ return err+ }+ continue+ }+ key, err := view.GetKey(filter.Column)+ if err != nil || !av.AutomationEditableKey(key.Type) || filter.Value != nil && filter.Value.Type != key.Type {+ return unavailableAutomationReference("fields", filter.Column,+ fmt.Sprintf("automation condition field [%s] is unavailable", filter.Column))+ }+ if key.Type == av.KeyTypeRelation {+ if _, err = tx.automationTarget(view, &av.AutomationAction{Target: "related", RelationKeyID: key.ID}, boxID); err != nil {+ return err+ }+ }+ }+ return av.ValidateAutomationFilters(view, filters)}func (tx *Transaction) validateAutomationRule(source *av.AttributeView, rule *av.AutomationRule, boxID string) error {@@ -356,10 +455,11 @@ func (tx *Transaction) validateAutomationRule(source *av.AttributeView, rule *avif rule.KeyID != "" {key, err := source.GetKey(rule.KeyID)if err != nil || !av.AutomationEditableKey(key.Type) {- return fmt.Errorf("automation trigger field [%s] is unavailable", rule.KeyID)+ return unavailableAutomationReference("fields", rule.KeyID,+ fmt.Sprintf("automation trigger field [%s] is unavailable", rule.KeyID))}}- if err := av.ValidateAutomationFilters(source, rule.Conditions); err != nil {+ if err := tx.validateAutomationFilters(source, rule.Conditions, boxID); err != nil {return err}for _, action := range rule.Actions {@@ -371,34 +471,41 @@ func (tx *Transaction) validateAutomationRule(source *av.AttributeView, rule *avif err != nil {return err}- if err = av.ValidateAutomationFilters(target, action.Filters); err != nil {+ if err = tx.validateAutomationFilters(target, action.Filters, boxID); err != nil {return err}for keyID, field := range action.Fields {+ if field == nil {+ return fmt.Errorf("invalid automation action value [%s]", keyID)+ }key, keyErr := target.GetKey(keyID)- if keyErr != nil || !av.AutomationEditableKey(key.Type) || field == nil {- return fmt.Errorf("automation action field [%s] is unavailable", keyID)+ if keyErr != nil || !av.AutomationEditableKey(key.Type) {+ return unavailableAutomationReference("fields", keyID,+ fmt.Sprintf("automation action field [%s] is unavailable", keyID))}switch field.Mode {case "currentTime":if key.Type != av.KeyTypeDate {- return fmt.Errorf("automation trigger time requires a date field")+ return unavailableAutomationReference("fields", keyID, "automation trigger time requires a date field")}case "triggerItem":if key.Type != av.KeyTypeRelation || key.Relation == nil || key.Relation.AvID != source.ID {- return fmt.Errorf("automation trigger item requires a relation to the source database")+ return unavailableAutomationReference("fields", keyID, "automation trigger item requires a relation to the source database")}case "static":- if field.Value == nil || field.Value.Type != key.Type {- return fmt.Errorf("automation field value type mismatch [%s]", keyID)+ if field.Value == nil {+ return fmt.Errorf("invalid automation static value [%s]", keyID)+ }+ if field.Value.Type != key.Type {+ return unavailableAutomationReference("fields", keyID, fmt.Sprintf("automation field value type mismatch [%s]", keyID))}case "source":sourceKey, sourceErr := source.GetKey(field.KeyID)if sourceErr != nil || sourceKey.Type != key.Type && !(sourceKey.Type == av.KeyTypeBlock && key.Type == av.KeyTypeText) {- return fmt.Errorf("automation source field type mismatch [%s]", keyID)+ return unavailableAutomationReference("fields", field.KeyID, fmt.Sprintf("automation source field type mismatch [%s]", field.KeyID))}if key.Type == av.KeyTypeRelation && (sourceKey.Relation == nil || key.Relation == nil || sourceKey.Relation.AvID != key.Relation.AvID) {- return fmt.Errorf("automation relation target mismatch [%s]", keyID)+ return unavailableAutomationReference("fields", keyID, fmt.Sprintf("automation relation target mismatch [%s]", keyID))}default:return fmt.Errorf("invalid automation value mode")diff --git a/kernel/model/attribute_view_automation_fts5_test.go b/kernel/model/attribute_view_automation_fts5_test.goindex 86bddc459b..6900729649 100644--- a/kernel/model/attribute_view_automation_fts5_test.go+++ b/kernel/model/attribute_view_automation_fts5_test.go@@ -4,6 +4,7 @@ package modelimport ("bytes"+ "encoding/json""errors""fmt""os"@@ -22,6 +23,237 @@ import ("github.com/siyuan-note/siyuan/kernel/util")+func TestAutomationUnavailableReferencesPauseAndPreserveEdits(t *testing.T) {+ for _, missing := range []string{"action-field", "changed-field-type", "condition-field", "action-filter", "trigger-field", "source-field", "related-field", "database", "related-condition-database"} {+ t.Run(missing, func(t *testing.T) {+ fixture, source, target, statusID, logsID := setupAutomationTest(t)+ rule := source.Automations.Rules[0]+ source.Automations.Rules = source.Automations.Rules[:1]+ action := rule.Actions[0]+ removedID := target.KeyValues[2].Key.ID+ switch missing {+ case "action-field":+ removeAttributeViewFieldDefinition(target, removedID)+ case "changed-field-type":+ key, _ := target.GetKey(removedID)+ key.Type = av.KeyTypeText+ case "condition-field":+ removedID = ast.NewNodeID()+ rule.Conditions[0].Column = removedID+ case "action-filter":+ action.Filters = []*av.ViewFilter{{Column: removedID, Operator: av.FilterOperatorIsEmpty, Value: &av.Value{Type: av.KeyTypeDate}}}+ delete(action.Fields, removedID)+ removeAttributeViewFieldDefinition(target, removedID)+ case "trigger-field":+ removedID = ast.NewNodeID()+ rule.KeyID = removedID+ case "source-field":+ removedID = ast.NewNodeID()+ action.Fields[target.GetBlockKey().ID].KeyID = removedID+ case "related-field":+ removedID = logsID+ action.Target, action.RelationKeyID = "related", logsID+ removeAttributeViewFieldDefinition(source, logsID)+ case "related-condition-database":+ rule.Conditions = []*av.ViewFilter{{Column: logsID, Operator: av.FilterOperatorIsNotEmpty, Value: &av.Value{Type: av.KeyTypeRelation}}}+ fallthrough+ case "database":+ removedID = target.ID+ }+ for _, view := range []*av.AttributeView{source, target} {+ if err := av.SaveAttributeView(view); err != nil {+ t.Fatal(err)+ }+ }+ if missing == "database" || missing == "related-condition-database" {+ if err := os.Remove(filepath.Join(util.DataDir, "storage", "av", target.ID+".json")); err != nil {+ t.Fatal(err)+ }+ cache.ClearAVCache()+ }+ rule.Enabled = false+ wantConfig, _ := json.Marshal(source.Automations)+ rule.Enabled = true+ itemID := source.GetBlockKeyValues().Values[0].BlockID+ tx := automationStatusTransaction(source, fixture.sourceID, statusID, itemID, "Active")+ if err := PerformTxSync(tx); err != nil {+ t.Fatalf("missing reference %s rolled back the source edit: %v", removedID, err)+ }+ stored := readAttributeViewItemsTest(t, source.ID)+ gotConfig, _ := json.Marshal(stored.Automations)+ if !bytes.Equal(wantConfig, gotConfig) || stored.GetValue(statusID, itemID).MSelect[0].Content != "Active" {+ t.Fatalf("paused rule lost configuration or edit: %s", gotConfig)+ }+ if err := PerformTxSync(&Transaction{DoOperations: cloneOperations(tx.UndoOperations), isReplay: true}); err != nil {+ t.Fatal(err)+ }+ if readAttributeViewItemsTest(t, source.ID).Automations.Rules[0].Enabled {+ t.Fatal("undo re-enabled an unavailable rule")+ }+ if err := PerformTxSync(&Transaction{DoOperations: cloneOperations(tx.DoOperations), isReplay: true}); err != nil {+ t.Fatal(err)+ }+ if err := PerformTxSync(automationStatusTransaction(stored, fixture.sourceID, statusID, itemID, "Done")); err != nil {+ t.Fatal("a later edit failed after the rule was paused", err)+ }+ })+ }+}++func TestAutomationPausedRuleRollsBackOnCommitFailure(t *testing.T) {+ fixture, source, target, statusID, _ := setupAutomationTest(t)+ removeAttributeViewFieldDefinition(target, target.KeyValues[2].Key.ID)+ if err := av.SaveAttributeView(target); err != nil {+ t.Fatal(err)+ }+ itemID := source.GetBlockKeyValues().Values[0].BlockID+ tx := automationStatusTransaction(source, fixture.sourceID, statusID, itemID, "Active")+ injected := errors.New("injected commit failure after pausing")+ tx.writeTransactionTree = func(*parse.Tree) error { return injected }+ if err := PerformTxSync(tx); err == nil || !strings.Contains(err.Error(), injected.Error()) {+ t.Fatal("commit failure was ignored", err)+ }+ stored := readAttributeViewItemsTest(t, source.ID)+ if !stored.Automations.Rules[0].Enabled || stored.GetValue(statusID, itemID).MSelect[0].Content != "Alpha" {+ t.Fatal("failed commit retained the paused rule or source edit")+ }+}++func TestAutomationMalformedActionStillRollsBack(t *testing.T) {+ fixture, source, _, statusID, _ := setupAutomationTest(t)+ rule := source.Automations.Rules[0]+ rule.Actions[0].Fields = map[string]*av.AutomationValue{source.GetBlockKey().ID: nil}+ if err := av.SaveAttributeView(source); err != nil {+ t.Fatal(err)+ }+ itemID := source.GetBlockKeyValues().Values[0].BlockID+ if err := PerformTxSync(automationStatusTransaction(source, fixture.sourceID, statusID, itemID, "Active")); err == nil {+ t.Fatal("malformed rule data was treated as a removed field")+ }+ stored := readAttributeViewItemsTest(t, source.ID)+ if !stored.Automations.Rules[0].Enabled || stored.GetValue(statusID, itemID).MSelect[0].Content != "Alpha" {+ t.Fatal("malformed rule data lost the original configuration or source content")+ }+}++func TestAutomationUnmatchedRuleKeepsActionReadErrorsDeferred(t *testing.T) {+ fixture, source, target, statusID, logsID := setupAutomationTest(t)+ source.Automations.Rules = source.Automations.Rules[:1]+ key, _ := source.GetKey(logsID)+ key.Relation.IsTwoWay = false+ if err := av.SaveAttributeView(source); err != nil {+ t.Fatal(err)+ }+ path := filepath.Join(util.DataDir, "storage", "av", target.ID+".json")+ before := []byte("corrupted target")+ if err := os.WriteFile(path, before, 0600); err != nil {+ t.Fatal(err)+ }+ cache.ClearAVCache()+ itemID := source.GetBlockKeyValues().Values[0].BlockID+ if err := PerformTxSync(automationStatusTransaction(source, fixture.sourceID, statusID, itemID, "Done")); err != nil {+ t.Fatal("an unmatched rule tried to execute its action", err)+ }+ stored := readAttributeViewItemsTest(t, source.ID)+ if !stored.Automations.Rules[0].Enabled || stored.GetValue(statusID, itemID).MSelect[0].Content != "Done" {+ t.Fatal("an unmatched rule was paused or its unrelated source edit was lost")+ }+ after, err := os.ReadFile(path)+ if err != nil || !bytes.Equal(before, after) {+ t.Fatal("an unmatched rule changed its unreadable target")+ }+ if err := PerformTxSync(automationStatusTransaction(stored, fixture.sourceID, statusID, itemID, "Active")); err == nil {+ t.Fatal("a matching rule accepted corrupted target data")+ }+ stored = readAttributeViewItemsTest(t, source.ID)+ if !stored.Automations.Rules[0].Enabled || stored.GetValue(statusID, itemID).MSelect[0].Content != "Done" {+ t.Fatal("a failed matching rule changed the source configuration or content")+ }+}++func TestAutomationPausesOnlyUnavailableRule(t *testing.T) {+ fixture, source, target, statusID, _ := setupAutomationTest(t)+ removeAttributeViewFieldDefinition(target, target.KeyValues[2].Key.ID)+ if err := av.SaveAttributeView(target); err != nil {+ t.Fatal(err)+ }+ valid := source.Automations.Rules[1]+ valid.Conditions = nil+ valid.Actions = []*av.AutomationAction{{Type: "edit", Target: "current", Fields: map[string]*av.AutomationValue{+ source.GetBlockKey().ID: {Mode: "static", Value: &av.Value{Type: av.KeyTypeBlock, Block: &av.ValueBlock{Content: "Valid rule"}}},+ }}}+ if err := av.SaveAttributeView(source); err != nil {+ t.Fatal(err)+ }+ itemID := source.GetBlockKeyValues().Values[0].BlockID+ if err := PerformTxSync(automationStatusTransaction(source, fixture.sourceID, statusID, itemID, "Active")); err != nil {+ t.Fatal(err)+ }+ stored := readAttributeViewItemsTest(t, source.ID)+ if stored.Automations.Rules[0].Enabled || !stored.Automations.Rules[1].Enabled || stored.GetBlockValue(itemID).Block.Content != "Valid rule" {+ t.Fatal("pausing the unavailable rule prevented another valid rule from running")+ }+}++func TestAutomationPauseMessageIdentifiesMissingReference(t *testing.T) {+ previousDir, previousConf := util.WorkingDir, Conf+ util.WorkingDir, _ = filepath.Abs("../../app")+ Conf = NewAppConf()+ Conf.Lang = "en"+ defer func() { util.WorkingDir, Conf = previousDir, previousConf }()+ message := (automationPausedRule{"Start <script>", &automationUnavailableReference{kind: "fields", id: "missing-field"}}).message()+ if !strings.Contains(message, "Rule paused") || !strings.Contains(message, "Start") || !strings.Contains(message, "missing-field") {+ t.Fatal("pause message omits the rule or missing field", message)+ }+ if strings.Contains(message, "<script>") || !strings.Contains(message, "<script>") {+ t.Fatal("pause message rendered the rule name as HTML", message)+ }+}++func TestAutomationEncryptedUnavailableReferencePause(t *testing.T) {+ _, source, target, statusID, _ := setupAutomationTest(t)+ boxID := ast.NewNodeID()+ markRuntimeEncryptedBox(boxID)+ setDEKForTest(boxID, bytes.Repeat([]byte{0x72}, 32))+ t.Cleanup(func() {+ for _, view := range []*av.AttributeView{source, target} {+ av.SetAVBoxID(view.ID, "")+ }+ forgetRuntimeEncryptedBox(boxID)+ encryptedBoxLifecycles.Delete(boxID)+ cachedDEKsLock.Lock()+ delete(cachedDEKs, boxID)+ cachedDEKsLock.Unlock()+ })+ removeAttributeViewFieldDefinition(target, target.KeyValues[2].Key.ID)+ for _, view := range []*av.AttributeView{source, target} {+ av.SetAVBoxID(view.ID, boxID)+ if err := av.SaveAttributeView(view); err != nil {+ t.Fatal(err)+ }+ if err := os.Remove(filepath.Join(util.DataDir, "storage", "av", view.ID+".json")); err != nil {+ t.Fatal(err)+ }+ }+ itemID := source.GetBlockKeyValues().Values[0].BlockID+ if err := PerformTxSync(automationStatusTransaction(source, "", statusID, itemID, "Active")); err != nil {+ t.Fatal(err)+ }+ stored, err := av.ParseAttributeViewForIndexInBox(source.ID, boxID)+ if err != nil || stored.Automations.Rules[0].Enabled || stored.GetValue(statusID, itemID).MSelect[0].Content != "Active" {+ t.Fatal("encrypted edit did not pause the unavailable rule", err)+ }+ for _, view := range []*av.AttributeView{source, target} {+ data, err := os.ReadFile(filepath.Join(util.DataDir, boxID, "storage", "av", view.ID+".json"))+ if err != nil || !util.IsCiphertext(data) {+ t.Fatal("pausing an encrypted rule did not preserve ciphertext storage", err)+ }+ if _, err := os.Stat(filepath.Join(util.DataDir, "storage", "av", view.ID+".json")); !os.IsNotExist(err) {+ t.Fatal("pausing an encrypted rule created a plaintext copy", err)+ }+ }+}+func TestAutomationAddIgnoresMirrorContext(t *testing.T) {for _, sameDatabase := range []bool{false, true} {for _, boundDocument := range []bool{false, true} {diff --git a/kernel/model/attribute_view_transaction.go b/kernel/model/attribute_view_transaction.goindex a887bc6e2d..3e97df1387 100644--- a/kernel/model/attribute_view_transaction.go+++ b/kernel/model/attribute_view_transaction.go@@ -125,6 +125,11 @@ func (tx *Transaction) invalidateAttributeViewHistory(avID string) {func (tx *Transaction) finishAttributeViewMutation(rollback bool) {if !rollback {+ if automation := tx.attributeViewAutomations; automation != nil {+ for _, paused := range automation.paused {+ util.PushMsg(paused.message(), 7000)+ }+ }for avID := range tx.invalidatedAvHistory {GlobalUndoLog.ClearAttributeView(avID)}