@a5c-ai/krate 5.0.1-staging.d8e10a163 → 5.0.1-staging.efc2092bb

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -19,7 +19,7 @@
19
19
  "platformNamespace": "krate-org-default"
20
20
  }
21
21
  ],
22
- "generatedAt": "2026-05-12T07:33:55.328Z",
22
+ "generatedAt": "2026-05-12T11:17:34.828Z",
23
23
  "correlationId": null,
24
24
  "controller": {
25
25
  "mode": "krate-workspace",
@@ -579,7 +579,7 @@
579
579
  "maintainers"
580
580
  ],
581
581
  "invitedBy": "admin",
582
- "expiresAt": "2026-05-19T07:33:55.325Z"
582
+ "expiresAt": "2026-05-19T11:17:34.825Z"
583
583
  },
584
584
  "status": {
585
585
  "phase": "Pending",
@@ -591,7 +591,7 @@
591
591
  "Pending": 1
592
592
  },
593
593
  "storage": "etcd",
594
- "yaml": "apiVersion: krate.a5c.ai/v1alpha1\nkind: Invite\nmetadata:\n namespace: krate-org-default\n labels:\n role: member\n annotations:\n name: new-user-example-com\n resourceVersion: 1\nspec:\n organizationRef: default\n email: new-user@example.com\n role: member\n teams:\n - maintainers\n invitedBy: admin\n expiresAt: 2026-05-19T07:33:55.325Z\nstatus:\n phase: Pending\n storage: etcd\n",
594
+ "yaml": "apiVersion: krate.a5c.ai/v1alpha1\nkind: Invite\nmetadata:\n namespace: krate-org-default\n labels:\n role: member\n annotations:\n name: new-user-example-com\n resourceVersion: 1\nspec:\n organizationRef: default\n email: new-user@example.com\n role: member\n teams:\n - maintainers\n invitedBy: admin\n expiresAt: 2026-05-19T11:17:34.825Z\nstatus:\n phase: Pending\n storage: etcd\n",
595
595
  "action": {
596
596
  "list": "Open Invite records in krate-org-default",
597
597
  "watch": "Watch Invite updates in krate-org-default",
@@ -1826,7 +1826,7 @@
1826
1826
  "maintainers"
1827
1827
  ],
1828
1828
  "phase": "Pending",
1829
- "expiresAt": "2026-05-19T07:33:55.325Z"
1829
+ "expiresAt": "2026-05-19T11:17:34.825Z"
1830
1830
  }
1831
1831
  ],
1832
1832
  "mappings": [
@@ -2332,7 +2332,7 @@
2332
2332
  "maintainers"
2333
2333
  ],
2334
2334
  "phase": "Pending",
2335
- "expiresAt": "2026-05-19T07:33:55.325Z"
2335
+ "expiresAt": "2026-05-19T11:17:34.825Z"
2336
2336
  }
2337
2337
  ],
