-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsupertag-automation.el
More file actions
1507 lines (1340 loc) · 66.6 KB
/
Copy pathsupertag-automation.el
File metadata and controls
1507 lines (1340 loc) · 66.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; supertag-automation.el --- Unified Automation System for Supertag -*- lexical-binding: t; -*-
;;; Commentary:
;; This file implements the complete Automation System 2.0 for the
;; Supertag system. It unifies all automation-related functionality
;; including:
;;
;; 1. Rule indexing and CRUD operations
;; 2. Event-driven automation execution
;; 3. Real-time field synchronization between related entities
;; 4. Rollup calculations and formula fields
;; 5. Bidirectional relation updates
;; 6. Database-level automation rules
;;
;; Key design principles:
;; 1. Pure data-centric architecture with single source of truth
;; 2. Automatic rule indexing for O(1) performance
;; 3. No manual behavior attachment - rules are applied via index
;; 4. Modern automation with multi-action support
;; 5. Unified state management and execution engine
;;; Code:
(require 'cl-lib)
(require 'supertag-core-index)
(require 'supertag-core-store) ; For data storage operations
(require 'supertag-core-schema) ; For validation functions
(require 'supertag-core-transform) ; For atomic data updates
(require 'supertag-core-notify) ; For event notifications
(require 'supertag-core-state) ; For state management
;; Declare function from supertag-automation-sync to avoid circular dependency
(declare-function supertag-automation-sync-handle-event "supertag-automation-sync"
(operation collection id payload previous &rest metadata))
(require 'supertag-ops-relation) ; For relation operations
(require 'supertag-ops-tag) ; For tag operations
(require 'supertag-ops-node) ; For node operations
(require 'supertag-ops-field) ; For field operations
(require 'supertag-service-org)
(require 'supertag-services-query)
(require 'supertag-services-formula)
(require 'org-id) ; For ID generation
(require 'ht)
;;; Customization
(defgroup supertag-automation nil
"Automation settings for Supertag."
:group 'supertag)
(defcustom supertag-automation-verbose nil
"When non-nil, log verbose automation diagnostics.
This controls routine messages such as rule execution traces and no-op action
skips. Errors and warnings are still reported regardless of this flag."
:type 'boolean
:group 'supertag-automation)
(defun supertag-automation--log (format-string &rest args)
"Log FORMAT-STRING with ARGS when `supertag-automation-verbose' is non-nil."
(when supertag-automation-verbose
(apply #'message format-string args)))
;;; --- Core State Management ---
(defun supertag-automation--ensure-plist (data)
"Return a plist copy of DATA, converting hash tables when necessary.
This function preserves all data including tags and fields."
(cond
((null data) nil)
((hash-table-p data)
;; Convert hash table to plist while preserving all data
(let (plist)
(maphash (lambda (k v)
;; Ensure we don't lose data during conversion
(if (eq k :tags)
;; Special handling for tags to ensure list format
(setq plist (plist-put plist :tags (if (listp v) v (list v))))
(setq plist (plist-put plist k v))))
data)
plist))
((listp data)
(copy-tree data))
(t
(error "Unsupported automation entity format: %S" data))))
(defun supertag-automation--normalize-keyword (name)
"Normalize NAME into a keyword symbol."
(cond
((keywordp name) name)
((symbolp name) (intern (concat ":" (symbol-name name))))
((stringp name) (intern (concat ":" name)))
(t (error "Unsupported property key: %S" name))))
(defvar supertag-automation--enabled t
"Global flag to enable/disable automation execution.")
(defvar supertag-automation--executing nil
"Flag to indicate if automation is currently executing actions.
This prevents recursive automation triggers during action execution.")
(defvar supertag-automation--event-queue nil
"Queue of pending automation events to be processed asynchronously.
Each event is a list: (event-handler-fn args...)")
(defvar supertag-automation--processing-timer nil
"Timer for processing queued automation events.")
(defvar supertag-automation--processing-queue nil
"Queue for automation tasks to prevent infinite loops.")
(defvar supertag-automation--active-calculations (make-hash-table :test 'equal)
"Hash table tracking active rollup calculations to prevent recursion.")
(defvar supertag-automation--sync-cache (make-hash-table :test 'equal)
"Cache for field sync operations to improve performance.")
(defvar supertag-automation--current-event nil
"Dynamically-bound event context during condition evaluation.
Plist keys:
:path - store event path
:old - old value at that path
:new - new value at that path
:tag-event - when tag event happens, either :added or :removed
:tag - the tag name for a tag event")
(defun supertag-automation--event-type (&optional event)
"Return a keyword describing EVENT type.
EVENT defaults to `supertag-automation--current-event'."
(let* ((ev (or event supertag-automation--current-event))
(path (plist-get ev :path)))
(cond
((plist-member ev :tag-event) :tag-change)
((and (listp path) (eq (car path) :field-values)) :global-field-change)
((and (listp path) (eq (car path) :nodes)
(>= (length path) 4)
(eq (nth 2 path) :properties))
:property-change)
((and (listp path) (eq (car path) :nodes)) :node-change)
(t :unknown))))
(defun supertag-automation--trigger-match-p (trigger &optional event)
"Return non-nil when TRIGGER matches EVENT.
EVENT defaults to `supertag-automation--current-event'. Unknown triggers
do not match (fail closed)."
(let* ((ev (or event supertag-automation--current-event))
(normalized (if (fboundp 'supertag-automation--normalize-trigger)
(supertag-automation--normalize-trigger trigger)
trigger))
(event-type (supertag-automation--event-type ev))
(tag-op (plist-get ev :tag-event))
(tag (plist-get ev :tag)))
(pcase normalized
((or 'nil :always) t)
(:on-change (not (eq event-type :unknown)))
(:on-field-change (eq event-type :global-field-change))
(:on-property-change (memq event-type '(:property-change :global-field-change)))
(:on-schedule nil)
(:manual nil)
(`(:on-tag-added ,tag-name)
(and (eq event-type :tag-change)
(eq tag-op :added)
(equal tag-name tag)))
(`(:on-tag-removed ,tag-name)
(and (eq event-type :tag-change)
(eq tag-op :removed)
(equal tag-name tag)))
(_
(supertag-automation--log "Automation: unknown trigger %S (event=%S)" trigger ev)
nil))))
;;; --- Automation Rule Indexing System ---
(defvar supertag--rule-index (make-hash-table :test 'equal)
"The master index for automation rules.
This data structure allows for O(1) lookup of relevant rules
based on a trigger event, avoiding a full scan of all rules.
The structure is a hash table where:
- KEY is the trigger source (e.g., a property keyword like :status,
or a tag name like \"Project\").
- VALUE is a list of rule IDs that are interested in this source.")
(defvar supertag-automation--rule-index-source-token nil
"Source token represented by `supertag--rule-index'.")
(defun supertag-automation-clear-rule-index ()
"Clear the derived Automation rule index."
(setq supertag--rule-index (make-hash-table :test 'equal)
supertag-automation--rule-index-source-token nil))
(defun supertag--extract-trigger-sources (condition)
"Recursively parse a rule CONDITION and extract all trigger sources.
A trigger source is a specific property or tag that the rule depends on.
Returns a list of sources, e.g., '(:status :priority \"Project\")."
(let (sources)
;; cl-labels allows defining a local recursive function walk.
(cl-labels ((walk (form)
(when (listp form)
(pcase (car form)
('has-tag (push (cadr form) sources))
;; Multi-tag conditions: index all tags
((or 'has-any-tag 'has-all-tags)
(dolist (tag (cdr form))
(when (stringp tag)
(push tag sources))))
('field-equals
(let ((key (cadr form)))
;; Index by keyword, to match the key generated by the event handler.
(when (stringp key)
;; 1) Legacy keyword key (e.g. :status)
(push (intern (concat ":" key)) sources)
;; 2) Stable global field id when available, so
;; global field value events (:field-values
;; node-id field-id) can match rules written
;; with human-readable names.
(let ((fid (supertag-field-resolve-id nil key)))
(when (and fid (not (string-empty-p fid)))
(push fid sources))))))
('field-changed
(let ((key (cadr form)))
(when (stringp key)
;; Same strategy as field-equals: index by
;; compatibility keyword and stable global id.
(push (intern (concat ":" key)) sources)
(let ((fid (supertag-field-resolve-id nil key)))
(when (and fid (not (string-empty-p fid)))
(push fid sources))))))
;; Global field conditions (new)
('global-field-equals
(let ((key (cadr form)))
;; Index by string field-id for global fields
(when (stringp key)
(push key sources))))
('global-field-changed
(let ((key (cadr form)))
(when (stringp key)
(push key sources))))
('global-field-test
(let ((key (cadr form)))
(when (stringp key)
(push key sources))))
('property-equals
(let ((key (cadr form)))
;; Support both keyword (:status) and string ("status")
(cond
((keywordp key) (push key sources))
((stringp key)
;; Legacy keyword key
(push (intern (concat ":" key)) sources)
;; String properties are backed by global fields.
(let ((fid (supertag-field-resolve-id nil key)))
(when (and fid (not (string-empty-p fid)))
(push fid sources)))))))
('property-test
(let ((key (cadr form)))
(cond
((keywordp key) (push key sources))
((stringp key)
(push (intern (concat ":" key)) sources)
(let ((fid (supertag-field-resolve-id nil key)))
(when (and fid (not (string-empty-p fid)))
(push fid sources)))))))
;; Handle quoted conditions e.g. '(and ...) by unwrapping
('quote
(walk (cadr form)))
;; For logical operators, walk their sub-expressions.
((or 'and 'or 'not)
(dolist (subform (cdr form))
(walk subform)))
;; Ignore any other operators/forms.
(_ nil)))))
(walk condition))
;; Return the collected sources without duplicates.
(cl-remove-duplicates sources :test 'equal)))
(defun supertag--add-rule-to-index (rule)
"Parse a RULE and add its ID to the global rule index."
(let* ((rule-id (plist-get rule :id))
(sources (supertag--extract-trigger-sources (plist-get rule :condition))))
;; Also consider the trigger itself as a source if it's specific
(pcase (plist-get rule :trigger)
(`(:on-tag-added ,tag-name) (push tag-name sources))
(`(:on-tag-removed ,tag-name) (push tag-name sources)))
(dolist (source (cl-remove-duplicates sources :test 'equal))
(let ((rules (gethash source supertag--rule-index '())))
;; Use cl-pushnew to add the rule-id to the list of rules, avoiding duplicates.
(cl-pushnew rule-id rules :test 'equal)
;; Put the updated list back into the hash table.
(puthash source rules supertag--rule-index)))))
(defun supertag--remove-rule-from-index (rule)
"Parse a RULE and remove its ID from the global rule index."
(let* ((rule-id (plist-get rule :id))
(sources (supertag--extract-trigger-sources (plist-get rule :condition))))
(pcase (plist-get rule :trigger)
(`(:on-tag-added ,tag-name) (push tag-name sources))
(`(:on-tag-removed ,tag-name) (push tag-name sources)))
(dolist (source (cl-remove-duplicates sources))
(when-let ((rules (gethash source supertag--rule-index)))
(puthash source (remove rule-id rules) supertag--rule-index)))))
(defun supertag-rebuild-rule-index ()
"Clear and rebuild the entire automation rule index.
This function should be called once on system startup to ensure
the index is synchronized with the stored rules."
(interactive)
(supertag-automation-clear-rule-index)
(let ((all-rules (supertag-automation-list)))
(dolist (rule all-rules)
(supertag--add-rule-to-index rule)))
(setq supertag-automation--rule-index-source-token
(supertag-index-source-token '(:automations))))
(defun supertag-automation--ensure-rule-index ()
"Cold rebuild the rule index when Automation facts changed."
(unless (supertag-index-source-current-p
supertag-automation--rule-index-source-token '(:automations))
(supertag-rebuild-rule-index)))
(defun supertag--get-rules-from-index (event-path)
"Get a list of relevant rule IDs from the index based on an EVENT-PATH.
EVENT-PATH is the path from the :store-changed event, e.g.,
'(:nodes \"node-id\" :properties :status).
This function uses the pre-built supertag--rule-index for O(1) lookups."
(supertag-automation--ensure-rule-index)
;; Handle both flat (:nodes "ID") and nested ((:nodes "ID")) paths.
(let* ((path (if (and (listp (car event-path)) (memq (caar event-path) '(:nodes :tags)))
(car event-path)
event-path))
(entity-type (car path))
(entity-id (cadr path))
(node-data (cond
((eq entity-type :nodes)
(supertag-query-node entity-id))
;; Global field values: (:field-values node-id field-id)
((eq entity-type :field-values)
(supertag-query-node entity-id))
(t nil)))
(node-tags (when node-data (plist-get node-data :tags)))
(changed-prop (let ((p path))
(cond
;; Global field value change: (:field-values "node-id" "field-id")
((and (eq (car p) :field-values) (>= (length p) 3) (stringp (caddr p)))
(caddr p)) ; Return field-id as string for global fields
;; Node property change event, e.g., '(:nodes "id" :properties :status)
((and (eq (car p) :nodes)
(>= (length p) 4)
(eq (nth 2 p) :properties)
(keywordp (nth 3 p)))
(nth 3 p))
(t (progn
(unless (and (eq (car p) :nodes) (= (length p) 2))
)
nil)))))
(candidate-rules '()))
;; 1. Collect rules based on the specific property/field that changed
(when changed-prop
(when-let ((rules (gethash changed-prop supertag--rule-index)))
(setq candidate-rules (append rules candidate-rules)))
;; Also check keyword version for global fields (backward compat)
(when (stringp changed-prop)
(when-let ((rules (gethash (intern (concat ":" changed-prop)) supertag--rule-index)))
(setq candidate-rules (append rules candidate-rules))))
(unless (gethash changed-prop supertag--rule-index)
))
;; 2. Collect rules based on the tags of the affected node
(when node-tags
(dolist (tag-name node-tags)
(when-let ((rules (gethash tag-name supertag--rule-index)))
(setq candidate-rules (append rules candidate-rules)))))
;; 3. Return a unique list of rule IDs
(cl-remove-duplicates candidate-rules :test 'equal)))
;;; --- Data Validation ---
(defun supertag-automation--normalize-trigger (trigger)
"Normalize trigger to canonical format.
TRIGGER can be:
- A keyword like :on-field-change, :on-schedule
- A list like (:on-tag-added \"tagname\") or (:on-tag-removed \"tagname\")
Returns the normalized trigger."
(cond
;; Already a list form - return as is
((and (listp trigger) (keywordp (car trigger))) trigger)
;; Simple keyword - return as is
((keywordp trigger) trigger)
;; String - try to intern it
((stringp trigger) (intern trigger))
;; Otherwise return as is
(t trigger)))
(defun supertag--validate-automation-data (data)
"Validate automation data structure."
(unless (plist-get data :name)
(error "Automation missing required :name field: %S" data))
(unless (plist-get data :trigger)
(error "Automation missing required :trigger field: %S" data))
(unless (plist-get data :actions)
(error "Automation missing required :actions field: %S" data)))
;;; --- Core CRUD Operations ---
(defun supertag-automation-create (automation-data)
"Create a new automation rule.
AUTOMATION-DATA should contain:
- :name (required) - Automation name
- :description (optional) - Description
- :trigger (required) - Trigger condition
- :condition (optional) - When to execute (list of conditions)
- :actions (required) - List of actions to perform
- :enabled (optional) - Whether automation is enabled (default: t)
Returns the created automation data with assigned ID."
(supertag-with-transaction
(let* ((name (plist-get automation-data :name))
(id (format "auto-%s" (replace-regexp-in-string "[^a-zA-Z0-9-]" "-" name))))
;; Validate input data
(supertag--validate-automation-data automation-data)
;; Check if automation already exists and delete it if it does
(let ((existing (supertag-automation-get id)))
(when existing
(supertag-automation-delete id)))
;; Prepare automation data
(let ((automation-plist (list :id id
:name name
:description (or (plist-get automation-data :description) "")
:trigger (supertag-automation--normalize-trigger
(plist-get automation-data :trigger))
:condition (plist-get automation-data :condition)
:actions (plist-get automation-data :actions)
:schedule (plist-get automation-data :schedule)
:enabled (if (plist-member automation-data :enabled)
(plist-get automation-data :enabled)
t)
:created-at (current-time)
:modified-at (current-time))))
;; Store automation
(supertag-store-put-entity :automations id automation-plist t)
;; Add to the rule index
(supertag--add-rule-to-index automation-plist)
;; Register to scheduler if needed
(when (eq (plist-get automation-plist :trigger) :on-schedule)
(supertag-automation--register-scheduled-rule automation-plist))
;; Notify system
(when (fboundp 'supertag-notify)
(supertag-notify :automation-created :automation-id id :data automation-plist))
automation-plist))))
(defun supertag-automation-get (id)
"Get automation by ID."
(supertag-store-get-entity :automations id))
(defun supertag-automation-update (id updater)
"Update automation data.
ID is the automation identifier.
UPDATER is a function that receives current data and returns updated data."
(supertag-with-transaction
(let ((automation (supertag-automation-get id)))
(when automation
(let ((updated-automation (funcall updater automation)))
(when updated-automation
;; Update scheduler bridge
(when (eq (plist-get automation :trigger) :on-schedule)
(supertag-automation--deregister-scheduled-rule automation))
;; Update indexes
(supertag--remove-rule-from-index automation)
;; Normalize trigger before re-index
(setq updated-automation
(plist-put updated-automation :trigger
(supertag-automation--normalize-trigger
(plist-get updated-automation :trigger))))
(supertag--add-rule-to-index updated-automation)
(let ((final-automation (plist-put updated-automation :modified-at (current-time))))
(supertag--validate-automation-data final-automation)
(supertag-store-put-entity :automations id final-automation t)
;; Re-register if still scheduled
(when (eq (plist-get final-automation :trigger) :on-schedule)
(supertag-automation--register-scheduled-rule final-automation))
(when (fboundp 'supertag-notify)
(supertag-notify :automation-updated :automation-id id :data final-automation))
final-automation)))))))
(defun supertag-automation-delete (id)
"Delete automation by ID.
Returns the deleted automation data or nil if not found."
(supertag-with-transaction
(let ((automation (supertag-automation-get id)))
(when automation
;; Deregister from scheduler if needed
(when (eq (plist-get automation :trigger) :on-schedule)
(supertag-automation--deregister-scheduled-rule automation))
;; Remove from the rule index
(supertag--remove-rule-from-index automation)
;; Delete from storage
(supertag-store-remove-entity :automations id)
;; Notify system
(when (fboundp 'supertag-notify)
(supertag-notify :automation-deleted :automation-id id :data automation))
(message "Deleted automation: %s" (plist-get automation :name))
automation))))
(defun supertag-automation-list (&optional filter)
"List all automations with optional FILTER.
FILTER can be a function that receives automation data and returns t/nil."
(supertag-query-automations filter))
(defun supertag-automation-get-by-name (name)
"Get automation by name.
Returns automation data or nil if not found."
(let ((id (format "auto-%s" (replace-regexp-in-string "[^a-zA-Z0-9-]" "-" name))))
(supertag-automation-get id)))
;;; --- Query Functions ---
(defun supertag-automation-find-by-trigger (trigger-type)
"Find all automations with specific trigger type."
(let ((target (supertag-automation--normalize-trigger trigger-type)))
(supertag-automation-list
(lambda (automation)
(equal (supertag-automation--normalize-trigger
(plist-get automation :trigger))
target)))))
(defun supertag-automation-find-enabled ()
"Find all enabled automations."
(supertag-automation-list (lambda (automation)
(plist-get automation :enabled))))
(defun supertag-automation-find-scheduled ()
"Find all scheduled automations."
(supertag-automation-list (lambda (automation)
(eq (plist-get automation :trigger) :on-schedule))))
;; === Scheduler Bridge ===
(defun supertag-automation--register-scheduled-rule (rule)
"Register a :on-schedule RULE with the central scheduler, if available."
(when (and (eq (plist-get rule :trigger) :on-schedule)
(fboundp 'supertag-scheduler-register-task))
(let* ((schedule (plist-get rule :schedule))
(time (plist-get schedule :time))
(days (plist-get schedule :days-of-week))
(id-sym (intern (plist-get rule :id)))
(runner
(lambda ()
(let ((rule-now (supertag-automation-get (plist-get rule :id))))
(when (and rule-now (plist-get rule-now :enabled))
(dolist (action (plist-get rule-now :actions))
(when (eq (plist-get action :action) :call-function)
(supertag-automation-execute-action
:call-function nil (plist-get action :params)
(list :scheduled t :rule (plist-get rule-now :id))))))))))
(condition-case err
(progn
(supertag-scheduler-register-task id-sym :daily runner
:time time
:days-of-week days)
(supertag-automation--log "[Automation Scheduler] Registered scheduled rule: %s at %s days=%S"
(plist-get rule :id) time days))
(error (message "ERROR: Failed to register scheduled rule %s: %S"
(plist-get rule :id) err))))))
(defun supertag-automation--deregister-scheduled-rule (rule)
"Deregister a scheduled RULE from the scheduler, if available."
(when (and (fboundp 'supertag-scheduler-deregister-task)
(plist-get rule :id))
(let ((id-sym (intern (plist-get rule :id))))
(condition-case err
(progn
(supertag-scheduler-deregister-task id-sym)
(supertag-automation--log "[Automation Scheduler] Deregistered scheduled rule: %s"
(plist-get rule :id)))
(error (message "ERROR: Failed to deregister scheduled rule %s: %S"
(plist-get rule :id) err))))))
(defun supertag-automation--register-all-scheduled ()
"Register all :on-schedule rules with the scheduler."
(when (fboundp 'supertag-scheduler-register-task)
(dolist (rule (supertag-automation-find-scheduled))
(supertag-automation--register-scheduled-rule rule))))
;;; --- Rule Execution Engine ---
(defun supertag-rule-get (id)
"Get any rule by ID. Unified interface for rule retrieval."
(supertag-automation-get id))
(defun supertag-rule-execute (rule node-id context)
"Execute a rule with given context.
This is called by the automation engine when a rule matches an event."
(when (and rule (plist-get rule :enabled))
;; Prevent recursive automation during action execution
(unless supertag-automation--executing
(let ((actions (plist-get rule :actions))
(rule-id (plist-get rule :id))
(supertag-automation--executing t))
(supertag-automation--log "Executing rule %s on node %s" rule-id node-id)
(unwind-protect
(progn
(supertag-automation--execute-actions actions node-id context))
;; Always clear the executing flag
(setq supertag-automation--executing nil))))))
(defun supertag-automation--execute-actions (actions node-id context)
"Execute ACTIONS sequentially for NODE-ID with CONTEXT.
ACTIONS should be a list of plists, each containing :action and
optionally :params."
(dolist (action-spec actions)
(let ((action-type (plist-get action-spec :action))
(params (plist-get action-spec :params)))
(if action-type
(supertag-automation-execute-action action-type node-id params context)
(message "Automation Warning: Invalid action spec (missing :action): %S" action-spec)))))
(defun supertag-automation-execute-action (action-type node-id params context)
"Execute a specific action type.
ACTION-TYPE is the action to perform
NODE-ID is the target node
PARAMS are action parameters
CONTEXT provides execution context"
(pcase action-type
(:update-property
(supertag-automation-action-update-property node-id params))
(:update-todo-state
(supertag-automation-action-update-todo-state node-id params))
(:add-tag
(supertag-automation-action-add-tag node-id params))
(:remove-tag
(supertag-automation-action-remove-tag node-id params))
(:create-node
(supertag-automation-action-create-node params))
(:update-field
(supertag-automation-action-update-field node-id params))
(:move-node
(supertag-automation-action-move-node node-id params))
(:call-function
(supertag-automation-action-call-function node-id params context))
(:case
(supertag-automation-action-case node-id params context))
(_
(message "Unknown action type: %s" action-type))))
(defun supertag-automation--case-resolve (spec node-data context)
"Resolve SPEC to a value for CASE evaluation using NODE-DATA and CONTEXT."
(pcase spec
(`(:field ,field-name)
(let ((tags (plist-get node-data :tags)))
(supertag-automation--get-field-value (plist-get node-data :id) tags field-name)))
(`(:property ,prop)
(let ((props (plist-get node-data :properties)))
(plist-get props prop)))
(`(:value ,value) value)
(`(:literal ,value) value)
(`(:context ,key)
(plist-get context key))
(`(:function ,fn)
(when (functionp fn)
(funcall fn node-data context)))
(`(:eval ,fn)
(when (functionp fn)
(funcall fn node-data context)))
(_ spec)))
(defun supertag-automation--case-branch-match-p (branch value node-data context)
"Return non-nil when BRANCH matches VALUE.
BRANCH can specify :equals (single value or list), :in (list),
:match (regexp), or :test (function)."
(cond
((plist-get branch :default) nil)
((plist-member branch :equals)
(let ((expected (plist-get branch :equals)))
(if (listp expected)
(cl-some (lambda (item) (equal item value)) expected)
(equal expected value))))
((plist-member branch :in)
(let ((collection (plist-get branch :in)))
(and (listp collection)
(cl-some (lambda (item) (equal item value)) collection))))
((plist-member branch :match)
(let ((pattern (plist-get branch :match)))
(cond
((and (stringp pattern) (stringp value))
(string-match-p pattern value))
((functionp pattern)
(funcall pattern value node-data context))
(t nil))))
((plist-member branch :test)
(let ((fn (plist-get branch :test)))
(when (functionp fn)
(funcall fn value node-data context))))
(t nil)))
(defun supertag-automation-action-case (node-id params context)
"Evaluate CASE action described by PARAMS for NODE-ID with CONTEXT."
(let* ((node-data (or (supertag-query-node node-id)
(list :id node-id :tags nil :properties nil)))
(on-spec (plist-get params :on))
(branches (plist-get params :branches))
(value (supertag-automation--case-resolve on-spec node-data context))
(matched nil)
(default-actions nil))
(unless (listp branches)
(message "Automation Warning: CASE branches must be a list, got %S" branches)
(setq branches nil))
(dolist (branch branches)
(let ((actions (or (plist-get branch :actions)
(plist-get branch :do)
(plist-get branch :then))))
(cond
((plist-get branch :default)
(setq default-actions actions))
((and (not matched)
(supertag-automation--case-branch-match-p branch value node-data context))
(setq matched t)
(when actions
(supertag-automation--execute-actions actions node-id context))))))
(when (and (not matched) default-actions)
(supertag-automation--execute-actions default-actions node-id context))))
(defun supertag-automation-action-update-property (node-id params)
"Update an Org-mode property on the node.
This updates native Org properties like :SCHEDULED:, :DEADLINE:, etc.
Preserves all existing node data including tags and other properties."
(let ((property (plist-get params :property))
(value (plist-get params :value)))
(when (and node-id property)
(supertag-node-update
node-id
(lambda (node)
(when node
(let* ((props (copy-tree (or (plist-get node :properties) '())))
(current (plist-get props property)))
(if (equal current value)
(progn
(supertag-automation--log "SKIP(update-property): %s unchanged on node %s" property node-id)
node) ; Return unchanged node to preserve data
;; Create new node copy with updated property while preserving everything else
(let ((new-props (plist-put props property value))
(updated-node (copy-tree node)))
(plist-put updated-node :properties new-props))))))))))
(defun supertag-automation-action-update-todo-state (node-id params)
"Update the TODO state of a node.
PARAMS should contain :state with the new TODO keyword (e.g., \"DONE\")."
(require 'supertag-service-org)
(when-let ((state (plist-get params :state)))
(supertag-service-org-set-todo-state node-id state)))
(defun supertag-automation--semantic-tag-id (tag)
"Return TAG's Semantic Tag ID, or TAG when it is unresolved."
(or (and (supertag-tag-get tag) tag)
(supertag-tag-resolve-occurrence tag)
tag))
(defun supertag-automation-action-add-tag (node-id params)
"Add a tag to the node.
Append inline #tag at the end of the headline when it is not present."
(when (and node-id)
(when-let ((tag-name (plist-get params :tag)))
(let* ((node (supertag-query-node node-id))
(tags (plist-get node :tags))
(tag-id (supertag-automation--semantic-tag-id tag-name)))
(if (and tag-id (member tag-id tags))
(supertag-automation--log "SKIP(add-tag): tag '%s' already on node %s" tag-name node-id)
(unless (supertag-tag-get tag-id)
(setq tag-id
(plist-get (supertag-tag-create `(:name ,tag-name)) :id)))
(supertag-service-org-add-tag node-id tag-id 'end)
(supertag-automation--log "Automation: Added tag '%s' to node %s" tag-name node-id))))))
(defun supertag-automation-action-remove-tag (node-id params)
"Remove a tag from the node.
Uses the same Org-first path as UI commands."
(when (and node-id)
(when-let ((tag-name (plist-get params :tag)))
(let* ((node (supertag-query-node node-id))
(tags (plist-get node :tags))
(tag-id (supertag-automation--semantic-tag-id tag-name)))
(if (not (member tag-id tags))
(supertag-automation--log "SKIP(remove-tag): tag '%s' not present on node %s" tag-name node-id)
(supertag-service-org-remove-tag node-id tag-id)
(supertag-automation--log "Automation: Removed tag '%s' from node %s" tag-name node-id))))))
(defun supertag-automation-action-call-function (node-id params context)
"Call a custom function with node context."
(let ((function (plist-get params :function))
(args (plist-get params :args)))
(when (functionp function)
(apply function node-id context args))))
(defun supertag-automation-action-move-node (node-id params)
"Move NODE-ID to target file via org service."
(let ((target-file (plist-get params :target-file))
(leave-link (plist-get params :leave-link))
(target-level (plist-get params :target-level)))
(cond
((not node-id)
(message "ERROR(:move-node): node-id is nil"))
((not (and target-file (stringp target-file)))
(message "ERROR(:move-node): requires :target-file (string)"))
((not (fboundp 'supertag-service-org-move-node-to-file))
(message "ERROR(:move-node): supertag-service-org-move-node-to-file not available"))
(t
;; Check if node is already in target file to avoid error
(let* ((marker (when (fboundp 'org-id-find) (org-id-find node-id 'marker)))
(source-file (when marker (buffer-file-name (marker-buffer marker)))))
(if (and source-file (string= source-file target-file))
;; Node already in target file, skip silently
(supertag-automation--log "SKIP(:move-node): node %s already in %s" node-id target-file)
;; Actually move the node
(condition-case err
(supertag-service-org-move-node-to-file node-id target-file leave-link target-level)
(error
(message "ERROR(:move-node): %s" (error-message-string err))))))))))
(defun supertag-automation-action-create-node (params)
"Create a new node. PARAMS supports: :title, :tags (list of strings)."
(let ((title (plist-get params :title))
(tags (plist-get params :tags)))
(cond
((not (fboundp 'supertag-node-create))
(message "ERROR(:create-node): supertag-node-create not available"))
(t
(let ((node-id (apply #'supertag-node-create
(append (when title (list :title title))
(when tags (list :tags tags))))))
(supertag-automation--log "Created node %s (title=%s tags=%S)" node-id title tags)
node-id)))))
(defun supertag-automation-action-update-field (node-id params)
"Update a tag field for NODE-ID. PARAMS: :tag (id/name), :field (string), :value."
(let ((tag-id (plist-get params :tag))
(field (plist-get params :field))
(value (plist-get params :value)))
(cond
((not node-id)
(message "ERROR(:update-field): node-id is nil"))
((not (and tag-id (stringp field)))
(message "ERROR(:update-field): requires :tag and string :field")))
(when (and node-id tag-id (stringp field))
(let ((current (supertag-query-field-value node-id tag-id field t)))
(if (equal current value)
(supertag-automation--log "SKIP(update-field): %s/%s unchanged on node %s" tag-id field node-id)
(supertag-field-set node-id tag-id field value))))))
;;; --- Field Synchronization Engine ---
(defun supertag-automation-sync-field (from-id to-id field-name value &optional relation-config)
"Sync a field value between two entities.
FROM-ID is the source entity ID.
TO-ID is the target entity ID.
FIELD-NAME is the field to sync.
VALUE is the value to sync.
RELATION-CONFIG provides sync configuration from the relation."
(when supertag-automation--enabled
(let ((sync-key (format "%s->%s:%s" from-id to-id field-name)))
;; Check cache to avoid redundant syncs
(unless (equal (gethash sync-key supertag-automation--sync-cache) value)
;; Update cache
(puthash sync-key value supertag-automation--sync-cache)
;; Determine target entity type and update
(let ((target-node (supertag-query-node to-id))
(target-tag (supertag-tag-get to-id)))
(cond
;; Target is a node
(target-node
(supertag-automation--sync-to-node to-id field-name value))
;; Target is a tag/database
(target-tag
(supertag-automation--sync-to-tag to-id field-name value))
(t
(message "Warning: Unknown target entity type for %s" to-id))))
(supertag-automation--log "Synced property %s=%s from %s to %s"
field-name value from-id to-id)))))
(defun supertag-automation--sync-to-node (node-id field-name value)
"Sync field to a node entity via properties.
Preserves all existing node data including tags and other properties."
(let ((field-key (supertag-automation--normalize-keyword field-name)))
(supertag-node-update
node-id
(lambda (node)
(when node
(let* ((props (copy-tree (or (plist-get node :properties) '())))
(current (plist-get props field-key)))
(if (equal current value)
node ; Return unchanged node to preserve data
;; Create new props with updated field
(let ((new-props (plist-put props field-key value)))
;; Update node while preserving all other fields
(let ((updated-node (copy-tree node)))
(plist-put updated-node :properties new-props))))))))))
(defun supertag-automation--sync-to-tag (tag-id field-name value)
"Sync field to a tag entity."
(let ((field-key (supertag-automation--normalize-keyword field-name)))
(when (fboundp 'supertag-tag-update)
(supertag-tag-update
tag-id
(lambda (tag)
(let ((current (plist-get tag field-key)))
(if (equal current value)
nil
(plist-put tag field-key value))))))))
(defun supertag-automation-sync-all-relations (entity-id field-name value)
"Sync a field to all related entities.
ENTITY-ID is the source entity.
FIELD-NAME is the field that changed.
VALUE is the new value."
(when supertag-automation--enabled
;; Find all relations from this entity
(let ((relations (when (fboundp 'supertag-query-relations-from)
(supertag-query-relations-from
entity-id nil :semantic-edge))))
(dolist (relation relations)
(let* ((sync-fields (plist-get relation :sync-fields))
(sync-direction (plist-get relation :sync-direction))
(to-id (plist-get relation :to)))
;; Check if this field should be synced
(when (and sync-fields
(member field-name sync-fields)
(not (eq sync-direction :disabled)))
(supertag-automation-sync-field entity-id to-id field-name value relation)))))))
;;; --- Rollup Calculation Engine ---
(defun supertag-automation-calculate-rollup (source-node-id relation-name &optional force)
"Calculate rollup value for a 'one' side node based on a relation.
This function aligns with the Behavior System 2.0 guide.
SOURCE-NODE-ID is the ID of the 'one' side node (e.g., a project).
RELATION-NAME is the name of the relation template (e.g., 'tasks').
FORCE bypasses recursion protection when t."
(let ((calculation-key (format "rollup-%s-%s" source-node-id relation-name)))
(when (or force (not (gethash calculation-key supertag-automation--active-calculations)))
(puthash calculation-key t supertag-automation--active-calculations)
(unwind-protect
(when-let* ((relation-template (when (fboundp 'supertag-relation-get-by-name)
(supertag-relation-get-by-name relation-name)))
(rollup-config (plist-get relation-template :rollup)))
(let* ((from-field (plist-get rollup-config :from-field))
(to-field (plist-get rollup-config :to-field))
(rollup-func (plist-get rollup-config :function))
(result (supertag-automation--execute-rollup-calculation
source-node-id relation-name from-field rollup-func)))
(when result
(message "Calculated rollup for %s: %s = %s"
source-node-id to-field result)
result)))
;; Always clean up active calculation flag
(remhash calculation-key supertag-automation--active-calculations)))))
(defun supertag-automation--execute-rollup-calculation (source-node-id relation-name from-field rollup-function)
"Execute the actual rollup calculation for a given source node."
(let* ((related-nodes-info (when (fboundp 'supertag-relation-get-related-nodes)
(supertag-relation-get-related-nodes source-node-id relation-name)))
(values (supertag-automation--collect-field-values
related-nodes-info from-field)))
(when values
(supertag-automation--apply-rollup-function rollup-function values))))
(defun supertag-automation--collect-field-values (entities-info field-name)
"Collect values of FIELD-NAME from a list of entity info plists."