2338
2338
  "mappings": [
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "project": "Krate",
3
3
  "version": "0.1.0",
4
- "generatedAt": "2026-05-12T07:33:55.336Z",
4
+ "generatedAt": "2026-05-12T11:17:34.835Z",
5
5
  "status": "ready-for-local-development",
6
6
  "components": [
7
7
  {
@@ -431,7 +431,7 @@
431
431
  "maintainers"
432
432
  ],
433
433
  "invitedBy": "admin",
434
- "expiresAt": "2026-05-19T07:33:55.325Z"
434
+ "expiresAt": "2026-05-19T11:17:34.825Z"
435
435
  },
436
436
  "status": {
437
437
  "phase": "Pending",
@@ -513,7 +513,7 @@
513
513
  },
514
514
  "auditLog": [
515
515
  {
516
- "at": "2026-05-12T07:33:55.324Z",
516
+ "at": "2026-05-12T11:17:34.824Z",
517
517
  "operation": "create",
518
518
  "user": "admin@example.com",
519
519
  "groups": [
@@ -525,7 +525,7 @@
525
525
  "allowed": true
526
526
  },
527
527
  {
528
- "at": "2026-05-12T07:33:55.324Z",
528
+ "at": "2026-05-12T11:17:34.824Z",
529
529
  "operation": "create",
530
530
  "user": "admin@example.com",
531
531
  "groups": [
@@ -537,7 +537,7 @@
537
537
  "allowed": true
538
538
  },
539
539
  {
540
- "at": "2026-05-12T07:33:55.324Z",
540
+ "at": "2026-05-12T11:17:34.824Z",
541
541
  "operation": "create",
542
542
  "user": "admin@example.com",
543
543
  "groups": [
@@ -549,7 +549,7 @@
549
549
  "allowed": true
550
550
  },
551
551
  {
552
- "at": "2026-05-12T07:33:55.325Z",
552
+ "at": "2026-05-12T11:17:34.825Z",
553
553
  "operation": "create",
554
554
  "user": "platform@example.com",
555
555
  "groups": [
@@ -561,7 +561,7 @@
561
561
  "allowed": true
562
562
  },
563
563
  {
564
- "at": "2026-05-12T07:33:55.325Z",
564
+ "at": "2026-05-12T11:17:34.825Z",
565
565
  "operation": "create",
566
566
  "user": "admin@example.com",
567
567
  "groups": [
@@ -573,7 +573,7 @@
573
573
  "allowed": true
574
574
  },
575
575
  {
576
- "at": "2026-05-12T07:33:55.325Z",
576
+ "at": "2026-05-12T11:17:34.825Z",
577
577
  "operation": "create",
578
578
  "user": "platform@example.com",
579
579
  "groups": [
@@ -585,7 +585,7 @@
585
585
  "allowed": true
586
586
  },
587
587
  {
588
- "at": "2026-05-12T07:33:55.325Z",
588
+ "at": "2026-05-12T11:17:34.825Z",
589
589
  "operation": "create",
590
590
  "user": "platform@example.com",
591
591
  "groups": [
@@ -597,7 +597,7 @@
597
597
  "allowed": true
598
598
  },
599
599
  {
600
- "at": "2026-05-12T07:33:55.325Z",
600
+ "at": "2026-05-12T11:17:34.825Z",
601
601
  "operation": "create",
602
602
  "user": "platform@example.com",
603
603
  "groups": [
@@ -609,7 +609,7 @@
609
609
  "allowed": true
610
610
  },
611
611
  {
612
- "at": "2026-05-12T07:33:55.325Z",
612
+ "at": "2026-05-12T11:17:34.825Z",
613
613
  "operation": "create",
614
614
  "user": "admin@example.com",
615
615
  "groups": [
@@ -621,7 +621,7 @@
621
621
  "allowed": true
622
622
  },
623
623
  {
624
- "at": "2026-05-12T07:33:55.326Z",
624
+ "at": "2026-05-12T11:17:34.826Z",
625
625
  "operation": "create",
626
626
  "user": "platform@example.com",
627
627
  "groups": [
@@ -633,7 +633,7 @@
633
633
  "allowed": true
634
634
  },
635
635
  {
636
- "at": "2026-05-12T07:33:55.326Z",
636
+ "at": "2026-05-12T11:17:34.826Z",
637
637
  "operation": "create",
638
638
  "user": "platform@example.com",
639
639
  "groups": [
@@ -847,7 +847,7 @@
847
847
  }
848
848
  },
849
849
  "audit": {
850
- "at": "2026-05-12T07:33:55.324Z",
850
+ "at": "2026-05-12T11:17:34.824Z",
851
851
  "operation": "create",
852
852
  "user": "admin@example.com",
853
853
  "groups": [
@@ -888,7 +888,7 @@
888
888
  }
889
889
  },
890
890
  "audit": {
891
- "at": "2026-05-12T07:33:55.324Z",
891
+ "at": "2026-05-12T11:17:34.824Z",
892
892
  "operation": "create",
893
893
  "user": "admin@example.com",
894
894
  "groups": [
@@ -924,7 +924,7 @@
924
924
  }
925
925
  },
926
926
  "audit": {
927
- "at": "2026-05-12T07:33:55.324Z",
927
+ "at": "2026-05-12T11:17:34.824Z",
928
928
  "operation": "create",
929
929
  "user": "admin@example.com",
930
930
  "groups": [
@@ -969,7 +969,7 @@
969
969
  }
970
970
  },
971
971
  "audit": {
972
- "at": "2026-05-12T07:33:55.325Z",
972
+ "at": "2026-05-12T11:17:34.825Z",
973
973
  "operation": "create",
974
974
  "user": "platform@example.com",
975
975
  "groups": [
@@ -1013,7 +1013,7 @@
1013
1013
  }
1014
1014
  },
1015
1015
  "audit": {
1016
- "at": "2026-05-12T07:33:55.325Z",
1016
+ "at": "2026-05-12T11:17:34.825Z",
1017
1017
  "operation": "create",
1018
1018
  "user": "admin@example.com",
1019
1019
  "groups": [
@@ -1061,7 +1061,7 @@
1061
1061
  }
1062
1062
  },
1063
1063
  "audit": {
1064
- "at": "2026-05-12T07:33:55.325Z",
1064
+ "at": "2026-05-12T11:17:34.825Z",
1065
1065
  "operation": "create",
1066
1066
  "user": "platform@example.com",
1067
1067
  "groups": [
@@ -1111,7 +1111,7 @@
1111
1111
  }
1112
1112
  },
1113
1113
  "audit": {
1114
- "at": "2026-05-12T07:33:55.325Z",
1114
+ "at": "2026-05-12T11:17:34.825Z",
1115
1115
  "operation": "create",
1116
1116
  "user": "platform@example.com",
1117
1117
  "groups": [
@@ -1162,7 +1162,7 @@
1162
1162
  }
1163
1163
  },
1164
1164
  "audit": {
1165
- "at": "2026-05-12T07:33:55.325Z",
1165
+ "at": "2026-05-12T11:17:34.825Z",
1166
1166
  "operation": "create",
1167
1167
  "user": "platform@example.com",
1168
1168
  "groups": [
@@ -1197,7 +1197,7 @@
1197
1197
  "maintainers"
1198
1198
  ],
1199
1199
  "invitedBy": "admin",
1200
- "expiresAt": "2026-05-19T07:33:55.325Z"
1200
+ "expiresAt": "2026-05-19T11:17:34.825Z"
1201
1201
  },
1202
1202
  "status": {
1203
1203
  "phase": "Pending",
@@ -1205,7 +1205,7 @@
1205
1205
  }
1206
1206
  },
1207
1207
  "audit": {
1208
- "at": "2026-05-12T07:33:55.325Z",
1208
+ "at": "2026-05-12T11:17:34.825Z",
1209
1209
  "operation": "create",
1210
1210
  "user": "admin@example.com",
1211
1211
  "groups": [
@@ -1256,7 +1256,7 @@
1256
1256
  }
1257
1257
  },
1258
1258
  "audit": {
1259
- "at": "2026-05-12T07:33:55.326Z",
1259
+ "at": "2026-05-12T11:17:34.826Z",
1260
1260
  "operation": "create",
1261
1261
  "user": "platform@example.com",
1262
1262
  "groups": [
@@ -1307,7 +1307,7 @@
1307
1307
  }
1308
1308
  },
1309
1309
  "audit": {
1310
- "at": "2026-05-12T07:33:55.326Z",
1310
+ "at": "2026-05-12T11:17:34.826Z",
1311
1311
  "operation": "create",
1312
1312
  "user": "platform@example.com",
1313
1313
  "groups": [
@@ -1708,7 +1708,7 @@
1708
1708
  "maintainers"
1709
1709
  ],
1710
1710
  "invitedBy": "admin",
1711
- "expiresAt": "2026-05-19T07:33:55.325Z"
1711
+ "expiresAt": "2026-05-19T11:17:34.825Z"
1712
1712
  },
1713
1713
  "status": {
1714
1714
  "phase": "Pending",
@@ -2345,7 +2345,7 @@
2345
2345
  }
2346
2346
  },
2347
2347
  "audit": {
2348
- "at": "2026-05-12T07:33:55.324Z",
2348
+ "at": "2026-05-12T11:17:34.824Z",
2349
2349
  "operation": "create",
2350
2350
  "user": "admin@example.com",
2351
2351
  "groups": [
@@ -2386,7 +2386,7 @@
2386
2386
  }
2387
2387
  },
2388
2388
  "audit": {
2389
- "at": "2026-05-12T07:33:55.324Z",
2389
+ "at": "2026-05-12T11:17:34.824Z",
2390
2390
  "operation": "create",
2391
2391
  "user": "admin@example.com",
2392
2392
  "groups": [
@@ -2422,7 +2422,7 @@
2422
2422
  }
2423
2423
  },
2424
2424
  "audit": {
2425
- "at": "2026-05-12T07:33:55.324Z",
2425
+ "at": "2026-05-12T11:17:34.824Z",
2426
2426
  "operation": "create",
2427
2427
  "user": "admin@example.com",
2428
2428
  "groups": [
@@ -2467,7 +2467,7 @@
2467
2467
  }
2468
2468
  },
2469
2469
  "audit": {
2470
- "at": "2026-05-12T07:33:55.325Z",
2470
+ "at": "2026-05-12T11:17:34.825Z",
2471
2471
  "operation": "create",
2472
2472
  "user": "platform@example.com",
2473
2473
  "groups": [
@@ -2511,7 +2511,7 @@
2511
2511
  }
2512
2512
  },
2513
2513
  "audit": {
2514
- "at": "2026-05-12T07:33:55.325Z",
2514
+ "at": "2026-05-12T11:17:34.825Z",
2515
2515
  "operation": "create",
2516
2516
  "user": "admin@example.com",
2517
2517
  "groups": [
@@ -2559,7 +2559,7 @@
2559
2559
  }
2560
2560
  },
2561
2561
  "audit": {
2562
- "at": "2026-05-12T07:33:55.325Z",
2562
+ "at": "2026-05-12T11:17:34.825Z",
2563
2563
  "operation": "create",
2564
2564
  "user": "platform@example.com",
2565
2565
  "groups": [
@@ -2609,7 +2609,7 @@
2609
2609
  }
2610
2610
  },
2611
2611
  "audit": {
2612
- "at": "2026-05-12T07:33:55.325Z",
2612
+ "at": "2026-05-12T11:17:34.825Z",
2613
2613
  "operation": "create",
2614
2614
  "user": "platform@example.com",
2615
2615
  "groups": [
@@ -2660,7 +2660,7 @@
2660
2660
  }
2661
2661
  },
2662
2662
  "audit": {
2663
- "at": "2026-05-12T07:33:55.325Z",
2663
+ "at": "2026-05-12T11:17:34.825Z",
2664
2664
  "operation": "create",
2665
2665
  "user": "platform@example.com",
2666
2666
  "groups": [
@@ -2695,7 +2695,7 @@
2695
2695
  "maintainers"
2696
2696
  ],
2697
2697
  "invitedBy": "admin",
2698
- "expiresAt": "2026-05-19T07:33:55.325Z"
2698
+ "expiresAt": "2026-05-19T11:17:34.825Z"
2699
2699
  },
2700
2700
  "status": {
2701
2701
  "phase": "Pending",
@@ -2703,7 +2703,7 @@
2703
2703
  }
2704
2704
  },
2705
2705
  "audit": {
2706
- "at": "2026-05-12T07:33:55.325Z",
2706
+ "at": "2026-05-12T11:17:34.825Z",
2707
2707
  "operation": "create",
2708
2708
  "user": "admin@example.com",
2709
2709
  "groups": [
@@ -2754,7 +2754,7 @@
2754
2754
  }
2755
2755
  },
2756
2756
  "audit": {
2757
- "at": "2026-05-12T07:33:55.326Z",
2757
+ "at": "2026-05-12T11:17:34.826Z",
2758
2758
  "operation": "create",
2759
2759
  "user": "platform@example.com",
2760
2760
  "groups": [
@@ -2805,7 +2805,7 @@
2805
2805
  }
2806
2806
  },
2807
2807
  "audit": {
2808
- "at": "2026-05-12T07:33:55.326Z",
2808
+ "at": "2026-05-12T11:17:34.826Z",
2809
2809
  "operation": "create",
2810
2810
  "user": "platform@example.com",
2811
2811
  "groups": [
@@ -2820,7 +2820,7 @@
2820
2820
  ],
2821
2821
  "auditLog": [
2822
2822
  {
2823
- "at": "2026-05-12T07:33:55.324Z",
2823
+ "at": "2026-05-12T11:17:34.824Z",
2824
2824
  "operation": "create",
2825
2825
  "user": "admin@example.com",
2826
2826
  "groups": [
@@ -2832,7 +2832,7 @@
2832
2832
  "allowed": true
2833
2833
  },
2834
2834
  {
2835
- "at": "2026-05-12T07:33:55.324Z",
2835
+ "at": "2026-05-12T11:17:34.824Z",
2836
2836
  "operation": "create",
2837
2837
  "user": "admin@example.com",
2838
2838
  "groups": [
@@ -2844,7 +2844,7 @@
2844
2844
  "allowed": true
2845
2845
  },
2846
2846
  {
2847
- "at": "2026-05-12T07:33:55.324Z",
2847
+ "at": "2026-05-12T11:17:34.824Z",
2848
2848
  "operation": "create",
2849
2849
  "user": "admin@example.com",
2850
2850
  "groups": [
@@ -2856,7 +2856,7 @@
2856
2856
  "allowed": true
2857
2857
  },
2858
2858
  {
2859
- "at": "2026-05-12T07:33:55.325Z",
2859
+ "at": "2026-05-12T11:17:34.825Z",
2860
2860
  "operation": "create",
2861
2861
  "user": "platform@example.com",
2862
2862
  "groups": [
@@ -2868,7 +2868,7 @@
2868
2868
  "allowed": true
2869
2869
  },
2870
2870
  {
2871
- "at": "2026-05-12T07:33:55.325Z",
2871
+ "at": "2026-05-12T11:17:34.825Z",
2872
2872
  "operation": "create",
2873
2873
  "user": "admin@example.com",
2874
2874
  "groups": [
@@ -2880,7 +2880,7 @@
2880
2880
  "allowed": true
2881
2881
  },
2882
2882
  {
2883
- "at": "2026-05-12T07:33:55.325Z",
2883
+ "at": "2026-05-12T11:17:34.825Z",
2884
2884
  "operation": "create",
2885
2885
  "user": "platform@example.com",
2886
2886
  "groups": [
@@ -2892,7 +2892,7 @@
2892
2892
  "allowed": true
2893
2893
  },
2894
2894
  {
2895
- "at": "2026-05-12T07:33:55.325Z",
2895
+ "at": "2026-05-12T11:17:34.825Z",
2896
2896
  "operation": "create",
2897
2897
  "user": "platform@example.com",
2898
2898
  "groups": [
@@ -2904,7 +2904,7 @@
2904
2904
  "allowed": true
2905
2905
  },
2906
2906
  {
2907
- "at": "2026-05-12T07:33:55.325Z",
2907
+ "at": "2026-05-12T11:17:34.825Z",
2908
2908
  "operation": "create",
2909
2909
  "user": "platform@example.com",
2910
2910
  "groups": [
@@ -2916,7 +2916,7 @@
2916
2916
  "allowed": true
2917
2917
  },
2918
2918
  {
2919
- "at": "2026-05-12T07:33:55.325Z",
2919
+ "at": "2026-05-12T11:17:34.825Z",
2920
2920
  "operation": "create",
2921
2921
  "user": "admin@example.com",
2922
2922
  "groups": [
@@ -2928,7 +2928,7 @@
2928
2928
  "allowed": true
2929
2929
  },
2930
2930
  {
2931
- "at": "2026-05-12T07:33:55.326Z",
2931
+ "at": "2026-05-12T11:17:34.826Z",
2932
2932
  "operation": "create",
2933
2933
  "user": "platform@example.com",
2934
2934
  "groups": [
@@ -2940,7 +2940,7 @@
2940
2940
  "allowed": true
2941
2941
  },
2942
2942
  {
2943
- "at": "2026-05-12T07:33:55.326Z",
2943
+ "at": "2026-05-12T11:17:34.826Z",
2944
2944
  "operation": "create",
2945
2945
  "user": "platform@example.com",
2946
2946
  "groups": [
@@ -3,7 +3,7 @@
3
3
  "description": "Kubernetes-native forge runtime with Argo CD and Krate-managed repository hosting",
4
4
  "package": {
5
5
  "name": "@a5c-ai/krate",
6
- "version": "5.0.1-staging.d8e10a163",
6
+ "version": "5.0.1-staging.efc2092bb",
7
7
  "private": true
8
8
  },
9
9
  "entrypoints": {
@@ -162,7 +162,7 @@
162
162
  "lifecycle": {
163
163
  "project": "Krate",
164
164
  "version": "0.1.0",
165
- "generatedAt": "2026-05-12T07:33:55.336Z",
165
+ "generatedAt": "2026-05-12T11:17:34.835Z",
166
166
  "status": "ready-for-local-development",
167
167
  "components": [
168
168
  {
@@ -464,7 +464,7 @@
464
464
  "ok": true,
465
465
  "assertions": []
466
466
  },
467
- "generatedAt": "2026-05-12T07:33:55.336Z",
467
+ "generatedAt": "2026-05-12T11:17:34.835Z",
468
468
  "controller": {
469
469
  "status": "degraded",
470
470
  "namespace": "krate-org-default",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@a5c-ai/krate",
3
- "version": "5.0.1-staging.d8e10a163",
3
+ "version": "5.0.1-staging.efc2092bb",
4
4
  "description": "a5c.ai Krate: Kubernetes-native forge runtime with Argo CD GitOps and Gitea-backed Git hosting.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
@@ -3,12 +3,14 @@ import { createAgentStackController } from './agent-stack-controller.js';
3
3
  import { assembleContextBundle } from './agent-context-bundles.js';
4
4
  import { createResource, clone } from './resource-model.js';
5
5
  import { createAgentMuxClient } from './agent-mux-client.js';
6
+ import { createAgentMemoryController } from './agent-memory-controller.js';
7
+ import { createAgentApprovalController } from './agent-approval-controller.js';
6
8
 
7
9
  export const AGENT_DISPATCH_CONTROLLER_BOUNDARY = {
8
10
  role: 'agent-dispatch-controller',
9
11
  scope: 'Manual dispatch orchestration with permission gating and context assembly',
10
12
  owns: ['dispatch creation', 'attempt lifecycle', 'Agent Mux session binding'],
11
- delegatesTo: ['agent-permission-review', 'agent-stack-controller', 'agent-context-bundles', 'agent-mux-client'],
13
+ delegatesTo: ['agent-permission-review', 'agent-stack-controller', 'agent-context-bundles', 'agent-mux-client', 'agent-memory-controller', 'agent-approval-controller'],
12
14
  mustNotOwn: ['secret values', 'UI rendering']
13
15
  };
14
16
 
@@ -16,6 +18,8 @@ export function createAgentDispatchController(options = {}) {
16
18
  const permissionReviewer = options.permissionReviewer || createPermissionReviewer();
17
19
  const stackController = options.stackController || createAgentStackController();
18
20
  const agentMuxClient = options.agentMuxClient || createAgentMuxClient();
21
+ const memoryController = options.memoryController || createAgentMemoryController();
22
+ const approvalController = options.approvalController || createAgentApprovalController();
19
23
 
20
24
  return {
21
25
  role: 'agent-dispatch-controller',
@@ -32,10 +36,68 @@ export function createAgentDispatchController(options = {}) {
32
36
  }
33
37
  const permissionSnapshot = permissionReviewer.createPermissionSnapshot(review);
34
38
 
35
- // 3. Assemble context bundle
39
+ // 3. Memory snapshot — create if any AgentMemoryRepository exists in resources
40
+ let memorySnapshot = null;
41
+ const memoryRepos = resources.AgentMemoryRepository || [];
42
+ if (memoryRepos.length > 0) {
43
+ const memRepo = memoryRepos[0];
44
+ const timeTravel = memoryController.resolveTimeTravel({ mode: 'current', commits: [] });
45
+ memorySnapshot = memoryController.createMemorySnapshot({
46
+ memoryRepository: memRepo.metadata.name,
47
+ requestedRef: ref,
48
+ resolvedCommit: timeTravel.resolvedCommit || ref,
49
+ queryManifest: {},
50
+ selectedRecords: [],
51
+ selectedDocuments: [],
52
+ ontologyDigest: '',
53
+ namespace,
54
+ organizationRef,
55
+ });
56
+ }
57
+
58
+ // 4. Approval gate — if review requires approval, create approval and return early
59
+ if (review.decision === 'requires-approval') {
60
+ const now = new Date().toISOString();
61
+ const runName = `dispatch-${Date.now()}`;
62
+
63
+ const run = createResource('AgentDispatchRun', { name: runName, namespace }, {
64
+ organizationRef,
65
+ repository,
66
+ sourceRefs: clone(sourceRefs),
67
+ agentStack,
68
+ taskKind: taskKind || 'diagnostic',
69
+ contextBundleRef: null,
70
+ });
71
+ run.status = { phase: 'AwaitingApproval', queuedAt: now };
72
+ if (memorySnapshot) {
73
+ run.spec.memorySnapshotRef = memorySnapshot.metadata.name;
74
+ }
75
+
76
+ const approvalResult = approvalController.createApprovalRequest({
77
+ dispatchRun: runName,
78
+ action: 'secret-access',
79
+ requestedBy: actor,
80
+ context: `Dispatch requires approval for agent stack: ${agentStack}`,
81
+ namespace,
82
+ organizationRef,
83
+ resources,
84
+ });
85
+
86
+ return {
87
+ error: false,
88
+ run,
89
+ approval: approvalResult.error ? null : approvalResult.approval,
90
+ awaitingApproval: true,
91
+ memorySnapshot,
92
+ permissionSnapshot,
93
+ review,
94
+ };
95
+ }
96
+
97
+ // 5. Assemble context bundle
36
98
  const contextBundle = assembleContextBundle({ stack, repository, ref, sourceRefs, contextLabels: [], resources });
37
99
 
38
- // 4. Create resources
100
+ // 6. Create resources
39
101
  const now = new Date().toISOString();
40
102
  const runName = `dispatch-${Date.now()}`;
41
103
 
@@ -48,6 +110,9 @@ export function createAgentDispatchController(options = {}) {
48
110
  contextBundleRef: contextBundle.metadata.name,
49
111
  });
50
112
  run.status = { phase: 'Pending', queuedAt: now };
113
+ if (memorySnapshot) {
114
+ run.spec.memorySnapshotRef = memorySnapshot.metadata.name;
115
+ }
51
116
 
52
117
  const attempt = createResource('AgentDispatchAttempt', { name: `${runName}-attempt-1`, namespace }, {
53
118
  organizationRef,
@@ -58,7 +123,8 @@ export function createAgentDispatchController(options = {}) {
58
123
  });
59
124
  attempt.status = { permissionSnapshot, queueEnteredAt: now };
60
125
 
61
- // 5. Try Agent Mux launch
126
+ // 7. Try Agent Mux launch
127
+ let transcript = null;
62
128
  if (agentMuxClient.isAvailable()) {
63
129
  try {
64
130
  const session = await agentMuxClient.launchSession({ stack, contextBundle, permissionSnapshot });
@@ -67,6 +133,16 @@ export function createAgentDispatchController(options = {}) {
67
133
  attempt.status.agentMuxSessionId = session.sessionId;
68
134
  run.status.phase = 'Running';
69
135
  attempt.status.startedAt = now;
136
+
137
+ // 8. After successful launch — start SSE subscription + create initial transcript
138
+ const collectedEvents = [];
139
+ const subscription = agentMuxClient.subscribeToEvents(session.runId, (event) => {
140
+ collectedEvents.push(event);
141
+ });
142
+ run.status.sseSubscription = { runId: session.runId, active: true };
143
+
144
+ transcript = agentMuxClient.reconcileTranscript(session.sessionId, collectedEvents, { namespace, organizationRef });
145
+ run.status.transcriptRef = transcript.metadata.name;
70
146
  } else {
71
147
  run.status.phase = 'Queued';
72
148
  run.status.conditions = [{ type: 'AgentMuxBound', status: 'False', reason: 'LaunchFailed', message: 'Agent Mux launch returned no session' }];
@@ -80,7 +156,7 @@ export function createAgentDispatchController(options = {}) {
80
156
  run.status.conditions = [{ type: 'AgentMuxBound', status: 'False', reason: 'Unavailable', message: 'Agent Mux gateway not configured' }];
81
157
  }
82
158
 
83
- return { error: false, run, attempt, contextBundle, permissionSnapshot };
159
+ return { error: false, run, attempt, contextBundle, permissionSnapshot, memorySnapshot, transcript };
84
160
  }
85
161
  };
86
162
  }
@@ -99,6 +99,50 @@ export function createKrateHttpHandler({ runtime = createKrateRuntime(), control
99
99
  : await scopedController.denyAgentAction(input);
100
100
  return send(response, result.error ? 400 : 200, result);
101
101
  }
102
+ // Agent webhook ingress
103
+ const webhookIngestMatch = url.pathname.match(/^\/api\/orgs\/([^/]+)\/agents\/webhooks\/ingest$/);
104
+ if (request.method === 'POST' && webhookIngestMatch) {
105
+ const org = webhookIngestMatch[1];
106
+ const body = await readJson(request);
107
+ const event = normalizeWebhookEvent(body, org);
108
+ const scopedController = createKrateApiController({ namespace: orgNamespaceName(org) });
109
+ const result = await scopedController.processWebhookEvent({ event, organizationRef: org, namespace: orgNamespaceName(org) });
110
+ return send(response, 200, result);
111
+ }
112
+
113
+ // Pipeline failure event
114
+ const pipelineFailMatch = url.pathname.match(/^\/api\/orgs\/([^/]+)\/agents\/events\/pipeline-failure$/);
115
+ if (request.method === 'POST' && pipelineFailMatch) {
116
+ const org = pipelineFailMatch[1];
117
+ const body = await readJson(request);
118
+ const event = { type: 'ci-failure', source: { kind: 'Pipeline', name: body.name || 'unknown', namespace: body.namespace }, repository: body.repository || '', ref: body.ref || 'main', actor: body.actor || 'system', payload: body };
119
+ const scopedController = createKrateApiController({ namespace: orgNamespaceName(org) });
120
+ const result = await scopedController.processWebhookEvent({ event, organizationRef: org, namespace: orgNamespaceName(org) });
121
+ return send(response, 200, result);
122
+ }
123
+
124
+ // Comment event
125
+ const commentMatch = url.pathname.match(/^\/api\/orgs\/([^/]+)\/agents\/events\/comment$/);
126
+ if (request.method === 'POST' && commentMatch) {
127
+ const org = commentMatch[1];
128
+ const body = await readJson(request);
129
+ const event = { type: 'comment', source: { kind: body.kind || 'Issue', name: body.name || 'unknown' }, repository: body.repository || '', ref: body.ref || 'main', actor: body.actor || 'system', payload: { body: body.body || '' } };
130
+ const scopedController = createKrateApiController({ namespace: orgNamespaceName(org) });
131
+ const result = await scopedController.processWebhookEvent({ event, organizationRef: org, namespace: orgNamespaceName(org) });
132
+ return send(response, 200, result);
133
+ }
134
+
135
+ // Label event
136
+ const labelMatch = url.pathname.match(/^\/api\/orgs\/([^/]+)\/agents\/events\/label$/);
137
+ if (request.method === 'POST' && labelMatch) {
138
+ const org = labelMatch[1];
139
+ const body = await readJson(request);
140
+ const event = { type: 'label-added', source: { kind: body.kind || 'Issue', name: body.name || 'unknown' }, repository: body.repository || '', ref: body.ref || 'main', actor: body.actor || 'system', payload: { label: body.label || '' } };
141
+ const scopedController = createKrateApiController({ namespace: orgNamespaceName(org) });
142
+ const result = await scopedController.processWebhookEvent({ event, organizationRef: org, namespace: orgNamespaceName(org) });
143
+ return send(response, 200, result);
144
+ }
145
+
102
146
  const agentTriggerProcessMatch = url.pathname.match(/^\/api\/orgs\/([^/]+)\/agents\/triggers\/process$/);
103
147
  if (request.method === 'POST' && agentTriggerProcessMatch) {
104
148
  const org = agentTriggerProcessMatch[1];
@@ -149,3 +193,28 @@ function send(response, status, body) {
149
193
  response.writeHead(status, jsonHeaders);
150
194
  response.end(JSON.stringify(body, null, 2));
151
195
  }
196
+
197
+ export function normalizeWebhookEvent(body, org) {
198
+ // GitHub/Gitea webhook normalization
199
+ if (body.action === 'completed' && body.workflow_run?.conclusion === 'failure') {
200
+ return { type: 'ci-failure', source: { kind: 'Pipeline', name: body.workflow_run?.name || 'unknown' }, repository: body.repository?.full_name || '', ref: body.workflow_run?.head_branch || 'main', actor: body.sender?.login || 'system', payload: body };
201
+ }
202
+ if (body.action === 'opened' && body.pull_request) {
203
+ return { type: 'pr-opened', source: { kind: 'PullRequest', name: String(body.pull_request.number) }, repository: body.repository?.full_name || '', ref: body.pull_request?.head?.ref || 'main', actor: body.sender?.login || 'system', payload: body };
204
+ }
205
+ if (body.action === 'created' && body.comment) {
206
+ const kind = body.issue?.pull_request ? 'PullRequest' : 'Issue';
207
+ return { type: 'comment', source: { kind, name: String(body.issue?.number || 'unknown') }, repository: body.repository?.full_name || '', ref: 'main', actor: body.sender?.login || body.comment?.user?.login || 'system', payload: { body: body.comment?.body || '' } };
208
+ }
209
+ if (body.action === 'labeled') {
210
+ return { type: 'label-added', source: { kind: body.pull_request ? 'PullRequest' : 'Issue', name: String(body.issue?.number || body.pull_request?.number || 'unknown') }, repository: body.repository?.full_name || '', ref: 'main', actor: body.sender?.login || 'system', payload: { label: body.label?.name || '' } };
211
+ }
212
+ if (body.action === 'opened' && body.issue && !body.pull_request) {
213
+ return { type: 'issue-created', source: { kind: 'Issue', name: String(body.issue.number) }, repository: body.repository?.full_name || '', ref: 'main', actor: body.sender?.login || 'system', payload: body };
214
+ }
215
+ if (body.ref && body.commits) {
216
+ return { type: 'push', source: { kind: 'Repository', name: body.repository?.full_name || '' }, repository: body.repository?.full_name || '', ref: body.ref?.replace('refs/heads/', '') || 'main', actor: body.sender?.login || body.pusher?.name || 'system', payload: body };
217
+ }
218
+ // Generic fallback
219
+ return { type: 'webhook', source: { kind: 'WebhookDelivery', name: 'unknown' }, repository: body.repository?.full_name || '', ref: 'main', actor: body.sender?.login || 'system', payload: body };
220
+ }
@@ -53,6 +53,15 @@ test('Successful dispatch with Agent Mux available', async () => {
53
53
  role: 'agent-mux-client',
54
54
  isAvailable() { return true; },
55
55
  async launchSession() { return { runId: `amux-${Date.now()}`, sessionId: `session-${Date.now()}` }; },
56
+ subscribeToEvents(runId, cb) { return { abort() {} }; },
57
+ reconcileTranscript(sessionId, events, opts) {
58
+ return createResource('AgentSessionTranscript', { name: `transcript-${sessionId}`, namespace: opts?.namespace || 'default' }, {
59
+ organizationRef: opts?.organizationRef || 'default',
60
+ sessionRef: sessionId,
61
+ messages: [],
62
+ cost: { inputTokens: 0, outputTokens: 0, totalTokens: 0 },
63
+ }, { phase: 'Reconciled', reconciledAt: new Date().toISOString() });
64
+ },
56
65
  };
57
66
  const resources = buildValidResources('dispatch-stack');
58
67
  const controller = createAgentDispatchController({ agentMuxClient: muxClient });
@@ -174,3 +183,133 @@ test('Context bundle referenced correctly', async () => {
174
183
  'Run contextBundleRef should match context bundle name'
175
184
  );
176
185
  });
186
+
187
+ function makeMemoryRepository(name) {
188
+ return createResource('AgentMemoryRepository', { name, namespace: 'krate-org-default' }, {
189
+ organizationRef: 'default',
190
+ repositoryRef: 'memory-repo',
191
+ defaultBranch: 'main',
192
+ layoutProfile: 'standard',
193
+ });
194
+ }
195
+
196
+ test('Dispatch with AgentMemoryRepository creates memorySnapshot', async () => {
197
+ const muxClient = createAgentMuxClient({ gateway: '', enabled: false });
198
+ const resources = {
199
+ ...buildValidResources('mem-stack'),
200
+ AgentMemoryRepository: [makeMemoryRepository('org-memory')],
201
+ };
202
+ const controller = createAgentDispatchController({ agentMuxClient: muxClient });
203
+
204
+ const result = await controller.createManualDispatch({
205
+ repository: 'test-repo',
206
+ ref: 'main',
207
+ agentStack: 'mem-stack',
208
+ actor: 'test-user',
209
+ namespace: 'krate-org-default',
210
+ organizationRef: 'default',
211
+ resources,
212
+ });
213
+
214
+ assert.equal(result.error, false, 'Dispatch should succeed');
215
+ assert.ok(result.memorySnapshot, 'Result should include memorySnapshot');
216
+ assert.equal(result.memorySnapshot.kind, 'AgentMemorySnapshot', 'memorySnapshot should be an AgentMemorySnapshot');
217
+ assert.equal(result.memorySnapshot.spec.memoryRepository, 'org-memory', 'memorySnapshot should reference the memory repo');
218
+ assert.equal(result.memorySnapshot.status.phase, 'Pinned', 'memorySnapshot should be Pinned');
219
+ assert.equal(result.run.spec.memorySnapshotRef, result.memorySnapshot.metadata.name, 'Run should reference memorySnapshot');
220
+ });
221
+
222
+ test('Dispatch without memory repos has null memorySnapshot', async () => {
223
+ const muxClient = createAgentMuxClient({ gateway: '', enabled: false });
224
+ const resources = buildValidResources('no-mem-stack');
225
+ const controller = createAgentDispatchController({ agentMuxClient: muxClient });
226
+
227
+ const result = await controller.createManualDispatch({
228
+ repository: 'test-repo',
229
+ ref: 'main',
230
+ agentStack: 'no-mem-stack',
231
+ actor: 'test-user',
232
+ namespace: 'krate-org-default',
233
+ organizationRef: 'default',
234
+ resources,
235
+ });
236
+
237
+ assert.equal(result.error, false, 'Dispatch should succeed');
238
+ assert.equal(result.memorySnapshot, null, 'memorySnapshot should be null when no AgentMemoryRepository');
239
+ assert.equal(result.run.spec.memorySnapshotRef, undefined, 'Run should not have memorySnapshotRef');
240
+ });
241
+
242
+ test('Dispatch with requires-approval returns early with awaitingApproval', async () => {
243
+ // Build resources where the secret grant has requiredApproval set,
244
+ // which causes the permission reviewer to return 'requires-approval'
245
+ const stack = makeStack('approval-stack');
246
+ const resources = {
247
+ AgentStack: [stack],
248
+ AgentServiceAccount: [makeServiceAccount('sa-default')],
249
+ AgentRoleBinding: [makeRoleBinding('rb-1', 'sa-default')],
250
+ AgentSecretGrant: [makeSecretGrant('sg-model', 'sa-default', 'model-provider')],
251
+ };
252
+ // Add requiredApproval to the secret grant so permission review returns 'requires-approval'
253
+ resources.AgentSecretGrant[0].spec.requiredApproval = 'manager';
254
+
255
+ const controller = createAgentDispatchController();
256
+
257
+ const result = await controller.createManualDispatch({
258
+ repository: 'test-repo',
259
+ ref: 'main',
260
+ agentStack: 'approval-stack',
261
+ actor: 'test-user',
262
+ namespace: 'krate-org-default',
263
+ organizationRef: 'default',
264
+ resources,
265
+ });
266
+
267
+ assert.equal(result.error, false, 'Dispatch should not error — it is awaiting approval');
268
+ assert.equal(result.awaitingApproval, true, 'Result should indicate awaitingApproval');
269
+ assert.equal(result.run.status.phase, 'AwaitingApproval', 'Run phase should be AwaitingApproval');
270
+ assert.ok(result.approval, 'Result should include the approval resource');
271
+ assert.equal(result.approval.kind, 'AgentApproval', 'Approval should be an AgentApproval resource');
272
+ assert.equal(result.approval.status.phase, 'Pending', 'Approval should be in Pending phase');
273
+ assert.ok(result.permissionSnapshot, 'Result should include permissionSnapshot');
274
+ // No contextBundle or attempt since we returned early
275
+ assert.equal(result.contextBundle, undefined, 'No contextBundle when awaiting approval');
276
+ assert.equal(result.attempt, undefined, 'No attempt when awaiting approval');
277
+ });
278
+
279
+ test('Successful launch creates transcript ref on run', async () => {
280
+ const sessionId = `session-${Date.now()}`;
281
+ const runId = `amux-${Date.now()}`;
282
+ const muxClient = {
283
+ role: 'agent-mux-client',
284
+ isAvailable() { return true; },
285
+ async launchSession() { return { runId, sessionId }; },
286
+ subscribeToEvents(rid, cb) { return { abort() {} }; },
287
+ reconcileTranscript(sid, events, opts) {
288
+ return createResource('AgentSessionTranscript', { name: `transcript-${sid}`, namespace: opts?.namespace || 'default' }, {
289
+ organizationRef: opts?.organizationRef || 'default',
290
+ sessionRef: sid,
291
+ messages: [],
292
+ cost: { inputTokens: 0, outputTokens: 0, totalTokens: 0 },
293
+ }, { phase: 'Reconciled', reconciledAt: new Date().toISOString() });
294
+ },
295
+ };
296
+ const resources = buildValidResources('transcript-stack');
297
+ const controller = createAgentDispatchController({ agentMuxClient: muxClient });
298
+
299
+ const result = await controller.createManualDispatch({
300
+ repository: 'test-repo',
301
+ ref: 'main',
302
+ agentStack: 'transcript-stack',
303
+ actor: 'test-user',
304
+ namespace: 'krate-org-default',
305
+ organizationRef: 'default',
306
+ resources,
307
+ });
308
+
309
+ assert.equal(result.error, false, 'Dispatch should succeed');
310
+ assert.equal(result.run.status.phase, 'Running', 'Run phase should be Running');
311
+ assert.ok(result.transcript, 'Result should include transcript');
312
+ assert.equal(result.transcript.kind, 'AgentSessionTranscript', 'Transcript should be AgentSessionTranscript');
313
+ assert.ok(result.run.status.transcriptRef, 'Run should have transcriptRef in status');
314
+ assert.equal(result.run.status.transcriptRef, result.transcript.metadata.name, 'Run transcriptRef should match transcript name');
315
+ });
@@ -0,0 +1,190 @@
1
+ import { describe, it } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { normalizeWebhookEvent } from '../src/index.js';
4
+
5
+ describe('normalizeWebhookEvent', () => {
6
+ // 1. GitHub workflow_run failure -> type: 'ci-failure'
7
+ it('normalizes GitHub workflow_run failure to ci-failure', () => {
8
+ const body = {
9
+ action: 'completed',
10
+ workflow_run: { conclusion: 'failure', name: 'CI Build', head_branch: 'feature-x' },
11
+ repository: { full_name: 'acme/app' },
12
+ sender: { login: 'alice' }
13
+ };
14
+ const event = normalizeWebhookEvent(body, 'acme');
15
+
16
+ assert.equal(event.type, 'ci-failure');
17
+ assert.equal(event.source.kind, 'Pipeline');
18
+ assert.equal(event.source.name, 'CI Build');
19
+ assert.equal(event.repository, 'acme/app');
20
+ assert.equal(event.ref, 'feature-x');
21
+ assert.equal(event.actor, 'alice');
22
+ assert.equal(event.payload, body);
23
+ });
24
+
25
+ // 2. GitHub PR opened -> type: 'pr-opened'
26
+ it('normalizes GitHub PR opened to pr-opened', () => {
27
+ const body = {
28
+ action: 'opened',
29
+ pull_request: { number: 42, head: { ref: 'feat/login' } },
30
+ repository: { full_name: 'acme/app' },
31
+ sender: { login: 'bob' }
32
+ };
33
+ const event = normalizeWebhookEvent(body, 'acme');
34
+
35
+ assert.equal(event.type, 'pr-opened');
36
+ assert.equal(event.source.kind, 'PullRequest');
37
+ assert.equal(event.source.name, '42');
38
+ assert.equal(event.repository, 'acme/app');
39
+ assert.equal(event.ref, 'feat/login');
40
+ assert.equal(event.actor, 'bob');
41
+ assert.equal(event.payload, body);
42
+ });
43
+
44
+ // 3. GitHub issue comment -> type: 'comment' with kind 'Issue'
45
+ it('normalizes GitHub issue comment to comment with kind Issue', () => {
46
+ const body = {
47
+ action: 'created',
48
+ comment: { body: 'Looks good!', user: { login: 'reviewer' } },
49
+ issue: { number: 10 },
50
+ repository: { full_name: 'acme/app' },
51
+ sender: { login: 'reviewer' }
52
+ };
53
+ const event = normalizeWebhookEvent(body, 'acme');
54
+
55
+ assert.equal(event.type, 'comment');
56
+ assert.equal(event.source.kind, 'Issue');
57
+ assert.equal(event.source.name, '10');
58
+ assert.equal(event.repository, 'acme/app');
59
+ assert.equal(event.actor, 'reviewer');
60
+ assert.deepEqual(event.payload, { body: 'Looks good!' });
61
+ });
62
+
63
+ // 4. GitHub PR comment -> type: 'comment' with kind 'PullRequest'
64
+ it('normalizes GitHub PR comment to comment with kind PullRequest', () => {
65
+ const body = {
66
+ action: 'created',
67
+ comment: { body: 'LGTM', user: { login: 'reviewer' } },
68
+ issue: { number: 7, pull_request: { url: 'https://api.github.com/repos/acme/app/pulls/7' } },
69
+ repository: { full_name: 'acme/app' },
70
+ sender: { login: 'reviewer' }
71
+ };
72
+ const event = normalizeWebhookEvent(body, 'acme');
73
+
74
+ assert.equal(event.type, 'comment');
75
+ assert.equal(event.source.kind, 'PullRequest');
76
+ assert.equal(event.source.name, '7');
77
+ assert.equal(event.repository, 'acme/app');
78
+ assert.equal(event.actor, 'reviewer');
79
+ assert.deepEqual(event.payload, { body: 'LGTM' });
80
+ });
81
+
82
+ // 5. GitHub label added -> type: 'label-added'
83
+ it('normalizes GitHub label added to label-added', () => {
84
+ const body = {
85
+ action: 'labeled',
86
+ label: { name: 'bug' },
87
+ issue: { number: 15 },
88
+ repository: { full_name: 'acme/app' },
89
+ sender: { login: 'triager' }
90
+ };
91
+ const event = normalizeWebhookEvent(body, 'acme');
92
+
93
+ assert.equal(event.type, 'label-added');
94
+ assert.equal(event.source.kind, 'Issue');
95
+ assert.equal(event.source.name, '15');
96
+ assert.equal(event.repository, 'acme/app');
97
+ assert.equal(event.actor, 'triager');
98
+ assert.deepEqual(event.payload, { label: 'bug' });
99
+ });
100
+
101
+ // 6. GitHub push -> type: 'push'
102
+ it('normalizes GitHub push to push', () => {
103
+ const body = {
104
+ ref: 'refs/heads/main',
105
+ commits: [{ id: 'abc123', message: 'fix typo' }],
106
+ repository: { full_name: 'acme/app' },
107
+ sender: { login: 'dev' },
108
+ pusher: { name: 'dev' }
109
+ };
110
+ const event = normalizeWebhookEvent(body, 'acme');
111
+
112
+ assert.equal(event.type, 'push');
113
+ assert.equal(event.source.kind, 'Repository');
114
+ assert.equal(event.source.name, 'acme/app');
115
+ assert.equal(event.repository, 'acme/app');
116
+ assert.equal(event.ref, 'main');
117
+ assert.equal(event.actor, 'dev');
118
+ assert.equal(event.payload, body);
119
+ });
120
+
121
+ // 7. Unknown payload -> type: 'webhook' (fallback)
122
+ it('returns webhook fallback for unknown payload', () => {
123
+ const body = {
124
+ action: 'some-unknown-action',
125
+ repository: { full_name: 'acme/app' },
126
+ sender: { login: 'bot' }
127
+ };
128
+ const event = normalizeWebhookEvent(body, 'acme');
129
+
130
+ assert.equal(event.type, 'webhook');
131
+ assert.equal(event.source.kind, 'WebhookDelivery');
132
+ assert.equal(event.source.name, 'unknown');
133
+ assert.equal(event.repository, 'acme/app');
134
+ assert.equal(event.actor, 'bot');
135
+ assert.equal(event.payload, body);
136
+ });
137
+
138
+ // 8. Empty/minimal payload -> doesn't crash
139
+ it('handles empty payload without crashing', () => {
140
+ const event = normalizeWebhookEvent({}, 'acme');
141
+
142
+ assert.equal(event.type, 'webhook');
143
+ assert.equal(event.source.kind, 'WebhookDelivery');
144
+ assert.equal(event.source.name, 'unknown');
145
+ assert.equal(event.repository, '');
146
+ assert.equal(event.actor, 'system');
147
+ assert.equal(event.ref, 'main');
148
+ });
149
+
150
+ it('handles minimal payload with only action without crashing', () => {
151
+ const event = normalizeWebhookEvent({ action: 'opened' }, 'acme');
152
+
153
+ assert.equal(event.type, 'webhook');
154
+ assert.equal(event.source.kind, 'WebhookDelivery');
155
+ });
156
+
157
+ // Additional: label on PR (not issue)
158
+ it('normalizes label on PR with kind PullRequest', () => {
159
+ const body = {
160
+ action: 'labeled',
161
+ label: { name: 'ready-to-merge' },
162
+ pull_request: { number: 99 },
163
+ repository: { full_name: 'acme/app' },
164
+ sender: { login: 'lead' }
165
+ };
166
+ const event = normalizeWebhookEvent(body, 'acme');
167
+
168
+ assert.equal(event.type, 'label-added');
169
+ assert.equal(event.source.kind, 'PullRequest');
170
+ assert.equal(event.source.name, '99');
171
+ assert.deepEqual(event.payload, { label: 'ready-to-merge' });
172
+ });
173
+
174
+ // Additional: issue opened (not PR)
175
+ it('normalizes issue opened to issue-created', () => {
176
+ const body = {
177
+ action: 'opened',
178
+ issue: { number: 33 },
179
+ repository: { full_name: 'acme/app' },
180
+ sender: { login: 'reporter' }
181
+ };
182
+ const event = normalizeWebhookEvent(body, 'acme');
183
+
184
+ assert.equal(event.type, 'issue-created');
185
+ assert.equal(event.source.kind, 'Issue');
186
+ assert.equal(event.source.name, '33');
187
+ assert.equal(event.repository, 'acme/app');
188
+ assert.equal(event.actor, 'reporter');
189
+ });
190
+ });